]> bbs.cooldavid.org Git - net-next-2.6.git/blame - mm/slub.c
slab allocators: Remove SLAB_DEBUG_INITIAL flag
[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 *
81819f0f
CL
100 * - Variable sizing of the per node arrays
101 */
102
103/* Enable to test recovery from slab corruption on boot */
104#undef SLUB_RESILIENCY_TEST
105
106#if PAGE_SHIFT <= 12
107
108/*
109 * Small page size. Make sure that we do not fragment memory
110 */
111#define DEFAULT_MAX_ORDER 1
112#define DEFAULT_MIN_OBJECTS 4
113
114#else
115
116/*
117 * Large page machines are customarily able to handle larger
118 * page orders.
119 */
120#define DEFAULT_MAX_ORDER 2
121#define DEFAULT_MIN_OBJECTS 8
122
123#endif
124
2086d26a
CL
125/*
126 * Mininum number of partial slabs. These will be left on the partial
127 * lists even if they are empty. kmem_cache_shrink may reclaim them.
128 */
e95eed57
CL
129#define MIN_PARTIAL 2
130
2086d26a
CL
131/*
132 * Maximum number of desirable partial slabs.
133 * The existence of more partial slabs makes kmem_cache_shrink
134 * sort the partial list by the number of objects in the.
135 */
136#define MAX_PARTIAL 10
137
81819f0f
CL
138#define DEBUG_DEFAULT_FLAGS (SLAB_DEBUG_FREE | SLAB_RED_ZONE | \
139 SLAB_POISON | SLAB_STORE_USER)
140/*
141 * Set of flags that will prevent slab merging
142 */
143#define SLUB_NEVER_MERGE (SLAB_RED_ZONE | SLAB_POISON | SLAB_STORE_USER | \
144 SLAB_TRACE | SLAB_DESTROY_BY_RCU)
145
146#define SLUB_MERGE_SAME (SLAB_DEBUG_FREE | SLAB_RECLAIM_ACCOUNT | \
147 SLAB_CACHE_DMA)
148
149#ifndef ARCH_KMALLOC_MINALIGN
47bfdc0d 150#define ARCH_KMALLOC_MINALIGN __alignof__(unsigned long long)
81819f0f
CL
151#endif
152
153#ifndef ARCH_SLAB_MINALIGN
47bfdc0d 154#define ARCH_SLAB_MINALIGN __alignof__(unsigned long long)
81819f0f
CL
155#endif
156
157/* Internal SLUB flags */
158#define __OBJECT_POISON 0x80000000 /* Poison object */
159
160static int kmem_size = sizeof(struct kmem_cache);
161
162#ifdef CONFIG_SMP
163static struct notifier_block slab_notifier;
164#endif
165
166static enum {
167 DOWN, /* No slab functionality available */
168 PARTIAL, /* kmem_cache_open() works but kmalloc does not */
169 UP, /* Everything works */
170 SYSFS /* Sysfs up */
171} slab_state = DOWN;
172
173/* A list of all slab caches on the system */
174static DECLARE_RWSEM(slub_lock);
175LIST_HEAD(slab_caches);
176
177#ifdef CONFIG_SYSFS
178static int sysfs_slab_add(struct kmem_cache *);
179static int sysfs_slab_alias(struct kmem_cache *, const char *);
180static void sysfs_slab_remove(struct kmem_cache *);
181#else
182static int sysfs_slab_add(struct kmem_cache *s) { return 0; }
183static int sysfs_slab_alias(struct kmem_cache *s, const char *p) { return 0; }
184static void sysfs_slab_remove(struct kmem_cache *s) {}
185#endif
186
187/********************************************************************
188 * Core slab cache functions
189 *******************************************************************/
190
191int slab_is_available(void)
192{
193 return slab_state >= UP;
194}
195
196static inline struct kmem_cache_node *get_node(struct kmem_cache *s, int node)
197{
198#ifdef CONFIG_NUMA
199 return s->node[node];
200#else
201 return &s->local_node;
202#endif
203}
204
205/*
206 * Object debugging
207 */
208static void print_section(char *text, u8 *addr, unsigned int length)
209{
210 int i, offset;
211 int newline = 1;
212 char ascii[17];
213
214 ascii[16] = 0;
215
216 for (i = 0; i < length; i++) {
217 if (newline) {
218 printk(KERN_ERR "%10s 0x%p: ", text, addr + i);
219 newline = 0;
220 }
221 printk(" %02x", addr[i]);
222 offset = i % 16;
223 ascii[offset] = isgraph(addr[i]) ? addr[i] : '.';
224 if (offset == 15) {
225 printk(" %s\n",ascii);
226 newline = 1;
227 }
228 }
229 if (!newline) {
230 i %= 16;
231 while (i < 16) {
232 printk(" ");
233 ascii[i] = ' ';
234 i++;
235 }
236 printk(" %s\n", ascii);
237 }
238}
239
240/*
241 * Slow version of get and set free pointer.
242 *
243 * This requires touching the cache lines of kmem_cache.
244 * The offset can also be obtained from the page. In that
245 * case it is in the cacheline that we already need to touch.
246 */
247static void *get_freepointer(struct kmem_cache *s, void *object)
248{
249 return *(void **)(object + s->offset);
250}
251
252static void set_freepointer(struct kmem_cache *s, void *object, void *fp)
253{
254 *(void **)(object + s->offset) = fp;
255}
256
257/*
258 * Tracking user of a slab.
259 */
260struct track {
261 void *addr; /* Called from address */
262 int cpu; /* Was running on cpu */
263 int pid; /* Pid context */
264 unsigned long when; /* When did the operation occur */
265};
266
267enum track_item { TRACK_ALLOC, TRACK_FREE };
268
269static struct track *get_track(struct kmem_cache *s, void *object,
270 enum track_item alloc)
271{
272 struct track *p;
273
274 if (s->offset)
275 p = object + s->offset + sizeof(void *);
276 else
277 p = object + s->inuse;
278
279 return p + alloc;
280}
281
282static void set_track(struct kmem_cache *s, void *object,
283 enum track_item alloc, void *addr)
284{
285 struct track *p;
286
287 if (s->offset)
288 p = object + s->offset + sizeof(void *);
289 else
290 p = object + s->inuse;
291
292 p += alloc;
293 if (addr) {
294 p->addr = addr;
295 p->cpu = smp_processor_id();
296 p->pid = current ? current->pid : -1;
297 p->when = jiffies;
298 } else
299 memset(p, 0, sizeof(struct track));
300}
301
81819f0f
CL
302static void init_tracking(struct kmem_cache *s, void *object)
303{
304 if (s->flags & SLAB_STORE_USER) {
305 set_track(s, object, TRACK_FREE, NULL);
306 set_track(s, object, TRACK_ALLOC, NULL);
307 }
308}
309
310static void print_track(const char *s, struct track *t)
311{
312 if (!t->addr)
313 return;
314
315 printk(KERN_ERR "%s: ", s);
316 __print_symbol("%s", (unsigned long)t->addr);
317 printk(" jiffies_ago=%lu cpu=%u pid=%d\n", jiffies - t->when, t->cpu, t->pid);
318}
319
320static void print_trailer(struct kmem_cache *s, u8 *p)
321{
322 unsigned int off; /* Offset of last byte */
323
324 if (s->flags & SLAB_RED_ZONE)
325 print_section("Redzone", p + s->objsize,
326 s->inuse - s->objsize);
327
328 printk(KERN_ERR "FreePointer 0x%p -> 0x%p\n",
329 p + s->offset,
330 get_freepointer(s, p));
331
332 if (s->offset)
333 off = s->offset + sizeof(void *);
334 else
335 off = s->inuse;
336
337 if (s->flags & SLAB_STORE_USER) {
338 print_track("Last alloc", get_track(s, p, TRACK_ALLOC));
339 print_track("Last free ", get_track(s, p, TRACK_FREE));
340 off += 2 * sizeof(struct track);
341 }
342
343 if (off != s->size)
344 /* Beginning of the filler is the free pointer */
345 print_section("Filler", p + off, s->size - off);
346}
347
348static void object_err(struct kmem_cache *s, struct page *page,
349 u8 *object, char *reason)
350{
351 u8 *addr = page_address(page);
352
353 printk(KERN_ERR "*** SLUB %s: %s@0x%p slab 0x%p\n",
354 s->name, reason, object, page);
355 printk(KERN_ERR " offset=%tu flags=0x%04lx inuse=%u freelist=0x%p\n",
356 object - addr, page->flags, page->inuse, page->freelist);
357 if (object > addr + 16)
358 print_section("Bytes b4", object - 16, 16);
359 print_section("Object", object, min(s->objsize, 128));
360 print_trailer(s, object);
361 dump_stack();
362}
363
364static void slab_err(struct kmem_cache *s, struct page *page, char *reason, ...)
365{
366 va_list args;
367 char buf[100];
368
369 va_start(args, reason);
370 vsnprintf(buf, sizeof(buf), reason, args);
371 va_end(args);
372 printk(KERN_ERR "*** SLUB %s: %s in slab @0x%p\n", s->name, buf,
373 page);
374 dump_stack();
375}
376
377static void init_object(struct kmem_cache *s, void *object, int active)
378{
379 u8 *p = object;
380
381 if (s->flags & __OBJECT_POISON) {
382 memset(p, POISON_FREE, s->objsize - 1);
383 p[s->objsize -1] = POISON_END;
384 }
385
386 if (s->flags & SLAB_RED_ZONE)
387 memset(p + s->objsize,
388 active ? SLUB_RED_ACTIVE : SLUB_RED_INACTIVE,
389 s->inuse - s->objsize);
390}
391
392static int check_bytes(u8 *start, unsigned int value, unsigned int bytes)
393{
394 while (bytes) {
395 if (*start != (u8)value)
396 return 0;
397 start++;
398 bytes--;
399 }
400 return 1;
401}
402
403
404static int check_valid_pointer(struct kmem_cache *s, struct page *page,
405 void *object)
406{
407 void *base;
408
409 if (!object)
410 return 1;
411
412 base = page_address(page);
413 if (object < base || object >= base + s->objects * s->size ||
414 (object - base) % s->size) {
415 return 0;
416 }
417
418 return 1;
419}
420
421/*
422 * Object layout:
423 *
424 * object address
425 * Bytes of the object to be managed.
426 * If the freepointer may overlay the object then the free
427 * pointer is the first word of the object.
428 * Poisoning uses 0x6b (POISON_FREE) and the last byte is
429 * 0xa5 (POISON_END)
430 *
431 * object + s->objsize
432 * Padding to reach word boundary. This is also used for Redzoning.
433 * Padding is extended to word size if Redzoning is enabled
434 * and objsize == inuse.
435 * We fill with 0xbb (RED_INACTIVE) for inactive objects and with
436 * 0xcc (RED_ACTIVE) for objects in use.
437 *
438 * object + s->inuse
439 * A. Free pointer (if we cannot overwrite object on free)
440 * B. Tracking data for SLAB_STORE_USER
441 * C. Padding to reach required alignment boundary
442 * Padding is done using 0x5a (POISON_INUSE)
443 *
444 * object + s->size
445 *
446 * If slabcaches are merged then the objsize and inuse boundaries are to
447 * be ignored. And therefore no slab options that rely on these boundaries
448 * may be used with merged slabcaches.
449 */
450
451static void restore_bytes(struct kmem_cache *s, char *message, u8 data,
452 void *from, void *to)
453{
70d71228 454 printk(KERN_ERR "@@@ SLUB %s: Restoring %s (0x%x) from 0x%p-0x%p\n",
81819f0f
CL
455 s->name, message, data, from, to - 1);
456 memset(from, data, to - from);
457}
458
459static int check_pad_bytes(struct kmem_cache *s, struct page *page, u8 *p)
460{
461 unsigned long off = s->inuse; /* The end of info */
462
463 if (s->offset)
464 /* Freepointer is placed after the object. */
465 off += sizeof(void *);
466
467 if (s->flags & SLAB_STORE_USER)
468 /* We also have user information there */
469 off += 2 * sizeof(struct track);
470
471 if (s->size == off)
472 return 1;
473
474 if (check_bytes(p + off, POISON_INUSE, s->size - off))
475 return 1;
476
477 object_err(s, page, p, "Object padding check fails");
478
479 /*
480 * Restore padding
481 */
482 restore_bytes(s, "object padding", POISON_INUSE, p + off, p + s->size);
483 return 0;
484}
485
486static int slab_pad_check(struct kmem_cache *s, struct page *page)
487{
488 u8 *p;
489 int length, remainder;
490
491 if (!(s->flags & SLAB_POISON))
492 return 1;
493
494 p = page_address(page);
495 length = s->objects * s->size;
496 remainder = (PAGE_SIZE << s->order) - length;
497 if (!remainder)
498 return 1;
499
500 if (!check_bytes(p + length, POISON_INUSE, remainder)) {
70d71228 501 slab_err(s, page, "Padding check failed");
81819f0f
CL
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)) {
70d71228
CL
587 slab_err(s, page, "Not a valid slab page flags=%lx "
588 "mapping=0x%p count=%d", page->flags, page->mapping,
81819f0f
CL
589 page_count(page));
590 return 0;
591 }
592 if (page->offset * sizeof(void *) != s->offset) {
70d71228
CL
593 slab_err(s, page, "Corrupted offset %lu flags=0x%lx "
594 "mapping=0x%p count=%d",
81819f0f 595 (unsigned long)(page->offset * sizeof(void *)),
81819f0f
CL
596 page->flags,
597 page->mapping,
598 page_count(page));
81819f0f
CL
599 return 0;
600 }
601 if (page->inuse > s->objects) {
70d71228
CL
602 slab_err(s, page, "inuse %u > max %u @0x%p flags=%lx "
603 "mapping=0x%p count=%d",
604 s->name, page->inuse, s->objects, page->flags,
81819f0f 605 page->mapping, page_count(page));
81819f0f
CL
606 return 0;
607 }
608 /* Slab_pad_check fixes things up after itself */
609 slab_pad_check(s, page);
610 return 1;
611}
612
613/*
614 * Determine if a certain object on a page is on the freelist and
615 * therefore free. Must hold the slab lock for cpu slabs to
616 * guarantee that the chains are consistent.
617 */
618static int on_freelist(struct kmem_cache *s, struct page *page, void *search)
619{
620 int nr = 0;
621 void *fp = page->freelist;
622 void *object = NULL;
623
624 while (fp && nr <= s->objects) {
625 if (fp == search)
626 return 1;
627 if (!check_valid_pointer(s, page, fp)) {
628 if (object) {
629 object_err(s, page, object,
630 "Freechain corrupt");
631 set_freepointer(s, object, NULL);
632 break;
633 } else {
70d71228
CL
634 slab_err(s, page, "Freepointer 0x%p corrupt",
635 fp);
81819f0f
CL
636 page->freelist = NULL;
637 page->inuse = s->objects;
70d71228
CL
638 printk(KERN_ERR "@@@ SLUB %s: Freelist "
639 "cleared. Slab 0x%p\n",
640 s->name, page);
81819f0f
CL
641 return 0;
642 }
643 break;
644 }
645 object = fp;
646 fp = get_freepointer(s, object);
647 nr++;
648 }
649
650 if (page->inuse != s->objects - nr) {
70d71228
CL
651 slab_err(s, page, "Wrong object count. Counter is %d but "
652 "counted were %d", s, page, page->inuse,
653 s->objects - nr);
81819f0f 654 page->inuse = s->objects - nr;
70d71228
CL
655 printk(KERN_ERR "@@@ SLUB %s: Object count adjusted. "
656 "Slab @0x%p\n", s->name, page);
81819f0f
CL
657 }
658 return search == NULL;
659}
660
643b1138
CL
661/*
662 * Tracking of fully allocated slabs for debugging
663 */
e95eed57 664static void add_full(struct kmem_cache_node *n, struct page *page)
643b1138 665{
643b1138
CL
666 spin_lock(&n->list_lock);
667 list_add(&page->lru, &n->full);
668 spin_unlock(&n->list_lock);
669}
670
671static void remove_full(struct kmem_cache *s, struct page *page)
672{
673 struct kmem_cache_node *n;
674
675 if (!(s->flags & SLAB_STORE_USER))
676 return;
677
678 n = get_node(s, page_to_nid(page));
679
680 spin_lock(&n->list_lock);
681 list_del(&page->lru);
682 spin_unlock(&n->list_lock);
683}
684
81819f0f
CL
685static int alloc_object_checks(struct kmem_cache *s, struct page *page,
686 void *object)
687{
688 if (!check_slab(s, page))
689 goto bad;
690
691 if (object && !on_freelist(s, page, object)) {
70d71228
CL
692 slab_err(s, page, "Object 0x%p already allocated", object);
693 goto bad;
81819f0f
CL
694 }
695
696 if (!check_valid_pointer(s, page, object)) {
697 object_err(s, page, object, "Freelist Pointer check fails");
70d71228 698 goto bad;
81819f0f
CL
699 }
700
701 if (!object)
702 return 1;
703
704 if (!check_object(s, page, object, 0))
705 goto bad;
81819f0f 706
81819f0f 707 return 1;
81819f0f
CL
708bad:
709 if (PageSlab(page)) {
710 /*
711 * If this is a slab page then lets do the best we can
712 * to avoid issues in the future. Marking all objects
713 * as used avoids touching the remainder.
714 */
715 printk(KERN_ERR "@@@ SLUB: %s slab 0x%p. Marking all objects used.\n",
716 s->name, page);
717 page->inuse = s->objects;
718 page->freelist = NULL;
719 /* Fix up fields that may be corrupted */
720 page->offset = s->offset / sizeof(void *);
721 }
722 return 0;
723}
724
725static int free_object_checks(struct kmem_cache *s, struct page *page,
726 void *object)
727{
728 if (!check_slab(s, page))
729 goto fail;
730
731 if (!check_valid_pointer(s, page, object)) {
70d71228 732 slab_err(s, page, "Invalid object pointer 0x%p", object);
81819f0f
CL
733 goto fail;
734 }
735
736 if (on_freelist(s, page, object)) {
70d71228 737 slab_err(s, page, "Object 0x%p already free", object);
81819f0f
CL
738 goto fail;
739 }
740
741 if (!check_object(s, page, object, 1))
742 return 0;
743
744 if (unlikely(s != page->slab)) {
745 if (!PageSlab(page))
70d71228
CL
746 slab_err(s, page, "Attempt to free object(0x%p) "
747 "outside of slab", object);
81819f0f 748 else
70d71228 749 if (!page->slab) {
81819f0f 750 printk(KERN_ERR
70d71228 751 "SLUB <none>: no slab for object 0x%p.\n",
81819f0f 752 object);
70d71228
CL
753 dump_stack();
754 }
81819f0f 755 else
70d71228
CL
756 slab_err(s, page, "object at 0x%p belongs "
757 "to slab %s", object, page->slab->name);
81819f0f
CL
758 goto fail;
759 }
81819f0f
CL
760 return 1;
761fail:
81819f0f
CL
762 printk(KERN_ERR "@@@ SLUB: %s slab 0x%p object at 0x%p not freed.\n",
763 s->name, page, object);
764 return 0;
765}
766
767/*
768 * Slab allocation and freeing
769 */
770static struct page *allocate_slab(struct kmem_cache *s, gfp_t flags, int node)
771{
772 struct page * page;
773 int pages = 1 << s->order;
774
775 if (s->order)
776 flags |= __GFP_COMP;
777
778 if (s->flags & SLAB_CACHE_DMA)
779 flags |= SLUB_DMA;
780
781 if (node == -1)
782 page = alloc_pages(flags, s->order);
783 else
784 page = alloc_pages_node(node, flags, s->order);
785
786 if (!page)
787 return NULL;
788
789 mod_zone_page_state(page_zone(page),
790 (s->flags & SLAB_RECLAIM_ACCOUNT) ?
791 NR_SLAB_RECLAIMABLE : NR_SLAB_UNRECLAIMABLE,
792 pages);
793
794 return page;
795}
796
797static void setup_object(struct kmem_cache *s, struct page *page,
798 void *object)
799{
800 if (PageError(page)) {
801 init_object(s, object, 0);
802 init_tracking(s, object);
803 }
804
805 if (unlikely(s->ctor)) {
806 int mode = SLAB_CTOR_CONSTRUCTOR;
807
808 if (!(s->flags & __GFP_WAIT))
809 mode |= SLAB_CTOR_ATOMIC;
810
811 s->ctor(object, s, mode);
812 }
813}
814
815static struct page *new_slab(struct kmem_cache *s, gfp_t flags, int node)
816{
817 struct page *page;
818 struct kmem_cache_node *n;
819 void *start;
820 void *end;
821 void *last;
822 void *p;
823
824 if (flags & __GFP_NO_GROW)
825 return NULL;
826
827 BUG_ON(flags & ~(GFP_DMA | GFP_LEVEL_MASK));
828
829 if (flags & __GFP_WAIT)
830 local_irq_enable();
831
832 page = allocate_slab(s, flags & GFP_LEVEL_MASK, node);
833 if (!page)
834 goto out;
835
836 n = get_node(s, page_to_nid(page));
837 if (n)
838 atomic_long_inc(&n->nr_slabs);
839 page->offset = s->offset / sizeof(void *);
840 page->slab = s;
841 page->flags |= 1 << PG_slab;
842 if (s->flags & (SLAB_DEBUG_FREE | SLAB_RED_ZONE | SLAB_POISON |
843 SLAB_STORE_USER | SLAB_TRACE))
844 page->flags |= 1 << PG_error;
845
846 start = page_address(page);
847 end = start + s->objects * s->size;
848
849 if (unlikely(s->flags & SLAB_POISON))
850 memset(start, POISON_INUSE, PAGE_SIZE << s->order);
851
852 last = start;
853 for (p = start + s->size; p < end; p += s->size) {
854 setup_object(s, page, last);
855 set_freepointer(s, last, p);
856 last = p;
857 }
858 setup_object(s, page, last);
859 set_freepointer(s, last, NULL);
860
861 page->freelist = start;
862 page->inuse = 0;
863out:
864 if (flags & __GFP_WAIT)
865 local_irq_disable();
866 return page;
867}
868
869static void __free_slab(struct kmem_cache *s, struct page *page)
870{
871 int pages = 1 << s->order;
872
873 if (unlikely(PageError(page) || s->dtor)) {
874 void *start = page_address(page);
875 void *end = start + (pages << PAGE_SHIFT);
876 void *p;
877
878 slab_pad_check(s, page);
879 for (p = start; p <= end - s->size; p += s->size) {
880 if (s->dtor)
881 s->dtor(p, s, 0);
882 check_object(s, page, p, 0);
883 }
884 }
885
886 mod_zone_page_state(page_zone(page),
887 (s->flags & SLAB_RECLAIM_ACCOUNT) ?
888 NR_SLAB_RECLAIMABLE : NR_SLAB_UNRECLAIMABLE,
889 - pages);
890
891 page->mapping = NULL;
892 __free_pages(page, s->order);
893}
894
895static void rcu_free_slab(struct rcu_head *h)
896{
897 struct page *page;
898
899 page = container_of((struct list_head *)h, struct page, lru);
900 __free_slab(page->slab, page);
901}
902
903static void free_slab(struct kmem_cache *s, struct page *page)
904{
905 if (unlikely(s->flags & SLAB_DESTROY_BY_RCU)) {
906 /*
907 * RCU free overloads the RCU head over the LRU
908 */
909 struct rcu_head *head = (void *)&page->lru;
910
911 call_rcu(head, rcu_free_slab);
912 } else
913 __free_slab(s, page);
914}
915
916static void discard_slab(struct kmem_cache *s, struct page *page)
917{
918 struct kmem_cache_node *n = get_node(s, page_to_nid(page));
919
920 atomic_long_dec(&n->nr_slabs);
921 reset_page_mapcount(page);
922 page->flags &= ~(1 << PG_slab | 1 << PG_error);
923 free_slab(s, page);
924}
925
926/*
927 * Per slab locking using the pagelock
928 */
929static __always_inline void slab_lock(struct page *page)
930{
931 bit_spin_lock(PG_locked, &page->flags);
932}
933
934static __always_inline void slab_unlock(struct page *page)
935{
936 bit_spin_unlock(PG_locked, &page->flags);
937}
938
939static __always_inline int slab_trylock(struct page *page)
940{
941 int rc = 1;
942
943 rc = bit_spin_trylock(PG_locked, &page->flags);
944 return rc;
945}
946
947/*
948 * Management of partially allocated slabs
949 */
e95eed57 950static void add_partial_tail(struct kmem_cache_node *n, struct page *page)
81819f0f 951{
e95eed57
CL
952 spin_lock(&n->list_lock);
953 n->nr_partial++;
954 list_add_tail(&page->lru, &n->partial);
955 spin_unlock(&n->list_lock);
956}
81819f0f 957
e95eed57
CL
958static void add_partial(struct kmem_cache_node *n, struct page *page)
959{
81819f0f
CL
960 spin_lock(&n->list_lock);
961 n->nr_partial++;
962 list_add(&page->lru, &n->partial);
963 spin_unlock(&n->list_lock);
964}
965
966static void remove_partial(struct kmem_cache *s,
967 struct page *page)
968{
969 struct kmem_cache_node *n = get_node(s, page_to_nid(page));
970
971 spin_lock(&n->list_lock);
972 list_del(&page->lru);
973 n->nr_partial--;
974 spin_unlock(&n->list_lock);
975}
976
977/*
978 * Lock page and remove it from the partial list
979 *
980 * Must hold list_lock
981 */
982static int lock_and_del_slab(struct kmem_cache_node *n, struct page *page)
983{
984 if (slab_trylock(page)) {
985 list_del(&page->lru);
986 n->nr_partial--;
987 return 1;
988 }
989 return 0;
990}
991
992/*
993 * Try to get a partial slab from a specific node
994 */
995static struct page *get_partial_node(struct kmem_cache_node *n)
996{
997 struct page *page;
998
999 /*
1000 * Racy check. If we mistakenly see no partial slabs then we
1001 * just allocate an empty slab. If we mistakenly try to get a
1002 * partial slab then get_partials() will return NULL.
1003 */
1004 if (!n || !n->nr_partial)
1005 return NULL;
1006
1007 spin_lock(&n->list_lock);
1008 list_for_each_entry(page, &n->partial, lru)
1009 if (lock_and_del_slab(n, page))
1010 goto out;
1011 page = NULL;
1012out:
1013 spin_unlock(&n->list_lock);
1014 return page;
1015}
1016
1017/*
1018 * Get a page from somewhere. Search in increasing NUMA
1019 * distances.
1020 */
1021static struct page *get_any_partial(struct kmem_cache *s, gfp_t flags)
1022{
1023#ifdef CONFIG_NUMA
1024 struct zonelist *zonelist;
1025 struct zone **z;
1026 struct page *page;
1027
1028 /*
1029 * The defrag ratio allows to configure the tradeoffs between
1030 * inter node defragmentation and node local allocations.
1031 * A lower defrag_ratio increases the tendency to do local
1032 * allocations instead of scanning throught the partial
1033 * lists on other nodes.
1034 *
1035 * If defrag_ratio is set to 0 then kmalloc() always
1036 * returns node local objects. If its higher then kmalloc()
1037 * may return off node objects in order to avoid fragmentation.
1038 *
1039 * A higher ratio means slabs may be taken from other nodes
1040 * thus reducing the number of partial slabs on those nodes.
1041 *
1042 * If /sys/slab/xx/defrag_ratio is set to 100 (which makes
1043 * defrag_ratio = 1000) then every (well almost) allocation
1044 * will first attempt to defrag slab caches on other nodes. This
1045 * means scanning over all nodes to look for partial slabs which
1046 * may be a bit expensive to do on every slab allocation.
1047 */
1048 if (!s->defrag_ratio || get_cycles() % 1024 > s->defrag_ratio)
1049 return NULL;
1050
1051 zonelist = &NODE_DATA(slab_node(current->mempolicy))
1052 ->node_zonelists[gfp_zone(flags)];
1053 for (z = zonelist->zones; *z; z++) {
1054 struct kmem_cache_node *n;
1055
1056 n = get_node(s, zone_to_nid(*z));
1057
1058 if (n && cpuset_zone_allowed_hardwall(*z, flags) &&
e95eed57 1059 n->nr_partial > MIN_PARTIAL) {
81819f0f
CL
1060 page = get_partial_node(n);
1061 if (page)
1062 return page;
1063 }
1064 }
1065#endif
1066 return NULL;
1067}
1068
1069/*
1070 * Get a partial page, lock it and return it.
1071 */
1072static struct page *get_partial(struct kmem_cache *s, gfp_t flags, int node)
1073{
1074 struct page *page;
1075 int searchnode = (node == -1) ? numa_node_id() : node;
1076
1077 page = get_partial_node(get_node(s, searchnode));
1078 if (page || (flags & __GFP_THISNODE))
1079 return page;
1080
1081 return get_any_partial(s, flags);
1082}
1083
1084/*
1085 * Move a page back to the lists.
1086 *
1087 * Must be called with the slab lock held.
1088 *
1089 * On exit the slab lock will have been dropped.
1090 */
1091static void putback_slab(struct kmem_cache *s, struct page *page)
1092{
e95eed57
CL
1093 struct kmem_cache_node *n = get_node(s, page_to_nid(page));
1094
81819f0f 1095 if (page->inuse) {
e95eed57 1096
81819f0f 1097 if (page->freelist)
e95eed57
CL
1098 add_partial(n, page);
1099 else if (PageError(page) && (s->flags & SLAB_STORE_USER))
1100 add_full(n, page);
81819f0f 1101 slab_unlock(page);
e95eed57 1102
81819f0f 1103 } else {
e95eed57
CL
1104 if (n->nr_partial < MIN_PARTIAL) {
1105 /*
1106 * Adding an empty page to the partial slabs in order
1107 * to avoid page allocator overhead. This page needs to
1108 * come after all the others that are not fully empty
1109 * in order to make sure that we do maximum
1110 * defragmentation.
1111 */
1112 add_partial_tail(n, page);
1113 slab_unlock(page);
1114 } else {
1115 slab_unlock(page);
1116 discard_slab(s, page);
1117 }
81819f0f
CL
1118 }
1119}
1120
1121/*
1122 * Remove the cpu slab
1123 */
1124static void deactivate_slab(struct kmem_cache *s, struct page *page, int cpu)
1125{
1126 s->cpu_slab[cpu] = NULL;
1127 ClearPageActive(page);
1128
1129 putback_slab(s, page);
1130}
1131
1132static void flush_slab(struct kmem_cache *s, struct page *page, int cpu)
1133{
1134 slab_lock(page);
1135 deactivate_slab(s, page, cpu);
1136}
1137
1138/*
1139 * Flush cpu slab.
1140 * Called from IPI handler with interrupts disabled.
1141 */
1142static void __flush_cpu_slab(struct kmem_cache *s, int cpu)
1143{
1144 struct page *page = s->cpu_slab[cpu];
1145
1146 if (likely(page))
1147 flush_slab(s, page, cpu);
1148}
1149
1150static void flush_cpu_slab(void *d)
1151{
1152 struct kmem_cache *s = d;
1153 int cpu = smp_processor_id();
1154
1155 __flush_cpu_slab(s, cpu);
1156}
1157
1158static void flush_all(struct kmem_cache *s)
1159{
1160#ifdef CONFIG_SMP
1161 on_each_cpu(flush_cpu_slab, s, 1, 1);
1162#else
1163 unsigned long flags;
1164
1165 local_irq_save(flags);
1166 flush_cpu_slab(s);
1167 local_irq_restore(flags);
1168#endif
1169}
1170
1171/*
1172 * slab_alloc is optimized to only modify two cachelines on the fast path
1173 * (aside from the stack):
1174 *
1175 * 1. The page struct
1176 * 2. The first cacheline of the object to be allocated.
1177 *
1178 * The only cache lines that are read (apart from code) is the
1179 * per cpu array in the kmem_cache struct.
1180 *
1181 * Fastpath is not possible if we need to get a new slab or have
1182 * debugging enabled (which means all slabs are marked with PageError)
1183 */
77c5e2d0
CL
1184static void *slab_alloc(struct kmem_cache *s,
1185 gfp_t gfpflags, int node, void *addr)
81819f0f
CL
1186{
1187 struct page *page;
1188 void **object;
1189 unsigned long flags;
1190 int cpu;
1191
1192 local_irq_save(flags);
1193 cpu = smp_processor_id();
1194 page = s->cpu_slab[cpu];
1195 if (!page)
1196 goto new_slab;
1197
1198 slab_lock(page);
1199 if (unlikely(node != -1 && page_to_nid(page) != node))
1200 goto another_slab;
1201redo:
1202 object = page->freelist;
1203 if (unlikely(!object))
1204 goto another_slab;
1205 if (unlikely(PageError(page)))
1206 goto debug;
1207
1208have_object:
1209 page->inuse++;
1210 page->freelist = object[page->offset];
1211 slab_unlock(page);
1212 local_irq_restore(flags);
1213 return object;
1214
1215another_slab:
1216 deactivate_slab(s, page, cpu);
1217
1218new_slab:
1219 page = get_partial(s, gfpflags, node);
1220 if (likely(page)) {
1221have_slab:
1222 s->cpu_slab[cpu] = page;
1223 SetPageActive(page);
1224 goto redo;
1225 }
1226
1227 page = new_slab(s, gfpflags, node);
1228 if (page) {
1229 cpu = smp_processor_id();
1230 if (s->cpu_slab[cpu]) {
1231 /*
1232 * Someone else populated the cpu_slab while we enabled
1233 * interrupts, or we have got scheduled on another cpu.
1234 * The page may not be on the requested node.
1235 */
1236 if (node == -1 ||
1237 page_to_nid(s->cpu_slab[cpu]) == node) {
1238 /*
1239 * Current cpuslab is acceptable and we
1240 * want the current one since its cache hot
1241 */
1242 discard_slab(s, page);
1243 page = s->cpu_slab[cpu];
1244 slab_lock(page);
1245 goto redo;
1246 }
1247 /* Dump the current slab */
1248 flush_slab(s, s->cpu_slab[cpu], cpu);
1249 }
1250 slab_lock(page);
1251 goto have_slab;
1252 }
1253 local_irq_restore(flags);
1254 return NULL;
1255debug:
1256 if (!alloc_object_checks(s, page, object))
1257 goto another_slab;
1258 if (s->flags & SLAB_STORE_USER)
77c5e2d0 1259 set_track(s, object, TRACK_ALLOC, addr);
70d71228
CL
1260 if (s->flags & SLAB_TRACE) {
1261 printk(KERN_INFO "TRACE %s alloc 0x%p inuse=%d fp=0x%p\n",
1262 s->name, object, page->inuse,
1263 page->freelist);
1264 dump_stack();
1265 }
1266 init_object(s, object, 1);
81819f0f
CL
1267 goto have_object;
1268}
1269
1270void *kmem_cache_alloc(struct kmem_cache *s, gfp_t gfpflags)
1271{
77c5e2d0 1272 return slab_alloc(s, gfpflags, -1, __builtin_return_address(0));
81819f0f
CL
1273}
1274EXPORT_SYMBOL(kmem_cache_alloc);
1275
1276#ifdef CONFIG_NUMA
1277void *kmem_cache_alloc_node(struct kmem_cache *s, gfp_t gfpflags, int node)
1278{
77c5e2d0 1279 return slab_alloc(s, gfpflags, node, __builtin_return_address(0));
81819f0f
CL
1280}
1281EXPORT_SYMBOL(kmem_cache_alloc_node);
1282#endif
1283
1284/*
1285 * The fastpath only writes the cacheline of the page struct and the first
1286 * cacheline of the object.
1287 *
1288 * No special cachelines need to be read
1289 */
77c5e2d0
CL
1290static void slab_free(struct kmem_cache *s, struct page *page,
1291 void *x, void *addr)
81819f0f
CL
1292{
1293 void *prior;
1294 void **object = (void *)x;
1295 unsigned long flags;
1296
1297 local_irq_save(flags);
1298 slab_lock(page);
1299
1300 if (unlikely(PageError(page)))
1301 goto debug;
1302checks_ok:
1303 prior = object[page->offset] = page->freelist;
1304 page->freelist = object;
1305 page->inuse--;
1306
1307 if (unlikely(PageActive(page)))
1308 /*
1309 * Cpu slabs are never on partial lists and are
1310 * never freed.
1311 */
1312 goto out_unlock;
1313
1314 if (unlikely(!page->inuse))
1315 goto slab_empty;
1316
1317 /*
1318 * Objects left in the slab. If it
1319 * was not on the partial list before
1320 * then add it.
1321 */
1322 if (unlikely(!prior))
e95eed57 1323 add_partial(get_node(s, page_to_nid(page)), page);
81819f0f
CL
1324
1325out_unlock:
1326 slab_unlock(page);
1327 local_irq_restore(flags);
1328 return;
1329
1330slab_empty:
1331 if (prior)
1332 /*
643b1138 1333 * Slab on the partial list.
81819f0f
CL
1334 */
1335 remove_partial(s, page);
1336
1337 slab_unlock(page);
1338 discard_slab(s, page);
1339 local_irq_restore(flags);
1340 return;
1341
1342debug:
77c5e2d0
CL
1343 if (!free_object_checks(s, page, x))
1344 goto out_unlock;
643b1138
CL
1345 if (!PageActive(page) && !page->freelist)
1346 remove_full(s, page);
77c5e2d0
CL
1347 if (s->flags & SLAB_STORE_USER)
1348 set_track(s, x, TRACK_FREE, addr);
70d71228
CL
1349 if (s->flags & SLAB_TRACE) {
1350 printk(KERN_INFO "TRACE %s free 0x%p inuse=%d fp=0x%p\n",
1351 s->name, object, page->inuse,
1352 page->freelist);
1353 print_section("Object", (void *)object, s->objsize);
1354 dump_stack();
1355 }
1356 init_object(s, object, 0);
77c5e2d0 1357 goto checks_ok;
81819f0f
CL
1358}
1359
1360void kmem_cache_free(struct kmem_cache *s, void *x)
1361{
77c5e2d0 1362 struct page *page;
81819f0f 1363
b49af68f 1364 page = virt_to_head_page(x);
81819f0f 1365
77c5e2d0 1366 slab_free(s, page, x, __builtin_return_address(0));
81819f0f
CL
1367}
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{
b49af68f 1373 struct page *page = virt_to_head_page(x);
81819f0f
CL
1374
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 */
5af60839 1491 if ((flags & SLAB_HWCACHE_ALIGN) &&
81819f0f
CL
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);
643b1138 1507 INIT_LIST_HEAD(&n->full);
81819f0f
CL
1508}
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);
e95eed57 1540 add_partial(n, page);
81819f0f
CL
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
81819f0f
CL
1743 /*
1744 * The page->offset field is only 16 bit wide. This is an offset
1745 * in units of words from the beginning of an object. If the slab
1746 * size is bigger then we cannot move the free pointer behind the
1747 * object anymore.
1748 *
1749 * On 32 bit platforms the limit is 256k. On 64bit platforms
1750 * the limit is 512k.
1751 *
1752 * Debugging or ctor/dtors may create a need to move the free
1753 * pointer. Fail if this happens.
1754 */
1755 if (s->size >= 65535 * sizeof(void *)) {
1756 BUG_ON(flags & (SLAB_RED_ZONE | SLAB_POISON |
1757 SLAB_STORE_USER | SLAB_DESTROY_BY_RCU));
1758 BUG_ON(ctor || dtor);
1759 }
1760 else
1761 /*
1762 * Enable debugging if selected on the kernel commandline.
1763 */
1764 if (slub_debug && (!slub_debug_slabs ||
1765 strncmp(slub_debug_slabs, name,
1766 strlen(slub_debug_slabs)) == 0))
1767 s->flags |= slub_debug;
1768
1769 if (!calculate_sizes(s))
1770 goto error;
1771
1772 s->refcount = 1;
1773#ifdef CONFIG_NUMA
1774 s->defrag_ratio = 100;
1775#endif
1776
1777 if (init_kmem_cache_nodes(s, gfpflags & ~SLUB_DMA))
1778 return 1;
1779error:
1780 if (flags & SLAB_PANIC)
1781 panic("Cannot create slab %s size=%lu realsize=%u "
1782 "order=%u offset=%u flags=%lx\n",
1783 s->name, (unsigned long)size, s->size, s->order,
1784 s->offset, flags);
1785 return 0;
1786}
1787EXPORT_SYMBOL(kmem_cache_open);
1788
1789/*
1790 * Check if a given pointer is valid
1791 */
1792int kmem_ptr_validate(struct kmem_cache *s, const void *object)
1793{
1794 struct page * page;
1795 void *addr;
1796
1797 page = get_object_page(object);
1798
1799 if (!page || s != page->slab)
1800 /* No slab or wrong slab */
1801 return 0;
1802
1803 addr = page_address(page);
1804 if (object < addr || object >= addr + s->objects * s->size)
1805 /* Out of bounds */
1806 return 0;
1807
1808 if ((object - addr) % s->size)
1809 /* Improperly aligned */
1810 return 0;
1811
1812 /*
1813 * We could also check if the object is on the slabs freelist.
1814 * But this would be too expensive and it seems that the main
1815 * purpose of kmem_ptr_valid is to check if the object belongs
1816 * to a certain slab.
1817 */
1818 return 1;
1819}
1820EXPORT_SYMBOL(kmem_ptr_validate);
1821
1822/*
1823 * Determine the size of a slab object
1824 */
1825unsigned int kmem_cache_size(struct kmem_cache *s)
1826{
1827 return s->objsize;
1828}
1829EXPORT_SYMBOL(kmem_cache_size);
1830
1831const char *kmem_cache_name(struct kmem_cache *s)
1832{
1833 return s->name;
1834}
1835EXPORT_SYMBOL(kmem_cache_name);
1836
1837/*
1838 * Attempt to free all slabs on a node
1839 */
1840static int free_list(struct kmem_cache *s, struct kmem_cache_node *n,
1841 struct list_head *list)
1842{
1843 int slabs_inuse = 0;
1844 unsigned long flags;
1845 struct page *page, *h;
1846
1847 spin_lock_irqsave(&n->list_lock, flags);
1848 list_for_each_entry_safe(page, h, list, lru)
1849 if (!page->inuse) {
1850 list_del(&page->lru);
1851 discard_slab(s, page);
1852 } else
1853 slabs_inuse++;
1854 spin_unlock_irqrestore(&n->list_lock, flags);
1855 return slabs_inuse;
1856}
1857
1858/*
1859 * Release all resources used by slab cache
1860 */
1861static int kmem_cache_close(struct kmem_cache *s)
1862{
1863 int node;
1864
1865 flush_all(s);
1866
1867 /* Attempt to free all objects */
1868 for_each_online_node(node) {
1869 struct kmem_cache_node *n = get_node(s, node);
1870
2086d26a 1871 n->nr_partial -= free_list(s, n, &n->partial);
81819f0f
CL
1872 if (atomic_long_read(&n->nr_slabs))
1873 return 1;
1874 }
1875 free_kmem_cache_nodes(s);
1876 return 0;
1877}
1878
1879/*
1880 * Close a cache and release the kmem_cache structure
1881 * (must be used for caches created using kmem_cache_create)
1882 */
1883void kmem_cache_destroy(struct kmem_cache *s)
1884{
1885 down_write(&slub_lock);
1886 s->refcount--;
1887 if (!s->refcount) {
1888 list_del(&s->list);
1889 if (kmem_cache_close(s))
1890 WARN_ON(1);
1891 sysfs_slab_remove(s);
1892 kfree(s);
1893 }
1894 up_write(&slub_lock);
1895}
1896EXPORT_SYMBOL(kmem_cache_destroy);
1897
1898/********************************************************************
1899 * Kmalloc subsystem
1900 *******************************************************************/
1901
1902struct kmem_cache kmalloc_caches[KMALLOC_SHIFT_HIGH + 1] __cacheline_aligned;
1903EXPORT_SYMBOL(kmalloc_caches);
1904
1905#ifdef CONFIG_ZONE_DMA
1906static struct kmem_cache *kmalloc_caches_dma[KMALLOC_SHIFT_HIGH + 1];
1907#endif
1908
1909static int __init setup_slub_min_order(char *str)
1910{
1911 get_option (&str, &slub_min_order);
1912
1913 return 1;
1914}
1915
1916__setup("slub_min_order=", setup_slub_min_order);
1917
1918static int __init setup_slub_max_order(char *str)
1919{
1920 get_option (&str, &slub_max_order);
1921
1922 return 1;
1923}
1924
1925__setup("slub_max_order=", setup_slub_max_order);
1926
1927static int __init setup_slub_min_objects(char *str)
1928{
1929 get_option (&str, &slub_min_objects);
1930
1931 return 1;
1932}
1933
1934__setup("slub_min_objects=", setup_slub_min_objects);
1935
1936static int __init setup_slub_nomerge(char *str)
1937{
1938 slub_nomerge = 1;
1939 return 1;
1940}
1941
1942__setup("slub_nomerge", setup_slub_nomerge);
1943
1944static int __init setup_slub_debug(char *str)
1945{
1946 if (!str || *str != '=')
1947 slub_debug = DEBUG_DEFAULT_FLAGS;
1948 else {
1949 str++;
1950 if (*str == 0 || *str == ',')
1951 slub_debug = DEBUG_DEFAULT_FLAGS;
1952 else
1953 for( ;*str && *str != ','; str++)
1954 switch (*str) {
1955 case 'f' : case 'F' :
1956 slub_debug |= SLAB_DEBUG_FREE;
1957 break;
1958 case 'z' : case 'Z' :
1959 slub_debug |= SLAB_RED_ZONE;
1960 break;
1961 case 'p' : case 'P' :
1962 slub_debug |= SLAB_POISON;
1963 break;
1964 case 'u' : case 'U' :
1965 slub_debug |= SLAB_STORE_USER;
1966 break;
1967 case 't' : case 'T' :
1968 slub_debug |= SLAB_TRACE;
1969 break;
1970 default:
1971 printk(KERN_ERR "slub_debug option '%c' "
1972 "unknown. skipped\n",*str);
1973 }
1974 }
1975
1976 if (*str == ',')
1977 slub_debug_slabs = str + 1;
1978 return 1;
1979}
1980
1981__setup("slub_debug", setup_slub_debug);
1982
1983static struct kmem_cache *create_kmalloc_cache(struct kmem_cache *s,
1984 const char *name, int size, gfp_t gfp_flags)
1985{
1986 unsigned int flags = 0;
1987
1988 if (gfp_flags & SLUB_DMA)
1989 flags = SLAB_CACHE_DMA;
1990
1991 down_write(&slub_lock);
1992 if (!kmem_cache_open(s, gfp_flags, name, size, ARCH_KMALLOC_MINALIGN,
1993 flags, NULL, NULL))
1994 goto panic;
1995
1996 list_add(&s->list, &slab_caches);
1997 up_write(&slub_lock);
1998 if (sysfs_slab_add(s))
1999 goto panic;
2000 return s;
2001
2002panic:
2003 panic("Creation of kmalloc slab %s size=%d failed.\n", name, size);
2004}
2005
2006static struct kmem_cache *get_slab(size_t size, gfp_t flags)
2007{
2008 int index = kmalloc_index(size);
2009
614410d5 2010 if (!index)
81819f0f
CL
2011 return NULL;
2012
2013 /* Allocation too large? */
2014 BUG_ON(index < 0);
2015
2016#ifdef CONFIG_ZONE_DMA
2017 if ((flags & SLUB_DMA)) {
2018 struct kmem_cache *s;
2019 struct kmem_cache *x;
2020 char *text;
2021 size_t realsize;
2022
2023 s = kmalloc_caches_dma[index];
2024 if (s)
2025 return s;
2026
2027 /* Dynamically create dma cache */
2028 x = kmalloc(kmem_size, flags & ~SLUB_DMA);
2029 if (!x)
2030 panic("Unable to allocate memory for dma cache\n");
2031
2032 if (index <= KMALLOC_SHIFT_HIGH)
2033 realsize = 1 << index;
2034 else {
2035 if (index == 1)
2036 realsize = 96;
2037 else
2038 realsize = 192;
2039 }
2040
2041 text = kasprintf(flags & ~SLUB_DMA, "kmalloc_dma-%d",
2042 (unsigned int)realsize);
2043 s = create_kmalloc_cache(x, text, realsize, flags);
2044 kmalloc_caches_dma[index] = s;
2045 return s;
2046 }
2047#endif
2048 return &kmalloc_caches[index];
2049}
2050
2051void *__kmalloc(size_t size, gfp_t flags)
2052{
2053 struct kmem_cache *s = get_slab(size, flags);
2054
2055 if (s)
77c5e2d0 2056 return slab_alloc(s, flags, -1, __builtin_return_address(0));
81819f0f
CL
2057 return NULL;
2058}
2059EXPORT_SYMBOL(__kmalloc);
2060
2061#ifdef CONFIG_NUMA
2062void *__kmalloc_node(size_t size, gfp_t flags, int node)
2063{
2064 struct kmem_cache *s = get_slab(size, flags);
2065
2066 if (s)
77c5e2d0 2067 return slab_alloc(s, flags, node, __builtin_return_address(0));
81819f0f
CL
2068 return NULL;
2069}
2070EXPORT_SYMBOL(__kmalloc_node);
2071#endif
2072
2073size_t ksize(const void *object)
2074{
2075 struct page *page = get_object_page(object);
2076 struct kmem_cache *s;
2077
2078 BUG_ON(!page);
2079 s = page->slab;
2080 BUG_ON(!s);
2081
2082 /*
2083 * Debugging requires use of the padding between object
2084 * and whatever may come after it.
2085 */
2086 if (s->flags & (SLAB_RED_ZONE | SLAB_POISON))
2087 return s->objsize;
2088
2089 /*
2090 * If we have the need to store the freelist pointer
2091 * back there or track user information then we can
2092 * only use the space before that information.
2093 */
2094 if (s->flags & (SLAB_DESTROY_BY_RCU | SLAB_STORE_USER))
2095 return s->inuse;
2096
2097 /*
2098 * Else we can use all the padding etc for the allocation
2099 */
2100 return s->size;
2101}
2102EXPORT_SYMBOL(ksize);
2103
2104void kfree(const void *x)
2105{
2106 struct kmem_cache *s;
2107 struct page *page;
2108
2109 if (!x)
2110 return;
2111
b49af68f 2112 page = virt_to_head_page(x);
81819f0f
CL
2113 s = page->slab;
2114
77c5e2d0 2115 slab_free(s, page, (void *)x, __builtin_return_address(0));
81819f0f
CL
2116}
2117EXPORT_SYMBOL(kfree);
2118
2086d26a
CL
2119/*
2120 * kmem_cache_shrink removes empty slabs from the partial lists
2121 * and then sorts the partially allocated slabs by the number
2122 * of items in use. The slabs with the most items in use
2123 * come first. New allocations will remove these from the
2124 * partial list because they are full. The slabs with the
2125 * least items are placed last. If it happens that the objects
2126 * are freed then the page can be returned to the page allocator.
2127 */
2128int kmem_cache_shrink(struct kmem_cache *s)
2129{
2130 int node;
2131 int i;
2132 struct kmem_cache_node *n;
2133 struct page *page;
2134 struct page *t;
2135 struct list_head *slabs_by_inuse =
2136 kmalloc(sizeof(struct list_head) * s->objects, GFP_KERNEL);
2137 unsigned long flags;
2138
2139 if (!slabs_by_inuse)
2140 return -ENOMEM;
2141
2142 flush_all(s);
2143 for_each_online_node(node) {
2144 n = get_node(s, node);
2145
2146 if (!n->nr_partial)
2147 continue;
2148
2149 for (i = 0; i < s->objects; i++)
2150 INIT_LIST_HEAD(slabs_by_inuse + i);
2151
2152 spin_lock_irqsave(&n->list_lock, flags);
2153
2154 /*
2155 * Build lists indexed by the items in use in
2156 * each slab or free slabs if empty.
2157 *
2158 * Note that concurrent frees may occur while
2159 * we hold the list_lock. page->inuse here is
2160 * the upper limit.
2161 */
2162 list_for_each_entry_safe(page, t, &n->partial, lru) {
2163 if (!page->inuse && slab_trylock(page)) {
2164 /*
2165 * Must hold slab lock here because slab_free
2166 * may have freed the last object and be
2167 * waiting to release the slab.
2168 */
2169 list_del(&page->lru);
2170 n->nr_partial--;
2171 slab_unlock(page);
2172 discard_slab(s, page);
2173 } else {
2174 if (n->nr_partial > MAX_PARTIAL)
2175 list_move(&page->lru,
2176 slabs_by_inuse + page->inuse);
2177 }
2178 }
2179
2180 if (n->nr_partial <= MAX_PARTIAL)
2181 goto out;
2182
2183 /*
2184 * Rebuild the partial list with the slabs filled up
2185 * most first and the least used slabs at the end.
2186 */
2187 for (i = s->objects - 1; i >= 0; i--)
2188 list_splice(slabs_by_inuse + i, n->partial.prev);
2189
2190 out:
2191 spin_unlock_irqrestore(&n->list_lock, flags);
2192 }
2193
2194 kfree(slabs_by_inuse);
2195 return 0;
2196}
2197EXPORT_SYMBOL(kmem_cache_shrink);
2198
81819f0f
CL
2199/**
2200 * krealloc - reallocate memory. The contents will remain unchanged.
2201 *
2202 * @p: object to reallocate memory for.
2203 * @new_size: how many bytes of memory are required.
2204 * @flags: the type of memory to allocate.
2205 *
2206 * The contents of the object pointed to are preserved up to the
2207 * lesser of the new and old sizes. If @p is %NULL, krealloc()
2208 * behaves exactly like kmalloc(). If @size is 0 and @p is not a
2209 * %NULL pointer, the object pointed to is freed.
2210 */
2211void *krealloc(const void *p, size_t new_size, gfp_t flags)
2212{
2213 struct kmem_cache *new_cache;
2214 void *ret;
2215 struct page *page;
2216
2217 if (unlikely(!p))
2218 return kmalloc(new_size, flags);
2219
2220 if (unlikely(!new_size)) {
2221 kfree(p);
2222 return NULL;
2223 }
2224
b49af68f 2225 page = virt_to_head_page(p);
81819f0f
CL
2226
2227 new_cache = get_slab(new_size, flags);
2228
2229 /*
2230 * If new size fits in the current cache, bail out.
2231 */
2232 if (likely(page->slab == new_cache))
2233 return (void *)p;
2234
2235 ret = kmalloc(new_size, flags);
2236 if (ret) {
2237 memcpy(ret, p, min(new_size, ksize(p)));
2238 kfree(p);
2239 }
2240 return ret;
2241}
2242EXPORT_SYMBOL(krealloc);
2243
2244/********************************************************************
2245 * Basic setup of slabs
2246 *******************************************************************/
2247
2248void __init kmem_cache_init(void)
2249{
2250 int i;
2251
2252#ifdef CONFIG_NUMA
2253 /*
2254 * Must first have the slab cache available for the allocations of the
2255 * struct kmalloc_cache_node's. There is special bootstrap code in
2256 * kmem_cache_open for slab_state == DOWN.
2257 */
2258 create_kmalloc_cache(&kmalloc_caches[0], "kmem_cache_node",
2259 sizeof(struct kmem_cache_node), GFP_KERNEL);
2260#endif
2261
2262 /* Able to allocate the per node structures */
2263 slab_state = PARTIAL;
2264
2265 /* Caches that are not of the two-to-the-power-of size */
2266 create_kmalloc_cache(&kmalloc_caches[1],
2267 "kmalloc-96", 96, GFP_KERNEL);
2268 create_kmalloc_cache(&kmalloc_caches[2],
2269 "kmalloc-192", 192, GFP_KERNEL);
2270
2271 for (i = KMALLOC_SHIFT_LOW; i <= KMALLOC_SHIFT_HIGH; i++)
2272 create_kmalloc_cache(&kmalloc_caches[i],
2273 "kmalloc", 1 << i, GFP_KERNEL);
2274
2275 slab_state = UP;
2276
2277 /* Provide the correct kmalloc names now that the caches are up */
2278 for (i = KMALLOC_SHIFT_LOW; i <= KMALLOC_SHIFT_HIGH; i++)
2279 kmalloc_caches[i]. name =
2280 kasprintf(GFP_KERNEL, "kmalloc-%d", 1 << i);
2281
2282#ifdef CONFIG_SMP
2283 register_cpu_notifier(&slab_notifier);
2284#endif
2285
2286 if (nr_cpu_ids) /* Remove when nr_cpu_ids is fixed upstream ! */
2287 kmem_size = offsetof(struct kmem_cache, cpu_slab)
2288 + nr_cpu_ids * sizeof(struct page *);
2289
2290 printk(KERN_INFO "SLUB: Genslabs=%d, HWalign=%d, Order=%d-%d, MinObjects=%d,"
2291 " Processors=%d, Nodes=%d\n",
2292 KMALLOC_SHIFT_HIGH, L1_CACHE_BYTES,
2293 slub_min_order, slub_max_order, slub_min_objects,
2294 nr_cpu_ids, nr_node_ids);
2295}
2296
2297/*
2298 * Find a mergeable slab cache
2299 */
2300static int slab_unmergeable(struct kmem_cache *s)
2301{
2302 if (slub_nomerge || (s->flags & SLUB_NEVER_MERGE))
2303 return 1;
2304
2305 if (s->ctor || s->dtor)
2306 return 1;
2307
2308 return 0;
2309}
2310
2311static struct kmem_cache *find_mergeable(size_t size,
2312 size_t align, unsigned long flags,
2313 void (*ctor)(void *, struct kmem_cache *, unsigned long),
2314 void (*dtor)(void *, struct kmem_cache *, unsigned long))
2315{
2316 struct list_head *h;
2317
2318 if (slub_nomerge || (flags & SLUB_NEVER_MERGE))
2319 return NULL;
2320
2321 if (ctor || dtor)
2322 return NULL;
2323
2324 size = ALIGN(size, sizeof(void *));
2325 align = calculate_alignment(flags, align, size);
2326 size = ALIGN(size, align);
2327
2328 list_for_each(h, &slab_caches) {
2329 struct kmem_cache *s =
2330 container_of(h, struct kmem_cache, list);
2331
2332 if (slab_unmergeable(s))
2333 continue;
2334
2335 if (size > s->size)
2336 continue;
2337
2338 if (((flags | slub_debug) & SLUB_MERGE_SAME) !=
2339 (s->flags & SLUB_MERGE_SAME))
2340 continue;
2341 /*
2342 * Check if alignment is compatible.
2343 * Courtesy of Adrian Drzewiecki
2344 */
2345 if ((s->size & ~(align -1)) != s->size)
2346 continue;
2347
2348 if (s->size - size >= sizeof(void *))
2349 continue;
2350
2351 return s;
2352 }
2353 return NULL;
2354}
2355
2356struct kmem_cache *kmem_cache_create(const char *name, size_t size,
2357 size_t align, unsigned long flags,
2358 void (*ctor)(void *, struct kmem_cache *, unsigned long),
2359 void (*dtor)(void *, struct kmem_cache *, unsigned long))
2360{
2361 struct kmem_cache *s;
2362
2363 down_write(&slub_lock);
2364 s = find_mergeable(size, align, flags, dtor, ctor);
2365 if (s) {
2366 s->refcount++;
2367 /*
2368 * Adjust the object sizes so that we clear
2369 * the complete object on kzalloc.
2370 */
2371 s->objsize = max(s->objsize, (int)size);
2372 s->inuse = max_t(int, s->inuse, ALIGN(size, sizeof(void *)));
2373 if (sysfs_slab_alias(s, name))
2374 goto err;
2375 } else {
2376 s = kmalloc(kmem_size, GFP_KERNEL);
2377 if (s && kmem_cache_open(s, GFP_KERNEL, name,
2378 size, align, flags, ctor, dtor)) {
2379 if (sysfs_slab_add(s)) {
2380 kfree(s);
2381 goto err;
2382 }
2383 list_add(&s->list, &slab_caches);
2384 } else
2385 kfree(s);
2386 }
2387 up_write(&slub_lock);
2388 return s;
2389
2390err:
2391 up_write(&slub_lock);
2392 if (flags & SLAB_PANIC)
2393 panic("Cannot create slabcache %s\n", name);
2394 else
2395 s = NULL;
2396 return s;
2397}
2398EXPORT_SYMBOL(kmem_cache_create);
2399
2400void *kmem_cache_zalloc(struct kmem_cache *s, gfp_t flags)
2401{
2402 void *x;
2403
77c5e2d0 2404 x = slab_alloc(s, flags, -1, __builtin_return_address(0));
81819f0f
CL
2405 if (x)
2406 memset(x, 0, s->objsize);
2407 return x;
2408}
2409EXPORT_SYMBOL(kmem_cache_zalloc);
2410
2411#ifdef CONFIG_SMP
2412static void for_all_slabs(void (*func)(struct kmem_cache *, int), int cpu)
2413{
2414 struct list_head *h;
2415
2416 down_read(&slub_lock);
2417 list_for_each(h, &slab_caches) {
2418 struct kmem_cache *s =
2419 container_of(h, struct kmem_cache, list);
2420
2421 func(s, cpu);
2422 }
2423 up_read(&slub_lock);
2424}
2425
2426/*
2427 * Use the cpu notifier to insure that the slab are flushed
2428 * when necessary.
2429 */
2430static int __cpuinit slab_cpuup_callback(struct notifier_block *nfb,
2431 unsigned long action, void *hcpu)
2432{
2433 long cpu = (long)hcpu;
2434
2435 switch (action) {
2436 case CPU_UP_CANCELED:
2437 case CPU_DEAD:
2438 for_all_slabs(__flush_cpu_slab, cpu);
2439 break;
2440 default:
2441 break;
2442 }
2443 return NOTIFY_OK;
2444}
2445
2446static struct notifier_block __cpuinitdata slab_notifier =
2447 { &slab_cpuup_callback, NULL, 0 };
2448
2449#endif
2450
81819f0f
CL
2451#ifdef CONFIG_NUMA
2452
2453/*****************************************************************
2454 * Generic reaper used to support the page allocator
2455 * (the cpu slabs are reaped by a per slab workqueue).
2456 *
2457 * Maybe move this to the page allocator?
2458 ****************************************************************/
2459
2460static DEFINE_PER_CPU(unsigned long, reap_node);
2461
2462static void init_reap_node(int cpu)
2463{
2464 int node;
2465
2466 node = next_node(cpu_to_node(cpu), node_online_map);
2467 if (node == MAX_NUMNODES)
2468 node = first_node(node_online_map);
2469
2470 __get_cpu_var(reap_node) = node;
2471}
2472
2473static void next_reap_node(void)
2474{
2475 int node = __get_cpu_var(reap_node);
2476
2477 /*
2478 * Also drain per cpu pages on remote zones
2479 */
2480 if (node != numa_node_id())
2481 drain_node_pages(node);
2482
2483 node = next_node(node, node_online_map);
2484 if (unlikely(node >= MAX_NUMNODES))
2485 node = first_node(node_online_map);
2486 __get_cpu_var(reap_node) = node;
2487}
2488#else
2489#define init_reap_node(cpu) do { } while (0)
2490#define next_reap_node(void) do { } while (0)
2491#endif
2492
2493#define REAPTIMEOUT_CPUC (2*HZ)
2494
2495#ifdef CONFIG_SMP
2496static DEFINE_PER_CPU(struct delayed_work, reap_work);
2497
2498static void cache_reap(struct work_struct *unused)
2499{
2500 next_reap_node();
2501 refresh_cpu_vm_stats(smp_processor_id());
2502 schedule_delayed_work(&__get_cpu_var(reap_work),
2503 REAPTIMEOUT_CPUC);
2504}
2505
2506static void __devinit start_cpu_timer(int cpu)
2507{
2508 struct delayed_work *reap_work = &per_cpu(reap_work, cpu);
2509
2510 /*
2511 * When this gets called from do_initcalls via cpucache_init(),
2512 * init_workqueues() has already run, so keventd will be setup
2513 * at that time.
2514 */
2515 if (keventd_up() && reap_work->work.func == NULL) {
2516 init_reap_node(cpu);
2517 INIT_DELAYED_WORK(reap_work, cache_reap);
2518 schedule_delayed_work_on(cpu, reap_work, HZ + 3 * cpu);
2519 }
2520}
2521
2522static int __init cpucache_init(void)
2523{
2524 int cpu;
2525
2526 /*
2527 * Register the timers that drain pcp pages and update vm statistics
2528 */
2529 for_each_online_cpu(cpu)
2530 start_cpu_timer(cpu);
2531 return 0;
2532}
2533__initcall(cpucache_init);
2534#endif
2535
2536#ifdef SLUB_RESILIENCY_TEST
2537static unsigned long validate_slab_cache(struct kmem_cache *s);
2538
2539static void resiliency_test(void)
2540{
2541 u8 *p;
2542
2543 printk(KERN_ERR "SLUB resiliency testing\n");
2544 printk(KERN_ERR "-----------------------\n");
2545 printk(KERN_ERR "A. Corruption after allocation\n");
2546
2547 p = kzalloc(16, GFP_KERNEL);
2548 p[16] = 0x12;
2549 printk(KERN_ERR "\n1. kmalloc-16: Clobber Redzone/next pointer"
2550 " 0x12->0x%p\n\n", p + 16);
2551
2552 validate_slab_cache(kmalloc_caches + 4);
2553
2554 /* Hmmm... The next two are dangerous */
2555 p = kzalloc(32, GFP_KERNEL);
2556 p[32 + sizeof(void *)] = 0x34;
2557 printk(KERN_ERR "\n2. kmalloc-32: Clobber next pointer/next slab"
2558 " 0x34 -> -0x%p\n", p);
2559 printk(KERN_ERR "If allocated object is overwritten then not detectable\n\n");
2560
2561 validate_slab_cache(kmalloc_caches + 5);
2562 p = kzalloc(64, GFP_KERNEL);
2563 p += 64 + (get_cycles() & 0xff) * sizeof(void *);
2564 *p = 0x56;
2565 printk(KERN_ERR "\n3. kmalloc-64: corrupting random byte 0x56->0x%p\n",
2566 p);
2567 printk(KERN_ERR "If allocated object is overwritten then not detectable\n\n");
2568 validate_slab_cache(kmalloc_caches + 6);
2569
2570 printk(KERN_ERR "\nB. Corruption after free\n");
2571 p = kzalloc(128, GFP_KERNEL);
2572 kfree(p);
2573 *p = 0x78;
2574 printk(KERN_ERR "1. kmalloc-128: Clobber first word 0x78->0x%p\n\n", p);
2575 validate_slab_cache(kmalloc_caches + 7);
2576
2577 p = kzalloc(256, GFP_KERNEL);
2578 kfree(p);
2579 p[50] = 0x9a;
2580 printk(KERN_ERR "\n2. kmalloc-256: Clobber 50th byte 0x9a->0x%p\n\n", p);
2581 validate_slab_cache(kmalloc_caches + 8);
2582
2583 p = kzalloc(512, GFP_KERNEL);
2584 kfree(p);
2585 p[512] = 0xab;
2586 printk(KERN_ERR "\n3. kmalloc-512: Clobber redzone 0xab->0x%p\n\n", p);
2587 validate_slab_cache(kmalloc_caches + 9);
2588}
2589#else
2590static void resiliency_test(void) {};
2591#endif
2592
2593/*
2594 * These are not as efficient as kmalloc for the non debug case.
2595 * We do not have the page struct available so we have to touch one
2596 * cacheline in struct kmem_cache to check slab flags.
2597 */
2598void *__kmalloc_track_caller(size_t size, gfp_t gfpflags, void *caller)
2599{
2600 struct kmem_cache *s = get_slab(size, gfpflags);
81819f0f
CL
2601
2602 if (!s)
2603 return NULL;
2604
77c5e2d0 2605 return slab_alloc(s, gfpflags, -1, caller);
81819f0f
CL
2606}
2607
2608void *__kmalloc_node_track_caller(size_t size, gfp_t gfpflags,
2609 int node, void *caller)
2610{
2611 struct kmem_cache *s = get_slab(size, gfpflags);
81819f0f
CL
2612
2613 if (!s)
2614 return NULL;
2615
77c5e2d0 2616 return slab_alloc(s, gfpflags, node, caller);
81819f0f
CL
2617}
2618
2619#ifdef CONFIG_SYSFS
2620
53e15af0
CL
2621static int validate_slab(struct kmem_cache *s, struct page *page)
2622{
2623 void *p;
2624 void *addr = page_address(page);
2625 unsigned long map[BITS_TO_LONGS(s->objects)];
2626
2627 if (!check_slab(s, page) ||
2628 !on_freelist(s, page, NULL))
2629 return 0;
2630
2631 /* Now we know that a valid freelist exists */
2632 bitmap_zero(map, s->objects);
2633
2634 for(p = page->freelist; p; p = get_freepointer(s, p)) {
2635 set_bit((p - addr) / s->size, map);
2636 if (!check_object(s, page, p, 0))
2637 return 0;
2638 }
2639
2640 for(p = addr; p < addr + s->objects * s->size; p += s->size)
2641 if (!test_bit((p - addr) / s->size, map))
2642 if (!check_object(s, page, p, 1))
2643 return 0;
2644 return 1;
2645}
2646
2647static void validate_slab_slab(struct kmem_cache *s, struct page *page)
2648{
2649 if (slab_trylock(page)) {
2650 validate_slab(s, page);
2651 slab_unlock(page);
2652 } else
2653 printk(KERN_INFO "SLUB %s: Skipped busy slab 0x%p\n",
2654 s->name, page);
2655
2656 if (s->flags & DEBUG_DEFAULT_FLAGS) {
2657 if (!PageError(page))
2658 printk(KERN_ERR "SLUB %s: PageError not set "
2659 "on slab 0x%p\n", s->name, page);
2660 } else {
2661 if (PageError(page))
2662 printk(KERN_ERR "SLUB %s: PageError set on "
2663 "slab 0x%p\n", s->name, page);
2664 }
2665}
2666
2667static int validate_slab_node(struct kmem_cache *s, struct kmem_cache_node *n)
2668{
2669 unsigned long count = 0;
2670 struct page *page;
2671 unsigned long flags;
2672
2673 spin_lock_irqsave(&n->list_lock, flags);
2674
2675 list_for_each_entry(page, &n->partial, lru) {
2676 validate_slab_slab(s, page);
2677 count++;
2678 }
2679 if (count != n->nr_partial)
2680 printk(KERN_ERR "SLUB %s: %ld partial slabs counted but "
2681 "counter=%ld\n", s->name, count, n->nr_partial);
2682
2683 if (!(s->flags & SLAB_STORE_USER))
2684 goto out;
2685
2686 list_for_each_entry(page, &n->full, lru) {
2687 validate_slab_slab(s, page);
2688 count++;
2689 }
2690 if (count != atomic_long_read(&n->nr_slabs))
2691 printk(KERN_ERR "SLUB: %s %ld slabs counted but "
2692 "counter=%ld\n", s->name, count,
2693 atomic_long_read(&n->nr_slabs));
2694
2695out:
2696 spin_unlock_irqrestore(&n->list_lock, flags);
2697 return count;
2698}
2699
2700static unsigned long validate_slab_cache(struct kmem_cache *s)
2701{
2702 int node;
2703 unsigned long count = 0;
2704
2705 flush_all(s);
2706 for_each_online_node(node) {
2707 struct kmem_cache_node *n = get_node(s, node);
2708
2709 count += validate_slab_node(s, n);
2710 }
2711 return count;
2712}
2713
88a420e4
CL
2714/*
2715 * Generate lists of locations where slabcache objects are allocated
2716 * and freed.
2717 */
2718
2719struct location {
2720 unsigned long count;
2721 void *addr;
2722};
2723
2724struct loc_track {
2725 unsigned long max;
2726 unsigned long count;
2727 struct location *loc;
2728};
2729
2730static void free_loc_track(struct loc_track *t)
2731{
2732 if (t->max)
2733 free_pages((unsigned long)t->loc,
2734 get_order(sizeof(struct location) * t->max));
2735}
2736
2737static int alloc_loc_track(struct loc_track *t, unsigned long max)
2738{
2739 struct location *l;
2740 int order;
2741
2742 if (!max)
2743 max = PAGE_SIZE / sizeof(struct location);
2744
2745 order = get_order(sizeof(struct location) * max);
2746
2747 l = (void *)__get_free_pages(GFP_KERNEL, order);
2748
2749 if (!l)
2750 return 0;
2751
2752 if (t->count) {
2753 memcpy(l, t->loc, sizeof(struct location) * t->count);
2754 free_loc_track(t);
2755 }
2756 t->max = max;
2757 t->loc = l;
2758 return 1;
2759}
2760
2761static int add_location(struct loc_track *t, struct kmem_cache *s,
2762 void *addr)
2763{
2764 long start, end, pos;
2765 struct location *l;
2766 void *caddr;
2767
2768 start = -1;
2769 end = t->count;
2770
2771 for ( ; ; ) {
2772 pos = start + (end - start + 1) / 2;
2773
2774 /*
2775 * There is nothing at "end". If we end up there
2776 * we need to add something to before end.
2777 */
2778 if (pos == end)
2779 break;
2780
2781 caddr = t->loc[pos].addr;
2782 if (addr == caddr) {
2783 t->loc[pos].count++;
2784 return 1;
2785 }
2786
2787 if (addr < caddr)
2788 end = pos;
2789 else
2790 start = pos;
2791 }
2792
2793 /*
2794 * Not found. Insert new tracking element
2795 */
2796 if (t->count >= t->max && !alloc_loc_track(t, 2 * t->max))
2797 return 0;
2798
2799 l = t->loc + pos;
2800 if (pos < t->count)
2801 memmove(l + 1, l,
2802 (t->count - pos) * sizeof(struct location));
2803 t->count++;
2804 l->count = 1;
2805 l->addr = addr;
2806 return 1;
2807}
2808
2809static void process_slab(struct loc_track *t, struct kmem_cache *s,
2810 struct page *page, enum track_item alloc)
2811{
2812 void *addr = page_address(page);
2813 unsigned long map[BITS_TO_LONGS(s->objects)];
2814 void *p;
2815
2816 bitmap_zero(map, s->objects);
2817 for (p = page->freelist; p; p = get_freepointer(s, p))
2818 set_bit((p - addr) / s->size, map);
2819
2820 for (p = addr; p < addr + s->objects * s->size; p += s->size)
2821 if (!test_bit((p - addr) / s->size, map)) {
2822 void *addr = get_track(s, p, alloc)->addr;
2823
2824 add_location(t, s, addr);
2825 }
2826}
2827
2828static int list_locations(struct kmem_cache *s, char *buf,
2829 enum track_item alloc)
2830{
2831 int n = 0;
2832 unsigned long i;
2833 struct loc_track t;
2834 int node;
2835
2836 t.count = 0;
2837 t.max = 0;
2838
2839 /* Push back cpu slabs */
2840 flush_all(s);
2841
2842 for_each_online_node(node) {
2843 struct kmem_cache_node *n = get_node(s, node);
2844 unsigned long flags;
2845 struct page *page;
2846
2847 if (!atomic_read(&n->nr_slabs))
2848 continue;
2849
2850 spin_lock_irqsave(&n->list_lock, flags);
2851 list_for_each_entry(page, &n->partial, lru)
2852 process_slab(&t, s, page, alloc);
2853 list_for_each_entry(page, &n->full, lru)
2854 process_slab(&t, s, page, alloc);
2855 spin_unlock_irqrestore(&n->list_lock, flags);
2856 }
2857
2858 for (i = 0; i < t.count; i++) {
2859 void *addr = t.loc[i].addr;
2860
2861 if (n > PAGE_SIZE - 100)
2862 break;
2863 n += sprintf(buf + n, "%7ld ", t.loc[i].count);
2864 if (addr)
2865 n += sprint_symbol(buf + n, (unsigned long)t.loc[i].addr);
2866 else
2867 n += sprintf(buf + n, "<not-available>");
2868 n += sprintf(buf + n, "\n");
2869 }
2870
2871 free_loc_track(&t);
2872 if (!t.count)
2873 n += sprintf(buf, "No data\n");
2874 return n;
2875}
2876
81819f0f
CL
2877static unsigned long count_partial(struct kmem_cache_node *n)
2878{
2879 unsigned long flags;
2880 unsigned long x = 0;
2881 struct page *page;
2882
2883 spin_lock_irqsave(&n->list_lock, flags);
2884 list_for_each_entry(page, &n->partial, lru)
2885 x += page->inuse;
2886 spin_unlock_irqrestore(&n->list_lock, flags);
2887 return x;
2888}
2889
2890enum slab_stat_type {
2891 SL_FULL,
2892 SL_PARTIAL,
2893 SL_CPU,
2894 SL_OBJECTS
2895};
2896
2897#define SO_FULL (1 << SL_FULL)
2898#define SO_PARTIAL (1 << SL_PARTIAL)
2899#define SO_CPU (1 << SL_CPU)
2900#define SO_OBJECTS (1 << SL_OBJECTS)
2901
2902static unsigned long slab_objects(struct kmem_cache *s,
2903 char *buf, unsigned long flags)
2904{
2905 unsigned long total = 0;
2906 int cpu;
2907 int node;
2908 int x;
2909 unsigned long *nodes;
2910 unsigned long *per_cpu;
2911
2912 nodes = kzalloc(2 * sizeof(unsigned long) * nr_node_ids, GFP_KERNEL);
2913 per_cpu = nodes + nr_node_ids;
2914
2915 for_each_possible_cpu(cpu) {
2916 struct page *page = s->cpu_slab[cpu];
2917 int node;
2918
2919 if (page) {
2920 node = page_to_nid(page);
2921 if (flags & SO_CPU) {
2922 int x = 0;
2923
2924 if (flags & SO_OBJECTS)
2925 x = page->inuse;
2926 else
2927 x = 1;
2928 total += x;
2929 nodes[node] += x;
2930 }
2931 per_cpu[node]++;
2932 }
2933 }
2934
2935 for_each_online_node(node) {
2936 struct kmem_cache_node *n = get_node(s, node);
2937
2938 if (flags & SO_PARTIAL) {
2939 if (flags & SO_OBJECTS)
2940 x = count_partial(n);
2941 else
2942 x = n->nr_partial;
2943 total += x;
2944 nodes[node] += x;
2945 }
2946
2947 if (flags & SO_FULL) {
2948 int full_slabs = atomic_read(&n->nr_slabs)
2949 - per_cpu[node]
2950 - n->nr_partial;
2951
2952 if (flags & SO_OBJECTS)
2953 x = full_slabs * s->objects;
2954 else
2955 x = full_slabs;
2956 total += x;
2957 nodes[node] += x;
2958 }
2959 }
2960
2961 x = sprintf(buf, "%lu", total);
2962#ifdef CONFIG_NUMA
2963 for_each_online_node(node)
2964 if (nodes[node])
2965 x += sprintf(buf + x, " N%d=%lu",
2966 node, nodes[node]);
2967#endif
2968 kfree(nodes);
2969 return x + sprintf(buf + x, "\n");
2970}
2971
2972static int any_slab_objects(struct kmem_cache *s)
2973{
2974 int node;
2975 int cpu;
2976
2977 for_each_possible_cpu(cpu)
2978 if (s->cpu_slab[cpu])
2979 return 1;
2980
2981 for_each_node(node) {
2982 struct kmem_cache_node *n = get_node(s, node);
2983
2984 if (n->nr_partial || atomic_read(&n->nr_slabs))
2985 return 1;
2986 }
2987 return 0;
2988}
2989
2990#define to_slab_attr(n) container_of(n, struct slab_attribute, attr)
2991#define to_slab(n) container_of(n, struct kmem_cache, kobj);
2992
2993struct slab_attribute {
2994 struct attribute attr;
2995 ssize_t (*show)(struct kmem_cache *s, char *buf);
2996 ssize_t (*store)(struct kmem_cache *s, const char *x, size_t count);
2997};
2998
2999#define SLAB_ATTR_RO(_name) \
3000 static struct slab_attribute _name##_attr = __ATTR_RO(_name)
3001
3002#define SLAB_ATTR(_name) \
3003 static struct slab_attribute _name##_attr = \
3004 __ATTR(_name, 0644, _name##_show, _name##_store)
3005
81819f0f
CL
3006static ssize_t slab_size_show(struct kmem_cache *s, char *buf)
3007{
3008 return sprintf(buf, "%d\n", s->size);
3009}
3010SLAB_ATTR_RO(slab_size);
3011
3012static ssize_t align_show(struct kmem_cache *s, char *buf)
3013{
3014 return sprintf(buf, "%d\n", s->align);
3015}
3016SLAB_ATTR_RO(align);
3017
3018static ssize_t object_size_show(struct kmem_cache *s, char *buf)
3019{
3020 return sprintf(buf, "%d\n", s->objsize);
3021}
3022SLAB_ATTR_RO(object_size);
3023
3024static ssize_t objs_per_slab_show(struct kmem_cache *s, char *buf)
3025{
3026 return sprintf(buf, "%d\n", s->objects);
3027}
3028SLAB_ATTR_RO(objs_per_slab);
3029
3030static ssize_t order_show(struct kmem_cache *s, char *buf)
3031{
3032 return sprintf(buf, "%d\n", s->order);
3033}
3034SLAB_ATTR_RO(order);
3035
3036static ssize_t ctor_show(struct kmem_cache *s, char *buf)
3037{
3038 if (s->ctor) {
3039 int n = sprint_symbol(buf, (unsigned long)s->ctor);
3040
3041 return n + sprintf(buf + n, "\n");
3042 }
3043 return 0;
3044}
3045SLAB_ATTR_RO(ctor);
3046
3047static ssize_t dtor_show(struct kmem_cache *s, char *buf)
3048{
3049 if (s->dtor) {
3050 int n = sprint_symbol(buf, (unsigned long)s->dtor);
3051
3052 return n + sprintf(buf + n, "\n");
3053 }
3054 return 0;
3055}
3056SLAB_ATTR_RO(dtor);
3057
3058static ssize_t aliases_show(struct kmem_cache *s, char *buf)
3059{
3060 return sprintf(buf, "%d\n", s->refcount - 1);
3061}
3062SLAB_ATTR_RO(aliases);
3063
3064static ssize_t slabs_show(struct kmem_cache *s, char *buf)
3065{
3066 return slab_objects(s, buf, SO_FULL|SO_PARTIAL|SO_CPU);
3067}
3068SLAB_ATTR_RO(slabs);
3069
3070static ssize_t partial_show(struct kmem_cache *s, char *buf)
3071{
3072 return slab_objects(s, buf, SO_PARTIAL);
3073}
3074SLAB_ATTR_RO(partial);
3075
3076static ssize_t cpu_slabs_show(struct kmem_cache *s, char *buf)
3077{
3078 return slab_objects(s, buf, SO_CPU);
3079}
3080SLAB_ATTR_RO(cpu_slabs);
3081
3082static ssize_t objects_show(struct kmem_cache *s, char *buf)
3083{
3084 return slab_objects(s, buf, SO_FULL|SO_PARTIAL|SO_CPU|SO_OBJECTS);
3085}
3086SLAB_ATTR_RO(objects);
3087
3088static ssize_t sanity_checks_show(struct kmem_cache *s, char *buf)
3089{
3090 return sprintf(buf, "%d\n", !!(s->flags & SLAB_DEBUG_FREE));
3091}
3092
3093static ssize_t sanity_checks_store(struct kmem_cache *s,
3094 const char *buf, size_t length)
3095{
3096 s->flags &= ~SLAB_DEBUG_FREE;
3097 if (buf[0] == '1')
3098 s->flags |= SLAB_DEBUG_FREE;
3099 return length;
3100}
3101SLAB_ATTR(sanity_checks);
3102
3103static ssize_t trace_show(struct kmem_cache *s, char *buf)
3104{
3105 return sprintf(buf, "%d\n", !!(s->flags & SLAB_TRACE));
3106}
3107
3108static ssize_t trace_store(struct kmem_cache *s, const char *buf,
3109 size_t length)
3110{
3111 s->flags &= ~SLAB_TRACE;
3112 if (buf[0] == '1')
3113 s->flags |= SLAB_TRACE;
3114 return length;
3115}
3116SLAB_ATTR(trace);
3117
3118static ssize_t reclaim_account_show(struct kmem_cache *s, char *buf)
3119{
3120 return sprintf(buf, "%d\n", !!(s->flags & SLAB_RECLAIM_ACCOUNT));
3121}
3122
3123static ssize_t reclaim_account_store(struct kmem_cache *s,
3124 const char *buf, size_t length)
3125{
3126 s->flags &= ~SLAB_RECLAIM_ACCOUNT;
3127 if (buf[0] == '1')
3128 s->flags |= SLAB_RECLAIM_ACCOUNT;
3129 return length;
3130}
3131SLAB_ATTR(reclaim_account);
3132
3133static ssize_t hwcache_align_show(struct kmem_cache *s, char *buf)
3134{
5af60839 3135 return sprintf(buf, "%d\n", !!(s->flags & SLAB_HWCACHE_ALIGN));
81819f0f
CL
3136}
3137SLAB_ATTR_RO(hwcache_align);
3138
3139#ifdef CONFIG_ZONE_DMA
3140static ssize_t cache_dma_show(struct kmem_cache *s, char *buf)
3141{
3142 return sprintf(buf, "%d\n", !!(s->flags & SLAB_CACHE_DMA));
3143}
3144SLAB_ATTR_RO(cache_dma);
3145#endif
3146
3147static ssize_t destroy_by_rcu_show(struct kmem_cache *s, char *buf)
3148{
3149 return sprintf(buf, "%d\n", !!(s->flags & SLAB_DESTROY_BY_RCU));
3150}
3151SLAB_ATTR_RO(destroy_by_rcu);
3152
3153static ssize_t red_zone_show(struct kmem_cache *s, char *buf)
3154{
3155 return sprintf(buf, "%d\n", !!(s->flags & SLAB_RED_ZONE));
3156}
3157
3158static ssize_t red_zone_store(struct kmem_cache *s,
3159 const char *buf, size_t length)
3160{
3161 if (any_slab_objects(s))
3162 return -EBUSY;
3163
3164 s->flags &= ~SLAB_RED_ZONE;
3165 if (buf[0] == '1')
3166 s->flags |= SLAB_RED_ZONE;
3167 calculate_sizes(s);
3168 return length;
3169}
3170SLAB_ATTR(red_zone);
3171
3172static ssize_t poison_show(struct kmem_cache *s, char *buf)
3173{
3174 return sprintf(buf, "%d\n", !!(s->flags & SLAB_POISON));
3175}
3176
3177static ssize_t poison_store(struct kmem_cache *s,
3178 const char *buf, size_t length)
3179{
3180 if (any_slab_objects(s))
3181 return -EBUSY;
3182
3183 s->flags &= ~SLAB_POISON;
3184 if (buf[0] == '1')
3185 s->flags |= SLAB_POISON;
3186 calculate_sizes(s);
3187 return length;
3188}
3189SLAB_ATTR(poison);
3190
3191static ssize_t store_user_show(struct kmem_cache *s, char *buf)
3192{
3193 return sprintf(buf, "%d\n", !!(s->flags & SLAB_STORE_USER));
3194}
3195
3196static ssize_t store_user_store(struct kmem_cache *s,
3197 const char *buf, size_t length)
3198{
3199 if (any_slab_objects(s))
3200 return -EBUSY;
3201
3202 s->flags &= ~SLAB_STORE_USER;
3203 if (buf[0] == '1')
3204 s->flags |= SLAB_STORE_USER;
3205 calculate_sizes(s);
3206 return length;
3207}
3208SLAB_ATTR(store_user);
3209
53e15af0
CL
3210static ssize_t validate_show(struct kmem_cache *s, char *buf)
3211{
3212 return 0;
3213}
3214
3215static ssize_t validate_store(struct kmem_cache *s,
3216 const char *buf, size_t length)
3217{
3218 if (buf[0] == '1')
3219 validate_slab_cache(s);
3220 else
3221 return -EINVAL;
3222 return length;
3223}
3224SLAB_ATTR(validate);
3225
2086d26a
CL
3226static ssize_t shrink_show(struct kmem_cache *s, char *buf)
3227{
3228 return 0;
3229}
3230
3231static ssize_t shrink_store(struct kmem_cache *s,
3232 const char *buf, size_t length)
3233{
3234 if (buf[0] == '1') {
3235 int rc = kmem_cache_shrink(s);
3236
3237 if (rc)
3238 return rc;
3239 } else
3240 return -EINVAL;
3241 return length;
3242}
3243SLAB_ATTR(shrink);
3244
88a420e4
CL
3245static ssize_t alloc_calls_show(struct kmem_cache *s, char *buf)
3246{
3247 if (!(s->flags & SLAB_STORE_USER))
3248 return -ENOSYS;
3249 return list_locations(s, buf, TRACK_ALLOC);
3250}
3251SLAB_ATTR_RO(alloc_calls);
3252
3253static ssize_t free_calls_show(struct kmem_cache *s, char *buf)
3254{
3255 if (!(s->flags & SLAB_STORE_USER))
3256 return -ENOSYS;
3257 return list_locations(s, buf, TRACK_FREE);
3258}
3259SLAB_ATTR_RO(free_calls);
3260
81819f0f
CL
3261#ifdef CONFIG_NUMA
3262static ssize_t defrag_ratio_show(struct kmem_cache *s, char *buf)
3263{
3264 return sprintf(buf, "%d\n", s->defrag_ratio / 10);
3265}
3266
3267static ssize_t defrag_ratio_store(struct kmem_cache *s,
3268 const char *buf, size_t length)
3269{
3270 int n = simple_strtoul(buf, NULL, 10);
3271
3272 if (n < 100)
3273 s->defrag_ratio = n * 10;
3274 return length;
3275}
3276SLAB_ATTR(defrag_ratio);
3277#endif
3278
3279static struct attribute * slab_attrs[] = {
3280 &slab_size_attr.attr,
3281 &object_size_attr.attr,
3282 &objs_per_slab_attr.attr,
3283 &order_attr.attr,
3284 &objects_attr.attr,
3285 &slabs_attr.attr,
3286 &partial_attr.attr,
3287 &cpu_slabs_attr.attr,
3288 &ctor_attr.attr,
3289 &dtor_attr.attr,
3290 &aliases_attr.attr,
3291 &align_attr.attr,
3292 &sanity_checks_attr.attr,
3293 &trace_attr.attr,
3294 &hwcache_align_attr.attr,
3295 &reclaim_account_attr.attr,
3296 &destroy_by_rcu_attr.attr,
3297 &red_zone_attr.attr,
3298 &poison_attr.attr,
3299 &store_user_attr.attr,
53e15af0 3300 &validate_attr.attr,
2086d26a 3301 &shrink_attr.attr,
88a420e4
CL
3302 &alloc_calls_attr.attr,
3303 &free_calls_attr.attr,
81819f0f
CL
3304#ifdef CONFIG_ZONE_DMA
3305 &cache_dma_attr.attr,
3306#endif
3307#ifdef CONFIG_NUMA
3308 &defrag_ratio_attr.attr,
3309#endif
3310 NULL
3311};
3312
3313static struct attribute_group slab_attr_group = {
3314 .attrs = slab_attrs,
3315};
3316
3317static ssize_t slab_attr_show(struct kobject *kobj,
3318 struct attribute *attr,
3319 char *buf)
3320{
3321 struct slab_attribute *attribute;
3322 struct kmem_cache *s;
3323 int err;
3324
3325 attribute = to_slab_attr(attr);
3326 s = to_slab(kobj);
3327
3328 if (!attribute->show)
3329 return -EIO;
3330
3331 err = attribute->show(s, buf);
3332
3333 return err;
3334}
3335
3336static ssize_t slab_attr_store(struct kobject *kobj,
3337 struct attribute *attr,
3338 const char *buf, size_t len)
3339{
3340 struct slab_attribute *attribute;
3341 struct kmem_cache *s;
3342 int err;
3343
3344 attribute = to_slab_attr(attr);
3345 s = to_slab(kobj);
3346
3347 if (!attribute->store)
3348 return -EIO;
3349
3350 err = attribute->store(s, buf, len);
3351
3352 return err;
3353}
3354
3355static struct sysfs_ops slab_sysfs_ops = {
3356 .show = slab_attr_show,
3357 .store = slab_attr_store,
3358};
3359
3360static struct kobj_type slab_ktype = {
3361 .sysfs_ops = &slab_sysfs_ops,
3362};
3363
3364static int uevent_filter(struct kset *kset, struct kobject *kobj)
3365{
3366 struct kobj_type *ktype = get_ktype(kobj);
3367
3368 if (ktype == &slab_ktype)
3369 return 1;
3370 return 0;
3371}
3372
3373static struct kset_uevent_ops slab_uevent_ops = {
3374 .filter = uevent_filter,
3375};
3376
3377decl_subsys(slab, &slab_ktype, &slab_uevent_ops);
3378
3379#define ID_STR_LENGTH 64
3380
3381/* Create a unique string id for a slab cache:
3382 * format
3383 * :[flags-]size:[memory address of kmemcache]
3384 */
3385static char *create_unique_id(struct kmem_cache *s)
3386{
3387 char *name = kmalloc(ID_STR_LENGTH, GFP_KERNEL);
3388 char *p = name;
3389
3390 BUG_ON(!name);
3391
3392 *p++ = ':';
3393 /*
3394 * First flags affecting slabcache operations. We will only
3395 * get here for aliasable slabs so we do not need to support
3396 * too many flags. The flags here must cover all flags that
3397 * are matched during merging to guarantee that the id is
3398 * unique.
3399 */
3400 if (s->flags & SLAB_CACHE_DMA)
3401 *p++ = 'd';
3402 if (s->flags & SLAB_RECLAIM_ACCOUNT)
3403 *p++ = 'a';
3404 if (s->flags & SLAB_DEBUG_FREE)
3405 *p++ = 'F';
3406 if (p != name + 1)
3407 *p++ = '-';
3408 p += sprintf(p, "%07d", s->size);
3409 BUG_ON(p > name + ID_STR_LENGTH - 1);
3410 return name;
3411}
3412
3413static int sysfs_slab_add(struct kmem_cache *s)
3414{
3415 int err;
3416 const char *name;
3417 int unmergeable;
3418
3419 if (slab_state < SYSFS)
3420 /* Defer until later */
3421 return 0;
3422
3423 unmergeable = slab_unmergeable(s);
3424 if (unmergeable) {
3425 /*
3426 * Slabcache can never be merged so we can use the name proper.
3427 * This is typically the case for debug situations. In that
3428 * case we can catch duplicate names easily.
3429 */
3430 sysfs_remove_link(&slab_subsys.kset.kobj, s->name);
3431 name = s->name;
3432 } else {
3433 /*
3434 * Create a unique name for the slab as a target
3435 * for the symlinks.
3436 */
3437 name = create_unique_id(s);
3438 }
3439
3440 kobj_set_kset_s(s, slab_subsys);
3441 kobject_set_name(&s->kobj, name);
3442 kobject_init(&s->kobj);
3443 err = kobject_add(&s->kobj);
3444 if (err)
3445 return err;
3446
3447 err = sysfs_create_group(&s->kobj, &slab_attr_group);
3448 if (err)
3449 return err;
3450 kobject_uevent(&s->kobj, KOBJ_ADD);
3451 if (!unmergeable) {
3452 /* Setup first alias */
3453 sysfs_slab_alias(s, s->name);
3454 kfree(name);
3455 }
3456 return 0;
3457}
3458
3459static void sysfs_slab_remove(struct kmem_cache *s)
3460{
3461 kobject_uevent(&s->kobj, KOBJ_REMOVE);
3462 kobject_del(&s->kobj);
3463}
3464
3465/*
3466 * Need to buffer aliases during bootup until sysfs becomes
3467 * available lest we loose that information.
3468 */
3469struct saved_alias {
3470 struct kmem_cache *s;
3471 const char *name;
3472 struct saved_alias *next;
3473};
3474
3475struct saved_alias *alias_list;
3476
3477static int sysfs_slab_alias(struct kmem_cache *s, const char *name)
3478{
3479 struct saved_alias *al;
3480
3481 if (slab_state == SYSFS) {
3482 /*
3483 * If we have a leftover link then remove it.
3484 */
3485 sysfs_remove_link(&slab_subsys.kset.kobj, name);
3486 return sysfs_create_link(&slab_subsys.kset.kobj,
3487 &s->kobj, name);
3488 }
3489
3490 al = kmalloc(sizeof(struct saved_alias), GFP_KERNEL);
3491 if (!al)
3492 return -ENOMEM;
3493
3494 al->s = s;
3495 al->name = name;
3496 al->next = alias_list;
3497 alias_list = al;
3498 return 0;
3499}
3500
3501static int __init slab_sysfs_init(void)
3502{
3503 int err;
3504
3505 err = subsystem_register(&slab_subsys);
3506 if (err) {
3507 printk(KERN_ERR "Cannot register slab subsystem.\n");
3508 return -ENOSYS;
3509 }
3510
3511 finish_bootstrap();
3512
3513 while (alias_list) {
3514 struct saved_alias *al = alias_list;
3515
3516 alias_list = alias_list->next;
3517 err = sysfs_slab_alias(al->s, al->name);
3518 BUG_ON(err);
3519 kfree(al);
3520 }
3521
3522 resiliency_test();
3523 return 0;
3524}
3525
3526__initcall(slab_sysfs_init);
3527#else
3528__initcall(finish_bootstrap);
3529#endif