blob: ccfd41141b6bd54b28cc412bfc7aa4583f869bd4 [file] [log] [blame]
Christoph Lameter81819f02007-05-06 14:49:36 -07001/*
2 * SLUB: A slab allocator that limits cache line use instead of queuing
3 * objects in per cpu and per node lists.
4 *
5 * The allocator synchronizes using per slab locks and only
6 * uses a centralized lock to manage a pool of partial slabs.
7 *
8 * (C) 2007 SGI, Christoph Lameter <clameter@sgi.com>
9 */
10
11#include <linux/mm.h>
12#include <linux/module.h>
13#include <linux/bit_spinlock.h>
14#include <linux/interrupt.h>
15#include <linux/bitops.h>
16#include <linux/slab.h>
17#include <linux/seq_file.h>
18#include <linux/cpu.h>
19#include <linux/cpuset.h>
20#include <linux/mempolicy.h>
21#include <linux/ctype.h>
22#include <linux/kallsyms.h>
Yasunori Gotob9049e22007-10-21 16:41:37 -070023#include <linux/memory.h>
Christoph Lameter81819f02007-05-06 14:49:36 -070024
25/*
26 * Lock order:
27 * 1. slab_lock(page)
28 * 2. slab->list_lock
29 *
30 * The slab_lock protects operations on the object of a particular
31 * slab and its metadata in the page struct. If the slab lock
32 * has been taken then no allocations nor frees can be performed
33 * on the objects in the slab nor can the slab be added or removed
34 * from the partial or full lists since this would mean modifying
35 * the page_struct of the slab.
36 *
37 * The list_lock protects the partial and full list on each node and
38 * the partial slab counter. If taken then no new slabs may be added or
39 * removed from the lists nor make the number of partial slabs be modified.
40 * (Note that the total number of slabs is an atomic value that may be
41 * modified without taking the list lock).
42 *
43 * The list_lock is a centralized lock and thus we avoid taking it as
44 * much as possible. As long as SLUB does not have to handle partial
45 * slabs, operations can continue without any centralized lock. F.e.
46 * allocating a long series of objects that fill up slabs does not require
47 * the list lock.
48 *
49 * The lock order is sometimes inverted when we are trying to get a slab
50 * off a list. We take the list_lock and then look for a page on the list
51 * to use. While we do that objects in the slabs may be freed. We can
52 * only operate on the slab if we have also taken the slab_lock. So we use
53 * a slab_trylock() on the slab. If trylock was successful then no frees
54 * can occur anymore and we can use the slab for allocations etc. If the
55 * slab_trylock() does not succeed then frees are in progress in the slab and
56 * we must stay away from it for a while since we may cause a bouncing
57 * cacheline if we try to acquire the lock. So go onto the next slab.
58 * If all pages are busy then we may allocate a new slab instead of reusing
59 * a partial slab. A new slab has noone operating on it and thus there is
60 * no danger of cacheline contention.
61 *
62 * Interrupts are disabled during allocation and deallocation in order to
63 * make the slab allocator safe to use in the context of an irq. In addition
64 * interrupts are disabled to ensure that the processor does not change
65 * while handling per_cpu slabs, due to kernel preemption.
66 *
67 * SLUB assigns one slab for allocation to each processor.
68 * Allocations only occur from these slabs called cpu slabs.
69 *
Christoph Lameter672bba32007-05-09 02:32:39 -070070 * Slabs with free elements are kept on a partial list and during regular
71 * operations no list for full slabs is used. If an object in a full slab is
Christoph Lameter81819f02007-05-06 14:49:36 -070072 * freed then the slab will show up again on the partial lists.
Christoph Lameter672bba32007-05-09 02:32:39 -070073 * We track full slabs for debugging purposes though because otherwise we
74 * cannot scan all objects.
Christoph Lameter81819f02007-05-06 14:49:36 -070075 *
76 * Slabs are freed when they become empty. Teardown and setup is
77 * minimal so we rely on the page allocators per cpu caches for
78 * fast frees and allocs.
79 *
80 * Overloading of page flags that are otherwise used for LRU management.
81 *
Christoph Lameter4b6f0752007-05-16 22:10:53 -070082 * PageActive The slab is frozen and exempt from list processing.
83 * This means that the slab is dedicated to a purpose
84 * such as satisfying allocations for a specific
85 * processor. Objects may be freed in the slab while
86 * it is frozen but slab_free will then skip the usual
87 * list operations. It is up to the processor holding
88 * the slab to integrate the slab into the slab lists
89 * when the slab is no longer needed.
90 *
91 * One use of this flag is to mark slabs that are
92 * used for allocations. Then such a slab becomes a cpu
93 * slab. The cpu slab may be equipped with an additional
Christoph Lameterdfb4f092007-10-16 01:26:05 -070094 * freelist that allows lockless access to
Christoph Lameter894b8782007-05-10 03:15:16 -070095 * free objects in addition to the regular freelist
96 * that requires the slab lock.
Christoph Lameter81819f02007-05-06 14:49:36 -070097 *
98 * PageError Slab requires special handling due to debug
99 * options set. This moves slab handling out of
Christoph Lameter894b8782007-05-10 03:15:16 -0700100 * the fast path and disables lockless freelists.
Christoph Lameter81819f02007-05-06 14:49:36 -0700101 */
102
Christoph Lameter5577bd82007-05-16 22:10:56 -0700103#define FROZEN (1 << PG_active)
104
105#ifdef CONFIG_SLUB_DEBUG
106#define SLABDEBUG (1 << PG_error)
107#else
108#define SLABDEBUG 0
109#endif
110
Christoph Lameter4b6f0752007-05-16 22:10:53 -0700111static inline int SlabFrozen(struct page *page)
112{
Christoph Lameter5577bd82007-05-16 22:10:56 -0700113 return page->flags & FROZEN;
Christoph Lameter4b6f0752007-05-16 22:10:53 -0700114}
115
116static inline void SetSlabFrozen(struct page *page)
117{
Christoph Lameter5577bd82007-05-16 22:10:56 -0700118 page->flags |= FROZEN;
Christoph Lameter4b6f0752007-05-16 22:10:53 -0700119}
120
121static inline void ClearSlabFrozen(struct page *page)
122{
Christoph Lameter5577bd82007-05-16 22:10:56 -0700123 page->flags &= ~FROZEN;
Christoph Lameter4b6f0752007-05-16 22:10:53 -0700124}
125
Christoph Lameter35e5d7e2007-05-09 02:32:42 -0700126static inline int SlabDebug(struct page *page)
127{
Christoph Lameter5577bd82007-05-16 22:10:56 -0700128 return page->flags & SLABDEBUG;
Christoph Lameter35e5d7e2007-05-09 02:32:42 -0700129}
130
131static inline void SetSlabDebug(struct page *page)
132{
Christoph Lameter5577bd82007-05-16 22:10:56 -0700133 page->flags |= SLABDEBUG;
Christoph Lameter35e5d7e2007-05-09 02:32:42 -0700134}
135
136static inline void ClearSlabDebug(struct page *page)
137{
Christoph Lameter5577bd82007-05-16 22:10:56 -0700138 page->flags &= ~SLABDEBUG;
Christoph Lameter35e5d7e2007-05-09 02:32:42 -0700139}
140
Christoph Lameter81819f02007-05-06 14:49:36 -0700141/*
142 * Issues still to be resolved:
143 *
Christoph Lameter81819f02007-05-06 14:49:36 -0700144 * - Support PAGE_ALLOC_DEBUG. Should be easy to do.
145 *
Christoph Lameter81819f02007-05-06 14:49:36 -0700146 * - Variable sizing of the per node arrays
147 */
148
149/* Enable to test recovery from slab corruption on boot */
150#undef SLUB_RESILIENCY_TEST
151
Christoph Lameter1f842602008-01-07 23:20:30 -0800152/*
153 * Currently fastpath is not supported if preemption is enabled.
154 */
155#if defined(CONFIG_FAST_CMPXCHG_LOCAL) && !defined(CONFIG_PREEMPT)
156#define SLUB_FASTPATH
157#endif
158
Christoph Lameter81819f02007-05-06 14:49:36 -0700159#if PAGE_SHIFT <= 12
160
161/*
162 * Small page size. Make sure that we do not fragment memory
163 */
164#define DEFAULT_MAX_ORDER 1
165#define DEFAULT_MIN_OBJECTS 4
166
167#else
168
169/*
170 * Large page machines are customarily able to handle larger
171 * page orders.
172 */
173#define DEFAULT_MAX_ORDER 2
174#define DEFAULT_MIN_OBJECTS 8
175
176#endif
177
178/*
Christoph Lameter2086d262007-05-06 14:49:46 -0700179 * Mininum number of partial slabs. These will be left on the partial
180 * lists even if they are empty. kmem_cache_shrink may reclaim them.
181 */
Christoph Lameter76be8952007-12-21 14:37:37 -0800182#define MIN_PARTIAL 5
Christoph Lametere95eed52007-05-06 14:49:44 -0700183
Christoph Lameter2086d262007-05-06 14:49:46 -0700184/*
185 * Maximum number of desirable partial slabs.
186 * The existence of more partial slabs makes kmem_cache_shrink
187 * sort the partial list by the number of objects in the.
188 */
189#define MAX_PARTIAL 10
190
Christoph Lameter81819f02007-05-06 14:49:36 -0700191#define DEBUG_DEFAULT_FLAGS (SLAB_DEBUG_FREE | SLAB_RED_ZONE | \
192 SLAB_POISON | SLAB_STORE_USER)
Christoph Lameter672bba32007-05-09 02:32:39 -0700193
Christoph Lameter81819f02007-05-06 14:49:36 -0700194/*
195 * Set of flags that will prevent slab merging
196 */
197#define SLUB_NEVER_MERGE (SLAB_RED_ZONE | SLAB_POISON | SLAB_STORE_USER | \
198 SLAB_TRACE | SLAB_DESTROY_BY_RCU)
199
200#define SLUB_MERGE_SAME (SLAB_DEBUG_FREE | SLAB_RECLAIM_ACCOUNT | \
201 SLAB_CACHE_DMA)
202
203#ifndef ARCH_KMALLOC_MINALIGN
Christoph Lameter47bfdc02007-05-06 14:49:37 -0700204#define ARCH_KMALLOC_MINALIGN __alignof__(unsigned long long)
Christoph Lameter81819f02007-05-06 14:49:36 -0700205#endif
206
207#ifndef ARCH_SLAB_MINALIGN
Christoph Lameter47bfdc02007-05-06 14:49:37 -0700208#define ARCH_SLAB_MINALIGN __alignof__(unsigned long long)
Christoph Lameter81819f02007-05-06 14:49:36 -0700209#endif
210
211/* Internal SLUB flags */
Christoph Lameter1ceef402007-08-07 15:11:48 -0700212#define __OBJECT_POISON 0x80000000 /* Poison object */
213#define __SYSFS_ADD_DEFERRED 0x40000000 /* Not yet visible via sysfs */
Christoph Lameter81819f02007-05-06 14:49:36 -0700214
Christoph Lameter65c02d42007-05-09 02:32:35 -0700215/* Not all arches define cache_line_size */
216#ifndef cache_line_size
217#define cache_line_size() L1_CACHE_BYTES
218#endif
219
Christoph Lameter81819f02007-05-06 14:49:36 -0700220static int kmem_size = sizeof(struct kmem_cache);
221
222#ifdef CONFIG_SMP
223static struct notifier_block slab_notifier;
224#endif
225
226static enum {
227 DOWN, /* No slab functionality available */
228 PARTIAL, /* kmem_cache_open() works but kmalloc does not */
Christoph Lameter672bba32007-05-09 02:32:39 -0700229 UP, /* Everything works but does not show up in sysfs */
Christoph Lameter81819f02007-05-06 14:49:36 -0700230 SYSFS /* Sysfs up */
231} slab_state = DOWN;
232
233/* A list of all slab caches on the system */
234static DECLARE_RWSEM(slub_lock);
Adrian Bunk5af328a2007-07-17 04:03:27 -0700235static LIST_HEAD(slab_caches);
Christoph Lameter81819f02007-05-06 14:49:36 -0700236
Christoph Lameter02cbc872007-05-09 02:32:43 -0700237/*
238 * Tracking user of a slab.
239 */
240struct track {
241 void *addr; /* Called from address */
242 int cpu; /* Was running on cpu */
243 int pid; /* Pid context */
244 unsigned long when; /* When did the operation occur */
245};
246
247enum track_item { TRACK_ALLOC, TRACK_FREE };
248
Christoph Lameter41ecc552007-05-09 02:32:44 -0700249#if defined(CONFIG_SYSFS) && defined(CONFIG_SLUB_DEBUG)
Christoph Lameter81819f02007-05-06 14:49:36 -0700250static int sysfs_slab_add(struct kmem_cache *);
251static int sysfs_slab_alias(struct kmem_cache *, const char *);
252static void sysfs_slab_remove(struct kmem_cache *);
Christoph Lameter8ff12cf2008-02-07 17:47:41 -0800253
Christoph Lameter81819f02007-05-06 14:49:36 -0700254#else
Christoph Lameter0c710012007-07-17 04:03:24 -0700255static inline int sysfs_slab_add(struct kmem_cache *s) { return 0; }
256static inline int sysfs_slab_alias(struct kmem_cache *s, const char *p)
257 { return 0; }
Christoph Lameter151c6022008-01-07 22:29:05 -0800258static inline void sysfs_slab_remove(struct kmem_cache *s)
259{
260 kfree(s);
261}
Christoph Lameter8ff12cf2008-02-07 17:47:41 -0800262
Christoph Lameter81819f02007-05-06 14:49:36 -0700263#endif
264
Christoph Lameter8ff12cf2008-02-07 17:47:41 -0800265static inline void stat(struct kmem_cache_cpu *c, enum stat_item si)
266{
267#ifdef CONFIG_SLUB_STATS
268 c->stat[si]++;
269#endif
270}
271
Christoph Lameter81819f02007-05-06 14:49:36 -0700272/********************************************************************
273 * Core slab cache functions
274 *******************************************************************/
275
276int slab_is_available(void)
277{
278 return slab_state >= UP;
279}
280
281static inline struct kmem_cache_node *get_node(struct kmem_cache *s, int node)
282{
283#ifdef CONFIG_NUMA
284 return s->node[node];
285#else
286 return &s->local_node;
287#endif
288}
289
Christoph Lameterdfb4f092007-10-16 01:26:05 -0700290static inline struct kmem_cache_cpu *get_cpu_slab(struct kmem_cache *s, int cpu)
291{
Christoph Lameter4c93c3552007-10-16 01:26:08 -0700292#ifdef CONFIG_SMP
293 return s->cpu_slab[cpu];
294#else
295 return &s->cpu_slab;
296#endif
Christoph Lameterdfb4f092007-10-16 01:26:05 -0700297}
298
Christoph Lameter683d0ba2008-01-07 23:20:29 -0800299/*
300 * The end pointer in a slab is special. It points to the first object in the
301 * slab but has bit 0 set to mark it.
302 *
303 * Note that SLUB relies on page_mapping returning NULL for pages with bit 0
304 * in the mapping set.
305 */
306static inline int is_end(void *addr)
307{
308 return (unsigned long)addr & PAGE_MAPPING_ANON;
309}
310
Adrian Bunkdada1232008-02-13 23:30:32 +0200311static void *slab_address(struct page *page)
Christoph Lameter683d0ba2008-01-07 23:20:29 -0800312{
313 return page->end - PAGE_MAPPING_ANON;
314}
315
Christoph Lameter02cbc872007-05-09 02:32:43 -0700316static inline int check_valid_pointer(struct kmem_cache *s,
317 struct page *page, const void *object)
318{
319 void *base;
320
Christoph Lameter683d0ba2008-01-07 23:20:29 -0800321 if (object == page->end)
Christoph Lameter02cbc872007-05-09 02:32:43 -0700322 return 1;
323
Christoph Lameter683d0ba2008-01-07 23:20:29 -0800324 base = slab_address(page);
Christoph Lameter02cbc872007-05-09 02:32:43 -0700325 if (object < base || object >= base + s->objects * s->size ||
326 (object - base) % s->size) {
327 return 0;
328 }
329
330 return 1;
331}
332
Christoph Lameter81819f02007-05-06 14:49:36 -0700333/*
Christoph Lameter7656c722007-05-09 02:32:40 -0700334 * Slow version of get and set free pointer.
335 *
336 * This version requires touching the cache lines of kmem_cache which
337 * we avoid to do in the fast alloc free paths. There we obtain the offset
338 * from the page struct.
339 */
340static inline void *get_freepointer(struct kmem_cache *s, void *object)
341{
342 return *(void **)(object + s->offset);
343}
344
345static inline void set_freepointer(struct kmem_cache *s, void *object, void *fp)
346{
347 *(void **)(object + s->offset) = fp;
348}
349
350/* Loop over all objects in a slab */
351#define for_each_object(__p, __s, __addr) \
352 for (__p = (__addr); __p < (__addr) + (__s)->objects * (__s)->size;\
353 __p += (__s)->size)
354
355/* Scan freelist */
356#define for_each_free_object(__p, __s, __free) \
Christoph Lameter683d0ba2008-01-07 23:20:29 -0800357 for (__p = (__free); (__p) != page->end; __p = get_freepointer((__s),\
358 __p))
Christoph Lameter7656c722007-05-09 02:32:40 -0700359
360/* Determine object index from a given position */
361static inline int slab_index(void *p, struct kmem_cache *s, void *addr)
362{
363 return (p - addr) / s->size;
364}
365
Christoph Lameter41ecc552007-05-09 02:32:44 -0700366#ifdef CONFIG_SLUB_DEBUG
367/*
368 * Debug settings:
369 */
Christoph Lameterf0630ff2007-07-15 23:38:14 -0700370#ifdef CONFIG_SLUB_DEBUG_ON
371static int slub_debug = DEBUG_DEFAULT_FLAGS;
372#else
Christoph Lameter41ecc552007-05-09 02:32:44 -0700373static int slub_debug;
Christoph Lameterf0630ff2007-07-15 23:38:14 -0700374#endif
Christoph Lameter41ecc552007-05-09 02:32:44 -0700375
376static char *slub_debug_slabs;
377
Christoph Lameter7656c722007-05-09 02:32:40 -0700378/*
Christoph Lameter81819f02007-05-06 14:49:36 -0700379 * Object debugging
380 */
381static void print_section(char *text, u8 *addr, unsigned int length)
382{
383 int i, offset;
384 int newline = 1;
385 char ascii[17];
386
387 ascii[16] = 0;
388
389 for (i = 0; i < length; i++) {
390 if (newline) {
Christoph Lameter24922682007-07-17 04:03:18 -0700391 printk(KERN_ERR "%8s 0x%p: ", text, addr + i);
Christoph Lameter81819f02007-05-06 14:49:36 -0700392 newline = 0;
393 }
Pekka Enberg06428782008-01-07 23:20:27 -0800394 printk(KERN_CONT " %02x", addr[i]);
Christoph Lameter81819f02007-05-06 14:49:36 -0700395 offset = i % 16;
396 ascii[offset] = isgraph(addr[i]) ? addr[i] : '.';
397 if (offset == 15) {
Pekka Enberg06428782008-01-07 23:20:27 -0800398 printk(KERN_CONT " %s\n", ascii);
Christoph Lameter81819f02007-05-06 14:49:36 -0700399 newline = 1;
400 }
401 }
402 if (!newline) {
403 i %= 16;
404 while (i < 16) {
Pekka Enberg06428782008-01-07 23:20:27 -0800405 printk(KERN_CONT " ");
Christoph Lameter81819f02007-05-06 14:49:36 -0700406 ascii[i] = ' ';
407 i++;
408 }
Pekka Enberg06428782008-01-07 23:20:27 -0800409 printk(KERN_CONT " %s\n", ascii);
Christoph Lameter81819f02007-05-06 14:49:36 -0700410 }
411}
412
Christoph Lameter81819f02007-05-06 14:49:36 -0700413static struct track *get_track(struct kmem_cache *s, void *object,
414 enum track_item alloc)
415{
416 struct track *p;
417
418 if (s->offset)
419 p = object + s->offset + sizeof(void *);
420 else
421 p = object + s->inuse;
422
423 return p + alloc;
424}
425
426static void set_track(struct kmem_cache *s, void *object,
427 enum track_item alloc, void *addr)
428{
429 struct track *p;
430
431 if (s->offset)
432 p = object + s->offset + sizeof(void *);
433 else
434 p = object + s->inuse;
435
436 p += alloc;
437 if (addr) {
438 p->addr = addr;
439 p->cpu = smp_processor_id();
440 p->pid = current ? current->pid : -1;
441 p->when = jiffies;
442 } else
443 memset(p, 0, sizeof(struct track));
444}
445
Christoph Lameter81819f02007-05-06 14:49:36 -0700446static void init_tracking(struct kmem_cache *s, void *object)
447{
Christoph Lameter24922682007-07-17 04:03:18 -0700448 if (!(s->flags & SLAB_STORE_USER))
449 return;
450
451 set_track(s, object, TRACK_FREE, NULL);
452 set_track(s, object, TRACK_ALLOC, NULL);
Christoph Lameter81819f02007-05-06 14:49:36 -0700453}
454
455static void print_track(const char *s, struct track *t)
456{
457 if (!t->addr)
458 return;
459
Christoph Lameter24922682007-07-17 04:03:18 -0700460 printk(KERN_ERR "INFO: %s in ", s);
Christoph Lameter81819f02007-05-06 14:49:36 -0700461 __print_symbol("%s", (unsigned long)t->addr);
Christoph Lameter24922682007-07-17 04:03:18 -0700462 printk(" age=%lu cpu=%u pid=%d\n", jiffies - t->when, t->cpu, t->pid);
Christoph Lameter81819f02007-05-06 14:49:36 -0700463}
464
Christoph Lameter24922682007-07-17 04:03:18 -0700465static void print_tracking(struct kmem_cache *s, void *object)
466{
467 if (!(s->flags & SLAB_STORE_USER))
468 return;
469
470 print_track("Allocated", get_track(s, object, TRACK_ALLOC));
471 print_track("Freed", get_track(s, object, TRACK_FREE));
472}
473
474static void print_page_info(struct page *page)
475{
476 printk(KERN_ERR "INFO: Slab 0x%p used=%u fp=0x%p flags=0x%04lx\n",
477 page, page->inuse, page->freelist, page->flags);
478
479}
480
481static void slab_bug(struct kmem_cache *s, char *fmt, ...)
482{
483 va_list args;
484 char buf[100];
485
486 va_start(args, fmt);
487 vsnprintf(buf, sizeof(buf), fmt, args);
488 va_end(args);
489 printk(KERN_ERR "========================================"
490 "=====================================\n");
491 printk(KERN_ERR "BUG %s: %s\n", s->name, buf);
492 printk(KERN_ERR "----------------------------------------"
493 "-------------------------------------\n\n");
494}
495
496static void slab_fix(struct kmem_cache *s, char *fmt, ...)
497{
498 va_list args;
499 char buf[100];
500
501 va_start(args, fmt);
502 vsnprintf(buf, sizeof(buf), fmt, args);
503 va_end(args);
504 printk(KERN_ERR "FIX %s: %s\n", s->name, buf);
505}
506
507static void print_trailer(struct kmem_cache *s, struct page *page, u8 *p)
Christoph Lameter81819f02007-05-06 14:49:36 -0700508{
509 unsigned int off; /* Offset of last byte */
Christoph Lameter683d0ba2008-01-07 23:20:29 -0800510 u8 *addr = slab_address(page);
Christoph Lameter24922682007-07-17 04:03:18 -0700511
512 print_tracking(s, p);
513
514 print_page_info(page);
515
516 printk(KERN_ERR "INFO: Object 0x%p @offset=%tu fp=0x%p\n\n",
517 p, p - addr, get_freepointer(s, p));
518
519 if (p > addr + 16)
520 print_section("Bytes b4", p - 16, 16);
521
522 print_section("Object", p, min(s->objsize, 128));
Christoph Lameter81819f02007-05-06 14:49:36 -0700523
524 if (s->flags & SLAB_RED_ZONE)
525 print_section("Redzone", p + s->objsize,
526 s->inuse - s->objsize);
527
Christoph Lameter81819f02007-05-06 14:49:36 -0700528 if (s->offset)
529 off = s->offset + sizeof(void *);
530 else
531 off = s->inuse;
532
Christoph Lameter24922682007-07-17 04:03:18 -0700533 if (s->flags & SLAB_STORE_USER)
Christoph Lameter81819f02007-05-06 14:49:36 -0700534 off += 2 * sizeof(struct track);
Christoph Lameter81819f02007-05-06 14:49:36 -0700535
536 if (off != s->size)
537 /* Beginning of the filler is the free pointer */
Christoph Lameter24922682007-07-17 04:03:18 -0700538 print_section("Padding", p + off, s->size - off);
539
540 dump_stack();
Christoph Lameter81819f02007-05-06 14:49:36 -0700541}
542
543static void object_err(struct kmem_cache *s, struct page *page,
544 u8 *object, char *reason)
545{
Christoph Lameter24922682007-07-17 04:03:18 -0700546 slab_bug(s, reason);
547 print_trailer(s, page, object);
Christoph Lameter81819f02007-05-06 14:49:36 -0700548}
549
Christoph Lameter24922682007-07-17 04:03:18 -0700550static void slab_err(struct kmem_cache *s, struct page *page, char *fmt, ...)
Christoph Lameter81819f02007-05-06 14:49:36 -0700551{
552 va_list args;
553 char buf[100];
554
Christoph Lameter24922682007-07-17 04:03:18 -0700555 va_start(args, fmt);
556 vsnprintf(buf, sizeof(buf), fmt, args);
Christoph Lameter81819f02007-05-06 14:49:36 -0700557 va_end(args);
Christoph Lameter24922682007-07-17 04:03:18 -0700558 slab_bug(s, fmt);
559 print_page_info(page);
Christoph Lameter81819f02007-05-06 14:49:36 -0700560 dump_stack();
561}
562
563static void init_object(struct kmem_cache *s, void *object, int active)
564{
565 u8 *p = object;
566
567 if (s->flags & __OBJECT_POISON) {
568 memset(p, POISON_FREE, s->objsize - 1);
Pekka Enberg06428782008-01-07 23:20:27 -0800569 p[s->objsize - 1] = POISON_END;
Christoph Lameter81819f02007-05-06 14:49:36 -0700570 }
571
572 if (s->flags & SLAB_RED_ZONE)
573 memset(p + s->objsize,
574 active ? SLUB_RED_ACTIVE : SLUB_RED_INACTIVE,
575 s->inuse - s->objsize);
576}
577
Christoph Lameter24922682007-07-17 04:03:18 -0700578static u8 *check_bytes(u8 *start, unsigned int value, unsigned int bytes)
Christoph Lameter81819f02007-05-06 14:49:36 -0700579{
580 while (bytes) {
581 if (*start != (u8)value)
Christoph Lameter24922682007-07-17 04:03:18 -0700582 return start;
Christoph Lameter81819f02007-05-06 14:49:36 -0700583 start++;
584 bytes--;
585 }
Christoph Lameter24922682007-07-17 04:03:18 -0700586 return NULL;
587}
588
589static void restore_bytes(struct kmem_cache *s, char *message, u8 data,
590 void *from, void *to)
591{
592 slab_fix(s, "Restoring 0x%p-0x%p=0x%x\n", from, to - 1, data);
593 memset(from, data, to - from);
594}
595
596static int check_bytes_and_report(struct kmem_cache *s, struct page *page,
597 u8 *object, char *what,
Pekka Enberg06428782008-01-07 23:20:27 -0800598 u8 *start, unsigned int value, unsigned int bytes)
Christoph Lameter24922682007-07-17 04:03:18 -0700599{
600 u8 *fault;
601 u8 *end;
602
603 fault = check_bytes(start, value, bytes);
604 if (!fault)
605 return 1;
606
607 end = start + bytes;
608 while (end > fault && end[-1] == value)
609 end--;
610
611 slab_bug(s, "%s overwritten", what);
612 printk(KERN_ERR "INFO: 0x%p-0x%p. First byte 0x%x instead of 0x%x\n",
613 fault, end - 1, fault[0], value);
614 print_trailer(s, page, object);
615
616 restore_bytes(s, what, value, fault, end);
617 return 0;
Christoph Lameter81819f02007-05-06 14:49:36 -0700618}
619
Christoph Lameter81819f02007-05-06 14:49:36 -0700620/*
621 * Object layout:
622 *
623 * object address
624 * Bytes of the object to be managed.
625 * If the freepointer may overlay the object then the free
626 * pointer is the first word of the object.
Christoph Lameter672bba32007-05-09 02:32:39 -0700627 *
Christoph Lameter81819f02007-05-06 14:49:36 -0700628 * Poisoning uses 0x6b (POISON_FREE) and the last byte is
629 * 0xa5 (POISON_END)
630 *
631 * object + s->objsize
632 * Padding to reach word boundary. This is also used for Redzoning.
Christoph Lameter672bba32007-05-09 02:32:39 -0700633 * Padding is extended by another word if Redzoning is enabled and
634 * objsize == inuse.
635 *
Christoph Lameter81819f02007-05-06 14:49:36 -0700636 * We fill with 0xbb (RED_INACTIVE) for inactive objects and with
637 * 0xcc (RED_ACTIVE) for objects in use.
638 *
639 * object + s->inuse
Christoph Lameter672bba32007-05-09 02:32:39 -0700640 * Meta data starts here.
641 *
Christoph Lameter81819f02007-05-06 14:49:36 -0700642 * A. Free pointer (if we cannot overwrite object on free)
643 * B. Tracking data for SLAB_STORE_USER
Christoph Lameter672bba32007-05-09 02:32:39 -0700644 * C. Padding to reach required alignment boundary or at mininum
645 * one word if debuggin is on to be able to detect writes
646 * before the word boundary.
647 *
648 * Padding is done using 0x5a (POISON_INUSE)
Christoph Lameter81819f02007-05-06 14:49:36 -0700649 *
650 * object + s->size
Christoph Lameter672bba32007-05-09 02:32:39 -0700651 * Nothing is used beyond s->size.
Christoph Lameter81819f02007-05-06 14:49:36 -0700652 *
Christoph Lameter672bba32007-05-09 02:32:39 -0700653 * If slabcaches are merged then the objsize and inuse boundaries are mostly
654 * ignored. And therefore no slab options that rely on these boundaries
Christoph Lameter81819f02007-05-06 14:49:36 -0700655 * may be used with merged slabcaches.
656 */
657
Christoph Lameter81819f02007-05-06 14:49:36 -0700658static int check_pad_bytes(struct kmem_cache *s, struct page *page, u8 *p)
659{
660 unsigned long off = s->inuse; /* The end of info */
661
662 if (s->offset)
663 /* Freepointer is placed after the object. */
664 off += sizeof(void *);
665
666 if (s->flags & SLAB_STORE_USER)
667 /* We also have user information there */
668 off += 2 * sizeof(struct track);
669
670 if (s->size == off)
671 return 1;
672
Christoph Lameter24922682007-07-17 04:03:18 -0700673 return check_bytes_and_report(s, page, p, "Object padding",
674 p + off, POISON_INUSE, s->size - off);
Christoph Lameter81819f02007-05-06 14:49:36 -0700675}
676
677static int slab_pad_check(struct kmem_cache *s, struct page *page)
678{
Christoph Lameter24922682007-07-17 04:03:18 -0700679 u8 *start;
680 u8 *fault;
681 u8 *end;
682 int length;
683 int remainder;
Christoph Lameter81819f02007-05-06 14:49:36 -0700684
685 if (!(s->flags & SLAB_POISON))
686 return 1;
687
Christoph Lameter683d0ba2008-01-07 23:20:29 -0800688 start = slab_address(page);
Christoph Lameter24922682007-07-17 04:03:18 -0700689 end = start + (PAGE_SIZE << s->order);
Christoph Lameter81819f02007-05-06 14:49:36 -0700690 length = s->objects * s->size;
Christoph Lameter24922682007-07-17 04:03:18 -0700691 remainder = end - (start + length);
Christoph Lameter81819f02007-05-06 14:49:36 -0700692 if (!remainder)
693 return 1;
694
Christoph Lameter24922682007-07-17 04:03:18 -0700695 fault = check_bytes(start + length, POISON_INUSE, remainder);
696 if (!fault)
697 return 1;
698 while (end > fault && end[-1] == POISON_INUSE)
699 end--;
700
701 slab_err(s, page, "Padding overwritten. 0x%p-0x%p", fault, end - 1);
702 print_section("Padding", start, length);
703
704 restore_bytes(s, "slab padding", POISON_INUSE, start, end);
705 return 0;
Christoph Lameter81819f02007-05-06 14:49:36 -0700706}
707
708static int check_object(struct kmem_cache *s, struct page *page,
709 void *object, int active)
710{
711 u8 *p = object;
712 u8 *endobject = object + s->objsize;
713
714 if (s->flags & SLAB_RED_ZONE) {
715 unsigned int red =
716 active ? SLUB_RED_ACTIVE : SLUB_RED_INACTIVE;
717
Christoph Lameter24922682007-07-17 04:03:18 -0700718 if (!check_bytes_and_report(s, page, object, "Redzone",
719 endobject, red, s->inuse - s->objsize))
Christoph Lameter81819f02007-05-06 14:49:36 -0700720 return 0;
Christoph Lameter81819f02007-05-06 14:49:36 -0700721 } else {
Ingo Molnar3adbefe2008-02-05 17:57:39 -0800722 if ((s->flags & SLAB_POISON) && s->objsize < s->inuse) {
723 check_bytes_and_report(s, page, p, "Alignment padding",
724 endobject, POISON_INUSE, s->inuse - s->objsize);
725 }
Christoph Lameter81819f02007-05-06 14:49:36 -0700726 }
727
728 if (s->flags & SLAB_POISON) {
729 if (!active && (s->flags & __OBJECT_POISON) &&
Christoph Lameter24922682007-07-17 04:03:18 -0700730 (!check_bytes_and_report(s, page, p, "Poison", p,
731 POISON_FREE, s->objsize - 1) ||
732 !check_bytes_and_report(s, page, p, "Poison",
Pekka Enberg06428782008-01-07 23:20:27 -0800733 p + s->objsize - 1, POISON_END, 1)))
Christoph Lameter81819f02007-05-06 14:49:36 -0700734 return 0;
Christoph Lameter81819f02007-05-06 14:49:36 -0700735 /*
736 * check_pad_bytes cleans up on its own.
737 */
738 check_pad_bytes(s, page, p);
739 }
740
741 if (!s->offset && active)
742 /*
743 * Object and freepointer overlap. Cannot check
744 * freepointer while object is allocated.
745 */
746 return 1;
747
748 /* Check free pointer validity */
749 if (!check_valid_pointer(s, page, get_freepointer(s, p))) {
750 object_err(s, page, p, "Freepointer corrupt");
751 /*
752 * No choice but to zap it and thus loose the remainder
753 * of the free objects in this slab. May cause
Christoph Lameter672bba32007-05-09 02:32:39 -0700754 * another error because the object count is now wrong.
Christoph Lameter81819f02007-05-06 14:49:36 -0700755 */
Christoph Lameter683d0ba2008-01-07 23:20:29 -0800756 set_freepointer(s, p, page->end);
Christoph Lameter81819f02007-05-06 14:49:36 -0700757 return 0;
758 }
759 return 1;
760}
761
762static int check_slab(struct kmem_cache *s, struct page *page)
763{
764 VM_BUG_ON(!irqs_disabled());
765
766 if (!PageSlab(page)) {
Christoph Lameter24922682007-07-17 04:03:18 -0700767 slab_err(s, page, "Not a valid slab page");
Christoph Lameter81819f02007-05-06 14:49:36 -0700768 return 0;
769 }
Christoph Lameter81819f02007-05-06 14:49:36 -0700770 if (page->inuse > s->objects) {
Christoph Lameter24922682007-07-17 04:03:18 -0700771 slab_err(s, page, "inuse %u > max %u",
772 s->name, page->inuse, s->objects);
Christoph Lameter81819f02007-05-06 14:49:36 -0700773 return 0;
774 }
775 /* Slab_pad_check fixes things up after itself */
776 slab_pad_check(s, page);
777 return 1;
778}
779
780/*
Christoph Lameter672bba32007-05-09 02:32:39 -0700781 * Determine if a certain object on a page is on the freelist. Must hold the
782 * slab lock to guarantee that the chains are in a consistent state.
Christoph Lameter81819f02007-05-06 14:49:36 -0700783 */
784static int on_freelist(struct kmem_cache *s, struct page *page, void *search)
785{
786 int nr = 0;
787 void *fp = page->freelist;
788 void *object = NULL;
789
Christoph Lameter683d0ba2008-01-07 23:20:29 -0800790 while (fp != page->end && nr <= s->objects) {
Christoph Lameter81819f02007-05-06 14:49:36 -0700791 if (fp == search)
792 return 1;
793 if (!check_valid_pointer(s, page, fp)) {
794 if (object) {
795 object_err(s, page, object,
796 "Freechain corrupt");
Christoph Lameter683d0ba2008-01-07 23:20:29 -0800797 set_freepointer(s, object, page->end);
Christoph Lameter81819f02007-05-06 14:49:36 -0700798 break;
799 } else {
Christoph Lameter24922682007-07-17 04:03:18 -0700800 slab_err(s, page, "Freepointer corrupt");
Christoph Lameter683d0ba2008-01-07 23:20:29 -0800801 page->freelist = page->end;
Christoph Lameter81819f02007-05-06 14:49:36 -0700802 page->inuse = s->objects;
Christoph Lameter24922682007-07-17 04:03:18 -0700803 slab_fix(s, "Freelist cleared");
Christoph Lameter81819f02007-05-06 14:49:36 -0700804 return 0;
805 }
806 break;
807 }
808 object = fp;
809 fp = get_freepointer(s, object);
810 nr++;
811 }
812
813 if (page->inuse != s->objects - nr) {
Christoph Lameter70d71222007-05-06 14:49:47 -0700814 slab_err(s, page, "Wrong object count. Counter is %d but "
Christoph Lameter24922682007-07-17 04:03:18 -0700815 "counted were %d", page->inuse, s->objects - nr);
Christoph Lameter81819f02007-05-06 14:49:36 -0700816 page->inuse = s->objects - nr;
Christoph Lameter24922682007-07-17 04:03:18 -0700817 slab_fix(s, "Object count adjusted.");
Christoph Lameter81819f02007-05-06 14:49:36 -0700818 }
819 return search == NULL;
820}
821
Christoph Lameter3ec09742007-05-16 22:11:00 -0700822static void trace(struct kmem_cache *s, struct page *page, void *object, int alloc)
823{
824 if (s->flags & SLAB_TRACE) {
825 printk(KERN_INFO "TRACE %s %s 0x%p inuse=%d fp=0x%p\n",
826 s->name,
827 alloc ? "alloc" : "free",
828 object, page->inuse,
829 page->freelist);
830
831 if (!alloc)
832 print_section("Object", (void *)object, s->objsize);
833
834 dump_stack();
835 }
836}
837
Christoph Lameter643b1132007-05-06 14:49:42 -0700838/*
Christoph Lameter672bba32007-05-09 02:32:39 -0700839 * Tracking of fully allocated slabs for debugging purposes.
Christoph Lameter643b1132007-05-06 14:49:42 -0700840 */
Christoph Lametere95eed52007-05-06 14:49:44 -0700841static void add_full(struct kmem_cache_node *n, struct page *page)
Christoph Lameter643b1132007-05-06 14:49:42 -0700842{
Christoph Lameter643b1132007-05-06 14:49:42 -0700843 spin_lock(&n->list_lock);
844 list_add(&page->lru, &n->full);
845 spin_unlock(&n->list_lock);
846}
847
848static void remove_full(struct kmem_cache *s, struct page *page)
849{
850 struct kmem_cache_node *n;
851
852 if (!(s->flags & SLAB_STORE_USER))
853 return;
854
855 n = get_node(s, page_to_nid(page));
856
857 spin_lock(&n->list_lock);
858 list_del(&page->lru);
859 spin_unlock(&n->list_lock);
860}
861
Christoph Lameter3ec09742007-05-16 22:11:00 -0700862static void setup_object_debug(struct kmem_cache *s, struct page *page,
863 void *object)
864{
865 if (!(s->flags & (SLAB_STORE_USER|SLAB_RED_ZONE|__OBJECT_POISON)))
866 return;
867
868 init_object(s, object, 0);
869 init_tracking(s, object);
870}
871
872static int alloc_debug_processing(struct kmem_cache *s, struct page *page,
873 void *object, void *addr)
Christoph Lameter81819f02007-05-06 14:49:36 -0700874{
875 if (!check_slab(s, page))
876 goto bad;
877
878 if (object && !on_freelist(s, page, object)) {
Christoph Lameter24922682007-07-17 04:03:18 -0700879 object_err(s, page, object, "Object already allocated");
Christoph Lameter70d71222007-05-06 14:49:47 -0700880 goto bad;
Christoph Lameter81819f02007-05-06 14:49:36 -0700881 }
882
883 if (!check_valid_pointer(s, page, object)) {
884 object_err(s, page, object, "Freelist Pointer check fails");
Christoph Lameter70d71222007-05-06 14:49:47 -0700885 goto bad;
Christoph Lameter81819f02007-05-06 14:49:36 -0700886 }
887
Christoph Lameter3ec09742007-05-16 22:11:00 -0700888 if (object && !check_object(s, page, object, 0))
Christoph Lameter81819f02007-05-06 14:49:36 -0700889 goto bad;
Christoph Lameter81819f02007-05-06 14:49:36 -0700890
Christoph Lameter3ec09742007-05-16 22:11:00 -0700891 /* Success perform special debug activities for allocs */
892 if (s->flags & SLAB_STORE_USER)
893 set_track(s, object, TRACK_ALLOC, addr);
894 trace(s, page, object, 1);
895 init_object(s, object, 1);
Christoph Lameter81819f02007-05-06 14:49:36 -0700896 return 1;
Christoph Lameter3ec09742007-05-16 22:11:00 -0700897
Christoph Lameter81819f02007-05-06 14:49:36 -0700898bad:
899 if (PageSlab(page)) {
900 /*
901 * If this is a slab page then lets do the best we can
902 * to avoid issues in the future. Marking all objects
Christoph Lameter672bba32007-05-09 02:32:39 -0700903 * as used avoids touching the remaining objects.
Christoph Lameter81819f02007-05-06 14:49:36 -0700904 */
Christoph Lameter24922682007-07-17 04:03:18 -0700905 slab_fix(s, "Marking all objects used");
Christoph Lameter81819f02007-05-06 14:49:36 -0700906 page->inuse = s->objects;
Christoph Lameter683d0ba2008-01-07 23:20:29 -0800907 page->freelist = page->end;
Christoph Lameter81819f02007-05-06 14:49:36 -0700908 }
909 return 0;
910}
911
Christoph Lameter3ec09742007-05-16 22:11:00 -0700912static int free_debug_processing(struct kmem_cache *s, struct page *page,
913 void *object, void *addr)
Christoph Lameter81819f02007-05-06 14:49:36 -0700914{
915 if (!check_slab(s, page))
916 goto fail;
917
918 if (!check_valid_pointer(s, page, object)) {
Christoph Lameter70d71222007-05-06 14:49:47 -0700919 slab_err(s, page, "Invalid object pointer 0x%p", object);
Christoph Lameter81819f02007-05-06 14:49:36 -0700920 goto fail;
921 }
922
923 if (on_freelist(s, page, object)) {
Christoph Lameter24922682007-07-17 04:03:18 -0700924 object_err(s, page, object, "Object already free");
Christoph Lameter81819f02007-05-06 14:49:36 -0700925 goto fail;
926 }
927
928 if (!check_object(s, page, object, 1))
929 return 0;
930
931 if (unlikely(s != page->slab)) {
Ingo Molnar3adbefe2008-02-05 17:57:39 -0800932 if (!PageSlab(page)) {
Christoph Lameter70d71222007-05-06 14:49:47 -0700933 slab_err(s, page, "Attempt to free object(0x%p) "
934 "outside of slab", object);
Ingo Molnar3adbefe2008-02-05 17:57:39 -0800935 } else if (!page->slab) {
Christoph Lameter81819f02007-05-06 14:49:36 -0700936 printk(KERN_ERR
Christoph Lameter70d71222007-05-06 14:49:47 -0700937 "SLUB <none>: no slab for object 0x%p.\n",
Christoph Lameter81819f02007-05-06 14:49:36 -0700938 object);
Christoph Lameter70d71222007-05-06 14:49:47 -0700939 dump_stack();
Pekka Enberg06428782008-01-07 23:20:27 -0800940 } else
Christoph Lameter24922682007-07-17 04:03:18 -0700941 object_err(s, page, object,
942 "page slab pointer corrupt.");
Christoph Lameter81819f02007-05-06 14:49:36 -0700943 goto fail;
944 }
Christoph Lameter3ec09742007-05-16 22:11:00 -0700945
946 /* Special debug activities for freeing objects */
Christoph Lameter683d0ba2008-01-07 23:20:29 -0800947 if (!SlabFrozen(page) && page->freelist == page->end)
Christoph Lameter3ec09742007-05-16 22:11:00 -0700948 remove_full(s, page);
949 if (s->flags & SLAB_STORE_USER)
950 set_track(s, object, TRACK_FREE, addr);
951 trace(s, page, object, 0);
952 init_object(s, object, 0);
Christoph Lameter81819f02007-05-06 14:49:36 -0700953 return 1;
Christoph Lameter3ec09742007-05-16 22:11:00 -0700954
Christoph Lameter81819f02007-05-06 14:49:36 -0700955fail:
Christoph Lameter24922682007-07-17 04:03:18 -0700956 slab_fix(s, "Object at 0x%p not freed", object);
Christoph Lameter81819f02007-05-06 14:49:36 -0700957 return 0;
958}
959
Christoph Lameter41ecc552007-05-09 02:32:44 -0700960static int __init setup_slub_debug(char *str)
961{
Christoph Lameterf0630ff2007-07-15 23:38:14 -0700962 slub_debug = DEBUG_DEFAULT_FLAGS;
963 if (*str++ != '=' || !*str)
964 /*
965 * No options specified. Switch on full debugging.
966 */
967 goto out;
Christoph Lameter41ecc552007-05-09 02:32:44 -0700968
969 if (*str == ',')
Christoph Lameterf0630ff2007-07-15 23:38:14 -0700970 /*
971 * No options but restriction on slabs. This means full
972 * debugging for slabs matching a pattern.
973 */
974 goto check_slabs;
975
976 slub_debug = 0;
977 if (*str == '-')
978 /*
979 * Switch off all debugging measures.
980 */
981 goto out;
982
983 /*
984 * Determine which debug features should be switched on
985 */
Pekka Enberg06428782008-01-07 23:20:27 -0800986 for (; *str && *str != ','; str++) {
Christoph Lameterf0630ff2007-07-15 23:38:14 -0700987 switch (tolower(*str)) {
988 case 'f':
989 slub_debug |= SLAB_DEBUG_FREE;
990 break;
991 case 'z':
992 slub_debug |= SLAB_RED_ZONE;
993 break;
994 case 'p':
995 slub_debug |= SLAB_POISON;
996 break;
997 case 'u':
998 slub_debug |= SLAB_STORE_USER;
999 break;
1000 case 't':
1001 slub_debug |= SLAB_TRACE;
1002 break;
1003 default:
1004 printk(KERN_ERR "slub_debug option '%c' "
Pekka Enberg06428782008-01-07 23:20:27 -08001005 "unknown. skipped\n", *str);
Christoph Lameterf0630ff2007-07-15 23:38:14 -07001006 }
1007 }
1008
1009check_slabs:
1010 if (*str == ',')
Christoph Lameter41ecc552007-05-09 02:32:44 -07001011 slub_debug_slabs = str + 1;
Christoph Lameterf0630ff2007-07-15 23:38:14 -07001012out:
Christoph Lameter41ecc552007-05-09 02:32:44 -07001013 return 1;
1014}
1015
1016__setup("slub_debug", setup_slub_debug);
1017
Christoph Lameterba0268a2007-09-11 15:24:11 -07001018static unsigned long kmem_cache_flags(unsigned long objsize,
1019 unsigned long flags, const char *name,
Christoph Lameter4ba9b9d2007-10-16 23:25:51 -07001020 void (*ctor)(struct kmem_cache *, void *))
Christoph Lameter41ecc552007-05-09 02:32:44 -07001021{
1022 /*
1023 * The page->offset field is only 16 bit wide. This is an offset
1024 * in units of words from the beginning of an object. If the slab
1025 * size is bigger then we cannot move the free pointer behind the
1026 * object anymore.
1027 *
1028 * On 32 bit platforms the limit is 256k. On 64bit platforms
1029 * the limit is 512k.
1030 *
Christoph Lameterc59def9f2007-05-16 22:10:50 -07001031 * Debugging or ctor may create a need to move the free
Christoph Lameter41ecc552007-05-09 02:32:44 -07001032 * pointer. Fail if this happens.
1033 */
Christoph Lameterba0268a2007-09-11 15:24:11 -07001034 if (objsize >= 65535 * sizeof(void *)) {
1035 BUG_ON(flags & (SLAB_RED_ZONE | SLAB_POISON |
Christoph Lameter41ecc552007-05-09 02:32:44 -07001036 SLAB_STORE_USER | SLAB_DESTROY_BY_RCU));
Christoph Lameterba0268a2007-09-11 15:24:11 -07001037 BUG_ON(ctor);
1038 } else {
Christoph Lameter41ecc552007-05-09 02:32:44 -07001039 /*
1040 * Enable debugging if selected on the kernel commandline.
1041 */
1042 if (slub_debug && (!slub_debug_slabs ||
Christoph Lameterba0268a2007-09-11 15:24:11 -07001043 strncmp(slub_debug_slabs, name,
Ingo Molnar3adbefe2008-02-05 17:57:39 -08001044 strlen(slub_debug_slabs)) == 0))
Christoph Lameterba0268a2007-09-11 15:24:11 -07001045 flags |= slub_debug;
1046 }
1047
1048 return flags;
Christoph Lameter41ecc552007-05-09 02:32:44 -07001049}
1050#else
Christoph Lameter3ec09742007-05-16 22:11:00 -07001051static inline void setup_object_debug(struct kmem_cache *s,
1052 struct page *page, void *object) {}
Christoph Lameter41ecc552007-05-09 02:32:44 -07001053
Christoph Lameter3ec09742007-05-16 22:11:00 -07001054static inline int alloc_debug_processing(struct kmem_cache *s,
1055 struct page *page, void *object, void *addr) { return 0; }
Christoph Lameter41ecc552007-05-09 02:32:44 -07001056
Christoph Lameter3ec09742007-05-16 22:11:00 -07001057static inline int free_debug_processing(struct kmem_cache *s,
1058 struct page *page, void *object, void *addr) { return 0; }
Christoph Lameter41ecc552007-05-09 02:32:44 -07001059
Christoph Lameter41ecc552007-05-09 02:32:44 -07001060static inline int slab_pad_check(struct kmem_cache *s, struct page *page)
1061 { return 1; }
1062static inline int check_object(struct kmem_cache *s, struct page *page,
1063 void *object, int active) { return 1; }
Christoph Lameter3ec09742007-05-16 22:11:00 -07001064static inline void add_full(struct kmem_cache_node *n, struct page *page) {}
Christoph Lameterba0268a2007-09-11 15:24:11 -07001065static inline unsigned long kmem_cache_flags(unsigned long objsize,
1066 unsigned long flags, const char *name,
Christoph Lameter4ba9b9d2007-10-16 23:25:51 -07001067 void (*ctor)(struct kmem_cache *, void *))
Christoph Lameterba0268a2007-09-11 15:24:11 -07001068{
1069 return flags;
1070}
Christoph Lameter41ecc552007-05-09 02:32:44 -07001071#define slub_debug 0
1072#endif
Christoph Lameter81819f02007-05-06 14:49:36 -07001073/*
1074 * Slab allocation and freeing
1075 */
1076static struct page *allocate_slab(struct kmem_cache *s, gfp_t flags, int node)
1077{
Pekka Enberg06428782008-01-07 23:20:27 -08001078 struct page *page;
Christoph Lameter81819f02007-05-06 14:49:36 -07001079 int pages = 1 << s->order;
1080
Christoph Lameterb7a49f02008-02-14 14:21:32 -08001081 flags |= s->allocflags;
Mel Gormane12ba742007-10-16 01:25:52 -07001082
Christoph Lameter81819f02007-05-06 14:49:36 -07001083 if (node == -1)
1084 page = alloc_pages(flags, s->order);
1085 else
1086 page = alloc_pages_node(node, flags, s->order);
1087
1088 if (!page)
1089 return NULL;
1090
1091 mod_zone_page_state(page_zone(page),
1092 (s->flags & SLAB_RECLAIM_ACCOUNT) ?
1093 NR_SLAB_RECLAIMABLE : NR_SLAB_UNRECLAIMABLE,
1094 pages);
1095
1096 return page;
1097}
1098
1099static void setup_object(struct kmem_cache *s, struct page *page,
1100 void *object)
1101{
Christoph Lameter3ec09742007-05-16 22:11:00 -07001102 setup_object_debug(s, page, object);
Christoph Lameter4f104932007-05-06 14:50:17 -07001103 if (unlikely(s->ctor))
Christoph Lameter4ba9b9d2007-10-16 23:25:51 -07001104 s->ctor(s, object);
Christoph Lameter81819f02007-05-06 14:49:36 -07001105}
1106
1107static struct page *new_slab(struct kmem_cache *s, gfp_t flags, int node)
1108{
1109 struct page *page;
1110 struct kmem_cache_node *n;
1111 void *start;
Christoph Lameter81819f02007-05-06 14:49:36 -07001112 void *last;
1113 void *p;
1114
Christoph Lameter6cb06222007-10-16 01:25:41 -07001115 BUG_ON(flags & GFP_SLAB_BUG_MASK);
Christoph Lameter81819f02007-05-06 14:49:36 -07001116
Christoph Lameter6cb06222007-10-16 01:25:41 -07001117 page = allocate_slab(s,
1118 flags & (GFP_RECLAIM_MASK | GFP_CONSTRAINT_MASK), node);
Christoph Lameter81819f02007-05-06 14:49:36 -07001119 if (!page)
1120 goto out;
1121
1122 n = get_node(s, page_to_nid(page));
1123 if (n)
1124 atomic_long_inc(&n->nr_slabs);
Christoph Lameter81819f02007-05-06 14:49:36 -07001125 page->slab = s;
1126 page->flags |= 1 << PG_slab;
1127 if (s->flags & (SLAB_DEBUG_FREE | SLAB_RED_ZONE | SLAB_POISON |
1128 SLAB_STORE_USER | SLAB_TRACE))
Christoph Lameter35e5d7e2007-05-09 02:32:42 -07001129 SetSlabDebug(page);
Christoph Lameter81819f02007-05-06 14:49:36 -07001130
1131 start = page_address(page);
Christoph Lameter683d0ba2008-01-07 23:20:29 -08001132 page->end = start + 1;
Christoph Lameter81819f02007-05-06 14:49:36 -07001133
1134 if (unlikely(s->flags & SLAB_POISON))
1135 memset(start, POISON_INUSE, PAGE_SIZE << s->order);
1136
1137 last = start;
Christoph Lameter7656c722007-05-09 02:32:40 -07001138 for_each_object(p, s, start) {
Christoph Lameter81819f02007-05-06 14:49:36 -07001139 setup_object(s, page, last);
1140 set_freepointer(s, last, p);
1141 last = p;
1142 }
1143 setup_object(s, page, last);
Christoph Lameter683d0ba2008-01-07 23:20:29 -08001144 set_freepointer(s, last, page->end);
Christoph Lameter81819f02007-05-06 14:49:36 -07001145
1146 page->freelist = start;
1147 page->inuse = 0;
1148out:
Christoph Lameter81819f02007-05-06 14:49:36 -07001149 return page;
1150}
1151
1152static void __free_slab(struct kmem_cache *s, struct page *page)
1153{
1154 int pages = 1 << s->order;
1155
Christoph Lameterc59def9f2007-05-16 22:10:50 -07001156 if (unlikely(SlabDebug(page))) {
Christoph Lameter81819f02007-05-06 14:49:36 -07001157 void *p;
1158
1159 slab_pad_check(s, page);
Christoph Lameter683d0ba2008-01-07 23:20:29 -08001160 for_each_object(p, s, slab_address(page))
Christoph Lameter81819f02007-05-06 14:49:36 -07001161 check_object(s, page, p, 0);
Peter Zijlstra2208b762007-07-26 20:54:34 +02001162 ClearSlabDebug(page);
Christoph Lameter81819f02007-05-06 14:49:36 -07001163 }
1164
1165 mod_zone_page_state(page_zone(page),
1166 (s->flags & SLAB_RECLAIM_ACCOUNT) ?
1167 NR_SLAB_RECLAIMABLE : NR_SLAB_UNRECLAIMABLE,
Pekka Enberg06428782008-01-07 23:20:27 -08001168 -pages);
Christoph Lameter81819f02007-05-06 14:49:36 -07001169
Christoph Lameter683d0ba2008-01-07 23:20:29 -08001170 page->mapping = NULL;
Christoph Lameter81819f02007-05-06 14:49:36 -07001171 __free_pages(page, s->order);
1172}
1173
1174static void rcu_free_slab(struct rcu_head *h)
1175{
1176 struct page *page;
1177
1178 page = container_of((struct list_head *)h, struct page, lru);
1179 __free_slab(page->slab, page);
1180}
1181
1182static void free_slab(struct kmem_cache *s, struct page *page)
1183{
1184 if (unlikely(s->flags & SLAB_DESTROY_BY_RCU)) {
1185 /*
1186 * RCU free overloads the RCU head over the LRU
1187 */
1188 struct rcu_head *head = (void *)&page->lru;
1189
1190 call_rcu(head, rcu_free_slab);
1191 } else
1192 __free_slab(s, page);
1193}
1194
1195static void discard_slab(struct kmem_cache *s, struct page *page)
1196{
1197 struct kmem_cache_node *n = get_node(s, page_to_nid(page));
1198
1199 atomic_long_dec(&n->nr_slabs);
1200 reset_page_mapcount(page);
Christoph Lameter35e5d7e2007-05-09 02:32:42 -07001201 __ClearPageSlab(page);
Christoph Lameter81819f02007-05-06 14:49:36 -07001202 free_slab(s, page);
1203}
1204
1205/*
1206 * Per slab locking using the pagelock
1207 */
1208static __always_inline void slab_lock(struct page *page)
1209{
1210 bit_spin_lock(PG_locked, &page->flags);
1211}
1212
1213static __always_inline void slab_unlock(struct page *page)
1214{
Nick Piggina76d3542008-01-07 23:20:27 -08001215 __bit_spin_unlock(PG_locked, &page->flags);
Christoph Lameter81819f02007-05-06 14:49:36 -07001216}
1217
1218static __always_inline int slab_trylock(struct page *page)
1219{
1220 int rc = 1;
1221
1222 rc = bit_spin_trylock(PG_locked, &page->flags);
1223 return rc;
1224}
1225
1226/*
1227 * Management of partially allocated slabs
1228 */
Christoph Lameter7c2e1322008-01-07 23:20:27 -08001229static void add_partial(struct kmem_cache_node *n,
1230 struct page *page, int tail)
Christoph Lameter81819f02007-05-06 14:49:36 -07001231{
Christoph Lametere95eed52007-05-06 14:49:44 -07001232 spin_lock(&n->list_lock);
1233 n->nr_partial++;
Christoph Lameter7c2e1322008-01-07 23:20:27 -08001234 if (tail)
1235 list_add_tail(&page->lru, &n->partial);
1236 else
1237 list_add(&page->lru, &n->partial);
Christoph Lameter81819f02007-05-06 14:49:36 -07001238 spin_unlock(&n->list_lock);
1239}
1240
1241static void remove_partial(struct kmem_cache *s,
1242 struct page *page)
1243{
1244 struct kmem_cache_node *n = get_node(s, page_to_nid(page));
1245
1246 spin_lock(&n->list_lock);
1247 list_del(&page->lru);
1248 n->nr_partial--;
1249 spin_unlock(&n->list_lock);
1250}
1251
1252/*
Christoph Lameter672bba32007-05-09 02:32:39 -07001253 * Lock slab and remove from the partial list.
Christoph Lameter81819f02007-05-06 14:49:36 -07001254 *
Christoph Lameter672bba32007-05-09 02:32:39 -07001255 * Must hold list_lock.
Christoph Lameter81819f02007-05-06 14:49:36 -07001256 */
Christoph Lameter4b6f0752007-05-16 22:10:53 -07001257static inline int lock_and_freeze_slab(struct kmem_cache_node *n, struct page *page)
Christoph Lameter81819f02007-05-06 14:49:36 -07001258{
1259 if (slab_trylock(page)) {
1260 list_del(&page->lru);
1261 n->nr_partial--;
Christoph Lameter4b6f0752007-05-16 22:10:53 -07001262 SetSlabFrozen(page);
Christoph Lameter81819f02007-05-06 14:49:36 -07001263 return 1;
1264 }
1265 return 0;
1266}
1267
1268/*
Christoph Lameter672bba32007-05-09 02:32:39 -07001269 * Try to allocate a partial slab from a specific node.
Christoph Lameter81819f02007-05-06 14:49:36 -07001270 */
1271static struct page *get_partial_node(struct kmem_cache_node *n)
1272{
1273 struct page *page;
1274
1275 /*
1276 * Racy check. If we mistakenly see no partial slabs then we
1277 * just allocate an empty slab. If we mistakenly try to get a
Christoph Lameter672bba32007-05-09 02:32:39 -07001278 * partial slab and there is none available then get_partials()
1279 * will return NULL.
Christoph Lameter81819f02007-05-06 14:49:36 -07001280 */
1281 if (!n || !n->nr_partial)
1282 return NULL;
1283
1284 spin_lock(&n->list_lock);
1285 list_for_each_entry(page, &n->partial, lru)
Christoph Lameter4b6f0752007-05-16 22:10:53 -07001286 if (lock_and_freeze_slab(n, page))
Christoph Lameter81819f02007-05-06 14:49:36 -07001287 goto out;
1288 page = NULL;
1289out:
1290 spin_unlock(&n->list_lock);
1291 return page;
1292}
1293
1294/*
Christoph Lameter672bba32007-05-09 02:32:39 -07001295 * Get a page from somewhere. Search in increasing NUMA distances.
Christoph Lameter81819f02007-05-06 14:49:36 -07001296 */
1297static struct page *get_any_partial(struct kmem_cache *s, gfp_t flags)
1298{
1299#ifdef CONFIG_NUMA
1300 struct zonelist *zonelist;
1301 struct zone **z;
1302 struct page *page;
1303
1304 /*
Christoph Lameter672bba32007-05-09 02:32:39 -07001305 * The defrag ratio allows a configuration of the tradeoffs between
1306 * inter node defragmentation and node local allocations. A lower
1307 * defrag_ratio increases the tendency to do local allocations
1308 * instead of attempting to obtain partial slabs from other nodes.
Christoph Lameter81819f02007-05-06 14:49:36 -07001309 *
Christoph Lameter672bba32007-05-09 02:32:39 -07001310 * If the defrag_ratio is set to 0 then kmalloc() always
1311 * returns node local objects. If the ratio is higher then kmalloc()
1312 * may return off node objects because partial slabs are obtained
1313 * from other nodes and filled up.
Christoph Lameter81819f02007-05-06 14:49:36 -07001314 *
1315 * If /sys/slab/xx/defrag_ratio is set to 100 (which makes
Christoph Lameter672bba32007-05-09 02:32:39 -07001316 * defrag_ratio = 1000) then every (well almost) allocation will
1317 * first attempt to defrag slab caches on other nodes. This means
1318 * scanning over all nodes to look for partial slabs which may be
1319 * expensive if we do it every time we are trying to find a slab
1320 * with available objects.
Christoph Lameter81819f02007-05-06 14:49:36 -07001321 */
Christoph Lameter98246012008-01-07 23:20:26 -08001322 if (!s->remote_node_defrag_ratio ||
1323 get_cycles() % 1024 > s->remote_node_defrag_ratio)
Christoph Lameter81819f02007-05-06 14:49:36 -07001324 return NULL;
1325
Ingo Molnar3adbefe2008-02-05 17:57:39 -08001326 zonelist = &NODE_DATA(
1327 slab_node(current->mempolicy))->node_zonelists[gfp_zone(flags)];
Christoph Lameter81819f02007-05-06 14:49:36 -07001328 for (z = zonelist->zones; *z; z++) {
1329 struct kmem_cache_node *n;
1330
1331 n = get_node(s, zone_to_nid(*z));
1332
1333 if (n && cpuset_zone_allowed_hardwall(*z, flags) &&
Christoph Lametere95eed52007-05-06 14:49:44 -07001334 n->nr_partial > MIN_PARTIAL) {
Christoph Lameter81819f02007-05-06 14:49:36 -07001335 page = get_partial_node(n);
1336 if (page)
1337 return page;
1338 }
1339 }
1340#endif
1341 return NULL;
1342}
1343
1344/*
1345 * Get a partial page, lock it and return it.
1346 */
1347static struct page *get_partial(struct kmem_cache *s, gfp_t flags, int node)
1348{
1349 struct page *page;
1350 int searchnode = (node == -1) ? numa_node_id() : node;
1351
1352 page = get_partial_node(get_node(s, searchnode));
1353 if (page || (flags & __GFP_THISNODE))
1354 return page;
1355
1356 return get_any_partial(s, flags);
1357}
1358
1359/*
1360 * Move a page back to the lists.
1361 *
1362 * Must be called with the slab lock held.
1363 *
1364 * On exit the slab lock will have been dropped.
1365 */
Christoph Lameter7c2e1322008-01-07 23:20:27 -08001366static void unfreeze_slab(struct kmem_cache *s, struct page *page, int tail)
Christoph Lameter81819f02007-05-06 14:49:36 -07001367{
Christoph Lametere95eed52007-05-06 14:49:44 -07001368 struct kmem_cache_node *n = get_node(s, page_to_nid(page));
Christoph Lameter8ff12cf2008-02-07 17:47:41 -08001369 struct kmem_cache_cpu *c = get_cpu_slab(s, smp_processor_id());
Christoph Lametere95eed52007-05-06 14:49:44 -07001370
Christoph Lameter4b6f0752007-05-16 22:10:53 -07001371 ClearSlabFrozen(page);
Christoph Lameter81819f02007-05-06 14:49:36 -07001372 if (page->inuse) {
Christoph Lametere95eed52007-05-06 14:49:44 -07001373
Christoph Lameter8ff12cf2008-02-07 17:47:41 -08001374 if (page->freelist != page->end) {
Christoph Lameter7c2e1322008-01-07 23:20:27 -08001375 add_partial(n, page, tail);
Christoph Lameter8ff12cf2008-02-07 17:47:41 -08001376 stat(c, tail ? DEACTIVATE_TO_TAIL : DEACTIVATE_TO_HEAD);
1377 } else {
1378 stat(c, DEACTIVATE_FULL);
1379 if (SlabDebug(page) && (s->flags & SLAB_STORE_USER))
1380 add_full(n, page);
1381 }
Christoph Lameter81819f02007-05-06 14:49:36 -07001382 slab_unlock(page);
1383 } else {
Christoph Lameter8ff12cf2008-02-07 17:47:41 -08001384 stat(c, DEACTIVATE_EMPTY);
Christoph Lametere95eed52007-05-06 14:49:44 -07001385 if (n->nr_partial < MIN_PARTIAL) {
1386 /*
Christoph Lameter672bba32007-05-09 02:32:39 -07001387 * Adding an empty slab to the partial slabs in order
1388 * to avoid page allocator overhead. This slab needs
1389 * to come after the other slabs with objects in
1390 * order to fill them up. That way the size of the
1391 * partial list stays small. kmem_cache_shrink can
1392 * reclaim empty slabs from the partial list.
Christoph Lametere95eed52007-05-06 14:49:44 -07001393 */
Christoph Lameter7c2e1322008-01-07 23:20:27 -08001394 add_partial(n, page, 1);
Christoph Lametere95eed52007-05-06 14:49:44 -07001395 slab_unlock(page);
1396 } else {
1397 slab_unlock(page);
Christoph Lameter8ff12cf2008-02-07 17:47:41 -08001398 stat(get_cpu_slab(s, raw_smp_processor_id()), FREE_SLAB);
Christoph Lametere95eed52007-05-06 14:49:44 -07001399 discard_slab(s, page);
1400 }
Christoph Lameter81819f02007-05-06 14:49:36 -07001401 }
1402}
1403
1404/*
1405 * Remove the cpu slab
1406 */
Christoph Lameterdfb4f092007-10-16 01:26:05 -07001407static void deactivate_slab(struct kmem_cache *s, struct kmem_cache_cpu *c)
Christoph Lameter81819f02007-05-06 14:49:36 -07001408{
Christoph Lameterdfb4f092007-10-16 01:26:05 -07001409 struct page *page = c->page;
Christoph Lameter7c2e1322008-01-07 23:20:27 -08001410 int tail = 1;
Christoph Lameter8ff12cf2008-02-07 17:47:41 -08001411
1412 if (c->freelist)
1413 stat(c, DEACTIVATE_REMOTE_FREES);
Christoph Lameter894b8782007-05-10 03:15:16 -07001414 /*
1415 * Merge cpu freelist into freelist. Typically we get here
1416 * because both freelists are empty. So this is unlikely
1417 * to occur.
Christoph Lameter683d0ba2008-01-07 23:20:29 -08001418 *
1419 * We need to use _is_end here because deactivate slab may
1420 * be called for a debug slab. Then c->freelist may contain
1421 * a dummy pointer.
Christoph Lameter894b8782007-05-10 03:15:16 -07001422 */
Christoph Lameter683d0ba2008-01-07 23:20:29 -08001423 while (unlikely(!is_end(c->freelist))) {
Christoph Lameter894b8782007-05-10 03:15:16 -07001424 void **object;
1425
Christoph Lameter7c2e1322008-01-07 23:20:27 -08001426 tail = 0; /* Hot objects. Put the slab first */
1427
Christoph Lameter894b8782007-05-10 03:15:16 -07001428 /* Retrieve object from cpu_freelist */
Christoph Lameterdfb4f092007-10-16 01:26:05 -07001429 object = c->freelist;
Christoph Lameterb3fba8d2007-10-16 01:26:06 -07001430 c->freelist = c->freelist[c->offset];
Christoph Lameter894b8782007-05-10 03:15:16 -07001431
1432 /* And put onto the regular freelist */
Christoph Lameterb3fba8d2007-10-16 01:26:06 -07001433 object[c->offset] = page->freelist;
Christoph Lameter894b8782007-05-10 03:15:16 -07001434 page->freelist = object;
1435 page->inuse--;
1436 }
Christoph Lameterdfb4f092007-10-16 01:26:05 -07001437 c->page = NULL;
Christoph Lameter7c2e1322008-01-07 23:20:27 -08001438 unfreeze_slab(s, page, tail);
Christoph Lameter81819f02007-05-06 14:49:36 -07001439}
1440
Christoph Lameterdfb4f092007-10-16 01:26:05 -07001441static inline void flush_slab(struct kmem_cache *s, struct kmem_cache_cpu *c)
Christoph Lameter81819f02007-05-06 14:49:36 -07001442{
Christoph Lameter8ff12cf2008-02-07 17:47:41 -08001443 stat(c, CPUSLAB_FLUSH);
Christoph Lameterdfb4f092007-10-16 01:26:05 -07001444 slab_lock(c->page);
1445 deactivate_slab(s, c);
Christoph Lameter81819f02007-05-06 14:49:36 -07001446}
1447
1448/*
1449 * Flush cpu slab.
1450 * Called from IPI handler with interrupts disabled.
1451 */
Christoph Lameter0c710012007-07-17 04:03:24 -07001452static inline void __flush_cpu_slab(struct kmem_cache *s, int cpu)
Christoph Lameter81819f02007-05-06 14:49:36 -07001453{
Christoph Lameterdfb4f092007-10-16 01:26:05 -07001454 struct kmem_cache_cpu *c = get_cpu_slab(s, cpu);
Christoph Lameter81819f02007-05-06 14:49:36 -07001455
Christoph Lameterdfb4f092007-10-16 01:26:05 -07001456 if (likely(c && c->page))
1457 flush_slab(s, c);
Christoph Lameter81819f02007-05-06 14:49:36 -07001458}
1459
1460static void flush_cpu_slab(void *d)
1461{
1462 struct kmem_cache *s = d;
Christoph Lameter81819f02007-05-06 14:49:36 -07001463
Christoph Lameterdfb4f092007-10-16 01:26:05 -07001464 __flush_cpu_slab(s, smp_processor_id());
Christoph Lameter81819f02007-05-06 14:49:36 -07001465}
1466
1467static void flush_all(struct kmem_cache *s)
1468{
1469#ifdef CONFIG_SMP
1470 on_each_cpu(flush_cpu_slab, s, 1, 1);
1471#else
1472 unsigned long flags;
1473
1474 local_irq_save(flags);
1475 flush_cpu_slab(s);
1476 local_irq_restore(flags);
1477#endif
1478}
1479
1480/*
Christoph Lameterdfb4f092007-10-16 01:26:05 -07001481 * Check if the objects in a per cpu structure fit numa
1482 * locality expectations.
1483 */
1484static inline int node_match(struct kmem_cache_cpu *c, int node)
1485{
1486#ifdef CONFIG_NUMA
1487 if (node != -1 && c->node != node)
1488 return 0;
1489#endif
1490 return 1;
1491}
1492
1493/*
Christoph Lameter894b8782007-05-10 03:15:16 -07001494 * Slow path. The lockless freelist is empty or we need to perform
1495 * debugging duties.
Christoph Lameter81819f02007-05-06 14:49:36 -07001496 *
Christoph Lameter894b8782007-05-10 03:15:16 -07001497 * Interrupts are disabled.
Christoph Lameter81819f02007-05-06 14:49:36 -07001498 *
Christoph Lameter894b8782007-05-10 03:15:16 -07001499 * Processing is still very fast if new objects have been freed to the
1500 * regular freelist. In that case we simply take over the regular freelist
1501 * as the lockless freelist and zap the regular freelist.
Christoph Lameter81819f02007-05-06 14:49:36 -07001502 *
Christoph Lameter894b8782007-05-10 03:15:16 -07001503 * If that is not working then we fall back to the partial lists. We take the
1504 * first element of the freelist as the object to allocate now and move the
1505 * rest of the freelist to the lockless freelist.
1506 *
1507 * And if we were unable to get a new slab from the partial slab lists then
1508 * we need to allocate a new slab. This is slowest path since we may sleep.
Christoph Lameter81819f02007-05-06 14:49:36 -07001509 */
Christoph Lameter894b8782007-05-10 03:15:16 -07001510static void *__slab_alloc(struct kmem_cache *s,
Christoph Lameterdfb4f092007-10-16 01:26:05 -07001511 gfp_t gfpflags, int node, void *addr, struct kmem_cache_cpu *c)
Christoph Lameter81819f02007-05-06 14:49:36 -07001512{
Christoph Lameter81819f02007-05-06 14:49:36 -07001513 void **object;
Christoph Lameterdfb4f092007-10-16 01:26:05 -07001514 struct page *new;
Christoph Lameter1f842602008-01-07 23:20:30 -08001515#ifdef SLUB_FASTPATH
1516 unsigned long flags;
Christoph Lameter81819f02007-05-06 14:49:36 -07001517
Christoph Lameter1f842602008-01-07 23:20:30 -08001518 local_irq_save(flags);
1519#endif
Christoph Lameterdfb4f092007-10-16 01:26:05 -07001520 if (!c->page)
Christoph Lameter81819f02007-05-06 14:49:36 -07001521 goto new_slab;
1522
Christoph Lameterdfb4f092007-10-16 01:26:05 -07001523 slab_lock(c->page);
1524 if (unlikely(!node_match(c, node)))
Christoph Lameter81819f02007-05-06 14:49:36 -07001525 goto another_slab;
Christoph Lameter8ff12cf2008-02-07 17:47:41 -08001526 stat(c, ALLOC_REFILL);
Christoph Lameter894b8782007-05-10 03:15:16 -07001527load_freelist:
Christoph Lameterdfb4f092007-10-16 01:26:05 -07001528 object = c->page->freelist;
Christoph Lameter683d0ba2008-01-07 23:20:29 -08001529 if (unlikely(object == c->page->end))
Christoph Lameter81819f02007-05-06 14:49:36 -07001530 goto another_slab;
Christoph Lameterdfb4f092007-10-16 01:26:05 -07001531 if (unlikely(SlabDebug(c->page)))
Christoph Lameter81819f02007-05-06 14:49:36 -07001532 goto debug;
1533
Christoph Lameterdfb4f092007-10-16 01:26:05 -07001534 object = c->page->freelist;
Christoph Lameterb3fba8d2007-10-16 01:26:06 -07001535 c->freelist = object[c->offset];
Christoph Lameterdfb4f092007-10-16 01:26:05 -07001536 c->page->inuse = s->objects;
Christoph Lameter683d0ba2008-01-07 23:20:29 -08001537 c->page->freelist = c->page->end;
Christoph Lameterdfb4f092007-10-16 01:26:05 -07001538 c->node = page_to_nid(c->page);
Christoph Lameter1f842602008-01-07 23:20:30 -08001539unlock_out:
Christoph Lameterdfb4f092007-10-16 01:26:05 -07001540 slab_unlock(c->page);
Christoph Lameter8ff12cf2008-02-07 17:47:41 -08001541 stat(c, ALLOC_SLOWPATH);
Christoph Lameter1f842602008-01-07 23:20:30 -08001542out:
1543#ifdef SLUB_FASTPATH
1544 local_irq_restore(flags);
1545#endif
Christoph Lameter81819f02007-05-06 14:49:36 -07001546 return object;
1547
1548another_slab:
Christoph Lameterdfb4f092007-10-16 01:26:05 -07001549 deactivate_slab(s, c);
Christoph Lameter81819f02007-05-06 14:49:36 -07001550
1551new_slab:
Christoph Lameterdfb4f092007-10-16 01:26:05 -07001552 new = get_partial(s, gfpflags, node);
1553 if (new) {
1554 c->page = new;
Christoph Lameter8ff12cf2008-02-07 17:47:41 -08001555 stat(c, ALLOC_FROM_PARTIAL);
Christoph Lameter894b8782007-05-10 03:15:16 -07001556 goto load_freelist;
Christoph Lameter81819f02007-05-06 14:49:36 -07001557 }
1558
Christoph Lameterb811c202007-10-16 23:25:51 -07001559 if (gfpflags & __GFP_WAIT)
1560 local_irq_enable();
1561
Christoph Lameterdfb4f092007-10-16 01:26:05 -07001562 new = new_slab(s, gfpflags, node);
Christoph Lameterb811c202007-10-16 23:25:51 -07001563
1564 if (gfpflags & __GFP_WAIT)
1565 local_irq_disable();
1566
Christoph Lameterdfb4f092007-10-16 01:26:05 -07001567 if (new) {
1568 c = get_cpu_slab(s, smp_processor_id());
Christoph Lameter8ff12cf2008-02-07 17:47:41 -08001569 stat(c, ALLOC_SLAB);
Christoph Lameter05aa3452007-11-05 11:31:58 -08001570 if (c->page)
Christoph Lameterdfb4f092007-10-16 01:26:05 -07001571 flush_slab(s, c);
Christoph Lameterdfb4f092007-10-16 01:26:05 -07001572 slab_lock(new);
1573 SetSlabFrozen(new);
1574 c->page = new;
Christoph Lameter4b6f0752007-05-16 22:10:53 -07001575 goto load_freelist;
Christoph Lameter81819f02007-05-06 14:49:36 -07001576 }
Christoph Lameter1f842602008-01-07 23:20:30 -08001577 object = NULL;
1578 goto out;
Christoph Lameter81819f02007-05-06 14:49:36 -07001579debug:
Christoph Lameterdfb4f092007-10-16 01:26:05 -07001580 object = c->page->freelist;
1581 if (!alloc_debug_processing(s, c->page, object, addr))
Christoph Lameter81819f02007-05-06 14:49:36 -07001582 goto another_slab;
Christoph Lameter894b8782007-05-10 03:15:16 -07001583
Christoph Lameterdfb4f092007-10-16 01:26:05 -07001584 c->page->inuse++;
Christoph Lameterb3fba8d2007-10-16 01:26:06 -07001585 c->page->freelist = object[c->offset];
Christoph Lameteree3c72a2007-10-16 01:26:07 -07001586 c->node = -1;
Christoph Lameter1f842602008-01-07 23:20:30 -08001587 goto unlock_out;
Christoph Lameter894b8782007-05-10 03:15:16 -07001588}
1589
1590/*
1591 * Inlined fastpath so that allocation functions (kmalloc, kmem_cache_alloc)
1592 * have the fastpath folded into their functions. So no function call
1593 * overhead for requests that can be satisfied on the fastpath.
1594 *
1595 * The fastpath works by first checking if the lockless freelist can be used.
1596 * If not then __slab_alloc is called for slow processing.
1597 *
1598 * Otherwise we can simply pick the next object from the lockless free list.
1599 */
Pekka Enberg06428782008-01-07 23:20:27 -08001600static __always_inline void *slab_alloc(struct kmem_cache *s,
Christoph Lameterce15fea2007-07-17 04:03:28 -07001601 gfp_t gfpflags, int node, void *addr)
Christoph Lameter894b8782007-05-10 03:15:16 -07001602{
Christoph Lameter894b8782007-05-10 03:15:16 -07001603 void **object;
Christoph Lameterdfb4f092007-10-16 01:26:05 -07001604 struct kmem_cache_cpu *c;
Christoph Lameter894b8782007-05-10 03:15:16 -07001605
Christoph Lameter1f842602008-01-07 23:20:30 -08001606/*
1607 * The SLUB_FASTPATH path is provisional and is currently disabled if the
1608 * kernel is compiled with preemption or if the arch does not support
1609 * fast cmpxchg operations. There are a couple of coming changes that will
1610 * simplify matters and allow preemption. Ultimately we may end up making
1611 * SLUB_FASTPATH the default.
1612 *
1613 * 1. The introduction of the per cpu allocator will avoid array lookups
1614 * through get_cpu_slab(). A special register can be used instead.
1615 *
1616 * 2. The introduction of per cpu atomic operations (cpu_ops) means that
1617 * we can realize the logic here entirely with per cpu atomics. The
1618 * per cpu atomic ops will take care of the preemption issues.
1619 */
1620
1621#ifdef SLUB_FASTPATH
1622 c = get_cpu_slab(s, raw_smp_processor_id());
1623 do {
1624 object = c->freelist;
1625 if (unlikely(is_end(object) || !node_match(c, node))) {
1626 object = __slab_alloc(s, gfpflags, node, addr, c);
1627 break;
1628 }
Christoph Lameter8ff12cf2008-02-07 17:47:41 -08001629 stat(c, ALLOC_FASTPATH);
Christoph Lameter1f842602008-01-07 23:20:30 -08001630 } while (cmpxchg_local(&c->freelist, object, object[c->offset])
1631 != object);
1632#else
1633 unsigned long flags;
1634
Christoph Lameter894b8782007-05-10 03:15:16 -07001635 local_irq_save(flags);
Christoph Lameterdfb4f092007-10-16 01:26:05 -07001636 c = get_cpu_slab(s, smp_processor_id());
Christoph Lameter683d0ba2008-01-07 23:20:29 -08001637 if (unlikely(is_end(c->freelist) || !node_match(c, node)))
Christoph Lameter894b8782007-05-10 03:15:16 -07001638
Christoph Lameterdfb4f092007-10-16 01:26:05 -07001639 object = __slab_alloc(s, gfpflags, node, addr, c);
Christoph Lameter894b8782007-05-10 03:15:16 -07001640
1641 else {
Christoph Lameterdfb4f092007-10-16 01:26:05 -07001642 object = c->freelist;
Christoph Lameterb3fba8d2007-10-16 01:26:06 -07001643 c->freelist = object[c->offset];
Christoph Lameter8ff12cf2008-02-07 17:47:41 -08001644 stat(c, ALLOC_FASTPATH);
Christoph Lameter894b8782007-05-10 03:15:16 -07001645 }
1646 local_irq_restore(flags);
Christoph Lameter1f842602008-01-07 23:20:30 -08001647#endif
Christoph Lameterd07dbea2007-07-17 04:03:23 -07001648
1649 if (unlikely((gfpflags & __GFP_ZERO) && object))
Christoph Lameter42a9fdb2007-10-16 01:26:09 -07001650 memset(object, 0, c->objsize);
Christoph Lameterd07dbea2007-07-17 04:03:23 -07001651
Christoph Lameter894b8782007-05-10 03:15:16 -07001652 return object;
Christoph Lameter81819f02007-05-06 14:49:36 -07001653}
1654
1655void *kmem_cache_alloc(struct kmem_cache *s, gfp_t gfpflags)
1656{
Christoph Lameterce15fea2007-07-17 04:03:28 -07001657 return slab_alloc(s, gfpflags, -1, __builtin_return_address(0));
Christoph Lameter81819f02007-05-06 14:49:36 -07001658}
1659EXPORT_SYMBOL(kmem_cache_alloc);
1660
1661#ifdef CONFIG_NUMA
1662void *kmem_cache_alloc_node(struct kmem_cache *s, gfp_t gfpflags, int node)
1663{
Christoph Lameterce15fea2007-07-17 04:03:28 -07001664 return slab_alloc(s, gfpflags, node, __builtin_return_address(0));
Christoph Lameter81819f02007-05-06 14:49:36 -07001665}
1666EXPORT_SYMBOL(kmem_cache_alloc_node);
1667#endif
1668
1669/*
Christoph Lameter894b8782007-05-10 03:15:16 -07001670 * Slow patch handling. This may still be called frequently since objects
1671 * have a longer lifetime than the cpu slabs in most processing loads.
Christoph Lameter81819f02007-05-06 14:49:36 -07001672 *
Christoph Lameter894b8782007-05-10 03:15:16 -07001673 * So we still attempt to reduce cache line usage. Just take the slab
1674 * lock and free the item. If there is no additional partial page
1675 * handling required then we can return immediately.
Christoph Lameter81819f02007-05-06 14:49:36 -07001676 */
Christoph Lameter894b8782007-05-10 03:15:16 -07001677static void __slab_free(struct kmem_cache *s, struct page *page,
Christoph Lameterb3fba8d2007-10-16 01:26:06 -07001678 void *x, void *addr, unsigned int offset)
Christoph Lameter81819f02007-05-06 14:49:36 -07001679{
1680 void *prior;
1681 void **object = (void *)x;
Christoph Lameter8ff12cf2008-02-07 17:47:41 -08001682 struct kmem_cache_cpu *c;
Christoph Lameter81819f02007-05-06 14:49:36 -07001683
Christoph Lameter1f842602008-01-07 23:20:30 -08001684#ifdef SLUB_FASTPATH
1685 unsigned long flags;
1686
1687 local_irq_save(flags);
1688#endif
Christoph Lameter8ff12cf2008-02-07 17:47:41 -08001689 c = get_cpu_slab(s, raw_smp_processor_id());
1690 stat(c, FREE_SLOWPATH);
Christoph Lameter81819f02007-05-06 14:49:36 -07001691 slab_lock(page);
1692
Christoph Lameter35e5d7e2007-05-09 02:32:42 -07001693 if (unlikely(SlabDebug(page)))
Christoph Lameter81819f02007-05-06 14:49:36 -07001694 goto debug;
1695checks_ok:
Christoph Lameterb3fba8d2007-10-16 01:26:06 -07001696 prior = object[offset] = page->freelist;
Christoph Lameter81819f02007-05-06 14:49:36 -07001697 page->freelist = object;
1698 page->inuse--;
1699
Christoph Lameter8ff12cf2008-02-07 17:47:41 -08001700 if (unlikely(SlabFrozen(page))) {
1701 stat(c, FREE_FROZEN);
Christoph Lameter81819f02007-05-06 14:49:36 -07001702 goto out_unlock;
Christoph Lameter8ff12cf2008-02-07 17:47:41 -08001703 }
Christoph Lameter81819f02007-05-06 14:49:36 -07001704
1705 if (unlikely(!page->inuse))
1706 goto slab_empty;
1707
1708 /*
1709 * Objects left in the slab. If it
1710 * was not on the partial list before
1711 * then add it.
1712 */
Christoph Lameter8ff12cf2008-02-07 17:47:41 -08001713 if (unlikely(prior == page->end)) {
Christoph Lameter7c2e1322008-01-07 23:20:27 -08001714 add_partial(get_node(s, page_to_nid(page)), page, 1);
Christoph Lameter8ff12cf2008-02-07 17:47:41 -08001715 stat(c, FREE_ADD_PARTIAL);
1716 }
Christoph Lameter81819f02007-05-06 14:49:36 -07001717
1718out_unlock:
1719 slab_unlock(page);
Christoph Lameter1f842602008-01-07 23:20:30 -08001720#ifdef SLUB_FASTPATH
1721 local_irq_restore(flags);
1722#endif
Christoph Lameter81819f02007-05-06 14:49:36 -07001723 return;
1724
1725slab_empty:
Christoph Lameter8ff12cf2008-02-07 17:47:41 -08001726 if (prior != page->end) {
Christoph Lameter81819f02007-05-06 14:49:36 -07001727 /*
Christoph Lameter672bba32007-05-09 02:32:39 -07001728 * Slab still on the partial list.
Christoph Lameter81819f02007-05-06 14:49:36 -07001729 */
1730 remove_partial(s, page);
Christoph Lameter8ff12cf2008-02-07 17:47:41 -08001731 stat(c, FREE_REMOVE_PARTIAL);
1732 }
Christoph Lameter81819f02007-05-06 14:49:36 -07001733 slab_unlock(page);
Christoph Lameter8ff12cf2008-02-07 17:47:41 -08001734 stat(c, FREE_SLAB);
Christoph Lameter1f842602008-01-07 23:20:30 -08001735#ifdef SLUB_FASTPATH
1736 local_irq_restore(flags);
1737#endif
Christoph Lameter81819f02007-05-06 14:49:36 -07001738 discard_slab(s, page);
Christoph Lameter81819f02007-05-06 14:49:36 -07001739 return;
1740
1741debug:
Christoph Lameter3ec09742007-05-16 22:11:00 -07001742 if (!free_debug_processing(s, page, x, addr))
Christoph Lameter77c5e2d2007-05-06 14:49:42 -07001743 goto out_unlock;
Christoph Lameter77c5e2d2007-05-06 14:49:42 -07001744 goto checks_ok;
Christoph Lameter81819f02007-05-06 14:49:36 -07001745}
1746
Christoph Lameter894b8782007-05-10 03:15:16 -07001747/*
1748 * Fastpath with forced inlining to produce a kfree and kmem_cache_free that
1749 * can perform fastpath freeing without additional function calls.
1750 *
1751 * The fastpath is only possible if we are freeing to the current cpu slab
1752 * of this processor. This typically the case if we have just allocated
1753 * the item before.
1754 *
1755 * If fastpath is not possible then fall back to __slab_free where we deal
1756 * with all sorts of special processing.
1757 */
Pekka Enberg06428782008-01-07 23:20:27 -08001758static __always_inline void slab_free(struct kmem_cache *s,
Christoph Lameter894b8782007-05-10 03:15:16 -07001759 struct page *page, void *x, void *addr)
1760{
1761 void **object = (void *)x;
Christoph Lameterdfb4f092007-10-16 01:26:05 -07001762 struct kmem_cache_cpu *c;
Christoph Lameter894b8782007-05-10 03:15:16 -07001763
Christoph Lameter1f842602008-01-07 23:20:30 -08001764#ifdef SLUB_FASTPATH
1765 void **freelist;
1766
1767 c = get_cpu_slab(s, raw_smp_processor_id());
1768 debug_check_no_locks_freed(object, s->objsize);
1769 do {
1770 freelist = c->freelist;
1771 barrier();
1772 /*
1773 * If the compiler would reorder the retrieval of c->page to
1774 * come before c->freelist then an interrupt could
1775 * change the cpu slab before we retrieve c->freelist. We
1776 * could be matching on a page no longer active and put the
1777 * object onto the freelist of the wrong slab.
1778 *
1779 * On the other hand: If we already have the freelist pointer
1780 * then any change of cpu_slab will cause the cmpxchg to fail
1781 * since the freelist pointers are unique per slab.
1782 */
1783 if (unlikely(page != c->page || c->node < 0)) {
1784 __slab_free(s, page, x, addr, c->offset);
1785 break;
1786 }
1787 object[c->offset] = freelist;
Christoph Lameter8ff12cf2008-02-07 17:47:41 -08001788 stat(c, FREE_FASTPATH);
Christoph Lameter1f842602008-01-07 23:20:30 -08001789 } while (cmpxchg_local(&c->freelist, freelist, object) != freelist);
1790#else
1791 unsigned long flags;
1792
Christoph Lameter894b8782007-05-10 03:15:16 -07001793 local_irq_save(flags);
Peter Zijlstra02febdf2007-07-26 20:01:38 +02001794 debug_check_no_locks_freed(object, s->objsize);
Christoph Lameterdfb4f092007-10-16 01:26:05 -07001795 c = get_cpu_slab(s, smp_processor_id());
Christoph Lameteree3c72a2007-10-16 01:26:07 -07001796 if (likely(page == c->page && c->node >= 0)) {
Christoph Lameterb3fba8d2007-10-16 01:26:06 -07001797 object[c->offset] = c->freelist;
Christoph Lameterdfb4f092007-10-16 01:26:05 -07001798 c->freelist = object;
Christoph Lameter8ff12cf2008-02-07 17:47:41 -08001799 stat(c, FREE_FASTPATH);
Christoph Lameter894b8782007-05-10 03:15:16 -07001800 } else
Christoph Lameterb3fba8d2007-10-16 01:26:06 -07001801 __slab_free(s, page, x, addr, c->offset);
Christoph Lameter894b8782007-05-10 03:15:16 -07001802
1803 local_irq_restore(flags);
Christoph Lameter1f842602008-01-07 23:20:30 -08001804#endif
Christoph Lameter894b8782007-05-10 03:15:16 -07001805}
1806
Christoph Lameter81819f02007-05-06 14:49:36 -07001807void kmem_cache_free(struct kmem_cache *s, void *x)
1808{
Christoph Lameter77c5e2d2007-05-06 14:49:42 -07001809 struct page *page;
Christoph Lameter81819f02007-05-06 14:49:36 -07001810
Christoph Lameterb49af682007-05-06 14:49:41 -07001811 page = virt_to_head_page(x);
Christoph Lameter81819f02007-05-06 14:49:36 -07001812
Christoph Lameter77c5e2d2007-05-06 14:49:42 -07001813 slab_free(s, page, x, __builtin_return_address(0));
Christoph Lameter81819f02007-05-06 14:49:36 -07001814}
1815EXPORT_SYMBOL(kmem_cache_free);
1816
1817/* Figure out on which slab object the object resides */
1818static struct page *get_object_page(const void *x)
1819{
Christoph Lameterb49af682007-05-06 14:49:41 -07001820 struct page *page = virt_to_head_page(x);
Christoph Lameter81819f02007-05-06 14:49:36 -07001821
1822 if (!PageSlab(page))
1823 return NULL;
1824
1825 return page;
1826}
1827
1828/*
Christoph Lameter672bba32007-05-09 02:32:39 -07001829 * Object placement in a slab is made very easy because we always start at
1830 * offset 0. If we tune the size of the object to the alignment then we can
1831 * get the required alignment by putting one properly sized object after
1832 * another.
Christoph Lameter81819f02007-05-06 14:49:36 -07001833 *
1834 * Notice that the allocation order determines the sizes of the per cpu
1835 * caches. Each processor has always one slab available for allocations.
1836 * Increasing the allocation order reduces the number of times that slabs
Christoph Lameter672bba32007-05-09 02:32:39 -07001837 * must be moved on and off the partial lists and is therefore a factor in
Christoph Lameter81819f02007-05-06 14:49:36 -07001838 * locking overhead.
Christoph Lameter81819f02007-05-06 14:49:36 -07001839 */
1840
1841/*
1842 * Mininum / Maximum order of slab pages. This influences locking overhead
1843 * and slab fragmentation. A higher order reduces the number of partial slabs
1844 * and increases the number of allocations possible without having to
1845 * take the list_lock.
1846 */
1847static int slub_min_order;
1848static int slub_max_order = DEFAULT_MAX_ORDER;
Christoph Lameter81819f02007-05-06 14:49:36 -07001849static int slub_min_objects = DEFAULT_MIN_OBJECTS;
1850
1851/*
1852 * Merge control. If this is set then no merging of slab caches will occur.
Christoph Lameter672bba32007-05-09 02:32:39 -07001853 * (Could be removed. This was introduced to pacify the merge skeptics.)
Christoph Lameter81819f02007-05-06 14:49:36 -07001854 */
1855static int slub_nomerge;
1856
1857/*
Christoph Lameter81819f02007-05-06 14:49:36 -07001858 * Calculate the order of allocation given an slab object size.
1859 *
Christoph Lameter672bba32007-05-09 02:32:39 -07001860 * The order of allocation has significant impact on performance and other
1861 * system components. Generally order 0 allocations should be preferred since
1862 * order 0 does not cause fragmentation in the page allocator. Larger objects
1863 * be problematic to put into order 0 slabs because there may be too much
1864 * unused space left. We go to a higher order if more than 1/8th of the slab
1865 * would be wasted.
Christoph Lameter81819f02007-05-06 14:49:36 -07001866 *
Christoph Lameter672bba32007-05-09 02:32:39 -07001867 * In order to reach satisfactory performance we must ensure that a minimum
1868 * number of objects is in one slab. Otherwise we may generate too much
1869 * activity on the partial lists which requires taking the list_lock. This is
1870 * less a concern for large slabs though which are rarely used.
Christoph Lameter81819f02007-05-06 14:49:36 -07001871 *
Christoph Lameter672bba32007-05-09 02:32:39 -07001872 * slub_max_order specifies the order where we begin to stop considering the
1873 * number of objects in a slab as critical. If we reach slub_max_order then
1874 * we try to keep the page order as low as possible. So we accept more waste
1875 * of space in favor of a small page order.
1876 *
1877 * Higher order allocations also allow the placement of more objects in a
1878 * slab and thereby reduce object handling overhead. If the user has
1879 * requested a higher mininum order then we start with that one instead of
1880 * the smallest order which will fit the object.
Christoph Lameter81819f02007-05-06 14:49:36 -07001881 */
Christoph Lameter5e6d4442007-05-09 02:32:46 -07001882static inline int slab_order(int size, int min_objects,
1883 int max_order, int fract_leftover)
Christoph Lameter81819f02007-05-06 14:49:36 -07001884{
1885 int order;
1886 int rem;
Christoph Lameter6300ea72007-07-17 04:03:20 -07001887 int min_order = slub_min_order;
Christoph Lameter81819f02007-05-06 14:49:36 -07001888
Christoph Lameter6300ea72007-07-17 04:03:20 -07001889 for (order = max(min_order,
Christoph Lameter5e6d4442007-05-09 02:32:46 -07001890 fls(min_objects * size - 1) - PAGE_SHIFT);
1891 order <= max_order; order++) {
1892
Christoph Lameter81819f02007-05-06 14:49:36 -07001893 unsigned long slab_size = PAGE_SIZE << order;
1894
Christoph Lameter5e6d4442007-05-09 02:32:46 -07001895 if (slab_size < min_objects * size)
Christoph Lameter81819f02007-05-06 14:49:36 -07001896 continue;
1897
Christoph Lameter81819f02007-05-06 14:49:36 -07001898 rem = slab_size % size;
1899
Christoph Lameter5e6d4442007-05-09 02:32:46 -07001900 if (rem <= slab_size / fract_leftover)
Christoph Lameter81819f02007-05-06 14:49:36 -07001901 break;
1902
1903 }
Christoph Lameter672bba32007-05-09 02:32:39 -07001904
Christoph Lameter81819f02007-05-06 14:49:36 -07001905 return order;
1906}
1907
Christoph Lameter5e6d4442007-05-09 02:32:46 -07001908static inline int calculate_order(int size)
1909{
1910 int order;
1911 int min_objects;
1912 int fraction;
1913
1914 /*
1915 * Attempt to find best configuration for a slab. This
1916 * works by first attempting to generate a layout with
1917 * the best configuration and backing off gradually.
1918 *
1919 * First we reduce the acceptable waste in a slab. Then
1920 * we reduce the minimum objects required in a slab.
1921 */
1922 min_objects = slub_min_objects;
1923 while (min_objects > 1) {
1924 fraction = 8;
1925 while (fraction >= 4) {
1926 order = slab_order(size, min_objects,
1927 slub_max_order, fraction);
1928 if (order <= slub_max_order)
1929 return order;
1930 fraction /= 2;
1931 }
1932 min_objects /= 2;
1933 }
1934
1935 /*
1936 * We were unable to place multiple objects in a slab. Now
1937 * lets see if we can place a single object there.
1938 */
1939 order = slab_order(size, 1, slub_max_order, 1);
1940 if (order <= slub_max_order)
1941 return order;
1942
1943 /*
1944 * Doh this slab cannot be placed using slub_max_order.
1945 */
1946 order = slab_order(size, 1, MAX_ORDER, 1);
1947 if (order <= MAX_ORDER)
1948 return order;
1949 return -ENOSYS;
1950}
1951
Christoph Lameter81819f02007-05-06 14:49:36 -07001952/*
Christoph Lameter672bba32007-05-09 02:32:39 -07001953 * Figure out what the alignment of the objects will be.
Christoph Lameter81819f02007-05-06 14:49:36 -07001954 */
1955static unsigned long calculate_alignment(unsigned long flags,
1956 unsigned long align, unsigned long size)
1957{
1958 /*
1959 * If the user wants hardware cache aligned objects then
1960 * follow that suggestion if the object is sufficiently
1961 * large.
1962 *
1963 * The hardware cache alignment cannot override the
1964 * specified alignment though. If that is greater
1965 * then use it.
1966 */
Christoph Lameter5af60832007-05-06 14:49:56 -07001967 if ((flags & SLAB_HWCACHE_ALIGN) &&
Christoph Lameter65c02d42007-05-09 02:32:35 -07001968 size > cache_line_size() / 2)
1969 return max_t(unsigned long, align, cache_line_size());
Christoph Lameter81819f02007-05-06 14:49:36 -07001970
1971 if (align < ARCH_SLAB_MINALIGN)
1972 return ARCH_SLAB_MINALIGN;
1973
1974 return ALIGN(align, sizeof(void *));
1975}
1976
Christoph Lameterdfb4f092007-10-16 01:26:05 -07001977static void init_kmem_cache_cpu(struct kmem_cache *s,
1978 struct kmem_cache_cpu *c)
1979{
1980 c->page = NULL;
Christoph Lameter683d0ba2008-01-07 23:20:29 -08001981 c->freelist = (void *)PAGE_MAPPING_ANON;
Christoph Lameterdfb4f092007-10-16 01:26:05 -07001982 c->node = 0;
Christoph Lameter42a9fdb2007-10-16 01:26:09 -07001983 c->offset = s->offset / sizeof(void *);
1984 c->objsize = s->objsize;
Christoph Lameterdfb4f092007-10-16 01:26:05 -07001985}
1986
Christoph Lameter81819f02007-05-06 14:49:36 -07001987static void init_kmem_cache_node(struct kmem_cache_node *n)
1988{
1989 n->nr_partial = 0;
1990 atomic_long_set(&n->nr_slabs, 0);
1991 spin_lock_init(&n->list_lock);
1992 INIT_LIST_HEAD(&n->partial);
Christoph Lameter8ab13722007-07-17 04:03:32 -07001993#ifdef CONFIG_SLUB_DEBUG
Christoph Lameter643b1132007-05-06 14:49:42 -07001994 INIT_LIST_HEAD(&n->full);
Christoph Lameter8ab13722007-07-17 04:03:32 -07001995#endif
Christoph Lameter81819f02007-05-06 14:49:36 -07001996}
1997
Christoph Lameter4c93c3552007-10-16 01:26:08 -07001998#ifdef CONFIG_SMP
1999/*
2000 * Per cpu array for per cpu structures.
2001 *
2002 * The per cpu array places all kmem_cache_cpu structures from one processor
2003 * close together meaning that it becomes possible that multiple per cpu
2004 * structures are contained in one cacheline. This may be particularly
2005 * beneficial for the kmalloc caches.
2006 *
2007 * A desktop system typically has around 60-80 slabs. With 100 here we are
2008 * likely able to get per cpu structures for all caches from the array defined
2009 * here. We must be able to cover all kmalloc caches during bootstrap.
2010 *
2011 * If the per cpu array is exhausted then fall back to kmalloc
2012 * of individual cachelines. No sharing is possible then.
2013 */
2014#define NR_KMEM_CACHE_CPU 100
2015
2016static DEFINE_PER_CPU(struct kmem_cache_cpu,
2017 kmem_cache_cpu)[NR_KMEM_CACHE_CPU];
2018
2019static DEFINE_PER_CPU(struct kmem_cache_cpu *, kmem_cache_cpu_free);
2020static cpumask_t kmem_cach_cpu_free_init_once = CPU_MASK_NONE;
2021
2022static struct kmem_cache_cpu *alloc_kmem_cache_cpu(struct kmem_cache *s,
2023 int cpu, gfp_t flags)
2024{
2025 struct kmem_cache_cpu *c = per_cpu(kmem_cache_cpu_free, cpu);
2026
2027 if (c)
2028 per_cpu(kmem_cache_cpu_free, cpu) =
2029 (void *)c->freelist;
2030 else {
2031 /* Table overflow: So allocate ourselves */
2032 c = kmalloc_node(
2033 ALIGN(sizeof(struct kmem_cache_cpu), cache_line_size()),
2034 flags, cpu_to_node(cpu));
2035 if (!c)
2036 return NULL;
2037 }
2038
2039 init_kmem_cache_cpu(s, c);
2040 return c;
2041}
2042
2043static void free_kmem_cache_cpu(struct kmem_cache_cpu *c, int cpu)
2044{
2045 if (c < per_cpu(kmem_cache_cpu, cpu) ||
2046 c > per_cpu(kmem_cache_cpu, cpu) + NR_KMEM_CACHE_CPU) {
2047 kfree(c);
2048 return;
2049 }
2050 c->freelist = (void *)per_cpu(kmem_cache_cpu_free, cpu);
2051 per_cpu(kmem_cache_cpu_free, cpu) = c;
2052}
2053
2054static void free_kmem_cache_cpus(struct kmem_cache *s)
2055{
2056 int cpu;
2057
2058 for_each_online_cpu(cpu) {
2059 struct kmem_cache_cpu *c = get_cpu_slab(s, cpu);
2060
2061 if (c) {
2062 s->cpu_slab[cpu] = NULL;
2063 free_kmem_cache_cpu(c, cpu);
2064 }
2065 }
2066}
2067
2068static int alloc_kmem_cache_cpus(struct kmem_cache *s, gfp_t flags)
2069{
2070 int cpu;
2071
2072 for_each_online_cpu(cpu) {
2073 struct kmem_cache_cpu *c = get_cpu_slab(s, cpu);
2074
2075 if (c)
2076 continue;
2077
2078 c = alloc_kmem_cache_cpu(s, cpu, flags);
2079 if (!c) {
2080 free_kmem_cache_cpus(s);
2081 return 0;
2082 }
2083 s->cpu_slab[cpu] = c;
2084 }
2085 return 1;
2086}
2087
2088/*
2089 * Initialize the per cpu array.
2090 */
2091static void init_alloc_cpu_cpu(int cpu)
2092{
2093 int i;
2094
2095 if (cpu_isset(cpu, kmem_cach_cpu_free_init_once))
2096 return;
2097
2098 for (i = NR_KMEM_CACHE_CPU - 1; i >= 0; i--)
2099 free_kmem_cache_cpu(&per_cpu(kmem_cache_cpu, cpu)[i], cpu);
2100
2101 cpu_set(cpu, kmem_cach_cpu_free_init_once);
2102}
2103
2104static void __init init_alloc_cpu(void)
2105{
2106 int cpu;
2107
2108 for_each_online_cpu(cpu)
2109 init_alloc_cpu_cpu(cpu);
2110 }
2111
2112#else
2113static inline void free_kmem_cache_cpus(struct kmem_cache *s) {}
2114static inline void init_alloc_cpu(void) {}
2115
2116static inline int alloc_kmem_cache_cpus(struct kmem_cache *s, gfp_t flags)
2117{
2118 init_kmem_cache_cpu(s, &s->cpu_slab);
2119 return 1;
2120}
2121#endif
2122
Christoph Lameter81819f02007-05-06 14:49:36 -07002123#ifdef CONFIG_NUMA
2124/*
2125 * No kmalloc_node yet so do it by hand. We know that this is the first
2126 * slab on the node for this slabcache. There are no concurrent accesses
2127 * possible.
2128 *
2129 * Note that this function only works on the kmalloc_node_cache
Christoph Lameter4c93c3552007-10-16 01:26:08 -07002130 * when allocating for the kmalloc_node_cache. This is used for bootstrapping
2131 * memory on a fresh node that has no slab structures yet.
Christoph Lameter81819f02007-05-06 14:49:36 -07002132 */
Adrian Bunk1cd7daa2007-10-16 01:24:18 -07002133static struct kmem_cache_node *early_kmem_cache_node_alloc(gfp_t gfpflags,
2134 int node)
Christoph Lameter81819f02007-05-06 14:49:36 -07002135{
2136 struct page *page;
2137 struct kmem_cache_node *n;
rootba84c732008-01-07 23:20:28 -08002138 unsigned long flags;
Christoph Lameter81819f02007-05-06 14:49:36 -07002139
2140 BUG_ON(kmalloc_caches->size < sizeof(struct kmem_cache_node));
2141
Christoph Lametera2f92ee2007-08-22 14:01:57 -07002142 page = new_slab(kmalloc_caches, gfpflags, node);
Christoph Lameter81819f02007-05-06 14:49:36 -07002143
2144 BUG_ON(!page);
Christoph Lametera2f92ee2007-08-22 14:01:57 -07002145 if (page_to_nid(page) != node) {
2146 printk(KERN_ERR "SLUB: Unable to allocate memory from "
2147 "node %d\n", node);
2148 printk(KERN_ERR "SLUB: Allocating a useless per node structure "
2149 "in order to be able to continue\n");
2150 }
2151
Christoph Lameter81819f02007-05-06 14:49:36 -07002152 n = page->freelist;
2153 BUG_ON(!n);
2154 page->freelist = get_freepointer(kmalloc_caches, n);
2155 page->inuse++;
2156 kmalloc_caches->node[node] = n;
Christoph Lameter8ab13722007-07-17 04:03:32 -07002157#ifdef CONFIG_SLUB_DEBUG
Christoph Lameterd45f39c2007-07-17 04:03:21 -07002158 init_object(kmalloc_caches, n, 1);
2159 init_tracking(kmalloc_caches, n);
Christoph Lameter8ab13722007-07-17 04:03:32 -07002160#endif
Christoph Lameter81819f02007-05-06 14:49:36 -07002161 init_kmem_cache_node(n);
2162 atomic_long_inc(&n->nr_slabs);
rootba84c732008-01-07 23:20:28 -08002163 /*
2164 * lockdep requires consistent irq usage for each lock
2165 * so even though there cannot be a race this early in
2166 * the boot sequence, we still disable irqs.
2167 */
2168 local_irq_save(flags);
Christoph Lameter7c2e1322008-01-07 23:20:27 -08002169 add_partial(n, page, 0);
rootba84c732008-01-07 23:20:28 -08002170 local_irq_restore(flags);
Christoph Lameter81819f02007-05-06 14:49:36 -07002171 return n;
2172}
2173
2174static void free_kmem_cache_nodes(struct kmem_cache *s)
2175{
2176 int node;
2177
Christoph Lameterf64dc582007-10-16 01:25:33 -07002178 for_each_node_state(node, N_NORMAL_MEMORY) {
Christoph Lameter81819f02007-05-06 14:49:36 -07002179 struct kmem_cache_node *n = s->node[node];
2180 if (n && n != &s->local_node)
2181 kmem_cache_free(kmalloc_caches, n);
2182 s->node[node] = NULL;
2183 }
2184}
2185
2186static int init_kmem_cache_nodes(struct kmem_cache *s, gfp_t gfpflags)
2187{
2188 int node;
2189 int local_node;
2190
2191 if (slab_state >= UP)
2192 local_node = page_to_nid(virt_to_page(s));
2193 else
2194 local_node = 0;
2195
Christoph Lameterf64dc582007-10-16 01:25:33 -07002196 for_each_node_state(node, N_NORMAL_MEMORY) {
Christoph Lameter81819f02007-05-06 14:49:36 -07002197 struct kmem_cache_node *n;
2198
2199 if (local_node == node)
2200 n = &s->local_node;
2201 else {
2202 if (slab_state == DOWN) {
2203 n = early_kmem_cache_node_alloc(gfpflags,
2204 node);
2205 continue;
2206 }
2207 n = kmem_cache_alloc_node(kmalloc_caches,
2208 gfpflags, node);
2209
2210 if (!n) {
2211 free_kmem_cache_nodes(s);
2212 return 0;
2213 }
2214
2215 }
2216 s->node[node] = n;
2217 init_kmem_cache_node(n);
2218 }
2219 return 1;
2220}
2221#else
2222static void free_kmem_cache_nodes(struct kmem_cache *s)
2223{
2224}
2225
2226static int init_kmem_cache_nodes(struct kmem_cache *s, gfp_t gfpflags)
2227{
2228 init_kmem_cache_node(&s->local_node);
2229 return 1;
2230}
2231#endif
2232
2233/*
2234 * calculate_sizes() determines the order and the distribution of data within
2235 * a slab object.
2236 */
2237static int calculate_sizes(struct kmem_cache *s)
2238{
2239 unsigned long flags = s->flags;
2240 unsigned long size = s->objsize;
2241 unsigned long align = s->align;
2242
2243 /*
2244 * Determine if we can poison the object itself. If the user of
2245 * the slab may touch the object after free or before allocation
2246 * then we should never poison the object itself.
2247 */
2248 if ((flags & SLAB_POISON) && !(flags & SLAB_DESTROY_BY_RCU) &&
Christoph Lameterc59def9f2007-05-16 22:10:50 -07002249 !s->ctor)
Christoph Lameter81819f02007-05-06 14:49:36 -07002250 s->flags |= __OBJECT_POISON;
2251 else
2252 s->flags &= ~__OBJECT_POISON;
2253
2254 /*
2255 * Round up object size to the next word boundary. We can only
2256 * place the free pointer at word boundaries and this determines
2257 * the possible location of the free pointer.
2258 */
2259 size = ALIGN(size, sizeof(void *));
2260
Christoph Lameter41ecc552007-05-09 02:32:44 -07002261#ifdef CONFIG_SLUB_DEBUG
Christoph Lameter81819f02007-05-06 14:49:36 -07002262 /*
Christoph Lameter672bba32007-05-09 02:32:39 -07002263 * If we are Redzoning then check if there is some space between the
Christoph Lameter81819f02007-05-06 14:49:36 -07002264 * end of the object and the free pointer. If not then add an
Christoph Lameter672bba32007-05-09 02:32:39 -07002265 * additional word to have some bytes to store Redzone information.
Christoph Lameter81819f02007-05-06 14:49:36 -07002266 */
2267 if ((flags & SLAB_RED_ZONE) && size == s->objsize)
2268 size += sizeof(void *);
Christoph Lameter41ecc552007-05-09 02:32:44 -07002269#endif
Christoph Lameter81819f02007-05-06 14:49:36 -07002270
2271 /*
Christoph Lameter672bba32007-05-09 02:32:39 -07002272 * With that we have determined the number of bytes in actual use
2273 * by the object. This is the potential offset to the free pointer.
Christoph Lameter81819f02007-05-06 14:49:36 -07002274 */
2275 s->inuse = size;
2276
2277 if (((flags & (SLAB_DESTROY_BY_RCU | SLAB_POISON)) ||
Christoph Lameterc59def9f2007-05-16 22:10:50 -07002278 s->ctor)) {
Christoph Lameter81819f02007-05-06 14:49:36 -07002279 /*
2280 * Relocate free pointer after the object if it is not
2281 * permitted to overwrite the first word of the object on
2282 * kmem_cache_free.
2283 *
2284 * This is the case if we do RCU, have a constructor or
2285 * destructor or are poisoning the objects.
2286 */
2287 s->offset = size;
2288 size += sizeof(void *);
2289 }
2290
Christoph Lameterc12b3c62007-05-23 13:57:31 -07002291#ifdef CONFIG_SLUB_DEBUG
Christoph Lameter81819f02007-05-06 14:49:36 -07002292 if (flags & SLAB_STORE_USER)
2293 /*
2294 * Need to store information about allocs and frees after
2295 * the object.
2296 */
2297 size += 2 * sizeof(struct track);
2298
Christoph Lameterbe7b3fb2007-05-09 02:32:36 -07002299 if (flags & SLAB_RED_ZONE)
Christoph Lameter81819f02007-05-06 14:49:36 -07002300 /*
2301 * Add some empty padding so that we can catch
2302 * overwrites from earlier objects rather than let
2303 * tracking information or the free pointer be
2304 * corrupted if an user writes before the start
2305 * of the object.
2306 */
2307 size += sizeof(void *);
Christoph Lameter41ecc552007-05-09 02:32:44 -07002308#endif
Christoph Lameter672bba32007-05-09 02:32:39 -07002309
Christoph Lameter81819f02007-05-06 14:49:36 -07002310 /*
2311 * Determine the alignment based on various parameters that the
Christoph Lameter65c02d42007-05-09 02:32:35 -07002312 * user specified and the dynamic determination of cache line size
2313 * on bootup.
Christoph Lameter81819f02007-05-06 14:49:36 -07002314 */
2315 align = calculate_alignment(flags, align, s->objsize);
2316
2317 /*
2318 * SLUB stores one object immediately after another beginning from
2319 * offset 0. In order to align the objects we have to simply size
2320 * each object to conform to the alignment.
2321 */
2322 size = ALIGN(size, align);
2323 s->size = size;
2324
2325 s->order = calculate_order(size);
2326 if (s->order < 0)
2327 return 0;
2328
Christoph Lameterb7a49f02008-02-14 14:21:32 -08002329 s->allocflags = 0;
2330 if (s->order)
2331 s->allocflags |= __GFP_COMP;
2332
2333 if (s->flags & SLAB_CACHE_DMA)
2334 s->allocflags |= SLUB_DMA;
2335
2336 if (s->flags & SLAB_RECLAIM_ACCOUNT)
2337 s->allocflags |= __GFP_RECLAIMABLE;
2338
Christoph Lameter81819f02007-05-06 14:49:36 -07002339 /*
2340 * Determine the number of objects per slab
2341 */
2342 s->objects = (PAGE_SIZE << s->order) / size;
2343
Christoph Lameterb3fba8d2007-10-16 01:26:06 -07002344 return !!s->objects;
Christoph Lameter81819f02007-05-06 14:49:36 -07002345
2346}
2347
Christoph Lameter81819f02007-05-06 14:49:36 -07002348static int kmem_cache_open(struct kmem_cache *s, gfp_t gfpflags,
2349 const char *name, size_t size,
2350 size_t align, unsigned long flags,
Christoph Lameter4ba9b9d2007-10-16 23:25:51 -07002351 void (*ctor)(struct kmem_cache *, void *))
Christoph Lameter81819f02007-05-06 14:49:36 -07002352{
2353 memset(s, 0, kmem_size);
2354 s->name = name;
2355 s->ctor = ctor;
Christoph Lameter81819f02007-05-06 14:49:36 -07002356 s->objsize = size;
Christoph Lameter81819f02007-05-06 14:49:36 -07002357 s->align = align;
Christoph Lameterba0268a2007-09-11 15:24:11 -07002358 s->flags = kmem_cache_flags(size, flags, name, ctor);
Christoph Lameter81819f02007-05-06 14:49:36 -07002359
2360 if (!calculate_sizes(s))
2361 goto error;
2362
2363 s->refcount = 1;
2364#ifdef CONFIG_NUMA
Christoph Lameter98246012008-01-07 23:20:26 -08002365 s->remote_node_defrag_ratio = 100;
Christoph Lameter81819f02007-05-06 14:49:36 -07002366#endif
Christoph Lameterdfb4f092007-10-16 01:26:05 -07002367 if (!init_kmem_cache_nodes(s, gfpflags & ~SLUB_DMA))
2368 goto error;
Christoph Lameter81819f02007-05-06 14:49:36 -07002369
Christoph Lameterdfb4f092007-10-16 01:26:05 -07002370 if (alloc_kmem_cache_cpus(s, gfpflags & ~SLUB_DMA))
Christoph Lameter81819f02007-05-06 14:49:36 -07002371 return 1;
Christoph Lameter4c93c3552007-10-16 01:26:08 -07002372 free_kmem_cache_nodes(s);
Christoph Lameter81819f02007-05-06 14:49:36 -07002373error:
2374 if (flags & SLAB_PANIC)
2375 panic("Cannot create slab %s size=%lu realsize=%u "
2376 "order=%u offset=%u flags=%lx\n",
2377 s->name, (unsigned long)size, s->size, s->order,
2378 s->offset, flags);
2379 return 0;
2380}
Christoph Lameter81819f02007-05-06 14:49:36 -07002381
2382/*
2383 * Check if a given pointer is valid
2384 */
2385int kmem_ptr_validate(struct kmem_cache *s, const void *object)
2386{
Pekka Enberg06428782008-01-07 23:20:27 -08002387 struct page *page;
Christoph Lameter81819f02007-05-06 14:49:36 -07002388
2389 page = get_object_page(object);
2390
2391 if (!page || s != page->slab)
2392 /* No slab or wrong slab */
2393 return 0;
2394
Christoph Lameterabcd08a2007-05-09 02:32:37 -07002395 if (!check_valid_pointer(s, page, object))
Christoph Lameter81819f02007-05-06 14:49:36 -07002396 return 0;
2397
2398 /*
2399 * We could also check if the object is on the slabs freelist.
2400 * But this would be too expensive and it seems that the main
2401 * purpose of kmem_ptr_valid is to check if the object belongs
2402 * to a certain slab.
2403 */
2404 return 1;
2405}
2406EXPORT_SYMBOL(kmem_ptr_validate);
2407
2408/*
2409 * Determine the size of a slab object
2410 */
2411unsigned int kmem_cache_size(struct kmem_cache *s)
2412{
2413 return s->objsize;
2414}
2415EXPORT_SYMBOL(kmem_cache_size);
2416
2417const char *kmem_cache_name(struct kmem_cache *s)
2418{
2419 return s->name;
2420}
2421EXPORT_SYMBOL(kmem_cache_name);
2422
2423/*
Christoph Lameter672bba32007-05-09 02:32:39 -07002424 * Attempt to free all slabs on a node. Return the number of slabs we
2425 * were unable to free.
Christoph Lameter81819f02007-05-06 14:49:36 -07002426 */
2427static int free_list(struct kmem_cache *s, struct kmem_cache_node *n,
2428 struct list_head *list)
2429{
2430 int slabs_inuse = 0;
2431 unsigned long flags;
2432 struct page *page, *h;
2433
2434 spin_lock_irqsave(&n->list_lock, flags);
2435 list_for_each_entry_safe(page, h, list, lru)
2436 if (!page->inuse) {
2437 list_del(&page->lru);
2438 discard_slab(s, page);
2439 } else
2440 slabs_inuse++;
2441 spin_unlock_irqrestore(&n->list_lock, flags);
2442 return slabs_inuse;
2443}
2444
2445/*
Christoph Lameter672bba32007-05-09 02:32:39 -07002446 * Release all resources used by a slab cache.
Christoph Lameter81819f02007-05-06 14:49:36 -07002447 */
Christoph Lameter0c710012007-07-17 04:03:24 -07002448static inline int kmem_cache_close(struct kmem_cache *s)
Christoph Lameter81819f02007-05-06 14:49:36 -07002449{
2450 int node;
2451
2452 flush_all(s);
2453
2454 /* Attempt to free all objects */
Christoph Lameter4c93c3552007-10-16 01:26:08 -07002455 free_kmem_cache_cpus(s);
Christoph Lameterf64dc582007-10-16 01:25:33 -07002456 for_each_node_state(node, N_NORMAL_MEMORY) {
Christoph Lameter81819f02007-05-06 14:49:36 -07002457 struct kmem_cache_node *n = get_node(s, node);
2458
Christoph Lameter2086d262007-05-06 14:49:46 -07002459 n->nr_partial -= free_list(s, n, &n->partial);
Christoph Lameter81819f02007-05-06 14:49:36 -07002460 if (atomic_long_read(&n->nr_slabs))
2461 return 1;
2462 }
2463 free_kmem_cache_nodes(s);
2464 return 0;
2465}
2466
2467/*
2468 * Close a cache and release the kmem_cache structure
2469 * (must be used for caches created using kmem_cache_create)
2470 */
2471void kmem_cache_destroy(struct kmem_cache *s)
2472{
2473 down_write(&slub_lock);
2474 s->refcount--;
2475 if (!s->refcount) {
2476 list_del(&s->list);
Christoph Lametera0e1d1b2007-07-17 04:03:31 -07002477 up_write(&slub_lock);
Christoph Lameter81819f02007-05-06 14:49:36 -07002478 if (kmem_cache_close(s))
2479 WARN_ON(1);
2480 sysfs_slab_remove(s);
Christoph Lametera0e1d1b2007-07-17 04:03:31 -07002481 } else
2482 up_write(&slub_lock);
Christoph Lameter81819f02007-05-06 14:49:36 -07002483}
2484EXPORT_SYMBOL(kmem_cache_destroy);
2485
2486/********************************************************************
2487 * Kmalloc subsystem
2488 *******************************************************************/
2489
Christoph Lameteraadb4bc2007-10-16 01:24:38 -07002490struct kmem_cache kmalloc_caches[PAGE_SHIFT] __cacheline_aligned;
Christoph Lameter81819f02007-05-06 14:49:36 -07002491EXPORT_SYMBOL(kmalloc_caches);
2492
2493#ifdef CONFIG_ZONE_DMA
Christoph Lameteraadb4bc2007-10-16 01:24:38 -07002494static struct kmem_cache *kmalloc_caches_dma[PAGE_SHIFT];
Christoph Lameter81819f02007-05-06 14:49:36 -07002495#endif
2496
2497static int __init setup_slub_min_order(char *str)
2498{
Pekka Enberg06428782008-01-07 23:20:27 -08002499 get_option(&str, &slub_min_order);
Christoph Lameter81819f02007-05-06 14:49:36 -07002500
2501 return 1;
2502}
2503
2504__setup("slub_min_order=", setup_slub_min_order);
2505
2506static int __init setup_slub_max_order(char *str)
2507{
Pekka Enberg06428782008-01-07 23:20:27 -08002508 get_option(&str, &slub_max_order);
Christoph Lameter81819f02007-05-06 14:49:36 -07002509
2510 return 1;
2511}
2512
2513__setup("slub_max_order=", setup_slub_max_order);
2514
2515static int __init setup_slub_min_objects(char *str)
2516{
Pekka Enberg06428782008-01-07 23:20:27 -08002517 get_option(&str, &slub_min_objects);
Christoph Lameter81819f02007-05-06 14:49:36 -07002518
2519 return 1;
2520}
2521
2522__setup("slub_min_objects=", setup_slub_min_objects);
2523
2524static int __init setup_slub_nomerge(char *str)
2525{
2526 slub_nomerge = 1;
2527 return 1;
2528}
2529
2530__setup("slub_nomerge", setup_slub_nomerge);
2531
Christoph Lameter81819f02007-05-06 14:49:36 -07002532static struct kmem_cache *create_kmalloc_cache(struct kmem_cache *s,
2533 const char *name, int size, gfp_t gfp_flags)
2534{
2535 unsigned int flags = 0;
2536
2537 if (gfp_flags & SLUB_DMA)
2538 flags = SLAB_CACHE_DMA;
2539
2540 down_write(&slub_lock);
2541 if (!kmem_cache_open(s, gfp_flags, name, size, ARCH_KMALLOC_MINALIGN,
Christoph Lameterc59def9f2007-05-16 22:10:50 -07002542 flags, NULL))
Christoph Lameter81819f02007-05-06 14:49:36 -07002543 goto panic;
2544
2545 list_add(&s->list, &slab_caches);
2546 up_write(&slub_lock);
2547 if (sysfs_slab_add(s))
2548 goto panic;
2549 return s;
2550
2551panic:
2552 panic("Creation of kmalloc slab %s size=%d failed.\n", name, size);
2553}
2554
Christoph Lameter2e443fd2007-07-17 04:03:24 -07002555#ifdef CONFIG_ZONE_DMA
Christoph Lameter1ceef402007-08-07 15:11:48 -07002556
2557static void sysfs_add_func(struct work_struct *w)
2558{
2559 struct kmem_cache *s;
2560
2561 down_write(&slub_lock);
2562 list_for_each_entry(s, &slab_caches, list) {
2563 if (s->flags & __SYSFS_ADD_DEFERRED) {
2564 s->flags &= ~__SYSFS_ADD_DEFERRED;
2565 sysfs_slab_add(s);
2566 }
2567 }
2568 up_write(&slub_lock);
2569}
2570
2571static DECLARE_WORK(sysfs_add_work, sysfs_add_func);
2572
Christoph Lameter2e443fd2007-07-17 04:03:24 -07002573static noinline struct kmem_cache *dma_kmalloc_cache(int index, gfp_t flags)
2574{
2575 struct kmem_cache *s;
Christoph Lameter2e443fd2007-07-17 04:03:24 -07002576 char *text;
2577 size_t realsize;
2578
2579 s = kmalloc_caches_dma[index];
2580 if (s)
2581 return s;
2582
2583 /* Dynamically create dma cache */
Christoph Lameter1ceef402007-08-07 15:11:48 -07002584 if (flags & __GFP_WAIT)
2585 down_write(&slub_lock);
2586 else {
2587 if (!down_write_trylock(&slub_lock))
2588 goto out;
2589 }
2590
2591 if (kmalloc_caches_dma[index])
2592 goto unlock_out;
Christoph Lameter2e443fd2007-07-17 04:03:24 -07002593
Christoph Lameter7b55f622007-07-17 04:03:27 -07002594 realsize = kmalloc_caches[index].objsize;
Ingo Molnar3adbefe2008-02-05 17:57:39 -08002595 text = kasprintf(flags & ~SLUB_DMA, "kmalloc_dma-%d",
2596 (unsigned int)realsize);
Christoph Lameter1ceef402007-08-07 15:11:48 -07002597 s = kmalloc(kmem_size, flags & ~SLUB_DMA);
2598
2599 if (!s || !text || !kmem_cache_open(s, flags, text,
2600 realsize, ARCH_KMALLOC_MINALIGN,
2601 SLAB_CACHE_DMA|__SYSFS_ADD_DEFERRED, NULL)) {
2602 kfree(s);
2603 kfree(text);
2604 goto unlock_out;
Christoph Lameterdfce8642007-07-17 04:03:25 -07002605 }
Christoph Lameter1ceef402007-08-07 15:11:48 -07002606
2607 list_add(&s->list, &slab_caches);
2608 kmalloc_caches_dma[index] = s;
2609
2610 schedule_work(&sysfs_add_work);
2611
2612unlock_out:
Christoph Lameterdfce8642007-07-17 04:03:25 -07002613 up_write(&slub_lock);
Christoph Lameter1ceef402007-08-07 15:11:48 -07002614out:
Christoph Lameterdfce8642007-07-17 04:03:25 -07002615 return kmalloc_caches_dma[index];
Christoph Lameter2e443fd2007-07-17 04:03:24 -07002616}
2617#endif
2618
Christoph Lameterf1b26332007-07-17 04:03:26 -07002619/*
2620 * Conversion table for small slabs sizes / 8 to the index in the
2621 * kmalloc array. This is necessary for slabs < 192 since we have non power
2622 * of two cache sizes there. The size of larger slabs can be determined using
2623 * fls.
2624 */
2625static s8 size_index[24] = {
2626 3, /* 8 */
2627 4, /* 16 */
2628 5, /* 24 */
2629 5, /* 32 */
2630 6, /* 40 */
2631 6, /* 48 */
2632 6, /* 56 */
2633 6, /* 64 */
2634 1, /* 72 */
2635 1, /* 80 */
2636 1, /* 88 */
2637 1, /* 96 */
2638 7, /* 104 */
2639 7, /* 112 */
2640 7, /* 120 */
2641 7, /* 128 */
2642 2, /* 136 */
2643 2, /* 144 */
2644 2, /* 152 */
2645 2, /* 160 */
2646 2, /* 168 */
2647 2, /* 176 */
2648 2, /* 184 */
2649 2 /* 192 */
2650};
2651
Christoph Lameter81819f02007-05-06 14:49:36 -07002652static struct kmem_cache *get_slab(size_t size, gfp_t flags)
2653{
Christoph Lameterf1b26332007-07-17 04:03:26 -07002654 int index;
Christoph Lameter81819f02007-05-06 14:49:36 -07002655
Christoph Lameterf1b26332007-07-17 04:03:26 -07002656 if (size <= 192) {
2657 if (!size)
2658 return ZERO_SIZE_PTR;
Christoph Lameter81819f02007-05-06 14:49:36 -07002659
Christoph Lameterf1b26332007-07-17 04:03:26 -07002660 index = size_index[(size - 1) / 8];
Christoph Lameteraadb4bc2007-10-16 01:24:38 -07002661 } else
Christoph Lameterf1b26332007-07-17 04:03:26 -07002662 index = fls(size - 1);
Christoph Lameter81819f02007-05-06 14:49:36 -07002663
2664#ifdef CONFIG_ZONE_DMA
Christoph Lameterf1b26332007-07-17 04:03:26 -07002665 if (unlikely((flags & SLUB_DMA)))
Christoph Lameter2e443fd2007-07-17 04:03:24 -07002666 return dma_kmalloc_cache(index, flags);
Christoph Lameterf1b26332007-07-17 04:03:26 -07002667
Christoph Lameter81819f02007-05-06 14:49:36 -07002668#endif
2669 return &kmalloc_caches[index];
2670}
2671
2672void *__kmalloc(size_t size, gfp_t flags)
2673{
Christoph Lameteraadb4bc2007-10-16 01:24:38 -07002674 struct kmem_cache *s;
Christoph Lameter81819f02007-05-06 14:49:36 -07002675
Christoph Lameteraadb4bc2007-10-16 01:24:38 -07002676 if (unlikely(size > PAGE_SIZE / 2))
Pekka Enbergeada35e2008-02-11 22:47:46 +02002677 return kmalloc_large(size, flags);
Christoph Lameteraadb4bc2007-10-16 01:24:38 -07002678
2679 s = get_slab(size, flags);
2680
2681 if (unlikely(ZERO_OR_NULL_PTR(s)))
Christoph Lameter6cb8f912007-07-17 04:03:22 -07002682 return s;
2683
Christoph Lameterce15fea2007-07-17 04:03:28 -07002684 return slab_alloc(s, flags, -1, __builtin_return_address(0));
Christoph Lameter81819f02007-05-06 14:49:36 -07002685}
2686EXPORT_SYMBOL(__kmalloc);
2687
2688#ifdef CONFIG_NUMA
2689void *__kmalloc_node(size_t size, gfp_t flags, int node)
2690{
Christoph Lameteraadb4bc2007-10-16 01:24:38 -07002691 struct kmem_cache *s;
Christoph Lameter81819f02007-05-06 14:49:36 -07002692
Christoph Lameteraadb4bc2007-10-16 01:24:38 -07002693 if (unlikely(size > PAGE_SIZE / 2))
Pekka Enbergeada35e2008-02-11 22:47:46 +02002694 return kmalloc_large(size, flags);
Christoph Lameteraadb4bc2007-10-16 01:24:38 -07002695
2696 s = get_slab(size, flags);
2697
2698 if (unlikely(ZERO_OR_NULL_PTR(s)))
Christoph Lameter6cb8f912007-07-17 04:03:22 -07002699 return s;
2700
Christoph Lameterce15fea2007-07-17 04:03:28 -07002701 return slab_alloc(s, flags, node, __builtin_return_address(0));
Christoph Lameter81819f02007-05-06 14:49:36 -07002702}
2703EXPORT_SYMBOL(__kmalloc_node);
2704#endif
2705
2706size_t ksize(const void *object)
2707{
Christoph Lameter272c1d22007-06-08 13:46:49 -07002708 struct page *page;
Christoph Lameter81819f02007-05-06 14:49:36 -07002709 struct kmem_cache *s;
2710
Christoph Lameteref8b4522007-10-16 01:24:46 -07002711 BUG_ON(!object);
2712 if (unlikely(object == ZERO_SIZE_PTR))
Christoph Lameter272c1d22007-06-08 13:46:49 -07002713 return 0;
2714
Vegard Nossum294a80a2007-12-04 23:45:30 -08002715 page = virt_to_head_page(object);
Christoph Lameter81819f02007-05-06 14:49:36 -07002716 BUG_ON(!page);
Vegard Nossum294a80a2007-12-04 23:45:30 -08002717
2718 if (unlikely(!PageSlab(page)))
2719 return PAGE_SIZE << compound_order(page);
2720
Christoph Lameter81819f02007-05-06 14:49:36 -07002721 s = page->slab;
2722 BUG_ON(!s);
2723
2724 /*
2725 * Debugging requires use of the padding between object
2726 * and whatever may come after it.
2727 */
2728 if (s->flags & (SLAB_RED_ZONE | SLAB_POISON))
2729 return s->objsize;
2730
2731 /*
2732 * If we have the need to store the freelist pointer
2733 * back there or track user information then we can
2734 * only use the space before that information.
2735 */
2736 if (s->flags & (SLAB_DESTROY_BY_RCU | SLAB_STORE_USER))
2737 return s->inuse;
2738
2739 /*
2740 * Else we can use all the padding etc for the allocation
2741 */
2742 return s->size;
2743}
2744EXPORT_SYMBOL(ksize);
2745
2746void kfree(const void *x)
2747{
Christoph Lameter81819f02007-05-06 14:49:36 -07002748 struct page *page;
Christoph Lameter5bb983b2008-02-07 17:47:41 -08002749 void *object = (void *)x;
Christoph Lameter81819f02007-05-06 14:49:36 -07002750
Satyam Sharma2408c552007-10-16 01:24:44 -07002751 if (unlikely(ZERO_OR_NULL_PTR(x)))
Christoph Lameter81819f02007-05-06 14:49:36 -07002752 return;
2753
Christoph Lameterb49af682007-05-06 14:49:41 -07002754 page = virt_to_head_page(x);
Christoph Lameteraadb4bc2007-10-16 01:24:38 -07002755 if (unlikely(!PageSlab(page))) {
2756 put_page(page);
2757 return;
2758 }
Christoph Lameter5bb983b2008-02-07 17:47:41 -08002759 slab_free(page->slab, page, object, __builtin_return_address(0));
Christoph Lameter81819f02007-05-06 14:49:36 -07002760}
2761EXPORT_SYMBOL(kfree);
2762
Christoph Lameterf61396a2008-01-07 23:20:26 -08002763static unsigned long count_partial(struct kmem_cache_node *n)
2764{
2765 unsigned long flags;
2766 unsigned long x = 0;
2767 struct page *page;
2768
2769 spin_lock_irqsave(&n->list_lock, flags);
2770 list_for_each_entry(page, &n->partial, lru)
2771 x += page->inuse;
2772 spin_unlock_irqrestore(&n->list_lock, flags);
2773 return x;
2774}
2775
Christoph Lameter2086d262007-05-06 14:49:46 -07002776/*
Christoph Lameter672bba32007-05-09 02:32:39 -07002777 * kmem_cache_shrink removes empty slabs from the partial lists and sorts
2778 * the remaining slabs by the number of items in use. The slabs with the
2779 * most items in use come first. New allocations will then fill those up
2780 * and thus they can be removed from the partial lists.
2781 *
2782 * The slabs with the least items are placed last. This results in them
2783 * being allocated from last increasing the chance that the last objects
2784 * are freed in them.
Christoph Lameter2086d262007-05-06 14:49:46 -07002785 */
2786int kmem_cache_shrink(struct kmem_cache *s)
2787{
2788 int node;
2789 int i;
2790 struct kmem_cache_node *n;
2791 struct page *page;
2792 struct page *t;
2793 struct list_head *slabs_by_inuse =
2794 kmalloc(sizeof(struct list_head) * s->objects, GFP_KERNEL);
2795 unsigned long flags;
2796
2797 if (!slabs_by_inuse)
2798 return -ENOMEM;
2799
2800 flush_all(s);
Christoph Lameterf64dc582007-10-16 01:25:33 -07002801 for_each_node_state(node, N_NORMAL_MEMORY) {
Christoph Lameter2086d262007-05-06 14:49:46 -07002802 n = get_node(s, node);
2803
2804 if (!n->nr_partial)
2805 continue;
2806
2807 for (i = 0; i < s->objects; i++)
2808 INIT_LIST_HEAD(slabs_by_inuse + i);
2809
2810 spin_lock_irqsave(&n->list_lock, flags);
2811
2812 /*
Christoph Lameter672bba32007-05-09 02:32:39 -07002813 * Build lists indexed by the items in use in each slab.
Christoph Lameter2086d262007-05-06 14:49:46 -07002814 *
Christoph Lameter672bba32007-05-09 02:32:39 -07002815 * Note that concurrent frees may occur while we hold the
2816 * list_lock. page->inuse here is the upper limit.
Christoph Lameter2086d262007-05-06 14:49:46 -07002817 */
2818 list_for_each_entry_safe(page, t, &n->partial, lru) {
2819 if (!page->inuse && slab_trylock(page)) {
2820 /*
2821 * Must hold slab lock here because slab_free
2822 * may have freed the last object and be
2823 * waiting to release the slab.
2824 */
2825 list_del(&page->lru);
2826 n->nr_partial--;
2827 slab_unlock(page);
2828 discard_slab(s, page);
2829 } else {
Christoph Lameterfcda3d82007-07-30 13:06:46 -07002830 list_move(&page->lru,
2831 slabs_by_inuse + page->inuse);
Christoph Lameter2086d262007-05-06 14:49:46 -07002832 }
2833 }
2834
Christoph Lameter2086d262007-05-06 14:49:46 -07002835 /*
Christoph Lameter672bba32007-05-09 02:32:39 -07002836 * Rebuild the partial list with the slabs filled up most
2837 * first and the least used slabs at the end.
Christoph Lameter2086d262007-05-06 14:49:46 -07002838 */
2839 for (i = s->objects - 1; i >= 0; i--)
2840 list_splice(slabs_by_inuse + i, n->partial.prev);
2841
Christoph Lameter2086d262007-05-06 14:49:46 -07002842 spin_unlock_irqrestore(&n->list_lock, flags);
2843 }
2844
2845 kfree(slabs_by_inuse);
2846 return 0;
2847}
2848EXPORT_SYMBOL(kmem_cache_shrink);
2849
Yasunori Gotob9049e22007-10-21 16:41:37 -07002850#if defined(CONFIG_NUMA) && defined(CONFIG_MEMORY_HOTPLUG)
2851static int slab_mem_going_offline_callback(void *arg)
2852{
2853 struct kmem_cache *s;
2854
2855 down_read(&slub_lock);
2856 list_for_each_entry(s, &slab_caches, list)
2857 kmem_cache_shrink(s);
2858 up_read(&slub_lock);
2859
2860 return 0;
2861}
2862
2863static void slab_mem_offline_callback(void *arg)
2864{
2865 struct kmem_cache_node *n;
2866 struct kmem_cache *s;
2867 struct memory_notify *marg = arg;
2868 int offline_node;
2869
2870 offline_node = marg->status_change_nid;
2871
2872 /*
2873 * If the node still has available memory. we need kmem_cache_node
2874 * for it yet.
2875 */
2876 if (offline_node < 0)
2877 return;
2878
2879 down_read(&slub_lock);
2880 list_for_each_entry(s, &slab_caches, list) {
2881 n = get_node(s, offline_node);
2882 if (n) {
2883 /*
2884 * if n->nr_slabs > 0, slabs still exist on the node
2885 * that is going down. We were unable to free them,
2886 * and offline_pages() function shoudn't call this
2887 * callback. So, we must fail.
2888 */
Al Viro27bb6282007-10-29 04:42:55 +00002889 BUG_ON(atomic_long_read(&n->nr_slabs));
Yasunori Gotob9049e22007-10-21 16:41:37 -07002890
2891 s->node[offline_node] = NULL;
2892 kmem_cache_free(kmalloc_caches, n);
2893 }
2894 }
2895 up_read(&slub_lock);
2896}
2897
2898static int slab_mem_going_online_callback(void *arg)
2899{
2900 struct kmem_cache_node *n;
2901 struct kmem_cache *s;
2902 struct memory_notify *marg = arg;
2903 int nid = marg->status_change_nid;
2904 int ret = 0;
2905
2906 /*
2907 * If the node's memory is already available, then kmem_cache_node is
2908 * already created. Nothing to do.
2909 */
2910 if (nid < 0)
2911 return 0;
2912
2913 /*
2914 * We are bringing a node online. No memory is availabe yet. We must
2915 * allocate a kmem_cache_node structure in order to bring the node
2916 * online.
2917 */
2918 down_read(&slub_lock);
2919 list_for_each_entry(s, &slab_caches, list) {
2920 /*
2921 * XXX: kmem_cache_alloc_node will fallback to other nodes
2922 * since memory is not yet available from the node that
2923 * is brought up.
2924 */
2925 n = kmem_cache_alloc(kmalloc_caches, GFP_KERNEL);
2926 if (!n) {
2927 ret = -ENOMEM;
2928 goto out;
2929 }
2930 init_kmem_cache_node(n);
2931 s->node[nid] = n;
2932 }
2933out:
2934 up_read(&slub_lock);
2935 return ret;
2936}
2937
2938static int slab_memory_callback(struct notifier_block *self,
2939 unsigned long action, void *arg)
2940{
2941 int ret = 0;
2942
2943 switch (action) {
2944 case MEM_GOING_ONLINE:
2945 ret = slab_mem_going_online_callback(arg);
2946 break;
2947 case MEM_GOING_OFFLINE:
2948 ret = slab_mem_going_offline_callback(arg);
2949 break;
2950 case MEM_OFFLINE:
2951 case MEM_CANCEL_ONLINE:
2952 slab_mem_offline_callback(arg);
2953 break;
2954 case MEM_ONLINE:
2955 case MEM_CANCEL_OFFLINE:
2956 break;
2957 }
2958
2959 ret = notifier_from_errno(ret);
2960 return ret;
2961}
2962
2963#endif /* CONFIG_MEMORY_HOTPLUG */
2964
Christoph Lameter81819f02007-05-06 14:49:36 -07002965/********************************************************************
2966 * Basic setup of slabs
2967 *******************************************************************/
2968
2969void __init kmem_cache_init(void)
2970{
2971 int i;
Christoph Lameter4b356be2007-06-16 10:16:13 -07002972 int caches = 0;
Christoph Lameter81819f02007-05-06 14:49:36 -07002973
Christoph Lameter4c93c3552007-10-16 01:26:08 -07002974 init_alloc_cpu();
2975
Christoph Lameter81819f02007-05-06 14:49:36 -07002976#ifdef CONFIG_NUMA
2977 /*
2978 * Must first have the slab cache available for the allocations of the
Christoph Lameter672bba32007-05-09 02:32:39 -07002979 * struct kmem_cache_node's. There is special bootstrap code in
Christoph Lameter81819f02007-05-06 14:49:36 -07002980 * kmem_cache_open for slab_state == DOWN.
2981 */
2982 create_kmalloc_cache(&kmalloc_caches[0], "kmem_cache_node",
2983 sizeof(struct kmem_cache_node), GFP_KERNEL);
Christoph Lameter8ffa6872007-05-31 00:40:51 -07002984 kmalloc_caches[0].refcount = -1;
Christoph Lameter4b356be2007-06-16 10:16:13 -07002985 caches++;
Yasunori Gotob9049e22007-10-21 16:41:37 -07002986
2987 hotplug_memory_notifier(slab_memory_callback, 1);
Christoph Lameter81819f02007-05-06 14:49:36 -07002988#endif
2989
2990 /* Able to allocate the per node structures */
2991 slab_state = PARTIAL;
2992
2993 /* Caches that are not of the two-to-the-power-of size */
Christoph Lameter4b356be2007-06-16 10:16:13 -07002994 if (KMALLOC_MIN_SIZE <= 64) {
2995 create_kmalloc_cache(&kmalloc_caches[1],
Christoph Lameter81819f02007-05-06 14:49:36 -07002996 "kmalloc-96", 96, GFP_KERNEL);
Christoph Lameter4b356be2007-06-16 10:16:13 -07002997 caches++;
2998 }
2999 if (KMALLOC_MIN_SIZE <= 128) {
3000 create_kmalloc_cache(&kmalloc_caches[2],
Christoph Lameter81819f02007-05-06 14:49:36 -07003001 "kmalloc-192", 192, GFP_KERNEL);
Christoph Lameter4b356be2007-06-16 10:16:13 -07003002 caches++;
3003 }
Christoph Lameter81819f02007-05-06 14:49:36 -07003004
Christoph Lameteraadb4bc2007-10-16 01:24:38 -07003005 for (i = KMALLOC_SHIFT_LOW; i < PAGE_SHIFT; i++) {
Christoph Lameter81819f02007-05-06 14:49:36 -07003006 create_kmalloc_cache(&kmalloc_caches[i],
3007 "kmalloc", 1 << i, GFP_KERNEL);
Christoph Lameter4b356be2007-06-16 10:16:13 -07003008 caches++;
3009 }
Christoph Lameter81819f02007-05-06 14:49:36 -07003010
Christoph Lameterf1b26332007-07-17 04:03:26 -07003011
3012 /*
3013 * Patch up the size_index table if we have strange large alignment
3014 * requirements for the kmalloc array. This is only the case for
3015 * mips it seems. The standard arches will not generate any code here.
3016 *
3017 * Largest permitted alignment is 256 bytes due to the way we
3018 * handle the index determination for the smaller caches.
3019 *
3020 * Make sure that nothing crazy happens if someone starts tinkering
3021 * around with ARCH_KMALLOC_MINALIGN
3022 */
3023 BUILD_BUG_ON(KMALLOC_MIN_SIZE > 256 ||
3024 (KMALLOC_MIN_SIZE & (KMALLOC_MIN_SIZE - 1)));
3025
Christoph Lameter12ad6842007-07-17 04:03:28 -07003026 for (i = 8; i < KMALLOC_MIN_SIZE; i += 8)
Christoph Lameterf1b26332007-07-17 04:03:26 -07003027 size_index[(i - 1) / 8] = KMALLOC_SHIFT_LOW;
3028
Christoph Lameter81819f02007-05-06 14:49:36 -07003029 slab_state = UP;
3030
3031 /* Provide the correct kmalloc names now that the caches are up */
Christoph Lameteraadb4bc2007-10-16 01:24:38 -07003032 for (i = KMALLOC_SHIFT_LOW; i < PAGE_SHIFT; i++)
Christoph Lameter81819f02007-05-06 14:49:36 -07003033 kmalloc_caches[i]. name =
3034 kasprintf(GFP_KERNEL, "kmalloc-%d", 1 << i);
3035
3036#ifdef CONFIG_SMP
3037 register_cpu_notifier(&slab_notifier);
Christoph Lameter4c93c3552007-10-16 01:26:08 -07003038 kmem_size = offsetof(struct kmem_cache, cpu_slab) +
3039 nr_cpu_ids * sizeof(struct kmem_cache_cpu *);
3040#else
3041 kmem_size = sizeof(struct kmem_cache);
Christoph Lameter81819f02007-05-06 14:49:36 -07003042#endif
3043
Christoph Lameter81819f02007-05-06 14:49:36 -07003044
Ingo Molnar3adbefe2008-02-05 17:57:39 -08003045 printk(KERN_INFO
3046 "SLUB: Genslabs=%d, HWalign=%d, Order=%d-%d, MinObjects=%d,"
Christoph Lameter4b356be2007-06-16 10:16:13 -07003047 " CPUs=%d, Nodes=%d\n",
3048 caches, cache_line_size(),
Christoph Lameter81819f02007-05-06 14:49:36 -07003049 slub_min_order, slub_max_order, slub_min_objects,
3050 nr_cpu_ids, nr_node_ids);
3051}
3052
3053/*
3054 * Find a mergeable slab cache
3055 */
3056static int slab_unmergeable(struct kmem_cache *s)
3057{
3058 if (slub_nomerge || (s->flags & SLUB_NEVER_MERGE))
3059 return 1;
3060
Christoph Lameterc59def9f2007-05-16 22:10:50 -07003061 if (s->ctor)
Christoph Lameter81819f02007-05-06 14:49:36 -07003062 return 1;
3063
Christoph Lameter8ffa6872007-05-31 00:40:51 -07003064 /*
3065 * We may have set a slab to be unmergeable during bootstrap.
3066 */
3067 if (s->refcount < 0)
3068 return 1;
3069
Christoph Lameter81819f02007-05-06 14:49:36 -07003070 return 0;
3071}
3072
3073static struct kmem_cache *find_mergeable(size_t size,
Christoph Lameterba0268a2007-09-11 15:24:11 -07003074 size_t align, unsigned long flags, const char *name,
Christoph Lameter4ba9b9d2007-10-16 23:25:51 -07003075 void (*ctor)(struct kmem_cache *, void *))
Christoph Lameter81819f02007-05-06 14:49:36 -07003076{
Christoph Lameter5b95a4a2007-07-17 04:03:19 -07003077 struct kmem_cache *s;
Christoph Lameter81819f02007-05-06 14:49:36 -07003078
3079 if (slub_nomerge || (flags & SLUB_NEVER_MERGE))
3080 return NULL;
3081
Christoph Lameterc59def9f2007-05-16 22:10:50 -07003082 if (ctor)
Christoph Lameter81819f02007-05-06 14:49:36 -07003083 return NULL;
3084
3085 size = ALIGN(size, sizeof(void *));
3086 align = calculate_alignment(flags, align, size);
3087 size = ALIGN(size, align);
Christoph Lameterba0268a2007-09-11 15:24:11 -07003088 flags = kmem_cache_flags(size, flags, name, NULL);
Christoph Lameter81819f02007-05-06 14:49:36 -07003089
Christoph Lameter5b95a4a2007-07-17 04:03:19 -07003090 list_for_each_entry(s, &slab_caches, list) {
Christoph Lameter81819f02007-05-06 14:49:36 -07003091 if (slab_unmergeable(s))
3092 continue;
3093
3094 if (size > s->size)
3095 continue;
3096
Christoph Lameterba0268a2007-09-11 15:24:11 -07003097 if ((flags & SLUB_MERGE_SAME) != (s->flags & SLUB_MERGE_SAME))
Christoph Lameter81819f02007-05-06 14:49:36 -07003098 continue;
3099 /*
3100 * Check if alignment is compatible.
3101 * Courtesy of Adrian Drzewiecki
3102 */
Pekka Enberg06428782008-01-07 23:20:27 -08003103 if ((s->size & ~(align - 1)) != s->size)
Christoph Lameter81819f02007-05-06 14:49:36 -07003104 continue;
3105
3106 if (s->size - size >= sizeof(void *))
3107 continue;
3108
3109 return s;
3110 }
3111 return NULL;
3112}
3113
3114struct kmem_cache *kmem_cache_create(const char *name, size_t size,
3115 size_t align, unsigned long flags,
Christoph Lameter4ba9b9d2007-10-16 23:25:51 -07003116 void (*ctor)(struct kmem_cache *, void *))
Christoph Lameter81819f02007-05-06 14:49:36 -07003117{
3118 struct kmem_cache *s;
3119
3120 down_write(&slub_lock);
Christoph Lameterba0268a2007-09-11 15:24:11 -07003121 s = find_mergeable(size, align, flags, name, ctor);
Christoph Lameter81819f02007-05-06 14:49:36 -07003122 if (s) {
Christoph Lameter42a9fdb2007-10-16 01:26:09 -07003123 int cpu;
3124
Christoph Lameter81819f02007-05-06 14:49:36 -07003125 s->refcount++;
3126 /*
3127 * Adjust the object sizes so that we clear
3128 * the complete object on kzalloc.
3129 */
3130 s->objsize = max(s->objsize, (int)size);
Christoph Lameter42a9fdb2007-10-16 01:26:09 -07003131
3132 /*
3133 * And then we need to update the object size in the
3134 * per cpu structures
3135 */
3136 for_each_online_cpu(cpu)
3137 get_cpu_slab(s, cpu)->objsize = s->objsize;
Christoph Lameter81819f02007-05-06 14:49:36 -07003138 s->inuse = max_t(int, s->inuse, ALIGN(size, sizeof(void *)));
Christoph Lametera0e1d1b2007-07-17 04:03:31 -07003139 up_write(&slub_lock);
Christoph Lameter81819f02007-05-06 14:49:36 -07003140 if (sysfs_slab_alias(s, name))
3141 goto err;
Christoph Lametera0e1d1b2007-07-17 04:03:31 -07003142 return s;
3143 }
3144 s = kmalloc(kmem_size, GFP_KERNEL);
3145 if (s) {
3146 if (kmem_cache_open(s, GFP_KERNEL, name,
Christoph Lameterc59def9f2007-05-16 22:10:50 -07003147 size, align, flags, ctor)) {
Christoph Lameter81819f02007-05-06 14:49:36 -07003148 list_add(&s->list, &slab_caches);
Christoph Lametera0e1d1b2007-07-17 04:03:31 -07003149 up_write(&slub_lock);
3150 if (sysfs_slab_add(s))
3151 goto err;
3152 return s;
3153 }
3154 kfree(s);
Christoph Lameter81819f02007-05-06 14:49:36 -07003155 }
3156 up_write(&slub_lock);
Christoph Lameter81819f02007-05-06 14:49:36 -07003157
3158err:
Christoph Lameter81819f02007-05-06 14:49:36 -07003159 if (flags & SLAB_PANIC)
3160 panic("Cannot create slabcache %s\n", name);
3161 else
3162 s = NULL;
3163 return s;
3164}
3165EXPORT_SYMBOL(kmem_cache_create);
3166
Christoph Lameter81819f02007-05-06 14:49:36 -07003167#ifdef CONFIG_SMP
Christoph Lameter27390bc2007-06-01 00:47:09 -07003168/*
Christoph Lameter672bba32007-05-09 02:32:39 -07003169 * Use the cpu notifier to insure that the cpu slabs are flushed when
3170 * necessary.
Christoph Lameter81819f02007-05-06 14:49:36 -07003171 */
3172static int __cpuinit slab_cpuup_callback(struct notifier_block *nfb,
3173 unsigned long action, void *hcpu)
3174{
3175 long cpu = (long)hcpu;
Christoph Lameter5b95a4a2007-07-17 04:03:19 -07003176 struct kmem_cache *s;
3177 unsigned long flags;
Christoph Lameter81819f02007-05-06 14:49:36 -07003178
3179 switch (action) {
Christoph Lameter4c93c3552007-10-16 01:26:08 -07003180 case CPU_UP_PREPARE:
3181 case CPU_UP_PREPARE_FROZEN:
3182 init_alloc_cpu_cpu(cpu);
3183 down_read(&slub_lock);
3184 list_for_each_entry(s, &slab_caches, list)
3185 s->cpu_slab[cpu] = alloc_kmem_cache_cpu(s, cpu,
3186 GFP_KERNEL);
3187 up_read(&slub_lock);
3188 break;
3189
Christoph Lameter81819f02007-05-06 14:49:36 -07003190 case CPU_UP_CANCELED:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07003191 case CPU_UP_CANCELED_FROZEN:
Christoph Lameter81819f02007-05-06 14:49:36 -07003192 case CPU_DEAD:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07003193 case CPU_DEAD_FROZEN:
Christoph Lameter5b95a4a2007-07-17 04:03:19 -07003194 down_read(&slub_lock);
3195 list_for_each_entry(s, &slab_caches, list) {
Christoph Lameter4c93c3552007-10-16 01:26:08 -07003196 struct kmem_cache_cpu *c = get_cpu_slab(s, cpu);
3197
Christoph Lameter5b95a4a2007-07-17 04:03:19 -07003198 local_irq_save(flags);
3199 __flush_cpu_slab(s, cpu);
3200 local_irq_restore(flags);
Christoph Lameter4c93c3552007-10-16 01:26:08 -07003201 free_kmem_cache_cpu(c, cpu);
3202 s->cpu_slab[cpu] = NULL;
Christoph Lameter5b95a4a2007-07-17 04:03:19 -07003203 }
3204 up_read(&slub_lock);
Christoph Lameter81819f02007-05-06 14:49:36 -07003205 break;
3206 default:
3207 break;
3208 }
3209 return NOTIFY_OK;
3210}
3211
Pekka Enberg06428782008-01-07 23:20:27 -08003212static struct notifier_block __cpuinitdata slab_notifier = {
Ingo Molnar3adbefe2008-02-05 17:57:39 -08003213 .notifier_call = slab_cpuup_callback
Pekka Enberg06428782008-01-07 23:20:27 -08003214};
Christoph Lameter81819f02007-05-06 14:49:36 -07003215
3216#endif
3217
Christoph Lameter81819f02007-05-06 14:49:36 -07003218void *__kmalloc_track_caller(size_t size, gfp_t gfpflags, void *caller)
3219{
Christoph Lameteraadb4bc2007-10-16 01:24:38 -07003220 struct kmem_cache *s;
3221
3222 if (unlikely(size > PAGE_SIZE / 2))
Pekka Enbergeada35e2008-02-11 22:47:46 +02003223 return kmalloc_large(size, gfpflags);
3224
Christoph Lameteraadb4bc2007-10-16 01:24:38 -07003225 s = get_slab(size, gfpflags);
Christoph Lameter81819f02007-05-06 14:49:36 -07003226
Satyam Sharma2408c552007-10-16 01:24:44 -07003227 if (unlikely(ZERO_OR_NULL_PTR(s)))
Christoph Lameter6cb8f912007-07-17 04:03:22 -07003228 return s;
Christoph Lameter81819f02007-05-06 14:49:36 -07003229
Christoph Lameterce15fea2007-07-17 04:03:28 -07003230 return slab_alloc(s, gfpflags, -1, caller);
Christoph Lameter81819f02007-05-06 14:49:36 -07003231}
3232
3233void *__kmalloc_node_track_caller(size_t size, gfp_t gfpflags,
3234 int node, void *caller)
3235{
Christoph Lameteraadb4bc2007-10-16 01:24:38 -07003236 struct kmem_cache *s;
3237
3238 if (unlikely(size > PAGE_SIZE / 2))
Pekka Enbergeada35e2008-02-11 22:47:46 +02003239 return kmalloc_large(size, gfpflags);
3240
Christoph Lameteraadb4bc2007-10-16 01:24:38 -07003241 s = get_slab(size, gfpflags);
Christoph Lameter81819f02007-05-06 14:49:36 -07003242
Satyam Sharma2408c552007-10-16 01:24:44 -07003243 if (unlikely(ZERO_OR_NULL_PTR(s)))
Christoph Lameter6cb8f912007-07-17 04:03:22 -07003244 return s;
Christoph Lameter81819f02007-05-06 14:49:36 -07003245
Christoph Lameterce15fea2007-07-17 04:03:28 -07003246 return slab_alloc(s, gfpflags, node, caller);
Christoph Lameter81819f02007-05-06 14:49:36 -07003247}
3248
Christoph Lameter41ecc552007-05-09 02:32:44 -07003249#if defined(CONFIG_SYSFS) && defined(CONFIG_SLUB_DEBUG)
Christoph Lameter434e2452007-07-17 04:03:30 -07003250static int validate_slab(struct kmem_cache *s, struct page *page,
3251 unsigned long *map)
Christoph Lameter53e15af2007-05-06 14:49:43 -07003252{
3253 void *p;
Christoph Lameter683d0ba2008-01-07 23:20:29 -08003254 void *addr = slab_address(page);
Christoph Lameter53e15af2007-05-06 14:49:43 -07003255
3256 if (!check_slab(s, page) ||
3257 !on_freelist(s, page, NULL))
3258 return 0;
3259
3260 /* Now we know that a valid freelist exists */
3261 bitmap_zero(map, s->objects);
3262
Christoph Lameter7656c722007-05-09 02:32:40 -07003263 for_each_free_object(p, s, page->freelist) {
3264 set_bit(slab_index(p, s, addr), map);
Christoph Lameter53e15af2007-05-06 14:49:43 -07003265 if (!check_object(s, page, p, 0))
3266 return 0;
3267 }
3268
Christoph Lameter7656c722007-05-09 02:32:40 -07003269 for_each_object(p, s, addr)
3270 if (!test_bit(slab_index(p, s, addr), map))
Christoph Lameter53e15af2007-05-06 14:49:43 -07003271 if (!check_object(s, page, p, 1))
3272 return 0;
3273 return 1;
3274}
3275
Christoph Lameter434e2452007-07-17 04:03:30 -07003276static void validate_slab_slab(struct kmem_cache *s, struct page *page,
3277 unsigned long *map)
Christoph Lameter53e15af2007-05-06 14:49:43 -07003278{
3279 if (slab_trylock(page)) {
Christoph Lameter434e2452007-07-17 04:03:30 -07003280 validate_slab(s, page, map);
Christoph Lameter53e15af2007-05-06 14:49:43 -07003281 slab_unlock(page);
3282 } else
3283 printk(KERN_INFO "SLUB %s: Skipped busy slab 0x%p\n",
3284 s->name, page);
3285
3286 if (s->flags & DEBUG_DEFAULT_FLAGS) {
Christoph Lameter35e5d7e2007-05-09 02:32:42 -07003287 if (!SlabDebug(page))
3288 printk(KERN_ERR "SLUB %s: SlabDebug not set "
Christoph Lameter53e15af2007-05-06 14:49:43 -07003289 "on slab 0x%p\n", s->name, page);
3290 } else {
Christoph Lameter35e5d7e2007-05-09 02:32:42 -07003291 if (SlabDebug(page))
3292 printk(KERN_ERR "SLUB %s: SlabDebug set on "
Christoph Lameter53e15af2007-05-06 14:49:43 -07003293 "slab 0x%p\n", s->name, page);
3294 }
3295}
3296
Christoph Lameter434e2452007-07-17 04:03:30 -07003297static int validate_slab_node(struct kmem_cache *s,
3298 struct kmem_cache_node *n, unsigned long *map)
Christoph Lameter53e15af2007-05-06 14:49:43 -07003299{
3300 unsigned long count = 0;
3301 struct page *page;
3302 unsigned long flags;
3303
3304 spin_lock_irqsave(&n->list_lock, flags);
3305
3306 list_for_each_entry(page, &n->partial, lru) {
Christoph Lameter434e2452007-07-17 04:03:30 -07003307 validate_slab_slab(s, page, map);
Christoph Lameter53e15af2007-05-06 14:49:43 -07003308 count++;
3309 }
3310 if (count != n->nr_partial)
3311 printk(KERN_ERR "SLUB %s: %ld partial slabs counted but "
3312 "counter=%ld\n", s->name, count, n->nr_partial);
3313
3314 if (!(s->flags & SLAB_STORE_USER))
3315 goto out;
3316
3317 list_for_each_entry(page, &n->full, lru) {
Christoph Lameter434e2452007-07-17 04:03:30 -07003318 validate_slab_slab(s, page, map);
Christoph Lameter53e15af2007-05-06 14:49:43 -07003319 count++;
3320 }
3321 if (count != atomic_long_read(&n->nr_slabs))
3322 printk(KERN_ERR "SLUB: %s %ld slabs counted but "
3323 "counter=%ld\n", s->name, count,
3324 atomic_long_read(&n->nr_slabs));
3325
3326out:
3327 spin_unlock_irqrestore(&n->list_lock, flags);
3328 return count;
3329}
3330
Christoph Lameter434e2452007-07-17 04:03:30 -07003331static long validate_slab_cache(struct kmem_cache *s)
Christoph Lameter53e15af2007-05-06 14:49:43 -07003332{
3333 int node;
3334 unsigned long count = 0;
Christoph Lameter434e2452007-07-17 04:03:30 -07003335 unsigned long *map = kmalloc(BITS_TO_LONGS(s->objects) *
3336 sizeof(unsigned long), GFP_KERNEL);
3337
3338 if (!map)
3339 return -ENOMEM;
Christoph Lameter53e15af2007-05-06 14:49:43 -07003340
3341 flush_all(s);
Christoph Lameterf64dc582007-10-16 01:25:33 -07003342 for_each_node_state(node, N_NORMAL_MEMORY) {
Christoph Lameter53e15af2007-05-06 14:49:43 -07003343 struct kmem_cache_node *n = get_node(s, node);
3344
Christoph Lameter434e2452007-07-17 04:03:30 -07003345 count += validate_slab_node(s, n, map);
Christoph Lameter53e15af2007-05-06 14:49:43 -07003346 }
Christoph Lameter434e2452007-07-17 04:03:30 -07003347 kfree(map);
Christoph Lameter53e15af2007-05-06 14:49:43 -07003348 return count;
3349}
3350
Christoph Lameterb3459702007-05-09 02:32:41 -07003351#ifdef SLUB_RESILIENCY_TEST
3352static void resiliency_test(void)
3353{
3354 u8 *p;
3355
3356 printk(KERN_ERR "SLUB resiliency testing\n");
3357 printk(KERN_ERR "-----------------------\n");
3358 printk(KERN_ERR "A. Corruption after allocation\n");
3359
3360 p = kzalloc(16, GFP_KERNEL);
3361 p[16] = 0x12;
3362 printk(KERN_ERR "\n1. kmalloc-16: Clobber Redzone/next pointer"
3363 " 0x12->0x%p\n\n", p + 16);
3364
3365 validate_slab_cache(kmalloc_caches + 4);
3366
3367 /* Hmmm... The next two are dangerous */
3368 p = kzalloc(32, GFP_KERNEL);
3369 p[32 + sizeof(void *)] = 0x34;
3370 printk(KERN_ERR "\n2. kmalloc-32: Clobber next pointer/next slab"
Ingo Molnar3adbefe2008-02-05 17:57:39 -08003371 " 0x34 -> -0x%p\n", p);
3372 printk(KERN_ERR
3373 "If allocated object is overwritten then not detectable\n\n");
Christoph Lameterb3459702007-05-09 02:32:41 -07003374
3375 validate_slab_cache(kmalloc_caches + 5);
3376 p = kzalloc(64, GFP_KERNEL);
3377 p += 64 + (get_cycles() & 0xff) * sizeof(void *);
3378 *p = 0x56;
3379 printk(KERN_ERR "\n3. kmalloc-64: corrupting random byte 0x56->0x%p\n",
3380 p);
Ingo Molnar3adbefe2008-02-05 17:57:39 -08003381 printk(KERN_ERR
3382 "If allocated object is overwritten then not detectable\n\n");
Christoph Lameterb3459702007-05-09 02:32:41 -07003383 validate_slab_cache(kmalloc_caches + 6);
3384
3385 printk(KERN_ERR "\nB. Corruption after free\n");
3386 p = kzalloc(128, GFP_KERNEL);
3387 kfree(p);
3388 *p = 0x78;
3389 printk(KERN_ERR "1. kmalloc-128: Clobber first word 0x78->0x%p\n\n", p);
3390 validate_slab_cache(kmalloc_caches + 7);
3391
3392 p = kzalloc(256, GFP_KERNEL);
3393 kfree(p);
3394 p[50] = 0x9a;
Ingo Molnar3adbefe2008-02-05 17:57:39 -08003395 printk(KERN_ERR "\n2. kmalloc-256: Clobber 50th byte 0x9a->0x%p\n\n",
3396 p);
Christoph Lameterb3459702007-05-09 02:32:41 -07003397 validate_slab_cache(kmalloc_caches + 8);
3398
3399 p = kzalloc(512, GFP_KERNEL);
3400 kfree(p);
3401 p[512] = 0xab;
3402 printk(KERN_ERR "\n3. kmalloc-512: Clobber redzone 0xab->0x%p\n\n", p);
3403 validate_slab_cache(kmalloc_caches + 9);
3404}
3405#else
3406static void resiliency_test(void) {};
3407#endif
3408
Christoph Lameter88a420e2007-05-06 14:49:45 -07003409/*
Christoph Lameter672bba32007-05-09 02:32:39 -07003410 * Generate lists of code addresses where slabcache objects are allocated
Christoph Lameter88a420e2007-05-06 14:49:45 -07003411 * and freed.
3412 */
3413
3414struct location {
3415 unsigned long count;
3416 void *addr;
Christoph Lameter45edfa52007-05-09 02:32:45 -07003417 long long sum_time;
3418 long min_time;
3419 long max_time;
3420 long min_pid;
3421 long max_pid;
3422 cpumask_t cpus;
3423 nodemask_t nodes;
Christoph Lameter88a420e2007-05-06 14:49:45 -07003424};
3425
3426struct loc_track {
3427 unsigned long max;
3428 unsigned long count;
3429 struct location *loc;
3430};
3431
3432static void free_loc_track(struct loc_track *t)
3433{
3434 if (t->max)
3435 free_pages((unsigned long)t->loc,
3436 get_order(sizeof(struct location) * t->max));
3437}
3438
Christoph Lameter68dff6a2007-07-17 04:03:20 -07003439static int alloc_loc_track(struct loc_track *t, unsigned long max, gfp_t flags)
Christoph Lameter88a420e2007-05-06 14:49:45 -07003440{
3441 struct location *l;
3442 int order;
3443
Christoph Lameter88a420e2007-05-06 14:49:45 -07003444 order = get_order(sizeof(struct location) * max);
3445
Christoph Lameter68dff6a2007-07-17 04:03:20 -07003446 l = (void *)__get_free_pages(flags, order);
Christoph Lameter88a420e2007-05-06 14:49:45 -07003447 if (!l)
3448 return 0;
3449
3450 if (t->count) {
3451 memcpy(l, t->loc, sizeof(struct location) * t->count);
3452 free_loc_track(t);
3453 }
3454 t->max = max;
3455 t->loc = l;
3456 return 1;
3457}
3458
3459static int add_location(struct loc_track *t, struct kmem_cache *s,
Christoph Lameter45edfa52007-05-09 02:32:45 -07003460 const struct track *track)
Christoph Lameter88a420e2007-05-06 14:49:45 -07003461{
3462 long start, end, pos;
3463 struct location *l;
3464 void *caddr;
Christoph Lameter45edfa52007-05-09 02:32:45 -07003465 unsigned long age = jiffies - track->when;
Christoph Lameter88a420e2007-05-06 14:49:45 -07003466
3467 start = -1;
3468 end = t->count;
3469
3470 for ( ; ; ) {
3471 pos = start + (end - start + 1) / 2;
3472
3473 /*
3474 * There is nothing at "end". If we end up there
3475 * we need to add something to before end.
3476 */
3477 if (pos == end)
3478 break;
3479
3480 caddr = t->loc[pos].addr;
Christoph Lameter45edfa52007-05-09 02:32:45 -07003481 if (track->addr == caddr) {
3482
3483 l = &t->loc[pos];
3484 l->count++;
3485 if (track->when) {
3486 l->sum_time += age;
3487 if (age < l->min_time)
3488 l->min_time = age;
3489 if (age > l->max_time)
3490 l->max_time = age;
3491
3492 if (track->pid < l->min_pid)
3493 l->min_pid = track->pid;
3494 if (track->pid > l->max_pid)
3495 l->max_pid = track->pid;
3496
3497 cpu_set(track->cpu, l->cpus);
3498 }
3499 node_set(page_to_nid(virt_to_page(track)), l->nodes);
Christoph Lameter88a420e2007-05-06 14:49:45 -07003500 return 1;
3501 }
3502
Christoph Lameter45edfa52007-05-09 02:32:45 -07003503 if (track->addr < caddr)
Christoph Lameter88a420e2007-05-06 14:49:45 -07003504 end = pos;
3505 else
3506 start = pos;
3507 }
3508
3509 /*
Christoph Lameter672bba32007-05-09 02:32:39 -07003510 * Not found. Insert new tracking element.
Christoph Lameter88a420e2007-05-06 14:49:45 -07003511 */
Christoph Lameter68dff6a2007-07-17 04:03:20 -07003512 if (t->count >= t->max && !alloc_loc_track(t, 2 * t->max, GFP_ATOMIC))
Christoph Lameter88a420e2007-05-06 14:49:45 -07003513 return 0;
3514
3515 l = t->loc + pos;
3516 if (pos < t->count)
3517 memmove(l + 1, l,
3518 (t->count - pos) * sizeof(struct location));
3519 t->count++;
3520 l->count = 1;
Christoph Lameter45edfa52007-05-09 02:32:45 -07003521 l->addr = track->addr;
3522 l->sum_time = age;
3523 l->min_time = age;
3524 l->max_time = age;
3525 l->min_pid = track->pid;
3526 l->max_pid = track->pid;
3527 cpus_clear(l->cpus);
3528 cpu_set(track->cpu, l->cpus);
3529 nodes_clear(l->nodes);
3530 node_set(page_to_nid(virt_to_page(track)), l->nodes);
Christoph Lameter88a420e2007-05-06 14:49:45 -07003531 return 1;
3532}
3533
3534static void process_slab(struct loc_track *t, struct kmem_cache *s,
3535 struct page *page, enum track_item alloc)
3536{
Christoph Lameter683d0ba2008-01-07 23:20:29 -08003537 void *addr = slab_address(page);
Christoph Lameter7656c722007-05-09 02:32:40 -07003538 DECLARE_BITMAP(map, s->objects);
Christoph Lameter88a420e2007-05-06 14:49:45 -07003539 void *p;
3540
3541 bitmap_zero(map, s->objects);
Christoph Lameter7656c722007-05-09 02:32:40 -07003542 for_each_free_object(p, s, page->freelist)
3543 set_bit(slab_index(p, s, addr), map);
Christoph Lameter88a420e2007-05-06 14:49:45 -07003544
Christoph Lameter7656c722007-05-09 02:32:40 -07003545 for_each_object(p, s, addr)
Christoph Lameter45edfa52007-05-09 02:32:45 -07003546 if (!test_bit(slab_index(p, s, addr), map))
3547 add_location(t, s, get_track(s, p, alloc));
Christoph Lameter88a420e2007-05-06 14:49:45 -07003548}
3549
3550static int list_locations(struct kmem_cache *s, char *buf,
3551 enum track_item alloc)
3552{
Harvey Harrisone374d482008-01-31 15:20:50 -08003553 int len = 0;
Christoph Lameter88a420e2007-05-06 14:49:45 -07003554 unsigned long i;
Christoph Lameter68dff6a2007-07-17 04:03:20 -07003555 struct loc_track t = { 0, 0, NULL };
Christoph Lameter88a420e2007-05-06 14:49:45 -07003556 int node;
3557
Christoph Lameter68dff6a2007-07-17 04:03:20 -07003558 if (!alloc_loc_track(&t, PAGE_SIZE / sizeof(struct location),
Andrew Mortonea3061d2007-10-16 01:26:09 -07003559 GFP_TEMPORARY))
Christoph Lameter68dff6a2007-07-17 04:03:20 -07003560 return sprintf(buf, "Out of memory\n");
Christoph Lameter88a420e2007-05-06 14:49:45 -07003561
3562 /* Push back cpu slabs */
3563 flush_all(s);
3564
Christoph Lameterf64dc582007-10-16 01:25:33 -07003565 for_each_node_state(node, N_NORMAL_MEMORY) {
Christoph Lameter88a420e2007-05-06 14:49:45 -07003566 struct kmem_cache_node *n = get_node(s, node);
3567 unsigned long flags;
3568 struct page *page;
3569
Christoph Lameter9e869432007-08-22 14:01:56 -07003570 if (!atomic_long_read(&n->nr_slabs))
Christoph Lameter88a420e2007-05-06 14:49:45 -07003571 continue;
3572
3573 spin_lock_irqsave(&n->list_lock, flags);
3574 list_for_each_entry(page, &n->partial, lru)
3575 process_slab(&t, s, page, alloc);
3576 list_for_each_entry(page, &n->full, lru)
3577 process_slab(&t, s, page, alloc);
3578 spin_unlock_irqrestore(&n->list_lock, flags);
3579 }
3580
3581 for (i = 0; i < t.count; i++) {
Christoph Lameter45edfa52007-05-09 02:32:45 -07003582 struct location *l = &t.loc[i];
Christoph Lameter88a420e2007-05-06 14:49:45 -07003583
Harvey Harrisone374d482008-01-31 15:20:50 -08003584 if (len > PAGE_SIZE - 100)
Christoph Lameter88a420e2007-05-06 14:49:45 -07003585 break;
Harvey Harrisone374d482008-01-31 15:20:50 -08003586 len += sprintf(buf + len, "%7ld ", l->count);
Christoph Lameter45edfa52007-05-09 02:32:45 -07003587
3588 if (l->addr)
Harvey Harrisone374d482008-01-31 15:20:50 -08003589 len += sprint_symbol(buf + len, (unsigned long)l->addr);
Christoph Lameter88a420e2007-05-06 14:49:45 -07003590 else
Harvey Harrisone374d482008-01-31 15:20:50 -08003591 len += sprintf(buf + len, "<not-available>");
Christoph Lameter45edfa52007-05-09 02:32:45 -07003592
3593 if (l->sum_time != l->min_time) {
3594 unsigned long remainder;
3595
Harvey Harrisone374d482008-01-31 15:20:50 -08003596 len += sprintf(buf + len, " age=%ld/%ld/%ld",
Christoph Lameter45edfa52007-05-09 02:32:45 -07003597 l->min_time,
3598 div_long_long_rem(l->sum_time, l->count, &remainder),
3599 l->max_time);
3600 } else
Harvey Harrisone374d482008-01-31 15:20:50 -08003601 len += sprintf(buf + len, " age=%ld",
Christoph Lameter45edfa52007-05-09 02:32:45 -07003602 l->min_time);
3603
3604 if (l->min_pid != l->max_pid)
Harvey Harrisone374d482008-01-31 15:20:50 -08003605 len += sprintf(buf + len, " pid=%ld-%ld",
Christoph Lameter45edfa52007-05-09 02:32:45 -07003606 l->min_pid, l->max_pid);
3607 else
Harvey Harrisone374d482008-01-31 15:20:50 -08003608 len += sprintf(buf + len, " pid=%ld",
Christoph Lameter45edfa52007-05-09 02:32:45 -07003609 l->min_pid);
3610
Christoph Lameter84966342007-06-23 17:16:32 -07003611 if (num_online_cpus() > 1 && !cpus_empty(l->cpus) &&
Harvey Harrisone374d482008-01-31 15:20:50 -08003612 len < PAGE_SIZE - 60) {
3613 len += sprintf(buf + len, " cpus=");
3614 len += cpulist_scnprintf(buf + len, PAGE_SIZE - len - 50,
Christoph Lameter45edfa52007-05-09 02:32:45 -07003615 l->cpus);
3616 }
3617
Christoph Lameter84966342007-06-23 17:16:32 -07003618 if (num_online_nodes() > 1 && !nodes_empty(l->nodes) &&
Harvey Harrisone374d482008-01-31 15:20:50 -08003619 len < PAGE_SIZE - 60) {
3620 len += sprintf(buf + len, " nodes=");
3621 len += nodelist_scnprintf(buf + len, PAGE_SIZE - len - 50,
Christoph Lameter45edfa52007-05-09 02:32:45 -07003622 l->nodes);
3623 }
3624
Harvey Harrisone374d482008-01-31 15:20:50 -08003625 len += sprintf(buf + len, "\n");
Christoph Lameter88a420e2007-05-06 14:49:45 -07003626 }
3627
3628 free_loc_track(&t);
3629 if (!t.count)
Harvey Harrisone374d482008-01-31 15:20:50 -08003630 len += sprintf(buf, "No data\n");
3631 return len;
Christoph Lameter88a420e2007-05-06 14:49:45 -07003632}
3633
Christoph Lameter81819f02007-05-06 14:49:36 -07003634enum slab_stat_type {
3635 SL_FULL,
3636 SL_PARTIAL,
3637 SL_CPU,
3638 SL_OBJECTS
3639};
3640
3641#define SO_FULL (1 << SL_FULL)
3642#define SO_PARTIAL (1 << SL_PARTIAL)
3643#define SO_CPU (1 << SL_CPU)
3644#define SO_OBJECTS (1 << SL_OBJECTS)
3645
3646static unsigned long slab_objects(struct kmem_cache *s,
3647 char *buf, unsigned long flags)
3648{
3649 unsigned long total = 0;
3650 int cpu;
3651 int node;
3652 int x;
3653 unsigned long *nodes;
3654 unsigned long *per_cpu;
3655
3656 nodes = kzalloc(2 * sizeof(unsigned long) * nr_node_ids, GFP_KERNEL);
3657 per_cpu = nodes + nr_node_ids;
3658
3659 for_each_possible_cpu(cpu) {
Christoph Lameterdfb4f092007-10-16 01:26:05 -07003660 struct page *page;
3661 struct kmem_cache_cpu *c = get_cpu_slab(s, cpu);
Christoph Lameter81819f02007-05-06 14:49:36 -07003662
Christoph Lameterdfb4f092007-10-16 01:26:05 -07003663 if (!c)
3664 continue;
3665
3666 page = c->page;
Christoph Lameteree3c72a2007-10-16 01:26:07 -07003667 node = c->node;
3668 if (node < 0)
3669 continue;
Christoph Lameter81819f02007-05-06 14:49:36 -07003670 if (page) {
Christoph Lameter81819f02007-05-06 14:49:36 -07003671 if (flags & SO_CPU) {
Christoph Lameter81819f02007-05-06 14:49:36 -07003672 if (flags & SO_OBJECTS)
3673 x = page->inuse;
3674 else
3675 x = 1;
3676 total += x;
Christoph Lameteree3c72a2007-10-16 01:26:07 -07003677 nodes[node] += x;
Christoph Lameter81819f02007-05-06 14:49:36 -07003678 }
Christoph Lameteree3c72a2007-10-16 01:26:07 -07003679 per_cpu[node]++;
Christoph Lameter81819f02007-05-06 14:49:36 -07003680 }
3681 }
3682
Christoph Lameterf64dc582007-10-16 01:25:33 -07003683 for_each_node_state(node, N_NORMAL_MEMORY) {
Christoph Lameter81819f02007-05-06 14:49:36 -07003684 struct kmem_cache_node *n = get_node(s, node);
3685
3686 if (flags & SO_PARTIAL) {
3687 if (flags & SO_OBJECTS)
3688 x = count_partial(n);
3689 else
3690 x = n->nr_partial;
3691 total += x;
3692 nodes[node] += x;
3693 }
3694
3695 if (flags & SO_FULL) {
Christoph Lameter9e869432007-08-22 14:01:56 -07003696 int full_slabs = atomic_long_read(&n->nr_slabs)
Christoph Lameter81819f02007-05-06 14:49:36 -07003697 - per_cpu[node]
3698 - n->nr_partial;
3699
3700 if (flags & SO_OBJECTS)
3701 x = full_slabs * s->objects;
3702 else
3703 x = full_slabs;
3704 total += x;
3705 nodes[node] += x;
3706 }
3707 }
3708
3709 x = sprintf(buf, "%lu", total);
3710#ifdef CONFIG_NUMA
Christoph Lameterf64dc582007-10-16 01:25:33 -07003711 for_each_node_state(node, N_NORMAL_MEMORY)
Christoph Lameter81819f02007-05-06 14:49:36 -07003712 if (nodes[node])
3713 x += sprintf(buf + x, " N%d=%lu",
3714 node, nodes[node]);
3715#endif
3716 kfree(nodes);
3717 return x + sprintf(buf + x, "\n");
3718}
3719
3720static int any_slab_objects(struct kmem_cache *s)
3721{
3722 int node;
3723 int cpu;
3724
Christoph Lameterdfb4f092007-10-16 01:26:05 -07003725 for_each_possible_cpu(cpu) {
3726 struct kmem_cache_cpu *c = get_cpu_slab(s, cpu);
Christoph Lameter81819f02007-05-06 14:49:36 -07003727
Christoph Lameterdfb4f092007-10-16 01:26:05 -07003728 if (c && c->page)
3729 return 1;
3730 }
3731
3732 for_each_online_node(node) {
Christoph Lameter81819f02007-05-06 14:49:36 -07003733 struct kmem_cache_node *n = get_node(s, node);
3734
Christoph Lameterdfb4f092007-10-16 01:26:05 -07003735 if (!n)
3736 continue;
3737
Christoph Lameter9e869432007-08-22 14:01:56 -07003738 if (n->nr_partial || atomic_long_read(&n->nr_slabs))
Christoph Lameter81819f02007-05-06 14:49:36 -07003739 return 1;
3740 }
3741 return 0;
3742}
3743
3744#define to_slab_attr(n) container_of(n, struct slab_attribute, attr)
3745#define to_slab(n) container_of(n, struct kmem_cache, kobj);
3746
3747struct slab_attribute {
3748 struct attribute attr;
3749 ssize_t (*show)(struct kmem_cache *s, char *buf);
3750 ssize_t (*store)(struct kmem_cache *s, const char *x, size_t count);
3751};
3752
3753#define SLAB_ATTR_RO(_name) \
3754 static struct slab_attribute _name##_attr = __ATTR_RO(_name)
3755
3756#define SLAB_ATTR(_name) \
3757 static struct slab_attribute _name##_attr = \
3758 __ATTR(_name, 0644, _name##_show, _name##_store)
3759
Christoph Lameter81819f02007-05-06 14:49:36 -07003760static ssize_t slab_size_show(struct kmem_cache *s, char *buf)
3761{
3762 return sprintf(buf, "%d\n", s->size);
3763}
3764SLAB_ATTR_RO(slab_size);
3765
3766static ssize_t align_show(struct kmem_cache *s, char *buf)
3767{
3768 return sprintf(buf, "%d\n", s->align);
3769}
3770SLAB_ATTR_RO(align);
3771
3772static ssize_t object_size_show(struct kmem_cache *s, char *buf)
3773{
3774 return sprintf(buf, "%d\n", s->objsize);
3775}
3776SLAB_ATTR_RO(object_size);
3777
3778static ssize_t objs_per_slab_show(struct kmem_cache *s, char *buf)
3779{
3780 return sprintf(buf, "%d\n", s->objects);
3781}
3782SLAB_ATTR_RO(objs_per_slab);
3783
3784static ssize_t order_show(struct kmem_cache *s, char *buf)
3785{
3786 return sprintf(buf, "%d\n", s->order);
3787}
3788SLAB_ATTR_RO(order);
3789
3790static ssize_t ctor_show(struct kmem_cache *s, char *buf)
3791{
3792 if (s->ctor) {
3793 int n = sprint_symbol(buf, (unsigned long)s->ctor);
3794
3795 return n + sprintf(buf + n, "\n");
3796 }
3797 return 0;
3798}
3799SLAB_ATTR_RO(ctor);
3800
Christoph Lameter81819f02007-05-06 14:49:36 -07003801static ssize_t aliases_show(struct kmem_cache *s, char *buf)
3802{
3803 return sprintf(buf, "%d\n", s->refcount - 1);
3804}
3805SLAB_ATTR_RO(aliases);
3806
3807static ssize_t slabs_show(struct kmem_cache *s, char *buf)
3808{
3809 return slab_objects(s, buf, SO_FULL|SO_PARTIAL|SO_CPU);
3810}
3811SLAB_ATTR_RO(slabs);
3812
3813static ssize_t partial_show(struct kmem_cache *s, char *buf)
3814{
3815 return slab_objects(s, buf, SO_PARTIAL);
3816}
3817SLAB_ATTR_RO(partial);
3818
3819static ssize_t cpu_slabs_show(struct kmem_cache *s, char *buf)
3820{
3821 return slab_objects(s, buf, SO_CPU);
3822}
3823SLAB_ATTR_RO(cpu_slabs);
3824
3825static ssize_t objects_show(struct kmem_cache *s, char *buf)
3826{
3827 return slab_objects(s, buf, SO_FULL|SO_PARTIAL|SO_CPU|SO_OBJECTS);
3828}
3829SLAB_ATTR_RO(objects);
3830
3831static ssize_t sanity_checks_show(struct kmem_cache *s, char *buf)
3832{
3833 return sprintf(buf, "%d\n", !!(s->flags & SLAB_DEBUG_FREE));
3834}
3835
3836static ssize_t sanity_checks_store(struct kmem_cache *s,
3837 const char *buf, size_t length)
3838{
3839 s->flags &= ~SLAB_DEBUG_FREE;
3840 if (buf[0] == '1')
3841 s->flags |= SLAB_DEBUG_FREE;
3842 return length;
3843}
3844SLAB_ATTR(sanity_checks);
3845
3846static ssize_t trace_show(struct kmem_cache *s, char *buf)
3847{
3848 return sprintf(buf, "%d\n", !!(s->flags & SLAB_TRACE));
3849}
3850
3851static ssize_t trace_store(struct kmem_cache *s, const char *buf,
3852 size_t length)
3853{
3854 s->flags &= ~SLAB_TRACE;
3855 if (buf[0] == '1')
3856 s->flags |= SLAB_TRACE;
3857 return length;
3858}
3859SLAB_ATTR(trace);
3860
3861static ssize_t reclaim_account_show(struct kmem_cache *s, char *buf)
3862{
3863 return sprintf(buf, "%d\n", !!(s->flags & SLAB_RECLAIM_ACCOUNT));
3864}
3865
3866static ssize_t reclaim_account_store(struct kmem_cache *s,
3867 const char *buf, size_t length)
3868{
3869 s->flags &= ~SLAB_RECLAIM_ACCOUNT;
3870 if (buf[0] == '1')
3871 s->flags |= SLAB_RECLAIM_ACCOUNT;
3872 return length;
3873}
3874SLAB_ATTR(reclaim_account);
3875
3876static ssize_t hwcache_align_show(struct kmem_cache *s, char *buf)
3877{
Christoph Lameter5af60832007-05-06 14:49:56 -07003878 return sprintf(buf, "%d\n", !!(s->flags & SLAB_HWCACHE_ALIGN));
Christoph Lameter81819f02007-05-06 14:49:36 -07003879}
3880SLAB_ATTR_RO(hwcache_align);
3881
3882#ifdef CONFIG_ZONE_DMA
3883static ssize_t cache_dma_show(struct kmem_cache *s, char *buf)
3884{
3885 return sprintf(buf, "%d\n", !!(s->flags & SLAB_CACHE_DMA));
3886}
3887SLAB_ATTR_RO(cache_dma);
3888#endif
3889
3890static ssize_t destroy_by_rcu_show(struct kmem_cache *s, char *buf)
3891{
3892 return sprintf(buf, "%d\n", !!(s->flags & SLAB_DESTROY_BY_RCU));
3893}
3894SLAB_ATTR_RO(destroy_by_rcu);
3895
3896static ssize_t red_zone_show(struct kmem_cache *s, char *buf)
3897{
3898 return sprintf(buf, "%d\n", !!(s->flags & SLAB_RED_ZONE));
3899}
3900
3901static ssize_t red_zone_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_RED_ZONE;
3908 if (buf[0] == '1')
3909 s->flags |= SLAB_RED_ZONE;
3910 calculate_sizes(s);
3911 return length;
3912}
3913SLAB_ATTR(red_zone);
3914
3915static ssize_t poison_show(struct kmem_cache *s, char *buf)
3916{
3917 return sprintf(buf, "%d\n", !!(s->flags & SLAB_POISON));
3918}
3919
3920static ssize_t poison_store(struct kmem_cache *s,
3921 const char *buf, size_t length)
3922{
3923 if (any_slab_objects(s))
3924 return -EBUSY;
3925
3926 s->flags &= ~SLAB_POISON;
3927 if (buf[0] == '1')
3928 s->flags |= SLAB_POISON;
3929 calculate_sizes(s);
3930 return length;
3931}
3932SLAB_ATTR(poison);
3933
3934static ssize_t store_user_show(struct kmem_cache *s, char *buf)
3935{
3936 return sprintf(buf, "%d\n", !!(s->flags & SLAB_STORE_USER));
3937}
3938
3939static ssize_t store_user_store(struct kmem_cache *s,
3940 const char *buf, size_t length)
3941{
3942 if (any_slab_objects(s))
3943 return -EBUSY;
3944
3945 s->flags &= ~SLAB_STORE_USER;
3946 if (buf[0] == '1')
3947 s->flags |= SLAB_STORE_USER;
3948 calculate_sizes(s);
3949 return length;
3950}
3951SLAB_ATTR(store_user);
3952
Christoph Lameter53e15af2007-05-06 14:49:43 -07003953static ssize_t validate_show(struct kmem_cache *s, char *buf)
3954{
3955 return 0;
3956}
3957
3958static ssize_t validate_store(struct kmem_cache *s,
3959 const char *buf, size_t length)
3960{
Christoph Lameter434e2452007-07-17 04:03:30 -07003961 int ret = -EINVAL;
3962
3963 if (buf[0] == '1') {
3964 ret = validate_slab_cache(s);
3965 if (ret >= 0)
3966 ret = length;
3967 }
3968 return ret;
Christoph Lameter53e15af2007-05-06 14:49:43 -07003969}
3970SLAB_ATTR(validate);
3971
Christoph Lameter2086d262007-05-06 14:49:46 -07003972static ssize_t shrink_show(struct kmem_cache *s, char *buf)
3973{
3974 return 0;
3975}
3976
3977static ssize_t shrink_store(struct kmem_cache *s,
3978 const char *buf, size_t length)
3979{
3980 if (buf[0] == '1') {
3981 int rc = kmem_cache_shrink(s);
3982
3983 if (rc)
3984 return rc;
3985 } else
3986 return -EINVAL;
3987 return length;
3988}
3989SLAB_ATTR(shrink);
3990
Christoph Lameter88a420e2007-05-06 14:49:45 -07003991static ssize_t alloc_calls_show(struct kmem_cache *s, char *buf)
3992{
3993 if (!(s->flags & SLAB_STORE_USER))
3994 return -ENOSYS;
3995 return list_locations(s, buf, TRACK_ALLOC);
3996}
3997SLAB_ATTR_RO(alloc_calls);
3998
3999static ssize_t free_calls_show(struct kmem_cache *s, char *buf)
4000{
4001 if (!(s->flags & SLAB_STORE_USER))
4002 return -ENOSYS;
4003 return list_locations(s, buf, TRACK_FREE);
4004}
4005SLAB_ATTR_RO(free_calls);
4006
Christoph Lameter81819f02007-05-06 14:49:36 -07004007#ifdef CONFIG_NUMA
Christoph Lameter98246012008-01-07 23:20:26 -08004008static ssize_t remote_node_defrag_ratio_show(struct kmem_cache *s, char *buf)
Christoph Lameter81819f02007-05-06 14:49:36 -07004009{
Christoph Lameter98246012008-01-07 23:20:26 -08004010 return sprintf(buf, "%d\n", s->remote_node_defrag_ratio / 10);
Christoph Lameter81819f02007-05-06 14:49:36 -07004011}
4012
Christoph Lameter98246012008-01-07 23:20:26 -08004013static ssize_t remote_node_defrag_ratio_store(struct kmem_cache *s,
Christoph Lameter81819f02007-05-06 14:49:36 -07004014 const char *buf, size_t length)
4015{
4016 int n = simple_strtoul(buf, NULL, 10);
4017
4018 if (n < 100)
Christoph Lameter98246012008-01-07 23:20:26 -08004019 s->remote_node_defrag_ratio = n * 10;
Christoph Lameter81819f02007-05-06 14:49:36 -07004020 return length;
4021}
Christoph Lameter98246012008-01-07 23:20:26 -08004022SLAB_ATTR(remote_node_defrag_ratio);
Christoph Lameter81819f02007-05-06 14:49:36 -07004023#endif
4024
Christoph Lameter8ff12cf2008-02-07 17:47:41 -08004025#ifdef CONFIG_SLUB_STATS
4026
4027static int show_stat(struct kmem_cache *s, char *buf, enum stat_item si)
4028{
4029 unsigned long sum = 0;
4030 int cpu;
4031 int len;
4032 int *data = kmalloc(nr_cpu_ids * sizeof(int), GFP_KERNEL);
4033
4034 if (!data)
4035 return -ENOMEM;
4036
4037 for_each_online_cpu(cpu) {
4038 unsigned x = get_cpu_slab(s, cpu)->stat[si];
4039
4040 data[cpu] = x;
4041 sum += x;
4042 }
4043
4044 len = sprintf(buf, "%lu", sum);
4045
4046 for_each_online_cpu(cpu) {
4047 if (data[cpu] && len < PAGE_SIZE - 20)
4048 len += sprintf(buf + len, " c%d=%u", cpu, data[cpu]);
4049 }
4050 kfree(data);
4051 return len + sprintf(buf + len, "\n");
4052}
4053
4054#define STAT_ATTR(si, text) \
4055static ssize_t text##_show(struct kmem_cache *s, char *buf) \
4056{ \
4057 return show_stat(s, buf, si); \
4058} \
4059SLAB_ATTR_RO(text); \
4060
4061STAT_ATTR(ALLOC_FASTPATH, alloc_fastpath);
4062STAT_ATTR(ALLOC_SLOWPATH, alloc_slowpath);
4063STAT_ATTR(FREE_FASTPATH, free_fastpath);
4064STAT_ATTR(FREE_SLOWPATH, free_slowpath);
4065STAT_ATTR(FREE_FROZEN, free_frozen);
4066STAT_ATTR(FREE_ADD_PARTIAL, free_add_partial);
4067STAT_ATTR(FREE_REMOVE_PARTIAL, free_remove_partial);
4068STAT_ATTR(ALLOC_FROM_PARTIAL, alloc_from_partial);
4069STAT_ATTR(ALLOC_SLAB, alloc_slab);
4070STAT_ATTR(ALLOC_REFILL, alloc_refill);
4071STAT_ATTR(FREE_SLAB, free_slab);
4072STAT_ATTR(CPUSLAB_FLUSH, cpuslab_flush);
4073STAT_ATTR(DEACTIVATE_FULL, deactivate_full);
4074STAT_ATTR(DEACTIVATE_EMPTY, deactivate_empty);
4075STAT_ATTR(DEACTIVATE_TO_HEAD, deactivate_to_head);
4076STAT_ATTR(DEACTIVATE_TO_TAIL, deactivate_to_tail);
4077STAT_ATTR(DEACTIVATE_REMOTE_FREES, deactivate_remote_frees);
4078
4079#endif
4080
Pekka Enberg06428782008-01-07 23:20:27 -08004081static struct attribute *slab_attrs[] = {
Christoph Lameter81819f02007-05-06 14:49:36 -07004082 &slab_size_attr.attr,
4083 &object_size_attr.attr,
4084 &objs_per_slab_attr.attr,
4085 &order_attr.attr,
4086 &objects_attr.attr,
4087 &slabs_attr.attr,
4088 &partial_attr.attr,
4089 &cpu_slabs_attr.attr,
4090 &ctor_attr.attr,
Christoph Lameter81819f02007-05-06 14:49:36 -07004091 &aliases_attr.attr,
4092 &align_attr.attr,
4093 &sanity_checks_attr.attr,
4094 &trace_attr.attr,
4095 &hwcache_align_attr.attr,
4096 &reclaim_account_attr.attr,
4097 &destroy_by_rcu_attr.attr,
4098 &red_zone_attr.attr,
4099 &poison_attr.attr,
4100 &store_user_attr.attr,
Christoph Lameter53e15af2007-05-06 14:49:43 -07004101 &validate_attr.attr,
Christoph Lameter2086d262007-05-06 14:49:46 -07004102 &shrink_attr.attr,
Christoph Lameter88a420e2007-05-06 14:49:45 -07004103 &alloc_calls_attr.attr,
4104 &free_calls_attr.attr,
Christoph Lameter81819f02007-05-06 14:49:36 -07004105#ifdef CONFIG_ZONE_DMA
4106 &cache_dma_attr.attr,
4107#endif
4108#ifdef CONFIG_NUMA
Christoph Lameter98246012008-01-07 23:20:26 -08004109 &remote_node_defrag_ratio_attr.attr,
Christoph Lameter81819f02007-05-06 14:49:36 -07004110#endif
Christoph Lameter8ff12cf2008-02-07 17:47:41 -08004111#ifdef CONFIG_SLUB_STATS
4112 &alloc_fastpath_attr.attr,
4113 &alloc_slowpath_attr.attr,
4114 &free_fastpath_attr.attr,
4115 &free_slowpath_attr.attr,
4116 &free_frozen_attr.attr,
4117 &free_add_partial_attr.attr,
4118 &free_remove_partial_attr.attr,
4119 &alloc_from_partial_attr.attr,
4120 &alloc_slab_attr.attr,
4121 &alloc_refill_attr.attr,
4122 &free_slab_attr.attr,
4123 &cpuslab_flush_attr.attr,
4124 &deactivate_full_attr.attr,
4125 &deactivate_empty_attr.attr,
4126 &deactivate_to_head_attr.attr,
4127 &deactivate_to_tail_attr.attr,
4128 &deactivate_remote_frees_attr.attr,
4129#endif
Christoph Lameter81819f02007-05-06 14:49:36 -07004130 NULL
4131};
4132
4133static struct attribute_group slab_attr_group = {
4134 .attrs = slab_attrs,
4135};
4136
4137static ssize_t slab_attr_show(struct kobject *kobj,
4138 struct attribute *attr,
4139 char *buf)
4140{
4141 struct slab_attribute *attribute;
4142 struct kmem_cache *s;
4143 int err;
4144
4145 attribute = to_slab_attr(attr);
4146 s = to_slab(kobj);
4147
4148 if (!attribute->show)
4149 return -EIO;
4150
4151 err = attribute->show(s, buf);
4152
4153 return err;
4154}
4155
4156static ssize_t slab_attr_store(struct kobject *kobj,
4157 struct attribute *attr,
4158 const char *buf, size_t len)
4159{
4160 struct slab_attribute *attribute;
4161 struct kmem_cache *s;
4162 int err;
4163
4164 attribute = to_slab_attr(attr);
4165 s = to_slab(kobj);
4166
4167 if (!attribute->store)
4168 return -EIO;
4169
4170 err = attribute->store(s, buf, len);
4171
4172 return err;
4173}
4174
Christoph Lameter151c6022008-01-07 22:29:05 -08004175static void kmem_cache_release(struct kobject *kobj)
4176{
4177 struct kmem_cache *s = to_slab(kobj);
4178
4179 kfree(s);
4180}
4181
Christoph Lameter81819f02007-05-06 14:49:36 -07004182static struct sysfs_ops slab_sysfs_ops = {
4183 .show = slab_attr_show,
4184 .store = slab_attr_store,
4185};
4186
4187static struct kobj_type slab_ktype = {
4188 .sysfs_ops = &slab_sysfs_ops,
Christoph Lameter151c6022008-01-07 22:29:05 -08004189 .release = kmem_cache_release
Christoph Lameter81819f02007-05-06 14:49:36 -07004190};
4191
4192static int uevent_filter(struct kset *kset, struct kobject *kobj)
4193{
4194 struct kobj_type *ktype = get_ktype(kobj);
4195
4196 if (ktype == &slab_ktype)
4197 return 1;
4198 return 0;
4199}
4200
4201static struct kset_uevent_ops slab_uevent_ops = {
4202 .filter = uevent_filter,
4203};
4204
Greg Kroah-Hartman27c3a312007-11-01 09:29:06 -06004205static struct kset *slab_kset;
Christoph Lameter81819f02007-05-06 14:49:36 -07004206
4207#define ID_STR_LENGTH 64
4208
4209/* Create a unique string id for a slab cache:
4210 * format
4211 * :[flags-]size:[memory address of kmemcache]
4212 */
4213static char *create_unique_id(struct kmem_cache *s)
4214{
4215 char *name = kmalloc(ID_STR_LENGTH, GFP_KERNEL);
4216 char *p = name;
4217
4218 BUG_ON(!name);
4219
4220 *p++ = ':';
4221 /*
4222 * First flags affecting slabcache operations. We will only
4223 * get here for aliasable slabs so we do not need to support
4224 * too many flags. The flags here must cover all flags that
4225 * are matched during merging to guarantee that the id is
4226 * unique.
4227 */
4228 if (s->flags & SLAB_CACHE_DMA)
4229 *p++ = 'd';
4230 if (s->flags & SLAB_RECLAIM_ACCOUNT)
4231 *p++ = 'a';
4232 if (s->flags & SLAB_DEBUG_FREE)
4233 *p++ = 'F';
4234 if (p != name + 1)
4235 *p++ = '-';
4236 p += sprintf(p, "%07d", s->size);
4237 BUG_ON(p > name + ID_STR_LENGTH - 1);
4238 return name;
4239}
4240
4241static int sysfs_slab_add(struct kmem_cache *s)
4242{
4243 int err;
4244 const char *name;
4245 int unmergeable;
4246
4247 if (slab_state < SYSFS)
4248 /* Defer until later */
4249 return 0;
4250
4251 unmergeable = slab_unmergeable(s);
4252 if (unmergeable) {
4253 /*
4254 * Slabcache can never be merged so we can use the name proper.
4255 * This is typically the case for debug situations. In that
4256 * case we can catch duplicate names easily.
4257 */
Greg Kroah-Hartman27c3a312007-11-01 09:29:06 -06004258 sysfs_remove_link(&slab_kset->kobj, s->name);
Christoph Lameter81819f02007-05-06 14:49:36 -07004259 name = s->name;
4260 } else {
4261 /*
4262 * Create a unique name for the slab as a target
4263 * for the symlinks.
4264 */
4265 name = create_unique_id(s);
4266 }
4267
Greg Kroah-Hartman27c3a312007-11-01 09:29:06 -06004268 s->kobj.kset = slab_kset;
Greg Kroah-Hartman1eada112007-12-17 23:05:35 -07004269 err = kobject_init_and_add(&s->kobj, &slab_ktype, NULL, name);
4270 if (err) {
4271 kobject_put(&s->kobj);
Christoph Lameter81819f02007-05-06 14:49:36 -07004272 return err;
Greg Kroah-Hartman1eada112007-12-17 23:05:35 -07004273 }
Christoph Lameter81819f02007-05-06 14:49:36 -07004274
4275 err = sysfs_create_group(&s->kobj, &slab_attr_group);
4276 if (err)
4277 return err;
4278 kobject_uevent(&s->kobj, KOBJ_ADD);
4279 if (!unmergeable) {
4280 /* Setup first alias */
4281 sysfs_slab_alias(s, s->name);
4282 kfree(name);
4283 }
4284 return 0;
4285}
4286
4287static void sysfs_slab_remove(struct kmem_cache *s)
4288{
4289 kobject_uevent(&s->kobj, KOBJ_REMOVE);
4290 kobject_del(&s->kobj);
Christoph Lameter151c6022008-01-07 22:29:05 -08004291 kobject_put(&s->kobj);
Christoph Lameter81819f02007-05-06 14:49:36 -07004292}
4293
4294/*
4295 * Need to buffer aliases during bootup until sysfs becomes
4296 * available lest we loose that information.
4297 */
4298struct saved_alias {
4299 struct kmem_cache *s;
4300 const char *name;
4301 struct saved_alias *next;
4302};
4303
Adrian Bunk5af328a2007-07-17 04:03:27 -07004304static struct saved_alias *alias_list;
Christoph Lameter81819f02007-05-06 14:49:36 -07004305
4306static int sysfs_slab_alias(struct kmem_cache *s, const char *name)
4307{
4308 struct saved_alias *al;
4309
4310 if (slab_state == SYSFS) {
4311 /*
4312 * If we have a leftover link then remove it.
4313 */
Greg Kroah-Hartman27c3a312007-11-01 09:29:06 -06004314 sysfs_remove_link(&slab_kset->kobj, name);
4315 return sysfs_create_link(&slab_kset->kobj, &s->kobj, name);
Christoph Lameter81819f02007-05-06 14:49:36 -07004316 }
4317
4318 al = kmalloc(sizeof(struct saved_alias), GFP_KERNEL);
4319 if (!al)
4320 return -ENOMEM;
4321
4322 al->s = s;
4323 al->name = name;
4324 al->next = alias_list;
4325 alias_list = al;
4326 return 0;
4327}
4328
4329static int __init slab_sysfs_init(void)
4330{
Christoph Lameter5b95a4a2007-07-17 04:03:19 -07004331 struct kmem_cache *s;
Christoph Lameter81819f02007-05-06 14:49:36 -07004332 int err;
4333
Greg Kroah-Hartman0ff21e42007-11-06 10:36:58 -08004334 slab_kset = kset_create_and_add("slab", &slab_uevent_ops, kernel_kobj);
Greg Kroah-Hartman27c3a312007-11-01 09:29:06 -06004335 if (!slab_kset) {
Christoph Lameter81819f02007-05-06 14:49:36 -07004336 printk(KERN_ERR "Cannot register slab subsystem.\n");
4337 return -ENOSYS;
4338 }
4339
Christoph Lameter26a7bd02007-05-09 02:32:39 -07004340 slab_state = SYSFS;
4341
Christoph Lameter5b95a4a2007-07-17 04:03:19 -07004342 list_for_each_entry(s, &slab_caches, list) {
Christoph Lameter26a7bd02007-05-09 02:32:39 -07004343 err = sysfs_slab_add(s);
Christoph Lameter5d540fb2007-08-30 23:56:26 -07004344 if (err)
4345 printk(KERN_ERR "SLUB: Unable to add boot slab %s"
4346 " to sysfs\n", s->name);
Christoph Lameter26a7bd02007-05-09 02:32:39 -07004347 }
Christoph Lameter81819f02007-05-06 14:49:36 -07004348
4349 while (alias_list) {
4350 struct saved_alias *al = alias_list;
4351
4352 alias_list = alias_list->next;
4353 err = sysfs_slab_alias(al->s, al->name);
Christoph Lameter5d540fb2007-08-30 23:56:26 -07004354 if (err)
4355 printk(KERN_ERR "SLUB: Unable to add boot slab alias"
4356 " %s to sysfs\n", s->name);
Christoph Lameter81819f02007-05-06 14:49:36 -07004357 kfree(al);
4358 }
4359
4360 resiliency_test();
4361 return 0;
4362}
4363
4364__initcall(slab_sysfs_init);
Christoph Lameter81819f02007-05-06 14:49:36 -07004365#endif
Pekka J Enberg57ed3ed2008-01-01 17:23:28 +01004366
4367/*
4368 * The /proc/slabinfo ABI
4369 */
Linus Torvalds158a9622008-01-02 13:04:48 -08004370#ifdef CONFIG_SLABINFO
4371
4372ssize_t slabinfo_write(struct file *file, const char __user * buffer,
4373 size_t count, loff_t *ppos)
4374{
4375 return -EINVAL;
4376}
4377
Pekka J Enberg57ed3ed2008-01-01 17:23:28 +01004378
4379static void print_slabinfo_header(struct seq_file *m)
4380{
4381 seq_puts(m, "slabinfo - version: 2.1\n");
4382 seq_puts(m, "# name <active_objs> <num_objs> <objsize> "
4383 "<objperslab> <pagesperslab>");
4384 seq_puts(m, " : tunables <limit> <batchcount> <sharedfactor>");
4385 seq_puts(m, " : slabdata <active_slabs> <num_slabs> <sharedavail>");
4386 seq_putc(m, '\n');
4387}
4388
4389static void *s_start(struct seq_file *m, loff_t *pos)
4390{
4391 loff_t n = *pos;
4392
4393 down_read(&slub_lock);
4394 if (!n)
4395 print_slabinfo_header(m);
4396
4397 return seq_list_start(&slab_caches, *pos);
4398}
4399
4400static void *s_next(struct seq_file *m, void *p, loff_t *pos)
4401{
4402 return seq_list_next(p, &slab_caches, pos);
4403}
4404
4405static void s_stop(struct seq_file *m, void *p)
4406{
4407 up_read(&slub_lock);
4408}
4409
4410static int s_show(struct seq_file *m, void *p)
4411{
4412 unsigned long nr_partials = 0;
4413 unsigned long nr_slabs = 0;
4414 unsigned long nr_inuse = 0;
4415 unsigned long nr_objs;
4416 struct kmem_cache *s;
4417 int node;
4418
4419 s = list_entry(p, struct kmem_cache, list);
4420
4421 for_each_online_node(node) {
4422 struct kmem_cache_node *n = get_node(s, node);
4423
4424 if (!n)
4425 continue;
4426
4427 nr_partials += n->nr_partial;
4428 nr_slabs += atomic_long_read(&n->nr_slabs);
4429 nr_inuse += count_partial(n);
4430 }
4431
4432 nr_objs = nr_slabs * s->objects;
4433 nr_inuse += (nr_slabs - nr_partials) * s->objects;
4434
4435 seq_printf(m, "%-17s %6lu %6lu %6u %4u %4d", s->name, nr_inuse,
4436 nr_objs, s->size, s->objects, (1 << s->order));
4437 seq_printf(m, " : tunables %4u %4u %4u", 0, 0, 0);
4438 seq_printf(m, " : slabdata %6lu %6lu %6lu", nr_slabs, nr_slabs,
4439 0UL);
4440 seq_putc(m, '\n');
4441 return 0;
4442}
4443
4444const struct seq_operations slabinfo_op = {
4445 .start = s_start,
4446 .next = s_next,
4447 .stop = s_stop,
4448 .show = s_show,
4449};
4450
Linus Torvalds158a9622008-01-02 13:04:48 -08004451#endif /* CONFIG_SLABINFO */