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 *);
255 static inline int sysfs_slab_add(struct kmem_cache *s) { return 0; }
256 static inline int sysfs_slab_alias(struct kmem_cache *s, const char *p)
258 static inline void sysfs_slab_remove(struct kmem_cache *s)
265 static inline void stat(struct kmem_cache_cpu *c, enum stat_item si)
267 #ifdef CONFIG_SLUB_STATS
272 /********************************************************************
273 * Core slab cache functions
274 *******************************************************************/
276 int slab_is_available(void)
278 return slab_state >= UP;
281 static inline struct kmem_cache_node *get_node(struct kmem_cache *s, int node)
284 return s->node[node];
286 return &s->local_node;
290 static inline struct kmem_cache_cpu *get_cpu_slab(struct kmem_cache *s, int cpu)
293 return s->cpu_slab[cpu];
300 * The end pointer in a slab is special. It points to the first object in the
301 * slab but has bit 0 set to mark it.
303 * Note that SLUB relies on page_mapping returning NULL for pages with bit 0
304 * in the mapping set.
306 static inline int is_end(void *addr)
308 return (unsigned long)addr & PAGE_MAPPING_ANON;
311 static void *slab_address(struct page *page)
313 return page->end - PAGE_MAPPING_ANON;
316 static inline int check_valid_pointer(struct kmem_cache *s,
317 struct page *page, const void *object)
321 if (object == page->end)
324 base = slab_address(page);
325 if (object < base || object >= base + s->objects * s->size ||
326 (object - base) % s->size) {
334 * Slow version of get and set free pointer.
336 * This version requires touching the cache lines of kmem_cache which
337 * we avoid to do in the fast alloc free paths. There we obtain the offset
338 * from the page struct.
340 static inline void *get_freepointer(struct kmem_cache *s, void *object)
342 return *(void **)(object + s->offset);
345 static inline void set_freepointer(struct kmem_cache *s, void *object, void *fp)
347 *(void **)(object + s->offset) = fp;
350 /* Loop over all objects in a slab */
351 #define for_each_object(__p, __s, __addr) \
352 for (__p = (__addr); __p < (__addr) + (__s)->objects * (__s)->size;\
356 #define for_each_free_object(__p, __s, __free) \
357 for (__p = (__free); (__p) != page->end; __p = get_freepointer((__s),\
360 /* Determine object index from a given position */
361 static inline int slab_index(void *p, struct kmem_cache *s, void *addr)
363 return (p - addr) / s->size;
366 #ifdef CONFIG_SLUB_DEBUG
370 #ifdef CONFIG_SLUB_DEBUG_ON
371 static int slub_debug = DEBUG_DEFAULT_FLAGS;
373 static int slub_debug;
376 static char *slub_debug_slabs;
381 static void print_section(char *text, u8 *addr, unsigned int length)
389 for (i = 0; i < length; i++) {
391 printk(KERN_ERR "%8s 0x%p: ", text, addr + i);
394 printk(KERN_CONT " %02x", addr[i]);
396 ascii[offset] = isgraph(addr[i]) ? addr[i] : '.';
398 printk(KERN_CONT " %s\n", ascii);
405 printk(KERN_CONT " ");
409 printk(KERN_CONT " %s\n", ascii);
413 static struct track *get_track(struct kmem_cache *s, void *object,
414 enum track_item alloc)
419 p = object + s->offset + sizeof(void *);
421 p = object + s->inuse;
426 static void set_track(struct kmem_cache *s, void *object,
427 enum track_item alloc, void *addr)
432 p = object + s->offset + sizeof(void *);
434 p = object + s->inuse;
439 p->cpu = smp_processor_id();
440 p->pid = current ? current->pid : -1;
443 memset(p, 0, sizeof(struct track));
446 static void init_tracking(struct kmem_cache *s, void *object)
448 if (!(s->flags & SLAB_STORE_USER))
451 set_track(s, object, TRACK_FREE, NULL);
452 set_track(s, object, TRACK_ALLOC, NULL);
455 static void print_track(const char *s, struct track *t)
460 printk(KERN_ERR "INFO: %s in ", s);
461 __print_symbol("%s", (unsigned long)t->addr);
462 printk(" age=%lu cpu=%u pid=%d\n", jiffies - t->when, t->cpu, t->pid);
465 static void print_tracking(struct kmem_cache *s, void *object)
467 if (!(s->flags & SLAB_STORE_USER))
470 print_track("Allocated", get_track(s, object, TRACK_ALLOC));
471 print_track("Freed", get_track(s, object, TRACK_FREE));
474 static void print_page_info(struct page *page)
476 printk(KERN_ERR "INFO: Slab 0x%p used=%u fp=0x%p flags=0x%04lx\n",
477 page, page->inuse, page->freelist, page->flags);
481 static void slab_bug(struct kmem_cache *s, char *fmt, ...)
487 vsnprintf(buf, sizeof(buf), fmt, args);
489 printk(KERN_ERR "========================================"
490 "=====================================\n");
491 printk(KERN_ERR "BUG %s: %s\n", s->name, buf);
492 printk(KERN_ERR "----------------------------------------"
493 "-------------------------------------\n\n");
496 static void slab_fix(struct kmem_cache *s, char *fmt, ...)
502 vsnprintf(buf, sizeof(buf), fmt, args);
504 printk(KERN_ERR "FIX %s: %s\n", s->name, buf);
507 static void print_trailer(struct kmem_cache *s, struct page *page, u8 *p)
509 unsigned int off; /* Offset of last byte */
510 u8 *addr = slab_address(page);
512 print_tracking(s, p);
514 print_page_info(page);
516 printk(KERN_ERR "INFO: Object 0x%p @offset=%tu fp=0x%p\n\n",
517 p, p - addr, get_freepointer(s, p));
520 print_section("Bytes b4", p - 16, 16);
522 print_section("Object", p, min(s->objsize, 128));
524 if (s->flags & SLAB_RED_ZONE)
525 print_section("Redzone", p + s->objsize,
526 s->inuse - s->objsize);
529 off = s->offset + sizeof(void *);
533 if (s->flags & SLAB_STORE_USER)
534 off += 2 * sizeof(struct track);
537 /* Beginning of the filler is the free pointer */
538 print_section("Padding", p + off, s->size - off);
543 static void object_err(struct kmem_cache *s, struct page *page,
544 u8 *object, char *reason)
547 print_trailer(s, page, object);
550 static void slab_err(struct kmem_cache *s, struct page *page, char *fmt, ...)
556 vsnprintf(buf, sizeof(buf), fmt, args);
559 print_page_info(page);
563 static void init_object(struct kmem_cache *s, void *object, int active)
567 if (s->flags & __OBJECT_POISON) {
568 memset(p, POISON_FREE, s->objsize - 1);
569 p[s->objsize - 1] = POISON_END;
572 if (s->flags & SLAB_RED_ZONE)
573 memset(p + s->objsize,
574 active ? SLUB_RED_ACTIVE : SLUB_RED_INACTIVE,
575 s->inuse - s->objsize);
578 static u8 *check_bytes(u8 *start, unsigned int value, unsigned int bytes)
581 if (*start != (u8)value)
589 static void restore_bytes(struct kmem_cache *s, char *message, u8 data,
590 void *from, void *to)
592 slab_fix(s, "Restoring 0x%p-0x%p=0x%x\n", from, to - 1, data);
593 memset(from, data, to - from);
596 static int check_bytes_and_report(struct kmem_cache *s, struct page *page,
597 u8 *object, char *what,
598 u8 *start, unsigned int value, unsigned int bytes)
603 fault = check_bytes(start, value, bytes);
608 while (end > fault && end[-1] == value)
611 slab_bug(s, "%s overwritten", what);
612 printk(KERN_ERR "INFO: 0x%p-0x%p. First byte 0x%x instead of 0x%x\n",
613 fault, end - 1, fault[0], value);
614 print_trailer(s, page, object);
616 restore_bytes(s, what, value, fault, end);
624 * Bytes of the object to be managed.
625 * If the freepointer may overlay the object then the free
626 * pointer is the first word of the object.
628 * Poisoning uses 0x6b (POISON_FREE) and the last byte is
631 * object + s->objsize
632 * Padding to reach word boundary. This is also used for Redzoning.
633 * Padding is extended by another word if Redzoning is enabled and
636 * We fill with 0xbb (RED_INACTIVE) for inactive objects and with
637 * 0xcc (RED_ACTIVE) for objects in use.
640 * Meta data starts here.
642 * A. Free pointer (if we cannot overwrite object on free)
643 * B. Tracking data for SLAB_STORE_USER
644 * C. Padding to reach required alignment boundary or at mininum
645 * one word if debuggin is on to be able to detect writes
646 * before the word boundary.
648 * Padding is done using 0x5a (POISON_INUSE)
651 * Nothing is used beyond s->size.
653 * If slabcaches are merged then the objsize and inuse boundaries are mostly
654 * ignored. And therefore no slab options that rely on these boundaries
655 * may be used with merged slabcaches.
658 static int check_pad_bytes(struct kmem_cache *s, struct page *page, u8 *p)
660 unsigned long off = s->inuse; /* The end of info */
663 /* Freepointer is placed after the object. */
664 off += sizeof(void *);
666 if (s->flags & SLAB_STORE_USER)
667 /* We also have user information there */
668 off += 2 * sizeof(struct track);
673 return check_bytes_and_report(s, page, p, "Object padding",
674 p + off, POISON_INUSE, s->size - off);
677 static int slab_pad_check(struct kmem_cache *s, struct page *page)
685 if (!(s->flags & SLAB_POISON))
688 start = slab_address(page);
689 end = start + (PAGE_SIZE << s->order);
690 length = s->objects * s->size;
691 remainder = end - (start + length);
695 fault = check_bytes(start + length, POISON_INUSE, remainder);
698 while (end > fault && end[-1] == POISON_INUSE)
701 slab_err(s, page, "Padding overwritten. 0x%p-0x%p", fault, end - 1);
702 print_section("Padding", start, length);
704 restore_bytes(s, "slab padding", POISON_INUSE, start, end);
708 static int check_object(struct kmem_cache *s, struct page *page,
709 void *object, int active)
712 u8 *endobject = object + s->objsize;
714 if (s->flags & SLAB_RED_ZONE) {
716 active ? SLUB_RED_ACTIVE : SLUB_RED_INACTIVE;
718 if (!check_bytes_and_report(s, page, object, "Redzone",
719 endobject, red, s->inuse - s->objsize))
722 if ((s->flags & SLAB_POISON) && s->objsize < s->inuse) {
723 check_bytes_and_report(s, page, p, "Alignment padding",
724 endobject, POISON_INUSE, s->inuse - s->objsize);
728 if (s->flags & SLAB_POISON) {
729 if (!active && (s->flags & __OBJECT_POISON) &&
730 (!check_bytes_and_report(s, page, p, "Poison", p,
731 POISON_FREE, s->objsize - 1) ||
732 !check_bytes_and_report(s, page, p, "Poison",
733 p + s->objsize - 1, POISON_END, 1)))
736 * check_pad_bytes cleans up on its own.
738 check_pad_bytes(s, page, p);
741 if (!s->offset && active)
743 * Object and freepointer overlap. Cannot check
744 * freepointer while object is allocated.
748 /* Check free pointer validity */
749 if (!check_valid_pointer(s, page, get_freepointer(s, p))) {
750 object_err(s, page, p, "Freepointer corrupt");
752 * No choice but to zap it and thus loose the remainder
753 * of the free objects in this slab. May cause
754 * another error because the object count is now wrong.
756 set_freepointer(s, p, page->end);
762 static int check_slab(struct kmem_cache *s, struct page *page)
764 VM_BUG_ON(!irqs_disabled());
766 if (!PageSlab(page)) {
767 slab_err(s, page, "Not a valid slab page");
770 if (page->inuse > s->objects) {
771 slab_err(s, page, "inuse %u > max %u",
772 s->name, page->inuse, s->objects);
775 /* Slab_pad_check fixes things up after itself */
776 slab_pad_check(s, page);
781 * Determine if a certain object on a page is on the freelist. Must hold the
782 * slab lock to guarantee that the chains are in a consistent state.
784 static int on_freelist(struct kmem_cache *s, struct page *page, void *search)
787 void *fp = page->freelist;
790 while (fp != page->end && nr <= s->objects) {
793 if (!check_valid_pointer(s, page, fp)) {
795 object_err(s, page, object,
796 "Freechain corrupt");
797 set_freepointer(s, object, page->end);
800 slab_err(s, page, "Freepointer corrupt");
801 page->freelist = page->end;
802 page->inuse = s->objects;
803 slab_fix(s, "Freelist cleared");
809 fp = get_freepointer(s, object);
813 if (page->inuse != s->objects - nr) {
814 slab_err(s, page, "Wrong object count. Counter is %d but "
815 "counted were %d", page->inuse, s->objects - nr);
816 page->inuse = s->objects - nr;
817 slab_fix(s, "Object count adjusted.");
819 return search == NULL;
822 static void trace(struct kmem_cache *s, struct page *page, void *object, int alloc)
824 if (s->flags & SLAB_TRACE) {
825 printk(KERN_INFO "TRACE %s %s 0x%p inuse=%d fp=0x%p\n",
827 alloc ? "alloc" : "free",
832 print_section("Object", (void *)object, s->objsize);
839 * Tracking of fully allocated slabs for debugging purposes.
841 static void add_full(struct kmem_cache_node *n, struct page *page)
843 spin_lock(&n->list_lock);
844 list_add(&page->lru, &n->full);
845 spin_unlock(&n->list_lock);
848 static void remove_full(struct kmem_cache *s, struct page *page)
850 struct kmem_cache_node *n;
852 if (!(s->flags & SLAB_STORE_USER))
855 n = get_node(s, page_to_nid(page));
857 spin_lock(&n->list_lock);
858 list_del(&page->lru);
859 spin_unlock(&n->list_lock);
862 static void setup_object_debug(struct kmem_cache *s, struct page *page,
865 if (!(s->flags & (SLAB_STORE_USER|SLAB_RED_ZONE|__OBJECT_POISON)))
868 init_object(s, object, 0);
869 init_tracking(s, object);
872 static int alloc_debug_processing(struct kmem_cache *s, struct page *page,
873 void *object, void *addr)
875 if (!check_slab(s, page))
878 if (object && !on_freelist(s, page, object)) {
879 object_err(s, page, object, "Object already allocated");
883 if (!check_valid_pointer(s, page, object)) {
884 object_err(s, page, object, "Freelist Pointer check fails");
888 if (object && !check_object(s, page, object, 0))
891 /* Success perform special debug activities for allocs */
892 if (s->flags & SLAB_STORE_USER)
893 set_track(s, object, TRACK_ALLOC, addr);
894 trace(s, page, object, 1);
895 init_object(s, object, 1);
899 if (PageSlab(page)) {
901 * If this is a slab page then lets do the best we can
902 * to avoid issues in the future. Marking all objects
903 * as used avoids touching the remaining objects.
905 slab_fix(s, "Marking all objects used");
906 page->inuse = s->objects;
907 page->freelist = page->end;
912 static int free_debug_processing(struct kmem_cache *s, struct page *page,
913 void *object, void *addr)
915 if (!check_slab(s, page))
918 if (!check_valid_pointer(s, page, object)) {
919 slab_err(s, page, "Invalid object pointer 0x%p", object);
923 if (on_freelist(s, page, object)) {
924 object_err(s, page, object, "Object already free");
928 if (!check_object(s, page, object, 1))
931 if (unlikely(s != page->slab)) {
932 if (!PageSlab(page)) {
933 slab_err(s, page, "Attempt to free object(0x%p) "
934 "outside of slab", object);
935 } else if (!page->slab) {
937 "SLUB <none>: no slab for object 0x%p.\n",
941 object_err(s, page, object,
942 "page slab pointer corrupt.");
946 /* Special debug activities for freeing objects */
947 if (!SlabFrozen(page) && page->freelist == page->end)
948 remove_full(s, page);
949 if (s->flags & SLAB_STORE_USER)
950 set_track(s, object, TRACK_FREE, addr);
951 trace(s, page, object, 0);
952 init_object(s, object, 0);
956 slab_fix(s, "Object at 0x%p not freed", object);
960 static int __init setup_slub_debug(char *str)
962 slub_debug = DEBUG_DEFAULT_FLAGS;
963 if (*str++ != '=' || !*str)
965 * No options specified. Switch on full debugging.
971 * No options but restriction on slabs. This means full
972 * debugging for slabs matching a pattern.
979 * Switch off all debugging measures.
984 * Determine which debug features should be switched on
986 for (; *str && *str != ','; str++) {
987 switch (tolower(*str)) {
989 slub_debug |= SLAB_DEBUG_FREE;
992 slub_debug |= SLAB_RED_ZONE;
995 slub_debug |= SLAB_POISON;
998 slub_debug |= SLAB_STORE_USER;
1001 slub_debug |= SLAB_TRACE;
1004 printk(KERN_ERR "slub_debug option '%c' "
1005 "unknown. skipped\n", *str);
1011 slub_debug_slabs = str + 1;
1016 __setup("slub_debug", setup_slub_debug);
1018 static unsigned long kmem_cache_flags(unsigned long objsize,
1019 unsigned long flags, const char *name,
1020 void (*ctor)(struct kmem_cache *, void *))
1023 * The page->offset field is only 16 bit wide. This is an offset
1024 * in units of words from the beginning of an object. If the slab
1025 * size is bigger then we cannot move the free pointer behind the
1028 * On 32 bit platforms the limit is 256k. On 64bit platforms
1029 * the limit is 512k.
1031 * Debugging or ctor may create a need to move the free
1032 * pointer. Fail if this happens.
1034 if (objsize >= 65535 * sizeof(void *)) {
1035 BUG_ON(flags & (SLAB_RED_ZONE | SLAB_POISON |
1036 SLAB_STORE_USER | SLAB_DESTROY_BY_RCU));
1040 * Enable debugging if selected on the kernel commandline.
1042 if (slub_debug && (!slub_debug_slabs ||
1043 strncmp(slub_debug_slabs, name,
1044 strlen(slub_debug_slabs)) == 0))
1045 flags |= slub_debug;
1051 static inline void setup_object_debug(struct kmem_cache *s,
1052 struct page *page, void *object) {}
1054 static inline int alloc_debug_processing(struct kmem_cache *s,
1055 struct page *page, void *object, void *addr) { return 0; }
1057 static inline int free_debug_processing(struct kmem_cache *s,
1058 struct page *page, void *object, void *addr) { return 0; }
1060 static inline int slab_pad_check(struct kmem_cache *s, struct page *page)
1062 static inline int check_object(struct kmem_cache *s, struct page *page,
1063 void *object, int active) { return 1; }
1064 static inline void add_full(struct kmem_cache_node *n, struct page *page) {}
1065 static inline unsigned long kmem_cache_flags(unsigned long objsize,
1066 unsigned long flags, const char *name,
1067 void (*ctor)(struct kmem_cache *, void *))
1071 #define slub_debug 0
1074 * Slab allocation and freeing
1076 static struct page *allocate_slab(struct kmem_cache *s, gfp_t flags, int node)
1079 int pages = 1 << s->order;
1081 flags |= s->allocflags;
1084 page = alloc_pages(flags, s->order);
1086 page = alloc_pages_node(node, flags, s->order);
1091 mod_zone_page_state(page_zone(page),
1092 (s->flags & SLAB_RECLAIM_ACCOUNT) ?
1093 NR_SLAB_RECLAIMABLE : NR_SLAB_UNRECLAIMABLE,
1099 static void setup_object(struct kmem_cache *s, struct page *page,
1102 setup_object_debug(s, page, object);
1103 if (unlikely(s->ctor))
1107 static struct page *new_slab(struct kmem_cache *s, gfp_t flags, int node)
1110 struct kmem_cache_node *n;
1115 BUG_ON(flags & GFP_SLAB_BUG_MASK);
1117 page = allocate_slab(s,
1118 flags & (GFP_RECLAIM_MASK | GFP_CONSTRAINT_MASK), node);
1122 n = get_node(s, page_to_nid(page));
1124 atomic_long_inc(&n->nr_slabs);
1126 page->flags |= 1 << PG_slab;
1127 if (s->flags & (SLAB_DEBUG_FREE | SLAB_RED_ZONE | SLAB_POISON |
1128 SLAB_STORE_USER | SLAB_TRACE))
1131 start = page_address(page);
1132 page->end = start + 1;
1134 if (unlikely(s->flags & SLAB_POISON))
1135 memset(start, POISON_INUSE, PAGE_SIZE << s->order);
1138 for_each_object(p, s, start) {
1139 setup_object(s, page, last);
1140 set_freepointer(s, last, p);
1143 setup_object(s, page, last);
1144 set_freepointer(s, last, page->end);
1146 page->freelist = start;
1152 static void __free_slab(struct kmem_cache *s, struct page *page)
1154 int pages = 1 << s->order;
1156 if (unlikely(SlabDebug(page))) {
1159 slab_pad_check(s, page);
1160 for_each_object(p, s, slab_address(page))
1161 check_object(s, page, p, 0);
1162 ClearSlabDebug(page);
1165 mod_zone_page_state(page_zone(page),
1166 (s->flags & SLAB_RECLAIM_ACCOUNT) ?
1167 NR_SLAB_RECLAIMABLE : NR_SLAB_UNRECLAIMABLE,
1170 page->mapping = NULL;
1171 __free_pages(page, s->order);
1174 static void rcu_free_slab(struct rcu_head *h)
1178 page = container_of((struct list_head *)h, struct page, lru);
1179 __free_slab(page->slab, page);
1182 static void free_slab(struct kmem_cache *s, struct page *page)
1184 if (unlikely(s->flags & SLAB_DESTROY_BY_RCU)) {
1186 * RCU free overloads the RCU head over the LRU
1188 struct rcu_head *head = (void *)&page->lru;
1190 call_rcu(head, rcu_free_slab);
1192 __free_slab(s, page);
1195 static void discard_slab(struct kmem_cache *s, struct page *page)
1197 struct kmem_cache_node *n = get_node(s, page_to_nid(page));
1199 atomic_long_dec(&n->nr_slabs);
1200 reset_page_mapcount(page);
1201 __ClearPageSlab(page);
1206 * Per slab locking using the pagelock
1208 static __always_inline void slab_lock(struct page *page)
1210 bit_spin_lock(PG_locked, &page->flags);
1213 static __always_inline void slab_unlock(struct page *page)
1215 __bit_spin_unlock(PG_locked, &page->flags);
1218 static __always_inline int slab_trylock(struct page *page)
1222 rc = bit_spin_trylock(PG_locked, &page->flags);
1227 * Management of partially allocated slabs
1229 static void add_partial(struct kmem_cache_node *n,
1230 struct page *page, int tail)
1232 spin_lock(&n->list_lock);
1235 list_add_tail(&page->lru, &n->partial);
1237 list_add(&page->lru, &n->partial);
1238 spin_unlock(&n->list_lock);
1241 static void remove_partial(struct kmem_cache *s,
1244 struct kmem_cache_node *n = get_node(s, page_to_nid(page));
1246 spin_lock(&n->list_lock);
1247 list_del(&page->lru);
1249 spin_unlock(&n->list_lock);
1253 * Lock slab and remove from the partial list.
1255 * Must hold list_lock.
1257 static inline int lock_and_freeze_slab(struct kmem_cache_node *n, struct page *page)
1259 if (slab_trylock(page)) {
1260 list_del(&page->lru);
1262 SetSlabFrozen(page);
1269 * Try to allocate a partial slab from a specific node.
1271 static struct page *get_partial_node(struct kmem_cache_node *n)
1276 * Racy check. If we mistakenly see no partial slabs then we
1277 * just allocate an empty slab. If we mistakenly try to get a
1278 * partial slab and there is none available then get_partials()
1281 if (!n || !n->nr_partial)
1284 spin_lock(&n->list_lock);
1285 list_for_each_entry(page, &n->partial, lru)
1286 if (lock_and_freeze_slab(n, page))
1290 spin_unlock(&n->list_lock);
1295 * Get a page from somewhere. Search in increasing NUMA distances.
1297 static struct page *get_any_partial(struct kmem_cache *s, gfp_t flags)
1300 struct zonelist *zonelist;
1305 * The defrag ratio allows a configuration of the tradeoffs between
1306 * inter node defragmentation and node local allocations. A lower
1307 * defrag_ratio increases the tendency to do local allocations
1308 * instead of attempting to obtain partial slabs from other nodes.
1310 * If the defrag_ratio is set to 0 then kmalloc() always
1311 * returns node local objects. If the ratio is higher then kmalloc()
1312 * may return off node objects because partial slabs are obtained
1313 * from other nodes and filled up.
1315 * If /sys/slab/xx/defrag_ratio is set to 100 (which makes
1316 * defrag_ratio = 1000) then every (well almost) allocation will
1317 * first attempt to defrag slab caches on other nodes. This means
1318 * scanning over all nodes to look for partial slabs which may be
1319 * expensive if we do it every time we are trying to find a slab
1320 * with available objects.
1322 if (!s->remote_node_defrag_ratio ||
1323 get_cycles() % 1024 > s->remote_node_defrag_ratio)
1326 zonelist = &NODE_DATA(
1327 slab_node(current->mempolicy))->node_zonelists[gfp_zone(flags)];
1328 for (z = zonelist->zones; *z; z++) {
1329 struct kmem_cache_node *n;
1331 n = get_node(s, zone_to_nid(*z));
1333 if (n && cpuset_zone_allowed_hardwall(*z, flags) &&
1334 n->nr_partial > MIN_PARTIAL) {
1335 page = get_partial_node(n);
1345 * Get a partial page, lock it and return it.
1347 static struct page *get_partial(struct kmem_cache *s, gfp_t flags, int node)
1350 int searchnode = (node == -1) ? numa_node_id() : node;
1352 page = get_partial_node(get_node(s, searchnode));
1353 if (page || (flags & __GFP_THISNODE))
1356 return get_any_partial(s, flags);
1360 * Move a page back to the lists.
1362 * Must be called with the slab lock held.
1364 * On exit the slab lock will have been dropped.
1366 static void unfreeze_slab(struct kmem_cache *s, struct page *page, int tail)
1368 struct kmem_cache_node *n = get_node(s, page_to_nid(page));
1369 struct kmem_cache_cpu *c = get_cpu_slab(s, smp_processor_id());
1371 ClearSlabFrozen(page);
1374 if (page->freelist != page->end) {
1375 add_partial(n, page, tail);
1376 stat(c, tail ? DEACTIVATE_TO_TAIL : DEACTIVATE_TO_HEAD);
1378 stat(c, DEACTIVATE_FULL);
1379 if (SlabDebug(page) && (s->flags & SLAB_STORE_USER))
1384 stat(c, DEACTIVATE_EMPTY);
1385 if (n->nr_partial < MIN_PARTIAL) {
1387 * Adding an empty slab to the partial slabs in order
1388 * to avoid page allocator overhead. This slab needs
1389 * to come after the other slabs with objects in
1390 * order to fill them up. That way the size of the
1391 * partial list stays small. kmem_cache_shrink can
1392 * reclaim empty slabs from the partial list.
1394 add_partial(n, page, 1);
1398 stat(get_cpu_slab(s, raw_smp_processor_id()), FREE_SLAB);
1399 discard_slab(s, page);
1405 * Remove the cpu slab
1407 static void deactivate_slab(struct kmem_cache *s, struct kmem_cache_cpu *c)
1409 struct page *page = c->page;
1413 stat(c, DEACTIVATE_REMOTE_FREES);
1415 * Merge cpu freelist into freelist. Typically we get here
1416 * because both freelists are empty. So this is unlikely
1419 * We need to use _is_end here because deactivate slab may
1420 * be called for a debug slab. Then c->freelist may contain
1423 while (unlikely(!is_end(c->freelist))) {
1426 tail = 0; /* Hot objects. Put the slab first */
1428 /* Retrieve object from cpu_freelist */
1429 object = c->freelist;
1430 c->freelist = c->freelist[c->offset];
1432 /* And put onto the regular freelist */
1433 object[c->offset] = page->freelist;
1434 page->freelist = object;
1438 unfreeze_slab(s, page, tail);
1441 static inline void flush_slab(struct kmem_cache *s, struct kmem_cache_cpu *c)
1443 stat(c, CPUSLAB_FLUSH);
1445 deactivate_slab(s, c);
1450 * Called from IPI handler with interrupts disabled.
1452 static inline void __flush_cpu_slab(struct kmem_cache *s, int cpu)
1454 struct kmem_cache_cpu *c = get_cpu_slab(s, cpu);
1456 if (likely(c && c->page))
1460 static void flush_cpu_slab(void *d)
1462 struct kmem_cache *s = d;
1464 __flush_cpu_slab(s, smp_processor_id());
1467 static void flush_all(struct kmem_cache *s)
1470 on_each_cpu(flush_cpu_slab, s, 1, 1);
1472 unsigned long flags;
1474 local_irq_save(flags);
1476 local_irq_restore(flags);
1481 * Check if the objects in a per cpu structure fit numa
1482 * locality expectations.
1484 static inline int node_match(struct kmem_cache_cpu *c, int node)
1487 if (node != -1 && c->node != node)
1494 * Slow path. The lockless freelist is empty or we need to perform
1497 * Interrupts are disabled.
1499 * Processing is still very fast if new objects have been freed to the
1500 * regular freelist. In that case we simply take over the regular freelist
1501 * as the lockless freelist and zap the regular freelist.
1503 * If that is not working then we fall back to the partial lists. We take the
1504 * first element of the freelist as the object to allocate now and move the
1505 * rest of the freelist to the lockless freelist.
1507 * And if we were unable to get a new slab from the partial slab lists then
1508 * we need to allocate a new slab. This is slowest path since we may sleep.
1510 static void *__slab_alloc(struct kmem_cache *s,
1511 gfp_t gfpflags, int node, void *addr, struct kmem_cache_cpu *c)
1515 #ifdef SLUB_FASTPATH
1516 unsigned long flags;
1518 local_irq_save(flags);
1524 if (unlikely(!node_match(c, node)))
1526 stat(c, ALLOC_REFILL);
1528 object = c->page->freelist;
1529 if (unlikely(object == c->page->end))
1531 if (unlikely(SlabDebug(c->page)))
1534 object = c->page->freelist;
1535 c->freelist = object[c->offset];
1536 c->page->inuse = s->objects;
1537 c->page->freelist = c->page->end;
1538 c->node = page_to_nid(c->page);
1540 slab_unlock(c->page);
1541 stat(c, ALLOC_SLOWPATH);
1543 #ifdef SLUB_FASTPATH
1544 local_irq_restore(flags);
1549 deactivate_slab(s, c);
1552 new = get_partial(s, gfpflags, node);
1555 stat(c, ALLOC_FROM_PARTIAL);
1559 if (gfpflags & __GFP_WAIT)
1562 new = new_slab(s, gfpflags, node);
1564 if (gfpflags & __GFP_WAIT)
1565 local_irq_disable();
1568 c = get_cpu_slab(s, smp_processor_id());
1569 stat(c, ALLOC_SLAB);
1580 object = c->page->freelist;
1581 if (!alloc_debug_processing(s, c->page, object, addr))
1585 c->page->freelist = object[c->offset];
1591 * Inlined fastpath so that allocation functions (kmalloc, kmem_cache_alloc)
1592 * have the fastpath folded into their functions. So no function call
1593 * overhead for requests that can be satisfied on the fastpath.
1595 * The fastpath works by first checking if the lockless freelist can be used.
1596 * If not then __slab_alloc is called for slow processing.
1598 * Otherwise we can simply pick the next object from the lockless free list.
1600 static __always_inline void *slab_alloc(struct kmem_cache *s,
1601 gfp_t gfpflags, int node, void *addr)
1604 struct kmem_cache_cpu *c;
1607 * The SLUB_FASTPATH path is provisional and is currently disabled if the
1608 * kernel is compiled with preemption or if the arch does not support
1609 * fast cmpxchg operations. There are a couple of coming changes that will
1610 * simplify matters and allow preemption. Ultimately we may end up making
1611 * SLUB_FASTPATH the default.
1613 * 1. The introduction of the per cpu allocator will avoid array lookups
1614 * through get_cpu_slab(). A special register can be used instead.
1616 * 2. The introduction of per cpu atomic operations (cpu_ops) means that
1617 * we can realize the logic here entirely with per cpu atomics. The
1618 * per cpu atomic ops will take care of the preemption issues.
1621 #ifdef SLUB_FASTPATH
1622 c = get_cpu_slab(s, raw_smp_processor_id());
1624 object = c->freelist;
1625 if (unlikely(is_end(object) || !node_match(c, node))) {
1626 object = __slab_alloc(s, gfpflags, node, addr, c);
1629 stat(c, ALLOC_FASTPATH);
1630 } while (cmpxchg_local(&c->freelist, object, object[c->offset])
1633 unsigned long flags;
1635 local_irq_save(flags);
1636 c = get_cpu_slab(s, smp_processor_id());
1637 if (unlikely(is_end(c->freelist) || !node_match(c, node)))
1639 object = __slab_alloc(s, gfpflags, node, addr, c);
1642 object = c->freelist;
1643 c->freelist = object[c->offset];
1644 stat(c, ALLOC_FASTPATH);
1646 local_irq_restore(flags);
1649 if (unlikely((gfpflags & __GFP_ZERO) && object))
1650 memset(object, 0, c->objsize);
1655 void *kmem_cache_alloc(struct kmem_cache *s, gfp_t gfpflags)
1657 return slab_alloc(s, gfpflags, -1, __builtin_return_address(0));
1659 EXPORT_SYMBOL(kmem_cache_alloc);
1662 void *kmem_cache_alloc_node(struct kmem_cache *s, gfp_t gfpflags, int node)
1664 return slab_alloc(s, gfpflags, node, __builtin_return_address(0));
1666 EXPORT_SYMBOL(kmem_cache_alloc_node);
1670 * Slow patch handling. This may still be called frequently since objects
1671 * have a longer lifetime than the cpu slabs in most processing loads.
1673 * So we still attempt to reduce cache line usage. Just take the slab
1674 * lock and free the item. If there is no additional partial page
1675 * handling required then we can return immediately.
1677 static void __slab_free(struct kmem_cache *s, struct page *page,
1678 void *x, void *addr, unsigned int offset)
1681 void **object = (void *)x;
1682 struct kmem_cache_cpu *c;
1684 #ifdef SLUB_FASTPATH
1685 unsigned long flags;
1687 local_irq_save(flags);
1689 c = get_cpu_slab(s, raw_smp_processor_id());
1690 stat(c, FREE_SLOWPATH);
1693 if (unlikely(SlabDebug(page)))
1696 prior = object[offset] = page->freelist;
1697 page->freelist = object;
1700 if (unlikely(SlabFrozen(page))) {
1701 stat(c, FREE_FROZEN);
1705 if (unlikely(!page->inuse))
1709 * Objects left in the slab. If it
1710 * was not on the partial list before
1713 if (unlikely(prior == page->end)) {
1714 add_partial(get_node(s, page_to_nid(page)), page, 1);
1715 stat(c, FREE_ADD_PARTIAL);
1720 #ifdef SLUB_FASTPATH
1721 local_irq_restore(flags);
1726 if (prior != page->end) {
1728 * Slab still on the partial list.
1730 remove_partial(s, page);
1731 stat(c, FREE_REMOVE_PARTIAL);
1735 #ifdef SLUB_FASTPATH
1736 local_irq_restore(flags);
1738 discard_slab(s, page);
1742 if (!free_debug_processing(s, page, x, addr))
1748 * Fastpath with forced inlining to produce a kfree and kmem_cache_free that
1749 * can perform fastpath freeing without additional function calls.
1751 * The fastpath is only possible if we are freeing to the current cpu slab
1752 * of this processor. This typically the case if we have just allocated
1755 * If fastpath is not possible then fall back to __slab_free where we deal
1756 * with all sorts of special processing.
1758 static __always_inline void slab_free(struct kmem_cache *s,
1759 struct page *page, void *x, void *addr)
1761 void **object = (void *)x;
1762 struct kmem_cache_cpu *c;
1764 #ifdef SLUB_FASTPATH
1767 c = get_cpu_slab(s, raw_smp_processor_id());
1768 debug_check_no_locks_freed(object, s->objsize);
1770 freelist = c->freelist;
1773 * If the compiler would reorder the retrieval of c->page to
1774 * come before c->freelist then an interrupt could
1775 * change the cpu slab before we retrieve c->freelist. We
1776 * could be matching on a page no longer active and put the
1777 * object onto the freelist of the wrong slab.
1779 * On the other hand: If we already have the freelist pointer
1780 * then any change of cpu_slab will cause the cmpxchg to fail
1781 * since the freelist pointers are unique per slab.
1783 if (unlikely(page != c->page || c->node < 0)) {
1784 __slab_free(s, page, x, addr, c->offset);
1787 object[c->offset] = freelist;
1788 stat(c, FREE_FASTPATH);
1789 } while (cmpxchg_local(&c->freelist, freelist, object) != freelist);
1791 unsigned long flags;
1793 local_irq_save(flags);
1794 debug_check_no_locks_freed(object, s->objsize);
1795 c = get_cpu_slab(s, smp_processor_id());
1796 if (likely(page == c->page && c->node >= 0)) {
1797 object[c->offset] = c->freelist;
1798 c->freelist = object;
1799 stat(c, FREE_FASTPATH);
1801 __slab_free(s, page, x, addr, c->offset);
1803 local_irq_restore(flags);
1807 void kmem_cache_free(struct kmem_cache *s, void *x)
1811 page = virt_to_head_page(x);
1813 slab_free(s, page, x, __builtin_return_address(0));
1815 EXPORT_SYMBOL(kmem_cache_free);
1817 /* Figure out on which slab object the object resides */
1818 static struct page *get_object_page(const void *x)
1820 struct page *page = virt_to_head_page(x);
1822 if (!PageSlab(page))
1829 * Object placement in a slab is made very easy because we always start at
1830 * offset 0. If we tune the size of the object to the alignment then we can
1831 * get the required alignment by putting one properly sized object after
1834 * Notice that the allocation order determines the sizes of the per cpu
1835 * caches. Each processor has always one slab available for allocations.
1836 * Increasing the allocation order reduces the number of times that slabs
1837 * must be moved on and off the partial lists and is therefore a factor in
1842 * Mininum / Maximum order of slab pages. This influences locking overhead
1843 * and slab fragmentation. A higher order reduces the number of partial slabs
1844 * and increases the number of allocations possible without having to
1845 * take the list_lock.
1847 static int slub_min_order;
1848 static int slub_max_order = DEFAULT_MAX_ORDER;
1849 static int slub_min_objects = DEFAULT_MIN_OBJECTS;
1852 * Merge control. If this is set then no merging of slab caches will occur.
1853 * (Could be removed. This was introduced to pacify the merge skeptics.)
1855 static int slub_nomerge;
1858 * Calculate the order of allocation given an slab object size.
1860 * The order of allocation has significant impact on performance and other
1861 * system components. Generally order 0 allocations should be preferred since
1862 * order 0 does not cause fragmentation in the page allocator. Larger objects
1863 * be problematic to put into order 0 slabs because there may be too much
1864 * unused space left. We go to a higher order if more than 1/8th of the slab
1867 * In order to reach satisfactory performance we must ensure that a minimum
1868 * number of objects is in one slab. Otherwise we may generate too much
1869 * activity on the partial lists which requires taking the list_lock. This is
1870 * less a concern for large slabs though which are rarely used.
1872 * slub_max_order specifies the order where we begin to stop considering the
1873 * number of objects in a slab as critical. If we reach slub_max_order then
1874 * we try to keep the page order as low as possible. So we accept more waste
1875 * of space in favor of a small page order.
1877 * Higher order allocations also allow the placement of more objects in a
1878 * slab and thereby reduce object handling overhead. If the user has
1879 * requested a higher mininum order then we start with that one instead of
1880 * the smallest order which will fit the object.
1882 static inline int slab_order(int size, int min_objects,
1883 int max_order, int fract_leftover)
1887 int min_order = slub_min_order;
1889 for (order = max(min_order,
1890 fls(min_objects * size - 1) - PAGE_SHIFT);
1891 order <= max_order; order++) {
1893 unsigned long slab_size = PAGE_SIZE << order;
1895 if (slab_size < min_objects * size)
1898 rem = slab_size % size;
1900 if (rem <= slab_size / fract_leftover)
1908 static inline int calculate_order(int size)
1915 * Attempt to find best configuration for a slab. This
1916 * works by first attempting to generate a layout with
1917 * the best configuration and backing off gradually.
1919 * First we reduce the acceptable waste in a slab. Then
1920 * we reduce the minimum objects required in a slab.
1922 min_objects = slub_min_objects;
1923 while (min_objects > 1) {
1925 while (fraction >= 4) {
1926 order = slab_order(size, min_objects,
1927 slub_max_order, fraction);
1928 if (order <= slub_max_order)
1936 * We were unable to place multiple objects in a slab. Now
1937 * lets see if we can place a single object there.
1939 order = slab_order(size, 1, slub_max_order, 1);
1940 if (order <= slub_max_order)
1944 * Doh this slab cannot be placed using slub_max_order.
1946 order = slab_order(size, 1, MAX_ORDER, 1);
1947 if (order <= MAX_ORDER)
1953 * Figure out what the alignment of the objects will be.
1955 static unsigned long calculate_alignment(unsigned long flags,
1956 unsigned long align, unsigned long size)
1959 * If the user wants hardware cache aligned objects then
1960 * follow that suggestion if the object is sufficiently
1963 * The hardware cache alignment cannot override the
1964 * specified alignment though. If that is greater
1967 if ((flags & SLAB_HWCACHE_ALIGN) &&
1968 size > cache_line_size() / 2)
1969 return max_t(unsigned long, align, cache_line_size());
1971 if (align < ARCH_SLAB_MINALIGN)
1972 return ARCH_SLAB_MINALIGN;
1974 return ALIGN(align, sizeof(void *));
1977 static void init_kmem_cache_cpu(struct kmem_cache *s,
1978 struct kmem_cache_cpu *c)
1981 c->freelist = (void *)PAGE_MAPPING_ANON;
1983 c->offset = s->offset / sizeof(void *);
1984 c->objsize = s->objsize;
1987 static void init_kmem_cache_node(struct kmem_cache_node *n)
1990 atomic_long_set(&n->nr_slabs, 0);
1991 spin_lock_init(&n->list_lock);
1992 INIT_LIST_HEAD(&n->partial);
1993 #ifdef CONFIG_SLUB_DEBUG
1994 INIT_LIST_HEAD(&n->full);
2000 * Per cpu array for per cpu structures.
2002 * The per cpu array places all kmem_cache_cpu structures from one processor
2003 * close together meaning that it becomes possible that multiple per cpu
2004 * structures are contained in one cacheline. This may be particularly
2005 * beneficial for the kmalloc caches.
2007 * A desktop system typically has around 60-80 slabs. With 100 here we are
2008 * likely able to get per cpu structures for all caches from the array defined
2009 * here. We must be able to cover all kmalloc caches during bootstrap.
2011 * If the per cpu array is exhausted then fall back to kmalloc
2012 * of individual cachelines. No sharing is possible then.
2014 #define NR_KMEM_CACHE_CPU 100
2016 static DEFINE_PER_CPU(struct kmem_cache_cpu,
2017 kmem_cache_cpu)[NR_KMEM_CACHE_CPU];
2019 static DEFINE_PER_CPU(struct kmem_cache_cpu *, kmem_cache_cpu_free);
2020 static cpumask_t kmem_cach_cpu_free_init_once = CPU_MASK_NONE;
2022 static struct kmem_cache_cpu *alloc_kmem_cache_cpu(struct kmem_cache *s,
2023 int cpu, gfp_t flags)
2025 struct kmem_cache_cpu *c = per_cpu(kmem_cache_cpu_free, cpu);
2028 per_cpu(kmem_cache_cpu_free, cpu) =
2029 (void *)c->freelist;
2031 /* Table overflow: So allocate ourselves */
2033 ALIGN(sizeof(struct kmem_cache_cpu), cache_line_size()),
2034 flags, cpu_to_node(cpu));
2039 init_kmem_cache_cpu(s, c);
2043 static void free_kmem_cache_cpu(struct kmem_cache_cpu *c, int cpu)
2045 if (c < per_cpu(kmem_cache_cpu, cpu) ||
2046 c > per_cpu(kmem_cache_cpu, cpu) + NR_KMEM_CACHE_CPU) {
2050 c->freelist = (void *)per_cpu(kmem_cache_cpu_free, cpu);
2051 per_cpu(kmem_cache_cpu_free, cpu) = c;
2054 static void free_kmem_cache_cpus(struct kmem_cache *s)
2058 for_each_online_cpu(cpu) {
2059 struct kmem_cache_cpu *c = get_cpu_slab(s, cpu);
2062 s->cpu_slab[cpu] = NULL;
2063 free_kmem_cache_cpu(c, cpu);
2068 static int alloc_kmem_cache_cpus(struct kmem_cache *s, gfp_t flags)
2072 for_each_online_cpu(cpu) {
2073 struct kmem_cache_cpu *c = get_cpu_slab(s, cpu);
2078 c = alloc_kmem_cache_cpu(s, cpu, flags);
2080 free_kmem_cache_cpus(s);
2083 s->cpu_slab[cpu] = c;
2089 * Initialize the per cpu array.
2091 static void init_alloc_cpu_cpu(int cpu)
2095 if (cpu_isset(cpu, kmem_cach_cpu_free_init_once))
2098 for (i = NR_KMEM_CACHE_CPU - 1; i >= 0; i--)
2099 free_kmem_cache_cpu(&per_cpu(kmem_cache_cpu, cpu)[i], cpu);
2101 cpu_set(cpu, kmem_cach_cpu_free_init_once);
2104 static void __init init_alloc_cpu(void)
2108 for_each_online_cpu(cpu)
2109 init_alloc_cpu_cpu(cpu);
2113 static inline void free_kmem_cache_cpus(struct kmem_cache *s) {}
2114 static inline void init_alloc_cpu(void) {}
2116 static inline int alloc_kmem_cache_cpus(struct kmem_cache *s, gfp_t flags)
2118 init_kmem_cache_cpu(s, &s->cpu_slab);
2125 * No kmalloc_node yet so do it by hand. We know that this is the first
2126 * slab on the node for this slabcache. There are no concurrent accesses
2129 * Note that this function only works on the kmalloc_node_cache
2130 * when allocating for the kmalloc_node_cache. This is used for bootstrapping
2131 * memory on a fresh node that has no slab structures yet.
2133 static struct kmem_cache_node *early_kmem_cache_node_alloc(gfp_t gfpflags,
2137 struct kmem_cache_node *n;
2138 unsigned long flags;
2140 BUG_ON(kmalloc_caches->size < sizeof(struct kmem_cache_node));
2142 page = new_slab(kmalloc_caches, gfpflags, node);
2145 if (page_to_nid(page) != node) {
2146 printk(KERN_ERR "SLUB: Unable to allocate memory from "
2148 printk(KERN_ERR "SLUB: Allocating a useless per node structure "
2149 "in order to be able to continue\n");
2154 page->freelist = get_freepointer(kmalloc_caches, n);
2156 kmalloc_caches->node[node] = n;
2157 #ifdef CONFIG_SLUB_DEBUG
2158 init_object(kmalloc_caches, n, 1);
2159 init_tracking(kmalloc_caches, n);
2161 init_kmem_cache_node(n);
2162 atomic_long_inc(&n->nr_slabs);
2164 * lockdep requires consistent irq usage for each lock
2165 * so even though there cannot be a race this early in
2166 * the boot sequence, we still disable irqs.
2168 local_irq_save(flags);
2169 add_partial(n, page, 0);
2170 local_irq_restore(flags);
2174 static void free_kmem_cache_nodes(struct kmem_cache *s)
2178 for_each_node_state(node, N_NORMAL_MEMORY) {
2179 struct kmem_cache_node *n = s->node[node];
2180 if (n && n != &s->local_node)
2181 kmem_cache_free(kmalloc_caches, n);
2182 s->node[node] = NULL;
2186 static int init_kmem_cache_nodes(struct kmem_cache *s, gfp_t gfpflags)
2191 if (slab_state >= UP)
2192 local_node = page_to_nid(virt_to_page(s));
2196 for_each_node_state(node, N_NORMAL_MEMORY) {
2197 struct kmem_cache_node *n;
2199 if (local_node == node)
2202 if (slab_state == DOWN) {
2203 n = early_kmem_cache_node_alloc(gfpflags,
2207 n = kmem_cache_alloc_node(kmalloc_caches,
2211 free_kmem_cache_nodes(s);
2217 init_kmem_cache_node(n);
2222 static void free_kmem_cache_nodes(struct kmem_cache *s)
2226 static int init_kmem_cache_nodes(struct kmem_cache *s, gfp_t gfpflags)
2228 init_kmem_cache_node(&s->local_node);
2234 * calculate_sizes() determines the order and the distribution of data within
2237 static int calculate_sizes(struct kmem_cache *s)
2239 unsigned long flags = s->flags;
2240 unsigned long size = s->objsize;
2241 unsigned long align = s->align;
2244 * Determine if we can poison the object itself. If the user of
2245 * the slab may touch the object after free or before allocation
2246 * then we should never poison the object itself.
2248 if ((flags & SLAB_POISON) && !(flags & SLAB_DESTROY_BY_RCU) &&
2250 s->flags |= __OBJECT_POISON;
2252 s->flags &= ~__OBJECT_POISON;
2255 * Round up object size to the next word boundary. We can only
2256 * place the free pointer at word boundaries and this determines
2257 * the possible location of the free pointer.
2259 size = ALIGN(size, sizeof(void *));
2261 #ifdef CONFIG_SLUB_DEBUG
2263 * If we are Redzoning then check if there is some space between the
2264 * end of the object and the free pointer. If not then add an
2265 * additional word to have some bytes to store Redzone information.
2267 if ((flags & SLAB_RED_ZONE) && size == s->objsize)
2268 size += sizeof(void *);
2272 * With that we have determined the number of bytes in actual use
2273 * by the object. This is the potential offset to the free pointer.
2277 if (((flags & (SLAB_DESTROY_BY_RCU | SLAB_POISON)) ||
2280 * Relocate free pointer after the object if it is not
2281 * permitted to overwrite the first word of the object on
2284 * This is the case if we do RCU, have a constructor or
2285 * destructor or are poisoning the objects.
2288 size += sizeof(void *);
2291 #ifdef CONFIG_SLUB_DEBUG
2292 if (flags & SLAB_STORE_USER)
2294 * Need to store information about allocs and frees after
2297 size += 2 * sizeof(struct track);
2299 if (flags & SLAB_RED_ZONE)
2301 * Add some empty padding so that we can catch
2302 * overwrites from earlier objects rather than let
2303 * tracking information or the free pointer be
2304 * corrupted if an user writes before the start
2307 size += sizeof(void *);
2311 * Determine the alignment based on various parameters that the
2312 * user specified and the dynamic determination of cache line size
2315 align = calculate_alignment(flags, align, s->objsize);
2318 * SLUB stores one object immediately after another beginning from
2319 * offset 0. In order to align the objects we have to simply size
2320 * each object to conform to the alignment.
2322 size = ALIGN(size, align);
2325 s->order = calculate_order(size);
2331 s->allocflags |= __GFP_COMP;
2333 if (s->flags & SLAB_CACHE_DMA)
2334 s->allocflags |= SLUB_DMA;
2336 if (s->flags & SLAB_RECLAIM_ACCOUNT)
2337 s->allocflags |= __GFP_RECLAIMABLE;
2340 * Determine the number of objects per slab
2342 s->objects = (PAGE_SIZE << s->order) / size;
2344 return !!s->objects;
2348 static int kmem_cache_open(struct kmem_cache *s, gfp_t gfpflags,
2349 const char *name, size_t size,
2350 size_t align, unsigned long flags,
2351 void (*ctor)(struct kmem_cache *, void *))
2353 memset(s, 0, kmem_size);
2358 s->flags = kmem_cache_flags(size, flags, name, ctor);
2360 if (!calculate_sizes(s))
2365 s->remote_node_defrag_ratio = 100;
2367 if (!init_kmem_cache_nodes(s, gfpflags & ~SLUB_DMA))
2370 if (alloc_kmem_cache_cpus(s, gfpflags & ~SLUB_DMA))
2372 free_kmem_cache_nodes(s);
2374 if (flags & SLAB_PANIC)
2375 panic("Cannot create slab %s size=%lu realsize=%u "
2376 "order=%u offset=%u flags=%lx\n",
2377 s->name, (unsigned long)size, s->size, s->order,
2383 * Check if a given pointer is valid
2385 int kmem_ptr_validate(struct kmem_cache *s, const void *object)
2389 page = get_object_page(object);
2391 if (!page || s != page->slab)
2392 /* No slab or wrong slab */
2395 if (!check_valid_pointer(s, page, object))
2399 * We could also check if the object is on the slabs freelist.
2400 * But this would be too expensive and it seems that the main
2401 * purpose of kmem_ptr_valid is to check if the object belongs
2402 * to a certain slab.
2406 EXPORT_SYMBOL(kmem_ptr_validate);
2409 * Determine the size of a slab object
2411 unsigned int kmem_cache_size(struct kmem_cache *s)
2415 EXPORT_SYMBOL(kmem_cache_size);
2417 const char *kmem_cache_name(struct kmem_cache *s)
2421 EXPORT_SYMBOL(kmem_cache_name);
2424 * Attempt to free all slabs on a node. Return the number of slabs we
2425 * were unable to free.
2427 static int free_list(struct kmem_cache *s, struct kmem_cache_node *n,
2428 struct list_head *list)
2430 int slabs_inuse = 0;
2431 unsigned long flags;
2432 struct page *page, *h;
2434 spin_lock_irqsave(&n->list_lock, flags);
2435 list_for_each_entry_safe(page, h, list, lru)
2437 list_del(&page->lru);
2438 discard_slab(s, page);
2441 spin_unlock_irqrestore(&n->list_lock, flags);
2446 * Release all resources used by a slab cache.
2448 static inline int kmem_cache_close(struct kmem_cache *s)
2454 /* Attempt to free all objects */
2455 free_kmem_cache_cpus(s);
2456 for_each_node_state(node, N_NORMAL_MEMORY) {
2457 struct kmem_cache_node *n = get_node(s, node);
2459 n->nr_partial -= free_list(s, n, &n->partial);
2460 if (atomic_long_read(&n->nr_slabs))
2463 free_kmem_cache_nodes(s);
2468 * Close a cache and release the kmem_cache structure
2469 * (must be used for caches created using kmem_cache_create)
2471 void kmem_cache_destroy(struct kmem_cache *s)
2473 down_write(&slub_lock);
2477 up_write(&slub_lock);
2478 if (kmem_cache_close(s))
2480 sysfs_slab_remove(s);
2482 up_write(&slub_lock);
2484 EXPORT_SYMBOL(kmem_cache_destroy);
2486 /********************************************************************
2488 *******************************************************************/
2490 struct kmem_cache kmalloc_caches[PAGE_SHIFT] __cacheline_aligned;
2491 EXPORT_SYMBOL(kmalloc_caches);
2493 #ifdef CONFIG_ZONE_DMA
2494 static struct kmem_cache *kmalloc_caches_dma[PAGE_SHIFT];
2497 static int __init setup_slub_min_order(char *str)
2499 get_option(&str, &slub_min_order);
2504 __setup("slub_min_order=", setup_slub_min_order);
2506 static int __init setup_slub_max_order(char *str)
2508 get_option(&str, &slub_max_order);
2513 __setup("slub_max_order=", setup_slub_max_order);
2515 static int __init setup_slub_min_objects(char *str)
2517 get_option(&str, &slub_min_objects);
2522 __setup("slub_min_objects=", setup_slub_min_objects);
2524 static int __init setup_slub_nomerge(char *str)
2530 __setup("slub_nomerge", setup_slub_nomerge);
2532 static struct kmem_cache *create_kmalloc_cache(struct kmem_cache *s,
2533 const char *name, int size, gfp_t gfp_flags)
2535 unsigned int flags = 0;
2537 if (gfp_flags & SLUB_DMA)
2538 flags = SLAB_CACHE_DMA;
2540 down_write(&slub_lock);
2541 if (!kmem_cache_open(s, gfp_flags, name, size, ARCH_KMALLOC_MINALIGN,
2545 list_add(&s->list, &slab_caches);
2546 up_write(&slub_lock);
2547 if (sysfs_slab_add(s))
2552 panic("Creation of kmalloc slab %s size=%d failed.\n", name, size);
2555 #ifdef CONFIG_ZONE_DMA
2557 static void sysfs_add_func(struct work_struct *w)
2559 struct kmem_cache *s;
2561 down_write(&slub_lock);
2562 list_for_each_entry(s, &slab_caches, list) {
2563 if (s->flags & __SYSFS_ADD_DEFERRED) {
2564 s->flags &= ~__SYSFS_ADD_DEFERRED;
2568 up_write(&slub_lock);
2571 static DECLARE_WORK(sysfs_add_work, sysfs_add_func);
2573 static noinline struct kmem_cache *dma_kmalloc_cache(int index, gfp_t flags)
2575 struct kmem_cache *s;
2579 s = kmalloc_caches_dma[index];
2583 /* Dynamically create dma cache */
2584 if (flags & __GFP_WAIT)
2585 down_write(&slub_lock);
2587 if (!down_write_trylock(&slub_lock))
2591 if (kmalloc_caches_dma[index])
2594 realsize = kmalloc_caches[index].objsize;
2595 text = kasprintf(flags & ~SLUB_DMA, "kmalloc_dma-%d",
2596 (unsigned int)realsize);
2597 s = kmalloc(kmem_size, flags & ~SLUB_DMA);
2599 if (!s || !text || !kmem_cache_open(s, flags, text,
2600 realsize, ARCH_KMALLOC_MINALIGN,
2601 SLAB_CACHE_DMA|__SYSFS_ADD_DEFERRED, NULL)) {
2607 list_add(&s->list, &slab_caches);
2608 kmalloc_caches_dma[index] = s;
2610 schedule_work(&sysfs_add_work);
2613 up_write(&slub_lock);
2615 return kmalloc_caches_dma[index];
2620 * Conversion table for small slabs sizes / 8 to the index in the
2621 * kmalloc array. This is necessary for slabs < 192 since we have non power
2622 * of two cache sizes there. The size of larger slabs can be determined using
2625 static s8 size_index[24] = {
2652 static struct kmem_cache *get_slab(size_t size, gfp_t flags)
2658 return ZERO_SIZE_PTR;
2660 index = size_index[(size - 1) / 8];
2662 index = fls(size - 1);
2664 #ifdef CONFIG_ZONE_DMA
2665 if (unlikely((flags & SLUB_DMA)))
2666 return dma_kmalloc_cache(index, flags);
2669 return &kmalloc_caches[index];
2672 void *__kmalloc(size_t size, gfp_t flags)
2674 struct kmem_cache *s;
2676 if (unlikely(size > PAGE_SIZE / 2))
2677 return kmalloc_large(size, flags);
2679 s = get_slab(size, flags);
2681 if (unlikely(ZERO_OR_NULL_PTR(s)))
2684 return slab_alloc(s, flags, -1, __builtin_return_address(0));
2686 EXPORT_SYMBOL(__kmalloc);
2689 void *__kmalloc_node(size_t size, gfp_t flags, int node)
2691 struct kmem_cache *s;
2693 if (unlikely(size > PAGE_SIZE / 2))
2694 return kmalloc_large(size, flags);
2696 s = get_slab(size, flags);
2698 if (unlikely(ZERO_OR_NULL_PTR(s)))
2701 return slab_alloc(s, flags, node, __builtin_return_address(0));
2703 EXPORT_SYMBOL(__kmalloc_node);
2706 size_t ksize(const void *object)
2709 struct kmem_cache *s;
2712 if (unlikely(object == ZERO_SIZE_PTR))
2715 page = virt_to_head_page(object);
2718 if (unlikely(!PageSlab(page)))
2719 return PAGE_SIZE << compound_order(page);
2725 * Debugging requires use of the padding between object
2726 * and whatever may come after it.
2728 if (s->flags & (SLAB_RED_ZONE | SLAB_POISON))
2732 * If we have the need to store the freelist pointer
2733 * back there or track user information then we can
2734 * only use the space before that information.
2736 if (s->flags & (SLAB_DESTROY_BY_RCU | SLAB_STORE_USER))
2740 * Else we can use all the padding etc for the allocation
2744 EXPORT_SYMBOL(ksize);
2746 void kfree(const void *x)
2749 void *object = (void *)x;
2751 if (unlikely(ZERO_OR_NULL_PTR(x)))
2754 page = virt_to_head_page(x);
2755 if (unlikely(!PageSlab(page))) {
2759 slab_free(page->slab, page, object, __builtin_return_address(0));
2761 EXPORT_SYMBOL(kfree);
2763 static unsigned long count_partial(struct kmem_cache_node *n)
2765 unsigned long flags;
2766 unsigned long x = 0;
2769 spin_lock_irqsave(&n->list_lock, flags);
2770 list_for_each_entry(page, &n->partial, lru)
2772 spin_unlock_irqrestore(&n->list_lock, flags);
2777 * kmem_cache_shrink removes empty slabs from the partial lists and sorts
2778 * the remaining slabs by the number of items in use. The slabs with the
2779 * most items in use come first. New allocations will then fill those up
2780 * and thus they can be removed from the partial lists.
2782 * The slabs with the least items are placed last. This results in them
2783 * being allocated from last increasing the chance that the last objects
2784 * are freed in them.
2786 int kmem_cache_shrink(struct kmem_cache *s)
2790 struct kmem_cache_node *n;
2793 struct list_head *slabs_by_inuse =
2794 kmalloc(sizeof(struct list_head) * s->objects, GFP_KERNEL);
2795 unsigned long flags;
2797 if (!slabs_by_inuse)
2801 for_each_node_state(node, N_NORMAL_MEMORY) {
2802 n = get_node(s, node);
2807 for (i = 0; i < s->objects; i++)
2808 INIT_LIST_HEAD(slabs_by_inuse + i);
2810 spin_lock_irqsave(&n->list_lock, flags);
2813 * Build lists indexed by the items in use in each slab.
2815 * Note that concurrent frees may occur while we hold the
2816 * list_lock. page->inuse here is the upper limit.
2818 list_for_each_entry_safe(page, t, &n->partial, lru) {
2819 if (!page->inuse && slab_trylock(page)) {
2821 * Must hold slab lock here because slab_free
2822 * may have freed the last object and be
2823 * waiting to release the slab.
2825 list_del(&page->lru);
2828 discard_slab(s, page);
2830 list_move(&page->lru,
2831 slabs_by_inuse + page->inuse);
2836 * Rebuild the partial list with the slabs filled up most
2837 * first and the least used slabs at the end.
2839 for (i = s->objects - 1; i >= 0; i--)
2840 list_splice(slabs_by_inuse + i, n->partial.prev);
2842 spin_unlock_irqrestore(&n->list_lock, flags);
2845 kfree(slabs_by_inuse);
2848 EXPORT_SYMBOL(kmem_cache_shrink);
2850 #if defined(CONFIG_NUMA) && defined(CONFIG_MEMORY_HOTPLUG)
2851 static int slab_mem_going_offline_callback(void *arg)
2853 struct kmem_cache *s;
2855 down_read(&slub_lock);
2856 list_for_each_entry(s, &slab_caches, list)
2857 kmem_cache_shrink(s);
2858 up_read(&slub_lock);
2863 static void slab_mem_offline_callback(void *arg)
2865 struct kmem_cache_node *n;
2866 struct kmem_cache *s;
2867 struct memory_notify *marg = arg;
2870 offline_node = marg->status_change_nid;
2873 * If the node still has available memory. we need kmem_cache_node
2876 if (offline_node < 0)
2879 down_read(&slub_lock);
2880 list_for_each_entry(s, &slab_caches, list) {
2881 n = get_node(s, offline_node);
2884 * if n->nr_slabs > 0, slabs still exist on the node
2885 * that is going down. We were unable to free them,
2886 * and offline_pages() function shoudn't call this
2887 * callback. So, we must fail.
2889 BUG_ON(atomic_long_read(&n->nr_slabs));
2891 s->node[offline_node] = NULL;
2892 kmem_cache_free(kmalloc_caches, n);
2895 up_read(&slub_lock);
2898 static int slab_mem_going_online_callback(void *arg)
2900 struct kmem_cache_node *n;
2901 struct kmem_cache *s;
2902 struct memory_notify *marg = arg;
2903 int nid = marg->status_change_nid;
2907 * If the node's memory is already available, then kmem_cache_node is
2908 * already created. Nothing to do.
2914 * We are bringing a node online. No memory is availabe yet. We must
2915 * allocate a kmem_cache_node structure in order to bring the node
2918 down_read(&slub_lock);
2919 list_for_each_entry(s, &slab_caches, list) {
2921 * XXX: kmem_cache_alloc_node will fallback to other nodes
2922 * since memory is not yet available from the node that
2925 n = kmem_cache_alloc(kmalloc_caches, GFP_KERNEL);
2930 init_kmem_cache_node(n);
2934 up_read(&slub_lock);
2938 static int slab_memory_callback(struct notifier_block *self,
2939 unsigned long action, void *arg)
2944 case MEM_GOING_ONLINE:
2945 ret = slab_mem_going_online_callback(arg);
2947 case MEM_GOING_OFFLINE:
2948 ret = slab_mem_going_offline_callback(arg);
2951 case MEM_CANCEL_ONLINE:
2952 slab_mem_offline_callback(arg);
2955 case MEM_CANCEL_OFFLINE:
2959 ret = notifier_from_errno(ret);
2963 #endif /* CONFIG_MEMORY_HOTPLUG */
2965 /********************************************************************
2966 * Basic setup of slabs
2967 *******************************************************************/
2969 void __init kmem_cache_init(void)
2978 * Must first have the slab cache available for the allocations of the
2979 * struct kmem_cache_node's. There is special bootstrap code in
2980 * kmem_cache_open for slab_state == DOWN.
2982 create_kmalloc_cache(&kmalloc_caches[0], "kmem_cache_node",
2983 sizeof(struct kmem_cache_node), GFP_KERNEL);
2984 kmalloc_caches[0].refcount = -1;
2987 hotplug_memory_notifier(slab_memory_callback, 1);
2990 /* Able to allocate the per node structures */
2991 slab_state = PARTIAL;
2993 /* Caches that are not of the two-to-the-power-of size */
2994 if (KMALLOC_MIN_SIZE <= 64) {
2995 create_kmalloc_cache(&kmalloc_caches[1],
2996 "kmalloc-96", 96, GFP_KERNEL);
2999 if (KMALLOC_MIN_SIZE <= 128) {
3000 create_kmalloc_cache(&kmalloc_caches[2],
3001 "kmalloc-192", 192, GFP_KERNEL);
3005 for (i = KMALLOC_SHIFT_LOW; i < PAGE_SHIFT; i++) {
3006 create_kmalloc_cache(&kmalloc_caches[i],
3007 "kmalloc", 1 << i, GFP_KERNEL);
3013 * Patch up the size_index table if we have strange large alignment
3014 * requirements for the kmalloc array. This is only the case for
3015 * mips it seems. The standard arches will not generate any code here.
3017 * Largest permitted alignment is 256 bytes due to the way we
3018 * handle the index determination for the smaller caches.
3020 * Make sure that nothing crazy happens if someone starts tinkering
3021 * around with ARCH_KMALLOC_MINALIGN
3023 BUILD_BUG_ON(KMALLOC_MIN_SIZE > 256 ||
3024 (KMALLOC_MIN_SIZE & (KMALLOC_MIN_SIZE - 1)));
3026 for (i = 8; i < KMALLOC_MIN_SIZE; i += 8)
3027 size_index[(i - 1) / 8] = KMALLOC_SHIFT_LOW;
3031 /* Provide the correct kmalloc names now that the caches are up */
3032 for (i = KMALLOC_SHIFT_LOW; i < PAGE_SHIFT; i++)
3033 kmalloc_caches[i]. name =
3034 kasprintf(GFP_KERNEL, "kmalloc-%d", 1 << i);
3037 register_cpu_notifier(&slab_notifier);
3038 kmem_size = offsetof(struct kmem_cache, cpu_slab) +
3039 nr_cpu_ids * sizeof(struct kmem_cache_cpu *);
3041 kmem_size = sizeof(struct kmem_cache);
3046 "SLUB: Genslabs=%d, HWalign=%d, Order=%d-%d, MinObjects=%d,"
3047 " CPUs=%d, Nodes=%d\n",
3048 caches, cache_line_size(),
3049 slub_min_order, slub_max_order, slub_min_objects,
3050 nr_cpu_ids, nr_node_ids);
3054 * Find a mergeable slab cache
3056 static int slab_unmergeable(struct kmem_cache *s)
3058 if (slub_nomerge || (s->flags & SLUB_NEVER_MERGE))
3065 * We may have set a slab to be unmergeable during bootstrap.
3067 if (s->refcount < 0)
3073 static struct kmem_cache *find_mergeable(size_t size,
3074 size_t align, unsigned long flags, const char *name,
3075 void (*ctor)(struct kmem_cache *, void *))
3077 struct kmem_cache *s;
3079 if (slub_nomerge || (flags & SLUB_NEVER_MERGE))
3085 size = ALIGN(size, sizeof(void *));
3086 align = calculate_alignment(flags, align, size);
3087 size = ALIGN(size, align);
3088 flags = kmem_cache_flags(size, flags, name, NULL);
3090 list_for_each_entry(s, &slab_caches, list) {
3091 if (slab_unmergeable(s))
3097 if ((flags & SLUB_MERGE_SAME) != (s->flags & SLUB_MERGE_SAME))
3100 * Check if alignment is compatible.
3101 * Courtesy of Adrian Drzewiecki
3103 if ((s->size & ~(align - 1)) != s->size)
3106 if (s->size - size >= sizeof(void *))
3114 struct kmem_cache *kmem_cache_create(const char *name, size_t size,
3115 size_t align, unsigned long flags,
3116 void (*ctor)(struct kmem_cache *, void *))
3118 struct kmem_cache *s;
3120 down_write(&slub_lock);
3121 s = find_mergeable(size, align, flags, name, ctor);
3127 * Adjust the object sizes so that we clear
3128 * the complete object on kzalloc.
3130 s->objsize = max(s->objsize, (int)size);
3133 * And then we need to update the object size in the
3134 * per cpu structures
3136 for_each_online_cpu(cpu)
3137 get_cpu_slab(s, cpu)->objsize = s->objsize;
3138 s->inuse = max_t(int, s->inuse, ALIGN(size, sizeof(void *)));
3139 up_write(&slub_lock);
3140 if (sysfs_slab_alias(s, name))
3144 s = kmalloc(kmem_size, GFP_KERNEL);
3146 if (kmem_cache_open(s, GFP_KERNEL, name,
3147 size, align, flags, ctor)) {
3148 list_add(&s->list, &slab_caches);
3149 up_write(&slub_lock);
3150 if (sysfs_slab_add(s))
3156 up_write(&slub_lock);
3159 if (flags & SLAB_PANIC)
3160 panic("Cannot create slabcache %s\n", name);
3165 EXPORT_SYMBOL(kmem_cache_create);
3169 * Use the cpu notifier to insure that the cpu slabs are flushed when
3172 static int __cpuinit slab_cpuup_callback(struct notifier_block *nfb,
3173 unsigned long action, void *hcpu)
3175 long cpu = (long)hcpu;
3176 struct kmem_cache *s;
3177 unsigned long flags;
3180 case CPU_UP_PREPARE:
3181 case CPU_UP_PREPARE_FROZEN:
3182 init_alloc_cpu_cpu(cpu);
3183 down_read(&slub_lock);
3184 list_for_each_entry(s, &slab_caches, list)
3185 s->cpu_slab[cpu] = alloc_kmem_cache_cpu(s, cpu,
3187 up_read(&slub_lock);
3190 case CPU_UP_CANCELED:
3191 case CPU_UP_CANCELED_FROZEN:
3193 case CPU_DEAD_FROZEN:
3194 down_read(&slub_lock);
3195 list_for_each_entry(s, &slab_caches, list) {
3196 struct kmem_cache_cpu *c = get_cpu_slab(s, cpu);
3198 local_irq_save(flags);
3199 __flush_cpu_slab(s, cpu);
3200 local_irq_restore(flags);
3201 free_kmem_cache_cpu(c, cpu);
3202 s->cpu_slab[cpu] = NULL;
3204 up_read(&slub_lock);
3212 static struct notifier_block __cpuinitdata slab_notifier = {
3213 .notifier_call = slab_cpuup_callback
3218 void *__kmalloc_track_caller(size_t size, gfp_t gfpflags, void *caller)
3220 struct kmem_cache *s;
3222 if (unlikely(size > PAGE_SIZE / 2))
3223 return kmalloc_large(size, gfpflags);
3225 s = get_slab(size, gfpflags);
3227 if (unlikely(ZERO_OR_NULL_PTR(s)))
3230 return slab_alloc(s, gfpflags, -1, caller);
3233 void *__kmalloc_node_track_caller(size_t size, gfp_t gfpflags,
3234 int node, void *caller)
3236 struct kmem_cache *s;
3238 if (unlikely(size > PAGE_SIZE / 2))
3239 return kmalloc_large(size, gfpflags);
3241 s = get_slab(size, gfpflags);
3243 if (unlikely(ZERO_OR_NULL_PTR(s)))
3246 return slab_alloc(s, gfpflags, node, caller);
3249 #if defined(CONFIG_SYSFS) && defined(CONFIG_SLUB_DEBUG)
3250 static int validate_slab(struct kmem_cache *s, struct page *page,
3254 void *addr = slab_address(page);
3256 if (!check_slab(s, page) ||
3257 !on_freelist(s, page, NULL))
3260 /* Now we know that a valid freelist exists */
3261 bitmap_zero(map, s->objects);
3263 for_each_free_object(p, s, page->freelist) {
3264 set_bit(slab_index(p, s, addr), map);
3265 if (!check_object(s, page, p, 0))
3269 for_each_object(p, s, addr)
3270 if (!test_bit(slab_index(p, s, addr), map))
3271 if (!check_object(s, page, p, 1))
3276 static void validate_slab_slab(struct kmem_cache *s, struct page *page,
3279 if (slab_trylock(page)) {
3280 validate_slab(s, page, map);
3283 printk(KERN_INFO "SLUB %s: Skipped busy slab 0x%p\n",
3286 if (s->flags & DEBUG_DEFAULT_FLAGS) {
3287 if (!SlabDebug(page))
3288 printk(KERN_ERR "SLUB %s: SlabDebug not set "
3289 "on slab 0x%p\n", s->name, page);
3291 if (SlabDebug(page))
3292 printk(KERN_ERR "SLUB %s: SlabDebug set on "
3293 "slab 0x%p\n", s->name, page);
3297 static int validate_slab_node(struct kmem_cache *s,
3298 struct kmem_cache_node *n, unsigned long *map)
3300 unsigned long count = 0;
3302 unsigned long flags;
3304 spin_lock_irqsave(&n->list_lock, flags);
3306 list_for_each_entry(page, &n->partial, lru) {
3307 validate_slab_slab(s, page, map);
3310 if (count != n->nr_partial)
3311 printk(KERN_ERR "SLUB %s: %ld partial slabs counted but "
3312 "counter=%ld\n", s->name, count, n->nr_partial);
3314 if (!(s->flags & SLAB_STORE_USER))
3317 list_for_each_entry(page, &n->full, lru) {
3318 validate_slab_slab(s, page, map);
3321 if (count != atomic_long_read(&n->nr_slabs))
3322 printk(KERN_ERR "SLUB: %s %ld slabs counted but "
3323 "counter=%ld\n", s->name, count,
3324 atomic_long_read(&n->nr_slabs));
3327 spin_unlock_irqrestore(&n->list_lock, flags);
3331 static long validate_slab_cache(struct kmem_cache *s)
3334 unsigned long count = 0;
3335 unsigned long *map = kmalloc(BITS_TO_LONGS(s->objects) *
3336 sizeof(unsigned long), GFP_KERNEL);
3342 for_each_node_state(node, N_NORMAL_MEMORY) {
3343 struct kmem_cache_node *n = get_node(s, node);
3345 count += validate_slab_node(s, n, map);
3351 #ifdef SLUB_RESILIENCY_TEST
3352 static void resiliency_test(void)
3356 printk(KERN_ERR "SLUB resiliency testing\n");
3357 printk(KERN_ERR "-----------------------\n");
3358 printk(KERN_ERR "A. Corruption after allocation\n");
3360 p = kzalloc(16, GFP_KERNEL);
3362 printk(KERN_ERR "\n1. kmalloc-16: Clobber Redzone/next pointer"
3363 " 0x12->0x%p\n\n", p + 16);
3365 validate_slab_cache(kmalloc_caches + 4);
3367 /* Hmmm... The next two are dangerous */
3368 p = kzalloc(32, GFP_KERNEL);
3369 p[32 + sizeof(void *)] = 0x34;
3370 printk(KERN_ERR "\n2. kmalloc-32: Clobber next pointer/next slab"
3371 " 0x34 -> -0x%p\n", p);
3373 "If allocated object is overwritten then not detectable\n\n");
3375 validate_slab_cache(kmalloc_caches + 5);
3376 p = kzalloc(64, GFP_KERNEL);
3377 p += 64 + (get_cycles() & 0xff) * sizeof(void *);
3379 printk(KERN_ERR "\n3. kmalloc-64: corrupting random byte 0x56->0x%p\n",
3382 "If allocated object is overwritten then not detectable\n\n");
3383 validate_slab_cache(kmalloc_caches + 6);
3385 printk(KERN_ERR "\nB. Corruption after free\n");
3386 p = kzalloc(128, GFP_KERNEL);
3389 printk(KERN_ERR "1. kmalloc-128: Clobber first word 0x78->0x%p\n\n", p);
3390 validate_slab_cache(kmalloc_caches + 7);
3392 p = kzalloc(256, GFP_KERNEL);
3395 printk(KERN_ERR "\n2. kmalloc-256: Clobber 50th byte 0x9a->0x%p\n\n",
3397 validate_slab_cache(kmalloc_caches + 8);
3399 p = kzalloc(512, GFP_KERNEL);
3402 printk(KERN_ERR "\n3. kmalloc-512: Clobber redzone 0xab->0x%p\n\n", p);
3403 validate_slab_cache(kmalloc_caches + 9);
3406 static void resiliency_test(void) {};
3410 * Generate lists of code addresses where slabcache objects are allocated
3415 unsigned long count;
3428 unsigned long count;
3429 struct location *loc;
3432 static void free_loc_track(struct loc_track *t)
3435 free_pages((unsigned long)t->loc,
3436 get_order(sizeof(struct location) * t->max));
3439 static int alloc_loc_track(struct loc_track *t, unsigned long max, gfp_t flags)
3444 order = get_order(sizeof(struct location) * max);
3446 l = (void *)__get_free_pages(flags, order);
3451 memcpy(l, t->loc, sizeof(struct location) * t->count);
3459 static int add_location(struct loc_track *t, struct kmem_cache *s,
3460 const struct track *track)
3462 long start, end, pos;
3465 unsigned long age = jiffies - track->when;
3471 pos = start + (end - start + 1) / 2;
3474 * There is nothing at "end". If we end up there
3475 * we need to add something to before end.
3480 caddr = t->loc[pos].addr;
3481 if (track->addr == caddr) {
3487 if (age < l->min_time)
3489 if (age > l->max_time)
3492 if (track->pid < l->min_pid)
3493 l->min_pid = track->pid;
3494 if (track->pid > l->max_pid)
3495 l->max_pid = track->pid;
3497 cpu_set(track->cpu, l->cpus);
3499 node_set(page_to_nid(virt_to_page(track)), l->nodes);
3503 if (track->addr < caddr)
3510 * Not found. Insert new tracking element.
3512 if (t->count >= t->max && !alloc_loc_track(t, 2 * t->max, GFP_ATOMIC))
3518 (t->count - pos) * sizeof(struct location));
3521 l->addr = track->addr;
3525 l->min_pid = track->pid;
3526 l->max_pid = track->pid;
3527 cpus_clear(l->cpus);
3528 cpu_set(track->cpu, l->cpus);
3529 nodes_clear(l->nodes);
3530 node_set(page_to_nid(virt_to_page(track)), l->nodes);
3534 static void process_slab(struct loc_track *t, struct kmem_cache *s,
3535 struct page *page, enum track_item alloc)
3537 void *addr = slab_address(page);
3538 DECLARE_BITMAP(map, s->objects);
3541 bitmap_zero(map, s->objects);
3542 for_each_free_object(p, s, page->freelist)
3543 set_bit(slab_index(p, s, addr), map);
3545 for_each_object(p, s, addr)
3546 if (!test_bit(slab_index(p, s, addr), map))
3547 add_location(t, s, get_track(s, p, alloc));
3550 static int list_locations(struct kmem_cache *s, char *buf,
3551 enum track_item alloc)
3555 struct loc_track t = { 0, 0, NULL };
3558 if (!alloc_loc_track(&t, PAGE_SIZE / sizeof(struct location),
3560 return sprintf(buf, "Out of memory\n");
3562 /* Push back cpu slabs */
3565 for_each_node_state(node, N_NORMAL_MEMORY) {
3566 struct kmem_cache_node *n = get_node(s, node);
3567 unsigned long flags;
3570 if (!atomic_long_read(&n->nr_slabs))
3573 spin_lock_irqsave(&n->list_lock, flags);
3574 list_for_each_entry(page, &n->partial, lru)
3575 process_slab(&t, s, page, alloc);
3576 list_for_each_entry(page, &n->full, lru)
3577 process_slab(&t, s, page, alloc);
3578 spin_unlock_irqrestore(&n->list_lock, flags);
3581 for (i = 0; i < t.count; i++) {
3582 struct location *l = &t.loc[i];
3584 if (len > PAGE_SIZE - 100)
3586 len += sprintf(buf + len, "%7ld ", l->count);
3589 len += sprint_symbol(buf + len, (unsigned long)l->addr);
3591 len += sprintf(buf + len, "<not-available>");
3593 if (l->sum_time != l->min_time) {
3594 unsigned long remainder;
3596 len += sprintf(buf + len, " age=%ld/%ld/%ld",
3598 div_long_long_rem(l->sum_time, l->count, &remainder),
3601 len += sprintf(buf + len, " age=%ld",
3604 if (l->min_pid != l->max_pid)
3605 len += sprintf(buf + len, " pid=%ld-%ld",
3606 l->min_pid, l->max_pid);
3608 len += sprintf(buf + len, " pid=%ld",
3611 if (num_online_cpus() > 1 && !cpus_empty(l->cpus) &&
3612 len < PAGE_SIZE - 60) {
3613 len += sprintf(buf + len, " cpus=");
3614 len += cpulist_scnprintf(buf + len, PAGE_SIZE - len - 50,
3618 if (num_online_nodes() > 1 && !nodes_empty(l->nodes) &&
3619 len < PAGE_SIZE - 60) {
3620 len += sprintf(buf + len, " nodes=");
3621 len += nodelist_scnprintf(buf + len, PAGE_SIZE - len - 50,
3625 len += sprintf(buf + len, "\n");
3630 len += sprintf(buf, "No data\n");
3634 enum slab_stat_type {
3641 #define SO_FULL (1 << SL_FULL)
3642 #define SO_PARTIAL (1 << SL_PARTIAL)
3643 #define SO_CPU (1 << SL_CPU)
3644 #define SO_OBJECTS (1 << SL_OBJECTS)
3646 static unsigned long slab_objects(struct kmem_cache *s,
3647 char *buf, unsigned long flags)
3649 unsigned long total = 0;
3653 unsigned long *nodes;
3654 unsigned long *per_cpu;
3656 nodes = kzalloc(2 * sizeof(unsigned long) * nr_node_ids, GFP_KERNEL);
3657 per_cpu = nodes + nr_node_ids;
3659 for_each_possible_cpu(cpu) {
3661 struct kmem_cache_cpu *c = get_cpu_slab(s, cpu);
3671 if (flags & SO_CPU) {
3672 if (flags & SO_OBJECTS)
3683 for_each_node_state(node, N_NORMAL_MEMORY) {
3684 struct kmem_cache_node *n = get_node(s, node);
3686 if (flags & SO_PARTIAL) {
3687 if (flags & SO_OBJECTS)
3688 x = count_partial(n);
3695 if (flags & SO_FULL) {
3696 int full_slabs = atomic_long_read(&n->nr_slabs)
3700 if (flags & SO_OBJECTS)
3701 x = full_slabs * s->objects;
3709 x = sprintf(buf, "%lu", total);
3711 for_each_node_state(node, N_NORMAL_MEMORY)
3713 x += sprintf(buf + x, " N%d=%lu",
3717 return x + sprintf(buf + x, "\n");
3720 static int any_slab_objects(struct kmem_cache *s)
3725 for_each_possible_cpu(cpu) {
3726 struct kmem_cache_cpu *c = get_cpu_slab(s, cpu);
3732 for_each_online_node(node) {
3733 struct kmem_cache_node *n = get_node(s, node);
3738 if (n->nr_partial || atomic_long_read(&n->nr_slabs))
3744 #define to_slab_attr(n) container_of(n, struct slab_attribute, attr)
3745 #define to_slab(n) container_of(n, struct kmem_cache, kobj);
3747 struct slab_attribute {
3748 struct attribute attr;
3749 ssize_t (*show)(struct kmem_cache *s, char *buf);
3750 ssize_t (*store)(struct kmem_cache *s, const char *x, size_t count);
3753 #define SLAB_ATTR_RO(_name) \
3754 static struct slab_attribute _name##_attr = __ATTR_RO(_name)
3756 #define SLAB_ATTR(_name) \
3757 static struct slab_attribute _name##_attr = \
3758 __ATTR(_name, 0644, _name##_show, _name##_store)
3760 static ssize_t slab_size_show(struct kmem_cache *s, char *buf)
3762 return sprintf(buf, "%d\n", s->size);
3764 SLAB_ATTR_RO(slab_size);
3766 static ssize_t align_show(struct kmem_cache *s, char *buf)
3768 return sprintf(buf, "%d\n", s->align);
3770 SLAB_ATTR_RO(align);
3772 static ssize_t object_size_show(struct kmem_cache *s, char *buf)
3774 return sprintf(buf, "%d\n", s->objsize);
3776 SLAB_ATTR_RO(object_size);
3778 static ssize_t objs_per_slab_show(struct kmem_cache *s, char *buf)
3780 return sprintf(buf, "%d\n", s->objects);
3782 SLAB_ATTR_RO(objs_per_slab);
3784 static ssize_t order_show(struct kmem_cache *s, char *buf)
3786 return sprintf(buf, "%d\n", s->order);
3788 SLAB_ATTR_RO(order);
3790 static ssize_t ctor_show(struct kmem_cache *s, char *buf)
3793 int n = sprint_symbol(buf, (unsigned long)s->ctor);
3795 return n + sprintf(buf + n, "\n");
3801 static ssize_t aliases_show(struct kmem_cache *s, char *buf)
3803 return sprintf(buf, "%d\n", s->refcount - 1);
3805 SLAB_ATTR_RO(aliases);
3807 static ssize_t slabs_show(struct kmem_cache *s, char *buf)
3809 return slab_objects(s, buf, SO_FULL|SO_PARTIAL|SO_CPU);
3811 SLAB_ATTR_RO(slabs);
3813 static ssize_t partial_show(struct kmem_cache *s, char *buf)
3815 return slab_objects(s, buf, SO_PARTIAL);
3817 SLAB_ATTR_RO(partial);
3819 static ssize_t cpu_slabs_show(struct kmem_cache *s, char *buf)
3821 return slab_objects(s, buf, SO_CPU);
3823 SLAB_ATTR_RO(cpu_slabs);
3825 static ssize_t objects_show(struct kmem_cache *s, char *buf)
3827 return slab_objects(s, buf, SO_FULL|SO_PARTIAL|SO_CPU|SO_OBJECTS);
3829 SLAB_ATTR_RO(objects);
3831 static ssize_t sanity_checks_show(struct kmem_cache *s, char *buf)
3833 return sprintf(buf, "%d\n", !!(s->flags & SLAB_DEBUG_FREE));
3836 static ssize_t sanity_checks_store(struct kmem_cache *s,
3837 const char *buf, size_t length)
3839 s->flags &= ~SLAB_DEBUG_FREE;
3841 s->flags |= SLAB_DEBUG_FREE;
3844 SLAB_ATTR(sanity_checks);
3846 static ssize_t trace_show(struct kmem_cache *s, char *buf)
3848 return sprintf(buf, "%d\n", !!(s->flags & SLAB_TRACE));
3851 static ssize_t trace_store(struct kmem_cache *s, const char *buf,
3854 s->flags &= ~SLAB_TRACE;
3856 s->flags |= SLAB_TRACE;
3861 static ssize_t reclaim_account_show(struct kmem_cache *s, char *buf)
3863 return sprintf(buf, "%d\n", !!(s->flags & SLAB_RECLAIM_ACCOUNT));
3866 static ssize_t reclaim_account_store(struct kmem_cache *s,
3867 const char *buf, size_t length)
3869 s->flags &= ~SLAB_RECLAIM_ACCOUNT;
3871 s->flags |= SLAB_RECLAIM_ACCOUNT;
3874 SLAB_ATTR(reclaim_account);
3876 static ssize_t hwcache_align_show(struct kmem_cache *s, char *buf)
3878 return sprintf(buf, "%d\n", !!(s->flags & SLAB_HWCACHE_ALIGN));
3880 SLAB_ATTR_RO(hwcache_align);
3882 #ifdef CONFIG_ZONE_DMA
3883 static ssize_t cache_dma_show(struct kmem_cache *s, char *buf)
3885 return sprintf(buf, "%d\n", !!(s->flags & SLAB_CACHE_DMA));
3887 SLAB_ATTR_RO(cache_dma);
3890 static ssize_t destroy_by_rcu_show(struct kmem_cache *s, char *buf)
3892 return sprintf(buf, "%d\n", !!(s->flags & SLAB_DESTROY_BY_RCU));
3894 SLAB_ATTR_RO(destroy_by_rcu);
3896 static ssize_t red_zone_show(struct kmem_cache *s, char *buf)
3898 return sprintf(buf, "%d\n", !!(s->flags & SLAB_RED_ZONE));
3901 static ssize_t red_zone_store(struct kmem_cache *s,
3902 const char *buf, size_t length)
3904 if (any_slab_objects(s))
3907 s->flags &= ~SLAB_RED_ZONE;
3909 s->flags |= SLAB_RED_ZONE;
3913 SLAB_ATTR(red_zone);
3915 static ssize_t poison_show(struct kmem_cache *s, char *buf)
3917 return sprintf(buf, "%d\n", !!(s->flags & SLAB_POISON));
3920 static ssize_t poison_store(struct kmem_cache *s,
3921 const char *buf, size_t length)
3923 if (any_slab_objects(s))
3926 s->flags &= ~SLAB_POISON;
3928 s->flags |= SLAB_POISON;
3934 static ssize_t store_user_show(struct kmem_cache *s, char *buf)
3936 return sprintf(buf, "%d\n", !!(s->flags & SLAB_STORE_USER));
3939 static ssize_t store_user_store(struct kmem_cache *s,
3940 const char *buf, size_t length)
3942 if (any_slab_objects(s))
3945 s->flags &= ~SLAB_STORE_USER;
3947 s->flags |= SLAB_STORE_USER;
3951 SLAB_ATTR(store_user);
3953 static ssize_t validate_show(struct kmem_cache *s, char *buf)
3958 static ssize_t validate_store(struct kmem_cache *s,
3959 const char *buf, size_t length)
3963 if (buf[0] == '1') {
3964 ret = validate_slab_cache(s);
3970 SLAB_ATTR(validate);
3972 static ssize_t shrink_show(struct kmem_cache *s, char *buf)
3977 static ssize_t shrink_store(struct kmem_cache *s,
3978 const char *buf, size_t length)
3980 if (buf[0] == '1') {
3981 int rc = kmem_cache_shrink(s);
3991 static ssize_t alloc_calls_show(struct kmem_cache *s, char *buf)
3993 if (!(s->flags & SLAB_STORE_USER))
3995 return list_locations(s, buf, TRACK_ALLOC);
3997 SLAB_ATTR_RO(alloc_calls);
3999 static ssize_t free_calls_show(struct kmem_cache *s, char *buf)
4001 if (!(s->flags & SLAB_STORE_USER))
4003 return list_locations(s, buf, TRACK_FREE);
4005 SLAB_ATTR_RO(free_calls);
4008 static ssize_t remote_node_defrag_ratio_show(struct kmem_cache *s, char *buf)
4010 return sprintf(buf, "%d\n", s->remote_node_defrag_ratio / 10);
4013 static ssize_t remote_node_defrag_ratio_store(struct kmem_cache *s,
4014 const char *buf, size_t length)
4016 int n = simple_strtoul(buf, NULL, 10);
4019 s->remote_node_defrag_ratio = n * 10;
4022 SLAB_ATTR(remote_node_defrag_ratio);
4025 #ifdef CONFIG_SLUB_STATS
4027 static int show_stat(struct kmem_cache *s, char *buf, enum stat_item si)
4029 unsigned long sum = 0;
4032 int *data = kmalloc(nr_cpu_ids * sizeof(int), GFP_KERNEL);
4037 for_each_online_cpu(cpu) {
4038 unsigned x = get_cpu_slab(s, cpu)->stat[si];
4044 len = sprintf(buf, "%lu", sum);
4046 for_each_online_cpu(cpu) {
4047 if (data[cpu] && len < PAGE_SIZE - 20)
4048 len += sprintf(buf + len, " c%d=%u", cpu, data[cpu]);
4051 return len + sprintf(buf + len, "\n");
4054 #define STAT_ATTR(si, text) \
4055 static ssize_t text##_show(struct kmem_cache *s, char *buf) \
4057 return show_stat(s, buf, si); \
4059 SLAB_ATTR_RO(text); \
4061 STAT_ATTR(ALLOC_FASTPATH, alloc_fastpath);
4062 STAT_ATTR(ALLOC_SLOWPATH, alloc_slowpath);
4063 STAT_ATTR(FREE_FASTPATH, free_fastpath);
4064 STAT_ATTR(FREE_SLOWPATH, free_slowpath);
4065 STAT_ATTR(FREE_FROZEN, free_frozen);
4066 STAT_ATTR(FREE_ADD_PARTIAL, free_add_partial);
4067 STAT_ATTR(FREE_REMOVE_PARTIAL, free_remove_partial);
4068 STAT_ATTR(ALLOC_FROM_PARTIAL, alloc_from_partial);
4069 STAT_ATTR(ALLOC_SLAB, alloc_slab);
4070 STAT_ATTR(ALLOC_REFILL, alloc_refill);
4071 STAT_ATTR(FREE_SLAB, free_slab);
4072 STAT_ATTR(CPUSLAB_FLUSH, cpuslab_flush);
4073 STAT_ATTR(DEACTIVATE_FULL, deactivate_full);
4074 STAT_ATTR(DEACTIVATE_EMPTY, deactivate_empty);
4075 STAT_ATTR(DEACTIVATE_TO_HEAD, deactivate_to_head);
4076 STAT_ATTR(DEACTIVATE_TO_TAIL, deactivate_to_tail);
4077 STAT_ATTR(DEACTIVATE_REMOTE_FREES, deactivate_remote_frees);
4081 static struct attribute *slab_attrs[] = {
4082 &slab_size_attr.attr,
4083 &object_size_attr.attr,
4084 &objs_per_slab_attr.attr,
4089 &cpu_slabs_attr.attr,
4093 &sanity_checks_attr.attr,
4095 &hwcache_align_attr.attr,
4096 &reclaim_account_attr.attr,
4097 &destroy_by_rcu_attr.attr,
4098 &red_zone_attr.attr,
4100 &store_user_attr.attr,
4101 &validate_attr.attr,
4103 &alloc_calls_attr.attr,
4104 &free_calls_attr.attr,
4105 #ifdef CONFIG_ZONE_DMA
4106 &cache_dma_attr.attr,
4109 &remote_node_defrag_ratio_attr.attr,
4111 #ifdef CONFIG_SLUB_STATS
4112 &alloc_fastpath_attr.attr,
4113 &alloc_slowpath_attr.attr,
4114 &free_fastpath_attr.attr,
4115 &free_slowpath_attr.attr,
4116 &free_frozen_attr.attr,
4117 &free_add_partial_attr.attr,
4118 &free_remove_partial_attr.attr,
4119 &alloc_from_partial_attr.attr,
4120 &alloc_slab_attr.attr,
4121 &alloc_refill_attr.attr,
4122 &free_slab_attr.attr,
4123 &cpuslab_flush_attr.attr,
4124 &deactivate_full_attr.attr,
4125 &deactivate_empty_attr.attr,
4126 &deactivate_to_head_attr.attr,
4127 &deactivate_to_tail_attr.attr,
4128 &deactivate_remote_frees_attr.attr,
4133 static struct attribute_group slab_attr_group = {
4134 .attrs = slab_attrs,
4137 static ssize_t slab_attr_show(struct kobject *kobj,
4138 struct attribute *attr,
4141 struct slab_attribute *attribute;
4142 struct kmem_cache *s;
4145 attribute = to_slab_attr(attr);
4148 if (!attribute->show)
4151 err = attribute->show(s, buf);
4156 static ssize_t slab_attr_store(struct kobject *kobj,
4157 struct attribute *attr,
4158 const char *buf, size_t len)
4160 struct slab_attribute *attribute;
4161 struct kmem_cache *s;
4164 attribute = to_slab_attr(attr);
4167 if (!attribute->store)
4170 err = attribute->store(s, buf, len);
4175 static void kmem_cache_release(struct kobject *kobj)
4177 struct kmem_cache *s = to_slab(kobj);
4182 static struct sysfs_ops slab_sysfs_ops = {
4183 .show = slab_attr_show,
4184 .store = slab_attr_store,
4187 static struct kobj_type slab_ktype = {
4188 .sysfs_ops = &slab_sysfs_ops,
4189 .release = kmem_cache_release
4192 static int uevent_filter(struct kset *kset, struct kobject *kobj)
4194 struct kobj_type *ktype = get_ktype(kobj);
4196 if (ktype == &slab_ktype)
4201 static struct kset_uevent_ops slab_uevent_ops = {
4202 .filter = uevent_filter,
4205 static struct kset *slab_kset;
4207 #define ID_STR_LENGTH 64
4209 /* Create a unique string id for a slab cache:
4211 * :[flags-]size:[memory address of kmemcache]
4213 static char *create_unique_id(struct kmem_cache *s)
4215 char *name = kmalloc(ID_STR_LENGTH, GFP_KERNEL);
4222 * First flags affecting slabcache operations. We will only
4223 * get here for aliasable slabs so we do not need to support
4224 * too many flags. The flags here must cover all flags that
4225 * are matched during merging to guarantee that the id is
4228 if (s->flags & SLAB_CACHE_DMA)
4230 if (s->flags & SLAB_RECLAIM_ACCOUNT)
4232 if (s->flags & SLAB_DEBUG_FREE)
4236 p += sprintf(p, "%07d", s->size);
4237 BUG_ON(p > name + ID_STR_LENGTH - 1);
4241 static int sysfs_slab_add(struct kmem_cache *s)
4247 if (slab_state < SYSFS)
4248 /* Defer until later */
4251 unmergeable = slab_unmergeable(s);
4254 * Slabcache can never be merged so we can use the name proper.
4255 * This is typically the case for debug situations. In that
4256 * case we can catch duplicate names easily.
4258 sysfs_remove_link(&slab_kset->kobj, s->name);
4262 * Create a unique name for the slab as a target
4265 name = create_unique_id(s);
4268 s->kobj.kset = slab_kset;
4269 err = kobject_init_and_add(&s->kobj, &slab_ktype, NULL, name);
4271 kobject_put(&s->kobj);
4275 err = sysfs_create_group(&s->kobj, &slab_attr_group);
4278 kobject_uevent(&s->kobj, KOBJ_ADD);
4280 /* Setup first alias */
4281 sysfs_slab_alias(s, s->name);
4287 static void sysfs_slab_remove(struct kmem_cache *s)
4289 kobject_uevent(&s->kobj, KOBJ_REMOVE);
4290 kobject_del(&s->kobj);
4291 kobject_put(&s->kobj);
4295 * Need to buffer aliases during bootup until sysfs becomes
4296 * available lest we loose that information.
4298 struct saved_alias {
4299 struct kmem_cache *s;
4301 struct saved_alias *next;
4304 static struct saved_alias *alias_list;
4306 static int sysfs_slab_alias(struct kmem_cache *s, const char *name)
4308 struct saved_alias *al;
4310 if (slab_state == SYSFS) {
4312 * If we have a leftover link then remove it.
4314 sysfs_remove_link(&slab_kset->kobj, name);
4315 return sysfs_create_link(&slab_kset->kobj, &s->kobj, name);
4318 al = kmalloc(sizeof(struct saved_alias), GFP_KERNEL);
4324 al->next = alias_list;
4329 static int __init slab_sysfs_init(void)
4331 struct kmem_cache *s;
4334 slab_kset = kset_create_and_add("slab", &slab_uevent_ops, kernel_kobj);
4336 printk(KERN_ERR "Cannot register slab subsystem.\n");
4342 list_for_each_entry(s, &slab_caches, list) {
4343 err = sysfs_slab_add(s);
4345 printk(KERN_ERR "SLUB: Unable to add boot slab %s"
4346 " to sysfs\n", s->name);
4349 while (alias_list) {
4350 struct saved_alias *al = alias_list;
4352 alias_list = alias_list->next;
4353 err = sysfs_slab_alias(al->s, al->name);
4355 printk(KERN_ERR "SLUB: Unable to add boot slab alias"
4356 " %s to sysfs\n", s->name);
4364 __initcall(slab_sysfs_init);
4368 * The /proc/slabinfo ABI
4370 #ifdef CONFIG_SLABINFO
4372 ssize_t slabinfo_write(struct file *file, const char __user * buffer,
4373 size_t count, loff_t *ppos)
4379 static void print_slabinfo_header(struct seq_file *m)
4381 seq_puts(m, "slabinfo - version: 2.1\n");
4382 seq_puts(m, "# name <active_objs> <num_objs> <objsize> "
4383 "<objperslab> <pagesperslab>");
4384 seq_puts(m, " : tunables <limit> <batchcount> <sharedfactor>");
4385 seq_puts(m, " : slabdata <active_slabs> <num_slabs> <sharedavail>");
4389 static void *s_start(struct seq_file *m, loff_t *pos)
4393 down_read(&slub_lock);
4395 print_slabinfo_header(m);
4397 return seq_list_start(&slab_caches, *pos);
4400 static void *s_next(struct seq_file *m, void *p, loff_t *pos)
4402 return seq_list_next(p, &slab_caches, pos);
4405 static void s_stop(struct seq_file *m, void *p)
4407 up_read(&slub_lock);
4410 static int s_show(struct seq_file *m, void *p)
4412 unsigned long nr_partials = 0;
4413 unsigned long nr_slabs = 0;
4414 unsigned long nr_inuse = 0;
4415 unsigned long nr_objs;
4416 struct kmem_cache *s;
4419 s = list_entry(p, struct kmem_cache, list);
4421 for_each_online_node(node) {
4422 struct kmem_cache_node *n = get_node(s, node);
4427 nr_partials += n->nr_partial;
4428 nr_slabs += atomic_long_read(&n->nr_slabs);
4429 nr_inuse += count_partial(n);
4432 nr_objs = nr_slabs * s->objects;
4433 nr_inuse += (nr_slabs - nr_partials) * s->objects;
4435 seq_printf(m, "%-17s %6lu %6lu %6u %4u %4d", s->name, nr_inuse,
4436 nr_objs, s->size, s->objects, (1 << s->order));
4437 seq_printf(m, " : tunables %4u %4u %4u", 0, 0, 0);
4438 seq_printf(m, " : slabdata %6lu %6lu %6lu", nr_slabs, nr_slabs,
4444 const struct seq_operations slabinfo_op = {
4451 #endif /* CONFIG_SLABINFO */