]> bbs.cooldavid.org Git - net-next-2.6.git/blame - mm/slub.c
slub: fix object tracking
[net-next-2.6.git] / mm / slub.c
CommitLineData
81819f0f
CL
1/*
2 * SLUB: A slab allocator that limits cache line use instead of queuing
3 * objects in per cpu and per node lists.
4 *
5 * The allocator synchronizes using per slab locks and only
6 * uses a centralized lock to manage a pool of partial slabs.
7 *
8 * (C) 2007 SGI, Christoph Lameter <clameter@sgi.com>
9 */
10
11#include <linux/mm.h>
12#include <linux/module.h>
13#include <linux/bit_spinlock.h>
14#include <linux/interrupt.h>
15#include <linux/bitops.h>
16#include <linux/slab.h>
17#include <linux/seq_file.h>
18#include <linux/cpu.h>
19#include <linux/cpuset.h>
20#include <linux/mempolicy.h>
21#include <linux/ctype.h>
22#include <linux/kallsyms.h>
23
24/*
25 * Lock order:
26 * 1. slab_lock(page)
27 * 2. slab->list_lock
28 *
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.
35 *
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).
41 *
42 * The list_lock is a centralized lock and thus we avoid taking it as
43 * much as possible. As long as SLUB does not have to handle partial
44 * slabs, operations can continue without any centralized lock. F.e.
45 * allocating a long series of objects that fill up slabs does not require
46 * the list lock.
47 *
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.
60 *
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.
65 *
66 * SLUB assigns one slab for allocation to each processor.
67 * Allocations only occur from these slabs called cpu slabs.
68 *
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.
74 *
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.
78 *
79 * Overloading of page flags that are otherwise used for LRU management.
80 *
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.
84 *
85 * PageError Slab requires special handling due to debug
86 * options set. This moves slab handling out of
87 * the fast path.
88 */
89
90/*
91 * Issues still to be resolved:
92 *
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.
97 *
98 * - Support PAGE_ALLOC_DEBUG. Should be easy to do.
99 *
100 * - Support DEBUG_SLAB_LEAK. Trouble is we do not know where the full
101 * slabs are in SLUB.
102 *
103 * - SLAB_DEBUG_INITIAL is not supported but I have never seen a use of
104 * it.
105 *
106 * - Variable sizing of the per node arrays
107 */
108
109/* Enable to test recovery from slab corruption on boot */
110#undef SLUB_RESILIENCY_TEST
111
112#if PAGE_SHIFT <= 12
113
114/*
115 * Small page size. Make sure that we do not fragment memory
116 */
117#define DEFAULT_MAX_ORDER 1
118#define DEFAULT_MIN_OBJECTS 4
119
120#else
121
122/*
123 * Large page machines are customarily able to handle larger
124 * page orders.
125 */
126#define DEFAULT_MAX_ORDER 2
127#define DEFAULT_MIN_OBJECTS 8
128
129#endif
130
131/*
132 * Flags from the regular SLAB that SLUB does not support:
133 */
134#define SLUB_UNIMPLEMENTED (SLAB_DEBUG_INITIAL)
135
136#define DEBUG_DEFAULT_FLAGS (SLAB_DEBUG_FREE | SLAB_RED_ZONE | \
137 SLAB_POISON | SLAB_STORE_USER)
138/*
139 * Set of flags that will prevent slab merging
140 */
141#define SLUB_NEVER_MERGE (SLAB_RED_ZONE | SLAB_POISON | SLAB_STORE_USER | \
142 SLAB_TRACE | SLAB_DESTROY_BY_RCU)
143
144#define SLUB_MERGE_SAME (SLAB_DEBUG_FREE | SLAB_RECLAIM_ACCOUNT | \
145 SLAB_CACHE_DMA)
146
147#ifndef ARCH_KMALLOC_MINALIGN
47bfdc0d 148#define ARCH_KMALLOC_MINALIGN __alignof__(unsigned long long)
81819f0f
CL
149#endif
150
151#ifndef ARCH_SLAB_MINALIGN
47bfdc0d 152#define ARCH_SLAB_MINALIGN __alignof__(unsigned long long)
81819f0f
CL
153#endif
154
155/* Internal SLUB flags */
156#define __OBJECT_POISON 0x80000000 /* Poison object */
157
158static int kmem_size = sizeof(struct kmem_cache);
159
160#ifdef CONFIG_SMP
161static struct notifier_block slab_notifier;
162#endif
163
164static enum {
165 DOWN, /* No slab functionality available */
166 PARTIAL, /* kmem_cache_open() works but kmalloc does not */
167 UP, /* Everything works */
168 SYSFS /* Sysfs up */
169} slab_state = DOWN;
170
171/* A list of all slab caches on the system */
172static DECLARE_RWSEM(slub_lock);
173LIST_HEAD(slab_caches);
174
175#ifdef CONFIG_SYSFS
176static int sysfs_slab_add(struct kmem_cache *);
177static int sysfs_slab_alias(struct kmem_cache *, const char *);
178static void sysfs_slab_remove(struct kmem_cache *);
179#else
180static int sysfs_slab_add(struct kmem_cache *s) { return 0; }
181static int sysfs_slab_alias(struct kmem_cache *s, const char *p) { return 0; }
182static void sysfs_slab_remove(struct kmem_cache *s) {}
183#endif
184
185/********************************************************************
186 * Core slab cache functions
187 *******************************************************************/
188
189int slab_is_available(void)
190{
191 return slab_state >= UP;
192}
193
194static inline struct kmem_cache_node *get_node(struct kmem_cache *s, int node)
195{
196#ifdef CONFIG_NUMA
197 return s->node[node];
198#else
199 return &s->local_node;
200#endif
201}
202
203/*
204 * Object debugging
205 */
206static void print_section(char *text, u8 *addr, unsigned int length)
207{
208 int i, offset;
209 int newline = 1;
210 char ascii[17];
211
212 ascii[16] = 0;
213
214 for (i = 0; i < length; i++) {
215 if (newline) {
216 printk(KERN_ERR "%10s 0x%p: ", text, addr + i);
217 newline = 0;
218 }
219 printk(" %02x", addr[i]);
220 offset = i % 16;
221 ascii[offset] = isgraph(addr[i]) ? addr[i] : '.';
222 if (offset == 15) {
223 printk(" %s\n",ascii);
224 newline = 1;
225 }
226 }
227 if (!newline) {
228 i %= 16;
229 while (i < 16) {
230 printk(" ");
231 ascii[i] = ' ';
232 i++;
233 }
234 printk(" %s\n", ascii);
235 }
236}
237
238/*
239 * Slow version of get and set free pointer.
240 *
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.
244 */
245static void *get_freepointer(struct kmem_cache *s, void *object)
246{
247 return *(void **)(object + s->offset);
248}
249
250static void set_freepointer(struct kmem_cache *s, void *object, void *fp)
251{
252 *(void **)(object + s->offset) = fp;
253}
254
255/*
256 * Tracking user of a slab.
257 */
258struct track {
259 void *addr; /* Called from address */
260 int cpu; /* Was running on cpu */
261 int pid; /* Pid context */
262 unsigned long when; /* When did the operation occur */
263};
264
265enum track_item { TRACK_ALLOC, TRACK_FREE };
266
267static struct track *get_track(struct kmem_cache *s, void *object,
268 enum track_item alloc)
269{
270 struct track *p;
271
272 if (s->offset)
273 p = object + s->offset + sizeof(void *);
274 else
275 p = object + s->inuse;
276
277 return p + alloc;
278}
279
280static void set_track(struct kmem_cache *s, void *object,
281 enum track_item alloc, void *addr)
282{
283 struct track *p;
284
285 if (s->offset)
286 p = object + s->offset + sizeof(void *);
287 else
288 p = object + s->inuse;
289
290 p += alloc;
291 if (addr) {
292 p->addr = addr;
293 p->cpu = smp_processor_id();
294 p->pid = current ? current->pid : -1;
295 p->when = jiffies;
296 } else
297 memset(p, 0, sizeof(struct track));
298}
299
81819f0f
CL
300static void init_tracking(struct kmem_cache *s, void *object)
301{
302 if (s->flags & SLAB_STORE_USER) {
303 set_track(s, object, TRACK_FREE, NULL);
304 set_track(s, object, TRACK_ALLOC, NULL);
305 }
306}
307
308static void print_track(const char *s, struct track *t)
309{
310 if (!t->addr)
311 return;
312
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);
316}
317
318static void print_trailer(struct kmem_cache *s, u8 *p)
319{
320 unsigned int off; /* Offset of last byte */
321
322 if (s->flags & SLAB_RED_ZONE)
323 print_section("Redzone", p + s->objsize,
324 s->inuse - s->objsize);
325
326 printk(KERN_ERR "FreePointer 0x%p -> 0x%p\n",
327 p + s->offset,
328 get_freepointer(s, p));
329
330 if (s->offset)
331 off = s->offset + sizeof(void *);
332 else
333 off = s->inuse;
334
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);
339 }
340
341 if (off != s->size)
342 /* Beginning of the filler is the free pointer */
343 print_section("Filler", p + off, s->size - off);
344}
345
346static void object_err(struct kmem_cache *s, struct page *page,
347 u8 *object, char *reason)
348{
349 u8 *addr = page_address(page);
350
351 printk(KERN_ERR "*** SLUB %s: %s@0x%p slab 0x%p\n",
352 s->name, reason, object, page);
353 printk(KERN_ERR " offset=%tu flags=0x%04lx inuse=%u freelist=0x%p\n",
354 object - addr, page->flags, page->inuse, page->freelist);
355 if (object > addr + 16)
356 print_section("Bytes b4", object - 16, 16);
357 print_section("Object", object, min(s->objsize, 128));
358 print_trailer(s, object);
359 dump_stack();
360}
361
362static void slab_err(struct kmem_cache *s, struct page *page, char *reason, ...)
363{
364 va_list args;
365 char buf[100];
366
367 va_start(args, reason);
368 vsnprintf(buf, sizeof(buf), reason, args);
369 va_end(args);
370 printk(KERN_ERR "*** SLUB %s: %s in slab @0x%p\n", s->name, buf,
371 page);
372 dump_stack();
373}
374
375static void init_object(struct kmem_cache *s, void *object, int active)
376{
377 u8 *p = object;
378
379 if (s->flags & __OBJECT_POISON) {
380 memset(p, POISON_FREE, s->objsize - 1);
381 p[s->objsize -1] = POISON_END;
382 }
383
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);
388}
389
390static int check_bytes(u8 *start, unsigned int value, unsigned int bytes)
391{
392 while (bytes) {
393 if (*start != (u8)value)
394 return 0;
395 start++;
396 bytes--;
397 }
398 return 1;
399}
400
401
402static int check_valid_pointer(struct kmem_cache *s, struct page *page,
403 void *object)
404{
405 void *base;
406
407 if (!object)
408 return 1;
409
410 base = page_address(page);
411 if (object < base || object >= base + s->objects * s->size ||
412 (object - base) % s->size) {
413 return 0;
414 }
415
416 return 1;
417}
418
419/*
420 * Object layout:
421 *
422 * object address
423 * Bytes of the object to be managed.
424 * If the freepointer may overlay the object then the free
425 * pointer is the first word of the object.
426 * Poisoning uses 0x6b (POISON_FREE) and the last byte is
427 * 0xa5 (POISON_END)
428 *
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.
435 *
436 * object + s->inuse
437 * A. Free pointer (if we cannot overwrite object on free)
438 * B. Tracking data for SLAB_STORE_USER
439 * C. Padding to reach required alignment boundary
440 * Padding is done using 0x5a (POISON_INUSE)
441 *
442 * object + s->size
443 *
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.
447 */
448
449static void restore_bytes(struct kmem_cache *s, char *message, u8 data,
450 void *from, void *to)
451{
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);
455}
456
457static int check_pad_bytes(struct kmem_cache *s, struct page *page, u8 *p)
458{
459 unsigned long off = s->inuse; /* The end of info */
460
461 if (s->offset)
462 /* Freepointer is placed after the object. */
463 off += sizeof(void *);
464
465 if (s->flags & SLAB_STORE_USER)
466 /* We also have user information there */
467 off += 2 * sizeof(struct track);
468
469 if (s->size == off)
470 return 1;
471
472 if (check_bytes(p + off, POISON_INUSE, s->size - off))
473 return 1;
474
475 object_err(s, page, p, "Object padding check fails");
476
477 /*
478 * Restore padding
479 */
480 restore_bytes(s, "object padding", POISON_INUSE, p + off, p + s->size);
481 return 0;
482}
483
484static int slab_pad_check(struct kmem_cache *s, struct page *page)
485{
486 u8 *p;
487 int length, remainder;
488
489 if (!(s->flags & SLAB_POISON))
490 return 1;
491
492 p = page_address(page);
493 length = s->objects * s->size;
494 remainder = (PAGE_SIZE << s->order) - length;
495 if (!remainder)
496 return 1;
497
498 if (!check_bytes(p + length, POISON_INUSE, remainder)) {
499 printk(KERN_ERR "SLUB: %s slab 0x%p: Padding fails check\n",
500 s->name, p);
501 dump_stack();
502 restore_bytes(s, "slab padding", POISON_INUSE, p + length,
503 p + length + remainder);
504 return 0;
505 }
506 return 1;
507}
508
509static int check_object(struct kmem_cache *s, struct page *page,
510 void *object, int active)
511{
512 u8 *p = object;
513 u8 *endobject = object + s->objsize;
514
515 if (s->flags & SLAB_RED_ZONE) {
516 unsigned int red =
517 active ? SLUB_RED_ACTIVE : SLUB_RED_INACTIVE;
518
519 if (!check_bytes(endobject, red, s->inuse - s->objsize)) {
520 object_err(s, page, object,
521 active ? "Redzone Active" : "Redzone Inactive");
522 restore_bytes(s, "redzone", red,
523 endobject, object + s->inuse);
524 return 0;
525 }
526 } else {
527 if ((s->flags & SLAB_POISON) && s->objsize < s->inuse &&
528 !check_bytes(endobject, POISON_INUSE,
529 s->inuse - s->objsize)) {
530 object_err(s, page, p, "Alignment padding check fails");
531 /*
532 * Fix it so that there will not be another report.
533 *
534 * Hmmm... We may be corrupting an object that now expects
535 * to be longer than allowed.
536 */
537 restore_bytes(s, "alignment padding", POISON_INUSE,
538 endobject, object + s->inuse);
539 }
540 }
541
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)) {
546
547 object_err(s, page, p, "Poison check failed");
548 restore_bytes(s, "Poison", POISON_FREE,
549 p, p + s->objsize -1);
550 restore_bytes(s, "Poison", POISON_END,
551 p + s->objsize - 1, p + s->objsize);
552 return 0;
553 }
554 /*
555 * check_pad_bytes cleans up on its own.
556 */
557 check_pad_bytes(s, page, p);
558 }
559
560 if (!s->offset && active)
561 /*
562 * Object and freepointer overlap. Cannot check
563 * freepointer while object is allocated.
564 */
565 return 1;
566
567 /* Check free pointer validity */
568 if (!check_valid_pointer(s, page, get_freepointer(s, p))) {
569 object_err(s, page, p, "Freepointer corrupt");
570 /*
571 * No choice but to zap it and thus loose the remainder
572 * of the free objects in this slab. May cause
573 * another error because the object count maybe
574 * wrong now.
575 */
576 set_freepointer(s, p, NULL);
577 return 0;
578 }
579 return 1;
580}
581
582static int check_slab(struct kmem_cache *s, struct page *page)
583{
584 VM_BUG_ON(!irqs_disabled());
585
586 if (!PageSlab(page)) {
587 printk(KERN_ERR "SLUB: %s Not a valid slab page @0x%p "
588 "flags=%lx mapping=0x%p count=%d \n",
589 s->name, page, page->flags, page->mapping,
590 page_count(page));
591 return 0;
592 }
593 if (page->offset * sizeof(void *) != s->offset) {
594 printk(KERN_ERR "SLUB: %s Corrupted offset %lu in slab @0x%p"
595 " flags=0x%lx mapping=0x%p count=%d\n",
596 s->name,
597 (unsigned long)(page->offset * sizeof(void *)),
598 page,
599 page->flags,
600 page->mapping,
601 page_count(page));
602 dump_stack();
603 return 0;
604 }
605 if (page->inuse > s->objects) {
606 printk(KERN_ERR "SLUB: %s Inuse %u > max %u in slab "
607 "page @0x%p flags=%lx mapping=0x%p count=%d\n",
608 s->name, page->inuse, s->objects, page, page->flags,
609 page->mapping, page_count(page));
610 dump_stack();
611 return 0;
612 }
613 /* Slab_pad_check fixes things up after itself */
614 slab_pad_check(s, page);
615 return 1;
616}
617
618/*
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.
622 */
623static int on_freelist(struct kmem_cache *s, struct page *page, void *search)
624{
625 int nr = 0;
626 void *fp = page->freelist;
627 void *object = NULL;
628
629 while (fp && nr <= s->objects) {
630 if (fp == search)
631 return 1;
632 if (!check_valid_pointer(s, page, fp)) {
633 if (object) {
634 object_err(s, page, object,
635 "Freechain corrupt");
636 set_freepointer(s, object, NULL);
637 break;
638 } else {
639 printk(KERN_ERR "SLUB: %s slab 0x%p "
640 "freepointer 0x%p corrupted.\n",
641 s->name, page, fp);
642 dump_stack();
643 page->freelist = NULL;
644 page->inuse = s->objects;
645 return 0;
646 }
647 break;
648 }
649 object = fp;
650 fp = get_freepointer(s, object);
651 nr++;
652 }
653
654 if (page->inuse != s->objects - nr) {
655 printk(KERN_ERR "slab %s: page 0x%p wrong object count."
656 " counter is %d but counted were %d\n",
657 s->name, page, page->inuse,
658 s->objects - nr);
659 page->inuse = s->objects - nr;
660 }
661 return search == NULL;
662}
663
664static int alloc_object_checks(struct kmem_cache *s, struct page *page,
665 void *object)
666{
667 if (!check_slab(s, page))
668 goto bad;
669
670 if (object && !on_freelist(s, page, object)) {
671 printk(KERN_ERR "SLUB: %s Object 0x%p@0x%p "
672 "already allocated.\n",
673 s->name, object, page);
674 goto dump;
675 }
676
677 if (!check_valid_pointer(s, page, object)) {
678 object_err(s, page, object, "Freelist Pointer check fails");
679 goto dump;
680 }
681
682 if (!object)
683 return 1;
684
685 if (!check_object(s, page, object, 0))
686 goto bad;
687 init_object(s, object, 1);
688
689 if (s->flags & SLAB_TRACE) {
690 printk(KERN_INFO "TRACE %s alloc 0x%p inuse=%d fp=0x%p\n",
691 s->name, object, page->inuse,
692 page->freelist);
693 dump_stack();
694 }
695 return 1;
696dump:
697 dump_stack();
698bad:
699 if (PageSlab(page)) {
700 /*
701 * If this is a slab page then lets do the best we can
702 * to avoid issues in the future. Marking all objects
703 * as used avoids touching the remainder.
704 */
705 printk(KERN_ERR "@@@ SLUB: %s slab 0x%p. Marking all objects used.\n",
706 s->name, page);
707 page->inuse = s->objects;
708 page->freelist = NULL;
709 /* Fix up fields that may be corrupted */
710 page->offset = s->offset / sizeof(void *);
711 }
712 return 0;
713}
714
715static int free_object_checks(struct kmem_cache *s, struct page *page,
716 void *object)
717{
718 if (!check_slab(s, page))
719 goto fail;
720
721 if (!check_valid_pointer(s, page, object)) {
722 printk(KERN_ERR "SLUB: %s slab 0x%p invalid "
723 "object pointer 0x%p\n",
724 s->name, page, object);
725 goto fail;
726 }
727
728 if (on_freelist(s, page, object)) {
729 printk(KERN_ERR "SLUB: %s slab 0x%p object "
730 "0x%p already free.\n", s->name, page, object);
731 goto fail;
732 }
733
734 if (!check_object(s, page, object, 1))
735 return 0;
736
737 if (unlikely(s != page->slab)) {
738 if (!PageSlab(page))
739 printk(KERN_ERR "slab_free %s size %d: attempt to"
740 "free object(0x%p) outside of slab.\n",
741 s->name, s->size, object);
742 else
743 if (!page->slab)
744 printk(KERN_ERR
745 "slab_free : no slab(NULL) for object 0x%p.\n",
746 object);
747 else
748 printk(KERN_ERR "slab_free %s(%d): object at 0x%p"
749 " belongs to slab %s(%d)\n",
750 s->name, s->size, object,
751 page->slab->name, page->slab->size);
752 goto fail;
753 }
754 if (s->flags & SLAB_TRACE) {
755 printk(KERN_INFO "TRACE %s free 0x%p inuse=%d fp=0x%p\n",
756 s->name, object, page->inuse,
757 page->freelist);
758 print_section("Object", object, s->objsize);
759 dump_stack();
760 }
761 init_object(s, object, 0);
762 return 1;
763fail:
764 dump_stack();
765 printk(KERN_ERR "@@@ SLUB: %s slab 0x%p object at 0x%p not freed.\n",
766 s->name, page, object);
767 return 0;
768}
769
770/*
771 * Slab allocation and freeing
772 */
773static struct page *allocate_slab(struct kmem_cache *s, gfp_t flags, int node)
774{
775 struct page * page;
776 int pages = 1 << s->order;
777
778 if (s->order)
779 flags |= __GFP_COMP;
780
781 if (s->flags & SLAB_CACHE_DMA)
782 flags |= SLUB_DMA;
783
784 if (node == -1)
785 page = alloc_pages(flags, s->order);
786 else
787 page = alloc_pages_node(node, flags, s->order);
788
789 if (!page)
790 return NULL;
791
792 mod_zone_page_state(page_zone(page),
793 (s->flags & SLAB_RECLAIM_ACCOUNT) ?
794 NR_SLAB_RECLAIMABLE : NR_SLAB_UNRECLAIMABLE,
795 pages);
796
797 return page;
798}
799
800static void setup_object(struct kmem_cache *s, struct page *page,
801 void *object)
802{
803 if (PageError(page)) {
804 init_object(s, object, 0);
805 init_tracking(s, object);
806 }
807
808 if (unlikely(s->ctor)) {
809 int mode = SLAB_CTOR_CONSTRUCTOR;
810
811 if (!(s->flags & __GFP_WAIT))
812 mode |= SLAB_CTOR_ATOMIC;
813
814 s->ctor(object, s, mode);
815 }
816}
817
818static struct page *new_slab(struct kmem_cache *s, gfp_t flags, int node)
819{
820 struct page *page;
821 struct kmem_cache_node *n;
822 void *start;
823 void *end;
824 void *last;
825 void *p;
826
827 if (flags & __GFP_NO_GROW)
828 return NULL;
829
830 BUG_ON(flags & ~(GFP_DMA | GFP_LEVEL_MASK));
831
832 if (flags & __GFP_WAIT)
833 local_irq_enable();
834
835 page = allocate_slab(s, flags & GFP_LEVEL_MASK, node);
836 if (!page)
837 goto out;
838
839 n = get_node(s, page_to_nid(page));
840 if (n)
841 atomic_long_inc(&n->nr_slabs);
842 page->offset = s->offset / sizeof(void *);
843 page->slab = s;
844 page->flags |= 1 << PG_slab;
845 if (s->flags & (SLAB_DEBUG_FREE | SLAB_RED_ZONE | SLAB_POISON |
846 SLAB_STORE_USER | SLAB_TRACE))
847 page->flags |= 1 << PG_error;
848
849 start = page_address(page);
850 end = start + s->objects * s->size;
851
852 if (unlikely(s->flags & SLAB_POISON))
853 memset(start, POISON_INUSE, PAGE_SIZE << s->order);
854
855 last = start;
856 for (p = start + s->size; p < end; p += s->size) {
857 setup_object(s, page, last);
858 set_freepointer(s, last, p);
859 last = p;
860 }
861 setup_object(s, page, last);
862 set_freepointer(s, last, NULL);
863
864 page->freelist = start;
865 page->inuse = 0;
866out:
867 if (flags & __GFP_WAIT)
868 local_irq_disable();
869 return page;
870}
871
872static void __free_slab(struct kmem_cache *s, struct page *page)
873{
874 int pages = 1 << s->order;
875
876 if (unlikely(PageError(page) || s->dtor)) {
877 void *start = page_address(page);
878 void *end = start + (pages << PAGE_SHIFT);
879 void *p;
880
881 slab_pad_check(s, page);
882 for (p = start; p <= end - s->size; p += s->size) {
883 if (s->dtor)
884 s->dtor(p, s, 0);
885 check_object(s, page, p, 0);
886 }
887 }
888
889 mod_zone_page_state(page_zone(page),
890 (s->flags & SLAB_RECLAIM_ACCOUNT) ?
891 NR_SLAB_RECLAIMABLE : NR_SLAB_UNRECLAIMABLE,
892 - pages);
893
894 page->mapping = NULL;
895 __free_pages(page, s->order);
896}
897
898static void rcu_free_slab(struct rcu_head *h)
899{
900 struct page *page;
901
902 page = container_of((struct list_head *)h, struct page, lru);
903 __free_slab(page->slab, page);
904}
905
906static void free_slab(struct kmem_cache *s, struct page *page)
907{
908 if (unlikely(s->flags & SLAB_DESTROY_BY_RCU)) {
909 /*
910 * RCU free overloads the RCU head over the LRU
911 */
912 struct rcu_head *head = (void *)&page->lru;
913
914 call_rcu(head, rcu_free_slab);
915 } else
916 __free_slab(s, page);
917}
918
919static void discard_slab(struct kmem_cache *s, struct page *page)
920{
921 struct kmem_cache_node *n = get_node(s, page_to_nid(page));
922
923 atomic_long_dec(&n->nr_slabs);
924 reset_page_mapcount(page);
925 page->flags &= ~(1 << PG_slab | 1 << PG_error);
926 free_slab(s, page);
927}
928
929/*
930 * Per slab locking using the pagelock
931 */
932static __always_inline void slab_lock(struct page *page)
933{
934 bit_spin_lock(PG_locked, &page->flags);
935}
936
937static __always_inline void slab_unlock(struct page *page)
938{
939 bit_spin_unlock(PG_locked, &page->flags);
940}
941
942static __always_inline int slab_trylock(struct page *page)
943{
944 int rc = 1;
945
946 rc = bit_spin_trylock(PG_locked, &page->flags);
947 return rc;
948}
949
950/*
951 * Management of partially allocated slabs
952 */
953static void add_partial(struct kmem_cache *s, struct page *page)
954{
955 struct kmem_cache_node *n = get_node(s, page_to_nid(page));
956
957 spin_lock(&n->list_lock);
958 n->nr_partial++;
959 list_add(&page->lru, &n->partial);
960 spin_unlock(&n->list_lock);
961}
962
963static void remove_partial(struct kmem_cache *s,
964 struct page *page)
965{
966 struct kmem_cache_node *n = get_node(s, page_to_nid(page));
967
968 spin_lock(&n->list_lock);
969 list_del(&page->lru);
970 n->nr_partial--;
971 spin_unlock(&n->list_lock);
972}
973
974/*
975 * Lock page and remove it from the partial list
976 *
977 * Must hold list_lock
978 */
979static int lock_and_del_slab(struct kmem_cache_node *n, struct page *page)
980{
981 if (slab_trylock(page)) {
982 list_del(&page->lru);
983 n->nr_partial--;
984 return 1;
985 }
986 return 0;
987}
988
989/*
990 * Try to get a partial slab from a specific node
991 */
992static struct page *get_partial_node(struct kmem_cache_node *n)
993{
994 struct page *page;
995
996 /*
997 * Racy check. If we mistakenly see no partial slabs then we
998 * just allocate an empty slab. If we mistakenly try to get a
999 * partial slab then get_partials() will return NULL.
1000 */
1001 if (!n || !n->nr_partial)
1002 return NULL;
1003
1004 spin_lock(&n->list_lock);
1005 list_for_each_entry(page, &n->partial, lru)
1006 if (lock_and_del_slab(n, page))
1007 goto out;
1008 page = NULL;
1009out:
1010 spin_unlock(&n->list_lock);
1011 return page;
1012}
1013
1014/*
1015 * Get a page from somewhere. Search in increasing NUMA
1016 * distances.
1017 */
1018static struct page *get_any_partial(struct kmem_cache *s, gfp_t flags)
1019{
1020#ifdef CONFIG_NUMA
1021 struct zonelist *zonelist;
1022 struct zone **z;
1023 struct page *page;
1024
1025 /*
1026 * The defrag ratio allows to configure the tradeoffs between
1027 * inter node defragmentation and node local allocations.
1028 * A lower defrag_ratio increases the tendency to do local
1029 * allocations instead of scanning throught the partial
1030 * lists on other nodes.
1031 *
1032 * If defrag_ratio is set to 0 then kmalloc() always
1033 * returns node local objects. If its higher then kmalloc()
1034 * may return off node objects in order to avoid fragmentation.
1035 *
1036 * A higher ratio means slabs may be taken from other nodes
1037 * thus reducing the number of partial slabs on those nodes.
1038 *
1039 * If /sys/slab/xx/defrag_ratio is set to 100 (which makes
1040 * defrag_ratio = 1000) then every (well almost) allocation
1041 * will first attempt to defrag slab caches on other nodes. This
1042 * means scanning over all nodes to look for partial slabs which
1043 * may be a bit expensive to do on every slab allocation.
1044 */
1045 if (!s->defrag_ratio || get_cycles() % 1024 > s->defrag_ratio)
1046 return NULL;
1047
1048 zonelist = &NODE_DATA(slab_node(current->mempolicy))
1049 ->node_zonelists[gfp_zone(flags)];
1050 for (z = zonelist->zones; *z; z++) {
1051 struct kmem_cache_node *n;
1052
1053 n = get_node(s, zone_to_nid(*z));
1054
1055 if (n && cpuset_zone_allowed_hardwall(*z, flags) &&
1056 n->nr_partial > 2) {
1057 page = get_partial_node(n);
1058 if (page)
1059 return page;
1060 }
1061 }
1062#endif
1063 return NULL;
1064}
1065
1066/*
1067 * Get a partial page, lock it and return it.
1068 */
1069static struct page *get_partial(struct kmem_cache *s, gfp_t flags, int node)
1070{
1071 struct page *page;
1072 int searchnode = (node == -1) ? numa_node_id() : node;
1073
1074 page = get_partial_node(get_node(s, searchnode));
1075 if (page || (flags & __GFP_THISNODE))
1076 return page;
1077
1078 return get_any_partial(s, flags);
1079}
1080
1081/*
1082 * Move a page back to the lists.
1083 *
1084 * Must be called with the slab lock held.
1085 *
1086 * On exit the slab lock will have been dropped.
1087 */
1088static void putback_slab(struct kmem_cache *s, struct page *page)
1089{
1090 if (page->inuse) {
1091 if (page->freelist)
1092 add_partial(s, page);
1093 slab_unlock(page);
1094 } else {
1095 slab_unlock(page);
1096 discard_slab(s, page);
1097 }
1098}
1099
1100/*
1101 * Remove the cpu slab
1102 */
1103static void deactivate_slab(struct kmem_cache *s, struct page *page, int cpu)
1104{
1105 s->cpu_slab[cpu] = NULL;
1106 ClearPageActive(page);
1107
1108 putback_slab(s, page);
1109}
1110
1111static void flush_slab(struct kmem_cache *s, struct page *page, int cpu)
1112{
1113 slab_lock(page);
1114 deactivate_slab(s, page, cpu);
1115}
1116
1117/*
1118 * Flush cpu slab.
1119 * Called from IPI handler with interrupts disabled.
1120 */
1121static void __flush_cpu_slab(struct kmem_cache *s, int cpu)
1122{
1123 struct page *page = s->cpu_slab[cpu];
1124
1125 if (likely(page))
1126 flush_slab(s, page, cpu);
1127}
1128
1129static void flush_cpu_slab(void *d)
1130{
1131 struct kmem_cache *s = d;
1132 int cpu = smp_processor_id();
1133
1134 __flush_cpu_slab(s, cpu);
1135}
1136
1137static void flush_all(struct kmem_cache *s)
1138{
1139#ifdef CONFIG_SMP
1140 on_each_cpu(flush_cpu_slab, s, 1, 1);
1141#else
1142 unsigned long flags;
1143
1144 local_irq_save(flags);
1145 flush_cpu_slab(s);
1146 local_irq_restore(flags);
1147#endif
1148}
1149
1150/*
1151 * slab_alloc is optimized to only modify two cachelines on the fast path
1152 * (aside from the stack):
1153 *
1154 * 1. The page struct
1155 * 2. The first cacheline of the object to be allocated.
1156 *
1157 * The only cache lines that are read (apart from code) is the
1158 * per cpu array in the kmem_cache struct.
1159 *
1160 * Fastpath is not possible if we need to get a new slab or have
1161 * debugging enabled (which means all slabs are marked with PageError)
1162 */
77c5e2d0
CL
1163static void *slab_alloc(struct kmem_cache *s,
1164 gfp_t gfpflags, int node, void *addr)
81819f0f
CL
1165{
1166 struct page *page;
1167 void **object;
1168 unsigned long flags;
1169 int cpu;
1170
1171 local_irq_save(flags);
1172 cpu = smp_processor_id();
1173 page = s->cpu_slab[cpu];
1174 if (!page)
1175 goto new_slab;
1176
1177 slab_lock(page);
1178 if (unlikely(node != -1 && page_to_nid(page) != node))
1179 goto another_slab;
1180redo:
1181 object = page->freelist;
1182 if (unlikely(!object))
1183 goto another_slab;
1184 if (unlikely(PageError(page)))
1185 goto debug;
1186
1187have_object:
1188 page->inuse++;
1189 page->freelist = object[page->offset];
1190 slab_unlock(page);
1191 local_irq_restore(flags);
1192 return object;
1193
1194another_slab:
1195 deactivate_slab(s, page, cpu);
1196
1197new_slab:
1198 page = get_partial(s, gfpflags, node);
1199 if (likely(page)) {
1200have_slab:
1201 s->cpu_slab[cpu] = page;
1202 SetPageActive(page);
1203 goto redo;
1204 }
1205
1206 page = new_slab(s, gfpflags, node);
1207 if (page) {
1208 cpu = smp_processor_id();
1209 if (s->cpu_slab[cpu]) {
1210 /*
1211 * Someone else populated the cpu_slab while we enabled
1212 * interrupts, or we have got scheduled on another cpu.
1213 * The page may not be on the requested node.
1214 */
1215 if (node == -1 ||
1216 page_to_nid(s->cpu_slab[cpu]) == node) {
1217 /*
1218 * Current cpuslab is acceptable and we
1219 * want the current one since its cache hot
1220 */
1221 discard_slab(s, page);
1222 page = s->cpu_slab[cpu];
1223 slab_lock(page);
1224 goto redo;
1225 }
1226 /* Dump the current slab */
1227 flush_slab(s, s->cpu_slab[cpu], cpu);
1228 }
1229 slab_lock(page);
1230 goto have_slab;
1231 }
1232 local_irq_restore(flags);
1233 return NULL;
1234debug:
1235 if (!alloc_object_checks(s, page, object))
1236 goto another_slab;
1237 if (s->flags & SLAB_STORE_USER)
77c5e2d0 1238 set_track(s, object, TRACK_ALLOC, addr);
81819f0f
CL
1239 goto have_object;
1240}
1241
1242void *kmem_cache_alloc(struct kmem_cache *s, gfp_t gfpflags)
1243{
77c5e2d0 1244 return slab_alloc(s, gfpflags, -1, __builtin_return_address(0));
81819f0f
CL
1245}
1246EXPORT_SYMBOL(kmem_cache_alloc);
1247
1248#ifdef CONFIG_NUMA
1249void *kmem_cache_alloc_node(struct kmem_cache *s, gfp_t gfpflags, int node)
1250{
77c5e2d0 1251 return slab_alloc(s, gfpflags, node, __builtin_return_address(0));
81819f0f
CL
1252}
1253EXPORT_SYMBOL(kmem_cache_alloc_node);
1254#endif
1255
1256/*
1257 * The fastpath only writes the cacheline of the page struct and the first
1258 * cacheline of the object.
1259 *
1260 * No special cachelines need to be read
1261 */
77c5e2d0
CL
1262static void slab_free(struct kmem_cache *s, struct page *page,
1263 void *x, void *addr)
81819f0f
CL
1264{
1265 void *prior;
1266 void **object = (void *)x;
1267 unsigned long flags;
1268
1269 local_irq_save(flags);
1270 slab_lock(page);
1271
1272 if (unlikely(PageError(page)))
1273 goto debug;
1274checks_ok:
1275 prior = object[page->offset] = page->freelist;
1276 page->freelist = object;
1277 page->inuse--;
1278
1279 if (unlikely(PageActive(page)))
1280 /*
1281 * Cpu slabs are never on partial lists and are
1282 * never freed.
1283 */
1284 goto out_unlock;
1285
1286 if (unlikely(!page->inuse))
1287 goto slab_empty;
1288
1289 /*
1290 * Objects left in the slab. If it
1291 * was not on the partial list before
1292 * then add it.
1293 */
1294 if (unlikely(!prior))
1295 add_partial(s, page);
1296
1297out_unlock:
1298 slab_unlock(page);
1299 local_irq_restore(flags);
1300 return;
1301
1302slab_empty:
1303 if (prior)
1304 /*
1305 * Partially used slab that is on the partial list.
1306 */
1307 remove_partial(s, page);
1308
1309 slab_unlock(page);
1310 discard_slab(s, page);
1311 local_irq_restore(flags);
1312 return;
1313
1314debug:
77c5e2d0
CL
1315 if (!free_object_checks(s, page, x))
1316 goto out_unlock;
1317 if (s->flags & SLAB_STORE_USER)
1318 set_track(s, x, TRACK_FREE, addr);
1319 goto checks_ok;
81819f0f
CL
1320}
1321
1322void kmem_cache_free(struct kmem_cache *s, void *x)
1323{
77c5e2d0 1324 struct page *page;
81819f0f 1325
b49af68f 1326 page = virt_to_head_page(x);
81819f0f 1327
77c5e2d0 1328 slab_free(s, page, x, __builtin_return_address(0));
81819f0f
CL
1329}
1330EXPORT_SYMBOL(kmem_cache_free);
1331
1332/* Figure out on which slab object the object resides */
1333static struct page *get_object_page(const void *x)
1334{
b49af68f 1335 struct page *page = virt_to_head_page(x);
81819f0f
CL
1336
1337 if (!PageSlab(page))
1338 return NULL;
1339
1340 return page;
1341}
1342
1343/*
1344 * kmem_cache_open produces objects aligned at "size" and the first object
1345 * is placed at offset 0 in the slab (We have no metainformation on the
1346 * slab, all slabs are in essence "off slab").
1347 *
1348 * In order to get the desired alignment one just needs to align the
1349 * size.
1350 *
1351 * Notice that the allocation order determines the sizes of the per cpu
1352 * caches. Each processor has always one slab available for allocations.
1353 * Increasing the allocation order reduces the number of times that slabs
1354 * must be moved on and off the partial lists and therefore may influence
1355 * locking overhead.
1356 *
1357 * The offset is used to relocate the free list link in each object. It is
1358 * therefore possible to move the free list link behind the object. This
1359 * is necessary for RCU to work properly and also useful for debugging.
1360 */
1361
1362/*
1363 * Mininum / Maximum order of slab pages. This influences locking overhead
1364 * and slab fragmentation. A higher order reduces the number of partial slabs
1365 * and increases the number of allocations possible without having to
1366 * take the list_lock.
1367 */
1368static int slub_min_order;
1369static int slub_max_order = DEFAULT_MAX_ORDER;
1370
1371/*
1372 * Minimum number of objects per slab. This is necessary in order to
1373 * reduce locking overhead. Similar to the queue size in SLAB.
1374 */
1375static int slub_min_objects = DEFAULT_MIN_OBJECTS;
1376
1377/*
1378 * Merge control. If this is set then no merging of slab caches will occur.
1379 */
1380static int slub_nomerge;
1381
1382/*
1383 * Debug settings:
1384 */
1385static int slub_debug;
1386
1387static char *slub_debug_slabs;
1388
1389/*
1390 * Calculate the order of allocation given an slab object size.
1391 *
1392 * The order of allocation has significant impact on other elements
1393 * of the system. Generally order 0 allocations should be preferred
1394 * since they do not cause fragmentation in the page allocator. Larger
1395 * objects may have problems with order 0 because there may be too much
1396 * space left unused in a slab. We go to a higher order if more than 1/8th
1397 * of the slab would be wasted.
1398 *
1399 * In order to reach satisfactory performance we must ensure that
1400 * a minimum number of objects is in one slab. Otherwise we may
1401 * generate too much activity on the partial lists. This is less a
1402 * concern for large slabs though. slub_max_order specifies the order
1403 * where we begin to stop considering the number of objects in a slab.
1404 *
1405 * Higher order allocations also allow the placement of more objects
1406 * in a slab and thereby reduce object handling overhead. If the user
1407 * has requested a higher mininum order then we start with that one
1408 * instead of zero.
1409 */
1410static int calculate_order(int size)
1411{
1412 int order;
1413 int rem;
1414
1415 for (order = max(slub_min_order, fls(size - 1) - PAGE_SHIFT);
1416 order < MAX_ORDER; order++) {
1417 unsigned long slab_size = PAGE_SIZE << order;
1418
1419 if (slub_max_order > order &&
1420 slab_size < slub_min_objects * size)
1421 continue;
1422
1423 if (slab_size < size)
1424 continue;
1425
1426 rem = slab_size % size;
1427
1428 if (rem <= (PAGE_SIZE << order) / 8)
1429 break;
1430
1431 }
1432 if (order >= MAX_ORDER)
1433 return -E2BIG;
1434 return order;
1435}
1436
1437/*
1438 * Function to figure out which alignment to use from the
1439 * various ways of specifying it.
1440 */
1441static unsigned long calculate_alignment(unsigned long flags,
1442 unsigned long align, unsigned long size)
1443{
1444 /*
1445 * If the user wants hardware cache aligned objects then
1446 * follow that suggestion if the object is sufficiently
1447 * large.
1448 *
1449 * The hardware cache alignment cannot override the
1450 * specified alignment though. If that is greater
1451 * then use it.
1452 */
1453 if ((flags & (SLAB_MUST_HWCACHE_ALIGN | SLAB_HWCACHE_ALIGN)) &&
1454 size > L1_CACHE_BYTES / 2)
1455 return max_t(unsigned long, align, L1_CACHE_BYTES);
1456
1457 if (align < ARCH_SLAB_MINALIGN)
1458 return ARCH_SLAB_MINALIGN;
1459
1460 return ALIGN(align, sizeof(void *));
1461}
1462
1463static void init_kmem_cache_node(struct kmem_cache_node *n)
1464{
1465 n->nr_partial = 0;
1466 atomic_long_set(&n->nr_slabs, 0);
1467 spin_lock_init(&n->list_lock);
1468 INIT_LIST_HEAD(&n->partial);
1469}
1470
1471#ifdef CONFIG_NUMA
1472/*
1473 * No kmalloc_node yet so do it by hand. We know that this is the first
1474 * slab on the node for this slabcache. There are no concurrent accesses
1475 * possible.
1476 *
1477 * Note that this function only works on the kmalloc_node_cache
1478 * when allocating for the kmalloc_node_cache.
1479 */
1480static struct kmem_cache_node * __init early_kmem_cache_node_alloc(gfp_t gfpflags,
1481 int node)
1482{
1483 struct page *page;
1484 struct kmem_cache_node *n;
1485
1486 BUG_ON(kmalloc_caches->size < sizeof(struct kmem_cache_node));
1487
1488 page = new_slab(kmalloc_caches, gfpflags | GFP_THISNODE, node);
1489 /* new_slab() disables interupts */
1490 local_irq_enable();
1491
1492 BUG_ON(!page);
1493 n = page->freelist;
1494 BUG_ON(!n);
1495 page->freelist = get_freepointer(kmalloc_caches, n);
1496 page->inuse++;
1497 kmalloc_caches->node[node] = n;
1498 init_object(kmalloc_caches, n, 1);
1499 init_kmem_cache_node(n);
1500 atomic_long_inc(&n->nr_slabs);
1501 add_partial(kmalloc_caches, page);
1502 return n;
1503}
1504
1505static void free_kmem_cache_nodes(struct kmem_cache *s)
1506{
1507 int node;
1508
1509 for_each_online_node(node) {
1510 struct kmem_cache_node *n = s->node[node];
1511 if (n && n != &s->local_node)
1512 kmem_cache_free(kmalloc_caches, n);
1513 s->node[node] = NULL;
1514 }
1515}
1516
1517static int init_kmem_cache_nodes(struct kmem_cache *s, gfp_t gfpflags)
1518{
1519 int node;
1520 int local_node;
1521
1522 if (slab_state >= UP)
1523 local_node = page_to_nid(virt_to_page(s));
1524 else
1525 local_node = 0;
1526
1527 for_each_online_node(node) {
1528 struct kmem_cache_node *n;
1529
1530 if (local_node == node)
1531 n = &s->local_node;
1532 else {
1533 if (slab_state == DOWN) {
1534 n = early_kmem_cache_node_alloc(gfpflags,
1535 node);
1536 continue;
1537 }
1538 n = kmem_cache_alloc_node(kmalloc_caches,
1539 gfpflags, node);
1540
1541 if (!n) {
1542 free_kmem_cache_nodes(s);
1543 return 0;
1544 }
1545
1546 }
1547 s->node[node] = n;
1548 init_kmem_cache_node(n);
1549 }
1550 return 1;
1551}
1552#else
1553static void free_kmem_cache_nodes(struct kmem_cache *s)
1554{
1555}
1556
1557static int init_kmem_cache_nodes(struct kmem_cache *s, gfp_t gfpflags)
1558{
1559 init_kmem_cache_node(&s->local_node);
1560 return 1;
1561}
1562#endif
1563
1564/*
1565 * calculate_sizes() determines the order and the distribution of data within
1566 * a slab object.
1567 */
1568static int calculate_sizes(struct kmem_cache *s)
1569{
1570 unsigned long flags = s->flags;
1571 unsigned long size = s->objsize;
1572 unsigned long align = s->align;
1573
1574 /*
1575 * Determine if we can poison the object itself. If the user of
1576 * the slab may touch the object after free or before allocation
1577 * then we should never poison the object itself.
1578 */
1579 if ((flags & SLAB_POISON) && !(flags & SLAB_DESTROY_BY_RCU) &&
1580 !s->ctor && !s->dtor)
1581 s->flags |= __OBJECT_POISON;
1582 else
1583 s->flags &= ~__OBJECT_POISON;
1584
1585 /*
1586 * Round up object size to the next word boundary. We can only
1587 * place the free pointer at word boundaries and this determines
1588 * the possible location of the free pointer.
1589 */
1590 size = ALIGN(size, sizeof(void *));
1591
1592 /*
1593 * If we are redzoning then check if there is some space between the
1594 * end of the object and the free pointer. If not then add an
1595 * additional word, so that we can establish a redzone between
1596 * the object and the freepointer to be able to check for overwrites.
1597 */
1598 if ((flags & SLAB_RED_ZONE) && size == s->objsize)
1599 size += sizeof(void *);
1600
1601 /*
1602 * With that we have determined how much of the slab is in actual
1603 * use by the object. This is the potential offset to the free
1604 * pointer.
1605 */
1606 s->inuse = size;
1607
1608 if (((flags & (SLAB_DESTROY_BY_RCU | SLAB_POISON)) ||
1609 s->ctor || s->dtor)) {
1610 /*
1611 * Relocate free pointer after the object if it is not
1612 * permitted to overwrite the first word of the object on
1613 * kmem_cache_free.
1614 *
1615 * This is the case if we do RCU, have a constructor or
1616 * destructor or are poisoning the objects.
1617 */
1618 s->offset = size;
1619 size += sizeof(void *);
1620 }
1621
1622 if (flags & SLAB_STORE_USER)
1623 /*
1624 * Need to store information about allocs and frees after
1625 * the object.
1626 */
1627 size += 2 * sizeof(struct track);
1628
1629 if (flags & DEBUG_DEFAULT_FLAGS)
1630 /*
1631 * Add some empty padding so that we can catch
1632 * overwrites from earlier objects rather than let
1633 * tracking information or the free pointer be
1634 * corrupted if an user writes before the start
1635 * of the object.
1636 */
1637 size += sizeof(void *);
1638 /*
1639 * Determine the alignment based on various parameters that the
1640 * user specified (this is unecessarily complex due to the attempt
1641 * to be compatible with SLAB. Should be cleaned up some day).
1642 */
1643 align = calculate_alignment(flags, align, s->objsize);
1644
1645 /*
1646 * SLUB stores one object immediately after another beginning from
1647 * offset 0. In order to align the objects we have to simply size
1648 * each object to conform to the alignment.
1649 */
1650 size = ALIGN(size, align);
1651 s->size = size;
1652
1653 s->order = calculate_order(size);
1654 if (s->order < 0)
1655 return 0;
1656
1657 /*
1658 * Determine the number of objects per slab
1659 */
1660 s->objects = (PAGE_SIZE << s->order) / size;
1661
1662 /*
1663 * Verify that the number of objects is within permitted limits.
1664 * The page->inuse field is only 16 bit wide! So we cannot have
1665 * more than 64k objects per slab.
1666 */
1667 if (!s->objects || s->objects > 65535)
1668 return 0;
1669 return 1;
1670
1671}
1672
1673static int __init finish_bootstrap(void)
1674{
1675 struct list_head *h;
1676 int err;
1677
1678 slab_state = SYSFS;
1679
1680 list_for_each(h, &slab_caches) {
1681 struct kmem_cache *s =
1682 container_of(h, struct kmem_cache, list);
1683
1684 err = sysfs_slab_add(s);
1685 BUG_ON(err);
1686 }
1687 return 0;
1688}
1689
1690static int kmem_cache_open(struct kmem_cache *s, gfp_t gfpflags,
1691 const char *name, size_t size,
1692 size_t align, unsigned long flags,
1693 void (*ctor)(void *, struct kmem_cache *, unsigned long),
1694 void (*dtor)(void *, struct kmem_cache *, unsigned long))
1695{
1696 memset(s, 0, kmem_size);
1697 s->name = name;
1698 s->ctor = ctor;
1699 s->dtor = dtor;
1700 s->objsize = size;
1701 s->flags = flags;
1702 s->align = align;
1703
1704 BUG_ON(flags & SLUB_UNIMPLEMENTED);
1705
1706 /*
1707 * The page->offset field is only 16 bit wide. This is an offset
1708 * in units of words from the beginning of an object. If the slab
1709 * size is bigger then we cannot move the free pointer behind the
1710 * object anymore.
1711 *
1712 * On 32 bit platforms the limit is 256k. On 64bit platforms
1713 * the limit is 512k.
1714 *
1715 * Debugging or ctor/dtors may create a need to move the free
1716 * pointer. Fail if this happens.
1717 */
1718 if (s->size >= 65535 * sizeof(void *)) {
1719 BUG_ON(flags & (SLAB_RED_ZONE | SLAB_POISON |
1720 SLAB_STORE_USER | SLAB_DESTROY_BY_RCU));
1721 BUG_ON(ctor || dtor);
1722 }
1723 else
1724 /*
1725 * Enable debugging if selected on the kernel commandline.
1726 */
1727 if (slub_debug && (!slub_debug_slabs ||
1728 strncmp(slub_debug_slabs, name,
1729 strlen(slub_debug_slabs)) == 0))
1730 s->flags |= slub_debug;
1731
1732 if (!calculate_sizes(s))
1733 goto error;
1734
1735 s->refcount = 1;
1736#ifdef CONFIG_NUMA
1737 s->defrag_ratio = 100;
1738#endif
1739
1740 if (init_kmem_cache_nodes(s, gfpflags & ~SLUB_DMA))
1741 return 1;
1742error:
1743 if (flags & SLAB_PANIC)
1744 panic("Cannot create slab %s size=%lu realsize=%u "
1745 "order=%u offset=%u flags=%lx\n",
1746 s->name, (unsigned long)size, s->size, s->order,
1747 s->offset, flags);
1748 return 0;
1749}
1750EXPORT_SYMBOL(kmem_cache_open);
1751
1752/*
1753 * Check if a given pointer is valid
1754 */
1755int kmem_ptr_validate(struct kmem_cache *s, const void *object)
1756{
1757 struct page * page;
1758 void *addr;
1759
1760 page = get_object_page(object);
1761
1762 if (!page || s != page->slab)
1763 /* No slab or wrong slab */
1764 return 0;
1765
1766 addr = page_address(page);
1767 if (object < addr || object >= addr + s->objects * s->size)
1768 /* Out of bounds */
1769 return 0;
1770
1771 if ((object - addr) % s->size)
1772 /* Improperly aligned */
1773 return 0;
1774
1775 /*
1776 * We could also check if the object is on the slabs freelist.
1777 * But this would be too expensive and it seems that the main
1778 * purpose of kmem_ptr_valid is to check if the object belongs
1779 * to a certain slab.
1780 */
1781 return 1;
1782}
1783EXPORT_SYMBOL(kmem_ptr_validate);
1784
1785/*
1786 * Determine the size of a slab object
1787 */
1788unsigned int kmem_cache_size(struct kmem_cache *s)
1789{
1790 return s->objsize;
1791}
1792EXPORT_SYMBOL(kmem_cache_size);
1793
1794const char *kmem_cache_name(struct kmem_cache *s)
1795{
1796 return s->name;
1797}
1798EXPORT_SYMBOL(kmem_cache_name);
1799
1800/*
1801 * Attempt to free all slabs on a node
1802 */
1803static int free_list(struct kmem_cache *s, struct kmem_cache_node *n,
1804 struct list_head *list)
1805{
1806 int slabs_inuse = 0;
1807 unsigned long flags;
1808 struct page *page, *h;
1809
1810 spin_lock_irqsave(&n->list_lock, flags);
1811 list_for_each_entry_safe(page, h, list, lru)
1812 if (!page->inuse) {
1813 list_del(&page->lru);
1814 discard_slab(s, page);
1815 } else
1816 slabs_inuse++;
1817 spin_unlock_irqrestore(&n->list_lock, flags);
1818 return slabs_inuse;
1819}
1820
1821/*
1822 * Release all resources used by slab cache
1823 */
1824static int kmem_cache_close(struct kmem_cache *s)
1825{
1826 int node;
1827
1828 flush_all(s);
1829
1830 /* Attempt to free all objects */
1831 for_each_online_node(node) {
1832 struct kmem_cache_node *n = get_node(s, node);
1833
1834 free_list(s, n, &n->partial);
1835 if (atomic_long_read(&n->nr_slabs))
1836 return 1;
1837 }
1838 free_kmem_cache_nodes(s);
1839 return 0;
1840}
1841
1842/*
1843 * Close a cache and release the kmem_cache structure
1844 * (must be used for caches created using kmem_cache_create)
1845 */
1846void kmem_cache_destroy(struct kmem_cache *s)
1847{
1848 down_write(&slub_lock);
1849 s->refcount--;
1850 if (!s->refcount) {
1851 list_del(&s->list);
1852 if (kmem_cache_close(s))
1853 WARN_ON(1);
1854 sysfs_slab_remove(s);
1855 kfree(s);
1856 }
1857 up_write(&slub_lock);
1858}
1859EXPORT_SYMBOL(kmem_cache_destroy);
1860
1861/********************************************************************
1862 * Kmalloc subsystem
1863 *******************************************************************/
1864
1865struct kmem_cache kmalloc_caches[KMALLOC_SHIFT_HIGH + 1] __cacheline_aligned;
1866EXPORT_SYMBOL(kmalloc_caches);
1867
1868#ifdef CONFIG_ZONE_DMA
1869static struct kmem_cache *kmalloc_caches_dma[KMALLOC_SHIFT_HIGH + 1];
1870#endif
1871
1872static int __init setup_slub_min_order(char *str)
1873{
1874 get_option (&str, &slub_min_order);
1875
1876 return 1;
1877}
1878
1879__setup("slub_min_order=", setup_slub_min_order);
1880
1881static int __init setup_slub_max_order(char *str)
1882{
1883 get_option (&str, &slub_max_order);
1884
1885 return 1;
1886}
1887
1888__setup("slub_max_order=", setup_slub_max_order);
1889
1890static int __init setup_slub_min_objects(char *str)
1891{
1892 get_option (&str, &slub_min_objects);
1893
1894 return 1;
1895}
1896
1897__setup("slub_min_objects=", setup_slub_min_objects);
1898
1899static int __init setup_slub_nomerge(char *str)
1900{
1901 slub_nomerge = 1;
1902 return 1;
1903}
1904
1905__setup("slub_nomerge", setup_slub_nomerge);
1906
1907static int __init setup_slub_debug(char *str)
1908{
1909 if (!str || *str != '=')
1910 slub_debug = DEBUG_DEFAULT_FLAGS;
1911 else {
1912 str++;
1913 if (*str == 0 || *str == ',')
1914 slub_debug = DEBUG_DEFAULT_FLAGS;
1915 else
1916 for( ;*str && *str != ','; str++)
1917 switch (*str) {
1918 case 'f' : case 'F' :
1919 slub_debug |= SLAB_DEBUG_FREE;
1920 break;
1921 case 'z' : case 'Z' :
1922 slub_debug |= SLAB_RED_ZONE;
1923 break;
1924 case 'p' : case 'P' :
1925 slub_debug |= SLAB_POISON;
1926 break;
1927 case 'u' : case 'U' :
1928 slub_debug |= SLAB_STORE_USER;
1929 break;
1930 case 't' : case 'T' :
1931 slub_debug |= SLAB_TRACE;
1932 break;
1933 default:
1934 printk(KERN_ERR "slub_debug option '%c' "
1935 "unknown. skipped\n",*str);
1936 }
1937 }
1938
1939 if (*str == ',')
1940 slub_debug_slabs = str + 1;
1941 return 1;
1942}
1943
1944__setup("slub_debug", setup_slub_debug);
1945
1946static struct kmem_cache *create_kmalloc_cache(struct kmem_cache *s,
1947 const char *name, int size, gfp_t gfp_flags)
1948{
1949 unsigned int flags = 0;
1950
1951 if (gfp_flags & SLUB_DMA)
1952 flags = SLAB_CACHE_DMA;
1953
1954 down_write(&slub_lock);
1955 if (!kmem_cache_open(s, gfp_flags, name, size, ARCH_KMALLOC_MINALIGN,
1956 flags, NULL, NULL))
1957 goto panic;
1958
1959 list_add(&s->list, &slab_caches);
1960 up_write(&slub_lock);
1961 if (sysfs_slab_add(s))
1962 goto panic;
1963 return s;
1964
1965panic:
1966 panic("Creation of kmalloc slab %s size=%d failed.\n", name, size);
1967}
1968
1969static struct kmem_cache *get_slab(size_t size, gfp_t flags)
1970{
1971 int index = kmalloc_index(size);
1972
614410d5 1973 if (!index)
81819f0f
CL
1974 return NULL;
1975
1976 /* Allocation too large? */
1977 BUG_ON(index < 0);
1978
1979#ifdef CONFIG_ZONE_DMA
1980 if ((flags & SLUB_DMA)) {
1981 struct kmem_cache *s;
1982 struct kmem_cache *x;
1983 char *text;
1984 size_t realsize;
1985
1986 s = kmalloc_caches_dma[index];
1987 if (s)
1988 return s;
1989
1990 /* Dynamically create dma cache */
1991 x = kmalloc(kmem_size, flags & ~SLUB_DMA);
1992 if (!x)
1993 panic("Unable to allocate memory for dma cache\n");
1994
1995 if (index <= KMALLOC_SHIFT_HIGH)
1996 realsize = 1 << index;
1997 else {
1998 if (index == 1)
1999 realsize = 96;
2000 else
2001 realsize = 192;
2002 }
2003
2004 text = kasprintf(flags & ~SLUB_DMA, "kmalloc_dma-%d",
2005 (unsigned int)realsize);
2006 s = create_kmalloc_cache(x, text, realsize, flags);
2007 kmalloc_caches_dma[index] = s;
2008 return s;
2009 }
2010#endif
2011 return &kmalloc_caches[index];
2012}
2013
2014void *__kmalloc(size_t size, gfp_t flags)
2015{
2016 struct kmem_cache *s = get_slab(size, flags);
2017
2018 if (s)
77c5e2d0 2019 return slab_alloc(s, flags, -1, __builtin_return_address(0));
81819f0f
CL
2020 return NULL;
2021}
2022EXPORT_SYMBOL(__kmalloc);
2023
2024#ifdef CONFIG_NUMA
2025void *__kmalloc_node(size_t size, gfp_t flags, int node)
2026{
2027 struct kmem_cache *s = get_slab(size, flags);
2028
2029 if (s)
77c5e2d0 2030 return slab_alloc(s, flags, node, __builtin_return_address(0));
81819f0f
CL
2031 return NULL;
2032}
2033EXPORT_SYMBOL(__kmalloc_node);
2034#endif
2035
2036size_t ksize(const void *object)
2037{
2038 struct page *page = get_object_page(object);
2039 struct kmem_cache *s;
2040
2041 BUG_ON(!page);
2042 s = page->slab;
2043 BUG_ON(!s);
2044
2045 /*
2046 * Debugging requires use of the padding between object
2047 * and whatever may come after it.
2048 */
2049 if (s->flags & (SLAB_RED_ZONE | SLAB_POISON))
2050 return s->objsize;
2051
2052 /*
2053 * If we have the need to store the freelist pointer
2054 * back there or track user information then we can
2055 * only use the space before that information.
2056 */
2057 if (s->flags & (SLAB_DESTROY_BY_RCU | SLAB_STORE_USER))
2058 return s->inuse;
2059
2060 /*
2061 * Else we can use all the padding etc for the allocation
2062 */
2063 return s->size;
2064}
2065EXPORT_SYMBOL(ksize);
2066
2067void kfree(const void *x)
2068{
2069 struct kmem_cache *s;
2070 struct page *page;
2071
2072 if (!x)
2073 return;
2074
b49af68f 2075 page = virt_to_head_page(x);
81819f0f
CL
2076 s = page->slab;
2077
77c5e2d0 2078 slab_free(s, page, (void *)x, __builtin_return_address(0));
81819f0f
CL
2079}
2080EXPORT_SYMBOL(kfree);
2081
2082/**
2083 * krealloc - reallocate memory. The contents will remain unchanged.
2084 *
2085 * @p: object to reallocate memory for.
2086 * @new_size: how many bytes of memory are required.
2087 * @flags: the type of memory to allocate.
2088 *
2089 * The contents of the object pointed to are preserved up to the
2090 * lesser of the new and old sizes. If @p is %NULL, krealloc()
2091 * behaves exactly like kmalloc(). If @size is 0 and @p is not a
2092 * %NULL pointer, the object pointed to is freed.
2093 */
2094void *krealloc(const void *p, size_t new_size, gfp_t flags)
2095{
2096 struct kmem_cache *new_cache;
2097 void *ret;
2098 struct page *page;
2099
2100 if (unlikely(!p))
2101 return kmalloc(new_size, flags);
2102
2103 if (unlikely(!new_size)) {
2104 kfree(p);
2105 return NULL;
2106 }
2107
b49af68f 2108 page = virt_to_head_page(p);
81819f0f
CL
2109
2110 new_cache = get_slab(new_size, flags);
2111
2112 /*
2113 * If new size fits in the current cache, bail out.
2114 */
2115 if (likely(page->slab == new_cache))
2116 return (void *)p;
2117
2118 ret = kmalloc(new_size, flags);
2119 if (ret) {
2120 memcpy(ret, p, min(new_size, ksize(p)));
2121 kfree(p);
2122 }
2123 return ret;
2124}
2125EXPORT_SYMBOL(krealloc);
2126
2127/********************************************************************
2128 * Basic setup of slabs
2129 *******************************************************************/
2130
2131void __init kmem_cache_init(void)
2132{
2133 int i;
2134
2135#ifdef CONFIG_NUMA
2136 /*
2137 * Must first have the slab cache available for the allocations of the
2138 * struct kmalloc_cache_node's. There is special bootstrap code in
2139 * kmem_cache_open for slab_state == DOWN.
2140 */
2141 create_kmalloc_cache(&kmalloc_caches[0], "kmem_cache_node",
2142 sizeof(struct kmem_cache_node), GFP_KERNEL);
2143#endif
2144
2145 /* Able to allocate the per node structures */
2146 slab_state = PARTIAL;
2147
2148 /* Caches that are not of the two-to-the-power-of size */
2149 create_kmalloc_cache(&kmalloc_caches[1],
2150 "kmalloc-96", 96, GFP_KERNEL);
2151 create_kmalloc_cache(&kmalloc_caches[2],
2152 "kmalloc-192", 192, GFP_KERNEL);
2153
2154 for (i = KMALLOC_SHIFT_LOW; i <= KMALLOC_SHIFT_HIGH; i++)
2155 create_kmalloc_cache(&kmalloc_caches[i],
2156 "kmalloc", 1 << i, GFP_KERNEL);
2157
2158 slab_state = UP;
2159
2160 /* Provide the correct kmalloc names now that the caches are up */
2161 for (i = KMALLOC_SHIFT_LOW; i <= KMALLOC_SHIFT_HIGH; i++)
2162 kmalloc_caches[i]. name =
2163 kasprintf(GFP_KERNEL, "kmalloc-%d", 1 << i);
2164
2165#ifdef CONFIG_SMP
2166 register_cpu_notifier(&slab_notifier);
2167#endif
2168
2169 if (nr_cpu_ids) /* Remove when nr_cpu_ids is fixed upstream ! */
2170 kmem_size = offsetof(struct kmem_cache, cpu_slab)
2171 + nr_cpu_ids * sizeof(struct page *);
2172
2173 printk(KERN_INFO "SLUB: Genslabs=%d, HWalign=%d, Order=%d-%d, MinObjects=%d,"
2174 " Processors=%d, Nodes=%d\n",
2175 KMALLOC_SHIFT_HIGH, L1_CACHE_BYTES,
2176 slub_min_order, slub_max_order, slub_min_objects,
2177 nr_cpu_ids, nr_node_ids);
2178}
2179
2180/*
2181 * Find a mergeable slab cache
2182 */
2183static int slab_unmergeable(struct kmem_cache *s)
2184{
2185 if (slub_nomerge || (s->flags & SLUB_NEVER_MERGE))
2186 return 1;
2187
2188 if (s->ctor || s->dtor)
2189 return 1;
2190
2191 return 0;
2192}
2193
2194static struct kmem_cache *find_mergeable(size_t size,
2195 size_t align, unsigned long flags,
2196 void (*ctor)(void *, struct kmem_cache *, unsigned long),
2197 void (*dtor)(void *, struct kmem_cache *, unsigned long))
2198{
2199 struct list_head *h;
2200
2201 if (slub_nomerge || (flags & SLUB_NEVER_MERGE))
2202 return NULL;
2203
2204 if (ctor || dtor)
2205 return NULL;
2206
2207 size = ALIGN(size, sizeof(void *));
2208 align = calculate_alignment(flags, align, size);
2209 size = ALIGN(size, align);
2210
2211 list_for_each(h, &slab_caches) {
2212 struct kmem_cache *s =
2213 container_of(h, struct kmem_cache, list);
2214
2215 if (slab_unmergeable(s))
2216 continue;
2217
2218 if (size > s->size)
2219 continue;
2220
2221 if (((flags | slub_debug) & SLUB_MERGE_SAME) !=
2222 (s->flags & SLUB_MERGE_SAME))
2223 continue;
2224 /*
2225 * Check if alignment is compatible.
2226 * Courtesy of Adrian Drzewiecki
2227 */
2228 if ((s->size & ~(align -1)) != s->size)
2229 continue;
2230
2231 if (s->size - size >= sizeof(void *))
2232 continue;
2233
2234 return s;
2235 }
2236 return NULL;
2237}
2238
2239struct kmem_cache *kmem_cache_create(const char *name, size_t size,
2240 size_t align, unsigned long flags,
2241 void (*ctor)(void *, struct kmem_cache *, unsigned long),
2242 void (*dtor)(void *, struct kmem_cache *, unsigned long))
2243{
2244 struct kmem_cache *s;
2245
2246 down_write(&slub_lock);
2247 s = find_mergeable(size, align, flags, dtor, ctor);
2248 if (s) {
2249 s->refcount++;
2250 /*
2251 * Adjust the object sizes so that we clear
2252 * the complete object on kzalloc.
2253 */
2254 s->objsize = max(s->objsize, (int)size);
2255 s->inuse = max_t(int, s->inuse, ALIGN(size, sizeof(void *)));
2256 if (sysfs_slab_alias(s, name))
2257 goto err;
2258 } else {
2259 s = kmalloc(kmem_size, GFP_KERNEL);
2260 if (s && kmem_cache_open(s, GFP_KERNEL, name,
2261 size, align, flags, ctor, dtor)) {
2262 if (sysfs_slab_add(s)) {
2263 kfree(s);
2264 goto err;
2265 }
2266 list_add(&s->list, &slab_caches);
2267 } else
2268 kfree(s);
2269 }
2270 up_write(&slub_lock);
2271 return s;
2272
2273err:
2274 up_write(&slub_lock);
2275 if (flags & SLAB_PANIC)
2276 panic("Cannot create slabcache %s\n", name);
2277 else
2278 s = NULL;
2279 return s;
2280}
2281EXPORT_SYMBOL(kmem_cache_create);
2282
2283void *kmem_cache_zalloc(struct kmem_cache *s, gfp_t flags)
2284{
2285 void *x;
2286
77c5e2d0 2287 x = slab_alloc(s, flags, -1, __builtin_return_address(0));
81819f0f
CL
2288 if (x)
2289 memset(x, 0, s->objsize);
2290 return x;
2291}
2292EXPORT_SYMBOL(kmem_cache_zalloc);
2293
2294#ifdef CONFIG_SMP
2295static void for_all_slabs(void (*func)(struct kmem_cache *, int), int cpu)
2296{
2297 struct list_head *h;
2298
2299 down_read(&slub_lock);
2300 list_for_each(h, &slab_caches) {
2301 struct kmem_cache *s =
2302 container_of(h, struct kmem_cache, list);
2303
2304 func(s, cpu);
2305 }
2306 up_read(&slub_lock);
2307}
2308
2309/*
2310 * Use the cpu notifier to insure that the slab are flushed
2311 * when necessary.
2312 */
2313static int __cpuinit slab_cpuup_callback(struct notifier_block *nfb,
2314 unsigned long action, void *hcpu)
2315{
2316 long cpu = (long)hcpu;
2317
2318 switch (action) {
2319 case CPU_UP_CANCELED:
2320 case CPU_DEAD:
2321 for_all_slabs(__flush_cpu_slab, cpu);
2322 break;
2323 default:
2324 break;
2325 }
2326 return NOTIFY_OK;
2327}
2328
2329static struct notifier_block __cpuinitdata slab_notifier =
2330 { &slab_cpuup_callback, NULL, 0 };
2331
2332#endif
2333
2334/***************************************************************
2335 * Compatiblility definitions
2336 **************************************************************/
2337
2338int kmem_cache_shrink(struct kmem_cache *s)
2339{
2340 flush_all(s);
2341 return 0;
2342}
2343EXPORT_SYMBOL(kmem_cache_shrink);
2344
2345#ifdef CONFIG_NUMA
2346
2347/*****************************************************************
2348 * Generic reaper used to support the page allocator
2349 * (the cpu slabs are reaped by a per slab workqueue).
2350 *
2351 * Maybe move this to the page allocator?
2352 ****************************************************************/
2353
2354static DEFINE_PER_CPU(unsigned long, reap_node);
2355
2356static void init_reap_node(int cpu)
2357{
2358 int node;
2359
2360 node = next_node(cpu_to_node(cpu), node_online_map);
2361 if (node == MAX_NUMNODES)
2362 node = first_node(node_online_map);
2363
2364 __get_cpu_var(reap_node) = node;
2365}
2366
2367static void next_reap_node(void)
2368{
2369 int node = __get_cpu_var(reap_node);
2370
2371 /*
2372 * Also drain per cpu pages on remote zones
2373 */
2374 if (node != numa_node_id())
2375 drain_node_pages(node);
2376
2377 node = next_node(node, node_online_map);
2378 if (unlikely(node >= MAX_NUMNODES))
2379 node = first_node(node_online_map);
2380 __get_cpu_var(reap_node) = node;
2381}
2382#else
2383#define init_reap_node(cpu) do { } while (0)
2384#define next_reap_node(void) do { } while (0)
2385#endif
2386
2387#define REAPTIMEOUT_CPUC (2*HZ)
2388
2389#ifdef CONFIG_SMP
2390static DEFINE_PER_CPU(struct delayed_work, reap_work);
2391
2392static void cache_reap(struct work_struct *unused)
2393{
2394 next_reap_node();
2395 refresh_cpu_vm_stats(smp_processor_id());
2396 schedule_delayed_work(&__get_cpu_var(reap_work),
2397 REAPTIMEOUT_CPUC);
2398}
2399
2400static void __devinit start_cpu_timer(int cpu)
2401{
2402 struct delayed_work *reap_work = &per_cpu(reap_work, cpu);
2403
2404 /*
2405 * When this gets called from do_initcalls via cpucache_init(),
2406 * init_workqueues() has already run, so keventd will be setup
2407 * at that time.
2408 */
2409 if (keventd_up() && reap_work->work.func == NULL) {
2410 init_reap_node(cpu);
2411 INIT_DELAYED_WORK(reap_work, cache_reap);
2412 schedule_delayed_work_on(cpu, reap_work, HZ + 3 * cpu);
2413 }
2414}
2415
2416static int __init cpucache_init(void)
2417{
2418 int cpu;
2419
2420 /*
2421 * Register the timers that drain pcp pages and update vm statistics
2422 */
2423 for_each_online_cpu(cpu)
2424 start_cpu_timer(cpu);
2425 return 0;
2426}
2427__initcall(cpucache_init);
2428#endif
2429
2430#ifdef SLUB_RESILIENCY_TEST
2431static unsigned long validate_slab_cache(struct kmem_cache *s);
2432
2433static void resiliency_test(void)
2434{
2435 u8 *p;
2436
2437 printk(KERN_ERR "SLUB resiliency testing\n");
2438 printk(KERN_ERR "-----------------------\n");
2439 printk(KERN_ERR "A. Corruption after allocation\n");
2440
2441 p = kzalloc(16, GFP_KERNEL);
2442 p[16] = 0x12;
2443 printk(KERN_ERR "\n1. kmalloc-16: Clobber Redzone/next pointer"
2444 " 0x12->0x%p\n\n", p + 16);
2445
2446 validate_slab_cache(kmalloc_caches + 4);
2447
2448 /* Hmmm... The next two are dangerous */
2449 p = kzalloc(32, GFP_KERNEL);
2450 p[32 + sizeof(void *)] = 0x34;
2451 printk(KERN_ERR "\n2. kmalloc-32: Clobber next pointer/next slab"
2452 " 0x34 -> -0x%p\n", p);
2453 printk(KERN_ERR "If allocated object is overwritten then not detectable\n\n");
2454
2455 validate_slab_cache(kmalloc_caches + 5);
2456 p = kzalloc(64, GFP_KERNEL);
2457 p += 64 + (get_cycles() & 0xff) * sizeof(void *);
2458 *p = 0x56;
2459 printk(KERN_ERR "\n3. kmalloc-64: corrupting random byte 0x56->0x%p\n",
2460 p);
2461 printk(KERN_ERR "If allocated object is overwritten then not detectable\n\n");
2462 validate_slab_cache(kmalloc_caches + 6);
2463
2464 printk(KERN_ERR "\nB. Corruption after free\n");
2465 p = kzalloc(128, GFP_KERNEL);
2466 kfree(p);
2467 *p = 0x78;
2468 printk(KERN_ERR "1. kmalloc-128: Clobber first word 0x78->0x%p\n\n", p);
2469 validate_slab_cache(kmalloc_caches + 7);
2470
2471 p = kzalloc(256, GFP_KERNEL);
2472 kfree(p);
2473 p[50] = 0x9a;
2474 printk(KERN_ERR "\n2. kmalloc-256: Clobber 50th byte 0x9a->0x%p\n\n", p);
2475 validate_slab_cache(kmalloc_caches + 8);
2476
2477 p = kzalloc(512, GFP_KERNEL);
2478 kfree(p);
2479 p[512] = 0xab;
2480 printk(KERN_ERR "\n3. kmalloc-512: Clobber redzone 0xab->0x%p\n\n", p);
2481 validate_slab_cache(kmalloc_caches + 9);
2482}
2483#else
2484static void resiliency_test(void) {};
2485#endif
2486
2487/*
2488 * These are not as efficient as kmalloc for the non debug case.
2489 * We do not have the page struct available so we have to touch one
2490 * cacheline in struct kmem_cache to check slab flags.
2491 */
2492void *__kmalloc_track_caller(size_t size, gfp_t gfpflags, void *caller)
2493{
2494 struct kmem_cache *s = get_slab(size, gfpflags);
81819f0f
CL
2495
2496 if (!s)
2497 return NULL;
2498
77c5e2d0 2499 return slab_alloc(s, gfpflags, -1, caller);
81819f0f
CL
2500}
2501
2502void *__kmalloc_node_track_caller(size_t size, gfp_t gfpflags,
2503 int node, void *caller)
2504{
2505 struct kmem_cache *s = get_slab(size, gfpflags);
81819f0f
CL
2506
2507 if (!s)
2508 return NULL;
2509
77c5e2d0 2510 return slab_alloc(s, gfpflags, node, caller);
81819f0f
CL
2511}
2512
2513#ifdef CONFIG_SYSFS
2514
2515static unsigned long count_partial(struct kmem_cache_node *n)
2516{
2517 unsigned long flags;
2518 unsigned long x = 0;
2519 struct page *page;
2520
2521 spin_lock_irqsave(&n->list_lock, flags);
2522 list_for_each_entry(page, &n->partial, lru)
2523 x += page->inuse;
2524 spin_unlock_irqrestore(&n->list_lock, flags);
2525 return x;
2526}
2527
2528enum slab_stat_type {
2529 SL_FULL,
2530 SL_PARTIAL,
2531 SL_CPU,
2532 SL_OBJECTS
2533};
2534
2535#define SO_FULL (1 << SL_FULL)
2536#define SO_PARTIAL (1 << SL_PARTIAL)
2537#define SO_CPU (1 << SL_CPU)
2538#define SO_OBJECTS (1 << SL_OBJECTS)
2539
2540static unsigned long slab_objects(struct kmem_cache *s,
2541 char *buf, unsigned long flags)
2542{
2543 unsigned long total = 0;
2544 int cpu;
2545 int node;
2546 int x;
2547 unsigned long *nodes;
2548 unsigned long *per_cpu;
2549
2550 nodes = kzalloc(2 * sizeof(unsigned long) * nr_node_ids, GFP_KERNEL);
2551 per_cpu = nodes + nr_node_ids;
2552
2553 for_each_possible_cpu(cpu) {
2554 struct page *page = s->cpu_slab[cpu];
2555 int node;
2556
2557 if (page) {
2558 node = page_to_nid(page);
2559 if (flags & SO_CPU) {
2560 int x = 0;
2561
2562 if (flags & SO_OBJECTS)
2563 x = page->inuse;
2564 else
2565 x = 1;
2566 total += x;
2567 nodes[node] += x;
2568 }
2569 per_cpu[node]++;
2570 }
2571 }
2572
2573 for_each_online_node(node) {
2574 struct kmem_cache_node *n = get_node(s, node);
2575
2576 if (flags & SO_PARTIAL) {
2577 if (flags & SO_OBJECTS)
2578 x = count_partial(n);
2579 else
2580 x = n->nr_partial;
2581 total += x;
2582 nodes[node] += x;
2583 }
2584
2585 if (flags & SO_FULL) {
2586 int full_slabs = atomic_read(&n->nr_slabs)
2587 - per_cpu[node]
2588 - n->nr_partial;
2589
2590 if (flags & SO_OBJECTS)
2591 x = full_slabs * s->objects;
2592 else
2593 x = full_slabs;
2594 total += x;
2595 nodes[node] += x;
2596 }
2597 }
2598
2599 x = sprintf(buf, "%lu", total);
2600#ifdef CONFIG_NUMA
2601 for_each_online_node(node)
2602 if (nodes[node])
2603 x += sprintf(buf + x, " N%d=%lu",
2604 node, nodes[node]);
2605#endif
2606 kfree(nodes);
2607 return x + sprintf(buf + x, "\n");
2608}
2609
2610static int any_slab_objects(struct kmem_cache *s)
2611{
2612 int node;
2613 int cpu;
2614
2615 for_each_possible_cpu(cpu)
2616 if (s->cpu_slab[cpu])
2617 return 1;
2618
2619 for_each_node(node) {
2620 struct kmem_cache_node *n = get_node(s, node);
2621
2622 if (n->nr_partial || atomic_read(&n->nr_slabs))
2623 return 1;
2624 }
2625 return 0;
2626}
2627
2628#define to_slab_attr(n) container_of(n, struct slab_attribute, attr)
2629#define to_slab(n) container_of(n, struct kmem_cache, kobj);
2630
2631struct slab_attribute {
2632 struct attribute attr;
2633 ssize_t (*show)(struct kmem_cache *s, char *buf);
2634 ssize_t (*store)(struct kmem_cache *s, const char *x, size_t count);
2635};
2636
2637#define SLAB_ATTR_RO(_name) \
2638 static struct slab_attribute _name##_attr = __ATTR_RO(_name)
2639
2640#define SLAB_ATTR(_name) \
2641 static struct slab_attribute _name##_attr = \
2642 __ATTR(_name, 0644, _name##_show, _name##_store)
2643
2644
2645static ssize_t slab_size_show(struct kmem_cache *s, char *buf)
2646{
2647 return sprintf(buf, "%d\n", s->size);
2648}
2649SLAB_ATTR_RO(slab_size);
2650
2651static ssize_t align_show(struct kmem_cache *s, char *buf)
2652{
2653 return sprintf(buf, "%d\n", s->align);
2654}
2655SLAB_ATTR_RO(align);
2656
2657static ssize_t object_size_show(struct kmem_cache *s, char *buf)
2658{
2659 return sprintf(buf, "%d\n", s->objsize);
2660}
2661SLAB_ATTR_RO(object_size);
2662
2663static ssize_t objs_per_slab_show(struct kmem_cache *s, char *buf)
2664{
2665 return sprintf(buf, "%d\n", s->objects);
2666}
2667SLAB_ATTR_RO(objs_per_slab);
2668
2669static ssize_t order_show(struct kmem_cache *s, char *buf)
2670{
2671 return sprintf(buf, "%d\n", s->order);
2672}
2673SLAB_ATTR_RO(order);
2674
2675static ssize_t ctor_show(struct kmem_cache *s, char *buf)
2676{
2677 if (s->ctor) {
2678 int n = sprint_symbol(buf, (unsigned long)s->ctor);
2679
2680 return n + sprintf(buf + n, "\n");
2681 }
2682 return 0;
2683}
2684SLAB_ATTR_RO(ctor);
2685
2686static ssize_t dtor_show(struct kmem_cache *s, char *buf)
2687{
2688 if (s->dtor) {
2689 int n = sprint_symbol(buf, (unsigned long)s->dtor);
2690
2691 return n + sprintf(buf + n, "\n");
2692 }
2693 return 0;
2694}
2695SLAB_ATTR_RO(dtor);
2696
2697static ssize_t aliases_show(struct kmem_cache *s, char *buf)
2698{
2699 return sprintf(buf, "%d\n", s->refcount - 1);
2700}
2701SLAB_ATTR_RO(aliases);
2702
2703static ssize_t slabs_show(struct kmem_cache *s, char *buf)
2704{
2705 return slab_objects(s, buf, SO_FULL|SO_PARTIAL|SO_CPU);
2706}
2707SLAB_ATTR_RO(slabs);
2708
2709static ssize_t partial_show(struct kmem_cache *s, char *buf)
2710{
2711 return slab_objects(s, buf, SO_PARTIAL);
2712}
2713SLAB_ATTR_RO(partial);
2714
2715static ssize_t cpu_slabs_show(struct kmem_cache *s, char *buf)
2716{
2717 return slab_objects(s, buf, SO_CPU);
2718}
2719SLAB_ATTR_RO(cpu_slabs);
2720
2721static ssize_t objects_show(struct kmem_cache *s, char *buf)
2722{
2723 return slab_objects(s, buf, SO_FULL|SO_PARTIAL|SO_CPU|SO_OBJECTS);
2724}
2725SLAB_ATTR_RO(objects);
2726
2727static ssize_t sanity_checks_show(struct kmem_cache *s, char *buf)
2728{
2729 return sprintf(buf, "%d\n", !!(s->flags & SLAB_DEBUG_FREE));
2730}
2731
2732static ssize_t sanity_checks_store(struct kmem_cache *s,
2733 const char *buf, size_t length)
2734{
2735 s->flags &= ~SLAB_DEBUG_FREE;
2736 if (buf[0] == '1')
2737 s->flags |= SLAB_DEBUG_FREE;
2738 return length;
2739}
2740SLAB_ATTR(sanity_checks);
2741
2742static ssize_t trace_show(struct kmem_cache *s, char *buf)
2743{
2744 return sprintf(buf, "%d\n", !!(s->flags & SLAB_TRACE));
2745}
2746
2747static ssize_t trace_store(struct kmem_cache *s, const char *buf,
2748 size_t length)
2749{
2750 s->flags &= ~SLAB_TRACE;
2751 if (buf[0] == '1')
2752 s->flags |= SLAB_TRACE;
2753 return length;
2754}
2755SLAB_ATTR(trace);
2756
2757static ssize_t reclaim_account_show(struct kmem_cache *s, char *buf)
2758{
2759 return sprintf(buf, "%d\n", !!(s->flags & SLAB_RECLAIM_ACCOUNT));
2760}
2761
2762static ssize_t reclaim_account_store(struct kmem_cache *s,
2763 const char *buf, size_t length)
2764{
2765 s->flags &= ~SLAB_RECLAIM_ACCOUNT;
2766 if (buf[0] == '1')
2767 s->flags |= SLAB_RECLAIM_ACCOUNT;
2768 return length;
2769}
2770SLAB_ATTR(reclaim_account);
2771
2772static ssize_t hwcache_align_show(struct kmem_cache *s, char *buf)
2773{
2774 return sprintf(buf, "%d\n", !!(s->flags &
2775 (SLAB_HWCACHE_ALIGN|SLAB_MUST_HWCACHE_ALIGN)));
2776}
2777SLAB_ATTR_RO(hwcache_align);
2778
2779#ifdef CONFIG_ZONE_DMA
2780static ssize_t cache_dma_show(struct kmem_cache *s, char *buf)
2781{
2782 return sprintf(buf, "%d\n", !!(s->flags & SLAB_CACHE_DMA));
2783}
2784SLAB_ATTR_RO(cache_dma);
2785#endif
2786
2787static ssize_t destroy_by_rcu_show(struct kmem_cache *s, char *buf)
2788{
2789 return sprintf(buf, "%d\n", !!(s->flags & SLAB_DESTROY_BY_RCU));
2790}
2791SLAB_ATTR_RO(destroy_by_rcu);
2792
2793static ssize_t red_zone_show(struct kmem_cache *s, char *buf)
2794{
2795 return sprintf(buf, "%d\n", !!(s->flags & SLAB_RED_ZONE));
2796}
2797
2798static ssize_t red_zone_store(struct kmem_cache *s,
2799 const char *buf, size_t length)
2800{
2801 if (any_slab_objects(s))
2802 return -EBUSY;
2803
2804 s->flags &= ~SLAB_RED_ZONE;
2805 if (buf[0] == '1')
2806 s->flags |= SLAB_RED_ZONE;
2807 calculate_sizes(s);
2808 return length;
2809}
2810SLAB_ATTR(red_zone);
2811
2812static ssize_t poison_show(struct kmem_cache *s, char *buf)
2813{
2814 return sprintf(buf, "%d\n", !!(s->flags & SLAB_POISON));
2815}
2816
2817static ssize_t poison_store(struct kmem_cache *s,
2818 const char *buf, size_t length)
2819{
2820 if (any_slab_objects(s))
2821 return -EBUSY;
2822
2823 s->flags &= ~SLAB_POISON;
2824 if (buf[0] == '1')
2825 s->flags |= SLAB_POISON;
2826 calculate_sizes(s);
2827 return length;
2828}
2829SLAB_ATTR(poison);
2830
2831static ssize_t store_user_show(struct kmem_cache *s, char *buf)
2832{
2833 return sprintf(buf, "%d\n", !!(s->flags & SLAB_STORE_USER));
2834}
2835
2836static ssize_t store_user_store(struct kmem_cache *s,
2837 const char *buf, size_t length)
2838{
2839 if (any_slab_objects(s))
2840 return -EBUSY;
2841
2842 s->flags &= ~SLAB_STORE_USER;
2843 if (buf[0] == '1')
2844 s->flags |= SLAB_STORE_USER;
2845 calculate_sizes(s);
2846 return length;
2847}
2848SLAB_ATTR(store_user);
2849
2850#ifdef CONFIG_NUMA
2851static ssize_t defrag_ratio_show(struct kmem_cache *s, char *buf)
2852{
2853 return sprintf(buf, "%d\n", s->defrag_ratio / 10);
2854}
2855
2856static ssize_t defrag_ratio_store(struct kmem_cache *s,
2857 const char *buf, size_t length)
2858{
2859 int n = simple_strtoul(buf, NULL, 10);
2860
2861 if (n < 100)
2862 s->defrag_ratio = n * 10;
2863 return length;
2864}
2865SLAB_ATTR(defrag_ratio);
2866#endif
2867
2868static struct attribute * slab_attrs[] = {
2869 &slab_size_attr.attr,
2870 &object_size_attr.attr,
2871 &objs_per_slab_attr.attr,
2872 &order_attr.attr,
2873 &objects_attr.attr,
2874 &slabs_attr.attr,
2875 &partial_attr.attr,
2876 &cpu_slabs_attr.attr,
2877 &ctor_attr.attr,
2878 &dtor_attr.attr,
2879 &aliases_attr.attr,
2880 &align_attr.attr,
2881 &sanity_checks_attr.attr,
2882 &trace_attr.attr,
2883 &hwcache_align_attr.attr,
2884 &reclaim_account_attr.attr,
2885 &destroy_by_rcu_attr.attr,
2886 &red_zone_attr.attr,
2887 &poison_attr.attr,
2888 &store_user_attr.attr,
2889#ifdef CONFIG_ZONE_DMA
2890 &cache_dma_attr.attr,
2891#endif
2892#ifdef CONFIG_NUMA
2893 &defrag_ratio_attr.attr,
2894#endif
2895 NULL
2896};
2897
2898static struct attribute_group slab_attr_group = {
2899 .attrs = slab_attrs,
2900};
2901
2902static ssize_t slab_attr_show(struct kobject *kobj,
2903 struct attribute *attr,
2904 char *buf)
2905{
2906 struct slab_attribute *attribute;
2907 struct kmem_cache *s;
2908 int err;
2909
2910 attribute = to_slab_attr(attr);
2911 s = to_slab(kobj);
2912
2913 if (!attribute->show)
2914 return -EIO;
2915
2916 err = attribute->show(s, buf);
2917
2918 return err;
2919}
2920
2921static ssize_t slab_attr_store(struct kobject *kobj,
2922 struct attribute *attr,
2923 const char *buf, size_t len)
2924{
2925 struct slab_attribute *attribute;
2926 struct kmem_cache *s;
2927 int err;
2928
2929 attribute = to_slab_attr(attr);
2930 s = to_slab(kobj);
2931
2932 if (!attribute->store)
2933 return -EIO;
2934
2935 err = attribute->store(s, buf, len);
2936
2937 return err;
2938}
2939
2940static struct sysfs_ops slab_sysfs_ops = {
2941 .show = slab_attr_show,
2942 .store = slab_attr_store,
2943};
2944
2945static struct kobj_type slab_ktype = {
2946 .sysfs_ops = &slab_sysfs_ops,
2947};
2948
2949static int uevent_filter(struct kset *kset, struct kobject *kobj)
2950{
2951 struct kobj_type *ktype = get_ktype(kobj);
2952
2953 if (ktype == &slab_ktype)
2954 return 1;
2955 return 0;
2956}
2957
2958static struct kset_uevent_ops slab_uevent_ops = {
2959 .filter = uevent_filter,
2960};
2961
2962decl_subsys(slab, &slab_ktype, &slab_uevent_ops);
2963
2964#define ID_STR_LENGTH 64
2965
2966/* Create a unique string id for a slab cache:
2967 * format
2968 * :[flags-]size:[memory address of kmemcache]
2969 */
2970static char *create_unique_id(struct kmem_cache *s)
2971{
2972 char *name = kmalloc(ID_STR_LENGTH, GFP_KERNEL);
2973 char *p = name;
2974
2975 BUG_ON(!name);
2976
2977 *p++ = ':';
2978 /*
2979 * First flags affecting slabcache operations. We will only
2980 * get here for aliasable slabs so we do not need to support
2981 * too many flags. The flags here must cover all flags that
2982 * are matched during merging to guarantee that the id is
2983 * unique.
2984 */
2985 if (s->flags & SLAB_CACHE_DMA)
2986 *p++ = 'd';
2987 if (s->flags & SLAB_RECLAIM_ACCOUNT)
2988 *p++ = 'a';
2989 if (s->flags & SLAB_DEBUG_FREE)
2990 *p++ = 'F';
2991 if (p != name + 1)
2992 *p++ = '-';
2993 p += sprintf(p, "%07d", s->size);
2994 BUG_ON(p > name + ID_STR_LENGTH - 1);
2995 return name;
2996}
2997
2998static int sysfs_slab_add(struct kmem_cache *s)
2999{
3000 int err;
3001 const char *name;
3002 int unmergeable;
3003
3004 if (slab_state < SYSFS)
3005 /* Defer until later */
3006 return 0;
3007
3008 unmergeable = slab_unmergeable(s);
3009 if (unmergeable) {
3010 /*
3011 * Slabcache can never be merged so we can use the name proper.
3012 * This is typically the case for debug situations. In that
3013 * case we can catch duplicate names easily.
3014 */
3015 sysfs_remove_link(&slab_subsys.kset.kobj, s->name);
3016 name = s->name;
3017 } else {
3018 /*
3019 * Create a unique name for the slab as a target
3020 * for the symlinks.
3021 */
3022 name = create_unique_id(s);
3023 }
3024
3025 kobj_set_kset_s(s, slab_subsys);
3026 kobject_set_name(&s->kobj, name);
3027 kobject_init(&s->kobj);
3028 err = kobject_add(&s->kobj);
3029 if (err)
3030 return err;
3031
3032 err = sysfs_create_group(&s->kobj, &slab_attr_group);
3033 if (err)
3034 return err;
3035 kobject_uevent(&s->kobj, KOBJ_ADD);
3036 if (!unmergeable) {
3037 /* Setup first alias */
3038 sysfs_slab_alias(s, s->name);
3039 kfree(name);
3040 }
3041 return 0;
3042}
3043
3044static void sysfs_slab_remove(struct kmem_cache *s)
3045{
3046 kobject_uevent(&s->kobj, KOBJ_REMOVE);
3047 kobject_del(&s->kobj);
3048}
3049
3050/*
3051 * Need to buffer aliases during bootup until sysfs becomes
3052 * available lest we loose that information.
3053 */
3054struct saved_alias {
3055 struct kmem_cache *s;
3056 const char *name;
3057 struct saved_alias *next;
3058};
3059
3060struct saved_alias *alias_list;
3061
3062static int sysfs_slab_alias(struct kmem_cache *s, const char *name)
3063{
3064 struct saved_alias *al;
3065
3066 if (slab_state == SYSFS) {
3067 /*
3068 * If we have a leftover link then remove it.
3069 */
3070 sysfs_remove_link(&slab_subsys.kset.kobj, name);
3071 return sysfs_create_link(&slab_subsys.kset.kobj,
3072 &s->kobj, name);
3073 }
3074
3075 al = kmalloc(sizeof(struct saved_alias), GFP_KERNEL);
3076 if (!al)
3077 return -ENOMEM;
3078
3079 al->s = s;
3080 al->name = name;
3081 al->next = alias_list;
3082 alias_list = al;
3083 return 0;
3084}
3085
3086static int __init slab_sysfs_init(void)
3087{
3088 int err;
3089
3090 err = subsystem_register(&slab_subsys);
3091 if (err) {
3092 printk(KERN_ERR "Cannot register slab subsystem.\n");
3093 return -ENOSYS;
3094 }
3095
3096 finish_bootstrap();
3097
3098 while (alias_list) {
3099 struct saved_alias *al = alias_list;
3100
3101 alias_list = alias_list->next;
3102 err = sysfs_slab_alias(al->s, al->name);
3103 BUG_ON(err);
3104 kfree(al);
3105 }
3106
3107 resiliency_test();
3108 return 0;
3109}
3110
3111__initcall(slab_sysfs_init);
3112#else
3113__initcall(finish_bootstrap);
3114#endif