blob: 03ae5490c3dd260ac10616871d20a6596b5ca3dd [file] [log] [blame]
Christoph Lameter81819f02007-05-06 14:49:36 -07001/*
2 * SLUB: A slab allocator that limits cache line use instead of queuing
3 * objects in per cpu and per node lists.
4 *
5 * The allocator synchronizes using per slab locks and only
6 * uses a centralized lock to manage a pool of partial slabs.
7 *
8 * (C) 2007 SGI, Christoph Lameter <clameter@sgi.com>
9 */
10
11#include <linux/mm.h>
12#include <linux/module.h>
13#include <linux/bit_spinlock.h>
14#include <linux/interrupt.h>
15#include <linux/bitops.h>
16#include <linux/slab.h>
17#include <linux/seq_file.h>
18#include <linux/cpu.h>
19#include <linux/cpuset.h>
20#include <linux/mempolicy.h>
21#include <linux/ctype.h>
22#include <linux/kallsyms.h>
23
24/*
25 * Lock order:
26 * 1. slab_lock(page)
27 * 2. slab->list_lock
28 *
29 * The slab_lock protects operations on the object of a particular
30 * slab and its metadata in the page struct. If the slab lock
31 * has been taken then no allocations nor frees can be performed
32 * on the objects in the slab nor can the slab be added or removed
33 * from the partial or full lists since this would mean modifying
34 * the page_struct of the slab.
35 *
36 * The list_lock protects the partial and full list on each node and
37 * the partial slab counter. If taken then no new slabs may be added or
38 * removed from the lists nor make the number of partial slabs be modified.
39 * (Note that the total number of slabs is an atomic value that may be
40 * modified without taking the list lock).
41 *
42 * The list_lock is a centralized lock and thus we avoid taking it as
43 * much as possible. As long as SLUB does not have to handle partial
44 * slabs, operations can continue without any centralized lock. F.e.
45 * allocating a long series of objects that fill up slabs does not require
46 * the list lock.
47 *
48 * The lock order is sometimes inverted when we are trying to get a slab
49 * off a list. We take the list_lock and then look for a page on the list
50 * to use. While we do that objects in the slabs may be freed. We can
51 * only operate on the slab if we have also taken the slab_lock. So we use
52 * a slab_trylock() on the slab. If trylock was successful then no frees
53 * can occur anymore and we can use the slab for allocations etc. If the
54 * slab_trylock() does not succeed then frees are in progress in the slab and
55 * we must stay away from it for a while since we may cause a bouncing
56 * cacheline if we try to acquire the lock. So go onto the next slab.
57 * If all pages are busy then we may allocate a new slab instead of reusing
58 * a partial slab. A new slab has noone operating on it and thus there is
59 * no danger of cacheline contention.
60 *
61 * Interrupts are disabled during allocation and deallocation in order to
62 * make the slab allocator safe to use in the context of an irq. In addition
63 * interrupts are disabled to ensure that the processor does not change
64 * while handling per_cpu slabs, due to kernel preemption.
65 *
66 * SLUB assigns one slab for allocation to each processor.
67 * Allocations only occur from these slabs called cpu slabs.
68 *
Christoph Lameter672bba32007-05-09 02:32:39 -070069 * Slabs with free elements are kept on a partial list and during regular
70 * operations no list for full slabs is used. If an object in a full slab is
Christoph Lameter81819f02007-05-06 14:49:36 -070071 * freed then the slab will show up again on the partial lists.
Christoph Lameter672bba32007-05-09 02:32:39 -070072 * We track full slabs for debugging purposes though because otherwise we
73 * cannot scan all objects.
Christoph Lameter81819f02007-05-06 14:49:36 -070074 *
75 * Slabs are freed when they become empty. Teardown and setup is
76 * minimal so we rely on the page allocators per cpu caches for
77 * fast frees and allocs.
78 *
79 * Overloading of page flags that are otherwise used for LRU management.
80 *
Christoph Lameter4b6f0752007-05-16 22:10:53 -070081 * PageActive The slab is frozen and exempt from list processing.
82 * This means that the slab is dedicated to a purpose
83 * such as satisfying allocations for a specific
84 * processor. Objects may be freed in the slab while
85 * it is frozen but slab_free will then skip the usual
86 * list operations. It is up to the processor holding
87 * the slab to integrate the slab into the slab lists
88 * when the slab is no longer needed.
89 *
90 * One use of this flag is to mark slabs that are
91 * used for allocations. Then such a slab becomes a cpu
92 * slab. The cpu slab may be equipped with an additional
Christoph Lameter894b8782007-05-10 03:15:16 -070093 * lockless_freelist that allows lockless access to
94 * free objects in addition to the regular freelist
95 * that requires the slab lock.
Christoph Lameter81819f02007-05-06 14:49:36 -070096 *
97 * PageError Slab requires special handling due to debug
98 * options set. This moves slab handling out of
Christoph Lameter894b8782007-05-10 03:15:16 -070099 * the fast path and disables lockless freelists.
Christoph Lameter81819f02007-05-06 14:49:36 -0700100 */
101
Christoph Lameter5577bd82007-05-16 22:10:56 -0700102#define FROZEN (1 << PG_active)
103
104#ifdef CONFIG_SLUB_DEBUG
105#define SLABDEBUG (1 << PG_error)
106#else
107#define SLABDEBUG 0
108#endif
109
Christoph Lameter4b6f0752007-05-16 22:10:53 -0700110static inline int SlabFrozen(struct page *page)
111{
Christoph Lameter5577bd82007-05-16 22:10:56 -0700112 return page->flags & FROZEN;
Christoph Lameter4b6f0752007-05-16 22:10:53 -0700113}
114
115static inline void SetSlabFrozen(struct page *page)
116{
Christoph Lameter5577bd82007-05-16 22:10:56 -0700117 page->flags |= FROZEN;
Christoph Lameter4b6f0752007-05-16 22:10:53 -0700118}
119
120static inline void ClearSlabFrozen(struct page *page)
121{
Christoph Lameter5577bd82007-05-16 22:10:56 -0700122 page->flags &= ~FROZEN;
Christoph Lameter4b6f0752007-05-16 22:10:53 -0700123}
124
Christoph Lameter35e5d7e2007-05-09 02:32:42 -0700125static inline int SlabDebug(struct page *page)
126{
Christoph Lameter5577bd82007-05-16 22:10:56 -0700127 return page->flags & SLABDEBUG;
Christoph Lameter35e5d7e2007-05-09 02:32:42 -0700128}
129
130static inline void SetSlabDebug(struct page *page)
131{
Christoph Lameter5577bd82007-05-16 22:10:56 -0700132 page->flags |= SLABDEBUG;
Christoph Lameter35e5d7e2007-05-09 02:32:42 -0700133}
134
135static inline void ClearSlabDebug(struct page *page)
136{
Christoph Lameter5577bd82007-05-16 22:10:56 -0700137 page->flags &= ~SLABDEBUG;
Christoph Lameter35e5d7e2007-05-09 02:32:42 -0700138}
139
Christoph Lameter81819f02007-05-06 14:49:36 -0700140/*
141 * Issues still to be resolved:
142 *
143 * - The per cpu array is updated for each new slab and and is a remote
144 * cacheline for most nodes. This could become a bouncing cacheline given
Christoph Lameter672bba32007-05-09 02:32:39 -0700145 * enough frequent updates. There are 16 pointers in a cacheline, so at
146 * max 16 cpus could compete for the cacheline which may be okay.
Christoph Lameter81819f02007-05-06 14:49:36 -0700147 *
148 * - Support PAGE_ALLOC_DEBUG. Should be easy to do.
149 *
Christoph Lameter81819f02007-05-06 14:49:36 -0700150 * - Variable sizing of the per node arrays
151 */
152
153/* Enable to test recovery from slab corruption on boot */
154#undef SLUB_RESILIENCY_TEST
155
156#if PAGE_SHIFT <= 12
157
158/*
159 * Small page size. Make sure that we do not fragment memory
160 */
161#define DEFAULT_MAX_ORDER 1
162#define DEFAULT_MIN_OBJECTS 4
163
164#else
165
166/*
167 * Large page machines are customarily able to handle larger
168 * page orders.
169 */
170#define DEFAULT_MAX_ORDER 2
171#define DEFAULT_MIN_OBJECTS 8
172
173#endif
174
175/*
Christoph Lameter2086d262007-05-06 14:49:46 -0700176 * Mininum number of partial slabs. These will be left on the partial
177 * lists even if they are empty. kmem_cache_shrink may reclaim them.
178 */
Christoph Lametere95eed52007-05-06 14:49:44 -0700179#define MIN_PARTIAL 2
180
Christoph Lameter2086d262007-05-06 14:49:46 -0700181/*
182 * Maximum number of desirable partial slabs.
183 * The existence of more partial slabs makes kmem_cache_shrink
184 * sort the partial list by the number of objects in the.
185 */
186#define MAX_PARTIAL 10
187
Christoph Lameter81819f02007-05-06 14:49:36 -0700188#define DEBUG_DEFAULT_FLAGS (SLAB_DEBUG_FREE | SLAB_RED_ZONE | \
189 SLAB_POISON | SLAB_STORE_USER)
Christoph Lameter672bba32007-05-09 02:32:39 -0700190
Christoph Lameter81819f02007-05-06 14:49:36 -0700191/*
192 * Set of flags that will prevent slab merging
193 */
194#define SLUB_NEVER_MERGE (SLAB_RED_ZONE | SLAB_POISON | SLAB_STORE_USER | \
195 SLAB_TRACE | SLAB_DESTROY_BY_RCU)
196
197#define SLUB_MERGE_SAME (SLAB_DEBUG_FREE | SLAB_RECLAIM_ACCOUNT | \
198 SLAB_CACHE_DMA)
199
200#ifndef ARCH_KMALLOC_MINALIGN
Christoph Lameter47bfdc02007-05-06 14:49:37 -0700201#define ARCH_KMALLOC_MINALIGN __alignof__(unsigned long long)
Christoph Lameter81819f02007-05-06 14:49:36 -0700202#endif
203
204#ifndef ARCH_SLAB_MINALIGN
Christoph Lameter47bfdc02007-05-06 14:49:37 -0700205#define ARCH_SLAB_MINALIGN __alignof__(unsigned long long)
Christoph Lameter81819f02007-05-06 14:49:36 -0700206#endif
207
Christoph Lameter6300ea72007-07-17 04:03:20 -0700208/*
209 * The page->inuse field is 16 bit thus we have this limitation
210 */
211#define MAX_OBJECTS_PER_SLAB 65535
212
Christoph Lameter81819f02007-05-06 14:49:36 -0700213/* Internal SLUB flags */
214#define __OBJECT_POISON 0x80000000 /* Poison object */
215
Christoph Lameter65c02d42007-05-09 02:32:35 -0700216/* Not all arches define cache_line_size */
217#ifndef cache_line_size
218#define cache_line_size() L1_CACHE_BYTES
219#endif
220
Christoph Lameter81819f02007-05-06 14:49:36 -0700221static int kmem_size = sizeof(struct kmem_cache);
222
223#ifdef CONFIG_SMP
224static struct notifier_block slab_notifier;
225#endif
226
227static enum {
228 DOWN, /* No slab functionality available */
229 PARTIAL, /* kmem_cache_open() works but kmalloc does not */
Christoph Lameter672bba32007-05-09 02:32:39 -0700230 UP, /* Everything works but does not show up in sysfs */
Christoph Lameter81819f02007-05-06 14:49:36 -0700231 SYSFS /* Sysfs up */
232} slab_state = DOWN;
233
234/* A list of all slab caches on the system */
235static DECLARE_RWSEM(slub_lock);
236LIST_HEAD(slab_caches);
237
Christoph Lameter02cbc872007-05-09 02:32:43 -0700238/*
239 * Tracking user of a slab.
240 */
241struct track {
242 void *addr; /* Called from address */
243 int cpu; /* Was running on cpu */
244 int pid; /* Pid context */
245 unsigned long when; /* When did the operation occur */
246};
247
248enum track_item { TRACK_ALLOC, TRACK_FREE };
249
Christoph Lameter41ecc552007-05-09 02:32:44 -0700250#if defined(CONFIG_SYSFS) && defined(CONFIG_SLUB_DEBUG)
Christoph Lameter81819f02007-05-06 14:49:36 -0700251static int sysfs_slab_add(struct kmem_cache *);
252static int sysfs_slab_alias(struct kmem_cache *, const char *);
253static void sysfs_slab_remove(struct kmem_cache *);
254#else
255static int sysfs_slab_add(struct kmem_cache *s) { return 0; }
256static int sysfs_slab_alias(struct kmem_cache *s, const char *p) { return 0; }
257static void sysfs_slab_remove(struct kmem_cache *s) {}
258#endif
259
260/********************************************************************
261 * Core slab cache functions
262 *******************************************************************/
263
264int slab_is_available(void)
265{
266 return slab_state >= UP;
267}
268
269static inline struct kmem_cache_node *get_node(struct kmem_cache *s, int node)
270{
271#ifdef CONFIG_NUMA
272 return s->node[node];
273#else
274 return &s->local_node;
275#endif
276}
277
Christoph Lameter02cbc872007-05-09 02:32:43 -0700278static inline int check_valid_pointer(struct kmem_cache *s,
279 struct page *page, const void *object)
280{
281 void *base;
282
283 if (!object)
284 return 1;
285
286 base = page_address(page);
287 if (object < base || object >= base + s->objects * s->size ||
288 (object - base) % s->size) {
289 return 0;
290 }
291
292 return 1;
293}
294
Christoph Lameter81819f02007-05-06 14:49:36 -0700295/*
Christoph Lameter7656c722007-05-09 02:32:40 -0700296 * Slow version of get and set free pointer.
297 *
298 * This version requires touching the cache lines of kmem_cache which
299 * we avoid to do in the fast alloc free paths. There we obtain the offset
300 * from the page struct.
301 */
302static inline void *get_freepointer(struct kmem_cache *s, void *object)
303{
304 return *(void **)(object + s->offset);
305}
306
307static inline void set_freepointer(struct kmem_cache *s, void *object, void *fp)
308{
309 *(void **)(object + s->offset) = fp;
310}
311
312/* Loop over all objects in a slab */
313#define for_each_object(__p, __s, __addr) \
314 for (__p = (__addr); __p < (__addr) + (__s)->objects * (__s)->size;\
315 __p += (__s)->size)
316
317/* Scan freelist */
318#define for_each_free_object(__p, __s, __free) \
319 for (__p = (__free); __p; __p = get_freepointer((__s), __p))
320
321/* Determine object index from a given position */
322static inline int slab_index(void *p, struct kmem_cache *s, void *addr)
323{
324 return (p - addr) / s->size;
325}
326
Christoph Lameter41ecc552007-05-09 02:32:44 -0700327#ifdef CONFIG_SLUB_DEBUG
328/*
329 * Debug settings:
330 */
Christoph Lameterf0630ff2007-07-15 23:38:14 -0700331#ifdef CONFIG_SLUB_DEBUG_ON
332static int slub_debug = DEBUG_DEFAULT_FLAGS;
333#else
Christoph Lameter41ecc552007-05-09 02:32:44 -0700334static int slub_debug;
Christoph Lameterf0630ff2007-07-15 23:38:14 -0700335#endif
Christoph Lameter41ecc552007-05-09 02:32:44 -0700336
337static char *slub_debug_slabs;
338
Christoph Lameter7656c722007-05-09 02:32:40 -0700339/*
Christoph Lameter81819f02007-05-06 14:49:36 -0700340 * Object debugging
341 */
342static void print_section(char *text, u8 *addr, unsigned int length)
343{
344 int i, offset;
345 int newline = 1;
346 char ascii[17];
347
348 ascii[16] = 0;
349
350 for (i = 0; i < length; i++) {
351 if (newline) {
Christoph Lameter24922682007-07-17 04:03:18 -0700352 printk(KERN_ERR "%8s 0x%p: ", text, addr + i);
Christoph Lameter81819f02007-05-06 14:49:36 -0700353 newline = 0;
354 }
355 printk(" %02x", addr[i]);
356 offset = i % 16;
357 ascii[offset] = isgraph(addr[i]) ? addr[i] : '.';
358 if (offset == 15) {
359 printk(" %s\n",ascii);
360 newline = 1;
361 }
362 }
363 if (!newline) {
364 i %= 16;
365 while (i < 16) {
366 printk(" ");
367 ascii[i] = ' ';
368 i++;
369 }
370 printk(" %s\n", ascii);
371 }
372}
373
Christoph Lameter81819f02007-05-06 14:49:36 -0700374static struct track *get_track(struct kmem_cache *s, void *object,
375 enum track_item alloc)
376{
377 struct track *p;
378
379 if (s->offset)
380 p = object + s->offset + sizeof(void *);
381 else
382 p = object + s->inuse;
383
384 return p + alloc;
385}
386
387static void set_track(struct kmem_cache *s, void *object,
388 enum track_item alloc, void *addr)
389{
390 struct track *p;
391
392 if (s->offset)
393 p = object + s->offset + sizeof(void *);
394 else
395 p = object + s->inuse;
396
397 p += alloc;
398 if (addr) {
399 p->addr = addr;
400 p->cpu = smp_processor_id();
401 p->pid = current ? current->pid : -1;
402 p->when = jiffies;
403 } else
404 memset(p, 0, sizeof(struct track));
405}
406
Christoph Lameter81819f02007-05-06 14:49:36 -0700407static void init_tracking(struct kmem_cache *s, void *object)
408{
Christoph Lameter24922682007-07-17 04:03:18 -0700409 if (!(s->flags & SLAB_STORE_USER))
410 return;
411
412 set_track(s, object, TRACK_FREE, NULL);
413 set_track(s, object, TRACK_ALLOC, NULL);
Christoph Lameter81819f02007-05-06 14:49:36 -0700414}
415
416static void print_track(const char *s, struct track *t)
417{
418 if (!t->addr)
419 return;
420
Christoph Lameter24922682007-07-17 04:03:18 -0700421 printk(KERN_ERR "INFO: %s in ", s);
Christoph Lameter81819f02007-05-06 14:49:36 -0700422 __print_symbol("%s", (unsigned long)t->addr);
Christoph Lameter24922682007-07-17 04:03:18 -0700423 printk(" age=%lu cpu=%u pid=%d\n", jiffies - t->when, t->cpu, t->pid);
Christoph Lameter81819f02007-05-06 14:49:36 -0700424}
425
Christoph Lameter24922682007-07-17 04:03:18 -0700426static void print_tracking(struct kmem_cache *s, void *object)
427{
428 if (!(s->flags & SLAB_STORE_USER))
429 return;
430
431 print_track("Allocated", get_track(s, object, TRACK_ALLOC));
432 print_track("Freed", get_track(s, object, TRACK_FREE));
433}
434
435static void print_page_info(struct page *page)
436{
437 printk(KERN_ERR "INFO: Slab 0x%p used=%u fp=0x%p flags=0x%04lx\n",
438 page, page->inuse, page->freelist, page->flags);
439
440}
441
442static void slab_bug(struct kmem_cache *s, char *fmt, ...)
443{
444 va_list args;
445 char buf[100];
446
447 va_start(args, fmt);
448 vsnprintf(buf, sizeof(buf), fmt, args);
449 va_end(args);
450 printk(KERN_ERR "========================================"
451 "=====================================\n");
452 printk(KERN_ERR "BUG %s: %s\n", s->name, buf);
453 printk(KERN_ERR "----------------------------------------"
454 "-------------------------------------\n\n");
455}
456
457static void slab_fix(struct kmem_cache *s, char *fmt, ...)
458{
459 va_list args;
460 char buf[100];
461
462 va_start(args, fmt);
463 vsnprintf(buf, sizeof(buf), fmt, args);
464 va_end(args);
465 printk(KERN_ERR "FIX %s: %s\n", s->name, buf);
466}
467
468static void print_trailer(struct kmem_cache *s, struct page *page, u8 *p)
Christoph Lameter81819f02007-05-06 14:49:36 -0700469{
470 unsigned int off; /* Offset of last byte */
Christoph Lameter24922682007-07-17 04:03:18 -0700471 u8 *addr = page_address(page);
472
473 print_tracking(s, p);
474
475 print_page_info(page);
476
477 printk(KERN_ERR "INFO: Object 0x%p @offset=%tu fp=0x%p\n\n",
478 p, p - addr, get_freepointer(s, p));
479
480 if (p > addr + 16)
481 print_section("Bytes b4", p - 16, 16);
482
483 print_section("Object", p, min(s->objsize, 128));
Christoph Lameter81819f02007-05-06 14:49:36 -0700484
485 if (s->flags & SLAB_RED_ZONE)
486 print_section("Redzone", p + s->objsize,
487 s->inuse - s->objsize);
488
Christoph Lameter81819f02007-05-06 14:49:36 -0700489 if (s->offset)
490 off = s->offset + sizeof(void *);
491 else
492 off = s->inuse;
493
Christoph Lameter24922682007-07-17 04:03:18 -0700494 if (s->flags & SLAB_STORE_USER)
Christoph Lameter81819f02007-05-06 14:49:36 -0700495 off += 2 * sizeof(struct track);
Christoph Lameter81819f02007-05-06 14:49:36 -0700496
497 if (off != s->size)
498 /* Beginning of the filler is the free pointer */
Christoph Lameter24922682007-07-17 04:03:18 -0700499 print_section("Padding", p + off, s->size - off);
500
501 dump_stack();
Christoph Lameter81819f02007-05-06 14:49:36 -0700502}
503
504static void object_err(struct kmem_cache *s, struct page *page,
505 u8 *object, char *reason)
506{
Christoph Lameter24922682007-07-17 04:03:18 -0700507 slab_bug(s, reason);
508 print_trailer(s, page, object);
Christoph Lameter81819f02007-05-06 14:49:36 -0700509}
510
Christoph Lameter24922682007-07-17 04:03:18 -0700511static void slab_err(struct kmem_cache *s, struct page *page, char *fmt, ...)
Christoph Lameter81819f02007-05-06 14:49:36 -0700512{
513 va_list args;
514 char buf[100];
515
Christoph Lameter24922682007-07-17 04:03:18 -0700516 va_start(args, fmt);
517 vsnprintf(buf, sizeof(buf), fmt, args);
Christoph Lameter81819f02007-05-06 14:49:36 -0700518 va_end(args);
Christoph Lameter24922682007-07-17 04:03:18 -0700519 slab_bug(s, fmt);
520 print_page_info(page);
Christoph Lameter81819f02007-05-06 14:49:36 -0700521 dump_stack();
522}
523
524static void init_object(struct kmem_cache *s, void *object, int active)
525{
526 u8 *p = object;
527
528 if (s->flags & __OBJECT_POISON) {
529 memset(p, POISON_FREE, s->objsize - 1);
530 p[s->objsize -1] = POISON_END;
531 }
532
533 if (s->flags & SLAB_RED_ZONE)
534 memset(p + s->objsize,
535 active ? SLUB_RED_ACTIVE : SLUB_RED_INACTIVE,
536 s->inuse - s->objsize);
537}
538
Christoph Lameter24922682007-07-17 04:03:18 -0700539static u8 *check_bytes(u8 *start, unsigned int value, unsigned int bytes)
Christoph Lameter81819f02007-05-06 14:49:36 -0700540{
541 while (bytes) {
542 if (*start != (u8)value)
Christoph Lameter24922682007-07-17 04:03:18 -0700543 return start;
Christoph Lameter81819f02007-05-06 14:49:36 -0700544 start++;
545 bytes--;
546 }
Christoph Lameter24922682007-07-17 04:03:18 -0700547 return NULL;
548}
549
550static void restore_bytes(struct kmem_cache *s, char *message, u8 data,
551 void *from, void *to)
552{
553 slab_fix(s, "Restoring 0x%p-0x%p=0x%x\n", from, to - 1, data);
554 memset(from, data, to - from);
555}
556
557static int check_bytes_and_report(struct kmem_cache *s, struct page *page,
558 u8 *object, char *what,
559 u8* start, unsigned int value, unsigned int bytes)
560{
561 u8 *fault;
562 u8 *end;
563
564 fault = check_bytes(start, value, bytes);
565 if (!fault)
566 return 1;
567
568 end = start + bytes;
569 while (end > fault && end[-1] == value)
570 end--;
571
572 slab_bug(s, "%s overwritten", what);
573 printk(KERN_ERR "INFO: 0x%p-0x%p. First byte 0x%x instead of 0x%x\n",
574 fault, end - 1, fault[0], value);
575 print_trailer(s, page, object);
576
577 restore_bytes(s, what, value, fault, end);
578 return 0;
Christoph Lameter81819f02007-05-06 14:49:36 -0700579}
580
Christoph Lameter81819f02007-05-06 14:49:36 -0700581/*
582 * Object layout:
583 *
584 * object address
585 * Bytes of the object to be managed.
586 * If the freepointer may overlay the object then the free
587 * pointer is the first word of the object.
Christoph Lameter672bba32007-05-09 02:32:39 -0700588 *
Christoph Lameter81819f02007-05-06 14:49:36 -0700589 * Poisoning uses 0x6b (POISON_FREE) and the last byte is
590 * 0xa5 (POISON_END)
591 *
592 * object + s->objsize
593 * Padding to reach word boundary. This is also used for Redzoning.
Christoph Lameter672bba32007-05-09 02:32:39 -0700594 * Padding is extended by another word if Redzoning is enabled and
595 * objsize == inuse.
596 *
Christoph Lameter81819f02007-05-06 14:49:36 -0700597 * We fill with 0xbb (RED_INACTIVE) for inactive objects and with
598 * 0xcc (RED_ACTIVE) for objects in use.
599 *
600 * object + s->inuse
Christoph Lameter672bba32007-05-09 02:32:39 -0700601 * Meta data starts here.
602 *
Christoph Lameter81819f02007-05-06 14:49:36 -0700603 * A. Free pointer (if we cannot overwrite object on free)
604 * B. Tracking data for SLAB_STORE_USER
Christoph Lameter672bba32007-05-09 02:32:39 -0700605 * C. Padding to reach required alignment boundary or at mininum
606 * one word if debuggin is on to be able to detect writes
607 * before the word boundary.
608 *
609 * Padding is done using 0x5a (POISON_INUSE)
Christoph Lameter81819f02007-05-06 14:49:36 -0700610 *
611 * object + s->size
Christoph Lameter672bba32007-05-09 02:32:39 -0700612 * Nothing is used beyond s->size.
Christoph Lameter81819f02007-05-06 14:49:36 -0700613 *
Christoph Lameter672bba32007-05-09 02:32:39 -0700614 * If slabcaches are merged then the objsize and inuse boundaries are mostly
615 * ignored. And therefore no slab options that rely on these boundaries
Christoph Lameter81819f02007-05-06 14:49:36 -0700616 * may be used with merged slabcaches.
617 */
618
Christoph Lameter81819f02007-05-06 14:49:36 -0700619static int check_pad_bytes(struct kmem_cache *s, struct page *page, u8 *p)
620{
621 unsigned long off = s->inuse; /* The end of info */
622
623 if (s->offset)
624 /* Freepointer is placed after the object. */
625 off += sizeof(void *);
626
627 if (s->flags & SLAB_STORE_USER)
628 /* We also have user information there */
629 off += 2 * sizeof(struct track);
630
631 if (s->size == off)
632 return 1;
633
Christoph Lameter24922682007-07-17 04:03:18 -0700634 return check_bytes_and_report(s, page, p, "Object padding",
635 p + off, POISON_INUSE, s->size - off);
Christoph Lameter81819f02007-05-06 14:49:36 -0700636}
637
638static int slab_pad_check(struct kmem_cache *s, struct page *page)
639{
Christoph Lameter24922682007-07-17 04:03:18 -0700640 u8 *start;
641 u8 *fault;
642 u8 *end;
643 int length;
644 int remainder;
Christoph Lameter81819f02007-05-06 14:49:36 -0700645
646 if (!(s->flags & SLAB_POISON))
647 return 1;
648
Christoph Lameter24922682007-07-17 04:03:18 -0700649 start = page_address(page);
650 end = start + (PAGE_SIZE << s->order);
Christoph Lameter81819f02007-05-06 14:49:36 -0700651 length = s->objects * s->size;
Christoph Lameter24922682007-07-17 04:03:18 -0700652 remainder = end - (start + length);
Christoph Lameter81819f02007-05-06 14:49:36 -0700653 if (!remainder)
654 return 1;
655
Christoph Lameter24922682007-07-17 04:03:18 -0700656 fault = check_bytes(start + length, POISON_INUSE, remainder);
657 if (!fault)
658 return 1;
659 while (end > fault && end[-1] == POISON_INUSE)
660 end--;
661
662 slab_err(s, page, "Padding overwritten. 0x%p-0x%p", fault, end - 1);
663 print_section("Padding", start, length);
664
665 restore_bytes(s, "slab padding", POISON_INUSE, start, end);
666 return 0;
Christoph Lameter81819f02007-05-06 14:49:36 -0700667}
668
669static int check_object(struct kmem_cache *s, struct page *page,
670 void *object, int active)
671{
672 u8 *p = object;
673 u8 *endobject = object + s->objsize;
674
675 if (s->flags & SLAB_RED_ZONE) {
676 unsigned int red =
677 active ? SLUB_RED_ACTIVE : SLUB_RED_INACTIVE;
678
Christoph Lameter24922682007-07-17 04:03:18 -0700679 if (!check_bytes_and_report(s, page, object, "Redzone",
680 endobject, red, s->inuse - s->objsize))
Christoph Lameter81819f02007-05-06 14:49:36 -0700681 return 0;
Christoph Lameter81819f02007-05-06 14:49:36 -0700682 } else {
Christoph Lameter24922682007-07-17 04:03:18 -0700683 if ((s->flags & SLAB_POISON) && s->objsize < s->inuse)
684 check_bytes_and_report(s, page, p, "Alignment padding", endobject,
685 POISON_INUSE, s->inuse - s->objsize);
Christoph Lameter81819f02007-05-06 14:49:36 -0700686 }
687
688 if (s->flags & SLAB_POISON) {
689 if (!active && (s->flags & __OBJECT_POISON) &&
Christoph Lameter24922682007-07-17 04:03:18 -0700690 (!check_bytes_and_report(s, page, p, "Poison", p,
691 POISON_FREE, s->objsize - 1) ||
692 !check_bytes_and_report(s, page, p, "Poison",
693 p + s->objsize -1, POISON_END, 1)))
Christoph Lameter81819f02007-05-06 14:49:36 -0700694 return 0;
Christoph Lameter81819f02007-05-06 14:49:36 -0700695 /*
696 * check_pad_bytes cleans up on its own.
697 */
698 check_pad_bytes(s, page, p);
699 }
700
701 if (!s->offset && active)
702 /*
703 * Object and freepointer overlap. Cannot check
704 * freepointer while object is allocated.
705 */
706 return 1;
707
708 /* Check free pointer validity */
709 if (!check_valid_pointer(s, page, get_freepointer(s, p))) {
710 object_err(s, page, p, "Freepointer corrupt");
711 /*
712 * No choice but to zap it and thus loose the remainder
713 * of the free objects in this slab. May cause
Christoph Lameter672bba32007-05-09 02:32:39 -0700714 * another error because the object count is now wrong.
Christoph Lameter81819f02007-05-06 14:49:36 -0700715 */
716 set_freepointer(s, p, NULL);
717 return 0;
718 }
719 return 1;
720}
721
722static int check_slab(struct kmem_cache *s, struct page *page)
723{
724 VM_BUG_ON(!irqs_disabled());
725
726 if (!PageSlab(page)) {
Christoph Lameter24922682007-07-17 04:03:18 -0700727 slab_err(s, page, "Not a valid slab page");
Christoph Lameter81819f02007-05-06 14:49:36 -0700728 return 0;
729 }
730 if (page->offset * sizeof(void *) != s->offset) {
Christoph Lameter24922682007-07-17 04:03:18 -0700731 slab_err(s, page, "Corrupted offset %lu",
732 (unsigned long)(page->offset * sizeof(void *)));
Christoph Lameter81819f02007-05-06 14:49:36 -0700733 return 0;
734 }
735 if (page->inuse > s->objects) {
Christoph Lameter24922682007-07-17 04:03:18 -0700736 slab_err(s, page, "inuse %u > max %u",
737 s->name, page->inuse, s->objects);
Christoph Lameter81819f02007-05-06 14:49:36 -0700738 return 0;
739 }
740 /* Slab_pad_check fixes things up after itself */
741 slab_pad_check(s, page);
742 return 1;
743}
744
745/*
Christoph Lameter672bba32007-05-09 02:32:39 -0700746 * Determine if a certain object on a page is on the freelist. Must hold the
747 * slab lock to guarantee that the chains are in a consistent state.
Christoph Lameter81819f02007-05-06 14:49:36 -0700748 */
749static int on_freelist(struct kmem_cache *s, struct page *page, void *search)
750{
751 int nr = 0;
752 void *fp = page->freelist;
753 void *object = NULL;
754
755 while (fp && nr <= s->objects) {
756 if (fp == search)
757 return 1;
758 if (!check_valid_pointer(s, page, fp)) {
759 if (object) {
760 object_err(s, page, object,
761 "Freechain corrupt");
762 set_freepointer(s, object, NULL);
763 break;
764 } else {
Christoph Lameter24922682007-07-17 04:03:18 -0700765 slab_err(s, page, "Freepointer corrupt");
Christoph Lameter81819f02007-05-06 14:49:36 -0700766 page->freelist = NULL;
767 page->inuse = s->objects;
Christoph Lameter24922682007-07-17 04:03:18 -0700768 slab_fix(s, "Freelist cleared");
Christoph Lameter81819f02007-05-06 14:49:36 -0700769 return 0;
770 }
771 break;
772 }
773 object = fp;
774 fp = get_freepointer(s, object);
775 nr++;
776 }
777
778 if (page->inuse != s->objects - nr) {
Christoph Lameter70d71222007-05-06 14:49:47 -0700779 slab_err(s, page, "Wrong object count. Counter is %d but "
Christoph Lameter24922682007-07-17 04:03:18 -0700780 "counted were %d", page->inuse, s->objects - nr);
Christoph Lameter81819f02007-05-06 14:49:36 -0700781 page->inuse = s->objects - nr;
Christoph Lameter24922682007-07-17 04:03:18 -0700782 slab_fix(s, "Object count adjusted.");
Christoph Lameter81819f02007-05-06 14:49:36 -0700783 }
784 return search == NULL;
785}
786
Christoph Lameter3ec09742007-05-16 22:11:00 -0700787static void trace(struct kmem_cache *s, struct page *page, void *object, int alloc)
788{
789 if (s->flags & SLAB_TRACE) {
790 printk(KERN_INFO "TRACE %s %s 0x%p inuse=%d fp=0x%p\n",
791 s->name,
792 alloc ? "alloc" : "free",
793 object, page->inuse,
794 page->freelist);
795
796 if (!alloc)
797 print_section("Object", (void *)object, s->objsize);
798
799 dump_stack();
800 }
801}
802
Christoph Lameter643b1132007-05-06 14:49:42 -0700803/*
Christoph Lameter672bba32007-05-09 02:32:39 -0700804 * Tracking of fully allocated slabs for debugging purposes.
Christoph Lameter643b1132007-05-06 14:49:42 -0700805 */
Christoph Lametere95eed52007-05-06 14:49:44 -0700806static void add_full(struct kmem_cache_node *n, struct page *page)
Christoph Lameter643b1132007-05-06 14:49:42 -0700807{
Christoph Lameter643b1132007-05-06 14:49:42 -0700808 spin_lock(&n->list_lock);
809 list_add(&page->lru, &n->full);
810 spin_unlock(&n->list_lock);
811}
812
813static void remove_full(struct kmem_cache *s, struct page *page)
814{
815 struct kmem_cache_node *n;
816
817 if (!(s->flags & SLAB_STORE_USER))
818 return;
819
820 n = get_node(s, page_to_nid(page));
821
822 spin_lock(&n->list_lock);
823 list_del(&page->lru);
824 spin_unlock(&n->list_lock);
825}
826
Christoph Lameter3ec09742007-05-16 22:11:00 -0700827static void setup_object_debug(struct kmem_cache *s, struct page *page,
828 void *object)
829{
830 if (!(s->flags & (SLAB_STORE_USER|SLAB_RED_ZONE|__OBJECT_POISON)))
831 return;
832
833 init_object(s, object, 0);
834 init_tracking(s, object);
835}
836
837static int alloc_debug_processing(struct kmem_cache *s, struct page *page,
838 void *object, void *addr)
Christoph Lameter81819f02007-05-06 14:49:36 -0700839{
840 if (!check_slab(s, page))
841 goto bad;
842
843 if (object && !on_freelist(s, page, object)) {
Christoph Lameter24922682007-07-17 04:03:18 -0700844 object_err(s, page, object, "Object already allocated");
Christoph Lameter70d71222007-05-06 14:49:47 -0700845 goto bad;
Christoph Lameter81819f02007-05-06 14:49:36 -0700846 }
847
848 if (!check_valid_pointer(s, page, object)) {
849 object_err(s, page, object, "Freelist Pointer check fails");
Christoph Lameter70d71222007-05-06 14:49:47 -0700850 goto bad;
Christoph Lameter81819f02007-05-06 14:49:36 -0700851 }
852
Christoph Lameter3ec09742007-05-16 22:11:00 -0700853 if (object && !check_object(s, page, object, 0))
Christoph Lameter81819f02007-05-06 14:49:36 -0700854 goto bad;
Christoph Lameter81819f02007-05-06 14:49:36 -0700855
Christoph Lameter3ec09742007-05-16 22:11:00 -0700856 /* Success perform special debug activities for allocs */
857 if (s->flags & SLAB_STORE_USER)
858 set_track(s, object, TRACK_ALLOC, addr);
859 trace(s, page, object, 1);
860 init_object(s, object, 1);
Christoph Lameter81819f02007-05-06 14:49:36 -0700861 return 1;
Christoph Lameter3ec09742007-05-16 22:11:00 -0700862
Christoph Lameter81819f02007-05-06 14:49:36 -0700863bad:
864 if (PageSlab(page)) {
865 /*
866 * If this is a slab page then lets do the best we can
867 * to avoid issues in the future. Marking all objects
Christoph Lameter672bba32007-05-09 02:32:39 -0700868 * as used avoids touching the remaining objects.
Christoph Lameter81819f02007-05-06 14:49:36 -0700869 */
Christoph Lameter24922682007-07-17 04:03:18 -0700870 slab_fix(s, "Marking all objects used");
Christoph Lameter81819f02007-05-06 14:49:36 -0700871 page->inuse = s->objects;
872 page->freelist = NULL;
873 /* Fix up fields that may be corrupted */
874 page->offset = s->offset / sizeof(void *);
875 }
876 return 0;
877}
878
Christoph Lameter3ec09742007-05-16 22:11:00 -0700879static int free_debug_processing(struct kmem_cache *s, struct page *page,
880 void *object, void *addr)
Christoph Lameter81819f02007-05-06 14:49:36 -0700881{
882 if (!check_slab(s, page))
883 goto fail;
884
885 if (!check_valid_pointer(s, page, object)) {
Christoph Lameter70d71222007-05-06 14:49:47 -0700886 slab_err(s, page, "Invalid object pointer 0x%p", object);
Christoph Lameter81819f02007-05-06 14:49:36 -0700887 goto fail;
888 }
889
890 if (on_freelist(s, page, object)) {
Christoph Lameter24922682007-07-17 04:03:18 -0700891 object_err(s, page, object, "Object already free");
Christoph Lameter81819f02007-05-06 14:49:36 -0700892 goto fail;
893 }
894
895 if (!check_object(s, page, object, 1))
896 return 0;
897
898 if (unlikely(s != page->slab)) {
899 if (!PageSlab(page))
Christoph Lameter70d71222007-05-06 14:49:47 -0700900 slab_err(s, page, "Attempt to free object(0x%p) "
901 "outside of slab", object);
Christoph Lameter81819f02007-05-06 14:49:36 -0700902 else
Christoph Lameter70d71222007-05-06 14:49:47 -0700903 if (!page->slab) {
Christoph Lameter81819f02007-05-06 14:49:36 -0700904 printk(KERN_ERR
Christoph Lameter70d71222007-05-06 14:49:47 -0700905 "SLUB <none>: no slab for object 0x%p.\n",
Christoph Lameter81819f02007-05-06 14:49:36 -0700906 object);
Christoph Lameter70d71222007-05-06 14:49:47 -0700907 dump_stack();
908 }
Christoph Lameter81819f02007-05-06 14:49:36 -0700909 else
Christoph Lameter24922682007-07-17 04:03:18 -0700910 object_err(s, page, object,
911 "page slab pointer corrupt.");
Christoph Lameter81819f02007-05-06 14:49:36 -0700912 goto fail;
913 }
Christoph Lameter3ec09742007-05-16 22:11:00 -0700914
915 /* Special debug activities for freeing objects */
916 if (!SlabFrozen(page) && !page->freelist)
917 remove_full(s, page);
918 if (s->flags & SLAB_STORE_USER)
919 set_track(s, object, TRACK_FREE, addr);
920 trace(s, page, object, 0);
921 init_object(s, object, 0);
Christoph Lameter81819f02007-05-06 14:49:36 -0700922 return 1;
Christoph Lameter3ec09742007-05-16 22:11:00 -0700923
Christoph Lameter81819f02007-05-06 14:49:36 -0700924fail:
Christoph Lameter24922682007-07-17 04:03:18 -0700925 slab_fix(s, "Object at 0x%p not freed", object);
Christoph Lameter81819f02007-05-06 14:49:36 -0700926 return 0;
927}
928
Christoph Lameter41ecc552007-05-09 02:32:44 -0700929static int __init setup_slub_debug(char *str)
930{
Christoph Lameterf0630ff2007-07-15 23:38:14 -0700931 slub_debug = DEBUG_DEFAULT_FLAGS;
932 if (*str++ != '=' || !*str)
933 /*
934 * No options specified. Switch on full debugging.
935 */
936 goto out;
Christoph Lameter41ecc552007-05-09 02:32:44 -0700937
938 if (*str == ',')
Christoph Lameterf0630ff2007-07-15 23:38:14 -0700939 /*
940 * No options but restriction on slabs. This means full
941 * debugging for slabs matching a pattern.
942 */
943 goto check_slabs;
944
945 slub_debug = 0;
946 if (*str == '-')
947 /*
948 * Switch off all debugging measures.
949 */
950 goto out;
951
952 /*
953 * Determine which debug features should be switched on
954 */
955 for ( ;*str && *str != ','; str++) {
956 switch (tolower(*str)) {
957 case 'f':
958 slub_debug |= SLAB_DEBUG_FREE;
959 break;
960 case 'z':
961 slub_debug |= SLAB_RED_ZONE;
962 break;
963 case 'p':
964 slub_debug |= SLAB_POISON;
965 break;
966 case 'u':
967 slub_debug |= SLAB_STORE_USER;
968 break;
969 case 't':
970 slub_debug |= SLAB_TRACE;
971 break;
972 default:
973 printk(KERN_ERR "slub_debug option '%c' "
974 "unknown. skipped\n",*str);
975 }
976 }
977
978check_slabs:
979 if (*str == ',')
Christoph Lameter41ecc552007-05-09 02:32:44 -0700980 slub_debug_slabs = str + 1;
Christoph Lameterf0630ff2007-07-15 23:38:14 -0700981out:
Christoph Lameter41ecc552007-05-09 02:32:44 -0700982 return 1;
983}
984
985__setup("slub_debug", setup_slub_debug);
986
987static void kmem_cache_open_debug_check(struct kmem_cache *s)
988{
989 /*
990 * The page->offset field is only 16 bit wide. This is an offset
991 * in units of words from the beginning of an object. If the slab
992 * size is bigger then we cannot move the free pointer behind the
993 * object anymore.
994 *
995 * On 32 bit platforms the limit is 256k. On 64bit platforms
996 * the limit is 512k.
997 *
Christoph Lameterc59def9f2007-05-16 22:10:50 -0700998 * Debugging or ctor may create a need to move the free
Christoph Lameter41ecc552007-05-09 02:32:44 -0700999 * pointer. Fail if this happens.
1000 */
Christoph Lameter33e9e242007-05-23 13:57:56 -07001001 if (s->objsize >= 65535 * sizeof(void *)) {
Christoph Lameter41ecc552007-05-09 02:32:44 -07001002 BUG_ON(s->flags & (SLAB_RED_ZONE | SLAB_POISON |
1003 SLAB_STORE_USER | SLAB_DESTROY_BY_RCU));
Christoph Lameterc59def9f2007-05-16 22:10:50 -07001004 BUG_ON(s->ctor);
Christoph Lameter41ecc552007-05-09 02:32:44 -07001005 }
1006 else
1007 /*
1008 * Enable debugging if selected on the kernel commandline.
1009 */
1010 if (slub_debug && (!slub_debug_slabs ||
1011 strncmp(slub_debug_slabs, s->name,
1012 strlen(slub_debug_slabs)) == 0))
1013 s->flags |= slub_debug;
1014}
1015#else
Christoph Lameter3ec09742007-05-16 22:11:00 -07001016static inline void setup_object_debug(struct kmem_cache *s,
1017 struct page *page, void *object) {}
Christoph Lameter41ecc552007-05-09 02:32:44 -07001018
Christoph Lameter3ec09742007-05-16 22:11:00 -07001019static inline int alloc_debug_processing(struct kmem_cache *s,
1020 struct page *page, void *object, void *addr) { return 0; }
Christoph Lameter41ecc552007-05-09 02:32:44 -07001021
Christoph Lameter3ec09742007-05-16 22:11:00 -07001022static inline int free_debug_processing(struct kmem_cache *s,
1023 struct page *page, void *object, void *addr) { return 0; }
Christoph Lameter41ecc552007-05-09 02:32:44 -07001024
Christoph Lameter41ecc552007-05-09 02:32:44 -07001025static inline int slab_pad_check(struct kmem_cache *s, struct page *page)
1026 { return 1; }
1027static inline int check_object(struct kmem_cache *s, struct page *page,
1028 void *object, int active) { return 1; }
Christoph Lameter3ec09742007-05-16 22:11:00 -07001029static inline void add_full(struct kmem_cache_node *n, struct page *page) {}
Christoph Lameter41ecc552007-05-09 02:32:44 -07001030static inline void kmem_cache_open_debug_check(struct kmem_cache *s) {}
1031#define slub_debug 0
1032#endif
Christoph Lameter81819f02007-05-06 14:49:36 -07001033/*
1034 * Slab allocation and freeing
1035 */
1036static struct page *allocate_slab(struct kmem_cache *s, gfp_t flags, int node)
1037{
1038 struct page * page;
1039 int pages = 1 << s->order;
1040
1041 if (s->order)
1042 flags |= __GFP_COMP;
1043
1044 if (s->flags & SLAB_CACHE_DMA)
1045 flags |= SLUB_DMA;
1046
1047 if (node == -1)
1048 page = alloc_pages(flags, s->order);
1049 else
1050 page = alloc_pages_node(node, flags, s->order);
1051
1052 if (!page)
1053 return NULL;
1054
1055 mod_zone_page_state(page_zone(page),
1056 (s->flags & SLAB_RECLAIM_ACCOUNT) ?
1057 NR_SLAB_RECLAIMABLE : NR_SLAB_UNRECLAIMABLE,
1058 pages);
1059
1060 return page;
1061}
1062
1063static void setup_object(struct kmem_cache *s, struct page *page,
1064 void *object)
1065{
Christoph Lameter3ec09742007-05-16 22:11:00 -07001066 setup_object_debug(s, page, object);
Christoph Lameter4f104932007-05-06 14:50:17 -07001067 if (unlikely(s->ctor))
Christoph Lametera35afb82007-05-16 22:10:57 -07001068 s->ctor(object, s, 0);
Christoph Lameter81819f02007-05-06 14:49:36 -07001069}
1070
1071static struct page *new_slab(struct kmem_cache *s, gfp_t flags, int node)
1072{
1073 struct page *page;
1074 struct kmem_cache_node *n;
1075 void *start;
1076 void *end;
1077 void *last;
1078 void *p;
1079
Christoph Lameter81819f02007-05-06 14:49:36 -07001080 BUG_ON(flags & ~(GFP_DMA | GFP_LEVEL_MASK));
1081
1082 if (flags & __GFP_WAIT)
1083 local_irq_enable();
1084
1085 page = allocate_slab(s, flags & GFP_LEVEL_MASK, node);
1086 if (!page)
1087 goto out;
1088
1089 n = get_node(s, page_to_nid(page));
1090 if (n)
1091 atomic_long_inc(&n->nr_slabs);
1092 page->offset = s->offset / sizeof(void *);
1093 page->slab = s;
1094 page->flags |= 1 << PG_slab;
1095 if (s->flags & (SLAB_DEBUG_FREE | SLAB_RED_ZONE | SLAB_POISON |
1096 SLAB_STORE_USER | SLAB_TRACE))
Christoph Lameter35e5d7e2007-05-09 02:32:42 -07001097 SetSlabDebug(page);
Christoph Lameter81819f02007-05-06 14:49:36 -07001098
1099 start = page_address(page);
1100 end = start + s->objects * s->size;
1101
1102 if (unlikely(s->flags & SLAB_POISON))
1103 memset(start, POISON_INUSE, PAGE_SIZE << s->order);
1104
1105 last = start;
Christoph Lameter7656c722007-05-09 02:32:40 -07001106 for_each_object(p, s, start) {
Christoph Lameter81819f02007-05-06 14:49:36 -07001107 setup_object(s, page, last);
1108 set_freepointer(s, last, p);
1109 last = p;
1110 }
1111 setup_object(s, page, last);
1112 set_freepointer(s, last, NULL);
1113
1114 page->freelist = start;
Christoph Lameter894b8782007-05-10 03:15:16 -07001115 page->lockless_freelist = NULL;
Christoph Lameter81819f02007-05-06 14:49:36 -07001116 page->inuse = 0;
1117out:
1118 if (flags & __GFP_WAIT)
1119 local_irq_disable();
1120 return page;
1121}
1122
1123static void __free_slab(struct kmem_cache *s, struct page *page)
1124{
1125 int pages = 1 << s->order;
1126
Christoph Lameterc59def9f2007-05-16 22:10:50 -07001127 if (unlikely(SlabDebug(page))) {
Christoph Lameter81819f02007-05-06 14:49:36 -07001128 void *p;
1129
1130 slab_pad_check(s, page);
Christoph Lameterc59def9f2007-05-16 22:10:50 -07001131 for_each_object(p, s, page_address(page))
Christoph Lameter81819f02007-05-06 14:49:36 -07001132 check_object(s, page, p, 0);
Christoph Lameter81819f02007-05-06 14:49:36 -07001133 }
1134
1135 mod_zone_page_state(page_zone(page),
1136 (s->flags & SLAB_RECLAIM_ACCOUNT) ?
1137 NR_SLAB_RECLAIMABLE : NR_SLAB_UNRECLAIMABLE,
1138 - pages);
1139
1140 page->mapping = NULL;
1141 __free_pages(page, s->order);
1142}
1143
1144static void rcu_free_slab(struct rcu_head *h)
1145{
1146 struct page *page;
1147
1148 page = container_of((struct list_head *)h, struct page, lru);
1149 __free_slab(page->slab, page);
1150}
1151
1152static void free_slab(struct kmem_cache *s, struct page *page)
1153{
1154 if (unlikely(s->flags & SLAB_DESTROY_BY_RCU)) {
1155 /*
1156 * RCU free overloads the RCU head over the LRU
1157 */
1158 struct rcu_head *head = (void *)&page->lru;
1159
1160 call_rcu(head, rcu_free_slab);
1161 } else
1162 __free_slab(s, page);
1163}
1164
1165static void discard_slab(struct kmem_cache *s, struct page *page)
1166{
1167 struct kmem_cache_node *n = get_node(s, page_to_nid(page));
1168
1169 atomic_long_dec(&n->nr_slabs);
1170 reset_page_mapcount(page);
Christoph Lameter35e5d7e2007-05-09 02:32:42 -07001171 ClearSlabDebug(page);
1172 __ClearPageSlab(page);
Christoph Lameter81819f02007-05-06 14:49:36 -07001173 free_slab(s, page);
1174}
1175
1176/*
1177 * Per slab locking using the pagelock
1178 */
1179static __always_inline void slab_lock(struct page *page)
1180{
1181 bit_spin_lock(PG_locked, &page->flags);
1182}
1183
1184static __always_inline void slab_unlock(struct page *page)
1185{
1186 bit_spin_unlock(PG_locked, &page->flags);
1187}
1188
1189static __always_inline int slab_trylock(struct page *page)
1190{
1191 int rc = 1;
1192
1193 rc = bit_spin_trylock(PG_locked, &page->flags);
1194 return rc;
1195}
1196
1197/*
1198 * Management of partially allocated slabs
1199 */
Christoph Lametere95eed52007-05-06 14:49:44 -07001200static void add_partial_tail(struct kmem_cache_node *n, struct page *page)
Christoph Lameter81819f02007-05-06 14:49:36 -07001201{
Christoph Lametere95eed52007-05-06 14:49:44 -07001202 spin_lock(&n->list_lock);
1203 n->nr_partial++;
1204 list_add_tail(&page->lru, &n->partial);
1205 spin_unlock(&n->list_lock);
1206}
Christoph Lameter81819f02007-05-06 14:49:36 -07001207
Christoph Lametere95eed52007-05-06 14:49:44 -07001208static void add_partial(struct kmem_cache_node *n, struct page *page)
1209{
Christoph Lameter81819f02007-05-06 14:49:36 -07001210 spin_lock(&n->list_lock);
1211 n->nr_partial++;
1212 list_add(&page->lru, &n->partial);
1213 spin_unlock(&n->list_lock);
1214}
1215
1216static void remove_partial(struct kmem_cache *s,
1217 struct page *page)
1218{
1219 struct kmem_cache_node *n = get_node(s, page_to_nid(page));
1220
1221 spin_lock(&n->list_lock);
1222 list_del(&page->lru);
1223 n->nr_partial--;
1224 spin_unlock(&n->list_lock);
1225}
1226
1227/*
Christoph Lameter672bba32007-05-09 02:32:39 -07001228 * Lock slab and remove from the partial list.
Christoph Lameter81819f02007-05-06 14:49:36 -07001229 *
Christoph Lameter672bba32007-05-09 02:32:39 -07001230 * Must hold list_lock.
Christoph Lameter81819f02007-05-06 14:49:36 -07001231 */
Christoph Lameter4b6f0752007-05-16 22:10:53 -07001232static inline int lock_and_freeze_slab(struct kmem_cache_node *n, struct page *page)
Christoph Lameter81819f02007-05-06 14:49:36 -07001233{
1234 if (slab_trylock(page)) {
1235 list_del(&page->lru);
1236 n->nr_partial--;
Christoph Lameter4b6f0752007-05-16 22:10:53 -07001237 SetSlabFrozen(page);
Christoph Lameter81819f02007-05-06 14:49:36 -07001238 return 1;
1239 }
1240 return 0;
1241}
1242
1243/*
Christoph Lameter672bba32007-05-09 02:32:39 -07001244 * Try to allocate a partial slab from a specific node.
Christoph Lameter81819f02007-05-06 14:49:36 -07001245 */
1246static struct page *get_partial_node(struct kmem_cache_node *n)
1247{
1248 struct page *page;
1249
1250 /*
1251 * Racy check. If we mistakenly see no partial slabs then we
1252 * just allocate an empty slab. If we mistakenly try to get a
Christoph Lameter672bba32007-05-09 02:32:39 -07001253 * partial slab and there is none available then get_partials()
1254 * will return NULL.
Christoph Lameter81819f02007-05-06 14:49:36 -07001255 */
1256 if (!n || !n->nr_partial)
1257 return NULL;
1258
1259 spin_lock(&n->list_lock);
1260 list_for_each_entry(page, &n->partial, lru)
Christoph Lameter4b6f0752007-05-16 22:10:53 -07001261 if (lock_and_freeze_slab(n, page))
Christoph Lameter81819f02007-05-06 14:49:36 -07001262 goto out;
1263 page = NULL;
1264out:
1265 spin_unlock(&n->list_lock);
1266 return page;
1267}
1268
1269/*
Christoph Lameter672bba32007-05-09 02:32:39 -07001270 * Get a page from somewhere. Search in increasing NUMA distances.
Christoph Lameter81819f02007-05-06 14:49:36 -07001271 */
1272static struct page *get_any_partial(struct kmem_cache *s, gfp_t flags)
1273{
1274#ifdef CONFIG_NUMA
1275 struct zonelist *zonelist;
1276 struct zone **z;
1277 struct page *page;
1278
1279 /*
Christoph Lameter672bba32007-05-09 02:32:39 -07001280 * The defrag ratio allows a configuration of the tradeoffs between
1281 * inter node defragmentation and node local allocations. A lower
1282 * defrag_ratio increases the tendency to do local allocations
1283 * instead of attempting to obtain partial slabs from other nodes.
Christoph Lameter81819f02007-05-06 14:49:36 -07001284 *
Christoph Lameter672bba32007-05-09 02:32:39 -07001285 * If the defrag_ratio is set to 0 then kmalloc() always
1286 * returns node local objects. If the ratio is higher then kmalloc()
1287 * may return off node objects because partial slabs are obtained
1288 * from other nodes and filled up.
Christoph Lameter81819f02007-05-06 14:49:36 -07001289 *
1290 * If /sys/slab/xx/defrag_ratio is set to 100 (which makes
Christoph Lameter672bba32007-05-09 02:32:39 -07001291 * defrag_ratio = 1000) then every (well almost) allocation will
1292 * first attempt to defrag slab caches on other nodes. This means
1293 * scanning over all nodes to look for partial slabs which may be
1294 * expensive if we do it every time we are trying to find a slab
1295 * with available objects.
Christoph Lameter81819f02007-05-06 14:49:36 -07001296 */
1297 if (!s->defrag_ratio || get_cycles() % 1024 > s->defrag_ratio)
1298 return NULL;
1299
1300 zonelist = &NODE_DATA(slab_node(current->mempolicy))
1301 ->node_zonelists[gfp_zone(flags)];
1302 for (z = zonelist->zones; *z; z++) {
1303 struct kmem_cache_node *n;
1304
1305 n = get_node(s, zone_to_nid(*z));
1306
1307 if (n && cpuset_zone_allowed_hardwall(*z, flags) &&
Christoph Lametere95eed52007-05-06 14:49:44 -07001308 n->nr_partial > MIN_PARTIAL) {
Christoph Lameter81819f02007-05-06 14:49:36 -07001309 page = get_partial_node(n);
1310 if (page)
1311 return page;
1312 }
1313 }
1314#endif
1315 return NULL;
1316}
1317
1318/*
1319 * Get a partial page, lock it and return it.
1320 */
1321static struct page *get_partial(struct kmem_cache *s, gfp_t flags, int node)
1322{
1323 struct page *page;
1324 int searchnode = (node == -1) ? numa_node_id() : node;
1325
1326 page = get_partial_node(get_node(s, searchnode));
1327 if (page || (flags & __GFP_THISNODE))
1328 return page;
1329
1330 return get_any_partial(s, flags);
1331}
1332
1333/*
1334 * Move a page back to the lists.
1335 *
1336 * Must be called with the slab lock held.
1337 *
1338 * On exit the slab lock will have been dropped.
1339 */
Christoph Lameter4b6f0752007-05-16 22:10:53 -07001340static void unfreeze_slab(struct kmem_cache *s, struct page *page)
Christoph Lameter81819f02007-05-06 14:49:36 -07001341{
Christoph Lametere95eed52007-05-06 14:49:44 -07001342 struct kmem_cache_node *n = get_node(s, page_to_nid(page));
1343
Christoph Lameter4b6f0752007-05-16 22:10:53 -07001344 ClearSlabFrozen(page);
Christoph Lameter81819f02007-05-06 14:49:36 -07001345 if (page->inuse) {
Christoph Lametere95eed52007-05-06 14:49:44 -07001346
Christoph Lameter81819f02007-05-06 14:49:36 -07001347 if (page->freelist)
Christoph Lametere95eed52007-05-06 14:49:44 -07001348 add_partial(n, page);
Christoph Lameter35e5d7e2007-05-09 02:32:42 -07001349 else if (SlabDebug(page) && (s->flags & SLAB_STORE_USER))
Christoph Lametere95eed52007-05-06 14:49:44 -07001350 add_full(n, page);
Christoph Lameter81819f02007-05-06 14:49:36 -07001351 slab_unlock(page);
Christoph Lametere95eed52007-05-06 14:49:44 -07001352
Christoph Lameter81819f02007-05-06 14:49:36 -07001353 } else {
Christoph Lametere95eed52007-05-06 14:49:44 -07001354 if (n->nr_partial < MIN_PARTIAL) {
1355 /*
Christoph Lameter672bba32007-05-09 02:32:39 -07001356 * Adding an empty slab to the partial slabs in order
1357 * to avoid page allocator overhead. This slab needs
1358 * to come after the other slabs with objects in
1359 * order to fill them up. That way the size of the
1360 * partial list stays small. kmem_cache_shrink can
1361 * reclaim empty slabs from the partial list.
Christoph Lametere95eed52007-05-06 14:49:44 -07001362 */
1363 add_partial_tail(n, page);
1364 slab_unlock(page);
1365 } else {
1366 slab_unlock(page);
1367 discard_slab(s, page);
1368 }
Christoph Lameter81819f02007-05-06 14:49:36 -07001369 }
1370}
1371
1372/*
1373 * Remove the cpu slab
1374 */
1375static void deactivate_slab(struct kmem_cache *s, struct page *page, int cpu)
1376{
Christoph Lameter894b8782007-05-10 03:15:16 -07001377 /*
1378 * Merge cpu freelist into freelist. Typically we get here
1379 * because both freelists are empty. So this is unlikely
1380 * to occur.
1381 */
1382 while (unlikely(page->lockless_freelist)) {
1383 void **object;
1384
1385 /* Retrieve object from cpu_freelist */
1386 object = page->lockless_freelist;
1387 page->lockless_freelist = page->lockless_freelist[page->offset];
1388
1389 /* And put onto the regular freelist */
1390 object[page->offset] = page->freelist;
1391 page->freelist = object;
1392 page->inuse--;
1393 }
Christoph Lameter81819f02007-05-06 14:49:36 -07001394 s->cpu_slab[cpu] = NULL;
Christoph Lameter4b6f0752007-05-16 22:10:53 -07001395 unfreeze_slab(s, page);
Christoph Lameter81819f02007-05-06 14:49:36 -07001396}
1397
1398static void flush_slab(struct kmem_cache *s, struct page *page, int cpu)
1399{
1400 slab_lock(page);
1401 deactivate_slab(s, page, cpu);
1402}
1403
1404/*
1405 * Flush cpu slab.
1406 * Called from IPI handler with interrupts disabled.
1407 */
1408static void __flush_cpu_slab(struct kmem_cache *s, int cpu)
1409{
1410 struct page *page = s->cpu_slab[cpu];
1411
1412 if (likely(page))
1413 flush_slab(s, page, cpu);
1414}
1415
1416static void flush_cpu_slab(void *d)
1417{
1418 struct kmem_cache *s = d;
1419 int cpu = smp_processor_id();
1420
1421 __flush_cpu_slab(s, cpu);
1422}
1423
1424static void flush_all(struct kmem_cache *s)
1425{
1426#ifdef CONFIG_SMP
1427 on_each_cpu(flush_cpu_slab, s, 1, 1);
1428#else
1429 unsigned long flags;
1430
1431 local_irq_save(flags);
1432 flush_cpu_slab(s);
1433 local_irq_restore(flags);
1434#endif
1435}
1436
1437/*
Christoph Lameter894b8782007-05-10 03:15:16 -07001438 * Slow path. The lockless freelist is empty or we need to perform
1439 * debugging duties.
Christoph Lameter81819f02007-05-06 14:49:36 -07001440 *
Christoph Lameter894b8782007-05-10 03:15:16 -07001441 * Interrupts are disabled.
Christoph Lameter81819f02007-05-06 14:49:36 -07001442 *
Christoph Lameter894b8782007-05-10 03:15:16 -07001443 * Processing is still very fast if new objects have been freed to the
1444 * regular freelist. In that case we simply take over the regular freelist
1445 * as the lockless freelist and zap the regular freelist.
Christoph Lameter81819f02007-05-06 14:49:36 -07001446 *
Christoph Lameter894b8782007-05-10 03:15:16 -07001447 * If that is not working then we fall back to the partial lists. We take the
1448 * first element of the freelist as the object to allocate now and move the
1449 * rest of the freelist to the lockless freelist.
1450 *
1451 * And if we were unable to get a new slab from the partial slab lists then
1452 * we need to allocate a new slab. This is slowest path since we may sleep.
Christoph Lameter81819f02007-05-06 14:49:36 -07001453 */
Christoph Lameter894b8782007-05-10 03:15:16 -07001454static void *__slab_alloc(struct kmem_cache *s,
1455 gfp_t gfpflags, int node, void *addr, struct page *page)
Christoph Lameter81819f02007-05-06 14:49:36 -07001456{
Christoph Lameter81819f02007-05-06 14:49:36 -07001457 void **object;
Christoph Lameter894b8782007-05-10 03:15:16 -07001458 int cpu = smp_processor_id();
Christoph Lameter81819f02007-05-06 14:49:36 -07001459
Christoph Lameter81819f02007-05-06 14:49:36 -07001460 if (!page)
1461 goto new_slab;
1462
1463 slab_lock(page);
1464 if (unlikely(node != -1 && page_to_nid(page) != node))
1465 goto another_slab;
Christoph Lameter894b8782007-05-10 03:15:16 -07001466load_freelist:
Christoph Lameter81819f02007-05-06 14:49:36 -07001467 object = page->freelist;
1468 if (unlikely(!object))
1469 goto another_slab;
Christoph Lameter35e5d7e2007-05-09 02:32:42 -07001470 if (unlikely(SlabDebug(page)))
Christoph Lameter81819f02007-05-06 14:49:36 -07001471 goto debug;
1472
Christoph Lameter894b8782007-05-10 03:15:16 -07001473 object = page->freelist;
1474 page->lockless_freelist = object[page->offset];
1475 page->inuse = s->objects;
1476 page->freelist = NULL;
Christoph Lameter81819f02007-05-06 14:49:36 -07001477 slab_unlock(page);
Christoph Lameter81819f02007-05-06 14:49:36 -07001478 return object;
1479
1480another_slab:
1481 deactivate_slab(s, page, cpu);
1482
1483new_slab:
1484 page = get_partial(s, gfpflags, node);
Christoph Lameter894b8782007-05-10 03:15:16 -07001485 if (page) {
Christoph Lameter81819f02007-05-06 14:49:36 -07001486 s->cpu_slab[cpu] = page;
Christoph Lameter894b8782007-05-10 03:15:16 -07001487 goto load_freelist;
Christoph Lameter81819f02007-05-06 14:49:36 -07001488 }
1489
1490 page = new_slab(s, gfpflags, node);
1491 if (page) {
1492 cpu = smp_processor_id();
1493 if (s->cpu_slab[cpu]) {
1494 /*
Christoph Lameter672bba32007-05-09 02:32:39 -07001495 * Someone else populated the cpu_slab while we
1496 * enabled interrupts, or we have gotten scheduled
1497 * on another cpu. The page may not be on the
1498 * requested node even if __GFP_THISNODE was
1499 * specified. So we need to recheck.
Christoph Lameter81819f02007-05-06 14:49:36 -07001500 */
1501 if (node == -1 ||
1502 page_to_nid(s->cpu_slab[cpu]) == node) {
1503 /*
1504 * Current cpuslab is acceptable and we
1505 * want the current one since its cache hot
1506 */
1507 discard_slab(s, page);
1508 page = s->cpu_slab[cpu];
1509 slab_lock(page);
Christoph Lameter894b8782007-05-10 03:15:16 -07001510 goto load_freelist;
Christoph Lameter81819f02007-05-06 14:49:36 -07001511 }
Christoph Lameter672bba32007-05-09 02:32:39 -07001512 /* New slab does not fit our expectations */
Christoph Lameter81819f02007-05-06 14:49:36 -07001513 flush_slab(s, s->cpu_slab[cpu], cpu);
1514 }
1515 slab_lock(page);
Christoph Lameter4b6f0752007-05-16 22:10:53 -07001516 SetSlabFrozen(page);
1517 s->cpu_slab[cpu] = page;
1518 goto load_freelist;
Christoph Lameter81819f02007-05-06 14:49:36 -07001519 }
Christoph Lameter81819f02007-05-06 14:49:36 -07001520 return NULL;
1521debug:
Christoph Lameter894b8782007-05-10 03:15:16 -07001522 object = page->freelist;
Christoph Lameter3ec09742007-05-16 22:11:00 -07001523 if (!alloc_debug_processing(s, page, object, addr))
Christoph Lameter81819f02007-05-06 14:49:36 -07001524 goto another_slab;
Christoph Lameter894b8782007-05-10 03:15:16 -07001525
1526 page->inuse++;
1527 page->freelist = object[page->offset];
1528 slab_unlock(page);
1529 return object;
1530}
1531
1532/*
1533 * Inlined fastpath so that allocation functions (kmalloc, kmem_cache_alloc)
1534 * have the fastpath folded into their functions. So no function call
1535 * overhead for requests that can be satisfied on the fastpath.
1536 *
1537 * The fastpath works by first checking if the lockless freelist can be used.
1538 * If not then __slab_alloc is called for slow processing.
1539 *
1540 * Otherwise we can simply pick the next object from the lockless free list.
1541 */
1542static void __always_inline *slab_alloc(struct kmem_cache *s,
1543 gfp_t gfpflags, int node, void *addr)
1544{
1545 struct page *page;
1546 void **object;
1547 unsigned long flags;
1548
1549 local_irq_save(flags);
1550 page = s->cpu_slab[smp_processor_id()];
1551 if (unlikely(!page || !page->lockless_freelist ||
1552 (node != -1 && page_to_nid(page) != node)))
1553
1554 object = __slab_alloc(s, gfpflags, node, addr, page);
1555
1556 else {
1557 object = page->lockless_freelist;
1558 page->lockless_freelist = object[page->offset];
1559 }
1560 local_irq_restore(flags);
1561 return object;
Christoph Lameter81819f02007-05-06 14:49:36 -07001562}
1563
1564void *kmem_cache_alloc(struct kmem_cache *s, gfp_t gfpflags)
1565{
Christoph Lameter77c5e2d2007-05-06 14:49:42 -07001566 return slab_alloc(s, gfpflags, -1, __builtin_return_address(0));
Christoph Lameter81819f02007-05-06 14:49:36 -07001567}
1568EXPORT_SYMBOL(kmem_cache_alloc);
1569
1570#ifdef CONFIG_NUMA
1571void *kmem_cache_alloc_node(struct kmem_cache *s, gfp_t gfpflags, int node)
1572{
Christoph Lameter77c5e2d2007-05-06 14:49:42 -07001573 return slab_alloc(s, gfpflags, node, __builtin_return_address(0));
Christoph Lameter81819f02007-05-06 14:49:36 -07001574}
1575EXPORT_SYMBOL(kmem_cache_alloc_node);
1576#endif
1577
1578/*
Christoph Lameter894b8782007-05-10 03:15:16 -07001579 * Slow patch handling. This may still be called frequently since objects
1580 * have a longer lifetime than the cpu slabs in most processing loads.
Christoph Lameter81819f02007-05-06 14:49:36 -07001581 *
Christoph Lameter894b8782007-05-10 03:15:16 -07001582 * So we still attempt to reduce cache line usage. Just take the slab
1583 * lock and free the item. If there is no additional partial page
1584 * handling required then we can return immediately.
Christoph Lameter81819f02007-05-06 14:49:36 -07001585 */
Christoph Lameter894b8782007-05-10 03:15:16 -07001586static void __slab_free(struct kmem_cache *s, struct page *page,
Christoph Lameter77c5e2d2007-05-06 14:49:42 -07001587 void *x, void *addr)
Christoph Lameter81819f02007-05-06 14:49:36 -07001588{
1589 void *prior;
1590 void **object = (void *)x;
Christoph Lameter81819f02007-05-06 14:49:36 -07001591
Christoph Lameter81819f02007-05-06 14:49:36 -07001592 slab_lock(page);
1593
Christoph Lameter35e5d7e2007-05-09 02:32:42 -07001594 if (unlikely(SlabDebug(page)))
Christoph Lameter81819f02007-05-06 14:49:36 -07001595 goto debug;
1596checks_ok:
1597 prior = object[page->offset] = page->freelist;
1598 page->freelist = object;
1599 page->inuse--;
1600
Christoph Lameter4b6f0752007-05-16 22:10:53 -07001601 if (unlikely(SlabFrozen(page)))
Christoph Lameter81819f02007-05-06 14:49:36 -07001602 goto out_unlock;
1603
1604 if (unlikely(!page->inuse))
1605 goto slab_empty;
1606
1607 /*
1608 * Objects left in the slab. If it
1609 * was not on the partial list before
1610 * then add it.
1611 */
1612 if (unlikely(!prior))
Christoph Lametere95eed52007-05-06 14:49:44 -07001613 add_partial(get_node(s, page_to_nid(page)), page);
Christoph Lameter81819f02007-05-06 14:49:36 -07001614
1615out_unlock:
1616 slab_unlock(page);
Christoph Lameter81819f02007-05-06 14:49:36 -07001617 return;
1618
1619slab_empty:
1620 if (prior)
1621 /*
Christoph Lameter672bba32007-05-09 02:32:39 -07001622 * Slab still on the partial list.
Christoph Lameter81819f02007-05-06 14:49:36 -07001623 */
1624 remove_partial(s, page);
1625
1626 slab_unlock(page);
1627 discard_slab(s, page);
Christoph Lameter81819f02007-05-06 14:49:36 -07001628 return;
1629
1630debug:
Christoph Lameter3ec09742007-05-16 22:11:00 -07001631 if (!free_debug_processing(s, page, x, addr))
Christoph Lameter77c5e2d2007-05-06 14:49:42 -07001632 goto out_unlock;
Christoph Lameter77c5e2d2007-05-06 14:49:42 -07001633 goto checks_ok;
Christoph Lameter81819f02007-05-06 14:49:36 -07001634}
1635
Christoph Lameter894b8782007-05-10 03:15:16 -07001636/*
1637 * Fastpath with forced inlining to produce a kfree and kmem_cache_free that
1638 * can perform fastpath freeing without additional function calls.
1639 *
1640 * The fastpath is only possible if we are freeing to the current cpu slab
1641 * of this processor. This typically the case if we have just allocated
1642 * the item before.
1643 *
1644 * If fastpath is not possible then fall back to __slab_free where we deal
1645 * with all sorts of special processing.
1646 */
1647static void __always_inline slab_free(struct kmem_cache *s,
1648 struct page *page, void *x, void *addr)
1649{
1650 void **object = (void *)x;
1651 unsigned long flags;
1652
1653 local_irq_save(flags);
1654 if (likely(page == s->cpu_slab[smp_processor_id()] &&
1655 !SlabDebug(page))) {
1656 object[page->offset] = page->lockless_freelist;
1657 page->lockless_freelist = object;
1658 } else
1659 __slab_free(s, page, x, addr);
1660
1661 local_irq_restore(flags);
1662}
1663
Christoph Lameter81819f02007-05-06 14:49:36 -07001664void kmem_cache_free(struct kmem_cache *s, void *x)
1665{
Christoph Lameter77c5e2d2007-05-06 14:49:42 -07001666 struct page *page;
Christoph Lameter81819f02007-05-06 14:49:36 -07001667
Christoph Lameterb49af682007-05-06 14:49:41 -07001668 page = virt_to_head_page(x);
Christoph Lameter81819f02007-05-06 14:49:36 -07001669
Christoph Lameter77c5e2d2007-05-06 14:49:42 -07001670 slab_free(s, page, x, __builtin_return_address(0));
Christoph Lameter81819f02007-05-06 14:49:36 -07001671}
1672EXPORT_SYMBOL(kmem_cache_free);
1673
1674/* Figure out on which slab object the object resides */
1675static struct page *get_object_page(const void *x)
1676{
Christoph Lameterb49af682007-05-06 14:49:41 -07001677 struct page *page = virt_to_head_page(x);
Christoph Lameter81819f02007-05-06 14:49:36 -07001678
1679 if (!PageSlab(page))
1680 return NULL;
1681
1682 return page;
1683}
1684
1685/*
Christoph Lameter672bba32007-05-09 02:32:39 -07001686 * Object placement in a slab is made very easy because we always start at
1687 * offset 0. If we tune the size of the object to the alignment then we can
1688 * get the required alignment by putting one properly sized object after
1689 * another.
Christoph Lameter81819f02007-05-06 14:49:36 -07001690 *
1691 * Notice that the allocation order determines the sizes of the per cpu
1692 * caches. Each processor has always one slab available for allocations.
1693 * Increasing the allocation order reduces the number of times that slabs
Christoph Lameter672bba32007-05-09 02:32:39 -07001694 * must be moved on and off the partial lists and is therefore a factor in
Christoph Lameter81819f02007-05-06 14:49:36 -07001695 * locking overhead.
Christoph Lameter81819f02007-05-06 14:49:36 -07001696 */
1697
1698/*
1699 * Mininum / Maximum order of slab pages. This influences locking overhead
1700 * and slab fragmentation. A higher order reduces the number of partial slabs
1701 * and increases the number of allocations possible without having to
1702 * take the list_lock.
1703 */
1704static int slub_min_order;
1705static int slub_max_order = DEFAULT_MAX_ORDER;
Christoph Lameter81819f02007-05-06 14:49:36 -07001706static int slub_min_objects = DEFAULT_MIN_OBJECTS;
1707
1708/*
1709 * Merge control. If this is set then no merging of slab caches will occur.
Christoph Lameter672bba32007-05-09 02:32:39 -07001710 * (Could be removed. This was introduced to pacify the merge skeptics.)
Christoph Lameter81819f02007-05-06 14:49:36 -07001711 */
1712static int slub_nomerge;
1713
1714/*
Christoph Lameter81819f02007-05-06 14:49:36 -07001715 * Calculate the order of allocation given an slab object size.
1716 *
Christoph Lameter672bba32007-05-09 02:32:39 -07001717 * The order of allocation has significant impact on performance and other
1718 * system components. Generally order 0 allocations should be preferred since
1719 * order 0 does not cause fragmentation in the page allocator. Larger objects
1720 * be problematic to put into order 0 slabs because there may be too much
1721 * unused space left. We go to a higher order if more than 1/8th of the slab
1722 * would be wasted.
Christoph Lameter81819f02007-05-06 14:49:36 -07001723 *
Christoph Lameter672bba32007-05-09 02:32:39 -07001724 * In order to reach satisfactory performance we must ensure that a minimum
1725 * number of objects is in one slab. Otherwise we may generate too much
1726 * activity on the partial lists which requires taking the list_lock. This is
1727 * less a concern for large slabs though which are rarely used.
Christoph Lameter81819f02007-05-06 14:49:36 -07001728 *
Christoph Lameter672bba32007-05-09 02:32:39 -07001729 * slub_max_order specifies the order where we begin to stop considering the
1730 * number of objects in a slab as critical. If we reach slub_max_order then
1731 * we try to keep the page order as low as possible. So we accept more waste
1732 * of space in favor of a small page order.
1733 *
1734 * Higher order allocations also allow the placement of more objects in a
1735 * slab and thereby reduce object handling overhead. If the user has
1736 * requested a higher mininum order then we start with that one instead of
1737 * the smallest order which will fit the object.
Christoph Lameter81819f02007-05-06 14:49:36 -07001738 */
Christoph Lameter5e6d4442007-05-09 02:32:46 -07001739static inline int slab_order(int size, int min_objects,
1740 int max_order, int fract_leftover)
Christoph Lameter81819f02007-05-06 14:49:36 -07001741{
1742 int order;
1743 int rem;
Christoph Lameter6300ea72007-07-17 04:03:20 -07001744 int min_order = slub_min_order;
Christoph Lameter81819f02007-05-06 14:49:36 -07001745
Christoph Lameter6300ea72007-07-17 04:03:20 -07001746 /*
1747 * If we would create too many object per slab then reduce
1748 * the slab order even if it goes below slub_min_order.
1749 */
1750 while (min_order > 0 &&
1751 (PAGE_SIZE << min_order) >= MAX_OBJECTS_PER_SLAB * size)
1752 min_order--;
1753
1754 for (order = max(min_order,
Christoph Lameter5e6d4442007-05-09 02:32:46 -07001755 fls(min_objects * size - 1) - PAGE_SHIFT);
1756 order <= max_order; order++) {
1757
Christoph Lameter81819f02007-05-06 14:49:36 -07001758 unsigned long slab_size = PAGE_SIZE << order;
1759
Christoph Lameter5e6d4442007-05-09 02:32:46 -07001760 if (slab_size < min_objects * size)
Christoph Lameter81819f02007-05-06 14:49:36 -07001761 continue;
1762
Christoph Lameter81819f02007-05-06 14:49:36 -07001763 rem = slab_size % size;
1764
Christoph Lameter5e6d4442007-05-09 02:32:46 -07001765 if (rem <= slab_size / fract_leftover)
Christoph Lameter81819f02007-05-06 14:49:36 -07001766 break;
1767
Christoph Lameter6300ea72007-07-17 04:03:20 -07001768 /* If the next size is too high then exit now */
1769 if (slab_size * 2 >= MAX_OBJECTS_PER_SLAB * size)
1770 break;
Christoph Lameter81819f02007-05-06 14:49:36 -07001771 }
Christoph Lameter672bba32007-05-09 02:32:39 -07001772
Christoph Lameter81819f02007-05-06 14:49:36 -07001773 return order;
1774}
1775
Christoph Lameter5e6d4442007-05-09 02:32:46 -07001776static inline int calculate_order(int size)
1777{
1778 int order;
1779 int min_objects;
1780 int fraction;
1781
1782 /*
1783 * Attempt to find best configuration for a slab. This
1784 * works by first attempting to generate a layout with
1785 * the best configuration and backing off gradually.
1786 *
1787 * First we reduce the acceptable waste in a slab. Then
1788 * we reduce the minimum objects required in a slab.
1789 */
1790 min_objects = slub_min_objects;
1791 while (min_objects > 1) {
1792 fraction = 8;
1793 while (fraction >= 4) {
1794 order = slab_order(size, min_objects,
1795 slub_max_order, fraction);
1796 if (order <= slub_max_order)
1797 return order;
1798 fraction /= 2;
1799 }
1800 min_objects /= 2;
1801 }
1802
1803 /*
1804 * We were unable to place multiple objects in a slab. Now
1805 * lets see if we can place a single object there.
1806 */
1807 order = slab_order(size, 1, slub_max_order, 1);
1808 if (order <= slub_max_order)
1809 return order;
1810
1811 /*
1812 * Doh this slab cannot be placed using slub_max_order.
1813 */
1814 order = slab_order(size, 1, MAX_ORDER, 1);
1815 if (order <= MAX_ORDER)
1816 return order;
1817 return -ENOSYS;
1818}
1819
Christoph Lameter81819f02007-05-06 14:49:36 -07001820/*
Christoph Lameter672bba32007-05-09 02:32:39 -07001821 * Figure out what the alignment of the objects will be.
Christoph Lameter81819f02007-05-06 14:49:36 -07001822 */
1823static unsigned long calculate_alignment(unsigned long flags,
1824 unsigned long align, unsigned long size)
1825{
1826 /*
1827 * If the user wants hardware cache aligned objects then
1828 * follow that suggestion if the object is sufficiently
1829 * large.
1830 *
1831 * The hardware cache alignment cannot override the
1832 * specified alignment though. If that is greater
1833 * then use it.
1834 */
Christoph Lameter5af60832007-05-06 14:49:56 -07001835 if ((flags & SLAB_HWCACHE_ALIGN) &&
Christoph Lameter65c02d42007-05-09 02:32:35 -07001836 size > cache_line_size() / 2)
1837 return max_t(unsigned long, align, cache_line_size());
Christoph Lameter81819f02007-05-06 14:49:36 -07001838
1839 if (align < ARCH_SLAB_MINALIGN)
1840 return ARCH_SLAB_MINALIGN;
1841
1842 return ALIGN(align, sizeof(void *));
1843}
1844
1845static void init_kmem_cache_node(struct kmem_cache_node *n)
1846{
1847 n->nr_partial = 0;
1848 atomic_long_set(&n->nr_slabs, 0);
1849 spin_lock_init(&n->list_lock);
1850 INIT_LIST_HEAD(&n->partial);
Christoph Lameter643b1132007-05-06 14:49:42 -07001851 INIT_LIST_HEAD(&n->full);
Christoph Lameter81819f02007-05-06 14:49:36 -07001852}
1853
1854#ifdef CONFIG_NUMA
1855/*
1856 * No kmalloc_node yet so do it by hand. We know that this is the first
1857 * slab on the node for this slabcache. There are no concurrent accesses
1858 * possible.
1859 *
1860 * Note that this function only works on the kmalloc_node_cache
1861 * when allocating for the kmalloc_node_cache.
1862 */
1863static struct kmem_cache_node * __init early_kmem_cache_node_alloc(gfp_t gfpflags,
1864 int node)
1865{
1866 struct page *page;
1867 struct kmem_cache_node *n;
1868
1869 BUG_ON(kmalloc_caches->size < sizeof(struct kmem_cache_node));
1870
1871 page = new_slab(kmalloc_caches, gfpflags | GFP_THISNODE, node);
Christoph Lameter81819f02007-05-06 14:49:36 -07001872
1873 BUG_ON(!page);
1874 n = page->freelist;
1875 BUG_ON(!n);
1876 page->freelist = get_freepointer(kmalloc_caches, n);
1877 page->inuse++;
1878 kmalloc_caches->node[node] = n;
Christoph Lameter3ec09742007-05-16 22:11:00 -07001879 setup_object_debug(kmalloc_caches, page, n);
Christoph Lameter81819f02007-05-06 14:49:36 -07001880 init_kmem_cache_node(n);
1881 atomic_long_inc(&n->nr_slabs);
Christoph Lametere95eed52007-05-06 14:49:44 -07001882 add_partial(n, page);
Christoph Lameterdbc55fa2007-07-03 09:31:04 -07001883
1884 /*
1885 * new_slab() disables interupts. If we do not reenable interrupts here
1886 * then bootup would continue with interrupts disabled.
1887 */
1888 local_irq_enable();
Christoph Lameter81819f02007-05-06 14:49:36 -07001889 return n;
1890}
1891
1892static void free_kmem_cache_nodes(struct kmem_cache *s)
1893{
1894 int node;
1895
1896 for_each_online_node(node) {
1897 struct kmem_cache_node *n = s->node[node];
1898 if (n && n != &s->local_node)
1899 kmem_cache_free(kmalloc_caches, n);
1900 s->node[node] = NULL;
1901 }
1902}
1903
1904static int init_kmem_cache_nodes(struct kmem_cache *s, gfp_t gfpflags)
1905{
1906 int node;
1907 int local_node;
1908
1909 if (slab_state >= UP)
1910 local_node = page_to_nid(virt_to_page(s));
1911 else
1912 local_node = 0;
1913
1914 for_each_online_node(node) {
1915 struct kmem_cache_node *n;
1916
1917 if (local_node == node)
1918 n = &s->local_node;
1919 else {
1920 if (slab_state == DOWN) {
1921 n = early_kmem_cache_node_alloc(gfpflags,
1922 node);
1923 continue;
1924 }
1925 n = kmem_cache_alloc_node(kmalloc_caches,
1926 gfpflags, node);
1927
1928 if (!n) {
1929 free_kmem_cache_nodes(s);
1930 return 0;
1931 }
1932
1933 }
1934 s->node[node] = n;
1935 init_kmem_cache_node(n);
1936 }
1937 return 1;
1938}
1939#else
1940static void free_kmem_cache_nodes(struct kmem_cache *s)
1941{
1942}
1943
1944static int init_kmem_cache_nodes(struct kmem_cache *s, gfp_t gfpflags)
1945{
1946 init_kmem_cache_node(&s->local_node);
1947 return 1;
1948}
1949#endif
1950
1951/*
1952 * calculate_sizes() determines the order and the distribution of data within
1953 * a slab object.
1954 */
1955static int calculate_sizes(struct kmem_cache *s)
1956{
1957 unsigned long flags = s->flags;
1958 unsigned long size = s->objsize;
1959 unsigned long align = s->align;
1960
1961 /*
1962 * Determine if we can poison the object itself. If the user of
1963 * the slab may touch the object after free or before allocation
1964 * then we should never poison the object itself.
1965 */
1966 if ((flags & SLAB_POISON) && !(flags & SLAB_DESTROY_BY_RCU) &&
Christoph Lameterc59def9f2007-05-16 22:10:50 -07001967 !s->ctor)
Christoph Lameter81819f02007-05-06 14:49:36 -07001968 s->flags |= __OBJECT_POISON;
1969 else
1970 s->flags &= ~__OBJECT_POISON;
1971
1972 /*
1973 * Round up object size to the next word boundary. We can only
1974 * place the free pointer at word boundaries and this determines
1975 * the possible location of the free pointer.
1976 */
1977 size = ALIGN(size, sizeof(void *));
1978
Christoph Lameter41ecc552007-05-09 02:32:44 -07001979#ifdef CONFIG_SLUB_DEBUG
Christoph Lameter81819f02007-05-06 14:49:36 -07001980 /*
Christoph Lameter672bba32007-05-09 02:32:39 -07001981 * If we are Redzoning then check if there is some space between the
Christoph Lameter81819f02007-05-06 14:49:36 -07001982 * end of the object and the free pointer. If not then add an
Christoph Lameter672bba32007-05-09 02:32:39 -07001983 * additional word to have some bytes to store Redzone information.
Christoph Lameter81819f02007-05-06 14:49:36 -07001984 */
1985 if ((flags & SLAB_RED_ZONE) && size == s->objsize)
1986 size += sizeof(void *);
Christoph Lameter41ecc552007-05-09 02:32:44 -07001987#endif
Christoph Lameter81819f02007-05-06 14:49:36 -07001988
1989 /*
Christoph Lameter672bba32007-05-09 02:32:39 -07001990 * With that we have determined the number of bytes in actual use
1991 * by the object. This is the potential offset to the free pointer.
Christoph Lameter81819f02007-05-06 14:49:36 -07001992 */
1993 s->inuse = size;
1994
1995 if (((flags & (SLAB_DESTROY_BY_RCU | SLAB_POISON)) ||
Christoph Lameterc59def9f2007-05-16 22:10:50 -07001996 s->ctor)) {
Christoph Lameter81819f02007-05-06 14:49:36 -07001997 /*
1998 * Relocate free pointer after the object if it is not
1999 * permitted to overwrite the first word of the object on
2000 * kmem_cache_free.
2001 *
2002 * This is the case if we do RCU, have a constructor or
2003 * destructor or are poisoning the objects.
2004 */
2005 s->offset = size;
2006 size += sizeof(void *);
2007 }
2008
Christoph Lameterc12b3c62007-05-23 13:57:31 -07002009#ifdef CONFIG_SLUB_DEBUG
Christoph Lameter81819f02007-05-06 14:49:36 -07002010 if (flags & SLAB_STORE_USER)
2011 /*
2012 * Need to store information about allocs and frees after
2013 * the object.
2014 */
2015 size += 2 * sizeof(struct track);
2016
Christoph Lameterbe7b3fb2007-05-09 02:32:36 -07002017 if (flags & SLAB_RED_ZONE)
Christoph Lameter81819f02007-05-06 14:49:36 -07002018 /*
2019 * Add some empty padding so that we can catch
2020 * overwrites from earlier objects rather than let
2021 * tracking information or the free pointer be
2022 * corrupted if an user writes before the start
2023 * of the object.
2024 */
2025 size += sizeof(void *);
Christoph Lameter41ecc552007-05-09 02:32:44 -07002026#endif
Christoph Lameter672bba32007-05-09 02:32:39 -07002027
Christoph Lameter81819f02007-05-06 14:49:36 -07002028 /*
2029 * Determine the alignment based on various parameters that the
Christoph Lameter65c02d42007-05-09 02:32:35 -07002030 * user specified and the dynamic determination of cache line size
2031 * on bootup.
Christoph Lameter81819f02007-05-06 14:49:36 -07002032 */
2033 align = calculate_alignment(flags, align, s->objsize);
2034
2035 /*
2036 * SLUB stores one object immediately after another beginning from
2037 * offset 0. In order to align the objects we have to simply size
2038 * each object to conform to the alignment.
2039 */
2040 size = ALIGN(size, align);
2041 s->size = size;
2042
2043 s->order = calculate_order(size);
2044 if (s->order < 0)
2045 return 0;
2046
2047 /*
2048 * Determine the number of objects per slab
2049 */
2050 s->objects = (PAGE_SIZE << s->order) / size;
2051
2052 /*
2053 * Verify that the number of objects is within permitted limits.
2054 * The page->inuse field is only 16 bit wide! So we cannot have
2055 * more than 64k objects per slab.
2056 */
Christoph Lameter6300ea72007-07-17 04:03:20 -07002057 if (!s->objects || s->objects > MAX_OBJECTS_PER_SLAB)
Christoph Lameter81819f02007-05-06 14:49:36 -07002058 return 0;
2059 return 1;
2060
2061}
2062
Christoph Lameter81819f02007-05-06 14:49:36 -07002063static int kmem_cache_open(struct kmem_cache *s, gfp_t gfpflags,
2064 const char *name, size_t size,
2065 size_t align, unsigned long flags,
Christoph Lameterc59def9f2007-05-16 22:10:50 -07002066 void (*ctor)(void *, struct kmem_cache *, unsigned long))
Christoph Lameter81819f02007-05-06 14:49:36 -07002067{
2068 memset(s, 0, kmem_size);
2069 s->name = name;
2070 s->ctor = ctor;
Christoph Lameter81819f02007-05-06 14:49:36 -07002071 s->objsize = size;
2072 s->flags = flags;
2073 s->align = align;
Christoph Lameter41ecc552007-05-09 02:32:44 -07002074 kmem_cache_open_debug_check(s);
Christoph Lameter81819f02007-05-06 14:49:36 -07002075
2076 if (!calculate_sizes(s))
2077 goto error;
2078
2079 s->refcount = 1;
2080#ifdef CONFIG_NUMA
2081 s->defrag_ratio = 100;
2082#endif
2083
2084 if (init_kmem_cache_nodes(s, gfpflags & ~SLUB_DMA))
2085 return 1;
2086error:
2087 if (flags & SLAB_PANIC)
2088 panic("Cannot create slab %s size=%lu realsize=%u "
2089 "order=%u offset=%u flags=%lx\n",
2090 s->name, (unsigned long)size, s->size, s->order,
2091 s->offset, flags);
2092 return 0;
2093}
Christoph Lameter81819f02007-05-06 14:49:36 -07002094
2095/*
2096 * Check if a given pointer is valid
2097 */
2098int kmem_ptr_validate(struct kmem_cache *s, const void *object)
2099{
2100 struct page * page;
Christoph Lameter81819f02007-05-06 14:49:36 -07002101
2102 page = get_object_page(object);
2103
2104 if (!page || s != page->slab)
2105 /* No slab or wrong slab */
2106 return 0;
2107
Christoph Lameterabcd08a2007-05-09 02:32:37 -07002108 if (!check_valid_pointer(s, page, object))
Christoph Lameter81819f02007-05-06 14:49:36 -07002109 return 0;
2110
2111 /*
2112 * We could also check if the object is on the slabs freelist.
2113 * But this would be too expensive and it seems that the main
2114 * purpose of kmem_ptr_valid is to check if the object belongs
2115 * to a certain slab.
2116 */
2117 return 1;
2118}
2119EXPORT_SYMBOL(kmem_ptr_validate);
2120
2121/*
2122 * Determine the size of a slab object
2123 */
2124unsigned int kmem_cache_size(struct kmem_cache *s)
2125{
2126 return s->objsize;
2127}
2128EXPORT_SYMBOL(kmem_cache_size);
2129
2130const char *kmem_cache_name(struct kmem_cache *s)
2131{
2132 return s->name;
2133}
2134EXPORT_SYMBOL(kmem_cache_name);
2135
2136/*
Christoph Lameter672bba32007-05-09 02:32:39 -07002137 * Attempt to free all slabs on a node. Return the number of slabs we
2138 * were unable to free.
Christoph Lameter81819f02007-05-06 14:49:36 -07002139 */
2140static int free_list(struct kmem_cache *s, struct kmem_cache_node *n,
2141 struct list_head *list)
2142{
2143 int slabs_inuse = 0;
2144 unsigned long flags;
2145 struct page *page, *h;
2146
2147 spin_lock_irqsave(&n->list_lock, flags);
2148 list_for_each_entry_safe(page, h, list, lru)
2149 if (!page->inuse) {
2150 list_del(&page->lru);
2151 discard_slab(s, page);
2152 } else
2153 slabs_inuse++;
2154 spin_unlock_irqrestore(&n->list_lock, flags);
2155 return slabs_inuse;
2156}
2157
2158/*
Christoph Lameter672bba32007-05-09 02:32:39 -07002159 * Release all resources used by a slab cache.
Christoph Lameter81819f02007-05-06 14:49:36 -07002160 */
2161static int kmem_cache_close(struct kmem_cache *s)
2162{
2163 int node;
2164
2165 flush_all(s);
2166
2167 /* Attempt to free all objects */
2168 for_each_online_node(node) {
2169 struct kmem_cache_node *n = get_node(s, node);
2170
Christoph Lameter2086d262007-05-06 14:49:46 -07002171 n->nr_partial -= free_list(s, n, &n->partial);
Christoph Lameter81819f02007-05-06 14:49:36 -07002172 if (atomic_long_read(&n->nr_slabs))
2173 return 1;
2174 }
2175 free_kmem_cache_nodes(s);
2176 return 0;
2177}
2178
2179/*
2180 * Close a cache and release the kmem_cache structure
2181 * (must be used for caches created using kmem_cache_create)
2182 */
2183void kmem_cache_destroy(struct kmem_cache *s)
2184{
2185 down_write(&slub_lock);
2186 s->refcount--;
2187 if (!s->refcount) {
2188 list_del(&s->list);
2189 if (kmem_cache_close(s))
2190 WARN_ON(1);
2191 sysfs_slab_remove(s);
2192 kfree(s);
2193 }
2194 up_write(&slub_lock);
2195}
2196EXPORT_SYMBOL(kmem_cache_destroy);
2197
2198/********************************************************************
2199 * Kmalloc subsystem
2200 *******************************************************************/
2201
2202struct kmem_cache kmalloc_caches[KMALLOC_SHIFT_HIGH + 1] __cacheline_aligned;
2203EXPORT_SYMBOL(kmalloc_caches);
2204
2205#ifdef CONFIG_ZONE_DMA
2206static struct kmem_cache *kmalloc_caches_dma[KMALLOC_SHIFT_HIGH + 1];
2207#endif
2208
2209static int __init setup_slub_min_order(char *str)
2210{
2211 get_option (&str, &slub_min_order);
2212
2213 return 1;
2214}
2215
2216__setup("slub_min_order=", setup_slub_min_order);
2217
2218static int __init setup_slub_max_order(char *str)
2219{
2220 get_option (&str, &slub_max_order);
2221
2222 return 1;
2223}
2224
2225__setup("slub_max_order=", setup_slub_max_order);
2226
2227static int __init setup_slub_min_objects(char *str)
2228{
2229 get_option (&str, &slub_min_objects);
2230
2231 return 1;
2232}
2233
2234__setup("slub_min_objects=", setup_slub_min_objects);
2235
2236static int __init setup_slub_nomerge(char *str)
2237{
2238 slub_nomerge = 1;
2239 return 1;
2240}
2241
2242__setup("slub_nomerge", setup_slub_nomerge);
2243
Christoph Lameter81819f02007-05-06 14:49:36 -07002244static struct kmem_cache *create_kmalloc_cache(struct kmem_cache *s,
2245 const char *name, int size, gfp_t gfp_flags)
2246{
2247 unsigned int flags = 0;
2248
2249 if (gfp_flags & SLUB_DMA)
2250 flags = SLAB_CACHE_DMA;
2251
2252 down_write(&slub_lock);
2253 if (!kmem_cache_open(s, gfp_flags, name, size, ARCH_KMALLOC_MINALIGN,
Christoph Lameterc59def9f2007-05-16 22:10:50 -07002254 flags, NULL))
Christoph Lameter81819f02007-05-06 14:49:36 -07002255 goto panic;
2256
2257 list_add(&s->list, &slab_caches);
2258 up_write(&slub_lock);
2259 if (sysfs_slab_add(s))
2260 goto panic;
2261 return s;
2262
2263panic:
2264 panic("Creation of kmalloc slab %s size=%d failed.\n", name, size);
2265}
2266
2267static struct kmem_cache *get_slab(size_t size, gfp_t flags)
2268{
2269 int index = kmalloc_index(size);
2270
Christoph Lameter614410d2007-05-06 14:49:38 -07002271 if (!index)
Christoph Lameter81819f02007-05-06 14:49:36 -07002272 return NULL;
2273
2274 /* Allocation too large? */
2275 BUG_ON(index < 0);
2276
2277#ifdef CONFIG_ZONE_DMA
2278 if ((flags & SLUB_DMA)) {
2279 struct kmem_cache *s;
2280 struct kmem_cache *x;
2281 char *text;
2282 size_t realsize;
2283
2284 s = kmalloc_caches_dma[index];
2285 if (s)
2286 return s;
2287
2288 /* Dynamically create dma cache */
2289 x = kmalloc(kmem_size, flags & ~SLUB_DMA);
2290 if (!x)
2291 panic("Unable to allocate memory for dma cache\n");
2292
2293 if (index <= KMALLOC_SHIFT_HIGH)
2294 realsize = 1 << index;
2295 else {
2296 if (index == 1)
2297 realsize = 96;
2298 else
2299 realsize = 192;
2300 }
2301
2302 text = kasprintf(flags & ~SLUB_DMA, "kmalloc_dma-%d",
2303 (unsigned int)realsize);
2304 s = create_kmalloc_cache(x, text, realsize, flags);
2305 kmalloc_caches_dma[index] = s;
2306 return s;
2307 }
2308#endif
2309 return &kmalloc_caches[index];
2310}
2311
2312void *__kmalloc(size_t size, gfp_t flags)
2313{
2314 struct kmem_cache *s = get_slab(size, flags);
2315
2316 if (s)
Christoph Lameter77c5e2d2007-05-06 14:49:42 -07002317 return slab_alloc(s, flags, -1, __builtin_return_address(0));
Christoph Lameter272c1d22007-06-08 13:46:49 -07002318 return ZERO_SIZE_PTR;
Christoph Lameter81819f02007-05-06 14:49:36 -07002319}
2320EXPORT_SYMBOL(__kmalloc);
2321
2322#ifdef CONFIG_NUMA
2323void *__kmalloc_node(size_t size, gfp_t flags, int node)
2324{
2325 struct kmem_cache *s = get_slab(size, flags);
2326
2327 if (s)
Christoph Lameter77c5e2d2007-05-06 14:49:42 -07002328 return slab_alloc(s, flags, node, __builtin_return_address(0));
Christoph Lameter272c1d22007-06-08 13:46:49 -07002329 return ZERO_SIZE_PTR;
Christoph Lameter81819f02007-05-06 14:49:36 -07002330}
2331EXPORT_SYMBOL(__kmalloc_node);
2332#endif
2333
2334size_t ksize(const void *object)
2335{
Christoph Lameter272c1d22007-06-08 13:46:49 -07002336 struct page *page;
Christoph Lameter81819f02007-05-06 14:49:36 -07002337 struct kmem_cache *s;
2338
Christoph Lameter272c1d22007-06-08 13:46:49 -07002339 if (object == ZERO_SIZE_PTR)
2340 return 0;
2341
2342 page = get_object_page(object);
Christoph Lameter81819f02007-05-06 14:49:36 -07002343 BUG_ON(!page);
2344 s = page->slab;
2345 BUG_ON(!s);
2346
2347 /*
2348 * Debugging requires use of the padding between object
2349 * and whatever may come after it.
2350 */
2351 if (s->flags & (SLAB_RED_ZONE | SLAB_POISON))
2352 return s->objsize;
2353
2354 /*
2355 * If we have the need to store the freelist pointer
2356 * back there or track user information then we can
2357 * only use the space before that information.
2358 */
2359 if (s->flags & (SLAB_DESTROY_BY_RCU | SLAB_STORE_USER))
2360 return s->inuse;
2361
2362 /*
2363 * Else we can use all the padding etc for the allocation
2364 */
2365 return s->size;
2366}
2367EXPORT_SYMBOL(ksize);
2368
2369void kfree(const void *x)
2370{
2371 struct kmem_cache *s;
2372 struct page *page;
2373
Christoph Lameter272c1d22007-06-08 13:46:49 -07002374 /*
2375 * This has to be an unsigned comparison. According to Linus
2376 * some gcc version treat a pointer as a signed entity. Then
2377 * this comparison would be true for all "negative" pointers
2378 * (which would cover the whole upper half of the address space).
2379 */
2380 if ((unsigned long)x <= (unsigned long)ZERO_SIZE_PTR)
Christoph Lameter81819f02007-05-06 14:49:36 -07002381 return;
2382
Christoph Lameterb49af682007-05-06 14:49:41 -07002383 page = virt_to_head_page(x);
Christoph Lameter81819f02007-05-06 14:49:36 -07002384 s = page->slab;
2385
Christoph Lameter77c5e2d2007-05-06 14:49:42 -07002386 slab_free(s, page, (void *)x, __builtin_return_address(0));
Christoph Lameter81819f02007-05-06 14:49:36 -07002387}
2388EXPORT_SYMBOL(kfree);
2389
Christoph Lameter2086d262007-05-06 14:49:46 -07002390/*
Christoph Lameter672bba32007-05-09 02:32:39 -07002391 * kmem_cache_shrink removes empty slabs from the partial lists and sorts
2392 * the remaining slabs by the number of items in use. The slabs with the
2393 * most items in use come first. New allocations will then fill those up
2394 * and thus they can be removed from the partial lists.
2395 *
2396 * The slabs with the least items are placed last. This results in them
2397 * being allocated from last increasing the chance that the last objects
2398 * are freed in them.
Christoph Lameter2086d262007-05-06 14:49:46 -07002399 */
2400int kmem_cache_shrink(struct kmem_cache *s)
2401{
2402 int node;
2403 int i;
2404 struct kmem_cache_node *n;
2405 struct page *page;
2406 struct page *t;
2407 struct list_head *slabs_by_inuse =
2408 kmalloc(sizeof(struct list_head) * s->objects, GFP_KERNEL);
2409 unsigned long flags;
2410
2411 if (!slabs_by_inuse)
2412 return -ENOMEM;
2413
2414 flush_all(s);
2415 for_each_online_node(node) {
2416 n = get_node(s, node);
2417
2418 if (!n->nr_partial)
2419 continue;
2420
2421 for (i = 0; i < s->objects; i++)
2422 INIT_LIST_HEAD(slabs_by_inuse + i);
2423
2424 spin_lock_irqsave(&n->list_lock, flags);
2425
2426 /*
Christoph Lameter672bba32007-05-09 02:32:39 -07002427 * Build lists indexed by the items in use in each slab.
Christoph Lameter2086d262007-05-06 14:49:46 -07002428 *
Christoph Lameter672bba32007-05-09 02:32:39 -07002429 * Note that concurrent frees may occur while we hold the
2430 * list_lock. page->inuse here is the upper limit.
Christoph Lameter2086d262007-05-06 14:49:46 -07002431 */
2432 list_for_each_entry_safe(page, t, &n->partial, lru) {
2433 if (!page->inuse && slab_trylock(page)) {
2434 /*
2435 * Must hold slab lock here because slab_free
2436 * may have freed the last object and be
2437 * waiting to release the slab.
2438 */
2439 list_del(&page->lru);
2440 n->nr_partial--;
2441 slab_unlock(page);
2442 discard_slab(s, page);
2443 } else {
2444 if (n->nr_partial > MAX_PARTIAL)
2445 list_move(&page->lru,
2446 slabs_by_inuse + page->inuse);
2447 }
2448 }
2449
2450 if (n->nr_partial <= MAX_PARTIAL)
2451 goto out;
2452
2453 /*
Christoph Lameter672bba32007-05-09 02:32:39 -07002454 * Rebuild the partial list with the slabs filled up most
2455 * first and the least used slabs at the end.
Christoph Lameter2086d262007-05-06 14:49:46 -07002456 */
2457 for (i = s->objects - 1; i >= 0; i--)
2458 list_splice(slabs_by_inuse + i, n->partial.prev);
2459
2460 out:
2461 spin_unlock_irqrestore(&n->list_lock, flags);
2462 }
2463
2464 kfree(slabs_by_inuse);
2465 return 0;
2466}
2467EXPORT_SYMBOL(kmem_cache_shrink);
2468
Christoph Lameter81819f02007-05-06 14:49:36 -07002469/**
2470 * krealloc - reallocate memory. The contents will remain unchanged.
Christoph Lameter81819f02007-05-06 14:49:36 -07002471 * @p: object to reallocate memory for.
2472 * @new_size: how many bytes of memory are required.
2473 * @flags: the type of memory to allocate.
2474 *
2475 * The contents of the object pointed to are preserved up to the
2476 * lesser of the new and old sizes. If @p is %NULL, krealloc()
2477 * behaves exactly like kmalloc(). If @size is 0 and @p is not a
2478 * %NULL pointer, the object pointed to is freed.
2479 */
2480void *krealloc(const void *p, size_t new_size, gfp_t flags)
2481{
Christoph Lameter81819f02007-05-06 14:49:36 -07002482 void *ret;
Christoph Lameter1f99a282007-05-09 02:32:38 -07002483 size_t ks;
Christoph Lameter81819f02007-05-06 14:49:36 -07002484
Christoph Lameter272c1d22007-06-08 13:46:49 -07002485 if (unlikely(!p || p == ZERO_SIZE_PTR))
Christoph Lameter81819f02007-05-06 14:49:36 -07002486 return kmalloc(new_size, flags);
2487
2488 if (unlikely(!new_size)) {
2489 kfree(p);
Christoph Lameter272c1d22007-06-08 13:46:49 -07002490 return ZERO_SIZE_PTR;
Christoph Lameter81819f02007-05-06 14:49:36 -07002491 }
2492
Christoph Lameter1f99a282007-05-09 02:32:38 -07002493 ks = ksize(p);
2494 if (ks >= new_size)
Christoph Lameter81819f02007-05-06 14:49:36 -07002495 return (void *)p;
2496
2497 ret = kmalloc(new_size, flags);
2498 if (ret) {
Christoph Lameter1f99a282007-05-09 02:32:38 -07002499 memcpy(ret, p, min(new_size, ks));
Christoph Lameter81819f02007-05-06 14:49:36 -07002500 kfree(p);
2501 }
2502 return ret;
2503}
2504EXPORT_SYMBOL(krealloc);
2505
2506/********************************************************************
2507 * Basic setup of slabs
2508 *******************************************************************/
2509
2510void __init kmem_cache_init(void)
2511{
2512 int i;
Christoph Lameter4b356be2007-06-16 10:16:13 -07002513 int caches = 0;
Christoph Lameter81819f02007-05-06 14:49:36 -07002514
2515#ifdef CONFIG_NUMA
2516 /*
2517 * Must first have the slab cache available for the allocations of the
Christoph Lameter672bba32007-05-09 02:32:39 -07002518 * struct kmem_cache_node's. There is special bootstrap code in
Christoph Lameter81819f02007-05-06 14:49:36 -07002519 * kmem_cache_open for slab_state == DOWN.
2520 */
2521 create_kmalloc_cache(&kmalloc_caches[0], "kmem_cache_node",
2522 sizeof(struct kmem_cache_node), GFP_KERNEL);
Christoph Lameter8ffa6872007-05-31 00:40:51 -07002523 kmalloc_caches[0].refcount = -1;
Christoph Lameter4b356be2007-06-16 10:16:13 -07002524 caches++;
Christoph Lameter81819f02007-05-06 14:49:36 -07002525#endif
2526
2527 /* Able to allocate the per node structures */
2528 slab_state = PARTIAL;
2529
2530 /* Caches that are not of the two-to-the-power-of size */
Christoph Lameter4b356be2007-06-16 10:16:13 -07002531 if (KMALLOC_MIN_SIZE <= 64) {
2532 create_kmalloc_cache(&kmalloc_caches[1],
Christoph Lameter81819f02007-05-06 14:49:36 -07002533 "kmalloc-96", 96, GFP_KERNEL);
Christoph Lameter4b356be2007-06-16 10:16:13 -07002534 caches++;
2535 }
2536 if (KMALLOC_MIN_SIZE <= 128) {
2537 create_kmalloc_cache(&kmalloc_caches[2],
Christoph Lameter81819f02007-05-06 14:49:36 -07002538 "kmalloc-192", 192, GFP_KERNEL);
Christoph Lameter4b356be2007-06-16 10:16:13 -07002539 caches++;
2540 }
Christoph Lameter81819f02007-05-06 14:49:36 -07002541
Christoph Lameter4b356be2007-06-16 10:16:13 -07002542 for (i = KMALLOC_SHIFT_LOW; i <= KMALLOC_SHIFT_HIGH; i++) {
Christoph Lameter81819f02007-05-06 14:49:36 -07002543 create_kmalloc_cache(&kmalloc_caches[i],
2544 "kmalloc", 1 << i, GFP_KERNEL);
Christoph Lameter4b356be2007-06-16 10:16:13 -07002545 caches++;
2546 }
Christoph Lameter81819f02007-05-06 14:49:36 -07002547
2548 slab_state = UP;
2549
2550 /* Provide the correct kmalloc names now that the caches are up */
2551 for (i = KMALLOC_SHIFT_LOW; i <= KMALLOC_SHIFT_HIGH; i++)
2552 kmalloc_caches[i]. name =
2553 kasprintf(GFP_KERNEL, "kmalloc-%d", 1 << i);
2554
2555#ifdef CONFIG_SMP
2556 register_cpu_notifier(&slab_notifier);
2557#endif
2558
Christoph Lameterbcf889f2007-05-10 03:15:44 -07002559 kmem_size = offsetof(struct kmem_cache, cpu_slab) +
2560 nr_cpu_ids * sizeof(struct page *);
Christoph Lameter81819f02007-05-06 14:49:36 -07002561
2562 printk(KERN_INFO "SLUB: Genslabs=%d, HWalign=%d, Order=%d-%d, MinObjects=%d,"
Christoph Lameter4b356be2007-06-16 10:16:13 -07002563 " CPUs=%d, Nodes=%d\n",
2564 caches, cache_line_size(),
Christoph Lameter81819f02007-05-06 14:49:36 -07002565 slub_min_order, slub_max_order, slub_min_objects,
2566 nr_cpu_ids, nr_node_ids);
2567}
2568
2569/*
2570 * Find a mergeable slab cache
2571 */
2572static int slab_unmergeable(struct kmem_cache *s)
2573{
2574 if (slub_nomerge || (s->flags & SLUB_NEVER_MERGE))
2575 return 1;
2576
Christoph Lameterc59def9f2007-05-16 22:10:50 -07002577 if (s->ctor)
Christoph Lameter81819f02007-05-06 14:49:36 -07002578 return 1;
2579
Christoph Lameter8ffa6872007-05-31 00:40:51 -07002580 /*
2581 * We may have set a slab to be unmergeable during bootstrap.
2582 */
2583 if (s->refcount < 0)
2584 return 1;
2585
Christoph Lameter81819f02007-05-06 14:49:36 -07002586 return 0;
2587}
2588
2589static struct kmem_cache *find_mergeable(size_t size,
2590 size_t align, unsigned long flags,
Christoph Lameterc59def9f2007-05-16 22:10:50 -07002591 void (*ctor)(void *, struct kmem_cache *, unsigned long))
Christoph Lameter81819f02007-05-06 14:49:36 -07002592{
Christoph Lameter5b95a4a2007-07-17 04:03:19 -07002593 struct kmem_cache *s;
Christoph Lameter81819f02007-05-06 14:49:36 -07002594
2595 if (slub_nomerge || (flags & SLUB_NEVER_MERGE))
2596 return NULL;
2597
Christoph Lameterc59def9f2007-05-16 22:10:50 -07002598 if (ctor)
Christoph Lameter81819f02007-05-06 14:49:36 -07002599 return NULL;
2600
2601 size = ALIGN(size, sizeof(void *));
2602 align = calculate_alignment(flags, align, size);
2603 size = ALIGN(size, align);
2604
Christoph Lameter5b95a4a2007-07-17 04:03:19 -07002605 list_for_each_entry(s, &slab_caches, list) {
Christoph Lameter81819f02007-05-06 14:49:36 -07002606 if (slab_unmergeable(s))
2607 continue;
2608
2609 if (size > s->size)
2610 continue;
2611
2612 if (((flags | slub_debug) & SLUB_MERGE_SAME) !=
2613 (s->flags & SLUB_MERGE_SAME))
2614 continue;
2615 /*
2616 * Check if alignment is compatible.
2617 * Courtesy of Adrian Drzewiecki
2618 */
2619 if ((s->size & ~(align -1)) != s->size)
2620 continue;
2621
2622 if (s->size - size >= sizeof(void *))
2623 continue;
2624
2625 return s;
2626 }
2627 return NULL;
2628}
2629
2630struct kmem_cache *kmem_cache_create(const char *name, size_t size,
2631 size_t align, unsigned long flags,
2632 void (*ctor)(void *, struct kmem_cache *, unsigned long),
2633 void (*dtor)(void *, struct kmem_cache *, unsigned long))
2634{
2635 struct kmem_cache *s;
2636
Christoph Lameterc59def9f2007-05-16 22:10:50 -07002637 BUG_ON(dtor);
Christoph Lameter81819f02007-05-06 14:49:36 -07002638 down_write(&slub_lock);
Christoph Lameterc59def9f2007-05-16 22:10:50 -07002639 s = find_mergeable(size, align, flags, ctor);
Christoph Lameter81819f02007-05-06 14:49:36 -07002640 if (s) {
2641 s->refcount++;
2642 /*
2643 * Adjust the object sizes so that we clear
2644 * the complete object on kzalloc.
2645 */
2646 s->objsize = max(s->objsize, (int)size);
2647 s->inuse = max_t(int, s->inuse, ALIGN(size, sizeof(void *)));
2648 if (sysfs_slab_alias(s, name))
2649 goto err;
2650 } else {
2651 s = kmalloc(kmem_size, GFP_KERNEL);
2652 if (s && kmem_cache_open(s, GFP_KERNEL, name,
Christoph Lameterc59def9f2007-05-16 22:10:50 -07002653 size, align, flags, ctor)) {
Christoph Lameter81819f02007-05-06 14:49:36 -07002654 if (sysfs_slab_add(s)) {
2655 kfree(s);
2656 goto err;
2657 }
2658 list_add(&s->list, &slab_caches);
2659 } else
2660 kfree(s);
2661 }
2662 up_write(&slub_lock);
2663 return s;
2664
2665err:
2666 up_write(&slub_lock);
2667 if (flags & SLAB_PANIC)
2668 panic("Cannot create slabcache %s\n", name);
2669 else
2670 s = NULL;
2671 return s;
2672}
2673EXPORT_SYMBOL(kmem_cache_create);
2674
2675void *kmem_cache_zalloc(struct kmem_cache *s, gfp_t flags)
2676{
2677 void *x;
2678
Christoph Lameter77c5e2d2007-05-06 14:49:42 -07002679 x = slab_alloc(s, flags, -1, __builtin_return_address(0));
Christoph Lameter81819f02007-05-06 14:49:36 -07002680 if (x)
2681 memset(x, 0, s->objsize);
2682 return x;
2683}
2684EXPORT_SYMBOL(kmem_cache_zalloc);
2685
2686#ifdef CONFIG_SMP
Christoph Lameter27390bc2007-06-01 00:47:09 -07002687/*
Christoph Lameter672bba32007-05-09 02:32:39 -07002688 * Use the cpu notifier to insure that the cpu slabs are flushed when
2689 * necessary.
Christoph Lameter81819f02007-05-06 14:49:36 -07002690 */
2691static int __cpuinit slab_cpuup_callback(struct notifier_block *nfb,
2692 unsigned long action, void *hcpu)
2693{
2694 long cpu = (long)hcpu;
Christoph Lameter5b95a4a2007-07-17 04:03:19 -07002695 struct kmem_cache *s;
2696 unsigned long flags;
Christoph Lameter81819f02007-05-06 14:49:36 -07002697
2698 switch (action) {
2699 case CPU_UP_CANCELED:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07002700 case CPU_UP_CANCELED_FROZEN:
Christoph Lameter81819f02007-05-06 14:49:36 -07002701 case CPU_DEAD:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07002702 case CPU_DEAD_FROZEN:
Christoph Lameter5b95a4a2007-07-17 04:03:19 -07002703 down_read(&slub_lock);
2704 list_for_each_entry(s, &slab_caches, list) {
2705 local_irq_save(flags);
2706 __flush_cpu_slab(s, cpu);
2707 local_irq_restore(flags);
2708 }
2709 up_read(&slub_lock);
Christoph Lameter81819f02007-05-06 14:49:36 -07002710 break;
2711 default:
2712 break;
2713 }
2714 return NOTIFY_OK;
2715}
2716
2717static struct notifier_block __cpuinitdata slab_notifier =
2718 { &slab_cpuup_callback, NULL, 0 };
2719
2720#endif
2721
Christoph Lameter81819f02007-05-06 14:49:36 -07002722void *__kmalloc_track_caller(size_t size, gfp_t gfpflags, void *caller)
2723{
2724 struct kmem_cache *s = get_slab(size, gfpflags);
Christoph Lameter81819f02007-05-06 14:49:36 -07002725
2726 if (!s)
Christoph Lameter272c1d22007-06-08 13:46:49 -07002727 return ZERO_SIZE_PTR;
Christoph Lameter81819f02007-05-06 14:49:36 -07002728
Christoph Lameter77c5e2d2007-05-06 14:49:42 -07002729 return slab_alloc(s, gfpflags, -1, caller);
Christoph Lameter81819f02007-05-06 14:49:36 -07002730}
2731
2732void *__kmalloc_node_track_caller(size_t size, gfp_t gfpflags,
2733 int node, void *caller)
2734{
2735 struct kmem_cache *s = get_slab(size, gfpflags);
Christoph Lameter81819f02007-05-06 14:49:36 -07002736
2737 if (!s)
Christoph Lameter272c1d22007-06-08 13:46:49 -07002738 return ZERO_SIZE_PTR;
Christoph Lameter81819f02007-05-06 14:49:36 -07002739
Christoph Lameter77c5e2d2007-05-06 14:49:42 -07002740 return slab_alloc(s, gfpflags, node, caller);
Christoph Lameter81819f02007-05-06 14:49:36 -07002741}
2742
Christoph Lameter41ecc552007-05-09 02:32:44 -07002743#if defined(CONFIG_SYSFS) && defined(CONFIG_SLUB_DEBUG)
Christoph Lameter53e15af2007-05-06 14:49:43 -07002744static int validate_slab(struct kmem_cache *s, struct page *page)
2745{
2746 void *p;
2747 void *addr = page_address(page);
Christoph Lameter7656c722007-05-09 02:32:40 -07002748 DECLARE_BITMAP(map, s->objects);
Christoph Lameter53e15af2007-05-06 14:49:43 -07002749
2750 if (!check_slab(s, page) ||
2751 !on_freelist(s, page, NULL))
2752 return 0;
2753
2754 /* Now we know that a valid freelist exists */
2755 bitmap_zero(map, s->objects);
2756
Christoph Lameter7656c722007-05-09 02:32:40 -07002757 for_each_free_object(p, s, page->freelist) {
2758 set_bit(slab_index(p, s, addr), map);
Christoph Lameter53e15af2007-05-06 14:49:43 -07002759 if (!check_object(s, page, p, 0))
2760 return 0;
2761 }
2762
Christoph Lameter7656c722007-05-09 02:32:40 -07002763 for_each_object(p, s, addr)
2764 if (!test_bit(slab_index(p, s, addr), map))
Christoph Lameter53e15af2007-05-06 14:49:43 -07002765 if (!check_object(s, page, p, 1))
2766 return 0;
2767 return 1;
2768}
2769
2770static void validate_slab_slab(struct kmem_cache *s, struct page *page)
2771{
2772 if (slab_trylock(page)) {
2773 validate_slab(s, page);
2774 slab_unlock(page);
2775 } else
2776 printk(KERN_INFO "SLUB %s: Skipped busy slab 0x%p\n",
2777 s->name, page);
2778
2779 if (s->flags & DEBUG_DEFAULT_FLAGS) {
Christoph Lameter35e5d7e2007-05-09 02:32:42 -07002780 if (!SlabDebug(page))
2781 printk(KERN_ERR "SLUB %s: SlabDebug not set "
Christoph Lameter53e15af2007-05-06 14:49:43 -07002782 "on slab 0x%p\n", s->name, page);
2783 } else {
Christoph Lameter35e5d7e2007-05-09 02:32:42 -07002784 if (SlabDebug(page))
2785 printk(KERN_ERR "SLUB %s: SlabDebug set on "
Christoph Lameter53e15af2007-05-06 14:49:43 -07002786 "slab 0x%p\n", s->name, page);
2787 }
2788}
2789
2790static int validate_slab_node(struct kmem_cache *s, struct kmem_cache_node *n)
2791{
2792 unsigned long count = 0;
2793 struct page *page;
2794 unsigned long flags;
2795
2796 spin_lock_irqsave(&n->list_lock, flags);
2797
2798 list_for_each_entry(page, &n->partial, lru) {
2799 validate_slab_slab(s, page);
2800 count++;
2801 }
2802 if (count != n->nr_partial)
2803 printk(KERN_ERR "SLUB %s: %ld partial slabs counted but "
2804 "counter=%ld\n", s->name, count, n->nr_partial);
2805
2806 if (!(s->flags & SLAB_STORE_USER))
2807 goto out;
2808
2809 list_for_each_entry(page, &n->full, lru) {
2810 validate_slab_slab(s, page);
2811 count++;
2812 }
2813 if (count != atomic_long_read(&n->nr_slabs))
2814 printk(KERN_ERR "SLUB: %s %ld slabs counted but "
2815 "counter=%ld\n", s->name, count,
2816 atomic_long_read(&n->nr_slabs));
2817
2818out:
2819 spin_unlock_irqrestore(&n->list_lock, flags);
2820 return count;
2821}
2822
2823static unsigned long validate_slab_cache(struct kmem_cache *s)
2824{
2825 int node;
2826 unsigned long count = 0;
2827
2828 flush_all(s);
2829 for_each_online_node(node) {
2830 struct kmem_cache_node *n = get_node(s, node);
2831
2832 count += validate_slab_node(s, n);
2833 }
2834 return count;
2835}
2836
Christoph Lameterb3459702007-05-09 02:32:41 -07002837#ifdef SLUB_RESILIENCY_TEST
2838static void resiliency_test(void)
2839{
2840 u8 *p;
2841
2842 printk(KERN_ERR "SLUB resiliency testing\n");
2843 printk(KERN_ERR "-----------------------\n");
2844 printk(KERN_ERR "A. Corruption after allocation\n");
2845
2846 p = kzalloc(16, GFP_KERNEL);
2847 p[16] = 0x12;
2848 printk(KERN_ERR "\n1. kmalloc-16: Clobber Redzone/next pointer"
2849 " 0x12->0x%p\n\n", p + 16);
2850
2851 validate_slab_cache(kmalloc_caches + 4);
2852
2853 /* Hmmm... The next two are dangerous */
2854 p = kzalloc(32, GFP_KERNEL);
2855 p[32 + sizeof(void *)] = 0x34;
2856 printk(KERN_ERR "\n2. kmalloc-32: Clobber next pointer/next slab"
2857 " 0x34 -> -0x%p\n", p);
2858 printk(KERN_ERR "If allocated object is overwritten then not detectable\n\n");
2859
2860 validate_slab_cache(kmalloc_caches + 5);
2861 p = kzalloc(64, GFP_KERNEL);
2862 p += 64 + (get_cycles() & 0xff) * sizeof(void *);
2863 *p = 0x56;
2864 printk(KERN_ERR "\n3. kmalloc-64: corrupting random byte 0x56->0x%p\n",
2865 p);
2866 printk(KERN_ERR "If allocated object is overwritten then not detectable\n\n");
2867 validate_slab_cache(kmalloc_caches + 6);
2868
2869 printk(KERN_ERR "\nB. Corruption after free\n");
2870 p = kzalloc(128, GFP_KERNEL);
2871 kfree(p);
2872 *p = 0x78;
2873 printk(KERN_ERR "1. kmalloc-128: Clobber first word 0x78->0x%p\n\n", p);
2874 validate_slab_cache(kmalloc_caches + 7);
2875
2876 p = kzalloc(256, GFP_KERNEL);
2877 kfree(p);
2878 p[50] = 0x9a;
2879 printk(KERN_ERR "\n2. kmalloc-256: Clobber 50th byte 0x9a->0x%p\n\n", p);
2880 validate_slab_cache(kmalloc_caches + 8);
2881
2882 p = kzalloc(512, GFP_KERNEL);
2883 kfree(p);
2884 p[512] = 0xab;
2885 printk(KERN_ERR "\n3. kmalloc-512: Clobber redzone 0xab->0x%p\n\n", p);
2886 validate_slab_cache(kmalloc_caches + 9);
2887}
2888#else
2889static void resiliency_test(void) {};
2890#endif
2891
Christoph Lameter88a420e2007-05-06 14:49:45 -07002892/*
Christoph Lameter672bba32007-05-09 02:32:39 -07002893 * Generate lists of code addresses where slabcache objects are allocated
Christoph Lameter88a420e2007-05-06 14:49:45 -07002894 * and freed.
2895 */
2896
2897struct location {
2898 unsigned long count;
2899 void *addr;
Christoph Lameter45edfa52007-05-09 02:32:45 -07002900 long long sum_time;
2901 long min_time;
2902 long max_time;
2903 long min_pid;
2904 long max_pid;
2905 cpumask_t cpus;
2906 nodemask_t nodes;
Christoph Lameter88a420e2007-05-06 14:49:45 -07002907};
2908
2909struct loc_track {
2910 unsigned long max;
2911 unsigned long count;
2912 struct location *loc;
2913};
2914
2915static void free_loc_track(struct loc_track *t)
2916{
2917 if (t->max)
2918 free_pages((unsigned long)t->loc,
2919 get_order(sizeof(struct location) * t->max));
2920}
2921
Christoph Lameter68dff6a2007-07-17 04:03:20 -07002922static int alloc_loc_track(struct loc_track *t, unsigned long max, gfp_t flags)
Christoph Lameter88a420e2007-05-06 14:49:45 -07002923{
2924 struct location *l;
2925 int order;
2926
Christoph Lameter88a420e2007-05-06 14:49:45 -07002927 order = get_order(sizeof(struct location) * max);
2928
Christoph Lameter68dff6a2007-07-17 04:03:20 -07002929 l = (void *)__get_free_pages(flags, order);
Christoph Lameter88a420e2007-05-06 14:49:45 -07002930 if (!l)
2931 return 0;
2932
2933 if (t->count) {
2934 memcpy(l, t->loc, sizeof(struct location) * t->count);
2935 free_loc_track(t);
2936 }
2937 t->max = max;
2938 t->loc = l;
2939 return 1;
2940}
2941
2942static int add_location(struct loc_track *t, struct kmem_cache *s,
Christoph Lameter45edfa52007-05-09 02:32:45 -07002943 const struct track *track)
Christoph Lameter88a420e2007-05-06 14:49:45 -07002944{
2945 long start, end, pos;
2946 struct location *l;
2947 void *caddr;
Christoph Lameter45edfa52007-05-09 02:32:45 -07002948 unsigned long age = jiffies - track->when;
Christoph Lameter88a420e2007-05-06 14:49:45 -07002949
2950 start = -1;
2951 end = t->count;
2952
2953 for ( ; ; ) {
2954 pos = start + (end - start + 1) / 2;
2955
2956 /*
2957 * There is nothing at "end". If we end up there
2958 * we need to add something to before end.
2959 */
2960 if (pos == end)
2961 break;
2962
2963 caddr = t->loc[pos].addr;
Christoph Lameter45edfa52007-05-09 02:32:45 -07002964 if (track->addr == caddr) {
2965
2966 l = &t->loc[pos];
2967 l->count++;
2968 if (track->when) {
2969 l->sum_time += age;
2970 if (age < l->min_time)
2971 l->min_time = age;
2972 if (age > l->max_time)
2973 l->max_time = age;
2974
2975 if (track->pid < l->min_pid)
2976 l->min_pid = track->pid;
2977 if (track->pid > l->max_pid)
2978 l->max_pid = track->pid;
2979
2980 cpu_set(track->cpu, l->cpus);
2981 }
2982 node_set(page_to_nid(virt_to_page(track)), l->nodes);
Christoph Lameter88a420e2007-05-06 14:49:45 -07002983 return 1;
2984 }
2985
Christoph Lameter45edfa52007-05-09 02:32:45 -07002986 if (track->addr < caddr)
Christoph Lameter88a420e2007-05-06 14:49:45 -07002987 end = pos;
2988 else
2989 start = pos;
2990 }
2991
2992 /*
Christoph Lameter672bba32007-05-09 02:32:39 -07002993 * Not found. Insert new tracking element.
Christoph Lameter88a420e2007-05-06 14:49:45 -07002994 */
Christoph Lameter68dff6a2007-07-17 04:03:20 -07002995 if (t->count >= t->max && !alloc_loc_track(t, 2 * t->max, GFP_ATOMIC))
Christoph Lameter88a420e2007-05-06 14:49:45 -07002996 return 0;
2997
2998 l = t->loc + pos;
2999 if (pos < t->count)
3000 memmove(l + 1, l,
3001 (t->count - pos) * sizeof(struct location));
3002 t->count++;
3003 l->count = 1;
Christoph Lameter45edfa52007-05-09 02:32:45 -07003004 l->addr = track->addr;
3005 l->sum_time = age;
3006 l->min_time = age;
3007 l->max_time = age;
3008 l->min_pid = track->pid;
3009 l->max_pid = track->pid;
3010 cpus_clear(l->cpus);
3011 cpu_set(track->cpu, l->cpus);
3012 nodes_clear(l->nodes);
3013 node_set(page_to_nid(virt_to_page(track)), l->nodes);
Christoph Lameter88a420e2007-05-06 14:49:45 -07003014 return 1;
3015}
3016
3017static void process_slab(struct loc_track *t, struct kmem_cache *s,
3018 struct page *page, enum track_item alloc)
3019{
3020 void *addr = page_address(page);
Christoph Lameter7656c722007-05-09 02:32:40 -07003021 DECLARE_BITMAP(map, s->objects);
Christoph Lameter88a420e2007-05-06 14:49:45 -07003022 void *p;
3023
3024 bitmap_zero(map, s->objects);
Christoph Lameter7656c722007-05-09 02:32:40 -07003025 for_each_free_object(p, s, page->freelist)
3026 set_bit(slab_index(p, s, addr), map);
Christoph Lameter88a420e2007-05-06 14:49:45 -07003027
Christoph Lameter7656c722007-05-09 02:32:40 -07003028 for_each_object(p, s, addr)
Christoph Lameter45edfa52007-05-09 02:32:45 -07003029 if (!test_bit(slab_index(p, s, addr), map))
3030 add_location(t, s, get_track(s, p, alloc));
Christoph Lameter88a420e2007-05-06 14:49:45 -07003031}
3032
3033static int list_locations(struct kmem_cache *s, char *buf,
3034 enum track_item alloc)
3035{
3036 int n = 0;
3037 unsigned long i;
Christoph Lameter68dff6a2007-07-17 04:03:20 -07003038 struct loc_track t = { 0, 0, NULL };
Christoph Lameter88a420e2007-05-06 14:49:45 -07003039 int node;
3040
Christoph Lameter68dff6a2007-07-17 04:03:20 -07003041 if (!alloc_loc_track(&t, PAGE_SIZE / sizeof(struct location),
3042 GFP_KERNEL))
3043 return sprintf(buf, "Out of memory\n");
Christoph Lameter88a420e2007-05-06 14:49:45 -07003044
3045 /* Push back cpu slabs */
3046 flush_all(s);
3047
3048 for_each_online_node(node) {
3049 struct kmem_cache_node *n = get_node(s, node);
3050 unsigned long flags;
3051 struct page *page;
3052
3053 if (!atomic_read(&n->nr_slabs))
3054 continue;
3055
3056 spin_lock_irqsave(&n->list_lock, flags);
3057 list_for_each_entry(page, &n->partial, lru)
3058 process_slab(&t, s, page, alloc);
3059 list_for_each_entry(page, &n->full, lru)
3060 process_slab(&t, s, page, alloc);
3061 spin_unlock_irqrestore(&n->list_lock, flags);
3062 }
3063
3064 for (i = 0; i < t.count; i++) {
Christoph Lameter45edfa52007-05-09 02:32:45 -07003065 struct location *l = &t.loc[i];
Christoph Lameter88a420e2007-05-06 14:49:45 -07003066
3067 if (n > PAGE_SIZE - 100)
3068 break;
Christoph Lameter45edfa52007-05-09 02:32:45 -07003069 n += sprintf(buf + n, "%7ld ", l->count);
3070
3071 if (l->addr)
3072 n += sprint_symbol(buf + n, (unsigned long)l->addr);
Christoph Lameter88a420e2007-05-06 14:49:45 -07003073 else
3074 n += sprintf(buf + n, "<not-available>");
Christoph Lameter45edfa52007-05-09 02:32:45 -07003075
3076 if (l->sum_time != l->min_time) {
3077 unsigned long remainder;
3078
3079 n += sprintf(buf + n, " age=%ld/%ld/%ld",
3080 l->min_time,
3081 div_long_long_rem(l->sum_time, l->count, &remainder),
3082 l->max_time);
3083 } else
3084 n += sprintf(buf + n, " age=%ld",
3085 l->min_time);
3086
3087 if (l->min_pid != l->max_pid)
3088 n += sprintf(buf + n, " pid=%ld-%ld",
3089 l->min_pid, l->max_pid);
3090 else
3091 n += sprintf(buf + n, " pid=%ld",
3092 l->min_pid);
3093
Christoph Lameter84966342007-06-23 17:16:32 -07003094 if (num_online_cpus() > 1 && !cpus_empty(l->cpus) &&
3095 n < PAGE_SIZE - 60) {
Christoph Lameter45edfa52007-05-09 02:32:45 -07003096 n += sprintf(buf + n, " cpus=");
3097 n += cpulist_scnprintf(buf + n, PAGE_SIZE - n - 50,
3098 l->cpus);
3099 }
3100
Christoph Lameter84966342007-06-23 17:16:32 -07003101 if (num_online_nodes() > 1 && !nodes_empty(l->nodes) &&
3102 n < PAGE_SIZE - 60) {
Christoph Lameter45edfa52007-05-09 02:32:45 -07003103 n += sprintf(buf + n, " nodes=");
3104 n += nodelist_scnprintf(buf + n, PAGE_SIZE - n - 50,
3105 l->nodes);
3106 }
3107
Christoph Lameter88a420e2007-05-06 14:49:45 -07003108 n += sprintf(buf + n, "\n");
3109 }
3110
3111 free_loc_track(&t);
3112 if (!t.count)
3113 n += sprintf(buf, "No data\n");
3114 return n;
3115}
3116
Christoph Lameter81819f02007-05-06 14:49:36 -07003117static unsigned long count_partial(struct kmem_cache_node *n)
3118{
3119 unsigned long flags;
3120 unsigned long x = 0;
3121 struct page *page;
3122
3123 spin_lock_irqsave(&n->list_lock, flags);
3124 list_for_each_entry(page, &n->partial, lru)
3125 x += page->inuse;
3126 spin_unlock_irqrestore(&n->list_lock, flags);
3127 return x;
3128}
3129
3130enum slab_stat_type {
3131 SL_FULL,
3132 SL_PARTIAL,
3133 SL_CPU,
3134 SL_OBJECTS
3135};
3136
3137#define SO_FULL (1 << SL_FULL)
3138#define SO_PARTIAL (1 << SL_PARTIAL)
3139#define SO_CPU (1 << SL_CPU)
3140#define SO_OBJECTS (1 << SL_OBJECTS)
3141
3142static unsigned long slab_objects(struct kmem_cache *s,
3143 char *buf, unsigned long flags)
3144{
3145 unsigned long total = 0;
3146 int cpu;
3147 int node;
3148 int x;
3149 unsigned long *nodes;
3150 unsigned long *per_cpu;
3151
3152 nodes = kzalloc(2 * sizeof(unsigned long) * nr_node_ids, GFP_KERNEL);
3153 per_cpu = nodes + nr_node_ids;
3154
3155 for_each_possible_cpu(cpu) {
3156 struct page *page = s->cpu_slab[cpu];
3157 int node;
3158
3159 if (page) {
3160 node = page_to_nid(page);
3161 if (flags & SO_CPU) {
3162 int x = 0;
3163
3164 if (flags & SO_OBJECTS)
3165 x = page->inuse;
3166 else
3167 x = 1;
3168 total += x;
3169 nodes[node] += x;
3170 }
3171 per_cpu[node]++;
3172 }
3173 }
3174
3175 for_each_online_node(node) {
3176 struct kmem_cache_node *n = get_node(s, node);
3177
3178 if (flags & SO_PARTIAL) {
3179 if (flags & SO_OBJECTS)
3180 x = count_partial(n);
3181 else
3182 x = n->nr_partial;
3183 total += x;
3184 nodes[node] += x;
3185 }
3186
3187 if (flags & SO_FULL) {
3188 int full_slabs = atomic_read(&n->nr_slabs)
3189 - per_cpu[node]
3190 - n->nr_partial;
3191
3192 if (flags & SO_OBJECTS)
3193 x = full_slabs * s->objects;
3194 else
3195 x = full_slabs;
3196 total += x;
3197 nodes[node] += x;
3198 }
3199 }
3200
3201 x = sprintf(buf, "%lu", total);
3202#ifdef CONFIG_NUMA
3203 for_each_online_node(node)
3204 if (nodes[node])
3205 x += sprintf(buf + x, " N%d=%lu",
3206 node, nodes[node]);
3207#endif
3208 kfree(nodes);
3209 return x + sprintf(buf + x, "\n");
3210}
3211
3212static int any_slab_objects(struct kmem_cache *s)
3213{
3214 int node;
3215 int cpu;
3216
3217 for_each_possible_cpu(cpu)
3218 if (s->cpu_slab[cpu])
3219 return 1;
3220
3221 for_each_node(node) {
3222 struct kmem_cache_node *n = get_node(s, node);
3223
3224 if (n->nr_partial || atomic_read(&n->nr_slabs))
3225 return 1;
3226 }
3227 return 0;
3228}
3229
3230#define to_slab_attr(n) container_of(n, struct slab_attribute, attr)
3231#define to_slab(n) container_of(n, struct kmem_cache, kobj);
3232
3233struct slab_attribute {
3234 struct attribute attr;
3235 ssize_t (*show)(struct kmem_cache *s, char *buf);
3236 ssize_t (*store)(struct kmem_cache *s, const char *x, size_t count);
3237};
3238
3239#define SLAB_ATTR_RO(_name) \
3240 static struct slab_attribute _name##_attr = __ATTR_RO(_name)
3241
3242#define SLAB_ATTR(_name) \
3243 static struct slab_attribute _name##_attr = \
3244 __ATTR(_name, 0644, _name##_show, _name##_store)
3245
Christoph Lameter81819f02007-05-06 14:49:36 -07003246static ssize_t slab_size_show(struct kmem_cache *s, char *buf)
3247{
3248 return sprintf(buf, "%d\n", s->size);
3249}
3250SLAB_ATTR_RO(slab_size);
3251
3252static ssize_t align_show(struct kmem_cache *s, char *buf)
3253{
3254 return sprintf(buf, "%d\n", s->align);
3255}
3256SLAB_ATTR_RO(align);
3257
3258static ssize_t object_size_show(struct kmem_cache *s, char *buf)
3259{
3260 return sprintf(buf, "%d\n", s->objsize);
3261}
3262SLAB_ATTR_RO(object_size);
3263
3264static ssize_t objs_per_slab_show(struct kmem_cache *s, char *buf)
3265{
3266 return sprintf(buf, "%d\n", s->objects);
3267}
3268SLAB_ATTR_RO(objs_per_slab);
3269
3270static ssize_t order_show(struct kmem_cache *s, char *buf)
3271{
3272 return sprintf(buf, "%d\n", s->order);
3273}
3274SLAB_ATTR_RO(order);
3275
3276static ssize_t ctor_show(struct kmem_cache *s, char *buf)
3277{
3278 if (s->ctor) {
3279 int n = sprint_symbol(buf, (unsigned long)s->ctor);
3280
3281 return n + sprintf(buf + n, "\n");
3282 }
3283 return 0;
3284}
3285SLAB_ATTR_RO(ctor);
3286
Christoph Lameter81819f02007-05-06 14:49:36 -07003287static ssize_t aliases_show(struct kmem_cache *s, char *buf)
3288{
3289 return sprintf(buf, "%d\n", s->refcount - 1);
3290}
3291SLAB_ATTR_RO(aliases);
3292
3293static ssize_t slabs_show(struct kmem_cache *s, char *buf)
3294{
3295 return slab_objects(s, buf, SO_FULL|SO_PARTIAL|SO_CPU);
3296}
3297SLAB_ATTR_RO(slabs);
3298
3299static ssize_t partial_show(struct kmem_cache *s, char *buf)
3300{
3301 return slab_objects(s, buf, SO_PARTIAL);
3302}
3303SLAB_ATTR_RO(partial);
3304
3305static ssize_t cpu_slabs_show(struct kmem_cache *s, char *buf)
3306{
3307 return slab_objects(s, buf, SO_CPU);
3308}
3309SLAB_ATTR_RO(cpu_slabs);
3310
3311static ssize_t objects_show(struct kmem_cache *s, char *buf)
3312{
3313 return slab_objects(s, buf, SO_FULL|SO_PARTIAL|SO_CPU|SO_OBJECTS);
3314}
3315SLAB_ATTR_RO(objects);
3316
3317static ssize_t sanity_checks_show(struct kmem_cache *s, char *buf)
3318{
3319 return sprintf(buf, "%d\n", !!(s->flags & SLAB_DEBUG_FREE));
3320}
3321
3322static ssize_t sanity_checks_store(struct kmem_cache *s,
3323 const char *buf, size_t length)
3324{
3325 s->flags &= ~SLAB_DEBUG_FREE;
3326 if (buf[0] == '1')
3327 s->flags |= SLAB_DEBUG_FREE;
3328 return length;
3329}
3330SLAB_ATTR(sanity_checks);
3331
3332static ssize_t trace_show(struct kmem_cache *s, char *buf)
3333{
3334 return sprintf(buf, "%d\n", !!(s->flags & SLAB_TRACE));
3335}
3336
3337static ssize_t trace_store(struct kmem_cache *s, const char *buf,
3338 size_t length)
3339{
3340 s->flags &= ~SLAB_TRACE;
3341 if (buf[0] == '1')
3342 s->flags |= SLAB_TRACE;
3343 return length;
3344}
3345SLAB_ATTR(trace);
3346
3347static ssize_t reclaim_account_show(struct kmem_cache *s, char *buf)
3348{
3349 return sprintf(buf, "%d\n", !!(s->flags & SLAB_RECLAIM_ACCOUNT));
3350}
3351
3352static ssize_t reclaim_account_store(struct kmem_cache *s,
3353 const char *buf, size_t length)
3354{
3355 s->flags &= ~SLAB_RECLAIM_ACCOUNT;
3356 if (buf[0] == '1')
3357 s->flags |= SLAB_RECLAIM_ACCOUNT;
3358 return length;
3359}
3360SLAB_ATTR(reclaim_account);
3361
3362static ssize_t hwcache_align_show(struct kmem_cache *s, char *buf)
3363{
Christoph Lameter5af60832007-05-06 14:49:56 -07003364 return sprintf(buf, "%d\n", !!(s->flags & SLAB_HWCACHE_ALIGN));
Christoph Lameter81819f02007-05-06 14:49:36 -07003365}
3366SLAB_ATTR_RO(hwcache_align);
3367
3368#ifdef CONFIG_ZONE_DMA
3369static ssize_t cache_dma_show(struct kmem_cache *s, char *buf)
3370{
3371 return sprintf(buf, "%d\n", !!(s->flags & SLAB_CACHE_DMA));
3372}
3373SLAB_ATTR_RO(cache_dma);
3374#endif
3375
3376static ssize_t destroy_by_rcu_show(struct kmem_cache *s, char *buf)
3377{
3378 return sprintf(buf, "%d\n", !!(s->flags & SLAB_DESTROY_BY_RCU));
3379}
3380SLAB_ATTR_RO(destroy_by_rcu);
3381
3382static ssize_t red_zone_show(struct kmem_cache *s, char *buf)
3383{
3384 return sprintf(buf, "%d\n", !!(s->flags & SLAB_RED_ZONE));
3385}
3386
3387static ssize_t red_zone_store(struct kmem_cache *s,
3388 const char *buf, size_t length)
3389{
3390 if (any_slab_objects(s))
3391 return -EBUSY;
3392
3393 s->flags &= ~SLAB_RED_ZONE;
3394 if (buf[0] == '1')
3395 s->flags |= SLAB_RED_ZONE;
3396 calculate_sizes(s);
3397 return length;
3398}
3399SLAB_ATTR(red_zone);
3400
3401static ssize_t poison_show(struct kmem_cache *s, char *buf)
3402{
3403 return sprintf(buf, "%d\n", !!(s->flags & SLAB_POISON));
3404}
3405
3406static ssize_t poison_store(struct kmem_cache *s,
3407 const char *buf, size_t length)
3408{
3409 if (any_slab_objects(s))
3410 return -EBUSY;
3411
3412 s->flags &= ~SLAB_POISON;
3413 if (buf[0] == '1')
3414 s->flags |= SLAB_POISON;
3415 calculate_sizes(s);
3416 return length;
3417}
3418SLAB_ATTR(poison);
3419
3420static ssize_t store_user_show(struct kmem_cache *s, char *buf)
3421{
3422 return sprintf(buf, "%d\n", !!(s->flags & SLAB_STORE_USER));
3423}
3424
3425static ssize_t store_user_store(struct kmem_cache *s,
3426 const char *buf, size_t length)
3427{
3428 if (any_slab_objects(s))
3429 return -EBUSY;
3430
3431 s->flags &= ~SLAB_STORE_USER;
3432 if (buf[0] == '1')
3433 s->flags |= SLAB_STORE_USER;
3434 calculate_sizes(s);
3435 return length;
3436}
3437SLAB_ATTR(store_user);
3438
Christoph Lameter53e15af2007-05-06 14:49:43 -07003439static ssize_t validate_show(struct kmem_cache *s, char *buf)
3440{
3441 return 0;
3442}
3443
3444static ssize_t validate_store(struct kmem_cache *s,
3445 const char *buf, size_t length)
3446{
3447 if (buf[0] == '1')
3448 validate_slab_cache(s);
3449 else
3450 return -EINVAL;
3451 return length;
3452}
3453SLAB_ATTR(validate);
3454
Christoph Lameter2086d262007-05-06 14:49:46 -07003455static ssize_t shrink_show(struct kmem_cache *s, char *buf)
3456{
3457 return 0;
3458}
3459
3460static ssize_t shrink_store(struct kmem_cache *s,
3461 const char *buf, size_t length)
3462{
3463 if (buf[0] == '1') {
3464 int rc = kmem_cache_shrink(s);
3465
3466 if (rc)
3467 return rc;
3468 } else
3469 return -EINVAL;
3470 return length;
3471}
3472SLAB_ATTR(shrink);
3473
Christoph Lameter88a420e2007-05-06 14:49:45 -07003474static ssize_t alloc_calls_show(struct kmem_cache *s, char *buf)
3475{
3476 if (!(s->flags & SLAB_STORE_USER))
3477 return -ENOSYS;
3478 return list_locations(s, buf, TRACK_ALLOC);
3479}
3480SLAB_ATTR_RO(alloc_calls);
3481
3482static ssize_t free_calls_show(struct kmem_cache *s, char *buf)
3483{
3484 if (!(s->flags & SLAB_STORE_USER))
3485 return -ENOSYS;
3486 return list_locations(s, buf, TRACK_FREE);
3487}
3488SLAB_ATTR_RO(free_calls);
3489
Christoph Lameter81819f02007-05-06 14:49:36 -07003490#ifdef CONFIG_NUMA
3491static ssize_t defrag_ratio_show(struct kmem_cache *s, char *buf)
3492{
3493 return sprintf(buf, "%d\n", s->defrag_ratio / 10);
3494}
3495
3496static ssize_t defrag_ratio_store(struct kmem_cache *s,
3497 const char *buf, size_t length)
3498{
3499 int n = simple_strtoul(buf, NULL, 10);
3500
3501 if (n < 100)
3502 s->defrag_ratio = n * 10;
3503 return length;
3504}
3505SLAB_ATTR(defrag_ratio);
3506#endif
3507
3508static struct attribute * slab_attrs[] = {
3509 &slab_size_attr.attr,
3510 &object_size_attr.attr,
3511 &objs_per_slab_attr.attr,
3512 &order_attr.attr,
3513 &objects_attr.attr,
3514 &slabs_attr.attr,
3515 &partial_attr.attr,
3516 &cpu_slabs_attr.attr,
3517 &ctor_attr.attr,
Christoph Lameter81819f02007-05-06 14:49:36 -07003518 &aliases_attr.attr,
3519 &align_attr.attr,
3520 &sanity_checks_attr.attr,
3521 &trace_attr.attr,
3522 &hwcache_align_attr.attr,
3523 &reclaim_account_attr.attr,
3524 &destroy_by_rcu_attr.attr,
3525 &red_zone_attr.attr,
3526 &poison_attr.attr,
3527 &store_user_attr.attr,
Christoph Lameter53e15af2007-05-06 14:49:43 -07003528 &validate_attr.attr,
Christoph Lameter2086d262007-05-06 14:49:46 -07003529 &shrink_attr.attr,
Christoph Lameter88a420e2007-05-06 14:49:45 -07003530 &alloc_calls_attr.attr,
3531 &free_calls_attr.attr,
Christoph Lameter81819f02007-05-06 14:49:36 -07003532#ifdef CONFIG_ZONE_DMA
3533 &cache_dma_attr.attr,
3534#endif
3535#ifdef CONFIG_NUMA
3536 &defrag_ratio_attr.attr,
3537#endif
3538 NULL
3539};
3540
3541static struct attribute_group slab_attr_group = {
3542 .attrs = slab_attrs,
3543};
3544
3545static ssize_t slab_attr_show(struct kobject *kobj,
3546 struct attribute *attr,
3547 char *buf)
3548{
3549 struct slab_attribute *attribute;
3550 struct kmem_cache *s;
3551 int err;
3552
3553 attribute = to_slab_attr(attr);
3554 s = to_slab(kobj);
3555
3556 if (!attribute->show)
3557 return -EIO;
3558
3559 err = attribute->show(s, buf);
3560
3561 return err;
3562}
3563
3564static ssize_t slab_attr_store(struct kobject *kobj,
3565 struct attribute *attr,
3566 const char *buf, size_t len)
3567{
3568 struct slab_attribute *attribute;
3569 struct kmem_cache *s;
3570 int err;
3571
3572 attribute = to_slab_attr(attr);
3573 s = to_slab(kobj);
3574
3575 if (!attribute->store)
3576 return -EIO;
3577
3578 err = attribute->store(s, buf, len);
3579
3580 return err;
3581}
3582
3583static struct sysfs_ops slab_sysfs_ops = {
3584 .show = slab_attr_show,
3585 .store = slab_attr_store,
3586};
3587
3588static struct kobj_type slab_ktype = {
3589 .sysfs_ops = &slab_sysfs_ops,
3590};
3591
3592static int uevent_filter(struct kset *kset, struct kobject *kobj)
3593{
3594 struct kobj_type *ktype = get_ktype(kobj);
3595
3596 if (ktype == &slab_ktype)
3597 return 1;
3598 return 0;
3599}
3600
3601static struct kset_uevent_ops slab_uevent_ops = {
3602 .filter = uevent_filter,
3603};
3604
3605decl_subsys(slab, &slab_ktype, &slab_uevent_ops);
3606
3607#define ID_STR_LENGTH 64
3608
3609/* Create a unique string id for a slab cache:
3610 * format
3611 * :[flags-]size:[memory address of kmemcache]
3612 */
3613static char *create_unique_id(struct kmem_cache *s)
3614{
3615 char *name = kmalloc(ID_STR_LENGTH, GFP_KERNEL);
3616 char *p = name;
3617
3618 BUG_ON(!name);
3619
3620 *p++ = ':';
3621 /*
3622 * First flags affecting slabcache operations. We will only
3623 * get here for aliasable slabs so we do not need to support
3624 * too many flags. The flags here must cover all flags that
3625 * are matched during merging to guarantee that the id is
3626 * unique.
3627 */
3628 if (s->flags & SLAB_CACHE_DMA)
3629 *p++ = 'd';
3630 if (s->flags & SLAB_RECLAIM_ACCOUNT)
3631 *p++ = 'a';
3632 if (s->flags & SLAB_DEBUG_FREE)
3633 *p++ = 'F';
3634 if (p != name + 1)
3635 *p++ = '-';
3636 p += sprintf(p, "%07d", s->size);
3637 BUG_ON(p > name + ID_STR_LENGTH - 1);
3638 return name;
3639}
3640
3641static int sysfs_slab_add(struct kmem_cache *s)
3642{
3643 int err;
3644 const char *name;
3645 int unmergeable;
3646
3647 if (slab_state < SYSFS)
3648 /* Defer until later */
3649 return 0;
3650
3651 unmergeable = slab_unmergeable(s);
3652 if (unmergeable) {
3653 /*
3654 * Slabcache can never be merged so we can use the name proper.
3655 * This is typically the case for debug situations. In that
3656 * case we can catch duplicate names easily.
3657 */
Linus Torvalds0f9008e2007-05-07 12:31:58 -07003658 sysfs_remove_link(&slab_subsys.kobj, s->name);
Christoph Lameter81819f02007-05-06 14:49:36 -07003659 name = s->name;
3660 } else {
3661 /*
3662 * Create a unique name for the slab as a target
3663 * for the symlinks.
3664 */
3665 name = create_unique_id(s);
3666 }
3667
3668 kobj_set_kset_s(s, slab_subsys);
3669 kobject_set_name(&s->kobj, name);
3670 kobject_init(&s->kobj);
3671 err = kobject_add(&s->kobj);
3672 if (err)
3673 return err;
3674
3675 err = sysfs_create_group(&s->kobj, &slab_attr_group);
3676 if (err)
3677 return err;
3678 kobject_uevent(&s->kobj, KOBJ_ADD);
3679 if (!unmergeable) {
3680 /* Setup first alias */
3681 sysfs_slab_alias(s, s->name);
3682 kfree(name);
3683 }
3684 return 0;
3685}
3686
3687static void sysfs_slab_remove(struct kmem_cache *s)
3688{
3689 kobject_uevent(&s->kobj, KOBJ_REMOVE);
3690 kobject_del(&s->kobj);
3691}
3692
3693/*
3694 * Need to buffer aliases during bootup until sysfs becomes
3695 * available lest we loose that information.
3696 */
3697struct saved_alias {
3698 struct kmem_cache *s;
3699 const char *name;
3700 struct saved_alias *next;
3701};
3702
3703struct saved_alias *alias_list;
3704
3705static int sysfs_slab_alias(struct kmem_cache *s, const char *name)
3706{
3707 struct saved_alias *al;
3708
3709 if (slab_state == SYSFS) {
3710 /*
3711 * If we have a leftover link then remove it.
3712 */
Linus Torvalds0f9008e2007-05-07 12:31:58 -07003713 sysfs_remove_link(&slab_subsys.kobj, name);
3714 return sysfs_create_link(&slab_subsys.kobj,
Christoph Lameter81819f02007-05-06 14:49:36 -07003715 &s->kobj, name);
3716 }
3717
3718 al = kmalloc(sizeof(struct saved_alias), GFP_KERNEL);
3719 if (!al)
3720 return -ENOMEM;
3721
3722 al->s = s;
3723 al->name = name;
3724 al->next = alias_list;
3725 alias_list = al;
3726 return 0;
3727}
3728
3729static int __init slab_sysfs_init(void)
3730{
Christoph Lameter5b95a4a2007-07-17 04:03:19 -07003731 struct kmem_cache *s;
Christoph Lameter81819f02007-05-06 14:49:36 -07003732 int err;
3733
3734 err = subsystem_register(&slab_subsys);
3735 if (err) {
3736 printk(KERN_ERR "Cannot register slab subsystem.\n");
3737 return -ENOSYS;
3738 }
3739
Christoph Lameter26a7bd02007-05-09 02:32:39 -07003740 slab_state = SYSFS;
3741
Christoph Lameter5b95a4a2007-07-17 04:03:19 -07003742 list_for_each_entry(s, &slab_caches, list) {
Christoph Lameter26a7bd02007-05-09 02:32:39 -07003743 err = sysfs_slab_add(s);
3744 BUG_ON(err);
3745 }
Christoph Lameter81819f02007-05-06 14:49:36 -07003746
3747 while (alias_list) {
3748 struct saved_alias *al = alias_list;
3749
3750 alias_list = alias_list->next;
3751 err = sysfs_slab_alias(al->s, al->name);
3752 BUG_ON(err);
3753 kfree(al);
3754 }
3755
3756 resiliency_test();
3757 return 0;
3758}
3759
3760__initcall(slab_sysfs_init);
Christoph Lameter81819f02007-05-06 14:49:36 -07003761#endif