repo init
[linux-rt-nao.git] / mm / slub.c
blobed9a4a0f9f06990e984683a1d2b261a3daedd072
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
9 */
11 #include <linux/mm.h>
12 #include <linux/swap.h> /* struct reclaim_state */
13 #include <linux/module.h>
14 #include <linux/bit_spinlock.h>
15 #include <linux/interrupt.h>
16 #include <linux/bitops.h>
17 #include <linux/slab.h>
18 #include <linux/proc_fs.h>
19 #include <linux/seq_file.h>
20 #include <trace/kmemtrace.h>
21 #include <linux/cpu.h>
22 #include <linux/cpuset.h>
23 #include <linux/mempolicy.h>
24 #include <linux/ctype.h>
25 #include <linux/debugobjects.h>
26 #include <linux/kallsyms.h>
27 #include <linux/memory.h>
28 #include <linux/math64.h>
29 #include <linux/kmemcheck.h>
30 #include <linux/fault-inject.h>
33 * Lock order:
34 * 1. slab_lock(page)
35 * 2. slab->list_lock
37 * The slab_lock protects operations on the object of a particular
38 * slab and its metadata in the page struct. If the slab lock
39 * has been taken then no allocations nor frees can be performed
40 * on the objects in the slab nor can the slab be added or removed
41 * from the partial or full lists since this would mean modifying
42 * the page_struct of the slab.
44 * The list_lock protects the partial and full list on each node and
45 * the partial slab counter. If taken then no new slabs may be added or
46 * removed from the lists nor make the number of partial slabs be modified.
47 * (Note that the total number of slabs is an atomic value that may be
48 * modified without taking the list lock).
50 * The list_lock is a centralized lock and thus we avoid taking it as
51 * much as possible. As long as SLUB does not have to handle partial
52 * slabs, operations can continue without any centralized lock. F.e.
53 * allocating a long series of objects that fill up slabs does not require
54 * the list lock.
56 * The lock order is sometimes inverted when we are trying to get a slab
57 * off a list. We take the list_lock and then look for a page on the list
58 * to use. While we do that objects in the slabs may be freed. We can
59 * only operate on the slab if we have also taken the slab_lock. So we use
60 * a slab_trylock() on the slab. If trylock was successful then no frees
61 * can occur anymore and we can use the slab for allocations etc. If the
62 * slab_trylock() does not succeed then frees are in progress in the slab and
63 * we must stay away from it for a while since we may cause a bouncing
64 * cacheline if we try to acquire the lock. So go onto the next slab.
65 * If all pages are busy then we may allocate a new slab instead of reusing
66 * a partial slab. A new slab has noone operating on it and thus there is
67 * no danger of cacheline contention.
69 * Interrupts are disabled during allocation and deallocation in order to
70 * make the slab allocator safe to use in the context of an irq. In addition
71 * interrupts are disabled to ensure that the processor does not change
72 * while handling per_cpu slabs, due to kernel preemption.
74 * SLUB assigns one slab for allocation to each processor.
75 * Allocations only occur from these slabs called cpu slabs.
77 * Slabs with free elements are kept on a partial list and during regular
78 * operations no list for full slabs is used. If an object in a full slab is
79 * freed then the slab will show up again on the partial lists.
80 * We track full slabs for debugging purposes though because otherwise we
81 * cannot scan all objects.
83 * Slabs are freed when they become empty. Teardown and setup is
84 * minimal so we rely on the page allocators per cpu caches for
85 * fast frees and allocs.
87 * Overloading of page flags that are otherwise used for LRU management.
89 * PageActive The slab is frozen and exempt from list processing.
90 * This means that the slab is dedicated to a purpose
91 * such as satisfying allocations for a specific
92 * processor. Objects may be freed in the slab while
93 * it is frozen but slab_free will then skip the usual
94 * list operations. It is up to the processor holding
95 * the slab to integrate the slab into the slab lists
96 * when the slab is no longer needed.
98 * One use of this flag is to mark slabs that are
99 * used for allocations. Then such a slab becomes a cpu
100 * slab. The cpu slab may be equipped with an additional
101 * freelist that allows lockless access to
102 * free objects in addition to the regular freelist
103 * that requires the slab lock.
105 * PageError Slab requires special handling due to debug
106 * options set. This moves slab handling out of
107 * the fast path and disables lockless freelists.
110 #ifdef CONFIG_SLUB_DEBUG
111 #define SLABDEBUG 1
112 #else
113 #define SLABDEBUG 0
114 #endif
117 * Issues still to be resolved:
119 * - Support PAGE_ALLOC_DEBUG. Should be easy to do.
121 * - Variable sizing of the per node arrays
124 /* Enable to test recovery from slab corruption on boot */
125 #undef SLUB_RESILIENCY_TEST
128 * Mininum number of partial slabs. These will be left on the partial
129 * lists even if they are empty. kmem_cache_shrink may reclaim them.
131 #define MIN_PARTIAL 5
134 * Maximum number of desirable partial slabs.
135 * The existence of more partial slabs makes kmem_cache_shrink
136 * sort the partial list by the number of objects in the.
138 #define MAX_PARTIAL 10
140 #define DEBUG_DEFAULT_FLAGS (SLAB_DEBUG_FREE | SLAB_RED_ZONE | \
141 SLAB_POISON | SLAB_STORE_USER)
144 * Set of flags that will prevent slab merging
146 #define SLUB_NEVER_MERGE (SLAB_RED_ZONE | SLAB_POISON | SLAB_STORE_USER | \
147 SLAB_TRACE | SLAB_DESTROY_BY_RCU)
149 #define SLUB_MERGE_SAME (SLAB_DEBUG_FREE | SLAB_RECLAIM_ACCOUNT | \
150 SLAB_CACHE_DMA | SLAB_NOTRACK)
152 #ifndef ARCH_KMALLOC_MINALIGN
153 #define ARCH_KMALLOC_MINALIGN __alignof__(unsigned long long)
154 #endif
156 #ifndef ARCH_SLAB_MINALIGN
157 #define ARCH_SLAB_MINALIGN __alignof__(unsigned long long)
158 #endif
160 #define OO_SHIFT 16
161 #define OO_MASK ((1 << OO_SHIFT) - 1)
162 #define MAX_OBJS_PER_PAGE 65535 /* since page.objects is u16 */
164 /* Internal SLUB flags */
165 #define __OBJECT_POISON 0x80000000 /* Poison object */
166 #define __SYSFS_ADD_DEFERRED 0x40000000 /* Not yet visible via sysfs */
168 static int kmem_size = sizeof(struct kmem_cache);
170 #ifdef CONFIG_SMP
171 static struct notifier_block slab_notifier;
172 #endif
174 static enum {
175 DOWN, /* No slab functionality available */
176 PARTIAL, /* kmem_cache_open() works but kmalloc does not */
177 UP, /* Everything works but does not show up in sysfs */
178 SYSFS /* Sysfs up */
179 } slab_state = DOWN;
181 /* A list of all slab caches on the system */
182 static DECLARE_RWSEM(slub_lock);
183 static LIST_HEAD(slab_caches);
186 * Tracking user of a slab.
188 struct track {
189 unsigned long addr; /* Called from address */
190 int cpu; /* Was running on cpu */
191 int pid; /* Pid context */
192 unsigned long when; /* When did the operation occur */
195 enum track_item { TRACK_ALLOC, TRACK_FREE };
197 #ifdef CONFIG_SLUB_DEBUG
198 static int sysfs_slab_add(struct kmem_cache *);
199 static int sysfs_slab_alias(struct kmem_cache *, const char *);
200 static void sysfs_slab_remove(struct kmem_cache *);
202 #else
203 static inline int sysfs_slab_add(struct kmem_cache *s) { return 0; }
204 static inline int sysfs_slab_alias(struct kmem_cache *s, const char *p)
205 { return 0; }
206 static inline void sysfs_slab_remove(struct kmem_cache *s)
208 kfree(s);
211 #endif
213 static inline void stat(struct kmem_cache_cpu *c, enum stat_item si)
215 #ifdef CONFIG_SLUB_STATS
216 c->stat[si]++;
217 #endif
220 /********************************************************************
221 * Core slab cache functions
222 *******************************************************************/
224 int slab_is_available(void)
226 return slab_state >= UP;
229 static inline struct kmem_cache_node *get_node(struct kmem_cache *s, int node)
231 #ifdef CONFIG_NUMA
232 return s->node[node];
233 #else
234 return &s->local_node;
235 #endif
238 static inline struct kmem_cache_cpu *get_cpu_slab(struct kmem_cache *s, int cpu)
240 #ifdef CONFIG_SMP
241 return s->cpu_slab[cpu];
242 #else
243 return &s->cpu_slab;
244 #endif
247 /* Verify that a pointer has an address that is valid within a slab page */
248 static inline int check_valid_pointer(struct kmem_cache *s,
249 struct page *page, const void *object)
251 void *base;
253 if (!object)
254 return 1;
256 base = page_address(page);
257 if (object < base || object >= base + page->objects * s->size ||
258 (object - base) % s->size) {
259 return 0;
262 return 1;
266 * Slow version of get and set free pointer.
268 * This version requires touching the cache lines of kmem_cache which
269 * we avoid to do in the fast alloc free paths. There we obtain the offset
270 * from the page struct.
272 static inline void *get_freepointer(struct kmem_cache *s, void *object)
274 return *(void **)(object + s->offset);
277 static inline void set_freepointer(struct kmem_cache *s, void *object, void *fp)
279 *(void **)(object + s->offset) = fp;
282 /* Loop over all objects in a slab */
283 #define for_each_object(__p, __s, __addr, __objects) \
284 for (__p = (__addr); __p < (__addr) + (__objects) * (__s)->size;\
285 __p += (__s)->size)
287 /* Scan freelist */
288 #define for_each_free_object(__p, __s, __free) \
289 for (__p = (__free); __p; __p = get_freepointer((__s), __p))
291 /* Determine object index from a given position */
292 static inline int slab_index(void *p, struct kmem_cache *s, void *addr)
294 return (p - addr) / s->size;
297 static inline struct kmem_cache_order_objects oo_make(int order,
298 unsigned long size)
300 struct kmem_cache_order_objects x = {
301 (order << OO_SHIFT) + (PAGE_SIZE << order) / size
304 return x;
307 static inline int oo_order(struct kmem_cache_order_objects x)
309 return x.x >> OO_SHIFT;
312 static inline int oo_objects(struct kmem_cache_order_objects x)
314 return x.x & OO_MASK;
317 #ifdef CONFIG_SLUB_DEBUG
319 * Debug settings:
321 #ifdef CONFIG_SLUB_DEBUG_ON
322 static int slub_debug = DEBUG_DEFAULT_FLAGS;
323 #else
324 static int slub_debug;
325 #endif
327 static char *slub_debug_slabs;
330 * Object debugging
332 static void print_section(char *text, u8 *addr, unsigned int length)
334 int i, offset;
335 int newline = 1;
336 char ascii[17];
338 ascii[16] = 0;
340 for (i = 0; i < length; i++) {
341 if (newline) {
342 printk(KERN_ERR "%8s 0x%p: ", text, addr + i);
343 newline = 0;
345 printk(KERN_CONT " %02x", addr[i]);
346 offset = i % 16;
347 ascii[offset] = isgraph(addr[i]) ? addr[i] : '.';
348 if (offset == 15) {
349 printk(KERN_CONT " %s\n", ascii);
350 newline = 1;
353 if (!newline) {
354 i %= 16;
355 while (i < 16) {
356 printk(KERN_CONT " ");
357 ascii[i] = ' ';
358 i++;
360 printk(KERN_CONT " %s\n", ascii);
364 static struct track *get_track(struct kmem_cache *s, void *object,
365 enum track_item alloc)
367 struct track *p;
369 if (s->offset)
370 p = object + s->offset + sizeof(void *);
371 else
372 p = object + s->inuse;
374 return p + alloc;
377 static void set_track(struct kmem_cache *s, void *object,
378 enum track_item alloc, unsigned long addr)
380 struct track *p;
382 if (s->offset)
383 p = object + s->offset + sizeof(void *);
384 else
385 p = object + s->inuse;
387 p += alloc;
388 if (addr) {
389 p->addr = addr;
390 p->cpu = smp_processor_id();
391 p->pid = current->pid;
392 p->when = jiffies;
393 } else
394 memset(p, 0, sizeof(struct track));
397 static void init_tracking(struct kmem_cache *s, void *object)
399 if (!(s->flags & SLAB_STORE_USER))
400 return;
402 set_track(s, object, TRACK_FREE, 0UL);
403 set_track(s, object, TRACK_ALLOC, 0UL);
406 static void print_track(const char *s, struct track *t)
408 if (!t->addr)
409 return;
411 printk(KERN_ERR "INFO: %s in %pS age=%lu cpu=%u pid=%d\n",
412 s, (void *)t->addr, jiffies - t->when, t->cpu, t->pid);
415 static void print_tracking(struct kmem_cache *s, void *object)
417 if (!(s->flags & SLAB_STORE_USER))
418 return;
420 print_track("Allocated", get_track(s, object, TRACK_ALLOC));
421 print_track("Freed", get_track(s, object, TRACK_FREE));
424 static void print_page_info(struct page *page)
426 printk(KERN_ERR "INFO: Slab 0x%p objects=%u used=%u fp=0x%p flags=0x%04lx\n",
427 page, page->objects, page->inuse, page->freelist, page->flags);
431 static void slab_bug(struct kmem_cache *s, char *fmt, ...)
433 va_list args;
434 char buf[100];
436 va_start(args, fmt);
437 vsnprintf(buf, sizeof(buf), fmt, args);
438 va_end(args);
439 printk(KERN_ERR "========================================"
440 "=====================================\n");
441 printk(KERN_ERR "BUG %s: %s\n", s->name, buf);
442 printk(KERN_ERR "----------------------------------------"
443 "-------------------------------------\n\n");
446 static void slab_fix(struct kmem_cache *s, char *fmt, ...)
448 va_list args;
449 char buf[100];
451 va_start(args, fmt);
452 vsnprintf(buf, sizeof(buf), fmt, args);
453 va_end(args);
454 printk(KERN_ERR "FIX %s: %s\n", s->name, buf);
457 static void print_trailer(struct kmem_cache *s, struct page *page, u8 *p)
459 unsigned int off; /* Offset of last byte */
460 u8 *addr = page_address(page);
462 print_tracking(s, p);
464 print_page_info(page);
466 printk(KERN_ERR "INFO: Object 0x%p @offset=%tu fp=0x%p\n\n",
467 p, p - addr, get_freepointer(s, p));
469 if (p > addr + 16)
470 print_section("Bytes b4", p - 16, 16);
472 print_section("Object", p, min_t(unsigned long, s->objsize, PAGE_SIZE));
474 if (s->flags & SLAB_RED_ZONE)
475 print_section("Redzone", p + s->objsize,
476 s->inuse - s->objsize);
478 if (s->offset)
479 off = s->offset + sizeof(void *);
480 else
481 off = s->inuse;
483 if (s->flags & SLAB_STORE_USER)
484 off += 2 * sizeof(struct track);
486 if (off != s->size)
487 /* Beginning of the filler is the free pointer */
488 print_section("Padding", p + off, s->size - off);
490 dump_stack();
493 static void object_err(struct kmem_cache *s, struct page *page,
494 u8 *object, char *reason)
496 slab_bug(s, "%s", reason);
497 print_trailer(s, page, object);
500 static void slab_err(struct kmem_cache *s, struct page *page, char *fmt, ...)
502 va_list args;
503 char buf[100];
505 va_start(args, fmt);
506 vsnprintf(buf, sizeof(buf), fmt, args);
507 va_end(args);
508 slab_bug(s, "%s", buf);
509 print_page_info(page);
510 dump_stack();
513 static void init_object(struct kmem_cache *s, void *object, int active)
515 u8 *p = object;
517 if (s->flags & __OBJECT_POISON) {
518 memset(p, POISON_FREE, s->objsize - 1);
519 p[s->objsize - 1] = POISON_END;
522 if (s->flags & SLAB_RED_ZONE)
523 memset(p + s->objsize,
524 active ? SLUB_RED_ACTIVE : SLUB_RED_INACTIVE,
525 s->inuse - s->objsize);
528 static u8 *check_bytes(u8 *start, unsigned int value, unsigned int bytes)
530 while (bytes) {
531 if (*start != (u8)value)
532 return start;
533 start++;
534 bytes--;
536 return NULL;
539 static void restore_bytes(struct kmem_cache *s, char *message, u8 data,
540 void *from, void *to)
542 slab_fix(s, "Restoring 0x%p-0x%p=0x%x\n", from, to - 1, data);
543 memset(from, data, to - from);
546 static int check_bytes_and_report(struct kmem_cache *s, struct page *page,
547 u8 *object, char *what,
548 u8 *start, unsigned int value, unsigned int bytes)
550 u8 *fault;
551 u8 *end;
553 fault = check_bytes(start, value, bytes);
554 if (!fault)
555 return 1;
557 end = start + bytes;
558 while (end > fault && end[-1] == value)
559 end--;
561 slab_bug(s, "%s overwritten", what);
562 printk(KERN_ERR "INFO: 0x%p-0x%p. First byte 0x%x instead of 0x%x\n",
563 fault, end - 1, fault[0], value);
564 print_trailer(s, page, object);
566 restore_bytes(s, what, value, fault, end);
567 return 0;
571 * Object layout:
573 * object address
574 * Bytes of the object to be managed.
575 * If the freepointer may overlay the object then the free
576 * pointer is the first word of the object.
578 * Poisoning uses 0x6b (POISON_FREE) and the last byte is
579 * 0xa5 (POISON_END)
581 * object + s->objsize
582 * Padding to reach word boundary. This is also used for Redzoning.
583 * Padding is extended by another word if Redzoning is enabled and
584 * objsize == inuse.
586 * We fill with 0xbb (RED_INACTIVE) for inactive objects and with
587 * 0xcc (RED_ACTIVE) for objects in use.
589 * object + s->inuse
590 * Meta data starts here.
592 * A. Free pointer (if we cannot overwrite object on free)
593 * B. Tracking data for SLAB_STORE_USER
594 * C. Padding to reach required alignment boundary or at mininum
595 * one word if debugging is on to be able to detect writes
596 * before the word boundary.
598 * Padding is done using 0x5a (POISON_INUSE)
600 * object + s->size
601 * Nothing is used beyond s->size.
603 * If slabcaches are merged then the objsize and inuse boundaries are mostly
604 * ignored. And therefore no slab options that rely on these boundaries
605 * may be used with merged slabcaches.
608 static int check_pad_bytes(struct kmem_cache *s, struct page *page, u8 *p)
610 unsigned long off = s->inuse; /* The end of info */
612 if (s->offset)
613 /* Freepointer is placed after the object. */
614 off += sizeof(void *);
616 if (s->flags & SLAB_STORE_USER)
617 /* We also have user information there */
618 off += 2 * sizeof(struct track);
620 if (s->size == off)
621 return 1;
623 return check_bytes_and_report(s, page, p, "Object padding",
624 p + off, POISON_INUSE, s->size - off);
627 /* Check the pad bytes at the end of a slab page */
628 static int slab_pad_check(struct kmem_cache *s, struct page *page)
630 u8 *start;
631 u8 *fault;
632 u8 *end;
633 int length;
634 int remainder;
636 if (!(s->flags & SLAB_POISON))
637 return 1;
639 start = page_address(page);
640 length = (PAGE_SIZE << compound_order(page));
641 end = start + length;
642 remainder = length % s->size;
643 if (!remainder)
644 return 1;
646 fault = check_bytes(end - remainder, POISON_INUSE, remainder);
647 if (!fault)
648 return 1;
649 while (end > fault && end[-1] == POISON_INUSE)
650 end--;
652 slab_err(s, page, "Padding overwritten. 0x%p-0x%p", fault, end - 1);
653 print_section("Padding", end - remainder, remainder);
655 restore_bytes(s, "slab padding", POISON_INUSE, start, end);
656 return 0;
659 static int check_object(struct kmem_cache *s, struct page *page,
660 void *object, int active)
662 u8 *p = object;
663 u8 *endobject = object + s->objsize;
665 if (s->flags & SLAB_RED_ZONE) {
666 unsigned int red =
667 active ? SLUB_RED_ACTIVE : SLUB_RED_INACTIVE;
669 if (!check_bytes_and_report(s, page, object, "Redzone",
670 endobject, red, s->inuse - s->objsize))
671 return 0;
672 } else {
673 if ((s->flags & SLAB_POISON) && s->objsize < s->inuse) {
674 check_bytes_and_report(s, page, p, "Alignment padding",
675 endobject, POISON_INUSE, s->inuse - s->objsize);
679 if (s->flags & SLAB_POISON) {
680 if (!active && (s->flags & __OBJECT_POISON) &&
681 (!check_bytes_and_report(s, page, p, "Poison", p,
682 POISON_FREE, s->objsize - 1) ||
683 !check_bytes_and_report(s, page, p, "Poison",
684 p + s->objsize - 1, POISON_END, 1)))
685 return 0;
687 * check_pad_bytes cleans up on its own.
689 check_pad_bytes(s, page, p);
692 if (!s->offset && active)
694 * Object and freepointer overlap. Cannot check
695 * freepointer while object is allocated.
697 return 1;
699 /* Check free pointer validity */
700 if (!check_valid_pointer(s, page, get_freepointer(s, p))) {
701 object_err(s, page, p, "Freepointer corrupt");
703 * No choice but to zap it and thus lose the remainder
704 * of the free objects in this slab. May cause
705 * another error because the object count is now wrong.
707 set_freepointer(s, p, NULL);
708 return 0;
710 return 1;
713 static int check_slab(struct kmem_cache *s, struct page *page)
715 int maxobj;
717 VM_BUG_ON(!irqs_disabled());
719 if (!PageSlab(page)) {
720 slab_err(s, page, "Not a valid slab page");
721 return 0;
724 maxobj = (PAGE_SIZE << compound_order(page)) / s->size;
725 if (page->objects > maxobj) {
726 slab_err(s, page, "objects %u > max %u",
727 s->name, page->objects, maxobj);
728 return 0;
730 if (page->inuse > page->objects) {
731 slab_err(s, page, "inuse %u > max %u",
732 s->name, page->inuse, page->objects);
733 return 0;
735 /* Slab_pad_check fixes things up after itself */
736 slab_pad_check(s, page);
737 return 1;
741 * Determine if a certain object on a page is on the freelist. Must hold the
742 * slab lock to guarantee that the chains are in a consistent state.
744 static int on_freelist(struct kmem_cache *s, struct page *page, void *search)
746 int nr = 0;
747 void *fp = page->freelist;
748 void *object = NULL;
749 unsigned long max_objects;
751 while (fp && nr <= page->objects) {
752 if (fp == search)
753 return 1;
754 if (!check_valid_pointer(s, page, fp)) {
755 if (object) {
756 object_err(s, page, object,
757 "Freechain corrupt");
758 set_freepointer(s, object, NULL);
759 break;
760 } else {
761 slab_err(s, page, "Freepointer corrupt");
762 page->freelist = NULL;
763 page->inuse = page->objects;
764 slab_fix(s, "Freelist cleared");
765 return 0;
767 break;
769 object = fp;
770 fp = get_freepointer(s, object);
771 nr++;
774 max_objects = (PAGE_SIZE << compound_order(page)) / s->size;
775 if (max_objects > MAX_OBJS_PER_PAGE)
776 max_objects = MAX_OBJS_PER_PAGE;
778 if (page->objects != max_objects) {
779 slab_err(s, page, "Wrong number of objects. Found %d but "
780 "should be %d", page->objects, max_objects);
781 page->objects = max_objects;
782 slab_fix(s, "Number of objects adjusted.");
784 if (page->inuse != page->objects - nr) {
785 slab_err(s, page, "Wrong object count. Counter is %d but "
786 "counted were %d", page->inuse, page->objects - nr);
787 page->inuse = page->objects - nr;
788 slab_fix(s, "Object count adjusted.");
790 return search == NULL;
793 static void trace(struct kmem_cache *s, struct page *page, void *object,
794 int alloc)
796 if (s->flags & SLAB_TRACE) {
797 printk(KERN_INFO "TRACE %s %s 0x%p inuse=%d fp=0x%p\n",
798 s->name,
799 alloc ? "alloc" : "free",
800 object, page->inuse,
801 page->freelist);
803 if (!alloc)
804 print_section("Object", (void *)object, s->objsize);
806 dump_stack();
811 * Tracking of fully allocated slabs for debugging purposes.
813 static void add_full(struct kmem_cache_node *n, struct page *page)
815 spin_lock(&n->list_lock);
816 list_add(&page->lru, &n->full);
817 spin_unlock(&n->list_lock);
820 static void remove_full(struct kmem_cache *s, struct page *page)
822 struct kmem_cache_node *n;
824 if (!(s->flags & SLAB_STORE_USER))
825 return;
827 n = get_node(s, page_to_nid(page));
829 spin_lock(&n->list_lock);
830 list_del(&page->lru);
831 spin_unlock(&n->list_lock);
834 /* Tracking of the number of slabs for debugging purposes */
835 static inline unsigned long slabs_node(struct kmem_cache *s, int node)
837 struct kmem_cache_node *n = get_node(s, node);
839 return atomic_long_read(&n->nr_slabs);
842 static inline void inc_slabs_node(struct kmem_cache *s, int node, int objects)
844 struct kmem_cache_node *n = get_node(s, node);
847 * May be called early in order to allocate a slab for the
848 * kmem_cache_node structure. Solve the chicken-egg
849 * dilemma by deferring the increment of the count during
850 * bootstrap (see early_kmem_cache_node_alloc).
852 if (!NUMA_BUILD || n) {
853 atomic_long_inc(&n->nr_slabs);
854 atomic_long_add(objects, &n->total_objects);
857 static inline void dec_slabs_node(struct kmem_cache *s, int node, int objects)
859 struct kmem_cache_node *n = get_node(s, node);
861 atomic_long_dec(&n->nr_slabs);
862 atomic_long_sub(objects, &n->total_objects);
865 /* Object debug checks for alloc/free paths */
866 static void setup_object_debug(struct kmem_cache *s, struct page *page,
867 void *object)
869 if (!(s->flags & (SLAB_STORE_USER|SLAB_RED_ZONE|__OBJECT_POISON)))
870 return;
872 init_object(s, object, 0);
873 init_tracking(s, object);
876 static int alloc_debug_processing(struct kmem_cache *s, struct page *page,
877 void *object, unsigned long addr)
879 if (!check_slab(s, page))
880 goto bad;
882 if (!on_freelist(s, page, object)) {
883 object_err(s, page, object, "Object already allocated");
884 goto bad;
887 if (!check_valid_pointer(s, page, object)) {
888 object_err(s, page, object, "Freelist Pointer check fails");
889 goto bad;
892 if (!check_object(s, page, object, 0))
893 goto bad;
895 /* Success perform special debug activities for allocs */
896 if (s->flags & SLAB_STORE_USER)
897 set_track(s, object, TRACK_ALLOC, addr);
898 trace(s, page, object, 1);
899 init_object(s, object, 1);
900 return 1;
902 bad:
903 if (PageSlab(page)) {
905 * If this is a slab page then lets do the best we can
906 * to avoid issues in the future. Marking all objects
907 * as used avoids touching the remaining objects.
909 slab_fix(s, "Marking all objects used");
910 page->inuse = page->objects;
911 page->freelist = NULL;
913 return 0;
916 static int free_debug_processing(struct kmem_cache *s, struct page *page,
917 void *object, unsigned long addr)
919 if (!check_slab(s, page))
920 goto fail;
922 if (!check_valid_pointer(s, page, object)) {
923 slab_err(s, page, "Invalid object pointer 0x%p", object);
924 goto fail;
927 if (on_freelist(s, page, object)) {
928 object_err(s, page, object, "Object already free");
929 goto fail;
932 if (!check_object(s, page, object, 1))
933 return 0;
935 if (unlikely(s != page->slab)) {
936 if (!PageSlab(page)) {
937 slab_err(s, page, "Attempt to free object(0x%p) "
938 "outside of slab", object);
939 } else if (!page->slab) {
940 printk(KERN_ERR
941 "SLUB <none>: no slab for object 0x%p.\n",
942 object);
943 dump_stack();
944 } else
945 object_err(s, page, object,
946 "page slab pointer corrupt.");
947 goto fail;
950 /* Special debug activities for freeing objects */
951 if (!PageSlubFrozen(page) && !page->freelist)
952 remove_full(s, page);
953 if (s->flags & SLAB_STORE_USER)
954 set_track(s, object, TRACK_FREE, addr);
955 trace(s, page, object, 0);
956 init_object(s, object, 0);
957 return 1;
959 fail:
960 slab_fix(s, "Object at 0x%p not freed", object);
961 return 0;
964 static int __init setup_slub_debug(char *str)
966 slub_debug = DEBUG_DEFAULT_FLAGS;
967 if (*str++ != '=' || !*str)
969 * No options specified. Switch on full debugging.
971 goto out;
973 if (*str == ',')
975 * No options but restriction on slabs. This means full
976 * debugging for slabs matching a pattern.
978 goto check_slabs;
980 slub_debug = 0;
981 if (*str == '-')
983 * Switch off all debugging measures.
985 goto out;
988 * Determine which debug features should be switched on
990 for (; *str && *str != ','; str++) {
991 switch (tolower(*str)) {
992 case 'f':
993 slub_debug |= SLAB_DEBUG_FREE;
994 break;
995 case 'z':
996 slub_debug |= SLAB_RED_ZONE;
997 break;
998 case 'p':
999 slub_debug |= SLAB_POISON;
1000 break;
1001 case 'u':
1002 slub_debug |= SLAB_STORE_USER;
1003 break;
1004 case 't':
1005 slub_debug |= SLAB_TRACE;
1006 break;
1007 default:
1008 printk(KERN_ERR "slub_debug option '%c' "
1009 "unknown. skipped\n", *str);
1013 check_slabs:
1014 if (*str == ',')
1015 slub_debug_slabs = str + 1;
1016 out:
1017 return 1;
1020 __setup("slub_debug", setup_slub_debug);
1022 static unsigned long kmem_cache_flags(unsigned long objsize,
1023 unsigned long flags, const char *name,
1024 void (*ctor)(void *))
1027 * Enable debugging if selected on the kernel commandline.
1029 if (slub_debug && (!slub_debug_slabs ||
1030 strncmp(slub_debug_slabs, name, strlen(slub_debug_slabs)) == 0))
1031 flags |= slub_debug;
1033 return flags;
1035 #else
1036 static inline void setup_object_debug(struct kmem_cache *s,
1037 struct page *page, void *object) {}
1039 static inline int alloc_debug_processing(struct kmem_cache *s,
1040 struct page *page, void *object, unsigned long addr) { return 0; }
1042 static inline int free_debug_processing(struct kmem_cache *s,
1043 struct page *page, void *object, unsigned long addr) { return 0; }
1045 static inline int slab_pad_check(struct kmem_cache *s, struct page *page)
1046 { return 1; }
1047 static inline int check_object(struct kmem_cache *s, struct page *page,
1048 void *object, int active) { return 1; }
1049 static inline void add_full(struct kmem_cache_node *n, struct page *page) {}
1050 static inline unsigned long kmem_cache_flags(unsigned long objsize,
1051 unsigned long flags, const char *name,
1052 void (*ctor)(void *))
1054 return flags;
1056 #define slub_debug 0
1058 static inline unsigned long slabs_node(struct kmem_cache *s, int node)
1059 { return 0; }
1060 static inline void inc_slabs_node(struct kmem_cache *s, int node,
1061 int objects) {}
1062 static inline void dec_slabs_node(struct kmem_cache *s, int node,
1063 int objects) {}
1064 #endif
1067 * Slab allocation and freeing
1069 static inline struct page *alloc_slab_page(gfp_t flags, int node,
1070 struct kmem_cache_order_objects oo)
1072 int order = oo_order(oo);
1074 flags |= __GFP_NOTRACK;
1076 if (node == -1)
1077 return alloc_pages(flags, order);
1078 else
1079 return alloc_pages_node(node, flags, order);
1082 static struct page *allocate_slab(struct kmem_cache *s, gfp_t flags, int node)
1084 struct page *page;
1085 struct kmem_cache_order_objects oo = s->oo;
1087 flags |= s->allocflags;
1089 page = alloc_slab_page(flags | __GFP_NOWARN | __GFP_NORETRY, node,
1090 oo);
1091 if (unlikely(!page)) {
1092 oo = s->min;
1094 * Allocation may have failed due to fragmentation.
1095 * Try a lower order alloc if possible
1097 page = alloc_slab_page(flags, node, oo);
1098 if (!page)
1099 return NULL;
1101 stat(get_cpu_slab(s, raw_smp_processor_id()), ORDER_FALLBACK);
1104 if (kmemcheck_enabled
1105 && !(s->flags & (SLAB_NOTRACK | DEBUG_DEFAULT_FLAGS)))
1107 int pages = 1 << oo_order(oo);
1109 kmemcheck_alloc_shadow(page, oo_order(oo), flags, node);
1112 * Objects from caches that have a constructor don't get
1113 * cleared when they're allocated, so we need to do it here.
1115 if (s->ctor)
1116 kmemcheck_mark_uninitialized_pages(page, pages);
1117 else
1118 kmemcheck_mark_unallocated_pages(page, pages);
1121 page->objects = oo_objects(oo);
1122 mod_zone_page_state(page_zone(page),
1123 (s->flags & SLAB_RECLAIM_ACCOUNT) ?
1124 NR_SLAB_RECLAIMABLE : NR_SLAB_UNRECLAIMABLE,
1125 1 << oo_order(oo));
1127 return page;
1130 static void setup_object(struct kmem_cache *s, struct page *page,
1131 void *object)
1133 setup_object_debug(s, page, object);
1134 if (unlikely(s->ctor))
1135 s->ctor(object);
1138 static struct page *new_slab(struct kmem_cache *s, gfp_t flags, int node)
1140 struct page *page;
1141 void *start;
1142 void *last;
1143 void *p;
1145 BUG_ON(flags & GFP_SLAB_BUG_MASK);
1147 page = allocate_slab(s,
1148 flags & (GFP_RECLAIM_MASK | GFP_CONSTRAINT_MASK), node);
1149 if (!page)
1150 goto out;
1152 inc_slabs_node(s, page_to_nid(page), page->objects);
1153 page->slab = s;
1154 page->flags |= 1 << PG_slab;
1155 if (s->flags & (SLAB_DEBUG_FREE | SLAB_RED_ZONE | SLAB_POISON |
1156 SLAB_STORE_USER | SLAB_TRACE))
1157 __SetPageSlubDebug(page);
1159 start = page_address(page);
1161 if (unlikely(s->flags & SLAB_POISON))
1162 memset(start, POISON_INUSE, PAGE_SIZE << compound_order(page));
1164 last = start;
1165 for_each_object(p, s, start, page->objects) {
1166 setup_object(s, page, last);
1167 set_freepointer(s, last, p);
1168 last = p;
1170 setup_object(s, page, last);
1171 set_freepointer(s, last, NULL);
1173 page->freelist = start;
1174 page->inuse = 0;
1175 out:
1176 return page;
1179 static void __free_slab(struct kmem_cache *s, struct page *page)
1181 int order = compound_order(page);
1182 int pages = 1 << order;
1184 if (unlikely(SLABDEBUG && PageSlubDebug(page))) {
1185 void *p;
1187 slab_pad_check(s, page);
1188 for_each_object(p, s, page_address(page),
1189 page->objects)
1190 check_object(s, page, p, 0);
1191 __ClearPageSlubDebug(page);
1194 kmemcheck_free_shadow(page, compound_order(page));
1196 mod_zone_page_state(page_zone(page),
1197 (s->flags & SLAB_RECLAIM_ACCOUNT) ?
1198 NR_SLAB_RECLAIMABLE : NR_SLAB_UNRECLAIMABLE,
1199 -pages);
1201 __ClearPageSlab(page);
1202 reset_page_mapcount(page);
1203 if (current->reclaim_state)
1204 current->reclaim_state->reclaimed_slab += pages;
1205 __free_pages(page, order);
1208 static void rcu_free_slab(struct rcu_head *h)
1210 struct page *page;
1212 page = container_of((struct list_head *)h, struct page, lru);
1213 __free_slab(page->slab, page);
1216 static void free_slab(struct kmem_cache *s, struct page *page)
1218 if (unlikely(s->flags & SLAB_DESTROY_BY_RCU)) {
1220 * RCU free overloads the RCU head over the LRU
1222 struct rcu_head *head = (void *)&page->lru;
1224 call_rcu(head, rcu_free_slab);
1225 } else
1226 __free_slab(s, page);
1229 static void discard_slab(struct kmem_cache *s, struct page *page)
1231 dec_slabs_node(s, page_to_nid(page), page->objects);
1232 free_slab(s, page);
1236 * Per slab locking using the pagelock
1238 static __always_inline void slab_lock(struct page *page)
1240 bit_spin_lock(PG_locked, &page->flags);
1243 static __always_inline void slab_unlock(struct page *page)
1245 __bit_spin_unlock(PG_locked, &page->flags);
1248 static __always_inline int slab_trylock(struct page *page)
1250 int rc = 1;
1252 rc = bit_spin_trylock(PG_locked, &page->flags);
1253 return rc;
1257 * Management of partially allocated slabs
1259 static void add_partial(struct kmem_cache_node *n,
1260 struct page *page, int tail)
1262 spin_lock(&n->list_lock);
1263 n->nr_partial++;
1264 if (tail)
1265 list_add_tail(&page->lru, &n->partial);
1266 else
1267 list_add(&page->lru, &n->partial);
1268 spin_unlock(&n->list_lock);
1271 static void remove_partial(struct kmem_cache *s, struct page *page)
1273 struct kmem_cache_node *n = get_node(s, page_to_nid(page));
1275 spin_lock(&n->list_lock);
1276 list_del(&page->lru);
1277 n->nr_partial--;
1278 spin_unlock(&n->list_lock);
1282 * Lock slab and remove from the partial list.
1284 * Must hold list_lock.
1286 static inline int lock_and_freeze_slab(struct kmem_cache_node *n,
1287 struct page *page)
1289 if (slab_trylock(page)) {
1290 list_del(&page->lru);
1291 n->nr_partial--;
1292 __SetPageSlubFrozen(page);
1293 return 1;
1295 return 0;
1299 * Try to allocate a partial slab from a specific node.
1301 static struct page *get_partial_node(struct kmem_cache_node *n)
1303 struct page *page;
1306 * Racy check. If we mistakenly see no partial slabs then we
1307 * just allocate an empty slab. If we mistakenly try to get a
1308 * partial slab and there is none available then get_partials()
1309 * will return NULL.
1311 if (!n || !n->nr_partial)
1312 return NULL;
1314 spin_lock(&n->list_lock);
1315 list_for_each_entry(page, &n->partial, lru)
1316 if (lock_and_freeze_slab(n, page))
1317 goto out;
1318 page = NULL;
1319 out:
1320 spin_unlock(&n->list_lock);
1321 return page;
1325 * Get a page from somewhere. Search in increasing NUMA distances.
1327 static struct page *get_any_partial(struct kmem_cache *s, gfp_t flags)
1329 #ifdef CONFIG_NUMA
1330 struct zonelist *zonelist;
1331 struct zoneref *z;
1332 struct zone *zone;
1333 enum zone_type high_zoneidx = gfp_zone(flags);
1334 struct page *page;
1337 * The defrag ratio allows a configuration of the tradeoffs between
1338 * inter node defragmentation and node local allocations. A lower
1339 * defrag_ratio increases the tendency to do local allocations
1340 * instead of attempting to obtain partial slabs from other nodes.
1342 * If the defrag_ratio is set to 0 then kmalloc() always
1343 * returns node local objects. If the ratio is higher then kmalloc()
1344 * may return off node objects because partial slabs are obtained
1345 * from other nodes and filled up.
1347 * If /sys/kernel/slab/xx/defrag_ratio is set to 100 (which makes
1348 * defrag_ratio = 1000) then every (well almost) allocation will
1349 * first attempt to defrag slab caches on other nodes. This means
1350 * scanning over all nodes to look for partial slabs which may be
1351 * expensive if we do it every time we are trying to find a slab
1352 * with available objects.
1354 if (!s->remote_node_defrag_ratio ||
1355 get_cycles() % 1024 > s->remote_node_defrag_ratio)
1356 return NULL;
1358 zonelist = node_zonelist(slab_node(current->mempolicy), flags);
1359 for_each_zone_zonelist(zone, z, zonelist, high_zoneidx) {
1360 struct kmem_cache_node *n;
1362 n = get_node(s, zone_to_nid(zone));
1364 if (n && cpuset_zone_allowed_hardwall(zone, flags) &&
1365 n->nr_partial > n->min_partial) {
1366 page = get_partial_node(n);
1367 if (page)
1368 return page;
1371 #endif
1372 return NULL;
1376 * Get a partial page, lock it and return it.
1378 static struct page *get_partial(struct kmem_cache *s, gfp_t flags, int node)
1380 struct page *page;
1381 int searchnode = (node == -1) ? numa_node_id() : node;
1383 page = get_partial_node(get_node(s, searchnode));
1384 if (page || (flags & __GFP_THISNODE))
1385 return page;
1387 return get_any_partial(s, flags);
1391 * Move a page back to the lists.
1393 * Must be called with the slab lock held.
1395 * On exit the slab lock will have been dropped.
1397 static void unfreeze_slab(struct kmem_cache *s, struct page *page, int tail)
1399 struct kmem_cache_node *n = get_node(s, page_to_nid(page));
1400 struct kmem_cache_cpu *c = get_cpu_slab(s, smp_processor_id());
1402 __ClearPageSlubFrozen(page);
1403 if (page->inuse) {
1405 if (page->freelist) {
1406 add_partial(n, page, tail);
1407 stat(c, tail ? DEACTIVATE_TO_TAIL : DEACTIVATE_TO_HEAD);
1408 } else {
1409 stat(c, DEACTIVATE_FULL);
1410 if (SLABDEBUG && PageSlubDebug(page) &&
1411 (s->flags & SLAB_STORE_USER))
1412 add_full(n, page);
1414 slab_unlock(page);
1415 } else {
1416 stat(c, DEACTIVATE_EMPTY);
1417 if (n->nr_partial < n->min_partial) {
1419 * Adding an empty slab to the partial slabs in order
1420 * to avoid page allocator overhead. This slab needs
1421 * to come after the other slabs with objects in
1422 * so that the others get filled first. That way the
1423 * size of the partial list stays small.
1425 * kmem_cache_shrink can reclaim any empty slabs from
1426 * the partial list.
1428 add_partial(n, page, 1);
1429 slab_unlock(page);
1430 } else {
1431 slab_unlock(page);
1432 stat(get_cpu_slab(s, raw_smp_processor_id()), FREE_SLAB);
1433 discard_slab(s, page);
1439 * Remove the cpu slab
1441 static void deactivate_slab(struct kmem_cache *s, struct kmem_cache_cpu *c)
1443 struct page *page = c->page;
1444 int tail = 1;
1446 if (page->freelist)
1447 stat(c, DEACTIVATE_REMOTE_FREES);
1449 * Merge cpu freelist into slab freelist. Typically we get here
1450 * because both freelists are empty. So this is unlikely
1451 * to occur.
1453 while (unlikely(c->freelist)) {
1454 void **object;
1456 tail = 0; /* Hot objects. Put the slab first */
1458 /* Retrieve object from cpu_freelist */
1459 object = c->freelist;
1460 c->freelist = c->freelist[c->offset];
1462 /* And put onto the regular freelist */
1463 object[c->offset] = page->freelist;
1464 page->freelist = object;
1465 page->inuse--;
1467 c->page = NULL;
1468 unfreeze_slab(s, page, tail);
1471 static inline void flush_slab(struct kmem_cache *s, struct kmem_cache_cpu *c)
1473 stat(c, CPUSLAB_FLUSH);
1474 slab_lock(c->page);
1475 deactivate_slab(s, c);
1479 * Flush cpu slab.
1481 * Called from IPI handler with interrupts disabled.
1483 static inline void __flush_cpu_slab(struct kmem_cache *s, int cpu)
1485 struct kmem_cache_cpu *c = get_cpu_slab(s, cpu);
1487 if (likely(c && c->page))
1488 flush_slab(s, c);
1491 static void flush_cpu_slab(void *d)
1493 struct kmem_cache *s = d;
1495 __flush_cpu_slab(s, smp_processor_id());
1498 static void flush_all(struct kmem_cache *s)
1500 on_each_cpu(flush_cpu_slab, s, 1);
1504 * Check if the objects in a per cpu structure fit numa
1505 * locality expectations.
1507 static inline int node_match(struct kmem_cache_cpu *c, int node)
1509 #ifdef CONFIG_NUMA
1510 if (node != -1 && c->node != node)
1511 return 0;
1512 #endif
1513 return 1;
1517 * Slow path. The lockless freelist is empty or we need to perform
1518 * debugging duties.
1520 * Interrupts are disabled.
1522 * Processing is still very fast if new objects have been freed to the
1523 * regular freelist. In that case we simply take over the regular freelist
1524 * as the lockless freelist and zap the regular freelist.
1526 * If that is not working then we fall back to the partial lists. We take the
1527 * first element of the freelist as the object to allocate now and move the
1528 * rest of the freelist to the lockless freelist.
1530 * And if we were unable to get a new slab from the partial slab lists then
1531 * we need to allocate a new slab. This is the slowest path since it involves
1532 * a call to the page allocator and the setup of a new slab.
1534 static void *__slab_alloc(struct kmem_cache *s, gfp_t gfpflags, int node,
1535 unsigned long addr, struct kmem_cache_cpu *c)
1537 void **object;
1538 struct page *new;
1540 /* We handle __GFP_ZERO in the caller */
1541 gfpflags &= ~__GFP_ZERO;
1543 if (!c->page)
1544 goto new_slab;
1546 slab_lock(c->page);
1547 if (unlikely(!node_match(c, node)))
1548 goto another_slab;
1550 stat(c, ALLOC_REFILL);
1552 load_freelist:
1553 object = c->page->freelist;
1554 if (unlikely(!object))
1555 goto another_slab;
1556 if (unlikely(SLABDEBUG && PageSlubDebug(c->page)))
1557 goto debug;
1559 c->freelist = object[c->offset];
1560 c->page->inuse = c->page->objects;
1561 c->page->freelist = NULL;
1562 c->node = page_to_nid(c->page);
1563 unlock_out:
1564 slab_unlock(c->page);
1565 stat(c, ALLOC_SLOWPATH);
1566 return object;
1568 another_slab:
1569 deactivate_slab(s, c);
1571 new_slab:
1572 new = get_partial(s, gfpflags, node);
1573 if (new) {
1574 c->page = new;
1575 stat(c, ALLOC_FROM_PARTIAL);
1576 goto load_freelist;
1579 if (gfpflags & __GFP_WAIT)
1580 local_irq_enable();
1582 new = new_slab(s, gfpflags, node);
1584 if (gfpflags & __GFP_WAIT)
1585 local_irq_disable();
1587 if (new) {
1588 c = get_cpu_slab(s, smp_processor_id());
1589 stat(c, ALLOC_SLAB);
1590 if (c->page)
1591 flush_slab(s, c);
1592 slab_lock(new);
1593 __SetPageSlubFrozen(new);
1594 c->page = new;
1595 goto load_freelist;
1597 return NULL;
1598 debug:
1599 if (!alloc_debug_processing(s, c->page, object, addr))
1600 goto another_slab;
1602 c->page->inuse++;
1603 c->page->freelist = object[c->offset];
1604 c->node = -1;
1605 goto unlock_out;
1609 * Inlined fastpath so that allocation functions (kmalloc, kmem_cache_alloc)
1610 * have the fastpath folded into their functions. So no function call
1611 * overhead for requests that can be satisfied on the fastpath.
1613 * The fastpath works by first checking if the lockless freelist can be used.
1614 * If not then __slab_alloc is called for slow processing.
1616 * Otherwise we can simply pick the next object from the lockless free list.
1618 static __always_inline void *slab_alloc(struct kmem_cache *s,
1619 gfp_t gfpflags, int node, unsigned long addr)
1621 void **object;
1622 struct kmem_cache_cpu *c;
1623 unsigned long flags;
1624 unsigned int objsize;
1626 lockdep_trace_alloc(gfpflags);
1627 might_sleep_if(gfpflags & __GFP_WAIT);
1629 if (should_failslab(s->objsize, gfpflags))
1630 return NULL;
1632 local_irq_save(flags);
1633 c = get_cpu_slab(s, smp_processor_id());
1634 objsize = c->objsize;
1635 if (unlikely(!c->freelist || !node_match(c, node)))
1637 object = __slab_alloc(s, gfpflags, node, addr, c);
1639 else {
1640 object = c->freelist;
1641 c->freelist = object[c->offset];
1642 stat(c, ALLOC_FASTPATH);
1644 local_irq_restore(flags);
1646 if (unlikely((gfpflags & __GFP_ZERO) && object))
1647 memset(object, 0, objsize);
1649 kmemcheck_slab_alloc(s, gfpflags, object, c->objsize);
1650 return object;
1653 void *kmem_cache_alloc(struct kmem_cache *s, gfp_t gfpflags)
1655 void *ret = slab_alloc(s, gfpflags, -1, _RET_IP_);
1657 trace_kmem_cache_alloc(_RET_IP_, ret, s->objsize, s->size, gfpflags);
1659 return ret;
1661 EXPORT_SYMBOL(kmem_cache_alloc);
1663 #ifdef CONFIG_KMEMTRACE
1664 void *kmem_cache_alloc_notrace(struct kmem_cache *s, gfp_t gfpflags)
1666 return slab_alloc(s, gfpflags, -1, _RET_IP_);
1668 EXPORT_SYMBOL(kmem_cache_alloc_notrace);
1669 #endif
1671 #ifdef CONFIG_NUMA
1672 void *kmem_cache_alloc_node(struct kmem_cache *s, gfp_t gfpflags, int node)
1674 void *ret = slab_alloc(s, gfpflags, node, _RET_IP_);
1676 trace_kmem_cache_alloc_node(_RET_IP_, ret,
1677 s->objsize, s->size, gfpflags, node);
1679 return ret;
1681 EXPORT_SYMBOL(kmem_cache_alloc_node);
1682 #endif
1684 #ifdef CONFIG_KMEMTRACE
1685 void *kmem_cache_alloc_node_notrace(struct kmem_cache *s,
1686 gfp_t gfpflags,
1687 int node)
1689 return slab_alloc(s, gfpflags, node, _RET_IP_);
1691 EXPORT_SYMBOL(kmem_cache_alloc_node_notrace);
1692 #endif
1695 * Slow patch handling. This may still be called frequently since objects
1696 * have a longer lifetime than the cpu slabs in most processing loads.
1698 * So we still attempt to reduce cache line usage. Just take the slab
1699 * lock and free the item. If there is no additional partial page
1700 * handling required then we can return immediately.
1702 static void __slab_free(struct kmem_cache *s, struct page *page,
1703 void *x, unsigned long addr, unsigned int offset)
1705 void *prior;
1706 void **object = (void *)x;
1707 struct kmem_cache_cpu *c;
1709 c = get_cpu_slab(s, raw_smp_processor_id());
1710 stat(c, FREE_SLOWPATH);
1711 slab_lock(page);
1713 if (unlikely(SLABDEBUG && PageSlubDebug(page)))
1714 goto debug;
1716 checks_ok:
1717 prior = object[offset] = page->freelist;
1718 page->freelist = object;
1719 page->inuse--;
1721 if (unlikely(PageSlubFrozen(page))) {
1722 stat(c, FREE_FROZEN);
1723 goto out_unlock;
1726 if (unlikely(!page->inuse))
1727 goto slab_empty;
1730 * Objects left in the slab. If it was not on the partial list before
1731 * then add it.
1733 if (unlikely(!prior)) {
1734 add_partial(get_node(s, page_to_nid(page)), page, 1);
1735 stat(c, FREE_ADD_PARTIAL);
1738 out_unlock:
1739 slab_unlock(page);
1740 return;
1742 slab_empty:
1743 if (prior) {
1745 * Slab still on the partial list.
1747 remove_partial(s, page);
1748 stat(c, FREE_REMOVE_PARTIAL);
1750 slab_unlock(page);
1751 stat(c, FREE_SLAB);
1752 discard_slab(s, page);
1753 return;
1755 debug:
1756 if (!free_debug_processing(s, page, x, addr))
1757 goto out_unlock;
1758 goto checks_ok;
1762 * Fastpath with forced inlining to produce a kfree and kmem_cache_free that
1763 * can perform fastpath freeing without additional function calls.
1765 * The fastpath is only possible if we are freeing to the current cpu slab
1766 * of this processor. This typically the case if we have just allocated
1767 * the item before.
1769 * If fastpath is not possible then fall back to __slab_free where we deal
1770 * with all sorts of special processing.
1772 static __always_inline void slab_free(struct kmem_cache *s,
1773 struct page *page, void *x, unsigned long addr)
1775 void **object = (void *)x;
1776 struct kmem_cache_cpu *c;
1777 unsigned long flags;
1779 local_irq_save(flags);
1780 c = get_cpu_slab(s, smp_processor_id());
1781 kmemcheck_slab_free(s, object, c->objsize);
1782 debug_check_no_locks_freed(object, c->objsize);
1783 if (!(s->flags & SLAB_DEBUG_OBJECTS))
1784 debug_check_no_obj_freed(object, s->objsize);
1785 if (likely(page == c->page && c->node >= 0)) {
1786 object[c->offset] = c->freelist;
1787 c->freelist = object;
1788 stat(c, FREE_FASTPATH);
1789 } else
1790 __slab_free(s, page, x, addr, c->offset);
1792 local_irq_restore(flags);
1795 void kmem_cache_free(struct kmem_cache *s, void *x)
1797 struct page *page;
1799 page = virt_to_head_page(x);
1801 slab_free(s, page, x, _RET_IP_);
1803 trace_kmem_cache_free(_RET_IP_, x);
1805 EXPORT_SYMBOL(kmem_cache_free);
1807 /* Figure out on which slab page the object resides */
1808 static struct page *get_object_page(const void *x)
1810 struct page *page = virt_to_head_page(x);
1812 if (!PageSlab(page))
1813 return NULL;
1815 return page;
1819 * Object placement in a slab is made very easy because we always start at
1820 * offset 0. If we tune the size of the object to the alignment then we can
1821 * get the required alignment by putting one properly sized object after
1822 * another.
1824 * Notice that the allocation order determines the sizes of the per cpu
1825 * caches. Each processor has always one slab available for allocations.
1826 * Increasing the allocation order reduces the number of times that slabs
1827 * must be moved on and off the partial lists and is therefore a factor in
1828 * locking overhead.
1832 * Mininum / Maximum order of slab pages. This influences locking overhead
1833 * and slab fragmentation. A higher order reduces the number of partial slabs
1834 * and increases the number of allocations possible without having to
1835 * take the list_lock.
1837 static int slub_min_order;
1838 static int slub_max_order = PAGE_ALLOC_COSTLY_ORDER;
1839 static int slub_min_objects;
1842 * Merge control. If this is set then no merging of slab caches will occur.
1843 * (Could be removed. This was introduced to pacify the merge skeptics.)
1845 static int slub_nomerge;
1848 * Calculate the order of allocation given an slab object size.
1850 * The order of allocation has significant impact on performance and other
1851 * system components. Generally order 0 allocations should be preferred since
1852 * order 0 does not cause fragmentation in the page allocator. Larger objects
1853 * be problematic to put into order 0 slabs because there may be too much
1854 * unused space left. We go to a higher order if more than 1/16th of the slab
1855 * would be wasted.
1857 * In order to reach satisfactory performance we must ensure that a minimum
1858 * number of objects is in one slab. Otherwise we may generate too much
1859 * activity on the partial lists which requires taking the list_lock. This is
1860 * less a concern for large slabs though which are rarely used.
1862 * slub_max_order specifies the order where we begin to stop considering the
1863 * number of objects in a slab as critical. If we reach slub_max_order then
1864 * we try to keep the page order as low as possible. So we accept more waste
1865 * of space in favor of a small page order.
1867 * Higher order allocations also allow the placement of more objects in a
1868 * slab and thereby reduce object handling overhead. If the user has
1869 * requested a higher mininum order then we start with that one instead of
1870 * the smallest order which will fit the object.
1872 static inline int slab_order(int size, int min_objects,
1873 int max_order, int fract_leftover)
1875 int order;
1876 int rem;
1877 int min_order = slub_min_order;
1879 if ((PAGE_SIZE << min_order) / size > MAX_OBJS_PER_PAGE)
1880 return get_order(size * MAX_OBJS_PER_PAGE) - 1;
1882 for (order = max(min_order,
1883 fls(min_objects * size - 1) - PAGE_SHIFT);
1884 order <= max_order; order++) {
1886 unsigned long slab_size = PAGE_SIZE << order;
1888 if (slab_size < min_objects * size)
1889 continue;
1891 rem = slab_size % size;
1893 if (rem <= slab_size / fract_leftover)
1894 break;
1898 return order;
1901 static inline int calculate_order(int size)
1903 int order;
1904 int min_objects;
1905 int fraction;
1908 * Attempt to find best configuration for a slab. This
1909 * works by first attempting to generate a layout with
1910 * the best configuration and backing off gradually.
1912 * First we reduce the acceptable waste in a slab. Then
1913 * we reduce the minimum objects required in a slab.
1915 min_objects = slub_min_objects;
1916 if (!min_objects)
1917 min_objects = 4 * (fls(nr_cpu_ids) + 1);
1918 while (min_objects > 1) {
1919 fraction = 16;
1920 while (fraction >= 4) {
1921 order = slab_order(size, min_objects,
1922 slub_max_order, fraction);
1923 if (order <= slub_max_order)
1924 return order;
1925 fraction /= 2;
1927 min_objects /= 2;
1931 * We were unable to place multiple objects in a slab. Now
1932 * lets see if we can place a single object there.
1934 order = slab_order(size, 1, slub_max_order, 1);
1935 if (order <= slub_max_order)
1936 return order;
1939 * Doh this slab cannot be placed using slub_max_order.
1941 order = slab_order(size, 1, MAX_ORDER, 1);
1942 if (order <= MAX_ORDER)
1943 return order;
1944 return -ENOSYS;
1948 * Figure out what the alignment of the objects will be.
1950 static unsigned long calculate_alignment(unsigned long flags,
1951 unsigned long align, unsigned long size)
1954 * If the user wants hardware cache aligned objects then follow that
1955 * suggestion if the object is sufficiently large.
1957 * The hardware cache alignment cannot override the specified
1958 * alignment though. If that is greater then use it.
1960 if (flags & SLAB_HWCACHE_ALIGN) {
1961 unsigned long ralign = cache_line_size();
1962 while (size <= ralign / 2)
1963 ralign /= 2;
1964 align = max(align, ralign);
1967 if (align < ARCH_SLAB_MINALIGN)
1968 align = ARCH_SLAB_MINALIGN;
1970 return ALIGN(align, sizeof(void *));
1973 static void init_kmem_cache_cpu(struct kmem_cache *s,
1974 struct kmem_cache_cpu *c)
1976 c->page = NULL;
1977 c->freelist = NULL;
1978 c->node = 0;
1979 c->offset = s->offset / sizeof(void *);
1980 c->objsize = s->objsize;
1981 #ifdef CONFIG_SLUB_STATS
1982 memset(c->stat, 0, NR_SLUB_STAT_ITEMS * sizeof(unsigned));
1983 #endif
1986 static void
1987 init_kmem_cache_node(struct kmem_cache_node *n, struct kmem_cache *s)
1989 n->nr_partial = 0;
1992 * The larger the object size is, the more pages we want on the partial
1993 * list to avoid pounding the page allocator excessively.
1995 n->min_partial = ilog2(s->size);
1996 if (n->min_partial < MIN_PARTIAL)
1997 n->min_partial = MIN_PARTIAL;
1998 else if (n->min_partial > MAX_PARTIAL)
1999 n->min_partial = MAX_PARTIAL;
2001 spin_lock_init(&n->list_lock);
2002 INIT_LIST_HEAD(&n->partial);
2003 #ifdef CONFIG_SLUB_DEBUG
2004 atomic_long_set(&n->nr_slabs, 0);
2005 atomic_long_set(&n->total_objects, 0);
2006 INIT_LIST_HEAD(&n->full);
2007 #endif
2010 #ifdef CONFIG_SMP
2012 * Per cpu array for per cpu structures.
2014 * The per cpu array places all kmem_cache_cpu structures from one processor
2015 * close together meaning that it becomes possible that multiple per cpu
2016 * structures are contained in one cacheline. This may be particularly
2017 * beneficial for the kmalloc caches.
2019 * A desktop system typically has around 60-80 slabs. With 100 here we are
2020 * likely able to get per cpu structures for all caches from the array defined
2021 * here. We must be able to cover all kmalloc caches during bootstrap.
2023 * If the per cpu array is exhausted then fall back to kmalloc
2024 * of individual cachelines. No sharing is possible then.
2026 #define NR_KMEM_CACHE_CPU 100
2028 static DEFINE_PER_CPU(struct kmem_cache_cpu,
2029 kmem_cache_cpu)[NR_KMEM_CACHE_CPU];
2031 static DEFINE_PER_CPU(struct kmem_cache_cpu *, kmem_cache_cpu_free);
2032 static DECLARE_BITMAP(kmem_cach_cpu_free_init_once, CONFIG_NR_CPUS);
2034 static struct kmem_cache_cpu *alloc_kmem_cache_cpu(struct kmem_cache *s,
2035 int cpu, gfp_t flags)
2037 struct kmem_cache_cpu *c = per_cpu(kmem_cache_cpu_free, cpu);
2039 if (c)
2040 per_cpu(kmem_cache_cpu_free, cpu) =
2041 (void *)c->freelist;
2042 else {
2043 /* Table overflow: So allocate ourselves */
2044 c = kmalloc_node(
2045 ALIGN(sizeof(struct kmem_cache_cpu), cache_line_size()),
2046 flags, cpu_to_node(cpu));
2047 if (!c)
2048 return NULL;
2051 init_kmem_cache_cpu(s, c);
2052 return c;
2055 static void free_kmem_cache_cpu(struct kmem_cache_cpu *c, int cpu)
2057 if (c < per_cpu(kmem_cache_cpu, cpu) ||
2058 c >= per_cpu(kmem_cache_cpu, cpu) + NR_KMEM_CACHE_CPU) {
2059 kfree(c);
2060 return;
2062 c->freelist = (void *)per_cpu(kmem_cache_cpu_free, cpu);
2063 per_cpu(kmem_cache_cpu_free, cpu) = c;
2066 static void free_kmem_cache_cpus(struct kmem_cache *s)
2068 int cpu;
2070 for_each_online_cpu(cpu) {
2071 struct kmem_cache_cpu *c = get_cpu_slab(s, cpu);
2073 if (c) {
2074 s->cpu_slab[cpu] = NULL;
2075 free_kmem_cache_cpu(c, cpu);
2080 static int alloc_kmem_cache_cpus(struct kmem_cache *s, gfp_t flags)
2082 int cpu;
2084 for_each_online_cpu(cpu) {
2085 struct kmem_cache_cpu *c = get_cpu_slab(s, cpu);
2087 if (c)
2088 continue;
2090 c = alloc_kmem_cache_cpu(s, cpu, flags);
2091 if (!c) {
2092 free_kmem_cache_cpus(s);
2093 return 0;
2095 s->cpu_slab[cpu] = c;
2097 return 1;
2101 * Initialize the per cpu array.
2103 static void init_alloc_cpu_cpu(int cpu)
2105 int i;
2107 if (cpumask_test_cpu(cpu, to_cpumask(kmem_cach_cpu_free_init_once)))
2108 return;
2110 for (i = NR_KMEM_CACHE_CPU - 1; i >= 0; i--)
2111 free_kmem_cache_cpu(&per_cpu(kmem_cache_cpu, cpu)[i], cpu);
2113 cpumask_set_cpu(cpu, to_cpumask(kmem_cach_cpu_free_init_once));
2116 static void __init init_alloc_cpu(void)
2118 int cpu;
2120 for_each_online_cpu(cpu)
2121 init_alloc_cpu_cpu(cpu);
2124 #else
2125 static inline void free_kmem_cache_cpus(struct kmem_cache *s) {}
2126 static inline void init_alloc_cpu(void) {}
2128 static inline int alloc_kmem_cache_cpus(struct kmem_cache *s, gfp_t flags)
2130 init_kmem_cache_cpu(s, &s->cpu_slab);
2131 return 1;
2133 #endif
2135 #ifdef CONFIG_NUMA
2137 * No kmalloc_node yet so do it by hand. We know that this is the first
2138 * slab on the node for this slabcache. There are no concurrent accesses
2139 * possible.
2141 * Note that this function only works on the kmalloc_node_cache
2142 * when allocating for the kmalloc_node_cache. This is used for bootstrapping
2143 * memory on a fresh node that has no slab structures yet.
2145 static void early_kmem_cache_node_alloc(gfp_t gfpflags, int node)
2147 struct page *page;
2148 struct kmem_cache_node *n;
2149 unsigned long flags;
2151 BUG_ON(kmalloc_caches->size < sizeof(struct kmem_cache_node));
2153 page = new_slab(kmalloc_caches, gfpflags, node);
2155 BUG_ON(!page);
2156 if (page_to_nid(page) != node) {
2157 printk(KERN_ERR "SLUB: Unable to allocate memory from "
2158 "node %d\n", node);
2159 printk(KERN_ERR "SLUB: Allocating a useless per node structure "
2160 "in order to be able to continue\n");
2163 n = page->freelist;
2164 BUG_ON(!n);
2165 page->freelist = get_freepointer(kmalloc_caches, n);
2166 page->inuse++;
2167 kmalloc_caches->node[node] = n;
2168 #ifdef CONFIG_SLUB_DEBUG
2169 init_object(kmalloc_caches, n, 1);
2170 init_tracking(kmalloc_caches, n);
2171 #endif
2172 init_kmem_cache_node(n, kmalloc_caches);
2173 inc_slabs_node(kmalloc_caches, node, page->objects);
2176 * lockdep requires consistent irq usage for each lock
2177 * so even though there cannot be a race this early in
2178 * the boot sequence, we still disable irqs.
2180 local_irq_save(flags);
2181 add_partial(n, page, 0);
2182 local_irq_restore(flags);
2185 static void free_kmem_cache_nodes(struct kmem_cache *s)
2187 int node;
2189 for_each_node_state(node, N_NORMAL_MEMORY) {
2190 struct kmem_cache_node *n = s->node[node];
2191 if (n && n != &s->local_node)
2192 kmem_cache_free(kmalloc_caches, n);
2193 s->node[node] = NULL;
2197 static int init_kmem_cache_nodes(struct kmem_cache *s, gfp_t gfpflags)
2199 int node;
2200 int local_node;
2202 if (slab_state >= UP)
2203 local_node = page_to_nid(virt_to_page(s));
2204 else
2205 local_node = 0;
2207 for_each_node_state(node, N_NORMAL_MEMORY) {
2208 struct kmem_cache_node *n;
2210 if (local_node == node)
2211 n = &s->local_node;
2212 else {
2213 if (slab_state == DOWN) {
2214 early_kmem_cache_node_alloc(gfpflags, node);
2215 continue;
2217 n = kmem_cache_alloc_node(kmalloc_caches,
2218 gfpflags, node);
2220 if (!n) {
2221 free_kmem_cache_nodes(s);
2222 return 0;
2226 s->node[node] = n;
2227 init_kmem_cache_node(n, s);
2229 return 1;
2231 #else
2232 static void free_kmem_cache_nodes(struct kmem_cache *s)
2236 static int init_kmem_cache_nodes(struct kmem_cache *s, gfp_t gfpflags)
2238 init_kmem_cache_node(&s->local_node, s);
2239 return 1;
2241 #endif
2244 * calculate_sizes() determines the order and the distribution of data within
2245 * a slab object.
2247 static int calculate_sizes(struct kmem_cache *s, int forced_order)
2249 unsigned long flags = s->flags;
2250 unsigned long size = s->objsize;
2251 unsigned long align = s->align;
2252 int order;
2255 * Round up object size to the next word boundary. We can only
2256 * place the free pointer at word boundaries and this determines
2257 * the possible location of the free pointer.
2259 size = ALIGN(size, sizeof(void *));
2261 #ifdef CONFIG_SLUB_DEBUG
2263 * Determine if we can poison the object itself. If the user of
2264 * the slab may touch the object after free or before allocation
2265 * then we should never poison the object itself.
2267 if ((flags & SLAB_POISON) && !(flags & SLAB_DESTROY_BY_RCU) &&
2268 !s->ctor)
2269 s->flags |= __OBJECT_POISON;
2270 else
2271 s->flags &= ~__OBJECT_POISON;
2275 * If we are Redzoning then check if there is some space between the
2276 * end of the object and the free pointer. If not then add an
2277 * additional word to have some bytes to store Redzone information.
2279 if ((flags & SLAB_RED_ZONE) && size == s->objsize)
2280 size += sizeof(void *);
2281 #endif
2284 * With that we have determined the number of bytes in actual use
2285 * by the object. This is the potential offset to the free pointer.
2287 s->inuse = size;
2289 if (((flags & (SLAB_DESTROY_BY_RCU | SLAB_POISON)) ||
2290 s->ctor)) {
2292 * Relocate free pointer after the object if it is not
2293 * permitted to overwrite the first word of the object on
2294 * kmem_cache_free.
2296 * This is the case if we do RCU, have a constructor or
2297 * destructor or are poisoning the objects.
2299 s->offset = size;
2300 size += sizeof(void *);
2303 #ifdef CONFIG_SLUB_DEBUG
2304 if (flags & SLAB_STORE_USER)
2306 * Need to store information about allocs and frees after
2307 * the object.
2309 size += 2 * sizeof(struct track);
2311 if (flags & SLAB_RED_ZONE)
2313 * Add some empty padding so that we can catch
2314 * overwrites from earlier objects rather than let
2315 * tracking information or the free pointer be
2316 * corrupted if a user writes before the start
2317 * of the object.
2319 size += sizeof(void *);
2320 #endif
2323 * Determine the alignment based on various parameters that the
2324 * user specified and the dynamic determination of cache line size
2325 * on bootup.
2327 align = calculate_alignment(flags, align, s->objsize);
2330 * SLUB stores one object immediately after another beginning from
2331 * offset 0. In order to align the objects we have to simply size
2332 * each object to conform to the alignment.
2334 size = ALIGN(size, align);
2335 s->size = size;
2336 if (forced_order >= 0)
2337 order = forced_order;
2338 else
2339 order = calculate_order(size);
2341 if (order < 0)
2342 return 0;
2344 s->allocflags = 0;
2345 if (order)
2346 s->allocflags |= __GFP_COMP;
2348 if (s->flags & SLAB_CACHE_DMA)
2349 s->allocflags |= SLUB_DMA;
2351 if (s->flags & SLAB_RECLAIM_ACCOUNT)
2352 s->allocflags |= __GFP_RECLAIMABLE;
2355 * Determine the number of objects per slab
2357 s->oo = oo_make(order, size);
2358 s->min = oo_make(get_order(size), size);
2359 if (oo_objects(s->oo) > oo_objects(s->max))
2360 s->max = s->oo;
2362 return !!oo_objects(s->oo);
2366 static int kmem_cache_open(struct kmem_cache *s, gfp_t gfpflags,
2367 const char *name, size_t size,
2368 size_t align, unsigned long flags,
2369 void (*ctor)(void *))
2371 memset(s, 0, kmem_size);
2372 s->name = name;
2373 s->ctor = ctor;
2374 s->objsize = size;
2375 s->align = align;
2376 s->flags = kmem_cache_flags(size, flags, name, ctor);
2378 if (!calculate_sizes(s, -1))
2379 goto error;
2381 s->refcount = 1;
2382 #ifdef CONFIG_NUMA
2383 s->remote_node_defrag_ratio = 1000;
2384 #endif
2385 if (!init_kmem_cache_nodes(s, gfpflags & ~SLUB_DMA))
2386 goto error;
2388 if (alloc_kmem_cache_cpus(s, gfpflags & ~SLUB_DMA))
2389 return 1;
2390 free_kmem_cache_nodes(s);
2391 error:
2392 if (flags & SLAB_PANIC)
2393 panic("Cannot create slab %s size=%lu realsize=%u "
2394 "order=%u offset=%u flags=%lx\n",
2395 s->name, (unsigned long)size, s->size, oo_order(s->oo),
2396 s->offset, flags);
2397 return 0;
2401 * Check if a given pointer is valid
2403 int kmem_ptr_validate(struct kmem_cache *s, const void *object)
2405 struct page *page;
2407 page = get_object_page(object);
2409 if (!page || s != page->slab)
2410 /* No slab or wrong slab */
2411 return 0;
2413 if (!check_valid_pointer(s, page, object))
2414 return 0;
2417 * We could also check if the object is on the slabs freelist.
2418 * But this would be too expensive and it seems that the main
2419 * purpose of kmem_ptr_valid() is to check if the object belongs
2420 * to a certain slab.
2422 return 1;
2424 EXPORT_SYMBOL(kmem_ptr_validate);
2427 * Determine the size of a slab object
2429 unsigned int kmem_cache_size(struct kmem_cache *s)
2431 return s->objsize;
2433 EXPORT_SYMBOL(kmem_cache_size);
2435 const char *kmem_cache_name(struct kmem_cache *s)
2437 return s->name;
2439 EXPORT_SYMBOL(kmem_cache_name);
2441 static void list_slab_objects(struct kmem_cache *s, struct page *page,
2442 const char *text)
2444 #ifdef CONFIG_SLUB_DEBUG
2445 void *addr = page_address(page);
2446 void *p;
2447 DECLARE_BITMAP(map, page->objects);
2449 bitmap_zero(map, page->objects);
2450 slab_err(s, page, "%s", text);
2451 slab_lock(page);
2452 for_each_free_object(p, s, page->freelist)
2453 set_bit(slab_index(p, s, addr), map);
2455 for_each_object(p, s, addr, page->objects) {
2457 if (!test_bit(slab_index(p, s, addr), map)) {
2458 printk(KERN_ERR "INFO: Object 0x%p @offset=%tu\n",
2459 p, p - addr);
2460 print_tracking(s, p);
2463 slab_unlock(page);
2464 #endif
2468 * Attempt to free all partial slabs on a node.
2470 static void free_partial(struct kmem_cache *s, struct kmem_cache_node *n)
2472 unsigned long flags;
2473 struct page *page, *h;
2475 spin_lock_irqsave(&n->list_lock, flags);
2476 list_for_each_entry_safe(page, h, &n->partial, lru) {
2477 if (!page->inuse) {
2478 list_del(&page->lru);
2479 discard_slab(s, page);
2480 n->nr_partial--;
2481 } else {
2482 list_slab_objects(s, page,
2483 "Objects remaining on kmem_cache_close()");
2486 spin_unlock_irqrestore(&n->list_lock, flags);
2490 * Release all resources used by a slab cache.
2492 static inline int kmem_cache_close(struct kmem_cache *s)
2494 int node;
2496 flush_all(s);
2498 /* Attempt to free all objects */
2499 free_kmem_cache_cpus(s);
2500 for_each_node_state(node, N_NORMAL_MEMORY) {
2501 struct kmem_cache_node *n = get_node(s, node);
2503 free_partial(s, n);
2504 if (n->nr_partial || slabs_node(s, node))
2505 return 1;
2507 free_kmem_cache_nodes(s);
2508 return 0;
2512 * Close a cache and release the kmem_cache structure
2513 * (must be used for caches created using kmem_cache_create)
2515 void kmem_cache_destroy(struct kmem_cache *s)
2517 down_write(&slub_lock);
2518 s->refcount--;
2519 if (!s->refcount) {
2520 list_del(&s->list);
2521 up_write(&slub_lock);
2522 if (kmem_cache_close(s)) {
2523 printk(KERN_ERR "SLUB %s: %s called for cache that "
2524 "still has objects.\n", s->name, __func__);
2525 dump_stack();
2527 sysfs_slab_remove(s);
2528 } else
2529 up_write(&slub_lock);
2531 EXPORT_SYMBOL(kmem_cache_destroy);
2533 /********************************************************************
2534 * Kmalloc subsystem
2535 *******************************************************************/
2537 struct kmem_cache kmalloc_caches[SLUB_PAGE_SHIFT] __cacheline_aligned;
2538 EXPORT_SYMBOL(kmalloc_caches);
2540 static int __init setup_slub_min_order(char *str)
2542 get_option(&str, &slub_min_order);
2544 return 1;
2547 __setup("slub_min_order=", setup_slub_min_order);
2549 static int __init setup_slub_max_order(char *str)
2551 get_option(&str, &slub_max_order);
2553 return 1;
2556 __setup("slub_max_order=", setup_slub_max_order);
2558 static int __init setup_slub_min_objects(char *str)
2560 get_option(&str, &slub_min_objects);
2562 return 1;
2565 __setup("slub_min_objects=", setup_slub_min_objects);
2567 static int __init setup_slub_nomerge(char *str)
2569 slub_nomerge = 1;
2570 return 1;
2573 __setup("slub_nomerge", setup_slub_nomerge);
2575 static struct kmem_cache *create_kmalloc_cache(struct kmem_cache *s,
2576 const char *name, int size, gfp_t gfp_flags)
2578 unsigned int flags = 0;
2580 if (gfp_flags & SLUB_DMA)
2581 flags = SLAB_CACHE_DMA;
2583 down_write(&slub_lock);
2584 if (!kmem_cache_open(s, gfp_flags, name, size, ARCH_KMALLOC_MINALIGN,
2585 flags, NULL))
2586 goto panic;
2588 list_add(&s->list, &slab_caches);
2589 up_write(&slub_lock);
2590 if (sysfs_slab_add(s))
2591 goto panic;
2592 return s;
2594 panic:
2595 panic("Creation of kmalloc slab %s size=%d failed.\n", name, size);
2598 #ifdef CONFIG_ZONE_DMA
2599 static struct kmem_cache *kmalloc_caches_dma[SLUB_PAGE_SHIFT];
2601 static void sysfs_add_func(struct work_struct *w)
2603 struct kmem_cache *s;
2605 down_write(&slub_lock);
2606 list_for_each_entry(s, &slab_caches, list) {
2607 if (s->flags & __SYSFS_ADD_DEFERRED) {
2608 s->flags &= ~__SYSFS_ADD_DEFERRED;
2609 sysfs_slab_add(s);
2612 up_write(&slub_lock);
2615 static DECLARE_WORK(sysfs_add_work, sysfs_add_func);
2617 static noinline struct kmem_cache *dma_kmalloc_cache(int index, gfp_t flags)
2619 struct kmem_cache *s;
2620 char *text;
2621 size_t realsize;
2623 s = kmalloc_caches_dma[index];
2624 if (s)
2625 return s;
2627 /* Dynamically create dma cache */
2628 if (flags & __GFP_WAIT)
2629 down_write(&slub_lock);
2630 else {
2631 if (!down_write_trylock(&slub_lock))
2632 goto out;
2635 if (kmalloc_caches_dma[index])
2636 goto unlock_out;
2638 realsize = kmalloc_caches[index].objsize;
2639 text = kasprintf(flags & ~SLUB_DMA, "kmalloc_dma-%d",
2640 (unsigned int)realsize);
2641 s = kmalloc(kmem_size, flags & ~SLUB_DMA);
2643 if (!s || !text || !kmem_cache_open(s, flags, text,
2644 realsize, ARCH_KMALLOC_MINALIGN,
2645 SLAB_CACHE_DMA|SLAB_NOTRACK|__SYSFS_ADD_DEFERRED,
2646 NULL)) {
2647 kfree(s);
2648 kfree(text);
2649 goto unlock_out;
2652 list_add(&s->list, &slab_caches);
2653 kmalloc_caches_dma[index] = s;
2655 schedule_work(&sysfs_add_work);
2657 unlock_out:
2658 up_write(&slub_lock);
2659 out:
2660 return kmalloc_caches_dma[index];
2662 #endif
2665 * Conversion table for small slabs sizes / 8 to the index in the
2666 * kmalloc array. This is necessary for slabs < 192 since we have non power
2667 * of two cache sizes there. The size of larger slabs can be determined using
2668 * fls.
2670 static s8 size_index[24] = {
2671 3, /* 8 */
2672 4, /* 16 */
2673 5, /* 24 */
2674 5, /* 32 */
2675 6, /* 40 */
2676 6, /* 48 */
2677 6, /* 56 */
2678 6, /* 64 */
2679 1, /* 72 */
2680 1, /* 80 */
2681 1, /* 88 */
2682 1, /* 96 */
2683 7, /* 104 */
2684 7, /* 112 */
2685 7, /* 120 */
2686 7, /* 128 */
2687 2, /* 136 */
2688 2, /* 144 */
2689 2, /* 152 */
2690 2, /* 160 */
2691 2, /* 168 */
2692 2, /* 176 */
2693 2, /* 184 */
2694 2 /* 192 */
2697 static struct kmem_cache *get_slab(size_t size, gfp_t flags)
2699 int index;
2701 if (size <= 192) {
2702 if (!size)
2703 return ZERO_SIZE_PTR;
2705 index = size_index[(size - 1) / 8];
2706 } else
2707 index = fls(size - 1);
2709 #ifdef CONFIG_ZONE_DMA
2710 if (unlikely((flags & SLUB_DMA)))
2711 return dma_kmalloc_cache(index, flags);
2713 #endif
2714 return &kmalloc_caches[index];
2717 void *__kmalloc(size_t size, gfp_t flags)
2719 struct kmem_cache *s;
2720 void *ret;
2722 if (unlikely(size > SLUB_MAX_SIZE))
2723 return kmalloc_large(size, flags);
2725 s = get_slab(size, flags);
2727 if (unlikely(ZERO_OR_NULL_PTR(s)))
2728 return s;
2730 ret = slab_alloc(s, flags, -1, _RET_IP_);
2732 trace_kmalloc(_RET_IP_, ret, size, s->size, flags);
2734 return ret;
2736 EXPORT_SYMBOL(__kmalloc);
2738 static void *kmalloc_large_node(size_t size, gfp_t flags, int node)
2740 struct page *page;
2742 flags |= __GFP_COMP | __GFP_NOTRACK;
2743 page = alloc_pages_node(node, flags, get_order(size));
2744 if (page)
2745 return page_address(page);
2746 else
2747 return NULL;
2750 #ifdef CONFIG_NUMA
2751 void *__kmalloc_node(size_t size, gfp_t flags, int node)
2753 struct kmem_cache *s;
2754 void *ret;
2756 if (unlikely(size > SLUB_MAX_SIZE)) {
2757 ret = kmalloc_large_node(size, flags, node);
2759 trace_kmalloc_node(_RET_IP_, ret,
2760 size, PAGE_SIZE << get_order(size),
2761 flags, node);
2763 return ret;
2766 s = get_slab(size, flags);
2768 if (unlikely(ZERO_OR_NULL_PTR(s)))
2769 return s;
2771 ret = slab_alloc(s, flags, node, _RET_IP_);
2773 trace_kmalloc_node(_RET_IP_, ret, size, s->size, flags, node);
2775 return ret;
2777 EXPORT_SYMBOL(__kmalloc_node);
2778 #endif
2780 size_t ksize(const void *object)
2782 struct page *page;
2783 struct kmem_cache *s;
2785 if (unlikely(object == ZERO_SIZE_PTR))
2786 return 0;
2788 page = virt_to_head_page(object);
2790 if (unlikely(!PageSlab(page))) {
2791 WARN_ON(!PageCompound(page));
2792 return PAGE_SIZE << compound_order(page);
2794 s = page->slab;
2796 #ifdef CONFIG_SLUB_DEBUG
2798 * Debugging requires use of the padding between object
2799 * and whatever may come after it.
2801 if (s->flags & (SLAB_RED_ZONE | SLAB_POISON))
2802 return s->objsize;
2804 #endif
2806 * If we have the need to store the freelist pointer
2807 * back there or track user information then we can
2808 * only use the space before that information.
2810 if (s->flags & (SLAB_DESTROY_BY_RCU | SLAB_STORE_USER))
2811 return s->inuse;
2813 * Else we can use all the padding etc for the allocation
2815 return s->size;
2817 EXPORT_SYMBOL(ksize);
2819 void kfree(const void *x)
2821 struct page *page;
2822 void *object = (void *)x;
2824 trace_kfree(_RET_IP_, x);
2826 if (unlikely(ZERO_OR_NULL_PTR(x)))
2827 return;
2829 page = virt_to_head_page(x);
2830 if (unlikely(!PageSlab(page))) {
2831 BUG_ON(!PageCompound(page));
2832 put_page(page);
2833 return;
2835 slab_free(page->slab, page, object, _RET_IP_);
2837 EXPORT_SYMBOL(kfree);
2840 * kmem_cache_shrink removes empty slabs from the partial lists and sorts
2841 * the remaining slabs by the number of items in use. The slabs with the
2842 * most items in use come first. New allocations will then fill those up
2843 * and thus they can be removed from the partial lists.
2845 * The slabs with the least items are placed last. This results in them
2846 * being allocated from last increasing the chance that the last objects
2847 * are freed in them.
2849 int kmem_cache_shrink(struct kmem_cache *s)
2851 int node;
2852 int i;
2853 struct kmem_cache_node *n;
2854 struct page *page;
2855 struct page *t;
2856 int objects = oo_objects(s->max);
2857 struct list_head *slabs_by_inuse =
2858 kmalloc(sizeof(struct list_head) * objects, GFP_KERNEL);
2859 unsigned long flags;
2861 if (!slabs_by_inuse)
2862 return -ENOMEM;
2864 flush_all(s);
2865 for_each_node_state(node, N_NORMAL_MEMORY) {
2866 n = get_node(s, node);
2868 if (!n->nr_partial)
2869 continue;
2871 for (i = 0; i < objects; i++)
2872 INIT_LIST_HEAD(slabs_by_inuse + i);
2874 spin_lock_irqsave(&n->list_lock, flags);
2877 * Build lists indexed by the items in use in each slab.
2879 * Note that concurrent frees may occur while we hold the
2880 * list_lock. page->inuse here is the upper limit.
2882 list_for_each_entry_safe(page, t, &n->partial, lru) {
2883 if (!page->inuse && slab_trylock(page)) {
2885 * Must hold slab lock here because slab_free
2886 * may have freed the last object and be
2887 * waiting to release the slab.
2889 list_del(&page->lru);
2890 n->nr_partial--;
2891 slab_unlock(page);
2892 discard_slab(s, page);
2893 } else {
2894 list_move(&page->lru,
2895 slabs_by_inuse + page->inuse);
2900 * Rebuild the partial list with the slabs filled up most
2901 * first and the least used slabs at the end.
2903 for (i = objects - 1; i >= 0; i--)
2904 list_splice(slabs_by_inuse + i, n->partial.prev);
2906 spin_unlock_irqrestore(&n->list_lock, flags);
2909 kfree(slabs_by_inuse);
2910 return 0;
2912 EXPORT_SYMBOL(kmem_cache_shrink);
2914 #if defined(CONFIG_NUMA) && defined(CONFIG_MEMORY_HOTPLUG)
2915 static int slab_mem_going_offline_callback(void *arg)
2917 struct kmem_cache *s;
2919 down_read(&slub_lock);
2920 list_for_each_entry(s, &slab_caches, list)
2921 kmem_cache_shrink(s);
2922 up_read(&slub_lock);
2924 return 0;
2927 static void slab_mem_offline_callback(void *arg)
2929 struct kmem_cache_node *n;
2930 struct kmem_cache *s;
2931 struct memory_notify *marg = arg;
2932 int offline_node;
2934 offline_node = marg->status_change_nid;
2937 * If the node still has available memory. we need kmem_cache_node
2938 * for it yet.
2940 if (offline_node < 0)
2941 return;
2943 down_read(&slub_lock);
2944 list_for_each_entry(s, &slab_caches, list) {
2945 n = get_node(s, offline_node);
2946 if (n) {
2948 * if n->nr_slabs > 0, slabs still exist on the node
2949 * that is going down. We were unable to free them,
2950 * and offline_pages() function shoudn't call this
2951 * callback. So, we must fail.
2953 BUG_ON(slabs_node(s, offline_node));
2955 s->node[offline_node] = NULL;
2956 kmem_cache_free(kmalloc_caches, n);
2959 up_read(&slub_lock);
2962 static int slab_mem_going_online_callback(void *arg)
2964 struct kmem_cache_node *n;
2965 struct kmem_cache *s;
2966 struct memory_notify *marg = arg;
2967 int nid = marg->status_change_nid;
2968 int ret = 0;
2971 * If the node's memory is already available, then kmem_cache_node is
2972 * already created. Nothing to do.
2974 if (nid < 0)
2975 return 0;
2978 * We are bringing a node online. No memory is available yet. We must
2979 * allocate a kmem_cache_node structure in order to bring the node
2980 * online.
2982 down_read(&slub_lock);
2983 list_for_each_entry(s, &slab_caches, list) {
2985 * XXX: kmem_cache_alloc_node will fallback to other nodes
2986 * since memory is not yet available from the node that
2987 * is brought up.
2989 n = kmem_cache_alloc(kmalloc_caches, GFP_KERNEL);
2990 if (!n) {
2991 ret = -ENOMEM;
2992 goto out;
2994 init_kmem_cache_node(n, s);
2995 s->node[nid] = n;
2997 out:
2998 up_read(&slub_lock);
2999 return ret;
3002 static int slab_memory_callback(struct notifier_block *self,
3003 unsigned long action, void *arg)
3005 int ret = 0;
3007 switch (action) {
3008 case MEM_GOING_ONLINE:
3009 ret = slab_mem_going_online_callback(arg);
3010 break;
3011 case MEM_GOING_OFFLINE:
3012 ret = slab_mem_going_offline_callback(arg);
3013 break;
3014 case MEM_OFFLINE:
3015 case MEM_CANCEL_ONLINE:
3016 slab_mem_offline_callback(arg);
3017 break;
3018 case MEM_ONLINE:
3019 case MEM_CANCEL_OFFLINE:
3020 break;
3022 if (ret)
3023 ret = notifier_from_errno(ret);
3024 else
3025 ret = NOTIFY_OK;
3026 return ret;
3029 #endif /* CONFIG_MEMORY_HOTPLUG */
3031 /********************************************************************
3032 * Basic setup of slabs
3033 *******************************************************************/
3035 void __init kmem_cache_init(void)
3037 int i;
3038 int caches = 0;
3040 init_alloc_cpu();
3042 #ifdef CONFIG_NUMA
3044 * Must first have the slab cache available for the allocations of the
3045 * struct kmem_cache_node's. There is special bootstrap code in
3046 * kmem_cache_open for slab_state == DOWN.
3048 create_kmalloc_cache(&kmalloc_caches[0], "kmem_cache_node",
3049 sizeof(struct kmem_cache_node), GFP_KERNEL);
3050 kmalloc_caches[0].refcount = -1;
3051 caches++;
3053 hotplug_memory_notifier(slab_memory_callback, SLAB_CALLBACK_PRI);
3054 #endif
3056 /* Able to allocate the per node structures */
3057 slab_state = PARTIAL;
3059 /* Caches that are not of the two-to-the-power-of size */
3060 if (KMALLOC_MIN_SIZE <= 64) {
3061 create_kmalloc_cache(&kmalloc_caches[1],
3062 "kmalloc-96", 96, GFP_KERNEL);
3063 caches++;
3064 create_kmalloc_cache(&kmalloc_caches[2],
3065 "kmalloc-192", 192, GFP_KERNEL);
3066 caches++;
3069 for (i = KMALLOC_SHIFT_LOW; i < SLUB_PAGE_SHIFT; i++) {
3070 create_kmalloc_cache(&kmalloc_caches[i],
3071 "kmalloc", 1 << i, GFP_KERNEL);
3072 caches++;
3077 * Patch up the size_index table if we have strange large alignment
3078 * requirements for the kmalloc array. This is only the case for
3079 * MIPS it seems. The standard arches will not generate any code here.
3081 * Largest permitted alignment is 256 bytes due to the way we
3082 * handle the index determination for the smaller caches.
3084 * Make sure that nothing crazy happens if someone starts tinkering
3085 * around with ARCH_KMALLOC_MINALIGN
3087 BUILD_BUG_ON(KMALLOC_MIN_SIZE > 256 ||
3088 (KMALLOC_MIN_SIZE & (KMALLOC_MIN_SIZE - 1)));
3090 for (i = 8; i < KMALLOC_MIN_SIZE; i += 8)
3091 size_index[(i - 1) / 8] = KMALLOC_SHIFT_LOW;
3093 if (KMALLOC_MIN_SIZE == 128) {
3095 * The 192 byte sized cache is not used if the alignment
3096 * is 128 byte. Redirect kmalloc to use the 256 byte cache
3097 * instead.
3099 for (i = 128 + 8; i <= 192; i += 8)
3100 size_index[(i - 1) / 8] = 8;
3103 slab_state = UP;
3105 /* Provide the correct kmalloc names now that the caches are up */
3106 for (i = KMALLOC_SHIFT_LOW; i < SLUB_PAGE_SHIFT; i++)
3107 kmalloc_caches[i]. name =
3108 kasprintf(GFP_KERNEL, "kmalloc-%d", 1 << i);
3110 #ifdef CONFIG_SMP
3111 register_cpu_notifier(&slab_notifier);
3112 kmem_size = offsetof(struct kmem_cache, cpu_slab) +
3113 nr_cpu_ids * sizeof(struct kmem_cache_cpu *);
3114 #else
3115 kmem_size = sizeof(struct kmem_cache);
3116 #endif
3118 printk(KERN_INFO
3119 "SLUB: Genslabs=%d, HWalign=%d, Order=%d-%d, MinObjects=%d,"
3120 " CPUs=%d, Nodes=%d\n",
3121 caches, cache_line_size(),
3122 slub_min_order, slub_max_order, slub_min_objects,
3123 nr_cpu_ids, nr_node_ids);
3127 * Find a mergeable slab cache
3129 static int slab_unmergeable(struct kmem_cache *s)
3131 if (slub_nomerge || (s->flags & SLUB_NEVER_MERGE))
3132 return 1;
3134 if (s->ctor)
3135 return 1;
3138 * We may have set a slab to be unmergeable during bootstrap.
3140 if (s->refcount < 0)
3141 return 1;
3143 return 0;
3146 static struct kmem_cache *find_mergeable(size_t size,
3147 size_t align, unsigned long flags, const char *name,
3148 void (*ctor)(void *))
3150 struct kmem_cache *s;
3152 if (slub_nomerge || (flags & SLUB_NEVER_MERGE))
3153 return NULL;
3155 if (ctor)
3156 return NULL;
3158 size = ALIGN(size, sizeof(void *));
3159 align = calculate_alignment(flags, align, size);
3160 size = ALIGN(size, align);
3161 flags = kmem_cache_flags(size, flags, name, NULL);
3163 list_for_each_entry(s, &slab_caches, list) {
3164 if (slab_unmergeable(s))
3165 continue;
3167 if (size > s->size)
3168 continue;
3170 if ((flags & SLUB_MERGE_SAME) != (s->flags & SLUB_MERGE_SAME))
3171 continue;
3173 * Check if alignment is compatible.
3174 * Courtesy of Adrian Drzewiecki
3176 if ((s->size & ~(align - 1)) != s->size)
3177 continue;
3179 if (s->size - size >= sizeof(void *))
3180 continue;
3182 return s;
3184 return NULL;
3187 struct kmem_cache *kmem_cache_create(const char *name, size_t size,
3188 size_t align, unsigned long flags, void (*ctor)(void *))
3190 struct kmem_cache *s;
3192 down_write(&slub_lock);
3193 s = find_mergeable(size, align, flags, name, ctor);
3194 if (s) {
3195 int cpu;
3197 s->refcount++;
3199 * Adjust the object sizes so that we clear
3200 * the complete object on kzalloc.
3202 s->objsize = max(s->objsize, (int)size);
3205 * And then we need to update the object size in the
3206 * per cpu structures
3208 for_each_online_cpu(cpu)
3209 get_cpu_slab(s, cpu)->objsize = s->objsize;
3211 s->inuse = max_t(int, s->inuse, ALIGN(size, sizeof(void *)));
3212 up_write(&slub_lock);
3214 if (sysfs_slab_alias(s, name)) {
3215 down_write(&slub_lock);
3216 s->refcount--;
3217 up_write(&slub_lock);
3218 goto err;
3220 return s;
3223 s = kmalloc(kmem_size, GFP_KERNEL);
3224 if (s) {
3225 if (kmem_cache_open(s, GFP_KERNEL, name,
3226 size, align, flags, ctor)) {
3227 list_add(&s->list, &slab_caches);
3228 up_write(&slub_lock);
3229 if (sysfs_slab_add(s)) {
3230 down_write(&slub_lock);
3231 list_del(&s->list);
3232 up_write(&slub_lock);
3233 kfree(s);
3234 goto err;
3236 return s;
3238 kfree(s);
3240 up_write(&slub_lock);
3242 err:
3243 if (flags & SLAB_PANIC)
3244 panic("Cannot create slabcache %s\n", name);
3245 else
3246 s = NULL;
3247 return s;
3249 EXPORT_SYMBOL(kmem_cache_create);
3251 #ifdef CONFIG_SMP
3253 * Use the cpu notifier to insure that the cpu slabs are flushed when
3254 * necessary.
3256 static int __cpuinit slab_cpuup_callback(struct notifier_block *nfb,
3257 unsigned long action, void *hcpu)
3259 long cpu = (long)hcpu;
3260 struct kmem_cache *s;
3261 unsigned long flags;
3263 switch (action) {
3264 case CPU_UP_PREPARE:
3265 case CPU_UP_PREPARE_FROZEN:
3266 init_alloc_cpu_cpu(cpu);
3267 down_read(&slub_lock);
3268 list_for_each_entry(s, &slab_caches, list)
3269 s->cpu_slab[cpu] = alloc_kmem_cache_cpu(s, cpu,
3270 GFP_KERNEL);
3271 up_read(&slub_lock);
3272 break;
3274 case CPU_UP_CANCELED:
3275 case CPU_UP_CANCELED_FROZEN:
3276 case CPU_DEAD:
3277 case CPU_DEAD_FROZEN:
3278 down_read(&slub_lock);
3279 list_for_each_entry(s, &slab_caches, list) {
3280 struct kmem_cache_cpu *c = get_cpu_slab(s, cpu);
3282 local_irq_save(flags);
3283 __flush_cpu_slab(s, cpu);
3284 local_irq_restore(flags);
3285 free_kmem_cache_cpu(c, cpu);
3286 s->cpu_slab[cpu] = NULL;
3288 up_read(&slub_lock);
3289 break;
3290 default:
3291 break;
3293 return NOTIFY_OK;
3296 static struct notifier_block __cpuinitdata slab_notifier = {
3297 .notifier_call = slab_cpuup_callback
3300 #endif
3302 void *__kmalloc_track_caller(size_t size, gfp_t gfpflags, unsigned long caller)
3304 struct kmem_cache *s;
3305 void *ret;
3307 if (unlikely(size > SLUB_MAX_SIZE))
3308 return kmalloc_large(size, gfpflags);
3310 s = get_slab(size, gfpflags);
3312 if (unlikely(ZERO_OR_NULL_PTR(s)))
3313 return s;
3315 ret = slab_alloc(s, gfpflags, -1, caller);
3317 /* Honor the call site pointer we recieved. */
3318 trace_kmalloc(caller, ret, size, s->size, gfpflags);
3320 return ret;
3323 void *__kmalloc_node_track_caller(size_t size, gfp_t gfpflags,
3324 int node, unsigned long caller)
3326 struct kmem_cache *s;
3327 void *ret;
3329 if (unlikely(size > SLUB_MAX_SIZE))
3330 return kmalloc_large_node(size, gfpflags, node);
3332 s = get_slab(size, gfpflags);
3334 if (unlikely(ZERO_OR_NULL_PTR(s)))
3335 return s;
3337 ret = slab_alloc(s, gfpflags, node, caller);
3339 /* Honor the call site pointer we recieved. */
3340 trace_kmalloc_node(caller, ret, size, s->size, gfpflags, node);
3342 return ret;
3345 #ifdef CONFIG_SLUB_DEBUG
3346 static unsigned long count_partial(struct kmem_cache_node *n,
3347 int (*get_count)(struct page *))
3349 unsigned long flags;
3350 unsigned long x = 0;
3351 struct page *page;
3353 spin_lock_irqsave(&n->list_lock, flags);
3354 list_for_each_entry(page, &n->partial, lru)
3355 x += get_count(page);
3356 spin_unlock_irqrestore(&n->list_lock, flags);
3357 return x;
3360 static int count_inuse(struct page *page)
3362 return page->inuse;
3365 static int count_total(struct page *page)
3367 return page->objects;
3370 static int count_free(struct page *page)
3372 return page->objects - page->inuse;
3375 static int validate_slab(struct kmem_cache *s, struct page *page,
3376 unsigned long *map)
3378 void *p;
3379 void *addr = page_address(page);
3381 if (!check_slab(s, page) ||
3382 !on_freelist(s, page, NULL))
3383 return 0;
3385 /* Now we know that a valid freelist exists */
3386 bitmap_zero(map, page->objects);
3388 for_each_free_object(p, s, page->freelist) {
3389 set_bit(slab_index(p, s, addr), map);
3390 if (!check_object(s, page, p, 0))
3391 return 0;
3394 for_each_object(p, s, addr, page->objects)
3395 if (!test_bit(slab_index(p, s, addr), map))
3396 if (!check_object(s, page, p, 1))
3397 return 0;
3398 return 1;
3401 static void validate_slab_slab(struct kmem_cache *s, struct page *page,
3402 unsigned long *map)
3404 if (slab_trylock(page)) {
3405 validate_slab(s, page, map);
3406 slab_unlock(page);
3407 } else
3408 printk(KERN_INFO "SLUB %s: Skipped busy slab 0x%p\n",
3409 s->name, page);
3411 if (s->flags & DEBUG_DEFAULT_FLAGS) {
3412 if (!PageSlubDebug(page))
3413 printk(KERN_ERR "SLUB %s: SlubDebug not set "
3414 "on slab 0x%p\n", s->name, page);
3415 } else {
3416 if (PageSlubDebug(page))
3417 printk(KERN_ERR "SLUB %s: SlubDebug set on "
3418 "slab 0x%p\n", s->name, page);
3422 static int validate_slab_node(struct kmem_cache *s,
3423 struct kmem_cache_node *n, unsigned long *map)
3425 unsigned long count = 0;
3426 struct page *page;
3427 unsigned long flags;
3429 spin_lock_irqsave(&n->list_lock, flags);
3431 list_for_each_entry(page, &n->partial, lru) {
3432 validate_slab_slab(s, page, map);
3433 count++;
3435 if (count != n->nr_partial)
3436 printk(KERN_ERR "SLUB %s: %ld partial slabs counted but "
3437 "counter=%ld\n", s->name, count, n->nr_partial);
3439 if (!(s->flags & SLAB_STORE_USER))
3440 goto out;
3442 list_for_each_entry(page, &n->full, lru) {
3443 validate_slab_slab(s, page, map);
3444 count++;
3446 if (count != atomic_long_read(&n->nr_slabs))
3447 printk(KERN_ERR "SLUB: %s %ld slabs counted but "
3448 "counter=%ld\n", s->name, count,
3449 atomic_long_read(&n->nr_slabs));
3451 out:
3452 spin_unlock_irqrestore(&n->list_lock, flags);
3453 return count;
3456 static long validate_slab_cache(struct kmem_cache *s)
3458 int node;
3459 unsigned long count = 0;
3460 unsigned long *map = kmalloc(BITS_TO_LONGS(oo_objects(s->max)) *
3461 sizeof(unsigned long), GFP_KERNEL);
3463 if (!map)
3464 return -ENOMEM;
3466 flush_all(s);
3467 for_each_node_state(node, N_NORMAL_MEMORY) {
3468 struct kmem_cache_node *n = get_node(s, node);
3470 count += validate_slab_node(s, n, map);
3472 kfree(map);
3473 return count;
3476 #ifdef SLUB_RESILIENCY_TEST
3477 static void resiliency_test(void)
3479 u8 *p;
3481 printk(KERN_ERR "SLUB resiliency testing\n");
3482 printk(KERN_ERR "-----------------------\n");
3483 printk(KERN_ERR "A. Corruption after allocation\n");
3485 p = kzalloc(16, GFP_KERNEL);
3486 p[16] = 0x12;
3487 printk(KERN_ERR "\n1. kmalloc-16: Clobber Redzone/next pointer"
3488 " 0x12->0x%p\n\n", p + 16);
3490 validate_slab_cache(kmalloc_caches + 4);
3492 /* Hmmm... The next two are dangerous */
3493 p = kzalloc(32, GFP_KERNEL);
3494 p[32 + sizeof(void *)] = 0x34;
3495 printk(KERN_ERR "\n2. kmalloc-32: Clobber next pointer/next slab"
3496 " 0x34 -> -0x%p\n", p);
3497 printk(KERN_ERR
3498 "If allocated object is overwritten then not detectable\n\n");
3500 validate_slab_cache(kmalloc_caches + 5);
3501 p = kzalloc(64, GFP_KERNEL);
3502 p += 64 + (get_cycles() & 0xff) * sizeof(void *);
3503 *p = 0x56;
3504 printk(KERN_ERR "\n3. kmalloc-64: corrupting random byte 0x56->0x%p\n",
3506 printk(KERN_ERR
3507 "If allocated object is overwritten then not detectable\n\n");
3508 validate_slab_cache(kmalloc_caches + 6);
3510 printk(KERN_ERR "\nB. Corruption after free\n");
3511 p = kzalloc(128, GFP_KERNEL);
3512 kfree(p);
3513 *p = 0x78;
3514 printk(KERN_ERR "1. kmalloc-128: Clobber first word 0x78->0x%p\n\n", p);
3515 validate_slab_cache(kmalloc_caches + 7);
3517 p = kzalloc(256, GFP_KERNEL);
3518 kfree(p);
3519 p[50] = 0x9a;
3520 printk(KERN_ERR "\n2. kmalloc-256: Clobber 50th byte 0x9a->0x%p\n\n",
3522 validate_slab_cache(kmalloc_caches + 8);
3524 p = kzalloc(512, GFP_KERNEL);
3525 kfree(p);
3526 p[512] = 0xab;
3527 printk(KERN_ERR "\n3. kmalloc-512: Clobber redzone 0xab->0x%p\n\n", p);
3528 validate_slab_cache(kmalloc_caches + 9);
3530 #else
3531 static void resiliency_test(void) {};
3532 #endif
3535 * Generate lists of code addresses where slabcache objects are allocated
3536 * and freed.
3539 struct location {
3540 unsigned long count;
3541 unsigned long addr;
3542 long long sum_time;
3543 long min_time;
3544 long max_time;
3545 long min_pid;
3546 long max_pid;
3547 DECLARE_BITMAP(cpus, NR_CPUS);
3548 nodemask_t nodes;
3551 struct loc_track {
3552 unsigned long max;
3553 unsigned long count;
3554 struct location *loc;
3557 static void free_loc_track(struct loc_track *t)
3559 if (t->max)
3560 free_pages((unsigned long)t->loc,
3561 get_order(sizeof(struct location) * t->max));
3564 static int alloc_loc_track(struct loc_track *t, unsigned long max, gfp_t flags)
3566 struct location *l;
3567 int order;
3569 order = get_order(sizeof(struct location) * max);
3571 l = (void *)__get_free_pages(flags, order);
3572 if (!l)
3573 return 0;
3575 if (t->count) {
3576 memcpy(l, t->loc, sizeof(struct location) * t->count);
3577 free_loc_track(t);
3579 t->max = max;
3580 t->loc = l;
3581 return 1;
3584 static int add_location(struct loc_track *t, struct kmem_cache *s,
3585 const struct track *track)
3587 long start, end, pos;
3588 struct location *l;
3589 unsigned long caddr;
3590 unsigned long age = jiffies - track->when;
3592 start = -1;
3593 end = t->count;
3595 for ( ; ; ) {
3596 pos = start + (end - start + 1) / 2;
3599 * There is nothing at "end". If we end up there
3600 * we need to add something to before end.
3602 if (pos == end)
3603 break;
3605 caddr = t->loc[pos].addr;
3606 if (track->addr == caddr) {
3608 l = &t->loc[pos];
3609 l->count++;
3610 if (track->when) {
3611 l->sum_time += age;
3612 if (age < l->min_time)
3613 l->min_time = age;
3614 if (age > l->max_time)
3615 l->max_time = age;
3617 if (track->pid < l->min_pid)
3618 l->min_pid = track->pid;
3619 if (track->pid > l->max_pid)
3620 l->max_pid = track->pid;
3622 cpumask_set_cpu(track->cpu,
3623 to_cpumask(l->cpus));
3625 node_set(page_to_nid(virt_to_page(track)), l->nodes);
3626 return 1;
3629 if (track->addr < caddr)
3630 end = pos;
3631 else
3632 start = pos;
3636 * Not found. Insert new tracking element.
3638 if (t->count >= t->max && !alloc_loc_track(t, 2 * t->max, GFP_ATOMIC))
3639 return 0;
3641 l = t->loc + pos;
3642 if (pos < t->count)
3643 memmove(l + 1, l,
3644 (t->count - pos) * sizeof(struct location));
3645 t->count++;
3646 l->count = 1;
3647 l->addr = track->addr;
3648 l->sum_time = age;
3649 l->min_time = age;
3650 l->max_time = age;
3651 l->min_pid = track->pid;
3652 l->max_pid = track->pid;
3653 cpumask_clear(to_cpumask(l->cpus));
3654 cpumask_set_cpu(track->cpu, to_cpumask(l->cpus));
3655 nodes_clear(l->nodes);
3656 node_set(page_to_nid(virt_to_page(track)), l->nodes);
3657 return 1;
3660 static void process_slab(struct loc_track *t, struct kmem_cache *s,
3661 struct page *page, enum track_item alloc)
3663 void *addr = page_address(page);
3664 DECLARE_BITMAP(map, page->objects);
3665 void *p;
3667 bitmap_zero(map, page->objects);
3668 for_each_free_object(p, s, page->freelist)
3669 set_bit(slab_index(p, s, addr), map);
3671 for_each_object(p, s, addr, page->objects)
3672 if (!test_bit(slab_index(p, s, addr), map))
3673 add_location(t, s, get_track(s, p, alloc));
3676 static int list_locations(struct kmem_cache *s, char *buf,
3677 enum track_item alloc)
3679 int len = 0;
3680 unsigned long i;
3681 struct loc_track t = { 0, 0, NULL };
3682 int node;
3684 if (!alloc_loc_track(&t, PAGE_SIZE / sizeof(struct location),
3685 GFP_TEMPORARY))
3686 return sprintf(buf, "Out of memory\n");
3688 /* Push back cpu slabs */
3689 flush_all(s);
3691 for_each_node_state(node, N_NORMAL_MEMORY) {
3692 struct kmem_cache_node *n = get_node(s, node);
3693 unsigned long flags;
3694 struct page *page;
3696 if (!atomic_long_read(&n->nr_slabs))
3697 continue;
3699 spin_lock_irqsave(&n->list_lock, flags);
3700 list_for_each_entry(page, &n->partial, lru)
3701 process_slab(&t, s, page, alloc);
3702 list_for_each_entry(page, &n->full, lru)
3703 process_slab(&t, s, page, alloc);
3704 spin_unlock_irqrestore(&n->list_lock, flags);
3707 for (i = 0; i < t.count; i++) {
3708 struct location *l = &t.loc[i];
3710 if (len > PAGE_SIZE - KSYM_SYMBOL_LEN - 100)
3711 break;
3712 len += sprintf(buf + len, "%7ld ", l->count);
3714 if (l->addr)
3715 len += sprint_symbol(buf + len, (unsigned long)l->addr);
3716 else
3717 len += sprintf(buf + len, "<not-available>");
3719 if (l->sum_time != l->min_time) {
3720 len += sprintf(buf + len, " age=%ld/%ld/%ld",
3721 l->min_time,
3722 (long)div_u64(l->sum_time, l->count),
3723 l->max_time);
3724 } else
3725 len += sprintf(buf + len, " age=%ld",
3726 l->min_time);
3728 if (l->min_pid != l->max_pid)
3729 len += sprintf(buf + len, " pid=%ld-%ld",
3730 l->min_pid, l->max_pid);
3731 else
3732 len += sprintf(buf + len, " pid=%ld",
3733 l->min_pid);
3735 if (num_online_cpus() > 1 &&
3736 !cpumask_empty(to_cpumask(l->cpus)) &&
3737 len < PAGE_SIZE - 60) {
3738 len += sprintf(buf + len, " cpus=");
3739 len += cpulist_scnprintf(buf + len, PAGE_SIZE - len - 50,
3740 to_cpumask(l->cpus));
3743 if (num_online_nodes() > 1 && !nodes_empty(l->nodes) &&
3744 len < PAGE_SIZE - 60) {
3745 len += sprintf(buf + len, " nodes=");
3746 len += nodelist_scnprintf(buf + len, PAGE_SIZE - len - 50,
3747 l->nodes);
3750 len += sprintf(buf + len, "\n");
3753 free_loc_track(&t);
3754 if (!t.count)
3755 len += sprintf(buf, "No data\n");
3756 return len;
3759 enum slab_stat_type {
3760 SL_ALL, /* All slabs */
3761 SL_PARTIAL, /* Only partially allocated slabs */
3762 SL_CPU, /* Only slabs used for cpu caches */
3763 SL_OBJECTS, /* Determine allocated objects not slabs */
3764 SL_TOTAL /* Determine object capacity not slabs */
3767 #define SO_ALL (1 << SL_ALL)
3768 #define SO_PARTIAL (1 << SL_PARTIAL)
3769 #define SO_CPU (1 << SL_CPU)
3770 #define SO_OBJECTS (1 << SL_OBJECTS)
3771 #define SO_TOTAL (1 << SL_TOTAL)
3773 static ssize_t show_slab_objects(struct kmem_cache *s,
3774 char *buf, unsigned long flags)
3776 unsigned long total = 0;
3777 int node;
3778 int x;
3779 unsigned long *nodes;
3780 unsigned long *per_cpu;
3782 nodes = kzalloc(2 * sizeof(unsigned long) * nr_node_ids, GFP_KERNEL);
3783 if (!nodes)
3784 return -ENOMEM;
3785 per_cpu = nodes + nr_node_ids;
3787 if (flags & SO_CPU) {
3788 int cpu;
3790 for_each_possible_cpu(cpu) {
3791 struct kmem_cache_cpu *c = get_cpu_slab(s, cpu);
3793 if (!c || c->node < 0)
3794 continue;
3796 if (c->page) {
3797 if (flags & SO_TOTAL)
3798 x = c->page->objects;
3799 else if (flags & SO_OBJECTS)
3800 x = c->page->inuse;
3801 else
3802 x = 1;
3804 total += x;
3805 nodes[c->node] += x;
3807 per_cpu[c->node]++;
3811 if (flags & SO_ALL) {
3812 for_each_node_state(node, N_NORMAL_MEMORY) {
3813 struct kmem_cache_node *n = get_node(s, node);
3815 if (flags & SO_TOTAL)
3816 x = atomic_long_read(&n->total_objects);
3817 else if (flags & SO_OBJECTS)
3818 x = atomic_long_read(&n->total_objects) -
3819 count_partial(n, count_free);
3821 else
3822 x = atomic_long_read(&n->nr_slabs);
3823 total += x;
3824 nodes[node] += x;
3827 } else if (flags & SO_PARTIAL) {
3828 for_each_node_state(node, N_NORMAL_MEMORY) {
3829 struct kmem_cache_node *n = get_node(s, node);
3831 if (flags & SO_TOTAL)
3832 x = count_partial(n, count_total);
3833 else if (flags & SO_OBJECTS)
3834 x = count_partial(n, count_inuse);
3835 else
3836 x = n->nr_partial;
3837 total += x;
3838 nodes[node] += x;
3841 x = sprintf(buf, "%lu", total);
3842 #ifdef CONFIG_NUMA
3843 for_each_node_state(node, N_NORMAL_MEMORY)
3844 if (nodes[node])
3845 x += sprintf(buf + x, " N%d=%lu",
3846 node, nodes[node]);
3847 #endif
3848 kfree(nodes);
3849 return x + sprintf(buf + x, "\n");
3852 static int any_slab_objects(struct kmem_cache *s)
3854 int node;
3856 for_each_online_node(node) {
3857 struct kmem_cache_node *n = get_node(s, node);
3859 if (!n)
3860 continue;
3862 if (atomic_long_read(&n->total_objects))
3863 return 1;
3865 return 0;
3868 #define to_slab_attr(n) container_of(n, struct slab_attribute, attr)
3869 #define to_slab(n) container_of(n, struct kmem_cache, kobj);
3871 struct slab_attribute {
3872 struct attribute attr;
3873 ssize_t (*show)(struct kmem_cache *s, char *buf);
3874 ssize_t (*store)(struct kmem_cache *s, const char *x, size_t count);
3877 #define SLAB_ATTR_RO(_name) \
3878 static struct slab_attribute _name##_attr = __ATTR_RO(_name)
3880 #define SLAB_ATTR(_name) \
3881 static struct slab_attribute _name##_attr = \
3882 __ATTR(_name, 0644, _name##_show, _name##_store)
3884 static ssize_t slab_size_show(struct kmem_cache *s, char *buf)
3886 return sprintf(buf, "%d\n", s->size);
3888 SLAB_ATTR_RO(slab_size);
3890 static ssize_t align_show(struct kmem_cache *s, char *buf)
3892 return sprintf(buf, "%d\n", s->align);
3894 SLAB_ATTR_RO(align);
3896 static ssize_t object_size_show(struct kmem_cache *s, char *buf)
3898 return sprintf(buf, "%d\n", s->objsize);
3900 SLAB_ATTR_RO(object_size);
3902 static ssize_t objs_per_slab_show(struct kmem_cache *s, char *buf)
3904 return sprintf(buf, "%d\n", oo_objects(s->oo));
3906 SLAB_ATTR_RO(objs_per_slab);
3908 static ssize_t order_store(struct kmem_cache *s,
3909 const char *buf, size_t length)
3911 unsigned long order;
3912 int err;
3914 err = strict_strtoul(buf, 10, &order);
3915 if (err)
3916 return err;
3918 if (order > slub_max_order || order < slub_min_order)
3919 return -EINVAL;
3921 calculate_sizes(s, order);
3922 return length;
3925 static ssize_t order_show(struct kmem_cache *s, char *buf)
3927 return sprintf(buf, "%d\n", oo_order(s->oo));
3929 SLAB_ATTR(order);
3931 static ssize_t ctor_show(struct kmem_cache *s, char *buf)
3933 if (s->ctor) {
3934 int n = sprint_symbol(buf, (unsigned long)s->ctor);
3936 return n + sprintf(buf + n, "\n");
3938 return 0;
3940 SLAB_ATTR_RO(ctor);
3942 static ssize_t aliases_show(struct kmem_cache *s, char *buf)
3944 return sprintf(buf, "%d\n", s->refcount - 1);
3946 SLAB_ATTR_RO(aliases);
3948 static ssize_t slabs_show(struct kmem_cache *s, char *buf)
3950 return show_slab_objects(s, buf, SO_ALL);
3952 SLAB_ATTR_RO(slabs);
3954 static ssize_t partial_show(struct kmem_cache *s, char *buf)
3956 return show_slab_objects(s, buf, SO_PARTIAL);
3958 SLAB_ATTR_RO(partial);
3960 static ssize_t cpu_slabs_show(struct kmem_cache *s, char *buf)
3962 return show_slab_objects(s, buf, SO_CPU);
3964 SLAB_ATTR_RO(cpu_slabs);
3966 static ssize_t objects_show(struct kmem_cache *s, char *buf)
3968 return show_slab_objects(s, buf, SO_ALL|SO_OBJECTS);
3970 SLAB_ATTR_RO(objects);
3972 static ssize_t objects_partial_show(struct kmem_cache *s, char *buf)
3974 return show_slab_objects(s, buf, SO_PARTIAL|SO_OBJECTS);
3976 SLAB_ATTR_RO(objects_partial);
3978 static ssize_t total_objects_show(struct kmem_cache *s, char *buf)
3980 return show_slab_objects(s, buf, SO_ALL|SO_TOTAL);
3982 SLAB_ATTR_RO(total_objects);
3984 static ssize_t sanity_checks_show(struct kmem_cache *s, char *buf)
3986 return sprintf(buf, "%d\n", !!(s->flags & SLAB_DEBUG_FREE));
3989 static ssize_t sanity_checks_store(struct kmem_cache *s,
3990 const char *buf, size_t length)
3992 s->flags &= ~SLAB_DEBUG_FREE;
3993 if (buf[0] == '1')
3994 s->flags |= SLAB_DEBUG_FREE;
3995 return length;
3997 SLAB_ATTR(sanity_checks);
3999 static ssize_t trace_show(struct kmem_cache *s, char *buf)
4001 return sprintf(buf, "%d\n", !!(s->flags & SLAB_TRACE));
4004 static ssize_t trace_store(struct kmem_cache *s, const char *buf,
4005 size_t length)
4007 s->flags &= ~SLAB_TRACE;
4008 if (buf[0] == '1')
4009 s->flags |= SLAB_TRACE;
4010 return length;
4012 SLAB_ATTR(trace);
4014 static ssize_t reclaim_account_show(struct kmem_cache *s, char *buf)
4016 return sprintf(buf, "%d\n", !!(s->flags & SLAB_RECLAIM_ACCOUNT));
4019 static ssize_t reclaim_account_store(struct kmem_cache *s,
4020 const char *buf, size_t length)
4022 s->flags &= ~SLAB_RECLAIM_ACCOUNT;
4023 if (buf[0] == '1')
4024 s->flags |= SLAB_RECLAIM_ACCOUNT;
4025 return length;
4027 SLAB_ATTR(reclaim_account);
4029 static ssize_t hwcache_align_show(struct kmem_cache *s, char *buf)
4031 return sprintf(buf, "%d\n", !!(s->flags & SLAB_HWCACHE_ALIGN));
4033 SLAB_ATTR_RO(hwcache_align);
4035 #ifdef CONFIG_ZONE_DMA
4036 static ssize_t cache_dma_show(struct kmem_cache *s, char *buf)
4038 return sprintf(buf, "%d\n", !!(s->flags & SLAB_CACHE_DMA));
4040 SLAB_ATTR_RO(cache_dma);
4041 #endif
4043 static ssize_t destroy_by_rcu_show(struct kmem_cache *s, char *buf)
4045 return sprintf(buf, "%d\n", !!(s->flags & SLAB_DESTROY_BY_RCU));
4047 SLAB_ATTR_RO(destroy_by_rcu);
4049 static ssize_t red_zone_show(struct kmem_cache *s, char *buf)
4051 return sprintf(buf, "%d\n", !!(s->flags & SLAB_RED_ZONE));
4054 static ssize_t red_zone_store(struct kmem_cache *s,
4055 const char *buf, size_t length)
4057 if (any_slab_objects(s))
4058 return -EBUSY;
4060 s->flags &= ~SLAB_RED_ZONE;
4061 if (buf[0] == '1')
4062 s->flags |= SLAB_RED_ZONE;
4063 calculate_sizes(s, -1);
4064 return length;
4066 SLAB_ATTR(red_zone);
4068 static ssize_t poison_show(struct kmem_cache *s, char *buf)
4070 return sprintf(buf, "%d\n", !!(s->flags & SLAB_POISON));
4073 static ssize_t poison_store(struct kmem_cache *s,
4074 const char *buf, size_t length)
4076 if (any_slab_objects(s))
4077 return -EBUSY;
4079 s->flags &= ~SLAB_POISON;
4080 if (buf[0] == '1')
4081 s->flags |= SLAB_POISON;
4082 calculate_sizes(s, -1);
4083 return length;
4085 SLAB_ATTR(poison);
4087 static ssize_t store_user_show(struct kmem_cache *s, char *buf)
4089 return sprintf(buf, "%d\n", !!(s->flags & SLAB_STORE_USER));
4092 static ssize_t store_user_store(struct kmem_cache *s,
4093 const char *buf, size_t length)
4095 if (any_slab_objects(s))
4096 return -EBUSY;
4098 s->flags &= ~SLAB_STORE_USER;
4099 if (buf[0] == '1')
4100 s->flags |= SLAB_STORE_USER;
4101 calculate_sizes(s, -1);
4102 return length;
4104 SLAB_ATTR(store_user);
4106 static ssize_t validate_show(struct kmem_cache *s, char *buf)
4108 return 0;
4111 static ssize_t validate_store(struct kmem_cache *s,
4112 const char *buf, size_t length)
4114 int ret = -EINVAL;
4116 if (buf[0] == '1') {
4117 ret = validate_slab_cache(s);
4118 if (ret >= 0)
4119 ret = length;
4121 return ret;
4123 SLAB_ATTR(validate);
4125 static ssize_t shrink_show(struct kmem_cache *s, char *buf)
4127 return 0;
4130 static ssize_t shrink_store(struct kmem_cache *s,
4131 const char *buf, size_t length)
4133 if (buf[0] == '1') {
4134 int rc = kmem_cache_shrink(s);
4136 if (rc)
4137 return rc;
4138 } else
4139 return -EINVAL;
4140 return length;
4142 SLAB_ATTR(shrink);
4144 static ssize_t alloc_calls_show(struct kmem_cache *s, char *buf)
4146 if (!(s->flags & SLAB_STORE_USER))
4147 return -ENOSYS;
4148 return list_locations(s, buf, TRACK_ALLOC);
4150 SLAB_ATTR_RO(alloc_calls);
4152 static ssize_t free_calls_show(struct kmem_cache *s, char *buf)
4154 if (!(s->flags & SLAB_STORE_USER))
4155 return -ENOSYS;
4156 return list_locations(s, buf, TRACK_FREE);
4158 SLAB_ATTR_RO(free_calls);
4160 #ifdef CONFIG_NUMA
4161 static ssize_t remote_node_defrag_ratio_show(struct kmem_cache *s, char *buf)
4163 return sprintf(buf, "%d\n", s->remote_node_defrag_ratio / 10);
4166 static ssize_t remote_node_defrag_ratio_store(struct kmem_cache *s,
4167 const char *buf, size_t length)
4169 unsigned long ratio;
4170 int err;
4172 err = strict_strtoul(buf, 10, &ratio);
4173 if (err)
4174 return err;
4176 if (ratio <= 100)
4177 s->remote_node_defrag_ratio = ratio * 10;
4179 return length;
4181 SLAB_ATTR(remote_node_defrag_ratio);
4182 #endif
4184 #ifdef CONFIG_SLUB_STATS
4185 static int show_stat(struct kmem_cache *s, char *buf, enum stat_item si)
4187 unsigned long sum = 0;
4188 int cpu;
4189 int len;
4190 int *data = kmalloc(nr_cpu_ids * sizeof(int), GFP_KERNEL);
4192 if (!data)
4193 return -ENOMEM;
4195 for_each_online_cpu(cpu) {
4196 unsigned x = get_cpu_slab(s, cpu)->stat[si];
4198 data[cpu] = x;
4199 sum += x;
4202 len = sprintf(buf, "%lu", sum);
4204 #ifdef CONFIG_SMP
4205 for_each_online_cpu(cpu) {
4206 if (data[cpu] && len < PAGE_SIZE - 20)
4207 len += sprintf(buf + len, " C%d=%u", cpu, data[cpu]);
4209 #endif
4210 kfree(data);
4211 return len + sprintf(buf + len, "\n");
4214 #define STAT_ATTR(si, text) \
4215 static ssize_t text##_show(struct kmem_cache *s, char *buf) \
4217 return show_stat(s, buf, si); \
4219 SLAB_ATTR_RO(text); \
4221 STAT_ATTR(ALLOC_FASTPATH, alloc_fastpath);
4222 STAT_ATTR(ALLOC_SLOWPATH, alloc_slowpath);
4223 STAT_ATTR(FREE_FASTPATH, free_fastpath);
4224 STAT_ATTR(FREE_SLOWPATH, free_slowpath);
4225 STAT_ATTR(FREE_FROZEN, free_frozen);
4226 STAT_ATTR(FREE_ADD_PARTIAL, free_add_partial);
4227 STAT_ATTR(FREE_REMOVE_PARTIAL, free_remove_partial);
4228 STAT_ATTR(ALLOC_FROM_PARTIAL, alloc_from_partial);
4229 STAT_ATTR(ALLOC_SLAB, alloc_slab);
4230 STAT_ATTR(ALLOC_REFILL, alloc_refill);
4231 STAT_ATTR(FREE_SLAB, free_slab);
4232 STAT_ATTR(CPUSLAB_FLUSH, cpuslab_flush);
4233 STAT_ATTR(DEACTIVATE_FULL, deactivate_full);
4234 STAT_ATTR(DEACTIVATE_EMPTY, deactivate_empty);
4235 STAT_ATTR(DEACTIVATE_TO_HEAD, deactivate_to_head);
4236 STAT_ATTR(DEACTIVATE_TO_TAIL, deactivate_to_tail);
4237 STAT_ATTR(DEACTIVATE_REMOTE_FREES, deactivate_remote_frees);
4238 STAT_ATTR(ORDER_FALLBACK, order_fallback);
4239 #endif
4241 static struct attribute *slab_attrs[] = {
4242 &slab_size_attr.attr,
4243 &object_size_attr.attr,
4244 &objs_per_slab_attr.attr,
4245 &order_attr.attr,
4246 &objects_attr.attr,
4247 &objects_partial_attr.attr,
4248 &total_objects_attr.attr,
4249 &slabs_attr.attr,
4250 &partial_attr.attr,
4251 &cpu_slabs_attr.attr,
4252 &ctor_attr.attr,
4253 &aliases_attr.attr,
4254 &align_attr.attr,
4255 &sanity_checks_attr.attr,
4256 &trace_attr.attr,
4257 &hwcache_align_attr.attr,
4258 &reclaim_account_attr.attr,
4259 &destroy_by_rcu_attr.attr,
4260 &red_zone_attr.attr,
4261 &poison_attr.attr,
4262 &store_user_attr.attr,
4263 &validate_attr.attr,
4264 &shrink_attr.attr,
4265 &alloc_calls_attr.attr,
4266 &free_calls_attr.attr,
4267 #ifdef CONFIG_ZONE_DMA
4268 &cache_dma_attr.attr,
4269 #endif
4270 #ifdef CONFIG_NUMA
4271 &remote_node_defrag_ratio_attr.attr,
4272 #endif
4273 #ifdef CONFIG_SLUB_STATS
4274 &alloc_fastpath_attr.attr,
4275 &alloc_slowpath_attr.attr,
4276 &free_fastpath_attr.attr,
4277 &free_slowpath_attr.attr,
4278 &free_frozen_attr.attr,
4279 &free_add_partial_attr.attr,
4280 &free_remove_partial_attr.attr,
4281 &alloc_from_partial_attr.attr,
4282 &alloc_slab_attr.attr,
4283 &alloc_refill_attr.attr,
4284 &free_slab_attr.attr,
4285 &cpuslab_flush_attr.attr,
4286 &deactivate_full_attr.attr,
4287 &deactivate_empty_attr.attr,
4288 &deactivate_to_head_attr.attr,
4289 &deactivate_to_tail_attr.attr,
4290 &deactivate_remote_frees_attr.attr,
4291 &order_fallback_attr.attr,
4292 #endif
4293 NULL
4296 static struct attribute_group slab_attr_group = {
4297 .attrs = slab_attrs,
4300 static ssize_t slab_attr_show(struct kobject *kobj,
4301 struct attribute *attr,
4302 char *buf)
4304 struct slab_attribute *attribute;
4305 struct kmem_cache *s;
4306 int err;
4308 attribute = to_slab_attr(attr);
4309 s = to_slab(kobj);
4311 if (!attribute->show)
4312 return -EIO;
4314 err = attribute->show(s, buf);
4316 return err;
4319 static ssize_t slab_attr_store(struct kobject *kobj,
4320 struct attribute *attr,
4321 const char *buf, size_t len)
4323 struct slab_attribute *attribute;
4324 struct kmem_cache *s;
4325 int err;
4327 attribute = to_slab_attr(attr);
4328 s = to_slab(kobj);
4330 if (!attribute->store)
4331 return -EIO;
4333 err = attribute->store(s, buf, len);
4335 return err;
4338 static void kmem_cache_release(struct kobject *kobj)
4340 struct kmem_cache *s = to_slab(kobj);
4342 kfree(s);
4345 static struct sysfs_ops slab_sysfs_ops = {
4346 .show = slab_attr_show,
4347 .store = slab_attr_store,
4350 static struct kobj_type slab_ktype = {
4351 .sysfs_ops = &slab_sysfs_ops,
4352 .release = kmem_cache_release
4355 static int uevent_filter(struct kset *kset, struct kobject *kobj)
4357 struct kobj_type *ktype = get_ktype(kobj);
4359 if (ktype == &slab_ktype)
4360 return 1;
4361 return 0;
4364 static struct kset_uevent_ops slab_uevent_ops = {
4365 .filter = uevent_filter,
4368 static struct kset *slab_kset;
4370 #define ID_STR_LENGTH 64
4372 /* Create a unique string id for a slab cache:
4374 * Format :[flags-]size
4376 static char *create_unique_id(struct kmem_cache *s)
4378 char *name = kmalloc(ID_STR_LENGTH, GFP_KERNEL);
4379 char *p = name;
4381 BUG_ON(!name);
4383 *p++ = ':';
4385 * First flags affecting slabcache operations. We will only
4386 * get here for aliasable slabs so we do not need to support
4387 * too many flags. The flags here must cover all flags that
4388 * are matched during merging to guarantee that the id is
4389 * unique.
4391 if (s->flags & SLAB_CACHE_DMA)
4392 *p++ = 'd';
4393 if (s->flags & SLAB_RECLAIM_ACCOUNT)
4394 *p++ = 'a';
4395 if (s->flags & SLAB_DEBUG_FREE)
4396 *p++ = 'F';
4397 if (!(s->flags & SLAB_NOTRACK))
4398 *p++ = 't';
4399 if (p != name + 1)
4400 *p++ = '-';
4401 p += sprintf(p, "%07d", s->size);
4402 BUG_ON(p > name + ID_STR_LENGTH - 1);
4403 return name;
4406 static int sysfs_slab_add(struct kmem_cache *s)
4408 int err;
4409 const char *name;
4410 int unmergeable;
4412 if (slab_state < SYSFS)
4413 /* Defer until later */
4414 return 0;
4416 unmergeable = slab_unmergeable(s);
4417 if (unmergeable) {
4419 * Slabcache can never be merged so we can use the name proper.
4420 * This is typically the case for debug situations. In that
4421 * case we can catch duplicate names easily.
4423 sysfs_remove_link(&slab_kset->kobj, s->name);
4424 name = s->name;
4425 } else {
4427 * Create a unique name for the slab as a target
4428 * for the symlinks.
4430 name = create_unique_id(s);
4433 s->kobj.kset = slab_kset;
4434 err = kobject_init_and_add(&s->kobj, &slab_ktype, NULL, name);
4435 if (err) {
4436 kobject_put(&s->kobj);
4437 return err;
4440 err = sysfs_create_group(&s->kobj, &slab_attr_group);
4441 if (err)
4442 return err;
4443 kobject_uevent(&s->kobj, KOBJ_ADD);
4444 if (!unmergeable) {
4445 /* Setup first alias */
4446 sysfs_slab_alias(s, s->name);
4447 kfree(name);
4449 return 0;
4452 static void sysfs_slab_remove(struct kmem_cache *s)
4454 kobject_uevent(&s->kobj, KOBJ_REMOVE);
4455 kobject_del(&s->kobj);
4456 kobject_put(&s->kobj);
4460 * Need to buffer aliases during bootup until sysfs becomes
4461 * available lest we lose that information.
4463 struct saved_alias {
4464 struct kmem_cache *s;
4465 const char *name;
4466 struct saved_alias *next;
4469 static struct saved_alias *alias_list;
4471 static int sysfs_slab_alias(struct kmem_cache *s, const char *name)
4473 struct saved_alias *al;
4475 if (slab_state == SYSFS) {
4477 * If we have a leftover link then remove it.
4479 sysfs_remove_link(&slab_kset->kobj, name);
4480 return sysfs_create_link(&slab_kset->kobj, &s->kobj, name);
4483 al = kmalloc(sizeof(struct saved_alias), GFP_KERNEL);
4484 if (!al)
4485 return -ENOMEM;
4487 al->s = s;
4488 al->name = name;
4489 al->next = alias_list;
4490 alias_list = al;
4491 return 0;
4494 static int __init slab_sysfs_init(void)
4496 struct kmem_cache *s;
4497 int err;
4499 slab_kset = kset_create_and_add("slab", &slab_uevent_ops, kernel_kobj);
4500 if (!slab_kset) {
4501 printk(KERN_ERR "Cannot register slab subsystem.\n");
4502 return -ENOSYS;
4505 slab_state = SYSFS;
4507 list_for_each_entry(s, &slab_caches, list) {
4508 err = sysfs_slab_add(s);
4509 if (err)
4510 printk(KERN_ERR "SLUB: Unable to add boot slab %s"
4511 " to sysfs\n", s->name);
4514 while (alias_list) {
4515 struct saved_alias *al = alias_list;
4517 alias_list = alias_list->next;
4518 err = sysfs_slab_alias(al->s, al->name);
4519 if (err)
4520 printk(KERN_ERR "SLUB: Unable to add boot slab alias"
4521 " %s to sysfs\n", s->name);
4522 kfree(al);
4525 resiliency_test();
4526 return 0;
4529 __initcall(slab_sysfs_init);
4530 #endif
4533 * The /proc/slabinfo ABI
4535 #ifdef CONFIG_SLABINFO
4536 static void print_slabinfo_header(struct seq_file *m)
4538 seq_puts(m, "slabinfo - version: 2.1\n");
4539 seq_puts(m, "# name <active_objs> <num_objs> <objsize> "
4540 "<objperslab> <pagesperslab>");
4541 seq_puts(m, " : tunables <limit> <batchcount> <sharedfactor>");
4542 seq_puts(m, " : slabdata <active_slabs> <num_slabs> <sharedavail>");
4543 seq_putc(m, '\n');
4546 static void *s_start(struct seq_file *m, loff_t *pos)
4548 loff_t n = *pos;
4550 down_read(&slub_lock);
4551 if (!n)
4552 print_slabinfo_header(m);
4554 return seq_list_start(&slab_caches, *pos);
4557 static void *s_next(struct seq_file *m, void *p, loff_t *pos)
4559 return seq_list_next(p, &slab_caches, pos);
4562 static void s_stop(struct seq_file *m, void *p)
4564 up_read(&slub_lock);
4567 static int s_show(struct seq_file *m, void *p)
4569 unsigned long nr_partials = 0;
4570 unsigned long nr_slabs = 0;
4571 unsigned long nr_inuse = 0;
4572 unsigned long nr_objs = 0;
4573 unsigned long nr_free = 0;
4574 struct kmem_cache *s;
4575 int node;
4577 s = list_entry(p, struct kmem_cache, list);
4579 for_each_online_node(node) {
4580 struct kmem_cache_node *n = get_node(s, node);
4582 if (!n)
4583 continue;
4585 nr_partials += n->nr_partial;
4586 nr_slabs += atomic_long_read(&n->nr_slabs);
4587 nr_objs += atomic_long_read(&n->total_objects);
4588 nr_free += count_partial(n, count_free);
4591 nr_inuse = nr_objs - nr_free;
4593 seq_printf(m, "%-17s %6lu %6lu %6u %4u %4d", s->name, nr_inuse,
4594 nr_objs, s->size, oo_objects(s->oo),
4595 (1 << oo_order(s->oo)));
4596 seq_printf(m, " : tunables %4u %4u %4u", 0, 0, 0);
4597 seq_printf(m, " : slabdata %6lu %6lu %6lu", nr_slabs, nr_slabs,
4598 0UL);
4599 seq_putc(m, '\n');
4600 return 0;
4603 static const struct seq_operations slabinfo_op = {
4604 .start = s_start,
4605 .next = s_next,
4606 .stop = s_stop,
4607 .show = s_show,
4610 static int slabinfo_open(struct inode *inode, struct file *file)
4612 return seq_open(file, &slabinfo_op);
4615 static const struct file_operations proc_slabinfo_operations = {
4616 .open = slabinfo_open,
4617 .read = seq_read,
4618 .llseek = seq_lseek,
4619 .release = seq_release,
4622 static int __init slab_proc_init(void)
4624 proc_create("slabinfo",S_IWUSR|S_IRUGO,NULL,&proc_slabinfo_operations);
4625 return 0;
4627 module_init(slab_proc_init);
4628 #endif /* CONFIG_SLABINFO */