[POWERPC] PS3: Compare firmware version
[pv_ops_mirror.git] / mm / slub.c
blobc9ab68881b43ebe8525fb2fa1e943f9810229c21
1 /*
2 * SLUB: A slab allocator that limits cache line use instead of queuing
3 * objects in per cpu and per node lists.
5 * The allocator synchronizes using per slab locks and only
6 * uses a centralized lock to manage a pool of partial slabs.
8 * (C) 2007 SGI, Christoph Lameter <clameter@sgi.com>
9 */
11 #include <linux/mm.h>
12 #include <linux/module.h>
13 #include <linux/bit_spinlock.h>
14 #include <linux/interrupt.h>
15 #include <linux/bitops.h>
16 #include <linux/slab.h>
17 #include <linux/seq_file.h>
18 #include <linux/cpu.h>
19 #include <linux/cpuset.h>
20 #include <linux/mempolicy.h>
21 #include <linux/ctype.h>
22 #include <linux/kallsyms.h>
25 * Lock order:
26 * 1. slab_lock(page)
27 * 2. slab->list_lock
29 * The slab_lock protects operations on the object of a particular
30 * slab and its metadata in the page struct. If the slab lock
31 * has been taken then no allocations nor frees can be performed
32 * on the objects in the slab nor can the slab be added or removed
33 * from the partial or full lists since this would mean modifying
34 * the page_struct of the slab.
36 * The list_lock protects the partial and full list on each node and
37 * the partial slab counter. If taken then no new slabs may be added or
38 * removed from the lists nor make the number of partial slabs be modified.
39 * (Note that the total number of slabs is an atomic value that may be
40 * modified without taking the list lock).
42 * The list_lock is a centralized lock and thus we avoid taking it as
43 * much as possible. As long as SLUB does not have to handle partial
44 * slabs, operations can continue without any centralized lock. F.e.
45 * allocating a long series of objects that fill up slabs does not require
46 * the list lock.
48 * The lock order is sometimes inverted when we are trying to get a slab
49 * off a list. We take the list_lock and then look for a page on the list
50 * to use. While we do that objects in the slabs may be freed. We can
51 * only operate on the slab if we have also taken the slab_lock. So we use
52 * a slab_trylock() on the slab. If trylock was successful then no frees
53 * can occur anymore and we can use the slab for allocations etc. If the
54 * slab_trylock() does not succeed then frees are in progress in the slab and
55 * we must stay away from it for a while since we may cause a bouncing
56 * cacheline if we try to acquire the lock. So go onto the next slab.
57 * If all pages are busy then we may allocate a new slab instead of reusing
58 * a partial slab. A new slab has noone operating on it and thus there is
59 * no danger of cacheline contention.
61 * Interrupts are disabled during allocation and deallocation in order to
62 * make the slab allocator safe to use in the context of an irq. In addition
63 * interrupts are disabled to ensure that the processor does not change
64 * while handling per_cpu slabs, due to kernel preemption.
66 * SLUB assigns one slab for allocation to each processor.
67 * Allocations only occur from these slabs called cpu slabs.
69 * Slabs with free elements are kept on a partial list and during regular
70 * operations no list for full slabs is used. If an object in a full slab is
71 * freed then the slab will show up again on the partial lists.
72 * We track full slabs for debugging purposes though because otherwise we
73 * cannot scan all objects.
75 * Slabs are freed when they become empty. Teardown and setup is
76 * minimal so we rely on the page allocators per cpu caches for
77 * fast frees and allocs.
79 * Overloading of page flags that are otherwise used for LRU management.
81 * PageActive The slab is frozen and exempt from list processing.
82 * This means that the slab is dedicated to a purpose
83 * such as satisfying allocations for a specific
84 * processor. Objects may be freed in the slab while
85 * it is frozen but slab_free will then skip the usual
86 * list operations. It is up to the processor holding
87 * the slab to integrate the slab into the slab lists
88 * when the slab is no longer needed.
90 * One use of this flag is to mark slabs that are
91 * used for allocations. Then such a slab becomes a cpu
92 * slab. The cpu slab may be equipped with an additional
93 * lockless_freelist that allows lockless access to
94 * free objects in addition to the regular freelist
95 * that requires the slab lock.
97 * PageError Slab requires special handling due to debug
98 * options set. This moves slab handling out of
99 * the fast path and disables lockless freelists.
102 #define FROZEN (1 << PG_active)
104 #ifdef CONFIG_SLUB_DEBUG
105 #define SLABDEBUG (1 << PG_error)
106 #else
107 #define SLABDEBUG 0
108 #endif
110 static inline int SlabFrozen(struct page *page)
112 return page->flags & FROZEN;
115 static inline void SetSlabFrozen(struct page *page)
117 page->flags |= FROZEN;
120 static inline void ClearSlabFrozen(struct page *page)
122 page->flags &= ~FROZEN;
125 static inline int SlabDebug(struct page *page)
127 return page->flags & SLABDEBUG;
130 static inline void SetSlabDebug(struct page *page)
132 page->flags |= SLABDEBUG;
135 static inline void ClearSlabDebug(struct page *page)
137 page->flags &= ~SLABDEBUG;
141 * Issues still to be resolved:
143 * - The per cpu array is updated for each new slab and and is a remote
144 * cacheline for most nodes. This could become a bouncing cacheline given
145 * enough frequent updates. There are 16 pointers in a cacheline, so at
146 * max 16 cpus could compete for the cacheline which may be okay.
148 * - Support PAGE_ALLOC_DEBUG. Should be easy to do.
150 * - Variable sizing of the per node arrays
153 /* Enable to test recovery from slab corruption on boot */
154 #undef SLUB_RESILIENCY_TEST
156 #if PAGE_SHIFT <= 12
159 * Small page size. Make sure that we do not fragment memory
161 #define DEFAULT_MAX_ORDER 1
162 #define DEFAULT_MIN_OBJECTS 4
164 #else
167 * Large page machines are customarily able to handle larger
168 * page orders.
170 #define DEFAULT_MAX_ORDER 2
171 #define DEFAULT_MIN_OBJECTS 8
173 #endif
176 * Mininum number of partial slabs. These will be left on the partial
177 * lists even if they are empty. kmem_cache_shrink may reclaim them.
179 #define MIN_PARTIAL 2
182 * Maximum number of desirable partial slabs.
183 * The existence of more partial slabs makes kmem_cache_shrink
184 * sort the partial list by the number of objects in the.
186 #define MAX_PARTIAL 10
188 #define DEBUG_DEFAULT_FLAGS (SLAB_DEBUG_FREE | SLAB_RED_ZONE | \
189 SLAB_POISON | SLAB_STORE_USER)
192 * Set of flags that will prevent slab merging
194 #define SLUB_NEVER_MERGE (SLAB_RED_ZONE | SLAB_POISON | SLAB_STORE_USER | \
195 SLAB_TRACE | SLAB_DESTROY_BY_RCU)
197 #define SLUB_MERGE_SAME (SLAB_DEBUG_FREE | SLAB_RECLAIM_ACCOUNT | \
198 SLAB_CACHE_DMA)
200 #ifndef ARCH_KMALLOC_MINALIGN
201 #define ARCH_KMALLOC_MINALIGN __alignof__(unsigned long long)
202 #endif
204 #ifndef ARCH_SLAB_MINALIGN
205 #define ARCH_SLAB_MINALIGN __alignof__(unsigned long long)
206 #endif
208 /* Internal SLUB flags */
209 #define __OBJECT_POISON 0x80000000 /* Poison object */
211 /* Not all arches define cache_line_size */
212 #ifndef cache_line_size
213 #define cache_line_size() L1_CACHE_BYTES
214 #endif
216 static int kmem_size = sizeof(struct kmem_cache);
218 #ifdef CONFIG_SMP
219 static struct notifier_block slab_notifier;
220 #endif
222 static enum {
223 DOWN, /* No slab functionality available */
224 PARTIAL, /* kmem_cache_open() works but kmalloc does not */
225 UP, /* Everything works but does not show up in sysfs */
226 SYSFS /* Sysfs up */
227 } slab_state = DOWN;
229 /* A list of all slab caches on the system */
230 static DECLARE_RWSEM(slub_lock);
231 LIST_HEAD(slab_caches);
234 * Tracking user of a slab.
236 struct track {
237 void *addr; /* Called from address */
238 int cpu; /* Was running on cpu */
239 int pid; /* Pid context */
240 unsigned long when; /* When did the operation occur */
243 enum track_item { TRACK_ALLOC, TRACK_FREE };
245 #if defined(CONFIG_SYSFS) && defined(CONFIG_SLUB_DEBUG)
246 static int sysfs_slab_add(struct kmem_cache *);
247 static int sysfs_slab_alias(struct kmem_cache *, const char *);
248 static void sysfs_slab_remove(struct kmem_cache *);
249 #else
250 static int sysfs_slab_add(struct kmem_cache *s) { return 0; }
251 static int sysfs_slab_alias(struct kmem_cache *s, const char *p) { return 0; }
252 static void sysfs_slab_remove(struct kmem_cache *s) {}
253 #endif
255 /********************************************************************
256 * Core slab cache functions
257 *******************************************************************/
259 int slab_is_available(void)
261 return slab_state >= UP;
264 static inline struct kmem_cache_node *get_node(struct kmem_cache *s, int node)
266 #ifdef CONFIG_NUMA
267 return s->node[node];
268 #else
269 return &s->local_node;
270 #endif
273 static inline int check_valid_pointer(struct kmem_cache *s,
274 struct page *page, const void *object)
276 void *base;
278 if (!object)
279 return 1;
281 base = page_address(page);
282 if (object < base || object >= base + s->objects * s->size ||
283 (object - base) % s->size) {
284 return 0;
287 return 1;
291 * Slow version of get and set free pointer.
293 * This version requires touching the cache lines of kmem_cache which
294 * we avoid to do in the fast alloc free paths. There we obtain the offset
295 * from the page struct.
297 static inline void *get_freepointer(struct kmem_cache *s, void *object)
299 return *(void **)(object + s->offset);
302 static inline void set_freepointer(struct kmem_cache *s, void *object, void *fp)
304 *(void **)(object + s->offset) = fp;
307 /* Loop over all objects in a slab */
308 #define for_each_object(__p, __s, __addr) \
309 for (__p = (__addr); __p < (__addr) + (__s)->objects * (__s)->size;\
310 __p += (__s)->size)
312 /* Scan freelist */
313 #define for_each_free_object(__p, __s, __free) \
314 for (__p = (__free); __p; __p = get_freepointer((__s), __p))
316 /* Determine object index from a given position */
317 static inline int slab_index(void *p, struct kmem_cache *s, void *addr)
319 return (p - addr) / s->size;
322 #ifdef CONFIG_SLUB_DEBUG
324 * Debug settings:
326 static int slub_debug;
328 static char *slub_debug_slabs;
331 * Object debugging
333 static void print_section(char *text, u8 *addr, unsigned int length)
335 int i, offset;
336 int newline = 1;
337 char ascii[17];
339 ascii[16] = 0;
341 for (i = 0; i < length; i++) {
342 if (newline) {
343 printk(KERN_ERR "%10s 0x%p: ", text, addr + i);
344 newline = 0;
346 printk(" %02x", addr[i]);
347 offset = i % 16;
348 ascii[offset] = isgraph(addr[i]) ? addr[i] : '.';
349 if (offset == 15) {
350 printk(" %s\n",ascii);
351 newline = 1;
354 if (!newline) {
355 i %= 16;
356 while (i < 16) {
357 printk(" ");
358 ascii[i] = ' ';
359 i++;
361 printk(" %s\n", ascii);
365 static struct track *get_track(struct kmem_cache *s, void *object,
366 enum track_item alloc)
368 struct track *p;
370 if (s->offset)
371 p = object + s->offset + sizeof(void *);
372 else
373 p = object + s->inuse;
375 return p + alloc;
378 static void set_track(struct kmem_cache *s, void *object,
379 enum track_item alloc, void *addr)
381 struct track *p;
383 if (s->offset)
384 p = object + s->offset + sizeof(void *);
385 else
386 p = object + s->inuse;
388 p += alloc;
389 if (addr) {
390 p->addr = addr;
391 p->cpu = smp_processor_id();
392 p->pid = current ? current->pid : -1;
393 p->when = jiffies;
394 } else
395 memset(p, 0, sizeof(struct track));
398 static void init_tracking(struct kmem_cache *s, void *object)
400 if (s->flags & SLAB_STORE_USER) {
401 set_track(s, object, TRACK_FREE, NULL);
402 set_track(s, object, TRACK_ALLOC, NULL);
406 static void print_track(const char *s, struct track *t)
408 if (!t->addr)
409 return;
411 printk(KERN_ERR "%s: ", s);
412 __print_symbol("%s", (unsigned long)t->addr);
413 printk(" jiffies_ago=%lu cpu=%u pid=%d\n", jiffies - t->when, t->cpu, t->pid);
416 static void print_trailer(struct kmem_cache *s, u8 *p)
418 unsigned int off; /* Offset of last byte */
420 if (s->flags & SLAB_RED_ZONE)
421 print_section("Redzone", p + s->objsize,
422 s->inuse - s->objsize);
424 printk(KERN_ERR "FreePointer 0x%p -> 0x%p\n",
425 p + s->offset,
426 get_freepointer(s, p));
428 if (s->offset)
429 off = s->offset + sizeof(void *);
430 else
431 off = s->inuse;
433 if (s->flags & SLAB_STORE_USER) {
434 print_track("Last alloc", get_track(s, p, TRACK_ALLOC));
435 print_track("Last free ", get_track(s, p, TRACK_FREE));
436 off += 2 * sizeof(struct track);
439 if (off != s->size)
440 /* Beginning of the filler is the free pointer */
441 print_section("Filler", p + off, s->size - off);
444 static void object_err(struct kmem_cache *s, struct page *page,
445 u8 *object, char *reason)
447 u8 *addr = page_address(page);
449 printk(KERN_ERR "*** SLUB %s: %s@0x%p slab 0x%p\n",
450 s->name, reason, object, page);
451 printk(KERN_ERR " offset=%tu flags=0x%04lx inuse=%u freelist=0x%p\n",
452 object - addr, page->flags, page->inuse, page->freelist);
453 if (object > addr + 16)
454 print_section("Bytes b4", object - 16, 16);
455 print_section("Object", object, min(s->objsize, 128));
456 print_trailer(s, object);
457 dump_stack();
460 static void slab_err(struct kmem_cache *s, struct page *page, char *reason, ...)
462 va_list args;
463 char buf[100];
465 va_start(args, reason);
466 vsnprintf(buf, sizeof(buf), reason, args);
467 va_end(args);
468 printk(KERN_ERR "*** SLUB %s: %s in slab @0x%p\n", s->name, buf,
469 page);
470 dump_stack();
473 static void init_object(struct kmem_cache *s, void *object, int active)
475 u8 *p = object;
477 if (s->flags & __OBJECT_POISON) {
478 memset(p, POISON_FREE, s->objsize - 1);
479 p[s->objsize -1] = POISON_END;
482 if (s->flags & SLAB_RED_ZONE)
483 memset(p + s->objsize,
484 active ? SLUB_RED_ACTIVE : SLUB_RED_INACTIVE,
485 s->inuse - s->objsize);
488 static int check_bytes(u8 *start, unsigned int value, unsigned int bytes)
490 while (bytes) {
491 if (*start != (u8)value)
492 return 0;
493 start++;
494 bytes--;
496 return 1;
500 * Object layout:
502 * object address
503 * Bytes of the object to be managed.
504 * If the freepointer may overlay the object then the free
505 * pointer is the first word of the object.
507 * Poisoning uses 0x6b (POISON_FREE) and the last byte is
508 * 0xa5 (POISON_END)
510 * object + s->objsize
511 * Padding to reach word boundary. This is also used for Redzoning.
512 * Padding is extended by another word if Redzoning is enabled and
513 * objsize == inuse.
515 * We fill with 0xbb (RED_INACTIVE) for inactive objects and with
516 * 0xcc (RED_ACTIVE) for objects in use.
518 * object + s->inuse
519 * Meta data starts here.
521 * A. Free pointer (if we cannot overwrite object on free)
522 * B. Tracking data for SLAB_STORE_USER
523 * C. Padding to reach required alignment boundary or at mininum
524 * one word if debuggin is on to be able to detect writes
525 * before the word boundary.
527 * Padding is done using 0x5a (POISON_INUSE)
529 * object + s->size
530 * Nothing is used beyond s->size.
532 * If slabcaches are merged then the objsize and inuse boundaries are mostly
533 * ignored. And therefore no slab options that rely on these boundaries
534 * may be used with merged slabcaches.
537 static void restore_bytes(struct kmem_cache *s, char *message, u8 data,
538 void *from, void *to)
540 printk(KERN_ERR "@@@ SLUB %s: Restoring %s (0x%x) from 0x%p-0x%p\n",
541 s->name, message, data, from, to - 1);
542 memset(from, data, to - from);
545 static int check_pad_bytes(struct kmem_cache *s, struct page *page, u8 *p)
547 unsigned long off = s->inuse; /* The end of info */
549 if (s->offset)
550 /* Freepointer is placed after the object. */
551 off += sizeof(void *);
553 if (s->flags & SLAB_STORE_USER)
554 /* We also have user information there */
555 off += 2 * sizeof(struct track);
557 if (s->size == off)
558 return 1;
560 if (check_bytes(p + off, POISON_INUSE, s->size - off))
561 return 1;
563 object_err(s, page, p, "Object padding check fails");
566 * Restore padding
568 restore_bytes(s, "object padding", POISON_INUSE, p + off, p + s->size);
569 return 0;
572 static int slab_pad_check(struct kmem_cache *s, struct page *page)
574 u8 *p;
575 int length, remainder;
577 if (!(s->flags & SLAB_POISON))
578 return 1;
580 p = page_address(page);
581 length = s->objects * s->size;
582 remainder = (PAGE_SIZE << s->order) - length;
583 if (!remainder)
584 return 1;
586 if (!check_bytes(p + length, POISON_INUSE, remainder)) {
587 slab_err(s, page, "Padding check failed");
588 restore_bytes(s, "slab padding", POISON_INUSE, p + length,
589 p + length + remainder);
590 return 0;
592 return 1;
595 static int check_object(struct kmem_cache *s, struct page *page,
596 void *object, int active)
598 u8 *p = object;
599 u8 *endobject = object + s->objsize;
601 if (s->flags & SLAB_RED_ZONE) {
602 unsigned int red =
603 active ? SLUB_RED_ACTIVE : SLUB_RED_INACTIVE;
605 if (!check_bytes(endobject, red, s->inuse - s->objsize)) {
606 object_err(s, page, object,
607 active ? "Redzone Active" : "Redzone Inactive");
608 restore_bytes(s, "redzone", red,
609 endobject, object + s->inuse);
610 return 0;
612 } else {
613 if ((s->flags & SLAB_POISON) && s->objsize < s->inuse &&
614 !check_bytes(endobject, POISON_INUSE,
615 s->inuse - s->objsize)) {
616 object_err(s, page, p, "Alignment padding check fails");
618 * Fix it so that there will not be another report.
620 * Hmmm... We may be corrupting an object that now expects
621 * to be longer than allowed.
623 restore_bytes(s, "alignment padding", POISON_INUSE,
624 endobject, object + s->inuse);
628 if (s->flags & SLAB_POISON) {
629 if (!active && (s->flags & __OBJECT_POISON) &&
630 (!check_bytes(p, POISON_FREE, s->objsize - 1) ||
631 p[s->objsize - 1] != POISON_END)) {
633 object_err(s, page, p, "Poison check failed");
634 restore_bytes(s, "Poison", POISON_FREE,
635 p, p + s->objsize -1);
636 restore_bytes(s, "Poison", POISON_END,
637 p + s->objsize - 1, p + s->objsize);
638 return 0;
641 * check_pad_bytes cleans up on its own.
643 check_pad_bytes(s, page, p);
646 if (!s->offset && active)
648 * Object and freepointer overlap. Cannot check
649 * freepointer while object is allocated.
651 return 1;
653 /* Check free pointer validity */
654 if (!check_valid_pointer(s, page, get_freepointer(s, p))) {
655 object_err(s, page, p, "Freepointer corrupt");
657 * No choice but to zap it and thus loose the remainder
658 * of the free objects in this slab. May cause
659 * another error because the object count is now wrong.
661 set_freepointer(s, p, NULL);
662 return 0;
664 return 1;
667 static int check_slab(struct kmem_cache *s, struct page *page)
669 VM_BUG_ON(!irqs_disabled());
671 if (!PageSlab(page)) {
672 slab_err(s, page, "Not a valid slab page flags=%lx "
673 "mapping=0x%p count=%d", page->flags, page->mapping,
674 page_count(page));
675 return 0;
677 if (page->offset * sizeof(void *) != s->offset) {
678 slab_err(s, page, "Corrupted offset %lu flags=0x%lx "
679 "mapping=0x%p count=%d",
680 (unsigned long)(page->offset * sizeof(void *)),
681 page->flags,
682 page->mapping,
683 page_count(page));
684 return 0;
686 if (page->inuse > s->objects) {
687 slab_err(s, page, "inuse %u > max %u @0x%p flags=%lx "
688 "mapping=0x%p count=%d",
689 s->name, page->inuse, s->objects, page->flags,
690 page->mapping, page_count(page));
691 return 0;
693 /* Slab_pad_check fixes things up after itself */
694 slab_pad_check(s, page);
695 return 1;
699 * Determine if a certain object on a page is on the freelist. Must hold the
700 * slab lock to guarantee that the chains are in a consistent state.
702 static int on_freelist(struct kmem_cache *s, struct page *page, void *search)
704 int nr = 0;
705 void *fp = page->freelist;
706 void *object = NULL;
708 while (fp && nr <= s->objects) {
709 if (fp == search)
710 return 1;
711 if (!check_valid_pointer(s, page, fp)) {
712 if (object) {
713 object_err(s, page, object,
714 "Freechain corrupt");
715 set_freepointer(s, object, NULL);
716 break;
717 } else {
718 slab_err(s, page, "Freepointer 0x%p corrupt",
719 fp);
720 page->freelist = NULL;
721 page->inuse = s->objects;
722 printk(KERN_ERR "@@@ SLUB %s: Freelist "
723 "cleared. Slab 0x%p\n",
724 s->name, page);
725 return 0;
727 break;
729 object = fp;
730 fp = get_freepointer(s, object);
731 nr++;
734 if (page->inuse != s->objects - nr) {
735 slab_err(s, page, "Wrong object count. Counter is %d but "
736 "counted were %d", s, page, page->inuse,
737 s->objects - nr);
738 page->inuse = s->objects - nr;
739 printk(KERN_ERR "@@@ SLUB %s: Object count adjusted. "
740 "Slab @0x%p\n", s->name, page);
742 return search == NULL;
745 static void trace(struct kmem_cache *s, struct page *page, void *object, int alloc)
747 if (s->flags & SLAB_TRACE) {
748 printk(KERN_INFO "TRACE %s %s 0x%p inuse=%d fp=0x%p\n",
749 s->name,
750 alloc ? "alloc" : "free",
751 object, page->inuse,
752 page->freelist);
754 if (!alloc)
755 print_section("Object", (void *)object, s->objsize);
757 dump_stack();
762 * Tracking of fully allocated slabs for debugging purposes.
764 static void add_full(struct kmem_cache_node *n, struct page *page)
766 spin_lock(&n->list_lock);
767 list_add(&page->lru, &n->full);
768 spin_unlock(&n->list_lock);
771 static void remove_full(struct kmem_cache *s, struct page *page)
773 struct kmem_cache_node *n;
775 if (!(s->flags & SLAB_STORE_USER))
776 return;
778 n = get_node(s, page_to_nid(page));
780 spin_lock(&n->list_lock);
781 list_del(&page->lru);
782 spin_unlock(&n->list_lock);
785 static void setup_object_debug(struct kmem_cache *s, struct page *page,
786 void *object)
788 if (!(s->flags & (SLAB_STORE_USER|SLAB_RED_ZONE|__OBJECT_POISON)))
789 return;
791 init_object(s, object, 0);
792 init_tracking(s, object);
795 static int alloc_debug_processing(struct kmem_cache *s, struct page *page,
796 void *object, void *addr)
798 if (!check_slab(s, page))
799 goto bad;
801 if (object && !on_freelist(s, page, object)) {
802 slab_err(s, page, "Object 0x%p already allocated", object);
803 goto bad;
806 if (!check_valid_pointer(s, page, object)) {
807 object_err(s, page, object, "Freelist Pointer check fails");
808 goto bad;
811 if (object && !check_object(s, page, object, 0))
812 goto bad;
814 /* Success perform special debug activities for allocs */
815 if (s->flags & SLAB_STORE_USER)
816 set_track(s, object, TRACK_ALLOC, addr);
817 trace(s, page, object, 1);
818 init_object(s, object, 1);
819 return 1;
821 bad:
822 if (PageSlab(page)) {
824 * If this is a slab page then lets do the best we can
825 * to avoid issues in the future. Marking all objects
826 * as used avoids touching the remaining objects.
828 printk(KERN_ERR "@@@ SLUB: %s slab 0x%p. Marking all objects used.\n",
829 s->name, page);
830 page->inuse = s->objects;
831 page->freelist = NULL;
832 /* Fix up fields that may be corrupted */
833 page->offset = s->offset / sizeof(void *);
835 return 0;
838 static int free_debug_processing(struct kmem_cache *s, struct page *page,
839 void *object, void *addr)
841 if (!check_slab(s, page))
842 goto fail;
844 if (!check_valid_pointer(s, page, object)) {
845 slab_err(s, page, "Invalid object pointer 0x%p", object);
846 goto fail;
849 if (on_freelist(s, page, object)) {
850 slab_err(s, page, "Object 0x%p already free", object);
851 goto fail;
854 if (!check_object(s, page, object, 1))
855 return 0;
857 if (unlikely(s != page->slab)) {
858 if (!PageSlab(page))
859 slab_err(s, page, "Attempt to free object(0x%p) "
860 "outside of slab", object);
861 else
862 if (!page->slab) {
863 printk(KERN_ERR
864 "SLUB <none>: no slab for object 0x%p.\n",
865 object);
866 dump_stack();
868 else
869 slab_err(s, page, "object at 0x%p belongs "
870 "to slab %s", object, page->slab->name);
871 goto fail;
874 /* Special debug activities for freeing objects */
875 if (!SlabFrozen(page) && !page->freelist)
876 remove_full(s, page);
877 if (s->flags & SLAB_STORE_USER)
878 set_track(s, object, TRACK_FREE, addr);
879 trace(s, page, object, 0);
880 init_object(s, object, 0);
881 return 1;
883 fail:
884 printk(KERN_ERR "@@@ SLUB: %s slab 0x%p object at 0x%p not freed.\n",
885 s->name, page, object);
886 return 0;
889 static int __init setup_slub_debug(char *str)
891 if (!str || *str != '=')
892 slub_debug = DEBUG_DEFAULT_FLAGS;
893 else {
894 str++;
895 if (*str == 0 || *str == ',')
896 slub_debug = DEBUG_DEFAULT_FLAGS;
897 else
898 for( ;*str && *str != ','; str++)
899 switch (*str) {
900 case 'f' : case 'F' :
901 slub_debug |= SLAB_DEBUG_FREE;
902 break;
903 case 'z' : case 'Z' :
904 slub_debug |= SLAB_RED_ZONE;
905 break;
906 case 'p' : case 'P' :
907 slub_debug |= SLAB_POISON;
908 break;
909 case 'u' : case 'U' :
910 slub_debug |= SLAB_STORE_USER;
911 break;
912 case 't' : case 'T' :
913 slub_debug |= SLAB_TRACE;
914 break;
915 default:
916 printk(KERN_ERR "slub_debug option '%c' "
917 "unknown. skipped\n",*str);
921 if (*str == ',')
922 slub_debug_slabs = str + 1;
923 return 1;
926 __setup("slub_debug", setup_slub_debug);
928 static void kmem_cache_open_debug_check(struct kmem_cache *s)
931 * The page->offset field is only 16 bit wide. This is an offset
932 * in units of words from the beginning of an object. If the slab
933 * size is bigger then we cannot move the free pointer behind the
934 * object anymore.
936 * On 32 bit platforms the limit is 256k. On 64bit platforms
937 * the limit is 512k.
939 * Debugging or ctor may create a need to move the free
940 * pointer. Fail if this happens.
942 if (s->objsize >= 65535 * sizeof(void *)) {
943 BUG_ON(s->flags & (SLAB_RED_ZONE | SLAB_POISON |
944 SLAB_STORE_USER | SLAB_DESTROY_BY_RCU));
945 BUG_ON(s->ctor);
947 else
949 * Enable debugging if selected on the kernel commandline.
951 if (slub_debug && (!slub_debug_slabs ||
952 strncmp(slub_debug_slabs, s->name,
953 strlen(slub_debug_slabs)) == 0))
954 s->flags |= slub_debug;
956 #else
957 static inline void setup_object_debug(struct kmem_cache *s,
958 struct page *page, void *object) {}
960 static inline int alloc_debug_processing(struct kmem_cache *s,
961 struct page *page, void *object, void *addr) { return 0; }
963 static inline int free_debug_processing(struct kmem_cache *s,
964 struct page *page, void *object, void *addr) { return 0; }
966 static inline int slab_pad_check(struct kmem_cache *s, struct page *page)
967 { return 1; }
968 static inline int check_object(struct kmem_cache *s, struct page *page,
969 void *object, int active) { return 1; }
970 static inline void add_full(struct kmem_cache_node *n, struct page *page) {}
971 static inline void kmem_cache_open_debug_check(struct kmem_cache *s) {}
972 #define slub_debug 0
973 #endif
975 * Slab allocation and freeing
977 static struct page *allocate_slab(struct kmem_cache *s, gfp_t flags, int node)
979 struct page * page;
980 int pages = 1 << s->order;
982 if (s->order)
983 flags |= __GFP_COMP;
985 if (s->flags & SLAB_CACHE_DMA)
986 flags |= SLUB_DMA;
988 if (node == -1)
989 page = alloc_pages(flags, s->order);
990 else
991 page = alloc_pages_node(node, flags, s->order);
993 if (!page)
994 return NULL;
996 mod_zone_page_state(page_zone(page),
997 (s->flags & SLAB_RECLAIM_ACCOUNT) ?
998 NR_SLAB_RECLAIMABLE : NR_SLAB_UNRECLAIMABLE,
999 pages);
1001 return page;
1004 static void setup_object(struct kmem_cache *s, struct page *page,
1005 void *object)
1007 setup_object_debug(s, page, object);
1008 if (unlikely(s->ctor))
1009 s->ctor(object, s, 0);
1012 static struct page *new_slab(struct kmem_cache *s, gfp_t flags, int node)
1014 struct page *page;
1015 struct kmem_cache_node *n;
1016 void *start;
1017 void *end;
1018 void *last;
1019 void *p;
1021 BUG_ON(flags & ~(GFP_DMA | GFP_LEVEL_MASK));
1023 if (flags & __GFP_WAIT)
1024 local_irq_enable();
1026 page = allocate_slab(s, flags & GFP_LEVEL_MASK, node);
1027 if (!page)
1028 goto out;
1030 n = get_node(s, page_to_nid(page));
1031 if (n)
1032 atomic_long_inc(&n->nr_slabs);
1033 page->offset = s->offset / sizeof(void *);
1034 page->slab = s;
1035 page->flags |= 1 << PG_slab;
1036 if (s->flags & (SLAB_DEBUG_FREE | SLAB_RED_ZONE | SLAB_POISON |
1037 SLAB_STORE_USER | SLAB_TRACE))
1038 SetSlabDebug(page);
1040 start = page_address(page);
1041 end = start + s->objects * s->size;
1043 if (unlikely(s->flags & SLAB_POISON))
1044 memset(start, POISON_INUSE, PAGE_SIZE << s->order);
1046 last = start;
1047 for_each_object(p, s, start) {
1048 setup_object(s, page, last);
1049 set_freepointer(s, last, p);
1050 last = p;
1052 setup_object(s, page, last);
1053 set_freepointer(s, last, NULL);
1055 page->freelist = start;
1056 page->lockless_freelist = NULL;
1057 page->inuse = 0;
1058 out:
1059 if (flags & __GFP_WAIT)
1060 local_irq_disable();
1061 return page;
1064 static void __free_slab(struct kmem_cache *s, struct page *page)
1066 int pages = 1 << s->order;
1068 if (unlikely(SlabDebug(page))) {
1069 void *p;
1071 slab_pad_check(s, page);
1072 for_each_object(p, s, page_address(page))
1073 check_object(s, page, p, 0);
1076 mod_zone_page_state(page_zone(page),
1077 (s->flags & SLAB_RECLAIM_ACCOUNT) ?
1078 NR_SLAB_RECLAIMABLE : NR_SLAB_UNRECLAIMABLE,
1079 - pages);
1081 page->mapping = NULL;
1082 __free_pages(page, s->order);
1085 static void rcu_free_slab(struct rcu_head *h)
1087 struct page *page;
1089 page = container_of((struct list_head *)h, struct page, lru);
1090 __free_slab(page->slab, page);
1093 static void free_slab(struct kmem_cache *s, struct page *page)
1095 if (unlikely(s->flags & SLAB_DESTROY_BY_RCU)) {
1097 * RCU free overloads the RCU head over the LRU
1099 struct rcu_head *head = (void *)&page->lru;
1101 call_rcu(head, rcu_free_slab);
1102 } else
1103 __free_slab(s, page);
1106 static void discard_slab(struct kmem_cache *s, struct page *page)
1108 struct kmem_cache_node *n = get_node(s, page_to_nid(page));
1110 atomic_long_dec(&n->nr_slabs);
1111 reset_page_mapcount(page);
1112 ClearSlabDebug(page);
1113 __ClearPageSlab(page);
1114 free_slab(s, page);
1118 * Per slab locking using the pagelock
1120 static __always_inline void slab_lock(struct page *page)
1122 bit_spin_lock(PG_locked, &page->flags);
1125 static __always_inline void slab_unlock(struct page *page)
1127 bit_spin_unlock(PG_locked, &page->flags);
1130 static __always_inline int slab_trylock(struct page *page)
1132 int rc = 1;
1134 rc = bit_spin_trylock(PG_locked, &page->flags);
1135 return rc;
1139 * Management of partially allocated slabs
1141 static void add_partial_tail(struct kmem_cache_node *n, struct page *page)
1143 spin_lock(&n->list_lock);
1144 n->nr_partial++;
1145 list_add_tail(&page->lru, &n->partial);
1146 spin_unlock(&n->list_lock);
1149 static void add_partial(struct kmem_cache_node *n, struct page *page)
1151 spin_lock(&n->list_lock);
1152 n->nr_partial++;
1153 list_add(&page->lru, &n->partial);
1154 spin_unlock(&n->list_lock);
1157 static void remove_partial(struct kmem_cache *s,
1158 struct page *page)
1160 struct kmem_cache_node *n = get_node(s, page_to_nid(page));
1162 spin_lock(&n->list_lock);
1163 list_del(&page->lru);
1164 n->nr_partial--;
1165 spin_unlock(&n->list_lock);
1169 * Lock slab and remove from the partial list.
1171 * Must hold list_lock.
1173 static inline int lock_and_freeze_slab(struct kmem_cache_node *n, struct page *page)
1175 if (slab_trylock(page)) {
1176 list_del(&page->lru);
1177 n->nr_partial--;
1178 SetSlabFrozen(page);
1179 return 1;
1181 return 0;
1185 * Try to allocate a partial slab from a specific node.
1187 static struct page *get_partial_node(struct kmem_cache_node *n)
1189 struct page *page;
1192 * Racy check. If we mistakenly see no partial slabs then we
1193 * just allocate an empty slab. If we mistakenly try to get a
1194 * partial slab and there is none available then get_partials()
1195 * will return NULL.
1197 if (!n || !n->nr_partial)
1198 return NULL;
1200 spin_lock(&n->list_lock);
1201 list_for_each_entry(page, &n->partial, lru)
1202 if (lock_and_freeze_slab(n, page))
1203 goto out;
1204 page = NULL;
1205 out:
1206 spin_unlock(&n->list_lock);
1207 return page;
1211 * Get a page from somewhere. Search in increasing NUMA distances.
1213 static struct page *get_any_partial(struct kmem_cache *s, gfp_t flags)
1215 #ifdef CONFIG_NUMA
1216 struct zonelist *zonelist;
1217 struct zone **z;
1218 struct page *page;
1221 * The defrag ratio allows a configuration of the tradeoffs between
1222 * inter node defragmentation and node local allocations. A lower
1223 * defrag_ratio increases the tendency to do local allocations
1224 * instead of attempting to obtain partial slabs from other nodes.
1226 * If the defrag_ratio is set to 0 then kmalloc() always
1227 * returns node local objects. If the ratio is higher then kmalloc()
1228 * may return off node objects because partial slabs are obtained
1229 * from other nodes and filled up.
1231 * If /sys/slab/xx/defrag_ratio is set to 100 (which makes
1232 * defrag_ratio = 1000) then every (well almost) allocation will
1233 * first attempt to defrag slab caches on other nodes. This means
1234 * scanning over all nodes to look for partial slabs which may be
1235 * expensive if we do it every time we are trying to find a slab
1236 * with available objects.
1238 if (!s->defrag_ratio || get_cycles() % 1024 > s->defrag_ratio)
1239 return NULL;
1241 zonelist = &NODE_DATA(slab_node(current->mempolicy))
1242 ->node_zonelists[gfp_zone(flags)];
1243 for (z = zonelist->zones; *z; z++) {
1244 struct kmem_cache_node *n;
1246 n = get_node(s, zone_to_nid(*z));
1248 if (n && cpuset_zone_allowed_hardwall(*z, flags) &&
1249 n->nr_partial > MIN_PARTIAL) {
1250 page = get_partial_node(n);
1251 if (page)
1252 return page;
1255 #endif
1256 return NULL;
1260 * Get a partial page, lock it and return it.
1262 static struct page *get_partial(struct kmem_cache *s, gfp_t flags, int node)
1264 struct page *page;
1265 int searchnode = (node == -1) ? numa_node_id() : node;
1267 page = get_partial_node(get_node(s, searchnode));
1268 if (page || (flags & __GFP_THISNODE))
1269 return page;
1271 return get_any_partial(s, flags);
1275 * Move a page back to the lists.
1277 * Must be called with the slab lock held.
1279 * On exit the slab lock will have been dropped.
1281 static void unfreeze_slab(struct kmem_cache *s, struct page *page)
1283 struct kmem_cache_node *n = get_node(s, page_to_nid(page));
1285 ClearSlabFrozen(page);
1286 if (page->inuse) {
1288 if (page->freelist)
1289 add_partial(n, page);
1290 else if (SlabDebug(page) && (s->flags & SLAB_STORE_USER))
1291 add_full(n, page);
1292 slab_unlock(page);
1294 } else {
1295 if (n->nr_partial < MIN_PARTIAL) {
1297 * Adding an empty slab to the partial slabs in order
1298 * to avoid page allocator overhead. This slab needs
1299 * to come after the other slabs with objects in
1300 * order to fill them up. That way the size of the
1301 * partial list stays small. kmem_cache_shrink can
1302 * reclaim empty slabs from the partial list.
1304 add_partial_tail(n, page);
1305 slab_unlock(page);
1306 } else {
1307 slab_unlock(page);
1308 discard_slab(s, page);
1314 * Remove the cpu slab
1316 static void deactivate_slab(struct kmem_cache *s, struct page *page, int cpu)
1319 * Merge cpu freelist into freelist. Typically we get here
1320 * because both freelists are empty. So this is unlikely
1321 * to occur.
1323 while (unlikely(page->lockless_freelist)) {
1324 void **object;
1326 /* Retrieve object from cpu_freelist */
1327 object = page->lockless_freelist;
1328 page->lockless_freelist = page->lockless_freelist[page->offset];
1330 /* And put onto the regular freelist */
1331 object[page->offset] = page->freelist;
1332 page->freelist = object;
1333 page->inuse--;
1335 s->cpu_slab[cpu] = NULL;
1336 unfreeze_slab(s, page);
1339 static void flush_slab(struct kmem_cache *s, struct page *page, int cpu)
1341 slab_lock(page);
1342 deactivate_slab(s, page, cpu);
1346 * Flush cpu slab.
1347 * Called from IPI handler with interrupts disabled.
1349 static void __flush_cpu_slab(struct kmem_cache *s, int cpu)
1351 struct page *page = s->cpu_slab[cpu];
1353 if (likely(page))
1354 flush_slab(s, page, cpu);
1357 static void flush_cpu_slab(void *d)
1359 struct kmem_cache *s = d;
1360 int cpu = smp_processor_id();
1362 __flush_cpu_slab(s, cpu);
1365 static void flush_all(struct kmem_cache *s)
1367 #ifdef CONFIG_SMP
1368 on_each_cpu(flush_cpu_slab, s, 1, 1);
1369 #else
1370 unsigned long flags;
1372 local_irq_save(flags);
1373 flush_cpu_slab(s);
1374 local_irq_restore(flags);
1375 #endif
1379 * Slow path. The lockless freelist is empty or we need to perform
1380 * debugging duties.
1382 * Interrupts are disabled.
1384 * Processing is still very fast if new objects have been freed to the
1385 * regular freelist. In that case we simply take over the regular freelist
1386 * as the lockless freelist and zap the regular freelist.
1388 * If that is not working then we fall back to the partial lists. We take the
1389 * first element of the freelist as the object to allocate now and move the
1390 * rest of the freelist to the lockless freelist.
1392 * And if we were unable to get a new slab from the partial slab lists then
1393 * we need to allocate a new slab. This is slowest path since we may sleep.
1395 static void *__slab_alloc(struct kmem_cache *s,
1396 gfp_t gfpflags, int node, void *addr, struct page *page)
1398 void **object;
1399 int cpu = smp_processor_id();
1401 if (!page)
1402 goto new_slab;
1404 slab_lock(page);
1405 if (unlikely(node != -1 && page_to_nid(page) != node))
1406 goto another_slab;
1407 load_freelist:
1408 object = page->freelist;
1409 if (unlikely(!object))
1410 goto another_slab;
1411 if (unlikely(SlabDebug(page)))
1412 goto debug;
1414 object = page->freelist;
1415 page->lockless_freelist = object[page->offset];
1416 page->inuse = s->objects;
1417 page->freelist = NULL;
1418 slab_unlock(page);
1419 return object;
1421 another_slab:
1422 deactivate_slab(s, page, cpu);
1424 new_slab:
1425 page = get_partial(s, gfpflags, node);
1426 if (page) {
1427 s->cpu_slab[cpu] = page;
1428 goto load_freelist;
1431 page = new_slab(s, gfpflags, node);
1432 if (page) {
1433 cpu = smp_processor_id();
1434 if (s->cpu_slab[cpu]) {
1436 * Someone else populated the cpu_slab while we
1437 * enabled interrupts, or we have gotten scheduled
1438 * on another cpu. The page may not be on the
1439 * requested node even if __GFP_THISNODE was
1440 * specified. So we need to recheck.
1442 if (node == -1 ||
1443 page_to_nid(s->cpu_slab[cpu]) == node) {
1445 * Current cpuslab is acceptable and we
1446 * want the current one since its cache hot
1448 discard_slab(s, page);
1449 page = s->cpu_slab[cpu];
1450 slab_lock(page);
1451 goto load_freelist;
1453 /* New slab does not fit our expectations */
1454 flush_slab(s, s->cpu_slab[cpu], cpu);
1456 slab_lock(page);
1457 SetSlabFrozen(page);
1458 s->cpu_slab[cpu] = page;
1459 goto load_freelist;
1461 return NULL;
1462 debug:
1463 object = page->freelist;
1464 if (!alloc_debug_processing(s, page, object, addr))
1465 goto another_slab;
1467 page->inuse++;
1468 page->freelist = object[page->offset];
1469 slab_unlock(page);
1470 return object;
1474 * Inlined fastpath so that allocation functions (kmalloc, kmem_cache_alloc)
1475 * have the fastpath folded into their functions. So no function call
1476 * overhead for requests that can be satisfied on the fastpath.
1478 * The fastpath works by first checking if the lockless freelist can be used.
1479 * If not then __slab_alloc is called for slow processing.
1481 * Otherwise we can simply pick the next object from the lockless free list.
1483 static void __always_inline *slab_alloc(struct kmem_cache *s,
1484 gfp_t gfpflags, int node, void *addr)
1486 struct page *page;
1487 void **object;
1488 unsigned long flags;
1490 local_irq_save(flags);
1491 page = s->cpu_slab[smp_processor_id()];
1492 if (unlikely(!page || !page->lockless_freelist ||
1493 (node != -1 && page_to_nid(page) != node)))
1495 object = __slab_alloc(s, gfpflags, node, addr, page);
1497 else {
1498 object = page->lockless_freelist;
1499 page->lockless_freelist = object[page->offset];
1501 local_irq_restore(flags);
1502 return object;
1505 void *kmem_cache_alloc(struct kmem_cache *s, gfp_t gfpflags)
1507 return slab_alloc(s, gfpflags, -1, __builtin_return_address(0));
1509 EXPORT_SYMBOL(kmem_cache_alloc);
1511 #ifdef CONFIG_NUMA
1512 void *kmem_cache_alloc_node(struct kmem_cache *s, gfp_t gfpflags, int node)
1514 return slab_alloc(s, gfpflags, node, __builtin_return_address(0));
1516 EXPORT_SYMBOL(kmem_cache_alloc_node);
1517 #endif
1520 * Slow patch handling. This may still be called frequently since objects
1521 * have a longer lifetime than the cpu slabs in most processing loads.
1523 * So we still attempt to reduce cache line usage. Just take the slab
1524 * lock and free the item. If there is no additional partial page
1525 * handling required then we can return immediately.
1527 static void __slab_free(struct kmem_cache *s, struct page *page,
1528 void *x, void *addr)
1530 void *prior;
1531 void **object = (void *)x;
1533 slab_lock(page);
1535 if (unlikely(SlabDebug(page)))
1536 goto debug;
1537 checks_ok:
1538 prior = object[page->offset] = page->freelist;
1539 page->freelist = object;
1540 page->inuse--;
1542 if (unlikely(SlabFrozen(page)))
1543 goto out_unlock;
1545 if (unlikely(!page->inuse))
1546 goto slab_empty;
1549 * Objects left in the slab. If it
1550 * was not on the partial list before
1551 * then add it.
1553 if (unlikely(!prior))
1554 add_partial(get_node(s, page_to_nid(page)), page);
1556 out_unlock:
1557 slab_unlock(page);
1558 return;
1560 slab_empty:
1561 if (prior)
1563 * Slab still on the partial list.
1565 remove_partial(s, page);
1567 slab_unlock(page);
1568 discard_slab(s, page);
1569 return;
1571 debug:
1572 if (!free_debug_processing(s, page, x, addr))
1573 goto out_unlock;
1574 goto checks_ok;
1578 * Fastpath with forced inlining to produce a kfree and kmem_cache_free that
1579 * can perform fastpath freeing without additional function calls.
1581 * The fastpath is only possible if we are freeing to the current cpu slab
1582 * of this processor. This typically the case if we have just allocated
1583 * the item before.
1585 * If fastpath is not possible then fall back to __slab_free where we deal
1586 * with all sorts of special processing.
1588 static void __always_inline slab_free(struct kmem_cache *s,
1589 struct page *page, void *x, void *addr)
1591 void **object = (void *)x;
1592 unsigned long flags;
1594 local_irq_save(flags);
1595 if (likely(page == s->cpu_slab[smp_processor_id()] &&
1596 !SlabDebug(page))) {
1597 object[page->offset] = page->lockless_freelist;
1598 page->lockless_freelist = object;
1599 } else
1600 __slab_free(s, page, x, addr);
1602 local_irq_restore(flags);
1605 void kmem_cache_free(struct kmem_cache *s, void *x)
1607 struct page *page;
1609 page = virt_to_head_page(x);
1611 slab_free(s, page, x, __builtin_return_address(0));
1613 EXPORT_SYMBOL(kmem_cache_free);
1615 /* Figure out on which slab object the object resides */
1616 static struct page *get_object_page(const void *x)
1618 struct page *page = virt_to_head_page(x);
1620 if (!PageSlab(page))
1621 return NULL;
1623 return page;
1627 * Object placement in a slab is made very easy because we always start at
1628 * offset 0. If we tune the size of the object to the alignment then we can
1629 * get the required alignment by putting one properly sized object after
1630 * another.
1632 * Notice that the allocation order determines the sizes of the per cpu
1633 * caches. Each processor has always one slab available for allocations.
1634 * Increasing the allocation order reduces the number of times that slabs
1635 * must be moved on and off the partial lists and is therefore a factor in
1636 * locking overhead.
1640 * Mininum / Maximum order of slab pages. This influences locking overhead
1641 * and slab fragmentation. A higher order reduces the number of partial slabs
1642 * and increases the number of allocations possible without having to
1643 * take the list_lock.
1645 static int slub_min_order;
1646 static int slub_max_order = DEFAULT_MAX_ORDER;
1647 static int slub_min_objects = DEFAULT_MIN_OBJECTS;
1650 * Merge control. If this is set then no merging of slab caches will occur.
1651 * (Could be removed. This was introduced to pacify the merge skeptics.)
1653 static int slub_nomerge;
1656 * Calculate the order of allocation given an slab object size.
1658 * The order of allocation has significant impact on performance and other
1659 * system components. Generally order 0 allocations should be preferred since
1660 * order 0 does not cause fragmentation in the page allocator. Larger objects
1661 * be problematic to put into order 0 slabs because there may be too much
1662 * unused space left. We go to a higher order if more than 1/8th of the slab
1663 * would be wasted.
1665 * In order to reach satisfactory performance we must ensure that a minimum
1666 * number of objects is in one slab. Otherwise we may generate too much
1667 * activity on the partial lists which requires taking the list_lock. This is
1668 * less a concern for large slabs though which are rarely used.
1670 * slub_max_order specifies the order where we begin to stop considering the
1671 * number of objects in a slab as critical. If we reach slub_max_order then
1672 * we try to keep the page order as low as possible. So we accept more waste
1673 * of space in favor of a small page order.
1675 * Higher order allocations also allow the placement of more objects in a
1676 * slab and thereby reduce object handling overhead. If the user has
1677 * requested a higher mininum order then we start with that one instead of
1678 * the smallest order which will fit the object.
1680 static inline int slab_order(int size, int min_objects,
1681 int max_order, int fract_leftover)
1683 int order;
1684 int rem;
1686 for (order = max(slub_min_order,
1687 fls(min_objects * size - 1) - PAGE_SHIFT);
1688 order <= max_order; order++) {
1690 unsigned long slab_size = PAGE_SIZE << order;
1692 if (slab_size < min_objects * size)
1693 continue;
1695 rem = slab_size % size;
1697 if (rem <= slab_size / fract_leftover)
1698 break;
1702 return order;
1705 static inline int calculate_order(int size)
1707 int order;
1708 int min_objects;
1709 int fraction;
1712 * Attempt to find best configuration for a slab. This
1713 * works by first attempting to generate a layout with
1714 * the best configuration and backing off gradually.
1716 * First we reduce the acceptable waste in a slab. Then
1717 * we reduce the minimum objects required in a slab.
1719 min_objects = slub_min_objects;
1720 while (min_objects > 1) {
1721 fraction = 8;
1722 while (fraction >= 4) {
1723 order = slab_order(size, min_objects,
1724 slub_max_order, fraction);
1725 if (order <= slub_max_order)
1726 return order;
1727 fraction /= 2;
1729 min_objects /= 2;
1733 * We were unable to place multiple objects in a slab. Now
1734 * lets see if we can place a single object there.
1736 order = slab_order(size, 1, slub_max_order, 1);
1737 if (order <= slub_max_order)
1738 return order;
1741 * Doh this slab cannot be placed using slub_max_order.
1743 order = slab_order(size, 1, MAX_ORDER, 1);
1744 if (order <= MAX_ORDER)
1745 return order;
1746 return -ENOSYS;
1750 * Figure out what the alignment of the objects will be.
1752 static unsigned long calculate_alignment(unsigned long flags,
1753 unsigned long align, unsigned long size)
1756 * If the user wants hardware cache aligned objects then
1757 * follow that suggestion if the object is sufficiently
1758 * large.
1760 * The hardware cache alignment cannot override the
1761 * specified alignment though. If that is greater
1762 * then use it.
1764 if ((flags & SLAB_HWCACHE_ALIGN) &&
1765 size > cache_line_size() / 2)
1766 return max_t(unsigned long, align, cache_line_size());
1768 if (align < ARCH_SLAB_MINALIGN)
1769 return ARCH_SLAB_MINALIGN;
1771 return ALIGN(align, sizeof(void *));
1774 static void init_kmem_cache_node(struct kmem_cache_node *n)
1776 n->nr_partial = 0;
1777 atomic_long_set(&n->nr_slabs, 0);
1778 spin_lock_init(&n->list_lock);
1779 INIT_LIST_HEAD(&n->partial);
1780 INIT_LIST_HEAD(&n->full);
1783 #ifdef CONFIG_NUMA
1785 * No kmalloc_node yet so do it by hand. We know that this is the first
1786 * slab on the node for this slabcache. There are no concurrent accesses
1787 * possible.
1789 * Note that this function only works on the kmalloc_node_cache
1790 * when allocating for the kmalloc_node_cache.
1792 static struct kmem_cache_node * __init early_kmem_cache_node_alloc(gfp_t gfpflags,
1793 int node)
1795 struct page *page;
1796 struct kmem_cache_node *n;
1798 BUG_ON(kmalloc_caches->size < sizeof(struct kmem_cache_node));
1800 page = new_slab(kmalloc_caches, gfpflags | GFP_THISNODE, node);
1801 /* new_slab() disables interupts */
1802 local_irq_enable();
1804 BUG_ON(!page);
1805 n = page->freelist;
1806 BUG_ON(!n);
1807 page->freelist = get_freepointer(kmalloc_caches, n);
1808 page->inuse++;
1809 kmalloc_caches->node[node] = n;
1810 setup_object_debug(kmalloc_caches, page, n);
1811 init_kmem_cache_node(n);
1812 atomic_long_inc(&n->nr_slabs);
1813 add_partial(n, page);
1814 return n;
1817 static void free_kmem_cache_nodes(struct kmem_cache *s)
1819 int node;
1821 for_each_online_node(node) {
1822 struct kmem_cache_node *n = s->node[node];
1823 if (n && n != &s->local_node)
1824 kmem_cache_free(kmalloc_caches, n);
1825 s->node[node] = NULL;
1829 static int init_kmem_cache_nodes(struct kmem_cache *s, gfp_t gfpflags)
1831 int node;
1832 int local_node;
1834 if (slab_state >= UP)
1835 local_node = page_to_nid(virt_to_page(s));
1836 else
1837 local_node = 0;
1839 for_each_online_node(node) {
1840 struct kmem_cache_node *n;
1842 if (local_node == node)
1843 n = &s->local_node;
1844 else {
1845 if (slab_state == DOWN) {
1846 n = early_kmem_cache_node_alloc(gfpflags,
1847 node);
1848 continue;
1850 n = kmem_cache_alloc_node(kmalloc_caches,
1851 gfpflags, node);
1853 if (!n) {
1854 free_kmem_cache_nodes(s);
1855 return 0;
1859 s->node[node] = n;
1860 init_kmem_cache_node(n);
1862 return 1;
1864 #else
1865 static void free_kmem_cache_nodes(struct kmem_cache *s)
1869 static int init_kmem_cache_nodes(struct kmem_cache *s, gfp_t gfpflags)
1871 init_kmem_cache_node(&s->local_node);
1872 return 1;
1874 #endif
1877 * calculate_sizes() determines the order and the distribution of data within
1878 * a slab object.
1880 static int calculate_sizes(struct kmem_cache *s)
1882 unsigned long flags = s->flags;
1883 unsigned long size = s->objsize;
1884 unsigned long align = s->align;
1887 * Determine if we can poison the object itself. If the user of
1888 * the slab may touch the object after free or before allocation
1889 * then we should never poison the object itself.
1891 if ((flags & SLAB_POISON) && !(flags & SLAB_DESTROY_BY_RCU) &&
1892 !s->ctor)
1893 s->flags |= __OBJECT_POISON;
1894 else
1895 s->flags &= ~__OBJECT_POISON;
1898 * Round up object size to the next word boundary. We can only
1899 * place the free pointer at word boundaries and this determines
1900 * the possible location of the free pointer.
1902 size = ALIGN(size, sizeof(void *));
1904 #ifdef CONFIG_SLUB_DEBUG
1906 * If we are Redzoning then check if there is some space between the
1907 * end of the object and the free pointer. If not then add an
1908 * additional word to have some bytes to store Redzone information.
1910 if ((flags & SLAB_RED_ZONE) && size == s->objsize)
1911 size += sizeof(void *);
1912 #endif
1915 * With that we have determined the number of bytes in actual use
1916 * by the object. This is the potential offset to the free pointer.
1918 s->inuse = size;
1920 if (((flags & (SLAB_DESTROY_BY_RCU | SLAB_POISON)) ||
1921 s->ctor)) {
1923 * Relocate free pointer after the object if it is not
1924 * permitted to overwrite the first word of the object on
1925 * kmem_cache_free.
1927 * This is the case if we do RCU, have a constructor or
1928 * destructor or are poisoning the objects.
1930 s->offset = size;
1931 size += sizeof(void *);
1934 #ifdef CONFIG_SLUB_DEBUG
1935 if (flags & SLAB_STORE_USER)
1937 * Need to store information about allocs and frees after
1938 * the object.
1940 size += 2 * sizeof(struct track);
1942 if (flags & SLAB_RED_ZONE)
1944 * Add some empty padding so that we can catch
1945 * overwrites from earlier objects rather than let
1946 * tracking information or the free pointer be
1947 * corrupted if an user writes before the start
1948 * of the object.
1950 size += sizeof(void *);
1951 #endif
1954 * Determine the alignment based on various parameters that the
1955 * user specified and the dynamic determination of cache line size
1956 * on bootup.
1958 align = calculate_alignment(flags, align, s->objsize);
1961 * SLUB stores one object immediately after another beginning from
1962 * offset 0. In order to align the objects we have to simply size
1963 * each object to conform to the alignment.
1965 size = ALIGN(size, align);
1966 s->size = size;
1968 s->order = calculate_order(size);
1969 if (s->order < 0)
1970 return 0;
1973 * Determine the number of objects per slab
1975 s->objects = (PAGE_SIZE << s->order) / size;
1978 * Verify that the number of objects is within permitted limits.
1979 * The page->inuse field is only 16 bit wide! So we cannot have
1980 * more than 64k objects per slab.
1982 if (!s->objects || s->objects > 65535)
1983 return 0;
1984 return 1;
1988 static int kmem_cache_open(struct kmem_cache *s, gfp_t gfpflags,
1989 const char *name, size_t size,
1990 size_t align, unsigned long flags,
1991 void (*ctor)(void *, struct kmem_cache *, unsigned long))
1993 memset(s, 0, kmem_size);
1994 s->name = name;
1995 s->ctor = ctor;
1996 s->objsize = size;
1997 s->flags = flags;
1998 s->align = align;
1999 kmem_cache_open_debug_check(s);
2001 if (!calculate_sizes(s))
2002 goto error;
2004 s->refcount = 1;
2005 #ifdef CONFIG_NUMA
2006 s->defrag_ratio = 100;
2007 #endif
2009 if (init_kmem_cache_nodes(s, gfpflags & ~SLUB_DMA))
2010 return 1;
2011 error:
2012 if (flags & SLAB_PANIC)
2013 panic("Cannot create slab %s size=%lu realsize=%u "
2014 "order=%u offset=%u flags=%lx\n",
2015 s->name, (unsigned long)size, s->size, s->order,
2016 s->offset, flags);
2017 return 0;
2019 EXPORT_SYMBOL(kmem_cache_open);
2022 * Check if a given pointer is valid
2024 int kmem_ptr_validate(struct kmem_cache *s, const void *object)
2026 struct page * page;
2028 page = get_object_page(object);
2030 if (!page || s != page->slab)
2031 /* No slab or wrong slab */
2032 return 0;
2034 if (!check_valid_pointer(s, page, object))
2035 return 0;
2038 * We could also check if the object is on the slabs freelist.
2039 * But this would be too expensive and it seems that the main
2040 * purpose of kmem_ptr_valid is to check if the object belongs
2041 * to a certain slab.
2043 return 1;
2045 EXPORT_SYMBOL(kmem_ptr_validate);
2048 * Determine the size of a slab object
2050 unsigned int kmem_cache_size(struct kmem_cache *s)
2052 return s->objsize;
2054 EXPORT_SYMBOL(kmem_cache_size);
2056 const char *kmem_cache_name(struct kmem_cache *s)
2058 return s->name;
2060 EXPORT_SYMBOL(kmem_cache_name);
2063 * Attempt to free all slabs on a node. Return the number of slabs we
2064 * were unable to free.
2066 static int free_list(struct kmem_cache *s, struct kmem_cache_node *n,
2067 struct list_head *list)
2069 int slabs_inuse = 0;
2070 unsigned long flags;
2071 struct page *page, *h;
2073 spin_lock_irqsave(&n->list_lock, flags);
2074 list_for_each_entry_safe(page, h, list, lru)
2075 if (!page->inuse) {
2076 list_del(&page->lru);
2077 discard_slab(s, page);
2078 } else
2079 slabs_inuse++;
2080 spin_unlock_irqrestore(&n->list_lock, flags);
2081 return slabs_inuse;
2085 * Release all resources used by a slab cache.
2087 static int kmem_cache_close(struct kmem_cache *s)
2089 int node;
2091 flush_all(s);
2093 /* Attempt to free all objects */
2094 for_each_online_node(node) {
2095 struct kmem_cache_node *n = get_node(s, node);
2097 n->nr_partial -= free_list(s, n, &n->partial);
2098 if (atomic_long_read(&n->nr_slabs))
2099 return 1;
2101 free_kmem_cache_nodes(s);
2102 return 0;
2106 * Close a cache and release the kmem_cache structure
2107 * (must be used for caches created using kmem_cache_create)
2109 void kmem_cache_destroy(struct kmem_cache *s)
2111 down_write(&slub_lock);
2112 s->refcount--;
2113 if (!s->refcount) {
2114 list_del(&s->list);
2115 if (kmem_cache_close(s))
2116 WARN_ON(1);
2117 sysfs_slab_remove(s);
2118 kfree(s);
2120 up_write(&slub_lock);
2122 EXPORT_SYMBOL(kmem_cache_destroy);
2124 /********************************************************************
2125 * Kmalloc subsystem
2126 *******************************************************************/
2128 struct kmem_cache kmalloc_caches[KMALLOC_SHIFT_HIGH + 1] __cacheline_aligned;
2129 EXPORT_SYMBOL(kmalloc_caches);
2131 #ifdef CONFIG_ZONE_DMA
2132 static struct kmem_cache *kmalloc_caches_dma[KMALLOC_SHIFT_HIGH + 1];
2133 #endif
2135 static int __init setup_slub_min_order(char *str)
2137 get_option (&str, &slub_min_order);
2139 return 1;
2142 __setup("slub_min_order=", setup_slub_min_order);
2144 static int __init setup_slub_max_order(char *str)
2146 get_option (&str, &slub_max_order);
2148 return 1;
2151 __setup("slub_max_order=", setup_slub_max_order);
2153 static int __init setup_slub_min_objects(char *str)
2155 get_option (&str, &slub_min_objects);
2157 return 1;
2160 __setup("slub_min_objects=", setup_slub_min_objects);
2162 static int __init setup_slub_nomerge(char *str)
2164 slub_nomerge = 1;
2165 return 1;
2168 __setup("slub_nomerge", setup_slub_nomerge);
2170 static struct kmem_cache *create_kmalloc_cache(struct kmem_cache *s,
2171 const char *name, int size, gfp_t gfp_flags)
2173 unsigned int flags = 0;
2175 if (gfp_flags & SLUB_DMA)
2176 flags = SLAB_CACHE_DMA;
2178 down_write(&slub_lock);
2179 if (!kmem_cache_open(s, gfp_flags, name, size, ARCH_KMALLOC_MINALIGN,
2180 flags, NULL))
2181 goto panic;
2183 list_add(&s->list, &slab_caches);
2184 up_write(&slub_lock);
2185 if (sysfs_slab_add(s))
2186 goto panic;
2187 return s;
2189 panic:
2190 panic("Creation of kmalloc slab %s size=%d failed.\n", name, size);
2193 static struct kmem_cache *get_slab(size_t size, gfp_t flags)
2195 int index = kmalloc_index(size);
2197 if (!index)
2198 return NULL;
2200 /* Allocation too large? */
2201 BUG_ON(index < 0);
2203 #ifdef CONFIG_ZONE_DMA
2204 if ((flags & SLUB_DMA)) {
2205 struct kmem_cache *s;
2206 struct kmem_cache *x;
2207 char *text;
2208 size_t realsize;
2210 s = kmalloc_caches_dma[index];
2211 if (s)
2212 return s;
2214 /* Dynamically create dma cache */
2215 x = kmalloc(kmem_size, flags & ~SLUB_DMA);
2216 if (!x)
2217 panic("Unable to allocate memory for dma cache\n");
2219 if (index <= KMALLOC_SHIFT_HIGH)
2220 realsize = 1 << index;
2221 else {
2222 if (index == 1)
2223 realsize = 96;
2224 else
2225 realsize = 192;
2228 text = kasprintf(flags & ~SLUB_DMA, "kmalloc_dma-%d",
2229 (unsigned int)realsize);
2230 s = create_kmalloc_cache(x, text, realsize, flags);
2231 kmalloc_caches_dma[index] = s;
2232 return s;
2234 #endif
2235 return &kmalloc_caches[index];
2238 void *__kmalloc(size_t size, gfp_t flags)
2240 struct kmem_cache *s = get_slab(size, flags);
2242 if (s)
2243 return slab_alloc(s, flags, -1, __builtin_return_address(0));
2244 return ZERO_SIZE_PTR;
2246 EXPORT_SYMBOL(__kmalloc);
2248 #ifdef CONFIG_NUMA
2249 void *__kmalloc_node(size_t size, gfp_t flags, int node)
2251 struct kmem_cache *s = get_slab(size, flags);
2253 if (s)
2254 return slab_alloc(s, flags, node, __builtin_return_address(0));
2255 return ZERO_SIZE_PTR;
2257 EXPORT_SYMBOL(__kmalloc_node);
2258 #endif
2260 size_t ksize(const void *object)
2262 struct page *page;
2263 struct kmem_cache *s;
2265 if (object == ZERO_SIZE_PTR)
2266 return 0;
2268 page = get_object_page(object);
2269 BUG_ON(!page);
2270 s = page->slab;
2271 BUG_ON(!s);
2274 * Debugging requires use of the padding between object
2275 * and whatever may come after it.
2277 if (s->flags & (SLAB_RED_ZONE | SLAB_POISON))
2278 return s->objsize;
2281 * If we have the need to store the freelist pointer
2282 * back there or track user information then we can
2283 * only use the space before that information.
2285 if (s->flags & (SLAB_DESTROY_BY_RCU | SLAB_STORE_USER))
2286 return s->inuse;
2289 * Else we can use all the padding etc for the allocation
2291 return s->size;
2293 EXPORT_SYMBOL(ksize);
2295 void kfree(const void *x)
2297 struct kmem_cache *s;
2298 struct page *page;
2301 * This has to be an unsigned comparison. According to Linus
2302 * some gcc version treat a pointer as a signed entity. Then
2303 * this comparison would be true for all "negative" pointers
2304 * (which would cover the whole upper half of the address space).
2306 if ((unsigned long)x <= (unsigned long)ZERO_SIZE_PTR)
2307 return;
2309 page = virt_to_head_page(x);
2310 s = page->slab;
2312 slab_free(s, page, (void *)x, __builtin_return_address(0));
2314 EXPORT_SYMBOL(kfree);
2317 * kmem_cache_shrink removes empty slabs from the partial lists and sorts
2318 * the remaining slabs by the number of items in use. The slabs with the
2319 * most items in use come first. New allocations will then fill those up
2320 * and thus they can be removed from the partial lists.
2322 * The slabs with the least items are placed last. This results in them
2323 * being allocated from last increasing the chance that the last objects
2324 * are freed in them.
2326 int kmem_cache_shrink(struct kmem_cache *s)
2328 int node;
2329 int i;
2330 struct kmem_cache_node *n;
2331 struct page *page;
2332 struct page *t;
2333 struct list_head *slabs_by_inuse =
2334 kmalloc(sizeof(struct list_head) * s->objects, GFP_KERNEL);
2335 unsigned long flags;
2337 if (!slabs_by_inuse)
2338 return -ENOMEM;
2340 flush_all(s);
2341 for_each_online_node(node) {
2342 n = get_node(s, node);
2344 if (!n->nr_partial)
2345 continue;
2347 for (i = 0; i < s->objects; i++)
2348 INIT_LIST_HEAD(slabs_by_inuse + i);
2350 spin_lock_irqsave(&n->list_lock, flags);
2353 * Build lists indexed by the items in use in each slab.
2355 * Note that concurrent frees may occur while we hold the
2356 * list_lock. page->inuse here is the upper limit.
2358 list_for_each_entry_safe(page, t, &n->partial, lru) {
2359 if (!page->inuse && slab_trylock(page)) {
2361 * Must hold slab lock here because slab_free
2362 * may have freed the last object and be
2363 * waiting to release the slab.
2365 list_del(&page->lru);
2366 n->nr_partial--;
2367 slab_unlock(page);
2368 discard_slab(s, page);
2369 } else {
2370 if (n->nr_partial > MAX_PARTIAL)
2371 list_move(&page->lru,
2372 slabs_by_inuse + page->inuse);
2376 if (n->nr_partial <= MAX_PARTIAL)
2377 goto out;
2380 * Rebuild the partial list with the slabs filled up most
2381 * first and the least used slabs at the end.
2383 for (i = s->objects - 1; i >= 0; i--)
2384 list_splice(slabs_by_inuse + i, n->partial.prev);
2386 out:
2387 spin_unlock_irqrestore(&n->list_lock, flags);
2390 kfree(slabs_by_inuse);
2391 return 0;
2393 EXPORT_SYMBOL(kmem_cache_shrink);
2396 * krealloc - reallocate memory. The contents will remain unchanged.
2397 * @p: object to reallocate memory for.
2398 * @new_size: how many bytes of memory are required.
2399 * @flags: the type of memory to allocate.
2401 * The contents of the object pointed to are preserved up to the
2402 * lesser of the new and old sizes. If @p is %NULL, krealloc()
2403 * behaves exactly like kmalloc(). If @size is 0 and @p is not a
2404 * %NULL pointer, the object pointed to is freed.
2406 void *krealloc(const void *p, size_t new_size, gfp_t flags)
2408 void *ret;
2409 size_t ks;
2411 if (unlikely(!p || p == ZERO_SIZE_PTR))
2412 return kmalloc(new_size, flags);
2414 if (unlikely(!new_size)) {
2415 kfree(p);
2416 return ZERO_SIZE_PTR;
2419 ks = ksize(p);
2420 if (ks >= new_size)
2421 return (void *)p;
2423 ret = kmalloc(new_size, flags);
2424 if (ret) {
2425 memcpy(ret, p, min(new_size, ks));
2426 kfree(p);
2428 return ret;
2430 EXPORT_SYMBOL(krealloc);
2432 /********************************************************************
2433 * Basic setup of slabs
2434 *******************************************************************/
2436 void __init kmem_cache_init(void)
2438 int i;
2440 #ifdef CONFIG_NUMA
2442 * Must first have the slab cache available for the allocations of the
2443 * struct kmem_cache_node's. There is special bootstrap code in
2444 * kmem_cache_open for slab_state == DOWN.
2446 create_kmalloc_cache(&kmalloc_caches[0], "kmem_cache_node",
2447 sizeof(struct kmem_cache_node), GFP_KERNEL);
2448 kmalloc_caches[0].refcount = -1;
2449 #endif
2451 /* Able to allocate the per node structures */
2452 slab_state = PARTIAL;
2454 /* Caches that are not of the two-to-the-power-of size */
2455 create_kmalloc_cache(&kmalloc_caches[1],
2456 "kmalloc-96", 96, GFP_KERNEL);
2457 create_kmalloc_cache(&kmalloc_caches[2],
2458 "kmalloc-192", 192, GFP_KERNEL);
2460 for (i = KMALLOC_SHIFT_LOW; i <= KMALLOC_SHIFT_HIGH; i++)
2461 create_kmalloc_cache(&kmalloc_caches[i],
2462 "kmalloc", 1 << i, GFP_KERNEL);
2464 slab_state = UP;
2466 /* Provide the correct kmalloc names now that the caches are up */
2467 for (i = KMALLOC_SHIFT_LOW; i <= KMALLOC_SHIFT_HIGH; i++)
2468 kmalloc_caches[i]. name =
2469 kasprintf(GFP_KERNEL, "kmalloc-%d", 1 << i);
2471 #ifdef CONFIG_SMP
2472 register_cpu_notifier(&slab_notifier);
2473 #endif
2475 kmem_size = offsetof(struct kmem_cache, cpu_slab) +
2476 nr_cpu_ids * sizeof(struct page *);
2478 printk(KERN_INFO "SLUB: Genslabs=%d, HWalign=%d, Order=%d-%d, MinObjects=%d,"
2479 " Processors=%d, Nodes=%d\n",
2480 KMALLOC_SHIFT_HIGH, cache_line_size(),
2481 slub_min_order, slub_max_order, slub_min_objects,
2482 nr_cpu_ids, nr_node_ids);
2486 * Find a mergeable slab cache
2488 static int slab_unmergeable(struct kmem_cache *s)
2490 if (slub_nomerge || (s->flags & SLUB_NEVER_MERGE))
2491 return 1;
2493 if (s->ctor)
2494 return 1;
2497 * We may have set a slab to be unmergeable during bootstrap.
2499 if (s->refcount < 0)
2500 return 1;
2502 return 0;
2505 static struct kmem_cache *find_mergeable(size_t size,
2506 size_t align, unsigned long flags,
2507 void (*ctor)(void *, struct kmem_cache *, unsigned long))
2509 struct list_head *h;
2511 if (slub_nomerge || (flags & SLUB_NEVER_MERGE))
2512 return NULL;
2514 if (ctor)
2515 return NULL;
2517 size = ALIGN(size, sizeof(void *));
2518 align = calculate_alignment(flags, align, size);
2519 size = ALIGN(size, align);
2521 list_for_each(h, &slab_caches) {
2522 struct kmem_cache *s =
2523 container_of(h, struct kmem_cache, list);
2525 if (slab_unmergeable(s))
2526 continue;
2528 if (size > s->size)
2529 continue;
2531 if (((flags | slub_debug) & SLUB_MERGE_SAME) !=
2532 (s->flags & SLUB_MERGE_SAME))
2533 continue;
2535 * Check if alignment is compatible.
2536 * Courtesy of Adrian Drzewiecki
2538 if ((s->size & ~(align -1)) != s->size)
2539 continue;
2541 if (s->size - size >= sizeof(void *))
2542 continue;
2544 return s;
2546 return NULL;
2549 struct kmem_cache *kmem_cache_create(const char *name, size_t size,
2550 size_t align, unsigned long flags,
2551 void (*ctor)(void *, struct kmem_cache *, unsigned long),
2552 void (*dtor)(void *, struct kmem_cache *, unsigned long))
2554 struct kmem_cache *s;
2556 BUG_ON(dtor);
2557 down_write(&slub_lock);
2558 s = find_mergeable(size, align, flags, ctor);
2559 if (s) {
2560 s->refcount++;
2562 * Adjust the object sizes so that we clear
2563 * the complete object on kzalloc.
2565 s->objsize = max(s->objsize, (int)size);
2566 s->inuse = max_t(int, s->inuse, ALIGN(size, sizeof(void *)));
2567 if (sysfs_slab_alias(s, name))
2568 goto err;
2569 } else {
2570 s = kmalloc(kmem_size, GFP_KERNEL);
2571 if (s && kmem_cache_open(s, GFP_KERNEL, name,
2572 size, align, flags, ctor)) {
2573 if (sysfs_slab_add(s)) {
2574 kfree(s);
2575 goto err;
2577 list_add(&s->list, &slab_caches);
2578 } else
2579 kfree(s);
2581 up_write(&slub_lock);
2582 return s;
2584 err:
2585 up_write(&slub_lock);
2586 if (flags & SLAB_PANIC)
2587 panic("Cannot create slabcache %s\n", name);
2588 else
2589 s = NULL;
2590 return s;
2592 EXPORT_SYMBOL(kmem_cache_create);
2594 void *kmem_cache_zalloc(struct kmem_cache *s, gfp_t flags)
2596 void *x;
2598 x = slab_alloc(s, flags, -1, __builtin_return_address(0));
2599 if (x)
2600 memset(x, 0, s->objsize);
2601 return x;
2603 EXPORT_SYMBOL(kmem_cache_zalloc);
2605 #ifdef CONFIG_SMP
2606 static void for_all_slabs(void (*func)(struct kmem_cache *, int), int cpu)
2608 struct list_head *h;
2610 down_read(&slub_lock);
2611 list_for_each(h, &slab_caches) {
2612 struct kmem_cache *s =
2613 container_of(h, struct kmem_cache, list);
2615 func(s, cpu);
2617 up_read(&slub_lock);
2621 * Version of __flush_cpu_slab for the case that interrupts
2622 * are enabled.
2624 static void cpu_slab_flush(struct kmem_cache *s, int cpu)
2626 unsigned long flags;
2628 local_irq_save(flags);
2629 __flush_cpu_slab(s, cpu);
2630 local_irq_restore(flags);
2634 * Use the cpu notifier to insure that the cpu slabs are flushed when
2635 * necessary.
2637 static int __cpuinit slab_cpuup_callback(struct notifier_block *nfb,
2638 unsigned long action, void *hcpu)
2640 long cpu = (long)hcpu;
2642 switch (action) {
2643 case CPU_UP_CANCELED:
2644 case CPU_UP_CANCELED_FROZEN:
2645 case CPU_DEAD:
2646 case CPU_DEAD_FROZEN:
2647 for_all_slabs(cpu_slab_flush, cpu);
2648 break;
2649 default:
2650 break;
2652 return NOTIFY_OK;
2655 static struct notifier_block __cpuinitdata slab_notifier =
2656 { &slab_cpuup_callback, NULL, 0 };
2658 #endif
2660 void *__kmalloc_track_caller(size_t size, gfp_t gfpflags, void *caller)
2662 struct kmem_cache *s = get_slab(size, gfpflags);
2664 if (!s)
2665 return ZERO_SIZE_PTR;
2667 return slab_alloc(s, gfpflags, -1, caller);
2670 void *__kmalloc_node_track_caller(size_t size, gfp_t gfpflags,
2671 int node, void *caller)
2673 struct kmem_cache *s = get_slab(size, gfpflags);
2675 if (!s)
2676 return ZERO_SIZE_PTR;
2678 return slab_alloc(s, gfpflags, node, caller);
2681 #if defined(CONFIG_SYSFS) && defined(CONFIG_SLUB_DEBUG)
2682 static int validate_slab(struct kmem_cache *s, struct page *page)
2684 void *p;
2685 void *addr = page_address(page);
2686 DECLARE_BITMAP(map, s->objects);
2688 if (!check_slab(s, page) ||
2689 !on_freelist(s, page, NULL))
2690 return 0;
2692 /* Now we know that a valid freelist exists */
2693 bitmap_zero(map, s->objects);
2695 for_each_free_object(p, s, page->freelist) {
2696 set_bit(slab_index(p, s, addr), map);
2697 if (!check_object(s, page, p, 0))
2698 return 0;
2701 for_each_object(p, s, addr)
2702 if (!test_bit(slab_index(p, s, addr), map))
2703 if (!check_object(s, page, p, 1))
2704 return 0;
2705 return 1;
2708 static void validate_slab_slab(struct kmem_cache *s, struct page *page)
2710 if (slab_trylock(page)) {
2711 validate_slab(s, page);
2712 slab_unlock(page);
2713 } else
2714 printk(KERN_INFO "SLUB %s: Skipped busy slab 0x%p\n",
2715 s->name, page);
2717 if (s->flags & DEBUG_DEFAULT_FLAGS) {
2718 if (!SlabDebug(page))
2719 printk(KERN_ERR "SLUB %s: SlabDebug not set "
2720 "on slab 0x%p\n", s->name, page);
2721 } else {
2722 if (SlabDebug(page))
2723 printk(KERN_ERR "SLUB %s: SlabDebug set on "
2724 "slab 0x%p\n", s->name, page);
2728 static int validate_slab_node(struct kmem_cache *s, struct kmem_cache_node *n)
2730 unsigned long count = 0;
2731 struct page *page;
2732 unsigned long flags;
2734 spin_lock_irqsave(&n->list_lock, flags);
2736 list_for_each_entry(page, &n->partial, lru) {
2737 validate_slab_slab(s, page);
2738 count++;
2740 if (count != n->nr_partial)
2741 printk(KERN_ERR "SLUB %s: %ld partial slabs counted but "
2742 "counter=%ld\n", s->name, count, n->nr_partial);
2744 if (!(s->flags & SLAB_STORE_USER))
2745 goto out;
2747 list_for_each_entry(page, &n->full, lru) {
2748 validate_slab_slab(s, page);
2749 count++;
2751 if (count != atomic_long_read(&n->nr_slabs))
2752 printk(KERN_ERR "SLUB: %s %ld slabs counted but "
2753 "counter=%ld\n", s->name, count,
2754 atomic_long_read(&n->nr_slabs));
2756 out:
2757 spin_unlock_irqrestore(&n->list_lock, flags);
2758 return count;
2761 static unsigned long validate_slab_cache(struct kmem_cache *s)
2763 int node;
2764 unsigned long count = 0;
2766 flush_all(s);
2767 for_each_online_node(node) {
2768 struct kmem_cache_node *n = get_node(s, node);
2770 count += validate_slab_node(s, n);
2772 return count;
2775 #ifdef SLUB_RESILIENCY_TEST
2776 static void resiliency_test(void)
2778 u8 *p;
2780 printk(KERN_ERR "SLUB resiliency testing\n");
2781 printk(KERN_ERR "-----------------------\n");
2782 printk(KERN_ERR "A. Corruption after allocation\n");
2784 p = kzalloc(16, GFP_KERNEL);
2785 p[16] = 0x12;
2786 printk(KERN_ERR "\n1. kmalloc-16: Clobber Redzone/next pointer"
2787 " 0x12->0x%p\n\n", p + 16);
2789 validate_slab_cache(kmalloc_caches + 4);
2791 /* Hmmm... The next two are dangerous */
2792 p = kzalloc(32, GFP_KERNEL);
2793 p[32 + sizeof(void *)] = 0x34;
2794 printk(KERN_ERR "\n2. kmalloc-32: Clobber next pointer/next slab"
2795 " 0x34 -> -0x%p\n", p);
2796 printk(KERN_ERR "If allocated object is overwritten then not detectable\n\n");
2798 validate_slab_cache(kmalloc_caches + 5);
2799 p = kzalloc(64, GFP_KERNEL);
2800 p += 64 + (get_cycles() & 0xff) * sizeof(void *);
2801 *p = 0x56;
2802 printk(KERN_ERR "\n3. kmalloc-64: corrupting random byte 0x56->0x%p\n",
2804 printk(KERN_ERR "If allocated object is overwritten then not detectable\n\n");
2805 validate_slab_cache(kmalloc_caches + 6);
2807 printk(KERN_ERR "\nB. Corruption after free\n");
2808 p = kzalloc(128, GFP_KERNEL);
2809 kfree(p);
2810 *p = 0x78;
2811 printk(KERN_ERR "1. kmalloc-128: Clobber first word 0x78->0x%p\n\n", p);
2812 validate_slab_cache(kmalloc_caches + 7);
2814 p = kzalloc(256, GFP_KERNEL);
2815 kfree(p);
2816 p[50] = 0x9a;
2817 printk(KERN_ERR "\n2. kmalloc-256: Clobber 50th byte 0x9a->0x%p\n\n", p);
2818 validate_slab_cache(kmalloc_caches + 8);
2820 p = kzalloc(512, GFP_KERNEL);
2821 kfree(p);
2822 p[512] = 0xab;
2823 printk(KERN_ERR "\n3. kmalloc-512: Clobber redzone 0xab->0x%p\n\n", p);
2824 validate_slab_cache(kmalloc_caches + 9);
2826 #else
2827 static void resiliency_test(void) {};
2828 #endif
2831 * Generate lists of code addresses where slabcache objects are allocated
2832 * and freed.
2835 struct location {
2836 unsigned long count;
2837 void *addr;
2838 long long sum_time;
2839 long min_time;
2840 long max_time;
2841 long min_pid;
2842 long max_pid;
2843 cpumask_t cpus;
2844 nodemask_t nodes;
2847 struct loc_track {
2848 unsigned long max;
2849 unsigned long count;
2850 struct location *loc;
2853 static void free_loc_track(struct loc_track *t)
2855 if (t->max)
2856 free_pages((unsigned long)t->loc,
2857 get_order(sizeof(struct location) * t->max));
2860 static int alloc_loc_track(struct loc_track *t, unsigned long max)
2862 struct location *l;
2863 int order;
2865 if (!max)
2866 max = PAGE_SIZE / sizeof(struct location);
2868 order = get_order(sizeof(struct location) * max);
2870 l = (void *)__get_free_pages(GFP_KERNEL, order);
2872 if (!l)
2873 return 0;
2875 if (t->count) {
2876 memcpy(l, t->loc, sizeof(struct location) * t->count);
2877 free_loc_track(t);
2879 t->max = max;
2880 t->loc = l;
2881 return 1;
2884 static int add_location(struct loc_track *t, struct kmem_cache *s,
2885 const struct track *track)
2887 long start, end, pos;
2888 struct location *l;
2889 void *caddr;
2890 unsigned long age = jiffies - track->when;
2892 start = -1;
2893 end = t->count;
2895 for ( ; ; ) {
2896 pos = start + (end - start + 1) / 2;
2899 * There is nothing at "end". If we end up there
2900 * we need to add something to before end.
2902 if (pos == end)
2903 break;
2905 caddr = t->loc[pos].addr;
2906 if (track->addr == caddr) {
2908 l = &t->loc[pos];
2909 l->count++;
2910 if (track->when) {
2911 l->sum_time += age;
2912 if (age < l->min_time)
2913 l->min_time = age;
2914 if (age > l->max_time)
2915 l->max_time = age;
2917 if (track->pid < l->min_pid)
2918 l->min_pid = track->pid;
2919 if (track->pid > l->max_pid)
2920 l->max_pid = track->pid;
2922 cpu_set(track->cpu, l->cpus);
2924 node_set(page_to_nid(virt_to_page(track)), l->nodes);
2925 return 1;
2928 if (track->addr < caddr)
2929 end = pos;
2930 else
2931 start = pos;
2935 * Not found. Insert new tracking element.
2937 if (t->count >= t->max && !alloc_loc_track(t, 2 * t->max))
2938 return 0;
2940 l = t->loc + pos;
2941 if (pos < t->count)
2942 memmove(l + 1, l,
2943 (t->count - pos) * sizeof(struct location));
2944 t->count++;
2945 l->count = 1;
2946 l->addr = track->addr;
2947 l->sum_time = age;
2948 l->min_time = age;
2949 l->max_time = age;
2950 l->min_pid = track->pid;
2951 l->max_pid = track->pid;
2952 cpus_clear(l->cpus);
2953 cpu_set(track->cpu, l->cpus);
2954 nodes_clear(l->nodes);
2955 node_set(page_to_nid(virt_to_page(track)), l->nodes);
2956 return 1;
2959 static void process_slab(struct loc_track *t, struct kmem_cache *s,
2960 struct page *page, enum track_item alloc)
2962 void *addr = page_address(page);
2963 DECLARE_BITMAP(map, s->objects);
2964 void *p;
2966 bitmap_zero(map, s->objects);
2967 for_each_free_object(p, s, page->freelist)
2968 set_bit(slab_index(p, s, addr), map);
2970 for_each_object(p, s, addr)
2971 if (!test_bit(slab_index(p, s, addr), map))
2972 add_location(t, s, get_track(s, p, alloc));
2975 static int list_locations(struct kmem_cache *s, char *buf,
2976 enum track_item alloc)
2978 int n = 0;
2979 unsigned long i;
2980 struct loc_track t;
2981 int node;
2983 t.count = 0;
2984 t.max = 0;
2986 /* Push back cpu slabs */
2987 flush_all(s);
2989 for_each_online_node(node) {
2990 struct kmem_cache_node *n = get_node(s, node);
2991 unsigned long flags;
2992 struct page *page;
2994 if (!atomic_read(&n->nr_slabs))
2995 continue;
2997 spin_lock_irqsave(&n->list_lock, flags);
2998 list_for_each_entry(page, &n->partial, lru)
2999 process_slab(&t, s, page, alloc);
3000 list_for_each_entry(page, &n->full, lru)
3001 process_slab(&t, s, page, alloc);
3002 spin_unlock_irqrestore(&n->list_lock, flags);
3005 for (i = 0; i < t.count; i++) {
3006 struct location *l = &t.loc[i];
3008 if (n > PAGE_SIZE - 100)
3009 break;
3010 n += sprintf(buf + n, "%7ld ", l->count);
3012 if (l->addr)
3013 n += sprint_symbol(buf + n, (unsigned long)l->addr);
3014 else
3015 n += sprintf(buf + n, "<not-available>");
3017 if (l->sum_time != l->min_time) {
3018 unsigned long remainder;
3020 n += sprintf(buf + n, " age=%ld/%ld/%ld",
3021 l->min_time,
3022 div_long_long_rem(l->sum_time, l->count, &remainder),
3023 l->max_time);
3024 } else
3025 n += sprintf(buf + n, " age=%ld",
3026 l->min_time);
3028 if (l->min_pid != l->max_pid)
3029 n += sprintf(buf + n, " pid=%ld-%ld",
3030 l->min_pid, l->max_pid);
3031 else
3032 n += sprintf(buf + n, " pid=%ld",
3033 l->min_pid);
3035 if (num_online_cpus() > 1 && !cpus_empty(l->cpus)) {
3036 n += sprintf(buf + n, " cpus=");
3037 n += cpulist_scnprintf(buf + n, PAGE_SIZE - n - 50,
3038 l->cpus);
3041 if (num_online_nodes() > 1 && !nodes_empty(l->nodes)) {
3042 n += sprintf(buf + n, " nodes=");
3043 n += nodelist_scnprintf(buf + n, PAGE_SIZE - n - 50,
3044 l->nodes);
3047 n += sprintf(buf + n, "\n");
3050 free_loc_track(&t);
3051 if (!t.count)
3052 n += sprintf(buf, "No data\n");
3053 return n;
3056 static unsigned long count_partial(struct kmem_cache_node *n)
3058 unsigned long flags;
3059 unsigned long x = 0;
3060 struct page *page;
3062 spin_lock_irqsave(&n->list_lock, flags);
3063 list_for_each_entry(page, &n->partial, lru)
3064 x += page->inuse;
3065 spin_unlock_irqrestore(&n->list_lock, flags);
3066 return x;
3069 enum slab_stat_type {
3070 SL_FULL,
3071 SL_PARTIAL,
3072 SL_CPU,
3073 SL_OBJECTS
3076 #define SO_FULL (1 << SL_FULL)
3077 #define SO_PARTIAL (1 << SL_PARTIAL)
3078 #define SO_CPU (1 << SL_CPU)
3079 #define SO_OBJECTS (1 << SL_OBJECTS)
3081 static unsigned long slab_objects(struct kmem_cache *s,
3082 char *buf, unsigned long flags)
3084 unsigned long total = 0;
3085 int cpu;
3086 int node;
3087 int x;
3088 unsigned long *nodes;
3089 unsigned long *per_cpu;
3091 nodes = kzalloc(2 * sizeof(unsigned long) * nr_node_ids, GFP_KERNEL);
3092 per_cpu = nodes + nr_node_ids;
3094 for_each_possible_cpu(cpu) {
3095 struct page *page = s->cpu_slab[cpu];
3096 int node;
3098 if (page) {
3099 node = page_to_nid(page);
3100 if (flags & SO_CPU) {
3101 int x = 0;
3103 if (flags & SO_OBJECTS)
3104 x = page->inuse;
3105 else
3106 x = 1;
3107 total += x;
3108 nodes[node] += x;
3110 per_cpu[node]++;
3114 for_each_online_node(node) {
3115 struct kmem_cache_node *n = get_node(s, node);
3117 if (flags & SO_PARTIAL) {
3118 if (flags & SO_OBJECTS)
3119 x = count_partial(n);
3120 else
3121 x = n->nr_partial;
3122 total += x;
3123 nodes[node] += x;
3126 if (flags & SO_FULL) {
3127 int full_slabs = atomic_read(&n->nr_slabs)
3128 - per_cpu[node]
3129 - n->nr_partial;
3131 if (flags & SO_OBJECTS)
3132 x = full_slabs * s->objects;
3133 else
3134 x = full_slabs;
3135 total += x;
3136 nodes[node] += x;
3140 x = sprintf(buf, "%lu", total);
3141 #ifdef CONFIG_NUMA
3142 for_each_online_node(node)
3143 if (nodes[node])
3144 x += sprintf(buf + x, " N%d=%lu",
3145 node, nodes[node]);
3146 #endif
3147 kfree(nodes);
3148 return x + sprintf(buf + x, "\n");
3151 static int any_slab_objects(struct kmem_cache *s)
3153 int node;
3154 int cpu;
3156 for_each_possible_cpu(cpu)
3157 if (s->cpu_slab[cpu])
3158 return 1;
3160 for_each_node(node) {
3161 struct kmem_cache_node *n = get_node(s, node);
3163 if (n->nr_partial || atomic_read(&n->nr_slabs))
3164 return 1;
3166 return 0;
3169 #define to_slab_attr(n) container_of(n, struct slab_attribute, attr)
3170 #define to_slab(n) container_of(n, struct kmem_cache, kobj);
3172 struct slab_attribute {
3173 struct attribute attr;
3174 ssize_t (*show)(struct kmem_cache *s, char *buf);
3175 ssize_t (*store)(struct kmem_cache *s, const char *x, size_t count);
3178 #define SLAB_ATTR_RO(_name) \
3179 static struct slab_attribute _name##_attr = __ATTR_RO(_name)
3181 #define SLAB_ATTR(_name) \
3182 static struct slab_attribute _name##_attr = \
3183 __ATTR(_name, 0644, _name##_show, _name##_store)
3185 static ssize_t slab_size_show(struct kmem_cache *s, char *buf)
3187 return sprintf(buf, "%d\n", s->size);
3189 SLAB_ATTR_RO(slab_size);
3191 static ssize_t align_show(struct kmem_cache *s, char *buf)
3193 return sprintf(buf, "%d\n", s->align);
3195 SLAB_ATTR_RO(align);
3197 static ssize_t object_size_show(struct kmem_cache *s, char *buf)
3199 return sprintf(buf, "%d\n", s->objsize);
3201 SLAB_ATTR_RO(object_size);
3203 static ssize_t objs_per_slab_show(struct kmem_cache *s, char *buf)
3205 return sprintf(buf, "%d\n", s->objects);
3207 SLAB_ATTR_RO(objs_per_slab);
3209 static ssize_t order_show(struct kmem_cache *s, char *buf)
3211 return sprintf(buf, "%d\n", s->order);
3213 SLAB_ATTR_RO(order);
3215 static ssize_t ctor_show(struct kmem_cache *s, char *buf)
3217 if (s->ctor) {
3218 int n = sprint_symbol(buf, (unsigned long)s->ctor);
3220 return n + sprintf(buf + n, "\n");
3222 return 0;
3224 SLAB_ATTR_RO(ctor);
3226 static ssize_t aliases_show(struct kmem_cache *s, char *buf)
3228 return sprintf(buf, "%d\n", s->refcount - 1);
3230 SLAB_ATTR_RO(aliases);
3232 static ssize_t slabs_show(struct kmem_cache *s, char *buf)
3234 return slab_objects(s, buf, SO_FULL|SO_PARTIAL|SO_CPU);
3236 SLAB_ATTR_RO(slabs);
3238 static ssize_t partial_show(struct kmem_cache *s, char *buf)
3240 return slab_objects(s, buf, SO_PARTIAL);
3242 SLAB_ATTR_RO(partial);
3244 static ssize_t cpu_slabs_show(struct kmem_cache *s, char *buf)
3246 return slab_objects(s, buf, SO_CPU);
3248 SLAB_ATTR_RO(cpu_slabs);
3250 static ssize_t objects_show(struct kmem_cache *s, char *buf)
3252 return slab_objects(s, buf, SO_FULL|SO_PARTIAL|SO_CPU|SO_OBJECTS);
3254 SLAB_ATTR_RO(objects);
3256 static ssize_t sanity_checks_show(struct kmem_cache *s, char *buf)
3258 return sprintf(buf, "%d\n", !!(s->flags & SLAB_DEBUG_FREE));
3261 static ssize_t sanity_checks_store(struct kmem_cache *s,
3262 const char *buf, size_t length)
3264 s->flags &= ~SLAB_DEBUG_FREE;
3265 if (buf[0] == '1')
3266 s->flags |= SLAB_DEBUG_FREE;
3267 return length;
3269 SLAB_ATTR(sanity_checks);
3271 static ssize_t trace_show(struct kmem_cache *s, char *buf)
3273 return sprintf(buf, "%d\n", !!(s->flags & SLAB_TRACE));
3276 static ssize_t trace_store(struct kmem_cache *s, const char *buf,
3277 size_t length)
3279 s->flags &= ~SLAB_TRACE;
3280 if (buf[0] == '1')
3281 s->flags |= SLAB_TRACE;
3282 return length;
3284 SLAB_ATTR(trace);
3286 static ssize_t reclaim_account_show(struct kmem_cache *s, char *buf)
3288 return sprintf(buf, "%d\n", !!(s->flags & SLAB_RECLAIM_ACCOUNT));
3291 static ssize_t reclaim_account_store(struct kmem_cache *s,
3292 const char *buf, size_t length)
3294 s->flags &= ~SLAB_RECLAIM_ACCOUNT;
3295 if (buf[0] == '1')
3296 s->flags |= SLAB_RECLAIM_ACCOUNT;
3297 return length;
3299 SLAB_ATTR(reclaim_account);
3301 static ssize_t hwcache_align_show(struct kmem_cache *s, char *buf)
3303 return sprintf(buf, "%d\n", !!(s->flags & SLAB_HWCACHE_ALIGN));
3305 SLAB_ATTR_RO(hwcache_align);
3307 #ifdef CONFIG_ZONE_DMA
3308 static ssize_t cache_dma_show(struct kmem_cache *s, char *buf)
3310 return sprintf(buf, "%d\n", !!(s->flags & SLAB_CACHE_DMA));
3312 SLAB_ATTR_RO(cache_dma);
3313 #endif
3315 static ssize_t destroy_by_rcu_show(struct kmem_cache *s, char *buf)
3317 return sprintf(buf, "%d\n", !!(s->flags & SLAB_DESTROY_BY_RCU));
3319 SLAB_ATTR_RO(destroy_by_rcu);
3321 static ssize_t red_zone_show(struct kmem_cache *s, char *buf)
3323 return sprintf(buf, "%d\n", !!(s->flags & SLAB_RED_ZONE));
3326 static ssize_t red_zone_store(struct kmem_cache *s,
3327 const char *buf, size_t length)
3329 if (any_slab_objects(s))
3330 return -EBUSY;
3332 s->flags &= ~SLAB_RED_ZONE;
3333 if (buf[0] == '1')
3334 s->flags |= SLAB_RED_ZONE;
3335 calculate_sizes(s);
3336 return length;
3338 SLAB_ATTR(red_zone);
3340 static ssize_t poison_show(struct kmem_cache *s, char *buf)
3342 return sprintf(buf, "%d\n", !!(s->flags & SLAB_POISON));
3345 static ssize_t poison_store(struct kmem_cache *s,
3346 const char *buf, size_t length)
3348 if (any_slab_objects(s))
3349 return -EBUSY;
3351 s->flags &= ~SLAB_POISON;
3352 if (buf[0] == '1')
3353 s->flags |= SLAB_POISON;
3354 calculate_sizes(s);
3355 return length;
3357 SLAB_ATTR(poison);
3359 static ssize_t store_user_show(struct kmem_cache *s, char *buf)
3361 return sprintf(buf, "%d\n", !!(s->flags & SLAB_STORE_USER));
3364 static ssize_t store_user_store(struct kmem_cache *s,
3365 const char *buf, size_t length)
3367 if (any_slab_objects(s))
3368 return -EBUSY;
3370 s->flags &= ~SLAB_STORE_USER;
3371 if (buf[0] == '1')
3372 s->flags |= SLAB_STORE_USER;
3373 calculate_sizes(s);
3374 return length;
3376 SLAB_ATTR(store_user);
3378 static ssize_t validate_show(struct kmem_cache *s, char *buf)
3380 return 0;
3383 static ssize_t validate_store(struct kmem_cache *s,
3384 const char *buf, size_t length)
3386 if (buf[0] == '1')
3387 validate_slab_cache(s);
3388 else
3389 return -EINVAL;
3390 return length;
3392 SLAB_ATTR(validate);
3394 static ssize_t shrink_show(struct kmem_cache *s, char *buf)
3396 return 0;
3399 static ssize_t shrink_store(struct kmem_cache *s,
3400 const char *buf, size_t length)
3402 if (buf[0] == '1') {
3403 int rc = kmem_cache_shrink(s);
3405 if (rc)
3406 return rc;
3407 } else
3408 return -EINVAL;
3409 return length;
3411 SLAB_ATTR(shrink);
3413 static ssize_t alloc_calls_show(struct kmem_cache *s, char *buf)
3415 if (!(s->flags & SLAB_STORE_USER))
3416 return -ENOSYS;
3417 return list_locations(s, buf, TRACK_ALLOC);
3419 SLAB_ATTR_RO(alloc_calls);
3421 static ssize_t free_calls_show(struct kmem_cache *s, char *buf)
3423 if (!(s->flags & SLAB_STORE_USER))
3424 return -ENOSYS;
3425 return list_locations(s, buf, TRACK_FREE);
3427 SLAB_ATTR_RO(free_calls);
3429 #ifdef CONFIG_NUMA
3430 static ssize_t defrag_ratio_show(struct kmem_cache *s, char *buf)
3432 return sprintf(buf, "%d\n", s->defrag_ratio / 10);
3435 static ssize_t defrag_ratio_store(struct kmem_cache *s,
3436 const char *buf, size_t length)
3438 int n = simple_strtoul(buf, NULL, 10);
3440 if (n < 100)
3441 s->defrag_ratio = n * 10;
3442 return length;
3444 SLAB_ATTR(defrag_ratio);
3445 #endif
3447 static struct attribute * slab_attrs[] = {
3448 &slab_size_attr.attr,
3449 &object_size_attr.attr,
3450 &objs_per_slab_attr.attr,
3451 &order_attr.attr,
3452 &objects_attr.attr,
3453 &slabs_attr.attr,
3454 &partial_attr.attr,
3455 &cpu_slabs_attr.attr,
3456 &ctor_attr.attr,
3457 &aliases_attr.attr,
3458 &align_attr.attr,
3459 &sanity_checks_attr.attr,
3460 &trace_attr.attr,
3461 &hwcache_align_attr.attr,
3462 &reclaim_account_attr.attr,
3463 &destroy_by_rcu_attr.attr,
3464 &red_zone_attr.attr,
3465 &poison_attr.attr,
3466 &store_user_attr.attr,
3467 &validate_attr.attr,
3468 &shrink_attr.attr,
3469 &alloc_calls_attr.attr,
3470 &free_calls_attr.attr,
3471 #ifdef CONFIG_ZONE_DMA
3472 &cache_dma_attr.attr,
3473 #endif
3474 #ifdef CONFIG_NUMA
3475 &defrag_ratio_attr.attr,
3476 #endif
3477 NULL
3480 static struct attribute_group slab_attr_group = {
3481 .attrs = slab_attrs,
3484 static ssize_t slab_attr_show(struct kobject *kobj,
3485 struct attribute *attr,
3486 char *buf)
3488 struct slab_attribute *attribute;
3489 struct kmem_cache *s;
3490 int err;
3492 attribute = to_slab_attr(attr);
3493 s = to_slab(kobj);
3495 if (!attribute->show)
3496 return -EIO;
3498 err = attribute->show(s, buf);
3500 return err;
3503 static ssize_t slab_attr_store(struct kobject *kobj,
3504 struct attribute *attr,
3505 const char *buf, size_t len)
3507 struct slab_attribute *attribute;
3508 struct kmem_cache *s;
3509 int err;
3511 attribute = to_slab_attr(attr);
3512 s = to_slab(kobj);
3514 if (!attribute->store)
3515 return -EIO;
3517 err = attribute->store(s, buf, len);
3519 return err;
3522 static struct sysfs_ops slab_sysfs_ops = {
3523 .show = slab_attr_show,
3524 .store = slab_attr_store,
3527 static struct kobj_type slab_ktype = {
3528 .sysfs_ops = &slab_sysfs_ops,
3531 static int uevent_filter(struct kset *kset, struct kobject *kobj)
3533 struct kobj_type *ktype = get_ktype(kobj);
3535 if (ktype == &slab_ktype)
3536 return 1;
3537 return 0;
3540 static struct kset_uevent_ops slab_uevent_ops = {
3541 .filter = uevent_filter,
3544 decl_subsys(slab, &slab_ktype, &slab_uevent_ops);
3546 #define ID_STR_LENGTH 64
3548 /* Create a unique string id for a slab cache:
3549 * format
3550 * :[flags-]size:[memory address of kmemcache]
3552 static char *create_unique_id(struct kmem_cache *s)
3554 char *name = kmalloc(ID_STR_LENGTH, GFP_KERNEL);
3555 char *p = name;
3557 BUG_ON(!name);
3559 *p++ = ':';
3561 * First flags affecting slabcache operations. We will only
3562 * get here for aliasable slabs so we do not need to support
3563 * too many flags. The flags here must cover all flags that
3564 * are matched during merging to guarantee that the id is
3565 * unique.
3567 if (s->flags & SLAB_CACHE_DMA)
3568 *p++ = 'd';
3569 if (s->flags & SLAB_RECLAIM_ACCOUNT)
3570 *p++ = 'a';
3571 if (s->flags & SLAB_DEBUG_FREE)
3572 *p++ = 'F';
3573 if (p != name + 1)
3574 *p++ = '-';
3575 p += sprintf(p, "%07d", s->size);
3576 BUG_ON(p > name + ID_STR_LENGTH - 1);
3577 return name;
3580 static int sysfs_slab_add(struct kmem_cache *s)
3582 int err;
3583 const char *name;
3584 int unmergeable;
3586 if (slab_state < SYSFS)
3587 /* Defer until later */
3588 return 0;
3590 unmergeable = slab_unmergeable(s);
3591 if (unmergeable) {
3593 * Slabcache can never be merged so we can use the name proper.
3594 * This is typically the case for debug situations. In that
3595 * case we can catch duplicate names easily.
3597 sysfs_remove_link(&slab_subsys.kobj, s->name);
3598 name = s->name;
3599 } else {
3601 * Create a unique name for the slab as a target
3602 * for the symlinks.
3604 name = create_unique_id(s);
3607 kobj_set_kset_s(s, slab_subsys);
3608 kobject_set_name(&s->kobj, name);
3609 kobject_init(&s->kobj);
3610 err = kobject_add(&s->kobj);
3611 if (err)
3612 return err;
3614 err = sysfs_create_group(&s->kobj, &slab_attr_group);
3615 if (err)
3616 return err;
3617 kobject_uevent(&s->kobj, KOBJ_ADD);
3618 if (!unmergeable) {
3619 /* Setup first alias */
3620 sysfs_slab_alias(s, s->name);
3621 kfree(name);
3623 return 0;
3626 static void sysfs_slab_remove(struct kmem_cache *s)
3628 kobject_uevent(&s->kobj, KOBJ_REMOVE);
3629 kobject_del(&s->kobj);
3633 * Need to buffer aliases during bootup until sysfs becomes
3634 * available lest we loose that information.
3636 struct saved_alias {
3637 struct kmem_cache *s;
3638 const char *name;
3639 struct saved_alias *next;
3642 struct saved_alias *alias_list;
3644 static int sysfs_slab_alias(struct kmem_cache *s, const char *name)
3646 struct saved_alias *al;
3648 if (slab_state == SYSFS) {
3650 * If we have a leftover link then remove it.
3652 sysfs_remove_link(&slab_subsys.kobj, name);
3653 return sysfs_create_link(&slab_subsys.kobj,
3654 &s->kobj, name);
3657 al = kmalloc(sizeof(struct saved_alias), GFP_KERNEL);
3658 if (!al)
3659 return -ENOMEM;
3661 al->s = s;
3662 al->name = name;
3663 al->next = alias_list;
3664 alias_list = al;
3665 return 0;
3668 static int __init slab_sysfs_init(void)
3670 struct list_head *h;
3671 int err;
3673 err = subsystem_register(&slab_subsys);
3674 if (err) {
3675 printk(KERN_ERR "Cannot register slab subsystem.\n");
3676 return -ENOSYS;
3679 slab_state = SYSFS;
3681 list_for_each(h, &slab_caches) {
3682 struct kmem_cache *s =
3683 container_of(h, struct kmem_cache, list);
3685 err = sysfs_slab_add(s);
3686 BUG_ON(err);
3689 while (alias_list) {
3690 struct saved_alias *al = alias_list;
3692 alias_list = alias_list->next;
3693 err = sysfs_slab_alias(al->s, al->name);
3694 BUG_ON(err);
3695 kfree(al);
3698 resiliency_test();
3699 return 0;
3702 __initcall(slab_sysfs_init);
3703 #endif