blob: 5d895d44c327f5544b290c8dffd48eb9ff1cde11 [file] [log] [blame]
Christoph Lameter81819f02007-05-06 14:49:36 -07001/*
2 * SLUB: A slab allocator that limits cache line use instead of queuing
3 * objects in per cpu and per node lists.
4 *
5 * The allocator synchronizes using per slab locks and only
6 * uses a centralized lock to manage a pool of partial slabs.
7 *
8 * (C) 2007 SGI, Christoph Lameter <clameter@sgi.com>
9 */
10
11#include <linux/mm.h>
12#include <linux/module.h>
13#include <linux/bit_spinlock.h>
14#include <linux/interrupt.h>
15#include <linux/bitops.h>
16#include <linux/slab.h>
17#include <linux/seq_file.h>
18#include <linux/cpu.h>
19#include <linux/cpuset.h>
20#include <linux/mempolicy.h>
21#include <linux/ctype.h>
22#include <linux/kallsyms.h>
23
24/*
25 * Lock order:
26 * 1. slab_lock(page)
27 * 2. slab->list_lock
28 *
29 * The slab_lock protects operations on the object of a particular
30 * slab and its metadata in the page struct. If the slab lock
31 * has been taken then no allocations nor frees can be performed
32 * on the objects in the slab nor can the slab be added or removed
33 * from the partial or full lists since this would mean modifying
34 * the page_struct of the slab.
35 *
36 * The list_lock protects the partial and full list on each node and
37 * the partial slab counter. If taken then no new slabs may be added or
38 * removed from the lists nor make the number of partial slabs be modified.
39 * (Note that the total number of slabs is an atomic value that may be
40 * modified without taking the list lock).
41 *
42 * The list_lock is a centralized lock and thus we avoid taking it as
43 * much as possible. As long as SLUB does not have to handle partial
44 * slabs, operations can continue without any centralized lock. F.e.
45 * allocating a long series of objects that fill up slabs does not require
46 * the list lock.
47 *
48 * The lock order is sometimes inverted when we are trying to get a slab
49 * off a list. We take the list_lock and then look for a page on the list
50 * to use. While we do that objects in the slabs may be freed. We can
51 * only operate on the slab if we have also taken the slab_lock. So we use
52 * a slab_trylock() on the slab. If trylock was successful then no frees
53 * can occur anymore and we can use the slab for allocations etc. If the
54 * slab_trylock() does not succeed then frees are in progress in the slab and
55 * we must stay away from it for a while since we may cause a bouncing
56 * cacheline if we try to acquire the lock. So go onto the next slab.
57 * If all pages are busy then we may allocate a new slab instead of reusing
58 * a partial slab. A new slab has noone operating on it and thus there is
59 * no danger of cacheline contention.
60 *
61 * Interrupts are disabled during allocation and deallocation in order to
62 * make the slab allocator safe to use in the context of an irq. In addition
63 * interrupts are disabled to ensure that the processor does not change
64 * while handling per_cpu slabs, due to kernel preemption.
65 *
66 * SLUB assigns one slab for allocation to each processor.
67 * Allocations only occur from these slabs called cpu slabs.
68 *
Christoph Lameter672bba32007-05-09 02:32:39 -070069 * Slabs with free elements are kept on a partial list and during regular
70 * operations no list for full slabs is used. If an object in a full slab is
Christoph Lameter81819f02007-05-06 14:49:36 -070071 * freed then the slab will show up again on the partial lists.
Christoph Lameter672bba32007-05-09 02:32:39 -070072 * We track full slabs for debugging purposes though because otherwise we
73 * cannot scan all objects.
Christoph Lameter81819f02007-05-06 14:49:36 -070074 *
75 * Slabs are freed when they become empty. Teardown and setup is
76 * minimal so we rely on the page allocators per cpu caches for
77 * fast frees and allocs.
78 *
79 * Overloading of page flags that are otherwise used for LRU management.
80 *
Christoph Lameter4b6f0752007-05-16 22:10:53 -070081 * PageActive The slab is frozen and exempt from list processing.
82 * This means that the slab is dedicated to a purpose
83 * such as satisfying allocations for a specific
84 * processor. Objects may be freed in the slab while
85 * it is frozen but slab_free will then skip the usual
86 * list operations. It is up to the processor holding
87 * the slab to integrate the slab into the slab lists
88 * when the slab is no longer needed.
89 *
90 * One use of this flag is to mark slabs that are
91 * used for allocations. Then such a slab becomes a cpu
92 * slab. The cpu slab may be equipped with an additional
Christoph Lameterdfb4f092007-10-16 01:26:05 -070093 * freelist that allows lockless access to
Christoph Lameter894b8782007-05-10 03:15:16 -070094 * free objects in addition to the regular freelist
95 * that requires the slab lock.
Christoph Lameter81819f02007-05-06 14:49:36 -070096 *
97 * PageError Slab requires special handling due to debug
98 * options set. This moves slab handling out of
Christoph Lameter894b8782007-05-10 03:15:16 -070099 * the fast path and disables lockless freelists.
Christoph Lameter81819f02007-05-06 14:49:36 -0700100 */
101
Christoph Lameter5577bd82007-05-16 22:10:56 -0700102#define FROZEN (1 << PG_active)
103
104#ifdef CONFIG_SLUB_DEBUG
105#define SLABDEBUG (1 << PG_error)
106#else
107#define SLABDEBUG 0
108#endif
109
Christoph Lameter4b6f0752007-05-16 22:10:53 -0700110static inline int SlabFrozen(struct page *page)
111{
Christoph Lameter5577bd82007-05-16 22:10:56 -0700112 return page->flags & FROZEN;
Christoph Lameter4b6f0752007-05-16 22:10:53 -0700113}
114
115static inline void SetSlabFrozen(struct page *page)
116{
Christoph Lameter5577bd82007-05-16 22:10:56 -0700117 page->flags |= FROZEN;
Christoph Lameter4b6f0752007-05-16 22:10:53 -0700118}
119
120static inline void ClearSlabFrozen(struct page *page)
121{
Christoph Lameter5577bd82007-05-16 22:10:56 -0700122 page->flags &= ~FROZEN;
Christoph Lameter4b6f0752007-05-16 22:10:53 -0700123}
124
Christoph Lameter35e5d7e2007-05-09 02:32:42 -0700125static inline int SlabDebug(struct page *page)
126{
Christoph Lameter5577bd82007-05-16 22:10:56 -0700127 return page->flags & SLABDEBUG;
Christoph Lameter35e5d7e2007-05-09 02:32:42 -0700128}
129
130static inline void SetSlabDebug(struct page *page)
131{
Christoph Lameter5577bd82007-05-16 22:10:56 -0700132 page->flags |= SLABDEBUG;
Christoph Lameter35e5d7e2007-05-09 02:32:42 -0700133}
134
135static inline void ClearSlabDebug(struct page *page)
136{
Christoph Lameter5577bd82007-05-16 22:10:56 -0700137 page->flags &= ~SLABDEBUG;
Christoph Lameter35e5d7e2007-05-09 02:32:42 -0700138}
139
Christoph Lameter81819f02007-05-06 14:49:36 -0700140/*
141 * Issues still to be resolved:
142 *
Christoph Lameter81819f02007-05-06 14:49:36 -0700143 * - Support PAGE_ALLOC_DEBUG. Should be easy to do.
144 *
Christoph Lameter81819f02007-05-06 14:49:36 -0700145 * - Variable sizing of the per node arrays
146 */
147
148/* Enable to test recovery from slab corruption on boot */
149#undef SLUB_RESILIENCY_TEST
150
151#if PAGE_SHIFT <= 12
152
153/*
154 * Small page size. Make sure that we do not fragment memory
155 */
156#define DEFAULT_MAX_ORDER 1
157#define DEFAULT_MIN_OBJECTS 4
158
159#else
160
161/*
162 * Large page machines are customarily able to handle larger
163 * page orders.
164 */
165#define DEFAULT_MAX_ORDER 2
166#define DEFAULT_MIN_OBJECTS 8
167
168#endif
169
170/*
Christoph Lameter2086d262007-05-06 14:49:46 -0700171 * Mininum number of partial slabs. These will be left on the partial
172 * lists even if they are empty. kmem_cache_shrink may reclaim them.
173 */
Christoph Lametere95eed52007-05-06 14:49:44 -0700174#define MIN_PARTIAL 2
175
Christoph Lameter2086d262007-05-06 14:49:46 -0700176/*
177 * Maximum number of desirable partial slabs.
178 * The existence of more partial slabs makes kmem_cache_shrink
179 * sort the partial list by the number of objects in the.
180 */
181#define MAX_PARTIAL 10
182
Christoph Lameter81819f02007-05-06 14:49:36 -0700183#define DEBUG_DEFAULT_FLAGS (SLAB_DEBUG_FREE | SLAB_RED_ZONE | \
184 SLAB_POISON | SLAB_STORE_USER)
Christoph Lameter672bba32007-05-09 02:32:39 -0700185
Christoph Lameter81819f02007-05-06 14:49:36 -0700186/*
187 * Set of flags that will prevent slab merging
188 */
189#define SLUB_NEVER_MERGE (SLAB_RED_ZONE | SLAB_POISON | SLAB_STORE_USER | \
190 SLAB_TRACE | SLAB_DESTROY_BY_RCU)
191
192#define SLUB_MERGE_SAME (SLAB_DEBUG_FREE | SLAB_RECLAIM_ACCOUNT | \
193 SLAB_CACHE_DMA)
194
195#ifndef ARCH_KMALLOC_MINALIGN
Christoph Lameter47bfdc02007-05-06 14:49:37 -0700196#define ARCH_KMALLOC_MINALIGN __alignof__(unsigned long long)
Christoph Lameter81819f02007-05-06 14:49:36 -0700197#endif
198
199#ifndef ARCH_SLAB_MINALIGN
Christoph Lameter47bfdc02007-05-06 14:49:37 -0700200#define ARCH_SLAB_MINALIGN __alignof__(unsigned long long)
Christoph Lameter81819f02007-05-06 14:49:36 -0700201#endif
202
203/* Internal SLUB flags */
Christoph Lameter1ceef402007-08-07 15:11:48 -0700204#define __OBJECT_POISON 0x80000000 /* Poison object */
205#define __SYSFS_ADD_DEFERRED 0x40000000 /* Not yet visible via sysfs */
Christoph Lameter81819f02007-05-06 14:49:36 -0700206
Christoph Lameter65c02d42007-05-09 02:32:35 -0700207/* Not all arches define cache_line_size */
208#ifndef cache_line_size
209#define cache_line_size() L1_CACHE_BYTES
210#endif
211
Christoph Lameter81819f02007-05-06 14:49:36 -0700212static int kmem_size = sizeof(struct kmem_cache);
213
214#ifdef CONFIG_SMP
215static struct notifier_block slab_notifier;
216#endif
217
218static enum {
219 DOWN, /* No slab functionality available */
220 PARTIAL, /* kmem_cache_open() works but kmalloc does not */
Christoph Lameter672bba32007-05-09 02:32:39 -0700221 UP, /* Everything works but does not show up in sysfs */
Christoph Lameter81819f02007-05-06 14:49:36 -0700222 SYSFS /* Sysfs up */
223} slab_state = DOWN;
224
225/* A list of all slab caches on the system */
226static DECLARE_RWSEM(slub_lock);
Adrian Bunk5af328a2007-07-17 04:03:27 -0700227static LIST_HEAD(slab_caches);
Christoph Lameter81819f02007-05-06 14:49:36 -0700228
Christoph Lameter02cbc872007-05-09 02:32:43 -0700229/*
230 * Tracking user of a slab.
231 */
232struct track {
233 void *addr; /* Called from address */
234 int cpu; /* Was running on cpu */
235 int pid; /* Pid context */
236 unsigned long when; /* When did the operation occur */
237};
238
239enum track_item { TRACK_ALLOC, TRACK_FREE };
240
Christoph Lameter41ecc552007-05-09 02:32:44 -0700241#if defined(CONFIG_SYSFS) && defined(CONFIG_SLUB_DEBUG)
Christoph Lameter81819f02007-05-06 14:49:36 -0700242static int sysfs_slab_add(struct kmem_cache *);
243static int sysfs_slab_alias(struct kmem_cache *, const char *);
244static void sysfs_slab_remove(struct kmem_cache *);
245#else
Christoph Lameter0c710012007-07-17 04:03:24 -0700246static inline int sysfs_slab_add(struct kmem_cache *s) { return 0; }
247static inline int sysfs_slab_alias(struct kmem_cache *s, const char *p)
248 { return 0; }
249static inline void sysfs_slab_remove(struct kmem_cache *s) {}
Christoph Lameter81819f02007-05-06 14:49:36 -0700250#endif
251
252/********************************************************************
253 * Core slab cache functions
254 *******************************************************************/
255
256int slab_is_available(void)
257{
258 return slab_state >= UP;
259}
260
261static inline struct kmem_cache_node *get_node(struct kmem_cache *s, int node)
262{
263#ifdef CONFIG_NUMA
264 return s->node[node];
265#else
266 return &s->local_node;
267#endif
268}
269
Christoph Lameterdfb4f092007-10-16 01:26:05 -0700270static inline struct kmem_cache_cpu *get_cpu_slab(struct kmem_cache *s, int cpu)
271{
272 return &s->cpu_slab[cpu];
273}
274
Christoph Lameter02cbc872007-05-09 02:32:43 -0700275static inline int check_valid_pointer(struct kmem_cache *s,
276 struct page *page, const void *object)
277{
278 void *base;
279
280 if (!object)
281 return 1;
282
283 base = page_address(page);
284 if (object < base || object >= base + s->objects * s->size ||
285 (object - base) % s->size) {
286 return 0;
287 }
288
289 return 1;
290}
291
Christoph Lameter81819f02007-05-06 14:49:36 -0700292/*
Christoph Lameter7656c722007-05-09 02:32:40 -0700293 * Slow version of get and set free pointer.
294 *
295 * This version requires touching the cache lines of kmem_cache which
296 * we avoid to do in the fast alloc free paths. There we obtain the offset
297 * from the page struct.
298 */
299static inline void *get_freepointer(struct kmem_cache *s, void *object)
300{
301 return *(void **)(object + s->offset);
302}
303
304static inline void set_freepointer(struct kmem_cache *s, void *object, void *fp)
305{
306 *(void **)(object + s->offset) = fp;
307}
308
309/* Loop over all objects in a slab */
310#define for_each_object(__p, __s, __addr) \
311 for (__p = (__addr); __p < (__addr) + (__s)->objects * (__s)->size;\
312 __p += (__s)->size)
313
314/* Scan freelist */
315#define for_each_free_object(__p, __s, __free) \
316 for (__p = (__free); __p; __p = get_freepointer((__s), __p))
317
318/* Determine object index from a given position */
319static inline int slab_index(void *p, struct kmem_cache *s, void *addr)
320{
321 return (p - addr) / s->size;
322}
323
Christoph Lameter41ecc552007-05-09 02:32:44 -0700324#ifdef CONFIG_SLUB_DEBUG
325/*
326 * Debug settings:
327 */
Christoph Lameterf0630ff2007-07-15 23:38:14 -0700328#ifdef CONFIG_SLUB_DEBUG_ON
329static int slub_debug = DEBUG_DEFAULT_FLAGS;
330#else
Christoph Lameter41ecc552007-05-09 02:32:44 -0700331static int slub_debug;
Christoph Lameterf0630ff2007-07-15 23:38:14 -0700332#endif
Christoph Lameter41ecc552007-05-09 02:32:44 -0700333
334static char *slub_debug_slabs;
335
Christoph Lameter7656c722007-05-09 02:32:40 -0700336/*
Christoph Lameter81819f02007-05-06 14:49:36 -0700337 * Object debugging
338 */
339static void print_section(char *text, u8 *addr, unsigned int length)
340{
341 int i, offset;
342 int newline = 1;
343 char ascii[17];
344
345 ascii[16] = 0;
346
347 for (i = 0; i < length; i++) {
348 if (newline) {
Christoph Lameter24922682007-07-17 04:03:18 -0700349 printk(KERN_ERR "%8s 0x%p: ", text, addr + i);
Christoph Lameter81819f02007-05-06 14:49:36 -0700350 newline = 0;
351 }
352 printk(" %02x", addr[i]);
353 offset = i % 16;
354 ascii[offset] = isgraph(addr[i]) ? addr[i] : '.';
355 if (offset == 15) {
356 printk(" %s\n",ascii);
357 newline = 1;
358 }
359 }
360 if (!newline) {
361 i %= 16;
362 while (i < 16) {
363 printk(" ");
364 ascii[i] = ' ';
365 i++;
366 }
367 printk(" %s\n", ascii);
368 }
369}
370
Christoph Lameter81819f02007-05-06 14:49:36 -0700371static struct track *get_track(struct kmem_cache *s, void *object,
372 enum track_item alloc)
373{
374 struct track *p;
375
376 if (s->offset)
377 p = object + s->offset + sizeof(void *);
378 else
379 p = object + s->inuse;
380
381 return p + alloc;
382}
383
384static void set_track(struct kmem_cache *s, void *object,
385 enum track_item alloc, void *addr)
386{
387 struct track *p;
388
389 if (s->offset)
390 p = object + s->offset + sizeof(void *);
391 else
392 p = object + s->inuse;
393
394 p += alloc;
395 if (addr) {
396 p->addr = addr;
397 p->cpu = smp_processor_id();
398 p->pid = current ? current->pid : -1;
399 p->when = jiffies;
400 } else
401 memset(p, 0, sizeof(struct track));
402}
403
Christoph Lameter81819f02007-05-06 14:49:36 -0700404static void init_tracking(struct kmem_cache *s, void *object)
405{
Christoph Lameter24922682007-07-17 04:03:18 -0700406 if (!(s->flags & SLAB_STORE_USER))
407 return;
408
409 set_track(s, object, TRACK_FREE, NULL);
410 set_track(s, object, TRACK_ALLOC, NULL);
Christoph Lameter81819f02007-05-06 14:49:36 -0700411}
412
413static void print_track(const char *s, struct track *t)
414{
415 if (!t->addr)
416 return;
417
Christoph Lameter24922682007-07-17 04:03:18 -0700418 printk(KERN_ERR "INFO: %s in ", s);
Christoph Lameter81819f02007-05-06 14:49:36 -0700419 __print_symbol("%s", (unsigned long)t->addr);
Christoph Lameter24922682007-07-17 04:03:18 -0700420 printk(" age=%lu cpu=%u pid=%d\n", jiffies - t->when, t->cpu, t->pid);
Christoph Lameter81819f02007-05-06 14:49:36 -0700421}
422
Christoph Lameter24922682007-07-17 04:03:18 -0700423static void print_tracking(struct kmem_cache *s, void *object)
424{
425 if (!(s->flags & SLAB_STORE_USER))
426 return;
427
428 print_track("Allocated", get_track(s, object, TRACK_ALLOC));
429 print_track("Freed", get_track(s, object, TRACK_FREE));
430}
431
432static void print_page_info(struct page *page)
433{
434 printk(KERN_ERR "INFO: Slab 0x%p used=%u fp=0x%p flags=0x%04lx\n",
435 page, page->inuse, page->freelist, page->flags);
436
437}
438
439static void slab_bug(struct kmem_cache *s, char *fmt, ...)
440{
441 va_list args;
442 char buf[100];
443
444 va_start(args, fmt);
445 vsnprintf(buf, sizeof(buf), fmt, args);
446 va_end(args);
447 printk(KERN_ERR "========================================"
448 "=====================================\n");
449 printk(KERN_ERR "BUG %s: %s\n", s->name, buf);
450 printk(KERN_ERR "----------------------------------------"
451 "-------------------------------------\n\n");
452}
453
454static void slab_fix(struct kmem_cache *s, char *fmt, ...)
455{
456 va_list args;
457 char buf[100];
458
459 va_start(args, fmt);
460 vsnprintf(buf, sizeof(buf), fmt, args);
461 va_end(args);
462 printk(KERN_ERR "FIX %s: %s\n", s->name, buf);
463}
464
465static void print_trailer(struct kmem_cache *s, struct page *page, u8 *p)
Christoph Lameter81819f02007-05-06 14:49:36 -0700466{
467 unsigned int off; /* Offset of last byte */
Christoph Lameter24922682007-07-17 04:03:18 -0700468 u8 *addr = page_address(page);
469
470 print_tracking(s, p);
471
472 print_page_info(page);
473
474 printk(KERN_ERR "INFO: Object 0x%p @offset=%tu fp=0x%p\n\n",
475 p, p - addr, get_freepointer(s, p));
476
477 if (p > addr + 16)
478 print_section("Bytes b4", p - 16, 16);
479
480 print_section("Object", p, min(s->objsize, 128));
Christoph Lameter81819f02007-05-06 14:49:36 -0700481
482 if (s->flags & SLAB_RED_ZONE)
483 print_section("Redzone", p + s->objsize,
484 s->inuse - s->objsize);
485
Christoph Lameter81819f02007-05-06 14:49:36 -0700486 if (s->offset)
487 off = s->offset + sizeof(void *);
488 else
489 off = s->inuse;
490
Christoph Lameter24922682007-07-17 04:03:18 -0700491 if (s->flags & SLAB_STORE_USER)
Christoph Lameter81819f02007-05-06 14:49:36 -0700492 off += 2 * sizeof(struct track);
Christoph Lameter81819f02007-05-06 14:49:36 -0700493
494 if (off != s->size)
495 /* Beginning of the filler is the free pointer */
Christoph Lameter24922682007-07-17 04:03:18 -0700496 print_section("Padding", p + off, s->size - off);
497
498 dump_stack();
Christoph Lameter81819f02007-05-06 14:49:36 -0700499}
500
501static void object_err(struct kmem_cache *s, struct page *page,
502 u8 *object, char *reason)
503{
Christoph Lameter24922682007-07-17 04:03:18 -0700504 slab_bug(s, reason);
505 print_trailer(s, page, object);
Christoph Lameter81819f02007-05-06 14:49:36 -0700506}
507
Christoph Lameter24922682007-07-17 04:03:18 -0700508static void slab_err(struct kmem_cache *s, struct page *page, char *fmt, ...)
Christoph Lameter81819f02007-05-06 14:49:36 -0700509{
510 va_list args;
511 char buf[100];
512
Christoph Lameter24922682007-07-17 04:03:18 -0700513 va_start(args, fmt);
514 vsnprintf(buf, sizeof(buf), fmt, args);
Christoph Lameter81819f02007-05-06 14:49:36 -0700515 va_end(args);
Christoph Lameter24922682007-07-17 04:03:18 -0700516 slab_bug(s, fmt);
517 print_page_info(page);
Christoph Lameter81819f02007-05-06 14:49:36 -0700518 dump_stack();
519}
520
521static void init_object(struct kmem_cache *s, void *object, int active)
522{
523 u8 *p = object;
524
525 if (s->flags & __OBJECT_POISON) {
526 memset(p, POISON_FREE, s->objsize - 1);
527 p[s->objsize -1] = POISON_END;
528 }
529
530 if (s->flags & SLAB_RED_ZONE)
531 memset(p + s->objsize,
532 active ? SLUB_RED_ACTIVE : SLUB_RED_INACTIVE,
533 s->inuse - s->objsize);
534}
535
Christoph Lameter24922682007-07-17 04:03:18 -0700536static u8 *check_bytes(u8 *start, unsigned int value, unsigned int bytes)
Christoph Lameter81819f02007-05-06 14:49:36 -0700537{
538 while (bytes) {
539 if (*start != (u8)value)
Christoph Lameter24922682007-07-17 04:03:18 -0700540 return start;
Christoph Lameter81819f02007-05-06 14:49:36 -0700541 start++;
542 bytes--;
543 }
Christoph Lameter24922682007-07-17 04:03:18 -0700544 return NULL;
545}
546
547static void restore_bytes(struct kmem_cache *s, char *message, u8 data,
548 void *from, void *to)
549{
550 slab_fix(s, "Restoring 0x%p-0x%p=0x%x\n", from, to - 1, data);
551 memset(from, data, to - from);
552}
553
554static int check_bytes_and_report(struct kmem_cache *s, struct page *page,
555 u8 *object, char *what,
556 u8* start, unsigned int value, unsigned int bytes)
557{
558 u8 *fault;
559 u8 *end;
560
561 fault = check_bytes(start, value, bytes);
562 if (!fault)
563 return 1;
564
565 end = start + bytes;
566 while (end > fault && end[-1] == value)
567 end--;
568
569 slab_bug(s, "%s overwritten", what);
570 printk(KERN_ERR "INFO: 0x%p-0x%p. First byte 0x%x instead of 0x%x\n",
571 fault, end - 1, fault[0], value);
572 print_trailer(s, page, object);
573
574 restore_bytes(s, what, value, fault, end);
575 return 0;
Christoph Lameter81819f02007-05-06 14:49:36 -0700576}
577
Christoph Lameter81819f02007-05-06 14:49:36 -0700578/*
579 * Object layout:
580 *
581 * object address
582 * Bytes of the object to be managed.
583 * If the freepointer may overlay the object then the free
584 * pointer is the first word of the object.
Christoph Lameter672bba32007-05-09 02:32:39 -0700585 *
Christoph Lameter81819f02007-05-06 14:49:36 -0700586 * Poisoning uses 0x6b (POISON_FREE) and the last byte is
587 * 0xa5 (POISON_END)
588 *
589 * object + s->objsize
590 * Padding to reach word boundary. This is also used for Redzoning.
Christoph Lameter672bba32007-05-09 02:32:39 -0700591 * Padding is extended by another word if Redzoning is enabled and
592 * objsize == inuse.
593 *
Christoph Lameter81819f02007-05-06 14:49:36 -0700594 * We fill with 0xbb (RED_INACTIVE) for inactive objects and with
595 * 0xcc (RED_ACTIVE) for objects in use.
596 *
597 * object + s->inuse
Christoph Lameter672bba32007-05-09 02:32:39 -0700598 * Meta data starts here.
599 *
Christoph Lameter81819f02007-05-06 14:49:36 -0700600 * A. Free pointer (if we cannot overwrite object on free)
601 * B. Tracking data for SLAB_STORE_USER
Christoph Lameter672bba32007-05-09 02:32:39 -0700602 * C. Padding to reach required alignment boundary or at mininum
603 * one word if debuggin is on to be able to detect writes
604 * before the word boundary.
605 *
606 * Padding is done using 0x5a (POISON_INUSE)
Christoph Lameter81819f02007-05-06 14:49:36 -0700607 *
608 * object + s->size
Christoph Lameter672bba32007-05-09 02:32:39 -0700609 * Nothing is used beyond s->size.
Christoph Lameter81819f02007-05-06 14:49:36 -0700610 *
Christoph Lameter672bba32007-05-09 02:32:39 -0700611 * If slabcaches are merged then the objsize and inuse boundaries are mostly
612 * ignored. And therefore no slab options that rely on these boundaries
Christoph Lameter81819f02007-05-06 14:49:36 -0700613 * may be used with merged slabcaches.
614 */
615
Christoph Lameter81819f02007-05-06 14:49:36 -0700616static int check_pad_bytes(struct kmem_cache *s, struct page *page, u8 *p)
617{
618 unsigned long off = s->inuse; /* The end of info */
619
620 if (s->offset)
621 /* Freepointer is placed after the object. */
622 off += sizeof(void *);
623
624 if (s->flags & SLAB_STORE_USER)
625 /* We also have user information there */
626 off += 2 * sizeof(struct track);
627
628 if (s->size == off)
629 return 1;
630
Christoph Lameter24922682007-07-17 04:03:18 -0700631 return check_bytes_and_report(s, page, p, "Object padding",
632 p + off, POISON_INUSE, s->size - off);
Christoph Lameter81819f02007-05-06 14:49:36 -0700633}
634
635static int slab_pad_check(struct kmem_cache *s, struct page *page)
636{
Christoph Lameter24922682007-07-17 04:03:18 -0700637 u8 *start;
638 u8 *fault;
639 u8 *end;
640 int length;
641 int remainder;
Christoph Lameter81819f02007-05-06 14:49:36 -0700642
643 if (!(s->flags & SLAB_POISON))
644 return 1;
645
Christoph Lameter24922682007-07-17 04:03:18 -0700646 start = page_address(page);
647 end = start + (PAGE_SIZE << s->order);
Christoph Lameter81819f02007-05-06 14:49:36 -0700648 length = s->objects * s->size;
Christoph Lameter24922682007-07-17 04:03:18 -0700649 remainder = end - (start + length);
Christoph Lameter81819f02007-05-06 14:49:36 -0700650 if (!remainder)
651 return 1;
652
Christoph Lameter24922682007-07-17 04:03:18 -0700653 fault = check_bytes(start + length, POISON_INUSE, remainder);
654 if (!fault)
655 return 1;
656 while (end > fault && end[-1] == POISON_INUSE)
657 end--;
658
659 slab_err(s, page, "Padding overwritten. 0x%p-0x%p", fault, end - 1);
660 print_section("Padding", start, length);
661
662 restore_bytes(s, "slab padding", POISON_INUSE, start, end);
663 return 0;
Christoph Lameter81819f02007-05-06 14:49:36 -0700664}
665
666static int check_object(struct kmem_cache *s, struct page *page,
667 void *object, int active)
668{
669 u8 *p = object;
670 u8 *endobject = object + s->objsize;
671
672 if (s->flags & SLAB_RED_ZONE) {
673 unsigned int red =
674 active ? SLUB_RED_ACTIVE : SLUB_RED_INACTIVE;
675
Christoph Lameter24922682007-07-17 04:03:18 -0700676 if (!check_bytes_and_report(s, page, object, "Redzone",
677 endobject, red, s->inuse - s->objsize))
Christoph Lameter81819f02007-05-06 14:49:36 -0700678 return 0;
Christoph Lameter81819f02007-05-06 14:49:36 -0700679 } else {
Christoph Lameter24922682007-07-17 04:03:18 -0700680 if ((s->flags & SLAB_POISON) && s->objsize < s->inuse)
681 check_bytes_and_report(s, page, p, "Alignment padding", endobject,
682 POISON_INUSE, s->inuse - s->objsize);
Christoph Lameter81819f02007-05-06 14:49:36 -0700683 }
684
685 if (s->flags & SLAB_POISON) {
686 if (!active && (s->flags & __OBJECT_POISON) &&
Christoph Lameter24922682007-07-17 04:03:18 -0700687 (!check_bytes_and_report(s, page, p, "Poison", p,
688 POISON_FREE, s->objsize - 1) ||
689 !check_bytes_and_report(s, page, p, "Poison",
690 p + s->objsize -1, POISON_END, 1)))
Christoph Lameter81819f02007-05-06 14:49:36 -0700691 return 0;
Christoph Lameter81819f02007-05-06 14:49:36 -0700692 /*
693 * check_pad_bytes cleans up on its own.
694 */
695 check_pad_bytes(s, page, p);
696 }
697
698 if (!s->offset && active)
699 /*
700 * Object and freepointer overlap. Cannot check
701 * freepointer while object is allocated.
702 */
703 return 1;
704
705 /* Check free pointer validity */
706 if (!check_valid_pointer(s, page, get_freepointer(s, p))) {
707 object_err(s, page, p, "Freepointer corrupt");
708 /*
709 * No choice but to zap it and thus loose the remainder
710 * of the free objects in this slab. May cause
Christoph Lameter672bba32007-05-09 02:32:39 -0700711 * another error because the object count is now wrong.
Christoph Lameter81819f02007-05-06 14:49:36 -0700712 */
713 set_freepointer(s, p, NULL);
714 return 0;
715 }
716 return 1;
717}
718
719static int check_slab(struct kmem_cache *s, struct page *page)
720{
721 VM_BUG_ON(!irqs_disabled());
722
723 if (!PageSlab(page)) {
Christoph Lameter24922682007-07-17 04:03:18 -0700724 slab_err(s, page, "Not a valid slab page");
Christoph Lameter81819f02007-05-06 14:49:36 -0700725 return 0;
726 }
Christoph Lameter81819f02007-05-06 14:49:36 -0700727 if (page->inuse > s->objects) {
Christoph Lameter24922682007-07-17 04:03:18 -0700728 slab_err(s, page, "inuse %u > max %u",
729 s->name, page->inuse, s->objects);
Christoph Lameter81819f02007-05-06 14:49:36 -0700730 return 0;
731 }
732 /* Slab_pad_check fixes things up after itself */
733 slab_pad_check(s, page);
734 return 1;
735}
736
737/*
Christoph Lameter672bba32007-05-09 02:32:39 -0700738 * Determine if a certain object on a page is on the freelist. Must hold the
739 * slab lock to guarantee that the chains are in a consistent state.
Christoph Lameter81819f02007-05-06 14:49:36 -0700740 */
741static int on_freelist(struct kmem_cache *s, struct page *page, void *search)
742{
743 int nr = 0;
744 void *fp = page->freelist;
745 void *object = NULL;
746
747 while (fp && nr <= s->objects) {
748 if (fp == search)
749 return 1;
750 if (!check_valid_pointer(s, page, fp)) {
751 if (object) {
752 object_err(s, page, object,
753 "Freechain corrupt");
754 set_freepointer(s, object, NULL);
755 break;
756 } else {
Christoph Lameter24922682007-07-17 04:03:18 -0700757 slab_err(s, page, "Freepointer corrupt");
Christoph Lameter81819f02007-05-06 14:49:36 -0700758 page->freelist = NULL;
759 page->inuse = s->objects;
Christoph Lameter24922682007-07-17 04:03:18 -0700760 slab_fix(s, "Freelist cleared");
Christoph Lameter81819f02007-05-06 14:49:36 -0700761 return 0;
762 }
763 break;
764 }
765 object = fp;
766 fp = get_freepointer(s, object);
767 nr++;
768 }
769
770 if (page->inuse != s->objects - nr) {
Christoph Lameter70d71222007-05-06 14:49:47 -0700771 slab_err(s, page, "Wrong object count. Counter is %d but "
Christoph Lameter24922682007-07-17 04:03:18 -0700772 "counted were %d", page->inuse, s->objects - nr);
Christoph Lameter81819f02007-05-06 14:49:36 -0700773 page->inuse = s->objects - nr;
Christoph Lameter24922682007-07-17 04:03:18 -0700774 slab_fix(s, "Object count adjusted.");
Christoph Lameter81819f02007-05-06 14:49:36 -0700775 }
776 return search == NULL;
777}
778
Christoph Lameter3ec09742007-05-16 22:11:00 -0700779static void trace(struct kmem_cache *s, struct page *page, void *object, int alloc)
780{
781 if (s->flags & SLAB_TRACE) {
782 printk(KERN_INFO "TRACE %s %s 0x%p inuse=%d fp=0x%p\n",
783 s->name,
784 alloc ? "alloc" : "free",
785 object, page->inuse,
786 page->freelist);
787
788 if (!alloc)
789 print_section("Object", (void *)object, s->objsize);
790
791 dump_stack();
792 }
793}
794
Christoph Lameter643b1132007-05-06 14:49:42 -0700795/*
Christoph Lameter672bba32007-05-09 02:32:39 -0700796 * Tracking of fully allocated slabs for debugging purposes.
Christoph Lameter643b1132007-05-06 14:49:42 -0700797 */
Christoph Lametere95eed52007-05-06 14:49:44 -0700798static void add_full(struct kmem_cache_node *n, struct page *page)
Christoph Lameter643b1132007-05-06 14:49:42 -0700799{
Christoph Lameter643b1132007-05-06 14:49:42 -0700800 spin_lock(&n->list_lock);
801 list_add(&page->lru, &n->full);
802 spin_unlock(&n->list_lock);
803}
804
805static void remove_full(struct kmem_cache *s, struct page *page)
806{
807 struct kmem_cache_node *n;
808
809 if (!(s->flags & SLAB_STORE_USER))
810 return;
811
812 n = get_node(s, page_to_nid(page));
813
814 spin_lock(&n->list_lock);
815 list_del(&page->lru);
816 spin_unlock(&n->list_lock);
817}
818
Christoph Lameter3ec09742007-05-16 22:11:00 -0700819static void setup_object_debug(struct kmem_cache *s, struct page *page,
820 void *object)
821{
822 if (!(s->flags & (SLAB_STORE_USER|SLAB_RED_ZONE|__OBJECT_POISON)))
823 return;
824
825 init_object(s, object, 0);
826 init_tracking(s, object);
827}
828
829static int alloc_debug_processing(struct kmem_cache *s, struct page *page,
830 void *object, void *addr)
Christoph Lameter81819f02007-05-06 14:49:36 -0700831{
832 if (!check_slab(s, page))
833 goto bad;
834
835 if (object && !on_freelist(s, page, object)) {
Christoph Lameter24922682007-07-17 04:03:18 -0700836 object_err(s, page, object, "Object already allocated");
Christoph Lameter70d71222007-05-06 14:49:47 -0700837 goto bad;
Christoph Lameter81819f02007-05-06 14:49:36 -0700838 }
839
840 if (!check_valid_pointer(s, page, object)) {
841 object_err(s, page, object, "Freelist Pointer check fails");
Christoph Lameter70d71222007-05-06 14:49:47 -0700842 goto bad;
Christoph Lameter81819f02007-05-06 14:49:36 -0700843 }
844
Christoph Lameter3ec09742007-05-16 22:11:00 -0700845 if (object && !check_object(s, page, object, 0))
Christoph Lameter81819f02007-05-06 14:49:36 -0700846 goto bad;
Christoph Lameter81819f02007-05-06 14:49:36 -0700847
Christoph Lameter3ec09742007-05-16 22:11:00 -0700848 /* Success perform special debug activities for allocs */
849 if (s->flags & SLAB_STORE_USER)
850 set_track(s, object, TRACK_ALLOC, addr);
851 trace(s, page, object, 1);
852 init_object(s, object, 1);
Christoph Lameter81819f02007-05-06 14:49:36 -0700853 return 1;
Christoph Lameter3ec09742007-05-16 22:11:00 -0700854
Christoph Lameter81819f02007-05-06 14:49:36 -0700855bad:
856 if (PageSlab(page)) {
857 /*
858 * If this is a slab page then lets do the best we can
859 * to avoid issues in the future. Marking all objects
Christoph Lameter672bba32007-05-09 02:32:39 -0700860 * as used avoids touching the remaining objects.
Christoph Lameter81819f02007-05-06 14:49:36 -0700861 */
Christoph Lameter24922682007-07-17 04:03:18 -0700862 slab_fix(s, "Marking all objects used");
Christoph Lameter81819f02007-05-06 14:49:36 -0700863 page->inuse = s->objects;
864 page->freelist = NULL;
Christoph Lameter81819f02007-05-06 14:49:36 -0700865 }
866 return 0;
867}
868
Christoph Lameter3ec09742007-05-16 22:11:00 -0700869static int free_debug_processing(struct kmem_cache *s, struct page *page,
870 void *object, void *addr)
Christoph Lameter81819f02007-05-06 14:49:36 -0700871{
872 if (!check_slab(s, page))
873 goto fail;
874
875 if (!check_valid_pointer(s, page, object)) {
Christoph Lameter70d71222007-05-06 14:49:47 -0700876 slab_err(s, page, "Invalid object pointer 0x%p", object);
Christoph Lameter81819f02007-05-06 14:49:36 -0700877 goto fail;
878 }
879
880 if (on_freelist(s, page, object)) {
Christoph Lameter24922682007-07-17 04:03:18 -0700881 object_err(s, page, object, "Object already free");
Christoph Lameter81819f02007-05-06 14:49:36 -0700882 goto fail;
883 }
884
885 if (!check_object(s, page, object, 1))
886 return 0;
887
888 if (unlikely(s != page->slab)) {
889 if (!PageSlab(page))
Christoph Lameter70d71222007-05-06 14:49:47 -0700890 slab_err(s, page, "Attempt to free object(0x%p) "
891 "outside of slab", object);
Christoph Lameter81819f02007-05-06 14:49:36 -0700892 else
Christoph Lameter70d71222007-05-06 14:49:47 -0700893 if (!page->slab) {
Christoph Lameter81819f02007-05-06 14:49:36 -0700894 printk(KERN_ERR
Christoph Lameter70d71222007-05-06 14:49:47 -0700895 "SLUB <none>: no slab for object 0x%p.\n",
Christoph Lameter81819f02007-05-06 14:49:36 -0700896 object);
Christoph Lameter70d71222007-05-06 14:49:47 -0700897 dump_stack();
898 }
Christoph Lameter81819f02007-05-06 14:49:36 -0700899 else
Christoph Lameter24922682007-07-17 04:03:18 -0700900 object_err(s, page, object,
901 "page slab pointer corrupt.");
Christoph Lameter81819f02007-05-06 14:49:36 -0700902 goto fail;
903 }
Christoph Lameter3ec09742007-05-16 22:11:00 -0700904
905 /* Special debug activities for freeing objects */
906 if (!SlabFrozen(page) && !page->freelist)
907 remove_full(s, page);
908 if (s->flags & SLAB_STORE_USER)
909 set_track(s, object, TRACK_FREE, addr);
910 trace(s, page, object, 0);
911 init_object(s, object, 0);
Christoph Lameter81819f02007-05-06 14:49:36 -0700912 return 1;
Christoph Lameter3ec09742007-05-16 22:11:00 -0700913
Christoph Lameter81819f02007-05-06 14:49:36 -0700914fail:
Christoph Lameter24922682007-07-17 04:03:18 -0700915 slab_fix(s, "Object at 0x%p not freed", object);
Christoph Lameter81819f02007-05-06 14:49:36 -0700916 return 0;
917}
918
Christoph Lameter41ecc552007-05-09 02:32:44 -0700919static int __init setup_slub_debug(char *str)
920{
Christoph Lameterf0630ff2007-07-15 23:38:14 -0700921 slub_debug = DEBUG_DEFAULT_FLAGS;
922 if (*str++ != '=' || !*str)
923 /*
924 * No options specified. Switch on full debugging.
925 */
926 goto out;
Christoph Lameter41ecc552007-05-09 02:32:44 -0700927
928 if (*str == ',')
Christoph Lameterf0630ff2007-07-15 23:38:14 -0700929 /*
930 * No options but restriction on slabs. This means full
931 * debugging for slabs matching a pattern.
932 */
933 goto check_slabs;
934
935 slub_debug = 0;
936 if (*str == '-')
937 /*
938 * Switch off all debugging measures.
939 */
940 goto out;
941
942 /*
943 * Determine which debug features should be switched on
944 */
945 for ( ;*str && *str != ','; str++) {
946 switch (tolower(*str)) {
947 case 'f':
948 slub_debug |= SLAB_DEBUG_FREE;
949 break;
950 case 'z':
951 slub_debug |= SLAB_RED_ZONE;
952 break;
953 case 'p':
954 slub_debug |= SLAB_POISON;
955 break;
956 case 'u':
957 slub_debug |= SLAB_STORE_USER;
958 break;
959 case 't':
960 slub_debug |= SLAB_TRACE;
961 break;
962 default:
963 printk(KERN_ERR "slub_debug option '%c' "
964 "unknown. skipped\n",*str);
965 }
966 }
967
968check_slabs:
969 if (*str == ',')
Christoph Lameter41ecc552007-05-09 02:32:44 -0700970 slub_debug_slabs = str + 1;
Christoph Lameterf0630ff2007-07-15 23:38:14 -0700971out:
Christoph Lameter41ecc552007-05-09 02:32:44 -0700972 return 1;
973}
974
975__setup("slub_debug", setup_slub_debug);
976
Christoph Lameterba0268a2007-09-11 15:24:11 -0700977static unsigned long kmem_cache_flags(unsigned long objsize,
978 unsigned long flags, const char *name,
979 void (*ctor)(void *, struct kmem_cache *, unsigned long))
Christoph Lameter41ecc552007-05-09 02:32:44 -0700980{
981 /*
982 * The page->offset field is only 16 bit wide. This is an offset
983 * in units of words from the beginning of an object. If the slab
984 * size is bigger then we cannot move the free pointer behind the
985 * object anymore.
986 *
987 * On 32 bit platforms the limit is 256k. On 64bit platforms
988 * the limit is 512k.
989 *
Christoph Lameterc59def92007-05-16 22:10:50 -0700990 * Debugging or ctor may create a need to move the free
Christoph Lameter41ecc552007-05-09 02:32:44 -0700991 * pointer. Fail if this happens.
992 */
Christoph Lameterba0268a2007-09-11 15:24:11 -0700993 if (objsize >= 65535 * sizeof(void *)) {
994 BUG_ON(flags & (SLAB_RED_ZONE | SLAB_POISON |
Christoph Lameter41ecc552007-05-09 02:32:44 -0700995 SLAB_STORE_USER | SLAB_DESTROY_BY_RCU));
Christoph Lameterba0268a2007-09-11 15:24:11 -0700996 BUG_ON(ctor);
997 } else {
Christoph Lameter41ecc552007-05-09 02:32:44 -0700998 /*
999 * Enable debugging if selected on the kernel commandline.
1000 */
1001 if (slub_debug && (!slub_debug_slabs ||
Christoph Lameterba0268a2007-09-11 15:24:11 -07001002 strncmp(slub_debug_slabs, name,
Christoph Lameter41ecc552007-05-09 02:32:44 -07001003 strlen(slub_debug_slabs)) == 0))
Christoph Lameterba0268a2007-09-11 15:24:11 -07001004 flags |= slub_debug;
1005 }
1006
1007 return flags;
Christoph Lameter41ecc552007-05-09 02:32:44 -07001008}
1009#else
Christoph Lameter3ec09742007-05-16 22:11:00 -07001010static inline void setup_object_debug(struct kmem_cache *s,
1011 struct page *page, void *object) {}
Christoph Lameter41ecc552007-05-09 02:32:44 -07001012
Christoph Lameter3ec09742007-05-16 22:11:00 -07001013static inline int alloc_debug_processing(struct kmem_cache *s,
1014 struct page *page, void *object, void *addr) { return 0; }
Christoph Lameter41ecc552007-05-09 02:32:44 -07001015
Christoph Lameter3ec09742007-05-16 22:11:00 -07001016static inline int free_debug_processing(struct kmem_cache *s,
1017 struct page *page, void *object, void *addr) { return 0; }
Christoph Lameter41ecc552007-05-09 02:32:44 -07001018
Christoph Lameter41ecc552007-05-09 02:32:44 -07001019static inline int slab_pad_check(struct kmem_cache *s, struct page *page)
1020 { return 1; }
1021static inline int check_object(struct kmem_cache *s, struct page *page,
1022 void *object, int active) { return 1; }
Christoph Lameter3ec09742007-05-16 22:11:00 -07001023static inline void add_full(struct kmem_cache_node *n, struct page *page) {}
Christoph Lameterba0268a2007-09-11 15:24:11 -07001024static inline unsigned long kmem_cache_flags(unsigned long objsize,
1025 unsigned long flags, const char *name,
1026 void (*ctor)(void *, struct kmem_cache *, unsigned long))
1027{
1028 return flags;
1029}
Christoph Lameter41ecc552007-05-09 02:32:44 -07001030#define slub_debug 0
1031#endif
Christoph Lameter81819f02007-05-06 14:49:36 -07001032/*
1033 * Slab allocation and freeing
1034 */
1035static struct page *allocate_slab(struct kmem_cache *s, gfp_t flags, int node)
1036{
1037 struct page * page;
1038 int pages = 1 << s->order;
1039
1040 if (s->order)
1041 flags |= __GFP_COMP;
1042
1043 if (s->flags & SLAB_CACHE_DMA)
1044 flags |= SLUB_DMA;
1045
Mel Gormane12ba742007-10-16 01:25:52 -07001046 if (s->flags & SLAB_RECLAIM_ACCOUNT)
1047 flags |= __GFP_RECLAIMABLE;
1048
Christoph Lameter81819f02007-05-06 14:49:36 -07001049 if (node == -1)
1050 page = alloc_pages(flags, s->order);
1051 else
1052 page = alloc_pages_node(node, flags, s->order);
1053
1054 if (!page)
1055 return NULL;
1056
1057 mod_zone_page_state(page_zone(page),
1058 (s->flags & SLAB_RECLAIM_ACCOUNT) ?
1059 NR_SLAB_RECLAIMABLE : NR_SLAB_UNRECLAIMABLE,
1060 pages);
1061
1062 return page;
1063}
1064
1065static void setup_object(struct kmem_cache *s, struct page *page,
1066 void *object)
1067{
Christoph Lameter3ec09742007-05-16 22:11:00 -07001068 setup_object_debug(s, page, object);
Christoph Lameter4f104932007-05-06 14:50:17 -07001069 if (unlikely(s->ctor))
Christoph Lametera35afb82007-05-16 22:10:57 -07001070 s->ctor(object, s, 0);
Christoph Lameter81819f02007-05-06 14:49:36 -07001071}
1072
1073static struct page *new_slab(struct kmem_cache *s, gfp_t flags, int node)
1074{
1075 struct page *page;
1076 struct kmem_cache_node *n;
1077 void *start;
1078 void *end;
1079 void *last;
1080 void *p;
1081
Christoph Lameter6cb06222007-10-16 01:25:41 -07001082 BUG_ON(flags & GFP_SLAB_BUG_MASK);
Christoph Lameter81819f02007-05-06 14:49:36 -07001083
1084 if (flags & __GFP_WAIT)
1085 local_irq_enable();
1086
Christoph Lameter6cb06222007-10-16 01:25:41 -07001087 page = allocate_slab(s,
1088 flags & (GFP_RECLAIM_MASK | GFP_CONSTRAINT_MASK), node);
Christoph Lameter81819f02007-05-06 14:49:36 -07001089 if (!page)
1090 goto out;
1091
1092 n = get_node(s, page_to_nid(page));
1093 if (n)
1094 atomic_long_inc(&n->nr_slabs);
Christoph Lameter81819f02007-05-06 14:49:36 -07001095 page->slab = s;
1096 page->flags |= 1 << PG_slab;
1097 if (s->flags & (SLAB_DEBUG_FREE | SLAB_RED_ZONE | SLAB_POISON |
1098 SLAB_STORE_USER | SLAB_TRACE))
Christoph Lameter35e5d7e2007-05-09 02:32:42 -07001099 SetSlabDebug(page);
Christoph Lameter81819f02007-05-06 14:49:36 -07001100
1101 start = page_address(page);
1102 end = start + s->objects * s->size;
1103
1104 if (unlikely(s->flags & SLAB_POISON))
1105 memset(start, POISON_INUSE, PAGE_SIZE << s->order);
1106
1107 last = start;
Christoph Lameter7656c722007-05-09 02:32:40 -07001108 for_each_object(p, s, start) {
Christoph Lameter81819f02007-05-06 14:49:36 -07001109 setup_object(s, page, last);
1110 set_freepointer(s, last, p);
1111 last = p;
1112 }
1113 setup_object(s, page, last);
1114 set_freepointer(s, last, NULL);
1115
1116 page->freelist = start;
1117 page->inuse = 0;
1118out:
1119 if (flags & __GFP_WAIT)
1120 local_irq_disable();
1121 return page;
1122}
1123
1124static void __free_slab(struct kmem_cache *s, struct page *page)
1125{
1126 int pages = 1 << s->order;
1127
Christoph Lameterc59def92007-05-16 22:10:50 -07001128 if (unlikely(SlabDebug(page))) {
Christoph Lameter81819f02007-05-06 14:49:36 -07001129 void *p;
1130
1131 slab_pad_check(s, page);
Christoph Lameterc59def92007-05-16 22:10:50 -07001132 for_each_object(p, s, page_address(page))
Christoph Lameter81819f02007-05-06 14:49:36 -07001133 check_object(s, page, p, 0);
Peter Zijlstra2208b762007-07-26 20:54:34 +02001134 ClearSlabDebug(page);
Christoph Lameter81819f02007-05-06 14:49:36 -07001135 }
1136
1137 mod_zone_page_state(page_zone(page),
1138 (s->flags & SLAB_RECLAIM_ACCOUNT) ?
1139 NR_SLAB_RECLAIMABLE : NR_SLAB_UNRECLAIMABLE,
1140 - pages);
1141
Christoph Lameter81819f02007-05-06 14:49:36 -07001142 __free_pages(page, s->order);
1143}
1144
1145static void rcu_free_slab(struct rcu_head *h)
1146{
1147 struct page *page;
1148
1149 page = container_of((struct list_head *)h, struct page, lru);
1150 __free_slab(page->slab, page);
1151}
1152
1153static void free_slab(struct kmem_cache *s, struct page *page)
1154{
1155 if (unlikely(s->flags & SLAB_DESTROY_BY_RCU)) {
1156 /*
1157 * RCU free overloads the RCU head over the LRU
1158 */
1159 struct rcu_head *head = (void *)&page->lru;
1160
1161 call_rcu(head, rcu_free_slab);
1162 } else
1163 __free_slab(s, page);
1164}
1165
1166static void discard_slab(struct kmem_cache *s, struct page *page)
1167{
1168 struct kmem_cache_node *n = get_node(s, page_to_nid(page));
1169
1170 atomic_long_dec(&n->nr_slabs);
1171 reset_page_mapcount(page);
Christoph Lameter35e5d7e2007-05-09 02:32:42 -07001172 __ClearPageSlab(page);
Christoph Lameter81819f02007-05-06 14:49:36 -07001173 free_slab(s, page);
1174}
1175
1176/*
1177 * Per slab locking using the pagelock
1178 */
1179static __always_inline void slab_lock(struct page *page)
1180{
1181 bit_spin_lock(PG_locked, &page->flags);
1182}
1183
1184static __always_inline void slab_unlock(struct page *page)
1185{
1186 bit_spin_unlock(PG_locked, &page->flags);
1187}
1188
1189static __always_inline int slab_trylock(struct page *page)
1190{
1191 int rc = 1;
1192
1193 rc = bit_spin_trylock(PG_locked, &page->flags);
1194 return rc;
1195}
1196
1197/*
1198 * Management of partially allocated slabs
1199 */
Christoph Lametere95eed52007-05-06 14:49:44 -07001200static void add_partial_tail(struct kmem_cache_node *n, struct page *page)
Christoph Lameter81819f02007-05-06 14:49:36 -07001201{
Christoph Lametere95eed52007-05-06 14:49:44 -07001202 spin_lock(&n->list_lock);
1203 n->nr_partial++;
1204 list_add_tail(&page->lru, &n->partial);
1205 spin_unlock(&n->list_lock);
1206}
Christoph Lameter81819f02007-05-06 14:49:36 -07001207
Christoph Lametere95eed52007-05-06 14:49:44 -07001208static void add_partial(struct kmem_cache_node *n, struct page *page)
1209{
Christoph Lameter81819f02007-05-06 14:49:36 -07001210 spin_lock(&n->list_lock);
1211 n->nr_partial++;
1212 list_add(&page->lru, &n->partial);
1213 spin_unlock(&n->list_lock);
1214}
1215
1216static void remove_partial(struct kmem_cache *s,
1217 struct page *page)
1218{
1219 struct kmem_cache_node *n = get_node(s, page_to_nid(page));
1220
1221 spin_lock(&n->list_lock);
1222 list_del(&page->lru);
1223 n->nr_partial--;
1224 spin_unlock(&n->list_lock);
1225}
1226
1227/*
Christoph Lameter672bba32007-05-09 02:32:39 -07001228 * Lock slab and remove from the partial list.
Christoph Lameter81819f02007-05-06 14:49:36 -07001229 *
Christoph Lameter672bba32007-05-09 02:32:39 -07001230 * Must hold list_lock.
Christoph Lameter81819f02007-05-06 14:49:36 -07001231 */
Christoph Lameter4b6f0752007-05-16 22:10:53 -07001232static inline int lock_and_freeze_slab(struct kmem_cache_node *n, struct page *page)
Christoph Lameter81819f02007-05-06 14:49:36 -07001233{
1234 if (slab_trylock(page)) {
1235 list_del(&page->lru);
1236 n->nr_partial--;
Christoph Lameter4b6f0752007-05-16 22:10:53 -07001237 SetSlabFrozen(page);
Christoph Lameter81819f02007-05-06 14:49:36 -07001238 return 1;
1239 }
1240 return 0;
1241}
1242
1243/*
Christoph Lameter672bba32007-05-09 02:32:39 -07001244 * Try to allocate a partial slab from a specific node.
Christoph Lameter81819f02007-05-06 14:49:36 -07001245 */
1246static struct page *get_partial_node(struct kmem_cache_node *n)
1247{
1248 struct page *page;
1249
1250 /*
1251 * Racy check. If we mistakenly see no partial slabs then we
1252 * just allocate an empty slab. If we mistakenly try to get a
Christoph Lameter672bba32007-05-09 02:32:39 -07001253 * partial slab and there is none available then get_partials()
1254 * will return NULL.
Christoph Lameter81819f02007-05-06 14:49:36 -07001255 */
1256 if (!n || !n->nr_partial)
1257 return NULL;
1258
1259 spin_lock(&n->list_lock);
1260 list_for_each_entry(page, &n->partial, lru)
Christoph Lameter4b6f0752007-05-16 22:10:53 -07001261 if (lock_and_freeze_slab(n, page))
Christoph Lameter81819f02007-05-06 14:49:36 -07001262 goto out;
1263 page = NULL;
1264out:
1265 spin_unlock(&n->list_lock);
1266 return page;
1267}
1268
1269/*
Christoph Lameter672bba32007-05-09 02:32:39 -07001270 * Get a page from somewhere. Search in increasing NUMA distances.
Christoph Lameter81819f02007-05-06 14:49:36 -07001271 */
1272static struct page *get_any_partial(struct kmem_cache *s, gfp_t flags)
1273{
1274#ifdef CONFIG_NUMA
1275 struct zonelist *zonelist;
1276 struct zone **z;
1277 struct page *page;
1278
1279 /*
Christoph Lameter672bba32007-05-09 02:32:39 -07001280 * The defrag ratio allows a configuration of the tradeoffs between
1281 * inter node defragmentation and node local allocations. A lower
1282 * defrag_ratio increases the tendency to do local allocations
1283 * instead of attempting to obtain partial slabs from other nodes.
Christoph Lameter81819f02007-05-06 14:49:36 -07001284 *
Christoph Lameter672bba32007-05-09 02:32:39 -07001285 * If the defrag_ratio is set to 0 then kmalloc() always
1286 * returns node local objects. If the ratio is higher then kmalloc()
1287 * may return off node objects because partial slabs are obtained
1288 * from other nodes and filled up.
Christoph Lameter81819f02007-05-06 14:49:36 -07001289 *
1290 * If /sys/slab/xx/defrag_ratio is set to 100 (which makes
Christoph Lameter672bba32007-05-09 02:32:39 -07001291 * defrag_ratio = 1000) then every (well almost) allocation will
1292 * first attempt to defrag slab caches on other nodes. This means
1293 * scanning over all nodes to look for partial slabs which may be
1294 * expensive if we do it every time we are trying to find a slab
1295 * with available objects.
Christoph Lameter81819f02007-05-06 14:49:36 -07001296 */
1297 if (!s->defrag_ratio || get_cycles() % 1024 > s->defrag_ratio)
1298 return NULL;
1299
1300 zonelist = &NODE_DATA(slab_node(current->mempolicy))
1301 ->node_zonelists[gfp_zone(flags)];
1302 for (z = zonelist->zones; *z; z++) {
1303 struct kmem_cache_node *n;
1304
1305 n = get_node(s, zone_to_nid(*z));
1306
1307 if (n && cpuset_zone_allowed_hardwall(*z, flags) &&
Christoph Lametere95eed52007-05-06 14:49:44 -07001308 n->nr_partial > MIN_PARTIAL) {
Christoph Lameter81819f02007-05-06 14:49:36 -07001309 page = get_partial_node(n);
1310 if (page)
1311 return page;
1312 }
1313 }
1314#endif
1315 return NULL;
1316}
1317
1318/*
1319 * Get a partial page, lock it and return it.
1320 */
1321static struct page *get_partial(struct kmem_cache *s, gfp_t flags, int node)
1322{
1323 struct page *page;
1324 int searchnode = (node == -1) ? numa_node_id() : node;
1325
1326 page = get_partial_node(get_node(s, searchnode));
1327 if (page || (flags & __GFP_THISNODE))
1328 return page;
1329
1330 return get_any_partial(s, flags);
1331}
1332
1333/*
1334 * Move a page back to the lists.
1335 *
1336 * Must be called with the slab lock held.
1337 *
1338 * On exit the slab lock will have been dropped.
1339 */
Christoph Lameter4b6f0752007-05-16 22:10:53 -07001340static void unfreeze_slab(struct kmem_cache *s, struct page *page)
Christoph Lameter81819f02007-05-06 14:49:36 -07001341{
Christoph Lametere95eed52007-05-06 14:49:44 -07001342 struct kmem_cache_node *n = get_node(s, page_to_nid(page));
1343
Christoph Lameter4b6f0752007-05-16 22:10:53 -07001344 ClearSlabFrozen(page);
Christoph Lameter81819f02007-05-06 14:49:36 -07001345 if (page->inuse) {
Christoph Lametere95eed52007-05-06 14:49:44 -07001346
Christoph Lameter81819f02007-05-06 14:49:36 -07001347 if (page->freelist)
Christoph Lametere95eed52007-05-06 14:49:44 -07001348 add_partial(n, page);
Christoph Lameter35e5d7e2007-05-09 02:32:42 -07001349 else if (SlabDebug(page) && (s->flags & SLAB_STORE_USER))
Christoph Lametere95eed52007-05-06 14:49:44 -07001350 add_full(n, page);
Christoph Lameter81819f02007-05-06 14:49:36 -07001351 slab_unlock(page);
Christoph Lametere95eed52007-05-06 14:49:44 -07001352
Christoph Lameter81819f02007-05-06 14:49:36 -07001353 } else {
Christoph Lametere95eed52007-05-06 14:49:44 -07001354 if (n->nr_partial < MIN_PARTIAL) {
1355 /*
Christoph Lameter672bba32007-05-09 02:32:39 -07001356 * Adding an empty slab to the partial slabs in order
1357 * to avoid page allocator overhead. This slab needs
1358 * to come after the other slabs with objects in
1359 * order to fill them up. That way the size of the
1360 * partial list stays small. kmem_cache_shrink can
1361 * reclaim empty slabs from the partial list.
Christoph Lametere95eed52007-05-06 14:49:44 -07001362 */
1363 add_partial_tail(n, page);
1364 slab_unlock(page);
1365 } else {
1366 slab_unlock(page);
1367 discard_slab(s, page);
1368 }
Christoph Lameter81819f02007-05-06 14:49:36 -07001369 }
1370}
1371
1372/*
1373 * Remove the cpu slab
1374 */
Christoph Lameterdfb4f092007-10-16 01:26:05 -07001375static void deactivate_slab(struct kmem_cache *s, struct kmem_cache_cpu *c)
Christoph Lameter81819f02007-05-06 14:49:36 -07001376{
Christoph Lameterdfb4f092007-10-16 01:26:05 -07001377 struct page *page = c->page;
Christoph Lameter894b8782007-05-10 03:15:16 -07001378 /*
1379 * Merge cpu freelist into freelist. Typically we get here
1380 * because both freelists are empty. So this is unlikely
1381 * to occur.
1382 */
Christoph Lameterdfb4f092007-10-16 01:26:05 -07001383 while (unlikely(c->freelist)) {
Christoph Lameter894b8782007-05-10 03:15:16 -07001384 void **object;
1385
1386 /* Retrieve object from cpu_freelist */
Christoph Lameterdfb4f092007-10-16 01:26:05 -07001387 object = c->freelist;
Christoph Lameterb3fba8d2007-10-16 01:26:06 -07001388 c->freelist = c->freelist[c->offset];
Christoph Lameter894b8782007-05-10 03:15:16 -07001389
1390 /* And put onto the regular freelist */
Christoph Lameterb3fba8d2007-10-16 01:26:06 -07001391 object[c->offset] = page->freelist;
Christoph Lameter894b8782007-05-10 03:15:16 -07001392 page->freelist = object;
1393 page->inuse--;
1394 }
Christoph Lameterdfb4f092007-10-16 01:26:05 -07001395 c->page = NULL;
Christoph Lameter4b6f0752007-05-16 22:10:53 -07001396 unfreeze_slab(s, page);
Christoph Lameter81819f02007-05-06 14:49:36 -07001397}
1398
Christoph Lameterdfb4f092007-10-16 01:26:05 -07001399static inline void flush_slab(struct kmem_cache *s, struct kmem_cache_cpu *c)
Christoph Lameter81819f02007-05-06 14:49:36 -07001400{
Christoph Lameterdfb4f092007-10-16 01:26:05 -07001401 slab_lock(c->page);
1402 deactivate_slab(s, c);
Christoph Lameter81819f02007-05-06 14:49:36 -07001403}
1404
1405/*
1406 * Flush cpu slab.
1407 * Called from IPI handler with interrupts disabled.
1408 */
Christoph Lameter0c710012007-07-17 04:03:24 -07001409static inline void __flush_cpu_slab(struct kmem_cache *s, int cpu)
Christoph Lameter81819f02007-05-06 14:49:36 -07001410{
Christoph Lameterdfb4f092007-10-16 01:26:05 -07001411 struct kmem_cache_cpu *c = get_cpu_slab(s, cpu);
Christoph Lameter81819f02007-05-06 14:49:36 -07001412
Christoph Lameterdfb4f092007-10-16 01:26:05 -07001413 if (likely(c && c->page))
1414 flush_slab(s, c);
Christoph Lameter81819f02007-05-06 14:49:36 -07001415}
1416
1417static void flush_cpu_slab(void *d)
1418{
1419 struct kmem_cache *s = d;
Christoph Lameter81819f02007-05-06 14:49:36 -07001420
Christoph Lameterdfb4f092007-10-16 01:26:05 -07001421 __flush_cpu_slab(s, smp_processor_id());
Christoph Lameter81819f02007-05-06 14:49:36 -07001422}
1423
1424static void flush_all(struct kmem_cache *s)
1425{
1426#ifdef CONFIG_SMP
1427 on_each_cpu(flush_cpu_slab, s, 1, 1);
1428#else
1429 unsigned long flags;
1430
1431 local_irq_save(flags);
1432 flush_cpu_slab(s);
1433 local_irq_restore(flags);
1434#endif
1435}
1436
1437/*
Christoph Lameterdfb4f092007-10-16 01:26:05 -07001438 * Check if the objects in a per cpu structure fit numa
1439 * locality expectations.
1440 */
1441static inline int node_match(struct kmem_cache_cpu *c, int node)
1442{
1443#ifdef CONFIG_NUMA
1444 if (node != -1 && c->node != node)
1445 return 0;
1446#endif
1447 return 1;
1448}
1449
1450/*
Christoph Lameter894b8782007-05-10 03:15:16 -07001451 * Slow path. The lockless freelist is empty or we need to perform
1452 * debugging duties.
Christoph Lameter81819f02007-05-06 14:49:36 -07001453 *
Christoph Lameter894b8782007-05-10 03:15:16 -07001454 * Interrupts are disabled.
Christoph Lameter81819f02007-05-06 14:49:36 -07001455 *
Christoph Lameter894b8782007-05-10 03:15:16 -07001456 * Processing is still very fast if new objects have been freed to the
1457 * regular freelist. In that case we simply take over the regular freelist
1458 * as the lockless freelist and zap the regular freelist.
Christoph Lameter81819f02007-05-06 14:49:36 -07001459 *
Christoph Lameter894b8782007-05-10 03:15:16 -07001460 * If that is not working then we fall back to the partial lists. We take the
1461 * first element of the freelist as the object to allocate now and move the
1462 * rest of the freelist to the lockless freelist.
1463 *
1464 * And if we were unable to get a new slab from the partial slab lists then
1465 * we need to allocate a new slab. This is slowest path since we may sleep.
Christoph Lameter81819f02007-05-06 14:49:36 -07001466 */
Christoph Lameter894b8782007-05-10 03:15:16 -07001467static void *__slab_alloc(struct kmem_cache *s,
Christoph Lameterdfb4f092007-10-16 01:26:05 -07001468 gfp_t gfpflags, int node, void *addr, struct kmem_cache_cpu *c)
Christoph Lameter81819f02007-05-06 14:49:36 -07001469{
Christoph Lameter81819f02007-05-06 14:49:36 -07001470 void **object;
Christoph Lameterdfb4f092007-10-16 01:26:05 -07001471 struct page *new;
Christoph Lameter81819f02007-05-06 14:49:36 -07001472
Christoph Lameterdfb4f092007-10-16 01:26:05 -07001473 if (!c->page)
Christoph Lameter81819f02007-05-06 14:49:36 -07001474 goto new_slab;
1475
Christoph Lameterdfb4f092007-10-16 01:26:05 -07001476 slab_lock(c->page);
1477 if (unlikely(!node_match(c, node)))
Christoph Lameter81819f02007-05-06 14:49:36 -07001478 goto another_slab;
Christoph Lameter894b8782007-05-10 03:15:16 -07001479load_freelist:
Christoph Lameterdfb4f092007-10-16 01:26:05 -07001480 object = c->page->freelist;
Christoph Lameter81819f02007-05-06 14:49:36 -07001481 if (unlikely(!object))
1482 goto another_slab;
Christoph Lameterdfb4f092007-10-16 01:26:05 -07001483 if (unlikely(SlabDebug(c->page)))
Christoph Lameter81819f02007-05-06 14:49:36 -07001484 goto debug;
1485
Christoph Lameterdfb4f092007-10-16 01:26:05 -07001486 object = c->page->freelist;
Christoph Lameterb3fba8d2007-10-16 01:26:06 -07001487 c->freelist = object[c->offset];
Christoph Lameterdfb4f092007-10-16 01:26:05 -07001488 c->page->inuse = s->objects;
1489 c->page->freelist = NULL;
1490 c->node = page_to_nid(c->page);
1491 slab_unlock(c->page);
Christoph Lameter81819f02007-05-06 14:49:36 -07001492 return object;
1493
1494another_slab:
Christoph Lameterdfb4f092007-10-16 01:26:05 -07001495 deactivate_slab(s, c);
Christoph Lameter81819f02007-05-06 14:49:36 -07001496
1497new_slab:
Christoph Lameterdfb4f092007-10-16 01:26:05 -07001498 new = get_partial(s, gfpflags, node);
1499 if (new) {
1500 c->page = new;
Christoph Lameter894b8782007-05-10 03:15:16 -07001501 goto load_freelist;
Christoph Lameter81819f02007-05-06 14:49:36 -07001502 }
1503
Christoph Lameterdfb4f092007-10-16 01:26:05 -07001504 new = new_slab(s, gfpflags, node);
1505 if (new) {
1506 c = get_cpu_slab(s, smp_processor_id());
1507 if (c->page) {
Christoph Lameter81819f02007-05-06 14:49:36 -07001508 /*
Christoph Lameter672bba32007-05-09 02:32:39 -07001509 * Someone else populated the cpu_slab while we
1510 * enabled interrupts, or we have gotten scheduled
1511 * on another cpu. The page may not be on the
1512 * requested node even if __GFP_THISNODE was
1513 * specified. So we need to recheck.
Christoph Lameter81819f02007-05-06 14:49:36 -07001514 */
Christoph Lameterdfb4f092007-10-16 01:26:05 -07001515 if (node_match(c, node)) {
Christoph Lameter81819f02007-05-06 14:49:36 -07001516 /*
1517 * Current cpuslab is acceptable and we
1518 * want the current one since its cache hot
1519 */
Christoph Lameterdfb4f092007-10-16 01:26:05 -07001520 discard_slab(s, new);
1521 slab_lock(c->page);
Christoph Lameter894b8782007-05-10 03:15:16 -07001522 goto load_freelist;
Christoph Lameter81819f02007-05-06 14:49:36 -07001523 }
Christoph Lameter672bba32007-05-09 02:32:39 -07001524 /* New slab does not fit our expectations */
Christoph Lameterdfb4f092007-10-16 01:26:05 -07001525 flush_slab(s, c);
Christoph Lameter81819f02007-05-06 14:49:36 -07001526 }
Christoph Lameterdfb4f092007-10-16 01:26:05 -07001527 slab_lock(new);
1528 SetSlabFrozen(new);
1529 c->page = new;
Christoph Lameter4b6f0752007-05-16 22:10:53 -07001530 goto load_freelist;
Christoph Lameter81819f02007-05-06 14:49:36 -07001531 }
Christoph Lameter81819f02007-05-06 14:49:36 -07001532 return NULL;
1533debug:
Christoph Lameterdfb4f092007-10-16 01:26:05 -07001534 object = c->page->freelist;
1535 if (!alloc_debug_processing(s, c->page, object, addr))
Christoph Lameter81819f02007-05-06 14:49:36 -07001536 goto another_slab;
Christoph Lameter894b8782007-05-10 03:15:16 -07001537
Christoph Lameterdfb4f092007-10-16 01:26:05 -07001538 c->page->inuse++;
Christoph Lameterb3fba8d2007-10-16 01:26:06 -07001539 c->page->freelist = object[c->offset];
Christoph Lameterdfb4f092007-10-16 01:26:05 -07001540 slab_unlock(c->page);
Christoph Lameter894b8782007-05-10 03:15:16 -07001541 return object;
1542}
1543
1544/*
1545 * Inlined fastpath so that allocation functions (kmalloc, kmem_cache_alloc)
1546 * have the fastpath folded into their functions. So no function call
1547 * overhead for requests that can be satisfied on the fastpath.
1548 *
1549 * The fastpath works by first checking if the lockless freelist can be used.
1550 * If not then __slab_alloc is called for slow processing.
1551 *
1552 * Otherwise we can simply pick the next object from the lockless free list.
1553 */
1554static void __always_inline *slab_alloc(struct kmem_cache *s,
Christoph Lameterce15fea2007-07-17 04:03:28 -07001555 gfp_t gfpflags, int node, void *addr)
Christoph Lameter894b8782007-05-10 03:15:16 -07001556{
Christoph Lameter894b8782007-05-10 03:15:16 -07001557 void **object;
1558 unsigned long flags;
Christoph Lameterdfb4f092007-10-16 01:26:05 -07001559 struct kmem_cache_cpu *c;
Christoph Lameter894b8782007-05-10 03:15:16 -07001560
1561 local_irq_save(flags);
Christoph Lameterdfb4f092007-10-16 01:26:05 -07001562 c = get_cpu_slab(s, smp_processor_id());
1563 if (unlikely(!c->page || !c->freelist ||
1564 !node_match(c, node)))
Christoph Lameter894b8782007-05-10 03:15:16 -07001565
Christoph Lameterdfb4f092007-10-16 01:26:05 -07001566 object = __slab_alloc(s, gfpflags, node, addr, c);
Christoph Lameter894b8782007-05-10 03:15:16 -07001567
1568 else {
Christoph Lameterdfb4f092007-10-16 01:26:05 -07001569 object = c->freelist;
Christoph Lameterb3fba8d2007-10-16 01:26:06 -07001570 c->freelist = object[c->offset];
Christoph Lameter894b8782007-05-10 03:15:16 -07001571 }
1572 local_irq_restore(flags);
Christoph Lameterd07dbea2007-07-17 04:03:23 -07001573
1574 if (unlikely((gfpflags & __GFP_ZERO) && object))
Christoph Lameterce15fea2007-07-17 04:03:28 -07001575 memset(object, 0, s->objsize);
Christoph Lameterd07dbea2007-07-17 04:03:23 -07001576
Christoph Lameter894b8782007-05-10 03:15:16 -07001577 return object;
Christoph Lameter81819f02007-05-06 14:49:36 -07001578}
1579
1580void *kmem_cache_alloc(struct kmem_cache *s, gfp_t gfpflags)
1581{
Christoph Lameterce15fea2007-07-17 04:03:28 -07001582 return slab_alloc(s, gfpflags, -1, __builtin_return_address(0));
Christoph Lameter81819f02007-05-06 14:49:36 -07001583}
1584EXPORT_SYMBOL(kmem_cache_alloc);
1585
1586#ifdef CONFIG_NUMA
1587void *kmem_cache_alloc_node(struct kmem_cache *s, gfp_t gfpflags, int node)
1588{
Christoph Lameterce15fea2007-07-17 04:03:28 -07001589 return slab_alloc(s, gfpflags, node, __builtin_return_address(0));
Christoph Lameter81819f02007-05-06 14:49:36 -07001590}
1591EXPORT_SYMBOL(kmem_cache_alloc_node);
1592#endif
1593
1594/*
Christoph Lameter894b8782007-05-10 03:15:16 -07001595 * Slow patch handling. This may still be called frequently since objects
1596 * have a longer lifetime than the cpu slabs in most processing loads.
Christoph Lameter81819f02007-05-06 14:49:36 -07001597 *
Christoph Lameter894b8782007-05-10 03:15:16 -07001598 * So we still attempt to reduce cache line usage. Just take the slab
1599 * lock and free the item. If there is no additional partial page
1600 * handling required then we can return immediately.
Christoph Lameter81819f02007-05-06 14:49:36 -07001601 */
Christoph Lameter894b8782007-05-10 03:15:16 -07001602static void __slab_free(struct kmem_cache *s, struct page *page,
Christoph Lameterb3fba8d2007-10-16 01:26:06 -07001603 void *x, void *addr, unsigned int offset)
Christoph Lameter81819f02007-05-06 14:49:36 -07001604{
1605 void *prior;
1606 void **object = (void *)x;
Christoph Lameter81819f02007-05-06 14:49:36 -07001607
Christoph Lameter81819f02007-05-06 14:49:36 -07001608 slab_lock(page);
1609
Christoph Lameter35e5d7e2007-05-09 02:32:42 -07001610 if (unlikely(SlabDebug(page)))
Christoph Lameter81819f02007-05-06 14:49:36 -07001611 goto debug;
1612checks_ok:
Christoph Lameterb3fba8d2007-10-16 01:26:06 -07001613 prior = object[offset] = page->freelist;
Christoph Lameter81819f02007-05-06 14:49:36 -07001614 page->freelist = object;
1615 page->inuse--;
1616
Christoph Lameter4b6f0752007-05-16 22:10:53 -07001617 if (unlikely(SlabFrozen(page)))
Christoph Lameter81819f02007-05-06 14:49:36 -07001618 goto out_unlock;
1619
1620 if (unlikely(!page->inuse))
1621 goto slab_empty;
1622
1623 /*
1624 * Objects left in the slab. If it
1625 * was not on the partial list before
1626 * then add it.
1627 */
1628 if (unlikely(!prior))
Christoph Lametere95eed52007-05-06 14:49:44 -07001629 add_partial(get_node(s, page_to_nid(page)), page);
Christoph Lameter81819f02007-05-06 14:49:36 -07001630
1631out_unlock:
1632 slab_unlock(page);
Christoph Lameter81819f02007-05-06 14:49:36 -07001633 return;
1634
1635slab_empty:
1636 if (prior)
1637 /*
Christoph Lameter672bba32007-05-09 02:32:39 -07001638 * Slab still on the partial list.
Christoph Lameter81819f02007-05-06 14:49:36 -07001639 */
1640 remove_partial(s, page);
1641
1642 slab_unlock(page);
1643 discard_slab(s, page);
Christoph Lameter81819f02007-05-06 14:49:36 -07001644 return;
1645
1646debug:
Christoph Lameter3ec09742007-05-16 22:11:00 -07001647 if (!free_debug_processing(s, page, x, addr))
Christoph Lameter77c5e2d2007-05-06 14:49:42 -07001648 goto out_unlock;
Christoph Lameter77c5e2d2007-05-06 14:49:42 -07001649 goto checks_ok;
Christoph Lameter81819f02007-05-06 14:49:36 -07001650}
1651
Christoph Lameter894b8782007-05-10 03:15:16 -07001652/*
1653 * Fastpath with forced inlining to produce a kfree and kmem_cache_free that
1654 * can perform fastpath freeing without additional function calls.
1655 *
1656 * The fastpath is only possible if we are freeing to the current cpu slab
1657 * of this processor. This typically the case if we have just allocated
1658 * the item before.
1659 *
1660 * If fastpath is not possible then fall back to __slab_free where we deal
1661 * with all sorts of special processing.
1662 */
1663static void __always_inline slab_free(struct kmem_cache *s,
1664 struct page *page, void *x, void *addr)
1665{
1666 void **object = (void *)x;
1667 unsigned long flags;
Christoph Lameterdfb4f092007-10-16 01:26:05 -07001668 struct kmem_cache_cpu *c;
Christoph Lameter894b8782007-05-10 03:15:16 -07001669
1670 local_irq_save(flags);
Peter Zijlstra02febdf2007-07-26 20:01:38 +02001671 debug_check_no_locks_freed(object, s->objsize);
Christoph Lameterdfb4f092007-10-16 01:26:05 -07001672 c = get_cpu_slab(s, smp_processor_id());
1673 if (likely(page == c->page && !SlabDebug(page))) {
Christoph Lameterb3fba8d2007-10-16 01:26:06 -07001674 object[c->offset] = c->freelist;
Christoph Lameterdfb4f092007-10-16 01:26:05 -07001675 c->freelist = object;
Christoph Lameter894b8782007-05-10 03:15:16 -07001676 } else
Christoph Lameterb3fba8d2007-10-16 01:26:06 -07001677 __slab_free(s, page, x, addr, c->offset);
Christoph Lameter894b8782007-05-10 03:15:16 -07001678
1679 local_irq_restore(flags);
1680}
1681
Christoph Lameter81819f02007-05-06 14:49:36 -07001682void kmem_cache_free(struct kmem_cache *s, void *x)
1683{
Christoph Lameter77c5e2d2007-05-06 14:49:42 -07001684 struct page *page;
Christoph Lameter81819f02007-05-06 14:49:36 -07001685
Christoph Lameterb49af682007-05-06 14:49:41 -07001686 page = virt_to_head_page(x);
Christoph Lameter81819f02007-05-06 14:49:36 -07001687
Christoph Lameter77c5e2d2007-05-06 14:49:42 -07001688 slab_free(s, page, x, __builtin_return_address(0));
Christoph Lameter81819f02007-05-06 14:49:36 -07001689}
1690EXPORT_SYMBOL(kmem_cache_free);
1691
1692/* Figure out on which slab object the object resides */
1693static struct page *get_object_page(const void *x)
1694{
Christoph Lameterb49af682007-05-06 14:49:41 -07001695 struct page *page = virt_to_head_page(x);
Christoph Lameter81819f02007-05-06 14:49:36 -07001696
1697 if (!PageSlab(page))
1698 return NULL;
1699
1700 return page;
1701}
1702
1703/*
Christoph Lameter672bba32007-05-09 02:32:39 -07001704 * Object placement in a slab is made very easy because we always start at
1705 * offset 0. If we tune the size of the object to the alignment then we can
1706 * get the required alignment by putting one properly sized object after
1707 * another.
Christoph Lameter81819f02007-05-06 14:49:36 -07001708 *
1709 * Notice that the allocation order determines the sizes of the per cpu
1710 * caches. Each processor has always one slab available for allocations.
1711 * Increasing the allocation order reduces the number of times that slabs
Christoph Lameter672bba32007-05-09 02:32:39 -07001712 * must be moved on and off the partial lists and is therefore a factor in
Christoph Lameter81819f02007-05-06 14:49:36 -07001713 * locking overhead.
Christoph Lameter81819f02007-05-06 14:49:36 -07001714 */
1715
1716/*
1717 * Mininum / Maximum order of slab pages. This influences locking overhead
1718 * and slab fragmentation. A higher order reduces the number of partial slabs
1719 * and increases the number of allocations possible without having to
1720 * take the list_lock.
1721 */
1722static int slub_min_order;
1723static int slub_max_order = DEFAULT_MAX_ORDER;
Christoph Lameter81819f02007-05-06 14:49:36 -07001724static int slub_min_objects = DEFAULT_MIN_OBJECTS;
1725
1726/*
1727 * Merge control. If this is set then no merging of slab caches will occur.
Christoph Lameter672bba32007-05-09 02:32:39 -07001728 * (Could be removed. This was introduced to pacify the merge skeptics.)
Christoph Lameter81819f02007-05-06 14:49:36 -07001729 */
1730static int slub_nomerge;
1731
1732/*
Christoph Lameter81819f02007-05-06 14:49:36 -07001733 * Calculate the order of allocation given an slab object size.
1734 *
Christoph Lameter672bba32007-05-09 02:32:39 -07001735 * The order of allocation has significant impact on performance and other
1736 * system components. Generally order 0 allocations should be preferred since
1737 * order 0 does not cause fragmentation in the page allocator. Larger objects
1738 * be problematic to put into order 0 slabs because there may be too much
1739 * unused space left. We go to a higher order if more than 1/8th of the slab
1740 * would be wasted.
Christoph Lameter81819f02007-05-06 14:49:36 -07001741 *
Christoph Lameter672bba32007-05-09 02:32:39 -07001742 * In order to reach satisfactory performance we must ensure that a minimum
1743 * number of objects is in one slab. Otherwise we may generate too much
1744 * activity on the partial lists which requires taking the list_lock. This is
1745 * less a concern for large slabs though which are rarely used.
Christoph Lameter81819f02007-05-06 14:49:36 -07001746 *
Christoph Lameter672bba32007-05-09 02:32:39 -07001747 * slub_max_order specifies the order where we begin to stop considering the
1748 * number of objects in a slab as critical. If we reach slub_max_order then
1749 * we try to keep the page order as low as possible. So we accept more waste
1750 * of space in favor of a small page order.
1751 *
1752 * Higher order allocations also allow the placement of more objects in a
1753 * slab and thereby reduce object handling overhead. If the user has
1754 * requested a higher mininum order then we start with that one instead of
1755 * the smallest order which will fit the object.
Christoph Lameter81819f02007-05-06 14:49:36 -07001756 */
Christoph Lameter5e6d4442007-05-09 02:32:46 -07001757static inline int slab_order(int size, int min_objects,
1758 int max_order, int fract_leftover)
Christoph Lameter81819f02007-05-06 14:49:36 -07001759{
1760 int order;
1761 int rem;
Christoph Lameter6300ea72007-07-17 04:03:20 -07001762 int min_order = slub_min_order;
Christoph Lameter81819f02007-05-06 14:49:36 -07001763
Christoph Lameter6300ea72007-07-17 04:03:20 -07001764 for (order = max(min_order,
Christoph Lameter5e6d4442007-05-09 02:32:46 -07001765 fls(min_objects * size - 1) - PAGE_SHIFT);
1766 order <= max_order; order++) {
1767
Christoph Lameter81819f02007-05-06 14:49:36 -07001768 unsigned long slab_size = PAGE_SIZE << order;
1769
Christoph Lameter5e6d4442007-05-09 02:32:46 -07001770 if (slab_size < min_objects * size)
Christoph Lameter81819f02007-05-06 14:49:36 -07001771 continue;
1772
Christoph Lameter81819f02007-05-06 14:49:36 -07001773 rem = slab_size % size;
1774
Christoph Lameter5e6d4442007-05-09 02:32:46 -07001775 if (rem <= slab_size / fract_leftover)
Christoph Lameter81819f02007-05-06 14:49:36 -07001776 break;
1777
1778 }
Christoph Lameter672bba32007-05-09 02:32:39 -07001779
Christoph Lameter81819f02007-05-06 14:49:36 -07001780 return order;
1781}
1782
Christoph Lameter5e6d4442007-05-09 02:32:46 -07001783static inline int calculate_order(int size)
1784{
1785 int order;
1786 int min_objects;
1787 int fraction;
1788
1789 /*
1790 * Attempt to find best configuration for a slab. This
1791 * works by first attempting to generate a layout with
1792 * the best configuration and backing off gradually.
1793 *
1794 * First we reduce the acceptable waste in a slab. Then
1795 * we reduce the minimum objects required in a slab.
1796 */
1797 min_objects = slub_min_objects;
1798 while (min_objects > 1) {
1799 fraction = 8;
1800 while (fraction >= 4) {
1801 order = slab_order(size, min_objects,
1802 slub_max_order, fraction);
1803 if (order <= slub_max_order)
1804 return order;
1805 fraction /= 2;
1806 }
1807 min_objects /= 2;
1808 }
1809
1810 /*
1811 * We were unable to place multiple objects in a slab. Now
1812 * lets see if we can place a single object there.
1813 */
1814 order = slab_order(size, 1, slub_max_order, 1);
1815 if (order <= slub_max_order)
1816 return order;
1817
1818 /*
1819 * Doh this slab cannot be placed using slub_max_order.
1820 */
1821 order = slab_order(size, 1, MAX_ORDER, 1);
1822 if (order <= MAX_ORDER)
1823 return order;
1824 return -ENOSYS;
1825}
1826
Christoph Lameter81819f02007-05-06 14:49:36 -07001827/*
Christoph Lameter672bba32007-05-09 02:32:39 -07001828 * Figure out what the alignment of the objects will be.
Christoph Lameter81819f02007-05-06 14:49:36 -07001829 */
1830static unsigned long calculate_alignment(unsigned long flags,
1831 unsigned long align, unsigned long size)
1832{
1833 /*
1834 * If the user wants hardware cache aligned objects then
1835 * follow that suggestion if the object is sufficiently
1836 * large.
1837 *
1838 * The hardware cache alignment cannot override the
1839 * specified alignment though. If that is greater
1840 * then use it.
1841 */
Christoph Lameter5af60832007-05-06 14:49:56 -07001842 if ((flags & SLAB_HWCACHE_ALIGN) &&
Christoph Lameter65c02d42007-05-09 02:32:35 -07001843 size > cache_line_size() / 2)
1844 return max_t(unsigned long, align, cache_line_size());
Christoph Lameter81819f02007-05-06 14:49:36 -07001845
1846 if (align < ARCH_SLAB_MINALIGN)
1847 return ARCH_SLAB_MINALIGN;
1848
1849 return ALIGN(align, sizeof(void *));
1850}
1851
Christoph Lameterdfb4f092007-10-16 01:26:05 -07001852static void init_kmem_cache_cpu(struct kmem_cache *s,
1853 struct kmem_cache_cpu *c)
1854{
1855 c->page = NULL;
1856 c->freelist = NULL;
Christoph Lameterb3fba8d2007-10-16 01:26:06 -07001857 c->offset = s->offset / sizeof(void *);
Christoph Lameterdfb4f092007-10-16 01:26:05 -07001858 c->node = 0;
1859}
1860
1861static inline int alloc_kmem_cache_cpus(struct kmem_cache *s, gfp_t flags)
1862{
1863 int cpu;
1864
1865 for_each_possible_cpu(cpu)
1866 init_kmem_cache_cpu(s, get_cpu_slab(s, cpu));
1867
1868 return 1;
1869}
1870
Christoph Lameter81819f02007-05-06 14:49:36 -07001871static void init_kmem_cache_node(struct kmem_cache_node *n)
1872{
1873 n->nr_partial = 0;
1874 atomic_long_set(&n->nr_slabs, 0);
1875 spin_lock_init(&n->list_lock);
1876 INIT_LIST_HEAD(&n->partial);
Christoph Lameter8ab13722007-07-17 04:03:32 -07001877#ifdef CONFIG_SLUB_DEBUG
Christoph Lameter643b1132007-05-06 14:49:42 -07001878 INIT_LIST_HEAD(&n->full);
Christoph Lameter8ab13722007-07-17 04:03:32 -07001879#endif
Christoph Lameter81819f02007-05-06 14:49:36 -07001880}
1881
1882#ifdef CONFIG_NUMA
1883/*
1884 * No kmalloc_node yet so do it by hand. We know that this is the first
1885 * slab on the node for this slabcache. There are no concurrent accesses
1886 * possible.
1887 *
1888 * Note that this function only works on the kmalloc_node_cache
1889 * when allocating for the kmalloc_node_cache.
1890 */
Adrian Bunk1cd7daa2007-10-16 01:24:18 -07001891static struct kmem_cache_node *early_kmem_cache_node_alloc(gfp_t gfpflags,
1892 int node)
Christoph Lameter81819f02007-05-06 14:49:36 -07001893{
1894 struct page *page;
1895 struct kmem_cache_node *n;
1896
1897 BUG_ON(kmalloc_caches->size < sizeof(struct kmem_cache_node));
1898
Christoph Lametera2f92ee2007-08-22 14:01:57 -07001899 page = new_slab(kmalloc_caches, gfpflags, node);
Christoph Lameter81819f02007-05-06 14:49:36 -07001900
1901 BUG_ON(!page);
Christoph Lametera2f92ee2007-08-22 14:01:57 -07001902 if (page_to_nid(page) != node) {
1903 printk(KERN_ERR "SLUB: Unable to allocate memory from "
1904 "node %d\n", node);
1905 printk(KERN_ERR "SLUB: Allocating a useless per node structure "
1906 "in order to be able to continue\n");
1907 }
1908
Christoph Lameter81819f02007-05-06 14:49:36 -07001909 n = page->freelist;
1910 BUG_ON(!n);
1911 page->freelist = get_freepointer(kmalloc_caches, n);
1912 page->inuse++;
1913 kmalloc_caches->node[node] = n;
Christoph Lameter8ab13722007-07-17 04:03:32 -07001914#ifdef CONFIG_SLUB_DEBUG
Christoph Lameterd45f39c2007-07-17 04:03:21 -07001915 init_object(kmalloc_caches, n, 1);
1916 init_tracking(kmalloc_caches, n);
Christoph Lameter8ab13722007-07-17 04:03:32 -07001917#endif
Christoph Lameter81819f02007-05-06 14:49:36 -07001918 init_kmem_cache_node(n);
1919 atomic_long_inc(&n->nr_slabs);
Christoph Lametere95eed52007-05-06 14:49:44 -07001920 add_partial(n, page);
Christoph Lameterdbc55fa2007-07-03 09:31:04 -07001921
1922 /*
1923 * new_slab() disables interupts. If we do not reenable interrupts here
1924 * then bootup would continue with interrupts disabled.
1925 */
1926 local_irq_enable();
Christoph Lameter81819f02007-05-06 14:49:36 -07001927 return n;
1928}
1929
1930static void free_kmem_cache_nodes(struct kmem_cache *s)
1931{
1932 int node;
1933
Christoph Lameterf64dc582007-10-16 01:25:33 -07001934 for_each_node_state(node, N_NORMAL_MEMORY) {
Christoph Lameter81819f02007-05-06 14:49:36 -07001935 struct kmem_cache_node *n = s->node[node];
1936 if (n && n != &s->local_node)
1937 kmem_cache_free(kmalloc_caches, n);
1938 s->node[node] = NULL;
1939 }
1940}
1941
1942static int init_kmem_cache_nodes(struct kmem_cache *s, gfp_t gfpflags)
1943{
1944 int node;
1945 int local_node;
1946
1947 if (slab_state >= UP)
1948 local_node = page_to_nid(virt_to_page(s));
1949 else
1950 local_node = 0;
1951
Christoph Lameterf64dc582007-10-16 01:25:33 -07001952 for_each_node_state(node, N_NORMAL_MEMORY) {
Christoph Lameter81819f02007-05-06 14:49:36 -07001953 struct kmem_cache_node *n;
1954
1955 if (local_node == node)
1956 n = &s->local_node;
1957 else {
1958 if (slab_state == DOWN) {
1959 n = early_kmem_cache_node_alloc(gfpflags,
1960 node);
1961 continue;
1962 }
1963 n = kmem_cache_alloc_node(kmalloc_caches,
1964 gfpflags, node);
1965
1966 if (!n) {
1967 free_kmem_cache_nodes(s);
1968 return 0;
1969 }
1970
1971 }
1972 s->node[node] = n;
1973 init_kmem_cache_node(n);
1974 }
1975 return 1;
1976}
1977#else
1978static void free_kmem_cache_nodes(struct kmem_cache *s)
1979{
1980}
1981
1982static int init_kmem_cache_nodes(struct kmem_cache *s, gfp_t gfpflags)
1983{
1984 init_kmem_cache_node(&s->local_node);
1985 return 1;
1986}
1987#endif
1988
1989/*
1990 * calculate_sizes() determines the order and the distribution of data within
1991 * a slab object.
1992 */
1993static int calculate_sizes(struct kmem_cache *s)
1994{
1995 unsigned long flags = s->flags;
1996 unsigned long size = s->objsize;
1997 unsigned long align = s->align;
1998
1999 /*
2000 * Determine if we can poison the object itself. If the user of
2001 * the slab may touch the object after free or before allocation
2002 * then we should never poison the object itself.
2003 */
2004 if ((flags & SLAB_POISON) && !(flags & SLAB_DESTROY_BY_RCU) &&
Christoph Lameterc59def92007-05-16 22:10:50 -07002005 !s->ctor)
Christoph Lameter81819f02007-05-06 14:49:36 -07002006 s->flags |= __OBJECT_POISON;
2007 else
2008 s->flags &= ~__OBJECT_POISON;
2009
2010 /*
2011 * Round up object size to the next word boundary. We can only
2012 * place the free pointer at word boundaries and this determines
2013 * the possible location of the free pointer.
2014 */
2015 size = ALIGN(size, sizeof(void *));
2016
Christoph Lameter41ecc552007-05-09 02:32:44 -07002017#ifdef CONFIG_SLUB_DEBUG
Christoph Lameter81819f02007-05-06 14:49:36 -07002018 /*
Christoph Lameter672bba32007-05-09 02:32:39 -07002019 * If we are Redzoning then check if there is some space between the
Christoph Lameter81819f02007-05-06 14:49:36 -07002020 * end of the object and the free pointer. If not then add an
Christoph Lameter672bba32007-05-09 02:32:39 -07002021 * additional word to have some bytes to store Redzone information.
Christoph Lameter81819f02007-05-06 14:49:36 -07002022 */
2023 if ((flags & SLAB_RED_ZONE) && size == s->objsize)
2024 size += sizeof(void *);
Christoph Lameter41ecc552007-05-09 02:32:44 -07002025#endif
Christoph Lameter81819f02007-05-06 14:49:36 -07002026
2027 /*
Christoph Lameter672bba32007-05-09 02:32:39 -07002028 * With that we have determined the number of bytes in actual use
2029 * by the object. This is the potential offset to the free pointer.
Christoph Lameter81819f02007-05-06 14:49:36 -07002030 */
2031 s->inuse = size;
2032
2033 if (((flags & (SLAB_DESTROY_BY_RCU | SLAB_POISON)) ||
Christoph Lameterc59def92007-05-16 22:10:50 -07002034 s->ctor)) {
Christoph Lameter81819f02007-05-06 14:49:36 -07002035 /*
2036 * Relocate free pointer after the object if it is not
2037 * permitted to overwrite the first word of the object on
2038 * kmem_cache_free.
2039 *
2040 * This is the case if we do RCU, have a constructor or
2041 * destructor or are poisoning the objects.
2042 */
2043 s->offset = size;
2044 size += sizeof(void *);
2045 }
2046
Christoph Lameterc12b3c62007-05-23 13:57:31 -07002047#ifdef CONFIG_SLUB_DEBUG
Christoph Lameter81819f02007-05-06 14:49:36 -07002048 if (flags & SLAB_STORE_USER)
2049 /*
2050 * Need to store information about allocs and frees after
2051 * the object.
2052 */
2053 size += 2 * sizeof(struct track);
2054
Christoph Lameterbe7b3fb2007-05-09 02:32:36 -07002055 if (flags & SLAB_RED_ZONE)
Christoph Lameter81819f02007-05-06 14:49:36 -07002056 /*
2057 * Add some empty padding so that we can catch
2058 * overwrites from earlier objects rather than let
2059 * tracking information or the free pointer be
2060 * corrupted if an user writes before the start
2061 * of the object.
2062 */
2063 size += sizeof(void *);
Christoph Lameter41ecc552007-05-09 02:32:44 -07002064#endif
Christoph Lameter672bba32007-05-09 02:32:39 -07002065
Christoph Lameter81819f02007-05-06 14:49:36 -07002066 /*
2067 * Determine the alignment based on various parameters that the
Christoph Lameter65c02d42007-05-09 02:32:35 -07002068 * user specified and the dynamic determination of cache line size
2069 * on bootup.
Christoph Lameter81819f02007-05-06 14:49:36 -07002070 */
2071 align = calculate_alignment(flags, align, s->objsize);
2072
2073 /*
2074 * SLUB stores one object immediately after another beginning from
2075 * offset 0. In order to align the objects we have to simply size
2076 * each object to conform to the alignment.
2077 */
2078 size = ALIGN(size, align);
2079 s->size = size;
2080
2081 s->order = calculate_order(size);
2082 if (s->order < 0)
2083 return 0;
2084
2085 /*
2086 * Determine the number of objects per slab
2087 */
2088 s->objects = (PAGE_SIZE << s->order) / size;
2089
Christoph Lameterb3fba8d2007-10-16 01:26:06 -07002090 return !!s->objects;
Christoph Lameter81819f02007-05-06 14:49:36 -07002091
2092}
2093
Christoph Lameter81819f02007-05-06 14:49:36 -07002094static int kmem_cache_open(struct kmem_cache *s, gfp_t gfpflags,
2095 const char *name, size_t size,
2096 size_t align, unsigned long flags,
Christoph Lameterc59def92007-05-16 22:10:50 -07002097 void (*ctor)(void *, struct kmem_cache *, unsigned long))
Christoph Lameter81819f02007-05-06 14:49:36 -07002098{
2099 memset(s, 0, kmem_size);
2100 s->name = name;
2101 s->ctor = ctor;
Christoph Lameter81819f02007-05-06 14:49:36 -07002102 s->objsize = size;
Christoph Lameter81819f02007-05-06 14:49:36 -07002103 s->align = align;
Christoph Lameterba0268a2007-09-11 15:24:11 -07002104 s->flags = kmem_cache_flags(size, flags, name, ctor);
Christoph Lameter81819f02007-05-06 14:49:36 -07002105
2106 if (!calculate_sizes(s))
2107 goto error;
2108
2109 s->refcount = 1;
2110#ifdef CONFIG_NUMA
2111 s->defrag_ratio = 100;
2112#endif
Christoph Lameterdfb4f092007-10-16 01:26:05 -07002113 if (!init_kmem_cache_nodes(s, gfpflags & ~SLUB_DMA))
2114 goto error;
Christoph Lameter81819f02007-05-06 14:49:36 -07002115
Christoph Lameterdfb4f092007-10-16 01:26:05 -07002116 if (alloc_kmem_cache_cpus(s, gfpflags & ~SLUB_DMA))
Christoph Lameter81819f02007-05-06 14:49:36 -07002117 return 1;
2118error:
2119 if (flags & SLAB_PANIC)
2120 panic("Cannot create slab %s size=%lu realsize=%u "
2121 "order=%u offset=%u flags=%lx\n",
2122 s->name, (unsigned long)size, s->size, s->order,
2123 s->offset, flags);
2124 return 0;
2125}
Christoph Lameter81819f02007-05-06 14:49:36 -07002126
2127/*
2128 * Check if a given pointer is valid
2129 */
2130int kmem_ptr_validate(struct kmem_cache *s, const void *object)
2131{
2132 struct page * page;
Christoph Lameter81819f02007-05-06 14:49:36 -07002133
2134 page = get_object_page(object);
2135
2136 if (!page || s != page->slab)
2137 /* No slab or wrong slab */
2138 return 0;
2139
Christoph Lameterabcd08a2007-05-09 02:32:37 -07002140 if (!check_valid_pointer(s, page, object))
Christoph Lameter81819f02007-05-06 14:49:36 -07002141 return 0;
2142
2143 /*
2144 * We could also check if the object is on the slabs freelist.
2145 * But this would be too expensive and it seems that the main
2146 * purpose of kmem_ptr_valid is to check if the object belongs
2147 * to a certain slab.
2148 */
2149 return 1;
2150}
2151EXPORT_SYMBOL(kmem_ptr_validate);
2152
2153/*
2154 * Determine the size of a slab object
2155 */
2156unsigned int kmem_cache_size(struct kmem_cache *s)
2157{
2158 return s->objsize;
2159}
2160EXPORT_SYMBOL(kmem_cache_size);
2161
2162const char *kmem_cache_name(struct kmem_cache *s)
2163{
2164 return s->name;
2165}
2166EXPORT_SYMBOL(kmem_cache_name);
2167
2168/*
Christoph Lameter672bba32007-05-09 02:32:39 -07002169 * Attempt to free all slabs on a node. Return the number of slabs we
2170 * were unable to free.
Christoph Lameter81819f02007-05-06 14:49:36 -07002171 */
2172static int free_list(struct kmem_cache *s, struct kmem_cache_node *n,
2173 struct list_head *list)
2174{
2175 int slabs_inuse = 0;
2176 unsigned long flags;
2177 struct page *page, *h;
2178
2179 spin_lock_irqsave(&n->list_lock, flags);
2180 list_for_each_entry_safe(page, h, list, lru)
2181 if (!page->inuse) {
2182 list_del(&page->lru);
2183 discard_slab(s, page);
2184 } else
2185 slabs_inuse++;
2186 spin_unlock_irqrestore(&n->list_lock, flags);
2187 return slabs_inuse;
2188}
2189
2190/*
Christoph Lameter672bba32007-05-09 02:32:39 -07002191 * Release all resources used by a slab cache.
Christoph Lameter81819f02007-05-06 14:49:36 -07002192 */
Christoph Lameter0c710012007-07-17 04:03:24 -07002193static inline int kmem_cache_close(struct kmem_cache *s)
Christoph Lameter81819f02007-05-06 14:49:36 -07002194{
2195 int node;
2196
2197 flush_all(s);
2198
2199 /* Attempt to free all objects */
Christoph Lameterf64dc582007-10-16 01:25:33 -07002200 for_each_node_state(node, N_NORMAL_MEMORY) {
Christoph Lameter81819f02007-05-06 14:49:36 -07002201 struct kmem_cache_node *n = get_node(s, node);
2202
Christoph Lameter2086d262007-05-06 14:49:46 -07002203 n->nr_partial -= free_list(s, n, &n->partial);
Christoph Lameter81819f02007-05-06 14:49:36 -07002204 if (atomic_long_read(&n->nr_slabs))
2205 return 1;
2206 }
2207 free_kmem_cache_nodes(s);
2208 return 0;
2209}
2210
2211/*
2212 * Close a cache and release the kmem_cache structure
2213 * (must be used for caches created using kmem_cache_create)
2214 */
2215void kmem_cache_destroy(struct kmem_cache *s)
2216{
2217 down_write(&slub_lock);
2218 s->refcount--;
2219 if (!s->refcount) {
2220 list_del(&s->list);
Christoph Lametera0e1d1b2007-07-17 04:03:31 -07002221 up_write(&slub_lock);
Christoph Lameter81819f02007-05-06 14:49:36 -07002222 if (kmem_cache_close(s))
2223 WARN_ON(1);
2224 sysfs_slab_remove(s);
2225 kfree(s);
Christoph Lametera0e1d1b2007-07-17 04:03:31 -07002226 } else
2227 up_write(&slub_lock);
Christoph Lameter81819f02007-05-06 14:49:36 -07002228}
2229EXPORT_SYMBOL(kmem_cache_destroy);
2230
2231/********************************************************************
2232 * Kmalloc subsystem
2233 *******************************************************************/
2234
Christoph Lameteraadb4bc2007-10-16 01:24:38 -07002235struct kmem_cache kmalloc_caches[PAGE_SHIFT] __cacheline_aligned;
Christoph Lameter81819f02007-05-06 14:49:36 -07002236EXPORT_SYMBOL(kmalloc_caches);
2237
2238#ifdef CONFIG_ZONE_DMA
Christoph Lameteraadb4bc2007-10-16 01:24:38 -07002239static struct kmem_cache *kmalloc_caches_dma[PAGE_SHIFT];
Christoph Lameter81819f02007-05-06 14:49:36 -07002240#endif
2241
2242static int __init setup_slub_min_order(char *str)
2243{
2244 get_option (&str, &slub_min_order);
2245
2246 return 1;
2247}
2248
2249__setup("slub_min_order=", setup_slub_min_order);
2250
2251static int __init setup_slub_max_order(char *str)
2252{
2253 get_option (&str, &slub_max_order);
2254
2255 return 1;
2256}
2257
2258__setup("slub_max_order=", setup_slub_max_order);
2259
2260static int __init setup_slub_min_objects(char *str)
2261{
2262 get_option (&str, &slub_min_objects);
2263
2264 return 1;
2265}
2266
2267__setup("slub_min_objects=", setup_slub_min_objects);
2268
2269static int __init setup_slub_nomerge(char *str)
2270{
2271 slub_nomerge = 1;
2272 return 1;
2273}
2274
2275__setup("slub_nomerge", setup_slub_nomerge);
2276
Christoph Lameter81819f02007-05-06 14:49:36 -07002277static struct kmem_cache *create_kmalloc_cache(struct kmem_cache *s,
2278 const char *name, int size, gfp_t gfp_flags)
2279{
2280 unsigned int flags = 0;
2281
2282 if (gfp_flags & SLUB_DMA)
2283 flags = SLAB_CACHE_DMA;
2284
2285 down_write(&slub_lock);
2286 if (!kmem_cache_open(s, gfp_flags, name, size, ARCH_KMALLOC_MINALIGN,
Christoph Lameterc59def92007-05-16 22:10:50 -07002287 flags, NULL))
Christoph Lameter81819f02007-05-06 14:49:36 -07002288 goto panic;
2289
2290 list_add(&s->list, &slab_caches);
2291 up_write(&slub_lock);
2292 if (sysfs_slab_add(s))
2293 goto panic;
2294 return s;
2295
2296panic:
2297 panic("Creation of kmalloc slab %s size=%d failed.\n", name, size);
2298}
2299
Christoph Lameter2e443fd2007-07-17 04:03:24 -07002300#ifdef CONFIG_ZONE_DMA
Christoph Lameter1ceef402007-08-07 15:11:48 -07002301
2302static void sysfs_add_func(struct work_struct *w)
2303{
2304 struct kmem_cache *s;
2305
2306 down_write(&slub_lock);
2307 list_for_each_entry(s, &slab_caches, list) {
2308 if (s->flags & __SYSFS_ADD_DEFERRED) {
2309 s->flags &= ~__SYSFS_ADD_DEFERRED;
2310 sysfs_slab_add(s);
2311 }
2312 }
2313 up_write(&slub_lock);
2314}
2315
2316static DECLARE_WORK(sysfs_add_work, sysfs_add_func);
2317
Christoph Lameter2e443fd2007-07-17 04:03:24 -07002318static noinline struct kmem_cache *dma_kmalloc_cache(int index, gfp_t flags)
2319{
2320 struct kmem_cache *s;
Christoph Lameter2e443fd2007-07-17 04:03:24 -07002321 char *text;
2322 size_t realsize;
2323
2324 s = kmalloc_caches_dma[index];
2325 if (s)
2326 return s;
2327
2328 /* Dynamically create dma cache */
Christoph Lameter1ceef402007-08-07 15:11:48 -07002329 if (flags & __GFP_WAIT)
2330 down_write(&slub_lock);
2331 else {
2332 if (!down_write_trylock(&slub_lock))
2333 goto out;
2334 }
2335
2336 if (kmalloc_caches_dma[index])
2337 goto unlock_out;
Christoph Lameter2e443fd2007-07-17 04:03:24 -07002338
Christoph Lameter7b55f622007-07-17 04:03:27 -07002339 realsize = kmalloc_caches[index].objsize;
Christoph Lameter1ceef402007-08-07 15:11:48 -07002340 text = kasprintf(flags & ~SLUB_DMA, "kmalloc_dma-%d", (unsigned int)realsize),
2341 s = kmalloc(kmem_size, flags & ~SLUB_DMA);
2342
2343 if (!s || !text || !kmem_cache_open(s, flags, text,
2344 realsize, ARCH_KMALLOC_MINALIGN,
2345 SLAB_CACHE_DMA|__SYSFS_ADD_DEFERRED, NULL)) {
2346 kfree(s);
2347 kfree(text);
2348 goto unlock_out;
Christoph Lameterdfce8642007-07-17 04:03:25 -07002349 }
Christoph Lameter1ceef402007-08-07 15:11:48 -07002350
2351 list_add(&s->list, &slab_caches);
2352 kmalloc_caches_dma[index] = s;
2353
2354 schedule_work(&sysfs_add_work);
2355
2356unlock_out:
Christoph Lameterdfce8642007-07-17 04:03:25 -07002357 up_write(&slub_lock);
Christoph Lameter1ceef402007-08-07 15:11:48 -07002358out:
Christoph Lameterdfce8642007-07-17 04:03:25 -07002359 return kmalloc_caches_dma[index];
Christoph Lameter2e443fd2007-07-17 04:03:24 -07002360}
2361#endif
2362
Christoph Lameterf1b26332007-07-17 04:03:26 -07002363/*
2364 * Conversion table for small slabs sizes / 8 to the index in the
2365 * kmalloc array. This is necessary for slabs < 192 since we have non power
2366 * of two cache sizes there. The size of larger slabs can be determined using
2367 * fls.
2368 */
2369static s8 size_index[24] = {
2370 3, /* 8 */
2371 4, /* 16 */
2372 5, /* 24 */
2373 5, /* 32 */
2374 6, /* 40 */
2375 6, /* 48 */
2376 6, /* 56 */
2377 6, /* 64 */
2378 1, /* 72 */
2379 1, /* 80 */
2380 1, /* 88 */
2381 1, /* 96 */
2382 7, /* 104 */
2383 7, /* 112 */
2384 7, /* 120 */
2385 7, /* 128 */
2386 2, /* 136 */
2387 2, /* 144 */
2388 2, /* 152 */
2389 2, /* 160 */
2390 2, /* 168 */
2391 2, /* 176 */
2392 2, /* 184 */
2393 2 /* 192 */
2394};
2395
Christoph Lameter81819f02007-05-06 14:49:36 -07002396static struct kmem_cache *get_slab(size_t size, gfp_t flags)
2397{
Christoph Lameterf1b26332007-07-17 04:03:26 -07002398 int index;
Christoph Lameter81819f02007-05-06 14:49:36 -07002399
Christoph Lameterf1b26332007-07-17 04:03:26 -07002400 if (size <= 192) {
2401 if (!size)
2402 return ZERO_SIZE_PTR;
Christoph Lameter81819f02007-05-06 14:49:36 -07002403
Christoph Lameterf1b26332007-07-17 04:03:26 -07002404 index = size_index[(size - 1) / 8];
Christoph Lameteraadb4bc2007-10-16 01:24:38 -07002405 } else
Christoph Lameterf1b26332007-07-17 04:03:26 -07002406 index = fls(size - 1);
Christoph Lameter81819f02007-05-06 14:49:36 -07002407
2408#ifdef CONFIG_ZONE_DMA
Christoph Lameterf1b26332007-07-17 04:03:26 -07002409 if (unlikely((flags & SLUB_DMA)))
Christoph Lameter2e443fd2007-07-17 04:03:24 -07002410 return dma_kmalloc_cache(index, flags);
Christoph Lameterf1b26332007-07-17 04:03:26 -07002411
Christoph Lameter81819f02007-05-06 14:49:36 -07002412#endif
2413 return &kmalloc_caches[index];
2414}
2415
2416void *__kmalloc(size_t size, gfp_t flags)
2417{
Christoph Lameteraadb4bc2007-10-16 01:24:38 -07002418 struct kmem_cache *s;
Christoph Lameter81819f02007-05-06 14:49:36 -07002419
Christoph Lameteraadb4bc2007-10-16 01:24:38 -07002420 if (unlikely(size > PAGE_SIZE / 2))
2421 return (void *)__get_free_pages(flags | __GFP_COMP,
2422 get_order(size));
2423
2424 s = get_slab(size, flags);
2425
2426 if (unlikely(ZERO_OR_NULL_PTR(s)))
Christoph Lameter6cb8f912007-07-17 04:03:22 -07002427 return s;
2428
Christoph Lameterce15fea2007-07-17 04:03:28 -07002429 return slab_alloc(s, flags, -1, __builtin_return_address(0));
Christoph Lameter81819f02007-05-06 14:49:36 -07002430}
2431EXPORT_SYMBOL(__kmalloc);
2432
2433#ifdef CONFIG_NUMA
2434void *__kmalloc_node(size_t size, gfp_t flags, int node)
2435{
Christoph Lameteraadb4bc2007-10-16 01:24:38 -07002436 struct kmem_cache *s;
Christoph Lameter81819f02007-05-06 14:49:36 -07002437
Christoph Lameteraadb4bc2007-10-16 01:24:38 -07002438 if (unlikely(size > PAGE_SIZE / 2))
2439 return (void *)__get_free_pages(flags | __GFP_COMP,
2440 get_order(size));
2441
2442 s = get_slab(size, flags);
2443
2444 if (unlikely(ZERO_OR_NULL_PTR(s)))
Christoph Lameter6cb8f912007-07-17 04:03:22 -07002445 return s;
2446
Christoph Lameterce15fea2007-07-17 04:03:28 -07002447 return slab_alloc(s, flags, node, __builtin_return_address(0));
Christoph Lameter81819f02007-05-06 14:49:36 -07002448}
2449EXPORT_SYMBOL(__kmalloc_node);
2450#endif
2451
2452size_t ksize(const void *object)
2453{
Christoph Lameter272c1d22007-06-08 13:46:49 -07002454 struct page *page;
Christoph Lameter81819f02007-05-06 14:49:36 -07002455 struct kmem_cache *s;
2456
Christoph Lameteref8b4522007-10-16 01:24:46 -07002457 BUG_ON(!object);
2458 if (unlikely(object == ZERO_SIZE_PTR))
Christoph Lameter272c1d22007-06-08 13:46:49 -07002459 return 0;
2460
2461 page = get_object_page(object);
Christoph Lameter81819f02007-05-06 14:49:36 -07002462 BUG_ON(!page);
2463 s = page->slab;
2464 BUG_ON(!s);
2465
2466 /*
2467 * Debugging requires use of the padding between object
2468 * and whatever may come after it.
2469 */
2470 if (s->flags & (SLAB_RED_ZONE | SLAB_POISON))
2471 return s->objsize;
2472
2473 /*
2474 * If we have the need to store the freelist pointer
2475 * back there or track user information then we can
2476 * only use the space before that information.
2477 */
2478 if (s->flags & (SLAB_DESTROY_BY_RCU | SLAB_STORE_USER))
2479 return s->inuse;
2480
2481 /*
2482 * Else we can use all the padding etc for the allocation
2483 */
2484 return s->size;
2485}
2486EXPORT_SYMBOL(ksize);
2487
2488void kfree(const void *x)
2489{
Christoph Lameter81819f02007-05-06 14:49:36 -07002490 struct page *page;
2491
Satyam Sharma2408c552007-10-16 01:24:44 -07002492 if (unlikely(ZERO_OR_NULL_PTR(x)))
Christoph Lameter81819f02007-05-06 14:49:36 -07002493 return;
2494
Christoph Lameterb49af682007-05-06 14:49:41 -07002495 page = virt_to_head_page(x);
Christoph Lameteraadb4bc2007-10-16 01:24:38 -07002496 if (unlikely(!PageSlab(page))) {
2497 put_page(page);
2498 return;
2499 }
2500 slab_free(page->slab, page, (void *)x, __builtin_return_address(0));
Christoph Lameter81819f02007-05-06 14:49:36 -07002501}
2502EXPORT_SYMBOL(kfree);
2503
Christoph Lameter2086d262007-05-06 14:49:46 -07002504/*
Christoph Lameter672bba32007-05-09 02:32:39 -07002505 * kmem_cache_shrink removes empty slabs from the partial lists and sorts
2506 * the remaining slabs by the number of items in use. The slabs with the
2507 * most items in use come first. New allocations will then fill those up
2508 * and thus they can be removed from the partial lists.
2509 *
2510 * The slabs with the least items are placed last. This results in them
2511 * being allocated from last increasing the chance that the last objects
2512 * are freed in them.
Christoph Lameter2086d262007-05-06 14:49:46 -07002513 */
2514int kmem_cache_shrink(struct kmem_cache *s)
2515{
2516 int node;
2517 int i;
2518 struct kmem_cache_node *n;
2519 struct page *page;
2520 struct page *t;
2521 struct list_head *slabs_by_inuse =
2522 kmalloc(sizeof(struct list_head) * s->objects, GFP_KERNEL);
2523 unsigned long flags;
2524
2525 if (!slabs_by_inuse)
2526 return -ENOMEM;
2527
2528 flush_all(s);
Christoph Lameterf64dc582007-10-16 01:25:33 -07002529 for_each_node_state(node, N_NORMAL_MEMORY) {
Christoph Lameter2086d262007-05-06 14:49:46 -07002530 n = get_node(s, node);
2531
2532 if (!n->nr_partial)
2533 continue;
2534
2535 for (i = 0; i < s->objects; i++)
2536 INIT_LIST_HEAD(slabs_by_inuse + i);
2537
2538 spin_lock_irqsave(&n->list_lock, flags);
2539
2540 /*
Christoph Lameter672bba32007-05-09 02:32:39 -07002541 * Build lists indexed by the items in use in each slab.
Christoph Lameter2086d262007-05-06 14:49:46 -07002542 *
Christoph Lameter672bba32007-05-09 02:32:39 -07002543 * Note that concurrent frees may occur while we hold the
2544 * list_lock. page->inuse here is the upper limit.
Christoph Lameter2086d262007-05-06 14:49:46 -07002545 */
2546 list_for_each_entry_safe(page, t, &n->partial, lru) {
2547 if (!page->inuse && slab_trylock(page)) {
2548 /*
2549 * Must hold slab lock here because slab_free
2550 * may have freed the last object and be
2551 * waiting to release the slab.
2552 */
2553 list_del(&page->lru);
2554 n->nr_partial--;
2555 slab_unlock(page);
2556 discard_slab(s, page);
2557 } else {
Christoph Lameterfcda3d82007-07-30 13:06:46 -07002558 list_move(&page->lru,
2559 slabs_by_inuse + page->inuse);
Christoph Lameter2086d262007-05-06 14:49:46 -07002560 }
2561 }
2562
Christoph Lameter2086d262007-05-06 14:49:46 -07002563 /*
Christoph Lameter672bba32007-05-09 02:32:39 -07002564 * Rebuild the partial list with the slabs filled up most
2565 * first and the least used slabs at the end.
Christoph Lameter2086d262007-05-06 14:49:46 -07002566 */
2567 for (i = s->objects - 1; i >= 0; i--)
2568 list_splice(slabs_by_inuse + i, n->partial.prev);
2569
Christoph Lameter2086d262007-05-06 14:49:46 -07002570 spin_unlock_irqrestore(&n->list_lock, flags);
2571 }
2572
2573 kfree(slabs_by_inuse);
2574 return 0;
2575}
2576EXPORT_SYMBOL(kmem_cache_shrink);
2577
Christoph Lameter81819f02007-05-06 14:49:36 -07002578/********************************************************************
2579 * Basic setup of slabs
2580 *******************************************************************/
2581
2582void __init kmem_cache_init(void)
2583{
2584 int i;
Christoph Lameter4b356be2007-06-16 10:16:13 -07002585 int caches = 0;
Christoph Lameter81819f02007-05-06 14:49:36 -07002586
2587#ifdef CONFIG_NUMA
2588 /*
2589 * Must first have the slab cache available for the allocations of the
Christoph Lameter672bba32007-05-09 02:32:39 -07002590 * struct kmem_cache_node's. There is special bootstrap code in
Christoph Lameter81819f02007-05-06 14:49:36 -07002591 * kmem_cache_open for slab_state == DOWN.
2592 */
2593 create_kmalloc_cache(&kmalloc_caches[0], "kmem_cache_node",
2594 sizeof(struct kmem_cache_node), GFP_KERNEL);
Christoph Lameter8ffa6872007-05-31 00:40:51 -07002595 kmalloc_caches[0].refcount = -1;
Christoph Lameter4b356be2007-06-16 10:16:13 -07002596 caches++;
Christoph Lameter81819f02007-05-06 14:49:36 -07002597#endif
2598
2599 /* Able to allocate the per node structures */
2600 slab_state = PARTIAL;
2601
2602 /* Caches that are not of the two-to-the-power-of size */
Christoph Lameter4b356be2007-06-16 10:16:13 -07002603 if (KMALLOC_MIN_SIZE <= 64) {
2604 create_kmalloc_cache(&kmalloc_caches[1],
Christoph Lameter81819f02007-05-06 14:49:36 -07002605 "kmalloc-96", 96, GFP_KERNEL);
Christoph Lameter4b356be2007-06-16 10:16:13 -07002606 caches++;
2607 }
2608 if (KMALLOC_MIN_SIZE <= 128) {
2609 create_kmalloc_cache(&kmalloc_caches[2],
Christoph Lameter81819f02007-05-06 14:49:36 -07002610 "kmalloc-192", 192, GFP_KERNEL);
Christoph Lameter4b356be2007-06-16 10:16:13 -07002611 caches++;
2612 }
Christoph Lameter81819f02007-05-06 14:49:36 -07002613
Christoph Lameteraadb4bc2007-10-16 01:24:38 -07002614 for (i = KMALLOC_SHIFT_LOW; i < PAGE_SHIFT; i++) {
Christoph Lameter81819f02007-05-06 14:49:36 -07002615 create_kmalloc_cache(&kmalloc_caches[i],
2616 "kmalloc", 1 << i, GFP_KERNEL);
Christoph Lameter4b356be2007-06-16 10:16:13 -07002617 caches++;
2618 }
Christoph Lameter81819f02007-05-06 14:49:36 -07002619
Christoph Lameterf1b26332007-07-17 04:03:26 -07002620
2621 /*
2622 * Patch up the size_index table if we have strange large alignment
2623 * requirements for the kmalloc array. This is only the case for
2624 * mips it seems. The standard arches will not generate any code here.
2625 *
2626 * Largest permitted alignment is 256 bytes due to the way we
2627 * handle the index determination for the smaller caches.
2628 *
2629 * Make sure that nothing crazy happens if someone starts tinkering
2630 * around with ARCH_KMALLOC_MINALIGN
2631 */
2632 BUILD_BUG_ON(KMALLOC_MIN_SIZE > 256 ||
2633 (KMALLOC_MIN_SIZE & (KMALLOC_MIN_SIZE - 1)));
2634
Christoph Lameter12ad6842007-07-17 04:03:28 -07002635 for (i = 8; i < KMALLOC_MIN_SIZE; i += 8)
Christoph Lameterf1b26332007-07-17 04:03:26 -07002636 size_index[(i - 1) / 8] = KMALLOC_SHIFT_LOW;
2637
Christoph Lameter81819f02007-05-06 14:49:36 -07002638 slab_state = UP;
2639
2640 /* Provide the correct kmalloc names now that the caches are up */
Christoph Lameteraadb4bc2007-10-16 01:24:38 -07002641 for (i = KMALLOC_SHIFT_LOW; i < PAGE_SHIFT; i++)
Christoph Lameter81819f02007-05-06 14:49:36 -07002642 kmalloc_caches[i]. name =
2643 kasprintf(GFP_KERNEL, "kmalloc-%d", 1 << i);
2644
2645#ifdef CONFIG_SMP
2646 register_cpu_notifier(&slab_notifier);
2647#endif
2648
Christoph Lameterbcf889f2007-05-10 03:15:44 -07002649 kmem_size = offsetof(struct kmem_cache, cpu_slab) +
Christoph Lameterdfb4f092007-10-16 01:26:05 -07002650 nr_cpu_ids * sizeof(struct kmem_cache_cpu);
Christoph Lameter81819f02007-05-06 14:49:36 -07002651
2652 printk(KERN_INFO "SLUB: Genslabs=%d, HWalign=%d, Order=%d-%d, MinObjects=%d,"
Christoph Lameter4b356be2007-06-16 10:16:13 -07002653 " CPUs=%d, Nodes=%d\n",
2654 caches, cache_line_size(),
Christoph Lameter81819f02007-05-06 14:49:36 -07002655 slub_min_order, slub_max_order, slub_min_objects,
2656 nr_cpu_ids, nr_node_ids);
2657}
2658
2659/*
2660 * Find a mergeable slab cache
2661 */
2662static int slab_unmergeable(struct kmem_cache *s)
2663{
2664 if (slub_nomerge || (s->flags & SLUB_NEVER_MERGE))
2665 return 1;
2666
Christoph Lameterc59def92007-05-16 22:10:50 -07002667 if (s->ctor)
Christoph Lameter81819f02007-05-06 14:49:36 -07002668 return 1;
2669
Christoph Lameter8ffa6872007-05-31 00:40:51 -07002670 /*
2671 * We may have set a slab to be unmergeable during bootstrap.
2672 */
2673 if (s->refcount < 0)
2674 return 1;
2675
Christoph Lameter81819f02007-05-06 14:49:36 -07002676 return 0;
2677}
2678
2679static struct kmem_cache *find_mergeable(size_t size,
Christoph Lameterba0268a2007-09-11 15:24:11 -07002680 size_t align, unsigned long flags, const char *name,
Christoph Lameterc59def92007-05-16 22:10:50 -07002681 void (*ctor)(void *, struct kmem_cache *, unsigned long))
Christoph Lameter81819f02007-05-06 14:49:36 -07002682{
Christoph Lameter5b95a4ac2007-07-17 04:03:19 -07002683 struct kmem_cache *s;
Christoph Lameter81819f02007-05-06 14:49:36 -07002684
2685 if (slub_nomerge || (flags & SLUB_NEVER_MERGE))
2686 return NULL;
2687
Christoph Lameterc59def92007-05-16 22:10:50 -07002688 if (ctor)
Christoph Lameter81819f02007-05-06 14:49:36 -07002689 return NULL;
2690
2691 size = ALIGN(size, sizeof(void *));
2692 align = calculate_alignment(flags, align, size);
2693 size = ALIGN(size, align);
Christoph Lameterba0268a2007-09-11 15:24:11 -07002694 flags = kmem_cache_flags(size, flags, name, NULL);
Christoph Lameter81819f02007-05-06 14:49:36 -07002695
Christoph Lameter5b95a4ac2007-07-17 04:03:19 -07002696 list_for_each_entry(s, &slab_caches, list) {
Christoph Lameter81819f02007-05-06 14:49:36 -07002697 if (slab_unmergeable(s))
2698 continue;
2699
2700 if (size > s->size)
2701 continue;
2702
Christoph Lameterba0268a2007-09-11 15:24:11 -07002703 if ((flags & SLUB_MERGE_SAME) != (s->flags & SLUB_MERGE_SAME))
Christoph Lameter81819f02007-05-06 14:49:36 -07002704 continue;
2705 /*
2706 * Check if alignment is compatible.
2707 * Courtesy of Adrian Drzewiecki
2708 */
2709 if ((s->size & ~(align -1)) != s->size)
2710 continue;
2711
2712 if (s->size - size >= sizeof(void *))
2713 continue;
2714
2715 return s;
2716 }
2717 return NULL;
2718}
2719
2720struct kmem_cache *kmem_cache_create(const char *name, size_t size,
2721 size_t align, unsigned long flags,
Paul Mundt20c2df82007-07-20 10:11:58 +09002722 void (*ctor)(void *, struct kmem_cache *, unsigned long))
Christoph Lameter81819f02007-05-06 14:49:36 -07002723{
2724 struct kmem_cache *s;
2725
2726 down_write(&slub_lock);
Christoph Lameterba0268a2007-09-11 15:24:11 -07002727 s = find_mergeable(size, align, flags, name, ctor);
Christoph Lameter81819f02007-05-06 14:49:36 -07002728 if (s) {
2729 s->refcount++;
2730 /*
2731 * Adjust the object sizes so that we clear
2732 * the complete object on kzalloc.
2733 */
2734 s->objsize = max(s->objsize, (int)size);
2735 s->inuse = max_t(int, s->inuse, ALIGN(size, sizeof(void *)));
Christoph Lametera0e1d1b2007-07-17 04:03:31 -07002736 up_write(&slub_lock);
Christoph Lameter81819f02007-05-06 14:49:36 -07002737 if (sysfs_slab_alias(s, name))
2738 goto err;
Christoph Lametera0e1d1b2007-07-17 04:03:31 -07002739 return s;
2740 }
2741 s = kmalloc(kmem_size, GFP_KERNEL);
2742 if (s) {
2743 if (kmem_cache_open(s, GFP_KERNEL, name,
Christoph Lameterc59def92007-05-16 22:10:50 -07002744 size, align, flags, ctor)) {
Christoph Lameter81819f02007-05-06 14:49:36 -07002745 list_add(&s->list, &slab_caches);
Christoph Lametera0e1d1b2007-07-17 04:03:31 -07002746 up_write(&slub_lock);
2747 if (sysfs_slab_add(s))
2748 goto err;
2749 return s;
2750 }
2751 kfree(s);
Christoph Lameter81819f02007-05-06 14:49:36 -07002752 }
2753 up_write(&slub_lock);
Christoph Lameter81819f02007-05-06 14:49:36 -07002754
2755err:
Christoph Lameter81819f02007-05-06 14:49:36 -07002756 if (flags & SLAB_PANIC)
2757 panic("Cannot create slabcache %s\n", name);
2758 else
2759 s = NULL;
2760 return s;
2761}
2762EXPORT_SYMBOL(kmem_cache_create);
2763
Christoph Lameter81819f02007-05-06 14:49:36 -07002764#ifdef CONFIG_SMP
Christoph Lameter27390bc2007-06-01 00:47:09 -07002765/*
Christoph Lameter672bba32007-05-09 02:32:39 -07002766 * Use the cpu notifier to insure that the cpu slabs are flushed when
2767 * necessary.
Christoph Lameter81819f02007-05-06 14:49:36 -07002768 */
2769static int __cpuinit slab_cpuup_callback(struct notifier_block *nfb,
2770 unsigned long action, void *hcpu)
2771{
2772 long cpu = (long)hcpu;
Christoph Lameter5b95a4ac2007-07-17 04:03:19 -07002773 struct kmem_cache *s;
2774 unsigned long flags;
Christoph Lameter81819f02007-05-06 14:49:36 -07002775
2776 switch (action) {
2777 case CPU_UP_CANCELED:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07002778 case CPU_UP_CANCELED_FROZEN:
Christoph Lameter81819f02007-05-06 14:49:36 -07002779 case CPU_DEAD:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07002780 case CPU_DEAD_FROZEN:
Christoph Lameter5b95a4ac2007-07-17 04:03:19 -07002781 down_read(&slub_lock);
2782 list_for_each_entry(s, &slab_caches, list) {
2783 local_irq_save(flags);
2784 __flush_cpu_slab(s, cpu);
2785 local_irq_restore(flags);
2786 }
2787 up_read(&slub_lock);
Christoph Lameter81819f02007-05-06 14:49:36 -07002788 break;
2789 default:
2790 break;
2791 }
2792 return NOTIFY_OK;
2793}
2794
2795static struct notifier_block __cpuinitdata slab_notifier =
2796 { &slab_cpuup_callback, NULL, 0 };
2797
2798#endif
2799
Christoph Lameter81819f02007-05-06 14:49:36 -07002800void *__kmalloc_track_caller(size_t size, gfp_t gfpflags, void *caller)
2801{
Christoph Lameteraadb4bc2007-10-16 01:24:38 -07002802 struct kmem_cache *s;
2803
2804 if (unlikely(size > PAGE_SIZE / 2))
2805 return (void *)__get_free_pages(gfpflags | __GFP_COMP,
2806 get_order(size));
2807 s = get_slab(size, gfpflags);
Christoph Lameter81819f02007-05-06 14:49:36 -07002808
Satyam Sharma2408c552007-10-16 01:24:44 -07002809 if (unlikely(ZERO_OR_NULL_PTR(s)))
Christoph Lameter6cb8f912007-07-17 04:03:22 -07002810 return s;
Christoph Lameter81819f02007-05-06 14:49:36 -07002811
Christoph Lameterce15fea2007-07-17 04:03:28 -07002812 return slab_alloc(s, gfpflags, -1, caller);
Christoph Lameter81819f02007-05-06 14:49:36 -07002813}
2814
2815void *__kmalloc_node_track_caller(size_t size, gfp_t gfpflags,
2816 int node, void *caller)
2817{
Christoph Lameteraadb4bc2007-10-16 01:24:38 -07002818 struct kmem_cache *s;
2819
2820 if (unlikely(size > PAGE_SIZE / 2))
2821 return (void *)__get_free_pages(gfpflags | __GFP_COMP,
2822 get_order(size));
2823 s = get_slab(size, gfpflags);
Christoph Lameter81819f02007-05-06 14:49:36 -07002824
Satyam Sharma2408c552007-10-16 01:24:44 -07002825 if (unlikely(ZERO_OR_NULL_PTR(s)))
Christoph Lameter6cb8f912007-07-17 04:03:22 -07002826 return s;
Christoph Lameter81819f02007-05-06 14:49:36 -07002827
Christoph Lameterce15fea2007-07-17 04:03:28 -07002828 return slab_alloc(s, gfpflags, node, caller);
Christoph Lameter81819f02007-05-06 14:49:36 -07002829}
2830
Christoph Lameter41ecc552007-05-09 02:32:44 -07002831#if defined(CONFIG_SYSFS) && defined(CONFIG_SLUB_DEBUG)
Christoph Lameter434e2452007-07-17 04:03:30 -07002832static int validate_slab(struct kmem_cache *s, struct page *page,
2833 unsigned long *map)
Christoph Lameter53e15af2007-05-06 14:49:43 -07002834{
2835 void *p;
2836 void *addr = page_address(page);
Christoph Lameter53e15af2007-05-06 14:49:43 -07002837
2838 if (!check_slab(s, page) ||
2839 !on_freelist(s, page, NULL))
2840 return 0;
2841
2842 /* Now we know that a valid freelist exists */
2843 bitmap_zero(map, s->objects);
2844
Christoph Lameter7656c722007-05-09 02:32:40 -07002845 for_each_free_object(p, s, page->freelist) {
2846 set_bit(slab_index(p, s, addr), map);
Christoph Lameter53e15af2007-05-06 14:49:43 -07002847 if (!check_object(s, page, p, 0))
2848 return 0;
2849 }
2850
Christoph Lameter7656c722007-05-09 02:32:40 -07002851 for_each_object(p, s, addr)
2852 if (!test_bit(slab_index(p, s, addr), map))
Christoph Lameter53e15af2007-05-06 14:49:43 -07002853 if (!check_object(s, page, p, 1))
2854 return 0;
2855 return 1;
2856}
2857
Christoph Lameter434e2452007-07-17 04:03:30 -07002858static void validate_slab_slab(struct kmem_cache *s, struct page *page,
2859 unsigned long *map)
Christoph Lameter53e15af2007-05-06 14:49:43 -07002860{
2861 if (slab_trylock(page)) {
Christoph Lameter434e2452007-07-17 04:03:30 -07002862 validate_slab(s, page, map);
Christoph Lameter53e15af2007-05-06 14:49:43 -07002863 slab_unlock(page);
2864 } else
2865 printk(KERN_INFO "SLUB %s: Skipped busy slab 0x%p\n",
2866 s->name, page);
2867
2868 if (s->flags & DEBUG_DEFAULT_FLAGS) {
Christoph Lameter35e5d7e2007-05-09 02:32:42 -07002869 if (!SlabDebug(page))
2870 printk(KERN_ERR "SLUB %s: SlabDebug not set "
Christoph Lameter53e15af2007-05-06 14:49:43 -07002871 "on slab 0x%p\n", s->name, page);
2872 } else {
Christoph Lameter35e5d7e2007-05-09 02:32:42 -07002873 if (SlabDebug(page))
2874 printk(KERN_ERR "SLUB %s: SlabDebug set on "
Christoph Lameter53e15af2007-05-06 14:49:43 -07002875 "slab 0x%p\n", s->name, page);
2876 }
2877}
2878
Christoph Lameter434e2452007-07-17 04:03:30 -07002879static int validate_slab_node(struct kmem_cache *s,
2880 struct kmem_cache_node *n, unsigned long *map)
Christoph Lameter53e15af2007-05-06 14:49:43 -07002881{
2882 unsigned long count = 0;
2883 struct page *page;
2884 unsigned long flags;
2885
2886 spin_lock_irqsave(&n->list_lock, flags);
2887
2888 list_for_each_entry(page, &n->partial, lru) {
Christoph Lameter434e2452007-07-17 04:03:30 -07002889 validate_slab_slab(s, page, map);
Christoph Lameter53e15af2007-05-06 14:49:43 -07002890 count++;
2891 }
2892 if (count != n->nr_partial)
2893 printk(KERN_ERR "SLUB %s: %ld partial slabs counted but "
2894 "counter=%ld\n", s->name, count, n->nr_partial);
2895
2896 if (!(s->flags & SLAB_STORE_USER))
2897 goto out;
2898
2899 list_for_each_entry(page, &n->full, lru) {
Christoph Lameter434e2452007-07-17 04:03:30 -07002900 validate_slab_slab(s, page, map);
Christoph Lameter53e15af2007-05-06 14:49:43 -07002901 count++;
2902 }
2903 if (count != atomic_long_read(&n->nr_slabs))
2904 printk(KERN_ERR "SLUB: %s %ld slabs counted but "
2905 "counter=%ld\n", s->name, count,
2906 atomic_long_read(&n->nr_slabs));
2907
2908out:
2909 spin_unlock_irqrestore(&n->list_lock, flags);
2910 return count;
2911}
2912
Christoph Lameter434e2452007-07-17 04:03:30 -07002913static long validate_slab_cache(struct kmem_cache *s)
Christoph Lameter53e15af2007-05-06 14:49:43 -07002914{
2915 int node;
2916 unsigned long count = 0;
Christoph Lameter434e2452007-07-17 04:03:30 -07002917 unsigned long *map = kmalloc(BITS_TO_LONGS(s->objects) *
2918 sizeof(unsigned long), GFP_KERNEL);
2919
2920 if (!map)
2921 return -ENOMEM;
Christoph Lameter53e15af2007-05-06 14:49:43 -07002922
2923 flush_all(s);
Christoph Lameterf64dc582007-10-16 01:25:33 -07002924 for_each_node_state(node, N_NORMAL_MEMORY) {
Christoph Lameter53e15af2007-05-06 14:49:43 -07002925 struct kmem_cache_node *n = get_node(s, node);
2926
Christoph Lameter434e2452007-07-17 04:03:30 -07002927 count += validate_slab_node(s, n, map);
Christoph Lameter53e15af2007-05-06 14:49:43 -07002928 }
Christoph Lameter434e2452007-07-17 04:03:30 -07002929 kfree(map);
Christoph Lameter53e15af2007-05-06 14:49:43 -07002930 return count;
2931}
2932
Christoph Lameterb3459702007-05-09 02:32:41 -07002933#ifdef SLUB_RESILIENCY_TEST
2934static void resiliency_test(void)
2935{
2936 u8 *p;
2937
2938 printk(KERN_ERR "SLUB resiliency testing\n");
2939 printk(KERN_ERR "-----------------------\n");
2940 printk(KERN_ERR "A. Corruption after allocation\n");
2941
2942 p = kzalloc(16, GFP_KERNEL);
2943 p[16] = 0x12;
2944 printk(KERN_ERR "\n1. kmalloc-16: Clobber Redzone/next pointer"
2945 " 0x12->0x%p\n\n", p + 16);
2946
2947 validate_slab_cache(kmalloc_caches + 4);
2948
2949 /* Hmmm... The next two are dangerous */
2950 p = kzalloc(32, GFP_KERNEL);
2951 p[32 + sizeof(void *)] = 0x34;
2952 printk(KERN_ERR "\n2. kmalloc-32: Clobber next pointer/next slab"
2953 " 0x34 -> -0x%p\n", p);
2954 printk(KERN_ERR "If allocated object is overwritten then not detectable\n\n");
2955
2956 validate_slab_cache(kmalloc_caches + 5);
2957 p = kzalloc(64, GFP_KERNEL);
2958 p += 64 + (get_cycles() & 0xff) * sizeof(void *);
2959 *p = 0x56;
2960 printk(KERN_ERR "\n3. kmalloc-64: corrupting random byte 0x56->0x%p\n",
2961 p);
2962 printk(KERN_ERR "If allocated object is overwritten then not detectable\n\n");
2963 validate_slab_cache(kmalloc_caches + 6);
2964
2965 printk(KERN_ERR "\nB. Corruption after free\n");
2966 p = kzalloc(128, GFP_KERNEL);
2967 kfree(p);
2968 *p = 0x78;
2969 printk(KERN_ERR "1. kmalloc-128: Clobber first word 0x78->0x%p\n\n", p);
2970 validate_slab_cache(kmalloc_caches + 7);
2971
2972 p = kzalloc(256, GFP_KERNEL);
2973 kfree(p);
2974 p[50] = 0x9a;
2975 printk(KERN_ERR "\n2. kmalloc-256: Clobber 50th byte 0x9a->0x%p\n\n", p);
2976 validate_slab_cache(kmalloc_caches + 8);
2977
2978 p = kzalloc(512, GFP_KERNEL);
2979 kfree(p);
2980 p[512] = 0xab;
2981 printk(KERN_ERR "\n3. kmalloc-512: Clobber redzone 0xab->0x%p\n\n", p);
2982 validate_slab_cache(kmalloc_caches + 9);
2983}
2984#else
2985static void resiliency_test(void) {};
2986#endif
2987
Christoph Lameter88a420e2007-05-06 14:49:45 -07002988/*
Christoph Lameter672bba32007-05-09 02:32:39 -07002989 * Generate lists of code addresses where slabcache objects are allocated
Christoph Lameter88a420e2007-05-06 14:49:45 -07002990 * and freed.
2991 */
2992
2993struct location {
2994 unsigned long count;
2995 void *addr;
Christoph Lameter45edfa52007-05-09 02:32:45 -07002996 long long sum_time;
2997 long min_time;
2998 long max_time;
2999 long min_pid;
3000 long max_pid;
3001 cpumask_t cpus;
3002 nodemask_t nodes;
Christoph Lameter88a420e2007-05-06 14:49:45 -07003003};
3004
3005struct loc_track {
3006 unsigned long max;
3007 unsigned long count;
3008 struct location *loc;
3009};
3010
3011static void free_loc_track(struct loc_track *t)
3012{
3013 if (t->max)
3014 free_pages((unsigned long)t->loc,
3015 get_order(sizeof(struct location) * t->max));
3016}
3017
Christoph Lameter68dff6a2007-07-17 04:03:20 -07003018static int alloc_loc_track(struct loc_track *t, unsigned long max, gfp_t flags)
Christoph Lameter88a420e2007-05-06 14:49:45 -07003019{
3020 struct location *l;
3021 int order;
3022
Christoph Lameter88a420e2007-05-06 14:49:45 -07003023 order = get_order(sizeof(struct location) * max);
3024
Christoph Lameter68dff6a2007-07-17 04:03:20 -07003025 l = (void *)__get_free_pages(flags, order);
Christoph Lameter88a420e2007-05-06 14:49:45 -07003026 if (!l)
3027 return 0;
3028
3029 if (t->count) {
3030 memcpy(l, t->loc, sizeof(struct location) * t->count);
3031 free_loc_track(t);
3032 }
3033 t->max = max;
3034 t->loc = l;
3035 return 1;
3036}
3037
3038static int add_location(struct loc_track *t, struct kmem_cache *s,
Christoph Lameter45edfa52007-05-09 02:32:45 -07003039 const struct track *track)
Christoph Lameter88a420e2007-05-06 14:49:45 -07003040{
3041 long start, end, pos;
3042 struct location *l;
3043 void *caddr;
Christoph Lameter45edfa52007-05-09 02:32:45 -07003044 unsigned long age = jiffies - track->when;
Christoph Lameter88a420e2007-05-06 14:49:45 -07003045
3046 start = -1;
3047 end = t->count;
3048
3049 for ( ; ; ) {
3050 pos = start + (end - start + 1) / 2;
3051
3052 /*
3053 * There is nothing at "end". If we end up there
3054 * we need to add something to before end.
3055 */
3056 if (pos == end)
3057 break;
3058
3059 caddr = t->loc[pos].addr;
Christoph Lameter45edfa52007-05-09 02:32:45 -07003060 if (track->addr == caddr) {
3061
3062 l = &t->loc[pos];
3063 l->count++;
3064 if (track->when) {
3065 l->sum_time += age;
3066 if (age < l->min_time)
3067 l->min_time = age;
3068 if (age > l->max_time)
3069 l->max_time = age;
3070
3071 if (track->pid < l->min_pid)
3072 l->min_pid = track->pid;
3073 if (track->pid > l->max_pid)
3074 l->max_pid = track->pid;
3075
3076 cpu_set(track->cpu, l->cpus);
3077 }
3078 node_set(page_to_nid(virt_to_page(track)), l->nodes);
Christoph Lameter88a420e2007-05-06 14:49:45 -07003079 return 1;
3080 }
3081
Christoph Lameter45edfa52007-05-09 02:32:45 -07003082 if (track->addr < caddr)
Christoph Lameter88a420e2007-05-06 14:49:45 -07003083 end = pos;
3084 else
3085 start = pos;
3086 }
3087
3088 /*
Christoph Lameter672bba32007-05-09 02:32:39 -07003089 * Not found. Insert new tracking element.
Christoph Lameter88a420e2007-05-06 14:49:45 -07003090 */
Christoph Lameter68dff6a2007-07-17 04:03:20 -07003091 if (t->count >= t->max && !alloc_loc_track(t, 2 * t->max, GFP_ATOMIC))
Christoph Lameter88a420e2007-05-06 14:49:45 -07003092 return 0;
3093
3094 l = t->loc + pos;
3095 if (pos < t->count)
3096 memmove(l + 1, l,
3097 (t->count - pos) * sizeof(struct location));
3098 t->count++;
3099 l->count = 1;
Christoph Lameter45edfa52007-05-09 02:32:45 -07003100 l->addr = track->addr;
3101 l->sum_time = age;
3102 l->min_time = age;
3103 l->max_time = age;
3104 l->min_pid = track->pid;
3105 l->max_pid = track->pid;
3106 cpus_clear(l->cpus);
3107 cpu_set(track->cpu, l->cpus);
3108 nodes_clear(l->nodes);
3109 node_set(page_to_nid(virt_to_page(track)), l->nodes);
Christoph Lameter88a420e2007-05-06 14:49:45 -07003110 return 1;
3111}
3112
3113static void process_slab(struct loc_track *t, struct kmem_cache *s,
3114 struct page *page, enum track_item alloc)
3115{
3116 void *addr = page_address(page);
Christoph Lameter7656c722007-05-09 02:32:40 -07003117 DECLARE_BITMAP(map, s->objects);
Christoph Lameter88a420e2007-05-06 14:49:45 -07003118 void *p;
3119
3120 bitmap_zero(map, s->objects);
Christoph Lameter7656c722007-05-09 02:32:40 -07003121 for_each_free_object(p, s, page->freelist)
3122 set_bit(slab_index(p, s, addr), map);
Christoph Lameter88a420e2007-05-06 14:49:45 -07003123
Christoph Lameter7656c722007-05-09 02:32:40 -07003124 for_each_object(p, s, addr)
Christoph Lameter45edfa52007-05-09 02:32:45 -07003125 if (!test_bit(slab_index(p, s, addr), map))
3126 add_location(t, s, get_track(s, p, alloc));
Christoph Lameter88a420e2007-05-06 14:49:45 -07003127}
3128
3129static int list_locations(struct kmem_cache *s, char *buf,
3130 enum track_item alloc)
3131{
3132 int n = 0;
3133 unsigned long i;
Christoph Lameter68dff6a2007-07-17 04:03:20 -07003134 struct loc_track t = { 0, 0, NULL };
Christoph Lameter88a420e2007-05-06 14:49:45 -07003135 int node;
3136
Christoph Lameter68dff6a2007-07-17 04:03:20 -07003137 if (!alloc_loc_track(&t, PAGE_SIZE / sizeof(struct location),
3138 GFP_KERNEL))
3139 return sprintf(buf, "Out of memory\n");
Christoph Lameter88a420e2007-05-06 14:49:45 -07003140
3141 /* Push back cpu slabs */
3142 flush_all(s);
3143
Christoph Lameterf64dc582007-10-16 01:25:33 -07003144 for_each_node_state(node, N_NORMAL_MEMORY) {
Christoph Lameter88a420e2007-05-06 14:49:45 -07003145 struct kmem_cache_node *n = get_node(s, node);
3146 unsigned long flags;
3147 struct page *page;
3148
Christoph Lameter9e869432007-08-22 14:01:56 -07003149 if (!atomic_long_read(&n->nr_slabs))
Christoph Lameter88a420e2007-05-06 14:49:45 -07003150 continue;
3151
3152 spin_lock_irqsave(&n->list_lock, flags);
3153 list_for_each_entry(page, &n->partial, lru)
3154 process_slab(&t, s, page, alloc);
3155 list_for_each_entry(page, &n->full, lru)
3156 process_slab(&t, s, page, alloc);
3157 spin_unlock_irqrestore(&n->list_lock, flags);
3158 }
3159
3160 for (i = 0; i < t.count; i++) {
Christoph Lameter45edfa52007-05-09 02:32:45 -07003161 struct location *l = &t.loc[i];
Christoph Lameter88a420e2007-05-06 14:49:45 -07003162
3163 if (n > PAGE_SIZE - 100)
3164 break;
Christoph Lameter45edfa52007-05-09 02:32:45 -07003165 n += sprintf(buf + n, "%7ld ", l->count);
3166
3167 if (l->addr)
3168 n += sprint_symbol(buf + n, (unsigned long)l->addr);
Christoph Lameter88a420e2007-05-06 14:49:45 -07003169 else
3170 n += sprintf(buf + n, "<not-available>");
Christoph Lameter45edfa52007-05-09 02:32:45 -07003171
3172 if (l->sum_time != l->min_time) {
3173 unsigned long remainder;
3174
3175 n += sprintf(buf + n, " age=%ld/%ld/%ld",
3176 l->min_time,
3177 div_long_long_rem(l->sum_time, l->count, &remainder),
3178 l->max_time);
3179 } else
3180 n += sprintf(buf + n, " age=%ld",
3181 l->min_time);
3182
3183 if (l->min_pid != l->max_pid)
3184 n += sprintf(buf + n, " pid=%ld-%ld",
3185 l->min_pid, l->max_pid);
3186 else
3187 n += sprintf(buf + n, " pid=%ld",
3188 l->min_pid);
3189
Christoph Lameter84966342007-06-23 17:16:32 -07003190 if (num_online_cpus() > 1 && !cpus_empty(l->cpus) &&
3191 n < PAGE_SIZE - 60) {
Christoph Lameter45edfa52007-05-09 02:32:45 -07003192 n += sprintf(buf + n, " cpus=");
3193 n += cpulist_scnprintf(buf + n, PAGE_SIZE - n - 50,
3194 l->cpus);
3195 }
3196
Christoph Lameter84966342007-06-23 17:16:32 -07003197 if (num_online_nodes() > 1 && !nodes_empty(l->nodes) &&
3198 n < PAGE_SIZE - 60) {
Christoph Lameter45edfa52007-05-09 02:32:45 -07003199 n += sprintf(buf + n, " nodes=");
3200 n += nodelist_scnprintf(buf + n, PAGE_SIZE - n - 50,
3201 l->nodes);
3202 }
3203
Christoph Lameter88a420e2007-05-06 14:49:45 -07003204 n += sprintf(buf + n, "\n");
3205 }
3206
3207 free_loc_track(&t);
3208 if (!t.count)
3209 n += sprintf(buf, "No data\n");
3210 return n;
3211}
3212
Christoph Lameter81819f02007-05-06 14:49:36 -07003213static unsigned long count_partial(struct kmem_cache_node *n)
3214{
3215 unsigned long flags;
3216 unsigned long x = 0;
3217 struct page *page;
3218
3219 spin_lock_irqsave(&n->list_lock, flags);
3220 list_for_each_entry(page, &n->partial, lru)
3221 x += page->inuse;
3222 spin_unlock_irqrestore(&n->list_lock, flags);
3223 return x;
3224}
3225
3226enum slab_stat_type {
3227 SL_FULL,
3228 SL_PARTIAL,
3229 SL_CPU,
3230 SL_OBJECTS
3231};
3232
3233#define SO_FULL (1 << SL_FULL)
3234#define SO_PARTIAL (1 << SL_PARTIAL)
3235#define SO_CPU (1 << SL_CPU)
3236#define SO_OBJECTS (1 << SL_OBJECTS)
3237
3238static unsigned long slab_objects(struct kmem_cache *s,
3239 char *buf, unsigned long flags)
3240{
3241 unsigned long total = 0;
3242 int cpu;
3243 int node;
3244 int x;
3245 unsigned long *nodes;
3246 unsigned long *per_cpu;
3247
3248 nodes = kzalloc(2 * sizeof(unsigned long) * nr_node_ids, GFP_KERNEL);
3249 per_cpu = nodes + nr_node_ids;
3250
3251 for_each_possible_cpu(cpu) {
Christoph Lameterdfb4f092007-10-16 01:26:05 -07003252 struct page *page;
3253 struct kmem_cache_cpu *c = get_cpu_slab(s, cpu);
Christoph Lameter81819f02007-05-06 14:49:36 -07003254
Christoph Lameterdfb4f092007-10-16 01:26:05 -07003255 if (!c)
3256 continue;
3257
3258 page = c->page;
Christoph Lameter81819f02007-05-06 14:49:36 -07003259 if (page) {
Christoph Lameter81819f02007-05-06 14:49:36 -07003260 if (flags & SO_CPU) {
3261 int x = 0;
3262
3263 if (flags & SO_OBJECTS)
3264 x = page->inuse;
3265 else
3266 x = 1;
3267 total += x;
Christoph Lameterdfb4f092007-10-16 01:26:05 -07003268 nodes[c->node] += x;
Christoph Lameter81819f02007-05-06 14:49:36 -07003269 }
Christoph Lameterdfb4f092007-10-16 01:26:05 -07003270 per_cpu[c->node]++;
Christoph Lameter81819f02007-05-06 14:49:36 -07003271 }
3272 }
3273
Christoph Lameterf64dc582007-10-16 01:25:33 -07003274 for_each_node_state(node, N_NORMAL_MEMORY) {
Christoph Lameter81819f02007-05-06 14:49:36 -07003275 struct kmem_cache_node *n = get_node(s, node);
3276
3277 if (flags & SO_PARTIAL) {
3278 if (flags & SO_OBJECTS)
3279 x = count_partial(n);
3280 else
3281 x = n->nr_partial;
3282 total += x;
3283 nodes[node] += x;
3284 }
3285
3286 if (flags & SO_FULL) {
Christoph Lameter9e869432007-08-22 14:01:56 -07003287 int full_slabs = atomic_long_read(&n->nr_slabs)
Christoph Lameter81819f02007-05-06 14:49:36 -07003288 - per_cpu[node]
3289 - n->nr_partial;
3290
3291 if (flags & SO_OBJECTS)
3292 x = full_slabs * s->objects;
3293 else
3294 x = full_slabs;
3295 total += x;
3296 nodes[node] += x;
3297 }
3298 }
3299
3300 x = sprintf(buf, "%lu", total);
3301#ifdef CONFIG_NUMA
Christoph Lameterf64dc582007-10-16 01:25:33 -07003302 for_each_node_state(node, N_NORMAL_MEMORY)
Christoph Lameter81819f02007-05-06 14:49:36 -07003303 if (nodes[node])
3304 x += sprintf(buf + x, " N%d=%lu",
3305 node, nodes[node]);
3306#endif
3307 kfree(nodes);
3308 return x + sprintf(buf + x, "\n");
3309}
3310
3311static int any_slab_objects(struct kmem_cache *s)
3312{
3313 int node;
3314 int cpu;
3315
Christoph Lameterdfb4f092007-10-16 01:26:05 -07003316 for_each_possible_cpu(cpu) {
3317 struct kmem_cache_cpu *c = get_cpu_slab(s, cpu);
Christoph Lameter81819f02007-05-06 14:49:36 -07003318
Christoph Lameterdfb4f092007-10-16 01:26:05 -07003319 if (c && c->page)
3320 return 1;
3321 }
3322
3323 for_each_online_node(node) {
Christoph Lameter81819f02007-05-06 14:49:36 -07003324 struct kmem_cache_node *n = get_node(s, node);
3325
Christoph Lameterdfb4f092007-10-16 01:26:05 -07003326 if (!n)
3327 continue;
3328
Christoph Lameter9e869432007-08-22 14:01:56 -07003329 if (n->nr_partial || atomic_long_read(&n->nr_slabs))
Christoph Lameter81819f02007-05-06 14:49:36 -07003330 return 1;
3331 }
3332 return 0;
3333}
3334
3335#define to_slab_attr(n) container_of(n, struct slab_attribute, attr)
3336#define to_slab(n) container_of(n, struct kmem_cache, kobj);
3337
3338struct slab_attribute {
3339 struct attribute attr;
3340 ssize_t (*show)(struct kmem_cache *s, char *buf);
3341 ssize_t (*store)(struct kmem_cache *s, const char *x, size_t count);
3342};
3343
3344#define SLAB_ATTR_RO(_name) \
3345 static struct slab_attribute _name##_attr = __ATTR_RO(_name)
3346
3347#define SLAB_ATTR(_name) \
3348 static struct slab_attribute _name##_attr = \
3349 __ATTR(_name, 0644, _name##_show, _name##_store)
3350
Christoph Lameter81819f02007-05-06 14:49:36 -07003351static ssize_t slab_size_show(struct kmem_cache *s, char *buf)
3352{
3353 return sprintf(buf, "%d\n", s->size);
3354}
3355SLAB_ATTR_RO(slab_size);
3356
3357static ssize_t align_show(struct kmem_cache *s, char *buf)
3358{
3359 return sprintf(buf, "%d\n", s->align);
3360}
3361SLAB_ATTR_RO(align);
3362
3363static ssize_t object_size_show(struct kmem_cache *s, char *buf)
3364{
3365 return sprintf(buf, "%d\n", s->objsize);
3366}
3367SLAB_ATTR_RO(object_size);
3368
3369static ssize_t objs_per_slab_show(struct kmem_cache *s, char *buf)
3370{
3371 return sprintf(buf, "%d\n", s->objects);
3372}
3373SLAB_ATTR_RO(objs_per_slab);
3374
3375static ssize_t order_show(struct kmem_cache *s, char *buf)
3376{
3377 return sprintf(buf, "%d\n", s->order);
3378}
3379SLAB_ATTR_RO(order);
3380
3381static ssize_t ctor_show(struct kmem_cache *s, char *buf)
3382{
3383 if (s->ctor) {
3384 int n = sprint_symbol(buf, (unsigned long)s->ctor);
3385
3386 return n + sprintf(buf + n, "\n");
3387 }
3388 return 0;
3389}
3390SLAB_ATTR_RO(ctor);
3391
Christoph Lameter81819f02007-05-06 14:49:36 -07003392static ssize_t aliases_show(struct kmem_cache *s, char *buf)
3393{
3394 return sprintf(buf, "%d\n", s->refcount - 1);
3395}
3396SLAB_ATTR_RO(aliases);
3397
3398static ssize_t slabs_show(struct kmem_cache *s, char *buf)
3399{
3400 return slab_objects(s, buf, SO_FULL|SO_PARTIAL|SO_CPU);
3401}
3402SLAB_ATTR_RO(slabs);
3403
3404static ssize_t partial_show(struct kmem_cache *s, char *buf)
3405{
3406 return slab_objects(s, buf, SO_PARTIAL);
3407}
3408SLAB_ATTR_RO(partial);
3409
3410static ssize_t cpu_slabs_show(struct kmem_cache *s, char *buf)
3411{
3412 return slab_objects(s, buf, SO_CPU);
3413}
3414SLAB_ATTR_RO(cpu_slabs);
3415
3416static ssize_t objects_show(struct kmem_cache *s, char *buf)
3417{
3418 return slab_objects(s, buf, SO_FULL|SO_PARTIAL|SO_CPU|SO_OBJECTS);
3419}
3420SLAB_ATTR_RO(objects);
3421
3422static ssize_t sanity_checks_show(struct kmem_cache *s, char *buf)
3423{
3424 return sprintf(buf, "%d\n", !!(s->flags & SLAB_DEBUG_FREE));
3425}
3426
3427static ssize_t sanity_checks_store(struct kmem_cache *s,
3428 const char *buf, size_t length)
3429{
3430 s->flags &= ~SLAB_DEBUG_FREE;
3431 if (buf[0] == '1')
3432 s->flags |= SLAB_DEBUG_FREE;
3433 return length;
3434}
3435SLAB_ATTR(sanity_checks);
3436
3437static ssize_t trace_show(struct kmem_cache *s, char *buf)
3438{
3439 return sprintf(buf, "%d\n", !!(s->flags & SLAB_TRACE));
3440}
3441
3442static ssize_t trace_store(struct kmem_cache *s, const char *buf,
3443 size_t length)
3444{
3445 s->flags &= ~SLAB_TRACE;
3446 if (buf[0] == '1')
3447 s->flags |= SLAB_TRACE;
3448 return length;
3449}
3450SLAB_ATTR(trace);
3451
3452static ssize_t reclaim_account_show(struct kmem_cache *s, char *buf)
3453{
3454 return sprintf(buf, "%d\n", !!(s->flags & SLAB_RECLAIM_ACCOUNT));
3455}
3456
3457static ssize_t reclaim_account_store(struct kmem_cache *s,
3458 const char *buf, size_t length)
3459{
3460 s->flags &= ~SLAB_RECLAIM_ACCOUNT;
3461 if (buf[0] == '1')
3462 s->flags |= SLAB_RECLAIM_ACCOUNT;
3463 return length;
3464}
3465SLAB_ATTR(reclaim_account);
3466
3467static ssize_t hwcache_align_show(struct kmem_cache *s, char *buf)
3468{
Christoph Lameter5af60832007-05-06 14:49:56 -07003469 return sprintf(buf, "%d\n", !!(s->flags & SLAB_HWCACHE_ALIGN));
Christoph Lameter81819f02007-05-06 14:49:36 -07003470}
3471SLAB_ATTR_RO(hwcache_align);
3472
3473#ifdef CONFIG_ZONE_DMA
3474static ssize_t cache_dma_show(struct kmem_cache *s, char *buf)
3475{
3476 return sprintf(buf, "%d\n", !!(s->flags & SLAB_CACHE_DMA));
3477}
3478SLAB_ATTR_RO(cache_dma);
3479#endif
3480
3481static ssize_t destroy_by_rcu_show(struct kmem_cache *s, char *buf)
3482{
3483 return sprintf(buf, "%d\n", !!(s->flags & SLAB_DESTROY_BY_RCU));
3484}
3485SLAB_ATTR_RO(destroy_by_rcu);
3486
3487static ssize_t red_zone_show(struct kmem_cache *s, char *buf)
3488{
3489 return sprintf(buf, "%d\n", !!(s->flags & SLAB_RED_ZONE));
3490}
3491
3492static ssize_t red_zone_store(struct kmem_cache *s,
3493 const char *buf, size_t length)
3494{
3495 if (any_slab_objects(s))
3496 return -EBUSY;
3497
3498 s->flags &= ~SLAB_RED_ZONE;
3499 if (buf[0] == '1')
3500 s->flags |= SLAB_RED_ZONE;
3501 calculate_sizes(s);
3502 return length;
3503}
3504SLAB_ATTR(red_zone);
3505
3506static ssize_t poison_show(struct kmem_cache *s, char *buf)
3507{
3508 return sprintf(buf, "%d\n", !!(s->flags & SLAB_POISON));
3509}
3510
3511static ssize_t poison_store(struct kmem_cache *s,
3512 const char *buf, size_t length)
3513{
3514 if (any_slab_objects(s))
3515 return -EBUSY;
3516
3517 s->flags &= ~SLAB_POISON;
3518 if (buf[0] == '1')
3519 s->flags |= SLAB_POISON;
3520 calculate_sizes(s);
3521 return length;
3522}
3523SLAB_ATTR(poison);
3524
3525static ssize_t store_user_show(struct kmem_cache *s, char *buf)
3526{
3527 return sprintf(buf, "%d\n", !!(s->flags & SLAB_STORE_USER));
3528}
3529
3530static ssize_t store_user_store(struct kmem_cache *s,
3531 const char *buf, size_t length)
3532{
3533 if (any_slab_objects(s))
3534 return -EBUSY;
3535
3536 s->flags &= ~SLAB_STORE_USER;
3537 if (buf[0] == '1')
3538 s->flags |= SLAB_STORE_USER;
3539 calculate_sizes(s);
3540 return length;
3541}
3542SLAB_ATTR(store_user);
3543
Christoph Lameter53e15af2007-05-06 14:49:43 -07003544static ssize_t validate_show(struct kmem_cache *s, char *buf)
3545{
3546 return 0;
3547}
3548
3549static ssize_t validate_store(struct kmem_cache *s,
3550 const char *buf, size_t length)
3551{
Christoph Lameter434e2452007-07-17 04:03:30 -07003552 int ret = -EINVAL;
3553
3554 if (buf[0] == '1') {
3555 ret = validate_slab_cache(s);
3556 if (ret >= 0)
3557 ret = length;
3558 }
3559 return ret;
Christoph Lameter53e15af2007-05-06 14:49:43 -07003560}
3561SLAB_ATTR(validate);
3562
Christoph Lameter2086d262007-05-06 14:49:46 -07003563static ssize_t shrink_show(struct kmem_cache *s, char *buf)
3564{
3565 return 0;
3566}
3567
3568static ssize_t shrink_store(struct kmem_cache *s,
3569 const char *buf, size_t length)
3570{
3571 if (buf[0] == '1') {
3572 int rc = kmem_cache_shrink(s);
3573
3574 if (rc)
3575 return rc;
3576 } else
3577 return -EINVAL;
3578 return length;
3579}
3580SLAB_ATTR(shrink);
3581
Christoph Lameter88a420e2007-05-06 14:49:45 -07003582static ssize_t alloc_calls_show(struct kmem_cache *s, char *buf)
3583{
3584 if (!(s->flags & SLAB_STORE_USER))
3585 return -ENOSYS;
3586 return list_locations(s, buf, TRACK_ALLOC);
3587}
3588SLAB_ATTR_RO(alloc_calls);
3589
3590static ssize_t free_calls_show(struct kmem_cache *s, char *buf)
3591{
3592 if (!(s->flags & SLAB_STORE_USER))
3593 return -ENOSYS;
3594 return list_locations(s, buf, TRACK_FREE);
3595}
3596SLAB_ATTR_RO(free_calls);
3597
Christoph Lameter81819f02007-05-06 14:49:36 -07003598#ifdef CONFIG_NUMA
3599static ssize_t defrag_ratio_show(struct kmem_cache *s, char *buf)
3600{
3601 return sprintf(buf, "%d\n", s->defrag_ratio / 10);
3602}
3603
3604static ssize_t defrag_ratio_store(struct kmem_cache *s,
3605 const char *buf, size_t length)
3606{
3607 int n = simple_strtoul(buf, NULL, 10);
3608
3609 if (n < 100)
3610 s->defrag_ratio = n * 10;
3611 return length;
3612}
3613SLAB_ATTR(defrag_ratio);
3614#endif
3615
3616static struct attribute * slab_attrs[] = {
3617 &slab_size_attr.attr,
3618 &object_size_attr.attr,
3619 &objs_per_slab_attr.attr,
3620 &order_attr.attr,
3621 &objects_attr.attr,
3622 &slabs_attr.attr,
3623 &partial_attr.attr,
3624 &cpu_slabs_attr.attr,
3625 &ctor_attr.attr,
Christoph Lameter81819f02007-05-06 14:49:36 -07003626 &aliases_attr.attr,
3627 &align_attr.attr,
3628 &sanity_checks_attr.attr,
3629 &trace_attr.attr,
3630 &hwcache_align_attr.attr,
3631 &reclaim_account_attr.attr,
3632 &destroy_by_rcu_attr.attr,
3633 &red_zone_attr.attr,
3634 &poison_attr.attr,
3635 &store_user_attr.attr,
Christoph Lameter53e15af2007-05-06 14:49:43 -07003636 &validate_attr.attr,
Christoph Lameter2086d262007-05-06 14:49:46 -07003637 &shrink_attr.attr,
Christoph Lameter88a420e2007-05-06 14:49:45 -07003638 &alloc_calls_attr.attr,
3639 &free_calls_attr.attr,
Christoph Lameter81819f02007-05-06 14:49:36 -07003640#ifdef CONFIG_ZONE_DMA
3641 &cache_dma_attr.attr,
3642#endif
3643#ifdef CONFIG_NUMA
3644 &defrag_ratio_attr.attr,
3645#endif
3646 NULL
3647};
3648
3649static struct attribute_group slab_attr_group = {
3650 .attrs = slab_attrs,
3651};
3652
3653static ssize_t slab_attr_show(struct kobject *kobj,
3654 struct attribute *attr,
3655 char *buf)
3656{
3657 struct slab_attribute *attribute;
3658 struct kmem_cache *s;
3659 int err;
3660
3661 attribute = to_slab_attr(attr);
3662 s = to_slab(kobj);
3663
3664 if (!attribute->show)
3665 return -EIO;
3666
3667 err = attribute->show(s, buf);
3668
3669 return err;
3670}
3671
3672static ssize_t slab_attr_store(struct kobject *kobj,
3673 struct attribute *attr,
3674 const char *buf, size_t len)
3675{
3676 struct slab_attribute *attribute;
3677 struct kmem_cache *s;
3678 int err;
3679
3680 attribute = to_slab_attr(attr);
3681 s = to_slab(kobj);
3682
3683 if (!attribute->store)
3684 return -EIO;
3685
3686 err = attribute->store(s, buf, len);
3687
3688 return err;
3689}
3690
3691static struct sysfs_ops slab_sysfs_ops = {
3692 .show = slab_attr_show,
3693 .store = slab_attr_store,
3694};
3695
3696static struct kobj_type slab_ktype = {
3697 .sysfs_ops = &slab_sysfs_ops,
3698};
3699
3700static int uevent_filter(struct kset *kset, struct kobject *kobj)
3701{
3702 struct kobj_type *ktype = get_ktype(kobj);
3703
3704 if (ktype == &slab_ktype)
3705 return 1;
3706 return 0;
3707}
3708
3709static struct kset_uevent_ops slab_uevent_ops = {
3710 .filter = uevent_filter,
3711};
3712
Adrian Bunk5af328a2007-07-17 04:03:27 -07003713static decl_subsys(slab, &slab_ktype, &slab_uevent_ops);
Christoph Lameter81819f02007-05-06 14:49:36 -07003714
3715#define ID_STR_LENGTH 64
3716
3717/* Create a unique string id for a slab cache:
3718 * format
3719 * :[flags-]size:[memory address of kmemcache]
3720 */
3721static char *create_unique_id(struct kmem_cache *s)
3722{
3723 char *name = kmalloc(ID_STR_LENGTH, GFP_KERNEL);
3724 char *p = name;
3725
3726 BUG_ON(!name);
3727
3728 *p++ = ':';
3729 /*
3730 * First flags affecting slabcache operations. We will only
3731 * get here for aliasable slabs so we do not need to support
3732 * too many flags. The flags here must cover all flags that
3733 * are matched during merging to guarantee that the id is
3734 * unique.
3735 */
3736 if (s->flags & SLAB_CACHE_DMA)
3737 *p++ = 'd';
3738 if (s->flags & SLAB_RECLAIM_ACCOUNT)
3739 *p++ = 'a';
3740 if (s->flags & SLAB_DEBUG_FREE)
3741 *p++ = 'F';
3742 if (p != name + 1)
3743 *p++ = '-';
3744 p += sprintf(p, "%07d", s->size);
3745 BUG_ON(p > name + ID_STR_LENGTH - 1);
3746 return name;
3747}
3748
3749static int sysfs_slab_add(struct kmem_cache *s)
3750{
3751 int err;
3752 const char *name;
3753 int unmergeable;
3754
3755 if (slab_state < SYSFS)
3756 /* Defer until later */
3757 return 0;
3758
3759 unmergeable = slab_unmergeable(s);
3760 if (unmergeable) {
3761 /*
3762 * Slabcache can never be merged so we can use the name proper.
3763 * This is typically the case for debug situations. In that
3764 * case we can catch duplicate names easily.
3765 */
Linus Torvalds0f9008e2007-05-07 12:31:58 -07003766 sysfs_remove_link(&slab_subsys.kobj, s->name);
Christoph Lameter81819f02007-05-06 14:49:36 -07003767 name = s->name;
3768 } else {
3769 /*
3770 * Create a unique name for the slab as a target
3771 * for the symlinks.
3772 */
3773 name = create_unique_id(s);
3774 }
3775
3776 kobj_set_kset_s(s, slab_subsys);
3777 kobject_set_name(&s->kobj, name);
3778 kobject_init(&s->kobj);
3779 err = kobject_add(&s->kobj);
3780 if (err)
3781 return err;
3782
3783 err = sysfs_create_group(&s->kobj, &slab_attr_group);
3784 if (err)
3785 return err;
3786 kobject_uevent(&s->kobj, KOBJ_ADD);
3787 if (!unmergeable) {
3788 /* Setup first alias */
3789 sysfs_slab_alias(s, s->name);
3790 kfree(name);
3791 }
3792 return 0;
3793}
3794
3795static void sysfs_slab_remove(struct kmem_cache *s)
3796{
3797 kobject_uevent(&s->kobj, KOBJ_REMOVE);
3798 kobject_del(&s->kobj);
3799}
3800
3801/*
3802 * Need to buffer aliases during bootup until sysfs becomes
3803 * available lest we loose that information.
3804 */
3805struct saved_alias {
3806 struct kmem_cache *s;
3807 const char *name;
3808 struct saved_alias *next;
3809};
3810
Adrian Bunk5af328a2007-07-17 04:03:27 -07003811static struct saved_alias *alias_list;
Christoph Lameter81819f02007-05-06 14:49:36 -07003812
3813static int sysfs_slab_alias(struct kmem_cache *s, const char *name)
3814{
3815 struct saved_alias *al;
3816
3817 if (slab_state == SYSFS) {
3818 /*
3819 * If we have a leftover link then remove it.
3820 */
Linus Torvalds0f9008e2007-05-07 12:31:58 -07003821 sysfs_remove_link(&slab_subsys.kobj, name);
3822 return sysfs_create_link(&slab_subsys.kobj,
Christoph Lameter81819f02007-05-06 14:49:36 -07003823 &s->kobj, name);
3824 }
3825
3826 al = kmalloc(sizeof(struct saved_alias), GFP_KERNEL);
3827 if (!al)
3828 return -ENOMEM;
3829
3830 al->s = s;
3831 al->name = name;
3832 al->next = alias_list;
3833 alias_list = al;
3834 return 0;
3835}
3836
3837static int __init slab_sysfs_init(void)
3838{
Christoph Lameter5b95a4ac2007-07-17 04:03:19 -07003839 struct kmem_cache *s;
Christoph Lameter81819f02007-05-06 14:49:36 -07003840 int err;
3841
3842 err = subsystem_register(&slab_subsys);
3843 if (err) {
3844 printk(KERN_ERR "Cannot register slab subsystem.\n");
3845 return -ENOSYS;
3846 }
3847
Christoph Lameter26a7bd02007-05-09 02:32:39 -07003848 slab_state = SYSFS;
3849
Christoph Lameter5b95a4ac2007-07-17 04:03:19 -07003850 list_for_each_entry(s, &slab_caches, list) {
Christoph Lameter26a7bd02007-05-09 02:32:39 -07003851 err = sysfs_slab_add(s);
Christoph Lameter5d540fb2007-08-30 23:56:26 -07003852 if (err)
3853 printk(KERN_ERR "SLUB: Unable to add boot slab %s"
3854 " to sysfs\n", s->name);
Christoph Lameter26a7bd02007-05-09 02:32:39 -07003855 }
Christoph Lameter81819f02007-05-06 14:49:36 -07003856
3857 while (alias_list) {
3858 struct saved_alias *al = alias_list;
3859
3860 alias_list = alias_list->next;
3861 err = sysfs_slab_alias(al->s, al->name);
Christoph Lameter5d540fb2007-08-30 23:56:26 -07003862 if (err)
3863 printk(KERN_ERR "SLUB: Unable to add boot slab alias"
3864 " %s to sysfs\n", s->name);
Christoph Lameter81819f02007-05-06 14:49:36 -07003865 kfree(al);
3866 }
3867
3868 resiliency_test();
3869 return 0;
3870}
3871
3872__initcall(slab_sysfs_init);
Christoph Lameter81819f02007-05-06 14:49:36 -07003873#endif