blob: e29a42988c78ca17d9594c2cb9fdb3cb87c257d0 [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{
Christoph Lameter4c93c3552007-10-16 01:26:08 -0700272#ifdef CONFIG_SMP
273 return s->cpu_slab[cpu];
274#else
275 return &s->cpu_slab;
276#endif
Christoph Lameterdfb4f092007-10-16 01:26:05 -0700277}
278
Christoph Lameter02cbc872007-05-09 02:32:43 -0700279static inline int check_valid_pointer(struct kmem_cache *s,
280 struct page *page, const void *object)
281{
282 void *base;
283
284 if (!object)
285 return 1;
286
287 base = page_address(page);
288 if (object < base || object >= base + s->objects * s->size ||
289 (object - base) % s->size) {
290 return 0;
291 }
292
293 return 1;
294}
295
Christoph Lameter81819f02007-05-06 14:49:36 -0700296/*
Christoph Lameter7656c722007-05-09 02:32:40 -0700297 * Slow version of get and set free pointer.
298 *
299 * This version requires touching the cache lines of kmem_cache which
300 * we avoid to do in the fast alloc free paths. There we obtain the offset
301 * from the page struct.
302 */
303static inline void *get_freepointer(struct kmem_cache *s, void *object)
304{
305 return *(void **)(object + s->offset);
306}
307
308static inline void set_freepointer(struct kmem_cache *s, void *object, void *fp)
309{
310 *(void **)(object + s->offset) = fp;
311}
312
313/* Loop over all objects in a slab */
314#define for_each_object(__p, __s, __addr) \
315 for (__p = (__addr); __p < (__addr) + (__s)->objects * (__s)->size;\
316 __p += (__s)->size)
317
318/* Scan freelist */
319#define for_each_free_object(__p, __s, __free) \
320 for (__p = (__free); __p; __p = get_freepointer((__s), __p))
321
322/* Determine object index from a given position */
323static inline int slab_index(void *p, struct kmem_cache *s, void *addr)
324{
325 return (p - addr) / s->size;
326}
327
Christoph Lameter41ecc552007-05-09 02:32:44 -0700328#ifdef CONFIG_SLUB_DEBUG
329/*
330 * Debug settings:
331 */
Christoph Lameterf0630ff2007-07-15 23:38:14 -0700332#ifdef CONFIG_SLUB_DEBUG_ON
333static int slub_debug = DEBUG_DEFAULT_FLAGS;
334#else
Christoph Lameter41ecc552007-05-09 02:32:44 -0700335static int slub_debug;
Christoph Lameterf0630ff2007-07-15 23:38:14 -0700336#endif
Christoph Lameter41ecc552007-05-09 02:32:44 -0700337
338static char *slub_debug_slabs;
339
Christoph Lameter7656c722007-05-09 02:32:40 -0700340/*
Christoph Lameter81819f02007-05-06 14:49:36 -0700341 * Object debugging
342 */
343static void print_section(char *text, u8 *addr, unsigned int length)
344{
345 int i, offset;
346 int newline = 1;
347 char ascii[17];
348
349 ascii[16] = 0;
350
351 for (i = 0; i < length; i++) {
352 if (newline) {
Christoph Lameter24922682007-07-17 04:03:18 -0700353 printk(KERN_ERR "%8s 0x%p: ", text, addr + i);
Christoph Lameter81819f02007-05-06 14:49:36 -0700354 newline = 0;
355 }
356 printk(" %02x", addr[i]);
357 offset = i % 16;
358 ascii[offset] = isgraph(addr[i]) ? addr[i] : '.';
359 if (offset == 15) {
360 printk(" %s\n",ascii);
361 newline = 1;
362 }
363 }
364 if (!newline) {
365 i %= 16;
366 while (i < 16) {
367 printk(" ");
368 ascii[i] = ' ';
369 i++;
370 }
371 printk(" %s\n", ascii);
372 }
373}
374
Christoph Lameter81819f02007-05-06 14:49:36 -0700375static struct track *get_track(struct kmem_cache *s, void *object,
376 enum track_item alloc)
377{
378 struct track *p;
379
380 if (s->offset)
381 p = object + s->offset + sizeof(void *);
382 else
383 p = object + s->inuse;
384
385 return p + alloc;
386}
387
388static void set_track(struct kmem_cache *s, void *object,
389 enum track_item alloc, void *addr)
390{
391 struct track *p;
392
393 if (s->offset)
394 p = object + s->offset + sizeof(void *);
395 else
396 p = object + s->inuse;
397
398 p += alloc;
399 if (addr) {
400 p->addr = addr;
401 p->cpu = smp_processor_id();
402 p->pid = current ? current->pid : -1;
403 p->when = jiffies;
404 } else
405 memset(p, 0, sizeof(struct track));
406}
407
Christoph Lameter81819f02007-05-06 14:49:36 -0700408static void init_tracking(struct kmem_cache *s, void *object)
409{
Christoph Lameter24922682007-07-17 04:03:18 -0700410 if (!(s->flags & SLAB_STORE_USER))
411 return;
412
413 set_track(s, object, TRACK_FREE, NULL);
414 set_track(s, object, TRACK_ALLOC, NULL);
Christoph Lameter81819f02007-05-06 14:49:36 -0700415}
416
417static void print_track(const char *s, struct track *t)
418{
419 if (!t->addr)
420 return;
421
Christoph Lameter24922682007-07-17 04:03:18 -0700422 printk(KERN_ERR "INFO: %s in ", s);
Christoph Lameter81819f02007-05-06 14:49:36 -0700423 __print_symbol("%s", (unsigned long)t->addr);
Christoph Lameter24922682007-07-17 04:03:18 -0700424 printk(" age=%lu cpu=%u pid=%d\n", jiffies - t->when, t->cpu, t->pid);
Christoph Lameter81819f02007-05-06 14:49:36 -0700425}
426
Christoph Lameter24922682007-07-17 04:03:18 -0700427static void print_tracking(struct kmem_cache *s, void *object)
428{
429 if (!(s->flags & SLAB_STORE_USER))
430 return;
431
432 print_track("Allocated", get_track(s, object, TRACK_ALLOC));
433 print_track("Freed", get_track(s, object, TRACK_FREE));
434}
435
436static void print_page_info(struct page *page)
437{
438 printk(KERN_ERR "INFO: Slab 0x%p used=%u fp=0x%p flags=0x%04lx\n",
439 page, page->inuse, page->freelist, page->flags);
440
441}
442
443static void slab_bug(struct kmem_cache *s, char *fmt, ...)
444{
445 va_list args;
446 char buf[100];
447
448 va_start(args, fmt);
449 vsnprintf(buf, sizeof(buf), fmt, args);
450 va_end(args);
451 printk(KERN_ERR "========================================"
452 "=====================================\n");
453 printk(KERN_ERR "BUG %s: %s\n", s->name, buf);
454 printk(KERN_ERR "----------------------------------------"
455 "-------------------------------------\n\n");
456}
457
458static void slab_fix(struct kmem_cache *s, char *fmt, ...)
459{
460 va_list args;
461 char buf[100];
462
463 va_start(args, fmt);
464 vsnprintf(buf, sizeof(buf), fmt, args);
465 va_end(args);
466 printk(KERN_ERR "FIX %s: %s\n", s->name, buf);
467}
468
469static void print_trailer(struct kmem_cache *s, struct page *page, u8 *p)
Christoph Lameter81819f02007-05-06 14:49:36 -0700470{
471 unsigned int off; /* Offset of last byte */
Christoph Lameter24922682007-07-17 04:03:18 -0700472 u8 *addr = page_address(page);
473
474 print_tracking(s, p);
475
476 print_page_info(page);
477
478 printk(KERN_ERR "INFO: Object 0x%p @offset=%tu fp=0x%p\n\n",
479 p, p - addr, get_freepointer(s, p));
480
481 if (p > addr + 16)
482 print_section("Bytes b4", p - 16, 16);
483
484 print_section("Object", p, min(s->objsize, 128));
Christoph Lameter81819f02007-05-06 14:49:36 -0700485
486 if (s->flags & SLAB_RED_ZONE)
487 print_section("Redzone", p + s->objsize,
488 s->inuse - s->objsize);
489
Christoph Lameter81819f02007-05-06 14:49:36 -0700490 if (s->offset)
491 off = s->offset + sizeof(void *);
492 else
493 off = s->inuse;
494
Christoph Lameter24922682007-07-17 04:03:18 -0700495 if (s->flags & SLAB_STORE_USER)
Christoph Lameter81819f02007-05-06 14:49:36 -0700496 off += 2 * sizeof(struct track);
Christoph Lameter81819f02007-05-06 14:49:36 -0700497
498 if (off != s->size)
499 /* Beginning of the filler is the free pointer */
Christoph Lameter24922682007-07-17 04:03:18 -0700500 print_section("Padding", p + off, s->size - off);
501
502 dump_stack();
Christoph Lameter81819f02007-05-06 14:49:36 -0700503}
504
505static void object_err(struct kmem_cache *s, struct page *page,
506 u8 *object, char *reason)
507{
Christoph Lameter24922682007-07-17 04:03:18 -0700508 slab_bug(s, reason);
509 print_trailer(s, page, object);
Christoph Lameter81819f02007-05-06 14:49:36 -0700510}
511
Christoph Lameter24922682007-07-17 04:03:18 -0700512static void slab_err(struct kmem_cache *s, struct page *page, char *fmt, ...)
Christoph Lameter81819f02007-05-06 14:49:36 -0700513{
514 va_list args;
515 char buf[100];
516
Christoph Lameter24922682007-07-17 04:03:18 -0700517 va_start(args, fmt);
518 vsnprintf(buf, sizeof(buf), fmt, args);
Christoph Lameter81819f02007-05-06 14:49:36 -0700519 va_end(args);
Christoph Lameter24922682007-07-17 04:03:18 -0700520 slab_bug(s, fmt);
521 print_page_info(page);
Christoph Lameter81819f02007-05-06 14:49:36 -0700522 dump_stack();
523}
524
525static void init_object(struct kmem_cache *s, void *object, int active)
526{
527 u8 *p = object;
528
529 if (s->flags & __OBJECT_POISON) {
530 memset(p, POISON_FREE, s->objsize - 1);
531 p[s->objsize -1] = POISON_END;
532 }
533
534 if (s->flags & SLAB_RED_ZONE)
535 memset(p + s->objsize,
536 active ? SLUB_RED_ACTIVE : SLUB_RED_INACTIVE,
537 s->inuse - s->objsize);
538}
539
Christoph Lameter24922682007-07-17 04:03:18 -0700540static u8 *check_bytes(u8 *start, unsigned int value, unsigned int bytes)
Christoph Lameter81819f02007-05-06 14:49:36 -0700541{
542 while (bytes) {
543 if (*start != (u8)value)
Christoph Lameter24922682007-07-17 04:03:18 -0700544 return start;
Christoph Lameter81819f02007-05-06 14:49:36 -0700545 start++;
546 bytes--;
547 }
Christoph Lameter24922682007-07-17 04:03:18 -0700548 return NULL;
549}
550
551static void restore_bytes(struct kmem_cache *s, char *message, u8 data,
552 void *from, void *to)
553{
554 slab_fix(s, "Restoring 0x%p-0x%p=0x%x\n", from, to - 1, data);
555 memset(from, data, to - from);
556}
557
558static int check_bytes_and_report(struct kmem_cache *s, struct page *page,
559 u8 *object, char *what,
560 u8* start, unsigned int value, unsigned int bytes)
561{
562 u8 *fault;
563 u8 *end;
564
565 fault = check_bytes(start, value, bytes);
566 if (!fault)
567 return 1;
568
569 end = start + bytes;
570 while (end > fault && end[-1] == value)
571 end--;
572
573 slab_bug(s, "%s overwritten", what);
574 printk(KERN_ERR "INFO: 0x%p-0x%p. First byte 0x%x instead of 0x%x\n",
575 fault, end - 1, fault[0], value);
576 print_trailer(s, page, object);
577
578 restore_bytes(s, what, value, fault, end);
579 return 0;
Christoph Lameter81819f02007-05-06 14:49:36 -0700580}
581
Christoph Lameter81819f02007-05-06 14:49:36 -0700582/*
583 * Object layout:
584 *
585 * object address
586 * Bytes of the object to be managed.
587 * If the freepointer may overlay the object then the free
588 * pointer is the first word of the object.
Christoph Lameter672bba32007-05-09 02:32:39 -0700589 *
Christoph Lameter81819f02007-05-06 14:49:36 -0700590 * Poisoning uses 0x6b (POISON_FREE) and the last byte is
591 * 0xa5 (POISON_END)
592 *
593 * object + s->objsize
594 * Padding to reach word boundary. This is also used for Redzoning.
Christoph Lameter672bba32007-05-09 02:32:39 -0700595 * Padding is extended by another word if Redzoning is enabled and
596 * objsize == inuse.
597 *
Christoph Lameter81819f02007-05-06 14:49:36 -0700598 * We fill with 0xbb (RED_INACTIVE) for inactive objects and with
599 * 0xcc (RED_ACTIVE) for objects in use.
600 *
601 * object + s->inuse
Christoph Lameter672bba32007-05-09 02:32:39 -0700602 * Meta data starts here.
603 *
Christoph Lameter81819f02007-05-06 14:49:36 -0700604 * A. Free pointer (if we cannot overwrite object on free)
605 * B. Tracking data for SLAB_STORE_USER
Christoph Lameter672bba32007-05-09 02:32:39 -0700606 * C. Padding to reach required alignment boundary or at mininum
607 * one word if debuggin is on to be able to detect writes
608 * before the word boundary.
609 *
610 * Padding is done using 0x5a (POISON_INUSE)
Christoph Lameter81819f02007-05-06 14:49:36 -0700611 *
612 * object + s->size
Christoph Lameter672bba32007-05-09 02:32:39 -0700613 * Nothing is used beyond s->size.
Christoph Lameter81819f02007-05-06 14:49:36 -0700614 *
Christoph Lameter672bba32007-05-09 02:32:39 -0700615 * If slabcaches are merged then the objsize and inuse boundaries are mostly
616 * ignored. And therefore no slab options that rely on these boundaries
Christoph Lameter81819f02007-05-06 14:49:36 -0700617 * may be used with merged slabcaches.
618 */
619
Christoph Lameter81819f02007-05-06 14:49:36 -0700620static int check_pad_bytes(struct kmem_cache *s, struct page *page, u8 *p)
621{
622 unsigned long off = s->inuse; /* The end of info */
623
624 if (s->offset)
625 /* Freepointer is placed after the object. */
626 off += sizeof(void *);
627
628 if (s->flags & SLAB_STORE_USER)
629 /* We also have user information there */
630 off += 2 * sizeof(struct track);
631
632 if (s->size == off)
633 return 1;
634
Christoph Lameter24922682007-07-17 04:03:18 -0700635 return check_bytes_and_report(s, page, p, "Object padding",
636 p + off, POISON_INUSE, s->size - off);
Christoph Lameter81819f02007-05-06 14:49:36 -0700637}
638
639static int slab_pad_check(struct kmem_cache *s, struct page *page)
640{
Christoph Lameter24922682007-07-17 04:03:18 -0700641 u8 *start;
642 u8 *fault;
643 u8 *end;
644 int length;
645 int remainder;
Christoph Lameter81819f02007-05-06 14:49:36 -0700646
647 if (!(s->flags & SLAB_POISON))
648 return 1;
649
Christoph Lameter24922682007-07-17 04:03:18 -0700650 start = page_address(page);
651 end = start + (PAGE_SIZE << s->order);
Christoph Lameter81819f02007-05-06 14:49:36 -0700652 length = s->objects * s->size;
Christoph Lameter24922682007-07-17 04:03:18 -0700653 remainder = end - (start + length);
Christoph Lameter81819f02007-05-06 14:49:36 -0700654 if (!remainder)
655 return 1;
656
Christoph Lameter24922682007-07-17 04:03:18 -0700657 fault = check_bytes(start + length, POISON_INUSE, remainder);
658 if (!fault)
659 return 1;
660 while (end > fault && end[-1] == POISON_INUSE)
661 end--;
662
663 slab_err(s, page, "Padding overwritten. 0x%p-0x%p", fault, end - 1);
664 print_section("Padding", start, length);
665
666 restore_bytes(s, "slab padding", POISON_INUSE, start, end);
667 return 0;
Christoph Lameter81819f02007-05-06 14:49:36 -0700668}
669
670static int check_object(struct kmem_cache *s, struct page *page,
671 void *object, int active)
672{
673 u8 *p = object;
674 u8 *endobject = object + s->objsize;
675
676 if (s->flags & SLAB_RED_ZONE) {
677 unsigned int red =
678 active ? SLUB_RED_ACTIVE : SLUB_RED_INACTIVE;
679
Christoph Lameter24922682007-07-17 04:03:18 -0700680 if (!check_bytes_and_report(s, page, object, "Redzone",
681 endobject, red, s->inuse - s->objsize))
Christoph Lameter81819f02007-05-06 14:49:36 -0700682 return 0;
Christoph Lameter81819f02007-05-06 14:49:36 -0700683 } else {
Christoph Lameter24922682007-07-17 04:03:18 -0700684 if ((s->flags & SLAB_POISON) && s->objsize < s->inuse)
685 check_bytes_and_report(s, page, p, "Alignment padding", endobject,
686 POISON_INUSE, s->inuse - s->objsize);
Christoph Lameter81819f02007-05-06 14:49:36 -0700687 }
688
689 if (s->flags & SLAB_POISON) {
690 if (!active && (s->flags & __OBJECT_POISON) &&
Christoph Lameter24922682007-07-17 04:03:18 -0700691 (!check_bytes_and_report(s, page, p, "Poison", p,
692 POISON_FREE, s->objsize - 1) ||
693 !check_bytes_and_report(s, page, p, "Poison",
694 p + s->objsize -1, POISON_END, 1)))
Christoph Lameter81819f02007-05-06 14:49:36 -0700695 return 0;
Christoph Lameter81819f02007-05-06 14:49:36 -0700696 /*
697 * check_pad_bytes cleans up on its own.
698 */
699 check_pad_bytes(s, page, p);
700 }
701
702 if (!s->offset && active)
703 /*
704 * Object and freepointer overlap. Cannot check
705 * freepointer while object is allocated.
706 */
707 return 1;
708
709 /* Check free pointer validity */
710 if (!check_valid_pointer(s, page, get_freepointer(s, p))) {
711 object_err(s, page, p, "Freepointer corrupt");
712 /*
713 * No choice but to zap it and thus loose the remainder
714 * of the free objects in this slab. May cause
Christoph Lameter672bba32007-05-09 02:32:39 -0700715 * another error because the object count is now wrong.
Christoph Lameter81819f02007-05-06 14:49:36 -0700716 */
717 set_freepointer(s, p, NULL);
718 return 0;
719 }
720 return 1;
721}
722
723static int check_slab(struct kmem_cache *s, struct page *page)
724{
725 VM_BUG_ON(!irqs_disabled());
726
727 if (!PageSlab(page)) {
Christoph Lameter24922682007-07-17 04:03:18 -0700728 slab_err(s, page, "Not a valid slab page");
Christoph Lameter81819f02007-05-06 14:49:36 -0700729 return 0;
730 }
Christoph Lameter81819f02007-05-06 14:49:36 -0700731 if (page->inuse > s->objects) {
Christoph Lameter24922682007-07-17 04:03:18 -0700732 slab_err(s, page, "inuse %u > max %u",
733 s->name, page->inuse, s->objects);
Christoph Lameter81819f02007-05-06 14:49:36 -0700734 return 0;
735 }
736 /* Slab_pad_check fixes things up after itself */
737 slab_pad_check(s, page);
738 return 1;
739}
740
741/*
Christoph Lameter672bba32007-05-09 02:32:39 -0700742 * Determine if a certain object on a page is on the freelist. Must hold the
743 * slab lock to guarantee that the chains are in a consistent state.
Christoph Lameter81819f02007-05-06 14:49:36 -0700744 */
745static int on_freelist(struct kmem_cache *s, struct page *page, void *search)
746{
747 int nr = 0;
748 void *fp = page->freelist;
749 void *object = NULL;
750
751 while (fp && nr <= s->objects) {
752 if (fp == search)
753 return 1;
754 if (!check_valid_pointer(s, page, fp)) {
755 if (object) {
756 object_err(s, page, object,
757 "Freechain corrupt");
758 set_freepointer(s, object, NULL);
759 break;
760 } else {
Christoph Lameter24922682007-07-17 04:03:18 -0700761 slab_err(s, page, "Freepointer corrupt");
Christoph Lameter81819f02007-05-06 14:49:36 -0700762 page->freelist = NULL;
763 page->inuse = s->objects;
Christoph Lameter24922682007-07-17 04:03:18 -0700764 slab_fix(s, "Freelist cleared");
Christoph Lameter81819f02007-05-06 14:49:36 -0700765 return 0;
766 }
767 break;
768 }
769 object = fp;
770 fp = get_freepointer(s, object);
771 nr++;
772 }
773
774 if (page->inuse != s->objects - nr) {
Christoph Lameter70d71222007-05-06 14:49:47 -0700775 slab_err(s, page, "Wrong object count. Counter is %d but "
Christoph Lameter24922682007-07-17 04:03:18 -0700776 "counted were %d", page->inuse, s->objects - nr);
Christoph Lameter81819f02007-05-06 14:49:36 -0700777 page->inuse = s->objects - nr;
Christoph Lameter24922682007-07-17 04:03:18 -0700778 slab_fix(s, "Object count adjusted.");
Christoph Lameter81819f02007-05-06 14:49:36 -0700779 }
780 return search == NULL;
781}
782
Christoph Lameter3ec09742007-05-16 22:11:00 -0700783static void trace(struct kmem_cache *s, struct page *page, void *object, int alloc)
784{
785 if (s->flags & SLAB_TRACE) {
786 printk(KERN_INFO "TRACE %s %s 0x%p inuse=%d fp=0x%p\n",
787 s->name,
788 alloc ? "alloc" : "free",
789 object, page->inuse,
790 page->freelist);
791
792 if (!alloc)
793 print_section("Object", (void *)object, s->objsize);
794
795 dump_stack();
796 }
797}
798
Christoph Lameter643b1132007-05-06 14:49:42 -0700799/*
Christoph Lameter672bba32007-05-09 02:32:39 -0700800 * Tracking of fully allocated slabs for debugging purposes.
Christoph Lameter643b1132007-05-06 14:49:42 -0700801 */
Christoph Lametere95eed52007-05-06 14:49:44 -0700802static void add_full(struct kmem_cache_node *n, struct page *page)
Christoph Lameter643b1132007-05-06 14:49:42 -0700803{
Christoph Lameter643b1132007-05-06 14:49:42 -0700804 spin_lock(&n->list_lock);
805 list_add(&page->lru, &n->full);
806 spin_unlock(&n->list_lock);
807}
808
809static void remove_full(struct kmem_cache *s, struct page *page)
810{
811 struct kmem_cache_node *n;
812
813 if (!(s->flags & SLAB_STORE_USER))
814 return;
815
816 n = get_node(s, page_to_nid(page));
817
818 spin_lock(&n->list_lock);
819 list_del(&page->lru);
820 spin_unlock(&n->list_lock);
821}
822
Christoph Lameter3ec09742007-05-16 22:11:00 -0700823static void setup_object_debug(struct kmem_cache *s, struct page *page,
824 void *object)
825{
826 if (!(s->flags & (SLAB_STORE_USER|SLAB_RED_ZONE|__OBJECT_POISON)))
827 return;
828
829 init_object(s, object, 0);
830 init_tracking(s, object);
831}
832
833static int alloc_debug_processing(struct kmem_cache *s, struct page *page,
834 void *object, void *addr)
Christoph Lameter81819f02007-05-06 14:49:36 -0700835{
836 if (!check_slab(s, page))
837 goto bad;
838
839 if (object && !on_freelist(s, page, object)) {
Christoph Lameter24922682007-07-17 04:03:18 -0700840 object_err(s, page, object, "Object already allocated");
Christoph Lameter70d71222007-05-06 14:49:47 -0700841 goto bad;
Christoph Lameter81819f02007-05-06 14:49:36 -0700842 }
843
844 if (!check_valid_pointer(s, page, object)) {
845 object_err(s, page, object, "Freelist Pointer check fails");
Christoph Lameter70d71222007-05-06 14:49:47 -0700846 goto bad;
Christoph Lameter81819f02007-05-06 14:49:36 -0700847 }
848
Christoph Lameter3ec09742007-05-16 22:11:00 -0700849 if (object && !check_object(s, page, object, 0))
Christoph Lameter81819f02007-05-06 14:49:36 -0700850 goto bad;
Christoph Lameter81819f02007-05-06 14:49:36 -0700851
Christoph Lameter3ec09742007-05-16 22:11:00 -0700852 /* Success perform special debug activities for allocs */
853 if (s->flags & SLAB_STORE_USER)
854 set_track(s, object, TRACK_ALLOC, addr);
855 trace(s, page, object, 1);
856 init_object(s, object, 1);
Christoph Lameter81819f02007-05-06 14:49:36 -0700857 return 1;
Christoph Lameter3ec09742007-05-16 22:11:00 -0700858
Christoph Lameter81819f02007-05-06 14:49:36 -0700859bad:
860 if (PageSlab(page)) {
861 /*
862 * If this is a slab page then lets do the best we can
863 * to avoid issues in the future. Marking all objects
Christoph Lameter672bba32007-05-09 02:32:39 -0700864 * as used avoids touching the remaining objects.
Christoph Lameter81819f02007-05-06 14:49:36 -0700865 */
Christoph Lameter24922682007-07-17 04:03:18 -0700866 slab_fix(s, "Marking all objects used");
Christoph Lameter81819f02007-05-06 14:49:36 -0700867 page->inuse = s->objects;
868 page->freelist = NULL;
Christoph Lameter81819f02007-05-06 14:49:36 -0700869 }
870 return 0;
871}
872
Christoph Lameter3ec09742007-05-16 22:11:00 -0700873static int free_debug_processing(struct kmem_cache *s, struct page *page,
874 void *object, void *addr)
Christoph Lameter81819f02007-05-06 14:49:36 -0700875{
876 if (!check_slab(s, page))
877 goto fail;
878
879 if (!check_valid_pointer(s, page, object)) {
Christoph Lameter70d71222007-05-06 14:49:47 -0700880 slab_err(s, page, "Invalid object pointer 0x%p", object);
Christoph Lameter81819f02007-05-06 14:49:36 -0700881 goto fail;
882 }
883
884 if (on_freelist(s, page, object)) {
Christoph Lameter24922682007-07-17 04:03:18 -0700885 object_err(s, page, object, "Object already free");
Christoph Lameter81819f02007-05-06 14:49:36 -0700886 goto fail;
887 }
888
889 if (!check_object(s, page, object, 1))
890 return 0;
891
892 if (unlikely(s != page->slab)) {
893 if (!PageSlab(page))
Christoph Lameter70d71222007-05-06 14:49:47 -0700894 slab_err(s, page, "Attempt to free object(0x%p) "
895 "outside of slab", object);
Christoph Lameter81819f02007-05-06 14:49:36 -0700896 else
Christoph Lameter70d71222007-05-06 14:49:47 -0700897 if (!page->slab) {
Christoph Lameter81819f02007-05-06 14:49:36 -0700898 printk(KERN_ERR
Christoph Lameter70d71222007-05-06 14:49:47 -0700899 "SLUB <none>: no slab for object 0x%p.\n",
Christoph Lameter81819f02007-05-06 14:49:36 -0700900 object);
Christoph Lameter70d71222007-05-06 14:49:47 -0700901 dump_stack();
902 }
Christoph Lameter81819f02007-05-06 14:49:36 -0700903 else
Christoph Lameter24922682007-07-17 04:03:18 -0700904 object_err(s, page, object,
905 "page slab pointer corrupt.");
Christoph Lameter81819f02007-05-06 14:49:36 -0700906 goto fail;
907 }
Christoph Lameter3ec09742007-05-16 22:11:00 -0700908
909 /* Special debug activities for freeing objects */
910 if (!SlabFrozen(page) && !page->freelist)
911 remove_full(s, page);
912 if (s->flags & SLAB_STORE_USER)
913 set_track(s, object, TRACK_FREE, addr);
914 trace(s, page, object, 0);
915 init_object(s, object, 0);
Christoph Lameter81819f02007-05-06 14:49:36 -0700916 return 1;
Christoph Lameter3ec09742007-05-16 22:11:00 -0700917
Christoph Lameter81819f02007-05-06 14:49:36 -0700918fail:
Christoph Lameter24922682007-07-17 04:03:18 -0700919 slab_fix(s, "Object at 0x%p not freed", object);
Christoph Lameter81819f02007-05-06 14:49:36 -0700920 return 0;
921}
922
Christoph Lameter41ecc552007-05-09 02:32:44 -0700923static int __init setup_slub_debug(char *str)
924{
Christoph Lameterf0630ff2007-07-15 23:38:14 -0700925 slub_debug = DEBUG_DEFAULT_FLAGS;
926 if (*str++ != '=' || !*str)
927 /*
928 * No options specified. Switch on full debugging.
929 */
930 goto out;
Christoph Lameter41ecc552007-05-09 02:32:44 -0700931
932 if (*str == ',')
Christoph Lameterf0630ff2007-07-15 23:38:14 -0700933 /*
934 * No options but restriction on slabs. This means full
935 * debugging for slabs matching a pattern.
936 */
937 goto check_slabs;
938
939 slub_debug = 0;
940 if (*str == '-')
941 /*
942 * Switch off all debugging measures.
943 */
944 goto out;
945
946 /*
947 * Determine which debug features should be switched on
948 */
949 for ( ;*str && *str != ','; str++) {
950 switch (tolower(*str)) {
951 case 'f':
952 slub_debug |= SLAB_DEBUG_FREE;
953 break;
954 case 'z':
955 slub_debug |= SLAB_RED_ZONE;
956 break;
957 case 'p':
958 slub_debug |= SLAB_POISON;
959 break;
960 case 'u':
961 slub_debug |= SLAB_STORE_USER;
962 break;
963 case 't':
964 slub_debug |= SLAB_TRACE;
965 break;
966 default:
967 printk(KERN_ERR "slub_debug option '%c' "
968 "unknown. skipped\n",*str);
969 }
970 }
971
972check_slabs:
973 if (*str == ',')
Christoph Lameter41ecc552007-05-09 02:32:44 -0700974 slub_debug_slabs = str + 1;
Christoph Lameterf0630ff2007-07-15 23:38:14 -0700975out:
Christoph Lameter41ecc552007-05-09 02:32:44 -0700976 return 1;
977}
978
979__setup("slub_debug", setup_slub_debug);
980
Christoph Lameterba0268a2007-09-11 15:24:11 -0700981static unsigned long kmem_cache_flags(unsigned long objsize,
982 unsigned long flags, const char *name,
Christoph Lameter4ba9b9d2007-10-16 23:25:51 -0700983 void (*ctor)(struct kmem_cache *, void *))
Christoph Lameter41ecc552007-05-09 02:32:44 -0700984{
985 /*
986 * The page->offset field is only 16 bit wide. This is an offset
987 * in units of words from the beginning of an object. If the slab
988 * size is bigger then we cannot move the free pointer behind the
989 * object anymore.
990 *
991 * On 32 bit platforms the limit is 256k. On 64bit platforms
992 * the limit is 512k.
993 *
Christoph Lameterc59def92007-05-16 22:10:50 -0700994 * Debugging or ctor may create a need to move the free
Christoph Lameter41ecc552007-05-09 02:32:44 -0700995 * pointer. Fail if this happens.
996 */
Christoph Lameterba0268a2007-09-11 15:24:11 -0700997 if (objsize >= 65535 * sizeof(void *)) {
998 BUG_ON(flags & (SLAB_RED_ZONE | SLAB_POISON |
Christoph Lameter41ecc552007-05-09 02:32:44 -0700999 SLAB_STORE_USER | SLAB_DESTROY_BY_RCU));
Christoph Lameterba0268a2007-09-11 15:24:11 -07001000 BUG_ON(ctor);
1001 } else {
Christoph Lameter41ecc552007-05-09 02:32:44 -07001002 /*
1003 * Enable debugging if selected on the kernel commandline.
1004 */
1005 if (slub_debug && (!slub_debug_slabs ||
Christoph Lameterba0268a2007-09-11 15:24:11 -07001006 strncmp(slub_debug_slabs, name,
Christoph Lameter41ecc552007-05-09 02:32:44 -07001007 strlen(slub_debug_slabs)) == 0))
Christoph Lameterba0268a2007-09-11 15:24:11 -07001008 flags |= slub_debug;
1009 }
1010
1011 return flags;
Christoph Lameter41ecc552007-05-09 02:32:44 -07001012}
1013#else
Christoph Lameter3ec09742007-05-16 22:11:00 -07001014static inline void setup_object_debug(struct kmem_cache *s,
1015 struct page *page, void *object) {}
Christoph Lameter41ecc552007-05-09 02:32:44 -07001016
Christoph Lameter3ec09742007-05-16 22:11:00 -07001017static inline int alloc_debug_processing(struct kmem_cache *s,
1018 struct page *page, void *object, void *addr) { return 0; }
Christoph Lameter41ecc552007-05-09 02:32:44 -07001019
Christoph Lameter3ec09742007-05-16 22:11:00 -07001020static inline int free_debug_processing(struct kmem_cache *s,
1021 struct page *page, void *object, void *addr) { return 0; }
Christoph Lameter41ecc552007-05-09 02:32:44 -07001022
Christoph Lameter41ecc552007-05-09 02:32:44 -07001023static inline int slab_pad_check(struct kmem_cache *s, struct page *page)
1024 { return 1; }
1025static inline int check_object(struct kmem_cache *s, struct page *page,
1026 void *object, int active) { return 1; }
Christoph Lameter3ec09742007-05-16 22:11:00 -07001027static inline void add_full(struct kmem_cache_node *n, struct page *page) {}
Christoph Lameterba0268a2007-09-11 15:24:11 -07001028static inline unsigned long kmem_cache_flags(unsigned long objsize,
1029 unsigned long flags, const char *name,
Christoph Lameter4ba9b9d2007-10-16 23:25:51 -07001030 void (*ctor)(struct kmem_cache *, void *))
Christoph Lameterba0268a2007-09-11 15:24:11 -07001031{
1032 return flags;
1033}
Christoph Lameter41ecc552007-05-09 02:32:44 -07001034#define slub_debug 0
1035#endif
Christoph Lameter81819f02007-05-06 14:49:36 -07001036/*
1037 * Slab allocation and freeing
1038 */
1039static struct page *allocate_slab(struct kmem_cache *s, gfp_t flags, int node)
1040{
1041 struct page * page;
1042 int pages = 1 << s->order;
1043
1044 if (s->order)
1045 flags |= __GFP_COMP;
1046
1047 if (s->flags & SLAB_CACHE_DMA)
1048 flags |= SLUB_DMA;
1049
Mel Gormane12ba742007-10-16 01:25:52 -07001050 if (s->flags & SLAB_RECLAIM_ACCOUNT)
1051 flags |= __GFP_RECLAIMABLE;
1052
Christoph Lameter81819f02007-05-06 14:49:36 -07001053 if (node == -1)
1054 page = alloc_pages(flags, s->order);
1055 else
1056 page = alloc_pages_node(node, flags, s->order);
1057
1058 if (!page)
1059 return NULL;
1060
1061 mod_zone_page_state(page_zone(page),
1062 (s->flags & SLAB_RECLAIM_ACCOUNT) ?
1063 NR_SLAB_RECLAIMABLE : NR_SLAB_UNRECLAIMABLE,
1064 pages);
1065
1066 return page;
1067}
1068
1069static void setup_object(struct kmem_cache *s, struct page *page,
1070 void *object)
1071{
Christoph Lameter3ec09742007-05-16 22:11:00 -07001072 setup_object_debug(s, page, object);
Christoph Lameter4f104932007-05-06 14:50:17 -07001073 if (unlikely(s->ctor))
Christoph Lameter4ba9b9d2007-10-16 23:25:51 -07001074 s->ctor(s, object);
Christoph Lameter81819f02007-05-06 14:49:36 -07001075}
1076
1077static struct page *new_slab(struct kmem_cache *s, gfp_t flags, int node)
1078{
1079 struct page *page;
1080 struct kmem_cache_node *n;
1081 void *start;
1082 void *end;
1083 void *last;
1084 void *p;
1085
Christoph Lameter6cb06222007-10-16 01:25:41 -07001086 BUG_ON(flags & GFP_SLAB_BUG_MASK);
Christoph Lameter81819f02007-05-06 14:49:36 -07001087
Christoph Lameter6cb06222007-10-16 01:25:41 -07001088 page = allocate_slab(s,
1089 flags & (GFP_RECLAIM_MASK | GFP_CONSTRAINT_MASK), node);
Christoph Lameter81819f02007-05-06 14:49:36 -07001090 if (!page)
1091 goto out;
1092
1093 n = get_node(s, page_to_nid(page));
1094 if (n)
1095 atomic_long_inc(&n->nr_slabs);
Christoph Lameter81819f02007-05-06 14:49:36 -07001096 page->slab = s;
1097 page->flags |= 1 << PG_slab;
1098 if (s->flags & (SLAB_DEBUG_FREE | SLAB_RED_ZONE | SLAB_POISON |
1099 SLAB_STORE_USER | SLAB_TRACE))
Christoph Lameter35e5d7e2007-05-09 02:32:42 -07001100 SetSlabDebug(page);
Christoph Lameter81819f02007-05-06 14:49:36 -07001101
1102 start = page_address(page);
1103 end = start + s->objects * s->size;
1104
1105 if (unlikely(s->flags & SLAB_POISON))
1106 memset(start, POISON_INUSE, PAGE_SIZE << s->order);
1107
1108 last = start;
Christoph Lameter7656c722007-05-09 02:32:40 -07001109 for_each_object(p, s, start) {
Christoph Lameter81819f02007-05-06 14:49:36 -07001110 setup_object(s, page, last);
1111 set_freepointer(s, last, p);
1112 last = p;
1113 }
1114 setup_object(s, page, last);
1115 set_freepointer(s, last, NULL);
1116
1117 page->freelist = start;
1118 page->inuse = 0;
1119out:
Christoph Lameter81819f02007-05-06 14:49:36 -07001120 return page;
1121}
1122
1123static void __free_slab(struct kmem_cache *s, struct page *page)
1124{
1125 int pages = 1 << s->order;
1126
Christoph Lameterc59def92007-05-16 22:10:50 -07001127 if (unlikely(SlabDebug(page))) {
Christoph Lameter81819f02007-05-06 14:49:36 -07001128 void *p;
1129
1130 slab_pad_check(s, page);
Christoph Lameterc59def92007-05-16 22:10:50 -07001131 for_each_object(p, s, page_address(page))
Christoph Lameter81819f02007-05-06 14:49:36 -07001132 check_object(s, page, p, 0);
Peter Zijlstra2208b762007-07-26 20:54:34 +02001133 ClearSlabDebug(page);
Christoph Lameter81819f02007-05-06 14:49:36 -07001134 }
1135
1136 mod_zone_page_state(page_zone(page),
1137 (s->flags & SLAB_RECLAIM_ACCOUNT) ?
1138 NR_SLAB_RECLAIMABLE : NR_SLAB_UNRECLAIMABLE,
1139 - pages);
1140
Christoph Lameter81819f02007-05-06 14:49:36 -07001141 __free_pages(page, s->order);
1142}
1143
1144static void rcu_free_slab(struct rcu_head *h)
1145{
1146 struct page *page;
1147
1148 page = container_of((struct list_head *)h, struct page, lru);
1149 __free_slab(page->slab, page);
1150}
1151
1152static void free_slab(struct kmem_cache *s, struct page *page)
1153{
1154 if (unlikely(s->flags & SLAB_DESTROY_BY_RCU)) {
1155 /*
1156 * RCU free overloads the RCU head over the LRU
1157 */
1158 struct rcu_head *head = (void *)&page->lru;
1159
1160 call_rcu(head, rcu_free_slab);
1161 } else
1162 __free_slab(s, page);
1163}
1164
1165static void discard_slab(struct kmem_cache *s, struct page *page)
1166{
1167 struct kmem_cache_node *n = get_node(s, page_to_nid(page));
1168
1169 atomic_long_dec(&n->nr_slabs);
1170 reset_page_mapcount(page);
Christoph Lameter35e5d7e2007-05-09 02:32:42 -07001171 __ClearPageSlab(page);
Christoph Lameter81819f02007-05-06 14:49:36 -07001172 free_slab(s, page);
1173}
1174
1175/*
1176 * Per slab locking using the pagelock
1177 */
1178static __always_inline void slab_lock(struct page *page)
1179{
1180 bit_spin_lock(PG_locked, &page->flags);
1181}
1182
1183static __always_inline void slab_unlock(struct page *page)
1184{
1185 bit_spin_unlock(PG_locked, &page->flags);
1186}
1187
1188static __always_inline int slab_trylock(struct page *page)
1189{
1190 int rc = 1;
1191
1192 rc = bit_spin_trylock(PG_locked, &page->flags);
1193 return rc;
1194}
1195
1196/*
1197 * Management of partially allocated slabs
1198 */
Christoph Lametere95eed52007-05-06 14:49:44 -07001199static void add_partial_tail(struct kmem_cache_node *n, struct page *page)
Christoph Lameter81819f02007-05-06 14:49:36 -07001200{
Christoph Lametere95eed52007-05-06 14:49:44 -07001201 spin_lock(&n->list_lock);
1202 n->nr_partial++;
1203 list_add_tail(&page->lru, &n->partial);
1204 spin_unlock(&n->list_lock);
1205}
Christoph Lameter81819f02007-05-06 14:49:36 -07001206
Christoph Lametere95eed52007-05-06 14:49:44 -07001207static void add_partial(struct kmem_cache_node *n, struct page *page)
1208{
Christoph Lameter81819f02007-05-06 14:49:36 -07001209 spin_lock(&n->list_lock);
1210 n->nr_partial++;
1211 list_add(&page->lru, &n->partial);
1212 spin_unlock(&n->list_lock);
1213}
1214
1215static void remove_partial(struct kmem_cache *s,
1216 struct page *page)
1217{
1218 struct kmem_cache_node *n = get_node(s, page_to_nid(page));
1219
1220 spin_lock(&n->list_lock);
1221 list_del(&page->lru);
1222 n->nr_partial--;
1223 spin_unlock(&n->list_lock);
1224}
1225
1226/*
Christoph Lameter672bba32007-05-09 02:32:39 -07001227 * Lock slab and remove from the partial list.
Christoph Lameter81819f02007-05-06 14:49:36 -07001228 *
Christoph Lameter672bba32007-05-09 02:32:39 -07001229 * Must hold list_lock.
Christoph Lameter81819f02007-05-06 14:49:36 -07001230 */
Christoph Lameter4b6f0752007-05-16 22:10:53 -07001231static inline int lock_and_freeze_slab(struct kmem_cache_node *n, struct page *page)
Christoph Lameter81819f02007-05-06 14:49:36 -07001232{
1233 if (slab_trylock(page)) {
1234 list_del(&page->lru);
1235 n->nr_partial--;
Christoph Lameter4b6f0752007-05-16 22:10:53 -07001236 SetSlabFrozen(page);
Christoph Lameter81819f02007-05-06 14:49:36 -07001237 return 1;
1238 }
1239 return 0;
1240}
1241
1242/*
Christoph Lameter672bba32007-05-09 02:32:39 -07001243 * Try to allocate a partial slab from a specific node.
Christoph Lameter81819f02007-05-06 14:49:36 -07001244 */
1245static struct page *get_partial_node(struct kmem_cache_node *n)
1246{
1247 struct page *page;
1248
1249 /*
1250 * Racy check. If we mistakenly see no partial slabs then we
1251 * just allocate an empty slab. If we mistakenly try to get a
Christoph Lameter672bba32007-05-09 02:32:39 -07001252 * partial slab and there is none available then get_partials()
1253 * will return NULL.
Christoph Lameter81819f02007-05-06 14:49:36 -07001254 */
1255 if (!n || !n->nr_partial)
1256 return NULL;
1257
1258 spin_lock(&n->list_lock);
1259 list_for_each_entry(page, &n->partial, lru)
Christoph Lameter4b6f0752007-05-16 22:10:53 -07001260 if (lock_and_freeze_slab(n, page))
Christoph Lameter81819f02007-05-06 14:49:36 -07001261 goto out;
1262 page = NULL;
1263out:
1264 spin_unlock(&n->list_lock);
1265 return page;
1266}
1267
1268/*
Christoph Lameter672bba32007-05-09 02:32:39 -07001269 * Get a page from somewhere. Search in increasing NUMA distances.
Christoph Lameter81819f02007-05-06 14:49:36 -07001270 */
1271static struct page *get_any_partial(struct kmem_cache *s, gfp_t flags)
1272{
1273#ifdef CONFIG_NUMA
1274 struct zonelist *zonelist;
1275 struct zone **z;
1276 struct page *page;
1277
1278 /*
Christoph Lameter672bba32007-05-09 02:32:39 -07001279 * The defrag ratio allows a configuration of the tradeoffs between
1280 * inter node defragmentation and node local allocations. A lower
1281 * defrag_ratio increases the tendency to do local allocations
1282 * instead of attempting to obtain partial slabs from other nodes.
Christoph Lameter81819f02007-05-06 14:49:36 -07001283 *
Christoph Lameter672bba32007-05-09 02:32:39 -07001284 * If the defrag_ratio is set to 0 then kmalloc() always
1285 * returns node local objects. If the ratio is higher then kmalloc()
1286 * may return off node objects because partial slabs are obtained
1287 * from other nodes and filled up.
Christoph Lameter81819f02007-05-06 14:49:36 -07001288 *
1289 * If /sys/slab/xx/defrag_ratio is set to 100 (which makes
Christoph Lameter672bba32007-05-09 02:32:39 -07001290 * defrag_ratio = 1000) then every (well almost) allocation will
1291 * first attempt to defrag slab caches on other nodes. This means
1292 * scanning over all nodes to look for partial slabs which may be
1293 * expensive if we do it every time we are trying to find a slab
1294 * with available objects.
Christoph Lameter81819f02007-05-06 14:49:36 -07001295 */
1296 if (!s->defrag_ratio || get_cycles() % 1024 > s->defrag_ratio)
1297 return NULL;
1298
1299 zonelist = &NODE_DATA(slab_node(current->mempolicy))
1300 ->node_zonelists[gfp_zone(flags)];
1301 for (z = zonelist->zones; *z; z++) {
1302 struct kmem_cache_node *n;
1303
1304 n = get_node(s, zone_to_nid(*z));
1305
1306 if (n && cpuset_zone_allowed_hardwall(*z, flags) &&
Christoph Lametere95eed52007-05-06 14:49:44 -07001307 n->nr_partial > MIN_PARTIAL) {
Christoph Lameter81819f02007-05-06 14:49:36 -07001308 page = get_partial_node(n);
1309 if (page)
1310 return page;
1311 }
1312 }
1313#endif
1314 return NULL;
1315}
1316
1317/*
1318 * Get a partial page, lock it and return it.
1319 */
1320static struct page *get_partial(struct kmem_cache *s, gfp_t flags, int node)
1321{
1322 struct page *page;
1323 int searchnode = (node == -1) ? numa_node_id() : node;
1324
1325 page = get_partial_node(get_node(s, searchnode));
1326 if (page || (flags & __GFP_THISNODE))
1327 return page;
1328
1329 return get_any_partial(s, flags);
1330}
1331
1332/*
1333 * Move a page back to the lists.
1334 *
1335 * Must be called with the slab lock held.
1336 *
1337 * On exit the slab lock will have been dropped.
1338 */
Christoph Lameter4b6f0752007-05-16 22:10:53 -07001339static void unfreeze_slab(struct kmem_cache *s, struct page *page)
Christoph Lameter81819f02007-05-06 14:49:36 -07001340{
Christoph Lametere95eed52007-05-06 14:49:44 -07001341 struct kmem_cache_node *n = get_node(s, page_to_nid(page));
1342
Christoph Lameter4b6f0752007-05-16 22:10:53 -07001343 ClearSlabFrozen(page);
Christoph Lameter81819f02007-05-06 14:49:36 -07001344 if (page->inuse) {
Christoph Lametere95eed52007-05-06 14:49:44 -07001345
Christoph Lameter81819f02007-05-06 14:49:36 -07001346 if (page->freelist)
Christoph Lametere95eed52007-05-06 14:49:44 -07001347 add_partial(n, page);
Christoph Lameter35e5d7e2007-05-09 02:32:42 -07001348 else if (SlabDebug(page) && (s->flags & SLAB_STORE_USER))
Christoph Lametere95eed52007-05-06 14:49:44 -07001349 add_full(n, page);
Christoph Lameter81819f02007-05-06 14:49:36 -07001350 slab_unlock(page);
Christoph Lametere95eed52007-05-06 14:49:44 -07001351
Christoph Lameter81819f02007-05-06 14:49:36 -07001352 } else {
Christoph Lametere95eed52007-05-06 14:49:44 -07001353 if (n->nr_partial < MIN_PARTIAL) {
1354 /*
Christoph Lameter672bba32007-05-09 02:32:39 -07001355 * Adding an empty slab to the partial slabs in order
1356 * to avoid page allocator overhead. This slab needs
1357 * to come after the other slabs with objects in
1358 * order to fill them up. That way the size of the
1359 * partial list stays small. kmem_cache_shrink can
1360 * reclaim empty slabs from the partial list.
Christoph Lametere95eed52007-05-06 14:49:44 -07001361 */
1362 add_partial_tail(n, page);
1363 slab_unlock(page);
1364 } else {
1365 slab_unlock(page);
1366 discard_slab(s, page);
1367 }
Christoph Lameter81819f02007-05-06 14:49:36 -07001368 }
1369}
1370
1371/*
1372 * Remove the cpu slab
1373 */
Christoph Lameterdfb4f092007-10-16 01:26:05 -07001374static void deactivate_slab(struct kmem_cache *s, struct kmem_cache_cpu *c)
Christoph Lameter81819f02007-05-06 14:49:36 -07001375{
Christoph Lameterdfb4f092007-10-16 01:26:05 -07001376 struct page *page = c->page;
Christoph Lameter894b8782007-05-10 03:15:16 -07001377 /*
1378 * Merge cpu freelist into freelist. Typically we get here
1379 * because both freelists are empty. So this is unlikely
1380 * to occur.
1381 */
Christoph Lameterdfb4f092007-10-16 01:26:05 -07001382 while (unlikely(c->freelist)) {
Christoph Lameter894b8782007-05-10 03:15:16 -07001383 void **object;
1384
1385 /* Retrieve object from cpu_freelist */
Christoph Lameterdfb4f092007-10-16 01:26:05 -07001386 object = c->freelist;
Christoph Lameterb3fba8d2007-10-16 01:26:06 -07001387 c->freelist = c->freelist[c->offset];
Christoph Lameter894b8782007-05-10 03:15:16 -07001388
1389 /* And put onto the regular freelist */
Christoph Lameterb3fba8d2007-10-16 01:26:06 -07001390 object[c->offset] = page->freelist;
Christoph Lameter894b8782007-05-10 03:15:16 -07001391 page->freelist = object;
1392 page->inuse--;
1393 }
Christoph Lameterdfb4f092007-10-16 01:26:05 -07001394 c->page = NULL;
Christoph Lameter4b6f0752007-05-16 22:10:53 -07001395 unfreeze_slab(s, page);
Christoph Lameter81819f02007-05-06 14:49:36 -07001396}
1397
Christoph Lameterdfb4f092007-10-16 01:26:05 -07001398static inline void flush_slab(struct kmem_cache *s, struct kmem_cache_cpu *c)
Christoph Lameter81819f02007-05-06 14:49:36 -07001399{
Christoph Lameterdfb4f092007-10-16 01:26:05 -07001400 slab_lock(c->page);
1401 deactivate_slab(s, c);
Christoph Lameter81819f02007-05-06 14:49:36 -07001402}
1403
1404/*
1405 * Flush cpu slab.
1406 * Called from IPI handler with interrupts disabled.
1407 */
Christoph Lameter0c710012007-07-17 04:03:24 -07001408static inline void __flush_cpu_slab(struct kmem_cache *s, int cpu)
Christoph Lameter81819f02007-05-06 14:49:36 -07001409{
Christoph Lameterdfb4f092007-10-16 01:26:05 -07001410 struct kmem_cache_cpu *c = get_cpu_slab(s, cpu);
Christoph Lameter81819f02007-05-06 14:49:36 -07001411
Christoph Lameterdfb4f092007-10-16 01:26:05 -07001412 if (likely(c && c->page))
1413 flush_slab(s, c);
Christoph Lameter81819f02007-05-06 14:49:36 -07001414}
1415
1416static void flush_cpu_slab(void *d)
1417{
1418 struct kmem_cache *s = d;
Christoph Lameter81819f02007-05-06 14:49:36 -07001419
Christoph Lameterdfb4f092007-10-16 01:26:05 -07001420 __flush_cpu_slab(s, smp_processor_id());
Christoph Lameter81819f02007-05-06 14:49:36 -07001421}
1422
1423static void flush_all(struct kmem_cache *s)
1424{
1425#ifdef CONFIG_SMP
1426 on_each_cpu(flush_cpu_slab, s, 1, 1);
1427#else
1428 unsigned long flags;
1429
1430 local_irq_save(flags);
1431 flush_cpu_slab(s);
1432 local_irq_restore(flags);
1433#endif
1434}
1435
1436/*
Christoph Lameterdfb4f092007-10-16 01:26:05 -07001437 * Check if the objects in a per cpu structure fit numa
1438 * locality expectations.
1439 */
1440static inline int node_match(struct kmem_cache_cpu *c, int node)
1441{
1442#ifdef CONFIG_NUMA
1443 if (node != -1 && c->node != node)
1444 return 0;
1445#endif
1446 return 1;
1447}
1448
1449/*
Christoph Lameter894b8782007-05-10 03:15:16 -07001450 * Slow path. The lockless freelist is empty or we need to perform
1451 * debugging duties.
Christoph Lameter81819f02007-05-06 14:49:36 -07001452 *
Christoph Lameter894b8782007-05-10 03:15:16 -07001453 * Interrupts are disabled.
Christoph Lameter81819f02007-05-06 14:49:36 -07001454 *
Christoph Lameter894b8782007-05-10 03:15:16 -07001455 * Processing is still very fast if new objects have been freed to the
1456 * regular freelist. In that case we simply take over the regular freelist
1457 * as the lockless freelist and zap the regular freelist.
Christoph Lameter81819f02007-05-06 14:49:36 -07001458 *
Christoph Lameter894b8782007-05-10 03:15:16 -07001459 * If that is not working then we fall back to the partial lists. We take the
1460 * first element of the freelist as the object to allocate now and move the
1461 * rest of the freelist to the lockless freelist.
1462 *
1463 * And if we were unable to get a new slab from the partial slab lists then
1464 * we need to allocate a new slab. This is slowest path since we may sleep.
Christoph Lameter81819f02007-05-06 14:49:36 -07001465 */
Christoph Lameter894b8782007-05-10 03:15:16 -07001466static void *__slab_alloc(struct kmem_cache *s,
Christoph Lameterdfb4f092007-10-16 01:26:05 -07001467 gfp_t gfpflags, int node, void *addr, struct kmem_cache_cpu *c)
Christoph Lameter81819f02007-05-06 14:49:36 -07001468{
Christoph Lameter81819f02007-05-06 14:49:36 -07001469 void **object;
Christoph Lameterdfb4f092007-10-16 01:26:05 -07001470 struct page *new;
Christoph Lameter81819f02007-05-06 14:49:36 -07001471
Christoph Lameterdfb4f092007-10-16 01:26:05 -07001472 if (!c->page)
Christoph Lameter81819f02007-05-06 14:49:36 -07001473 goto new_slab;
1474
Christoph Lameterdfb4f092007-10-16 01:26:05 -07001475 slab_lock(c->page);
1476 if (unlikely(!node_match(c, node)))
Christoph Lameter81819f02007-05-06 14:49:36 -07001477 goto another_slab;
Christoph Lameter894b8782007-05-10 03:15:16 -07001478load_freelist:
Christoph Lameterdfb4f092007-10-16 01:26:05 -07001479 object = c->page->freelist;
Christoph Lameter81819f02007-05-06 14:49:36 -07001480 if (unlikely(!object))
1481 goto another_slab;
Christoph Lameterdfb4f092007-10-16 01:26:05 -07001482 if (unlikely(SlabDebug(c->page)))
Christoph Lameter81819f02007-05-06 14:49:36 -07001483 goto debug;
1484
Christoph Lameterdfb4f092007-10-16 01:26:05 -07001485 object = c->page->freelist;
Christoph Lameterb3fba8d2007-10-16 01:26:06 -07001486 c->freelist = object[c->offset];
Christoph Lameterdfb4f092007-10-16 01:26:05 -07001487 c->page->inuse = s->objects;
1488 c->page->freelist = NULL;
1489 c->node = page_to_nid(c->page);
1490 slab_unlock(c->page);
Christoph Lameter81819f02007-05-06 14:49:36 -07001491 return object;
1492
1493another_slab:
Christoph Lameterdfb4f092007-10-16 01:26:05 -07001494 deactivate_slab(s, c);
Christoph Lameter81819f02007-05-06 14:49:36 -07001495
1496new_slab:
Christoph Lameterdfb4f092007-10-16 01:26:05 -07001497 new = get_partial(s, gfpflags, node);
1498 if (new) {
1499 c->page = new;
Christoph Lameter894b8782007-05-10 03:15:16 -07001500 goto load_freelist;
Christoph Lameter81819f02007-05-06 14:49:36 -07001501 }
1502
Christoph Lameterb811c202007-10-16 23:25:51 -07001503 if (gfpflags & __GFP_WAIT)
1504 local_irq_enable();
1505
Christoph Lameterdfb4f092007-10-16 01:26:05 -07001506 new = new_slab(s, gfpflags, node);
Christoph Lameterb811c202007-10-16 23:25:51 -07001507
1508 if (gfpflags & __GFP_WAIT)
1509 local_irq_disable();
1510
Christoph Lameterdfb4f092007-10-16 01:26:05 -07001511 if (new) {
1512 c = get_cpu_slab(s, smp_processor_id());
1513 if (c->page) {
Christoph Lameter81819f02007-05-06 14:49:36 -07001514 /*
Christoph Lameter672bba32007-05-09 02:32:39 -07001515 * Someone else populated the cpu_slab while we
1516 * enabled interrupts, or we have gotten scheduled
1517 * on another cpu. The page may not be on the
1518 * requested node even if __GFP_THISNODE was
1519 * specified. So we need to recheck.
Christoph Lameter81819f02007-05-06 14:49:36 -07001520 */
Christoph Lameterdfb4f092007-10-16 01:26:05 -07001521 if (node_match(c, node)) {
Christoph Lameter81819f02007-05-06 14:49:36 -07001522 /*
1523 * Current cpuslab is acceptable and we
1524 * want the current one since its cache hot
1525 */
Christoph Lameterdfb4f092007-10-16 01:26:05 -07001526 discard_slab(s, new);
1527 slab_lock(c->page);
Christoph Lameter894b8782007-05-10 03:15:16 -07001528 goto load_freelist;
Christoph Lameter81819f02007-05-06 14:49:36 -07001529 }
Christoph Lameter672bba32007-05-09 02:32:39 -07001530 /* New slab does not fit our expectations */
Christoph Lameterdfb4f092007-10-16 01:26:05 -07001531 flush_slab(s, c);
Christoph Lameter81819f02007-05-06 14:49:36 -07001532 }
Christoph Lameterdfb4f092007-10-16 01:26:05 -07001533 slab_lock(new);
1534 SetSlabFrozen(new);
1535 c->page = new;
Christoph Lameter4b6f0752007-05-16 22:10:53 -07001536 goto load_freelist;
Christoph Lameter81819f02007-05-06 14:49:36 -07001537 }
Christoph Lameter81819f02007-05-06 14:49:36 -07001538 return NULL;
1539debug:
Christoph Lameterdfb4f092007-10-16 01:26:05 -07001540 object = c->page->freelist;
1541 if (!alloc_debug_processing(s, c->page, object, addr))
Christoph Lameter81819f02007-05-06 14:49:36 -07001542 goto another_slab;
Christoph Lameter894b8782007-05-10 03:15:16 -07001543
Christoph Lameterdfb4f092007-10-16 01:26:05 -07001544 c->page->inuse++;
Christoph Lameterb3fba8d2007-10-16 01:26:06 -07001545 c->page->freelist = object[c->offset];
Christoph Lameteree3c72a2007-10-16 01:26:07 -07001546 c->node = -1;
Christoph Lameterdfb4f092007-10-16 01:26:05 -07001547 slab_unlock(c->page);
Christoph Lameter894b8782007-05-10 03:15:16 -07001548 return object;
1549}
1550
1551/*
1552 * Inlined fastpath so that allocation functions (kmalloc, kmem_cache_alloc)
1553 * have the fastpath folded into their functions. So no function call
1554 * overhead for requests that can be satisfied on the fastpath.
1555 *
1556 * The fastpath works by first checking if the lockless freelist can be used.
1557 * If not then __slab_alloc is called for slow processing.
1558 *
1559 * Otherwise we can simply pick the next object from the lockless free list.
1560 */
1561static void __always_inline *slab_alloc(struct kmem_cache *s,
Christoph Lameterce15fea2007-07-17 04:03:28 -07001562 gfp_t gfpflags, int node, void *addr)
Christoph Lameter894b8782007-05-10 03:15:16 -07001563{
Christoph Lameter894b8782007-05-10 03:15:16 -07001564 void **object;
1565 unsigned long flags;
Christoph Lameterdfb4f092007-10-16 01:26:05 -07001566 struct kmem_cache_cpu *c;
Christoph Lameter894b8782007-05-10 03:15:16 -07001567
1568 local_irq_save(flags);
Christoph Lameterdfb4f092007-10-16 01:26:05 -07001569 c = get_cpu_slab(s, smp_processor_id());
Christoph Lameteree3c72a2007-10-16 01:26:07 -07001570 if (unlikely(!c->freelist || !node_match(c, node)))
Christoph Lameter894b8782007-05-10 03:15:16 -07001571
Christoph Lameterdfb4f092007-10-16 01:26:05 -07001572 object = __slab_alloc(s, gfpflags, node, addr, c);
Christoph Lameter894b8782007-05-10 03:15:16 -07001573
1574 else {
Christoph Lameterdfb4f092007-10-16 01:26:05 -07001575 object = c->freelist;
Christoph Lameterb3fba8d2007-10-16 01:26:06 -07001576 c->freelist = object[c->offset];
Christoph Lameter894b8782007-05-10 03:15:16 -07001577 }
1578 local_irq_restore(flags);
Christoph Lameterd07dbea2007-07-17 04:03:23 -07001579
1580 if (unlikely((gfpflags & __GFP_ZERO) && object))
Christoph Lameter42a9fdb2007-10-16 01:26:09 -07001581 memset(object, 0, c->objsize);
Christoph Lameterd07dbea2007-07-17 04:03:23 -07001582
Christoph Lameter894b8782007-05-10 03:15:16 -07001583 return object;
Christoph Lameter81819f02007-05-06 14:49:36 -07001584}
1585
1586void *kmem_cache_alloc(struct kmem_cache *s, gfp_t gfpflags)
1587{
Christoph Lameterce15fea2007-07-17 04:03:28 -07001588 return slab_alloc(s, gfpflags, -1, __builtin_return_address(0));
Christoph Lameter81819f02007-05-06 14:49:36 -07001589}
1590EXPORT_SYMBOL(kmem_cache_alloc);
1591
1592#ifdef CONFIG_NUMA
1593void *kmem_cache_alloc_node(struct kmem_cache *s, gfp_t gfpflags, int node)
1594{
Christoph Lameterce15fea2007-07-17 04:03:28 -07001595 return slab_alloc(s, gfpflags, node, __builtin_return_address(0));
Christoph Lameter81819f02007-05-06 14:49:36 -07001596}
1597EXPORT_SYMBOL(kmem_cache_alloc_node);
1598#endif
1599
1600/*
Christoph Lameter894b8782007-05-10 03:15:16 -07001601 * Slow patch handling. This may still be called frequently since objects
1602 * have a longer lifetime than the cpu slabs in most processing loads.
Christoph Lameter81819f02007-05-06 14:49:36 -07001603 *
Christoph Lameter894b8782007-05-10 03:15:16 -07001604 * So we still attempt to reduce cache line usage. Just take the slab
1605 * lock and free the item. If there is no additional partial page
1606 * handling required then we can return immediately.
Christoph Lameter81819f02007-05-06 14:49:36 -07001607 */
Christoph Lameter894b8782007-05-10 03:15:16 -07001608static void __slab_free(struct kmem_cache *s, struct page *page,
Christoph Lameterb3fba8d2007-10-16 01:26:06 -07001609 void *x, void *addr, unsigned int offset)
Christoph Lameter81819f02007-05-06 14:49:36 -07001610{
1611 void *prior;
1612 void **object = (void *)x;
Christoph Lameter81819f02007-05-06 14:49:36 -07001613
Christoph Lameter81819f02007-05-06 14:49:36 -07001614 slab_lock(page);
1615
Christoph Lameter35e5d7e2007-05-09 02:32:42 -07001616 if (unlikely(SlabDebug(page)))
Christoph Lameter81819f02007-05-06 14:49:36 -07001617 goto debug;
1618checks_ok:
Christoph Lameterb3fba8d2007-10-16 01:26:06 -07001619 prior = object[offset] = page->freelist;
Christoph Lameter81819f02007-05-06 14:49:36 -07001620 page->freelist = object;
1621 page->inuse--;
1622
Christoph Lameter4b6f0752007-05-16 22:10:53 -07001623 if (unlikely(SlabFrozen(page)))
Christoph Lameter81819f02007-05-06 14:49:36 -07001624 goto out_unlock;
1625
1626 if (unlikely(!page->inuse))
1627 goto slab_empty;
1628
1629 /*
1630 * Objects left in the slab. If it
1631 * was not on the partial list before
1632 * then add it.
1633 */
1634 if (unlikely(!prior))
Christoph Lametere95eed52007-05-06 14:49:44 -07001635 add_partial(get_node(s, page_to_nid(page)), page);
Christoph Lameter81819f02007-05-06 14:49:36 -07001636
1637out_unlock:
1638 slab_unlock(page);
Christoph Lameter81819f02007-05-06 14:49:36 -07001639 return;
1640
1641slab_empty:
1642 if (prior)
1643 /*
Christoph Lameter672bba32007-05-09 02:32:39 -07001644 * Slab still on the partial list.
Christoph Lameter81819f02007-05-06 14:49:36 -07001645 */
1646 remove_partial(s, page);
1647
1648 slab_unlock(page);
1649 discard_slab(s, page);
Christoph Lameter81819f02007-05-06 14:49:36 -07001650 return;
1651
1652debug:
Christoph Lameter3ec09742007-05-16 22:11:00 -07001653 if (!free_debug_processing(s, page, x, addr))
Christoph Lameter77c5e2d2007-05-06 14:49:42 -07001654 goto out_unlock;
Christoph Lameter77c5e2d2007-05-06 14:49:42 -07001655 goto checks_ok;
Christoph Lameter81819f02007-05-06 14:49:36 -07001656}
1657
Christoph Lameter894b8782007-05-10 03:15:16 -07001658/*
1659 * Fastpath with forced inlining to produce a kfree and kmem_cache_free that
1660 * can perform fastpath freeing without additional function calls.
1661 *
1662 * The fastpath is only possible if we are freeing to the current cpu slab
1663 * of this processor. This typically the case if we have just allocated
1664 * the item before.
1665 *
1666 * If fastpath is not possible then fall back to __slab_free where we deal
1667 * with all sorts of special processing.
1668 */
1669static void __always_inline slab_free(struct kmem_cache *s,
1670 struct page *page, void *x, void *addr)
1671{
1672 void **object = (void *)x;
1673 unsigned long flags;
Christoph Lameterdfb4f092007-10-16 01:26:05 -07001674 struct kmem_cache_cpu *c;
Christoph Lameter894b8782007-05-10 03:15:16 -07001675
1676 local_irq_save(flags);
Peter Zijlstra02febdf2007-07-26 20:01:38 +02001677 debug_check_no_locks_freed(object, s->objsize);
Christoph Lameterdfb4f092007-10-16 01:26:05 -07001678 c = get_cpu_slab(s, smp_processor_id());
Christoph Lameteree3c72a2007-10-16 01:26:07 -07001679 if (likely(page == c->page && c->node >= 0)) {
Christoph Lameterb3fba8d2007-10-16 01:26:06 -07001680 object[c->offset] = c->freelist;
Christoph Lameterdfb4f092007-10-16 01:26:05 -07001681 c->freelist = object;
Christoph Lameter894b8782007-05-10 03:15:16 -07001682 } else
Christoph Lameterb3fba8d2007-10-16 01:26:06 -07001683 __slab_free(s, page, x, addr, c->offset);
Christoph Lameter894b8782007-05-10 03:15:16 -07001684
1685 local_irq_restore(flags);
1686}
1687
Christoph Lameter81819f02007-05-06 14:49:36 -07001688void kmem_cache_free(struct kmem_cache *s, void *x)
1689{
Christoph Lameter77c5e2d2007-05-06 14:49:42 -07001690 struct page *page;
Christoph Lameter81819f02007-05-06 14:49:36 -07001691
Christoph Lameterb49af682007-05-06 14:49:41 -07001692 page = virt_to_head_page(x);
Christoph Lameter81819f02007-05-06 14:49:36 -07001693
Christoph Lameter77c5e2d2007-05-06 14:49:42 -07001694 slab_free(s, page, x, __builtin_return_address(0));
Christoph Lameter81819f02007-05-06 14:49:36 -07001695}
1696EXPORT_SYMBOL(kmem_cache_free);
1697
1698/* Figure out on which slab object the object resides */
1699static struct page *get_object_page(const void *x)
1700{
Christoph Lameterb49af682007-05-06 14:49:41 -07001701 struct page *page = virt_to_head_page(x);
Christoph Lameter81819f02007-05-06 14:49:36 -07001702
1703 if (!PageSlab(page))
1704 return NULL;
1705
1706 return page;
1707}
1708
1709/*
Christoph Lameter672bba32007-05-09 02:32:39 -07001710 * Object placement in a slab is made very easy because we always start at
1711 * offset 0. If we tune the size of the object to the alignment then we can
1712 * get the required alignment by putting one properly sized object after
1713 * another.
Christoph Lameter81819f02007-05-06 14:49:36 -07001714 *
1715 * Notice that the allocation order determines the sizes of the per cpu
1716 * caches. Each processor has always one slab available for allocations.
1717 * Increasing the allocation order reduces the number of times that slabs
Christoph Lameter672bba32007-05-09 02:32:39 -07001718 * must be moved on and off the partial lists and is therefore a factor in
Christoph Lameter81819f02007-05-06 14:49:36 -07001719 * locking overhead.
Christoph Lameter81819f02007-05-06 14:49:36 -07001720 */
1721
1722/*
1723 * Mininum / Maximum order of slab pages. This influences locking overhead
1724 * and slab fragmentation. A higher order reduces the number of partial slabs
1725 * and increases the number of allocations possible without having to
1726 * take the list_lock.
1727 */
1728static int slub_min_order;
1729static int slub_max_order = DEFAULT_MAX_ORDER;
Christoph Lameter81819f02007-05-06 14:49:36 -07001730static int slub_min_objects = DEFAULT_MIN_OBJECTS;
1731
1732/*
1733 * Merge control. If this is set then no merging of slab caches will occur.
Christoph Lameter672bba32007-05-09 02:32:39 -07001734 * (Could be removed. This was introduced to pacify the merge skeptics.)
Christoph Lameter81819f02007-05-06 14:49:36 -07001735 */
1736static int slub_nomerge;
1737
1738/*
Christoph Lameter81819f02007-05-06 14:49:36 -07001739 * Calculate the order of allocation given an slab object size.
1740 *
Christoph Lameter672bba32007-05-09 02:32:39 -07001741 * The order of allocation has significant impact on performance and other
1742 * system components. Generally order 0 allocations should be preferred since
1743 * order 0 does not cause fragmentation in the page allocator. Larger objects
1744 * be problematic to put into order 0 slabs because there may be too much
1745 * unused space left. We go to a higher order if more than 1/8th of the slab
1746 * would be wasted.
Christoph Lameter81819f02007-05-06 14:49:36 -07001747 *
Christoph Lameter672bba32007-05-09 02:32:39 -07001748 * In order to reach satisfactory performance we must ensure that a minimum
1749 * number of objects is in one slab. Otherwise we may generate too much
1750 * activity on the partial lists which requires taking the list_lock. This is
1751 * less a concern for large slabs though which are rarely used.
Christoph Lameter81819f02007-05-06 14:49:36 -07001752 *
Christoph Lameter672bba32007-05-09 02:32:39 -07001753 * slub_max_order specifies the order where we begin to stop considering the
1754 * number of objects in a slab as critical. If we reach slub_max_order then
1755 * we try to keep the page order as low as possible. So we accept more waste
1756 * of space in favor of a small page order.
1757 *
1758 * Higher order allocations also allow the placement of more objects in a
1759 * slab and thereby reduce object handling overhead. If the user has
1760 * requested a higher mininum order then we start with that one instead of
1761 * the smallest order which will fit the object.
Christoph Lameter81819f02007-05-06 14:49:36 -07001762 */
Christoph Lameter5e6d4442007-05-09 02:32:46 -07001763static inline int slab_order(int size, int min_objects,
1764 int max_order, int fract_leftover)
Christoph Lameter81819f02007-05-06 14:49:36 -07001765{
1766 int order;
1767 int rem;
Christoph Lameter6300ea72007-07-17 04:03:20 -07001768 int min_order = slub_min_order;
Christoph Lameter81819f02007-05-06 14:49:36 -07001769
Christoph Lameter6300ea72007-07-17 04:03:20 -07001770 for (order = max(min_order,
Christoph Lameter5e6d4442007-05-09 02:32:46 -07001771 fls(min_objects * size - 1) - PAGE_SHIFT);
1772 order <= max_order; order++) {
1773
Christoph Lameter81819f02007-05-06 14:49:36 -07001774 unsigned long slab_size = PAGE_SIZE << order;
1775
Christoph Lameter5e6d4442007-05-09 02:32:46 -07001776 if (slab_size < min_objects * size)
Christoph Lameter81819f02007-05-06 14:49:36 -07001777 continue;
1778
Christoph Lameter81819f02007-05-06 14:49:36 -07001779 rem = slab_size % size;
1780
Christoph Lameter5e6d4442007-05-09 02:32:46 -07001781 if (rem <= slab_size / fract_leftover)
Christoph Lameter81819f02007-05-06 14:49:36 -07001782 break;
1783
1784 }
Christoph Lameter672bba32007-05-09 02:32:39 -07001785
Christoph Lameter81819f02007-05-06 14:49:36 -07001786 return order;
1787}
1788
Christoph Lameter5e6d4442007-05-09 02:32:46 -07001789static inline int calculate_order(int size)
1790{
1791 int order;
1792 int min_objects;
1793 int fraction;
1794
1795 /*
1796 * Attempt to find best configuration for a slab. This
1797 * works by first attempting to generate a layout with
1798 * the best configuration and backing off gradually.
1799 *
1800 * First we reduce the acceptable waste in a slab. Then
1801 * we reduce the minimum objects required in a slab.
1802 */
1803 min_objects = slub_min_objects;
1804 while (min_objects > 1) {
1805 fraction = 8;
1806 while (fraction >= 4) {
1807 order = slab_order(size, min_objects,
1808 slub_max_order, fraction);
1809 if (order <= slub_max_order)
1810 return order;
1811 fraction /= 2;
1812 }
1813 min_objects /= 2;
1814 }
1815
1816 /*
1817 * We were unable to place multiple objects in a slab. Now
1818 * lets see if we can place a single object there.
1819 */
1820 order = slab_order(size, 1, slub_max_order, 1);
1821 if (order <= slub_max_order)
1822 return order;
1823
1824 /*
1825 * Doh this slab cannot be placed using slub_max_order.
1826 */
1827 order = slab_order(size, 1, MAX_ORDER, 1);
1828 if (order <= MAX_ORDER)
1829 return order;
1830 return -ENOSYS;
1831}
1832
Christoph Lameter81819f02007-05-06 14:49:36 -07001833/*
Christoph Lameter672bba32007-05-09 02:32:39 -07001834 * Figure out what the alignment of the objects will be.
Christoph Lameter81819f02007-05-06 14:49:36 -07001835 */
1836static unsigned long calculate_alignment(unsigned long flags,
1837 unsigned long align, unsigned long size)
1838{
1839 /*
1840 * If the user wants hardware cache aligned objects then
1841 * follow that suggestion if the object is sufficiently
1842 * large.
1843 *
1844 * The hardware cache alignment cannot override the
1845 * specified alignment though. If that is greater
1846 * then use it.
1847 */
Christoph Lameter5af60832007-05-06 14:49:56 -07001848 if ((flags & SLAB_HWCACHE_ALIGN) &&
Christoph Lameter65c02d42007-05-09 02:32:35 -07001849 size > cache_line_size() / 2)
1850 return max_t(unsigned long, align, cache_line_size());
Christoph Lameter81819f02007-05-06 14:49:36 -07001851
1852 if (align < ARCH_SLAB_MINALIGN)
1853 return ARCH_SLAB_MINALIGN;
1854
1855 return ALIGN(align, sizeof(void *));
1856}
1857
Christoph Lameterdfb4f092007-10-16 01:26:05 -07001858static void init_kmem_cache_cpu(struct kmem_cache *s,
1859 struct kmem_cache_cpu *c)
1860{
1861 c->page = NULL;
1862 c->freelist = NULL;
1863 c->node = 0;
Christoph Lameter42a9fdb2007-10-16 01:26:09 -07001864 c->offset = s->offset / sizeof(void *);
1865 c->objsize = s->objsize;
Christoph Lameterdfb4f092007-10-16 01:26:05 -07001866}
1867
Christoph Lameter81819f02007-05-06 14:49:36 -07001868static void init_kmem_cache_node(struct kmem_cache_node *n)
1869{
1870 n->nr_partial = 0;
1871 atomic_long_set(&n->nr_slabs, 0);
1872 spin_lock_init(&n->list_lock);
1873 INIT_LIST_HEAD(&n->partial);
Christoph Lameter8ab13722007-07-17 04:03:32 -07001874#ifdef CONFIG_SLUB_DEBUG
Christoph Lameter643b1132007-05-06 14:49:42 -07001875 INIT_LIST_HEAD(&n->full);
Christoph Lameter8ab13722007-07-17 04:03:32 -07001876#endif
Christoph Lameter81819f02007-05-06 14:49:36 -07001877}
1878
Christoph Lameter4c93c3552007-10-16 01:26:08 -07001879#ifdef CONFIG_SMP
1880/*
1881 * Per cpu array for per cpu structures.
1882 *
1883 * The per cpu array places all kmem_cache_cpu structures from one processor
1884 * close together meaning that it becomes possible that multiple per cpu
1885 * structures are contained in one cacheline. This may be particularly
1886 * beneficial for the kmalloc caches.
1887 *
1888 * A desktop system typically has around 60-80 slabs. With 100 here we are
1889 * likely able to get per cpu structures for all caches from the array defined
1890 * here. We must be able to cover all kmalloc caches during bootstrap.
1891 *
1892 * If the per cpu array is exhausted then fall back to kmalloc
1893 * of individual cachelines. No sharing is possible then.
1894 */
1895#define NR_KMEM_CACHE_CPU 100
1896
1897static DEFINE_PER_CPU(struct kmem_cache_cpu,
1898 kmem_cache_cpu)[NR_KMEM_CACHE_CPU];
1899
1900static DEFINE_PER_CPU(struct kmem_cache_cpu *, kmem_cache_cpu_free);
1901static cpumask_t kmem_cach_cpu_free_init_once = CPU_MASK_NONE;
1902
1903static struct kmem_cache_cpu *alloc_kmem_cache_cpu(struct kmem_cache *s,
1904 int cpu, gfp_t flags)
1905{
1906 struct kmem_cache_cpu *c = per_cpu(kmem_cache_cpu_free, cpu);
1907
1908 if (c)
1909 per_cpu(kmem_cache_cpu_free, cpu) =
1910 (void *)c->freelist;
1911 else {
1912 /* Table overflow: So allocate ourselves */
1913 c = kmalloc_node(
1914 ALIGN(sizeof(struct kmem_cache_cpu), cache_line_size()),
1915 flags, cpu_to_node(cpu));
1916 if (!c)
1917 return NULL;
1918 }
1919
1920 init_kmem_cache_cpu(s, c);
1921 return c;
1922}
1923
1924static void free_kmem_cache_cpu(struct kmem_cache_cpu *c, int cpu)
1925{
1926 if (c < per_cpu(kmem_cache_cpu, cpu) ||
1927 c > per_cpu(kmem_cache_cpu, cpu) + NR_KMEM_CACHE_CPU) {
1928 kfree(c);
1929 return;
1930 }
1931 c->freelist = (void *)per_cpu(kmem_cache_cpu_free, cpu);
1932 per_cpu(kmem_cache_cpu_free, cpu) = c;
1933}
1934
1935static void free_kmem_cache_cpus(struct kmem_cache *s)
1936{
1937 int cpu;
1938
1939 for_each_online_cpu(cpu) {
1940 struct kmem_cache_cpu *c = get_cpu_slab(s, cpu);
1941
1942 if (c) {
1943 s->cpu_slab[cpu] = NULL;
1944 free_kmem_cache_cpu(c, cpu);
1945 }
1946 }
1947}
1948
1949static int alloc_kmem_cache_cpus(struct kmem_cache *s, gfp_t flags)
1950{
1951 int cpu;
1952
1953 for_each_online_cpu(cpu) {
1954 struct kmem_cache_cpu *c = get_cpu_slab(s, cpu);
1955
1956 if (c)
1957 continue;
1958
1959 c = alloc_kmem_cache_cpu(s, cpu, flags);
1960 if (!c) {
1961 free_kmem_cache_cpus(s);
1962 return 0;
1963 }
1964 s->cpu_slab[cpu] = c;
1965 }
1966 return 1;
1967}
1968
1969/*
1970 * Initialize the per cpu array.
1971 */
1972static void init_alloc_cpu_cpu(int cpu)
1973{
1974 int i;
1975
1976 if (cpu_isset(cpu, kmem_cach_cpu_free_init_once))
1977 return;
1978
1979 for (i = NR_KMEM_CACHE_CPU - 1; i >= 0; i--)
1980 free_kmem_cache_cpu(&per_cpu(kmem_cache_cpu, cpu)[i], cpu);
1981
1982 cpu_set(cpu, kmem_cach_cpu_free_init_once);
1983}
1984
1985static void __init init_alloc_cpu(void)
1986{
1987 int cpu;
1988
1989 for_each_online_cpu(cpu)
1990 init_alloc_cpu_cpu(cpu);
1991 }
1992
1993#else
1994static inline void free_kmem_cache_cpus(struct kmem_cache *s) {}
1995static inline void init_alloc_cpu(void) {}
1996
1997static inline int alloc_kmem_cache_cpus(struct kmem_cache *s, gfp_t flags)
1998{
1999 init_kmem_cache_cpu(s, &s->cpu_slab);
2000 return 1;
2001}
2002#endif
2003
Christoph Lameter81819f02007-05-06 14:49:36 -07002004#ifdef CONFIG_NUMA
2005/*
2006 * No kmalloc_node yet so do it by hand. We know that this is the first
2007 * slab on the node for this slabcache. There are no concurrent accesses
2008 * possible.
2009 *
2010 * Note that this function only works on the kmalloc_node_cache
Christoph Lameter4c93c3552007-10-16 01:26:08 -07002011 * when allocating for the kmalloc_node_cache. This is used for bootstrapping
2012 * memory on a fresh node that has no slab structures yet.
Christoph Lameter81819f02007-05-06 14:49:36 -07002013 */
Adrian Bunk1cd7daa2007-10-16 01:24:18 -07002014static struct kmem_cache_node *early_kmem_cache_node_alloc(gfp_t gfpflags,
2015 int node)
Christoph Lameter81819f02007-05-06 14:49:36 -07002016{
2017 struct page *page;
2018 struct kmem_cache_node *n;
2019
2020 BUG_ON(kmalloc_caches->size < sizeof(struct kmem_cache_node));
2021
Christoph Lametera2f92ee2007-08-22 14:01:57 -07002022 page = new_slab(kmalloc_caches, gfpflags, node);
Christoph Lameter81819f02007-05-06 14:49:36 -07002023
2024 BUG_ON(!page);
Christoph Lametera2f92ee2007-08-22 14:01:57 -07002025 if (page_to_nid(page) != node) {
2026 printk(KERN_ERR "SLUB: Unable to allocate memory from "
2027 "node %d\n", node);
2028 printk(KERN_ERR "SLUB: Allocating a useless per node structure "
2029 "in order to be able to continue\n");
2030 }
2031
Christoph Lameter81819f02007-05-06 14:49:36 -07002032 n = page->freelist;
2033 BUG_ON(!n);
2034 page->freelist = get_freepointer(kmalloc_caches, n);
2035 page->inuse++;
2036 kmalloc_caches->node[node] = n;
Christoph Lameter8ab13722007-07-17 04:03:32 -07002037#ifdef CONFIG_SLUB_DEBUG
Christoph Lameterd45f39c2007-07-17 04:03:21 -07002038 init_object(kmalloc_caches, n, 1);
2039 init_tracking(kmalloc_caches, n);
Christoph Lameter8ab13722007-07-17 04:03:32 -07002040#endif
Christoph Lameter81819f02007-05-06 14:49:36 -07002041 init_kmem_cache_node(n);
2042 atomic_long_inc(&n->nr_slabs);
Christoph Lametere95eed52007-05-06 14:49:44 -07002043 add_partial(n, page);
Christoph Lameter81819f02007-05-06 14:49:36 -07002044 return n;
2045}
2046
2047static void free_kmem_cache_nodes(struct kmem_cache *s)
2048{
2049 int node;
2050
Christoph Lameterf64dc582007-10-16 01:25:33 -07002051 for_each_node_state(node, N_NORMAL_MEMORY) {
Christoph Lameter81819f02007-05-06 14:49:36 -07002052 struct kmem_cache_node *n = s->node[node];
2053 if (n && n != &s->local_node)
2054 kmem_cache_free(kmalloc_caches, n);
2055 s->node[node] = NULL;
2056 }
2057}
2058
2059static int init_kmem_cache_nodes(struct kmem_cache *s, gfp_t gfpflags)
2060{
2061 int node;
2062 int local_node;
2063
2064 if (slab_state >= UP)
2065 local_node = page_to_nid(virt_to_page(s));
2066 else
2067 local_node = 0;
2068
Christoph Lameterf64dc582007-10-16 01:25:33 -07002069 for_each_node_state(node, N_NORMAL_MEMORY) {
Christoph Lameter81819f02007-05-06 14:49:36 -07002070 struct kmem_cache_node *n;
2071
2072 if (local_node == node)
2073 n = &s->local_node;
2074 else {
2075 if (slab_state == DOWN) {
2076 n = early_kmem_cache_node_alloc(gfpflags,
2077 node);
2078 continue;
2079 }
2080 n = kmem_cache_alloc_node(kmalloc_caches,
2081 gfpflags, node);
2082
2083 if (!n) {
2084 free_kmem_cache_nodes(s);
2085 return 0;
2086 }
2087
2088 }
2089 s->node[node] = n;
2090 init_kmem_cache_node(n);
2091 }
2092 return 1;
2093}
2094#else
2095static void free_kmem_cache_nodes(struct kmem_cache *s)
2096{
2097}
2098
2099static int init_kmem_cache_nodes(struct kmem_cache *s, gfp_t gfpflags)
2100{
2101 init_kmem_cache_node(&s->local_node);
2102 return 1;
2103}
2104#endif
2105
2106/*
2107 * calculate_sizes() determines the order and the distribution of data within
2108 * a slab object.
2109 */
2110static int calculate_sizes(struct kmem_cache *s)
2111{
2112 unsigned long flags = s->flags;
2113 unsigned long size = s->objsize;
2114 unsigned long align = s->align;
2115
2116 /*
2117 * Determine if we can poison the object itself. If the user of
2118 * the slab may touch the object after free or before allocation
2119 * then we should never poison the object itself.
2120 */
2121 if ((flags & SLAB_POISON) && !(flags & SLAB_DESTROY_BY_RCU) &&
Christoph Lameterc59def92007-05-16 22:10:50 -07002122 !s->ctor)
Christoph Lameter81819f02007-05-06 14:49:36 -07002123 s->flags |= __OBJECT_POISON;
2124 else
2125 s->flags &= ~__OBJECT_POISON;
2126
2127 /*
2128 * Round up object size to the next word boundary. We can only
2129 * place the free pointer at word boundaries and this determines
2130 * the possible location of the free pointer.
2131 */
2132 size = ALIGN(size, sizeof(void *));
2133
Christoph Lameter41ecc552007-05-09 02:32:44 -07002134#ifdef CONFIG_SLUB_DEBUG
Christoph Lameter81819f02007-05-06 14:49:36 -07002135 /*
Christoph Lameter672bba32007-05-09 02:32:39 -07002136 * If we are Redzoning then check if there is some space between the
Christoph Lameter81819f02007-05-06 14:49:36 -07002137 * end of the object and the free pointer. If not then add an
Christoph Lameter672bba32007-05-09 02:32:39 -07002138 * additional word to have some bytes to store Redzone information.
Christoph Lameter81819f02007-05-06 14:49:36 -07002139 */
2140 if ((flags & SLAB_RED_ZONE) && size == s->objsize)
2141 size += sizeof(void *);
Christoph Lameter41ecc552007-05-09 02:32:44 -07002142#endif
Christoph Lameter81819f02007-05-06 14:49:36 -07002143
2144 /*
Christoph Lameter672bba32007-05-09 02:32:39 -07002145 * With that we have determined the number of bytes in actual use
2146 * by the object. This is the potential offset to the free pointer.
Christoph Lameter81819f02007-05-06 14:49:36 -07002147 */
2148 s->inuse = size;
2149
2150 if (((flags & (SLAB_DESTROY_BY_RCU | SLAB_POISON)) ||
Christoph Lameterc59def92007-05-16 22:10:50 -07002151 s->ctor)) {
Christoph Lameter81819f02007-05-06 14:49:36 -07002152 /*
2153 * Relocate free pointer after the object if it is not
2154 * permitted to overwrite the first word of the object on
2155 * kmem_cache_free.
2156 *
2157 * This is the case if we do RCU, have a constructor or
2158 * destructor or are poisoning the objects.
2159 */
2160 s->offset = size;
2161 size += sizeof(void *);
2162 }
2163
Christoph Lameterc12b3c62007-05-23 13:57:31 -07002164#ifdef CONFIG_SLUB_DEBUG
Christoph Lameter81819f02007-05-06 14:49:36 -07002165 if (flags & SLAB_STORE_USER)
2166 /*
2167 * Need to store information about allocs and frees after
2168 * the object.
2169 */
2170 size += 2 * sizeof(struct track);
2171
Christoph Lameterbe7b3fb2007-05-09 02:32:36 -07002172 if (flags & SLAB_RED_ZONE)
Christoph Lameter81819f02007-05-06 14:49:36 -07002173 /*
2174 * Add some empty padding so that we can catch
2175 * overwrites from earlier objects rather than let
2176 * tracking information or the free pointer be
2177 * corrupted if an user writes before the start
2178 * of the object.
2179 */
2180 size += sizeof(void *);
Christoph Lameter41ecc552007-05-09 02:32:44 -07002181#endif
Christoph Lameter672bba32007-05-09 02:32:39 -07002182
Christoph Lameter81819f02007-05-06 14:49:36 -07002183 /*
2184 * Determine the alignment based on various parameters that the
Christoph Lameter65c02d42007-05-09 02:32:35 -07002185 * user specified and the dynamic determination of cache line size
2186 * on bootup.
Christoph Lameter81819f02007-05-06 14:49:36 -07002187 */
2188 align = calculate_alignment(flags, align, s->objsize);
2189
2190 /*
2191 * SLUB stores one object immediately after another beginning from
2192 * offset 0. In order to align the objects we have to simply size
2193 * each object to conform to the alignment.
2194 */
2195 size = ALIGN(size, align);
2196 s->size = size;
2197
2198 s->order = calculate_order(size);
2199 if (s->order < 0)
2200 return 0;
2201
2202 /*
2203 * Determine the number of objects per slab
2204 */
2205 s->objects = (PAGE_SIZE << s->order) / size;
2206
Christoph Lameterb3fba8d2007-10-16 01:26:06 -07002207 return !!s->objects;
Christoph Lameter81819f02007-05-06 14:49:36 -07002208
2209}
2210
Christoph Lameter81819f02007-05-06 14:49:36 -07002211static int kmem_cache_open(struct kmem_cache *s, gfp_t gfpflags,
2212 const char *name, size_t size,
2213 size_t align, unsigned long flags,
Christoph Lameter4ba9b9d2007-10-16 23:25:51 -07002214 void (*ctor)(struct kmem_cache *, void *))
Christoph Lameter81819f02007-05-06 14:49:36 -07002215{
2216 memset(s, 0, kmem_size);
2217 s->name = name;
2218 s->ctor = ctor;
Christoph Lameter81819f02007-05-06 14:49:36 -07002219 s->objsize = size;
Christoph Lameter81819f02007-05-06 14:49:36 -07002220 s->align = align;
Christoph Lameterba0268a2007-09-11 15:24:11 -07002221 s->flags = kmem_cache_flags(size, flags, name, ctor);
Christoph Lameter81819f02007-05-06 14:49:36 -07002222
2223 if (!calculate_sizes(s))
2224 goto error;
2225
2226 s->refcount = 1;
2227#ifdef CONFIG_NUMA
2228 s->defrag_ratio = 100;
2229#endif
Christoph Lameterdfb4f092007-10-16 01:26:05 -07002230 if (!init_kmem_cache_nodes(s, gfpflags & ~SLUB_DMA))
2231 goto error;
Christoph Lameter81819f02007-05-06 14:49:36 -07002232
Christoph Lameterdfb4f092007-10-16 01:26:05 -07002233 if (alloc_kmem_cache_cpus(s, gfpflags & ~SLUB_DMA))
Christoph Lameter81819f02007-05-06 14:49:36 -07002234 return 1;
Christoph Lameter4c93c3552007-10-16 01:26:08 -07002235 free_kmem_cache_nodes(s);
Christoph Lameter81819f02007-05-06 14:49:36 -07002236error:
2237 if (flags & SLAB_PANIC)
2238 panic("Cannot create slab %s size=%lu realsize=%u "
2239 "order=%u offset=%u flags=%lx\n",
2240 s->name, (unsigned long)size, s->size, s->order,
2241 s->offset, flags);
2242 return 0;
2243}
Christoph Lameter81819f02007-05-06 14:49:36 -07002244
2245/*
2246 * Check if a given pointer is valid
2247 */
2248int kmem_ptr_validate(struct kmem_cache *s, const void *object)
2249{
2250 struct page * page;
Christoph Lameter81819f02007-05-06 14:49:36 -07002251
2252 page = get_object_page(object);
2253
2254 if (!page || s != page->slab)
2255 /* No slab or wrong slab */
2256 return 0;
2257
Christoph Lameterabcd08a2007-05-09 02:32:37 -07002258 if (!check_valid_pointer(s, page, object))
Christoph Lameter81819f02007-05-06 14:49:36 -07002259 return 0;
2260
2261 /*
2262 * We could also check if the object is on the slabs freelist.
2263 * But this would be too expensive and it seems that the main
2264 * purpose of kmem_ptr_valid is to check if the object belongs
2265 * to a certain slab.
2266 */
2267 return 1;
2268}
2269EXPORT_SYMBOL(kmem_ptr_validate);
2270
2271/*
2272 * Determine the size of a slab object
2273 */
2274unsigned int kmem_cache_size(struct kmem_cache *s)
2275{
2276 return s->objsize;
2277}
2278EXPORT_SYMBOL(kmem_cache_size);
2279
2280const char *kmem_cache_name(struct kmem_cache *s)
2281{
2282 return s->name;
2283}
2284EXPORT_SYMBOL(kmem_cache_name);
2285
2286/*
Christoph Lameter672bba32007-05-09 02:32:39 -07002287 * Attempt to free all slabs on a node. Return the number of slabs we
2288 * were unable to free.
Christoph Lameter81819f02007-05-06 14:49:36 -07002289 */
2290static int free_list(struct kmem_cache *s, struct kmem_cache_node *n,
2291 struct list_head *list)
2292{
2293 int slabs_inuse = 0;
2294 unsigned long flags;
2295 struct page *page, *h;
2296
2297 spin_lock_irqsave(&n->list_lock, flags);
2298 list_for_each_entry_safe(page, h, list, lru)
2299 if (!page->inuse) {
2300 list_del(&page->lru);
2301 discard_slab(s, page);
2302 } else
2303 slabs_inuse++;
2304 spin_unlock_irqrestore(&n->list_lock, flags);
2305 return slabs_inuse;
2306}
2307
2308/*
Christoph Lameter672bba32007-05-09 02:32:39 -07002309 * Release all resources used by a slab cache.
Christoph Lameter81819f02007-05-06 14:49:36 -07002310 */
Christoph Lameter0c710012007-07-17 04:03:24 -07002311static inline int kmem_cache_close(struct kmem_cache *s)
Christoph Lameter81819f02007-05-06 14:49:36 -07002312{
2313 int node;
2314
2315 flush_all(s);
2316
2317 /* Attempt to free all objects */
Christoph Lameter4c93c3552007-10-16 01:26:08 -07002318 free_kmem_cache_cpus(s);
Christoph Lameterf64dc582007-10-16 01:25:33 -07002319 for_each_node_state(node, N_NORMAL_MEMORY) {
Christoph Lameter81819f02007-05-06 14:49:36 -07002320 struct kmem_cache_node *n = get_node(s, node);
2321
Christoph Lameter2086d262007-05-06 14:49:46 -07002322 n->nr_partial -= free_list(s, n, &n->partial);
Christoph Lameter81819f02007-05-06 14:49:36 -07002323 if (atomic_long_read(&n->nr_slabs))
2324 return 1;
2325 }
2326 free_kmem_cache_nodes(s);
2327 return 0;
2328}
2329
2330/*
2331 * Close a cache and release the kmem_cache structure
2332 * (must be used for caches created using kmem_cache_create)
2333 */
2334void kmem_cache_destroy(struct kmem_cache *s)
2335{
2336 down_write(&slub_lock);
2337 s->refcount--;
2338 if (!s->refcount) {
2339 list_del(&s->list);
Christoph Lametera0e1d1b2007-07-17 04:03:31 -07002340 up_write(&slub_lock);
Christoph Lameter81819f02007-05-06 14:49:36 -07002341 if (kmem_cache_close(s))
2342 WARN_ON(1);
2343 sysfs_slab_remove(s);
2344 kfree(s);
Christoph Lametera0e1d1b2007-07-17 04:03:31 -07002345 } else
2346 up_write(&slub_lock);
Christoph Lameter81819f02007-05-06 14:49:36 -07002347}
2348EXPORT_SYMBOL(kmem_cache_destroy);
2349
2350/********************************************************************
2351 * Kmalloc subsystem
2352 *******************************************************************/
2353
Christoph Lameteraadb4bc2007-10-16 01:24:38 -07002354struct kmem_cache kmalloc_caches[PAGE_SHIFT] __cacheline_aligned;
Christoph Lameter81819f02007-05-06 14:49:36 -07002355EXPORT_SYMBOL(kmalloc_caches);
2356
2357#ifdef CONFIG_ZONE_DMA
Christoph Lameteraadb4bc2007-10-16 01:24:38 -07002358static struct kmem_cache *kmalloc_caches_dma[PAGE_SHIFT];
Christoph Lameter81819f02007-05-06 14:49:36 -07002359#endif
2360
2361static int __init setup_slub_min_order(char *str)
2362{
2363 get_option (&str, &slub_min_order);
2364
2365 return 1;
2366}
2367
2368__setup("slub_min_order=", setup_slub_min_order);
2369
2370static int __init setup_slub_max_order(char *str)
2371{
2372 get_option (&str, &slub_max_order);
2373
2374 return 1;
2375}
2376
2377__setup("slub_max_order=", setup_slub_max_order);
2378
2379static int __init setup_slub_min_objects(char *str)
2380{
2381 get_option (&str, &slub_min_objects);
2382
2383 return 1;
2384}
2385
2386__setup("slub_min_objects=", setup_slub_min_objects);
2387
2388static int __init setup_slub_nomerge(char *str)
2389{
2390 slub_nomerge = 1;
2391 return 1;
2392}
2393
2394__setup("slub_nomerge", setup_slub_nomerge);
2395
Christoph Lameter81819f02007-05-06 14:49:36 -07002396static struct kmem_cache *create_kmalloc_cache(struct kmem_cache *s,
2397 const char *name, int size, gfp_t gfp_flags)
2398{
2399 unsigned int flags = 0;
2400
2401 if (gfp_flags & SLUB_DMA)
2402 flags = SLAB_CACHE_DMA;
2403
2404 down_write(&slub_lock);
2405 if (!kmem_cache_open(s, gfp_flags, name, size, ARCH_KMALLOC_MINALIGN,
Christoph Lameterc59def92007-05-16 22:10:50 -07002406 flags, NULL))
Christoph Lameter81819f02007-05-06 14:49:36 -07002407 goto panic;
2408
2409 list_add(&s->list, &slab_caches);
2410 up_write(&slub_lock);
2411 if (sysfs_slab_add(s))
2412 goto panic;
2413 return s;
2414
2415panic:
2416 panic("Creation of kmalloc slab %s size=%d failed.\n", name, size);
2417}
2418
Christoph Lameter2e443fd2007-07-17 04:03:24 -07002419#ifdef CONFIG_ZONE_DMA
Christoph Lameter1ceef402007-08-07 15:11:48 -07002420
2421static void sysfs_add_func(struct work_struct *w)
2422{
2423 struct kmem_cache *s;
2424
2425 down_write(&slub_lock);
2426 list_for_each_entry(s, &slab_caches, list) {
2427 if (s->flags & __SYSFS_ADD_DEFERRED) {
2428 s->flags &= ~__SYSFS_ADD_DEFERRED;
2429 sysfs_slab_add(s);
2430 }
2431 }
2432 up_write(&slub_lock);
2433}
2434
2435static DECLARE_WORK(sysfs_add_work, sysfs_add_func);
2436
Christoph Lameter2e443fd2007-07-17 04:03:24 -07002437static noinline struct kmem_cache *dma_kmalloc_cache(int index, gfp_t flags)
2438{
2439 struct kmem_cache *s;
Christoph Lameter2e443fd2007-07-17 04:03:24 -07002440 char *text;
2441 size_t realsize;
2442
2443 s = kmalloc_caches_dma[index];
2444 if (s)
2445 return s;
2446
2447 /* Dynamically create dma cache */
Christoph Lameter1ceef402007-08-07 15:11:48 -07002448 if (flags & __GFP_WAIT)
2449 down_write(&slub_lock);
2450 else {
2451 if (!down_write_trylock(&slub_lock))
2452 goto out;
2453 }
2454
2455 if (kmalloc_caches_dma[index])
2456 goto unlock_out;
Christoph Lameter2e443fd2007-07-17 04:03:24 -07002457
Christoph Lameter7b55f622007-07-17 04:03:27 -07002458 realsize = kmalloc_caches[index].objsize;
Christoph Lameter1ceef402007-08-07 15:11:48 -07002459 text = kasprintf(flags & ~SLUB_DMA, "kmalloc_dma-%d", (unsigned int)realsize),
2460 s = kmalloc(kmem_size, flags & ~SLUB_DMA);
2461
2462 if (!s || !text || !kmem_cache_open(s, flags, text,
2463 realsize, ARCH_KMALLOC_MINALIGN,
2464 SLAB_CACHE_DMA|__SYSFS_ADD_DEFERRED, NULL)) {
2465 kfree(s);
2466 kfree(text);
2467 goto unlock_out;
Christoph Lameterdfce8642007-07-17 04:03:25 -07002468 }
Christoph Lameter1ceef402007-08-07 15:11:48 -07002469
2470 list_add(&s->list, &slab_caches);
2471 kmalloc_caches_dma[index] = s;
2472
2473 schedule_work(&sysfs_add_work);
2474
2475unlock_out:
Christoph Lameterdfce8642007-07-17 04:03:25 -07002476 up_write(&slub_lock);
Christoph Lameter1ceef402007-08-07 15:11:48 -07002477out:
Christoph Lameterdfce8642007-07-17 04:03:25 -07002478 return kmalloc_caches_dma[index];
Christoph Lameter2e443fd2007-07-17 04:03:24 -07002479}
2480#endif
2481
Christoph Lameterf1b26332007-07-17 04:03:26 -07002482/*
2483 * Conversion table for small slabs sizes / 8 to the index in the
2484 * kmalloc array. This is necessary for slabs < 192 since we have non power
2485 * of two cache sizes there. The size of larger slabs can be determined using
2486 * fls.
2487 */
2488static s8 size_index[24] = {
2489 3, /* 8 */
2490 4, /* 16 */
2491 5, /* 24 */
2492 5, /* 32 */
2493 6, /* 40 */
2494 6, /* 48 */
2495 6, /* 56 */
2496 6, /* 64 */
2497 1, /* 72 */
2498 1, /* 80 */
2499 1, /* 88 */
2500 1, /* 96 */
2501 7, /* 104 */
2502 7, /* 112 */
2503 7, /* 120 */
2504 7, /* 128 */
2505 2, /* 136 */
2506 2, /* 144 */
2507 2, /* 152 */
2508 2, /* 160 */
2509 2, /* 168 */
2510 2, /* 176 */
2511 2, /* 184 */
2512 2 /* 192 */
2513};
2514
Christoph Lameter81819f02007-05-06 14:49:36 -07002515static struct kmem_cache *get_slab(size_t size, gfp_t flags)
2516{
Christoph Lameterf1b26332007-07-17 04:03:26 -07002517 int index;
Christoph Lameter81819f02007-05-06 14:49:36 -07002518
Christoph Lameterf1b26332007-07-17 04:03:26 -07002519 if (size <= 192) {
2520 if (!size)
2521 return ZERO_SIZE_PTR;
Christoph Lameter81819f02007-05-06 14:49:36 -07002522
Christoph Lameterf1b26332007-07-17 04:03:26 -07002523 index = size_index[(size - 1) / 8];
Christoph Lameteraadb4bc2007-10-16 01:24:38 -07002524 } else
Christoph Lameterf1b26332007-07-17 04:03:26 -07002525 index = fls(size - 1);
Christoph Lameter81819f02007-05-06 14:49:36 -07002526
2527#ifdef CONFIG_ZONE_DMA
Christoph Lameterf1b26332007-07-17 04:03:26 -07002528 if (unlikely((flags & SLUB_DMA)))
Christoph Lameter2e443fd2007-07-17 04:03:24 -07002529 return dma_kmalloc_cache(index, flags);
Christoph Lameterf1b26332007-07-17 04:03:26 -07002530
Christoph Lameter81819f02007-05-06 14:49:36 -07002531#endif
2532 return &kmalloc_caches[index];
2533}
2534
2535void *__kmalloc(size_t size, gfp_t flags)
2536{
Christoph Lameteraadb4bc2007-10-16 01:24:38 -07002537 struct kmem_cache *s;
Christoph Lameter81819f02007-05-06 14:49:36 -07002538
Christoph Lameteraadb4bc2007-10-16 01:24:38 -07002539 if (unlikely(size > PAGE_SIZE / 2))
2540 return (void *)__get_free_pages(flags | __GFP_COMP,
2541 get_order(size));
2542
2543 s = get_slab(size, flags);
2544
2545 if (unlikely(ZERO_OR_NULL_PTR(s)))
Christoph Lameter6cb8f912007-07-17 04:03:22 -07002546 return s;
2547
Christoph Lameterce15fea2007-07-17 04:03:28 -07002548 return slab_alloc(s, flags, -1, __builtin_return_address(0));
Christoph Lameter81819f02007-05-06 14:49:36 -07002549}
2550EXPORT_SYMBOL(__kmalloc);
2551
2552#ifdef CONFIG_NUMA
2553void *__kmalloc_node(size_t size, gfp_t flags, int node)
2554{
Christoph Lameteraadb4bc2007-10-16 01:24:38 -07002555 struct kmem_cache *s;
Christoph Lameter81819f02007-05-06 14:49:36 -07002556
Christoph Lameteraadb4bc2007-10-16 01:24:38 -07002557 if (unlikely(size > PAGE_SIZE / 2))
2558 return (void *)__get_free_pages(flags | __GFP_COMP,
2559 get_order(size));
2560
2561 s = get_slab(size, flags);
2562
2563 if (unlikely(ZERO_OR_NULL_PTR(s)))
Christoph Lameter6cb8f912007-07-17 04:03:22 -07002564 return s;
2565
Christoph Lameterce15fea2007-07-17 04:03:28 -07002566 return slab_alloc(s, flags, node, __builtin_return_address(0));
Christoph Lameter81819f02007-05-06 14:49:36 -07002567}
2568EXPORT_SYMBOL(__kmalloc_node);
2569#endif
2570
2571size_t ksize(const void *object)
2572{
Christoph Lameter272c1d22007-06-08 13:46:49 -07002573 struct page *page;
Christoph Lameter81819f02007-05-06 14:49:36 -07002574 struct kmem_cache *s;
2575
Christoph Lameteref8b4522007-10-16 01:24:46 -07002576 BUG_ON(!object);
2577 if (unlikely(object == ZERO_SIZE_PTR))
Christoph Lameter272c1d22007-06-08 13:46:49 -07002578 return 0;
2579
2580 page = get_object_page(object);
Christoph Lameter81819f02007-05-06 14:49:36 -07002581 BUG_ON(!page);
2582 s = page->slab;
2583 BUG_ON(!s);
2584
2585 /*
2586 * Debugging requires use of the padding between object
2587 * and whatever may come after it.
2588 */
2589 if (s->flags & (SLAB_RED_ZONE | SLAB_POISON))
2590 return s->objsize;
2591
2592 /*
2593 * If we have the need to store the freelist pointer
2594 * back there or track user information then we can
2595 * only use the space before that information.
2596 */
2597 if (s->flags & (SLAB_DESTROY_BY_RCU | SLAB_STORE_USER))
2598 return s->inuse;
2599
2600 /*
2601 * Else we can use all the padding etc for the allocation
2602 */
2603 return s->size;
2604}
2605EXPORT_SYMBOL(ksize);
2606
2607void kfree(const void *x)
2608{
Christoph Lameter81819f02007-05-06 14:49:36 -07002609 struct page *page;
2610
Satyam Sharma2408c552007-10-16 01:24:44 -07002611 if (unlikely(ZERO_OR_NULL_PTR(x)))
Christoph Lameter81819f02007-05-06 14:49:36 -07002612 return;
2613
Christoph Lameterb49af682007-05-06 14:49:41 -07002614 page = virt_to_head_page(x);
Christoph Lameteraadb4bc2007-10-16 01:24:38 -07002615 if (unlikely(!PageSlab(page))) {
2616 put_page(page);
2617 return;
2618 }
2619 slab_free(page->slab, page, (void *)x, __builtin_return_address(0));
Christoph Lameter81819f02007-05-06 14:49:36 -07002620}
2621EXPORT_SYMBOL(kfree);
2622
Christoph Lameter2086d262007-05-06 14:49:46 -07002623/*
Christoph Lameter672bba32007-05-09 02:32:39 -07002624 * kmem_cache_shrink removes empty slabs from the partial lists and sorts
2625 * the remaining slabs by the number of items in use. The slabs with the
2626 * most items in use come first. New allocations will then fill those up
2627 * and thus they can be removed from the partial lists.
2628 *
2629 * The slabs with the least items are placed last. This results in them
2630 * being allocated from last increasing the chance that the last objects
2631 * are freed in them.
Christoph Lameter2086d262007-05-06 14:49:46 -07002632 */
2633int kmem_cache_shrink(struct kmem_cache *s)
2634{
2635 int node;
2636 int i;
2637 struct kmem_cache_node *n;
2638 struct page *page;
2639 struct page *t;
2640 struct list_head *slabs_by_inuse =
2641 kmalloc(sizeof(struct list_head) * s->objects, GFP_KERNEL);
2642 unsigned long flags;
2643
2644 if (!slabs_by_inuse)
2645 return -ENOMEM;
2646
2647 flush_all(s);
Christoph Lameterf64dc582007-10-16 01:25:33 -07002648 for_each_node_state(node, N_NORMAL_MEMORY) {
Christoph Lameter2086d262007-05-06 14:49:46 -07002649 n = get_node(s, node);
2650
2651 if (!n->nr_partial)
2652 continue;
2653
2654 for (i = 0; i < s->objects; i++)
2655 INIT_LIST_HEAD(slabs_by_inuse + i);
2656
2657 spin_lock_irqsave(&n->list_lock, flags);
2658
2659 /*
Christoph Lameter672bba32007-05-09 02:32:39 -07002660 * Build lists indexed by the items in use in each slab.
Christoph Lameter2086d262007-05-06 14:49:46 -07002661 *
Christoph Lameter672bba32007-05-09 02:32:39 -07002662 * Note that concurrent frees may occur while we hold the
2663 * list_lock. page->inuse here is the upper limit.
Christoph Lameter2086d262007-05-06 14:49:46 -07002664 */
2665 list_for_each_entry_safe(page, t, &n->partial, lru) {
2666 if (!page->inuse && slab_trylock(page)) {
2667 /*
2668 * Must hold slab lock here because slab_free
2669 * may have freed the last object and be
2670 * waiting to release the slab.
2671 */
2672 list_del(&page->lru);
2673 n->nr_partial--;
2674 slab_unlock(page);
2675 discard_slab(s, page);
2676 } else {
Christoph Lameterfcda3d82007-07-30 13:06:46 -07002677 list_move(&page->lru,
2678 slabs_by_inuse + page->inuse);
Christoph Lameter2086d262007-05-06 14:49:46 -07002679 }
2680 }
2681
Christoph Lameter2086d262007-05-06 14:49:46 -07002682 /*
Christoph Lameter672bba32007-05-09 02:32:39 -07002683 * Rebuild the partial list with the slabs filled up most
2684 * first and the least used slabs at the end.
Christoph Lameter2086d262007-05-06 14:49:46 -07002685 */
2686 for (i = s->objects - 1; i >= 0; i--)
2687 list_splice(slabs_by_inuse + i, n->partial.prev);
2688
Christoph Lameter2086d262007-05-06 14:49:46 -07002689 spin_unlock_irqrestore(&n->list_lock, flags);
2690 }
2691
2692 kfree(slabs_by_inuse);
2693 return 0;
2694}
2695EXPORT_SYMBOL(kmem_cache_shrink);
2696
Christoph Lameter81819f02007-05-06 14:49:36 -07002697/********************************************************************
2698 * Basic setup of slabs
2699 *******************************************************************/
2700
2701void __init kmem_cache_init(void)
2702{
2703 int i;
Christoph Lameter4b356be2007-06-16 10:16:13 -07002704 int caches = 0;
Christoph Lameter81819f02007-05-06 14:49:36 -07002705
Christoph Lameter4c93c3552007-10-16 01:26:08 -07002706 init_alloc_cpu();
2707
Christoph Lameter81819f02007-05-06 14:49:36 -07002708#ifdef CONFIG_NUMA
2709 /*
2710 * Must first have the slab cache available for the allocations of the
Christoph Lameter672bba32007-05-09 02:32:39 -07002711 * struct kmem_cache_node's. There is special bootstrap code in
Christoph Lameter81819f02007-05-06 14:49:36 -07002712 * kmem_cache_open for slab_state == DOWN.
2713 */
2714 create_kmalloc_cache(&kmalloc_caches[0], "kmem_cache_node",
2715 sizeof(struct kmem_cache_node), GFP_KERNEL);
Christoph Lameter8ffa6872007-05-31 00:40:51 -07002716 kmalloc_caches[0].refcount = -1;
Christoph Lameter4b356be2007-06-16 10:16:13 -07002717 caches++;
Christoph Lameter81819f02007-05-06 14:49:36 -07002718#endif
2719
2720 /* Able to allocate the per node structures */
2721 slab_state = PARTIAL;
2722
2723 /* Caches that are not of the two-to-the-power-of size */
Christoph Lameter4b356be2007-06-16 10:16:13 -07002724 if (KMALLOC_MIN_SIZE <= 64) {
2725 create_kmalloc_cache(&kmalloc_caches[1],
Christoph Lameter81819f02007-05-06 14:49:36 -07002726 "kmalloc-96", 96, GFP_KERNEL);
Christoph Lameter4b356be2007-06-16 10:16:13 -07002727 caches++;
2728 }
2729 if (KMALLOC_MIN_SIZE <= 128) {
2730 create_kmalloc_cache(&kmalloc_caches[2],
Christoph Lameter81819f02007-05-06 14:49:36 -07002731 "kmalloc-192", 192, GFP_KERNEL);
Christoph Lameter4b356be2007-06-16 10:16:13 -07002732 caches++;
2733 }
Christoph Lameter81819f02007-05-06 14:49:36 -07002734
Christoph Lameteraadb4bc2007-10-16 01:24:38 -07002735 for (i = KMALLOC_SHIFT_LOW; i < PAGE_SHIFT; i++) {
Christoph Lameter81819f02007-05-06 14:49:36 -07002736 create_kmalloc_cache(&kmalloc_caches[i],
2737 "kmalloc", 1 << i, GFP_KERNEL);
Christoph Lameter4b356be2007-06-16 10:16:13 -07002738 caches++;
2739 }
Christoph Lameter81819f02007-05-06 14:49:36 -07002740
Christoph Lameterf1b26332007-07-17 04:03:26 -07002741
2742 /*
2743 * Patch up the size_index table if we have strange large alignment
2744 * requirements for the kmalloc array. This is only the case for
2745 * mips it seems. The standard arches will not generate any code here.
2746 *
2747 * Largest permitted alignment is 256 bytes due to the way we
2748 * handle the index determination for the smaller caches.
2749 *
2750 * Make sure that nothing crazy happens if someone starts tinkering
2751 * around with ARCH_KMALLOC_MINALIGN
2752 */
2753 BUILD_BUG_ON(KMALLOC_MIN_SIZE > 256 ||
2754 (KMALLOC_MIN_SIZE & (KMALLOC_MIN_SIZE - 1)));
2755
Christoph Lameter12ad6842007-07-17 04:03:28 -07002756 for (i = 8; i < KMALLOC_MIN_SIZE; i += 8)
Christoph Lameterf1b26332007-07-17 04:03:26 -07002757 size_index[(i - 1) / 8] = KMALLOC_SHIFT_LOW;
2758
Christoph Lameter81819f02007-05-06 14:49:36 -07002759 slab_state = UP;
2760
2761 /* Provide the correct kmalloc names now that the caches are up */
Christoph Lameteraadb4bc2007-10-16 01:24:38 -07002762 for (i = KMALLOC_SHIFT_LOW; i < PAGE_SHIFT; i++)
Christoph Lameter81819f02007-05-06 14:49:36 -07002763 kmalloc_caches[i]. name =
2764 kasprintf(GFP_KERNEL, "kmalloc-%d", 1 << i);
2765
2766#ifdef CONFIG_SMP
2767 register_cpu_notifier(&slab_notifier);
Christoph Lameter4c93c3552007-10-16 01:26:08 -07002768 kmem_size = offsetof(struct kmem_cache, cpu_slab) +
2769 nr_cpu_ids * sizeof(struct kmem_cache_cpu *);
2770#else
2771 kmem_size = sizeof(struct kmem_cache);
Christoph Lameter81819f02007-05-06 14:49:36 -07002772#endif
2773
Christoph Lameter81819f02007-05-06 14:49:36 -07002774
2775 printk(KERN_INFO "SLUB: Genslabs=%d, HWalign=%d, Order=%d-%d, MinObjects=%d,"
Christoph Lameter4b356be2007-06-16 10:16:13 -07002776 " CPUs=%d, Nodes=%d\n",
2777 caches, cache_line_size(),
Christoph Lameter81819f02007-05-06 14:49:36 -07002778 slub_min_order, slub_max_order, slub_min_objects,
2779 nr_cpu_ids, nr_node_ids);
2780}
2781
2782/*
2783 * Find a mergeable slab cache
2784 */
2785static int slab_unmergeable(struct kmem_cache *s)
2786{
2787 if (slub_nomerge || (s->flags & SLUB_NEVER_MERGE))
2788 return 1;
2789
Christoph Lameterc59def92007-05-16 22:10:50 -07002790 if (s->ctor)
Christoph Lameter81819f02007-05-06 14:49:36 -07002791 return 1;
2792
Christoph Lameter8ffa6872007-05-31 00:40:51 -07002793 /*
2794 * We may have set a slab to be unmergeable during bootstrap.
2795 */
2796 if (s->refcount < 0)
2797 return 1;
2798
Christoph Lameter81819f02007-05-06 14:49:36 -07002799 return 0;
2800}
2801
2802static struct kmem_cache *find_mergeable(size_t size,
Christoph Lameterba0268a2007-09-11 15:24:11 -07002803 size_t align, unsigned long flags, const char *name,
Christoph Lameter4ba9b9d2007-10-16 23:25:51 -07002804 void (*ctor)(struct kmem_cache *, void *))
Christoph Lameter81819f02007-05-06 14:49:36 -07002805{
Christoph Lameter5b95a4ac2007-07-17 04:03:19 -07002806 struct kmem_cache *s;
Christoph Lameter81819f02007-05-06 14:49:36 -07002807
2808 if (slub_nomerge || (flags & SLUB_NEVER_MERGE))
2809 return NULL;
2810
Christoph Lameterc59def92007-05-16 22:10:50 -07002811 if (ctor)
Christoph Lameter81819f02007-05-06 14:49:36 -07002812 return NULL;
2813
2814 size = ALIGN(size, sizeof(void *));
2815 align = calculate_alignment(flags, align, size);
2816 size = ALIGN(size, align);
Christoph Lameterba0268a2007-09-11 15:24:11 -07002817 flags = kmem_cache_flags(size, flags, name, NULL);
Christoph Lameter81819f02007-05-06 14:49:36 -07002818
Christoph Lameter5b95a4ac2007-07-17 04:03:19 -07002819 list_for_each_entry(s, &slab_caches, list) {
Christoph Lameter81819f02007-05-06 14:49:36 -07002820 if (slab_unmergeable(s))
2821 continue;
2822
2823 if (size > s->size)
2824 continue;
2825
Christoph Lameterba0268a2007-09-11 15:24:11 -07002826 if ((flags & SLUB_MERGE_SAME) != (s->flags & SLUB_MERGE_SAME))
Christoph Lameter81819f02007-05-06 14:49:36 -07002827 continue;
2828 /*
2829 * Check if alignment is compatible.
2830 * Courtesy of Adrian Drzewiecki
2831 */
2832 if ((s->size & ~(align -1)) != s->size)
2833 continue;
2834
2835 if (s->size - size >= sizeof(void *))
2836 continue;
2837
2838 return s;
2839 }
2840 return NULL;
2841}
2842
2843struct kmem_cache *kmem_cache_create(const char *name, size_t size,
2844 size_t align, unsigned long flags,
Christoph Lameter4ba9b9d2007-10-16 23:25:51 -07002845 void (*ctor)(struct kmem_cache *, void *))
Christoph Lameter81819f02007-05-06 14:49:36 -07002846{
2847 struct kmem_cache *s;
2848
2849 down_write(&slub_lock);
Christoph Lameterba0268a2007-09-11 15:24:11 -07002850 s = find_mergeable(size, align, flags, name, ctor);
Christoph Lameter81819f02007-05-06 14:49:36 -07002851 if (s) {
Christoph Lameter42a9fdb2007-10-16 01:26:09 -07002852 int cpu;
2853
Christoph Lameter81819f02007-05-06 14:49:36 -07002854 s->refcount++;
2855 /*
2856 * Adjust the object sizes so that we clear
2857 * the complete object on kzalloc.
2858 */
2859 s->objsize = max(s->objsize, (int)size);
Christoph Lameter42a9fdb2007-10-16 01:26:09 -07002860
2861 /*
2862 * And then we need to update the object size in the
2863 * per cpu structures
2864 */
2865 for_each_online_cpu(cpu)
2866 get_cpu_slab(s, cpu)->objsize = s->objsize;
Christoph Lameter81819f02007-05-06 14:49:36 -07002867 s->inuse = max_t(int, s->inuse, ALIGN(size, sizeof(void *)));
Christoph Lametera0e1d1b2007-07-17 04:03:31 -07002868 up_write(&slub_lock);
Christoph Lameter81819f02007-05-06 14:49:36 -07002869 if (sysfs_slab_alias(s, name))
2870 goto err;
Christoph Lametera0e1d1b2007-07-17 04:03:31 -07002871 return s;
2872 }
2873 s = kmalloc(kmem_size, GFP_KERNEL);
2874 if (s) {
2875 if (kmem_cache_open(s, GFP_KERNEL, name,
Christoph Lameterc59def92007-05-16 22:10:50 -07002876 size, align, flags, ctor)) {
Christoph Lameter81819f02007-05-06 14:49:36 -07002877 list_add(&s->list, &slab_caches);
Christoph Lametera0e1d1b2007-07-17 04:03:31 -07002878 up_write(&slub_lock);
2879 if (sysfs_slab_add(s))
2880 goto err;
2881 return s;
2882 }
2883 kfree(s);
Christoph Lameter81819f02007-05-06 14:49:36 -07002884 }
2885 up_write(&slub_lock);
Christoph Lameter81819f02007-05-06 14:49:36 -07002886
2887err:
Christoph Lameter81819f02007-05-06 14:49:36 -07002888 if (flags & SLAB_PANIC)
2889 panic("Cannot create slabcache %s\n", name);
2890 else
2891 s = NULL;
2892 return s;
2893}
2894EXPORT_SYMBOL(kmem_cache_create);
2895
Christoph Lameter81819f02007-05-06 14:49:36 -07002896#ifdef CONFIG_SMP
Christoph Lameter27390bc2007-06-01 00:47:09 -07002897/*
Christoph Lameter672bba32007-05-09 02:32:39 -07002898 * Use the cpu notifier to insure that the cpu slabs are flushed when
2899 * necessary.
Christoph Lameter81819f02007-05-06 14:49:36 -07002900 */
2901static int __cpuinit slab_cpuup_callback(struct notifier_block *nfb,
2902 unsigned long action, void *hcpu)
2903{
2904 long cpu = (long)hcpu;
Christoph Lameter5b95a4ac2007-07-17 04:03:19 -07002905 struct kmem_cache *s;
2906 unsigned long flags;
Christoph Lameter81819f02007-05-06 14:49:36 -07002907
2908 switch (action) {
Christoph Lameter4c93c3552007-10-16 01:26:08 -07002909 case CPU_UP_PREPARE:
2910 case CPU_UP_PREPARE_FROZEN:
2911 init_alloc_cpu_cpu(cpu);
2912 down_read(&slub_lock);
2913 list_for_each_entry(s, &slab_caches, list)
2914 s->cpu_slab[cpu] = alloc_kmem_cache_cpu(s, cpu,
2915 GFP_KERNEL);
2916 up_read(&slub_lock);
2917 break;
2918
Christoph Lameter81819f02007-05-06 14:49:36 -07002919 case CPU_UP_CANCELED:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07002920 case CPU_UP_CANCELED_FROZEN:
Christoph Lameter81819f02007-05-06 14:49:36 -07002921 case CPU_DEAD:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07002922 case CPU_DEAD_FROZEN:
Christoph Lameter5b95a4ac2007-07-17 04:03:19 -07002923 down_read(&slub_lock);
2924 list_for_each_entry(s, &slab_caches, list) {
Christoph Lameter4c93c3552007-10-16 01:26:08 -07002925 struct kmem_cache_cpu *c = get_cpu_slab(s, cpu);
2926
Christoph Lameter5b95a4ac2007-07-17 04:03:19 -07002927 local_irq_save(flags);
2928 __flush_cpu_slab(s, cpu);
2929 local_irq_restore(flags);
Christoph Lameter4c93c3552007-10-16 01:26:08 -07002930 free_kmem_cache_cpu(c, cpu);
2931 s->cpu_slab[cpu] = NULL;
Christoph Lameter5b95a4ac2007-07-17 04:03:19 -07002932 }
2933 up_read(&slub_lock);
Christoph Lameter81819f02007-05-06 14:49:36 -07002934 break;
2935 default:
2936 break;
2937 }
2938 return NOTIFY_OK;
2939}
2940
2941static struct notifier_block __cpuinitdata slab_notifier =
2942 { &slab_cpuup_callback, NULL, 0 };
2943
2944#endif
2945
Christoph Lameter81819f02007-05-06 14:49:36 -07002946void *__kmalloc_track_caller(size_t size, gfp_t gfpflags, void *caller)
2947{
Christoph Lameteraadb4bc2007-10-16 01:24:38 -07002948 struct kmem_cache *s;
2949
2950 if (unlikely(size > PAGE_SIZE / 2))
2951 return (void *)__get_free_pages(gfpflags | __GFP_COMP,
2952 get_order(size));
2953 s = get_slab(size, gfpflags);
Christoph Lameter81819f02007-05-06 14:49:36 -07002954
Satyam Sharma2408c552007-10-16 01:24:44 -07002955 if (unlikely(ZERO_OR_NULL_PTR(s)))
Christoph Lameter6cb8f912007-07-17 04:03:22 -07002956 return s;
Christoph Lameter81819f02007-05-06 14:49:36 -07002957
Christoph Lameterce15fea2007-07-17 04:03:28 -07002958 return slab_alloc(s, gfpflags, -1, caller);
Christoph Lameter81819f02007-05-06 14:49:36 -07002959}
2960
2961void *__kmalloc_node_track_caller(size_t size, gfp_t gfpflags,
2962 int node, void *caller)
2963{
Christoph Lameteraadb4bc2007-10-16 01:24:38 -07002964 struct kmem_cache *s;
2965
2966 if (unlikely(size > PAGE_SIZE / 2))
2967 return (void *)__get_free_pages(gfpflags | __GFP_COMP,
2968 get_order(size));
2969 s = get_slab(size, gfpflags);
Christoph Lameter81819f02007-05-06 14:49:36 -07002970
Satyam Sharma2408c552007-10-16 01:24:44 -07002971 if (unlikely(ZERO_OR_NULL_PTR(s)))
Christoph Lameter6cb8f912007-07-17 04:03:22 -07002972 return s;
Christoph Lameter81819f02007-05-06 14:49:36 -07002973
Christoph Lameterce15fea2007-07-17 04:03:28 -07002974 return slab_alloc(s, gfpflags, node, caller);
Christoph Lameter81819f02007-05-06 14:49:36 -07002975}
2976
Christoph Lameter41ecc552007-05-09 02:32:44 -07002977#if defined(CONFIG_SYSFS) && defined(CONFIG_SLUB_DEBUG)
Christoph Lameter434e2452007-07-17 04:03:30 -07002978static int validate_slab(struct kmem_cache *s, struct page *page,
2979 unsigned long *map)
Christoph Lameter53e15af2007-05-06 14:49:43 -07002980{
2981 void *p;
2982 void *addr = page_address(page);
Christoph Lameter53e15af2007-05-06 14:49:43 -07002983
2984 if (!check_slab(s, page) ||
2985 !on_freelist(s, page, NULL))
2986 return 0;
2987
2988 /* Now we know that a valid freelist exists */
2989 bitmap_zero(map, s->objects);
2990
Christoph Lameter7656c722007-05-09 02:32:40 -07002991 for_each_free_object(p, s, page->freelist) {
2992 set_bit(slab_index(p, s, addr), map);
Christoph Lameter53e15af2007-05-06 14:49:43 -07002993 if (!check_object(s, page, p, 0))
2994 return 0;
2995 }
2996
Christoph Lameter7656c722007-05-09 02:32:40 -07002997 for_each_object(p, s, addr)
2998 if (!test_bit(slab_index(p, s, addr), map))
Christoph Lameter53e15af2007-05-06 14:49:43 -07002999 if (!check_object(s, page, p, 1))
3000 return 0;
3001 return 1;
3002}
3003
Christoph Lameter434e2452007-07-17 04:03:30 -07003004static void validate_slab_slab(struct kmem_cache *s, struct page *page,
3005 unsigned long *map)
Christoph Lameter53e15af2007-05-06 14:49:43 -07003006{
3007 if (slab_trylock(page)) {
Christoph Lameter434e2452007-07-17 04:03:30 -07003008 validate_slab(s, page, map);
Christoph Lameter53e15af2007-05-06 14:49:43 -07003009 slab_unlock(page);
3010 } else
3011 printk(KERN_INFO "SLUB %s: Skipped busy slab 0x%p\n",
3012 s->name, page);
3013
3014 if (s->flags & DEBUG_DEFAULT_FLAGS) {
Christoph Lameter35e5d7e2007-05-09 02:32:42 -07003015 if (!SlabDebug(page))
3016 printk(KERN_ERR "SLUB %s: SlabDebug not set "
Christoph Lameter53e15af2007-05-06 14:49:43 -07003017 "on slab 0x%p\n", s->name, page);
3018 } else {
Christoph Lameter35e5d7e2007-05-09 02:32:42 -07003019 if (SlabDebug(page))
3020 printk(KERN_ERR "SLUB %s: SlabDebug set on "
Christoph Lameter53e15af2007-05-06 14:49:43 -07003021 "slab 0x%p\n", s->name, page);
3022 }
3023}
3024
Christoph Lameter434e2452007-07-17 04:03:30 -07003025static int validate_slab_node(struct kmem_cache *s,
3026 struct kmem_cache_node *n, unsigned long *map)
Christoph Lameter53e15af2007-05-06 14:49:43 -07003027{
3028 unsigned long count = 0;
3029 struct page *page;
3030 unsigned long flags;
3031
3032 spin_lock_irqsave(&n->list_lock, flags);
3033
3034 list_for_each_entry(page, &n->partial, lru) {
Christoph Lameter434e2452007-07-17 04:03:30 -07003035 validate_slab_slab(s, page, map);
Christoph Lameter53e15af2007-05-06 14:49:43 -07003036 count++;
3037 }
3038 if (count != n->nr_partial)
3039 printk(KERN_ERR "SLUB %s: %ld partial slabs counted but "
3040 "counter=%ld\n", s->name, count, n->nr_partial);
3041
3042 if (!(s->flags & SLAB_STORE_USER))
3043 goto out;
3044
3045 list_for_each_entry(page, &n->full, lru) {
Christoph Lameter434e2452007-07-17 04:03:30 -07003046 validate_slab_slab(s, page, map);
Christoph Lameter53e15af2007-05-06 14:49:43 -07003047 count++;
3048 }
3049 if (count != atomic_long_read(&n->nr_slabs))
3050 printk(KERN_ERR "SLUB: %s %ld slabs counted but "
3051 "counter=%ld\n", s->name, count,
3052 atomic_long_read(&n->nr_slabs));
3053
3054out:
3055 spin_unlock_irqrestore(&n->list_lock, flags);
3056 return count;
3057}
3058
Christoph Lameter434e2452007-07-17 04:03:30 -07003059static long validate_slab_cache(struct kmem_cache *s)
Christoph Lameter53e15af2007-05-06 14:49:43 -07003060{
3061 int node;
3062 unsigned long count = 0;
Christoph Lameter434e2452007-07-17 04:03:30 -07003063 unsigned long *map = kmalloc(BITS_TO_LONGS(s->objects) *
3064 sizeof(unsigned long), GFP_KERNEL);
3065
3066 if (!map)
3067 return -ENOMEM;
Christoph Lameter53e15af2007-05-06 14:49:43 -07003068
3069 flush_all(s);
Christoph Lameterf64dc582007-10-16 01:25:33 -07003070 for_each_node_state(node, N_NORMAL_MEMORY) {
Christoph Lameter53e15af2007-05-06 14:49:43 -07003071 struct kmem_cache_node *n = get_node(s, node);
3072
Christoph Lameter434e2452007-07-17 04:03:30 -07003073 count += validate_slab_node(s, n, map);
Christoph Lameter53e15af2007-05-06 14:49:43 -07003074 }
Christoph Lameter434e2452007-07-17 04:03:30 -07003075 kfree(map);
Christoph Lameter53e15af2007-05-06 14:49:43 -07003076 return count;
3077}
3078
Christoph Lameterb3459702007-05-09 02:32:41 -07003079#ifdef SLUB_RESILIENCY_TEST
3080static void resiliency_test(void)
3081{
3082 u8 *p;
3083
3084 printk(KERN_ERR "SLUB resiliency testing\n");
3085 printk(KERN_ERR "-----------------------\n");
3086 printk(KERN_ERR "A. Corruption after allocation\n");
3087
3088 p = kzalloc(16, GFP_KERNEL);
3089 p[16] = 0x12;
3090 printk(KERN_ERR "\n1. kmalloc-16: Clobber Redzone/next pointer"
3091 " 0x12->0x%p\n\n", p + 16);
3092
3093 validate_slab_cache(kmalloc_caches + 4);
3094
3095 /* Hmmm... The next two are dangerous */
3096 p = kzalloc(32, GFP_KERNEL);
3097 p[32 + sizeof(void *)] = 0x34;
3098 printk(KERN_ERR "\n2. kmalloc-32: Clobber next pointer/next slab"
3099 " 0x34 -> -0x%p\n", p);
3100 printk(KERN_ERR "If allocated object is overwritten then not detectable\n\n");
3101
3102 validate_slab_cache(kmalloc_caches + 5);
3103 p = kzalloc(64, GFP_KERNEL);
3104 p += 64 + (get_cycles() & 0xff) * sizeof(void *);
3105 *p = 0x56;
3106 printk(KERN_ERR "\n3. kmalloc-64: corrupting random byte 0x56->0x%p\n",
3107 p);
3108 printk(KERN_ERR "If allocated object is overwritten then not detectable\n\n");
3109 validate_slab_cache(kmalloc_caches + 6);
3110
3111 printk(KERN_ERR "\nB. Corruption after free\n");
3112 p = kzalloc(128, GFP_KERNEL);
3113 kfree(p);
3114 *p = 0x78;
3115 printk(KERN_ERR "1. kmalloc-128: Clobber first word 0x78->0x%p\n\n", p);
3116 validate_slab_cache(kmalloc_caches + 7);
3117
3118 p = kzalloc(256, GFP_KERNEL);
3119 kfree(p);
3120 p[50] = 0x9a;
3121 printk(KERN_ERR "\n2. kmalloc-256: Clobber 50th byte 0x9a->0x%p\n\n", p);
3122 validate_slab_cache(kmalloc_caches + 8);
3123
3124 p = kzalloc(512, GFP_KERNEL);
3125 kfree(p);
3126 p[512] = 0xab;
3127 printk(KERN_ERR "\n3. kmalloc-512: Clobber redzone 0xab->0x%p\n\n", p);
3128 validate_slab_cache(kmalloc_caches + 9);
3129}
3130#else
3131static void resiliency_test(void) {};
3132#endif
3133
Christoph Lameter88a420e2007-05-06 14:49:45 -07003134/*
Christoph Lameter672bba32007-05-09 02:32:39 -07003135 * Generate lists of code addresses where slabcache objects are allocated
Christoph Lameter88a420e2007-05-06 14:49:45 -07003136 * and freed.
3137 */
3138
3139struct location {
3140 unsigned long count;
3141 void *addr;
Christoph Lameter45edfa52007-05-09 02:32:45 -07003142 long long sum_time;
3143 long min_time;
3144 long max_time;
3145 long min_pid;
3146 long max_pid;
3147 cpumask_t cpus;
3148 nodemask_t nodes;
Christoph Lameter88a420e2007-05-06 14:49:45 -07003149};
3150
3151struct loc_track {
3152 unsigned long max;
3153 unsigned long count;
3154 struct location *loc;
3155};
3156
3157static void free_loc_track(struct loc_track *t)
3158{
3159 if (t->max)
3160 free_pages((unsigned long)t->loc,
3161 get_order(sizeof(struct location) * t->max));
3162}
3163
Christoph Lameter68dff6a2007-07-17 04:03:20 -07003164static int alloc_loc_track(struct loc_track *t, unsigned long max, gfp_t flags)
Christoph Lameter88a420e2007-05-06 14:49:45 -07003165{
3166 struct location *l;
3167 int order;
3168
Christoph Lameter88a420e2007-05-06 14:49:45 -07003169 order = get_order(sizeof(struct location) * max);
3170
Christoph Lameter68dff6a2007-07-17 04:03:20 -07003171 l = (void *)__get_free_pages(flags, order);
Christoph Lameter88a420e2007-05-06 14:49:45 -07003172 if (!l)
3173 return 0;
3174
3175 if (t->count) {
3176 memcpy(l, t->loc, sizeof(struct location) * t->count);
3177 free_loc_track(t);
3178 }
3179 t->max = max;
3180 t->loc = l;
3181 return 1;
3182}
3183
3184static int add_location(struct loc_track *t, struct kmem_cache *s,
Christoph Lameter45edfa52007-05-09 02:32:45 -07003185 const struct track *track)
Christoph Lameter88a420e2007-05-06 14:49:45 -07003186{
3187 long start, end, pos;
3188 struct location *l;
3189 void *caddr;
Christoph Lameter45edfa52007-05-09 02:32:45 -07003190 unsigned long age = jiffies - track->when;
Christoph Lameter88a420e2007-05-06 14:49:45 -07003191
3192 start = -1;
3193 end = t->count;
3194
3195 for ( ; ; ) {
3196 pos = start + (end - start + 1) / 2;
3197
3198 /*
3199 * There is nothing at "end". If we end up there
3200 * we need to add something to before end.
3201 */
3202 if (pos == end)
3203 break;
3204
3205 caddr = t->loc[pos].addr;
Christoph Lameter45edfa52007-05-09 02:32:45 -07003206 if (track->addr == caddr) {
3207
3208 l = &t->loc[pos];
3209 l->count++;
3210 if (track->when) {
3211 l->sum_time += age;
3212 if (age < l->min_time)
3213 l->min_time = age;
3214 if (age > l->max_time)
3215 l->max_time = age;
3216
3217 if (track->pid < l->min_pid)
3218 l->min_pid = track->pid;
3219 if (track->pid > l->max_pid)
3220 l->max_pid = track->pid;
3221
3222 cpu_set(track->cpu, l->cpus);
3223 }
3224 node_set(page_to_nid(virt_to_page(track)), l->nodes);
Christoph Lameter88a420e2007-05-06 14:49:45 -07003225 return 1;
3226 }
3227
Christoph Lameter45edfa52007-05-09 02:32:45 -07003228 if (track->addr < caddr)
Christoph Lameter88a420e2007-05-06 14:49:45 -07003229 end = pos;
3230 else
3231 start = pos;
3232 }
3233
3234 /*
Christoph Lameter672bba32007-05-09 02:32:39 -07003235 * Not found. Insert new tracking element.
Christoph Lameter88a420e2007-05-06 14:49:45 -07003236 */
Christoph Lameter68dff6a2007-07-17 04:03:20 -07003237 if (t->count >= t->max && !alloc_loc_track(t, 2 * t->max, GFP_ATOMIC))
Christoph Lameter88a420e2007-05-06 14:49:45 -07003238 return 0;
3239
3240 l = t->loc + pos;
3241 if (pos < t->count)
3242 memmove(l + 1, l,
3243 (t->count - pos) * sizeof(struct location));
3244 t->count++;
3245 l->count = 1;
Christoph Lameter45edfa52007-05-09 02:32:45 -07003246 l->addr = track->addr;
3247 l->sum_time = age;
3248 l->min_time = age;
3249 l->max_time = age;
3250 l->min_pid = track->pid;
3251 l->max_pid = track->pid;
3252 cpus_clear(l->cpus);
3253 cpu_set(track->cpu, l->cpus);
3254 nodes_clear(l->nodes);
3255 node_set(page_to_nid(virt_to_page(track)), l->nodes);
Christoph Lameter88a420e2007-05-06 14:49:45 -07003256 return 1;
3257}
3258
3259static void process_slab(struct loc_track *t, struct kmem_cache *s,
3260 struct page *page, enum track_item alloc)
3261{
3262 void *addr = page_address(page);
Christoph Lameter7656c722007-05-09 02:32:40 -07003263 DECLARE_BITMAP(map, s->objects);
Christoph Lameter88a420e2007-05-06 14:49:45 -07003264 void *p;
3265
3266 bitmap_zero(map, s->objects);
Christoph Lameter7656c722007-05-09 02:32:40 -07003267 for_each_free_object(p, s, page->freelist)
3268 set_bit(slab_index(p, s, addr), map);
Christoph Lameter88a420e2007-05-06 14:49:45 -07003269
Christoph Lameter7656c722007-05-09 02:32:40 -07003270 for_each_object(p, s, addr)
Christoph Lameter45edfa52007-05-09 02:32:45 -07003271 if (!test_bit(slab_index(p, s, addr), map))
3272 add_location(t, s, get_track(s, p, alloc));
Christoph Lameter88a420e2007-05-06 14:49:45 -07003273}
3274
3275static int list_locations(struct kmem_cache *s, char *buf,
3276 enum track_item alloc)
3277{
3278 int n = 0;
3279 unsigned long i;
Christoph Lameter68dff6a2007-07-17 04:03:20 -07003280 struct loc_track t = { 0, 0, NULL };
Christoph Lameter88a420e2007-05-06 14:49:45 -07003281 int node;
3282
Christoph Lameter68dff6a2007-07-17 04:03:20 -07003283 if (!alloc_loc_track(&t, PAGE_SIZE / sizeof(struct location),
Andrew Mortonea3061d2007-10-16 01:26:09 -07003284 GFP_TEMPORARY))
Christoph Lameter68dff6a2007-07-17 04:03:20 -07003285 return sprintf(buf, "Out of memory\n");
Christoph Lameter88a420e2007-05-06 14:49:45 -07003286
3287 /* Push back cpu slabs */
3288 flush_all(s);
3289
Christoph Lameterf64dc582007-10-16 01:25:33 -07003290 for_each_node_state(node, N_NORMAL_MEMORY) {
Christoph Lameter88a420e2007-05-06 14:49:45 -07003291 struct kmem_cache_node *n = get_node(s, node);
3292 unsigned long flags;
3293 struct page *page;
3294
Christoph Lameter9e869432007-08-22 14:01:56 -07003295 if (!atomic_long_read(&n->nr_slabs))
Christoph Lameter88a420e2007-05-06 14:49:45 -07003296 continue;
3297
3298 spin_lock_irqsave(&n->list_lock, flags);
3299 list_for_each_entry(page, &n->partial, lru)
3300 process_slab(&t, s, page, alloc);
3301 list_for_each_entry(page, &n->full, lru)
3302 process_slab(&t, s, page, alloc);
3303 spin_unlock_irqrestore(&n->list_lock, flags);
3304 }
3305
3306 for (i = 0; i < t.count; i++) {
Christoph Lameter45edfa52007-05-09 02:32:45 -07003307 struct location *l = &t.loc[i];
Christoph Lameter88a420e2007-05-06 14:49:45 -07003308
3309 if (n > PAGE_SIZE - 100)
3310 break;
Christoph Lameter45edfa52007-05-09 02:32:45 -07003311 n += sprintf(buf + n, "%7ld ", l->count);
3312
3313 if (l->addr)
3314 n += sprint_symbol(buf + n, (unsigned long)l->addr);
Christoph Lameter88a420e2007-05-06 14:49:45 -07003315 else
3316 n += sprintf(buf + n, "<not-available>");
Christoph Lameter45edfa52007-05-09 02:32:45 -07003317
3318 if (l->sum_time != l->min_time) {
3319 unsigned long remainder;
3320
3321 n += sprintf(buf + n, " age=%ld/%ld/%ld",
3322 l->min_time,
3323 div_long_long_rem(l->sum_time, l->count, &remainder),
3324 l->max_time);
3325 } else
3326 n += sprintf(buf + n, " age=%ld",
3327 l->min_time);
3328
3329 if (l->min_pid != l->max_pid)
3330 n += sprintf(buf + n, " pid=%ld-%ld",
3331 l->min_pid, l->max_pid);
3332 else
3333 n += sprintf(buf + n, " pid=%ld",
3334 l->min_pid);
3335
Christoph Lameter84966342007-06-23 17:16:32 -07003336 if (num_online_cpus() > 1 && !cpus_empty(l->cpus) &&
3337 n < PAGE_SIZE - 60) {
Christoph Lameter45edfa52007-05-09 02:32:45 -07003338 n += sprintf(buf + n, " cpus=");
3339 n += cpulist_scnprintf(buf + n, PAGE_SIZE - n - 50,
3340 l->cpus);
3341 }
3342
Christoph Lameter84966342007-06-23 17:16:32 -07003343 if (num_online_nodes() > 1 && !nodes_empty(l->nodes) &&
3344 n < PAGE_SIZE - 60) {
Christoph Lameter45edfa52007-05-09 02:32:45 -07003345 n += sprintf(buf + n, " nodes=");
3346 n += nodelist_scnprintf(buf + n, PAGE_SIZE - n - 50,
3347 l->nodes);
3348 }
3349
Christoph Lameter88a420e2007-05-06 14:49:45 -07003350 n += sprintf(buf + n, "\n");
3351 }
3352
3353 free_loc_track(&t);
3354 if (!t.count)
3355 n += sprintf(buf, "No data\n");
3356 return n;
3357}
3358
Christoph Lameter81819f02007-05-06 14:49:36 -07003359static unsigned long count_partial(struct kmem_cache_node *n)
3360{
3361 unsigned long flags;
3362 unsigned long x = 0;
3363 struct page *page;
3364
3365 spin_lock_irqsave(&n->list_lock, flags);
3366 list_for_each_entry(page, &n->partial, lru)
3367 x += page->inuse;
3368 spin_unlock_irqrestore(&n->list_lock, flags);
3369 return x;
3370}
3371
3372enum slab_stat_type {
3373 SL_FULL,
3374 SL_PARTIAL,
3375 SL_CPU,
3376 SL_OBJECTS
3377};
3378
3379#define SO_FULL (1 << SL_FULL)
3380#define SO_PARTIAL (1 << SL_PARTIAL)
3381#define SO_CPU (1 << SL_CPU)
3382#define SO_OBJECTS (1 << SL_OBJECTS)
3383
3384static unsigned long slab_objects(struct kmem_cache *s,
3385 char *buf, unsigned long flags)
3386{
3387 unsigned long total = 0;
3388 int cpu;
3389 int node;
3390 int x;
3391 unsigned long *nodes;
3392 unsigned long *per_cpu;
3393
3394 nodes = kzalloc(2 * sizeof(unsigned long) * nr_node_ids, GFP_KERNEL);
3395 per_cpu = nodes + nr_node_ids;
3396
3397 for_each_possible_cpu(cpu) {
Christoph Lameterdfb4f092007-10-16 01:26:05 -07003398 struct page *page;
Christoph Lameteree3c72a2007-10-16 01:26:07 -07003399 int node;
Christoph Lameterdfb4f092007-10-16 01:26:05 -07003400 struct kmem_cache_cpu *c = get_cpu_slab(s, cpu);
Christoph Lameter81819f02007-05-06 14:49:36 -07003401
Christoph Lameterdfb4f092007-10-16 01:26:05 -07003402 if (!c)
3403 continue;
3404
3405 page = c->page;
Christoph Lameteree3c72a2007-10-16 01:26:07 -07003406 node = c->node;
3407 if (node < 0)
3408 continue;
Christoph Lameter81819f02007-05-06 14:49:36 -07003409 if (page) {
Christoph Lameter81819f02007-05-06 14:49:36 -07003410 if (flags & SO_CPU) {
3411 int x = 0;
3412
3413 if (flags & SO_OBJECTS)
3414 x = page->inuse;
3415 else
3416 x = 1;
3417 total += x;
Christoph Lameteree3c72a2007-10-16 01:26:07 -07003418 nodes[node] += x;
Christoph Lameter81819f02007-05-06 14:49:36 -07003419 }
Christoph Lameteree3c72a2007-10-16 01:26:07 -07003420 per_cpu[node]++;
Christoph Lameter81819f02007-05-06 14:49:36 -07003421 }
3422 }
3423
Christoph Lameterf64dc582007-10-16 01:25:33 -07003424 for_each_node_state(node, N_NORMAL_MEMORY) {
Christoph Lameter81819f02007-05-06 14:49:36 -07003425 struct kmem_cache_node *n = get_node(s, node);
3426
3427 if (flags & SO_PARTIAL) {
3428 if (flags & SO_OBJECTS)
3429 x = count_partial(n);
3430 else
3431 x = n->nr_partial;
3432 total += x;
3433 nodes[node] += x;
3434 }
3435
3436 if (flags & SO_FULL) {
Christoph Lameter9e869432007-08-22 14:01:56 -07003437 int full_slabs = atomic_long_read(&n->nr_slabs)
Christoph Lameter81819f02007-05-06 14:49:36 -07003438 - per_cpu[node]
3439 - n->nr_partial;
3440
3441 if (flags & SO_OBJECTS)
3442 x = full_slabs * s->objects;
3443 else
3444 x = full_slabs;
3445 total += x;
3446 nodes[node] += x;
3447 }
3448 }
3449
3450 x = sprintf(buf, "%lu", total);
3451#ifdef CONFIG_NUMA
Christoph Lameterf64dc582007-10-16 01:25:33 -07003452 for_each_node_state(node, N_NORMAL_MEMORY)
Christoph Lameter81819f02007-05-06 14:49:36 -07003453 if (nodes[node])
3454 x += sprintf(buf + x, " N%d=%lu",
3455 node, nodes[node]);
3456#endif
3457 kfree(nodes);
3458 return x + sprintf(buf + x, "\n");
3459}
3460
3461static int any_slab_objects(struct kmem_cache *s)
3462{
3463 int node;
3464 int cpu;
3465
Christoph Lameterdfb4f092007-10-16 01:26:05 -07003466 for_each_possible_cpu(cpu) {
3467 struct kmem_cache_cpu *c = get_cpu_slab(s, cpu);
Christoph Lameter81819f02007-05-06 14:49:36 -07003468
Christoph Lameterdfb4f092007-10-16 01:26:05 -07003469 if (c && c->page)
3470 return 1;
3471 }
3472
3473 for_each_online_node(node) {
Christoph Lameter81819f02007-05-06 14:49:36 -07003474 struct kmem_cache_node *n = get_node(s, node);
3475
Christoph Lameterdfb4f092007-10-16 01:26:05 -07003476 if (!n)
3477 continue;
3478
Christoph Lameter9e869432007-08-22 14:01:56 -07003479 if (n->nr_partial || atomic_long_read(&n->nr_slabs))
Christoph Lameter81819f02007-05-06 14:49:36 -07003480 return 1;
3481 }
3482 return 0;
3483}
3484
3485#define to_slab_attr(n) container_of(n, struct slab_attribute, attr)
3486#define to_slab(n) container_of(n, struct kmem_cache, kobj);
3487
3488struct slab_attribute {
3489 struct attribute attr;
3490 ssize_t (*show)(struct kmem_cache *s, char *buf);
3491 ssize_t (*store)(struct kmem_cache *s, const char *x, size_t count);
3492};
3493
3494#define SLAB_ATTR_RO(_name) \
3495 static struct slab_attribute _name##_attr = __ATTR_RO(_name)
3496
3497#define SLAB_ATTR(_name) \
3498 static struct slab_attribute _name##_attr = \
3499 __ATTR(_name, 0644, _name##_show, _name##_store)
3500
Christoph Lameter81819f02007-05-06 14:49:36 -07003501static ssize_t slab_size_show(struct kmem_cache *s, char *buf)
3502{
3503 return sprintf(buf, "%d\n", s->size);
3504}
3505SLAB_ATTR_RO(slab_size);
3506
3507static ssize_t align_show(struct kmem_cache *s, char *buf)
3508{
3509 return sprintf(buf, "%d\n", s->align);
3510}
3511SLAB_ATTR_RO(align);
3512
3513static ssize_t object_size_show(struct kmem_cache *s, char *buf)
3514{
3515 return sprintf(buf, "%d\n", s->objsize);
3516}
3517SLAB_ATTR_RO(object_size);
3518
3519static ssize_t objs_per_slab_show(struct kmem_cache *s, char *buf)
3520{
3521 return sprintf(buf, "%d\n", s->objects);
3522}
3523SLAB_ATTR_RO(objs_per_slab);
3524
3525static ssize_t order_show(struct kmem_cache *s, char *buf)
3526{
3527 return sprintf(buf, "%d\n", s->order);
3528}
3529SLAB_ATTR_RO(order);
3530
3531static ssize_t ctor_show(struct kmem_cache *s, char *buf)
3532{
3533 if (s->ctor) {
3534 int n = sprint_symbol(buf, (unsigned long)s->ctor);
3535
3536 return n + sprintf(buf + n, "\n");
3537 }
3538 return 0;
3539}
3540SLAB_ATTR_RO(ctor);
3541
Christoph Lameter81819f02007-05-06 14:49:36 -07003542static ssize_t aliases_show(struct kmem_cache *s, char *buf)
3543{
3544 return sprintf(buf, "%d\n", s->refcount - 1);
3545}
3546SLAB_ATTR_RO(aliases);
3547
3548static ssize_t slabs_show(struct kmem_cache *s, char *buf)
3549{
3550 return slab_objects(s, buf, SO_FULL|SO_PARTIAL|SO_CPU);
3551}
3552SLAB_ATTR_RO(slabs);
3553
3554static ssize_t partial_show(struct kmem_cache *s, char *buf)
3555{
3556 return slab_objects(s, buf, SO_PARTIAL);
3557}
3558SLAB_ATTR_RO(partial);
3559
3560static ssize_t cpu_slabs_show(struct kmem_cache *s, char *buf)
3561{
3562 return slab_objects(s, buf, SO_CPU);
3563}
3564SLAB_ATTR_RO(cpu_slabs);
3565
3566static ssize_t objects_show(struct kmem_cache *s, char *buf)
3567{
3568 return slab_objects(s, buf, SO_FULL|SO_PARTIAL|SO_CPU|SO_OBJECTS);
3569}
3570SLAB_ATTR_RO(objects);
3571
3572static ssize_t sanity_checks_show(struct kmem_cache *s, char *buf)
3573{
3574 return sprintf(buf, "%d\n", !!(s->flags & SLAB_DEBUG_FREE));
3575}
3576
3577static ssize_t sanity_checks_store(struct kmem_cache *s,
3578 const char *buf, size_t length)
3579{
3580 s->flags &= ~SLAB_DEBUG_FREE;
3581 if (buf[0] == '1')
3582 s->flags |= SLAB_DEBUG_FREE;
3583 return length;
3584}
3585SLAB_ATTR(sanity_checks);
3586
3587static ssize_t trace_show(struct kmem_cache *s, char *buf)
3588{
3589 return sprintf(buf, "%d\n", !!(s->flags & SLAB_TRACE));
3590}
3591
3592static ssize_t trace_store(struct kmem_cache *s, const char *buf,
3593 size_t length)
3594{
3595 s->flags &= ~SLAB_TRACE;
3596 if (buf[0] == '1')
3597 s->flags |= SLAB_TRACE;
3598 return length;
3599}
3600SLAB_ATTR(trace);
3601
3602static ssize_t reclaim_account_show(struct kmem_cache *s, char *buf)
3603{
3604 return sprintf(buf, "%d\n", !!(s->flags & SLAB_RECLAIM_ACCOUNT));
3605}
3606
3607static ssize_t reclaim_account_store(struct kmem_cache *s,
3608 const char *buf, size_t length)
3609{
3610 s->flags &= ~SLAB_RECLAIM_ACCOUNT;
3611 if (buf[0] == '1')
3612 s->flags |= SLAB_RECLAIM_ACCOUNT;
3613 return length;
3614}
3615SLAB_ATTR(reclaim_account);
3616
3617static ssize_t hwcache_align_show(struct kmem_cache *s, char *buf)
3618{
Christoph Lameter5af60832007-05-06 14:49:56 -07003619 return sprintf(buf, "%d\n", !!(s->flags & SLAB_HWCACHE_ALIGN));
Christoph Lameter81819f02007-05-06 14:49:36 -07003620}
3621SLAB_ATTR_RO(hwcache_align);
3622
3623#ifdef CONFIG_ZONE_DMA
3624static ssize_t cache_dma_show(struct kmem_cache *s, char *buf)
3625{
3626 return sprintf(buf, "%d\n", !!(s->flags & SLAB_CACHE_DMA));
3627}
3628SLAB_ATTR_RO(cache_dma);
3629#endif
3630
3631static ssize_t destroy_by_rcu_show(struct kmem_cache *s, char *buf)
3632{
3633 return sprintf(buf, "%d\n", !!(s->flags & SLAB_DESTROY_BY_RCU));
3634}
3635SLAB_ATTR_RO(destroy_by_rcu);
3636
3637static ssize_t red_zone_show(struct kmem_cache *s, char *buf)
3638{
3639 return sprintf(buf, "%d\n", !!(s->flags & SLAB_RED_ZONE));
3640}
3641
3642static ssize_t red_zone_store(struct kmem_cache *s,
3643 const char *buf, size_t length)
3644{
3645 if (any_slab_objects(s))
3646 return -EBUSY;
3647
3648 s->flags &= ~SLAB_RED_ZONE;
3649 if (buf[0] == '1')
3650 s->flags |= SLAB_RED_ZONE;
3651 calculate_sizes(s);
3652 return length;
3653}
3654SLAB_ATTR(red_zone);
3655
3656static ssize_t poison_show(struct kmem_cache *s, char *buf)
3657{
3658 return sprintf(buf, "%d\n", !!(s->flags & SLAB_POISON));
3659}
3660
3661static ssize_t poison_store(struct kmem_cache *s,
3662 const char *buf, size_t length)
3663{
3664 if (any_slab_objects(s))
3665 return -EBUSY;
3666
3667 s->flags &= ~SLAB_POISON;
3668 if (buf[0] == '1')
3669 s->flags |= SLAB_POISON;
3670 calculate_sizes(s);
3671 return length;
3672}
3673SLAB_ATTR(poison);
3674
3675static ssize_t store_user_show(struct kmem_cache *s, char *buf)
3676{
3677 return sprintf(buf, "%d\n", !!(s->flags & SLAB_STORE_USER));
3678}
3679
3680static ssize_t store_user_store(struct kmem_cache *s,
3681 const char *buf, size_t length)
3682{
3683 if (any_slab_objects(s))
3684 return -EBUSY;
3685
3686 s->flags &= ~SLAB_STORE_USER;
3687 if (buf[0] == '1')
3688 s->flags |= SLAB_STORE_USER;
3689 calculate_sizes(s);
3690 return length;
3691}
3692SLAB_ATTR(store_user);
3693
Christoph Lameter53e15af2007-05-06 14:49:43 -07003694static ssize_t validate_show(struct kmem_cache *s, char *buf)
3695{
3696 return 0;
3697}
3698
3699static ssize_t validate_store(struct kmem_cache *s,
3700 const char *buf, size_t length)
3701{
Christoph Lameter434e2452007-07-17 04:03:30 -07003702 int ret = -EINVAL;
3703
3704 if (buf[0] == '1') {
3705 ret = validate_slab_cache(s);
3706 if (ret >= 0)
3707 ret = length;
3708 }
3709 return ret;
Christoph Lameter53e15af2007-05-06 14:49:43 -07003710}
3711SLAB_ATTR(validate);
3712
Christoph Lameter2086d262007-05-06 14:49:46 -07003713static ssize_t shrink_show(struct kmem_cache *s, char *buf)
3714{
3715 return 0;
3716}
3717
3718static ssize_t shrink_store(struct kmem_cache *s,
3719 const char *buf, size_t length)
3720{
3721 if (buf[0] == '1') {
3722 int rc = kmem_cache_shrink(s);
3723
3724 if (rc)
3725 return rc;
3726 } else
3727 return -EINVAL;
3728 return length;
3729}
3730SLAB_ATTR(shrink);
3731
Christoph Lameter88a420e2007-05-06 14:49:45 -07003732static ssize_t alloc_calls_show(struct kmem_cache *s, char *buf)
3733{
3734 if (!(s->flags & SLAB_STORE_USER))
3735 return -ENOSYS;
3736 return list_locations(s, buf, TRACK_ALLOC);
3737}
3738SLAB_ATTR_RO(alloc_calls);
3739
3740static ssize_t free_calls_show(struct kmem_cache *s, char *buf)
3741{
3742 if (!(s->flags & SLAB_STORE_USER))
3743 return -ENOSYS;
3744 return list_locations(s, buf, TRACK_FREE);
3745}
3746SLAB_ATTR_RO(free_calls);
3747
Christoph Lameter81819f02007-05-06 14:49:36 -07003748#ifdef CONFIG_NUMA
3749static ssize_t defrag_ratio_show(struct kmem_cache *s, char *buf)
3750{
3751 return sprintf(buf, "%d\n", s->defrag_ratio / 10);
3752}
3753
3754static ssize_t defrag_ratio_store(struct kmem_cache *s,
3755 const char *buf, size_t length)
3756{
3757 int n = simple_strtoul(buf, NULL, 10);
3758
3759 if (n < 100)
3760 s->defrag_ratio = n * 10;
3761 return length;
3762}
3763SLAB_ATTR(defrag_ratio);
3764#endif
3765
3766static struct attribute * slab_attrs[] = {
3767 &slab_size_attr.attr,
3768 &object_size_attr.attr,
3769 &objs_per_slab_attr.attr,
3770 &order_attr.attr,
3771 &objects_attr.attr,
3772 &slabs_attr.attr,
3773 &partial_attr.attr,
3774 &cpu_slabs_attr.attr,
3775 &ctor_attr.attr,
Christoph Lameter81819f02007-05-06 14:49:36 -07003776 &aliases_attr.attr,
3777 &align_attr.attr,
3778 &sanity_checks_attr.attr,
3779 &trace_attr.attr,
3780 &hwcache_align_attr.attr,
3781 &reclaim_account_attr.attr,
3782 &destroy_by_rcu_attr.attr,
3783 &red_zone_attr.attr,
3784 &poison_attr.attr,
3785 &store_user_attr.attr,
Christoph Lameter53e15af2007-05-06 14:49:43 -07003786 &validate_attr.attr,
Christoph Lameter2086d262007-05-06 14:49:46 -07003787 &shrink_attr.attr,
Christoph Lameter88a420e2007-05-06 14:49:45 -07003788 &alloc_calls_attr.attr,
3789 &free_calls_attr.attr,
Christoph Lameter81819f02007-05-06 14:49:36 -07003790#ifdef CONFIG_ZONE_DMA
3791 &cache_dma_attr.attr,
3792#endif
3793#ifdef CONFIG_NUMA
3794 &defrag_ratio_attr.attr,
3795#endif
3796 NULL
3797};
3798
3799static struct attribute_group slab_attr_group = {
3800 .attrs = slab_attrs,
3801};
3802
3803static ssize_t slab_attr_show(struct kobject *kobj,
3804 struct attribute *attr,
3805 char *buf)
3806{
3807 struct slab_attribute *attribute;
3808 struct kmem_cache *s;
3809 int err;
3810
3811 attribute = to_slab_attr(attr);
3812 s = to_slab(kobj);
3813
3814 if (!attribute->show)
3815 return -EIO;
3816
3817 err = attribute->show(s, buf);
3818
3819 return err;
3820}
3821
3822static ssize_t slab_attr_store(struct kobject *kobj,
3823 struct attribute *attr,
3824 const char *buf, size_t len)
3825{
3826 struct slab_attribute *attribute;
3827 struct kmem_cache *s;
3828 int err;
3829
3830 attribute = to_slab_attr(attr);
3831 s = to_slab(kobj);
3832
3833 if (!attribute->store)
3834 return -EIO;
3835
3836 err = attribute->store(s, buf, len);
3837
3838 return err;
3839}
3840
3841static struct sysfs_ops slab_sysfs_ops = {
3842 .show = slab_attr_show,
3843 .store = slab_attr_store,
3844};
3845
3846static struct kobj_type slab_ktype = {
3847 .sysfs_ops = &slab_sysfs_ops,
3848};
3849
3850static int uevent_filter(struct kset *kset, struct kobject *kobj)
3851{
3852 struct kobj_type *ktype = get_ktype(kobj);
3853
3854 if (ktype == &slab_ktype)
3855 return 1;
3856 return 0;
3857}
3858
3859static struct kset_uevent_ops slab_uevent_ops = {
3860 .filter = uevent_filter,
3861};
3862
Adrian Bunk5af328a2007-07-17 04:03:27 -07003863static decl_subsys(slab, &slab_ktype, &slab_uevent_ops);
Christoph Lameter81819f02007-05-06 14:49:36 -07003864
3865#define ID_STR_LENGTH 64
3866
3867/* Create a unique string id for a slab cache:
3868 * format
3869 * :[flags-]size:[memory address of kmemcache]
3870 */
3871static char *create_unique_id(struct kmem_cache *s)
3872{
3873 char *name = kmalloc(ID_STR_LENGTH, GFP_KERNEL);
3874 char *p = name;
3875
3876 BUG_ON(!name);
3877
3878 *p++ = ':';
3879 /*
3880 * First flags affecting slabcache operations. We will only
3881 * get here for aliasable slabs so we do not need to support
3882 * too many flags. The flags here must cover all flags that
3883 * are matched during merging to guarantee that the id is
3884 * unique.
3885 */
3886 if (s->flags & SLAB_CACHE_DMA)
3887 *p++ = 'd';
3888 if (s->flags & SLAB_RECLAIM_ACCOUNT)
3889 *p++ = 'a';
3890 if (s->flags & SLAB_DEBUG_FREE)
3891 *p++ = 'F';
3892 if (p != name + 1)
3893 *p++ = '-';
3894 p += sprintf(p, "%07d", s->size);
3895 BUG_ON(p > name + ID_STR_LENGTH - 1);
3896 return name;
3897}
3898
3899static int sysfs_slab_add(struct kmem_cache *s)
3900{
3901 int err;
3902 const char *name;
3903 int unmergeable;
3904
3905 if (slab_state < SYSFS)
3906 /* Defer until later */
3907 return 0;
3908
3909 unmergeable = slab_unmergeable(s);
3910 if (unmergeable) {
3911 /*
3912 * Slabcache can never be merged so we can use the name proper.
3913 * This is typically the case for debug situations. In that
3914 * case we can catch duplicate names easily.
3915 */
Linus Torvalds0f9008e2007-05-07 12:31:58 -07003916 sysfs_remove_link(&slab_subsys.kobj, s->name);
Christoph Lameter81819f02007-05-06 14:49:36 -07003917 name = s->name;
3918 } else {
3919 /*
3920 * Create a unique name for the slab as a target
3921 * for the symlinks.
3922 */
3923 name = create_unique_id(s);
3924 }
3925
3926 kobj_set_kset_s(s, slab_subsys);
3927 kobject_set_name(&s->kobj, name);
3928 kobject_init(&s->kobj);
3929 err = kobject_add(&s->kobj);
3930 if (err)
3931 return err;
3932
3933 err = sysfs_create_group(&s->kobj, &slab_attr_group);
3934 if (err)
3935 return err;
3936 kobject_uevent(&s->kobj, KOBJ_ADD);
3937 if (!unmergeable) {
3938 /* Setup first alias */
3939 sysfs_slab_alias(s, s->name);
3940 kfree(name);
3941 }
3942 return 0;
3943}
3944
3945static void sysfs_slab_remove(struct kmem_cache *s)
3946{
3947 kobject_uevent(&s->kobj, KOBJ_REMOVE);
3948 kobject_del(&s->kobj);
3949}
3950
3951/*
3952 * Need to buffer aliases during bootup until sysfs becomes
3953 * available lest we loose that information.
3954 */
3955struct saved_alias {
3956 struct kmem_cache *s;
3957 const char *name;
3958 struct saved_alias *next;
3959};
3960
Adrian Bunk5af328a2007-07-17 04:03:27 -07003961static struct saved_alias *alias_list;
Christoph Lameter81819f02007-05-06 14:49:36 -07003962
3963static int sysfs_slab_alias(struct kmem_cache *s, const char *name)
3964{
3965 struct saved_alias *al;
3966
3967 if (slab_state == SYSFS) {
3968 /*
3969 * If we have a leftover link then remove it.
3970 */
Linus Torvalds0f9008e2007-05-07 12:31:58 -07003971 sysfs_remove_link(&slab_subsys.kobj, name);
3972 return sysfs_create_link(&slab_subsys.kobj,
Christoph Lameter81819f02007-05-06 14:49:36 -07003973 &s->kobj, name);
3974 }
3975
3976 al = kmalloc(sizeof(struct saved_alias), GFP_KERNEL);
3977 if (!al)
3978 return -ENOMEM;
3979
3980 al->s = s;
3981 al->name = name;
3982 al->next = alias_list;
3983 alias_list = al;
3984 return 0;
3985}
3986
3987static int __init slab_sysfs_init(void)
3988{
Christoph Lameter5b95a4ac2007-07-17 04:03:19 -07003989 struct kmem_cache *s;
Christoph Lameter81819f02007-05-06 14:49:36 -07003990 int err;
3991
3992 err = subsystem_register(&slab_subsys);
3993 if (err) {
3994 printk(KERN_ERR "Cannot register slab subsystem.\n");
3995 return -ENOSYS;
3996 }
3997
Christoph Lameter26a7bd02007-05-09 02:32:39 -07003998 slab_state = SYSFS;
3999
Christoph Lameter5b95a4ac2007-07-17 04:03:19 -07004000 list_for_each_entry(s, &slab_caches, list) {
Christoph Lameter26a7bd02007-05-09 02:32:39 -07004001 err = sysfs_slab_add(s);
Christoph Lameter5d540fb2007-08-30 23:56:26 -07004002 if (err)
4003 printk(KERN_ERR "SLUB: Unable to add boot slab %s"
4004 " to sysfs\n", s->name);
Christoph Lameter26a7bd02007-05-09 02:32:39 -07004005 }
Christoph Lameter81819f02007-05-06 14:49:36 -07004006
4007 while (alias_list) {
4008 struct saved_alias *al = alias_list;
4009
4010 alias_list = alias_list->next;
4011 err = sysfs_slab_alias(al->s, al->name);
Christoph Lameter5d540fb2007-08-30 23:56:26 -07004012 if (err)
4013 printk(KERN_ERR "SLUB: Unable to add boot slab alias"
4014 " %s to sysfs\n", s->name);
Christoph Lameter81819f02007-05-06 14:49:36 -07004015 kfree(al);
4016 }
4017
4018 resiliency_test();
4019 return 0;
4020}
4021
4022__initcall(slab_sysfs_init);
Christoph Lameter81819f02007-05-06 14:49:36 -07004023#endif