ACPI: thinkpad-acpi: add development version tag
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / mm / slub.c
blobc1f2fdadf37550dbff1ee4ea422088b36d71cf9a
1 /*
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>
9 */
11 #include <linux/mm.h>
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>
25 * Lock order:
26 * 1. slab_lock(page)
27 * 2. slab->list_lock
29 * The slab_lock protects operations on the object of a particular
30 * slab and its metadata in the page struct. If the slab lock
31 * has been taken then no allocations nor frees can be performed
32 * on the objects in the slab nor can the slab be added or removed
33 * from the partial or full lists since this would mean modifying
34 * the page_struct of the slab.
36 * The list_lock protects the partial and full list on each node and
37 * the partial slab counter. If taken then no new slabs may be added or
38 * removed from the lists nor make the number of partial slabs be modified.
39 * (Note that the total number of slabs is an atomic value that may be
40 * modified without taking the list lock).
42 * The list_lock is a centralized lock and thus we avoid taking it as
43 * much as possible. As long as SLUB does not have to handle partial
44 * slabs, operations can continue without any centralized lock. F.e.
45 * allocating a long series of objects that fill up slabs does not require
46 * the list lock.
48 * The lock order is sometimes inverted when we are trying to get a slab
49 * off a list. We take the list_lock and then look for a page on the list
50 * to use. While we do that objects in the slabs may be freed. We can
51 * only operate on the slab if we have also taken the slab_lock. So we use
52 * a slab_trylock() on the slab. If trylock was successful then no frees
53 * can occur anymore and we can use the slab for allocations etc. If the
54 * slab_trylock() does not succeed then frees are in progress in the slab and
55 * we must stay away from it for a while since we may cause a bouncing
56 * cacheline if we try to acquire the lock. So go onto the next slab.
57 * If all pages are busy then we may allocate a new slab instead of reusing
58 * a partial slab. A new slab has noone operating on it and thus there is
59 * no danger of cacheline contention.
61 * Interrupts are disabled during allocation and deallocation in order to
62 * make the slab allocator safe to use in the context of an irq. In addition
63 * interrupts are disabled to ensure that the processor does not change
64 * while handling per_cpu slabs, due to kernel preemption.
66 * SLUB assigns one slab for allocation to each processor.
67 * Allocations only occur from these slabs called cpu slabs.
69 * Slabs with free elements are kept on a partial list and during regular
70 * operations no list for full slabs is used. If an object in a full slab is
71 * freed then the slab will show up again on the partial lists.
72 * We track full slabs for debugging purposes though because otherwise we
73 * cannot scan all objects.
75 * Slabs are freed when they become empty. Teardown and setup is
76 * minimal so we rely on the page allocators per cpu caches for
77 * fast frees and allocs.
79 * Overloading of page flags that are otherwise used for LRU management.
81 * PageActive The slab is frozen and exempt from list processing.
82 * This means that the slab is dedicated to a purpose
83 * such as satisfying allocations for a specific
84 * processor. Objects may be freed in the slab while
85 * it is frozen but slab_free will then skip the usual
86 * list operations. It is up to the processor holding
87 * the slab to integrate the slab into the slab lists
88 * when the slab is no longer needed.
90 * One use of this flag is to mark slabs that are
91 * used for allocations. Then such a slab becomes a cpu
92 * slab. The cpu slab may be equipped with an additional
93 * lockless_freelist that allows lockless access to
94 * free objects in addition to the regular freelist
95 * that requires the slab lock.
97 * PageError Slab requires special handling due to debug
98 * options set. This moves slab handling out of
99 * the fast path and disables lockless freelists.
102 #define FROZEN (1 << PG_active)
104 #ifdef CONFIG_SLUB_DEBUG
105 #define SLABDEBUG (1 << PG_error)
106 #else
107 #define SLABDEBUG 0
108 #endif
110 static inline int SlabFrozen(struct page *page)
112 return page->flags & FROZEN;
115 static inline void SetSlabFrozen(struct page *page)
117 page->flags |= FROZEN;
120 static inline void ClearSlabFrozen(struct page *page)
122 page->flags &= ~FROZEN;
125 static inline int SlabDebug(struct page *page)
127 return page->flags & SLABDEBUG;
130 static inline void SetSlabDebug(struct page *page)
132 page->flags |= SLABDEBUG;
135 static inline void ClearSlabDebug(struct page *page)
137 page->flags &= ~SLABDEBUG;
141 * Issues still to be resolved:
143 * - The per cpu array is updated for each new slab and and is a remote
144 * cacheline for most nodes. This could become a bouncing cacheline given
145 * enough frequent updates. There are 16 pointers in a cacheline, so at
146 * max 16 cpus could compete for the cacheline which may be okay.
148 * - Support PAGE_ALLOC_DEBUG. Should be easy to do.
150 * - Variable sizing of the per node arrays
153 /* Enable to test recovery from slab corruption on boot */
154 #undef SLUB_RESILIENCY_TEST
156 #if PAGE_SHIFT <= 12
159 * Small page size. Make sure that we do not fragment memory
161 #define DEFAULT_MAX_ORDER 1
162 #define DEFAULT_MIN_OBJECTS 4
164 #else
167 * Large page machines are customarily able to handle larger
168 * page orders.
170 #define DEFAULT_MAX_ORDER 2
171 #define DEFAULT_MIN_OBJECTS 8
173 #endif
176 * Mininum number of partial slabs. These will be left on the partial
177 * lists even if they are empty. kmem_cache_shrink may reclaim them.
179 #define MIN_PARTIAL 2
182 * Maximum number of desirable partial slabs.
183 * The existence of more partial slabs makes kmem_cache_shrink
184 * sort the partial list by the number of objects in the.
186 #define MAX_PARTIAL 10
188 #define DEBUG_DEFAULT_FLAGS (SLAB_DEBUG_FREE | SLAB_RED_ZONE | \
189 SLAB_POISON | SLAB_STORE_USER)
192 * Set of flags that will prevent slab merging
194 #define SLUB_NEVER_MERGE (SLAB_RED_ZONE | SLAB_POISON | SLAB_STORE_USER | \
195 SLAB_TRACE | SLAB_DESTROY_BY_RCU)
197 #define SLUB_MERGE_SAME (SLAB_DEBUG_FREE | SLAB_RECLAIM_ACCOUNT | \
198 SLAB_CACHE_DMA)
200 #ifndef ARCH_KMALLOC_MINALIGN
201 #define ARCH_KMALLOC_MINALIGN __alignof__(unsigned long long)
202 #endif
204 #ifndef ARCH_SLAB_MINALIGN
205 #define ARCH_SLAB_MINALIGN __alignof__(unsigned long long)
206 #endif
209 * The page->inuse field is 16 bit thus we have this limitation
211 #define MAX_OBJECTS_PER_SLAB 65535
213 /* Internal SLUB flags */
214 #define __OBJECT_POISON 0x80000000 /* Poison object */
215 #define __SYSFS_ADD_DEFERRED 0x40000000 /* Not yet visible via sysfs */
217 /* Not all arches define cache_line_size */
218 #ifndef cache_line_size
219 #define cache_line_size() L1_CACHE_BYTES
220 #endif
222 static int kmem_size = sizeof(struct kmem_cache);
224 #ifdef CONFIG_SMP
225 static struct notifier_block slab_notifier;
226 #endif
228 static enum {
229 DOWN, /* No slab functionality available */
230 PARTIAL, /* kmem_cache_open() works but kmalloc does not */
231 UP, /* Everything works but does not show up in sysfs */
232 SYSFS /* Sysfs up */
233 } slab_state = DOWN;
235 /* A list of all slab caches on the system */
236 static DECLARE_RWSEM(slub_lock);
237 static LIST_HEAD(slab_caches);
240 * Tracking user of a slab.
242 struct track {
243 void *addr; /* Called from address */
244 int cpu; /* Was running on cpu */
245 int pid; /* Pid context */
246 unsigned long when; /* When did the operation occur */
249 enum track_item { TRACK_ALLOC, TRACK_FREE };
251 #if defined(CONFIG_SYSFS) && defined(CONFIG_SLUB_DEBUG)
252 static int sysfs_slab_add(struct kmem_cache *);
253 static int sysfs_slab_alias(struct kmem_cache *, const char *);
254 static void sysfs_slab_remove(struct kmem_cache *);
255 #else
256 static inline int sysfs_slab_add(struct kmem_cache *s) { return 0; }
257 static inline int sysfs_slab_alias(struct kmem_cache *s, const char *p)
258 { return 0; }
259 static inline void sysfs_slab_remove(struct kmem_cache *s) {}
260 #endif
262 /********************************************************************
263 * Core slab cache functions
264 *******************************************************************/
266 int slab_is_available(void)
268 return slab_state >= UP;
271 static inline struct kmem_cache_node *get_node(struct kmem_cache *s, int node)
273 #ifdef CONFIG_NUMA
274 return s->node[node];
275 #else
276 return &s->local_node;
277 #endif
280 static inline int check_valid_pointer(struct kmem_cache *s,
281 struct page *page, const void *object)
283 void *base;
285 if (!object)
286 return 1;
288 base = page_address(page);
289 if (object < base || object >= base + s->objects * s->size ||
290 (object - base) % s->size) {
291 return 0;
294 return 1;
298 * Slow version of get and set free pointer.
300 * This version requires touching the cache lines of kmem_cache which
301 * we avoid to do in the fast alloc free paths. There we obtain the offset
302 * from the page struct.
304 static inline void *get_freepointer(struct kmem_cache *s, void *object)
306 return *(void **)(object + s->offset);
309 static inline void set_freepointer(struct kmem_cache *s, void *object, void *fp)
311 *(void **)(object + s->offset) = fp;
314 /* Loop over all objects in a slab */
315 #define for_each_object(__p, __s, __addr) \
316 for (__p = (__addr); __p < (__addr) + (__s)->objects * (__s)->size;\
317 __p += (__s)->size)
319 /* Scan freelist */
320 #define for_each_free_object(__p, __s, __free) \
321 for (__p = (__free); __p; __p = get_freepointer((__s), __p))
323 /* Determine object index from a given position */
324 static inline int slab_index(void *p, struct kmem_cache *s, void *addr)
326 return (p - addr) / s->size;
329 #ifdef CONFIG_SLUB_DEBUG
331 * Debug settings:
333 #ifdef CONFIG_SLUB_DEBUG_ON
334 static int slub_debug = DEBUG_DEFAULT_FLAGS;
335 #else
336 static int slub_debug;
337 #endif
339 static char *slub_debug_slabs;
342 * Object debugging
344 static void print_section(char *text, u8 *addr, unsigned int length)
346 int i, offset;
347 int newline = 1;
348 char ascii[17];
350 ascii[16] = 0;
352 for (i = 0; i < length; i++) {
353 if (newline) {
354 printk(KERN_ERR "%8s 0x%p: ", text, addr + i);
355 newline = 0;
357 printk(" %02x", addr[i]);
358 offset = i % 16;
359 ascii[offset] = isgraph(addr[i]) ? addr[i] : '.';
360 if (offset == 15) {
361 printk(" %s\n",ascii);
362 newline = 1;
365 if (!newline) {
366 i %= 16;
367 while (i < 16) {
368 printk(" ");
369 ascii[i] = ' ';
370 i++;
372 printk(" %s\n", ascii);
376 static struct track *get_track(struct kmem_cache *s, void *object,
377 enum track_item alloc)
379 struct track *p;
381 if (s->offset)
382 p = object + s->offset + sizeof(void *);
383 else
384 p = object + s->inuse;
386 return p + alloc;
389 static void set_track(struct kmem_cache *s, void *object,
390 enum track_item alloc, void *addr)
392 struct track *p;
394 if (s->offset)
395 p = object + s->offset + sizeof(void *);
396 else
397 p = object + s->inuse;
399 p += alloc;
400 if (addr) {
401 p->addr = addr;
402 p->cpu = smp_processor_id();
403 p->pid = current ? current->pid : -1;
404 p->when = jiffies;
405 } else
406 memset(p, 0, sizeof(struct track));
409 static void init_tracking(struct kmem_cache *s, void *object)
411 if (!(s->flags & SLAB_STORE_USER))
412 return;
414 set_track(s, object, TRACK_FREE, NULL);
415 set_track(s, object, TRACK_ALLOC, NULL);
418 static void print_track(const char *s, struct track *t)
420 if (!t->addr)
421 return;
423 printk(KERN_ERR "INFO: %s in ", s);
424 __print_symbol("%s", (unsigned long)t->addr);
425 printk(" age=%lu cpu=%u pid=%d\n", jiffies - t->when, t->cpu, t->pid);
428 static void print_tracking(struct kmem_cache *s, void *object)
430 if (!(s->flags & SLAB_STORE_USER))
431 return;
433 print_track("Allocated", get_track(s, object, TRACK_ALLOC));
434 print_track("Freed", get_track(s, object, TRACK_FREE));
437 static void print_page_info(struct page *page)
439 printk(KERN_ERR "INFO: Slab 0x%p used=%u fp=0x%p flags=0x%04lx\n",
440 page, page->inuse, page->freelist, page->flags);
444 static void slab_bug(struct kmem_cache *s, char *fmt, ...)
446 va_list args;
447 char buf[100];
449 va_start(args, fmt);
450 vsnprintf(buf, sizeof(buf), fmt, args);
451 va_end(args);
452 printk(KERN_ERR "========================================"
453 "=====================================\n");
454 printk(KERN_ERR "BUG %s: %s\n", s->name, buf);
455 printk(KERN_ERR "----------------------------------------"
456 "-------------------------------------\n\n");
459 static void slab_fix(struct kmem_cache *s, char *fmt, ...)
461 va_list args;
462 char buf[100];
464 va_start(args, fmt);
465 vsnprintf(buf, sizeof(buf), fmt, args);
466 va_end(args);
467 printk(KERN_ERR "FIX %s: %s\n", s->name, buf);
470 static void print_trailer(struct kmem_cache *s, struct page *page, u8 *p)
472 unsigned int off; /* Offset of last byte */
473 u8 *addr = page_address(page);
475 print_tracking(s, p);
477 print_page_info(page);
479 printk(KERN_ERR "INFO: Object 0x%p @offset=%tu fp=0x%p\n\n",
480 p, p - addr, get_freepointer(s, p));
482 if (p > addr + 16)
483 print_section("Bytes b4", p - 16, 16);
485 print_section("Object", p, min(s->objsize, 128));
487 if (s->flags & SLAB_RED_ZONE)
488 print_section("Redzone", p + s->objsize,
489 s->inuse - s->objsize);
491 if (s->offset)
492 off = s->offset + sizeof(void *);
493 else
494 off = s->inuse;
496 if (s->flags & SLAB_STORE_USER)
497 off += 2 * sizeof(struct track);
499 if (off != s->size)
500 /* Beginning of the filler is the free pointer */
501 print_section("Padding", p + off, s->size - off);
503 dump_stack();
506 static void object_err(struct kmem_cache *s, struct page *page,
507 u8 *object, char *reason)
509 slab_bug(s, reason);
510 print_trailer(s, page, object);
513 static void slab_err(struct kmem_cache *s, struct page *page, char *fmt, ...)
515 va_list args;
516 char buf[100];
518 va_start(args, fmt);
519 vsnprintf(buf, sizeof(buf), fmt, args);
520 va_end(args);
521 slab_bug(s, fmt);
522 print_page_info(page);
523 dump_stack();
526 static void init_object(struct kmem_cache *s, void *object, int active)
528 u8 *p = object;
530 if (s->flags & __OBJECT_POISON) {
531 memset(p, POISON_FREE, s->objsize - 1);
532 p[s->objsize -1] = POISON_END;
535 if (s->flags & SLAB_RED_ZONE)
536 memset(p + s->objsize,
537 active ? SLUB_RED_ACTIVE : SLUB_RED_INACTIVE,
538 s->inuse - s->objsize);
541 static u8 *check_bytes(u8 *start, unsigned int value, unsigned int bytes)
543 while (bytes) {
544 if (*start != (u8)value)
545 return start;
546 start++;
547 bytes--;
549 return NULL;
552 static void restore_bytes(struct kmem_cache *s, char *message, u8 data,
553 void *from, void *to)
555 slab_fix(s, "Restoring 0x%p-0x%p=0x%x\n", from, to - 1, data);
556 memset(from, data, to - from);
559 static int check_bytes_and_report(struct kmem_cache *s, struct page *page,
560 u8 *object, char *what,
561 u8* start, unsigned int value, unsigned int bytes)
563 u8 *fault;
564 u8 *end;
566 fault = check_bytes(start, value, bytes);
567 if (!fault)
568 return 1;
570 end = start + bytes;
571 while (end > fault && end[-1] == value)
572 end--;
574 slab_bug(s, "%s overwritten", what);
575 printk(KERN_ERR "INFO: 0x%p-0x%p. First byte 0x%x instead of 0x%x\n",
576 fault, end - 1, fault[0], value);
577 print_trailer(s, page, object);
579 restore_bytes(s, what, value, fault, end);
580 return 0;
584 * Object layout:
586 * object address
587 * Bytes of the object to be managed.
588 * If the freepointer may overlay the object then the free
589 * pointer is the first word of the object.
591 * Poisoning uses 0x6b (POISON_FREE) and the last byte is
592 * 0xa5 (POISON_END)
594 * object + s->objsize
595 * Padding to reach word boundary. This is also used for Redzoning.
596 * Padding is extended by another word if Redzoning is enabled and
597 * objsize == inuse.
599 * We fill with 0xbb (RED_INACTIVE) for inactive objects and with
600 * 0xcc (RED_ACTIVE) for objects in use.
602 * object + s->inuse
603 * Meta data starts here.
605 * A. Free pointer (if we cannot overwrite object on free)
606 * B. Tracking data for SLAB_STORE_USER
607 * C. Padding to reach required alignment boundary or at mininum
608 * one word if debuggin is on to be able to detect writes
609 * before the word boundary.
611 * Padding is done using 0x5a (POISON_INUSE)
613 * object + s->size
614 * Nothing is used beyond s->size.
616 * If slabcaches are merged then the objsize and inuse boundaries are mostly
617 * ignored. And therefore no slab options that rely on these boundaries
618 * may be used with merged slabcaches.
621 static int check_pad_bytes(struct kmem_cache *s, struct page *page, u8 *p)
623 unsigned long off = s->inuse; /* The end of info */
625 if (s->offset)
626 /* Freepointer is placed after the object. */
627 off += sizeof(void *);
629 if (s->flags & SLAB_STORE_USER)
630 /* We also have user information there */
631 off += 2 * sizeof(struct track);
633 if (s->size == off)
634 return 1;
636 return check_bytes_and_report(s, page, p, "Object padding",
637 p + off, POISON_INUSE, s->size - off);
640 static int slab_pad_check(struct kmem_cache *s, struct page *page)
642 u8 *start;
643 u8 *fault;
644 u8 *end;
645 int length;
646 int remainder;
648 if (!(s->flags & SLAB_POISON))
649 return 1;
651 start = page_address(page);
652 end = start + (PAGE_SIZE << s->order);
653 length = s->objects * s->size;
654 remainder = end - (start + length);
655 if (!remainder)
656 return 1;
658 fault = check_bytes(start + length, POISON_INUSE, remainder);
659 if (!fault)
660 return 1;
661 while (end > fault && end[-1] == POISON_INUSE)
662 end--;
664 slab_err(s, page, "Padding overwritten. 0x%p-0x%p", fault, end - 1);
665 print_section("Padding", start, length);
667 restore_bytes(s, "slab padding", POISON_INUSE, start, end);
668 return 0;
671 static int check_object(struct kmem_cache *s, struct page *page,
672 void *object, int active)
674 u8 *p = object;
675 u8 *endobject = object + s->objsize;
677 if (s->flags & SLAB_RED_ZONE) {
678 unsigned int red =
679 active ? SLUB_RED_ACTIVE : SLUB_RED_INACTIVE;
681 if (!check_bytes_and_report(s, page, object, "Redzone",
682 endobject, red, s->inuse - s->objsize))
683 return 0;
684 } else {
685 if ((s->flags & SLAB_POISON) && s->objsize < s->inuse)
686 check_bytes_and_report(s, page, p, "Alignment padding", endobject,
687 POISON_INUSE, s->inuse - s->objsize);
690 if (s->flags & SLAB_POISON) {
691 if (!active && (s->flags & __OBJECT_POISON) &&
692 (!check_bytes_and_report(s, page, p, "Poison", p,
693 POISON_FREE, s->objsize - 1) ||
694 !check_bytes_and_report(s, page, p, "Poison",
695 p + s->objsize -1, POISON_END, 1)))
696 return 0;
698 * check_pad_bytes cleans up on its own.
700 check_pad_bytes(s, page, p);
703 if (!s->offset && active)
705 * Object and freepointer overlap. Cannot check
706 * freepointer while object is allocated.
708 return 1;
710 /* Check free pointer validity */
711 if (!check_valid_pointer(s, page, get_freepointer(s, p))) {
712 object_err(s, page, p, "Freepointer corrupt");
714 * No choice but to zap it and thus loose the remainder
715 * of the free objects in this slab. May cause
716 * another error because the object count is now wrong.
718 set_freepointer(s, p, NULL);
719 return 0;
721 return 1;
724 static int check_slab(struct kmem_cache *s, struct page *page)
726 VM_BUG_ON(!irqs_disabled());
728 if (!PageSlab(page)) {
729 slab_err(s, page, "Not a valid slab page");
730 return 0;
732 if (page->offset * sizeof(void *) != s->offset) {
733 slab_err(s, page, "Corrupted offset %lu",
734 (unsigned long)(page->offset * sizeof(void *)));
735 return 0;
737 if (page->inuse > s->objects) {
738 slab_err(s, page, "inuse %u > max %u",
739 s->name, page->inuse, s->objects);
740 return 0;
742 /* Slab_pad_check fixes things up after itself */
743 slab_pad_check(s, page);
744 return 1;
748 * Determine if a certain object on a page is on the freelist. Must hold the
749 * slab lock to guarantee that the chains are in a consistent state.
751 static int on_freelist(struct kmem_cache *s, struct page *page, void *search)
753 int nr = 0;
754 void *fp = page->freelist;
755 void *object = NULL;
757 while (fp && nr <= s->objects) {
758 if (fp == search)
759 return 1;
760 if (!check_valid_pointer(s, page, fp)) {
761 if (object) {
762 object_err(s, page, object,
763 "Freechain corrupt");
764 set_freepointer(s, object, NULL);
765 break;
766 } else {
767 slab_err(s, page, "Freepointer corrupt");
768 page->freelist = NULL;
769 page->inuse = s->objects;
770 slab_fix(s, "Freelist cleared");
771 return 0;
773 break;
775 object = fp;
776 fp = get_freepointer(s, object);
777 nr++;
780 if (page->inuse != s->objects - nr) {
781 slab_err(s, page, "Wrong object count. Counter is %d but "
782 "counted were %d", page->inuse, s->objects - nr);
783 page->inuse = s->objects - nr;
784 slab_fix(s, "Object count adjusted.");
786 return search == NULL;
789 static void trace(struct kmem_cache *s, struct page *page, void *object, int alloc)
791 if (s->flags & SLAB_TRACE) {
792 printk(KERN_INFO "TRACE %s %s 0x%p inuse=%d fp=0x%p\n",
793 s->name,
794 alloc ? "alloc" : "free",
795 object, page->inuse,
796 page->freelist);
798 if (!alloc)
799 print_section("Object", (void *)object, s->objsize);
801 dump_stack();
806 * Tracking of fully allocated slabs for debugging purposes.
808 static void add_full(struct kmem_cache_node *n, struct page *page)
810 spin_lock(&n->list_lock);
811 list_add(&page->lru, &n->full);
812 spin_unlock(&n->list_lock);
815 static void remove_full(struct kmem_cache *s, struct page *page)
817 struct kmem_cache_node *n;
819 if (!(s->flags & SLAB_STORE_USER))
820 return;
822 n = get_node(s, page_to_nid(page));
824 spin_lock(&n->list_lock);
825 list_del(&page->lru);
826 spin_unlock(&n->list_lock);
829 static void setup_object_debug(struct kmem_cache *s, struct page *page,
830 void *object)
832 if (!(s->flags & (SLAB_STORE_USER|SLAB_RED_ZONE|__OBJECT_POISON)))
833 return;
835 init_object(s, object, 0);
836 init_tracking(s, object);
839 static int alloc_debug_processing(struct kmem_cache *s, struct page *page,
840 void *object, void *addr)
842 if (!check_slab(s, page))
843 goto bad;
845 if (object && !on_freelist(s, page, object)) {
846 object_err(s, page, object, "Object already allocated");
847 goto bad;
850 if (!check_valid_pointer(s, page, object)) {
851 object_err(s, page, object, "Freelist Pointer check fails");
852 goto bad;
855 if (object && !check_object(s, page, object, 0))
856 goto bad;
858 /* Success perform special debug activities for allocs */
859 if (s->flags & SLAB_STORE_USER)
860 set_track(s, object, TRACK_ALLOC, addr);
861 trace(s, page, object, 1);
862 init_object(s, object, 1);
863 return 1;
865 bad:
866 if (PageSlab(page)) {
868 * If this is a slab page then lets do the best we can
869 * to avoid issues in the future. Marking all objects
870 * as used avoids touching the remaining objects.
872 slab_fix(s, "Marking all objects used");
873 page->inuse = s->objects;
874 page->freelist = NULL;
875 /* Fix up fields that may be corrupted */
876 page->offset = s->offset / sizeof(void *);
878 return 0;
881 static int free_debug_processing(struct kmem_cache *s, struct page *page,
882 void *object, void *addr)
884 if (!check_slab(s, page))
885 goto fail;
887 if (!check_valid_pointer(s, page, object)) {
888 slab_err(s, page, "Invalid object pointer 0x%p", object);
889 goto fail;
892 if (on_freelist(s, page, object)) {
893 object_err(s, page, object, "Object already free");
894 goto fail;
897 if (!check_object(s, page, object, 1))
898 return 0;
900 if (unlikely(s != page->slab)) {
901 if (!PageSlab(page))
902 slab_err(s, page, "Attempt to free object(0x%p) "
903 "outside of slab", object);
904 else
905 if (!page->slab) {
906 printk(KERN_ERR
907 "SLUB <none>: no slab for object 0x%p.\n",
908 object);
909 dump_stack();
911 else
912 object_err(s, page, object,
913 "page slab pointer corrupt.");
914 goto fail;
917 /* Special debug activities for freeing objects */
918 if (!SlabFrozen(page) && !page->freelist)
919 remove_full(s, page);
920 if (s->flags & SLAB_STORE_USER)
921 set_track(s, object, TRACK_FREE, addr);
922 trace(s, page, object, 0);
923 init_object(s, object, 0);
924 return 1;
926 fail:
927 slab_fix(s, "Object at 0x%p not freed", object);
928 return 0;
931 static int __init setup_slub_debug(char *str)
933 slub_debug = DEBUG_DEFAULT_FLAGS;
934 if (*str++ != '=' || !*str)
936 * No options specified. Switch on full debugging.
938 goto out;
940 if (*str == ',')
942 * No options but restriction on slabs. This means full
943 * debugging for slabs matching a pattern.
945 goto check_slabs;
947 slub_debug = 0;
948 if (*str == '-')
950 * Switch off all debugging measures.
952 goto out;
955 * Determine which debug features should be switched on
957 for ( ;*str && *str != ','; str++) {
958 switch (tolower(*str)) {
959 case 'f':
960 slub_debug |= SLAB_DEBUG_FREE;
961 break;
962 case 'z':
963 slub_debug |= SLAB_RED_ZONE;
964 break;
965 case 'p':
966 slub_debug |= SLAB_POISON;
967 break;
968 case 'u':
969 slub_debug |= SLAB_STORE_USER;
970 break;
971 case 't':
972 slub_debug |= SLAB_TRACE;
973 break;
974 default:
975 printk(KERN_ERR "slub_debug option '%c' "
976 "unknown. skipped\n",*str);
980 check_slabs:
981 if (*str == ',')
982 slub_debug_slabs = str + 1;
983 out:
984 return 1;
987 __setup("slub_debug", setup_slub_debug);
989 static unsigned long kmem_cache_flags(unsigned long objsize,
990 unsigned long flags, const char *name,
991 void (*ctor)(void *, struct kmem_cache *, unsigned long))
994 * The page->offset field is only 16 bit wide. This is an offset
995 * in units of words from the beginning of an object. If the slab
996 * size is bigger then we cannot move the free pointer behind the
997 * object anymore.
999 * On 32 bit platforms the limit is 256k. On 64bit platforms
1000 * the limit is 512k.
1002 * Debugging or ctor may create a need to move the free
1003 * pointer. Fail if this happens.
1005 if (objsize >= 65535 * sizeof(void *)) {
1006 BUG_ON(flags & (SLAB_RED_ZONE | SLAB_POISON |
1007 SLAB_STORE_USER | SLAB_DESTROY_BY_RCU));
1008 BUG_ON(ctor);
1009 } else {
1011 * Enable debugging if selected on the kernel commandline.
1013 if (slub_debug && (!slub_debug_slabs ||
1014 strncmp(slub_debug_slabs, name,
1015 strlen(slub_debug_slabs)) == 0))
1016 flags |= slub_debug;
1019 return flags;
1021 #else
1022 static inline void setup_object_debug(struct kmem_cache *s,
1023 struct page *page, void *object) {}
1025 static inline int alloc_debug_processing(struct kmem_cache *s,
1026 struct page *page, void *object, void *addr) { return 0; }
1028 static inline int free_debug_processing(struct kmem_cache *s,
1029 struct page *page, void *object, void *addr) { return 0; }
1031 static inline int slab_pad_check(struct kmem_cache *s, struct page *page)
1032 { return 1; }
1033 static inline int check_object(struct kmem_cache *s, struct page *page,
1034 void *object, int active) { return 1; }
1035 static inline void add_full(struct kmem_cache_node *n, struct page *page) {}
1036 static inline unsigned long kmem_cache_flags(unsigned long objsize,
1037 unsigned long flags, const char *name,
1038 void (*ctor)(void *, struct kmem_cache *, unsigned long))
1040 return flags;
1042 #define slub_debug 0
1043 #endif
1045 * Slab allocation and freeing
1047 static struct page *allocate_slab(struct kmem_cache *s, gfp_t flags, int node)
1049 struct page * page;
1050 int pages = 1 << s->order;
1052 if (s->order)
1053 flags |= __GFP_COMP;
1055 if (s->flags & SLAB_CACHE_DMA)
1056 flags |= SLUB_DMA;
1058 if (node == -1)
1059 page = alloc_pages(flags, s->order);
1060 else
1061 page = alloc_pages_node(node, flags, s->order);
1063 if (!page)
1064 return NULL;
1066 mod_zone_page_state(page_zone(page),
1067 (s->flags & SLAB_RECLAIM_ACCOUNT) ?
1068 NR_SLAB_RECLAIMABLE : NR_SLAB_UNRECLAIMABLE,
1069 pages);
1071 return page;
1074 static void setup_object(struct kmem_cache *s, struct page *page,
1075 void *object)
1077 setup_object_debug(s, page, object);
1078 if (unlikely(s->ctor))
1079 s->ctor(object, s, 0);
1082 static struct page *new_slab(struct kmem_cache *s, gfp_t flags, int node)
1084 struct page *page;
1085 struct kmem_cache_node *n;
1086 void *start;
1087 void *end;
1088 void *last;
1089 void *p;
1091 BUG_ON(flags & ~(GFP_DMA | __GFP_ZERO | GFP_LEVEL_MASK));
1093 if (flags & __GFP_WAIT)
1094 local_irq_enable();
1096 page = allocate_slab(s, flags & GFP_LEVEL_MASK, node);
1097 if (!page)
1098 goto out;
1100 n = get_node(s, page_to_nid(page));
1101 if (n)
1102 atomic_long_inc(&n->nr_slabs);
1103 page->offset = s->offset / sizeof(void *);
1104 page->slab = s;
1105 page->flags |= 1 << PG_slab;
1106 if (s->flags & (SLAB_DEBUG_FREE | SLAB_RED_ZONE | SLAB_POISON |
1107 SLAB_STORE_USER | SLAB_TRACE))
1108 SetSlabDebug(page);
1110 start = page_address(page);
1111 end = start + s->objects * s->size;
1113 if (unlikely(s->flags & SLAB_POISON))
1114 memset(start, POISON_INUSE, PAGE_SIZE << s->order);
1116 last = start;
1117 for_each_object(p, s, start) {
1118 setup_object(s, page, last);
1119 set_freepointer(s, last, p);
1120 last = p;
1122 setup_object(s, page, last);
1123 set_freepointer(s, last, NULL);
1125 page->freelist = start;
1126 page->lockless_freelist = NULL;
1127 page->inuse = 0;
1128 out:
1129 if (flags & __GFP_WAIT)
1130 local_irq_disable();
1131 return page;
1134 static void __free_slab(struct kmem_cache *s, struct page *page)
1136 int pages = 1 << s->order;
1138 if (unlikely(SlabDebug(page))) {
1139 void *p;
1141 slab_pad_check(s, page);
1142 for_each_object(p, s, page_address(page))
1143 check_object(s, page, p, 0);
1144 ClearSlabDebug(page);
1147 mod_zone_page_state(page_zone(page),
1148 (s->flags & SLAB_RECLAIM_ACCOUNT) ?
1149 NR_SLAB_RECLAIMABLE : NR_SLAB_UNRECLAIMABLE,
1150 - pages);
1152 page->mapping = NULL;
1153 __free_pages(page, s->order);
1156 static void rcu_free_slab(struct rcu_head *h)
1158 struct page *page;
1160 page = container_of((struct list_head *)h, struct page, lru);
1161 __free_slab(page->slab, page);
1164 static void free_slab(struct kmem_cache *s, struct page *page)
1166 if (unlikely(s->flags & SLAB_DESTROY_BY_RCU)) {
1168 * RCU free overloads the RCU head over the LRU
1170 struct rcu_head *head = (void *)&page->lru;
1172 call_rcu(head, rcu_free_slab);
1173 } else
1174 __free_slab(s, page);
1177 static void discard_slab(struct kmem_cache *s, struct page *page)
1179 struct kmem_cache_node *n = get_node(s, page_to_nid(page));
1181 atomic_long_dec(&n->nr_slabs);
1182 reset_page_mapcount(page);
1183 __ClearPageSlab(page);
1184 free_slab(s, page);
1188 * Per slab locking using the pagelock
1190 static __always_inline void slab_lock(struct page *page)
1192 bit_spin_lock(PG_locked, &page->flags);
1195 static __always_inline void slab_unlock(struct page *page)
1197 bit_spin_unlock(PG_locked, &page->flags);
1200 static __always_inline int slab_trylock(struct page *page)
1202 int rc = 1;
1204 rc = bit_spin_trylock(PG_locked, &page->flags);
1205 return rc;
1209 * Management of partially allocated slabs
1211 static void add_partial_tail(struct kmem_cache_node *n, struct page *page)
1213 spin_lock(&n->list_lock);
1214 n->nr_partial++;
1215 list_add_tail(&page->lru, &n->partial);
1216 spin_unlock(&n->list_lock);
1219 static void add_partial(struct kmem_cache_node *n, struct page *page)
1221 spin_lock(&n->list_lock);
1222 n->nr_partial++;
1223 list_add(&page->lru, &n->partial);
1224 spin_unlock(&n->list_lock);
1227 static void remove_partial(struct kmem_cache *s,
1228 struct page *page)
1230 struct kmem_cache_node *n = get_node(s, page_to_nid(page));
1232 spin_lock(&n->list_lock);
1233 list_del(&page->lru);
1234 n->nr_partial--;
1235 spin_unlock(&n->list_lock);
1239 * Lock slab and remove from the partial list.
1241 * Must hold list_lock.
1243 static inline int lock_and_freeze_slab(struct kmem_cache_node *n, struct page *page)
1245 if (slab_trylock(page)) {
1246 list_del(&page->lru);
1247 n->nr_partial--;
1248 SetSlabFrozen(page);
1249 return 1;
1251 return 0;
1255 * Try to allocate a partial slab from a specific node.
1257 static struct page *get_partial_node(struct kmem_cache_node *n)
1259 struct page *page;
1262 * Racy check. If we mistakenly see no partial slabs then we
1263 * just allocate an empty slab. If we mistakenly try to get a
1264 * partial slab and there is none available then get_partials()
1265 * will return NULL.
1267 if (!n || !n->nr_partial)
1268 return NULL;
1270 spin_lock(&n->list_lock);
1271 list_for_each_entry(page, &n->partial, lru)
1272 if (lock_and_freeze_slab(n, page))
1273 goto out;
1274 page = NULL;
1275 out:
1276 spin_unlock(&n->list_lock);
1277 return page;
1281 * Get a page from somewhere. Search in increasing NUMA distances.
1283 static struct page *get_any_partial(struct kmem_cache *s, gfp_t flags)
1285 #ifdef CONFIG_NUMA
1286 struct zonelist *zonelist;
1287 struct zone **z;
1288 struct page *page;
1291 * The defrag ratio allows a configuration of the tradeoffs between
1292 * inter node defragmentation and node local allocations. A lower
1293 * defrag_ratio increases the tendency to do local allocations
1294 * instead of attempting to obtain partial slabs from other nodes.
1296 * If the defrag_ratio is set to 0 then kmalloc() always
1297 * returns node local objects. If the ratio is higher then kmalloc()
1298 * may return off node objects because partial slabs are obtained
1299 * from other nodes and filled up.
1301 * If /sys/slab/xx/defrag_ratio is set to 100 (which makes
1302 * defrag_ratio = 1000) then every (well almost) allocation will
1303 * first attempt to defrag slab caches on other nodes. This means
1304 * scanning over all nodes to look for partial slabs which may be
1305 * expensive if we do it every time we are trying to find a slab
1306 * with available objects.
1308 if (!s->defrag_ratio || get_cycles() % 1024 > s->defrag_ratio)
1309 return NULL;
1311 zonelist = &NODE_DATA(slab_node(current->mempolicy))
1312 ->node_zonelists[gfp_zone(flags)];
1313 for (z = zonelist->zones; *z; z++) {
1314 struct kmem_cache_node *n;
1316 n = get_node(s, zone_to_nid(*z));
1318 if (n && cpuset_zone_allowed_hardwall(*z, flags) &&
1319 n->nr_partial > MIN_PARTIAL) {
1320 page = get_partial_node(n);
1321 if (page)
1322 return page;
1325 #endif
1326 return NULL;
1330 * Get a partial page, lock it and return it.
1332 static struct page *get_partial(struct kmem_cache *s, gfp_t flags, int node)
1334 struct page *page;
1335 int searchnode = (node == -1) ? numa_node_id() : node;
1337 page = get_partial_node(get_node(s, searchnode));
1338 if (page || (flags & __GFP_THISNODE))
1339 return page;
1341 return get_any_partial(s, flags);
1345 * Move a page back to the lists.
1347 * Must be called with the slab lock held.
1349 * On exit the slab lock will have been dropped.
1351 static void unfreeze_slab(struct kmem_cache *s, struct page *page)
1353 struct kmem_cache_node *n = get_node(s, page_to_nid(page));
1355 ClearSlabFrozen(page);
1356 if (page->inuse) {
1358 if (page->freelist)
1359 add_partial(n, page);
1360 else if (SlabDebug(page) && (s->flags & SLAB_STORE_USER))
1361 add_full(n, page);
1362 slab_unlock(page);
1364 } else {
1365 if (n->nr_partial < MIN_PARTIAL) {
1367 * Adding an empty slab to the partial slabs in order
1368 * to avoid page allocator overhead. This slab needs
1369 * to come after the other slabs with objects in
1370 * order to fill them up. That way the size of the
1371 * partial list stays small. kmem_cache_shrink can
1372 * reclaim empty slabs from the partial list.
1374 add_partial_tail(n, page);
1375 slab_unlock(page);
1376 } else {
1377 slab_unlock(page);
1378 discard_slab(s, page);
1384 * Remove the cpu slab
1386 static void deactivate_slab(struct kmem_cache *s, struct page *page, int cpu)
1389 * Merge cpu freelist into freelist. Typically we get here
1390 * because both freelists are empty. So this is unlikely
1391 * to occur.
1393 while (unlikely(page->lockless_freelist)) {
1394 void **object;
1396 /* Retrieve object from cpu_freelist */
1397 object = page->lockless_freelist;
1398 page->lockless_freelist = page->lockless_freelist[page->offset];
1400 /* And put onto the regular freelist */
1401 object[page->offset] = page->freelist;
1402 page->freelist = object;
1403 page->inuse--;
1405 s->cpu_slab[cpu] = NULL;
1406 unfreeze_slab(s, page);
1409 static inline void flush_slab(struct kmem_cache *s, struct page *page, int cpu)
1411 slab_lock(page);
1412 deactivate_slab(s, page, cpu);
1416 * Flush cpu slab.
1417 * Called from IPI handler with interrupts disabled.
1419 static inline void __flush_cpu_slab(struct kmem_cache *s, int cpu)
1421 struct page *page = s->cpu_slab[cpu];
1423 if (likely(page))
1424 flush_slab(s, page, cpu);
1427 static void flush_cpu_slab(void *d)
1429 struct kmem_cache *s = d;
1430 int cpu = smp_processor_id();
1432 __flush_cpu_slab(s, cpu);
1435 static void flush_all(struct kmem_cache *s)
1437 #ifdef CONFIG_SMP
1438 on_each_cpu(flush_cpu_slab, s, 1, 1);
1439 #else
1440 unsigned long flags;
1442 local_irq_save(flags);
1443 flush_cpu_slab(s);
1444 local_irq_restore(flags);
1445 #endif
1449 * Slow path. The lockless freelist is empty or we need to perform
1450 * debugging duties.
1452 * Interrupts are disabled.
1454 * Processing is still very fast if new objects have been freed to the
1455 * regular freelist. In that case we simply take over the regular freelist
1456 * as the lockless freelist and zap the regular freelist.
1458 * If that is not working then we fall back to the partial lists. We take the
1459 * first element of the freelist as the object to allocate now and move the
1460 * rest of the freelist to the lockless freelist.
1462 * And if we were unable to get a new slab from the partial slab lists then
1463 * we need to allocate a new slab. This is slowest path since we may sleep.
1465 static void *__slab_alloc(struct kmem_cache *s,
1466 gfp_t gfpflags, int node, void *addr, struct page *page)
1468 void **object;
1469 int cpu = smp_processor_id();
1471 if (!page)
1472 goto new_slab;
1474 slab_lock(page);
1475 if (unlikely(node != -1 && page_to_nid(page) != node))
1476 goto another_slab;
1477 load_freelist:
1478 object = page->freelist;
1479 if (unlikely(!object))
1480 goto another_slab;
1481 if (unlikely(SlabDebug(page)))
1482 goto debug;
1484 object = page->freelist;
1485 page->lockless_freelist = object[page->offset];
1486 page->inuse = s->objects;
1487 page->freelist = NULL;
1488 slab_unlock(page);
1489 return object;
1491 another_slab:
1492 deactivate_slab(s, page, cpu);
1494 new_slab:
1495 page = get_partial(s, gfpflags, node);
1496 if (page) {
1497 s->cpu_slab[cpu] = page;
1498 goto load_freelist;
1501 page = new_slab(s, gfpflags, node);
1502 if (page) {
1503 cpu = smp_processor_id();
1504 if (s->cpu_slab[cpu])
1505 flush_slab(s, s->cpu_slab[cpu], cpu);
1506 slab_lock(page);
1507 SetSlabFrozen(page);
1508 s->cpu_slab[cpu] = page;
1509 goto load_freelist;
1511 return NULL;
1512 debug:
1513 object = page->freelist;
1514 if (!alloc_debug_processing(s, page, object, addr))
1515 goto another_slab;
1517 page->inuse++;
1518 page->freelist = object[page->offset];
1519 slab_unlock(page);
1520 return object;
1524 * Inlined fastpath so that allocation functions (kmalloc, kmem_cache_alloc)
1525 * have the fastpath folded into their functions. So no function call
1526 * overhead for requests that can be satisfied on the fastpath.
1528 * The fastpath works by first checking if the lockless freelist can be used.
1529 * If not then __slab_alloc is called for slow processing.
1531 * Otherwise we can simply pick the next object from the lockless free list.
1533 static void __always_inline *slab_alloc(struct kmem_cache *s,
1534 gfp_t gfpflags, int node, void *addr)
1536 struct page *page;
1537 void **object;
1538 unsigned long flags;
1540 local_irq_save(flags);
1541 page = s->cpu_slab[smp_processor_id()];
1542 if (unlikely(!page || !page->lockless_freelist ||
1543 (node != -1 && page_to_nid(page) != node)))
1545 object = __slab_alloc(s, gfpflags, node, addr, page);
1547 else {
1548 object = page->lockless_freelist;
1549 page->lockless_freelist = object[page->offset];
1551 local_irq_restore(flags);
1553 if (unlikely((gfpflags & __GFP_ZERO) && object))
1554 memset(object, 0, s->objsize);
1556 return object;
1559 void *kmem_cache_alloc(struct kmem_cache *s, gfp_t gfpflags)
1561 return slab_alloc(s, gfpflags, -1, __builtin_return_address(0));
1563 EXPORT_SYMBOL(kmem_cache_alloc);
1565 #ifdef CONFIG_NUMA
1566 void *kmem_cache_alloc_node(struct kmem_cache *s, gfp_t gfpflags, int node)
1568 return slab_alloc(s, gfpflags, node, __builtin_return_address(0));
1570 EXPORT_SYMBOL(kmem_cache_alloc_node);
1571 #endif
1574 * Slow patch handling. This may still be called frequently since objects
1575 * have a longer lifetime than the cpu slabs in most processing loads.
1577 * So we still attempt to reduce cache line usage. Just take the slab
1578 * lock and free the item. If there is no additional partial page
1579 * handling required then we can return immediately.
1581 static void __slab_free(struct kmem_cache *s, struct page *page,
1582 void *x, void *addr)
1584 void *prior;
1585 void **object = (void *)x;
1587 slab_lock(page);
1589 if (unlikely(SlabDebug(page)))
1590 goto debug;
1591 checks_ok:
1592 prior = object[page->offset] = page->freelist;
1593 page->freelist = object;
1594 page->inuse--;
1596 if (unlikely(SlabFrozen(page)))
1597 goto out_unlock;
1599 if (unlikely(!page->inuse))
1600 goto slab_empty;
1603 * Objects left in the slab. If it
1604 * was not on the partial list before
1605 * then add it.
1607 if (unlikely(!prior))
1608 add_partial(get_node(s, page_to_nid(page)), page);
1610 out_unlock:
1611 slab_unlock(page);
1612 return;
1614 slab_empty:
1615 if (prior)
1617 * Slab still on the partial list.
1619 remove_partial(s, page);
1621 slab_unlock(page);
1622 discard_slab(s, page);
1623 return;
1625 debug:
1626 if (!free_debug_processing(s, page, x, addr))
1627 goto out_unlock;
1628 goto checks_ok;
1632 * Fastpath with forced inlining to produce a kfree and kmem_cache_free that
1633 * can perform fastpath freeing without additional function calls.
1635 * The fastpath is only possible if we are freeing to the current cpu slab
1636 * of this processor. This typically the case if we have just allocated
1637 * the item before.
1639 * If fastpath is not possible then fall back to __slab_free where we deal
1640 * with all sorts of special processing.
1642 static void __always_inline slab_free(struct kmem_cache *s,
1643 struct page *page, void *x, void *addr)
1645 void **object = (void *)x;
1646 unsigned long flags;
1648 local_irq_save(flags);
1649 debug_check_no_locks_freed(object, s->objsize);
1650 if (likely(page == s->cpu_slab[smp_processor_id()] &&
1651 !SlabDebug(page))) {
1652 object[page->offset] = page->lockless_freelist;
1653 page->lockless_freelist = object;
1654 } else
1655 __slab_free(s, page, x, addr);
1657 local_irq_restore(flags);
1660 void kmem_cache_free(struct kmem_cache *s, void *x)
1662 struct page *page;
1664 page = virt_to_head_page(x);
1666 slab_free(s, page, x, __builtin_return_address(0));
1668 EXPORT_SYMBOL(kmem_cache_free);
1670 /* Figure out on which slab object the object resides */
1671 static struct page *get_object_page(const void *x)
1673 struct page *page = virt_to_head_page(x);
1675 if (!PageSlab(page))
1676 return NULL;
1678 return page;
1682 * Object placement in a slab is made very easy because we always start at
1683 * offset 0. If we tune the size of the object to the alignment then we can
1684 * get the required alignment by putting one properly sized object after
1685 * another.
1687 * Notice that the allocation order determines the sizes of the per cpu
1688 * caches. Each processor has always one slab available for allocations.
1689 * Increasing the allocation order reduces the number of times that slabs
1690 * must be moved on and off the partial lists and is therefore a factor in
1691 * locking overhead.
1695 * Mininum / Maximum order of slab pages. This influences locking overhead
1696 * and slab fragmentation. A higher order reduces the number of partial slabs
1697 * and increases the number of allocations possible without having to
1698 * take the list_lock.
1700 static int slub_min_order;
1701 static int slub_max_order = DEFAULT_MAX_ORDER;
1702 static int slub_min_objects = DEFAULT_MIN_OBJECTS;
1705 * Merge control. If this is set then no merging of slab caches will occur.
1706 * (Could be removed. This was introduced to pacify the merge skeptics.)
1708 static int slub_nomerge;
1711 * Calculate the order of allocation given an slab object size.
1713 * The order of allocation has significant impact on performance and other
1714 * system components. Generally order 0 allocations should be preferred since
1715 * order 0 does not cause fragmentation in the page allocator. Larger objects
1716 * be problematic to put into order 0 slabs because there may be too much
1717 * unused space left. We go to a higher order if more than 1/8th of the slab
1718 * would be wasted.
1720 * In order to reach satisfactory performance we must ensure that a minimum
1721 * number of objects is in one slab. Otherwise we may generate too much
1722 * activity on the partial lists which requires taking the list_lock. This is
1723 * less a concern for large slabs though which are rarely used.
1725 * slub_max_order specifies the order where we begin to stop considering the
1726 * number of objects in a slab as critical. If we reach slub_max_order then
1727 * we try to keep the page order as low as possible. So we accept more waste
1728 * of space in favor of a small page order.
1730 * Higher order allocations also allow the placement of more objects in a
1731 * slab and thereby reduce object handling overhead. If the user has
1732 * requested a higher mininum order then we start with that one instead of
1733 * the smallest order which will fit the object.
1735 static inline int slab_order(int size, int min_objects,
1736 int max_order, int fract_leftover)
1738 int order;
1739 int rem;
1740 int min_order = slub_min_order;
1743 * If we would create too many object per slab then reduce
1744 * the slab order even if it goes below slub_min_order.
1746 while (min_order > 0 &&
1747 (PAGE_SIZE << min_order) >= MAX_OBJECTS_PER_SLAB * size)
1748 min_order--;
1750 for (order = max(min_order,
1751 fls(min_objects * size - 1) - PAGE_SHIFT);
1752 order <= max_order; order++) {
1754 unsigned long slab_size = PAGE_SIZE << order;
1756 if (slab_size < min_objects * size)
1757 continue;
1759 rem = slab_size % size;
1761 if (rem <= slab_size / fract_leftover)
1762 break;
1764 /* If the next size is too high then exit now */
1765 if (slab_size * 2 >= MAX_OBJECTS_PER_SLAB * size)
1766 break;
1769 return order;
1772 static inline int calculate_order(int size)
1774 int order;
1775 int min_objects;
1776 int fraction;
1779 * Attempt to find best configuration for a slab. This
1780 * works by first attempting to generate a layout with
1781 * the best configuration and backing off gradually.
1783 * First we reduce the acceptable waste in a slab. Then
1784 * we reduce the minimum objects required in a slab.
1786 min_objects = slub_min_objects;
1787 while (min_objects > 1) {
1788 fraction = 8;
1789 while (fraction >= 4) {
1790 order = slab_order(size, min_objects,
1791 slub_max_order, fraction);
1792 if (order <= slub_max_order)
1793 return order;
1794 fraction /= 2;
1796 min_objects /= 2;
1800 * We were unable to place multiple objects in a slab. Now
1801 * lets see if we can place a single object there.
1803 order = slab_order(size, 1, slub_max_order, 1);
1804 if (order <= slub_max_order)
1805 return order;
1808 * Doh this slab cannot be placed using slub_max_order.
1810 order = slab_order(size, 1, MAX_ORDER, 1);
1811 if (order <= MAX_ORDER)
1812 return order;
1813 return -ENOSYS;
1817 * Figure out what the alignment of the objects will be.
1819 static unsigned long calculate_alignment(unsigned long flags,
1820 unsigned long align, unsigned long size)
1823 * If the user wants hardware cache aligned objects then
1824 * follow that suggestion if the object is sufficiently
1825 * large.
1827 * The hardware cache alignment cannot override the
1828 * specified alignment though. If that is greater
1829 * then use it.
1831 if ((flags & SLAB_HWCACHE_ALIGN) &&
1832 size > cache_line_size() / 2)
1833 return max_t(unsigned long, align, cache_line_size());
1835 if (align < ARCH_SLAB_MINALIGN)
1836 return ARCH_SLAB_MINALIGN;
1838 return ALIGN(align, sizeof(void *));
1841 static void init_kmem_cache_node(struct kmem_cache_node *n)
1843 n->nr_partial = 0;
1844 atomic_long_set(&n->nr_slabs, 0);
1845 spin_lock_init(&n->list_lock);
1846 INIT_LIST_HEAD(&n->partial);
1847 #ifdef CONFIG_SLUB_DEBUG
1848 INIT_LIST_HEAD(&n->full);
1849 #endif
1852 #ifdef CONFIG_NUMA
1854 * No kmalloc_node yet so do it by hand. We know that this is the first
1855 * slab on the node for this slabcache. There are no concurrent accesses
1856 * possible.
1858 * Note that this function only works on the kmalloc_node_cache
1859 * when allocating for the kmalloc_node_cache.
1861 static struct kmem_cache_node * __init early_kmem_cache_node_alloc(gfp_t gfpflags,
1862 int node)
1864 struct page *page;
1865 struct kmem_cache_node *n;
1867 BUG_ON(kmalloc_caches->size < sizeof(struct kmem_cache_node));
1869 page = new_slab(kmalloc_caches, gfpflags, node);
1871 BUG_ON(!page);
1872 if (page_to_nid(page) != node) {
1873 printk(KERN_ERR "SLUB: Unable to allocate memory from "
1874 "node %d\n", node);
1875 printk(KERN_ERR "SLUB: Allocating a useless per node structure "
1876 "in order to be able to continue\n");
1879 n = page->freelist;
1880 BUG_ON(!n);
1881 page->freelist = get_freepointer(kmalloc_caches, n);
1882 page->inuse++;
1883 kmalloc_caches->node[node] = n;
1884 #ifdef CONFIG_SLUB_DEBUG
1885 init_object(kmalloc_caches, n, 1);
1886 init_tracking(kmalloc_caches, n);
1887 #endif
1888 init_kmem_cache_node(n);
1889 atomic_long_inc(&n->nr_slabs);
1890 add_partial(n, page);
1893 * new_slab() disables interupts. If we do not reenable interrupts here
1894 * then bootup would continue with interrupts disabled.
1896 local_irq_enable();
1897 return n;
1900 static void free_kmem_cache_nodes(struct kmem_cache *s)
1902 int node;
1904 for_each_online_node(node) {
1905 struct kmem_cache_node *n = s->node[node];
1906 if (n && n != &s->local_node)
1907 kmem_cache_free(kmalloc_caches, n);
1908 s->node[node] = NULL;
1912 static int init_kmem_cache_nodes(struct kmem_cache *s, gfp_t gfpflags)
1914 int node;
1915 int local_node;
1917 if (slab_state >= UP)
1918 local_node = page_to_nid(virt_to_page(s));
1919 else
1920 local_node = 0;
1922 for_each_online_node(node) {
1923 struct kmem_cache_node *n;
1925 if (local_node == node)
1926 n = &s->local_node;
1927 else {
1928 if (slab_state == DOWN) {
1929 n = early_kmem_cache_node_alloc(gfpflags,
1930 node);
1931 continue;
1933 n = kmem_cache_alloc_node(kmalloc_caches,
1934 gfpflags, node);
1936 if (!n) {
1937 free_kmem_cache_nodes(s);
1938 return 0;
1942 s->node[node] = n;
1943 init_kmem_cache_node(n);
1945 return 1;
1947 #else
1948 static void free_kmem_cache_nodes(struct kmem_cache *s)
1952 static int init_kmem_cache_nodes(struct kmem_cache *s, gfp_t gfpflags)
1954 init_kmem_cache_node(&s->local_node);
1955 return 1;
1957 #endif
1960 * calculate_sizes() determines the order and the distribution of data within
1961 * a slab object.
1963 static int calculate_sizes(struct kmem_cache *s)
1965 unsigned long flags = s->flags;
1966 unsigned long size = s->objsize;
1967 unsigned long align = s->align;
1970 * Determine if we can poison the object itself. If the user of
1971 * the slab may touch the object after free or before allocation
1972 * then we should never poison the object itself.
1974 if ((flags & SLAB_POISON) && !(flags & SLAB_DESTROY_BY_RCU) &&
1975 !s->ctor)
1976 s->flags |= __OBJECT_POISON;
1977 else
1978 s->flags &= ~__OBJECT_POISON;
1981 * Round up object size to the next word boundary. We can only
1982 * place the free pointer at word boundaries and this determines
1983 * the possible location of the free pointer.
1985 size = ALIGN(size, sizeof(void *));
1987 #ifdef CONFIG_SLUB_DEBUG
1989 * If we are Redzoning then check if there is some space between the
1990 * end of the object and the free pointer. If not then add an
1991 * additional word to have some bytes to store Redzone information.
1993 if ((flags & SLAB_RED_ZONE) && size == s->objsize)
1994 size += sizeof(void *);
1995 #endif
1998 * With that we have determined the number of bytes in actual use
1999 * by the object. This is the potential offset to the free pointer.
2001 s->inuse = size;
2003 if (((flags & (SLAB_DESTROY_BY_RCU | SLAB_POISON)) ||
2004 s->ctor)) {
2006 * Relocate free pointer after the object if it is not
2007 * permitted to overwrite the first word of the object on
2008 * kmem_cache_free.
2010 * This is the case if we do RCU, have a constructor or
2011 * destructor or are poisoning the objects.
2013 s->offset = size;
2014 size += sizeof(void *);
2017 #ifdef CONFIG_SLUB_DEBUG
2018 if (flags & SLAB_STORE_USER)
2020 * Need to store information about allocs and frees after
2021 * the object.
2023 size += 2 * sizeof(struct track);
2025 if (flags & SLAB_RED_ZONE)
2027 * Add some empty padding so that we can catch
2028 * overwrites from earlier objects rather than let
2029 * tracking information or the free pointer be
2030 * corrupted if an user writes before the start
2031 * of the object.
2033 size += sizeof(void *);
2034 #endif
2037 * Determine the alignment based on various parameters that the
2038 * user specified and the dynamic determination of cache line size
2039 * on bootup.
2041 align = calculate_alignment(flags, align, s->objsize);
2044 * SLUB stores one object immediately after another beginning from
2045 * offset 0. In order to align the objects we have to simply size
2046 * each object to conform to the alignment.
2048 size = ALIGN(size, align);
2049 s->size = size;
2051 s->order = calculate_order(size);
2052 if (s->order < 0)
2053 return 0;
2056 * Determine the number of objects per slab
2058 s->objects = (PAGE_SIZE << s->order) / size;
2061 * Verify that the number of objects is within permitted limits.
2062 * The page->inuse field is only 16 bit wide! So we cannot have
2063 * more than 64k objects per slab.
2065 if (!s->objects || s->objects > MAX_OBJECTS_PER_SLAB)
2066 return 0;
2067 return 1;
2071 static int kmem_cache_open(struct kmem_cache *s, gfp_t gfpflags,
2072 const char *name, size_t size,
2073 size_t align, unsigned long flags,
2074 void (*ctor)(void *, struct kmem_cache *, unsigned long))
2076 memset(s, 0, kmem_size);
2077 s->name = name;
2078 s->ctor = ctor;
2079 s->objsize = size;
2080 s->align = align;
2081 s->flags = kmem_cache_flags(size, flags, name, ctor);
2083 if (!calculate_sizes(s))
2084 goto error;
2086 s->refcount = 1;
2087 #ifdef CONFIG_NUMA
2088 s->defrag_ratio = 100;
2089 #endif
2091 if (init_kmem_cache_nodes(s, gfpflags & ~SLUB_DMA))
2092 return 1;
2093 error:
2094 if (flags & SLAB_PANIC)
2095 panic("Cannot create slab %s size=%lu realsize=%u "
2096 "order=%u offset=%u flags=%lx\n",
2097 s->name, (unsigned long)size, s->size, s->order,
2098 s->offset, flags);
2099 return 0;
2103 * Check if a given pointer is valid
2105 int kmem_ptr_validate(struct kmem_cache *s, const void *object)
2107 struct page * page;
2109 page = get_object_page(object);
2111 if (!page || s != page->slab)
2112 /* No slab or wrong slab */
2113 return 0;
2115 if (!check_valid_pointer(s, page, object))
2116 return 0;
2119 * We could also check if the object is on the slabs freelist.
2120 * But this would be too expensive and it seems that the main
2121 * purpose of kmem_ptr_valid is to check if the object belongs
2122 * to a certain slab.
2124 return 1;
2126 EXPORT_SYMBOL(kmem_ptr_validate);
2129 * Determine the size of a slab object
2131 unsigned int kmem_cache_size(struct kmem_cache *s)
2133 return s->objsize;
2135 EXPORT_SYMBOL(kmem_cache_size);
2137 const char *kmem_cache_name(struct kmem_cache *s)
2139 return s->name;
2141 EXPORT_SYMBOL(kmem_cache_name);
2144 * Attempt to free all slabs on a node. Return the number of slabs we
2145 * were unable to free.
2147 static int free_list(struct kmem_cache *s, struct kmem_cache_node *n,
2148 struct list_head *list)
2150 int slabs_inuse = 0;
2151 unsigned long flags;
2152 struct page *page, *h;
2154 spin_lock_irqsave(&n->list_lock, flags);
2155 list_for_each_entry_safe(page, h, list, lru)
2156 if (!page->inuse) {
2157 list_del(&page->lru);
2158 discard_slab(s, page);
2159 } else
2160 slabs_inuse++;
2161 spin_unlock_irqrestore(&n->list_lock, flags);
2162 return slabs_inuse;
2166 * Release all resources used by a slab cache.
2168 static inline int kmem_cache_close(struct kmem_cache *s)
2170 int node;
2172 flush_all(s);
2174 /* Attempt to free all objects */
2175 for_each_online_node(node) {
2176 struct kmem_cache_node *n = get_node(s, node);
2178 n->nr_partial -= free_list(s, n, &n->partial);
2179 if (atomic_long_read(&n->nr_slabs))
2180 return 1;
2182 free_kmem_cache_nodes(s);
2183 return 0;
2187 * Close a cache and release the kmem_cache structure
2188 * (must be used for caches created using kmem_cache_create)
2190 void kmem_cache_destroy(struct kmem_cache *s)
2192 down_write(&slub_lock);
2193 s->refcount--;
2194 if (!s->refcount) {
2195 list_del(&s->list);
2196 up_write(&slub_lock);
2197 if (kmem_cache_close(s))
2198 WARN_ON(1);
2199 sysfs_slab_remove(s);
2200 kfree(s);
2201 } else
2202 up_write(&slub_lock);
2204 EXPORT_SYMBOL(kmem_cache_destroy);
2206 /********************************************************************
2207 * Kmalloc subsystem
2208 *******************************************************************/
2210 struct kmem_cache kmalloc_caches[KMALLOC_SHIFT_HIGH + 1] __cacheline_aligned;
2211 EXPORT_SYMBOL(kmalloc_caches);
2213 #ifdef CONFIG_ZONE_DMA
2214 static struct kmem_cache *kmalloc_caches_dma[KMALLOC_SHIFT_HIGH + 1];
2215 #endif
2217 static int __init setup_slub_min_order(char *str)
2219 get_option (&str, &slub_min_order);
2221 return 1;
2224 __setup("slub_min_order=", setup_slub_min_order);
2226 static int __init setup_slub_max_order(char *str)
2228 get_option (&str, &slub_max_order);
2230 return 1;
2233 __setup("slub_max_order=", setup_slub_max_order);
2235 static int __init setup_slub_min_objects(char *str)
2237 get_option (&str, &slub_min_objects);
2239 return 1;
2242 __setup("slub_min_objects=", setup_slub_min_objects);
2244 static int __init setup_slub_nomerge(char *str)
2246 slub_nomerge = 1;
2247 return 1;
2250 __setup("slub_nomerge", setup_slub_nomerge);
2252 static struct kmem_cache *create_kmalloc_cache(struct kmem_cache *s,
2253 const char *name, int size, gfp_t gfp_flags)
2255 unsigned int flags = 0;
2257 if (gfp_flags & SLUB_DMA)
2258 flags = SLAB_CACHE_DMA;
2260 down_write(&slub_lock);
2261 if (!kmem_cache_open(s, gfp_flags, name, size, ARCH_KMALLOC_MINALIGN,
2262 flags, NULL))
2263 goto panic;
2265 list_add(&s->list, &slab_caches);
2266 up_write(&slub_lock);
2267 if (sysfs_slab_add(s))
2268 goto panic;
2269 return s;
2271 panic:
2272 panic("Creation of kmalloc slab %s size=%d failed.\n", name, size);
2275 #ifdef CONFIG_ZONE_DMA
2277 static void sysfs_add_func(struct work_struct *w)
2279 struct kmem_cache *s;
2281 down_write(&slub_lock);
2282 list_for_each_entry(s, &slab_caches, list) {
2283 if (s->flags & __SYSFS_ADD_DEFERRED) {
2284 s->flags &= ~__SYSFS_ADD_DEFERRED;
2285 sysfs_slab_add(s);
2288 up_write(&slub_lock);
2291 static DECLARE_WORK(sysfs_add_work, sysfs_add_func);
2293 static noinline struct kmem_cache *dma_kmalloc_cache(int index, gfp_t flags)
2295 struct kmem_cache *s;
2296 char *text;
2297 size_t realsize;
2299 s = kmalloc_caches_dma[index];
2300 if (s)
2301 return s;
2303 /* Dynamically create dma cache */
2304 if (flags & __GFP_WAIT)
2305 down_write(&slub_lock);
2306 else {
2307 if (!down_write_trylock(&slub_lock))
2308 goto out;
2311 if (kmalloc_caches_dma[index])
2312 goto unlock_out;
2314 realsize = kmalloc_caches[index].objsize;
2315 text = kasprintf(flags & ~SLUB_DMA, "kmalloc_dma-%d", (unsigned int)realsize),
2316 s = kmalloc(kmem_size, flags & ~SLUB_DMA);
2318 if (!s || !text || !kmem_cache_open(s, flags, text,
2319 realsize, ARCH_KMALLOC_MINALIGN,
2320 SLAB_CACHE_DMA|__SYSFS_ADD_DEFERRED, NULL)) {
2321 kfree(s);
2322 kfree(text);
2323 goto unlock_out;
2326 list_add(&s->list, &slab_caches);
2327 kmalloc_caches_dma[index] = s;
2329 schedule_work(&sysfs_add_work);
2331 unlock_out:
2332 up_write(&slub_lock);
2333 out:
2334 return kmalloc_caches_dma[index];
2336 #endif
2339 * Conversion table for small slabs sizes / 8 to the index in the
2340 * kmalloc array. This is necessary for slabs < 192 since we have non power
2341 * of two cache sizes there. The size of larger slabs can be determined using
2342 * fls.
2344 static s8 size_index[24] = {
2345 3, /* 8 */
2346 4, /* 16 */
2347 5, /* 24 */
2348 5, /* 32 */
2349 6, /* 40 */
2350 6, /* 48 */
2351 6, /* 56 */
2352 6, /* 64 */
2353 1, /* 72 */
2354 1, /* 80 */
2355 1, /* 88 */
2356 1, /* 96 */
2357 7, /* 104 */
2358 7, /* 112 */
2359 7, /* 120 */
2360 7, /* 128 */
2361 2, /* 136 */
2362 2, /* 144 */
2363 2, /* 152 */
2364 2, /* 160 */
2365 2, /* 168 */
2366 2, /* 176 */
2367 2, /* 184 */
2368 2 /* 192 */
2371 static struct kmem_cache *get_slab(size_t size, gfp_t flags)
2373 int index;
2375 if (size <= 192) {
2376 if (!size)
2377 return ZERO_SIZE_PTR;
2379 index = size_index[(size - 1) / 8];
2380 } else {
2381 if (size > KMALLOC_MAX_SIZE)
2382 return NULL;
2384 index = fls(size - 1);
2387 #ifdef CONFIG_ZONE_DMA
2388 if (unlikely((flags & SLUB_DMA)))
2389 return dma_kmalloc_cache(index, flags);
2391 #endif
2392 return &kmalloc_caches[index];
2395 void *__kmalloc(size_t size, gfp_t flags)
2397 struct kmem_cache *s = get_slab(size, flags);
2399 if (ZERO_OR_NULL_PTR(s))
2400 return s;
2402 return slab_alloc(s, flags, -1, __builtin_return_address(0));
2404 EXPORT_SYMBOL(__kmalloc);
2406 #ifdef CONFIG_NUMA
2407 void *__kmalloc_node(size_t size, gfp_t flags, int node)
2409 struct kmem_cache *s = get_slab(size, flags);
2411 if (ZERO_OR_NULL_PTR(s))
2412 return s;
2414 return slab_alloc(s, flags, node, __builtin_return_address(0));
2416 EXPORT_SYMBOL(__kmalloc_node);
2417 #endif
2419 size_t ksize(const void *object)
2421 struct page *page;
2422 struct kmem_cache *s;
2424 if (ZERO_OR_NULL_PTR(object))
2425 return 0;
2427 page = get_object_page(object);
2428 BUG_ON(!page);
2429 s = page->slab;
2430 BUG_ON(!s);
2433 * Debugging requires use of the padding between object
2434 * and whatever may come after it.
2436 if (s->flags & (SLAB_RED_ZONE | SLAB_POISON))
2437 return s->objsize;
2440 * If we have the need to store the freelist pointer
2441 * back there or track user information then we can
2442 * only use the space before that information.
2444 if (s->flags & (SLAB_DESTROY_BY_RCU | SLAB_STORE_USER))
2445 return s->inuse;
2448 * Else we can use all the padding etc for the allocation
2450 return s->size;
2452 EXPORT_SYMBOL(ksize);
2454 void kfree(const void *x)
2456 struct kmem_cache *s;
2457 struct page *page;
2460 * This has to be an unsigned comparison. According to Linus
2461 * some gcc version treat a pointer as a signed entity. Then
2462 * this comparison would be true for all "negative" pointers
2463 * (which would cover the whole upper half of the address space).
2465 if (ZERO_OR_NULL_PTR(x))
2466 return;
2468 page = virt_to_head_page(x);
2469 s = page->slab;
2471 slab_free(s, page, (void *)x, __builtin_return_address(0));
2473 EXPORT_SYMBOL(kfree);
2476 * kmem_cache_shrink removes empty slabs from the partial lists and sorts
2477 * the remaining slabs by the number of items in use. The slabs with the
2478 * most items in use come first. New allocations will then fill those up
2479 * and thus they can be removed from the partial lists.
2481 * The slabs with the least items are placed last. This results in them
2482 * being allocated from last increasing the chance that the last objects
2483 * are freed in them.
2485 int kmem_cache_shrink(struct kmem_cache *s)
2487 int node;
2488 int i;
2489 struct kmem_cache_node *n;
2490 struct page *page;
2491 struct page *t;
2492 struct list_head *slabs_by_inuse =
2493 kmalloc(sizeof(struct list_head) * s->objects, GFP_KERNEL);
2494 unsigned long flags;
2496 if (!slabs_by_inuse)
2497 return -ENOMEM;
2499 flush_all(s);
2500 for_each_online_node(node) {
2501 n = get_node(s, node);
2503 if (!n->nr_partial)
2504 continue;
2506 for (i = 0; i < s->objects; i++)
2507 INIT_LIST_HEAD(slabs_by_inuse + i);
2509 spin_lock_irqsave(&n->list_lock, flags);
2512 * Build lists indexed by the items in use in each slab.
2514 * Note that concurrent frees may occur while we hold the
2515 * list_lock. page->inuse here is the upper limit.
2517 list_for_each_entry_safe(page, t, &n->partial, lru) {
2518 if (!page->inuse && slab_trylock(page)) {
2520 * Must hold slab lock here because slab_free
2521 * may have freed the last object and be
2522 * waiting to release the slab.
2524 list_del(&page->lru);
2525 n->nr_partial--;
2526 slab_unlock(page);
2527 discard_slab(s, page);
2528 } else {
2529 list_move(&page->lru,
2530 slabs_by_inuse + page->inuse);
2535 * Rebuild the partial list with the slabs filled up most
2536 * first and the least used slabs at the end.
2538 for (i = s->objects - 1; i >= 0; i--)
2539 list_splice(slabs_by_inuse + i, n->partial.prev);
2541 spin_unlock_irqrestore(&n->list_lock, flags);
2544 kfree(slabs_by_inuse);
2545 return 0;
2547 EXPORT_SYMBOL(kmem_cache_shrink);
2549 /********************************************************************
2550 * Basic setup of slabs
2551 *******************************************************************/
2553 void __init kmem_cache_init(void)
2555 int i;
2556 int caches = 0;
2558 #ifdef CONFIG_NUMA
2560 * Must first have the slab cache available for the allocations of the
2561 * struct kmem_cache_node's. There is special bootstrap code in
2562 * kmem_cache_open for slab_state == DOWN.
2564 create_kmalloc_cache(&kmalloc_caches[0], "kmem_cache_node",
2565 sizeof(struct kmem_cache_node), GFP_KERNEL);
2566 kmalloc_caches[0].refcount = -1;
2567 caches++;
2568 #endif
2570 /* Able to allocate the per node structures */
2571 slab_state = PARTIAL;
2573 /* Caches that are not of the two-to-the-power-of size */
2574 if (KMALLOC_MIN_SIZE <= 64) {
2575 create_kmalloc_cache(&kmalloc_caches[1],
2576 "kmalloc-96", 96, GFP_KERNEL);
2577 caches++;
2579 if (KMALLOC_MIN_SIZE <= 128) {
2580 create_kmalloc_cache(&kmalloc_caches[2],
2581 "kmalloc-192", 192, GFP_KERNEL);
2582 caches++;
2585 for (i = KMALLOC_SHIFT_LOW; i <= KMALLOC_SHIFT_HIGH; i++) {
2586 create_kmalloc_cache(&kmalloc_caches[i],
2587 "kmalloc", 1 << i, GFP_KERNEL);
2588 caches++;
2593 * Patch up the size_index table if we have strange large alignment
2594 * requirements for the kmalloc array. This is only the case for
2595 * mips it seems. The standard arches will not generate any code here.
2597 * Largest permitted alignment is 256 bytes due to the way we
2598 * handle the index determination for the smaller caches.
2600 * Make sure that nothing crazy happens if someone starts tinkering
2601 * around with ARCH_KMALLOC_MINALIGN
2603 BUILD_BUG_ON(KMALLOC_MIN_SIZE > 256 ||
2604 (KMALLOC_MIN_SIZE & (KMALLOC_MIN_SIZE - 1)));
2606 for (i = 8; i < KMALLOC_MIN_SIZE; i += 8)
2607 size_index[(i - 1) / 8] = KMALLOC_SHIFT_LOW;
2609 slab_state = UP;
2611 /* Provide the correct kmalloc names now that the caches are up */
2612 for (i = KMALLOC_SHIFT_LOW; i <= KMALLOC_SHIFT_HIGH; i++)
2613 kmalloc_caches[i]. name =
2614 kasprintf(GFP_KERNEL, "kmalloc-%d", 1 << i);
2616 #ifdef CONFIG_SMP
2617 register_cpu_notifier(&slab_notifier);
2618 #endif
2620 kmem_size = offsetof(struct kmem_cache, cpu_slab) +
2621 nr_cpu_ids * sizeof(struct page *);
2623 printk(KERN_INFO "SLUB: Genslabs=%d, HWalign=%d, Order=%d-%d, MinObjects=%d,"
2624 " CPUs=%d, Nodes=%d\n",
2625 caches, cache_line_size(),
2626 slub_min_order, slub_max_order, slub_min_objects,
2627 nr_cpu_ids, nr_node_ids);
2631 * Find a mergeable slab cache
2633 static int slab_unmergeable(struct kmem_cache *s)
2635 if (slub_nomerge || (s->flags & SLUB_NEVER_MERGE))
2636 return 1;
2638 if (s->ctor)
2639 return 1;
2642 * We may have set a slab to be unmergeable during bootstrap.
2644 if (s->refcount < 0)
2645 return 1;
2647 return 0;
2650 static struct kmem_cache *find_mergeable(size_t size,
2651 size_t align, unsigned long flags, const char *name,
2652 void (*ctor)(void *, struct kmem_cache *, unsigned long))
2654 struct kmem_cache *s;
2656 if (slub_nomerge || (flags & SLUB_NEVER_MERGE))
2657 return NULL;
2659 if (ctor)
2660 return NULL;
2662 size = ALIGN(size, sizeof(void *));
2663 align = calculate_alignment(flags, align, size);
2664 size = ALIGN(size, align);
2665 flags = kmem_cache_flags(size, flags, name, NULL);
2667 list_for_each_entry(s, &slab_caches, list) {
2668 if (slab_unmergeable(s))
2669 continue;
2671 if (size > s->size)
2672 continue;
2674 if ((flags & SLUB_MERGE_SAME) != (s->flags & SLUB_MERGE_SAME))
2675 continue;
2677 * Check if alignment is compatible.
2678 * Courtesy of Adrian Drzewiecki
2680 if ((s->size & ~(align -1)) != s->size)
2681 continue;
2683 if (s->size - size >= sizeof(void *))
2684 continue;
2686 return s;
2688 return NULL;
2691 struct kmem_cache *kmem_cache_create(const char *name, size_t size,
2692 size_t align, unsigned long flags,
2693 void (*ctor)(void *, struct kmem_cache *, unsigned long))
2695 struct kmem_cache *s;
2697 down_write(&slub_lock);
2698 s = find_mergeable(size, align, flags, name, ctor);
2699 if (s) {
2700 s->refcount++;
2702 * Adjust the object sizes so that we clear
2703 * the complete object on kzalloc.
2705 s->objsize = max(s->objsize, (int)size);
2706 s->inuse = max_t(int, s->inuse, ALIGN(size, sizeof(void *)));
2707 up_write(&slub_lock);
2708 if (sysfs_slab_alias(s, name))
2709 goto err;
2710 return s;
2712 s = kmalloc(kmem_size, GFP_KERNEL);
2713 if (s) {
2714 if (kmem_cache_open(s, GFP_KERNEL, name,
2715 size, align, flags, ctor)) {
2716 list_add(&s->list, &slab_caches);
2717 up_write(&slub_lock);
2718 if (sysfs_slab_add(s))
2719 goto err;
2720 return s;
2722 kfree(s);
2724 up_write(&slub_lock);
2726 err:
2727 if (flags & SLAB_PANIC)
2728 panic("Cannot create slabcache %s\n", name);
2729 else
2730 s = NULL;
2731 return s;
2733 EXPORT_SYMBOL(kmem_cache_create);
2735 #ifdef CONFIG_SMP
2737 * Use the cpu notifier to insure that the cpu slabs are flushed when
2738 * necessary.
2740 static int __cpuinit slab_cpuup_callback(struct notifier_block *nfb,
2741 unsigned long action, void *hcpu)
2743 long cpu = (long)hcpu;
2744 struct kmem_cache *s;
2745 unsigned long flags;
2747 switch (action) {
2748 case CPU_UP_CANCELED:
2749 case CPU_UP_CANCELED_FROZEN:
2750 case CPU_DEAD:
2751 case CPU_DEAD_FROZEN:
2752 down_read(&slub_lock);
2753 list_for_each_entry(s, &slab_caches, list) {
2754 local_irq_save(flags);
2755 __flush_cpu_slab(s, cpu);
2756 local_irq_restore(flags);
2758 up_read(&slub_lock);
2759 break;
2760 default:
2761 break;
2763 return NOTIFY_OK;
2766 static struct notifier_block __cpuinitdata slab_notifier =
2767 { &slab_cpuup_callback, NULL, 0 };
2769 #endif
2771 void *__kmalloc_track_caller(size_t size, gfp_t gfpflags, void *caller)
2773 struct kmem_cache *s = get_slab(size, gfpflags);
2775 if (ZERO_OR_NULL_PTR(s))
2776 return s;
2778 return slab_alloc(s, gfpflags, -1, caller);
2781 void *__kmalloc_node_track_caller(size_t size, gfp_t gfpflags,
2782 int node, void *caller)
2784 struct kmem_cache *s = get_slab(size, gfpflags);
2786 if (ZERO_OR_NULL_PTR(s))
2787 return s;
2789 return slab_alloc(s, gfpflags, node, caller);
2792 #if defined(CONFIG_SYSFS) && defined(CONFIG_SLUB_DEBUG)
2793 static int validate_slab(struct kmem_cache *s, struct page *page,
2794 unsigned long *map)
2796 void *p;
2797 void *addr = page_address(page);
2799 if (!check_slab(s, page) ||
2800 !on_freelist(s, page, NULL))
2801 return 0;
2803 /* Now we know that a valid freelist exists */
2804 bitmap_zero(map, s->objects);
2806 for_each_free_object(p, s, page->freelist) {
2807 set_bit(slab_index(p, s, addr), map);
2808 if (!check_object(s, page, p, 0))
2809 return 0;
2812 for_each_object(p, s, addr)
2813 if (!test_bit(slab_index(p, s, addr), map))
2814 if (!check_object(s, page, p, 1))
2815 return 0;
2816 return 1;
2819 static void validate_slab_slab(struct kmem_cache *s, struct page *page,
2820 unsigned long *map)
2822 if (slab_trylock(page)) {
2823 validate_slab(s, page, map);
2824 slab_unlock(page);
2825 } else
2826 printk(KERN_INFO "SLUB %s: Skipped busy slab 0x%p\n",
2827 s->name, page);
2829 if (s->flags & DEBUG_DEFAULT_FLAGS) {
2830 if (!SlabDebug(page))
2831 printk(KERN_ERR "SLUB %s: SlabDebug not set "
2832 "on slab 0x%p\n", s->name, page);
2833 } else {
2834 if (SlabDebug(page))
2835 printk(KERN_ERR "SLUB %s: SlabDebug set on "
2836 "slab 0x%p\n", s->name, page);
2840 static int validate_slab_node(struct kmem_cache *s,
2841 struct kmem_cache_node *n, unsigned long *map)
2843 unsigned long count = 0;
2844 struct page *page;
2845 unsigned long flags;
2847 spin_lock_irqsave(&n->list_lock, flags);
2849 list_for_each_entry(page, &n->partial, lru) {
2850 validate_slab_slab(s, page, map);
2851 count++;
2853 if (count != n->nr_partial)
2854 printk(KERN_ERR "SLUB %s: %ld partial slabs counted but "
2855 "counter=%ld\n", s->name, count, n->nr_partial);
2857 if (!(s->flags & SLAB_STORE_USER))
2858 goto out;
2860 list_for_each_entry(page, &n->full, lru) {
2861 validate_slab_slab(s, page, map);
2862 count++;
2864 if (count != atomic_long_read(&n->nr_slabs))
2865 printk(KERN_ERR "SLUB: %s %ld slabs counted but "
2866 "counter=%ld\n", s->name, count,
2867 atomic_long_read(&n->nr_slabs));
2869 out:
2870 spin_unlock_irqrestore(&n->list_lock, flags);
2871 return count;
2874 static long validate_slab_cache(struct kmem_cache *s)
2876 int node;
2877 unsigned long count = 0;
2878 unsigned long *map = kmalloc(BITS_TO_LONGS(s->objects) *
2879 sizeof(unsigned long), GFP_KERNEL);
2881 if (!map)
2882 return -ENOMEM;
2884 flush_all(s);
2885 for_each_online_node(node) {
2886 struct kmem_cache_node *n = get_node(s, node);
2888 count += validate_slab_node(s, n, map);
2890 kfree(map);
2891 return count;
2894 #ifdef SLUB_RESILIENCY_TEST
2895 static void resiliency_test(void)
2897 u8 *p;
2899 printk(KERN_ERR "SLUB resiliency testing\n");
2900 printk(KERN_ERR "-----------------------\n");
2901 printk(KERN_ERR "A. Corruption after allocation\n");
2903 p = kzalloc(16, GFP_KERNEL);
2904 p[16] = 0x12;
2905 printk(KERN_ERR "\n1. kmalloc-16: Clobber Redzone/next pointer"
2906 " 0x12->0x%p\n\n", p + 16);
2908 validate_slab_cache(kmalloc_caches + 4);
2910 /* Hmmm... The next two are dangerous */
2911 p = kzalloc(32, GFP_KERNEL);
2912 p[32 + sizeof(void *)] = 0x34;
2913 printk(KERN_ERR "\n2. kmalloc-32: Clobber next pointer/next slab"
2914 " 0x34 -> -0x%p\n", p);
2915 printk(KERN_ERR "If allocated object is overwritten then not detectable\n\n");
2917 validate_slab_cache(kmalloc_caches + 5);
2918 p = kzalloc(64, GFP_KERNEL);
2919 p += 64 + (get_cycles() & 0xff) * sizeof(void *);
2920 *p = 0x56;
2921 printk(KERN_ERR "\n3. kmalloc-64: corrupting random byte 0x56->0x%p\n",
2923 printk(KERN_ERR "If allocated object is overwritten then not detectable\n\n");
2924 validate_slab_cache(kmalloc_caches + 6);
2926 printk(KERN_ERR "\nB. Corruption after free\n");
2927 p = kzalloc(128, GFP_KERNEL);
2928 kfree(p);
2929 *p = 0x78;
2930 printk(KERN_ERR "1. kmalloc-128: Clobber first word 0x78->0x%p\n\n", p);
2931 validate_slab_cache(kmalloc_caches + 7);
2933 p = kzalloc(256, GFP_KERNEL);
2934 kfree(p);
2935 p[50] = 0x9a;
2936 printk(KERN_ERR "\n2. kmalloc-256: Clobber 50th byte 0x9a->0x%p\n\n", p);
2937 validate_slab_cache(kmalloc_caches + 8);
2939 p = kzalloc(512, GFP_KERNEL);
2940 kfree(p);
2941 p[512] = 0xab;
2942 printk(KERN_ERR "\n3. kmalloc-512: Clobber redzone 0xab->0x%p\n\n", p);
2943 validate_slab_cache(kmalloc_caches + 9);
2945 #else
2946 static void resiliency_test(void) {};
2947 #endif
2950 * Generate lists of code addresses where slabcache objects are allocated
2951 * and freed.
2954 struct location {
2955 unsigned long count;
2956 void *addr;
2957 long long sum_time;
2958 long min_time;
2959 long max_time;
2960 long min_pid;
2961 long max_pid;
2962 cpumask_t cpus;
2963 nodemask_t nodes;
2966 struct loc_track {
2967 unsigned long max;
2968 unsigned long count;
2969 struct location *loc;
2972 static void free_loc_track(struct loc_track *t)
2974 if (t->max)
2975 free_pages((unsigned long)t->loc,
2976 get_order(sizeof(struct location) * t->max));
2979 static int alloc_loc_track(struct loc_track *t, unsigned long max, gfp_t flags)
2981 struct location *l;
2982 int order;
2984 order = get_order(sizeof(struct location) * max);
2986 l = (void *)__get_free_pages(flags, order);
2987 if (!l)
2988 return 0;
2990 if (t->count) {
2991 memcpy(l, t->loc, sizeof(struct location) * t->count);
2992 free_loc_track(t);
2994 t->max = max;
2995 t->loc = l;
2996 return 1;
2999 static int add_location(struct loc_track *t, struct kmem_cache *s,
3000 const struct track *track)
3002 long start, end, pos;
3003 struct location *l;
3004 void *caddr;
3005 unsigned long age = jiffies - track->when;
3007 start = -1;
3008 end = t->count;
3010 for ( ; ; ) {
3011 pos = start + (end - start + 1) / 2;
3014 * There is nothing at "end". If we end up there
3015 * we need to add something to before end.
3017 if (pos == end)
3018 break;
3020 caddr = t->loc[pos].addr;
3021 if (track->addr == caddr) {
3023 l = &t->loc[pos];
3024 l->count++;
3025 if (track->when) {
3026 l->sum_time += age;
3027 if (age < l->min_time)
3028 l->min_time = age;
3029 if (age > l->max_time)
3030 l->max_time = age;
3032 if (track->pid < l->min_pid)
3033 l->min_pid = track->pid;
3034 if (track->pid > l->max_pid)
3035 l->max_pid = track->pid;
3037 cpu_set(track->cpu, l->cpus);
3039 node_set(page_to_nid(virt_to_page(track)), l->nodes);
3040 return 1;
3043 if (track->addr < caddr)
3044 end = pos;
3045 else
3046 start = pos;
3050 * Not found. Insert new tracking element.
3052 if (t->count >= t->max && !alloc_loc_track(t, 2 * t->max, GFP_ATOMIC))
3053 return 0;
3055 l = t->loc + pos;
3056 if (pos < t->count)
3057 memmove(l + 1, l,
3058 (t->count - pos) * sizeof(struct location));
3059 t->count++;
3060 l->count = 1;
3061 l->addr = track->addr;
3062 l->sum_time = age;
3063 l->min_time = age;
3064 l->max_time = age;
3065 l->min_pid = track->pid;
3066 l->max_pid = track->pid;
3067 cpus_clear(l->cpus);
3068 cpu_set(track->cpu, l->cpus);
3069 nodes_clear(l->nodes);
3070 node_set(page_to_nid(virt_to_page(track)), l->nodes);
3071 return 1;
3074 static void process_slab(struct loc_track *t, struct kmem_cache *s,
3075 struct page *page, enum track_item alloc)
3077 void *addr = page_address(page);
3078 DECLARE_BITMAP(map, s->objects);
3079 void *p;
3081 bitmap_zero(map, s->objects);
3082 for_each_free_object(p, s, page->freelist)
3083 set_bit(slab_index(p, s, addr), map);
3085 for_each_object(p, s, addr)
3086 if (!test_bit(slab_index(p, s, addr), map))
3087 add_location(t, s, get_track(s, p, alloc));
3090 static int list_locations(struct kmem_cache *s, char *buf,
3091 enum track_item alloc)
3093 int n = 0;
3094 unsigned long i;
3095 struct loc_track t = { 0, 0, NULL };
3096 int node;
3098 if (!alloc_loc_track(&t, PAGE_SIZE / sizeof(struct location),
3099 GFP_KERNEL))
3100 return sprintf(buf, "Out of memory\n");
3102 /* Push back cpu slabs */
3103 flush_all(s);
3105 for_each_online_node(node) {
3106 struct kmem_cache_node *n = get_node(s, node);
3107 unsigned long flags;
3108 struct page *page;
3110 if (!atomic_long_read(&n->nr_slabs))
3111 continue;
3113 spin_lock_irqsave(&n->list_lock, flags);
3114 list_for_each_entry(page, &n->partial, lru)
3115 process_slab(&t, s, page, alloc);
3116 list_for_each_entry(page, &n->full, lru)
3117 process_slab(&t, s, page, alloc);
3118 spin_unlock_irqrestore(&n->list_lock, flags);
3121 for (i = 0; i < t.count; i++) {
3122 struct location *l = &t.loc[i];
3124 if (n > PAGE_SIZE - 100)
3125 break;
3126 n += sprintf(buf + n, "%7ld ", l->count);
3128 if (l->addr)
3129 n += sprint_symbol(buf + n, (unsigned long)l->addr);
3130 else
3131 n += sprintf(buf + n, "<not-available>");
3133 if (l->sum_time != l->min_time) {
3134 unsigned long remainder;
3136 n += sprintf(buf + n, " age=%ld/%ld/%ld",
3137 l->min_time,
3138 div_long_long_rem(l->sum_time, l->count, &remainder),
3139 l->max_time);
3140 } else
3141 n += sprintf(buf + n, " age=%ld",
3142 l->min_time);
3144 if (l->min_pid != l->max_pid)
3145 n += sprintf(buf + n, " pid=%ld-%ld",
3146 l->min_pid, l->max_pid);
3147 else
3148 n += sprintf(buf + n, " pid=%ld",
3149 l->min_pid);
3151 if (num_online_cpus() > 1 && !cpus_empty(l->cpus) &&
3152 n < PAGE_SIZE - 60) {
3153 n += sprintf(buf + n, " cpus=");
3154 n += cpulist_scnprintf(buf + n, PAGE_SIZE - n - 50,
3155 l->cpus);
3158 if (num_online_nodes() > 1 && !nodes_empty(l->nodes) &&
3159 n < PAGE_SIZE - 60) {
3160 n += sprintf(buf + n, " nodes=");
3161 n += nodelist_scnprintf(buf + n, PAGE_SIZE - n - 50,
3162 l->nodes);
3165 n += sprintf(buf + n, "\n");
3168 free_loc_track(&t);
3169 if (!t.count)
3170 n += sprintf(buf, "No data\n");
3171 return n;
3174 static unsigned long count_partial(struct kmem_cache_node *n)
3176 unsigned long flags;
3177 unsigned long x = 0;
3178 struct page *page;
3180 spin_lock_irqsave(&n->list_lock, flags);
3181 list_for_each_entry(page, &n->partial, lru)
3182 x += page->inuse;
3183 spin_unlock_irqrestore(&n->list_lock, flags);
3184 return x;
3187 enum slab_stat_type {
3188 SL_FULL,
3189 SL_PARTIAL,
3190 SL_CPU,
3191 SL_OBJECTS
3194 #define SO_FULL (1 << SL_FULL)
3195 #define SO_PARTIAL (1 << SL_PARTIAL)
3196 #define SO_CPU (1 << SL_CPU)
3197 #define SO_OBJECTS (1 << SL_OBJECTS)
3199 static unsigned long slab_objects(struct kmem_cache *s,
3200 char *buf, unsigned long flags)
3202 unsigned long total = 0;
3203 int cpu;
3204 int node;
3205 int x;
3206 unsigned long *nodes;
3207 unsigned long *per_cpu;
3209 nodes = kzalloc(2 * sizeof(unsigned long) * nr_node_ids, GFP_KERNEL);
3210 per_cpu = nodes + nr_node_ids;
3212 for_each_possible_cpu(cpu) {
3213 struct page *page = s->cpu_slab[cpu];
3214 int node;
3216 if (page) {
3217 node = page_to_nid(page);
3218 if (flags & SO_CPU) {
3219 int x = 0;
3221 if (flags & SO_OBJECTS)
3222 x = page->inuse;
3223 else
3224 x = 1;
3225 total += x;
3226 nodes[node] += x;
3228 per_cpu[node]++;
3232 for_each_online_node(node) {
3233 struct kmem_cache_node *n = get_node(s, node);
3235 if (flags & SO_PARTIAL) {
3236 if (flags & SO_OBJECTS)
3237 x = count_partial(n);
3238 else
3239 x = n->nr_partial;
3240 total += x;
3241 nodes[node] += x;
3244 if (flags & SO_FULL) {
3245 int full_slabs = atomic_long_read(&n->nr_slabs)
3246 - per_cpu[node]
3247 - n->nr_partial;
3249 if (flags & SO_OBJECTS)
3250 x = full_slabs * s->objects;
3251 else
3252 x = full_slabs;
3253 total += x;
3254 nodes[node] += x;
3258 x = sprintf(buf, "%lu", total);
3259 #ifdef CONFIG_NUMA
3260 for_each_online_node(node)
3261 if (nodes[node])
3262 x += sprintf(buf + x, " N%d=%lu",
3263 node, nodes[node]);
3264 #endif
3265 kfree(nodes);
3266 return x + sprintf(buf + x, "\n");
3269 static int any_slab_objects(struct kmem_cache *s)
3271 int node;
3272 int cpu;
3274 for_each_possible_cpu(cpu)
3275 if (s->cpu_slab[cpu])
3276 return 1;
3278 for_each_node(node) {
3279 struct kmem_cache_node *n = get_node(s, node);
3281 if (n->nr_partial || atomic_long_read(&n->nr_slabs))
3282 return 1;
3284 return 0;
3287 #define to_slab_attr(n) container_of(n, struct slab_attribute, attr)
3288 #define to_slab(n) container_of(n, struct kmem_cache, kobj);
3290 struct slab_attribute {
3291 struct attribute attr;
3292 ssize_t (*show)(struct kmem_cache *s, char *buf);
3293 ssize_t (*store)(struct kmem_cache *s, const char *x, size_t count);
3296 #define SLAB_ATTR_RO(_name) \
3297 static struct slab_attribute _name##_attr = __ATTR_RO(_name)
3299 #define SLAB_ATTR(_name) \
3300 static struct slab_attribute _name##_attr = \
3301 __ATTR(_name, 0644, _name##_show, _name##_store)
3303 static ssize_t slab_size_show(struct kmem_cache *s, char *buf)
3305 return sprintf(buf, "%d\n", s->size);
3307 SLAB_ATTR_RO(slab_size);
3309 static ssize_t align_show(struct kmem_cache *s, char *buf)
3311 return sprintf(buf, "%d\n", s->align);
3313 SLAB_ATTR_RO(align);
3315 static ssize_t object_size_show(struct kmem_cache *s, char *buf)
3317 return sprintf(buf, "%d\n", s->objsize);
3319 SLAB_ATTR_RO(object_size);
3321 static ssize_t objs_per_slab_show(struct kmem_cache *s, char *buf)
3323 return sprintf(buf, "%d\n", s->objects);
3325 SLAB_ATTR_RO(objs_per_slab);
3327 static ssize_t order_show(struct kmem_cache *s, char *buf)
3329 return sprintf(buf, "%d\n", s->order);
3331 SLAB_ATTR_RO(order);
3333 static ssize_t ctor_show(struct kmem_cache *s, char *buf)
3335 if (s->ctor) {
3336 int n = sprint_symbol(buf, (unsigned long)s->ctor);
3338 return n + sprintf(buf + n, "\n");
3340 return 0;
3342 SLAB_ATTR_RO(ctor);
3344 static ssize_t aliases_show(struct kmem_cache *s, char *buf)
3346 return sprintf(buf, "%d\n", s->refcount - 1);
3348 SLAB_ATTR_RO(aliases);
3350 static ssize_t slabs_show(struct kmem_cache *s, char *buf)
3352 return slab_objects(s, buf, SO_FULL|SO_PARTIAL|SO_CPU);
3354 SLAB_ATTR_RO(slabs);
3356 static ssize_t partial_show(struct kmem_cache *s, char *buf)
3358 return slab_objects(s, buf, SO_PARTIAL);
3360 SLAB_ATTR_RO(partial);
3362 static ssize_t cpu_slabs_show(struct kmem_cache *s, char *buf)
3364 return slab_objects(s, buf, SO_CPU);
3366 SLAB_ATTR_RO(cpu_slabs);
3368 static ssize_t objects_show(struct kmem_cache *s, char *buf)
3370 return slab_objects(s, buf, SO_FULL|SO_PARTIAL|SO_CPU|SO_OBJECTS);
3372 SLAB_ATTR_RO(objects);
3374 static ssize_t sanity_checks_show(struct kmem_cache *s, char *buf)
3376 return sprintf(buf, "%d\n", !!(s->flags & SLAB_DEBUG_FREE));
3379 static ssize_t sanity_checks_store(struct kmem_cache *s,
3380 const char *buf, size_t length)
3382 s->flags &= ~SLAB_DEBUG_FREE;
3383 if (buf[0] == '1')
3384 s->flags |= SLAB_DEBUG_FREE;
3385 return length;
3387 SLAB_ATTR(sanity_checks);
3389 static ssize_t trace_show(struct kmem_cache *s, char *buf)
3391 return sprintf(buf, "%d\n", !!(s->flags & SLAB_TRACE));
3394 static ssize_t trace_store(struct kmem_cache *s, const char *buf,
3395 size_t length)
3397 s->flags &= ~SLAB_TRACE;
3398 if (buf[0] == '1')
3399 s->flags |= SLAB_TRACE;
3400 return length;
3402 SLAB_ATTR(trace);
3404 static ssize_t reclaim_account_show(struct kmem_cache *s, char *buf)
3406 return sprintf(buf, "%d\n", !!(s->flags & SLAB_RECLAIM_ACCOUNT));
3409 static ssize_t reclaim_account_store(struct kmem_cache *s,
3410 const char *buf, size_t length)
3412 s->flags &= ~SLAB_RECLAIM_ACCOUNT;
3413 if (buf[0] == '1')
3414 s->flags |= SLAB_RECLAIM_ACCOUNT;
3415 return length;
3417 SLAB_ATTR(reclaim_account);
3419 static ssize_t hwcache_align_show(struct kmem_cache *s, char *buf)
3421 return sprintf(buf, "%d\n", !!(s->flags & SLAB_HWCACHE_ALIGN));
3423 SLAB_ATTR_RO(hwcache_align);
3425 #ifdef CONFIG_ZONE_DMA
3426 static ssize_t cache_dma_show(struct kmem_cache *s, char *buf)
3428 return sprintf(buf, "%d\n", !!(s->flags & SLAB_CACHE_DMA));
3430 SLAB_ATTR_RO(cache_dma);
3431 #endif
3433 static ssize_t destroy_by_rcu_show(struct kmem_cache *s, char *buf)
3435 return sprintf(buf, "%d\n", !!(s->flags & SLAB_DESTROY_BY_RCU));
3437 SLAB_ATTR_RO(destroy_by_rcu);
3439 static ssize_t red_zone_show(struct kmem_cache *s, char *buf)
3441 return sprintf(buf, "%d\n", !!(s->flags & SLAB_RED_ZONE));
3444 static ssize_t red_zone_store(struct kmem_cache *s,
3445 const char *buf, size_t length)
3447 if (any_slab_objects(s))
3448 return -EBUSY;
3450 s->flags &= ~SLAB_RED_ZONE;
3451 if (buf[0] == '1')
3452 s->flags |= SLAB_RED_ZONE;
3453 calculate_sizes(s);
3454 return length;
3456 SLAB_ATTR(red_zone);
3458 static ssize_t poison_show(struct kmem_cache *s, char *buf)
3460 return sprintf(buf, "%d\n", !!(s->flags & SLAB_POISON));
3463 static ssize_t poison_store(struct kmem_cache *s,
3464 const char *buf, size_t length)
3466 if (any_slab_objects(s))
3467 return -EBUSY;
3469 s->flags &= ~SLAB_POISON;
3470 if (buf[0] == '1')
3471 s->flags |= SLAB_POISON;
3472 calculate_sizes(s);
3473 return length;
3475 SLAB_ATTR(poison);
3477 static ssize_t store_user_show(struct kmem_cache *s, char *buf)
3479 return sprintf(buf, "%d\n", !!(s->flags & SLAB_STORE_USER));
3482 static ssize_t store_user_store(struct kmem_cache *s,
3483 const char *buf, size_t length)
3485 if (any_slab_objects(s))
3486 return -EBUSY;
3488 s->flags &= ~SLAB_STORE_USER;
3489 if (buf[0] == '1')
3490 s->flags |= SLAB_STORE_USER;
3491 calculate_sizes(s);
3492 return length;
3494 SLAB_ATTR(store_user);
3496 static ssize_t validate_show(struct kmem_cache *s, char *buf)
3498 return 0;
3501 static ssize_t validate_store(struct kmem_cache *s,
3502 const char *buf, size_t length)
3504 int ret = -EINVAL;
3506 if (buf[0] == '1') {
3507 ret = validate_slab_cache(s);
3508 if (ret >= 0)
3509 ret = length;
3511 return ret;
3513 SLAB_ATTR(validate);
3515 static ssize_t shrink_show(struct kmem_cache *s, char *buf)
3517 return 0;
3520 static ssize_t shrink_store(struct kmem_cache *s,
3521 const char *buf, size_t length)
3523 if (buf[0] == '1') {
3524 int rc = kmem_cache_shrink(s);
3526 if (rc)
3527 return rc;
3528 } else
3529 return -EINVAL;
3530 return length;
3532 SLAB_ATTR(shrink);
3534 static ssize_t alloc_calls_show(struct kmem_cache *s, char *buf)
3536 if (!(s->flags & SLAB_STORE_USER))
3537 return -ENOSYS;
3538 return list_locations(s, buf, TRACK_ALLOC);
3540 SLAB_ATTR_RO(alloc_calls);
3542 static ssize_t free_calls_show(struct kmem_cache *s, char *buf)
3544 if (!(s->flags & SLAB_STORE_USER))
3545 return -ENOSYS;
3546 return list_locations(s, buf, TRACK_FREE);
3548 SLAB_ATTR_RO(free_calls);
3550 #ifdef CONFIG_NUMA
3551 static ssize_t defrag_ratio_show(struct kmem_cache *s, char *buf)
3553 return sprintf(buf, "%d\n", s->defrag_ratio / 10);
3556 static ssize_t defrag_ratio_store(struct kmem_cache *s,
3557 const char *buf, size_t length)
3559 int n = simple_strtoul(buf, NULL, 10);
3561 if (n < 100)
3562 s->defrag_ratio = n * 10;
3563 return length;
3565 SLAB_ATTR(defrag_ratio);
3566 #endif
3568 static struct attribute * slab_attrs[] = {
3569 &slab_size_attr.attr,
3570 &object_size_attr.attr,
3571 &objs_per_slab_attr.attr,
3572 &order_attr.attr,
3573 &objects_attr.attr,
3574 &slabs_attr.attr,
3575 &partial_attr.attr,
3576 &cpu_slabs_attr.attr,
3577 &ctor_attr.attr,
3578 &aliases_attr.attr,
3579 &align_attr.attr,
3580 &sanity_checks_attr.attr,
3581 &trace_attr.attr,
3582 &hwcache_align_attr.attr,
3583 &reclaim_account_attr.attr,
3584 &destroy_by_rcu_attr.attr,
3585 &red_zone_attr.attr,
3586 &poison_attr.attr,
3587 &store_user_attr.attr,
3588 &validate_attr.attr,
3589 &shrink_attr.attr,
3590 &alloc_calls_attr.attr,
3591 &free_calls_attr.attr,
3592 #ifdef CONFIG_ZONE_DMA
3593 &cache_dma_attr.attr,
3594 #endif
3595 #ifdef CONFIG_NUMA
3596 &defrag_ratio_attr.attr,
3597 #endif
3598 NULL
3601 static struct attribute_group slab_attr_group = {
3602 .attrs = slab_attrs,
3605 static ssize_t slab_attr_show(struct kobject *kobj,
3606 struct attribute *attr,
3607 char *buf)
3609 struct slab_attribute *attribute;
3610 struct kmem_cache *s;
3611 int err;
3613 attribute = to_slab_attr(attr);
3614 s = to_slab(kobj);
3616 if (!attribute->show)
3617 return -EIO;
3619 err = attribute->show(s, buf);
3621 return err;
3624 static ssize_t slab_attr_store(struct kobject *kobj,
3625 struct attribute *attr,
3626 const char *buf, size_t len)
3628 struct slab_attribute *attribute;
3629 struct kmem_cache *s;
3630 int err;
3632 attribute = to_slab_attr(attr);
3633 s = to_slab(kobj);
3635 if (!attribute->store)
3636 return -EIO;
3638 err = attribute->store(s, buf, len);
3640 return err;
3643 static struct sysfs_ops slab_sysfs_ops = {
3644 .show = slab_attr_show,
3645 .store = slab_attr_store,
3648 static struct kobj_type slab_ktype = {
3649 .sysfs_ops = &slab_sysfs_ops,
3652 static int uevent_filter(struct kset *kset, struct kobject *kobj)
3654 struct kobj_type *ktype = get_ktype(kobj);
3656 if (ktype == &slab_ktype)
3657 return 1;
3658 return 0;
3661 static struct kset_uevent_ops slab_uevent_ops = {
3662 .filter = uevent_filter,
3665 static decl_subsys(slab, &slab_ktype, &slab_uevent_ops);
3667 #define ID_STR_LENGTH 64
3669 /* Create a unique string id for a slab cache:
3670 * format
3671 * :[flags-]size:[memory address of kmemcache]
3673 static char *create_unique_id(struct kmem_cache *s)
3675 char *name = kmalloc(ID_STR_LENGTH, GFP_KERNEL);
3676 char *p = name;
3678 BUG_ON(!name);
3680 *p++ = ':';
3682 * First flags affecting slabcache operations. We will only
3683 * get here for aliasable slabs so we do not need to support
3684 * too many flags. The flags here must cover all flags that
3685 * are matched during merging to guarantee that the id is
3686 * unique.
3688 if (s->flags & SLAB_CACHE_DMA)
3689 *p++ = 'd';
3690 if (s->flags & SLAB_RECLAIM_ACCOUNT)
3691 *p++ = 'a';
3692 if (s->flags & SLAB_DEBUG_FREE)
3693 *p++ = 'F';
3694 if (p != name + 1)
3695 *p++ = '-';
3696 p += sprintf(p, "%07d", s->size);
3697 BUG_ON(p > name + ID_STR_LENGTH - 1);
3698 return name;
3701 static int sysfs_slab_add(struct kmem_cache *s)
3703 int err;
3704 const char *name;
3705 int unmergeable;
3707 if (slab_state < SYSFS)
3708 /* Defer until later */
3709 return 0;
3711 unmergeable = slab_unmergeable(s);
3712 if (unmergeable) {
3714 * Slabcache can never be merged so we can use the name proper.
3715 * This is typically the case for debug situations. In that
3716 * case we can catch duplicate names easily.
3718 sysfs_remove_link(&slab_subsys.kobj, s->name);
3719 name = s->name;
3720 } else {
3722 * Create a unique name for the slab as a target
3723 * for the symlinks.
3725 name = create_unique_id(s);
3728 kobj_set_kset_s(s, slab_subsys);
3729 kobject_set_name(&s->kobj, name);
3730 kobject_init(&s->kobj);
3731 err = kobject_add(&s->kobj);
3732 if (err)
3733 return err;
3735 err = sysfs_create_group(&s->kobj, &slab_attr_group);
3736 if (err)
3737 return err;
3738 kobject_uevent(&s->kobj, KOBJ_ADD);
3739 if (!unmergeable) {
3740 /* Setup first alias */
3741 sysfs_slab_alias(s, s->name);
3742 kfree(name);
3744 return 0;
3747 static void sysfs_slab_remove(struct kmem_cache *s)
3749 kobject_uevent(&s->kobj, KOBJ_REMOVE);
3750 kobject_del(&s->kobj);
3754 * Need to buffer aliases during bootup until sysfs becomes
3755 * available lest we loose that information.
3757 struct saved_alias {
3758 struct kmem_cache *s;
3759 const char *name;
3760 struct saved_alias *next;
3763 static struct saved_alias *alias_list;
3765 static int sysfs_slab_alias(struct kmem_cache *s, const char *name)
3767 struct saved_alias *al;
3769 if (slab_state == SYSFS) {
3771 * If we have a leftover link then remove it.
3773 sysfs_remove_link(&slab_subsys.kobj, name);
3774 return sysfs_create_link(&slab_subsys.kobj,
3775 &s->kobj, name);
3778 al = kmalloc(sizeof(struct saved_alias), GFP_KERNEL);
3779 if (!al)
3780 return -ENOMEM;
3782 al->s = s;
3783 al->name = name;
3784 al->next = alias_list;
3785 alias_list = al;
3786 return 0;
3789 static int __init slab_sysfs_init(void)
3791 struct kmem_cache *s;
3792 int err;
3794 err = subsystem_register(&slab_subsys);
3795 if (err) {
3796 printk(KERN_ERR "Cannot register slab subsystem.\n");
3797 return -ENOSYS;
3800 slab_state = SYSFS;
3802 list_for_each_entry(s, &slab_caches, list) {
3803 err = sysfs_slab_add(s);
3804 if (err)
3805 printk(KERN_ERR "SLUB: Unable to add boot slab %s"
3806 " to sysfs\n", s->name);
3809 while (alias_list) {
3810 struct saved_alias *al = alias_list;
3812 alias_list = alias_list->next;
3813 err = sysfs_slab_alias(al->s, al->name);
3814 if (err)
3815 printk(KERN_ERR "SLUB: Unable to add boot slab alias"
3816 " %s to sysfs\n", s->name);
3817 kfree(al);
3820 resiliency_test();
3821 return 0;
3824 __initcall(slab_sysfs_init);
3825 #endif