afs: the AFS RPC op CBGetCapabilities is actually CBTellMeAboutYourself
[linux-2.6/linux-2.6-openrd.git] / mm / slub.c
blobb145e798bf3d477054b0445719522be94ca7e9f8
1 /*
2 * SLUB: A slab allocator that limits cache line use instead of queuing
3 * objects in per cpu and per node lists.
5 * The allocator synchronizes using per slab locks and only
6 * uses a centralized lock to manage a pool of partial slabs.
8 * (C) 2007 SGI, Christoph Lameter <clameter@sgi.com>
9 */
11 #include <linux/mm.h>
12 #include <linux/module.h>
13 #include <linux/bit_spinlock.h>
14 #include <linux/interrupt.h>
15 #include <linux/bitops.h>
16 #include <linux/slab.h>
17 #include <linux/seq_file.h>
18 #include <linux/cpu.h>
19 #include <linux/cpuset.h>
20 #include <linux/mempolicy.h>
21 #include <linux/ctype.h>
22 #include <linux/kallsyms.h>
23 #include <linux/memory.h>
26 * Lock order:
27 * 1. slab_lock(page)
28 * 2. slab->list_lock
30 * The slab_lock protects operations on the object of a particular
31 * slab and its metadata in the page struct. If the slab lock
32 * has been taken then no allocations nor frees can be performed
33 * on the objects in the slab nor can the slab be added or removed
34 * from the partial or full lists since this would mean modifying
35 * the page_struct of the slab.
37 * The list_lock protects the partial and full list on each node and
38 * the partial slab counter. If taken then no new slabs may be added or
39 * removed from the lists nor make the number of partial slabs be modified.
40 * (Note that the total number of slabs is an atomic value that may be
41 * modified without taking the list lock).
43 * The list_lock is a centralized lock and thus we avoid taking it as
44 * much as possible. As long as SLUB does not have to handle partial
45 * slabs, operations can continue without any centralized lock. F.e.
46 * allocating a long series of objects that fill up slabs does not require
47 * the list lock.
49 * The lock order is sometimes inverted when we are trying to get a slab
50 * off a list. We take the list_lock and then look for a page on the list
51 * to use. While we do that objects in the slabs may be freed. We can
52 * only operate on the slab if we have also taken the slab_lock. So we use
53 * a slab_trylock() on the slab. If trylock was successful then no frees
54 * can occur anymore and we can use the slab for allocations etc. If the
55 * slab_trylock() does not succeed then frees are in progress in the slab and
56 * we must stay away from it for a while since we may cause a bouncing
57 * cacheline if we try to acquire the lock. So go onto the next slab.
58 * If all pages are busy then we may allocate a new slab instead of reusing
59 * a partial slab. A new slab has noone operating on it and thus there is
60 * no danger of cacheline contention.
62 * Interrupts are disabled during allocation and deallocation in order to
63 * make the slab allocator safe to use in the context of an irq. In addition
64 * interrupts are disabled to ensure that the processor does not change
65 * while handling per_cpu slabs, due to kernel preemption.
67 * SLUB assigns one slab for allocation to each processor.
68 * Allocations only occur from these slabs called cpu slabs.
70 * Slabs with free elements are kept on a partial list and during regular
71 * operations no list for full slabs is used. If an object in a full slab is
72 * freed then the slab will show up again on the partial lists.
73 * We track full slabs for debugging purposes though because otherwise we
74 * cannot scan all objects.
76 * Slabs are freed when they become empty. Teardown and setup is
77 * minimal so we rely on the page allocators per cpu caches for
78 * fast frees and allocs.
80 * Overloading of page flags that are otherwise used for LRU management.
82 * PageActive The slab is frozen and exempt from list processing.
83 * This means that the slab is dedicated to a purpose
84 * such as satisfying allocations for a specific
85 * processor. Objects may be freed in the slab while
86 * it is frozen but slab_free will then skip the usual
87 * list operations. It is up to the processor holding
88 * the slab to integrate the slab into the slab lists
89 * when the slab is no longer needed.
91 * One use of this flag is to mark slabs that are
92 * used for allocations. Then such a slab becomes a cpu
93 * slab. The cpu slab may be equipped with an additional
94 * freelist that allows lockless access to
95 * free objects in addition to the regular freelist
96 * that requires the slab lock.
98 * PageError Slab requires special handling due to debug
99 * options set. This moves slab handling out of
100 * the fast path and disables lockless freelists.
103 #define FROZEN (1 << PG_active)
105 #ifdef CONFIG_SLUB_DEBUG
106 #define SLABDEBUG (1 << PG_error)
107 #else
108 #define SLABDEBUG 0
109 #endif
111 static inline int SlabFrozen(struct page *page)
113 return page->flags & FROZEN;
116 static inline void SetSlabFrozen(struct page *page)
118 page->flags |= FROZEN;
121 static inline void ClearSlabFrozen(struct page *page)
123 page->flags &= ~FROZEN;
126 static inline int SlabDebug(struct page *page)
128 return page->flags & SLABDEBUG;
131 static inline void SetSlabDebug(struct page *page)
133 page->flags |= SLABDEBUG;
136 static inline void ClearSlabDebug(struct page *page)
138 page->flags &= ~SLABDEBUG;
142 * Issues still to be resolved:
144 * - Support PAGE_ALLOC_DEBUG. Should be easy to do.
146 * - Variable sizing of the per node arrays
149 /* Enable to test recovery from slab corruption on boot */
150 #undef SLUB_RESILIENCY_TEST
153 * Mininum number of partial slabs. These will be left on the partial
154 * lists even if they are empty. kmem_cache_shrink may reclaim them.
156 #define MIN_PARTIAL 5
159 * Maximum number of desirable partial slabs.
160 * The existence of more partial slabs makes kmem_cache_shrink
161 * sort the partial list by the number of objects in the.
163 #define MAX_PARTIAL 10
165 #define DEBUG_DEFAULT_FLAGS (SLAB_DEBUG_FREE | SLAB_RED_ZONE | \
166 SLAB_POISON | SLAB_STORE_USER)
169 * Set of flags that will prevent slab merging
171 #define SLUB_NEVER_MERGE (SLAB_RED_ZONE | SLAB_POISON | SLAB_STORE_USER | \
172 SLAB_TRACE | SLAB_DESTROY_BY_RCU)
174 #define SLUB_MERGE_SAME (SLAB_DEBUG_FREE | SLAB_RECLAIM_ACCOUNT | \
175 SLAB_CACHE_DMA)
177 #ifndef ARCH_KMALLOC_MINALIGN
178 #define ARCH_KMALLOC_MINALIGN __alignof__(unsigned long long)
179 #endif
181 #ifndef ARCH_SLAB_MINALIGN
182 #define ARCH_SLAB_MINALIGN __alignof__(unsigned long long)
183 #endif
185 /* Internal SLUB flags */
186 #define __OBJECT_POISON 0x80000000 /* Poison object */
187 #define __SYSFS_ADD_DEFERRED 0x40000000 /* Not yet visible via sysfs */
189 static int kmem_size = sizeof(struct kmem_cache);
191 #ifdef CONFIG_SMP
192 static struct notifier_block slab_notifier;
193 #endif
195 static enum {
196 DOWN, /* No slab functionality available */
197 PARTIAL, /* kmem_cache_open() works but kmalloc does not */
198 UP, /* Everything works but does not show up in sysfs */
199 SYSFS /* Sysfs up */
200 } slab_state = DOWN;
202 /* A list of all slab caches on the system */
203 static DECLARE_RWSEM(slub_lock);
204 static LIST_HEAD(slab_caches);
207 * Tracking user of a slab.
209 struct track {
210 void *addr; /* Called from address */
211 int cpu; /* Was running on cpu */
212 int pid; /* Pid context */
213 unsigned long when; /* When did the operation occur */
216 enum track_item { TRACK_ALLOC, TRACK_FREE };
218 #if defined(CONFIG_SYSFS) && defined(CONFIG_SLUB_DEBUG)
219 static int sysfs_slab_add(struct kmem_cache *);
220 static int sysfs_slab_alias(struct kmem_cache *, const char *);
221 static void sysfs_slab_remove(struct kmem_cache *);
223 #else
224 static inline int sysfs_slab_add(struct kmem_cache *s) { return 0; }
225 static inline int sysfs_slab_alias(struct kmem_cache *s, const char *p)
226 { return 0; }
227 static inline void sysfs_slab_remove(struct kmem_cache *s)
229 kfree(s);
232 #endif
234 static inline void stat(struct kmem_cache_cpu *c, enum stat_item si)
236 #ifdef CONFIG_SLUB_STATS
237 c->stat[si]++;
238 #endif
241 /********************************************************************
242 * Core slab cache functions
243 *******************************************************************/
245 int slab_is_available(void)
247 return slab_state >= UP;
250 static inline struct kmem_cache_node *get_node(struct kmem_cache *s, int node)
252 #ifdef CONFIG_NUMA
253 return s->node[node];
254 #else
255 return &s->local_node;
256 #endif
259 static inline struct kmem_cache_cpu *get_cpu_slab(struct kmem_cache *s, int cpu)
261 #ifdef CONFIG_SMP
262 return s->cpu_slab[cpu];
263 #else
264 return &s->cpu_slab;
265 #endif
268 /* Verify that a pointer has an address that is valid within a slab page */
269 static inline int check_valid_pointer(struct kmem_cache *s,
270 struct page *page, const void *object)
272 void *base;
274 if (!object)
275 return 1;
277 base = page_address(page);
278 if (object < base || object >= base + page->objects * s->size ||
279 (object - base) % s->size) {
280 return 0;
283 return 1;
287 * Slow version of get and set free pointer.
289 * This version requires touching the cache lines of kmem_cache which
290 * we avoid to do in the fast alloc free paths. There we obtain the offset
291 * from the page struct.
293 static inline void *get_freepointer(struct kmem_cache *s, void *object)
295 return *(void **)(object + s->offset);
298 static inline void set_freepointer(struct kmem_cache *s, void *object, void *fp)
300 *(void **)(object + s->offset) = fp;
303 /* Loop over all objects in a slab */
304 #define for_each_object(__p, __s, __addr, __objects) \
305 for (__p = (__addr); __p < (__addr) + (__objects) * (__s)->size;\
306 __p += (__s)->size)
308 /* Scan freelist */
309 #define for_each_free_object(__p, __s, __free) \
310 for (__p = (__free); __p; __p = get_freepointer((__s), __p))
312 /* Determine object index from a given position */
313 static inline int slab_index(void *p, struct kmem_cache *s, void *addr)
315 return (p - addr) / s->size;
318 static inline struct kmem_cache_order_objects oo_make(int order,
319 unsigned long size)
321 struct kmem_cache_order_objects x = {
322 (order << 16) + (PAGE_SIZE << order) / size
325 return x;
328 static inline int oo_order(struct kmem_cache_order_objects x)
330 return x.x >> 16;
333 static inline int oo_objects(struct kmem_cache_order_objects x)
335 return x.x & ((1 << 16) - 1);
338 #ifdef CONFIG_SLUB_DEBUG
340 * Debug settings:
342 #ifdef CONFIG_SLUB_DEBUG_ON
343 static int slub_debug = DEBUG_DEFAULT_FLAGS;
344 #else
345 static int slub_debug;
346 #endif
348 static char *slub_debug_slabs;
351 * Object debugging
353 static void print_section(char *text, u8 *addr, unsigned int length)
355 int i, offset;
356 int newline = 1;
357 char ascii[17];
359 ascii[16] = 0;
361 for (i = 0; i < length; i++) {
362 if (newline) {
363 printk(KERN_ERR "%8s 0x%p: ", text, addr + i);
364 newline = 0;
366 printk(KERN_CONT " %02x", addr[i]);
367 offset = i % 16;
368 ascii[offset] = isgraph(addr[i]) ? addr[i] : '.';
369 if (offset == 15) {
370 printk(KERN_CONT " %s\n", ascii);
371 newline = 1;
374 if (!newline) {
375 i %= 16;
376 while (i < 16) {
377 printk(KERN_CONT " ");
378 ascii[i] = ' ';
379 i++;
381 printk(KERN_CONT " %s\n", ascii);
385 static struct track *get_track(struct kmem_cache *s, void *object,
386 enum track_item alloc)
388 struct track *p;
390 if (s->offset)
391 p = object + s->offset + sizeof(void *);
392 else
393 p = object + s->inuse;
395 return p + alloc;
398 static void set_track(struct kmem_cache *s, void *object,
399 enum track_item alloc, void *addr)
401 struct track *p;
403 if (s->offset)
404 p = object + s->offset + sizeof(void *);
405 else
406 p = object + s->inuse;
408 p += alloc;
409 if (addr) {
410 p->addr = addr;
411 p->cpu = smp_processor_id();
412 p->pid = current ? current->pid : -1;
413 p->when = jiffies;
414 } else
415 memset(p, 0, sizeof(struct track));
418 static void init_tracking(struct kmem_cache *s, void *object)
420 if (!(s->flags & SLAB_STORE_USER))
421 return;
423 set_track(s, object, TRACK_FREE, NULL);
424 set_track(s, object, TRACK_ALLOC, NULL);
427 static void print_track(const char *s, struct track *t)
429 if (!t->addr)
430 return;
432 printk(KERN_ERR "INFO: %s in ", s);
433 __print_symbol("%s", (unsigned long)t->addr);
434 printk(" age=%lu cpu=%u pid=%d\n", jiffies - t->when, t->cpu, t->pid);
437 static void print_tracking(struct kmem_cache *s, void *object)
439 if (!(s->flags & SLAB_STORE_USER))
440 return;
442 print_track("Allocated", get_track(s, object, TRACK_ALLOC));
443 print_track("Freed", get_track(s, object, TRACK_FREE));
446 static void print_page_info(struct page *page)
448 printk(KERN_ERR "INFO: Slab 0x%p objects=%u used=%u fp=0x%p flags=0x%04lx\n",
449 page, page->objects, page->inuse, page->freelist, page->flags);
453 static void slab_bug(struct kmem_cache *s, char *fmt, ...)
455 va_list args;
456 char buf[100];
458 va_start(args, fmt);
459 vsnprintf(buf, sizeof(buf), fmt, args);
460 va_end(args);
461 printk(KERN_ERR "========================================"
462 "=====================================\n");
463 printk(KERN_ERR "BUG %s: %s\n", s->name, buf);
464 printk(KERN_ERR "----------------------------------------"
465 "-------------------------------------\n\n");
468 static void slab_fix(struct kmem_cache *s, char *fmt, ...)
470 va_list args;
471 char buf[100];
473 va_start(args, fmt);
474 vsnprintf(buf, sizeof(buf), fmt, args);
475 va_end(args);
476 printk(KERN_ERR "FIX %s: %s\n", s->name, buf);
479 static void print_trailer(struct kmem_cache *s, struct page *page, u8 *p)
481 unsigned int off; /* Offset of last byte */
482 u8 *addr = page_address(page);
484 print_tracking(s, p);
486 print_page_info(page);
488 printk(KERN_ERR "INFO: Object 0x%p @offset=%tu fp=0x%p\n\n",
489 p, p - addr, get_freepointer(s, p));
491 if (p > addr + 16)
492 print_section("Bytes b4", p - 16, 16);
494 print_section("Object", p, min(s->objsize, 128));
496 if (s->flags & SLAB_RED_ZONE)
497 print_section("Redzone", p + s->objsize,
498 s->inuse - s->objsize);
500 if (s->offset)
501 off = s->offset + sizeof(void *);
502 else
503 off = s->inuse;
505 if (s->flags & SLAB_STORE_USER)
506 off += 2 * sizeof(struct track);
508 if (off != s->size)
509 /* Beginning of the filler is the free pointer */
510 print_section("Padding", p + off, s->size - off);
512 dump_stack();
515 static void object_err(struct kmem_cache *s, struct page *page,
516 u8 *object, char *reason)
518 slab_bug(s, "%s", reason);
519 print_trailer(s, page, object);
522 static void slab_err(struct kmem_cache *s, struct page *page, char *fmt, ...)
524 va_list args;
525 char buf[100];
527 va_start(args, fmt);
528 vsnprintf(buf, sizeof(buf), fmt, args);
529 va_end(args);
530 slab_bug(s, "%s", buf);
531 print_page_info(page);
532 dump_stack();
535 static void init_object(struct kmem_cache *s, void *object, int active)
537 u8 *p = object;
539 if (s->flags & __OBJECT_POISON) {
540 memset(p, POISON_FREE, s->objsize - 1);
541 p[s->objsize - 1] = POISON_END;
544 if (s->flags & SLAB_RED_ZONE)
545 memset(p + s->objsize,
546 active ? SLUB_RED_ACTIVE : SLUB_RED_INACTIVE,
547 s->inuse - s->objsize);
550 static u8 *check_bytes(u8 *start, unsigned int value, unsigned int bytes)
552 while (bytes) {
553 if (*start != (u8)value)
554 return start;
555 start++;
556 bytes--;
558 return NULL;
561 static void restore_bytes(struct kmem_cache *s, char *message, u8 data,
562 void *from, void *to)
564 slab_fix(s, "Restoring 0x%p-0x%p=0x%x\n", from, to - 1, data);
565 memset(from, data, to - from);
568 static int check_bytes_and_report(struct kmem_cache *s, struct page *page,
569 u8 *object, char *what,
570 u8 *start, unsigned int value, unsigned int bytes)
572 u8 *fault;
573 u8 *end;
575 fault = check_bytes(start, value, bytes);
576 if (!fault)
577 return 1;
579 end = start + bytes;
580 while (end > fault && end[-1] == value)
581 end--;
583 slab_bug(s, "%s overwritten", what);
584 printk(KERN_ERR "INFO: 0x%p-0x%p. First byte 0x%x instead of 0x%x\n",
585 fault, end - 1, fault[0], value);
586 print_trailer(s, page, object);
588 restore_bytes(s, what, value, fault, end);
589 return 0;
593 * Object layout:
595 * object address
596 * Bytes of the object to be managed.
597 * If the freepointer may overlay the object then the free
598 * pointer is the first word of the object.
600 * Poisoning uses 0x6b (POISON_FREE) and the last byte is
601 * 0xa5 (POISON_END)
603 * object + s->objsize
604 * Padding to reach word boundary. This is also used for Redzoning.
605 * Padding is extended by another word if Redzoning is enabled and
606 * objsize == inuse.
608 * We fill with 0xbb (RED_INACTIVE) for inactive objects and with
609 * 0xcc (RED_ACTIVE) for objects in use.
611 * object + s->inuse
612 * Meta data starts here.
614 * A. Free pointer (if we cannot overwrite object on free)
615 * B. Tracking data for SLAB_STORE_USER
616 * C. Padding to reach required alignment boundary or at mininum
617 * one word if debugging is on to be able to detect writes
618 * before the word boundary.
620 * Padding is done using 0x5a (POISON_INUSE)
622 * object + s->size
623 * Nothing is used beyond s->size.
625 * If slabcaches are merged then the objsize and inuse boundaries are mostly
626 * ignored. And therefore no slab options that rely on these boundaries
627 * may be used with merged slabcaches.
630 static int check_pad_bytes(struct kmem_cache *s, struct page *page, u8 *p)
632 unsigned long off = s->inuse; /* The end of info */
634 if (s->offset)
635 /* Freepointer is placed after the object. */
636 off += sizeof(void *);
638 if (s->flags & SLAB_STORE_USER)
639 /* We also have user information there */
640 off += 2 * sizeof(struct track);
642 if (s->size == off)
643 return 1;
645 return check_bytes_and_report(s, page, p, "Object padding",
646 p + off, POISON_INUSE, s->size - off);
649 /* Check the pad bytes at the end of a slab page */
650 static int slab_pad_check(struct kmem_cache *s, struct page *page)
652 u8 *start;
653 u8 *fault;
654 u8 *end;
655 int length;
656 int remainder;
658 if (!(s->flags & SLAB_POISON))
659 return 1;
661 start = page_address(page);
662 length = (PAGE_SIZE << compound_order(page));
663 end = start + length;
664 remainder = length % s->size;
665 if (!remainder)
666 return 1;
668 fault = check_bytes(end - remainder, POISON_INUSE, remainder);
669 if (!fault)
670 return 1;
671 while (end > fault && end[-1] == POISON_INUSE)
672 end--;
674 slab_err(s, page, "Padding overwritten. 0x%p-0x%p", fault, end - 1);
675 print_section("Padding", end - remainder, remainder);
677 restore_bytes(s, "slab padding", POISON_INUSE, start, end);
678 return 0;
681 static int check_object(struct kmem_cache *s, struct page *page,
682 void *object, int active)
684 u8 *p = object;
685 u8 *endobject = object + s->objsize;
687 if (s->flags & SLAB_RED_ZONE) {
688 unsigned int red =
689 active ? SLUB_RED_ACTIVE : SLUB_RED_INACTIVE;
691 if (!check_bytes_and_report(s, page, object, "Redzone",
692 endobject, red, s->inuse - s->objsize))
693 return 0;
694 } else {
695 if ((s->flags & SLAB_POISON) && s->objsize < s->inuse) {
696 check_bytes_and_report(s, page, p, "Alignment padding",
697 endobject, POISON_INUSE, s->inuse - s->objsize);
701 if (s->flags & SLAB_POISON) {
702 if (!active && (s->flags & __OBJECT_POISON) &&
703 (!check_bytes_and_report(s, page, p, "Poison", p,
704 POISON_FREE, s->objsize - 1) ||
705 !check_bytes_and_report(s, page, p, "Poison",
706 p + s->objsize - 1, POISON_END, 1)))
707 return 0;
709 * check_pad_bytes cleans up on its own.
711 check_pad_bytes(s, page, p);
714 if (!s->offset && active)
716 * Object and freepointer overlap. Cannot check
717 * freepointer while object is allocated.
719 return 1;
721 /* Check free pointer validity */
722 if (!check_valid_pointer(s, page, get_freepointer(s, p))) {
723 object_err(s, page, p, "Freepointer corrupt");
725 * No choice but to zap it and thus loose the remainder
726 * of the free objects in this slab. May cause
727 * another error because the object count is now wrong.
729 set_freepointer(s, p, NULL);
730 return 0;
732 return 1;
735 static int check_slab(struct kmem_cache *s, struct page *page)
737 int maxobj;
739 VM_BUG_ON(!irqs_disabled());
741 if (!PageSlab(page)) {
742 slab_err(s, page, "Not a valid slab page");
743 return 0;
746 maxobj = (PAGE_SIZE << compound_order(page)) / s->size;
747 if (page->objects > maxobj) {
748 slab_err(s, page, "objects %u > max %u",
749 s->name, page->objects, maxobj);
750 return 0;
752 if (page->inuse > page->objects) {
753 slab_err(s, page, "inuse %u > max %u",
754 s->name, page->inuse, page->objects);
755 return 0;
757 /* Slab_pad_check fixes things up after itself */
758 slab_pad_check(s, page);
759 return 1;
763 * Determine if a certain object on a page is on the freelist. Must hold the
764 * slab lock to guarantee that the chains are in a consistent state.
766 static int on_freelist(struct kmem_cache *s, struct page *page, void *search)
768 int nr = 0;
769 void *fp = page->freelist;
770 void *object = NULL;
771 unsigned long max_objects;
773 while (fp && nr <= page->objects) {
774 if (fp == search)
775 return 1;
776 if (!check_valid_pointer(s, page, fp)) {
777 if (object) {
778 object_err(s, page, object,
779 "Freechain corrupt");
780 set_freepointer(s, object, NULL);
781 break;
782 } else {
783 slab_err(s, page, "Freepointer corrupt");
784 page->freelist = NULL;
785 page->inuse = page->objects;
786 slab_fix(s, "Freelist cleared");
787 return 0;
789 break;
791 object = fp;
792 fp = get_freepointer(s, object);
793 nr++;
796 max_objects = (PAGE_SIZE << compound_order(page)) / s->size;
797 if (max_objects > 65535)
798 max_objects = 65535;
800 if (page->objects != max_objects) {
801 slab_err(s, page, "Wrong number of objects. Found %d but "
802 "should be %d", page->objects, max_objects);
803 page->objects = max_objects;
804 slab_fix(s, "Number of objects adjusted.");
806 if (page->inuse != page->objects - nr) {
807 slab_err(s, page, "Wrong object count. Counter is %d but "
808 "counted were %d", page->inuse, page->objects - nr);
809 page->inuse = page->objects - nr;
810 slab_fix(s, "Object count adjusted.");
812 return search == NULL;
815 static void trace(struct kmem_cache *s, struct page *page, void *object, int alloc)
817 if (s->flags & SLAB_TRACE) {
818 printk(KERN_INFO "TRACE %s %s 0x%p inuse=%d fp=0x%p\n",
819 s->name,
820 alloc ? "alloc" : "free",
821 object, page->inuse,
822 page->freelist);
824 if (!alloc)
825 print_section("Object", (void *)object, s->objsize);
827 dump_stack();
832 * Tracking of fully allocated slabs for debugging purposes.
834 static void add_full(struct kmem_cache_node *n, struct page *page)
836 spin_lock(&n->list_lock);
837 list_add(&page->lru, &n->full);
838 spin_unlock(&n->list_lock);
841 static void remove_full(struct kmem_cache *s, struct page *page)
843 struct kmem_cache_node *n;
845 if (!(s->flags & SLAB_STORE_USER))
846 return;
848 n = get_node(s, page_to_nid(page));
850 spin_lock(&n->list_lock);
851 list_del(&page->lru);
852 spin_unlock(&n->list_lock);
855 /* Tracking of the number of slabs for debugging purposes */
856 static inline unsigned long slabs_node(struct kmem_cache *s, int node)
858 struct kmem_cache_node *n = get_node(s, node);
860 return atomic_long_read(&n->nr_slabs);
863 static inline void inc_slabs_node(struct kmem_cache *s, int node, int objects)
865 struct kmem_cache_node *n = get_node(s, node);
868 * May be called early in order to allocate a slab for the
869 * kmem_cache_node structure. Solve the chicken-egg
870 * dilemma by deferring the increment of the count during
871 * bootstrap (see early_kmem_cache_node_alloc).
873 if (!NUMA_BUILD || n) {
874 atomic_long_inc(&n->nr_slabs);
875 atomic_long_add(objects, &n->total_objects);
878 static inline void dec_slabs_node(struct kmem_cache *s, int node, int objects)
880 struct kmem_cache_node *n = get_node(s, node);
882 atomic_long_dec(&n->nr_slabs);
883 atomic_long_sub(objects, &n->total_objects);
886 /* Object debug checks for alloc/free paths */
887 static void setup_object_debug(struct kmem_cache *s, struct page *page,
888 void *object)
890 if (!(s->flags & (SLAB_STORE_USER|SLAB_RED_ZONE|__OBJECT_POISON)))
891 return;
893 init_object(s, object, 0);
894 init_tracking(s, object);
897 static int alloc_debug_processing(struct kmem_cache *s, struct page *page,
898 void *object, void *addr)
900 if (!check_slab(s, page))
901 goto bad;
903 if (!on_freelist(s, page, object)) {
904 object_err(s, page, object, "Object already allocated");
905 goto bad;
908 if (!check_valid_pointer(s, page, object)) {
909 object_err(s, page, object, "Freelist Pointer check fails");
910 goto bad;
913 if (!check_object(s, page, object, 0))
914 goto bad;
916 /* Success perform special debug activities for allocs */
917 if (s->flags & SLAB_STORE_USER)
918 set_track(s, object, TRACK_ALLOC, addr);
919 trace(s, page, object, 1);
920 init_object(s, object, 1);
921 return 1;
923 bad:
924 if (PageSlab(page)) {
926 * If this is a slab page then lets do the best we can
927 * to avoid issues in the future. Marking all objects
928 * as used avoids touching the remaining objects.
930 slab_fix(s, "Marking all objects used");
931 page->inuse = page->objects;
932 page->freelist = NULL;
934 return 0;
937 static int free_debug_processing(struct kmem_cache *s, struct page *page,
938 void *object, void *addr)
940 if (!check_slab(s, page))
941 goto fail;
943 if (!check_valid_pointer(s, page, object)) {
944 slab_err(s, page, "Invalid object pointer 0x%p", object);
945 goto fail;
948 if (on_freelist(s, page, object)) {
949 object_err(s, page, object, "Object already free");
950 goto fail;
953 if (!check_object(s, page, object, 1))
954 return 0;
956 if (unlikely(s != page->slab)) {
957 if (!PageSlab(page)) {
958 slab_err(s, page, "Attempt to free object(0x%p) "
959 "outside of slab", object);
960 } else if (!page->slab) {
961 printk(KERN_ERR
962 "SLUB <none>: no slab for object 0x%p.\n",
963 object);
964 dump_stack();
965 } else
966 object_err(s, page, object,
967 "page slab pointer corrupt.");
968 goto fail;
971 /* Special debug activities for freeing objects */
972 if (!SlabFrozen(page) && !page->freelist)
973 remove_full(s, page);
974 if (s->flags & SLAB_STORE_USER)
975 set_track(s, object, TRACK_FREE, addr);
976 trace(s, page, object, 0);
977 init_object(s, object, 0);
978 return 1;
980 fail:
981 slab_fix(s, "Object at 0x%p not freed", object);
982 return 0;
985 static int __init setup_slub_debug(char *str)
987 slub_debug = DEBUG_DEFAULT_FLAGS;
988 if (*str++ != '=' || !*str)
990 * No options specified. Switch on full debugging.
992 goto out;
994 if (*str == ',')
996 * No options but restriction on slabs. This means full
997 * debugging for slabs matching a pattern.
999 goto check_slabs;
1001 slub_debug = 0;
1002 if (*str == '-')
1004 * Switch off all debugging measures.
1006 goto out;
1009 * Determine which debug features should be switched on
1011 for (; *str && *str != ','; str++) {
1012 switch (tolower(*str)) {
1013 case 'f':
1014 slub_debug |= SLAB_DEBUG_FREE;
1015 break;
1016 case 'z':
1017 slub_debug |= SLAB_RED_ZONE;
1018 break;
1019 case 'p':
1020 slub_debug |= SLAB_POISON;
1021 break;
1022 case 'u':
1023 slub_debug |= SLAB_STORE_USER;
1024 break;
1025 case 't':
1026 slub_debug |= SLAB_TRACE;
1027 break;
1028 default:
1029 printk(KERN_ERR "slub_debug option '%c' "
1030 "unknown. skipped\n", *str);
1034 check_slabs:
1035 if (*str == ',')
1036 slub_debug_slabs = str + 1;
1037 out:
1038 return 1;
1041 __setup("slub_debug", setup_slub_debug);
1043 static unsigned long kmem_cache_flags(unsigned long objsize,
1044 unsigned long flags, const char *name,
1045 void (*ctor)(struct kmem_cache *, void *))
1048 * Enable debugging if selected on the kernel commandline.
1050 if (slub_debug && (!slub_debug_slabs ||
1051 strncmp(slub_debug_slabs, name, strlen(slub_debug_slabs)) == 0))
1052 flags |= slub_debug;
1054 return flags;
1056 #else
1057 static inline void setup_object_debug(struct kmem_cache *s,
1058 struct page *page, void *object) {}
1060 static inline int alloc_debug_processing(struct kmem_cache *s,
1061 struct page *page, void *object, void *addr) { return 0; }
1063 static inline int free_debug_processing(struct kmem_cache *s,
1064 struct page *page, void *object, void *addr) { return 0; }
1066 static inline int slab_pad_check(struct kmem_cache *s, struct page *page)
1067 { return 1; }
1068 static inline int check_object(struct kmem_cache *s, struct page *page,
1069 void *object, int active) { return 1; }
1070 static inline void add_full(struct kmem_cache_node *n, struct page *page) {}
1071 static inline unsigned long kmem_cache_flags(unsigned long objsize,
1072 unsigned long flags, const char *name,
1073 void (*ctor)(struct kmem_cache *, void *))
1075 return flags;
1077 #define slub_debug 0
1079 static inline unsigned long slabs_node(struct kmem_cache *s, int node)
1080 { return 0; }
1081 static inline void inc_slabs_node(struct kmem_cache *s, int node,
1082 int objects) {}
1083 static inline void dec_slabs_node(struct kmem_cache *s, int node,
1084 int objects) {}
1085 #endif
1088 * Slab allocation and freeing
1090 static inline struct page *alloc_slab_page(gfp_t flags, int node,
1091 struct kmem_cache_order_objects oo)
1093 int order = oo_order(oo);
1095 if (node == -1)
1096 return alloc_pages(flags, order);
1097 else
1098 return alloc_pages_node(node, flags, order);
1101 static struct page *allocate_slab(struct kmem_cache *s, gfp_t flags, int node)
1103 struct page *page;
1104 struct kmem_cache_order_objects oo = s->oo;
1106 flags |= s->allocflags;
1108 page = alloc_slab_page(flags | __GFP_NOWARN | __GFP_NORETRY, node,
1109 oo);
1110 if (unlikely(!page)) {
1111 oo = s->min;
1113 * Allocation may have failed due to fragmentation.
1114 * Try a lower order alloc if possible
1116 page = alloc_slab_page(flags, node, oo);
1117 if (!page)
1118 return NULL;
1120 stat(get_cpu_slab(s, raw_smp_processor_id()), ORDER_FALLBACK);
1122 page->objects = oo_objects(oo);
1123 mod_zone_page_state(page_zone(page),
1124 (s->flags & SLAB_RECLAIM_ACCOUNT) ?
1125 NR_SLAB_RECLAIMABLE : NR_SLAB_UNRECLAIMABLE,
1126 1 << oo_order(oo));
1128 return page;
1131 static void setup_object(struct kmem_cache *s, struct page *page,
1132 void *object)
1134 setup_object_debug(s, page, object);
1135 if (unlikely(s->ctor))
1136 s->ctor(s, object);
1139 static struct page *new_slab(struct kmem_cache *s, gfp_t flags, int node)
1141 struct page *page;
1142 void *start;
1143 void *last;
1144 void *p;
1146 BUG_ON(flags & GFP_SLAB_BUG_MASK);
1148 page = allocate_slab(s,
1149 flags & (GFP_RECLAIM_MASK | GFP_CONSTRAINT_MASK), node);
1150 if (!page)
1151 goto out;
1153 inc_slabs_node(s, page_to_nid(page), page->objects);
1154 page->slab = s;
1155 page->flags |= 1 << PG_slab;
1156 if (s->flags & (SLAB_DEBUG_FREE | SLAB_RED_ZONE | SLAB_POISON |
1157 SLAB_STORE_USER | SLAB_TRACE))
1158 SetSlabDebug(page);
1160 start = page_address(page);
1162 if (unlikely(s->flags & SLAB_POISON))
1163 memset(start, POISON_INUSE, PAGE_SIZE << compound_order(page));
1165 last = start;
1166 for_each_object(p, s, start, page->objects) {
1167 setup_object(s, page, last);
1168 set_freepointer(s, last, p);
1169 last = p;
1171 setup_object(s, page, last);
1172 set_freepointer(s, last, NULL);
1174 page->freelist = start;
1175 page->inuse = 0;
1176 out:
1177 return page;
1180 static void __free_slab(struct kmem_cache *s, struct page *page)
1182 int order = compound_order(page);
1183 int pages = 1 << order;
1185 if (unlikely(SlabDebug(page))) {
1186 void *p;
1188 slab_pad_check(s, page);
1189 for_each_object(p, s, page_address(page),
1190 page->objects)
1191 check_object(s, page, p, 0);
1192 ClearSlabDebug(page);
1195 mod_zone_page_state(page_zone(page),
1196 (s->flags & SLAB_RECLAIM_ACCOUNT) ?
1197 NR_SLAB_RECLAIMABLE : NR_SLAB_UNRECLAIMABLE,
1198 -pages);
1200 __ClearPageSlab(page);
1201 reset_page_mapcount(page);
1202 __free_pages(page, order);
1205 static void rcu_free_slab(struct rcu_head *h)
1207 struct page *page;
1209 page = container_of((struct list_head *)h, struct page, lru);
1210 __free_slab(page->slab, page);
1213 static void free_slab(struct kmem_cache *s, struct page *page)
1215 if (unlikely(s->flags & SLAB_DESTROY_BY_RCU)) {
1217 * RCU free overloads the RCU head over the LRU
1219 struct rcu_head *head = (void *)&page->lru;
1221 call_rcu(head, rcu_free_slab);
1222 } else
1223 __free_slab(s, page);
1226 static void discard_slab(struct kmem_cache *s, struct page *page)
1228 dec_slabs_node(s, page_to_nid(page), page->objects);
1229 free_slab(s, page);
1233 * Per slab locking using the pagelock
1235 static __always_inline void slab_lock(struct page *page)
1237 bit_spin_lock(PG_locked, &page->flags);
1240 static __always_inline void slab_unlock(struct page *page)
1242 __bit_spin_unlock(PG_locked, &page->flags);
1245 static __always_inline int slab_trylock(struct page *page)
1247 int rc = 1;
1249 rc = bit_spin_trylock(PG_locked, &page->flags);
1250 return rc;
1254 * Management of partially allocated slabs
1256 static void add_partial(struct kmem_cache_node *n,
1257 struct page *page, int tail)
1259 spin_lock(&n->list_lock);
1260 n->nr_partial++;
1261 if (tail)
1262 list_add_tail(&page->lru, &n->partial);
1263 else
1264 list_add(&page->lru, &n->partial);
1265 spin_unlock(&n->list_lock);
1268 static void remove_partial(struct kmem_cache *s,
1269 struct page *page)
1271 struct kmem_cache_node *n = get_node(s, page_to_nid(page));
1273 spin_lock(&n->list_lock);
1274 list_del(&page->lru);
1275 n->nr_partial--;
1276 spin_unlock(&n->list_lock);
1280 * Lock slab and remove from the partial list.
1282 * Must hold list_lock.
1284 static inline int lock_and_freeze_slab(struct kmem_cache_node *n, struct page *page)
1286 if (slab_trylock(page)) {
1287 list_del(&page->lru);
1288 n->nr_partial--;
1289 SetSlabFrozen(page);
1290 return 1;
1292 return 0;
1296 * Try to allocate a partial slab from a specific node.
1298 static struct page *get_partial_node(struct kmem_cache_node *n)
1300 struct page *page;
1303 * Racy check. If we mistakenly see no partial slabs then we
1304 * just allocate an empty slab. If we mistakenly try to get a
1305 * partial slab and there is none available then get_partials()
1306 * will return NULL.
1308 if (!n || !n->nr_partial)
1309 return NULL;
1311 spin_lock(&n->list_lock);
1312 list_for_each_entry(page, &n->partial, lru)
1313 if (lock_and_freeze_slab(n, page))
1314 goto out;
1315 page = NULL;
1316 out:
1317 spin_unlock(&n->list_lock);
1318 return page;
1322 * Get a page from somewhere. Search in increasing NUMA distances.
1324 static struct page *get_any_partial(struct kmem_cache *s, gfp_t flags)
1326 #ifdef CONFIG_NUMA
1327 struct zonelist *zonelist;
1328 struct zoneref *z;
1329 struct zone *zone;
1330 enum zone_type high_zoneidx = gfp_zone(flags);
1331 struct page *page;
1334 * The defrag ratio allows a configuration of the tradeoffs between
1335 * inter node defragmentation and node local allocations. A lower
1336 * defrag_ratio increases the tendency to do local allocations
1337 * instead of attempting to obtain partial slabs from other nodes.
1339 * If the defrag_ratio is set to 0 then kmalloc() always
1340 * returns node local objects. If the ratio is higher then kmalloc()
1341 * may return off node objects because partial slabs are obtained
1342 * from other nodes and filled up.
1344 * If /sys/kernel/slab/xx/defrag_ratio is set to 100 (which makes
1345 * defrag_ratio = 1000) then every (well almost) allocation will
1346 * first attempt to defrag slab caches on other nodes. This means
1347 * scanning over all nodes to look for partial slabs which may be
1348 * expensive if we do it every time we are trying to find a slab
1349 * with available objects.
1351 if (!s->remote_node_defrag_ratio ||
1352 get_cycles() % 1024 > s->remote_node_defrag_ratio)
1353 return NULL;
1355 zonelist = node_zonelist(slab_node(current->mempolicy), flags);
1356 for_each_zone_zonelist(zone, z, zonelist, high_zoneidx) {
1357 struct kmem_cache_node *n;
1359 n = get_node(s, zone_to_nid(zone));
1361 if (n && cpuset_zone_allowed_hardwall(zone, flags) &&
1362 n->nr_partial > MIN_PARTIAL) {
1363 page = get_partial_node(n);
1364 if (page)
1365 return page;
1368 #endif
1369 return NULL;
1373 * Get a partial page, lock it and return it.
1375 static struct page *get_partial(struct kmem_cache *s, gfp_t flags, int node)
1377 struct page *page;
1378 int searchnode = (node == -1) ? numa_node_id() : node;
1380 page = get_partial_node(get_node(s, searchnode));
1381 if (page || (flags & __GFP_THISNODE))
1382 return page;
1384 return get_any_partial(s, flags);
1388 * Move a page back to the lists.
1390 * Must be called with the slab lock held.
1392 * On exit the slab lock will have been dropped.
1394 static void unfreeze_slab(struct kmem_cache *s, struct page *page, int tail)
1396 struct kmem_cache_node *n = get_node(s, page_to_nid(page));
1397 struct kmem_cache_cpu *c = get_cpu_slab(s, smp_processor_id());
1399 ClearSlabFrozen(page);
1400 if (page->inuse) {
1402 if (page->freelist) {
1403 add_partial(n, page, tail);
1404 stat(c, tail ? DEACTIVATE_TO_TAIL : DEACTIVATE_TO_HEAD);
1405 } else {
1406 stat(c, DEACTIVATE_FULL);
1407 if (SlabDebug(page) && (s->flags & SLAB_STORE_USER))
1408 add_full(n, page);
1410 slab_unlock(page);
1411 } else {
1412 stat(c, DEACTIVATE_EMPTY);
1413 if (n->nr_partial < MIN_PARTIAL) {
1415 * Adding an empty slab to the partial slabs in order
1416 * to avoid page allocator overhead. This slab needs
1417 * to come after the other slabs with objects in
1418 * so that the others get filled first. That way the
1419 * size of the partial list stays small.
1421 * kmem_cache_shrink can reclaim any empty slabs from the
1422 * partial list.
1424 add_partial(n, page, 1);
1425 slab_unlock(page);
1426 } else {
1427 slab_unlock(page);
1428 stat(get_cpu_slab(s, raw_smp_processor_id()), FREE_SLAB);
1429 discard_slab(s, page);
1435 * Remove the cpu slab
1437 static void deactivate_slab(struct kmem_cache *s, struct kmem_cache_cpu *c)
1439 struct page *page = c->page;
1440 int tail = 1;
1442 if (page->freelist)
1443 stat(c, DEACTIVATE_REMOTE_FREES);
1445 * Merge cpu freelist into slab freelist. Typically we get here
1446 * because both freelists are empty. So this is unlikely
1447 * to occur.
1449 while (unlikely(c->freelist)) {
1450 void **object;
1452 tail = 0; /* Hot objects. Put the slab first */
1454 /* Retrieve object from cpu_freelist */
1455 object = c->freelist;
1456 c->freelist = c->freelist[c->offset];
1458 /* And put onto the regular freelist */
1459 object[c->offset] = page->freelist;
1460 page->freelist = object;
1461 page->inuse--;
1463 c->page = NULL;
1464 unfreeze_slab(s, page, tail);
1467 static inline void flush_slab(struct kmem_cache *s, struct kmem_cache_cpu *c)
1469 stat(c, CPUSLAB_FLUSH);
1470 slab_lock(c->page);
1471 deactivate_slab(s, c);
1475 * Flush cpu slab.
1477 * Called from IPI handler with interrupts disabled.
1479 static inline void __flush_cpu_slab(struct kmem_cache *s, int cpu)
1481 struct kmem_cache_cpu *c = get_cpu_slab(s, cpu);
1483 if (likely(c && c->page))
1484 flush_slab(s, c);
1487 static void flush_cpu_slab(void *d)
1489 struct kmem_cache *s = d;
1491 __flush_cpu_slab(s, smp_processor_id());
1494 static void flush_all(struct kmem_cache *s)
1496 #ifdef CONFIG_SMP
1497 on_each_cpu(flush_cpu_slab, s, 1, 1);
1498 #else
1499 unsigned long flags;
1501 local_irq_save(flags);
1502 flush_cpu_slab(s);
1503 local_irq_restore(flags);
1504 #endif
1508 * Check if the objects in a per cpu structure fit numa
1509 * locality expectations.
1511 static inline int node_match(struct kmem_cache_cpu *c, int node)
1513 #ifdef CONFIG_NUMA
1514 if (node != -1 && c->node != node)
1515 return 0;
1516 #endif
1517 return 1;
1521 * Slow path. The lockless freelist is empty or we need to perform
1522 * debugging duties.
1524 * Interrupts are disabled.
1526 * Processing is still very fast if new objects have been freed to the
1527 * regular freelist. In that case we simply take over the regular freelist
1528 * as the lockless freelist and zap the regular freelist.
1530 * If that is not working then we fall back to the partial lists. We take the
1531 * first element of the freelist as the object to allocate now and move the
1532 * rest of the freelist to the lockless freelist.
1534 * And if we were unable to get a new slab from the partial slab lists then
1535 * we need to allocate a new slab. This is the slowest path since it involves
1536 * a call to the page allocator and the setup of a new slab.
1538 static void *__slab_alloc(struct kmem_cache *s,
1539 gfp_t gfpflags, int node, void *addr, struct kmem_cache_cpu *c)
1541 void **object;
1542 struct page *new;
1544 /* We handle __GFP_ZERO in the caller */
1545 gfpflags &= ~__GFP_ZERO;
1547 if (!c->page)
1548 goto new_slab;
1550 slab_lock(c->page);
1551 if (unlikely(!node_match(c, node)))
1552 goto another_slab;
1554 stat(c, ALLOC_REFILL);
1556 load_freelist:
1557 object = c->page->freelist;
1558 if (unlikely(!object))
1559 goto another_slab;
1560 if (unlikely(SlabDebug(c->page)))
1561 goto debug;
1563 c->freelist = object[c->offset];
1564 c->page->inuse = c->page->objects;
1565 c->page->freelist = NULL;
1566 c->node = page_to_nid(c->page);
1567 unlock_out:
1568 slab_unlock(c->page);
1569 stat(c, ALLOC_SLOWPATH);
1570 return object;
1572 another_slab:
1573 deactivate_slab(s, c);
1575 new_slab:
1576 new = get_partial(s, gfpflags, node);
1577 if (new) {
1578 c->page = new;
1579 stat(c, ALLOC_FROM_PARTIAL);
1580 goto load_freelist;
1583 if (gfpflags & __GFP_WAIT)
1584 local_irq_enable();
1586 new = new_slab(s, gfpflags, node);
1588 if (gfpflags & __GFP_WAIT)
1589 local_irq_disable();
1591 if (new) {
1592 c = get_cpu_slab(s, smp_processor_id());
1593 stat(c, ALLOC_SLAB);
1594 if (c->page)
1595 flush_slab(s, c);
1596 slab_lock(new);
1597 SetSlabFrozen(new);
1598 c->page = new;
1599 goto load_freelist;
1601 return NULL;
1602 debug:
1603 if (!alloc_debug_processing(s, c->page, object, addr))
1604 goto another_slab;
1606 c->page->inuse++;
1607 c->page->freelist = object[c->offset];
1608 c->node = -1;
1609 goto unlock_out;
1613 * Inlined fastpath so that allocation functions (kmalloc, kmem_cache_alloc)
1614 * have the fastpath folded into their functions. So no function call
1615 * overhead for requests that can be satisfied on the fastpath.
1617 * The fastpath works by first checking if the lockless freelist can be used.
1618 * If not then __slab_alloc is called for slow processing.
1620 * Otherwise we can simply pick the next object from the lockless free list.
1622 static __always_inline void *slab_alloc(struct kmem_cache *s,
1623 gfp_t gfpflags, int node, void *addr)
1625 void **object;
1626 struct kmem_cache_cpu *c;
1627 unsigned long flags;
1629 local_irq_save(flags);
1630 c = get_cpu_slab(s, smp_processor_id());
1631 if (unlikely(!c->freelist || !node_match(c, node)))
1633 object = __slab_alloc(s, gfpflags, node, addr, c);
1635 else {
1636 object = c->freelist;
1637 c->freelist = object[c->offset];
1638 stat(c, ALLOC_FASTPATH);
1640 local_irq_restore(flags);
1642 if (unlikely((gfpflags & __GFP_ZERO) && object))
1643 memset(object, 0, c->objsize);
1645 return object;
1648 void *kmem_cache_alloc(struct kmem_cache *s, gfp_t gfpflags)
1650 return slab_alloc(s, gfpflags, -1, __builtin_return_address(0));
1652 EXPORT_SYMBOL(kmem_cache_alloc);
1654 #ifdef CONFIG_NUMA
1655 void *kmem_cache_alloc_node(struct kmem_cache *s, gfp_t gfpflags, int node)
1657 return slab_alloc(s, gfpflags, node, __builtin_return_address(0));
1659 EXPORT_SYMBOL(kmem_cache_alloc_node);
1660 #endif
1663 * Slow patch handling. This may still be called frequently since objects
1664 * have a longer lifetime than the cpu slabs in most processing loads.
1666 * So we still attempt to reduce cache line usage. Just take the slab
1667 * lock and free the item. If there is no additional partial page
1668 * handling required then we can return immediately.
1670 static void __slab_free(struct kmem_cache *s, struct page *page,
1671 void *x, void *addr, unsigned int offset)
1673 void *prior;
1674 void **object = (void *)x;
1675 struct kmem_cache_cpu *c;
1677 c = get_cpu_slab(s, raw_smp_processor_id());
1678 stat(c, FREE_SLOWPATH);
1679 slab_lock(page);
1681 if (unlikely(SlabDebug(page)))
1682 goto debug;
1684 checks_ok:
1685 prior = object[offset] = page->freelist;
1686 page->freelist = object;
1687 page->inuse--;
1689 if (unlikely(SlabFrozen(page))) {
1690 stat(c, FREE_FROZEN);
1691 goto out_unlock;
1694 if (unlikely(!page->inuse))
1695 goto slab_empty;
1698 * Objects left in the slab. If it was not on the partial list before
1699 * then add it.
1701 if (unlikely(!prior)) {
1702 add_partial(get_node(s, page_to_nid(page)), page, 1);
1703 stat(c, FREE_ADD_PARTIAL);
1706 out_unlock:
1707 slab_unlock(page);
1708 return;
1710 slab_empty:
1711 if (prior) {
1713 * Slab still on the partial list.
1715 remove_partial(s, page);
1716 stat(c, FREE_REMOVE_PARTIAL);
1718 slab_unlock(page);
1719 stat(c, FREE_SLAB);
1720 discard_slab(s, page);
1721 return;
1723 debug:
1724 if (!free_debug_processing(s, page, x, addr))
1725 goto out_unlock;
1726 goto checks_ok;
1730 * Fastpath with forced inlining to produce a kfree and kmem_cache_free that
1731 * can perform fastpath freeing without additional function calls.
1733 * The fastpath is only possible if we are freeing to the current cpu slab
1734 * of this processor. This typically the case if we have just allocated
1735 * the item before.
1737 * If fastpath is not possible then fall back to __slab_free where we deal
1738 * with all sorts of special processing.
1740 static __always_inline void slab_free(struct kmem_cache *s,
1741 struct page *page, void *x, void *addr)
1743 void **object = (void *)x;
1744 struct kmem_cache_cpu *c;
1745 unsigned long flags;
1747 local_irq_save(flags);
1748 c = get_cpu_slab(s, smp_processor_id());
1749 debug_check_no_locks_freed(object, c->objsize);
1750 if (likely(page == c->page && c->node >= 0)) {
1751 object[c->offset] = c->freelist;
1752 c->freelist = object;
1753 stat(c, FREE_FASTPATH);
1754 } else
1755 __slab_free(s, page, x, addr, c->offset);
1757 local_irq_restore(flags);
1760 void kmem_cache_free(struct kmem_cache *s, void *x)
1762 struct page *page;
1764 page = virt_to_head_page(x);
1766 slab_free(s, page, x, __builtin_return_address(0));
1768 EXPORT_SYMBOL(kmem_cache_free);
1770 /* Figure out on which slab object the object resides */
1771 static struct page *get_object_page(const void *x)
1773 struct page *page = virt_to_head_page(x);
1775 if (!PageSlab(page))
1776 return NULL;
1778 return page;
1782 * Object placement in a slab is made very easy because we always start at
1783 * offset 0. If we tune the size of the object to the alignment then we can
1784 * get the required alignment by putting one properly sized object after
1785 * another.
1787 * Notice that the allocation order determines the sizes of the per cpu
1788 * caches. Each processor has always one slab available for allocations.
1789 * Increasing the allocation order reduces the number of times that slabs
1790 * must be moved on and off the partial lists and is therefore a factor in
1791 * locking overhead.
1795 * Mininum / Maximum order of slab pages. This influences locking overhead
1796 * and slab fragmentation. A higher order reduces the number of partial slabs
1797 * and increases the number of allocations possible without having to
1798 * take the list_lock.
1800 static int slub_min_order;
1801 static int slub_max_order = PAGE_ALLOC_COSTLY_ORDER;
1802 static int slub_min_objects;
1805 * Merge control. If this is set then no merging of slab caches will occur.
1806 * (Could be removed. This was introduced to pacify the merge skeptics.)
1808 static int slub_nomerge;
1811 * Calculate the order of allocation given an slab object size.
1813 * The order of allocation has significant impact on performance and other
1814 * system components. Generally order 0 allocations should be preferred since
1815 * order 0 does not cause fragmentation in the page allocator. Larger objects
1816 * be problematic to put into order 0 slabs because there may be too much
1817 * unused space left. We go to a higher order if more than 1/16th of the slab
1818 * would be wasted.
1820 * In order to reach satisfactory performance we must ensure that a minimum
1821 * number of objects is in one slab. Otherwise we may generate too much
1822 * activity on the partial lists which requires taking the list_lock. This is
1823 * less a concern for large slabs though which are rarely used.
1825 * slub_max_order specifies the order where we begin to stop considering the
1826 * number of objects in a slab as critical. If we reach slub_max_order then
1827 * we try to keep the page order as low as possible. So we accept more waste
1828 * of space in favor of a small page order.
1830 * Higher order allocations also allow the placement of more objects in a
1831 * slab and thereby reduce object handling overhead. If the user has
1832 * requested a higher mininum order then we start with that one instead of
1833 * the smallest order which will fit the object.
1835 static inline int slab_order(int size, int min_objects,
1836 int max_order, int fract_leftover)
1838 int order;
1839 int rem;
1840 int min_order = slub_min_order;
1842 if ((PAGE_SIZE << min_order) / size > 65535)
1843 return get_order(size * 65535) - 1;
1845 for (order = max(min_order,
1846 fls(min_objects * size - 1) - PAGE_SHIFT);
1847 order <= max_order; order++) {
1849 unsigned long slab_size = PAGE_SIZE << order;
1851 if (slab_size < min_objects * size)
1852 continue;
1854 rem = slab_size % size;
1856 if (rem <= slab_size / fract_leftover)
1857 break;
1861 return order;
1864 static inline int calculate_order(int size)
1866 int order;
1867 int min_objects;
1868 int fraction;
1871 * Attempt to find best configuration for a slab. This
1872 * works by first attempting to generate a layout with
1873 * the best configuration and backing off gradually.
1875 * First we reduce the acceptable waste in a slab. Then
1876 * we reduce the minimum objects required in a slab.
1878 min_objects = slub_min_objects;
1879 if (!min_objects)
1880 min_objects = 4 * (fls(nr_cpu_ids) + 1);
1881 while (min_objects > 1) {
1882 fraction = 16;
1883 while (fraction >= 4) {
1884 order = slab_order(size, min_objects,
1885 slub_max_order, fraction);
1886 if (order <= slub_max_order)
1887 return order;
1888 fraction /= 2;
1890 min_objects /= 2;
1894 * We were unable to place multiple objects in a slab. Now
1895 * lets see if we can place a single object there.
1897 order = slab_order(size, 1, slub_max_order, 1);
1898 if (order <= slub_max_order)
1899 return order;
1902 * Doh this slab cannot be placed using slub_max_order.
1904 order = slab_order(size, 1, MAX_ORDER, 1);
1905 if (order <= MAX_ORDER)
1906 return order;
1907 return -ENOSYS;
1911 * Figure out what the alignment of the objects will be.
1913 static unsigned long calculate_alignment(unsigned long flags,
1914 unsigned long align, unsigned long size)
1917 * If the user wants hardware cache aligned objects then follow that
1918 * suggestion if the object is sufficiently large.
1920 * The hardware cache alignment cannot override the specified
1921 * alignment though. If that is greater then use it.
1923 if (flags & SLAB_HWCACHE_ALIGN) {
1924 unsigned long ralign = cache_line_size();
1925 while (size <= ralign / 2)
1926 ralign /= 2;
1927 align = max(align, ralign);
1930 if (align < ARCH_SLAB_MINALIGN)
1931 align = ARCH_SLAB_MINALIGN;
1933 return ALIGN(align, sizeof(void *));
1936 static void init_kmem_cache_cpu(struct kmem_cache *s,
1937 struct kmem_cache_cpu *c)
1939 c->page = NULL;
1940 c->freelist = NULL;
1941 c->node = 0;
1942 c->offset = s->offset / sizeof(void *);
1943 c->objsize = s->objsize;
1944 #ifdef CONFIG_SLUB_STATS
1945 memset(c->stat, 0, NR_SLUB_STAT_ITEMS * sizeof(unsigned));
1946 #endif
1949 static void init_kmem_cache_node(struct kmem_cache_node *n)
1951 n->nr_partial = 0;
1952 spin_lock_init(&n->list_lock);
1953 INIT_LIST_HEAD(&n->partial);
1954 #ifdef CONFIG_SLUB_DEBUG
1955 atomic_long_set(&n->nr_slabs, 0);
1956 INIT_LIST_HEAD(&n->full);
1957 #endif
1960 #ifdef CONFIG_SMP
1962 * Per cpu array for per cpu structures.
1964 * The per cpu array places all kmem_cache_cpu structures from one processor
1965 * close together meaning that it becomes possible that multiple per cpu
1966 * structures are contained in one cacheline. This may be particularly
1967 * beneficial for the kmalloc caches.
1969 * A desktop system typically has around 60-80 slabs. With 100 here we are
1970 * likely able to get per cpu structures for all caches from the array defined
1971 * here. We must be able to cover all kmalloc caches during bootstrap.
1973 * If the per cpu array is exhausted then fall back to kmalloc
1974 * of individual cachelines. No sharing is possible then.
1976 #define NR_KMEM_CACHE_CPU 100
1978 static DEFINE_PER_CPU(struct kmem_cache_cpu,
1979 kmem_cache_cpu)[NR_KMEM_CACHE_CPU];
1981 static DEFINE_PER_CPU(struct kmem_cache_cpu *, kmem_cache_cpu_free);
1982 static cpumask_t kmem_cach_cpu_free_init_once = CPU_MASK_NONE;
1984 static struct kmem_cache_cpu *alloc_kmem_cache_cpu(struct kmem_cache *s,
1985 int cpu, gfp_t flags)
1987 struct kmem_cache_cpu *c = per_cpu(kmem_cache_cpu_free, cpu);
1989 if (c)
1990 per_cpu(kmem_cache_cpu_free, cpu) =
1991 (void *)c->freelist;
1992 else {
1993 /* Table overflow: So allocate ourselves */
1994 c = kmalloc_node(
1995 ALIGN(sizeof(struct kmem_cache_cpu), cache_line_size()),
1996 flags, cpu_to_node(cpu));
1997 if (!c)
1998 return NULL;
2001 init_kmem_cache_cpu(s, c);
2002 return c;
2005 static void free_kmem_cache_cpu(struct kmem_cache_cpu *c, int cpu)
2007 if (c < per_cpu(kmem_cache_cpu, cpu) ||
2008 c > per_cpu(kmem_cache_cpu, cpu) + NR_KMEM_CACHE_CPU) {
2009 kfree(c);
2010 return;
2012 c->freelist = (void *)per_cpu(kmem_cache_cpu_free, cpu);
2013 per_cpu(kmem_cache_cpu_free, cpu) = c;
2016 static void free_kmem_cache_cpus(struct kmem_cache *s)
2018 int cpu;
2020 for_each_online_cpu(cpu) {
2021 struct kmem_cache_cpu *c = get_cpu_slab(s, cpu);
2023 if (c) {
2024 s->cpu_slab[cpu] = NULL;
2025 free_kmem_cache_cpu(c, cpu);
2030 static int alloc_kmem_cache_cpus(struct kmem_cache *s, gfp_t flags)
2032 int cpu;
2034 for_each_online_cpu(cpu) {
2035 struct kmem_cache_cpu *c = get_cpu_slab(s, cpu);
2037 if (c)
2038 continue;
2040 c = alloc_kmem_cache_cpu(s, cpu, flags);
2041 if (!c) {
2042 free_kmem_cache_cpus(s);
2043 return 0;
2045 s->cpu_slab[cpu] = c;
2047 return 1;
2051 * Initialize the per cpu array.
2053 static void init_alloc_cpu_cpu(int cpu)
2055 int i;
2057 if (cpu_isset(cpu, kmem_cach_cpu_free_init_once))
2058 return;
2060 for (i = NR_KMEM_CACHE_CPU - 1; i >= 0; i--)
2061 free_kmem_cache_cpu(&per_cpu(kmem_cache_cpu, cpu)[i], cpu);
2063 cpu_set(cpu, kmem_cach_cpu_free_init_once);
2066 static void __init init_alloc_cpu(void)
2068 int cpu;
2070 for_each_online_cpu(cpu)
2071 init_alloc_cpu_cpu(cpu);
2074 #else
2075 static inline void free_kmem_cache_cpus(struct kmem_cache *s) {}
2076 static inline void init_alloc_cpu(void) {}
2078 static inline int alloc_kmem_cache_cpus(struct kmem_cache *s, gfp_t flags)
2080 init_kmem_cache_cpu(s, &s->cpu_slab);
2081 return 1;
2083 #endif
2085 #ifdef CONFIG_NUMA
2087 * No kmalloc_node yet so do it by hand. We know that this is the first
2088 * slab on the node for this slabcache. There are no concurrent accesses
2089 * possible.
2091 * Note that this function only works on the kmalloc_node_cache
2092 * when allocating for the kmalloc_node_cache. This is used for bootstrapping
2093 * memory on a fresh node that has no slab structures yet.
2095 static struct kmem_cache_node *early_kmem_cache_node_alloc(gfp_t gfpflags,
2096 int node)
2098 struct page *page;
2099 struct kmem_cache_node *n;
2100 unsigned long flags;
2102 BUG_ON(kmalloc_caches->size < sizeof(struct kmem_cache_node));
2104 page = new_slab(kmalloc_caches, gfpflags, node);
2106 BUG_ON(!page);
2107 if (page_to_nid(page) != node) {
2108 printk(KERN_ERR "SLUB: Unable to allocate memory from "
2109 "node %d\n", node);
2110 printk(KERN_ERR "SLUB: Allocating a useless per node structure "
2111 "in order to be able to continue\n");
2114 n = page->freelist;
2115 BUG_ON(!n);
2116 page->freelist = get_freepointer(kmalloc_caches, n);
2117 page->inuse++;
2118 kmalloc_caches->node[node] = n;
2119 #ifdef CONFIG_SLUB_DEBUG
2120 init_object(kmalloc_caches, n, 1);
2121 init_tracking(kmalloc_caches, n);
2122 #endif
2123 init_kmem_cache_node(n);
2124 inc_slabs_node(kmalloc_caches, node, page->objects);
2127 * lockdep requires consistent irq usage for each lock
2128 * so even though there cannot be a race this early in
2129 * the boot sequence, we still disable irqs.
2131 local_irq_save(flags);
2132 add_partial(n, page, 0);
2133 local_irq_restore(flags);
2134 return n;
2137 static void free_kmem_cache_nodes(struct kmem_cache *s)
2139 int node;
2141 for_each_node_state(node, N_NORMAL_MEMORY) {
2142 struct kmem_cache_node *n = s->node[node];
2143 if (n && n != &s->local_node)
2144 kmem_cache_free(kmalloc_caches, n);
2145 s->node[node] = NULL;
2149 static int init_kmem_cache_nodes(struct kmem_cache *s, gfp_t gfpflags)
2151 int node;
2152 int local_node;
2154 if (slab_state >= UP)
2155 local_node = page_to_nid(virt_to_page(s));
2156 else
2157 local_node = 0;
2159 for_each_node_state(node, N_NORMAL_MEMORY) {
2160 struct kmem_cache_node *n;
2162 if (local_node == node)
2163 n = &s->local_node;
2164 else {
2165 if (slab_state == DOWN) {
2166 n = early_kmem_cache_node_alloc(gfpflags,
2167 node);
2168 continue;
2170 n = kmem_cache_alloc_node(kmalloc_caches,
2171 gfpflags, node);
2173 if (!n) {
2174 free_kmem_cache_nodes(s);
2175 return 0;
2179 s->node[node] = n;
2180 init_kmem_cache_node(n);
2182 return 1;
2184 #else
2185 static void free_kmem_cache_nodes(struct kmem_cache *s)
2189 static int init_kmem_cache_nodes(struct kmem_cache *s, gfp_t gfpflags)
2191 init_kmem_cache_node(&s->local_node);
2192 return 1;
2194 #endif
2197 * calculate_sizes() determines the order and the distribution of data within
2198 * a slab object.
2200 static int calculate_sizes(struct kmem_cache *s, int forced_order)
2202 unsigned long flags = s->flags;
2203 unsigned long size = s->objsize;
2204 unsigned long align = s->align;
2205 int order;
2208 * Round up object size to the next word boundary. We can only
2209 * place the free pointer at word boundaries and this determines
2210 * the possible location of the free pointer.
2212 size = ALIGN(size, sizeof(void *));
2214 #ifdef CONFIG_SLUB_DEBUG
2216 * Determine if we can poison the object itself. If the user of
2217 * the slab may touch the object after free or before allocation
2218 * then we should never poison the object itself.
2220 if ((flags & SLAB_POISON) && !(flags & SLAB_DESTROY_BY_RCU) &&
2221 !s->ctor)
2222 s->flags |= __OBJECT_POISON;
2223 else
2224 s->flags &= ~__OBJECT_POISON;
2228 * If we are Redzoning then check if there is some space between the
2229 * end of the object and the free pointer. If not then add an
2230 * additional word to have some bytes to store Redzone information.
2232 if ((flags & SLAB_RED_ZONE) && size == s->objsize)
2233 size += sizeof(void *);
2234 #endif
2237 * With that we have determined the number of bytes in actual use
2238 * by the object. This is the potential offset to the free pointer.
2240 s->inuse = size;
2242 if (((flags & (SLAB_DESTROY_BY_RCU | SLAB_POISON)) ||
2243 s->ctor)) {
2245 * Relocate free pointer after the object if it is not
2246 * permitted to overwrite the first word of the object on
2247 * kmem_cache_free.
2249 * This is the case if we do RCU, have a constructor or
2250 * destructor or are poisoning the objects.
2252 s->offset = size;
2253 size += sizeof(void *);
2256 #ifdef CONFIG_SLUB_DEBUG
2257 if (flags & SLAB_STORE_USER)
2259 * Need to store information about allocs and frees after
2260 * the object.
2262 size += 2 * sizeof(struct track);
2264 if (flags & SLAB_RED_ZONE)
2266 * Add some empty padding so that we can catch
2267 * overwrites from earlier objects rather than let
2268 * tracking information or the free pointer be
2269 * corrupted if an user writes before the start
2270 * of the object.
2272 size += sizeof(void *);
2273 #endif
2276 * Determine the alignment based on various parameters that the
2277 * user specified and the dynamic determination of cache line size
2278 * on bootup.
2280 align = calculate_alignment(flags, align, s->objsize);
2283 * SLUB stores one object immediately after another beginning from
2284 * offset 0. In order to align the objects we have to simply size
2285 * each object to conform to the alignment.
2287 size = ALIGN(size, align);
2288 s->size = size;
2289 if (forced_order >= 0)
2290 order = forced_order;
2291 else
2292 order = calculate_order(size);
2294 if (order < 0)
2295 return 0;
2297 s->allocflags = 0;
2298 if (order)
2299 s->allocflags |= __GFP_COMP;
2301 if (s->flags & SLAB_CACHE_DMA)
2302 s->allocflags |= SLUB_DMA;
2304 if (s->flags & SLAB_RECLAIM_ACCOUNT)
2305 s->allocflags |= __GFP_RECLAIMABLE;
2308 * Determine the number of objects per slab
2310 s->oo = oo_make(order, size);
2311 s->min = oo_make(get_order(size), size);
2312 if (oo_objects(s->oo) > oo_objects(s->max))
2313 s->max = s->oo;
2315 return !!oo_objects(s->oo);
2319 static int kmem_cache_open(struct kmem_cache *s, gfp_t gfpflags,
2320 const char *name, size_t size,
2321 size_t align, unsigned long flags,
2322 void (*ctor)(struct kmem_cache *, void *))
2324 memset(s, 0, kmem_size);
2325 s->name = name;
2326 s->ctor = ctor;
2327 s->objsize = size;
2328 s->align = align;
2329 s->flags = kmem_cache_flags(size, flags, name, ctor);
2331 if (!calculate_sizes(s, -1))
2332 goto error;
2334 s->refcount = 1;
2335 #ifdef CONFIG_NUMA
2336 s->remote_node_defrag_ratio = 100;
2337 #endif
2338 if (!init_kmem_cache_nodes(s, gfpflags & ~SLUB_DMA))
2339 goto error;
2341 if (alloc_kmem_cache_cpus(s, gfpflags & ~SLUB_DMA))
2342 return 1;
2343 free_kmem_cache_nodes(s);
2344 error:
2345 if (flags & SLAB_PANIC)
2346 panic("Cannot create slab %s size=%lu realsize=%u "
2347 "order=%u offset=%u flags=%lx\n",
2348 s->name, (unsigned long)size, s->size, oo_order(s->oo),
2349 s->offset, flags);
2350 return 0;
2354 * Check if a given pointer is valid
2356 int kmem_ptr_validate(struct kmem_cache *s, const void *object)
2358 struct page *page;
2360 page = get_object_page(object);
2362 if (!page || s != page->slab)
2363 /* No slab or wrong slab */
2364 return 0;
2366 if (!check_valid_pointer(s, page, object))
2367 return 0;
2370 * We could also check if the object is on the slabs freelist.
2371 * But this would be too expensive and it seems that the main
2372 * purpose of kmem_ptr_valid() is to check if the object belongs
2373 * to a certain slab.
2375 return 1;
2377 EXPORT_SYMBOL(kmem_ptr_validate);
2380 * Determine the size of a slab object
2382 unsigned int kmem_cache_size(struct kmem_cache *s)
2384 return s->objsize;
2386 EXPORT_SYMBOL(kmem_cache_size);
2388 const char *kmem_cache_name(struct kmem_cache *s)
2390 return s->name;
2392 EXPORT_SYMBOL(kmem_cache_name);
2394 static void list_slab_objects(struct kmem_cache *s, struct page *page,
2395 const char *text)
2397 #ifdef CONFIG_SLUB_DEBUG
2398 void *addr = page_address(page);
2399 void *p;
2400 DECLARE_BITMAP(map, page->objects);
2402 bitmap_zero(map, page->objects);
2403 slab_err(s, page, "%s", text);
2404 slab_lock(page);
2405 for_each_free_object(p, s, page->freelist)
2406 set_bit(slab_index(p, s, addr), map);
2408 for_each_object(p, s, addr, page->objects) {
2410 if (!test_bit(slab_index(p, s, addr), map)) {
2411 printk(KERN_ERR "INFO: Object 0x%p @offset=%tu\n",
2412 p, p - addr);
2413 print_tracking(s, p);
2416 slab_unlock(page);
2417 #endif
2421 * Attempt to free all partial slabs on a node.
2423 static void free_partial(struct kmem_cache *s, struct kmem_cache_node *n)
2425 unsigned long flags;
2426 struct page *page, *h;
2428 spin_lock_irqsave(&n->list_lock, flags);
2429 list_for_each_entry_safe(page, h, &n->partial, lru) {
2430 if (!page->inuse) {
2431 list_del(&page->lru);
2432 discard_slab(s, page);
2433 n->nr_partial--;
2434 } else {
2435 list_slab_objects(s, page,
2436 "Objects remaining on kmem_cache_close()");
2439 spin_unlock_irqrestore(&n->list_lock, flags);
2443 * Release all resources used by a slab cache.
2445 static inline int kmem_cache_close(struct kmem_cache *s)
2447 int node;
2449 flush_all(s);
2451 /* Attempt to free all objects */
2452 free_kmem_cache_cpus(s);
2453 for_each_node_state(node, N_NORMAL_MEMORY) {
2454 struct kmem_cache_node *n = get_node(s, node);
2456 free_partial(s, n);
2457 if (n->nr_partial || slabs_node(s, node))
2458 return 1;
2460 free_kmem_cache_nodes(s);
2461 return 0;
2465 * Close a cache and release the kmem_cache structure
2466 * (must be used for caches created using kmem_cache_create)
2468 void kmem_cache_destroy(struct kmem_cache *s)
2470 down_write(&slub_lock);
2471 s->refcount--;
2472 if (!s->refcount) {
2473 list_del(&s->list);
2474 up_write(&slub_lock);
2475 if (kmem_cache_close(s)) {
2476 printk(KERN_ERR "SLUB %s: %s called for cache that "
2477 "still has objects.\n", s->name, __func__);
2478 dump_stack();
2480 sysfs_slab_remove(s);
2481 } else
2482 up_write(&slub_lock);
2484 EXPORT_SYMBOL(kmem_cache_destroy);
2486 /********************************************************************
2487 * Kmalloc subsystem
2488 *******************************************************************/
2490 struct kmem_cache kmalloc_caches[PAGE_SHIFT + 1] __cacheline_aligned;
2491 EXPORT_SYMBOL(kmalloc_caches);
2493 static int __init setup_slub_min_order(char *str)
2495 get_option(&str, &slub_min_order);
2497 return 1;
2500 __setup("slub_min_order=", setup_slub_min_order);
2502 static int __init setup_slub_max_order(char *str)
2504 get_option(&str, &slub_max_order);
2506 return 1;
2509 __setup("slub_max_order=", setup_slub_max_order);
2511 static int __init setup_slub_min_objects(char *str)
2513 get_option(&str, &slub_min_objects);
2515 return 1;
2518 __setup("slub_min_objects=", setup_slub_min_objects);
2520 static int __init setup_slub_nomerge(char *str)
2522 slub_nomerge = 1;
2523 return 1;
2526 __setup("slub_nomerge", setup_slub_nomerge);
2528 static struct kmem_cache *create_kmalloc_cache(struct kmem_cache *s,
2529 const char *name, int size, gfp_t gfp_flags)
2531 unsigned int flags = 0;
2533 if (gfp_flags & SLUB_DMA)
2534 flags = SLAB_CACHE_DMA;
2536 down_write(&slub_lock);
2537 if (!kmem_cache_open(s, gfp_flags, name, size, ARCH_KMALLOC_MINALIGN,
2538 flags, NULL))
2539 goto panic;
2541 list_add(&s->list, &slab_caches);
2542 up_write(&slub_lock);
2543 if (sysfs_slab_add(s))
2544 goto panic;
2545 return s;
2547 panic:
2548 panic("Creation of kmalloc slab %s size=%d failed.\n", name, size);
2551 #ifdef CONFIG_ZONE_DMA
2552 static struct kmem_cache *kmalloc_caches_dma[PAGE_SHIFT + 1];
2554 static void sysfs_add_func(struct work_struct *w)
2556 struct kmem_cache *s;
2558 down_write(&slub_lock);
2559 list_for_each_entry(s, &slab_caches, list) {
2560 if (s->flags & __SYSFS_ADD_DEFERRED) {
2561 s->flags &= ~__SYSFS_ADD_DEFERRED;
2562 sysfs_slab_add(s);
2565 up_write(&slub_lock);
2568 static DECLARE_WORK(sysfs_add_work, sysfs_add_func);
2570 static noinline struct kmem_cache *dma_kmalloc_cache(int index, gfp_t flags)
2572 struct kmem_cache *s;
2573 char *text;
2574 size_t realsize;
2576 s = kmalloc_caches_dma[index];
2577 if (s)
2578 return s;
2580 /* Dynamically create dma cache */
2581 if (flags & __GFP_WAIT)
2582 down_write(&slub_lock);
2583 else {
2584 if (!down_write_trylock(&slub_lock))
2585 goto out;
2588 if (kmalloc_caches_dma[index])
2589 goto unlock_out;
2591 realsize = kmalloc_caches[index].objsize;
2592 text = kasprintf(flags & ~SLUB_DMA, "kmalloc_dma-%d",
2593 (unsigned int)realsize);
2594 s = kmalloc(kmem_size, flags & ~SLUB_DMA);
2596 if (!s || !text || !kmem_cache_open(s, flags, text,
2597 realsize, ARCH_KMALLOC_MINALIGN,
2598 SLAB_CACHE_DMA|__SYSFS_ADD_DEFERRED, NULL)) {
2599 kfree(s);
2600 kfree(text);
2601 goto unlock_out;
2604 list_add(&s->list, &slab_caches);
2605 kmalloc_caches_dma[index] = s;
2607 schedule_work(&sysfs_add_work);
2609 unlock_out:
2610 up_write(&slub_lock);
2611 out:
2612 return kmalloc_caches_dma[index];
2614 #endif
2617 * Conversion table for small slabs sizes / 8 to the index in the
2618 * kmalloc array. This is necessary for slabs < 192 since we have non power
2619 * of two cache sizes there. The size of larger slabs can be determined using
2620 * fls.
2622 static s8 size_index[24] = {
2623 3, /* 8 */
2624 4, /* 16 */
2625 5, /* 24 */
2626 5, /* 32 */
2627 6, /* 40 */
2628 6, /* 48 */
2629 6, /* 56 */
2630 6, /* 64 */
2631 1, /* 72 */
2632 1, /* 80 */
2633 1, /* 88 */
2634 1, /* 96 */
2635 7, /* 104 */
2636 7, /* 112 */
2637 7, /* 120 */
2638 7, /* 128 */
2639 2, /* 136 */
2640 2, /* 144 */
2641 2, /* 152 */
2642 2, /* 160 */
2643 2, /* 168 */
2644 2, /* 176 */
2645 2, /* 184 */
2646 2 /* 192 */
2649 static struct kmem_cache *get_slab(size_t size, gfp_t flags)
2651 int index;
2653 if (size <= 192) {
2654 if (!size)
2655 return ZERO_SIZE_PTR;
2657 index = size_index[(size - 1) / 8];
2658 } else
2659 index = fls(size - 1);
2661 #ifdef CONFIG_ZONE_DMA
2662 if (unlikely((flags & SLUB_DMA)))
2663 return dma_kmalloc_cache(index, flags);
2665 #endif
2666 return &kmalloc_caches[index];
2669 void *__kmalloc(size_t size, gfp_t flags)
2671 struct kmem_cache *s;
2673 if (unlikely(size > PAGE_SIZE))
2674 return kmalloc_large(size, flags);
2676 s = get_slab(size, flags);
2678 if (unlikely(ZERO_OR_NULL_PTR(s)))
2679 return s;
2681 return slab_alloc(s, flags, -1, __builtin_return_address(0));
2683 EXPORT_SYMBOL(__kmalloc);
2685 static void *kmalloc_large_node(size_t size, gfp_t flags, int node)
2687 struct page *page = alloc_pages_node(node, flags | __GFP_COMP,
2688 get_order(size));
2690 if (page)
2691 return page_address(page);
2692 else
2693 return NULL;
2696 #ifdef CONFIG_NUMA
2697 void *__kmalloc_node(size_t size, gfp_t flags, int node)
2699 struct kmem_cache *s;
2701 if (unlikely(size > PAGE_SIZE))
2702 return kmalloc_large_node(size, flags, node);
2704 s = get_slab(size, flags);
2706 if (unlikely(ZERO_OR_NULL_PTR(s)))
2707 return s;
2709 return slab_alloc(s, flags, node, __builtin_return_address(0));
2711 EXPORT_SYMBOL(__kmalloc_node);
2712 #endif
2714 size_t ksize(const void *object)
2716 struct page *page;
2717 struct kmem_cache *s;
2719 if (unlikely(object == ZERO_SIZE_PTR))
2720 return 0;
2722 page = virt_to_head_page(object);
2724 if (unlikely(!PageSlab(page)))
2725 return PAGE_SIZE << compound_order(page);
2727 s = page->slab;
2729 #ifdef CONFIG_SLUB_DEBUG
2731 * Debugging requires use of the padding between object
2732 * and whatever may come after it.
2734 if (s->flags & (SLAB_RED_ZONE | SLAB_POISON))
2735 return s->objsize;
2737 #endif
2739 * If we have the need to store the freelist pointer
2740 * back there or track user information then we can
2741 * only use the space before that information.
2743 if (s->flags & (SLAB_DESTROY_BY_RCU | SLAB_STORE_USER))
2744 return s->inuse;
2746 * Else we can use all the padding etc for the allocation
2748 return s->size;
2750 EXPORT_SYMBOL(ksize);
2752 void kfree(const void *x)
2754 struct page *page;
2755 void *object = (void *)x;
2757 if (unlikely(ZERO_OR_NULL_PTR(x)))
2758 return;
2760 page = virt_to_head_page(x);
2761 if (unlikely(!PageSlab(page))) {
2762 put_page(page);
2763 return;
2765 slab_free(page->slab, page, object, __builtin_return_address(0));
2767 EXPORT_SYMBOL(kfree);
2770 * kmem_cache_shrink removes empty slabs from the partial lists and sorts
2771 * the remaining slabs by the number of items in use. The slabs with the
2772 * most items in use come first. New allocations will then fill those up
2773 * and thus they can be removed from the partial lists.
2775 * The slabs with the least items are placed last. This results in them
2776 * being allocated from last increasing the chance that the last objects
2777 * are freed in them.
2779 int kmem_cache_shrink(struct kmem_cache *s)
2781 int node;
2782 int i;
2783 struct kmem_cache_node *n;
2784 struct page *page;
2785 struct page *t;
2786 int objects = oo_objects(s->max);
2787 struct list_head *slabs_by_inuse =
2788 kmalloc(sizeof(struct list_head) * objects, GFP_KERNEL);
2789 unsigned long flags;
2791 if (!slabs_by_inuse)
2792 return -ENOMEM;
2794 flush_all(s);
2795 for_each_node_state(node, N_NORMAL_MEMORY) {
2796 n = get_node(s, node);
2798 if (!n->nr_partial)
2799 continue;
2801 for (i = 0; i < objects; i++)
2802 INIT_LIST_HEAD(slabs_by_inuse + i);
2804 spin_lock_irqsave(&n->list_lock, flags);
2807 * Build lists indexed by the items in use in each slab.
2809 * Note that concurrent frees may occur while we hold the
2810 * list_lock. page->inuse here is the upper limit.
2812 list_for_each_entry_safe(page, t, &n->partial, lru) {
2813 if (!page->inuse && slab_trylock(page)) {
2815 * Must hold slab lock here because slab_free
2816 * may have freed the last object and be
2817 * waiting to release the slab.
2819 list_del(&page->lru);
2820 n->nr_partial--;
2821 slab_unlock(page);
2822 discard_slab(s, page);
2823 } else {
2824 list_move(&page->lru,
2825 slabs_by_inuse + page->inuse);
2830 * Rebuild the partial list with the slabs filled up most
2831 * first and the least used slabs at the end.
2833 for (i = objects - 1; i >= 0; i--)
2834 list_splice(slabs_by_inuse + i, n->partial.prev);
2836 spin_unlock_irqrestore(&n->list_lock, flags);
2839 kfree(slabs_by_inuse);
2840 return 0;
2842 EXPORT_SYMBOL(kmem_cache_shrink);
2844 #if defined(CONFIG_NUMA) && defined(CONFIG_MEMORY_HOTPLUG)
2845 static int slab_mem_going_offline_callback(void *arg)
2847 struct kmem_cache *s;
2849 down_read(&slub_lock);
2850 list_for_each_entry(s, &slab_caches, list)
2851 kmem_cache_shrink(s);
2852 up_read(&slub_lock);
2854 return 0;
2857 static void slab_mem_offline_callback(void *arg)
2859 struct kmem_cache_node *n;
2860 struct kmem_cache *s;
2861 struct memory_notify *marg = arg;
2862 int offline_node;
2864 offline_node = marg->status_change_nid;
2867 * If the node still has available memory. we need kmem_cache_node
2868 * for it yet.
2870 if (offline_node < 0)
2871 return;
2873 down_read(&slub_lock);
2874 list_for_each_entry(s, &slab_caches, list) {
2875 n = get_node(s, offline_node);
2876 if (n) {
2878 * if n->nr_slabs > 0, slabs still exist on the node
2879 * that is going down. We were unable to free them,
2880 * and offline_pages() function shoudn't call this
2881 * callback. So, we must fail.
2883 BUG_ON(slabs_node(s, offline_node));
2885 s->node[offline_node] = NULL;
2886 kmem_cache_free(kmalloc_caches, n);
2889 up_read(&slub_lock);
2892 static int slab_mem_going_online_callback(void *arg)
2894 struct kmem_cache_node *n;
2895 struct kmem_cache *s;
2896 struct memory_notify *marg = arg;
2897 int nid = marg->status_change_nid;
2898 int ret = 0;
2901 * If the node's memory is already available, then kmem_cache_node is
2902 * already created. Nothing to do.
2904 if (nid < 0)
2905 return 0;
2908 * We are bringing a node online. No memory is availabe yet. We must
2909 * allocate a kmem_cache_node structure in order to bring the node
2910 * online.
2912 down_read(&slub_lock);
2913 list_for_each_entry(s, &slab_caches, list) {
2915 * XXX: kmem_cache_alloc_node will fallback to other nodes
2916 * since memory is not yet available from the node that
2917 * is brought up.
2919 n = kmem_cache_alloc(kmalloc_caches, GFP_KERNEL);
2920 if (!n) {
2921 ret = -ENOMEM;
2922 goto out;
2924 init_kmem_cache_node(n);
2925 s->node[nid] = n;
2927 out:
2928 up_read(&slub_lock);
2929 return ret;
2932 static int slab_memory_callback(struct notifier_block *self,
2933 unsigned long action, void *arg)
2935 int ret = 0;
2937 switch (action) {
2938 case MEM_GOING_ONLINE:
2939 ret = slab_mem_going_online_callback(arg);
2940 break;
2941 case MEM_GOING_OFFLINE:
2942 ret = slab_mem_going_offline_callback(arg);
2943 break;
2944 case MEM_OFFLINE:
2945 case MEM_CANCEL_ONLINE:
2946 slab_mem_offline_callback(arg);
2947 break;
2948 case MEM_ONLINE:
2949 case MEM_CANCEL_OFFLINE:
2950 break;
2953 ret = notifier_from_errno(ret);
2954 return ret;
2957 #endif /* CONFIG_MEMORY_HOTPLUG */
2959 /********************************************************************
2960 * Basic setup of slabs
2961 *******************************************************************/
2963 void __init kmem_cache_init(void)
2965 int i;
2966 int caches = 0;
2968 init_alloc_cpu();
2970 #ifdef CONFIG_NUMA
2972 * Must first have the slab cache available for the allocations of the
2973 * struct kmem_cache_node's. There is special bootstrap code in
2974 * kmem_cache_open for slab_state == DOWN.
2976 create_kmalloc_cache(&kmalloc_caches[0], "kmem_cache_node",
2977 sizeof(struct kmem_cache_node), GFP_KERNEL);
2978 kmalloc_caches[0].refcount = -1;
2979 caches++;
2981 hotplug_memory_notifier(slab_memory_callback, SLAB_CALLBACK_PRI);
2982 #endif
2984 /* Able to allocate the per node structures */
2985 slab_state = PARTIAL;
2987 /* Caches that are not of the two-to-the-power-of size */
2988 if (KMALLOC_MIN_SIZE <= 64) {
2989 create_kmalloc_cache(&kmalloc_caches[1],
2990 "kmalloc-96", 96, GFP_KERNEL);
2991 caches++;
2993 if (KMALLOC_MIN_SIZE <= 128) {
2994 create_kmalloc_cache(&kmalloc_caches[2],
2995 "kmalloc-192", 192, GFP_KERNEL);
2996 caches++;
2999 for (i = KMALLOC_SHIFT_LOW; i <= PAGE_SHIFT; i++) {
3000 create_kmalloc_cache(&kmalloc_caches[i],
3001 "kmalloc", 1 << i, GFP_KERNEL);
3002 caches++;
3007 * Patch up the size_index table if we have strange large alignment
3008 * requirements for the kmalloc array. This is only the case for
3009 * MIPS it seems. The standard arches will not generate any code here.
3011 * Largest permitted alignment is 256 bytes due to the way we
3012 * handle the index determination for the smaller caches.
3014 * Make sure that nothing crazy happens if someone starts tinkering
3015 * around with ARCH_KMALLOC_MINALIGN
3017 BUILD_BUG_ON(KMALLOC_MIN_SIZE > 256 ||
3018 (KMALLOC_MIN_SIZE & (KMALLOC_MIN_SIZE - 1)));
3020 for (i = 8; i < KMALLOC_MIN_SIZE; i += 8)
3021 size_index[(i - 1) / 8] = KMALLOC_SHIFT_LOW;
3023 slab_state = UP;
3025 /* Provide the correct kmalloc names now that the caches are up */
3026 for (i = KMALLOC_SHIFT_LOW; i <= PAGE_SHIFT; i++)
3027 kmalloc_caches[i]. name =
3028 kasprintf(GFP_KERNEL, "kmalloc-%d", 1 << i);
3030 #ifdef CONFIG_SMP
3031 register_cpu_notifier(&slab_notifier);
3032 kmem_size = offsetof(struct kmem_cache, cpu_slab) +
3033 nr_cpu_ids * sizeof(struct kmem_cache_cpu *);
3034 #else
3035 kmem_size = sizeof(struct kmem_cache);
3036 #endif
3038 printk(KERN_INFO
3039 "SLUB: Genslabs=%d, HWalign=%d, Order=%d-%d, MinObjects=%d,"
3040 " CPUs=%d, Nodes=%d\n",
3041 caches, cache_line_size(),
3042 slub_min_order, slub_max_order, slub_min_objects,
3043 nr_cpu_ids, nr_node_ids);
3047 * Find a mergeable slab cache
3049 static int slab_unmergeable(struct kmem_cache *s)
3051 if (slub_nomerge || (s->flags & SLUB_NEVER_MERGE))
3052 return 1;
3054 if (s->ctor)
3055 return 1;
3058 * We may have set a slab to be unmergeable during bootstrap.
3060 if (s->refcount < 0)
3061 return 1;
3063 return 0;
3066 static struct kmem_cache *find_mergeable(size_t size,
3067 size_t align, unsigned long flags, const char *name,
3068 void (*ctor)(struct kmem_cache *, void *))
3070 struct kmem_cache *s;
3072 if (slub_nomerge || (flags & SLUB_NEVER_MERGE))
3073 return NULL;
3075 if (ctor)
3076 return NULL;
3078 size = ALIGN(size, sizeof(void *));
3079 align = calculate_alignment(flags, align, size);
3080 size = ALIGN(size, align);
3081 flags = kmem_cache_flags(size, flags, name, NULL);
3083 list_for_each_entry(s, &slab_caches, list) {
3084 if (slab_unmergeable(s))
3085 continue;
3087 if (size > s->size)
3088 continue;
3090 if ((flags & SLUB_MERGE_SAME) != (s->flags & SLUB_MERGE_SAME))
3091 continue;
3093 * Check if alignment is compatible.
3094 * Courtesy of Adrian Drzewiecki
3096 if ((s->size & ~(align - 1)) != s->size)
3097 continue;
3099 if (s->size - size >= sizeof(void *))
3100 continue;
3102 return s;
3104 return NULL;
3107 struct kmem_cache *kmem_cache_create(const char *name, size_t size,
3108 size_t align, unsigned long flags,
3109 void (*ctor)(struct kmem_cache *, void *))
3111 struct kmem_cache *s;
3113 down_write(&slub_lock);
3114 s = find_mergeable(size, align, flags, name, ctor);
3115 if (s) {
3116 int cpu;
3118 s->refcount++;
3120 * Adjust the object sizes so that we clear
3121 * the complete object on kzalloc.
3123 s->objsize = max(s->objsize, (int)size);
3126 * And then we need to update the object size in the
3127 * per cpu structures
3129 for_each_online_cpu(cpu)
3130 get_cpu_slab(s, cpu)->objsize = s->objsize;
3132 s->inuse = max_t(int, s->inuse, ALIGN(size, sizeof(void *)));
3133 up_write(&slub_lock);
3135 if (sysfs_slab_alias(s, name))
3136 goto err;
3137 return s;
3140 s = kmalloc(kmem_size, GFP_KERNEL);
3141 if (s) {
3142 if (kmem_cache_open(s, GFP_KERNEL, name,
3143 size, align, flags, ctor)) {
3144 list_add(&s->list, &slab_caches);
3145 up_write(&slub_lock);
3146 if (sysfs_slab_add(s))
3147 goto err;
3148 return s;
3150 kfree(s);
3152 up_write(&slub_lock);
3154 err:
3155 if (flags & SLAB_PANIC)
3156 panic("Cannot create slabcache %s\n", name);
3157 else
3158 s = NULL;
3159 return s;
3161 EXPORT_SYMBOL(kmem_cache_create);
3163 #ifdef CONFIG_SMP
3165 * Use the cpu notifier to insure that the cpu slabs are flushed when
3166 * necessary.
3168 static int __cpuinit slab_cpuup_callback(struct notifier_block *nfb,
3169 unsigned long action, void *hcpu)
3171 long cpu = (long)hcpu;
3172 struct kmem_cache *s;
3173 unsigned long flags;
3175 switch (action) {
3176 case CPU_UP_PREPARE:
3177 case CPU_UP_PREPARE_FROZEN:
3178 init_alloc_cpu_cpu(cpu);
3179 down_read(&slub_lock);
3180 list_for_each_entry(s, &slab_caches, list)
3181 s->cpu_slab[cpu] = alloc_kmem_cache_cpu(s, cpu,
3182 GFP_KERNEL);
3183 up_read(&slub_lock);
3184 break;
3186 case CPU_UP_CANCELED:
3187 case CPU_UP_CANCELED_FROZEN:
3188 case CPU_DEAD:
3189 case CPU_DEAD_FROZEN:
3190 down_read(&slub_lock);
3191 list_for_each_entry(s, &slab_caches, list) {
3192 struct kmem_cache_cpu *c = get_cpu_slab(s, cpu);
3194 local_irq_save(flags);
3195 __flush_cpu_slab(s, cpu);
3196 local_irq_restore(flags);
3197 free_kmem_cache_cpu(c, cpu);
3198 s->cpu_slab[cpu] = NULL;
3200 up_read(&slub_lock);
3201 break;
3202 default:
3203 break;
3205 return NOTIFY_OK;
3208 static struct notifier_block __cpuinitdata slab_notifier = {
3209 .notifier_call = slab_cpuup_callback
3212 #endif
3214 void *__kmalloc_track_caller(size_t size, gfp_t gfpflags, void *caller)
3216 struct kmem_cache *s;
3218 if (unlikely(size > PAGE_SIZE))
3219 return kmalloc_large(size, gfpflags);
3221 s = get_slab(size, gfpflags);
3223 if (unlikely(ZERO_OR_NULL_PTR(s)))
3224 return s;
3226 return slab_alloc(s, gfpflags, -1, caller);
3229 void *__kmalloc_node_track_caller(size_t size, gfp_t gfpflags,
3230 int node, void *caller)
3232 struct kmem_cache *s;
3234 if (unlikely(size > PAGE_SIZE))
3235 return kmalloc_large_node(size, gfpflags, node);
3237 s = get_slab(size, gfpflags);
3239 if (unlikely(ZERO_OR_NULL_PTR(s)))
3240 return s;
3242 return slab_alloc(s, gfpflags, node, caller);
3245 #if (defined(CONFIG_SYSFS) && defined(CONFIG_SLUB_DEBUG)) || defined(CONFIG_SLABINFO)
3246 static unsigned long count_partial(struct kmem_cache_node *n,
3247 int (*get_count)(struct page *))
3249 unsigned long flags;
3250 unsigned long x = 0;
3251 struct page *page;
3253 spin_lock_irqsave(&n->list_lock, flags);
3254 list_for_each_entry(page, &n->partial, lru)
3255 x += get_count(page);
3256 spin_unlock_irqrestore(&n->list_lock, flags);
3257 return x;
3260 static int count_inuse(struct page *page)
3262 return page->inuse;
3265 static int count_total(struct page *page)
3267 return page->objects;
3270 static int count_free(struct page *page)
3272 return page->objects - page->inuse;
3274 #endif
3276 #if defined(CONFIG_SYSFS) && defined(CONFIG_SLUB_DEBUG)
3277 static int validate_slab(struct kmem_cache *s, struct page *page,
3278 unsigned long *map)
3280 void *p;
3281 void *addr = page_address(page);
3283 if (!check_slab(s, page) ||
3284 !on_freelist(s, page, NULL))
3285 return 0;
3287 /* Now we know that a valid freelist exists */
3288 bitmap_zero(map, page->objects);
3290 for_each_free_object(p, s, page->freelist) {
3291 set_bit(slab_index(p, s, addr), map);
3292 if (!check_object(s, page, p, 0))
3293 return 0;
3296 for_each_object(p, s, addr, page->objects)
3297 if (!test_bit(slab_index(p, s, addr), map))
3298 if (!check_object(s, page, p, 1))
3299 return 0;
3300 return 1;
3303 static void validate_slab_slab(struct kmem_cache *s, struct page *page,
3304 unsigned long *map)
3306 if (slab_trylock(page)) {
3307 validate_slab(s, page, map);
3308 slab_unlock(page);
3309 } else
3310 printk(KERN_INFO "SLUB %s: Skipped busy slab 0x%p\n",
3311 s->name, page);
3313 if (s->flags & DEBUG_DEFAULT_FLAGS) {
3314 if (!SlabDebug(page))
3315 printk(KERN_ERR "SLUB %s: SlabDebug not set "
3316 "on slab 0x%p\n", s->name, page);
3317 } else {
3318 if (SlabDebug(page))
3319 printk(KERN_ERR "SLUB %s: SlabDebug set on "
3320 "slab 0x%p\n", s->name, page);
3324 static int validate_slab_node(struct kmem_cache *s,
3325 struct kmem_cache_node *n, unsigned long *map)
3327 unsigned long count = 0;
3328 struct page *page;
3329 unsigned long flags;
3331 spin_lock_irqsave(&n->list_lock, flags);
3333 list_for_each_entry(page, &n->partial, lru) {
3334 validate_slab_slab(s, page, map);
3335 count++;
3337 if (count != n->nr_partial)
3338 printk(KERN_ERR "SLUB %s: %ld partial slabs counted but "
3339 "counter=%ld\n", s->name, count, n->nr_partial);
3341 if (!(s->flags & SLAB_STORE_USER))
3342 goto out;
3344 list_for_each_entry(page, &n->full, lru) {
3345 validate_slab_slab(s, page, map);
3346 count++;
3348 if (count != atomic_long_read(&n->nr_slabs))
3349 printk(KERN_ERR "SLUB: %s %ld slabs counted but "
3350 "counter=%ld\n", s->name, count,
3351 atomic_long_read(&n->nr_slabs));
3353 out:
3354 spin_unlock_irqrestore(&n->list_lock, flags);
3355 return count;
3358 static long validate_slab_cache(struct kmem_cache *s)
3360 int node;
3361 unsigned long count = 0;
3362 unsigned long *map = kmalloc(BITS_TO_LONGS(oo_objects(s->max)) *
3363 sizeof(unsigned long), GFP_KERNEL);
3365 if (!map)
3366 return -ENOMEM;
3368 flush_all(s);
3369 for_each_node_state(node, N_NORMAL_MEMORY) {
3370 struct kmem_cache_node *n = get_node(s, node);
3372 count += validate_slab_node(s, n, map);
3374 kfree(map);
3375 return count;
3378 #ifdef SLUB_RESILIENCY_TEST
3379 static void resiliency_test(void)
3381 u8 *p;
3383 printk(KERN_ERR "SLUB resiliency testing\n");
3384 printk(KERN_ERR "-----------------------\n");
3385 printk(KERN_ERR "A. Corruption after allocation\n");
3387 p = kzalloc(16, GFP_KERNEL);
3388 p[16] = 0x12;
3389 printk(KERN_ERR "\n1. kmalloc-16: Clobber Redzone/next pointer"
3390 " 0x12->0x%p\n\n", p + 16);
3392 validate_slab_cache(kmalloc_caches + 4);
3394 /* Hmmm... The next two are dangerous */
3395 p = kzalloc(32, GFP_KERNEL);
3396 p[32 + sizeof(void *)] = 0x34;
3397 printk(KERN_ERR "\n2. kmalloc-32: Clobber next pointer/next slab"
3398 " 0x34 -> -0x%p\n", p);
3399 printk(KERN_ERR
3400 "If allocated object is overwritten then not detectable\n\n");
3402 validate_slab_cache(kmalloc_caches + 5);
3403 p = kzalloc(64, GFP_KERNEL);
3404 p += 64 + (get_cycles() & 0xff) * sizeof(void *);
3405 *p = 0x56;
3406 printk(KERN_ERR "\n3. kmalloc-64: corrupting random byte 0x56->0x%p\n",
3408 printk(KERN_ERR
3409 "If allocated object is overwritten then not detectable\n\n");
3410 validate_slab_cache(kmalloc_caches + 6);
3412 printk(KERN_ERR "\nB. Corruption after free\n");
3413 p = kzalloc(128, GFP_KERNEL);
3414 kfree(p);
3415 *p = 0x78;
3416 printk(KERN_ERR "1. kmalloc-128: Clobber first word 0x78->0x%p\n\n", p);
3417 validate_slab_cache(kmalloc_caches + 7);
3419 p = kzalloc(256, GFP_KERNEL);
3420 kfree(p);
3421 p[50] = 0x9a;
3422 printk(KERN_ERR "\n2. kmalloc-256: Clobber 50th byte 0x9a->0x%p\n\n",
3424 validate_slab_cache(kmalloc_caches + 8);
3426 p = kzalloc(512, GFP_KERNEL);
3427 kfree(p);
3428 p[512] = 0xab;
3429 printk(KERN_ERR "\n3. kmalloc-512: Clobber redzone 0xab->0x%p\n\n", p);
3430 validate_slab_cache(kmalloc_caches + 9);
3432 #else
3433 static void resiliency_test(void) {};
3434 #endif
3437 * Generate lists of code addresses where slabcache objects are allocated
3438 * and freed.
3441 struct location {
3442 unsigned long count;
3443 void *addr;
3444 long long sum_time;
3445 long min_time;
3446 long max_time;
3447 long min_pid;
3448 long max_pid;
3449 cpumask_t cpus;
3450 nodemask_t nodes;
3453 struct loc_track {
3454 unsigned long max;
3455 unsigned long count;
3456 struct location *loc;
3459 static void free_loc_track(struct loc_track *t)
3461 if (t->max)
3462 free_pages((unsigned long)t->loc,
3463 get_order(sizeof(struct location) * t->max));
3466 static int alloc_loc_track(struct loc_track *t, unsigned long max, gfp_t flags)
3468 struct location *l;
3469 int order;
3471 order = get_order(sizeof(struct location) * max);
3473 l = (void *)__get_free_pages(flags, order);
3474 if (!l)
3475 return 0;
3477 if (t->count) {
3478 memcpy(l, t->loc, sizeof(struct location) * t->count);
3479 free_loc_track(t);
3481 t->max = max;
3482 t->loc = l;
3483 return 1;
3486 static int add_location(struct loc_track *t, struct kmem_cache *s,
3487 const struct track *track)
3489 long start, end, pos;
3490 struct location *l;
3491 void *caddr;
3492 unsigned long age = jiffies - track->when;
3494 start = -1;
3495 end = t->count;
3497 for ( ; ; ) {
3498 pos = start + (end - start + 1) / 2;
3501 * There is nothing at "end". If we end up there
3502 * we need to add something to before end.
3504 if (pos == end)
3505 break;
3507 caddr = t->loc[pos].addr;
3508 if (track->addr == caddr) {
3510 l = &t->loc[pos];
3511 l->count++;
3512 if (track->when) {
3513 l->sum_time += age;
3514 if (age < l->min_time)
3515 l->min_time = age;
3516 if (age > l->max_time)
3517 l->max_time = age;
3519 if (track->pid < l->min_pid)
3520 l->min_pid = track->pid;
3521 if (track->pid > l->max_pid)
3522 l->max_pid = track->pid;
3524 cpu_set(track->cpu, l->cpus);
3526 node_set(page_to_nid(virt_to_page(track)), l->nodes);
3527 return 1;
3530 if (track->addr < caddr)
3531 end = pos;
3532 else
3533 start = pos;
3537 * Not found. Insert new tracking element.
3539 if (t->count >= t->max && !alloc_loc_track(t, 2 * t->max, GFP_ATOMIC))
3540 return 0;
3542 l = t->loc + pos;
3543 if (pos < t->count)
3544 memmove(l + 1, l,
3545 (t->count - pos) * sizeof(struct location));
3546 t->count++;
3547 l->count = 1;
3548 l->addr = track->addr;
3549 l->sum_time = age;
3550 l->min_time = age;
3551 l->max_time = age;
3552 l->min_pid = track->pid;
3553 l->max_pid = track->pid;
3554 cpus_clear(l->cpus);
3555 cpu_set(track->cpu, l->cpus);
3556 nodes_clear(l->nodes);
3557 node_set(page_to_nid(virt_to_page(track)), l->nodes);
3558 return 1;
3561 static void process_slab(struct loc_track *t, struct kmem_cache *s,
3562 struct page *page, enum track_item alloc)
3564 void *addr = page_address(page);
3565 DECLARE_BITMAP(map, page->objects);
3566 void *p;
3568 bitmap_zero(map, page->objects);
3569 for_each_free_object(p, s, page->freelist)
3570 set_bit(slab_index(p, s, addr), map);
3572 for_each_object(p, s, addr, page->objects)
3573 if (!test_bit(slab_index(p, s, addr), map))
3574 add_location(t, s, get_track(s, p, alloc));
3577 static int list_locations(struct kmem_cache *s, char *buf,
3578 enum track_item alloc)
3580 int len = 0;
3581 unsigned long i;
3582 struct loc_track t = { 0, 0, NULL };
3583 int node;
3585 if (!alloc_loc_track(&t, PAGE_SIZE / sizeof(struct location),
3586 GFP_TEMPORARY))
3587 return sprintf(buf, "Out of memory\n");
3589 /* Push back cpu slabs */
3590 flush_all(s);
3592 for_each_node_state(node, N_NORMAL_MEMORY) {
3593 struct kmem_cache_node *n = get_node(s, node);
3594 unsigned long flags;
3595 struct page *page;
3597 if (!atomic_long_read(&n->nr_slabs))
3598 continue;
3600 spin_lock_irqsave(&n->list_lock, flags);
3601 list_for_each_entry(page, &n->partial, lru)
3602 process_slab(&t, s, page, alloc);
3603 list_for_each_entry(page, &n->full, lru)
3604 process_slab(&t, s, page, alloc);
3605 spin_unlock_irqrestore(&n->list_lock, flags);
3608 for (i = 0; i < t.count; i++) {
3609 struct location *l = &t.loc[i];
3611 if (len > PAGE_SIZE - 100)
3612 break;
3613 len += sprintf(buf + len, "%7ld ", l->count);
3615 if (l->addr)
3616 len += sprint_symbol(buf + len, (unsigned long)l->addr);
3617 else
3618 len += sprintf(buf + len, "<not-available>");
3620 if (l->sum_time != l->min_time) {
3621 unsigned long remainder;
3623 len += sprintf(buf + len, " age=%ld/%ld/%ld",
3624 l->min_time,
3625 div_long_long_rem(l->sum_time, l->count, &remainder),
3626 l->max_time);
3627 } else
3628 len += sprintf(buf + len, " age=%ld",
3629 l->min_time);
3631 if (l->min_pid != l->max_pid)
3632 len += sprintf(buf + len, " pid=%ld-%ld",
3633 l->min_pid, l->max_pid);
3634 else
3635 len += sprintf(buf + len, " pid=%ld",
3636 l->min_pid);
3638 if (num_online_cpus() > 1 && !cpus_empty(l->cpus) &&
3639 len < PAGE_SIZE - 60) {
3640 len += sprintf(buf + len, " cpus=");
3641 len += cpulist_scnprintf(buf + len, PAGE_SIZE - len - 50,
3642 l->cpus);
3645 if (num_online_nodes() > 1 && !nodes_empty(l->nodes) &&
3646 len < PAGE_SIZE - 60) {
3647 len += sprintf(buf + len, " nodes=");
3648 len += nodelist_scnprintf(buf + len, PAGE_SIZE - len - 50,
3649 l->nodes);
3652 len += sprintf(buf + len, "\n");
3655 free_loc_track(&t);
3656 if (!t.count)
3657 len += sprintf(buf, "No data\n");
3658 return len;
3661 enum slab_stat_type {
3662 SL_ALL, /* All slabs */
3663 SL_PARTIAL, /* Only partially allocated slabs */
3664 SL_CPU, /* Only slabs used for cpu caches */
3665 SL_OBJECTS, /* Determine allocated objects not slabs */
3666 SL_TOTAL /* Determine object capacity not slabs */
3669 #define SO_ALL (1 << SL_ALL)
3670 #define SO_PARTIAL (1 << SL_PARTIAL)
3671 #define SO_CPU (1 << SL_CPU)
3672 #define SO_OBJECTS (1 << SL_OBJECTS)
3673 #define SO_TOTAL (1 << SL_TOTAL)
3675 static ssize_t show_slab_objects(struct kmem_cache *s,
3676 char *buf, unsigned long flags)
3678 unsigned long total = 0;
3679 int node;
3680 int x;
3681 unsigned long *nodes;
3682 unsigned long *per_cpu;
3684 nodes = kzalloc(2 * sizeof(unsigned long) * nr_node_ids, GFP_KERNEL);
3685 if (!nodes)
3686 return -ENOMEM;
3687 per_cpu = nodes + nr_node_ids;
3689 if (flags & SO_CPU) {
3690 int cpu;
3692 for_each_possible_cpu(cpu) {
3693 struct kmem_cache_cpu *c = get_cpu_slab(s, cpu);
3695 if (!c || c->node < 0)
3696 continue;
3698 if (c->page) {
3699 if (flags & SO_TOTAL)
3700 x = c->page->objects;
3701 else if (flags & SO_OBJECTS)
3702 x = c->page->inuse;
3703 else
3704 x = 1;
3706 total += x;
3707 nodes[c->node] += x;
3709 per_cpu[c->node]++;
3713 if (flags & SO_ALL) {
3714 for_each_node_state(node, N_NORMAL_MEMORY) {
3715 struct kmem_cache_node *n = get_node(s, node);
3717 if (flags & SO_TOTAL)
3718 x = atomic_long_read(&n->total_objects);
3719 else if (flags & SO_OBJECTS)
3720 x = atomic_long_read(&n->total_objects) -
3721 count_partial(n, count_free);
3723 else
3724 x = atomic_long_read(&n->nr_slabs);
3725 total += x;
3726 nodes[node] += x;
3729 } else if (flags & SO_PARTIAL) {
3730 for_each_node_state(node, N_NORMAL_MEMORY) {
3731 struct kmem_cache_node *n = get_node(s, node);
3733 if (flags & SO_TOTAL)
3734 x = count_partial(n, count_total);
3735 else if (flags & SO_OBJECTS)
3736 x = count_partial(n, count_inuse);
3737 else
3738 x = n->nr_partial;
3739 total += x;
3740 nodes[node] += x;
3743 x = sprintf(buf, "%lu", total);
3744 #ifdef CONFIG_NUMA
3745 for_each_node_state(node, N_NORMAL_MEMORY)
3746 if (nodes[node])
3747 x += sprintf(buf + x, " N%d=%lu",
3748 node, nodes[node]);
3749 #endif
3750 kfree(nodes);
3751 return x + sprintf(buf + x, "\n");
3754 static int any_slab_objects(struct kmem_cache *s)
3756 int node;
3758 for_each_online_node(node) {
3759 struct kmem_cache_node *n = get_node(s, node);
3761 if (!n)
3762 continue;
3764 if (atomic_read(&n->total_objects))
3765 return 1;
3767 return 0;
3770 #define to_slab_attr(n) container_of(n, struct slab_attribute, attr)
3771 #define to_slab(n) container_of(n, struct kmem_cache, kobj);
3773 struct slab_attribute {
3774 struct attribute attr;
3775 ssize_t (*show)(struct kmem_cache *s, char *buf);
3776 ssize_t (*store)(struct kmem_cache *s, const char *x, size_t count);
3779 #define SLAB_ATTR_RO(_name) \
3780 static struct slab_attribute _name##_attr = __ATTR_RO(_name)
3782 #define SLAB_ATTR(_name) \
3783 static struct slab_attribute _name##_attr = \
3784 __ATTR(_name, 0644, _name##_show, _name##_store)
3786 static ssize_t slab_size_show(struct kmem_cache *s, char *buf)
3788 return sprintf(buf, "%d\n", s->size);
3790 SLAB_ATTR_RO(slab_size);
3792 static ssize_t align_show(struct kmem_cache *s, char *buf)
3794 return sprintf(buf, "%d\n", s->align);
3796 SLAB_ATTR_RO(align);
3798 static ssize_t object_size_show(struct kmem_cache *s, char *buf)
3800 return sprintf(buf, "%d\n", s->objsize);
3802 SLAB_ATTR_RO(object_size);
3804 static ssize_t objs_per_slab_show(struct kmem_cache *s, char *buf)
3806 return sprintf(buf, "%d\n", oo_objects(s->oo));
3808 SLAB_ATTR_RO(objs_per_slab);
3810 static ssize_t order_store(struct kmem_cache *s,
3811 const char *buf, size_t length)
3813 int order = simple_strtoul(buf, NULL, 10);
3815 if (order > slub_max_order || order < slub_min_order)
3816 return -EINVAL;
3818 calculate_sizes(s, order);
3819 return length;
3822 static ssize_t order_show(struct kmem_cache *s, char *buf)
3824 return sprintf(buf, "%d\n", oo_order(s->oo));
3826 SLAB_ATTR(order);
3828 static ssize_t ctor_show(struct kmem_cache *s, char *buf)
3830 if (s->ctor) {
3831 int n = sprint_symbol(buf, (unsigned long)s->ctor);
3833 return n + sprintf(buf + n, "\n");
3835 return 0;
3837 SLAB_ATTR_RO(ctor);
3839 static ssize_t aliases_show(struct kmem_cache *s, char *buf)
3841 return sprintf(buf, "%d\n", s->refcount - 1);
3843 SLAB_ATTR_RO(aliases);
3845 static ssize_t slabs_show(struct kmem_cache *s, char *buf)
3847 return show_slab_objects(s, buf, SO_ALL);
3849 SLAB_ATTR_RO(slabs);
3851 static ssize_t partial_show(struct kmem_cache *s, char *buf)
3853 return show_slab_objects(s, buf, SO_PARTIAL);
3855 SLAB_ATTR_RO(partial);
3857 static ssize_t cpu_slabs_show(struct kmem_cache *s, char *buf)
3859 return show_slab_objects(s, buf, SO_CPU);
3861 SLAB_ATTR_RO(cpu_slabs);
3863 static ssize_t objects_show(struct kmem_cache *s, char *buf)
3865 return show_slab_objects(s, buf, SO_ALL|SO_OBJECTS);
3867 SLAB_ATTR_RO(objects);
3869 static ssize_t objects_partial_show(struct kmem_cache *s, char *buf)
3871 return show_slab_objects(s, buf, SO_PARTIAL|SO_OBJECTS);
3873 SLAB_ATTR_RO(objects_partial);
3875 static ssize_t total_objects_show(struct kmem_cache *s, char *buf)
3877 return show_slab_objects(s, buf, SO_ALL|SO_TOTAL);
3879 SLAB_ATTR_RO(total_objects);
3881 static ssize_t sanity_checks_show(struct kmem_cache *s, char *buf)
3883 return sprintf(buf, "%d\n", !!(s->flags & SLAB_DEBUG_FREE));
3886 static ssize_t sanity_checks_store(struct kmem_cache *s,
3887 const char *buf, size_t length)
3889 s->flags &= ~SLAB_DEBUG_FREE;
3890 if (buf[0] == '1')
3891 s->flags |= SLAB_DEBUG_FREE;
3892 return length;
3894 SLAB_ATTR(sanity_checks);
3896 static ssize_t trace_show(struct kmem_cache *s, char *buf)
3898 return sprintf(buf, "%d\n", !!(s->flags & SLAB_TRACE));
3901 static ssize_t trace_store(struct kmem_cache *s, const char *buf,
3902 size_t length)
3904 s->flags &= ~SLAB_TRACE;
3905 if (buf[0] == '1')
3906 s->flags |= SLAB_TRACE;
3907 return length;
3909 SLAB_ATTR(trace);
3911 static ssize_t reclaim_account_show(struct kmem_cache *s, char *buf)
3913 return sprintf(buf, "%d\n", !!(s->flags & SLAB_RECLAIM_ACCOUNT));
3916 static ssize_t reclaim_account_store(struct kmem_cache *s,
3917 const char *buf, size_t length)
3919 s->flags &= ~SLAB_RECLAIM_ACCOUNT;
3920 if (buf[0] == '1')
3921 s->flags |= SLAB_RECLAIM_ACCOUNT;
3922 return length;
3924 SLAB_ATTR(reclaim_account);
3926 static ssize_t hwcache_align_show(struct kmem_cache *s, char *buf)
3928 return sprintf(buf, "%d\n", !!(s->flags & SLAB_HWCACHE_ALIGN));
3930 SLAB_ATTR_RO(hwcache_align);
3932 #ifdef CONFIG_ZONE_DMA
3933 static ssize_t cache_dma_show(struct kmem_cache *s, char *buf)
3935 return sprintf(buf, "%d\n", !!(s->flags & SLAB_CACHE_DMA));
3937 SLAB_ATTR_RO(cache_dma);
3938 #endif
3940 static ssize_t destroy_by_rcu_show(struct kmem_cache *s, char *buf)
3942 return sprintf(buf, "%d\n", !!(s->flags & SLAB_DESTROY_BY_RCU));
3944 SLAB_ATTR_RO(destroy_by_rcu);
3946 static ssize_t red_zone_show(struct kmem_cache *s, char *buf)
3948 return sprintf(buf, "%d\n", !!(s->flags & SLAB_RED_ZONE));
3951 static ssize_t red_zone_store(struct kmem_cache *s,
3952 const char *buf, size_t length)
3954 if (any_slab_objects(s))
3955 return -EBUSY;
3957 s->flags &= ~SLAB_RED_ZONE;
3958 if (buf[0] == '1')
3959 s->flags |= SLAB_RED_ZONE;
3960 calculate_sizes(s, -1);
3961 return length;
3963 SLAB_ATTR(red_zone);
3965 static ssize_t poison_show(struct kmem_cache *s, char *buf)
3967 return sprintf(buf, "%d\n", !!(s->flags & SLAB_POISON));
3970 static ssize_t poison_store(struct kmem_cache *s,
3971 const char *buf, size_t length)
3973 if (any_slab_objects(s))
3974 return -EBUSY;
3976 s->flags &= ~SLAB_POISON;
3977 if (buf[0] == '1')
3978 s->flags |= SLAB_POISON;
3979 calculate_sizes(s, -1);
3980 return length;
3982 SLAB_ATTR(poison);
3984 static ssize_t store_user_show(struct kmem_cache *s, char *buf)
3986 return sprintf(buf, "%d\n", !!(s->flags & SLAB_STORE_USER));
3989 static ssize_t store_user_store(struct kmem_cache *s,
3990 const char *buf, size_t length)
3992 if (any_slab_objects(s))
3993 return -EBUSY;
3995 s->flags &= ~SLAB_STORE_USER;
3996 if (buf[0] == '1')
3997 s->flags |= SLAB_STORE_USER;
3998 calculate_sizes(s, -1);
3999 return length;
4001 SLAB_ATTR(store_user);
4003 static ssize_t validate_show(struct kmem_cache *s, char *buf)
4005 return 0;
4008 static ssize_t validate_store(struct kmem_cache *s,
4009 const char *buf, size_t length)
4011 int ret = -EINVAL;
4013 if (buf[0] == '1') {
4014 ret = validate_slab_cache(s);
4015 if (ret >= 0)
4016 ret = length;
4018 return ret;
4020 SLAB_ATTR(validate);
4022 static ssize_t shrink_show(struct kmem_cache *s, char *buf)
4024 return 0;
4027 static ssize_t shrink_store(struct kmem_cache *s,
4028 const char *buf, size_t length)
4030 if (buf[0] == '1') {
4031 int rc = kmem_cache_shrink(s);
4033 if (rc)
4034 return rc;
4035 } else
4036 return -EINVAL;
4037 return length;
4039 SLAB_ATTR(shrink);
4041 static ssize_t alloc_calls_show(struct kmem_cache *s, char *buf)
4043 if (!(s->flags & SLAB_STORE_USER))
4044 return -ENOSYS;
4045 return list_locations(s, buf, TRACK_ALLOC);
4047 SLAB_ATTR_RO(alloc_calls);
4049 static ssize_t free_calls_show(struct kmem_cache *s, char *buf)
4051 if (!(s->flags & SLAB_STORE_USER))
4052 return -ENOSYS;
4053 return list_locations(s, buf, TRACK_FREE);
4055 SLAB_ATTR_RO(free_calls);
4057 #ifdef CONFIG_NUMA
4058 static ssize_t remote_node_defrag_ratio_show(struct kmem_cache *s, char *buf)
4060 return sprintf(buf, "%d\n", s->remote_node_defrag_ratio / 10);
4063 static ssize_t remote_node_defrag_ratio_store(struct kmem_cache *s,
4064 const char *buf, size_t length)
4066 int n = simple_strtoul(buf, NULL, 10);
4068 if (n < 100)
4069 s->remote_node_defrag_ratio = n * 10;
4070 return length;
4072 SLAB_ATTR(remote_node_defrag_ratio);
4073 #endif
4075 #ifdef CONFIG_SLUB_STATS
4076 static int show_stat(struct kmem_cache *s, char *buf, enum stat_item si)
4078 unsigned long sum = 0;
4079 int cpu;
4080 int len;
4081 int *data = kmalloc(nr_cpu_ids * sizeof(int), GFP_KERNEL);
4083 if (!data)
4084 return -ENOMEM;
4086 for_each_online_cpu(cpu) {
4087 unsigned x = get_cpu_slab(s, cpu)->stat[si];
4089 data[cpu] = x;
4090 sum += x;
4093 len = sprintf(buf, "%lu", sum);
4095 #ifdef CONFIG_SMP
4096 for_each_online_cpu(cpu) {
4097 if (data[cpu] && len < PAGE_SIZE - 20)
4098 len += sprintf(buf + len, " C%d=%u", cpu, data[cpu]);
4100 #endif
4101 kfree(data);
4102 return len + sprintf(buf + len, "\n");
4105 #define STAT_ATTR(si, text) \
4106 static ssize_t text##_show(struct kmem_cache *s, char *buf) \
4108 return show_stat(s, buf, si); \
4110 SLAB_ATTR_RO(text); \
4112 STAT_ATTR(ALLOC_FASTPATH, alloc_fastpath);
4113 STAT_ATTR(ALLOC_SLOWPATH, alloc_slowpath);
4114 STAT_ATTR(FREE_FASTPATH, free_fastpath);
4115 STAT_ATTR(FREE_SLOWPATH, free_slowpath);
4116 STAT_ATTR(FREE_FROZEN, free_frozen);
4117 STAT_ATTR(FREE_ADD_PARTIAL, free_add_partial);
4118 STAT_ATTR(FREE_REMOVE_PARTIAL, free_remove_partial);
4119 STAT_ATTR(ALLOC_FROM_PARTIAL, alloc_from_partial);
4120 STAT_ATTR(ALLOC_SLAB, alloc_slab);
4121 STAT_ATTR(ALLOC_REFILL, alloc_refill);
4122 STAT_ATTR(FREE_SLAB, free_slab);
4123 STAT_ATTR(CPUSLAB_FLUSH, cpuslab_flush);
4124 STAT_ATTR(DEACTIVATE_FULL, deactivate_full);
4125 STAT_ATTR(DEACTIVATE_EMPTY, deactivate_empty);
4126 STAT_ATTR(DEACTIVATE_TO_HEAD, deactivate_to_head);
4127 STAT_ATTR(DEACTIVATE_TO_TAIL, deactivate_to_tail);
4128 STAT_ATTR(DEACTIVATE_REMOTE_FREES, deactivate_remote_frees);
4129 STAT_ATTR(ORDER_FALLBACK, order_fallback);
4130 #endif
4132 static struct attribute *slab_attrs[] = {
4133 &slab_size_attr.attr,
4134 &object_size_attr.attr,
4135 &objs_per_slab_attr.attr,
4136 &order_attr.attr,
4137 &objects_attr.attr,
4138 &objects_partial_attr.attr,
4139 &total_objects_attr.attr,
4140 &slabs_attr.attr,
4141 &partial_attr.attr,
4142 &cpu_slabs_attr.attr,
4143 &ctor_attr.attr,
4144 &aliases_attr.attr,
4145 &align_attr.attr,
4146 &sanity_checks_attr.attr,
4147 &trace_attr.attr,
4148 &hwcache_align_attr.attr,
4149 &reclaim_account_attr.attr,
4150 &destroy_by_rcu_attr.attr,
4151 &red_zone_attr.attr,
4152 &poison_attr.attr,
4153 &store_user_attr.attr,
4154 &validate_attr.attr,
4155 &shrink_attr.attr,
4156 &alloc_calls_attr.attr,
4157 &free_calls_attr.attr,
4158 #ifdef CONFIG_ZONE_DMA
4159 &cache_dma_attr.attr,
4160 #endif
4161 #ifdef CONFIG_NUMA
4162 &remote_node_defrag_ratio_attr.attr,
4163 #endif
4164 #ifdef CONFIG_SLUB_STATS
4165 &alloc_fastpath_attr.attr,
4166 &alloc_slowpath_attr.attr,
4167 &free_fastpath_attr.attr,
4168 &free_slowpath_attr.attr,
4169 &free_frozen_attr.attr,
4170 &free_add_partial_attr.attr,
4171 &free_remove_partial_attr.attr,
4172 &alloc_from_partial_attr.attr,
4173 &alloc_slab_attr.attr,
4174 &alloc_refill_attr.attr,
4175 &free_slab_attr.attr,
4176 &cpuslab_flush_attr.attr,
4177 &deactivate_full_attr.attr,
4178 &deactivate_empty_attr.attr,
4179 &deactivate_to_head_attr.attr,
4180 &deactivate_to_tail_attr.attr,
4181 &deactivate_remote_frees_attr.attr,
4182 &order_fallback_attr.attr,
4183 #endif
4184 NULL
4187 static struct attribute_group slab_attr_group = {
4188 .attrs = slab_attrs,
4191 static ssize_t slab_attr_show(struct kobject *kobj,
4192 struct attribute *attr,
4193 char *buf)
4195 struct slab_attribute *attribute;
4196 struct kmem_cache *s;
4197 int err;
4199 attribute = to_slab_attr(attr);
4200 s = to_slab(kobj);
4202 if (!attribute->show)
4203 return -EIO;
4205 err = attribute->show(s, buf);
4207 return err;
4210 static ssize_t slab_attr_store(struct kobject *kobj,
4211 struct attribute *attr,
4212 const char *buf, size_t len)
4214 struct slab_attribute *attribute;
4215 struct kmem_cache *s;
4216 int err;
4218 attribute = to_slab_attr(attr);
4219 s = to_slab(kobj);
4221 if (!attribute->store)
4222 return -EIO;
4224 err = attribute->store(s, buf, len);
4226 return err;
4229 static void kmem_cache_release(struct kobject *kobj)
4231 struct kmem_cache *s = to_slab(kobj);
4233 kfree(s);
4236 static struct sysfs_ops slab_sysfs_ops = {
4237 .show = slab_attr_show,
4238 .store = slab_attr_store,
4241 static struct kobj_type slab_ktype = {
4242 .sysfs_ops = &slab_sysfs_ops,
4243 .release = kmem_cache_release
4246 static int uevent_filter(struct kset *kset, struct kobject *kobj)
4248 struct kobj_type *ktype = get_ktype(kobj);
4250 if (ktype == &slab_ktype)
4251 return 1;
4252 return 0;
4255 static struct kset_uevent_ops slab_uevent_ops = {
4256 .filter = uevent_filter,
4259 static struct kset *slab_kset;
4261 #define ID_STR_LENGTH 64
4263 /* Create a unique string id for a slab cache:
4265 * Format :[flags-]size
4267 static char *create_unique_id(struct kmem_cache *s)
4269 char *name = kmalloc(ID_STR_LENGTH, GFP_KERNEL);
4270 char *p = name;
4272 BUG_ON(!name);
4274 *p++ = ':';
4276 * First flags affecting slabcache operations. We will only
4277 * get here for aliasable slabs so we do not need to support
4278 * too many flags. The flags here must cover all flags that
4279 * are matched during merging to guarantee that the id is
4280 * unique.
4282 if (s->flags & SLAB_CACHE_DMA)
4283 *p++ = 'd';
4284 if (s->flags & SLAB_RECLAIM_ACCOUNT)
4285 *p++ = 'a';
4286 if (s->flags & SLAB_DEBUG_FREE)
4287 *p++ = 'F';
4288 if (p != name + 1)
4289 *p++ = '-';
4290 p += sprintf(p, "%07d", s->size);
4291 BUG_ON(p > name + ID_STR_LENGTH - 1);
4292 return name;
4295 static int sysfs_slab_add(struct kmem_cache *s)
4297 int err;
4298 const char *name;
4299 int unmergeable;
4301 if (slab_state < SYSFS)
4302 /* Defer until later */
4303 return 0;
4305 unmergeable = slab_unmergeable(s);
4306 if (unmergeable) {
4308 * Slabcache can never be merged so we can use the name proper.
4309 * This is typically the case for debug situations. In that
4310 * case we can catch duplicate names easily.
4312 sysfs_remove_link(&slab_kset->kobj, s->name);
4313 name = s->name;
4314 } else {
4316 * Create a unique name for the slab as a target
4317 * for the symlinks.
4319 name = create_unique_id(s);
4322 s->kobj.kset = slab_kset;
4323 err = kobject_init_and_add(&s->kobj, &slab_ktype, NULL, name);
4324 if (err) {
4325 kobject_put(&s->kobj);
4326 return err;
4329 err = sysfs_create_group(&s->kobj, &slab_attr_group);
4330 if (err)
4331 return err;
4332 kobject_uevent(&s->kobj, KOBJ_ADD);
4333 if (!unmergeable) {
4334 /* Setup first alias */
4335 sysfs_slab_alias(s, s->name);
4336 kfree(name);
4338 return 0;
4341 static void sysfs_slab_remove(struct kmem_cache *s)
4343 kobject_uevent(&s->kobj, KOBJ_REMOVE);
4344 kobject_del(&s->kobj);
4345 kobject_put(&s->kobj);
4349 * Need to buffer aliases during bootup until sysfs becomes
4350 * available lest we loose that information.
4352 struct saved_alias {
4353 struct kmem_cache *s;
4354 const char *name;
4355 struct saved_alias *next;
4358 static struct saved_alias *alias_list;
4360 static int sysfs_slab_alias(struct kmem_cache *s, const char *name)
4362 struct saved_alias *al;
4364 if (slab_state == SYSFS) {
4366 * If we have a leftover link then remove it.
4368 sysfs_remove_link(&slab_kset->kobj, name);
4369 return sysfs_create_link(&slab_kset->kobj, &s->kobj, name);
4372 al = kmalloc(sizeof(struct saved_alias), GFP_KERNEL);
4373 if (!al)
4374 return -ENOMEM;
4376 al->s = s;
4377 al->name = name;
4378 al->next = alias_list;
4379 alias_list = al;
4380 return 0;
4383 static int __init slab_sysfs_init(void)
4385 struct kmem_cache *s;
4386 int err;
4388 slab_kset = kset_create_and_add("slab", &slab_uevent_ops, kernel_kobj);
4389 if (!slab_kset) {
4390 printk(KERN_ERR "Cannot register slab subsystem.\n");
4391 return -ENOSYS;
4394 slab_state = SYSFS;
4396 list_for_each_entry(s, &slab_caches, list) {
4397 err = sysfs_slab_add(s);
4398 if (err)
4399 printk(KERN_ERR "SLUB: Unable to add boot slab %s"
4400 " to sysfs\n", s->name);
4403 while (alias_list) {
4404 struct saved_alias *al = alias_list;
4406 alias_list = alias_list->next;
4407 err = sysfs_slab_alias(al->s, al->name);
4408 if (err)
4409 printk(KERN_ERR "SLUB: Unable to add boot slab alias"
4410 " %s to sysfs\n", s->name);
4411 kfree(al);
4414 resiliency_test();
4415 return 0;
4418 __initcall(slab_sysfs_init);
4419 #endif
4422 * The /proc/slabinfo ABI
4424 #ifdef CONFIG_SLABINFO
4426 ssize_t slabinfo_write(struct file *file, const char __user * buffer,
4427 size_t count, loff_t *ppos)
4429 return -EINVAL;
4433 static void print_slabinfo_header(struct seq_file *m)
4435 seq_puts(m, "slabinfo - version: 2.1\n");
4436 seq_puts(m, "# name <active_objs> <num_objs> <objsize> "
4437 "<objperslab> <pagesperslab>");
4438 seq_puts(m, " : tunables <limit> <batchcount> <sharedfactor>");
4439 seq_puts(m, " : slabdata <active_slabs> <num_slabs> <sharedavail>");
4440 seq_putc(m, '\n');
4443 static void *s_start(struct seq_file *m, loff_t *pos)
4445 loff_t n = *pos;
4447 down_read(&slub_lock);
4448 if (!n)
4449 print_slabinfo_header(m);
4451 return seq_list_start(&slab_caches, *pos);
4454 static void *s_next(struct seq_file *m, void *p, loff_t *pos)
4456 return seq_list_next(p, &slab_caches, pos);
4459 static void s_stop(struct seq_file *m, void *p)
4461 up_read(&slub_lock);
4464 static int s_show(struct seq_file *m, void *p)
4466 unsigned long nr_partials = 0;
4467 unsigned long nr_slabs = 0;
4468 unsigned long nr_inuse = 0;
4469 unsigned long nr_objs = 0;
4470 unsigned long nr_free = 0;
4471 struct kmem_cache *s;
4472 int node;
4474 s = list_entry(p, struct kmem_cache, list);
4476 for_each_online_node(node) {
4477 struct kmem_cache_node *n = get_node(s, node);
4479 if (!n)
4480 continue;
4482 nr_partials += n->nr_partial;
4483 nr_slabs += atomic_long_read(&n->nr_slabs);
4484 nr_objs += atomic_long_read(&n->total_objects);
4485 nr_free += count_partial(n, count_free);
4488 nr_inuse = nr_objs - nr_free;
4490 seq_printf(m, "%-17s %6lu %6lu %6u %4u %4d", s->name, nr_inuse,
4491 nr_objs, s->size, oo_objects(s->oo),
4492 (1 << oo_order(s->oo)));
4493 seq_printf(m, " : tunables %4u %4u %4u", 0, 0, 0);
4494 seq_printf(m, " : slabdata %6lu %6lu %6lu", nr_slabs, nr_slabs,
4495 0UL);
4496 seq_putc(m, '\n');
4497 return 0;
4500 const struct seq_operations slabinfo_op = {
4501 .start = s_start,
4502 .next = s_next,
4503 .stop = s_stop,
4504 .show = s_show,
4507 #endif /* CONFIG_SLABINFO */