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 * - Support DEBUG_SLAB_LEAK. Trouble is we do not know where the full
103 * - SLAB_DEBUG_INITIAL is not supported but I have never seen a use of
106 * - Variable sizing of the per node arrays
109 /* Enable to test recovery from slab corruption on boot */
110 #undef SLUB_RESILIENCY_TEST
115 * Small page size. Make sure that we do not fragment memory
117 #define DEFAULT_MAX_ORDER 1
118 #define DEFAULT_MIN_OBJECTS 4
123 * Large page machines are customarily able to handle larger
126 #define DEFAULT_MAX_ORDER 2
127 #define DEFAULT_MIN_OBJECTS 8
132 * Flags from the regular SLAB that SLUB does not support:
134 #define SLUB_UNIMPLEMENTED (SLAB_DEBUG_INITIAL)
136 #define DEBUG_DEFAULT_FLAGS (SLAB_DEBUG_FREE | SLAB_RED_ZONE | \
137 SLAB_POISON | SLAB_STORE_USER)
139 * Set of flags that will prevent slab merging
141 #define SLUB_NEVER_MERGE (SLAB_RED_ZONE | SLAB_POISON | SLAB_STORE_USER | \
142 SLAB_TRACE | SLAB_DESTROY_BY_RCU)
144 #define SLUB_MERGE_SAME (SLAB_DEBUG_FREE | SLAB_RECLAIM_ACCOUNT | \
147 #ifndef ARCH_KMALLOC_MINALIGN
148 #define ARCH_KMALLOC_MINALIGN __alignof__(unsigned long long)
151 #ifndef ARCH_SLAB_MINALIGN
152 #define ARCH_SLAB_MINALIGN __alignof__(unsigned long long)
155 /* Internal SLUB flags */
156 #define __OBJECT_POISON 0x80000000 /* Poison object */
158 static int kmem_size
= sizeof(struct kmem_cache
);
161 static struct notifier_block slab_notifier
;
165 DOWN
, /* No slab functionality available */
166 PARTIAL
, /* kmem_cache_open() works but kmalloc does not */
167 UP
, /* Everything works */
171 /* A list of all slab caches on the system */
172 static DECLARE_RWSEM(slub_lock
);
173 LIST_HEAD(slab_caches
);
176 static int sysfs_slab_add(struct kmem_cache
*);
177 static int sysfs_slab_alias(struct kmem_cache
*, const char *);
178 static void sysfs_slab_remove(struct kmem_cache
*);
180 static int sysfs_slab_add(struct kmem_cache
*s
) { return 0; }
181 static int sysfs_slab_alias(struct kmem_cache
*s
, const char *p
) { return 0; }
182 static void sysfs_slab_remove(struct kmem_cache
*s
) {}
185 /********************************************************************
186 * Core slab cache functions
187 *******************************************************************/
189 int slab_is_available(void)
191 return slab_state
>= UP
;
194 static inline struct kmem_cache_node
*get_node(struct kmem_cache
*s
, int node
)
197 return s
->node
[node
];
199 return &s
->local_node
;
206 static void print_section(char *text
, u8
*addr
, unsigned int length
)
214 for (i
= 0; i
< length
; i
++) {
216 printk(KERN_ERR
"%10s 0x%p: ", text
, addr
+ i
);
219 printk(" %02x", addr
[i
]);
221 ascii
[offset
] = isgraph(addr
[i
]) ? addr
[i
] : '.';
223 printk(" %s\n",ascii
);
234 printk(" %s\n", ascii
);
239 * Slow version of get and set free pointer.
241 * This requires touching the cache lines of kmem_cache.
242 * The offset can also be obtained from the page. In that
243 * case it is in the cacheline that we already need to touch.
245 static void *get_freepointer(struct kmem_cache
*s
, void *object
)
247 return *(void **)(object
+ s
->offset
);
250 static void set_freepointer(struct kmem_cache
*s
, void *object
, void *fp
)
252 *(void **)(object
+ s
->offset
) = fp
;
256 * Tracking user of a slab.
259 void *addr
; /* Called from address */
260 int cpu
; /* Was running on cpu */
261 int pid
; /* Pid context */
262 unsigned long when
; /* When did the operation occur */
265 enum track_item
{ TRACK_ALLOC
, TRACK_FREE
};
267 static struct track
*get_track(struct kmem_cache
*s
, void *object
,
268 enum track_item alloc
)
273 p
= object
+ s
->offset
+ sizeof(void *);
275 p
= object
+ s
->inuse
;
280 static void set_track(struct kmem_cache
*s
, void *object
,
281 enum track_item alloc
, void *addr
)
286 p
= object
+ s
->offset
+ sizeof(void *);
288 p
= object
+ s
->inuse
;
293 p
->cpu
= smp_processor_id();
294 p
->pid
= current
? current
->pid
: -1;
297 memset(p
, 0, sizeof(struct track
));
300 static void init_tracking(struct kmem_cache
*s
, void *object
)
302 if (s
->flags
& SLAB_STORE_USER
) {
303 set_track(s
, object
, TRACK_FREE
, NULL
);
304 set_track(s
, object
, TRACK_ALLOC
, NULL
);
308 static void print_track(const char *s
, struct track
*t
)
313 printk(KERN_ERR
"%s: ", s
);
314 __print_symbol("%s", (unsigned long)t
->addr
);
315 printk(" jiffies_ago=%lu cpu=%u pid=%d\n", jiffies
- t
->when
, t
->cpu
, t
->pid
);
318 static void print_trailer(struct kmem_cache
*s
, u8
*p
)
320 unsigned int off
; /* Offset of last byte */
322 if (s
->flags
& SLAB_RED_ZONE
)
323 print_section("Redzone", p
+ s
->objsize
,
324 s
->inuse
- s
->objsize
);
326 printk(KERN_ERR
"FreePointer 0x%p -> 0x%p\n",
328 get_freepointer(s
, p
));
331 off
= s
->offset
+ sizeof(void *);
335 if (s
->flags
& SLAB_STORE_USER
) {
336 print_track("Last alloc", get_track(s
, p
, TRACK_ALLOC
));
337 print_track("Last free ", get_track(s
, p
, TRACK_FREE
));
338 off
+= 2 * sizeof(struct track
);
342 /* Beginning of the filler is the free pointer */
343 print_section("Filler", p
+ off
, s
->size
- off
);
346 static void object_err(struct kmem_cache
*s
, struct page
*page
,
347 u8
*object
, char *reason
)
349 u8
*addr
= page_address(page
);
351 printk(KERN_ERR
"*** SLUB %s: %s@0x%p slab 0x%p\n",
352 s
->name
, reason
, object
, page
);
353 printk(KERN_ERR
" offset=%tu flags=0x%04lx inuse=%u freelist=0x%p\n",
354 object
- addr
, page
->flags
, page
->inuse
, page
->freelist
);
355 if (object
> addr
+ 16)
356 print_section("Bytes b4", object
- 16, 16);
357 print_section("Object", object
, min(s
->objsize
, 128));
358 print_trailer(s
, object
);
362 static void slab_err(struct kmem_cache
*s
, struct page
*page
, char *reason
, ...)
367 va_start(args
, reason
);
368 vsnprintf(buf
, sizeof(buf
), reason
, args
);
370 printk(KERN_ERR
"*** SLUB %s: %s in slab @0x%p\n", s
->name
, buf
,
375 static void init_object(struct kmem_cache
*s
, void *object
, int active
)
379 if (s
->flags
& __OBJECT_POISON
) {
380 memset(p
, POISON_FREE
, s
->objsize
- 1);
381 p
[s
->objsize
-1] = POISON_END
;
384 if (s
->flags
& SLAB_RED_ZONE
)
385 memset(p
+ s
->objsize
,
386 active
? SLUB_RED_ACTIVE
: SLUB_RED_INACTIVE
,
387 s
->inuse
- s
->objsize
);
390 static int check_bytes(u8
*start
, unsigned int value
, unsigned int bytes
)
393 if (*start
!= (u8
)value
)
402 static int check_valid_pointer(struct kmem_cache
*s
, struct page
*page
,
410 base
= page_address(page
);
411 if (object
< base
|| object
>= base
+ s
->objects
* s
->size
||
412 (object
- base
) % s
->size
) {
423 * Bytes of the object to be managed.
424 * If the freepointer may overlay the object then the free
425 * pointer is the first word of the object.
426 * Poisoning uses 0x6b (POISON_FREE) and the last byte is
429 * object + s->objsize
430 * Padding to reach word boundary. This is also used for Redzoning.
431 * Padding is extended to word size if Redzoning is enabled
432 * and objsize == inuse.
433 * We fill with 0xbb (RED_INACTIVE) for inactive objects and with
434 * 0xcc (RED_ACTIVE) for objects in use.
437 * A. Free pointer (if we cannot overwrite object on free)
438 * B. Tracking data for SLAB_STORE_USER
439 * C. Padding to reach required alignment boundary
440 * Padding is done using 0x5a (POISON_INUSE)
444 * If slabcaches are merged then the objsize and inuse boundaries are to
445 * be ignored. And therefore no slab options that rely on these boundaries
446 * may be used with merged slabcaches.
449 static void restore_bytes(struct kmem_cache
*s
, char *message
, u8 data
,
450 void *from
, void *to
)
452 printk(KERN_ERR
"@@@ SLUB: %s Restoring %s (0x%x) from 0x%p-0x%p\n",
453 s
->name
, message
, data
, from
, to
- 1);
454 memset(from
, data
, to
- from
);
457 static int check_pad_bytes(struct kmem_cache
*s
, struct page
*page
, u8
*p
)
459 unsigned long off
= s
->inuse
; /* The end of info */
462 /* Freepointer is placed after the object. */
463 off
+= sizeof(void *);
465 if (s
->flags
& SLAB_STORE_USER
)
466 /* We also have user information there */
467 off
+= 2 * sizeof(struct track
);
472 if (check_bytes(p
+ off
, POISON_INUSE
, s
->size
- off
))
475 object_err(s
, page
, p
, "Object padding check fails");
480 restore_bytes(s
, "object padding", POISON_INUSE
, p
+ off
, p
+ s
->size
);
484 static int slab_pad_check(struct kmem_cache
*s
, struct page
*page
)
487 int length
, remainder
;
489 if (!(s
->flags
& SLAB_POISON
))
492 p
= page_address(page
);
493 length
= s
->objects
* s
->size
;
494 remainder
= (PAGE_SIZE
<< s
->order
) - length
;
498 if (!check_bytes(p
+ length
, POISON_INUSE
, remainder
)) {
499 printk(KERN_ERR
"SLUB: %s slab 0x%p: Padding fails check\n",
502 restore_bytes(s
, "slab padding", POISON_INUSE
, p
+ length
,
503 p
+ length
+ remainder
);
509 static int check_object(struct kmem_cache
*s
, struct page
*page
,
510 void *object
, int active
)
513 u8
*endobject
= object
+ s
->objsize
;
515 if (s
->flags
& SLAB_RED_ZONE
) {
517 active
? SLUB_RED_ACTIVE
: SLUB_RED_INACTIVE
;
519 if (!check_bytes(endobject
, red
, s
->inuse
- s
->objsize
)) {
520 object_err(s
, page
, object
,
521 active
? "Redzone Active" : "Redzone Inactive");
522 restore_bytes(s
, "redzone", red
,
523 endobject
, object
+ s
->inuse
);
527 if ((s
->flags
& SLAB_POISON
) && s
->objsize
< s
->inuse
&&
528 !check_bytes(endobject
, POISON_INUSE
,
529 s
->inuse
- s
->objsize
)) {
530 object_err(s
, page
, p
, "Alignment padding check fails");
532 * Fix it so that there will not be another report.
534 * Hmmm... We may be corrupting an object that now expects
535 * to be longer than allowed.
537 restore_bytes(s
, "alignment padding", POISON_INUSE
,
538 endobject
, object
+ s
->inuse
);
542 if (s
->flags
& SLAB_POISON
) {
543 if (!active
&& (s
->flags
& __OBJECT_POISON
) &&
544 (!check_bytes(p
, POISON_FREE
, s
->objsize
- 1) ||
545 p
[s
->objsize
- 1] != POISON_END
)) {
547 object_err(s
, page
, p
, "Poison check failed");
548 restore_bytes(s
, "Poison", POISON_FREE
,
549 p
, p
+ s
->objsize
-1);
550 restore_bytes(s
, "Poison", POISON_END
,
551 p
+ s
->objsize
- 1, p
+ s
->objsize
);
555 * check_pad_bytes cleans up on its own.
557 check_pad_bytes(s
, page
, p
);
560 if (!s
->offset
&& active
)
562 * Object and freepointer overlap. Cannot check
563 * freepointer while object is allocated.
567 /* Check free pointer validity */
568 if (!check_valid_pointer(s
, page
, get_freepointer(s
, p
))) {
569 object_err(s
, page
, p
, "Freepointer corrupt");
571 * No choice but to zap it and thus loose the remainder
572 * of the free objects in this slab. May cause
573 * another error because the object count maybe
576 set_freepointer(s
, p
, NULL
);
582 static int check_slab(struct kmem_cache
*s
, struct page
*page
)
584 VM_BUG_ON(!irqs_disabled());
586 if (!PageSlab(page
)) {
587 printk(KERN_ERR
"SLUB: %s Not a valid slab page @0x%p "
588 "flags=%lx mapping=0x%p count=%d \n",
589 s
->name
, page
, page
->flags
, page
->mapping
,
593 if (page
->offset
* sizeof(void *) != s
->offset
) {
594 printk(KERN_ERR
"SLUB: %s Corrupted offset %lu in slab @0x%p"
595 " flags=0x%lx mapping=0x%p count=%d\n",
597 (unsigned long)(page
->offset
* sizeof(void *)),
605 if (page
->inuse
> s
->objects
) {
606 printk(KERN_ERR
"SLUB: %s Inuse %u > max %u in slab "
607 "page @0x%p flags=%lx mapping=0x%p count=%d\n",
608 s
->name
, page
->inuse
, s
->objects
, page
, page
->flags
,
609 page
->mapping
, page_count(page
));
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 printk(KERN_ERR
"SLUB: %s slab 0x%p "
640 "freepointer 0x%p corrupted.\n",
643 page
->freelist
= NULL
;
644 page
->inuse
= s
->objects
;
650 fp
= get_freepointer(s
, object
);
654 if (page
->inuse
!= s
->objects
- nr
) {
655 printk(KERN_ERR
"slab %s: page 0x%p wrong object count."
656 " counter is %d but counted were %d\n",
657 s
->name
, page
, page
->inuse
,
659 page
->inuse
= s
->objects
- nr
;
661 return search
== NULL
;
665 * Tracking of fully allocated slabs for debugging
667 static void add_full(struct kmem_cache
*s
, struct page
*page
)
669 struct kmem_cache_node
*n
;
671 VM_BUG_ON(!irqs_disabled());
673 VM_BUG_ON(!irqs_disabled());
675 if (!(s
->flags
& SLAB_STORE_USER
))
678 n
= get_node(s
, page_to_nid(page
));
679 spin_lock(&n
->list_lock
);
680 list_add(&page
->lru
, &n
->full
);
681 spin_unlock(&n
->list_lock
);
684 static void remove_full(struct kmem_cache
*s
, struct page
*page
)
686 struct kmem_cache_node
*n
;
688 if (!(s
->flags
& SLAB_STORE_USER
))
691 n
= get_node(s
, page_to_nid(page
));
693 spin_lock(&n
->list_lock
);
694 list_del(&page
->lru
);
695 spin_unlock(&n
->list_lock
);
698 static int alloc_object_checks(struct kmem_cache
*s
, struct page
*page
,
701 if (!check_slab(s
, page
))
704 if (object
&& !on_freelist(s
, page
, object
)) {
705 printk(KERN_ERR
"SLUB: %s Object 0x%p@0x%p "
706 "already allocated.\n",
707 s
->name
, object
, page
);
711 if (!check_valid_pointer(s
, page
, object
)) {
712 object_err(s
, page
, object
, "Freelist Pointer check fails");
719 if (!check_object(s
, page
, object
, 0))
721 init_object(s
, object
, 1);
723 if (s
->flags
& SLAB_TRACE
) {
724 printk(KERN_INFO
"TRACE %s alloc 0x%p inuse=%d fp=0x%p\n",
725 s
->name
, object
, page
->inuse
,
733 if (PageSlab(page
)) {
735 * If this is a slab page then lets do the best we can
736 * to avoid issues in the future. Marking all objects
737 * as used avoids touching the remainder.
739 printk(KERN_ERR
"@@@ SLUB: %s slab 0x%p. Marking all objects used.\n",
741 page
->inuse
= s
->objects
;
742 page
->freelist
= NULL
;
743 /* Fix up fields that may be corrupted */
744 page
->offset
= s
->offset
/ sizeof(void *);
749 static int free_object_checks(struct kmem_cache
*s
, struct page
*page
,
752 if (!check_slab(s
, page
))
755 if (!check_valid_pointer(s
, page
, object
)) {
756 printk(KERN_ERR
"SLUB: %s slab 0x%p invalid "
757 "object pointer 0x%p\n",
758 s
->name
, page
, object
);
762 if (on_freelist(s
, page
, object
)) {
763 printk(KERN_ERR
"SLUB: %s slab 0x%p object "
764 "0x%p already free.\n", s
->name
, page
, object
);
768 if (!check_object(s
, page
, object
, 1))
771 if (unlikely(s
!= page
->slab
)) {
773 printk(KERN_ERR
"slab_free %s size %d: attempt to"
774 "free object(0x%p) outside of slab.\n",
775 s
->name
, s
->size
, object
);
779 "slab_free : no slab(NULL) for object 0x%p.\n",
782 printk(KERN_ERR
"slab_free %s(%d): object at 0x%p"
783 " belongs to slab %s(%d)\n",
784 s
->name
, s
->size
, object
,
785 page
->slab
->name
, page
->slab
->size
);
788 if (s
->flags
& SLAB_TRACE
) {
789 printk(KERN_INFO
"TRACE %s free 0x%p inuse=%d fp=0x%p\n",
790 s
->name
, object
, page
->inuse
,
792 print_section("Object", object
, s
->objsize
);
795 init_object(s
, object
, 0);
799 printk(KERN_ERR
"@@@ SLUB: %s slab 0x%p object at 0x%p not freed.\n",
800 s
->name
, page
, object
);
805 * Slab allocation and freeing
807 static struct page
*allocate_slab(struct kmem_cache
*s
, gfp_t flags
, int node
)
810 int pages
= 1 << s
->order
;
815 if (s
->flags
& SLAB_CACHE_DMA
)
819 page
= alloc_pages(flags
, s
->order
);
821 page
= alloc_pages_node(node
, flags
, s
->order
);
826 mod_zone_page_state(page_zone(page
),
827 (s
->flags
& SLAB_RECLAIM_ACCOUNT
) ?
828 NR_SLAB_RECLAIMABLE
: NR_SLAB_UNRECLAIMABLE
,
834 static void setup_object(struct kmem_cache
*s
, struct page
*page
,
837 if (PageError(page
)) {
838 init_object(s
, object
, 0);
839 init_tracking(s
, object
);
842 if (unlikely(s
->ctor
)) {
843 int mode
= SLAB_CTOR_CONSTRUCTOR
;
845 if (!(s
->flags
& __GFP_WAIT
))
846 mode
|= SLAB_CTOR_ATOMIC
;
848 s
->ctor(object
, s
, mode
);
852 static struct page
*new_slab(struct kmem_cache
*s
, gfp_t flags
, int node
)
855 struct kmem_cache_node
*n
;
861 if (flags
& __GFP_NO_GROW
)
864 BUG_ON(flags
& ~(GFP_DMA
| GFP_LEVEL_MASK
));
866 if (flags
& __GFP_WAIT
)
869 page
= allocate_slab(s
, flags
& GFP_LEVEL_MASK
, node
);
873 n
= get_node(s
, page_to_nid(page
));
875 atomic_long_inc(&n
->nr_slabs
);
876 page
->offset
= s
->offset
/ sizeof(void *);
878 page
->flags
|= 1 << PG_slab
;
879 if (s
->flags
& (SLAB_DEBUG_FREE
| SLAB_RED_ZONE
| SLAB_POISON
|
880 SLAB_STORE_USER
| SLAB_TRACE
))
881 page
->flags
|= 1 << PG_error
;
883 start
= page_address(page
);
884 end
= start
+ s
->objects
* s
->size
;
886 if (unlikely(s
->flags
& SLAB_POISON
))
887 memset(start
, POISON_INUSE
, PAGE_SIZE
<< s
->order
);
890 for (p
= start
+ s
->size
; p
< end
; p
+= s
->size
) {
891 setup_object(s
, page
, last
);
892 set_freepointer(s
, last
, p
);
895 setup_object(s
, page
, last
);
896 set_freepointer(s
, last
, NULL
);
898 page
->freelist
= start
;
901 if (flags
& __GFP_WAIT
)
906 static void __free_slab(struct kmem_cache
*s
, struct page
*page
)
908 int pages
= 1 << s
->order
;
910 if (unlikely(PageError(page
) || s
->dtor
)) {
911 void *start
= page_address(page
);
912 void *end
= start
+ (pages
<< PAGE_SHIFT
);
915 slab_pad_check(s
, page
);
916 for (p
= start
; p
<= end
- s
->size
; p
+= s
->size
) {
919 check_object(s
, page
, p
, 0);
923 mod_zone_page_state(page_zone(page
),
924 (s
->flags
& SLAB_RECLAIM_ACCOUNT
) ?
925 NR_SLAB_RECLAIMABLE
: NR_SLAB_UNRECLAIMABLE
,
928 page
->mapping
= NULL
;
929 __free_pages(page
, s
->order
);
932 static void rcu_free_slab(struct rcu_head
*h
)
936 page
= container_of((struct list_head
*)h
, struct page
, lru
);
937 __free_slab(page
->slab
, page
);
940 static void free_slab(struct kmem_cache
*s
, struct page
*page
)
942 if (unlikely(s
->flags
& SLAB_DESTROY_BY_RCU
)) {
944 * RCU free overloads the RCU head over the LRU
946 struct rcu_head
*head
= (void *)&page
->lru
;
948 call_rcu(head
, rcu_free_slab
);
950 __free_slab(s
, page
);
953 static void discard_slab(struct kmem_cache
*s
, struct page
*page
)
955 struct kmem_cache_node
*n
= get_node(s
, page_to_nid(page
));
957 atomic_long_dec(&n
->nr_slabs
);
958 reset_page_mapcount(page
);
959 page
->flags
&= ~(1 << PG_slab
| 1 << PG_error
);
964 * Per slab locking using the pagelock
966 static __always_inline
void slab_lock(struct page
*page
)
968 bit_spin_lock(PG_locked
, &page
->flags
);
971 static __always_inline
void slab_unlock(struct page
*page
)
973 bit_spin_unlock(PG_locked
, &page
->flags
);
976 static __always_inline
int slab_trylock(struct page
*page
)
980 rc
= bit_spin_trylock(PG_locked
, &page
->flags
);
985 * Management of partially allocated slabs
987 static void add_partial(struct kmem_cache
*s
, struct page
*page
)
989 struct kmem_cache_node
*n
= get_node(s
, page_to_nid(page
));
991 spin_lock(&n
->list_lock
);
993 list_add(&page
->lru
, &n
->partial
);
994 spin_unlock(&n
->list_lock
);
997 static void remove_partial(struct kmem_cache
*s
,
1000 struct kmem_cache_node
*n
= get_node(s
, page_to_nid(page
));
1002 spin_lock(&n
->list_lock
);
1003 list_del(&page
->lru
);
1005 spin_unlock(&n
->list_lock
);
1009 * Lock page and remove it from the partial list
1011 * Must hold list_lock
1013 static int lock_and_del_slab(struct kmem_cache_node
*n
, struct page
*page
)
1015 if (slab_trylock(page
)) {
1016 list_del(&page
->lru
);
1024 * Try to get a partial slab from a specific node
1026 static struct page
*get_partial_node(struct kmem_cache_node
*n
)
1031 * Racy check. If we mistakenly see no partial slabs then we
1032 * just allocate an empty slab. If we mistakenly try to get a
1033 * partial slab then get_partials() will return NULL.
1035 if (!n
|| !n
->nr_partial
)
1038 spin_lock(&n
->list_lock
);
1039 list_for_each_entry(page
, &n
->partial
, lru
)
1040 if (lock_and_del_slab(n
, page
))
1044 spin_unlock(&n
->list_lock
);
1049 * Get a page from somewhere. Search in increasing NUMA
1052 static struct page
*get_any_partial(struct kmem_cache
*s
, gfp_t flags
)
1055 struct zonelist
*zonelist
;
1060 * The defrag ratio allows to configure the tradeoffs between
1061 * inter node defragmentation and node local allocations.
1062 * A lower defrag_ratio increases the tendency to do local
1063 * allocations instead of scanning throught the partial
1064 * lists on other nodes.
1066 * If defrag_ratio is set to 0 then kmalloc() always
1067 * returns node local objects. If its higher then kmalloc()
1068 * may return off node objects in order to avoid fragmentation.
1070 * A higher ratio means slabs may be taken from other nodes
1071 * thus reducing the number of partial slabs on those nodes.
1073 * If /sys/slab/xx/defrag_ratio is set to 100 (which makes
1074 * defrag_ratio = 1000) then every (well almost) allocation
1075 * will first attempt to defrag slab caches on other nodes. This
1076 * means scanning over all nodes to look for partial slabs which
1077 * may be a bit expensive to do on every slab allocation.
1079 if (!s
->defrag_ratio
|| get_cycles() % 1024 > s
->defrag_ratio
)
1082 zonelist
= &NODE_DATA(slab_node(current
->mempolicy
))
1083 ->node_zonelists
[gfp_zone(flags
)];
1084 for (z
= zonelist
->zones
; *z
; z
++) {
1085 struct kmem_cache_node
*n
;
1087 n
= get_node(s
, zone_to_nid(*z
));
1089 if (n
&& cpuset_zone_allowed_hardwall(*z
, flags
) &&
1090 n
->nr_partial
> 2) {
1091 page
= get_partial_node(n
);
1101 * Get a partial page, lock it and return it.
1103 static struct page
*get_partial(struct kmem_cache
*s
, gfp_t flags
, int node
)
1106 int searchnode
= (node
== -1) ? numa_node_id() : node
;
1108 page
= get_partial_node(get_node(s
, searchnode
));
1109 if (page
|| (flags
& __GFP_THISNODE
))
1112 return get_any_partial(s
, flags
);
1116 * Move a page back to the lists.
1118 * Must be called with the slab lock held.
1120 * On exit the slab lock will have been dropped.
1122 static void putback_slab(struct kmem_cache
*s
, struct page
*page
)
1126 add_partial(s
, page
);
1127 else if (PageError(page
))
1132 discard_slab(s
, page
);
1137 * Remove the cpu slab
1139 static void deactivate_slab(struct kmem_cache
*s
, struct page
*page
, int cpu
)
1141 s
->cpu_slab
[cpu
] = NULL
;
1142 ClearPageActive(page
);
1144 putback_slab(s
, page
);
1147 static void flush_slab(struct kmem_cache
*s
, struct page
*page
, int cpu
)
1150 deactivate_slab(s
, page
, cpu
);
1155 * Called from IPI handler with interrupts disabled.
1157 static void __flush_cpu_slab(struct kmem_cache
*s
, int cpu
)
1159 struct page
*page
= s
->cpu_slab
[cpu
];
1162 flush_slab(s
, page
, cpu
);
1165 static void flush_cpu_slab(void *d
)
1167 struct kmem_cache
*s
= d
;
1168 int cpu
= smp_processor_id();
1170 __flush_cpu_slab(s
, cpu
);
1173 static void flush_all(struct kmem_cache
*s
)
1176 on_each_cpu(flush_cpu_slab
, s
, 1, 1);
1178 unsigned long flags
;
1180 local_irq_save(flags
);
1182 local_irq_restore(flags
);
1187 * slab_alloc is optimized to only modify two cachelines on the fast path
1188 * (aside from the stack):
1190 * 1. The page struct
1191 * 2. The first cacheline of the object to be allocated.
1193 * The only cache lines that are read (apart from code) is the
1194 * per cpu array in the kmem_cache struct.
1196 * Fastpath is not possible if we need to get a new slab or have
1197 * debugging enabled (which means all slabs are marked with PageError)
1199 static void *slab_alloc(struct kmem_cache
*s
,
1200 gfp_t gfpflags
, int node
, void *addr
)
1204 unsigned long flags
;
1207 local_irq_save(flags
);
1208 cpu
= smp_processor_id();
1209 page
= s
->cpu_slab
[cpu
];
1214 if (unlikely(node
!= -1 && page_to_nid(page
) != node
))
1217 object
= page
->freelist
;
1218 if (unlikely(!object
))
1220 if (unlikely(PageError(page
)))
1225 page
->freelist
= object
[page
->offset
];
1227 local_irq_restore(flags
);
1231 deactivate_slab(s
, page
, cpu
);
1234 page
= get_partial(s
, gfpflags
, node
);
1237 s
->cpu_slab
[cpu
] = page
;
1238 SetPageActive(page
);
1242 page
= new_slab(s
, gfpflags
, node
);
1244 cpu
= smp_processor_id();
1245 if (s
->cpu_slab
[cpu
]) {
1247 * Someone else populated the cpu_slab while we enabled
1248 * interrupts, or we have got scheduled on another cpu.
1249 * The page may not be on the requested node.
1252 page_to_nid(s
->cpu_slab
[cpu
]) == node
) {
1254 * Current cpuslab is acceptable and we
1255 * want the current one since its cache hot
1257 discard_slab(s
, page
);
1258 page
= s
->cpu_slab
[cpu
];
1262 /* Dump the current slab */
1263 flush_slab(s
, s
->cpu_slab
[cpu
], cpu
);
1268 local_irq_restore(flags
);
1271 if (!alloc_object_checks(s
, page
, object
))
1273 if (s
->flags
& SLAB_STORE_USER
)
1274 set_track(s
, object
, TRACK_ALLOC
, addr
);
1278 void *kmem_cache_alloc(struct kmem_cache
*s
, gfp_t gfpflags
)
1280 return slab_alloc(s
, gfpflags
, -1, __builtin_return_address(0));
1282 EXPORT_SYMBOL(kmem_cache_alloc
);
1285 void *kmem_cache_alloc_node(struct kmem_cache
*s
, gfp_t gfpflags
, int node
)
1287 return slab_alloc(s
, gfpflags
, node
, __builtin_return_address(0));
1289 EXPORT_SYMBOL(kmem_cache_alloc_node
);
1293 * The fastpath only writes the cacheline of the page struct and the first
1294 * cacheline of the object.
1296 * No special cachelines need to be read
1298 static void slab_free(struct kmem_cache
*s
, struct page
*page
,
1299 void *x
, void *addr
)
1302 void **object
= (void *)x
;
1303 unsigned long flags
;
1305 local_irq_save(flags
);
1308 if (unlikely(PageError(page
)))
1311 prior
= object
[page
->offset
] = page
->freelist
;
1312 page
->freelist
= object
;
1315 if (unlikely(PageActive(page
)))
1317 * Cpu slabs are never on partial lists and are
1322 if (unlikely(!page
->inuse
))
1326 * Objects left in the slab. If it
1327 * was not on the partial list before
1330 if (unlikely(!prior
))
1331 add_partial(s
, page
);
1335 local_irq_restore(flags
);
1341 * Slab on the partial list.
1343 remove_partial(s
, page
);
1346 discard_slab(s
, page
);
1347 local_irq_restore(flags
);
1351 if (!free_object_checks(s
, page
, x
))
1353 if (!PageActive(page
) && !page
->freelist
)
1354 remove_full(s
, page
);
1355 if (s
->flags
& SLAB_STORE_USER
)
1356 set_track(s
, x
, TRACK_FREE
, addr
);
1360 void kmem_cache_free(struct kmem_cache
*s
, void *x
)
1364 page
= virt_to_head_page(x
);
1366 slab_free(s
, page
, x
, __builtin_return_address(0));
1368 EXPORT_SYMBOL(kmem_cache_free
);
1370 /* Figure out on which slab object the object resides */
1371 static struct page
*get_object_page(const void *x
)
1373 struct page
*page
= virt_to_head_page(x
);
1375 if (!PageSlab(page
))
1382 * kmem_cache_open produces objects aligned at "size" and the first object
1383 * is placed at offset 0 in the slab (We have no metainformation on the
1384 * slab, all slabs are in essence "off slab").
1386 * In order to get the desired alignment one just needs to align the
1389 * Notice that the allocation order determines the sizes of the per cpu
1390 * caches. Each processor has always one slab available for allocations.
1391 * Increasing the allocation order reduces the number of times that slabs
1392 * must be moved on and off the partial lists and therefore may influence
1395 * The offset is used to relocate the free list link in each object. It is
1396 * therefore possible to move the free list link behind the object. This
1397 * is necessary for RCU to work properly and also useful for debugging.
1401 * Mininum / Maximum order of slab pages. This influences locking overhead
1402 * and slab fragmentation. A higher order reduces the number of partial slabs
1403 * and increases the number of allocations possible without having to
1404 * take the list_lock.
1406 static int slub_min_order
;
1407 static int slub_max_order
= DEFAULT_MAX_ORDER
;
1410 * Minimum number of objects per slab. This is necessary in order to
1411 * reduce locking overhead. Similar to the queue size in SLAB.
1413 static int slub_min_objects
= DEFAULT_MIN_OBJECTS
;
1416 * Merge control. If this is set then no merging of slab caches will occur.
1418 static int slub_nomerge
;
1423 static int slub_debug
;
1425 static char *slub_debug_slabs
;
1428 * Calculate the order of allocation given an slab object size.
1430 * The order of allocation has significant impact on other elements
1431 * of the system. Generally order 0 allocations should be preferred
1432 * since they do not cause fragmentation in the page allocator. Larger
1433 * objects may have problems with order 0 because there may be too much
1434 * space left unused in a slab. We go to a higher order if more than 1/8th
1435 * of the slab would be wasted.
1437 * In order to reach satisfactory performance we must ensure that
1438 * a minimum number of objects is in one slab. Otherwise we may
1439 * generate too much activity on the partial lists. This is less a
1440 * concern for large slabs though. slub_max_order specifies the order
1441 * where we begin to stop considering the number of objects in a slab.
1443 * Higher order allocations also allow the placement of more objects
1444 * in a slab and thereby reduce object handling overhead. If the user
1445 * has requested a higher mininum order then we start with that one
1448 static int calculate_order(int size
)
1453 for (order
= max(slub_min_order
, fls(size
- 1) - PAGE_SHIFT
);
1454 order
< MAX_ORDER
; order
++) {
1455 unsigned long slab_size
= PAGE_SIZE
<< order
;
1457 if (slub_max_order
> order
&&
1458 slab_size
< slub_min_objects
* size
)
1461 if (slab_size
< size
)
1464 rem
= slab_size
% size
;
1466 if (rem
<= (PAGE_SIZE
<< order
) / 8)
1470 if (order
>= MAX_ORDER
)
1476 * Function to figure out which alignment to use from the
1477 * various ways of specifying it.
1479 static unsigned long calculate_alignment(unsigned long flags
,
1480 unsigned long align
, unsigned long size
)
1483 * If the user wants hardware cache aligned objects then
1484 * follow that suggestion if the object is sufficiently
1487 * The hardware cache alignment cannot override the
1488 * specified alignment though. If that is greater
1491 if ((flags
& (SLAB_MUST_HWCACHE_ALIGN
| SLAB_HWCACHE_ALIGN
)) &&
1492 size
> L1_CACHE_BYTES
/ 2)
1493 return max_t(unsigned long, align
, L1_CACHE_BYTES
);
1495 if (align
< ARCH_SLAB_MINALIGN
)
1496 return ARCH_SLAB_MINALIGN
;
1498 return ALIGN(align
, sizeof(void *));
1501 static void init_kmem_cache_node(struct kmem_cache_node
*n
)
1504 atomic_long_set(&n
->nr_slabs
, 0);
1505 spin_lock_init(&n
->list_lock
);
1506 INIT_LIST_HEAD(&n
->partial
);
1507 INIT_LIST_HEAD(&n
->full
);
1512 * No kmalloc_node yet so do it by hand. We know that this is the first
1513 * slab on the node for this slabcache. There are no concurrent accesses
1516 * Note that this function only works on the kmalloc_node_cache
1517 * when allocating for the kmalloc_node_cache.
1519 static struct kmem_cache_node
* __init
early_kmem_cache_node_alloc(gfp_t gfpflags
,
1523 struct kmem_cache_node
*n
;
1525 BUG_ON(kmalloc_caches
->size
< sizeof(struct kmem_cache_node
));
1527 page
= new_slab(kmalloc_caches
, gfpflags
| GFP_THISNODE
, node
);
1528 /* new_slab() disables interupts */
1534 page
->freelist
= get_freepointer(kmalloc_caches
, n
);
1536 kmalloc_caches
->node
[node
] = n
;
1537 init_object(kmalloc_caches
, n
, 1);
1538 init_kmem_cache_node(n
);
1539 atomic_long_inc(&n
->nr_slabs
);
1540 add_partial(kmalloc_caches
, page
);
1544 static void free_kmem_cache_nodes(struct kmem_cache
*s
)
1548 for_each_online_node(node
) {
1549 struct kmem_cache_node
*n
= s
->node
[node
];
1550 if (n
&& n
!= &s
->local_node
)
1551 kmem_cache_free(kmalloc_caches
, n
);
1552 s
->node
[node
] = NULL
;
1556 static int init_kmem_cache_nodes(struct kmem_cache
*s
, gfp_t gfpflags
)
1561 if (slab_state
>= UP
)
1562 local_node
= page_to_nid(virt_to_page(s
));
1566 for_each_online_node(node
) {
1567 struct kmem_cache_node
*n
;
1569 if (local_node
== node
)
1572 if (slab_state
== DOWN
) {
1573 n
= early_kmem_cache_node_alloc(gfpflags
,
1577 n
= kmem_cache_alloc_node(kmalloc_caches
,
1581 free_kmem_cache_nodes(s
);
1587 init_kmem_cache_node(n
);
1592 static void free_kmem_cache_nodes(struct kmem_cache
*s
)
1596 static int init_kmem_cache_nodes(struct kmem_cache
*s
, gfp_t gfpflags
)
1598 init_kmem_cache_node(&s
->local_node
);
1604 * calculate_sizes() determines the order and the distribution of data within
1607 static int calculate_sizes(struct kmem_cache
*s
)
1609 unsigned long flags
= s
->flags
;
1610 unsigned long size
= s
->objsize
;
1611 unsigned long align
= s
->align
;
1614 * Determine if we can poison the object itself. If the user of
1615 * the slab may touch the object after free or before allocation
1616 * then we should never poison the object itself.
1618 if ((flags
& SLAB_POISON
) && !(flags
& SLAB_DESTROY_BY_RCU
) &&
1619 !s
->ctor
&& !s
->dtor
)
1620 s
->flags
|= __OBJECT_POISON
;
1622 s
->flags
&= ~__OBJECT_POISON
;
1625 * Round up object size to the next word boundary. We can only
1626 * place the free pointer at word boundaries and this determines
1627 * the possible location of the free pointer.
1629 size
= ALIGN(size
, sizeof(void *));
1632 * If we are redzoning then check if there is some space between the
1633 * end of the object and the free pointer. If not then add an
1634 * additional word, so that we can establish a redzone between
1635 * the object and the freepointer to be able to check for overwrites.
1637 if ((flags
& SLAB_RED_ZONE
) && size
== s
->objsize
)
1638 size
+= sizeof(void *);
1641 * With that we have determined how much of the slab is in actual
1642 * use by the object. This is the potential offset to the free
1647 if (((flags
& (SLAB_DESTROY_BY_RCU
| SLAB_POISON
)) ||
1648 s
->ctor
|| s
->dtor
)) {
1650 * Relocate free pointer after the object if it is not
1651 * permitted to overwrite the first word of the object on
1654 * This is the case if we do RCU, have a constructor or
1655 * destructor or are poisoning the objects.
1658 size
+= sizeof(void *);
1661 if (flags
& SLAB_STORE_USER
)
1663 * Need to store information about allocs and frees after
1666 size
+= 2 * sizeof(struct track
);
1668 if (flags
& DEBUG_DEFAULT_FLAGS
)
1670 * Add some empty padding so that we can catch
1671 * overwrites from earlier objects rather than let
1672 * tracking information or the free pointer be
1673 * corrupted if an user writes before the start
1676 size
+= sizeof(void *);
1678 * Determine the alignment based on various parameters that the
1679 * user specified (this is unecessarily complex due to the attempt
1680 * to be compatible with SLAB. Should be cleaned up some day).
1682 align
= calculate_alignment(flags
, align
, s
->objsize
);
1685 * SLUB stores one object immediately after another beginning from
1686 * offset 0. In order to align the objects we have to simply size
1687 * each object to conform to the alignment.
1689 size
= ALIGN(size
, align
);
1692 s
->order
= calculate_order(size
);
1697 * Determine the number of objects per slab
1699 s
->objects
= (PAGE_SIZE
<< s
->order
) / size
;
1702 * Verify that the number of objects is within permitted limits.
1703 * The page->inuse field is only 16 bit wide! So we cannot have
1704 * more than 64k objects per slab.
1706 if (!s
->objects
|| s
->objects
> 65535)
1712 static int __init
finish_bootstrap(void)
1714 struct list_head
*h
;
1719 list_for_each(h
, &slab_caches
) {
1720 struct kmem_cache
*s
=
1721 container_of(h
, struct kmem_cache
, list
);
1723 err
= sysfs_slab_add(s
);
1729 static int kmem_cache_open(struct kmem_cache
*s
, gfp_t gfpflags
,
1730 const char *name
, size_t size
,
1731 size_t align
, unsigned long flags
,
1732 void (*ctor
)(void *, struct kmem_cache
*, unsigned long),
1733 void (*dtor
)(void *, struct kmem_cache
*, unsigned long))
1735 memset(s
, 0, kmem_size
);
1743 BUG_ON(flags
& SLUB_UNIMPLEMENTED
);
1746 * The page->offset field is only 16 bit wide. This is an offset
1747 * in units of words from the beginning of an object. If the slab
1748 * size is bigger then we cannot move the free pointer behind the
1751 * On 32 bit platforms the limit is 256k. On 64bit platforms
1752 * the limit is 512k.
1754 * Debugging or ctor/dtors may create a need to move the free
1755 * pointer. Fail if this happens.
1757 if (s
->size
>= 65535 * sizeof(void *)) {
1758 BUG_ON(flags
& (SLAB_RED_ZONE
| SLAB_POISON
|
1759 SLAB_STORE_USER
| SLAB_DESTROY_BY_RCU
));
1760 BUG_ON(ctor
|| dtor
);
1764 * Enable debugging if selected on the kernel commandline.
1766 if (slub_debug
&& (!slub_debug_slabs
||
1767 strncmp(slub_debug_slabs
, name
,
1768 strlen(slub_debug_slabs
)) == 0))
1769 s
->flags
|= slub_debug
;
1771 if (!calculate_sizes(s
))
1776 s
->defrag_ratio
= 100;
1779 if (init_kmem_cache_nodes(s
, gfpflags
& ~SLUB_DMA
))
1782 if (flags
& SLAB_PANIC
)
1783 panic("Cannot create slab %s size=%lu realsize=%u "
1784 "order=%u offset=%u flags=%lx\n",
1785 s
->name
, (unsigned long)size
, s
->size
, s
->order
,
1789 EXPORT_SYMBOL(kmem_cache_open
);
1792 * Check if a given pointer is valid
1794 int kmem_ptr_validate(struct kmem_cache
*s
, const void *object
)
1799 page
= get_object_page(object
);
1801 if (!page
|| s
!= page
->slab
)
1802 /* No slab or wrong slab */
1805 addr
= page_address(page
);
1806 if (object
< addr
|| object
>= addr
+ s
->objects
* s
->size
)
1810 if ((object
- addr
) % s
->size
)
1811 /* Improperly aligned */
1815 * We could also check if the object is on the slabs freelist.
1816 * But this would be too expensive and it seems that the main
1817 * purpose of kmem_ptr_valid is to check if the object belongs
1818 * to a certain slab.
1822 EXPORT_SYMBOL(kmem_ptr_validate
);
1825 * Determine the size of a slab object
1827 unsigned int kmem_cache_size(struct kmem_cache
*s
)
1831 EXPORT_SYMBOL(kmem_cache_size
);
1833 const char *kmem_cache_name(struct kmem_cache
*s
)
1837 EXPORT_SYMBOL(kmem_cache_name
);
1840 * Attempt to free all slabs on a node
1842 static int free_list(struct kmem_cache
*s
, struct kmem_cache_node
*n
,
1843 struct list_head
*list
)
1845 int slabs_inuse
= 0;
1846 unsigned long flags
;
1847 struct page
*page
, *h
;
1849 spin_lock_irqsave(&n
->list_lock
, flags
);
1850 list_for_each_entry_safe(page
, h
, list
, lru
)
1852 list_del(&page
->lru
);
1853 discard_slab(s
, page
);
1856 spin_unlock_irqrestore(&n
->list_lock
, flags
);
1861 * Release all resources used by slab cache
1863 static int kmem_cache_close(struct kmem_cache
*s
)
1869 /* Attempt to free all objects */
1870 for_each_online_node(node
) {
1871 struct kmem_cache_node
*n
= get_node(s
, node
);
1873 free_list(s
, n
, &n
->partial
);
1874 if (atomic_long_read(&n
->nr_slabs
))
1877 free_kmem_cache_nodes(s
);
1882 * Close a cache and release the kmem_cache structure
1883 * (must be used for caches created using kmem_cache_create)
1885 void kmem_cache_destroy(struct kmem_cache
*s
)
1887 down_write(&slub_lock
);
1891 if (kmem_cache_close(s
))
1893 sysfs_slab_remove(s
);
1896 up_write(&slub_lock
);
1898 EXPORT_SYMBOL(kmem_cache_destroy
);
1900 /********************************************************************
1902 *******************************************************************/
1904 struct kmem_cache kmalloc_caches
[KMALLOC_SHIFT_HIGH
+ 1] __cacheline_aligned
;
1905 EXPORT_SYMBOL(kmalloc_caches
);
1907 #ifdef CONFIG_ZONE_DMA
1908 static struct kmem_cache
*kmalloc_caches_dma
[KMALLOC_SHIFT_HIGH
+ 1];
1911 static int __init
setup_slub_min_order(char *str
)
1913 get_option (&str
, &slub_min_order
);
1918 __setup("slub_min_order=", setup_slub_min_order
);
1920 static int __init
setup_slub_max_order(char *str
)
1922 get_option (&str
, &slub_max_order
);
1927 __setup("slub_max_order=", setup_slub_max_order
);
1929 static int __init
setup_slub_min_objects(char *str
)
1931 get_option (&str
, &slub_min_objects
);
1936 __setup("slub_min_objects=", setup_slub_min_objects
);
1938 static int __init
setup_slub_nomerge(char *str
)
1944 __setup("slub_nomerge", setup_slub_nomerge
);
1946 static int __init
setup_slub_debug(char *str
)
1948 if (!str
|| *str
!= '=')
1949 slub_debug
= DEBUG_DEFAULT_FLAGS
;
1952 if (*str
== 0 || *str
== ',')
1953 slub_debug
= DEBUG_DEFAULT_FLAGS
;
1955 for( ;*str
&& *str
!= ','; str
++)
1957 case 'f' : case 'F' :
1958 slub_debug
|= SLAB_DEBUG_FREE
;
1960 case 'z' : case 'Z' :
1961 slub_debug
|= SLAB_RED_ZONE
;
1963 case 'p' : case 'P' :
1964 slub_debug
|= SLAB_POISON
;
1966 case 'u' : case 'U' :
1967 slub_debug
|= SLAB_STORE_USER
;
1969 case 't' : case 'T' :
1970 slub_debug
|= SLAB_TRACE
;
1973 printk(KERN_ERR
"slub_debug option '%c' "
1974 "unknown. skipped\n",*str
);
1979 slub_debug_slabs
= str
+ 1;
1983 __setup("slub_debug", setup_slub_debug
);
1985 static struct kmem_cache
*create_kmalloc_cache(struct kmem_cache
*s
,
1986 const char *name
, int size
, gfp_t gfp_flags
)
1988 unsigned int flags
= 0;
1990 if (gfp_flags
& SLUB_DMA
)
1991 flags
= SLAB_CACHE_DMA
;
1993 down_write(&slub_lock
);
1994 if (!kmem_cache_open(s
, gfp_flags
, name
, size
, ARCH_KMALLOC_MINALIGN
,
1998 list_add(&s
->list
, &slab_caches
);
1999 up_write(&slub_lock
);
2000 if (sysfs_slab_add(s
))
2005 panic("Creation of kmalloc slab %s size=%d failed.\n", name
, size
);
2008 static struct kmem_cache
*get_slab(size_t size
, gfp_t flags
)
2010 int index
= kmalloc_index(size
);
2015 /* Allocation too large? */
2018 #ifdef CONFIG_ZONE_DMA
2019 if ((flags
& SLUB_DMA
)) {
2020 struct kmem_cache
*s
;
2021 struct kmem_cache
*x
;
2025 s
= kmalloc_caches_dma
[index
];
2029 /* Dynamically create dma cache */
2030 x
= kmalloc(kmem_size
, flags
& ~SLUB_DMA
);
2032 panic("Unable to allocate memory for dma cache\n");
2034 if (index
<= KMALLOC_SHIFT_HIGH
)
2035 realsize
= 1 << index
;
2043 text
= kasprintf(flags
& ~SLUB_DMA
, "kmalloc_dma-%d",
2044 (unsigned int)realsize
);
2045 s
= create_kmalloc_cache(x
, text
, realsize
, flags
);
2046 kmalloc_caches_dma
[index
] = s
;
2050 return &kmalloc_caches
[index
];
2053 void *__kmalloc(size_t size
, gfp_t flags
)
2055 struct kmem_cache
*s
= get_slab(size
, flags
);
2058 return slab_alloc(s
, flags
, -1, __builtin_return_address(0));
2061 EXPORT_SYMBOL(__kmalloc
);
2064 void *__kmalloc_node(size_t size
, gfp_t flags
, int node
)
2066 struct kmem_cache
*s
= get_slab(size
, flags
);
2069 return slab_alloc(s
, flags
, node
, __builtin_return_address(0));
2072 EXPORT_SYMBOL(__kmalloc_node
);
2075 size_t ksize(const void *object
)
2077 struct page
*page
= get_object_page(object
);
2078 struct kmem_cache
*s
;
2085 * Debugging requires use of the padding between object
2086 * and whatever may come after it.
2088 if (s
->flags
& (SLAB_RED_ZONE
| SLAB_POISON
))
2092 * If we have the need to store the freelist pointer
2093 * back there or track user information then we can
2094 * only use the space before that information.
2096 if (s
->flags
& (SLAB_DESTROY_BY_RCU
| SLAB_STORE_USER
))
2100 * Else we can use all the padding etc for the allocation
2104 EXPORT_SYMBOL(ksize
);
2106 void kfree(const void *x
)
2108 struct kmem_cache
*s
;
2114 page
= virt_to_head_page(x
);
2117 slab_free(s
, page
, (void *)x
, __builtin_return_address(0));
2119 EXPORT_SYMBOL(kfree
);
2122 * krealloc - reallocate memory. The contents will remain unchanged.
2124 * @p: object to reallocate memory for.
2125 * @new_size: how many bytes of memory are required.
2126 * @flags: the type of memory to allocate.
2128 * The contents of the object pointed to are preserved up to the
2129 * lesser of the new and old sizes. If @p is %NULL, krealloc()
2130 * behaves exactly like kmalloc(). If @size is 0 and @p is not a
2131 * %NULL pointer, the object pointed to is freed.
2133 void *krealloc(const void *p
, size_t new_size
, gfp_t flags
)
2135 struct kmem_cache
*new_cache
;
2140 return kmalloc(new_size
, flags
);
2142 if (unlikely(!new_size
)) {
2147 page
= virt_to_head_page(p
);
2149 new_cache
= get_slab(new_size
, flags
);
2152 * If new size fits in the current cache, bail out.
2154 if (likely(page
->slab
== new_cache
))
2157 ret
= kmalloc(new_size
, flags
);
2159 memcpy(ret
, p
, min(new_size
, ksize(p
)));
2164 EXPORT_SYMBOL(krealloc
);
2166 /********************************************************************
2167 * Basic setup of slabs
2168 *******************************************************************/
2170 void __init
kmem_cache_init(void)
2176 * Must first have the slab cache available for the allocations of the
2177 * struct kmalloc_cache_node's. There is special bootstrap code in
2178 * kmem_cache_open for slab_state == DOWN.
2180 create_kmalloc_cache(&kmalloc_caches
[0], "kmem_cache_node",
2181 sizeof(struct kmem_cache_node
), GFP_KERNEL
);
2184 /* Able to allocate the per node structures */
2185 slab_state
= PARTIAL
;
2187 /* Caches that are not of the two-to-the-power-of size */
2188 create_kmalloc_cache(&kmalloc_caches
[1],
2189 "kmalloc-96", 96, GFP_KERNEL
);
2190 create_kmalloc_cache(&kmalloc_caches
[2],
2191 "kmalloc-192", 192, GFP_KERNEL
);
2193 for (i
= KMALLOC_SHIFT_LOW
; i
<= KMALLOC_SHIFT_HIGH
; i
++)
2194 create_kmalloc_cache(&kmalloc_caches
[i
],
2195 "kmalloc", 1 << i
, GFP_KERNEL
);
2199 /* Provide the correct kmalloc names now that the caches are up */
2200 for (i
= KMALLOC_SHIFT_LOW
; i
<= KMALLOC_SHIFT_HIGH
; i
++)
2201 kmalloc_caches
[i
]. name
=
2202 kasprintf(GFP_KERNEL
, "kmalloc-%d", 1 << i
);
2205 register_cpu_notifier(&slab_notifier
);
2208 if (nr_cpu_ids
) /* Remove when nr_cpu_ids is fixed upstream ! */
2209 kmem_size
= offsetof(struct kmem_cache
, cpu_slab
)
2210 + nr_cpu_ids
* sizeof(struct page
*);
2212 printk(KERN_INFO
"SLUB: Genslabs=%d, HWalign=%d, Order=%d-%d, MinObjects=%d,"
2213 " Processors=%d, Nodes=%d\n",
2214 KMALLOC_SHIFT_HIGH
, L1_CACHE_BYTES
,
2215 slub_min_order
, slub_max_order
, slub_min_objects
,
2216 nr_cpu_ids
, nr_node_ids
);
2220 * Find a mergeable slab cache
2222 static int slab_unmergeable(struct kmem_cache
*s
)
2224 if (slub_nomerge
|| (s
->flags
& SLUB_NEVER_MERGE
))
2227 if (s
->ctor
|| s
->dtor
)
2233 static struct kmem_cache
*find_mergeable(size_t size
,
2234 size_t align
, unsigned long flags
,
2235 void (*ctor
)(void *, struct kmem_cache
*, unsigned long),
2236 void (*dtor
)(void *, struct kmem_cache
*, unsigned long))
2238 struct list_head
*h
;
2240 if (slub_nomerge
|| (flags
& SLUB_NEVER_MERGE
))
2246 size
= ALIGN(size
, sizeof(void *));
2247 align
= calculate_alignment(flags
, align
, size
);
2248 size
= ALIGN(size
, align
);
2250 list_for_each(h
, &slab_caches
) {
2251 struct kmem_cache
*s
=
2252 container_of(h
, struct kmem_cache
, list
);
2254 if (slab_unmergeable(s
))
2260 if (((flags
| slub_debug
) & SLUB_MERGE_SAME
) !=
2261 (s
->flags
& SLUB_MERGE_SAME
))
2264 * Check if alignment is compatible.
2265 * Courtesy of Adrian Drzewiecki
2267 if ((s
->size
& ~(align
-1)) != s
->size
)
2270 if (s
->size
- size
>= sizeof(void *))
2278 struct kmem_cache
*kmem_cache_create(const char *name
, size_t size
,
2279 size_t align
, unsigned long flags
,
2280 void (*ctor
)(void *, struct kmem_cache
*, unsigned long),
2281 void (*dtor
)(void *, struct kmem_cache
*, unsigned long))
2283 struct kmem_cache
*s
;
2285 down_write(&slub_lock
);
2286 s
= find_mergeable(size
, align
, flags
, dtor
, ctor
);
2290 * Adjust the object sizes so that we clear
2291 * the complete object on kzalloc.
2293 s
->objsize
= max(s
->objsize
, (int)size
);
2294 s
->inuse
= max_t(int, s
->inuse
, ALIGN(size
, sizeof(void *)));
2295 if (sysfs_slab_alias(s
, name
))
2298 s
= kmalloc(kmem_size
, GFP_KERNEL
);
2299 if (s
&& kmem_cache_open(s
, GFP_KERNEL
, name
,
2300 size
, align
, flags
, ctor
, dtor
)) {
2301 if (sysfs_slab_add(s
)) {
2305 list_add(&s
->list
, &slab_caches
);
2309 up_write(&slub_lock
);
2313 up_write(&slub_lock
);
2314 if (flags
& SLAB_PANIC
)
2315 panic("Cannot create slabcache %s\n", name
);
2320 EXPORT_SYMBOL(kmem_cache_create
);
2322 void *kmem_cache_zalloc(struct kmem_cache
*s
, gfp_t flags
)
2326 x
= slab_alloc(s
, flags
, -1, __builtin_return_address(0));
2328 memset(x
, 0, s
->objsize
);
2331 EXPORT_SYMBOL(kmem_cache_zalloc
);
2334 static void for_all_slabs(void (*func
)(struct kmem_cache
*, int), int cpu
)
2336 struct list_head
*h
;
2338 down_read(&slub_lock
);
2339 list_for_each(h
, &slab_caches
) {
2340 struct kmem_cache
*s
=
2341 container_of(h
, struct kmem_cache
, list
);
2345 up_read(&slub_lock
);
2349 * Use the cpu notifier to insure that the slab are flushed
2352 static int __cpuinit
slab_cpuup_callback(struct notifier_block
*nfb
,
2353 unsigned long action
, void *hcpu
)
2355 long cpu
= (long)hcpu
;
2358 case CPU_UP_CANCELED
:
2360 for_all_slabs(__flush_cpu_slab
, cpu
);
2368 static struct notifier_block __cpuinitdata slab_notifier
=
2369 { &slab_cpuup_callback
, NULL
, 0 };
2373 /***************************************************************
2374 * Compatiblility definitions
2375 **************************************************************/
2377 int kmem_cache_shrink(struct kmem_cache
*s
)
2382 EXPORT_SYMBOL(kmem_cache_shrink
);
2386 /*****************************************************************
2387 * Generic reaper used to support the page allocator
2388 * (the cpu slabs are reaped by a per slab workqueue).
2390 * Maybe move this to the page allocator?
2391 ****************************************************************/
2393 static DEFINE_PER_CPU(unsigned long, reap_node
);
2395 static void init_reap_node(int cpu
)
2399 node
= next_node(cpu_to_node(cpu
), node_online_map
);
2400 if (node
== MAX_NUMNODES
)
2401 node
= first_node(node_online_map
);
2403 __get_cpu_var(reap_node
) = node
;
2406 static void next_reap_node(void)
2408 int node
= __get_cpu_var(reap_node
);
2411 * Also drain per cpu pages on remote zones
2413 if (node
!= numa_node_id())
2414 drain_node_pages(node
);
2416 node
= next_node(node
, node_online_map
);
2417 if (unlikely(node
>= MAX_NUMNODES
))
2418 node
= first_node(node_online_map
);
2419 __get_cpu_var(reap_node
) = node
;
2422 #define init_reap_node(cpu) do { } while (0)
2423 #define next_reap_node(void) do { } while (0)
2426 #define REAPTIMEOUT_CPUC (2*HZ)
2429 static DEFINE_PER_CPU(struct delayed_work
, reap_work
);
2431 static void cache_reap(struct work_struct
*unused
)
2434 refresh_cpu_vm_stats(smp_processor_id());
2435 schedule_delayed_work(&__get_cpu_var(reap_work
),
2439 static void __devinit
start_cpu_timer(int cpu
)
2441 struct delayed_work
*reap_work
= &per_cpu(reap_work
, cpu
);
2444 * When this gets called from do_initcalls via cpucache_init(),
2445 * init_workqueues() has already run, so keventd will be setup
2448 if (keventd_up() && reap_work
->work
.func
== NULL
) {
2449 init_reap_node(cpu
);
2450 INIT_DELAYED_WORK(reap_work
, cache_reap
);
2451 schedule_delayed_work_on(cpu
, reap_work
, HZ
+ 3 * cpu
);
2455 static int __init
cpucache_init(void)
2460 * Register the timers that drain pcp pages and update vm statistics
2462 for_each_online_cpu(cpu
)
2463 start_cpu_timer(cpu
);
2466 __initcall(cpucache_init
);
2469 #ifdef SLUB_RESILIENCY_TEST
2470 static unsigned long validate_slab_cache(struct kmem_cache
*s
);
2472 static void resiliency_test(void)
2476 printk(KERN_ERR
"SLUB resiliency testing\n");
2477 printk(KERN_ERR
"-----------------------\n");
2478 printk(KERN_ERR
"A. Corruption after allocation\n");
2480 p
= kzalloc(16, GFP_KERNEL
);
2482 printk(KERN_ERR
"\n1. kmalloc-16: Clobber Redzone/next pointer"
2483 " 0x12->0x%p\n\n", p
+ 16);
2485 validate_slab_cache(kmalloc_caches
+ 4);
2487 /* Hmmm... The next two are dangerous */
2488 p
= kzalloc(32, GFP_KERNEL
);
2489 p
[32 + sizeof(void *)] = 0x34;
2490 printk(KERN_ERR
"\n2. kmalloc-32: Clobber next pointer/next slab"
2491 " 0x34 -> -0x%p\n", p
);
2492 printk(KERN_ERR
"If allocated object is overwritten then not detectable\n\n");
2494 validate_slab_cache(kmalloc_caches
+ 5);
2495 p
= kzalloc(64, GFP_KERNEL
);
2496 p
+= 64 + (get_cycles() & 0xff) * sizeof(void *);
2498 printk(KERN_ERR
"\n3. kmalloc-64: corrupting random byte 0x56->0x%p\n",
2500 printk(KERN_ERR
"If allocated object is overwritten then not detectable\n\n");
2501 validate_slab_cache(kmalloc_caches
+ 6);
2503 printk(KERN_ERR
"\nB. Corruption after free\n");
2504 p
= kzalloc(128, GFP_KERNEL
);
2507 printk(KERN_ERR
"1. kmalloc-128: Clobber first word 0x78->0x%p\n\n", p
);
2508 validate_slab_cache(kmalloc_caches
+ 7);
2510 p
= kzalloc(256, GFP_KERNEL
);
2513 printk(KERN_ERR
"\n2. kmalloc-256: Clobber 50th byte 0x9a->0x%p\n\n", p
);
2514 validate_slab_cache(kmalloc_caches
+ 8);
2516 p
= kzalloc(512, GFP_KERNEL
);
2519 printk(KERN_ERR
"\n3. kmalloc-512: Clobber redzone 0xab->0x%p\n\n", p
);
2520 validate_slab_cache(kmalloc_caches
+ 9);
2523 static void resiliency_test(void) {};
2527 * These are not as efficient as kmalloc for the non debug case.
2528 * We do not have the page struct available so we have to touch one
2529 * cacheline in struct kmem_cache to check slab flags.
2531 void *__kmalloc_track_caller(size_t size
, gfp_t gfpflags
, void *caller
)
2533 struct kmem_cache
*s
= get_slab(size
, gfpflags
);
2538 return slab_alloc(s
, gfpflags
, -1, caller
);
2541 void *__kmalloc_node_track_caller(size_t size
, gfp_t gfpflags
,
2542 int node
, void *caller
)
2544 struct kmem_cache
*s
= get_slab(size
, gfpflags
);
2549 return slab_alloc(s
, gfpflags
, node
, caller
);
2554 static unsigned long count_partial(struct kmem_cache_node
*n
)
2556 unsigned long flags
;
2557 unsigned long x
= 0;
2560 spin_lock_irqsave(&n
->list_lock
, flags
);
2561 list_for_each_entry(page
, &n
->partial
, lru
)
2563 spin_unlock_irqrestore(&n
->list_lock
, flags
);
2567 enum slab_stat_type
{
2574 #define SO_FULL (1 << SL_FULL)
2575 #define SO_PARTIAL (1 << SL_PARTIAL)
2576 #define SO_CPU (1 << SL_CPU)
2577 #define SO_OBJECTS (1 << SL_OBJECTS)
2579 static unsigned long slab_objects(struct kmem_cache
*s
,
2580 char *buf
, unsigned long flags
)
2582 unsigned long total
= 0;
2586 unsigned long *nodes
;
2587 unsigned long *per_cpu
;
2589 nodes
= kzalloc(2 * sizeof(unsigned long) * nr_node_ids
, GFP_KERNEL
);
2590 per_cpu
= nodes
+ nr_node_ids
;
2592 for_each_possible_cpu(cpu
) {
2593 struct page
*page
= s
->cpu_slab
[cpu
];
2597 node
= page_to_nid(page
);
2598 if (flags
& SO_CPU
) {
2601 if (flags
& SO_OBJECTS
)
2612 for_each_online_node(node
) {
2613 struct kmem_cache_node
*n
= get_node(s
, node
);
2615 if (flags
& SO_PARTIAL
) {
2616 if (flags
& SO_OBJECTS
)
2617 x
= count_partial(n
);
2624 if (flags
& SO_FULL
) {
2625 int full_slabs
= atomic_read(&n
->nr_slabs
)
2629 if (flags
& SO_OBJECTS
)
2630 x
= full_slabs
* s
->objects
;
2638 x
= sprintf(buf
, "%lu", total
);
2640 for_each_online_node(node
)
2642 x
+= sprintf(buf
+ x
, " N%d=%lu",
2646 return x
+ sprintf(buf
+ x
, "\n");
2649 static int any_slab_objects(struct kmem_cache
*s
)
2654 for_each_possible_cpu(cpu
)
2655 if (s
->cpu_slab
[cpu
])
2658 for_each_node(node
) {
2659 struct kmem_cache_node
*n
= get_node(s
, node
);
2661 if (n
->nr_partial
|| atomic_read(&n
->nr_slabs
))
2667 #define to_slab_attr(n) container_of(n, struct slab_attribute, attr)
2668 #define to_slab(n) container_of(n, struct kmem_cache, kobj);
2670 struct slab_attribute
{
2671 struct attribute attr
;
2672 ssize_t (*show
)(struct kmem_cache
*s
, char *buf
);
2673 ssize_t (*store
)(struct kmem_cache
*s
, const char *x
, size_t count
);
2676 #define SLAB_ATTR_RO(_name) \
2677 static struct slab_attribute _name##_attr = __ATTR_RO(_name)
2679 #define SLAB_ATTR(_name) \
2680 static struct slab_attribute _name##_attr = \
2681 __ATTR(_name, 0644, _name##_show, _name##_store)
2684 static ssize_t
slab_size_show(struct kmem_cache
*s
, char *buf
)
2686 return sprintf(buf
, "%d\n", s
->size
);
2688 SLAB_ATTR_RO(slab_size
);
2690 static ssize_t
align_show(struct kmem_cache
*s
, char *buf
)
2692 return sprintf(buf
, "%d\n", s
->align
);
2694 SLAB_ATTR_RO(align
);
2696 static ssize_t
object_size_show(struct kmem_cache
*s
, char *buf
)
2698 return sprintf(buf
, "%d\n", s
->objsize
);
2700 SLAB_ATTR_RO(object_size
);
2702 static ssize_t
objs_per_slab_show(struct kmem_cache
*s
, char *buf
)
2704 return sprintf(buf
, "%d\n", s
->objects
);
2706 SLAB_ATTR_RO(objs_per_slab
);
2708 static ssize_t
order_show(struct kmem_cache
*s
, char *buf
)
2710 return sprintf(buf
, "%d\n", s
->order
);
2712 SLAB_ATTR_RO(order
);
2714 static ssize_t
ctor_show(struct kmem_cache
*s
, char *buf
)
2717 int n
= sprint_symbol(buf
, (unsigned long)s
->ctor
);
2719 return n
+ sprintf(buf
+ n
, "\n");
2725 static ssize_t
dtor_show(struct kmem_cache
*s
, char *buf
)
2728 int n
= sprint_symbol(buf
, (unsigned long)s
->dtor
);
2730 return n
+ sprintf(buf
+ n
, "\n");
2736 static ssize_t
aliases_show(struct kmem_cache
*s
, char *buf
)
2738 return sprintf(buf
, "%d\n", s
->refcount
- 1);
2740 SLAB_ATTR_RO(aliases
);
2742 static ssize_t
slabs_show(struct kmem_cache
*s
, char *buf
)
2744 return slab_objects(s
, buf
, SO_FULL
|SO_PARTIAL
|SO_CPU
);
2746 SLAB_ATTR_RO(slabs
);
2748 static ssize_t
partial_show(struct kmem_cache
*s
, char *buf
)
2750 return slab_objects(s
, buf
, SO_PARTIAL
);
2752 SLAB_ATTR_RO(partial
);
2754 static ssize_t
cpu_slabs_show(struct kmem_cache
*s
, char *buf
)
2756 return slab_objects(s
, buf
, SO_CPU
);
2758 SLAB_ATTR_RO(cpu_slabs
);
2760 static ssize_t
objects_show(struct kmem_cache
*s
, char *buf
)
2762 return slab_objects(s
, buf
, SO_FULL
|SO_PARTIAL
|SO_CPU
|SO_OBJECTS
);
2764 SLAB_ATTR_RO(objects
);
2766 static ssize_t
sanity_checks_show(struct kmem_cache
*s
, char *buf
)
2768 return sprintf(buf
, "%d\n", !!(s
->flags
& SLAB_DEBUG_FREE
));
2771 static ssize_t
sanity_checks_store(struct kmem_cache
*s
,
2772 const char *buf
, size_t length
)
2774 s
->flags
&= ~SLAB_DEBUG_FREE
;
2776 s
->flags
|= SLAB_DEBUG_FREE
;
2779 SLAB_ATTR(sanity_checks
);
2781 static ssize_t
trace_show(struct kmem_cache
*s
, char *buf
)
2783 return sprintf(buf
, "%d\n", !!(s
->flags
& SLAB_TRACE
));
2786 static ssize_t
trace_store(struct kmem_cache
*s
, const char *buf
,
2789 s
->flags
&= ~SLAB_TRACE
;
2791 s
->flags
|= SLAB_TRACE
;
2796 static ssize_t
reclaim_account_show(struct kmem_cache
*s
, char *buf
)
2798 return sprintf(buf
, "%d\n", !!(s
->flags
& SLAB_RECLAIM_ACCOUNT
));
2801 static ssize_t
reclaim_account_store(struct kmem_cache
*s
,
2802 const char *buf
, size_t length
)
2804 s
->flags
&= ~SLAB_RECLAIM_ACCOUNT
;
2806 s
->flags
|= SLAB_RECLAIM_ACCOUNT
;
2809 SLAB_ATTR(reclaim_account
);
2811 static ssize_t
hwcache_align_show(struct kmem_cache
*s
, char *buf
)
2813 return sprintf(buf
, "%d\n", !!(s
->flags
&
2814 (SLAB_HWCACHE_ALIGN
|SLAB_MUST_HWCACHE_ALIGN
)));
2816 SLAB_ATTR_RO(hwcache_align
);
2818 #ifdef CONFIG_ZONE_DMA
2819 static ssize_t
cache_dma_show(struct kmem_cache
*s
, char *buf
)
2821 return sprintf(buf
, "%d\n", !!(s
->flags
& SLAB_CACHE_DMA
));
2823 SLAB_ATTR_RO(cache_dma
);
2826 static ssize_t
destroy_by_rcu_show(struct kmem_cache
*s
, char *buf
)
2828 return sprintf(buf
, "%d\n", !!(s
->flags
& SLAB_DESTROY_BY_RCU
));
2830 SLAB_ATTR_RO(destroy_by_rcu
);
2832 static ssize_t
red_zone_show(struct kmem_cache
*s
, char *buf
)
2834 return sprintf(buf
, "%d\n", !!(s
->flags
& SLAB_RED_ZONE
));
2837 static ssize_t
red_zone_store(struct kmem_cache
*s
,
2838 const char *buf
, size_t length
)
2840 if (any_slab_objects(s
))
2843 s
->flags
&= ~SLAB_RED_ZONE
;
2845 s
->flags
|= SLAB_RED_ZONE
;
2849 SLAB_ATTR(red_zone
);
2851 static ssize_t
poison_show(struct kmem_cache
*s
, char *buf
)
2853 return sprintf(buf
, "%d\n", !!(s
->flags
& SLAB_POISON
));
2856 static ssize_t
poison_store(struct kmem_cache
*s
,
2857 const char *buf
, size_t length
)
2859 if (any_slab_objects(s
))
2862 s
->flags
&= ~SLAB_POISON
;
2864 s
->flags
|= SLAB_POISON
;
2870 static ssize_t
store_user_show(struct kmem_cache
*s
, char *buf
)
2872 return sprintf(buf
, "%d\n", !!(s
->flags
& SLAB_STORE_USER
));
2875 static ssize_t
store_user_store(struct kmem_cache
*s
,
2876 const char *buf
, size_t length
)
2878 if (any_slab_objects(s
))
2881 s
->flags
&= ~SLAB_STORE_USER
;
2883 s
->flags
|= SLAB_STORE_USER
;
2887 SLAB_ATTR(store_user
);
2890 static ssize_t
defrag_ratio_show(struct kmem_cache
*s
, char *buf
)
2892 return sprintf(buf
, "%d\n", s
->defrag_ratio
/ 10);
2895 static ssize_t
defrag_ratio_store(struct kmem_cache
*s
,
2896 const char *buf
, size_t length
)
2898 int n
= simple_strtoul(buf
, NULL
, 10);
2901 s
->defrag_ratio
= n
* 10;
2904 SLAB_ATTR(defrag_ratio
);
2907 static struct attribute
* slab_attrs
[] = {
2908 &slab_size_attr
.attr
,
2909 &object_size_attr
.attr
,
2910 &objs_per_slab_attr
.attr
,
2915 &cpu_slabs_attr
.attr
,
2920 &sanity_checks_attr
.attr
,
2922 &hwcache_align_attr
.attr
,
2923 &reclaim_account_attr
.attr
,
2924 &destroy_by_rcu_attr
.attr
,
2925 &red_zone_attr
.attr
,
2927 &store_user_attr
.attr
,
2928 #ifdef CONFIG_ZONE_DMA
2929 &cache_dma_attr
.attr
,
2932 &defrag_ratio_attr
.attr
,
2937 static struct attribute_group slab_attr_group
= {
2938 .attrs
= slab_attrs
,
2941 static ssize_t
slab_attr_show(struct kobject
*kobj
,
2942 struct attribute
*attr
,
2945 struct slab_attribute
*attribute
;
2946 struct kmem_cache
*s
;
2949 attribute
= to_slab_attr(attr
);
2952 if (!attribute
->show
)
2955 err
= attribute
->show(s
, buf
);
2960 static ssize_t
slab_attr_store(struct kobject
*kobj
,
2961 struct attribute
*attr
,
2962 const char *buf
, size_t len
)
2964 struct slab_attribute
*attribute
;
2965 struct kmem_cache
*s
;
2968 attribute
= to_slab_attr(attr
);
2971 if (!attribute
->store
)
2974 err
= attribute
->store(s
, buf
, len
);
2979 static struct sysfs_ops slab_sysfs_ops
= {
2980 .show
= slab_attr_show
,
2981 .store
= slab_attr_store
,
2984 static struct kobj_type slab_ktype
= {
2985 .sysfs_ops
= &slab_sysfs_ops
,
2988 static int uevent_filter(struct kset
*kset
, struct kobject
*kobj
)
2990 struct kobj_type
*ktype
= get_ktype(kobj
);
2992 if (ktype
== &slab_ktype
)
2997 static struct kset_uevent_ops slab_uevent_ops
= {
2998 .filter
= uevent_filter
,
3001 decl_subsys(slab
, &slab_ktype
, &slab_uevent_ops
);
3003 #define ID_STR_LENGTH 64
3005 /* Create a unique string id for a slab cache:
3007 * :[flags-]size:[memory address of kmemcache]
3009 static char *create_unique_id(struct kmem_cache
*s
)
3011 char *name
= kmalloc(ID_STR_LENGTH
, GFP_KERNEL
);
3018 * First flags affecting slabcache operations. We will only
3019 * get here for aliasable slabs so we do not need to support
3020 * too many flags. The flags here must cover all flags that
3021 * are matched during merging to guarantee that the id is
3024 if (s
->flags
& SLAB_CACHE_DMA
)
3026 if (s
->flags
& SLAB_RECLAIM_ACCOUNT
)
3028 if (s
->flags
& SLAB_DEBUG_FREE
)
3032 p
+= sprintf(p
, "%07d", s
->size
);
3033 BUG_ON(p
> name
+ ID_STR_LENGTH
- 1);
3037 static int sysfs_slab_add(struct kmem_cache
*s
)
3043 if (slab_state
< SYSFS
)
3044 /* Defer until later */
3047 unmergeable
= slab_unmergeable(s
);
3050 * Slabcache can never be merged so we can use the name proper.
3051 * This is typically the case for debug situations. In that
3052 * case we can catch duplicate names easily.
3054 sysfs_remove_link(&slab_subsys
.kset
.kobj
, s
->name
);
3058 * Create a unique name for the slab as a target
3061 name
= create_unique_id(s
);
3064 kobj_set_kset_s(s
, slab_subsys
);
3065 kobject_set_name(&s
->kobj
, name
);
3066 kobject_init(&s
->kobj
);
3067 err
= kobject_add(&s
->kobj
);
3071 err
= sysfs_create_group(&s
->kobj
, &slab_attr_group
);
3074 kobject_uevent(&s
->kobj
, KOBJ_ADD
);
3076 /* Setup first alias */
3077 sysfs_slab_alias(s
, s
->name
);
3083 static void sysfs_slab_remove(struct kmem_cache
*s
)
3085 kobject_uevent(&s
->kobj
, KOBJ_REMOVE
);
3086 kobject_del(&s
->kobj
);
3090 * Need to buffer aliases during bootup until sysfs becomes
3091 * available lest we loose that information.
3093 struct saved_alias
{
3094 struct kmem_cache
*s
;
3096 struct saved_alias
*next
;
3099 struct saved_alias
*alias_list
;
3101 static int sysfs_slab_alias(struct kmem_cache
*s
, const char *name
)
3103 struct saved_alias
*al
;
3105 if (slab_state
== SYSFS
) {
3107 * If we have a leftover link then remove it.
3109 sysfs_remove_link(&slab_subsys
.kset
.kobj
, name
);
3110 return sysfs_create_link(&slab_subsys
.kset
.kobj
,
3114 al
= kmalloc(sizeof(struct saved_alias
), GFP_KERNEL
);
3120 al
->next
= alias_list
;
3125 static int __init
slab_sysfs_init(void)
3129 err
= subsystem_register(&slab_subsys
);
3131 printk(KERN_ERR
"Cannot register slab subsystem.\n");
3137 while (alias_list
) {
3138 struct saved_alias
*al
= alias_list
;
3140 alias_list
= alias_list
->next
;
3141 err
= sysfs_slab_alias(al
->s
, al
->name
);
3150 __initcall(slab_sysfs_init
);
3152 __initcall(finish_bootstrap
);