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