x86: add proper header for reboot_force
[linux-2.6/zen-sources.git] / mm / slub.c
blob70db2897c1ea626ea3b6f576fda91fa6c3c48cd9
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/debugobjects.h>
23 #include <linux/kallsyms.h>
24 #include <linux/memory.h>
27 * Lock order:
28 * 1. slab_lock(page)
29 * 2. slab->list_lock
31 * The slab_lock protects operations on the object of a particular
32 * slab and its metadata in the page struct. If the slab lock
33 * has been taken then no allocations nor frees can be performed
34 * on the objects in the slab nor can the slab be added or removed
35 * from the partial or full lists since this would mean modifying
36 * the page_struct of the slab.
38 * The list_lock protects the partial and full list on each node and
39 * the partial slab counter. If taken then no new slabs may be added or
40 * removed from the lists nor make the number of partial slabs be modified.
41 * (Note that the total number of slabs is an atomic value that may be
42 * modified without taking the list lock).
44 * The list_lock is a centralized lock and thus we avoid taking it as
45 * much as possible. As long as SLUB does not have to handle partial
46 * slabs, operations can continue without any centralized lock. F.e.
47 * allocating a long series of objects that fill up slabs does not require
48 * the list lock.
50 * The lock order is sometimes inverted when we are trying to get a slab
51 * off a list. We take the list_lock and then look for a page on the list
52 * to use. While we do that objects in the slabs may be freed. We can
53 * only operate on the slab if we have also taken the slab_lock. So we use
54 * a slab_trylock() on the slab. If trylock was successful then no frees
55 * can occur anymore and we can use the slab for allocations etc. If the
56 * slab_trylock() does not succeed then frees are in progress in the slab and
57 * we must stay away from it for a while since we may cause a bouncing
58 * cacheline if we try to acquire the lock. So go onto the next slab.
59 * If all pages are busy then we may allocate a new slab instead of reusing
60 * a partial slab. A new slab has noone operating on it and thus there is
61 * no danger of cacheline contention.
63 * Interrupts are disabled during allocation and deallocation in order to
64 * make the slab allocator safe to use in the context of an irq. In addition
65 * interrupts are disabled to ensure that the processor does not change
66 * while handling per_cpu slabs, due to kernel preemption.
68 * SLUB assigns one slab for allocation to each processor.
69 * Allocations only occur from these slabs called cpu slabs.
71 * Slabs with free elements are kept on a partial list and during regular
72 * operations no list for full slabs is used. If an object in a full slab is
73 * freed then the slab will show up again on the partial lists.
74 * We track full slabs for debugging purposes though because otherwise we
75 * cannot scan all objects.
77 * Slabs are freed when they become empty. Teardown and setup is
78 * minimal so we rely on the page allocators per cpu caches for
79 * fast frees and allocs.
81 * Overloading of page flags that are otherwise used for LRU management.
83 * PageActive The slab is frozen and exempt from list processing.
84 * This means that the slab is dedicated to a purpose
85 * such as satisfying allocations for a specific
86 * processor. Objects may be freed in the slab while
87 * it is frozen but slab_free will then skip the usual
88 * list operations. It is up to the processor holding
89 * the slab to integrate the slab into the slab lists
90 * when the slab is no longer needed.
92 * One use of this flag is to mark slabs that are
93 * used for allocations. Then such a slab becomes a cpu
94 * slab. The cpu slab may be equipped with an additional
95 * freelist that allows lockless access to
96 * free objects in addition to the regular freelist
97 * that requires the slab lock.
99 * PageError Slab requires special handling due to debug
100 * options set. This moves slab handling out of
101 * the fast path and disables lockless freelists.
104 #define FROZEN (1 << PG_active)
106 #ifdef CONFIG_SLUB_DEBUG
107 #define SLABDEBUG (1 << PG_error)
108 #else
109 #define SLABDEBUG 0
110 #endif
112 static inline int SlabFrozen(struct page *page)
114 return page->flags & FROZEN;
117 static inline void SetSlabFrozen(struct page *page)
119 page->flags |= FROZEN;
122 static inline void ClearSlabFrozen(struct page *page)
124 page->flags &= ~FROZEN;
127 static inline int SlabDebug(struct page *page)
129 return page->flags & SLABDEBUG;
132 static inline void SetSlabDebug(struct page *page)
134 page->flags |= SLABDEBUG;
137 static inline void ClearSlabDebug(struct page *page)
139 page->flags &= ~SLABDEBUG;
143 * Issues still to be resolved:
145 * - Support PAGE_ALLOC_DEBUG. Should be easy to do.
147 * - Variable sizing of the per node arrays
150 /* Enable to test recovery from slab corruption on boot */
151 #undef SLUB_RESILIENCY_TEST
154 * Mininum number of partial slabs. These will be left on the partial
155 * lists even if they are empty. kmem_cache_shrink may reclaim them.
157 #define MIN_PARTIAL 5
160 * Maximum number of desirable partial slabs.
161 * The existence of more partial slabs makes kmem_cache_shrink
162 * sort the partial list by the number of objects in the.
164 #define MAX_PARTIAL 10
166 #define DEBUG_DEFAULT_FLAGS (SLAB_DEBUG_FREE | SLAB_RED_ZONE | \
167 SLAB_POISON | SLAB_STORE_USER)
170 * Set of flags that will prevent slab merging
172 #define SLUB_NEVER_MERGE (SLAB_RED_ZONE | SLAB_POISON | SLAB_STORE_USER | \
173 SLAB_TRACE | SLAB_DESTROY_BY_RCU)
175 #define SLUB_MERGE_SAME (SLAB_DEBUG_FREE | SLAB_RECLAIM_ACCOUNT | \
176 SLAB_CACHE_DMA)
178 #ifndef ARCH_KMALLOC_MINALIGN
179 #define ARCH_KMALLOC_MINALIGN __alignof__(unsigned long long)
180 #endif
182 #ifndef ARCH_SLAB_MINALIGN
183 #define ARCH_SLAB_MINALIGN __alignof__(unsigned long long)
184 #endif
186 /* Internal SLUB flags */
187 #define __OBJECT_POISON 0x80000000 /* Poison object */
188 #define __SYSFS_ADD_DEFERRED 0x40000000 /* Not yet visible via sysfs */
190 static int kmem_size = sizeof(struct kmem_cache);
192 #ifdef CONFIG_SMP
193 static struct notifier_block slab_notifier;
194 #endif
196 static enum {
197 DOWN, /* No slab functionality available */
198 PARTIAL, /* kmem_cache_open() works but kmalloc does not */
199 UP, /* Everything works but does not show up in sysfs */
200 SYSFS /* Sysfs up */
201 } slab_state = DOWN;
203 /* A list of all slab caches on the system */
204 static DECLARE_RWSEM(slub_lock);
205 static LIST_HEAD(slab_caches);
208 * Tracking user of a slab.
210 struct track {
211 void *addr; /* Called from address */
212 int cpu; /* Was running on cpu */
213 int pid; /* Pid context */
214 unsigned long when; /* When did the operation occur */
217 enum track_item { TRACK_ALLOC, TRACK_FREE };
219 #if defined(CONFIG_SYSFS) && defined(CONFIG_SLUB_DEBUG)
220 static int sysfs_slab_add(struct kmem_cache *);
221 static int sysfs_slab_alias(struct kmem_cache *, const char *);
222 static void sysfs_slab_remove(struct kmem_cache *);
224 #else
225 static inline int sysfs_slab_add(struct kmem_cache *s) { return 0; }
226 static inline int sysfs_slab_alias(struct kmem_cache *s, const char *p)
227 { return 0; }
228 static inline void sysfs_slab_remove(struct kmem_cache *s)
230 kfree(s);
233 #endif
235 static inline void stat(struct kmem_cache_cpu *c, enum stat_item si)
237 #ifdef CONFIG_SLUB_STATS
238 c->stat[si]++;
239 #endif
242 /********************************************************************
243 * Core slab cache functions
244 *******************************************************************/
246 int slab_is_available(void)
248 return slab_state >= UP;
251 static inline struct kmem_cache_node *get_node(struct kmem_cache *s, int node)
253 #ifdef CONFIG_NUMA
254 return s->node[node];
255 #else
256 return &s->local_node;
257 #endif
260 static inline struct kmem_cache_cpu *get_cpu_slab(struct kmem_cache *s, int cpu)
262 #ifdef CONFIG_SMP
263 return s->cpu_slab[cpu];
264 #else
265 return &s->cpu_slab;
266 #endif
269 /* Verify that a pointer has an address that is valid within a slab page */
270 static inline int check_valid_pointer(struct kmem_cache *s,
271 struct page *page, const void *object)
273 void *base;
275 if (!object)
276 return 1;
278 base = page_address(page);
279 if (object < base || object >= base + page->objects * s->size ||
280 (object - base) % s->size) {
281 return 0;
284 return 1;
288 * Slow version of get and set free pointer.
290 * This version requires touching the cache lines of kmem_cache which
291 * we avoid to do in the fast alloc free paths. There we obtain the offset
292 * from the page struct.
294 static inline void *get_freepointer(struct kmem_cache *s, void *object)
296 return *(void **)(object + s->offset);
299 static inline void set_freepointer(struct kmem_cache *s, void *object, void *fp)
301 *(void **)(object + s->offset) = fp;
304 /* Loop over all objects in a slab */
305 #define for_each_object(__p, __s, __addr, __objects) \
306 for (__p = (__addr); __p < (__addr) + (__objects) * (__s)->size;\
307 __p += (__s)->size)
309 /* Scan freelist */
310 #define for_each_free_object(__p, __s, __free) \
311 for (__p = (__free); __p; __p = get_freepointer((__s), __p))
313 /* Determine object index from a given position */
314 static inline int slab_index(void *p, struct kmem_cache *s, void *addr)
316 return (p - addr) / s->size;
319 static inline struct kmem_cache_order_objects oo_make(int order,
320 unsigned long size)
322 struct kmem_cache_order_objects x = {
323 (order << 16) + (PAGE_SIZE << order) / size
326 return x;
329 static inline int oo_order(struct kmem_cache_order_objects x)
331 return x.x >> 16;
334 static inline int oo_objects(struct kmem_cache_order_objects x)
336 return x.x & ((1 << 16) - 1);
339 #ifdef CONFIG_SLUB_DEBUG
341 * Debug settings:
343 #ifdef CONFIG_SLUB_DEBUG_ON
344 static int slub_debug = DEBUG_DEFAULT_FLAGS;
345 #else
346 static int slub_debug;
347 #endif
349 static char *slub_debug_slabs;
352 * Object debugging
354 static void print_section(char *text, u8 *addr, unsigned int length)
356 int i, offset;
357 int newline = 1;
358 char ascii[17];
360 ascii[16] = 0;
362 for (i = 0; i < length; i++) {
363 if (newline) {
364 printk(KERN_ERR "%8s 0x%p: ", text, addr + i);
365 newline = 0;
367 printk(KERN_CONT " %02x", addr[i]);
368 offset = i % 16;
369 ascii[offset] = isgraph(addr[i]) ? addr[i] : '.';
370 if (offset == 15) {
371 printk(KERN_CONT " %s\n", ascii);
372 newline = 1;
375 if (!newline) {
376 i %= 16;
377 while (i < 16) {
378 printk(KERN_CONT " ");
379 ascii[i] = ' ';
380 i++;
382 printk(KERN_CONT " %s\n", ascii);
386 static struct track *get_track(struct kmem_cache *s, void *object,
387 enum track_item alloc)
389 struct track *p;
391 if (s->offset)
392 p = object + s->offset + sizeof(void *);
393 else
394 p = object + s->inuse;
396 return p + alloc;
399 static void set_track(struct kmem_cache *s, void *object,
400 enum track_item alloc, void *addr)
402 struct track *p;
404 if (s->offset)
405 p = object + s->offset + sizeof(void *);
406 else
407 p = object + s->inuse;
409 p += alloc;
410 if (addr) {
411 p->addr = addr;
412 p->cpu = smp_processor_id();
413 p->pid = current ? current->pid : -1;
414 p->when = jiffies;
415 } else
416 memset(p, 0, sizeof(struct track));
419 static void init_tracking(struct kmem_cache *s, void *object)
421 if (!(s->flags & SLAB_STORE_USER))
422 return;
424 set_track(s, object, TRACK_FREE, NULL);
425 set_track(s, object, TRACK_ALLOC, NULL);
428 static void print_track(const char *s, struct track *t)
430 if (!t->addr)
431 return;
433 printk(KERN_ERR "INFO: %s in ", s);
434 __print_symbol("%s", (unsigned long)t->addr);
435 printk(" age=%lu cpu=%u pid=%d\n", jiffies - t->when, t->cpu, t->pid);
438 static void print_tracking(struct kmem_cache *s, void *object)
440 if (!(s->flags & SLAB_STORE_USER))
441 return;
443 print_track("Allocated", get_track(s, object, TRACK_ALLOC));
444 print_track("Freed", get_track(s, object, TRACK_FREE));
447 static void print_page_info(struct page *page)
449 printk(KERN_ERR "INFO: Slab 0x%p objects=%u used=%u fp=0x%p flags=0x%04lx\n",
450 page, page->objects, page->inuse, page->freelist, page->flags);
454 static void slab_bug(struct kmem_cache *s, char *fmt, ...)
456 va_list args;
457 char buf[100];
459 va_start(args, fmt);
460 vsnprintf(buf, sizeof(buf), fmt, args);
461 va_end(args);
462 printk(KERN_ERR "========================================"
463 "=====================================\n");
464 printk(KERN_ERR "BUG %s: %s\n", s->name, buf);
465 printk(KERN_ERR "----------------------------------------"
466 "-------------------------------------\n\n");
469 static void slab_fix(struct kmem_cache *s, char *fmt, ...)
471 va_list args;
472 char buf[100];
474 va_start(args, fmt);
475 vsnprintf(buf, sizeof(buf), fmt, args);
476 va_end(args);
477 printk(KERN_ERR "FIX %s: %s\n", s->name, buf);
480 static void print_trailer(struct kmem_cache *s, struct page *page, u8 *p)
482 unsigned int off; /* Offset of last byte */
483 u8 *addr = page_address(page);
485 print_tracking(s, p);
487 print_page_info(page);
489 printk(KERN_ERR "INFO: Object 0x%p @offset=%tu fp=0x%p\n\n",
490 p, p - addr, get_freepointer(s, p));
492 if (p > addr + 16)
493 print_section("Bytes b4", p - 16, 16);
495 print_section("Object", p, min(s->objsize, 128));
497 if (s->flags & SLAB_RED_ZONE)
498 print_section("Redzone", p + s->objsize,
499 s->inuse - s->objsize);
501 if (s->offset)
502 off = s->offset + sizeof(void *);
503 else
504 off = s->inuse;
506 if (s->flags & SLAB_STORE_USER)
507 off += 2 * sizeof(struct track);
509 if (off != s->size)
510 /* Beginning of the filler is the free pointer */
511 print_section("Padding", p + off, s->size - off);
513 dump_stack();
516 static void object_err(struct kmem_cache *s, struct page *page,
517 u8 *object, char *reason)
519 slab_bug(s, "%s", reason);
520 print_trailer(s, page, object);
523 static void slab_err(struct kmem_cache *s, struct page *page, char *fmt, ...)
525 va_list args;
526 char buf[100];
528 va_start(args, fmt);
529 vsnprintf(buf, sizeof(buf), fmt, args);
530 va_end(args);
531 slab_bug(s, "%s", buf);
532 print_page_info(page);
533 dump_stack();
536 static void init_object(struct kmem_cache *s, void *object, int active)
538 u8 *p = object;
540 if (s->flags & __OBJECT_POISON) {
541 memset(p, POISON_FREE, s->objsize - 1);
542 p[s->objsize - 1] = POISON_END;
545 if (s->flags & SLAB_RED_ZONE)
546 memset(p + s->objsize,
547 active ? SLUB_RED_ACTIVE : SLUB_RED_INACTIVE,
548 s->inuse - s->objsize);
551 static u8 *check_bytes(u8 *start, unsigned int value, unsigned int bytes)
553 while (bytes) {
554 if (*start != (u8)value)
555 return start;
556 start++;
557 bytes--;
559 return NULL;
562 static void restore_bytes(struct kmem_cache *s, char *message, u8 data,
563 void *from, void *to)
565 slab_fix(s, "Restoring 0x%p-0x%p=0x%x\n", from, to - 1, data);
566 memset(from, data, to - from);
569 static int check_bytes_and_report(struct kmem_cache *s, struct page *page,
570 u8 *object, char *what,
571 u8 *start, unsigned int value, unsigned int bytes)
573 u8 *fault;
574 u8 *end;
576 fault = check_bytes(start, value, bytes);
577 if (!fault)
578 return 1;
580 end = start + bytes;
581 while (end > fault && end[-1] == value)
582 end--;
584 slab_bug(s, "%s overwritten", what);
585 printk(KERN_ERR "INFO: 0x%p-0x%p. First byte 0x%x instead of 0x%x\n",
586 fault, end - 1, fault[0], value);
587 print_trailer(s, page, object);
589 restore_bytes(s, what, value, fault, end);
590 return 0;
594 * Object layout:
596 * object address
597 * Bytes of the object to be managed.
598 * If the freepointer may overlay the object then the free
599 * pointer is the first word of the object.
601 * Poisoning uses 0x6b (POISON_FREE) and the last byte is
602 * 0xa5 (POISON_END)
604 * object + s->objsize
605 * Padding to reach word boundary. This is also used for Redzoning.
606 * Padding is extended by another word if Redzoning is enabled and
607 * objsize == inuse.
609 * We fill with 0xbb (RED_INACTIVE) for inactive objects and with
610 * 0xcc (RED_ACTIVE) for objects in use.
612 * object + s->inuse
613 * Meta data starts here.
615 * A. Free pointer (if we cannot overwrite object on free)
616 * B. Tracking data for SLAB_STORE_USER
617 * C. Padding to reach required alignment boundary or at mininum
618 * one word if debugging is on to be able to detect writes
619 * before the word boundary.
621 * Padding is done using 0x5a (POISON_INUSE)
623 * object + s->size
624 * Nothing is used beyond s->size.
626 * If slabcaches are merged then the objsize and inuse boundaries are mostly
627 * ignored. And therefore no slab options that rely on these boundaries
628 * may be used with merged slabcaches.
631 static int check_pad_bytes(struct kmem_cache *s, struct page *page, u8 *p)
633 unsigned long off = s->inuse; /* The end of info */
635 if (s->offset)
636 /* Freepointer is placed after the object. */
637 off += sizeof(void *);
639 if (s->flags & SLAB_STORE_USER)
640 /* We also have user information there */
641 off += 2 * sizeof(struct track);
643 if (s->size == off)
644 return 1;
646 return check_bytes_and_report(s, page, p, "Object padding",
647 p + off, POISON_INUSE, s->size - off);
650 /* Check the pad bytes at the end of a slab page */
651 static int slab_pad_check(struct kmem_cache *s, struct page *page)
653 u8 *start;
654 u8 *fault;
655 u8 *end;
656 int length;
657 int remainder;
659 if (!(s->flags & SLAB_POISON))
660 return 1;
662 start = page_address(page);
663 length = (PAGE_SIZE << compound_order(page));
664 end = start + length;
665 remainder = length % s->size;
666 if (!remainder)
667 return 1;
669 fault = check_bytes(end - remainder, POISON_INUSE, remainder);
670 if (!fault)
671 return 1;
672 while (end > fault && end[-1] == POISON_INUSE)
673 end--;
675 slab_err(s, page, "Padding overwritten. 0x%p-0x%p", fault, end - 1);
676 print_section("Padding", end - remainder, remainder);
678 restore_bytes(s, "slab padding", POISON_INUSE, start, end);
679 return 0;
682 static int check_object(struct kmem_cache *s, struct page *page,
683 void *object, int active)
685 u8 *p = object;
686 u8 *endobject = object + s->objsize;
688 if (s->flags & SLAB_RED_ZONE) {
689 unsigned int red =
690 active ? SLUB_RED_ACTIVE : SLUB_RED_INACTIVE;
692 if (!check_bytes_and_report(s, page, object, "Redzone",
693 endobject, red, s->inuse - s->objsize))
694 return 0;
695 } else {
696 if ((s->flags & SLAB_POISON) && s->objsize < s->inuse) {
697 check_bytes_and_report(s, page, p, "Alignment padding",
698 endobject, POISON_INUSE, s->inuse - s->objsize);
702 if (s->flags & SLAB_POISON) {
703 if (!active && (s->flags & __OBJECT_POISON) &&
704 (!check_bytes_and_report(s, page, p, "Poison", p,
705 POISON_FREE, s->objsize - 1) ||
706 !check_bytes_and_report(s, page, p, "Poison",
707 p + s->objsize - 1, POISON_END, 1)))
708 return 0;
710 * check_pad_bytes cleans up on its own.
712 check_pad_bytes(s, page, p);
715 if (!s->offset && active)
717 * Object and freepointer overlap. Cannot check
718 * freepointer while object is allocated.
720 return 1;
722 /* Check free pointer validity */
723 if (!check_valid_pointer(s, page, get_freepointer(s, p))) {
724 object_err(s, page, p, "Freepointer corrupt");
726 * No choice but to zap it and thus loose the remainder
727 * of the free objects in this slab. May cause
728 * another error because the object count is now wrong.
730 set_freepointer(s, p, NULL);
731 return 0;
733 return 1;
736 static int check_slab(struct kmem_cache *s, struct page *page)
738 int maxobj;
740 VM_BUG_ON(!irqs_disabled());
742 if (!PageSlab(page)) {
743 slab_err(s, page, "Not a valid slab page");
744 return 0;
747 maxobj = (PAGE_SIZE << compound_order(page)) / s->size;
748 if (page->objects > maxobj) {
749 slab_err(s, page, "objects %u > max %u",
750 s->name, page->objects, maxobj);
751 return 0;
753 if (page->inuse > page->objects) {
754 slab_err(s, page, "inuse %u > max %u",
755 s->name, page->inuse, page->objects);
756 return 0;
758 /* Slab_pad_check fixes things up after itself */
759 slab_pad_check(s, page);
760 return 1;
764 * Determine if a certain object on a page is on the freelist. Must hold the
765 * slab lock to guarantee that the chains are in a consistent state.
767 static int on_freelist(struct kmem_cache *s, struct page *page, void *search)
769 int nr = 0;
770 void *fp = page->freelist;
771 void *object = NULL;
772 unsigned long max_objects;
774 while (fp && nr <= page->objects) {
775 if (fp == search)
776 return 1;
777 if (!check_valid_pointer(s, page, fp)) {
778 if (object) {
779 object_err(s, page, object,
780 "Freechain corrupt");
781 set_freepointer(s, object, NULL);
782 break;
783 } else {
784 slab_err(s, page, "Freepointer corrupt");
785 page->freelist = NULL;
786 page->inuse = page->objects;
787 slab_fix(s, "Freelist cleared");
788 return 0;
790 break;
792 object = fp;
793 fp = get_freepointer(s, object);
794 nr++;
797 max_objects = (PAGE_SIZE << compound_order(page)) / s->size;
798 if (max_objects > 65535)
799 max_objects = 65535;
801 if (page->objects != max_objects) {
802 slab_err(s, page, "Wrong number of objects. Found %d but "
803 "should be %d", page->objects, max_objects);
804 page->objects = max_objects;
805 slab_fix(s, "Number of objects adjusted.");
807 if (page->inuse != page->objects - nr) {
808 slab_err(s, page, "Wrong object count. Counter is %d but "
809 "counted were %d", page->inuse, page->objects - nr);
810 page->inuse = page->objects - nr;
811 slab_fix(s, "Object count adjusted.");
813 return search == NULL;
816 static void trace(struct kmem_cache *s, struct page *page, void *object, int alloc)
818 if (s->flags & SLAB_TRACE) {
819 printk(KERN_INFO "TRACE %s %s 0x%p inuse=%d fp=0x%p\n",
820 s->name,
821 alloc ? "alloc" : "free",
822 object, page->inuse,
823 page->freelist);
825 if (!alloc)
826 print_section("Object", (void *)object, s->objsize);
828 dump_stack();
833 * Tracking of fully allocated slabs for debugging purposes.
835 static void add_full(struct kmem_cache_node *n, struct page *page)
837 spin_lock(&n->list_lock);
838 list_add(&page->lru, &n->full);
839 spin_unlock(&n->list_lock);
842 static void remove_full(struct kmem_cache *s, struct page *page)
844 struct kmem_cache_node *n;
846 if (!(s->flags & SLAB_STORE_USER))
847 return;
849 n = get_node(s, page_to_nid(page));
851 spin_lock(&n->list_lock);
852 list_del(&page->lru);
853 spin_unlock(&n->list_lock);
856 /* Tracking of the number of slabs for debugging purposes */
857 static inline unsigned long slabs_node(struct kmem_cache *s, int node)
859 struct kmem_cache_node *n = get_node(s, node);
861 return atomic_long_read(&n->nr_slabs);
864 static inline void inc_slabs_node(struct kmem_cache *s, int node, int objects)
866 struct kmem_cache_node *n = get_node(s, node);
869 * May be called early in order to allocate a slab for the
870 * kmem_cache_node structure. Solve the chicken-egg
871 * dilemma by deferring the increment of the count during
872 * bootstrap (see early_kmem_cache_node_alloc).
874 if (!NUMA_BUILD || n) {
875 atomic_long_inc(&n->nr_slabs);
876 atomic_long_add(objects, &n->total_objects);
879 static inline void dec_slabs_node(struct kmem_cache *s, int node, int objects)
881 struct kmem_cache_node *n = get_node(s, node);
883 atomic_long_dec(&n->nr_slabs);
884 atomic_long_sub(objects, &n->total_objects);
887 /* Object debug checks for alloc/free paths */
888 static void setup_object_debug(struct kmem_cache *s, struct page *page,
889 void *object)
891 if (!(s->flags & (SLAB_STORE_USER|SLAB_RED_ZONE|__OBJECT_POISON)))
892 return;
894 init_object(s, object, 0);
895 init_tracking(s, object);
898 static int alloc_debug_processing(struct kmem_cache *s, struct page *page,
899 void *object, void *addr)
901 if (!check_slab(s, page))
902 goto bad;
904 if (!on_freelist(s, page, object)) {
905 object_err(s, page, object, "Object already allocated");
906 goto bad;
909 if (!check_valid_pointer(s, page, object)) {
910 object_err(s, page, object, "Freelist Pointer check fails");
911 goto bad;
914 if (!check_object(s, page, object, 0))
915 goto bad;
917 /* Success perform special debug activities for allocs */
918 if (s->flags & SLAB_STORE_USER)
919 set_track(s, object, TRACK_ALLOC, addr);
920 trace(s, page, object, 1);
921 init_object(s, object, 1);
922 return 1;
924 bad:
925 if (PageSlab(page)) {
927 * If this is a slab page then lets do the best we can
928 * to avoid issues in the future. Marking all objects
929 * as used avoids touching the remaining objects.
931 slab_fix(s, "Marking all objects used");
932 page->inuse = page->objects;
933 page->freelist = NULL;
935 return 0;
938 static int free_debug_processing(struct kmem_cache *s, struct page *page,
939 void *object, void *addr)
941 if (!check_slab(s, page))
942 goto fail;
944 if (!check_valid_pointer(s, page, object)) {
945 slab_err(s, page, "Invalid object pointer 0x%p", object);
946 goto fail;
949 if (on_freelist(s, page, object)) {
950 object_err(s, page, object, "Object already free");
951 goto fail;
954 if (!check_object(s, page, object, 1))
955 return 0;
957 if (unlikely(s != page->slab)) {
958 if (!PageSlab(page)) {
959 slab_err(s, page, "Attempt to free object(0x%p) "
960 "outside of slab", object);
961 } else if (!page->slab) {
962 printk(KERN_ERR
963 "SLUB <none>: no slab for object 0x%p.\n",
964 object);
965 dump_stack();
966 } else
967 object_err(s, page, object,
968 "page slab pointer corrupt.");
969 goto fail;
972 /* Special debug activities for freeing objects */
973 if (!SlabFrozen(page) && !page->freelist)
974 remove_full(s, page);
975 if (s->flags & SLAB_STORE_USER)
976 set_track(s, object, TRACK_FREE, addr);
977 trace(s, page, object, 0);
978 init_object(s, object, 0);
979 return 1;
981 fail:
982 slab_fix(s, "Object at 0x%p not freed", object);
983 return 0;
986 static int __init setup_slub_debug(char *str)
988 slub_debug = DEBUG_DEFAULT_FLAGS;
989 if (*str++ != '=' || !*str)
991 * No options specified. Switch on full debugging.
993 goto out;
995 if (*str == ',')
997 * No options but restriction on slabs. This means full
998 * debugging for slabs matching a pattern.
1000 goto check_slabs;
1002 slub_debug = 0;
1003 if (*str == '-')
1005 * Switch off all debugging measures.
1007 goto out;
1010 * Determine which debug features should be switched on
1012 for (; *str && *str != ','; str++) {
1013 switch (tolower(*str)) {
1014 case 'f':
1015 slub_debug |= SLAB_DEBUG_FREE;
1016 break;
1017 case 'z':
1018 slub_debug |= SLAB_RED_ZONE;
1019 break;
1020 case 'p':
1021 slub_debug |= SLAB_POISON;
1022 break;
1023 case 'u':
1024 slub_debug |= SLAB_STORE_USER;
1025 break;
1026 case 't':
1027 slub_debug |= SLAB_TRACE;
1028 break;
1029 default:
1030 printk(KERN_ERR "slub_debug option '%c' "
1031 "unknown. skipped\n", *str);
1035 check_slabs:
1036 if (*str == ',')
1037 slub_debug_slabs = str + 1;
1038 out:
1039 return 1;
1042 __setup("slub_debug", setup_slub_debug);
1044 static unsigned long kmem_cache_flags(unsigned long objsize,
1045 unsigned long flags, const char *name,
1046 void (*ctor)(struct kmem_cache *, void *))
1049 * Enable debugging if selected on the kernel commandline.
1051 if (slub_debug && (!slub_debug_slabs ||
1052 strncmp(slub_debug_slabs, name, strlen(slub_debug_slabs)) == 0))
1053 flags |= slub_debug;
1055 return flags;
1057 #else
1058 static inline void setup_object_debug(struct kmem_cache *s,
1059 struct page *page, void *object) {}
1061 static inline int alloc_debug_processing(struct kmem_cache *s,
1062 struct page *page, void *object, void *addr) { return 0; }
1064 static inline int free_debug_processing(struct kmem_cache *s,
1065 struct page *page, void *object, void *addr) { return 0; }
1067 static inline int slab_pad_check(struct kmem_cache *s, struct page *page)
1068 { return 1; }
1069 static inline int check_object(struct kmem_cache *s, struct page *page,
1070 void *object, int active) { return 1; }
1071 static inline void add_full(struct kmem_cache_node *n, struct page *page) {}
1072 static inline unsigned long kmem_cache_flags(unsigned long objsize,
1073 unsigned long flags, const char *name,
1074 void (*ctor)(struct kmem_cache *, void *))
1076 return flags;
1078 #define slub_debug 0
1080 static inline unsigned long slabs_node(struct kmem_cache *s, int node)
1081 { return 0; }
1082 static inline void inc_slabs_node(struct kmem_cache *s, int node,
1083 int objects) {}
1084 static inline void dec_slabs_node(struct kmem_cache *s, int node,
1085 int objects) {}
1086 #endif
1089 * Slab allocation and freeing
1091 static inline struct page *alloc_slab_page(gfp_t flags, int node,
1092 struct kmem_cache_order_objects oo)
1094 int order = oo_order(oo);
1096 if (node == -1)
1097 return alloc_pages(flags, order);
1098 else
1099 return alloc_pages_node(node, flags, order);
1102 static struct page *allocate_slab(struct kmem_cache *s, gfp_t flags, int node)
1104 struct page *page;
1105 struct kmem_cache_order_objects oo = s->oo;
1107 flags |= s->allocflags;
1109 page = alloc_slab_page(flags | __GFP_NOWARN | __GFP_NORETRY, node,
1110 oo);
1111 if (unlikely(!page)) {
1112 oo = s->min;
1114 * Allocation may have failed due to fragmentation.
1115 * Try a lower order alloc if possible
1117 page = alloc_slab_page(flags, node, oo);
1118 if (!page)
1119 return NULL;
1121 stat(get_cpu_slab(s, raw_smp_processor_id()), ORDER_FALLBACK);
1123 page->objects = oo_objects(oo);
1124 mod_zone_page_state(page_zone(page),
1125 (s->flags & SLAB_RECLAIM_ACCOUNT) ?
1126 NR_SLAB_RECLAIMABLE : NR_SLAB_UNRECLAIMABLE,
1127 1 << oo_order(oo));
1129 return page;
1132 static void setup_object(struct kmem_cache *s, struct page *page,
1133 void *object)
1135 setup_object_debug(s, page, object);
1136 if (unlikely(s->ctor))
1137 s->ctor(s, object);
1140 static struct page *new_slab(struct kmem_cache *s, gfp_t flags, int node)
1142 struct page *page;
1143 void *start;
1144 void *last;
1145 void *p;
1147 BUG_ON(flags & GFP_SLAB_BUG_MASK);
1149 page = allocate_slab(s,
1150 flags & (GFP_RECLAIM_MASK | GFP_CONSTRAINT_MASK), node);
1151 if (!page)
1152 goto out;
1154 inc_slabs_node(s, page_to_nid(page), page->objects);
1155 page->slab = s;
1156 page->flags |= 1 << PG_slab;
1157 if (s->flags & (SLAB_DEBUG_FREE | SLAB_RED_ZONE | SLAB_POISON |
1158 SLAB_STORE_USER | SLAB_TRACE))
1159 SetSlabDebug(page);
1161 start = page_address(page);
1163 if (unlikely(s->flags & SLAB_POISON))
1164 memset(start, POISON_INUSE, PAGE_SIZE << compound_order(page));
1166 last = start;
1167 for_each_object(p, s, start, page->objects) {
1168 setup_object(s, page, last);
1169 set_freepointer(s, last, p);
1170 last = p;
1172 setup_object(s, page, last);
1173 set_freepointer(s, last, NULL);
1175 page->freelist = start;
1176 page->inuse = 0;
1177 out:
1178 return page;
1181 static void __free_slab(struct kmem_cache *s, struct page *page)
1183 int order = compound_order(page);
1184 int pages = 1 << order;
1186 if (unlikely(SlabDebug(page))) {
1187 void *p;
1189 slab_pad_check(s, page);
1190 for_each_object(p, s, page_address(page),
1191 page->objects)
1192 check_object(s, page, p, 0);
1193 ClearSlabDebug(page);
1196 mod_zone_page_state(page_zone(page),
1197 (s->flags & SLAB_RECLAIM_ACCOUNT) ?
1198 NR_SLAB_RECLAIMABLE : NR_SLAB_UNRECLAIMABLE,
1199 -pages);
1201 __ClearPageSlab(page);
1202 reset_page_mapcount(page);
1203 __free_pages(page, order);
1206 static void rcu_free_slab(struct rcu_head *h)
1208 struct page *page;
1210 page = container_of((struct list_head *)h, struct page, lru);
1211 __free_slab(page->slab, page);
1214 static void free_slab(struct kmem_cache *s, struct page *page)
1216 if (unlikely(s->flags & SLAB_DESTROY_BY_RCU)) {
1218 * RCU free overloads the RCU head over the LRU
1220 struct rcu_head *head = (void *)&page->lru;
1222 call_rcu(head, rcu_free_slab);
1223 } else
1224 __free_slab(s, page);
1227 static void discard_slab(struct kmem_cache *s, struct page *page)
1229 dec_slabs_node(s, page_to_nid(page), page->objects);
1230 free_slab(s, page);
1234 * Per slab locking using the pagelock
1236 static __always_inline void slab_lock(struct page *page)
1238 bit_spin_lock(PG_locked, &page->flags);
1241 static __always_inline void slab_unlock(struct page *page)
1243 __bit_spin_unlock(PG_locked, &page->flags);
1246 static __always_inline int slab_trylock(struct page *page)
1248 int rc = 1;
1250 rc = bit_spin_trylock(PG_locked, &page->flags);
1251 return rc;
1255 * Management of partially allocated slabs
1257 static void add_partial(struct kmem_cache_node *n,
1258 struct page *page, int tail)
1260 spin_lock(&n->list_lock);
1261 n->nr_partial++;
1262 if (tail)
1263 list_add_tail(&page->lru, &n->partial);
1264 else
1265 list_add(&page->lru, &n->partial);
1266 spin_unlock(&n->list_lock);
1269 static void remove_partial(struct kmem_cache *s,
1270 struct page *page)
1272 struct kmem_cache_node *n = get_node(s, page_to_nid(page));
1274 spin_lock(&n->list_lock);
1275 list_del(&page->lru);
1276 n->nr_partial--;
1277 spin_unlock(&n->list_lock);
1281 * Lock slab and remove from the partial list.
1283 * Must hold list_lock.
1285 static inline int lock_and_freeze_slab(struct kmem_cache_node *n, struct page *page)
1287 if (slab_trylock(page)) {
1288 list_del(&page->lru);
1289 n->nr_partial--;
1290 SetSlabFrozen(page);
1291 return 1;
1293 return 0;
1297 * Try to allocate a partial slab from a specific node.
1299 static struct page *get_partial_node(struct kmem_cache_node *n)
1301 struct page *page;
1304 * Racy check. If we mistakenly see no partial slabs then we
1305 * just allocate an empty slab. If we mistakenly try to get a
1306 * partial slab and there is none available then get_partials()
1307 * will return NULL.
1309 if (!n || !n->nr_partial)
1310 return NULL;
1312 spin_lock(&n->list_lock);
1313 list_for_each_entry(page, &n->partial, lru)
1314 if (lock_and_freeze_slab(n, page))
1315 goto out;
1316 page = NULL;
1317 out:
1318 spin_unlock(&n->list_lock);
1319 return page;
1323 * Get a page from somewhere. Search in increasing NUMA distances.
1325 static struct page *get_any_partial(struct kmem_cache *s, gfp_t flags)
1327 #ifdef CONFIG_NUMA
1328 struct zonelist *zonelist;
1329 struct zoneref *z;
1330 struct zone *zone;
1331 enum zone_type high_zoneidx = gfp_zone(flags);
1332 struct page *page;
1335 * The defrag ratio allows a configuration of the tradeoffs between
1336 * inter node defragmentation and node local allocations. A lower
1337 * defrag_ratio increases the tendency to do local allocations
1338 * instead of attempting to obtain partial slabs from other nodes.
1340 * If the defrag_ratio is set to 0 then kmalloc() always
1341 * returns node local objects. If the ratio is higher then kmalloc()
1342 * may return off node objects because partial slabs are obtained
1343 * from other nodes and filled up.
1345 * If /sys/kernel/slab/xx/defrag_ratio is set to 100 (which makes
1346 * defrag_ratio = 1000) then every (well almost) allocation will
1347 * first attempt to defrag slab caches on other nodes. This means
1348 * scanning over all nodes to look for partial slabs which may be
1349 * expensive if we do it every time we are trying to find a slab
1350 * with available objects.
1352 if (!s->remote_node_defrag_ratio ||
1353 get_cycles() % 1024 > s->remote_node_defrag_ratio)
1354 return NULL;
1356 zonelist = node_zonelist(slab_node(current->mempolicy), flags);
1357 for_each_zone_zonelist(zone, z, zonelist, high_zoneidx) {
1358 struct kmem_cache_node *n;
1360 n = get_node(s, zone_to_nid(zone));
1362 if (n && cpuset_zone_allowed_hardwall(zone, flags) &&
1363 n->nr_partial > MIN_PARTIAL) {
1364 page = get_partial_node(n);
1365 if (page)
1366 return page;
1369 #endif
1370 return NULL;
1374 * Get a partial page, lock it and return it.
1376 static struct page *get_partial(struct kmem_cache *s, gfp_t flags, int node)
1378 struct page *page;
1379 int searchnode = (node == -1) ? numa_node_id() : node;
1381 page = get_partial_node(get_node(s, searchnode));
1382 if (page || (flags & __GFP_THISNODE))
1383 return page;
1385 return get_any_partial(s, flags);
1389 * Move a page back to the lists.
1391 * Must be called with the slab lock held.
1393 * On exit the slab lock will have been dropped.
1395 static void unfreeze_slab(struct kmem_cache *s, struct page *page, int tail)
1397 struct kmem_cache_node *n = get_node(s, page_to_nid(page));
1398 struct kmem_cache_cpu *c = get_cpu_slab(s, smp_processor_id());
1400 ClearSlabFrozen(page);
1401 if (page->inuse) {
1403 if (page->freelist) {
1404 add_partial(n, page, tail);
1405 stat(c, tail ? DEACTIVATE_TO_TAIL : DEACTIVATE_TO_HEAD);
1406 } else {
1407 stat(c, DEACTIVATE_FULL);
1408 if (SlabDebug(page) && (s->flags & SLAB_STORE_USER))
1409 add_full(n, page);
1411 slab_unlock(page);
1412 } else {
1413 stat(c, DEACTIVATE_EMPTY);
1414 if (n->nr_partial < MIN_PARTIAL) {
1416 * Adding an empty slab to the partial slabs in order
1417 * to avoid page allocator overhead. This slab needs
1418 * to come after the other slabs with objects in
1419 * so that the others get filled first. That way the
1420 * size of the partial list stays small.
1422 * kmem_cache_shrink can reclaim any empty slabs from the
1423 * partial list.
1425 add_partial(n, page, 1);
1426 slab_unlock(page);
1427 } else {
1428 slab_unlock(page);
1429 stat(get_cpu_slab(s, raw_smp_processor_id()), FREE_SLAB);
1430 discard_slab(s, page);
1436 * Remove the cpu slab
1438 static void deactivate_slab(struct kmem_cache *s, struct kmem_cache_cpu *c)
1440 struct page *page = c->page;
1441 int tail = 1;
1443 if (page->freelist)
1444 stat(c, DEACTIVATE_REMOTE_FREES);
1446 * Merge cpu freelist into slab freelist. Typically we get here
1447 * because both freelists are empty. So this is unlikely
1448 * to occur.
1450 while (unlikely(c->freelist)) {
1451 void **object;
1453 tail = 0; /* Hot objects. Put the slab first */
1455 /* Retrieve object from cpu_freelist */
1456 object = c->freelist;
1457 c->freelist = c->freelist[c->offset];
1459 /* And put onto the regular freelist */
1460 object[c->offset] = page->freelist;
1461 page->freelist = object;
1462 page->inuse--;
1464 c->page = NULL;
1465 unfreeze_slab(s, page, tail);
1468 static inline void flush_slab(struct kmem_cache *s, struct kmem_cache_cpu *c)
1470 stat(c, CPUSLAB_FLUSH);
1471 slab_lock(c->page);
1472 deactivate_slab(s, c);
1476 * Flush cpu slab.
1478 * Called from IPI handler with interrupts disabled.
1480 static inline void __flush_cpu_slab(struct kmem_cache *s, int cpu)
1482 struct kmem_cache_cpu *c = get_cpu_slab(s, cpu);
1484 if (likely(c && c->page))
1485 flush_slab(s, c);
1488 static void flush_cpu_slab(void *d)
1490 struct kmem_cache *s = d;
1492 __flush_cpu_slab(s, smp_processor_id());
1495 static void flush_all(struct kmem_cache *s)
1497 #ifdef CONFIG_SMP
1498 on_each_cpu(flush_cpu_slab, s, 1, 1);
1499 #else
1500 unsigned long flags;
1502 local_irq_save(flags);
1503 flush_cpu_slab(s);
1504 local_irq_restore(flags);
1505 #endif
1509 * Check if the objects in a per cpu structure fit numa
1510 * locality expectations.
1512 static inline int node_match(struct kmem_cache_cpu *c, int node)
1514 #ifdef CONFIG_NUMA
1515 if (node != -1 && c->node != node)
1516 return 0;
1517 #endif
1518 return 1;
1522 * Slow path. The lockless freelist is empty or we need to perform
1523 * debugging duties.
1525 * Interrupts are disabled.
1527 * Processing is still very fast if new objects have been freed to the
1528 * regular freelist. In that case we simply take over the regular freelist
1529 * as the lockless freelist and zap the regular freelist.
1531 * If that is not working then we fall back to the partial lists. We take the
1532 * first element of the freelist as the object to allocate now and move the
1533 * rest of the freelist to the lockless freelist.
1535 * And if we were unable to get a new slab from the partial slab lists then
1536 * we need to allocate a new slab. This is the slowest path since it involves
1537 * a call to the page allocator and the setup of a new slab.
1539 static void *__slab_alloc(struct kmem_cache *s,
1540 gfp_t gfpflags, int node, void *addr, struct kmem_cache_cpu *c)
1542 void **object;
1543 struct page *new;
1545 /* We handle __GFP_ZERO in the caller */
1546 gfpflags &= ~__GFP_ZERO;
1548 if (!c->page)
1549 goto new_slab;
1551 slab_lock(c->page);
1552 if (unlikely(!node_match(c, node)))
1553 goto another_slab;
1555 stat(c, ALLOC_REFILL);
1557 load_freelist:
1558 object = c->page->freelist;
1559 if (unlikely(!object))
1560 goto another_slab;
1561 if (unlikely(SlabDebug(c->page)))
1562 goto debug;
1564 c->freelist = object[c->offset];
1565 c->page->inuse = c->page->objects;
1566 c->page->freelist = NULL;
1567 c->node = page_to_nid(c->page);
1568 unlock_out:
1569 slab_unlock(c->page);
1570 stat(c, ALLOC_SLOWPATH);
1571 return object;
1573 another_slab:
1574 deactivate_slab(s, c);
1576 new_slab:
1577 new = get_partial(s, gfpflags, node);
1578 if (new) {
1579 c->page = new;
1580 stat(c, ALLOC_FROM_PARTIAL);
1581 goto load_freelist;
1584 if (gfpflags & __GFP_WAIT)
1585 local_irq_enable();
1587 new = new_slab(s, gfpflags, node);
1589 if (gfpflags & __GFP_WAIT)
1590 local_irq_disable();
1592 if (new) {
1593 c = get_cpu_slab(s, smp_processor_id());
1594 stat(c, ALLOC_SLAB);
1595 if (c->page)
1596 flush_slab(s, c);
1597 slab_lock(new);
1598 SetSlabFrozen(new);
1599 c->page = new;
1600 goto load_freelist;
1602 return NULL;
1603 debug:
1604 if (!alloc_debug_processing(s, c->page, object, addr))
1605 goto another_slab;
1607 c->page->inuse++;
1608 c->page->freelist = object[c->offset];
1609 c->node = -1;
1610 goto unlock_out;
1614 * Inlined fastpath so that allocation functions (kmalloc, kmem_cache_alloc)
1615 * have the fastpath folded into their functions. So no function call
1616 * overhead for requests that can be satisfied on the fastpath.
1618 * The fastpath works by first checking if the lockless freelist can be used.
1619 * If not then __slab_alloc is called for slow processing.
1621 * Otherwise we can simply pick the next object from the lockless free list.
1623 static __always_inline void *slab_alloc(struct kmem_cache *s,
1624 gfp_t gfpflags, int node, void *addr)
1626 void **object;
1627 struct kmem_cache_cpu *c;
1628 unsigned long flags;
1630 local_irq_save(flags);
1631 c = get_cpu_slab(s, smp_processor_id());
1632 if (unlikely(!c->freelist || !node_match(c, node)))
1634 object = __slab_alloc(s, gfpflags, node, addr, c);
1636 else {
1637 object = c->freelist;
1638 c->freelist = object[c->offset];
1639 stat(c, ALLOC_FASTPATH);
1641 local_irq_restore(flags);
1643 if (unlikely((gfpflags & __GFP_ZERO) && object))
1644 memset(object, 0, c->objsize);
1646 return object;
1649 void *kmem_cache_alloc(struct kmem_cache *s, gfp_t gfpflags)
1651 return slab_alloc(s, gfpflags, -1, __builtin_return_address(0));
1653 EXPORT_SYMBOL(kmem_cache_alloc);
1655 #ifdef CONFIG_NUMA
1656 void *kmem_cache_alloc_node(struct kmem_cache *s, gfp_t gfpflags, int node)
1658 return slab_alloc(s, gfpflags, node, __builtin_return_address(0));
1660 EXPORT_SYMBOL(kmem_cache_alloc_node);
1661 #endif
1664 * Slow patch handling. This may still be called frequently since objects
1665 * have a longer lifetime than the cpu slabs in most processing loads.
1667 * So we still attempt to reduce cache line usage. Just take the slab
1668 * lock and free the item. If there is no additional partial page
1669 * handling required then we can return immediately.
1671 static void __slab_free(struct kmem_cache *s, struct page *page,
1672 void *x, void *addr, unsigned int offset)
1674 void *prior;
1675 void **object = (void *)x;
1676 struct kmem_cache_cpu *c;
1678 c = get_cpu_slab(s, raw_smp_processor_id());
1679 stat(c, FREE_SLOWPATH);
1680 slab_lock(page);
1682 if (unlikely(SlabDebug(page)))
1683 goto debug;
1685 checks_ok:
1686 prior = object[offset] = page->freelist;
1687 page->freelist = object;
1688 page->inuse--;
1690 if (unlikely(SlabFrozen(page))) {
1691 stat(c, FREE_FROZEN);
1692 goto out_unlock;
1695 if (unlikely(!page->inuse))
1696 goto slab_empty;
1699 * Objects left in the slab. If it was not on the partial list before
1700 * then add it.
1702 if (unlikely(!prior)) {
1703 add_partial(get_node(s, page_to_nid(page)), page, 1);
1704 stat(c, FREE_ADD_PARTIAL);
1707 out_unlock:
1708 slab_unlock(page);
1709 return;
1711 slab_empty:
1712 if (prior) {
1714 * Slab still on the partial list.
1716 remove_partial(s, page);
1717 stat(c, FREE_REMOVE_PARTIAL);
1719 slab_unlock(page);
1720 stat(c, FREE_SLAB);
1721 discard_slab(s, page);
1722 return;
1724 debug:
1725 if (!free_debug_processing(s, page, x, addr))
1726 goto out_unlock;
1727 goto checks_ok;
1731 * Fastpath with forced inlining to produce a kfree and kmem_cache_free that
1732 * can perform fastpath freeing without additional function calls.
1734 * The fastpath is only possible if we are freeing to the current cpu slab
1735 * of this processor. This typically the case if we have just allocated
1736 * the item before.
1738 * If fastpath is not possible then fall back to __slab_free where we deal
1739 * with all sorts of special processing.
1741 static __always_inline void slab_free(struct kmem_cache *s,
1742 struct page *page, void *x, void *addr)
1744 void **object = (void *)x;
1745 struct kmem_cache_cpu *c;
1746 unsigned long flags;
1748 local_irq_save(flags);
1749 c = get_cpu_slab(s, smp_processor_id());
1750 debug_check_no_locks_freed(object, c->objsize);
1751 if (!(s->flags & SLAB_DEBUG_OBJECTS))
1752 debug_check_no_obj_freed(object, s->objsize);
1753 if (likely(page == c->page && c->node >= 0)) {
1754 object[c->offset] = c->freelist;
1755 c->freelist = object;
1756 stat(c, FREE_FASTPATH);
1757 } else
1758 __slab_free(s, page, x, addr, c->offset);
1760 local_irq_restore(flags);
1763 void kmem_cache_free(struct kmem_cache *s, void *x)
1765 struct page *page;
1767 page = virt_to_head_page(x);
1769 slab_free(s, page, x, __builtin_return_address(0));
1771 EXPORT_SYMBOL(kmem_cache_free);
1773 /* Figure out on which slab object the object resides */
1774 static struct page *get_object_page(const void *x)
1776 struct page *page = virt_to_head_page(x);
1778 if (!PageSlab(page))
1779 return NULL;
1781 return page;
1785 * Object placement in a slab is made very easy because we always start at
1786 * offset 0. If we tune the size of the object to the alignment then we can
1787 * get the required alignment by putting one properly sized object after
1788 * another.
1790 * Notice that the allocation order determines the sizes of the per cpu
1791 * caches. Each processor has always one slab available for allocations.
1792 * Increasing the allocation order reduces the number of times that slabs
1793 * must be moved on and off the partial lists and is therefore a factor in
1794 * locking overhead.
1798 * Mininum / Maximum order of slab pages. This influences locking overhead
1799 * and slab fragmentation. A higher order reduces the number of partial slabs
1800 * and increases the number of allocations possible without having to
1801 * take the list_lock.
1803 static int slub_min_order;
1804 static int slub_max_order = PAGE_ALLOC_COSTLY_ORDER;
1805 static int slub_min_objects;
1808 * Merge control. If this is set then no merging of slab caches will occur.
1809 * (Could be removed. This was introduced to pacify the merge skeptics.)
1811 static int slub_nomerge;
1814 * Calculate the order of allocation given an slab object size.
1816 * The order of allocation has significant impact on performance and other
1817 * system components. Generally order 0 allocations should be preferred since
1818 * order 0 does not cause fragmentation in the page allocator. Larger objects
1819 * be problematic to put into order 0 slabs because there may be too much
1820 * unused space left. We go to a higher order if more than 1/16th of the slab
1821 * would be wasted.
1823 * In order to reach satisfactory performance we must ensure that a minimum
1824 * number of objects is in one slab. Otherwise we may generate too much
1825 * activity on the partial lists which requires taking the list_lock. This is
1826 * less a concern for large slabs though which are rarely used.
1828 * slub_max_order specifies the order where we begin to stop considering the
1829 * number of objects in a slab as critical. If we reach slub_max_order then
1830 * we try to keep the page order as low as possible. So we accept more waste
1831 * of space in favor of a small page order.
1833 * Higher order allocations also allow the placement of more objects in a
1834 * slab and thereby reduce object handling overhead. If the user has
1835 * requested a higher mininum order then we start with that one instead of
1836 * the smallest order which will fit the object.
1838 static inline int slab_order(int size, int min_objects,
1839 int max_order, int fract_leftover)
1841 int order;
1842 int rem;
1843 int min_order = slub_min_order;
1845 if ((PAGE_SIZE << min_order) / size > 65535)
1846 return get_order(size * 65535) - 1;
1848 for (order = max(min_order,
1849 fls(min_objects * size - 1) - PAGE_SHIFT);
1850 order <= max_order; order++) {
1852 unsigned long slab_size = PAGE_SIZE << order;
1854 if (slab_size < min_objects * size)
1855 continue;
1857 rem = slab_size % size;
1859 if (rem <= slab_size / fract_leftover)
1860 break;
1864 return order;
1867 static inline int calculate_order(int size)
1869 int order;
1870 int min_objects;
1871 int fraction;
1874 * Attempt to find best configuration for a slab. This
1875 * works by first attempting to generate a layout with
1876 * the best configuration and backing off gradually.
1878 * First we reduce the acceptable waste in a slab. Then
1879 * we reduce the minimum objects required in a slab.
1881 min_objects = slub_min_objects;
1882 if (!min_objects)
1883 min_objects = 4 * (fls(nr_cpu_ids) + 1);
1884 while (min_objects > 1) {
1885 fraction = 16;
1886 while (fraction >= 4) {
1887 order = slab_order(size, min_objects,
1888 slub_max_order, fraction);
1889 if (order <= slub_max_order)
1890 return order;
1891 fraction /= 2;
1893 min_objects /= 2;
1897 * We were unable to place multiple objects in a slab. Now
1898 * lets see if we can place a single object there.
1900 order = slab_order(size, 1, slub_max_order, 1);
1901 if (order <= slub_max_order)
1902 return order;
1905 * Doh this slab cannot be placed using slub_max_order.
1907 order = slab_order(size, 1, MAX_ORDER, 1);
1908 if (order <= MAX_ORDER)
1909 return order;
1910 return -ENOSYS;
1914 * Figure out what the alignment of the objects will be.
1916 static unsigned long calculate_alignment(unsigned long flags,
1917 unsigned long align, unsigned long size)
1920 * If the user wants hardware cache aligned objects then follow that
1921 * suggestion if the object is sufficiently large.
1923 * The hardware cache alignment cannot override the specified
1924 * alignment though. If that is greater then use it.
1926 if (flags & SLAB_HWCACHE_ALIGN) {
1927 unsigned long ralign = cache_line_size();
1928 while (size <= ralign / 2)
1929 ralign /= 2;
1930 align = max(align, ralign);
1933 if (align < ARCH_SLAB_MINALIGN)
1934 align = ARCH_SLAB_MINALIGN;
1936 return ALIGN(align, sizeof(void *));
1939 static void init_kmem_cache_cpu(struct kmem_cache *s,
1940 struct kmem_cache_cpu *c)
1942 c->page = NULL;
1943 c->freelist = NULL;
1944 c->node = 0;
1945 c->offset = s->offset / sizeof(void *);
1946 c->objsize = s->objsize;
1947 #ifdef CONFIG_SLUB_STATS
1948 memset(c->stat, 0, NR_SLUB_STAT_ITEMS * sizeof(unsigned));
1949 #endif
1952 static void init_kmem_cache_node(struct kmem_cache_node *n)
1954 n->nr_partial = 0;
1955 spin_lock_init(&n->list_lock);
1956 INIT_LIST_HEAD(&n->partial);
1957 #ifdef CONFIG_SLUB_DEBUG
1958 atomic_long_set(&n->nr_slabs, 0);
1959 INIT_LIST_HEAD(&n->full);
1960 #endif
1963 #ifdef CONFIG_SMP
1965 * Per cpu array for per cpu structures.
1967 * The per cpu array places all kmem_cache_cpu structures from one processor
1968 * close together meaning that it becomes possible that multiple per cpu
1969 * structures are contained in one cacheline. This may be particularly
1970 * beneficial for the kmalloc caches.
1972 * A desktop system typically has around 60-80 slabs. With 100 here we are
1973 * likely able to get per cpu structures for all caches from the array defined
1974 * here. We must be able to cover all kmalloc caches during bootstrap.
1976 * If the per cpu array is exhausted then fall back to kmalloc
1977 * of individual cachelines. No sharing is possible then.
1979 #define NR_KMEM_CACHE_CPU 100
1981 static DEFINE_PER_CPU(struct kmem_cache_cpu,
1982 kmem_cache_cpu)[NR_KMEM_CACHE_CPU];
1984 static DEFINE_PER_CPU(struct kmem_cache_cpu *, kmem_cache_cpu_free);
1985 static cpumask_t kmem_cach_cpu_free_init_once = CPU_MASK_NONE;
1987 static struct kmem_cache_cpu *alloc_kmem_cache_cpu(struct kmem_cache *s,
1988 int cpu, gfp_t flags)
1990 struct kmem_cache_cpu *c = per_cpu(kmem_cache_cpu_free, cpu);
1992 if (c)
1993 per_cpu(kmem_cache_cpu_free, cpu) =
1994 (void *)c->freelist;
1995 else {
1996 /* Table overflow: So allocate ourselves */
1997 c = kmalloc_node(
1998 ALIGN(sizeof(struct kmem_cache_cpu), cache_line_size()),
1999 flags, cpu_to_node(cpu));
2000 if (!c)
2001 return NULL;
2004 init_kmem_cache_cpu(s, c);
2005 return c;
2008 static void free_kmem_cache_cpu(struct kmem_cache_cpu *c, int cpu)
2010 if (c < per_cpu(kmem_cache_cpu, cpu) ||
2011 c > per_cpu(kmem_cache_cpu, cpu) + NR_KMEM_CACHE_CPU) {
2012 kfree(c);
2013 return;
2015 c->freelist = (void *)per_cpu(kmem_cache_cpu_free, cpu);
2016 per_cpu(kmem_cache_cpu_free, cpu) = c;
2019 static void free_kmem_cache_cpus(struct kmem_cache *s)
2021 int cpu;
2023 for_each_online_cpu(cpu) {
2024 struct kmem_cache_cpu *c = get_cpu_slab(s, cpu);
2026 if (c) {
2027 s->cpu_slab[cpu] = NULL;
2028 free_kmem_cache_cpu(c, cpu);
2033 static int alloc_kmem_cache_cpus(struct kmem_cache *s, gfp_t flags)
2035 int cpu;
2037 for_each_online_cpu(cpu) {
2038 struct kmem_cache_cpu *c = get_cpu_slab(s, cpu);
2040 if (c)
2041 continue;
2043 c = alloc_kmem_cache_cpu(s, cpu, flags);
2044 if (!c) {
2045 free_kmem_cache_cpus(s);
2046 return 0;
2048 s->cpu_slab[cpu] = c;
2050 return 1;
2054 * Initialize the per cpu array.
2056 static void init_alloc_cpu_cpu(int cpu)
2058 int i;
2060 if (cpu_isset(cpu, kmem_cach_cpu_free_init_once))
2061 return;
2063 for (i = NR_KMEM_CACHE_CPU - 1; i >= 0; i--)
2064 free_kmem_cache_cpu(&per_cpu(kmem_cache_cpu, cpu)[i], cpu);
2066 cpu_set(cpu, kmem_cach_cpu_free_init_once);
2069 static void __init init_alloc_cpu(void)
2071 int cpu;
2073 for_each_online_cpu(cpu)
2074 init_alloc_cpu_cpu(cpu);
2077 #else
2078 static inline void free_kmem_cache_cpus(struct kmem_cache *s) {}
2079 static inline void init_alloc_cpu(void) {}
2081 static inline int alloc_kmem_cache_cpus(struct kmem_cache *s, gfp_t flags)
2083 init_kmem_cache_cpu(s, &s->cpu_slab);
2084 return 1;
2086 #endif
2088 #ifdef CONFIG_NUMA
2090 * No kmalloc_node yet so do it by hand. We know that this is the first
2091 * slab on the node for this slabcache. There are no concurrent accesses
2092 * possible.
2094 * Note that this function only works on the kmalloc_node_cache
2095 * when allocating for the kmalloc_node_cache. This is used for bootstrapping
2096 * memory on a fresh node that has no slab structures yet.
2098 static struct kmem_cache_node *early_kmem_cache_node_alloc(gfp_t gfpflags,
2099 int node)
2101 struct page *page;
2102 struct kmem_cache_node *n;
2103 unsigned long flags;
2105 BUG_ON(kmalloc_caches->size < sizeof(struct kmem_cache_node));
2107 page = new_slab(kmalloc_caches, gfpflags, node);
2109 BUG_ON(!page);
2110 if (page_to_nid(page) != node) {
2111 printk(KERN_ERR "SLUB: Unable to allocate memory from "
2112 "node %d\n", node);
2113 printk(KERN_ERR "SLUB: Allocating a useless per node structure "
2114 "in order to be able to continue\n");
2117 n = page->freelist;
2118 BUG_ON(!n);
2119 page->freelist = get_freepointer(kmalloc_caches, n);
2120 page->inuse++;
2121 kmalloc_caches->node[node] = n;
2122 #ifdef CONFIG_SLUB_DEBUG
2123 init_object(kmalloc_caches, n, 1);
2124 init_tracking(kmalloc_caches, n);
2125 #endif
2126 init_kmem_cache_node(n);
2127 inc_slabs_node(kmalloc_caches, node, page->objects);
2130 * lockdep requires consistent irq usage for each lock
2131 * so even though there cannot be a race this early in
2132 * the boot sequence, we still disable irqs.
2134 local_irq_save(flags);
2135 add_partial(n, page, 0);
2136 local_irq_restore(flags);
2137 return n;
2140 static void free_kmem_cache_nodes(struct kmem_cache *s)
2142 int node;
2144 for_each_node_state(node, N_NORMAL_MEMORY) {
2145 struct kmem_cache_node *n = s->node[node];
2146 if (n && n != &s->local_node)
2147 kmem_cache_free(kmalloc_caches, n);
2148 s->node[node] = NULL;
2152 static int init_kmem_cache_nodes(struct kmem_cache *s, gfp_t gfpflags)
2154 int node;
2155 int local_node;
2157 if (slab_state >= UP)
2158 local_node = page_to_nid(virt_to_page(s));
2159 else
2160 local_node = 0;
2162 for_each_node_state(node, N_NORMAL_MEMORY) {
2163 struct kmem_cache_node *n;
2165 if (local_node == node)
2166 n = &s->local_node;
2167 else {
2168 if (slab_state == DOWN) {
2169 n = early_kmem_cache_node_alloc(gfpflags,
2170 node);
2171 continue;
2173 n = kmem_cache_alloc_node(kmalloc_caches,
2174 gfpflags, node);
2176 if (!n) {
2177 free_kmem_cache_nodes(s);
2178 return 0;
2182 s->node[node] = n;
2183 init_kmem_cache_node(n);
2185 return 1;
2187 #else
2188 static void free_kmem_cache_nodes(struct kmem_cache *s)
2192 static int init_kmem_cache_nodes(struct kmem_cache *s, gfp_t gfpflags)
2194 init_kmem_cache_node(&s->local_node);
2195 return 1;
2197 #endif
2200 * calculate_sizes() determines the order and the distribution of data within
2201 * a slab object.
2203 static int calculate_sizes(struct kmem_cache *s, int forced_order)
2205 unsigned long flags = s->flags;
2206 unsigned long size = s->objsize;
2207 unsigned long align = s->align;
2208 int order;
2211 * Round up object size to the next word boundary. We can only
2212 * place the free pointer at word boundaries and this determines
2213 * the possible location of the free pointer.
2215 size = ALIGN(size, sizeof(void *));
2217 #ifdef CONFIG_SLUB_DEBUG
2219 * Determine if we can poison the object itself. If the user of
2220 * the slab may touch the object after free or before allocation
2221 * then we should never poison the object itself.
2223 if ((flags & SLAB_POISON) && !(flags & SLAB_DESTROY_BY_RCU) &&
2224 !s->ctor)
2225 s->flags |= __OBJECT_POISON;
2226 else
2227 s->flags &= ~__OBJECT_POISON;
2231 * If we are Redzoning then check if there is some space between the
2232 * end of the object and the free pointer. If not then add an
2233 * additional word to have some bytes to store Redzone information.
2235 if ((flags & SLAB_RED_ZONE) && size == s->objsize)
2236 size += sizeof(void *);
2237 #endif
2240 * With that we have determined the number of bytes in actual use
2241 * by the object. This is the potential offset to the free pointer.
2243 s->inuse = size;
2245 if (((flags & (SLAB_DESTROY_BY_RCU | SLAB_POISON)) ||
2246 s->ctor)) {
2248 * Relocate free pointer after the object if it is not
2249 * permitted to overwrite the first word of the object on
2250 * kmem_cache_free.
2252 * This is the case if we do RCU, have a constructor or
2253 * destructor or are poisoning the objects.
2255 s->offset = size;
2256 size += sizeof(void *);
2259 #ifdef CONFIG_SLUB_DEBUG
2260 if (flags & SLAB_STORE_USER)
2262 * Need to store information about allocs and frees after
2263 * the object.
2265 size += 2 * sizeof(struct track);
2267 if (flags & SLAB_RED_ZONE)
2269 * Add some empty padding so that we can catch
2270 * overwrites from earlier objects rather than let
2271 * tracking information or the free pointer be
2272 * corrupted if an user writes before the start
2273 * of the object.
2275 size += sizeof(void *);
2276 #endif
2279 * Determine the alignment based on various parameters that the
2280 * user specified and the dynamic determination of cache line size
2281 * on bootup.
2283 align = calculate_alignment(flags, align, s->objsize);
2286 * SLUB stores one object immediately after another beginning from
2287 * offset 0. In order to align the objects we have to simply size
2288 * each object to conform to the alignment.
2290 size = ALIGN(size, align);
2291 s->size = size;
2292 if (forced_order >= 0)
2293 order = forced_order;
2294 else
2295 order = calculate_order(size);
2297 if (order < 0)
2298 return 0;
2300 s->allocflags = 0;
2301 if (order)
2302 s->allocflags |= __GFP_COMP;
2304 if (s->flags & SLAB_CACHE_DMA)
2305 s->allocflags |= SLUB_DMA;
2307 if (s->flags & SLAB_RECLAIM_ACCOUNT)
2308 s->allocflags |= __GFP_RECLAIMABLE;
2311 * Determine the number of objects per slab
2313 s->oo = oo_make(order, size);
2314 s->min = oo_make(get_order(size), size);
2315 if (oo_objects(s->oo) > oo_objects(s->max))
2316 s->max = s->oo;
2318 return !!oo_objects(s->oo);
2322 static int kmem_cache_open(struct kmem_cache *s, gfp_t gfpflags,
2323 const char *name, size_t size,
2324 size_t align, unsigned long flags,
2325 void (*ctor)(struct kmem_cache *, void *))
2327 memset(s, 0, kmem_size);
2328 s->name = name;
2329 s->ctor = ctor;
2330 s->objsize = size;
2331 s->align = align;
2332 s->flags = kmem_cache_flags(size, flags, name, ctor);
2334 if (!calculate_sizes(s, -1))
2335 goto error;
2337 s->refcount = 1;
2338 #ifdef CONFIG_NUMA
2339 s->remote_node_defrag_ratio = 100;
2340 #endif
2341 if (!init_kmem_cache_nodes(s, gfpflags & ~SLUB_DMA))
2342 goto error;
2344 if (alloc_kmem_cache_cpus(s, gfpflags & ~SLUB_DMA))
2345 return 1;
2346 free_kmem_cache_nodes(s);
2347 error:
2348 if (flags & SLAB_PANIC)
2349 panic("Cannot create slab %s size=%lu realsize=%u "
2350 "order=%u offset=%u flags=%lx\n",
2351 s->name, (unsigned long)size, s->size, oo_order(s->oo),
2352 s->offset, flags);
2353 return 0;
2357 * Check if a given pointer is valid
2359 int kmem_ptr_validate(struct kmem_cache *s, const void *object)
2361 struct page *page;
2363 page = get_object_page(object);
2365 if (!page || s != page->slab)
2366 /* No slab or wrong slab */
2367 return 0;
2369 if (!check_valid_pointer(s, page, object))
2370 return 0;
2373 * We could also check if the object is on the slabs freelist.
2374 * But this would be too expensive and it seems that the main
2375 * purpose of kmem_ptr_valid() is to check if the object belongs
2376 * to a certain slab.
2378 return 1;
2380 EXPORT_SYMBOL(kmem_ptr_validate);
2383 * Determine the size of a slab object
2385 unsigned int kmem_cache_size(struct kmem_cache *s)
2387 return s->objsize;
2389 EXPORT_SYMBOL(kmem_cache_size);
2391 const char *kmem_cache_name(struct kmem_cache *s)
2393 return s->name;
2395 EXPORT_SYMBOL(kmem_cache_name);
2397 static void list_slab_objects(struct kmem_cache *s, struct page *page,
2398 const char *text)
2400 #ifdef CONFIG_SLUB_DEBUG
2401 void *addr = page_address(page);
2402 void *p;
2403 DECLARE_BITMAP(map, page->objects);
2405 bitmap_zero(map, page->objects);
2406 slab_err(s, page, "%s", text);
2407 slab_lock(page);
2408 for_each_free_object(p, s, page->freelist)
2409 set_bit(slab_index(p, s, addr), map);
2411 for_each_object(p, s, addr, page->objects) {
2413 if (!test_bit(slab_index(p, s, addr), map)) {
2414 printk(KERN_ERR "INFO: Object 0x%p @offset=%tu\n",
2415 p, p - addr);
2416 print_tracking(s, p);
2419 slab_unlock(page);
2420 #endif
2424 * Attempt to free all partial slabs on a node.
2426 static void free_partial(struct kmem_cache *s, struct kmem_cache_node *n)
2428 unsigned long flags;
2429 struct page *page, *h;
2431 spin_lock_irqsave(&n->list_lock, flags);
2432 list_for_each_entry_safe(page, h, &n->partial, lru) {
2433 if (!page->inuse) {
2434 list_del(&page->lru);
2435 discard_slab(s, page);
2436 n->nr_partial--;
2437 } else {
2438 list_slab_objects(s, page,
2439 "Objects remaining on kmem_cache_close()");
2442 spin_unlock_irqrestore(&n->list_lock, flags);
2446 * Release all resources used by a slab cache.
2448 static inline int kmem_cache_close(struct kmem_cache *s)
2450 int node;
2452 flush_all(s);
2454 /* Attempt to free all objects */
2455 free_kmem_cache_cpus(s);
2456 for_each_node_state(node, N_NORMAL_MEMORY) {
2457 struct kmem_cache_node *n = get_node(s, node);
2459 free_partial(s, n);
2460 if (n->nr_partial || slabs_node(s, node))
2461 return 1;
2463 free_kmem_cache_nodes(s);
2464 return 0;
2468 * Close a cache and release the kmem_cache structure
2469 * (must be used for caches created using kmem_cache_create)
2471 void kmem_cache_destroy(struct kmem_cache *s)
2473 down_write(&slub_lock);
2474 s->refcount--;
2475 if (!s->refcount) {
2476 list_del(&s->list);
2477 up_write(&slub_lock);
2478 if (kmem_cache_close(s)) {
2479 printk(KERN_ERR "SLUB %s: %s called for cache that "
2480 "still has objects.\n", s->name, __func__);
2481 dump_stack();
2483 sysfs_slab_remove(s);
2484 } else
2485 up_write(&slub_lock);
2487 EXPORT_SYMBOL(kmem_cache_destroy);
2489 /********************************************************************
2490 * Kmalloc subsystem
2491 *******************************************************************/
2493 struct kmem_cache kmalloc_caches[PAGE_SHIFT + 1] __cacheline_aligned;
2494 EXPORT_SYMBOL(kmalloc_caches);
2496 static int __init setup_slub_min_order(char *str)
2498 get_option(&str, &slub_min_order);
2500 return 1;
2503 __setup("slub_min_order=", setup_slub_min_order);
2505 static int __init setup_slub_max_order(char *str)
2507 get_option(&str, &slub_max_order);
2509 return 1;
2512 __setup("slub_max_order=", setup_slub_max_order);
2514 static int __init setup_slub_min_objects(char *str)
2516 get_option(&str, &slub_min_objects);
2518 return 1;
2521 __setup("slub_min_objects=", setup_slub_min_objects);
2523 static int __init setup_slub_nomerge(char *str)
2525 slub_nomerge = 1;
2526 return 1;
2529 __setup("slub_nomerge", setup_slub_nomerge);
2531 static struct kmem_cache *create_kmalloc_cache(struct kmem_cache *s,
2532 const char *name, int size, gfp_t gfp_flags)
2534 unsigned int flags = 0;
2536 if (gfp_flags & SLUB_DMA)
2537 flags = SLAB_CACHE_DMA;
2539 down_write(&slub_lock);
2540 if (!kmem_cache_open(s, gfp_flags, name, size, ARCH_KMALLOC_MINALIGN,
2541 flags, NULL))
2542 goto panic;
2544 list_add(&s->list, &slab_caches);
2545 up_write(&slub_lock);
2546 if (sysfs_slab_add(s))
2547 goto panic;
2548 return s;
2550 panic:
2551 panic("Creation of kmalloc slab %s size=%d failed.\n", name, size);
2554 #ifdef CONFIG_ZONE_DMA
2555 static struct kmem_cache *kmalloc_caches_dma[PAGE_SHIFT + 1];
2557 static void sysfs_add_func(struct work_struct *w)
2559 struct kmem_cache *s;
2561 down_write(&slub_lock);
2562 list_for_each_entry(s, &slab_caches, list) {
2563 if (s->flags & __SYSFS_ADD_DEFERRED) {
2564 s->flags &= ~__SYSFS_ADD_DEFERRED;
2565 sysfs_slab_add(s);
2568 up_write(&slub_lock);
2571 static DECLARE_WORK(sysfs_add_work, sysfs_add_func);
2573 static noinline struct kmem_cache *dma_kmalloc_cache(int index, gfp_t flags)
2575 struct kmem_cache *s;
2576 char *text;
2577 size_t realsize;
2579 s = kmalloc_caches_dma[index];
2580 if (s)
2581 return s;
2583 /* Dynamically create dma cache */
2584 if (flags & __GFP_WAIT)
2585 down_write(&slub_lock);
2586 else {
2587 if (!down_write_trylock(&slub_lock))
2588 goto out;
2591 if (kmalloc_caches_dma[index])
2592 goto unlock_out;
2594 realsize = kmalloc_caches[index].objsize;
2595 text = kasprintf(flags & ~SLUB_DMA, "kmalloc_dma-%d",
2596 (unsigned int)realsize);
2597 s = kmalloc(kmem_size, flags & ~SLUB_DMA);
2599 if (!s || !text || !kmem_cache_open(s, flags, text,
2600 realsize, ARCH_KMALLOC_MINALIGN,
2601 SLAB_CACHE_DMA|__SYSFS_ADD_DEFERRED, NULL)) {
2602 kfree(s);
2603 kfree(text);
2604 goto unlock_out;
2607 list_add(&s->list, &slab_caches);
2608 kmalloc_caches_dma[index] = s;
2610 schedule_work(&sysfs_add_work);
2612 unlock_out:
2613 up_write(&slub_lock);
2614 out:
2615 return kmalloc_caches_dma[index];
2617 #endif
2620 * Conversion table for small slabs sizes / 8 to the index in the
2621 * kmalloc array. This is necessary for slabs < 192 since we have non power
2622 * of two cache sizes there. The size of larger slabs can be determined using
2623 * fls.
2625 static s8 size_index[24] = {
2626 3, /* 8 */
2627 4, /* 16 */
2628 5, /* 24 */
2629 5, /* 32 */
2630 6, /* 40 */
2631 6, /* 48 */
2632 6, /* 56 */
2633 6, /* 64 */
2634 1, /* 72 */
2635 1, /* 80 */
2636 1, /* 88 */
2637 1, /* 96 */
2638 7, /* 104 */
2639 7, /* 112 */
2640 7, /* 120 */
2641 7, /* 128 */
2642 2, /* 136 */
2643 2, /* 144 */
2644 2, /* 152 */
2645 2, /* 160 */
2646 2, /* 168 */
2647 2, /* 176 */
2648 2, /* 184 */
2649 2 /* 192 */
2652 static struct kmem_cache *get_slab(size_t size, gfp_t flags)
2654 int index;
2656 if (size <= 192) {
2657 if (!size)
2658 return ZERO_SIZE_PTR;
2660 index = size_index[(size - 1) / 8];
2661 } else
2662 index = fls(size - 1);
2664 #ifdef CONFIG_ZONE_DMA
2665 if (unlikely((flags & SLUB_DMA)))
2666 return dma_kmalloc_cache(index, flags);
2668 #endif
2669 return &kmalloc_caches[index];
2672 void *__kmalloc(size_t size, gfp_t flags)
2674 struct kmem_cache *s;
2676 if (unlikely(size > PAGE_SIZE))
2677 return kmalloc_large(size, flags);
2679 s = get_slab(size, flags);
2681 if (unlikely(ZERO_OR_NULL_PTR(s)))
2682 return s;
2684 return slab_alloc(s, flags, -1, __builtin_return_address(0));
2686 EXPORT_SYMBOL(__kmalloc);
2688 static void *kmalloc_large_node(size_t size, gfp_t flags, int node)
2690 struct page *page = alloc_pages_node(node, flags | __GFP_COMP,
2691 get_order(size));
2693 if (page)
2694 return page_address(page);
2695 else
2696 return NULL;
2699 #ifdef CONFIG_NUMA
2700 void *__kmalloc_node(size_t size, gfp_t flags, int node)
2702 struct kmem_cache *s;
2704 if (unlikely(size > PAGE_SIZE))
2705 return kmalloc_large_node(size, flags, node);
2707 s = get_slab(size, flags);
2709 if (unlikely(ZERO_OR_NULL_PTR(s)))
2710 return s;
2712 return slab_alloc(s, flags, node, __builtin_return_address(0));
2714 EXPORT_SYMBOL(__kmalloc_node);
2715 #endif
2717 size_t ksize(const void *object)
2719 struct page *page;
2720 struct kmem_cache *s;
2722 if (unlikely(object == ZERO_SIZE_PTR))
2723 return 0;
2725 page = virt_to_head_page(object);
2727 if (unlikely(!PageSlab(page)))
2728 return PAGE_SIZE << compound_order(page);
2730 s = page->slab;
2732 #ifdef CONFIG_SLUB_DEBUG
2734 * Debugging requires use of the padding between object
2735 * and whatever may come after it.
2737 if (s->flags & (SLAB_RED_ZONE | SLAB_POISON))
2738 return s->objsize;
2740 #endif
2742 * If we have the need to store the freelist pointer
2743 * back there or track user information then we can
2744 * only use the space before that information.
2746 if (s->flags & (SLAB_DESTROY_BY_RCU | SLAB_STORE_USER))
2747 return s->inuse;
2749 * Else we can use all the padding etc for the allocation
2751 return s->size;
2753 EXPORT_SYMBOL(ksize);
2755 void kfree(const void *x)
2757 struct page *page;
2758 void *object = (void *)x;
2760 if (unlikely(ZERO_OR_NULL_PTR(x)))
2761 return;
2763 page = virt_to_head_page(x);
2764 if (unlikely(!PageSlab(page))) {
2765 put_page(page);
2766 return;
2768 slab_free(page->slab, page, object, __builtin_return_address(0));
2770 EXPORT_SYMBOL(kfree);
2773 * kmem_cache_shrink removes empty slabs from the partial lists and sorts
2774 * the remaining slabs by the number of items in use. The slabs with the
2775 * most items in use come first. New allocations will then fill those up
2776 * and thus they can be removed from the partial lists.
2778 * The slabs with the least items are placed last. This results in them
2779 * being allocated from last increasing the chance that the last objects
2780 * are freed in them.
2782 int kmem_cache_shrink(struct kmem_cache *s)
2784 int node;
2785 int i;
2786 struct kmem_cache_node *n;
2787 struct page *page;
2788 struct page *t;
2789 int objects = oo_objects(s->max);
2790 struct list_head *slabs_by_inuse =
2791 kmalloc(sizeof(struct list_head) * objects, GFP_KERNEL);
2792 unsigned long flags;
2794 if (!slabs_by_inuse)
2795 return -ENOMEM;
2797 flush_all(s);
2798 for_each_node_state(node, N_NORMAL_MEMORY) {
2799 n = get_node(s, node);
2801 if (!n->nr_partial)
2802 continue;
2804 for (i = 0; i < objects; i++)
2805 INIT_LIST_HEAD(slabs_by_inuse + i);
2807 spin_lock_irqsave(&n->list_lock, flags);
2810 * Build lists indexed by the items in use in each slab.
2812 * Note that concurrent frees may occur while we hold the
2813 * list_lock. page->inuse here is the upper limit.
2815 list_for_each_entry_safe(page, t, &n->partial, lru) {
2816 if (!page->inuse && slab_trylock(page)) {
2818 * Must hold slab lock here because slab_free
2819 * may have freed the last object and be
2820 * waiting to release the slab.
2822 list_del(&page->lru);
2823 n->nr_partial--;
2824 slab_unlock(page);
2825 discard_slab(s, page);
2826 } else {
2827 list_move(&page->lru,
2828 slabs_by_inuse + page->inuse);
2833 * Rebuild the partial list with the slabs filled up most
2834 * first and the least used slabs at the end.
2836 for (i = objects - 1; i >= 0; i--)
2837 list_splice(slabs_by_inuse + i, n->partial.prev);
2839 spin_unlock_irqrestore(&n->list_lock, flags);
2842 kfree(slabs_by_inuse);
2843 return 0;
2845 EXPORT_SYMBOL(kmem_cache_shrink);
2847 #if defined(CONFIG_NUMA) && defined(CONFIG_MEMORY_HOTPLUG)
2848 static int slab_mem_going_offline_callback(void *arg)
2850 struct kmem_cache *s;
2852 down_read(&slub_lock);
2853 list_for_each_entry(s, &slab_caches, list)
2854 kmem_cache_shrink(s);
2855 up_read(&slub_lock);
2857 return 0;
2860 static void slab_mem_offline_callback(void *arg)
2862 struct kmem_cache_node *n;
2863 struct kmem_cache *s;
2864 struct memory_notify *marg = arg;
2865 int offline_node;
2867 offline_node = marg->status_change_nid;
2870 * If the node still has available memory. we need kmem_cache_node
2871 * for it yet.
2873 if (offline_node < 0)
2874 return;
2876 down_read(&slub_lock);
2877 list_for_each_entry(s, &slab_caches, list) {
2878 n = get_node(s, offline_node);
2879 if (n) {
2881 * if n->nr_slabs > 0, slabs still exist on the node
2882 * that is going down. We were unable to free them,
2883 * and offline_pages() function shoudn't call this
2884 * callback. So, we must fail.
2886 BUG_ON(slabs_node(s, offline_node));
2888 s->node[offline_node] = NULL;
2889 kmem_cache_free(kmalloc_caches, n);
2892 up_read(&slub_lock);
2895 static int slab_mem_going_online_callback(void *arg)
2897 struct kmem_cache_node *n;
2898 struct kmem_cache *s;
2899 struct memory_notify *marg = arg;
2900 int nid = marg->status_change_nid;
2901 int ret = 0;
2904 * If the node's memory is already available, then kmem_cache_node is
2905 * already created. Nothing to do.
2907 if (nid < 0)
2908 return 0;
2911 * We are bringing a node online. No memory is availabe yet. We must
2912 * allocate a kmem_cache_node structure in order to bring the node
2913 * online.
2915 down_read(&slub_lock);
2916 list_for_each_entry(s, &slab_caches, list) {
2918 * XXX: kmem_cache_alloc_node will fallback to other nodes
2919 * since memory is not yet available from the node that
2920 * is brought up.
2922 n = kmem_cache_alloc(kmalloc_caches, GFP_KERNEL);
2923 if (!n) {
2924 ret = -ENOMEM;
2925 goto out;
2927 init_kmem_cache_node(n);
2928 s->node[nid] = n;
2930 out:
2931 up_read(&slub_lock);
2932 return ret;
2935 static int slab_memory_callback(struct notifier_block *self,
2936 unsigned long action, void *arg)
2938 int ret = 0;
2940 switch (action) {
2941 case MEM_GOING_ONLINE:
2942 ret = slab_mem_going_online_callback(arg);
2943 break;
2944 case MEM_GOING_OFFLINE:
2945 ret = slab_mem_going_offline_callback(arg);
2946 break;
2947 case MEM_OFFLINE:
2948 case MEM_CANCEL_ONLINE:
2949 slab_mem_offline_callback(arg);
2950 break;
2951 case MEM_ONLINE:
2952 case MEM_CANCEL_OFFLINE:
2953 break;
2956 ret = notifier_from_errno(ret);
2957 return ret;
2960 #endif /* CONFIG_MEMORY_HOTPLUG */
2962 /********************************************************************
2963 * Basic setup of slabs
2964 *******************************************************************/
2966 void __init kmem_cache_init(void)
2968 int i;
2969 int caches = 0;
2971 init_alloc_cpu();
2973 #ifdef CONFIG_NUMA
2975 * Must first have the slab cache available for the allocations of the
2976 * struct kmem_cache_node's. There is special bootstrap code in
2977 * kmem_cache_open for slab_state == DOWN.
2979 create_kmalloc_cache(&kmalloc_caches[0], "kmem_cache_node",
2980 sizeof(struct kmem_cache_node), GFP_KERNEL);
2981 kmalloc_caches[0].refcount = -1;
2982 caches++;
2984 hotplug_memory_notifier(slab_memory_callback, SLAB_CALLBACK_PRI);
2985 #endif
2987 /* Able to allocate the per node structures */
2988 slab_state = PARTIAL;
2990 /* Caches that are not of the two-to-the-power-of size */
2991 if (KMALLOC_MIN_SIZE <= 64) {
2992 create_kmalloc_cache(&kmalloc_caches[1],
2993 "kmalloc-96", 96, GFP_KERNEL);
2994 caches++;
2996 if (KMALLOC_MIN_SIZE <= 128) {
2997 create_kmalloc_cache(&kmalloc_caches[2],
2998 "kmalloc-192", 192, GFP_KERNEL);
2999 caches++;
3002 for (i = KMALLOC_SHIFT_LOW; i <= PAGE_SHIFT; i++) {
3003 create_kmalloc_cache(&kmalloc_caches[i],
3004 "kmalloc", 1 << i, GFP_KERNEL);
3005 caches++;
3010 * Patch up the size_index table if we have strange large alignment
3011 * requirements for the kmalloc array. This is only the case for
3012 * MIPS it seems. The standard arches will not generate any code here.
3014 * Largest permitted alignment is 256 bytes due to the way we
3015 * handle the index determination for the smaller caches.
3017 * Make sure that nothing crazy happens if someone starts tinkering
3018 * around with ARCH_KMALLOC_MINALIGN
3020 BUILD_BUG_ON(KMALLOC_MIN_SIZE > 256 ||
3021 (KMALLOC_MIN_SIZE & (KMALLOC_MIN_SIZE - 1)));
3023 for (i = 8; i < KMALLOC_MIN_SIZE; i += 8)
3024 size_index[(i - 1) / 8] = KMALLOC_SHIFT_LOW;
3026 slab_state = UP;
3028 /* Provide the correct kmalloc names now that the caches are up */
3029 for (i = KMALLOC_SHIFT_LOW; i <= PAGE_SHIFT; i++)
3030 kmalloc_caches[i]. name =
3031 kasprintf(GFP_KERNEL, "kmalloc-%d", 1 << i);
3033 #ifdef CONFIG_SMP
3034 register_cpu_notifier(&slab_notifier);
3035 kmem_size = offsetof(struct kmem_cache, cpu_slab) +
3036 nr_cpu_ids * sizeof(struct kmem_cache_cpu *);
3037 #else
3038 kmem_size = sizeof(struct kmem_cache);
3039 #endif
3041 printk(KERN_INFO
3042 "SLUB: Genslabs=%d, HWalign=%d, Order=%d-%d, MinObjects=%d,"
3043 " CPUs=%d, Nodes=%d\n",
3044 caches, cache_line_size(),
3045 slub_min_order, slub_max_order, slub_min_objects,
3046 nr_cpu_ids, nr_node_ids);
3050 * Find a mergeable slab cache
3052 static int slab_unmergeable(struct kmem_cache *s)
3054 if (slub_nomerge || (s->flags & SLUB_NEVER_MERGE))
3055 return 1;
3057 if (s->ctor)
3058 return 1;
3061 * We may have set a slab to be unmergeable during bootstrap.
3063 if (s->refcount < 0)
3064 return 1;
3066 return 0;
3069 static struct kmem_cache *find_mergeable(size_t size,
3070 size_t align, unsigned long flags, const char *name,
3071 void (*ctor)(struct kmem_cache *, void *))
3073 struct kmem_cache *s;
3075 if (slub_nomerge || (flags & SLUB_NEVER_MERGE))
3076 return NULL;
3078 if (ctor)
3079 return NULL;
3081 size = ALIGN(size, sizeof(void *));
3082 align = calculate_alignment(flags, align, size);
3083 size = ALIGN(size, align);
3084 flags = kmem_cache_flags(size, flags, name, NULL);
3086 list_for_each_entry(s, &slab_caches, list) {
3087 if (slab_unmergeable(s))
3088 continue;
3090 if (size > s->size)
3091 continue;
3093 if ((flags & SLUB_MERGE_SAME) != (s->flags & SLUB_MERGE_SAME))
3094 continue;
3096 * Check if alignment is compatible.
3097 * Courtesy of Adrian Drzewiecki
3099 if ((s->size & ~(align - 1)) != s->size)
3100 continue;
3102 if (s->size - size >= sizeof(void *))
3103 continue;
3105 return s;
3107 return NULL;
3110 struct kmem_cache *kmem_cache_create(const char *name, size_t size,
3111 size_t align, unsigned long flags,
3112 void (*ctor)(struct kmem_cache *, void *))
3114 struct kmem_cache *s;
3116 down_write(&slub_lock);
3117 s = find_mergeable(size, align, flags, name, ctor);
3118 if (s) {
3119 int cpu;
3121 s->refcount++;
3123 * Adjust the object sizes so that we clear
3124 * the complete object on kzalloc.
3126 s->objsize = max(s->objsize, (int)size);
3129 * And then we need to update the object size in the
3130 * per cpu structures
3132 for_each_online_cpu(cpu)
3133 get_cpu_slab(s, cpu)->objsize = s->objsize;
3135 s->inuse = max_t(int, s->inuse, ALIGN(size, sizeof(void *)));
3136 up_write(&slub_lock);
3138 if (sysfs_slab_alias(s, name))
3139 goto err;
3140 return s;
3143 s = kmalloc(kmem_size, GFP_KERNEL);
3144 if (s) {
3145 if (kmem_cache_open(s, GFP_KERNEL, name,
3146 size, align, flags, ctor)) {
3147 list_add(&s->list, &slab_caches);
3148 up_write(&slub_lock);
3149 if (sysfs_slab_add(s))
3150 goto err;
3151 return s;
3153 kfree(s);
3155 up_write(&slub_lock);
3157 err:
3158 if (flags & SLAB_PANIC)
3159 panic("Cannot create slabcache %s\n", name);
3160 else
3161 s = NULL;
3162 return s;
3164 EXPORT_SYMBOL(kmem_cache_create);
3166 #ifdef CONFIG_SMP
3168 * Use the cpu notifier to insure that the cpu slabs are flushed when
3169 * necessary.
3171 static int __cpuinit slab_cpuup_callback(struct notifier_block *nfb,
3172 unsigned long action, void *hcpu)
3174 long cpu = (long)hcpu;
3175 struct kmem_cache *s;
3176 unsigned long flags;
3178 switch (action) {
3179 case CPU_UP_PREPARE:
3180 case CPU_UP_PREPARE_FROZEN:
3181 init_alloc_cpu_cpu(cpu);
3182 down_read(&slub_lock);
3183 list_for_each_entry(s, &slab_caches, list)
3184 s->cpu_slab[cpu] = alloc_kmem_cache_cpu(s, cpu,
3185 GFP_KERNEL);
3186 up_read(&slub_lock);
3187 break;
3189 case CPU_UP_CANCELED:
3190 case CPU_UP_CANCELED_FROZEN:
3191 case CPU_DEAD:
3192 case CPU_DEAD_FROZEN:
3193 down_read(&slub_lock);
3194 list_for_each_entry(s, &slab_caches, list) {
3195 struct kmem_cache_cpu *c = get_cpu_slab(s, cpu);
3197 local_irq_save(flags);
3198 __flush_cpu_slab(s, cpu);
3199 local_irq_restore(flags);
3200 free_kmem_cache_cpu(c, cpu);
3201 s->cpu_slab[cpu] = NULL;
3203 up_read(&slub_lock);
3204 break;
3205 default:
3206 break;
3208 return NOTIFY_OK;
3211 static struct notifier_block __cpuinitdata slab_notifier = {
3212 .notifier_call = slab_cpuup_callback
3215 #endif
3217 void *__kmalloc_track_caller(size_t size, gfp_t gfpflags, void *caller)
3219 struct kmem_cache *s;
3221 if (unlikely(size > PAGE_SIZE))
3222 return kmalloc_large(size, gfpflags);
3224 s = get_slab(size, gfpflags);
3226 if (unlikely(ZERO_OR_NULL_PTR(s)))
3227 return s;
3229 return slab_alloc(s, gfpflags, -1, caller);
3232 void *__kmalloc_node_track_caller(size_t size, gfp_t gfpflags,
3233 int node, void *caller)
3235 struct kmem_cache *s;
3237 if (unlikely(size > PAGE_SIZE))
3238 return kmalloc_large_node(size, gfpflags, node);
3240 s = get_slab(size, gfpflags);
3242 if (unlikely(ZERO_OR_NULL_PTR(s)))
3243 return s;
3245 return slab_alloc(s, gfpflags, node, caller);
3248 #if (defined(CONFIG_SYSFS) && defined(CONFIG_SLUB_DEBUG)) || defined(CONFIG_SLABINFO)
3249 static unsigned long count_partial(struct kmem_cache_node *n,
3250 int (*get_count)(struct page *))
3252 unsigned long flags;
3253 unsigned long x = 0;
3254 struct page *page;
3256 spin_lock_irqsave(&n->list_lock, flags);
3257 list_for_each_entry(page, &n->partial, lru)
3258 x += get_count(page);
3259 spin_unlock_irqrestore(&n->list_lock, flags);
3260 return x;
3263 static int count_inuse(struct page *page)
3265 return page->inuse;
3268 static int count_total(struct page *page)
3270 return page->objects;
3273 static int count_free(struct page *page)
3275 return page->objects - page->inuse;
3277 #endif
3279 #if defined(CONFIG_SYSFS) && defined(CONFIG_SLUB_DEBUG)
3280 static int validate_slab(struct kmem_cache *s, struct page *page,
3281 unsigned long *map)
3283 void *p;
3284 void *addr = page_address(page);
3286 if (!check_slab(s, page) ||
3287 !on_freelist(s, page, NULL))
3288 return 0;
3290 /* Now we know that a valid freelist exists */
3291 bitmap_zero(map, page->objects);
3293 for_each_free_object(p, s, page->freelist) {
3294 set_bit(slab_index(p, s, addr), map);
3295 if (!check_object(s, page, p, 0))
3296 return 0;
3299 for_each_object(p, s, addr, page->objects)
3300 if (!test_bit(slab_index(p, s, addr), map))
3301 if (!check_object(s, page, p, 1))
3302 return 0;
3303 return 1;
3306 static void validate_slab_slab(struct kmem_cache *s, struct page *page,
3307 unsigned long *map)
3309 if (slab_trylock(page)) {
3310 validate_slab(s, page, map);
3311 slab_unlock(page);
3312 } else
3313 printk(KERN_INFO "SLUB %s: Skipped busy slab 0x%p\n",
3314 s->name, page);
3316 if (s->flags & DEBUG_DEFAULT_FLAGS) {
3317 if (!SlabDebug(page))
3318 printk(KERN_ERR "SLUB %s: SlabDebug not set "
3319 "on slab 0x%p\n", s->name, page);
3320 } else {
3321 if (SlabDebug(page))
3322 printk(KERN_ERR "SLUB %s: SlabDebug set on "
3323 "slab 0x%p\n", s->name, page);
3327 static int validate_slab_node(struct kmem_cache *s,
3328 struct kmem_cache_node *n, unsigned long *map)
3330 unsigned long count = 0;
3331 struct page *page;
3332 unsigned long flags;
3334 spin_lock_irqsave(&n->list_lock, flags);
3336 list_for_each_entry(page, &n->partial, lru) {
3337 validate_slab_slab(s, page, map);
3338 count++;
3340 if (count != n->nr_partial)
3341 printk(KERN_ERR "SLUB %s: %ld partial slabs counted but "
3342 "counter=%ld\n", s->name, count, n->nr_partial);
3344 if (!(s->flags & SLAB_STORE_USER))
3345 goto out;
3347 list_for_each_entry(page, &n->full, lru) {
3348 validate_slab_slab(s, page, map);
3349 count++;
3351 if (count != atomic_long_read(&n->nr_slabs))
3352 printk(KERN_ERR "SLUB: %s %ld slabs counted but "
3353 "counter=%ld\n", s->name, count,
3354 atomic_long_read(&n->nr_slabs));
3356 out:
3357 spin_unlock_irqrestore(&n->list_lock, flags);
3358 return count;
3361 static long validate_slab_cache(struct kmem_cache *s)
3363 int node;
3364 unsigned long count = 0;
3365 unsigned long *map = kmalloc(BITS_TO_LONGS(oo_objects(s->max)) *
3366 sizeof(unsigned long), GFP_KERNEL);
3368 if (!map)
3369 return -ENOMEM;
3371 flush_all(s);
3372 for_each_node_state(node, N_NORMAL_MEMORY) {
3373 struct kmem_cache_node *n = get_node(s, node);
3375 count += validate_slab_node(s, n, map);
3377 kfree(map);
3378 return count;
3381 #ifdef SLUB_RESILIENCY_TEST
3382 static void resiliency_test(void)
3384 u8 *p;
3386 printk(KERN_ERR "SLUB resiliency testing\n");
3387 printk(KERN_ERR "-----------------------\n");
3388 printk(KERN_ERR "A. Corruption after allocation\n");
3390 p = kzalloc(16, GFP_KERNEL);
3391 p[16] = 0x12;
3392 printk(KERN_ERR "\n1. kmalloc-16: Clobber Redzone/next pointer"
3393 " 0x12->0x%p\n\n", p + 16);
3395 validate_slab_cache(kmalloc_caches + 4);
3397 /* Hmmm... The next two are dangerous */
3398 p = kzalloc(32, GFP_KERNEL);
3399 p[32 + sizeof(void *)] = 0x34;
3400 printk(KERN_ERR "\n2. kmalloc-32: Clobber next pointer/next slab"
3401 " 0x34 -> -0x%p\n", p);
3402 printk(KERN_ERR
3403 "If allocated object is overwritten then not detectable\n\n");
3405 validate_slab_cache(kmalloc_caches + 5);
3406 p = kzalloc(64, GFP_KERNEL);
3407 p += 64 + (get_cycles() & 0xff) * sizeof(void *);
3408 *p = 0x56;
3409 printk(KERN_ERR "\n3. kmalloc-64: corrupting random byte 0x56->0x%p\n",
3411 printk(KERN_ERR
3412 "If allocated object is overwritten then not detectable\n\n");
3413 validate_slab_cache(kmalloc_caches + 6);
3415 printk(KERN_ERR "\nB. Corruption after free\n");
3416 p = kzalloc(128, GFP_KERNEL);
3417 kfree(p);
3418 *p = 0x78;
3419 printk(KERN_ERR "1. kmalloc-128: Clobber first word 0x78->0x%p\n\n", p);
3420 validate_slab_cache(kmalloc_caches + 7);
3422 p = kzalloc(256, GFP_KERNEL);
3423 kfree(p);
3424 p[50] = 0x9a;
3425 printk(KERN_ERR "\n2. kmalloc-256: Clobber 50th byte 0x9a->0x%p\n\n",
3427 validate_slab_cache(kmalloc_caches + 8);
3429 p = kzalloc(512, GFP_KERNEL);
3430 kfree(p);
3431 p[512] = 0xab;
3432 printk(KERN_ERR "\n3. kmalloc-512: Clobber redzone 0xab->0x%p\n\n", p);
3433 validate_slab_cache(kmalloc_caches + 9);
3435 #else
3436 static void resiliency_test(void) {};
3437 #endif
3440 * Generate lists of code addresses where slabcache objects are allocated
3441 * and freed.
3444 struct location {
3445 unsigned long count;
3446 void *addr;
3447 long long sum_time;
3448 long min_time;
3449 long max_time;
3450 long min_pid;
3451 long max_pid;
3452 cpumask_t cpus;
3453 nodemask_t nodes;
3456 struct loc_track {
3457 unsigned long max;
3458 unsigned long count;
3459 struct location *loc;
3462 static void free_loc_track(struct loc_track *t)
3464 if (t->max)
3465 free_pages((unsigned long)t->loc,
3466 get_order(sizeof(struct location) * t->max));
3469 static int alloc_loc_track(struct loc_track *t, unsigned long max, gfp_t flags)
3471 struct location *l;
3472 int order;
3474 order = get_order(sizeof(struct location) * max);
3476 l = (void *)__get_free_pages(flags, order);
3477 if (!l)
3478 return 0;
3480 if (t->count) {
3481 memcpy(l, t->loc, sizeof(struct location) * t->count);
3482 free_loc_track(t);
3484 t->max = max;
3485 t->loc = l;
3486 return 1;
3489 static int add_location(struct loc_track *t, struct kmem_cache *s,
3490 const struct track *track)
3492 long start, end, pos;
3493 struct location *l;
3494 void *caddr;
3495 unsigned long age = jiffies - track->when;
3497 start = -1;
3498 end = t->count;
3500 for ( ; ; ) {
3501 pos = start + (end - start + 1) / 2;
3504 * There is nothing at "end". If we end up there
3505 * we need to add something to before end.
3507 if (pos == end)
3508 break;
3510 caddr = t->loc[pos].addr;
3511 if (track->addr == caddr) {
3513 l = &t->loc[pos];
3514 l->count++;
3515 if (track->when) {
3516 l->sum_time += age;
3517 if (age < l->min_time)
3518 l->min_time = age;
3519 if (age > l->max_time)
3520 l->max_time = age;
3522 if (track->pid < l->min_pid)
3523 l->min_pid = track->pid;
3524 if (track->pid > l->max_pid)
3525 l->max_pid = track->pid;
3527 cpu_set(track->cpu, l->cpus);
3529 node_set(page_to_nid(virt_to_page(track)), l->nodes);
3530 return 1;
3533 if (track->addr < caddr)
3534 end = pos;
3535 else
3536 start = pos;
3540 * Not found. Insert new tracking element.
3542 if (t->count >= t->max && !alloc_loc_track(t, 2 * t->max, GFP_ATOMIC))
3543 return 0;
3545 l = t->loc + pos;
3546 if (pos < t->count)
3547 memmove(l + 1, l,
3548 (t->count - pos) * sizeof(struct location));
3549 t->count++;
3550 l->count = 1;
3551 l->addr = track->addr;
3552 l->sum_time = age;
3553 l->min_time = age;
3554 l->max_time = age;
3555 l->min_pid = track->pid;
3556 l->max_pid = track->pid;
3557 cpus_clear(l->cpus);
3558 cpu_set(track->cpu, l->cpus);
3559 nodes_clear(l->nodes);
3560 node_set(page_to_nid(virt_to_page(track)), l->nodes);
3561 return 1;
3564 static void process_slab(struct loc_track *t, struct kmem_cache *s,
3565 struct page *page, enum track_item alloc)
3567 void *addr = page_address(page);
3568 DECLARE_BITMAP(map, page->objects);
3569 void *p;
3571 bitmap_zero(map, page->objects);
3572 for_each_free_object(p, s, page->freelist)
3573 set_bit(slab_index(p, s, addr), map);
3575 for_each_object(p, s, addr, page->objects)
3576 if (!test_bit(slab_index(p, s, addr), map))
3577 add_location(t, s, get_track(s, p, alloc));
3580 static int list_locations(struct kmem_cache *s, char *buf,
3581 enum track_item alloc)
3583 int len = 0;
3584 unsigned long i;
3585 struct loc_track t = { 0, 0, NULL };
3586 int node;
3588 if (!alloc_loc_track(&t, PAGE_SIZE / sizeof(struct location),
3589 GFP_TEMPORARY))
3590 return sprintf(buf, "Out of memory\n");
3592 /* Push back cpu slabs */
3593 flush_all(s);
3595 for_each_node_state(node, N_NORMAL_MEMORY) {
3596 struct kmem_cache_node *n = get_node(s, node);
3597 unsigned long flags;
3598 struct page *page;
3600 if (!atomic_long_read(&n->nr_slabs))
3601 continue;
3603 spin_lock_irqsave(&n->list_lock, flags);
3604 list_for_each_entry(page, &n->partial, lru)
3605 process_slab(&t, s, page, alloc);
3606 list_for_each_entry(page, &n->full, lru)
3607 process_slab(&t, s, page, alloc);
3608 spin_unlock_irqrestore(&n->list_lock, flags);
3611 for (i = 0; i < t.count; i++) {
3612 struct location *l = &t.loc[i];
3614 if (len > PAGE_SIZE - 100)
3615 break;
3616 len += sprintf(buf + len, "%7ld ", l->count);
3618 if (l->addr)
3619 len += sprint_symbol(buf + len, (unsigned long)l->addr);
3620 else
3621 len += sprintf(buf + len, "<not-available>");
3623 if (l->sum_time != l->min_time) {
3624 unsigned long remainder;
3626 len += sprintf(buf + len, " age=%ld/%ld/%ld",
3627 l->min_time,
3628 div_long_long_rem(l->sum_time, l->count, &remainder),
3629 l->max_time);
3630 } else
3631 len += sprintf(buf + len, " age=%ld",
3632 l->min_time);
3634 if (l->min_pid != l->max_pid)
3635 len += sprintf(buf + len, " pid=%ld-%ld",
3636 l->min_pid, l->max_pid);
3637 else
3638 len += sprintf(buf + len, " pid=%ld",
3639 l->min_pid);
3641 if (num_online_cpus() > 1 && !cpus_empty(l->cpus) &&
3642 len < PAGE_SIZE - 60) {
3643 len += sprintf(buf + len, " cpus=");
3644 len += cpulist_scnprintf(buf + len, PAGE_SIZE - len - 50,
3645 l->cpus);
3648 if (num_online_nodes() > 1 && !nodes_empty(l->nodes) &&
3649 len < PAGE_SIZE - 60) {
3650 len += sprintf(buf + len, " nodes=");
3651 len += nodelist_scnprintf(buf + len, PAGE_SIZE - len - 50,
3652 l->nodes);
3655 len += sprintf(buf + len, "\n");
3658 free_loc_track(&t);
3659 if (!t.count)
3660 len += sprintf(buf, "No data\n");
3661 return len;
3664 enum slab_stat_type {
3665 SL_ALL, /* All slabs */
3666 SL_PARTIAL, /* Only partially allocated slabs */
3667 SL_CPU, /* Only slabs used for cpu caches */
3668 SL_OBJECTS, /* Determine allocated objects not slabs */
3669 SL_TOTAL /* Determine object capacity not slabs */
3672 #define SO_ALL (1 << SL_ALL)
3673 #define SO_PARTIAL (1 << SL_PARTIAL)
3674 #define SO_CPU (1 << SL_CPU)
3675 #define SO_OBJECTS (1 << SL_OBJECTS)
3676 #define SO_TOTAL (1 << SL_TOTAL)
3678 static ssize_t show_slab_objects(struct kmem_cache *s,
3679 char *buf, unsigned long flags)
3681 unsigned long total = 0;
3682 int node;
3683 int x;
3684 unsigned long *nodes;
3685 unsigned long *per_cpu;
3687 nodes = kzalloc(2 * sizeof(unsigned long) * nr_node_ids, GFP_KERNEL);
3688 if (!nodes)
3689 return -ENOMEM;
3690 per_cpu = nodes + nr_node_ids;
3692 if (flags & SO_CPU) {
3693 int cpu;
3695 for_each_possible_cpu(cpu) {
3696 struct kmem_cache_cpu *c = get_cpu_slab(s, cpu);
3698 if (!c || c->node < 0)
3699 continue;
3701 if (c->page) {
3702 if (flags & SO_TOTAL)
3703 x = c->page->objects;
3704 else if (flags & SO_OBJECTS)
3705 x = c->page->inuse;
3706 else
3707 x = 1;
3709 total += x;
3710 nodes[c->node] += x;
3712 per_cpu[c->node]++;
3716 if (flags & SO_ALL) {
3717 for_each_node_state(node, N_NORMAL_MEMORY) {
3718 struct kmem_cache_node *n = get_node(s, node);
3720 if (flags & SO_TOTAL)
3721 x = atomic_long_read(&n->total_objects);
3722 else if (flags & SO_OBJECTS)
3723 x = atomic_long_read(&n->total_objects) -
3724 count_partial(n, count_free);
3726 else
3727 x = atomic_long_read(&n->nr_slabs);
3728 total += x;
3729 nodes[node] += x;
3732 } else if (flags & SO_PARTIAL) {
3733 for_each_node_state(node, N_NORMAL_MEMORY) {
3734 struct kmem_cache_node *n = get_node(s, node);
3736 if (flags & SO_TOTAL)
3737 x = count_partial(n, count_total);
3738 else if (flags & SO_OBJECTS)
3739 x = count_partial(n, count_inuse);
3740 else
3741 x = n->nr_partial;
3742 total += x;
3743 nodes[node] += x;
3746 x = sprintf(buf, "%lu", total);
3747 #ifdef CONFIG_NUMA
3748 for_each_node_state(node, N_NORMAL_MEMORY)
3749 if (nodes[node])
3750 x += sprintf(buf + x, " N%d=%lu",
3751 node, nodes[node]);
3752 #endif
3753 kfree(nodes);
3754 return x + sprintf(buf + x, "\n");
3757 static int any_slab_objects(struct kmem_cache *s)
3759 int node;
3761 for_each_online_node(node) {
3762 struct kmem_cache_node *n = get_node(s, node);
3764 if (!n)
3765 continue;
3767 if (atomic_read(&n->total_objects))
3768 return 1;
3770 return 0;
3773 #define to_slab_attr(n) container_of(n, struct slab_attribute, attr)
3774 #define to_slab(n) container_of(n, struct kmem_cache, kobj);
3776 struct slab_attribute {
3777 struct attribute attr;
3778 ssize_t (*show)(struct kmem_cache *s, char *buf);
3779 ssize_t (*store)(struct kmem_cache *s, const char *x, size_t count);
3782 #define SLAB_ATTR_RO(_name) \
3783 static struct slab_attribute _name##_attr = __ATTR_RO(_name)
3785 #define SLAB_ATTR(_name) \
3786 static struct slab_attribute _name##_attr = \
3787 __ATTR(_name, 0644, _name##_show, _name##_store)
3789 static ssize_t slab_size_show(struct kmem_cache *s, char *buf)
3791 return sprintf(buf, "%d\n", s->size);
3793 SLAB_ATTR_RO(slab_size);
3795 static ssize_t align_show(struct kmem_cache *s, char *buf)
3797 return sprintf(buf, "%d\n", s->align);
3799 SLAB_ATTR_RO(align);
3801 static ssize_t object_size_show(struct kmem_cache *s, char *buf)
3803 return sprintf(buf, "%d\n", s->objsize);
3805 SLAB_ATTR_RO(object_size);
3807 static ssize_t objs_per_slab_show(struct kmem_cache *s, char *buf)
3809 return sprintf(buf, "%d\n", oo_objects(s->oo));
3811 SLAB_ATTR_RO(objs_per_slab);
3813 static ssize_t order_store(struct kmem_cache *s,
3814 const char *buf, size_t length)
3816 int order = simple_strtoul(buf, NULL, 10);
3818 if (order > slub_max_order || order < slub_min_order)
3819 return -EINVAL;
3821 calculate_sizes(s, order);
3822 return length;
3825 static ssize_t order_show(struct kmem_cache *s, char *buf)
3827 return sprintf(buf, "%d\n", oo_order(s->oo));
3829 SLAB_ATTR(order);
3831 static ssize_t ctor_show(struct kmem_cache *s, char *buf)
3833 if (s->ctor) {
3834 int n = sprint_symbol(buf, (unsigned long)s->ctor);
3836 return n + sprintf(buf + n, "\n");
3838 return 0;
3840 SLAB_ATTR_RO(ctor);
3842 static ssize_t aliases_show(struct kmem_cache *s, char *buf)
3844 return sprintf(buf, "%d\n", s->refcount - 1);
3846 SLAB_ATTR_RO(aliases);
3848 static ssize_t slabs_show(struct kmem_cache *s, char *buf)
3850 return show_slab_objects(s, buf, SO_ALL);
3852 SLAB_ATTR_RO(slabs);
3854 static ssize_t partial_show(struct kmem_cache *s, char *buf)
3856 return show_slab_objects(s, buf, SO_PARTIAL);
3858 SLAB_ATTR_RO(partial);
3860 static ssize_t cpu_slabs_show(struct kmem_cache *s, char *buf)
3862 return show_slab_objects(s, buf, SO_CPU);
3864 SLAB_ATTR_RO(cpu_slabs);
3866 static ssize_t objects_show(struct kmem_cache *s, char *buf)
3868 return show_slab_objects(s, buf, SO_ALL|SO_OBJECTS);
3870 SLAB_ATTR_RO(objects);
3872 static ssize_t objects_partial_show(struct kmem_cache *s, char *buf)
3874 return show_slab_objects(s, buf, SO_PARTIAL|SO_OBJECTS);
3876 SLAB_ATTR_RO(objects_partial);
3878 static ssize_t total_objects_show(struct kmem_cache *s, char *buf)
3880 return show_slab_objects(s, buf, SO_ALL|SO_TOTAL);
3882 SLAB_ATTR_RO(total_objects);
3884 static ssize_t sanity_checks_show(struct kmem_cache *s, char *buf)
3886 return sprintf(buf, "%d\n", !!(s->flags & SLAB_DEBUG_FREE));
3889 static ssize_t sanity_checks_store(struct kmem_cache *s,
3890 const char *buf, size_t length)
3892 s->flags &= ~SLAB_DEBUG_FREE;
3893 if (buf[0] == '1')
3894 s->flags |= SLAB_DEBUG_FREE;
3895 return length;
3897 SLAB_ATTR(sanity_checks);
3899 static ssize_t trace_show(struct kmem_cache *s, char *buf)
3901 return sprintf(buf, "%d\n", !!(s->flags & SLAB_TRACE));
3904 static ssize_t trace_store(struct kmem_cache *s, const char *buf,
3905 size_t length)
3907 s->flags &= ~SLAB_TRACE;
3908 if (buf[0] == '1')
3909 s->flags |= SLAB_TRACE;
3910 return length;
3912 SLAB_ATTR(trace);
3914 static ssize_t reclaim_account_show(struct kmem_cache *s, char *buf)
3916 return sprintf(buf, "%d\n", !!(s->flags & SLAB_RECLAIM_ACCOUNT));
3919 static ssize_t reclaim_account_store(struct kmem_cache *s,
3920 const char *buf, size_t length)
3922 s->flags &= ~SLAB_RECLAIM_ACCOUNT;
3923 if (buf[0] == '1')
3924 s->flags |= SLAB_RECLAIM_ACCOUNT;
3925 return length;
3927 SLAB_ATTR(reclaim_account);
3929 static ssize_t hwcache_align_show(struct kmem_cache *s, char *buf)
3931 return sprintf(buf, "%d\n", !!(s->flags & SLAB_HWCACHE_ALIGN));
3933 SLAB_ATTR_RO(hwcache_align);
3935 #ifdef CONFIG_ZONE_DMA
3936 static ssize_t cache_dma_show(struct kmem_cache *s, char *buf)
3938 return sprintf(buf, "%d\n", !!(s->flags & SLAB_CACHE_DMA));
3940 SLAB_ATTR_RO(cache_dma);
3941 #endif
3943 static ssize_t destroy_by_rcu_show(struct kmem_cache *s, char *buf)
3945 return sprintf(buf, "%d\n", !!(s->flags & SLAB_DESTROY_BY_RCU));
3947 SLAB_ATTR_RO(destroy_by_rcu);
3949 static ssize_t red_zone_show(struct kmem_cache *s, char *buf)
3951 return sprintf(buf, "%d\n", !!(s->flags & SLAB_RED_ZONE));
3954 static ssize_t red_zone_store(struct kmem_cache *s,
3955 const char *buf, size_t length)
3957 if (any_slab_objects(s))
3958 return -EBUSY;
3960 s->flags &= ~SLAB_RED_ZONE;
3961 if (buf[0] == '1')
3962 s->flags |= SLAB_RED_ZONE;
3963 calculate_sizes(s, -1);
3964 return length;
3966 SLAB_ATTR(red_zone);
3968 static ssize_t poison_show(struct kmem_cache *s, char *buf)
3970 return sprintf(buf, "%d\n", !!(s->flags & SLAB_POISON));
3973 static ssize_t poison_store(struct kmem_cache *s,
3974 const char *buf, size_t length)
3976 if (any_slab_objects(s))
3977 return -EBUSY;
3979 s->flags &= ~SLAB_POISON;
3980 if (buf[0] == '1')
3981 s->flags |= SLAB_POISON;
3982 calculate_sizes(s, -1);
3983 return length;
3985 SLAB_ATTR(poison);
3987 static ssize_t store_user_show(struct kmem_cache *s, char *buf)
3989 return sprintf(buf, "%d\n", !!(s->flags & SLAB_STORE_USER));
3992 static ssize_t store_user_store(struct kmem_cache *s,
3993 const char *buf, size_t length)
3995 if (any_slab_objects(s))
3996 return -EBUSY;
3998 s->flags &= ~SLAB_STORE_USER;
3999 if (buf[0] == '1')
4000 s->flags |= SLAB_STORE_USER;
4001 calculate_sizes(s, -1);
4002 return length;
4004 SLAB_ATTR(store_user);
4006 static ssize_t validate_show(struct kmem_cache *s, char *buf)
4008 return 0;
4011 static ssize_t validate_store(struct kmem_cache *s,
4012 const char *buf, size_t length)
4014 int ret = -EINVAL;
4016 if (buf[0] == '1') {
4017 ret = validate_slab_cache(s);
4018 if (ret >= 0)
4019 ret = length;
4021 return ret;
4023 SLAB_ATTR(validate);
4025 static ssize_t shrink_show(struct kmem_cache *s, char *buf)
4027 return 0;
4030 static ssize_t shrink_store(struct kmem_cache *s,
4031 const char *buf, size_t length)
4033 if (buf[0] == '1') {
4034 int rc = kmem_cache_shrink(s);
4036 if (rc)
4037 return rc;
4038 } else
4039 return -EINVAL;
4040 return length;
4042 SLAB_ATTR(shrink);
4044 static ssize_t alloc_calls_show(struct kmem_cache *s, char *buf)
4046 if (!(s->flags & SLAB_STORE_USER))
4047 return -ENOSYS;
4048 return list_locations(s, buf, TRACK_ALLOC);
4050 SLAB_ATTR_RO(alloc_calls);
4052 static ssize_t free_calls_show(struct kmem_cache *s, char *buf)
4054 if (!(s->flags & SLAB_STORE_USER))
4055 return -ENOSYS;
4056 return list_locations(s, buf, TRACK_FREE);
4058 SLAB_ATTR_RO(free_calls);
4060 #ifdef CONFIG_NUMA
4061 static ssize_t remote_node_defrag_ratio_show(struct kmem_cache *s, char *buf)
4063 return sprintf(buf, "%d\n", s->remote_node_defrag_ratio / 10);
4066 static ssize_t remote_node_defrag_ratio_store(struct kmem_cache *s,
4067 const char *buf, size_t length)
4069 int n = simple_strtoul(buf, NULL, 10);
4071 if (n < 100)
4072 s->remote_node_defrag_ratio = n * 10;
4073 return length;
4075 SLAB_ATTR(remote_node_defrag_ratio);
4076 #endif
4078 #ifdef CONFIG_SLUB_STATS
4079 static int show_stat(struct kmem_cache *s, char *buf, enum stat_item si)
4081 unsigned long sum = 0;
4082 int cpu;
4083 int len;
4084 int *data = kmalloc(nr_cpu_ids * sizeof(int), GFP_KERNEL);
4086 if (!data)
4087 return -ENOMEM;
4089 for_each_online_cpu(cpu) {
4090 unsigned x = get_cpu_slab(s, cpu)->stat[si];
4092 data[cpu] = x;
4093 sum += x;
4096 len = sprintf(buf, "%lu", sum);
4098 #ifdef CONFIG_SMP
4099 for_each_online_cpu(cpu) {
4100 if (data[cpu] && len < PAGE_SIZE - 20)
4101 len += sprintf(buf + len, " C%d=%u", cpu, data[cpu]);
4103 #endif
4104 kfree(data);
4105 return len + sprintf(buf + len, "\n");
4108 #define STAT_ATTR(si, text) \
4109 static ssize_t text##_show(struct kmem_cache *s, char *buf) \
4111 return show_stat(s, buf, si); \
4113 SLAB_ATTR_RO(text); \
4115 STAT_ATTR(ALLOC_FASTPATH, alloc_fastpath);
4116 STAT_ATTR(ALLOC_SLOWPATH, alloc_slowpath);
4117 STAT_ATTR(FREE_FASTPATH, free_fastpath);
4118 STAT_ATTR(FREE_SLOWPATH, free_slowpath);
4119 STAT_ATTR(FREE_FROZEN, free_frozen);
4120 STAT_ATTR(FREE_ADD_PARTIAL, free_add_partial);
4121 STAT_ATTR(FREE_REMOVE_PARTIAL, free_remove_partial);
4122 STAT_ATTR(ALLOC_FROM_PARTIAL, alloc_from_partial);
4123 STAT_ATTR(ALLOC_SLAB, alloc_slab);
4124 STAT_ATTR(ALLOC_REFILL, alloc_refill);
4125 STAT_ATTR(FREE_SLAB, free_slab);
4126 STAT_ATTR(CPUSLAB_FLUSH, cpuslab_flush);
4127 STAT_ATTR(DEACTIVATE_FULL, deactivate_full);
4128 STAT_ATTR(DEACTIVATE_EMPTY, deactivate_empty);
4129 STAT_ATTR(DEACTIVATE_TO_HEAD, deactivate_to_head);
4130 STAT_ATTR(DEACTIVATE_TO_TAIL, deactivate_to_tail);
4131 STAT_ATTR(DEACTIVATE_REMOTE_FREES, deactivate_remote_frees);
4132 STAT_ATTR(ORDER_FALLBACK, order_fallback);
4133 #endif
4135 static struct attribute *slab_attrs[] = {
4136 &slab_size_attr.attr,
4137 &object_size_attr.attr,
4138 &objs_per_slab_attr.attr,
4139 &order_attr.attr,
4140 &objects_attr.attr,
4141 &objects_partial_attr.attr,
4142 &total_objects_attr.attr,
4143 &slabs_attr.attr,
4144 &partial_attr.attr,
4145 &cpu_slabs_attr.attr,
4146 &ctor_attr.attr,
4147 &aliases_attr.attr,
4148 &align_attr.attr,
4149 &sanity_checks_attr.attr,
4150 &trace_attr.attr,
4151 &hwcache_align_attr.attr,
4152 &reclaim_account_attr.attr,
4153 &destroy_by_rcu_attr.attr,
4154 &red_zone_attr.attr,
4155 &poison_attr.attr,
4156 &store_user_attr.attr,
4157 &validate_attr.attr,
4158 &shrink_attr.attr,
4159 &alloc_calls_attr.attr,
4160 &free_calls_attr.attr,
4161 #ifdef CONFIG_ZONE_DMA
4162 &cache_dma_attr.attr,
4163 #endif
4164 #ifdef CONFIG_NUMA
4165 &remote_node_defrag_ratio_attr.attr,
4166 #endif
4167 #ifdef CONFIG_SLUB_STATS
4168 &alloc_fastpath_attr.attr,
4169 &alloc_slowpath_attr.attr,
4170 &free_fastpath_attr.attr,
4171 &free_slowpath_attr.attr,
4172 &free_frozen_attr.attr,
4173 &free_add_partial_attr.attr,
4174 &free_remove_partial_attr.attr,
4175 &alloc_from_partial_attr.attr,
4176 &alloc_slab_attr.attr,
4177 &alloc_refill_attr.attr,
4178 &free_slab_attr.attr,
4179 &cpuslab_flush_attr.attr,
4180 &deactivate_full_attr.attr,
4181 &deactivate_empty_attr.attr,
4182 &deactivate_to_head_attr.attr,
4183 &deactivate_to_tail_attr.attr,
4184 &deactivate_remote_frees_attr.attr,
4185 &order_fallback_attr.attr,
4186 #endif
4187 NULL
4190 static struct attribute_group slab_attr_group = {
4191 .attrs = slab_attrs,
4194 static ssize_t slab_attr_show(struct kobject *kobj,
4195 struct attribute *attr,
4196 char *buf)
4198 struct slab_attribute *attribute;
4199 struct kmem_cache *s;
4200 int err;
4202 attribute = to_slab_attr(attr);
4203 s = to_slab(kobj);
4205 if (!attribute->show)
4206 return -EIO;
4208 err = attribute->show(s, buf);
4210 return err;
4213 static ssize_t slab_attr_store(struct kobject *kobj,
4214 struct attribute *attr,
4215 const char *buf, size_t len)
4217 struct slab_attribute *attribute;
4218 struct kmem_cache *s;
4219 int err;
4221 attribute = to_slab_attr(attr);
4222 s = to_slab(kobj);
4224 if (!attribute->store)
4225 return -EIO;
4227 err = attribute->store(s, buf, len);
4229 return err;
4232 static void kmem_cache_release(struct kobject *kobj)
4234 struct kmem_cache *s = to_slab(kobj);
4236 kfree(s);
4239 static struct sysfs_ops slab_sysfs_ops = {
4240 .show = slab_attr_show,
4241 .store = slab_attr_store,
4244 static struct kobj_type slab_ktype = {
4245 .sysfs_ops = &slab_sysfs_ops,
4246 .release = kmem_cache_release
4249 static int uevent_filter(struct kset *kset, struct kobject *kobj)
4251 struct kobj_type *ktype = get_ktype(kobj);
4253 if (ktype == &slab_ktype)
4254 return 1;
4255 return 0;
4258 static struct kset_uevent_ops slab_uevent_ops = {
4259 .filter = uevent_filter,
4262 static struct kset *slab_kset;
4264 #define ID_STR_LENGTH 64
4266 /* Create a unique string id for a slab cache:
4268 * Format :[flags-]size
4270 static char *create_unique_id(struct kmem_cache *s)
4272 char *name = kmalloc(ID_STR_LENGTH, GFP_KERNEL);
4273 char *p = name;
4275 BUG_ON(!name);
4277 *p++ = ':';
4279 * First flags affecting slabcache operations. We will only
4280 * get here for aliasable slabs so we do not need to support
4281 * too many flags. The flags here must cover all flags that
4282 * are matched during merging to guarantee that the id is
4283 * unique.
4285 if (s->flags & SLAB_CACHE_DMA)
4286 *p++ = 'd';
4287 if (s->flags & SLAB_RECLAIM_ACCOUNT)
4288 *p++ = 'a';
4289 if (s->flags & SLAB_DEBUG_FREE)
4290 *p++ = 'F';
4291 if (p != name + 1)
4292 *p++ = '-';
4293 p += sprintf(p, "%07d", s->size);
4294 BUG_ON(p > name + ID_STR_LENGTH - 1);
4295 return name;
4298 static int sysfs_slab_add(struct kmem_cache *s)
4300 int err;
4301 const char *name;
4302 int unmergeable;
4304 if (slab_state < SYSFS)
4305 /* Defer until later */
4306 return 0;
4308 unmergeable = slab_unmergeable(s);
4309 if (unmergeable) {
4311 * Slabcache can never be merged so we can use the name proper.
4312 * This is typically the case for debug situations. In that
4313 * case we can catch duplicate names easily.
4315 sysfs_remove_link(&slab_kset->kobj, s->name);
4316 name = s->name;
4317 } else {
4319 * Create a unique name for the slab as a target
4320 * for the symlinks.
4322 name = create_unique_id(s);
4325 s->kobj.kset = slab_kset;
4326 err = kobject_init_and_add(&s->kobj, &slab_ktype, NULL, name);
4327 if (err) {
4328 kobject_put(&s->kobj);
4329 return err;
4332 err = sysfs_create_group(&s->kobj, &slab_attr_group);
4333 if (err)
4334 return err;
4335 kobject_uevent(&s->kobj, KOBJ_ADD);
4336 if (!unmergeable) {
4337 /* Setup first alias */
4338 sysfs_slab_alias(s, s->name);
4339 kfree(name);
4341 return 0;
4344 static void sysfs_slab_remove(struct kmem_cache *s)
4346 kobject_uevent(&s->kobj, KOBJ_REMOVE);
4347 kobject_del(&s->kobj);
4348 kobject_put(&s->kobj);
4352 * Need to buffer aliases during bootup until sysfs becomes
4353 * available lest we loose that information.
4355 struct saved_alias {
4356 struct kmem_cache *s;
4357 const char *name;
4358 struct saved_alias *next;
4361 static struct saved_alias *alias_list;
4363 static int sysfs_slab_alias(struct kmem_cache *s, const char *name)
4365 struct saved_alias *al;
4367 if (slab_state == SYSFS) {
4369 * If we have a leftover link then remove it.
4371 sysfs_remove_link(&slab_kset->kobj, name);
4372 return sysfs_create_link(&slab_kset->kobj, &s->kobj, name);
4375 al = kmalloc(sizeof(struct saved_alias), GFP_KERNEL);
4376 if (!al)
4377 return -ENOMEM;
4379 al->s = s;
4380 al->name = name;
4381 al->next = alias_list;
4382 alias_list = al;
4383 return 0;
4386 static int __init slab_sysfs_init(void)
4388 struct kmem_cache *s;
4389 int err;
4391 slab_kset = kset_create_and_add("slab", &slab_uevent_ops, kernel_kobj);
4392 if (!slab_kset) {
4393 printk(KERN_ERR "Cannot register slab subsystem.\n");
4394 return -ENOSYS;
4397 slab_state = SYSFS;
4399 list_for_each_entry(s, &slab_caches, list) {
4400 err = sysfs_slab_add(s);
4401 if (err)
4402 printk(KERN_ERR "SLUB: Unable to add boot slab %s"
4403 " to sysfs\n", s->name);
4406 while (alias_list) {
4407 struct saved_alias *al = alias_list;
4409 alias_list = alias_list->next;
4410 err = sysfs_slab_alias(al->s, al->name);
4411 if (err)
4412 printk(KERN_ERR "SLUB: Unable to add boot slab alias"
4413 " %s to sysfs\n", s->name);
4414 kfree(al);
4417 resiliency_test();
4418 return 0;
4421 __initcall(slab_sysfs_init);
4422 #endif
4425 * The /proc/slabinfo ABI
4427 #ifdef CONFIG_SLABINFO
4429 ssize_t slabinfo_write(struct file *file, const char __user * buffer,
4430 size_t count, loff_t *ppos)
4432 return -EINVAL;
4436 static void print_slabinfo_header(struct seq_file *m)
4438 seq_puts(m, "slabinfo - version: 2.1\n");
4439 seq_puts(m, "# name <active_objs> <num_objs> <objsize> "
4440 "<objperslab> <pagesperslab>");
4441 seq_puts(m, " : tunables <limit> <batchcount> <sharedfactor>");
4442 seq_puts(m, " : slabdata <active_slabs> <num_slabs> <sharedavail>");
4443 seq_putc(m, '\n');
4446 static void *s_start(struct seq_file *m, loff_t *pos)
4448 loff_t n = *pos;
4450 down_read(&slub_lock);
4451 if (!n)
4452 print_slabinfo_header(m);
4454 return seq_list_start(&slab_caches, *pos);
4457 static void *s_next(struct seq_file *m, void *p, loff_t *pos)
4459 return seq_list_next(p, &slab_caches, pos);
4462 static void s_stop(struct seq_file *m, void *p)
4464 up_read(&slub_lock);
4467 static int s_show(struct seq_file *m, void *p)
4469 unsigned long nr_partials = 0;
4470 unsigned long nr_slabs = 0;
4471 unsigned long nr_inuse = 0;
4472 unsigned long nr_objs = 0;
4473 unsigned long nr_free = 0;
4474 struct kmem_cache *s;
4475 int node;
4477 s = list_entry(p, struct kmem_cache, list);
4479 for_each_online_node(node) {
4480 struct kmem_cache_node *n = get_node(s, node);
4482 if (!n)
4483 continue;
4485 nr_partials += n->nr_partial;
4486 nr_slabs += atomic_long_read(&n->nr_slabs);
4487 nr_objs += atomic_long_read(&n->total_objects);
4488 nr_free += count_partial(n, count_free);
4491 nr_inuse = nr_objs - nr_free;
4493 seq_printf(m, "%-17s %6lu %6lu %6u %4u %4d", s->name, nr_inuse,
4494 nr_objs, s->size, oo_objects(s->oo),
4495 (1 << oo_order(s->oo)));
4496 seq_printf(m, " : tunables %4u %4u %4u", 0, 0, 0);
4497 seq_printf(m, " : slabdata %6lu %6lu %6lu", nr_slabs, nr_slabs,
4498 0UL);
4499 seq_putc(m, '\n');
4500 return 0;
4503 const struct seq_operations slabinfo_op = {
4504 .start = s_start,
4505 .next = s_next,
4506 .stop = s_stop,
4507 .show = s_show,
4510 #endif /* CONFIG_SLABINFO */