blob: 38914bc64aca2cd58a4c40e35b1b8ef45b9e55d9 [file] [log] [blame]
Christoph Lameter81819f02007-05-06 14:49:36 -07001/*
2 * SLUB: A slab allocator that limits cache line use instead of queuing
3 * objects in per cpu and per node lists.
4 *
5 * The allocator synchronizes using per slab locks and only
6 * uses a centralized lock to manage a pool of partial slabs.
7 *
8 * (C) 2007 SGI, Christoph Lameter <clameter@sgi.com>
9 */
10
11#include <linux/mm.h>
12#include <linux/module.h>
13#include <linux/bit_spinlock.h>
14#include <linux/interrupt.h>
15#include <linux/bitops.h>
16#include <linux/slab.h>
17#include <linux/seq_file.h>
18#include <linux/cpu.h>
19#include <linux/cpuset.h>
20#include <linux/mempolicy.h>
21#include <linux/ctype.h>
22#include <linux/kallsyms.h>
Yasunori Gotob9049e22007-10-21 16:41:37 -070023#include <linux/memory.h>
Christoph Lameter81819f02007-05-06 14:49:36 -070024
25/*
26 * Lock order:
27 * 1. slab_lock(page)
28 * 2. slab->list_lock
29 *
30 * The slab_lock protects operations on the object of a particular
31 * slab and its metadata in the page struct. If the slab lock
32 * has been taken then no allocations nor frees can be performed
33 * on the objects in the slab nor can the slab be added or removed
34 * from the partial or full lists since this would mean modifying
35 * the page_struct of the slab.
36 *
37 * The list_lock protects the partial and full list on each node and
38 * the partial slab counter. If taken then no new slabs may be added or
39 * removed from the lists nor make the number of partial slabs be modified.
40 * (Note that the total number of slabs is an atomic value that may be
41 * modified without taking the list lock).
42 *
43 * The list_lock is a centralized lock and thus we avoid taking it as
44 * much as possible. As long as SLUB does not have to handle partial
45 * slabs, operations can continue without any centralized lock. F.e.
46 * allocating a long series of objects that fill up slabs does not require
47 * the list lock.
48 *
49 * The lock order is sometimes inverted when we are trying to get a slab
50 * off a list. We take the list_lock and then look for a page on the list
51 * to use. While we do that objects in the slabs may be freed. We can
52 * only operate on the slab if we have also taken the slab_lock. So we use
53 * a slab_trylock() on the slab. If trylock was successful then no frees
54 * can occur anymore and we can use the slab for allocations etc. If the
55 * slab_trylock() does not succeed then frees are in progress in the slab and
56 * we must stay away from it for a while since we may cause a bouncing
57 * cacheline if we try to acquire the lock. So go onto the next slab.
58 * If all pages are busy then we may allocate a new slab instead of reusing
59 * a partial slab. A new slab has noone operating on it and thus there is
60 * no danger of cacheline contention.
61 *
62 * Interrupts are disabled during allocation and deallocation in order to
63 * make the slab allocator safe to use in the context of an irq. In addition
64 * interrupts are disabled to ensure that the processor does not change
65 * while handling per_cpu slabs, due to kernel preemption.
66 *
67 * SLUB assigns one slab for allocation to each processor.
68 * Allocations only occur from these slabs called cpu slabs.
69 *
Christoph Lameter672bba32007-05-09 02:32:39 -070070 * Slabs with free elements are kept on a partial list and during regular
71 * operations no list for full slabs is used. If an object in a full slab is
Christoph Lameter81819f02007-05-06 14:49:36 -070072 * freed then the slab will show up again on the partial lists.
Christoph Lameter672bba32007-05-09 02:32:39 -070073 * We track full slabs for debugging purposes though because otherwise we
74 * cannot scan all objects.
Christoph Lameter81819f02007-05-06 14:49:36 -070075 *
76 * Slabs are freed when they become empty. Teardown and setup is
77 * minimal so we rely on the page allocators per cpu caches for
78 * fast frees and allocs.
79 *
80 * Overloading of page flags that are otherwise used for LRU management.
81 *
Christoph Lameter4b6f0752007-05-16 22:10:53 -070082 * PageActive The slab is frozen and exempt from list processing.
83 * This means that the slab is dedicated to a purpose
84 * such as satisfying allocations for a specific
85 * processor. Objects may be freed in the slab while
86 * it is frozen but slab_free will then skip the usual
87 * list operations. It is up to the processor holding
88 * the slab to integrate the slab into the slab lists
89 * when the slab is no longer needed.
90 *
91 * One use of this flag is to mark slabs that are
92 * used for allocations. Then such a slab becomes a cpu
93 * slab. The cpu slab may be equipped with an additional
Christoph Lameterdfb4f092007-10-16 01:26:05 -070094 * freelist that allows lockless access to
Christoph Lameter894b8782007-05-10 03:15:16 -070095 * free objects in addition to the regular freelist
96 * that requires the slab lock.
Christoph Lameter81819f02007-05-06 14:49:36 -070097 *
98 * PageError Slab requires special handling due to debug
99 * options set. This moves slab handling out of
Christoph Lameter894b8782007-05-10 03:15:16 -0700100 * the fast path and disables lockless freelists.
Christoph Lameter81819f02007-05-06 14:49:36 -0700101 */
102
Christoph Lameter5577bd82007-05-16 22:10:56 -0700103#define FROZEN (1 << PG_active)
104
105#ifdef CONFIG_SLUB_DEBUG
106#define SLABDEBUG (1 << PG_error)
107#else
108#define SLABDEBUG 0
109#endif
110
Christoph Lameter4b6f0752007-05-16 22:10:53 -0700111static inline int SlabFrozen(struct page *page)
112{
Christoph Lameter5577bd82007-05-16 22:10:56 -0700113 return page->flags & FROZEN;
Christoph Lameter4b6f0752007-05-16 22:10:53 -0700114}
115
116static inline void SetSlabFrozen(struct page *page)
117{
Christoph Lameter5577bd82007-05-16 22:10:56 -0700118 page->flags |= FROZEN;
Christoph Lameter4b6f0752007-05-16 22:10:53 -0700119}
120
121static inline void ClearSlabFrozen(struct page *page)
122{
Christoph Lameter5577bd82007-05-16 22:10:56 -0700123 page->flags &= ~FROZEN;
Christoph Lameter4b6f0752007-05-16 22:10:53 -0700124}
125
Christoph Lameter35e5d7e2007-05-09 02:32:42 -0700126static inline int SlabDebug(struct page *page)
127{
Christoph Lameter5577bd82007-05-16 22:10:56 -0700128 return page->flags & SLABDEBUG;
Christoph Lameter35e5d7e2007-05-09 02:32:42 -0700129}
130
131static inline void SetSlabDebug(struct page *page)
132{
Christoph Lameter5577bd82007-05-16 22:10:56 -0700133 page->flags |= SLABDEBUG;
Christoph Lameter35e5d7e2007-05-09 02:32:42 -0700134}
135
136static inline void ClearSlabDebug(struct page *page)
137{
Christoph Lameter5577bd82007-05-16 22:10:56 -0700138 page->flags &= ~SLABDEBUG;
Christoph Lameter35e5d7e2007-05-09 02:32:42 -0700139}
140
Christoph Lameter81819f02007-05-06 14:49:36 -0700141/*
142 * Issues still to be resolved:
143 *
Christoph Lameter81819f02007-05-06 14:49:36 -0700144 * - Support PAGE_ALLOC_DEBUG. Should be easy to do.
145 *
Christoph Lameter81819f02007-05-06 14:49:36 -0700146 * - Variable sizing of the per node arrays
147 */
148
149/* Enable to test recovery from slab corruption on boot */
150#undef SLUB_RESILIENCY_TEST
151
152#if PAGE_SHIFT <= 12
153
154/*
155 * Small page size. Make sure that we do not fragment memory
156 */
157#define DEFAULT_MAX_ORDER 1
158#define DEFAULT_MIN_OBJECTS 4
159
160#else
161
162/*
163 * Large page machines are customarily able to handle larger
164 * page orders.
165 */
166#define DEFAULT_MAX_ORDER 2
167#define DEFAULT_MIN_OBJECTS 8
168
169#endif
170
171/*
Christoph Lameter2086d262007-05-06 14:49:46 -0700172 * Mininum number of partial slabs. These will be left on the partial
173 * lists even if they are empty. kmem_cache_shrink may reclaim them.
174 */
Christoph Lameter76be8952007-12-21 14:37:37 -0800175#define MIN_PARTIAL 5
Christoph Lametere95eed52007-05-06 14:49:44 -0700176
Christoph Lameter2086d262007-05-06 14:49:46 -0700177/*
178 * Maximum number of desirable partial slabs.
179 * The existence of more partial slabs makes kmem_cache_shrink
180 * sort the partial list by the number of objects in the.
181 */
182#define MAX_PARTIAL 10
183
Christoph Lameter81819f02007-05-06 14:49:36 -0700184#define DEBUG_DEFAULT_FLAGS (SLAB_DEBUG_FREE | SLAB_RED_ZONE | \
185 SLAB_POISON | SLAB_STORE_USER)
Christoph Lameter672bba32007-05-09 02:32:39 -0700186
Christoph Lameter81819f02007-05-06 14:49:36 -0700187/*
188 * Set of flags that will prevent slab merging
189 */
190#define SLUB_NEVER_MERGE (SLAB_RED_ZONE | SLAB_POISON | SLAB_STORE_USER | \
191 SLAB_TRACE | SLAB_DESTROY_BY_RCU)
192
193#define SLUB_MERGE_SAME (SLAB_DEBUG_FREE | SLAB_RECLAIM_ACCOUNT | \
194 SLAB_CACHE_DMA)
195
196#ifndef ARCH_KMALLOC_MINALIGN
Christoph Lameter47bfdc02007-05-06 14:49:37 -0700197#define ARCH_KMALLOC_MINALIGN __alignof__(unsigned long long)
Christoph Lameter81819f02007-05-06 14:49:36 -0700198#endif
199
200#ifndef ARCH_SLAB_MINALIGN
Christoph Lameter47bfdc02007-05-06 14:49:37 -0700201#define ARCH_SLAB_MINALIGN __alignof__(unsigned long long)
Christoph Lameter81819f02007-05-06 14:49:36 -0700202#endif
203
204/* Internal SLUB flags */
Christoph Lameter1ceef402007-08-07 15:11:48 -0700205#define __OBJECT_POISON 0x80000000 /* Poison object */
206#define __SYSFS_ADD_DEFERRED 0x40000000 /* Not yet visible via sysfs */
Christoph Lameter71c7a062008-02-14 14:28:01 -0800207#define __KMALLOC_CACHE 0x20000000 /* objects freed using kfree */
208#define __PAGE_ALLOC_FALLBACK 0x10000000 /* Allow fallback to page alloc */
Christoph Lameter81819f02007-05-06 14:49:36 -0700209
210static int kmem_size = sizeof(struct kmem_cache);
211
212#ifdef CONFIG_SMP
213static struct notifier_block slab_notifier;
214#endif
215
216static enum {
217 DOWN, /* No slab functionality available */
218 PARTIAL, /* kmem_cache_open() works but kmalloc does not */
Christoph Lameter672bba32007-05-09 02:32:39 -0700219 UP, /* Everything works but does not show up in sysfs */
Christoph Lameter81819f02007-05-06 14:49:36 -0700220 SYSFS /* Sysfs up */
221} slab_state = DOWN;
222
223/* A list of all slab caches on the system */
224static DECLARE_RWSEM(slub_lock);
Adrian Bunk5af328a2007-07-17 04:03:27 -0700225static LIST_HEAD(slab_caches);
Christoph Lameter81819f02007-05-06 14:49:36 -0700226
Christoph Lameter02cbc872007-05-09 02:32:43 -0700227/*
228 * Tracking user of a slab.
229 */
230struct track {
231 void *addr; /* Called from address */
232 int cpu; /* Was running on cpu */
233 int pid; /* Pid context */
234 unsigned long when; /* When did the operation occur */
235};
236
237enum track_item { TRACK_ALLOC, TRACK_FREE };
238
Christoph Lameter41ecc552007-05-09 02:32:44 -0700239#if defined(CONFIG_SYSFS) && defined(CONFIG_SLUB_DEBUG)
Christoph Lameter81819f02007-05-06 14:49:36 -0700240static int sysfs_slab_add(struct kmem_cache *);
241static int sysfs_slab_alias(struct kmem_cache *, const char *);
242static void sysfs_slab_remove(struct kmem_cache *);
Christoph Lameter8ff12cf2008-02-07 17:47:41 -0800243
Christoph Lameter81819f02007-05-06 14:49:36 -0700244#else
Christoph Lameter0c710012007-07-17 04:03:24 -0700245static inline int sysfs_slab_add(struct kmem_cache *s) { return 0; }
246static inline int sysfs_slab_alias(struct kmem_cache *s, const char *p)
247 { return 0; }
Christoph Lameter151c6022008-01-07 22:29:05 -0800248static inline void sysfs_slab_remove(struct kmem_cache *s)
249{
250 kfree(s);
251}
Christoph Lameter8ff12cf2008-02-07 17:47:41 -0800252
Christoph Lameter81819f02007-05-06 14:49:36 -0700253#endif
254
Christoph Lameter8ff12cf2008-02-07 17:47:41 -0800255static inline void stat(struct kmem_cache_cpu *c, enum stat_item si)
256{
257#ifdef CONFIG_SLUB_STATS
258 c->stat[si]++;
259#endif
260}
261
Christoph Lameter81819f02007-05-06 14:49:36 -0700262/********************************************************************
263 * Core slab cache functions
264 *******************************************************************/
265
266int slab_is_available(void)
267{
268 return slab_state >= UP;
269}
270
271static inline struct kmem_cache_node *get_node(struct kmem_cache *s, int node)
272{
273#ifdef CONFIG_NUMA
274 return s->node[node];
275#else
276 return &s->local_node;
277#endif
278}
279
Christoph Lameterdfb4f092007-10-16 01:26:05 -0700280static inline struct kmem_cache_cpu *get_cpu_slab(struct kmem_cache *s, int cpu)
281{
Christoph Lameter4c93c3552007-10-16 01:26:08 -0700282#ifdef CONFIG_SMP
283 return s->cpu_slab[cpu];
284#else
285 return &s->cpu_slab;
286#endif
Christoph Lameterdfb4f092007-10-16 01:26:05 -0700287}
288
Christoph Lameter6446faa2008-02-15 23:45:26 -0800289/* Verify that a pointer has an address that is valid within a slab page */
Christoph Lameter02cbc872007-05-09 02:32:43 -0700290static inline int check_valid_pointer(struct kmem_cache *s,
291 struct page *page, const void *object)
292{
293 void *base;
294
Christoph Lametera973e9d2008-03-01 13:40:44 -0800295 if (!object)
Christoph Lameter02cbc872007-05-09 02:32:43 -0700296 return 1;
297
Christoph Lametera973e9d2008-03-01 13:40:44 -0800298 base = page_address(page);
Christoph Lameter02cbc872007-05-09 02:32:43 -0700299 if (object < base || object >= base + s->objects * s->size ||
300 (object - base) % s->size) {
301 return 0;
302 }
303
304 return 1;
305}
306
Christoph Lameter81819f02007-05-06 14:49:36 -0700307/*
Christoph Lameter7656c722007-05-09 02:32:40 -0700308 * Slow version of get and set free pointer.
309 *
310 * This version requires touching the cache lines of kmem_cache which
311 * we avoid to do in the fast alloc free paths. There we obtain the offset
312 * from the page struct.
313 */
314static inline void *get_freepointer(struct kmem_cache *s, void *object)
315{
316 return *(void **)(object + s->offset);
317}
318
319static inline void set_freepointer(struct kmem_cache *s, void *object, void *fp)
320{
321 *(void **)(object + s->offset) = fp;
322}
323
324/* Loop over all objects in a slab */
325#define for_each_object(__p, __s, __addr) \
326 for (__p = (__addr); __p < (__addr) + (__s)->objects * (__s)->size;\
327 __p += (__s)->size)
328
329/* Scan freelist */
330#define for_each_free_object(__p, __s, __free) \
Christoph Lametera973e9d2008-03-01 13:40:44 -0800331 for (__p = (__free); __p; __p = get_freepointer((__s), __p))
Christoph Lameter7656c722007-05-09 02:32:40 -0700332
333/* Determine object index from a given position */
334static inline int slab_index(void *p, struct kmem_cache *s, void *addr)
335{
336 return (p - addr) / s->size;
337}
338
Christoph Lameter41ecc552007-05-09 02:32:44 -0700339#ifdef CONFIG_SLUB_DEBUG
340/*
341 * Debug settings:
342 */
Christoph Lameterf0630ff2007-07-15 23:38:14 -0700343#ifdef CONFIG_SLUB_DEBUG_ON
344static int slub_debug = DEBUG_DEFAULT_FLAGS;
345#else
Christoph Lameter41ecc552007-05-09 02:32:44 -0700346static int slub_debug;
Christoph Lameterf0630ff2007-07-15 23:38:14 -0700347#endif
Christoph Lameter41ecc552007-05-09 02:32:44 -0700348
349static char *slub_debug_slabs;
350
Christoph Lameter7656c722007-05-09 02:32:40 -0700351/*
Christoph Lameter81819f02007-05-06 14:49:36 -0700352 * Object debugging
353 */
354static void print_section(char *text, u8 *addr, unsigned int length)
355{
356 int i, offset;
357 int newline = 1;
358 char ascii[17];
359
360 ascii[16] = 0;
361
362 for (i = 0; i < length; i++) {
363 if (newline) {
Christoph Lameter24922682007-07-17 04:03:18 -0700364 printk(KERN_ERR "%8s 0x%p: ", text, addr + i);
Christoph Lameter81819f02007-05-06 14:49:36 -0700365 newline = 0;
366 }
Pekka Enberg06428782008-01-07 23:20:27 -0800367 printk(KERN_CONT " %02x", addr[i]);
Christoph Lameter81819f02007-05-06 14:49:36 -0700368 offset = i % 16;
369 ascii[offset] = isgraph(addr[i]) ? addr[i] : '.';
370 if (offset == 15) {
Pekka Enberg06428782008-01-07 23:20:27 -0800371 printk(KERN_CONT " %s\n", ascii);
Christoph Lameter81819f02007-05-06 14:49:36 -0700372 newline = 1;
373 }
374 }
375 if (!newline) {
376 i %= 16;
377 while (i < 16) {
Pekka Enberg06428782008-01-07 23:20:27 -0800378 printk(KERN_CONT " ");
Christoph Lameter81819f02007-05-06 14:49:36 -0700379 ascii[i] = ' ';
380 i++;
381 }
Pekka Enberg06428782008-01-07 23:20:27 -0800382 printk(KERN_CONT " %s\n", ascii);
Christoph Lameter81819f02007-05-06 14:49:36 -0700383 }
384}
385
Christoph Lameter81819f02007-05-06 14:49:36 -0700386static struct track *get_track(struct kmem_cache *s, void *object,
387 enum track_item alloc)
388{
389 struct track *p;
390
391 if (s->offset)
392 p = object + s->offset + sizeof(void *);
393 else
394 p = object + s->inuse;
395
396 return p + alloc;
397}
398
399static void set_track(struct kmem_cache *s, void *object,
400 enum track_item alloc, void *addr)
401{
402 struct track *p;
403
404 if (s->offset)
405 p = object + s->offset + sizeof(void *);
406 else
407 p = object + s->inuse;
408
409 p += alloc;
410 if (addr) {
411 p->addr = addr;
412 p->cpu = smp_processor_id();
413 p->pid = current ? current->pid : -1;
414 p->when = jiffies;
415 } else
416 memset(p, 0, sizeof(struct track));
417}
418
Christoph Lameter81819f02007-05-06 14:49:36 -0700419static void init_tracking(struct kmem_cache *s, void *object)
420{
Christoph Lameter24922682007-07-17 04:03:18 -0700421 if (!(s->flags & SLAB_STORE_USER))
422 return;
423
424 set_track(s, object, TRACK_FREE, NULL);
425 set_track(s, object, TRACK_ALLOC, NULL);
Christoph Lameter81819f02007-05-06 14:49:36 -0700426}
427
428static void print_track(const char *s, struct track *t)
429{
430 if (!t->addr)
431 return;
432
Christoph Lameter24922682007-07-17 04:03:18 -0700433 printk(KERN_ERR "INFO: %s in ", s);
Christoph Lameter81819f02007-05-06 14:49:36 -0700434 __print_symbol("%s", (unsigned long)t->addr);
Christoph Lameter24922682007-07-17 04:03:18 -0700435 printk(" age=%lu cpu=%u pid=%d\n", jiffies - t->when, t->cpu, t->pid);
Christoph Lameter81819f02007-05-06 14:49:36 -0700436}
437
Christoph Lameter24922682007-07-17 04:03:18 -0700438static void print_tracking(struct kmem_cache *s, void *object)
439{
440 if (!(s->flags & SLAB_STORE_USER))
441 return;
442
443 print_track("Allocated", get_track(s, object, TRACK_ALLOC));
444 print_track("Freed", get_track(s, object, TRACK_FREE));
445}
446
447static void print_page_info(struct page *page)
448{
449 printk(KERN_ERR "INFO: Slab 0x%p used=%u fp=0x%p flags=0x%04lx\n",
450 page, page->inuse, page->freelist, page->flags);
451
452}
453
454static void slab_bug(struct kmem_cache *s, char *fmt, ...)
455{
456 va_list args;
457 char buf[100];
458
459 va_start(args, fmt);
460 vsnprintf(buf, sizeof(buf), fmt, args);
461 va_end(args);
462 printk(KERN_ERR "========================================"
463 "=====================================\n");
464 printk(KERN_ERR "BUG %s: %s\n", s->name, buf);
465 printk(KERN_ERR "----------------------------------------"
466 "-------------------------------------\n\n");
467}
468
469static void slab_fix(struct kmem_cache *s, char *fmt, ...)
470{
471 va_list args;
472 char buf[100];
473
474 va_start(args, fmt);
475 vsnprintf(buf, sizeof(buf), fmt, args);
476 va_end(args);
477 printk(KERN_ERR "FIX %s: %s\n", s->name, buf);
478}
479
480static void print_trailer(struct kmem_cache *s, struct page *page, u8 *p)
Christoph Lameter81819f02007-05-06 14:49:36 -0700481{
482 unsigned int off; /* Offset of last byte */
Christoph Lametera973e9d2008-03-01 13:40:44 -0800483 u8 *addr = page_address(page);
Christoph Lameter24922682007-07-17 04:03:18 -0700484
485 print_tracking(s, p);
486
487 print_page_info(page);
488
489 printk(KERN_ERR "INFO: Object 0x%p @offset=%tu fp=0x%p\n\n",
490 p, p - addr, get_freepointer(s, p));
491
492 if (p > addr + 16)
493 print_section("Bytes b4", p - 16, 16);
494
495 print_section("Object", p, min(s->objsize, 128));
Christoph Lameter81819f02007-05-06 14:49:36 -0700496
497 if (s->flags & SLAB_RED_ZONE)
498 print_section("Redzone", p + s->objsize,
499 s->inuse - s->objsize);
500
Christoph Lameter81819f02007-05-06 14:49:36 -0700501 if (s->offset)
502 off = s->offset + sizeof(void *);
503 else
504 off = s->inuse;
505
Christoph Lameter24922682007-07-17 04:03:18 -0700506 if (s->flags & SLAB_STORE_USER)
Christoph Lameter81819f02007-05-06 14:49:36 -0700507 off += 2 * sizeof(struct track);
Christoph Lameter81819f02007-05-06 14:49:36 -0700508
509 if (off != s->size)
510 /* Beginning of the filler is the free pointer */
Christoph Lameter24922682007-07-17 04:03:18 -0700511 print_section("Padding", p + off, s->size - off);
512
513 dump_stack();
Christoph Lameter81819f02007-05-06 14:49:36 -0700514}
515
516static void object_err(struct kmem_cache *s, struct page *page,
517 u8 *object, char *reason)
518{
Christoph Lameter3dc50632008-04-23 12:28:01 -0700519 slab_bug(s, "%s", reason);
Christoph Lameter24922682007-07-17 04:03:18 -0700520 print_trailer(s, page, object);
Christoph Lameter81819f02007-05-06 14:49:36 -0700521}
522
Christoph Lameter24922682007-07-17 04:03:18 -0700523static void slab_err(struct kmem_cache *s, struct page *page, char *fmt, ...)
Christoph Lameter81819f02007-05-06 14:49:36 -0700524{
525 va_list args;
526 char buf[100];
527
Christoph Lameter24922682007-07-17 04:03:18 -0700528 va_start(args, fmt);
529 vsnprintf(buf, sizeof(buf), fmt, args);
Christoph Lameter81819f02007-05-06 14:49:36 -0700530 va_end(args);
Christoph Lameter3dc50632008-04-23 12:28:01 -0700531 slab_bug(s, "%s", buf);
Christoph Lameter24922682007-07-17 04:03:18 -0700532 print_page_info(page);
Christoph Lameter81819f02007-05-06 14:49:36 -0700533 dump_stack();
534}
535
536static void init_object(struct kmem_cache *s, void *object, int active)
537{
538 u8 *p = object;
539
540 if (s->flags & __OBJECT_POISON) {
541 memset(p, POISON_FREE, s->objsize - 1);
Pekka Enberg06428782008-01-07 23:20:27 -0800542 p[s->objsize - 1] = POISON_END;
Christoph Lameter81819f02007-05-06 14:49:36 -0700543 }
544
545 if (s->flags & SLAB_RED_ZONE)
546 memset(p + s->objsize,
547 active ? SLUB_RED_ACTIVE : SLUB_RED_INACTIVE,
548 s->inuse - s->objsize);
549}
550
Christoph Lameter24922682007-07-17 04:03:18 -0700551static u8 *check_bytes(u8 *start, unsigned int value, unsigned int bytes)
Christoph Lameter81819f02007-05-06 14:49:36 -0700552{
553 while (bytes) {
554 if (*start != (u8)value)
Christoph Lameter24922682007-07-17 04:03:18 -0700555 return start;
Christoph Lameter81819f02007-05-06 14:49:36 -0700556 start++;
557 bytes--;
558 }
Christoph Lameter24922682007-07-17 04:03:18 -0700559 return NULL;
560}
561
562static void restore_bytes(struct kmem_cache *s, char *message, u8 data,
563 void *from, void *to)
564{
565 slab_fix(s, "Restoring 0x%p-0x%p=0x%x\n", from, to - 1, data);
566 memset(from, data, to - from);
567}
568
569static int check_bytes_and_report(struct kmem_cache *s, struct page *page,
570 u8 *object, char *what,
Pekka Enberg06428782008-01-07 23:20:27 -0800571 u8 *start, unsigned int value, unsigned int bytes)
Christoph Lameter24922682007-07-17 04:03:18 -0700572{
573 u8 *fault;
574 u8 *end;
575
576 fault = check_bytes(start, value, bytes);
577 if (!fault)
578 return 1;
579
580 end = start + bytes;
581 while (end > fault && end[-1] == value)
582 end--;
583
584 slab_bug(s, "%s overwritten", what);
585 printk(KERN_ERR "INFO: 0x%p-0x%p. First byte 0x%x instead of 0x%x\n",
586 fault, end - 1, fault[0], value);
587 print_trailer(s, page, object);
588
589 restore_bytes(s, what, value, fault, end);
590 return 0;
Christoph Lameter81819f02007-05-06 14:49:36 -0700591}
592
Christoph Lameter81819f02007-05-06 14:49:36 -0700593/*
594 * Object layout:
595 *
596 * object address
597 * Bytes of the object to be managed.
598 * If the freepointer may overlay the object then the free
599 * pointer is the first word of the object.
Christoph Lameter672bba32007-05-09 02:32:39 -0700600 *
Christoph Lameter81819f02007-05-06 14:49:36 -0700601 * Poisoning uses 0x6b (POISON_FREE) and the last byte is
602 * 0xa5 (POISON_END)
603 *
604 * object + s->objsize
605 * Padding to reach word boundary. This is also used for Redzoning.
Christoph Lameter672bba32007-05-09 02:32:39 -0700606 * Padding is extended by another word if Redzoning is enabled and
607 * objsize == inuse.
608 *
Christoph Lameter81819f02007-05-06 14:49:36 -0700609 * We fill with 0xbb (RED_INACTIVE) for inactive objects and with
610 * 0xcc (RED_ACTIVE) for objects in use.
611 *
612 * object + s->inuse
Christoph Lameter672bba32007-05-09 02:32:39 -0700613 * Meta data starts here.
614 *
Christoph Lameter81819f02007-05-06 14:49:36 -0700615 * A. Free pointer (if we cannot overwrite object on free)
616 * B. Tracking data for SLAB_STORE_USER
Christoph Lameter672bba32007-05-09 02:32:39 -0700617 * C. Padding to reach required alignment boundary or at mininum
Christoph Lameter6446faa2008-02-15 23:45:26 -0800618 * one word if debugging is on to be able to detect writes
Christoph Lameter672bba32007-05-09 02:32:39 -0700619 * before the word boundary.
620 *
621 * Padding is done using 0x5a (POISON_INUSE)
Christoph Lameter81819f02007-05-06 14:49:36 -0700622 *
623 * object + s->size
Christoph Lameter672bba32007-05-09 02:32:39 -0700624 * Nothing is used beyond s->size.
Christoph Lameter81819f02007-05-06 14:49:36 -0700625 *
Christoph Lameter672bba32007-05-09 02:32:39 -0700626 * If slabcaches are merged then the objsize and inuse boundaries are mostly
627 * ignored. And therefore no slab options that rely on these boundaries
Christoph Lameter81819f02007-05-06 14:49:36 -0700628 * may be used with merged slabcaches.
629 */
630
Christoph Lameter81819f02007-05-06 14:49:36 -0700631static int check_pad_bytes(struct kmem_cache *s, struct page *page, u8 *p)
632{
633 unsigned long off = s->inuse; /* The end of info */
634
635 if (s->offset)
636 /* Freepointer is placed after the object. */
637 off += sizeof(void *);
638
639 if (s->flags & SLAB_STORE_USER)
640 /* We also have user information there */
641 off += 2 * sizeof(struct track);
642
643 if (s->size == off)
644 return 1;
645
Christoph Lameter24922682007-07-17 04:03:18 -0700646 return check_bytes_and_report(s, page, p, "Object padding",
647 p + off, POISON_INUSE, s->size - off);
Christoph Lameter81819f02007-05-06 14:49:36 -0700648}
649
650static int slab_pad_check(struct kmem_cache *s, struct page *page)
651{
Christoph Lameter24922682007-07-17 04:03:18 -0700652 u8 *start;
653 u8 *fault;
654 u8 *end;
655 int length;
656 int remainder;
Christoph Lameter81819f02007-05-06 14:49:36 -0700657
658 if (!(s->flags & SLAB_POISON))
659 return 1;
660
Christoph Lametera973e9d2008-03-01 13:40:44 -0800661 start = page_address(page);
Christoph Lameter24922682007-07-17 04:03:18 -0700662 end = start + (PAGE_SIZE << s->order);
Christoph Lameter81819f02007-05-06 14:49:36 -0700663 length = s->objects * s->size;
Christoph Lameter24922682007-07-17 04:03:18 -0700664 remainder = end - (start + length);
Christoph Lameter81819f02007-05-06 14:49:36 -0700665 if (!remainder)
666 return 1;
667
Christoph Lameter24922682007-07-17 04:03:18 -0700668 fault = check_bytes(start + length, POISON_INUSE, remainder);
669 if (!fault)
670 return 1;
671 while (end > fault && end[-1] == POISON_INUSE)
672 end--;
673
674 slab_err(s, page, "Padding overwritten. 0x%p-0x%p", fault, end - 1);
675 print_section("Padding", start, length);
676
677 restore_bytes(s, "slab padding", POISON_INUSE, start, end);
678 return 0;
Christoph Lameter81819f02007-05-06 14:49:36 -0700679}
680
681static int check_object(struct kmem_cache *s, struct page *page,
682 void *object, int active)
683{
684 u8 *p = object;
685 u8 *endobject = object + s->objsize;
686
687 if (s->flags & SLAB_RED_ZONE) {
688 unsigned int red =
689 active ? SLUB_RED_ACTIVE : SLUB_RED_INACTIVE;
690
Christoph Lameter24922682007-07-17 04:03:18 -0700691 if (!check_bytes_and_report(s, page, object, "Redzone",
692 endobject, red, s->inuse - s->objsize))
Christoph Lameter81819f02007-05-06 14:49:36 -0700693 return 0;
Christoph Lameter81819f02007-05-06 14:49:36 -0700694 } else {
Ingo Molnar3adbefe2008-02-05 17:57:39 -0800695 if ((s->flags & SLAB_POISON) && s->objsize < s->inuse) {
696 check_bytes_and_report(s, page, p, "Alignment padding",
697 endobject, POISON_INUSE, s->inuse - s->objsize);
698 }
Christoph Lameter81819f02007-05-06 14:49:36 -0700699 }
700
701 if (s->flags & SLAB_POISON) {
702 if (!active && (s->flags & __OBJECT_POISON) &&
Christoph Lameter24922682007-07-17 04:03:18 -0700703 (!check_bytes_and_report(s, page, p, "Poison", p,
704 POISON_FREE, s->objsize - 1) ||
705 !check_bytes_and_report(s, page, p, "Poison",
Pekka Enberg06428782008-01-07 23:20:27 -0800706 p + s->objsize - 1, POISON_END, 1)))
Christoph Lameter81819f02007-05-06 14:49:36 -0700707 return 0;
Christoph Lameter81819f02007-05-06 14:49:36 -0700708 /*
709 * check_pad_bytes cleans up on its own.
710 */
711 check_pad_bytes(s, page, p);
712 }
713
714 if (!s->offset && active)
715 /*
716 * Object and freepointer overlap. Cannot check
717 * freepointer while object is allocated.
718 */
719 return 1;
720
721 /* Check free pointer validity */
722 if (!check_valid_pointer(s, page, get_freepointer(s, p))) {
723 object_err(s, page, p, "Freepointer corrupt");
724 /*
725 * No choice but to zap it and thus loose the remainder
726 * of the free objects in this slab. May cause
Christoph Lameter672bba32007-05-09 02:32:39 -0700727 * another error because the object count is now wrong.
Christoph Lameter81819f02007-05-06 14:49:36 -0700728 */
Christoph Lametera973e9d2008-03-01 13:40:44 -0800729 set_freepointer(s, p, NULL);
Christoph Lameter81819f02007-05-06 14:49:36 -0700730 return 0;
731 }
732 return 1;
733}
734
735static int check_slab(struct kmem_cache *s, struct page *page)
736{
737 VM_BUG_ON(!irqs_disabled());
738
739 if (!PageSlab(page)) {
Christoph Lameter24922682007-07-17 04:03:18 -0700740 slab_err(s, page, "Not a valid slab page");
Christoph Lameter81819f02007-05-06 14:49:36 -0700741 return 0;
742 }
Christoph Lameter81819f02007-05-06 14:49:36 -0700743 if (page->inuse > s->objects) {
Christoph Lameter24922682007-07-17 04:03:18 -0700744 slab_err(s, page, "inuse %u > max %u",
745 s->name, page->inuse, s->objects);
Christoph Lameter81819f02007-05-06 14:49:36 -0700746 return 0;
747 }
748 /* Slab_pad_check fixes things up after itself */
749 slab_pad_check(s, page);
750 return 1;
751}
752
753/*
Christoph Lameter672bba32007-05-09 02:32:39 -0700754 * Determine if a certain object on a page is on the freelist. Must hold the
755 * slab lock to guarantee that the chains are in a consistent state.
Christoph Lameter81819f02007-05-06 14:49:36 -0700756 */
757static int on_freelist(struct kmem_cache *s, struct page *page, void *search)
758{
759 int nr = 0;
760 void *fp = page->freelist;
761 void *object = NULL;
762
Christoph Lametera973e9d2008-03-01 13:40:44 -0800763 while (fp && nr <= s->objects) {
Christoph Lameter81819f02007-05-06 14:49:36 -0700764 if (fp == search)
765 return 1;
766 if (!check_valid_pointer(s, page, fp)) {
767 if (object) {
768 object_err(s, page, object,
769 "Freechain corrupt");
Christoph Lametera973e9d2008-03-01 13:40:44 -0800770 set_freepointer(s, object, NULL);
Christoph Lameter81819f02007-05-06 14:49:36 -0700771 break;
772 } else {
Christoph Lameter24922682007-07-17 04:03:18 -0700773 slab_err(s, page, "Freepointer corrupt");
Christoph Lametera973e9d2008-03-01 13:40:44 -0800774 page->freelist = NULL;
Christoph Lameter81819f02007-05-06 14:49:36 -0700775 page->inuse = s->objects;
Christoph Lameter24922682007-07-17 04:03:18 -0700776 slab_fix(s, "Freelist cleared");
Christoph Lameter81819f02007-05-06 14:49:36 -0700777 return 0;
778 }
779 break;
780 }
781 object = fp;
782 fp = get_freepointer(s, object);
783 nr++;
784 }
785
786 if (page->inuse != s->objects - nr) {
Christoph Lameter70d71222007-05-06 14:49:47 -0700787 slab_err(s, page, "Wrong object count. Counter is %d but "
Christoph Lameter24922682007-07-17 04:03:18 -0700788 "counted were %d", page->inuse, s->objects - nr);
Christoph Lameter81819f02007-05-06 14:49:36 -0700789 page->inuse = s->objects - nr;
Christoph Lameter24922682007-07-17 04:03:18 -0700790 slab_fix(s, "Object count adjusted.");
Christoph Lameter81819f02007-05-06 14:49:36 -0700791 }
792 return search == NULL;
793}
794
Christoph Lameter3ec09742007-05-16 22:11:00 -0700795static void trace(struct kmem_cache *s, struct page *page, void *object, int alloc)
796{
797 if (s->flags & SLAB_TRACE) {
798 printk(KERN_INFO "TRACE %s %s 0x%p inuse=%d fp=0x%p\n",
799 s->name,
800 alloc ? "alloc" : "free",
801 object, page->inuse,
802 page->freelist);
803
804 if (!alloc)
805 print_section("Object", (void *)object, s->objsize);
806
807 dump_stack();
808 }
809}
810
Christoph Lameter643b1132007-05-06 14:49:42 -0700811/*
Christoph Lameter672bba32007-05-09 02:32:39 -0700812 * Tracking of fully allocated slabs for debugging purposes.
Christoph Lameter643b1132007-05-06 14:49:42 -0700813 */
Christoph Lametere95eed52007-05-06 14:49:44 -0700814static void add_full(struct kmem_cache_node *n, struct page *page)
Christoph Lameter643b1132007-05-06 14:49:42 -0700815{
Christoph Lameter643b1132007-05-06 14:49:42 -0700816 spin_lock(&n->list_lock);
817 list_add(&page->lru, &n->full);
818 spin_unlock(&n->list_lock);
819}
820
821static void remove_full(struct kmem_cache *s, struct page *page)
822{
823 struct kmem_cache_node *n;
824
825 if (!(s->flags & SLAB_STORE_USER))
826 return;
827
828 n = get_node(s, page_to_nid(page));
829
830 spin_lock(&n->list_lock);
831 list_del(&page->lru);
832 spin_unlock(&n->list_lock);
833}
834
Christoph Lameter0f389ec2008-04-14 18:53:02 +0300835/* Tracking of the number of slabs for debugging purposes */
836static inline unsigned long slabs_node(struct kmem_cache *s, int node)
837{
838 struct kmem_cache_node *n = get_node(s, node);
839
840 return atomic_long_read(&n->nr_slabs);
841}
842
843static inline void inc_slabs_node(struct kmem_cache *s, int node)
844{
845 struct kmem_cache_node *n = get_node(s, node);
846
847 /*
848 * May be called early in order to allocate a slab for the
849 * kmem_cache_node structure. Solve the chicken-egg
850 * dilemma by deferring the increment of the count during
851 * bootstrap (see early_kmem_cache_node_alloc).
852 */
853 if (!NUMA_BUILD || n)
854 atomic_long_inc(&n->nr_slabs);
855}
856static inline void dec_slabs_node(struct kmem_cache *s, int node)
857{
858 struct kmem_cache_node *n = get_node(s, node);
859
860 atomic_long_dec(&n->nr_slabs);
861}
862
863/* Object debug checks for alloc/free paths */
Christoph Lameter3ec09742007-05-16 22:11:00 -0700864static void setup_object_debug(struct kmem_cache *s, struct page *page,
865 void *object)
866{
867 if (!(s->flags & (SLAB_STORE_USER|SLAB_RED_ZONE|__OBJECT_POISON)))
868 return;
869
870 init_object(s, object, 0);
871 init_tracking(s, object);
872}
873
874static int alloc_debug_processing(struct kmem_cache *s, struct page *page,
875 void *object, void *addr)
Christoph Lameter81819f02007-05-06 14:49:36 -0700876{
877 if (!check_slab(s, page))
878 goto bad;
879
Christoph Lameterd692ef62008-02-15 23:45:24 -0800880 if (!on_freelist(s, page, object)) {
Christoph Lameter24922682007-07-17 04:03:18 -0700881 object_err(s, page, object, "Object already allocated");
Christoph Lameter70d71222007-05-06 14:49:47 -0700882 goto bad;
Christoph Lameter81819f02007-05-06 14:49:36 -0700883 }
884
885 if (!check_valid_pointer(s, page, object)) {
886 object_err(s, page, object, "Freelist Pointer check fails");
Christoph Lameter70d71222007-05-06 14:49:47 -0700887 goto bad;
Christoph Lameter81819f02007-05-06 14:49:36 -0700888 }
889
Christoph Lameterd692ef62008-02-15 23:45:24 -0800890 if (!check_object(s, page, object, 0))
Christoph Lameter81819f02007-05-06 14:49:36 -0700891 goto bad;
Christoph Lameter81819f02007-05-06 14:49:36 -0700892
Christoph Lameter3ec09742007-05-16 22:11:00 -0700893 /* Success perform special debug activities for allocs */
894 if (s->flags & SLAB_STORE_USER)
895 set_track(s, object, TRACK_ALLOC, addr);
896 trace(s, page, object, 1);
897 init_object(s, object, 1);
Christoph Lameter81819f02007-05-06 14:49:36 -0700898 return 1;
Christoph Lameter3ec09742007-05-16 22:11:00 -0700899
Christoph Lameter81819f02007-05-06 14:49:36 -0700900bad:
901 if (PageSlab(page)) {
902 /*
903 * If this is a slab page then lets do the best we can
904 * to avoid issues in the future. Marking all objects
Christoph Lameter672bba32007-05-09 02:32:39 -0700905 * as used avoids touching the remaining objects.
Christoph Lameter81819f02007-05-06 14:49:36 -0700906 */
Christoph Lameter24922682007-07-17 04:03:18 -0700907 slab_fix(s, "Marking all objects used");
Christoph Lameter81819f02007-05-06 14:49:36 -0700908 page->inuse = s->objects;
Christoph Lametera973e9d2008-03-01 13:40:44 -0800909 page->freelist = NULL;
Christoph Lameter81819f02007-05-06 14:49:36 -0700910 }
911 return 0;
912}
913
Christoph Lameter3ec09742007-05-16 22:11:00 -0700914static int free_debug_processing(struct kmem_cache *s, struct page *page,
915 void *object, void *addr)
Christoph Lameter81819f02007-05-06 14:49:36 -0700916{
917 if (!check_slab(s, page))
918 goto fail;
919
920 if (!check_valid_pointer(s, page, object)) {
Christoph Lameter70d71222007-05-06 14:49:47 -0700921 slab_err(s, page, "Invalid object pointer 0x%p", object);
Christoph Lameter81819f02007-05-06 14:49:36 -0700922 goto fail;
923 }
924
925 if (on_freelist(s, page, object)) {
Christoph Lameter24922682007-07-17 04:03:18 -0700926 object_err(s, page, object, "Object already free");
Christoph Lameter81819f02007-05-06 14:49:36 -0700927 goto fail;
928 }
929
930 if (!check_object(s, page, object, 1))
931 return 0;
932
933 if (unlikely(s != page->slab)) {
Ingo Molnar3adbefe2008-02-05 17:57:39 -0800934 if (!PageSlab(page)) {
Christoph Lameter70d71222007-05-06 14:49:47 -0700935 slab_err(s, page, "Attempt to free object(0x%p) "
936 "outside of slab", object);
Ingo Molnar3adbefe2008-02-05 17:57:39 -0800937 } else if (!page->slab) {
Christoph Lameter81819f02007-05-06 14:49:36 -0700938 printk(KERN_ERR
Christoph Lameter70d71222007-05-06 14:49:47 -0700939 "SLUB <none>: no slab for object 0x%p.\n",
Christoph Lameter81819f02007-05-06 14:49:36 -0700940 object);
Christoph Lameter70d71222007-05-06 14:49:47 -0700941 dump_stack();
Pekka Enberg06428782008-01-07 23:20:27 -0800942 } else
Christoph Lameter24922682007-07-17 04:03:18 -0700943 object_err(s, page, object,
944 "page slab pointer corrupt.");
Christoph Lameter81819f02007-05-06 14:49:36 -0700945 goto fail;
946 }
Christoph Lameter3ec09742007-05-16 22:11:00 -0700947
948 /* Special debug activities for freeing objects */
Christoph Lametera973e9d2008-03-01 13:40:44 -0800949 if (!SlabFrozen(page) && !page->freelist)
Christoph Lameter3ec09742007-05-16 22:11:00 -0700950 remove_full(s, page);
951 if (s->flags & SLAB_STORE_USER)
952 set_track(s, object, TRACK_FREE, addr);
953 trace(s, page, object, 0);
954 init_object(s, object, 0);
Christoph Lameter81819f02007-05-06 14:49:36 -0700955 return 1;
Christoph Lameter3ec09742007-05-16 22:11:00 -0700956
Christoph Lameter81819f02007-05-06 14:49:36 -0700957fail:
Christoph Lameter24922682007-07-17 04:03:18 -0700958 slab_fix(s, "Object at 0x%p not freed", object);
Christoph Lameter81819f02007-05-06 14:49:36 -0700959 return 0;
960}
961
Christoph Lameter41ecc552007-05-09 02:32:44 -0700962static int __init setup_slub_debug(char *str)
963{
Christoph Lameterf0630ff2007-07-15 23:38:14 -0700964 slub_debug = DEBUG_DEFAULT_FLAGS;
965 if (*str++ != '=' || !*str)
966 /*
967 * No options specified. Switch on full debugging.
968 */
969 goto out;
Christoph Lameter41ecc552007-05-09 02:32:44 -0700970
971 if (*str == ',')
Christoph Lameterf0630ff2007-07-15 23:38:14 -0700972 /*
973 * No options but restriction on slabs. This means full
974 * debugging for slabs matching a pattern.
975 */
976 goto check_slabs;
977
978 slub_debug = 0;
979 if (*str == '-')
980 /*
981 * Switch off all debugging measures.
982 */
983 goto out;
984
985 /*
986 * Determine which debug features should be switched on
987 */
Pekka Enberg06428782008-01-07 23:20:27 -0800988 for (; *str && *str != ','; str++) {
Christoph Lameterf0630ff2007-07-15 23:38:14 -0700989 switch (tolower(*str)) {
990 case 'f':
991 slub_debug |= SLAB_DEBUG_FREE;
992 break;
993 case 'z':
994 slub_debug |= SLAB_RED_ZONE;
995 break;
996 case 'p':
997 slub_debug |= SLAB_POISON;
998 break;
999 case 'u':
1000 slub_debug |= SLAB_STORE_USER;
1001 break;
1002 case 't':
1003 slub_debug |= SLAB_TRACE;
1004 break;
1005 default:
1006 printk(KERN_ERR "slub_debug option '%c' "
Pekka Enberg06428782008-01-07 23:20:27 -08001007 "unknown. skipped\n", *str);
Christoph Lameterf0630ff2007-07-15 23:38:14 -07001008 }
1009 }
1010
1011check_slabs:
1012 if (*str == ',')
Christoph Lameter41ecc552007-05-09 02:32:44 -07001013 slub_debug_slabs = str + 1;
Christoph Lameterf0630ff2007-07-15 23:38:14 -07001014out:
Christoph Lameter41ecc552007-05-09 02:32:44 -07001015 return 1;
1016}
1017
1018__setup("slub_debug", setup_slub_debug);
1019
Christoph Lameterba0268a2007-09-11 15:24:11 -07001020static unsigned long kmem_cache_flags(unsigned long objsize,
1021 unsigned long flags, const char *name,
Christoph Lameter4ba9b9d2007-10-16 23:25:51 -07001022 void (*ctor)(struct kmem_cache *, void *))
Christoph Lameter41ecc552007-05-09 02:32:44 -07001023{
1024 /*
Christoph Lametere1533622008-02-15 23:45:24 -08001025 * Enable debugging if selected on the kernel commandline.
Christoph Lameter41ecc552007-05-09 02:32:44 -07001026 */
Christoph Lametere1533622008-02-15 23:45:24 -08001027 if (slub_debug && (!slub_debug_slabs ||
1028 strncmp(slub_debug_slabs, name, strlen(slub_debug_slabs)) == 0))
1029 flags |= slub_debug;
Christoph Lameterba0268a2007-09-11 15:24:11 -07001030
1031 return flags;
Christoph Lameter41ecc552007-05-09 02:32:44 -07001032}
1033#else
Christoph Lameter3ec09742007-05-16 22:11:00 -07001034static inline void setup_object_debug(struct kmem_cache *s,
1035 struct page *page, void *object) {}
Christoph Lameter41ecc552007-05-09 02:32:44 -07001036
Christoph Lameter3ec09742007-05-16 22:11:00 -07001037static inline int alloc_debug_processing(struct kmem_cache *s,
1038 struct page *page, void *object, void *addr) { return 0; }
Christoph Lameter41ecc552007-05-09 02:32:44 -07001039
Christoph Lameter3ec09742007-05-16 22:11:00 -07001040static inline int free_debug_processing(struct kmem_cache *s,
1041 struct page *page, void *object, void *addr) { return 0; }
Christoph Lameter41ecc552007-05-09 02:32:44 -07001042
Christoph Lameter41ecc552007-05-09 02:32:44 -07001043static inline int slab_pad_check(struct kmem_cache *s, struct page *page)
1044 { return 1; }
1045static inline int check_object(struct kmem_cache *s, struct page *page,
1046 void *object, int active) { return 1; }
Christoph Lameter3ec09742007-05-16 22:11:00 -07001047static inline void add_full(struct kmem_cache_node *n, struct page *page) {}
Christoph Lameterba0268a2007-09-11 15:24:11 -07001048static inline unsigned long kmem_cache_flags(unsigned long objsize,
1049 unsigned long flags, const char *name,
Christoph Lameter4ba9b9d2007-10-16 23:25:51 -07001050 void (*ctor)(struct kmem_cache *, void *))
Christoph Lameterba0268a2007-09-11 15:24:11 -07001051{
1052 return flags;
1053}
Christoph Lameter41ecc552007-05-09 02:32:44 -07001054#define slub_debug 0
Christoph Lameter0f389ec2008-04-14 18:53:02 +03001055
1056static inline unsigned long slabs_node(struct kmem_cache *s, int node)
1057 { return 0; }
1058static inline void inc_slabs_node(struct kmem_cache *s, int node) {}
1059static inline void dec_slabs_node(struct kmem_cache *s, int node) {}
Christoph Lameter41ecc552007-05-09 02:32:44 -07001060#endif
Christoph Lameter81819f02007-05-06 14:49:36 -07001061/*
1062 * Slab allocation and freeing
1063 */
1064static struct page *allocate_slab(struct kmem_cache *s, gfp_t flags, int node)
1065{
Pekka Enberg06428782008-01-07 23:20:27 -08001066 struct page *page;
Christoph Lameter81819f02007-05-06 14:49:36 -07001067 int pages = 1 << s->order;
1068
Christoph Lameterb7a49f02008-02-14 14:21:32 -08001069 flags |= s->allocflags;
Mel Gormane12ba742007-10-16 01:25:52 -07001070
Christoph Lameter81819f02007-05-06 14:49:36 -07001071 if (node == -1)
1072 page = alloc_pages(flags, s->order);
1073 else
1074 page = alloc_pages_node(node, flags, s->order);
1075
1076 if (!page)
1077 return NULL;
1078
1079 mod_zone_page_state(page_zone(page),
1080 (s->flags & SLAB_RECLAIM_ACCOUNT) ?
1081 NR_SLAB_RECLAIMABLE : NR_SLAB_UNRECLAIMABLE,
1082 pages);
1083
1084 return page;
1085}
1086
1087static void setup_object(struct kmem_cache *s, struct page *page,
1088 void *object)
1089{
Christoph Lameter3ec09742007-05-16 22:11:00 -07001090 setup_object_debug(s, page, object);
Christoph Lameter4f104932007-05-06 14:50:17 -07001091 if (unlikely(s->ctor))
Christoph Lameter4ba9b9d2007-10-16 23:25:51 -07001092 s->ctor(s, object);
Christoph Lameter81819f02007-05-06 14:49:36 -07001093}
1094
1095static struct page *new_slab(struct kmem_cache *s, gfp_t flags, int node)
1096{
1097 struct page *page;
Christoph Lameter81819f02007-05-06 14:49:36 -07001098 void *start;
Christoph Lameter81819f02007-05-06 14:49:36 -07001099 void *last;
1100 void *p;
1101
Christoph Lameter6cb06222007-10-16 01:25:41 -07001102 BUG_ON(flags & GFP_SLAB_BUG_MASK);
Christoph Lameter81819f02007-05-06 14:49:36 -07001103
Christoph Lameter6cb06222007-10-16 01:25:41 -07001104 page = allocate_slab(s,
1105 flags & (GFP_RECLAIM_MASK | GFP_CONSTRAINT_MASK), node);
Christoph Lameter81819f02007-05-06 14:49:36 -07001106 if (!page)
1107 goto out;
1108
Christoph Lameter0f389ec2008-04-14 18:53:02 +03001109 inc_slabs_node(s, page_to_nid(page));
Christoph Lameter81819f02007-05-06 14:49:36 -07001110 page->slab = s;
1111 page->flags |= 1 << PG_slab;
1112 if (s->flags & (SLAB_DEBUG_FREE | SLAB_RED_ZONE | SLAB_POISON |
1113 SLAB_STORE_USER | SLAB_TRACE))
Christoph Lameter35e5d7e2007-05-09 02:32:42 -07001114 SetSlabDebug(page);
Christoph Lameter81819f02007-05-06 14:49:36 -07001115
1116 start = page_address(page);
Christoph Lameter81819f02007-05-06 14:49:36 -07001117
1118 if (unlikely(s->flags & SLAB_POISON))
1119 memset(start, POISON_INUSE, PAGE_SIZE << s->order);
1120
1121 last = start;
Christoph Lameter7656c722007-05-09 02:32:40 -07001122 for_each_object(p, s, start) {
Christoph Lameter81819f02007-05-06 14:49:36 -07001123 setup_object(s, page, last);
1124 set_freepointer(s, last, p);
1125 last = p;
1126 }
1127 setup_object(s, page, last);
Christoph Lametera973e9d2008-03-01 13:40:44 -08001128 set_freepointer(s, last, NULL);
Christoph Lameter81819f02007-05-06 14:49:36 -07001129
1130 page->freelist = start;
1131 page->inuse = 0;
1132out:
Christoph Lameter81819f02007-05-06 14:49:36 -07001133 return page;
1134}
1135
1136static void __free_slab(struct kmem_cache *s, struct page *page)
1137{
1138 int pages = 1 << s->order;
1139
Christoph Lameterc59def92007-05-16 22:10:50 -07001140 if (unlikely(SlabDebug(page))) {
Christoph Lameter81819f02007-05-06 14:49:36 -07001141 void *p;
1142
1143 slab_pad_check(s, page);
Christoph Lametera973e9d2008-03-01 13:40:44 -08001144 for_each_object(p, s, page_address(page))
Christoph Lameter81819f02007-05-06 14:49:36 -07001145 check_object(s, page, p, 0);
Peter Zijlstra2208b762007-07-26 20:54:34 +02001146 ClearSlabDebug(page);
Christoph Lameter81819f02007-05-06 14:49:36 -07001147 }
1148
1149 mod_zone_page_state(page_zone(page),
1150 (s->flags & SLAB_RECLAIM_ACCOUNT) ?
1151 NR_SLAB_RECLAIMABLE : NR_SLAB_UNRECLAIMABLE,
Pekka Enberg06428782008-01-07 23:20:27 -08001152 -pages);
Christoph Lameter81819f02007-05-06 14:49:36 -07001153
Christoph Lameter49bd5222008-04-14 18:52:18 +03001154 __ClearPageSlab(page);
1155 reset_page_mapcount(page);
Christoph Lameter81819f02007-05-06 14:49:36 -07001156 __free_pages(page, s->order);
1157}
1158
1159static void rcu_free_slab(struct rcu_head *h)
1160{
1161 struct page *page;
1162
1163 page = container_of((struct list_head *)h, struct page, lru);
1164 __free_slab(page->slab, page);
1165}
1166
1167static void free_slab(struct kmem_cache *s, struct page *page)
1168{
1169 if (unlikely(s->flags & SLAB_DESTROY_BY_RCU)) {
1170 /*
1171 * RCU free overloads the RCU head over the LRU
1172 */
1173 struct rcu_head *head = (void *)&page->lru;
1174
1175 call_rcu(head, rcu_free_slab);
1176 } else
1177 __free_slab(s, page);
1178}
1179
1180static void discard_slab(struct kmem_cache *s, struct page *page)
1181{
Christoph Lameter0f389ec2008-04-14 18:53:02 +03001182 dec_slabs_node(s, page_to_nid(page));
Christoph Lameter81819f02007-05-06 14:49:36 -07001183 free_slab(s, page);
1184}
1185
1186/*
1187 * Per slab locking using the pagelock
1188 */
1189static __always_inline void slab_lock(struct page *page)
1190{
1191 bit_spin_lock(PG_locked, &page->flags);
1192}
1193
1194static __always_inline void slab_unlock(struct page *page)
1195{
Nick Piggina76d3542008-01-07 23:20:27 -08001196 __bit_spin_unlock(PG_locked, &page->flags);
Christoph Lameter81819f02007-05-06 14:49:36 -07001197}
1198
1199static __always_inline int slab_trylock(struct page *page)
1200{
1201 int rc = 1;
1202
1203 rc = bit_spin_trylock(PG_locked, &page->flags);
1204 return rc;
1205}
1206
1207/*
1208 * Management of partially allocated slabs
1209 */
Christoph Lameter7c2e1322008-01-07 23:20:27 -08001210static void add_partial(struct kmem_cache_node *n,
1211 struct page *page, int tail)
Christoph Lameter81819f02007-05-06 14:49:36 -07001212{
Christoph Lametere95eed52007-05-06 14:49:44 -07001213 spin_lock(&n->list_lock);
1214 n->nr_partial++;
Christoph Lameter7c2e1322008-01-07 23:20:27 -08001215 if (tail)
1216 list_add_tail(&page->lru, &n->partial);
1217 else
1218 list_add(&page->lru, &n->partial);
Christoph Lameter81819f02007-05-06 14:49:36 -07001219 spin_unlock(&n->list_lock);
1220}
1221
1222static void remove_partial(struct kmem_cache *s,
1223 struct page *page)
1224{
1225 struct kmem_cache_node *n = get_node(s, page_to_nid(page));
1226
1227 spin_lock(&n->list_lock);
1228 list_del(&page->lru);
1229 n->nr_partial--;
1230 spin_unlock(&n->list_lock);
1231}
1232
1233/*
Christoph Lameter672bba32007-05-09 02:32:39 -07001234 * Lock slab and remove from the partial list.
Christoph Lameter81819f02007-05-06 14:49:36 -07001235 *
Christoph Lameter672bba32007-05-09 02:32:39 -07001236 * Must hold list_lock.
Christoph Lameter81819f02007-05-06 14:49:36 -07001237 */
Christoph Lameter4b6f0752007-05-16 22:10:53 -07001238static inline int lock_and_freeze_slab(struct kmem_cache_node *n, struct page *page)
Christoph Lameter81819f02007-05-06 14:49:36 -07001239{
1240 if (slab_trylock(page)) {
1241 list_del(&page->lru);
1242 n->nr_partial--;
Christoph Lameter4b6f0752007-05-16 22:10:53 -07001243 SetSlabFrozen(page);
Christoph Lameter81819f02007-05-06 14:49:36 -07001244 return 1;
1245 }
1246 return 0;
1247}
1248
1249/*
Christoph Lameter672bba32007-05-09 02:32:39 -07001250 * Try to allocate a partial slab from a specific node.
Christoph Lameter81819f02007-05-06 14:49:36 -07001251 */
1252static struct page *get_partial_node(struct kmem_cache_node *n)
1253{
1254 struct page *page;
1255
1256 /*
1257 * Racy check. If we mistakenly see no partial slabs then we
1258 * just allocate an empty slab. If we mistakenly try to get a
Christoph Lameter672bba32007-05-09 02:32:39 -07001259 * partial slab and there is none available then get_partials()
1260 * will return NULL.
Christoph Lameter81819f02007-05-06 14:49:36 -07001261 */
1262 if (!n || !n->nr_partial)
1263 return NULL;
1264
1265 spin_lock(&n->list_lock);
1266 list_for_each_entry(page, &n->partial, lru)
Christoph Lameter4b6f0752007-05-16 22:10:53 -07001267 if (lock_and_freeze_slab(n, page))
Christoph Lameter81819f02007-05-06 14:49:36 -07001268 goto out;
1269 page = NULL;
1270out:
1271 spin_unlock(&n->list_lock);
1272 return page;
1273}
1274
1275/*
Christoph Lameter672bba32007-05-09 02:32:39 -07001276 * Get a page from somewhere. Search in increasing NUMA distances.
Christoph Lameter81819f02007-05-06 14:49:36 -07001277 */
1278static struct page *get_any_partial(struct kmem_cache *s, gfp_t flags)
1279{
1280#ifdef CONFIG_NUMA
1281 struct zonelist *zonelist;
Mel Gormandd1a2392008-04-28 02:12:17 -07001282 struct zoneref *z;
Mel Gorman54a6eb52008-04-28 02:12:16 -07001283 struct zone *zone;
1284 enum zone_type high_zoneidx = gfp_zone(flags);
Christoph Lameter81819f02007-05-06 14:49:36 -07001285 struct page *page;
1286
1287 /*
Christoph Lameter672bba32007-05-09 02:32:39 -07001288 * The defrag ratio allows a configuration of the tradeoffs between
1289 * inter node defragmentation and node local allocations. A lower
1290 * defrag_ratio increases the tendency to do local allocations
1291 * instead of attempting to obtain partial slabs from other nodes.
Christoph Lameter81819f02007-05-06 14:49:36 -07001292 *
Christoph Lameter672bba32007-05-09 02:32:39 -07001293 * If the defrag_ratio is set to 0 then kmalloc() always
1294 * returns node local objects. If the ratio is higher then kmalloc()
1295 * may return off node objects because partial slabs are obtained
1296 * from other nodes and filled up.
Christoph Lameter81819f02007-05-06 14:49:36 -07001297 *
Christoph Lameter6446faa2008-02-15 23:45:26 -08001298 * If /sys/kernel/slab/xx/defrag_ratio is set to 100 (which makes
Christoph Lameter672bba32007-05-09 02:32:39 -07001299 * defrag_ratio = 1000) then every (well almost) allocation will
1300 * first attempt to defrag slab caches on other nodes. This means
1301 * scanning over all nodes to look for partial slabs which may be
1302 * expensive if we do it every time we are trying to find a slab
1303 * with available objects.
Christoph Lameter81819f02007-05-06 14:49:36 -07001304 */
Christoph Lameter98246012008-01-07 23:20:26 -08001305 if (!s->remote_node_defrag_ratio ||
1306 get_cycles() % 1024 > s->remote_node_defrag_ratio)
Christoph Lameter81819f02007-05-06 14:49:36 -07001307 return NULL;
1308
Mel Gorman0e884602008-04-28 02:12:14 -07001309 zonelist = node_zonelist(slab_node(current->mempolicy), flags);
Mel Gorman54a6eb52008-04-28 02:12:16 -07001310 for_each_zone_zonelist(zone, z, zonelist, high_zoneidx) {
Christoph Lameter81819f02007-05-06 14:49:36 -07001311 struct kmem_cache_node *n;
1312
Mel Gorman54a6eb52008-04-28 02:12:16 -07001313 n = get_node(s, zone_to_nid(zone));
Christoph Lameter81819f02007-05-06 14:49:36 -07001314
Mel Gorman54a6eb52008-04-28 02:12:16 -07001315 if (n && cpuset_zone_allowed_hardwall(zone, flags) &&
Christoph Lametere95eed52007-05-06 14:49:44 -07001316 n->nr_partial > MIN_PARTIAL) {
Christoph Lameter81819f02007-05-06 14:49:36 -07001317 page = get_partial_node(n);
1318 if (page)
1319 return page;
1320 }
1321 }
1322#endif
1323 return NULL;
1324}
1325
1326/*
1327 * Get a partial page, lock it and return it.
1328 */
1329static struct page *get_partial(struct kmem_cache *s, gfp_t flags, int node)
1330{
1331 struct page *page;
1332 int searchnode = (node == -1) ? numa_node_id() : node;
1333
1334 page = get_partial_node(get_node(s, searchnode));
1335 if (page || (flags & __GFP_THISNODE))
1336 return page;
1337
1338 return get_any_partial(s, flags);
1339}
1340
1341/*
1342 * Move a page back to the lists.
1343 *
1344 * Must be called with the slab lock held.
1345 *
1346 * On exit the slab lock will have been dropped.
1347 */
Christoph Lameter7c2e1322008-01-07 23:20:27 -08001348static void unfreeze_slab(struct kmem_cache *s, struct page *page, int tail)
Christoph Lameter81819f02007-05-06 14:49:36 -07001349{
Christoph Lametere95eed52007-05-06 14:49:44 -07001350 struct kmem_cache_node *n = get_node(s, page_to_nid(page));
Christoph Lameter8ff12cf2008-02-07 17:47:41 -08001351 struct kmem_cache_cpu *c = get_cpu_slab(s, smp_processor_id());
Christoph Lametere95eed52007-05-06 14:49:44 -07001352
Christoph Lameter4b6f0752007-05-16 22:10:53 -07001353 ClearSlabFrozen(page);
Christoph Lameter81819f02007-05-06 14:49:36 -07001354 if (page->inuse) {
Christoph Lametere95eed52007-05-06 14:49:44 -07001355
Christoph Lametera973e9d2008-03-01 13:40:44 -08001356 if (page->freelist) {
Christoph Lameter7c2e1322008-01-07 23:20:27 -08001357 add_partial(n, page, tail);
Christoph Lameter8ff12cf2008-02-07 17:47:41 -08001358 stat(c, tail ? DEACTIVATE_TO_TAIL : DEACTIVATE_TO_HEAD);
1359 } else {
1360 stat(c, DEACTIVATE_FULL);
1361 if (SlabDebug(page) && (s->flags & SLAB_STORE_USER))
1362 add_full(n, page);
1363 }
Christoph Lameter81819f02007-05-06 14:49:36 -07001364 slab_unlock(page);
1365 } else {
Christoph Lameter8ff12cf2008-02-07 17:47:41 -08001366 stat(c, DEACTIVATE_EMPTY);
Christoph Lametere95eed52007-05-06 14:49:44 -07001367 if (n->nr_partial < MIN_PARTIAL) {
1368 /*
Christoph Lameter672bba32007-05-09 02:32:39 -07001369 * Adding an empty slab to the partial slabs in order
1370 * to avoid page allocator overhead. This slab needs
1371 * to come after the other slabs with objects in
Christoph Lameter6446faa2008-02-15 23:45:26 -08001372 * so that the others get filled first. That way the
1373 * size of the partial list stays small.
1374 *
1375 * kmem_cache_shrink can reclaim any empty slabs from the
1376 * partial list.
Christoph Lametere95eed52007-05-06 14:49:44 -07001377 */
Christoph Lameter7c2e1322008-01-07 23:20:27 -08001378 add_partial(n, page, 1);
Christoph Lametere95eed52007-05-06 14:49:44 -07001379 slab_unlock(page);
1380 } else {
1381 slab_unlock(page);
Christoph Lameter8ff12cf2008-02-07 17:47:41 -08001382 stat(get_cpu_slab(s, raw_smp_processor_id()), FREE_SLAB);
Christoph Lametere95eed52007-05-06 14:49:44 -07001383 discard_slab(s, page);
1384 }
Christoph Lameter81819f02007-05-06 14:49:36 -07001385 }
1386}
1387
1388/*
1389 * Remove the cpu slab
1390 */
Christoph Lameterdfb4f092007-10-16 01:26:05 -07001391static void deactivate_slab(struct kmem_cache *s, struct kmem_cache_cpu *c)
Christoph Lameter81819f02007-05-06 14:49:36 -07001392{
Christoph Lameterdfb4f092007-10-16 01:26:05 -07001393 struct page *page = c->page;
Christoph Lameter7c2e1322008-01-07 23:20:27 -08001394 int tail = 1;
Christoph Lameter8ff12cf2008-02-07 17:47:41 -08001395
Christoph Lameterb773ad72008-03-04 11:10:17 -08001396 if (page->freelist)
Christoph Lameter8ff12cf2008-02-07 17:47:41 -08001397 stat(c, DEACTIVATE_REMOTE_FREES);
Christoph Lameter894b8782007-05-10 03:15:16 -07001398 /*
Christoph Lameter6446faa2008-02-15 23:45:26 -08001399 * Merge cpu freelist into slab freelist. Typically we get here
Christoph Lameter894b8782007-05-10 03:15:16 -07001400 * because both freelists are empty. So this is unlikely
1401 * to occur.
1402 */
Christoph Lametera973e9d2008-03-01 13:40:44 -08001403 while (unlikely(c->freelist)) {
Christoph Lameter894b8782007-05-10 03:15:16 -07001404 void **object;
1405
Christoph Lameter7c2e1322008-01-07 23:20:27 -08001406 tail = 0; /* Hot objects. Put the slab first */
1407
Christoph Lameter894b8782007-05-10 03:15:16 -07001408 /* Retrieve object from cpu_freelist */
Christoph Lameterdfb4f092007-10-16 01:26:05 -07001409 object = c->freelist;
Christoph Lameterb3fba8d2007-10-16 01:26:06 -07001410 c->freelist = c->freelist[c->offset];
Christoph Lameter894b8782007-05-10 03:15:16 -07001411
1412 /* And put onto the regular freelist */
Christoph Lameterb3fba8d2007-10-16 01:26:06 -07001413 object[c->offset] = page->freelist;
Christoph Lameter894b8782007-05-10 03:15:16 -07001414 page->freelist = object;
1415 page->inuse--;
1416 }
Christoph Lameterdfb4f092007-10-16 01:26:05 -07001417 c->page = NULL;
Christoph Lameter7c2e1322008-01-07 23:20:27 -08001418 unfreeze_slab(s, page, tail);
Christoph Lameter81819f02007-05-06 14:49:36 -07001419}
1420
Christoph Lameterdfb4f092007-10-16 01:26:05 -07001421static inline void flush_slab(struct kmem_cache *s, struct kmem_cache_cpu *c)
Christoph Lameter81819f02007-05-06 14:49:36 -07001422{
Christoph Lameter8ff12cf2008-02-07 17:47:41 -08001423 stat(c, CPUSLAB_FLUSH);
Christoph Lameterdfb4f092007-10-16 01:26:05 -07001424 slab_lock(c->page);
1425 deactivate_slab(s, c);
Christoph Lameter81819f02007-05-06 14:49:36 -07001426}
1427
1428/*
1429 * Flush cpu slab.
Christoph Lameter6446faa2008-02-15 23:45:26 -08001430 *
Christoph Lameter81819f02007-05-06 14:49:36 -07001431 * Called from IPI handler with interrupts disabled.
1432 */
Christoph Lameter0c710012007-07-17 04:03:24 -07001433static inline void __flush_cpu_slab(struct kmem_cache *s, int cpu)
Christoph Lameter81819f02007-05-06 14:49:36 -07001434{
Christoph Lameterdfb4f092007-10-16 01:26:05 -07001435 struct kmem_cache_cpu *c = get_cpu_slab(s, cpu);
Christoph Lameter81819f02007-05-06 14:49:36 -07001436
Christoph Lameterdfb4f092007-10-16 01:26:05 -07001437 if (likely(c && c->page))
1438 flush_slab(s, c);
Christoph Lameter81819f02007-05-06 14:49:36 -07001439}
1440
1441static void flush_cpu_slab(void *d)
1442{
1443 struct kmem_cache *s = d;
Christoph Lameter81819f02007-05-06 14:49:36 -07001444
Christoph Lameterdfb4f092007-10-16 01:26:05 -07001445 __flush_cpu_slab(s, smp_processor_id());
Christoph Lameter81819f02007-05-06 14:49:36 -07001446}
1447
1448static void flush_all(struct kmem_cache *s)
1449{
1450#ifdef CONFIG_SMP
1451 on_each_cpu(flush_cpu_slab, s, 1, 1);
1452#else
1453 unsigned long flags;
1454
1455 local_irq_save(flags);
1456 flush_cpu_slab(s);
1457 local_irq_restore(flags);
1458#endif
1459}
1460
1461/*
Christoph Lameterdfb4f092007-10-16 01:26:05 -07001462 * Check if the objects in a per cpu structure fit numa
1463 * locality expectations.
1464 */
1465static inline int node_match(struct kmem_cache_cpu *c, int node)
1466{
1467#ifdef CONFIG_NUMA
1468 if (node != -1 && c->node != node)
1469 return 0;
1470#endif
1471 return 1;
1472}
1473
1474/*
Christoph Lameter894b8782007-05-10 03:15:16 -07001475 * Slow path. The lockless freelist is empty or we need to perform
1476 * debugging duties.
Christoph Lameter81819f02007-05-06 14:49:36 -07001477 *
Christoph Lameter894b8782007-05-10 03:15:16 -07001478 * Interrupts are disabled.
Christoph Lameter81819f02007-05-06 14:49:36 -07001479 *
Christoph Lameter894b8782007-05-10 03:15:16 -07001480 * Processing is still very fast if new objects have been freed to the
1481 * regular freelist. In that case we simply take over the regular freelist
1482 * as the lockless freelist and zap the regular freelist.
Christoph Lameter81819f02007-05-06 14:49:36 -07001483 *
Christoph Lameter894b8782007-05-10 03:15:16 -07001484 * If that is not working then we fall back to the partial lists. We take the
1485 * first element of the freelist as the object to allocate now and move the
1486 * rest of the freelist to the lockless freelist.
1487 *
1488 * And if we were unable to get a new slab from the partial slab lists then
Christoph Lameter6446faa2008-02-15 23:45:26 -08001489 * we need to allocate a new slab. This is the slowest path since it involves
1490 * a call to the page allocator and the setup of a new slab.
Christoph Lameter81819f02007-05-06 14:49:36 -07001491 */
Christoph Lameter894b8782007-05-10 03:15:16 -07001492static void *__slab_alloc(struct kmem_cache *s,
Christoph Lameterdfb4f092007-10-16 01:26:05 -07001493 gfp_t gfpflags, int node, void *addr, struct kmem_cache_cpu *c)
Christoph Lameter81819f02007-05-06 14:49:36 -07001494{
Christoph Lameter81819f02007-05-06 14:49:36 -07001495 void **object;
Christoph Lameterdfb4f092007-10-16 01:26:05 -07001496 struct page *new;
Christoph Lameter81819f02007-05-06 14:49:36 -07001497
Linus Torvaldse72e9c22008-03-27 20:56:33 -07001498 /* We handle __GFP_ZERO in the caller */
1499 gfpflags &= ~__GFP_ZERO;
1500
Christoph Lameterdfb4f092007-10-16 01:26:05 -07001501 if (!c->page)
Christoph Lameter81819f02007-05-06 14:49:36 -07001502 goto new_slab;
1503
Christoph Lameterdfb4f092007-10-16 01:26:05 -07001504 slab_lock(c->page);
1505 if (unlikely(!node_match(c, node)))
Christoph Lameter81819f02007-05-06 14:49:36 -07001506 goto another_slab;
Christoph Lameter6446faa2008-02-15 23:45:26 -08001507
Christoph Lameter8ff12cf2008-02-07 17:47:41 -08001508 stat(c, ALLOC_REFILL);
Christoph Lameter6446faa2008-02-15 23:45:26 -08001509
Christoph Lameter894b8782007-05-10 03:15:16 -07001510load_freelist:
Christoph Lameterdfb4f092007-10-16 01:26:05 -07001511 object = c->page->freelist;
Christoph Lametera973e9d2008-03-01 13:40:44 -08001512 if (unlikely(!object))
Christoph Lameter81819f02007-05-06 14:49:36 -07001513 goto another_slab;
Christoph Lameterdfb4f092007-10-16 01:26:05 -07001514 if (unlikely(SlabDebug(c->page)))
Christoph Lameter81819f02007-05-06 14:49:36 -07001515 goto debug;
1516
Christoph Lameterb3fba8d2007-10-16 01:26:06 -07001517 c->freelist = object[c->offset];
Christoph Lameterdfb4f092007-10-16 01:26:05 -07001518 c->page->inuse = s->objects;
Christoph Lametera973e9d2008-03-01 13:40:44 -08001519 c->page->freelist = NULL;
Christoph Lameterdfb4f092007-10-16 01:26:05 -07001520 c->node = page_to_nid(c->page);
Christoph Lameter1f842602008-01-07 23:20:30 -08001521unlock_out:
Christoph Lameterdfb4f092007-10-16 01:26:05 -07001522 slab_unlock(c->page);
Christoph Lameter8ff12cf2008-02-07 17:47:41 -08001523 stat(c, ALLOC_SLOWPATH);
Christoph Lameter81819f02007-05-06 14:49:36 -07001524 return object;
1525
1526another_slab:
Christoph Lameterdfb4f092007-10-16 01:26:05 -07001527 deactivate_slab(s, c);
Christoph Lameter81819f02007-05-06 14:49:36 -07001528
1529new_slab:
Christoph Lameterdfb4f092007-10-16 01:26:05 -07001530 new = get_partial(s, gfpflags, node);
1531 if (new) {
1532 c->page = new;
Christoph Lameter8ff12cf2008-02-07 17:47:41 -08001533 stat(c, ALLOC_FROM_PARTIAL);
Christoph Lameter894b8782007-05-10 03:15:16 -07001534 goto load_freelist;
Christoph Lameter81819f02007-05-06 14:49:36 -07001535 }
1536
Christoph Lameterb811c202007-10-16 23:25:51 -07001537 if (gfpflags & __GFP_WAIT)
1538 local_irq_enable();
1539
Christoph Lameterdfb4f092007-10-16 01:26:05 -07001540 new = new_slab(s, gfpflags, node);
Christoph Lameterb811c202007-10-16 23:25:51 -07001541
1542 if (gfpflags & __GFP_WAIT)
1543 local_irq_disable();
1544
Christoph Lameterdfb4f092007-10-16 01:26:05 -07001545 if (new) {
1546 c = get_cpu_slab(s, smp_processor_id());
Christoph Lameter8ff12cf2008-02-07 17:47:41 -08001547 stat(c, ALLOC_SLAB);
Christoph Lameter05aa3452007-11-05 11:31:58 -08001548 if (c->page)
Christoph Lameterdfb4f092007-10-16 01:26:05 -07001549 flush_slab(s, c);
Christoph Lameterdfb4f092007-10-16 01:26:05 -07001550 slab_lock(new);
1551 SetSlabFrozen(new);
1552 c->page = new;
Christoph Lameter4b6f0752007-05-16 22:10:53 -07001553 goto load_freelist;
Christoph Lameter81819f02007-05-06 14:49:36 -07001554 }
Linus Torvalds00e962c2008-02-19 09:08:49 -08001555
Christoph Lameter71c7a062008-02-14 14:28:01 -08001556 /*
1557 * No memory available.
1558 *
1559 * If the slab uses higher order allocs but the object is
1560 * smaller than a page size then we can fallback in emergencies
1561 * to the page allocator via kmalloc_large. The page allocator may
1562 * have failed to obtain a higher order page and we can try to
1563 * allocate a single page if the object fits into a single page.
1564 * That is only possible if certain conditions are met that are being
1565 * checked when a slab is created.
1566 */
Christoph Lametercaeab082008-03-12 23:57:49 -07001567 if (!(gfpflags & __GFP_NORETRY) &&
1568 (s->flags & __PAGE_ALLOC_FALLBACK)) {
1569 if (gfpflags & __GFP_WAIT)
1570 local_irq_enable();
1571 object = kmalloc_large(s->objsize, gfpflags);
1572 if (gfpflags & __GFP_WAIT)
1573 local_irq_disable();
1574 return object;
1575 }
Christoph Lameter71c7a062008-02-14 14:28:01 -08001576 return NULL;
Christoph Lameter81819f02007-05-06 14:49:36 -07001577debug:
Christoph Lameterdfb4f092007-10-16 01:26:05 -07001578 if (!alloc_debug_processing(s, c->page, object, addr))
Christoph Lameter81819f02007-05-06 14:49:36 -07001579 goto another_slab;
Christoph Lameter894b8782007-05-10 03:15:16 -07001580
Christoph Lameterdfb4f092007-10-16 01:26:05 -07001581 c->page->inuse++;
Christoph Lameterb3fba8d2007-10-16 01:26:06 -07001582 c->page->freelist = object[c->offset];
Christoph Lameteree3c72a2007-10-16 01:26:07 -07001583 c->node = -1;
Christoph Lameter1f842602008-01-07 23:20:30 -08001584 goto unlock_out;
Christoph Lameter894b8782007-05-10 03:15:16 -07001585}
1586
1587/*
1588 * Inlined fastpath so that allocation functions (kmalloc, kmem_cache_alloc)
1589 * have the fastpath folded into their functions. So no function call
1590 * overhead for requests that can be satisfied on the fastpath.
1591 *
1592 * The fastpath works by first checking if the lockless freelist can be used.
1593 * If not then __slab_alloc is called for slow processing.
1594 *
1595 * Otherwise we can simply pick the next object from the lockless free list.
1596 */
Pekka Enberg06428782008-01-07 23:20:27 -08001597static __always_inline void *slab_alloc(struct kmem_cache *s,
Christoph Lameterce15fea2007-07-17 04:03:28 -07001598 gfp_t gfpflags, int node, void *addr)
Christoph Lameter894b8782007-05-10 03:15:16 -07001599{
Christoph Lameter894b8782007-05-10 03:15:16 -07001600 void **object;
Christoph Lameterdfb4f092007-10-16 01:26:05 -07001601 struct kmem_cache_cpu *c;
Christoph Lameter1f842602008-01-07 23:20:30 -08001602 unsigned long flags;
1603
Christoph Lameter894b8782007-05-10 03:15:16 -07001604 local_irq_save(flags);
Christoph Lameterdfb4f092007-10-16 01:26:05 -07001605 c = get_cpu_slab(s, smp_processor_id());
Christoph Lametera973e9d2008-03-01 13:40:44 -08001606 if (unlikely(!c->freelist || !node_match(c, node)))
Christoph Lameter894b8782007-05-10 03:15:16 -07001607
Christoph Lameterdfb4f092007-10-16 01:26:05 -07001608 object = __slab_alloc(s, gfpflags, node, addr, c);
Christoph Lameter894b8782007-05-10 03:15:16 -07001609
1610 else {
Christoph Lameterdfb4f092007-10-16 01:26:05 -07001611 object = c->freelist;
Christoph Lameterb3fba8d2007-10-16 01:26:06 -07001612 c->freelist = object[c->offset];
Christoph Lameter8ff12cf2008-02-07 17:47:41 -08001613 stat(c, ALLOC_FASTPATH);
Christoph Lameter894b8782007-05-10 03:15:16 -07001614 }
1615 local_irq_restore(flags);
Christoph Lameterd07dbea2007-07-17 04:03:23 -07001616
1617 if (unlikely((gfpflags & __GFP_ZERO) && object))
Christoph Lameter42a9fdb2007-10-16 01:26:09 -07001618 memset(object, 0, c->objsize);
Christoph Lameterd07dbea2007-07-17 04:03:23 -07001619
Christoph Lameter894b8782007-05-10 03:15:16 -07001620 return object;
Christoph Lameter81819f02007-05-06 14:49:36 -07001621}
1622
1623void *kmem_cache_alloc(struct kmem_cache *s, gfp_t gfpflags)
1624{
Christoph Lameterce15fea2007-07-17 04:03:28 -07001625 return slab_alloc(s, gfpflags, -1, __builtin_return_address(0));
Christoph Lameter81819f02007-05-06 14:49:36 -07001626}
1627EXPORT_SYMBOL(kmem_cache_alloc);
1628
1629#ifdef CONFIG_NUMA
1630void *kmem_cache_alloc_node(struct kmem_cache *s, gfp_t gfpflags, int node)
1631{
Christoph Lameterce15fea2007-07-17 04:03:28 -07001632 return slab_alloc(s, gfpflags, node, __builtin_return_address(0));
Christoph Lameter81819f02007-05-06 14:49:36 -07001633}
1634EXPORT_SYMBOL(kmem_cache_alloc_node);
1635#endif
1636
1637/*
Christoph Lameter894b8782007-05-10 03:15:16 -07001638 * Slow patch handling. This may still be called frequently since objects
1639 * have a longer lifetime than the cpu slabs in most processing loads.
Christoph Lameter81819f02007-05-06 14:49:36 -07001640 *
Christoph Lameter894b8782007-05-10 03:15:16 -07001641 * So we still attempt to reduce cache line usage. Just take the slab
1642 * lock and free the item. If there is no additional partial page
1643 * handling required then we can return immediately.
Christoph Lameter81819f02007-05-06 14:49:36 -07001644 */
Christoph Lameter894b8782007-05-10 03:15:16 -07001645static void __slab_free(struct kmem_cache *s, struct page *page,
Christoph Lameterb3fba8d2007-10-16 01:26:06 -07001646 void *x, void *addr, unsigned int offset)
Christoph Lameter81819f02007-05-06 14:49:36 -07001647{
1648 void *prior;
1649 void **object = (void *)x;
Christoph Lameter8ff12cf2008-02-07 17:47:41 -08001650 struct kmem_cache_cpu *c;
Christoph Lameter81819f02007-05-06 14:49:36 -07001651
Christoph Lameter8ff12cf2008-02-07 17:47:41 -08001652 c = get_cpu_slab(s, raw_smp_processor_id());
1653 stat(c, FREE_SLOWPATH);
Christoph Lameter81819f02007-05-06 14:49:36 -07001654 slab_lock(page);
1655
Christoph Lameter35e5d7e2007-05-09 02:32:42 -07001656 if (unlikely(SlabDebug(page)))
Christoph Lameter81819f02007-05-06 14:49:36 -07001657 goto debug;
Christoph Lameter6446faa2008-02-15 23:45:26 -08001658
Christoph Lameter81819f02007-05-06 14:49:36 -07001659checks_ok:
Christoph Lameterb3fba8d2007-10-16 01:26:06 -07001660 prior = object[offset] = page->freelist;
Christoph Lameter81819f02007-05-06 14:49:36 -07001661 page->freelist = object;
1662 page->inuse--;
1663
Christoph Lameter8ff12cf2008-02-07 17:47:41 -08001664 if (unlikely(SlabFrozen(page))) {
1665 stat(c, FREE_FROZEN);
Christoph Lameter81819f02007-05-06 14:49:36 -07001666 goto out_unlock;
Christoph Lameter8ff12cf2008-02-07 17:47:41 -08001667 }
Christoph Lameter81819f02007-05-06 14:49:36 -07001668
1669 if (unlikely(!page->inuse))
1670 goto slab_empty;
1671
1672 /*
Christoph Lameter6446faa2008-02-15 23:45:26 -08001673 * Objects left in the slab. If it was not on the partial list before
Christoph Lameter81819f02007-05-06 14:49:36 -07001674 * then add it.
1675 */
Christoph Lametera973e9d2008-03-01 13:40:44 -08001676 if (unlikely(!prior)) {
Christoph Lameter7c2e1322008-01-07 23:20:27 -08001677 add_partial(get_node(s, page_to_nid(page)), page, 1);
Christoph Lameter8ff12cf2008-02-07 17:47:41 -08001678 stat(c, FREE_ADD_PARTIAL);
1679 }
Christoph Lameter81819f02007-05-06 14:49:36 -07001680
1681out_unlock:
1682 slab_unlock(page);
Christoph Lameter81819f02007-05-06 14:49:36 -07001683 return;
1684
1685slab_empty:
Christoph Lametera973e9d2008-03-01 13:40:44 -08001686 if (prior) {
Christoph Lameter81819f02007-05-06 14:49:36 -07001687 /*
Christoph Lameter672bba32007-05-09 02:32:39 -07001688 * Slab still on the partial list.
Christoph Lameter81819f02007-05-06 14:49:36 -07001689 */
1690 remove_partial(s, page);
Christoph Lameter8ff12cf2008-02-07 17:47:41 -08001691 stat(c, FREE_REMOVE_PARTIAL);
1692 }
Christoph Lameter81819f02007-05-06 14:49:36 -07001693 slab_unlock(page);
Christoph Lameter8ff12cf2008-02-07 17:47:41 -08001694 stat(c, FREE_SLAB);
Christoph Lameter81819f02007-05-06 14:49:36 -07001695 discard_slab(s, page);
Christoph Lameter81819f02007-05-06 14:49:36 -07001696 return;
1697
1698debug:
Christoph Lameter3ec09742007-05-16 22:11:00 -07001699 if (!free_debug_processing(s, page, x, addr))
Christoph Lameter77c5e2d2007-05-06 14:49:42 -07001700 goto out_unlock;
Christoph Lameter77c5e2d2007-05-06 14:49:42 -07001701 goto checks_ok;
Christoph Lameter81819f02007-05-06 14:49:36 -07001702}
1703
Christoph Lameter894b8782007-05-10 03:15:16 -07001704/*
1705 * Fastpath with forced inlining to produce a kfree and kmem_cache_free that
1706 * can perform fastpath freeing without additional function calls.
1707 *
1708 * The fastpath is only possible if we are freeing to the current cpu slab
1709 * of this processor. This typically the case if we have just allocated
1710 * the item before.
1711 *
1712 * If fastpath is not possible then fall back to __slab_free where we deal
1713 * with all sorts of special processing.
1714 */
Pekka Enberg06428782008-01-07 23:20:27 -08001715static __always_inline void slab_free(struct kmem_cache *s,
Christoph Lameter894b8782007-05-10 03:15:16 -07001716 struct page *page, void *x, void *addr)
1717{
1718 void **object = (void *)x;
Christoph Lameterdfb4f092007-10-16 01:26:05 -07001719 struct kmem_cache_cpu *c;
Christoph Lameter1f842602008-01-07 23:20:30 -08001720 unsigned long flags;
1721
Christoph Lameter894b8782007-05-10 03:15:16 -07001722 local_irq_save(flags);
Christoph Lameterdfb4f092007-10-16 01:26:05 -07001723 c = get_cpu_slab(s, smp_processor_id());
Christoph Lameter27d9e4e2008-02-15 23:45:25 -08001724 debug_check_no_locks_freed(object, c->objsize);
Christoph Lameteree3c72a2007-10-16 01:26:07 -07001725 if (likely(page == c->page && c->node >= 0)) {
Christoph Lameterb3fba8d2007-10-16 01:26:06 -07001726 object[c->offset] = c->freelist;
Christoph Lameterdfb4f092007-10-16 01:26:05 -07001727 c->freelist = object;
Christoph Lameter8ff12cf2008-02-07 17:47:41 -08001728 stat(c, FREE_FASTPATH);
Christoph Lameter894b8782007-05-10 03:15:16 -07001729 } else
Christoph Lameterb3fba8d2007-10-16 01:26:06 -07001730 __slab_free(s, page, x, addr, c->offset);
Christoph Lameter894b8782007-05-10 03:15:16 -07001731
1732 local_irq_restore(flags);
1733}
1734
Christoph Lameter81819f02007-05-06 14:49:36 -07001735void kmem_cache_free(struct kmem_cache *s, void *x)
1736{
Christoph Lameter77c5e2d2007-05-06 14:49:42 -07001737 struct page *page;
Christoph Lameter81819f02007-05-06 14:49:36 -07001738
Christoph Lameterb49af682007-05-06 14:49:41 -07001739 page = virt_to_head_page(x);
Christoph Lameter81819f02007-05-06 14:49:36 -07001740
Christoph Lameter77c5e2d2007-05-06 14:49:42 -07001741 slab_free(s, page, x, __builtin_return_address(0));
Christoph Lameter81819f02007-05-06 14:49:36 -07001742}
1743EXPORT_SYMBOL(kmem_cache_free);
1744
1745/* Figure out on which slab object the object resides */
1746static struct page *get_object_page(const void *x)
1747{
Christoph Lameterb49af682007-05-06 14:49:41 -07001748 struct page *page = virt_to_head_page(x);
Christoph Lameter81819f02007-05-06 14:49:36 -07001749
1750 if (!PageSlab(page))
1751 return NULL;
1752
1753 return page;
1754}
1755
1756/*
Christoph Lameter672bba32007-05-09 02:32:39 -07001757 * Object placement in a slab is made very easy because we always start at
1758 * offset 0. If we tune the size of the object to the alignment then we can
1759 * get the required alignment by putting one properly sized object after
1760 * another.
Christoph Lameter81819f02007-05-06 14:49:36 -07001761 *
1762 * Notice that the allocation order determines the sizes of the per cpu
1763 * caches. Each processor has always one slab available for allocations.
1764 * Increasing the allocation order reduces the number of times that slabs
Christoph Lameter672bba32007-05-09 02:32:39 -07001765 * must be moved on and off the partial lists and is therefore a factor in
Christoph Lameter81819f02007-05-06 14:49:36 -07001766 * locking overhead.
Christoph Lameter81819f02007-05-06 14:49:36 -07001767 */
1768
1769/*
1770 * Mininum / Maximum order of slab pages. This influences locking overhead
1771 * and slab fragmentation. A higher order reduces the number of partial slabs
1772 * and increases the number of allocations possible without having to
1773 * take the list_lock.
1774 */
1775static int slub_min_order;
1776static int slub_max_order = DEFAULT_MAX_ORDER;
Christoph Lameter81819f02007-05-06 14:49:36 -07001777static int slub_min_objects = DEFAULT_MIN_OBJECTS;
1778
1779/*
1780 * Merge control. If this is set then no merging of slab caches will occur.
Christoph Lameter672bba32007-05-09 02:32:39 -07001781 * (Could be removed. This was introduced to pacify the merge skeptics.)
Christoph Lameter81819f02007-05-06 14:49:36 -07001782 */
1783static int slub_nomerge;
1784
1785/*
Christoph Lameter81819f02007-05-06 14:49:36 -07001786 * Calculate the order of allocation given an slab object size.
1787 *
Christoph Lameter672bba32007-05-09 02:32:39 -07001788 * The order of allocation has significant impact on performance and other
1789 * system components. Generally order 0 allocations should be preferred since
1790 * order 0 does not cause fragmentation in the page allocator. Larger objects
1791 * be problematic to put into order 0 slabs because there may be too much
1792 * unused space left. We go to a higher order if more than 1/8th of the slab
1793 * would be wasted.
Christoph Lameter81819f02007-05-06 14:49:36 -07001794 *
Christoph Lameter672bba32007-05-09 02:32:39 -07001795 * In order to reach satisfactory performance we must ensure that a minimum
1796 * number of objects is in one slab. Otherwise we may generate too much
1797 * activity on the partial lists which requires taking the list_lock. This is
1798 * less a concern for large slabs though which are rarely used.
Christoph Lameter81819f02007-05-06 14:49:36 -07001799 *
Christoph Lameter672bba32007-05-09 02:32:39 -07001800 * slub_max_order specifies the order where we begin to stop considering the
1801 * number of objects in a slab as critical. If we reach slub_max_order then
1802 * we try to keep the page order as low as possible. So we accept more waste
1803 * of space in favor of a small page order.
1804 *
1805 * Higher order allocations also allow the placement of more objects in a
1806 * slab and thereby reduce object handling overhead. If the user has
1807 * requested a higher mininum order then we start with that one instead of
1808 * the smallest order which will fit the object.
Christoph Lameter81819f02007-05-06 14:49:36 -07001809 */
Christoph Lameter5e6d4442007-05-09 02:32:46 -07001810static inline int slab_order(int size, int min_objects,
1811 int max_order, int fract_leftover)
Christoph Lameter81819f02007-05-06 14:49:36 -07001812{
1813 int order;
1814 int rem;
Christoph Lameter6300ea72007-07-17 04:03:20 -07001815 int min_order = slub_min_order;
Christoph Lameter81819f02007-05-06 14:49:36 -07001816
Christoph Lameter6300ea72007-07-17 04:03:20 -07001817 for (order = max(min_order,
Christoph Lameter5e6d4442007-05-09 02:32:46 -07001818 fls(min_objects * size - 1) - PAGE_SHIFT);
1819 order <= max_order; order++) {
1820
Christoph Lameter81819f02007-05-06 14:49:36 -07001821 unsigned long slab_size = PAGE_SIZE << order;
1822
Christoph Lameter5e6d4442007-05-09 02:32:46 -07001823 if (slab_size < min_objects * size)
Christoph Lameter81819f02007-05-06 14:49:36 -07001824 continue;
1825
Christoph Lameter81819f02007-05-06 14:49:36 -07001826 rem = slab_size % size;
1827
Christoph Lameter5e6d4442007-05-09 02:32:46 -07001828 if (rem <= slab_size / fract_leftover)
Christoph Lameter81819f02007-05-06 14:49:36 -07001829 break;
1830
1831 }
Christoph Lameter672bba32007-05-09 02:32:39 -07001832
Christoph Lameter81819f02007-05-06 14:49:36 -07001833 return order;
1834}
1835
Christoph Lameter5e6d4442007-05-09 02:32:46 -07001836static inline int calculate_order(int size)
1837{
1838 int order;
1839 int min_objects;
1840 int fraction;
1841
1842 /*
1843 * Attempt to find best configuration for a slab. This
1844 * works by first attempting to generate a layout with
1845 * the best configuration and backing off gradually.
1846 *
1847 * First we reduce the acceptable waste in a slab. Then
1848 * we reduce the minimum objects required in a slab.
1849 */
1850 min_objects = slub_min_objects;
1851 while (min_objects > 1) {
1852 fraction = 8;
1853 while (fraction >= 4) {
1854 order = slab_order(size, min_objects,
1855 slub_max_order, fraction);
1856 if (order <= slub_max_order)
1857 return order;
1858 fraction /= 2;
1859 }
1860 min_objects /= 2;
1861 }
1862
1863 /*
1864 * We were unable to place multiple objects in a slab. Now
1865 * lets see if we can place a single object there.
1866 */
1867 order = slab_order(size, 1, slub_max_order, 1);
1868 if (order <= slub_max_order)
1869 return order;
1870
1871 /*
1872 * Doh this slab cannot be placed using slub_max_order.
1873 */
1874 order = slab_order(size, 1, MAX_ORDER, 1);
1875 if (order <= MAX_ORDER)
1876 return order;
1877 return -ENOSYS;
1878}
1879
Christoph Lameter81819f02007-05-06 14:49:36 -07001880/*
Christoph Lameter672bba32007-05-09 02:32:39 -07001881 * Figure out what the alignment of the objects will be.
Christoph Lameter81819f02007-05-06 14:49:36 -07001882 */
1883static unsigned long calculate_alignment(unsigned long flags,
1884 unsigned long align, unsigned long size)
1885{
1886 /*
Christoph Lameter6446faa2008-02-15 23:45:26 -08001887 * If the user wants hardware cache aligned objects then follow that
1888 * suggestion if the object is sufficiently large.
Christoph Lameter81819f02007-05-06 14:49:36 -07001889 *
Christoph Lameter6446faa2008-02-15 23:45:26 -08001890 * The hardware cache alignment cannot override the specified
1891 * alignment though. If that is greater then use it.
Christoph Lameter81819f02007-05-06 14:49:36 -07001892 */
Nick Pigginb6210382008-03-05 14:05:56 -08001893 if (flags & SLAB_HWCACHE_ALIGN) {
1894 unsigned long ralign = cache_line_size();
1895 while (size <= ralign / 2)
1896 ralign /= 2;
1897 align = max(align, ralign);
1898 }
Christoph Lameter81819f02007-05-06 14:49:36 -07001899
1900 if (align < ARCH_SLAB_MINALIGN)
Nick Pigginb6210382008-03-05 14:05:56 -08001901 align = ARCH_SLAB_MINALIGN;
Christoph Lameter81819f02007-05-06 14:49:36 -07001902
1903 return ALIGN(align, sizeof(void *));
1904}
1905
Christoph Lameterdfb4f092007-10-16 01:26:05 -07001906static void init_kmem_cache_cpu(struct kmem_cache *s,
1907 struct kmem_cache_cpu *c)
1908{
1909 c->page = NULL;
Christoph Lametera973e9d2008-03-01 13:40:44 -08001910 c->freelist = NULL;
Christoph Lameterdfb4f092007-10-16 01:26:05 -07001911 c->node = 0;
Christoph Lameter42a9fdb2007-10-16 01:26:09 -07001912 c->offset = s->offset / sizeof(void *);
1913 c->objsize = s->objsize;
Pekka Enberg62f75532008-04-14 18:50:44 +03001914#ifdef CONFIG_SLUB_STATS
1915 memset(c->stat, 0, NR_SLUB_STAT_ITEMS * sizeof(unsigned));
1916#endif
Christoph Lameterdfb4f092007-10-16 01:26:05 -07001917}
1918
Christoph Lameter81819f02007-05-06 14:49:36 -07001919static void init_kmem_cache_node(struct kmem_cache_node *n)
1920{
1921 n->nr_partial = 0;
Christoph Lameter81819f02007-05-06 14:49:36 -07001922 spin_lock_init(&n->list_lock);
1923 INIT_LIST_HEAD(&n->partial);
Christoph Lameter8ab13722007-07-17 04:03:32 -07001924#ifdef CONFIG_SLUB_DEBUG
Christoph Lameter0f389ec2008-04-14 18:53:02 +03001925 atomic_long_set(&n->nr_slabs, 0);
Christoph Lameter643b1132007-05-06 14:49:42 -07001926 INIT_LIST_HEAD(&n->full);
Christoph Lameter8ab13722007-07-17 04:03:32 -07001927#endif
Christoph Lameter81819f02007-05-06 14:49:36 -07001928}
1929
Christoph Lameter4c93c3552007-10-16 01:26:08 -07001930#ifdef CONFIG_SMP
1931/*
1932 * Per cpu array for per cpu structures.
1933 *
1934 * The per cpu array places all kmem_cache_cpu structures from one processor
1935 * close together meaning that it becomes possible that multiple per cpu
1936 * structures are contained in one cacheline. This may be particularly
1937 * beneficial for the kmalloc caches.
1938 *
1939 * A desktop system typically has around 60-80 slabs. With 100 here we are
1940 * likely able to get per cpu structures for all caches from the array defined
1941 * here. We must be able to cover all kmalloc caches during bootstrap.
1942 *
1943 * If the per cpu array is exhausted then fall back to kmalloc
1944 * of individual cachelines. No sharing is possible then.
1945 */
1946#define NR_KMEM_CACHE_CPU 100
1947
1948static DEFINE_PER_CPU(struct kmem_cache_cpu,
1949 kmem_cache_cpu)[NR_KMEM_CACHE_CPU];
1950
1951static DEFINE_PER_CPU(struct kmem_cache_cpu *, kmem_cache_cpu_free);
1952static cpumask_t kmem_cach_cpu_free_init_once = CPU_MASK_NONE;
1953
1954static struct kmem_cache_cpu *alloc_kmem_cache_cpu(struct kmem_cache *s,
1955 int cpu, gfp_t flags)
1956{
1957 struct kmem_cache_cpu *c = per_cpu(kmem_cache_cpu_free, cpu);
1958
1959 if (c)
1960 per_cpu(kmem_cache_cpu_free, cpu) =
1961 (void *)c->freelist;
1962 else {
1963 /* Table overflow: So allocate ourselves */
1964 c = kmalloc_node(
1965 ALIGN(sizeof(struct kmem_cache_cpu), cache_line_size()),
1966 flags, cpu_to_node(cpu));
1967 if (!c)
1968 return NULL;
1969 }
1970
1971 init_kmem_cache_cpu(s, c);
1972 return c;
1973}
1974
1975static void free_kmem_cache_cpu(struct kmem_cache_cpu *c, int cpu)
1976{
1977 if (c < per_cpu(kmem_cache_cpu, cpu) ||
1978 c > per_cpu(kmem_cache_cpu, cpu) + NR_KMEM_CACHE_CPU) {
1979 kfree(c);
1980 return;
1981 }
1982 c->freelist = (void *)per_cpu(kmem_cache_cpu_free, cpu);
1983 per_cpu(kmem_cache_cpu_free, cpu) = c;
1984}
1985
1986static void free_kmem_cache_cpus(struct kmem_cache *s)
1987{
1988 int cpu;
1989
1990 for_each_online_cpu(cpu) {
1991 struct kmem_cache_cpu *c = get_cpu_slab(s, cpu);
1992
1993 if (c) {
1994 s->cpu_slab[cpu] = NULL;
1995 free_kmem_cache_cpu(c, cpu);
1996 }
1997 }
1998}
1999
2000static int alloc_kmem_cache_cpus(struct kmem_cache *s, gfp_t flags)
2001{
2002 int cpu;
2003
2004 for_each_online_cpu(cpu) {
2005 struct kmem_cache_cpu *c = get_cpu_slab(s, cpu);
2006
2007 if (c)
2008 continue;
2009
2010 c = alloc_kmem_cache_cpu(s, cpu, flags);
2011 if (!c) {
2012 free_kmem_cache_cpus(s);
2013 return 0;
2014 }
2015 s->cpu_slab[cpu] = c;
2016 }
2017 return 1;
2018}
2019
2020/*
2021 * Initialize the per cpu array.
2022 */
2023static void init_alloc_cpu_cpu(int cpu)
2024{
2025 int i;
2026
2027 if (cpu_isset(cpu, kmem_cach_cpu_free_init_once))
2028 return;
2029
2030 for (i = NR_KMEM_CACHE_CPU - 1; i >= 0; i--)
2031 free_kmem_cache_cpu(&per_cpu(kmem_cache_cpu, cpu)[i], cpu);
2032
2033 cpu_set(cpu, kmem_cach_cpu_free_init_once);
2034}
2035
2036static void __init init_alloc_cpu(void)
2037{
2038 int cpu;
2039
2040 for_each_online_cpu(cpu)
2041 init_alloc_cpu_cpu(cpu);
2042 }
2043
2044#else
2045static inline void free_kmem_cache_cpus(struct kmem_cache *s) {}
2046static inline void init_alloc_cpu(void) {}
2047
2048static inline int alloc_kmem_cache_cpus(struct kmem_cache *s, gfp_t flags)
2049{
2050 init_kmem_cache_cpu(s, &s->cpu_slab);
2051 return 1;
2052}
2053#endif
2054
Christoph Lameter81819f02007-05-06 14:49:36 -07002055#ifdef CONFIG_NUMA
2056/*
2057 * No kmalloc_node yet so do it by hand. We know that this is the first
2058 * slab on the node for this slabcache. There are no concurrent accesses
2059 * possible.
2060 *
2061 * Note that this function only works on the kmalloc_node_cache
Christoph Lameter4c93c3552007-10-16 01:26:08 -07002062 * when allocating for the kmalloc_node_cache. This is used for bootstrapping
2063 * memory on a fresh node that has no slab structures yet.
Christoph Lameter81819f02007-05-06 14:49:36 -07002064 */
Adrian Bunk1cd7daa2007-10-16 01:24:18 -07002065static struct kmem_cache_node *early_kmem_cache_node_alloc(gfp_t gfpflags,
2066 int node)
Christoph Lameter81819f02007-05-06 14:49:36 -07002067{
2068 struct page *page;
2069 struct kmem_cache_node *n;
rootba84c732008-01-07 23:20:28 -08002070 unsigned long flags;
Christoph Lameter81819f02007-05-06 14:49:36 -07002071
2072 BUG_ON(kmalloc_caches->size < sizeof(struct kmem_cache_node));
2073
Christoph Lametera2f92ee2007-08-22 14:01:57 -07002074 page = new_slab(kmalloc_caches, gfpflags, node);
Christoph Lameter81819f02007-05-06 14:49:36 -07002075
2076 BUG_ON(!page);
Christoph Lametera2f92ee2007-08-22 14:01:57 -07002077 if (page_to_nid(page) != node) {
2078 printk(KERN_ERR "SLUB: Unable to allocate memory from "
2079 "node %d\n", node);
2080 printk(KERN_ERR "SLUB: Allocating a useless per node structure "
2081 "in order to be able to continue\n");
2082 }
2083
Christoph Lameter81819f02007-05-06 14:49:36 -07002084 n = page->freelist;
2085 BUG_ON(!n);
2086 page->freelist = get_freepointer(kmalloc_caches, n);
2087 page->inuse++;
2088 kmalloc_caches->node[node] = n;
Christoph Lameter8ab13722007-07-17 04:03:32 -07002089#ifdef CONFIG_SLUB_DEBUG
Christoph Lameterd45f39c2007-07-17 04:03:21 -07002090 init_object(kmalloc_caches, n, 1);
2091 init_tracking(kmalloc_caches, n);
Christoph Lameter8ab13722007-07-17 04:03:32 -07002092#endif
Christoph Lameter81819f02007-05-06 14:49:36 -07002093 init_kmem_cache_node(n);
Christoph Lameter0f389ec2008-04-14 18:53:02 +03002094 inc_slabs_node(kmalloc_caches, node);
Christoph Lameter6446faa2008-02-15 23:45:26 -08002095
rootba84c732008-01-07 23:20:28 -08002096 /*
2097 * lockdep requires consistent irq usage for each lock
2098 * so even though there cannot be a race this early in
2099 * the boot sequence, we still disable irqs.
2100 */
2101 local_irq_save(flags);
Christoph Lameter7c2e1322008-01-07 23:20:27 -08002102 add_partial(n, page, 0);
rootba84c732008-01-07 23:20:28 -08002103 local_irq_restore(flags);
Christoph Lameter81819f02007-05-06 14:49:36 -07002104 return n;
2105}
2106
2107static void free_kmem_cache_nodes(struct kmem_cache *s)
2108{
2109 int node;
2110
Christoph Lameterf64dc582007-10-16 01:25:33 -07002111 for_each_node_state(node, N_NORMAL_MEMORY) {
Christoph Lameter81819f02007-05-06 14:49:36 -07002112 struct kmem_cache_node *n = s->node[node];
2113 if (n && n != &s->local_node)
2114 kmem_cache_free(kmalloc_caches, n);
2115 s->node[node] = NULL;
2116 }
2117}
2118
2119static int init_kmem_cache_nodes(struct kmem_cache *s, gfp_t gfpflags)
2120{
2121 int node;
2122 int local_node;
2123
2124 if (slab_state >= UP)
2125 local_node = page_to_nid(virt_to_page(s));
2126 else
2127 local_node = 0;
2128
Christoph Lameterf64dc582007-10-16 01:25:33 -07002129 for_each_node_state(node, N_NORMAL_MEMORY) {
Christoph Lameter81819f02007-05-06 14:49:36 -07002130 struct kmem_cache_node *n;
2131
2132 if (local_node == node)
2133 n = &s->local_node;
2134 else {
2135 if (slab_state == DOWN) {
2136 n = early_kmem_cache_node_alloc(gfpflags,
2137 node);
2138 continue;
2139 }
2140 n = kmem_cache_alloc_node(kmalloc_caches,
2141 gfpflags, node);
2142
2143 if (!n) {
2144 free_kmem_cache_nodes(s);
2145 return 0;
2146 }
2147
2148 }
2149 s->node[node] = n;
2150 init_kmem_cache_node(n);
2151 }
2152 return 1;
2153}
2154#else
2155static void free_kmem_cache_nodes(struct kmem_cache *s)
2156{
2157}
2158
2159static int init_kmem_cache_nodes(struct kmem_cache *s, gfp_t gfpflags)
2160{
2161 init_kmem_cache_node(&s->local_node);
2162 return 1;
2163}
2164#endif
2165
2166/*
2167 * calculate_sizes() determines the order and the distribution of data within
2168 * a slab object.
2169 */
2170static int calculate_sizes(struct kmem_cache *s)
2171{
2172 unsigned long flags = s->flags;
2173 unsigned long size = s->objsize;
2174 unsigned long align = s->align;
2175
2176 /*
Christoph Lameterd8b42bf2008-02-15 23:45:25 -08002177 * Round up object size to the next word boundary. We can only
2178 * place the free pointer at word boundaries and this determines
2179 * the possible location of the free pointer.
2180 */
2181 size = ALIGN(size, sizeof(void *));
2182
2183#ifdef CONFIG_SLUB_DEBUG
2184 /*
Christoph Lameter81819f02007-05-06 14:49:36 -07002185 * Determine if we can poison the object itself. If the user of
2186 * the slab may touch the object after free or before allocation
2187 * then we should never poison the object itself.
2188 */
2189 if ((flags & SLAB_POISON) && !(flags & SLAB_DESTROY_BY_RCU) &&
Christoph Lameterc59def92007-05-16 22:10:50 -07002190 !s->ctor)
Christoph Lameter81819f02007-05-06 14:49:36 -07002191 s->flags |= __OBJECT_POISON;
2192 else
2193 s->flags &= ~__OBJECT_POISON;
2194
Christoph Lameter81819f02007-05-06 14:49:36 -07002195
2196 /*
Christoph Lameter672bba32007-05-09 02:32:39 -07002197 * If we are Redzoning then check if there is some space between the
Christoph Lameter81819f02007-05-06 14:49:36 -07002198 * end of the object and the free pointer. If not then add an
Christoph Lameter672bba32007-05-09 02:32:39 -07002199 * additional word to have some bytes to store Redzone information.
Christoph Lameter81819f02007-05-06 14:49:36 -07002200 */
2201 if ((flags & SLAB_RED_ZONE) && size == s->objsize)
2202 size += sizeof(void *);
Christoph Lameter41ecc552007-05-09 02:32:44 -07002203#endif
Christoph Lameter81819f02007-05-06 14:49:36 -07002204
2205 /*
Christoph Lameter672bba32007-05-09 02:32:39 -07002206 * With that we have determined the number of bytes in actual use
2207 * by the object. This is the potential offset to the free pointer.
Christoph Lameter81819f02007-05-06 14:49:36 -07002208 */
2209 s->inuse = size;
2210
2211 if (((flags & (SLAB_DESTROY_BY_RCU | SLAB_POISON)) ||
Christoph Lameterc59def92007-05-16 22:10:50 -07002212 s->ctor)) {
Christoph Lameter81819f02007-05-06 14:49:36 -07002213 /*
2214 * Relocate free pointer after the object if it is not
2215 * permitted to overwrite the first word of the object on
2216 * kmem_cache_free.
2217 *
2218 * This is the case if we do RCU, have a constructor or
2219 * destructor or are poisoning the objects.
2220 */
2221 s->offset = size;
2222 size += sizeof(void *);
2223 }
2224
Christoph Lameterc12b3c62007-05-23 13:57:31 -07002225#ifdef CONFIG_SLUB_DEBUG
Christoph Lameter81819f02007-05-06 14:49:36 -07002226 if (flags & SLAB_STORE_USER)
2227 /*
2228 * Need to store information about allocs and frees after
2229 * the object.
2230 */
2231 size += 2 * sizeof(struct track);
2232
Christoph Lameterbe7b3fb2007-05-09 02:32:36 -07002233 if (flags & SLAB_RED_ZONE)
Christoph Lameter81819f02007-05-06 14:49:36 -07002234 /*
2235 * Add some empty padding so that we can catch
2236 * overwrites from earlier objects rather than let
2237 * tracking information or the free pointer be
2238 * corrupted if an user writes before the start
2239 * of the object.
2240 */
2241 size += sizeof(void *);
Christoph Lameter41ecc552007-05-09 02:32:44 -07002242#endif
Christoph Lameter672bba32007-05-09 02:32:39 -07002243
Christoph Lameter81819f02007-05-06 14:49:36 -07002244 /*
2245 * Determine the alignment based on various parameters that the
Christoph Lameter65c02d42007-05-09 02:32:35 -07002246 * user specified and the dynamic determination of cache line size
2247 * on bootup.
Christoph Lameter81819f02007-05-06 14:49:36 -07002248 */
2249 align = calculate_alignment(flags, align, s->objsize);
2250
2251 /*
2252 * SLUB stores one object immediately after another beginning from
2253 * offset 0. In order to align the objects we have to simply size
2254 * each object to conform to the alignment.
2255 */
2256 size = ALIGN(size, align);
2257 s->size = size;
2258
Christoph Lameter71c7a062008-02-14 14:28:01 -08002259 if ((flags & __KMALLOC_CACHE) &&
2260 PAGE_SIZE / size < slub_min_objects) {
2261 /*
2262 * Kmalloc cache that would not have enough objects in
2263 * an order 0 page. Kmalloc slabs can fallback to
2264 * page allocator order 0 allocs so take a reasonably large
2265 * order that will allows us a good number of objects.
2266 */
2267 s->order = max(slub_max_order, PAGE_ALLOC_COSTLY_ORDER);
2268 s->flags |= __PAGE_ALLOC_FALLBACK;
2269 s->allocflags |= __GFP_NOWARN;
2270 } else
2271 s->order = calculate_order(size);
2272
Christoph Lameter81819f02007-05-06 14:49:36 -07002273 if (s->order < 0)
2274 return 0;
2275
Christoph Lameterb7a49f02008-02-14 14:21:32 -08002276 s->allocflags = 0;
2277 if (s->order)
2278 s->allocflags |= __GFP_COMP;
2279
2280 if (s->flags & SLAB_CACHE_DMA)
2281 s->allocflags |= SLUB_DMA;
2282
2283 if (s->flags & SLAB_RECLAIM_ACCOUNT)
2284 s->allocflags |= __GFP_RECLAIMABLE;
2285
Christoph Lameter81819f02007-05-06 14:49:36 -07002286 /*
2287 * Determine the number of objects per slab
2288 */
2289 s->objects = (PAGE_SIZE << s->order) / size;
2290
Christoph Lameterb3fba8d2007-10-16 01:26:06 -07002291 return !!s->objects;
Christoph Lameter81819f02007-05-06 14:49:36 -07002292
2293}
2294
Christoph Lameter81819f02007-05-06 14:49:36 -07002295static int kmem_cache_open(struct kmem_cache *s, gfp_t gfpflags,
2296 const char *name, size_t size,
2297 size_t align, unsigned long flags,
Christoph Lameter4ba9b9d2007-10-16 23:25:51 -07002298 void (*ctor)(struct kmem_cache *, void *))
Christoph Lameter81819f02007-05-06 14:49:36 -07002299{
2300 memset(s, 0, kmem_size);
2301 s->name = name;
2302 s->ctor = ctor;
Christoph Lameter81819f02007-05-06 14:49:36 -07002303 s->objsize = size;
Christoph Lameter81819f02007-05-06 14:49:36 -07002304 s->align = align;
Christoph Lameterba0268a2007-09-11 15:24:11 -07002305 s->flags = kmem_cache_flags(size, flags, name, ctor);
Christoph Lameter81819f02007-05-06 14:49:36 -07002306
2307 if (!calculate_sizes(s))
2308 goto error;
2309
2310 s->refcount = 1;
2311#ifdef CONFIG_NUMA
Christoph Lameter98246012008-01-07 23:20:26 -08002312 s->remote_node_defrag_ratio = 100;
Christoph Lameter81819f02007-05-06 14:49:36 -07002313#endif
Christoph Lameterdfb4f092007-10-16 01:26:05 -07002314 if (!init_kmem_cache_nodes(s, gfpflags & ~SLUB_DMA))
2315 goto error;
Christoph Lameter81819f02007-05-06 14:49:36 -07002316
Christoph Lameterdfb4f092007-10-16 01:26:05 -07002317 if (alloc_kmem_cache_cpus(s, gfpflags & ~SLUB_DMA))
Christoph Lameter81819f02007-05-06 14:49:36 -07002318 return 1;
Christoph Lameter4c93c3552007-10-16 01:26:08 -07002319 free_kmem_cache_nodes(s);
Christoph Lameter81819f02007-05-06 14:49:36 -07002320error:
2321 if (flags & SLAB_PANIC)
2322 panic("Cannot create slab %s size=%lu realsize=%u "
2323 "order=%u offset=%u flags=%lx\n",
2324 s->name, (unsigned long)size, s->size, s->order,
2325 s->offset, flags);
2326 return 0;
2327}
Christoph Lameter81819f02007-05-06 14:49:36 -07002328
2329/*
2330 * Check if a given pointer is valid
2331 */
2332int kmem_ptr_validate(struct kmem_cache *s, const void *object)
2333{
Pekka Enberg06428782008-01-07 23:20:27 -08002334 struct page *page;
Christoph Lameter81819f02007-05-06 14:49:36 -07002335
2336 page = get_object_page(object);
2337
2338 if (!page || s != page->slab)
2339 /* No slab or wrong slab */
2340 return 0;
2341
Christoph Lameterabcd08a2007-05-09 02:32:37 -07002342 if (!check_valid_pointer(s, page, object))
Christoph Lameter81819f02007-05-06 14:49:36 -07002343 return 0;
2344
2345 /*
2346 * We could also check if the object is on the slabs freelist.
2347 * But this would be too expensive and it seems that the main
Christoph Lameter6446faa2008-02-15 23:45:26 -08002348 * purpose of kmem_ptr_valid() is to check if the object belongs
Christoph Lameter81819f02007-05-06 14:49:36 -07002349 * to a certain slab.
2350 */
2351 return 1;
2352}
2353EXPORT_SYMBOL(kmem_ptr_validate);
2354
2355/*
2356 * Determine the size of a slab object
2357 */
2358unsigned int kmem_cache_size(struct kmem_cache *s)
2359{
2360 return s->objsize;
2361}
2362EXPORT_SYMBOL(kmem_cache_size);
2363
2364const char *kmem_cache_name(struct kmem_cache *s)
2365{
2366 return s->name;
2367}
2368EXPORT_SYMBOL(kmem_cache_name);
2369
2370/*
Christoph Lameter672bba32007-05-09 02:32:39 -07002371 * Attempt to free all slabs on a node. Return the number of slabs we
2372 * were unable to free.
Christoph Lameter81819f02007-05-06 14:49:36 -07002373 */
2374static int free_list(struct kmem_cache *s, struct kmem_cache_node *n,
2375 struct list_head *list)
2376{
2377 int slabs_inuse = 0;
2378 unsigned long flags;
2379 struct page *page, *h;
2380
2381 spin_lock_irqsave(&n->list_lock, flags);
2382 list_for_each_entry_safe(page, h, list, lru)
2383 if (!page->inuse) {
2384 list_del(&page->lru);
2385 discard_slab(s, page);
2386 } else
2387 slabs_inuse++;
2388 spin_unlock_irqrestore(&n->list_lock, flags);
2389 return slabs_inuse;
2390}
2391
2392/*
Christoph Lameter672bba32007-05-09 02:32:39 -07002393 * Release all resources used by a slab cache.
Christoph Lameter81819f02007-05-06 14:49:36 -07002394 */
Christoph Lameter0c710012007-07-17 04:03:24 -07002395static inline int kmem_cache_close(struct kmem_cache *s)
Christoph Lameter81819f02007-05-06 14:49:36 -07002396{
2397 int node;
2398
2399 flush_all(s);
2400
2401 /* Attempt to free all objects */
Christoph Lameter4c93c3552007-10-16 01:26:08 -07002402 free_kmem_cache_cpus(s);
Christoph Lameterf64dc582007-10-16 01:25:33 -07002403 for_each_node_state(node, N_NORMAL_MEMORY) {
Christoph Lameter81819f02007-05-06 14:49:36 -07002404 struct kmem_cache_node *n = get_node(s, node);
2405
Christoph Lameter2086d262007-05-06 14:49:46 -07002406 n->nr_partial -= free_list(s, n, &n->partial);
Christoph Lameter0f389ec2008-04-14 18:53:02 +03002407 if (slabs_node(s, node))
Christoph Lameter81819f02007-05-06 14:49:36 -07002408 return 1;
2409 }
2410 free_kmem_cache_nodes(s);
2411 return 0;
2412}
2413
2414/*
2415 * Close a cache and release the kmem_cache structure
2416 * (must be used for caches created using kmem_cache_create)
2417 */
2418void kmem_cache_destroy(struct kmem_cache *s)
2419{
2420 down_write(&slub_lock);
2421 s->refcount--;
2422 if (!s->refcount) {
2423 list_del(&s->list);
Christoph Lametera0e1d1b2007-07-17 04:03:31 -07002424 up_write(&slub_lock);
Christoph Lameter81819f02007-05-06 14:49:36 -07002425 if (kmem_cache_close(s))
2426 WARN_ON(1);
2427 sysfs_slab_remove(s);
Christoph Lametera0e1d1b2007-07-17 04:03:31 -07002428 } else
2429 up_write(&slub_lock);
Christoph Lameter81819f02007-05-06 14:49:36 -07002430}
2431EXPORT_SYMBOL(kmem_cache_destroy);
2432
2433/********************************************************************
2434 * Kmalloc subsystem
2435 *******************************************************************/
2436
Christoph Lameter331dc552008-02-14 14:28:09 -08002437struct kmem_cache kmalloc_caches[PAGE_SHIFT + 1] __cacheline_aligned;
Christoph Lameter81819f02007-05-06 14:49:36 -07002438EXPORT_SYMBOL(kmalloc_caches);
2439
Christoph Lameter81819f02007-05-06 14:49:36 -07002440static int __init setup_slub_min_order(char *str)
2441{
Pekka Enberg06428782008-01-07 23:20:27 -08002442 get_option(&str, &slub_min_order);
Christoph Lameter81819f02007-05-06 14:49:36 -07002443
2444 return 1;
2445}
2446
2447__setup("slub_min_order=", setup_slub_min_order);
2448
2449static int __init setup_slub_max_order(char *str)
2450{
Pekka Enberg06428782008-01-07 23:20:27 -08002451 get_option(&str, &slub_max_order);
Christoph Lameter81819f02007-05-06 14:49:36 -07002452
2453 return 1;
2454}
2455
2456__setup("slub_max_order=", setup_slub_max_order);
2457
2458static int __init setup_slub_min_objects(char *str)
2459{
Pekka Enberg06428782008-01-07 23:20:27 -08002460 get_option(&str, &slub_min_objects);
Christoph Lameter81819f02007-05-06 14:49:36 -07002461
2462 return 1;
2463}
2464
2465__setup("slub_min_objects=", setup_slub_min_objects);
2466
2467static int __init setup_slub_nomerge(char *str)
2468{
2469 slub_nomerge = 1;
2470 return 1;
2471}
2472
2473__setup("slub_nomerge", setup_slub_nomerge);
2474
Christoph Lameter81819f02007-05-06 14:49:36 -07002475static struct kmem_cache *create_kmalloc_cache(struct kmem_cache *s,
2476 const char *name, int size, gfp_t gfp_flags)
2477{
2478 unsigned int flags = 0;
2479
2480 if (gfp_flags & SLUB_DMA)
2481 flags = SLAB_CACHE_DMA;
2482
2483 down_write(&slub_lock);
2484 if (!kmem_cache_open(s, gfp_flags, name, size, ARCH_KMALLOC_MINALIGN,
Christoph Lameter71c7a062008-02-14 14:28:01 -08002485 flags | __KMALLOC_CACHE, NULL))
Christoph Lameter81819f02007-05-06 14:49:36 -07002486 goto panic;
2487
2488 list_add(&s->list, &slab_caches);
2489 up_write(&slub_lock);
2490 if (sysfs_slab_add(s))
2491 goto panic;
2492 return s;
2493
2494panic:
2495 panic("Creation of kmalloc slab %s size=%d failed.\n", name, size);
2496}
2497
Christoph Lameter2e443fd2007-07-17 04:03:24 -07002498#ifdef CONFIG_ZONE_DMA
Christoph Lameter4097d602008-04-14 18:51:18 +03002499static struct kmem_cache *kmalloc_caches_dma[PAGE_SHIFT + 1];
Christoph Lameter1ceef402007-08-07 15:11:48 -07002500
2501static void sysfs_add_func(struct work_struct *w)
2502{
2503 struct kmem_cache *s;
2504
2505 down_write(&slub_lock);
2506 list_for_each_entry(s, &slab_caches, list) {
2507 if (s->flags & __SYSFS_ADD_DEFERRED) {
2508 s->flags &= ~__SYSFS_ADD_DEFERRED;
2509 sysfs_slab_add(s);
2510 }
2511 }
2512 up_write(&slub_lock);
2513}
2514
2515static DECLARE_WORK(sysfs_add_work, sysfs_add_func);
2516
Christoph Lameter2e443fd2007-07-17 04:03:24 -07002517static noinline struct kmem_cache *dma_kmalloc_cache(int index, gfp_t flags)
2518{
2519 struct kmem_cache *s;
Christoph Lameter2e443fd2007-07-17 04:03:24 -07002520 char *text;
2521 size_t realsize;
2522
2523 s = kmalloc_caches_dma[index];
2524 if (s)
2525 return s;
2526
2527 /* Dynamically create dma cache */
Christoph Lameter1ceef402007-08-07 15:11:48 -07002528 if (flags & __GFP_WAIT)
2529 down_write(&slub_lock);
2530 else {
2531 if (!down_write_trylock(&slub_lock))
2532 goto out;
2533 }
2534
2535 if (kmalloc_caches_dma[index])
2536 goto unlock_out;
Christoph Lameter2e443fd2007-07-17 04:03:24 -07002537
Christoph Lameter7b55f622007-07-17 04:03:27 -07002538 realsize = kmalloc_caches[index].objsize;
Ingo Molnar3adbefe2008-02-05 17:57:39 -08002539 text = kasprintf(flags & ~SLUB_DMA, "kmalloc_dma-%d",
2540 (unsigned int)realsize);
Christoph Lameter1ceef402007-08-07 15:11:48 -07002541 s = kmalloc(kmem_size, flags & ~SLUB_DMA);
2542
2543 if (!s || !text || !kmem_cache_open(s, flags, text,
2544 realsize, ARCH_KMALLOC_MINALIGN,
2545 SLAB_CACHE_DMA|__SYSFS_ADD_DEFERRED, NULL)) {
2546 kfree(s);
2547 kfree(text);
2548 goto unlock_out;
Christoph Lameterdfce8642007-07-17 04:03:25 -07002549 }
Christoph Lameter1ceef402007-08-07 15:11:48 -07002550
2551 list_add(&s->list, &slab_caches);
2552 kmalloc_caches_dma[index] = s;
2553
2554 schedule_work(&sysfs_add_work);
2555
2556unlock_out:
Christoph Lameterdfce8642007-07-17 04:03:25 -07002557 up_write(&slub_lock);
Christoph Lameter1ceef402007-08-07 15:11:48 -07002558out:
Christoph Lameterdfce8642007-07-17 04:03:25 -07002559 return kmalloc_caches_dma[index];
Christoph Lameter2e443fd2007-07-17 04:03:24 -07002560}
2561#endif
2562
Christoph Lameterf1b26332007-07-17 04:03:26 -07002563/*
2564 * Conversion table for small slabs sizes / 8 to the index in the
2565 * kmalloc array. This is necessary for slabs < 192 since we have non power
2566 * of two cache sizes there. The size of larger slabs can be determined using
2567 * fls.
2568 */
2569static s8 size_index[24] = {
2570 3, /* 8 */
2571 4, /* 16 */
2572 5, /* 24 */
2573 5, /* 32 */
2574 6, /* 40 */
2575 6, /* 48 */
2576 6, /* 56 */
2577 6, /* 64 */
2578 1, /* 72 */
2579 1, /* 80 */
2580 1, /* 88 */
2581 1, /* 96 */
2582 7, /* 104 */
2583 7, /* 112 */
2584 7, /* 120 */
2585 7, /* 128 */
2586 2, /* 136 */
2587 2, /* 144 */
2588 2, /* 152 */
2589 2, /* 160 */
2590 2, /* 168 */
2591 2, /* 176 */
2592 2, /* 184 */
2593 2 /* 192 */
2594};
2595
Christoph Lameter81819f02007-05-06 14:49:36 -07002596static struct kmem_cache *get_slab(size_t size, gfp_t flags)
2597{
Christoph Lameterf1b26332007-07-17 04:03:26 -07002598 int index;
Christoph Lameter81819f02007-05-06 14:49:36 -07002599
Christoph Lameterf1b26332007-07-17 04:03:26 -07002600 if (size <= 192) {
2601 if (!size)
2602 return ZERO_SIZE_PTR;
Christoph Lameter81819f02007-05-06 14:49:36 -07002603
Christoph Lameterf1b26332007-07-17 04:03:26 -07002604 index = size_index[(size - 1) / 8];
Christoph Lameteraadb4bc2007-10-16 01:24:38 -07002605 } else
Christoph Lameterf1b26332007-07-17 04:03:26 -07002606 index = fls(size - 1);
Christoph Lameter81819f02007-05-06 14:49:36 -07002607
2608#ifdef CONFIG_ZONE_DMA
Christoph Lameterf1b26332007-07-17 04:03:26 -07002609 if (unlikely((flags & SLUB_DMA)))
Christoph Lameter2e443fd2007-07-17 04:03:24 -07002610 return dma_kmalloc_cache(index, flags);
Christoph Lameterf1b26332007-07-17 04:03:26 -07002611
Christoph Lameter81819f02007-05-06 14:49:36 -07002612#endif
2613 return &kmalloc_caches[index];
2614}
2615
2616void *__kmalloc(size_t size, gfp_t flags)
2617{
Christoph Lameteraadb4bc2007-10-16 01:24:38 -07002618 struct kmem_cache *s;
Christoph Lameter81819f02007-05-06 14:49:36 -07002619
Christoph Lameter331dc552008-02-14 14:28:09 -08002620 if (unlikely(size > PAGE_SIZE))
Pekka Enbergeada35e2008-02-11 22:47:46 +02002621 return kmalloc_large(size, flags);
Christoph Lameteraadb4bc2007-10-16 01:24:38 -07002622
2623 s = get_slab(size, flags);
2624
2625 if (unlikely(ZERO_OR_NULL_PTR(s)))
Christoph Lameter6cb8f912007-07-17 04:03:22 -07002626 return s;
2627
Christoph Lameterce15fea2007-07-17 04:03:28 -07002628 return slab_alloc(s, flags, -1, __builtin_return_address(0));
Christoph Lameter81819f02007-05-06 14:49:36 -07002629}
2630EXPORT_SYMBOL(__kmalloc);
2631
Christoph Lameterf619cfe2008-03-01 13:56:40 -08002632static void *kmalloc_large_node(size_t size, gfp_t flags, int node)
2633{
2634 struct page *page = alloc_pages_node(node, flags | __GFP_COMP,
2635 get_order(size));
2636
2637 if (page)
2638 return page_address(page);
2639 else
2640 return NULL;
2641}
2642
Christoph Lameter81819f02007-05-06 14:49:36 -07002643#ifdef CONFIG_NUMA
2644void *__kmalloc_node(size_t size, gfp_t flags, int node)
2645{
Christoph Lameteraadb4bc2007-10-16 01:24:38 -07002646 struct kmem_cache *s;
Christoph Lameter81819f02007-05-06 14:49:36 -07002647
Christoph Lameter331dc552008-02-14 14:28:09 -08002648 if (unlikely(size > PAGE_SIZE))
Christoph Lameterf619cfe2008-03-01 13:56:40 -08002649 return kmalloc_large_node(size, flags, node);
Christoph Lameteraadb4bc2007-10-16 01:24:38 -07002650
2651 s = get_slab(size, flags);
2652
2653 if (unlikely(ZERO_OR_NULL_PTR(s)))
Christoph Lameter6cb8f912007-07-17 04:03:22 -07002654 return s;
2655
Christoph Lameterce15fea2007-07-17 04:03:28 -07002656 return slab_alloc(s, flags, node, __builtin_return_address(0));
Christoph Lameter81819f02007-05-06 14:49:36 -07002657}
2658EXPORT_SYMBOL(__kmalloc_node);
2659#endif
2660
2661size_t ksize(const void *object)
2662{
Christoph Lameter272c1d22007-06-08 13:46:49 -07002663 struct page *page;
Christoph Lameter81819f02007-05-06 14:49:36 -07002664 struct kmem_cache *s;
2665
Christoph Lameteref8b4522007-10-16 01:24:46 -07002666 if (unlikely(object == ZERO_SIZE_PTR))
Christoph Lameter272c1d22007-06-08 13:46:49 -07002667 return 0;
2668
Vegard Nossum294a80a2007-12-04 23:45:30 -08002669 page = virt_to_head_page(object);
Vegard Nossum294a80a2007-12-04 23:45:30 -08002670
2671 if (unlikely(!PageSlab(page)))
2672 return PAGE_SIZE << compound_order(page);
2673
Christoph Lameter81819f02007-05-06 14:49:36 -07002674 s = page->slab;
Christoph Lameter81819f02007-05-06 14:49:36 -07002675
Christoph Lameterae20bfd2008-02-15 23:45:25 -08002676#ifdef CONFIG_SLUB_DEBUG
Christoph Lameter81819f02007-05-06 14:49:36 -07002677 /*
2678 * Debugging requires use of the padding between object
2679 * and whatever may come after it.
2680 */
2681 if (s->flags & (SLAB_RED_ZONE | SLAB_POISON))
2682 return s->objsize;
2683
Christoph Lameterae20bfd2008-02-15 23:45:25 -08002684#endif
Christoph Lameter81819f02007-05-06 14:49:36 -07002685 /*
2686 * If we have the need to store the freelist pointer
2687 * back there or track user information then we can
2688 * only use the space before that information.
2689 */
2690 if (s->flags & (SLAB_DESTROY_BY_RCU | SLAB_STORE_USER))
2691 return s->inuse;
Christoph Lameter81819f02007-05-06 14:49:36 -07002692 /*
2693 * Else we can use all the padding etc for the allocation
2694 */
2695 return s->size;
2696}
2697EXPORT_SYMBOL(ksize);
2698
2699void kfree(const void *x)
2700{
Christoph Lameter81819f02007-05-06 14:49:36 -07002701 struct page *page;
Christoph Lameter5bb983b2008-02-07 17:47:41 -08002702 void *object = (void *)x;
Christoph Lameter81819f02007-05-06 14:49:36 -07002703
Satyam Sharma2408c552007-10-16 01:24:44 -07002704 if (unlikely(ZERO_OR_NULL_PTR(x)))
Christoph Lameter81819f02007-05-06 14:49:36 -07002705 return;
2706
Christoph Lameterb49af682007-05-06 14:49:41 -07002707 page = virt_to_head_page(x);
Christoph Lameteraadb4bc2007-10-16 01:24:38 -07002708 if (unlikely(!PageSlab(page))) {
2709 put_page(page);
2710 return;
2711 }
Christoph Lameter5bb983b2008-02-07 17:47:41 -08002712 slab_free(page->slab, page, object, __builtin_return_address(0));
Christoph Lameter81819f02007-05-06 14:49:36 -07002713}
2714EXPORT_SYMBOL(kfree);
2715
Christoph Lameter2086d262007-05-06 14:49:46 -07002716/*
Christoph Lameter672bba32007-05-09 02:32:39 -07002717 * kmem_cache_shrink removes empty slabs from the partial lists and sorts
2718 * the remaining slabs by the number of items in use. The slabs with the
2719 * most items in use come first. New allocations will then fill those up
2720 * and thus they can be removed from the partial lists.
2721 *
2722 * The slabs with the least items are placed last. This results in them
2723 * being allocated from last increasing the chance that the last objects
2724 * are freed in them.
Christoph Lameter2086d262007-05-06 14:49:46 -07002725 */
2726int kmem_cache_shrink(struct kmem_cache *s)
2727{
2728 int node;
2729 int i;
2730 struct kmem_cache_node *n;
2731 struct page *page;
2732 struct page *t;
2733 struct list_head *slabs_by_inuse =
2734 kmalloc(sizeof(struct list_head) * s->objects, GFP_KERNEL);
2735 unsigned long flags;
2736
2737 if (!slabs_by_inuse)
2738 return -ENOMEM;
2739
2740 flush_all(s);
Christoph Lameterf64dc582007-10-16 01:25:33 -07002741 for_each_node_state(node, N_NORMAL_MEMORY) {
Christoph Lameter2086d262007-05-06 14:49:46 -07002742 n = get_node(s, node);
2743
2744 if (!n->nr_partial)
2745 continue;
2746
2747 for (i = 0; i < s->objects; i++)
2748 INIT_LIST_HEAD(slabs_by_inuse + i);
2749
2750 spin_lock_irqsave(&n->list_lock, flags);
2751
2752 /*
Christoph Lameter672bba32007-05-09 02:32:39 -07002753 * Build lists indexed by the items in use in each slab.
Christoph Lameter2086d262007-05-06 14:49:46 -07002754 *
Christoph Lameter672bba32007-05-09 02:32:39 -07002755 * Note that concurrent frees may occur while we hold the
2756 * list_lock. page->inuse here is the upper limit.
Christoph Lameter2086d262007-05-06 14:49:46 -07002757 */
2758 list_for_each_entry_safe(page, t, &n->partial, lru) {
2759 if (!page->inuse && slab_trylock(page)) {
2760 /*
2761 * Must hold slab lock here because slab_free
2762 * may have freed the last object and be
2763 * waiting to release the slab.
2764 */
2765 list_del(&page->lru);
2766 n->nr_partial--;
2767 slab_unlock(page);
2768 discard_slab(s, page);
2769 } else {
Christoph Lameterfcda3d82007-07-30 13:06:46 -07002770 list_move(&page->lru,
2771 slabs_by_inuse + page->inuse);
Christoph Lameter2086d262007-05-06 14:49:46 -07002772 }
2773 }
2774
Christoph Lameter2086d262007-05-06 14:49:46 -07002775 /*
Christoph Lameter672bba32007-05-09 02:32:39 -07002776 * Rebuild the partial list with the slabs filled up most
2777 * first and the least used slabs at the end.
Christoph Lameter2086d262007-05-06 14:49:46 -07002778 */
2779 for (i = s->objects - 1; i >= 0; i--)
2780 list_splice(slabs_by_inuse + i, n->partial.prev);
2781
Christoph Lameter2086d262007-05-06 14:49:46 -07002782 spin_unlock_irqrestore(&n->list_lock, flags);
2783 }
2784
2785 kfree(slabs_by_inuse);
2786 return 0;
2787}
2788EXPORT_SYMBOL(kmem_cache_shrink);
2789
Yasunori Gotob9049e22007-10-21 16:41:37 -07002790#if defined(CONFIG_NUMA) && defined(CONFIG_MEMORY_HOTPLUG)
2791static int slab_mem_going_offline_callback(void *arg)
2792{
2793 struct kmem_cache *s;
2794
2795 down_read(&slub_lock);
2796 list_for_each_entry(s, &slab_caches, list)
2797 kmem_cache_shrink(s);
2798 up_read(&slub_lock);
2799
2800 return 0;
2801}
2802
2803static void slab_mem_offline_callback(void *arg)
2804{
2805 struct kmem_cache_node *n;
2806 struct kmem_cache *s;
2807 struct memory_notify *marg = arg;
2808 int offline_node;
2809
2810 offline_node = marg->status_change_nid;
2811
2812 /*
2813 * If the node still has available memory. we need kmem_cache_node
2814 * for it yet.
2815 */
2816 if (offline_node < 0)
2817 return;
2818
2819 down_read(&slub_lock);
2820 list_for_each_entry(s, &slab_caches, list) {
2821 n = get_node(s, offline_node);
2822 if (n) {
2823 /*
2824 * if n->nr_slabs > 0, slabs still exist on the node
2825 * that is going down. We were unable to free them,
2826 * and offline_pages() function shoudn't call this
2827 * callback. So, we must fail.
2828 */
Christoph Lameter0f389ec2008-04-14 18:53:02 +03002829 BUG_ON(slabs_node(s, offline_node));
Yasunori Gotob9049e22007-10-21 16:41:37 -07002830
2831 s->node[offline_node] = NULL;
2832 kmem_cache_free(kmalloc_caches, n);
2833 }
2834 }
2835 up_read(&slub_lock);
2836}
2837
2838static int slab_mem_going_online_callback(void *arg)
2839{
2840 struct kmem_cache_node *n;
2841 struct kmem_cache *s;
2842 struct memory_notify *marg = arg;
2843 int nid = marg->status_change_nid;
2844 int ret = 0;
2845
2846 /*
2847 * If the node's memory is already available, then kmem_cache_node is
2848 * already created. Nothing to do.
2849 */
2850 if (nid < 0)
2851 return 0;
2852
2853 /*
2854 * We are bringing a node online. No memory is availabe yet. We must
2855 * allocate a kmem_cache_node structure in order to bring the node
2856 * online.
2857 */
2858 down_read(&slub_lock);
2859 list_for_each_entry(s, &slab_caches, list) {
2860 /*
2861 * XXX: kmem_cache_alloc_node will fallback to other nodes
2862 * since memory is not yet available from the node that
2863 * is brought up.
2864 */
2865 n = kmem_cache_alloc(kmalloc_caches, GFP_KERNEL);
2866 if (!n) {
2867 ret = -ENOMEM;
2868 goto out;
2869 }
2870 init_kmem_cache_node(n);
2871 s->node[nid] = n;
2872 }
2873out:
2874 up_read(&slub_lock);
2875 return ret;
2876}
2877
2878static int slab_memory_callback(struct notifier_block *self,
2879 unsigned long action, void *arg)
2880{
2881 int ret = 0;
2882
2883 switch (action) {
2884 case MEM_GOING_ONLINE:
2885 ret = slab_mem_going_online_callback(arg);
2886 break;
2887 case MEM_GOING_OFFLINE:
2888 ret = slab_mem_going_offline_callback(arg);
2889 break;
2890 case MEM_OFFLINE:
2891 case MEM_CANCEL_ONLINE:
2892 slab_mem_offline_callback(arg);
2893 break;
2894 case MEM_ONLINE:
2895 case MEM_CANCEL_OFFLINE:
2896 break;
2897 }
2898
2899 ret = notifier_from_errno(ret);
2900 return ret;
2901}
2902
2903#endif /* CONFIG_MEMORY_HOTPLUG */
2904
Christoph Lameter81819f02007-05-06 14:49:36 -07002905/********************************************************************
2906 * Basic setup of slabs
2907 *******************************************************************/
2908
2909void __init kmem_cache_init(void)
2910{
2911 int i;
Christoph Lameter4b356be2007-06-16 10:16:13 -07002912 int caches = 0;
Christoph Lameter81819f02007-05-06 14:49:36 -07002913
Christoph Lameter4c93c3552007-10-16 01:26:08 -07002914 init_alloc_cpu();
2915
Christoph Lameter81819f02007-05-06 14:49:36 -07002916#ifdef CONFIG_NUMA
2917 /*
2918 * Must first have the slab cache available for the allocations of the
Christoph Lameter672bba32007-05-09 02:32:39 -07002919 * struct kmem_cache_node's. There is special bootstrap code in
Christoph Lameter81819f02007-05-06 14:49:36 -07002920 * kmem_cache_open for slab_state == DOWN.
2921 */
2922 create_kmalloc_cache(&kmalloc_caches[0], "kmem_cache_node",
2923 sizeof(struct kmem_cache_node), GFP_KERNEL);
Christoph Lameter8ffa6872007-05-31 00:40:51 -07002924 kmalloc_caches[0].refcount = -1;
Christoph Lameter4b356be2007-06-16 10:16:13 -07002925 caches++;
Yasunori Gotob9049e22007-10-21 16:41:37 -07002926
2927 hotplug_memory_notifier(slab_memory_callback, 1);
Christoph Lameter81819f02007-05-06 14:49:36 -07002928#endif
2929
2930 /* Able to allocate the per node structures */
2931 slab_state = PARTIAL;
2932
2933 /* Caches that are not of the two-to-the-power-of size */
Christoph Lameter4b356be2007-06-16 10:16:13 -07002934 if (KMALLOC_MIN_SIZE <= 64) {
2935 create_kmalloc_cache(&kmalloc_caches[1],
Christoph Lameter81819f02007-05-06 14:49:36 -07002936 "kmalloc-96", 96, GFP_KERNEL);
Christoph Lameter4b356be2007-06-16 10:16:13 -07002937 caches++;
2938 }
2939 if (KMALLOC_MIN_SIZE <= 128) {
2940 create_kmalloc_cache(&kmalloc_caches[2],
Christoph Lameter81819f02007-05-06 14:49:36 -07002941 "kmalloc-192", 192, GFP_KERNEL);
Christoph Lameter4b356be2007-06-16 10:16:13 -07002942 caches++;
2943 }
Christoph Lameter81819f02007-05-06 14:49:36 -07002944
Christoph Lameter331dc552008-02-14 14:28:09 -08002945 for (i = KMALLOC_SHIFT_LOW; i <= PAGE_SHIFT; i++) {
Christoph Lameter81819f02007-05-06 14:49:36 -07002946 create_kmalloc_cache(&kmalloc_caches[i],
2947 "kmalloc", 1 << i, GFP_KERNEL);
Christoph Lameter4b356be2007-06-16 10:16:13 -07002948 caches++;
2949 }
Christoph Lameter81819f02007-05-06 14:49:36 -07002950
Christoph Lameterf1b26332007-07-17 04:03:26 -07002951
2952 /*
2953 * Patch up the size_index table if we have strange large alignment
2954 * requirements for the kmalloc array. This is only the case for
Christoph Lameter6446faa2008-02-15 23:45:26 -08002955 * MIPS it seems. The standard arches will not generate any code here.
Christoph Lameterf1b26332007-07-17 04:03:26 -07002956 *
2957 * Largest permitted alignment is 256 bytes due to the way we
2958 * handle the index determination for the smaller caches.
2959 *
2960 * Make sure that nothing crazy happens if someone starts tinkering
2961 * around with ARCH_KMALLOC_MINALIGN
2962 */
2963 BUILD_BUG_ON(KMALLOC_MIN_SIZE > 256 ||
2964 (KMALLOC_MIN_SIZE & (KMALLOC_MIN_SIZE - 1)));
2965
Christoph Lameter12ad6842007-07-17 04:03:28 -07002966 for (i = 8; i < KMALLOC_MIN_SIZE; i += 8)
Christoph Lameterf1b26332007-07-17 04:03:26 -07002967 size_index[(i - 1) / 8] = KMALLOC_SHIFT_LOW;
2968
Christoph Lameter81819f02007-05-06 14:49:36 -07002969 slab_state = UP;
2970
2971 /* Provide the correct kmalloc names now that the caches are up */
Christoph Lameter331dc552008-02-14 14:28:09 -08002972 for (i = KMALLOC_SHIFT_LOW; i <= PAGE_SHIFT; i++)
Christoph Lameter81819f02007-05-06 14:49:36 -07002973 kmalloc_caches[i]. name =
2974 kasprintf(GFP_KERNEL, "kmalloc-%d", 1 << i);
2975
2976#ifdef CONFIG_SMP
2977 register_cpu_notifier(&slab_notifier);
Christoph Lameter4c93c3552007-10-16 01:26:08 -07002978 kmem_size = offsetof(struct kmem_cache, cpu_slab) +
2979 nr_cpu_ids * sizeof(struct kmem_cache_cpu *);
2980#else
2981 kmem_size = sizeof(struct kmem_cache);
Christoph Lameter81819f02007-05-06 14:49:36 -07002982#endif
2983
Ingo Molnar3adbefe2008-02-05 17:57:39 -08002984 printk(KERN_INFO
2985 "SLUB: Genslabs=%d, HWalign=%d, Order=%d-%d, MinObjects=%d,"
Christoph Lameter4b356be2007-06-16 10:16:13 -07002986 " CPUs=%d, Nodes=%d\n",
2987 caches, cache_line_size(),
Christoph Lameter81819f02007-05-06 14:49:36 -07002988 slub_min_order, slub_max_order, slub_min_objects,
2989 nr_cpu_ids, nr_node_ids);
2990}
2991
2992/*
2993 * Find a mergeable slab cache
2994 */
2995static int slab_unmergeable(struct kmem_cache *s)
2996{
2997 if (slub_nomerge || (s->flags & SLUB_NEVER_MERGE))
2998 return 1;
2999
Christoph Lameter331dc552008-02-14 14:28:09 -08003000 if ((s->flags & __PAGE_ALLOC_FALLBACK))
Christoph Lameter71c7a062008-02-14 14:28:01 -08003001 return 1;
3002
Christoph Lameterc59def92007-05-16 22:10:50 -07003003 if (s->ctor)
Christoph Lameter81819f02007-05-06 14:49:36 -07003004 return 1;
3005
Christoph Lameter8ffa6872007-05-31 00:40:51 -07003006 /*
3007 * We may have set a slab to be unmergeable during bootstrap.
3008 */
3009 if (s->refcount < 0)
3010 return 1;
3011
Christoph Lameter81819f02007-05-06 14:49:36 -07003012 return 0;
3013}
3014
3015static struct kmem_cache *find_mergeable(size_t size,
Christoph Lameterba0268a2007-09-11 15:24:11 -07003016 size_t align, unsigned long flags, const char *name,
Christoph Lameter4ba9b9d2007-10-16 23:25:51 -07003017 void (*ctor)(struct kmem_cache *, void *))
Christoph Lameter81819f02007-05-06 14:49:36 -07003018{
Christoph Lameter5b95a4ac2007-07-17 04:03:19 -07003019 struct kmem_cache *s;
Christoph Lameter81819f02007-05-06 14:49:36 -07003020
3021 if (slub_nomerge || (flags & SLUB_NEVER_MERGE))
3022 return NULL;
3023
Christoph Lameterc59def92007-05-16 22:10:50 -07003024 if (ctor)
Christoph Lameter81819f02007-05-06 14:49:36 -07003025 return NULL;
3026
3027 size = ALIGN(size, sizeof(void *));
3028 align = calculate_alignment(flags, align, size);
3029 size = ALIGN(size, align);
Christoph Lameterba0268a2007-09-11 15:24:11 -07003030 flags = kmem_cache_flags(size, flags, name, NULL);
Christoph Lameter81819f02007-05-06 14:49:36 -07003031
Christoph Lameter5b95a4ac2007-07-17 04:03:19 -07003032 list_for_each_entry(s, &slab_caches, list) {
Christoph Lameter81819f02007-05-06 14:49:36 -07003033 if (slab_unmergeable(s))
3034 continue;
3035
3036 if (size > s->size)
3037 continue;
3038
Christoph Lameterba0268a2007-09-11 15:24:11 -07003039 if ((flags & SLUB_MERGE_SAME) != (s->flags & SLUB_MERGE_SAME))
Christoph Lameter81819f02007-05-06 14:49:36 -07003040 continue;
3041 /*
3042 * Check if alignment is compatible.
3043 * Courtesy of Adrian Drzewiecki
3044 */
Pekka Enberg06428782008-01-07 23:20:27 -08003045 if ((s->size & ~(align - 1)) != s->size)
Christoph Lameter81819f02007-05-06 14:49:36 -07003046 continue;
3047
3048 if (s->size - size >= sizeof(void *))
3049 continue;
3050
3051 return s;
3052 }
3053 return NULL;
3054}
3055
3056struct kmem_cache *kmem_cache_create(const char *name, size_t size,
3057 size_t align, unsigned long flags,
Christoph Lameter4ba9b9d2007-10-16 23:25:51 -07003058 void (*ctor)(struct kmem_cache *, void *))
Christoph Lameter81819f02007-05-06 14:49:36 -07003059{
3060 struct kmem_cache *s;
3061
3062 down_write(&slub_lock);
Christoph Lameterba0268a2007-09-11 15:24:11 -07003063 s = find_mergeable(size, align, flags, name, ctor);
Christoph Lameter81819f02007-05-06 14:49:36 -07003064 if (s) {
Christoph Lameter42a9fdb2007-10-16 01:26:09 -07003065 int cpu;
3066
Christoph Lameter81819f02007-05-06 14:49:36 -07003067 s->refcount++;
3068 /*
3069 * Adjust the object sizes so that we clear
3070 * the complete object on kzalloc.
3071 */
3072 s->objsize = max(s->objsize, (int)size);
Christoph Lameter42a9fdb2007-10-16 01:26:09 -07003073
3074 /*
3075 * And then we need to update the object size in the
3076 * per cpu structures
3077 */
3078 for_each_online_cpu(cpu)
3079 get_cpu_slab(s, cpu)->objsize = s->objsize;
Christoph Lameter6446faa2008-02-15 23:45:26 -08003080
Christoph Lameter81819f02007-05-06 14:49:36 -07003081 s->inuse = max_t(int, s->inuse, ALIGN(size, sizeof(void *)));
Christoph Lametera0e1d1b2007-07-17 04:03:31 -07003082 up_write(&slub_lock);
Christoph Lameter6446faa2008-02-15 23:45:26 -08003083
Christoph Lameter81819f02007-05-06 14:49:36 -07003084 if (sysfs_slab_alias(s, name))
3085 goto err;
Christoph Lametera0e1d1b2007-07-17 04:03:31 -07003086 return s;
3087 }
Christoph Lameter6446faa2008-02-15 23:45:26 -08003088
Christoph Lametera0e1d1b2007-07-17 04:03:31 -07003089 s = kmalloc(kmem_size, GFP_KERNEL);
3090 if (s) {
3091 if (kmem_cache_open(s, GFP_KERNEL, name,
Christoph Lameterc59def92007-05-16 22:10:50 -07003092 size, align, flags, ctor)) {
Christoph Lameter81819f02007-05-06 14:49:36 -07003093 list_add(&s->list, &slab_caches);
Christoph Lametera0e1d1b2007-07-17 04:03:31 -07003094 up_write(&slub_lock);
3095 if (sysfs_slab_add(s))
3096 goto err;
3097 return s;
3098 }
3099 kfree(s);
Christoph Lameter81819f02007-05-06 14:49:36 -07003100 }
3101 up_write(&slub_lock);
Christoph Lameter81819f02007-05-06 14:49:36 -07003102
3103err:
Christoph Lameter81819f02007-05-06 14:49:36 -07003104 if (flags & SLAB_PANIC)
3105 panic("Cannot create slabcache %s\n", name);
3106 else
3107 s = NULL;
3108 return s;
3109}
3110EXPORT_SYMBOL(kmem_cache_create);
3111
Christoph Lameter81819f02007-05-06 14:49:36 -07003112#ifdef CONFIG_SMP
Christoph Lameter27390bc2007-06-01 00:47:09 -07003113/*
Christoph Lameter672bba32007-05-09 02:32:39 -07003114 * Use the cpu notifier to insure that the cpu slabs are flushed when
3115 * necessary.
Christoph Lameter81819f02007-05-06 14:49:36 -07003116 */
3117static int __cpuinit slab_cpuup_callback(struct notifier_block *nfb,
3118 unsigned long action, void *hcpu)
3119{
3120 long cpu = (long)hcpu;
Christoph Lameter5b95a4ac2007-07-17 04:03:19 -07003121 struct kmem_cache *s;
3122 unsigned long flags;
Christoph Lameter81819f02007-05-06 14:49:36 -07003123
3124 switch (action) {
Christoph Lameter4c93c3552007-10-16 01:26:08 -07003125 case CPU_UP_PREPARE:
3126 case CPU_UP_PREPARE_FROZEN:
3127 init_alloc_cpu_cpu(cpu);
3128 down_read(&slub_lock);
3129 list_for_each_entry(s, &slab_caches, list)
3130 s->cpu_slab[cpu] = alloc_kmem_cache_cpu(s, cpu,
3131 GFP_KERNEL);
3132 up_read(&slub_lock);
3133 break;
3134
Christoph Lameter81819f02007-05-06 14:49:36 -07003135 case CPU_UP_CANCELED:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07003136 case CPU_UP_CANCELED_FROZEN:
Christoph Lameter81819f02007-05-06 14:49:36 -07003137 case CPU_DEAD:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07003138 case CPU_DEAD_FROZEN:
Christoph Lameter5b95a4ac2007-07-17 04:03:19 -07003139 down_read(&slub_lock);
3140 list_for_each_entry(s, &slab_caches, list) {
Christoph Lameter4c93c3552007-10-16 01:26:08 -07003141 struct kmem_cache_cpu *c = get_cpu_slab(s, cpu);
3142
Christoph Lameter5b95a4ac2007-07-17 04:03:19 -07003143 local_irq_save(flags);
3144 __flush_cpu_slab(s, cpu);
3145 local_irq_restore(flags);
Christoph Lameter4c93c3552007-10-16 01:26:08 -07003146 free_kmem_cache_cpu(c, cpu);
3147 s->cpu_slab[cpu] = NULL;
Christoph Lameter5b95a4ac2007-07-17 04:03:19 -07003148 }
3149 up_read(&slub_lock);
Christoph Lameter81819f02007-05-06 14:49:36 -07003150 break;
3151 default:
3152 break;
3153 }
3154 return NOTIFY_OK;
3155}
3156
Pekka Enberg06428782008-01-07 23:20:27 -08003157static struct notifier_block __cpuinitdata slab_notifier = {
Ingo Molnar3adbefe2008-02-05 17:57:39 -08003158 .notifier_call = slab_cpuup_callback
Pekka Enberg06428782008-01-07 23:20:27 -08003159};
Christoph Lameter81819f02007-05-06 14:49:36 -07003160
3161#endif
3162
Christoph Lameter81819f02007-05-06 14:49:36 -07003163void *__kmalloc_track_caller(size_t size, gfp_t gfpflags, void *caller)
3164{
Christoph Lameteraadb4bc2007-10-16 01:24:38 -07003165 struct kmem_cache *s;
3166
Christoph Lameter331dc552008-02-14 14:28:09 -08003167 if (unlikely(size > PAGE_SIZE))
Pekka Enbergeada35e2008-02-11 22:47:46 +02003168 return kmalloc_large(size, gfpflags);
3169
Christoph Lameteraadb4bc2007-10-16 01:24:38 -07003170 s = get_slab(size, gfpflags);
Christoph Lameter81819f02007-05-06 14:49:36 -07003171
Satyam Sharma2408c552007-10-16 01:24:44 -07003172 if (unlikely(ZERO_OR_NULL_PTR(s)))
Christoph Lameter6cb8f912007-07-17 04:03:22 -07003173 return s;
Christoph Lameter81819f02007-05-06 14:49:36 -07003174
Christoph Lameterce15fea2007-07-17 04:03:28 -07003175 return slab_alloc(s, gfpflags, -1, caller);
Christoph Lameter81819f02007-05-06 14:49:36 -07003176}
3177
3178void *__kmalloc_node_track_caller(size_t size, gfp_t gfpflags,
3179 int node, void *caller)
3180{
Christoph Lameteraadb4bc2007-10-16 01:24:38 -07003181 struct kmem_cache *s;
3182
Christoph Lameter331dc552008-02-14 14:28:09 -08003183 if (unlikely(size > PAGE_SIZE))
Christoph Lameterf619cfe2008-03-01 13:56:40 -08003184 return kmalloc_large_node(size, gfpflags, node);
Pekka Enbergeada35e2008-02-11 22:47:46 +02003185
Christoph Lameteraadb4bc2007-10-16 01:24:38 -07003186 s = get_slab(size, gfpflags);
Christoph Lameter81819f02007-05-06 14:49:36 -07003187
Satyam Sharma2408c552007-10-16 01:24:44 -07003188 if (unlikely(ZERO_OR_NULL_PTR(s)))
Christoph Lameter6cb8f912007-07-17 04:03:22 -07003189 return s;
Christoph Lameter81819f02007-05-06 14:49:36 -07003190
Christoph Lameterce15fea2007-07-17 04:03:28 -07003191 return slab_alloc(s, gfpflags, node, caller);
Christoph Lameter81819f02007-05-06 14:49:36 -07003192}
3193
Christoph Lameter5b06c8532008-04-14 18:51:34 +03003194#if (defined(CONFIG_SYSFS) && defined(CONFIG_SLUB_DEBUG)) || defined(CONFIG_SLABINFO)
3195static unsigned long count_partial(struct kmem_cache_node *n)
3196{
3197 unsigned long flags;
3198 unsigned long x = 0;
3199 struct page *page;
3200
3201 spin_lock_irqsave(&n->list_lock, flags);
3202 list_for_each_entry(page, &n->partial, lru)
3203 x += page->inuse;
3204 spin_unlock_irqrestore(&n->list_lock, flags);
3205 return x;
3206}
3207#endif
3208
Christoph Lameter41ecc552007-05-09 02:32:44 -07003209#if defined(CONFIG_SYSFS) && defined(CONFIG_SLUB_DEBUG)
Christoph Lameter434e2452007-07-17 04:03:30 -07003210static int validate_slab(struct kmem_cache *s, struct page *page,
3211 unsigned long *map)
Christoph Lameter53e15af2007-05-06 14:49:43 -07003212{
3213 void *p;
Christoph Lametera973e9d2008-03-01 13:40:44 -08003214 void *addr = page_address(page);
Christoph Lameter53e15af2007-05-06 14:49:43 -07003215
3216 if (!check_slab(s, page) ||
3217 !on_freelist(s, page, NULL))
3218 return 0;
3219
3220 /* Now we know that a valid freelist exists */
3221 bitmap_zero(map, s->objects);
3222
Christoph Lameter7656c722007-05-09 02:32:40 -07003223 for_each_free_object(p, s, page->freelist) {
3224 set_bit(slab_index(p, s, addr), map);
Christoph Lameter53e15af2007-05-06 14:49:43 -07003225 if (!check_object(s, page, p, 0))
3226 return 0;
3227 }
3228
Christoph Lameter7656c722007-05-09 02:32:40 -07003229 for_each_object(p, s, addr)
3230 if (!test_bit(slab_index(p, s, addr), map))
Christoph Lameter53e15af2007-05-06 14:49:43 -07003231 if (!check_object(s, page, p, 1))
3232 return 0;
3233 return 1;
3234}
3235
Christoph Lameter434e2452007-07-17 04:03:30 -07003236static void validate_slab_slab(struct kmem_cache *s, struct page *page,
3237 unsigned long *map)
Christoph Lameter53e15af2007-05-06 14:49:43 -07003238{
3239 if (slab_trylock(page)) {
Christoph Lameter434e2452007-07-17 04:03:30 -07003240 validate_slab(s, page, map);
Christoph Lameter53e15af2007-05-06 14:49:43 -07003241 slab_unlock(page);
3242 } else
3243 printk(KERN_INFO "SLUB %s: Skipped busy slab 0x%p\n",
3244 s->name, page);
3245
3246 if (s->flags & DEBUG_DEFAULT_FLAGS) {
Christoph Lameter35e5d7e2007-05-09 02:32:42 -07003247 if (!SlabDebug(page))
3248 printk(KERN_ERR "SLUB %s: SlabDebug not set "
Christoph Lameter53e15af2007-05-06 14:49:43 -07003249 "on slab 0x%p\n", s->name, page);
3250 } else {
Christoph Lameter35e5d7e2007-05-09 02:32:42 -07003251 if (SlabDebug(page))
3252 printk(KERN_ERR "SLUB %s: SlabDebug set on "
Christoph Lameter53e15af2007-05-06 14:49:43 -07003253 "slab 0x%p\n", s->name, page);
3254 }
3255}
3256
Christoph Lameter434e2452007-07-17 04:03:30 -07003257static int validate_slab_node(struct kmem_cache *s,
3258 struct kmem_cache_node *n, unsigned long *map)
Christoph Lameter53e15af2007-05-06 14:49:43 -07003259{
3260 unsigned long count = 0;
3261 struct page *page;
3262 unsigned long flags;
3263
3264 spin_lock_irqsave(&n->list_lock, flags);
3265
3266 list_for_each_entry(page, &n->partial, lru) {
Christoph Lameter434e2452007-07-17 04:03:30 -07003267 validate_slab_slab(s, page, map);
Christoph Lameter53e15af2007-05-06 14:49:43 -07003268 count++;
3269 }
3270 if (count != n->nr_partial)
3271 printk(KERN_ERR "SLUB %s: %ld partial slabs counted but "
3272 "counter=%ld\n", s->name, count, n->nr_partial);
3273
3274 if (!(s->flags & SLAB_STORE_USER))
3275 goto out;
3276
3277 list_for_each_entry(page, &n->full, lru) {
Christoph Lameter434e2452007-07-17 04:03:30 -07003278 validate_slab_slab(s, page, map);
Christoph Lameter53e15af2007-05-06 14:49:43 -07003279 count++;
3280 }
3281 if (count != atomic_long_read(&n->nr_slabs))
3282 printk(KERN_ERR "SLUB: %s %ld slabs counted but "
3283 "counter=%ld\n", s->name, count,
3284 atomic_long_read(&n->nr_slabs));
3285
3286out:
3287 spin_unlock_irqrestore(&n->list_lock, flags);
3288 return count;
3289}
3290
Christoph Lameter434e2452007-07-17 04:03:30 -07003291static long validate_slab_cache(struct kmem_cache *s)
Christoph Lameter53e15af2007-05-06 14:49:43 -07003292{
3293 int node;
3294 unsigned long count = 0;
Christoph Lameter434e2452007-07-17 04:03:30 -07003295 unsigned long *map = kmalloc(BITS_TO_LONGS(s->objects) *
3296 sizeof(unsigned long), GFP_KERNEL);
3297
3298 if (!map)
3299 return -ENOMEM;
Christoph Lameter53e15af2007-05-06 14:49:43 -07003300
3301 flush_all(s);
Christoph Lameterf64dc582007-10-16 01:25:33 -07003302 for_each_node_state(node, N_NORMAL_MEMORY) {
Christoph Lameter53e15af2007-05-06 14:49:43 -07003303 struct kmem_cache_node *n = get_node(s, node);
3304
Christoph Lameter434e2452007-07-17 04:03:30 -07003305 count += validate_slab_node(s, n, map);
Christoph Lameter53e15af2007-05-06 14:49:43 -07003306 }
Christoph Lameter434e2452007-07-17 04:03:30 -07003307 kfree(map);
Christoph Lameter53e15af2007-05-06 14:49:43 -07003308 return count;
3309}
3310
Christoph Lameterb3459702007-05-09 02:32:41 -07003311#ifdef SLUB_RESILIENCY_TEST
3312static void resiliency_test(void)
3313{
3314 u8 *p;
3315
3316 printk(KERN_ERR "SLUB resiliency testing\n");
3317 printk(KERN_ERR "-----------------------\n");
3318 printk(KERN_ERR "A. Corruption after allocation\n");
3319
3320 p = kzalloc(16, GFP_KERNEL);
3321 p[16] = 0x12;
3322 printk(KERN_ERR "\n1. kmalloc-16: Clobber Redzone/next pointer"
3323 " 0x12->0x%p\n\n", p + 16);
3324
3325 validate_slab_cache(kmalloc_caches + 4);
3326
3327 /* Hmmm... The next two are dangerous */
3328 p = kzalloc(32, GFP_KERNEL);
3329 p[32 + sizeof(void *)] = 0x34;
3330 printk(KERN_ERR "\n2. kmalloc-32: Clobber next pointer/next slab"
Ingo Molnar3adbefe2008-02-05 17:57:39 -08003331 " 0x34 -> -0x%p\n", p);
3332 printk(KERN_ERR
3333 "If allocated object is overwritten then not detectable\n\n");
Christoph Lameterb3459702007-05-09 02:32:41 -07003334
3335 validate_slab_cache(kmalloc_caches + 5);
3336 p = kzalloc(64, GFP_KERNEL);
3337 p += 64 + (get_cycles() & 0xff) * sizeof(void *);
3338 *p = 0x56;
3339 printk(KERN_ERR "\n3. kmalloc-64: corrupting random byte 0x56->0x%p\n",
3340 p);
Ingo Molnar3adbefe2008-02-05 17:57:39 -08003341 printk(KERN_ERR
3342 "If allocated object is overwritten then not detectable\n\n");
Christoph Lameterb3459702007-05-09 02:32:41 -07003343 validate_slab_cache(kmalloc_caches + 6);
3344
3345 printk(KERN_ERR "\nB. Corruption after free\n");
3346 p = kzalloc(128, GFP_KERNEL);
3347 kfree(p);
3348 *p = 0x78;
3349 printk(KERN_ERR "1. kmalloc-128: Clobber first word 0x78->0x%p\n\n", p);
3350 validate_slab_cache(kmalloc_caches + 7);
3351
3352 p = kzalloc(256, GFP_KERNEL);
3353 kfree(p);
3354 p[50] = 0x9a;
Ingo Molnar3adbefe2008-02-05 17:57:39 -08003355 printk(KERN_ERR "\n2. kmalloc-256: Clobber 50th byte 0x9a->0x%p\n\n",
3356 p);
Christoph Lameterb3459702007-05-09 02:32:41 -07003357 validate_slab_cache(kmalloc_caches + 8);
3358
3359 p = kzalloc(512, GFP_KERNEL);
3360 kfree(p);
3361 p[512] = 0xab;
3362 printk(KERN_ERR "\n3. kmalloc-512: Clobber redzone 0xab->0x%p\n\n", p);
3363 validate_slab_cache(kmalloc_caches + 9);
3364}
3365#else
3366static void resiliency_test(void) {};
3367#endif
3368
Christoph Lameter88a420e2007-05-06 14:49:45 -07003369/*
Christoph Lameter672bba32007-05-09 02:32:39 -07003370 * Generate lists of code addresses where slabcache objects are allocated
Christoph Lameter88a420e2007-05-06 14:49:45 -07003371 * and freed.
3372 */
3373
3374struct location {
3375 unsigned long count;
3376 void *addr;
Christoph Lameter45edfa52007-05-09 02:32:45 -07003377 long long sum_time;
3378 long min_time;
3379 long max_time;
3380 long min_pid;
3381 long max_pid;
3382 cpumask_t cpus;
3383 nodemask_t nodes;
Christoph Lameter88a420e2007-05-06 14:49:45 -07003384};
3385
3386struct loc_track {
3387 unsigned long max;
3388 unsigned long count;
3389 struct location *loc;
3390};
3391
3392static void free_loc_track(struct loc_track *t)
3393{
3394 if (t->max)
3395 free_pages((unsigned long)t->loc,
3396 get_order(sizeof(struct location) * t->max));
3397}
3398
Christoph Lameter68dff6a2007-07-17 04:03:20 -07003399static int alloc_loc_track(struct loc_track *t, unsigned long max, gfp_t flags)
Christoph Lameter88a420e2007-05-06 14:49:45 -07003400{
3401 struct location *l;
3402 int order;
3403
Christoph Lameter88a420e2007-05-06 14:49:45 -07003404 order = get_order(sizeof(struct location) * max);
3405
Christoph Lameter68dff6a2007-07-17 04:03:20 -07003406 l = (void *)__get_free_pages(flags, order);
Christoph Lameter88a420e2007-05-06 14:49:45 -07003407 if (!l)
3408 return 0;
3409
3410 if (t->count) {
3411 memcpy(l, t->loc, sizeof(struct location) * t->count);
3412 free_loc_track(t);
3413 }
3414 t->max = max;
3415 t->loc = l;
3416 return 1;
3417}
3418
3419static int add_location(struct loc_track *t, struct kmem_cache *s,
Christoph Lameter45edfa52007-05-09 02:32:45 -07003420 const struct track *track)
Christoph Lameter88a420e2007-05-06 14:49:45 -07003421{
3422 long start, end, pos;
3423 struct location *l;
3424 void *caddr;
Christoph Lameter45edfa52007-05-09 02:32:45 -07003425 unsigned long age = jiffies - track->when;
Christoph Lameter88a420e2007-05-06 14:49:45 -07003426
3427 start = -1;
3428 end = t->count;
3429
3430 for ( ; ; ) {
3431 pos = start + (end - start + 1) / 2;
3432
3433 /*
3434 * There is nothing at "end". If we end up there
3435 * we need to add something to before end.
3436 */
3437 if (pos == end)
3438 break;
3439
3440 caddr = t->loc[pos].addr;
Christoph Lameter45edfa52007-05-09 02:32:45 -07003441 if (track->addr == caddr) {
3442
3443 l = &t->loc[pos];
3444 l->count++;
3445 if (track->when) {
3446 l->sum_time += age;
3447 if (age < l->min_time)
3448 l->min_time = age;
3449 if (age > l->max_time)
3450 l->max_time = age;
3451
3452 if (track->pid < l->min_pid)
3453 l->min_pid = track->pid;
3454 if (track->pid > l->max_pid)
3455 l->max_pid = track->pid;
3456
3457 cpu_set(track->cpu, l->cpus);
3458 }
3459 node_set(page_to_nid(virt_to_page(track)), l->nodes);
Christoph Lameter88a420e2007-05-06 14:49:45 -07003460 return 1;
3461 }
3462
Christoph Lameter45edfa52007-05-09 02:32:45 -07003463 if (track->addr < caddr)
Christoph Lameter88a420e2007-05-06 14:49:45 -07003464 end = pos;
3465 else
3466 start = pos;
3467 }
3468
3469 /*
Christoph Lameter672bba32007-05-09 02:32:39 -07003470 * Not found. Insert new tracking element.
Christoph Lameter88a420e2007-05-06 14:49:45 -07003471 */
Christoph Lameter68dff6a2007-07-17 04:03:20 -07003472 if (t->count >= t->max && !alloc_loc_track(t, 2 * t->max, GFP_ATOMIC))
Christoph Lameter88a420e2007-05-06 14:49:45 -07003473 return 0;
3474
3475 l = t->loc + pos;
3476 if (pos < t->count)
3477 memmove(l + 1, l,
3478 (t->count - pos) * sizeof(struct location));
3479 t->count++;
3480 l->count = 1;
Christoph Lameter45edfa52007-05-09 02:32:45 -07003481 l->addr = track->addr;
3482 l->sum_time = age;
3483 l->min_time = age;
3484 l->max_time = age;
3485 l->min_pid = track->pid;
3486 l->max_pid = track->pid;
3487 cpus_clear(l->cpus);
3488 cpu_set(track->cpu, l->cpus);
3489 nodes_clear(l->nodes);
3490 node_set(page_to_nid(virt_to_page(track)), l->nodes);
Christoph Lameter88a420e2007-05-06 14:49:45 -07003491 return 1;
3492}
3493
3494static void process_slab(struct loc_track *t, struct kmem_cache *s,
3495 struct page *page, enum track_item alloc)
3496{
Christoph Lametera973e9d2008-03-01 13:40:44 -08003497 void *addr = page_address(page);
Christoph Lameter7656c722007-05-09 02:32:40 -07003498 DECLARE_BITMAP(map, s->objects);
Christoph Lameter88a420e2007-05-06 14:49:45 -07003499 void *p;
3500
3501 bitmap_zero(map, s->objects);
Christoph Lameter7656c722007-05-09 02:32:40 -07003502 for_each_free_object(p, s, page->freelist)
3503 set_bit(slab_index(p, s, addr), map);
Christoph Lameter88a420e2007-05-06 14:49:45 -07003504
Christoph Lameter7656c722007-05-09 02:32:40 -07003505 for_each_object(p, s, addr)
Christoph Lameter45edfa52007-05-09 02:32:45 -07003506 if (!test_bit(slab_index(p, s, addr), map))
3507 add_location(t, s, get_track(s, p, alloc));
Christoph Lameter88a420e2007-05-06 14:49:45 -07003508}
3509
3510static int list_locations(struct kmem_cache *s, char *buf,
3511 enum track_item alloc)
3512{
Harvey Harrisone374d482008-01-31 15:20:50 -08003513 int len = 0;
Christoph Lameter88a420e2007-05-06 14:49:45 -07003514 unsigned long i;
Christoph Lameter68dff6a2007-07-17 04:03:20 -07003515 struct loc_track t = { 0, 0, NULL };
Christoph Lameter88a420e2007-05-06 14:49:45 -07003516 int node;
3517
Christoph Lameter68dff6a2007-07-17 04:03:20 -07003518 if (!alloc_loc_track(&t, PAGE_SIZE / sizeof(struct location),
Andrew Mortonea3061d2007-10-16 01:26:09 -07003519 GFP_TEMPORARY))
Christoph Lameter68dff6a2007-07-17 04:03:20 -07003520 return sprintf(buf, "Out of memory\n");
Christoph Lameter88a420e2007-05-06 14:49:45 -07003521
3522 /* Push back cpu slabs */
3523 flush_all(s);
3524
Christoph Lameterf64dc582007-10-16 01:25:33 -07003525 for_each_node_state(node, N_NORMAL_MEMORY) {
Christoph Lameter88a420e2007-05-06 14:49:45 -07003526 struct kmem_cache_node *n = get_node(s, node);
3527 unsigned long flags;
3528 struct page *page;
3529
Christoph Lameter9e869432007-08-22 14:01:56 -07003530 if (!atomic_long_read(&n->nr_slabs))
Christoph Lameter88a420e2007-05-06 14:49:45 -07003531 continue;
3532
3533 spin_lock_irqsave(&n->list_lock, flags);
3534 list_for_each_entry(page, &n->partial, lru)
3535 process_slab(&t, s, page, alloc);
3536 list_for_each_entry(page, &n->full, lru)
3537 process_slab(&t, s, page, alloc);
3538 spin_unlock_irqrestore(&n->list_lock, flags);
3539 }
3540
3541 for (i = 0; i < t.count; i++) {
Christoph Lameter45edfa52007-05-09 02:32:45 -07003542 struct location *l = &t.loc[i];
Christoph Lameter88a420e2007-05-06 14:49:45 -07003543
Harvey Harrisone374d482008-01-31 15:20:50 -08003544 if (len > PAGE_SIZE - 100)
Christoph Lameter88a420e2007-05-06 14:49:45 -07003545 break;
Harvey Harrisone374d482008-01-31 15:20:50 -08003546 len += sprintf(buf + len, "%7ld ", l->count);
Christoph Lameter45edfa52007-05-09 02:32:45 -07003547
3548 if (l->addr)
Harvey Harrisone374d482008-01-31 15:20:50 -08003549 len += sprint_symbol(buf + len, (unsigned long)l->addr);
Christoph Lameter88a420e2007-05-06 14:49:45 -07003550 else
Harvey Harrisone374d482008-01-31 15:20:50 -08003551 len += sprintf(buf + len, "<not-available>");
Christoph Lameter45edfa52007-05-09 02:32:45 -07003552
3553 if (l->sum_time != l->min_time) {
3554 unsigned long remainder;
3555
Harvey Harrisone374d482008-01-31 15:20:50 -08003556 len += sprintf(buf + len, " age=%ld/%ld/%ld",
Christoph Lameter45edfa52007-05-09 02:32:45 -07003557 l->min_time,
3558 div_long_long_rem(l->sum_time, l->count, &remainder),
3559 l->max_time);
3560 } else
Harvey Harrisone374d482008-01-31 15:20:50 -08003561 len += sprintf(buf + len, " age=%ld",
Christoph Lameter45edfa52007-05-09 02:32:45 -07003562 l->min_time);
3563
3564 if (l->min_pid != l->max_pid)
Harvey Harrisone374d482008-01-31 15:20:50 -08003565 len += sprintf(buf + len, " pid=%ld-%ld",
Christoph Lameter45edfa52007-05-09 02:32:45 -07003566 l->min_pid, l->max_pid);
3567 else
Harvey Harrisone374d482008-01-31 15:20:50 -08003568 len += sprintf(buf + len, " pid=%ld",
Christoph Lameter45edfa52007-05-09 02:32:45 -07003569 l->min_pid);
3570
Christoph Lameter84966342007-06-23 17:16:32 -07003571 if (num_online_cpus() > 1 && !cpus_empty(l->cpus) &&
Harvey Harrisone374d482008-01-31 15:20:50 -08003572 len < PAGE_SIZE - 60) {
3573 len += sprintf(buf + len, " cpus=");
3574 len += cpulist_scnprintf(buf + len, PAGE_SIZE - len - 50,
Christoph Lameter45edfa52007-05-09 02:32:45 -07003575 l->cpus);
3576 }
3577
Christoph Lameter84966342007-06-23 17:16:32 -07003578 if (num_online_nodes() > 1 && !nodes_empty(l->nodes) &&
Harvey Harrisone374d482008-01-31 15:20:50 -08003579 len < PAGE_SIZE - 60) {
3580 len += sprintf(buf + len, " nodes=");
3581 len += nodelist_scnprintf(buf + len, PAGE_SIZE - len - 50,
Christoph Lameter45edfa52007-05-09 02:32:45 -07003582 l->nodes);
3583 }
3584
Harvey Harrisone374d482008-01-31 15:20:50 -08003585 len += sprintf(buf + len, "\n");
Christoph Lameter88a420e2007-05-06 14:49:45 -07003586 }
3587
3588 free_loc_track(&t);
3589 if (!t.count)
Harvey Harrisone374d482008-01-31 15:20:50 -08003590 len += sprintf(buf, "No data\n");
3591 return len;
Christoph Lameter88a420e2007-05-06 14:49:45 -07003592}
3593
Christoph Lameter81819f02007-05-06 14:49:36 -07003594enum slab_stat_type {
3595 SL_FULL,
3596 SL_PARTIAL,
3597 SL_CPU,
3598 SL_OBJECTS
3599};
3600
3601#define SO_FULL (1 << SL_FULL)
3602#define SO_PARTIAL (1 << SL_PARTIAL)
3603#define SO_CPU (1 << SL_CPU)
3604#define SO_OBJECTS (1 << SL_OBJECTS)
3605
Cyrill Gorcunov62e5c4b2008-03-02 23:28:24 +03003606static ssize_t show_slab_objects(struct kmem_cache *s,
3607 char *buf, unsigned long flags)
Christoph Lameter81819f02007-05-06 14:49:36 -07003608{
3609 unsigned long total = 0;
3610 int cpu;
3611 int node;
3612 int x;
3613 unsigned long *nodes;
3614 unsigned long *per_cpu;
3615
3616 nodes = kzalloc(2 * sizeof(unsigned long) * nr_node_ids, GFP_KERNEL);
Cyrill Gorcunov62e5c4b2008-03-02 23:28:24 +03003617 if (!nodes)
3618 return -ENOMEM;
Christoph Lameter81819f02007-05-06 14:49:36 -07003619 per_cpu = nodes + nr_node_ids;
3620
3621 for_each_possible_cpu(cpu) {
Christoph Lameterdfb4f092007-10-16 01:26:05 -07003622 struct page *page;
3623 struct kmem_cache_cpu *c = get_cpu_slab(s, cpu);
Christoph Lameter81819f02007-05-06 14:49:36 -07003624
Christoph Lameterdfb4f092007-10-16 01:26:05 -07003625 if (!c)
3626 continue;
3627
3628 page = c->page;
Christoph Lameteree3c72a2007-10-16 01:26:07 -07003629 node = c->node;
3630 if (node < 0)
3631 continue;
Christoph Lameter81819f02007-05-06 14:49:36 -07003632 if (page) {
Christoph Lameter81819f02007-05-06 14:49:36 -07003633 if (flags & SO_CPU) {
Christoph Lameter81819f02007-05-06 14:49:36 -07003634 if (flags & SO_OBJECTS)
3635 x = page->inuse;
3636 else
3637 x = 1;
3638 total += x;
Christoph Lameteree3c72a2007-10-16 01:26:07 -07003639 nodes[node] += x;
Christoph Lameter81819f02007-05-06 14:49:36 -07003640 }
Christoph Lameteree3c72a2007-10-16 01:26:07 -07003641 per_cpu[node]++;
Christoph Lameter81819f02007-05-06 14:49:36 -07003642 }
3643 }
3644
Christoph Lameterf64dc582007-10-16 01:25:33 -07003645 for_each_node_state(node, N_NORMAL_MEMORY) {
Christoph Lameter81819f02007-05-06 14:49:36 -07003646 struct kmem_cache_node *n = get_node(s, node);
3647
3648 if (flags & SO_PARTIAL) {
3649 if (flags & SO_OBJECTS)
3650 x = count_partial(n);
3651 else
3652 x = n->nr_partial;
3653 total += x;
3654 nodes[node] += x;
3655 }
3656
3657 if (flags & SO_FULL) {
Christoph Lameter9e869432007-08-22 14:01:56 -07003658 int full_slabs = atomic_long_read(&n->nr_slabs)
Christoph Lameter81819f02007-05-06 14:49:36 -07003659 - per_cpu[node]
3660 - n->nr_partial;
3661
3662 if (flags & SO_OBJECTS)
3663 x = full_slabs * s->objects;
3664 else
3665 x = full_slabs;
3666 total += x;
3667 nodes[node] += x;
3668 }
3669 }
3670
3671 x = sprintf(buf, "%lu", total);
3672#ifdef CONFIG_NUMA
Christoph Lameterf64dc582007-10-16 01:25:33 -07003673 for_each_node_state(node, N_NORMAL_MEMORY)
Christoph Lameter81819f02007-05-06 14:49:36 -07003674 if (nodes[node])
3675 x += sprintf(buf + x, " N%d=%lu",
3676 node, nodes[node]);
3677#endif
3678 kfree(nodes);
3679 return x + sprintf(buf + x, "\n");
3680}
3681
3682static int any_slab_objects(struct kmem_cache *s)
3683{
3684 int node;
3685 int cpu;
3686
Christoph Lameterdfb4f092007-10-16 01:26:05 -07003687 for_each_possible_cpu(cpu) {
3688 struct kmem_cache_cpu *c = get_cpu_slab(s, cpu);
Christoph Lameter81819f02007-05-06 14:49:36 -07003689
Christoph Lameterdfb4f092007-10-16 01:26:05 -07003690 if (c && c->page)
3691 return 1;
3692 }
3693
3694 for_each_online_node(node) {
Christoph Lameter81819f02007-05-06 14:49:36 -07003695 struct kmem_cache_node *n = get_node(s, node);
3696
Christoph Lameterdfb4f092007-10-16 01:26:05 -07003697 if (!n)
3698 continue;
3699
Christoph Lameter9e869432007-08-22 14:01:56 -07003700 if (n->nr_partial || atomic_long_read(&n->nr_slabs))
Christoph Lameter81819f02007-05-06 14:49:36 -07003701 return 1;
3702 }
3703 return 0;
3704}
3705
3706#define to_slab_attr(n) container_of(n, struct slab_attribute, attr)
3707#define to_slab(n) container_of(n, struct kmem_cache, kobj);
3708
3709struct slab_attribute {
3710 struct attribute attr;
3711 ssize_t (*show)(struct kmem_cache *s, char *buf);
3712 ssize_t (*store)(struct kmem_cache *s, const char *x, size_t count);
3713};
3714
3715#define SLAB_ATTR_RO(_name) \
3716 static struct slab_attribute _name##_attr = __ATTR_RO(_name)
3717
3718#define SLAB_ATTR(_name) \
3719 static struct slab_attribute _name##_attr = \
3720 __ATTR(_name, 0644, _name##_show, _name##_store)
3721
Christoph Lameter81819f02007-05-06 14:49:36 -07003722static ssize_t slab_size_show(struct kmem_cache *s, char *buf)
3723{
3724 return sprintf(buf, "%d\n", s->size);
3725}
3726SLAB_ATTR_RO(slab_size);
3727
3728static ssize_t align_show(struct kmem_cache *s, char *buf)
3729{
3730 return sprintf(buf, "%d\n", s->align);
3731}
3732SLAB_ATTR_RO(align);
3733
3734static ssize_t object_size_show(struct kmem_cache *s, char *buf)
3735{
3736 return sprintf(buf, "%d\n", s->objsize);
3737}
3738SLAB_ATTR_RO(object_size);
3739
3740static ssize_t objs_per_slab_show(struct kmem_cache *s, char *buf)
3741{
3742 return sprintf(buf, "%d\n", s->objects);
3743}
3744SLAB_ATTR_RO(objs_per_slab);
3745
3746static ssize_t order_show(struct kmem_cache *s, char *buf)
3747{
3748 return sprintf(buf, "%d\n", s->order);
3749}
3750SLAB_ATTR_RO(order);
3751
3752static ssize_t ctor_show(struct kmem_cache *s, char *buf)
3753{
3754 if (s->ctor) {
3755 int n = sprint_symbol(buf, (unsigned long)s->ctor);
3756
3757 return n + sprintf(buf + n, "\n");
3758 }
3759 return 0;
3760}
3761SLAB_ATTR_RO(ctor);
3762
Christoph Lameter81819f02007-05-06 14:49:36 -07003763static ssize_t aliases_show(struct kmem_cache *s, char *buf)
3764{
3765 return sprintf(buf, "%d\n", s->refcount - 1);
3766}
3767SLAB_ATTR_RO(aliases);
3768
3769static ssize_t slabs_show(struct kmem_cache *s, char *buf)
3770{
Christoph Lameterd9acf4b2008-02-15 15:22:21 -08003771 return show_slab_objects(s, buf, SO_FULL|SO_PARTIAL|SO_CPU);
Christoph Lameter81819f02007-05-06 14:49:36 -07003772}
3773SLAB_ATTR_RO(slabs);
3774
3775static ssize_t partial_show(struct kmem_cache *s, char *buf)
3776{
Christoph Lameterd9acf4b2008-02-15 15:22:21 -08003777 return show_slab_objects(s, buf, SO_PARTIAL);
Christoph Lameter81819f02007-05-06 14:49:36 -07003778}
3779SLAB_ATTR_RO(partial);
3780
3781static ssize_t cpu_slabs_show(struct kmem_cache *s, char *buf)
3782{
Christoph Lameterd9acf4b2008-02-15 15:22:21 -08003783 return show_slab_objects(s, buf, SO_CPU);
Christoph Lameter81819f02007-05-06 14:49:36 -07003784}
3785SLAB_ATTR_RO(cpu_slabs);
3786
3787static ssize_t objects_show(struct kmem_cache *s, char *buf)
3788{
Christoph Lameterd9acf4b2008-02-15 15:22:21 -08003789 return show_slab_objects(s, buf, SO_FULL|SO_PARTIAL|SO_CPU|SO_OBJECTS);
Christoph Lameter81819f02007-05-06 14:49:36 -07003790}
3791SLAB_ATTR_RO(objects);
3792
3793static ssize_t sanity_checks_show(struct kmem_cache *s, char *buf)
3794{
3795 return sprintf(buf, "%d\n", !!(s->flags & SLAB_DEBUG_FREE));
3796}
3797
3798static ssize_t sanity_checks_store(struct kmem_cache *s,
3799 const char *buf, size_t length)
3800{
3801 s->flags &= ~SLAB_DEBUG_FREE;
3802 if (buf[0] == '1')
3803 s->flags |= SLAB_DEBUG_FREE;
3804 return length;
3805}
3806SLAB_ATTR(sanity_checks);
3807
3808static ssize_t trace_show(struct kmem_cache *s, char *buf)
3809{
3810 return sprintf(buf, "%d\n", !!(s->flags & SLAB_TRACE));
3811}
3812
3813static ssize_t trace_store(struct kmem_cache *s, const char *buf,
3814 size_t length)
3815{
3816 s->flags &= ~SLAB_TRACE;
3817 if (buf[0] == '1')
3818 s->flags |= SLAB_TRACE;
3819 return length;
3820}
3821SLAB_ATTR(trace);
3822
3823static ssize_t reclaim_account_show(struct kmem_cache *s, char *buf)
3824{
3825 return sprintf(buf, "%d\n", !!(s->flags & SLAB_RECLAIM_ACCOUNT));
3826}
3827
3828static ssize_t reclaim_account_store(struct kmem_cache *s,
3829 const char *buf, size_t length)
3830{
3831 s->flags &= ~SLAB_RECLAIM_ACCOUNT;
3832 if (buf[0] == '1')
3833 s->flags |= SLAB_RECLAIM_ACCOUNT;
3834 return length;
3835}
3836SLAB_ATTR(reclaim_account);
3837
3838static ssize_t hwcache_align_show(struct kmem_cache *s, char *buf)
3839{
Christoph Lameter5af60832007-05-06 14:49:56 -07003840 return sprintf(buf, "%d\n", !!(s->flags & SLAB_HWCACHE_ALIGN));
Christoph Lameter81819f02007-05-06 14:49:36 -07003841}
3842SLAB_ATTR_RO(hwcache_align);
3843
3844#ifdef CONFIG_ZONE_DMA
3845static ssize_t cache_dma_show(struct kmem_cache *s, char *buf)
3846{
3847 return sprintf(buf, "%d\n", !!(s->flags & SLAB_CACHE_DMA));
3848}
3849SLAB_ATTR_RO(cache_dma);
3850#endif
3851
3852static ssize_t destroy_by_rcu_show(struct kmem_cache *s, char *buf)
3853{
3854 return sprintf(buf, "%d\n", !!(s->flags & SLAB_DESTROY_BY_RCU));
3855}
3856SLAB_ATTR_RO(destroy_by_rcu);
3857
3858static ssize_t red_zone_show(struct kmem_cache *s, char *buf)
3859{
3860 return sprintf(buf, "%d\n", !!(s->flags & SLAB_RED_ZONE));
3861}
3862
3863static ssize_t red_zone_store(struct kmem_cache *s,
3864 const char *buf, size_t length)
3865{
3866 if (any_slab_objects(s))
3867 return -EBUSY;
3868
3869 s->flags &= ~SLAB_RED_ZONE;
3870 if (buf[0] == '1')
3871 s->flags |= SLAB_RED_ZONE;
3872 calculate_sizes(s);
3873 return length;
3874}
3875SLAB_ATTR(red_zone);
3876
3877static ssize_t poison_show(struct kmem_cache *s, char *buf)
3878{
3879 return sprintf(buf, "%d\n", !!(s->flags & SLAB_POISON));
3880}
3881
3882static ssize_t poison_store(struct kmem_cache *s,
3883 const char *buf, size_t length)
3884{
3885 if (any_slab_objects(s))
3886 return -EBUSY;
3887
3888 s->flags &= ~SLAB_POISON;
3889 if (buf[0] == '1')
3890 s->flags |= SLAB_POISON;
3891 calculate_sizes(s);
3892 return length;
3893}
3894SLAB_ATTR(poison);
3895
3896static ssize_t store_user_show(struct kmem_cache *s, char *buf)
3897{
3898 return sprintf(buf, "%d\n", !!(s->flags & SLAB_STORE_USER));
3899}
3900
3901static ssize_t store_user_store(struct kmem_cache *s,
3902 const char *buf, size_t length)
3903{
3904 if (any_slab_objects(s))
3905 return -EBUSY;
3906
3907 s->flags &= ~SLAB_STORE_USER;
3908 if (buf[0] == '1')
3909 s->flags |= SLAB_STORE_USER;
3910 calculate_sizes(s);
3911 return length;
3912}
3913SLAB_ATTR(store_user);
3914
Christoph Lameter53e15af2007-05-06 14:49:43 -07003915static ssize_t validate_show(struct kmem_cache *s, char *buf)
3916{
3917 return 0;
3918}
3919
3920static ssize_t validate_store(struct kmem_cache *s,
3921 const char *buf, size_t length)
3922{
Christoph Lameter434e2452007-07-17 04:03:30 -07003923 int ret = -EINVAL;
3924
3925 if (buf[0] == '1') {
3926 ret = validate_slab_cache(s);
3927 if (ret >= 0)
3928 ret = length;
3929 }
3930 return ret;
Christoph Lameter53e15af2007-05-06 14:49:43 -07003931}
3932SLAB_ATTR(validate);
3933
Christoph Lameter2086d262007-05-06 14:49:46 -07003934static ssize_t shrink_show(struct kmem_cache *s, char *buf)
3935{
3936 return 0;
3937}
3938
3939static ssize_t shrink_store(struct kmem_cache *s,
3940 const char *buf, size_t length)
3941{
3942 if (buf[0] == '1') {
3943 int rc = kmem_cache_shrink(s);
3944
3945 if (rc)
3946 return rc;
3947 } else
3948 return -EINVAL;
3949 return length;
3950}
3951SLAB_ATTR(shrink);
3952
Christoph Lameter88a420e2007-05-06 14:49:45 -07003953static ssize_t alloc_calls_show(struct kmem_cache *s, char *buf)
3954{
3955 if (!(s->flags & SLAB_STORE_USER))
3956 return -ENOSYS;
3957 return list_locations(s, buf, TRACK_ALLOC);
3958}
3959SLAB_ATTR_RO(alloc_calls);
3960
3961static ssize_t free_calls_show(struct kmem_cache *s, char *buf)
3962{
3963 if (!(s->flags & SLAB_STORE_USER))
3964 return -ENOSYS;
3965 return list_locations(s, buf, TRACK_FREE);
3966}
3967SLAB_ATTR_RO(free_calls);
3968
Christoph Lameter81819f02007-05-06 14:49:36 -07003969#ifdef CONFIG_NUMA
Christoph Lameter98246012008-01-07 23:20:26 -08003970static ssize_t remote_node_defrag_ratio_show(struct kmem_cache *s, char *buf)
Christoph Lameter81819f02007-05-06 14:49:36 -07003971{
Christoph Lameter98246012008-01-07 23:20:26 -08003972 return sprintf(buf, "%d\n", s->remote_node_defrag_ratio / 10);
Christoph Lameter81819f02007-05-06 14:49:36 -07003973}
3974
Christoph Lameter98246012008-01-07 23:20:26 -08003975static ssize_t remote_node_defrag_ratio_store(struct kmem_cache *s,
Christoph Lameter81819f02007-05-06 14:49:36 -07003976 const char *buf, size_t length)
3977{
3978 int n = simple_strtoul(buf, NULL, 10);
3979
3980 if (n < 100)
Christoph Lameter98246012008-01-07 23:20:26 -08003981 s->remote_node_defrag_ratio = n * 10;
Christoph Lameter81819f02007-05-06 14:49:36 -07003982 return length;
3983}
Christoph Lameter98246012008-01-07 23:20:26 -08003984SLAB_ATTR(remote_node_defrag_ratio);
Christoph Lameter81819f02007-05-06 14:49:36 -07003985#endif
3986
Christoph Lameter8ff12cf2008-02-07 17:47:41 -08003987#ifdef CONFIG_SLUB_STATS
Christoph Lameter8ff12cf2008-02-07 17:47:41 -08003988static int show_stat(struct kmem_cache *s, char *buf, enum stat_item si)
3989{
3990 unsigned long sum = 0;
3991 int cpu;
3992 int len;
3993 int *data = kmalloc(nr_cpu_ids * sizeof(int), GFP_KERNEL);
3994
3995 if (!data)
3996 return -ENOMEM;
3997
3998 for_each_online_cpu(cpu) {
3999 unsigned x = get_cpu_slab(s, cpu)->stat[si];
4000
4001 data[cpu] = x;
4002 sum += x;
4003 }
4004
4005 len = sprintf(buf, "%lu", sum);
4006
Christoph Lameter50ef37b2008-04-14 18:52:05 +03004007#ifdef CONFIG_SMP
Christoph Lameter8ff12cf2008-02-07 17:47:41 -08004008 for_each_online_cpu(cpu) {
4009 if (data[cpu] && len < PAGE_SIZE - 20)
Christoph Lameter50ef37b2008-04-14 18:52:05 +03004010 len += sprintf(buf + len, " C%d=%u", cpu, data[cpu]);
Christoph Lameter8ff12cf2008-02-07 17:47:41 -08004011 }
Christoph Lameter50ef37b2008-04-14 18:52:05 +03004012#endif
Christoph Lameter8ff12cf2008-02-07 17:47:41 -08004013 kfree(data);
4014 return len + sprintf(buf + len, "\n");
4015}
4016
4017#define STAT_ATTR(si, text) \
4018static ssize_t text##_show(struct kmem_cache *s, char *buf) \
4019{ \
4020 return show_stat(s, buf, si); \
4021} \
4022SLAB_ATTR_RO(text); \
4023
4024STAT_ATTR(ALLOC_FASTPATH, alloc_fastpath);
4025STAT_ATTR(ALLOC_SLOWPATH, alloc_slowpath);
4026STAT_ATTR(FREE_FASTPATH, free_fastpath);
4027STAT_ATTR(FREE_SLOWPATH, free_slowpath);
4028STAT_ATTR(FREE_FROZEN, free_frozen);
4029STAT_ATTR(FREE_ADD_PARTIAL, free_add_partial);
4030STAT_ATTR(FREE_REMOVE_PARTIAL, free_remove_partial);
4031STAT_ATTR(ALLOC_FROM_PARTIAL, alloc_from_partial);
4032STAT_ATTR(ALLOC_SLAB, alloc_slab);
4033STAT_ATTR(ALLOC_REFILL, alloc_refill);
4034STAT_ATTR(FREE_SLAB, free_slab);
4035STAT_ATTR(CPUSLAB_FLUSH, cpuslab_flush);
4036STAT_ATTR(DEACTIVATE_FULL, deactivate_full);
4037STAT_ATTR(DEACTIVATE_EMPTY, deactivate_empty);
4038STAT_ATTR(DEACTIVATE_TO_HEAD, deactivate_to_head);
4039STAT_ATTR(DEACTIVATE_TO_TAIL, deactivate_to_tail);
4040STAT_ATTR(DEACTIVATE_REMOTE_FREES, deactivate_remote_frees);
4041
4042#endif
4043
Pekka Enberg06428782008-01-07 23:20:27 -08004044static struct attribute *slab_attrs[] = {
Christoph Lameter81819f02007-05-06 14:49:36 -07004045 &slab_size_attr.attr,
4046 &object_size_attr.attr,
4047 &objs_per_slab_attr.attr,
4048 &order_attr.attr,
4049 &objects_attr.attr,
4050 &slabs_attr.attr,
4051 &partial_attr.attr,
4052 &cpu_slabs_attr.attr,
4053 &ctor_attr.attr,
Christoph Lameter81819f02007-05-06 14:49:36 -07004054 &aliases_attr.attr,
4055 &align_attr.attr,
4056 &sanity_checks_attr.attr,
4057 &trace_attr.attr,
4058 &hwcache_align_attr.attr,
4059 &reclaim_account_attr.attr,
4060 &destroy_by_rcu_attr.attr,
4061 &red_zone_attr.attr,
4062 &poison_attr.attr,
4063 &store_user_attr.attr,
Christoph Lameter53e15af2007-05-06 14:49:43 -07004064 &validate_attr.attr,
Christoph Lameter2086d262007-05-06 14:49:46 -07004065 &shrink_attr.attr,
Christoph Lameter88a420e2007-05-06 14:49:45 -07004066 &alloc_calls_attr.attr,
4067 &free_calls_attr.attr,
Christoph Lameter81819f02007-05-06 14:49:36 -07004068#ifdef CONFIG_ZONE_DMA
4069 &cache_dma_attr.attr,
4070#endif
4071#ifdef CONFIG_NUMA
Christoph Lameter98246012008-01-07 23:20:26 -08004072 &remote_node_defrag_ratio_attr.attr,
Christoph Lameter81819f02007-05-06 14:49:36 -07004073#endif
Christoph Lameter8ff12cf2008-02-07 17:47:41 -08004074#ifdef CONFIG_SLUB_STATS
4075 &alloc_fastpath_attr.attr,
4076 &alloc_slowpath_attr.attr,
4077 &free_fastpath_attr.attr,
4078 &free_slowpath_attr.attr,
4079 &free_frozen_attr.attr,
4080 &free_add_partial_attr.attr,
4081 &free_remove_partial_attr.attr,
4082 &alloc_from_partial_attr.attr,
4083 &alloc_slab_attr.attr,
4084 &alloc_refill_attr.attr,
4085 &free_slab_attr.attr,
4086 &cpuslab_flush_attr.attr,
4087 &deactivate_full_attr.attr,
4088 &deactivate_empty_attr.attr,
4089 &deactivate_to_head_attr.attr,
4090 &deactivate_to_tail_attr.attr,
4091 &deactivate_remote_frees_attr.attr,
4092#endif
Christoph Lameter81819f02007-05-06 14:49:36 -07004093 NULL
4094};
4095
4096static struct attribute_group slab_attr_group = {
4097 .attrs = slab_attrs,
4098};
4099
4100static ssize_t slab_attr_show(struct kobject *kobj,
4101 struct attribute *attr,
4102 char *buf)
4103{
4104 struct slab_attribute *attribute;
4105 struct kmem_cache *s;
4106 int err;
4107
4108 attribute = to_slab_attr(attr);
4109 s = to_slab(kobj);
4110
4111 if (!attribute->show)
4112 return -EIO;
4113
4114 err = attribute->show(s, buf);
4115
4116 return err;
4117}
4118
4119static ssize_t slab_attr_store(struct kobject *kobj,
4120 struct attribute *attr,
4121 const char *buf, size_t len)
4122{
4123 struct slab_attribute *attribute;
4124 struct kmem_cache *s;
4125 int err;
4126
4127 attribute = to_slab_attr(attr);
4128 s = to_slab(kobj);
4129
4130 if (!attribute->store)
4131 return -EIO;
4132
4133 err = attribute->store(s, buf, len);
4134
4135 return err;
4136}
4137
Christoph Lameter151c6022008-01-07 22:29:05 -08004138static void kmem_cache_release(struct kobject *kobj)
4139{
4140 struct kmem_cache *s = to_slab(kobj);
4141
4142 kfree(s);
4143}
4144
Christoph Lameter81819f02007-05-06 14:49:36 -07004145static struct sysfs_ops slab_sysfs_ops = {
4146 .show = slab_attr_show,
4147 .store = slab_attr_store,
4148};
4149
4150static struct kobj_type slab_ktype = {
4151 .sysfs_ops = &slab_sysfs_ops,
Christoph Lameter151c6022008-01-07 22:29:05 -08004152 .release = kmem_cache_release
Christoph Lameter81819f02007-05-06 14:49:36 -07004153};
4154
4155static int uevent_filter(struct kset *kset, struct kobject *kobj)
4156{
4157 struct kobj_type *ktype = get_ktype(kobj);
4158
4159 if (ktype == &slab_ktype)
4160 return 1;
4161 return 0;
4162}
4163
4164static struct kset_uevent_ops slab_uevent_ops = {
4165 .filter = uevent_filter,
4166};
4167
Greg Kroah-Hartman27c3a312007-11-01 09:29:06 -06004168static struct kset *slab_kset;
Christoph Lameter81819f02007-05-06 14:49:36 -07004169
4170#define ID_STR_LENGTH 64
4171
4172/* Create a unique string id for a slab cache:
Christoph Lameter6446faa2008-02-15 23:45:26 -08004173 *
4174 * Format :[flags-]size
Christoph Lameter81819f02007-05-06 14:49:36 -07004175 */
4176static char *create_unique_id(struct kmem_cache *s)
4177{
4178 char *name = kmalloc(ID_STR_LENGTH, GFP_KERNEL);
4179 char *p = name;
4180
4181 BUG_ON(!name);
4182
4183 *p++ = ':';
4184 /*
4185 * First flags affecting slabcache operations. We will only
4186 * get here for aliasable slabs so we do not need to support
4187 * too many flags. The flags here must cover all flags that
4188 * are matched during merging to guarantee that the id is
4189 * unique.
4190 */
4191 if (s->flags & SLAB_CACHE_DMA)
4192 *p++ = 'd';
4193 if (s->flags & SLAB_RECLAIM_ACCOUNT)
4194 *p++ = 'a';
4195 if (s->flags & SLAB_DEBUG_FREE)
4196 *p++ = 'F';
4197 if (p != name + 1)
4198 *p++ = '-';
4199 p += sprintf(p, "%07d", s->size);
4200 BUG_ON(p > name + ID_STR_LENGTH - 1);
4201 return name;
4202}
4203
4204static int sysfs_slab_add(struct kmem_cache *s)
4205{
4206 int err;
4207 const char *name;
4208 int unmergeable;
4209
4210 if (slab_state < SYSFS)
4211 /* Defer until later */
4212 return 0;
4213
4214 unmergeable = slab_unmergeable(s);
4215 if (unmergeable) {
4216 /*
4217 * Slabcache can never be merged so we can use the name proper.
4218 * This is typically the case for debug situations. In that
4219 * case we can catch duplicate names easily.
4220 */
Greg Kroah-Hartman27c3a312007-11-01 09:29:06 -06004221 sysfs_remove_link(&slab_kset->kobj, s->name);
Christoph Lameter81819f02007-05-06 14:49:36 -07004222 name = s->name;
4223 } else {
4224 /*
4225 * Create a unique name for the slab as a target
4226 * for the symlinks.
4227 */
4228 name = create_unique_id(s);
4229 }
4230
Greg Kroah-Hartman27c3a312007-11-01 09:29:06 -06004231 s->kobj.kset = slab_kset;
Greg Kroah-Hartman1eada112007-12-17 23:05:35 -07004232 err = kobject_init_and_add(&s->kobj, &slab_ktype, NULL, name);
4233 if (err) {
4234 kobject_put(&s->kobj);
Christoph Lameter81819f02007-05-06 14:49:36 -07004235 return err;
Greg Kroah-Hartman1eada112007-12-17 23:05:35 -07004236 }
Christoph Lameter81819f02007-05-06 14:49:36 -07004237
4238 err = sysfs_create_group(&s->kobj, &slab_attr_group);
4239 if (err)
4240 return err;
4241 kobject_uevent(&s->kobj, KOBJ_ADD);
4242 if (!unmergeable) {
4243 /* Setup first alias */
4244 sysfs_slab_alias(s, s->name);
4245 kfree(name);
4246 }
4247 return 0;
4248}
4249
4250static void sysfs_slab_remove(struct kmem_cache *s)
4251{
4252 kobject_uevent(&s->kobj, KOBJ_REMOVE);
4253 kobject_del(&s->kobj);
Christoph Lameter151c6022008-01-07 22:29:05 -08004254 kobject_put(&s->kobj);
Christoph Lameter81819f02007-05-06 14:49:36 -07004255}
4256
4257/*
4258 * Need to buffer aliases during bootup until sysfs becomes
4259 * available lest we loose that information.
4260 */
4261struct saved_alias {
4262 struct kmem_cache *s;
4263 const char *name;
4264 struct saved_alias *next;
4265};
4266
Adrian Bunk5af328a2007-07-17 04:03:27 -07004267static struct saved_alias *alias_list;
Christoph Lameter81819f02007-05-06 14:49:36 -07004268
4269static int sysfs_slab_alias(struct kmem_cache *s, const char *name)
4270{
4271 struct saved_alias *al;
4272
4273 if (slab_state == SYSFS) {
4274 /*
4275 * If we have a leftover link then remove it.
4276 */
Greg Kroah-Hartman27c3a312007-11-01 09:29:06 -06004277 sysfs_remove_link(&slab_kset->kobj, name);
4278 return sysfs_create_link(&slab_kset->kobj, &s->kobj, name);
Christoph Lameter81819f02007-05-06 14:49:36 -07004279 }
4280
4281 al = kmalloc(sizeof(struct saved_alias), GFP_KERNEL);
4282 if (!al)
4283 return -ENOMEM;
4284
4285 al->s = s;
4286 al->name = name;
4287 al->next = alias_list;
4288 alias_list = al;
4289 return 0;
4290}
4291
4292static int __init slab_sysfs_init(void)
4293{
Christoph Lameter5b95a4ac2007-07-17 04:03:19 -07004294 struct kmem_cache *s;
Christoph Lameter81819f02007-05-06 14:49:36 -07004295 int err;
4296
Greg Kroah-Hartman0ff21e42007-11-06 10:36:58 -08004297 slab_kset = kset_create_and_add("slab", &slab_uevent_ops, kernel_kobj);
Greg Kroah-Hartman27c3a312007-11-01 09:29:06 -06004298 if (!slab_kset) {
Christoph Lameter81819f02007-05-06 14:49:36 -07004299 printk(KERN_ERR "Cannot register slab subsystem.\n");
4300 return -ENOSYS;
4301 }
4302
Christoph Lameter26a7bd02007-05-09 02:32:39 -07004303 slab_state = SYSFS;
4304
Christoph Lameter5b95a4ac2007-07-17 04:03:19 -07004305 list_for_each_entry(s, &slab_caches, list) {
Christoph Lameter26a7bd02007-05-09 02:32:39 -07004306 err = sysfs_slab_add(s);
Christoph Lameter5d540fb2007-08-30 23:56:26 -07004307 if (err)
4308 printk(KERN_ERR "SLUB: Unable to add boot slab %s"
4309 " to sysfs\n", s->name);
Christoph Lameter26a7bd02007-05-09 02:32:39 -07004310 }
Christoph Lameter81819f02007-05-06 14:49:36 -07004311
4312 while (alias_list) {
4313 struct saved_alias *al = alias_list;
4314
4315 alias_list = alias_list->next;
4316 err = sysfs_slab_alias(al->s, al->name);
Christoph Lameter5d540fb2007-08-30 23:56:26 -07004317 if (err)
4318 printk(KERN_ERR "SLUB: Unable to add boot slab alias"
4319 " %s to sysfs\n", s->name);
Christoph Lameter81819f02007-05-06 14:49:36 -07004320 kfree(al);
4321 }
4322
4323 resiliency_test();
4324 return 0;
4325}
4326
4327__initcall(slab_sysfs_init);
Christoph Lameter81819f02007-05-06 14:49:36 -07004328#endif
Pekka J Enberg57ed3ed2008-01-01 17:23:28 +01004329
4330/*
4331 * The /proc/slabinfo ABI
4332 */
Linus Torvalds158a9622008-01-02 13:04:48 -08004333#ifdef CONFIG_SLABINFO
4334
4335ssize_t slabinfo_write(struct file *file, const char __user * buffer,
4336 size_t count, loff_t *ppos)
4337{
4338 return -EINVAL;
4339}
4340
Pekka J Enberg57ed3ed2008-01-01 17:23:28 +01004341
4342static void print_slabinfo_header(struct seq_file *m)
4343{
4344 seq_puts(m, "slabinfo - version: 2.1\n");
4345 seq_puts(m, "# name <active_objs> <num_objs> <objsize> "
4346 "<objperslab> <pagesperslab>");
4347 seq_puts(m, " : tunables <limit> <batchcount> <sharedfactor>");
4348 seq_puts(m, " : slabdata <active_slabs> <num_slabs> <sharedavail>");
4349 seq_putc(m, '\n');
4350}
4351
4352static void *s_start(struct seq_file *m, loff_t *pos)
4353{
4354 loff_t n = *pos;
4355
4356 down_read(&slub_lock);
4357 if (!n)
4358 print_slabinfo_header(m);
4359
4360 return seq_list_start(&slab_caches, *pos);
4361}
4362
4363static void *s_next(struct seq_file *m, void *p, loff_t *pos)
4364{
4365 return seq_list_next(p, &slab_caches, pos);
4366}
4367
4368static void s_stop(struct seq_file *m, void *p)
4369{
4370 up_read(&slub_lock);
4371}
4372
4373static int s_show(struct seq_file *m, void *p)
4374{
4375 unsigned long nr_partials = 0;
4376 unsigned long nr_slabs = 0;
4377 unsigned long nr_inuse = 0;
4378 unsigned long nr_objs;
4379 struct kmem_cache *s;
4380 int node;
4381
4382 s = list_entry(p, struct kmem_cache, list);
4383
4384 for_each_online_node(node) {
4385 struct kmem_cache_node *n = get_node(s, node);
4386
4387 if (!n)
4388 continue;
4389
4390 nr_partials += n->nr_partial;
4391 nr_slabs += atomic_long_read(&n->nr_slabs);
4392 nr_inuse += count_partial(n);
4393 }
4394
4395 nr_objs = nr_slabs * s->objects;
4396 nr_inuse += (nr_slabs - nr_partials) * s->objects;
4397
4398 seq_printf(m, "%-17s %6lu %6lu %6u %4u %4d", s->name, nr_inuse,
4399 nr_objs, s->size, s->objects, (1 << s->order));
4400 seq_printf(m, " : tunables %4u %4u %4u", 0, 0, 0);
4401 seq_printf(m, " : slabdata %6lu %6lu %6lu", nr_slabs, nr_slabs,
4402 0UL);
4403 seq_putc(m, '\n');
4404 return 0;
4405}
4406
4407const struct seq_operations slabinfo_op = {
4408 .start = s_start,
4409 .next = s_next,
4410 .stop = s_stop,
4411 .show = s_show,
4412};
4413
Linus Torvalds158a9622008-01-02 13:04:48 -08004414#endif /* CONFIG_SLABINFO */