blob: a581fa8ae11aa60f264a296534fa991fd8064dfe [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 *
81 * PageActive The slab is used as a cpu cache. Allocations
82 * may be performed from the slab. The slab is not
83 * on any slab list and cannot be moved onto one.
84 *
85 * PageError Slab requires special handling due to debug
86 * options set. This moves slab handling out of
87 * the fast path.
88 */
89
Christoph Lameter35e5d7e2007-05-09 02:32:42 -070090static inline int SlabDebug(struct page *page)
91{
Christoph Lameter41ecc552007-05-09 02:32:44 -070092#ifdef CONFIG_SLUB_DEBUG
Christoph Lameter35e5d7e2007-05-09 02:32:42 -070093 return PageError(page);
Christoph Lameter41ecc552007-05-09 02:32:44 -070094#else
95 return 0;
96#endif
Christoph Lameter35e5d7e2007-05-09 02:32:42 -070097}
98
99static inline void SetSlabDebug(struct page *page)
100{
Christoph Lameter41ecc552007-05-09 02:32:44 -0700101#ifdef CONFIG_SLUB_DEBUG
Christoph Lameter35e5d7e2007-05-09 02:32:42 -0700102 SetPageError(page);
Christoph Lameter41ecc552007-05-09 02:32:44 -0700103#endif
Christoph Lameter35e5d7e2007-05-09 02:32:42 -0700104}
105
106static inline void ClearSlabDebug(struct page *page)
107{
Christoph Lameter41ecc552007-05-09 02:32:44 -0700108#ifdef CONFIG_SLUB_DEBUG
Christoph Lameter35e5d7e2007-05-09 02:32:42 -0700109 ClearPageError(page);
Christoph Lameter41ecc552007-05-09 02:32:44 -0700110#endif
Christoph Lameter35e5d7e2007-05-09 02:32:42 -0700111}
112
Christoph Lameter81819f02007-05-06 14:49:36 -0700113/*
114 * Issues still to be resolved:
115 *
116 * - The per cpu array is updated for each new slab and and is a remote
117 * cacheline for most nodes. This could become a bouncing cacheline given
Christoph Lameter672bba32007-05-09 02:32:39 -0700118 * enough frequent updates. There are 16 pointers in a cacheline, so at
119 * max 16 cpus could compete for the cacheline which may be okay.
Christoph Lameter81819f02007-05-06 14:49:36 -0700120 *
121 * - Support PAGE_ALLOC_DEBUG. Should be easy to do.
122 *
Christoph Lameter81819f02007-05-06 14:49:36 -0700123 * - Variable sizing of the per node arrays
124 */
125
126/* Enable to test recovery from slab corruption on boot */
127#undef SLUB_RESILIENCY_TEST
128
129#if PAGE_SHIFT <= 12
130
131/*
132 * Small page size. Make sure that we do not fragment memory
133 */
134#define DEFAULT_MAX_ORDER 1
135#define DEFAULT_MIN_OBJECTS 4
136
137#else
138
139/*
140 * Large page machines are customarily able to handle larger
141 * page orders.
142 */
143#define DEFAULT_MAX_ORDER 2
144#define DEFAULT_MIN_OBJECTS 8
145
146#endif
147
148/*
Christoph Lameter2086d262007-05-06 14:49:46 -0700149 * Mininum number of partial slabs. These will be left on the partial
150 * lists even if they are empty. kmem_cache_shrink may reclaim them.
151 */
Christoph Lametere95eed52007-05-06 14:49:44 -0700152#define MIN_PARTIAL 2
153
Christoph Lameter2086d262007-05-06 14:49:46 -0700154/*
155 * Maximum number of desirable partial slabs.
156 * The existence of more partial slabs makes kmem_cache_shrink
157 * sort the partial list by the number of objects in the.
158 */
159#define MAX_PARTIAL 10
160
Christoph Lameter81819f02007-05-06 14:49:36 -0700161#define DEBUG_DEFAULT_FLAGS (SLAB_DEBUG_FREE | SLAB_RED_ZONE | \
162 SLAB_POISON | SLAB_STORE_USER)
Christoph Lameter672bba32007-05-09 02:32:39 -0700163
Christoph Lameter81819f02007-05-06 14:49:36 -0700164/*
165 * Set of flags that will prevent slab merging
166 */
167#define SLUB_NEVER_MERGE (SLAB_RED_ZONE | SLAB_POISON | SLAB_STORE_USER | \
168 SLAB_TRACE | SLAB_DESTROY_BY_RCU)
169
170#define SLUB_MERGE_SAME (SLAB_DEBUG_FREE | SLAB_RECLAIM_ACCOUNT | \
171 SLAB_CACHE_DMA)
172
173#ifndef ARCH_KMALLOC_MINALIGN
Christoph Lameter47bfdc02007-05-06 14:49:37 -0700174#define ARCH_KMALLOC_MINALIGN __alignof__(unsigned long long)
Christoph Lameter81819f02007-05-06 14:49:36 -0700175#endif
176
177#ifndef ARCH_SLAB_MINALIGN
Christoph Lameter47bfdc02007-05-06 14:49:37 -0700178#define ARCH_SLAB_MINALIGN __alignof__(unsigned long long)
Christoph Lameter81819f02007-05-06 14:49:36 -0700179#endif
180
181/* Internal SLUB flags */
182#define __OBJECT_POISON 0x80000000 /* Poison object */
183
Christoph Lameter65c02d42007-05-09 02:32:35 -0700184/* Not all arches define cache_line_size */
185#ifndef cache_line_size
186#define cache_line_size() L1_CACHE_BYTES
187#endif
188
Christoph Lameter81819f02007-05-06 14:49:36 -0700189static int kmem_size = sizeof(struct kmem_cache);
190
191#ifdef CONFIG_SMP
192static struct notifier_block slab_notifier;
193#endif
194
195static enum {
196 DOWN, /* No slab functionality available */
197 PARTIAL, /* kmem_cache_open() works but kmalloc does not */
Christoph Lameter672bba32007-05-09 02:32:39 -0700198 UP, /* Everything works but does not show up in sysfs */
Christoph Lameter81819f02007-05-06 14:49:36 -0700199 SYSFS /* Sysfs up */
200} slab_state = DOWN;
201
202/* A list of all slab caches on the system */
203static DECLARE_RWSEM(slub_lock);
204LIST_HEAD(slab_caches);
205
Christoph Lameter02cbc872007-05-09 02:32:43 -0700206/*
207 * Tracking user of a slab.
208 */
209struct track {
210 void *addr; /* Called from address */
211 int cpu; /* Was running on cpu */
212 int pid; /* Pid context */
213 unsigned long when; /* When did the operation occur */
214};
215
216enum track_item { TRACK_ALLOC, TRACK_FREE };
217
Christoph Lameter41ecc552007-05-09 02:32:44 -0700218#if defined(CONFIG_SYSFS) && defined(CONFIG_SLUB_DEBUG)
Christoph Lameter81819f02007-05-06 14:49:36 -0700219static int sysfs_slab_add(struct kmem_cache *);
220static int sysfs_slab_alias(struct kmem_cache *, const char *);
221static void sysfs_slab_remove(struct kmem_cache *);
222#else
223static int sysfs_slab_add(struct kmem_cache *s) { return 0; }
224static int sysfs_slab_alias(struct kmem_cache *s, const char *p) { return 0; }
225static void sysfs_slab_remove(struct kmem_cache *s) {}
226#endif
227
228/********************************************************************
229 * Core slab cache functions
230 *******************************************************************/
231
232int slab_is_available(void)
233{
234 return slab_state >= UP;
235}
236
237static inline struct kmem_cache_node *get_node(struct kmem_cache *s, int node)
238{
239#ifdef CONFIG_NUMA
240 return s->node[node];
241#else
242 return &s->local_node;
243#endif
244}
245
Christoph Lameter02cbc872007-05-09 02:32:43 -0700246static inline int check_valid_pointer(struct kmem_cache *s,
247 struct page *page, const void *object)
248{
249 void *base;
250
251 if (!object)
252 return 1;
253
254 base = page_address(page);
255 if (object < base || object >= base + s->objects * s->size ||
256 (object - base) % s->size) {
257 return 0;
258 }
259
260 return 1;
261}
262
Christoph Lameter81819f02007-05-06 14:49:36 -0700263/*
Christoph Lameter7656c722007-05-09 02:32:40 -0700264 * Slow version of get and set free pointer.
265 *
266 * This version requires touching the cache lines of kmem_cache which
267 * we avoid to do in the fast alloc free paths. There we obtain the offset
268 * from the page struct.
269 */
270static inline void *get_freepointer(struct kmem_cache *s, void *object)
271{
272 return *(void **)(object + s->offset);
273}
274
275static inline void set_freepointer(struct kmem_cache *s, void *object, void *fp)
276{
277 *(void **)(object + s->offset) = fp;
278}
279
280/* Loop over all objects in a slab */
281#define for_each_object(__p, __s, __addr) \
282 for (__p = (__addr); __p < (__addr) + (__s)->objects * (__s)->size;\
283 __p += (__s)->size)
284
285/* Scan freelist */
286#define for_each_free_object(__p, __s, __free) \
287 for (__p = (__free); __p; __p = get_freepointer((__s), __p))
288
289/* Determine object index from a given position */
290static inline int slab_index(void *p, struct kmem_cache *s, void *addr)
291{
292 return (p - addr) / s->size;
293}
294
Christoph Lameter41ecc552007-05-09 02:32:44 -0700295#ifdef CONFIG_SLUB_DEBUG
296/*
297 * Debug settings:
298 */
299static int slub_debug;
300
301static char *slub_debug_slabs;
302
Christoph Lameter7656c722007-05-09 02:32:40 -0700303/*
Christoph Lameter81819f02007-05-06 14:49:36 -0700304 * Object debugging
305 */
306static void print_section(char *text, u8 *addr, unsigned int length)
307{
308 int i, offset;
309 int newline = 1;
310 char ascii[17];
311
312 ascii[16] = 0;
313
314 for (i = 0; i < length; i++) {
315 if (newline) {
316 printk(KERN_ERR "%10s 0x%p: ", text, addr + i);
317 newline = 0;
318 }
319 printk(" %02x", addr[i]);
320 offset = i % 16;
321 ascii[offset] = isgraph(addr[i]) ? addr[i] : '.';
322 if (offset == 15) {
323 printk(" %s\n",ascii);
324 newline = 1;
325 }
326 }
327 if (!newline) {
328 i %= 16;
329 while (i < 16) {
330 printk(" ");
331 ascii[i] = ' ';
332 i++;
333 }
334 printk(" %s\n", ascii);
335 }
336}
337
Christoph Lameter81819f02007-05-06 14:49:36 -0700338static struct track *get_track(struct kmem_cache *s, void *object,
339 enum track_item alloc)
340{
341 struct track *p;
342
343 if (s->offset)
344 p = object + s->offset + sizeof(void *);
345 else
346 p = object + s->inuse;
347
348 return p + alloc;
349}
350
351static void set_track(struct kmem_cache *s, void *object,
352 enum track_item alloc, void *addr)
353{
354 struct track *p;
355
356 if (s->offset)
357 p = object + s->offset + sizeof(void *);
358 else
359 p = object + s->inuse;
360
361 p += alloc;
362 if (addr) {
363 p->addr = addr;
364 p->cpu = smp_processor_id();
365 p->pid = current ? current->pid : -1;
366 p->when = jiffies;
367 } else
368 memset(p, 0, sizeof(struct track));
369}
370
Christoph Lameter81819f02007-05-06 14:49:36 -0700371static void init_tracking(struct kmem_cache *s, void *object)
372{
373 if (s->flags & SLAB_STORE_USER) {
374 set_track(s, object, TRACK_FREE, NULL);
375 set_track(s, object, TRACK_ALLOC, NULL);
376 }
377}
378
379static void print_track(const char *s, struct track *t)
380{
381 if (!t->addr)
382 return;
383
384 printk(KERN_ERR "%s: ", s);
385 __print_symbol("%s", (unsigned long)t->addr);
386 printk(" jiffies_ago=%lu cpu=%u pid=%d\n", jiffies - t->when, t->cpu, t->pid);
387}
388
389static void print_trailer(struct kmem_cache *s, u8 *p)
390{
391 unsigned int off; /* Offset of last byte */
392
393 if (s->flags & SLAB_RED_ZONE)
394 print_section("Redzone", p + s->objsize,
395 s->inuse - s->objsize);
396
397 printk(KERN_ERR "FreePointer 0x%p -> 0x%p\n",
398 p + s->offset,
399 get_freepointer(s, p));
400
401 if (s->offset)
402 off = s->offset + sizeof(void *);
403 else
404 off = s->inuse;
405
406 if (s->flags & SLAB_STORE_USER) {
407 print_track("Last alloc", get_track(s, p, TRACK_ALLOC));
408 print_track("Last free ", get_track(s, p, TRACK_FREE));
409 off += 2 * sizeof(struct track);
410 }
411
412 if (off != s->size)
413 /* Beginning of the filler is the free pointer */
414 print_section("Filler", p + off, s->size - off);
415}
416
417static void object_err(struct kmem_cache *s, struct page *page,
418 u8 *object, char *reason)
419{
420 u8 *addr = page_address(page);
421
422 printk(KERN_ERR "*** SLUB %s: %s@0x%p slab 0x%p\n",
423 s->name, reason, object, page);
424 printk(KERN_ERR " offset=%tu flags=0x%04lx inuse=%u freelist=0x%p\n",
425 object - addr, page->flags, page->inuse, page->freelist);
426 if (object > addr + 16)
427 print_section("Bytes b4", object - 16, 16);
428 print_section("Object", object, min(s->objsize, 128));
429 print_trailer(s, object);
430 dump_stack();
431}
432
433static void slab_err(struct kmem_cache *s, struct page *page, char *reason, ...)
434{
435 va_list args;
436 char buf[100];
437
438 va_start(args, reason);
439 vsnprintf(buf, sizeof(buf), reason, args);
440 va_end(args);
441 printk(KERN_ERR "*** SLUB %s: %s in slab @0x%p\n", s->name, buf,
442 page);
443 dump_stack();
444}
445
446static void init_object(struct kmem_cache *s, void *object, int active)
447{
448 u8 *p = object;
449
450 if (s->flags & __OBJECT_POISON) {
451 memset(p, POISON_FREE, s->objsize - 1);
452 p[s->objsize -1] = POISON_END;
453 }
454
455 if (s->flags & SLAB_RED_ZONE)
456 memset(p + s->objsize,
457 active ? SLUB_RED_ACTIVE : SLUB_RED_INACTIVE,
458 s->inuse - s->objsize);
459}
460
461static int check_bytes(u8 *start, unsigned int value, unsigned int bytes)
462{
463 while (bytes) {
464 if (*start != (u8)value)
465 return 0;
466 start++;
467 bytes--;
468 }
469 return 1;
470}
471
Christoph Lameter81819f02007-05-06 14:49:36 -0700472/*
473 * Object layout:
474 *
475 * object address
476 * Bytes of the object to be managed.
477 * If the freepointer may overlay the object then the free
478 * pointer is the first word of the object.
Christoph Lameter672bba32007-05-09 02:32:39 -0700479 *
Christoph Lameter81819f02007-05-06 14:49:36 -0700480 * Poisoning uses 0x6b (POISON_FREE) and the last byte is
481 * 0xa5 (POISON_END)
482 *
483 * object + s->objsize
484 * Padding to reach word boundary. This is also used for Redzoning.
Christoph Lameter672bba32007-05-09 02:32:39 -0700485 * Padding is extended by another word if Redzoning is enabled and
486 * objsize == inuse.
487 *
Christoph Lameter81819f02007-05-06 14:49:36 -0700488 * We fill with 0xbb (RED_INACTIVE) for inactive objects and with
489 * 0xcc (RED_ACTIVE) for objects in use.
490 *
491 * object + s->inuse
Christoph Lameter672bba32007-05-09 02:32:39 -0700492 * Meta data starts here.
493 *
Christoph Lameter81819f02007-05-06 14:49:36 -0700494 * A. Free pointer (if we cannot overwrite object on free)
495 * B. Tracking data for SLAB_STORE_USER
Christoph Lameter672bba32007-05-09 02:32:39 -0700496 * C. Padding to reach required alignment boundary or at mininum
497 * one word if debuggin is on to be able to detect writes
498 * before the word boundary.
499 *
500 * Padding is done using 0x5a (POISON_INUSE)
Christoph Lameter81819f02007-05-06 14:49:36 -0700501 *
502 * object + s->size
Christoph Lameter672bba32007-05-09 02:32:39 -0700503 * Nothing is used beyond s->size.
Christoph Lameter81819f02007-05-06 14:49:36 -0700504 *
Christoph Lameter672bba32007-05-09 02:32:39 -0700505 * If slabcaches are merged then the objsize and inuse boundaries are mostly
506 * ignored. And therefore no slab options that rely on these boundaries
Christoph Lameter81819f02007-05-06 14:49:36 -0700507 * may be used with merged slabcaches.
508 */
509
510static void restore_bytes(struct kmem_cache *s, char *message, u8 data,
511 void *from, void *to)
512{
Christoph Lameter70d71222007-05-06 14:49:47 -0700513 printk(KERN_ERR "@@@ SLUB %s: Restoring %s (0x%x) from 0x%p-0x%p\n",
Christoph Lameter81819f02007-05-06 14:49:36 -0700514 s->name, message, data, from, to - 1);
515 memset(from, data, to - from);
516}
517
518static int check_pad_bytes(struct kmem_cache *s, struct page *page, u8 *p)
519{
520 unsigned long off = s->inuse; /* The end of info */
521
522 if (s->offset)
523 /* Freepointer is placed after the object. */
524 off += sizeof(void *);
525
526 if (s->flags & SLAB_STORE_USER)
527 /* We also have user information there */
528 off += 2 * sizeof(struct track);
529
530 if (s->size == off)
531 return 1;
532
533 if (check_bytes(p + off, POISON_INUSE, s->size - off))
534 return 1;
535
536 object_err(s, page, p, "Object padding check fails");
537
538 /*
539 * Restore padding
540 */
541 restore_bytes(s, "object padding", POISON_INUSE, p + off, p + s->size);
542 return 0;
543}
544
545static int slab_pad_check(struct kmem_cache *s, struct page *page)
546{
547 u8 *p;
548 int length, remainder;
549
550 if (!(s->flags & SLAB_POISON))
551 return 1;
552
553 p = page_address(page);
554 length = s->objects * s->size;
555 remainder = (PAGE_SIZE << s->order) - length;
556 if (!remainder)
557 return 1;
558
559 if (!check_bytes(p + length, POISON_INUSE, remainder)) {
Christoph Lameter70d71222007-05-06 14:49:47 -0700560 slab_err(s, page, "Padding check failed");
Christoph Lameter81819f02007-05-06 14:49:36 -0700561 restore_bytes(s, "slab padding", POISON_INUSE, p + length,
562 p + length + remainder);
563 return 0;
564 }
565 return 1;
566}
567
568static int check_object(struct kmem_cache *s, struct page *page,
569 void *object, int active)
570{
571 u8 *p = object;
572 u8 *endobject = object + s->objsize;
573
574 if (s->flags & SLAB_RED_ZONE) {
575 unsigned int red =
576 active ? SLUB_RED_ACTIVE : SLUB_RED_INACTIVE;
577
578 if (!check_bytes(endobject, red, s->inuse - s->objsize)) {
579 object_err(s, page, object,
580 active ? "Redzone Active" : "Redzone Inactive");
581 restore_bytes(s, "redzone", red,
582 endobject, object + s->inuse);
583 return 0;
584 }
585 } else {
586 if ((s->flags & SLAB_POISON) && s->objsize < s->inuse &&
587 !check_bytes(endobject, POISON_INUSE,
588 s->inuse - s->objsize)) {
589 object_err(s, page, p, "Alignment padding check fails");
590 /*
591 * Fix it so that there will not be another report.
592 *
593 * Hmmm... We may be corrupting an object that now expects
594 * to be longer than allowed.
595 */
596 restore_bytes(s, "alignment padding", POISON_INUSE,
597 endobject, object + s->inuse);
598 }
599 }
600
601 if (s->flags & SLAB_POISON) {
602 if (!active && (s->flags & __OBJECT_POISON) &&
603 (!check_bytes(p, POISON_FREE, s->objsize - 1) ||
604 p[s->objsize - 1] != POISON_END)) {
605
606 object_err(s, page, p, "Poison check failed");
607 restore_bytes(s, "Poison", POISON_FREE,
608 p, p + s->objsize -1);
609 restore_bytes(s, "Poison", POISON_END,
610 p + s->objsize - 1, p + s->objsize);
611 return 0;
612 }
613 /*
614 * check_pad_bytes cleans up on its own.
615 */
616 check_pad_bytes(s, page, p);
617 }
618
619 if (!s->offset && active)
620 /*
621 * Object and freepointer overlap. Cannot check
622 * freepointer while object is allocated.
623 */
624 return 1;
625
626 /* Check free pointer validity */
627 if (!check_valid_pointer(s, page, get_freepointer(s, p))) {
628 object_err(s, page, p, "Freepointer corrupt");
629 /*
630 * No choice but to zap it and thus loose the remainder
631 * of the free objects in this slab. May cause
Christoph Lameter672bba32007-05-09 02:32:39 -0700632 * another error because the object count is now wrong.
Christoph Lameter81819f02007-05-06 14:49:36 -0700633 */
634 set_freepointer(s, p, NULL);
635 return 0;
636 }
637 return 1;
638}
639
640static int check_slab(struct kmem_cache *s, struct page *page)
641{
642 VM_BUG_ON(!irqs_disabled());
643
644 if (!PageSlab(page)) {
Christoph Lameter70d71222007-05-06 14:49:47 -0700645 slab_err(s, page, "Not a valid slab page flags=%lx "
646 "mapping=0x%p count=%d", page->flags, page->mapping,
Christoph Lameter81819f02007-05-06 14:49:36 -0700647 page_count(page));
648 return 0;
649 }
650 if (page->offset * sizeof(void *) != s->offset) {
Christoph Lameter70d71222007-05-06 14:49:47 -0700651 slab_err(s, page, "Corrupted offset %lu flags=0x%lx "
652 "mapping=0x%p count=%d",
Christoph Lameter81819f02007-05-06 14:49:36 -0700653 (unsigned long)(page->offset * sizeof(void *)),
Christoph Lameter81819f02007-05-06 14:49:36 -0700654 page->flags,
655 page->mapping,
656 page_count(page));
Christoph Lameter81819f02007-05-06 14:49:36 -0700657 return 0;
658 }
659 if (page->inuse > s->objects) {
Christoph Lameter70d71222007-05-06 14:49:47 -0700660 slab_err(s, page, "inuse %u > max %u @0x%p flags=%lx "
661 "mapping=0x%p count=%d",
662 s->name, page->inuse, s->objects, page->flags,
Christoph Lameter81819f02007-05-06 14:49:36 -0700663 page->mapping, page_count(page));
Christoph Lameter81819f02007-05-06 14:49:36 -0700664 return 0;
665 }
666 /* Slab_pad_check fixes things up after itself */
667 slab_pad_check(s, page);
668 return 1;
669}
670
671/*
Christoph Lameter672bba32007-05-09 02:32:39 -0700672 * Determine if a certain object on a page is on the freelist. Must hold the
673 * slab lock to guarantee that the chains are in a consistent state.
Christoph Lameter81819f02007-05-06 14:49:36 -0700674 */
675static int on_freelist(struct kmem_cache *s, struct page *page, void *search)
676{
677 int nr = 0;
678 void *fp = page->freelist;
679 void *object = NULL;
680
681 while (fp && nr <= s->objects) {
682 if (fp == search)
683 return 1;
684 if (!check_valid_pointer(s, page, fp)) {
685 if (object) {
686 object_err(s, page, object,
687 "Freechain corrupt");
688 set_freepointer(s, object, NULL);
689 break;
690 } else {
Christoph Lameter70d71222007-05-06 14:49:47 -0700691 slab_err(s, page, "Freepointer 0x%p corrupt",
692 fp);
Christoph Lameter81819f02007-05-06 14:49:36 -0700693 page->freelist = NULL;
694 page->inuse = s->objects;
Christoph Lameter70d71222007-05-06 14:49:47 -0700695 printk(KERN_ERR "@@@ SLUB %s: Freelist "
696 "cleared. Slab 0x%p\n",
697 s->name, page);
Christoph Lameter81819f02007-05-06 14:49:36 -0700698 return 0;
699 }
700 break;
701 }
702 object = fp;
703 fp = get_freepointer(s, object);
704 nr++;
705 }
706
707 if (page->inuse != s->objects - nr) {
Christoph Lameter70d71222007-05-06 14:49:47 -0700708 slab_err(s, page, "Wrong object count. Counter is %d but "
709 "counted were %d", s, page, page->inuse,
710 s->objects - nr);
Christoph Lameter81819f02007-05-06 14:49:36 -0700711 page->inuse = s->objects - nr;
Christoph Lameter70d71222007-05-06 14:49:47 -0700712 printk(KERN_ERR "@@@ SLUB %s: Object count adjusted. "
713 "Slab @0x%p\n", s->name, page);
Christoph Lameter81819f02007-05-06 14:49:36 -0700714 }
715 return search == NULL;
716}
717
Christoph Lameter643b1132007-05-06 14:49:42 -0700718/*
Christoph Lameter672bba32007-05-09 02:32:39 -0700719 * Tracking of fully allocated slabs for debugging purposes.
Christoph Lameter643b1132007-05-06 14:49:42 -0700720 */
Christoph Lametere95eed52007-05-06 14:49:44 -0700721static void add_full(struct kmem_cache_node *n, struct page *page)
Christoph Lameter643b1132007-05-06 14:49:42 -0700722{
Christoph Lameter643b1132007-05-06 14:49:42 -0700723 spin_lock(&n->list_lock);
724 list_add(&page->lru, &n->full);
725 spin_unlock(&n->list_lock);
726}
727
728static void remove_full(struct kmem_cache *s, struct page *page)
729{
730 struct kmem_cache_node *n;
731
732 if (!(s->flags & SLAB_STORE_USER))
733 return;
734
735 n = get_node(s, page_to_nid(page));
736
737 spin_lock(&n->list_lock);
738 list_del(&page->lru);
739 spin_unlock(&n->list_lock);
740}
741
Christoph Lameter81819f02007-05-06 14:49:36 -0700742static int alloc_object_checks(struct kmem_cache *s, struct page *page,
743 void *object)
744{
745 if (!check_slab(s, page))
746 goto bad;
747
748 if (object && !on_freelist(s, page, object)) {
Christoph Lameter70d71222007-05-06 14:49:47 -0700749 slab_err(s, page, "Object 0x%p already allocated", object);
750 goto bad;
Christoph Lameter81819f02007-05-06 14:49:36 -0700751 }
752
753 if (!check_valid_pointer(s, page, object)) {
754 object_err(s, page, object, "Freelist Pointer check fails");
Christoph Lameter70d71222007-05-06 14:49:47 -0700755 goto bad;
Christoph Lameter81819f02007-05-06 14:49:36 -0700756 }
757
758 if (!object)
759 return 1;
760
761 if (!check_object(s, page, object, 0))
762 goto bad;
Christoph Lameter81819f02007-05-06 14:49:36 -0700763
Christoph Lameter81819f02007-05-06 14:49:36 -0700764 return 1;
Christoph Lameter81819f02007-05-06 14:49:36 -0700765bad:
766 if (PageSlab(page)) {
767 /*
768 * If this is a slab page then lets do the best we can
769 * to avoid issues in the future. Marking all objects
Christoph Lameter672bba32007-05-09 02:32:39 -0700770 * as used avoids touching the remaining objects.
Christoph Lameter81819f02007-05-06 14:49:36 -0700771 */
772 printk(KERN_ERR "@@@ SLUB: %s slab 0x%p. Marking all objects used.\n",
773 s->name, page);
774 page->inuse = s->objects;
775 page->freelist = NULL;
776 /* Fix up fields that may be corrupted */
777 page->offset = s->offset / sizeof(void *);
778 }
779 return 0;
780}
781
782static int free_object_checks(struct kmem_cache *s, struct page *page,
783 void *object)
784{
785 if (!check_slab(s, page))
786 goto fail;
787
788 if (!check_valid_pointer(s, page, object)) {
Christoph Lameter70d71222007-05-06 14:49:47 -0700789 slab_err(s, page, "Invalid object pointer 0x%p", object);
Christoph Lameter81819f02007-05-06 14:49:36 -0700790 goto fail;
791 }
792
793 if (on_freelist(s, page, object)) {
Christoph Lameter70d71222007-05-06 14:49:47 -0700794 slab_err(s, page, "Object 0x%p already free", object);
Christoph Lameter81819f02007-05-06 14:49:36 -0700795 goto fail;
796 }
797
798 if (!check_object(s, page, object, 1))
799 return 0;
800
801 if (unlikely(s != page->slab)) {
802 if (!PageSlab(page))
Christoph Lameter70d71222007-05-06 14:49:47 -0700803 slab_err(s, page, "Attempt to free object(0x%p) "
804 "outside of slab", object);
Christoph Lameter81819f02007-05-06 14:49:36 -0700805 else
Christoph Lameter70d71222007-05-06 14:49:47 -0700806 if (!page->slab) {
Christoph Lameter81819f02007-05-06 14:49:36 -0700807 printk(KERN_ERR
Christoph Lameter70d71222007-05-06 14:49:47 -0700808 "SLUB <none>: no slab for object 0x%p.\n",
Christoph Lameter81819f02007-05-06 14:49:36 -0700809 object);
Christoph Lameter70d71222007-05-06 14:49:47 -0700810 dump_stack();
811 }
Christoph Lameter81819f02007-05-06 14:49:36 -0700812 else
Christoph Lameter70d71222007-05-06 14:49:47 -0700813 slab_err(s, page, "object at 0x%p belongs "
814 "to slab %s", object, page->slab->name);
Christoph Lameter81819f02007-05-06 14:49:36 -0700815 goto fail;
816 }
Christoph Lameter81819f02007-05-06 14:49:36 -0700817 return 1;
818fail:
Christoph Lameter81819f02007-05-06 14:49:36 -0700819 printk(KERN_ERR "@@@ SLUB: %s slab 0x%p object at 0x%p not freed.\n",
820 s->name, page, object);
821 return 0;
822}
823
Christoph Lameter636f0d72007-05-09 02:32:42 -0700824static void trace(struct kmem_cache *s, struct page *page, void *object, int alloc)
825{
826 if (s->flags & SLAB_TRACE) {
827 printk(KERN_INFO "TRACE %s %s 0x%p inuse=%d fp=0x%p\n",
828 s->name,
829 alloc ? "alloc" : "free",
830 object, page->inuse,
831 page->freelist);
832
833 if (!alloc)
834 print_section("Object", (void *)object, s->objsize);
835
836 dump_stack();
837 }
838}
839
Christoph Lameter41ecc552007-05-09 02:32:44 -0700840static int __init setup_slub_debug(char *str)
841{
842 if (!str || *str != '=')
843 slub_debug = DEBUG_DEFAULT_FLAGS;
844 else {
845 str++;
846 if (*str == 0 || *str == ',')
847 slub_debug = DEBUG_DEFAULT_FLAGS;
848 else
849 for( ;*str && *str != ','; str++)
850 switch (*str) {
851 case 'f' : case 'F' :
852 slub_debug |= SLAB_DEBUG_FREE;
853 break;
854 case 'z' : case 'Z' :
855 slub_debug |= SLAB_RED_ZONE;
856 break;
857 case 'p' : case 'P' :
858 slub_debug |= SLAB_POISON;
859 break;
860 case 'u' : case 'U' :
861 slub_debug |= SLAB_STORE_USER;
862 break;
863 case 't' : case 'T' :
864 slub_debug |= SLAB_TRACE;
865 break;
866 default:
867 printk(KERN_ERR "slub_debug option '%c' "
868 "unknown. skipped\n",*str);
869 }
870 }
871
872 if (*str == ',')
873 slub_debug_slabs = str + 1;
874 return 1;
875}
876
877__setup("slub_debug", setup_slub_debug);
878
879static void kmem_cache_open_debug_check(struct kmem_cache *s)
880{
881 /*
882 * The page->offset field is only 16 bit wide. This is an offset
883 * in units of words from the beginning of an object. If the slab
884 * size is bigger then we cannot move the free pointer behind the
885 * object anymore.
886 *
887 * On 32 bit platforms the limit is 256k. On 64bit platforms
888 * the limit is 512k.
889 *
890 * Debugging or ctor/dtors may create a need to move the free
891 * pointer. Fail if this happens.
892 */
893 if (s->size >= 65535 * sizeof(void *)) {
894 BUG_ON(s->flags & (SLAB_RED_ZONE | SLAB_POISON |
895 SLAB_STORE_USER | SLAB_DESTROY_BY_RCU));
896 BUG_ON(s->ctor || s->dtor);
897 }
898 else
899 /*
900 * Enable debugging if selected on the kernel commandline.
901 */
902 if (slub_debug && (!slub_debug_slabs ||
903 strncmp(slub_debug_slabs, s->name,
904 strlen(slub_debug_slabs)) == 0))
905 s->flags |= slub_debug;
906}
907#else
908
909static inline int alloc_object_checks(struct kmem_cache *s,
910 struct page *page, void *object) { return 0; }
911
912static inline int free_object_checks(struct kmem_cache *s,
913 struct page *page, void *object) { return 0; }
914
915static inline void add_full(struct kmem_cache_node *n, struct page *page) {}
916static inline void remove_full(struct kmem_cache *s, struct page *page) {}
917static inline void trace(struct kmem_cache *s, struct page *page,
918 void *object, int alloc) {}
919static inline void init_object(struct kmem_cache *s,
920 void *object, int active) {}
921static inline void init_tracking(struct kmem_cache *s, void *object) {}
922static inline int slab_pad_check(struct kmem_cache *s, struct page *page)
923 { return 1; }
924static inline int check_object(struct kmem_cache *s, struct page *page,
925 void *object, int active) { return 1; }
926static inline void set_track(struct kmem_cache *s, void *object,
927 enum track_item alloc, void *addr) {}
928static inline void kmem_cache_open_debug_check(struct kmem_cache *s) {}
929#define slub_debug 0
930#endif
Christoph Lameter81819f02007-05-06 14:49:36 -0700931/*
932 * Slab allocation and freeing
933 */
934static struct page *allocate_slab(struct kmem_cache *s, gfp_t flags, int node)
935{
936 struct page * page;
937 int pages = 1 << s->order;
938
939 if (s->order)
940 flags |= __GFP_COMP;
941
942 if (s->flags & SLAB_CACHE_DMA)
943 flags |= SLUB_DMA;
944
945 if (node == -1)
946 page = alloc_pages(flags, s->order);
947 else
948 page = alloc_pages_node(node, flags, s->order);
949
950 if (!page)
951 return NULL;
952
953 mod_zone_page_state(page_zone(page),
954 (s->flags & SLAB_RECLAIM_ACCOUNT) ?
955 NR_SLAB_RECLAIMABLE : NR_SLAB_UNRECLAIMABLE,
956 pages);
957
958 return page;
959}
960
961static void setup_object(struct kmem_cache *s, struct page *page,
962 void *object)
963{
Christoph Lameter35e5d7e2007-05-09 02:32:42 -0700964 if (SlabDebug(page)) {
Christoph Lameter81819f02007-05-06 14:49:36 -0700965 init_object(s, object, 0);
966 init_tracking(s, object);
967 }
968
Christoph Lameter4f104932007-05-06 14:50:17 -0700969 if (unlikely(s->ctor))
970 s->ctor(object, s, SLAB_CTOR_CONSTRUCTOR);
Christoph Lameter81819f02007-05-06 14:49:36 -0700971}
972
973static struct page *new_slab(struct kmem_cache *s, gfp_t flags, int node)
974{
975 struct page *page;
976 struct kmem_cache_node *n;
977 void *start;
978 void *end;
979 void *last;
980 void *p;
981
Christoph Lameter81819f02007-05-06 14:49:36 -0700982 BUG_ON(flags & ~(GFP_DMA | GFP_LEVEL_MASK));
983
984 if (flags & __GFP_WAIT)
985 local_irq_enable();
986
987 page = allocate_slab(s, flags & GFP_LEVEL_MASK, node);
988 if (!page)
989 goto out;
990
991 n = get_node(s, page_to_nid(page));
992 if (n)
993 atomic_long_inc(&n->nr_slabs);
994 page->offset = s->offset / sizeof(void *);
995 page->slab = s;
996 page->flags |= 1 << PG_slab;
997 if (s->flags & (SLAB_DEBUG_FREE | SLAB_RED_ZONE | SLAB_POISON |
998 SLAB_STORE_USER | SLAB_TRACE))
Christoph Lameter35e5d7e2007-05-09 02:32:42 -0700999 SetSlabDebug(page);
Christoph Lameter81819f02007-05-06 14:49:36 -07001000
1001 start = page_address(page);
1002 end = start + s->objects * s->size;
1003
1004 if (unlikely(s->flags & SLAB_POISON))
1005 memset(start, POISON_INUSE, PAGE_SIZE << s->order);
1006
1007 last = start;
Christoph Lameter7656c722007-05-09 02:32:40 -07001008 for_each_object(p, s, start) {
Christoph Lameter81819f02007-05-06 14:49:36 -07001009 setup_object(s, page, last);
1010 set_freepointer(s, last, p);
1011 last = p;
1012 }
1013 setup_object(s, page, last);
1014 set_freepointer(s, last, NULL);
1015
1016 page->freelist = start;
1017 page->inuse = 0;
1018out:
1019 if (flags & __GFP_WAIT)
1020 local_irq_disable();
1021 return page;
1022}
1023
1024static void __free_slab(struct kmem_cache *s, struct page *page)
1025{
1026 int pages = 1 << s->order;
1027
Christoph Lameter35e5d7e2007-05-09 02:32:42 -07001028 if (unlikely(SlabDebug(page) || s->dtor)) {
Christoph Lameter81819f02007-05-06 14:49:36 -07001029 void *p;
1030
1031 slab_pad_check(s, page);
Christoph Lameter7656c722007-05-09 02:32:40 -07001032 for_each_object(p, s, page_address(page)) {
Christoph Lameter81819f02007-05-06 14:49:36 -07001033 if (s->dtor)
1034 s->dtor(p, s, 0);
1035 check_object(s, page, p, 0);
1036 }
1037 }
1038
1039 mod_zone_page_state(page_zone(page),
1040 (s->flags & SLAB_RECLAIM_ACCOUNT) ?
1041 NR_SLAB_RECLAIMABLE : NR_SLAB_UNRECLAIMABLE,
1042 - pages);
1043
1044 page->mapping = NULL;
1045 __free_pages(page, s->order);
1046}
1047
1048static void rcu_free_slab(struct rcu_head *h)
1049{
1050 struct page *page;
1051
1052 page = container_of((struct list_head *)h, struct page, lru);
1053 __free_slab(page->slab, page);
1054}
1055
1056static void free_slab(struct kmem_cache *s, struct page *page)
1057{
1058 if (unlikely(s->flags & SLAB_DESTROY_BY_RCU)) {
1059 /*
1060 * RCU free overloads the RCU head over the LRU
1061 */
1062 struct rcu_head *head = (void *)&page->lru;
1063
1064 call_rcu(head, rcu_free_slab);
1065 } else
1066 __free_slab(s, page);
1067}
1068
1069static void discard_slab(struct kmem_cache *s, struct page *page)
1070{
1071 struct kmem_cache_node *n = get_node(s, page_to_nid(page));
1072
1073 atomic_long_dec(&n->nr_slabs);
1074 reset_page_mapcount(page);
Christoph Lameter35e5d7e2007-05-09 02:32:42 -07001075 ClearSlabDebug(page);
1076 __ClearPageSlab(page);
Christoph Lameter81819f02007-05-06 14:49:36 -07001077 free_slab(s, page);
1078}
1079
1080/*
1081 * Per slab locking using the pagelock
1082 */
1083static __always_inline void slab_lock(struct page *page)
1084{
1085 bit_spin_lock(PG_locked, &page->flags);
1086}
1087
1088static __always_inline void slab_unlock(struct page *page)
1089{
1090 bit_spin_unlock(PG_locked, &page->flags);
1091}
1092
1093static __always_inline int slab_trylock(struct page *page)
1094{
1095 int rc = 1;
1096
1097 rc = bit_spin_trylock(PG_locked, &page->flags);
1098 return rc;
1099}
1100
1101/*
1102 * Management of partially allocated slabs
1103 */
Christoph Lametere95eed52007-05-06 14:49:44 -07001104static void add_partial_tail(struct kmem_cache_node *n, struct page *page)
Christoph Lameter81819f02007-05-06 14:49:36 -07001105{
Christoph Lametere95eed52007-05-06 14:49:44 -07001106 spin_lock(&n->list_lock);
1107 n->nr_partial++;
1108 list_add_tail(&page->lru, &n->partial);
1109 spin_unlock(&n->list_lock);
1110}
Christoph Lameter81819f02007-05-06 14:49:36 -07001111
Christoph Lametere95eed52007-05-06 14:49:44 -07001112static void add_partial(struct kmem_cache_node *n, struct page *page)
1113{
Christoph Lameter81819f02007-05-06 14:49:36 -07001114 spin_lock(&n->list_lock);
1115 n->nr_partial++;
1116 list_add(&page->lru, &n->partial);
1117 spin_unlock(&n->list_lock);
1118}
1119
1120static void remove_partial(struct kmem_cache *s,
1121 struct page *page)
1122{
1123 struct kmem_cache_node *n = get_node(s, page_to_nid(page));
1124
1125 spin_lock(&n->list_lock);
1126 list_del(&page->lru);
1127 n->nr_partial--;
1128 spin_unlock(&n->list_lock);
1129}
1130
1131/*
Christoph Lameter672bba32007-05-09 02:32:39 -07001132 * Lock slab and remove from the partial list.
Christoph Lameter81819f02007-05-06 14:49:36 -07001133 *
Christoph Lameter672bba32007-05-09 02:32:39 -07001134 * Must hold list_lock.
Christoph Lameter81819f02007-05-06 14:49:36 -07001135 */
1136static int lock_and_del_slab(struct kmem_cache_node *n, struct page *page)
1137{
1138 if (slab_trylock(page)) {
1139 list_del(&page->lru);
1140 n->nr_partial--;
1141 return 1;
1142 }
1143 return 0;
1144}
1145
1146/*
Christoph Lameter672bba32007-05-09 02:32:39 -07001147 * Try to allocate a partial slab from a specific node.
Christoph Lameter81819f02007-05-06 14:49:36 -07001148 */
1149static struct page *get_partial_node(struct kmem_cache_node *n)
1150{
1151 struct page *page;
1152
1153 /*
1154 * Racy check. If we mistakenly see no partial slabs then we
1155 * just allocate an empty slab. If we mistakenly try to get a
Christoph Lameter672bba32007-05-09 02:32:39 -07001156 * partial slab and there is none available then get_partials()
1157 * will return NULL.
Christoph Lameter81819f02007-05-06 14:49:36 -07001158 */
1159 if (!n || !n->nr_partial)
1160 return NULL;
1161
1162 spin_lock(&n->list_lock);
1163 list_for_each_entry(page, &n->partial, lru)
1164 if (lock_and_del_slab(n, page))
1165 goto out;
1166 page = NULL;
1167out:
1168 spin_unlock(&n->list_lock);
1169 return page;
1170}
1171
1172/*
Christoph Lameter672bba32007-05-09 02:32:39 -07001173 * Get a page from somewhere. Search in increasing NUMA distances.
Christoph Lameter81819f02007-05-06 14:49:36 -07001174 */
1175static struct page *get_any_partial(struct kmem_cache *s, gfp_t flags)
1176{
1177#ifdef CONFIG_NUMA
1178 struct zonelist *zonelist;
1179 struct zone **z;
1180 struct page *page;
1181
1182 /*
Christoph Lameter672bba32007-05-09 02:32:39 -07001183 * The defrag ratio allows a configuration of the tradeoffs between
1184 * inter node defragmentation and node local allocations. A lower
1185 * defrag_ratio increases the tendency to do local allocations
1186 * instead of attempting to obtain partial slabs from other nodes.
Christoph Lameter81819f02007-05-06 14:49:36 -07001187 *
Christoph Lameter672bba32007-05-09 02:32:39 -07001188 * If the defrag_ratio is set to 0 then kmalloc() always
1189 * returns node local objects. If the ratio is higher then kmalloc()
1190 * may return off node objects because partial slabs are obtained
1191 * from other nodes and filled up.
Christoph Lameter81819f02007-05-06 14:49:36 -07001192 *
1193 * If /sys/slab/xx/defrag_ratio is set to 100 (which makes
Christoph Lameter672bba32007-05-09 02:32:39 -07001194 * defrag_ratio = 1000) then every (well almost) allocation will
1195 * first attempt to defrag slab caches on other nodes. This means
1196 * scanning over all nodes to look for partial slabs which may be
1197 * expensive if we do it every time we are trying to find a slab
1198 * with available objects.
Christoph Lameter81819f02007-05-06 14:49:36 -07001199 */
1200 if (!s->defrag_ratio || get_cycles() % 1024 > s->defrag_ratio)
1201 return NULL;
1202
1203 zonelist = &NODE_DATA(slab_node(current->mempolicy))
1204 ->node_zonelists[gfp_zone(flags)];
1205 for (z = zonelist->zones; *z; z++) {
1206 struct kmem_cache_node *n;
1207
1208 n = get_node(s, zone_to_nid(*z));
1209
1210 if (n && cpuset_zone_allowed_hardwall(*z, flags) &&
Christoph Lametere95eed52007-05-06 14:49:44 -07001211 n->nr_partial > MIN_PARTIAL) {
Christoph Lameter81819f02007-05-06 14:49:36 -07001212 page = get_partial_node(n);
1213 if (page)
1214 return page;
1215 }
1216 }
1217#endif
1218 return NULL;
1219}
1220
1221/*
1222 * Get a partial page, lock it and return it.
1223 */
1224static struct page *get_partial(struct kmem_cache *s, gfp_t flags, int node)
1225{
1226 struct page *page;
1227 int searchnode = (node == -1) ? numa_node_id() : node;
1228
1229 page = get_partial_node(get_node(s, searchnode));
1230 if (page || (flags & __GFP_THISNODE))
1231 return page;
1232
1233 return get_any_partial(s, flags);
1234}
1235
1236/*
1237 * Move a page back to the lists.
1238 *
1239 * Must be called with the slab lock held.
1240 *
1241 * On exit the slab lock will have been dropped.
1242 */
1243static void putback_slab(struct kmem_cache *s, struct page *page)
1244{
Christoph Lametere95eed52007-05-06 14:49:44 -07001245 struct kmem_cache_node *n = get_node(s, page_to_nid(page));
1246
Christoph Lameter81819f02007-05-06 14:49:36 -07001247 if (page->inuse) {
Christoph Lametere95eed52007-05-06 14:49:44 -07001248
Christoph Lameter81819f02007-05-06 14:49:36 -07001249 if (page->freelist)
Christoph Lametere95eed52007-05-06 14:49:44 -07001250 add_partial(n, page);
Christoph Lameter35e5d7e2007-05-09 02:32:42 -07001251 else if (SlabDebug(page) && (s->flags & SLAB_STORE_USER))
Christoph Lametere95eed52007-05-06 14:49:44 -07001252 add_full(n, page);
Christoph Lameter81819f02007-05-06 14:49:36 -07001253 slab_unlock(page);
Christoph Lametere95eed52007-05-06 14:49:44 -07001254
Christoph Lameter81819f02007-05-06 14:49:36 -07001255 } else {
Christoph Lametere95eed52007-05-06 14:49:44 -07001256 if (n->nr_partial < MIN_PARTIAL) {
1257 /*
Christoph Lameter672bba32007-05-09 02:32:39 -07001258 * Adding an empty slab to the partial slabs in order
1259 * to avoid page allocator overhead. This slab needs
1260 * to come after the other slabs with objects in
1261 * order to fill them up. That way the size of the
1262 * partial list stays small. kmem_cache_shrink can
1263 * reclaim empty slabs from the partial list.
Christoph Lametere95eed52007-05-06 14:49:44 -07001264 */
1265 add_partial_tail(n, page);
1266 slab_unlock(page);
1267 } else {
1268 slab_unlock(page);
1269 discard_slab(s, page);
1270 }
Christoph Lameter81819f02007-05-06 14:49:36 -07001271 }
1272}
1273
1274/*
1275 * Remove the cpu slab
1276 */
1277static void deactivate_slab(struct kmem_cache *s, struct page *page, int cpu)
1278{
1279 s->cpu_slab[cpu] = NULL;
1280 ClearPageActive(page);
1281
1282 putback_slab(s, page);
1283}
1284
1285static void flush_slab(struct kmem_cache *s, struct page *page, int cpu)
1286{
1287 slab_lock(page);
1288 deactivate_slab(s, page, cpu);
1289}
1290
1291/*
1292 * Flush cpu slab.
1293 * Called from IPI handler with interrupts disabled.
1294 */
1295static void __flush_cpu_slab(struct kmem_cache *s, int cpu)
1296{
1297 struct page *page = s->cpu_slab[cpu];
1298
1299 if (likely(page))
1300 flush_slab(s, page, cpu);
1301}
1302
1303static void flush_cpu_slab(void *d)
1304{
1305 struct kmem_cache *s = d;
1306 int cpu = smp_processor_id();
1307
1308 __flush_cpu_slab(s, cpu);
1309}
1310
1311static void flush_all(struct kmem_cache *s)
1312{
1313#ifdef CONFIG_SMP
1314 on_each_cpu(flush_cpu_slab, s, 1, 1);
1315#else
1316 unsigned long flags;
1317
1318 local_irq_save(flags);
1319 flush_cpu_slab(s);
1320 local_irq_restore(flags);
1321#endif
1322}
1323
1324/*
1325 * slab_alloc is optimized to only modify two cachelines on the fast path
1326 * (aside from the stack):
1327 *
1328 * 1. The page struct
1329 * 2. The first cacheline of the object to be allocated.
1330 *
Christoph Lameter672bba32007-05-09 02:32:39 -07001331 * The only other cache lines that are read (apart from code) is the
Christoph Lameter81819f02007-05-06 14:49:36 -07001332 * per cpu array in the kmem_cache struct.
1333 *
1334 * Fastpath is not possible if we need to get a new slab or have
Christoph Lameter35e5d7e2007-05-09 02:32:42 -07001335 * debugging enabled (which means all slabs are marked with SlabDebug)
Christoph Lameter81819f02007-05-06 14:49:36 -07001336 */
Christoph Lameter77c5e2d2007-05-06 14:49:42 -07001337static void *slab_alloc(struct kmem_cache *s,
1338 gfp_t gfpflags, int node, void *addr)
Christoph Lameter81819f02007-05-06 14:49:36 -07001339{
1340 struct page *page;
1341 void **object;
1342 unsigned long flags;
1343 int cpu;
1344
1345 local_irq_save(flags);
1346 cpu = smp_processor_id();
1347 page = s->cpu_slab[cpu];
1348 if (!page)
1349 goto new_slab;
1350
1351 slab_lock(page);
1352 if (unlikely(node != -1 && page_to_nid(page) != node))
1353 goto another_slab;
1354redo:
1355 object = page->freelist;
1356 if (unlikely(!object))
1357 goto another_slab;
Christoph Lameter35e5d7e2007-05-09 02:32:42 -07001358 if (unlikely(SlabDebug(page)))
Christoph Lameter81819f02007-05-06 14:49:36 -07001359 goto debug;
1360
1361have_object:
1362 page->inuse++;
1363 page->freelist = object[page->offset];
1364 slab_unlock(page);
1365 local_irq_restore(flags);
1366 return object;
1367
1368another_slab:
1369 deactivate_slab(s, page, cpu);
1370
1371new_slab:
1372 page = get_partial(s, gfpflags, node);
1373 if (likely(page)) {
1374have_slab:
1375 s->cpu_slab[cpu] = page;
1376 SetPageActive(page);
1377 goto redo;
1378 }
1379
1380 page = new_slab(s, gfpflags, node);
1381 if (page) {
1382 cpu = smp_processor_id();
1383 if (s->cpu_slab[cpu]) {
1384 /*
Christoph Lameter672bba32007-05-09 02:32:39 -07001385 * Someone else populated the cpu_slab while we
1386 * enabled interrupts, or we have gotten scheduled
1387 * on another cpu. The page may not be on the
1388 * requested node even if __GFP_THISNODE was
1389 * specified. So we need to recheck.
Christoph Lameter81819f02007-05-06 14:49:36 -07001390 */
1391 if (node == -1 ||
1392 page_to_nid(s->cpu_slab[cpu]) == node) {
1393 /*
1394 * Current cpuslab is acceptable and we
1395 * want the current one since its cache hot
1396 */
1397 discard_slab(s, page);
1398 page = s->cpu_slab[cpu];
1399 slab_lock(page);
1400 goto redo;
1401 }
Christoph Lameter672bba32007-05-09 02:32:39 -07001402 /* New slab does not fit our expectations */
Christoph Lameter81819f02007-05-06 14:49:36 -07001403 flush_slab(s, s->cpu_slab[cpu], cpu);
1404 }
1405 slab_lock(page);
1406 goto have_slab;
1407 }
1408 local_irq_restore(flags);
1409 return NULL;
1410debug:
1411 if (!alloc_object_checks(s, page, object))
1412 goto another_slab;
1413 if (s->flags & SLAB_STORE_USER)
Christoph Lameter77c5e2d2007-05-06 14:49:42 -07001414 set_track(s, object, TRACK_ALLOC, addr);
Christoph Lameter636f0d72007-05-09 02:32:42 -07001415 trace(s, page, object, 1);
Christoph Lameter70d71222007-05-06 14:49:47 -07001416 init_object(s, object, 1);
Christoph Lameter81819f02007-05-06 14:49:36 -07001417 goto have_object;
1418}
1419
1420void *kmem_cache_alloc(struct kmem_cache *s, gfp_t gfpflags)
1421{
Christoph Lameter77c5e2d2007-05-06 14:49:42 -07001422 return slab_alloc(s, gfpflags, -1, __builtin_return_address(0));
Christoph Lameter81819f02007-05-06 14:49:36 -07001423}
1424EXPORT_SYMBOL(kmem_cache_alloc);
1425
1426#ifdef CONFIG_NUMA
1427void *kmem_cache_alloc_node(struct kmem_cache *s, gfp_t gfpflags, int node)
1428{
Christoph Lameter77c5e2d2007-05-06 14:49:42 -07001429 return slab_alloc(s, gfpflags, node, __builtin_return_address(0));
Christoph Lameter81819f02007-05-06 14:49:36 -07001430}
1431EXPORT_SYMBOL(kmem_cache_alloc_node);
1432#endif
1433
1434/*
1435 * The fastpath only writes the cacheline of the page struct and the first
1436 * cacheline of the object.
1437 *
Christoph Lameter672bba32007-05-09 02:32:39 -07001438 * We read the cpu_slab cacheline to check if the slab is the per cpu
1439 * slab for this processor.
Christoph Lameter81819f02007-05-06 14:49:36 -07001440 */
Christoph Lameter77c5e2d2007-05-06 14:49:42 -07001441static void slab_free(struct kmem_cache *s, struct page *page,
1442 void *x, void *addr)
Christoph Lameter81819f02007-05-06 14:49:36 -07001443{
1444 void *prior;
1445 void **object = (void *)x;
1446 unsigned long flags;
1447
1448 local_irq_save(flags);
1449 slab_lock(page);
1450
Christoph Lameter35e5d7e2007-05-09 02:32:42 -07001451 if (unlikely(SlabDebug(page)))
Christoph Lameter81819f02007-05-06 14:49:36 -07001452 goto debug;
1453checks_ok:
1454 prior = object[page->offset] = page->freelist;
1455 page->freelist = object;
1456 page->inuse--;
1457
1458 if (unlikely(PageActive(page)))
1459 /*
1460 * Cpu slabs are never on partial lists and are
1461 * never freed.
1462 */
1463 goto out_unlock;
1464
1465 if (unlikely(!page->inuse))
1466 goto slab_empty;
1467
1468 /*
1469 * Objects left in the slab. If it
1470 * was not on the partial list before
1471 * then add it.
1472 */
1473 if (unlikely(!prior))
Christoph Lametere95eed52007-05-06 14:49:44 -07001474 add_partial(get_node(s, page_to_nid(page)), page);
Christoph Lameter81819f02007-05-06 14:49:36 -07001475
1476out_unlock:
1477 slab_unlock(page);
1478 local_irq_restore(flags);
1479 return;
1480
1481slab_empty:
1482 if (prior)
1483 /*
Christoph Lameter672bba32007-05-09 02:32:39 -07001484 * Slab still on the partial list.
Christoph Lameter81819f02007-05-06 14:49:36 -07001485 */
1486 remove_partial(s, page);
1487
1488 slab_unlock(page);
1489 discard_slab(s, page);
1490 local_irq_restore(flags);
1491 return;
1492
1493debug:
Christoph Lameter77c5e2d2007-05-06 14:49:42 -07001494 if (!free_object_checks(s, page, x))
1495 goto out_unlock;
Christoph Lameter643b1132007-05-06 14:49:42 -07001496 if (!PageActive(page) && !page->freelist)
1497 remove_full(s, page);
Christoph Lameter77c5e2d2007-05-06 14:49:42 -07001498 if (s->flags & SLAB_STORE_USER)
1499 set_track(s, x, TRACK_FREE, addr);
Christoph Lameter636f0d72007-05-09 02:32:42 -07001500 trace(s, page, object, 0);
Christoph Lameter70d71222007-05-06 14:49:47 -07001501 init_object(s, object, 0);
Christoph Lameter77c5e2d2007-05-06 14:49:42 -07001502 goto checks_ok;
Christoph Lameter81819f02007-05-06 14:49:36 -07001503}
1504
1505void kmem_cache_free(struct kmem_cache *s, void *x)
1506{
Christoph Lameter77c5e2d2007-05-06 14:49:42 -07001507 struct page *page;
Christoph Lameter81819f02007-05-06 14:49:36 -07001508
Christoph Lameterb49af682007-05-06 14:49:41 -07001509 page = virt_to_head_page(x);
Christoph Lameter81819f02007-05-06 14:49:36 -07001510
Christoph Lameter77c5e2d2007-05-06 14:49:42 -07001511 slab_free(s, page, x, __builtin_return_address(0));
Christoph Lameter81819f02007-05-06 14:49:36 -07001512}
1513EXPORT_SYMBOL(kmem_cache_free);
1514
1515/* Figure out on which slab object the object resides */
1516static struct page *get_object_page(const void *x)
1517{
Christoph Lameterb49af682007-05-06 14:49:41 -07001518 struct page *page = virt_to_head_page(x);
Christoph Lameter81819f02007-05-06 14:49:36 -07001519
1520 if (!PageSlab(page))
1521 return NULL;
1522
1523 return page;
1524}
1525
1526/*
Christoph Lameter672bba32007-05-09 02:32:39 -07001527 * Object placement in a slab is made very easy because we always start at
1528 * offset 0. If we tune the size of the object to the alignment then we can
1529 * get the required alignment by putting one properly sized object after
1530 * another.
Christoph Lameter81819f02007-05-06 14:49:36 -07001531 *
1532 * Notice that the allocation order determines the sizes of the per cpu
1533 * caches. Each processor has always one slab available for allocations.
1534 * Increasing the allocation order reduces the number of times that slabs
Christoph Lameter672bba32007-05-09 02:32:39 -07001535 * must be moved on and off the partial lists and is therefore a factor in
Christoph Lameter81819f02007-05-06 14:49:36 -07001536 * locking overhead.
Christoph Lameter81819f02007-05-06 14:49:36 -07001537 */
1538
1539/*
1540 * Mininum / Maximum order of slab pages. This influences locking overhead
1541 * and slab fragmentation. A higher order reduces the number of partial slabs
1542 * and increases the number of allocations possible without having to
1543 * take the list_lock.
1544 */
1545static int slub_min_order;
1546static int slub_max_order = DEFAULT_MAX_ORDER;
Christoph Lameter81819f02007-05-06 14:49:36 -07001547static int slub_min_objects = DEFAULT_MIN_OBJECTS;
1548
1549/*
1550 * Merge control. If this is set then no merging of slab caches will occur.
Christoph Lameter672bba32007-05-09 02:32:39 -07001551 * (Could be removed. This was introduced to pacify the merge skeptics.)
Christoph Lameter81819f02007-05-06 14:49:36 -07001552 */
1553static int slub_nomerge;
1554
1555/*
Christoph Lameter81819f02007-05-06 14:49:36 -07001556 * Calculate the order of allocation given an slab object size.
1557 *
Christoph Lameter672bba32007-05-09 02:32:39 -07001558 * The order of allocation has significant impact on performance and other
1559 * system components. Generally order 0 allocations should be preferred since
1560 * order 0 does not cause fragmentation in the page allocator. Larger objects
1561 * be problematic to put into order 0 slabs because there may be too much
1562 * unused space left. We go to a higher order if more than 1/8th of the slab
1563 * would be wasted.
Christoph Lameter81819f02007-05-06 14:49:36 -07001564 *
Christoph Lameter672bba32007-05-09 02:32:39 -07001565 * In order to reach satisfactory performance we must ensure that a minimum
1566 * number of objects is in one slab. Otherwise we may generate too much
1567 * activity on the partial lists which requires taking the list_lock. This is
1568 * less a concern for large slabs though which are rarely used.
Christoph Lameter81819f02007-05-06 14:49:36 -07001569 *
Christoph Lameter672bba32007-05-09 02:32:39 -07001570 * slub_max_order specifies the order where we begin to stop considering the
1571 * number of objects in a slab as critical. If we reach slub_max_order then
1572 * we try to keep the page order as low as possible. So we accept more waste
1573 * of space in favor of a small page order.
1574 *
1575 * Higher order allocations also allow the placement of more objects in a
1576 * slab and thereby reduce object handling overhead. If the user has
1577 * requested a higher mininum order then we start with that one instead of
1578 * the smallest order which will fit the object.
Christoph Lameter81819f02007-05-06 14:49:36 -07001579 */
Christoph Lameter5e6d4442007-05-09 02:32:46 -07001580static inline int slab_order(int size, int min_objects,
1581 int max_order, int fract_leftover)
Christoph Lameter81819f02007-05-06 14:49:36 -07001582{
1583 int order;
1584 int rem;
1585
Christoph Lameter5e6d4442007-05-09 02:32:46 -07001586 for (order = max(slub_min_order,
1587 fls(min_objects * size - 1) - PAGE_SHIFT);
1588 order <= max_order; order++) {
1589
Christoph Lameter81819f02007-05-06 14:49:36 -07001590 unsigned long slab_size = PAGE_SIZE << order;
1591
Christoph Lameter5e6d4442007-05-09 02:32:46 -07001592 if (slab_size < min_objects * size)
Christoph Lameter81819f02007-05-06 14:49:36 -07001593 continue;
1594
Christoph Lameter81819f02007-05-06 14:49:36 -07001595 rem = slab_size % size;
1596
Christoph Lameter5e6d4442007-05-09 02:32:46 -07001597 if (rem <= slab_size / fract_leftover)
Christoph Lameter81819f02007-05-06 14:49:36 -07001598 break;
1599
1600 }
Christoph Lameter672bba32007-05-09 02:32:39 -07001601
Christoph Lameter81819f02007-05-06 14:49:36 -07001602 return order;
1603}
1604
Christoph Lameter5e6d4442007-05-09 02:32:46 -07001605static inline int calculate_order(int size)
1606{
1607 int order;
1608 int min_objects;
1609 int fraction;
1610
1611 /*
1612 * Attempt to find best configuration for a slab. This
1613 * works by first attempting to generate a layout with
1614 * the best configuration and backing off gradually.
1615 *
1616 * First we reduce the acceptable waste in a slab. Then
1617 * we reduce the minimum objects required in a slab.
1618 */
1619 min_objects = slub_min_objects;
1620 while (min_objects > 1) {
1621 fraction = 8;
1622 while (fraction >= 4) {
1623 order = slab_order(size, min_objects,
1624 slub_max_order, fraction);
1625 if (order <= slub_max_order)
1626 return order;
1627 fraction /= 2;
1628 }
1629 min_objects /= 2;
1630 }
1631
1632 /*
1633 * We were unable to place multiple objects in a slab. Now
1634 * lets see if we can place a single object there.
1635 */
1636 order = slab_order(size, 1, slub_max_order, 1);
1637 if (order <= slub_max_order)
1638 return order;
1639
1640 /*
1641 * Doh this slab cannot be placed using slub_max_order.
1642 */
1643 order = slab_order(size, 1, MAX_ORDER, 1);
1644 if (order <= MAX_ORDER)
1645 return order;
1646 return -ENOSYS;
1647}
1648
Christoph Lameter81819f02007-05-06 14:49:36 -07001649/*
Christoph Lameter672bba32007-05-09 02:32:39 -07001650 * Figure out what the alignment of the objects will be.
Christoph Lameter81819f02007-05-06 14:49:36 -07001651 */
1652static unsigned long calculate_alignment(unsigned long flags,
1653 unsigned long align, unsigned long size)
1654{
1655 /*
1656 * If the user wants hardware cache aligned objects then
1657 * follow that suggestion if the object is sufficiently
1658 * large.
1659 *
1660 * The hardware cache alignment cannot override the
1661 * specified alignment though. If that is greater
1662 * then use it.
1663 */
Christoph Lameter5af60832007-05-06 14:49:56 -07001664 if ((flags & SLAB_HWCACHE_ALIGN) &&
Christoph Lameter65c02d42007-05-09 02:32:35 -07001665 size > cache_line_size() / 2)
1666 return max_t(unsigned long, align, cache_line_size());
Christoph Lameter81819f02007-05-06 14:49:36 -07001667
1668 if (align < ARCH_SLAB_MINALIGN)
1669 return ARCH_SLAB_MINALIGN;
1670
1671 return ALIGN(align, sizeof(void *));
1672}
1673
1674static void init_kmem_cache_node(struct kmem_cache_node *n)
1675{
1676 n->nr_partial = 0;
1677 atomic_long_set(&n->nr_slabs, 0);
1678 spin_lock_init(&n->list_lock);
1679 INIT_LIST_HEAD(&n->partial);
Christoph Lameter643b1132007-05-06 14:49:42 -07001680 INIT_LIST_HEAD(&n->full);
Christoph Lameter81819f02007-05-06 14:49:36 -07001681}
1682
1683#ifdef CONFIG_NUMA
1684/*
1685 * No kmalloc_node yet so do it by hand. We know that this is the first
1686 * slab on the node for this slabcache. There are no concurrent accesses
1687 * possible.
1688 *
1689 * Note that this function only works on the kmalloc_node_cache
1690 * when allocating for the kmalloc_node_cache.
1691 */
1692static struct kmem_cache_node * __init early_kmem_cache_node_alloc(gfp_t gfpflags,
1693 int node)
1694{
1695 struct page *page;
1696 struct kmem_cache_node *n;
1697
1698 BUG_ON(kmalloc_caches->size < sizeof(struct kmem_cache_node));
1699
1700 page = new_slab(kmalloc_caches, gfpflags | GFP_THISNODE, node);
1701 /* new_slab() disables interupts */
1702 local_irq_enable();
1703
1704 BUG_ON(!page);
1705 n = page->freelist;
1706 BUG_ON(!n);
1707 page->freelist = get_freepointer(kmalloc_caches, n);
1708 page->inuse++;
1709 kmalloc_caches->node[node] = n;
1710 init_object(kmalloc_caches, n, 1);
1711 init_kmem_cache_node(n);
1712 atomic_long_inc(&n->nr_slabs);
Christoph Lametere95eed52007-05-06 14:49:44 -07001713 add_partial(n, page);
Christoph Lameter81819f02007-05-06 14:49:36 -07001714 return n;
1715}
1716
1717static void free_kmem_cache_nodes(struct kmem_cache *s)
1718{
1719 int node;
1720
1721 for_each_online_node(node) {
1722 struct kmem_cache_node *n = s->node[node];
1723 if (n && n != &s->local_node)
1724 kmem_cache_free(kmalloc_caches, n);
1725 s->node[node] = NULL;
1726 }
1727}
1728
1729static int init_kmem_cache_nodes(struct kmem_cache *s, gfp_t gfpflags)
1730{
1731 int node;
1732 int local_node;
1733
1734 if (slab_state >= UP)
1735 local_node = page_to_nid(virt_to_page(s));
1736 else
1737 local_node = 0;
1738
1739 for_each_online_node(node) {
1740 struct kmem_cache_node *n;
1741
1742 if (local_node == node)
1743 n = &s->local_node;
1744 else {
1745 if (slab_state == DOWN) {
1746 n = early_kmem_cache_node_alloc(gfpflags,
1747 node);
1748 continue;
1749 }
1750 n = kmem_cache_alloc_node(kmalloc_caches,
1751 gfpflags, node);
1752
1753 if (!n) {
1754 free_kmem_cache_nodes(s);
1755 return 0;
1756 }
1757
1758 }
1759 s->node[node] = n;
1760 init_kmem_cache_node(n);
1761 }
1762 return 1;
1763}
1764#else
1765static void free_kmem_cache_nodes(struct kmem_cache *s)
1766{
1767}
1768
1769static int init_kmem_cache_nodes(struct kmem_cache *s, gfp_t gfpflags)
1770{
1771 init_kmem_cache_node(&s->local_node);
1772 return 1;
1773}
1774#endif
1775
1776/*
1777 * calculate_sizes() determines the order and the distribution of data within
1778 * a slab object.
1779 */
1780static int calculate_sizes(struct kmem_cache *s)
1781{
1782 unsigned long flags = s->flags;
1783 unsigned long size = s->objsize;
1784 unsigned long align = s->align;
1785
1786 /*
1787 * Determine if we can poison the object itself. If the user of
1788 * the slab may touch the object after free or before allocation
1789 * then we should never poison the object itself.
1790 */
1791 if ((flags & SLAB_POISON) && !(flags & SLAB_DESTROY_BY_RCU) &&
1792 !s->ctor && !s->dtor)
1793 s->flags |= __OBJECT_POISON;
1794 else
1795 s->flags &= ~__OBJECT_POISON;
1796
1797 /*
1798 * Round up object size to the next word boundary. We can only
1799 * place the free pointer at word boundaries and this determines
1800 * the possible location of the free pointer.
1801 */
1802 size = ALIGN(size, sizeof(void *));
1803
Christoph Lameter41ecc552007-05-09 02:32:44 -07001804#ifdef CONFIG_SLUB_DEBUG
Christoph Lameter81819f02007-05-06 14:49:36 -07001805 /*
Christoph Lameter672bba32007-05-09 02:32:39 -07001806 * If we are Redzoning then check if there is some space between the
Christoph Lameter81819f02007-05-06 14:49:36 -07001807 * end of the object and the free pointer. If not then add an
Christoph Lameter672bba32007-05-09 02:32:39 -07001808 * additional word to have some bytes to store Redzone information.
Christoph Lameter81819f02007-05-06 14:49:36 -07001809 */
1810 if ((flags & SLAB_RED_ZONE) && size == s->objsize)
1811 size += sizeof(void *);
Christoph Lameter41ecc552007-05-09 02:32:44 -07001812#endif
Christoph Lameter81819f02007-05-06 14:49:36 -07001813
1814 /*
Christoph Lameter672bba32007-05-09 02:32:39 -07001815 * With that we have determined the number of bytes in actual use
1816 * by the object. This is the potential offset to the free pointer.
Christoph Lameter81819f02007-05-06 14:49:36 -07001817 */
1818 s->inuse = size;
1819
Christoph Lameter41ecc552007-05-09 02:32:44 -07001820#ifdef CONFIG_SLUB_DEBUG
Christoph Lameter81819f02007-05-06 14:49:36 -07001821 if (((flags & (SLAB_DESTROY_BY_RCU | SLAB_POISON)) ||
1822 s->ctor || s->dtor)) {
1823 /*
1824 * Relocate free pointer after the object if it is not
1825 * permitted to overwrite the first word of the object on
1826 * kmem_cache_free.
1827 *
1828 * This is the case if we do RCU, have a constructor or
1829 * destructor or are poisoning the objects.
1830 */
1831 s->offset = size;
1832 size += sizeof(void *);
1833 }
1834
1835 if (flags & SLAB_STORE_USER)
1836 /*
1837 * Need to store information about allocs and frees after
1838 * the object.
1839 */
1840 size += 2 * sizeof(struct track);
1841
Christoph Lameterbe7b3fb2007-05-09 02:32:36 -07001842 if (flags & SLAB_RED_ZONE)
Christoph Lameter81819f02007-05-06 14:49:36 -07001843 /*
1844 * Add some empty padding so that we can catch
1845 * overwrites from earlier objects rather than let
1846 * tracking information or the free pointer be
1847 * corrupted if an user writes before the start
1848 * of the object.
1849 */
1850 size += sizeof(void *);
Christoph Lameter41ecc552007-05-09 02:32:44 -07001851#endif
Christoph Lameter672bba32007-05-09 02:32:39 -07001852
Christoph Lameter81819f02007-05-06 14:49:36 -07001853 /*
1854 * Determine the alignment based on various parameters that the
Christoph Lameter65c02d42007-05-09 02:32:35 -07001855 * user specified and the dynamic determination of cache line size
1856 * on bootup.
Christoph Lameter81819f02007-05-06 14:49:36 -07001857 */
1858 align = calculate_alignment(flags, align, s->objsize);
1859
1860 /*
1861 * SLUB stores one object immediately after another beginning from
1862 * offset 0. In order to align the objects we have to simply size
1863 * each object to conform to the alignment.
1864 */
1865 size = ALIGN(size, align);
1866 s->size = size;
1867
1868 s->order = calculate_order(size);
1869 if (s->order < 0)
1870 return 0;
1871
1872 /*
1873 * Determine the number of objects per slab
1874 */
1875 s->objects = (PAGE_SIZE << s->order) / size;
1876
1877 /*
1878 * Verify that the number of objects is within permitted limits.
1879 * The page->inuse field is only 16 bit wide! So we cannot have
1880 * more than 64k objects per slab.
1881 */
1882 if (!s->objects || s->objects > 65535)
1883 return 0;
1884 return 1;
1885
1886}
1887
Christoph Lameter81819f02007-05-06 14:49:36 -07001888static int kmem_cache_open(struct kmem_cache *s, gfp_t gfpflags,
1889 const char *name, size_t size,
1890 size_t align, unsigned long flags,
1891 void (*ctor)(void *, struct kmem_cache *, unsigned long),
1892 void (*dtor)(void *, struct kmem_cache *, unsigned long))
1893{
1894 memset(s, 0, kmem_size);
1895 s->name = name;
1896 s->ctor = ctor;
1897 s->dtor = dtor;
1898 s->objsize = size;
1899 s->flags = flags;
1900 s->align = align;
Christoph Lameter41ecc552007-05-09 02:32:44 -07001901 kmem_cache_open_debug_check(s);
Christoph Lameter81819f02007-05-06 14:49:36 -07001902
1903 if (!calculate_sizes(s))
1904 goto error;
1905
1906 s->refcount = 1;
1907#ifdef CONFIG_NUMA
1908 s->defrag_ratio = 100;
1909#endif
1910
1911 if (init_kmem_cache_nodes(s, gfpflags & ~SLUB_DMA))
1912 return 1;
1913error:
1914 if (flags & SLAB_PANIC)
1915 panic("Cannot create slab %s size=%lu realsize=%u "
1916 "order=%u offset=%u flags=%lx\n",
1917 s->name, (unsigned long)size, s->size, s->order,
1918 s->offset, flags);
1919 return 0;
1920}
1921EXPORT_SYMBOL(kmem_cache_open);
1922
1923/*
1924 * Check if a given pointer is valid
1925 */
1926int kmem_ptr_validate(struct kmem_cache *s, const void *object)
1927{
1928 struct page * page;
Christoph Lameter81819f02007-05-06 14:49:36 -07001929
1930 page = get_object_page(object);
1931
1932 if (!page || s != page->slab)
1933 /* No slab or wrong slab */
1934 return 0;
1935
Christoph Lameterabcd08a2007-05-09 02:32:37 -07001936 if (!check_valid_pointer(s, page, object))
Christoph Lameter81819f02007-05-06 14:49:36 -07001937 return 0;
1938
1939 /*
1940 * We could also check if the object is on the slabs freelist.
1941 * But this would be too expensive and it seems that the main
1942 * purpose of kmem_ptr_valid is to check if the object belongs
1943 * to a certain slab.
1944 */
1945 return 1;
1946}
1947EXPORT_SYMBOL(kmem_ptr_validate);
1948
1949/*
1950 * Determine the size of a slab object
1951 */
1952unsigned int kmem_cache_size(struct kmem_cache *s)
1953{
1954 return s->objsize;
1955}
1956EXPORT_SYMBOL(kmem_cache_size);
1957
1958const char *kmem_cache_name(struct kmem_cache *s)
1959{
1960 return s->name;
1961}
1962EXPORT_SYMBOL(kmem_cache_name);
1963
1964/*
Christoph Lameter672bba32007-05-09 02:32:39 -07001965 * Attempt to free all slabs on a node. Return the number of slabs we
1966 * were unable to free.
Christoph Lameter81819f02007-05-06 14:49:36 -07001967 */
1968static int free_list(struct kmem_cache *s, struct kmem_cache_node *n,
1969 struct list_head *list)
1970{
1971 int slabs_inuse = 0;
1972 unsigned long flags;
1973 struct page *page, *h;
1974
1975 spin_lock_irqsave(&n->list_lock, flags);
1976 list_for_each_entry_safe(page, h, list, lru)
1977 if (!page->inuse) {
1978 list_del(&page->lru);
1979 discard_slab(s, page);
1980 } else
1981 slabs_inuse++;
1982 spin_unlock_irqrestore(&n->list_lock, flags);
1983 return slabs_inuse;
1984}
1985
1986/*
Christoph Lameter672bba32007-05-09 02:32:39 -07001987 * Release all resources used by a slab cache.
Christoph Lameter81819f02007-05-06 14:49:36 -07001988 */
1989static int kmem_cache_close(struct kmem_cache *s)
1990{
1991 int node;
1992
1993 flush_all(s);
1994
1995 /* Attempt to free all objects */
1996 for_each_online_node(node) {
1997 struct kmem_cache_node *n = get_node(s, node);
1998
Christoph Lameter2086d262007-05-06 14:49:46 -07001999 n->nr_partial -= free_list(s, n, &n->partial);
Christoph Lameter81819f02007-05-06 14:49:36 -07002000 if (atomic_long_read(&n->nr_slabs))
2001 return 1;
2002 }
2003 free_kmem_cache_nodes(s);
2004 return 0;
2005}
2006
2007/*
2008 * Close a cache and release the kmem_cache structure
2009 * (must be used for caches created using kmem_cache_create)
2010 */
2011void kmem_cache_destroy(struct kmem_cache *s)
2012{
2013 down_write(&slub_lock);
2014 s->refcount--;
2015 if (!s->refcount) {
2016 list_del(&s->list);
2017 if (kmem_cache_close(s))
2018 WARN_ON(1);
2019 sysfs_slab_remove(s);
2020 kfree(s);
2021 }
2022 up_write(&slub_lock);
2023}
2024EXPORT_SYMBOL(kmem_cache_destroy);
2025
2026/********************************************************************
2027 * Kmalloc subsystem
2028 *******************************************************************/
2029
2030struct kmem_cache kmalloc_caches[KMALLOC_SHIFT_HIGH + 1] __cacheline_aligned;
2031EXPORT_SYMBOL(kmalloc_caches);
2032
2033#ifdef CONFIG_ZONE_DMA
2034static struct kmem_cache *kmalloc_caches_dma[KMALLOC_SHIFT_HIGH + 1];
2035#endif
2036
2037static int __init setup_slub_min_order(char *str)
2038{
2039 get_option (&str, &slub_min_order);
2040
2041 return 1;
2042}
2043
2044__setup("slub_min_order=", setup_slub_min_order);
2045
2046static int __init setup_slub_max_order(char *str)
2047{
2048 get_option (&str, &slub_max_order);
2049
2050 return 1;
2051}
2052
2053__setup("slub_max_order=", setup_slub_max_order);
2054
2055static int __init setup_slub_min_objects(char *str)
2056{
2057 get_option (&str, &slub_min_objects);
2058
2059 return 1;
2060}
2061
2062__setup("slub_min_objects=", setup_slub_min_objects);
2063
2064static int __init setup_slub_nomerge(char *str)
2065{
2066 slub_nomerge = 1;
2067 return 1;
2068}
2069
2070__setup("slub_nomerge", setup_slub_nomerge);
2071
Christoph Lameter81819f02007-05-06 14:49:36 -07002072static struct kmem_cache *create_kmalloc_cache(struct kmem_cache *s,
2073 const char *name, int size, gfp_t gfp_flags)
2074{
2075 unsigned int flags = 0;
2076
2077 if (gfp_flags & SLUB_DMA)
2078 flags = SLAB_CACHE_DMA;
2079
2080 down_write(&slub_lock);
2081 if (!kmem_cache_open(s, gfp_flags, name, size, ARCH_KMALLOC_MINALIGN,
2082 flags, NULL, NULL))
2083 goto panic;
2084
2085 list_add(&s->list, &slab_caches);
2086 up_write(&slub_lock);
2087 if (sysfs_slab_add(s))
2088 goto panic;
2089 return s;
2090
2091panic:
2092 panic("Creation of kmalloc slab %s size=%d failed.\n", name, size);
2093}
2094
2095static struct kmem_cache *get_slab(size_t size, gfp_t flags)
2096{
2097 int index = kmalloc_index(size);
2098
Christoph Lameter614410d2007-05-06 14:49:38 -07002099 if (!index)
Christoph Lameter81819f02007-05-06 14:49:36 -07002100 return NULL;
2101
2102 /* Allocation too large? */
2103 BUG_ON(index < 0);
2104
2105#ifdef CONFIG_ZONE_DMA
2106 if ((flags & SLUB_DMA)) {
2107 struct kmem_cache *s;
2108 struct kmem_cache *x;
2109 char *text;
2110 size_t realsize;
2111
2112 s = kmalloc_caches_dma[index];
2113 if (s)
2114 return s;
2115
2116 /* Dynamically create dma cache */
2117 x = kmalloc(kmem_size, flags & ~SLUB_DMA);
2118 if (!x)
2119 panic("Unable to allocate memory for dma cache\n");
2120
2121 if (index <= KMALLOC_SHIFT_HIGH)
2122 realsize = 1 << index;
2123 else {
2124 if (index == 1)
2125 realsize = 96;
2126 else
2127 realsize = 192;
2128 }
2129
2130 text = kasprintf(flags & ~SLUB_DMA, "kmalloc_dma-%d",
2131 (unsigned int)realsize);
2132 s = create_kmalloc_cache(x, text, realsize, flags);
2133 kmalloc_caches_dma[index] = s;
2134 return s;
2135 }
2136#endif
2137 return &kmalloc_caches[index];
2138}
2139
2140void *__kmalloc(size_t size, gfp_t flags)
2141{
2142 struct kmem_cache *s = get_slab(size, flags);
2143
2144 if (s)
Christoph Lameter77c5e2d2007-05-06 14:49:42 -07002145 return slab_alloc(s, flags, -1, __builtin_return_address(0));
Christoph Lameter81819f02007-05-06 14:49:36 -07002146 return NULL;
2147}
2148EXPORT_SYMBOL(__kmalloc);
2149
2150#ifdef CONFIG_NUMA
2151void *__kmalloc_node(size_t size, gfp_t flags, int node)
2152{
2153 struct kmem_cache *s = get_slab(size, flags);
2154
2155 if (s)
Christoph Lameter77c5e2d2007-05-06 14:49:42 -07002156 return slab_alloc(s, flags, node, __builtin_return_address(0));
Christoph Lameter81819f02007-05-06 14:49:36 -07002157 return NULL;
2158}
2159EXPORT_SYMBOL(__kmalloc_node);
2160#endif
2161
2162size_t ksize(const void *object)
2163{
2164 struct page *page = get_object_page(object);
2165 struct kmem_cache *s;
2166
2167 BUG_ON(!page);
2168 s = page->slab;
2169 BUG_ON(!s);
2170
2171 /*
2172 * Debugging requires use of the padding between object
2173 * and whatever may come after it.
2174 */
2175 if (s->flags & (SLAB_RED_ZONE | SLAB_POISON))
2176 return s->objsize;
2177
2178 /*
2179 * If we have the need to store the freelist pointer
2180 * back there or track user information then we can
2181 * only use the space before that information.
2182 */
2183 if (s->flags & (SLAB_DESTROY_BY_RCU | SLAB_STORE_USER))
2184 return s->inuse;
2185
2186 /*
2187 * Else we can use all the padding etc for the allocation
2188 */
2189 return s->size;
2190}
2191EXPORT_SYMBOL(ksize);
2192
2193void kfree(const void *x)
2194{
2195 struct kmem_cache *s;
2196 struct page *page;
2197
2198 if (!x)
2199 return;
2200
Christoph Lameterb49af682007-05-06 14:49:41 -07002201 page = virt_to_head_page(x);
Christoph Lameter81819f02007-05-06 14:49:36 -07002202 s = page->slab;
2203
Christoph Lameter77c5e2d2007-05-06 14:49:42 -07002204 slab_free(s, page, (void *)x, __builtin_return_address(0));
Christoph Lameter81819f02007-05-06 14:49:36 -07002205}
2206EXPORT_SYMBOL(kfree);
2207
Christoph Lameter2086d262007-05-06 14:49:46 -07002208/*
Christoph Lameter672bba32007-05-09 02:32:39 -07002209 * kmem_cache_shrink removes empty slabs from the partial lists and sorts
2210 * the remaining slabs by the number of items in use. The slabs with the
2211 * most items in use come first. New allocations will then fill those up
2212 * and thus they can be removed from the partial lists.
2213 *
2214 * The slabs with the least items are placed last. This results in them
2215 * being allocated from last increasing the chance that the last objects
2216 * are freed in them.
Christoph Lameter2086d262007-05-06 14:49:46 -07002217 */
2218int kmem_cache_shrink(struct kmem_cache *s)
2219{
2220 int node;
2221 int i;
2222 struct kmem_cache_node *n;
2223 struct page *page;
2224 struct page *t;
2225 struct list_head *slabs_by_inuse =
2226 kmalloc(sizeof(struct list_head) * s->objects, GFP_KERNEL);
2227 unsigned long flags;
2228
2229 if (!slabs_by_inuse)
2230 return -ENOMEM;
2231
2232 flush_all(s);
2233 for_each_online_node(node) {
2234 n = get_node(s, node);
2235
2236 if (!n->nr_partial)
2237 continue;
2238
2239 for (i = 0; i < s->objects; i++)
2240 INIT_LIST_HEAD(slabs_by_inuse + i);
2241
2242 spin_lock_irqsave(&n->list_lock, flags);
2243
2244 /*
Christoph Lameter672bba32007-05-09 02:32:39 -07002245 * Build lists indexed by the items in use in each slab.
Christoph Lameter2086d262007-05-06 14:49:46 -07002246 *
Christoph Lameter672bba32007-05-09 02:32:39 -07002247 * Note that concurrent frees may occur while we hold the
2248 * list_lock. page->inuse here is the upper limit.
Christoph Lameter2086d262007-05-06 14:49:46 -07002249 */
2250 list_for_each_entry_safe(page, t, &n->partial, lru) {
2251 if (!page->inuse && slab_trylock(page)) {
2252 /*
2253 * Must hold slab lock here because slab_free
2254 * may have freed the last object and be
2255 * waiting to release the slab.
2256 */
2257 list_del(&page->lru);
2258 n->nr_partial--;
2259 slab_unlock(page);
2260 discard_slab(s, page);
2261 } else {
2262 if (n->nr_partial > MAX_PARTIAL)
2263 list_move(&page->lru,
2264 slabs_by_inuse + page->inuse);
2265 }
2266 }
2267
2268 if (n->nr_partial <= MAX_PARTIAL)
2269 goto out;
2270
2271 /*
Christoph Lameter672bba32007-05-09 02:32:39 -07002272 * Rebuild the partial list with the slabs filled up most
2273 * first and the least used slabs at the end.
Christoph Lameter2086d262007-05-06 14:49:46 -07002274 */
2275 for (i = s->objects - 1; i >= 0; i--)
2276 list_splice(slabs_by_inuse + i, n->partial.prev);
2277
2278 out:
2279 spin_unlock_irqrestore(&n->list_lock, flags);
2280 }
2281
2282 kfree(slabs_by_inuse);
2283 return 0;
2284}
2285EXPORT_SYMBOL(kmem_cache_shrink);
2286
Christoph Lameter81819f02007-05-06 14:49:36 -07002287/**
2288 * krealloc - reallocate memory. The contents will remain unchanged.
Christoph Lameter81819f02007-05-06 14:49:36 -07002289 * @p: object to reallocate memory for.
2290 * @new_size: how many bytes of memory are required.
2291 * @flags: the type of memory to allocate.
2292 *
2293 * The contents of the object pointed to are preserved up to the
2294 * lesser of the new and old sizes. If @p is %NULL, krealloc()
2295 * behaves exactly like kmalloc(). If @size is 0 and @p is not a
2296 * %NULL pointer, the object pointed to is freed.
2297 */
2298void *krealloc(const void *p, size_t new_size, gfp_t flags)
2299{
Christoph Lameter81819f02007-05-06 14:49:36 -07002300 void *ret;
Christoph Lameter1f99a282007-05-09 02:32:38 -07002301 size_t ks;
Christoph Lameter81819f02007-05-06 14:49:36 -07002302
2303 if (unlikely(!p))
2304 return kmalloc(new_size, flags);
2305
2306 if (unlikely(!new_size)) {
2307 kfree(p);
2308 return NULL;
2309 }
2310
Christoph Lameter1f99a282007-05-09 02:32:38 -07002311 ks = ksize(p);
2312 if (ks >= new_size)
Christoph Lameter81819f02007-05-06 14:49:36 -07002313 return (void *)p;
2314
2315 ret = kmalloc(new_size, flags);
2316 if (ret) {
Christoph Lameter1f99a282007-05-09 02:32:38 -07002317 memcpy(ret, p, min(new_size, ks));
Christoph Lameter81819f02007-05-06 14:49:36 -07002318 kfree(p);
2319 }
2320 return ret;
2321}
2322EXPORT_SYMBOL(krealloc);
2323
2324/********************************************************************
2325 * Basic setup of slabs
2326 *******************************************************************/
2327
2328void __init kmem_cache_init(void)
2329{
2330 int i;
2331
2332#ifdef CONFIG_NUMA
2333 /*
2334 * Must first have the slab cache available for the allocations of the
Christoph Lameter672bba32007-05-09 02:32:39 -07002335 * struct kmem_cache_node's. There is special bootstrap code in
Christoph Lameter81819f02007-05-06 14:49:36 -07002336 * kmem_cache_open for slab_state == DOWN.
2337 */
2338 create_kmalloc_cache(&kmalloc_caches[0], "kmem_cache_node",
2339 sizeof(struct kmem_cache_node), GFP_KERNEL);
2340#endif
2341
2342 /* Able to allocate the per node structures */
2343 slab_state = PARTIAL;
2344
2345 /* Caches that are not of the two-to-the-power-of size */
2346 create_kmalloc_cache(&kmalloc_caches[1],
2347 "kmalloc-96", 96, GFP_KERNEL);
2348 create_kmalloc_cache(&kmalloc_caches[2],
2349 "kmalloc-192", 192, GFP_KERNEL);
2350
2351 for (i = KMALLOC_SHIFT_LOW; i <= KMALLOC_SHIFT_HIGH; i++)
2352 create_kmalloc_cache(&kmalloc_caches[i],
2353 "kmalloc", 1 << i, GFP_KERNEL);
2354
2355 slab_state = UP;
2356
2357 /* Provide the correct kmalloc names now that the caches are up */
2358 for (i = KMALLOC_SHIFT_LOW; i <= KMALLOC_SHIFT_HIGH; i++)
2359 kmalloc_caches[i]. name =
2360 kasprintf(GFP_KERNEL, "kmalloc-%d", 1 << i);
2361
2362#ifdef CONFIG_SMP
2363 register_cpu_notifier(&slab_notifier);
2364#endif
2365
2366 if (nr_cpu_ids) /* Remove when nr_cpu_ids is fixed upstream ! */
2367 kmem_size = offsetof(struct kmem_cache, cpu_slab)
2368 + nr_cpu_ids * sizeof(struct page *);
2369
2370 printk(KERN_INFO "SLUB: Genslabs=%d, HWalign=%d, Order=%d-%d, MinObjects=%d,"
2371 " Processors=%d, Nodes=%d\n",
Christoph Lameter65c02d42007-05-09 02:32:35 -07002372 KMALLOC_SHIFT_HIGH, cache_line_size(),
Christoph Lameter81819f02007-05-06 14:49:36 -07002373 slub_min_order, slub_max_order, slub_min_objects,
2374 nr_cpu_ids, nr_node_ids);
2375}
2376
2377/*
2378 * Find a mergeable slab cache
2379 */
2380static int slab_unmergeable(struct kmem_cache *s)
2381{
2382 if (slub_nomerge || (s->flags & SLUB_NEVER_MERGE))
2383 return 1;
2384
2385 if (s->ctor || s->dtor)
2386 return 1;
2387
2388 return 0;
2389}
2390
2391static struct kmem_cache *find_mergeable(size_t size,
2392 size_t align, unsigned long flags,
2393 void (*ctor)(void *, struct kmem_cache *, unsigned long),
2394 void (*dtor)(void *, struct kmem_cache *, unsigned long))
2395{
2396 struct list_head *h;
2397
2398 if (slub_nomerge || (flags & SLUB_NEVER_MERGE))
2399 return NULL;
2400
2401 if (ctor || dtor)
2402 return NULL;
2403
2404 size = ALIGN(size, sizeof(void *));
2405 align = calculate_alignment(flags, align, size);
2406 size = ALIGN(size, align);
2407
2408 list_for_each(h, &slab_caches) {
2409 struct kmem_cache *s =
2410 container_of(h, struct kmem_cache, list);
2411
2412 if (slab_unmergeable(s))
2413 continue;
2414
2415 if (size > s->size)
2416 continue;
2417
2418 if (((flags | slub_debug) & SLUB_MERGE_SAME) !=
2419 (s->flags & SLUB_MERGE_SAME))
2420 continue;
2421 /*
2422 * Check if alignment is compatible.
2423 * Courtesy of Adrian Drzewiecki
2424 */
2425 if ((s->size & ~(align -1)) != s->size)
2426 continue;
2427
2428 if (s->size - size >= sizeof(void *))
2429 continue;
2430
2431 return s;
2432 }
2433 return NULL;
2434}
2435
2436struct kmem_cache *kmem_cache_create(const char *name, size_t size,
2437 size_t align, unsigned long flags,
2438 void (*ctor)(void *, struct kmem_cache *, unsigned long),
2439 void (*dtor)(void *, struct kmem_cache *, unsigned long))
2440{
2441 struct kmem_cache *s;
2442
2443 down_write(&slub_lock);
2444 s = find_mergeable(size, align, flags, dtor, ctor);
2445 if (s) {
2446 s->refcount++;
2447 /*
2448 * Adjust the object sizes so that we clear
2449 * the complete object on kzalloc.
2450 */
2451 s->objsize = max(s->objsize, (int)size);
2452 s->inuse = max_t(int, s->inuse, ALIGN(size, sizeof(void *)));
2453 if (sysfs_slab_alias(s, name))
2454 goto err;
2455 } else {
2456 s = kmalloc(kmem_size, GFP_KERNEL);
2457 if (s && kmem_cache_open(s, GFP_KERNEL, name,
2458 size, align, flags, ctor, dtor)) {
2459 if (sysfs_slab_add(s)) {
2460 kfree(s);
2461 goto err;
2462 }
2463 list_add(&s->list, &slab_caches);
2464 } else
2465 kfree(s);
2466 }
2467 up_write(&slub_lock);
2468 return s;
2469
2470err:
2471 up_write(&slub_lock);
2472 if (flags & SLAB_PANIC)
2473 panic("Cannot create slabcache %s\n", name);
2474 else
2475 s = NULL;
2476 return s;
2477}
2478EXPORT_SYMBOL(kmem_cache_create);
2479
2480void *kmem_cache_zalloc(struct kmem_cache *s, gfp_t flags)
2481{
2482 void *x;
2483
Christoph Lameter77c5e2d2007-05-06 14:49:42 -07002484 x = slab_alloc(s, flags, -1, __builtin_return_address(0));
Christoph Lameter81819f02007-05-06 14:49:36 -07002485 if (x)
2486 memset(x, 0, s->objsize);
2487 return x;
2488}
2489EXPORT_SYMBOL(kmem_cache_zalloc);
2490
2491#ifdef CONFIG_SMP
2492static void for_all_slabs(void (*func)(struct kmem_cache *, int), int cpu)
2493{
2494 struct list_head *h;
2495
2496 down_read(&slub_lock);
2497 list_for_each(h, &slab_caches) {
2498 struct kmem_cache *s =
2499 container_of(h, struct kmem_cache, list);
2500
2501 func(s, cpu);
2502 }
2503 up_read(&slub_lock);
2504}
2505
2506/*
Christoph Lameter672bba32007-05-09 02:32:39 -07002507 * Use the cpu notifier to insure that the cpu slabs are flushed when
2508 * necessary.
Christoph Lameter81819f02007-05-06 14:49:36 -07002509 */
2510static int __cpuinit slab_cpuup_callback(struct notifier_block *nfb,
2511 unsigned long action, void *hcpu)
2512{
2513 long cpu = (long)hcpu;
2514
2515 switch (action) {
2516 case CPU_UP_CANCELED:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07002517 case CPU_UP_CANCELED_FROZEN:
Christoph Lameter81819f02007-05-06 14:49:36 -07002518 case CPU_DEAD:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07002519 case CPU_DEAD_FROZEN:
Christoph Lameter81819f02007-05-06 14:49:36 -07002520 for_all_slabs(__flush_cpu_slab, cpu);
2521 break;
2522 default:
2523 break;
2524 }
2525 return NOTIFY_OK;
2526}
2527
2528static struct notifier_block __cpuinitdata slab_notifier =
2529 { &slab_cpuup_callback, NULL, 0 };
2530
2531#endif
2532
Christoph Lameter81819f02007-05-06 14:49:36 -07002533#ifdef CONFIG_NUMA
2534
2535/*****************************************************************
2536 * Generic reaper used to support the page allocator
2537 * (the cpu slabs are reaped by a per slab workqueue).
2538 *
2539 * Maybe move this to the page allocator?
2540 ****************************************************************/
2541
2542static DEFINE_PER_CPU(unsigned long, reap_node);
2543
2544static void init_reap_node(int cpu)
2545{
2546 int node;
2547
2548 node = next_node(cpu_to_node(cpu), node_online_map);
2549 if (node == MAX_NUMNODES)
2550 node = first_node(node_online_map);
2551
2552 __get_cpu_var(reap_node) = node;
2553}
2554
2555static void next_reap_node(void)
2556{
2557 int node = __get_cpu_var(reap_node);
2558
2559 /*
2560 * Also drain per cpu pages on remote zones
2561 */
2562 if (node != numa_node_id())
2563 drain_node_pages(node);
2564
2565 node = next_node(node, node_online_map);
2566 if (unlikely(node >= MAX_NUMNODES))
2567 node = first_node(node_online_map);
2568 __get_cpu_var(reap_node) = node;
2569}
2570#else
2571#define init_reap_node(cpu) do { } while (0)
2572#define next_reap_node(void) do { } while (0)
2573#endif
2574
2575#define REAPTIMEOUT_CPUC (2*HZ)
2576
2577#ifdef CONFIG_SMP
2578static DEFINE_PER_CPU(struct delayed_work, reap_work);
2579
2580static void cache_reap(struct work_struct *unused)
2581{
2582 next_reap_node();
2583 refresh_cpu_vm_stats(smp_processor_id());
2584 schedule_delayed_work(&__get_cpu_var(reap_work),
2585 REAPTIMEOUT_CPUC);
2586}
2587
2588static void __devinit start_cpu_timer(int cpu)
2589{
2590 struct delayed_work *reap_work = &per_cpu(reap_work, cpu);
2591
2592 /*
2593 * When this gets called from do_initcalls via cpucache_init(),
2594 * init_workqueues() has already run, so keventd will be setup
2595 * at that time.
2596 */
2597 if (keventd_up() && reap_work->work.func == NULL) {
2598 init_reap_node(cpu);
2599 INIT_DELAYED_WORK(reap_work, cache_reap);
2600 schedule_delayed_work_on(cpu, reap_work, HZ + 3 * cpu);
2601 }
2602}
2603
2604static int __init cpucache_init(void)
2605{
2606 int cpu;
2607
2608 /*
2609 * Register the timers that drain pcp pages and update vm statistics
2610 */
2611 for_each_online_cpu(cpu)
2612 start_cpu_timer(cpu);
2613 return 0;
2614}
2615__initcall(cpucache_init);
2616#endif
2617
Christoph Lameter81819f02007-05-06 14:49:36 -07002618void *__kmalloc_track_caller(size_t size, gfp_t gfpflags, void *caller)
2619{
2620 struct kmem_cache *s = get_slab(size, gfpflags);
Christoph Lameter81819f02007-05-06 14:49:36 -07002621
2622 if (!s)
2623 return NULL;
2624
Christoph Lameter77c5e2d2007-05-06 14:49:42 -07002625 return slab_alloc(s, gfpflags, -1, caller);
Christoph Lameter81819f02007-05-06 14:49:36 -07002626}
2627
2628void *__kmalloc_node_track_caller(size_t size, gfp_t gfpflags,
2629 int node, void *caller)
2630{
2631 struct kmem_cache *s = get_slab(size, gfpflags);
Christoph Lameter81819f02007-05-06 14:49:36 -07002632
2633 if (!s)
2634 return NULL;
2635
Christoph Lameter77c5e2d2007-05-06 14:49:42 -07002636 return slab_alloc(s, gfpflags, node, caller);
Christoph Lameter81819f02007-05-06 14:49:36 -07002637}
2638
Christoph Lameter41ecc552007-05-09 02:32:44 -07002639#if defined(CONFIG_SYSFS) && defined(CONFIG_SLUB_DEBUG)
Christoph Lameter53e15af2007-05-06 14:49:43 -07002640static int validate_slab(struct kmem_cache *s, struct page *page)
2641{
2642 void *p;
2643 void *addr = page_address(page);
Christoph Lameter7656c722007-05-09 02:32:40 -07002644 DECLARE_BITMAP(map, s->objects);
Christoph Lameter53e15af2007-05-06 14:49:43 -07002645
2646 if (!check_slab(s, page) ||
2647 !on_freelist(s, page, NULL))
2648 return 0;
2649
2650 /* Now we know that a valid freelist exists */
2651 bitmap_zero(map, s->objects);
2652
Christoph Lameter7656c722007-05-09 02:32:40 -07002653 for_each_free_object(p, s, page->freelist) {
2654 set_bit(slab_index(p, s, addr), map);
Christoph Lameter53e15af2007-05-06 14:49:43 -07002655 if (!check_object(s, page, p, 0))
2656 return 0;
2657 }
2658
Christoph Lameter7656c722007-05-09 02:32:40 -07002659 for_each_object(p, s, addr)
2660 if (!test_bit(slab_index(p, s, addr), map))
Christoph Lameter53e15af2007-05-06 14:49:43 -07002661 if (!check_object(s, page, p, 1))
2662 return 0;
2663 return 1;
2664}
2665
2666static void validate_slab_slab(struct kmem_cache *s, struct page *page)
2667{
2668 if (slab_trylock(page)) {
2669 validate_slab(s, page);
2670 slab_unlock(page);
2671 } else
2672 printk(KERN_INFO "SLUB %s: Skipped busy slab 0x%p\n",
2673 s->name, page);
2674
2675 if (s->flags & DEBUG_DEFAULT_FLAGS) {
Christoph Lameter35e5d7e2007-05-09 02:32:42 -07002676 if (!SlabDebug(page))
2677 printk(KERN_ERR "SLUB %s: SlabDebug not set "
Christoph Lameter53e15af2007-05-06 14:49:43 -07002678 "on slab 0x%p\n", s->name, page);
2679 } else {
Christoph Lameter35e5d7e2007-05-09 02:32:42 -07002680 if (SlabDebug(page))
2681 printk(KERN_ERR "SLUB %s: SlabDebug set on "
Christoph Lameter53e15af2007-05-06 14:49:43 -07002682 "slab 0x%p\n", s->name, page);
2683 }
2684}
2685
2686static int validate_slab_node(struct kmem_cache *s, struct kmem_cache_node *n)
2687{
2688 unsigned long count = 0;
2689 struct page *page;
2690 unsigned long flags;
2691
2692 spin_lock_irqsave(&n->list_lock, flags);
2693
2694 list_for_each_entry(page, &n->partial, lru) {
2695 validate_slab_slab(s, page);
2696 count++;
2697 }
2698 if (count != n->nr_partial)
2699 printk(KERN_ERR "SLUB %s: %ld partial slabs counted but "
2700 "counter=%ld\n", s->name, count, n->nr_partial);
2701
2702 if (!(s->flags & SLAB_STORE_USER))
2703 goto out;
2704
2705 list_for_each_entry(page, &n->full, lru) {
2706 validate_slab_slab(s, page);
2707 count++;
2708 }
2709 if (count != atomic_long_read(&n->nr_slabs))
2710 printk(KERN_ERR "SLUB: %s %ld slabs counted but "
2711 "counter=%ld\n", s->name, count,
2712 atomic_long_read(&n->nr_slabs));
2713
2714out:
2715 spin_unlock_irqrestore(&n->list_lock, flags);
2716 return count;
2717}
2718
2719static unsigned long validate_slab_cache(struct kmem_cache *s)
2720{
2721 int node;
2722 unsigned long count = 0;
2723
2724 flush_all(s);
2725 for_each_online_node(node) {
2726 struct kmem_cache_node *n = get_node(s, node);
2727
2728 count += validate_slab_node(s, n);
2729 }
2730 return count;
2731}
2732
Christoph Lameterb3459702007-05-09 02:32:41 -07002733#ifdef SLUB_RESILIENCY_TEST
2734static void resiliency_test(void)
2735{
2736 u8 *p;
2737
2738 printk(KERN_ERR "SLUB resiliency testing\n");
2739 printk(KERN_ERR "-----------------------\n");
2740 printk(KERN_ERR "A. Corruption after allocation\n");
2741
2742 p = kzalloc(16, GFP_KERNEL);
2743 p[16] = 0x12;
2744 printk(KERN_ERR "\n1. kmalloc-16: Clobber Redzone/next pointer"
2745 " 0x12->0x%p\n\n", p + 16);
2746
2747 validate_slab_cache(kmalloc_caches + 4);
2748
2749 /* Hmmm... The next two are dangerous */
2750 p = kzalloc(32, GFP_KERNEL);
2751 p[32 + sizeof(void *)] = 0x34;
2752 printk(KERN_ERR "\n2. kmalloc-32: Clobber next pointer/next slab"
2753 " 0x34 -> -0x%p\n", p);
2754 printk(KERN_ERR "If allocated object is overwritten then not detectable\n\n");
2755
2756 validate_slab_cache(kmalloc_caches + 5);
2757 p = kzalloc(64, GFP_KERNEL);
2758 p += 64 + (get_cycles() & 0xff) * sizeof(void *);
2759 *p = 0x56;
2760 printk(KERN_ERR "\n3. kmalloc-64: corrupting random byte 0x56->0x%p\n",
2761 p);
2762 printk(KERN_ERR "If allocated object is overwritten then not detectable\n\n");
2763 validate_slab_cache(kmalloc_caches + 6);
2764
2765 printk(KERN_ERR "\nB. Corruption after free\n");
2766 p = kzalloc(128, GFP_KERNEL);
2767 kfree(p);
2768 *p = 0x78;
2769 printk(KERN_ERR "1. kmalloc-128: Clobber first word 0x78->0x%p\n\n", p);
2770 validate_slab_cache(kmalloc_caches + 7);
2771
2772 p = kzalloc(256, GFP_KERNEL);
2773 kfree(p);
2774 p[50] = 0x9a;
2775 printk(KERN_ERR "\n2. kmalloc-256: Clobber 50th byte 0x9a->0x%p\n\n", p);
2776 validate_slab_cache(kmalloc_caches + 8);
2777
2778 p = kzalloc(512, GFP_KERNEL);
2779 kfree(p);
2780 p[512] = 0xab;
2781 printk(KERN_ERR "\n3. kmalloc-512: Clobber redzone 0xab->0x%p\n\n", p);
2782 validate_slab_cache(kmalloc_caches + 9);
2783}
2784#else
2785static void resiliency_test(void) {};
2786#endif
2787
Christoph Lameter88a420e2007-05-06 14:49:45 -07002788/*
Christoph Lameter672bba32007-05-09 02:32:39 -07002789 * Generate lists of code addresses where slabcache objects are allocated
Christoph Lameter88a420e2007-05-06 14:49:45 -07002790 * and freed.
2791 */
2792
2793struct location {
2794 unsigned long count;
2795 void *addr;
Christoph Lameter45edfa52007-05-09 02:32:45 -07002796 long long sum_time;
2797 long min_time;
2798 long max_time;
2799 long min_pid;
2800 long max_pid;
2801 cpumask_t cpus;
2802 nodemask_t nodes;
Christoph Lameter88a420e2007-05-06 14:49:45 -07002803};
2804
2805struct loc_track {
2806 unsigned long max;
2807 unsigned long count;
2808 struct location *loc;
2809};
2810
2811static void free_loc_track(struct loc_track *t)
2812{
2813 if (t->max)
2814 free_pages((unsigned long)t->loc,
2815 get_order(sizeof(struct location) * t->max));
2816}
2817
2818static int alloc_loc_track(struct loc_track *t, unsigned long max)
2819{
2820 struct location *l;
2821 int order;
2822
2823 if (!max)
2824 max = PAGE_SIZE / sizeof(struct location);
2825
2826 order = get_order(sizeof(struct location) * max);
2827
2828 l = (void *)__get_free_pages(GFP_KERNEL, order);
2829
2830 if (!l)
2831 return 0;
2832
2833 if (t->count) {
2834 memcpy(l, t->loc, sizeof(struct location) * t->count);
2835 free_loc_track(t);
2836 }
2837 t->max = max;
2838 t->loc = l;
2839 return 1;
2840}
2841
2842static int add_location(struct loc_track *t, struct kmem_cache *s,
Christoph Lameter45edfa52007-05-09 02:32:45 -07002843 const struct track *track)
Christoph Lameter88a420e2007-05-06 14:49:45 -07002844{
2845 long start, end, pos;
2846 struct location *l;
2847 void *caddr;
Christoph Lameter45edfa52007-05-09 02:32:45 -07002848 unsigned long age = jiffies - track->when;
Christoph Lameter88a420e2007-05-06 14:49:45 -07002849
2850 start = -1;
2851 end = t->count;
2852
2853 for ( ; ; ) {
2854 pos = start + (end - start + 1) / 2;
2855
2856 /*
2857 * There is nothing at "end". If we end up there
2858 * we need to add something to before end.
2859 */
2860 if (pos == end)
2861 break;
2862
2863 caddr = t->loc[pos].addr;
Christoph Lameter45edfa52007-05-09 02:32:45 -07002864 if (track->addr == caddr) {
2865
2866 l = &t->loc[pos];
2867 l->count++;
2868 if (track->when) {
2869 l->sum_time += age;
2870 if (age < l->min_time)
2871 l->min_time = age;
2872 if (age > l->max_time)
2873 l->max_time = age;
2874
2875 if (track->pid < l->min_pid)
2876 l->min_pid = track->pid;
2877 if (track->pid > l->max_pid)
2878 l->max_pid = track->pid;
2879
2880 cpu_set(track->cpu, l->cpus);
2881 }
2882 node_set(page_to_nid(virt_to_page(track)), l->nodes);
Christoph Lameter88a420e2007-05-06 14:49:45 -07002883 return 1;
2884 }
2885
Christoph Lameter45edfa52007-05-09 02:32:45 -07002886 if (track->addr < caddr)
Christoph Lameter88a420e2007-05-06 14:49:45 -07002887 end = pos;
2888 else
2889 start = pos;
2890 }
2891
2892 /*
Christoph Lameter672bba32007-05-09 02:32:39 -07002893 * Not found. Insert new tracking element.
Christoph Lameter88a420e2007-05-06 14:49:45 -07002894 */
2895 if (t->count >= t->max && !alloc_loc_track(t, 2 * t->max))
2896 return 0;
2897
2898 l = t->loc + pos;
2899 if (pos < t->count)
2900 memmove(l + 1, l,
2901 (t->count - pos) * sizeof(struct location));
2902 t->count++;
2903 l->count = 1;
Christoph Lameter45edfa52007-05-09 02:32:45 -07002904 l->addr = track->addr;
2905 l->sum_time = age;
2906 l->min_time = age;
2907 l->max_time = age;
2908 l->min_pid = track->pid;
2909 l->max_pid = track->pid;
2910 cpus_clear(l->cpus);
2911 cpu_set(track->cpu, l->cpus);
2912 nodes_clear(l->nodes);
2913 node_set(page_to_nid(virt_to_page(track)), l->nodes);
Christoph Lameter88a420e2007-05-06 14:49:45 -07002914 return 1;
2915}
2916
2917static void process_slab(struct loc_track *t, struct kmem_cache *s,
2918 struct page *page, enum track_item alloc)
2919{
2920 void *addr = page_address(page);
Christoph Lameter7656c722007-05-09 02:32:40 -07002921 DECLARE_BITMAP(map, s->objects);
Christoph Lameter88a420e2007-05-06 14:49:45 -07002922 void *p;
2923
2924 bitmap_zero(map, s->objects);
Christoph Lameter7656c722007-05-09 02:32:40 -07002925 for_each_free_object(p, s, page->freelist)
2926 set_bit(slab_index(p, s, addr), map);
Christoph Lameter88a420e2007-05-06 14:49:45 -07002927
Christoph Lameter7656c722007-05-09 02:32:40 -07002928 for_each_object(p, s, addr)
Christoph Lameter45edfa52007-05-09 02:32:45 -07002929 if (!test_bit(slab_index(p, s, addr), map))
2930 add_location(t, s, get_track(s, p, alloc));
Christoph Lameter88a420e2007-05-06 14:49:45 -07002931}
2932
2933static int list_locations(struct kmem_cache *s, char *buf,
2934 enum track_item alloc)
2935{
2936 int n = 0;
2937 unsigned long i;
2938 struct loc_track t;
2939 int node;
2940
2941 t.count = 0;
2942 t.max = 0;
2943
2944 /* Push back cpu slabs */
2945 flush_all(s);
2946
2947 for_each_online_node(node) {
2948 struct kmem_cache_node *n = get_node(s, node);
2949 unsigned long flags;
2950 struct page *page;
2951
2952 if (!atomic_read(&n->nr_slabs))
2953 continue;
2954
2955 spin_lock_irqsave(&n->list_lock, flags);
2956 list_for_each_entry(page, &n->partial, lru)
2957 process_slab(&t, s, page, alloc);
2958 list_for_each_entry(page, &n->full, lru)
2959 process_slab(&t, s, page, alloc);
2960 spin_unlock_irqrestore(&n->list_lock, flags);
2961 }
2962
2963 for (i = 0; i < t.count; i++) {
Christoph Lameter45edfa52007-05-09 02:32:45 -07002964 struct location *l = &t.loc[i];
Christoph Lameter88a420e2007-05-06 14:49:45 -07002965
2966 if (n > PAGE_SIZE - 100)
2967 break;
Christoph Lameter45edfa52007-05-09 02:32:45 -07002968 n += sprintf(buf + n, "%7ld ", l->count);
2969
2970 if (l->addr)
2971 n += sprint_symbol(buf + n, (unsigned long)l->addr);
Christoph Lameter88a420e2007-05-06 14:49:45 -07002972 else
2973 n += sprintf(buf + n, "<not-available>");
Christoph Lameter45edfa52007-05-09 02:32:45 -07002974
2975 if (l->sum_time != l->min_time) {
2976 unsigned long remainder;
2977
2978 n += sprintf(buf + n, " age=%ld/%ld/%ld",
2979 l->min_time,
2980 div_long_long_rem(l->sum_time, l->count, &remainder),
2981 l->max_time);
2982 } else
2983 n += sprintf(buf + n, " age=%ld",
2984 l->min_time);
2985
2986 if (l->min_pid != l->max_pid)
2987 n += sprintf(buf + n, " pid=%ld-%ld",
2988 l->min_pid, l->max_pid);
2989 else
2990 n += sprintf(buf + n, " pid=%ld",
2991 l->min_pid);
2992
2993 if (num_online_cpus() > 1 && !cpus_empty(l->cpus)) {
2994 n += sprintf(buf + n, " cpus=");
2995 n += cpulist_scnprintf(buf + n, PAGE_SIZE - n - 50,
2996 l->cpus);
2997 }
2998
2999 if (num_online_nodes() > 1 && !nodes_empty(l->nodes)) {
3000 n += sprintf(buf + n, " nodes=");
3001 n += nodelist_scnprintf(buf + n, PAGE_SIZE - n - 50,
3002 l->nodes);
3003 }
3004
Christoph Lameter88a420e2007-05-06 14:49:45 -07003005 n += sprintf(buf + n, "\n");
3006 }
3007
3008 free_loc_track(&t);
3009 if (!t.count)
3010 n += sprintf(buf, "No data\n");
3011 return n;
3012}
3013
Christoph Lameter81819f02007-05-06 14:49:36 -07003014static unsigned long count_partial(struct kmem_cache_node *n)
3015{
3016 unsigned long flags;
3017 unsigned long x = 0;
3018 struct page *page;
3019
3020 spin_lock_irqsave(&n->list_lock, flags);
3021 list_for_each_entry(page, &n->partial, lru)
3022 x += page->inuse;
3023 spin_unlock_irqrestore(&n->list_lock, flags);
3024 return x;
3025}
3026
3027enum slab_stat_type {
3028 SL_FULL,
3029 SL_PARTIAL,
3030 SL_CPU,
3031 SL_OBJECTS
3032};
3033
3034#define SO_FULL (1 << SL_FULL)
3035#define SO_PARTIAL (1 << SL_PARTIAL)
3036#define SO_CPU (1 << SL_CPU)
3037#define SO_OBJECTS (1 << SL_OBJECTS)
3038
3039static unsigned long slab_objects(struct kmem_cache *s,
3040 char *buf, unsigned long flags)
3041{
3042 unsigned long total = 0;
3043 int cpu;
3044 int node;
3045 int x;
3046 unsigned long *nodes;
3047 unsigned long *per_cpu;
3048
3049 nodes = kzalloc(2 * sizeof(unsigned long) * nr_node_ids, GFP_KERNEL);
3050 per_cpu = nodes + nr_node_ids;
3051
3052 for_each_possible_cpu(cpu) {
3053 struct page *page = s->cpu_slab[cpu];
3054 int node;
3055
3056 if (page) {
3057 node = page_to_nid(page);
3058 if (flags & SO_CPU) {
3059 int x = 0;
3060
3061 if (flags & SO_OBJECTS)
3062 x = page->inuse;
3063 else
3064 x = 1;
3065 total += x;
3066 nodes[node] += x;
3067 }
3068 per_cpu[node]++;
3069 }
3070 }
3071
3072 for_each_online_node(node) {
3073 struct kmem_cache_node *n = get_node(s, node);
3074
3075 if (flags & SO_PARTIAL) {
3076 if (flags & SO_OBJECTS)
3077 x = count_partial(n);
3078 else
3079 x = n->nr_partial;
3080 total += x;
3081 nodes[node] += x;
3082 }
3083
3084 if (flags & SO_FULL) {
3085 int full_slabs = atomic_read(&n->nr_slabs)
3086 - per_cpu[node]
3087 - n->nr_partial;
3088
3089 if (flags & SO_OBJECTS)
3090 x = full_slabs * s->objects;
3091 else
3092 x = full_slabs;
3093 total += x;
3094 nodes[node] += x;
3095 }
3096 }
3097
3098 x = sprintf(buf, "%lu", total);
3099#ifdef CONFIG_NUMA
3100 for_each_online_node(node)
3101 if (nodes[node])
3102 x += sprintf(buf + x, " N%d=%lu",
3103 node, nodes[node]);
3104#endif
3105 kfree(nodes);
3106 return x + sprintf(buf + x, "\n");
3107}
3108
3109static int any_slab_objects(struct kmem_cache *s)
3110{
3111 int node;
3112 int cpu;
3113
3114 for_each_possible_cpu(cpu)
3115 if (s->cpu_slab[cpu])
3116 return 1;
3117
3118 for_each_node(node) {
3119 struct kmem_cache_node *n = get_node(s, node);
3120
3121 if (n->nr_partial || atomic_read(&n->nr_slabs))
3122 return 1;
3123 }
3124 return 0;
3125}
3126
3127#define to_slab_attr(n) container_of(n, struct slab_attribute, attr)
3128#define to_slab(n) container_of(n, struct kmem_cache, kobj);
3129
3130struct slab_attribute {
3131 struct attribute attr;
3132 ssize_t (*show)(struct kmem_cache *s, char *buf);
3133 ssize_t (*store)(struct kmem_cache *s, const char *x, size_t count);
3134};
3135
3136#define SLAB_ATTR_RO(_name) \
3137 static struct slab_attribute _name##_attr = __ATTR_RO(_name)
3138
3139#define SLAB_ATTR(_name) \
3140 static struct slab_attribute _name##_attr = \
3141 __ATTR(_name, 0644, _name##_show, _name##_store)
3142
Christoph Lameter81819f02007-05-06 14:49:36 -07003143static ssize_t slab_size_show(struct kmem_cache *s, char *buf)
3144{
3145 return sprintf(buf, "%d\n", s->size);
3146}
3147SLAB_ATTR_RO(slab_size);
3148
3149static ssize_t align_show(struct kmem_cache *s, char *buf)
3150{
3151 return sprintf(buf, "%d\n", s->align);
3152}
3153SLAB_ATTR_RO(align);
3154
3155static ssize_t object_size_show(struct kmem_cache *s, char *buf)
3156{
3157 return sprintf(buf, "%d\n", s->objsize);
3158}
3159SLAB_ATTR_RO(object_size);
3160
3161static ssize_t objs_per_slab_show(struct kmem_cache *s, char *buf)
3162{
3163 return sprintf(buf, "%d\n", s->objects);
3164}
3165SLAB_ATTR_RO(objs_per_slab);
3166
3167static ssize_t order_show(struct kmem_cache *s, char *buf)
3168{
3169 return sprintf(buf, "%d\n", s->order);
3170}
3171SLAB_ATTR_RO(order);
3172
3173static ssize_t ctor_show(struct kmem_cache *s, char *buf)
3174{
3175 if (s->ctor) {
3176 int n = sprint_symbol(buf, (unsigned long)s->ctor);
3177
3178 return n + sprintf(buf + n, "\n");
3179 }
3180 return 0;
3181}
3182SLAB_ATTR_RO(ctor);
3183
3184static ssize_t dtor_show(struct kmem_cache *s, char *buf)
3185{
3186 if (s->dtor) {
3187 int n = sprint_symbol(buf, (unsigned long)s->dtor);
3188
3189 return n + sprintf(buf + n, "\n");
3190 }
3191 return 0;
3192}
3193SLAB_ATTR_RO(dtor);
3194
3195static ssize_t aliases_show(struct kmem_cache *s, char *buf)
3196{
3197 return sprintf(buf, "%d\n", s->refcount - 1);
3198}
3199SLAB_ATTR_RO(aliases);
3200
3201static ssize_t slabs_show(struct kmem_cache *s, char *buf)
3202{
3203 return slab_objects(s, buf, SO_FULL|SO_PARTIAL|SO_CPU);
3204}
3205SLAB_ATTR_RO(slabs);
3206
3207static ssize_t partial_show(struct kmem_cache *s, char *buf)
3208{
3209 return slab_objects(s, buf, SO_PARTIAL);
3210}
3211SLAB_ATTR_RO(partial);
3212
3213static ssize_t cpu_slabs_show(struct kmem_cache *s, char *buf)
3214{
3215 return slab_objects(s, buf, SO_CPU);
3216}
3217SLAB_ATTR_RO(cpu_slabs);
3218
3219static ssize_t objects_show(struct kmem_cache *s, char *buf)
3220{
3221 return slab_objects(s, buf, SO_FULL|SO_PARTIAL|SO_CPU|SO_OBJECTS);
3222}
3223SLAB_ATTR_RO(objects);
3224
3225static ssize_t sanity_checks_show(struct kmem_cache *s, char *buf)
3226{
3227 return sprintf(buf, "%d\n", !!(s->flags & SLAB_DEBUG_FREE));
3228}
3229
3230static ssize_t sanity_checks_store(struct kmem_cache *s,
3231 const char *buf, size_t length)
3232{
3233 s->flags &= ~SLAB_DEBUG_FREE;
3234 if (buf[0] == '1')
3235 s->flags |= SLAB_DEBUG_FREE;
3236 return length;
3237}
3238SLAB_ATTR(sanity_checks);
3239
3240static ssize_t trace_show(struct kmem_cache *s, char *buf)
3241{
3242 return sprintf(buf, "%d\n", !!(s->flags & SLAB_TRACE));
3243}
3244
3245static ssize_t trace_store(struct kmem_cache *s, const char *buf,
3246 size_t length)
3247{
3248 s->flags &= ~SLAB_TRACE;
3249 if (buf[0] == '1')
3250 s->flags |= SLAB_TRACE;
3251 return length;
3252}
3253SLAB_ATTR(trace);
3254
3255static ssize_t reclaim_account_show(struct kmem_cache *s, char *buf)
3256{
3257 return sprintf(buf, "%d\n", !!(s->flags & SLAB_RECLAIM_ACCOUNT));
3258}
3259
3260static ssize_t reclaim_account_store(struct kmem_cache *s,
3261 const char *buf, size_t length)
3262{
3263 s->flags &= ~SLAB_RECLAIM_ACCOUNT;
3264 if (buf[0] == '1')
3265 s->flags |= SLAB_RECLAIM_ACCOUNT;
3266 return length;
3267}
3268SLAB_ATTR(reclaim_account);
3269
3270static ssize_t hwcache_align_show(struct kmem_cache *s, char *buf)
3271{
Christoph Lameter5af60832007-05-06 14:49:56 -07003272 return sprintf(buf, "%d\n", !!(s->flags & SLAB_HWCACHE_ALIGN));
Christoph Lameter81819f02007-05-06 14:49:36 -07003273}
3274SLAB_ATTR_RO(hwcache_align);
3275
3276#ifdef CONFIG_ZONE_DMA
3277static ssize_t cache_dma_show(struct kmem_cache *s, char *buf)
3278{
3279 return sprintf(buf, "%d\n", !!(s->flags & SLAB_CACHE_DMA));
3280}
3281SLAB_ATTR_RO(cache_dma);
3282#endif
3283
3284static ssize_t destroy_by_rcu_show(struct kmem_cache *s, char *buf)
3285{
3286 return sprintf(buf, "%d\n", !!(s->flags & SLAB_DESTROY_BY_RCU));
3287}
3288SLAB_ATTR_RO(destroy_by_rcu);
3289
3290static ssize_t red_zone_show(struct kmem_cache *s, char *buf)
3291{
3292 return sprintf(buf, "%d\n", !!(s->flags & SLAB_RED_ZONE));
3293}
3294
3295static ssize_t red_zone_store(struct kmem_cache *s,
3296 const char *buf, size_t length)
3297{
3298 if (any_slab_objects(s))
3299 return -EBUSY;
3300
3301 s->flags &= ~SLAB_RED_ZONE;
3302 if (buf[0] == '1')
3303 s->flags |= SLAB_RED_ZONE;
3304 calculate_sizes(s);
3305 return length;
3306}
3307SLAB_ATTR(red_zone);
3308
3309static ssize_t poison_show(struct kmem_cache *s, char *buf)
3310{
3311 return sprintf(buf, "%d\n", !!(s->flags & SLAB_POISON));
3312}
3313
3314static ssize_t poison_store(struct kmem_cache *s,
3315 const char *buf, size_t length)
3316{
3317 if (any_slab_objects(s))
3318 return -EBUSY;
3319
3320 s->flags &= ~SLAB_POISON;
3321 if (buf[0] == '1')
3322 s->flags |= SLAB_POISON;
3323 calculate_sizes(s);
3324 return length;
3325}
3326SLAB_ATTR(poison);
3327
3328static ssize_t store_user_show(struct kmem_cache *s, char *buf)
3329{
3330 return sprintf(buf, "%d\n", !!(s->flags & SLAB_STORE_USER));
3331}
3332
3333static ssize_t store_user_store(struct kmem_cache *s,
3334 const char *buf, size_t length)
3335{
3336 if (any_slab_objects(s))
3337 return -EBUSY;
3338
3339 s->flags &= ~SLAB_STORE_USER;
3340 if (buf[0] == '1')
3341 s->flags |= SLAB_STORE_USER;
3342 calculate_sizes(s);
3343 return length;
3344}
3345SLAB_ATTR(store_user);
3346
Christoph Lameter53e15af2007-05-06 14:49:43 -07003347static ssize_t validate_show(struct kmem_cache *s, char *buf)
3348{
3349 return 0;
3350}
3351
3352static ssize_t validate_store(struct kmem_cache *s,
3353 const char *buf, size_t length)
3354{
3355 if (buf[0] == '1')
3356 validate_slab_cache(s);
3357 else
3358 return -EINVAL;
3359 return length;
3360}
3361SLAB_ATTR(validate);
3362
Christoph Lameter2086d262007-05-06 14:49:46 -07003363static ssize_t shrink_show(struct kmem_cache *s, char *buf)
3364{
3365 return 0;
3366}
3367
3368static ssize_t shrink_store(struct kmem_cache *s,
3369 const char *buf, size_t length)
3370{
3371 if (buf[0] == '1') {
3372 int rc = kmem_cache_shrink(s);
3373
3374 if (rc)
3375 return rc;
3376 } else
3377 return -EINVAL;
3378 return length;
3379}
3380SLAB_ATTR(shrink);
3381
Christoph Lameter88a420e2007-05-06 14:49:45 -07003382static ssize_t alloc_calls_show(struct kmem_cache *s, char *buf)
3383{
3384 if (!(s->flags & SLAB_STORE_USER))
3385 return -ENOSYS;
3386 return list_locations(s, buf, TRACK_ALLOC);
3387}
3388SLAB_ATTR_RO(alloc_calls);
3389
3390static ssize_t free_calls_show(struct kmem_cache *s, char *buf)
3391{
3392 if (!(s->flags & SLAB_STORE_USER))
3393 return -ENOSYS;
3394 return list_locations(s, buf, TRACK_FREE);
3395}
3396SLAB_ATTR_RO(free_calls);
3397
Christoph Lameter81819f02007-05-06 14:49:36 -07003398#ifdef CONFIG_NUMA
3399static ssize_t defrag_ratio_show(struct kmem_cache *s, char *buf)
3400{
3401 return sprintf(buf, "%d\n", s->defrag_ratio / 10);
3402}
3403
3404static ssize_t defrag_ratio_store(struct kmem_cache *s,
3405 const char *buf, size_t length)
3406{
3407 int n = simple_strtoul(buf, NULL, 10);
3408
3409 if (n < 100)
3410 s->defrag_ratio = n * 10;
3411 return length;
3412}
3413SLAB_ATTR(defrag_ratio);
3414#endif
3415
3416static struct attribute * slab_attrs[] = {
3417 &slab_size_attr.attr,
3418 &object_size_attr.attr,
3419 &objs_per_slab_attr.attr,
3420 &order_attr.attr,
3421 &objects_attr.attr,
3422 &slabs_attr.attr,
3423 &partial_attr.attr,
3424 &cpu_slabs_attr.attr,
3425 &ctor_attr.attr,
3426 &dtor_attr.attr,
3427 &aliases_attr.attr,
3428 &align_attr.attr,
3429 &sanity_checks_attr.attr,
3430 &trace_attr.attr,
3431 &hwcache_align_attr.attr,
3432 &reclaim_account_attr.attr,
3433 &destroy_by_rcu_attr.attr,
3434 &red_zone_attr.attr,
3435 &poison_attr.attr,
3436 &store_user_attr.attr,
Christoph Lameter53e15af2007-05-06 14:49:43 -07003437 &validate_attr.attr,
Christoph Lameter2086d262007-05-06 14:49:46 -07003438 &shrink_attr.attr,
Christoph Lameter88a420e2007-05-06 14:49:45 -07003439 &alloc_calls_attr.attr,
3440 &free_calls_attr.attr,
Christoph Lameter81819f02007-05-06 14:49:36 -07003441#ifdef CONFIG_ZONE_DMA
3442 &cache_dma_attr.attr,
3443#endif
3444#ifdef CONFIG_NUMA
3445 &defrag_ratio_attr.attr,
3446#endif
3447 NULL
3448};
3449
3450static struct attribute_group slab_attr_group = {
3451 .attrs = slab_attrs,
3452};
3453
3454static ssize_t slab_attr_show(struct kobject *kobj,
3455 struct attribute *attr,
3456 char *buf)
3457{
3458 struct slab_attribute *attribute;
3459 struct kmem_cache *s;
3460 int err;
3461
3462 attribute = to_slab_attr(attr);
3463 s = to_slab(kobj);
3464
3465 if (!attribute->show)
3466 return -EIO;
3467
3468 err = attribute->show(s, buf);
3469
3470 return err;
3471}
3472
3473static ssize_t slab_attr_store(struct kobject *kobj,
3474 struct attribute *attr,
3475 const char *buf, size_t len)
3476{
3477 struct slab_attribute *attribute;
3478 struct kmem_cache *s;
3479 int err;
3480
3481 attribute = to_slab_attr(attr);
3482 s = to_slab(kobj);
3483
3484 if (!attribute->store)
3485 return -EIO;
3486
3487 err = attribute->store(s, buf, len);
3488
3489 return err;
3490}
3491
3492static struct sysfs_ops slab_sysfs_ops = {
3493 .show = slab_attr_show,
3494 .store = slab_attr_store,
3495};
3496
3497static struct kobj_type slab_ktype = {
3498 .sysfs_ops = &slab_sysfs_ops,
3499};
3500
3501static int uevent_filter(struct kset *kset, struct kobject *kobj)
3502{
3503 struct kobj_type *ktype = get_ktype(kobj);
3504
3505 if (ktype == &slab_ktype)
3506 return 1;
3507 return 0;
3508}
3509
3510static struct kset_uevent_ops slab_uevent_ops = {
3511 .filter = uevent_filter,
3512};
3513
3514decl_subsys(slab, &slab_ktype, &slab_uevent_ops);
3515
3516#define ID_STR_LENGTH 64
3517
3518/* Create a unique string id for a slab cache:
3519 * format
3520 * :[flags-]size:[memory address of kmemcache]
3521 */
3522static char *create_unique_id(struct kmem_cache *s)
3523{
3524 char *name = kmalloc(ID_STR_LENGTH, GFP_KERNEL);
3525 char *p = name;
3526
3527 BUG_ON(!name);
3528
3529 *p++ = ':';
3530 /*
3531 * First flags affecting slabcache operations. We will only
3532 * get here for aliasable slabs so we do not need to support
3533 * too many flags. The flags here must cover all flags that
3534 * are matched during merging to guarantee that the id is
3535 * unique.
3536 */
3537 if (s->flags & SLAB_CACHE_DMA)
3538 *p++ = 'd';
3539 if (s->flags & SLAB_RECLAIM_ACCOUNT)
3540 *p++ = 'a';
3541 if (s->flags & SLAB_DEBUG_FREE)
3542 *p++ = 'F';
3543 if (p != name + 1)
3544 *p++ = '-';
3545 p += sprintf(p, "%07d", s->size);
3546 BUG_ON(p > name + ID_STR_LENGTH - 1);
3547 return name;
3548}
3549
3550static int sysfs_slab_add(struct kmem_cache *s)
3551{
3552 int err;
3553 const char *name;
3554 int unmergeable;
3555
3556 if (slab_state < SYSFS)
3557 /* Defer until later */
3558 return 0;
3559
3560 unmergeable = slab_unmergeable(s);
3561 if (unmergeable) {
3562 /*
3563 * Slabcache can never be merged so we can use the name proper.
3564 * This is typically the case for debug situations. In that
3565 * case we can catch duplicate names easily.
3566 */
Linus Torvalds0f9008e2007-05-07 12:31:58 -07003567 sysfs_remove_link(&slab_subsys.kobj, s->name);
Christoph Lameter81819f02007-05-06 14:49:36 -07003568 name = s->name;
3569 } else {
3570 /*
3571 * Create a unique name for the slab as a target
3572 * for the symlinks.
3573 */
3574 name = create_unique_id(s);
3575 }
3576
3577 kobj_set_kset_s(s, slab_subsys);
3578 kobject_set_name(&s->kobj, name);
3579 kobject_init(&s->kobj);
3580 err = kobject_add(&s->kobj);
3581 if (err)
3582 return err;
3583
3584 err = sysfs_create_group(&s->kobj, &slab_attr_group);
3585 if (err)
3586 return err;
3587 kobject_uevent(&s->kobj, KOBJ_ADD);
3588 if (!unmergeable) {
3589 /* Setup first alias */
3590 sysfs_slab_alias(s, s->name);
3591 kfree(name);
3592 }
3593 return 0;
3594}
3595
3596static void sysfs_slab_remove(struct kmem_cache *s)
3597{
3598 kobject_uevent(&s->kobj, KOBJ_REMOVE);
3599 kobject_del(&s->kobj);
3600}
3601
3602/*
3603 * Need to buffer aliases during bootup until sysfs becomes
3604 * available lest we loose that information.
3605 */
3606struct saved_alias {
3607 struct kmem_cache *s;
3608 const char *name;
3609 struct saved_alias *next;
3610};
3611
3612struct saved_alias *alias_list;
3613
3614static int sysfs_slab_alias(struct kmem_cache *s, const char *name)
3615{
3616 struct saved_alias *al;
3617
3618 if (slab_state == SYSFS) {
3619 /*
3620 * If we have a leftover link then remove it.
3621 */
Linus Torvalds0f9008e2007-05-07 12:31:58 -07003622 sysfs_remove_link(&slab_subsys.kobj, name);
3623 return sysfs_create_link(&slab_subsys.kobj,
Christoph Lameter81819f02007-05-06 14:49:36 -07003624 &s->kobj, name);
3625 }
3626
3627 al = kmalloc(sizeof(struct saved_alias), GFP_KERNEL);
3628 if (!al)
3629 return -ENOMEM;
3630
3631 al->s = s;
3632 al->name = name;
3633 al->next = alias_list;
3634 alias_list = al;
3635 return 0;
3636}
3637
3638static int __init slab_sysfs_init(void)
3639{
Christoph Lameter26a7bd02007-05-09 02:32:39 -07003640 struct list_head *h;
Christoph Lameter81819f02007-05-06 14:49:36 -07003641 int err;
3642
3643 err = subsystem_register(&slab_subsys);
3644 if (err) {
3645 printk(KERN_ERR "Cannot register slab subsystem.\n");
3646 return -ENOSYS;
3647 }
3648
Christoph Lameter26a7bd02007-05-09 02:32:39 -07003649 slab_state = SYSFS;
3650
3651 list_for_each(h, &slab_caches) {
3652 struct kmem_cache *s =
3653 container_of(h, struct kmem_cache, list);
3654
3655 err = sysfs_slab_add(s);
3656 BUG_ON(err);
3657 }
Christoph Lameter81819f02007-05-06 14:49:36 -07003658
3659 while (alias_list) {
3660 struct saved_alias *al = alias_list;
3661
3662 alias_list = alias_list->next;
3663 err = sysfs_slab_alias(al->s, al->name);
3664 BUG_ON(err);
3665 kfree(al);
3666 }
3667
3668 resiliency_test();
3669 return 0;
3670}
3671
3672__initcall(slab_sysfs_init);
Christoph Lameter81819f02007-05-06 14:49:36 -07003673#endif