2 * SLUB: A slab allocator that limits cache line use instead of queuing
3 * objects in per cpu and per node lists.
5 * The allocator synchronizes using per slab locks and only
6 * uses a centralized lock to manage a pool of partial slabs.
8 * (C) 2007 SGI, Christoph Lameter <clameter@sgi.com>
12 #include <linux/module.h>
13 #include <linux/bit_spinlock.h>
14 #include <linux/interrupt.h>
15 #include <linux/bitops.h>
16 #include <linux/slab.h>
17 #include <linux/seq_file.h>
18 #include <linux/cpu.h>
19 #include <linux/cpuset.h>
20 #include <linux/mempolicy.h>
21 #include <linux/ctype.h>
22 #include <linux/kallsyms.h>
29 * The slab_lock protects operations on the object of a particular
30 * slab and its metadata in the page struct. If the slab lock
31 * has been taken then no allocations nor frees can be performed
32 * on the objects in the slab nor can the slab be added or removed
33 * from the partial or full lists since this would mean modifying
34 * the page_struct of the slab.
36 * The list_lock protects the partial and full list on each node and
37 * the partial slab counter. If taken then no new slabs may be added or
38 * removed from the lists nor make the number of partial slabs be modified.
39 * (Note that the total number of slabs is an atomic value that may be
40 * modified without taking the list lock).
42 * The list_lock is a centralized lock and thus we avoid taking it as
43 * much as possible. As long as SLUB does not have to handle partial
44 * slabs, operations can continue without any centralized lock. F.e.
45 * allocating a long series of objects that fill up slabs does not require
48 * The lock order is sometimes inverted when we are trying to get a slab
49 * off a list. We take the list_lock and then look for a page on the list
50 * to use. While we do that objects in the slabs may be freed. We can
51 * only operate on the slab if we have also taken the slab_lock. So we use
52 * a slab_trylock() on the slab. If trylock was successful then no frees
53 * can occur anymore and we can use the slab for allocations etc. If the
54 * slab_trylock() does not succeed then frees are in progress in the slab and
55 * we must stay away from it for a while since we may cause a bouncing
56 * cacheline if we try to acquire the lock. So go onto the next slab.
57 * If all pages are busy then we may allocate a new slab instead of reusing
58 * a partial slab. A new slab has noone operating on it and thus there is
59 * no danger of cacheline contention.
61 * Interrupts are disabled during allocation and deallocation in order to
62 * make the slab allocator safe to use in the context of an irq. In addition
63 * interrupts are disabled to ensure that the processor does not change
64 * while handling per_cpu slabs, due to kernel preemption.
66 * SLUB assigns one slab for allocation to each processor.
67 * Allocations only occur from these slabs called cpu slabs.
69 * Slabs with free elements are kept on a partial list.
70 * There is no list for full slabs. If an object in a full slab is
71 * freed then the slab will show up again on the partial lists.
72 * Otherwise there is no need to track full slabs unless we have to
73 * track full slabs for debugging purposes.
75 * Slabs are freed when they become empty. Teardown and setup is
76 * minimal so we rely on the page allocators per cpu caches for
77 * fast frees and allocs.
79 * Overloading of page flags that are otherwise used for LRU management.
81 * PageActive The slab is used as a cpu cache. Allocations
82 * may be performed from the slab. The slab is not
83 * on any slab list and cannot be moved onto one.
85 * PageError Slab requires special handling due to debug
86 * options set. This moves slab handling out of
91 * Issues still to be resolved:
93 * - The per cpu array is updated for each new slab and and is a remote
94 * cacheline for most nodes. This could become a bouncing cacheline given
95 * enough frequent updates. There are 16 pointers in a cacheline.so at
96 * max 16 cpus could compete. Likely okay.
98 * - Support PAGE_ALLOC_DEBUG. Should be easy to do.
100 * - Variable sizing of the per node arrays
103 /* Enable to test recovery from slab corruption on boot */
104 #undef SLUB_RESILIENCY_TEST
109 * Small page size. Make sure that we do not fragment memory
111 #define DEFAULT_MAX_ORDER 1
112 #define DEFAULT_MIN_OBJECTS 4
117 * Large page machines are customarily able to handle larger
120 #define DEFAULT_MAX_ORDER 2
121 #define DEFAULT_MIN_OBJECTS 8
126 * Mininum number of partial slabs. These will be left on the partial
127 * lists even if they are empty. kmem_cache_shrink may reclaim them.
129 #define MIN_PARTIAL 2
132 * Maximum number of desirable partial slabs.
133 * The existence of more partial slabs makes kmem_cache_shrink
134 * sort the partial list by the number of objects in the.
136 #define MAX_PARTIAL 10
138 #define DEBUG_DEFAULT_FLAGS (SLAB_DEBUG_FREE | SLAB_RED_ZONE | \
139 SLAB_POISON | SLAB_STORE_USER)
141 * Set of flags that will prevent slab merging
143 #define SLUB_NEVER_MERGE (SLAB_RED_ZONE | SLAB_POISON | SLAB_STORE_USER | \
144 SLAB_TRACE | SLAB_DESTROY_BY_RCU)
146 #define SLUB_MERGE_SAME (SLAB_DEBUG_FREE | SLAB_RECLAIM_ACCOUNT | \
149 #ifndef ARCH_KMALLOC_MINALIGN
150 #define ARCH_KMALLOC_MINALIGN __alignof__(unsigned long long)
153 #ifndef ARCH_SLAB_MINALIGN
154 #define ARCH_SLAB_MINALIGN __alignof__(unsigned long long)
157 /* Internal SLUB flags */
158 #define __OBJECT_POISON 0x80000000 /* Poison object */
160 /* Not all arches define cache_line_size */
161 #ifndef cache_line_size
162 #define cache_line_size() L1_CACHE_BYTES
165 static int kmem_size
= sizeof(struct kmem_cache
);
168 static struct notifier_block slab_notifier
;
172 DOWN
, /* No slab functionality available */
173 PARTIAL
, /* kmem_cache_open() works but kmalloc does not */
174 UP
, /* Everything works */
178 /* A list of all slab caches on the system */
179 static DECLARE_RWSEM(slub_lock
);
180 LIST_HEAD(slab_caches
);
183 static int sysfs_slab_add(struct kmem_cache
*);
184 static int sysfs_slab_alias(struct kmem_cache
*, const char *);
185 static void sysfs_slab_remove(struct kmem_cache
*);
187 static int sysfs_slab_add(struct kmem_cache
*s
) { return 0; }
188 static int sysfs_slab_alias(struct kmem_cache
*s
, const char *p
) { return 0; }
189 static void sysfs_slab_remove(struct kmem_cache
*s
) {}
192 /********************************************************************
193 * Core slab cache functions
194 *******************************************************************/
196 int slab_is_available(void)
198 return slab_state
>= UP
;
201 static inline struct kmem_cache_node
*get_node(struct kmem_cache
*s
, int node
)
204 return s
->node
[node
];
206 return &s
->local_node
;
213 static void print_section(char *text
, u8
*addr
, unsigned int length
)
221 for (i
= 0; i
< length
; i
++) {
223 printk(KERN_ERR
"%10s 0x%p: ", text
, addr
+ i
);
226 printk(" %02x", addr
[i
]);
228 ascii
[offset
] = isgraph(addr
[i
]) ? addr
[i
] : '.';
230 printk(" %s\n",ascii
);
241 printk(" %s\n", ascii
);
246 * Slow version of get and set free pointer.
248 * This requires touching the cache lines of kmem_cache.
249 * The offset can also be obtained from the page. In that
250 * case it is in the cacheline that we already need to touch.
252 static void *get_freepointer(struct kmem_cache
*s
, void *object
)
254 return *(void **)(object
+ s
->offset
);
257 static void set_freepointer(struct kmem_cache
*s
, void *object
, void *fp
)
259 *(void **)(object
+ s
->offset
) = fp
;
263 * Tracking user of a slab.
266 void *addr
; /* Called from address */
267 int cpu
; /* Was running on cpu */
268 int pid
; /* Pid context */
269 unsigned long when
; /* When did the operation occur */
272 enum track_item
{ TRACK_ALLOC
, TRACK_FREE
};
274 static struct track
*get_track(struct kmem_cache
*s
, void *object
,
275 enum track_item alloc
)
280 p
= object
+ s
->offset
+ sizeof(void *);
282 p
= object
+ s
->inuse
;
287 static void set_track(struct kmem_cache
*s
, void *object
,
288 enum track_item alloc
, void *addr
)
293 p
= object
+ s
->offset
+ sizeof(void *);
295 p
= object
+ s
->inuse
;
300 p
->cpu
= smp_processor_id();
301 p
->pid
= current
? current
->pid
: -1;
304 memset(p
, 0, sizeof(struct track
));
307 static void init_tracking(struct kmem_cache
*s
, void *object
)
309 if (s
->flags
& SLAB_STORE_USER
) {
310 set_track(s
, object
, TRACK_FREE
, NULL
);
311 set_track(s
, object
, TRACK_ALLOC
, NULL
);
315 static void print_track(const char *s
, struct track
*t
)
320 printk(KERN_ERR
"%s: ", s
);
321 __print_symbol("%s", (unsigned long)t
->addr
);
322 printk(" jiffies_ago=%lu cpu=%u pid=%d\n", jiffies
- t
->when
, t
->cpu
, t
->pid
);
325 static void print_trailer(struct kmem_cache
*s
, u8
*p
)
327 unsigned int off
; /* Offset of last byte */
329 if (s
->flags
& SLAB_RED_ZONE
)
330 print_section("Redzone", p
+ s
->objsize
,
331 s
->inuse
- s
->objsize
);
333 printk(KERN_ERR
"FreePointer 0x%p -> 0x%p\n",
335 get_freepointer(s
, p
));
338 off
= s
->offset
+ sizeof(void *);
342 if (s
->flags
& SLAB_STORE_USER
) {
343 print_track("Last alloc", get_track(s
, p
, TRACK_ALLOC
));
344 print_track("Last free ", get_track(s
, p
, TRACK_FREE
));
345 off
+= 2 * sizeof(struct track
);
349 /* Beginning of the filler is the free pointer */
350 print_section("Filler", p
+ off
, s
->size
- off
);
353 static void object_err(struct kmem_cache
*s
, struct page
*page
,
354 u8
*object
, char *reason
)
356 u8
*addr
= page_address(page
);
358 printk(KERN_ERR
"*** SLUB %s: %s@0x%p slab 0x%p\n",
359 s
->name
, reason
, object
, page
);
360 printk(KERN_ERR
" offset=%tu flags=0x%04lx inuse=%u freelist=0x%p\n",
361 object
- addr
, page
->flags
, page
->inuse
, page
->freelist
);
362 if (object
> addr
+ 16)
363 print_section("Bytes b4", object
- 16, 16);
364 print_section("Object", object
, min(s
->objsize
, 128));
365 print_trailer(s
, object
);
369 static void slab_err(struct kmem_cache
*s
, struct page
*page
, char *reason
, ...)
374 va_start(args
, reason
);
375 vsnprintf(buf
, sizeof(buf
), reason
, args
);
377 printk(KERN_ERR
"*** SLUB %s: %s in slab @0x%p\n", s
->name
, buf
,
382 static void init_object(struct kmem_cache
*s
, void *object
, int active
)
386 if (s
->flags
& __OBJECT_POISON
) {
387 memset(p
, POISON_FREE
, s
->objsize
- 1);
388 p
[s
->objsize
-1] = POISON_END
;
391 if (s
->flags
& SLAB_RED_ZONE
)
392 memset(p
+ s
->objsize
,
393 active
? SLUB_RED_ACTIVE
: SLUB_RED_INACTIVE
,
394 s
->inuse
- s
->objsize
);
397 static int check_bytes(u8
*start
, unsigned int value
, unsigned int bytes
)
400 if (*start
!= (u8
)value
)
409 static int check_valid_pointer(struct kmem_cache
*s
, struct page
*page
,
417 base
= page_address(page
);
418 if (object
< base
|| object
>= base
+ s
->objects
* s
->size
||
419 (object
- base
) % s
->size
) {
430 * Bytes of the object to be managed.
431 * If the freepointer may overlay the object then the free
432 * pointer is the first word of the object.
433 * Poisoning uses 0x6b (POISON_FREE) and the last byte is
436 * object + s->objsize
437 * Padding to reach word boundary. This is also used for Redzoning.
438 * Padding is extended to word size if Redzoning is enabled
439 * and objsize == inuse.
440 * We fill with 0xbb (RED_INACTIVE) for inactive objects and with
441 * 0xcc (RED_ACTIVE) for objects in use.
444 * A. Free pointer (if we cannot overwrite object on free)
445 * B. Tracking data for SLAB_STORE_USER
446 * C. Padding to reach required alignment boundary
447 * Padding is done using 0x5a (POISON_INUSE)
451 * If slabcaches are merged then the objsize and inuse boundaries are to
452 * be ignored. And therefore no slab options that rely on these boundaries
453 * may be used with merged slabcaches.
456 static void restore_bytes(struct kmem_cache
*s
, char *message
, u8 data
,
457 void *from
, void *to
)
459 printk(KERN_ERR
"@@@ SLUB %s: Restoring %s (0x%x) from 0x%p-0x%p\n",
460 s
->name
, message
, data
, from
, to
- 1);
461 memset(from
, data
, to
- from
);
464 static int check_pad_bytes(struct kmem_cache
*s
, struct page
*page
, u8
*p
)
466 unsigned long off
= s
->inuse
; /* The end of info */
469 /* Freepointer is placed after the object. */
470 off
+= sizeof(void *);
472 if (s
->flags
& SLAB_STORE_USER
)
473 /* We also have user information there */
474 off
+= 2 * sizeof(struct track
);
479 if (check_bytes(p
+ off
, POISON_INUSE
, s
->size
- off
))
482 object_err(s
, page
, p
, "Object padding check fails");
487 restore_bytes(s
, "object padding", POISON_INUSE
, p
+ off
, p
+ s
->size
);
491 static int slab_pad_check(struct kmem_cache
*s
, struct page
*page
)
494 int length
, remainder
;
496 if (!(s
->flags
& SLAB_POISON
))
499 p
= page_address(page
);
500 length
= s
->objects
* s
->size
;
501 remainder
= (PAGE_SIZE
<< s
->order
) - length
;
505 if (!check_bytes(p
+ length
, POISON_INUSE
, remainder
)) {
506 slab_err(s
, page
, "Padding check failed");
507 restore_bytes(s
, "slab padding", POISON_INUSE
, p
+ length
,
508 p
+ length
+ remainder
);
514 static int check_object(struct kmem_cache
*s
, struct page
*page
,
515 void *object
, int active
)
518 u8
*endobject
= object
+ s
->objsize
;
520 if (s
->flags
& SLAB_RED_ZONE
) {
522 active
? SLUB_RED_ACTIVE
: SLUB_RED_INACTIVE
;
524 if (!check_bytes(endobject
, red
, s
->inuse
- s
->objsize
)) {
525 object_err(s
, page
, object
,
526 active
? "Redzone Active" : "Redzone Inactive");
527 restore_bytes(s
, "redzone", red
,
528 endobject
, object
+ s
->inuse
);
532 if ((s
->flags
& SLAB_POISON
) && s
->objsize
< s
->inuse
&&
533 !check_bytes(endobject
, POISON_INUSE
,
534 s
->inuse
- s
->objsize
)) {
535 object_err(s
, page
, p
, "Alignment padding check fails");
537 * Fix it so that there will not be another report.
539 * Hmmm... We may be corrupting an object that now expects
540 * to be longer than allowed.
542 restore_bytes(s
, "alignment padding", POISON_INUSE
,
543 endobject
, object
+ s
->inuse
);
547 if (s
->flags
& SLAB_POISON
) {
548 if (!active
&& (s
->flags
& __OBJECT_POISON
) &&
549 (!check_bytes(p
, POISON_FREE
, s
->objsize
- 1) ||
550 p
[s
->objsize
- 1] != POISON_END
)) {
552 object_err(s
, page
, p
, "Poison check failed");
553 restore_bytes(s
, "Poison", POISON_FREE
,
554 p
, p
+ s
->objsize
-1);
555 restore_bytes(s
, "Poison", POISON_END
,
556 p
+ s
->objsize
- 1, p
+ s
->objsize
);
560 * check_pad_bytes cleans up on its own.
562 check_pad_bytes(s
, page
, p
);
565 if (!s
->offset
&& active
)
567 * Object and freepointer overlap. Cannot check
568 * freepointer while object is allocated.
572 /* Check free pointer validity */
573 if (!check_valid_pointer(s
, page
, get_freepointer(s
, p
))) {
574 object_err(s
, page
, p
, "Freepointer corrupt");
576 * No choice but to zap it and thus loose the remainder
577 * of the free objects in this slab. May cause
578 * another error because the object count maybe
581 set_freepointer(s
, p
, NULL
);
587 static int check_slab(struct kmem_cache
*s
, struct page
*page
)
589 VM_BUG_ON(!irqs_disabled());
591 if (!PageSlab(page
)) {
592 slab_err(s
, page
, "Not a valid slab page flags=%lx "
593 "mapping=0x%p count=%d", page
->flags
, page
->mapping
,
597 if (page
->offset
* sizeof(void *) != s
->offset
) {
598 slab_err(s
, page
, "Corrupted offset %lu flags=0x%lx "
599 "mapping=0x%p count=%d",
600 (unsigned long)(page
->offset
* sizeof(void *)),
606 if (page
->inuse
> s
->objects
) {
607 slab_err(s
, page
, "inuse %u > max %u @0x%p flags=%lx "
608 "mapping=0x%p count=%d",
609 s
->name
, page
->inuse
, s
->objects
, page
->flags
,
610 page
->mapping
, page_count(page
));
613 /* Slab_pad_check fixes things up after itself */
614 slab_pad_check(s
, page
);
619 * Determine if a certain object on a page is on the freelist and
620 * therefore free. Must hold the slab lock for cpu slabs to
621 * guarantee that the chains are consistent.
623 static int on_freelist(struct kmem_cache
*s
, struct page
*page
, void *search
)
626 void *fp
= page
->freelist
;
629 while (fp
&& nr
<= s
->objects
) {
632 if (!check_valid_pointer(s
, page
, fp
)) {
634 object_err(s
, page
, object
,
635 "Freechain corrupt");
636 set_freepointer(s
, object
, NULL
);
639 slab_err(s
, page
, "Freepointer 0x%p corrupt",
641 page
->freelist
= NULL
;
642 page
->inuse
= s
->objects
;
643 printk(KERN_ERR
"@@@ SLUB %s: Freelist "
644 "cleared. Slab 0x%p\n",
651 fp
= get_freepointer(s
, object
);
655 if (page
->inuse
!= s
->objects
- nr
) {
656 slab_err(s
, page
, "Wrong object count. Counter is %d but "
657 "counted were %d", s
, page
, page
->inuse
,
659 page
->inuse
= s
->objects
- nr
;
660 printk(KERN_ERR
"@@@ SLUB %s: Object count adjusted. "
661 "Slab @0x%p\n", s
->name
, page
);
663 return search
== NULL
;
667 * Tracking of fully allocated slabs for debugging
669 static void add_full(struct kmem_cache_node
*n
, struct page
*page
)
671 spin_lock(&n
->list_lock
);
672 list_add(&page
->lru
, &n
->full
);
673 spin_unlock(&n
->list_lock
);
676 static void remove_full(struct kmem_cache
*s
, struct page
*page
)
678 struct kmem_cache_node
*n
;
680 if (!(s
->flags
& SLAB_STORE_USER
))
683 n
= get_node(s
, page_to_nid(page
));
685 spin_lock(&n
->list_lock
);
686 list_del(&page
->lru
);
687 spin_unlock(&n
->list_lock
);
690 static int alloc_object_checks(struct kmem_cache
*s
, struct page
*page
,
693 if (!check_slab(s
, page
))
696 if (object
&& !on_freelist(s
, page
, object
)) {
697 slab_err(s
, page
, "Object 0x%p already allocated", object
);
701 if (!check_valid_pointer(s
, page
, object
)) {
702 object_err(s
, page
, object
, "Freelist Pointer check fails");
709 if (!check_object(s
, page
, object
, 0))
714 if (PageSlab(page
)) {
716 * If this is a slab page then lets do the best we can
717 * to avoid issues in the future. Marking all objects
718 * as used avoids touching the remainder.
720 printk(KERN_ERR
"@@@ SLUB: %s slab 0x%p. Marking all objects used.\n",
722 page
->inuse
= s
->objects
;
723 page
->freelist
= NULL
;
724 /* Fix up fields that may be corrupted */
725 page
->offset
= s
->offset
/ sizeof(void *);
730 static int free_object_checks(struct kmem_cache
*s
, struct page
*page
,
733 if (!check_slab(s
, page
))
736 if (!check_valid_pointer(s
, page
, object
)) {
737 slab_err(s
, page
, "Invalid object pointer 0x%p", object
);
741 if (on_freelist(s
, page
, object
)) {
742 slab_err(s
, page
, "Object 0x%p already free", object
);
746 if (!check_object(s
, page
, object
, 1))
749 if (unlikely(s
!= page
->slab
)) {
751 slab_err(s
, page
, "Attempt to free object(0x%p) "
752 "outside of slab", object
);
756 "SLUB <none>: no slab for object 0x%p.\n",
761 slab_err(s
, page
, "object at 0x%p belongs "
762 "to slab %s", object
, page
->slab
->name
);
767 printk(KERN_ERR
"@@@ SLUB: %s slab 0x%p object at 0x%p not freed.\n",
768 s
->name
, page
, object
);
773 * Slab allocation and freeing
775 static struct page
*allocate_slab(struct kmem_cache
*s
, gfp_t flags
, int node
)
778 int pages
= 1 << s
->order
;
783 if (s
->flags
& SLAB_CACHE_DMA
)
787 page
= alloc_pages(flags
, s
->order
);
789 page
= alloc_pages_node(node
, flags
, s
->order
);
794 mod_zone_page_state(page_zone(page
),
795 (s
->flags
& SLAB_RECLAIM_ACCOUNT
) ?
796 NR_SLAB_RECLAIMABLE
: NR_SLAB_UNRECLAIMABLE
,
802 static void setup_object(struct kmem_cache
*s
, struct page
*page
,
805 if (PageError(page
)) {
806 init_object(s
, object
, 0);
807 init_tracking(s
, object
);
810 if (unlikely(s
->ctor
))
811 s
->ctor(object
, s
, SLAB_CTOR_CONSTRUCTOR
);
814 static struct page
*new_slab(struct kmem_cache
*s
, gfp_t flags
, int node
)
817 struct kmem_cache_node
*n
;
823 BUG_ON(flags
& ~(GFP_DMA
| GFP_LEVEL_MASK
));
825 if (flags
& __GFP_WAIT
)
828 page
= allocate_slab(s
, flags
& GFP_LEVEL_MASK
, node
);
832 n
= get_node(s
, page_to_nid(page
));
834 atomic_long_inc(&n
->nr_slabs
);
835 page
->offset
= s
->offset
/ sizeof(void *);
837 page
->flags
|= 1 << PG_slab
;
838 if (s
->flags
& (SLAB_DEBUG_FREE
| SLAB_RED_ZONE
| SLAB_POISON
|
839 SLAB_STORE_USER
| SLAB_TRACE
))
840 page
->flags
|= 1 << PG_error
;
842 start
= page_address(page
);
843 end
= start
+ s
->objects
* s
->size
;
845 if (unlikely(s
->flags
& SLAB_POISON
))
846 memset(start
, POISON_INUSE
, PAGE_SIZE
<< s
->order
);
849 for (p
= start
+ s
->size
; p
< end
; p
+= s
->size
) {
850 setup_object(s
, page
, last
);
851 set_freepointer(s
, last
, p
);
854 setup_object(s
, page
, last
);
855 set_freepointer(s
, last
, NULL
);
857 page
->freelist
= start
;
860 if (flags
& __GFP_WAIT
)
865 static void __free_slab(struct kmem_cache
*s
, struct page
*page
)
867 int pages
= 1 << s
->order
;
869 if (unlikely(PageError(page
) || s
->dtor
)) {
870 void *start
= page_address(page
);
871 void *end
= start
+ (pages
<< PAGE_SHIFT
);
874 slab_pad_check(s
, page
);
875 for (p
= start
; p
<= end
- s
->size
; p
+= s
->size
) {
878 check_object(s
, page
, p
, 0);
882 mod_zone_page_state(page_zone(page
),
883 (s
->flags
& SLAB_RECLAIM_ACCOUNT
) ?
884 NR_SLAB_RECLAIMABLE
: NR_SLAB_UNRECLAIMABLE
,
887 page
->mapping
= NULL
;
888 __free_pages(page
, s
->order
);
891 static void rcu_free_slab(struct rcu_head
*h
)
895 page
= container_of((struct list_head
*)h
, struct page
, lru
);
896 __free_slab(page
->slab
, page
);
899 static void free_slab(struct kmem_cache
*s
, struct page
*page
)
901 if (unlikely(s
->flags
& SLAB_DESTROY_BY_RCU
)) {
903 * RCU free overloads the RCU head over the LRU
905 struct rcu_head
*head
= (void *)&page
->lru
;
907 call_rcu(head
, rcu_free_slab
);
909 __free_slab(s
, page
);
912 static void discard_slab(struct kmem_cache
*s
, struct page
*page
)
914 struct kmem_cache_node
*n
= get_node(s
, page_to_nid(page
));
916 atomic_long_dec(&n
->nr_slabs
);
917 reset_page_mapcount(page
);
918 page
->flags
&= ~(1 << PG_slab
| 1 << PG_error
);
923 * Per slab locking using the pagelock
925 static __always_inline
void slab_lock(struct page
*page
)
927 bit_spin_lock(PG_locked
, &page
->flags
);
930 static __always_inline
void slab_unlock(struct page
*page
)
932 bit_spin_unlock(PG_locked
, &page
->flags
);
935 static __always_inline
int slab_trylock(struct page
*page
)
939 rc
= bit_spin_trylock(PG_locked
, &page
->flags
);
944 * Management of partially allocated slabs
946 static void add_partial_tail(struct kmem_cache_node
*n
, struct page
*page
)
948 spin_lock(&n
->list_lock
);
950 list_add_tail(&page
->lru
, &n
->partial
);
951 spin_unlock(&n
->list_lock
);
954 static void add_partial(struct kmem_cache_node
*n
, struct page
*page
)
956 spin_lock(&n
->list_lock
);
958 list_add(&page
->lru
, &n
->partial
);
959 spin_unlock(&n
->list_lock
);
962 static void remove_partial(struct kmem_cache
*s
,
965 struct kmem_cache_node
*n
= get_node(s
, page_to_nid(page
));
967 spin_lock(&n
->list_lock
);
968 list_del(&page
->lru
);
970 spin_unlock(&n
->list_lock
);
974 * Lock page and remove it from the partial list
976 * Must hold list_lock
978 static int lock_and_del_slab(struct kmem_cache_node
*n
, struct page
*page
)
980 if (slab_trylock(page
)) {
981 list_del(&page
->lru
);
989 * Try to get a partial slab from a specific node
991 static struct page
*get_partial_node(struct kmem_cache_node
*n
)
996 * Racy check. If we mistakenly see no partial slabs then we
997 * just allocate an empty slab. If we mistakenly try to get a
998 * partial slab then get_partials() will return NULL.
1000 if (!n
|| !n
->nr_partial
)
1003 spin_lock(&n
->list_lock
);
1004 list_for_each_entry(page
, &n
->partial
, lru
)
1005 if (lock_and_del_slab(n
, page
))
1009 spin_unlock(&n
->list_lock
);
1014 * Get a page from somewhere. Search in increasing NUMA
1017 static struct page
*get_any_partial(struct kmem_cache
*s
, gfp_t flags
)
1020 struct zonelist
*zonelist
;
1025 * The defrag ratio allows to configure the tradeoffs between
1026 * inter node defragmentation and node local allocations.
1027 * A lower defrag_ratio increases the tendency to do local
1028 * allocations instead of scanning throught the partial
1029 * lists on other nodes.
1031 * If defrag_ratio is set to 0 then kmalloc() always
1032 * returns node local objects. If its higher then kmalloc()
1033 * may return off node objects in order to avoid fragmentation.
1035 * A higher ratio means slabs may be taken from other nodes
1036 * thus reducing the number of partial slabs on those nodes.
1038 * If /sys/slab/xx/defrag_ratio is set to 100 (which makes
1039 * defrag_ratio = 1000) then every (well almost) allocation
1040 * will first attempt to defrag slab caches on other nodes. This
1041 * means scanning over all nodes to look for partial slabs which
1042 * may be a bit expensive to do on every slab allocation.
1044 if (!s
->defrag_ratio
|| get_cycles() % 1024 > s
->defrag_ratio
)
1047 zonelist
= &NODE_DATA(slab_node(current
->mempolicy
))
1048 ->node_zonelists
[gfp_zone(flags
)];
1049 for (z
= zonelist
->zones
; *z
; z
++) {
1050 struct kmem_cache_node
*n
;
1052 n
= get_node(s
, zone_to_nid(*z
));
1054 if (n
&& cpuset_zone_allowed_hardwall(*z
, flags
) &&
1055 n
->nr_partial
> MIN_PARTIAL
) {
1056 page
= get_partial_node(n
);
1066 * Get a partial page, lock it and return it.
1068 static struct page
*get_partial(struct kmem_cache
*s
, gfp_t flags
, int node
)
1071 int searchnode
= (node
== -1) ? numa_node_id() : node
;
1073 page
= get_partial_node(get_node(s
, searchnode
));
1074 if (page
|| (flags
& __GFP_THISNODE
))
1077 return get_any_partial(s
, flags
);
1081 * Move a page back to the lists.
1083 * Must be called with the slab lock held.
1085 * On exit the slab lock will have been dropped.
1087 static void putback_slab(struct kmem_cache
*s
, struct page
*page
)
1089 struct kmem_cache_node
*n
= get_node(s
, page_to_nid(page
));
1094 add_partial(n
, page
);
1095 else if (PageError(page
) && (s
->flags
& SLAB_STORE_USER
))
1100 if (n
->nr_partial
< MIN_PARTIAL
) {
1102 * Adding an empty page to the partial slabs in order
1103 * to avoid page allocator overhead. This page needs to
1104 * come after all the others that are not fully empty
1105 * in order to make sure that we do maximum
1108 add_partial_tail(n
, page
);
1112 discard_slab(s
, page
);
1118 * Remove the cpu slab
1120 static void deactivate_slab(struct kmem_cache
*s
, struct page
*page
, int cpu
)
1122 s
->cpu_slab
[cpu
] = NULL
;
1123 ClearPageActive(page
);
1125 putback_slab(s
, page
);
1128 static void flush_slab(struct kmem_cache
*s
, struct page
*page
, int cpu
)
1131 deactivate_slab(s
, page
, cpu
);
1136 * Called from IPI handler with interrupts disabled.
1138 static void __flush_cpu_slab(struct kmem_cache
*s
, int cpu
)
1140 struct page
*page
= s
->cpu_slab
[cpu
];
1143 flush_slab(s
, page
, cpu
);
1146 static void flush_cpu_slab(void *d
)
1148 struct kmem_cache
*s
= d
;
1149 int cpu
= smp_processor_id();
1151 __flush_cpu_slab(s
, cpu
);
1154 static void flush_all(struct kmem_cache
*s
)
1157 on_each_cpu(flush_cpu_slab
, s
, 1, 1);
1159 unsigned long flags
;
1161 local_irq_save(flags
);
1163 local_irq_restore(flags
);
1168 * slab_alloc is optimized to only modify two cachelines on the fast path
1169 * (aside from the stack):
1171 * 1. The page struct
1172 * 2. The first cacheline of the object to be allocated.
1174 * The only cache lines that are read (apart from code) is the
1175 * per cpu array in the kmem_cache struct.
1177 * Fastpath is not possible if we need to get a new slab or have
1178 * debugging enabled (which means all slabs are marked with PageError)
1180 static void *slab_alloc(struct kmem_cache
*s
,
1181 gfp_t gfpflags
, int node
, void *addr
)
1185 unsigned long flags
;
1188 local_irq_save(flags
);
1189 cpu
= smp_processor_id();
1190 page
= s
->cpu_slab
[cpu
];
1195 if (unlikely(node
!= -1 && page_to_nid(page
) != node
))
1198 object
= page
->freelist
;
1199 if (unlikely(!object
))
1201 if (unlikely(PageError(page
)))
1206 page
->freelist
= object
[page
->offset
];
1208 local_irq_restore(flags
);
1212 deactivate_slab(s
, page
, cpu
);
1215 page
= get_partial(s
, gfpflags
, node
);
1218 s
->cpu_slab
[cpu
] = page
;
1219 SetPageActive(page
);
1223 page
= new_slab(s
, gfpflags
, node
);
1225 cpu
= smp_processor_id();
1226 if (s
->cpu_slab
[cpu
]) {
1228 * Someone else populated the cpu_slab while we enabled
1229 * interrupts, or we have got scheduled on another cpu.
1230 * The page may not be on the requested node.
1233 page_to_nid(s
->cpu_slab
[cpu
]) == node
) {
1235 * Current cpuslab is acceptable and we
1236 * want the current one since its cache hot
1238 discard_slab(s
, page
);
1239 page
= s
->cpu_slab
[cpu
];
1243 /* Dump the current slab */
1244 flush_slab(s
, s
->cpu_slab
[cpu
], cpu
);
1249 local_irq_restore(flags
);
1252 if (!alloc_object_checks(s
, page
, object
))
1254 if (s
->flags
& SLAB_STORE_USER
)
1255 set_track(s
, object
, TRACK_ALLOC
, addr
);
1256 if (s
->flags
& SLAB_TRACE
) {
1257 printk(KERN_INFO
"TRACE %s alloc 0x%p inuse=%d fp=0x%p\n",
1258 s
->name
, object
, page
->inuse
,
1262 init_object(s
, object
, 1);
1266 void *kmem_cache_alloc(struct kmem_cache
*s
, gfp_t gfpflags
)
1268 return slab_alloc(s
, gfpflags
, -1, __builtin_return_address(0));
1270 EXPORT_SYMBOL(kmem_cache_alloc
);
1273 void *kmem_cache_alloc_node(struct kmem_cache
*s
, gfp_t gfpflags
, int node
)
1275 return slab_alloc(s
, gfpflags
, node
, __builtin_return_address(0));
1277 EXPORT_SYMBOL(kmem_cache_alloc_node
);
1281 * The fastpath only writes the cacheline of the page struct and the first
1282 * cacheline of the object.
1284 * No special cachelines need to be read
1286 static void slab_free(struct kmem_cache
*s
, struct page
*page
,
1287 void *x
, void *addr
)
1290 void **object
= (void *)x
;
1291 unsigned long flags
;
1293 local_irq_save(flags
);
1296 if (unlikely(PageError(page
)))
1299 prior
= object
[page
->offset
] = page
->freelist
;
1300 page
->freelist
= object
;
1303 if (unlikely(PageActive(page
)))
1305 * Cpu slabs are never on partial lists and are
1310 if (unlikely(!page
->inuse
))
1314 * Objects left in the slab. If it
1315 * was not on the partial list before
1318 if (unlikely(!prior
))
1319 add_partial(get_node(s
, page_to_nid(page
)), page
);
1323 local_irq_restore(flags
);
1329 * Slab on the partial list.
1331 remove_partial(s
, page
);
1334 discard_slab(s
, page
);
1335 local_irq_restore(flags
);
1339 if (!free_object_checks(s
, page
, x
))
1341 if (!PageActive(page
) && !page
->freelist
)
1342 remove_full(s
, page
);
1343 if (s
->flags
& SLAB_STORE_USER
)
1344 set_track(s
, x
, TRACK_FREE
, addr
);
1345 if (s
->flags
& SLAB_TRACE
) {
1346 printk(KERN_INFO
"TRACE %s free 0x%p inuse=%d fp=0x%p\n",
1347 s
->name
, object
, page
->inuse
,
1349 print_section("Object", (void *)object
, s
->objsize
);
1352 init_object(s
, object
, 0);
1356 void kmem_cache_free(struct kmem_cache
*s
, void *x
)
1360 page
= virt_to_head_page(x
);
1362 slab_free(s
, page
, x
, __builtin_return_address(0));
1364 EXPORT_SYMBOL(kmem_cache_free
);
1366 /* Figure out on which slab object the object resides */
1367 static struct page
*get_object_page(const void *x
)
1369 struct page
*page
= virt_to_head_page(x
);
1371 if (!PageSlab(page
))
1378 * kmem_cache_open produces objects aligned at "size" and the first object
1379 * is placed at offset 0 in the slab (We have no metainformation on the
1380 * slab, all slabs are in essence "off slab").
1382 * In order to get the desired alignment one just needs to align the
1385 * Notice that the allocation order determines the sizes of the per cpu
1386 * caches. Each processor has always one slab available for allocations.
1387 * Increasing the allocation order reduces the number of times that slabs
1388 * must be moved on and off the partial lists and therefore may influence
1391 * The offset is used to relocate the free list link in each object. It is
1392 * therefore possible to move the free list link behind the object. This
1393 * is necessary for RCU to work properly and also useful for debugging.
1397 * Mininum / Maximum order of slab pages. This influences locking overhead
1398 * and slab fragmentation. A higher order reduces the number of partial slabs
1399 * and increases the number of allocations possible without having to
1400 * take the list_lock.
1402 static int slub_min_order
;
1403 static int slub_max_order
= DEFAULT_MAX_ORDER
;
1406 * Minimum number of objects per slab. This is necessary in order to
1407 * reduce locking overhead. Similar to the queue size in SLAB.
1409 static int slub_min_objects
= DEFAULT_MIN_OBJECTS
;
1412 * Merge control. If this is set then no merging of slab caches will occur.
1414 static int slub_nomerge
;
1419 static int slub_debug
;
1421 static char *slub_debug_slabs
;
1424 * Calculate the order of allocation given an slab object size.
1426 * The order of allocation has significant impact on other elements
1427 * of the system. Generally order 0 allocations should be preferred
1428 * since they do not cause fragmentation in the page allocator. Larger
1429 * objects may have problems with order 0 because there may be too much
1430 * space left unused in a slab. We go to a higher order if more than 1/8th
1431 * of the slab would be wasted.
1433 * In order to reach satisfactory performance we must ensure that
1434 * a minimum number of objects is in one slab. Otherwise we may
1435 * generate too much activity on the partial lists. This is less a
1436 * concern for large slabs though. slub_max_order specifies the order
1437 * where we begin to stop considering the number of objects in a slab.
1439 * Higher order allocations also allow the placement of more objects
1440 * in a slab and thereby reduce object handling overhead. If the user
1441 * has requested a higher mininum order then we start with that one
1444 static int calculate_order(int size
)
1449 for (order
= max(slub_min_order
, fls(size
- 1) - PAGE_SHIFT
);
1450 order
< MAX_ORDER
; order
++) {
1451 unsigned long slab_size
= PAGE_SIZE
<< order
;
1453 if (slub_max_order
> order
&&
1454 slab_size
< slub_min_objects
* size
)
1457 if (slab_size
< size
)
1460 rem
= slab_size
% size
;
1462 if (rem
<= (PAGE_SIZE
<< order
) / 8)
1466 if (order
>= MAX_ORDER
)
1472 * Function to figure out which alignment to use from the
1473 * various ways of specifying it.
1475 static unsigned long calculate_alignment(unsigned long flags
,
1476 unsigned long align
, unsigned long size
)
1479 * If the user wants hardware cache aligned objects then
1480 * follow that suggestion if the object is sufficiently
1483 * The hardware cache alignment cannot override the
1484 * specified alignment though. If that is greater
1487 if ((flags
& SLAB_HWCACHE_ALIGN
) &&
1488 size
> cache_line_size() / 2)
1489 return max_t(unsigned long, align
, cache_line_size());
1491 if (align
< ARCH_SLAB_MINALIGN
)
1492 return ARCH_SLAB_MINALIGN
;
1494 return ALIGN(align
, sizeof(void *));
1497 static void init_kmem_cache_node(struct kmem_cache_node
*n
)
1500 atomic_long_set(&n
->nr_slabs
, 0);
1501 spin_lock_init(&n
->list_lock
);
1502 INIT_LIST_HEAD(&n
->partial
);
1503 INIT_LIST_HEAD(&n
->full
);
1508 * No kmalloc_node yet so do it by hand. We know that this is the first
1509 * slab on the node for this slabcache. There are no concurrent accesses
1512 * Note that this function only works on the kmalloc_node_cache
1513 * when allocating for the kmalloc_node_cache.
1515 static struct kmem_cache_node
* __init
early_kmem_cache_node_alloc(gfp_t gfpflags
,
1519 struct kmem_cache_node
*n
;
1521 BUG_ON(kmalloc_caches
->size
< sizeof(struct kmem_cache_node
));
1523 page
= new_slab(kmalloc_caches
, gfpflags
| GFP_THISNODE
, node
);
1524 /* new_slab() disables interupts */
1530 page
->freelist
= get_freepointer(kmalloc_caches
, n
);
1532 kmalloc_caches
->node
[node
] = n
;
1533 init_object(kmalloc_caches
, n
, 1);
1534 init_kmem_cache_node(n
);
1535 atomic_long_inc(&n
->nr_slabs
);
1536 add_partial(n
, page
);
1540 static void free_kmem_cache_nodes(struct kmem_cache
*s
)
1544 for_each_online_node(node
) {
1545 struct kmem_cache_node
*n
= s
->node
[node
];
1546 if (n
&& n
!= &s
->local_node
)
1547 kmem_cache_free(kmalloc_caches
, n
);
1548 s
->node
[node
] = NULL
;
1552 static int init_kmem_cache_nodes(struct kmem_cache
*s
, gfp_t gfpflags
)
1557 if (slab_state
>= UP
)
1558 local_node
= page_to_nid(virt_to_page(s
));
1562 for_each_online_node(node
) {
1563 struct kmem_cache_node
*n
;
1565 if (local_node
== node
)
1568 if (slab_state
== DOWN
) {
1569 n
= early_kmem_cache_node_alloc(gfpflags
,
1573 n
= kmem_cache_alloc_node(kmalloc_caches
,
1577 free_kmem_cache_nodes(s
);
1583 init_kmem_cache_node(n
);
1588 static void free_kmem_cache_nodes(struct kmem_cache
*s
)
1592 static int init_kmem_cache_nodes(struct kmem_cache
*s
, gfp_t gfpflags
)
1594 init_kmem_cache_node(&s
->local_node
);
1600 * calculate_sizes() determines the order and the distribution of data within
1603 static int calculate_sizes(struct kmem_cache
*s
)
1605 unsigned long flags
= s
->flags
;
1606 unsigned long size
= s
->objsize
;
1607 unsigned long align
= s
->align
;
1610 * Determine if we can poison the object itself. If the user of
1611 * the slab may touch the object after free or before allocation
1612 * then we should never poison the object itself.
1614 if ((flags
& SLAB_POISON
) && !(flags
& SLAB_DESTROY_BY_RCU
) &&
1615 !s
->ctor
&& !s
->dtor
)
1616 s
->flags
|= __OBJECT_POISON
;
1618 s
->flags
&= ~__OBJECT_POISON
;
1621 * Round up object size to the next word boundary. We can only
1622 * place the free pointer at word boundaries and this determines
1623 * the possible location of the free pointer.
1625 size
= ALIGN(size
, sizeof(void *));
1628 * If we are redzoning then check if there is some space between the
1629 * end of the object and the free pointer. If not then add an
1630 * additional word, so that we can establish a redzone between
1631 * the object and the freepointer to be able to check for overwrites.
1633 if ((flags
& SLAB_RED_ZONE
) && size
== s
->objsize
)
1634 size
+= sizeof(void *);
1637 * With that we have determined how much of the slab is in actual
1638 * use by the object. This is the potential offset to the free
1643 if (((flags
& (SLAB_DESTROY_BY_RCU
| SLAB_POISON
)) ||
1644 s
->ctor
|| s
->dtor
)) {
1646 * Relocate free pointer after the object if it is not
1647 * permitted to overwrite the first word of the object on
1650 * This is the case if we do RCU, have a constructor or
1651 * destructor or are poisoning the objects.
1654 size
+= sizeof(void *);
1657 if (flags
& SLAB_STORE_USER
)
1659 * Need to store information about allocs and frees after
1662 size
+= 2 * sizeof(struct track
);
1664 if (flags
& SLAB_RED_ZONE
)
1666 * Add some empty padding so that we can catch
1667 * overwrites from earlier objects rather than let
1668 * tracking information or the free pointer be
1669 * corrupted if an user writes before the start
1672 size
+= sizeof(void *);
1674 * Determine the alignment based on various parameters that the
1675 * user specified and the dynamic determination of cache line size
1678 align
= calculate_alignment(flags
, align
, s
->objsize
);
1681 * SLUB stores one object immediately after another beginning from
1682 * offset 0. In order to align the objects we have to simply size
1683 * each object to conform to the alignment.
1685 size
= ALIGN(size
, align
);
1688 s
->order
= calculate_order(size
);
1693 * Determine the number of objects per slab
1695 s
->objects
= (PAGE_SIZE
<< s
->order
) / size
;
1698 * Verify that the number of objects is within permitted limits.
1699 * The page->inuse field is only 16 bit wide! So we cannot have
1700 * more than 64k objects per slab.
1702 if (!s
->objects
|| s
->objects
> 65535)
1708 static int __init
finish_bootstrap(void)
1710 struct list_head
*h
;
1715 list_for_each(h
, &slab_caches
) {
1716 struct kmem_cache
*s
=
1717 container_of(h
, struct kmem_cache
, list
);
1719 err
= sysfs_slab_add(s
);
1725 static int kmem_cache_open(struct kmem_cache
*s
, gfp_t gfpflags
,
1726 const char *name
, size_t size
,
1727 size_t align
, unsigned long flags
,
1728 void (*ctor
)(void *, struct kmem_cache
*, unsigned long),
1729 void (*dtor
)(void *, struct kmem_cache
*, unsigned long))
1731 memset(s
, 0, kmem_size
);
1740 * The page->offset field is only 16 bit wide. This is an offset
1741 * in units of words from the beginning of an object. If the slab
1742 * size is bigger then we cannot move the free pointer behind the
1745 * On 32 bit platforms the limit is 256k. On 64bit platforms
1746 * the limit is 512k.
1748 * Debugging or ctor/dtors may create a need to move the free
1749 * pointer. Fail if this happens.
1751 if (s
->size
>= 65535 * sizeof(void *)) {
1752 BUG_ON(flags
& (SLAB_RED_ZONE
| SLAB_POISON
|
1753 SLAB_STORE_USER
| SLAB_DESTROY_BY_RCU
));
1754 BUG_ON(ctor
|| dtor
);
1758 * Enable debugging if selected on the kernel commandline.
1760 if (slub_debug
&& (!slub_debug_slabs
||
1761 strncmp(slub_debug_slabs
, name
,
1762 strlen(slub_debug_slabs
)) == 0))
1763 s
->flags
|= slub_debug
;
1765 if (!calculate_sizes(s
))
1770 s
->defrag_ratio
= 100;
1773 if (init_kmem_cache_nodes(s
, gfpflags
& ~SLUB_DMA
))
1776 if (flags
& SLAB_PANIC
)
1777 panic("Cannot create slab %s size=%lu realsize=%u "
1778 "order=%u offset=%u flags=%lx\n",
1779 s
->name
, (unsigned long)size
, s
->size
, s
->order
,
1783 EXPORT_SYMBOL(kmem_cache_open
);
1786 * Check if a given pointer is valid
1788 int kmem_ptr_validate(struct kmem_cache
*s
, const void *object
)
1793 page
= get_object_page(object
);
1795 if (!page
|| s
!= page
->slab
)
1796 /* No slab or wrong slab */
1799 addr
= page_address(page
);
1800 if (object
< addr
|| object
>= addr
+ s
->objects
* s
->size
)
1804 if ((object
- addr
) % s
->size
)
1805 /* Improperly aligned */
1809 * We could also check if the object is on the slabs freelist.
1810 * But this would be too expensive and it seems that the main
1811 * purpose of kmem_ptr_valid is to check if the object belongs
1812 * to a certain slab.
1816 EXPORT_SYMBOL(kmem_ptr_validate
);
1819 * Determine the size of a slab object
1821 unsigned int kmem_cache_size(struct kmem_cache
*s
)
1825 EXPORT_SYMBOL(kmem_cache_size
);
1827 const char *kmem_cache_name(struct kmem_cache
*s
)
1831 EXPORT_SYMBOL(kmem_cache_name
);
1834 * Attempt to free all slabs on a node
1836 static int free_list(struct kmem_cache
*s
, struct kmem_cache_node
*n
,
1837 struct list_head
*list
)
1839 int slabs_inuse
= 0;
1840 unsigned long flags
;
1841 struct page
*page
, *h
;
1843 spin_lock_irqsave(&n
->list_lock
, flags
);
1844 list_for_each_entry_safe(page
, h
, list
, lru
)
1846 list_del(&page
->lru
);
1847 discard_slab(s
, page
);
1850 spin_unlock_irqrestore(&n
->list_lock
, flags
);
1855 * Release all resources used by slab cache
1857 static int kmem_cache_close(struct kmem_cache
*s
)
1863 /* Attempt to free all objects */
1864 for_each_online_node(node
) {
1865 struct kmem_cache_node
*n
= get_node(s
, node
);
1867 n
->nr_partial
-= free_list(s
, n
, &n
->partial
);
1868 if (atomic_long_read(&n
->nr_slabs
))
1871 free_kmem_cache_nodes(s
);
1876 * Close a cache and release the kmem_cache structure
1877 * (must be used for caches created using kmem_cache_create)
1879 void kmem_cache_destroy(struct kmem_cache
*s
)
1881 down_write(&slub_lock
);
1885 if (kmem_cache_close(s
))
1887 sysfs_slab_remove(s
);
1890 up_write(&slub_lock
);
1892 EXPORT_SYMBOL(kmem_cache_destroy
);
1894 /********************************************************************
1896 *******************************************************************/
1898 struct kmem_cache kmalloc_caches
[KMALLOC_SHIFT_HIGH
+ 1] __cacheline_aligned
;
1899 EXPORT_SYMBOL(kmalloc_caches
);
1901 #ifdef CONFIG_ZONE_DMA
1902 static struct kmem_cache
*kmalloc_caches_dma
[KMALLOC_SHIFT_HIGH
+ 1];
1905 static int __init
setup_slub_min_order(char *str
)
1907 get_option (&str
, &slub_min_order
);
1912 __setup("slub_min_order=", setup_slub_min_order
);
1914 static int __init
setup_slub_max_order(char *str
)
1916 get_option (&str
, &slub_max_order
);
1921 __setup("slub_max_order=", setup_slub_max_order
);
1923 static int __init
setup_slub_min_objects(char *str
)
1925 get_option (&str
, &slub_min_objects
);
1930 __setup("slub_min_objects=", setup_slub_min_objects
);
1932 static int __init
setup_slub_nomerge(char *str
)
1938 __setup("slub_nomerge", setup_slub_nomerge
);
1940 static int __init
setup_slub_debug(char *str
)
1942 if (!str
|| *str
!= '=')
1943 slub_debug
= DEBUG_DEFAULT_FLAGS
;
1946 if (*str
== 0 || *str
== ',')
1947 slub_debug
= DEBUG_DEFAULT_FLAGS
;
1949 for( ;*str
&& *str
!= ','; str
++)
1951 case 'f' : case 'F' :
1952 slub_debug
|= SLAB_DEBUG_FREE
;
1954 case 'z' : case 'Z' :
1955 slub_debug
|= SLAB_RED_ZONE
;
1957 case 'p' : case 'P' :
1958 slub_debug
|= SLAB_POISON
;
1960 case 'u' : case 'U' :
1961 slub_debug
|= SLAB_STORE_USER
;
1963 case 't' : case 'T' :
1964 slub_debug
|= SLAB_TRACE
;
1967 printk(KERN_ERR
"slub_debug option '%c' "
1968 "unknown. skipped\n",*str
);
1973 slub_debug_slabs
= str
+ 1;
1977 __setup("slub_debug", setup_slub_debug
);
1979 static struct kmem_cache
*create_kmalloc_cache(struct kmem_cache
*s
,
1980 const char *name
, int size
, gfp_t gfp_flags
)
1982 unsigned int flags
= 0;
1984 if (gfp_flags
& SLUB_DMA
)
1985 flags
= SLAB_CACHE_DMA
;
1987 down_write(&slub_lock
);
1988 if (!kmem_cache_open(s
, gfp_flags
, name
, size
, ARCH_KMALLOC_MINALIGN
,
1992 list_add(&s
->list
, &slab_caches
);
1993 up_write(&slub_lock
);
1994 if (sysfs_slab_add(s
))
1999 panic("Creation of kmalloc slab %s size=%d failed.\n", name
, size
);
2002 static struct kmem_cache
*get_slab(size_t size
, gfp_t flags
)
2004 int index
= kmalloc_index(size
);
2009 /* Allocation too large? */
2012 #ifdef CONFIG_ZONE_DMA
2013 if ((flags
& SLUB_DMA
)) {
2014 struct kmem_cache
*s
;
2015 struct kmem_cache
*x
;
2019 s
= kmalloc_caches_dma
[index
];
2023 /* Dynamically create dma cache */
2024 x
= kmalloc(kmem_size
, flags
& ~SLUB_DMA
);
2026 panic("Unable to allocate memory for dma cache\n");
2028 if (index
<= KMALLOC_SHIFT_HIGH
)
2029 realsize
= 1 << index
;
2037 text
= kasprintf(flags
& ~SLUB_DMA
, "kmalloc_dma-%d",
2038 (unsigned int)realsize
);
2039 s
= create_kmalloc_cache(x
, text
, realsize
, flags
);
2040 kmalloc_caches_dma
[index
] = s
;
2044 return &kmalloc_caches
[index
];
2047 void *__kmalloc(size_t size
, gfp_t flags
)
2049 struct kmem_cache
*s
= get_slab(size
, flags
);
2052 return slab_alloc(s
, flags
, -1, __builtin_return_address(0));
2055 EXPORT_SYMBOL(__kmalloc
);
2058 void *__kmalloc_node(size_t size
, gfp_t flags
, int node
)
2060 struct kmem_cache
*s
= get_slab(size
, flags
);
2063 return slab_alloc(s
, flags
, node
, __builtin_return_address(0));
2066 EXPORT_SYMBOL(__kmalloc_node
);
2069 size_t ksize(const void *object
)
2071 struct page
*page
= get_object_page(object
);
2072 struct kmem_cache
*s
;
2079 * Debugging requires use of the padding between object
2080 * and whatever may come after it.
2082 if (s
->flags
& (SLAB_RED_ZONE
| SLAB_POISON
))
2086 * If we have the need to store the freelist pointer
2087 * back there or track user information then we can
2088 * only use the space before that information.
2090 if (s
->flags
& (SLAB_DESTROY_BY_RCU
| SLAB_STORE_USER
))
2094 * Else we can use all the padding etc for the allocation
2098 EXPORT_SYMBOL(ksize
);
2100 void kfree(const void *x
)
2102 struct kmem_cache
*s
;
2108 page
= virt_to_head_page(x
);
2111 slab_free(s
, page
, (void *)x
, __builtin_return_address(0));
2113 EXPORT_SYMBOL(kfree
);
2116 * kmem_cache_shrink removes empty slabs from the partial lists
2117 * and then sorts the partially allocated slabs by the number
2118 * of items in use. The slabs with the most items in use
2119 * come first. New allocations will remove these from the
2120 * partial list because they are full. The slabs with the
2121 * least items are placed last. If it happens that the objects
2122 * are freed then the page can be returned to the page allocator.
2124 int kmem_cache_shrink(struct kmem_cache
*s
)
2128 struct kmem_cache_node
*n
;
2131 struct list_head
*slabs_by_inuse
=
2132 kmalloc(sizeof(struct list_head
) * s
->objects
, GFP_KERNEL
);
2133 unsigned long flags
;
2135 if (!slabs_by_inuse
)
2139 for_each_online_node(node
) {
2140 n
= get_node(s
, node
);
2145 for (i
= 0; i
< s
->objects
; i
++)
2146 INIT_LIST_HEAD(slabs_by_inuse
+ i
);
2148 spin_lock_irqsave(&n
->list_lock
, flags
);
2151 * Build lists indexed by the items in use in
2152 * each slab or free slabs if empty.
2154 * Note that concurrent frees may occur while
2155 * we hold the list_lock. page->inuse here is
2158 list_for_each_entry_safe(page
, t
, &n
->partial
, lru
) {
2159 if (!page
->inuse
&& slab_trylock(page
)) {
2161 * Must hold slab lock here because slab_free
2162 * may have freed the last object and be
2163 * waiting to release the slab.
2165 list_del(&page
->lru
);
2168 discard_slab(s
, page
);
2170 if (n
->nr_partial
> MAX_PARTIAL
)
2171 list_move(&page
->lru
,
2172 slabs_by_inuse
+ page
->inuse
);
2176 if (n
->nr_partial
<= MAX_PARTIAL
)
2180 * Rebuild the partial list with the slabs filled up
2181 * most first and the least used slabs at the end.
2183 for (i
= s
->objects
- 1; i
>= 0; i
--)
2184 list_splice(slabs_by_inuse
+ i
, n
->partial
.prev
);
2187 spin_unlock_irqrestore(&n
->list_lock
, flags
);
2190 kfree(slabs_by_inuse
);
2193 EXPORT_SYMBOL(kmem_cache_shrink
);
2196 * krealloc - reallocate memory. The contents will remain unchanged.
2198 * @p: object to reallocate memory for.
2199 * @new_size: how many bytes of memory are required.
2200 * @flags: the type of memory to allocate.
2202 * The contents of the object pointed to are preserved up to the
2203 * lesser of the new and old sizes. If @p is %NULL, krealloc()
2204 * behaves exactly like kmalloc(). If @size is 0 and @p is not a
2205 * %NULL pointer, the object pointed to is freed.
2207 void *krealloc(const void *p
, size_t new_size
, gfp_t flags
)
2209 struct kmem_cache
*new_cache
;
2214 return kmalloc(new_size
, flags
);
2216 if (unlikely(!new_size
)) {
2221 page
= virt_to_head_page(p
);
2223 new_cache
= get_slab(new_size
, flags
);
2226 * If new size fits in the current cache, bail out.
2228 if (likely(page
->slab
== new_cache
))
2231 ret
= kmalloc(new_size
, flags
);
2233 memcpy(ret
, p
, min(new_size
, ksize(p
)));
2238 EXPORT_SYMBOL(krealloc
);
2240 /********************************************************************
2241 * Basic setup of slabs
2242 *******************************************************************/
2244 void __init
kmem_cache_init(void)
2250 * Must first have the slab cache available for the allocations of the
2251 * struct kmalloc_cache_node's. There is special bootstrap code in
2252 * kmem_cache_open for slab_state == DOWN.
2254 create_kmalloc_cache(&kmalloc_caches
[0], "kmem_cache_node",
2255 sizeof(struct kmem_cache_node
), GFP_KERNEL
);
2258 /* Able to allocate the per node structures */
2259 slab_state
= PARTIAL
;
2261 /* Caches that are not of the two-to-the-power-of size */
2262 create_kmalloc_cache(&kmalloc_caches
[1],
2263 "kmalloc-96", 96, GFP_KERNEL
);
2264 create_kmalloc_cache(&kmalloc_caches
[2],
2265 "kmalloc-192", 192, GFP_KERNEL
);
2267 for (i
= KMALLOC_SHIFT_LOW
; i
<= KMALLOC_SHIFT_HIGH
; i
++)
2268 create_kmalloc_cache(&kmalloc_caches
[i
],
2269 "kmalloc", 1 << i
, GFP_KERNEL
);
2273 /* Provide the correct kmalloc names now that the caches are up */
2274 for (i
= KMALLOC_SHIFT_LOW
; i
<= KMALLOC_SHIFT_HIGH
; i
++)
2275 kmalloc_caches
[i
]. name
=
2276 kasprintf(GFP_KERNEL
, "kmalloc-%d", 1 << i
);
2279 register_cpu_notifier(&slab_notifier
);
2282 if (nr_cpu_ids
) /* Remove when nr_cpu_ids is fixed upstream ! */
2283 kmem_size
= offsetof(struct kmem_cache
, cpu_slab
)
2284 + nr_cpu_ids
* sizeof(struct page
*);
2286 printk(KERN_INFO
"SLUB: Genslabs=%d, HWalign=%d, Order=%d-%d, MinObjects=%d,"
2287 " Processors=%d, Nodes=%d\n",
2288 KMALLOC_SHIFT_HIGH
, cache_line_size(),
2289 slub_min_order
, slub_max_order
, slub_min_objects
,
2290 nr_cpu_ids
, nr_node_ids
);
2294 * Find a mergeable slab cache
2296 static int slab_unmergeable(struct kmem_cache
*s
)
2298 if (slub_nomerge
|| (s
->flags
& SLUB_NEVER_MERGE
))
2301 if (s
->ctor
|| s
->dtor
)
2307 static struct kmem_cache
*find_mergeable(size_t size
,
2308 size_t align
, unsigned long flags
,
2309 void (*ctor
)(void *, struct kmem_cache
*, unsigned long),
2310 void (*dtor
)(void *, struct kmem_cache
*, unsigned long))
2312 struct list_head
*h
;
2314 if (slub_nomerge
|| (flags
& SLUB_NEVER_MERGE
))
2320 size
= ALIGN(size
, sizeof(void *));
2321 align
= calculate_alignment(flags
, align
, size
);
2322 size
= ALIGN(size
, align
);
2324 list_for_each(h
, &slab_caches
) {
2325 struct kmem_cache
*s
=
2326 container_of(h
, struct kmem_cache
, list
);
2328 if (slab_unmergeable(s
))
2334 if (((flags
| slub_debug
) & SLUB_MERGE_SAME
) !=
2335 (s
->flags
& SLUB_MERGE_SAME
))
2338 * Check if alignment is compatible.
2339 * Courtesy of Adrian Drzewiecki
2341 if ((s
->size
& ~(align
-1)) != s
->size
)
2344 if (s
->size
- size
>= sizeof(void *))
2352 struct kmem_cache
*kmem_cache_create(const char *name
, size_t size
,
2353 size_t align
, unsigned long flags
,
2354 void (*ctor
)(void *, struct kmem_cache
*, unsigned long),
2355 void (*dtor
)(void *, struct kmem_cache
*, unsigned long))
2357 struct kmem_cache
*s
;
2359 down_write(&slub_lock
);
2360 s
= find_mergeable(size
, align
, flags
, dtor
, ctor
);
2364 * Adjust the object sizes so that we clear
2365 * the complete object on kzalloc.
2367 s
->objsize
= max(s
->objsize
, (int)size
);
2368 s
->inuse
= max_t(int, s
->inuse
, ALIGN(size
, sizeof(void *)));
2369 if (sysfs_slab_alias(s
, name
))
2372 s
= kmalloc(kmem_size
, GFP_KERNEL
);
2373 if (s
&& kmem_cache_open(s
, GFP_KERNEL
, name
,
2374 size
, align
, flags
, ctor
, dtor
)) {
2375 if (sysfs_slab_add(s
)) {
2379 list_add(&s
->list
, &slab_caches
);
2383 up_write(&slub_lock
);
2387 up_write(&slub_lock
);
2388 if (flags
& SLAB_PANIC
)
2389 panic("Cannot create slabcache %s\n", name
);
2394 EXPORT_SYMBOL(kmem_cache_create
);
2396 void *kmem_cache_zalloc(struct kmem_cache
*s
, gfp_t flags
)
2400 x
= slab_alloc(s
, flags
, -1, __builtin_return_address(0));
2402 memset(x
, 0, s
->objsize
);
2405 EXPORT_SYMBOL(kmem_cache_zalloc
);
2408 static void for_all_slabs(void (*func
)(struct kmem_cache
*, int), int cpu
)
2410 struct list_head
*h
;
2412 down_read(&slub_lock
);
2413 list_for_each(h
, &slab_caches
) {
2414 struct kmem_cache
*s
=
2415 container_of(h
, struct kmem_cache
, list
);
2419 up_read(&slub_lock
);
2423 * Use the cpu notifier to insure that the slab are flushed
2426 static int __cpuinit
slab_cpuup_callback(struct notifier_block
*nfb
,
2427 unsigned long action
, void *hcpu
)
2429 long cpu
= (long)hcpu
;
2432 case CPU_UP_CANCELED
:
2434 for_all_slabs(__flush_cpu_slab
, cpu
);
2442 static struct notifier_block __cpuinitdata slab_notifier
=
2443 { &slab_cpuup_callback
, NULL
, 0 };
2449 /*****************************************************************
2450 * Generic reaper used to support the page allocator
2451 * (the cpu slabs are reaped by a per slab workqueue).
2453 * Maybe move this to the page allocator?
2454 ****************************************************************/
2456 static DEFINE_PER_CPU(unsigned long, reap_node
);
2458 static void init_reap_node(int cpu
)
2462 node
= next_node(cpu_to_node(cpu
), node_online_map
);
2463 if (node
== MAX_NUMNODES
)
2464 node
= first_node(node_online_map
);
2466 __get_cpu_var(reap_node
) = node
;
2469 static void next_reap_node(void)
2471 int node
= __get_cpu_var(reap_node
);
2474 * Also drain per cpu pages on remote zones
2476 if (node
!= numa_node_id())
2477 drain_node_pages(node
);
2479 node
= next_node(node
, node_online_map
);
2480 if (unlikely(node
>= MAX_NUMNODES
))
2481 node
= first_node(node_online_map
);
2482 __get_cpu_var(reap_node
) = node
;
2485 #define init_reap_node(cpu) do { } while (0)
2486 #define next_reap_node(void) do { } while (0)
2489 #define REAPTIMEOUT_CPUC (2*HZ)
2492 static DEFINE_PER_CPU(struct delayed_work
, reap_work
);
2494 static void cache_reap(struct work_struct
*unused
)
2497 refresh_cpu_vm_stats(smp_processor_id());
2498 schedule_delayed_work(&__get_cpu_var(reap_work
),
2502 static void __devinit
start_cpu_timer(int cpu
)
2504 struct delayed_work
*reap_work
= &per_cpu(reap_work
, cpu
);
2507 * When this gets called from do_initcalls via cpucache_init(),
2508 * init_workqueues() has already run, so keventd will be setup
2511 if (keventd_up() && reap_work
->work
.func
== NULL
) {
2512 init_reap_node(cpu
);
2513 INIT_DELAYED_WORK(reap_work
, cache_reap
);
2514 schedule_delayed_work_on(cpu
, reap_work
, HZ
+ 3 * cpu
);
2518 static int __init
cpucache_init(void)
2523 * Register the timers that drain pcp pages and update vm statistics
2525 for_each_online_cpu(cpu
)
2526 start_cpu_timer(cpu
);
2529 __initcall(cpucache_init
);
2532 #ifdef SLUB_RESILIENCY_TEST
2533 static unsigned long validate_slab_cache(struct kmem_cache
*s
);
2535 static void resiliency_test(void)
2539 printk(KERN_ERR
"SLUB resiliency testing\n");
2540 printk(KERN_ERR
"-----------------------\n");
2541 printk(KERN_ERR
"A. Corruption after allocation\n");
2543 p
= kzalloc(16, GFP_KERNEL
);
2545 printk(KERN_ERR
"\n1. kmalloc-16: Clobber Redzone/next pointer"
2546 " 0x12->0x%p\n\n", p
+ 16);
2548 validate_slab_cache(kmalloc_caches
+ 4);
2550 /* Hmmm... The next two are dangerous */
2551 p
= kzalloc(32, GFP_KERNEL
);
2552 p
[32 + sizeof(void *)] = 0x34;
2553 printk(KERN_ERR
"\n2. kmalloc-32: Clobber next pointer/next slab"
2554 " 0x34 -> -0x%p\n", p
);
2555 printk(KERN_ERR
"If allocated object is overwritten then not detectable\n\n");
2557 validate_slab_cache(kmalloc_caches
+ 5);
2558 p
= kzalloc(64, GFP_KERNEL
);
2559 p
+= 64 + (get_cycles() & 0xff) * sizeof(void *);
2561 printk(KERN_ERR
"\n3. kmalloc-64: corrupting random byte 0x56->0x%p\n",
2563 printk(KERN_ERR
"If allocated object is overwritten then not detectable\n\n");
2564 validate_slab_cache(kmalloc_caches
+ 6);
2566 printk(KERN_ERR
"\nB. Corruption after free\n");
2567 p
= kzalloc(128, GFP_KERNEL
);
2570 printk(KERN_ERR
"1. kmalloc-128: Clobber first word 0x78->0x%p\n\n", p
);
2571 validate_slab_cache(kmalloc_caches
+ 7);
2573 p
= kzalloc(256, GFP_KERNEL
);
2576 printk(KERN_ERR
"\n2. kmalloc-256: Clobber 50th byte 0x9a->0x%p\n\n", p
);
2577 validate_slab_cache(kmalloc_caches
+ 8);
2579 p
= kzalloc(512, GFP_KERNEL
);
2582 printk(KERN_ERR
"\n3. kmalloc-512: Clobber redzone 0xab->0x%p\n\n", p
);
2583 validate_slab_cache(kmalloc_caches
+ 9);
2586 static void resiliency_test(void) {};
2590 * These are not as efficient as kmalloc for the non debug case.
2591 * We do not have the page struct available so we have to touch one
2592 * cacheline in struct kmem_cache to check slab flags.
2594 void *__kmalloc_track_caller(size_t size
, gfp_t gfpflags
, void *caller
)
2596 struct kmem_cache
*s
= get_slab(size
, gfpflags
);
2601 return slab_alloc(s
, gfpflags
, -1, caller
);
2604 void *__kmalloc_node_track_caller(size_t size
, gfp_t gfpflags
,
2605 int node
, void *caller
)
2607 struct kmem_cache
*s
= get_slab(size
, gfpflags
);
2612 return slab_alloc(s
, gfpflags
, node
, caller
);
2617 static int validate_slab(struct kmem_cache
*s
, struct page
*page
)
2620 void *addr
= page_address(page
);
2621 unsigned long map
[BITS_TO_LONGS(s
->objects
)];
2623 if (!check_slab(s
, page
) ||
2624 !on_freelist(s
, page
, NULL
))
2627 /* Now we know that a valid freelist exists */
2628 bitmap_zero(map
, s
->objects
);
2630 for(p
= page
->freelist
; p
; p
= get_freepointer(s
, p
)) {
2631 set_bit((p
- addr
) / s
->size
, map
);
2632 if (!check_object(s
, page
, p
, 0))
2636 for(p
= addr
; p
< addr
+ s
->objects
* s
->size
; p
+= s
->size
)
2637 if (!test_bit((p
- addr
) / s
->size
, map
))
2638 if (!check_object(s
, page
, p
, 1))
2643 static void validate_slab_slab(struct kmem_cache
*s
, struct page
*page
)
2645 if (slab_trylock(page
)) {
2646 validate_slab(s
, page
);
2649 printk(KERN_INFO
"SLUB %s: Skipped busy slab 0x%p\n",
2652 if (s
->flags
& DEBUG_DEFAULT_FLAGS
) {
2653 if (!PageError(page
))
2654 printk(KERN_ERR
"SLUB %s: PageError not set "
2655 "on slab 0x%p\n", s
->name
, page
);
2657 if (PageError(page
))
2658 printk(KERN_ERR
"SLUB %s: PageError set on "
2659 "slab 0x%p\n", s
->name
, page
);
2663 static int validate_slab_node(struct kmem_cache
*s
, struct kmem_cache_node
*n
)
2665 unsigned long count
= 0;
2667 unsigned long flags
;
2669 spin_lock_irqsave(&n
->list_lock
, flags
);
2671 list_for_each_entry(page
, &n
->partial
, lru
) {
2672 validate_slab_slab(s
, page
);
2675 if (count
!= n
->nr_partial
)
2676 printk(KERN_ERR
"SLUB %s: %ld partial slabs counted but "
2677 "counter=%ld\n", s
->name
, count
, n
->nr_partial
);
2679 if (!(s
->flags
& SLAB_STORE_USER
))
2682 list_for_each_entry(page
, &n
->full
, lru
) {
2683 validate_slab_slab(s
, page
);
2686 if (count
!= atomic_long_read(&n
->nr_slabs
))
2687 printk(KERN_ERR
"SLUB: %s %ld slabs counted but "
2688 "counter=%ld\n", s
->name
, count
,
2689 atomic_long_read(&n
->nr_slabs
));
2692 spin_unlock_irqrestore(&n
->list_lock
, flags
);
2696 static unsigned long validate_slab_cache(struct kmem_cache
*s
)
2699 unsigned long count
= 0;
2702 for_each_online_node(node
) {
2703 struct kmem_cache_node
*n
= get_node(s
, node
);
2705 count
+= validate_slab_node(s
, n
);
2711 * Generate lists of locations where slabcache objects are allocated
2716 unsigned long count
;
2722 unsigned long count
;
2723 struct location
*loc
;
2726 static void free_loc_track(struct loc_track
*t
)
2729 free_pages((unsigned long)t
->loc
,
2730 get_order(sizeof(struct location
) * t
->max
));
2733 static int alloc_loc_track(struct loc_track
*t
, unsigned long max
)
2739 max
= PAGE_SIZE
/ sizeof(struct location
);
2741 order
= get_order(sizeof(struct location
) * max
);
2743 l
= (void *)__get_free_pages(GFP_KERNEL
, order
);
2749 memcpy(l
, t
->loc
, sizeof(struct location
) * t
->count
);
2757 static int add_location(struct loc_track
*t
, struct kmem_cache
*s
,
2760 long start
, end
, pos
;
2768 pos
= start
+ (end
- start
+ 1) / 2;
2771 * There is nothing at "end". If we end up there
2772 * we need to add something to before end.
2777 caddr
= t
->loc
[pos
].addr
;
2778 if (addr
== caddr
) {
2779 t
->loc
[pos
].count
++;
2790 * Not found. Insert new tracking element
2792 if (t
->count
>= t
->max
&& !alloc_loc_track(t
, 2 * t
->max
))
2798 (t
->count
- pos
) * sizeof(struct location
));
2805 static void process_slab(struct loc_track
*t
, struct kmem_cache
*s
,
2806 struct page
*page
, enum track_item alloc
)
2808 void *addr
= page_address(page
);
2809 unsigned long map
[BITS_TO_LONGS(s
->objects
)];
2812 bitmap_zero(map
, s
->objects
);
2813 for (p
= page
->freelist
; p
; p
= get_freepointer(s
, p
))
2814 set_bit((p
- addr
) / s
->size
, map
);
2816 for (p
= addr
; p
< addr
+ s
->objects
* s
->size
; p
+= s
->size
)
2817 if (!test_bit((p
- addr
) / s
->size
, map
)) {
2818 void *addr
= get_track(s
, p
, alloc
)->addr
;
2820 add_location(t
, s
, addr
);
2824 static int list_locations(struct kmem_cache
*s
, char *buf
,
2825 enum track_item alloc
)
2835 /* Push back cpu slabs */
2838 for_each_online_node(node
) {
2839 struct kmem_cache_node
*n
= get_node(s
, node
);
2840 unsigned long flags
;
2843 if (!atomic_read(&n
->nr_slabs
))
2846 spin_lock_irqsave(&n
->list_lock
, flags
);
2847 list_for_each_entry(page
, &n
->partial
, lru
)
2848 process_slab(&t
, s
, page
, alloc
);
2849 list_for_each_entry(page
, &n
->full
, lru
)
2850 process_slab(&t
, s
, page
, alloc
);
2851 spin_unlock_irqrestore(&n
->list_lock
, flags
);
2854 for (i
= 0; i
< t
.count
; i
++) {
2855 void *addr
= t
.loc
[i
].addr
;
2857 if (n
> PAGE_SIZE
- 100)
2859 n
+= sprintf(buf
+ n
, "%7ld ", t
.loc
[i
].count
);
2861 n
+= sprint_symbol(buf
+ n
, (unsigned long)t
.loc
[i
].addr
);
2863 n
+= sprintf(buf
+ n
, "<not-available>");
2864 n
+= sprintf(buf
+ n
, "\n");
2869 n
+= sprintf(buf
, "No data\n");
2873 static unsigned long count_partial(struct kmem_cache_node
*n
)
2875 unsigned long flags
;
2876 unsigned long x
= 0;
2879 spin_lock_irqsave(&n
->list_lock
, flags
);
2880 list_for_each_entry(page
, &n
->partial
, lru
)
2882 spin_unlock_irqrestore(&n
->list_lock
, flags
);
2886 enum slab_stat_type
{
2893 #define SO_FULL (1 << SL_FULL)
2894 #define SO_PARTIAL (1 << SL_PARTIAL)
2895 #define SO_CPU (1 << SL_CPU)
2896 #define SO_OBJECTS (1 << SL_OBJECTS)
2898 static unsigned long slab_objects(struct kmem_cache
*s
,
2899 char *buf
, unsigned long flags
)
2901 unsigned long total
= 0;
2905 unsigned long *nodes
;
2906 unsigned long *per_cpu
;
2908 nodes
= kzalloc(2 * sizeof(unsigned long) * nr_node_ids
, GFP_KERNEL
);
2909 per_cpu
= nodes
+ nr_node_ids
;
2911 for_each_possible_cpu(cpu
) {
2912 struct page
*page
= s
->cpu_slab
[cpu
];
2916 node
= page_to_nid(page
);
2917 if (flags
& SO_CPU
) {
2920 if (flags
& SO_OBJECTS
)
2931 for_each_online_node(node
) {
2932 struct kmem_cache_node
*n
= get_node(s
, node
);
2934 if (flags
& SO_PARTIAL
) {
2935 if (flags
& SO_OBJECTS
)
2936 x
= count_partial(n
);
2943 if (flags
& SO_FULL
) {
2944 int full_slabs
= atomic_read(&n
->nr_slabs
)
2948 if (flags
& SO_OBJECTS
)
2949 x
= full_slabs
* s
->objects
;
2957 x
= sprintf(buf
, "%lu", total
);
2959 for_each_online_node(node
)
2961 x
+= sprintf(buf
+ x
, " N%d=%lu",
2965 return x
+ sprintf(buf
+ x
, "\n");
2968 static int any_slab_objects(struct kmem_cache
*s
)
2973 for_each_possible_cpu(cpu
)
2974 if (s
->cpu_slab
[cpu
])
2977 for_each_node(node
) {
2978 struct kmem_cache_node
*n
= get_node(s
, node
);
2980 if (n
->nr_partial
|| atomic_read(&n
->nr_slabs
))
2986 #define to_slab_attr(n) container_of(n, struct slab_attribute, attr)
2987 #define to_slab(n) container_of(n, struct kmem_cache, kobj);
2989 struct slab_attribute
{
2990 struct attribute attr
;
2991 ssize_t (*show
)(struct kmem_cache
*s
, char *buf
);
2992 ssize_t (*store
)(struct kmem_cache
*s
, const char *x
, size_t count
);
2995 #define SLAB_ATTR_RO(_name) \
2996 static struct slab_attribute _name##_attr = __ATTR_RO(_name)
2998 #define SLAB_ATTR(_name) \
2999 static struct slab_attribute _name##_attr = \
3000 __ATTR(_name, 0644, _name##_show, _name##_store)
3002 static ssize_t
slab_size_show(struct kmem_cache
*s
, char *buf
)
3004 return sprintf(buf
, "%d\n", s
->size
);
3006 SLAB_ATTR_RO(slab_size
);
3008 static ssize_t
align_show(struct kmem_cache
*s
, char *buf
)
3010 return sprintf(buf
, "%d\n", s
->align
);
3012 SLAB_ATTR_RO(align
);
3014 static ssize_t
object_size_show(struct kmem_cache
*s
, char *buf
)
3016 return sprintf(buf
, "%d\n", s
->objsize
);
3018 SLAB_ATTR_RO(object_size
);
3020 static ssize_t
objs_per_slab_show(struct kmem_cache
*s
, char *buf
)
3022 return sprintf(buf
, "%d\n", s
->objects
);
3024 SLAB_ATTR_RO(objs_per_slab
);
3026 static ssize_t
order_show(struct kmem_cache
*s
, char *buf
)
3028 return sprintf(buf
, "%d\n", s
->order
);
3030 SLAB_ATTR_RO(order
);
3032 static ssize_t
ctor_show(struct kmem_cache
*s
, char *buf
)
3035 int n
= sprint_symbol(buf
, (unsigned long)s
->ctor
);
3037 return n
+ sprintf(buf
+ n
, "\n");
3043 static ssize_t
dtor_show(struct kmem_cache
*s
, char *buf
)
3046 int n
= sprint_symbol(buf
, (unsigned long)s
->dtor
);
3048 return n
+ sprintf(buf
+ n
, "\n");
3054 static ssize_t
aliases_show(struct kmem_cache
*s
, char *buf
)
3056 return sprintf(buf
, "%d\n", s
->refcount
- 1);
3058 SLAB_ATTR_RO(aliases
);
3060 static ssize_t
slabs_show(struct kmem_cache
*s
, char *buf
)
3062 return slab_objects(s
, buf
, SO_FULL
|SO_PARTIAL
|SO_CPU
);
3064 SLAB_ATTR_RO(slabs
);
3066 static ssize_t
partial_show(struct kmem_cache
*s
, char *buf
)
3068 return slab_objects(s
, buf
, SO_PARTIAL
);
3070 SLAB_ATTR_RO(partial
);
3072 static ssize_t
cpu_slabs_show(struct kmem_cache
*s
, char *buf
)
3074 return slab_objects(s
, buf
, SO_CPU
);
3076 SLAB_ATTR_RO(cpu_slabs
);
3078 static ssize_t
objects_show(struct kmem_cache
*s
, char *buf
)
3080 return slab_objects(s
, buf
, SO_FULL
|SO_PARTIAL
|SO_CPU
|SO_OBJECTS
);
3082 SLAB_ATTR_RO(objects
);
3084 static ssize_t
sanity_checks_show(struct kmem_cache
*s
, char *buf
)
3086 return sprintf(buf
, "%d\n", !!(s
->flags
& SLAB_DEBUG_FREE
));
3089 static ssize_t
sanity_checks_store(struct kmem_cache
*s
,
3090 const char *buf
, size_t length
)
3092 s
->flags
&= ~SLAB_DEBUG_FREE
;
3094 s
->flags
|= SLAB_DEBUG_FREE
;
3097 SLAB_ATTR(sanity_checks
);
3099 static ssize_t
trace_show(struct kmem_cache
*s
, char *buf
)
3101 return sprintf(buf
, "%d\n", !!(s
->flags
& SLAB_TRACE
));
3104 static ssize_t
trace_store(struct kmem_cache
*s
, const char *buf
,
3107 s
->flags
&= ~SLAB_TRACE
;
3109 s
->flags
|= SLAB_TRACE
;
3114 static ssize_t
reclaim_account_show(struct kmem_cache
*s
, char *buf
)
3116 return sprintf(buf
, "%d\n", !!(s
->flags
& SLAB_RECLAIM_ACCOUNT
));
3119 static ssize_t
reclaim_account_store(struct kmem_cache
*s
,
3120 const char *buf
, size_t length
)
3122 s
->flags
&= ~SLAB_RECLAIM_ACCOUNT
;
3124 s
->flags
|= SLAB_RECLAIM_ACCOUNT
;
3127 SLAB_ATTR(reclaim_account
);
3129 static ssize_t
hwcache_align_show(struct kmem_cache
*s
, char *buf
)
3131 return sprintf(buf
, "%d\n", !!(s
->flags
& SLAB_HWCACHE_ALIGN
));
3133 SLAB_ATTR_RO(hwcache_align
);
3135 #ifdef CONFIG_ZONE_DMA
3136 static ssize_t
cache_dma_show(struct kmem_cache
*s
, char *buf
)
3138 return sprintf(buf
, "%d\n", !!(s
->flags
& SLAB_CACHE_DMA
));
3140 SLAB_ATTR_RO(cache_dma
);
3143 static ssize_t
destroy_by_rcu_show(struct kmem_cache
*s
, char *buf
)
3145 return sprintf(buf
, "%d\n", !!(s
->flags
& SLAB_DESTROY_BY_RCU
));
3147 SLAB_ATTR_RO(destroy_by_rcu
);
3149 static ssize_t
red_zone_show(struct kmem_cache
*s
, char *buf
)
3151 return sprintf(buf
, "%d\n", !!(s
->flags
& SLAB_RED_ZONE
));
3154 static ssize_t
red_zone_store(struct kmem_cache
*s
,
3155 const char *buf
, size_t length
)
3157 if (any_slab_objects(s
))
3160 s
->flags
&= ~SLAB_RED_ZONE
;
3162 s
->flags
|= SLAB_RED_ZONE
;
3166 SLAB_ATTR(red_zone
);
3168 static ssize_t
poison_show(struct kmem_cache
*s
, char *buf
)
3170 return sprintf(buf
, "%d\n", !!(s
->flags
& SLAB_POISON
));
3173 static ssize_t
poison_store(struct kmem_cache
*s
,
3174 const char *buf
, size_t length
)
3176 if (any_slab_objects(s
))
3179 s
->flags
&= ~SLAB_POISON
;
3181 s
->flags
|= SLAB_POISON
;
3187 static ssize_t
store_user_show(struct kmem_cache
*s
, char *buf
)
3189 return sprintf(buf
, "%d\n", !!(s
->flags
& SLAB_STORE_USER
));
3192 static ssize_t
store_user_store(struct kmem_cache
*s
,
3193 const char *buf
, size_t length
)
3195 if (any_slab_objects(s
))
3198 s
->flags
&= ~SLAB_STORE_USER
;
3200 s
->flags
|= SLAB_STORE_USER
;
3204 SLAB_ATTR(store_user
);
3206 static ssize_t
validate_show(struct kmem_cache
*s
, char *buf
)
3211 static ssize_t
validate_store(struct kmem_cache
*s
,
3212 const char *buf
, size_t length
)
3215 validate_slab_cache(s
);
3220 SLAB_ATTR(validate
);
3222 static ssize_t
shrink_show(struct kmem_cache
*s
, char *buf
)
3227 static ssize_t
shrink_store(struct kmem_cache
*s
,
3228 const char *buf
, size_t length
)
3230 if (buf
[0] == '1') {
3231 int rc
= kmem_cache_shrink(s
);
3241 static ssize_t
alloc_calls_show(struct kmem_cache
*s
, char *buf
)
3243 if (!(s
->flags
& SLAB_STORE_USER
))
3245 return list_locations(s
, buf
, TRACK_ALLOC
);
3247 SLAB_ATTR_RO(alloc_calls
);
3249 static ssize_t
free_calls_show(struct kmem_cache
*s
, char *buf
)
3251 if (!(s
->flags
& SLAB_STORE_USER
))
3253 return list_locations(s
, buf
, TRACK_FREE
);
3255 SLAB_ATTR_RO(free_calls
);
3258 static ssize_t
defrag_ratio_show(struct kmem_cache
*s
, char *buf
)
3260 return sprintf(buf
, "%d\n", s
->defrag_ratio
/ 10);
3263 static ssize_t
defrag_ratio_store(struct kmem_cache
*s
,
3264 const char *buf
, size_t length
)
3266 int n
= simple_strtoul(buf
, NULL
, 10);
3269 s
->defrag_ratio
= n
* 10;
3272 SLAB_ATTR(defrag_ratio
);
3275 static struct attribute
* slab_attrs
[] = {
3276 &slab_size_attr
.attr
,
3277 &object_size_attr
.attr
,
3278 &objs_per_slab_attr
.attr
,
3283 &cpu_slabs_attr
.attr
,
3288 &sanity_checks_attr
.attr
,
3290 &hwcache_align_attr
.attr
,
3291 &reclaim_account_attr
.attr
,
3292 &destroy_by_rcu_attr
.attr
,
3293 &red_zone_attr
.attr
,
3295 &store_user_attr
.attr
,
3296 &validate_attr
.attr
,
3298 &alloc_calls_attr
.attr
,
3299 &free_calls_attr
.attr
,
3300 #ifdef CONFIG_ZONE_DMA
3301 &cache_dma_attr
.attr
,
3304 &defrag_ratio_attr
.attr
,
3309 static struct attribute_group slab_attr_group
= {
3310 .attrs
= slab_attrs
,
3313 static ssize_t
slab_attr_show(struct kobject
*kobj
,
3314 struct attribute
*attr
,
3317 struct slab_attribute
*attribute
;
3318 struct kmem_cache
*s
;
3321 attribute
= to_slab_attr(attr
);
3324 if (!attribute
->show
)
3327 err
= attribute
->show(s
, buf
);
3332 static ssize_t
slab_attr_store(struct kobject
*kobj
,
3333 struct attribute
*attr
,
3334 const char *buf
, size_t len
)
3336 struct slab_attribute
*attribute
;
3337 struct kmem_cache
*s
;
3340 attribute
= to_slab_attr(attr
);
3343 if (!attribute
->store
)
3346 err
= attribute
->store(s
, buf
, len
);
3351 static struct sysfs_ops slab_sysfs_ops
= {
3352 .show
= slab_attr_show
,
3353 .store
= slab_attr_store
,
3356 static struct kobj_type slab_ktype
= {
3357 .sysfs_ops
= &slab_sysfs_ops
,
3360 static int uevent_filter(struct kset
*kset
, struct kobject
*kobj
)
3362 struct kobj_type
*ktype
= get_ktype(kobj
);
3364 if (ktype
== &slab_ktype
)
3369 static struct kset_uevent_ops slab_uevent_ops
= {
3370 .filter
= uevent_filter
,
3373 decl_subsys(slab
, &slab_ktype
, &slab_uevent_ops
);
3375 #define ID_STR_LENGTH 64
3377 /* Create a unique string id for a slab cache:
3379 * :[flags-]size:[memory address of kmemcache]
3381 static char *create_unique_id(struct kmem_cache
*s
)
3383 char *name
= kmalloc(ID_STR_LENGTH
, GFP_KERNEL
);
3390 * First flags affecting slabcache operations. We will only
3391 * get here for aliasable slabs so we do not need to support
3392 * too many flags. The flags here must cover all flags that
3393 * are matched during merging to guarantee that the id is
3396 if (s
->flags
& SLAB_CACHE_DMA
)
3398 if (s
->flags
& SLAB_RECLAIM_ACCOUNT
)
3400 if (s
->flags
& SLAB_DEBUG_FREE
)
3404 p
+= sprintf(p
, "%07d", s
->size
);
3405 BUG_ON(p
> name
+ ID_STR_LENGTH
- 1);
3409 static int sysfs_slab_add(struct kmem_cache
*s
)
3415 if (slab_state
< SYSFS
)
3416 /* Defer until later */
3419 unmergeable
= slab_unmergeable(s
);
3422 * Slabcache can never be merged so we can use the name proper.
3423 * This is typically the case for debug situations. In that
3424 * case we can catch duplicate names easily.
3426 sysfs_remove_link(&slab_subsys
.kobj
, s
->name
);
3430 * Create a unique name for the slab as a target
3433 name
= create_unique_id(s
);
3436 kobj_set_kset_s(s
, slab_subsys
);
3437 kobject_set_name(&s
->kobj
, name
);
3438 kobject_init(&s
->kobj
);
3439 err
= kobject_add(&s
->kobj
);
3443 err
= sysfs_create_group(&s
->kobj
, &slab_attr_group
);
3446 kobject_uevent(&s
->kobj
, KOBJ_ADD
);
3448 /* Setup first alias */
3449 sysfs_slab_alias(s
, s
->name
);
3455 static void sysfs_slab_remove(struct kmem_cache
*s
)
3457 kobject_uevent(&s
->kobj
, KOBJ_REMOVE
);
3458 kobject_del(&s
->kobj
);
3462 * Need to buffer aliases during bootup until sysfs becomes
3463 * available lest we loose that information.
3465 struct saved_alias
{
3466 struct kmem_cache
*s
;
3468 struct saved_alias
*next
;
3471 struct saved_alias
*alias_list
;
3473 static int sysfs_slab_alias(struct kmem_cache
*s
, const char *name
)
3475 struct saved_alias
*al
;
3477 if (slab_state
== SYSFS
) {
3479 * If we have a leftover link then remove it.
3481 sysfs_remove_link(&slab_subsys
.kobj
, name
);
3482 return sysfs_create_link(&slab_subsys
.kobj
,
3486 al
= kmalloc(sizeof(struct saved_alias
), GFP_KERNEL
);
3492 al
->next
= alias_list
;
3497 static int __init
slab_sysfs_init(void)
3501 err
= subsystem_register(&slab_subsys
);
3503 printk(KERN_ERR
"Cannot register slab subsystem.\n");
3509 while (alias_list
) {
3510 struct saved_alias
*al
= alias_list
;
3512 alias_list
= alias_list
->next
;
3513 err
= sysfs_slab_alias(al
->s
, al
->name
);
3522 __initcall(slab_sysfs_init
);
3524 __initcall(finish_bootstrap
);