blob: c4f40d373d1e5ec7050671faa8a109031c76f2ee [file] [log] [blame]
Christoph Lameter81819f02007-05-06 14:49:36 -07001/*
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
Christoph Lameter47bfdc02007-05-06 14:49:37 -0700148#define ARCH_KMALLOC_MINALIGN __alignof__(unsigned long long)
Christoph Lameter81819f02007-05-06 14:49:36 -0700149#endif
150
151#ifndef ARCH_SLAB_MINALIGN
Christoph Lameter47bfdc02007-05-06 14:49:37 -0700152#define ARCH_SLAB_MINALIGN __alignof__(unsigned long long)
Christoph Lameter81819f02007-05-06 14:49:36 -0700153#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
Christoph Lameter81819f02007-05-06 14:49:36 -0700300static 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
Christoph Lameter643b1132007-05-06 14:49:42 -0700664/*
665 * Tracking of fully allocated slabs for debugging
666 */
667static void add_full(struct kmem_cache *s, struct page *page)
668{
669 struct kmem_cache_node *n;
670
671 VM_BUG_ON(!irqs_disabled());
672
673 VM_BUG_ON(!irqs_disabled());
674
675 if (!(s->flags & SLAB_STORE_USER))
676 return;
677
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);
682}
683
684static void remove_full(struct kmem_cache *s, struct page *page)
685{
686 struct kmem_cache_node *n;
687
688 if (!(s->flags & SLAB_STORE_USER))
689 return;
690
691 n = get_node(s, page_to_nid(page));
692
693 spin_lock(&n->list_lock);
694 list_del(&page->lru);
695 spin_unlock(&n->list_lock);
696}
697
Christoph Lameter81819f02007-05-06 14:49:36 -0700698static int alloc_object_checks(struct kmem_cache *s, struct page *page,
699 void *object)
700{
701 if (!check_slab(s, page))
702 goto bad;
703
704 if (object && !on_freelist(s, page, object)) {
705 printk(KERN_ERR "SLUB: %s Object 0x%p@0x%p "
706 "already allocated.\n",
707 s->name, object, page);
708 goto dump;
709 }
710
711 if (!check_valid_pointer(s, page, object)) {
712 object_err(s, page, object, "Freelist Pointer check fails");
713 goto dump;
714 }
715
716 if (!object)
717 return 1;
718
719 if (!check_object(s, page, object, 0))
720 goto bad;
721 init_object(s, object, 1);
722
723 if (s->flags & SLAB_TRACE) {
724 printk(KERN_INFO "TRACE %s alloc 0x%p inuse=%d fp=0x%p\n",
725 s->name, object, page->inuse,
726 page->freelist);
727 dump_stack();
728 }
729 return 1;
730dump:
731 dump_stack();
732bad:
733 if (PageSlab(page)) {
734 /*
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.
738 */
739 printk(KERN_ERR "@@@ SLUB: %s slab 0x%p. Marking all objects used.\n",
740 s->name, page);
741 page->inuse = s->objects;
742 page->freelist = NULL;
743 /* Fix up fields that may be corrupted */
744 page->offset = s->offset / sizeof(void *);
745 }
746 return 0;
747}
748
749static int free_object_checks(struct kmem_cache *s, struct page *page,
750 void *object)
751{
752 if (!check_slab(s, page))
753 goto fail;
754
755 if (!check_valid_pointer(s, page, object)) {
756 printk(KERN_ERR "SLUB: %s slab 0x%p invalid "
757 "object pointer 0x%p\n",
758 s->name, page, object);
759 goto fail;
760 }
761
762 if (on_freelist(s, page, object)) {
763 printk(KERN_ERR "SLUB: %s slab 0x%p object "
764 "0x%p already free.\n", s->name, page, object);
765 goto fail;
766 }
767
768 if (!check_object(s, page, object, 1))
769 return 0;
770
771 if (unlikely(s != page->slab)) {
772 if (!PageSlab(page))
773 printk(KERN_ERR "slab_free %s size %d: attempt to"
774 "free object(0x%p) outside of slab.\n",
775 s->name, s->size, object);
776 else
777 if (!page->slab)
778 printk(KERN_ERR
779 "slab_free : no slab(NULL) for object 0x%p.\n",
780 object);
781 else
782 printk(KERN_ERR "slab_free %s(%d): object at 0x%p"
783 " belongs to slab %s(%d)\n",
784 s->name, s->size, object,
785 page->slab->name, page->slab->size);
786 goto fail;
787 }
788 if (s->flags & SLAB_TRACE) {
789 printk(KERN_INFO "TRACE %s free 0x%p inuse=%d fp=0x%p\n",
790 s->name, object, page->inuse,
791 page->freelist);
792 print_section("Object", object, s->objsize);
793 dump_stack();
794 }
795 init_object(s, object, 0);
796 return 1;
797fail:
798 dump_stack();
799 printk(KERN_ERR "@@@ SLUB: %s slab 0x%p object at 0x%p not freed.\n",
800 s->name, page, object);
801 return 0;
802}
803
804/*
805 * Slab allocation and freeing
806 */
807static struct page *allocate_slab(struct kmem_cache *s, gfp_t flags, int node)
808{
809 struct page * page;
810 int pages = 1 << s->order;
811
812 if (s->order)
813 flags |= __GFP_COMP;
814
815 if (s->flags & SLAB_CACHE_DMA)
816 flags |= SLUB_DMA;
817
818 if (node == -1)
819 page = alloc_pages(flags, s->order);
820 else
821 page = alloc_pages_node(node, flags, s->order);
822
823 if (!page)
824 return NULL;
825
826 mod_zone_page_state(page_zone(page),
827 (s->flags & SLAB_RECLAIM_ACCOUNT) ?
828 NR_SLAB_RECLAIMABLE : NR_SLAB_UNRECLAIMABLE,
829 pages);
830
831 return page;
832}
833
834static void setup_object(struct kmem_cache *s, struct page *page,
835 void *object)
836{
837 if (PageError(page)) {
838 init_object(s, object, 0);
839 init_tracking(s, object);
840 }
841
842 if (unlikely(s->ctor)) {
843 int mode = SLAB_CTOR_CONSTRUCTOR;
844
845 if (!(s->flags & __GFP_WAIT))
846 mode |= SLAB_CTOR_ATOMIC;
847
848 s->ctor(object, s, mode);
849 }
850}
851
852static struct page *new_slab(struct kmem_cache *s, gfp_t flags, int node)
853{
854 struct page *page;
855 struct kmem_cache_node *n;
856 void *start;
857 void *end;
858 void *last;
859 void *p;
860
861 if (flags & __GFP_NO_GROW)
862 return NULL;
863
864 BUG_ON(flags & ~(GFP_DMA | GFP_LEVEL_MASK));
865
866 if (flags & __GFP_WAIT)
867 local_irq_enable();
868
869 page = allocate_slab(s, flags & GFP_LEVEL_MASK, node);
870 if (!page)
871 goto out;
872
873 n = get_node(s, page_to_nid(page));
874 if (n)
875 atomic_long_inc(&n->nr_slabs);
876 page->offset = s->offset / sizeof(void *);
877 page->slab = s;
878 page->flags |= 1 << PG_slab;
879 if (s->flags & (SLAB_DEBUG_FREE | SLAB_RED_ZONE | SLAB_POISON |
880 SLAB_STORE_USER | SLAB_TRACE))
881 page->flags |= 1 << PG_error;
882
883 start = page_address(page);
884 end = start + s->objects * s->size;
885
886 if (unlikely(s->flags & SLAB_POISON))
887 memset(start, POISON_INUSE, PAGE_SIZE << s->order);
888
889 last = start;
890 for (p = start + s->size; p < end; p += s->size) {
891 setup_object(s, page, last);
892 set_freepointer(s, last, p);
893 last = p;
894 }
895 setup_object(s, page, last);
896 set_freepointer(s, last, NULL);
897
898 page->freelist = start;
899 page->inuse = 0;
900out:
901 if (flags & __GFP_WAIT)
902 local_irq_disable();
903 return page;
904}
905
906static void __free_slab(struct kmem_cache *s, struct page *page)
907{
908 int pages = 1 << s->order;
909
910 if (unlikely(PageError(page) || s->dtor)) {
911 void *start = page_address(page);
912 void *end = start + (pages << PAGE_SHIFT);
913 void *p;
914
915 slab_pad_check(s, page);
916 for (p = start; p <= end - s->size; p += s->size) {
917 if (s->dtor)
918 s->dtor(p, s, 0);
919 check_object(s, page, p, 0);
920 }
921 }
922
923 mod_zone_page_state(page_zone(page),
924 (s->flags & SLAB_RECLAIM_ACCOUNT) ?
925 NR_SLAB_RECLAIMABLE : NR_SLAB_UNRECLAIMABLE,
926 - pages);
927
928 page->mapping = NULL;
929 __free_pages(page, s->order);
930}
931
932static void rcu_free_slab(struct rcu_head *h)
933{
934 struct page *page;
935
936 page = container_of((struct list_head *)h, struct page, lru);
937 __free_slab(page->slab, page);
938}
939
940static void free_slab(struct kmem_cache *s, struct page *page)
941{
942 if (unlikely(s->flags & SLAB_DESTROY_BY_RCU)) {
943 /*
944 * RCU free overloads the RCU head over the LRU
945 */
946 struct rcu_head *head = (void *)&page->lru;
947
948 call_rcu(head, rcu_free_slab);
949 } else
950 __free_slab(s, page);
951}
952
953static void discard_slab(struct kmem_cache *s, struct page *page)
954{
955 struct kmem_cache_node *n = get_node(s, page_to_nid(page));
956
957 atomic_long_dec(&n->nr_slabs);
958 reset_page_mapcount(page);
959 page->flags &= ~(1 << PG_slab | 1 << PG_error);
960 free_slab(s, page);
961}
962
963/*
964 * Per slab locking using the pagelock
965 */
966static __always_inline void slab_lock(struct page *page)
967{
968 bit_spin_lock(PG_locked, &page->flags);
969}
970
971static __always_inline void slab_unlock(struct page *page)
972{
973 bit_spin_unlock(PG_locked, &page->flags);
974}
975
976static __always_inline int slab_trylock(struct page *page)
977{
978 int rc = 1;
979
980 rc = bit_spin_trylock(PG_locked, &page->flags);
981 return rc;
982}
983
984/*
985 * Management of partially allocated slabs
986 */
987static void add_partial(struct kmem_cache *s, struct page *page)
988{
989 struct kmem_cache_node *n = get_node(s, page_to_nid(page));
990
991 spin_lock(&n->list_lock);
992 n->nr_partial++;
993 list_add(&page->lru, &n->partial);
994 spin_unlock(&n->list_lock);
995}
996
997static void remove_partial(struct kmem_cache *s,
998 struct page *page)
999{
1000 struct kmem_cache_node *n = get_node(s, page_to_nid(page));
1001
1002 spin_lock(&n->list_lock);
1003 list_del(&page->lru);
1004 n->nr_partial--;
1005 spin_unlock(&n->list_lock);
1006}
1007
1008/*
1009 * Lock page and remove it from the partial list
1010 *
1011 * Must hold list_lock
1012 */
1013static int lock_and_del_slab(struct kmem_cache_node *n, struct page *page)
1014{
1015 if (slab_trylock(page)) {
1016 list_del(&page->lru);
1017 n->nr_partial--;
1018 return 1;
1019 }
1020 return 0;
1021}
1022
1023/*
1024 * Try to get a partial slab from a specific node
1025 */
1026static struct page *get_partial_node(struct kmem_cache_node *n)
1027{
1028 struct page *page;
1029
1030 /*
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.
1034 */
1035 if (!n || !n->nr_partial)
1036 return NULL;
1037
1038 spin_lock(&n->list_lock);
1039 list_for_each_entry(page, &n->partial, lru)
1040 if (lock_and_del_slab(n, page))
1041 goto out;
1042 page = NULL;
1043out:
1044 spin_unlock(&n->list_lock);
1045 return page;
1046}
1047
1048/*
1049 * Get a page from somewhere. Search in increasing NUMA
1050 * distances.
1051 */
1052static struct page *get_any_partial(struct kmem_cache *s, gfp_t flags)
1053{
1054#ifdef CONFIG_NUMA
1055 struct zonelist *zonelist;
1056 struct zone **z;
1057 struct page *page;
1058
1059 /*
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.
1065 *
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.
1069 *
1070 * A higher ratio means slabs may be taken from other nodes
1071 * thus reducing the number of partial slabs on those nodes.
1072 *
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.
1078 */
1079 if (!s->defrag_ratio || get_cycles() % 1024 > s->defrag_ratio)
1080 return NULL;
1081
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;
1086
1087 n = get_node(s, zone_to_nid(*z));
1088
1089 if (n && cpuset_zone_allowed_hardwall(*z, flags) &&
1090 n->nr_partial > 2) {
1091 page = get_partial_node(n);
1092 if (page)
1093 return page;
1094 }
1095 }
1096#endif
1097 return NULL;
1098}
1099
1100/*
1101 * Get a partial page, lock it and return it.
1102 */
1103static struct page *get_partial(struct kmem_cache *s, gfp_t flags, int node)
1104{
1105 struct page *page;
1106 int searchnode = (node == -1) ? numa_node_id() : node;
1107
1108 page = get_partial_node(get_node(s, searchnode));
1109 if (page || (flags & __GFP_THISNODE))
1110 return page;
1111
1112 return get_any_partial(s, flags);
1113}
1114
1115/*
1116 * Move a page back to the lists.
1117 *
1118 * Must be called with the slab lock held.
1119 *
1120 * On exit the slab lock will have been dropped.
1121 */
1122static void putback_slab(struct kmem_cache *s, struct page *page)
1123{
1124 if (page->inuse) {
1125 if (page->freelist)
1126 add_partial(s, page);
Christoph Lameter643b1132007-05-06 14:49:42 -07001127 else if (PageError(page))
1128 add_full(s, page);
Christoph Lameter81819f02007-05-06 14:49:36 -07001129 slab_unlock(page);
1130 } else {
1131 slab_unlock(page);
1132 discard_slab(s, page);
1133 }
1134}
1135
1136/*
1137 * Remove the cpu slab
1138 */
1139static void deactivate_slab(struct kmem_cache *s, struct page *page, int cpu)
1140{
1141 s->cpu_slab[cpu] = NULL;
1142 ClearPageActive(page);
1143
1144 putback_slab(s, page);
1145}
1146
1147static void flush_slab(struct kmem_cache *s, struct page *page, int cpu)
1148{
1149 slab_lock(page);
1150 deactivate_slab(s, page, cpu);
1151}
1152
1153/*
1154 * Flush cpu slab.
1155 * Called from IPI handler with interrupts disabled.
1156 */
1157static void __flush_cpu_slab(struct kmem_cache *s, int cpu)
1158{
1159 struct page *page = s->cpu_slab[cpu];
1160
1161 if (likely(page))
1162 flush_slab(s, page, cpu);
1163}
1164
1165static void flush_cpu_slab(void *d)
1166{
1167 struct kmem_cache *s = d;
1168 int cpu = smp_processor_id();
1169
1170 __flush_cpu_slab(s, cpu);
1171}
1172
1173static void flush_all(struct kmem_cache *s)
1174{
1175#ifdef CONFIG_SMP
1176 on_each_cpu(flush_cpu_slab, s, 1, 1);
1177#else
1178 unsigned long flags;
1179
1180 local_irq_save(flags);
1181 flush_cpu_slab(s);
1182 local_irq_restore(flags);
1183#endif
1184}
1185
1186/*
1187 * slab_alloc is optimized to only modify two cachelines on the fast path
1188 * (aside from the stack):
1189 *
1190 * 1. The page struct
1191 * 2. The first cacheline of the object to be allocated.
1192 *
1193 * The only cache lines that are read (apart from code) is the
1194 * per cpu array in the kmem_cache struct.
1195 *
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)
1198 */
Christoph Lameter77c5e2d2007-05-06 14:49:42 -07001199static void *slab_alloc(struct kmem_cache *s,
1200 gfp_t gfpflags, int node, void *addr)
Christoph Lameter81819f02007-05-06 14:49:36 -07001201{
1202 struct page *page;
1203 void **object;
1204 unsigned long flags;
1205 int cpu;
1206
1207 local_irq_save(flags);
1208 cpu = smp_processor_id();
1209 page = s->cpu_slab[cpu];
1210 if (!page)
1211 goto new_slab;
1212
1213 slab_lock(page);
1214 if (unlikely(node != -1 && page_to_nid(page) != node))
1215 goto another_slab;
1216redo:
1217 object = page->freelist;
1218 if (unlikely(!object))
1219 goto another_slab;
1220 if (unlikely(PageError(page)))
1221 goto debug;
1222
1223have_object:
1224 page->inuse++;
1225 page->freelist = object[page->offset];
1226 slab_unlock(page);
1227 local_irq_restore(flags);
1228 return object;
1229
1230another_slab:
1231 deactivate_slab(s, page, cpu);
1232
1233new_slab:
1234 page = get_partial(s, gfpflags, node);
1235 if (likely(page)) {
1236have_slab:
1237 s->cpu_slab[cpu] = page;
1238 SetPageActive(page);
1239 goto redo;
1240 }
1241
1242 page = new_slab(s, gfpflags, node);
1243 if (page) {
1244 cpu = smp_processor_id();
1245 if (s->cpu_slab[cpu]) {
1246 /*
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.
1250 */
1251 if (node == -1 ||
1252 page_to_nid(s->cpu_slab[cpu]) == node) {
1253 /*
1254 * Current cpuslab is acceptable and we
1255 * want the current one since its cache hot
1256 */
1257 discard_slab(s, page);
1258 page = s->cpu_slab[cpu];
1259 slab_lock(page);
1260 goto redo;
1261 }
1262 /* Dump the current slab */
1263 flush_slab(s, s->cpu_slab[cpu], cpu);
1264 }
1265 slab_lock(page);
1266 goto have_slab;
1267 }
1268 local_irq_restore(flags);
1269 return NULL;
1270debug:
1271 if (!alloc_object_checks(s, page, object))
1272 goto another_slab;
1273 if (s->flags & SLAB_STORE_USER)
Christoph Lameter77c5e2d2007-05-06 14:49:42 -07001274 set_track(s, object, TRACK_ALLOC, addr);
Christoph Lameter81819f02007-05-06 14:49:36 -07001275 goto have_object;
1276}
1277
1278void *kmem_cache_alloc(struct kmem_cache *s, gfp_t gfpflags)
1279{
Christoph Lameter77c5e2d2007-05-06 14:49:42 -07001280 return slab_alloc(s, gfpflags, -1, __builtin_return_address(0));
Christoph Lameter81819f02007-05-06 14:49:36 -07001281}
1282EXPORT_SYMBOL(kmem_cache_alloc);
1283
1284#ifdef CONFIG_NUMA
1285void *kmem_cache_alloc_node(struct kmem_cache *s, gfp_t gfpflags, int node)
1286{
Christoph Lameter77c5e2d2007-05-06 14:49:42 -07001287 return slab_alloc(s, gfpflags, node, __builtin_return_address(0));
Christoph Lameter81819f02007-05-06 14:49:36 -07001288}
1289EXPORT_SYMBOL(kmem_cache_alloc_node);
1290#endif
1291
1292/*
1293 * The fastpath only writes the cacheline of the page struct and the first
1294 * cacheline of the object.
1295 *
1296 * No special cachelines need to be read
1297 */
Christoph Lameter77c5e2d2007-05-06 14:49:42 -07001298static void slab_free(struct kmem_cache *s, struct page *page,
1299 void *x, void *addr)
Christoph Lameter81819f02007-05-06 14:49:36 -07001300{
1301 void *prior;
1302 void **object = (void *)x;
1303 unsigned long flags;
1304
1305 local_irq_save(flags);
1306 slab_lock(page);
1307
1308 if (unlikely(PageError(page)))
1309 goto debug;
1310checks_ok:
1311 prior = object[page->offset] = page->freelist;
1312 page->freelist = object;
1313 page->inuse--;
1314
1315 if (unlikely(PageActive(page)))
1316 /*
1317 * Cpu slabs are never on partial lists and are
1318 * never freed.
1319 */
1320 goto out_unlock;
1321
1322 if (unlikely(!page->inuse))
1323 goto slab_empty;
1324
1325 /*
1326 * Objects left in the slab. If it
1327 * was not on the partial list before
1328 * then add it.
1329 */
1330 if (unlikely(!prior))
1331 add_partial(s, page);
1332
1333out_unlock:
1334 slab_unlock(page);
1335 local_irq_restore(flags);
1336 return;
1337
1338slab_empty:
1339 if (prior)
1340 /*
Christoph Lameter643b1132007-05-06 14:49:42 -07001341 * Slab on the partial list.
Christoph Lameter81819f02007-05-06 14:49:36 -07001342 */
1343 remove_partial(s, page);
1344
1345 slab_unlock(page);
1346 discard_slab(s, page);
1347 local_irq_restore(flags);
1348 return;
1349
1350debug:
Christoph Lameter77c5e2d2007-05-06 14:49:42 -07001351 if (!free_object_checks(s, page, x))
1352 goto out_unlock;
Christoph Lameter643b1132007-05-06 14:49:42 -07001353 if (!PageActive(page) && !page->freelist)
1354 remove_full(s, page);
Christoph Lameter77c5e2d2007-05-06 14:49:42 -07001355 if (s->flags & SLAB_STORE_USER)
1356 set_track(s, x, TRACK_FREE, addr);
1357 goto checks_ok;
Christoph Lameter81819f02007-05-06 14:49:36 -07001358}
1359
1360void kmem_cache_free(struct kmem_cache *s, void *x)
1361{
Christoph Lameter77c5e2d2007-05-06 14:49:42 -07001362 struct page *page;
Christoph Lameter81819f02007-05-06 14:49:36 -07001363
Christoph Lameterb49af682007-05-06 14:49:41 -07001364 page = virt_to_head_page(x);
Christoph Lameter81819f02007-05-06 14:49:36 -07001365
Christoph Lameter77c5e2d2007-05-06 14:49:42 -07001366 slab_free(s, page, x, __builtin_return_address(0));
Christoph Lameter81819f02007-05-06 14:49:36 -07001367}
1368EXPORT_SYMBOL(kmem_cache_free);
1369
1370/* Figure out on which slab object the object resides */
1371static struct page *get_object_page(const void *x)
1372{
Christoph Lameterb49af682007-05-06 14:49:41 -07001373 struct page *page = virt_to_head_page(x);
Christoph Lameter81819f02007-05-06 14:49:36 -07001374
1375 if (!PageSlab(page))
1376 return NULL;
1377
1378 return page;
1379}
1380
1381/*
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").
1385 *
1386 * In order to get the desired alignment one just needs to align the
1387 * size.
1388 *
1389 * Notice that the allocation order determines the sizes of the per cpu
1390 * caches. Each processor has always one slab available for allocations.
1391 * Increasing the allocation order reduces the number of times that slabs
1392 * must be moved on and off the partial lists and therefore may influence
1393 * locking overhead.
1394 *
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.
1398 */
1399
1400/*
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.
1405 */
1406static int slub_min_order;
1407static int slub_max_order = DEFAULT_MAX_ORDER;
1408
1409/*
1410 * Minimum number of objects per slab. This is necessary in order to
1411 * reduce locking overhead. Similar to the queue size in SLAB.
1412 */
1413static int slub_min_objects = DEFAULT_MIN_OBJECTS;
1414
1415/*
1416 * Merge control. If this is set then no merging of slab caches will occur.
1417 */
1418static int slub_nomerge;
1419
1420/*
1421 * Debug settings:
1422 */
1423static int slub_debug;
1424
1425static char *slub_debug_slabs;
1426
1427/*
1428 * Calculate the order of allocation given an slab object size.
1429 *
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.
1436 *
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.
1442 *
1443 * Higher order allocations also allow the placement of more objects
1444 * in a slab and thereby reduce object handling overhead. If the user
1445 * has requested a higher mininum order then we start with that one
1446 * instead of zero.
1447 */
1448static int calculate_order(int size)
1449{
1450 int order;
1451 int rem;
1452
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;
1456
1457 if (slub_max_order > order &&
1458 slab_size < slub_min_objects * size)
1459 continue;
1460
1461 if (slab_size < size)
1462 continue;
1463
1464 rem = slab_size % size;
1465
1466 if (rem <= (PAGE_SIZE << order) / 8)
1467 break;
1468
1469 }
1470 if (order >= MAX_ORDER)
1471 return -E2BIG;
1472 return order;
1473}
1474
1475/*
1476 * Function to figure out which alignment to use from the
1477 * various ways of specifying it.
1478 */
1479static unsigned long calculate_alignment(unsigned long flags,
1480 unsigned long align, unsigned long size)
1481{
1482 /*
1483 * If the user wants hardware cache aligned objects then
1484 * follow that suggestion if the object is sufficiently
1485 * large.
1486 *
1487 * The hardware cache alignment cannot override the
1488 * specified alignment though. If that is greater
1489 * then use it.
1490 */
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);
1494
1495 if (align < ARCH_SLAB_MINALIGN)
1496 return ARCH_SLAB_MINALIGN;
1497
1498 return ALIGN(align, sizeof(void *));
1499}
1500
1501static void init_kmem_cache_node(struct kmem_cache_node *n)
1502{
1503 n->nr_partial = 0;
1504 atomic_long_set(&n->nr_slabs, 0);
1505 spin_lock_init(&n->list_lock);
1506 INIT_LIST_HEAD(&n->partial);
Christoph Lameter643b1132007-05-06 14:49:42 -07001507 INIT_LIST_HEAD(&n->full);
Christoph Lameter81819f02007-05-06 14:49:36 -07001508}
1509
1510#ifdef CONFIG_NUMA
1511/*
1512 * No kmalloc_node yet so do it by hand. We know that this is the first
1513 * slab on the node for this slabcache. There are no concurrent accesses
1514 * possible.
1515 *
1516 * Note that this function only works on the kmalloc_node_cache
1517 * when allocating for the kmalloc_node_cache.
1518 */
1519static struct kmem_cache_node * __init early_kmem_cache_node_alloc(gfp_t gfpflags,
1520 int node)
1521{
1522 struct page *page;
1523 struct kmem_cache_node *n;
1524
1525 BUG_ON(kmalloc_caches->size < sizeof(struct kmem_cache_node));
1526
1527 page = new_slab(kmalloc_caches, gfpflags | GFP_THISNODE, node);
1528 /* new_slab() disables interupts */
1529 local_irq_enable();
1530
1531 BUG_ON(!page);
1532 n = page->freelist;
1533 BUG_ON(!n);
1534 page->freelist = get_freepointer(kmalloc_caches, n);
1535 page->inuse++;
1536 kmalloc_caches->node[node] = n;
1537 init_object(kmalloc_caches, n, 1);
1538 init_kmem_cache_node(n);
1539 atomic_long_inc(&n->nr_slabs);
1540 add_partial(kmalloc_caches, page);
1541 return n;
1542}
1543
1544static void free_kmem_cache_nodes(struct kmem_cache *s)
1545{
1546 int node;
1547
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;
1553 }
1554}
1555
1556static int init_kmem_cache_nodes(struct kmem_cache *s, gfp_t gfpflags)
1557{
1558 int node;
1559 int local_node;
1560
1561 if (slab_state >= UP)
1562 local_node = page_to_nid(virt_to_page(s));
1563 else
1564 local_node = 0;
1565
1566 for_each_online_node(node) {
1567 struct kmem_cache_node *n;
1568
1569 if (local_node == node)
1570 n = &s->local_node;
1571 else {
1572 if (slab_state == DOWN) {
1573 n = early_kmem_cache_node_alloc(gfpflags,
1574 node);
1575 continue;
1576 }
1577 n = kmem_cache_alloc_node(kmalloc_caches,
1578 gfpflags, node);
1579
1580 if (!n) {
1581 free_kmem_cache_nodes(s);
1582 return 0;
1583 }
1584
1585 }
1586 s->node[node] = n;
1587 init_kmem_cache_node(n);
1588 }
1589 return 1;
1590}
1591#else
1592static void free_kmem_cache_nodes(struct kmem_cache *s)
1593{
1594}
1595
1596static int init_kmem_cache_nodes(struct kmem_cache *s, gfp_t gfpflags)
1597{
1598 init_kmem_cache_node(&s->local_node);
1599 return 1;
1600}
1601#endif
1602
1603/*
1604 * calculate_sizes() determines the order and the distribution of data within
1605 * a slab object.
1606 */
1607static int calculate_sizes(struct kmem_cache *s)
1608{
1609 unsigned long flags = s->flags;
1610 unsigned long size = s->objsize;
1611 unsigned long align = s->align;
1612
1613 /*
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.
1617 */
1618 if ((flags & SLAB_POISON) && !(flags & SLAB_DESTROY_BY_RCU) &&
1619 !s->ctor && !s->dtor)
1620 s->flags |= __OBJECT_POISON;
1621 else
1622 s->flags &= ~__OBJECT_POISON;
1623
1624 /*
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.
1628 */
1629 size = ALIGN(size, sizeof(void *));
1630
1631 /*
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.
1636 */
1637 if ((flags & SLAB_RED_ZONE) && size == s->objsize)
1638 size += sizeof(void *);
1639
1640 /*
1641 * With that we have determined how much of the slab is in actual
1642 * use by the object. This is the potential offset to the free
1643 * pointer.
1644 */
1645 s->inuse = size;
1646
1647 if (((flags & (SLAB_DESTROY_BY_RCU | SLAB_POISON)) ||
1648 s->ctor || s->dtor)) {
1649 /*
1650 * Relocate free pointer after the object if it is not
1651 * permitted to overwrite the first word of the object on
1652 * kmem_cache_free.
1653 *
1654 * This is the case if we do RCU, have a constructor or
1655 * destructor or are poisoning the objects.
1656 */
1657 s->offset = size;
1658 size += sizeof(void *);
1659 }
1660
1661 if (flags & SLAB_STORE_USER)
1662 /*
1663 * Need to store information about allocs and frees after
1664 * the object.
1665 */
1666 size += 2 * sizeof(struct track);
1667
1668 if (flags & DEBUG_DEFAULT_FLAGS)
1669 /*
1670 * Add some empty padding so that we can catch
1671 * overwrites from earlier objects rather than let
1672 * tracking information or the free pointer be
1673 * corrupted if an user writes before the start
1674 * of the object.
1675 */
1676 size += sizeof(void *);
1677 /*
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).
1681 */
1682 align = calculate_alignment(flags, align, s->objsize);
1683
1684 /*
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.
1688 */
1689 size = ALIGN(size, align);
1690 s->size = size;
1691
1692 s->order = calculate_order(size);
1693 if (s->order < 0)
1694 return 0;
1695
1696 /*
1697 * Determine the number of objects per slab
1698 */
1699 s->objects = (PAGE_SIZE << s->order) / size;
1700
1701 /*
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.
1705 */
1706 if (!s->objects || s->objects > 65535)
1707 return 0;
1708 return 1;
1709
1710}
1711
1712static int __init finish_bootstrap(void)
1713{
1714 struct list_head *h;
1715 int err;
1716
1717 slab_state = SYSFS;
1718
1719 list_for_each(h, &slab_caches) {
1720 struct kmem_cache *s =
1721 container_of(h, struct kmem_cache, list);
1722
1723 err = sysfs_slab_add(s);
1724 BUG_ON(err);
1725 }
1726 return 0;
1727}
1728
1729static 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))
1734{
1735 memset(s, 0, kmem_size);
1736 s->name = name;
1737 s->ctor = ctor;
1738 s->dtor = dtor;
1739 s->objsize = size;
1740 s->flags = flags;
1741 s->align = align;
1742
1743 BUG_ON(flags & SLUB_UNIMPLEMENTED);
1744
1745 /*
1746 * The page->offset field is only 16 bit wide. This is an offset
1747 * in units of words from the beginning of an object. If the slab
1748 * size is bigger then we cannot move the free pointer behind the
1749 * object anymore.
1750 *
1751 * On 32 bit platforms the limit is 256k. On 64bit platforms
1752 * the limit is 512k.
1753 *
1754 * Debugging or ctor/dtors may create a need to move the free
1755 * pointer. Fail if this happens.
1756 */
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);
1761 }
1762 else
1763 /*
1764 * Enable debugging if selected on the kernel commandline.
1765 */
1766 if (slub_debug && (!slub_debug_slabs ||
1767 strncmp(slub_debug_slabs, name,
1768 strlen(slub_debug_slabs)) == 0))
1769 s->flags |= slub_debug;
1770
1771 if (!calculate_sizes(s))
1772 goto error;
1773
1774 s->refcount = 1;
1775#ifdef CONFIG_NUMA
1776 s->defrag_ratio = 100;
1777#endif
1778
1779 if (init_kmem_cache_nodes(s, gfpflags & ~SLUB_DMA))
1780 return 1;
1781error:
1782 if (flags & SLAB_PANIC)
1783 panic("Cannot create slab %s size=%lu realsize=%u "
1784 "order=%u offset=%u flags=%lx\n",
1785 s->name, (unsigned long)size, s->size, s->order,
1786 s->offset, flags);
1787 return 0;
1788}
1789EXPORT_SYMBOL(kmem_cache_open);
1790
1791/*
1792 * Check if a given pointer is valid
1793 */
1794int kmem_ptr_validate(struct kmem_cache *s, const void *object)
1795{
1796 struct page * page;
1797 void *addr;
1798
1799 page = get_object_page(object);
1800
1801 if (!page || s != page->slab)
1802 /* No slab or wrong slab */
1803 return 0;
1804
1805 addr = page_address(page);
1806 if (object < addr || object >= addr + s->objects * s->size)
1807 /* Out of bounds */
1808 return 0;
1809
1810 if ((object - addr) % s->size)
1811 /* Improperly aligned */
1812 return 0;
1813
1814 /*
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.
1819 */
1820 return 1;
1821}
1822EXPORT_SYMBOL(kmem_ptr_validate);
1823
1824/*
1825 * Determine the size of a slab object
1826 */
1827unsigned int kmem_cache_size(struct kmem_cache *s)
1828{
1829 return s->objsize;
1830}
1831EXPORT_SYMBOL(kmem_cache_size);
1832
1833const char *kmem_cache_name(struct kmem_cache *s)
1834{
1835 return s->name;
1836}
1837EXPORT_SYMBOL(kmem_cache_name);
1838
1839/*
1840 * Attempt to free all slabs on a node
1841 */
1842static int free_list(struct kmem_cache *s, struct kmem_cache_node *n,
1843 struct list_head *list)
1844{
1845 int slabs_inuse = 0;
1846 unsigned long flags;
1847 struct page *page, *h;
1848
1849 spin_lock_irqsave(&n->list_lock, flags);
1850 list_for_each_entry_safe(page, h, list, lru)
1851 if (!page->inuse) {
1852 list_del(&page->lru);
1853 discard_slab(s, page);
1854 } else
1855 slabs_inuse++;
1856 spin_unlock_irqrestore(&n->list_lock, flags);
1857 return slabs_inuse;
1858}
1859
1860/*
1861 * Release all resources used by slab cache
1862 */
1863static int kmem_cache_close(struct kmem_cache *s)
1864{
1865 int node;
1866
1867 flush_all(s);
1868
1869 /* Attempt to free all objects */
1870 for_each_online_node(node) {
1871 struct kmem_cache_node *n = get_node(s, node);
1872
1873 free_list(s, n, &n->partial);
1874 if (atomic_long_read(&n->nr_slabs))
1875 return 1;
1876 }
1877 free_kmem_cache_nodes(s);
1878 return 0;
1879}
1880
1881/*
1882 * Close a cache and release the kmem_cache structure
1883 * (must be used for caches created using kmem_cache_create)
1884 */
1885void kmem_cache_destroy(struct kmem_cache *s)
1886{
1887 down_write(&slub_lock);
1888 s->refcount--;
1889 if (!s->refcount) {
1890 list_del(&s->list);
1891 if (kmem_cache_close(s))
1892 WARN_ON(1);
1893 sysfs_slab_remove(s);
1894 kfree(s);
1895 }
1896 up_write(&slub_lock);
1897}
1898EXPORT_SYMBOL(kmem_cache_destroy);
1899
1900/********************************************************************
1901 * Kmalloc subsystem
1902 *******************************************************************/
1903
1904struct kmem_cache kmalloc_caches[KMALLOC_SHIFT_HIGH + 1] __cacheline_aligned;
1905EXPORT_SYMBOL(kmalloc_caches);
1906
1907#ifdef CONFIG_ZONE_DMA
1908static struct kmem_cache *kmalloc_caches_dma[KMALLOC_SHIFT_HIGH + 1];
1909#endif
1910
1911static int __init setup_slub_min_order(char *str)
1912{
1913 get_option (&str, &slub_min_order);
1914
1915 return 1;
1916}
1917
1918__setup("slub_min_order=", setup_slub_min_order);
1919
1920static int __init setup_slub_max_order(char *str)
1921{
1922 get_option (&str, &slub_max_order);
1923
1924 return 1;
1925}
1926
1927__setup("slub_max_order=", setup_slub_max_order);
1928
1929static int __init setup_slub_min_objects(char *str)
1930{
1931 get_option (&str, &slub_min_objects);
1932
1933 return 1;
1934}
1935
1936__setup("slub_min_objects=", setup_slub_min_objects);
1937
1938static int __init setup_slub_nomerge(char *str)
1939{
1940 slub_nomerge = 1;
1941 return 1;
1942}
1943
1944__setup("slub_nomerge", setup_slub_nomerge);
1945
1946static int __init setup_slub_debug(char *str)
1947{
1948 if (!str || *str != '=')
1949 slub_debug = DEBUG_DEFAULT_FLAGS;
1950 else {
1951 str++;
1952 if (*str == 0 || *str == ',')
1953 slub_debug = DEBUG_DEFAULT_FLAGS;
1954 else
1955 for( ;*str && *str != ','; str++)
1956 switch (*str) {
1957 case 'f' : case 'F' :
1958 slub_debug |= SLAB_DEBUG_FREE;
1959 break;
1960 case 'z' : case 'Z' :
1961 slub_debug |= SLAB_RED_ZONE;
1962 break;
1963 case 'p' : case 'P' :
1964 slub_debug |= SLAB_POISON;
1965 break;
1966 case 'u' : case 'U' :
1967 slub_debug |= SLAB_STORE_USER;
1968 break;
1969 case 't' : case 'T' :
1970 slub_debug |= SLAB_TRACE;
1971 break;
1972 default:
1973 printk(KERN_ERR "slub_debug option '%c' "
1974 "unknown. skipped\n",*str);
1975 }
1976 }
1977
1978 if (*str == ',')
1979 slub_debug_slabs = str + 1;
1980 return 1;
1981}
1982
1983__setup("slub_debug", setup_slub_debug);
1984
1985static struct kmem_cache *create_kmalloc_cache(struct kmem_cache *s,
1986 const char *name, int size, gfp_t gfp_flags)
1987{
1988 unsigned int flags = 0;
1989
1990 if (gfp_flags & SLUB_DMA)
1991 flags = SLAB_CACHE_DMA;
1992
1993 down_write(&slub_lock);
1994 if (!kmem_cache_open(s, gfp_flags, name, size, ARCH_KMALLOC_MINALIGN,
1995 flags, NULL, NULL))
1996 goto panic;
1997
1998 list_add(&s->list, &slab_caches);
1999 up_write(&slub_lock);
2000 if (sysfs_slab_add(s))
2001 goto panic;
2002 return s;
2003
2004panic:
2005 panic("Creation of kmalloc slab %s size=%d failed.\n", name, size);
2006}
2007
2008static struct kmem_cache *get_slab(size_t size, gfp_t flags)
2009{
2010 int index = kmalloc_index(size);
2011
Christoph Lameter614410d2007-05-06 14:49:38 -07002012 if (!index)
Christoph Lameter81819f02007-05-06 14:49:36 -07002013 return NULL;
2014
2015 /* Allocation too large? */
2016 BUG_ON(index < 0);
2017
2018#ifdef CONFIG_ZONE_DMA
2019 if ((flags & SLUB_DMA)) {
2020 struct kmem_cache *s;
2021 struct kmem_cache *x;
2022 char *text;
2023 size_t realsize;
2024
2025 s = kmalloc_caches_dma[index];
2026 if (s)
2027 return s;
2028
2029 /* Dynamically create dma cache */
2030 x = kmalloc(kmem_size, flags & ~SLUB_DMA);
2031 if (!x)
2032 panic("Unable to allocate memory for dma cache\n");
2033
2034 if (index <= KMALLOC_SHIFT_HIGH)
2035 realsize = 1 << index;
2036 else {
2037 if (index == 1)
2038 realsize = 96;
2039 else
2040 realsize = 192;
2041 }
2042
2043 text = kasprintf(flags & ~SLUB_DMA, "kmalloc_dma-%d",
2044 (unsigned int)realsize);
2045 s = create_kmalloc_cache(x, text, realsize, flags);
2046 kmalloc_caches_dma[index] = s;
2047 return s;
2048 }
2049#endif
2050 return &kmalloc_caches[index];
2051}
2052
2053void *__kmalloc(size_t size, gfp_t flags)
2054{
2055 struct kmem_cache *s = get_slab(size, flags);
2056
2057 if (s)
Christoph Lameter77c5e2d2007-05-06 14:49:42 -07002058 return slab_alloc(s, flags, -1, __builtin_return_address(0));
Christoph Lameter81819f02007-05-06 14:49:36 -07002059 return NULL;
2060}
2061EXPORT_SYMBOL(__kmalloc);
2062
2063#ifdef CONFIG_NUMA
2064void *__kmalloc_node(size_t size, gfp_t flags, int node)
2065{
2066 struct kmem_cache *s = get_slab(size, flags);
2067
2068 if (s)
Christoph Lameter77c5e2d2007-05-06 14:49:42 -07002069 return slab_alloc(s, flags, node, __builtin_return_address(0));
Christoph Lameter81819f02007-05-06 14:49:36 -07002070 return NULL;
2071}
2072EXPORT_SYMBOL(__kmalloc_node);
2073#endif
2074
2075size_t ksize(const void *object)
2076{
2077 struct page *page = get_object_page(object);
2078 struct kmem_cache *s;
2079
2080 BUG_ON(!page);
2081 s = page->slab;
2082 BUG_ON(!s);
2083
2084 /*
2085 * Debugging requires use of the padding between object
2086 * and whatever may come after it.
2087 */
2088 if (s->flags & (SLAB_RED_ZONE | SLAB_POISON))
2089 return s->objsize;
2090
2091 /*
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.
2095 */
2096 if (s->flags & (SLAB_DESTROY_BY_RCU | SLAB_STORE_USER))
2097 return s->inuse;
2098
2099 /*
2100 * Else we can use all the padding etc for the allocation
2101 */
2102 return s->size;
2103}
2104EXPORT_SYMBOL(ksize);
2105
2106void kfree(const void *x)
2107{
2108 struct kmem_cache *s;
2109 struct page *page;
2110
2111 if (!x)
2112 return;
2113
Christoph Lameterb49af682007-05-06 14:49:41 -07002114 page = virt_to_head_page(x);
Christoph Lameter81819f02007-05-06 14:49:36 -07002115 s = page->slab;
2116
Christoph Lameter77c5e2d2007-05-06 14:49:42 -07002117 slab_free(s, page, (void *)x, __builtin_return_address(0));
Christoph Lameter81819f02007-05-06 14:49:36 -07002118}
2119EXPORT_SYMBOL(kfree);
2120
2121/**
2122 * krealloc - reallocate memory. The contents will remain unchanged.
2123 *
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.
2127 *
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.
2132 */
2133void *krealloc(const void *p, size_t new_size, gfp_t flags)
2134{
2135 struct kmem_cache *new_cache;
2136 void *ret;
2137 struct page *page;
2138
2139 if (unlikely(!p))
2140 return kmalloc(new_size, flags);
2141
2142 if (unlikely(!new_size)) {
2143 kfree(p);
2144 return NULL;
2145 }
2146
Christoph Lameterb49af682007-05-06 14:49:41 -07002147 page = virt_to_head_page(p);
Christoph Lameter81819f02007-05-06 14:49:36 -07002148
2149 new_cache = get_slab(new_size, flags);
2150
2151 /*
2152 * If new size fits in the current cache, bail out.
2153 */
2154 if (likely(page->slab == new_cache))
2155 return (void *)p;
2156
2157 ret = kmalloc(new_size, flags);
2158 if (ret) {
2159 memcpy(ret, p, min(new_size, ksize(p)));
2160 kfree(p);
2161 }
2162 return ret;
2163}
2164EXPORT_SYMBOL(krealloc);
2165
2166/********************************************************************
2167 * Basic setup of slabs
2168 *******************************************************************/
2169
2170void __init kmem_cache_init(void)
2171{
2172 int i;
2173
2174#ifdef CONFIG_NUMA
2175 /*
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.
2179 */
2180 create_kmalloc_cache(&kmalloc_caches[0], "kmem_cache_node",
2181 sizeof(struct kmem_cache_node), GFP_KERNEL);
2182#endif
2183
2184 /* Able to allocate the per node structures */
2185 slab_state = PARTIAL;
2186
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);
2192
2193 for (i = KMALLOC_SHIFT_LOW; i <= KMALLOC_SHIFT_HIGH; i++)
2194 create_kmalloc_cache(&kmalloc_caches[i],
2195 "kmalloc", 1 << i, GFP_KERNEL);
2196
2197 slab_state = UP;
2198
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);
2203
2204#ifdef CONFIG_SMP
2205 register_cpu_notifier(&slab_notifier);
2206#endif
2207
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 *);
2211
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);
2217}
2218
2219/*
2220 * Find a mergeable slab cache
2221 */
2222static int slab_unmergeable(struct kmem_cache *s)
2223{
2224 if (slub_nomerge || (s->flags & SLUB_NEVER_MERGE))
2225 return 1;
2226
2227 if (s->ctor || s->dtor)
2228 return 1;
2229
2230 return 0;
2231}
2232
2233static 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))
2237{
2238 struct list_head *h;
2239
2240 if (slub_nomerge || (flags & SLUB_NEVER_MERGE))
2241 return NULL;
2242
2243 if (ctor || dtor)
2244 return NULL;
2245
2246 size = ALIGN(size, sizeof(void *));
2247 align = calculate_alignment(flags, align, size);
2248 size = ALIGN(size, align);
2249
2250 list_for_each(h, &slab_caches) {
2251 struct kmem_cache *s =
2252 container_of(h, struct kmem_cache, list);
2253
2254 if (slab_unmergeable(s))
2255 continue;
2256
2257 if (size > s->size)
2258 continue;
2259
2260 if (((flags | slub_debug) & SLUB_MERGE_SAME) !=
2261 (s->flags & SLUB_MERGE_SAME))
2262 continue;
2263 /*
2264 * Check if alignment is compatible.
2265 * Courtesy of Adrian Drzewiecki
2266 */
2267 if ((s->size & ~(align -1)) != s->size)
2268 continue;
2269
2270 if (s->size - size >= sizeof(void *))
2271 continue;
2272
2273 return s;
2274 }
2275 return NULL;
2276}
2277
2278struct 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))
2282{
2283 struct kmem_cache *s;
2284
2285 down_write(&slub_lock);
2286 s = find_mergeable(size, align, flags, dtor, ctor);
2287 if (s) {
2288 s->refcount++;
2289 /*
2290 * Adjust the object sizes so that we clear
2291 * the complete object on kzalloc.
2292 */
2293 s->objsize = max(s->objsize, (int)size);
2294 s->inuse = max_t(int, s->inuse, ALIGN(size, sizeof(void *)));
2295 if (sysfs_slab_alias(s, name))
2296 goto err;
2297 } else {
2298 s = kmalloc(kmem_size, GFP_KERNEL);
2299 if (s && kmem_cache_open(s, GFP_KERNEL, name,
2300 size, align, flags, ctor, dtor)) {
2301 if (sysfs_slab_add(s)) {
2302 kfree(s);
2303 goto err;
2304 }
2305 list_add(&s->list, &slab_caches);
2306 } else
2307 kfree(s);
2308 }
2309 up_write(&slub_lock);
2310 return s;
2311
2312err:
2313 up_write(&slub_lock);
2314 if (flags & SLAB_PANIC)
2315 panic("Cannot create slabcache %s\n", name);
2316 else
2317 s = NULL;
2318 return s;
2319}
2320EXPORT_SYMBOL(kmem_cache_create);
2321
2322void *kmem_cache_zalloc(struct kmem_cache *s, gfp_t flags)
2323{
2324 void *x;
2325
Christoph Lameter77c5e2d2007-05-06 14:49:42 -07002326 x = slab_alloc(s, flags, -1, __builtin_return_address(0));
Christoph Lameter81819f02007-05-06 14:49:36 -07002327 if (x)
2328 memset(x, 0, s->objsize);
2329 return x;
2330}
2331EXPORT_SYMBOL(kmem_cache_zalloc);
2332
2333#ifdef CONFIG_SMP
2334static void for_all_slabs(void (*func)(struct kmem_cache *, int), int cpu)
2335{
2336 struct list_head *h;
2337
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);
2342
2343 func(s, cpu);
2344 }
2345 up_read(&slub_lock);
2346}
2347
2348/*
2349 * Use the cpu notifier to insure that the slab are flushed
2350 * when necessary.
2351 */
2352static int __cpuinit slab_cpuup_callback(struct notifier_block *nfb,
2353 unsigned long action, void *hcpu)
2354{
2355 long cpu = (long)hcpu;
2356
2357 switch (action) {
2358 case CPU_UP_CANCELED:
2359 case CPU_DEAD:
2360 for_all_slabs(__flush_cpu_slab, cpu);
2361 break;
2362 default:
2363 break;
2364 }
2365 return NOTIFY_OK;
2366}
2367
2368static struct notifier_block __cpuinitdata slab_notifier =
2369 { &slab_cpuup_callback, NULL, 0 };
2370
2371#endif
2372
2373/***************************************************************
2374 * Compatiblility definitions
2375 **************************************************************/
2376
2377int kmem_cache_shrink(struct kmem_cache *s)
2378{
2379 flush_all(s);
2380 return 0;
2381}
2382EXPORT_SYMBOL(kmem_cache_shrink);
2383
2384#ifdef CONFIG_NUMA
2385
2386/*****************************************************************
2387 * Generic reaper used to support the page allocator
2388 * (the cpu slabs are reaped by a per slab workqueue).
2389 *
2390 * Maybe move this to the page allocator?
2391 ****************************************************************/
2392
2393static DEFINE_PER_CPU(unsigned long, reap_node);
2394
2395static void init_reap_node(int cpu)
2396{
2397 int node;
2398
2399 node = next_node(cpu_to_node(cpu), node_online_map);
2400 if (node == MAX_NUMNODES)
2401 node = first_node(node_online_map);
2402
2403 __get_cpu_var(reap_node) = node;
2404}
2405
2406static void next_reap_node(void)
2407{
2408 int node = __get_cpu_var(reap_node);
2409
2410 /*
2411 * Also drain per cpu pages on remote zones
2412 */
2413 if (node != numa_node_id())
2414 drain_node_pages(node);
2415
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;
2420}
2421#else
2422#define init_reap_node(cpu) do { } while (0)
2423#define next_reap_node(void) do { } while (0)
2424#endif
2425
2426#define REAPTIMEOUT_CPUC (2*HZ)
2427
2428#ifdef CONFIG_SMP
2429static DEFINE_PER_CPU(struct delayed_work, reap_work);
2430
2431static void cache_reap(struct work_struct *unused)
2432{
2433 next_reap_node();
2434 refresh_cpu_vm_stats(smp_processor_id());
2435 schedule_delayed_work(&__get_cpu_var(reap_work),
2436 REAPTIMEOUT_CPUC);
2437}
2438
2439static void __devinit start_cpu_timer(int cpu)
2440{
2441 struct delayed_work *reap_work = &per_cpu(reap_work, cpu);
2442
2443 /*
2444 * When this gets called from do_initcalls via cpucache_init(),
2445 * init_workqueues() has already run, so keventd will be setup
2446 * at that time.
2447 */
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);
2452 }
2453}
2454
2455static int __init cpucache_init(void)
2456{
2457 int cpu;
2458
2459 /*
2460 * Register the timers that drain pcp pages and update vm statistics
2461 */
2462 for_each_online_cpu(cpu)
2463 start_cpu_timer(cpu);
2464 return 0;
2465}
2466__initcall(cpucache_init);
2467#endif
2468
2469#ifdef SLUB_RESILIENCY_TEST
2470static unsigned long validate_slab_cache(struct kmem_cache *s);
2471
2472static void resiliency_test(void)
2473{
2474 u8 *p;
2475
2476 printk(KERN_ERR "SLUB resiliency testing\n");
2477 printk(KERN_ERR "-----------------------\n");
2478 printk(KERN_ERR "A. Corruption after allocation\n");
2479
2480 p = kzalloc(16, GFP_KERNEL);
2481 p[16] = 0x12;
2482 printk(KERN_ERR "\n1. kmalloc-16: Clobber Redzone/next pointer"
2483 " 0x12->0x%p\n\n", p + 16);
2484
2485 validate_slab_cache(kmalloc_caches + 4);
2486
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");
2493
2494 validate_slab_cache(kmalloc_caches + 5);
2495 p = kzalloc(64, GFP_KERNEL);
2496 p += 64 + (get_cycles() & 0xff) * sizeof(void *);
2497 *p = 0x56;
2498 printk(KERN_ERR "\n3. kmalloc-64: corrupting random byte 0x56->0x%p\n",
2499 p);
2500 printk(KERN_ERR "If allocated object is overwritten then not detectable\n\n");
2501 validate_slab_cache(kmalloc_caches + 6);
2502
2503 printk(KERN_ERR "\nB. Corruption after free\n");
2504 p = kzalloc(128, GFP_KERNEL);
2505 kfree(p);
2506 *p = 0x78;
2507 printk(KERN_ERR "1. kmalloc-128: Clobber first word 0x78->0x%p\n\n", p);
2508 validate_slab_cache(kmalloc_caches + 7);
2509
2510 p = kzalloc(256, GFP_KERNEL);
2511 kfree(p);
2512 p[50] = 0x9a;
2513 printk(KERN_ERR "\n2. kmalloc-256: Clobber 50th byte 0x9a->0x%p\n\n", p);
2514 validate_slab_cache(kmalloc_caches + 8);
2515
2516 p = kzalloc(512, GFP_KERNEL);
2517 kfree(p);
2518 p[512] = 0xab;
2519 printk(KERN_ERR "\n3. kmalloc-512: Clobber redzone 0xab->0x%p\n\n", p);
2520 validate_slab_cache(kmalloc_caches + 9);
2521}
2522#else
2523static void resiliency_test(void) {};
2524#endif
2525
2526/*
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.
2530 */
2531void *__kmalloc_track_caller(size_t size, gfp_t gfpflags, void *caller)
2532{
2533 struct kmem_cache *s = get_slab(size, gfpflags);
Christoph Lameter81819f02007-05-06 14:49:36 -07002534
2535 if (!s)
2536 return NULL;
2537
Christoph Lameter77c5e2d2007-05-06 14:49:42 -07002538 return slab_alloc(s, gfpflags, -1, caller);
Christoph Lameter81819f02007-05-06 14:49:36 -07002539}
2540
2541void *__kmalloc_node_track_caller(size_t size, gfp_t gfpflags,
2542 int node, void *caller)
2543{
2544 struct kmem_cache *s = get_slab(size, gfpflags);
Christoph Lameter81819f02007-05-06 14:49:36 -07002545
2546 if (!s)
2547 return NULL;
2548
Christoph Lameter77c5e2d2007-05-06 14:49:42 -07002549 return slab_alloc(s, gfpflags, node, caller);
Christoph Lameter81819f02007-05-06 14:49:36 -07002550}
2551
2552#ifdef CONFIG_SYSFS
2553
2554static unsigned long count_partial(struct kmem_cache_node *n)
2555{
2556 unsigned long flags;
2557 unsigned long x = 0;
2558 struct page *page;
2559
2560 spin_lock_irqsave(&n->list_lock, flags);
2561 list_for_each_entry(page, &n->partial, lru)
2562 x += page->inuse;
2563 spin_unlock_irqrestore(&n->list_lock, flags);
2564 return x;
2565}
2566
2567enum slab_stat_type {
2568 SL_FULL,
2569 SL_PARTIAL,
2570 SL_CPU,
2571 SL_OBJECTS
2572};
2573
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)
2578
2579static unsigned long slab_objects(struct kmem_cache *s,
2580 char *buf, unsigned long flags)
2581{
2582 unsigned long total = 0;
2583 int cpu;
2584 int node;
2585 int x;
2586 unsigned long *nodes;
2587 unsigned long *per_cpu;
2588
2589 nodes = kzalloc(2 * sizeof(unsigned long) * nr_node_ids, GFP_KERNEL);
2590 per_cpu = nodes + nr_node_ids;
2591
2592 for_each_possible_cpu(cpu) {
2593 struct page *page = s->cpu_slab[cpu];
2594 int node;
2595
2596 if (page) {
2597 node = page_to_nid(page);
2598 if (flags & SO_CPU) {
2599 int x = 0;
2600
2601 if (flags & SO_OBJECTS)
2602 x = page->inuse;
2603 else
2604 x = 1;
2605 total += x;
2606 nodes[node] += x;
2607 }
2608 per_cpu[node]++;
2609 }
2610 }
2611
2612 for_each_online_node(node) {
2613 struct kmem_cache_node *n = get_node(s, node);
2614
2615 if (flags & SO_PARTIAL) {
2616 if (flags & SO_OBJECTS)
2617 x = count_partial(n);
2618 else
2619 x = n->nr_partial;
2620 total += x;
2621 nodes[node] += x;
2622 }
2623
2624 if (flags & SO_FULL) {
2625 int full_slabs = atomic_read(&n->nr_slabs)
2626 - per_cpu[node]
2627 - n->nr_partial;
2628
2629 if (flags & SO_OBJECTS)
2630 x = full_slabs * s->objects;
2631 else
2632 x = full_slabs;
2633 total += x;
2634 nodes[node] += x;
2635 }
2636 }
2637
2638 x = sprintf(buf, "%lu", total);
2639#ifdef CONFIG_NUMA
2640 for_each_online_node(node)
2641 if (nodes[node])
2642 x += sprintf(buf + x, " N%d=%lu",
2643 node, nodes[node]);
2644#endif
2645 kfree(nodes);
2646 return x + sprintf(buf + x, "\n");
2647}
2648
2649static int any_slab_objects(struct kmem_cache *s)
2650{
2651 int node;
2652 int cpu;
2653
2654 for_each_possible_cpu(cpu)
2655 if (s->cpu_slab[cpu])
2656 return 1;
2657
2658 for_each_node(node) {
2659 struct kmem_cache_node *n = get_node(s, node);
2660
2661 if (n->nr_partial || atomic_read(&n->nr_slabs))
2662 return 1;
2663 }
2664 return 0;
2665}
2666
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);
2669
2670struct 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);
2674};
2675
2676#define SLAB_ATTR_RO(_name) \
2677 static struct slab_attribute _name##_attr = __ATTR_RO(_name)
2678
2679#define SLAB_ATTR(_name) \
2680 static struct slab_attribute _name##_attr = \
2681 __ATTR(_name, 0644, _name##_show, _name##_store)
2682
2683
2684static ssize_t slab_size_show(struct kmem_cache *s, char *buf)
2685{
2686 return sprintf(buf, "%d\n", s->size);
2687}
2688SLAB_ATTR_RO(slab_size);
2689
2690static ssize_t align_show(struct kmem_cache *s, char *buf)
2691{
2692 return sprintf(buf, "%d\n", s->align);
2693}
2694SLAB_ATTR_RO(align);
2695
2696static ssize_t object_size_show(struct kmem_cache *s, char *buf)
2697{
2698 return sprintf(buf, "%d\n", s->objsize);
2699}
2700SLAB_ATTR_RO(object_size);
2701
2702static ssize_t objs_per_slab_show(struct kmem_cache *s, char *buf)
2703{
2704 return sprintf(buf, "%d\n", s->objects);
2705}
2706SLAB_ATTR_RO(objs_per_slab);
2707
2708static ssize_t order_show(struct kmem_cache *s, char *buf)
2709{
2710 return sprintf(buf, "%d\n", s->order);
2711}
2712SLAB_ATTR_RO(order);
2713
2714static ssize_t ctor_show(struct kmem_cache *s, char *buf)
2715{
2716 if (s->ctor) {
2717 int n = sprint_symbol(buf, (unsigned long)s->ctor);
2718
2719 return n + sprintf(buf + n, "\n");
2720 }
2721 return 0;
2722}
2723SLAB_ATTR_RO(ctor);
2724
2725static ssize_t dtor_show(struct kmem_cache *s, char *buf)
2726{
2727 if (s->dtor) {
2728 int n = sprint_symbol(buf, (unsigned long)s->dtor);
2729
2730 return n + sprintf(buf + n, "\n");
2731 }
2732 return 0;
2733}
2734SLAB_ATTR_RO(dtor);
2735
2736static ssize_t aliases_show(struct kmem_cache *s, char *buf)
2737{
2738 return sprintf(buf, "%d\n", s->refcount - 1);
2739}
2740SLAB_ATTR_RO(aliases);
2741
2742static ssize_t slabs_show(struct kmem_cache *s, char *buf)
2743{
2744 return slab_objects(s, buf, SO_FULL|SO_PARTIAL|SO_CPU);
2745}
2746SLAB_ATTR_RO(slabs);
2747
2748static ssize_t partial_show(struct kmem_cache *s, char *buf)
2749{
2750 return slab_objects(s, buf, SO_PARTIAL);
2751}
2752SLAB_ATTR_RO(partial);
2753
2754static ssize_t cpu_slabs_show(struct kmem_cache *s, char *buf)
2755{
2756 return slab_objects(s, buf, SO_CPU);
2757}
2758SLAB_ATTR_RO(cpu_slabs);
2759
2760static ssize_t objects_show(struct kmem_cache *s, char *buf)
2761{
2762 return slab_objects(s, buf, SO_FULL|SO_PARTIAL|SO_CPU|SO_OBJECTS);
2763}
2764SLAB_ATTR_RO(objects);
2765
2766static ssize_t sanity_checks_show(struct kmem_cache *s, char *buf)
2767{
2768 return sprintf(buf, "%d\n", !!(s->flags & SLAB_DEBUG_FREE));
2769}
2770
2771static ssize_t sanity_checks_store(struct kmem_cache *s,
2772 const char *buf, size_t length)
2773{
2774 s->flags &= ~SLAB_DEBUG_FREE;
2775 if (buf[0] == '1')
2776 s->flags |= SLAB_DEBUG_FREE;
2777 return length;
2778}
2779SLAB_ATTR(sanity_checks);
2780
2781static ssize_t trace_show(struct kmem_cache *s, char *buf)
2782{
2783 return sprintf(buf, "%d\n", !!(s->flags & SLAB_TRACE));
2784}
2785
2786static ssize_t trace_store(struct kmem_cache *s, const char *buf,
2787 size_t length)
2788{
2789 s->flags &= ~SLAB_TRACE;
2790 if (buf[0] == '1')
2791 s->flags |= SLAB_TRACE;
2792 return length;
2793}
2794SLAB_ATTR(trace);
2795
2796static ssize_t reclaim_account_show(struct kmem_cache *s, char *buf)
2797{
2798 return sprintf(buf, "%d\n", !!(s->flags & SLAB_RECLAIM_ACCOUNT));
2799}
2800
2801static ssize_t reclaim_account_store(struct kmem_cache *s,
2802 const char *buf, size_t length)
2803{
2804 s->flags &= ~SLAB_RECLAIM_ACCOUNT;
2805 if (buf[0] == '1')
2806 s->flags |= SLAB_RECLAIM_ACCOUNT;
2807 return length;
2808}
2809SLAB_ATTR(reclaim_account);
2810
2811static ssize_t hwcache_align_show(struct kmem_cache *s, char *buf)
2812{
2813 return sprintf(buf, "%d\n", !!(s->flags &
2814 (SLAB_HWCACHE_ALIGN|SLAB_MUST_HWCACHE_ALIGN)));
2815}
2816SLAB_ATTR_RO(hwcache_align);
2817
2818#ifdef CONFIG_ZONE_DMA
2819static ssize_t cache_dma_show(struct kmem_cache *s, char *buf)
2820{
2821 return sprintf(buf, "%d\n", !!(s->flags & SLAB_CACHE_DMA));
2822}
2823SLAB_ATTR_RO(cache_dma);
2824#endif
2825
2826static ssize_t destroy_by_rcu_show(struct kmem_cache *s, char *buf)
2827{
2828 return sprintf(buf, "%d\n", !!(s->flags & SLAB_DESTROY_BY_RCU));
2829}
2830SLAB_ATTR_RO(destroy_by_rcu);
2831
2832static ssize_t red_zone_show(struct kmem_cache *s, char *buf)
2833{
2834 return sprintf(buf, "%d\n", !!(s->flags & SLAB_RED_ZONE));
2835}
2836
2837static ssize_t red_zone_store(struct kmem_cache *s,
2838 const char *buf, size_t length)
2839{
2840 if (any_slab_objects(s))
2841 return -EBUSY;
2842
2843 s->flags &= ~SLAB_RED_ZONE;
2844 if (buf[0] == '1')
2845 s->flags |= SLAB_RED_ZONE;
2846 calculate_sizes(s);
2847 return length;
2848}
2849SLAB_ATTR(red_zone);
2850
2851static ssize_t poison_show(struct kmem_cache *s, char *buf)
2852{
2853 return sprintf(buf, "%d\n", !!(s->flags & SLAB_POISON));
2854}
2855
2856static ssize_t poison_store(struct kmem_cache *s,
2857 const char *buf, size_t length)
2858{
2859 if (any_slab_objects(s))
2860 return -EBUSY;
2861
2862 s->flags &= ~SLAB_POISON;
2863 if (buf[0] == '1')
2864 s->flags |= SLAB_POISON;
2865 calculate_sizes(s);
2866 return length;
2867}
2868SLAB_ATTR(poison);
2869
2870static ssize_t store_user_show(struct kmem_cache *s, char *buf)
2871{
2872 return sprintf(buf, "%d\n", !!(s->flags & SLAB_STORE_USER));
2873}
2874
2875static ssize_t store_user_store(struct kmem_cache *s,
2876 const char *buf, size_t length)
2877{
2878 if (any_slab_objects(s))
2879 return -EBUSY;
2880
2881 s->flags &= ~SLAB_STORE_USER;
2882 if (buf[0] == '1')
2883 s->flags |= SLAB_STORE_USER;
2884 calculate_sizes(s);
2885 return length;
2886}
2887SLAB_ATTR(store_user);
2888
2889#ifdef CONFIG_NUMA
2890static ssize_t defrag_ratio_show(struct kmem_cache *s, char *buf)
2891{
2892 return sprintf(buf, "%d\n", s->defrag_ratio / 10);
2893}
2894
2895static ssize_t defrag_ratio_store(struct kmem_cache *s,
2896 const char *buf, size_t length)
2897{
2898 int n = simple_strtoul(buf, NULL, 10);
2899
2900 if (n < 100)
2901 s->defrag_ratio = n * 10;
2902 return length;
2903}
2904SLAB_ATTR(defrag_ratio);
2905#endif
2906
2907static struct attribute * slab_attrs[] = {
2908 &slab_size_attr.attr,
2909 &object_size_attr.attr,
2910 &objs_per_slab_attr.attr,
2911 &order_attr.attr,
2912 &objects_attr.attr,
2913 &slabs_attr.attr,
2914 &partial_attr.attr,
2915 &cpu_slabs_attr.attr,
2916 &ctor_attr.attr,
2917 &dtor_attr.attr,
2918 &aliases_attr.attr,
2919 &align_attr.attr,
2920 &sanity_checks_attr.attr,
2921 &trace_attr.attr,
2922 &hwcache_align_attr.attr,
2923 &reclaim_account_attr.attr,
2924 &destroy_by_rcu_attr.attr,
2925 &red_zone_attr.attr,
2926 &poison_attr.attr,
2927 &store_user_attr.attr,
2928#ifdef CONFIG_ZONE_DMA
2929 &cache_dma_attr.attr,
2930#endif
2931#ifdef CONFIG_NUMA
2932 &defrag_ratio_attr.attr,
2933#endif
2934 NULL
2935};
2936
2937static struct attribute_group slab_attr_group = {
2938 .attrs = slab_attrs,
2939};
2940
2941static ssize_t slab_attr_show(struct kobject *kobj,
2942 struct attribute *attr,
2943 char *buf)
2944{
2945 struct slab_attribute *attribute;
2946 struct kmem_cache *s;
2947 int err;
2948
2949 attribute = to_slab_attr(attr);
2950 s = to_slab(kobj);
2951
2952 if (!attribute->show)
2953 return -EIO;
2954
2955 err = attribute->show(s, buf);
2956
2957 return err;
2958}
2959
2960static ssize_t slab_attr_store(struct kobject *kobj,
2961 struct attribute *attr,
2962 const char *buf, size_t len)
2963{
2964 struct slab_attribute *attribute;
2965 struct kmem_cache *s;
2966 int err;
2967
2968 attribute = to_slab_attr(attr);
2969 s = to_slab(kobj);
2970
2971 if (!attribute->store)
2972 return -EIO;
2973
2974 err = attribute->store(s, buf, len);
2975
2976 return err;
2977}
2978
2979static struct sysfs_ops slab_sysfs_ops = {
2980 .show = slab_attr_show,
2981 .store = slab_attr_store,
2982};
2983
2984static struct kobj_type slab_ktype = {
2985 .sysfs_ops = &slab_sysfs_ops,
2986};
2987
2988static int uevent_filter(struct kset *kset, struct kobject *kobj)
2989{
2990 struct kobj_type *ktype = get_ktype(kobj);
2991
2992 if (ktype == &slab_ktype)
2993 return 1;
2994 return 0;
2995}
2996
2997static struct kset_uevent_ops slab_uevent_ops = {
2998 .filter = uevent_filter,
2999};
3000
3001decl_subsys(slab, &slab_ktype, &slab_uevent_ops);
3002
3003#define ID_STR_LENGTH 64
3004
3005/* Create a unique string id for a slab cache:
3006 * format
3007 * :[flags-]size:[memory address of kmemcache]
3008 */
3009static char *create_unique_id(struct kmem_cache *s)
3010{
3011 char *name = kmalloc(ID_STR_LENGTH, GFP_KERNEL);
3012 char *p = name;
3013
3014 BUG_ON(!name);
3015
3016 *p++ = ':';
3017 /*
3018 * First flags affecting slabcache operations. We will only
3019 * get here for aliasable slabs so we do not need to support
3020 * too many flags. The flags here must cover all flags that
3021 * are matched during merging to guarantee that the id is
3022 * unique.
3023 */
3024 if (s->flags & SLAB_CACHE_DMA)
3025 *p++ = 'd';
3026 if (s->flags & SLAB_RECLAIM_ACCOUNT)
3027 *p++ = 'a';
3028 if (s->flags & SLAB_DEBUG_FREE)
3029 *p++ = 'F';
3030 if (p != name + 1)
3031 *p++ = '-';
3032 p += sprintf(p, "%07d", s->size);
3033 BUG_ON(p > name + ID_STR_LENGTH - 1);
3034 return name;
3035}
3036
3037static int sysfs_slab_add(struct kmem_cache *s)
3038{
3039 int err;
3040 const char *name;
3041 int unmergeable;
3042
3043 if (slab_state < SYSFS)
3044 /* Defer until later */
3045 return 0;
3046
3047 unmergeable = slab_unmergeable(s);
3048 if (unmergeable) {
3049 /*
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.
3053 */
3054 sysfs_remove_link(&slab_subsys.kset.kobj, s->name);
3055 name = s->name;
3056 } else {
3057 /*
3058 * Create a unique name for the slab as a target
3059 * for the symlinks.
3060 */
3061 name = create_unique_id(s);
3062 }
3063
3064 kobj_set_kset_s(s, slab_subsys);
3065 kobject_set_name(&s->kobj, name);
3066 kobject_init(&s->kobj);
3067 err = kobject_add(&s->kobj);
3068 if (err)
3069 return err;
3070
3071 err = sysfs_create_group(&s->kobj, &slab_attr_group);
3072 if (err)
3073 return err;
3074 kobject_uevent(&s->kobj, KOBJ_ADD);
3075 if (!unmergeable) {
3076 /* Setup first alias */
3077 sysfs_slab_alias(s, s->name);
3078 kfree(name);
3079 }
3080 return 0;
3081}
3082
3083static void sysfs_slab_remove(struct kmem_cache *s)
3084{
3085 kobject_uevent(&s->kobj, KOBJ_REMOVE);
3086 kobject_del(&s->kobj);
3087}
3088
3089/*
3090 * Need to buffer aliases during bootup until sysfs becomes
3091 * available lest we loose that information.
3092 */
3093struct saved_alias {
3094 struct kmem_cache *s;
3095 const char *name;
3096 struct saved_alias *next;
3097};
3098
3099struct saved_alias *alias_list;
3100
3101static int sysfs_slab_alias(struct kmem_cache *s, const char *name)
3102{
3103 struct saved_alias *al;
3104
3105 if (slab_state == SYSFS) {
3106 /*
3107 * If we have a leftover link then remove it.
3108 */
3109 sysfs_remove_link(&slab_subsys.kset.kobj, name);
3110 return sysfs_create_link(&slab_subsys.kset.kobj,
3111 &s->kobj, name);
3112 }
3113
3114 al = kmalloc(sizeof(struct saved_alias), GFP_KERNEL);
3115 if (!al)
3116 return -ENOMEM;
3117
3118 al->s = s;
3119 al->name = name;
3120 al->next = alias_list;
3121 alias_list = al;
3122 return 0;
3123}
3124
3125static int __init slab_sysfs_init(void)
3126{
3127 int err;
3128
3129 err = subsystem_register(&slab_subsys);
3130 if (err) {
3131 printk(KERN_ERR "Cannot register slab subsystem.\n");
3132 return -ENOSYS;
3133 }
3134
3135 finish_bootstrap();
3136
3137 while (alias_list) {
3138 struct saved_alias *al = alias_list;
3139
3140 alias_list = alias_list->next;
3141 err = sysfs_slab_alias(al->s, al->name);
3142 BUG_ON(err);
3143 kfree(al);
3144 }
3145
3146 resiliency_test();
3147 return 0;
3148}
3149
3150__initcall(slab_sysfs_init);
3151#else
3152__initcall(finish_bootstrap);
3153#endif