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