blob: 992ecd4f0d393c748dceec95e3ddd714b16557b4 [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>
Yasunori Gotob9049e22007-10-21 16:41:37 -070023#include <linux/memory.h>
Christoph Lameter81819f02007-05-06 14:49:36 -070024
25/*
26 * Lock order:
27 * 1. slab_lock(page)
28 * 2. slab->list_lock
29 *
30 * The slab_lock protects operations on the object of a particular
31 * slab and its metadata in the page struct. If the slab lock
32 * has been taken then no allocations nor frees can be performed
33 * on the objects in the slab nor can the slab be added or removed
34 * from the partial or full lists since this would mean modifying
35 * the page_struct of the slab.
36 *
37 * The list_lock protects the partial and full list on each node and
38 * the partial slab counter. If taken then no new slabs may be added or
39 * removed from the lists nor make the number of partial slabs be modified.
40 * (Note that the total number of slabs is an atomic value that may be
41 * modified without taking the list lock).
42 *
43 * The list_lock is a centralized lock and thus we avoid taking it as
44 * much as possible. As long as SLUB does not have to handle partial
45 * slabs, operations can continue without any centralized lock. F.e.
46 * allocating a long series of objects that fill up slabs does not require
47 * the list lock.
48 *
49 * The lock order is sometimes inverted when we are trying to get a slab
50 * off a list. We take the list_lock and then look for a page on the list
51 * to use. While we do that objects in the slabs may be freed. We can
52 * only operate on the slab if we have also taken the slab_lock. So we use
53 * a slab_trylock() on the slab. If trylock was successful then no frees
54 * can occur anymore and we can use the slab for allocations etc. If the
55 * slab_trylock() does not succeed then frees are in progress in the slab and
56 * we must stay away from it for a while since we may cause a bouncing
57 * cacheline if we try to acquire the lock. So go onto the next slab.
58 * If all pages are busy then we may allocate a new slab instead of reusing
59 * a partial slab. A new slab has noone operating on it and thus there is
60 * no danger of cacheline contention.
61 *
62 * Interrupts are disabled during allocation and deallocation in order to
63 * make the slab allocator safe to use in the context of an irq. In addition
64 * interrupts are disabled to ensure that the processor does not change
65 * while handling per_cpu slabs, due to kernel preemption.
66 *
67 * SLUB assigns one slab for allocation to each processor.
68 * Allocations only occur from these slabs called cpu slabs.
69 *
Christoph Lameter672bba32007-05-09 02:32:39 -070070 * Slabs with free elements are kept on a partial list and during regular
71 * operations no list for full slabs is used. If an object in a full slab is
Christoph Lameter81819f02007-05-06 14:49:36 -070072 * freed then the slab will show up again on the partial lists.
Christoph Lameter672bba32007-05-09 02:32:39 -070073 * We track full slabs for debugging purposes though because otherwise we
74 * cannot scan all objects.
Christoph Lameter81819f02007-05-06 14:49:36 -070075 *
76 * Slabs are freed when they become empty. Teardown and setup is
77 * minimal so we rely on the page allocators per cpu caches for
78 * fast frees and allocs.
79 *
80 * Overloading of page flags that are otherwise used for LRU management.
81 *
Christoph Lameter4b6f0752007-05-16 22:10:53 -070082 * PageActive The slab is frozen and exempt from list processing.
83 * This means that the slab is dedicated to a purpose
84 * such as satisfying allocations for a specific
85 * processor. Objects may be freed in the slab while
86 * it is frozen but slab_free will then skip the usual
87 * list operations. It is up to the processor holding
88 * the slab to integrate the slab into the slab lists
89 * when the slab is no longer needed.
90 *
91 * One use of this flag is to mark slabs that are
92 * used for allocations. Then such a slab becomes a cpu
93 * slab. The cpu slab may be equipped with an additional
Christoph Lameterdfb4f092007-10-16 01:26:05 -070094 * freelist that allows lockless access to
Christoph Lameter894b8782007-05-10 03:15:16 -070095 * free objects in addition to the regular freelist
96 * that requires the slab lock.
Christoph Lameter81819f02007-05-06 14:49:36 -070097 *
98 * PageError Slab requires special handling due to debug
99 * options set. This moves slab handling out of
Christoph Lameter894b8782007-05-10 03:15:16 -0700100 * the fast path and disables lockless freelists.
Christoph Lameter81819f02007-05-06 14:49:36 -0700101 */
102
Christoph Lameter5577bd82007-05-16 22:10:56 -0700103#define FROZEN (1 << PG_active)
104
105#ifdef CONFIG_SLUB_DEBUG
106#define SLABDEBUG (1 << PG_error)
107#else
108#define SLABDEBUG 0
109#endif
110
Christoph Lameter4b6f0752007-05-16 22:10:53 -0700111static inline int SlabFrozen(struct page *page)
112{
Christoph Lameter5577bd82007-05-16 22:10:56 -0700113 return page->flags & FROZEN;
Christoph Lameter4b6f0752007-05-16 22:10:53 -0700114}
115
116static inline void SetSlabFrozen(struct page *page)
117{
Christoph Lameter5577bd82007-05-16 22:10:56 -0700118 page->flags |= FROZEN;
Christoph Lameter4b6f0752007-05-16 22:10:53 -0700119}
120
121static inline void ClearSlabFrozen(struct page *page)
122{
Christoph Lameter5577bd82007-05-16 22:10:56 -0700123 page->flags &= ~FROZEN;
Christoph Lameter4b6f0752007-05-16 22:10:53 -0700124}
125
Christoph Lameter35e5d7e2007-05-09 02:32:42 -0700126static inline int SlabDebug(struct page *page)
127{
Christoph Lameter5577bd82007-05-16 22:10:56 -0700128 return page->flags & SLABDEBUG;
Christoph Lameter35e5d7e2007-05-09 02:32:42 -0700129}
130
131static inline void SetSlabDebug(struct page *page)
132{
Christoph Lameter5577bd82007-05-16 22:10:56 -0700133 page->flags |= SLABDEBUG;
Christoph Lameter35e5d7e2007-05-09 02:32:42 -0700134}
135
136static inline void ClearSlabDebug(struct page *page)
137{
Christoph Lameter5577bd82007-05-16 22:10:56 -0700138 page->flags &= ~SLABDEBUG;
Christoph Lameter35e5d7e2007-05-09 02:32:42 -0700139}
140
Christoph Lameter81819f02007-05-06 14:49:36 -0700141/*
142 * Issues still to be resolved:
143 *
Christoph Lameter81819f02007-05-06 14:49:36 -0700144 * - Support PAGE_ALLOC_DEBUG. Should be easy to do.
145 *
Christoph Lameter81819f02007-05-06 14:49:36 -0700146 * - Variable sizing of the per node arrays
147 */
148
149/* Enable to test recovery from slab corruption on boot */
150#undef SLUB_RESILIENCY_TEST
151
Christoph Lameter81819f02007-05-06 14:49:36 -0700152/*
Christoph Lameter2086d262007-05-06 14:49:46 -0700153 * Mininum number of partial slabs. These will be left on the partial
154 * lists even if they are empty. kmem_cache_shrink may reclaim them.
155 */
Christoph Lameter76be8952007-12-21 14:37:37 -0800156#define MIN_PARTIAL 5
Christoph Lametere95eed52007-05-06 14:49:44 -0700157
Christoph Lameter2086d262007-05-06 14:49:46 -0700158/*
159 * Maximum number of desirable partial slabs.
160 * The existence of more partial slabs makes kmem_cache_shrink
161 * sort the partial list by the number of objects in the.
162 */
163#define MAX_PARTIAL 10
164
Christoph Lameter81819f02007-05-06 14:49:36 -0700165#define DEBUG_DEFAULT_FLAGS (SLAB_DEBUG_FREE | SLAB_RED_ZONE | \
166 SLAB_POISON | SLAB_STORE_USER)
Christoph Lameter672bba32007-05-09 02:32:39 -0700167
Christoph Lameter81819f02007-05-06 14:49:36 -0700168/*
169 * Set of flags that will prevent slab merging
170 */
171#define SLUB_NEVER_MERGE (SLAB_RED_ZONE | SLAB_POISON | SLAB_STORE_USER | \
172 SLAB_TRACE | SLAB_DESTROY_BY_RCU)
173
174#define SLUB_MERGE_SAME (SLAB_DEBUG_FREE | SLAB_RECLAIM_ACCOUNT | \
175 SLAB_CACHE_DMA)
176
177#ifndef ARCH_KMALLOC_MINALIGN
Christoph Lameter47bfdc02007-05-06 14:49:37 -0700178#define ARCH_KMALLOC_MINALIGN __alignof__(unsigned long long)
Christoph Lameter81819f02007-05-06 14:49:36 -0700179#endif
180
181#ifndef ARCH_SLAB_MINALIGN
Christoph Lameter47bfdc02007-05-06 14:49:37 -0700182#define ARCH_SLAB_MINALIGN __alignof__(unsigned long long)
Christoph Lameter81819f02007-05-06 14:49:36 -0700183#endif
184
185/* Internal SLUB flags */
Christoph Lameter1ceef402007-08-07 15:11:48 -0700186#define __OBJECT_POISON 0x80000000 /* Poison object */
187#define __SYSFS_ADD_DEFERRED 0x40000000 /* Not yet visible via sysfs */
Christoph Lameter81819f02007-05-06 14:49:36 -0700188
189static int kmem_size = sizeof(struct kmem_cache);
190
191#ifdef CONFIG_SMP
192static struct notifier_block slab_notifier;
193#endif
194
195static enum {
196 DOWN, /* No slab functionality available */
197 PARTIAL, /* kmem_cache_open() works but kmalloc does not */
Christoph Lameter672bba32007-05-09 02:32:39 -0700198 UP, /* Everything works but does not show up in sysfs */
Christoph Lameter81819f02007-05-06 14:49:36 -0700199 SYSFS /* Sysfs up */
200} slab_state = DOWN;
201
202/* A list of all slab caches on the system */
203static DECLARE_RWSEM(slub_lock);
Adrian Bunk5af328a2007-07-17 04:03:27 -0700204static LIST_HEAD(slab_caches);
Christoph Lameter81819f02007-05-06 14:49:36 -0700205
Christoph Lameter02cbc872007-05-09 02:32:43 -0700206/*
207 * Tracking user of a slab.
208 */
209struct track {
210 void *addr; /* Called from address */
211 int cpu; /* Was running on cpu */
212 int pid; /* Pid context */
213 unsigned long when; /* When did the operation occur */
214};
215
216enum track_item { TRACK_ALLOC, TRACK_FREE };
217
Christoph Lameter41ecc552007-05-09 02:32:44 -0700218#if defined(CONFIG_SYSFS) && defined(CONFIG_SLUB_DEBUG)
Christoph Lameter81819f02007-05-06 14:49:36 -0700219static int sysfs_slab_add(struct kmem_cache *);
220static int sysfs_slab_alias(struct kmem_cache *, const char *);
221static void sysfs_slab_remove(struct kmem_cache *);
Christoph Lameter8ff12cf2008-02-07 17:47:41 -0800222
Christoph Lameter81819f02007-05-06 14:49:36 -0700223#else
Christoph Lameter0c710012007-07-17 04:03:24 -0700224static inline int sysfs_slab_add(struct kmem_cache *s) { return 0; }
225static inline int sysfs_slab_alias(struct kmem_cache *s, const char *p)
226 { return 0; }
Christoph Lameter151c6022008-01-07 22:29:05 -0800227static inline void sysfs_slab_remove(struct kmem_cache *s)
228{
229 kfree(s);
230}
Christoph Lameter8ff12cf2008-02-07 17:47:41 -0800231
Christoph Lameter81819f02007-05-06 14:49:36 -0700232#endif
233
Christoph Lameter8ff12cf2008-02-07 17:47:41 -0800234static inline void stat(struct kmem_cache_cpu *c, enum stat_item si)
235{
236#ifdef CONFIG_SLUB_STATS
237 c->stat[si]++;
238#endif
239}
240
Christoph Lameter81819f02007-05-06 14:49:36 -0700241/********************************************************************
242 * Core slab cache functions
243 *******************************************************************/
244
245int slab_is_available(void)
246{
247 return slab_state >= UP;
248}
249
250static inline struct kmem_cache_node *get_node(struct kmem_cache *s, int node)
251{
252#ifdef CONFIG_NUMA
253 return s->node[node];
254#else
255 return &s->local_node;
256#endif
257}
258
Christoph Lameterdfb4f092007-10-16 01:26:05 -0700259static inline struct kmem_cache_cpu *get_cpu_slab(struct kmem_cache *s, int cpu)
260{
Christoph Lameter4c93c3552007-10-16 01:26:08 -0700261#ifdef CONFIG_SMP
262 return s->cpu_slab[cpu];
263#else
264 return &s->cpu_slab;
265#endif
Christoph Lameterdfb4f092007-10-16 01:26:05 -0700266}
267
Christoph Lameter6446faa2008-02-15 23:45:26 -0800268/* Verify that a pointer has an address that is valid within a slab page */
Christoph Lameter02cbc872007-05-09 02:32:43 -0700269static inline int check_valid_pointer(struct kmem_cache *s,
270 struct page *page, const void *object)
271{
272 void *base;
273
Christoph Lametera973e9d2008-03-01 13:40:44 -0800274 if (!object)
Christoph Lameter02cbc872007-05-09 02:32:43 -0700275 return 1;
276
Christoph Lametera973e9d2008-03-01 13:40:44 -0800277 base = page_address(page);
Christoph Lameter39b26462008-04-14 19:11:30 +0300278 if (object < base || object >= base + page->objects * s->size ||
Christoph Lameter02cbc872007-05-09 02:32:43 -0700279 (object - base) % s->size) {
280 return 0;
281 }
282
283 return 1;
284}
285
Christoph Lameter81819f02007-05-06 14:49:36 -0700286/*
Christoph Lameter7656c722007-05-09 02:32:40 -0700287 * Slow version of get and set free pointer.
288 *
289 * This version requires touching the cache lines of kmem_cache which
290 * we avoid to do in the fast alloc free paths. There we obtain the offset
291 * from the page struct.
292 */
293static inline void *get_freepointer(struct kmem_cache *s, void *object)
294{
295 return *(void **)(object + s->offset);
296}
297
298static inline void set_freepointer(struct kmem_cache *s, void *object, void *fp)
299{
300 *(void **)(object + s->offset) = fp;
301}
302
303/* Loop over all objects in a slab */
Christoph Lameter224a88b2008-04-14 19:11:31 +0300304#define for_each_object(__p, __s, __addr, __objects) \
305 for (__p = (__addr); __p < (__addr) + (__objects) * (__s)->size;\
Christoph Lameter7656c722007-05-09 02:32:40 -0700306 __p += (__s)->size)
307
308/* Scan freelist */
309#define for_each_free_object(__p, __s, __free) \
Christoph Lametera973e9d2008-03-01 13:40:44 -0800310 for (__p = (__free); __p; __p = get_freepointer((__s), __p))
Christoph Lameter7656c722007-05-09 02:32:40 -0700311
312/* Determine object index from a given position */
313static inline int slab_index(void *p, struct kmem_cache *s, void *addr)
314{
315 return (p - addr) / s->size;
316}
317
Christoph Lameter834f3d12008-04-14 19:11:31 +0300318static inline struct kmem_cache_order_objects oo_make(int order,
319 unsigned long size)
320{
321 struct kmem_cache_order_objects x = {
322 (order << 16) + (PAGE_SIZE << order) / size
323 };
324
325 return x;
326}
327
328static inline int oo_order(struct kmem_cache_order_objects x)
329{
330 return x.x >> 16;
331}
332
333static inline int oo_objects(struct kmem_cache_order_objects x)
334{
335 return x.x & ((1 << 16) - 1);
336}
337
Christoph Lameter41ecc552007-05-09 02:32:44 -0700338#ifdef CONFIG_SLUB_DEBUG
339/*
340 * Debug settings:
341 */
Christoph Lameterf0630ff2007-07-15 23:38:14 -0700342#ifdef CONFIG_SLUB_DEBUG_ON
343static int slub_debug = DEBUG_DEFAULT_FLAGS;
344#else
Christoph Lameter41ecc552007-05-09 02:32:44 -0700345static int slub_debug;
Christoph Lameterf0630ff2007-07-15 23:38:14 -0700346#endif
Christoph Lameter41ecc552007-05-09 02:32:44 -0700347
348static char *slub_debug_slabs;
349
Christoph Lameter7656c722007-05-09 02:32:40 -0700350/*
Christoph Lameter81819f02007-05-06 14:49:36 -0700351 * Object debugging
352 */
353static void print_section(char *text, u8 *addr, unsigned int length)
354{
355 int i, offset;
356 int newline = 1;
357 char ascii[17];
358
359 ascii[16] = 0;
360
361 for (i = 0; i < length; i++) {
362 if (newline) {
Christoph Lameter24922682007-07-17 04:03:18 -0700363 printk(KERN_ERR "%8s 0x%p: ", text, addr + i);
Christoph Lameter81819f02007-05-06 14:49:36 -0700364 newline = 0;
365 }
Pekka Enberg06428782008-01-07 23:20:27 -0800366 printk(KERN_CONT " %02x", addr[i]);
Christoph Lameter81819f02007-05-06 14:49:36 -0700367 offset = i % 16;
368 ascii[offset] = isgraph(addr[i]) ? addr[i] : '.';
369 if (offset == 15) {
Pekka Enberg06428782008-01-07 23:20:27 -0800370 printk(KERN_CONT " %s\n", ascii);
Christoph Lameter81819f02007-05-06 14:49:36 -0700371 newline = 1;
372 }
373 }
374 if (!newline) {
375 i %= 16;
376 while (i < 16) {
Pekka Enberg06428782008-01-07 23:20:27 -0800377 printk(KERN_CONT " ");
Christoph Lameter81819f02007-05-06 14:49:36 -0700378 ascii[i] = ' ';
379 i++;
380 }
Pekka Enberg06428782008-01-07 23:20:27 -0800381 printk(KERN_CONT " %s\n", ascii);
Christoph Lameter81819f02007-05-06 14:49:36 -0700382 }
383}
384
Christoph Lameter81819f02007-05-06 14:49:36 -0700385static struct track *get_track(struct kmem_cache *s, void *object,
386 enum track_item alloc)
387{
388 struct track *p;
389
390 if (s->offset)
391 p = object + s->offset + sizeof(void *);
392 else
393 p = object + s->inuse;
394
395 return p + alloc;
396}
397
398static void set_track(struct kmem_cache *s, void *object,
399 enum track_item alloc, void *addr)
400{
401 struct track *p;
402
403 if (s->offset)
404 p = object + s->offset + sizeof(void *);
405 else
406 p = object + s->inuse;
407
408 p += alloc;
409 if (addr) {
410 p->addr = addr;
411 p->cpu = smp_processor_id();
412 p->pid = current ? current->pid : -1;
413 p->when = jiffies;
414 } else
415 memset(p, 0, sizeof(struct track));
416}
417
Christoph Lameter81819f02007-05-06 14:49:36 -0700418static void init_tracking(struct kmem_cache *s, void *object)
419{
Christoph Lameter24922682007-07-17 04:03:18 -0700420 if (!(s->flags & SLAB_STORE_USER))
421 return;
422
423 set_track(s, object, TRACK_FREE, NULL);
424 set_track(s, object, TRACK_ALLOC, NULL);
Christoph Lameter81819f02007-05-06 14:49:36 -0700425}
426
427static void print_track(const char *s, struct track *t)
428{
429 if (!t->addr)
430 return;
431
Christoph Lameter24922682007-07-17 04:03:18 -0700432 printk(KERN_ERR "INFO: %s in ", s);
Christoph Lameter81819f02007-05-06 14:49:36 -0700433 __print_symbol("%s", (unsigned long)t->addr);
Christoph Lameter24922682007-07-17 04:03:18 -0700434 printk(" age=%lu cpu=%u pid=%d\n", jiffies - t->when, t->cpu, t->pid);
Christoph Lameter81819f02007-05-06 14:49:36 -0700435}
436
Christoph Lameter24922682007-07-17 04:03:18 -0700437static void print_tracking(struct kmem_cache *s, void *object)
438{
439 if (!(s->flags & SLAB_STORE_USER))
440 return;
441
442 print_track("Allocated", get_track(s, object, TRACK_ALLOC));
443 print_track("Freed", get_track(s, object, TRACK_FREE));
444}
445
446static void print_page_info(struct page *page)
447{
Christoph Lameter39b26462008-04-14 19:11:30 +0300448 printk(KERN_ERR "INFO: Slab 0x%p objects=%u used=%u fp=0x%p flags=0x%04lx\n",
449 page, page->objects, page->inuse, page->freelist, page->flags);
Christoph Lameter24922682007-07-17 04:03:18 -0700450
451}
452
453static void slab_bug(struct kmem_cache *s, char *fmt, ...)
454{
455 va_list args;
456 char buf[100];
457
458 va_start(args, fmt);
459 vsnprintf(buf, sizeof(buf), fmt, args);
460 va_end(args);
461 printk(KERN_ERR "========================================"
462 "=====================================\n");
463 printk(KERN_ERR "BUG %s: %s\n", s->name, buf);
464 printk(KERN_ERR "----------------------------------------"
465 "-------------------------------------\n\n");
466}
467
468static void slab_fix(struct kmem_cache *s, char *fmt, ...)
469{
470 va_list args;
471 char buf[100];
472
473 va_start(args, fmt);
474 vsnprintf(buf, sizeof(buf), fmt, args);
475 va_end(args);
476 printk(KERN_ERR "FIX %s: %s\n", s->name, buf);
477}
478
479static void print_trailer(struct kmem_cache *s, struct page *page, u8 *p)
Christoph Lameter81819f02007-05-06 14:49:36 -0700480{
481 unsigned int off; /* Offset of last byte */
Christoph Lametera973e9d2008-03-01 13:40:44 -0800482 u8 *addr = page_address(page);
Christoph Lameter24922682007-07-17 04:03:18 -0700483
484 print_tracking(s, p);
485
486 print_page_info(page);
487
488 printk(KERN_ERR "INFO: Object 0x%p @offset=%tu fp=0x%p\n\n",
489 p, p - addr, get_freepointer(s, p));
490
491 if (p > addr + 16)
492 print_section("Bytes b4", p - 16, 16);
493
494 print_section("Object", p, min(s->objsize, 128));
Christoph Lameter81819f02007-05-06 14:49:36 -0700495
496 if (s->flags & SLAB_RED_ZONE)
497 print_section("Redzone", p + s->objsize,
498 s->inuse - s->objsize);
499
Christoph Lameter81819f02007-05-06 14:49:36 -0700500 if (s->offset)
501 off = s->offset + sizeof(void *);
502 else
503 off = s->inuse;
504
Christoph Lameter24922682007-07-17 04:03:18 -0700505 if (s->flags & SLAB_STORE_USER)
Christoph Lameter81819f02007-05-06 14:49:36 -0700506 off += 2 * sizeof(struct track);
Christoph Lameter81819f02007-05-06 14:49:36 -0700507
508 if (off != s->size)
509 /* Beginning of the filler is the free pointer */
Christoph Lameter24922682007-07-17 04:03:18 -0700510 print_section("Padding", p + off, s->size - off);
511
512 dump_stack();
Christoph Lameter81819f02007-05-06 14:49:36 -0700513}
514
515static void object_err(struct kmem_cache *s, struct page *page,
516 u8 *object, char *reason)
517{
Christoph Lameter3dc50632008-04-23 12:28:01 -0700518 slab_bug(s, "%s", reason);
Christoph Lameter24922682007-07-17 04:03:18 -0700519 print_trailer(s, page, object);
Christoph Lameter81819f02007-05-06 14:49:36 -0700520}
521
Christoph Lameter24922682007-07-17 04:03:18 -0700522static void slab_err(struct kmem_cache *s, struct page *page, char *fmt, ...)
Christoph Lameter81819f02007-05-06 14:49:36 -0700523{
524 va_list args;
525 char buf[100];
526
Christoph Lameter24922682007-07-17 04:03:18 -0700527 va_start(args, fmt);
528 vsnprintf(buf, sizeof(buf), fmt, args);
Christoph Lameter81819f02007-05-06 14:49:36 -0700529 va_end(args);
Christoph Lameter3dc50632008-04-23 12:28:01 -0700530 slab_bug(s, "%s", buf);
Christoph Lameter24922682007-07-17 04:03:18 -0700531 print_page_info(page);
Christoph Lameter81819f02007-05-06 14:49:36 -0700532 dump_stack();
533}
534
535static void init_object(struct kmem_cache *s, void *object, int active)
536{
537 u8 *p = object;
538
539 if (s->flags & __OBJECT_POISON) {
540 memset(p, POISON_FREE, s->objsize - 1);
Pekka Enberg06428782008-01-07 23:20:27 -0800541 p[s->objsize - 1] = POISON_END;
Christoph Lameter81819f02007-05-06 14:49:36 -0700542 }
543
544 if (s->flags & SLAB_RED_ZONE)
545 memset(p + s->objsize,
546 active ? SLUB_RED_ACTIVE : SLUB_RED_INACTIVE,
547 s->inuse - s->objsize);
548}
549
Christoph Lameter24922682007-07-17 04:03:18 -0700550static u8 *check_bytes(u8 *start, unsigned int value, unsigned int bytes)
Christoph Lameter81819f02007-05-06 14:49:36 -0700551{
552 while (bytes) {
553 if (*start != (u8)value)
Christoph Lameter24922682007-07-17 04:03:18 -0700554 return start;
Christoph Lameter81819f02007-05-06 14:49:36 -0700555 start++;
556 bytes--;
557 }
Christoph Lameter24922682007-07-17 04:03:18 -0700558 return NULL;
559}
560
561static void restore_bytes(struct kmem_cache *s, char *message, u8 data,
562 void *from, void *to)
563{
564 slab_fix(s, "Restoring 0x%p-0x%p=0x%x\n", from, to - 1, data);
565 memset(from, data, to - from);
566}
567
568static int check_bytes_and_report(struct kmem_cache *s, struct page *page,
569 u8 *object, char *what,
Pekka Enberg06428782008-01-07 23:20:27 -0800570 u8 *start, unsigned int value, unsigned int bytes)
Christoph Lameter24922682007-07-17 04:03:18 -0700571{
572 u8 *fault;
573 u8 *end;
574
575 fault = check_bytes(start, value, bytes);
576 if (!fault)
577 return 1;
578
579 end = start + bytes;
580 while (end > fault && end[-1] == value)
581 end--;
582
583 slab_bug(s, "%s overwritten", what);
584 printk(KERN_ERR "INFO: 0x%p-0x%p. First byte 0x%x instead of 0x%x\n",
585 fault, end - 1, fault[0], value);
586 print_trailer(s, page, object);
587
588 restore_bytes(s, what, value, fault, end);
589 return 0;
Christoph Lameter81819f02007-05-06 14:49:36 -0700590}
591
Christoph Lameter81819f02007-05-06 14:49:36 -0700592/*
593 * Object layout:
594 *
595 * object address
596 * Bytes of the object to be managed.
597 * If the freepointer may overlay the object then the free
598 * pointer is the first word of the object.
Christoph Lameter672bba32007-05-09 02:32:39 -0700599 *
Christoph Lameter81819f02007-05-06 14:49:36 -0700600 * Poisoning uses 0x6b (POISON_FREE) and the last byte is
601 * 0xa5 (POISON_END)
602 *
603 * object + s->objsize
604 * Padding to reach word boundary. This is also used for Redzoning.
Christoph Lameter672bba32007-05-09 02:32:39 -0700605 * Padding is extended by another word if Redzoning is enabled and
606 * objsize == inuse.
607 *
Christoph Lameter81819f02007-05-06 14:49:36 -0700608 * We fill with 0xbb (RED_INACTIVE) for inactive objects and with
609 * 0xcc (RED_ACTIVE) for objects in use.
610 *
611 * object + s->inuse
Christoph Lameter672bba32007-05-09 02:32:39 -0700612 * Meta data starts here.
613 *
Christoph Lameter81819f02007-05-06 14:49:36 -0700614 * A. Free pointer (if we cannot overwrite object on free)
615 * B. Tracking data for SLAB_STORE_USER
Christoph Lameter672bba32007-05-09 02:32:39 -0700616 * C. Padding to reach required alignment boundary or at mininum
Christoph Lameter6446faa2008-02-15 23:45:26 -0800617 * one word if debugging is on to be able to detect writes
Christoph Lameter672bba32007-05-09 02:32:39 -0700618 * before the word boundary.
619 *
620 * Padding is done using 0x5a (POISON_INUSE)
Christoph Lameter81819f02007-05-06 14:49:36 -0700621 *
622 * object + s->size
Christoph Lameter672bba32007-05-09 02:32:39 -0700623 * Nothing is used beyond s->size.
Christoph Lameter81819f02007-05-06 14:49:36 -0700624 *
Christoph Lameter672bba32007-05-09 02:32:39 -0700625 * If slabcaches are merged then the objsize and inuse boundaries are mostly
626 * ignored. And therefore no slab options that rely on these boundaries
Christoph Lameter81819f02007-05-06 14:49:36 -0700627 * may be used with merged slabcaches.
628 */
629
Christoph Lameter81819f02007-05-06 14:49:36 -0700630static int check_pad_bytes(struct kmem_cache *s, struct page *page, u8 *p)
631{
632 unsigned long off = s->inuse; /* The end of info */
633
634 if (s->offset)
635 /* Freepointer is placed after the object. */
636 off += sizeof(void *);
637
638 if (s->flags & SLAB_STORE_USER)
639 /* We also have user information there */
640 off += 2 * sizeof(struct track);
641
642 if (s->size == off)
643 return 1;
644
Christoph Lameter24922682007-07-17 04:03:18 -0700645 return check_bytes_and_report(s, page, p, "Object padding",
646 p + off, POISON_INUSE, s->size - off);
Christoph Lameter81819f02007-05-06 14:49:36 -0700647}
648
Christoph Lameter39b26462008-04-14 19:11:30 +0300649/* Check the pad bytes at the end of a slab page */
Christoph Lameter81819f02007-05-06 14:49:36 -0700650static int slab_pad_check(struct kmem_cache *s, struct page *page)
651{
Christoph Lameter24922682007-07-17 04:03:18 -0700652 u8 *start;
653 u8 *fault;
654 u8 *end;
655 int length;
656 int remainder;
Christoph Lameter81819f02007-05-06 14:49:36 -0700657
658 if (!(s->flags & SLAB_POISON))
659 return 1;
660
Christoph Lametera973e9d2008-03-01 13:40:44 -0800661 start = page_address(page);
Christoph Lameter834f3d12008-04-14 19:11:31 +0300662 length = (PAGE_SIZE << compound_order(page));
Christoph Lameter39b26462008-04-14 19:11:30 +0300663 end = start + length;
664 remainder = length % s->size;
Christoph Lameter81819f02007-05-06 14:49:36 -0700665 if (!remainder)
666 return 1;
667
Christoph Lameter39b26462008-04-14 19:11:30 +0300668 fault = check_bytes(end - remainder, POISON_INUSE, remainder);
Christoph Lameter24922682007-07-17 04:03:18 -0700669 if (!fault)
670 return 1;
671 while (end > fault && end[-1] == POISON_INUSE)
672 end--;
673
674 slab_err(s, page, "Padding overwritten. 0x%p-0x%p", fault, end - 1);
Christoph Lameter39b26462008-04-14 19:11:30 +0300675 print_section("Padding", end - remainder, remainder);
Christoph Lameter24922682007-07-17 04:03:18 -0700676
677 restore_bytes(s, "slab padding", POISON_INUSE, start, end);
678 return 0;
Christoph Lameter81819f02007-05-06 14:49:36 -0700679}
680
681static int check_object(struct kmem_cache *s, struct page *page,
682 void *object, int active)
683{
684 u8 *p = object;
685 u8 *endobject = object + s->objsize;
686
687 if (s->flags & SLAB_RED_ZONE) {
688 unsigned int red =
689 active ? SLUB_RED_ACTIVE : SLUB_RED_INACTIVE;
690
Christoph Lameter24922682007-07-17 04:03:18 -0700691 if (!check_bytes_and_report(s, page, object, "Redzone",
692 endobject, red, s->inuse - s->objsize))
Christoph Lameter81819f02007-05-06 14:49:36 -0700693 return 0;
Christoph Lameter81819f02007-05-06 14:49:36 -0700694 } else {
Ingo Molnar3adbefe2008-02-05 17:57:39 -0800695 if ((s->flags & SLAB_POISON) && s->objsize < s->inuse) {
696 check_bytes_and_report(s, page, p, "Alignment padding",
697 endobject, POISON_INUSE, s->inuse - s->objsize);
698 }
Christoph Lameter81819f02007-05-06 14:49:36 -0700699 }
700
701 if (s->flags & SLAB_POISON) {
702 if (!active && (s->flags & __OBJECT_POISON) &&
Christoph Lameter24922682007-07-17 04:03:18 -0700703 (!check_bytes_and_report(s, page, p, "Poison", p,
704 POISON_FREE, s->objsize - 1) ||
705 !check_bytes_and_report(s, page, p, "Poison",
Pekka Enberg06428782008-01-07 23:20:27 -0800706 p + s->objsize - 1, POISON_END, 1)))
Christoph Lameter81819f02007-05-06 14:49:36 -0700707 return 0;
Christoph Lameter81819f02007-05-06 14:49:36 -0700708 /*
709 * check_pad_bytes cleans up on its own.
710 */
711 check_pad_bytes(s, page, p);
712 }
713
714 if (!s->offset && active)
715 /*
716 * Object and freepointer overlap. Cannot check
717 * freepointer while object is allocated.
718 */
719 return 1;
720
721 /* Check free pointer validity */
722 if (!check_valid_pointer(s, page, get_freepointer(s, p))) {
723 object_err(s, page, p, "Freepointer corrupt");
724 /*
725 * No choice but to zap it and thus loose the remainder
726 * of the free objects in this slab. May cause
Christoph Lameter672bba32007-05-09 02:32:39 -0700727 * another error because the object count is now wrong.
Christoph Lameter81819f02007-05-06 14:49:36 -0700728 */
Christoph Lametera973e9d2008-03-01 13:40:44 -0800729 set_freepointer(s, p, NULL);
Christoph Lameter81819f02007-05-06 14:49:36 -0700730 return 0;
731 }
732 return 1;
733}
734
735static int check_slab(struct kmem_cache *s, struct page *page)
736{
Christoph Lameter39b26462008-04-14 19:11:30 +0300737 int maxobj;
738
Christoph Lameter81819f02007-05-06 14:49:36 -0700739 VM_BUG_ON(!irqs_disabled());
740
741 if (!PageSlab(page)) {
Christoph Lameter24922682007-07-17 04:03:18 -0700742 slab_err(s, page, "Not a valid slab page");
Christoph Lameter81819f02007-05-06 14:49:36 -0700743 return 0;
744 }
Christoph Lameter39b26462008-04-14 19:11:30 +0300745
746 maxobj = (PAGE_SIZE << compound_order(page)) / s->size;
747 if (page->objects > maxobj) {
748 slab_err(s, page, "objects %u > max %u",
749 s->name, page->objects, maxobj);
750 return 0;
751 }
752 if (page->inuse > page->objects) {
Christoph Lameter24922682007-07-17 04:03:18 -0700753 slab_err(s, page, "inuse %u > max %u",
Christoph Lameter39b26462008-04-14 19:11:30 +0300754 s->name, page->inuse, page->objects);
Christoph Lameter81819f02007-05-06 14:49:36 -0700755 return 0;
756 }
757 /* Slab_pad_check fixes things up after itself */
758 slab_pad_check(s, page);
759 return 1;
760}
761
762/*
Christoph Lameter672bba32007-05-09 02:32:39 -0700763 * Determine if a certain object on a page is on the freelist. Must hold the
764 * slab lock to guarantee that the chains are in a consistent state.
Christoph Lameter81819f02007-05-06 14:49:36 -0700765 */
766static int on_freelist(struct kmem_cache *s, struct page *page, void *search)
767{
768 int nr = 0;
769 void *fp = page->freelist;
770 void *object = NULL;
Christoph Lameter224a88b2008-04-14 19:11:31 +0300771 unsigned long max_objects;
Christoph Lameter81819f02007-05-06 14:49:36 -0700772
Christoph Lameter39b26462008-04-14 19:11:30 +0300773 while (fp && nr <= page->objects) {
Christoph Lameter81819f02007-05-06 14:49:36 -0700774 if (fp == search)
775 return 1;
776 if (!check_valid_pointer(s, page, fp)) {
777 if (object) {
778 object_err(s, page, object,
779 "Freechain corrupt");
Christoph Lametera973e9d2008-03-01 13:40:44 -0800780 set_freepointer(s, object, NULL);
Christoph Lameter81819f02007-05-06 14:49:36 -0700781 break;
782 } else {
Christoph Lameter24922682007-07-17 04:03:18 -0700783 slab_err(s, page, "Freepointer corrupt");
Christoph Lametera973e9d2008-03-01 13:40:44 -0800784 page->freelist = NULL;
Christoph Lameter39b26462008-04-14 19:11:30 +0300785 page->inuse = page->objects;
Christoph Lameter24922682007-07-17 04:03:18 -0700786 slab_fix(s, "Freelist cleared");
Christoph Lameter81819f02007-05-06 14:49:36 -0700787 return 0;
788 }
789 break;
790 }
791 object = fp;
792 fp = get_freepointer(s, object);
793 nr++;
794 }
795
Christoph Lameter224a88b2008-04-14 19:11:31 +0300796 max_objects = (PAGE_SIZE << compound_order(page)) / s->size;
797 if (max_objects > 65535)
798 max_objects = 65535;
799
800 if (page->objects != max_objects) {
801 slab_err(s, page, "Wrong number of objects. Found %d but "
802 "should be %d", page->objects, max_objects);
803 page->objects = max_objects;
804 slab_fix(s, "Number of objects adjusted.");
805 }
Christoph Lameter39b26462008-04-14 19:11:30 +0300806 if (page->inuse != page->objects - nr) {
Christoph Lameter70d71222007-05-06 14:49:47 -0700807 slab_err(s, page, "Wrong object count. Counter is %d but "
Christoph Lameter39b26462008-04-14 19:11:30 +0300808 "counted were %d", page->inuse, page->objects - nr);
809 page->inuse = page->objects - nr;
Christoph Lameter24922682007-07-17 04:03:18 -0700810 slab_fix(s, "Object count adjusted.");
Christoph Lameter81819f02007-05-06 14:49:36 -0700811 }
812 return search == NULL;
813}
814
Christoph Lameter3ec09742007-05-16 22:11:00 -0700815static void trace(struct kmem_cache *s, struct page *page, void *object, int alloc)
816{
817 if (s->flags & SLAB_TRACE) {
818 printk(KERN_INFO "TRACE %s %s 0x%p inuse=%d fp=0x%p\n",
819 s->name,
820 alloc ? "alloc" : "free",
821 object, page->inuse,
822 page->freelist);
823
824 if (!alloc)
825 print_section("Object", (void *)object, s->objsize);
826
827 dump_stack();
828 }
829}
830
Christoph Lameter643b1132007-05-06 14:49:42 -0700831/*
Christoph Lameter672bba32007-05-09 02:32:39 -0700832 * Tracking of fully allocated slabs for debugging purposes.
Christoph Lameter643b1132007-05-06 14:49:42 -0700833 */
Christoph Lametere95eed52007-05-06 14:49:44 -0700834static void add_full(struct kmem_cache_node *n, struct page *page)
Christoph Lameter643b1132007-05-06 14:49:42 -0700835{
Christoph Lameter643b1132007-05-06 14:49:42 -0700836 spin_lock(&n->list_lock);
837 list_add(&page->lru, &n->full);
838 spin_unlock(&n->list_lock);
839}
840
841static void remove_full(struct kmem_cache *s, struct page *page)
842{
843 struct kmem_cache_node *n;
844
845 if (!(s->flags & SLAB_STORE_USER))
846 return;
847
848 n = get_node(s, page_to_nid(page));
849
850 spin_lock(&n->list_lock);
851 list_del(&page->lru);
852 spin_unlock(&n->list_lock);
853}
854
Christoph Lameter0f389ec2008-04-14 18:53:02 +0300855/* Tracking of the number of slabs for debugging purposes */
856static inline unsigned long slabs_node(struct kmem_cache *s, int node)
857{
858 struct kmem_cache_node *n = get_node(s, node);
859
860 return atomic_long_read(&n->nr_slabs);
861}
862
Christoph Lameter205ab992008-04-14 19:11:40 +0300863static inline void inc_slabs_node(struct kmem_cache *s, int node, int objects)
Christoph Lameter0f389ec2008-04-14 18:53:02 +0300864{
865 struct kmem_cache_node *n = get_node(s, node);
866
867 /*
868 * May be called early in order to allocate a slab for the
869 * kmem_cache_node structure. Solve the chicken-egg
870 * dilemma by deferring the increment of the count during
871 * bootstrap (see early_kmem_cache_node_alloc).
872 */
Christoph Lameter205ab992008-04-14 19:11:40 +0300873 if (!NUMA_BUILD || n) {
Christoph Lameter0f389ec2008-04-14 18:53:02 +0300874 atomic_long_inc(&n->nr_slabs);
Christoph Lameter205ab992008-04-14 19:11:40 +0300875 atomic_long_add(objects, &n->total_objects);
876 }
Christoph Lameter0f389ec2008-04-14 18:53:02 +0300877}
Christoph Lameter205ab992008-04-14 19:11:40 +0300878static inline void dec_slabs_node(struct kmem_cache *s, int node, int objects)
Christoph Lameter0f389ec2008-04-14 18:53:02 +0300879{
880 struct kmem_cache_node *n = get_node(s, node);
881
882 atomic_long_dec(&n->nr_slabs);
Christoph Lameter205ab992008-04-14 19:11:40 +0300883 atomic_long_sub(objects, &n->total_objects);
Christoph Lameter0f389ec2008-04-14 18:53:02 +0300884}
885
886/* Object debug checks for alloc/free paths */
Christoph Lameter3ec09742007-05-16 22:11:00 -0700887static void setup_object_debug(struct kmem_cache *s, struct page *page,
888 void *object)
889{
890 if (!(s->flags & (SLAB_STORE_USER|SLAB_RED_ZONE|__OBJECT_POISON)))
891 return;
892
893 init_object(s, object, 0);
894 init_tracking(s, object);
895}
896
897static int alloc_debug_processing(struct kmem_cache *s, struct page *page,
898 void *object, void *addr)
Christoph Lameter81819f02007-05-06 14:49:36 -0700899{
900 if (!check_slab(s, page))
901 goto bad;
902
Christoph Lameterd692ef62008-02-15 23:45:24 -0800903 if (!on_freelist(s, page, object)) {
Christoph Lameter24922682007-07-17 04:03:18 -0700904 object_err(s, page, object, "Object already allocated");
Christoph Lameter70d71222007-05-06 14:49:47 -0700905 goto bad;
Christoph Lameter81819f02007-05-06 14:49:36 -0700906 }
907
908 if (!check_valid_pointer(s, page, object)) {
909 object_err(s, page, object, "Freelist Pointer check fails");
Christoph Lameter70d71222007-05-06 14:49:47 -0700910 goto bad;
Christoph Lameter81819f02007-05-06 14:49:36 -0700911 }
912
Christoph Lameterd692ef62008-02-15 23:45:24 -0800913 if (!check_object(s, page, object, 0))
Christoph Lameter81819f02007-05-06 14:49:36 -0700914 goto bad;
Christoph Lameter81819f02007-05-06 14:49:36 -0700915
Christoph Lameter3ec09742007-05-16 22:11:00 -0700916 /* Success perform special debug activities for allocs */
917 if (s->flags & SLAB_STORE_USER)
918 set_track(s, object, TRACK_ALLOC, addr);
919 trace(s, page, object, 1);
920 init_object(s, object, 1);
Christoph Lameter81819f02007-05-06 14:49:36 -0700921 return 1;
Christoph Lameter3ec09742007-05-16 22:11:00 -0700922
Christoph Lameter81819f02007-05-06 14:49:36 -0700923bad:
924 if (PageSlab(page)) {
925 /*
926 * If this is a slab page then lets do the best we can
927 * to avoid issues in the future. Marking all objects
Christoph Lameter672bba32007-05-09 02:32:39 -0700928 * as used avoids touching the remaining objects.
Christoph Lameter81819f02007-05-06 14:49:36 -0700929 */
Christoph Lameter24922682007-07-17 04:03:18 -0700930 slab_fix(s, "Marking all objects used");
Christoph Lameter39b26462008-04-14 19:11:30 +0300931 page->inuse = page->objects;
Christoph Lametera973e9d2008-03-01 13:40:44 -0800932 page->freelist = NULL;
Christoph Lameter81819f02007-05-06 14:49:36 -0700933 }
934 return 0;
935}
936
Christoph Lameter3ec09742007-05-16 22:11:00 -0700937static int free_debug_processing(struct kmem_cache *s, struct page *page,
938 void *object, void *addr)
Christoph Lameter81819f02007-05-06 14:49:36 -0700939{
940 if (!check_slab(s, page))
941 goto fail;
942
943 if (!check_valid_pointer(s, page, object)) {
Christoph Lameter70d71222007-05-06 14:49:47 -0700944 slab_err(s, page, "Invalid object pointer 0x%p", object);
Christoph Lameter81819f02007-05-06 14:49:36 -0700945 goto fail;
946 }
947
948 if (on_freelist(s, page, object)) {
Christoph Lameter24922682007-07-17 04:03:18 -0700949 object_err(s, page, object, "Object already free");
Christoph Lameter81819f02007-05-06 14:49:36 -0700950 goto fail;
951 }
952
953 if (!check_object(s, page, object, 1))
954 return 0;
955
956 if (unlikely(s != page->slab)) {
Ingo Molnar3adbefe2008-02-05 17:57:39 -0800957 if (!PageSlab(page)) {
Christoph Lameter70d71222007-05-06 14:49:47 -0700958 slab_err(s, page, "Attempt to free object(0x%p) "
959 "outside of slab", object);
Ingo Molnar3adbefe2008-02-05 17:57:39 -0800960 } else if (!page->slab) {
Christoph Lameter81819f02007-05-06 14:49:36 -0700961 printk(KERN_ERR
Christoph Lameter70d71222007-05-06 14:49:47 -0700962 "SLUB <none>: no slab for object 0x%p.\n",
Christoph Lameter81819f02007-05-06 14:49:36 -0700963 object);
Christoph Lameter70d71222007-05-06 14:49:47 -0700964 dump_stack();
Pekka Enberg06428782008-01-07 23:20:27 -0800965 } else
Christoph Lameter24922682007-07-17 04:03:18 -0700966 object_err(s, page, object,
967 "page slab pointer corrupt.");
Christoph Lameter81819f02007-05-06 14:49:36 -0700968 goto fail;
969 }
Christoph Lameter3ec09742007-05-16 22:11:00 -0700970
971 /* Special debug activities for freeing objects */
Christoph Lametera973e9d2008-03-01 13:40:44 -0800972 if (!SlabFrozen(page) && !page->freelist)
Christoph Lameter3ec09742007-05-16 22:11:00 -0700973 remove_full(s, page);
974 if (s->flags & SLAB_STORE_USER)
975 set_track(s, object, TRACK_FREE, addr);
976 trace(s, page, object, 0);
977 init_object(s, object, 0);
Christoph Lameter81819f02007-05-06 14:49:36 -0700978 return 1;
Christoph Lameter3ec09742007-05-16 22:11:00 -0700979
Christoph Lameter81819f02007-05-06 14:49:36 -0700980fail:
Christoph Lameter24922682007-07-17 04:03:18 -0700981 slab_fix(s, "Object at 0x%p not freed", object);
Christoph Lameter81819f02007-05-06 14:49:36 -0700982 return 0;
983}
984
Christoph Lameter41ecc552007-05-09 02:32:44 -0700985static int __init setup_slub_debug(char *str)
986{
Christoph Lameterf0630ff2007-07-15 23:38:14 -0700987 slub_debug = DEBUG_DEFAULT_FLAGS;
988 if (*str++ != '=' || !*str)
989 /*
990 * No options specified. Switch on full debugging.
991 */
992 goto out;
Christoph Lameter41ecc552007-05-09 02:32:44 -0700993
994 if (*str == ',')
Christoph Lameterf0630ff2007-07-15 23:38:14 -0700995 /*
996 * No options but restriction on slabs. This means full
997 * debugging for slabs matching a pattern.
998 */
999 goto check_slabs;
1000
1001 slub_debug = 0;
1002 if (*str == '-')
1003 /*
1004 * Switch off all debugging measures.
1005 */
1006 goto out;
1007
1008 /*
1009 * Determine which debug features should be switched on
1010 */
Pekka Enberg06428782008-01-07 23:20:27 -08001011 for (; *str && *str != ','; str++) {
Christoph Lameterf0630ff2007-07-15 23:38:14 -07001012 switch (tolower(*str)) {
1013 case 'f':
1014 slub_debug |= SLAB_DEBUG_FREE;
1015 break;
1016 case 'z':
1017 slub_debug |= SLAB_RED_ZONE;
1018 break;
1019 case 'p':
1020 slub_debug |= SLAB_POISON;
1021 break;
1022 case 'u':
1023 slub_debug |= SLAB_STORE_USER;
1024 break;
1025 case 't':
1026 slub_debug |= SLAB_TRACE;
1027 break;
1028 default:
1029 printk(KERN_ERR "slub_debug option '%c' "
Pekka Enberg06428782008-01-07 23:20:27 -08001030 "unknown. skipped\n", *str);
Christoph Lameterf0630ff2007-07-15 23:38:14 -07001031 }
1032 }
1033
1034check_slabs:
1035 if (*str == ',')
Christoph Lameter41ecc552007-05-09 02:32:44 -07001036 slub_debug_slabs = str + 1;
Christoph Lameterf0630ff2007-07-15 23:38:14 -07001037out:
Christoph Lameter41ecc552007-05-09 02:32:44 -07001038 return 1;
1039}
1040
1041__setup("slub_debug", setup_slub_debug);
1042
Christoph Lameterba0268a2007-09-11 15:24:11 -07001043static unsigned long kmem_cache_flags(unsigned long objsize,
1044 unsigned long flags, const char *name,
Christoph Lameter4ba9b9d2007-10-16 23:25:51 -07001045 void (*ctor)(struct kmem_cache *, void *))
Christoph Lameter41ecc552007-05-09 02:32:44 -07001046{
1047 /*
Christoph Lametere1533622008-02-15 23:45:24 -08001048 * Enable debugging if selected on the kernel commandline.
Christoph Lameter41ecc552007-05-09 02:32:44 -07001049 */
Christoph Lametere1533622008-02-15 23:45:24 -08001050 if (slub_debug && (!slub_debug_slabs ||
1051 strncmp(slub_debug_slabs, name, strlen(slub_debug_slabs)) == 0))
1052 flags |= slub_debug;
Christoph Lameterba0268a2007-09-11 15:24:11 -07001053
1054 return flags;
Christoph Lameter41ecc552007-05-09 02:32:44 -07001055}
1056#else
Christoph Lameter3ec09742007-05-16 22:11:00 -07001057static inline void setup_object_debug(struct kmem_cache *s,
1058 struct page *page, void *object) {}
Christoph Lameter41ecc552007-05-09 02:32:44 -07001059
Christoph Lameter3ec09742007-05-16 22:11:00 -07001060static inline int alloc_debug_processing(struct kmem_cache *s,
1061 struct page *page, void *object, void *addr) { return 0; }
Christoph Lameter41ecc552007-05-09 02:32:44 -07001062
Christoph Lameter3ec09742007-05-16 22:11:00 -07001063static inline int free_debug_processing(struct kmem_cache *s,
1064 struct page *page, void *object, void *addr) { return 0; }
Christoph Lameter41ecc552007-05-09 02:32:44 -07001065
Christoph Lameter41ecc552007-05-09 02:32:44 -07001066static inline int slab_pad_check(struct kmem_cache *s, struct page *page)
1067 { return 1; }
1068static inline int check_object(struct kmem_cache *s, struct page *page,
1069 void *object, int active) { return 1; }
Christoph Lameter3ec09742007-05-16 22:11:00 -07001070static inline void add_full(struct kmem_cache_node *n, struct page *page) {}
Christoph Lameterba0268a2007-09-11 15:24:11 -07001071static inline unsigned long kmem_cache_flags(unsigned long objsize,
1072 unsigned long flags, const char *name,
Christoph Lameter4ba9b9d2007-10-16 23:25:51 -07001073 void (*ctor)(struct kmem_cache *, void *))
Christoph Lameterba0268a2007-09-11 15:24:11 -07001074{
1075 return flags;
1076}
Christoph Lameter41ecc552007-05-09 02:32:44 -07001077#define slub_debug 0
Christoph Lameter0f389ec2008-04-14 18:53:02 +03001078
1079static inline unsigned long slabs_node(struct kmem_cache *s, int node)
1080 { return 0; }
Christoph Lameter205ab992008-04-14 19:11:40 +03001081static inline void inc_slabs_node(struct kmem_cache *s, int node,
1082 int objects) {}
1083static inline void dec_slabs_node(struct kmem_cache *s, int node,
1084 int objects) {}
Christoph Lameter41ecc552007-05-09 02:32:44 -07001085#endif
Christoph Lameter205ab992008-04-14 19:11:40 +03001086
Christoph Lameter81819f02007-05-06 14:49:36 -07001087/*
1088 * Slab allocation and freeing
1089 */
Christoph Lameter65c33762008-04-14 19:11:40 +03001090static inline struct page *alloc_slab_page(gfp_t flags, int node,
1091 struct kmem_cache_order_objects oo)
1092{
1093 int order = oo_order(oo);
1094
1095 if (node == -1)
1096 return alloc_pages(flags, order);
1097 else
1098 return alloc_pages_node(node, flags, order);
1099}
1100
Christoph Lameter81819f02007-05-06 14:49:36 -07001101static struct page *allocate_slab(struct kmem_cache *s, gfp_t flags, int node)
1102{
Pekka Enberg06428782008-01-07 23:20:27 -08001103 struct page *page;
Christoph Lameter834f3d12008-04-14 19:11:31 +03001104 struct kmem_cache_order_objects oo = s->oo;
Christoph Lameter81819f02007-05-06 14:49:36 -07001105
Christoph Lameterb7a49f02008-02-14 14:21:32 -08001106 flags |= s->allocflags;
Mel Gormane12ba742007-10-16 01:25:52 -07001107
Christoph Lameter65c33762008-04-14 19:11:40 +03001108 page = alloc_slab_page(flags | __GFP_NOWARN | __GFP_NORETRY, node,
1109 oo);
1110 if (unlikely(!page)) {
1111 oo = s->min;
1112 /*
1113 * Allocation may have failed due to fragmentation.
1114 * Try a lower order alloc if possible
1115 */
1116 page = alloc_slab_page(flags, node, oo);
1117 if (!page)
1118 return NULL;
Christoph Lameter81819f02007-05-06 14:49:36 -07001119
Christoph Lameter65c33762008-04-14 19:11:40 +03001120 stat(get_cpu_slab(s, raw_smp_processor_id()), ORDER_FALLBACK);
1121 }
Christoph Lameter834f3d12008-04-14 19:11:31 +03001122 page->objects = oo_objects(oo);
Christoph Lameter81819f02007-05-06 14:49:36 -07001123 mod_zone_page_state(page_zone(page),
1124 (s->flags & SLAB_RECLAIM_ACCOUNT) ?
1125 NR_SLAB_RECLAIMABLE : NR_SLAB_UNRECLAIMABLE,
Christoph Lameter65c33762008-04-14 19:11:40 +03001126 1 << oo_order(oo));
Christoph Lameter81819f02007-05-06 14:49:36 -07001127
1128 return page;
1129}
1130
1131static void setup_object(struct kmem_cache *s, struct page *page,
1132 void *object)
1133{
Christoph Lameter3ec09742007-05-16 22:11:00 -07001134 setup_object_debug(s, page, object);
Christoph Lameter4f104932007-05-06 14:50:17 -07001135 if (unlikely(s->ctor))
Christoph Lameter4ba9b9d2007-10-16 23:25:51 -07001136 s->ctor(s, object);
Christoph Lameter81819f02007-05-06 14:49:36 -07001137}
1138
1139static struct page *new_slab(struct kmem_cache *s, gfp_t flags, int node)
1140{
1141 struct page *page;
Christoph Lameter81819f02007-05-06 14:49:36 -07001142 void *start;
Christoph Lameter81819f02007-05-06 14:49:36 -07001143 void *last;
1144 void *p;
1145
Christoph Lameter6cb06222007-10-16 01:25:41 -07001146 BUG_ON(flags & GFP_SLAB_BUG_MASK);
Christoph Lameter81819f02007-05-06 14:49:36 -07001147
Christoph Lameter6cb06222007-10-16 01:25:41 -07001148 page = allocate_slab(s,
1149 flags & (GFP_RECLAIM_MASK | GFP_CONSTRAINT_MASK), node);
Christoph Lameter81819f02007-05-06 14:49:36 -07001150 if (!page)
1151 goto out;
1152
Christoph Lameter205ab992008-04-14 19:11:40 +03001153 inc_slabs_node(s, page_to_nid(page), page->objects);
Christoph Lameter81819f02007-05-06 14:49:36 -07001154 page->slab = s;
1155 page->flags |= 1 << PG_slab;
1156 if (s->flags & (SLAB_DEBUG_FREE | SLAB_RED_ZONE | SLAB_POISON |
1157 SLAB_STORE_USER | SLAB_TRACE))
Christoph Lameter35e5d7e2007-05-09 02:32:42 -07001158 SetSlabDebug(page);
Christoph Lameter81819f02007-05-06 14:49:36 -07001159
1160 start = page_address(page);
Christoph Lameter81819f02007-05-06 14:49:36 -07001161
1162 if (unlikely(s->flags & SLAB_POISON))
Christoph Lameter834f3d12008-04-14 19:11:31 +03001163 memset(start, POISON_INUSE, PAGE_SIZE << compound_order(page));
Christoph Lameter81819f02007-05-06 14:49:36 -07001164
1165 last = start;
Christoph Lameter224a88b2008-04-14 19:11:31 +03001166 for_each_object(p, s, start, page->objects) {
Christoph Lameter81819f02007-05-06 14:49:36 -07001167 setup_object(s, page, last);
1168 set_freepointer(s, last, p);
1169 last = p;
1170 }
1171 setup_object(s, page, last);
Christoph Lametera973e9d2008-03-01 13:40:44 -08001172 set_freepointer(s, last, NULL);
Christoph Lameter81819f02007-05-06 14:49:36 -07001173
1174 page->freelist = start;
1175 page->inuse = 0;
1176out:
Christoph Lameter81819f02007-05-06 14:49:36 -07001177 return page;
1178}
1179
1180static void __free_slab(struct kmem_cache *s, struct page *page)
1181{
Christoph Lameter834f3d12008-04-14 19:11:31 +03001182 int order = compound_order(page);
1183 int pages = 1 << order;
Christoph Lameter81819f02007-05-06 14:49:36 -07001184
Christoph Lameterc59def92007-05-16 22:10:50 -07001185 if (unlikely(SlabDebug(page))) {
Christoph Lameter81819f02007-05-06 14:49:36 -07001186 void *p;
1187
1188 slab_pad_check(s, page);
Christoph Lameter224a88b2008-04-14 19:11:31 +03001189 for_each_object(p, s, page_address(page),
1190 page->objects)
Christoph Lameter81819f02007-05-06 14:49:36 -07001191 check_object(s, page, p, 0);
Peter Zijlstra2208b762007-07-26 20:54:34 +02001192 ClearSlabDebug(page);
Christoph Lameter81819f02007-05-06 14:49:36 -07001193 }
1194
1195 mod_zone_page_state(page_zone(page),
1196 (s->flags & SLAB_RECLAIM_ACCOUNT) ?
1197 NR_SLAB_RECLAIMABLE : NR_SLAB_UNRECLAIMABLE,
Pekka Enberg06428782008-01-07 23:20:27 -08001198 -pages);
Christoph Lameter81819f02007-05-06 14:49:36 -07001199
Christoph Lameter49bd5222008-04-14 18:52:18 +03001200 __ClearPageSlab(page);
1201 reset_page_mapcount(page);
Christoph Lameter834f3d12008-04-14 19:11:31 +03001202 __free_pages(page, order);
Christoph Lameter81819f02007-05-06 14:49:36 -07001203}
1204
1205static void rcu_free_slab(struct rcu_head *h)
1206{
1207 struct page *page;
1208
1209 page = container_of((struct list_head *)h, struct page, lru);
1210 __free_slab(page->slab, page);
1211}
1212
1213static void free_slab(struct kmem_cache *s, struct page *page)
1214{
1215 if (unlikely(s->flags & SLAB_DESTROY_BY_RCU)) {
1216 /*
1217 * RCU free overloads the RCU head over the LRU
1218 */
1219 struct rcu_head *head = (void *)&page->lru;
1220
1221 call_rcu(head, rcu_free_slab);
1222 } else
1223 __free_slab(s, page);
1224}
1225
1226static void discard_slab(struct kmem_cache *s, struct page *page)
1227{
Christoph Lameter205ab992008-04-14 19:11:40 +03001228 dec_slabs_node(s, page_to_nid(page), page->objects);
Christoph Lameter81819f02007-05-06 14:49:36 -07001229 free_slab(s, page);
1230}
1231
1232/*
1233 * Per slab locking using the pagelock
1234 */
1235static __always_inline void slab_lock(struct page *page)
1236{
1237 bit_spin_lock(PG_locked, &page->flags);
1238}
1239
1240static __always_inline void slab_unlock(struct page *page)
1241{
Nick Piggina76d3542008-01-07 23:20:27 -08001242 __bit_spin_unlock(PG_locked, &page->flags);
Christoph Lameter81819f02007-05-06 14:49:36 -07001243}
1244
1245static __always_inline int slab_trylock(struct page *page)
1246{
1247 int rc = 1;
1248
1249 rc = bit_spin_trylock(PG_locked, &page->flags);
1250 return rc;
1251}
1252
1253/*
1254 * Management of partially allocated slabs
1255 */
Christoph Lameter7c2e1322008-01-07 23:20:27 -08001256static void add_partial(struct kmem_cache_node *n,
1257 struct page *page, int tail)
Christoph Lameter81819f02007-05-06 14:49:36 -07001258{
Christoph Lametere95eed52007-05-06 14:49:44 -07001259 spin_lock(&n->list_lock);
1260 n->nr_partial++;
Christoph Lameter7c2e1322008-01-07 23:20:27 -08001261 if (tail)
1262 list_add_tail(&page->lru, &n->partial);
1263 else
1264 list_add(&page->lru, &n->partial);
Christoph Lameter81819f02007-05-06 14:49:36 -07001265 spin_unlock(&n->list_lock);
1266}
1267
1268static void remove_partial(struct kmem_cache *s,
1269 struct page *page)
1270{
1271 struct kmem_cache_node *n = get_node(s, page_to_nid(page));
1272
1273 spin_lock(&n->list_lock);
1274 list_del(&page->lru);
1275 n->nr_partial--;
1276 spin_unlock(&n->list_lock);
1277}
1278
1279/*
Christoph Lameter672bba32007-05-09 02:32:39 -07001280 * Lock slab and remove from the partial list.
Christoph Lameter81819f02007-05-06 14:49:36 -07001281 *
Christoph Lameter672bba32007-05-09 02:32:39 -07001282 * Must hold list_lock.
Christoph Lameter81819f02007-05-06 14:49:36 -07001283 */
Christoph Lameter4b6f0752007-05-16 22:10:53 -07001284static inline int lock_and_freeze_slab(struct kmem_cache_node *n, struct page *page)
Christoph Lameter81819f02007-05-06 14:49:36 -07001285{
1286 if (slab_trylock(page)) {
1287 list_del(&page->lru);
1288 n->nr_partial--;
Christoph Lameter4b6f0752007-05-16 22:10:53 -07001289 SetSlabFrozen(page);
Christoph Lameter81819f02007-05-06 14:49:36 -07001290 return 1;
1291 }
1292 return 0;
1293}
1294
1295/*
Christoph Lameter672bba32007-05-09 02:32:39 -07001296 * Try to allocate a partial slab from a specific node.
Christoph Lameter81819f02007-05-06 14:49:36 -07001297 */
1298static struct page *get_partial_node(struct kmem_cache_node *n)
1299{
1300 struct page *page;
1301
1302 /*
1303 * Racy check. If we mistakenly see no partial slabs then we
1304 * just allocate an empty slab. If we mistakenly try to get a
Christoph Lameter672bba32007-05-09 02:32:39 -07001305 * partial slab and there is none available then get_partials()
1306 * will return NULL.
Christoph Lameter81819f02007-05-06 14:49:36 -07001307 */
1308 if (!n || !n->nr_partial)
1309 return NULL;
1310
1311 spin_lock(&n->list_lock);
1312 list_for_each_entry(page, &n->partial, lru)
Christoph Lameter4b6f0752007-05-16 22:10:53 -07001313 if (lock_and_freeze_slab(n, page))
Christoph Lameter81819f02007-05-06 14:49:36 -07001314 goto out;
1315 page = NULL;
1316out:
1317 spin_unlock(&n->list_lock);
1318 return page;
1319}
1320
1321/*
Christoph Lameter672bba32007-05-09 02:32:39 -07001322 * Get a page from somewhere. Search in increasing NUMA distances.
Christoph Lameter81819f02007-05-06 14:49:36 -07001323 */
1324static struct page *get_any_partial(struct kmem_cache *s, gfp_t flags)
1325{
1326#ifdef CONFIG_NUMA
1327 struct zonelist *zonelist;
Mel Gormandd1a2392008-04-28 02:12:17 -07001328 struct zoneref *z;
Mel Gorman54a6eb52008-04-28 02:12:16 -07001329 struct zone *zone;
1330 enum zone_type high_zoneidx = gfp_zone(flags);
Christoph Lameter81819f02007-05-06 14:49:36 -07001331 struct page *page;
1332
1333 /*
Christoph Lameter672bba32007-05-09 02:32:39 -07001334 * The defrag ratio allows a configuration of the tradeoffs between
1335 * inter node defragmentation and node local allocations. A lower
1336 * defrag_ratio increases the tendency to do local allocations
1337 * instead of attempting to obtain partial slabs from other nodes.
Christoph Lameter81819f02007-05-06 14:49:36 -07001338 *
Christoph Lameter672bba32007-05-09 02:32:39 -07001339 * If the defrag_ratio is set to 0 then kmalloc() always
1340 * returns node local objects. If the ratio is higher then kmalloc()
1341 * may return off node objects because partial slabs are obtained
1342 * from other nodes and filled up.
Christoph Lameter81819f02007-05-06 14:49:36 -07001343 *
Christoph Lameter6446faa2008-02-15 23:45:26 -08001344 * If /sys/kernel/slab/xx/defrag_ratio is set to 100 (which makes
Christoph Lameter672bba32007-05-09 02:32:39 -07001345 * defrag_ratio = 1000) then every (well almost) allocation will
1346 * first attempt to defrag slab caches on other nodes. This means
1347 * scanning over all nodes to look for partial slabs which may be
1348 * expensive if we do it every time we are trying to find a slab
1349 * with available objects.
Christoph Lameter81819f02007-05-06 14:49:36 -07001350 */
Christoph Lameter98246012008-01-07 23:20:26 -08001351 if (!s->remote_node_defrag_ratio ||
1352 get_cycles() % 1024 > s->remote_node_defrag_ratio)
Christoph Lameter81819f02007-05-06 14:49:36 -07001353 return NULL;
1354
Mel Gorman0e884602008-04-28 02:12:14 -07001355 zonelist = node_zonelist(slab_node(current->mempolicy), flags);
Mel Gorman54a6eb52008-04-28 02:12:16 -07001356 for_each_zone_zonelist(zone, z, zonelist, high_zoneidx) {
Christoph Lameter81819f02007-05-06 14:49:36 -07001357 struct kmem_cache_node *n;
1358
Mel Gorman54a6eb52008-04-28 02:12:16 -07001359 n = get_node(s, zone_to_nid(zone));
Christoph Lameter81819f02007-05-06 14:49:36 -07001360
Mel Gorman54a6eb52008-04-28 02:12:16 -07001361 if (n && cpuset_zone_allowed_hardwall(zone, flags) &&
Christoph Lametere95eed52007-05-06 14:49:44 -07001362 n->nr_partial > MIN_PARTIAL) {
Christoph Lameter81819f02007-05-06 14:49:36 -07001363 page = get_partial_node(n);
1364 if (page)
1365 return page;
1366 }
1367 }
1368#endif
1369 return NULL;
1370}
1371
1372/*
1373 * Get a partial page, lock it and return it.
1374 */
1375static struct page *get_partial(struct kmem_cache *s, gfp_t flags, int node)
1376{
1377 struct page *page;
1378 int searchnode = (node == -1) ? numa_node_id() : node;
1379
1380 page = get_partial_node(get_node(s, searchnode));
1381 if (page || (flags & __GFP_THISNODE))
1382 return page;
1383
1384 return get_any_partial(s, flags);
1385}
1386
1387/*
1388 * Move a page back to the lists.
1389 *
1390 * Must be called with the slab lock held.
1391 *
1392 * On exit the slab lock will have been dropped.
1393 */
Christoph Lameter7c2e1322008-01-07 23:20:27 -08001394static void unfreeze_slab(struct kmem_cache *s, struct page *page, int tail)
Christoph Lameter81819f02007-05-06 14:49:36 -07001395{
Christoph Lametere95eed52007-05-06 14:49:44 -07001396 struct kmem_cache_node *n = get_node(s, page_to_nid(page));
Christoph Lameter8ff12cf2008-02-07 17:47:41 -08001397 struct kmem_cache_cpu *c = get_cpu_slab(s, smp_processor_id());
Christoph Lametere95eed52007-05-06 14:49:44 -07001398
Christoph Lameter4b6f0752007-05-16 22:10:53 -07001399 ClearSlabFrozen(page);
Christoph Lameter81819f02007-05-06 14:49:36 -07001400 if (page->inuse) {
Christoph Lametere95eed52007-05-06 14:49:44 -07001401
Christoph Lametera973e9d2008-03-01 13:40:44 -08001402 if (page->freelist) {
Christoph Lameter7c2e1322008-01-07 23:20:27 -08001403 add_partial(n, page, tail);
Christoph Lameter8ff12cf2008-02-07 17:47:41 -08001404 stat(c, tail ? DEACTIVATE_TO_TAIL : DEACTIVATE_TO_HEAD);
1405 } else {
1406 stat(c, DEACTIVATE_FULL);
1407 if (SlabDebug(page) && (s->flags & SLAB_STORE_USER))
1408 add_full(n, page);
1409 }
Christoph Lameter81819f02007-05-06 14:49:36 -07001410 slab_unlock(page);
1411 } else {
Christoph Lameter8ff12cf2008-02-07 17:47:41 -08001412 stat(c, DEACTIVATE_EMPTY);
Christoph Lametere95eed52007-05-06 14:49:44 -07001413 if (n->nr_partial < MIN_PARTIAL) {
1414 /*
Christoph Lameter672bba32007-05-09 02:32:39 -07001415 * Adding an empty slab to the partial slabs in order
1416 * to avoid page allocator overhead. This slab needs
1417 * to come after the other slabs with objects in
Christoph Lameter6446faa2008-02-15 23:45:26 -08001418 * so that the others get filled first. That way the
1419 * size of the partial list stays small.
1420 *
1421 * kmem_cache_shrink can reclaim any empty slabs from the
1422 * partial list.
Christoph Lametere95eed52007-05-06 14:49:44 -07001423 */
Christoph Lameter7c2e1322008-01-07 23:20:27 -08001424 add_partial(n, page, 1);
Christoph Lametere95eed52007-05-06 14:49:44 -07001425 slab_unlock(page);
1426 } else {
1427 slab_unlock(page);
Christoph Lameter8ff12cf2008-02-07 17:47:41 -08001428 stat(get_cpu_slab(s, raw_smp_processor_id()), FREE_SLAB);
Christoph Lametere95eed52007-05-06 14:49:44 -07001429 discard_slab(s, page);
1430 }
Christoph Lameter81819f02007-05-06 14:49:36 -07001431 }
1432}
1433
1434/*
1435 * Remove the cpu slab
1436 */
Christoph Lameterdfb4f092007-10-16 01:26:05 -07001437static void deactivate_slab(struct kmem_cache *s, struct kmem_cache_cpu *c)
Christoph Lameter81819f02007-05-06 14:49:36 -07001438{
Christoph Lameterdfb4f092007-10-16 01:26:05 -07001439 struct page *page = c->page;
Christoph Lameter7c2e1322008-01-07 23:20:27 -08001440 int tail = 1;
Christoph Lameter8ff12cf2008-02-07 17:47:41 -08001441
Christoph Lameterb773ad72008-03-04 11:10:17 -08001442 if (page->freelist)
Christoph Lameter8ff12cf2008-02-07 17:47:41 -08001443 stat(c, DEACTIVATE_REMOTE_FREES);
Christoph Lameter894b8782007-05-10 03:15:16 -07001444 /*
Christoph Lameter6446faa2008-02-15 23:45:26 -08001445 * Merge cpu freelist into slab freelist. Typically we get here
Christoph Lameter894b8782007-05-10 03:15:16 -07001446 * because both freelists are empty. So this is unlikely
1447 * to occur.
1448 */
Christoph Lametera973e9d2008-03-01 13:40:44 -08001449 while (unlikely(c->freelist)) {
Christoph Lameter894b8782007-05-10 03:15:16 -07001450 void **object;
1451
Christoph Lameter7c2e1322008-01-07 23:20:27 -08001452 tail = 0; /* Hot objects. Put the slab first */
1453
Christoph Lameter894b8782007-05-10 03:15:16 -07001454 /* Retrieve object from cpu_freelist */
Christoph Lameterdfb4f092007-10-16 01:26:05 -07001455 object = c->freelist;
Christoph Lameterb3fba8d2007-10-16 01:26:06 -07001456 c->freelist = c->freelist[c->offset];
Christoph Lameter894b8782007-05-10 03:15:16 -07001457
1458 /* And put onto the regular freelist */
Christoph Lameterb3fba8d2007-10-16 01:26:06 -07001459 object[c->offset] = page->freelist;
Christoph Lameter894b8782007-05-10 03:15:16 -07001460 page->freelist = object;
1461 page->inuse--;
1462 }
Christoph Lameterdfb4f092007-10-16 01:26:05 -07001463 c->page = NULL;
Christoph Lameter7c2e1322008-01-07 23:20:27 -08001464 unfreeze_slab(s, page, tail);
Christoph Lameter81819f02007-05-06 14:49:36 -07001465}
1466
Christoph Lameterdfb4f092007-10-16 01:26:05 -07001467static inline void flush_slab(struct kmem_cache *s, struct kmem_cache_cpu *c)
Christoph Lameter81819f02007-05-06 14:49:36 -07001468{
Christoph Lameter8ff12cf2008-02-07 17:47:41 -08001469 stat(c, CPUSLAB_FLUSH);
Christoph Lameterdfb4f092007-10-16 01:26:05 -07001470 slab_lock(c->page);
1471 deactivate_slab(s, c);
Christoph Lameter81819f02007-05-06 14:49:36 -07001472}
1473
1474/*
1475 * Flush cpu slab.
Christoph Lameter6446faa2008-02-15 23:45:26 -08001476 *
Christoph Lameter81819f02007-05-06 14:49:36 -07001477 * Called from IPI handler with interrupts disabled.
1478 */
Christoph Lameter0c710012007-07-17 04:03:24 -07001479static inline void __flush_cpu_slab(struct kmem_cache *s, int cpu)
Christoph Lameter81819f02007-05-06 14:49:36 -07001480{
Christoph Lameterdfb4f092007-10-16 01:26:05 -07001481 struct kmem_cache_cpu *c = get_cpu_slab(s, cpu);
Christoph Lameter81819f02007-05-06 14:49:36 -07001482
Christoph Lameterdfb4f092007-10-16 01:26:05 -07001483 if (likely(c && c->page))
1484 flush_slab(s, c);
Christoph Lameter81819f02007-05-06 14:49:36 -07001485}
1486
1487static void flush_cpu_slab(void *d)
1488{
1489 struct kmem_cache *s = d;
Christoph Lameter81819f02007-05-06 14:49:36 -07001490
Christoph Lameterdfb4f092007-10-16 01:26:05 -07001491 __flush_cpu_slab(s, smp_processor_id());
Christoph Lameter81819f02007-05-06 14:49:36 -07001492}
1493
1494static void flush_all(struct kmem_cache *s)
1495{
1496#ifdef CONFIG_SMP
1497 on_each_cpu(flush_cpu_slab, s, 1, 1);
1498#else
1499 unsigned long flags;
1500
1501 local_irq_save(flags);
1502 flush_cpu_slab(s);
1503 local_irq_restore(flags);
1504#endif
1505}
1506
1507/*
Christoph Lameterdfb4f092007-10-16 01:26:05 -07001508 * Check if the objects in a per cpu structure fit numa
1509 * locality expectations.
1510 */
1511static inline int node_match(struct kmem_cache_cpu *c, int node)
1512{
1513#ifdef CONFIG_NUMA
1514 if (node != -1 && c->node != node)
1515 return 0;
1516#endif
1517 return 1;
1518}
1519
1520/*
Christoph Lameter894b8782007-05-10 03:15:16 -07001521 * Slow path. The lockless freelist is empty or we need to perform
1522 * debugging duties.
Christoph Lameter81819f02007-05-06 14:49:36 -07001523 *
Christoph Lameter894b8782007-05-10 03:15:16 -07001524 * Interrupts are disabled.
Christoph Lameter81819f02007-05-06 14:49:36 -07001525 *
Christoph Lameter894b8782007-05-10 03:15:16 -07001526 * Processing is still very fast if new objects have been freed to the
1527 * regular freelist. In that case we simply take over the regular freelist
1528 * as the lockless freelist and zap the regular freelist.
Christoph Lameter81819f02007-05-06 14:49:36 -07001529 *
Christoph Lameter894b8782007-05-10 03:15:16 -07001530 * If that is not working then we fall back to the partial lists. We take the
1531 * first element of the freelist as the object to allocate now and move the
1532 * rest of the freelist to the lockless freelist.
1533 *
1534 * And if we were unable to get a new slab from the partial slab lists then
Christoph Lameter6446faa2008-02-15 23:45:26 -08001535 * we need to allocate a new slab. This is the slowest path since it involves
1536 * a call to the page allocator and the setup of a new slab.
Christoph Lameter81819f02007-05-06 14:49:36 -07001537 */
Christoph Lameter894b8782007-05-10 03:15:16 -07001538static void *__slab_alloc(struct kmem_cache *s,
Christoph Lameterdfb4f092007-10-16 01:26:05 -07001539 gfp_t gfpflags, int node, void *addr, struct kmem_cache_cpu *c)
Christoph Lameter81819f02007-05-06 14:49:36 -07001540{
Christoph Lameter81819f02007-05-06 14:49:36 -07001541 void **object;
Christoph Lameterdfb4f092007-10-16 01:26:05 -07001542 struct page *new;
Christoph Lameter81819f02007-05-06 14:49:36 -07001543
Linus Torvaldse72e9c22008-03-27 20:56:33 -07001544 /* We handle __GFP_ZERO in the caller */
1545 gfpflags &= ~__GFP_ZERO;
1546
Christoph Lameterdfb4f092007-10-16 01:26:05 -07001547 if (!c->page)
Christoph Lameter81819f02007-05-06 14:49:36 -07001548 goto new_slab;
1549
Christoph Lameterdfb4f092007-10-16 01:26:05 -07001550 slab_lock(c->page);
1551 if (unlikely(!node_match(c, node)))
Christoph Lameter81819f02007-05-06 14:49:36 -07001552 goto another_slab;
Christoph Lameter6446faa2008-02-15 23:45:26 -08001553
Christoph Lameter8ff12cf2008-02-07 17:47:41 -08001554 stat(c, ALLOC_REFILL);
Christoph Lameter6446faa2008-02-15 23:45:26 -08001555
Christoph Lameter894b8782007-05-10 03:15:16 -07001556load_freelist:
Christoph Lameterdfb4f092007-10-16 01:26:05 -07001557 object = c->page->freelist;
Christoph Lametera973e9d2008-03-01 13:40:44 -08001558 if (unlikely(!object))
Christoph Lameter81819f02007-05-06 14:49:36 -07001559 goto another_slab;
Christoph Lameterdfb4f092007-10-16 01:26:05 -07001560 if (unlikely(SlabDebug(c->page)))
Christoph Lameter81819f02007-05-06 14:49:36 -07001561 goto debug;
1562
Christoph Lameterb3fba8d2007-10-16 01:26:06 -07001563 c->freelist = object[c->offset];
Christoph Lameter39b26462008-04-14 19:11:30 +03001564 c->page->inuse = c->page->objects;
Christoph Lametera973e9d2008-03-01 13:40:44 -08001565 c->page->freelist = NULL;
Christoph Lameterdfb4f092007-10-16 01:26:05 -07001566 c->node = page_to_nid(c->page);
Christoph Lameter1f842602008-01-07 23:20:30 -08001567unlock_out:
Christoph Lameterdfb4f092007-10-16 01:26:05 -07001568 slab_unlock(c->page);
Christoph Lameter8ff12cf2008-02-07 17:47:41 -08001569 stat(c, ALLOC_SLOWPATH);
Christoph Lameter81819f02007-05-06 14:49:36 -07001570 return object;
1571
1572another_slab:
Christoph Lameterdfb4f092007-10-16 01:26:05 -07001573 deactivate_slab(s, c);
Christoph Lameter81819f02007-05-06 14:49:36 -07001574
1575new_slab:
Christoph Lameterdfb4f092007-10-16 01:26:05 -07001576 new = get_partial(s, gfpflags, node);
1577 if (new) {
1578 c->page = new;
Christoph Lameter8ff12cf2008-02-07 17:47:41 -08001579 stat(c, ALLOC_FROM_PARTIAL);
Christoph Lameter894b8782007-05-10 03:15:16 -07001580 goto load_freelist;
Christoph Lameter81819f02007-05-06 14:49:36 -07001581 }
1582
Christoph Lameterb811c202007-10-16 23:25:51 -07001583 if (gfpflags & __GFP_WAIT)
1584 local_irq_enable();
1585
Christoph Lameterdfb4f092007-10-16 01:26:05 -07001586 new = new_slab(s, gfpflags, node);
Christoph Lameterb811c202007-10-16 23:25:51 -07001587
1588 if (gfpflags & __GFP_WAIT)
1589 local_irq_disable();
1590
Christoph Lameterdfb4f092007-10-16 01:26:05 -07001591 if (new) {
1592 c = get_cpu_slab(s, smp_processor_id());
Christoph Lameter8ff12cf2008-02-07 17:47:41 -08001593 stat(c, ALLOC_SLAB);
Christoph Lameter05aa3452007-11-05 11:31:58 -08001594 if (c->page)
Christoph Lameterdfb4f092007-10-16 01:26:05 -07001595 flush_slab(s, c);
Christoph Lameterdfb4f092007-10-16 01:26:05 -07001596 slab_lock(new);
1597 SetSlabFrozen(new);
1598 c->page = new;
Christoph Lameter4b6f0752007-05-16 22:10:53 -07001599 goto load_freelist;
Christoph Lameter81819f02007-05-06 14:49:36 -07001600 }
Christoph Lameter71c7a062008-02-14 14:28:01 -08001601 return NULL;
Christoph Lameter81819f02007-05-06 14:49:36 -07001602debug:
Christoph Lameterdfb4f092007-10-16 01:26:05 -07001603 if (!alloc_debug_processing(s, c->page, object, addr))
Christoph Lameter81819f02007-05-06 14:49:36 -07001604 goto another_slab;
Christoph Lameter894b8782007-05-10 03:15:16 -07001605
Christoph Lameterdfb4f092007-10-16 01:26:05 -07001606 c->page->inuse++;
Christoph Lameterb3fba8d2007-10-16 01:26:06 -07001607 c->page->freelist = object[c->offset];
Christoph Lameteree3c72a2007-10-16 01:26:07 -07001608 c->node = -1;
Christoph Lameter1f842602008-01-07 23:20:30 -08001609 goto unlock_out;
Christoph Lameter894b8782007-05-10 03:15:16 -07001610}
1611
1612/*
1613 * Inlined fastpath so that allocation functions (kmalloc, kmem_cache_alloc)
1614 * have the fastpath folded into their functions. So no function call
1615 * overhead for requests that can be satisfied on the fastpath.
1616 *
1617 * The fastpath works by first checking if the lockless freelist can be used.
1618 * If not then __slab_alloc is called for slow processing.
1619 *
1620 * Otherwise we can simply pick the next object from the lockless free list.
1621 */
Pekka Enberg06428782008-01-07 23:20:27 -08001622static __always_inline void *slab_alloc(struct kmem_cache *s,
Christoph Lameterce15fea2007-07-17 04:03:28 -07001623 gfp_t gfpflags, int node, void *addr)
Christoph Lameter894b8782007-05-10 03:15:16 -07001624{
Christoph Lameter894b8782007-05-10 03:15:16 -07001625 void **object;
Christoph Lameterdfb4f092007-10-16 01:26:05 -07001626 struct kmem_cache_cpu *c;
Christoph Lameter1f842602008-01-07 23:20:30 -08001627 unsigned long flags;
1628
Christoph Lameter894b8782007-05-10 03:15:16 -07001629 local_irq_save(flags);
Christoph Lameterdfb4f092007-10-16 01:26:05 -07001630 c = get_cpu_slab(s, smp_processor_id());
Christoph Lametera973e9d2008-03-01 13:40:44 -08001631 if (unlikely(!c->freelist || !node_match(c, node)))
Christoph Lameter894b8782007-05-10 03:15:16 -07001632
Christoph Lameterdfb4f092007-10-16 01:26:05 -07001633 object = __slab_alloc(s, gfpflags, node, addr, c);
Christoph Lameter894b8782007-05-10 03:15:16 -07001634
1635 else {
Christoph Lameterdfb4f092007-10-16 01:26:05 -07001636 object = c->freelist;
Christoph Lameterb3fba8d2007-10-16 01:26:06 -07001637 c->freelist = object[c->offset];
Christoph Lameter8ff12cf2008-02-07 17:47:41 -08001638 stat(c, ALLOC_FASTPATH);
Christoph Lameter894b8782007-05-10 03:15:16 -07001639 }
1640 local_irq_restore(flags);
Christoph Lameterd07dbea2007-07-17 04:03:23 -07001641
1642 if (unlikely((gfpflags & __GFP_ZERO) && object))
Christoph Lameter42a9fdb2007-10-16 01:26:09 -07001643 memset(object, 0, c->objsize);
Christoph Lameterd07dbea2007-07-17 04:03:23 -07001644
Christoph Lameter894b8782007-05-10 03:15:16 -07001645 return object;
Christoph Lameter81819f02007-05-06 14:49:36 -07001646}
1647
1648void *kmem_cache_alloc(struct kmem_cache *s, gfp_t gfpflags)
1649{
Christoph Lameterce15fea2007-07-17 04:03:28 -07001650 return slab_alloc(s, gfpflags, -1, __builtin_return_address(0));
Christoph Lameter81819f02007-05-06 14:49:36 -07001651}
1652EXPORT_SYMBOL(kmem_cache_alloc);
1653
1654#ifdef CONFIG_NUMA
1655void *kmem_cache_alloc_node(struct kmem_cache *s, gfp_t gfpflags, int node)
1656{
Christoph Lameterce15fea2007-07-17 04:03:28 -07001657 return slab_alloc(s, gfpflags, node, __builtin_return_address(0));
Christoph Lameter81819f02007-05-06 14:49:36 -07001658}
1659EXPORT_SYMBOL(kmem_cache_alloc_node);
1660#endif
1661
1662/*
Christoph Lameter894b8782007-05-10 03:15:16 -07001663 * Slow patch handling. This may still be called frequently since objects
1664 * have a longer lifetime than the cpu slabs in most processing loads.
Christoph Lameter81819f02007-05-06 14:49:36 -07001665 *
Christoph Lameter894b8782007-05-10 03:15:16 -07001666 * So we still attempt to reduce cache line usage. Just take the slab
1667 * lock and free the item. If there is no additional partial page
1668 * handling required then we can return immediately.
Christoph Lameter81819f02007-05-06 14:49:36 -07001669 */
Christoph Lameter894b8782007-05-10 03:15:16 -07001670static void __slab_free(struct kmem_cache *s, struct page *page,
Christoph Lameterb3fba8d2007-10-16 01:26:06 -07001671 void *x, void *addr, unsigned int offset)
Christoph Lameter81819f02007-05-06 14:49:36 -07001672{
1673 void *prior;
1674 void **object = (void *)x;
Christoph Lameter8ff12cf2008-02-07 17:47:41 -08001675 struct kmem_cache_cpu *c;
Christoph Lameter81819f02007-05-06 14:49:36 -07001676
Christoph Lameter8ff12cf2008-02-07 17:47:41 -08001677 c = get_cpu_slab(s, raw_smp_processor_id());
1678 stat(c, FREE_SLOWPATH);
Christoph Lameter81819f02007-05-06 14:49:36 -07001679 slab_lock(page);
1680
Christoph Lameter35e5d7e2007-05-09 02:32:42 -07001681 if (unlikely(SlabDebug(page)))
Christoph Lameter81819f02007-05-06 14:49:36 -07001682 goto debug;
Christoph Lameter6446faa2008-02-15 23:45:26 -08001683
Christoph Lameter81819f02007-05-06 14:49:36 -07001684checks_ok:
Christoph Lameterb3fba8d2007-10-16 01:26:06 -07001685 prior = object[offset] = page->freelist;
Christoph Lameter81819f02007-05-06 14:49:36 -07001686 page->freelist = object;
1687 page->inuse--;
1688
Christoph Lameter8ff12cf2008-02-07 17:47:41 -08001689 if (unlikely(SlabFrozen(page))) {
1690 stat(c, FREE_FROZEN);
Christoph Lameter81819f02007-05-06 14:49:36 -07001691 goto out_unlock;
Christoph Lameter8ff12cf2008-02-07 17:47:41 -08001692 }
Christoph Lameter81819f02007-05-06 14:49:36 -07001693
1694 if (unlikely(!page->inuse))
1695 goto slab_empty;
1696
1697 /*
Christoph Lameter6446faa2008-02-15 23:45:26 -08001698 * Objects left in the slab. If it was not on the partial list before
Christoph Lameter81819f02007-05-06 14:49:36 -07001699 * then add it.
1700 */
Christoph Lametera973e9d2008-03-01 13:40:44 -08001701 if (unlikely(!prior)) {
Christoph Lameter7c2e1322008-01-07 23:20:27 -08001702 add_partial(get_node(s, page_to_nid(page)), page, 1);
Christoph Lameter8ff12cf2008-02-07 17:47:41 -08001703 stat(c, FREE_ADD_PARTIAL);
1704 }
Christoph Lameter81819f02007-05-06 14:49:36 -07001705
1706out_unlock:
1707 slab_unlock(page);
Christoph Lameter81819f02007-05-06 14:49:36 -07001708 return;
1709
1710slab_empty:
Christoph Lametera973e9d2008-03-01 13:40:44 -08001711 if (prior) {
Christoph Lameter81819f02007-05-06 14:49:36 -07001712 /*
Christoph Lameter672bba32007-05-09 02:32:39 -07001713 * Slab still on the partial list.
Christoph Lameter81819f02007-05-06 14:49:36 -07001714 */
1715 remove_partial(s, page);
Christoph Lameter8ff12cf2008-02-07 17:47:41 -08001716 stat(c, FREE_REMOVE_PARTIAL);
1717 }
Christoph Lameter81819f02007-05-06 14:49:36 -07001718 slab_unlock(page);
Christoph Lameter8ff12cf2008-02-07 17:47:41 -08001719 stat(c, FREE_SLAB);
Christoph Lameter81819f02007-05-06 14:49:36 -07001720 discard_slab(s, page);
Christoph Lameter81819f02007-05-06 14:49:36 -07001721 return;
1722
1723debug:
Christoph Lameter3ec09742007-05-16 22:11:00 -07001724 if (!free_debug_processing(s, page, x, addr))
Christoph Lameter77c5e2d2007-05-06 14:49:42 -07001725 goto out_unlock;
Christoph Lameter77c5e2d2007-05-06 14:49:42 -07001726 goto checks_ok;
Christoph Lameter81819f02007-05-06 14:49:36 -07001727}
1728
Christoph Lameter894b8782007-05-10 03:15:16 -07001729/*
1730 * Fastpath with forced inlining to produce a kfree and kmem_cache_free that
1731 * can perform fastpath freeing without additional function calls.
1732 *
1733 * The fastpath is only possible if we are freeing to the current cpu slab
1734 * of this processor. This typically the case if we have just allocated
1735 * the item before.
1736 *
1737 * If fastpath is not possible then fall back to __slab_free where we deal
1738 * with all sorts of special processing.
1739 */
Pekka Enberg06428782008-01-07 23:20:27 -08001740static __always_inline void slab_free(struct kmem_cache *s,
Christoph Lameter894b8782007-05-10 03:15:16 -07001741 struct page *page, void *x, void *addr)
1742{
1743 void **object = (void *)x;
Christoph Lameterdfb4f092007-10-16 01:26:05 -07001744 struct kmem_cache_cpu *c;
Christoph Lameter1f842602008-01-07 23:20:30 -08001745 unsigned long flags;
1746
Christoph Lameter894b8782007-05-10 03:15:16 -07001747 local_irq_save(flags);
Christoph Lameterdfb4f092007-10-16 01:26:05 -07001748 c = get_cpu_slab(s, smp_processor_id());
Christoph Lameter27d9e4e2008-02-15 23:45:25 -08001749 debug_check_no_locks_freed(object, c->objsize);
Christoph Lameteree3c72a2007-10-16 01:26:07 -07001750 if (likely(page == c->page && c->node >= 0)) {
Christoph Lameterb3fba8d2007-10-16 01:26:06 -07001751 object[c->offset] = c->freelist;
Christoph Lameterdfb4f092007-10-16 01:26:05 -07001752 c->freelist = object;
Christoph Lameter8ff12cf2008-02-07 17:47:41 -08001753 stat(c, FREE_FASTPATH);
Christoph Lameter894b8782007-05-10 03:15:16 -07001754 } else
Christoph Lameterb3fba8d2007-10-16 01:26:06 -07001755 __slab_free(s, page, x, addr, c->offset);
Christoph Lameter894b8782007-05-10 03:15:16 -07001756
1757 local_irq_restore(flags);
1758}
1759
Christoph Lameter81819f02007-05-06 14:49:36 -07001760void kmem_cache_free(struct kmem_cache *s, void *x)
1761{
Christoph Lameter77c5e2d2007-05-06 14:49:42 -07001762 struct page *page;
Christoph Lameter81819f02007-05-06 14:49:36 -07001763
Christoph Lameterb49af682007-05-06 14:49:41 -07001764 page = virt_to_head_page(x);
Christoph Lameter81819f02007-05-06 14:49:36 -07001765
Christoph Lameter77c5e2d2007-05-06 14:49:42 -07001766 slab_free(s, page, x, __builtin_return_address(0));
Christoph Lameter81819f02007-05-06 14:49:36 -07001767}
1768EXPORT_SYMBOL(kmem_cache_free);
1769
1770/* Figure out on which slab object the object resides */
1771static struct page *get_object_page(const void *x)
1772{
Christoph Lameterb49af682007-05-06 14:49:41 -07001773 struct page *page = virt_to_head_page(x);
Christoph Lameter81819f02007-05-06 14:49:36 -07001774
1775 if (!PageSlab(page))
1776 return NULL;
1777
1778 return page;
1779}
1780
1781/*
Christoph Lameter672bba32007-05-09 02:32:39 -07001782 * Object placement in a slab is made very easy because we always start at
1783 * offset 0. If we tune the size of the object to the alignment then we can
1784 * get the required alignment by putting one properly sized object after
1785 * another.
Christoph Lameter81819f02007-05-06 14:49:36 -07001786 *
1787 * Notice that the allocation order determines the sizes of the per cpu
1788 * caches. Each processor has always one slab available for allocations.
1789 * Increasing the allocation order reduces the number of times that slabs
Christoph Lameter672bba32007-05-09 02:32:39 -07001790 * must be moved on and off the partial lists and is therefore a factor in
Christoph Lameter81819f02007-05-06 14:49:36 -07001791 * locking overhead.
Christoph Lameter81819f02007-05-06 14:49:36 -07001792 */
1793
1794/*
1795 * Mininum / Maximum order of slab pages. This influences locking overhead
1796 * and slab fragmentation. A higher order reduces the number of partial slabs
1797 * and increases the number of allocations possible without having to
1798 * take the list_lock.
1799 */
1800static int slub_min_order;
Christoph Lameter114e9e82008-04-14 19:11:41 +03001801static int slub_max_order = PAGE_ALLOC_COSTLY_ORDER;
Christoph Lameter9b2cd502008-04-14 19:11:41 +03001802static int slub_min_objects;
Christoph Lameter81819f02007-05-06 14:49:36 -07001803
1804/*
1805 * Merge control. If this is set then no merging of slab caches will occur.
Christoph Lameter672bba32007-05-09 02:32:39 -07001806 * (Could be removed. This was introduced to pacify the merge skeptics.)
Christoph Lameter81819f02007-05-06 14:49:36 -07001807 */
1808static int slub_nomerge;
1809
1810/*
Christoph Lameter81819f02007-05-06 14:49:36 -07001811 * Calculate the order of allocation given an slab object size.
1812 *
Christoph Lameter672bba32007-05-09 02:32:39 -07001813 * The order of allocation has significant impact on performance and other
1814 * system components. Generally order 0 allocations should be preferred since
1815 * order 0 does not cause fragmentation in the page allocator. Larger objects
1816 * be problematic to put into order 0 slabs because there may be too much
Christoph Lameterc124f5b2008-04-14 19:13:29 +03001817 * unused space left. We go to a higher order if more than 1/16th of the slab
Christoph Lameter672bba32007-05-09 02:32:39 -07001818 * would be wasted.
Christoph Lameter81819f02007-05-06 14:49:36 -07001819 *
Christoph Lameter672bba32007-05-09 02:32:39 -07001820 * In order to reach satisfactory performance we must ensure that a minimum
1821 * number of objects is in one slab. Otherwise we may generate too much
1822 * activity on the partial lists which requires taking the list_lock. This is
1823 * less a concern for large slabs though which are rarely used.
Christoph Lameter81819f02007-05-06 14:49:36 -07001824 *
Christoph Lameter672bba32007-05-09 02:32:39 -07001825 * slub_max_order specifies the order where we begin to stop considering the
1826 * number of objects in a slab as critical. If we reach slub_max_order then
1827 * we try to keep the page order as low as possible. So we accept more waste
1828 * of space in favor of a small page order.
1829 *
1830 * Higher order allocations also allow the placement of more objects in a
1831 * slab and thereby reduce object handling overhead. If the user has
1832 * requested a higher mininum order then we start with that one instead of
1833 * the smallest order which will fit the object.
Christoph Lameter81819f02007-05-06 14:49:36 -07001834 */
Christoph Lameter5e6d4442007-05-09 02:32:46 -07001835static inline int slab_order(int size, int min_objects,
1836 int max_order, int fract_leftover)
Christoph Lameter81819f02007-05-06 14:49:36 -07001837{
1838 int order;
1839 int rem;
Christoph Lameter6300ea72007-07-17 04:03:20 -07001840 int min_order = slub_min_order;
Christoph Lameter81819f02007-05-06 14:49:36 -07001841
Christoph Lameter39b26462008-04-14 19:11:30 +03001842 if ((PAGE_SIZE << min_order) / size > 65535)
1843 return get_order(size * 65535) - 1;
1844
Christoph Lameter6300ea72007-07-17 04:03:20 -07001845 for (order = max(min_order,
Christoph Lameter5e6d4442007-05-09 02:32:46 -07001846 fls(min_objects * size - 1) - PAGE_SHIFT);
1847 order <= max_order; order++) {
1848
Christoph Lameter81819f02007-05-06 14:49:36 -07001849 unsigned long slab_size = PAGE_SIZE << order;
1850
Christoph Lameter5e6d4442007-05-09 02:32:46 -07001851 if (slab_size < min_objects * size)
Christoph Lameter81819f02007-05-06 14:49:36 -07001852 continue;
1853
Christoph Lameter81819f02007-05-06 14:49:36 -07001854 rem = slab_size % size;
1855
Christoph Lameter5e6d4442007-05-09 02:32:46 -07001856 if (rem <= slab_size / fract_leftover)
Christoph Lameter81819f02007-05-06 14:49:36 -07001857 break;
1858
1859 }
Christoph Lameter672bba32007-05-09 02:32:39 -07001860
Christoph Lameter81819f02007-05-06 14:49:36 -07001861 return order;
1862}
1863
Christoph Lameter5e6d4442007-05-09 02:32:46 -07001864static inline int calculate_order(int size)
1865{
1866 int order;
1867 int min_objects;
1868 int fraction;
1869
1870 /*
1871 * Attempt to find best configuration for a slab. This
1872 * works by first attempting to generate a layout with
1873 * the best configuration and backing off gradually.
1874 *
1875 * First we reduce the acceptable waste in a slab. Then
1876 * we reduce the minimum objects required in a slab.
1877 */
1878 min_objects = slub_min_objects;
Christoph Lameter9b2cd502008-04-14 19:11:41 +03001879 if (!min_objects)
1880 min_objects = 4 * (fls(nr_cpu_ids) + 1);
Christoph Lameter5e6d4442007-05-09 02:32:46 -07001881 while (min_objects > 1) {
Christoph Lameterc124f5b2008-04-14 19:13:29 +03001882 fraction = 16;
Christoph Lameter5e6d4442007-05-09 02:32:46 -07001883 while (fraction >= 4) {
1884 order = slab_order(size, min_objects,
1885 slub_max_order, fraction);
1886 if (order <= slub_max_order)
1887 return order;
1888 fraction /= 2;
1889 }
1890 min_objects /= 2;
1891 }
1892
1893 /*
1894 * We were unable to place multiple objects in a slab. Now
1895 * lets see if we can place a single object there.
1896 */
1897 order = slab_order(size, 1, slub_max_order, 1);
1898 if (order <= slub_max_order)
1899 return order;
1900
1901 /*
1902 * Doh this slab cannot be placed using slub_max_order.
1903 */
1904 order = slab_order(size, 1, MAX_ORDER, 1);
1905 if (order <= MAX_ORDER)
1906 return order;
1907 return -ENOSYS;
1908}
1909
Christoph Lameter81819f02007-05-06 14:49:36 -07001910/*
Christoph Lameter672bba32007-05-09 02:32:39 -07001911 * Figure out what the alignment of the objects will be.
Christoph Lameter81819f02007-05-06 14:49:36 -07001912 */
1913static unsigned long calculate_alignment(unsigned long flags,
1914 unsigned long align, unsigned long size)
1915{
1916 /*
Christoph Lameter6446faa2008-02-15 23:45:26 -08001917 * If the user wants hardware cache aligned objects then follow that
1918 * suggestion if the object is sufficiently large.
Christoph Lameter81819f02007-05-06 14:49:36 -07001919 *
Christoph Lameter6446faa2008-02-15 23:45:26 -08001920 * The hardware cache alignment cannot override the specified
1921 * alignment though. If that is greater then use it.
Christoph Lameter81819f02007-05-06 14:49:36 -07001922 */
Nick Pigginb6210382008-03-05 14:05:56 -08001923 if (flags & SLAB_HWCACHE_ALIGN) {
1924 unsigned long ralign = cache_line_size();
1925 while (size <= ralign / 2)
1926 ralign /= 2;
1927 align = max(align, ralign);
1928 }
Christoph Lameter81819f02007-05-06 14:49:36 -07001929
1930 if (align < ARCH_SLAB_MINALIGN)
Nick Pigginb6210382008-03-05 14:05:56 -08001931 align = ARCH_SLAB_MINALIGN;
Christoph Lameter81819f02007-05-06 14:49:36 -07001932
1933 return ALIGN(align, sizeof(void *));
1934}
1935
Christoph Lameterdfb4f092007-10-16 01:26:05 -07001936static void init_kmem_cache_cpu(struct kmem_cache *s,
1937 struct kmem_cache_cpu *c)
1938{
1939 c->page = NULL;
Christoph Lametera973e9d2008-03-01 13:40:44 -08001940 c->freelist = NULL;
Christoph Lameterdfb4f092007-10-16 01:26:05 -07001941 c->node = 0;
Christoph Lameter42a9fdb2007-10-16 01:26:09 -07001942 c->offset = s->offset / sizeof(void *);
1943 c->objsize = s->objsize;
Pekka Enberg62f75532008-04-14 18:50:44 +03001944#ifdef CONFIG_SLUB_STATS
1945 memset(c->stat, 0, NR_SLUB_STAT_ITEMS * sizeof(unsigned));
1946#endif
Christoph Lameterdfb4f092007-10-16 01:26:05 -07001947}
1948
Christoph Lameter81819f02007-05-06 14:49:36 -07001949static void init_kmem_cache_node(struct kmem_cache_node *n)
1950{
1951 n->nr_partial = 0;
Christoph Lameter81819f02007-05-06 14:49:36 -07001952 spin_lock_init(&n->list_lock);
1953 INIT_LIST_HEAD(&n->partial);
Christoph Lameter8ab13722007-07-17 04:03:32 -07001954#ifdef CONFIG_SLUB_DEBUG
Christoph Lameter0f389ec2008-04-14 18:53:02 +03001955 atomic_long_set(&n->nr_slabs, 0);
Christoph Lameter643b1132007-05-06 14:49:42 -07001956 INIT_LIST_HEAD(&n->full);
Christoph Lameter8ab13722007-07-17 04:03:32 -07001957#endif
Christoph Lameter81819f02007-05-06 14:49:36 -07001958}
1959
Christoph Lameter4c93c3552007-10-16 01:26:08 -07001960#ifdef CONFIG_SMP
1961/*
1962 * Per cpu array for per cpu structures.
1963 *
1964 * The per cpu array places all kmem_cache_cpu structures from one processor
1965 * close together meaning that it becomes possible that multiple per cpu
1966 * structures are contained in one cacheline. This may be particularly
1967 * beneficial for the kmalloc caches.
1968 *
1969 * A desktop system typically has around 60-80 slabs. With 100 here we are
1970 * likely able to get per cpu structures for all caches from the array defined
1971 * here. We must be able to cover all kmalloc caches during bootstrap.
1972 *
1973 * If the per cpu array is exhausted then fall back to kmalloc
1974 * of individual cachelines. No sharing is possible then.
1975 */
1976#define NR_KMEM_CACHE_CPU 100
1977
1978static DEFINE_PER_CPU(struct kmem_cache_cpu,
1979 kmem_cache_cpu)[NR_KMEM_CACHE_CPU];
1980
1981static DEFINE_PER_CPU(struct kmem_cache_cpu *, kmem_cache_cpu_free);
1982static cpumask_t kmem_cach_cpu_free_init_once = CPU_MASK_NONE;
1983
1984static struct kmem_cache_cpu *alloc_kmem_cache_cpu(struct kmem_cache *s,
1985 int cpu, gfp_t flags)
1986{
1987 struct kmem_cache_cpu *c = per_cpu(kmem_cache_cpu_free, cpu);
1988
1989 if (c)
1990 per_cpu(kmem_cache_cpu_free, cpu) =
1991 (void *)c->freelist;
1992 else {
1993 /* Table overflow: So allocate ourselves */
1994 c = kmalloc_node(
1995 ALIGN(sizeof(struct kmem_cache_cpu), cache_line_size()),
1996 flags, cpu_to_node(cpu));
1997 if (!c)
1998 return NULL;
1999 }
2000
2001 init_kmem_cache_cpu(s, c);
2002 return c;
2003}
2004
2005static void free_kmem_cache_cpu(struct kmem_cache_cpu *c, int cpu)
2006{
2007 if (c < per_cpu(kmem_cache_cpu, cpu) ||
2008 c > per_cpu(kmem_cache_cpu, cpu) + NR_KMEM_CACHE_CPU) {
2009 kfree(c);
2010 return;
2011 }
2012 c->freelist = (void *)per_cpu(kmem_cache_cpu_free, cpu);
2013 per_cpu(kmem_cache_cpu_free, cpu) = c;
2014}
2015
2016static void free_kmem_cache_cpus(struct kmem_cache *s)
2017{
2018 int cpu;
2019
2020 for_each_online_cpu(cpu) {
2021 struct kmem_cache_cpu *c = get_cpu_slab(s, cpu);
2022
2023 if (c) {
2024 s->cpu_slab[cpu] = NULL;
2025 free_kmem_cache_cpu(c, cpu);
2026 }
2027 }
2028}
2029
2030static int alloc_kmem_cache_cpus(struct kmem_cache *s, gfp_t flags)
2031{
2032 int cpu;
2033
2034 for_each_online_cpu(cpu) {
2035 struct kmem_cache_cpu *c = get_cpu_slab(s, cpu);
2036
2037 if (c)
2038 continue;
2039
2040 c = alloc_kmem_cache_cpu(s, cpu, flags);
2041 if (!c) {
2042 free_kmem_cache_cpus(s);
2043 return 0;
2044 }
2045 s->cpu_slab[cpu] = c;
2046 }
2047 return 1;
2048}
2049
2050/*
2051 * Initialize the per cpu array.
2052 */
2053static void init_alloc_cpu_cpu(int cpu)
2054{
2055 int i;
2056
2057 if (cpu_isset(cpu, kmem_cach_cpu_free_init_once))
2058 return;
2059
2060 for (i = NR_KMEM_CACHE_CPU - 1; i >= 0; i--)
2061 free_kmem_cache_cpu(&per_cpu(kmem_cache_cpu, cpu)[i], cpu);
2062
2063 cpu_set(cpu, kmem_cach_cpu_free_init_once);
2064}
2065
2066static void __init init_alloc_cpu(void)
2067{
2068 int cpu;
2069
2070 for_each_online_cpu(cpu)
2071 init_alloc_cpu_cpu(cpu);
2072 }
2073
2074#else
2075static inline void free_kmem_cache_cpus(struct kmem_cache *s) {}
2076static inline void init_alloc_cpu(void) {}
2077
2078static inline int alloc_kmem_cache_cpus(struct kmem_cache *s, gfp_t flags)
2079{
2080 init_kmem_cache_cpu(s, &s->cpu_slab);
2081 return 1;
2082}
2083#endif
2084
Christoph Lameter81819f02007-05-06 14:49:36 -07002085#ifdef CONFIG_NUMA
2086/*
2087 * No kmalloc_node yet so do it by hand. We know that this is the first
2088 * slab on the node for this slabcache. There are no concurrent accesses
2089 * possible.
2090 *
2091 * Note that this function only works on the kmalloc_node_cache
Christoph Lameter4c93c3552007-10-16 01:26:08 -07002092 * when allocating for the kmalloc_node_cache. This is used for bootstrapping
2093 * memory on a fresh node that has no slab structures yet.
Christoph Lameter81819f02007-05-06 14:49:36 -07002094 */
Adrian Bunk1cd7daa2007-10-16 01:24:18 -07002095static struct kmem_cache_node *early_kmem_cache_node_alloc(gfp_t gfpflags,
2096 int node)
Christoph Lameter81819f02007-05-06 14:49:36 -07002097{
2098 struct page *page;
2099 struct kmem_cache_node *n;
rootba84c732008-01-07 23:20:28 -08002100 unsigned long flags;
Christoph Lameter81819f02007-05-06 14:49:36 -07002101
2102 BUG_ON(kmalloc_caches->size < sizeof(struct kmem_cache_node));
2103
Christoph Lametera2f92ee2007-08-22 14:01:57 -07002104 page = new_slab(kmalloc_caches, gfpflags, node);
Christoph Lameter81819f02007-05-06 14:49:36 -07002105
2106 BUG_ON(!page);
Christoph Lametera2f92ee2007-08-22 14:01:57 -07002107 if (page_to_nid(page) != node) {
2108 printk(KERN_ERR "SLUB: Unable to allocate memory from "
2109 "node %d\n", node);
2110 printk(KERN_ERR "SLUB: Allocating a useless per node structure "
2111 "in order to be able to continue\n");
2112 }
2113
Christoph Lameter81819f02007-05-06 14:49:36 -07002114 n = page->freelist;
2115 BUG_ON(!n);
2116 page->freelist = get_freepointer(kmalloc_caches, n);
2117 page->inuse++;
2118 kmalloc_caches->node[node] = n;
Christoph Lameter8ab13722007-07-17 04:03:32 -07002119#ifdef CONFIG_SLUB_DEBUG
Christoph Lameterd45f39c2007-07-17 04:03:21 -07002120 init_object(kmalloc_caches, n, 1);
2121 init_tracking(kmalloc_caches, n);
Christoph Lameter8ab13722007-07-17 04:03:32 -07002122#endif
Christoph Lameter81819f02007-05-06 14:49:36 -07002123 init_kmem_cache_node(n);
Christoph Lameter205ab992008-04-14 19:11:40 +03002124 inc_slabs_node(kmalloc_caches, node, page->objects);
Christoph Lameter6446faa2008-02-15 23:45:26 -08002125
rootba84c732008-01-07 23:20:28 -08002126 /*
2127 * lockdep requires consistent irq usage for each lock
2128 * so even though there cannot be a race this early in
2129 * the boot sequence, we still disable irqs.
2130 */
2131 local_irq_save(flags);
Christoph Lameter7c2e1322008-01-07 23:20:27 -08002132 add_partial(n, page, 0);
rootba84c732008-01-07 23:20:28 -08002133 local_irq_restore(flags);
Christoph Lameter81819f02007-05-06 14:49:36 -07002134 return n;
2135}
2136
2137static void free_kmem_cache_nodes(struct kmem_cache *s)
2138{
2139 int node;
2140
Christoph Lameterf64dc582007-10-16 01:25:33 -07002141 for_each_node_state(node, N_NORMAL_MEMORY) {
Christoph Lameter81819f02007-05-06 14:49:36 -07002142 struct kmem_cache_node *n = s->node[node];
2143 if (n && n != &s->local_node)
2144 kmem_cache_free(kmalloc_caches, n);
2145 s->node[node] = NULL;
2146 }
2147}
2148
2149static int init_kmem_cache_nodes(struct kmem_cache *s, gfp_t gfpflags)
2150{
2151 int node;
2152 int local_node;
2153
2154 if (slab_state >= UP)
2155 local_node = page_to_nid(virt_to_page(s));
2156 else
2157 local_node = 0;
2158
Christoph Lameterf64dc582007-10-16 01:25:33 -07002159 for_each_node_state(node, N_NORMAL_MEMORY) {
Christoph Lameter81819f02007-05-06 14:49:36 -07002160 struct kmem_cache_node *n;
2161
2162 if (local_node == node)
2163 n = &s->local_node;
2164 else {
2165 if (slab_state == DOWN) {
2166 n = early_kmem_cache_node_alloc(gfpflags,
2167 node);
2168 continue;
2169 }
2170 n = kmem_cache_alloc_node(kmalloc_caches,
2171 gfpflags, node);
2172
2173 if (!n) {
2174 free_kmem_cache_nodes(s);
2175 return 0;
2176 }
2177
2178 }
2179 s->node[node] = n;
2180 init_kmem_cache_node(n);
2181 }
2182 return 1;
2183}
2184#else
2185static void free_kmem_cache_nodes(struct kmem_cache *s)
2186{
2187}
2188
2189static int init_kmem_cache_nodes(struct kmem_cache *s, gfp_t gfpflags)
2190{
2191 init_kmem_cache_node(&s->local_node);
2192 return 1;
2193}
2194#endif
2195
2196/*
2197 * calculate_sizes() determines the order and the distribution of data within
2198 * a slab object.
2199 */
Christoph Lameter06b285d2008-04-14 19:11:41 +03002200static int calculate_sizes(struct kmem_cache *s, int forced_order)
Christoph Lameter81819f02007-05-06 14:49:36 -07002201{
2202 unsigned long flags = s->flags;
2203 unsigned long size = s->objsize;
2204 unsigned long align = s->align;
Christoph Lameter834f3d12008-04-14 19:11:31 +03002205 int order;
Christoph Lameter81819f02007-05-06 14:49:36 -07002206
2207 /*
Christoph Lameterd8b42bf2008-02-15 23:45:25 -08002208 * Round up object size to the next word boundary. We can only
2209 * place the free pointer at word boundaries and this determines
2210 * the possible location of the free pointer.
2211 */
2212 size = ALIGN(size, sizeof(void *));
2213
2214#ifdef CONFIG_SLUB_DEBUG
2215 /*
Christoph Lameter81819f02007-05-06 14:49:36 -07002216 * Determine if we can poison the object itself. If the user of
2217 * the slab may touch the object after free or before allocation
2218 * then we should never poison the object itself.
2219 */
2220 if ((flags & SLAB_POISON) && !(flags & SLAB_DESTROY_BY_RCU) &&
Christoph Lameterc59def92007-05-16 22:10:50 -07002221 !s->ctor)
Christoph Lameter81819f02007-05-06 14:49:36 -07002222 s->flags |= __OBJECT_POISON;
2223 else
2224 s->flags &= ~__OBJECT_POISON;
2225
Christoph Lameter81819f02007-05-06 14:49:36 -07002226
2227 /*
Christoph Lameter672bba32007-05-09 02:32:39 -07002228 * If we are Redzoning then check if there is some space between the
Christoph Lameter81819f02007-05-06 14:49:36 -07002229 * end of the object and the free pointer. If not then add an
Christoph Lameter672bba32007-05-09 02:32:39 -07002230 * additional word to have some bytes to store Redzone information.
Christoph Lameter81819f02007-05-06 14:49:36 -07002231 */
2232 if ((flags & SLAB_RED_ZONE) && size == s->objsize)
2233 size += sizeof(void *);
Christoph Lameter41ecc552007-05-09 02:32:44 -07002234#endif
Christoph Lameter81819f02007-05-06 14:49:36 -07002235
2236 /*
Christoph Lameter672bba32007-05-09 02:32:39 -07002237 * With that we have determined the number of bytes in actual use
2238 * by the object. This is the potential offset to the free pointer.
Christoph Lameter81819f02007-05-06 14:49:36 -07002239 */
2240 s->inuse = size;
2241
2242 if (((flags & (SLAB_DESTROY_BY_RCU | SLAB_POISON)) ||
Christoph Lameterc59def92007-05-16 22:10:50 -07002243 s->ctor)) {
Christoph Lameter81819f02007-05-06 14:49:36 -07002244 /*
2245 * Relocate free pointer after the object if it is not
2246 * permitted to overwrite the first word of the object on
2247 * kmem_cache_free.
2248 *
2249 * This is the case if we do RCU, have a constructor or
2250 * destructor or are poisoning the objects.
2251 */
2252 s->offset = size;
2253 size += sizeof(void *);
2254 }
2255
Christoph Lameterc12b3c62007-05-23 13:57:31 -07002256#ifdef CONFIG_SLUB_DEBUG
Christoph Lameter81819f02007-05-06 14:49:36 -07002257 if (flags & SLAB_STORE_USER)
2258 /*
2259 * Need to store information about allocs and frees after
2260 * the object.
2261 */
2262 size += 2 * sizeof(struct track);
2263
Christoph Lameterbe7b3fb2007-05-09 02:32:36 -07002264 if (flags & SLAB_RED_ZONE)
Christoph Lameter81819f02007-05-06 14:49:36 -07002265 /*
2266 * Add some empty padding so that we can catch
2267 * overwrites from earlier objects rather than let
2268 * tracking information or the free pointer be
2269 * corrupted if an user writes before the start
2270 * of the object.
2271 */
2272 size += sizeof(void *);
Christoph Lameter41ecc552007-05-09 02:32:44 -07002273#endif
Christoph Lameter672bba32007-05-09 02:32:39 -07002274
Christoph Lameter81819f02007-05-06 14:49:36 -07002275 /*
2276 * Determine the alignment based on various parameters that the
Christoph Lameter65c02d42007-05-09 02:32:35 -07002277 * user specified and the dynamic determination of cache line size
2278 * on bootup.
Christoph Lameter81819f02007-05-06 14:49:36 -07002279 */
2280 align = calculate_alignment(flags, align, s->objsize);
2281
2282 /*
2283 * SLUB stores one object immediately after another beginning from
2284 * offset 0. In order to align the objects we have to simply size
2285 * each object to conform to the alignment.
2286 */
2287 size = ALIGN(size, align);
2288 s->size = size;
Christoph Lameter06b285d2008-04-14 19:11:41 +03002289 if (forced_order >= 0)
2290 order = forced_order;
2291 else
2292 order = calculate_order(size);
Christoph Lameter81819f02007-05-06 14:49:36 -07002293
Christoph Lameter834f3d12008-04-14 19:11:31 +03002294 if (order < 0)
Christoph Lameter81819f02007-05-06 14:49:36 -07002295 return 0;
2296
Christoph Lameterb7a49f02008-02-14 14:21:32 -08002297 s->allocflags = 0;
Christoph Lameter834f3d12008-04-14 19:11:31 +03002298 if (order)
Christoph Lameterb7a49f02008-02-14 14:21:32 -08002299 s->allocflags |= __GFP_COMP;
2300
2301 if (s->flags & SLAB_CACHE_DMA)
2302 s->allocflags |= SLUB_DMA;
2303
2304 if (s->flags & SLAB_RECLAIM_ACCOUNT)
2305 s->allocflags |= __GFP_RECLAIMABLE;
2306
Christoph Lameter81819f02007-05-06 14:49:36 -07002307 /*
2308 * Determine the number of objects per slab
2309 */
Christoph Lameter834f3d12008-04-14 19:11:31 +03002310 s->oo = oo_make(order, size);
Christoph Lameter65c33762008-04-14 19:11:40 +03002311 s->min = oo_make(get_order(size), size);
Christoph Lameter205ab992008-04-14 19:11:40 +03002312 if (oo_objects(s->oo) > oo_objects(s->max))
2313 s->max = s->oo;
Christoph Lameter81819f02007-05-06 14:49:36 -07002314
Christoph Lameter834f3d12008-04-14 19:11:31 +03002315 return !!oo_objects(s->oo);
Christoph Lameter81819f02007-05-06 14:49:36 -07002316
2317}
2318
Christoph Lameter81819f02007-05-06 14:49:36 -07002319static int kmem_cache_open(struct kmem_cache *s, gfp_t gfpflags,
2320 const char *name, size_t size,
2321 size_t align, unsigned long flags,
Christoph Lameter4ba9b9d2007-10-16 23:25:51 -07002322 void (*ctor)(struct kmem_cache *, void *))
Christoph Lameter81819f02007-05-06 14:49:36 -07002323{
2324 memset(s, 0, kmem_size);
2325 s->name = name;
2326 s->ctor = ctor;
Christoph Lameter81819f02007-05-06 14:49:36 -07002327 s->objsize = size;
Christoph Lameter81819f02007-05-06 14:49:36 -07002328 s->align = align;
Christoph Lameterba0268a2007-09-11 15:24:11 -07002329 s->flags = kmem_cache_flags(size, flags, name, ctor);
Christoph Lameter81819f02007-05-06 14:49:36 -07002330
Christoph Lameter06b285d2008-04-14 19:11:41 +03002331 if (!calculate_sizes(s, -1))
Christoph Lameter81819f02007-05-06 14:49:36 -07002332 goto error;
2333
2334 s->refcount = 1;
2335#ifdef CONFIG_NUMA
Christoph Lameter98246012008-01-07 23:20:26 -08002336 s->remote_node_defrag_ratio = 100;
Christoph Lameter81819f02007-05-06 14:49:36 -07002337#endif
Christoph Lameterdfb4f092007-10-16 01:26:05 -07002338 if (!init_kmem_cache_nodes(s, gfpflags & ~SLUB_DMA))
2339 goto error;
Christoph Lameter81819f02007-05-06 14:49:36 -07002340
Christoph Lameterdfb4f092007-10-16 01:26:05 -07002341 if (alloc_kmem_cache_cpus(s, gfpflags & ~SLUB_DMA))
Christoph Lameter81819f02007-05-06 14:49:36 -07002342 return 1;
Christoph Lameter4c93c3552007-10-16 01:26:08 -07002343 free_kmem_cache_nodes(s);
Christoph Lameter81819f02007-05-06 14:49:36 -07002344error:
2345 if (flags & SLAB_PANIC)
2346 panic("Cannot create slab %s size=%lu realsize=%u "
2347 "order=%u offset=%u flags=%lx\n",
Christoph Lameter834f3d12008-04-14 19:11:31 +03002348 s->name, (unsigned long)size, s->size, oo_order(s->oo),
Christoph Lameter81819f02007-05-06 14:49:36 -07002349 s->offset, flags);
2350 return 0;
2351}
Christoph Lameter81819f02007-05-06 14:49:36 -07002352
2353/*
2354 * Check if a given pointer is valid
2355 */
2356int kmem_ptr_validate(struct kmem_cache *s, const void *object)
2357{
Pekka Enberg06428782008-01-07 23:20:27 -08002358 struct page *page;
Christoph Lameter81819f02007-05-06 14:49:36 -07002359
2360 page = get_object_page(object);
2361
2362 if (!page || s != page->slab)
2363 /* No slab or wrong slab */
2364 return 0;
2365
Christoph Lameterabcd08a2007-05-09 02:32:37 -07002366 if (!check_valid_pointer(s, page, object))
Christoph Lameter81819f02007-05-06 14:49:36 -07002367 return 0;
2368
2369 /*
2370 * We could also check if the object is on the slabs freelist.
2371 * But this would be too expensive and it seems that the main
Christoph Lameter6446faa2008-02-15 23:45:26 -08002372 * purpose of kmem_ptr_valid() is to check if the object belongs
Christoph Lameter81819f02007-05-06 14:49:36 -07002373 * to a certain slab.
2374 */
2375 return 1;
2376}
2377EXPORT_SYMBOL(kmem_ptr_validate);
2378
2379/*
2380 * Determine the size of a slab object
2381 */
2382unsigned int kmem_cache_size(struct kmem_cache *s)
2383{
2384 return s->objsize;
2385}
2386EXPORT_SYMBOL(kmem_cache_size);
2387
2388const char *kmem_cache_name(struct kmem_cache *s)
2389{
2390 return s->name;
2391}
2392EXPORT_SYMBOL(kmem_cache_name);
2393
Christoph Lameter33b12c32008-04-25 12:22:43 -07002394static void list_slab_objects(struct kmem_cache *s, struct page *page,
2395 const char *text)
Christoph Lameter81819f02007-05-06 14:49:36 -07002396{
Christoph Lameter33b12c32008-04-25 12:22:43 -07002397#ifdef CONFIG_SLUB_DEBUG
2398 void *addr = page_address(page);
2399 void *p;
2400 DECLARE_BITMAP(map, page->objects);
2401
2402 bitmap_zero(map, page->objects);
2403 slab_err(s, page, "%s", text);
2404 slab_lock(page);
2405 for_each_free_object(p, s, page->freelist)
2406 set_bit(slab_index(p, s, addr), map);
2407
2408 for_each_object(p, s, addr, page->objects) {
2409
2410 if (!test_bit(slab_index(p, s, addr), map)) {
2411 printk(KERN_ERR "INFO: Object 0x%p @offset=%tu\n",
2412 p, p - addr);
2413 print_tracking(s, p);
2414 }
2415 }
2416 slab_unlock(page);
2417#endif
2418}
2419
Christoph Lameter81819f02007-05-06 14:49:36 -07002420/*
Christoph Lameter599870b2008-04-23 12:36:52 -07002421 * Attempt to free all partial slabs on a node.
Christoph Lameter81819f02007-05-06 14:49:36 -07002422 */
Christoph Lameter599870b2008-04-23 12:36:52 -07002423static void free_partial(struct kmem_cache *s, struct kmem_cache_node *n)
Christoph Lameter81819f02007-05-06 14:49:36 -07002424{
Christoph Lameter81819f02007-05-06 14:49:36 -07002425 unsigned long flags;
2426 struct page *page, *h;
2427
2428 spin_lock_irqsave(&n->list_lock, flags);
Christoph Lameter33b12c32008-04-25 12:22:43 -07002429 list_for_each_entry_safe(page, h, &n->partial, lru) {
Christoph Lameter81819f02007-05-06 14:49:36 -07002430 if (!page->inuse) {
2431 list_del(&page->lru);
2432 discard_slab(s, page);
Christoph Lameter599870b2008-04-23 12:36:52 -07002433 n->nr_partial--;
Christoph Lameter33b12c32008-04-25 12:22:43 -07002434 } else {
2435 list_slab_objects(s, page,
2436 "Objects remaining on kmem_cache_close()");
Christoph Lameter599870b2008-04-23 12:36:52 -07002437 }
Christoph Lameter33b12c32008-04-25 12:22:43 -07002438 }
Christoph Lameter81819f02007-05-06 14:49:36 -07002439 spin_unlock_irqrestore(&n->list_lock, flags);
Christoph Lameter81819f02007-05-06 14:49:36 -07002440}
2441
2442/*
Christoph Lameter672bba32007-05-09 02:32:39 -07002443 * Release all resources used by a slab cache.
Christoph Lameter81819f02007-05-06 14:49:36 -07002444 */
Christoph Lameter0c710012007-07-17 04:03:24 -07002445static inline int kmem_cache_close(struct kmem_cache *s)
Christoph Lameter81819f02007-05-06 14:49:36 -07002446{
2447 int node;
2448
2449 flush_all(s);
2450
2451 /* Attempt to free all objects */
Christoph Lameter4c93c3552007-10-16 01:26:08 -07002452 free_kmem_cache_cpus(s);
Christoph Lameterf64dc582007-10-16 01:25:33 -07002453 for_each_node_state(node, N_NORMAL_MEMORY) {
Christoph Lameter81819f02007-05-06 14:49:36 -07002454 struct kmem_cache_node *n = get_node(s, node);
2455
Christoph Lameter599870b2008-04-23 12:36:52 -07002456 free_partial(s, n);
2457 if (n->nr_partial || slabs_node(s, node))
Christoph Lameter81819f02007-05-06 14:49:36 -07002458 return 1;
2459 }
2460 free_kmem_cache_nodes(s);
2461 return 0;
2462}
2463
2464/*
2465 * Close a cache and release the kmem_cache structure
2466 * (must be used for caches created using kmem_cache_create)
2467 */
2468void kmem_cache_destroy(struct kmem_cache *s)
2469{
2470 down_write(&slub_lock);
2471 s->refcount--;
2472 if (!s->refcount) {
2473 list_del(&s->list);
Christoph Lametera0e1d1b2007-07-17 04:03:31 -07002474 up_write(&slub_lock);
Pekka Enbergd629d812008-04-23 22:31:08 +03002475 if (kmem_cache_close(s)) {
2476 printk(KERN_ERR "SLUB %s: %s called for cache that "
2477 "still has objects.\n", s->name, __func__);
2478 dump_stack();
2479 }
Christoph Lameter81819f02007-05-06 14:49:36 -07002480 sysfs_slab_remove(s);
Christoph Lametera0e1d1b2007-07-17 04:03:31 -07002481 } else
2482 up_write(&slub_lock);
Christoph Lameter81819f02007-05-06 14:49:36 -07002483}
2484EXPORT_SYMBOL(kmem_cache_destroy);
2485
2486/********************************************************************
2487 * Kmalloc subsystem
2488 *******************************************************************/
2489
Christoph Lameter331dc552008-02-14 14:28:09 -08002490struct kmem_cache kmalloc_caches[PAGE_SHIFT + 1] __cacheline_aligned;
Christoph Lameter81819f02007-05-06 14:49:36 -07002491EXPORT_SYMBOL(kmalloc_caches);
2492
Christoph Lameter81819f02007-05-06 14:49:36 -07002493static int __init setup_slub_min_order(char *str)
2494{
Pekka Enberg06428782008-01-07 23:20:27 -08002495 get_option(&str, &slub_min_order);
Christoph Lameter81819f02007-05-06 14:49:36 -07002496
2497 return 1;
2498}
2499
2500__setup("slub_min_order=", setup_slub_min_order);
2501
2502static int __init setup_slub_max_order(char *str)
2503{
Pekka Enberg06428782008-01-07 23:20:27 -08002504 get_option(&str, &slub_max_order);
Christoph Lameter81819f02007-05-06 14:49:36 -07002505
2506 return 1;
2507}
2508
2509__setup("slub_max_order=", setup_slub_max_order);
2510
2511static int __init setup_slub_min_objects(char *str)
2512{
Pekka Enberg06428782008-01-07 23:20:27 -08002513 get_option(&str, &slub_min_objects);
Christoph Lameter81819f02007-05-06 14:49:36 -07002514
2515 return 1;
2516}
2517
2518__setup("slub_min_objects=", setup_slub_min_objects);
2519
2520static int __init setup_slub_nomerge(char *str)
2521{
2522 slub_nomerge = 1;
2523 return 1;
2524}
2525
2526__setup("slub_nomerge", setup_slub_nomerge);
2527
Christoph Lameter81819f02007-05-06 14:49:36 -07002528static struct kmem_cache *create_kmalloc_cache(struct kmem_cache *s,
2529 const char *name, int size, gfp_t gfp_flags)
2530{
2531 unsigned int flags = 0;
2532
2533 if (gfp_flags & SLUB_DMA)
2534 flags = SLAB_CACHE_DMA;
2535
2536 down_write(&slub_lock);
2537 if (!kmem_cache_open(s, gfp_flags, name, size, ARCH_KMALLOC_MINALIGN,
Christoph Lameter319d1e22008-04-14 19:11:41 +03002538 flags, NULL))
Christoph Lameter81819f02007-05-06 14:49:36 -07002539 goto panic;
2540
2541 list_add(&s->list, &slab_caches);
2542 up_write(&slub_lock);
2543 if (sysfs_slab_add(s))
2544 goto panic;
2545 return s;
2546
2547panic:
2548 panic("Creation of kmalloc slab %s size=%d failed.\n", name, size);
2549}
2550
Christoph Lameter2e443fd2007-07-17 04:03:24 -07002551#ifdef CONFIG_ZONE_DMA
Christoph Lameter4097d602008-04-14 18:51:18 +03002552static struct kmem_cache *kmalloc_caches_dma[PAGE_SHIFT + 1];
Christoph Lameter1ceef402007-08-07 15:11:48 -07002553
2554static void sysfs_add_func(struct work_struct *w)
2555{
2556 struct kmem_cache *s;
2557
2558 down_write(&slub_lock);
2559 list_for_each_entry(s, &slab_caches, list) {
2560 if (s->flags & __SYSFS_ADD_DEFERRED) {
2561 s->flags &= ~__SYSFS_ADD_DEFERRED;
2562 sysfs_slab_add(s);
2563 }
2564 }
2565 up_write(&slub_lock);
2566}
2567
2568static DECLARE_WORK(sysfs_add_work, sysfs_add_func);
2569
Christoph Lameter2e443fd2007-07-17 04:03:24 -07002570static noinline struct kmem_cache *dma_kmalloc_cache(int index, gfp_t flags)
2571{
2572 struct kmem_cache *s;
Christoph Lameter2e443fd2007-07-17 04:03:24 -07002573 char *text;
2574 size_t realsize;
2575
2576 s = kmalloc_caches_dma[index];
2577 if (s)
2578 return s;
2579
2580 /* Dynamically create dma cache */
Christoph Lameter1ceef402007-08-07 15:11:48 -07002581 if (flags & __GFP_WAIT)
2582 down_write(&slub_lock);
2583 else {
2584 if (!down_write_trylock(&slub_lock))
2585 goto out;
2586 }
2587
2588 if (kmalloc_caches_dma[index])
2589 goto unlock_out;
Christoph Lameter2e443fd2007-07-17 04:03:24 -07002590
Christoph Lameter7b55f622007-07-17 04:03:27 -07002591 realsize = kmalloc_caches[index].objsize;
Ingo Molnar3adbefe2008-02-05 17:57:39 -08002592 text = kasprintf(flags & ~SLUB_DMA, "kmalloc_dma-%d",
2593 (unsigned int)realsize);
Christoph Lameter1ceef402007-08-07 15:11:48 -07002594 s = kmalloc(kmem_size, flags & ~SLUB_DMA);
2595
2596 if (!s || !text || !kmem_cache_open(s, flags, text,
2597 realsize, ARCH_KMALLOC_MINALIGN,
2598 SLAB_CACHE_DMA|__SYSFS_ADD_DEFERRED, NULL)) {
2599 kfree(s);
2600 kfree(text);
2601 goto unlock_out;
Christoph Lameterdfce8642007-07-17 04:03:25 -07002602 }
Christoph Lameter1ceef402007-08-07 15:11:48 -07002603
2604 list_add(&s->list, &slab_caches);
2605 kmalloc_caches_dma[index] = s;
2606
2607 schedule_work(&sysfs_add_work);
2608
2609unlock_out:
Christoph Lameterdfce8642007-07-17 04:03:25 -07002610 up_write(&slub_lock);
Christoph Lameter1ceef402007-08-07 15:11:48 -07002611out:
Christoph Lameterdfce8642007-07-17 04:03:25 -07002612 return kmalloc_caches_dma[index];
Christoph Lameter2e443fd2007-07-17 04:03:24 -07002613}
2614#endif
2615
Christoph Lameterf1b26332007-07-17 04:03:26 -07002616/*
2617 * Conversion table for small slabs sizes / 8 to the index in the
2618 * kmalloc array. This is necessary for slabs < 192 since we have non power
2619 * of two cache sizes there. The size of larger slabs can be determined using
2620 * fls.
2621 */
2622static s8 size_index[24] = {
2623 3, /* 8 */
2624 4, /* 16 */
2625 5, /* 24 */
2626 5, /* 32 */
2627 6, /* 40 */
2628 6, /* 48 */
2629 6, /* 56 */
2630 6, /* 64 */
2631 1, /* 72 */
2632 1, /* 80 */
2633 1, /* 88 */
2634 1, /* 96 */
2635 7, /* 104 */
2636 7, /* 112 */
2637 7, /* 120 */
2638 7, /* 128 */
2639 2, /* 136 */
2640 2, /* 144 */
2641 2, /* 152 */
2642 2, /* 160 */
2643 2, /* 168 */
2644 2, /* 176 */
2645 2, /* 184 */
2646 2 /* 192 */
2647};
2648
Christoph Lameter81819f02007-05-06 14:49:36 -07002649static struct kmem_cache *get_slab(size_t size, gfp_t flags)
2650{
Christoph Lameterf1b26332007-07-17 04:03:26 -07002651 int index;
Christoph Lameter81819f02007-05-06 14:49:36 -07002652
Christoph Lameterf1b26332007-07-17 04:03:26 -07002653 if (size <= 192) {
2654 if (!size)
2655 return ZERO_SIZE_PTR;
Christoph Lameter81819f02007-05-06 14:49:36 -07002656
Christoph Lameterf1b26332007-07-17 04:03:26 -07002657 index = size_index[(size - 1) / 8];
Christoph Lameteraadb4bc2007-10-16 01:24:38 -07002658 } else
Christoph Lameterf1b26332007-07-17 04:03:26 -07002659 index = fls(size - 1);
Christoph Lameter81819f02007-05-06 14:49:36 -07002660
2661#ifdef CONFIG_ZONE_DMA
Christoph Lameterf1b26332007-07-17 04:03:26 -07002662 if (unlikely((flags & SLUB_DMA)))
Christoph Lameter2e443fd2007-07-17 04:03:24 -07002663 return dma_kmalloc_cache(index, flags);
Christoph Lameterf1b26332007-07-17 04:03:26 -07002664
Christoph Lameter81819f02007-05-06 14:49:36 -07002665#endif
2666 return &kmalloc_caches[index];
2667}
2668
2669void *__kmalloc(size_t size, gfp_t flags)
2670{
Christoph Lameteraadb4bc2007-10-16 01:24:38 -07002671 struct kmem_cache *s;
Christoph Lameter81819f02007-05-06 14:49:36 -07002672
Christoph Lameter331dc552008-02-14 14:28:09 -08002673 if (unlikely(size > PAGE_SIZE))
Pekka Enbergeada35e2008-02-11 22:47:46 +02002674 return kmalloc_large(size, flags);
Christoph Lameteraadb4bc2007-10-16 01:24:38 -07002675
2676 s = get_slab(size, flags);
2677
2678 if (unlikely(ZERO_OR_NULL_PTR(s)))
Christoph Lameter6cb8f912007-07-17 04:03:22 -07002679 return s;
2680
Christoph Lameterce15fea2007-07-17 04:03:28 -07002681 return slab_alloc(s, flags, -1, __builtin_return_address(0));
Christoph Lameter81819f02007-05-06 14:49:36 -07002682}
2683EXPORT_SYMBOL(__kmalloc);
2684
Christoph Lameterf619cfe2008-03-01 13:56:40 -08002685static void *kmalloc_large_node(size_t size, gfp_t flags, int node)
2686{
2687 struct page *page = alloc_pages_node(node, flags | __GFP_COMP,
2688 get_order(size));
2689
2690 if (page)
2691 return page_address(page);
2692 else
2693 return NULL;
2694}
2695
Christoph Lameter81819f02007-05-06 14:49:36 -07002696#ifdef CONFIG_NUMA
2697void *__kmalloc_node(size_t size, gfp_t flags, int node)
2698{
Christoph Lameteraadb4bc2007-10-16 01:24:38 -07002699 struct kmem_cache *s;
Christoph Lameter81819f02007-05-06 14:49:36 -07002700
Christoph Lameter331dc552008-02-14 14:28:09 -08002701 if (unlikely(size > PAGE_SIZE))
Christoph Lameterf619cfe2008-03-01 13:56:40 -08002702 return kmalloc_large_node(size, flags, node);
Christoph Lameteraadb4bc2007-10-16 01:24:38 -07002703
2704 s = get_slab(size, flags);
2705
2706 if (unlikely(ZERO_OR_NULL_PTR(s)))
Christoph Lameter6cb8f912007-07-17 04:03:22 -07002707 return s;
2708
Christoph Lameterce15fea2007-07-17 04:03:28 -07002709 return slab_alloc(s, flags, node, __builtin_return_address(0));
Christoph Lameter81819f02007-05-06 14:49:36 -07002710}
2711EXPORT_SYMBOL(__kmalloc_node);
2712#endif
2713
2714size_t ksize(const void *object)
2715{
Christoph Lameter272c1d22007-06-08 13:46:49 -07002716 struct page *page;
Christoph Lameter81819f02007-05-06 14:49:36 -07002717 struct kmem_cache *s;
2718
Christoph Lameteref8b4522007-10-16 01:24:46 -07002719 if (unlikely(object == ZERO_SIZE_PTR))
Christoph Lameter272c1d22007-06-08 13:46:49 -07002720 return 0;
2721
Vegard Nossum294a80a2007-12-04 23:45:30 -08002722 page = virt_to_head_page(object);
Vegard Nossum294a80a2007-12-04 23:45:30 -08002723
2724 if (unlikely(!PageSlab(page)))
2725 return PAGE_SIZE << compound_order(page);
2726
Christoph Lameter81819f02007-05-06 14:49:36 -07002727 s = page->slab;
Christoph Lameter81819f02007-05-06 14:49:36 -07002728
Christoph Lameterae20bfd2008-02-15 23:45:25 -08002729#ifdef CONFIG_SLUB_DEBUG
Christoph Lameter81819f02007-05-06 14:49:36 -07002730 /*
2731 * Debugging requires use of the padding between object
2732 * and whatever may come after it.
2733 */
2734 if (s->flags & (SLAB_RED_ZONE | SLAB_POISON))
2735 return s->objsize;
2736
Christoph Lameterae20bfd2008-02-15 23:45:25 -08002737#endif
Christoph Lameter81819f02007-05-06 14:49:36 -07002738 /*
2739 * If we have the need to store the freelist pointer
2740 * back there or track user information then we can
2741 * only use the space before that information.
2742 */
2743 if (s->flags & (SLAB_DESTROY_BY_RCU | SLAB_STORE_USER))
2744 return s->inuse;
Christoph Lameter81819f02007-05-06 14:49:36 -07002745 /*
2746 * Else we can use all the padding etc for the allocation
2747 */
2748 return s->size;
2749}
2750EXPORT_SYMBOL(ksize);
2751
2752void kfree(const void *x)
2753{
Christoph Lameter81819f02007-05-06 14:49:36 -07002754 struct page *page;
Christoph Lameter5bb983b2008-02-07 17:47:41 -08002755 void *object = (void *)x;
Christoph Lameter81819f02007-05-06 14:49:36 -07002756
Satyam Sharma2408c552007-10-16 01:24:44 -07002757 if (unlikely(ZERO_OR_NULL_PTR(x)))
Christoph Lameter81819f02007-05-06 14:49:36 -07002758 return;
2759
Christoph Lameterb49af682007-05-06 14:49:41 -07002760 page = virt_to_head_page(x);
Christoph Lameteraadb4bc2007-10-16 01:24:38 -07002761 if (unlikely(!PageSlab(page))) {
2762 put_page(page);
2763 return;
2764 }
Christoph Lameter5bb983b2008-02-07 17:47:41 -08002765 slab_free(page->slab, page, object, __builtin_return_address(0));
Christoph Lameter81819f02007-05-06 14:49:36 -07002766}
2767EXPORT_SYMBOL(kfree);
2768
Christoph Lameter2086d262007-05-06 14:49:46 -07002769/*
Christoph Lameter672bba32007-05-09 02:32:39 -07002770 * kmem_cache_shrink removes empty slabs from the partial lists and sorts
2771 * the remaining slabs by the number of items in use. The slabs with the
2772 * most items in use come first. New allocations will then fill those up
2773 * and thus they can be removed from the partial lists.
2774 *
2775 * The slabs with the least items are placed last. This results in them
2776 * being allocated from last increasing the chance that the last objects
2777 * are freed in them.
Christoph Lameter2086d262007-05-06 14:49:46 -07002778 */
2779int kmem_cache_shrink(struct kmem_cache *s)
2780{
2781 int node;
2782 int i;
2783 struct kmem_cache_node *n;
2784 struct page *page;
2785 struct page *t;
Christoph Lameter205ab992008-04-14 19:11:40 +03002786 int objects = oo_objects(s->max);
Christoph Lameter2086d262007-05-06 14:49:46 -07002787 struct list_head *slabs_by_inuse =
Christoph Lameter834f3d12008-04-14 19:11:31 +03002788 kmalloc(sizeof(struct list_head) * objects, GFP_KERNEL);
Christoph Lameter2086d262007-05-06 14:49:46 -07002789 unsigned long flags;
2790
2791 if (!slabs_by_inuse)
2792 return -ENOMEM;
2793
2794 flush_all(s);
Christoph Lameterf64dc582007-10-16 01:25:33 -07002795 for_each_node_state(node, N_NORMAL_MEMORY) {
Christoph Lameter2086d262007-05-06 14:49:46 -07002796 n = get_node(s, node);
2797
2798 if (!n->nr_partial)
2799 continue;
2800
Christoph Lameter834f3d12008-04-14 19:11:31 +03002801 for (i = 0; i < objects; i++)
Christoph Lameter2086d262007-05-06 14:49:46 -07002802 INIT_LIST_HEAD(slabs_by_inuse + i);
2803
2804 spin_lock_irqsave(&n->list_lock, flags);
2805
2806 /*
Christoph Lameter672bba32007-05-09 02:32:39 -07002807 * Build lists indexed by the items in use in each slab.
Christoph Lameter2086d262007-05-06 14:49:46 -07002808 *
Christoph Lameter672bba32007-05-09 02:32:39 -07002809 * Note that concurrent frees may occur while we hold the
2810 * list_lock. page->inuse here is the upper limit.
Christoph Lameter2086d262007-05-06 14:49:46 -07002811 */
2812 list_for_each_entry_safe(page, t, &n->partial, lru) {
2813 if (!page->inuse && slab_trylock(page)) {
2814 /*
2815 * Must hold slab lock here because slab_free
2816 * may have freed the last object and be
2817 * waiting to release the slab.
2818 */
2819 list_del(&page->lru);
2820 n->nr_partial--;
2821 slab_unlock(page);
2822 discard_slab(s, page);
2823 } else {
Christoph Lameterfcda3d82007-07-30 13:06:46 -07002824 list_move(&page->lru,
2825 slabs_by_inuse + page->inuse);
Christoph Lameter2086d262007-05-06 14:49:46 -07002826 }
2827 }
2828
Christoph Lameter2086d262007-05-06 14:49:46 -07002829 /*
Christoph Lameter672bba32007-05-09 02:32:39 -07002830 * Rebuild the partial list with the slabs filled up most
2831 * first and the least used slabs at the end.
Christoph Lameter2086d262007-05-06 14:49:46 -07002832 */
Christoph Lameter834f3d12008-04-14 19:11:31 +03002833 for (i = objects - 1; i >= 0; i--)
Christoph Lameter2086d262007-05-06 14:49:46 -07002834 list_splice(slabs_by_inuse + i, n->partial.prev);
2835
Christoph Lameter2086d262007-05-06 14:49:46 -07002836 spin_unlock_irqrestore(&n->list_lock, flags);
2837 }
2838
2839 kfree(slabs_by_inuse);
2840 return 0;
2841}
2842EXPORT_SYMBOL(kmem_cache_shrink);
2843
Yasunori Gotob9049e22007-10-21 16:41:37 -07002844#if defined(CONFIG_NUMA) && defined(CONFIG_MEMORY_HOTPLUG)
2845static int slab_mem_going_offline_callback(void *arg)
2846{
2847 struct kmem_cache *s;
2848
2849 down_read(&slub_lock);
2850 list_for_each_entry(s, &slab_caches, list)
2851 kmem_cache_shrink(s);
2852 up_read(&slub_lock);
2853
2854 return 0;
2855}
2856
2857static void slab_mem_offline_callback(void *arg)
2858{
2859 struct kmem_cache_node *n;
2860 struct kmem_cache *s;
2861 struct memory_notify *marg = arg;
2862 int offline_node;
2863
2864 offline_node = marg->status_change_nid;
2865
2866 /*
2867 * If the node still has available memory. we need kmem_cache_node
2868 * for it yet.
2869 */
2870 if (offline_node < 0)
2871 return;
2872
2873 down_read(&slub_lock);
2874 list_for_each_entry(s, &slab_caches, list) {
2875 n = get_node(s, offline_node);
2876 if (n) {
2877 /*
2878 * if n->nr_slabs > 0, slabs still exist on the node
2879 * that is going down. We were unable to free them,
2880 * and offline_pages() function shoudn't call this
2881 * callback. So, we must fail.
2882 */
Christoph Lameter0f389ec2008-04-14 18:53:02 +03002883 BUG_ON(slabs_node(s, offline_node));
Yasunori Gotob9049e22007-10-21 16:41:37 -07002884
2885 s->node[offline_node] = NULL;
2886 kmem_cache_free(kmalloc_caches, n);
2887 }
2888 }
2889 up_read(&slub_lock);
2890}
2891
2892static int slab_mem_going_online_callback(void *arg)
2893{
2894 struct kmem_cache_node *n;
2895 struct kmem_cache *s;
2896 struct memory_notify *marg = arg;
2897 int nid = marg->status_change_nid;
2898 int ret = 0;
2899
2900 /*
2901 * If the node's memory is already available, then kmem_cache_node is
2902 * already created. Nothing to do.
2903 */
2904 if (nid < 0)
2905 return 0;
2906
2907 /*
2908 * We are bringing a node online. No memory is availabe yet. We must
2909 * allocate a kmem_cache_node structure in order to bring the node
2910 * online.
2911 */
2912 down_read(&slub_lock);
2913 list_for_each_entry(s, &slab_caches, list) {
2914 /*
2915 * XXX: kmem_cache_alloc_node will fallback to other nodes
2916 * since memory is not yet available from the node that
2917 * is brought up.
2918 */
2919 n = kmem_cache_alloc(kmalloc_caches, GFP_KERNEL);
2920 if (!n) {
2921 ret = -ENOMEM;
2922 goto out;
2923 }
2924 init_kmem_cache_node(n);
2925 s->node[nid] = n;
2926 }
2927out:
2928 up_read(&slub_lock);
2929 return ret;
2930}
2931
2932static int slab_memory_callback(struct notifier_block *self,
2933 unsigned long action, void *arg)
2934{
2935 int ret = 0;
2936
2937 switch (action) {
2938 case MEM_GOING_ONLINE:
2939 ret = slab_mem_going_online_callback(arg);
2940 break;
2941 case MEM_GOING_OFFLINE:
2942 ret = slab_mem_going_offline_callback(arg);
2943 break;
2944 case MEM_OFFLINE:
2945 case MEM_CANCEL_ONLINE:
2946 slab_mem_offline_callback(arg);
2947 break;
2948 case MEM_ONLINE:
2949 case MEM_CANCEL_OFFLINE:
2950 break;
2951 }
2952
2953 ret = notifier_from_errno(ret);
2954 return ret;
2955}
2956
2957#endif /* CONFIG_MEMORY_HOTPLUG */
2958
Christoph Lameter81819f02007-05-06 14:49:36 -07002959/********************************************************************
2960 * Basic setup of slabs
2961 *******************************************************************/
2962
2963void __init kmem_cache_init(void)
2964{
2965 int i;
Christoph Lameter4b356be2007-06-16 10:16:13 -07002966 int caches = 0;
Christoph Lameter81819f02007-05-06 14:49:36 -07002967
Christoph Lameter4c93c3552007-10-16 01:26:08 -07002968 init_alloc_cpu();
2969
Christoph Lameter81819f02007-05-06 14:49:36 -07002970#ifdef CONFIG_NUMA
2971 /*
2972 * Must first have the slab cache available for the allocations of the
Christoph Lameter672bba32007-05-09 02:32:39 -07002973 * struct kmem_cache_node's. There is special bootstrap code in
Christoph Lameter81819f02007-05-06 14:49:36 -07002974 * kmem_cache_open for slab_state == DOWN.
2975 */
2976 create_kmalloc_cache(&kmalloc_caches[0], "kmem_cache_node",
2977 sizeof(struct kmem_cache_node), GFP_KERNEL);
Christoph Lameter8ffa6872007-05-31 00:40:51 -07002978 kmalloc_caches[0].refcount = -1;
Christoph Lameter4b356be2007-06-16 10:16:13 -07002979 caches++;
Yasunori Gotob9049e22007-10-21 16:41:37 -07002980
2981 hotplug_memory_notifier(slab_memory_callback, 1);
Christoph Lameter81819f02007-05-06 14:49:36 -07002982#endif
2983
2984 /* Able to allocate the per node structures */
2985 slab_state = PARTIAL;
2986
2987 /* Caches that are not of the two-to-the-power-of size */
Christoph Lameter4b356be2007-06-16 10:16:13 -07002988 if (KMALLOC_MIN_SIZE <= 64) {
2989 create_kmalloc_cache(&kmalloc_caches[1],
Christoph Lameter81819f02007-05-06 14:49:36 -07002990 "kmalloc-96", 96, GFP_KERNEL);
Christoph Lameter4b356be2007-06-16 10:16:13 -07002991 caches++;
2992 }
2993 if (KMALLOC_MIN_SIZE <= 128) {
2994 create_kmalloc_cache(&kmalloc_caches[2],
Christoph Lameter81819f02007-05-06 14:49:36 -07002995 "kmalloc-192", 192, GFP_KERNEL);
Christoph Lameter4b356be2007-06-16 10:16:13 -07002996 caches++;
2997 }
Christoph Lameter81819f02007-05-06 14:49:36 -07002998
Christoph Lameter331dc552008-02-14 14:28:09 -08002999 for (i = KMALLOC_SHIFT_LOW; i <= PAGE_SHIFT; i++) {
Christoph Lameter81819f02007-05-06 14:49:36 -07003000 create_kmalloc_cache(&kmalloc_caches[i],
3001 "kmalloc", 1 << i, GFP_KERNEL);
Christoph Lameter4b356be2007-06-16 10:16:13 -07003002 caches++;
3003 }
Christoph Lameter81819f02007-05-06 14:49:36 -07003004
Christoph Lameterf1b26332007-07-17 04:03:26 -07003005
3006 /*
3007 * Patch up the size_index table if we have strange large alignment
3008 * requirements for the kmalloc array. This is only the case for
Christoph Lameter6446faa2008-02-15 23:45:26 -08003009 * MIPS it seems. The standard arches will not generate any code here.
Christoph Lameterf1b26332007-07-17 04:03:26 -07003010 *
3011 * Largest permitted alignment is 256 bytes due to the way we
3012 * handle the index determination for the smaller caches.
3013 *
3014 * Make sure that nothing crazy happens if someone starts tinkering
3015 * around with ARCH_KMALLOC_MINALIGN
3016 */
3017 BUILD_BUG_ON(KMALLOC_MIN_SIZE > 256 ||
3018 (KMALLOC_MIN_SIZE & (KMALLOC_MIN_SIZE - 1)));
3019
Christoph Lameter12ad6842007-07-17 04:03:28 -07003020 for (i = 8; i < KMALLOC_MIN_SIZE; i += 8)
Christoph Lameterf1b26332007-07-17 04:03:26 -07003021 size_index[(i - 1) / 8] = KMALLOC_SHIFT_LOW;
3022
Christoph Lameter81819f02007-05-06 14:49:36 -07003023 slab_state = UP;
3024
3025 /* Provide the correct kmalloc names now that the caches are up */
Christoph Lameter331dc552008-02-14 14:28:09 -08003026 for (i = KMALLOC_SHIFT_LOW; i <= PAGE_SHIFT; i++)
Christoph Lameter81819f02007-05-06 14:49:36 -07003027 kmalloc_caches[i]. name =
3028 kasprintf(GFP_KERNEL, "kmalloc-%d", 1 << i);
3029
3030#ifdef CONFIG_SMP
3031 register_cpu_notifier(&slab_notifier);
Christoph Lameter4c93c3552007-10-16 01:26:08 -07003032 kmem_size = offsetof(struct kmem_cache, cpu_slab) +
3033 nr_cpu_ids * sizeof(struct kmem_cache_cpu *);
3034#else
3035 kmem_size = sizeof(struct kmem_cache);
Christoph Lameter81819f02007-05-06 14:49:36 -07003036#endif
3037
Ingo Molnar3adbefe2008-02-05 17:57:39 -08003038 printk(KERN_INFO
3039 "SLUB: Genslabs=%d, HWalign=%d, Order=%d-%d, MinObjects=%d,"
Christoph Lameter4b356be2007-06-16 10:16:13 -07003040 " CPUs=%d, Nodes=%d\n",
3041 caches, cache_line_size(),
Christoph Lameter81819f02007-05-06 14:49:36 -07003042 slub_min_order, slub_max_order, slub_min_objects,
3043 nr_cpu_ids, nr_node_ids);
3044}
3045
3046/*
3047 * Find a mergeable slab cache
3048 */
3049static int slab_unmergeable(struct kmem_cache *s)
3050{
3051 if (slub_nomerge || (s->flags & SLUB_NEVER_MERGE))
3052 return 1;
3053
Christoph Lameterc59def92007-05-16 22:10:50 -07003054 if (s->ctor)
Christoph Lameter81819f02007-05-06 14:49:36 -07003055 return 1;
3056
Christoph Lameter8ffa6872007-05-31 00:40:51 -07003057 /*
3058 * We may have set a slab to be unmergeable during bootstrap.
3059 */
3060 if (s->refcount < 0)
3061 return 1;
3062
Christoph Lameter81819f02007-05-06 14:49:36 -07003063 return 0;
3064}
3065
3066static struct kmem_cache *find_mergeable(size_t size,
Christoph Lameterba0268a2007-09-11 15:24:11 -07003067 size_t align, unsigned long flags, const char *name,
Christoph Lameter4ba9b9d2007-10-16 23:25:51 -07003068 void (*ctor)(struct kmem_cache *, void *))
Christoph Lameter81819f02007-05-06 14:49:36 -07003069{
Christoph Lameter5b95a4ac2007-07-17 04:03:19 -07003070 struct kmem_cache *s;
Christoph Lameter81819f02007-05-06 14:49:36 -07003071
3072 if (slub_nomerge || (flags & SLUB_NEVER_MERGE))
3073 return NULL;
3074
Christoph Lameterc59def92007-05-16 22:10:50 -07003075 if (ctor)
Christoph Lameter81819f02007-05-06 14:49:36 -07003076 return NULL;
3077
3078 size = ALIGN(size, sizeof(void *));
3079 align = calculate_alignment(flags, align, size);
3080 size = ALIGN(size, align);
Christoph Lameterba0268a2007-09-11 15:24:11 -07003081 flags = kmem_cache_flags(size, flags, name, NULL);
Christoph Lameter81819f02007-05-06 14:49:36 -07003082
Christoph Lameter5b95a4ac2007-07-17 04:03:19 -07003083 list_for_each_entry(s, &slab_caches, list) {
Christoph Lameter81819f02007-05-06 14:49:36 -07003084 if (slab_unmergeable(s))
3085 continue;
3086
3087 if (size > s->size)
3088 continue;
3089
Christoph Lameterba0268a2007-09-11 15:24:11 -07003090 if ((flags & SLUB_MERGE_SAME) != (s->flags & SLUB_MERGE_SAME))
Christoph Lameter81819f02007-05-06 14:49:36 -07003091 continue;
3092 /*
3093 * Check if alignment is compatible.
3094 * Courtesy of Adrian Drzewiecki
3095 */
Pekka Enberg06428782008-01-07 23:20:27 -08003096 if ((s->size & ~(align - 1)) != s->size)
Christoph Lameter81819f02007-05-06 14:49:36 -07003097 continue;
3098
3099 if (s->size - size >= sizeof(void *))
3100 continue;
3101
3102 return s;
3103 }
3104 return NULL;
3105}
3106
3107struct kmem_cache *kmem_cache_create(const char *name, size_t size,
3108 size_t align, unsigned long flags,
Christoph Lameter4ba9b9d2007-10-16 23:25:51 -07003109 void (*ctor)(struct kmem_cache *, void *))
Christoph Lameter81819f02007-05-06 14:49:36 -07003110{
3111 struct kmem_cache *s;
3112
3113 down_write(&slub_lock);
Christoph Lameterba0268a2007-09-11 15:24:11 -07003114 s = find_mergeable(size, align, flags, name, ctor);
Christoph Lameter81819f02007-05-06 14:49:36 -07003115 if (s) {
Christoph Lameter42a9fdb2007-10-16 01:26:09 -07003116 int cpu;
3117
Christoph Lameter81819f02007-05-06 14:49:36 -07003118 s->refcount++;
3119 /*
3120 * Adjust the object sizes so that we clear
3121 * the complete object on kzalloc.
3122 */
3123 s->objsize = max(s->objsize, (int)size);
Christoph Lameter42a9fdb2007-10-16 01:26:09 -07003124
3125 /*
3126 * And then we need to update the object size in the
3127 * per cpu structures
3128 */
3129 for_each_online_cpu(cpu)
3130 get_cpu_slab(s, cpu)->objsize = s->objsize;
Christoph Lameter6446faa2008-02-15 23:45:26 -08003131
Christoph Lameter81819f02007-05-06 14:49:36 -07003132 s->inuse = max_t(int, s->inuse, ALIGN(size, sizeof(void *)));
Christoph Lametera0e1d1b2007-07-17 04:03:31 -07003133 up_write(&slub_lock);
Christoph Lameter6446faa2008-02-15 23:45:26 -08003134
Christoph Lameter81819f02007-05-06 14:49:36 -07003135 if (sysfs_slab_alias(s, name))
3136 goto err;
Christoph Lametera0e1d1b2007-07-17 04:03:31 -07003137 return s;
3138 }
Christoph Lameter6446faa2008-02-15 23:45:26 -08003139
Christoph Lametera0e1d1b2007-07-17 04:03:31 -07003140 s = kmalloc(kmem_size, GFP_KERNEL);
3141 if (s) {
3142 if (kmem_cache_open(s, GFP_KERNEL, name,
Christoph Lameterc59def92007-05-16 22:10:50 -07003143 size, align, flags, ctor)) {
Christoph Lameter81819f02007-05-06 14:49:36 -07003144 list_add(&s->list, &slab_caches);
Christoph Lametera0e1d1b2007-07-17 04:03:31 -07003145 up_write(&slub_lock);
3146 if (sysfs_slab_add(s))
3147 goto err;
3148 return s;
3149 }
3150 kfree(s);
Christoph Lameter81819f02007-05-06 14:49:36 -07003151 }
3152 up_write(&slub_lock);
Christoph Lameter81819f02007-05-06 14:49:36 -07003153
3154err:
Christoph Lameter81819f02007-05-06 14:49:36 -07003155 if (flags & SLAB_PANIC)
3156 panic("Cannot create slabcache %s\n", name);
3157 else
3158 s = NULL;
3159 return s;
3160}
3161EXPORT_SYMBOL(kmem_cache_create);
3162
Christoph Lameter81819f02007-05-06 14:49:36 -07003163#ifdef CONFIG_SMP
Christoph Lameter27390bc2007-06-01 00:47:09 -07003164/*
Christoph Lameter672bba32007-05-09 02:32:39 -07003165 * Use the cpu notifier to insure that the cpu slabs are flushed when
3166 * necessary.
Christoph Lameter81819f02007-05-06 14:49:36 -07003167 */
3168static int __cpuinit slab_cpuup_callback(struct notifier_block *nfb,
3169 unsigned long action, void *hcpu)
3170{
3171 long cpu = (long)hcpu;
Christoph Lameter5b95a4ac2007-07-17 04:03:19 -07003172 struct kmem_cache *s;
3173 unsigned long flags;
Christoph Lameter81819f02007-05-06 14:49:36 -07003174
3175 switch (action) {
Christoph Lameter4c93c3552007-10-16 01:26:08 -07003176 case CPU_UP_PREPARE:
3177 case CPU_UP_PREPARE_FROZEN:
3178 init_alloc_cpu_cpu(cpu);
3179 down_read(&slub_lock);
3180 list_for_each_entry(s, &slab_caches, list)
3181 s->cpu_slab[cpu] = alloc_kmem_cache_cpu(s, cpu,
3182 GFP_KERNEL);
3183 up_read(&slub_lock);
3184 break;
3185
Christoph Lameter81819f02007-05-06 14:49:36 -07003186 case CPU_UP_CANCELED:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07003187 case CPU_UP_CANCELED_FROZEN:
Christoph Lameter81819f02007-05-06 14:49:36 -07003188 case CPU_DEAD:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07003189 case CPU_DEAD_FROZEN:
Christoph Lameter5b95a4ac2007-07-17 04:03:19 -07003190 down_read(&slub_lock);
3191 list_for_each_entry(s, &slab_caches, list) {
Christoph Lameter4c93c3552007-10-16 01:26:08 -07003192 struct kmem_cache_cpu *c = get_cpu_slab(s, cpu);
3193
Christoph Lameter5b95a4ac2007-07-17 04:03:19 -07003194 local_irq_save(flags);
3195 __flush_cpu_slab(s, cpu);
3196 local_irq_restore(flags);
Christoph Lameter4c93c3552007-10-16 01:26:08 -07003197 free_kmem_cache_cpu(c, cpu);
3198 s->cpu_slab[cpu] = NULL;
Christoph Lameter5b95a4ac2007-07-17 04:03:19 -07003199 }
3200 up_read(&slub_lock);
Christoph Lameter81819f02007-05-06 14:49:36 -07003201 break;
3202 default:
3203 break;
3204 }
3205 return NOTIFY_OK;
3206}
3207
Pekka Enberg06428782008-01-07 23:20:27 -08003208static struct notifier_block __cpuinitdata slab_notifier = {
Ingo Molnar3adbefe2008-02-05 17:57:39 -08003209 .notifier_call = slab_cpuup_callback
Pekka Enberg06428782008-01-07 23:20:27 -08003210};
Christoph Lameter81819f02007-05-06 14:49:36 -07003211
3212#endif
3213
Christoph Lameter81819f02007-05-06 14:49:36 -07003214void *__kmalloc_track_caller(size_t size, gfp_t gfpflags, void *caller)
3215{
Christoph Lameteraadb4bc2007-10-16 01:24:38 -07003216 struct kmem_cache *s;
3217
Christoph Lameter331dc552008-02-14 14:28:09 -08003218 if (unlikely(size > PAGE_SIZE))
Pekka Enbergeada35e2008-02-11 22:47:46 +02003219 return kmalloc_large(size, gfpflags);
3220
Christoph Lameteraadb4bc2007-10-16 01:24:38 -07003221 s = get_slab(size, gfpflags);
Christoph Lameter81819f02007-05-06 14:49:36 -07003222
Satyam Sharma2408c552007-10-16 01:24:44 -07003223 if (unlikely(ZERO_OR_NULL_PTR(s)))
Christoph Lameter6cb8f912007-07-17 04:03:22 -07003224 return s;
Christoph Lameter81819f02007-05-06 14:49:36 -07003225
Christoph Lameterce15fea2007-07-17 04:03:28 -07003226 return slab_alloc(s, gfpflags, -1, caller);
Christoph Lameter81819f02007-05-06 14:49:36 -07003227}
3228
3229void *__kmalloc_node_track_caller(size_t size, gfp_t gfpflags,
3230 int node, void *caller)
3231{
Christoph Lameteraadb4bc2007-10-16 01:24:38 -07003232 struct kmem_cache *s;
3233
Christoph Lameter331dc552008-02-14 14:28:09 -08003234 if (unlikely(size > PAGE_SIZE))
Christoph Lameterf619cfe2008-03-01 13:56:40 -08003235 return kmalloc_large_node(size, gfpflags, node);
Pekka Enbergeada35e2008-02-11 22:47:46 +02003236
Christoph Lameteraadb4bc2007-10-16 01:24:38 -07003237 s = get_slab(size, gfpflags);
Christoph Lameter81819f02007-05-06 14:49:36 -07003238
Satyam Sharma2408c552007-10-16 01:24:44 -07003239 if (unlikely(ZERO_OR_NULL_PTR(s)))
Christoph Lameter6cb8f912007-07-17 04:03:22 -07003240 return s;
Christoph Lameter81819f02007-05-06 14:49:36 -07003241
Christoph Lameterce15fea2007-07-17 04:03:28 -07003242 return slab_alloc(s, gfpflags, node, caller);
Christoph Lameter81819f02007-05-06 14:49:36 -07003243}
3244
Christoph Lameter5b06c8532008-04-14 18:51:34 +03003245#if (defined(CONFIG_SYSFS) && defined(CONFIG_SLUB_DEBUG)) || defined(CONFIG_SLABINFO)
Christoph Lameter205ab992008-04-14 19:11:40 +03003246static unsigned long count_partial(struct kmem_cache_node *n,
3247 int (*get_count)(struct page *))
Christoph Lameter5b06c8532008-04-14 18:51:34 +03003248{
3249 unsigned long flags;
3250 unsigned long x = 0;
3251 struct page *page;
3252
3253 spin_lock_irqsave(&n->list_lock, flags);
3254 list_for_each_entry(page, &n->partial, lru)
Christoph Lameter205ab992008-04-14 19:11:40 +03003255 x += get_count(page);
Christoph Lameter5b06c8532008-04-14 18:51:34 +03003256 spin_unlock_irqrestore(&n->list_lock, flags);
3257 return x;
3258}
Christoph Lameter205ab992008-04-14 19:11:40 +03003259
3260static int count_inuse(struct page *page)
3261{
3262 return page->inuse;
3263}
3264
3265static int count_total(struct page *page)
3266{
3267 return page->objects;
3268}
3269
3270static int count_free(struct page *page)
3271{
3272 return page->objects - page->inuse;
3273}
Christoph Lameter5b06c8532008-04-14 18:51:34 +03003274#endif
3275
Christoph Lameter41ecc552007-05-09 02:32:44 -07003276#if defined(CONFIG_SYSFS) && defined(CONFIG_SLUB_DEBUG)
Christoph Lameter434e2452007-07-17 04:03:30 -07003277static int validate_slab(struct kmem_cache *s, struct page *page,
3278 unsigned long *map)
Christoph Lameter53e15af2007-05-06 14:49:43 -07003279{
3280 void *p;
Christoph Lametera973e9d2008-03-01 13:40:44 -08003281 void *addr = page_address(page);
Christoph Lameter53e15af2007-05-06 14:49:43 -07003282
3283 if (!check_slab(s, page) ||
3284 !on_freelist(s, page, NULL))
3285 return 0;
3286
3287 /* Now we know that a valid freelist exists */
Christoph Lameter39b26462008-04-14 19:11:30 +03003288 bitmap_zero(map, page->objects);
Christoph Lameter53e15af2007-05-06 14:49:43 -07003289
Christoph Lameter7656c722007-05-09 02:32:40 -07003290 for_each_free_object(p, s, page->freelist) {
3291 set_bit(slab_index(p, s, addr), map);
Christoph Lameter53e15af2007-05-06 14:49:43 -07003292 if (!check_object(s, page, p, 0))
3293 return 0;
3294 }
3295
Christoph Lameter224a88b2008-04-14 19:11:31 +03003296 for_each_object(p, s, addr, page->objects)
Christoph Lameter7656c722007-05-09 02:32:40 -07003297 if (!test_bit(slab_index(p, s, addr), map))
Christoph Lameter53e15af2007-05-06 14:49:43 -07003298 if (!check_object(s, page, p, 1))
3299 return 0;
3300 return 1;
3301}
3302
Christoph Lameter434e2452007-07-17 04:03:30 -07003303static void validate_slab_slab(struct kmem_cache *s, struct page *page,
3304 unsigned long *map)
Christoph Lameter53e15af2007-05-06 14:49:43 -07003305{
3306 if (slab_trylock(page)) {
Christoph Lameter434e2452007-07-17 04:03:30 -07003307 validate_slab(s, page, map);
Christoph Lameter53e15af2007-05-06 14:49:43 -07003308 slab_unlock(page);
3309 } else
3310 printk(KERN_INFO "SLUB %s: Skipped busy slab 0x%p\n",
3311 s->name, page);
3312
3313 if (s->flags & DEBUG_DEFAULT_FLAGS) {
Christoph Lameter35e5d7e2007-05-09 02:32:42 -07003314 if (!SlabDebug(page))
3315 printk(KERN_ERR "SLUB %s: SlabDebug not set "
Christoph Lameter53e15af2007-05-06 14:49:43 -07003316 "on slab 0x%p\n", s->name, page);
3317 } else {
Christoph Lameter35e5d7e2007-05-09 02:32:42 -07003318 if (SlabDebug(page))
3319 printk(KERN_ERR "SLUB %s: SlabDebug set on "
Christoph Lameter53e15af2007-05-06 14:49:43 -07003320 "slab 0x%p\n", s->name, page);
3321 }
3322}
3323
Christoph Lameter434e2452007-07-17 04:03:30 -07003324static int validate_slab_node(struct kmem_cache *s,
3325 struct kmem_cache_node *n, unsigned long *map)
Christoph Lameter53e15af2007-05-06 14:49:43 -07003326{
3327 unsigned long count = 0;
3328 struct page *page;
3329 unsigned long flags;
3330
3331 spin_lock_irqsave(&n->list_lock, flags);
3332
3333 list_for_each_entry(page, &n->partial, lru) {
Christoph Lameter434e2452007-07-17 04:03:30 -07003334 validate_slab_slab(s, page, map);
Christoph Lameter53e15af2007-05-06 14:49:43 -07003335 count++;
3336 }
3337 if (count != n->nr_partial)
3338 printk(KERN_ERR "SLUB %s: %ld partial slabs counted but "
3339 "counter=%ld\n", s->name, count, n->nr_partial);
3340
3341 if (!(s->flags & SLAB_STORE_USER))
3342 goto out;
3343
3344 list_for_each_entry(page, &n->full, lru) {
Christoph Lameter434e2452007-07-17 04:03:30 -07003345 validate_slab_slab(s, page, map);
Christoph Lameter53e15af2007-05-06 14:49:43 -07003346 count++;
3347 }
3348 if (count != atomic_long_read(&n->nr_slabs))
3349 printk(KERN_ERR "SLUB: %s %ld slabs counted but "
3350 "counter=%ld\n", s->name, count,
3351 atomic_long_read(&n->nr_slabs));
3352
3353out:
3354 spin_unlock_irqrestore(&n->list_lock, flags);
3355 return count;
3356}
3357
Christoph Lameter434e2452007-07-17 04:03:30 -07003358static long validate_slab_cache(struct kmem_cache *s)
Christoph Lameter53e15af2007-05-06 14:49:43 -07003359{
3360 int node;
3361 unsigned long count = 0;
Christoph Lameter205ab992008-04-14 19:11:40 +03003362 unsigned long *map = kmalloc(BITS_TO_LONGS(oo_objects(s->max)) *
Christoph Lameter434e2452007-07-17 04:03:30 -07003363 sizeof(unsigned long), GFP_KERNEL);
3364
3365 if (!map)
3366 return -ENOMEM;
Christoph Lameter53e15af2007-05-06 14:49:43 -07003367
3368 flush_all(s);
Christoph Lameterf64dc582007-10-16 01:25:33 -07003369 for_each_node_state(node, N_NORMAL_MEMORY) {
Christoph Lameter53e15af2007-05-06 14:49:43 -07003370 struct kmem_cache_node *n = get_node(s, node);
3371
Christoph Lameter434e2452007-07-17 04:03:30 -07003372 count += validate_slab_node(s, n, map);
Christoph Lameter53e15af2007-05-06 14:49:43 -07003373 }
Christoph Lameter434e2452007-07-17 04:03:30 -07003374 kfree(map);
Christoph Lameter53e15af2007-05-06 14:49:43 -07003375 return count;
3376}
3377
Christoph Lameterb3459702007-05-09 02:32:41 -07003378#ifdef SLUB_RESILIENCY_TEST
3379static void resiliency_test(void)
3380{
3381 u8 *p;
3382
3383 printk(KERN_ERR "SLUB resiliency testing\n");
3384 printk(KERN_ERR "-----------------------\n");
3385 printk(KERN_ERR "A. Corruption after allocation\n");
3386
3387 p = kzalloc(16, GFP_KERNEL);
3388 p[16] = 0x12;
3389 printk(KERN_ERR "\n1. kmalloc-16: Clobber Redzone/next pointer"
3390 " 0x12->0x%p\n\n", p + 16);
3391
3392 validate_slab_cache(kmalloc_caches + 4);
3393
3394 /* Hmmm... The next two are dangerous */
3395 p = kzalloc(32, GFP_KERNEL);
3396 p[32 + sizeof(void *)] = 0x34;
3397 printk(KERN_ERR "\n2. kmalloc-32: Clobber next pointer/next slab"
Ingo Molnar3adbefe2008-02-05 17:57:39 -08003398 " 0x34 -> -0x%p\n", p);
3399 printk(KERN_ERR
3400 "If allocated object is overwritten then not detectable\n\n");
Christoph Lameterb3459702007-05-09 02:32:41 -07003401
3402 validate_slab_cache(kmalloc_caches + 5);
3403 p = kzalloc(64, GFP_KERNEL);
3404 p += 64 + (get_cycles() & 0xff) * sizeof(void *);
3405 *p = 0x56;
3406 printk(KERN_ERR "\n3. kmalloc-64: corrupting random byte 0x56->0x%p\n",
3407 p);
Ingo Molnar3adbefe2008-02-05 17:57:39 -08003408 printk(KERN_ERR
3409 "If allocated object is overwritten then not detectable\n\n");
Christoph Lameterb3459702007-05-09 02:32:41 -07003410 validate_slab_cache(kmalloc_caches + 6);
3411
3412 printk(KERN_ERR "\nB. Corruption after free\n");
3413 p = kzalloc(128, GFP_KERNEL);
3414 kfree(p);
3415 *p = 0x78;
3416 printk(KERN_ERR "1. kmalloc-128: Clobber first word 0x78->0x%p\n\n", p);
3417 validate_slab_cache(kmalloc_caches + 7);
3418
3419 p = kzalloc(256, GFP_KERNEL);
3420 kfree(p);
3421 p[50] = 0x9a;
Ingo Molnar3adbefe2008-02-05 17:57:39 -08003422 printk(KERN_ERR "\n2. kmalloc-256: Clobber 50th byte 0x9a->0x%p\n\n",
3423 p);
Christoph Lameterb3459702007-05-09 02:32:41 -07003424 validate_slab_cache(kmalloc_caches + 8);
3425
3426 p = kzalloc(512, GFP_KERNEL);
3427 kfree(p);
3428 p[512] = 0xab;
3429 printk(KERN_ERR "\n3. kmalloc-512: Clobber redzone 0xab->0x%p\n\n", p);
3430 validate_slab_cache(kmalloc_caches + 9);
3431}
3432#else
3433static void resiliency_test(void) {};
3434#endif
3435
Christoph Lameter88a420e2007-05-06 14:49:45 -07003436/*
Christoph Lameter672bba32007-05-09 02:32:39 -07003437 * Generate lists of code addresses where slabcache objects are allocated
Christoph Lameter88a420e2007-05-06 14:49:45 -07003438 * and freed.
3439 */
3440
3441struct location {
3442 unsigned long count;
3443 void *addr;
Christoph Lameter45edfa52007-05-09 02:32:45 -07003444 long long sum_time;
3445 long min_time;
3446 long max_time;
3447 long min_pid;
3448 long max_pid;
3449 cpumask_t cpus;
3450 nodemask_t nodes;
Christoph Lameter88a420e2007-05-06 14:49:45 -07003451};
3452
3453struct loc_track {
3454 unsigned long max;
3455 unsigned long count;
3456 struct location *loc;
3457};
3458
3459static void free_loc_track(struct loc_track *t)
3460{
3461 if (t->max)
3462 free_pages((unsigned long)t->loc,
3463 get_order(sizeof(struct location) * t->max));
3464}
3465
Christoph Lameter68dff6a2007-07-17 04:03:20 -07003466static int alloc_loc_track(struct loc_track *t, unsigned long max, gfp_t flags)
Christoph Lameter88a420e2007-05-06 14:49:45 -07003467{
3468 struct location *l;
3469 int order;
3470
Christoph Lameter88a420e2007-05-06 14:49:45 -07003471 order = get_order(sizeof(struct location) * max);
3472
Christoph Lameter68dff6a2007-07-17 04:03:20 -07003473 l = (void *)__get_free_pages(flags, order);
Christoph Lameter88a420e2007-05-06 14:49:45 -07003474 if (!l)
3475 return 0;
3476
3477 if (t->count) {
3478 memcpy(l, t->loc, sizeof(struct location) * t->count);
3479 free_loc_track(t);
3480 }
3481 t->max = max;
3482 t->loc = l;
3483 return 1;
3484}
3485
3486static int add_location(struct loc_track *t, struct kmem_cache *s,
Christoph Lameter45edfa52007-05-09 02:32:45 -07003487 const struct track *track)
Christoph Lameter88a420e2007-05-06 14:49:45 -07003488{
3489 long start, end, pos;
3490 struct location *l;
3491 void *caddr;
Christoph Lameter45edfa52007-05-09 02:32:45 -07003492 unsigned long age = jiffies - track->when;
Christoph Lameter88a420e2007-05-06 14:49:45 -07003493
3494 start = -1;
3495 end = t->count;
3496
3497 for ( ; ; ) {
3498 pos = start + (end - start + 1) / 2;
3499
3500 /*
3501 * There is nothing at "end". If we end up there
3502 * we need to add something to before end.
3503 */
3504 if (pos == end)
3505 break;
3506
3507 caddr = t->loc[pos].addr;
Christoph Lameter45edfa52007-05-09 02:32:45 -07003508 if (track->addr == caddr) {
3509
3510 l = &t->loc[pos];
3511 l->count++;
3512 if (track->when) {
3513 l->sum_time += age;
3514 if (age < l->min_time)
3515 l->min_time = age;
3516 if (age > l->max_time)
3517 l->max_time = age;
3518
3519 if (track->pid < l->min_pid)
3520 l->min_pid = track->pid;
3521 if (track->pid > l->max_pid)
3522 l->max_pid = track->pid;
3523
3524 cpu_set(track->cpu, l->cpus);
3525 }
3526 node_set(page_to_nid(virt_to_page(track)), l->nodes);
Christoph Lameter88a420e2007-05-06 14:49:45 -07003527 return 1;
3528 }
3529
Christoph Lameter45edfa52007-05-09 02:32:45 -07003530 if (track->addr < caddr)
Christoph Lameter88a420e2007-05-06 14:49:45 -07003531 end = pos;
3532 else
3533 start = pos;
3534 }
3535
3536 /*
Christoph Lameter672bba32007-05-09 02:32:39 -07003537 * Not found. Insert new tracking element.
Christoph Lameter88a420e2007-05-06 14:49:45 -07003538 */
Christoph Lameter68dff6a2007-07-17 04:03:20 -07003539 if (t->count >= t->max && !alloc_loc_track(t, 2 * t->max, GFP_ATOMIC))
Christoph Lameter88a420e2007-05-06 14:49:45 -07003540 return 0;
3541
3542 l = t->loc + pos;
3543 if (pos < t->count)
3544 memmove(l + 1, l,
3545 (t->count - pos) * sizeof(struct location));
3546 t->count++;
3547 l->count = 1;
Christoph Lameter45edfa52007-05-09 02:32:45 -07003548 l->addr = track->addr;
3549 l->sum_time = age;
3550 l->min_time = age;
3551 l->max_time = age;
3552 l->min_pid = track->pid;
3553 l->max_pid = track->pid;
3554 cpus_clear(l->cpus);
3555 cpu_set(track->cpu, l->cpus);
3556 nodes_clear(l->nodes);
3557 node_set(page_to_nid(virt_to_page(track)), l->nodes);
Christoph Lameter88a420e2007-05-06 14:49:45 -07003558 return 1;
3559}
3560
3561static void process_slab(struct loc_track *t, struct kmem_cache *s,
3562 struct page *page, enum track_item alloc)
3563{
Christoph Lametera973e9d2008-03-01 13:40:44 -08003564 void *addr = page_address(page);
Christoph Lameter39b26462008-04-14 19:11:30 +03003565 DECLARE_BITMAP(map, page->objects);
Christoph Lameter88a420e2007-05-06 14:49:45 -07003566 void *p;
3567
Christoph Lameter39b26462008-04-14 19:11:30 +03003568 bitmap_zero(map, page->objects);
Christoph Lameter7656c722007-05-09 02:32:40 -07003569 for_each_free_object(p, s, page->freelist)
3570 set_bit(slab_index(p, s, addr), map);
Christoph Lameter88a420e2007-05-06 14:49:45 -07003571
Christoph Lameter224a88b2008-04-14 19:11:31 +03003572 for_each_object(p, s, addr, page->objects)
Christoph Lameter45edfa52007-05-09 02:32:45 -07003573 if (!test_bit(slab_index(p, s, addr), map))
3574 add_location(t, s, get_track(s, p, alloc));
Christoph Lameter88a420e2007-05-06 14:49:45 -07003575}
3576
3577static int list_locations(struct kmem_cache *s, char *buf,
3578 enum track_item alloc)
3579{
Harvey Harrisone374d482008-01-31 15:20:50 -08003580 int len = 0;
Christoph Lameter88a420e2007-05-06 14:49:45 -07003581 unsigned long i;
Christoph Lameter68dff6a2007-07-17 04:03:20 -07003582 struct loc_track t = { 0, 0, NULL };
Christoph Lameter88a420e2007-05-06 14:49:45 -07003583 int node;
3584
Christoph Lameter68dff6a2007-07-17 04:03:20 -07003585 if (!alloc_loc_track(&t, PAGE_SIZE / sizeof(struct location),
Andrew Mortonea3061d2007-10-16 01:26:09 -07003586 GFP_TEMPORARY))
Christoph Lameter68dff6a2007-07-17 04:03:20 -07003587 return sprintf(buf, "Out of memory\n");
Christoph Lameter88a420e2007-05-06 14:49:45 -07003588
3589 /* Push back cpu slabs */
3590 flush_all(s);
3591
Christoph Lameterf64dc582007-10-16 01:25:33 -07003592 for_each_node_state(node, N_NORMAL_MEMORY) {
Christoph Lameter88a420e2007-05-06 14:49:45 -07003593 struct kmem_cache_node *n = get_node(s, node);
3594 unsigned long flags;
3595 struct page *page;
3596
Christoph Lameter9e869432007-08-22 14:01:56 -07003597 if (!atomic_long_read(&n->nr_slabs))
Christoph Lameter88a420e2007-05-06 14:49:45 -07003598 continue;
3599
3600 spin_lock_irqsave(&n->list_lock, flags);
3601 list_for_each_entry(page, &n->partial, lru)
3602 process_slab(&t, s, page, alloc);
3603 list_for_each_entry(page, &n->full, lru)
3604 process_slab(&t, s, page, alloc);
3605 spin_unlock_irqrestore(&n->list_lock, flags);
3606 }
3607
3608 for (i = 0; i < t.count; i++) {
Christoph Lameter45edfa52007-05-09 02:32:45 -07003609 struct location *l = &t.loc[i];
Christoph Lameter88a420e2007-05-06 14:49:45 -07003610
Harvey Harrisone374d482008-01-31 15:20:50 -08003611 if (len > PAGE_SIZE - 100)
Christoph Lameter88a420e2007-05-06 14:49:45 -07003612 break;
Harvey Harrisone374d482008-01-31 15:20:50 -08003613 len += sprintf(buf + len, "%7ld ", l->count);
Christoph Lameter45edfa52007-05-09 02:32:45 -07003614
3615 if (l->addr)
Harvey Harrisone374d482008-01-31 15:20:50 -08003616 len += sprint_symbol(buf + len, (unsigned long)l->addr);
Christoph Lameter88a420e2007-05-06 14:49:45 -07003617 else
Harvey Harrisone374d482008-01-31 15:20:50 -08003618 len += sprintf(buf + len, "<not-available>");
Christoph Lameter45edfa52007-05-09 02:32:45 -07003619
3620 if (l->sum_time != l->min_time) {
3621 unsigned long remainder;
3622
Harvey Harrisone374d482008-01-31 15:20:50 -08003623 len += sprintf(buf + len, " age=%ld/%ld/%ld",
Christoph Lameter45edfa52007-05-09 02:32:45 -07003624 l->min_time,
3625 div_long_long_rem(l->sum_time, l->count, &remainder),
3626 l->max_time);
3627 } else
Harvey Harrisone374d482008-01-31 15:20:50 -08003628 len += sprintf(buf + len, " age=%ld",
Christoph Lameter45edfa52007-05-09 02:32:45 -07003629 l->min_time);
3630
3631 if (l->min_pid != l->max_pid)
Harvey Harrisone374d482008-01-31 15:20:50 -08003632 len += sprintf(buf + len, " pid=%ld-%ld",
Christoph Lameter45edfa52007-05-09 02:32:45 -07003633 l->min_pid, l->max_pid);
3634 else
Harvey Harrisone374d482008-01-31 15:20:50 -08003635 len += sprintf(buf + len, " pid=%ld",
Christoph Lameter45edfa52007-05-09 02:32:45 -07003636 l->min_pid);
3637
Christoph Lameter84966342007-06-23 17:16:32 -07003638 if (num_online_cpus() > 1 && !cpus_empty(l->cpus) &&
Harvey Harrisone374d482008-01-31 15:20:50 -08003639 len < PAGE_SIZE - 60) {
3640 len += sprintf(buf + len, " cpus=");
3641 len += cpulist_scnprintf(buf + len, PAGE_SIZE - len - 50,
Christoph Lameter45edfa52007-05-09 02:32:45 -07003642 l->cpus);
3643 }
3644
Christoph Lameter84966342007-06-23 17:16:32 -07003645 if (num_online_nodes() > 1 && !nodes_empty(l->nodes) &&
Harvey Harrisone374d482008-01-31 15:20:50 -08003646 len < PAGE_SIZE - 60) {
3647 len += sprintf(buf + len, " nodes=");
3648 len += nodelist_scnprintf(buf + len, PAGE_SIZE - len - 50,
Christoph Lameter45edfa52007-05-09 02:32:45 -07003649 l->nodes);
3650 }
3651
Harvey Harrisone374d482008-01-31 15:20:50 -08003652 len += sprintf(buf + len, "\n");
Christoph Lameter88a420e2007-05-06 14:49:45 -07003653 }
3654
3655 free_loc_track(&t);
3656 if (!t.count)
Harvey Harrisone374d482008-01-31 15:20:50 -08003657 len += sprintf(buf, "No data\n");
3658 return len;
Christoph Lameter88a420e2007-05-06 14:49:45 -07003659}
3660
Christoph Lameter81819f02007-05-06 14:49:36 -07003661enum slab_stat_type {
Christoph Lameter205ab992008-04-14 19:11:40 +03003662 SL_ALL, /* All slabs */
3663 SL_PARTIAL, /* Only partially allocated slabs */
3664 SL_CPU, /* Only slabs used for cpu caches */
3665 SL_OBJECTS, /* Determine allocated objects not slabs */
3666 SL_TOTAL /* Determine object capacity not slabs */
Christoph Lameter81819f02007-05-06 14:49:36 -07003667};
3668
Christoph Lameter205ab992008-04-14 19:11:40 +03003669#define SO_ALL (1 << SL_ALL)
Christoph Lameter81819f02007-05-06 14:49:36 -07003670#define SO_PARTIAL (1 << SL_PARTIAL)
3671#define SO_CPU (1 << SL_CPU)
3672#define SO_OBJECTS (1 << SL_OBJECTS)
Christoph Lameter205ab992008-04-14 19:11:40 +03003673#define SO_TOTAL (1 << SL_TOTAL)
Christoph Lameter81819f02007-05-06 14:49:36 -07003674
Cyrill Gorcunov62e5c4b2008-03-02 23:28:24 +03003675static ssize_t show_slab_objects(struct kmem_cache *s,
3676 char *buf, unsigned long flags)
Christoph Lameter81819f02007-05-06 14:49:36 -07003677{
3678 unsigned long total = 0;
Christoph Lameter81819f02007-05-06 14:49:36 -07003679 int node;
3680 int x;
3681 unsigned long *nodes;
3682 unsigned long *per_cpu;
3683
3684 nodes = kzalloc(2 * sizeof(unsigned long) * nr_node_ids, GFP_KERNEL);
Cyrill Gorcunov62e5c4b2008-03-02 23:28:24 +03003685 if (!nodes)
3686 return -ENOMEM;
Christoph Lameter81819f02007-05-06 14:49:36 -07003687 per_cpu = nodes + nr_node_ids;
3688
Christoph Lameter205ab992008-04-14 19:11:40 +03003689 if (flags & SO_CPU) {
3690 int cpu;
Christoph Lameter81819f02007-05-06 14:49:36 -07003691
Christoph Lameter205ab992008-04-14 19:11:40 +03003692 for_each_possible_cpu(cpu) {
3693 struct kmem_cache_cpu *c = get_cpu_slab(s, cpu);
Christoph Lameterdfb4f092007-10-16 01:26:05 -07003694
Christoph Lameter205ab992008-04-14 19:11:40 +03003695 if (!c || c->node < 0)
3696 continue;
3697
3698 if (c->page) {
3699 if (flags & SO_TOTAL)
3700 x = c->page->objects;
3701 else if (flags & SO_OBJECTS)
3702 x = c->page->inuse;
Christoph Lameter81819f02007-05-06 14:49:36 -07003703 else
3704 x = 1;
Christoph Lameter205ab992008-04-14 19:11:40 +03003705
Christoph Lameter81819f02007-05-06 14:49:36 -07003706 total += x;
Christoph Lameter205ab992008-04-14 19:11:40 +03003707 nodes[c->node] += x;
Christoph Lameter81819f02007-05-06 14:49:36 -07003708 }
Christoph Lameter205ab992008-04-14 19:11:40 +03003709 per_cpu[c->node]++;
Christoph Lameter81819f02007-05-06 14:49:36 -07003710 }
3711 }
3712
Christoph Lameter205ab992008-04-14 19:11:40 +03003713 if (flags & SO_ALL) {
3714 for_each_node_state(node, N_NORMAL_MEMORY) {
3715 struct kmem_cache_node *n = get_node(s, node);
Christoph Lameter81819f02007-05-06 14:49:36 -07003716
Christoph Lameter205ab992008-04-14 19:11:40 +03003717 if (flags & SO_TOTAL)
3718 x = atomic_long_read(&n->total_objects);
3719 else if (flags & SO_OBJECTS)
3720 x = atomic_long_read(&n->total_objects) -
3721 count_partial(n, count_free);
3722
3723 else
3724 x = atomic_long_read(&n->nr_slabs);
3725 total += x;
3726 nodes[node] += x;
3727 }
3728
3729 } else if (flags & SO_PARTIAL) {
3730 for_each_node_state(node, N_NORMAL_MEMORY) {
3731 struct kmem_cache_node *n = get_node(s, node);
3732
3733 if (flags & SO_TOTAL)
3734 x = count_partial(n, count_total);
3735 else if (flags & SO_OBJECTS)
3736 x = count_partial(n, count_inuse);
Christoph Lameter81819f02007-05-06 14:49:36 -07003737 else
3738 x = n->nr_partial;
3739 total += x;
3740 nodes[node] += x;
3741 }
Christoph Lameter81819f02007-05-06 14:49:36 -07003742 }
Christoph Lameter81819f02007-05-06 14:49:36 -07003743 x = sprintf(buf, "%lu", total);
3744#ifdef CONFIG_NUMA
Christoph Lameterf64dc582007-10-16 01:25:33 -07003745 for_each_node_state(node, N_NORMAL_MEMORY)
Christoph Lameter81819f02007-05-06 14:49:36 -07003746 if (nodes[node])
3747 x += sprintf(buf + x, " N%d=%lu",
3748 node, nodes[node]);
3749#endif
3750 kfree(nodes);
3751 return x + sprintf(buf + x, "\n");
3752}
3753
3754static int any_slab_objects(struct kmem_cache *s)
3755{
3756 int node;
Christoph Lameterdfb4f092007-10-16 01:26:05 -07003757
3758 for_each_online_node(node) {
Christoph Lameter81819f02007-05-06 14:49:36 -07003759 struct kmem_cache_node *n = get_node(s, node);
3760
Christoph Lameterdfb4f092007-10-16 01:26:05 -07003761 if (!n)
3762 continue;
3763
Christoph Lameter31d33ba2008-04-14 19:11:41 +03003764 if (atomic_read(&n->total_objects))
Christoph Lameter81819f02007-05-06 14:49:36 -07003765 return 1;
3766 }
3767 return 0;
3768}
3769
3770#define to_slab_attr(n) container_of(n, struct slab_attribute, attr)
3771#define to_slab(n) container_of(n, struct kmem_cache, kobj);
3772
3773struct slab_attribute {
3774 struct attribute attr;
3775 ssize_t (*show)(struct kmem_cache *s, char *buf);
3776 ssize_t (*store)(struct kmem_cache *s, const char *x, size_t count);
3777};
3778
3779#define SLAB_ATTR_RO(_name) \
3780 static struct slab_attribute _name##_attr = __ATTR_RO(_name)
3781
3782#define SLAB_ATTR(_name) \
3783 static struct slab_attribute _name##_attr = \
3784 __ATTR(_name, 0644, _name##_show, _name##_store)
3785
Christoph Lameter81819f02007-05-06 14:49:36 -07003786static ssize_t slab_size_show(struct kmem_cache *s, char *buf)
3787{
3788 return sprintf(buf, "%d\n", s->size);
3789}
3790SLAB_ATTR_RO(slab_size);
3791
3792static ssize_t align_show(struct kmem_cache *s, char *buf)
3793{
3794 return sprintf(buf, "%d\n", s->align);
3795}
3796SLAB_ATTR_RO(align);
3797
3798static ssize_t object_size_show(struct kmem_cache *s, char *buf)
3799{
3800 return sprintf(buf, "%d\n", s->objsize);
3801}
3802SLAB_ATTR_RO(object_size);
3803
3804static ssize_t objs_per_slab_show(struct kmem_cache *s, char *buf)
3805{
Christoph Lameter834f3d12008-04-14 19:11:31 +03003806 return sprintf(buf, "%d\n", oo_objects(s->oo));
Christoph Lameter81819f02007-05-06 14:49:36 -07003807}
3808SLAB_ATTR_RO(objs_per_slab);
3809
Christoph Lameter06b285d2008-04-14 19:11:41 +03003810static ssize_t order_store(struct kmem_cache *s,
3811 const char *buf, size_t length)
3812{
3813 int order = simple_strtoul(buf, NULL, 10);
3814
3815 if (order > slub_max_order || order < slub_min_order)
3816 return -EINVAL;
3817
3818 calculate_sizes(s, order);
3819 return length;
3820}
3821
Christoph Lameter81819f02007-05-06 14:49:36 -07003822static ssize_t order_show(struct kmem_cache *s, char *buf)
3823{
Christoph Lameter834f3d12008-04-14 19:11:31 +03003824 return sprintf(buf, "%d\n", oo_order(s->oo));
Christoph Lameter81819f02007-05-06 14:49:36 -07003825}
Christoph Lameter06b285d2008-04-14 19:11:41 +03003826SLAB_ATTR(order);
Christoph Lameter81819f02007-05-06 14:49:36 -07003827
3828static ssize_t ctor_show(struct kmem_cache *s, char *buf)
3829{
3830 if (s->ctor) {
3831 int n = sprint_symbol(buf, (unsigned long)s->ctor);
3832
3833 return n + sprintf(buf + n, "\n");
3834 }
3835 return 0;
3836}
3837SLAB_ATTR_RO(ctor);
3838
Christoph Lameter81819f02007-05-06 14:49:36 -07003839static ssize_t aliases_show(struct kmem_cache *s, char *buf)
3840{
3841 return sprintf(buf, "%d\n", s->refcount - 1);
3842}
3843SLAB_ATTR_RO(aliases);
3844
3845static ssize_t slabs_show(struct kmem_cache *s, char *buf)
3846{
Christoph Lameter205ab992008-04-14 19:11:40 +03003847 return show_slab_objects(s, buf, SO_ALL);
Christoph Lameter81819f02007-05-06 14:49:36 -07003848}
3849SLAB_ATTR_RO(slabs);
3850
3851static ssize_t partial_show(struct kmem_cache *s, char *buf)
3852{
Christoph Lameterd9acf4b2008-02-15 15:22:21 -08003853 return show_slab_objects(s, buf, SO_PARTIAL);
Christoph Lameter81819f02007-05-06 14:49:36 -07003854}
3855SLAB_ATTR_RO(partial);
3856
3857static ssize_t cpu_slabs_show(struct kmem_cache *s, char *buf)
3858{
Christoph Lameterd9acf4b2008-02-15 15:22:21 -08003859 return show_slab_objects(s, buf, SO_CPU);
Christoph Lameter81819f02007-05-06 14:49:36 -07003860}
3861SLAB_ATTR_RO(cpu_slabs);
3862
3863static ssize_t objects_show(struct kmem_cache *s, char *buf)
3864{
Christoph Lameter205ab992008-04-14 19:11:40 +03003865 return show_slab_objects(s, buf, SO_ALL|SO_OBJECTS);
Christoph Lameter81819f02007-05-06 14:49:36 -07003866}
3867SLAB_ATTR_RO(objects);
3868
Christoph Lameter205ab992008-04-14 19:11:40 +03003869static ssize_t objects_partial_show(struct kmem_cache *s, char *buf)
3870{
3871 return show_slab_objects(s, buf, SO_PARTIAL|SO_OBJECTS);
3872}
3873SLAB_ATTR_RO(objects_partial);
3874
3875static ssize_t total_objects_show(struct kmem_cache *s, char *buf)
3876{
3877 return show_slab_objects(s, buf, SO_ALL|SO_TOTAL);
3878}
3879SLAB_ATTR_RO(total_objects);
3880
Christoph Lameter81819f02007-05-06 14:49:36 -07003881static ssize_t sanity_checks_show(struct kmem_cache *s, char *buf)
3882{
3883 return sprintf(buf, "%d\n", !!(s->flags & SLAB_DEBUG_FREE));
3884}
3885
3886static ssize_t sanity_checks_store(struct kmem_cache *s,
3887 const char *buf, size_t length)
3888{
3889 s->flags &= ~SLAB_DEBUG_FREE;
3890 if (buf[0] == '1')
3891 s->flags |= SLAB_DEBUG_FREE;
3892 return length;
3893}
3894SLAB_ATTR(sanity_checks);
3895
3896static ssize_t trace_show(struct kmem_cache *s, char *buf)
3897{
3898 return sprintf(buf, "%d\n", !!(s->flags & SLAB_TRACE));
3899}
3900
3901static ssize_t trace_store(struct kmem_cache *s, const char *buf,
3902 size_t length)
3903{
3904 s->flags &= ~SLAB_TRACE;
3905 if (buf[0] == '1')
3906 s->flags |= SLAB_TRACE;
3907 return length;
3908}
3909SLAB_ATTR(trace);
3910
3911static ssize_t reclaim_account_show(struct kmem_cache *s, char *buf)
3912{
3913 return sprintf(buf, "%d\n", !!(s->flags & SLAB_RECLAIM_ACCOUNT));
3914}
3915
3916static ssize_t reclaim_account_store(struct kmem_cache *s,
3917 const char *buf, size_t length)
3918{
3919 s->flags &= ~SLAB_RECLAIM_ACCOUNT;
3920 if (buf[0] == '1')
3921 s->flags |= SLAB_RECLAIM_ACCOUNT;
3922 return length;
3923}
3924SLAB_ATTR(reclaim_account);
3925
3926static ssize_t hwcache_align_show(struct kmem_cache *s, char *buf)
3927{
Christoph Lameter5af60832007-05-06 14:49:56 -07003928 return sprintf(buf, "%d\n", !!(s->flags & SLAB_HWCACHE_ALIGN));
Christoph Lameter81819f02007-05-06 14:49:36 -07003929}
3930SLAB_ATTR_RO(hwcache_align);
3931
3932#ifdef CONFIG_ZONE_DMA
3933static ssize_t cache_dma_show(struct kmem_cache *s, char *buf)
3934{
3935 return sprintf(buf, "%d\n", !!(s->flags & SLAB_CACHE_DMA));
3936}
3937SLAB_ATTR_RO(cache_dma);
3938#endif
3939
3940static ssize_t destroy_by_rcu_show(struct kmem_cache *s, char *buf)
3941{
3942 return sprintf(buf, "%d\n", !!(s->flags & SLAB_DESTROY_BY_RCU));
3943}
3944SLAB_ATTR_RO(destroy_by_rcu);
3945
3946static ssize_t red_zone_show(struct kmem_cache *s, char *buf)
3947{
3948 return sprintf(buf, "%d\n", !!(s->flags & SLAB_RED_ZONE));
3949}
3950
3951static ssize_t red_zone_store(struct kmem_cache *s,
3952 const char *buf, size_t length)
3953{
3954 if (any_slab_objects(s))
3955 return -EBUSY;
3956
3957 s->flags &= ~SLAB_RED_ZONE;
3958 if (buf[0] == '1')
3959 s->flags |= SLAB_RED_ZONE;
Christoph Lameter06b285d2008-04-14 19:11:41 +03003960 calculate_sizes(s, -1);
Christoph Lameter81819f02007-05-06 14:49:36 -07003961 return length;
3962}
3963SLAB_ATTR(red_zone);
3964
3965static ssize_t poison_show(struct kmem_cache *s, char *buf)
3966{
3967 return sprintf(buf, "%d\n", !!(s->flags & SLAB_POISON));
3968}
3969
3970static ssize_t poison_store(struct kmem_cache *s,
3971 const char *buf, size_t length)
3972{
3973 if (any_slab_objects(s))
3974 return -EBUSY;
3975
3976 s->flags &= ~SLAB_POISON;
3977 if (buf[0] == '1')
3978 s->flags |= SLAB_POISON;
Christoph Lameter06b285d2008-04-14 19:11:41 +03003979 calculate_sizes(s, -1);
Christoph Lameter81819f02007-05-06 14:49:36 -07003980 return length;
3981}
3982SLAB_ATTR(poison);
3983
3984static ssize_t store_user_show(struct kmem_cache *s, char *buf)
3985{
3986 return sprintf(buf, "%d\n", !!(s->flags & SLAB_STORE_USER));
3987}
3988
3989static ssize_t store_user_store(struct kmem_cache *s,
3990 const char *buf, size_t length)
3991{
3992 if (any_slab_objects(s))
3993 return -EBUSY;
3994
3995 s->flags &= ~SLAB_STORE_USER;
3996 if (buf[0] == '1')
3997 s->flags |= SLAB_STORE_USER;
Christoph Lameter06b285d2008-04-14 19:11:41 +03003998 calculate_sizes(s, -1);
Christoph Lameter81819f02007-05-06 14:49:36 -07003999 return length;
4000}
4001SLAB_ATTR(store_user);
4002
Christoph Lameter53e15af2007-05-06 14:49:43 -07004003static ssize_t validate_show(struct kmem_cache *s, char *buf)
4004{
4005 return 0;
4006}
4007
4008static ssize_t validate_store(struct kmem_cache *s,
4009 const char *buf, size_t length)
4010{
Christoph Lameter434e2452007-07-17 04:03:30 -07004011 int ret = -EINVAL;
4012
4013 if (buf[0] == '1') {
4014 ret = validate_slab_cache(s);
4015 if (ret >= 0)
4016 ret = length;
4017 }
4018 return ret;
Christoph Lameter53e15af2007-05-06 14:49:43 -07004019}
4020SLAB_ATTR(validate);
4021
Christoph Lameter2086d262007-05-06 14:49:46 -07004022static ssize_t shrink_show(struct kmem_cache *s, char *buf)
4023{
4024 return 0;
4025}
4026
4027static ssize_t shrink_store(struct kmem_cache *s,
4028 const char *buf, size_t length)
4029{
4030 if (buf[0] == '1') {
4031 int rc = kmem_cache_shrink(s);
4032
4033 if (rc)
4034 return rc;
4035 } else
4036 return -EINVAL;
4037 return length;
4038}
4039SLAB_ATTR(shrink);
4040
Christoph Lameter88a420e2007-05-06 14:49:45 -07004041static ssize_t alloc_calls_show(struct kmem_cache *s, char *buf)
4042{
4043 if (!(s->flags & SLAB_STORE_USER))
4044 return -ENOSYS;
4045 return list_locations(s, buf, TRACK_ALLOC);
4046}
4047SLAB_ATTR_RO(alloc_calls);
4048
4049static ssize_t free_calls_show(struct kmem_cache *s, char *buf)
4050{
4051 if (!(s->flags & SLAB_STORE_USER))
4052 return -ENOSYS;
4053 return list_locations(s, buf, TRACK_FREE);
4054}
4055SLAB_ATTR_RO(free_calls);
4056
Christoph Lameter81819f02007-05-06 14:49:36 -07004057#ifdef CONFIG_NUMA
Christoph Lameter98246012008-01-07 23:20:26 -08004058static ssize_t remote_node_defrag_ratio_show(struct kmem_cache *s, char *buf)
Christoph Lameter81819f02007-05-06 14:49:36 -07004059{
Christoph Lameter98246012008-01-07 23:20:26 -08004060 return sprintf(buf, "%d\n", s->remote_node_defrag_ratio / 10);
Christoph Lameter81819f02007-05-06 14:49:36 -07004061}
4062
Christoph Lameter98246012008-01-07 23:20:26 -08004063static ssize_t remote_node_defrag_ratio_store(struct kmem_cache *s,
Christoph Lameter81819f02007-05-06 14:49:36 -07004064 const char *buf, size_t length)
4065{
4066 int n = simple_strtoul(buf, NULL, 10);
4067
4068 if (n < 100)
Christoph Lameter98246012008-01-07 23:20:26 -08004069 s->remote_node_defrag_ratio = n * 10;
Christoph Lameter81819f02007-05-06 14:49:36 -07004070 return length;
4071}
Christoph Lameter98246012008-01-07 23:20:26 -08004072SLAB_ATTR(remote_node_defrag_ratio);
Christoph Lameter81819f02007-05-06 14:49:36 -07004073#endif
4074
Christoph Lameter8ff12cf2008-02-07 17:47:41 -08004075#ifdef CONFIG_SLUB_STATS
Christoph Lameter8ff12cf2008-02-07 17:47:41 -08004076static int show_stat(struct kmem_cache *s, char *buf, enum stat_item si)
4077{
4078 unsigned long sum = 0;
4079 int cpu;
4080 int len;
4081 int *data = kmalloc(nr_cpu_ids * sizeof(int), GFP_KERNEL);
4082
4083 if (!data)
4084 return -ENOMEM;
4085
4086 for_each_online_cpu(cpu) {
4087 unsigned x = get_cpu_slab(s, cpu)->stat[si];
4088
4089 data[cpu] = x;
4090 sum += x;
4091 }
4092
4093 len = sprintf(buf, "%lu", sum);
4094
Christoph Lameter50ef37b2008-04-14 18:52:05 +03004095#ifdef CONFIG_SMP
Christoph Lameter8ff12cf2008-02-07 17:47:41 -08004096 for_each_online_cpu(cpu) {
4097 if (data[cpu] && len < PAGE_SIZE - 20)
Christoph Lameter50ef37b2008-04-14 18:52:05 +03004098 len += sprintf(buf + len, " C%d=%u", cpu, data[cpu]);
Christoph Lameter8ff12cf2008-02-07 17:47:41 -08004099 }
Christoph Lameter50ef37b2008-04-14 18:52:05 +03004100#endif
Christoph Lameter8ff12cf2008-02-07 17:47:41 -08004101 kfree(data);
4102 return len + sprintf(buf + len, "\n");
4103}
4104
4105#define STAT_ATTR(si, text) \
4106static ssize_t text##_show(struct kmem_cache *s, char *buf) \
4107{ \
4108 return show_stat(s, buf, si); \
4109} \
4110SLAB_ATTR_RO(text); \
4111
4112STAT_ATTR(ALLOC_FASTPATH, alloc_fastpath);
4113STAT_ATTR(ALLOC_SLOWPATH, alloc_slowpath);
4114STAT_ATTR(FREE_FASTPATH, free_fastpath);
4115STAT_ATTR(FREE_SLOWPATH, free_slowpath);
4116STAT_ATTR(FREE_FROZEN, free_frozen);
4117STAT_ATTR(FREE_ADD_PARTIAL, free_add_partial);
4118STAT_ATTR(FREE_REMOVE_PARTIAL, free_remove_partial);
4119STAT_ATTR(ALLOC_FROM_PARTIAL, alloc_from_partial);
4120STAT_ATTR(ALLOC_SLAB, alloc_slab);
4121STAT_ATTR(ALLOC_REFILL, alloc_refill);
4122STAT_ATTR(FREE_SLAB, free_slab);
4123STAT_ATTR(CPUSLAB_FLUSH, cpuslab_flush);
4124STAT_ATTR(DEACTIVATE_FULL, deactivate_full);
4125STAT_ATTR(DEACTIVATE_EMPTY, deactivate_empty);
4126STAT_ATTR(DEACTIVATE_TO_HEAD, deactivate_to_head);
4127STAT_ATTR(DEACTIVATE_TO_TAIL, deactivate_to_tail);
4128STAT_ATTR(DEACTIVATE_REMOTE_FREES, deactivate_remote_frees);
Christoph Lameter65c33762008-04-14 19:11:40 +03004129STAT_ATTR(ORDER_FALLBACK, order_fallback);
Christoph Lameter8ff12cf2008-02-07 17:47:41 -08004130#endif
4131
Pekka Enberg06428782008-01-07 23:20:27 -08004132static struct attribute *slab_attrs[] = {
Christoph Lameter81819f02007-05-06 14:49:36 -07004133 &slab_size_attr.attr,
4134 &object_size_attr.attr,
4135 &objs_per_slab_attr.attr,
4136 &order_attr.attr,
4137 &objects_attr.attr,
Christoph Lameter205ab992008-04-14 19:11:40 +03004138 &objects_partial_attr.attr,
4139 &total_objects_attr.attr,
Christoph Lameter81819f02007-05-06 14:49:36 -07004140 &slabs_attr.attr,
4141 &partial_attr.attr,
4142 &cpu_slabs_attr.attr,
4143 &ctor_attr.attr,
Christoph Lameter81819f02007-05-06 14:49:36 -07004144 &aliases_attr.attr,
4145 &align_attr.attr,
4146 &sanity_checks_attr.attr,
4147 &trace_attr.attr,
4148 &hwcache_align_attr.attr,
4149 &reclaim_account_attr.attr,
4150 &destroy_by_rcu_attr.attr,
4151 &red_zone_attr.attr,
4152 &poison_attr.attr,
4153 &store_user_attr.attr,
Christoph Lameter53e15af2007-05-06 14:49:43 -07004154 &validate_attr.attr,
Christoph Lameter2086d262007-05-06 14:49:46 -07004155 &shrink_attr.attr,
Christoph Lameter88a420e2007-05-06 14:49:45 -07004156 &alloc_calls_attr.attr,
4157 &free_calls_attr.attr,
Christoph Lameter81819f02007-05-06 14:49:36 -07004158#ifdef CONFIG_ZONE_DMA
4159 &cache_dma_attr.attr,
4160#endif
4161#ifdef CONFIG_NUMA
Christoph Lameter98246012008-01-07 23:20:26 -08004162 &remote_node_defrag_ratio_attr.attr,
Christoph Lameter81819f02007-05-06 14:49:36 -07004163#endif
Christoph Lameter8ff12cf2008-02-07 17:47:41 -08004164#ifdef CONFIG_SLUB_STATS
4165 &alloc_fastpath_attr.attr,
4166 &alloc_slowpath_attr.attr,
4167 &free_fastpath_attr.attr,
4168 &free_slowpath_attr.attr,
4169 &free_frozen_attr.attr,
4170 &free_add_partial_attr.attr,
4171 &free_remove_partial_attr.attr,
4172 &alloc_from_partial_attr.attr,
4173 &alloc_slab_attr.attr,
4174 &alloc_refill_attr.attr,
4175 &free_slab_attr.attr,
4176 &cpuslab_flush_attr.attr,
4177 &deactivate_full_attr.attr,
4178 &deactivate_empty_attr.attr,
4179 &deactivate_to_head_attr.attr,
4180 &deactivate_to_tail_attr.attr,
4181 &deactivate_remote_frees_attr.attr,
Christoph Lameter65c33762008-04-14 19:11:40 +03004182 &order_fallback_attr.attr,
Christoph Lameter8ff12cf2008-02-07 17:47:41 -08004183#endif
Christoph Lameter81819f02007-05-06 14:49:36 -07004184 NULL
4185};
4186
4187static struct attribute_group slab_attr_group = {
4188 .attrs = slab_attrs,
4189};
4190
4191static ssize_t slab_attr_show(struct kobject *kobj,
4192 struct attribute *attr,
4193 char *buf)
4194{
4195 struct slab_attribute *attribute;
4196 struct kmem_cache *s;
4197 int err;
4198
4199 attribute = to_slab_attr(attr);
4200 s = to_slab(kobj);
4201
4202 if (!attribute->show)
4203 return -EIO;
4204
4205 err = attribute->show(s, buf);
4206
4207 return err;
4208}
4209
4210static ssize_t slab_attr_store(struct kobject *kobj,
4211 struct attribute *attr,
4212 const char *buf, size_t len)
4213{
4214 struct slab_attribute *attribute;
4215 struct kmem_cache *s;
4216 int err;
4217
4218 attribute = to_slab_attr(attr);
4219 s = to_slab(kobj);
4220
4221 if (!attribute->store)
4222 return -EIO;
4223
4224 err = attribute->store(s, buf, len);
4225
4226 return err;
4227}
4228
Christoph Lameter151c6022008-01-07 22:29:05 -08004229static void kmem_cache_release(struct kobject *kobj)
4230{
4231 struct kmem_cache *s = to_slab(kobj);
4232
4233 kfree(s);
4234}
4235
Christoph Lameter81819f02007-05-06 14:49:36 -07004236static struct sysfs_ops slab_sysfs_ops = {
4237 .show = slab_attr_show,
4238 .store = slab_attr_store,
4239};
4240
4241static struct kobj_type slab_ktype = {
4242 .sysfs_ops = &slab_sysfs_ops,
Christoph Lameter151c6022008-01-07 22:29:05 -08004243 .release = kmem_cache_release
Christoph Lameter81819f02007-05-06 14:49:36 -07004244};
4245
4246static int uevent_filter(struct kset *kset, struct kobject *kobj)
4247{
4248 struct kobj_type *ktype = get_ktype(kobj);
4249
4250 if (ktype == &slab_ktype)
4251 return 1;
4252 return 0;
4253}
4254
4255static struct kset_uevent_ops slab_uevent_ops = {
4256 .filter = uevent_filter,
4257};
4258
Greg Kroah-Hartman27c3a312007-11-01 09:29:06 -06004259static struct kset *slab_kset;
Christoph Lameter81819f02007-05-06 14:49:36 -07004260
4261#define ID_STR_LENGTH 64
4262
4263/* Create a unique string id for a slab cache:
Christoph Lameter6446faa2008-02-15 23:45:26 -08004264 *
4265 * Format :[flags-]size
Christoph Lameter81819f02007-05-06 14:49:36 -07004266 */
4267static char *create_unique_id(struct kmem_cache *s)
4268{
4269 char *name = kmalloc(ID_STR_LENGTH, GFP_KERNEL);
4270 char *p = name;
4271
4272 BUG_ON(!name);
4273
4274 *p++ = ':';
4275 /*
4276 * First flags affecting slabcache operations. We will only
4277 * get here for aliasable slabs so we do not need to support
4278 * too many flags. The flags here must cover all flags that
4279 * are matched during merging to guarantee that the id is
4280 * unique.
4281 */
4282 if (s->flags & SLAB_CACHE_DMA)
4283 *p++ = 'd';
4284 if (s->flags & SLAB_RECLAIM_ACCOUNT)
4285 *p++ = 'a';
4286 if (s->flags & SLAB_DEBUG_FREE)
4287 *p++ = 'F';
4288 if (p != name + 1)
4289 *p++ = '-';
4290 p += sprintf(p, "%07d", s->size);
4291 BUG_ON(p > name + ID_STR_LENGTH - 1);
4292 return name;
4293}
4294
4295static int sysfs_slab_add(struct kmem_cache *s)
4296{
4297 int err;
4298 const char *name;
4299 int unmergeable;
4300
4301 if (slab_state < SYSFS)
4302 /* Defer until later */
4303 return 0;
4304
4305 unmergeable = slab_unmergeable(s);
4306 if (unmergeable) {
4307 /*
4308 * Slabcache can never be merged so we can use the name proper.
4309 * This is typically the case for debug situations. In that
4310 * case we can catch duplicate names easily.
4311 */
Greg Kroah-Hartman27c3a312007-11-01 09:29:06 -06004312 sysfs_remove_link(&slab_kset->kobj, s->name);
Christoph Lameter81819f02007-05-06 14:49:36 -07004313 name = s->name;
4314 } else {
4315 /*
4316 * Create a unique name for the slab as a target
4317 * for the symlinks.
4318 */
4319 name = create_unique_id(s);
4320 }
4321
Greg Kroah-Hartman27c3a312007-11-01 09:29:06 -06004322 s->kobj.kset = slab_kset;
Greg Kroah-Hartman1eada112007-12-17 23:05:35 -07004323 err = kobject_init_and_add(&s->kobj, &slab_ktype, NULL, name);
4324 if (err) {
4325 kobject_put(&s->kobj);
Christoph Lameter81819f02007-05-06 14:49:36 -07004326 return err;
Greg Kroah-Hartman1eada112007-12-17 23:05:35 -07004327 }
Christoph Lameter81819f02007-05-06 14:49:36 -07004328
4329 err = sysfs_create_group(&s->kobj, &slab_attr_group);
4330 if (err)
4331 return err;
4332 kobject_uevent(&s->kobj, KOBJ_ADD);
4333 if (!unmergeable) {
4334 /* Setup first alias */
4335 sysfs_slab_alias(s, s->name);
4336 kfree(name);
4337 }
4338 return 0;
4339}
4340
4341static void sysfs_slab_remove(struct kmem_cache *s)
4342{
4343 kobject_uevent(&s->kobj, KOBJ_REMOVE);
4344 kobject_del(&s->kobj);
Christoph Lameter151c6022008-01-07 22:29:05 -08004345 kobject_put(&s->kobj);
Christoph Lameter81819f02007-05-06 14:49:36 -07004346}
4347
4348/*
4349 * Need to buffer aliases during bootup until sysfs becomes
4350 * available lest we loose that information.
4351 */
4352struct saved_alias {
4353 struct kmem_cache *s;
4354 const char *name;
4355 struct saved_alias *next;
4356};
4357
Adrian Bunk5af328a2007-07-17 04:03:27 -07004358static struct saved_alias *alias_list;
Christoph Lameter81819f02007-05-06 14:49:36 -07004359
4360static int sysfs_slab_alias(struct kmem_cache *s, const char *name)
4361{
4362 struct saved_alias *al;
4363
4364 if (slab_state == SYSFS) {
4365 /*
4366 * If we have a leftover link then remove it.
4367 */
Greg Kroah-Hartman27c3a312007-11-01 09:29:06 -06004368 sysfs_remove_link(&slab_kset->kobj, name);
4369 return sysfs_create_link(&slab_kset->kobj, &s->kobj, name);
Christoph Lameter81819f02007-05-06 14:49:36 -07004370 }
4371
4372 al = kmalloc(sizeof(struct saved_alias), GFP_KERNEL);
4373 if (!al)
4374 return -ENOMEM;
4375
4376 al->s = s;
4377 al->name = name;
4378 al->next = alias_list;
4379 alias_list = al;
4380 return 0;
4381}
4382
4383static int __init slab_sysfs_init(void)
4384{
Christoph Lameter5b95a4ac2007-07-17 04:03:19 -07004385 struct kmem_cache *s;
Christoph Lameter81819f02007-05-06 14:49:36 -07004386 int err;
4387
Greg Kroah-Hartman0ff21e42007-11-06 10:36:58 -08004388 slab_kset = kset_create_and_add("slab", &slab_uevent_ops, kernel_kobj);
Greg Kroah-Hartman27c3a312007-11-01 09:29:06 -06004389 if (!slab_kset) {
Christoph Lameter81819f02007-05-06 14:49:36 -07004390 printk(KERN_ERR "Cannot register slab subsystem.\n");
4391 return -ENOSYS;
4392 }
4393
Christoph Lameter26a7bd02007-05-09 02:32:39 -07004394 slab_state = SYSFS;
4395
Christoph Lameter5b95a4ac2007-07-17 04:03:19 -07004396 list_for_each_entry(s, &slab_caches, list) {
Christoph Lameter26a7bd02007-05-09 02:32:39 -07004397 err = sysfs_slab_add(s);
Christoph Lameter5d540fb2007-08-30 23:56:26 -07004398 if (err)
4399 printk(KERN_ERR "SLUB: Unable to add boot slab %s"
4400 " to sysfs\n", s->name);
Christoph Lameter26a7bd02007-05-09 02:32:39 -07004401 }
Christoph Lameter81819f02007-05-06 14:49:36 -07004402
4403 while (alias_list) {
4404 struct saved_alias *al = alias_list;
4405
4406 alias_list = alias_list->next;
4407 err = sysfs_slab_alias(al->s, al->name);
Christoph Lameter5d540fb2007-08-30 23:56:26 -07004408 if (err)
4409 printk(KERN_ERR "SLUB: Unable to add boot slab alias"
4410 " %s to sysfs\n", s->name);
Christoph Lameter81819f02007-05-06 14:49:36 -07004411 kfree(al);
4412 }
4413
4414 resiliency_test();
4415 return 0;
4416}
4417
4418__initcall(slab_sysfs_init);
Christoph Lameter81819f02007-05-06 14:49:36 -07004419#endif
Pekka J Enberg57ed3ed2008-01-01 17:23:28 +01004420
4421/*
4422 * The /proc/slabinfo ABI
4423 */
Linus Torvalds158a9622008-01-02 13:04:48 -08004424#ifdef CONFIG_SLABINFO
4425
4426ssize_t slabinfo_write(struct file *file, const char __user * buffer,
4427 size_t count, loff_t *ppos)
4428{
4429 return -EINVAL;
4430}
4431
Pekka J Enberg57ed3ed2008-01-01 17:23:28 +01004432
4433static void print_slabinfo_header(struct seq_file *m)
4434{
4435 seq_puts(m, "slabinfo - version: 2.1\n");
4436 seq_puts(m, "# name <active_objs> <num_objs> <objsize> "
4437 "<objperslab> <pagesperslab>");
4438 seq_puts(m, " : tunables <limit> <batchcount> <sharedfactor>");
4439 seq_puts(m, " : slabdata <active_slabs> <num_slabs> <sharedavail>");
4440 seq_putc(m, '\n');
4441}
4442
4443static void *s_start(struct seq_file *m, loff_t *pos)
4444{
4445 loff_t n = *pos;
4446
4447 down_read(&slub_lock);
4448 if (!n)
4449 print_slabinfo_header(m);
4450
4451 return seq_list_start(&slab_caches, *pos);
4452}
4453
4454static void *s_next(struct seq_file *m, void *p, loff_t *pos)
4455{
4456 return seq_list_next(p, &slab_caches, pos);
4457}
4458
4459static void s_stop(struct seq_file *m, void *p)
4460{
4461 up_read(&slub_lock);
4462}
4463
4464static int s_show(struct seq_file *m, void *p)
4465{
4466 unsigned long nr_partials = 0;
4467 unsigned long nr_slabs = 0;
4468 unsigned long nr_inuse = 0;
Christoph Lameter205ab992008-04-14 19:11:40 +03004469 unsigned long nr_objs = 0;
4470 unsigned long nr_free = 0;
Pekka J Enberg57ed3ed2008-01-01 17:23:28 +01004471 struct kmem_cache *s;
4472 int node;
4473
4474 s = list_entry(p, struct kmem_cache, list);
4475
4476 for_each_online_node(node) {
4477 struct kmem_cache_node *n = get_node(s, node);
4478
4479 if (!n)
4480 continue;
4481
4482 nr_partials += n->nr_partial;
4483 nr_slabs += atomic_long_read(&n->nr_slabs);
Christoph Lameter205ab992008-04-14 19:11:40 +03004484 nr_objs += atomic_long_read(&n->total_objects);
4485 nr_free += count_partial(n, count_free);
Pekka J Enberg57ed3ed2008-01-01 17:23:28 +01004486 }
4487
Christoph Lameter205ab992008-04-14 19:11:40 +03004488 nr_inuse = nr_objs - nr_free;
Pekka J Enberg57ed3ed2008-01-01 17:23:28 +01004489
4490 seq_printf(m, "%-17s %6lu %6lu %6u %4u %4d", s->name, nr_inuse,
Christoph Lameter834f3d12008-04-14 19:11:31 +03004491 nr_objs, s->size, oo_objects(s->oo),
4492 (1 << oo_order(s->oo)));
Pekka J Enberg57ed3ed2008-01-01 17:23:28 +01004493 seq_printf(m, " : tunables %4u %4u %4u", 0, 0, 0);
4494 seq_printf(m, " : slabdata %6lu %6lu %6lu", nr_slabs, nr_slabs,
4495 0UL);
4496 seq_putc(m, '\n');
4497 return 0;
4498}
4499
4500const struct seq_operations slabinfo_op = {
4501 .start = s_start,
4502 .next = s_next,
4503 .stop = s_stop,
4504 .show = s_show,
4505};
4506
Linus Torvalds158a9622008-01-02 13:04:48 -08004507#endif /* CONFIG_SLABINFO */