slub: enable tracking of full slabs
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / mm / slub.c
blobc4f40d373d1e5ec7050671faa8a109031c76f2ee
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.
70 * There is no list for full slabs. If an object in a full slab is
71 * freed then the slab will show up again on the partial lists.
72 * Otherwise there is no need to track full slabs unless we have to
73 * track full slabs for debugging purposes.
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 used as a cpu cache. Allocations
82 * may be performed from the slab. The slab is not
83 * on any slab list and cannot be moved onto one.
85 * PageError Slab requires special handling due to debug
86 * options set. This moves slab handling out of
87 * the fast path.
91 * Issues still to be resolved:
93 * - The per cpu array is updated for each new slab and and is a remote
94 * cacheline for most nodes. This could become a bouncing cacheline given
95 * enough frequent updates. There are 16 pointers in a cacheline.so at
96 * max 16 cpus could compete. Likely okay.
98 * - Support PAGE_ALLOC_DEBUG. Should be easy to do.
100 * - Support DEBUG_SLAB_LEAK. Trouble is we do not know where the full
101 * slabs are in SLUB.
103 * - SLAB_DEBUG_INITIAL is not supported but I have never seen a use of
104 * it.
106 * - Variable sizing of the per node arrays
109 /* Enable to test recovery from slab corruption on boot */
110 #undef SLUB_RESILIENCY_TEST
112 #if PAGE_SHIFT <= 12
115 * Small page size. Make sure that we do not fragment memory
117 #define DEFAULT_MAX_ORDER 1
118 #define DEFAULT_MIN_OBJECTS 4
120 #else
123 * Large page machines are customarily able to handle larger
124 * page orders.
126 #define DEFAULT_MAX_ORDER 2
127 #define DEFAULT_MIN_OBJECTS 8
129 #endif
132 * Flags from the regular SLAB that SLUB does not support:
134 #define SLUB_UNIMPLEMENTED (SLAB_DEBUG_INITIAL)
136 #define DEBUG_DEFAULT_FLAGS (SLAB_DEBUG_FREE | SLAB_RED_ZONE | \
137 SLAB_POISON | SLAB_STORE_USER)
139 * Set of flags that will prevent slab merging
141 #define SLUB_NEVER_MERGE (SLAB_RED_ZONE | SLAB_POISON | SLAB_STORE_USER | \
142 SLAB_TRACE | SLAB_DESTROY_BY_RCU)
144 #define SLUB_MERGE_SAME (SLAB_DEBUG_FREE | SLAB_RECLAIM_ACCOUNT | \
145 SLAB_CACHE_DMA)
147 #ifndef ARCH_KMALLOC_MINALIGN
148 #define ARCH_KMALLOC_MINALIGN __alignof__(unsigned long long)
149 #endif
151 #ifndef ARCH_SLAB_MINALIGN
152 #define ARCH_SLAB_MINALIGN __alignof__(unsigned long long)
153 #endif
155 /* Internal SLUB flags */
156 #define __OBJECT_POISON 0x80000000 /* Poison object */
158 static int kmem_size = sizeof(struct kmem_cache);
160 #ifdef CONFIG_SMP
161 static struct notifier_block slab_notifier;
162 #endif
164 static enum {
165 DOWN, /* No slab functionality available */
166 PARTIAL, /* kmem_cache_open() works but kmalloc does not */
167 UP, /* Everything works */
168 SYSFS /* Sysfs up */
169 } slab_state = DOWN;
171 /* A list of all slab caches on the system */
172 static DECLARE_RWSEM(slub_lock);
173 LIST_HEAD(slab_caches);
175 #ifdef CONFIG_SYSFS
176 static int sysfs_slab_add(struct kmem_cache *);
177 static int sysfs_slab_alias(struct kmem_cache *, const char *);
178 static void sysfs_slab_remove(struct kmem_cache *);
179 #else
180 static int sysfs_slab_add(struct kmem_cache *s) { return 0; }
181 static int sysfs_slab_alias(struct kmem_cache *s, const char *p) { return 0; }
182 static void sysfs_slab_remove(struct kmem_cache *s) {}
183 #endif
185 /********************************************************************
186 * Core slab cache functions
187 *******************************************************************/
189 int slab_is_available(void)
191 return slab_state >= UP;
194 static inline struct kmem_cache_node *get_node(struct kmem_cache *s, int node)
196 #ifdef CONFIG_NUMA
197 return s->node[node];
198 #else
199 return &s->local_node;
200 #endif
204 * Object debugging
206 static void print_section(char *text, u8 *addr, unsigned int length)
208 int i, offset;
209 int newline = 1;
210 char ascii[17];
212 ascii[16] = 0;
214 for (i = 0; i < length; i++) {
215 if (newline) {
216 printk(KERN_ERR "%10s 0x%p: ", text, addr + i);
217 newline = 0;
219 printk(" %02x", addr[i]);
220 offset = i % 16;
221 ascii[offset] = isgraph(addr[i]) ? addr[i] : '.';
222 if (offset == 15) {
223 printk(" %s\n",ascii);
224 newline = 1;
227 if (!newline) {
228 i %= 16;
229 while (i < 16) {
230 printk(" ");
231 ascii[i] = ' ';
232 i++;
234 printk(" %s\n", ascii);
239 * Slow version of get and set free pointer.
241 * This requires touching the cache lines of kmem_cache.
242 * The offset can also be obtained from the page. In that
243 * case it is in the cacheline that we already need to touch.
245 static void *get_freepointer(struct kmem_cache *s, void *object)
247 return *(void **)(object + s->offset);
250 static void set_freepointer(struct kmem_cache *s, void *object, void *fp)
252 *(void **)(object + s->offset) = fp;
256 * Tracking user of a slab.
258 struct track {
259 void *addr; /* Called from address */
260 int cpu; /* Was running on cpu */
261 int pid; /* Pid context */
262 unsigned long when; /* When did the operation occur */
265 enum track_item { TRACK_ALLOC, TRACK_FREE };
267 static struct track *get_track(struct kmem_cache *s, void *object,
268 enum track_item alloc)
270 struct track *p;
272 if (s->offset)
273 p = object + s->offset + sizeof(void *);
274 else
275 p = object + s->inuse;
277 return p + alloc;
280 static void set_track(struct kmem_cache *s, void *object,
281 enum track_item alloc, void *addr)
283 struct track *p;
285 if (s->offset)
286 p = object + s->offset + sizeof(void *);
287 else
288 p = object + s->inuse;
290 p += alloc;
291 if (addr) {
292 p->addr = addr;
293 p->cpu = smp_processor_id();
294 p->pid = current ? current->pid : -1;
295 p->when = jiffies;
296 } else
297 memset(p, 0, sizeof(struct track));
300 static void init_tracking(struct kmem_cache *s, void *object)
302 if (s->flags & SLAB_STORE_USER) {
303 set_track(s, object, TRACK_FREE, NULL);
304 set_track(s, object, TRACK_ALLOC, NULL);
308 static void print_track(const char *s, struct track *t)
310 if (!t->addr)
311 return;
313 printk(KERN_ERR "%s: ", s);
314 __print_symbol("%s", (unsigned long)t->addr);
315 printk(" jiffies_ago=%lu cpu=%u pid=%d\n", jiffies - t->when, t->cpu, t->pid);
318 static void print_trailer(struct kmem_cache *s, u8 *p)
320 unsigned int off; /* Offset of last byte */
322 if (s->flags & SLAB_RED_ZONE)
323 print_section("Redzone", p + s->objsize,
324 s->inuse - s->objsize);
326 printk(KERN_ERR "FreePointer 0x%p -> 0x%p\n",
327 p + s->offset,
328 get_freepointer(s, p));
330 if (s->offset)
331 off = s->offset + sizeof(void *);
332 else
333 off = s->inuse;
335 if (s->flags & SLAB_STORE_USER) {
336 print_track("Last alloc", get_track(s, p, TRACK_ALLOC));
337 print_track("Last free ", get_track(s, p, TRACK_FREE));
338 off += 2 * sizeof(struct track);
341 if (off != s->size)
342 /* Beginning of the filler is the free pointer */
343 print_section("Filler", p + off, s->size - off);
346 static void object_err(struct kmem_cache *s, struct page *page,
347 u8 *object, char *reason)
349 u8 *addr = page_address(page);
351 printk(KERN_ERR "*** SLUB %s: %s@0x%p slab 0x%p\n",
352 s->name, reason, object, page);
353 printk(KERN_ERR " offset=%tu flags=0x%04lx inuse=%u freelist=0x%p\n",
354 object - addr, page->flags, page->inuse, page->freelist);
355 if (object > addr + 16)
356 print_section("Bytes b4", object - 16, 16);
357 print_section("Object", object, min(s->objsize, 128));
358 print_trailer(s, object);
359 dump_stack();
362 static void slab_err(struct kmem_cache *s, struct page *page, char *reason, ...)
364 va_list args;
365 char buf[100];
367 va_start(args, reason);
368 vsnprintf(buf, sizeof(buf), reason, args);
369 va_end(args);
370 printk(KERN_ERR "*** SLUB %s: %s in slab @0x%p\n", s->name, buf,
371 page);
372 dump_stack();
375 static void init_object(struct kmem_cache *s, void *object, int active)
377 u8 *p = object;
379 if (s->flags & __OBJECT_POISON) {
380 memset(p, POISON_FREE, s->objsize - 1);
381 p[s->objsize -1] = POISON_END;
384 if (s->flags & SLAB_RED_ZONE)
385 memset(p + s->objsize,
386 active ? SLUB_RED_ACTIVE : SLUB_RED_INACTIVE,
387 s->inuse - s->objsize);
390 static int check_bytes(u8 *start, unsigned int value, unsigned int bytes)
392 while (bytes) {
393 if (*start != (u8)value)
394 return 0;
395 start++;
396 bytes--;
398 return 1;
402 static int check_valid_pointer(struct kmem_cache *s, struct page *page,
403 void *object)
405 void *base;
407 if (!object)
408 return 1;
410 base = page_address(page);
411 if (object < base || object >= base + s->objects * s->size ||
412 (object - base) % s->size) {
413 return 0;
416 return 1;
420 * Object layout:
422 * object address
423 * Bytes of the object to be managed.
424 * If the freepointer may overlay the object then the free
425 * pointer is the first word of the object.
426 * Poisoning uses 0x6b (POISON_FREE) and the last byte is
427 * 0xa5 (POISON_END)
429 * object + s->objsize
430 * Padding to reach word boundary. This is also used for Redzoning.
431 * Padding is extended to word size if Redzoning is enabled
432 * and objsize == inuse.
433 * We fill with 0xbb (RED_INACTIVE) for inactive objects and with
434 * 0xcc (RED_ACTIVE) for objects in use.
436 * object + s->inuse
437 * A. Free pointer (if we cannot overwrite object on free)
438 * B. Tracking data for SLAB_STORE_USER
439 * C. Padding to reach required alignment boundary
440 * Padding is done using 0x5a (POISON_INUSE)
442 * object + s->size
444 * If slabcaches are merged then the objsize and inuse boundaries are to
445 * be ignored. And therefore no slab options that rely on these boundaries
446 * may be used with merged slabcaches.
449 static void restore_bytes(struct kmem_cache *s, char *message, u8 data,
450 void *from, void *to)
452 printk(KERN_ERR "@@@ SLUB: %s Restoring %s (0x%x) from 0x%p-0x%p\n",
453 s->name, message, data, from, to - 1);
454 memset(from, data, to - from);
457 static int check_pad_bytes(struct kmem_cache *s, struct page *page, u8 *p)
459 unsigned long off = s->inuse; /* The end of info */
461 if (s->offset)
462 /* Freepointer is placed after the object. */
463 off += sizeof(void *);
465 if (s->flags & SLAB_STORE_USER)
466 /* We also have user information there */
467 off += 2 * sizeof(struct track);
469 if (s->size == off)
470 return 1;
472 if (check_bytes(p + off, POISON_INUSE, s->size - off))
473 return 1;
475 object_err(s, page, p, "Object padding check fails");
478 * Restore padding
480 restore_bytes(s, "object padding", POISON_INUSE, p + off, p + s->size);
481 return 0;
484 static int slab_pad_check(struct kmem_cache *s, struct page *page)
486 u8 *p;
487 int length, remainder;
489 if (!(s->flags & SLAB_POISON))
490 return 1;
492 p = page_address(page);
493 length = s->objects * s->size;
494 remainder = (PAGE_SIZE << s->order) - length;
495 if (!remainder)
496 return 1;
498 if (!check_bytes(p + length, POISON_INUSE, remainder)) {
499 printk(KERN_ERR "SLUB: %s slab 0x%p: Padding fails check\n",
500 s->name, p);
501 dump_stack();
502 restore_bytes(s, "slab padding", POISON_INUSE, p + length,
503 p + length + remainder);
504 return 0;
506 return 1;
509 static int check_object(struct kmem_cache *s, struct page *page,
510 void *object, int active)
512 u8 *p = object;
513 u8 *endobject = object + s->objsize;
515 if (s->flags & SLAB_RED_ZONE) {
516 unsigned int red =
517 active ? SLUB_RED_ACTIVE : SLUB_RED_INACTIVE;
519 if (!check_bytes(endobject, red, s->inuse - s->objsize)) {
520 object_err(s, page, object,
521 active ? "Redzone Active" : "Redzone Inactive");
522 restore_bytes(s, "redzone", red,
523 endobject, object + s->inuse);
524 return 0;
526 } else {
527 if ((s->flags & SLAB_POISON) && s->objsize < s->inuse &&
528 !check_bytes(endobject, POISON_INUSE,
529 s->inuse - s->objsize)) {
530 object_err(s, page, p, "Alignment padding check fails");
532 * Fix it so that there will not be another report.
534 * Hmmm... We may be corrupting an object that now expects
535 * to be longer than allowed.
537 restore_bytes(s, "alignment padding", POISON_INUSE,
538 endobject, object + s->inuse);
542 if (s->flags & SLAB_POISON) {
543 if (!active && (s->flags & __OBJECT_POISON) &&
544 (!check_bytes(p, POISON_FREE, s->objsize - 1) ||
545 p[s->objsize - 1] != POISON_END)) {
547 object_err(s, page, p, "Poison check failed");
548 restore_bytes(s, "Poison", POISON_FREE,
549 p, p + s->objsize -1);
550 restore_bytes(s, "Poison", POISON_END,
551 p + s->objsize - 1, p + s->objsize);
552 return 0;
555 * check_pad_bytes cleans up on its own.
557 check_pad_bytes(s, page, p);
560 if (!s->offset && active)
562 * Object and freepointer overlap. Cannot check
563 * freepointer while object is allocated.
565 return 1;
567 /* Check free pointer validity */
568 if (!check_valid_pointer(s, page, get_freepointer(s, p))) {
569 object_err(s, page, p, "Freepointer corrupt");
571 * No choice but to zap it and thus loose the remainder
572 * of the free objects in this slab. May cause
573 * another error because the object count maybe
574 * wrong now.
576 set_freepointer(s, p, NULL);
577 return 0;
579 return 1;
582 static int check_slab(struct kmem_cache *s, struct page *page)
584 VM_BUG_ON(!irqs_disabled());
586 if (!PageSlab(page)) {
587 printk(KERN_ERR "SLUB: %s Not a valid slab page @0x%p "
588 "flags=%lx mapping=0x%p count=%d \n",
589 s->name, page, page->flags, page->mapping,
590 page_count(page));
591 return 0;
593 if (page->offset * sizeof(void *) != s->offset) {
594 printk(KERN_ERR "SLUB: %s Corrupted offset %lu in slab @0x%p"
595 " flags=0x%lx mapping=0x%p count=%d\n",
596 s->name,
597 (unsigned long)(page->offset * sizeof(void *)),
598 page,
599 page->flags,
600 page->mapping,
601 page_count(page));
602 dump_stack();
603 return 0;
605 if (page->inuse > s->objects) {
606 printk(KERN_ERR "SLUB: %s Inuse %u > max %u in slab "
607 "page @0x%p flags=%lx mapping=0x%p count=%d\n",
608 s->name, page->inuse, s->objects, page, page->flags,
609 page->mapping, page_count(page));
610 dump_stack();
611 return 0;
613 /* Slab_pad_check fixes things up after itself */
614 slab_pad_check(s, page);
615 return 1;
619 * Determine if a certain object on a page is on the freelist and
620 * therefore free. Must hold the slab lock for cpu slabs to
621 * guarantee that the chains are consistent.
623 static int on_freelist(struct kmem_cache *s, struct page *page, void *search)
625 int nr = 0;
626 void *fp = page->freelist;
627 void *object = NULL;
629 while (fp && nr <= s->objects) {
630 if (fp == search)
631 return 1;
632 if (!check_valid_pointer(s, page, fp)) {
633 if (object) {
634 object_err(s, page, object,
635 "Freechain corrupt");
636 set_freepointer(s, object, NULL);
637 break;
638 } else {
639 printk(KERN_ERR "SLUB: %s slab 0x%p "
640 "freepointer 0x%p corrupted.\n",
641 s->name, page, fp);
642 dump_stack();
643 page->freelist = NULL;
644 page->inuse = s->objects;
645 return 0;
647 break;
649 object = fp;
650 fp = get_freepointer(s, object);
651 nr++;
654 if (page->inuse != s->objects - nr) {
655 printk(KERN_ERR "slab %s: page 0x%p wrong object count."
656 " counter is %d but counted were %d\n",
657 s->name, page, page->inuse,
658 s->objects - nr);
659 page->inuse = s->objects - nr;
661 return search == NULL;
665 * Tracking of fully allocated slabs for debugging
667 static void add_full(struct kmem_cache *s, struct page *page)
669 struct kmem_cache_node *n;
671 VM_BUG_ON(!irqs_disabled());
673 VM_BUG_ON(!irqs_disabled());
675 if (!(s->flags & SLAB_STORE_USER))
676 return;
678 n = get_node(s, page_to_nid(page));
679 spin_lock(&n->list_lock);
680 list_add(&page->lru, &n->full);
681 spin_unlock(&n->list_lock);
684 static void remove_full(struct kmem_cache *s, struct page *page)
686 struct kmem_cache_node *n;
688 if (!(s->flags & SLAB_STORE_USER))
689 return;
691 n = get_node(s, page_to_nid(page));
693 spin_lock(&n->list_lock);
694 list_del(&page->lru);
695 spin_unlock(&n->list_lock);
698 static int alloc_object_checks(struct kmem_cache *s, struct page *page,
699 void *object)
701 if (!check_slab(s, page))
702 goto bad;
704 if (object && !on_freelist(s, page, object)) {
705 printk(KERN_ERR "SLUB: %s Object 0x%p@0x%p "
706 "already allocated.\n",
707 s->name, object, page);
708 goto dump;
711 if (!check_valid_pointer(s, page, object)) {
712 object_err(s, page, object, "Freelist Pointer check fails");
713 goto dump;
716 if (!object)
717 return 1;
719 if (!check_object(s, page, object, 0))
720 goto bad;
721 init_object(s, object, 1);
723 if (s->flags & SLAB_TRACE) {
724 printk(KERN_INFO "TRACE %s alloc 0x%p inuse=%d fp=0x%p\n",
725 s->name, object, page->inuse,
726 page->freelist);
727 dump_stack();
729 return 1;
730 dump:
731 dump_stack();
732 bad:
733 if (PageSlab(page)) {
735 * If this is a slab page then lets do the best we can
736 * to avoid issues in the future. Marking all objects
737 * as used avoids touching the remainder.
739 printk(KERN_ERR "@@@ SLUB: %s slab 0x%p. Marking all objects used.\n",
740 s->name, page);
741 page->inuse = s->objects;
742 page->freelist = NULL;
743 /* Fix up fields that may be corrupted */
744 page->offset = s->offset / sizeof(void *);
746 return 0;
749 static int free_object_checks(struct kmem_cache *s, struct page *page,
750 void *object)
752 if (!check_slab(s, page))
753 goto fail;
755 if (!check_valid_pointer(s, page, object)) {
756 printk(KERN_ERR "SLUB: %s slab 0x%p invalid "
757 "object pointer 0x%p\n",
758 s->name, page, object);
759 goto fail;
762 if (on_freelist(s, page, object)) {
763 printk(KERN_ERR "SLUB: %s slab 0x%p object "
764 "0x%p already free.\n", s->name, page, object);
765 goto fail;
768 if (!check_object(s, page, object, 1))
769 return 0;
771 if (unlikely(s != page->slab)) {
772 if (!PageSlab(page))
773 printk(KERN_ERR "slab_free %s size %d: attempt to"
774 "free object(0x%p) outside of slab.\n",
775 s->name, s->size, object);
776 else
777 if (!page->slab)
778 printk(KERN_ERR
779 "slab_free : no slab(NULL) for object 0x%p.\n",
780 object);
781 else
782 printk(KERN_ERR "slab_free %s(%d): object at 0x%p"
783 " belongs to slab %s(%d)\n",
784 s->name, s->size, object,
785 page->slab->name, page->slab->size);
786 goto fail;
788 if (s->flags & SLAB_TRACE) {
789 printk(KERN_INFO "TRACE %s free 0x%p inuse=%d fp=0x%p\n",
790 s->name, object, page->inuse,
791 page->freelist);
792 print_section("Object", object, s->objsize);
793 dump_stack();
795 init_object(s, object, 0);
796 return 1;
797 fail:
798 dump_stack();
799 printk(KERN_ERR "@@@ SLUB: %s slab 0x%p object at 0x%p not freed.\n",
800 s->name, page, object);
801 return 0;
805 * Slab allocation and freeing
807 static struct page *allocate_slab(struct kmem_cache *s, gfp_t flags, int node)
809 struct page * page;
810 int pages = 1 << s->order;
812 if (s->order)
813 flags |= __GFP_COMP;
815 if (s->flags & SLAB_CACHE_DMA)
816 flags |= SLUB_DMA;
818 if (node == -1)
819 page = alloc_pages(flags, s->order);
820 else
821 page = alloc_pages_node(node, flags, s->order);
823 if (!page)
824 return NULL;
826 mod_zone_page_state(page_zone(page),
827 (s->flags & SLAB_RECLAIM_ACCOUNT) ?
828 NR_SLAB_RECLAIMABLE : NR_SLAB_UNRECLAIMABLE,
829 pages);
831 return page;
834 static void setup_object(struct kmem_cache *s, struct page *page,
835 void *object)
837 if (PageError(page)) {
838 init_object(s, object, 0);
839 init_tracking(s, object);
842 if (unlikely(s->ctor)) {
843 int mode = SLAB_CTOR_CONSTRUCTOR;
845 if (!(s->flags & __GFP_WAIT))
846 mode |= SLAB_CTOR_ATOMIC;
848 s->ctor(object, s, mode);
852 static struct page *new_slab(struct kmem_cache *s, gfp_t flags, int node)
854 struct page *page;
855 struct kmem_cache_node *n;
856 void *start;
857 void *end;
858 void *last;
859 void *p;
861 if (flags & __GFP_NO_GROW)
862 return NULL;
864 BUG_ON(flags & ~(GFP_DMA | GFP_LEVEL_MASK));
866 if (flags & __GFP_WAIT)
867 local_irq_enable();
869 page = allocate_slab(s, flags & GFP_LEVEL_MASK, node);
870 if (!page)
871 goto out;
873 n = get_node(s, page_to_nid(page));
874 if (n)
875 atomic_long_inc(&n->nr_slabs);
876 page->offset = s->offset / sizeof(void *);
877 page->slab = s;
878 page->flags |= 1 << PG_slab;
879 if (s->flags & (SLAB_DEBUG_FREE | SLAB_RED_ZONE | SLAB_POISON |
880 SLAB_STORE_USER | SLAB_TRACE))
881 page->flags |= 1 << PG_error;
883 start = page_address(page);
884 end = start + s->objects * s->size;
886 if (unlikely(s->flags & SLAB_POISON))
887 memset(start, POISON_INUSE, PAGE_SIZE << s->order);
889 last = start;
890 for (p = start + s->size; p < end; p += s->size) {
891 setup_object(s, page, last);
892 set_freepointer(s, last, p);
893 last = p;
895 setup_object(s, page, last);
896 set_freepointer(s, last, NULL);
898 page->freelist = start;
899 page->inuse = 0;
900 out:
901 if (flags & __GFP_WAIT)
902 local_irq_disable();
903 return page;
906 static void __free_slab(struct kmem_cache *s, struct page *page)
908 int pages = 1 << s->order;
910 if (unlikely(PageError(page) || s->dtor)) {
911 void *start = page_address(page);
912 void *end = start + (pages << PAGE_SHIFT);
913 void *p;
915 slab_pad_check(s, page);
916 for (p = start; p <= end - s->size; p += s->size) {
917 if (s->dtor)
918 s->dtor(p, s, 0);
919 check_object(s, page, p, 0);
923 mod_zone_page_state(page_zone(page),
924 (s->flags & SLAB_RECLAIM_ACCOUNT) ?
925 NR_SLAB_RECLAIMABLE : NR_SLAB_UNRECLAIMABLE,
926 - pages);
928 page->mapping = NULL;
929 __free_pages(page, s->order);
932 static void rcu_free_slab(struct rcu_head *h)
934 struct page *page;
936 page = container_of((struct list_head *)h, struct page, lru);
937 __free_slab(page->slab, page);
940 static void free_slab(struct kmem_cache *s, struct page *page)
942 if (unlikely(s->flags & SLAB_DESTROY_BY_RCU)) {
944 * RCU free overloads the RCU head over the LRU
946 struct rcu_head *head = (void *)&page->lru;
948 call_rcu(head, rcu_free_slab);
949 } else
950 __free_slab(s, page);
953 static void discard_slab(struct kmem_cache *s, struct page *page)
955 struct kmem_cache_node *n = get_node(s, page_to_nid(page));
957 atomic_long_dec(&n->nr_slabs);
958 reset_page_mapcount(page);
959 page->flags &= ~(1 << PG_slab | 1 << PG_error);
960 free_slab(s, page);
964 * Per slab locking using the pagelock
966 static __always_inline void slab_lock(struct page *page)
968 bit_spin_lock(PG_locked, &page->flags);
971 static __always_inline void slab_unlock(struct page *page)
973 bit_spin_unlock(PG_locked, &page->flags);
976 static __always_inline int slab_trylock(struct page *page)
978 int rc = 1;
980 rc = bit_spin_trylock(PG_locked, &page->flags);
981 return rc;
985 * Management of partially allocated slabs
987 static void add_partial(struct kmem_cache *s, struct page *page)
989 struct kmem_cache_node *n = get_node(s, page_to_nid(page));
991 spin_lock(&n->list_lock);
992 n->nr_partial++;
993 list_add(&page->lru, &n->partial);
994 spin_unlock(&n->list_lock);
997 static void remove_partial(struct kmem_cache *s,
998 struct page *page)
1000 struct kmem_cache_node *n = get_node(s, page_to_nid(page));
1002 spin_lock(&n->list_lock);
1003 list_del(&page->lru);
1004 n->nr_partial--;
1005 spin_unlock(&n->list_lock);
1009 * Lock page and remove it from the partial list
1011 * Must hold list_lock
1013 static int lock_and_del_slab(struct kmem_cache_node *n, struct page *page)
1015 if (slab_trylock(page)) {
1016 list_del(&page->lru);
1017 n->nr_partial--;
1018 return 1;
1020 return 0;
1024 * Try to get a partial slab from a specific node
1026 static struct page *get_partial_node(struct kmem_cache_node *n)
1028 struct page *page;
1031 * Racy check. If we mistakenly see no partial slabs then we
1032 * just allocate an empty slab. If we mistakenly try to get a
1033 * partial slab then get_partials() will return NULL.
1035 if (!n || !n->nr_partial)
1036 return NULL;
1038 spin_lock(&n->list_lock);
1039 list_for_each_entry(page, &n->partial, lru)
1040 if (lock_and_del_slab(n, page))
1041 goto out;
1042 page = NULL;
1043 out:
1044 spin_unlock(&n->list_lock);
1045 return page;
1049 * Get a page from somewhere. Search in increasing NUMA
1050 * distances.
1052 static struct page *get_any_partial(struct kmem_cache *s, gfp_t flags)
1054 #ifdef CONFIG_NUMA
1055 struct zonelist *zonelist;
1056 struct zone **z;
1057 struct page *page;
1060 * The defrag ratio allows to configure the tradeoffs between
1061 * inter node defragmentation and node local allocations.
1062 * A lower defrag_ratio increases the tendency to do local
1063 * allocations instead of scanning throught the partial
1064 * lists on other nodes.
1066 * If defrag_ratio is set to 0 then kmalloc() always
1067 * returns node local objects. If its higher then kmalloc()
1068 * may return off node objects in order to avoid fragmentation.
1070 * A higher ratio means slabs may be taken from other nodes
1071 * thus reducing the number of partial slabs on those nodes.
1073 * If /sys/slab/xx/defrag_ratio is set to 100 (which makes
1074 * defrag_ratio = 1000) then every (well almost) allocation
1075 * will first attempt to defrag slab caches on other nodes. This
1076 * means scanning over all nodes to look for partial slabs which
1077 * may be a bit expensive to do on every slab allocation.
1079 if (!s->defrag_ratio || get_cycles() % 1024 > s->defrag_ratio)
1080 return NULL;
1082 zonelist = &NODE_DATA(slab_node(current->mempolicy))
1083 ->node_zonelists[gfp_zone(flags)];
1084 for (z = zonelist->zones; *z; z++) {
1085 struct kmem_cache_node *n;
1087 n = get_node(s, zone_to_nid(*z));
1089 if (n && cpuset_zone_allowed_hardwall(*z, flags) &&
1090 n->nr_partial > 2) {
1091 page = get_partial_node(n);
1092 if (page)
1093 return page;
1096 #endif
1097 return NULL;
1101 * Get a partial page, lock it and return it.
1103 static struct page *get_partial(struct kmem_cache *s, gfp_t flags, int node)
1105 struct page *page;
1106 int searchnode = (node == -1) ? numa_node_id() : node;
1108 page = get_partial_node(get_node(s, searchnode));
1109 if (page || (flags & __GFP_THISNODE))
1110 return page;
1112 return get_any_partial(s, flags);
1116 * Move a page back to the lists.
1118 * Must be called with the slab lock held.
1120 * On exit the slab lock will have been dropped.
1122 static void putback_slab(struct kmem_cache *s, struct page *page)
1124 if (page->inuse) {
1125 if (page->freelist)
1126 add_partial(s, page);
1127 else if (PageError(page))
1128 add_full(s, page);
1129 slab_unlock(page);
1130 } else {
1131 slab_unlock(page);
1132 discard_slab(s, page);
1137 * Remove the cpu slab
1139 static void deactivate_slab(struct kmem_cache *s, struct page *page, int cpu)
1141 s->cpu_slab[cpu] = NULL;
1142 ClearPageActive(page);
1144 putback_slab(s, page);
1147 static void flush_slab(struct kmem_cache *s, struct page *page, int cpu)
1149 slab_lock(page);
1150 deactivate_slab(s, page, cpu);
1154 * Flush cpu slab.
1155 * Called from IPI handler with interrupts disabled.
1157 static void __flush_cpu_slab(struct kmem_cache *s, int cpu)
1159 struct page *page = s->cpu_slab[cpu];
1161 if (likely(page))
1162 flush_slab(s, page, cpu);
1165 static void flush_cpu_slab(void *d)
1167 struct kmem_cache *s = d;
1168 int cpu = smp_processor_id();
1170 __flush_cpu_slab(s, cpu);
1173 static void flush_all(struct kmem_cache *s)
1175 #ifdef CONFIG_SMP
1176 on_each_cpu(flush_cpu_slab, s, 1, 1);
1177 #else
1178 unsigned long flags;
1180 local_irq_save(flags);
1181 flush_cpu_slab(s);
1182 local_irq_restore(flags);
1183 #endif
1187 * slab_alloc is optimized to only modify two cachelines on the fast path
1188 * (aside from the stack):
1190 * 1. The page struct
1191 * 2. The first cacheline of the object to be allocated.
1193 * The only cache lines that are read (apart from code) is the
1194 * per cpu array in the kmem_cache struct.
1196 * Fastpath is not possible if we need to get a new slab or have
1197 * debugging enabled (which means all slabs are marked with PageError)
1199 static void *slab_alloc(struct kmem_cache *s,
1200 gfp_t gfpflags, int node, void *addr)
1202 struct page *page;
1203 void **object;
1204 unsigned long flags;
1205 int cpu;
1207 local_irq_save(flags);
1208 cpu = smp_processor_id();
1209 page = s->cpu_slab[cpu];
1210 if (!page)
1211 goto new_slab;
1213 slab_lock(page);
1214 if (unlikely(node != -1 && page_to_nid(page) != node))
1215 goto another_slab;
1216 redo:
1217 object = page->freelist;
1218 if (unlikely(!object))
1219 goto another_slab;
1220 if (unlikely(PageError(page)))
1221 goto debug;
1223 have_object:
1224 page->inuse++;
1225 page->freelist = object[page->offset];
1226 slab_unlock(page);
1227 local_irq_restore(flags);
1228 return object;
1230 another_slab:
1231 deactivate_slab(s, page, cpu);
1233 new_slab:
1234 page = get_partial(s, gfpflags, node);
1235 if (likely(page)) {
1236 have_slab:
1237 s->cpu_slab[cpu] = page;
1238 SetPageActive(page);
1239 goto redo;
1242 page = new_slab(s, gfpflags, node);
1243 if (page) {
1244 cpu = smp_processor_id();
1245 if (s->cpu_slab[cpu]) {
1247 * Someone else populated the cpu_slab while we enabled
1248 * interrupts, or we have got scheduled on another cpu.
1249 * The page may not be on the requested node.
1251 if (node == -1 ||
1252 page_to_nid(s->cpu_slab[cpu]) == node) {
1254 * Current cpuslab is acceptable and we
1255 * want the current one since its cache hot
1257 discard_slab(s, page);
1258 page = s->cpu_slab[cpu];
1259 slab_lock(page);
1260 goto redo;
1262 /* Dump the current slab */
1263 flush_slab(s, s->cpu_slab[cpu], cpu);
1265 slab_lock(page);
1266 goto have_slab;
1268 local_irq_restore(flags);
1269 return NULL;
1270 debug:
1271 if (!alloc_object_checks(s, page, object))
1272 goto another_slab;
1273 if (s->flags & SLAB_STORE_USER)
1274 set_track(s, object, TRACK_ALLOC, addr);
1275 goto have_object;
1278 void *kmem_cache_alloc(struct kmem_cache *s, gfp_t gfpflags)
1280 return slab_alloc(s, gfpflags, -1, __builtin_return_address(0));
1282 EXPORT_SYMBOL(kmem_cache_alloc);
1284 #ifdef CONFIG_NUMA
1285 void *kmem_cache_alloc_node(struct kmem_cache *s, gfp_t gfpflags, int node)
1287 return slab_alloc(s, gfpflags, node, __builtin_return_address(0));
1289 EXPORT_SYMBOL(kmem_cache_alloc_node);
1290 #endif
1293 * The fastpath only writes the cacheline of the page struct and the first
1294 * cacheline of the object.
1296 * No special cachelines need to be read
1298 static void slab_free(struct kmem_cache *s, struct page *page,
1299 void *x, void *addr)
1301 void *prior;
1302 void **object = (void *)x;
1303 unsigned long flags;
1305 local_irq_save(flags);
1306 slab_lock(page);
1308 if (unlikely(PageError(page)))
1309 goto debug;
1310 checks_ok:
1311 prior = object[page->offset] = page->freelist;
1312 page->freelist = object;
1313 page->inuse--;
1315 if (unlikely(PageActive(page)))
1317 * Cpu slabs are never on partial lists and are
1318 * never freed.
1320 goto out_unlock;
1322 if (unlikely(!page->inuse))
1323 goto slab_empty;
1326 * Objects left in the slab. If it
1327 * was not on the partial list before
1328 * then add it.
1330 if (unlikely(!prior))
1331 add_partial(s, page);
1333 out_unlock:
1334 slab_unlock(page);
1335 local_irq_restore(flags);
1336 return;
1338 slab_empty:
1339 if (prior)
1341 * Slab on the partial list.
1343 remove_partial(s, page);
1345 slab_unlock(page);
1346 discard_slab(s, page);
1347 local_irq_restore(flags);
1348 return;
1350 debug:
1351 if (!free_object_checks(s, page, x))
1352 goto out_unlock;
1353 if (!PageActive(page) && !page->freelist)
1354 remove_full(s, page);
1355 if (s->flags & SLAB_STORE_USER)
1356 set_track(s, x, TRACK_FREE, addr);
1357 goto checks_ok;
1360 void kmem_cache_free(struct kmem_cache *s, void *x)
1362 struct page *page;
1364 page = virt_to_head_page(x);
1366 slab_free(s, page, x, __builtin_return_address(0));
1368 EXPORT_SYMBOL(kmem_cache_free);
1370 /* Figure out on which slab object the object resides */
1371 static struct page *get_object_page(const void *x)
1373 struct page *page = virt_to_head_page(x);
1375 if (!PageSlab(page))
1376 return NULL;
1378 return page;
1382 * kmem_cache_open produces objects aligned at "size" and the first object
1383 * is placed at offset 0 in the slab (We have no metainformation on the
1384 * slab, all slabs are in essence "off slab").
1386 * In order to get the desired alignment one just needs to align the
1387 * size.
1389 * Notice that the allocation order determines the sizes of the per cpu
1390 * caches. Each processor has always one slab available for allocations.
1391 * Increasing the allocation order reduces the number of times that slabs
1392 * must be moved on and off the partial lists and therefore may influence
1393 * locking overhead.
1395 * The offset is used to relocate the free list link in each object. It is
1396 * therefore possible to move the free list link behind the object. This
1397 * is necessary for RCU to work properly and also useful for debugging.
1401 * Mininum / Maximum order of slab pages. This influences locking overhead
1402 * and slab fragmentation. A higher order reduces the number of partial slabs
1403 * and increases the number of allocations possible without having to
1404 * take the list_lock.
1406 static int slub_min_order;
1407 static int slub_max_order = DEFAULT_MAX_ORDER;
1410 * Minimum number of objects per slab. This is necessary in order to
1411 * reduce locking overhead. Similar to the queue size in SLAB.
1413 static int slub_min_objects = DEFAULT_MIN_OBJECTS;
1416 * Merge control. If this is set then no merging of slab caches will occur.
1418 static int slub_nomerge;
1421 * Debug settings:
1423 static int slub_debug;
1425 static char *slub_debug_slabs;
1428 * Calculate the order of allocation given an slab object size.
1430 * The order of allocation has significant impact on other elements
1431 * of the system. Generally order 0 allocations should be preferred
1432 * since they do not cause fragmentation in the page allocator. Larger
1433 * objects may have problems with order 0 because there may be too much
1434 * space left unused in a slab. We go to a higher order if more than 1/8th
1435 * of the slab would be wasted.
1437 * In order to reach satisfactory performance we must ensure that
1438 * a minimum number of objects is in one slab. Otherwise we may
1439 * generate too much activity on the partial lists. This is less a
1440 * concern for large slabs though. slub_max_order specifies the order
1441 * where we begin to stop considering the number of objects in a slab.
1443 * Higher order allocations also allow the placement of more objects
1444 * in a slab and thereby reduce object handling overhead. If the user
1445 * has requested a higher mininum order then we start with that one
1446 * instead of zero.
1448 static int calculate_order(int size)
1450 int order;
1451 int rem;
1453 for (order = max(slub_min_order, fls(size - 1) - PAGE_SHIFT);
1454 order < MAX_ORDER; order++) {
1455 unsigned long slab_size = PAGE_SIZE << order;
1457 if (slub_max_order > order &&
1458 slab_size < slub_min_objects * size)
1459 continue;
1461 if (slab_size < size)
1462 continue;
1464 rem = slab_size % size;
1466 if (rem <= (PAGE_SIZE << order) / 8)
1467 break;
1470 if (order >= MAX_ORDER)
1471 return -E2BIG;
1472 return order;
1476 * Function to figure out which alignment to use from the
1477 * various ways of specifying it.
1479 static unsigned long calculate_alignment(unsigned long flags,
1480 unsigned long align, unsigned long size)
1483 * If the user wants hardware cache aligned objects then
1484 * follow that suggestion if the object is sufficiently
1485 * large.
1487 * The hardware cache alignment cannot override the
1488 * specified alignment though. If that is greater
1489 * then use it.
1491 if ((flags & (SLAB_MUST_HWCACHE_ALIGN | SLAB_HWCACHE_ALIGN)) &&
1492 size > L1_CACHE_BYTES / 2)
1493 return max_t(unsigned long, align, L1_CACHE_BYTES);
1495 if (align < ARCH_SLAB_MINALIGN)
1496 return ARCH_SLAB_MINALIGN;
1498 return ALIGN(align, sizeof(void *));
1501 static void init_kmem_cache_node(struct kmem_cache_node *n)
1503 n->nr_partial = 0;
1504 atomic_long_set(&n->nr_slabs, 0);
1505 spin_lock_init(&n->list_lock);
1506 INIT_LIST_HEAD(&n->partial);
1507 INIT_LIST_HEAD(&n->full);
1510 #ifdef CONFIG_NUMA
1512 * No kmalloc_node yet so do it by hand. We know that this is the first
1513 * slab on the node for this slabcache. There are no concurrent accesses
1514 * possible.
1516 * Note that this function only works on the kmalloc_node_cache
1517 * when allocating for the kmalloc_node_cache.
1519 static struct kmem_cache_node * __init early_kmem_cache_node_alloc(gfp_t gfpflags,
1520 int node)
1522 struct page *page;
1523 struct kmem_cache_node *n;
1525 BUG_ON(kmalloc_caches->size < sizeof(struct kmem_cache_node));
1527 page = new_slab(kmalloc_caches, gfpflags | GFP_THISNODE, node);
1528 /* new_slab() disables interupts */
1529 local_irq_enable();
1531 BUG_ON(!page);
1532 n = page->freelist;
1533 BUG_ON(!n);
1534 page->freelist = get_freepointer(kmalloc_caches, n);
1535 page->inuse++;
1536 kmalloc_caches->node[node] = n;
1537 init_object(kmalloc_caches, n, 1);
1538 init_kmem_cache_node(n);
1539 atomic_long_inc(&n->nr_slabs);
1540 add_partial(kmalloc_caches, page);
1541 return n;
1544 static void free_kmem_cache_nodes(struct kmem_cache *s)
1546 int node;
1548 for_each_online_node(node) {
1549 struct kmem_cache_node *n = s->node[node];
1550 if (n && n != &s->local_node)
1551 kmem_cache_free(kmalloc_caches, n);
1552 s->node[node] = NULL;
1556 static int init_kmem_cache_nodes(struct kmem_cache *s, gfp_t gfpflags)
1558 int node;
1559 int local_node;
1561 if (slab_state >= UP)
1562 local_node = page_to_nid(virt_to_page(s));
1563 else
1564 local_node = 0;
1566 for_each_online_node(node) {
1567 struct kmem_cache_node *n;
1569 if (local_node == node)
1570 n = &s->local_node;
1571 else {
1572 if (slab_state == DOWN) {
1573 n = early_kmem_cache_node_alloc(gfpflags,
1574 node);
1575 continue;
1577 n = kmem_cache_alloc_node(kmalloc_caches,
1578 gfpflags, node);
1580 if (!n) {
1581 free_kmem_cache_nodes(s);
1582 return 0;
1586 s->node[node] = n;
1587 init_kmem_cache_node(n);
1589 return 1;
1591 #else
1592 static void free_kmem_cache_nodes(struct kmem_cache *s)
1596 static int init_kmem_cache_nodes(struct kmem_cache *s, gfp_t gfpflags)
1598 init_kmem_cache_node(&s->local_node);
1599 return 1;
1601 #endif
1604 * calculate_sizes() determines the order and the distribution of data within
1605 * a slab object.
1607 static int calculate_sizes(struct kmem_cache *s)
1609 unsigned long flags = s->flags;
1610 unsigned long size = s->objsize;
1611 unsigned long align = s->align;
1614 * Determine if we can poison the object itself. If the user of
1615 * the slab may touch the object after free or before allocation
1616 * then we should never poison the object itself.
1618 if ((flags & SLAB_POISON) && !(flags & SLAB_DESTROY_BY_RCU) &&
1619 !s->ctor && !s->dtor)
1620 s->flags |= __OBJECT_POISON;
1621 else
1622 s->flags &= ~__OBJECT_POISON;
1625 * Round up object size to the next word boundary. We can only
1626 * place the free pointer at word boundaries and this determines
1627 * the possible location of the free pointer.
1629 size = ALIGN(size, sizeof(void *));
1632 * If we are redzoning then check if there is some space between the
1633 * end of the object and the free pointer. If not then add an
1634 * additional word, so that we can establish a redzone between
1635 * the object and the freepointer to be able to check for overwrites.
1637 if ((flags & SLAB_RED_ZONE) && size == s->objsize)
1638 size += sizeof(void *);
1641 * With that we have determined how much of the slab is in actual
1642 * use by the object. This is the potential offset to the free
1643 * pointer.
1645 s->inuse = size;
1647 if (((flags & (SLAB_DESTROY_BY_RCU | SLAB_POISON)) ||
1648 s->ctor || s->dtor)) {
1650 * Relocate free pointer after the object if it is not
1651 * permitted to overwrite the first word of the object on
1652 * kmem_cache_free.
1654 * This is the case if we do RCU, have a constructor or
1655 * destructor or are poisoning the objects.
1657 s->offset = size;
1658 size += sizeof(void *);
1661 if (flags & SLAB_STORE_USER)
1663 * Need to store information about allocs and frees after
1664 * the object.
1666 size += 2 * sizeof(struct track);
1668 if (flags & DEBUG_DEFAULT_FLAGS)
1670 * Add some empty padding so that we can catch
1671 * overwrites from earlier objects rather than let
1672 * tracking information or the free pointer be
1673 * corrupted if an user writes before the start
1674 * of the object.
1676 size += sizeof(void *);
1678 * Determine the alignment based on various parameters that the
1679 * user specified (this is unecessarily complex due to the attempt
1680 * to be compatible with SLAB. Should be cleaned up some day).
1682 align = calculate_alignment(flags, align, s->objsize);
1685 * SLUB stores one object immediately after another beginning from
1686 * offset 0. In order to align the objects we have to simply size
1687 * each object to conform to the alignment.
1689 size = ALIGN(size, align);
1690 s->size = size;
1692 s->order = calculate_order(size);
1693 if (s->order < 0)
1694 return 0;
1697 * Determine the number of objects per slab
1699 s->objects = (PAGE_SIZE << s->order) / size;
1702 * Verify that the number of objects is within permitted limits.
1703 * The page->inuse field is only 16 bit wide! So we cannot have
1704 * more than 64k objects per slab.
1706 if (!s->objects || s->objects > 65535)
1707 return 0;
1708 return 1;
1712 static int __init finish_bootstrap(void)
1714 struct list_head *h;
1715 int err;
1717 slab_state = SYSFS;
1719 list_for_each(h, &slab_caches) {
1720 struct kmem_cache *s =
1721 container_of(h, struct kmem_cache, list);
1723 err = sysfs_slab_add(s);
1724 BUG_ON(err);
1726 return 0;
1729 static int kmem_cache_open(struct kmem_cache *s, gfp_t gfpflags,
1730 const char *name, size_t size,
1731 size_t align, unsigned long flags,
1732 void (*ctor)(void *, struct kmem_cache *, unsigned long),
1733 void (*dtor)(void *, struct kmem_cache *, unsigned long))
1735 memset(s, 0, kmem_size);
1736 s->name = name;
1737 s->ctor = ctor;
1738 s->dtor = dtor;
1739 s->objsize = size;
1740 s->flags = flags;
1741 s->align = align;
1743 BUG_ON(flags & SLUB_UNIMPLEMENTED);
1746 * The page->offset field is only 16 bit wide. This is an offset
1747 * in units of words from the beginning of an object. If the slab
1748 * size is bigger then we cannot move the free pointer behind the
1749 * object anymore.
1751 * On 32 bit platforms the limit is 256k. On 64bit platforms
1752 * the limit is 512k.
1754 * Debugging or ctor/dtors may create a need to move the free
1755 * pointer. Fail if this happens.
1757 if (s->size >= 65535 * sizeof(void *)) {
1758 BUG_ON(flags & (SLAB_RED_ZONE | SLAB_POISON |
1759 SLAB_STORE_USER | SLAB_DESTROY_BY_RCU));
1760 BUG_ON(ctor || dtor);
1762 else
1764 * Enable debugging if selected on the kernel commandline.
1766 if (slub_debug && (!slub_debug_slabs ||
1767 strncmp(slub_debug_slabs, name,
1768 strlen(slub_debug_slabs)) == 0))
1769 s->flags |= slub_debug;
1771 if (!calculate_sizes(s))
1772 goto error;
1774 s->refcount = 1;
1775 #ifdef CONFIG_NUMA
1776 s->defrag_ratio = 100;
1777 #endif
1779 if (init_kmem_cache_nodes(s, gfpflags & ~SLUB_DMA))
1780 return 1;
1781 error:
1782 if (flags & SLAB_PANIC)
1783 panic("Cannot create slab %s size=%lu realsize=%u "
1784 "order=%u offset=%u flags=%lx\n",
1785 s->name, (unsigned long)size, s->size, s->order,
1786 s->offset, flags);
1787 return 0;
1789 EXPORT_SYMBOL(kmem_cache_open);
1792 * Check if a given pointer is valid
1794 int kmem_ptr_validate(struct kmem_cache *s, const void *object)
1796 struct page * page;
1797 void *addr;
1799 page = get_object_page(object);
1801 if (!page || s != page->slab)
1802 /* No slab or wrong slab */
1803 return 0;
1805 addr = page_address(page);
1806 if (object < addr || object >= addr + s->objects * s->size)
1807 /* Out of bounds */
1808 return 0;
1810 if ((object - addr) % s->size)
1811 /* Improperly aligned */
1812 return 0;
1815 * We could also check if the object is on the slabs freelist.
1816 * But this would be too expensive and it seems that the main
1817 * purpose of kmem_ptr_valid is to check if the object belongs
1818 * to a certain slab.
1820 return 1;
1822 EXPORT_SYMBOL(kmem_ptr_validate);
1825 * Determine the size of a slab object
1827 unsigned int kmem_cache_size(struct kmem_cache *s)
1829 return s->objsize;
1831 EXPORT_SYMBOL(kmem_cache_size);
1833 const char *kmem_cache_name(struct kmem_cache *s)
1835 return s->name;
1837 EXPORT_SYMBOL(kmem_cache_name);
1840 * Attempt to free all slabs on a node
1842 static int free_list(struct kmem_cache *s, struct kmem_cache_node *n,
1843 struct list_head *list)
1845 int slabs_inuse = 0;
1846 unsigned long flags;
1847 struct page *page, *h;
1849 spin_lock_irqsave(&n->list_lock, flags);
1850 list_for_each_entry_safe(page, h, list, lru)
1851 if (!page->inuse) {
1852 list_del(&page->lru);
1853 discard_slab(s, page);
1854 } else
1855 slabs_inuse++;
1856 spin_unlock_irqrestore(&n->list_lock, flags);
1857 return slabs_inuse;
1861 * Release all resources used by slab cache
1863 static int kmem_cache_close(struct kmem_cache *s)
1865 int node;
1867 flush_all(s);
1869 /* Attempt to free all objects */
1870 for_each_online_node(node) {
1871 struct kmem_cache_node *n = get_node(s, node);
1873 free_list(s, n, &n->partial);
1874 if (atomic_long_read(&n->nr_slabs))
1875 return 1;
1877 free_kmem_cache_nodes(s);
1878 return 0;
1882 * Close a cache and release the kmem_cache structure
1883 * (must be used for caches created using kmem_cache_create)
1885 void kmem_cache_destroy(struct kmem_cache *s)
1887 down_write(&slub_lock);
1888 s->refcount--;
1889 if (!s->refcount) {
1890 list_del(&s->list);
1891 if (kmem_cache_close(s))
1892 WARN_ON(1);
1893 sysfs_slab_remove(s);
1894 kfree(s);
1896 up_write(&slub_lock);
1898 EXPORT_SYMBOL(kmem_cache_destroy);
1900 /********************************************************************
1901 * Kmalloc subsystem
1902 *******************************************************************/
1904 struct kmem_cache kmalloc_caches[KMALLOC_SHIFT_HIGH + 1] __cacheline_aligned;
1905 EXPORT_SYMBOL(kmalloc_caches);
1907 #ifdef CONFIG_ZONE_DMA
1908 static struct kmem_cache *kmalloc_caches_dma[KMALLOC_SHIFT_HIGH + 1];
1909 #endif
1911 static int __init setup_slub_min_order(char *str)
1913 get_option (&str, &slub_min_order);
1915 return 1;
1918 __setup("slub_min_order=", setup_slub_min_order);
1920 static int __init setup_slub_max_order(char *str)
1922 get_option (&str, &slub_max_order);
1924 return 1;
1927 __setup("slub_max_order=", setup_slub_max_order);
1929 static int __init setup_slub_min_objects(char *str)
1931 get_option (&str, &slub_min_objects);
1933 return 1;
1936 __setup("slub_min_objects=", setup_slub_min_objects);
1938 static int __init setup_slub_nomerge(char *str)
1940 slub_nomerge = 1;
1941 return 1;
1944 __setup("slub_nomerge", setup_slub_nomerge);
1946 static int __init setup_slub_debug(char *str)
1948 if (!str || *str != '=')
1949 slub_debug = DEBUG_DEFAULT_FLAGS;
1950 else {
1951 str++;
1952 if (*str == 0 || *str == ',')
1953 slub_debug = DEBUG_DEFAULT_FLAGS;
1954 else
1955 for( ;*str && *str != ','; str++)
1956 switch (*str) {
1957 case 'f' : case 'F' :
1958 slub_debug |= SLAB_DEBUG_FREE;
1959 break;
1960 case 'z' : case 'Z' :
1961 slub_debug |= SLAB_RED_ZONE;
1962 break;
1963 case 'p' : case 'P' :
1964 slub_debug |= SLAB_POISON;
1965 break;
1966 case 'u' : case 'U' :
1967 slub_debug |= SLAB_STORE_USER;
1968 break;
1969 case 't' : case 'T' :
1970 slub_debug |= SLAB_TRACE;
1971 break;
1972 default:
1973 printk(KERN_ERR "slub_debug option '%c' "
1974 "unknown. skipped\n",*str);
1978 if (*str == ',')
1979 slub_debug_slabs = str + 1;
1980 return 1;
1983 __setup("slub_debug", setup_slub_debug);
1985 static struct kmem_cache *create_kmalloc_cache(struct kmem_cache *s,
1986 const char *name, int size, gfp_t gfp_flags)
1988 unsigned int flags = 0;
1990 if (gfp_flags & SLUB_DMA)
1991 flags = SLAB_CACHE_DMA;
1993 down_write(&slub_lock);
1994 if (!kmem_cache_open(s, gfp_flags, name, size, ARCH_KMALLOC_MINALIGN,
1995 flags, NULL, NULL))
1996 goto panic;
1998 list_add(&s->list, &slab_caches);
1999 up_write(&slub_lock);
2000 if (sysfs_slab_add(s))
2001 goto panic;
2002 return s;
2004 panic:
2005 panic("Creation of kmalloc slab %s size=%d failed.\n", name, size);
2008 static struct kmem_cache *get_slab(size_t size, gfp_t flags)
2010 int index = kmalloc_index(size);
2012 if (!index)
2013 return NULL;
2015 /* Allocation too large? */
2016 BUG_ON(index < 0);
2018 #ifdef CONFIG_ZONE_DMA
2019 if ((flags & SLUB_DMA)) {
2020 struct kmem_cache *s;
2021 struct kmem_cache *x;
2022 char *text;
2023 size_t realsize;
2025 s = kmalloc_caches_dma[index];
2026 if (s)
2027 return s;
2029 /* Dynamically create dma cache */
2030 x = kmalloc(kmem_size, flags & ~SLUB_DMA);
2031 if (!x)
2032 panic("Unable to allocate memory for dma cache\n");
2034 if (index <= KMALLOC_SHIFT_HIGH)
2035 realsize = 1 << index;
2036 else {
2037 if (index == 1)
2038 realsize = 96;
2039 else
2040 realsize = 192;
2043 text = kasprintf(flags & ~SLUB_DMA, "kmalloc_dma-%d",
2044 (unsigned int)realsize);
2045 s = create_kmalloc_cache(x, text, realsize, flags);
2046 kmalloc_caches_dma[index] = s;
2047 return s;
2049 #endif
2050 return &kmalloc_caches[index];
2053 void *__kmalloc(size_t size, gfp_t flags)
2055 struct kmem_cache *s = get_slab(size, flags);
2057 if (s)
2058 return slab_alloc(s, flags, -1, __builtin_return_address(0));
2059 return NULL;
2061 EXPORT_SYMBOL(__kmalloc);
2063 #ifdef CONFIG_NUMA
2064 void *__kmalloc_node(size_t size, gfp_t flags, int node)
2066 struct kmem_cache *s = get_slab(size, flags);
2068 if (s)
2069 return slab_alloc(s, flags, node, __builtin_return_address(0));
2070 return NULL;
2072 EXPORT_SYMBOL(__kmalloc_node);
2073 #endif
2075 size_t ksize(const void *object)
2077 struct page *page = get_object_page(object);
2078 struct kmem_cache *s;
2080 BUG_ON(!page);
2081 s = page->slab;
2082 BUG_ON(!s);
2085 * Debugging requires use of the padding between object
2086 * and whatever may come after it.
2088 if (s->flags & (SLAB_RED_ZONE | SLAB_POISON))
2089 return s->objsize;
2092 * If we have the need to store the freelist pointer
2093 * back there or track user information then we can
2094 * only use the space before that information.
2096 if (s->flags & (SLAB_DESTROY_BY_RCU | SLAB_STORE_USER))
2097 return s->inuse;
2100 * Else we can use all the padding etc for the allocation
2102 return s->size;
2104 EXPORT_SYMBOL(ksize);
2106 void kfree(const void *x)
2108 struct kmem_cache *s;
2109 struct page *page;
2111 if (!x)
2112 return;
2114 page = virt_to_head_page(x);
2115 s = page->slab;
2117 slab_free(s, page, (void *)x, __builtin_return_address(0));
2119 EXPORT_SYMBOL(kfree);
2122 * krealloc - reallocate memory. The contents will remain unchanged.
2124 * @p: object to reallocate memory for.
2125 * @new_size: how many bytes of memory are required.
2126 * @flags: the type of memory to allocate.
2128 * The contents of the object pointed to are preserved up to the
2129 * lesser of the new and old sizes. If @p is %NULL, krealloc()
2130 * behaves exactly like kmalloc(). If @size is 0 and @p is not a
2131 * %NULL pointer, the object pointed to is freed.
2133 void *krealloc(const void *p, size_t new_size, gfp_t flags)
2135 struct kmem_cache *new_cache;
2136 void *ret;
2137 struct page *page;
2139 if (unlikely(!p))
2140 return kmalloc(new_size, flags);
2142 if (unlikely(!new_size)) {
2143 kfree(p);
2144 return NULL;
2147 page = virt_to_head_page(p);
2149 new_cache = get_slab(new_size, flags);
2152 * If new size fits in the current cache, bail out.
2154 if (likely(page->slab == new_cache))
2155 return (void *)p;
2157 ret = kmalloc(new_size, flags);
2158 if (ret) {
2159 memcpy(ret, p, min(new_size, ksize(p)));
2160 kfree(p);
2162 return ret;
2164 EXPORT_SYMBOL(krealloc);
2166 /********************************************************************
2167 * Basic setup of slabs
2168 *******************************************************************/
2170 void __init kmem_cache_init(void)
2172 int i;
2174 #ifdef CONFIG_NUMA
2176 * Must first have the slab cache available for the allocations of the
2177 * struct kmalloc_cache_node's. There is special bootstrap code in
2178 * kmem_cache_open for slab_state == DOWN.
2180 create_kmalloc_cache(&kmalloc_caches[0], "kmem_cache_node",
2181 sizeof(struct kmem_cache_node), GFP_KERNEL);
2182 #endif
2184 /* Able to allocate the per node structures */
2185 slab_state = PARTIAL;
2187 /* Caches that are not of the two-to-the-power-of size */
2188 create_kmalloc_cache(&kmalloc_caches[1],
2189 "kmalloc-96", 96, GFP_KERNEL);
2190 create_kmalloc_cache(&kmalloc_caches[2],
2191 "kmalloc-192", 192, GFP_KERNEL);
2193 for (i = KMALLOC_SHIFT_LOW; i <= KMALLOC_SHIFT_HIGH; i++)
2194 create_kmalloc_cache(&kmalloc_caches[i],
2195 "kmalloc", 1 << i, GFP_KERNEL);
2197 slab_state = UP;
2199 /* Provide the correct kmalloc names now that the caches are up */
2200 for (i = KMALLOC_SHIFT_LOW; i <= KMALLOC_SHIFT_HIGH; i++)
2201 kmalloc_caches[i]. name =
2202 kasprintf(GFP_KERNEL, "kmalloc-%d", 1 << i);
2204 #ifdef CONFIG_SMP
2205 register_cpu_notifier(&slab_notifier);
2206 #endif
2208 if (nr_cpu_ids) /* Remove when nr_cpu_ids is fixed upstream ! */
2209 kmem_size = offsetof(struct kmem_cache, cpu_slab)
2210 + nr_cpu_ids * sizeof(struct page *);
2212 printk(KERN_INFO "SLUB: Genslabs=%d, HWalign=%d, Order=%d-%d, MinObjects=%d,"
2213 " Processors=%d, Nodes=%d\n",
2214 KMALLOC_SHIFT_HIGH, L1_CACHE_BYTES,
2215 slub_min_order, slub_max_order, slub_min_objects,
2216 nr_cpu_ids, nr_node_ids);
2220 * Find a mergeable slab cache
2222 static int slab_unmergeable(struct kmem_cache *s)
2224 if (slub_nomerge || (s->flags & SLUB_NEVER_MERGE))
2225 return 1;
2227 if (s->ctor || s->dtor)
2228 return 1;
2230 return 0;
2233 static struct kmem_cache *find_mergeable(size_t size,
2234 size_t align, unsigned long flags,
2235 void (*ctor)(void *, struct kmem_cache *, unsigned long),
2236 void (*dtor)(void *, struct kmem_cache *, unsigned long))
2238 struct list_head *h;
2240 if (slub_nomerge || (flags & SLUB_NEVER_MERGE))
2241 return NULL;
2243 if (ctor || dtor)
2244 return NULL;
2246 size = ALIGN(size, sizeof(void *));
2247 align = calculate_alignment(flags, align, size);
2248 size = ALIGN(size, align);
2250 list_for_each(h, &slab_caches) {
2251 struct kmem_cache *s =
2252 container_of(h, struct kmem_cache, list);
2254 if (slab_unmergeable(s))
2255 continue;
2257 if (size > s->size)
2258 continue;
2260 if (((flags | slub_debug) & SLUB_MERGE_SAME) !=
2261 (s->flags & SLUB_MERGE_SAME))
2262 continue;
2264 * Check if alignment is compatible.
2265 * Courtesy of Adrian Drzewiecki
2267 if ((s->size & ~(align -1)) != s->size)
2268 continue;
2270 if (s->size - size >= sizeof(void *))
2271 continue;
2273 return s;
2275 return NULL;
2278 struct kmem_cache *kmem_cache_create(const char *name, size_t size,
2279 size_t align, unsigned long flags,
2280 void (*ctor)(void *, struct kmem_cache *, unsigned long),
2281 void (*dtor)(void *, struct kmem_cache *, unsigned long))
2283 struct kmem_cache *s;
2285 down_write(&slub_lock);
2286 s = find_mergeable(size, align, flags, dtor, ctor);
2287 if (s) {
2288 s->refcount++;
2290 * Adjust the object sizes so that we clear
2291 * the complete object on kzalloc.
2293 s->objsize = max(s->objsize, (int)size);
2294 s->inuse = max_t(int, s->inuse, ALIGN(size, sizeof(void *)));
2295 if (sysfs_slab_alias(s, name))
2296 goto err;
2297 } else {
2298 s = kmalloc(kmem_size, GFP_KERNEL);
2299 if (s && kmem_cache_open(s, GFP_KERNEL, name,
2300 size, align, flags, ctor, dtor)) {
2301 if (sysfs_slab_add(s)) {
2302 kfree(s);
2303 goto err;
2305 list_add(&s->list, &slab_caches);
2306 } else
2307 kfree(s);
2309 up_write(&slub_lock);
2310 return s;
2312 err:
2313 up_write(&slub_lock);
2314 if (flags & SLAB_PANIC)
2315 panic("Cannot create slabcache %s\n", name);
2316 else
2317 s = NULL;
2318 return s;
2320 EXPORT_SYMBOL(kmem_cache_create);
2322 void *kmem_cache_zalloc(struct kmem_cache *s, gfp_t flags)
2324 void *x;
2326 x = slab_alloc(s, flags, -1, __builtin_return_address(0));
2327 if (x)
2328 memset(x, 0, s->objsize);
2329 return x;
2331 EXPORT_SYMBOL(kmem_cache_zalloc);
2333 #ifdef CONFIG_SMP
2334 static void for_all_slabs(void (*func)(struct kmem_cache *, int), int cpu)
2336 struct list_head *h;
2338 down_read(&slub_lock);
2339 list_for_each(h, &slab_caches) {
2340 struct kmem_cache *s =
2341 container_of(h, struct kmem_cache, list);
2343 func(s, cpu);
2345 up_read(&slub_lock);
2349 * Use the cpu notifier to insure that the slab are flushed
2350 * when necessary.
2352 static int __cpuinit slab_cpuup_callback(struct notifier_block *nfb,
2353 unsigned long action, void *hcpu)
2355 long cpu = (long)hcpu;
2357 switch (action) {
2358 case CPU_UP_CANCELED:
2359 case CPU_DEAD:
2360 for_all_slabs(__flush_cpu_slab, cpu);
2361 break;
2362 default:
2363 break;
2365 return NOTIFY_OK;
2368 static struct notifier_block __cpuinitdata slab_notifier =
2369 { &slab_cpuup_callback, NULL, 0 };
2371 #endif
2373 /***************************************************************
2374 * Compatiblility definitions
2375 **************************************************************/
2377 int kmem_cache_shrink(struct kmem_cache *s)
2379 flush_all(s);
2380 return 0;
2382 EXPORT_SYMBOL(kmem_cache_shrink);
2384 #ifdef CONFIG_NUMA
2386 /*****************************************************************
2387 * Generic reaper used to support the page allocator
2388 * (the cpu slabs are reaped by a per slab workqueue).
2390 * Maybe move this to the page allocator?
2391 ****************************************************************/
2393 static DEFINE_PER_CPU(unsigned long, reap_node);
2395 static void init_reap_node(int cpu)
2397 int node;
2399 node = next_node(cpu_to_node(cpu), node_online_map);
2400 if (node == MAX_NUMNODES)
2401 node = first_node(node_online_map);
2403 __get_cpu_var(reap_node) = node;
2406 static void next_reap_node(void)
2408 int node = __get_cpu_var(reap_node);
2411 * Also drain per cpu pages on remote zones
2413 if (node != numa_node_id())
2414 drain_node_pages(node);
2416 node = next_node(node, node_online_map);
2417 if (unlikely(node >= MAX_NUMNODES))
2418 node = first_node(node_online_map);
2419 __get_cpu_var(reap_node) = node;
2421 #else
2422 #define init_reap_node(cpu) do { } while (0)
2423 #define next_reap_node(void) do { } while (0)
2424 #endif
2426 #define REAPTIMEOUT_CPUC (2*HZ)
2428 #ifdef CONFIG_SMP
2429 static DEFINE_PER_CPU(struct delayed_work, reap_work);
2431 static void cache_reap(struct work_struct *unused)
2433 next_reap_node();
2434 refresh_cpu_vm_stats(smp_processor_id());
2435 schedule_delayed_work(&__get_cpu_var(reap_work),
2436 REAPTIMEOUT_CPUC);
2439 static void __devinit start_cpu_timer(int cpu)
2441 struct delayed_work *reap_work = &per_cpu(reap_work, cpu);
2444 * When this gets called from do_initcalls via cpucache_init(),
2445 * init_workqueues() has already run, so keventd will be setup
2446 * at that time.
2448 if (keventd_up() && reap_work->work.func == NULL) {
2449 init_reap_node(cpu);
2450 INIT_DELAYED_WORK(reap_work, cache_reap);
2451 schedule_delayed_work_on(cpu, reap_work, HZ + 3 * cpu);
2455 static int __init cpucache_init(void)
2457 int cpu;
2460 * Register the timers that drain pcp pages and update vm statistics
2462 for_each_online_cpu(cpu)
2463 start_cpu_timer(cpu);
2464 return 0;
2466 __initcall(cpucache_init);
2467 #endif
2469 #ifdef SLUB_RESILIENCY_TEST
2470 static unsigned long validate_slab_cache(struct kmem_cache *s);
2472 static void resiliency_test(void)
2474 u8 *p;
2476 printk(KERN_ERR "SLUB resiliency testing\n");
2477 printk(KERN_ERR "-----------------------\n");
2478 printk(KERN_ERR "A. Corruption after allocation\n");
2480 p = kzalloc(16, GFP_KERNEL);
2481 p[16] = 0x12;
2482 printk(KERN_ERR "\n1. kmalloc-16: Clobber Redzone/next pointer"
2483 " 0x12->0x%p\n\n", p + 16);
2485 validate_slab_cache(kmalloc_caches + 4);
2487 /* Hmmm... The next two are dangerous */
2488 p = kzalloc(32, GFP_KERNEL);
2489 p[32 + sizeof(void *)] = 0x34;
2490 printk(KERN_ERR "\n2. kmalloc-32: Clobber next pointer/next slab"
2491 " 0x34 -> -0x%p\n", p);
2492 printk(KERN_ERR "If allocated object is overwritten then not detectable\n\n");
2494 validate_slab_cache(kmalloc_caches + 5);
2495 p = kzalloc(64, GFP_KERNEL);
2496 p += 64 + (get_cycles() & 0xff) * sizeof(void *);
2497 *p = 0x56;
2498 printk(KERN_ERR "\n3. kmalloc-64: corrupting random byte 0x56->0x%p\n",
2500 printk(KERN_ERR "If allocated object is overwritten then not detectable\n\n");
2501 validate_slab_cache(kmalloc_caches + 6);
2503 printk(KERN_ERR "\nB. Corruption after free\n");
2504 p = kzalloc(128, GFP_KERNEL);
2505 kfree(p);
2506 *p = 0x78;
2507 printk(KERN_ERR "1. kmalloc-128: Clobber first word 0x78->0x%p\n\n", p);
2508 validate_slab_cache(kmalloc_caches + 7);
2510 p = kzalloc(256, GFP_KERNEL);
2511 kfree(p);
2512 p[50] = 0x9a;
2513 printk(KERN_ERR "\n2. kmalloc-256: Clobber 50th byte 0x9a->0x%p\n\n", p);
2514 validate_slab_cache(kmalloc_caches + 8);
2516 p = kzalloc(512, GFP_KERNEL);
2517 kfree(p);
2518 p[512] = 0xab;
2519 printk(KERN_ERR "\n3. kmalloc-512: Clobber redzone 0xab->0x%p\n\n", p);
2520 validate_slab_cache(kmalloc_caches + 9);
2522 #else
2523 static void resiliency_test(void) {};
2524 #endif
2527 * These are not as efficient as kmalloc for the non debug case.
2528 * We do not have the page struct available so we have to touch one
2529 * cacheline in struct kmem_cache to check slab flags.
2531 void *__kmalloc_track_caller(size_t size, gfp_t gfpflags, void *caller)
2533 struct kmem_cache *s = get_slab(size, gfpflags);
2535 if (!s)
2536 return NULL;
2538 return slab_alloc(s, gfpflags, -1, caller);
2541 void *__kmalloc_node_track_caller(size_t size, gfp_t gfpflags,
2542 int node, void *caller)
2544 struct kmem_cache *s = get_slab(size, gfpflags);
2546 if (!s)
2547 return NULL;
2549 return slab_alloc(s, gfpflags, node, caller);
2552 #ifdef CONFIG_SYSFS
2554 static unsigned long count_partial(struct kmem_cache_node *n)
2556 unsigned long flags;
2557 unsigned long x = 0;
2558 struct page *page;
2560 spin_lock_irqsave(&n->list_lock, flags);
2561 list_for_each_entry(page, &n->partial, lru)
2562 x += page->inuse;
2563 spin_unlock_irqrestore(&n->list_lock, flags);
2564 return x;
2567 enum slab_stat_type {
2568 SL_FULL,
2569 SL_PARTIAL,
2570 SL_CPU,
2571 SL_OBJECTS
2574 #define SO_FULL (1 << SL_FULL)
2575 #define SO_PARTIAL (1 << SL_PARTIAL)
2576 #define SO_CPU (1 << SL_CPU)
2577 #define SO_OBJECTS (1 << SL_OBJECTS)
2579 static unsigned long slab_objects(struct kmem_cache *s,
2580 char *buf, unsigned long flags)
2582 unsigned long total = 0;
2583 int cpu;
2584 int node;
2585 int x;
2586 unsigned long *nodes;
2587 unsigned long *per_cpu;
2589 nodes = kzalloc(2 * sizeof(unsigned long) * nr_node_ids, GFP_KERNEL);
2590 per_cpu = nodes + nr_node_ids;
2592 for_each_possible_cpu(cpu) {
2593 struct page *page = s->cpu_slab[cpu];
2594 int node;
2596 if (page) {
2597 node = page_to_nid(page);
2598 if (flags & SO_CPU) {
2599 int x = 0;
2601 if (flags & SO_OBJECTS)
2602 x = page->inuse;
2603 else
2604 x = 1;
2605 total += x;
2606 nodes[node] += x;
2608 per_cpu[node]++;
2612 for_each_online_node(node) {
2613 struct kmem_cache_node *n = get_node(s, node);
2615 if (flags & SO_PARTIAL) {
2616 if (flags & SO_OBJECTS)
2617 x = count_partial(n);
2618 else
2619 x = n->nr_partial;
2620 total += x;
2621 nodes[node] += x;
2624 if (flags & SO_FULL) {
2625 int full_slabs = atomic_read(&n->nr_slabs)
2626 - per_cpu[node]
2627 - n->nr_partial;
2629 if (flags & SO_OBJECTS)
2630 x = full_slabs * s->objects;
2631 else
2632 x = full_slabs;
2633 total += x;
2634 nodes[node] += x;
2638 x = sprintf(buf, "%lu", total);
2639 #ifdef CONFIG_NUMA
2640 for_each_online_node(node)
2641 if (nodes[node])
2642 x += sprintf(buf + x, " N%d=%lu",
2643 node, nodes[node]);
2644 #endif
2645 kfree(nodes);
2646 return x + sprintf(buf + x, "\n");
2649 static int any_slab_objects(struct kmem_cache *s)
2651 int node;
2652 int cpu;
2654 for_each_possible_cpu(cpu)
2655 if (s->cpu_slab[cpu])
2656 return 1;
2658 for_each_node(node) {
2659 struct kmem_cache_node *n = get_node(s, node);
2661 if (n->nr_partial || atomic_read(&n->nr_slabs))
2662 return 1;
2664 return 0;
2667 #define to_slab_attr(n) container_of(n, struct slab_attribute, attr)
2668 #define to_slab(n) container_of(n, struct kmem_cache, kobj);
2670 struct slab_attribute {
2671 struct attribute attr;
2672 ssize_t (*show)(struct kmem_cache *s, char *buf);
2673 ssize_t (*store)(struct kmem_cache *s, const char *x, size_t count);
2676 #define SLAB_ATTR_RO(_name) \
2677 static struct slab_attribute _name##_attr = __ATTR_RO(_name)
2679 #define SLAB_ATTR(_name) \
2680 static struct slab_attribute _name##_attr = \
2681 __ATTR(_name, 0644, _name##_show, _name##_store)
2684 static ssize_t slab_size_show(struct kmem_cache *s, char *buf)
2686 return sprintf(buf, "%d\n", s->size);
2688 SLAB_ATTR_RO(slab_size);
2690 static ssize_t align_show(struct kmem_cache *s, char *buf)
2692 return sprintf(buf, "%d\n", s->align);
2694 SLAB_ATTR_RO(align);
2696 static ssize_t object_size_show(struct kmem_cache *s, char *buf)
2698 return sprintf(buf, "%d\n", s->objsize);
2700 SLAB_ATTR_RO(object_size);
2702 static ssize_t objs_per_slab_show(struct kmem_cache *s, char *buf)
2704 return sprintf(buf, "%d\n", s->objects);
2706 SLAB_ATTR_RO(objs_per_slab);
2708 static ssize_t order_show(struct kmem_cache *s, char *buf)
2710 return sprintf(buf, "%d\n", s->order);
2712 SLAB_ATTR_RO(order);
2714 static ssize_t ctor_show(struct kmem_cache *s, char *buf)
2716 if (s->ctor) {
2717 int n = sprint_symbol(buf, (unsigned long)s->ctor);
2719 return n + sprintf(buf + n, "\n");
2721 return 0;
2723 SLAB_ATTR_RO(ctor);
2725 static ssize_t dtor_show(struct kmem_cache *s, char *buf)
2727 if (s->dtor) {
2728 int n = sprint_symbol(buf, (unsigned long)s->dtor);
2730 return n + sprintf(buf + n, "\n");
2732 return 0;
2734 SLAB_ATTR_RO(dtor);
2736 static ssize_t aliases_show(struct kmem_cache *s, char *buf)
2738 return sprintf(buf, "%d\n", s->refcount - 1);
2740 SLAB_ATTR_RO(aliases);
2742 static ssize_t slabs_show(struct kmem_cache *s, char *buf)
2744 return slab_objects(s, buf, SO_FULL|SO_PARTIAL|SO_CPU);
2746 SLAB_ATTR_RO(slabs);
2748 static ssize_t partial_show(struct kmem_cache *s, char *buf)
2750 return slab_objects(s, buf, SO_PARTIAL);
2752 SLAB_ATTR_RO(partial);
2754 static ssize_t cpu_slabs_show(struct kmem_cache *s, char *buf)
2756 return slab_objects(s, buf, SO_CPU);
2758 SLAB_ATTR_RO(cpu_slabs);
2760 static ssize_t objects_show(struct kmem_cache *s, char *buf)
2762 return slab_objects(s, buf, SO_FULL|SO_PARTIAL|SO_CPU|SO_OBJECTS);
2764 SLAB_ATTR_RO(objects);
2766 static ssize_t sanity_checks_show(struct kmem_cache *s, char *buf)
2768 return sprintf(buf, "%d\n", !!(s->flags & SLAB_DEBUG_FREE));
2771 static ssize_t sanity_checks_store(struct kmem_cache *s,
2772 const char *buf, size_t length)
2774 s->flags &= ~SLAB_DEBUG_FREE;
2775 if (buf[0] == '1')
2776 s->flags |= SLAB_DEBUG_FREE;
2777 return length;
2779 SLAB_ATTR(sanity_checks);
2781 static ssize_t trace_show(struct kmem_cache *s, char *buf)
2783 return sprintf(buf, "%d\n", !!(s->flags & SLAB_TRACE));
2786 static ssize_t trace_store(struct kmem_cache *s, const char *buf,
2787 size_t length)
2789 s->flags &= ~SLAB_TRACE;
2790 if (buf[0] == '1')
2791 s->flags |= SLAB_TRACE;
2792 return length;
2794 SLAB_ATTR(trace);
2796 static ssize_t reclaim_account_show(struct kmem_cache *s, char *buf)
2798 return sprintf(buf, "%d\n", !!(s->flags & SLAB_RECLAIM_ACCOUNT));
2801 static ssize_t reclaim_account_store(struct kmem_cache *s,
2802 const char *buf, size_t length)
2804 s->flags &= ~SLAB_RECLAIM_ACCOUNT;
2805 if (buf[0] == '1')
2806 s->flags |= SLAB_RECLAIM_ACCOUNT;
2807 return length;
2809 SLAB_ATTR(reclaim_account);
2811 static ssize_t hwcache_align_show(struct kmem_cache *s, char *buf)
2813 return sprintf(buf, "%d\n", !!(s->flags &
2814 (SLAB_HWCACHE_ALIGN|SLAB_MUST_HWCACHE_ALIGN)));
2816 SLAB_ATTR_RO(hwcache_align);
2818 #ifdef CONFIG_ZONE_DMA
2819 static ssize_t cache_dma_show(struct kmem_cache *s, char *buf)
2821 return sprintf(buf, "%d\n", !!(s->flags & SLAB_CACHE_DMA));
2823 SLAB_ATTR_RO(cache_dma);
2824 #endif
2826 static ssize_t destroy_by_rcu_show(struct kmem_cache *s, char *buf)
2828 return sprintf(buf, "%d\n", !!(s->flags & SLAB_DESTROY_BY_RCU));
2830 SLAB_ATTR_RO(destroy_by_rcu);
2832 static ssize_t red_zone_show(struct kmem_cache *s, char *buf)
2834 return sprintf(buf, "%d\n", !!(s->flags & SLAB_RED_ZONE));
2837 static ssize_t red_zone_store(struct kmem_cache *s,
2838 const char *buf, size_t length)
2840 if (any_slab_objects(s))
2841 return -EBUSY;
2843 s->flags &= ~SLAB_RED_ZONE;
2844 if (buf[0] == '1')
2845 s->flags |= SLAB_RED_ZONE;
2846 calculate_sizes(s);
2847 return length;
2849 SLAB_ATTR(red_zone);
2851 static ssize_t poison_show(struct kmem_cache *s, char *buf)
2853 return sprintf(buf, "%d\n", !!(s->flags & SLAB_POISON));
2856 static ssize_t poison_store(struct kmem_cache *s,
2857 const char *buf, size_t length)
2859 if (any_slab_objects(s))
2860 return -EBUSY;
2862 s->flags &= ~SLAB_POISON;
2863 if (buf[0] == '1')
2864 s->flags |= SLAB_POISON;
2865 calculate_sizes(s);
2866 return length;
2868 SLAB_ATTR(poison);
2870 static ssize_t store_user_show(struct kmem_cache *s, char *buf)
2872 return sprintf(buf, "%d\n", !!(s->flags & SLAB_STORE_USER));
2875 static ssize_t store_user_store(struct kmem_cache *s,
2876 const char *buf, size_t length)
2878 if (any_slab_objects(s))
2879 return -EBUSY;
2881 s->flags &= ~SLAB_STORE_USER;
2882 if (buf[0] == '1')
2883 s->flags |= SLAB_STORE_USER;
2884 calculate_sizes(s);
2885 return length;
2887 SLAB_ATTR(store_user);
2889 #ifdef CONFIG_NUMA
2890 static ssize_t defrag_ratio_show(struct kmem_cache *s, char *buf)
2892 return sprintf(buf, "%d\n", s->defrag_ratio / 10);
2895 static ssize_t defrag_ratio_store(struct kmem_cache *s,
2896 const char *buf, size_t length)
2898 int n = simple_strtoul(buf, NULL, 10);
2900 if (n < 100)
2901 s->defrag_ratio = n * 10;
2902 return length;
2904 SLAB_ATTR(defrag_ratio);
2905 #endif
2907 static struct attribute * slab_attrs[] = {
2908 &slab_size_attr.attr,
2909 &object_size_attr.attr,
2910 &objs_per_slab_attr.attr,
2911 &order_attr.attr,
2912 &objects_attr.attr,
2913 &slabs_attr.attr,
2914 &partial_attr.attr,
2915 &cpu_slabs_attr.attr,
2916 &ctor_attr.attr,
2917 &dtor_attr.attr,
2918 &aliases_attr.attr,
2919 &align_attr.attr,
2920 &sanity_checks_attr.attr,
2921 &trace_attr.attr,
2922 &hwcache_align_attr.attr,
2923 &reclaim_account_attr.attr,
2924 &destroy_by_rcu_attr.attr,
2925 &red_zone_attr.attr,
2926 &poison_attr.attr,
2927 &store_user_attr.attr,
2928 #ifdef CONFIG_ZONE_DMA
2929 &cache_dma_attr.attr,
2930 #endif
2931 #ifdef CONFIG_NUMA
2932 &defrag_ratio_attr.attr,
2933 #endif
2934 NULL
2937 static struct attribute_group slab_attr_group = {
2938 .attrs = slab_attrs,
2941 static ssize_t slab_attr_show(struct kobject *kobj,
2942 struct attribute *attr,
2943 char *buf)
2945 struct slab_attribute *attribute;
2946 struct kmem_cache *s;
2947 int err;
2949 attribute = to_slab_attr(attr);
2950 s = to_slab(kobj);
2952 if (!attribute->show)
2953 return -EIO;
2955 err = attribute->show(s, buf);
2957 return err;
2960 static ssize_t slab_attr_store(struct kobject *kobj,
2961 struct attribute *attr,
2962 const char *buf, size_t len)
2964 struct slab_attribute *attribute;
2965 struct kmem_cache *s;
2966 int err;
2968 attribute = to_slab_attr(attr);
2969 s = to_slab(kobj);
2971 if (!attribute->store)
2972 return -EIO;
2974 err = attribute->store(s, buf, len);
2976 return err;
2979 static struct sysfs_ops slab_sysfs_ops = {
2980 .show = slab_attr_show,
2981 .store = slab_attr_store,
2984 static struct kobj_type slab_ktype = {
2985 .sysfs_ops = &slab_sysfs_ops,
2988 static int uevent_filter(struct kset *kset, struct kobject *kobj)
2990 struct kobj_type *ktype = get_ktype(kobj);
2992 if (ktype == &slab_ktype)
2993 return 1;
2994 return 0;
2997 static struct kset_uevent_ops slab_uevent_ops = {
2998 .filter = uevent_filter,
3001 decl_subsys(slab, &slab_ktype, &slab_uevent_ops);
3003 #define ID_STR_LENGTH 64
3005 /* Create a unique string id for a slab cache:
3006 * format
3007 * :[flags-]size:[memory address of kmemcache]
3009 static char *create_unique_id(struct kmem_cache *s)
3011 char *name = kmalloc(ID_STR_LENGTH, GFP_KERNEL);
3012 char *p = name;
3014 BUG_ON(!name);
3016 *p++ = ':';
3018 * First flags affecting slabcache operations. We will only
3019 * get here for aliasable slabs so we do not need to support
3020 * too many flags. The flags here must cover all flags that
3021 * are matched during merging to guarantee that the id is
3022 * unique.
3024 if (s->flags & SLAB_CACHE_DMA)
3025 *p++ = 'd';
3026 if (s->flags & SLAB_RECLAIM_ACCOUNT)
3027 *p++ = 'a';
3028 if (s->flags & SLAB_DEBUG_FREE)
3029 *p++ = 'F';
3030 if (p != name + 1)
3031 *p++ = '-';
3032 p += sprintf(p, "%07d", s->size);
3033 BUG_ON(p > name + ID_STR_LENGTH - 1);
3034 return name;
3037 static int sysfs_slab_add(struct kmem_cache *s)
3039 int err;
3040 const char *name;
3041 int unmergeable;
3043 if (slab_state < SYSFS)
3044 /* Defer until later */
3045 return 0;
3047 unmergeable = slab_unmergeable(s);
3048 if (unmergeable) {
3050 * Slabcache can never be merged so we can use the name proper.
3051 * This is typically the case for debug situations. In that
3052 * case we can catch duplicate names easily.
3054 sysfs_remove_link(&slab_subsys.kset.kobj, s->name);
3055 name = s->name;
3056 } else {
3058 * Create a unique name for the slab as a target
3059 * for the symlinks.
3061 name = create_unique_id(s);
3064 kobj_set_kset_s(s, slab_subsys);
3065 kobject_set_name(&s->kobj, name);
3066 kobject_init(&s->kobj);
3067 err = kobject_add(&s->kobj);
3068 if (err)
3069 return err;
3071 err = sysfs_create_group(&s->kobj, &slab_attr_group);
3072 if (err)
3073 return err;
3074 kobject_uevent(&s->kobj, KOBJ_ADD);
3075 if (!unmergeable) {
3076 /* Setup first alias */
3077 sysfs_slab_alias(s, s->name);
3078 kfree(name);
3080 return 0;
3083 static void sysfs_slab_remove(struct kmem_cache *s)
3085 kobject_uevent(&s->kobj, KOBJ_REMOVE);
3086 kobject_del(&s->kobj);
3090 * Need to buffer aliases during bootup until sysfs becomes
3091 * available lest we loose that information.
3093 struct saved_alias {
3094 struct kmem_cache *s;
3095 const char *name;
3096 struct saved_alias *next;
3099 struct saved_alias *alias_list;
3101 static int sysfs_slab_alias(struct kmem_cache *s, const char *name)
3103 struct saved_alias *al;
3105 if (slab_state == SYSFS) {
3107 * If we have a leftover link then remove it.
3109 sysfs_remove_link(&slab_subsys.kset.kobj, name);
3110 return sysfs_create_link(&slab_subsys.kset.kobj,
3111 &s->kobj, name);
3114 al = kmalloc(sizeof(struct saved_alias), GFP_KERNEL);
3115 if (!al)
3116 return -ENOMEM;
3118 al->s = s;
3119 al->name = name;
3120 al->next = alias_list;
3121 alias_list = al;
3122 return 0;
3125 static int __init slab_sysfs_init(void)
3127 int err;
3129 err = subsystem_register(&slab_subsys);
3130 if (err) {
3131 printk(KERN_ERR "Cannot register slab subsystem.\n");
3132 return -ENOSYS;
3135 finish_bootstrap();
3137 while (alias_list) {
3138 struct saved_alias *al = alias_list;
3140 alias_list = alias_list->next;
3141 err = sysfs_slab_alias(al->s, al->name);
3142 BUG_ON(err);
3143 kfree(al);
3146 resiliency_test();
3147 return 0;
3150 __initcall(slab_sysfs_init);
3151 #else
3152 __initcall(finish_bootstrap);
3153 #endif