blob: ce96d485a88f9883a01f506f41154c58e4561cef [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 *
Christoph Lameter672bba32007-05-09 02:32:39 -070069 * Slabs with free elements are kept on a partial list and during regular
70 * operations no list for full slabs is used. If an object in a full slab is
Christoph Lameter81819f02007-05-06 14:49:36 -070071 * freed then the slab will show up again on the partial lists.
Christoph Lameter672bba32007-05-09 02:32:39 -070072 * We track full slabs for debugging purposes though because otherwise we
73 * cannot scan all objects.
Christoph Lameter81819f02007-05-06 14:49:36 -070074 *
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 *
Christoph Lameter4b6f0752007-05-16 22:10:53 -070081 * PageActive The slab is frozen and exempt from list processing.
82 * This means that the slab is dedicated to a purpose
83 * such as satisfying allocations for a specific
84 * processor. Objects may be freed in the slab while
85 * it is frozen but slab_free will then skip the usual
86 * list operations. It is up to the processor holding
87 * the slab to integrate the slab into the slab lists
88 * when the slab is no longer needed.
89 *
90 * One use of this flag is to mark slabs that are
91 * used for allocations. Then such a slab becomes a cpu
92 * slab. The cpu slab may be equipped with an additional
Christoph Lameter894b8782007-05-10 03:15:16 -070093 * lockless_freelist that allows lockless access to
94 * free objects in addition to the regular freelist
95 * that requires the slab lock.
Christoph Lameter81819f02007-05-06 14:49:36 -070096 *
97 * PageError Slab requires special handling due to debug
98 * options set. This moves slab handling out of
Christoph Lameter894b8782007-05-10 03:15:16 -070099 * the fast path and disables lockless freelists.
Christoph Lameter81819f02007-05-06 14:49:36 -0700100 */
101
Christoph Lameter4b6f0752007-05-16 22:10:53 -0700102static inline int SlabFrozen(struct page *page)
103{
104 return PageActive(page);
105}
106
107static inline void SetSlabFrozen(struct page *page)
108{
109 SetPageActive(page);
110}
111
112static inline void ClearSlabFrozen(struct page *page)
113{
114 ClearPageActive(page);
115}
116
Christoph Lameter35e5d7e2007-05-09 02:32:42 -0700117static inline int SlabDebug(struct page *page)
118{
Christoph Lameter41ecc552007-05-09 02:32:44 -0700119#ifdef CONFIG_SLUB_DEBUG
Christoph Lameter35e5d7e2007-05-09 02:32:42 -0700120 return PageError(page);
Christoph Lameter41ecc552007-05-09 02:32:44 -0700121#else
122 return 0;
123#endif
Christoph Lameter35e5d7e2007-05-09 02:32:42 -0700124}
125
126static inline void SetSlabDebug(struct page *page)
127{
Christoph Lameter41ecc552007-05-09 02:32:44 -0700128#ifdef CONFIG_SLUB_DEBUG
Christoph Lameter35e5d7e2007-05-09 02:32:42 -0700129 SetPageError(page);
Christoph Lameter41ecc552007-05-09 02:32:44 -0700130#endif
Christoph Lameter35e5d7e2007-05-09 02:32:42 -0700131}
132
133static inline void ClearSlabDebug(struct page *page)
134{
Christoph Lameter41ecc552007-05-09 02:32:44 -0700135#ifdef CONFIG_SLUB_DEBUG
Christoph Lameter35e5d7e2007-05-09 02:32:42 -0700136 ClearPageError(page);
Christoph Lameter41ecc552007-05-09 02:32:44 -0700137#endif
Christoph Lameter35e5d7e2007-05-09 02:32:42 -0700138}
139
Christoph Lameter81819f02007-05-06 14:49:36 -0700140/*
141 * Issues still to be resolved:
142 *
143 * - The per cpu array is updated for each new slab and and is a remote
144 * cacheline for most nodes. This could become a bouncing cacheline given
Christoph Lameter672bba32007-05-09 02:32:39 -0700145 * enough frequent updates. There are 16 pointers in a cacheline, so at
146 * max 16 cpus could compete for the cacheline which may be okay.
Christoph Lameter81819f02007-05-06 14:49:36 -0700147 *
148 * - Support PAGE_ALLOC_DEBUG. Should be easy to do.
149 *
Christoph Lameter81819f02007-05-06 14:49:36 -0700150 * - Variable sizing of the per node arrays
151 */
152
153/* Enable to test recovery from slab corruption on boot */
154#undef SLUB_RESILIENCY_TEST
155
156#if PAGE_SHIFT <= 12
157
158/*
159 * Small page size. Make sure that we do not fragment memory
160 */
161#define DEFAULT_MAX_ORDER 1
162#define DEFAULT_MIN_OBJECTS 4
163
164#else
165
166/*
167 * Large page machines are customarily able to handle larger
168 * page orders.
169 */
170#define DEFAULT_MAX_ORDER 2
171#define DEFAULT_MIN_OBJECTS 8
172
173#endif
174
175/*
Christoph Lameter2086d262007-05-06 14:49:46 -0700176 * Mininum number of partial slabs. These will be left on the partial
177 * lists even if they are empty. kmem_cache_shrink may reclaim them.
178 */
Christoph Lametere95eed52007-05-06 14:49:44 -0700179#define MIN_PARTIAL 2
180
Christoph Lameter2086d262007-05-06 14:49:46 -0700181/*
182 * Maximum number of desirable partial slabs.
183 * The existence of more partial slabs makes kmem_cache_shrink
184 * sort the partial list by the number of objects in the.
185 */
186#define MAX_PARTIAL 10
187
Christoph Lameter81819f02007-05-06 14:49:36 -0700188#define DEBUG_DEFAULT_FLAGS (SLAB_DEBUG_FREE | SLAB_RED_ZONE | \
189 SLAB_POISON | SLAB_STORE_USER)
Christoph Lameter672bba32007-05-09 02:32:39 -0700190
Christoph Lameter81819f02007-05-06 14:49:36 -0700191/*
192 * Set of flags that will prevent slab merging
193 */
194#define SLUB_NEVER_MERGE (SLAB_RED_ZONE | SLAB_POISON | SLAB_STORE_USER | \
195 SLAB_TRACE | SLAB_DESTROY_BY_RCU)
196
197#define SLUB_MERGE_SAME (SLAB_DEBUG_FREE | SLAB_RECLAIM_ACCOUNT | \
198 SLAB_CACHE_DMA)
199
200#ifndef ARCH_KMALLOC_MINALIGN
Christoph Lameter47bfdc02007-05-06 14:49:37 -0700201#define ARCH_KMALLOC_MINALIGN __alignof__(unsigned long long)
Christoph Lameter81819f02007-05-06 14:49:36 -0700202#endif
203
204#ifndef ARCH_SLAB_MINALIGN
Christoph Lameter47bfdc02007-05-06 14:49:37 -0700205#define ARCH_SLAB_MINALIGN __alignof__(unsigned long long)
Christoph Lameter81819f02007-05-06 14:49:36 -0700206#endif
207
208/* Internal SLUB flags */
209#define __OBJECT_POISON 0x80000000 /* Poison object */
210
Christoph Lameter65c02d42007-05-09 02:32:35 -0700211/* Not all arches define cache_line_size */
212#ifndef cache_line_size
213#define cache_line_size() L1_CACHE_BYTES
214#endif
215
Christoph Lameter81819f02007-05-06 14:49:36 -0700216static int kmem_size = sizeof(struct kmem_cache);
217
218#ifdef CONFIG_SMP
219static struct notifier_block slab_notifier;
220#endif
221
222static enum {
223 DOWN, /* No slab functionality available */
224 PARTIAL, /* kmem_cache_open() works but kmalloc does not */
Christoph Lameter672bba32007-05-09 02:32:39 -0700225 UP, /* Everything works but does not show up in sysfs */
Christoph Lameter81819f02007-05-06 14:49:36 -0700226 SYSFS /* Sysfs up */
227} slab_state = DOWN;
228
229/* A list of all slab caches on the system */
230static DECLARE_RWSEM(slub_lock);
231LIST_HEAD(slab_caches);
232
Christoph Lameter02cbc872007-05-09 02:32:43 -0700233/*
234 * Tracking user of a slab.
235 */
236struct track {
237 void *addr; /* Called from address */
238 int cpu; /* Was running on cpu */
239 int pid; /* Pid context */
240 unsigned long when; /* When did the operation occur */
241};
242
243enum track_item { TRACK_ALLOC, TRACK_FREE };
244
Christoph Lameter41ecc552007-05-09 02:32:44 -0700245#if defined(CONFIG_SYSFS) && defined(CONFIG_SLUB_DEBUG)
Christoph Lameter81819f02007-05-06 14:49:36 -0700246static int sysfs_slab_add(struct kmem_cache *);
247static int sysfs_slab_alias(struct kmem_cache *, const char *);
248static void sysfs_slab_remove(struct kmem_cache *);
249#else
250static int sysfs_slab_add(struct kmem_cache *s) { return 0; }
251static int sysfs_slab_alias(struct kmem_cache *s, const char *p) { return 0; }
252static void sysfs_slab_remove(struct kmem_cache *s) {}
253#endif
254
255/********************************************************************
256 * Core slab cache functions
257 *******************************************************************/
258
259int slab_is_available(void)
260{
261 return slab_state >= UP;
262}
263
264static inline struct kmem_cache_node *get_node(struct kmem_cache *s, int node)
265{
266#ifdef CONFIG_NUMA
267 return s->node[node];
268#else
269 return &s->local_node;
270#endif
271}
272
Christoph Lameter02cbc872007-05-09 02:32:43 -0700273static inline int check_valid_pointer(struct kmem_cache *s,
274 struct page *page, const void *object)
275{
276 void *base;
277
278 if (!object)
279 return 1;
280
281 base = page_address(page);
282 if (object < base || object >= base + s->objects * s->size ||
283 (object - base) % s->size) {
284 return 0;
285 }
286
287 return 1;
288}
289
Christoph Lameter81819f02007-05-06 14:49:36 -0700290/*
Christoph Lameter7656c722007-05-09 02:32:40 -0700291 * Slow version of get and set free pointer.
292 *
293 * This version requires touching the cache lines of kmem_cache which
294 * we avoid to do in the fast alloc free paths. There we obtain the offset
295 * from the page struct.
296 */
297static inline void *get_freepointer(struct kmem_cache *s, void *object)
298{
299 return *(void **)(object + s->offset);
300}
301
302static inline void set_freepointer(struct kmem_cache *s, void *object, void *fp)
303{
304 *(void **)(object + s->offset) = fp;
305}
306
307/* Loop over all objects in a slab */
308#define for_each_object(__p, __s, __addr) \
309 for (__p = (__addr); __p < (__addr) + (__s)->objects * (__s)->size;\
310 __p += (__s)->size)
311
312/* Scan freelist */
313#define for_each_free_object(__p, __s, __free) \
314 for (__p = (__free); __p; __p = get_freepointer((__s), __p))
315
316/* Determine object index from a given position */
317static inline int slab_index(void *p, struct kmem_cache *s, void *addr)
318{
319 return (p - addr) / s->size;
320}
321
Christoph Lameter41ecc552007-05-09 02:32:44 -0700322#ifdef CONFIG_SLUB_DEBUG
323/*
324 * Debug settings:
325 */
326static int slub_debug;
327
328static char *slub_debug_slabs;
329
Christoph Lameter7656c722007-05-09 02:32:40 -0700330/*
Christoph Lameter81819f02007-05-06 14:49:36 -0700331 * Object debugging
332 */
333static void print_section(char *text, u8 *addr, unsigned int length)
334{
335 int i, offset;
336 int newline = 1;
337 char ascii[17];
338
339 ascii[16] = 0;
340
341 for (i = 0; i < length; i++) {
342 if (newline) {
343 printk(KERN_ERR "%10s 0x%p: ", text, addr + i);
344 newline = 0;
345 }
346 printk(" %02x", addr[i]);
347 offset = i % 16;
348 ascii[offset] = isgraph(addr[i]) ? addr[i] : '.';
349 if (offset == 15) {
350 printk(" %s\n",ascii);
351 newline = 1;
352 }
353 }
354 if (!newline) {
355 i %= 16;
356 while (i < 16) {
357 printk(" ");
358 ascii[i] = ' ';
359 i++;
360 }
361 printk(" %s\n", ascii);
362 }
363}
364
Christoph Lameter81819f02007-05-06 14:49:36 -0700365static struct track *get_track(struct kmem_cache *s, void *object,
366 enum track_item alloc)
367{
368 struct track *p;
369
370 if (s->offset)
371 p = object + s->offset + sizeof(void *);
372 else
373 p = object + s->inuse;
374
375 return p + alloc;
376}
377
378static void set_track(struct kmem_cache *s, void *object,
379 enum track_item alloc, void *addr)
380{
381 struct track *p;
382
383 if (s->offset)
384 p = object + s->offset + sizeof(void *);
385 else
386 p = object + s->inuse;
387
388 p += alloc;
389 if (addr) {
390 p->addr = addr;
391 p->cpu = smp_processor_id();
392 p->pid = current ? current->pid : -1;
393 p->when = jiffies;
394 } else
395 memset(p, 0, sizeof(struct track));
396}
397
Christoph Lameter81819f02007-05-06 14:49:36 -0700398static void init_tracking(struct kmem_cache *s, void *object)
399{
400 if (s->flags & SLAB_STORE_USER) {
401 set_track(s, object, TRACK_FREE, NULL);
402 set_track(s, object, TRACK_ALLOC, NULL);
403 }
404}
405
406static void print_track(const char *s, struct track *t)
407{
408 if (!t->addr)
409 return;
410
411 printk(KERN_ERR "%s: ", s);
412 __print_symbol("%s", (unsigned long)t->addr);
413 printk(" jiffies_ago=%lu cpu=%u pid=%d\n", jiffies - t->when, t->cpu, t->pid);
414}
415
416static void print_trailer(struct kmem_cache *s, u8 *p)
417{
418 unsigned int off; /* Offset of last byte */
419
420 if (s->flags & SLAB_RED_ZONE)
421 print_section("Redzone", p + s->objsize,
422 s->inuse - s->objsize);
423
424 printk(KERN_ERR "FreePointer 0x%p -> 0x%p\n",
425 p + s->offset,
426 get_freepointer(s, p));
427
428 if (s->offset)
429 off = s->offset + sizeof(void *);
430 else
431 off = s->inuse;
432
433 if (s->flags & SLAB_STORE_USER) {
434 print_track("Last alloc", get_track(s, p, TRACK_ALLOC));
435 print_track("Last free ", get_track(s, p, TRACK_FREE));
436 off += 2 * sizeof(struct track);
437 }
438
439 if (off != s->size)
440 /* Beginning of the filler is the free pointer */
441 print_section("Filler", p + off, s->size - off);
442}
443
444static void object_err(struct kmem_cache *s, struct page *page,
445 u8 *object, char *reason)
446{
447 u8 *addr = page_address(page);
448
449 printk(KERN_ERR "*** SLUB %s: %s@0x%p slab 0x%p\n",
450 s->name, reason, object, page);
451 printk(KERN_ERR " offset=%tu flags=0x%04lx inuse=%u freelist=0x%p\n",
452 object - addr, page->flags, page->inuse, page->freelist);
453 if (object > addr + 16)
454 print_section("Bytes b4", object - 16, 16);
455 print_section("Object", object, min(s->objsize, 128));
456 print_trailer(s, object);
457 dump_stack();
458}
459
460static void slab_err(struct kmem_cache *s, struct page *page, char *reason, ...)
461{
462 va_list args;
463 char buf[100];
464
465 va_start(args, reason);
466 vsnprintf(buf, sizeof(buf), reason, args);
467 va_end(args);
468 printk(KERN_ERR "*** SLUB %s: %s in slab @0x%p\n", s->name, buf,
469 page);
470 dump_stack();
471}
472
473static void init_object(struct kmem_cache *s, void *object, int active)
474{
475 u8 *p = object;
476
477 if (s->flags & __OBJECT_POISON) {
478 memset(p, POISON_FREE, s->objsize - 1);
479 p[s->objsize -1] = POISON_END;
480 }
481
482 if (s->flags & SLAB_RED_ZONE)
483 memset(p + s->objsize,
484 active ? SLUB_RED_ACTIVE : SLUB_RED_INACTIVE,
485 s->inuse - s->objsize);
486}
487
488static int check_bytes(u8 *start, unsigned int value, unsigned int bytes)
489{
490 while (bytes) {
491 if (*start != (u8)value)
492 return 0;
493 start++;
494 bytes--;
495 }
496 return 1;
497}
498
Christoph Lameter81819f02007-05-06 14:49:36 -0700499/*
500 * Object layout:
501 *
502 * object address
503 * Bytes of the object to be managed.
504 * If the freepointer may overlay the object then the free
505 * pointer is the first word of the object.
Christoph Lameter672bba32007-05-09 02:32:39 -0700506 *
Christoph Lameter81819f02007-05-06 14:49:36 -0700507 * Poisoning uses 0x6b (POISON_FREE) and the last byte is
508 * 0xa5 (POISON_END)
509 *
510 * object + s->objsize
511 * Padding to reach word boundary. This is also used for Redzoning.
Christoph Lameter672bba32007-05-09 02:32:39 -0700512 * Padding is extended by another word if Redzoning is enabled and
513 * objsize == inuse.
514 *
Christoph Lameter81819f02007-05-06 14:49:36 -0700515 * We fill with 0xbb (RED_INACTIVE) for inactive objects and with
516 * 0xcc (RED_ACTIVE) for objects in use.
517 *
518 * object + s->inuse
Christoph Lameter672bba32007-05-09 02:32:39 -0700519 * Meta data starts here.
520 *
Christoph Lameter81819f02007-05-06 14:49:36 -0700521 * A. Free pointer (if we cannot overwrite object on free)
522 * B. Tracking data for SLAB_STORE_USER
Christoph Lameter672bba32007-05-09 02:32:39 -0700523 * C. Padding to reach required alignment boundary or at mininum
524 * one word if debuggin is on to be able to detect writes
525 * before the word boundary.
526 *
527 * Padding is done using 0x5a (POISON_INUSE)
Christoph Lameter81819f02007-05-06 14:49:36 -0700528 *
529 * object + s->size
Christoph Lameter672bba32007-05-09 02:32:39 -0700530 * Nothing is used beyond s->size.
Christoph Lameter81819f02007-05-06 14:49:36 -0700531 *
Christoph Lameter672bba32007-05-09 02:32:39 -0700532 * If slabcaches are merged then the objsize and inuse boundaries are mostly
533 * ignored. And therefore no slab options that rely on these boundaries
Christoph Lameter81819f02007-05-06 14:49:36 -0700534 * may be used with merged slabcaches.
535 */
536
537static void restore_bytes(struct kmem_cache *s, char *message, u8 data,
538 void *from, void *to)
539{
Christoph Lameter70d71222007-05-06 14:49:47 -0700540 printk(KERN_ERR "@@@ SLUB %s: Restoring %s (0x%x) from 0x%p-0x%p\n",
Christoph Lameter81819f02007-05-06 14:49:36 -0700541 s->name, message, data, from, to - 1);
542 memset(from, data, to - from);
543}
544
545static int check_pad_bytes(struct kmem_cache *s, struct page *page, u8 *p)
546{
547 unsigned long off = s->inuse; /* The end of info */
548
549 if (s->offset)
550 /* Freepointer is placed after the object. */
551 off += sizeof(void *);
552
553 if (s->flags & SLAB_STORE_USER)
554 /* We also have user information there */
555 off += 2 * sizeof(struct track);
556
557 if (s->size == off)
558 return 1;
559
560 if (check_bytes(p + off, POISON_INUSE, s->size - off))
561 return 1;
562
563 object_err(s, page, p, "Object padding check fails");
564
565 /*
566 * Restore padding
567 */
568 restore_bytes(s, "object padding", POISON_INUSE, p + off, p + s->size);
569 return 0;
570}
571
572static int slab_pad_check(struct kmem_cache *s, struct page *page)
573{
574 u8 *p;
575 int length, remainder;
576
577 if (!(s->flags & SLAB_POISON))
578 return 1;
579
580 p = page_address(page);
581 length = s->objects * s->size;
582 remainder = (PAGE_SIZE << s->order) - length;
583 if (!remainder)
584 return 1;
585
586 if (!check_bytes(p + length, POISON_INUSE, remainder)) {
Christoph Lameter70d71222007-05-06 14:49:47 -0700587 slab_err(s, page, "Padding check failed");
Christoph Lameter81819f02007-05-06 14:49:36 -0700588 restore_bytes(s, "slab padding", POISON_INUSE, p + length,
589 p + length + remainder);
590 return 0;
591 }
592 return 1;
593}
594
595static int check_object(struct kmem_cache *s, struct page *page,
596 void *object, int active)
597{
598 u8 *p = object;
599 u8 *endobject = object + s->objsize;
600
601 if (s->flags & SLAB_RED_ZONE) {
602 unsigned int red =
603 active ? SLUB_RED_ACTIVE : SLUB_RED_INACTIVE;
604
605 if (!check_bytes(endobject, red, s->inuse - s->objsize)) {
606 object_err(s, page, object,
607 active ? "Redzone Active" : "Redzone Inactive");
608 restore_bytes(s, "redzone", red,
609 endobject, object + s->inuse);
610 return 0;
611 }
612 } else {
613 if ((s->flags & SLAB_POISON) && s->objsize < s->inuse &&
614 !check_bytes(endobject, POISON_INUSE,
615 s->inuse - s->objsize)) {
616 object_err(s, page, p, "Alignment padding check fails");
617 /*
618 * Fix it so that there will not be another report.
619 *
620 * Hmmm... We may be corrupting an object that now expects
621 * to be longer than allowed.
622 */
623 restore_bytes(s, "alignment padding", POISON_INUSE,
624 endobject, object + s->inuse);
625 }
626 }
627
628 if (s->flags & SLAB_POISON) {
629 if (!active && (s->flags & __OBJECT_POISON) &&
630 (!check_bytes(p, POISON_FREE, s->objsize - 1) ||
631 p[s->objsize - 1] != POISON_END)) {
632
633 object_err(s, page, p, "Poison check failed");
634 restore_bytes(s, "Poison", POISON_FREE,
635 p, p + s->objsize -1);
636 restore_bytes(s, "Poison", POISON_END,
637 p + s->objsize - 1, p + s->objsize);
638 return 0;
639 }
640 /*
641 * check_pad_bytes cleans up on its own.
642 */
643 check_pad_bytes(s, page, p);
644 }
645
646 if (!s->offset && active)
647 /*
648 * Object and freepointer overlap. Cannot check
649 * freepointer while object is allocated.
650 */
651 return 1;
652
653 /* Check free pointer validity */
654 if (!check_valid_pointer(s, page, get_freepointer(s, p))) {
655 object_err(s, page, p, "Freepointer corrupt");
656 /*
657 * No choice but to zap it and thus loose the remainder
658 * of the free objects in this slab. May cause
Christoph Lameter672bba32007-05-09 02:32:39 -0700659 * another error because the object count is now wrong.
Christoph Lameter81819f02007-05-06 14:49:36 -0700660 */
661 set_freepointer(s, p, NULL);
662 return 0;
663 }
664 return 1;
665}
666
667static int check_slab(struct kmem_cache *s, struct page *page)
668{
669 VM_BUG_ON(!irqs_disabled());
670
671 if (!PageSlab(page)) {
Christoph Lameter70d71222007-05-06 14:49:47 -0700672 slab_err(s, page, "Not a valid slab page flags=%lx "
673 "mapping=0x%p count=%d", page->flags, page->mapping,
Christoph Lameter81819f02007-05-06 14:49:36 -0700674 page_count(page));
675 return 0;
676 }
677 if (page->offset * sizeof(void *) != s->offset) {
Christoph Lameter70d71222007-05-06 14:49:47 -0700678 slab_err(s, page, "Corrupted offset %lu flags=0x%lx "
679 "mapping=0x%p count=%d",
Christoph Lameter81819f02007-05-06 14:49:36 -0700680 (unsigned long)(page->offset * sizeof(void *)),
Christoph Lameter81819f02007-05-06 14:49:36 -0700681 page->flags,
682 page->mapping,
683 page_count(page));
Christoph Lameter81819f02007-05-06 14:49:36 -0700684 return 0;
685 }
686 if (page->inuse > s->objects) {
Christoph Lameter70d71222007-05-06 14:49:47 -0700687 slab_err(s, page, "inuse %u > max %u @0x%p flags=%lx "
688 "mapping=0x%p count=%d",
689 s->name, page->inuse, s->objects, page->flags,
Christoph Lameter81819f02007-05-06 14:49:36 -0700690 page->mapping, page_count(page));
Christoph Lameter81819f02007-05-06 14:49:36 -0700691 return 0;
692 }
693 /* Slab_pad_check fixes things up after itself */
694 slab_pad_check(s, page);
695 return 1;
696}
697
698/*
Christoph Lameter672bba32007-05-09 02:32:39 -0700699 * Determine if a certain object on a page is on the freelist. Must hold the
700 * slab lock to guarantee that the chains are in a consistent state.
Christoph Lameter81819f02007-05-06 14:49:36 -0700701 */
702static int on_freelist(struct kmem_cache *s, struct page *page, void *search)
703{
704 int nr = 0;
705 void *fp = page->freelist;
706 void *object = NULL;
707
708 while (fp && nr <= s->objects) {
709 if (fp == search)
710 return 1;
711 if (!check_valid_pointer(s, page, fp)) {
712 if (object) {
713 object_err(s, page, object,
714 "Freechain corrupt");
715 set_freepointer(s, object, NULL);
716 break;
717 } else {
Christoph Lameter70d71222007-05-06 14:49:47 -0700718 slab_err(s, page, "Freepointer 0x%p corrupt",
719 fp);
Christoph Lameter81819f02007-05-06 14:49:36 -0700720 page->freelist = NULL;
721 page->inuse = s->objects;
Christoph Lameter70d71222007-05-06 14:49:47 -0700722 printk(KERN_ERR "@@@ SLUB %s: Freelist "
723 "cleared. Slab 0x%p\n",
724 s->name, page);
Christoph Lameter81819f02007-05-06 14:49:36 -0700725 return 0;
726 }
727 break;
728 }
729 object = fp;
730 fp = get_freepointer(s, object);
731 nr++;
732 }
733
734 if (page->inuse != s->objects - nr) {
Christoph Lameter70d71222007-05-06 14:49:47 -0700735 slab_err(s, page, "Wrong object count. Counter is %d but "
736 "counted were %d", s, page, page->inuse,
737 s->objects - nr);
Christoph Lameter81819f02007-05-06 14:49:36 -0700738 page->inuse = s->objects - nr;
Christoph Lameter70d71222007-05-06 14:49:47 -0700739 printk(KERN_ERR "@@@ SLUB %s: Object count adjusted. "
740 "Slab @0x%p\n", s->name, page);
Christoph Lameter81819f02007-05-06 14:49:36 -0700741 }
742 return search == NULL;
743}
744
Christoph Lameter643b1132007-05-06 14:49:42 -0700745/*
Christoph Lameter672bba32007-05-09 02:32:39 -0700746 * Tracking of fully allocated slabs for debugging purposes.
Christoph Lameter643b1132007-05-06 14:49:42 -0700747 */
Christoph Lametere95eed52007-05-06 14:49:44 -0700748static void add_full(struct kmem_cache_node *n, struct page *page)
Christoph Lameter643b1132007-05-06 14:49:42 -0700749{
Christoph Lameter643b1132007-05-06 14:49:42 -0700750 spin_lock(&n->list_lock);
751 list_add(&page->lru, &n->full);
752 spin_unlock(&n->list_lock);
753}
754
755static void remove_full(struct kmem_cache *s, struct page *page)
756{
757 struct kmem_cache_node *n;
758
759 if (!(s->flags & SLAB_STORE_USER))
760 return;
761
762 n = get_node(s, page_to_nid(page));
763
764 spin_lock(&n->list_lock);
765 list_del(&page->lru);
766 spin_unlock(&n->list_lock);
767}
768
Christoph Lameter81819f02007-05-06 14:49:36 -0700769static int alloc_object_checks(struct kmem_cache *s, struct page *page,
770 void *object)
771{
772 if (!check_slab(s, page))
773 goto bad;
774
775 if (object && !on_freelist(s, page, object)) {
Christoph Lameter70d71222007-05-06 14:49:47 -0700776 slab_err(s, page, "Object 0x%p already allocated", object);
777 goto bad;
Christoph Lameter81819f02007-05-06 14:49:36 -0700778 }
779
780 if (!check_valid_pointer(s, page, object)) {
781 object_err(s, page, object, "Freelist Pointer check fails");
Christoph Lameter70d71222007-05-06 14:49:47 -0700782 goto bad;
Christoph Lameter81819f02007-05-06 14:49:36 -0700783 }
784
785 if (!object)
786 return 1;
787
788 if (!check_object(s, page, object, 0))
789 goto bad;
Christoph Lameter81819f02007-05-06 14:49:36 -0700790
Christoph Lameter81819f02007-05-06 14:49:36 -0700791 return 1;
Christoph Lameter81819f02007-05-06 14:49:36 -0700792bad:
793 if (PageSlab(page)) {
794 /*
795 * If this is a slab page then lets do the best we can
796 * to avoid issues in the future. Marking all objects
Christoph Lameter672bba32007-05-09 02:32:39 -0700797 * as used avoids touching the remaining objects.
Christoph Lameter81819f02007-05-06 14:49:36 -0700798 */
799 printk(KERN_ERR "@@@ SLUB: %s slab 0x%p. Marking all objects used.\n",
800 s->name, page);
801 page->inuse = s->objects;
802 page->freelist = NULL;
803 /* Fix up fields that may be corrupted */
804 page->offset = s->offset / sizeof(void *);
805 }
806 return 0;
807}
808
809static int free_object_checks(struct kmem_cache *s, struct page *page,
810 void *object)
811{
812 if (!check_slab(s, page))
813 goto fail;
814
815 if (!check_valid_pointer(s, page, object)) {
Christoph Lameter70d71222007-05-06 14:49:47 -0700816 slab_err(s, page, "Invalid object pointer 0x%p", object);
Christoph Lameter81819f02007-05-06 14:49:36 -0700817 goto fail;
818 }
819
820 if (on_freelist(s, page, object)) {
Christoph Lameter70d71222007-05-06 14:49:47 -0700821 slab_err(s, page, "Object 0x%p already free", object);
Christoph Lameter81819f02007-05-06 14:49:36 -0700822 goto fail;
823 }
824
825 if (!check_object(s, page, object, 1))
826 return 0;
827
828 if (unlikely(s != page->slab)) {
829 if (!PageSlab(page))
Christoph Lameter70d71222007-05-06 14:49:47 -0700830 slab_err(s, page, "Attempt to free object(0x%p) "
831 "outside of slab", object);
Christoph Lameter81819f02007-05-06 14:49:36 -0700832 else
Christoph Lameter70d71222007-05-06 14:49:47 -0700833 if (!page->slab) {
Christoph Lameter81819f02007-05-06 14:49:36 -0700834 printk(KERN_ERR
Christoph Lameter70d71222007-05-06 14:49:47 -0700835 "SLUB <none>: no slab for object 0x%p.\n",
Christoph Lameter81819f02007-05-06 14:49:36 -0700836 object);
Christoph Lameter70d71222007-05-06 14:49:47 -0700837 dump_stack();
838 }
Christoph Lameter81819f02007-05-06 14:49:36 -0700839 else
Christoph Lameter70d71222007-05-06 14:49:47 -0700840 slab_err(s, page, "object at 0x%p belongs "
841 "to slab %s", object, page->slab->name);
Christoph Lameter81819f02007-05-06 14:49:36 -0700842 goto fail;
843 }
Christoph Lameter81819f02007-05-06 14:49:36 -0700844 return 1;
845fail:
Christoph Lameter81819f02007-05-06 14:49:36 -0700846 printk(KERN_ERR "@@@ SLUB: %s slab 0x%p object at 0x%p not freed.\n",
847 s->name, page, object);
848 return 0;
849}
850
Christoph Lameter636f0d72007-05-09 02:32:42 -0700851static void trace(struct kmem_cache *s, struct page *page, void *object, int alloc)
852{
853 if (s->flags & SLAB_TRACE) {
854 printk(KERN_INFO "TRACE %s %s 0x%p inuse=%d fp=0x%p\n",
855 s->name,
856 alloc ? "alloc" : "free",
857 object, page->inuse,
858 page->freelist);
859
860 if (!alloc)
861 print_section("Object", (void *)object, s->objsize);
862
863 dump_stack();
864 }
865}
866
Christoph Lameter41ecc552007-05-09 02:32:44 -0700867static int __init setup_slub_debug(char *str)
868{
869 if (!str || *str != '=')
870 slub_debug = DEBUG_DEFAULT_FLAGS;
871 else {
872 str++;
873 if (*str == 0 || *str == ',')
874 slub_debug = DEBUG_DEFAULT_FLAGS;
875 else
876 for( ;*str && *str != ','; str++)
877 switch (*str) {
878 case 'f' : case 'F' :
879 slub_debug |= SLAB_DEBUG_FREE;
880 break;
881 case 'z' : case 'Z' :
882 slub_debug |= SLAB_RED_ZONE;
883 break;
884 case 'p' : case 'P' :
885 slub_debug |= SLAB_POISON;
886 break;
887 case 'u' : case 'U' :
888 slub_debug |= SLAB_STORE_USER;
889 break;
890 case 't' : case 'T' :
891 slub_debug |= SLAB_TRACE;
892 break;
893 default:
894 printk(KERN_ERR "slub_debug option '%c' "
895 "unknown. skipped\n",*str);
896 }
897 }
898
899 if (*str == ',')
900 slub_debug_slabs = str + 1;
901 return 1;
902}
903
904__setup("slub_debug", setup_slub_debug);
905
906static void kmem_cache_open_debug_check(struct kmem_cache *s)
907{
908 /*
909 * The page->offset field is only 16 bit wide. This is an offset
910 * in units of words from the beginning of an object. If the slab
911 * size is bigger then we cannot move the free pointer behind the
912 * object anymore.
913 *
914 * On 32 bit platforms the limit is 256k. On 64bit platforms
915 * the limit is 512k.
916 *
Christoph Lameterc59def92007-05-16 22:10:50 -0700917 * Debugging or ctor may create a need to move the free
Christoph Lameter41ecc552007-05-09 02:32:44 -0700918 * pointer. Fail if this happens.
919 */
920 if (s->size >= 65535 * sizeof(void *)) {
921 BUG_ON(s->flags & (SLAB_RED_ZONE | SLAB_POISON |
922 SLAB_STORE_USER | SLAB_DESTROY_BY_RCU));
Christoph Lameterc59def92007-05-16 22:10:50 -0700923 BUG_ON(s->ctor);
Christoph Lameter41ecc552007-05-09 02:32:44 -0700924 }
925 else
926 /*
927 * Enable debugging if selected on the kernel commandline.
928 */
929 if (slub_debug && (!slub_debug_slabs ||
930 strncmp(slub_debug_slabs, s->name,
931 strlen(slub_debug_slabs)) == 0))
932 s->flags |= slub_debug;
933}
934#else
935
936static inline int alloc_object_checks(struct kmem_cache *s,
937 struct page *page, void *object) { return 0; }
938
939static inline int free_object_checks(struct kmem_cache *s,
940 struct page *page, void *object) { return 0; }
941
942static inline void add_full(struct kmem_cache_node *n, struct page *page) {}
943static inline void remove_full(struct kmem_cache *s, struct page *page) {}
944static inline void trace(struct kmem_cache *s, struct page *page,
945 void *object, int alloc) {}
946static inline void init_object(struct kmem_cache *s,
947 void *object, int active) {}
948static inline void init_tracking(struct kmem_cache *s, void *object) {}
949static inline int slab_pad_check(struct kmem_cache *s, struct page *page)
950 { return 1; }
951static inline int check_object(struct kmem_cache *s, struct page *page,
952 void *object, int active) { return 1; }
953static inline void set_track(struct kmem_cache *s, void *object,
954 enum track_item alloc, void *addr) {}
955static inline void kmem_cache_open_debug_check(struct kmem_cache *s) {}
956#define slub_debug 0
957#endif
Christoph Lameter81819f02007-05-06 14:49:36 -0700958/*
959 * Slab allocation and freeing
960 */
961static struct page *allocate_slab(struct kmem_cache *s, gfp_t flags, int node)
962{
963 struct page * page;
964 int pages = 1 << s->order;
965
966 if (s->order)
967 flags |= __GFP_COMP;
968
969 if (s->flags & SLAB_CACHE_DMA)
970 flags |= SLUB_DMA;
971
972 if (node == -1)
973 page = alloc_pages(flags, s->order);
974 else
975 page = alloc_pages_node(node, flags, s->order);
976
977 if (!page)
978 return NULL;
979
980 mod_zone_page_state(page_zone(page),
981 (s->flags & SLAB_RECLAIM_ACCOUNT) ?
982 NR_SLAB_RECLAIMABLE : NR_SLAB_UNRECLAIMABLE,
983 pages);
984
985 return page;
986}
987
988static void setup_object(struct kmem_cache *s, struct page *page,
989 void *object)
990{
Christoph Lameter35e5d7e2007-05-09 02:32:42 -0700991 if (SlabDebug(page)) {
Christoph Lameter81819f02007-05-06 14:49:36 -0700992 init_object(s, object, 0);
993 init_tracking(s, object);
994 }
995
Christoph Lameter4f104932007-05-06 14:50:17 -0700996 if (unlikely(s->ctor))
997 s->ctor(object, s, SLAB_CTOR_CONSTRUCTOR);
Christoph Lameter81819f02007-05-06 14:49:36 -0700998}
999
1000static struct page *new_slab(struct kmem_cache *s, gfp_t flags, int node)
1001{
1002 struct page *page;
1003 struct kmem_cache_node *n;
1004 void *start;
1005 void *end;
1006 void *last;
1007 void *p;
1008
Christoph Lameter81819f02007-05-06 14:49:36 -07001009 BUG_ON(flags & ~(GFP_DMA | GFP_LEVEL_MASK));
1010
1011 if (flags & __GFP_WAIT)
1012 local_irq_enable();
1013
1014 page = allocate_slab(s, flags & GFP_LEVEL_MASK, node);
1015 if (!page)
1016 goto out;
1017
1018 n = get_node(s, page_to_nid(page));
1019 if (n)
1020 atomic_long_inc(&n->nr_slabs);
1021 page->offset = s->offset / sizeof(void *);
1022 page->slab = s;
1023 page->flags |= 1 << PG_slab;
1024 if (s->flags & (SLAB_DEBUG_FREE | SLAB_RED_ZONE | SLAB_POISON |
1025 SLAB_STORE_USER | SLAB_TRACE))
Christoph Lameter35e5d7e2007-05-09 02:32:42 -07001026 SetSlabDebug(page);
Christoph Lameter81819f02007-05-06 14:49:36 -07001027
1028 start = page_address(page);
1029 end = start + s->objects * s->size;
1030
1031 if (unlikely(s->flags & SLAB_POISON))
1032 memset(start, POISON_INUSE, PAGE_SIZE << s->order);
1033
1034 last = start;
Christoph Lameter7656c722007-05-09 02:32:40 -07001035 for_each_object(p, s, start) {
Christoph Lameter81819f02007-05-06 14:49:36 -07001036 setup_object(s, page, last);
1037 set_freepointer(s, last, p);
1038 last = p;
1039 }
1040 setup_object(s, page, last);
1041 set_freepointer(s, last, NULL);
1042
1043 page->freelist = start;
Christoph Lameter894b8782007-05-10 03:15:16 -07001044 page->lockless_freelist = NULL;
Christoph Lameter81819f02007-05-06 14:49:36 -07001045 page->inuse = 0;
1046out:
1047 if (flags & __GFP_WAIT)
1048 local_irq_disable();
1049 return page;
1050}
1051
1052static void __free_slab(struct kmem_cache *s, struct page *page)
1053{
1054 int pages = 1 << s->order;
1055
Christoph Lameterc59def92007-05-16 22:10:50 -07001056 if (unlikely(SlabDebug(page))) {
Christoph Lameter81819f02007-05-06 14:49:36 -07001057 void *p;
1058
1059 slab_pad_check(s, page);
Christoph Lameterc59def92007-05-16 22:10:50 -07001060 for_each_object(p, s, page_address(page))
Christoph Lameter81819f02007-05-06 14:49:36 -07001061 check_object(s, page, p, 0);
Christoph Lameter81819f02007-05-06 14:49:36 -07001062 }
1063
1064 mod_zone_page_state(page_zone(page),
1065 (s->flags & SLAB_RECLAIM_ACCOUNT) ?
1066 NR_SLAB_RECLAIMABLE : NR_SLAB_UNRECLAIMABLE,
1067 - pages);
1068
1069 page->mapping = NULL;
1070 __free_pages(page, s->order);
1071}
1072
1073static void rcu_free_slab(struct rcu_head *h)
1074{
1075 struct page *page;
1076
1077 page = container_of((struct list_head *)h, struct page, lru);
1078 __free_slab(page->slab, page);
1079}
1080
1081static void free_slab(struct kmem_cache *s, struct page *page)
1082{
1083 if (unlikely(s->flags & SLAB_DESTROY_BY_RCU)) {
1084 /*
1085 * RCU free overloads the RCU head over the LRU
1086 */
1087 struct rcu_head *head = (void *)&page->lru;
1088
1089 call_rcu(head, rcu_free_slab);
1090 } else
1091 __free_slab(s, page);
1092}
1093
1094static void discard_slab(struct kmem_cache *s, struct page *page)
1095{
1096 struct kmem_cache_node *n = get_node(s, page_to_nid(page));
1097
1098 atomic_long_dec(&n->nr_slabs);
1099 reset_page_mapcount(page);
Christoph Lameter35e5d7e2007-05-09 02:32:42 -07001100 ClearSlabDebug(page);
1101 __ClearPageSlab(page);
Christoph Lameter81819f02007-05-06 14:49:36 -07001102 free_slab(s, page);
1103}
1104
1105/*
1106 * Per slab locking using the pagelock
1107 */
1108static __always_inline void slab_lock(struct page *page)
1109{
1110 bit_spin_lock(PG_locked, &page->flags);
1111}
1112
1113static __always_inline void slab_unlock(struct page *page)
1114{
1115 bit_spin_unlock(PG_locked, &page->flags);
1116}
1117
1118static __always_inline int slab_trylock(struct page *page)
1119{
1120 int rc = 1;
1121
1122 rc = bit_spin_trylock(PG_locked, &page->flags);
1123 return rc;
1124}
1125
1126/*
1127 * Management of partially allocated slabs
1128 */
Christoph Lametere95eed52007-05-06 14:49:44 -07001129static void add_partial_tail(struct kmem_cache_node *n, struct page *page)
Christoph Lameter81819f02007-05-06 14:49:36 -07001130{
Christoph Lametere95eed52007-05-06 14:49:44 -07001131 spin_lock(&n->list_lock);
1132 n->nr_partial++;
1133 list_add_tail(&page->lru, &n->partial);
1134 spin_unlock(&n->list_lock);
1135}
Christoph Lameter81819f02007-05-06 14:49:36 -07001136
Christoph Lametere95eed52007-05-06 14:49:44 -07001137static void add_partial(struct kmem_cache_node *n, struct page *page)
1138{
Christoph Lameter81819f02007-05-06 14:49:36 -07001139 spin_lock(&n->list_lock);
1140 n->nr_partial++;
1141 list_add(&page->lru, &n->partial);
1142 spin_unlock(&n->list_lock);
1143}
1144
1145static void remove_partial(struct kmem_cache *s,
1146 struct page *page)
1147{
1148 struct kmem_cache_node *n = get_node(s, page_to_nid(page));
1149
1150 spin_lock(&n->list_lock);
1151 list_del(&page->lru);
1152 n->nr_partial--;
1153 spin_unlock(&n->list_lock);
1154}
1155
1156/*
Christoph Lameter672bba32007-05-09 02:32:39 -07001157 * Lock slab and remove from the partial list.
Christoph Lameter81819f02007-05-06 14:49:36 -07001158 *
Christoph Lameter672bba32007-05-09 02:32:39 -07001159 * Must hold list_lock.
Christoph Lameter81819f02007-05-06 14:49:36 -07001160 */
Christoph Lameter4b6f0752007-05-16 22:10:53 -07001161static inline int lock_and_freeze_slab(struct kmem_cache_node *n, struct page *page)
Christoph Lameter81819f02007-05-06 14:49:36 -07001162{
1163 if (slab_trylock(page)) {
1164 list_del(&page->lru);
1165 n->nr_partial--;
Christoph Lameter4b6f0752007-05-16 22:10:53 -07001166 SetSlabFrozen(page);
Christoph Lameter81819f02007-05-06 14:49:36 -07001167 return 1;
1168 }
1169 return 0;
1170}
1171
1172/*
Christoph Lameter672bba32007-05-09 02:32:39 -07001173 * Try to allocate a partial slab from a specific node.
Christoph Lameter81819f02007-05-06 14:49:36 -07001174 */
1175static struct page *get_partial_node(struct kmem_cache_node *n)
1176{
1177 struct page *page;
1178
1179 /*
1180 * Racy check. If we mistakenly see no partial slabs then we
1181 * just allocate an empty slab. If we mistakenly try to get a
Christoph Lameter672bba32007-05-09 02:32:39 -07001182 * partial slab and there is none available then get_partials()
1183 * will return NULL.
Christoph Lameter81819f02007-05-06 14:49:36 -07001184 */
1185 if (!n || !n->nr_partial)
1186 return NULL;
1187
1188 spin_lock(&n->list_lock);
1189 list_for_each_entry(page, &n->partial, lru)
Christoph Lameter4b6f0752007-05-16 22:10:53 -07001190 if (lock_and_freeze_slab(n, page))
Christoph Lameter81819f02007-05-06 14:49:36 -07001191 goto out;
1192 page = NULL;
1193out:
1194 spin_unlock(&n->list_lock);
1195 return page;
1196}
1197
1198/*
Christoph Lameter672bba32007-05-09 02:32:39 -07001199 * Get a page from somewhere. Search in increasing NUMA distances.
Christoph Lameter81819f02007-05-06 14:49:36 -07001200 */
1201static struct page *get_any_partial(struct kmem_cache *s, gfp_t flags)
1202{
1203#ifdef CONFIG_NUMA
1204 struct zonelist *zonelist;
1205 struct zone **z;
1206 struct page *page;
1207
1208 /*
Christoph Lameter672bba32007-05-09 02:32:39 -07001209 * The defrag ratio allows a configuration of the tradeoffs between
1210 * inter node defragmentation and node local allocations. A lower
1211 * defrag_ratio increases the tendency to do local allocations
1212 * instead of attempting to obtain partial slabs from other nodes.
Christoph Lameter81819f02007-05-06 14:49:36 -07001213 *
Christoph Lameter672bba32007-05-09 02:32:39 -07001214 * If the defrag_ratio is set to 0 then kmalloc() always
1215 * returns node local objects. If the ratio is higher then kmalloc()
1216 * may return off node objects because partial slabs are obtained
1217 * from other nodes and filled up.
Christoph Lameter81819f02007-05-06 14:49:36 -07001218 *
1219 * If /sys/slab/xx/defrag_ratio is set to 100 (which makes
Christoph Lameter672bba32007-05-09 02:32:39 -07001220 * defrag_ratio = 1000) then every (well almost) allocation will
1221 * first attempt to defrag slab caches on other nodes. This means
1222 * scanning over all nodes to look for partial slabs which may be
1223 * expensive if we do it every time we are trying to find a slab
1224 * with available objects.
Christoph Lameter81819f02007-05-06 14:49:36 -07001225 */
1226 if (!s->defrag_ratio || get_cycles() % 1024 > s->defrag_ratio)
1227 return NULL;
1228
1229 zonelist = &NODE_DATA(slab_node(current->mempolicy))
1230 ->node_zonelists[gfp_zone(flags)];
1231 for (z = zonelist->zones; *z; z++) {
1232 struct kmem_cache_node *n;
1233
1234 n = get_node(s, zone_to_nid(*z));
1235
1236 if (n && cpuset_zone_allowed_hardwall(*z, flags) &&
Christoph Lametere95eed52007-05-06 14:49:44 -07001237 n->nr_partial > MIN_PARTIAL) {
Christoph Lameter81819f02007-05-06 14:49:36 -07001238 page = get_partial_node(n);
1239 if (page)
1240 return page;
1241 }
1242 }
1243#endif
1244 return NULL;
1245}
1246
1247/*
1248 * Get a partial page, lock it and return it.
1249 */
1250static struct page *get_partial(struct kmem_cache *s, gfp_t flags, int node)
1251{
1252 struct page *page;
1253 int searchnode = (node == -1) ? numa_node_id() : node;
1254
1255 page = get_partial_node(get_node(s, searchnode));
1256 if (page || (flags & __GFP_THISNODE))
1257 return page;
1258
1259 return get_any_partial(s, flags);
1260}
1261
1262/*
1263 * Move a page back to the lists.
1264 *
1265 * Must be called with the slab lock held.
1266 *
1267 * On exit the slab lock will have been dropped.
1268 */
Christoph Lameter4b6f0752007-05-16 22:10:53 -07001269static void unfreeze_slab(struct kmem_cache *s, struct page *page)
Christoph Lameter81819f02007-05-06 14:49:36 -07001270{
Christoph Lametere95eed52007-05-06 14:49:44 -07001271 struct kmem_cache_node *n = get_node(s, page_to_nid(page));
1272
Christoph Lameter4b6f0752007-05-16 22:10:53 -07001273 ClearSlabFrozen(page);
Christoph Lameter81819f02007-05-06 14:49:36 -07001274 if (page->inuse) {
Christoph Lametere95eed52007-05-06 14:49:44 -07001275
Christoph Lameter81819f02007-05-06 14:49:36 -07001276 if (page->freelist)
Christoph Lametere95eed52007-05-06 14:49:44 -07001277 add_partial(n, page);
Christoph Lameter35e5d7e2007-05-09 02:32:42 -07001278 else if (SlabDebug(page) && (s->flags & SLAB_STORE_USER))
Christoph Lametere95eed52007-05-06 14:49:44 -07001279 add_full(n, page);
Christoph Lameter81819f02007-05-06 14:49:36 -07001280 slab_unlock(page);
Christoph Lametere95eed52007-05-06 14:49:44 -07001281
Christoph Lameter81819f02007-05-06 14:49:36 -07001282 } else {
Christoph Lametere95eed52007-05-06 14:49:44 -07001283 if (n->nr_partial < MIN_PARTIAL) {
1284 /*
Christoph Lameter672bba32007-05-09 02:32:39 -07001285 * Adding an empty slab to the partial slabs in order
1286 * to avoid page allocator overhead. This slab needs
1287 * to come after the other slabs with objects in
1288 * order to fill them up. That way the size of the
1289 * partial list stays small. kmem_cache_shrink can
1290 * reclaim empty slabs from the partial list.
Christoph Lametere95eed52007-05-06 14:49:44 -07001291 */
1292 add_partial_tail(n, page);
1293 slab_unlock(page);
1294 } else {
1295 slab_unlock(page);
1296 discard_slab(s, page);
1297 }
Christoph Lameter81819f02007-05-06 14:49:36 -07001298 }
1299}
1300
1301/*
1302 * Remove the cpu slab
1303 */
1304static void deactivate_slab(struct kmem_cache *s, struct page *page, int cpu)
1305{
Christoph Lameter894b8782007-05-10 03:15:16 -07001306 /*
1307 * Merge cpu freelist into freelist. Typically we get here
1308 * because both freelists are empty. So this is unlikely
1309 * to occur.
1310 */
1311 while (unlikely(page->lockless_freelist)) {
1312 void **object;
1313
1314 /* Retrieve object from cpu_freelist */
1315 object = page->lockless_freelist;
1316 page->lockless_freelist = page->lockless_freelist[page->offset];
1317
1318 /* And put onto the regular freelist */
1319 object[page->offset] = page->freelist;
1320 page->freelist = object;
1321 page->inuse--;
1322 }
Christoph Lameter81819f02007-05-06 14:49:36 -07001323 s->cpu_slab[cpu] = NULL;
Christoph Lameter4b6f0752007-05-16 22:10:53 -07001324 unfreeze_slab(s, page);
Christoph Lameter81819f02007-05-06 14:49:36 -07001325}
1326
1327static void flush_slab(struct kmem_cache *s, struct page *page, int cpu)
1328{
1329 slab_lock(page);
1330 deactivate_slab(s, page, cpu);
1331}
1332
1333/*
1334 * Flush cpu slab.
1335 * Called from IPI handler with interrupts disabled.
1336 */
1337static void __flush_cpu_slab(struct kmem_cache *s, int cpu)
1338{
1339 struct page *page = s->cpu_slab[cpu];
1340
1341 if (likely(page))
1342 flush_slab(s, page, cpu);
1343}
1344
1345static void flush_cpu_slab(void *d)
1346{
1347 struct kmem_cache *s = d;
1348 int cpu = smp_processor_id();
1349
1350 __flush_cpu_slab(s, cpu);
1351}
1352
1353static void flush_all(struct kmem_cache *s)
1354{
1355#ifdef CONFIG_SMP
1356 on_each_cpu(flush_cpu_slab, s, 1, 1);
1357#else
1358 unsigned long flags;
1359
1360 local_irq_save(flags);
1361 flush_cpu_slab(s);
1362 local_irq_restore(flags);
1363#endif
1364}
1365
1366/*
Christoph Lameter894b8782007-05-10 03:15:16 -07001367 * Slow path. The lockless freelist is empty or we need to perform
1368 * debugging duties.
Christoph Lameter81819f02007-05-06 14:49:36 -07001369 *
Christoph Lameter894b8782007-05-10 03:15:16 -07001370 * Interrupts are disabled.
Christoph Lameter81819f02007-05-06 14:49:36 -07001371 *
Christoph Lameter894b8782007-05-10 03:15:16 -07001372 * Processing is still very fast if new objects have been freed to the
1373 * regular freelist. In that case we simply take over the regular freelist
1374 * as the lockless freelist and zap the regular freelist.
Christoph Lameter81819f02007-05-06 14:49:36 -07001375 *
Christoph Lameter894b8782007-05-10 03:15:16 -07001376 * If that is not working then we fall back to the partial lists. We take the
1377 * first element of the freelist as the object to allocate now and move the
1378 * rest of the freelist to the lockless freelist.
1379 *
1380 * And if we were unable to get a new slab from the partial slab lists then
1381 * we need to allocate a new slab. This is slowest path since we may sleep.
Christoph Lameter81819f02007-05-06 14:49:36 -07001382 */
Christoph Lameter894b8782007-05-10 03:15:16 -07001383static void *__slab_alloc(struct kmem_cache *s,
1384 gfp_t gfpflags, int node, void *addr, struct page *page)
Christoph Lameter81819f02007-05-06 14:49:36 -07001385{
Christoph Lameter81819f02007-05-06 14:49:36 -07001386 void **object;
Christoph Lameter894b8782007-05-10 03:15:16 -07001387 int cpu = smp_processor_id();
Christoph Lameter81819f02007-05-06 14:49:36 -07001388
Christoph Lameter81819f02007-05-06 14:49:36 -07001389 if (!page)
1390 goto new_slab;
1391
1392 slab_lock(page);
1393 if (unlikely(node != -1 && page_to_nid(page) != node))
1394 goto another_slab;
Christoph Lameter894b8782007-05-10 03:15:16 -07001395load_freelist:
Christoph Lameter81819f02007-05-06 14:49:36 -07001396 object = page->freelist;
1397 if (unlikely(!object))
1398 goto another_slab;
Christoph Lameter35e5d7e2007-05-09 02:32:42 -07001399 if (unlikely(SlabDebug(page)))
Christoph Lameter81819f02007-05-06 14:49:36 -07001400 goto debug;
1401
Christoph Lameter894b8782007-05-10 03:15:16 -07001402 object = page->freelist;
1403 page->lockless_freelist = object[page->offset];
1404 page->inuse = s->objects;
1405 page->freelist = NULL;
Christoph Lameter81819f02007-05-06 14:49:36 -07001406 slab_unlock(page);
Christoph Lameter81819f02007-05-06 14:49:36 -07001407 return object;
1408
1409another_slab:
1410 deactivate_slab(s, page, cpu);
1411
1412new_slab:
1413 page = get_partial(s, gfpflags, node);
Christoph Lameter894b8782007-05-10 03:15:16 -07001414 if (page) {
Christoph Lameter81819f02007-05-06 14:49:36 -07001415 s->cpu_slab[cpu] = page;
Christoph Lameter894b8782007-05-10 03:15:16 -07001416 goto load_freelist;
Christoph Lameter81819f02007-05-06 14:49:36 -07001417 }
1418
1419 page = new_slab(s, gfpflags, node);
1420 if (page) {
1421 cpu = smp_processor_id();
1422 if (s->cpu_slab[cpu]) {
1423 /*
Christoph Lameter672bba32007-05-09 02:32:39 -07001424 * Someone else populated the cpu_slab while we
1425 * enabled interrupts, or we have gotten scheduled
1426 * on another cpu. The page may not be on the
1427 * requested node even if __GFP_THISNODE was
1428 * specified. So we need to recheck.
Christoph Lameter81819f02007-05-06 14:49:36 -07001429 */
1430 if (node == -1 ||
1431 page_to_nid(s->cpu_slab[cpu]) == node) {
1432 /*
1433 * Current cpuslab is acceptable and we
1434 * want the current one since its cache hot
1435 */
1436 discard_slab(s, page);
1437 page = s->cpu_slab[cpu];
1438 slab_lock(page);
Christoph Lameter894b8782007-05-10 03:15:16 -07001439 goto load_freelist;
Christoph Lameter81819f02007-05-06 14:49:36 -07001440 }
Christoph Lameter672bba32007-05-09 02:32:39 -07001441 /* New slab does not fit our expectations */
Christoph Lameter81819f02007-05-06 14:49:36 -07001442 flush_slab(s, s->cpu_slab[cpu], cpu);
1443 }
1444 slab_lock(page);
Christoph Lameter4b6f0752007-05-16 22:10:53 -07001445 SetSlabFrozen(page);
1446 s->cpu_slab[cpu] = page;
1447 goto load_freelist;
Christoph Lameter81819f02007-05-06 14:49:36 -07001448 }
Christoph Lameter81819f02007-05-06 14:49:36 -07001449 return NULL;
1450debug:
Christoph Lameter894b8782007-05-10 03:15:16 -07001451 object = page->freelist;
Christoph Lameter81819f02007-05-06 14:49:36 -07001452 if (!alloc_object_checks(s, page, object))
1453 goto another_slab;
1454 if (s->flags & SLAB_STORE_USER)
Christoph Lameter77c5e2d2007-05-06 14:49:42 -07001455 set_track(s, object, TRACK_ALLOC, addr);
Christoph Lameter636f0d72007-05-09 02:32:42 -07001456 trace(s, page, object, 1);
Christoph Lameter70d71222007-05-06 14:49:47 -07001457 init_object(s, object, 1);
Christoph Lameter894b8782007-05-10 03:15:16 -07001458
1459 page->inuse++;
1460 page->freelist = object[page->offset];
1461 slab_unlock(page);
1462 return object;
1463}
1464
1465/*
1466 * Inlined fastpath so that allocation functions (kmalloc, kmem_cache_alloc)
1467 * have the fastpath folded into their functions. So no function call
1468 * overhead for requests that can be satisfied on the fastpath.
1469 *
1470 * The fastpath works by first checking if the lockless freelist can be used.
1471 * If not then __slab_alloc is called for slow processing.
1472 *
1473 * Otherwise we can simply pick the next object from the lockless free list.
1474 */
1475static void __always_inline *slab_alloc(struct kmem_cache *s,
1476 gfp_t gfpflags, int node, void *addr)
1477{
1478 struct page *page;
1479 void **object;
1480 unsigned long flags;
1481
1482 local_irq_save(flags);
1483 page = s->cpu_slab[smp_processor_id()];
1484 if (unlikely(!page || !page->lockless_freelist ||
1485 (node != -1 && page_to_nid(page) != node)))
1486
1487 object = __slab_alloc(s, gfpflags, node, addr, page);
1488
1489 else {
1490 object = page->lockless_freelist;
1491 page->lockless_freelist = object[page->offset];
1492 }
1493 local_irq_restore(flags);
1494 return object;
Christoph Lameter81819f02007-05-06 14:49:36 -07001495}
1496
1497void *kmem_cache_alloc(struct kmem_cache *s, gfp_t gfpflags)
1498{
Christoph Lameter77c5e2d2007-05-06 14:49:42 -07001499 return slab_alloc(s, gfpflags, -1, __builtin_return_address(0));
Christoph Lameter81819f02007-05-06 14:49:36 -07001500}
1501EXPORT_SYMBOL(kmem_cache_alloc);
1502
1503#ifdef CONFIG_NUMA
1504void *kmem_cache_alloc_node(struct kmem_cache *s, gfp_t gfpflags, int node)
1505{
Christoph Lameter77c5e2d2007-05-06 14:49:42 -07001506 return slab_alloc(s, gfpflags, node, __builtin_return_address(0));
Christoph Lameter81819f02007-05-06 14:49:36 -07001507}
1508EXPORT_SYMBOL(kmem_cache_alloc_node);
1509#endif
1510
1511/*
Christoph Lameter894b8782007-05-10 03:15:16 -07001512 * Slow patch handling. This may still be called frequently since objects
1513 * have a longer lifetime than the cpu slabs in most processing loads.
Christoph Lameter81819f02007-05-06 14:49:36 -07001514 *
Christoph Lameter894b8782007-05-10 03:15:16 -07001515 * So we still attempt to reduce cache line usage. Just take the slab
1516 * lock and free the item. If there is no additional partial page
1517 * handling required then we can return immediately.
Christoph Lameter81819f02007-05-06 14:49:36 -07001518 */
Christoph Lameter894b8782007-05-10 03:15:16 -07001519static void __slab_free(struct kmem_cache *s, struct page *page,
Christoph Lameter77c5e2d2007-05-06 14:49:42 -07001520 void *x, void *addr)
Christoph Lameter81819f02007-05-06 14:49:36 -07001521{
1522 void *prior;
1523 void **object = (void *)x;
Christoph Lameter81819f02007-05-06 14:49:36 -07001524
Christoph Lameter81819f02007-05-06 14:49:36 -07001525 slab_lock(page);
1526
Christoph Lameter35e5d7e2007-05-09 02:32:42 -07001527 if (unlikely(SlabDebug(page)))
Christoph Lameter81819f02007-05-06 14:49:36 -07001528 goto debug;
1529checks_ok:
1530 prior = object[page->offset] = page->freelist;
1531 page->freelist = object;
1532 page->inuse--;
1533
Christoph Lameter4b6f0752007-05-16 22:10:53 -07001534 if (unlikely(SlabFrozen(page)))
Christoph Lameter81819f02007-05-06 14:49:36 -07001535 goto out_unlock;
1536
1537 if (unlikely(!page->inuse))
1538 goto slab_empty;
1539
1540 /*
1541 * Objects left in the slab. If it
1542 * was not on the partial list before
1543 * then add it.
1544 */
1545 if (unlikely(!prior))
Christoph Lametere95eed52007-05-06 14:49:44 -07001546 add_partial(get_node(s, page_to_nid(page)), page);
Christoph Lameter81819f02007-05-06 14:49:36 -07001547
1548out_unlock:
1549 slab_unlock(page);
Christoph Lameter81819f02007-05-06 14:49:36 -07001550 return;
1551
1552slab_empty:
1553 if (prior)
1554 /*
Christoph Lameter672bba32007-05-09 02:32:39 -07001555 * Slab still on the partial list.
Christoph Lameter81819f02007-05-06 14:49:36 -07001556 */
1557 remove_partial(s, page);
1558
1559 slab_unlock(page);
1560 discard_slab(s, page);
Christoph Lameter81819f02007-05-06 14:49:36 -07001561 return;
1562
1563debug:
Christoph Lameter77c5e2d2007-05-06 14:49:42 -07001564 if (!free_object_checks(s, page, x))
1565 goto out_unlock;
Christoph Lameter4b6f0752007-05-16 22:10:53 -07001566 if (!SlabFrozen(page) && !page->freelist)
Christoph Lameter643b1132007-05-06 14:49:42 -07001567 remove_full(s, page);
Christoph Lameter77c5e2d2007-05-06 14:49:42 -07001568 if (s->flags & SLAB_STORE_USER)
1569 set_track(s, x, TRACK_FREE, addr);
Christoph Lameter636f0d72007-05-09 02:32:42 -07001570 trace(s, page, object, 0);
Christoph Lameter70d71222007-05-06 14:49:47 -07001571 init_object(s, object, 0);
Christoph Lameter77c5e2d2007-05-06 14:49:42 -07001572 goto checks_ok;
Christoph Lameter81819f02007-05-06 14:49:36 -07001573}
1574
Christoph Lameter894b8782007-05-10 03:15:16 -07001575/*
1576 * Fastpath with forced inlining to produce a kfree and kmem_cache_free that
1577 * can perform fastpath freeing without additional function calls.
1578 *
1579 * The fastpath is only possible if we are freeing to the current cpu slab
1580 * of this processor. This typically the case if we have just allocated
1581 * the item before.
1582 *
1583 * If fastpath is not possible then fall back to __slab_free where we deal
1584 * with all sorts of special processing.
1585 */
1586static void __always_inline slab_free(struct kmem_cache *s,
1587 struct page *page, void *x, void *addr)
1588{
1589 void **object = (void *)x;
1590 unsigned long flags;
1591
1592 local_irq_save(flags);
1593 if (likely(page == s->cpu_slab[smp_processor_id()] &&
1594 !SlabDebug(page))) {
1595 object[page->offset] = page->lockless_freelist;
1596 page->lockless_freelist = object;
1597 } else
1598 __slab_free(s, page, x, addr);
1599
1600 local_irq_restore(flags);
1601}
1602
Christoph Lameter81819f02007-05-06 14:49:36 -07001603void kmem_cache_free(struct kmem_cache *s, void *x)
1604{
Christoph Lameter77c5e2d2007-05-06 14:49:42 -07001605 struct page *page;
Christoph Lameter81819f02007-05-06 14:49:36 -07001606
Christoph Lameterb49af682007-05-06 14:49:41 -07001607 page = virt_to_head_page(x);
Christoph Lameter81819f02007-05-06 14:49:36 -07001608
Christoph Lameter77c5e2d2007-05-06 14:49:42 -07001609 slab_free(s, page, x, __builtin_return_address(0));
Christoph Lameter81819f02007-05-06 14:49:36 -07001610}
1611EXPORT_SYMBOL(kmem_cache_free);
1612
1613/* Figure out on which slab object the object resides */
1614static struct page *get_object_page(const void *x)
1615{
Christoph Lameterb49af682007-05-06 14:49:41 -07001616 struct page *page = virt_to_head_page(x);
Christoph Lameter81819f02007-05-06 14:49:36 -07001617
1618 if (!PageSlab(page))
1619 return NULL;
1620
1621 return page;
1622}
1623
1624/*
Christoph Lameter672bba32007-05-09 02:32:39 -07001625 * Object placement in a slab is made very easy because we always start at
1626 * offset 0. If we tune the size of the object to the alignment then we can
1627 * get the required alignment by putting one properly sized object after
1628 * another.
Christoph Lameter81819f02007-05-06 14:49:36 -07001629 *
1630 * Notice that the allocation order determines the sizes of the per cpu
1631 * caches. Each processor has always one slab available for allocations.
1632 * Increasing the allocation order reduces the number of times that slabs
Christoph Lameter672bba32007-05-09 02:32:39 -07001633 * must be moved on and off the partial lists and is therefore a factor in
Christoph Lameter81819f02007-05-06 14:49:36 -07001634 * locking overhead.
Christoph Lameter81819f02007-05-06 14:49:36 -07001635 */
1636
1637/*
1638 * Mininum / Maximum order of slab pages. This influences locking overhead
1639 * and slab fragmentation. A higher order reduces the number of partial slabs
1640 * and increases the number of allocations possible without having to
1641 * take the list_lock.
1642 */
1643static int slub_min_order;
1644static int slub_max_order = DEFAULT_MAX_ORDER;
Christoph Lameter81819f02007-05-06 14:49:36 -07001645static int slub_min_objects = DEFAULT_MIN_OBJECTS;
1646
1647/*
1648 * Merge control. If this is set then no merging of slab caches will occur.
Christoph Lameter672bba32007-05-09 02:32:39 -07001649 * (Could be removed. This was introduced to pacify the merge skeptics.)
Christoph Lameter81819f02007-05-06 14:49:36 -07001650 */
1651static int slub_nomerge;
1652
1653/*
Christoph Lameter81819f02007-05-06 14:49:36 -07001654 * Calculate the order of allocation given an slab object size.
1655 *
Christoph Lameter672bba32007-05-09 02:32:39 -07001656 * The order of allocation has significant impact on performance and other
1657 * system components. Generally order 0 allocations should be preferred since
1658 * order 0 does not cause fragmentation in the page allocator. Larger objects
1659 * be problematic to put into order 0 slabs because there may be too much
1660 * unused space left. We go to a higher order if more than 1/8th of the slab
1661 * would be wasted.
Christoph Lameter81819f02007-05-06 14:49:36 -07001662 *
Christoph Lameter672bba32007-05-09 02:32:39 -07001663 * In order to reach satisfactory performance we must ensure that a minimum
1664 * number of objects is in one slab. Otherwise we may generate too much
1665 * activity on the partial lists which requires taking the list_lock. This is
1666 * less a concern for large slabs though which are rarely used.
Christoph Lameter81819f02007-05-06 14:49:36 -07001667 *
Christoph Lameter672bba32007-05-09 02:32:39 -07001668 * slub_max_order specifies the order where we begin to stop considering the
1669 * number of objects in a slab as critical. If we reach slub_max_order then
1670 * we try to keep the page order as low as possible. So we accept more waste
1671 * of space in favor of a small page order.
1672 *
1673 * Higher order allocations also allow the placement of more objects in a
1674 * slab and thereby reduce object handling overhead. If the user has
1675 * requested a higher mininum order then we start with that one instead of
1676 * the smallest order which will fit the object.
Christoph Lameter81819f02007-05-06 14:49:36 -07001677 */
Christoph Lameter5e6d4442007-05-09 02:32:46 -07001678static inline int slab_order(int size, int min_objects,
1679 int max_order, int fract_leftover)
Christoph Lameter81819f02007-05-06 14:49:36 -07001680{
1681 int order;
1682 int rem;
1683
Christoph Lameter5e6d4442007-05-09 02:32:46 -07001684 for (order = max(slub_min_order,
1685 fls(min_objects * size - 1) - PAGE_SHIFT);
1686 order <= max_order; order++) {
1687
Christoph Lameter81819f02007-05-06 14:49:36 -07001688 unsigned long slab_size = PAGE_SIZE << order;
1689
Christoph Lameter5e6d4442007-05-09 02:32:46 -07001690 if (slab_size < min_objects * size)
Christoph Lameter81819f02007-05-06 14:49:36 -07001691 continue;
1692
Christoph Lameter81819f02007-05-06 14:49:36 -07001693 rem = slab_size % size;
1694
Christoph Lameter5e6d4442007-05-09 02:32:46 -07001695 if (rem <= slab_size / fract_leftover)
Christoph Lameter81819f02007-05-06 14:49:36 -07001696 break;
1697
1698 }
Christoph Lameter672bba32007-05-09 02:32:39 -07001699
Christoph Lameter81819f02007-05-06 14:49:36 -07001700 return order;
1701}
1702
Christoph Lameter5e6d4442007-05-09 02:32:46 -07001703static inline int calculate_order(int size)
1704{
1705 int order;
1706 int min_objects;
1707 int fraction;
1708
1709 /*
1710 * Attempt to find best configuration for a slab. This
1711 * works by first attempting to generate a layout with
1712 * the best configuration and backing off gradually.
1713 *
1714 * First we reduce the acceptable waste in a slab. Then
1715 * we reduce the minimum objects required in a slab.
1716 */
1717 min_objects = slub_min_objects;
1718 while (min_objects > 1) {
1719 fraction = 8;
1720 while (fraction >= 4) {
1721 order = slab_order(size, min_objects,
1722 slub_max_order, fraction);
1723 if (order <= slub_max_order)
1724 return order;
1725 fraction /= 2;
1726 }
1727 min_objects /= 2;
1728 }
1729
1730 /*
1731 * We were unable to place multiple objects in a slab. Now
1732 * lets see if we can place a single object there.
1733 */
1734 order = slab_order(size, 1, slub_max_order, 1);
1735 if (order <= slub_max_order)
1736 return order;
1737
1738 /*
1739 * Doh this slab cannot be placed using slub_max_order.
1740 */
1741 order = slab_order(size, 1, MAX_ORDER, 1);
1742 if (order <= MAX_ORDER)
1743 return order;
1744 return -ENOSYS;
1745}
1746
Christoph Lameter81819f02007-05-06 14:49:36 -07001747/*
Christoph Lameter672bba32007-05-09 02:32:39 -07001748 * Figure out what the alignment of the objects will be.
Christoph Lameter81819f02007-05-06 14:49:36 -07001749 */
1750static unsigned long calculate_alignment(unsigned long flags,
1751 unsigned long align, unsigned long size)
1752{
1753 /*
1754 * If the user wants hardware cache aligned objects then
1755 * follow that suggestion if the object is sufficiently
1756 * large.
1757 *
1758 * The hardware cache alignment cannot override the
1759 * specified alignment though. If that is greater
1760 * then use it.
1761 */
Christoph Lameter5af60832007-05-06 14:49:56 -07001762 if ((flags & SLAB_HWCACHE_ALIGN) &&
Christoph Lameter65c02d42007-05-09 02:32:35 -07001763 size > cache_line_size() / 2)
1764 return max_t(unsigned long, align, cache_line_size());
Christoph Lameter81819f02007-05-06 14:49:36 -07001765
1766 if (align < ARCH_SLAB_MINALIGN)
1767 return ARCH_SLAB_MINALIGN;
1768
1769 return ALIGN(align, sizeof(void *));
1770}
1771
1772static void init_kmem_cache_node(struct kmem_cache_node *n)
1773{
1774 n->nr_partial = 0;
1775 atomic_long_set(&n->nr_slabs, 0);
1776 spin_lock_init(&n->list_lock);
1777 INIT_LIST_HEAD(&n->partial);
Christoph Lameter643b1132007-05-06 14:49:42 -07001778 INIT_LIST_HEAD(&n->full);
Christoph Lameter81819f02007-05-06 14:49:36 -07001779}
1780
1781#ifdef CONFIG_NUMA
1782/*
1783 * No kmalloc_node yet so do it by hand. We know that this is the first
1784 * slab on the node for this slabcache. There are no concurrent accesses
1785 * possible.
1786 *
1787 * Note that this function only works on the kmalloc_node_cache
1788 * when allocating for the kmalloc_node_cache.
1789 */
1790static struct kmem_cache_node * __init early_kmem_cache_node_alloc(gfp_t gfpflags,
1791 int node)
1792{
1793 struct page *page;
1794 struct kmem_cache_node *n;
1795
1796 BUG_ON(kmalloc_caches->size < sizeof(struct kmem_cache_node));
1797
1798 page = new_slab(kmalloc_caches, gfpflags | GFP_THISNODE, node);
1799 /* new_slab() disables interupts */
1800 local_irq_enable();
1801
1802 BUG_ON(!page);
1803 n = page->freelist;
1804 BUG_ON(!n);
1805 page->freelist = get_freepointer(kmalloc_caches, n);
1806 page->inuse++;
1807 kmalloc_caches->node[node] = n;
1808 init_object(kmalloc_caches, n, 1);
1809 init_kmem_cache_node(n);
1810 atomic_long_inc(&n->nr_slabs);
Christoph Lametere95eed52007-05-06 14:49:44 -07001811 add_partial(n, page);
Christoph Lameter81819f02007-05-06 14:49:36 -07001812 return n;
1813}
1814
1815static void free_kmem_cache_nodes(struct kmem_cache *s)
1816{
1817 int node;
1818
1819 for_each_online_node(node) {
1820 struct kmem_cache_node *n = s->node[node];
1821 if (n && n != &s->local_node)
1822 kmem_cache_free(kmalloc_caches, n);
1823 s->node[node] = NULL;
1824 }
1825}
1826
1827static int init_kmem_cache_nodes(struct kmem_cache *s, gfp_t gfpflags)
1828{
1829 int node;
1830 int local_node;
1831
1832 if (slab_state >= UP)
1833 local_node = page_to_nid(virt_to_page(s));
1834 else
1835 local_node = 0;
1836
1837 for_each_online_node(node) {
1838 struct kmem_cache_node *n;
1839
1840 if (local_node == node)
1841 n = &s->local_node;
1842 else {
1843 if (slab_state == DOWN) {
1844 n = early_kmem_cache_node_alloc(gfpflags,
1845 node);
1846 continue;
1847 }
1848 n = kmem_cache_alloc_node(kmalloc_caches,
1849 gfpflags, node);
1850
1851 if (!n) {
1852 free_kmem_cache_nodes(s);
1853 return 0;
1854 }
1855
1856 }
1857 s->node[node] = n;
1858 init_kmem_cache_node(n);
1859 }
1860 return 1;
1861}
1862#else
1863static void free_kmem_cache_nodes(struct kmem_cache *s)
1864{
1865}
1866
1867static int init_kmem_cache_nodes(struct kmem_cache *s, gfp_t gfpflags)
1868{
1869 init_kmem_cache_node(&s->local_node);
1870 return 1;
1871}
1872#endif
1873
1874/*
1875 * calculate_sizes() determines the order and the distribution of data within
1876 * a slab object.
1877 */
1878static int calculate_sizes(struct kmem_cache *s)
1879{
1880 unsigned long flags = s->flags;
1881 unsigned long size = s->objsize;
1882 unsigned long align = s->align;
1883
1884 /*
1885 * Determine if we can poison the object itself. If the user of
1886 * the slab may touch the object after free or before allocation
1887 * then we should never poison the object itself.
1888 */
1889 if ((flags & SLAB_POISON) && !(flags & SLAB_DESTROY_BY_RCU) &&
Christoph Lameterc59def92007-05-16 22:10:50 -07001890 !s->ctor)
Christoph Lameter81819f02007-05-06 14:49:36 -07001891 s->flags |= __OBJECT_POISON;
1892 else
1893 s->flags &= ~__OBJECT_POISON;
1894
1895 /*
1896 * Round up object size to the next word boundary. We can only
1897 * place the free pointer at word boundaries and this determines
1898 * the possible location of the free pointer.
1899 */
1900 size = ALIGN(size, sizeof(void *));
1901
Christoph Lameter41ecc552007-05-09 02:32:44 -07001902#ifdef CONFIG_SLUB_DEBUG
Christoph Lameter81819f02007-05-06 14:49:36 -07001903 /*
Christoph Lameter672bba32007-05-09 02:32:39 -07001904 * If we are Redzoning then check if there is some space between the
Christoph Lameter81819f02007-05-06 14:49:36 -07001905 * end of the object and the free pointer. If not then add an
Christoph Lameter672bba32007-05-09 02:32:39 -07001906 * additional word to have some bytes to store Redzone information.
Christoph Lameter81819f02007-05-06 14:49:36 -07001907 */
1908 if ((flags & SLAB_RED_ZONE) && size == s->objsize)
1909 size += sizeof(void *);
Christoph Lameter41ecc552007-05-09 02:32:44 -07001910#endif
Christoph Lameter81819f02007-05-06 14:49:36 -07001911
1912 /*
Christoph Lameter672bba32007-05-09 02:32:39 -07001913 * With that we have determined the number of bytes in actual use
1914 * by the object. This is the potential offset to the free pointer.
Christoph Lameter81819f02007-05-06 14:49:36 -07001915 */
1916 s->inuse = size;
1917
Christoph Lameter41ecc552007-05-09 02:32:44 -07001918#ifdef CONFIG_SLUB_DEBUG
Christoph Lameter81819f02007-05-06 14:49:36 -07001919 if (((flags & (SLAB_DESTROY_BY_RCU | SLAB_POISON)) ||
Christoph Lameterc59def92007-05-16 22:10:50 -07001920 s->ctor)) {
Christoph Lameter81819f02007-05-06 14:49:36 -07001921 /*
1922 * Relocate free pointer after the object if it is not
1923 * permitted to overwrite the first word of the object on
1924 * kmem_cache_free.
1925 *
1926 * This is the case if we do RCU, have a constructor or
1927 * destructor or are poisoning the objects.
1928 */
1929 s->offset = size;
1930 size += sizeof(void *);
1931 }
1932
1933 if (flags & SLAB_STORE_USER)
1934 /*
1935 * Need to store information about allocs and frees after
1936 * the object.
1937 */
1938 size += 2 * sizeof(struct track);
1939
Christoph Lameterbe7b3fb2007-05-09 02:32:36 -07001940 if (flags & SLAB_RED_ZONE)
Christoph Lameter81819f02007-05-06 14:49:36 -07001941 /*
1942 * Add some empty padding so that we can catch
1943 * overwrites from earlier objects rather than let
1944 * tracking information or the free pointer be
1945 * corrupted if an user writes before the start
1946 * of the object.
1947 */
1948 size += sizeof(void *);
Christoph Lameter41ecc552007-05-09 02:32:44 -07001949#endif
Christoph Lameter672bba32007-05-09 02:32:39 -07001950
Christoph Lameter81819f02007-05-06 14:49:36 -07001951 /*
1952 * Determine the alignment based on various parameters that the
Christoph Lameter65c02d42007-05-09 02:32:35 -07001953 * user specified and the dynamic determination of cache line size
1954 * on bootup.
Christoph Lameter81819f02007-05-06 14:49:36 -07001955 */
1956 align = calculate_alignment(flags, align, s->objsize);
1957
1958 /*
1959 * SLUB stores one object immediately after another beginning from
1960 * offset 0. In order to align the objects we have to simply size
1961 * each object to conform to the alignment.
1962 */
1963 size = ALIGN(size, align);
1964 s->size = size;
1965
1966 s->order = calculate_order(size);
1967 if (s->order < 0)
1968 return 0;
1969
1970 /*
1971 * Determine the number of objects per slab
1972 */
1973 s->objects = (PAGE_SIZE << s->order) / size;
1974
1975 /*
1976 * Verify that the number of objects is within permitted limits.
1977 * The page->inuse field is only 16 bit wide! So we cannot have
1978 * more than 64k objects per slab.
1979 */
1980 if (!s->objects || s->objects > 65535)
1981 return 0;
1982 return 1;
1983
1984}
1985
Christoph Lameter81819f02007-05-06 14:49:36 -07001986static int kmem_cache_open(struct kmem_cache *s, gfp_t gfpflags,
1987 const char *name, size_t size,
1988 size_t align, unsigned long flags,
Christoph Lameterc59def92007-05-16 22:10:50 -07001989 void (*ctor)(void *, struct kmem_cache *, unsigned long))
Christoph Lameter81819f02007-05-06 14:49:36 -07001990{
1991 memset(s, 0, kmem_size);
1992 s->name = name;
1993 s->ctor = ctor;
Christoph Lameter81819f02007-05-06 14:49:36 -07001994 s->objsize = size;
1995 s->flags = flags;
1996 s->align = align;
Christoph Lameter41ecc552007-05-09 02:32:44 -07001997 kmem_cache_open_debug_check(s);
Christoph Lameter81819f02007-05-06 14:49:36 -07001998
1999 if (!calculate_sizes(s))
2000 goto error;
2001
2002 s->refcount = 1;
2003#ifdef CONFIG_NUMA
2004 s->defrag_ratio = 100;
2005#endif
2006
2007 if (init_kmem_cache_nodes(s, gfpflags & ~SLUB_DMA))
2008 return 1;
2009error:
2010 if (flags & SLAB_PANIC)
2011 panic("Cannot create slab %s size=%lu realsize=%u "
2012 "order=%u offset=%u flags=%lx\n",
2013 s->name, (unsigned long)size, s->size, s->order,
2014 s->offset, flags);
2015 return 0;
2016}
2017EXPORT_SYMBOL(kmem_cache_open);
2018
2019/*
2020 * Check if a given pointer is valid
2021 */
2022int kmem_ptr_validate(struct kmem_cache *s, const void *object)
2023{
2024 struct page * page;
Christoph Lameter81819f02007-05-06 14:49:36 -07002025
2026 page = get_object_page(object);
2027
2028 if (!page || s != page->slab)
2029 /* No slab or wrong slab */
2030 return 0;
2031
Christoph Lameterabcd08a2007-05-09 02:32:37 -07002032 if (!check_valid_pointer(s, page, object))
Christoph Lameter81819f02007-05-06 14:49:36 -07002033 return 0;
2034
2035 /*
2036 * We could also check if the object is on the slabs freelist.
2037 * But this would be too expensive and it seems that the main
2038 * purpose of kmem_ptr_valid is to check if the object belongs
2039 * to a certain slab.
2040 */
2041 return 1;
2042}
2043EXPORT_SYMBOL(kmem_ptr_validate);
2044
2045/*
2046 * Determine the size of a slab object
2047 */
2048unsigned int kmem_cache_size(struct kmem_cache *s)
2049{
2050 return s->objsize;
2051}
2052EXPORT_SYMBOL(kmem_cache_size);
2053
2054const char *kmem_cache_name(struct kmem_cache *s)
2055{
2056 return s->name;
2057}
2058EXPORT_SYMBOL(kmem_cache_name);
2059
2060/*
Christoph Lameter672bba32007-05-09 02:32:39 -07002061 * Attempt to free all slabs on a node. Return the number of slabs we
2062 * were unable to free.
Christoph Lameter81819f02007-05-06 14:49:36 -07002063 */
2064static int free_list(struct kmem_cache *s, struct kmem_cache_node *n,
2065 struct list_head *list)
2066{
2067 int slabs_inuse = 0;
2068 unsigned long flags;
2069 struct page *page, *h;
2070
2071 spin_lock_irqsave(&n->list_lock, flags);
2072 list_for_each_entry_safe(page, h, list, lru)
2073 if (!page->inuse) {
2074 list_del(&page->lru);
2075 discard_slab(s, page);
2076 } else
2077 slabs_inuse++;
2078 spin_unlock_irqrestore(&n->list_lock, flags);
2079 return slabs_inuse;
2080}
2081
2082/*
Christoph Lameter672bba32007-05-09 02:32:39 -07002083 * Release all resources used by a slab cache.
Christoph Lameter81819f02007-05-06 14:49:36 -07002084 */
2085static int kmem_cache_close(struct kmem_cache *s)
2086{
2087 int node;
2088
2089 flush_all(s);
2090
2091 /* Attempt to free all objects */
2092 for_each_online_node(node) {
2093 struct kmem_cache_node *n = get_node(s, node);
2094
Christoph Lameter2086d262007-05-06 14:49:46 -07002095 n->nr_partial -= free_list(s, n, &n->partial);
Christoph Lameter81819f02007-05-06 14:49:36 -07002096 if (atomic_long_read(&n->nr_slabs))
2097 return 1;
2098 }
2099 free_kmem_cache_nodes(s);
2100 return 0;
2101}
2102
2103/*
2104 * Close a cache and release the kmem_cache structure
2105 * (must be used for caches created using kmem_cache_create)
2106 */
2107void kmem_cache_destroy(struct kmem_cache *s)
2108{
2109 down_write(&slub_lock);
2110 s->refcount--;
2111 if (!s->refcount) {
2112 list_del(&s->list);
2113 if (kmem_cache_close(s))
2114 WARN_ON(1);
2115 sysfs_slab_remove(s);
2116 kfree(s);
2117 }
2118 up_write(&slub_lock);
2119}
2120EXPORT_SYMBOL(kmem_cache_destroy);
2121
2122/********************************************************************
2123 * Kmalloc subsystem
2124 *******************************************************************/
2125
2126struct kmem_cache kmalloc_caches[KMALLOC_SHIFT_HIGH + 1] __cacheline_aligned;
2127EXPORT_SYMBOL(kmalloc_caches);
2128
2129#ifdef CONFIG_ZONE_DMA
2130static struct kmem_cache *kmalloc_caches_dma[KMALLOC_SHIFT_HIGH + 1];
2131#endif
2132
2133static int __init setup_slub_min_order(char *str)
2134{
2135 get_option (&str, &slub_min_order);
2136
2137 return 1;
2138}
2139
2140__setup("slub_min_order=", setup_slub_min_order);
2141
2142static int __init setup_slub_max_order(char *str)
2143{
2144 get_option (&str, &slub_max_order);
2145
2146 return 1;
2147}
2148
2149__setup("slub_max_order=", setup_slub_max_order);
2150
2151static int __init setup_slub_min_objects(char *str)
2152{
2153 get_option (&str, &slub_min_objects);
2154
2155 return 1;
2156}
2157
2158__setup("slub_min_objects=", setup_slub_min_objects);
2159
2160static int __init setup_slub_nomerge(char *str)
2161{
2162 slub_nomerge = 1;
2163 return 1;
2164}
2165
2166__setup("slub_nomerge", setup_slub_nomerge);
2167
Christoph Lameter81819f02007-05-06 14:49:36 -07002168static struct kmem_cache *create_kmalloc_cache(struct kmem_cache *s,
2169 const char *name, int size, gfp_t gfp_flags)
2170{
2171 unsigned int flags = 0;
2172
2173 if (gfp_flags & SLUB_DMA)
2174 flags = SLAB_CACHE_DMA;
2175
2176 down_write(&slub_lock);
2177 if (!kmem_cache_open(s, gfp_flags, name, size, ARCH_KMALLOC_MINALIGN,
Christoph Lameterc59def92007-05-16 22:10:50 -07002178 flags, NULL))
Christoph Lameter81819f02007-05-06 14:49:36 -07002179 goto panic;
2180
2181 list_add(&s->list, &slab_caches);
2182 up_write(&slub_lock);
2183 if (sysfs_slab_add(s))
2184 goto panic;
2185 return s;
2186
2187panic:
2188 panic("Creation of kmalloc slab %s size=%d failed.\n", name, size);
2189}
2190
2191static struct kmem_cache *get_slab(size_t size, gfp_t flags)
2192{
2193 int index = kmalloc_index(size);
2194
Christoph Lameter614410d2007-05-06 14:49:38 -07002195 if (!index)
Christoph Lameter81819f02007-05-06 14:49:36 -07002196 return NULL;
2197
2198 /* Allocation too large? */
2199 BUG_ON(index < 0);
2200
2201#ifdef CONFIG_ZONE_DMA
2202 if ((flags & SLUB_DMA)) {
2203 struct kmem_cache *s;
2204 struct kmem_cache *x;
2205 char *text;
2206 size_t realsize;
2207
2208 s = kmalloc_caches_dma[index];
2209 if (s)
2210 return s;
2211
2212 /* Dynamically create dma cache */
2213 x = kmalloc(kmem_size, flags & ~SLUB_DMA);
2214 if (!x)
2215 panic("Unable to allocate memory for dma cache\n");
2216
2217 if (index <= KMALLOC_SHIFT_HIGH)
2218 realsize = 1 << index;
2219 else {
2220 if (index == 1)
2221 realsize = 96;
2222 else
2223 realsize = 192;
2224 }
2225
2226 text = kasprintf(flags & ~SLUB_DMA, "kmalloc_dma-%d",
2227 (unsigned int)realsize);
2228 s = create_kmalloc_cache(x, text, realsize, flags);
2229 kmalloc_caches_dma[index] = s;
2230 return s;
2231 }
2232#endif
2233 return &kmalloc_caches[index];
2234}
2235
2236void *__kmalloc(size_t size, gfp_t flags)
2237{
2238 struct kmem_cache *s = get_slab(size, flags);
2239
2240 if (s)
Christoph Lameter77c5e2d2007-05-06 14:49:42 -07002241 return slab_alloc(s, flags, -1, __builtin_return_address(0));
Christoph Lameter81819f02007-05-06 14:49:36 -07002242 return NULL;
2243}
2244EXPORT_SYMBOL(__kmalloc);
2245
2246#ifdef CONFIG_NUMA
2247void *__kmalloc_node(size_t size, gfp_t flags, int node)
2248{
2249 struct kmem_cache *s = get_slab(size, flags);
2250
2251 if (s)
Christoph Lameter77c5e2d2007-05-06 14:49:42 -07002252 return slab_alloc(s, flags, node, __builtin_return_address(0));
Christoph Lameter81819f02007-05-06 14:49:36 -07002253 return NULL;
2254}
2255EXPORT_SYMBOL(__kmalloc_node);
2256#endif
2257
2258size_t ksize(const void *object)
2259{
2260 struct page *page = get_object_page(object);
2261 struct kmem_cache *s;
2262
2263 BUG_ON(!page);
2264 s = page->slab;
2265 BUG_ON(!s);
2266
2267 /*
2268 * Debugging requires use of the padding between object
2269 * and whatever may come after it.
2270 */
2271 if (s->flags & (SLAB_RED_ZONE | SLAB_POISON))
2272 return s->objsize;
2273
2274 /*
2275 * If we have the need to store the freelist pointer
2276 * back there or track user information then we can
2277 * only use the space before that information.
2278 */
2279 if (s->flags & (SLAB_DESTROY_BY_RCU | SLAB_STORE_USER))
2280 return s->inuse;
2281
2282 /*
2283 * Else we can use all the padding etc for the allocation
2284 */
2285 return s->size;
2286}
2287EXPORT_SYMBOL(ksize);
2288
2289void kfree(const void *x)
2290{
2291 struct kmem_cache *s;
2292 struct page *page;
2293
2294 if (!x)
2295 return;
2296
Christoph Lameterb49af682007-05-06 14:49:41 -07002297 page = virt_to_head_page(x);
Christoph Lameter81819f02007-05-06 14:49:36 -07002298 s = page->slab;
2299
Christoph Lameter77c5e2d2007-05-06 14:49:42 -07002300 slab_free(s, page, (void *)x, __builtin_return_address(0));
Christoph Lameter81819f02007-05-06 14:49:36 -07002301}
2302EXPORT_SYMBOL(kfree);
2303
Christoph Lameter2086d262007-05-06 14:49:46 -07002304/*
Christoph Lameter672bba32007-05-09 02:32:39 -07002305 * kmem_cache_shrink removes empty slabs from the partial lists and sorts
2306 * the remaining slabs by the number of items in use. The slabs with the
2307 * most items in use come first. New allocations will then fill those up
2308 * and thus they can be removed from the partial lists.
2309 *
2310 * The slabs with the least items are placed last. This results in them
2311 * being allocated from last increasing the chance that the last objects
2312 * are freed in them.
Christoph Lameter2086d262007-05-06 14:49:46 -07002313 */
2314int kmem_cache_shrink(struct kmem_cache *s)
2315{
2316 int node;
2317 int i;
2318 struct kmem_cache_node *n;
2319 struct page *page;
2320 struct page *t;
2321 struct list_head *slabs_by_inuse =
2322 kmalloc(sizeof(struct list_head) * s->objects, GFP_KERNEL);
2323 unsigned long flags;
2324
2325 if (!slabs_by_inuse)
2326 return -ENOMEM;
2327
2328 flush_all(s);
2329 for_each_online_node(node) {
2330 n = get_node(s, node);
2331
2332 if (!n->nr_partial)
2333 continue;
2334
2335 for (i = 0; i < s->objects; i++)
2336 INIT_LIST_HEAD(slabs_by_inuse + i);
2337
2338 spin_lock_irqsave(&n->list_lock, flags);
2339
2340 /*
Christoph Lameter672bba32007-05-09 02:32:39 -07002341 * Build lists indexed by the items in use in each slab.
Christoph Lameter2086d262007-05-06 14:49:46 -07002342 *
Christoph Lameter672bba32007-05-09 02:32:39 -07002343 * Note that concurrent frees may occur while we hold the
2344 * list_lock. page->inuse here is the upper limit.
Christoph Lameter2086d262007-05-06 14:49:46 -07002345 */
2346 list_for_each_entry_safe(page, t, &n->partial, lru) {
2347 if (!page->inuse && slab_trylock(page)) {
2348 /*
2349 * Must hold slab lock here because slab_free
2350 * may have freed the last object and be
2351 * waiting to release the slab.
2352 */
2353 list_del(&page->lru);
2354 n->nr_partial--;
2355 slab_unlock(page);
2356 discard_slab(s, page);
2357 } else {
2358 if (n->nr_partial > MAX_PARTIAL)
2359 list_move(&page->lru,
2360 slabs_by_inuse + page->inuse);
2361 }
2362 }
2363
2364 if (n->nr_partial <= MAX_PARTIAL)
2365 goto out;
2366
2367 /*
Christoph Lameter672bba32007-05-09 02:32:39 -07002368 * Rebuild the partial list with the slabs filled up most
2369 * first and the least used slabs at the end.
Christoph Lameter2086d262007-05-06 14:49:46 -07002370 */
2371 for (i = s->objects - 1; i >= 0; i--)
2372 list_splice(slabs_by_inuse + i, n->partial.prev);
2373
2374 out:
2375 spin_unlock_irqrestore(&n->list_lock, flags);
2376 }
2377
2378 kfree(slabs_by_inuse);
2379 return 0;
2380}
2381EXPORT_SYMBOL(kmem_cache_shrink);
2382
Christoph Lameter81819f02007-05-06 14:49:36 -07002383/**
2384 * krealloc - reallocate memory. The contents will remain unchanged.
Christoph Lameter81819f02007-05-06 14:49:36 -07002385 * @p: object to reallocate memory for.
2386 * @new_size: how many bytes of memory are required.
2387 * @flags: the type of memory to allocate.
2388 *
2389 * The contents of the object pointed to are preserved up to the
2390 * lesser of the new and old sizes. If @p is %NULL, krealloc()
2391 * behaves exactly like kmalloc(). If @size is 0 and @p is not a
2392 * %NULL pointer, the object pointed to is freed.
2393 */
2394void *krealloc(const void *p, size_t new_size, gfp_t flags)
2395{
Christoph Lameter81819f02007-05-06 14:49:36 -07002396 void *ret;
Christoph Lameter1f99a282007-05-09 02:32:38 -07002397 size_t ks;
Christoph Lameter81819f02007-05-06 14:49:36 -07002398
2399 if (unlikely(!p))
2400 return kmalloc(new_size, flags);
2401
2402 if (unlikely(!new_size)) {
2403 kfree(p);
2404 return NULL;
2405 }
2406
Christoph Lameter1f99a282007-05-09 02:32:38 -07002407 ks = ksize(p);
2408 if (ks >= new_size)
Christoph Lameter81819f02007-05-06 14:49:36 -07002409 return (void *)p;
2410
2411 ret = kmalloc(new_size, flags);
2412 if (ret) {
Christoph Lameter1f99a282007-05-09 02:32:38 -07002413 memcpy(ret, p, min(new_size, ks));
Christoph Lameter81819f02007-05-06 14:49:36 -07002414 kfree(p);
2415 }
2416 return ret;
2417}
2418EXPORT_SYMBOL(krealloc);
2419
2420/********************************************************************
2421 * Basic setup of slabs
2422 *******************************************************************/
2423
2424void __init kmem_cache_init(void)
2425{
2426 int i;
2427
2428#ifdef CONFIG_NUMA
2429 /*
2430 * Must first have the slab cache available for the allocations of the
Christoph Lameter672bba32007-05-09 02:32:39 -07002431 * struct kmem_cache_node's. There is special bootstrap code in
Christoph Lameter81819f02007-05-06 14:49:36 -07002432 * kmem_cache_open for slab_state == DOWN.
2433 */
2434 create_kmalloc_cache(&kmalloc_caches[0], "kmem_cache_node",
2435 sizeof(struct kmem_cache_node), GFP_KERNEL);
2436#endif
2437
2438 /* Able to allocate the per node structures */
2439 slab_state = PARTIAL;
2440
2441 /* Caches that are not of the two-to-the-power-of size */
2442 create_kmalloc_cache(&kmalloc_caches[1],
2443 "kmalloc-96", 96, GFP_KERNEL);
2444 create_kmalloc_cache(&kmalloc_caches[2],
2445 "kmalloc-192", 192, GFP_KERNEL);
2446
2447 for (i = KMALLOC_SHIFT_LOW; i <= KMALLOC_SHIFT_HIGH; i++)
2448 create_kmalloc_cache(&kmalloc_caches[i],
2449 "kmalloc", 1 << i, GFP_KERNEL);
2450
2451 slab_state = UP;
2452
2453 /* Provide the correct kmalloc names now that the caches are up */
2454 for (i = KMALLOC_SHIFT_LOW; i <= KMALLOC_SHIFT_HIGH; i++)
2455 kmalloc_caches[i]. name =
2456 kasprintf(GFP_KERNEL, "kmalloc-%d", 1 << i);
2457
2458#ifdef CONFIG_SMP
2459 register_cpu_notifier(&slab_notifier);
2460#endif
2461
Christoph Lameterbcf889f2007-05-10 03:15:44 -07002462 kmem_size = offsetof(struct kmem_cache, cpu_slab) +
2463 nr_cpu_ids * sizeof(struct page *);
Christoph Lameter81819f02007-05-06 14:49:36 -07002464
2465 printk(KERN_INFO "SLUB: Genslabs=%d, HWalign=%d, Order=%d-%d, MinObjects=%d,"
2466 " Processors=%d, Nodes=%d\n",
Christoph Lameter65c02d42007-05-09 02:32:35 -07002467 KMALLOC_SHIFT_HIGH, cache_line_size(),
Christoph Lameter81819f02007-05-06 14:49:36 -07002468 slub_min_order, slub_max_order, slub_min_objects,
2469 nr_cpu_ids, nr_node_ids);
2470}
2471
2472/*
2473 * Find a mergeable slab cache
2474 */
2475static int slab_unmergeable(struct kmem_cache *s)
2476{
2477 if (slub_nomerge || (s->flags & SLUB_NEVER_MERGE))
2478 return 1;
2479
Christoph Lameterc59def92007-05-16 22:10:50 -07002480 if (s->ctor)
Christoph Lameter81819f02007-05-06 14:49:36 -07002481 return 1;
2482
2483 return 0;
2484}
2485
2486static struct kmem_cache *find_mergeable(size_t size,
2487 size_t align, unsigned long flags,
Christoph Lameterc59def92007-05-16 22:10:50 -07002488 void (*ctor)(void *, struct kmem_cache *, unsigned long))
Christoph Lameter81819f02007-05-06 14:49:36 -07002489{
2490 struct list_head *h;
2491
2492 if (slub_nomerge || (flags & SLUB_NEVER_MERGE))
2493 return NULL;
2494
Christoph Lameterc59def92007-05-16 22:10:50 -07002495 if (ctor)
Christoph Lameter81819f02007-05-06 14:49:36 -07002496 return NULL;
2497
2498 size = ALIGN(size, sizeof(void *));
2499 align = calculate_alignment(flags, align, size);
2500 size = ALIGN(size, align);
2501
2502 list_for_each(h, &slab_caches) {
2503 struct kmem_cache *s =
2504 container_of(h, struct kmem_cache, list);
2505
2506 if (slab_unmergeable(s))
2507 continue;
2508
2509 if (size > s->size)
2510 continue;
2511
2512 if (((flags | slub_debug) & SLUB_MERGE_SAME) !=
2513 (s->flags & SLUB_MERGE_SAME))
2514 continue;
2515 /*
2516 * Check if alignment is compatible.
2517 * Courtesy of Adrian Drzewiecki
2518 */
2519 if ((s->size & ~(align -1)) != s->size)
2520 continue;
2521
2522 if (s->size - size >= sizeof(void *))
2523 continue;
2524
2525 return s;
2526 }
2527 return NULL;
2528}
2529
2530struct kmem_cache *kmem_cache_create(const char *name, size_t size,
2531 size_t align, unsigned long flags,
2532 void (*ctor)(void *, struct kmem_cache *, unsigned long),
2533 void (*dtor)(void *, struct kmem_cache *, unsigned long))
2534{
2535 struct kmem_cache *s;
2536
Christoph Lameterc59def92007-05-16 22:10:50 -07002537 BUG_ON(dtor);
Christoph Lameter81819f02007-05-06 14:49:36 -07002538 down_write(&slub_lock);
Christoph Lameterc59def92007-05-16 22:10:50 -07002539 s = find_mergeable(size, align, flags, ctor);
Christoph Lameter81819f02007-05-06 14:49:36 -07002540 if (s) {
2541 s->refcount++;
2542 /*
2543 * Adjust the object sizes so that we clear
2544 * the complete object on kzalloc.
2545 */
2546 s->objsize = max(s->objsize, (int)size);
2547 s->inuse = max_t(int, s->inuse, ALIGN(size, sizeof(void *)));
2548 if (sysfs_slab_alias(s, name))
2549 goto err;
2550 } else {
2551 s = kmalloc(kmem_size, GFP_KERNEL);
2552 if (s && kmem_cache_open(s, GFP_KERNEL, name,
Christoph Lameterc59def92007-05-16 22:10:50 -07002553 size, align, flags, ctor)) {
Christoph Lameter81819f02007-05-06 14:49:36 -07002554 if (sysfs_slab_add(s)) {
2555 kfree(s);
2556 goto err;
2557 }
2558 list_add(&s->list, &slab_caches);
2559 } else
2560 kfree(s);
2561 }
2562 up_write(&slub_lock);
2563 return s;
2564
2565err:
2566 up_write(&slub_lock);
2567 if (flags & SLAB_PANIC)
2568 panic("Cannot create slabcache %s\n", name);
2569 else
2570 s = NULL;
2571 return s;
2572}
2573EXPORT_SYMBOL(kmem_cache_create);
2574
2575void *kmem_cache_zalloc(struct kmem_cache *s, gfp_t flags)
2576{
2577 void *x;
2578
Christoph Lameter77c5e2d2007-05-06 14:49:42 -07002579 x = slab_alloc(s, flags, -1, __builtin_return_address(0));
Christoph Lameter81819f02007-05-06 14:49:36 -07002580 if (x)
2581 memset(x, 0, s->objsize);
2582 return x;
2583}
2584EXPORT_SYMBOL(kmem_cache_zalloc);
2585
2586#ifdef CONFIG_SMP
2587static void for_all_slabs(void (*func)(struct kmem_cache *, int), int cpu)
2588{
2589 struct list_head *h;
2590
2591 down_read(&slub_lock);
2592 list_for_each(h, &slab_caches) {
2593 struct kmem_cache *s =
2594 container_of(h, struct kmem_cache, list);
2595
2596 func(s, cpu);
2597 }
2598 up_read(&slub_lock);
2599}
2600
2601/*
Christoph Lameter672bba32007-05-09 02:32:39 -07002602 * Use the cpu notifier to insure that the cpu slabs are flushed when
2603 * necessary.
Christoph Lameter81819f02007-05-06 14:49:36 -07002604 */
2605static int __cpuinit slab_cpuup_callback(struct notifier_block *nfb,
2606 unsigned long action, void *hcpu)
2607{
2608 long cpu = (long)hcpu;
2609
2610 switch (action) {
2611 case CPU_UP_CANCELED:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07002612 case CPU_UP_CANCELED_FROZEN:
Christoph Lameter81819f02007-05-06 14:49:36 -07002613 case CPU_DEAD:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07002614 case CPU_DEAD_FROZEN:
Christoph Lameter81819f02007-05-06 14:49:36 -07002615 for_all_slabs(__flush_cpu_slab, cpu);
2616 break;
2617 default:
2618 break;
2619 }
2620 return NOTIFY_OK;
2621}
2622
2623static struct notifier_block __cpuinitdata slab_notifier =
2624 { &slab_cpuup_callback, NULL, 0 };
2625
2626#endif
2627
Christoph Lameter81819f02007-05-06 14:49:36 -07002628void *__kmalloc_track_caller(size_t size, gfp_t gfpflags, void *caller)
2629{
2630 struct kmem_cache *s = get_slab(size, gfpflags);
Christoph Lameter81819f02007-05-06 14:49:36 -07002631
2632 if (!s)
2633 return NULL;
2634
Christoph Lameter77c5e2d2007-05-06 14:49:42 -07002635 return slab_alloc(s, gfpflags, -1, caller);
Christoph Lameter81819f02007-05-06 14:49:36 -07002636}
2637
2638void *__kmalloc_node_track_caller(size_t size, gfp_t gfpflags,
2639 int node, void *caller)
2640{
2641 struct kmem_cache *s = get_slab(size, gfpflags);
Christoph Lameter81819f02007-05-06 14:49:36 -07002642
2643 if (!s)
2644 return NULL;
2645
Christoph Lameter77c5e2d2007-05-06 14:49:42 -07002646 return slab_alloc(s, gfpflags, node, caller);
Christoph Lameter81819f02007-05-06 14:49:36 -07002647}
2648
Christoph Lameter41ecc552007-05-09 02:32:44 -07002649#if defined(CONFIG_SYSFS) && defined(CONFIG_SLUB_DEBUG)
Christoph Lameter53e15af2007-05-06 14:49:43 -07002650static int validate_slab(struct kmem_cache *s, struct page *page)
2651{
2652 void *p;
2653 void *addr = page_address(page);
Christoph Lameter7656c722007-05-09 02:32:40 -07002654 DECLARE_BITMAP(map, s->objects);
Christoph Lameter53e15af2007-05-06 14:49:43 -07002655
2656 if (!check_slab(s, page) ||
2657 !on_freelist(s, page, NULL))
2658 return 0;
2659
2660 /* Now we know that a valid freelist exists */
2661 bitmap_zero(map, s->objects);
2662
Christoph Lameter7656c722007-05-09 02:32:40 -07002663 for_each_free_object(p, s, page->freelist) {
2664 set_bit(slab_index(p, s, addr), map);
Christoph Lameter53e15af2007-05-06 14:49:43 -07002665 if (!check_object(s, page, p, 0))
2666 return 0;
2667 }
2668
Christoph Lameter7656c722007-05-09 02:32:40 -07002669 for_each_object(p, s, addr)
2670 if (!test_bit(slab_index(p, s, addr), map))
Christoph Lameter53e15af2007-05-06 14:49:43 -07002671 if (!check_object(s, page, p, 1))
2672 return 0;
2673 return 1;
2674}
2675
2676static void validate_slab_slab(struct kmem_cache *s, struct page *page)
2677{
2678 if (slab_trylock(page)) {
2679 validate_slab(s, page);
2680 slab_unlock(page);
2681 } else
2682 printk(KERN_INFO "SLUB %s: Skipped busy slab 0x%p\n",
2683 s->name, page);
2684
2685 if (s->flags & DEBUG_DEFAULT_FLAGS) {
Christoph Lameter35e5d7e2007-05-09 02:32:42 -07002686 if (!SlabDebug(page))
2687 printk(KERN_ERR "SLUB %s: SlabDebug not set "
Christoph Lameter53e15af2007-05-06 14:49:43 -07002688 "on slab 0x%p\n", s->name, page);
2689 } else {
Christoph Lameter35e5d7e2007-05-09 02:32:42 -07002690 if (SlabDebug(page))
2691 printk(KERN_ERR "SLUB %s: SlabDebug set on "
Christoph Lameter53e15af2007-05-06 14:49:43 -07002692 "slab 0x%p\n", s->name, page);
2693 }
2694}
2695
2696static int validate_slab_node(struct kmem_cache *s, struct kmem_cache_node *n)
2697{
2698 unsigned long count = 0;
2699 struct page *page;
2700 unsigned long flags;
2701
2702 spin_lock_irqsave(&n->list_lock, flags);
2703
2704 list_for_each_entry(page, &n->partial, lru) {
2705 validate_slab_slab(s, page);
2706 count++;
2707 }
2708 if (count != n->nr_partial)
2709 printk(KERN_ERR "SLUB %s: %ld partial slabs counted but "
2710 "counter=%ld\n", s->name, count, n->nr_partial);
2711
2712 if (!(s->flags & SLAB_STORE_USER))
2713 goto out;
2714
2715 list_for_each_entry(page, &n->full, lru) {
2716 validate_slab_slab(s, page);
2717 count++;
2718 }
2719 if (count != atomic_long_read(&n->nr_slabs))
2720 printk(KERN_ERR "SLUB: %s %ld slabs counted but "
2721 "counter=%ld\n", s->name, count,
2722 atomic_long_read(&n->nr_slabs));
2723
2724out:
2725 spin_unlock_irqrestore(&n->list_lock, flags);
2726 return count;
2727}
2728
2729static unsigned long validate_slab_cache(struct kmem_cache *s)
2730{
2731 int node;
2732 unsigned long count = 0;
2733
2734 flush_all(s);
2735 for_each_online_node(node) {
2736 struct kmem_cache_node *n = get_node(s, node);
2737
2738 count += validate_slab_node(s, n);
2739 }
2740 return count;
2741}
2742
Christoph Lameterb3459702007-05-09 02:32:41 -07002743#ifdef SLUB_RESILIENCY_TEST
2744static void resiliency_test(void)
2745{
2746 u8 *p;
2747
2748 printk(KERN_ERR "SLUB resiliency testing\n");
2749 printk(KERN_ERR "-----------------------\n");
2750 printk(KERN_ERR "A. Corruption after allocation\n");
2751
2752 p = kzalloc(16, GFP_KERNEL);
2753 p[16] = 0x12;
2754 printk(KERN_ERR "\n1. kmalloc-16: Clobber Redzone/next pointer"
2755 " 0x12->0x%p\n\n", p + 16);
2756
2757 validate_slab_cache(kmalloc_caches + 4);
2758
2759 /* Hmmm... The next two are dangerous */
2760 p = kzalloc(32, GFP_KERNEL);
2761 p[32 + sizeof(void *)] = 0x34;
2762 printk(KERN_ERR "\n2. kmalloc-32: Clobber next pointer/next slab"
2763 " 0x34 -> -0x%p\n", p);
2764 printk(KERN_ERR "If allocated object is overwritten then not detectable\n\n");
2765
2766 validate_slab_cache(kmalloc_caches + 5);
2767 p = kzalloc(64, GFP_KERNEL);
2768 p += 64 + (get_cycles() & 0xff) * sizeof(void *);
2769 *p = 0x56;
2770 printk(KERN_ERR "\n3. kmalloc-64: corrupting random byte 0x56->0x%p\n",
2771 p);
2772 printk(KERN_ERR "If allocated object is overwritten then not detectable\n\n");
2773 validate_slab_cache(kmalloc_caches + 6);
2774
2775 printk(KERN_ERR "\nB. Corruption after free\n");
2776 p = kzalloc(128, GFP_KERNEL);
2777 kfree(p);
2778 *p = 0x78;
2779 printk(KERN_ERR "1. kmalloc-128: Clobber first word 0x78->0x%p\n\n", p);
2780 validate_slab_cache(kmalloc_caches + 7);
2781
2782 p = kzalloc(256, GFP_KERNEL);
2783 kfree(p);
2784 p[50] = 0x9a;
2785 printk(KERN_ERR "\n2. kmalloc-256: Clobber 50th byte 0x9a->0x%p\n\n", p);
2786 validate_slab_cache(kmalloc_caches + 8);
2787
2788 p = kzalloc(512, GFP_KERNEL);
2789 kfree(p);
2790 p[512] = 0xab;
2791 printk(KERN_ERR "\n3. kmalloc-512: Clobber redzone 0xab->0x%p\n\n", p);
2792 validate_slab_cache(kmalloc_caches + 9);
2793}
2794#else
2795static void resiliency_test(void) {};
2796#endif
2797
Christoph Lameter88a420e2007-05-06 14:49:45 -07002798/*
Christoph Lameter672bba32007-05-09 02:32:39 -07002799 * Generate lists of code addresses where slabcache objects are allocated
Christoph Lameter88a420e2007-05-06 14:49:45 -07002800 * and freed.
2801 */
2802
2803struct location {
2804 unsigned long count;
2805 void *addr;
Christoph Lameter45edfa52007-05-09 02:32:45 -07002806 long long sum_time;
2807 long min_time;
2808 long max_time;
2809 long min_pid;
2810 long max_pid;
2811 cpumask_t cpus;
2812 nodemask_t nodes;
Christoph Lameter88a420e2007-05-06 14:49:45 -07002813};
2814
2815struct loc_track {
2816 unsigned long max;
2817 unsigned long count;
2818 struct location *loc;
2819};
2820
2821static void free_loc_track(struct loc_track *t)
2822{
2823 if (t->max)
2824 free_pages((unsigned long)t->loc,
2825 get_order(sizeof(struct location) * t->max));
2826}
2827
2828static int alloc_loc_track(struct loc_track *t, unsigned long max)
2829{
2830 struct location *l;
2831 int order;
2832
2833 if (!max)
2834 max = PAGE_SIZE / sizeof(struct location);
2835
2836 order = get_order(sizeof(struct location) * max);
2837
2838 l = (void *)__get_free_pages(GFP_KERNEL, order);
2839
2840 if (!l)
2841 return 0;
2842
2843 if (t->count) {
2844 memcpy(l, t->loc, sizeof(struct location) * t->count);
2845 free_loc_track(t);
2846 }
2847 t->max = max;
2848 t->loc = l;
2849 return 1;
2850}
2851
2852static int add_location(struct loc_track *t, struct kmem_cache *s,
Christoph Lameter45edfa52007-05-09 02:32:45 -07002853 const struct track *track)
Christoph Lameter88a420e2007-05-06 14:49:45 -07002854{
2855 long start, end, pos;
2856 struct location *l;
2857 void *caddr;
Christoph Lameter45edfa52007-05-09 02:32:45 -07002858 unsigned long age = jiffies - track->when;
Christoph Lameter88a420e2007-05-06 14:49:45 -07002859
2860 start = -1;
2861 end = t->count;
2862
2863 for ( ; ; ) {
2864 pos = start + (end - start + 1) / 2;
2865
2866 /*
2867 * There is nothing at "end". If we end up there
2868 * we need to add something to before end.
2869 */
2870 if (pos == end)
2871 break;
2872
2873 caddr = t->loc[pos].addr;
Christoph Lameter45edfa52007-05-09 02:32:45 -07002874 if (track->addr == caddr) {
2875
2876 l = &t->loc[pos];
2877 l->count++;
2878 if (track->when) {
2879 l->sum_time += age;
2880 if (age < l->min_time)
2881 l->min_time = age;
2882 if (age > l->max_time)
2883 l->max_time = age;
2884
2885 if (track->pid < l->min_pid)
2886 l->min_pid = track->pid;
2887 if (track->pid > l->max_pid)
2888 l->max_pid = track->pid;
2889
2890 cpu_set(track->cpu, l->cpus);
2891 }
2892 node_set(page_to_nid(virt_to_page(track)), l->nodes);
Christoph Lameter88a420e2007-05-06 14:49:45 -07002893 return 1;
2894 }
2895
Christoph Lameter45edfa52007-05-09 02:32:45 -07002896 if (track->addr < caddr)
Christoph Lameter88a420e2007-05-06 14:49:45 -07002897 end = pos;
2898 else
2899 start = pos;
2900 }
2901
2902 /*
Christoph Lameter672bba32007-05-09 02:32:39 -07002903 * Not found. Insert new tracking element.
Christoph Lameter88a420e2007-05-06 14:49:45 -07002904 */
2905 if (t->count >= t->max && !alloc_loc_track(t, 2 * t->max))
2906 return 0;
2907
2908 l = t->loc + pos;
2909 if (pos < t->count)
2910 memmove(l + 1, l,
2911 (t->count - pos) * sizeof(struct location));
2912 t->count++;
2913 l->count = 1;
Christoph Lameter45edfa52007-05-09 02:32:45 -07002914 l->addr = track->addr;
2915 l->sum_time = age;
2916 l->min_time = age;
2917 l->max_time = age;
2918 l->min_pid = track->pid;
2919 l->max_pid = track->pid;
2920 cpus_clear(l->cpus);
2921 cpu_set(track->cpu, l->cpus);
2922 nodes_clear(l->nodes);
2923 node_set(page_to_nid(virt_to_page(track)), l->nodes);
Christoph Lameter88a420e2007-05-06 14:49:45 -07002924 return 1;
2925}
2926
2927static void process_slab(struct loc_track *t, struct kmem_cache *s,
2928 struct page *page, enum track_item alloc)
2929{
2930 void *addr = page_address(page);
Christoph Lameter7656c722007-05-09 02:32:40 -07002931 DECLARE_BITMAP(map, s->objects);
Christoph Lameter88a420e2007-05-06 14:49:45 -07002932 void *p;
2933
2934 bitmap_zero(map, s->objects);
Christoph Lameter7656c722007-05-09 02:32:40 -07002935 for_each_free_object(p, s, page->freelist)
2936 set_bit(slab_index(p, s, addr), map);
Christoph Lameter88a420e2007-05-06 14:49:45 -07002937
Christoph Lameter7656c722007-05-09 02:32:40 -07002938 for_each_object(p, s, addr)
Christoph Lameter45edfa52007-05-09 02:32:45 -07002939 if (!test_bit(slab_index(p, s, addr), map))
2940 add_location(t, s, get_track(s, p, alloc));
Christoph Lameter88a420e2007-05-06 14:49:45 -07002941}
2942
2943static int list_locations(struct kmem_cache *s, char *buf,
2944 enum track_item alloc)
2945{
2946 int n = 0;
2947 unsigned long i;
2948 struct loc_track t;
2949 int node;
2950
2951 t.count = 0;
2952 t.max = 0;
2953
2954 /* Push back cpu slabs */
2955 flush_all(s);
2956
2957 for_each_online_node(node) {
2958 struct kmem_cache_node *n = get_node(s, node);
2959 unsigned long flags;
2960 struct page *page;
2961
2962 if (!atomic_read(&n->nr_slabs))
2963 continue;
2964
2965 spin_lock_irqsave(&n->list_lock, flags);
2966 list_for_each_entry(page, &n->partial, lru)
2967 process_slab(&t, s, page, alloc);
2968 list_for_each_entry(page, &n->full, lru)
2969 process_slab(&t, s, page, alloc);
2970 spin_unlock_irqrestore(&n->list_lock, flags);
2971 }
2972
2973 for (i = 0; i < t.count; i++) {
Christoph Lameter45edfa52007-05-09 02:32:45 -07002974 struct location *l = &t.loc[i];
Christoph Lameter88a420e2007-05-06 14:49:45 -07002975
2976 if (n > PAGE_SIZE - 100)
2977 break;
Christoph Lameter45edfa52007-05-09 02:32:45 -07002978 n += sprintf(buf + n, "%7ld ", l->count);
2979
2980 if (l->addr)
2981 n += sprint_symbol(buf + n, (unsigned long)l->addr);
Christoph Lameter88a420e2007-05-06 14:49:45 -07002982 else
2983 n += sprintf(buf + n, "<not-available>");
Christoph Lameter45edfa52007-05-09 02:32:45 -07002984
2985 if (l->sum_time != l->min_time) {
2986 unsigned long remainder;
2987
2988 n += sprintf(buf + n, " age=%ld/%ld/%ld",
2989 l->min_time,
2990 div_long_long_rem(l->sum_time, l->count, &remainder),
2991 l->max_time);
2992 } else
2993 n += sprintf(buf + n, " age=%ld",
2994 l->min_time);
2995
2996 if (l->min_pid != l->max_pid)
2997 n += sprintf(buf + n, " pid=%ld-%ld",
2998 l->min_pid, l->max_pid);
2999 else
3000 n += sprintf(buf + n, " pid=%ld",
3001 l->min_pid);
3002
3003 if (num_online_cpus() > 1 && !cpus_empty(l->cpus)) {
3004 n += sprintf(buf + n, " cpus=");
3005 n += cpulist_scnprintf(buf + n, PAGE_SIZE - n - 50,
3006 l->cpus);
3007 }
3008
3009 if (num_online_nodes() > 1 && !nodes_empty(l->nodes)) {
3010 n += sprintf(buf + n, " nodes=");
3011 n += nodelist_scnprintf(buf + n, PAGE_SIZE - n - 50,
3012 l->nodes);
3013 }
3014
Christoph Lameter88a420e2007-05-06 14:49:45 -07003015 n += sprintf(buf + n, "\n");
3016 }
3017
3018 free_loc_track(&t);
3019 if (!t.count)
3020 n += sprintf(buf, "No data\n");
3021 return n;
3022}
3023
Christoph Lameter81819f02007-05-06 14:49:36 -07003024static unsigned long count_partial(struct kmem_cache_node *n)
3025{
3026 unsigned long flags;
3027 unsigned long x = 0;
3028 struct page *page;
3029
3030 spin_lock_irqsave(&n->list_lock, flags);
3031 list_for_each_entry(page, &n->partial, lru)
3032 x += page->inuse;
3033 spin_unlock_irqrestore(&n->list_lock, flags);
3034 return x;
3035}
3036
3037enum slab_stat_type {
3038 SL_FULL,
3039 SL_PARTIAL,
3040 SL_CPU,
3041 SL_OBJECTS
3042};
3043
3044#define SO_FULL (1 << SL_FULL)
3045#define SO_PARTIAL (1 << SL_PARTIAL)
3046#define SO_CPU (1 << SL_CPU)
3047#define SO_OBJECTS (1 << SL_OBJECTS)
3048
3049static unsigned long slab_objects(struct kmem_cache *s,
3050 char *buf, unsigned long flags)
3051{
3052 unsigned long total = 0;
3053 int cpu;
3054 int node;
3055 int x;
3056 unsigned long *nodes;
3057 unsigned long *per_cpu;
3058
3059 nodes = kzalloc(2 * sizeof(unsigned long) * nr_node_ids, GFP_KERNEL);
3060 per_cpu = nodes + nr_node_ids;
3061
3062 for_each_possible_cpu(cpu) {
3063 struct page *page = s->cpu_slab[cpu];
3064 int node;
3065
3066 if (page) {
3067 node = page_to_nid(page);
3068 if (flags & SO_CPU) {
3069 int x = 0;
3070
3071 if (flags & SO_OBJECTS)
3072 x = page->inuse;
3073 else
3074 x = 1;
3075 total += x;
3076 nodes[node] += x;
3077 }
3078 per_cpu[node]++;
3079 }
3080 }
3081
3082 for_each_online_node(node) {
3083 struct kmem_cache_node *n = get_node(s, node);
3084
3085 if (flags & SO_PARTIAL) {
3086 if (flags & SO_OBJECTS)
3087 x = count_partial(n);
3088 else
3089 x = n->nr_partial;
3090 total += x;
3091 nodes[node] += x;
3092 }
3093
3094 if (flags & SO_FULL) {
3095 int full_slabs = atomic_read(&n->nr_slabs)
3096 - per_cpu[node]
3097 - n->nr_partial;
3098
3099 if (flags & SO_OBJECTS)
3100 x = full_slabs * s->objects;
3101 else
3102 x = full_slabs;
3103 total += x;
3104 nodes[node] += x;
3105 }
3106 }
3107
3108 x = sprintf(buf, "%lu", total);
3109#ifdef CONFIG_NUMA
3110 for_each_online_node(node)
3111 if (nodes[node])
3112 x += sprintf(buf + x, " N%d=%lu",
3113 node, nodes[node]);
3114#endif
3115 kfree(nodes);
3116 return x + sprintf(buf + x, "\n");
3117}
3118
3119static int any_slab_objects(struct kmem_cache *s)
3120{
3121 int node;
3122 int cpu;
3123
3124 for_each_possible_cpu(cpu)
3125 if (s->cpu_slab[cpu])
3126 return 1;
3127
3128 for_each_node(node) {
3129 struct kmem_cache_node *n = get_node(s, node);
3130
3131 if (n->nr_partial || atomic_read(&n->nr_slabs))
3132 return 1;
3133 }
3134 return 0;
3135}
3136
3137#define to_slab_attr(n) container_of(n, struct slab_attribute, attr)
3138#define to_slab(n) container_of(n, struct kmem_cache, kobj);
3139
3140struct slab_attribute {
3141 struct attribute attr;
3142 ssize_t (*show)(struct kmem_cache *s, char *buf);
3143 ssize_t (*store)(struct kmem_cache *s, const char *x, size_t count);
3144};
3145
3146#define SLAB_ATTR_RO(_name) \
3147 static struct slab_attribute _name##_attr = __ATTR_RO(_name)
3148
3149#define SLAB_ATTR(_name) \
3150 static struct slab_attribute _name##_attr = \
3151 __ATTR(_name, 0644, _name##_show, _name##_store)
3152
Christoph Lameter81819f02007-05-06 14:49:36 -07003153static ssize_t slab_size_show(struct kmem_cache *s, char *buf)
3154{
3155 return sprintf(buf, "%d\n", s->size);
3156}
3157SLAB_ATTR_RO(slab_size);
3158
3159static ssize_t align_show(struct kmem_cache *s, char *buf)
3160{
3161 return sprintf(buf, "%d\n", s->align);
3162}
3163SLAB_ATTR_RO(align);
3164
3165static ssize_t object_size_show(struct kmem_cache *s, char *buf)
3166{
3167 return sprintf(buf, "%d\n", s->objsize);
3168}
3169SLAB_ATTR_RO(object_size);
3170
3171static ssize_t objs_per_slab_show(struct kmem_cache *s, char *buf)
3172{
3173 return sprintf(buf, "%d\n", s->objects);
3174}
3175SLAB_ATTR_RO(objs_per_slab);
3176
3177static ssize_t order_show(struct kmem_cache *s, char *buf)
3178{
3179 return sprintf(buf, "%d\n", s->order);
3180}
3181SLAB_ATTR_RO(order);
3182
3183static ssize_t ctor_show(struct kmem_cache *s, char *buf)
3184{
3185 if (s->ctor) {
3186 int n = sprint_symbol(buf, (unsigned long)s->ctor);
3187
3188 return n + sprintf(buf + n, "\n");
3189 }
3190 return 0;
3191}
3192SLAB_ATTR_RO(ctor);
3193
Christoph Lameter81819f02007-05-06 14:49:36 -07003194static ssize_t aliases_show(struct kmem_cache *s, char *buf)
3195{
3196 return sprintf(buf, "%d\n", s->refcount - 1);
3197}
3198SLAB_ATTR_RO(aliases);
3199
3200static ssize_t slabs_show(struct kmem_cache *s, char *buf)
3201{
3202 return slab_objects(s, buf, SO_FULL|SO_PARTIAL|SO_CPU);
3203}
3204SLAB_ATTR_RO(slabs);
3205
3206static ssize_t partial_show(struct kmem_cache *s, char *buf)
3207{
3208 return slab_objects(s, buf, SO_PARTIAL);
3209}
3210SLAB_ATTR_RO(partial);
3211
3212static ssize_t cpu_slabs_show(struct kmem_cache *s, char *buf)
3213{
3214 return slab_objects(s, buf, SO_CPU);
3215}
3216SLAB_ATTR_RO(cpu_slabs);
3217
3218static ssize_t objects_show(struct kmem_cache *s, char *buf)
3219{
3220 return slab_objects(s, buf, SO_FULL|SO_PARTIAL|SO_CPU|SO_OBJECTS);
3221}
3222SLAB_ATTR_RO(objects);
3223
3224static ssize_t sanity_checks_show(struct kmem_cache *s, char *buf)
3225{
3226 return sprintf(buf, "%d\n", !!(s->flags & SLAB_DEBUG_FREE));
3227}
3228
3229static ssize_t sanity_checks_store(struct kmem_cache *s,
3230 const char *buf, size_t length)
3231{
3232 s->flags &= ~SLAB_DEBUG_FREE;
3233 if (buf[0] == '1')
3234 s->flags |= SLAB_DEBUG_FREE;
3235 return length;
3236}
3237SLAB_ATTR(sanity_checks);
3238
3239static ssize_t trace_show(struct kmem_cache *s, char *buf)
3240{
3241 return sprintf(buf, "%d\n", !!(s->flags & SLAB_TRACE));
3242}
3243
3244static ssize_t trace_store(struct kmem_cache *s, const char *buf,
3245 size_t length)
3246{
3247 s->flags &= ~SLAB_TRACE;
3248 if (buf[0] == '1')
3249 s->flags |= SLAB_TRACE;
3250 return length;
3251}
3252SLAB_ATTR(trace);
3253
3254static ssize_t reclaim_account_show(struct kmem_cache *s, char *buf)
3255{
3256 return sprintf(buf, "%d\n", !!(s->flags & SLAB_RECLAIM_ACCOUNT));
3257}
3258
3259static ssize_t reclaim_account_store(struct kmem_cache *s,
3260 const char *buf, size_t length)
3261{
3262 s->flags &= ~SLAB_RECLAIM_ACCOUNT;
3263 if (buf[0] == '1')
3264 s->flags |= SLAB_RECLAIM_ACCOUNT;
3265 return length;
3266}
3267SLAB_ATTR(reclaim_account);
3268
3269static ssize_t hwcache_align_show(struct kmem_cache *s, char *buf)
3270{
Christoph Lameter5af60832007-05-06 14:49:56 -07003271 return sprintf(buf, "%d\n", !!(s->flags & SLAB_HWCACHE_ALIGN));
Christoph Lameter81819f02007-05-06 14:49:36 -07003272}
3273SLAB_ATTR_RO(hwcache_align);
3274
3275#ifdef CONFIG_ZONE_DMA
3276static ssize_t cache_dma_show(struct kmem_cache *s, char *buf)
3277{
3278 return sprintf(buf, "%d\n", !!(s->flags & SLAB_CACHE_DMA));
3279}
3280SLAB_ATTR_RO(cache_dma);
3281#endif
3282
3283static ssize_t destroy_by_rcu_show(struct kmem_cache *s, char *buf)
3284{
3285 return sprintf(buf, "%d\n", !!(s->flags & SLAB_DESTROY_BY_RCU));
3286}
3287SLAB_ATTR_RO(destroy_by_rcu);
3288
3289static ssize_t red_zone_show(struct kmem_cache *s, char *buf)
3290{
3291 return sprintf(buf, "%d\n", !!(s->flags & SLAB_RED_ZONE));
3292}
3293
3294static ssize_t red_zone_store(struct kmem_cache *s,
3295 const char *buf, size_t length)
3296{
3297 if (any_slab_objects(s))
3298 return -EBUSY;
3299
3300 s->flags &= ~SLAB_RED_ZONE;
3301 if (buf[0] == '1')
3302 s->flags |= SLAB_RED_ZONE;
3303 calculate_sizes(s);
3304 return length;
3305}
3306SLAB_ATTR(red_zone);
3307
3308static ssize_t poison_show(struct kmem_cache *s, char *buf)
3309{
3310 return sprintf(buf, "%d\n", !!(s->flags & SLAB_POISON));
3311}
3312
3313static ssize_t poison_store(struct kmem_cache *s,
3314 const char *buf, size_t length)
3315{
3316 if (any_slab_objects(s))
3317 return -EBUSY;
3318
3319 s->flags &= ~SLAB_POISON;
3320 if (buf[0] == '1')
3321 s->flags |= SLAB_POISON;
3322 calculate_sizes(s);
3323 return length;
3324}
3325SLAB_ATTR(poison);
3326
3327static ssize_t store_user_show(struct kmem_cache *s, char *buf)
3328{
3329 return sprintf(buf, "%d\n", !!(s->flags & SLAB_STORE_USER));
3330}
3331
3332static ssize_t store_user_store(struct kmem_cache *s,
3333 const char *buf, size_t length)
3334{
3335 if (any_slab_objects(s))
3336 return -EBUSY;
3337
3338 s->flags &= ~SLAB_STORE_USER;
3339 if (buf[0] == '1')
3340 s->flags |= SLAB_STORE_USER;
3341 calculate_sizes(s);
3342 return length;
3343}
3344SLAB_ATTR(store_user);
3345
Christoph Lameter53e15af2007-05-06 14:49:43 -07003346static ssize_t validate_show(struct kmem_cache *s, char *buf)
3347{
3348 return 0;
3349}
3350
3351static ssize_t validate_store(struct kmem_cache *s,
3352 const char *buf, size_t length)
3353{
3354 if (buf[0] == '1')
3355 validate_slab_cache(s);
3356 else
3357 return -EINVAL;
3358 return length;
3359}
3360SLAB_ATTR(validate);
3361
Christoph Lameter2086d262007-05-06 14:49:46 -07003362static ssize_t shrink_show(struct kmem_cache *s, char *buf)
3363{
3364 return 0;
3365}
3366
3367static ssize_t shrink_store(struct kmem_cache *s,
3368 const char *buf, size_t length)
3369{
3370 if (buf[0] == '1') {
3371 int rc = kmem_cache_shrink(s);
3372
3373 if (rc)
3374 return rc;
3375 } else
3376 return -EINVAL;
3377 return length;
3378}
3379SLAB_ATTR(shrink);
3380
Christoph Lameter88a420e2007-05-06 14:49:45 -07003381static ssize_t alloc_calls_show(struct kmem_cache *s, char *buf)
3382{
3383 if (!(s->flags & SLAB_STORE_USER))
3384 return -ENOSYS;
3385 return list_locations(s, buf, TRACK_ALLOC);
3386}
3387SLAB_ATTR_RO(alloc_calls);
3388
3389static ssize_t free_calls_show(struct kmem_cache *s, char *buf)
3390{
3391 if (!(s->flags & SLAB_STORE_USER))
3392 return -ENOSYS;
3393 return list_locations(s, buf, TRACK_FREE);
3394}
3395SLAB_ATTR_RO(free_calls);
3396
Christoph Lameter81819f02007-05-06 14:49:36 -07003397#ifdef CONFIG_NUMA
3398static ssize_t defrag_ratio_show(struct kmem_cache *s, char *buf)
3399{
3400 return sprintf(buf, "%d\n", s->defrag_ratio / 10);
3401}
3402
3403static ssize_t defrag_ratio_store(struct kmem_cache *s,
3404 const char *buf, size_t length)
3405{
3406 int n = simple_strtoul(buf, NULL, 10);
3407
3408 if (n < 100)
3409 s->defrag_ratio = n * 10;
3410 return length;
3411}
3412SLAB_ATTR(defrag_ratio);
3413#endif
3414
3415static struct attribute * slab_attrs[] = {
3416 &slab_size_attr.attr,
3417 &object_size_attr.attr,
3418 &objs_per_slab_attr.attr,
3419 &order_attr.attr,
3420 &objects_attr.attr,
3421 &slabs_attr.attr,
3422 &partial_attr.attr,
3423 &cpu_slabs_attr.attr,
3424 &ctor_attr.attr,
Christoph Lameter81819f02007-05-06 14:49:36 -07003425 &aliases_attr.attr,
3426 &align_attr.attr,
3427 &sanity_checks_attr.attr,
3428 &trace_attr.attr,
3429 &hwcache_align_attr.attr,
3430 &reclaim_account_attr.attr,
3431 &destroy_by_rcu_attr.attr,
3432 &red_zone_attr.attr,
3433 &poison_attr.attr,
3434 &store_user_attr.attr,
Christoph Lameter53e15af2007-05-06 14:49:43 -07003435 &validate_attr.attr,
Christoph Lameter2086d262007-05-06 14:49:46 -07003436 &shrink_attr.attr,
Christoph Lameter88a420e2007-05-06 14:49:45 -07003437 &alloc_calls_attr.attr,
3438 &free_calls_attr.attr,
Christoph Lameter81819f02007-05-06 14:49:36 -07003439#ifdef CONFIG_ZONE_DMA
3440 &cache_dma_attr.attr,
3441#endif
3442#ifdef CONFIG_NUMA
3443 &defrag_ratio_attr.attr,
3444#endif
3445 NULL
3446};
3447
3448static struct attribute_group slab_attr_group = {
3449 .attrs = slab_attrs,
3450};
3451
3452static ssize_t slab_attr_show(struct kobject *kobj,
3453 struct attribute *attr,
3454 char *buf)
3455{
3456 struct slab_attribute *attribute;
3457 struct kmem_cache *s;
3458 int err;
3459
3460 attribute = to_slab_attr(attr);
3461 s = to_slab(kobj);
3462
3463 if (!attribute->show)
3464 return -EIO;
3465
3466 err = attribute->show(s, buf);
3467
3468 return err;
3469}
3470
3471static ssize_t slab_attr_store(struct kobject *kobj,
3472 struct attribute *attr,
3473 const char *buf, size_t len)
3474{
3475 struct slab_attribute *attribute;
3476 struct kmem_cache *s;
3477 int err;
3478
3479 attribute = to_slab_attr(attr);
3480 s = to_slab(kobj);
3481
3482 if (!attribute->store)
3483 return -EIO;
3484
3485 err = attribute->store(s, buf, len);
3486
3487 return err;
3488}
3489
3490static struct sysfs_ops slab_sysfs_ops = {
3491 .show = slab_attr_show,
3492 .store = slab_attr_store,
3493};
3494
3495static struct kobj_type slab_ktype = {
3496 .sysfs_ops = &slab_sysfs_ops,
3497};
3498
3499static int uevent_filter(struct kset *kset, struct kobject *kobj)
3500{
3501 struct kobj_type *ktype = get_ktype(kobj);
3502
3503 if (ktype == &slab_ktype)
3504 return 1;
3505 return 0;
3506}
3507
3508static struct kset_uevent_ops slab_uevent_ops = {
3509 .filter = uevent_filter,
3510};
3511
3512decl_subsys(slab, &slab_ktype, &slab_uevent_ops);
3513
3514#define ID_STR_LENGTH 64
3515
3516/* Create a unique string id for a slab cache:
3517 * format
3518 * :[flags-]size:[memory address of kmemcache]
3519 */
3520static char *create_unique_id(struct kmem_cache *s)
3521{
3522 char *name = kmalloc(ID_STR_LENGTH, GFP_KERNEL);
3523 char *p = name;
3524
3525 BUG_ON(!name);
3526
3527 *p++ = ':';
3528 /*
3529 * First flags affecting slabcache operations. We will only
3530 * get here for aliasable slabs so we do not need to support
3531 * too many flags. The flags here must cover all flags that
3532 * are matched during merging to guarantee that the id is
3533 * unique.
3534 */
3535 if (s->flags & SLAB_CACHE_DMA)
3536 *p++ = 'd';
3537 if (s->flags & SLAB_RECLAIM_ACCOUNT)
3538 *p++ = 'a';
3539 if (s->flags & SLAB_DEBUG_FREE)
3540 *p++ = 'F';
3541 if (p != name + 1)
3542 *p++ = '-';
3543 p += sprintf(p, "%07d", s->size);
3544 BUG_ON(p > name + ID_STR_LENGTH - 1);
3545 return name;
3546}
3547
3548static int sysfs_slab_add(struct kmem_cache *s)
3549{
3550 int err;
3551 const char *name;
3552 int unmergeable;
3553
3554 if (slab_state < SYSFS)
3555 /* Defer until later */
3556 return 0;
3557
3558 unmergeable = slab_unmergeable(s);
3559 if (unmergeable) {
3560 /*
3561 * Slabcache can never be merged so we can use the name proper.
3562 * This is typically the case for debug situations. In that
3563 * case we can catch duplicate names easily.
3564 */
Linus Torvalds0f9008e2007-05-07 12:31:58 -07003565 sysfs_remove_link(&slab_subsys.kobj, s->name);
Christoph Lameter81819f02007-05-06 14:49:36 -07003566 name = s->name;
3567 } else {
3568 /*
3569 * Create a unique name for the slab as a target
3570 * for the symlinks.
3571 */
3572 name = create_unique_id(s);
3573 }
3574
3575 kobj_set_kset_s(s, slab_subsys);
3576 kobject_set_name(&s->kobj, name);
3577 kobject_init(&s->kobj);
3578 err = kobject_add(&s->kobj);
3579 if (err)
3580 return err;
3581
3582 err = sysfs_create_group(&s->kobj, &slab_attr_group);
3583 if (err)
3584 return err;
3585 kobject_uevent(&s->kobj, KOBJ_ADD);
3586 if (!unmergeable) {
3587 /* Setup first alias */
3588 sysfs_slab_alias(s, s->name);
3589 kfree(name);
3590 }
3591 return 0;
3592}
3593
3594static void sysfs_slab_remove(struct kmem_cache *s)
3595{
3596 kobject_uevent(&s->kobj, KOBJ_REMOVE);
3597 kobject_del(&s->kobj);
3598}
3599
3600/*
3601 * Need to buffer aliases during bootup until sysfs becomes
3602 * available lest we loose that information.
3603 */
3604struct saved_alias {
3605 struct kmem_cache *s;
3606 const char *name;
3607 struct saved_alias *next;
3608};
3609
3610struct saved_alias *alias_list;
3611
3612static int sysfs_slab_alias(struct kmem_cache *s, const char *name)
3613{
3614 struct saved_alias *al;
3615
3616 if (slab_state == SYSFS) {
3617 /*
3618 * If we have a leftover link then remove it.
3619 */
Linus Torvalds0f9008e2007-05-07 12:31:58 -07003620 sysfs_remove_link(&slab_subsys.kobj, name);
3621 return sysfs_create_link(&slab_subsys.kobj,
Christoph Lameter81819f02007-05-06 14:49:36 -07003622 &s->kobj, name);
3623 }
3624
3625 al = kmalloc(sizeof(struct saved_alias), GFP_KERNEL);
3626 if (!al)
3627 return -ENOMEM;
3628
3629 al->s = s;
3630 al->name = name;
3631 al->next = alias_list;
3632 alias_list = al;
3633 return 0;
3634}
3635
3636static int __init slab_sysfs_init(void)
3637{
Christoph Lameter26a7bd02007-05-09 02:32:39 -07003638 struct list_head *h;
Christoph Lameter81819f02007-05-06 14:49:36 -07003639 int err;
3640
3641 err = subsystem_register(&slab_subsys);
3642 if (err) {
3643 printk(KERN_ERR "Cannot register slab subsystem.\n");
3644 return -ENOSYS;
3645 }
3646
Christoph Lameter26a7bd02007-05-09 02:32:39 -07003647 slab_state = SYSFS;
3648
3649 list_for_each(h, &slab_caches) {
3650 struct kmem_cache *s =
3651 container_of(h, struct kmem_cache, list);
3652
3653 err = sysfs_slab_add(s);
3654 BUG_ON(err);
3655 }
Christoph Lameter81819f02007-05-06 14:49:36 -07003656
3657 while (alias_list) {
3658 struct saved_alias *al = alias_list;
3659
3660 alias_list = alias_list->next;
3661 err = sysfs_slab_alias(al->s, al->name);
3662 BUG_ON(err);
3663 kfree(al);
3664 }
3665
3666 resiliency_test();
3667 return 0;
3668}
3669
3670__initcall(slab_sysfs_init);
Christoph Lameter81819f02007-05-06 14:49:36 -07003671#endif