blob: e0cf6213abc0fcfcd403c5d9ad0da079ad2214f9 [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 Lameter5577bd82007-05-16 22:10:56 -0700102#define FROZEN (1 << PG_active)
103
104#ifdef CONFIG_SLUB_DEBUG
105#define SLABDEBUG (1 << PG_error)
106#else
107#define SLABDEBUG 0
108#endif
109
Christoph Lameter4b6f0752007-05-16 22:10:53 -0700110static inline int SlabFrozen(struct page *page)
111{
Christoph Lameter5577bd82007-05-16 22:10:56 -0700112 return page->flags & FROZEN;
Christoph Lameter4b6f0752007-05-16 22:10:53 -0700113}
114
115static inline void SetSlabFrozen(struct page *page)
116{
Christoph Lameter5577bd82007-05-16 22:10:56 -0700117 page->flags |= FROZEN;
Christoph Lameter4b6f0752007-05-16 22:10:53 -0700118}
119
120static inline void ClearSlabFrozen(struct page *page)
121{
Christoph Lameter5577bd82007-05-16 22:10:56 -0700122 page->flags &= ~FROZEN;
Christoph Lameter4b6f0752007-05-16 22:10:53 -0700123}
124
Christoph Lameter35e5d7e2007-05-09 02:32:42 -0700125static inline int SlabDebug(struct page *page)
126{
Christoph Lameter5577bd82007-05-16 22:10:56 -0700127 return page->flags & SLABDEBUG;
Christoph Lameter35e5d7e2007-05-09 02:32:42 -0700128}
129
130static inline void SetSlabDebug(struct page *page)
131{
Christoph Lameter5577bd82007-05-16 22:10:56 -0700132 page->flags |= SLABDEBUG;
Christoph Lameter35e5d7e2007-05-09 02:32:42 -0700133}
134
135static inline void ClearSlabDebug(struct page *page)
136{
Christoph Lameter5577bd82007-05-16 22:10:56 -0700137 page->flags &= ~SLABDEBUG;
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 Lameter3ec09742007-05-16 22:11:00 -0700745static void trace(struct kmem_cache *s, struct page *page, void *object, int alloc)
746{
747 if (s->flags & SLAB_TRACE) {
748 printk(KERN_INFO "TRACE %s %s 0x%p inuse=%d fp=0x%p\n",
749 s->name,
750 alloc ? "alloc" : "free",
751 object, page->inuse,
752 page->freelist);
753
754 if (!alloc)
755 print_section("Object", (void *)object, s->objsize);
756
757 dump_stack();
758 }
759}
760
Christoph Lameter643b1132007-05-06 14:49:42 -0700761/*
Christoph Lameter672bba32007-05-09 02:32:39 -0700762 * Tracking of fully allocated slabs for debugging purposes.
Christoph Lameter643b1132007-05-06 14:49:42 -0700763 */
Christoph Lametere95eed52007-05-06 14:49:44 -0700764static void add_full(struct kmem_cache_node *n, struct page *page)
Christoph Lameter643b1132007-05-06 14:49:42 -0700765{
Christoph Lameter643b1132007-05-06 14:49:42 -0700766 spin_lock(&n->list_lock);
767 list_add(&page->lru, &n->full);
768 spin_unlock(&n->list_lock);
769}
770
771static void remove_full(struct kmem_cache *s, struct page *page)
772{
773 struct kmem_cache_node *n;
774
775 if (!(s->flags & SLAB_STORE_USER))
776 return;
777
778 n = get_node(s, page_to_nid(page));
779
780 spin_lock(&n->list_lock);
781 list_del(&page->lru);
782 spin_unlock(&n->list_lock);
783}
784
Christoph Lameter3ec09742007-05-16 22:11:00 -0700785static void setup_object_debug(struct kmem_cache *s, struct page *page,
786 void *object)
787{
788 if (!(s->flags & (SLAB_STORE_USER|SLAB_RED_ZONE|__OBJECT_POISON)))
789 return;
790
791 init_object(s, object, 0);
792 init_tracking(s, object);
793}
794
795static int alloc_debug_processing(struct kmem_cache *s, struct page *page,
796 void *object, void *addr)
Christoph Lameter81819f02007-05-06 14:49:36 -0700797{
798 if (!check_slab(s, page))
799 goto bad;
800
801 if (object && !on_freelist(s, page, object)) {
Christoph Lameter70d71222007-05-06 14:49:47 -0700802 slab_err(s, page, "Object 0x%p already allocated", object);
803 goto bad;
Christoph Lameter81819f02007-05-06 14:49:36 -0700804 }
805
806 if (!check_valid_pointer(s, page, object)) {
807 object_err(s, page, object, "Freelist Pointer check fails");
Christoph Lameter70d71222007-05-06 14:49:47 -0700808 goto bad;
Christoph Lameter81819f02007-05-06 14:49:36 -0700809 }
810
Christoph Lameter3ec09742007-05-16 22:11:00 -0700811 if (object && !check_object(s, page, object, 0))
Christoph Lameter81819f02007-05-06 14:49:36 -0700812 goto bad;
Christoph Lameter81819f02007-05-06 14:49:36 -0700813
Christoph Lameter3ec09742007-05-16 22:11:00 -0700814 /* Success perform special debug activities for allocs */
815 if (s->flags & SLAB_STORE_USER)
816 set_track(s, object, TRACK_ALLOC, addr);
817 trace(s, page, object, 1);
818 init_object(s, object, 1);
Christoph Lameter81819f02007-05-06 14:49:36 -0700819 return 1;
Christoph Lameter3ec09742007-05-16 22:11:00 -0700820
Christoph Lameter81819f02007-05-06 14:49:36 -0700821bad:
822 if (PageSlab(page)) {
823 /*
824 * If this is a slab page then lets do the best we can
825 * to avoid issues in the future. Marking all objects
Christoph Lameter672bba32007-05-09 02:32:39 -0700826 * as used avoids touching the remaining objects.
Christoph Lameter81819f02007-05-06 14:49:36 -0700827 */
828 printk(KERN_ERR "@@@ SLUB: %s slab 0x%p. Marking all objects used.\n",
829 s->name, page);
830 page->inuse = s->objects;
831 page->freelist = NULL;
832 /* Fix up fields that may be corrupted */
833 page->offset = s->offset / sizeof(void *);
834 }
835 return 0;
836}
837
Christoph Lameter3ec09742007-05-16 22:11:00 -0700838static int free_debug_processing(struct kmem_cache *s, struct page *page,
839 void *object, void *addr)
Christoph Lameter81819f02007-05-06 14:49:36 -0700840{
841 if (!check_slab(s, page))
842 goto fail;
843
844 if (!check_valid_pointer(s, page, object)) {
Christoph Lameter70d71222007-05-06 14:49:47 -0700845 slab_err(s, page, "Invalid object pointer 0x%p", object);
Christoph Lameter81819f02007-05-06 14:49:36 -0700846 goto fail;
847 }
848
849 if (on_freelist(s, page, object)) {
Christoph Lameter70d71222007-05-06 14:49:47 -0700850 slab_err(s, page, "Object 0x%p already free", object);
Christoph Lameter81819f02007-05-06 14:49:36 -0700851 goto fail;
852 }
853
854 if (!check_object(s, page, object, 1))
855 return 0;
856
857 if (unlikely(s != page->slab)) {
858 if (!PageSlab(page))
Christoph Lameter70d71222007-05-06 14:49:47 -0700859 slab_err(s, page, "Attempt to free object(0x%p) "
860 "outside of slab", object);
Christoph Lameter81819f02007-05-06 14:49:36 -0700861 else
Christoph Lameter70d71222007-05-06 14:49:47 -0700862 if (!page->slab) {
Christoph Lameter81819f02007-05-06 14:49:36 -0700863 printk(KERN_ERR
Christoph Lameter70d71222007-05-06 14:49:47 -0700864 "SLUB <none>: no slab for object 0x%p.\n",
Christoph Lameter81819f02007-05-06 14:49:36 -0700865 object);
Christoph Lameter70d71222007-05-06 14:49:47 -0700866 dump_stack();
867 }
Christoph Lameter81819f02007-05-06 14:49:36 -0700868 else
Christoph Lameter70d71222007-05-06 14:49:47 -0700869 slab_err(s, page, "object at 0x%p belongs "
870 "to slab %s", object, page->slab->name);
Christoph Lameter81819f02007-05-06 14:49:36 -0700871 goto fail;
872 }
Christoph Lameter3ec09742007-05-16 22:11:00 -0700873
874 /* Special debug activities for freeing objects */
875 if (!SlabFrozen(page) && !page->freelist)
876 remove_full(s, page);
877 if (s->flags & SLAB_STORE_USER)
878 set_track(s, object, TRACK_FREE, addr);
879 trace(s, page, object, 0);
880 init_object(s, object, 0);
Christoph Lameter81819f02007-05-06 14:49:36 -0700881 return 1;
Christoph Lameter3ec09742007-05-16 22:11:00 -0700882
Christoph Lameter81819f02007-05-06 14:49:36 -0700883fail:
Christoph Lameter81819f02007-05-06 14:49:36 -0700884 printk(KERN_ERR "@@@ SLUB: %s slab 0x%p object at 0x%p not freed.\n",
885 s->name, page, object);
886 return 0;
887}
888
Christoph Lameter41ecc552007-05-09 02:32:44 -0700889static int __init setup_slub_debug(char *str)
890{
891 if (!str || *str != '=')
892 slub_debug = DEBUG_DEFAULT_FLAGS;
893 else {
894 str++;
895 if (*str == 0 || *str == ',')
896 slub_debug = DEBUG_DEFAULT_FLAGS;
897 else
898 for( ;*str && *str != ','; str++)
899 switch (*str) {
900 case 'f' : case 'F' :
901 slub_debug |= SLAB_DEBUG_FREE;
902 break;
903 case 'z' : case 'Z' :
904 slub_debug |= SLAB_RED_ZONE;
905 break;
906 case 'p' : case 'P' :
907 slub_debug |= SLAB_POISON;
908 break;
909 case 'u' : case 'U' :
910 slub_debug |= SLAB_STORE_USER;
911 break;
912 case 't' : case 'T' :
913 slub_debug |= SLAB_TRACE;
914 break;
915 default:
916 printk(KERN_ERR "slub_debug option '%c' "
917 "unknown. skipped\n",*str);
918 }
919 }
920
921 if (*str == ',')
922 slub_debug_slabs = str + 1;
923 return 1;
924}
925
926__setup("slub_debug", setup_slub_debug);
927
928static void kmem_cache_open_debug_check(struct kmem_cache *s)
929{
930 /*
931 * The page->offset field is only 16 bit wide. This is an offset
932 * in units of words from the beginning of an object. If the slab
933 * size is bigger then we cannot move the free pointer behind the
934 * object anymore.
935 *
936 * On 32 bit platforms the limit is 256k. On 64bit platforms
937 * the limit is 512k.
938 *
Christoph Lameterc59def9f2007-05-16 22:10:50 -0700939 * Debugging or ctor may create a need to move the free
Christoph Lameter41ecc552007-05-09 02:32:44 -0700940 * pointer. Fail if this happens.
941 */
Christoph Lameter33e9e242007-05-23 13:57:56 -0700942 if (s->objsize >= 65535 * sizeof(void *)) {
Christoph Lameter41ecc552007-05-09 02:32:44 -0700943 BUG_ON(s->flags & (SLAB_RED_ZONE | SLAB_POISON |
944 SLAB_STORE_USER | SLAB_DESTROY_BY_RCU));
Christoph Lameterc59def9f2007-05-16 22:10:50 -0700945 BUG_ON(s->ctor);
Christoph Lameter41ecc552007-05-09 02:32:44 -0700946 }
947 else
948 /*
949 * Enable debugging if selected on the kernel commandline.
950 */
951 if (slub_debug && (!slub_debug_slabs ||
952 strncmp(slub_debug_slabs, s->name,
953 strlen(slub_debug_slabs)) == 0))
954 s->flags |= slub_debug;
955}
956#else
Christoph Lameter3ec09742007-05-16 22:11:00 -0700957static inline void setup_object_debug(struct kmem_cache *s,
958 struct page *page, void *object) {}
Christoph Lameter41ecc552007-05-09 02:32:44 -0700959
Christoph Lameter3ec09742007-05-16 22:11:00 -0700960static inline int alloc_debug_processing(struct kmem_cache *s,
961 struct page *page, void *object, void *addr) { return 0; }
Christoph Lameter41ecc552007-05-09 02:32:44 -0700962
Christoph Lameter3ec09742007-05-16 22:11:00 -0700963static inline int free_debug_processing(struct kmem_cache *s,
964 struct page *page, void *object, void *addr) { return 0; }
Christoph Lameter41ecc552007-05-09 02:32:44 -0700965
Christoph Lameter41ecc552007-05-09 02:32:44 -0700966static inline int slab_pad_check(struct kmem_cache *s, struct page *page)
967 { return 1; }
968static inline int check_object(struct kmem_cache *s, struct page *page,
969 void *object, int active) { return 1; }
Christoph Lameter3ec09742007-05-16 22:11:00 -0700970static inline void add_full(struct kmem_cache_node *n, struct page *page) {}
Christoph Lameter41ecc552007-05-09 02:32:44 -0700971static inline void kmem_cache_open_debug_check(struct kmem_cache *s) {}
972#define slub_debug 0
973#endif
Christoph Lameter81819f02007-05-06 14:49:36 -0700974/*
975 * Slab allocation and freeing
976 */
977static struct page *allocate_slab(struct kmem_cache *s, gfp_t flags, int node)
978{
979 struct page * page;
980 int pages = 1 << s->order;
981
982 if (s->order)
983 flags |= __GFP_COMP;
984
985 if (s->flags & SLAB_CACHE_DMA)
986 flags |= SLUB_DMA;
987
988 if (node == -1)
989 page = alloc_pages(flags, s->order);
990 else
991 page = alloc_pages_node(node, flags, s->order);
992
993 if (!page)
994 return NULL;
995
996 mod_zone_page_state(page_zone(page),
997 (s->flags & SLAB_RECLAIM_ACCOUNT) ?
998 NR_SLAB_RECLAIMABLE : NR_SLAB_UNRECLAIMABLE,
999 pages);
1000
1001 return page;
1002}
1003
1004static void setup_object(struct kmem_cache *s, struct page *page,
1005 void *object)
1006{
Christoph Lameter3ec09742007-05-16 22:11:00 -07001007 setup_object_debug(s, page, object);
Christoph Lameter4f104932007-05-06 14:50:17 -07001008 if (unlikely(s->ctor))
Christoph Lametera35afb82007-05-16 22:10:57 -07001009 s->ctor(object, s, 0);
Christoph Lameter81819f02007-05-06 14:49:36 -07001010}
1011
1012static struct page *new_slab(struct kmem_cache *s, gfp_t flags, int node)
1013{
1014 struct page *page;
1015 struct kmem_cache_node *n;
1016 void *start;
1017 void *end;
1018 void *last;
1019 void *p;
1020
Christoph Lameter81819f02007-05-06 14:49:36 -07001021 BUG_ON(flags & ~(GFP_DMA | GFP_LEVEL_MASK));
1022
1023 if (flags & __GFP_WAIT)
1024 local_irq_enable();
1025
1026 page = allocate_slab(s, flags & GFP_LEVEL_MASK, node);
1027 if (!page)
1028 goto out;
1029
1030 n = get_node(s, page_to_nid(page));
1031 if (n)
1032 atomic_long_inc(&n->nr_slabs);
1033 page->offset = s->offset / sizeof(void *);
1034 page->slab = s;
1035 page->flags |= 1 << PG_slab;
1036 if (s->flags & (SLAB_DEBUG_FREE | SLAB_RED_ZONE | SLAB_POISON |
1037 SLAB_STORE_USER | SLAB_TRACE))
Christoph Lameter35e5d7e2007-05-09 02:32:42 -07001038 SetSlabDebug(page);
Christoph Lameter81819f02007-05-06 14:49:36 -07001039
1040 start = page_address(page);
1041 end = start + s->objects * s->size;
1042
1043 if (unlikely(s->flags & SLAB_POISON))
1044 memset(start, POISON_INUSE, PAGE_SIZE << s->order);
1045
1046 last = start;
Christoph Lameter7656c722007-05-09 02:32:40 -07001047 for_each_object(p, s, start) {
Christoph Lameter81819f02007-05-06 14:49:36 -07001048 setup_object(s, page, last);
1049 set_freepointer(s, last, p);
1050 last = p;
1051 }
1052 setup_object(s, page, last);
1053 set_freepointer(s, last, NULL);
1054
1055 page->freelist = start;
Christoph Lameter894b8782007-05-10 03:15:16 -07001056 page->lockless_freelist = NULL;
Christoph Lameter81819f02007-05-06 14:49:36 -07001057 page->inuse = 0;
1058out:
1059 if (flags & __GFP_WAIT)
1060 local_irq_disable();
1061 return page;
1062}
1063
1064static void __free_slab(struct kmem_cache *s, struct page *page)
1065{
1066 int pages = 1 << s->order;
1067
Christoph Lameterc59def9f2007-05-16 22:10:50 -07001068 if (unlikely(SlabDebug(page))) {
Christoph Lameter81819f02007-05-06 14:49:36 -07001069 void *p;
1070
1071 slab_pad_check(s, page);
Christoph Lameterc59def9f2007-05-16 22:10:50 -07001072 for_each_object(p, s, page_address(page))
Christoph Lameter81819f02007-05-06 14:49:36 -07001073 check_object(s, page, p, 0);
Christoph Lameter81819f02007-05-06 14:49:36 -07001074 }
1075
1076 mod_zone_page_state(page_zone(page),
1077 (s->flags & SLAB_RECLAIM_ACCOUNT) ?
1078 NR_SLAB_RECLAIMABLE : NR_SLAB_UNRECLAIMABLE,
1079 - pages);
1080
1081 page->mapping = NULL;
1082 __free_pages(page, s->order);
1083}
1084
1085static void rcu_free_slab(struct rcu_head *h)
1086{
1087 struct page *page;
1088
1089 page = container_of((struct list_head *)h, struct page, lru);
1090 __free_slab(page->slab, page);
1091}
1092
1093static void free_slab(struct kmem_cache *s, struct page *page)
1094{
1095 if (unlikely(s->flags & SLAB_DESTROY_BY_RCU)) {
1096 /*
1097 * RCU free overloads the RCU head over the LRU
1098 */
1099 struct rcu_head *head = (void *)&page->lru;
1100
1101 call_rcu(head, rcu_free_slab);
1102 } else
1103 __free_slab(s, page);
1104}
1105
1106static void discard_slab(struct kmem_cache *s, struct page *page)
1107{
1108 struct kmem_cache_node *n = get_node(s, page_to_nid(page));
1109
1110 atomic_long_dec(&n->nr_slabs);
1111 reset_page_mapcount(page);
Christoph Lameter35e5d7e2007-05-09 02:32:42 -07001112 ClearSlabDebug(page);
1113 __ClearPageSlab(page);
Christoph Lameter81819f02007-05-06 14:49:36 -07001114 free_slab(s, page);
1115}
1116
1117/*
1118 * Per slab locking using the pagelock
1119 */
1120static __always_inline void slab_lock(struct page *page)
1121{
1122 bit_spin_lock(PG_locked, &page->flags);
1123}
1124
1125static __always_inline void slab_unlock(struct page *page)
1126{
1127 bit_spin_unlock(PG_locked, &page->flags);
1128}
1129
1130static __always_inline int slab_trylock(struct page *page)
1131{
1132 int rc = 1;
1133
1134 rc = bit_spin_trylock(PG_locked, &page->flags);
1135 return rc;
1136}
1137
1138/*
1139 * Management of partially allocated slabs
1140 */
Christoph Lametere95eed52007-05-06 14:49:44 -07001141static void add_partial_tail(struct kmem_cache_node *n, struct page *page)
Christoph Lameter81819f02007-05-06 14:49:36 -07001142{
Christoph Lametere95eed52007-05-06 14:49:44 -07001143 spin_lock(&n->list_lock);
1144 n->nr_partial++;
1145 list_add_tail(&page->lru, &n->partial);
1146 spin_unlock(&n->list_lock);
1147}
Christoph Lameter81819f02007-05-06 14:49:36 -07001148
Christoph Lametere95eed52007-05-06 14:49:44 -07001149static void add_partial(struct kmem_cache_node *n, struct page *page)
1150{
Christoph Lameter81819f02007-05-06 14:49:36 -07001151 spin_lock(&n->list_lock);
1152 n->nr_partial++;
1153 list_add(&page->lru, &n->partial);
1154 spin_unlock(&n->list_lock);
1155}
1156
1157static void remove_partial(struct kmem_cache *s,
1158 struct page *page)
1159{
1160 struct kmem_cache_node *n = get_node(s, page_to_nid(page));
1161
1162 spin_lock(&n->list_lock);
1163 list_del(&page->lru);
1164 n->nr_partial--;
1165 spin_unlock(&n->list_lock);
1166}
1167
1168/*
Christoph Lameter672bba32007-05-09 02:32:39 -07001169 * Lock slab and remove from the partial list.
Christoph Lameter81819f02007-05-06 14:49:36 -07001170 *
Christoph Lameter672bba32007-05-09 02:32:39 -07001171 * Must hold list_lock.
Christoph Lameter81819f02007-05-06 14:49:36 -07001172 */
Christoph Lameter4b6f0752007-05-16 22:10:53 -07001173static inline int lock_and_freeze_slab(struct kmem_cache_node *n, struct page *page)
Christoph Lameter81819f02007-05-06 14:49:36 -07001174{
1175 if (slab_trylock(page)) {
1176 list_del(&page->lru);
1177 n->nr_partial--;
Christoph Lameter4b6f0752007-05-16 22:10:53 -07001178 SetSlabFrozen(page);
Christoph Lameter81819f02007-05-06 14:49:36 -07001179 return 1;
1180 }
1181 return 0;
1182}
1183
1184/*
Christoph Lameter672bba32007-05-09 02:32:39 -07001185 * Try to allocate a partial slab from a specific node.
Christoph Lameter81819f02007-05-06 14:49:36 -07001186 */
1187static struct page *get_partial_node(struct kmem_cache_node *n)
1188{
1189 struct page *page;
1190
1191 /*
1192 * Racy check. If we mistakenly see no partial slabs then we
1193 * just allocate an empty slab. If we mistakenly try to get a
Christoph Lameter672bba32007-05-09 02:32:39 -07001194 * partial slab and there is none available then get_partials()
1195 * will return NULL.
Christoph Lameter81819f02007-05-06 14:49:36 -07001196 */
1197 if (!n || !n->nr_partial)
1198 return NULL;
1199
1200 spin_lock(&n->list_lock);
1201 list_for_each_entry(page, &n->partial, lru)
Christoph Lameter4b6f0752007-05-16 22:10:53 -07001202 if (lock_and_freeze_slab(n, page))
Christoph Lameter81819f02007-05-06 14:49:36 -07001203 goto out;
1204 page = NULL;
1205out:
1206 spin_unlock(&n->list_lock);
1207 return page;
1208}
1209
1210/*
Christoph Lameter672bba32007-05-09 02:32:39 -07001211 * Get a page from somewhere. Search in increasing NUMA distances.
Christoph Lameter81819f02007-05-06 14:49:36 -07001212 */
1213static struct page *get_any_partial(struct kmem_cache *s, gfp_t flags)
1214{
1215#ifdef CONFIG_NUMA
1216 struct zonelist *zonelist;
1217 struct zone **z;
1218 struct page *page;
1219
1220 /*
Christoph Lameter672bba32007-05-09 02:32:39 -07001221 * The defrag ratio allows a configuration of the tradeoffs between
1222 * inter node defragmentation and node local allocations. A lower
1223 * defrag_ratio increases the tendency to do local allocations
1224 * instead of attempting to obtain partial slabs from other nodes.
Christoph Lameter81819f02007-05-06 14:49:36 -07001225 *
Christoph Lameter672bba32007-05-09 02:32:39 -07001226 * If the defrag_ratio is set to 0 then kmalloc() always
1227 * returns node local objects. If the ratio is higher then kmalloc()
1228 * may return off node objects because partial slabs are obtained
1229 * from other nodes and filled up.
Christoph Lameter81819f02007-05-06 14:49:36 -07001230 *
1231 * If /sys/slab/xx/defrag_ratio is set to 100 (which makes
Christoph Lameter672bba32007-05-09 02:32:39 -07001232 * defrag_ratio = 1000) then every (well almost) allocation will
1233 * first attempt to defrag slab caches on other nodes. This means
1234 * scanning over all nodes to look for partial slabs which may be
1235 * expensive if we do it every time we are trying to find a slab
1236 * with available objects.
Christoph Lameter81819f02007-05-06 14:49:36 -07001237 */
1238 if (!s->defrag_ratio || get_cycles() % 1024 > s->defrag_ratio)
1239 return NULL;
1240
1241 zonelist = &NODE_DATA(slab_node(current->mempolicy))
1242 ->node_zonelists[gfp_zone(flags)];
1243 for (z = zonelist->zones; *z; z++) {
1244 struct kmem_cache_node *n;
1245
1246 n = get_node(s, zone_to_nid(*z));
1247
1248 if (n && cpuset_zone_allowed_hardwall(*z, flags) &&
Christoph Lametere95eed52007-05-06 14:49:44 -07001249 n->nr_partial > MIN_PARTIAL) {
Christoph Lameter81819f02007-05-06 14:49:36 -07001250 page = get_partial_node(n);
1251 if (page)
1252 return page;
1253 }
1254 }
1255#endif
1256 return NULL;
1257}
1258
1259/*
1260 * Get a partial page, lock it and return it.
1261 */
1262static struct page *get_partial(struct kmem_cache *s, gfp_t flags, int node)
1263{
1264 struct page *page;
1265 int searchnode = (node == -1) ? numa_node_id() : node;
1266
1267 page = get_partial_node(get_node(s, searchnode));
1268 if (page || (flags & __GFP_THISNODE))
1269 return page;
1270
1271 return get_any_partial(s, flags);
1272}
1273
1274/*
1275 * Move a page back to the lists.
1276 *
1277 * Must be called with the slab lock held.
1278 *
1279 * On exit the slab lock will have been dropped.
1280 */
Christoph Lameter4b6f0752007-05-16 22:10:53 -07001281static void unfreeze_slab(struct kmem_cache *s, struct page *page)
Christoph Lameter81819f02007-05-06 14:49:36 -07001282{
Christoph Lametere95eed52007-05-06 14:49:44 -07001283 struct kmem_cache_node *n = get_node(s, page_to_nid(page));
1284
Christoph Lameter4b6f0752007-05-16 22:10:53 -07001285 ClearSlabFrozen(page);
Christoph Lameter81819f02007-05-06 14:49:36 -07001286 if (page->inuse) {
Christoph Lametere95eed52007-05-06 14:49:44 -07001287
Christoph Lameter81819f02007-05-06 14:49:36 -07001288 if (page->freelist)
Christoph Lametere95eed52007-05-06 14:49:44 -07001289 add_partial(n, page);
Christoph Lameter35e5d7e2007-05-09 02:32:42 -07001290 else if (SlabDebug(page) && (s->flags & SLAB_STORE_USER))
Christoph Lametere95eed52007-05-06 14:49:44 -07001291 add_full(n, page);
Christoph Lameter81819f02007-05-06 14:49:36 -07001292 slab_unlock(page);
Christoph Lametere95eed52007-05-06 14:49:44 -07001293
Christoph Lameter81819f02007-05-06 14:49:36 -07001294 } else {
Christoph Lametere95eed52007-05-06 14:49:44 -07001295 if (n->nr_partial < MIN_PARTIAL) {
1296 /*
Christoph Lameter672bba32007-05-09 02:32:39 -07001297 * Adding an empty slab to the partial slabs in order
1298 * to avoid page allocator overhead. This slab needs
1299 * to come after the other slabs with objects in
1300 * order to fill them up. That way the size of the
1301 * partial list stays small. kmem_cache_shrink can
1302 * reclaim empty slabs from the partial list.
Christoph Lametere95eed52007-05-06 14:49:44 -07001303 */
1304 add_partial_tail(n, page);
1305 slab_unlock(page);
1306 } else {
1307 slab_unlock(page);
1308 discard_slab(s, page);
1309 }
Christoph Lameter81819f02007-05-06 14:49:36 -07001310 }
1311}
1312
1313/*
1314 * Remove the cpu slab
1315 */
1316static void deactivate_slab(struct kmem_cache *s, struct page *page, int cpu)
1317{
Christoph Lameter894b8782007-05-10 03:15:16 -07001318 /*
1319 * Merge cpu freelist into freelist. Typically we get here
1320 * because both freelists are empty. So this is unlikely
1321 * to occur.
1322 */
1323 while (unlikely(page->lockless_freelist)) {
1324 void **object;
1325
1326 /* Retrieve object from cpu_freelist */
1327 object = page->lockless_freelist;
1328 page->lockless_freelist = page->lockless_freelist[page->offset];
1329
1330 /* And put onto the regular freelist */
1331 object[page->offset] = page->freelist;
1332 page->freelist = object;
1333 page->inuse--;
1334 }
Christoph Lameter81819f02007-05-06 14:49:36 -07001335 s->cpu_slab[cpu] = NULL;
Christoph Lameter4b6f0752007-05-16 22:10:53 -07001336 unfreeze_slab(s, page);
Christoph Lameter81819f02007-05-06 14:49:36 -07001337}
1338
1339static void flush_slab(struct kmem_cache *s, struct page *page, int cpu)
1340{
1341 slab_lock(page);
1342 deactivate_slab(s, page, cpu);
1343}
1344
1345/*
1346 * Flush cpu slab.
1347 * Called from IPI handler with interrupts disabled.
1348 */
1349static void __flush_cpu_slab(struct kmem_cache *s, int cpu)
1350{
1351 struct page *page = s->cpu_slab[cpu];
1352
1353 if (likely(page))
1354 flush_slab(s, page, cpu);
1355}
1356
1357static void flush_cpu_slab(void *d)
1358{
1359 struct kmem_cache *s = d;
1360 int cpu = smp_processor_id();
1361
1362 __flush_cpu_slab(s, cpu);
1363}
1364
1365static void flush_all(struct kmem_cache *s)
1366{
1367#ifdef CONFIG_SMP
1368 on_each_cpu(flush_cpu_slab, s, 1, 1);
1369#else
1370 unsigned long flags;
1371
1372 local_irq_save(flags);
1373 flush_cpu_slab(s);
1374 local_irq_restore(flags);
1375#endif
1376}
1377
1378/*
Christoph Lameter894b8782007-05-10 03:15:16 -07001379 * Slow path. The lockless freelist is empty or we need to perform
1380 * debugging duties.
Christoph Lameter81819f02007-05-06 14:49:36 -07001381 *
Christoph Lameter894b8782007-05-10 03:15:16 -07001382 * Interrupts are disabled.
Christoph Lameter81819f02007-05-06 14:49:36 -07001383 *
Christoph Lameter894b8782007-05-10 03:15:16 -07001384 * Processing is still very fast if new objects have been freed to the
1385 * regular freelist. In that case we simply take over the regular freelist
1386 * as the lockless freelist and zap the regular freelist.
Christoph Lameter81819f02007-05-06 14:49:36 -07001387 *
Christoph Lameter894b8782007-05-10 03:15:16 -07001388 * If that is not working then we fall back to the partial lists. We take the
1389 * first element of the freelist as the object to allocate now and move the
1390 * rest of the freelist to the lockless freelist.
1391 *
1392 * And if we were unable to get a new slab from the partial slab lists then
1393 * we need to allocate a new slab. This is slowest path since we may sleep.
Christoph Lameter81819f02007-05-06 14:49:36 -07001394 */
Christoph Lameter894b8782007-05-10 03:15:16 -07001395static void *__slab_alloc(struct kmem_cache *s,
1396 gfp_t gfpflags, int node, void *addr, struct page *page)
Christoph Lameter81819f02007-05-06 14:49:36 -07001397{
Christoph Lameter81819f02007-05-06 14:49:36 -07001398 void **object;
Christoph Lameter894b8782007-05-10 03:15:16 -07001399 int cpu = smp_processor_id();
Christoph Lameter81819f02007-05-06 14:49:36 -07001400
Christoph Lameter81819f02007-05-06 14:49:36 -07001401 if (!page)
1402 goto new_slab;
1403
1404 slab_lock(page);
1405 if (unlikely(node != -1 && page_to_nid(page) != node))
1406 goto another_slab;
Christoph Lameter894b8782007-05-10 03:15:16 -07001407load_freelist:
Christoph Lameter81819f02007-05-06 14:49:36 -07001408 object = page->freelist;
1409 if (unlikely(!object))
1410 goto another_slab;
Christoph Lameter35e5d7e2007-05-09 02:32:42 -07001411 if (unlikely(SlabDebug(page)))
Christoph Lameter81819f02007-05-06 14:49:36 -07001412 goto debug;
1413
Christoph Lameter894b8782007-05-10 03:15:16 -07001414 object = page->freelist;
1415 page->lockless_freelist = object[page->offset];
1416 page->inuse = s->objects;
1417 page->freelist = NULL;
Christoph Lameter81819f02007-05-06 14:49:36 -07001418 slab_unlock(page);
Christoph Lameter81819f02007-05-06 14:49:36 -07001419 return object;
1420
1421another_slab:
1422 deactivate_slab(s, page, cpu);
1423
1424new_slab:
1425 page = get_partial(s, gfpflags, node);
Christoph Lameter894b8782007-05-10 03:15:16 -07001426 if (page) {
Christoph Lameter81819f02007-05-06 14:49:36 -07001427 s->cpu_slab[cpu] = page;
Christoph Lameter894b8782007-05-10 03:15:16 -07001428 goto load_freelist;
Christoph Lameter81819f02007-05-06 14:49:36 -07001429 }
1430
1431 page = new_slab(s, gfpflags, node);
1432 if (page) {
1433 cpu = smp_processor_id();
1434 if (s->cpu_slab[cpu]) {
1435 /*
Christoph Lameter672bba32007-05-09 02:32:39 -07001436 * Someone else populated the cpu_slab while we
1437 * enabled interrupts, or we have gotten scheduled
1438 * on another cpu. The page may not be on the
1439 * requested node even if __GFP_THISNODE was
1440 * specified. So we need to recheck.
Christoph Lameter81819f02007-05-06 14:49:36 -07001441 */
1442 if (node == -1 ||
1443 page_to_nid(s->cpu_slab[cpu]) == node) {
1444 /*
1445 * Current cpuslab is acceptable and we
1446 * want the current one since its cache hot
1447 */
1448 discard_slab(s, page);
1449 page = s->cpu_slab[cpu];
1450 slab_lock(page);
Christoph Lameter894b8782007-05-10 03:15:16 -07001451 goto load_freelist;
Christoph Lameter81819f02007-05-06 14:49:36 -07001452 }
Christoph Lameter672bba32007-05-09 02:32:39 -07001453 /* New slab does not fit our expectations */
Christoph Lameter81819f02007-05-06 14:49:36 -07001454 flush_slab(s, s->cpu_slab[cpu], cpu);
1455 }
1456 slab_lock(page);
Christoph Lameter4b6f0752007-05-16 22:10:53 -07001457 SetSlabFrozen(page);
1458 s->cpu_slab[cpu] = page;
1459 goto load_freelist;
Christoph Lameter81819f02007-05-06 14:49:36 -07001460 }
Christoph Lameter81819f02007-05-06 14:49:36 -07001461 return NULL;
1462debug:
Christoph Lameter894b8782007-05-10 03:15:16 -07001463 object = page->freelist;
Christoph Lameter3ec09742007-05-16 22:11:00 -07001464 if (!alloc_debug_processing(s, page, object, addr))
Christoph Lameter81819f02007-05-06 14:49:36 -07001465 goto another_slab;
Christoph Lameter894b8782007-05-10 03:15:16 -07001466
1467 page->inuse++;
1468 page->freelist = object[page->offset];
1469 slab_unlock(page);
1470 return object;
1471}
1472
1473/*
1474 * Inlined fastpath so that allocation functions (kmalloc, kmem_cache_alloc)
1475 * have the fastpath folded into their functions. So no function call
1476 * overhead for requests that can be satisfied on the fastpath.
1477 *
1478 * The fastpath works by first checking if the lockless freelist can be used.
1479 * If not then __slab_alloc is called for slow processing.
1480 *
1481 * Otherwise we can simply pick the next object from the lockless free list.
1482 */
1483static void __always_inline *slab_alloc(struct kmem_cache *s,
1484 gfp_t gfpflags, int node, void *addr)
1485{
1486 struct page *page;
1487 void **object;
1488 unsigned long flags;
1489
1490 local_irq_save(flags);
1491 page = s->cpu_slab[smp_processor_id()];
1492 if (unlikely(!page || !page->lockless_freelist ||
1493 (node != -1 && page_to_nid(page) != node)))
1494
1495 object = __slab_alloc(s, gfpflags, node, addr, page);
1496
1497 else {
1498 object = page->lockless_freelist;
1499 page->lockless_freelist = object[page->offset];
1500 }
1501 local_irq_restore(flags);
1502 return object;
Christoph Lameter81819f02007-05-06 14:49:36 -07001503}
1504
1505void *kmem_cache_alloc(struct kmem_cache *s, gfp_t gfpflags)
1506{
Christoph Lameter77c5e2d2007-05-06 14:49:42 -07001507 return slab_alloc(s, gfpflags, -1, __builtin_return_address(0));
Christoph Lameter81819f02007-05-06 14:49:36 -07001508}
1509EXPORT_SYMBOL(kmem_cache_alloc);
1510
1511#ifdef CONFIG_NUMA
1512void *kmem_cache_alloc_node(struct kmem_cache *s, gfp_t gfpflags, int node)
1513{
Christoph Lameter77c5e2d2007-05-06 14:49:42 -07001514 return slab_alloc(s, gfpflags, node, __builtin_return_address(0));
Christoph Lameter81819f02007-05-06 14:49:36 -07001515}
1516EXPORT_SYMBOL(kmem_cache_alloc_node);
1517#endif
1518
1519/*
Christoph Lameter894b8782007-05-10 03:15:16 -07001520 * Slow patch handling. This may still be called frequently since objects
1521 * have a longer lifetime than the cpu slabs in most processing loads.
Christoph Lameter81819f02007-05-06 14:49:36 -07001522 *
Christoph Lameter894b8782007-05-10 03:15:16 -07001523 * So we still attempt to reduce cache line usage. Just take the slab
1524 * lock and free the item. If there is no additional partial page
1525 * handling required then we can return immediately.
Christoph Lameter81819f02007-05-06 14:49:36 -07001526 */
Christoph Lameter894b8782007-05-10 03:15:16 -07001527static void __slab_free(struct kmem_cache *s, struct page *page,
Christoph Lameter77c5e2d2007-05-06 14:49:42 -07001528 void *x, void *addr)
Christoph Lameter81819f02007-05-06 14:49:36 -07001529{
1530 void *prior;
1531 void **object = (void *)x;
Christoph Lameter81819f02007-05-06 14:49:36 -07001532
Christoph Lameter81819f02007-05-06 14:49:36 -07001533 slab_lock(page);
1534
Christoph Lameter35e5d7e2007-05-09 02:32:42 -07001535 if (unlikely(SlabDebug(page)))
Christoph Lameter81819f02007-05-06 14:49:36 -07001536 goto debug;
1537checks_ok:
1538 prior = object[page->offset] = page->freelist;
1539 page->freelist = object;
1540 page->inuse--;
1541
Christoph Lameter4b6f0752007-05-16 22:10:53 -07001542 if (unlikely(SlabFrozen(page)))
Christoph Lameter81819f02007-05-06 14:49:36 -07001543 goto out_unlock;
1544
1545 if (unlikely(!page->inuse))
1546 goto slab_empty;
1547
1548 /*
1549 * Objects left in the slab. If it
1550 * was not on the partial list before
1551 * then add it.
1552 */
1553 if (unlikely(!prior))
Christoph Lametere95eed52007-05-06 14:49:44 -07001554 add_partial(get_node(s, page_to_nid(page)), page);
Christoph Lameter81819f02007-05-06 14:49:36 -07001555
1556out_unlock:
1557 slab_unlock(page);
Christoph Lameter81819f02007-05-06 14:49:36 -07001558 return;
1559
1560slab_empty:
1561 if (prior)
1562 /*
Christoph Lameter672bba32007-05-09 02:32:39 -07001563 * Slab still on the partial list.
Christoph Lameter81819f02007-05-06 14:49:36 -07001564 */
1565 remove_partial(s, page);
1566
1567 slab_unlock(page);
1568 discard_slab(s, page);
Christoph Lameter81819f02007-05-06 14:49:36 -07001569 return;
1570
1571debug:
Christoph Lameter3ec09742007-05-16 22:11:00 -07001572 if (!free_debug_processing(s, page, x, addr))
Christoph Lameter77c5e2d2007-05-06 14:49:42 -07001573 goto out_unlock;
Christoph Lameter77c5e2d2007-05-06 14:49:42 -07001574 goto checks_ok;
Christoph Lameter81819f02007-05-06 14:49:36 -07001575}
1576
Christoph Lameter894b8782007-05-10 03:15:16 -07001577/*
1578 * Fastpath with forced inlining to produce a kfree and kmem_cache_free that
1579 * can perform fastpath freeing without additional function calls.
1580 *
1581 * The fastpath is only possible if we are freeing to the current cpu slab
1582 * of this processor. This typically the case if we have just allocated
1583 * the item before.
1584 *
1585 * If fastpath is not possible then fall back to __slab_free where we deal
1586 * with all sorts of special processing.
1587 */
1588static void __always_inline slab_free(struct kmem_cache *s,
1589 struct page *page, void *x, void *addr)
1590{
1591 void **object = (void *)x;
1592 unsigned long flags;
1593
1594 local_irq_save(flags);
1595 if (likely(page == s->cpu_slab[smp_processor_id()] &&
1596 !SlabDebug(page))) {
1597 object[page->offset] = page->lockless_freelist;
1598 page->lockless_freelist = object;
1599 } else
1600 __slab_free(s, page, x, addr);
1601
1602 local_irq_restore(flags);
1603}
1604
Christoph Lameter81819f02007-05-06 14:49:36 -07001605void kmem_cache_free(struct kmem_cache *s, void *x)
1606{
Christoph Lameter77c5e2d2007-05-06 14:49:42 -07001607 struct page *page;
Christoph Lameter81819f02007-05-06 14:49:36 -07001608
Christoph Lameterb49af682007-05-06 14:49:41 -07001609 page = virt_to_head_page(x);
Christoph Lameter81819f02007-05-06 14:49:36 -07001610
Christoph Lameter77c5e2d2007-05-06 14:49:42 -07001611 slab_free(s, page, x, __builtin_return_address(0));
Christoph Lameter81819f02007-05-06 14:49:36 -07001612}
1613EXPORT_SYMBOL(kmem_cache_free);
1614
1615/* Figure out on which slab object the object resides */
1616static struct page *get_object_page(const void *x)
1617{
Christoph Lameterb49af682007-05-06 14:49:41 -07001618 struct page *page = virt_to_head_page(x);
Christoph Lameter81819f02007-05-06 14:49:36 -07001619
1620 if (!PageSlab(page))
1621 return NULL;
1622
1623 return page;
1624}
1625
1626/*
Christoph Lameter672bba32007-05-09 02:32:39 -07001627 * Object placement in a slab is made very easy because we always start at
1628 * offset 0. If we tune the size of the object to the alignment then we can
1629 * get the required alignment by putting one properly sized object after
1630 * another.
Christoph Lameter81819f02007-05-06 14:49:36 -07001631 *
1632 * Notice that the allocation order determines the sizes of the per cpu
1633 * caches. Each processor has always one slab available for allocations.
1634 * Increasing the allocation order reduces the number of times that slabs
Christoph Lameter672bba32007-05-09 02:32:39 -07001635 * must be moved on and off the partial lists and is therefore a factor in
Christoph Lameter81819f02007-05-06 14:49:36 -07001636 * locking overhead.
Christoph Lameter81819f02007-05-06 14:49:36 -07001637 */
1638
1639/*
1640 * Mininum / Maximum order of slab pages. This influences locking overhead
1641 * and slab fragmentation. A higher order reduces the number of partial slabs
1642 * and increases the number of allocations possible without having to
1643 * take the list_lock.
1644 */
1645static int slub_min_order;
1646static int slub_max_order = DEFAULT_MAX_ORDER;
Christoph Lameter81819f02007-05-06 14:49:36 -07001647static int slub_min_objects = DEFAULT_MIN_OBJECTS;
1648
1649/*
1650 * Merge control. If this is set then no merging of slab caches will occur.
Christoph Lameter672bba32007-05-09 02:32:39 -07001651 * (Could be removed. This was introduced to pacify the merge skeptics.)
Christoph Lameter81819f02007-05-06 14:49:36 -07001652 */
1653static int slub_nomerge;
1654
1655/*
Christoph Lameter81819f02007-05-06 14:49:36 -07001656 * Calculate the order of allocation given an slab object size.
1657 *
Christoph Lameter672bba32007-05-09 02:32:39 -07001658 * The order of allocation has significant impact on performance and other
1659 * system components. Generally order 0 allocations should be preferred since
1660 * order 0 does not cause fragmentation in the page allocator. Larger objects
1661 * be problematic to put into order 0 slabs because there may be too much
1662 * unused space left. We go to a higher order if more than 1/8th of the slab
1663 * would be wasted.
Christoph Lameter81819f02007-05-06 14:49:36 -07001664 *
Christoph Lameter672bba32007-05-09 02:32:39 -07001665 * In order to reach satisfactory performance we must ensure that a minimum
1666 * number of objects is in one slab. Otherwise we may generate too much
1667 * activity on the partial lists which requires taking the list_lock. This is
1668 * less a concern for large slabs though which are rarely used.
Christoph Lameter81819f02007-05-06 14:49:36 -07001669 *
Christoph Lameter672bba32007-05-09 02:32:39 -07001670 * slub_max_order specifies the order where we begin to stop considering the
1671 * number of objects in a slab as critical. If we reach slub_max_order then
1672 * we try to keep the page order as low as possible. So we accept more waste
1673 * of space in favor of a small page order.
1674 *
1675 * Higher order allocations also allow the placement of more objects in a
1676 * slab and thereby reduce object handling overhead. If the user has
1677 * requested a higher mininum order then we start with that one instead of
1678 * the smallest order which will fit the object.
Christoph Lameter81819f02007-05-06 14:49:36 -07001679 */
Christoph Lameter5e6d4442007-05-09 02:32:46 -07001680static inline int slab_order(int size, int min_objects,
1681 int max_order, int fract_leftover)
Christoph Lameter81819f02007-05-06 14:49:36 -07001682{
1683 int order;
1684 int rem;
1685
Christoph Lameter5e6d4442007-05-09 02:32:46 -07001686 for (order = max(slub_min_order,
1687 fls(min_objects * size - 1) - PAGE_SHIFT);
1688 order <= max_order; order++) {
1689
Christoph Lameter81819f02007-05-06 14:49:36 -07001690 unsigned long slab_size = PAGE_SIZE << order;
1691
Christoph Lameter5e6d4442007-05-09 02:32:46 -07001692 if (slab_size < min_objects * size)
Christoph Lameter81819f02007-05-06 14:49:36 -07001693 continue;
1694
Christoph Lameter81819f02007-05-06 14:49:36 -07001695 rem = slab_size % size;
1696
Christoph Lameter5e6d4442007-05-09 02:32:46 -07001697 if (rem <= slab_size / fract_leftover)
Christoph Lameter81819f02007-05-06 14:49:36 -07001698 break;
1699
1700 }
Christoph Lameter672bba32007-05-09 02:32:39 -07001701
Christoph Lameter81819f02007-05-06 14:49:36 -07001702 return order;
1703}
1704
Christoph Lameter5e6d4442007-05-09 02:32:46 -07001705static inline int calculate_order(int size)
1706{
1707 int order;
1708 int min_objects;
1709 int fraction;
1710
1711 /*
1712 * Attempt to find best configuration for a slab. This
1713 * works by first attempting to generate a layout with
1714 * the best configuration and backing off gradually.
1715 *
1716 * First we reduce the acceptable waste in a slab. Then
1717 * we reduce the minimum objects required in a slab.
1718 */
1719 min_objects = slub_min_objects;
1720 while (min_objects > 1) {
1721 fraction = 8;
1722 while (fraction >= 4) {
1723 order = slab_order(size, min_objects,
1724 slub_max_order, fraction);
1725 if (order <= slub_max_order)
1726 return order;
1727 fraction /= 2;
1728 }
1729 min_objects /= 2;
1730 }
1731
1732 /*
1733 * We were unable to place multiple objects in a slab. Now
1734 * lets see if we can place a single object there.
1735 */
1736 order = slab_order(size, 1, slub_max_order, 1);
1737 if (order <= slub_max_order)
1738 return order;
1739
1740 /*
1741 * Doh this slab cannot be placed using slub_max_order.
1742 */
1743 order = slab_order(size, 1, MAX_ORDER, 1);
1744 if (order <= MAX_ORDER)
1745 return order;
1746 return -ENOSYS;
1747}
1748
Christoph Lameter81819f02007-05-06 14:49:36 -07001749/*
Christoph Lameter672bba32007-05-09 02:32:39 -07001750 * Figure out what the alignment of the objects will be.
Christoph Lameter81819f02007-05-06 14:49:36 -07001751 */
1752static unsigned long calculate_alignment(unsigned long flags,
1753 unsigned long align, unsigned long size)
1754{
1755 /*
1756 * If the user wants hardware cache aligned objects then
1757 * follow that suggestion if the object is sufficiently
1758 * large.
1759 *
1760 * The hardware cache alignment cannot override the
1761 * specified alignment though. If that is greater
1762 * then use it.
1763 */
Christoph Lameter5af60832007-05-06 14:49:56 -07001764 if ((flags & SLAB_HWCACHE_ALIGN) &&
Christoph Lameter65c02d42007-05-09 02:32:35 -07001765 size > cache_line_size() / 2)
1766 return max_t(unsigned long, align, cache_line_size());
Christoph Lameter81819f02007-05-06 14:49:36 -07001767
1768 if (align < ARCH_SLAB_MINALIGN)
1769 return ARCH_SLAB_MINALIGN;
1770
1771 return ALIGN(align, sizeof(void *));
1772}
1773
1774static void init_kmem_cache_node(struct kmem_cache_node *n)
1775{
1776 n->nr_partial = 0;
1777 atomic_long_set(&n->nr_slabs, 0);
1778 spin_lock_init(&n->list_lock);
1779 INIT_LIST_HEAD(&n->partial);
Christoph Lameter643b1132007-05-06 14:49:42 -07001780 INIT_LIST_HEAD(&n->full);
Christoph Lameter81819f02007-05-06 14:49:36 -07001781}
1782
1783#ifdef CONFIG_NUMA
1784/*
1785 * No kmalloc_node yet so do it by hand. We know that this is the first
1786 * slab on the node for this slabcache. There are no concurrent accesses
1787 * possible.
1788 *
1789 * Note that this function only works on the kmalloc_node_cache
1790 * when allocating for the kmalloc_node_cache.
1791 */
1792static struct kmem_cache_node * __init early_kmem_cache_node_alloc(gfp_t gfpflags,
1793 int node)
1794{
1795 struct page *page;
1796 struct kmem_cache_node *n;
1797
1798 BUG_ON(kmalloc_caches->size < sizeof(struct kmem_cache_node));
1799
1800 page = new_slab(kmalloc_caches, gfpflags | GFP_THISNODE, node);
Christoph Lameter81819f02007-05-06 14:49:36 -07001801
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;
Christoph Lameter3ec09742007-05-16 22:11:00 -07001808 setup_object_debug(kmalloc_caches, page, n);
Christoph Lameter81819f02007-05-06 14:49:36 -07001809 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 Lameterdbc55fa2007-07-03 09:31:04 -07001812
1813 /*
1814 * new_slab() disables interupts. If we do not reenable interrupts here
1815 * then bootup would continue with interrupts disabled.
1816 */
1817 local_irq_enable();
Christoph Lameter81819f02007-05-06 14:49:36 -07001818 return n;
1819}
1820
1821static void free_kmem_cache_nodes(struct kmem_cache *s)
1822{
1823 int node;
1824
1825 for_each_online_node(node) {
1826 struct kmem_cache_node *n = s->node[node];
1827 if (n && n != &s->local_node)
1828 kmem_cache_free(kmalloc_caches, n);
1829 s->node[node] = NULL;
1830 }
1831}
1832
1833static int init_kmem_cache_nodes(struct kmem_cache *s, gfp_t gfpflags)
1834{
1835 int node;
1836 int local_node;
1837
1838 if (slab_state >= UP)
1839 local_node = page_to_nid(virt_to_page(s));
1840 else
1841 local_node = 0;
1842
1843 for_each_online_node(node) {
1844 struct kmem_cache_node *n;
1845
1846 if (local_node == node)
1847 n = &s->local_node;
1848 else {
1849 if (slab_state == DOWN) {
1850 n = early_kmem_cache_node_alloc(gfpflags,
1851 node);
1852 continue;
1853 }
1854 n = kmem_cache_alloc_node(kmalloc_caches,
1855 gfpflags, node);
1856
1857 if (!n) {
1858 free_kmem_cache_nodes(s);
1859 return 0;
1860 }
1861
1862 }
1863 s->node[node] = n;
1864 init_kmem_cache_node(n);
1865 }
1866 return 1;
1867}
1868#else
1869static void free_kmem_cache_nodes(struct kmem_cache *s)
1870{
1871}
1872
1873static int init_kmem_cache_nodes(struct kmem_cache *s, gfp_t gfpflags)
1874{
1875 init_kmem_cache_node(&s->local_node);
1876 return 1;
1877}
1878#endif
1879
1880/*
1881 * calculate_sizes() determines the order and the distribution of data within
1882 * a slab object.
1883 */
1884static int calculate_sizes(struct kmem_cache *s)
1885{
1886 unsigned long flags = s->flags;
1887 unsigned long size = s->objsize;
1888 unsigned long align = s->align;
1889
1890 /*
1891 * Determine if we can poison the object itself. If the user of
1892 * the slab may touch the object after free or before allocation
1893 * then we should never poison the object itself.
1894 */
1895 if ((flags & SLAB_POISON) && !(flags & SLAB_DESTROY_BY_RCU) &&
Christoph Lameterc59def9f2007-05-16 22:10:50 -07001896 !s->ctor)
Christoph Lameter81819f02007-05-06 14:49:36 -07001897 s->flags |= __OBJECT_POISON;
1898 else
1899 s->flags &= ~__OBJECT_POISON;
1900
1901 /*
1902 * Round up object size to the next word boundary. We can only
1903 * place the free pointer at word boundaries and this determines
1904 * the possible location of the free pointer.
1905 */
1906 size = ALIGN(size, sizeof(void *));
1907
Christoph Lameter41ecc552007-05-09 02:32:44 -07001908#ifdef CONFIG_SLUB_DEBUG
Christoph Lameter81819f02007-05-06 14:49:36 -07001909 /*
Christoph Lameter672bba32007-05-09 02:32:39 -07001910 * If we are Redzoning then check if there is some space between the
Christoph Lameter81819f02007-05-06 14:49:36 -07001911 * end of the object and the free pointer. If not then add an
Christoph Lameter672bba32007-05-09 02:32:39 -07001912 * additional word to have some bytes to store Redzone information.
Christoph Lameter81819f02007-05-06 14:49:36 -07001913 */
1914 if ((flags & SLAB_RED_ZONE) && size == s->objsize)
1915 size += sizeof(void *);
Christoph Lameter41ecc552007-05-09 02:32:44 -07001916#endif
Christoph Lameter81819f02007-05-06 14:49:36 -07001917
1918 /*
Christoph Lameter672bba32007-05-09 02:32:39 -07001919 * With that we have determined the number of bytes in actual use
1920 * by the object. This is the potential offset to the free pointer.
Christoph Lameter81819f02007-05-06 14:49:36 -07001921 */
1922 s->inuse = size;
1923
1924 if (((flags & (SLAB_DESTROY_BY_RCU | SLAB_POISON)) ||
Christoph Lameterc59def9f2007-05-16 22:10:50 -07001925 s->ctor)) {
Christoph Lameter81819f02007-05-06 14:49:36 -07001926 /*
1927 * Relocate free pointer after the object if it is not
1928 * permitted to overwrite the first word of the object on
1929 * kmem_cache_free.
1930 *
1931 * This is the case if we do RCU, have a constructor or
1932 * destructor or are poisoning the objects.
1933 */
1934 s->offset = size;
1935 size += sizeof(void *);
1936 }
1937
Christoph Lameterc12b3c62007-05-23 13:57:31 -07001938#ifdef CONFIG_SLUB_DEBUG
Christoph Lameter81819f02007-05-06 14:49:36 -07001939 if (flags & SLAB_STORE_USER)
1940 /*
1941 * Need to store information about allocs and frees after
1942 * the object.
1943 */
1944 size += 2 * sizeof(struct track);
1945
Christoph Lameterbe7b3fb2007-05-09 02:32:36 -07001946 if (flags & SLAB_RED_ZONE)
Christoph Lameter81819f02007-05-06 14:49:36 -07001947 /*
1948 * Add some empty padding so that we can catch
1949 * overwrites from earlier objects rather than let
1950 * tracking information or the free pointer be
1951 * corrupted if an user writes before the start
1952 * of the object.
1953 */
1954 size += sizeof(void *);
Christoph Lameter41ecc552007-05-09 02:32:44 -07001955#endif
Christoph Lameter672bba32007-05-09 02:32:39 -07001956
Christoph Lameter81819f02007-05-06 14:49:36 -07001957 /*
1958 * Determine the alignment based on various parameters that the
Christoph Lameter65c02d42007-05-09 02:32:35 -07001959 * user specified and the dynamic determination of cache line size
1960 * on bootup.
Christoph Lameter81819f02007-05-06 14:49:36 -07001961 */
1962 align = calculate_alignment(flags, align, s->objsize);
1963
1964 /*
1965 * SLUB stores one object immediately after another beginning from
1966 * offset 0. In order to align the objects we have to simply size
1967 * each object to conform to the alignment.
1968 */
1969 size = ALIGN(size, align);
1970 s->size = size;
1971
1972 s->order = calculate_order(size);
1973 if (s->order < 0)
1974 return 0;
1975
1976 /*
1977 * Determine the number of objects per slab
1978 */
1979 s->objects = (PAGE_SIZE << s->order) / size;
1980
1981 /*
1982 * Verify that the number of objects is within permitted limits.
1983 * The page->inuse field is only 16 bit wide! So we cannot have
1984 * more than 64k objects per slab.
1985 */
1986 if (!s->objects || s->objects > 65535)
1987 return 0;
1988 return 1;
1989
1990}
1991
Christoph Lameter81819f02007-05-06 14:49:36 -07001992static int kmem_cache_open(struct kmem_cache *s, gfp_t gfpflags,
1993 const char *name, size_t size,
1994 size_t align, unsigned long flags,
Christoph Lameterc59def9f2007-05-16 22:10:50 -07001995 void (*ctor)(void *, struct kmem_cache *, unsigned long))
Christoph Lameter81819f02007-05-06 14:49:36 -07001996{
1997 memset(s, 0, kmem_size);
1998 s->name = name;
1999 s->ctor = ctor;
Christoph Lameter81819f02007-05-06 14:49:36 -07002000 s->objsize = size;
2001 s->flags = flags;
2002 s->align = align;
Christoph Lameter41ecc552007-05-09 02:32:44 -07002003 kmem_cache_open_debug_check(s);
Christoph Lameter81819f02007-05-06 14:49:36 -07002004
2005 if (!calculate_sizes(s))
2006 goto error;
2007
2008 s->refcount = 1;
2009#ifdef CONFIG_NUMA
2010 s->defrag_ratio = 100;
2011#endif
2012
2013 if (init_kmem_cache_nodes(s, gfpflags & ~SLUB_DMA))
2014 return 1;
2015error:
2016 if (flags & SLAB_PANIC)
2017 panic("Cannot create slab %s size=%lu realsize=%u "
2018 "order=%u offset=%u flags=%lx\n",
2019 s->name, (unsigned long)size, s->size, s->order,
2020 s->offset, flags);
2021 return 0;
2022}
Christoph Lameter81819f02007-05-06 14:49:36 -07002023
2024/*
2025 * Check if a given pointer is valid
2026 */
2027int kmem_ptr_validate(struct kmem_cache *s, const void *object)
2028{
2029 struct page * page;
Christoph Lameter81819f02007-05-06 14:49:36 -07002030
2031 page = get_object_page(object);
2032
2033 if (!page || s != page->slab)
2034 /* No slab or wrong slab */
2035 return 0;
2036
Christoph Lameterabcd08a2007-05-09 02:32:37 -07002037 if (!check_valid_pointer(s, page, object))
Christoph Lameter81819f02007-05-06 14:49:36 -07002038 return 0;
2039
2040 /*
2041 * We could also check if the object is on the slabs freelist.
2042 * But this would be too expensive and it seems that the main
2043 * purpose of kmem_ptr_valid is to check if the object belongs
2044 * to a certain slab.
2045 */
2046 return 1;
2047}
2048EXPORT_SYMBOL(kmem_ptr_validate);
2049
2050/*
2051 * Determine the size of a slab object
2052 */
2053unsigned int kmem_cache_size(struct kmem_cache *s)
2054{
2055 return s->objsize;
2056}
2057EXPORT_SYMBOL(kmem_cache_size);
2058
2059const char *kmem_cache_name(struct kmem_cache *s)
2060{
2061 return s->name;
2062}
2063EXPORT_SYMBOL(kmem_cache_name);
2064
2065/*
Christoph Lameter672bba32007-05-09 02:32:39 -07002066 * Attempt to free all slabs on a node. Return the number of slabs we
2067 * were unable to free.
Christoph Lameter81819f02007-05-06 14:49:36 -07002068 */
2069static int free_list(struct kmem_cache *s, struct kmem_cache_node *n,
2070 struct list_head *list)
2071{
2072 int slabs_inuse = 0;
2073 unsigned long flags;
2074 struct page *page, *h;
2075
2076 spin_lock_irqsave(&n->list_lock, flags);
2077 list_for_each_entry_safe(page, h, list, lru)
2078 if (!page->inuse) {
2079 list_del(&page->lru);
2080 discard_slab(s, page);
2081 } else
2082 slabs_inuse++;
2083 spin_unlock_irqrestore(&n->list_lock, flags);
2084 return slabs_inuse;
2085}
2086
2087/*
Christoph Lameter672bba32007-05-09 02:32:39 -07002088 * Release all resources used by a slab cache.
Christoph Lameter81819f02007-05-06 14:49:36 -07002089 */
2090static int kmem_cache_close(struct kmem_cache *s)
2091{
2092 int node;
2093
2094 flush_all(s);
2095
2096 /* Attempt to free all objects */
2097 for_each_online_node(node) {
2098 struct kmem_cache_node *n = get_node(s, node);
2099
Christoph Lameter2086d262007-05-06 14:49:46 -07002100 n->nr_partial -= free_list(s, n, &n->partial);
Christoph Lameter81819f02007-05-06 14:49:36 -07002101 if (atomic_long_read(&n->nr_slabs))
2102 return 1;
2103 }
2104 free_kmem_cache_nodes(s);
2105 return 0;
2106}
2107
2108/*
2109 * Close a cache and release the kmem_cache structure
2110 * (must be used for caches created using kmem_cache_create)
2111 */
2112void kmem_cache_destroy(struct kmem_cache *s)
2113{
2114 down_write(&slub_lock);
2115 s->refcount--;
2116 if (!s->refcount) {
2117 list_del(&s->list);
2118 if (kmem_cache_close(s))
2119 WARN_ON(1);
2120 sysfs_slab_remove(s);
2121 kfree(s);
2122 }
2123 up_write(&slub_lock);
2124}
2125EXPORT_SYMBOL(kmem_cache_destroy);
2126
2127/********************************************************************
2128 * Kmalloc subsystem
2129 *******************************************************************/
2130
2131struct kmem_cache kmalloc_caches[KMALLOC_SHIFT_HIGH + 1] __cacheline_aligned;
2132EXPORT_SYMBOL(kmalloc_caches);
2133
2134#ifdef CONFIG_ZONE_DMA
2135static struct kmem_cache *kmalloc_caches_dma[KMALLOC_SHIFT_HIGH + 1];
2136#endif
2137
2138static int __init setup_slub_min_order(char *str)
2139{
2140 get_option (&str, &slub_min_order);
2141
2142 return 1;
2143}
2144
2145__setup("slub_min_order=", setup_slub_min_order);
2146
2147static int __init setup_slub_max_order(char *str)
2148{
2149 get_option (&str, &slub_max_order);
2150
2151 return 1;
2152}
2153
2154__setup("slub_max_order=", setup_slub_max_order);
2155
2156static int __init setup_slub_min_objects(char *str)
2157{
2158 get_option (&str, &slub_min_objects);
2159
2160 return 1;
2161}
2162
2163__setup("slub_min_objects=", setup_slub_min_objects);
2164
2165static int __init setup_slub_nomerge(char *str)
2166{
2167 slub_nomerge = 1;
2168 return 1;
2169}
2170
2171__setup("slub_nomerge", setup_slub_nomerge);
2172
Christoph Lameter81819f02007-05-06 14:49:36 -07002173static struct kmem_cache *create_kmalloc_cache(struct kmem_cache *s,
2174 const char *name, int size, gfp_t gfp_flags)
2175{
2176 unsigned int flags = 0;
2177
2178 if (gfp_flags & SLUB_DMA)
2179 flags = SLAB_CACHE_DMA;
2180
2181 down_write(&slub_lock);
2182 if (!kmem_cache_open(s, gfp_flags, name, size, ARCH_KMALLOC_MINALIGN,
Christoph Lameterc59def9f2007-05-16 22:10:50 -07002183 flags, NULL))
Christoph Lameter81819f02007-05-06 14:49:36 -07002184 goto panic;
2185
2186 list_add(&s->list, &slab_caches);
2187 up_write(&slub_lock);
2188 if (sysfs_slab_add(s))
2189 goto panic;
2190 return s;
2191
2192panic:
2193 panic("Creation of kmalloc slab %s size=%d failed.\n", name, size);
2194}
2195
2196static struct kmem_cache *get_slab(size_t size, gfp_t flags)
2197{
2198 int index = kmalloc_index(size);
2199
Christoph Lameter614410d2007-05-06 14:49:38 -07002200 if (!index)
Christoph Lameter81819f02007-05-06 14:49:36 -07002201 return NULL;
2202
2203 /* Allocation too large? */
2204 BUG_ON(index < 0);
2205
2206#ifdef CONFIG_ZONE_DMA
2207 if ((flags & SLUB_DMA)) {
2208 struct kmem_cache *s;
2209 struct kmem_cache *x;
2210 char *text;
2211 size_t realsize;
2212
2213 s = kmalloc_caches_dma[index];
2214 if (s)
2215 return s;
2216
2217 /* Dynamically create dma cache */
2218 x = kmalloc(kmem_size, flags & ~SLUB_DMA);
2219 if (!x)
2220 panic("Unable to allocate memory for dma cache\n");
2221
2222 if (index <= KMALLOC_SHIFT_HIGH)
2223 realsize = 1 << index;
2224 else {
2225 if (index == 1)
2226 realsize = 96;
2227 else
2228 realsize = 192;
2229 }
2230
2231 text = kasprintf(flags & ~SLUB_DMA, "kmalloc_dma-%d",
2232 (unsigned int)realsize);
2233 s = create_kmalloc_cache(x, text, realsize, flags);
2234 kmalloc_caches_dma[index] = s;
2235 return s;
2236 }
2237#endif
2238 return &kmalloc_caches[index];
2239}
2240
2241void *__kmalloc(size_t size, gfp_t flags)
2242{
2243 struct kmem_cache *s = get_slab(size, flags);
2244
2245 if (s)
Christoph Lameter77c5e2d2007-05-06 14:49:42 -07002246 return slab_alloc(s, flags, -1, __builtin_return_address(0));
Christoph Lameter272c1d22007-06-08 13:46:49 -07002247 return ZERO_SIZE_PTR;
Christoph Lameter81819f02007-05-06 14:49:36 -07002248}
2249EXPORT_SYMBOL(__kmalloc);
2250
2251#ifdef CONFIG_NUMA
2252void *__kmalloc_node(size_t size, gfp_t flags, int node)
2253{
2254 struct kmem_cache *s = get_slab(size, flags);
2255
2256 if (s)
Christoph Lameter77c5e2d2007-05-06 14:49:42 -07002257 return slab_alloc(s, flags, node, __builtin_return_address(0));
Christoph Lameter272c1d22007-06-08 13:46:49 -07002258 return ZERO_SIZE_PTR;
Christoph Lameter81819f02007-05-06 14:49:36 -07002259}
2260EXPORT_SYMBOL(__kmalloc_node);
2261#endif
2262
2263size_t ksize(const void *object)
2264{
Christoph Lameter272c1d22007-06-08 13:46:49 -07002265 struct page *page;
Christoph Lameter81819f02007-05-06 14:49:36 -07002266 struct kmem_cache *s;
2267
Christoph Lameter272c1d22007-06-08 13:46:49 -07002268 if (object == ZERO_SIZE_PTR)
2269 return 0;
2270
2271 page = get_object_page(object);
Christoph Lameter81819f02007-05-06 14:49:36 -07002272 BUG_ON(!page);
2273 s = page->slab;
2274 BUG_ON(!s);
2275
2276 /*
2277 * Debugging requires use of the padding between object
2278 * and whatever may come after it.
2279 */
2280 if (s->flags & (SLAB_RED_ZONE | SLAB_POISON))
2281 return s->objsize;
2282
2283 /*
2284 * If we have the need to store the freelist pointer
2285 * back there or track user information then we can
2286 * only use the space before that information.
2287 */
2288 if (s->flags & (SLAB_DESTROY_BY_RCU | SLAB_STORE_USER))
2289 return s->inuse;
2290
2291 /*
2292 * Else we can use all the padding etc for the allocation
2293 */
2294 return s->size;
2295}
2296EXPORT_SYMBOL(ksize);
2297
2298void kfree(const void *x)
2299{
2300 struct kmem_cache *s;
2301 struct page *page;
2302
Christoph Lameter272c1d22007-06-08 13:46:49 -07002303 /*
2304 * This has to be an unsigned comparison. According to Linus
2305 * some gcc version treat a pointer as a signed entity. Then
2306 * this comparison would be true for all "negative" pointers
2307 * (which would cover the whole upper half of the address space).
2308 */
2309 if ((unsigned long)x <= (unsigned long)ZERO_SIZE_PTR)
Christoph Lameter81819f02007-05-06 14:49:36 -07002310 return;
2311
Christoph Lameterb49af682007-05-06 14:49:41 -07002312 page = virt_to_head_page(x);
Christoph Lameter81819f02007-05-06 14:49:36 -07002313 s = page->slab;
2314
Christoph Lameter77c5e2d2007-05-06 14:49:42 -07002315 slab_free(s, page, (void *)x, __builtin_return_address(0));
Christoph Lameter81819f02007-05-06 14:49:36 -07002316}
2317EXPORT_SYMBOL(kfree);
2318
Christoph Lameter2086d262007-05-06 14:49:46 -07002319/*
Christoph Lameter672bba32007-05-09 02:32:39 -07002320 * kmem_cache_shrink removes empty slabs from the partial lists and sorts
2321 * the remaining slabs by the number of items in use. The slabs with the
2322 * most items in use come first. New allocations will then fill those up
2323 * and thus they can be removed from the partial lists.
2324 *
2325 * The slabs with the least items are placed last. This results in them
2326 * being allocated from last increasing the chance that the last objects
2327 * are freed in them.
Christoph Lameter2086d262007-05-06 14:49:46 -07002328 */
2329int kmem_cache_shrink(struct kmem_cache *s)
2330{
2331 int node;
2332 int i;
2333 struct kmem_cache_node *n;
2334 struct page *page;
2335 struct page *t;
2336 struct list_head *slabs_by_inuse =
2337 kmalloc(sizeof(struct list_head) * s->objects, GFP_KERNEL);
2338 unsigned long flags;
2339
2340 if (!slabs_by_inuse)
2341 return -ENOMEM;
2342
2343 flush_all(s);
2344 for_each_online_node(node) {
2345 n = get_node(s, node);
2346
2347 if (!n->nr_partial)
2348 continue;
2349
2350 for (i = 0; i < s->objects; i++)
2351 INIT_LIST_HEAD(slabs_by_inuse + i);
2352
2353 spin_lock_irqsave(&n->list_lock, flags);
2354
2355 /*
Christoph Lameter672bba32007-05-09 02:32:39 -07002356 * Build lists indexed by the items in use in each slab.
Christoph Lameter2086d262007-05-06 14:49:46 -07002357 *
Christoph Lameter672bba32007-05-09 02:32:39 -07002358 * Note that concurrent frees may occur while we hold the
2359 * list_lock. page->inuse here is the upper limit.
Christoph Lameter2086d262007-05-06 14:49:46 -07002360 */
2361 list_for_each_entry_safe(page, t, &n->partial, lru) {
2362 if (!page->inuse && slab_trylock(page)) {
2363 /*
2364 * Must hold slab lock here because slab_free
2365 * may have freed the last object and be
2366 * waiting to release the slab.
2367 */
2368 list_del(&page->lru);
2369 n->nr_partial--;
2370 slab_unlock(page);
2371 discard_slab(s, page);
2372 } else {
2373 if (n->nr_partial > MAX_PARTIAL)
2374 list_move(&page->lru,
2375 slabs_by_inuse + page->inuse);
2376 }
2377 }
2378
2379 if (n->nr_partial <= MAX_PARTIAL)
2380 goto out;
2381
2382 /*
Christoph Lameter672bba32007-05-09 02:32:39 -07002383 * Rebuild the partial list with the slabs filled up most
2384 * first and the least used slabs at the end.
Christoph Lameter2086d262007-05-06 14:49:46 -07002385 */
2386 for (i = s->objects - 1; i >= 0; i--)
2387 list_splice(slabs_by_inuse + i, n->partial.prev);
2388
2389 out:
2390 spin_unlock_irqrestore(&n->list_lock, flags);
2391 }
2392
2393 kfree(slabs_by_inuse);
2394 return 0;
2395}
2396EXPORT_SYMBOL(kmem_cache_shrink);
2397
Christoph Lameter81819f02007-05-06 14:49:36 -07002398/**
2399 * krealloc - reallocate memory. The contents will remain unchanged.
Christoph Lameter81819f02007-05-06 14:49:36 -07002400 * @p: object to reallocate memory for.
2401 * @new_size: how many bytes of memory are required.
2402 * @flags: the type of memory to allocate.
2403 *
2404 * The contents of the object pointed to are preserved up to the
2405 * lesser of the new and old sizes. If @p is %NULL, krealloc()
2406 * behaves exactly like kmalloc(). If @size is 0 and @p is not a
2407 * %NULL pointer, the object pointed to is freed.
2408 */
2409void *krealloc(const void *p, size_t new_size, gfp_t flags)
2410{
Christoph Lameter81819f02007-05-06 14:49:36 -07002411 void *ret;
Christoph Lameter1f99a282007-05-09 02:32:38 -07002412 size_t ks;
Christoph Lameter81819f02007-05-06 14:49:36 -07002413
Christoph Lameter272c1d22007-06-08 13:46:49 -07002414 if (unlikely(!p || p == ZERO_SIZE_PTR))
Christoph Lameter81819f02007-05-06 14:49:36 -07002415 return kmalloc(new_size, flags);
2416
2417 if (unlikely(!new_size)) {
2418 kfree(p);
Christoph Lameter272c1d22007-06-08 13:46:49 -07002419 return ZERO_SIZE_PTR;
Christoph Lameter81819f02007-05-06 14:49:36 -07002420 }
2421
Christoph Lameter1f99a282007-05-09 02:32:38 -07002422 ks = ksize(p);
2423 if (ks >= new_size)
Christoph Lameter81819f02007-05-06 14:49:36 -07002424 return (void *)p;
2425
2426 ret = kmalloc(new_size, flags);
2427 if (ret) {
Christoph Lameter1f99a282007-05-09 02:32:38 -07002428 memcpy(ret, p, min(new_size, ks));
Christoph Lameter81819f02007-05-06 14:49:36 -07002429 kfree(p);
2430 }
2431 return ret;
2432}
2433EXPORT_SYMBOL(krealloc);
2434
2435/********************************************************************
2436 * Basic setup of slabs
2437 *******************************************************************/
2438
2439void __init kmem_cache_init(void)
2440{
2441 int i;
Christoph Lameter4b356be2007-06-16 10:16:13 -07002442 int caches = 0;
Christoph Lameter81819f02007-05-06 14:49:36 -07002443
2444#ifdef CONFIG_NUMA
2445 /*
2446 * Must first have the slab cache available for the allocations of the
Christoph Lameter672bba32007-05-09 02:32:39 -07002447 * struct kmem_cache_node's. There is special bootstrap code in
Christoph Lameter81819f02007-05-06 14:49:36 -07002448 * kmem_cache_open for slab_state == DOWN.
2449 */
2450 create_kmalloc_cache(&kmalloc_caches[0], "kmem_cache_node",
2451 sizeof(struct kmem_cache_node), GFP_KERNEL);
Christoph Lameter8ffa6872007-05-31 00:40:51 -07002452 kmalloc_caches[0].refcount = -1;
Christoph Lameter4b356be2007-06-16 10:16:13 -07002453 caches++;
Christoph Lameter81819f02007-05-06 14:49:36 -07002454#endif
2455
2456 /* Able to allocate the per node structures */
2457 slab_state = PARTIAL;
2458
2459 /* Caches that are not of the two-to-the-power-of size */
Christoph Lameter4b356be2007-06-16 10:16:13 -07002460 if (KMALLOC_MIN_SIZE <= 64) {
2461 create_kmalloc_cache(&kmalloc_caches[1],
Christoph Lameter81819f02007-05-06 14:49:36 -07002462 "kmalloc-96", 96, GFP_KERNEL);
Christoph Lameter4b356be2007-06-16 10:16:13 -07002463 caches++;
2464 }
2465 if (KMALLOC_MIN_SIZE <= 128) {
2466 create_kmalloc_cache(&kmalloc_caches[2],
Christoph Lameter81819f02007-05-06 14:49:36 -07002467 "kmalloc-192", 192, GFP_KERNEL);
Christoph Lameter4b356be2007-06-16 10:16:13 -07002468 caches++;
2469 }
Christoph Lameter81819f02007-05-06 14:49:36 -07002470
Christoph Lameter4b356be2007-06-16 10:16:13 -07002471 for (i = KMALLOC_SHIFT_LOW; i <= KMALLOC_SHIFT_HIGH; i++) {
Christoph Lameter81819f02007-05-06 14:49:36 -07002472 create_kmalloc_cache(&kmalloc_caches[i],
2473 "kmalloc", 1 << i, GFP_KERNEL);
Christoph Lameter4b356be2007-06-16 10:16:13 -07002474 caches++;
2475 }
Christoph Lameter81819f02007-05-06 14:49:36 -07002476
2477 slab_state = UP;
2478
2479 /* Provide the correct kmalloc names now that the caches are up */
2480 for (i = KMALLOC_SHIFT_LOW; i <= KMALLOC_SHIFT_HIGH; i++)
2481 kmalloc_caches[i]. name =
2482 kasprintf(GFP_KERNEL, "kmalloc-%d", 1 << i);
2483
2484#ifdef CONFIG_SMP
2485 register_cpu_notifier(&slab_notifier);
2486#endif
2487
Christoph Lameterbcf889f2007-05-10 03:15:44 -07002488 kmem_size = offsetof(struct kmem_cache, cpu_slab) +
2489 nr_cpu_ids * sizeof(struct page *);
Christoph Lameter81819f02007-05-06 14:49:36 -07002490
2491 printk(KERN_INFO "SLUB: Genslabs=%d, HWalign=%d, Order=%d-%d, MinObjects=%d,"
Christoph Lameter4b356be2007-06-16 10:16:13 -07002492 " CPUs=%d, Nodes=%d\n",
2493 caches, cache_line_size(),
Christoph Lameter81819f02007-05-06 14:49:36 -07002494 slub_min_order, slub_max_order, slub_min_objects,
2495 nr_cpu_ids, nr_node_ids);
2496}
2497
2498/*
2499 * Find a mergeable slab cache
2500 */
2501static int slab_unmergeable(struct kmem_cache *s)
2502{
2503 if (slub_nomerge || (s->flags & SLUB_NEVER_MERGE))
2504 return 1;
2505
Christoph Lameterc59def9f2007-05-16 22:10:50 -07002506 if (s->ctor)
Christoph Lameter81819f02007-05-06 14:49:36 -07002507 return 1;
2508
Christoph Lameter8ffa6872007-05-31 00:40:51 -07002509 /*
2510 * We may have set a slab to be unmergeable during bootstrap.
2511 */
2512 if (s->refcount < 0)
2513 return 1;
2514
Christoph Lameter81819f02007-05-06 14:49:36 -07002515 return 0;
2516}
2517
2518static struct kmem_cache *find_mergeable(size_t size,
2519 size_t align, unsigned long flags,
Christoph Lameterc59def9f2007-05-16 22:10:50 -07002520 void (*ctor)(void *, struct kmem_cache *, unsigned long))
Christoph Lameter81819f02007-05-06 14:49:36 -07002521{
2522 struct list_head *h;
2523
2524 if (slub_nomerge || (flags & SLUB_NEVER_MERGE))
2525 return NULL;
2526
Christoph Lameterc59def9f2007-05-16 22:10:50 -07002527 if (ctor)
Christoph Lameter81819f02007-05-06 14:49:36 -07002528 return NULL;
2529
2530 size = ALIGN(size, sizeof(void *));
2531 align = calculate_alignment(flags, align, size);
2532 size = ALIGN(size, align);
2533
2534 list_for_each(h, &slab_caches) {
2535 struct kmem_cache *s =
2536 container_of(h, struct kmem_cache, list);
2537
2538 if (slab_unmergeable(s))
2539 continue;
2540
2541 if (size > s->size)
2542 continue;
2543
2544 if (((flags | slub_debug) & SLUB_MERGE_SAME) !=
2545 (s->flags & SLUB_MERGE_SAME))
2546 continue;
2547 /*
2548 * Check if alignment is compatible.
2549 * Courtesy of Adrian Drzewiecki
2550 */
2551 if ((s->size & ~(align -1)) != s->size)
2552 continue;
2553
2554 if (s->size - size >= sizeof(void *))
2555 continue;
2556
2557 return s;
2558 }
2559 return NULL;
2560}
2561
2562struct kmem_cache *kmem_cache_create(const char *name, size_t size,
2563 size_t align, unsigned long flags,
2564 void (*ctor)(void *, struct kmem_cache *, unsigned long),
2565 void (*dtor)(void *, struct kmem_cache *, unsigned long))
2566{
2567 struct kmem_cache *s;
2568
Christoph Lameterc59def9f2007-05-16 22:10:50 -07002569 BUG_ON(dtor);
Christoph Lameter81819f02007-05-06 14:49:36 -07002570 down_write(&slub_lock);
Christoph Lameterc59def9f2007-05-16 22:10:50 -07002571 s = find_mergeable(size, align, flags, ctor);
Christoph Lameter81819f02007-05-06 14:49:36 -07002572 if (s) {
2573 s->refcount++;
2574 /*
2575 * Adjust the object sizes so that we clear
2576 * the complete object on kzalloc.
2577 */
2578 s->objsize = max(s->objsize, (int)size);
2579 s->inuse = max_t(int, s->inuse, ALIGN(size, sizeof(void *)));
2580 if (sysfs_slab_alias(s, name))
2581 goto err;
2582 } else {
2583 s = kmalloc(kmem_size, GFP_KERNEL);
2584 if (s && kmem_cache_open(s, GFP_KERNEL, name,
Christoph Lameterc59def9f2007-05-16 22:10:50 -07002585 size, align, flags, ctor)) {
Christoph Lameter81819f02007-05-06 14:49:36 -07002586 if (sysfs_slab_add(s)) {
2587 kfree(s);
2588 goto err;
2589 }
2590 list_add(&s->list, &slab_caches);
2591 } else
2592 kfree(s);
2593 }
2594 up_write(&slub_lock);
2595 return s;
2596
2597err:
2598 up_write(&slub_lock);
2599 if (flags & SLAB_PANIC)
2600 panic("Cannot create slabcache %s\n", name);
2601 else
2602 s = NULL;
2603 return s;
2604}
2605EXPORT_SYMBOL(kmem_cache_create);
2606
2607void *kmem_cache_zalloc(struct kmem_cache *s, gfp_t flags)
2608{
2609 void *x;
2610
Christoph Lameter77c5e2d2007-05-06 14:49:42 -07002611 x = slab_alloc(s, flags, -1, __builtin_return_address(0));
Christoph Lameter81819f02007-05-06 14:49:36 -07002612 if (x)
2613 memset(x, 0, s->objsize);
2614 return x;
2615}
2616EXPORT_SYMBOL(kmem_cache_zalloc);
2617
2618#ifdef CONFIG_SMP
2619static void for_all_slabs(void (*func)(struct kmem_cache *, int), int cpu)
2620{
2621 struct list_head *h;
2622
2623 down_read(&slub_lock);
2624 list_for_each(h, &slab_caches) {
2625 struct kmem_cache *s =
2626 container_of(h, struct kmem_cache, list);
2627
2628 func(s, cpu);
2629 }
2630 up_read(&slub_lock);
2631}
2632
2633/*
Christoph Lameter27390bc2007-06-01 00:47:09 -07002634 * Version of __flush_cpu_slab for the case that interrupts
2635 * are enabled.
2636 */
2637static void cpu_slab_flush(struct kmem_cache *s, int cpu)
2638{
2639 unsigned long flags;
2640
2641 local_irq_save(flags);
2642 __flush_cpu_slab(s, cpu);
2643 local_irq_restore(flags);
2644}
2645
2646/*
Christoph Lameter672bba32007-05-09 02:32:39 -07002647 * Use the cpu notifier to insure that the cpu slabs are flushed when
2648 * necessary.
Christoph Lameter81819f02007-05-06 14:49:36 -07002649 */
2650static int __cpuinit slab_cpuup_callback(struct notifier_block *nfb,
2651 unsigned long action, void *hcpu)
2652{
2653 long cpu = (long)hcpu;
2654
2655 switch (action) {
2656 case CPU_UP_CANCELED:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07002657 case CPU_UP_CANCELED_FROZEN:
Christoph Lameter81819f02007-05-06 14:49:36 -07002658 case CPU_DEAD:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07002659 case CPU_DEAD_FROZEN:
Christoph Lameter27390bc2007-06-01 00:47:09 -07002660 for_all_slabs(cpu_slab_flush, cpu);
Christoph Lameter81819f02007-05-06 14:49:36 -07002661 break;
2662 default:
2663 break;
2664 }
2665 return NOTIFY_OK;
2666}
2667
2668static struct notifier_block __cpuinitdata slab_notifier =
2669 { &slab_cpuup_callback, NULL, 0 };
2670
2671#endif
2672
Christoph Lameter81819f02007-05-06 14:49:36 -07002673void *__kmalloc_track_caller(size_t size, gfp_t gfpflags, void *caller)
2674{
2675 struct kmem_cache *s = get_slab(size, gfpflags);
Christoph Lameter81819f02007-05-06 14:49:36 -07002676
2677 if (!s)
Christoph Lameter272c1d22007-06-08 13:46:49 -07002678 return ZERO_SIZE_PTR;
Christoph Lameter81819f02007-05-06 14:49:36 -07002679
Christoph Lameter77c5e2d2007-05-06 14:49:42 -07002680 return slab_alloc(s, gfpflags, -1, caller);
Christoph Lameter81819f02007-05-06 14:49:36 -07002681}
2682
2683void *__kmalloc_node_track_caller(size_t size, gfp_t gfpflags,
2684 int node, void *caller)
2685{
2686 struct kmem_cache *s = get_slab(size, gfpflags);
Christoph Lameter81819f02007-05-06 14:49:36 -07002687
2688 if (!s)
Christoph Lameter272c1d22007-06-08 13:46:49 -07002689 return ZERO_SIZE_PTR;
Christoph Lameter81819f02007-05-06 14:49:36 -07002690
Christoph Lameter77c5e2d2007-05-06 14:49:42 -07002691 return slab_alloc(s, gfpflags, node, caller);
Christoph Lameter81819f02007-05-06 14:49:36 -07002692}
2693
Christoph Lameter41ecc552007-05-09 02:32:44 -07002694#if defined(CONFIG_SYSFS) && defined(CONFIG_SLUB_DEBUG)
Christoph Lameter53e15af2007-05-06 14:49:43 -07002695static int validate_slab(struct kmem_cache *s, struct page *page)
2696{
2697 void *p;
2698 void *addr = page_address(page);
Christoph Lameter7656c722007-05-09 02:32:40 -07002699 DECLARE_BITMAP(map, s->objects);
Christoph Lameter53e15af2007-05-06 14:49:43 -07002700
2701 if (!check_slab(s, page) ||
2702 !on_freelist(s, page, NULL))
2703 return 0;
2704
2705 /* Now we know that a valid freelist exists */
2706 bitmap_zero(map, s->objects);
2707
Christoph Lameter7656c722007-05-09 02:32:40 -07002708 for_each_free_object(p, s, page->freelist) {
2709 set_bit(slab_index(p, s, addr), map);
Christoph Lameter53e15af2007-05-06 14:49:43 -07002710 if (!check_object(s, page, p, 0))
2711 return 0;
2712 }
2713
Christoph Lameter7656c722007-05-09 02:32:40 -07002714 for_each_object(p, s, addr)
2715 if (!test_bit(slab_index(p, s, addr), map))
Christoph Lameter53e15af2007-05-06 14:49:43 -07002716 if (!check_object(s, page, p, 1))
2717 return 0;
2718 return 1;
2719}
2720
2721static void validate_slab_slab(struct kmem_cache *s, struct page *page)
2722{
2723 if (slab_trylock(page)) {
2724 validate_slab(s, page);
2725 slab_unlock(page);
2726 } else
2727 printk(KERN_INFO "SLUB %s: Skipped busy slab 0x%p\n",
2728 s->name, page);
2729
2730 if (s->flags & DEBUG_DEFAULT_FLAGS) {
Christoph Lameter35e5d7e2007-05-09 02:32:42 -07002731 if (!SlabDebug(page))
2732 printk(KERN_ERR "SLUB %s: SlabDebug not set "
Christoph Lameter53e15af2007-05-06 14:49:43 -07002733 "on slab 0x%p\n", s->name, page);
2734 } else {
Christoph Lameter35e5d7e2007-05-09 02:32:42 -07002735 if (SlabDebug(page))
2736 printk(KERN_ERR "SLUB %s: SlabDebug set on "
Christoph Lameter53e15af2007-05-06 14:49:43 -07002737 "slab 0x%p\n", s->name, page);
2738 }
2739}
2740
2741static int validate_slab_node(struct kmem_cache *s, struct kmem_cache_node *n)
2742{
2743 unsigned long count = 0;
2744 struct page *page;
2745 unsigned long flags;
2746
2747 spin_lock_irqsave(&n->list_lock, flags);
2748
2749 list_for_each_entry(page, &n->partial, lru) {
2750 validate_slab_slab(s, page);
2751 count++;
2752 }
2753 if (count != n->nr_partial)
2754 printk(KERN_ERR "SLUB %s: %ld partial slabs counted but "
2755 "counter=%ld\n", s->name, count, n->nr_partial);
2756
2757 if (!(s->flags & SLAB_STORE_USER))
2758 goto out;
2759
2760 list_for_each_entry(page, &n->full, lru) {
2761 validate_slab_slab(s, page);
2762 count++;
2763 }
2764 if (count != atomic_long_read(&n->nr_slabs))
2765 printk(KERN_ERR "SLUB: %s %ld slabs counted but "
2766 "counter=%ld\n", s->name, count,
2767 atomic_long_read(&n->nr_slabs));
2768
2769out:
2770 spin_unlock_irqrestore(&n->list_lock, flags);
2771 return count;
2772}
2773
2774static unsigned long validate_slab_cache(struct kmem_cache *s)
2775{
2776 int node;
2777 unsigned long count = 0;
2778
2779 flush_all(s);
2780 for_each_online_node(node) {
2781 struct kmem_cache_node *n = get_node(s, node);
2782
2783 count += validate_slab_node(s, n);
2784 }
2785 return count;
2786}
2787
Christoph Lameterb3459702007-05-09 02:32:41 -07002788#ifdef SLUB_RESILIENCY_TEST
2789static void resiliency_test(void)
2790{
2791 u8 *p;
2792
2793 printk(KERN_ERR "SLUB resiliency testing\n");
2794 printk(KERN_ERR "-----------------------\n");
2795 printk(KERN_ERR "A. Corruption after allocation\n");
2796
2797 p = kzalloc(16, GFP_KERNEL);
2798 p[16] = 0x12;
2799 printk(KERN_ERR "\n1. kmalloc-16: Clobber Redzone/next pointer"
2800 " 0x12->0x%p\n\n", p + 16);
2801
2802 validate_slab_cache(kmalloc_caches + 4);
2803
2804 /* Hmmm... The next two are dangerous */
2805 p = kzalloc(32, GFP_KERNEL);
2806 p[32 + sizeof(void *)] = 0x34;
2807 printk(KERN_ERR "\n2. kmalloc-32: Clobber next pointer/next slab"
2808 " 0x34 -> -0x%p\n", p);
2809 printk(KERN_ERR "If allocated object is overwritten then not detectable\n\n");
2810
2811 validate_slab_cache(kmalloc_caches + 5);
2812 p = kzalloc(64, GFP_KERNEL);
2813 p += 64 + (get_cycles() & 0xff) * sizeof(void *);
2814 *p = 0x56;
2815 printk(KERN_ERR "\n3. kmalloc-64: corrupting random byte 0x56->0x%p\n",
2816 p);
2817 printk(KERN_ERR "If allocated object is overwritten then not detectable\n\n");
2818 validate_slab_cache(kmalloc_caches + 6);
2819
2820 printk(KERN_ERR "\nB. Corruption after free\n");
2821 p = kzalloc(128, GFP_KERNEL);
2822 kfree(p);
2823 *p = 0x78;
2824 printk(KERN_ERR "1. kmalloc-128: Clobber first word 0x78->0x%p\n\n", p);
2825 validate_slab_cache(kmalloc_caches + 7);
2826
2827 p = kzalloc(256, GFP_KERNEL);
2828 kfree(p);
2829 p[50] = 0x9a;
2830 printk(KERN_ERR "\n2. kmalloc-256: Clobber 50th byte 0x9a->0x%p\n\n", p);
2831 validate_slab_cache(kmalloc_caches + 8);
2832
2833 p = kzalloc(512, GFP_KERNEL);
2834 kfree(p);
2835 p[512] = 0xab;
2836 printk(KERN_ERR "\n3. kmalloc-512: Clobber redzone 0xab->0x%p\n\n", p);
2837 validate_slab_cache(kmalloc_caches + 9);
2838}
2839#else
2840static void resiliency_test(void) {};
2841#endif
2842
Christoph Lameter88a420e2007-05-06 14:49:45 -07002843/*
Christoph Lameter672bba32007-05-09 02:32:39 -07002844 * Generate lists of code addresses where slabcache objects are allocated
Christoph Lameter88a420e2007-05-06 14:49:45 -07002845 * and freed.
2846 */
2847
2848struct location {
2849 unsigned long count;
2850 void *addr;
Christoph Lameter45edfa52007-05-09 02:32:45 -07002851 long long sum_time;
2852 long min_time;
2853 long max_time;
2854 long min_pid;
2855 long max_pid;
2856 cpumask_t cpus;
2857 nodemask_t nodes;
Christoph Lameter88a420e2007-05-06 14:49:45 -07002858};
2859
2860struct loc_track {
2861 unsigned long max;
2862 unsigned long count;
2863 struct location *loc;
2864};
2865
2866static void free_loc_track(struct loc_track *t)
2867{
2868 if (t->max)
2869 free_pages((unsigned long)t->loc,
2870 get_order(sizeof(struct location) * t->max));
2871}
2872
2873static int alloc_loc_track(struct loc_track *t, unsigned long max)
2874{
2875 struct location *l;
2876 int order;
2877
2878 if (!max)
2879 max = PAGE_SIZE / sizeof(struct location);
2880
2881 order = get_order(sizeof(struct location) * max);
2882
Christoph Lameterdd08c402007-06-16 10:15:57 -07002883 l = (void *)__get_free_pages(GFP_ATOMIC, order);
Christoph Lameter88a420e2007-05-06 14:49:45 -07002884
2885 if (!l)
2886 return 0;
2887
2888 if (t->count) {
2889 memcpy(l, t->loc, sizeof(struct location) * t->count);
2890 free_loc_track(t);
2891 }
2892 t->max = max;
2893 t->loc = l;
2894 return 1;
2895}
2896
2897static int add_location(struct loc_track *t, struct kmem_cache *s,
Christoph Lameter45edfa52007-05-09 02:32:45 -07002898 const struct track *track)
Christoph Lameter88a420e2007-05-06 14:49:45 -07002899{
2900 long start, end, pos;
2901 struct location *l;
2902 void *caddr;
Christoph Lameter45edfa52007-05-09 02:32:45 -07002903 unsigned long age = jiffies - track->when;
Christoph Lameter88a420e2007-05-06 14:49:45 -07002904
2905 start = -1;
2906 end = t->count;
2907
2908 for ( ; ; ) {
2909 pos = start + (end - start + 1) / 2;
2910
2911 /*
2912 * There is nothing at "end". If we end up there
2913 * we need to add something to before end.
2914 */
2915 if (pos == end)
2916 break;
2917
2918 caddr = t->loc[pos].addr;
Christoph Lameter45edfa52007-05-09 02:32:45 -07002919 if (track->addr == caddr) {
2920
2921 l = &t->loc[pos];
2922 l->count++;
2923 if (track->when) {
2924 l->sum_time += age;
2925 if (age < l->min_time)
2926 l->min_time = age;
2927 if (age > l->max_time)
2928 l->max_time = age;
2929
2930 if (track->pid < l->min_pid)
2931 l->min_pid = track->pid;
2932 if (track->pid > l->max_pid)
2933 l->max_pid = track->pid;
2934
2935 cpu_set(track->cpu, l->cpus);
2936 }
2937 node_set(page_to_nid(virt_to_page(track)), l->nodes);
Christoph Lameter88a420e2007-05-06 14:49:45 -07002938 return 1;
2939 }
2940
Christoph Lameter45edfa52007-05-09 02:32:45 -07002941 if (track->addr < caddr)
Christoph Lameter88a420e2007-05-06 14:49:45 -07002942 end = pos;
2943 else
2944 start = pos;
2945 }
2946
2947 /*
Christoph Lameter672bba32007-05-09 02:32:39 -07002948 * Not found. Insert new tracking element.
Christoph Lameter88a420e2007-05-06 14:49:45 -07002949 */
2950 if (t->count >= t->max && !alloc_loc_track(t, 2 * t->max))
2951 return 0;
2952
2953 l = t->loc + pos;
2954 if (pos < t->count)
2955 memmove(l + 1, l,
2956 (t->count - pos) * sizeof(struct location));
2957 t->count++;
2958 l->count = 1;
Christoph Lameter45edfa52007-05-09 02:32:45 -07002959 l->addr = track->addr;
2960 l->sum_time = age;
2961 l->min_time = age;
2962 l->max_time = age;
2963 l->min_pid = track->pid;
2964 l->max_pid = track->pid;
2965 cpus_clear(l->cpus);
2966 cpu_set(track->cpu, l->cpus);
2967 nodes_clear(l->nodes);
2968 node_set(page_to_nid(virt_to_page(track)), l->nodes);
Christoph Lameter88a420e2007-05-06 14:49:45 -07002969 return 1;
2970}
2971
2972static void process_slab(struct loc_track *t, struct kmem_cache *s,
2973 struct page *page, enum track_item alloc)
2974{
2975 void *addr = page_address(page);
Christoph Lameter7656c722007-05-09 02:32:40 -07002976 DECLARE_BITMAP(map, s->objects);
Christoph Lameter88a420e2007-05-06 14:49:45 -07002977 void *p;
2978
2979 bitmap_zero(map, s->objects);
Christoph Lameter7656c722007-05-09 02:32:40 -07002980 for_each_free_object(p, s, page->freelist)
2981 set_bit(slab_index(p, s, addr), map);
Christoph Lameter88a420e2007-05-06 14:49:45 -07002982
Christoph Lameter7656c722007-05-09 02:32:40 -07002983 for_each_object(p, s, addr)
Christoph Lameter45edfa52007-05-09 02:32:45 -07002984 if (!test_bit(slab_index(p, s, addr), map))
2985 add_location(t, s, get_track(s, p, alloc));
Christoph Lameter88a420e2007-05-06 14:49:45 -07002986}
2987
2988static int list_locations(struct kmem_cache *s, char *buf,
2989 enum track_item alloc)
2990{
2991 int n = 0;
2992 unsigned long i;
2993 struct loc_track t;
2994 int node;
2995
2996 t.count = 0;
2997 t.max = 0;
2998
2999 /* Push back cpu slabs */
3000 flush_all(s);
3001
3002 for_each_online_node(node) {
3003 struct kmem_cache_node *n = get_node(s, node);
3004 unsigned long flags;
3005 struct page *page;
3006
3007 if (!atomic_read(&n->nr_slabs))
3008 continue;
3009
3010 spin_lock_irqsave(&n->list_lock, flags);
3011 list_for_each_entry(page, &n->partial, lru)
3012 process_slab(&t, s, page, alloc);
3013 list_for_each_entry(page, &n->full, lru)
3014 process_slab(&t, s, page, alloc);
3015 spin_unlock_irqrestore(&n->list_lock, flags);
3016 }
3017
3018 for (i = 0; i < t.count; i++) {
Christoph Lameter45edfa52007-05-09 02:32:45 -07003019 struct location *l = &t.loc[i];
Christoph Lameter88a420e2007-05-06 14:49:45 -07003020
3021 if (n > PAGE_SIZE - 100)
3022 break;
Christoph Lameter45edfa52007-05-09 02:32:45 -07003023 n += sprintf(buf + n, "%7ld ", l->count);
3024
3025 if (l->addr)
3026 n += sprint_symbol(buf + n, (unsigned long)l->addr);
Christoph Lameter88a420e2007-05-06 14:49:45 -07003027 else
3028 n += sprintf(buf + n, "<not-available>");
Christoph Lameter45edfa52007-05-09 02:32:45 -07003029
3030 if (l->sum_time != l->min_time) {
3031 unsigned long remainder;
3032
3033 n += sprintf(buf + n, " age=%ld/%ld/%ld",
3034 l->min_time,
3035 div_long_long_rem(l->sum_time, l->count, &remainder),
3036 l->max_time);
3037 } else
3038 n += sprintf(buf + n, " age=%ld",
3039 l->min_time);
3040
3041 if (l->min_pid != l->max_pid)
3042 n += sprintf(buf + n, " pid=%ld-%ld",
3043 l->min_pid, l->max_pid);
3044 else
3045 n += sprintf(buf + n, " pid=%ld",
3046 l->min_pid);
3047
Christoph Lameter84966342007-06-23 17:16:32 -07003048 if (num_online_cpus() > 1 && !cpus_empty(l->cpus) &&
3049 n < PAGE_SIZE - 60) {
Christoph Lameter45edfa52007-05-09 02:32:45 -07003050 n += sprintf(buf + n, " cpus=");
3051 n += cpulist_scnprintf(buf + n, PAGE_SIZE - n - 50,
3052 l->cpus);
3053 }
3054
Christoph Lameter84966342007-06-23 17:16:32 -07003055 if (num_online_nodes() > 1 && !nodes_empty(l->nodes) &&
3056 n < PAGE_SIZE - 60) {
Christoph Lameter45edfa52007-05-09 02:32:45 -07003057 n += sprintf(buf + n, " nodes=");
3058 n += nodelist_scnprintf(buf + n, PAGE_SIZE - n - 50,
3059 l->nodes);
3060 }
3061
Christoph Lameter88a420e2007-05-06 14:49:45 -07003062 n += sprintf(buf + n, "\n");
3063 }
3064
3065 free_loc_track(&t);
3066 if (!t.count)
3067 n += sprintf(buf, "No data\n");
3068 return n;
3069}
3070
Christoph Lameter81819f02007-05-06 14:49:36 -07003071static unsigned long count_partial(struct kmem_cache_node *n)
3072{
3073 unsigned long flags;
3074 unsigned long x = 0;
3075 struct page *page;
3076
3077 spin_lock_irqsave(&n->list_lock, flags);
3078 list_for_each_entry(page, &n->partial, lru)
3079 x += page->inuse;
3080 spin_unlock_irqrestore(&n->list_lock, flags);
3081 return x;
3082}
3083
3084enum slab_stat_type {
3085 SL_FULL,
3086 SL_PARTIAL,
3087 SL_CPU,
3088 SL_OBJECTS
3089};
3090
3091#define SO_FULL (1 << SL_FULL)
3092#define SO_PARTIAL (1 << SL_PARTIAL)
3093#define SO_CPU (1 << SL_CPU)
3094#define SO_OBJECTS (1 << SL_OBJECTS)
3095
3096static unsigned long slab_objects(struct kmem_cache *s,
3097 char *buf, unsigned long flags)
3098{
3099 unsigned long total = 0;
3100 int cpu;
3101 int node;
3102 int x;
3103 unsigned long *nodes;
3104 unsigned long *per_cpu;
3105
3106 nodes = kzalloc(2 * sizeof(unsigned long) * nr_node_ids, GFP_KERNEL);
3107 per_cpu = nodes + nr_node_ids;
3108
3109 for_each_possible_cpu(cpu) {
3110 struct page *page = s->cpu_slab[cpu];
3111 int node;
3112
3113 if (page) {
3114 node = page_to_nid(page);
3115 if (flags & SO_CPU) {
3116 int x = 0;
3117
3118 if (flags & SO_OBJECTS)
3119 x = page->inuse;
3120 else
3121 x = 1;
3122 total += x;
3123 nodes[node] += x;
3124 }
3125 per_cpu[node]++;
3126 }
3127 }
3128
3129 for_each_online_node(node) {
3130 struct kmem_cache_node *n = get_node(s, node);
3131
3132 if (flags & SO_PARTIAL) {
3133 if (flags & SO_OBJECTS)
3134 x = count_partial(n);
3135 else
3136 x = n->nr_partial;
3137 total += x;
3138 nodes[node] += x;
3139 }
3140
3141 if (flags & SO_FULL) {
3142 int full_slabs = atomic_read(&n->nr_slabs)
3143 - per_cpu[node]
3144 - n->nr_partial;
3145
3146 if (flags & SO_OBJECTS)
3147 x = full_slabs * s->objects;
3148 else
3149 x = full_slabs;
3150 total += x;
3151 nodes[node] += x;
3152 }
3153 }
3154
3155 x = sprintf(buf, "%lu", total);
3156#ifdef CONFIG_NUMA
3157 for_each_online_node(node)
3158 if (nodes[node])
3159 x += sprintf(buf + x, " N%d=%lu",
3160 node, nodes[node]);
3161#endif
3162 kfree(nodes);
3163 return x + sprintf(buf + x, "\n");
3164}
3165
3166static int any_slab_objects(struct kmem_cache *s)
3167{
3168 int node;
3169 int cpu;
3170
3171 for_each_possible_cpu(cpu)
3172 if (s->cpu_slab[cpu])
3173 return 1;
3174
3175 for_each_node(node) {
3176 struct kmem_cache_node *n = get_node(s, node);
3177
3178 if (n->nr_partial || atomic_read(&n->nr_slabs))
3179 return 1;
3180 }
3181 return 0;
3182}
3183
3184#define to_slab_attr(n) container_of(n, struct slab_attribute, attr)
3185#define to_slab(n) container_of(n, struct kmem_cache, kobj);
3186
3187struct slab_attribute {
3188 struct attribute attr;
3189 ssize_t (*show)(struct kmem_cache *s, char *buf);
3190 ssize_t (*store)(struct kmem_cache *s, const char *x, size_t count);
3191};
3192
3193#define SLAB_ATTR_RO(_name) \
3194 static struct slab_attribute _name##_attr = __ATTR_RO(_name)
3195
3196#define SLAB_ATTR(_name) \
3197 static struct slab_attribute _name##_attr = \
3198 __ATTR(_name, 0644, _name##_show, _name##_store)
3199
Christoph Lameter81819f02007-05-06 14:49:36 -07003200static ssize_t slab_size_show(struct kmem_cache *s, char *buf)
3201{
3202 return sprintf(buf, "%d\n", s->size);
3203}
3204SLAB_ATTR_RO(slab_size);
3205
3206static ssize_t align_show(struct kmem_cache *s, char *buf)
3207{
3208 return sprintf(buf, "%d\n", s->align);
3209}
3210SLAB_ATTR_RO(align);
3211
3212static ssize_t object_size_show(struct kmem_cache *s, char *buf)
3213{
3214 return sprintf(buf, "%d\n", s->objsize);
3215}
3216SLAB_ATTR_RO(object_size);
3217
3218static ssize_t objs_per_slab_show(struct kmem_cache *s, char *buf)
3219{
3220 return sprintf(buf, "%d\n", s->objects);
3221}
3222SLAB_ATTR_RO(objs_per_slab);
3223
3224static ssize_t order_show(struct kmem_cache *s, char *buf)
3225{
3226 return sprintf(buf, "%d\n", s->order);
3227}
3228SLAB_ATTR_RO(order);
3229
3230static ssize_t ctor_show(struct kmem_cache *s, char *buf)
3231{
3232 if (s->ctor) {
3233 int n = sprint_symbol(buf, (unsigned long)s->ctor);
3234
3235 return n + sprintf(buf + n, "\n");
3236 }
3237 return 0;
3238}
3239SLAB_ATTR_RO(ctor);
3240
Christoph Lameter81819f02007-05-06 14:49:36 -07003241static ssize_t aliases_show(struct kmem_cache *s, char *buf)
3242{
3243 return sprintf(buf, "%d\n", s->refcount - 1);
3244}
3245SLAB_ATTR_RO(aliases);
3246
3247static ssize_t slabs_show(struct kmem_cache *s, char *buf)
3248{
3249 return slab_objects(s, buf, SO_FULL|SO_PARTIAL|SO_CPU);
3250}
3251SLAB_ATTR_RO(slabs);
3252
3253static ssize_t partial_show(struct kmem_cache *s, char *buf)
3254{
3255 return slab_objects(s, buf, SO_PARTIAL);
3256}
3257SLAB_ATTR_RO(partial);
3258
3259static ssize_t cpu_slabs_show(struct kmem_cache *s, char *buf)
3260{
3261 return slab_objects(s, buf, SO_CPU);
3262}
3263SLAB_ATTR_RO(cpu_slabs);
3264
3265static ssize_t objects_show(struct kmem_cache *s, char *buf)
3266{
3267 return slab_objects(s, buf, SO_FULL|SO_PARTIAL|SO_CPU|SO_OBJECTS);
3268}
3269SLAB_ATTR_RO(objects);
3270
3271static ssize_t sanity_checks_show(struct kmem_cache *s, char *buf)
3272{
3273 return sprintf(buf, "%d\n", !!(s->flags & SLAB_DEBUG_FREE));
3274}
3275
3276static ssize_t sanity_checks_store(struct kmem_cache *s,
3277 const char *buf, size_t length)
3278{
3279 s->flags &= ~SLAB_DEBUG_FREE;
3280 if (buf[0] == '1')
3281 s->flags |= SLAB_DEBUG_FREE;
3282 return length;
3283}
3284SLAB_ATTR(sanity_checks);
3285
3286static ssize_t trace_show(struct kmem_cache *s, char *buf)
3287{
3288 return sprintf(buf, "%d\n", !!(s->flags & SLAB_TRACE));
3289}
3290
3291static ssize_t trace_store(struct kmem_cache *s, const char *buf,
3292 size_t length)
3293{
3294 s->flags &= ~SLAB_TRACE;
3295 if (buf[0] == '1')
3296 s->flags |= SLAB_TRACE;
3297 return length;
3298}
3299SLAB_ATTR(trace);
3300
3301static ssize_t reclaim_account_show(struct kmem_cache *s, char *buf)
3302{
3303 return sprintf(buf, "%d\n", !!(s->flags & SLAB_RECLAIM_ACCOUNT));
3304}
3305
3306static ssize_t reclaim_account_store(struct kmem_cache *s,
3307 const char *buf, size_t length)
3308{
3309 s->flags &= ~SLAB_RECLAIM_ACCOUNT;
3310 if (buf[0] == '1')
3311 s->flags |= SLAB_RECLAIM_ACCOUNT;
3312 return length;
3313}
3314SLAB_ATTR(reclaim_account);
3315
3316static ssize_t hwcache_align_show(struct kmem_cache *s, char *buf)
3317{
Christoph Lameter5af60832007-05-06 14:49:56 -07003318 return sprintf(buf, "%d\n", !!(s->flags & SLAB_HWCACHE_ALIGN));
Christoph Lameter81819f02007-05-06 14:49:36 -07003319}
3320SLAB_ATTR_RO(hwcache_align);
3321
3322#ifdef CONFIG_ZONE_DMA
3323static ssize_t cache_dma_show(struct kmem_cache *s, char *buf)
3324{
3325 return sprintf(buf, "%d\n", !!(s->flags & SLAB_CACHE_DMA));
3326}
3327SLAB_ATTR_RO(cache_dma);
3328#endif
3329
3330static ssize_t destroy_by_rcu_show(struct kmem_cache *s, char *buf)
3331{
3332 return sprintf(buf, "%d\n", !!(s->flags & SLAB_DESTROY_BY_RCU));
3333}
3334SLAB_ATTR_RO(destroy_by_rcu);
3335
3336static ssize_t red_zone_show(struct kmem_cache *s, char *buf)
3337{
3338 return sprintf(buf, "%d\n", !!(s->flags & SLAB_RED_ZONE));
3339}
3340
3341static ssize_t red_zone_store(struct kmem_cache *s,
3342 const char *buf, size_t length)
3343{
3344 if (any_slab_objects(s))
3345 return -EBUSY;
3346
3347 s->flags &= ~SLAB_RED_ZONE;
3348 if (buf[0] == '1')
3349 s->flags |= SLAB_RED_ZONE;
3350 calculate_sizes(s);
3351 return length;
3352}
3353SLAB_ATTR(red_zone);
3354
3355static ssize_t poison_show(struct kmem_cache *s, char *buf)
3356{
3357 return sprintf(buf, "%d\n", !!(s->flags & SLAB_POISON));
3358}
3359
3360static ssize_t poison_store(struct kmem_cache *s,
3361 const char *buf, size_t length)
3362{
3363 if (any_slab_objects(s))
3364 return -EBUSY;
3365
3366 s->flags &= ~SLAB_POISON;
3367 if (buf[0] == '1')
3368 s->flags |= SLAB_POISON;
3369 calculate_sizes(s);
3370 return length;
3371}
3372SLAB_ATTR(poison);
3373
3374static ssize_t store_user_show(struct kmem_cache *s, char *buf)
3375{
3376 return sprintf(buf, "%d\n", !!(s->flags & SLAB_STORE_USER));
3377}
3378
3379static ssize_t store_user_store(struct kmem_cache *s,
3380 const char *buf, size_t length)
3381{
3382 if (any_slab_objects(s))
3383 return -EBUSY;
3384
3385 s->flags &= ~SLAB_STORE_USER;
3386 if (buf[0] == '1')
3387 s->flags |= SLAB_STORE_USER;
3388 calculate_sizes(s);
3389 return length;
3390}
3391SLAB_ATTR(store_user);
3392
Christoph Lameter53e15af2007-05-06 14:49:43 -07003393static ssize_t validate_show(struct kmem_cache *s, char *buf)
3394{
3395 return 0;
3396}
3397
3398static ssize_t validate_store(struct kmem_cache *s,
3399 const char *buf, size_t length)
3400{
3401 if (buf[0] == '1')
3402 validate_slab_cache(s);
3403 else
3404 return -EINVAL;
3405 return length;
3406}
3407SLAB_ATTR(validate);
3408
Christoph Lameter2086d262007-05-06 14:49:46 -07003409static ssize_t shrink_show(struct kmem_cache *s, char *buf)
3410{
3411 return 0;
3412}
3413
3414static ssize_t shrink_store(struct kmem_cache *s,
3415 const char *buf, size_t length)
3416{
3417 if (buf[0] == '1') {
3418 int rc = kmem_cache_shrink(s);
3419
3420 if (rc)
3421 return rc;
3422 } else
3423 return -EINVAL;
3424 return length;
3425}
3426SLAB_ATTR(shrink);
3427
Christoph Lameter88a420e2007-05-06 14:49:45 -07003428static ssize_t alloc_calls_show(struct kmem_cache *s, char *buf)
3429{
3430 if (!(s->flags & SLAB_STORE_USER))
3431 return -ENOSYS;
3432 return list_locations(s, buf, TRACK_ALLOC);
3433}
3434SLAB_ATTR_RO(alloc_calls);
3435
3436static ssize_t free_calls_show(struct kmem_cache *s, char *buf)
3437{
3438 if (!(s->flags & SLAB_STORE_USER))
3439 return -ENOSYS;
3440 return list_locations(s, buf, TRACK_FREE);
3441}
3442SLAB_ATTR_RO(free_calls);
3443
Christoph Lameter81819f02007-05-06 14:49:36 -07003444#ifdef CONFIG_NUMA
3445static ssize_t defrag_ratio_show(struct kmem_cache *s, char *buf)
3446{
3447 return sprintf(buf, "%d\n", s->defrag_ratio / 10);
3448}
3449
3450static ssize_t defrag_ratio_store(struct kmem_cache *s,
3451 const char *buf, size_t length)
3452{
3453 int n = simple_strtoul(buf, NULL, 10);
3454
3455 if (n < 100)
3456 s->defrag_ratio = n * 10;
3457 return length;
3458}
3459SLAB_ATTR(defrag_ratio);
3460#endif
3461
3462static struct attribute * slab_attrs[] = {
3463 &slab_size_attr.attr,
3464 &object_size_attr.attr,
3465 &objs_per_slab_attr.attr,
3466 &order_attr.attr,
3467 &objects_attr.attr,
3468 &slabs_attr.attr,
3469 &partial_attr.attr,
3470 &cpu_slabs_attr.attr,
3471 &ctor_attr.attr,
Christoph Lameter81819f02007-05-06 14:49:36 -07003472 &aliases_attr.attr,
3473 &align_attr.attr,
3474 &sanity_checks_attr.attr,
3475 &trace_attr.attr,
3476 &hwcache_align_attr.attr,
3477 &reclaim_account_attr.attr,
3478 &destroy_by_rcu_attr.attr,
3479 &red_zone_attr.attr,
3480 &poison_attr.attr,
3481 &store_user_attr.attr,
Christoph Lameter53e15af2007-05-06 14:49:43 -07003482 &validate_attr.attr,
Christoph Lameter2086d262007-05-06 14:49:46 -07003483 &shrink_attr.attr,
Christoph Lameter88a420e2007-05-06 14:49:45 -07003484 &alloc_calls_attr.attr,
3485 &free_calls_attr.attr,
Christoph Lameter81819f02007-05-06 14:49:36 -07003486#ifdef CONFIG_ZONE_DMA
3487 &cache_dma_attr.attr,
3488#endif
3489#ifdef CONFIG_NUMA
3490 &defrag_ratio_attr.attr,
3491#endif
3492 NULL
3493};
3494
3495static struct attribute_group slab_attr_group = {
3496 .attrs = slab_attrs,
3497};
3498
3499static ssize_t slab_attr_show(struct kobject *kobj,
3500 struct attribute *attr,
3501 char *buf)
3502{
3503 struct slab_attribute *attribute;
3504 struct kmem_cache *s;
3505 int err;
3506
3507 attribute = to_slab_attr(attr);
3508 s = to_slab(kobj);
3509
3510 if (!attribute->show)
3511 return -EIO;
3512
3513 err = attribute->show(s, buf);
3514
3515 return err;
3516}
3517
3518static ssize_t slab_attr_store(struct kobject *kobj,
3519 struct attribute *attr,
3520 const char *buf, size_t len)
3521{
3522 struct slab_attribute *attribute;
3523 struct kmem_cache *s;
3524 int err;
3525
3526 attribute = to_slab_attr(attr);
3527 s = to_slab(kobj);
3528
3529 if (!attribute->store)
3530 return -EIO;
3531
3532 err = attribute->store(s, buf, len);
3533
3534 return err;
3535}
3536
3537static struct sysfs_ops slab_sysfs_ops = {
3538 .show = slab_attr_show,
3539 .store = slab_attr_store,
3540};
3541
3542static struct kobj_type slab_ktype = {
3543 .sysfs_ops = &slab_sysfs_ops,
3544};
3545
3546static int uevent_filter(struct kset *kset, struct kobject *kobj)
3547{
3548 struct kobj_type *ktype = get_ktype(kobj);
3549
3550 if (ktype == &slab_ktype)
3551 return 1;
3552 return 0;
3553}
3554
3555static struct kset_uevent_ops slab_uevent_ops = {
3556 .filter = uevent_filter,
3557};
3558
3559decl_subsys(slab, &slab_ktype, &slab_uevent_ops);
3560
3561#define ID_STR_LENGTH 64
3562
3563/* Create a unique string id for a slab cache:
3564 * format
3565 * :[flags-]size:[memory address of kmemcache]
3566 */
3567static char *create_unique_id(struct kmem_cache *s)
3568{
3569 char *name = kmalloc(ID_STR_LENGTH, GFP_KERNEL);
3570 char *p = name;
3571
3572 BUG_ON(!name);
3573
3574 *p++ = ':';
3575 /*
3576 * First flags affecting slabcache operations. We will only
3577 * get here for aliasable slabs so we do not need to support
3578 * too many flags. The flags here must cover all flags that
3579 * are matched during merging to guarantee that the id is
3580 * unique.
3581 */
3582 if (s->flags & SLAB_CACHE_DMA)
3583 *p++ = 'd';
3584 if (s->flags & SLAB_RECLAIM_ACCOUNT)
3585 *p++ = 'a';
3586 if (s->flags & SLAB_DEBUG_FREE)
3587 *p++ = 'F';
3588 if (p != name + 1)
3589 *p++ = '-';
3590 p += sprintf(p, "%07d", s->size);
3591 BUG_ON(p > name + ID_STR_LENGTH - 1);
3592 return name;
3593}
3594
3595static int sysfs_slab_add(struct kmem_cache *s)
3596{
3597 int err;
3598 const char *name;
3599 int unmergeable;
3600
3601 if (slab_state < SYSFS)
3602 /* Defer until later */
3603 return 0;
3604
3605 unmergeable = slab_unmergeable(s);
3606 if (unmergeable) {
3607 /*
3608 * Slabcache can never be merged so we can use the name proper.
3609 * This is typically the case for debug situations. In that
3610 * case we can catch duplicate names easily.
3611 */
Linus Torvalds0f9008e2007-05-07 12:31:58 -07003612 sysfs_remove_link(&slab_subsys.kobj, s->name);
Christoph Lameter81819f02007-05-06 14:49:36 -07003613 name = s->name;
3614 } else {
3615 /*
3616 * Create a unique name for the slab as a target
3617 * for the symlinks.
3618 */
3619 name = create_unique_id(s);
3620 }
3621
3622 kobj_set_kset_s(s, slab_subsys);
3623 kobject_set_name(&s->kobj, name);
3624 kobject_init(&s->kobj);
3625 err = kobject_add(&s->kobj);
3626 if (err)
3627 return err;
3628
3629 err = sysfs_create_group(&s->kobj, &slab_attr_group);
3630 if (err)
3631 return err;
3632 kobject_uevent(&s->kobj, KOBJ_ADD);
3633 if (!unmergeable) {
3634 /* Setup first alias */
3635 sysfs_slab_alias(s, s->name);
3636 kfree(name);
3637 }
3638 return 0;
3639}
3640
3641static void sysfs_slab_remove(struct kmem_cache *s)
3642{
3643 kobject_uevent(&s->kobj, KOBJ_REMOVE);
3644 kobject_del(&s->kobj);
3645}
3646
3647/*
3648 * Need to buffer aliases during bootup until sysfs becomes
3649 * available lest we loose that information.
3650 */
3651struct saved_alias {
3652 struct kmem_cache *s;
3653 const char *name;
3654 struct saved_alias *next;
3655};
3656
3657struct saved_alias *alias_list;
3658
3659static int sysfs_slab_alias(struct kmem_cache *s, const char *name)
3660{
3661 struct saved_alias *al;
3662
3663 if (slab_state == SYSFS) {
3664 /*
3665 * If we have a leftover link then remove it.
3666 */
Linus Torvalds0f9008e2007-05-07 12:31:58 -07003667 sysfs_remove_link(&slab_subsys.kobj, name);
3668 return sysfs_create_link(&slab_subsys.kobj,
Christoph Lameter81819f02007-05-06 14:49:36 -07003669 &s->kobj, name);
3670 }
3671
3672 al = kmalloc(sizeof(struct saved_alias), GFP_KERNEL);
3673 if (!al)
3674 return -ENOMEM;
3675
3676 al->s = s;
3677 al->name = name;
3678 al->next = alias_list;
3679 alias_list = al;
3680 return 0;
3681}
3682
3683static int __init slab_sysfs_init(void)
3684{
Christoph Lameter26a7bd02007-05-09 02:32:39 -07003685 struct list_head *h;
Christoph Lameter81819f02007-05-06 14:49:36 -07003686 int err;
3687
3688 err = subsystem_register(&slab_subsys);
3689 if (err) {
3690 printk(KERN_ERR "Cannot register slab subsystem.\n");
3691 return -ENOSYS;
3692 }
3693
Christoph Lameter26a7bd02007-05-09 02:32:39 -07003694 slab_state = SYSFS;
3695
3696 list_for_each(h, &slab_caches) {
3697 struct kmem_cache *s =
3698 container_of(h, struct kmem_cache, list);
3699
3700 err = sysfs_slab_add(s);
3701 BUG_ON(err);
3702 }
Christoph Lameter81819f02007-05-06 14:49:36 -07003703
3704 while (alias_list) {
3705 struct saved_alias *al = alias_list;
3706
3707 alias_list = alias_list->next;
3708 err = sysfs_slab_alias(al->s, al->name);
3709 BUG_ON(err);
3710 kfree(al);
3711 }
3712
3713 resiliency_test();
3714 return 0;
3715}
3716
3717__initcall(slab_sysfs_init);
Christoph Lameter81819f02007-05-06 14:49:36 -07003718#endif