blob: 70db2897c1ea626ea3b6f576fda91fa6c3c48cd9 [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>
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -070022#include <linux/debugobjects.h>
Christoph Lameter81819f02007-05-06 14:49:36 -070023#include <linux/kallsyms.h>
Yasunori Gotob9049e22007-10-21 16:41:37 -070024#include <linux/memory.h>
Christoph Lameter81819f02007-05-06 14:49:36 -070025
26/*
27 * Lock order:
28 * 1. slab_lock(page)
29 * 2. slab->list_lock
30 *
31 * The slab_lock protects operations on the object of a particular
32 * slab and its metadata in the page struct. If the slab lock
33 * has been taken then no allocations nor frees can be performed
34 * on the objects in the slab nor can the slab be added or removed
35 * from the partial or full lists since this would mean modifying
36 * the page_struct of the slab.
37 *
38 * The list_lock protects the partial and full list on each node and
39 * the partial slab counter. If taken then no new slabs may be added or
40 * removed from the lists nor make the number of partial slabs be modified.
41 * (Note that the total number of slabs is an atomic value that may be
42 * modified without taking the list lock).
43 *
44 * The list_lock is a centralized lock and thus we avoid taking it as
45 * much as possible. As long as SLUB does not have to handle partial
46 * slabs, operations can continue without any centralized lock. F.e.
47 * allocating a long series of objects that fill up slabs does not require
48 * the list lock.
49 *
50 * The lock order is sometimes inverted when we are trying to get a slab
51 * off a list. We take the list_lock and then look for a page on the list
52 * to use. While we do that objects in the slabs may be freed. We can
53 * only operate on the slab if we have also taken the slab_lock. So we use
54 * a slab_trylock() on the slab. If trylock was successful then no frees
55 * can occur anymore and we can use the slab for allocations etc. If the
56 * slab_trylock() does not succeed then frees are in progress in the slab and
57 * we must stay away from it for a while since we may cause a bouncing
58 * cacheline if we try to acquire the lock. So go onto the next slab.
59 * If all pages are busy then we may allocate a new slab instead of reusing
60 * a partial slab. A new slab has noone operating on it and thus there is
61 * no danger of cacheline contention.
62 *
63 * Interrupts are disabled during allocation and deallocation in order to
64 * make the slab allocator safe to use in the context of an irq. In addition
65 * interrupts are disabled to ensure that the processor does not change
66 * while handling per_cpu slabs, due to kernel preemption.
67 *
68 * SLUB assigns one slab for allocation to each processor.
69 * Allocations only occur from these slabs called cpu slabs.
70 *
Christoph Lameter672bba32007-05-09 02:32:39 -070071 * Slabs with free elements are kept on a partial list and during regular
72 * operations no list for full slabs is used. If an object in a full slab is
Christoph Lameter81819f02007-05-06 14:49:36 -070073 * freed then the slab will show up again on the partial lists.
Christoph Lameter672bba32007-05-09 02:32:39 -070074 * We track full slabs for debugging purposes though because otherwise we
75 * cannot scan all objects.
Christoph Lameter81819f02007-05-06 14:49:36 -070076 *
77 * Slabs are freed when they become empty. Teardown and setup is
78 * minimal so we rely on the page allocators per cpu caches for
79 * fast frees and allocs.
80 *
81 * Overloading of page flags that are otherwise used for LRU management.
82 *
Christoph Lameter4b6f0752007-05-16 22:10:53 -070083 * PageActive The slab is frozen and exempt from list processing.
84 * This means that the slab is dedicated to a purpose
85 * such as satisfying allocations for a specific
86 * processor. Objects may be freed in the slab while
87 * it is frozen but slab_free will then skip the usual
88 * list operations. It is up to the processor holding
89 * the slab to integrate the slab into the slab lists
90 * when the slab is no longer needed.
91 *
92 * One use of this flag is to mark slabs that are
93 * used for allocations. Then such a slab becomes a cpu
94 * slab. The cpu slab may be equipped with an additional
Christoph Lameterdfb4f092007-10-16 01:26:05 -070095 * freelist that allows lockless access to
Christoph Lameter894b8782007-05-10 03:15:16 -070096 * free objects in addition to the regular freelist
97 * that requires the slab lock.
Christoph Lameter81819f02007-05-06 14:49:36 -070098 *
99 * PageError Slab requires special handling due to debug
100 * options set. This moves slab handling out of
Christoph Lameter894b8782007-05-10 03:15:16 -0700101 * the fast path and disables lockless freelists.
Christoph Lameter81819f02007-05-06 14:49:36 -0700102 */
103
Christoph Lameter5577bd82007-05-16 22:10:56 -0700104#define FROZEN (1 << PG_active)
105
106#ifdef CONFIG_SLUB_DEBUG
107#define SLABDEBUG (1 << PG_error)
108#else
109#define SLABDEBUG 0
110#endif
111
Christoph Lameter4b6f0752007-05-16 22:10:53 -0700112static inline int SlabFrozen(struct page *page)
113{
Christoph Lameter5577bd82007-05-16 22:10:56 -0700114 return page->flags & FROZEN;
Christoph Lameter4b6f0752007-05-16 22:10:53 -0700115}
116
117static inline void SetSlabFrozen(struct page *page)
118{
Christoph Lameter5577bd82007-05-16 22:10:56 -0700119 page->flags |= FROZEN;
Christoph Lameter4b6f0752007-05-16 22:10:53 -0700120}
121
122static inline void ClearSlabFrozen(struct page *page)
123{
Christoph Lameter5577bd82007-05-16 22:10:56 -0700124 page->flags &= ~FROZEN;
Christoph Lameter4b6f0752007-05-16 22:10:53 -0700125}
126
Christoph Lameter35e5d7e2007-05-09 02:32:42 -0700127static inline int SlabDebug(struct page *page)
128{
Christoph Lameter5577bd82007-05-16 22:10:56 -0700129 return page->flags & SLABDEBUG;
Christoph Lameter35e5d7e2007-05-09 02:32:42 -0700130}
131
132static inline void SetSlabDebug(struct page *page)
133{
Christoph Lameter5577bd82007-05-16 22:10:56 -0700134 page->flags |= SLABDEBUG;
Christoph Lameter35e5d7e2007-05-09 02:32:42 -0700135}
136
137static inline void ClearSlabDebug(struct page *page)
138{
Christoph Lameter5577bd82007-05-16 22:10:56 -0700139 page->flags &= ~SLABDEBUG;
Christoph Lameter35e5d7e2007-05-09 02:32:42 -0700140}
141
Christoph Lameter81819f02007-05-06 14:49:36 -0700142/*
143 * Issues still to be resolved:
144 *
Christoph Lameter81819f02007-05-06 14:49:36 -0700145 * - Support PAGE_ALLOC_DEBUG. Should be easy to do.
146 *
Christoph Lameter81819f02007-05-06 14:49:36 -0700147 * - Variable sizing of the per node arrays
148 */
149
150/* Enable to test recovery from slab corruption on boot */
151#undef SLUB_RESILIENCY_TEST
152
Christoph Lameter81819f02007-05-06 14:49:36 -0700153/*
Christoph Lameter2086d262007-05-06 14:49:46 -0700154 * Mininum number of partial slabs. These will be left on the partial
155 * lists even if they are empty. kmem_cache_shrink may reclaim them.
156 */
Christoph Lameter76be8952007-12-21 14:37:37 -0800157#define MIN_PARTIAL 5
Christoph Lametere95eed52007-05-06 14:49:44 -0700158
Christoph Lameter2086d262007-05-06 14:49:46 -0700159/*
160 * Maximum number of desirable partial slabs.
161 * The existence of more partial slabs makes kmem_cache_shrink
162 * sort the partial list by the number of objects in the.
163 */
164#define MAX_PARTIAL 10
165
Christoph Lameter81819f02007-05-06 14:49:36 -0700166#define DEBUG_DEFAULT_FLAGS (SLAB_DEBUG_FREE | SLAB_RED_ZONE | \
167 SLAB_POISON | SLAB_STORE_USER)
Christoph Lameter672bba32007-05-09 02:32:39 -0700168
Christoph Lameter81819f02007-05-06 14:49:36 -0700169/*
170 * Set of flags that will prevent slab merging
171 */
172#define SLUB_NEVER_MERGE (SLAB_RED_ZONE | SLAB_POISON | SLAB_STORE_USER | \
173 SLAB_TRACE | SLAB_DESTROY_BY_RCU)
174
175#define SLUB_MERGE_SAME (SLAB_DEBUG_FREE | SLAB_RECLAIM_ACCOUNT | \
176 SLAB_CACHE_DMA)
177
178#ifndef ARCH_KMALLOC_MINALIGN
Christoph Lameter47bfdc02007-05-06 14:49:37 -0700179#define ARCH_KMALLOC_MINALIGN __alignof__(unsigned long long)
Christoph Lameter81819f02007-05-06 14:49:36 -0700180#endif
181
182#ifndef ARCH_SLAB_MINALIGN
Christoph Lameter47bfdc02007-05-06 14:49:37 -0700183#define ARCH_SLAB_MINALIGN __alignof__(unsigned long long)
Christoph Lameter81819f02007-05-06 14:49:36 -0700184#endif
185
186/* Internal SLUB flags */
Christoph Lameter1ceef402007-08-07 15:11:48 -0700187#define __OBJECT_POISON 0x80000000 /* Poison object */
188#define __SYSFS_ADD_DEFERRED 0x40000000 /* Not yet visible via sysfs */
Christoph Lameter81819f02007-05-06 14:49:36 -0700189
190static int kmem_size = sizeof(struct kmem_cache);
191
192#ifdef CONFIG_SMP
193static struct notifier_block slab_notifier;
194#endif
195
196static enum {
197 DOWN, /* No slab functionality available */
198 PARTIAL, /* kmem_cache_open() works but kmalloc does not */
Christoph Lameter672bba32007-05-09 02:32:39 -0700199 UP, /* Everything works but does not show up in sysfs */
Christoph Lameter81819f02007-05-06 14:49:36 -0700200 SYSFS /* Sysfs up */
201} slab_state = DOWN;
202
203/* A list of all slab caches on the system */
204static DECLARE_RWSEM(slub_lock);
Adrian Bunk5af328a2007-07-17 04:03:27 -0700205static LIST_HEAD(slab_caches);
Christoph Lameter81819f02007-05-06 14:49:36 -0700206
Christoph Lameter02cbc872007-05-09 02:32:43 -0700207/*
208 * Tracking user of a slab.
209 */
210struct track {
211 void *addr; /* Called from address */
212 int cpu; /* Was running on cpu */
213 int pid; /* Pid context */
214 unsigned long when; /* When did the operation occur */
215};
216
217enum track_item { TRACK_ALLOC, TRACK_FREE };
218
Christoph Lameter41ecc552007-05-09 02:32:44 -0700219#if defined(CONFIG_SYSFS) && defined(CONFIG_SLUB_DEBUG)
Christoph Lameter81819f02007-05-06 14:49:36 -0700220static int sysfs_slab_add(struct kmem_cache *);
221static int sysfs_slab_alias(struct kmem_cache *, const char *);
222static void sysfs_slab_remove(struct kmem_cache *);
Christoph Lameter8ff12cf2008-02-07 17:47:41 -0800223
Christoph Lameter81819f02007-05-06 14:49:36 -0700224#else
Christoph Lameter0c710012007-07-17 04:03:24 -0700225static inline int sysfs_slab_add(struct kmem_cache *s) { return 0; }
226static inline int sysfs_slab_alias(struct kmem_cache *s, const char *p)
227 { return 0; }
Christoph Lameter151c6022008-01-07 22:29:05 -0800228static inline void sysfs_slab_remove(struct kmem_cache *s)
229{
230 kfree(s);
231}
Christoph Lameter8ff12cf2008-02-07 17:47:41 -0800232
Christoph Lameter81819f02007-05-06 14:49:36 -0700233#endif
234
Christoph Lameter8ff12cf2008-02-07 17:47:41 -0800235static inline void stat(struct kmem_cache_cpu *c, enum stat_item si)
236{
237#ifdef CONFIG_SLUB_STATS
238 c->stat[si]++;
239#endif
240}
241
Christoph Lameter81819f02007-05-06 14:49:36 -0700242/********************************************************************
243 * Core slab cache functions
244 *******************************************************************/
245
246int slab_is_available(void)
247{
248 return slab_state >= UP;
249}
250
251static inline struct kmem_cache_node *get_node(struct kmem_cache *s, int node)
252{
253#ifdef CONFIG_NUMA
254 return s->node[node];
255#else
256 return &s->local_node;
257#endif
258}
259
Christoph Lameterdfb4f092007-10-16 01:26:05 -0700260static inline struct kmem_cache_cpu *get_cpu_slab(struct kmem_cache *s, int cpu)
261{
Christoph Lameter4c93c3552007-10-16 01:26:08 -0700262#ifdef CONFIG_SMP
263 return s->cpu_slab[cpu];
264#else
265 return &s->cpu_slab;
266#endif
Christoph Lameterdfb4f092007-10-16 01:26:05 -0700267}
268
Christoph Lameter6446faa2008-02-15 23:45:26 -0800269/* Verify that a pointer has an address that is valid within a slab page */
Christoph Lameter02cbc872007-05-09 02:32:43 -0700270static inline int check_valid_pointer(struct kmem_cache *s,
271 struct page *page, const void *object)
272{
273 void *base;
274
Christoph Lametera973e9d2008-03-01 13:40:44 -0800275 if (!object)
Christoph Lameter02cbc872007-05-09 02:32:43 -0700276 return 1;
277
Christoph Lametera973e9d2008-03-01 13:40:44 -0800278 base = page_address(page);
Christoph Lameter39b26462008-04-14 19:11:30 +0300279 if (object < base || object >= base + page->objects * s->size ||
Christoph Lameter02cbc872007-05-09 02:32:43 -0700280 (object - base) % s->size) {
281 return 0;
282 }
283
284 return 1;
285}
286
Christoph Lameter81819f02007-05-06 14:49:36 -0700287/*
Christoph Lameter7656c722007-05-09 02:32:40 -0700288 * Slow version of get and set free pointer.
289 *
290 * This version requires touching the cache lines of kmem_cache which
291 * we avoid to do in the fast alloc free paths. There we obtain the offset
292 * from the page struct.
293 */
294static inline void *get_freepointer(struct kmem_cache *s, void *object)
295{
296 return *(void **)(object + s->offset);
297}
298
299static inline void set_freepointer(struct kmem_cache *s, void *object, void *fp)
300{
301 *(void **)(object + s->offset) = fp;
302}
303
304/* Loop over all objects in a slab */
Christoph Lameter224a88b2008-04-14 19:11:31 +0300305#define for_each_object(__p, __s, __addr, __objects) \
306 for (__p = (__addr); __p < (__addr) + (__objects) * (__s)->size;\
Christoph Lameter7656c722007-05-09 02:32:40 -0700307 __p += (__s)->size)
308
309/* Scan freelist */
310#define for_each_free_object(__p, __s, __free) \
Christoph Lametera973e9d2008-03-01 13:40:44 -0800311 for (__p = (__free); __p; __p = get_freepointer((__s), __p))
Christoph Lameter7656c722007-05-09 02:32:40 -0700312
313/* Determine object index from a given position */
314static inline int slab_index(void *p, struct kmem_cache *s, void *addr)
315{
316 return (p - addr) / s->size;
317}
318
Christoph Lameter834f3d12008-04-14 19:11:31 +0300319static inline struct kmem_cache_order_objects oo_make(int order,
320 unsigned long size)
321{
322 struct kmem_cache_order_objects x = {
323 (order << 16) + (PAGE_SIZE << order) / size
324 };
325
326 return x;
327}
328
329static inline int oo_order(struct kmem_cache_order_objects x)
330{
331 return x.x >> 16;
332}
333
334static inline int oo_objects(struct kmem_cache_order_objects x)
335{
336 return x.x & ((1 << 16) - 1);
337}
338
Christoph Lameter41ecc552007-05-09 02:32:44 -0700339#ifdef CONFIG_SLUB_DEBUG
340/*
341 * Debug settings:
342 */
Christoph Lameterf0630ff2007-07-15 23:38:14 -0700343#ifdef CONFIG_SLUB_DEBUG_ON
344static int slub_debug = DEBUG_DEFAULT_FLAGS;
345#else
Christoph Lameter41ecc552007-05-09 02:32:44 -0700346static int slub_debug;
Christoph Lameterf0630ff2007-07-15 23:38:14 -0700347#endif
Christoph Lameter41ecc552007-05-09 02:32:44 -0700348
349static char *slub_debug_slabs;
350
Christoph Lameter7656c722007-05-09 02:32:40 -0700351/*
Christoph Lameter81819f02007-05-06 14:49:36 -0700352 * Object debugging
353 */
354static void print_section(char *text, u8 *addr, unsigned int length)
355{
356 int i, offset;
357 int newline = 1;
358 char ascii[17];
359
360 ascii[16] = 0;
361
362 for (i = 0; i < length; i++) {
363 if (newline) {
Christoph Lameter24922682007-07-17 04:03:18 -0700364 printk(KERN_ERR "%8s 0x%p: ", text, addr + i);
Christoph Lameter81819f02007-05-06 14:49:36 -0700365 newline = 0;
366 }
Pekka Enberg06428782008-01-07 23:20:27 -0800367 printk(KERN_CONT " %02x", addr[i]);
Christoph Lameter81819f02007-05-06 14:49:36 -0700368 offset = i % 16;
369 ascii[offset] = isgraph(addr[i]) ? addr[i] : '.';
370 if (offset == 15) {
Pekka Enberg06428782008-01-07 23:20:27 -0800371 printk(KERN_CONT " %s\n", ascii);
Christoph Lameter81819f02007-05-06 14:49:36 -0700372 newline = 1;
373 }
374 }
375 if (!newline) {
376 i %= 16;
377 while (i < 16) {
Pekka Enberg06428782008-01-07 23:20:27 -0800378 printk(KERN_CONT " ");
Christoph Lameter81819f02007-05-06 14:49:36 -0700379 ascii[i] = ' ';
380 i++;
381 }
Pekka Enberg06428782008-01-07 23:20:27 -0800382 printk(KERN_CONT " %s\n", ascii);
Christoph Lameter81819f02007-05-06 14:49:36 -0700383 }
384}
385
Christoph Lameter81819f02007-05-06 14:49:36 -0700386static struct track *get_track(struct kmem_cache *s, void *object,
387 enum track_item alloc)
388{
389 struct track *p;
390
391 if (s->offset)
392 p = object + s->offset + sizeof(void *);
393 else
394 p = object + s->inuse;
395
396 return p + alloc;
397}
398
399static void set_track(struct kmem_cache *s, void *object,
400 enum track_item alloc, void *addr)
401{
402 struct track *p;
403
404 if (s->offset)
405 p = object + s->offset + sizeof(void *);
406 else
407 p = object + s->inuse;
408
409 p += alloc;
410 if (addr) {
411 p->addr = addr;
412 p->cpu = smp_processor_id();
413 p->pid = current ? current->pid : -1;
414 p->when = jiffies;
415 } else
416 memset(p, 0, sizeof(struct track));
417}
418
Christoph Lameter81819f02007-05-06 14:49:36 -0700419static void init_tracking(struct kmem_cache *s, void *object)
420{
Christoph Lameter24922682007-07-17 04:03:18 -0700421 if (!(s->flags & SLAB_STORE_USER))
422 return;
423
424 set_track(s, object, TRACK_FREE, NULL);
425 set_track(s, object, TRACK_ALLOC, NULL);
Christoph Lameter81819f02007-05-06 14:49:36 -0700426}
427
428static void print_track(const char *s, struct track *t)
429{
430 if (!t->addr)
431 return;
432
Christoph Lameter24922682007-07-17 04:03:18 -0700433 printk(KERN_ERR "INFO: %s in ", s);
Christoph Lameter81819f02007-05-06 14:49:36 -0700434 __print_symbol("%s", (unsigned long)t->addr);
Christoph Lameter24922682007-07-17 04:03:18 -0700435 printk(" age=%lu cpu=%u pid=%d\n", jiffies - t->when, t->cpu, t->pid);
Christoph Lameter81819f02007-05-06 14:49:36 -0700436}
437
Christoph Lameter24922682007-07-17 04:03:18 -0700438static void print_tracking(struct kmem_cache *s, void *object)
439{
440 if (!(s->flags & SLAB_STORE_USER))
441 return;
442
443 print_track("Allocated", get_track(s, object, TRACK_ALLOC));
444 print_track("Freed", get_track(s, object, TRACK_FREE));
445}
446
447static void print_page_info(struct page *page)
448{
Christoph Lameter39b26462008-04-14 19:11:30 +0300449 printk(KERN_ERR "INFO: Slab 0x%p objects=%u used=%u fp=0x%p flags=0x%04lx\n",
450 page, page->objects, page->inuse, page->freelist, page->flags);
Christoph Lameter24922682007-07-17 04:03:18 -0700451
452}
453
454static void slab_bug(struct kmem_cache *s, char *fmt, ...)
455{
456 va_list args;
457 char buf[100];
458
459 va_start(args, fmt);
460 vsnprintf(buf, sizeof(buf), fmt, args);
461 va_end(args);
462 printk(KERN_ERR "========================================"
463 "=====================================\n");
464 printk(KERN_ERR "BUG %s: %s\n", s->name, buf);
465 printk(KERN_ERR "----------------------------------------"
466 "-------------------------------------\n\n");
467}
468
469static void slab_fix(struct kmem_cache *s, char *fmt, ...)
470{
471 va_list args;
472 char buf[100];
473
474 va_start(args, fmt);
475 vsnprintf(buf, sizeof(buf), fmt, args);
476 va_end(args);
477 printk(KERN_ERR "FIX %s: %s\n", s->name, buf);
478}
479
480static void print_trailer(struct kmem_cache *s, struct page *page, u8 *p)
Christoph Lameter81819f02007-05-06 14:49:36 -0700481{
482 unsigned int off; /* Offset of last byte */
Christoph Lametera973e9d2008-03-01 13:40:44 -0800483 u8 *addr = page_address(page);
Christoph Lameter24922682007-07-17 04:03:18 -0700484
485 print_tracking(s, p);
486
487 print_page_info(page);
488
489 printk(KERN_ERR "INFO: Object 0x%p @offset=%tu fp=0x%p\n\n",
490 p, p - addr, get_freepointer(s, p));
491
492 if (p > addr + 16)
493 print_section("Bytes b4", p - 16, 16);
494
495 print_section("Object", p, min(s->objsize, 128));
Christoph Lameter81819f02007-05-06 14:49:36 -0700496
497 if (s->flags & SLAB_RED_ZONE)
498 print_section("Redzone", p + s->objsize,
499 s->inuse - s->objsize);
500
Christoph Lameter81819f02007-05-06 14:49:36 -0700501 if (s->offset)
502 off = s->offset + sizeof(void *);
503 else
504 off = s->inuse;
505
Christoph Lameter24922682007-07-17 04:03:18 -0700506 if (s->flags & SLAB_STORE_USER)
Christoph Lameter81819f02007-05-06 14:49:36 -0700507 off += 2 * sizeof(struct track);
Christoph Lameter81819f02007-05-06 14:49:36 -0700508
509 if (off != s->size)
510 /* Beginning of the filler is the free pointer */
Christoph Lameter24922682007-07-17 04:03:18 -0700511 print_section("Padding", p + off, s->size - off);
512
513 dump_stack();
Christoph Lameter81819f02007-05-06 14:49:36 -0700514}
515
516static void object_err(struct kmem_cache *s, struct page *page,
517 u8 *object, char *reason)
518{
Christoph Lameter3dc50632008-04-23 12:28:01 -0700519 slab_bug(s, "%s", reason);
Christoph Lameter24922682007-07-17 04:03:18 -0700520 print_trailer(s, page, object);
Christoph Lameter81819f02007-05-06 14:49:36 -0700521}
522
Christoph Lameter24922682007-07-17 04:03:18 -0700523static void slab_err(struct kmem_cache *s, struct page *page, char *fmt, ...)
Christoph Lameter81819f02007-05-06 14:49:36 -0700524{
525 va_list args;
526 char buf[100];
527
Christoph Lameter24922682007-07-17 04:03:18 -0700528 va_start(args, fmt);
529 vsnprintf(buf, sizeof(buf), fmt, args);
Christoph Lameter81819f02007-05-06 14:49:36 -0700530 va_end(args);
Christoph Lameter3dc50632008-04-23 12:28:01 -0700531 slab_bug(s, "%s", buf);
Christoph Lameter24922682007-07-17 04:03:18 -0700532 print_page_info(page);
Christoph Lameter81819f02007-05-06 14:49:36 -0700533 dump_stack();
534}
535
536static void init_object(struct kmem_cache *s, void *object, int active)
537{
538 u8 *p = object;
539
540 if (s->flags & __OBJECT_POISON) {
541 memset(p, POISON_FREE, s->objsize - 1);
Pekka Enberg06428782008-01-07 23:20:27 -0800542 p[s->objsize - 1] = POISON_END;
Christoph Lameter81819f02007-05-06 14:49:36 -0700543 }
544
545 if (s->flags & SLAB_RED_ZONE)
546 memset(p + s->objsize,
547 active ? SLUB_RED_ACTIVE : SLUB_RED_INACTIVE,
548 s->inuse - s->objsize);
549}
550
Christoph Lameter24922682007-07-17 04:03:18 -0700551static u8 *check_bytes(u8 *start, unsigned int value, unsigned int bytes)
Christoph Lameter81819f02007-05-06 14:49:36 -0700552{
553 while (bytes) {
554 if (*start != (u8)value)
Christoph Lameter24922682007-07-17 04:03:18 -0700555 return start;
Christoph Lameter81819f02007-05-06 14:49:36 -0700556 start++;
557 bytes--;
558 }
Christoph Lameter24922682007-07-17 04:03:18 -0700559 return NULL;
560}
561
562static void restore_bytes(struct kmem_cache *s, char *message, u8 data,
563 void *from, void *to)
564{
565 slab_fix(s, "Restoring 0x%p-0x%p=0x%x\n", from, to - 1, data);
566 memset(from, data, to - from);
567}
568
569static int check_bytes_and_report(struct kmem_cache *s, struct page *page,
570 u8 *object, char *what,
Pekka Enberg06428782008-01-07 23:20:27 -0800571 u8 *start, unsigned int value, unsigned int bytes)
Christoph Lameter24922682007-07-17 04:03:18 -0700572{
573 u8 *fault;
574 u8 *end;
575
576 fault = check_bytes(start, value, bytes);
577 if (!fault)
578 return 1;
579
580 end = start + bytes;
581 while (end > fault && end[-1] == value)
582 end--;
583
584 slab_bug(s, "%s overwritten", what);
585 printk(KERN_ERR "INFO: 0x%p-0x%p. First byte 0x%x instead of 0x%x\n",
586 fault, end - 1, fault[0], value);
587 print_trailer(s, page, object);
588
589 restore_bytes(s, what, value, fault, end);
590 return 0;
Christoph Lameter81819f02007-05-06 14:49:36 -0700591}
592
Christoph Lameter81819f02007-05-06 14:49:36 -0700593/*
594 * Object layout:
595 *
596 * object address
597 * Bytes of the object to be managed.
598 * If the freepointer may overlay the object then the free
599 * pointer is the first word of the object.
Christoph Lameter672bba32007-05-09 02:32:39 -0700600 *
Christoph Lameter81819f02007-05-06 14:49:36 -0700601 * Poisoning uses 0x6b (POISON_FREE) and the last byte is
602 * 0xa5 (POISON_END)
603 *
604 * object + s->objsize
605 * Padding to reach word boundary. This is also used for Redzoning.
Christoph Lameter672bba32007-05-09 02:32:39 -0700606 * Padding is extended by another word if Redzoning is enabled and
607 * objsize == inuse.
608 *
Christoph Lameter81819f02007-05-06 14:49:36 -0700609 * We fill with 0xbb (RED_INACTIVE) for inactive objects and with
610 * 0xcc (RED_ACTIVE) for objects in use.
611 *
612 * object + s->inuse
Christoph Lameter672bba32007-05-09 02:32:39 -0700613 * Meta data starts here.
614 *
Christoph Lameter81819f02007-05-06 14:49:36 -0700615 * A. Free pointer (if we cannot overwrite object on free)
616 * B. Tracking data for SLAB_STORE_USER
Christoph Lameter672bba32007-05-09 02:32:39 -0700617 * C. Padding to reach required alignment boundary or at mininum
Christoph Lameter6446faa2008-02-15 23:45:26 -0800618 * one word if debugging is on to be able to detect writes
Christoph Lameter672bba32007-05-09 02:32:39 -0700619 * before the word boundary.
620 *
621 * Padding is done using 0x5a (POISON_INUSE)
Christoph Lameter81819f02007-05-06 14:49:36 -0700622 *
623 * object + s->size
Christoph Lameter672bba32007-05-09 02:32:39 -0700624 * Nothing is used beyond s->size.
Christoph Lameter81819f02007-05-06 14:49:36 -0700625 *
Christoph Lameter672bba32007-05-09 02:32:39 -0700626 * If slabcaches are merged then the objsize and inuse boundaries are mostly
627 * ignored. And therefore no slab options that rely on these boundaries
Christoph Lameter81819f02007-05-06 14:49:36 -0700628 * may be used with merged slabcaches.
629 */
630
Christoph Lameter81819f02007-05-06 14:49:36 -0700631static int check_pad_bytes(struct kmem_cache *s, struct page *page, u8 *p)
632{
633 unsigned long off = s->inuse; /* The end of info */
634
635 if (s->offset)
636 /* Freepointer is placed after the object. */
637 off += sizeof(void *);
638
639 if (s->flags & SLAB_STORE_USER)
640 /* We also have user information there */
641 off += 2 * sizeof(struct track);
642
643 if (s->size == off)
644 return 1;
645
Christoph Lameter24922682007-07-17 04:03:18 -0700646 return check_bytes_and_report(s, page, p, "Object padding",
647 p + off, POISON_INUSE, s->size - off);
Christoph Lameter81819f02007-05-06 14:49:36 -0700648}
649
Christoph Lameter39b26462008-04-14 19:11:30 +0300650/* Check the pad bytes at the end of a slab page */
Christoph Lameter81819f02007-05-06 14:49:36 -0700651static int slab_pad_check(struct kmem_cache *s, struct page *page)
652{
Christoph Lameter24922682007-07-17 04:03:18 -0700653 u8 *start;
654 u8 *fault;
655 u8 *end;
656 int length;
657 int remainder;
Christoph Lameter81819f02007-05-06 14:49:36 -0700658
659 if (!(s->flags & SLAB_POISON))
660 return 1;
661
Christoph Lametera973e9d2008-03-01 13:40:44 -0800662 start = page_address(page);
Christoph Lameter834f3d12008-04-14 19:11:31 +0300663 length = (PAGE_SIZE << compound_order(page));
Christoph Lameter39b26462008-04-14 19:11:30 +0300664 end = start + length;
665 remainder = length % s->size;
Christoph Lameter81819f02007-05-06 14:49:36 -0700666 if (!remainder)
667 return 1;
668
Christoph Lameter39b26462008-04-14 19:11:30 +0300669 fault = check_bytes(end - remainder, POISON_INUSE, remainder);
Christoph Lameter24922682007-07-17 04:03:18 -0700670 if (!fault)
671 return 1;
672 while (end > fault && end[-1] == POISON_INUSE)
673 end--;
674
675 slab_err(s, page, "Padding overwritten. 0x%p-0x%p", fault, end - 1);
Christoph Lameter39b26462008-04-14 19:11:30 +0300676 print_section("Padding", end - remainder, remainder);
Christoph Lameter24922682007-07-17 04:03:18 -0700677
678 restore_bytes(s, "slab padding", POISON_INUSE, start, end);
679 return 0;
Christoph Lameter81819f02007-05-06 14:49:36 -0700680}
681
682static int check_object(struct kmem_cache *s, struct page *page,
683 void *object, int active)
684{
685 u8 *p = object;
686 u8 *endobject = object + s->objsize;
687
688 if (s->flags & SLAB_RED_ZONE) {
689 unsigned int red =
690 active ? SLUB_RED_ACTIVE : SLUB_RED_INACTIVE;
691
Christoph Lameter24922682007-07-17 04:03:18 -0700692 if (!check_bytes_and_report(s, page, object, "Redzone",
693 endobject, red, s->inuse - s->objsize))
Christoph Lameter81819f02007-05-06 14:49:36 -0700694 return 0;
Christoph Lameter81819f02007-05-06 14:49:36 -0700695 } else {
Ingo Molnar3adbefe2008-02-05 17:57:39 -0800696 if ((s->flags & SLAB_POISON) && s->objsize < s->inuse) {
697 check_bytes_and_report(s, page, p, "Alignment padding",
698 endobject, POISON_INUSE, s->inuse - s->objsize);
699 }
Christoph Lameter81819f02007-05-06 14:49:36 -0700700 }
701
702 if (s->flags & SLAB_POISON) {
703 if (!active && (s->flags & __OBJECT_POISON) &&
Christoph Lameter24922682007-07-17 04:03:18 -0700704 (!check_bytes_and_report(s, page, p, "Poison", p,
705 POISON_FREE, s->objsize - 1) ||
706 !check_bytes_and_report(s, page, p, "Poison",
Pekka Enberg06428782008-01-07 23:20:27 -0800707 p + s->objsize - 1, POISON_END, 1)))
Christoph Lameter81819f02007-05-06 14:49:36 -0700708 return 0;
Christoph Lameter81819f02007-05-06 14:49:36 -0700709 /*
710 * check_pad_bytes cleans up on its own.
711 */
712 check_pad_bytes(s, page, p);
713 }
714
715 if (!s->offset && active)
716 /*
717 * Object and freepointer overlap. Cannot check
718 * freepointer while object is allocated.
719 */
720 return 1;
721
722 /* Check free pointer validity */
723 if (!check_valid_pointer(s, page, get_freepointer(s, p))) {
724 object_err(s, page, p, "Freepointer corrupt");
725 /*
726 * No choice but to zap it and thus loose the remainder
727 * of the free objects in this slab. May cause
Christoph Lameter672bba32007-05-09 02:32:39 -0700728 * another error because the object count is now wrong.
Christoph Lameter81819f02007-05-06 14:49:36 -0700729 */
Christoph Lametera973e9d2008-03-01 13:40:44 -0800730 set_freepointer(s, p, NULL);
Christoph Lameter81819f02007-05-06 14:49:36 -0700731 return 0;
732 }
733 return 1;
734}
735
736static int check_slab(struct kmem_cache *s, struct page *page)
737{
Christoph Lameter39b26462008-04-14 19:11:30 +0300738 int maxobj;
739
Christoph Lameter81819f02007-05-06 14:49:36 -0700740 VM_BUG_ON(!irqs_disabled());
741
742 if (!PageSlab(page)) {
Christoph Lameter24922682007-07-17 04:03:18 -0700743 slab_err(s, page, "Not a valid slab page");
Christoph Lameter81819f02007-05-06 14:49:36 -0700744 return 0;
745 }
Christoph Lameter39b26462008-04-14 19:11:30 +0300746
747 maxobj = (PAGE_SIZE << compound_order(page)) / s->size;
748 if (page->objects > maxobj) {
749 slab_err(s, page, "objects %u > max %u",
750 s->name, page->objects, maxobj);
751 return 0;
752 }
753 if (page->inuse > page->objects) {
Christoph Lameter24922682007-07-17 04:03:18 -0700754 slab_err(s, page, "inuse %u > max %u",
Christoph Lameter39b26462008-04-14 19:11:30 +0300755 s->name, page->inuse, page->objects);
Christoph Lameter81819f02007-05-06 14:49:36 -0700756 return 0;
757 }
758 /* Slab_pad_check fixes things up after itself */
759 slab_pad_check(s, page);
760 return 1;
761}
762
763/*
Christoph Lameter672bba32007-05-09 02:32:39 -0700764 * Determine if a certain object on a page is on the freelist. Must hold the
765 * slab lock to guarantee that the chains are in a consistent state.
Christoph Lameter81819f02007-05-06 14:49:36 -0700766 */
767static int on_freelist(struct kmem_cache *s, struct page *page, void *search)
768{
769 int nr = 0;
770 void *fp = page->freelist;
771 void *object = NULL;
Christoph Lameter224a88b2008-04-14 19:11:31 +0300772 unsigned long max_objects;
Christoph Lameter81819f02007-05-06 14:49:36 -0700773
Christoph Lameter39b26462008-04-14 19:11:30 +0300774 while (fp && nr <= page->objects) {
Christoph Lameter81819f02007-05-06 14:49:36 -0700775 if (fp == search)
776 return 1;
777 if (!check_valid_pointer(s, page, fp)) {
778 if (object) {
779 object_err(s, page, object,
780 "Freechain corrupt");
Christoph Lametera973e9d2008-03-01 13:40:44 -0800781 set_freepointer(s, object, NULL);
Christoph Lameter81819f02007-05-06 14:49:36 -0700782 break;
783 } else {
Christoph Lameter24922682007-07-17 04:03:18 -0700784 slab_err(s, page, "Freepointer corrupt");
Christoph Lametera973e9d2008-03-01 13:40:44 -0800785 page->freelist = NULL;
Christoph Lameter39b26462008-04-14 19:11:30 +0300786 page->inuse = page->objects;
Christoph Lameter24922682007-07-17 04:03:18 -0700787 slab_fix(s, "Freelist cleared");
Christoph Lameter81819f02007-05-06 14:49:36 -0700788 return 0;
789 }
790 break;
791 }
792 object = fp;
793 fp = get_freepointer(s, object);
794 nr++;
795 }
796
Christoph Lameter224a88b2008-04-14 19:11:31 +0300797 max_objects = (PAGE_SIZE << compound_order(page)) / s->size;
798 if (max_objects > 65535)
799 max_objects = 65535;
800
801 if (page->objects != max_objects) {
802 slab_err(s, page, "Wrong number of objects. Found %d but "
803 "should be %d", page->objects, max_objects);
804 page->objects = max_objects;
805 slab_fix(s, "Number of objects adjusted.");
806 }
Christoph Lameter39b26462008-04-14 19:11:30 +0300807 if (page->inuse != page->objects - nr) {
Christoph Lameter70d71222007-05-06 14:49:47 -0700808 slab_err(s, page, "Wrong object count. Counter is %d but "
Christoph Lameter39b26462008-04-14 19:11:30 +0300809 "counted were %d", page->inuse, page->objects - nr);
810 page->inuse = page->objects - nr;
Christoph Lameter24922682007-07-17 04:03:18 -0700811 slab_fix(s, "Object count adjusted.");
Christoph Lameter81819f02007-05-06 14:49:36 -0700812 }
813 return search == NULL;
814}
815
Christoph Lameter3ec09742007-05-16 22:11:00 -0700816static void trace(struct kmem_cache *s, struct page *page, void *object, int alloc)
817{
818 if (s->flags & SLAB_TRACE) {
819 printk(KERN_INFO "TRACE %s %s 0x%p inuse=%d fp=0x%p\n",
820 s->name,
821 alloc ? "alloc" : "free",
822 object, page->inuse,
823 page->freelist);
824
825 if (!alloc)
826 print_section("Object", (void *)object, s->objsize);
827
828 dump_stack();
829 }
830}
831
Christoph Lameter643b1132007-05-06 14:49:42 -0700832/*
Christoph Lameter672bba32007-05-09 02:32:39 -0700833 * Tracking of fully allocated slabs for debugging purposes.
Christoph Lameter643b1132007-05-06 14:49:42 -0700834 */
Christoph Lametere95eed52007-05-06 14:49:44 -0700835static void add_full(struct kmem_cache_node *n, struct page *page)
Christoph Lameter643b1132007-05-06 14:49:42 -0700836{
Christoph Lameter643b1132007-05-06 14:49:42 -0700837 spin_lock(&n->list_lock);
838 list_add(&page->lru, &n->full);
839 spin_unlock(&n->list_lock);
840}
841
842static void remove_full(struct kmem_cache *s, struct page *page)
843{
844 struct kmem_cache_node *n;
845
846 if (!(s->flags & SLAB_STORE_USER))
847 return;
848
849 n = get_node(s, page_to_nid(page));
850
851 spin_lock(&n->list_lock);
852 list_del(&page->lru);
853 spin_unlock(&n->list_lock);
854}
855
Christoph Lameter0f389ec2008-04-14 18:53:02 +0300856/* Tracking of the number of slabs for debugging purposes */
857static inline unsigned long slabs_node(struct kmem_cache *s, int node)
858{
859 struct kmem_cache_node *n = get_node(s, node);
860
861 return atomic_long_read(&n->nr_slabs);
862}
863
Christoph Lameter205ab992008-04-14 19:11:40 +0300864static inline void inc_slabs_node(struct kmem_cache *s, int node, int objects)
Christoph Lameter0f389ec2008-04-14 18:53:02 +0300865{
866 struct kmem_cache_node *n = get_node(s, node);
867
868 /*
869 * May be called early in order to allocate a slab for the
870 * kmem_cache_node structure. Solve the chicken-egg
871 * dilemma by deferring the increment of the count during
872 * bootstrap (see early_kmem_cache_node_alloc).
873 */
Christoph Lameter205ab992008-04-14 19:11:40 +0300874 if (!NUMA_BUILD || n) {
Christoph Lameter0f389ec2008-04-14 18:53:02 +0300875 atomic_long_inc(&n->nr_slabs);
Christoph Lameter205ab992008-04-14 19:11:40 +0300876 atomic_long_add(objects, &n->total_objects);
877 }
Christoph Lameter0f389ec2008-04-14 18:53:02 +0300878}
Christoph Lameter205ab992008-04-14 19:11:40 +0300879static inline void dec_slabs_node(struct kmem_cache *s, int node, int objects)
Christoph Lameter0f389ec2008-04-14 18:53:02 +0300880{
881 struct kmem_cache_node *n = get_node(s, node);
882
883 atomic_long_dec(&n->nr_slabs);
Christoph Lameter205ab992008-04-14 19:11:40 +0300884 atomic_long_sub(objects, &n->total_objects);
Christoph Lameter0f389ec2008-04-14 18:53:02 +0300885}
886
887/* Object debug checks for alloc/free paths */
Christoph Lameter3ec09742007-05-16 22:11:00 -0700888static void setup_object_debug(struct kmem_cache *s, struct page *page,
889 void *object)
890{
891 if (!(s->flags & (SLAB_STORE_USER|SLAB_RED_ZONE|__OBJECT_POISON)))
892 return;
893
894 init_object(s, object, 0);
895 init_tracking(s, object);
896}
897
898static int alloc_debug_processing(struct kmem_cache *s, struct page *page,
899 void *object, void *addr)
Christoph Lameter81819f02007-05-06 14:49:36 -0700900{
901 if (!check_slab(s, page))
902 goto bad;
903
Christoph Lameterd692ef62008-02-15 23:45:24 -0800904 if (!on_freelist(s, page, object)) {
Christoph Lameter24922682007-07-17 04:03:18 -0700905 object_err(s, page, object, "Object already allocated");
Christoph Lameter70d71222007-05-06 14:49:47 -0700906 goto bad;
Christoph Lameter81819f02007-05-06 14:49:36 -0700907 }
908
909 if (!check_valid_pointer(s, page, object)) {
910 object_err(s, page, object, "Freelist Pointer check fails");
Christoph Lameter70d71222007-05-06 14:49:47 -0700911 goto bad;
Christoph Lameter81819f02007-05-06 14:49:36 -0700912 }
913
Christoph Lameterd692ef62008-02-15 23:45:24 -0800914 if (!check_object(s, page, object, 0))
Christoph Lameter81819f02007-05-06 14:49:36 -0700915 goto bad;
Christoph Lameter81819f02007-05-06 14:49:36 -0700916
Christoph Lameter3ec09742007-05-16 22:11:00 -0700917 /* Success perform special debug activities for allocs */
918 if (s->flags & SLAB_STORE_USER)
919 set_track(s, object, TRACK_ALLOC, addr);
920 trace(s, page, object, 1);
921 init_object(s, object, 1);
Christoph Lameter81819f02007-05-06 14:49:36 -0700922 return 1;
Christoph Lameter3ec09742007-05-16 22:11:00 -0700923
Christoph Lameter81819f02007-05-06 14:49:36 -0700924bad:
925 if (PageSlab(page)) {
926 /*
927 * If this is a slab page then lets do the best we can
928 * to avoid issues in the future. Marking all objects
Christoph Lameter672bba32007-05-09 02:32:39 -0700929 * as used avoids touching the remaining objects.
Christoph Lameter81819f02007-05-06 14:49:36 -0700930 */
Christoph Lameter24922682007-07-17 04:03:18 -0700931 slab_fix(s, "Marking all objects used");
Christoph Lameter39b26462008-04-14 19:11:30 +0300932 page->inuse = page->objects;
Christoph Lametera973e9d2008-03-01 13:40:44 -0800933 page->freelist = NULL;
Christoph Lameter81819f02007-05-06 14:49:36 -0700934 }
935 return 0;
936}
937
Christoph Lameter3ec09742007-05-16 22:11:00 -0700938static int free_debug_processing(struct kmem_cache *s, struct page *page,
939 void *object, void *addr)
Christoph Lameter81819f02007-05-06 14:49:36 -0700940{
941 if (!check_slab(s, page))
942 goto fail;
943
944 if (!check_valid_pointer(s, page, object)) {
Christoph Lameter70d71222007-05-06 14:49:47 -0700945 slab_err(s, page, "Invalid object pointer 0x%p", object);
Christoph Lameter81819f02007-05-06 14:49:36 -0700946 goto fail;
947 }
948
949 if (on_freelist(s, page, object)) {
Christoph Lameter24922682007-07-17 04:03:18 -0700950 object_err(s, page, object, "Object already free");
Christoph Lameter81819f02007-05-06 14:49:36 -0700951 goto fail;
952 }
953
954 if (!check_object(s, page, object, 1))
955 return 0;
956
957 if (unlikely(s != page->slab)) {
Ingo Molnar3adbefe2008-02-05 17:57:39 -0800958 if (!PageSlab(page)) {
Christoph Lameter70d71222007-05-06 14:49:47 -0700959 slab_err(s, page, "Attempt to free object(0x%p) "
960 "outside of slab", object);
Ingo Molnar3adbefe2008-02-05 17:57:39 -0800961 } else if (!page->slab) {
Christoph Lameter81819f02007-05-06 14:49:36 -0700962 printk(KERN_ERR
Christoph Lameter70d71222007-05-06 14:49:47 -0700963 "SLUB <none>: no slab for object 0x%p.\n",
Christoph Lameter81819f02007-05-06 14:49:36 -0700964 object);
Christoph Lameter70d71222007-05-06 14:49:47 -0700965 dump_stack();
Pekka Enberg06428782008-01-07 23:20:27 -0800966 } else
Christoph Lameter24922682007-07-17 04:03:18 -0700967 object_err(s, page, object,
968 "page slab pointer corrupt.");
Christoph Lameter81819f02007-05-06 14:49:36 -0700969 goto fail;
970 }
Christoph Lameter3ec09742007-05-16 22:11:00 -0700971
972 /* Special debug activities for freeing objects */
Christoph Lametera973e9d2008-03-01 13:40:44 -0800973 if (!SlabFrozen(page) && !page->freelist)
Christoph Lameter3ec09742007-05-16 22:11:00 -0700974 remove_full(s, page);
975 if (s->flags & SLAB_STORE_USER)
976 set_track(s, object, TRACK_FREE, addr);
977 trace(s, page, object, 0);
978 init_object(s, object, 0);
Christoph Lameter81819f02007-05-06 14:49:36 -0700979 return 1;
Christoph Lameter3ec09742007-05-16 22:11:00 -0700980
Christoph Lameter81819f02007-05-06 14:49:36 -0700981fail:
Christoph Lameter24922682007-07-17 04:03:18 -0700982 slab_fix(s, "Object at 0x%p not freed", object);
Christoph Lameter81819f02007-05-06 14:49:36 -0700983 return 0;
984}
985
Christoph Lameter41ecc552007-05-09 02:32:44 -0700986static int __init setup_slub_debug(char *str)
987{
Christoph Lameterf0630ff2007-07-15 23:38:14 -0700988 slub_debug = DEBUG_DEFAULT_FLAGS;
989 if (*str++ != '=' || !*str)
990 /*
991 * No options specified. Switch on full debugging.
992 */
993 goto out;
Christoph Lameter41ecc552007-05-09 02:32:44 -0700994
995 if (*str == ',')
Christoph Lameterf0630ff2007-07-15 23:38:14 -0700996 /*
997 * No options but restriction on slabs. This means full
998 * debugging for slabs matching a pattern.
999 */
1000 goto check_slabs;
1001
1002 slub_debug = 0;
1003 if (*str == '-')
1004 /*
1005 * Switch off all debugging measures.
1006 */
1007 goto out;
1008
1009 /*
1010 * Determine which debug features should be switched on
1011 */
Pekka Enberg06428782008-01-07 23:20:27 -08001012 for (; *str && *str != ','; str++) {
Christoph Lameterf0630ff2007-07-15 23:38:14 -07001013 switch (tolower(*str)) {
1014 case 'f':
1015 slub_debug |= SLAB_DEBUG_FREE;
1016 break;
1017 case 'z':
1018 slub_debug |= SLAB_RED_ZONE;
1019 break;
1020 case 'p':
1021 slub_debug |= SLAB_POISON;
1022 break;
1023 case 'u':
1024 slub_debug |= SLAB_STORE_USER;
1025 break;
1026 case 't':
1027 slub_debug |= SLAB_TRACE;
1028 break;
1029 default:
1030 printk(KERN_ERR "slub_debug option '%c' "
Pekka Enberg06428782008-01-07 23:20:27 -08001031 "unknown. skipped\n", *str);
Christoph Lameterf0630ff2007-07-15 23:38:14 -07001032 }
1033 }
1034
1035check_slabs:
1036 if (*str == ',')
Christoph Lameter41ecc552007-05-09 02:32:44 -07001037 slub_debug_slabs = str + 1;
Christoph Lameterf0630ff2007-07-15 23:38:14 -07001038out:
Christoph Lameter41ecc552007-05-09 02:32:44 -07001039 return 1;
1040}
1041
1042__setup("slub_debug", setup_slub_debug);
1043
Christoph Lameterba0268a2007-09-11 15:24:11 -07001044static unsigned long kmem_cache_flags(unsigned long objsize,
1045 unsigned long flags, const char *name,
Christoph Lameter4ba9b9d2007-10-16 23:25:51 -07001046 void (*ctor)(struct kmem_cache *, void *))
Christoph Lameter41ecc552007-05-09 02:32:44 -07001047{
1048 /*
Christoph Lametere1533622008-02-15 23:45:24 -08001049 * Enable debugging if selected on the kernel commandline.
Christoph Lameter41ecc552007-05-09 02:32:44 -07001050 */
Christoph Lametere1533622008-02-15 23:45:24 -08001051 if (slub_debug && (!slub_debug_slabs ||
1052 strncmp(slub_debug_slabs, name, strlen(slub_debug_slabs)) == 0))
1053 flags |= slub_debug;
Christoph Lameterba0268a2007-09-11 15:24:11 -07001054
1055 return flags;
Christoph Lameter41ecc552007-05-09 02:32:44 -07001056}
1057#else
Christoph Lameter3ec09742007-05-16 22:11:00 -07001058static inline void setup_object_debug(struct kmem_cache *s,
1059 struct page *page, void *object) {}
Christoph Lameter41ecc552007-05-09 02:32:44 -07001060
Christoph Lameter3ec09742007-05-16 22:11:00 -07001061static inline int alloc_debug_processing(struct kmem_cache *s,
1062 struct page *page, void *object, void *addr) { return 0; }
Christoph Lameter41ecc552007-05-09 02:32:44 -07001063
Christoph Lameter3ec09742007-05-16 22:11:00 -07001064static inline int free_debug_processing(struct kmem_cache *s,
1065 struct page *page, void *object, void *addr) { return 0; }
Christoph Lameter41ecc552007-05-09 02:32:44 -07001066
Christoph Lameter41ecc552007-05-09 02:32:44 -07001067static inline int slab_pad_check(struct kmem_cache *s, struct page *page)
1068 { return 1; }
1069static inline int check_object(struct kmem_cache *s, struct page *page,
1070 void *object, int active) { return 1; }
Christoph Lameter3ec09742007-05-16 22:11:00 -07001071static inline void add_full(struct kmem_cache_node *n, struct page *page) {}
Christoph Lameterba0268a2007-09-11 15:24:11 -07001072static inline unsigned long kmem_cache_flags(unsigned long objsize,
1073 unsigned long flags, const char *name,
Christoph Lameter4ba9b9d2007-10-16 23:25:51 -07001074 void (*ctor)(struct kmem_cache *, void *))
Christoph Lameterba0268a2007-09-11 15:24:11 -07001075{
1076 return flags;
1077}
Christoph Lameter41ecc552007-05-09 02:32:44 -07001078#define slub_debug 0
Christoph Lameter0f389ec2008-04-14 18:53:02 +03001079
1080static inline unsigned long slabs_node(struct kmem_cache *s, int node)
1081 { return 0; }
Christoph Lameter205ab992008-04-14 19:11:40 +03001082static inline void inc_slabs_node(struct kmem_cache *s, int node,
1083 int objects) {}
1084static inline void dec_slabs_node(struct kmem_cache *s, int node,
1085 int objects) {}
Christoph Lameter41ecc552007-05-09 02:32:44 -07001086#endif
Christoph Lameter205ab992008-04-14 19:11:40 +03001087
Christoph Lameter81819f02007-05-06 14:49:36 -07001088/*
1089 * Slab allocation and freeing
1090 */
Christoph Lameter65c33762008-04-14 19:11:40 +03001091static inline struct page *alloc_slab_page(gfp_t flags, int node,
1092 struct kmem_cache_order_objects oo)
1093{
1094 int order = oo_order(oo);
1095
1096 if (node == -1)
1097 return alloc_pages(flags, order);
1098 else
1099 return alloc_pages_node(node, flags, order);
1100}
1101
Christoph Lameter81819f02007-05-06 14:49:36 -07001102static struct page *allocate_slab(struct kmem_cache *s, gfp_t flags, int node)
1103{
Pekka Enberg06428782008-01-07 23:20:27 -08001104 struct page *page;
Christoph Lameter834f3d12008-04-14 19:11:31 +03001105 struct kmem_cache_order_objects oo = s->oo;
Christoph Lameter81819f02007-05-06 14:49:36 -07001106
Christoph Lameterb7a49f02008-02-14 14:21:32 -08001107 flags |= s->allocflags;
Mel Gormane12ba742007-10-16 01:25:52 -07001108
Christoph Lameter65c33762008-04-14 19:11:40 +03001109 page = alloc_slab_page(flags | __GFP_NOWARN | __GFP_NORETRY, node,
1110 oo);
1111 if (unlikely(!page)) {
1112 oo = s->min;
1113 /*
1114 * Allocation may have failed due to fragmentation.
1115 * Try a lower order alloc if possible
1116 */
1117 page = alloc_slab_page(flags, node, oo);
1118 if (!page)
1119 return NULL;
Christoph Lameter81819f02007-05-06 14:49:36 -07001120
Christoph Lameter65c33762008-04-14 19:11:40 +03001121 stat(get_cpu_slab(s, raw_smp_processor_id()), ORDER_FALLBACK);
1122 }
Christoph Lameter834f3d12008-04-14 19:11:31 +03001123 page->objects = oo_objects(oo);
Christoph Lameter81819f02007-05-06 14:49:36 -07001124 mod_zone_page_state(page_zone(page),
1125 (s->flags & SLAB_RECLAIM_ACCOUNT) ?
1126 NR_SLAB_RECLAIMABLE : NR_SLAB_UNRECLAIMABLE,
Christoph Lameter65c33762008-04-14 19:11:40 +03001127 1 << oo_order(oo));
Christoph Lameter81819f02007-05-06 14:49:36 -07001128
1129 return page;
1130}
1131
1132static void setup_object(struct kmem_cache *s, struct page *page,
1133 void *object)
1134{
Christoph Lameter3ec09742007-05-16 22:11:00 -07001135 setup_object_debug(s, page, object);
Christoph Lameter4f104932007-05-06 14:50:17 -07001136 if (unlikely(s->ctor))
Christoph Lameter4ba9b9d2007-10-16 23:25:51 -07001137 s->ctor(s, object);
Christoph Lameter81819f02007-05-06 14:49:36 -07001138}
1139
1140static struct page *new_slab(struct kmem_cache *s, gfp_t flags, int node)
1141{
1142 struct page *page;
Christoph Lameter81819f02007-05-06 14:49:36 -07001143 void *start;
Christoph Lameter81819f02007-05-06 14:49:36 -07001144 void *last;
1145 void *p;
1146
Christoph Lameter6cb06222007-10-16 01:25:41 -07001147 BUG_ON(flags & GFP_SLAB_BUG_MASK);
Christoph Lameter81819f02007-05-06 14:49:36 -07001148
Christoph Lameter6cb06222007-10-16 01:25:41 -07001149 page = allocate_slab(s,
1150 flags & (GFP_RECLAIM_MASK | GFP_CONSTRAINT_MASK), node);
Christoph Lameter81819f02007-05-06 14:49:36 -07001151 if (!page)
1152 goto out;
1153
Christoph Lameter205ab992008-04-14 19:11:40 +03001154 inc_slabs_node(s, page_to_nid(page), page->objects);
Christoph Lameter81819f02007-05-06 14:49:36 -07001155 page->slab = s;
1156 page->flags |= 1 << PG_slab;
1157 if (s->flags & (SLAB_DEBUG_FREE | SLAB_RED_ZONE | SLAB_POISON |
1158 SLAB_STORE_USER | SLAB_TRACE))
Christoph Lameter35e5d7e2007-05-09 02:32:42 -07001159 SetSlabDebug(page);
Christoph Lameter81819f02007-05-06 14:49:36 -07001160
1161 start = page_address(page);
Christoph Lameter81819f02007-05-06 14:49:36 -07001162
1163 if (unlikely(s->flags & SLAB_POISON))
Christoph Lameter834f3d12008-04-14 19:11:31 +03001164 memset(start, POISON_INUSE, PAGE_SIZE << compound_order(page));
Christoph Lameter81819f02007-05-06 14:49:36 -07001165
1166 last = start;
Christoph Lameter224a88b2008-04-14 19:11:31 +03001167 for_each_object(p, s, start, page->objects) {
Christoph Lameter81819f02007-05-06 14:49:36 -07001168 setup_object(s, page, last);
1169 set_freepointer(s, last, p);
1170 last = p;
1171 }
1172 setup_object(s, page, last);
Christoph Lametera973e9d2008-03-01 13:40:44 -08001173 set_freepointer(s, last, NULL);
Christoph Lameter81819f02007-05-06 14:49:36 -07001174
1175 page->freelist = start;
1176 page->inuse = 0;
1177out:
Christoph Lameter81819f02007-05-06 14:49:36 -07001178 return page;
1179}
1180
1181static void __free_slab(struct kmem_cache *s, struct page *page)
1182{
Christoph Lameter834f3d12008-04-14 19:11:31 +03001183 int order = compound_order(page);
1184 int pages = 1 << order;
Christoph Lameter81819f02007-05-06 14:49:36 -07001185
Christoph Lameterc59def9f2007-05-16 22:10:50 -07001186 if (unlikely(SlabDebug(page))) {
Christoph Lameter81819f02007-05-06 14:49:36 -07001187 void *p;
1188
1189 slab_pad_check(s, page);
Christoph Lameter224a88b2008-04-14 19:11:31 +03001190 for_each_object(p, s, page_address(page),
1191 page->objects)
Christoph Lameter81819f02007-05-06 14:49:36 -07001192 check_object(s, page, p, 0);
Peter Zijlstra2208b762007-07-26 20:54:34 +02001193 ClearSlabDebug(page);
Christoph Lameter81819f02007-05-06 14:49:36 -07001194 }
1195
1196 mod_zone_page_state(page_zone(page),
1197 (s->flags & SLAB_RECLAIM_ACCOUNT) ?
1198 NR_SLAB_RECLAIMABLE : NR_SLAB_UNRECLAIMABLE,
Pekka Enberg06428782008-01-07 23:20:27 -08001199 -pages);
Christoph Lameter81819f02007-05-06 14:49:36 -07001200
Christoph Lameter49bd5222008-04-14 18:52:18 +03001201 __ClearPageSlab(page);
1202 reset_page_mapcount(page);
Christoph Lameter834f3d12008-04-14 19:11:31 +03001203 __free_pages(page, order);
Christoph Lameter81819f02007-05-06 14:49:36 -07001204}
1205
1206static void rcu_free_slab(struct rcu_head *h)
1207{
1208 struct page *page;
1209
1210 page = container_of((struct list_head *)h, struct page, lru);
1211 __free_slab(page->slab, page);
1212}
1213
1214static void free_slab(struct kmem_cache *s, struct page *page)
1215{
1216 if (unlikely(s->flags & SLAB_DESTROY_BY_RCU)) {
1217 /*
1218 * RCU free overloads the RCU head over the LRU
1219 */
1220 struct rcu_head *head = (void *)&page->lru;
1221
1222 call_rcu(head, rcu_free_slab);
1223 } else
1224 __free_slab(s, page);
1225}
1226
1227static void discard_slab(struct kmem_cache *s, struct page *page)
1228{
Christoph Lameter205ab992008-04-14 19:11:40 +03001229 dec_slabs_node(s, page_to_nid(page), page->objects);
Christoph Lameter81819f02007-05-06 14:49:36 -07001230 free_slab(s, page);
1231}
1232
1233/*
1234 * Per slab locking using the pagelock
1235 */
1236static __always_inline void slab_lock(struct page *page)
1237{
1238 bit_spin_lock(PG_locked, &page->flags);
1239}
1240
1241static __always_inline void slab_unlock(struct page *page)
1242{
Nick Piggina76d3542008-01-07 23:20:27 -08001243 __bit_spin_unlock(PG_locked, &page->flags);
Christoph Lameter81819f02007-05-06 14:49:36 -07001244}
1245
1246static __always_inline int slab_trylock(struct page *page)
1247{
1248 int rc = 1;
1249
1250 rc = bit_spin_trylock(PG_locked, &page->flags);
1251 return rc;
1252}
1253
1254/*
1255 * Management of partially allocated slabs
1256 */
Christoph Lameter7c2e1322008-01-07 23:20:27 -08001257static void add_partial(struct kmem_cache_node *n,
1258 struct page *page, int tail)
Christoph Lameter81819f02007-05-06 14:49:36 -07001259{
Christoph Lametere95eed52007-05-06 14:49:44 -07001260 spin_lock(&n->list_lock);
1261 n->nr_partial++;
Christoph Lameter7c2e1322008-01-07 23:20:27 -08001262 if (tail)
1263 list_add_tail(&page->lru, &n->partial);
1264 else
1265 list_add(&page->lru, &n->partial);
Christoph Lameter81819f02007-05-06 14:49:36 -07001266 spin_unlock(&n->list_lock);
1267}
1268
1269static void remove_partial(struct kmem_cache *s,
1270 struct page *page)
1271{
1272 struct kmem_cache_node *n = get_node(s, page_to_nid(page));
1273
1274 spin_lock(&n->list_lock);
1275 list_del(&page->lru);
1276 n->nr_partial--;
1277 spin_unlock(&n->list_lock);
1278}
1279
1280/*
Christoph Lameter672bba32007-05-09 02:32:39 -07001281 * Lock slab and remove from the partial list.
Christoph Lameter81819f02007-05-06 14:49:36 -07001282 *
Christoph Lameter672bba32007-05-09 02:32:39 -07001283 * Must hold list_lock.
Christoph Lameter81819f02007-05-06 14:49:36 -07001284 */
Christoph Lameter4b6f0752007-05-16 22:10:53 -07001285static inline int lock_and_freeze_slab(struct kmem_cache_node *n, struct page *page)
Christoph Lameter81819f02007-05-06 14:49:36 -07001286{
1287 if (slab_trylock(page)) {
1288 list_del(&page->lru);
1289 n->nr_partial--;
Christoph Lameter4b6f0752007-05-16 22:10:53 -07001290 SetSlabFrozen(page);
Christoph Lameter81819f02007-05-06 14:49:36 -07001291 return 1;
1292 }
1293 return 0;
1294}
1295
1296/*
Christoph Lameter672bba32007-05-09 02:32:39 -07001297 * Try to allocate a partial slab from a specific node.
Christoph Lameter81819f02007-05-06 14:49:36 -07001298 */
1299static struct page *get_partial_node(struct kmem_cache_node *n)
1300{
1301 struct page *page;
1302
1303 /*
1304 * Racy check. If we mistakenly see no partial slabs then we
1305 * just allocate an empty slab. If we mistakenly try to get a
Christoph Lameter672bba32007-05-09 02:32:39 -07001306 * partial slab and there is none available then get_partials()
1307 * will return NULL.
Christoph Lameter81819f02007-05-06 14:49:36 -07001308 */
1309 if (!n || !n->nr_partial)
1310 return NULL;
1311
1312 spin_lock(&n->list_lock);
1313 list_for_each_entry(page, &n->partial, lru)
Christoph Lameter4b6f0752007-05-16 22:10:53 -07001314 if (lock_and_freeze_slab(n, page))
Christoph Lameter81819f02007-05-06 14:49:36 -07001315 goto out;
1316 page = NULL;
1317out:
1318 spin_unlock(&n->list_lock);
1319 return page;
1320}
1321
1322/*
Christoph Lameter672bba32007-05-09 02:32:39 -07001323 * Get a page from somewhere. Search in increasing NUMA distances.
Christoph Lameter81819f02007-05-06 14:49:36 -07001324 */
1325static struct page *get_any_partial(struct kmem_cache *s, gfp_t flags)
1326{
1327#ifdef CONFIG_NUMA
1328 struct zonelist *zonelist;
Mel Gormandd1a2392008-04-28 02:12:17 -07001329 struct zoneref *z;
Mel Gorman54a6eb52008-04-28 02:12:16 -07001330 struct zone *zone;
1331 enum zone_type high_zoneidx = gfp_zone(flags);
Christoph Lameter81819f02007-05-06 14:49:36 -07001332 struct page *page;
1333
1334 /*
Christoph Lameter672bba32007-05-09 02:32:39 -07001335 * The defrag ratio allows a configuration of the tradeoffs between
1336 * inter node defragmentation and node local allocations. A lower
1337 * defrag_ratio increases the tendency to do local allocations
1338 * instead of attempting to obtain partial slabs from other nodes.
Christoph Lameter81819f02007-05-06 14:49:36 -07001339 *
Christoph Lameter672bba32007-05-09 02:32:39 -07001340 * If the defrag_ratio is set to 0 then kmalloc() always
1341 * returns node local objects. If the ratio is higher then kmalloc()
1342 * may return off node objects because partial slabs are obtained
1343 * from other nodes and filled up.
Christoph Lameter81819f02007-05-06 14:49:36 -07001344 *
Christoph Lameter6446faa2008-02-15 23:45:26 -08001345 * If /sys/kernel/slab/xx/defrag_ratio is set to 100 (which makes
Christoph Lameter672bba32007-05-09 02:32:39 -07001346 * defrag_ratio = 1000) then every (well almost) allocation will
1347 * first attempt to defrag slab caches on other nodes. This means
1348 * scanning over all nodes to look for partial slabs which may be
1349 * expensive if we do it every time we are trying to find a slab
1350 * with available objects.
Christoph Lameter81819f02007-05-06 14:49:36 -07001351 */
Christoph Lameter98246012008-01-07 23:20:26 -08001352 if (!s->remote_node_defrag_ratio ||
1353 get_cycles() % 1024 > s->remote_node_defrag_ratio)
Christoph Lameter81819f02007-05-06 14:49:36 -07001354 return NULL;
1355
Mel Gorman0e884602008-04-28 02:12:14 -07001356 zonelist = node_zonelist(slab_node(current->mempolicy), flags);
Mel Gorman54a6eb52008-04-28 02:12:16 -07001357 for_each_zone_zonelist(zone, z, zonelist, high_zoneidx) {
Christoph Lameter81819f02007-05-06 14:49:36 -07001358 struct kmem_cache_node *n;
1359
Mel Gorman54a6eb52008-04-28 02:12:16 -07001360 n = get_node(s, zone_to_nid(zone));
Christoph Lameter81819f02007-05-06 14:49:36 -07001361
Mel Gorman54a6eb52008-04-28 02:12:16 -07001362 if (n && cpuset_zone_allowed_hardwall(zone, flags) &&
Christoph Lametere95eed52007-05-06 14:49:44 -07001363 n->nr_partial > MIN_PARTIAL) {
Christoph Lameter81819f02007-05-06 14:49:36 -07001364 page = get_partial_node(n);
1365 if (page)
1366 return page;
1367 }
1368 }
1369#endif
1370 return NULL;
1371}
1372
1373/*
1374 * Get a partial page, lock it and return it.
1375 */
1376static struct page *get_partial(struct kmem_cache *s, gfp_t flags, int node)
1377{
1378 struct page *page;
1379 int searchnode = (node == -1) ? numa_node_id() : node;
1380
1381 page = get_partial_node(get_node(s, searchnode));
1382 if (page || (flags & __GFP_THISNODE))
1383 return page;
1384
1385 return get_any_partial(s, flags);
1386}
1387
1388/*
1389 * Move a page back to the lists.
1390 *
1391 * Must be called with the slab lock held.
1392 *
1393 * On exit the slab lock will have been dropped.
1394 */
Christoph Lameter7c2e1322008-01-07 23:20:27 -08001395static void unfreeze_slab(struct kmem_cache *s, struct page *page, int tail)
Christoph Lameter81819f02007-05-06 14:49:36 -07001396{
Christoph Lametere95eed52007-05-06 14:49:44 -07001397 struct kmem_cache_node *n = get_node(s, page_to_nid(page));
Christoph Lameter8ff12cf2008-02-07 17:47:41 -08001398 struct kmem_cache_cpu *c = get_cpu_slab(s, smp_processor_id());
Christoph Lametere95eed52007-05-06 14:49:44 -07001399
Christoph Lameter4b6f0752007-05-16 22:10:53 -07001400 ClearSlabFrozen(page);
Christoph Lameter81819f02007-05-06 14:49:36 -07001401 if (page->inuse) {
Christoph Lametere95eed52007-05-06 14:49:44 -07001402
Christoph Lametera973e9d2008-03-01 13:40:44 -08001403 if (page->freelist) {
Christoph Lameter7c2e1322008-01-07 23:20:27 -08001404 add_partial(n, page, tail);
Christoph Lameter8ff12cf2008-02-07 17:47:41 -08001405 stat(c, tail ? DEACTIVATE_TO_TAIL : DEACTIVATE_TO_HEAD);
1406 } else {
1407 stat(c, DEACTIVATE_FULL);
1408 if (SlabDebug(page) && (s->flags & SLAB_STORE_USER))
1409 add_full(n, page);
1410 }
Christoph Lameter81819f02007-05-06 14:49:36 -07001411 slab_unlock(page);
1412 } else {
Christoph Lameter8ff12cf2008-02-07 17:47:41 -08001413 stat(c, DEACTIVATE_EMPTY);
Christoph Lametere95eed52007-05-06 14:49:44 -07001414 if (n->nr_partial < MIN_PARTIAL) {
1415 /*
Christoph Lameter672bba32007-05-09 02:32:39 -07001416 * Adding an empty slab to the partial slabs in order
1417 * to avoid page allocator overhead. This slab needs
1418 * to come after the other slabs with objects in
Christoph Lameter6446faa2008-02-15 23:45:26 -08001419 * so that the others get filled first. That way the
1420 * size of the partial list stays small.
1421 *
1422 * kmem_cache_shrink can reclaim any empty slabs from the
1423 * partial list.
Christoph Lametere95eed52007-05-06 14:49:44 -07001424 */
Christoph Lameter7c2e1322008-01-07 23:20:27 -08001425 add_partial(n, page, 1);
Christoph Lametere95eed52007-05-06 14:49:44 -07001426 slab_unlock(page);
1427 } else {
1428 slab_unlock(page);
Christoph Lameter8ff12cf2008-02-07 17:47:41 -08001429 stat(get_cpu_slab(s, raw_smp_processor_id()), FREE_SLAB);
Christoph Lametere95eed52007-05-06 14:49:44 -07001430 discard_slab(s, page);
1431 }
Christoph Lameter81819f02007-05-06 14:49:36 -07001432 }
1433}
1434
1435/*
1436 * Remove the cpu slab
1437 */
Christoph Lameterdfb4f092007-10-16 01:26:05 -07001438static void deactivate_slab(struct kmem_cache *s, struct kmem_cache_cpu *c)
Christoph Lameter81819f02007-05-06 14:49:36 -07001439{
Christoph Lameterdfb4f092007-10-16 01:26:05 -07001440 struct page *page = c->page;
Christoph Lameter7c2e1322008-01-07 23:20:27 -08001441 int tail = 1;
Christoph Lameter8ff12cf2008-02-07 17:47:41 -08001442
Christoph Lameterb773ad72008-03-04 11:10:17 -08001443 if (page->freelist)
Christoph Lameter8ff12cf2008-02-07 17:47:41 -08001444 stat(c, DEACTIVATE_REMOTE_FREES);
Christoph Lameter894b8782007-05-10 03:15:16 -07001445 /*
Christoph Lameter6446faa2008-02-15 23:45:26 -08001446 * Merge cpu freelist into slab freelist. Typically we get here
Christoph Lameter894b8782007-05-10 03:15:16 -07001447 * because both freelists are empty. So this is unlikely
1448 * to occur.
1449 */
Christoph Lametera973e9d2008-03-01 13:40:44 -08001450 while (unlikely(c->freelist)) {
Christoph Lameter894b8782007-05-10 03:15:16 -07001451 void **object;
1452
Christoph Lameter7c2e1322008-01-07 23:20:27 -08001453 tail = 0; /* Hot objects. Put the slab first */
1454
Christoph Lameter894b8782007-05-10 03:15:16 -07001455 /* Retrieve object from cpu_freelist */
Christoph Lameterdfb4f092007-10-16 01:26:05 -07001456 object = c->freelist;
Christoph Lameterb3fba8d2007-10-16 01:26:06 -07001457 c->freelist = c->freelist[c->offset];
Christoph Lameter894b8782007-05-10 03:15:16 -07001458
1459 /* And put onto the regular freelist */
Christoph Lameterb3fba8d2007-10-16 01:26:06 -07001460 object[c->offset] = page->freelist;
Christoph Lameter894b8782007-05-10 03:15:16 -07001461 page->freelist = object;
1462 page->inuse--;
1463 }
Christoph Lameterdfb4f092007-10-16 01:26:05 -07001464 c->page = NULL;
Christoph Lameter7c2e1322008-01-07 23:20:27 -08001465 unfreeze_slab(s, page, tail);
Christoph Lameter81819f02007-05-06 14:49:36 -07001466}
1467
Christoph Lameterdfb4f092007-10-16 01:26:05 -07001468static inline void flush_slab(struct kmem_cache *s, struct kmem_cache_cpu *c)
Christoph Lameter81819f02007-05-06 14:49:36 -07001469{
Christoph Lameter8ff12cf2008-02-07 17:47:41 -08001470 stat(c, CPUSLAB_FLUSH);
Christoph Lameterdfb4f092007-10-16 01:26:05 -07001471 slab_lock(c->page);
1472 deactivate_slab(s, c);
Christoph Lameter81819f02007-05-06 14:49:36 -07001473}
1474
1475/*
1476 * Flush cpu slab.
Christoph Lameter6446faa2008-02-15 23:45:26 -08001477 *
Christoph Lameter81819f02007-05-06 14:49:36 -07001478 * Called from IPI handler with interrupts disabled.
1479 */
Christoph Lameter0c710012007-07-17 04:03:24 -07001480static inline void __flush_cpu_slab(struct kmem_cache *s, int cpu)
Christoph Lameter81819f02007-05-06 14:49:36 -07001481{
Christoph Lameterdfb4f092007-10-16 01:26:05 -07001482 struct kmem_cache_cpu *c = get_cpu_slab(s, cpu);
Christoph Lameter81819f02007-05-06 14:49:36 -07001483
Christoph Lameterdfb4f092007-10-16 01:26:05 -07001484 if (likely(c && c->page))
1485 flush_slab(s, c);
Christoph Lameter81819f02007-05-06 14:49:36 -07001486}
1487
1488static void flush_cpu_slab(void *d)
1489{
1490 struct kmem_cache *s = d;
Christoph Lameter81819f02007-05-06 14:49:36 -07001491
Christoph Lameterdfb4f092007-10-16 01:26:05 -07001492 __flush_cpu_slab(s, smp_processor_id());
Christoph Lameter81819f02007-05-06 14:49:36 -07001493}
1494
1495static void flush_all(struct kmem_cache *s)
1496{
1497#ifdef CONFIG_SMP
1498 on_each_cpu(flush_cpu_slab, s, 1, 1);
1499#else
1500 unsigned long flags;
1501
1502 local_irq_save(flags);
1503 flush_cpu_slab(s);
1504 local_irq_restore(flags);
1505#endif
1506}
1507
1508/*
Christoph Lameterdfb4f092007-10-16 01:26:05 -07001509 * Check if the objects in a per cpu structure fit numa
1510 * locality expectations.
1511 */
1512static inline int node_match(struct kmem_cache_cpu *c, int node)
1513{
1514#ifdef CONFIG_NUMA
1515 if (node != -1 && c->node != node)
1516 return 0;
1517#endif
1518 return 1;
1519}
1520
1521/*
Christoph Lameter894b8782007-05-10 03:15:16 -07001522 * Slow path. The lockless freelist is empty or we need to perform
1523 * debugging duties.
Christoph Lameter81819f02007-05-06 14:49:36 -07001524 *
Christoph Lameter894b8782007-05-10 03:15:16 -07001525 * Interrupts are disabled.
Christoph Lameter81819f02007-05-06 14:49:36 -07001526 *
Christoph Lameter894b8782007-05-10 03:15:16 -07001527 * Processing is still very fast if new objects have been freed to the
1528 * regular freelist. In that case we simply take over the regular freelist
1529 * as the lockless freelist and zap the regular freelist.
Christoph Lameter81819f02007-05-06 14:49:36 -07001530 *
Christoph Lameter894b8782007-05-10 03:15:16 -07001531 * If that is not working then we fall back to the partial lists. We take the
1532 * first element of the freelist as the object to allocate now and move the
1533 * rest of the freelist to the lockless freelist.
1534 *
1535 * And if we were unable to get a new slab from the partial slab lists then
Christoph Lameter6446faa2008-02-15 23:45:26 -08001536 * we need to allocate a new slab. This is the slowest path since it involves
1537 * a call to the page allocator and the setup of a new slab.
Christoph Lameter81819f02007-05-06 14:49:36 -07001538 */
Christoph Lameter894b8782007-05-10 03:15:16 -07001539static void *__slab_alloc(struct kmem_cache *s,
Christoph Lameterdfb4f092007-10-16 01:26:05 -07001540 gfp_t gfpflags, int node, void *addr, struct kmem_cache_cpu *c)
Christoph Lameter81819f02007-05-06 14:49:36 -07001541{
Christoph Lameter81819f02007-05-06 14:49:36 -07001542 void **object;
Christoph Lameterdfb4f092007-10-16 01:26:05 -07001543 struct page *new;
Christoph Lameter81819f02007-05-06 14:49:36 -07001544
Linus Torvaldse72e9c22008-03-27 20:56:33 -07001545 /* We handle __GFP_ZERO in the caller */
1546 gfpflags &= ~__GFP_ZERO;
1547
Christoph Lameterdfb4f092007-10-16 01:26:05 -07001548 if (!c->page)
Christoph Lameter81819f02007-05-06 14:49:36 -07001549 goto new_slab;
1550
Christoph Lameterdfb4f092007-10-16 01:26:05 -07001551 slab_lock(c->page);
1552 if (unlikely(!node_match(c, node)))
Christoph Lameter81819f02007-05-06 14:49:36 -07001553 goto another_slab;
Christoph Lameter6446faa2008-02-15 23:45:26 -08001554
Christoph Lameter8ff12cf2008-02-07 17:47:41 -08001555 stat(c, ALLOC_REFILL);
Christoph Lameter6446faa2008-02-15 23:45:26 -08001556
Christoph Lameter894b8782007-05-10 03:15:16 -07001557load_freelist:
Christoph Lameterdfb4f092007-10-16 01:26:05 -07001558 object = c->page->freelist;
Christoph Lametera973e9d2008-03-01 13:40:44 -08001559 if (unlikely(!object))
Christoph Lameter81819f02007-05-06 14:49:36 -07001560 goto another_slab;
Christoph Lameterdfb4f092007-10-16 01:26:05 -07001561 if (unlikely(SlabDebug(c->page)))
Christoph Lameter81819f02007-05-06 14:49:36 -07001562 goto debug;
1563
Christoph Lameterb3fba8d2007-10-16 01:26:06 -07001564 c->freelist = object[c->offset];
Christoph Lameter39b26462008-04-14 19:11:30 +03001565 c->page->inuse = c->page->objects;
Christoph Lametera973e9d2008-03-01 13:40:44 -08001566 c->page->freelist = NULL;
Christoph Lameterdfb4f092007-10-16 01:26:05 -07001567 c->node = page_to_nid(c->page);
Christoph Lameter1f842602008-01-07 23:20:30 -08001568unlock_out:
Christoph Lameterdfb4f092007-10-16 01:26:05 -07001569 slab_unlock(c->page);
Christoph Lameter8ff12cf2008-02-07 17:47:41 -08001570 stat(c, ALLOC_SLOWPATH);
Christoph Lameter81819f02007-05-06 14:49:36 -07001571 return object;
1572
1573another_slab:
Christoph Lameterdfb4f092007-10-16 01:26:05 -07001574 deactivate_slab(s, c);
Christoph Lameter81819f02007-05-06 14:49:36 -07001575
1576new_slab:
Christoph Lameterdfb4f092007-10-16 01:26:05 -07001577 new = get_partial(s, gfpflags, node);
1578 if (new) {
1579 c->page = new;
Christoph Lameter8ff12cf2008-02-07 17:47:41 -08001580 stat(c, ALLOC_FROM_PARTIAL);
Christoph Lameter894b8782007-05-10 03:15:16 -07001581 goto load_freelist;
Christoph Lameter81819f02007-05-06 14:49:36 -07001582 }
1583
Christoph Lameterb811c202007-10-16 23:25:51 -07001584 if (gfpflags & __GFP_WAIT)
1585 local_irq_enable();
1586
Christoph Lameterdfb4f092007-10-16 01:26:05 -07001587 new = new_slab(s, gfpflags, node);
Christoph Lameterb811c202007-10-16 23:25:51 -07001588
1589 if (gfpflags & __GFP_WAIT)
1590 local_irq_disable();
1591
Christoph Lameterdfb4f092007-10-16 01:26:05 -07001592 if (new) {
1593 c = get_cpu_slab(s, smp_processor_id());
Christoph Lameter8ff12cf2008-02-07 17:47:41 -08001594 stat(c, ALLOC_SLAB);
Christoph Lameter05aa3452007-11-05 11:31:58 -08001595 if (c->page)
Christoph Lameterdfb4f092007-10-16 01:26:05 -07001596 flush_slab(s, c);
Christoph Lameterdfb4f092007-10-16 01:26:05 -07001597 slab_lock(new);
1598 SetSlabFrozen(new);
1599 c->page = new;
Christoph Lameter4b6f0752007-05-16 22:10:53 -07001600 goto load_freelist;
Christoph Lameter81819f02007-05-06 14:49:36 -07001601 }
Christoph Lameter71c7a062008-02-14 14:28:01 -08001602 return NULL;
Christoph Lameter81819f02007-05-06 14:49:36 -07001603debug:
Christoph Lameterdfb4f092007-10-16 01:26:05 -07001604 if (!alloc_debug_processing(s, c->page, object, addr))
Christoph Lameter81819f02007-05-06 14:49:36 -07001605 goto another_slab;
Christoph Lameter894b8782007-05-10 03:15:16 -07001606
Christoph Lameterdfb4f092007-10-16 01:26:05 -07001607 c->page->inuse++;
Christoph Lameterb3fba8d2007-10-16 01:26:06 -07001608 c->page->freelist = object[c->offset];
Christoph Lameteree3c72a2007-10-16 01:26:07 -07001609 c->node = -1;
Christoph Lameter1f842602008-01-07 23:20:30 -08001610 goto unlock_out;
Christoph Lameter894b8782007-05-10 03:15:16 -07001611}
1612
1613/*
1614 * Inlined fastpath so that allocation functions (kmalloc, kmem_cache_alloc)
1615 * have the fastpath folded into their functions. So no function call
1616 * overhead for requests that can be satisfied on the fastpath.
1617 *
1618 * The fastpath works by first checking if the lockless freelist can be used.
1619 * If not then __slab_alloc is called for slow processing.
1620 *
1621 * Otherwise we can simply pick the next object from the lockless free list.
1622 */
Pekka Enberg06428782008-01-07 23:20:27 -08001623static __always_inline void *slab_alloc(struct kmem_cache *s,
Christoph Lameterce15fea2007-07-17 04:03:28 -07001624 gfp_t gfpflags, int node, void *addr)
Christoph Lameter894b8782007-05-10 03:15:16 -07001625{
Christoph Lameter894b8782007-05-10 03:15:16 -07001626 void **object;
Christoph Lameterdfb4f092007-10-16 01:26:05 -07001627 struct kmem_cache_cpu *c;
Christoph Lameter1f842602008-01-07 23:20:30 -08001628 unsigned long flags;
1629
Christoph Lameter894b8782007-05-10 03:15:16 -07001630 local_irq_save(flags);
Christoph Lameterdfb4f092007-10-16 01:26:05 -07001631 c = get_cpu_slab(s, smp_processor_id());
Christoph Lametera973e9d2008-03-01 13:40:44 -08001632 if (unlikely(!c->freelist || !node_match(c, node)))
Christoph Lameter894b8782007-05-10 03:15:16 -07001633
Christoph Lameterdfb4f092007-10-16 01:26:05 -07001634 object = __slab_alloc(s, gfpflags, node, addr, c);
Christoph Lameter894b8782007-05-10 03:15:16 -07001635
1636 else {
Christoph Lameterdfb4f092007-10-16 01:26:05 -07001637 object = c->freelist;
Christoph Lameterb3fba8d2007-10-16 01:26:06 -07001638 c->freelist = object[c->offset];
Christoph Lameter8ff12cf2008-02-07 17:47:41 -08001639 stat(c, ALLOC_FASTPATH);
Christoph Lameter894b8782007-05-10 03:15:16 -07001640 }
1641 local_irq_restore(flags);
Christoph Lameterd07dbea2007-07-17 04:03:23 -07001642
1643 if (unlikely((gfpflags & __GFP_ZERO) && object))
Christoph Lameter42a9fdb2007-10-16 01:26:09 -07001644 memset(object, 0, c->objsize);
Christoph Lameterd07dbea2007-07-17 04:03:23 -07001645
Christoph Lameter894b8782007-05-10 03:15:16 -07001646 return object;
Christoph Lameter81819f02007-05-06 14:49:36 -07001647}
1648
1649void *kmem_cache_alloc(struct kmem_cache *s, gfp_t gfpflags)
1650{
Christoph Lameterce15fea2007-07-17 04:03:28 -07001651 return slab_alloc(s, gfpflags, -1, __builtin_return_address(0));
Christoph Lameter81819f02007-05-06 14:49:36 -07001652}
1653EXPORT_SYMBOL(kmem_cache_alloc);
1654
1655#ifdef CONFIG_NUMA
1656void *kmem_cache_alloc_node(struct kmem_cache *s, gfp_t gfpflags, int node)
1657{
Christoph Lameterce15fea2007-07-17 04:03:28 -07001658 return slab_alloc(s, gfpflags, node, __builtin_return_address(0));
Christoph Lameter81819f02007-05-06 14:49:36 -07001659}
1660EXPORT_SYMBOL(kmem_cache_alloc_node);
1661#endif
1662
1663/*
Christoph Lameter894b8782007-05-10 03:15:16 -07001664 * Slow patch handling. This may still be called frequently since objects
1665 * have a longer lifetime than the cpu slabs in most processing loads.
Christoph Lameter81819f02007-05-06 14:49:36 -07001666 *
Christoph Lameter894b8782007-05-10 03:15:16 -07001667 * So we still attempt to reduce cache line usage. Just take the slab
1668 * lock and free the item. If there is no additional partial page
1669 * handling required then we can return immediately.
Christoph Lameter81819f02007-05-06 14:49:36 -07001670 */
Christoph Lameter894b8782007-05-10 03:15:16 -07001671static void __slab_free(struct kmem_cache *s, struct page *page,
Christoph Lameterb3fba8d2007-10-16 01:26:06 -07001672 void *x, void *addr, unsigned int offset)
Christoph Lameter81819f02007-05-06 14:49:36 -07001673{
1674 void *prior;
1675 void **object = (void *)x;
Christoph Lameter8ff12cf2008-02-07 17:47:41 -08001676 struct kmem_cache_cpu *c;
Christoph Lameter81819f02007-05-06 14:49:36 -07001677
Christoph Lameter8ff12cf2008-02-07 17:47:41 -08001678 c = get_cpu_slab(s, raw_smp_processor_id());
1679 stat(c, FREE_SLOWPATH);
Christoph Lameter81819f02007-05-06 14:49:36 -07001680 slab_lock(page);
1681
Christoph Lameter35e5d7e2007-05-09 02:32:42 -07001682 if (unlikely(SlabDebug(page)))
Christoph Lameter81819f02007-05-06 14:49:36 -07001683 goto debug;
Christoph Lameter6446faa2008-02-15 23:45:26 -08001684
Christoph Lameter81819f02007-05-06 14:49:36 -07001685checks_ok:
Christoph Lameterb3fba8d2007-10-16 01:26:06 -07001686 prior = object[offset] = page->freelist;
Christoph Lameter81819f02007-05-06 14:49:36 -07001687 page->freelist = object;
1688 page->inuse--;
1689
Christoph Lameter8ff12cf2008-02-07 17:47:41 -08001690 if (unlikely(SlabFrozen(page))) {
1691 stat(c, FREE_FROZEN);
Christoph Lameter81819f02007-05-06 14:49:36 -07001692 goto out_unlock;
Christoph Lameter8ff12cf2008-02-07 17:47:41 -08001693 }
Christoph Lameter81819f02007-05-06 14:49:36 -07001694
1695 if (unlikely(!page->inuse))
1696 goto slab_empty;
1697
1698 /*
Christoph Lameter6446faa2008-02-15 23:45:26 -08001699 * Objects left in the slab. If it was not on the partial list before
Christoph Lameter81819f02007-05-06 14:49:36 -07001700 * then add it.
1701 */
Christoph Lametera973e9d2008-03-01 13:40:44 -08001702 if (unlikely(!prior)) {
Christoph Lameter7c2e1322008-01-07 23:20:27 -08001703 add_partial(get_node(s, page_to_nid(page)), page, 1);
Christoph Lameter8ff12cf2008-02-07 17:47:41 -08001704 stat(c, FREE_ADD_PARTIAL);
1705 }
Christoph Lameter81819f02007-05-06 14:49:36 -07001706
1707out_unlock:
1708 slab_unlock(page);
Christoph Lameter81819f02007-05-06 14:49:36 -07001709 return;
1710
1711slab_empty:
Christoph Lametera973e9d2008-03-01 13:40:44 -08001712 if (prior) {
Christoph Lameter81819f02007-05-06 14:49:36 -07001713 /*
Christoph Lameter672bba32007-05-09 02:32:39 -07001714 * Slab still on the partial list.
Christoph Lameter81819f02007-05-06 14:49:36 -07001715 */
1716 remove_partial(s, page);
Christoph Lameter8ff12cf2008-02-07 17:47:41 -08001717 stat(c, FREE_REMOVE_PARTIAL);
1718 }
Christoph Lameter81819f02007-05-06 14:49:36 -07001719 slab_unlock(page);
Christoph Lameter8ff12cf2008-02-07 17:47:41 -08001720 stat(c, FREE_SLAB);
Christoph Lameter81819f02007-05-06 14:49:36 -07001721 discard_slab(s, page);
Christoph Lameter81819f02007-05-06 14:49:36 -07001722 return;
1723
1724debug:
Christoph Lameter3ec09742007-05-16 22:11:00 -07001725 if (!free_debug_processing(s, page, x, addr))
Christoph Lameter77c5e2d2007-05-06 14:49:42 -07001726 goto out_unlock;
Christoph Lameter77c5e2d2007-05-06 14:49:42 -07001727 goto checks_ok;
Christoph Lameter81819f02007-05-06 14:49:36 -07001728}
1729
Christoph Lameter894b8782007-05-10 03:15:16 -07001730/*
1731 * Fastpath with forced inlining to produce a kfree and kmem_cache_free that
1732 * can perform fastpath freeing without additional function calls.
1733 *
1734 * The fastpath is only possible if we are freeing to the current cpu slab
1735 * of this processor. This typically the case if we have just allocated
1736 * the item before.
1737 *
1738 * If fastpath is not possible then fall back to __slab_free where we deal
1739 * with all sorts of special processing.
1740 */
Pekka Enberg06428782008-01-07 23:20:27 -08001741static __always_inline void slab_free(struct kmem_cache *s,
Christoph Lameter894b8782007-05-10 03:15:16 -07001742 struct page *page, void *x, void *addr)
1743{
1744 void **object = (void *)x;
Christoph Lameterdfb4f092007-10-16 01:26:05 -07001745 struct kmem_cache_cpu *c;
Christoph Lameter1f842602008-01-07 23:20:30 -08001746 unsigned long flags;
1747
Christoph Lameter894b8782007-05-10 03:15:16 -07001748 local_irq_save(flags);
Christoph Lameterdfb4f092007-10-16 01:26:05 -07001749 c = get_cpu_slab(s, smp_processor_id());
Christoph Lameter27d9e4e2008-02-15 23:45:25 -08001750 debug_check_no_locks_freed(object, c->objsize);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -07001751 if (!(s->flags & SLAB_DEBUG_OBJECTS))
1752 debug_check_no_obj_freed(object, s->objsize);
Christoph Lameteree3c72a2007-10-16 01:26:07 -07001753 if (likely(page == c->page && c->node >= 0)) {
Christoph Lameterb3fba8d2007-10-16 01:26:06 -07001754 object[c->offset] = c->freelist;
Christoph Lameterdfb4f092007-10-16 01:26:05 -07001755 c->freelist = object;
Christoph Lameter8ff12cf2008-02-07 17:47:41 -08001756 stat(c, FREE_FASTPATH);
Christoph Lameter894b8782007-05-10 03:15:16 -07001757 } else
Christoph Lameterb3fba8d2007-10-16 01:26:06 -07001758 __slab_free(s, page, x, addr, c->offset);
Christoph Lameter894b8782007-05-10 03:15:16 -07001759
1760 local_irq_restore(flags);
1761}
1762
Christoph Lameter81819f02007-05-06 14:49:36 -07001763void kmem_cache_free(struct kmem_cache *s, void *x)
1764{
Christoph Lameter77c5e2d2007-05-06 14:49:42 -07001765 struct page *page;
Christoph Lameter81819f02007-05-06 14:49:36 -07001766
Christoph Lameterb49af682007-05-06 14:49:41 -07001767 page = virt_to_head_page(x);
Christoph Lameter81819f02007-05-06 14:49:36 -07001768
Christoph Lameter77c5e2d2007-05-06 14:49:42 -07001769 slab_free(s, page, x, __builtin_return_address(0));
Christoph Lameter81819f02007-05-06 14:49:36 -07001770}
1771EXPORT_SYMBOL(kmem_cache_free);
1772
1773/* Figure out on which slab object the object resides */
1774static struct page *get_object_page(const void *x)
1775{
Christoph Lameterb49af682007-05-06 14:49:41 -07001776 struct page *page = virt_to_head_page(x);
Christoph Lameter81819f02007-05-06 14:49:36 -07001777
1778 if (!PageSlab(page))
1779 return NULL;
1780
1781 return page;
1782}
1783
1784/*
Christoph Lameter672bba32007-05-09 02:32:39 -07001785 * Object placement in a slab is made very easy because we always start at
1786 * offset 0. If we tune the size of the object to the alignment then we can
1787 * get the required alignment by putting one properly sized object after
1788 * another.
Christoph Lameter81819f02007-05-06 14:49:36 -07001789 *
1790 * Notice that the allocation order determines the sizes of the per cpu
1791 * caches. Each processor has always one slab available for allocations.
1792 * Increasing the allocation order reduces the number of times that slabs
Christoph Lameter672bba32007-05-09 02:32:39 -07001793 * must be moved on and off the partial lists and is therefore a factor in
Christoph Lameter81819f02007-05-06 14:49:36 -07001794 * locking overhead.
Christoph Lameter81819f02007-05-06 14:49:36 -07001795 */
1796
1797/*
1798 * Mininum / Maximum order of slab pages. This influences locking overhead
1799 * and slab fragmentation. A higher order reduces the number of partial slabs
1800 * and increases the number of allocations possible without having to
1801 * take the list_lock.
1802 */
1803static int slub_min_order;
Christoph Lameter114e9e82008-04-14 19:11:41 +03001804static int slub_max_order = PAGE_ALLOC_COSTLY_ORDER;
Christoph Lameter9b2cd502008-04-14 19:11:41 +03001805static int slub_min_objects;
Christoph Lameter81819f02007-05-06 14:49:36 -07001806
1807/*
1808 * Merge control. If this is set then no merging of slab caches will occur.
Christoph Lameter672bba32007-05-09 02:32:39 -07001809 * (Could be removed. This was introduced to pacify the merge skeptics.)
Christoph Lameter81819f02007-05-06 14:49:36 -07001810 */
1811static int slub_nomerge;
1812
1813/*
Christoph Lameter81819f02007-05-06 14:49:36 -07001814 * Calculate the order of allocation given an slab object size.
1815 *
Christoph Lameter672bba32007-05-09 02:32:39 -07001816 * The order of allocation has significant impact on performance and other
1817 * system components. Generally order 0 allocations should be preferred since
1818 * order 0 does not cause fragmentation in the page allocator. Larger objects
1819 * be problematic to put into order 0 slabs because there may be too much
Christoph Lameterc124f5b2008-04-14 19:13:29 +03001820 * unused space left. We go to a higher order if more than 1/16th of the slab
Christoph Lameter672bba32007-05-09 02:32:39 -07001821 * would be wasted.
Christoph Lameter81819f02007-05-06 14:49:36 -07001822 *
Christoph Lameter672bba32007-05-09 02:32:39 -07001823 * In order to reach satisfactory performance we must ensure that a minimum
1824 * number of objects is in one slab. Otherwise we may generate too much
1825 * activity on the partial lists which requires taking the list_lock. This is
1826 * less a concern for large slabs though which are rarely used.
Christoph Lameter81819f02007-05-06 14:49:36 -07001827 *
Christoph Lameter672bba32007-05-09 02:32:39 -07001828 * slub_max_order specifies the order where we begin to stop considering the
1829 * number of objects in a slab as critical. If we reach slub_max_order then
1830 * we try to keep the page order as low as possible. So we accept more waste
1831 * of space in favor of a small page order.
1832 *
1833 * Higher order allocations also allow the placement of more objects in a
1834 * slab and thereby reduce object handling overhead. If the user has
1835 * requested a higher mininum order then we start with that one instead of
1836 * the smallest order which will fit the object.
Christoph Lameter81819f02007-05-06 14:49:36 -07001837 */
Christoph Lameter5e6d4442007-05-09 02:32:46 -07001838static inline int slab_order(int size, int min_objects,
1839 int max_order, int fract_leftover)
Christoph Lameter81819f02007-05-06 14:49:36 -07001840{
1841 int order;
1842 int rem;
Christoph Lameter6300ea72007-07-17 04:03:20 -07001843 int min_order = slub_min_order;
Christoph Lameter81819f02007-05-06 14:49:36 -07001844
Christoph Lameter39b26462008-04-14 19:11:30 +03001845 if ((PAGE_SIZE << min_order) / size > 65535)
1846 return get_order(size * 65535) - 1;
1847
Christoph Lameter6300ea72007-07-17 04:03:20 -07001848 for (order = max(min_order,
Christoph Lameter5e6d4442007-05-09 02:32:46 -07001849 fls(min_objects * size - 1) - PAGE_SHIFT);
1850 order <= max_order; order++) {
1851
Christoph Lameter81819f02007-05-06 14:49:36 -07001852 unsigned long slab_size = PAGE_SIZE << order;
1853
Christoph Lameter5e6d4442007-05-09 02:32:46 -07001854 if (slab_size < min_objects * size)
Christoph Lameter81819f02007-05-06 14:49:36 -07001855 continue;
1856
Christoph Lameter81819f02007-05-06 14:49:36 -07001857 rem = slab_size % size;
1858
Christoph Lameter5e6d4442007-05-09 02:32:46 -07001859 if (rem <= slab_size / fract_leftover)
Christoph Lameter81819f02007-05-06 14:49:36 -07001860 break;
1861
1862 }
Christoph Lameter672bba32007-05-09 02:32:39 -07001863
Christoph Lameter81819f02007-05-06 14:49:36 -07001864 return order;
1865}
1866
Christoph Lameter5e6d4442007-05-09 02:32:46 -07001867static inline int calculate_order(int size)
1868{
1869 int order;
1870 int min_objects;
1871 int fraction;
1872
1873 /*
1874 * Attempt to find best configuration for a slab. This
1875 * works by first attempting to generate a layout with
1876 * the best configuration and backing off gradually.
1877 *
1878 * First we reduce the acceptable waste in a slab. Then
1879 * we reduce the minimum objects required in a slab.
1880 */
1881 min_objects = slub_min_objects;
Christoph Lameter9b2cd502008-04-14 19:11:41 +03001882 if (!min_objects)
1883 min_objects = 4 * (fls(nr_cpu_ids) + 1);
Christoph Lameter5e6d4442007-05-09 02:32:46 -07001884 while (min_objects > 1) {
Christoph Lameterc124f5b2008-04-14 19:13:29 +03001885 fraction = 16;
Christoph Lameter5e6d4442007-05-09 02:32:46 -07001886 while (fraction >= 4) {
1887 order = slab_order(size, min_objects,
1888 slub_max_order, fraction);
1889 if (order <= slub_max_order)
1890 return order;
1891 fraction /= 2;
1892 }
1893 min_objects /= 2;
1894 }
1895
1896 /*
1897 * We were unable to place multiple objects in a slab. Now
1898 * lets see if we can place a single object there.
1899 */
1900 order = slab_order(size, 1, slub_max_order, 1);
1901 if (order <= slub_max_order)
1902 return order;
1903
1904 /*
1905 * Doh this slab cannot be placed using slub_max_order.
1906 */
1907 order = slab_order(size, 1, MAX_ORDER, 1);
1908 if (order <= MAX_ORDER)
1909 return order;
1910 return -ENOSYS;
1911}
1912
Christoph Lameter81819f02007-05-06 14:49:36 -07001913/*
Christoph Lameter672bba32007-05-09 02:32:39 -07001914 * Figure out what the alignment of the objects will be.
Christoph Lameter81819f02007-05-06 14:49:36 -07001915 */
1916static unsigned long calculate_alignment(unsigned long flags,
1917 unsigned long align, unsigned long size)
1918{
1919 /*
Christoph Lameter6446faa2008-02-15 23:45:26 -08001920 * If the user wants hardware cache aligned objects then follow that
1921 * suggestion if the object is sufficiently large.
Christoph Lameter81819f02007-05-06 14:49:36 -07001922 *
Christoph Lameter6446faa2008-02-15 23:45:26 -08001923 * The hardware cache alignment cannot override the specified
1924 * alignment though. If that is greater then use it.
Christoph Lameter81819f02007-05-06 14:49:36 -07001925 */
Nick Pigginb6210382008-03-05 14:05:56 -08001926 if (flags & SLAB_HWCACHE_ALIGN) {
1927 unsigned long ralign = cache_line_size();
1928 while (size <= ralign / 2)
1929 ralign /= 2;
1930 align = max(align, ralign);
1931 }
Christoph Lameter81819f02007-05-06 14:49:36 -07001932
1933 if (align < ARCH_SLAB_MINALIGN)
Nick Pigginb6210382008-03-05 14:05:56 -08001934 align = ARCH_SLAB_MINALIGN;
Christoph Lameter81819f02007-05-06 14:49:36 -07001935
1936 return ALIGN(align, sizeof(void *));
1937}
1938
Christoph Lameterdfb4f092007-10-16 01:26:05 -07001939static void init_kmem_cache_cpu(struct kmem_cache *s,
1940 struct kmem_cache_cpu *c)
1941{
1942 c->page = NULL;
Christoph Lametera973e9d2008-03-01 13:40:44 -08001943 c->freelist = NULL;
Christoph Lameterdfb4f092007-10-16 01:26:05 -07001944 c->node = 0;
Christoph Lameter42a9fdb2007-10-16 01:26:09 -07001945 c->offset = s->offset / sizeof(void *);
1946 c->objsize = s->objsize;
Pekka Enberg62f75532008-04-14 18:50:44 +03001947#ifdef CONFIG_SLUB_STATS
1948 memset(c->stat, 0, NR_SLUB_STAT_ITEMS * sizeof(unsigned));
1949#endif
Christoph Lameterdfb4f092007-10-16 01:26:05 -07001950}
1951
Christoph Lameter81819f02007-05-06 14:49:36 -07001952static void init_kmem_cache_node(struct kmem_cache_node *n)
1953{
1954 n->nr_partial = 0;
Christoph Lameter81819f02007-05-06 14:49:36 -07001955 spin_lock_init(&n->list_lock);
1956 INIT_LIST_HEAD(&n->partial);
Christoph Lameter8ab13722007-07-17 04:03:32 -07001957#ifdef CONFIG_SLUB_DEBUG
Christoph Lameter0f389ec2008-04-14 18:53:02 +03001958 atomic_long_set(&n->nr_slabs, 0);
Christoph Lameter643b1132007-05-06 14:49:42 -07001959 INIT_LIST_HEAD(&n->full);
Christoph Lameter8ab13722007-07-17 04:03:32 -07001960#endif
Christoph Lameter81819f02007-05-06 14:49:36 -07001961}
1962
Christoph Lameter4c93c3552007-10-16 01:26:08 -07001963#ifdef CONFIG_SMP
1964/*
1965 * Per cpu array for per cpu structures.
1966 *
1967 * The per cpu array places all kmem_cache_cpu structures from one processor
1968 * close together meaning that it becomes possible that multiple per cpu
1969 * structures are contained in one cacheline. This may be particularly
1970 * beneficial for the kmalloc caches.
1971 *
1972 * A desktop system typically has around 60-80 slabs. With 100 here we are
1973 * likely able to get per cpu structures for all caches from the array defined
1974 * here. We must be able to cover all kmalloc caches during bootstrap.
1975 *
1976 * If the per cpu array is exhausted then fall back to kmalloc
1977 * of individual cachelines. No sharing is possible then.
1978 */
1979#define NR_KMEM_CACHE_CPU 100
1980
1981static DEFINE_PER_CPU(struct kmem_cache_cpu,
1982 kmem_cache_cpu)[NR_KMEM_CACHE_CPU];
1983
1984static DEFINE_PER_CPU(struct kmem_cache_cpu *, kmem_cache_cpu_free);
1985static cpumask_t kmem_cach_cpu_free_init_once = CPU_MASK_NONE;
1986
1987static struct kmem_cache_cpu *alloc_kmem_cache_cpu(struct kmem_cache *s,
1988 int cpu, gfp_t flags)
1989{
1990 struct kmem_cache_cpu *c = per_cpu(kmem_cache_cpu_free, cpu);
1991
1992 if (c)
1993 per_cpu(kmem_cache_cpu_free, cpu) =
1994 (void *)c->freelist;
1995 else {
1996 /* Table overflow: So allocate ourselves */
1997 c = kmalloc_node(
1998 ALIGN(sizeof(struct kmem_cache_cpu), cache_line_size()),
1999 flags, cpu_to_node(cpu));
2000 if (!c)
2001 return NULL;
2002 }
2003
2004 init_kmem_cache_cpu(s, c);
2005 return c;
2006}
2007
2008static void free_kmem_cache_cpu(struct kmem_cache_cpu *c, int cpu)
2009{
2010 if (c < per_cpu(kmem_cache_cpu, cpu) ||
2011 c > per_cpu(kmem_cache_cpu, cpu) + NR_KMEM_CACHE_CPU) {
2012 kfree(c);
2013 return;
2014 }
2015 c->freelist = (void *)per_cpu(kmem_cache_cpu_free, cpu);
2016 per_cpu(kmem_cache_cpu_free, cpu) = c;
2017}
2018
2019static void free_kmem_cache_cpus(struct kmem_cache *s)
2020{
2021 int cpu;
2022
2023 for_each_online_cpu(cpu) {
2024 struct kmem_cache_cpu *c = get_cpu_slab(s, cpu);
2025
2026 if (c) {
2027 s->cpu_slab[cpu] = NULL;
2028 free_kmem_cache_cpu(c, cpu);
2029 }
2030 }
2031}
2032
2033static int alloc_kmem_cache_cpus(struct kmem_cache *s, gfp_t flags)
2034{
2035 int cpu;
2036
2037 for_each_online_cpu(cpu) {
2038 struct kmem_cache_cpu *c = get_cpu_slab(s, cpu);
2039
2040 if (c)
2041 continue;
2042
2043 c = alloc_kmem_cache_cpu(s, cpu, flags);
2044 if (!c) {
2045 free_kmem_cache_cpus(s);
2046 return 0;
2047 }
2048 s->cpu_slab[cpu] = c;
2049 }
2050 return 1;
2051}
2052
2053/*
2054 * Initialize the per cpu array.
2055 */
2056static void init_alloc_cpu_cpu(int cpu)
2057{
2058 int i;
2059
2060 if (cpu_isset(cpu, kmem_cach_cpu_free_init_once))
2061 return;
2062
2063 for (i = NR_KMEM_CACHE_CPU - 1; i >= 0; i--)
2064 free_kmem_cache_cpu(&per_cpu(kmem_cache_cpu, cpu)[i], cpu);
2065
2066 cpu_set(cpu, kmem_cach_cpu_free_init_once);
2067}
2068
2069static void __init init_alloc_cpu(void)
2070{
2071 int cpu;
2072
2073 for_each_online_cpu(cpu)
2074 init_alloc_cpu_cpu(cpu);
2075 }
2076
2077#else
2078static inline void free_kmem_cache_cpus(struct kmem_cache *s) {}
2079static inline void init_alloc_cpu(void) {}
2080
2081static inline int alloc_kmem_cache_cpus(struct kmem_cache *s, gfp_t flags)
2082{
2083 init_kmem_cache_cpu(s, &s->cpu_slab);
2084 return 1;
2085}
2086#endif
2087
Christoph Lameter81819f02007-05-06 14:49:36 -07002088#ifdef CONFIG_NUMA
2089/*
2090 * No kmalloc_node yet so do it by hand. We know that this is the first
2091 * slab on the node for this slabcache. There are no concurrent accesses
2092 * possible.
2093 *
2094 * Note that this function only works on the kmalloc_node_cache
Christoph Lameter4c93c3552007-10-16 01:26:08 -07002095 * when allocating for the kmalloc_node_cache. This is used for bootstrapping
2096 * memory on a fresh node that has no slab structures yet.
Christoph Lameter81819f02007-05-06 14:49:36 -07002097 */
Adrian Bunk1cd7daa2007-10-16 01:24:18 -07002098static struct kmem_cache_node *early_kmem_cache_node_alloc(gfp_t gfpflags,
2099 int node)
Christoph Lameter81819f02007-05-06 14:49:36 -07002100{
2101 struct page *page;
2102 struct kmem_cache_node *n;
rootba84c732008-01-07 23:20:28 -08002103 unsigned long flags;
Christoph Lameter81819f02007-05-06 14:49:36 -07002104
2105 BUG_ON(kmalloc_caches->size < sizeof(struct kmem_cache_node));
2106
Christoph Lametera2f92ee2007-08-22 14:01:57 -07002107 page = new_slab(kmalloc_caches, gfpflags, node);
Christoph Lameter81819f02007-05-06 14:49:36 -07002108
2109 BUG_ON(!page);
Christoph Lametera2f92ee2007-08-22 14:01:57 -07002110 if (page_to_nid(page) != node) {
2111 printk(KERN_ERR "SLUB: Unable to allocate memory from "
2112 "node %d\n", node);
2113 printk(KERN_ERR "SLUB: Allocating a useless per node structure "
2114 "in order to be able to continue\n");
2115 }
2116
Christoph Lameter81819f02007-05-06 14:49:36 -07002117 n = page->freelist;
2118 BUG_ON(!n);
2119 page->freelist = get_freepointer(kmalloc_caches, n);
2120 page->inuse++;
2121 kmalloc_caches->node[node] = n;
Christoph Lameter8ab13722007-07-17 04:03:32 -07002122#ifdef CONFIG_SLUB_DEBUG
Christoph Lameterd45f39c2007-07-17 04:03:21 -07002123 init_object(kmalloc_caches, n, 1);
2124 init_tracking(kmalloc_caches, n);
Christoph Lameter8ab13722007-07-17 04:03:32 -07002125#endif
Christoph Lameter81819f02007-05-06 14:49:36 -07002126 init_kmem_cache_node(n);
Christoph Lameter205ab992008-04-14 19:11:40 +03002127 inc_slabs_node(kmalloc_caches, node, page->objects);
Christoph Lameter6446faa2008-02-15 23:45:26 -08002128
rootba84c732008-01-07 23:20:28 -08002129 /*
2130 * lockdep requires consistent irq usage for each lock
2131 * so even though there cannot be a race this early in
2132 * the boot sequence, we still disable irqs.
2133 */
2134 local_irq_save(flags);
Christoph Lameter7c2e1322008-01-07 23:20:27 -08002135 add_partial(n, page, 0);
rootba84c732008-01-07 23:20:28 -08002136 local_irq_restore(flags);
Christoph Lameter81819f02007-05-06 14:49:36 -07002137 return n;
2138}
2139
2140static void free_kmem_cache_nodes(struct kmem_cache *s)
2141{
2142 int node;
2143
Christoph Lameterf64dc582007-10-16 01:25:33 -07002144 for_each_node_state(node, N_NORMAL_MEMORY) {
Christoph Lameter81819f02007-05-06 14:49:36 -07002145 struct kmem_cache_node *n = s->node[node];
2146 if (n && n != &s->local_node)
2147 kmem_cache_free(kmalloc_caches, n);
2148 s->node[node] = NULL;
2149 }
2150}
2151
2152static int init_kmem_cache_nodes(struct kmem_cache *s, gfp_t gfpflags)
2153{
2154 int node;
2155 int local_node;
2156
2157 if (slab_state >= UP)
2158 local_node = page_to_nid(virt_to_page(s));
2159 else
2160 local_node = 0;
2161
Christoph Lameterf64dc582007-10-16 01:25:33 -07002162 for_each_node_state(node, N_NORMAL_MEMORY) {
Christoph Lameter81819f02007-05-06 14:49:36 -07002163 struct kmem_cache_node *n;
2164
2165 if (local_node == node)
2166 n = &s->local_node;
2167 else {
2168 if (slab_state == DOWN) {
2169 n = early_kmem_cache_node_alloc(gfpflags,
2170 node);
2171 continue;
2172 }
2173 n = kmem_cache_alloc_node(kmalloc_caches,
2174 gfpflags, node);
2175
2176 if (!n) {
2177 free_kmem_cache_nodes(s);
2178 return 0;
2179 }
2180
2181 }
2182 s->node[node] = n;
2183 init_kmem_cache_node(n);
2184 }
2185 return 1;
2186}
2187#else
2188static void free_kmem_cache_nodes(struct kmem_cache *s)
2189{
2190}
2191
2192static int init_kmem_cache_nodes(struct kmem_cache *s, gfp_t gfpflags)
2193{
2194 init_kmem_cache_node(&s->local_node);
2195 return 1;
2196}
2197#endif
2198
2199/*
2200 * calculate_sizes() determines the order and the distribution of data within
2201 * a slab object.
2202 */
Christoph Lameter06b285d2008-04-14 19:11:41 +03002203static int calculate_sizes(struct kmem_cache *s, int forced_order)
Christoph Lameter81819f02007-05-06 14:49:36 -07002204{
2205 unsigned long flags = s->flags;
2206 unsigned long size = s->objsize;
2207 unsigned long align = s->align;
Christoph Lameter834f3d12008-04-14 19:11:31 +03002208 int order;
Christoph Lameter81819f02007-05-06 14:49:36 -07002209
2210 /*
Christoph Lameterd8b42bf2008-02-15 23:45:25 -08002211 * Round up object size to the next word boundary. We can only
2212 * place the free pointer at word boundaries and this determines
2213 * the possible location of the free pointer.
2214 */
2215 size = ALIGN(size, sizeof(void *));
2216
2217#ifdef CONFIG_SLUB_DEBUG
2218 /*
Christoph Lameter81819f02007-05-06 14:49:36 -07002219 * Determine if we can poison the object itself. If the user of
2220 * the slab may touch the object after free or before allocation
2221 * then we should never poison the object itself.
2222 */
2223 if ((flags & SLAB_POISON) && !(flags & SLAB_DESTROY_BY_RCU) &&
Christoph Lameterc59def9f2007-05-16 22:10:50 -07002224 !s->ctor)
Christoph Lameter81819f02007-05-06 14:49:36 -07002225 s->flags |= __OBJECT_POISON;
2226 else
2227 s->flags &= ~__OBJECT_POISON;
2228
Christoph Lameter81819f02007-05-06 14:49:36 -07002229
2230 /*
Christoph Lameter672bba32007-05-09 02:32:39 -07002231 * If we are Redzoning then check if there is some space between the
Christoph Lameter81819f02007-05-06 14:49:36 -07002232 * end of the object and the free pointer. If not then add an
Christoph Lameter672bba32007-05-09 02:32:39 -07002233 * additional word to have some bytes to store Redzone information.
Christoph Lameter81819f02007-05-06 14:49:36 -07002234 */
2235 if ((flags & SLAB_RED_ZONE) && size == s->objsize)
2236 size += sizeof(void *);
Christoph Lameter41ecc552007-05-09 02:32:44 -07002237#endif
Christoph Lameter81819f02007-05-06 14:49:36 -07002238
2239 /*
Christoph Lameter672bba32007-05-09 02:32:39 -07002240 * With that we have determined the number of bytes in actual use
2241 * by the object. This is the potential offset to the free pointer.
Christoph Lameter81819f02007-05-06 14:49:36 -07002242 */
2243 s->inuse = size;
2244
2245 if (((flags & (SLAB_DESTROY_BY_RCU | SLAB_POISON)) ||
Christoph Lameterc59def9f2007-05-16 22:10:50 -07002246 s->ctor)) {
Christoph Lameter81819f02007-05-06 14:49:36 -07002247 /*
2248 * Relocate free pointer after the object if it is not
2249 * permitted to overwrite the first word of the object on
2250 * kmem_cache_free.
2251 *
2252 * This is the case if we do RCU, have a constructor or
2253 * destructor or are poisoning the objects.
2254 */
2255 s->offset = size;
2256 size += sizeof(void *);
2257 }
2258
Christoph Lameterc12b3c62007-05-23 13:57:31 -07002259#ifdef CONFIG_SLUB_DEBUG
Christoph Lameter81819f02007-05-06 14:49:36 -07002260 if (flags & SLAB_STORE_USER)
2261 /*
2262 * Need to store information about allocs and frees after
2263 * the object.
2264 */
2265 size += 2 * sizeof(struct track);
2266
Christoph Lameterbe7b3fb2007-05-09 02:32:36 -07002267 if (flags & SLAB_RED_ZONE)
Christoph Lameter81819f02007-05-06 14:49:36 -07002268 /*
2269 * Add some empty padding so that we can catch
2270 * overwrites from earlier objects rather than let
2271 * tracking information or the free pointer be
2272 * corrupted if an user writes before the start
2273 * of the object.
2274 */
2275 size += sizeof(void *);
Christoph Lameter41ecc552007-05-09 02:32:44 -07002276#endif
Christoph Lameter672bba32007-05-09 02:32:39 -07002277
Christoph Lameter81819f02007-05-06 14:49:36 -07002278 /*
2279 * Determine the alignment based on various parameters that the
Christoph Lameter65c02d42007-05-09 02:32:35 -07002280 * user specified and the dynamic determination of cache line size
2281 * on bootup.
Christoph Lameter81819f02007-05-06 14:49:36 -07002282 */
2283 align = calculate_alignment(flags, align, s->objsize);
2284
2285 /*
2286 * SLUB stores one object immediately after another beginning from
2287 * offset 0. In order to align the objects we have to simply size
2288 * each object to conform to the alignment.
2289 */
2290 size = ALIGN(size, align);
2291 s->size = size;
Christoph Lameter06b285d2008-04-14 19:11:41 +03002292 if (forced_order >= 0)
2293 order = forced_order;
2294 else
2295 order = calculate_order(size);
Christoph Lameter81819f02007-05-06 14:49:36 -07002296
Christoph Lameter834f3d12008-04-14 19:11:31 +03002297 if (order < 0)
Christoph Lameter81819f02007-05-06 14:49:36 -07002298 return 0;
2299
Christoph Lameterb7a49f02008-02-14 14:21:32 -08002300 s->allocflags = 0;
Christoph Lameter834f3d12008-04-14 19:11:31 +03002301 if (order)
Christoph Lameterb7a49f02008-02-14 14:21:32 -08002302 s->allocflags |= __GFP_COMP;
2303
2304 if (s->flags & SLAB_CACHE_DMA)
2305 s->allocflags |= SLUB_DMA;
2306
2307 if (s->flags & SLAB_RECLAIM_ACCOUNT)
2308 s->allocflags |= __GFP_RECLAIMABLE;
2309
Christoph Lameter81819f02007-05-06 14:49:36 -07002310 /*
2311 * Determine the number of objects per slab
2312 */
Christoph Lameter834f3d12008-04-14 19:11:31 +03002313 s->oo = oo_make(order, size);
Christoph Lameter65c33762008-04-14 19:11:40 +03002314 s->min = oo_make(get_order(size), size);
Christoph Lameter205ab992008-04-14 19:11:40 +03002315 if (oo_objects(s->oo) > oo_objects(s->max))
2316 s->max = s->oo;
Christoph Lameter81819f02007-05-06 14:49:36 -07002317
Christoph Lameter834f3d12008-04-14 19:11:31 +03002318 return !!oo_objects(s->oo);
Christoph Lameter81819f02007-05-06 14:49:36 -07002319
2320}
2321
Christoph Lameter81819f02007-05-06 14:49:36 -07002322static int kmem_cache_open(struct kmem_cache *s, gfp_t gfpflags,
2323 const char *name, size_t size,
2324 size_t align, unsigned long flags,
Christoph Lameter4ba9b9d2007-10-16 23:25:51 -07002325 void (*ctor)(struct kmem_cache *, void *))
Christoph Lameter81819f02007-05-06 14:49:36 -07002326{
2327 memset(s, 0, kmem_size);
2328 s->name = name;
2329 s->ctor = ctor;
Christoph Lameter81819f02007-05-06 14:49:36 -07002330 s->objsize = size;
Christoph Lameter81819f02007-05-06 14:49:36 -07002331 s->align = align;
Christoph Lameterba0268a2007-09-11 15:24:11 -07002332 s->flags = kmem_cache_flags(size, flags, name, ctor);
Christoph Lameter81819f02007-05-06 14:49:36 -07002333
Christoph Lameter06b285d2008-04-14 19:11:41 +03002334 if (!calculate_sizes(s, -1))
Christoph Lameter81819f02007-05-06 14:49:36 -07002335 goto error;
2336
2337 s->refcount = 1;
2338#ifdef CONFIG_NUMA
Christoph Lameter98246012008-01-07 23:20:26 -08002339 s->remote_node_defrag_ratio = 100;
Christoph Lameter81819f02007-05-06 14:49:36 -07002340#endif
Christoph Lameterdfb4f092007-10-16 01:26:05 -07002341 if (!init_kmem_cache_nodes(s, gfpflags & ~SLUB_DMA))
2342 goto error;
Christoph Lameter81819f02007-05-06 14:49:36 -07002343
Christoph Lameterdfb4f092007-10-16 01:26:05 -07002344 if (alloc_kmem_cache_cpus(s, gfpflags & ~SLUB_DMA))
Christoph Lameter81819f02007-05-06 14:49:36 -07002345 return 1;
Christoph Lameter4c93c3552007-10-16 01:26:08 -07002346 free_kmem_cache_nodes(s);
Christoph Lameter81819f02007-05-06 14:49:36 -07002347error:
2348 if (flags & SLAB_PANIC)
2349 panic("Cannot create slab %s size=%lu realsize=%u "
2350 "order=%u offset=%u flags=%lx\n",
Christoph Lameter834f3d12008-04-14 19:11:31 +03002351 s->name, (unsigned long)size, s->size, oo_order(s->oo),
Christoph Lameter81819f02007-05-06 14:49:36 -07002352 s->offset, flags);
2353 return 0;
2354}
Christoph Lameter81819f02007-05-06 14:49:36 -07002355
2356/*
2357 * Check if a given pointer is valid
2358 */
2359int kmem_ptr_validate(struct kmem_cache *s, const void *object)
2360{
Pekka Enberg06428782008-01-07 23:20:27 -08002361 struct page *page;
Christoph Lameter81819f02007-05-06 14:49:36 -07002362
2363 page = get_object_page(object);
2364
2365 if (!page || s != page->slab)
2366 /* No slab or wrong slab */
2367 return 0;
2368
Christoph Lameterabcd08a2007-05-09 02:32:37 -07002369 if (!check_valid_pointer(s, page, object))
Christoph Lameter81819f02007-05-06 14:49:36 -07002370 return 0;
2371
2372 /*
2373 * We could also check if the object is on the slabs freelist.
2374 * But this would be too expensive and it seems that the main
Christoph Lameter6446faa2008-02-15 23:45:26 -08002375 * purpose of kmem_ptr_valid() is to check if the object belongs
Christoph Lameter81819f02007-05-06 14:49:36 -07002376 * to a certain slab.
2377 */
2378 return 1;
2379}
2380EXPORT_SYMBOL(kmem_ptr_validate);
2381
2382/*
2383 * Determine the size of a slab object
2384 */
2385unsigned int kmem_cache_size(struct kmem_cache *s)
2386{
2387 return s->objsize;
2388}
2389EXPORT_SYMBOL(kmem_cache_size);
2390
2391const char *kmem_cache_name(struct kmem_cache *s)
2392{
2393 return s->name;
2394}
2395EXPORT_SYMBOL(kmem_cache_name);
2396
Christoph Lameter33b12c32008-04-25 12:22:43 -07002397static void list_slab_objects(struct kmem_cache *s, struct page *page,
2398 const char *text)
Christoph Lameter81819f02007-05-06 14:49:36 -07002399{
Christoph Lameter33b12c32008-04-25 12:22:43 -07002400#ifdef CONFIG_SLUB_DEBUG
2401 void *addr = page_address(page);
2402 void *p;
2403 DECLARE_BITMAP(map, page->objects);
2404
2405 bitmap_zero(map, page->objects);
2406 slab_err(s, page, "%s", text);
2407 slab_lock(page);
2408 for_each_free_object(p, s, page->freelist)
2409 set_bit(slab_index(p, s, addr), map);
2410
2411 for_each_object(p, s, addr, page->objects) {
2412
2413 if (!test_bit(slab_index(p, s, addr), map)) {
2414 printk(KERN_ERR "INFO: Object 0x%p @offset=%tu\n",
2415 p, p - addr);
2416 print_tracking(s, p);
2417 }
2418 }
2419 slab_unlock(page);
2420#endif
2421}
2422
Christoph Lameter81819f02007-05-06 14:49:36 -07002423/*
Christoph Lameter599870b2008-04-23 12:36:52 -07002424 * Attempt to free all partial slabs on a node.
Christoph Lameter81819f02007-05-06 14:49:36 -07002425 */
Christoph Lameter599870b2008-04-23 12:36:52 -07002426static void free_partial(struct kmem_cache *s, struct kmem_cache_node *n)
Christoph Lameter81819f02007-05-06 14:49:36 -07002427{
Christoph Lameter81819f02007-05-06 14:49:36 -07002428 unsigned long flags;
2429 struct page *page, *h;
2430
2431 spin_lock_irqsave(&n->list_lock, flags);
Christoph Lameter33b12c32008-04-25 12:22:43 -07002432 list_for_each_entry_safe(page, h, &n->partial, lru) {
Christoph Lameter81819f02007-05-06 14:49:36 -07002433 if (!page->inuse) {
2434 list_del(&page->lru);
2435 discard_slab(s, page);
Christoph Lameter599870b2008-04-23 12:36:52 -07002436 n->nr_partial--;
Christoph Lameter33b12c32008-04-25 12:22:43 -07002437 } else {
2438 list_slab_objects(s, page,
2439 "Objects remaining on kmem_cache_close()");
Christoph Lameter599870b2008-04-23 12:36:52 -07002440 }
Christoph Lameter33b12c32008-04-25 12:22:43 -07002441 }
Christoph Lameter81819f02007-05-06 14:49:36 -07002442 spin_unlock_irqrestore(&n->list_lock, flags);
Christoph Lameter81819f02007-05-06 14:49:36 -07002443}
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 Lameter599870b2008-04-23 12:36:52 -07002459 free_partial(s, n);
2460 if (n->nr_partial || slabs_node(s, node))
Christoph Lameter81819f02007-05-06 14:49:36 -07002461 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);
Pekka Enbergd629d812008-04-23 22:31:08 +03002478 if (kmem_cache_close(s)) {
2479 printk(KERN_ERR "SLUB %s: %s called for cache that "
2480 "still has objects.\n", s->name, __func__);
2481 dump_stack();
2482 }
Christoph Lameter81819f02007-05-06 14:49:36 -07002483 sysfs_slab_remove(s);
Christoph Lametera0e1d1b2007-07-17 04:03:31 -07002484 } else
2485 up_write(&slub_lock);
Christoph Lameter81819f02007-05-06 14:49:36 -07002486}
2487EXPORT_SYMBOL(kmem_cache_destroy);
2488
2489/********************************************************************
2490 * Kmalloc subsystem
2491 *******************************************************************/
2492
Christoph Lameter331dc552008-02-14 14:28:09 -08002493struct kmem_cache kmalloc_caches[PAGE_SHIFT + 1] __cacheline_aligned;
Christoph Lameter81819f02007-05-06 14:49:36 -07002494EXPORT_SYMBOL(kmalloc_caches);
2495
Christoph Lameter81819f02007-05-06 14:49:36 -07002496static int __init setup_slub_min_order(char *str)
2497{
Pekka Enberg06428782008-01-07 23:20:27 -08002498 get_option(&str, &slub_min_order);
Christoph Lameter81819f02007-05-06 14:49:36 -07002499
2500 return 1;
2501}
2502
2503__setup("slub_min_order=", setup_slub_min_order);
2504
2505static int __init setup_slub_max_order(char *str)
2506{
Pekka Enberg06428782008-01-07 23:20:27 -08002507 get_option(&str, &slub_max_order);
Christoph Lameter81819f02007-05-06 14:49:36 -07002508
2509 return 1;
2510}
2511
2512__setup("slub_max_order=", setup_slub_max_order);
2513
2514static int __init setup_slub_min_objects(char *str)
2515{
Pekka Enberg06428782008-01-07 23:20:27 -08002516 get_option(&str, &slub_min_objects);
Christoph Lameter81819f02007-05-06 14:49:36 -07002517
2518 return 1;
2519}
2520
2521__setup("slub_min_objects=", setup_slub_min_objects);
2522
2523static int __init setup_slub_nomerge(char *str)
2524{
2525 slub_nomerge = 1;
2526 return 1;
2527}
2528
2529__setup("slub_nomerge", setup_slub_nomerge);
2530
Christoph Lameter81819f02007-05-06 14:49:36 -07002531static struct kmem_cache *create_kmalloc_cache(struct kmem_cache *s,
2532 const char *name, int size, gfp_t gfp_flags)
2533{
2534 unsigned int flags = 0;
2535
2536 if (gfp_flags & SLUB_DMA)
2537 flags = SLAB_CACHE_DMA;
2538
2539 down_write(&slub_lock);
2540 if (!kmem_cache_open(s, gfp_flags, name, size, ARCH_KMALLOC_MINALIGN,
Christoph Lameter319d1e22008-04-14 19:11:41 +03002541 flags, NULL))
Christoph Lameter81819f02007-05-06 14:49:36 -07002542 goto panic;
2543
2544 list_add(&s->list, &slab_caches);
2545 up_write(&slub_lock);
2546 if (sysfs_slab_add(s))
2547 goto panic;
2548 return s;
2549
2550panic:
2551 panic("Creation of kmalloc slab %s size=%d failed.\n", name, size);
2552}
2553
Christoph Lameter2e443fd2007-07-17 04:03:24 -07002554#ifdef CONFIG_ZONE_DMA
Christoph Lameter4097d602008-04-14 18:51:18 +03002555static struct kmem_cache *kmalloc_caches_dma[PAGE_SHIFT + 1];
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 Lameter331dc552008-02-14 14:28:09 -08002676 if (unlikely(size > PAGE_SIZE))
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
Christoph Lameterf619cfe2008-03-01 13:56:40 -08002688static void *kmalloc_large_node(size_t size, gfp_t flags, int node)
2689{
2690 struct page *page = alloc_pages_node(node, flags | __GFP_COMP,
2691 get_order(size));
2692
2693 if (page)
2694 return page_address(page);
2695 else
2696 return NULL;
2697}
2698
Christoph Lameter81819f02007-05-06 14:49:36 -07002699#ifdef CONFIG_NUMA
2700void *__kmalloc_node(size_t size, gfp_t flags, int node)
2701{
Christoph Lameteraadb4bc2007-10-16 01:24:38 -07002702 struct kmem_cache *s;
Christoph Lameter81819f02007-05-06 14:49:36 -07002703
Christoph Lameter331dc552008-02-14 14:28:09 -08002704 if (unlikely(size > PAGE_SIZE))
Christoph Lameterf619cfe2008-03-01 13:56:40 -08002705 return kmalloc_large_node(size, flags, node);
Christoph Lameteraadb4bc2007-10-16 01:24:38 -07002706
2707 s = get_slab(size, flags);
2708
2709 if (unlikely(ZERO_OR_NULL_PTR(s)))
Christoph Lameter6cb8f912007-07-17 04:03:22 -07002710 return s;
2711
Christoph Lameterce15fea2007-07-17 04:03:28 -07002712 return slab_alloc(s, flags, node, __builtin_return_address(0));
Christoph Lameter81819f02007-05-06 14:49:36 -07002713}
2714EXPORT_SYMBOL(__kmalloc_node);
2715#endif
2716
2717size_t ksize(const void *object)
2718{
Christoph Lameter272c1d22007-06-08 13:46:49 -07002719 struct page *page;
Christoph Lameter81819f02007-05-06 14:49:36 -07002720 struct kmem_cache *s;
2721
Christoph Lameteref8b4522007-10-16 01:24:46 -07002722 if (unlikely(object == ZERO_SIZE_PTR))
Christoph Lameter272c1d22007-06-08 13:46:49 -07002723 return 0;
2724
Vegard Nossum294a80a2007-12-04 23:45:30 -08002725 page = virt_to_head_page(object);
Vegard Nossum294a80a2007-12-04 23:45:30 -08002726
2727 if (unlikely(!PageSlab(page)))
2728 return PAGE_SIZE << compound_order(page);
2729
Christoph Lameter81819f02007-05-06 14:49:36 -07002730 s = page->slab;
Christoph Lameter81819f02007-05-06 14:49:36 -07002731
Christoph Lameterae20bfd2008-02-15 23:45:25 -08002732#ifdef CONFIG_SLUB_DEBUG
Christoph Lameter81819f02007-05-06 14:49:36 -07002733 /*
2734 * Debugging requires use of the padding between object
2735 * and whatever may come after it.
2736 */
2737 if (s->flags & (SLAB_RED_ZONE | SLAB_POISON))
2738 return s->objsize;
2739
Christoph Lameterae20bfd2008-02-15 23:45:25 -08002740#endif
Christoph Lameter81819f02007-05-06 14:49:36 -07002741 /*
2742 * If we have the need to store the freelist pointer
2743 * back there or track user information then we can
2744 * only use the space before that information.
2745 */
2746 if (s->flags & (SLAB_DESTROY_BY_RCU | SLAB_STORE_USER))
2747 return s->inuse;
Christoph Lameter81819f02007-05-06 14:49:36 -07002748 /*
2749 * Else we can use all the padding etc for the allocation
2750 */
2751 return s->size;
2752}
2753EXPORT_SYMBOL(ksize);
2754
2755void kfree(const void *x)
2756{
Christoph Lameter81819f02007-05-06 14:49:36 -07002757 struct page *page;
Christoph Lameter5bb983b2008-02-07 17:47:41 -08002758 void *object = (void *)x;
Christoph Lameter81819f02007-05-06 14:49:36 -07002759
Satyam Sharma2408c552007-10-16 01:24:44 -07002760 if (unlikely(ZERO_OR_NULL_PTR(x)))
Christoph Lameter81819f02007-05-06 14:49:36 -07002761 return;
2762
Christoph Lameterb49af682007-05-06 14:49:41 -07002763 page = virt_to_head_page(x);
Christoph Lameteraadb4bc2007-10-16 01:24:38 -07002764 if (unlikely(!PageSlab(page))) {
2765 put_page(page);
2766 return;
2767 }
Christoph Lameter5bb983b2008-02-07 17:47:41 -08002768 slab_free(page->slab, page, object, __builtin_return_address(0));
Christoph Lameter81819f02007-05-06 14:49:36 -07002769}
2770EXPORT_SYMBOL(kfree);
2771
Christoph Lameter2086d262007-05-06 14:49:46 -07002772/*
Christoph Lameter672bba32007-05-09 02:32:39 -07002773 * kmem_cache_shrink removes empty slabs from the partial lists and sorts
2774 * the remaining slabs by the number of items in use. The slabs with the
2775 * most items in use come first. New allocations will then fill those up
2776 * and thus they can be removed from the partial lists.
2777 *
2778 * The slabs with the least items are placed last. This results in them
2779 * being allocated from last increasing the chance that the last objects
2780 * are freed in them.
Christoph Lameter2086d262007-05-06 14:49:46 -07002781 */
2782int kmem_cache_shrink(struct kmem_cache *s)
2783{
2784 int node;
2785 int i;
2786 struct kmem_cache_node *n;
2787 struct page *page;
2788 struct page *t;
Christoph Lameter205ab992008-04-14 19:11:40 +03002789 int objects = oo_objects(s->max);
Christoph Lameter2086d262007-05-06 14:49:46 -07002790 struct list_head *slabs_by_inuse =
Christoph Lameter834f3d12008-04-14 19:11:31 +03002791 kmalloc(sizeof(struct list_head) * objects, GFP_KERNEL);
Christoph Lameter2086d262007-05-06 14:49:46 -07002792 unsigned long flags;
2793
2794 if (!slabs_by_inuse)
2795 return -ENOMEM;
2796
2797 flush_all(s);
Christoph Lameterf64dc582007-10-16 01:25:33 -07002798 for_each_node_state(node, N_NORMAL_MEMORY) {
Christoph Lameter2086d262007-05-06 14:49:46 -07002799 n = get_node(s, node);
2800
2801 if (!n->nr_partial)
2802 continue;
2803
Christoph Lameter834f3d12008-04-14 19:11:31 +03002804 for (i = 0; i < objects; i++)
Christoph Lameter2086d262007-05-06 14:49:46 -07002805 INIT_LIST_HEAD(slabs_by_inuse + i);
2806
2807 spin_lock_irqsave(&n->list_lock, flags);
2808
2809 /*
Christoph Lameter672bba32007-05-09 02:32:39 -07002810 * Build lists indexed by the items in use in each slab.
Christoph Lameter2086d262007-05-06 14:49:46 -07002811 *
Christoph Lameter672bba32007-05-09 02:32:39 -07002812 * Note that concurrent frees may occur while we hold the
2813 * list_lock. page->inuse here is the upper limit.
Christoph Lameter2086d262007-05-06 14:49:46 -07002814 */
2815 list_for_each_entry_safe(page, t, &n->partial, lru) {
2816 if (!page->inuse && slab_trylock(page)) {
2817 /*
2818 * Must hold slab lock here because slab_free
2819 * may have freed the last object and be
2820 * waiting to release the slab.
2821 */
2822 list_del(&page->lru);
2823 n->nr_partial--;
2824 slab_unlock(page);
2825 discard_slab(s, page);
2826 } else {
Christoph Lameterfcda3d82007-07-30 13:06:46 -07002827 list_move(&page->lru,
2828 slabs_by_inuse + page->inuse);
Christoph Lameter2086d262007-05-06 14:49:46 -07002829 }
2830 }
2831
Christoph Lameter2086d262007-05-06 14:49:46 -07002832 /*
Christoph Lameter672bba32007-05-09 02:32:39 -07002833 * Rebuild the partial list with the slabs filled up most
2834 * first and the least used slabs at the end.
Christoph Lameter2086d262007-05-06 14:49:46 -07002835 */
Christoph Lameter834f3d12008-04-14 19:11:31 +03002836 for (i = objects - 1; i >= 0; i--)
Christoph Lameter2086d262007-05-06 14:49:46 -07002837 list_splice(slabs_by_inuse + i, n->partial.prev);
2838
Christoph Lameter2086d262007-05-06 14:49:46 -07002839 spin_unlock_irqrestore(&n->list_lock, flags);
2840 }
2841
2842 kfree(slabs_by_inuse);
2843 return 0;
2844}
2845EXPORT_SYMBOL(kmem_cache_shrink);
2846
Yasunori Gotob9049e22007-10-21 16:41:37 -07002847#if defined(CONFIG_NUMA) && defined(CONFIG_MEMORY_HOTPLUG)
2848static int slab_mem_going_offline_callback(void *arg)
2849{
2850 struct kmem_cache *s;
2851
2852 down_read(&slub_lock);
2853 list_for_each_entry(s, &slab_caches, list)
2854 kmem_cache_shrink(s);
2855 up_read(&slub_lock);
2856
2857 return 0;
2858}
2859
2860static void slab_mem_offline_callback(void *arg)
2861{
2862 struct kmem_cache_node *n;
2863 struct kmem_cache *s;
2864 struct memory_notify *marg = arg;
2865 int offline_node;
2866
2867 offline_node = marg->status_change_nid;
2868
2869 /*
2870 * If the node still has available memory. we need kmem_cache_node
2871 * for it yet.
2872 */
2873 if (offline_node < 0)
2874 return;
2875
2876 down_read(&slub_lock);
2877 list_for_each_entry(s, &slab_caches, list) {
2878 n = get_node(s, offline_node);
2879 if (n) {
2880 /*
2881 * if n->nr_slabs > 0, slabs still exist on the node
2882 * that is going down. We were unable to free them,
2883 * and offline_pages() function shoudn't call this
2884 * callback. So, we must fail.
2885 */
Christoph Lameter0f389ec2008-04-14 18:53:02 +03002886 BUG_ON(slabs_node(s, offline_node));
Yasunori Gotob9049e22007-10-21 16:41:37 -07002887
2888 s->node[offline_node] = NULL;
2889 kmem_cache_free(kmalloc_caches, n);
2890 }
2891 }
2892 up_read(&slub_lock);
2893}
2894
2895static int slab_mem_going_online_callback(void *arg)
2896{
2897 struct kmem_cache_node *n;
2898 struct kmem_cache *s;
2899 struct memory_notify *marg = arg;
2900 int nid = marg->status_change_nid;
2901 int ret = 0;
2902
2903 /*
2904 * If the node's memory is already available, then kmem_cache_node is
2905 * already created. Nothing to do.
2906 */
2907 if (nid < 0)
2908 return 0;
2909
2910 /*
2911 * We are bringing a node online. No memory is availabe yet. We must
2912 * allocate a kmem_cache_node structure in order to bring the node
2913 * online.
2914 */
2915 down_read(&slub_lock);
2916 list_for_each_entry(s, &slab_caches, list) {
2917 /*
2918 * XXX: kmem_cache_alloc_node will fallback to other nodes
2919 * since memory is not yet available from the node that
2920 * is brought up.
2921 */
2922 n = kmem_cache_alloc(kmalloc_caches, GFP_KERNEL);
2923 if (!n) {
2924 ret = -ENOMEM;
2925 goto out;
2926 }
2927 init_kmem_cache_node(n);
2928 s->node[nid] = n;
2929 }
2930out:
2931 up_read(&slub_lock);
2932 return ret;
2933}
2934
2935static int slab_memory_callback(struct notifier_block *self,
2936 unsigned long action, void *arg)
2937{
2938 int ret = 0;
2939
2940 switch (action) {
2941 case MEM_GOING_ONLINE:
2942 ret = slab_mem_going_online_callback(arg);
2943 break;
2944 case MEM_GOING_OFFLINE:
2945 ret = slab_mem_going_offline_callback(arg);
2946 break;
2947 case MEM_OFFLINE:
2948 case MEM_CANCEL_ONLINE:
2949 slab_mem_offline_callback(arg);
2950 break;
2951 case MEM_ONLINE:
2952 case MEM_CANCEL_OFFLINE:
2953 break;
2954 }
2955
2956 ret = notifier_from_errno(ret);
2957 return ret;
2958}
2959
2960#endif /* CONFIG_MEMORY_HOTPLUG */
2961
Christoph Lameter81819f02007-05-06 14:49:36 -07002962/********************************************************************
2963 * Basic setup of slabs
2964 *******************************************************************/
2965
2966void __init kmem_cache_init(void)
2967{
2968 int i;
Christoph Lameter4b356be2007-06-16 10:16:13 -07002969 int caches = 0;
Christoph Lameter81819f02007-05-06 14:49:36 -07002970
Christoph Lameter4c93c3552007-10-16 01:26:08 -07002971 init_alloc_cpu();
2972
Christoph Lameter81819f02007-05-06 14:49:36 -07002973#ifdef CONFIG_NUMA
2974 /*
2975 * Must first have the slab cache available for the allocations of the
Christoph Lameter672bba32007-05-09 02:32:39 -07002976 * struct kmem_cache_node's. There is special bootstrap code in
Christoph Lameter81819f02007-05-06 14:49:36 -07002977 * kmem_cache_open for slab_state == DOWN.
2978 */
2979 create_kmalloc_cache(&kmalloc_caches[0], "kmem_cache_node",
2980 sizeof(struct kmem_cache_node), GFP_KERNEL);
Christoph Lameter8ffa6872007-05-31 00:40:51 -07002981 kmalloc_caches[0].refcount = -1;
Christoph Lameter4b356be2007-06-16 10:16:13 -07002982 caches++;
Yasunori Gotob9049e22007-10-21 16:41:37 -07002983
Nadia Derbey0c40ba42008-04-29 01:00:41 -07002984 hotplug_memory_notifier(slab_memory_callback, SLAB_CALLBACK_PRI);
Christoph Lameter81819f02007-05-06 14:49:36 -07002985#endif
2986
2987 /* Able to allocate the per node structures */
2988 slab_state = PARTIAL;
2989
2990 /* Caches that are not of the two-to-the-power-of size */
Christoph Lameter4b356be2007-06-16 10:16:13 -07002991 if (KMALLOC_MIN_SIZE <= 64) {
2992 create_kmalloc_cache(&kmalloc_caches[1],
Christoph Lameter81819f02007-05-06 14:49:36 -07002993 "kmalloc-96", 96, GFP_KERNEL);
Christoph Lameter4b356be2007-06-16 10:16:13 -07002994 caches++;
2995 }
2996 if (KMALLOC_MIN_SIZE <= 128) {
2997 create_kmalloc_cache(&kmalloc_caches[2],
Christoph Lameter81819f02007-05-06 14:49:36 -07002998 "kmalloc-192", 192, GFP_KERNEL);
Christoph Lameter4b356be2007-06-16 10:16:13 -07002999 caches++;
3000 }
Christoph Lameter81819f02007-05-06 14:49:36 -07003001
Christoph Lameter331dc552008-02-14 14:28:09 -08003002 for (i = KMALLOC_SHIFT_LOW; i <= PAGE_SHIFT; i++) {
Christoph Lameter81819f02007-05-06 14:49:36 -07003003 create_kmalloc_cache(&kmalloc_caches[i],
3004 "kmalloc", 1 << i, GFP_KERNEL);
Christoph Lameter4b356be2007-06-16 10:16:13 -07003005 caches++;
3006 }
Christoph Lameter81819f02007-05-06 14:49:36 -07003007
Christoph Lameterf1b26332007-07-17 04:03:26 -07003008
3009 /*
3010 * Patch up the size_index table if we have strange large alignment
3011 * requirements for the kmalloc array. This is only the case for
Christoph Lameter6446faa2008-02-15 23:45:26 -08003012 * MIPS it seems. The standard arches will not generate any code here.
Christoph Lameterf1b26332007-07-17 04:03:26 -07003013 *
3014 * Largest permitted alignment is 256 bytes due to the way we
3015 * handle the index determination for the smaller caches.
3016 *
3017 * Make sure that nothing crazy happens if someone starts tinkering
3018 * around with ARCH_KMALLOC_MINALIGN
3019 */
3020 BUILD_BUG_ON(KMALLOC_MIN_SIZE > 256 ||
3021 (KMALLOC_MIN_SIZE & (KMALLOC_MIN_SIZE - 1)));
3022
Christoph Lameter12ad6842007-07-17 04:03:28 -07003023 for (i = 8; i < KMALLOC_MIN_SIZE; i += 8)
Christoph Lameterf1b26332007-07-17 04:03:26 -07003024 size_index[(i - 1) / 8] = KMALLOC_SHIFT_LOW;
3025
Christoph Lameter81819f02007-05-06 14:49:36 -07003026 slab_state = UP;
3027
3028 /* Provide the correct kmalloc names now that the caches are up */
Christoph Lameter331dc552008-02-14 14:28:09 -08003029 for (i = KMALLOC_SHIFT_LOW; i <= PAGE_SHIFT; i++)
Christoph Lameter81819f02007-05-06 14:49:36 -07003030 kmalloc_caches[i]. name =
3031 kasprintf(GFP_KERNEL, "kmalloc-%d", 1 << i);
3032
3033#ifdef CONFIG_SMP
3034 register_cpu_notifier(&slab_notifier);
Christoph Lameter4c93c3552007-10-16 01:26:08 -07003035 kmem_size = offsetof(struct kmem_cache, cpu_slab) +
3036 nr_cpu_ids * sizeof(struct kmem_cache_cpu *);
3037#else
3038 kmem_size = sizeof(struct kmem_cache);
Christoph Lameter81819f02007-05-06 14:49:36 -07003039#endif
3040
Ingo Molnar3adbefe2008-02-05 17:57:39 -08003041 printk(KERN_INFO
3042 "SLUB: Genslabs=%d, HWalign=%d, Order=%d-%d, MinObjects=%d,"
Christoph Lameter4b356be2007-06-16 10:16:13 -07003043 " CPUs=%d, Nodes=%d\n",
3044 caches, cache_line_size(),
Christoph Lameter81819f02007-05-06 14:49:36 -07003045 slub_min_order, slub_max_order, slub_min_objects,
3046 nr_cpu_ids, nr_node_ids);
3047}
3048
3049/*
3050 * Find a mergeable slab cache
3051 */
3052static int slab_unmergeable(struct kmem_cache *s)
3053{
3054 if (slub_nomerge || (s->flags & SLUB_NEVER_MERGE))
3055 return 1;
3056
Christoph Lameterc59def9f2007-05-16 22:10:50 -07003057 if (s->ctor)
Christoph Lameter81819f02007-05-06 14:49:36 -07003058 return 1;
3059
Christoph Lameter8ffa6872007-05-31 00:40:51 -07003060 /*
3061 * We may have set a slab to be unmergeable during bootstrap.
3062 */
3063 if (s->refcount < 0)
3064 return 1;
3065
Christoph Lameter81819f02007-05-06 14:49:36 -07003066 return 0;
3067}
3068
3069static struct kmem_cache *find_mergeable(size_t size,
Christoph Lameterba0268a2007-09-11 15:24:11 -07003070 size_t align, unsigned long flags, const char *name,
Christoph Lameter4ba9b9d2007-10-16 23:25:51 -07003071 void (*ctor)(struct kmem_cache *, void *))
Christoph Lameter81819f02007-05-06 14:49:36 -07003072{
Christoph Lameter5b95a4a2007-07-17 04:03:19 -07003073 struct kmem_cache *s;
Christoph Lameter81819f02007-05-06 14:49:36 -07003074
3075 if (slub_nomerge || (flags & SLUB_NEVER_MERGE))
3076 return NULL;
3077
Christoph Lameterc59def9f2007-05-16 22:10:50 -07003078 if (ctor)
Christoph Lameter81819f02007-05-06 14:49:36 -07003079 return NULL;
3080
3081 size = ALIGN(size, sizeof(void *));
3082 align = calculate_alignment(flags, align, size);
3083 size = ALIGN(size, align);
Christoph Lameterba0268a2007-09-11 15:24:11 -07003084 flags = kmem_cache_flags(size, flags, name, NULL);
Christoph Lameter81819f02007-05-06 14:49:36 -07003085
Christoph Lameter5b95a4a2007-07-17 04:03:19 -07003086 list_for_each_entry(s, &slab_caches, list) {
Christoph Lameter81819f02007-05-06 14:49:36 -07003087 if (slab_unmergeable(s))
3088 continue;
3089
3090 if (size > s->size)
3091 continue;
3092
Christoph Lameterba0268a2007-09-11 15:24:11 -07003093 if ((flags & SLUB_MERGE_SAME) != (s->flags & SLUB_MERGE_SAME))
Christoph Lameter81819f02007-05-06 14:49:36 -07003094 continue;
3095 /*
3096 * Check if alignment is compatible.
3097 * Courtesy of Adrian Drzewiecki
3098 */
Pekka Enberg06428782008-01-07 23:20:27 -08003099 if ((s->size & ~(align - 1)) != s->size)
Christoph Lameter81819f02007-05-06 14:49:36 -07003100 continue;
3101
3102 if (s->size - size >= sizeof(void *))
3103 continue;
3104
3105 return s;
3106 }
3107 return NULL;
3108}
3109
3110struct kmem_cache *kmem_cache_create(const char *name, size_t size,
3111 size_t align, unsigned long flags,
Christoph Lameter4ba9b9d2007-10-16 23:25:51 -07003112 void (*ctor)(struct kmem_cache *, void *))
Christoph Lameter81819f02007-05-06 14:49:36 -07003113{
3114 struct kmem_cache *s;
3115
3116 down_write(&slub_lock);
Christoph Lameterba0268a2007-09-11 15:24:11 -07003117 s = find_mergeable(size, align, flags, name, ctor);
Christoph Lameter81819f02007-05-06 14:49:36 -07003118 if (s) {
Christoph Lameter42a9fdb2007-10-16 01:26:09 -07003119 int cpu;
3120
Christoph Lameter81819f02007-05-06 14:49:36 -07003121 s->refcount++;
3122 /*
3123 * Adjust the object sizes so that we clear
3124 * the complete object on kzalloc.
3125 */
3126 s->objsize = max(s->objsize, (int)size);
Christoph Lameter42a9fdb2007-10-16 01:26:09 -07003127
3128 /*
3129 * And then we need to update the object size in the
3130 * per cpu structures
3131 */
3132 for_each_online_cpu(cpu)
3133 get_cpu_slab(s, cpu)->objsize = s->objsize;
Christoph Lameter6446faa2008-02-15 23:45:26 -08003134
Christoph Lameter81819f02007-05-06 14:49:36 -07003135 s->inuse = max_t(int, s->inuse, ALIGN(size, sizeof(void *)));
Christoph Lametera0e1d1b2007-07-17 04:03:31 -07003136 up_write(&slub_lock);
Christoph Lameter6446faa2008-02-15 23:45:26 -08003137
Christoph Lameter81819f02007-05-06 14:49:36 -07003138 if (sysfs_slab_alias(s, name))
3139 goto err;
Christoph Lametera0e1d1b2007-07-17 04:03:31 -07003140 return s;
3141 }
Christoph Lameter6446faa2008-02-15 23:45:26 -08003142
Christoph Lametera0e1d1b2007-07-17 04:03:31 -07003143 s = kmalloc(kmem_size, GFP_KERNEL);
3144 if (s) {
3145 if (kmem_cache_open(s, GFP_KERNEL, name,
Christoph Lameterc59def9f2007-05-16 22:10:50 -07003146 size, align, flags, ctor)) {
Christoph Lameter81819f02007-05-06 14:49:36 -07003147 list_add(&s->list, &slab_caches);
Christoph Lametera0e1d1b2007-07-17 04:03:31 -07003148 up_write(&slub_lock);
3149 if (sysfs_slab_add(s))
3150 goto err;
3151 return s;
3152 }
3153 kfree(s);
Christoph Lameter81819f02007-05-06 14:49:36 -07003154 }
3155 up_write(&slub_lock);
Christoph Lameter81819f02007-05-06 14:49:36 -07003156
3157err:
Christoph Lameter81819f02007-05-06 14:49:36 -07003158 if (flags & SLAB_PANIC)
3159 panic("Cannot create slabcache %s\n", name);
3160 else
3161 s = NULL;
3162 return s;
3163}
3164EXPORT_SYMBOL(kmem_cache_create);
3165
Christoph Lameter81819f02007-05-06 14:49:36 -07003166#ifdef CONFIG_SMP
Christoph Lameter27390bc2007-06-01 00:47:09 -07003167/*
Christoph Lameter672bba32007-05-09 02:32:39 -07003168 * Use the cpu notifier to insure that the cpu slabs are flushed when
3169 * necessary.
Christoph Lameter81819f02007-05-06 14:49:36 -07003170 */
3171static int __cpuinit slab_cpuup_callback(struct notifier_block *nfb,
3172 unsigned long action, void *hcpu)
3173{
3174 long cpu = (long)hcpu;
Christoph Lameter5b95a4a2007-07-17 04:03:19 -07003175 struct kmem_cache *s;
3176 unsigned long flags;
Christoph Lameter81819f02007-05-06 14:49:36 -07003177
3178 switch (action) {
Christoph Lameter4c93c3552007-10-16 01:26:08 -07003179 case CPU_UP_PREPARE:
3180 case CPU_UP_PREPARE_FROZEN:
3181 init_alloc_cpu_cpu(cpu);
3182 down_read(&slub_lock);
3183 list_for_each_entry(s, &slab_caches, list)
3184 s->cpu_slab[cpu] = alloc_kmem_cache_cpu(s, cpu,
3185 GFP_KERNEL);
3186 up_read(&slub_lock);
3187 break;
3188
Christoph Lameter81819f02007-05-06 14:49:36 -07003189 case CPU_UP_CANCELED:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07003190 case CPU_UP_CANCELED_FROZEN:
Christoph Lameter81819f02007-05-06 14:49:36 -07003191 case CPU_DEAD:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07003192 case CPU_DEAD_FROZEN:
Christoph Lameter5b95a4a2007-07-17 04:03:19 -07003193 down_read(&slub_lock);
3194 list_for_each_entry(s, &slab_caches, list) {
Christoph Lameter4c93c3552007-10-16 01:26:08 -07003195 struct kmem_cache_cpu *c = get_cpu_slab(s, cpu);
3196
Christoph Lameter5b95a4a2007-07-17 04:03:19 -07003197 local_irq_save(flags);
3198 __flush_cpu_slab(s, cpu);
3199 local_irq_restore(flags);
Christoph Lameter4c93c3552007-10-16 01:26:08 -07003200 free_kmem_cache_cpu(c, cpu);
3201 s->cpu_slab[cpu] = NULL;
Christoph Lameter5b95a4a2007-07-17 04:03:19 -07003202 }
3203 up_read(&slub_lock);
Christoph Lameter81819f02007-05-06 14:49:36 -07003204 break;
3205 default:
3206 break;
3207 }
3208 return NOTIFY_OK;
3209}
3210
Pekka Enberg06428782008-01-07 23:20:27 -08003211static struct notifier_block __cpuinitdata slab_notifier = {
Ingo Molnar3adbefe2008-02-05 17:57:39 -08003212 .notifier_call = slab_cpuup_callback
Pekka Enberg06428782008-01-07 23:20:27 -08003213};
Christoph Lameter81819f02007-05-06 14:49:36 -07003214
3215#endif
3216
Christoph Lameter81819f02007-05-06 14:49:36 -07003217void *__kmalloc_track_caller(size_t size, gfp_t gfpflags, void *caller)
3218{
Christoph Lameteraadb4bc2007-10-16 01:24:38 -07003219 struct kmem_cache *s;
3220
Christoph Lameter331dc552008-02-14 14:28:09 -08003221 if (unlikely(size > PAGE_SIZE))
Pekka Enbergeada35e2008-02-11 22:47:46 +02003222 return kmalloc_large(size, gfpflags);
3223
Christoph Lameteraadb4bc2007-10-16 01:24:38 -07003224 s = get_slab(size, gfpflags);
Christoph Lameter81819f02007-05-06 14:49:36 -07003225
Satyam Sharma2408c552007-10-16 01:24:44 -07003226 if (unlikely(ZERO_OR_NULL_PTR(s)))
Christoph Lameter6cb8f912007-07-17 04:03:22 -07003227 return s;
Christoph Lameter81819f02007-05-06 14:49:36 -07003228
Christoph Lameterce15fea2007-07-17 04:03:28 -07003229 return slab_alloc(s, gfpflags, -1, caller);
Christoph Lameter81819f02007-05-06 14:49:36 -07003230}
3231
3232void *__kmalloc_node_track_caller(size_t size, gfp_t gfpflags,
3233 int node, void *caller)
3234{
Christoph Lameteraadb4bc2007-10-16 01:24:38 -07003235 struct kmem_cache *s;
3236
Christoph Lameter331dc552008-02-14 14:28:09 -08003237 if (unlikely(size > PAGE_SIZE))
Christoph Lameterf619cfe2008-03-01 13:56:40 -08003238 return kmalloc_large_node(size, gfpflags, node);
Pekka Enbergeada35e2008-02-11 22:47:46 +02003239
Christoph Lameteraadb4bc2007-10-16 01:24:38 -07003240 s = get_slab(size, gfpflags);
Christoph Lameter81819f02007-05-06 14:49:36 -07003241
Satyam Sharma2408c552007-10-16 01:24:44 -07003242 if (unlikely(ZERO_OR_NULL_PTR(s)))
Christoph Lameter6cb8f912007-07-17 04:03:22 -07003243 return s;
Christoph Lameter81819f02007-05-06 14:49:36 -07003244
Christoph Lameterce15fea2007-07-17 04:03:28 -07003245 return slab_alloc(s, gfpflags, node, caller);
Christoph Lameter81819f02007-05-06 14:49:36 -07003246}
3247
Christoph Lameter5b06c8532008-04-14 18:51:34 +03003248#if (defined(CONFIG_SYSFS) && defined(CONFIG_SLUB_DEBUG)) || defined(CONFIG_SLABINFO)
Christoph Lameter205ab992008-04-14 19:11:40 +03003249static unsigned long count_partial(struct kmem_cache_node *n,
3250 int (*get_count)(struct page *))
Christoph Lameter5b06c8532008-04-14 18:51:34 +03003251{
3252 unsigned long flags;
3253 unsigned long x = 0;
3254 struct page *page;
3255
3256 spin_lock_irqsave(&n->list_lock, flags);
3257 list_for_each_entry(page, &n->partial, lru)
Christoph Lameter205ab992008-04-14 19:11:40 +03003258 x += get_count(page);
Christoph Lameter5b06c8532008-04-14 18:51:34 +03003259 spin_unlock_irqrestore(&n->list_lock, flags);
3260 return x;
3261}
Christoph Lameter205ab992008-04-14 19:11:40 +03003262
3263static int count_inuse(struct page *page)
3264{
3265 return page->inuse;
3266}
3267
3268static int count_total(struct page *page)
3269{
3270 return page->objects;
3271}
3272
3273static int count_free(struct page *page)
3274{
3275 return page->objects - page->inuse;
3276}
Christoph Lameter5b06c8532008-04-14 18:51:34 +03003277#endif
3278
Christoph Lameter41ecc552007-05-09 02:32:44 -07003279#if defined(CONFIG_SYSFS) && defined(CONFIG_SLUB_DEBUG)
Christoph Lameter434e2452007-07-17 04:03:30 -07003280static int validate_slab(struct kmem_cache *s, struct page *page,
3281 unsigned long *map)
Christoph Lameter53e15af2007-05-06 14:49:43 -07003282{
3283 void *p;
Christoph Lametera973e9d2008-03-01 13:40:44 -08003284 void *addr = page_address(page);
Christoph Lameter53e15af2007-05-06 14:49:43 -07003285
3286 if (!check_slab(s, page) ||
3287 !on_freelist(s, page, NULL))
3288 return 0;
3289
3290 /* Now we know that a valid freelist exists */
Christoph Lameter39b26462008-04-14 19:11:30 +03003291 bitmap_zero(map, page->objects);
Christoph Lameter53e15af2007-05-06 14:49:43 -07003292
Christoph Lameter7656c722007-05-09 02:32:40 -07003293 for_each_free_object(p, s, page->freelist) {
3294 set_bit(slab_index(p, s, addr), map);
Christoph Lameter53e15af2007-05-06 14:49:43 -07003295 if (!check_object(s, page, p, 0))
3296 return 0;
3297 }
3298
Christoph Lameter224a88b2008-04-14 19:11:31 +03003299 for_each_object(p, s, addr, page->objects)
Christoph Lameter7656c722007-05-09 02:32:40 -07003300 if (!test_bit(slab_index(p, s, addr), map))
Christoph Lameter53e15af2007-05-06 14:49:43 -07003301 if (!check_object(s, page, p, 1))
3302 return 0;
3303 return 1;
3304}
3305
Christoph Lameter434e2452007-07-17 04:03:30 -07003306static void validate_slab_slab(struct kmem_cache *s, struct page *page,
3307 unsigned long *map)
Christoph Lameter53e15af2007-05-06 14:49:43 -07003308{
3309 if (slab_trylock(page)) {
Christoph Lameter434e2452007-07-17 04:03:30 -07003310 validate_slab(s, page, map);
Christoph Lameter53e15af2007-05-06 14:49:43 -07003311 slab_unlock(page);
3312 } else
3313 printk(KERN_INFO "SLUB %s: Skipped busy slab 0x%p\n",
3314 s->name, page);
3315
3316 if (s->flags & DEBUG_DEFAULT_FLAGS) {
Christoph Lameter35e5d7e2007-05-09 02:32:42 -07003317 if (!SlabDebug(page))
3318 printk(KERN_ERR "SLUB %s: SlabDebug not set "
Christoph Lameter53e15af2007-05-06 14:49:43 -07003319 "on slab 0x%p\n", s->name, page);
3320 } else {
Christoph Lameter35e5d7e2007-05-09 02:32:42 -07003321 if (SlabDebug(page))
3322 printk(KERN_ERR "SLUB %s: SlabDebug set on "
Christoph Lameter53e15af2007-05-06 14:49:43 -07003323 "slab 0x%p\n", s->name, page);
3324 }
3325}
3326
Christoph Lameter434e2452007-07-17 04:03:30 -07003327static int validate_slab_node(struct kmem_cache *s,
3328 struct kmem_cache_node *n, unsigned long *map)
Christoph Lameter53e15af2007-05-06 14:49:43 -07003329{
3330 unsigned long count = 0;
3331 struct page *page;
3332 unsigned long flags;
3333
3334 spin_lock_irqsave(&n->list_lock, flags);
3335
3336 list_for_each_entry(page, &n->partial, lru) {
Christoph Lameter434e2452007-07-17 04:03:30 -07003337 validate_slab_slab(s, page, map);
Christoph Lameter53e15af2007-05-06 14:49:43 -07003338 count++;
3339 }
3340 if (count != n->nr_partial)
3341 printk(KERN_ERR "SLUB %s: %ld partial slabs counted but "
3342 "counter=%ld\n", s->name, count, n->nr_partial);
3343
3344 if (!(s->flags & SLAB_STORE_USER))
3345 goto out;
3346
3347 list_for_each_entry(page, &n->full, lru) {
Christoph Lameter434e2452007-07-17 04:03:30 -07003348 validate_slab_slab(s, page, map);
Christoph Lameter53e15af2007-05-06 14:49:43 -07003349 count++;
3350 }
3351 if (count != atomic_long_read(&n->nr_slabs))
3352 printk(KERN_ERR "SLUB: %s %ld slabs counted but "
3353 "counter=%ld\n", s->name, count,
3354 atomic_long_read(&n->nr_slabs));
3355
3356out:
3357 spin_unlock_irqrestore(&n->list_lock, flags);
3358 return count;
3359}
3360
Christoph Lameter434e2452007-07-17 04:03:30 -07003361static long validate_slab_cache(struct kmem_cache *s)
Christoph Lameter53e15af2007-05-06 14:49:43 -07003362{
3363 int node;
3364 unsigned long count = 0;
Christoph Lameter205ab992008-04-14 19:11:40 +03003365 unsigned long *map = kmalloc(BITS_TO_LONGS(oo_objects(s->max)) *
Christoph Lameter434e2452007-07-17 04:03:30 -07003366 sizeof(unsigned long), GFP_KERNEL);
3367
3368 if (!map)
3369 return -ENOMEM;
Christoph Lameter53e15af2007-05-06 14:49:43 -07003370
3371 flush_all(s);
Christoph Lameterf64dc582007-10-16 01:25:33 -07003372 for_each_node_state(node, N_NORMAL_MEMORY) {
Christoph Lameter53e15af2007-05-06 14:49:43 -07003373 struct kmem_cache_node *n = get_node(s, node);
3374
Christoph Lameter434e2452007-07-17 04:03:30 -07003375 count += validate_slab_node(s, n, map);
Christoph Lameter53e15af2007-05-06 14:49:43 -07003376 }
Christoph Lameter434e2452007-07-17 04:03:30 -07003377 kfree(map);
Christoph Lameter53e15af2007-05-06 14:49:43 -07003378 return count;
3379}
3380
Christoph Lameterb3459702007-05-09 02:32:41 -07003381#ifdef SLUB_RESILIENCY_TEST
3382static void resiliency_test(void)
3383{
3384 u8 *p;
3385
3386 printk(KERN_ERR "SLUB resiliency testing\n");
3387 printk(KERN_ERR "-----------------------\n");
3388 printk(KERN_ERR "A. Corruption after allocation\n");
3389
3390 p = kzalloc(16, GFP_KERNEL);
3391 p[16] = 0x12;
3392 printk(KERN_ERR "\n1. kmalloc-16: Clobber Redzone/next pointer"
3393 " 0x12->0x%p\n\n", p + 16);
3394
3395 validate_slab_cache(kmalloc_caches + 4);
3396
3397 /* Hmmm... The next two are dangerous */
3398 p = kzalloc(32, GFP_KERNEL);
3399 p[32 + sizeof(void *)] = 0x34;
3400 printk(KERN_ERR "\n2. kmalloc-32: Clobber next pointer/next slab"
Ingo Molnar3adbefe2008-02-05 17:57:39 -08003401 " 0x34 -> -0x%p\n", p);
3402 printk(KERN_ERR
3403 "If allocated object is overwritten then not detectable\n\n");
Christoph Lameterb3459702007-05-09 02:32:41 -07003404
3405 validate_slab_cache(kmalloc_caches + 5);
3406 p = kzalloc(64, GFP_KERNEL);
3407 p += 64 + (get_cycles() & 0xff) * sizeof(void *);
3408 *p = 0x56;
3409 printk(KERN_ERR "\n3. kmalloc-64: corrupting random byte 0x56->0x%p\n",
3410 p);
Ingo Molnar3adbefe2008-02-05 17:57:39 -08003411 printk(KERN_ERR
3412 "If allocated object is overwritten then not detectable\n\n");
Christoph Lameterb3459702007-05-09 02:32:41 -07003413 validate_slab_cache(kmalloc_caches + 6);
3414
3415 printk(KERN_ERR "\nB. Corruption after free\n");
3416 p = kzalloc(128, GFP_KERNEL);
3417 kfree(p);
3418 *p = 0x78;
3419 printk(KERN_ERR "1. kmalloc-128: Clobber first word 0x78->0x%p\n\n", p);
3420 validate_slab_cache(kmalloc_caches + 7);
3421
3422 p = kzalloc(256, GFP_KERNEL);
3423 kfree(p);
3424 p[50] = 0x9a;
Ingo Molnar3adbefe2008-02-05 17:57:39 -08003425 printk(KERN_ERR "\n2. kmalloc-256: Clobber 50th byte 0x9a->0x%p\n\n",
3426 p);
Christoph Lameterb3459702007-05-09 02:32:41 -07003427 validate_slab_cache(kmalloc_caches + 8);
3428
3429 p = kzalloc(512, GFP_KERNEL);
3430 kfree(p);
3431 p[512] = 0xab;
3432 printk(KERN_ERR "\n3. kmalloc-512: Clobber redzone 0xab->0x%p\n\n", p);
3433 validate_slab_cache(kmalloc_caches + 9);
3434}
3435#else
3436static void resiliency_test(void) {};
3437#endif
3438
Christoph Lameter88a420e2007-05-06 14:49:45 -07003439/*
Christoph Lameter672bba32007-05-09 02:32:39 -07003440 * Generate lists of code addresses where slabcache objects are allocated
Christoph Lameter88a420e2007-05-06 14:49:45 -07003441 * and freed.
3442 */
3443
3444struct location {
3445 unsigned long count;
3446 void *addr;
Christoph Lameter45edfa52007-05-09 02:32:45 -07003447 long long sum_time;
3448 long min_time;
3449 long max_time;
3450 long min_pid;
3451 long max_pid;
3452 cpumask_t cpus;
3453 nodemask_t nodes;
Christoph Lameter88a420e2007-05-06 14:49:45 -07003454};
3455
3456struct loc_track {
3457 unsigned long max;
3458 unsigned long count;
3459 struct location *loc;
3460};
3461
3462static void free_loc_track(struct loc_track *t)
3463{
3464 if (t->max)
3465 free_pages((unsigned long)t->loc,
3466 get_order(sizeof(struct location) * t->max));
3467}
3468
Christoph Lameter68dff6a2007-07-17 04:03:20 -07003469static int alloc_loc_track(struct loc_track *t, unsigned long max, gfp_t flags)
Christoph Lameter88a420e2007-05-06 14:49:45 -07003470{
3471 struct location *l;
3472 int order;
3473
Christoph Lameter88a420e2007-05-06 14:49:45 -07003474 order = get_order(sizeof(struct location) * max);
3475
Christoph Lameter68dff6a2007-07-17 04:03:20 -07003476 l = (void *)__get_free_pages(flags, order);
Christoph Lameter88a420e2007-05-06 14:49:45 -07003477 if (!l)
3478 return 0;
3479
3480 if (t->count) {
3481 memcpy(l, t->loc, sizeof(struct location) * t->count);
3482 free_loc_track(t);
3483 }
3484 t->max = max;
3485 t->loc = l;
3486 return 1;
3487}
3488
3489static int add_location(struct loc_track *t, struct kmem_cache *s,
Christoph Lameter45edfa52007-05-09 02:32:45 -07003490 const struct track *track)
Christoph Lameter88a420e2007-05-06 14:49:45 -07003491{
3492 long start, end, pos;
3493 struct location *l;
3494 void *caddr;
Christoph Lameter45edfa52007-05-09 02:32:45 -07003495 unsigned long age = jiffies - track->when;
Christoph Lameter88a420e2007-05-06 14:49:45 -07003496
3497 start = -1;
3498 end = t->count;
3499
3500 for ( ; ; ) {
3501 pos = start + (end - start + 1) / 2;
3502
3503 /*
3504 * There is nothing at "end". If we end up there
3505 * we need to add something to before end.
3506 */
3507 if (pos == end)
3508 break;
3509
3510 caddr = t->loc[pos].addr;
Christoph Lameter45edfa52007-05-09 02:32:45 -07003511 if (track->addr == caddr) {
3512
3513 l = &t->loc[pos];
3514 l->count++;
3515 if (track->when) {
3516 l->sum_time += age;
3517 if (age < l->min_time)
3518 l->min_time = age;
3519 if (age > l->max_time)
3520 l->max_time = age;
3521
3522 if (track->pid < l->min_pid)
3523 l->min_pid = track->pid;
3524 if (track->pid > l->max_pid)
3525 l->max_pid = track->pid;
3526
3527 cpu_set(track->cpu, l->cpus);
3528 }
3529 node_set(page_to_nid(virt_to_page(track)), l->nodes);
Christoph Lameter88a420e2007-05-06 14:49:45 -07003530 return 1;
3531 }
3532
Christoph Lameter45edfa52007-05-09 02:32:45 -07003533 if (track->addr < caddr)
Christoph Lameter88a420e2007-05-06 14:49:45 -07003534 end = pos;
3535 else
3536 start = pos;
3537 }
3538
3539 /*
Christoph Lameter672bba32007-05-09 02:32:39 -07003540 * Not found. Insert new tracking element.
Christoph Lameter88a420e2007-05-06 14:49:45 -07003541 */
Christoph Lameter68dff6a2007-07-17 04:03:20 -07003542 if (t->count >= t->max && !alloc_loc_track(t, 2 * t->max, GFP_ATOMIC))
Christoph Lameter88a420e2007-05-06 14:49:45 -07003543 return 0;
3544
3545 l = t->loc + pos;
3546 if (pos < t->count)
3547 memmove(l + 1, l,
3548 (t->count - pos) * sizeof(struct location));
3549 t->count++;
3550 l->count = 1;
Christoph Lameter45edfa52007-05-09 02:32:45 -07003551 l->addr = track->addr;
3552 l->sum_time = age;
3553 l->min_time = age;
3554 l->max_time = age;
3555 l->min_pid = track->pid;
3556 l->max_pid = track->pid;
3557 cpus_clear(l->cpus);
3558 cpu_set(track->cpu, l->cpus);
3559 nodes_clear(l->nodes);
3560 node_set(page_to_nid(virt_to_page(track)), l->nodes);
Christoph Lameter88a420e2007-05-06 14:49:45 -07003561 return 1;
3562}
3563
3564static void process_slab(struct loc_track *t, struct kmem_cache *s,
3565 struct page *page, enum track_item alloc)
3566{
Christoph Lametera973e9d2008-03-01 13:40:44 -08003567 void *addr = page_address(page);
Christoph Lameter39b26462008-04-14 19:11:30 +03003568 DECLARE_BITMAP(map, page->objects);
Christoph Lameter88a420e2007-05-06 14:49:45 -07003569 void *p;
3570
Christoph Lameter39b26462008-04-14 19:11:30 +03003571 bitmap_zero(map, page->objects);
Christoph Lameter7656c722007-05-09 02:32:40 -07003572 for_each_free_object(p, s, page->freelist)
3573 set_bit(slab_index(p, s, addr), map);
Christoph Lameter88a420e2007-05-06 14:49:45 -07003574
Christoph Lameter224a88b2008-04-14 19:11:31 +03003575 for_each_object(p, s, addr, page->objects)
Christoph Lameter45edfa52007-05-09 02:32:45 -07003576 if (!test_bit(slab_index(p, s, addr), map))
3577 add_location(t, s, get_track(s, p, alloc));
Christoph Lameter88a420e2007-05-06 14:49:45 -07003578}
3579
3580static int list_locations(struct kmem_cache *s, char *buf,
3581 enum track_item alloc)
3582{
Harvey Harrisone374d482008-01-31 15:20:50 -08003583 int len = 0;
Christoph Lameter88a420e2007-05-06 14:49:45 -07003584 unsigned long i;
Christoph Lameter68dff6a2007-07-17 04:03:20 -07003585 struct loc_track t = { 0, 0, NULL };
Christoph Lameter88a420e2007-05-06 14:49:45 -07003586 int node;
3587
Christoph Lameter68dff6a2007-07-17 04:03:20 -07003588 if (!alloc_loc_track(&t, PAGE_SIZE / sizeof(struct location),
Andrew Mortonea3061d2007-10-16 01:26:09 -07003589 GFP_TEMPORARY))
Christoph Lameter68dff6a2007-07-17 04:03:20 -07003590 return sprintf(buf, "Out of memory\n");
Christoph Lameter88a420e2007-05-06 14:49:45 -07003591
3592 /* Push back cpu slabs */
3593 flush_all(s);
3594
Christoph Lameterf64dc582007-10-16 01:25:33 -07003595 for_each_node_state(node, N_NORMAL_MEMORY) {
Christoph Lameter88a420e2007-05-06 14:49:45 -07003596 struct kmem_cache_node *n = get_node(s, node);
3597 unsigned long flags;
3598 struct page *page;
3599
Christoph Lameter9e869432007-08-22 14:01:56 -07003600 if (!atomic_long_read(&n->nr_slabs))
Christoph Lameter88a420e2007-05-06 14:49:45 -07003601 continue;
3602
3603 spin_lock_irqsave(&n->list_lock, flags);
3604 list_for_each_entry(page, &n->partial, lru)
3605 process_slab(&t, s, page, alloc);
3606 list_for_each_entry(page, &n->full, lru)
3607 process_slab(&t, s, page, alloc);
3608 spin_unlock_irqrestore(&n->list_lock, flags);
3609 }
3610
3611 for (i = 0; i < t.count; i++) {
Christoph Lameter45edfa52007-05-09 02:32:45 -07003612 struct location *l = &t.loc[i];
Christoph Lameter88a420e2007-05-06 14:49:45 -07003613
Harvey Harrisone374d482008-01-31 15:20:50 -08003614 if (len > PAGE_SIZE - 100)
Christoph Lameter88a420e2007-05-06 14:49:45 -07003615 break;
Harvey Harrisone374d482008-01-31 15:20:50 -08003616 len += sprintf(buf + len, "%7ld ", l->count);
Christoph Lameter45edfa52007-05-09 02:32:45 -07003617
3618 if (l->addr)
Harvey Harrisone374d482008-01-31 15:20:50 -08003619 len += sprint_symbol(buf + len, (unsigned long)l->addr);
Christoph Lameter88a420e2007-05-06 14:49:45 -07003620 else
Harvey Harrisone374d482008-01-31 15:20:50 -08003621 len += sprintf(buf + len, "<not-available>");
Christoph Lameter45edfa52007-05-09 02:32:45 -07003622
3623 if (l->sum_time != l->min_time) {
3624 unsigned long remainder;
3625
Harvey Harrisone374d482008-01-31 15:20:50 -08003626 len += sprintf(buf + len, " age=%ld/%ld/%ld",
Christoph Lameter45edfa52007-05-09 02:32:45 -07003627 l->min_time,
3628 div_long_long_rem(l->sum_time, l->count, &remainder),
3629 l->max_time);
3630 } else
Harvey Harrisone374d482008-01-31 15:20:50 -08003631 len += sprintf(buf + len, " age=%ld",
Christoph Lameter45edfa52007-05-09 02:32:45 -07003632 l->min_time);
3633
3634 if (l->min_pid != l->max_pid)
Harvey Harrisone374d482008-01-31 15:20:50 -08003635 len += sprintf(buf + len, " pid=%ld-%ld",
Christoph Lameter45edfa52007-05-09 02:32:45 -07003636 l->min_pid, l->max_pid);
3637 else
Harvey Harrisone374d482008-01-31 15:20:50 -08003638 len += sprintf(buf + len, " pid=%ld",
Christoph Lameter45edfa52007-05-09 02:32:45 -07003639 l->min_pid);
3640
Christoph Lameter84966342007-06-23 17:16:32 -07003641 if (num_online_cpus() > 1 && !cpus_empty(l->cpus) &&
Harvey Harrisone374d482008-01-31 15:20:50 -08003642 len < PAGE_SIZE - 60) {
3643 len += sprintf(buf + len, " cpus=");
3644 len += cpulist_scnprintf(buf + len, PAGE_SIZE - len - 50,
Christoph Lameter45edfa52007-05-09 02:32:45 -07003645 l->cpus);
3646 }
3647
Christoph Lameter84966342007-06-23 17:16:32 -07003648 if (num_online_nodes() > 1 && !nodes_empty(l->nodes) &&
Harvey Harrisone374d482008-01-31 15:20:50 -08003649 len < PAGE_SIZE - 60) {
3650 len += sprintf(buf + len, " nodes=");
3651 len += nodelist_scnprintf(buf + len, PAGE_SIZE - len - 50,
Christoph Lameter45edfa52007-05-09 02:32:45 -07003652 l->nodes);
3653 }
3654
Harvey Harrisone374d482008-01-31 15:20:50 -08003655 len += sprintf(buf + len, "\n");
Christoph Lameter88a420e2007-05-06 14:49:45 -07003656 }
3657
3658 free_loc_track(&t);
3659 if (!t.count)
Harvey Harrisone374d482008-01-31 15:20:50 -08003660 len += sprintf(buf, "No data\n");
3661 return len;
Christoph Lameter88a420e2007-05-06 14:49:45 -07003662}
3663
Christoph Lameter81819f02007-05-06 14:49:36 -07003664enum slab_stat_type {
Christoph Lameter205ab992008-04-14 19:11:40 +03003665 SL_ALL, /* All slabs */
3666 SL_PARTIAL, /* Only partially allocated slabs */
3667 SL_CPU, /* Only slabs used for cpu caches */
3668 SL_OBJECTS, /* Determine allocated objects not slabs */
3669 SL_TOTAL /* Determine object capacity not slabs */
Christoph Lameter81819f02007-05-06 14:49:36 -07003670};
3671
Christoph Lameter205ab992008-04-14 19:11:40 +03003672#define SO_ALL (1 << SL_ALL)
Christoph Lameter81819f02007-05-06 14:49:36 -07003673#define SO_PARTIAL (1 << SL_PARTIAL)
3674#define SO_CPU (1 << SL_CPU)
3675#define SO_OBJECTS (1 << SL_OBJECTS)
Christoph Lameter205ab992008-04-14 19:11:40 +03003676#define SO_TOTAL (1 << SL_TOTAL)
Christoph Lameter81819f02007-05-06 14:49:36 -07003677
Cyrill Gorcunov62e5c4b2008-03-02 23:28:24 +03003678static ssize_t show_slab_objects(struct kmem_cache *s,
3679 char *buf, unsigned long flags)
Christoph Lameter81819f02007-05-06 14:49:36 -07003680{
3681 unsigned long total = 0;
Christoph Lameter81819f02007-05-06 14:49:36 -07003682 int node;
3683 int x;
3684 unsigned long *nodes;
3685 unsigned long *per_cpu;
3686
3687 nodes = kzalloc(2 * sizeof(unsigned long) * nr_node_ids, GFP_KERNEL);
Cyrill Gorcunov62e5c4b2008-03-02 23:28:24 +03003688 if (!nodes)
3689 return -ENOMEM;
Christoph Lameter81819f02007-05-06 14:49:36 -07003690 per_cpu = nodes + nr_node_ids;
3691
Christoph Lameter205ab992008-04-14 19:11:40 +03003692 if (flags & SO_CPU) {
3693 int cpu;
Christoph Lameter81819f02007-05-06 14:49:36 -07003694
Christoph Lameter205ab992008-04-14 19:11:40 +03003695 for_each_possible_cpu(cpu) {
3696 struct kmem_cache_cpu *c = get_cpu_slab(s, cpu);
Christoph Lameterdfb4f092007-10-16 01:26:05 -07003697
Christoph Lameter205ab992008-04-14 19:11:40 +03003698 if (!c || c->node < 0)
3699 continue;
3700
3701 if (c->page) {
3702 if (flags & SO_TOTAL)
3703 x = c->page->objects;
3704 else if (flags & SO_OBJECTS)
3705 x = c->page->inuse;
Christoph Lameter81819f02007-05-06 14:49:36 -07003706 else
3707 x = 1;
Christoph Lameter205ab992008-04-14 19:11:40 +03003708
Christoph Lameter81819f02007-05-06 14:49:36 -07003709 total += x;
Christoph Lameter205ab992008-04-14 19:11:40 +03003710 nodes[c->node] += x;
Christoph Lameter81819f02007-05-06 14:49:36 -07003711 }
Christoph Lameter205ab992008-04-14 19:11:40 +03003712 per_cpu[c->node]++;
Christoph Lameter81819f02007-05-06 14:49:36 -07003713 }
3714 }
3715
Christoph Lameter205ab992008-04-14 19:11:40 +03003716 if (flags & SO_ALL) {
3717 for_each_node_state(node, N_NORMAL_MEMORY) {
3718 struct kmem_cache_node *n = get_node(s, node);
Christoph Lameter81819f02007-05-06 14:49:36 -07003719
Christoph Lameter205ab992008-04-14 19:11:40 +03003720 if (flags & SO_TOTAL)
3721 x = atomic_long_read(&n->total_objects);
3722 else if (flags & SO_OBJECTS)
3723 x = atomic_long_read(&n->total_objects) -
3724 count_partial(n, count_free);
3725
3726 else
3727 x = atomic_long_read(&n->nr_slabs);
3728 total += x;
3729 nodes[node] += x;
3730 }
3731
3732 } else if (flags & SO_PARTIAL) {
3733 for_each_node_state(node, N_NORMAL_MEMORY) {
3734 struct kmem_cache_node *n = get_node(s, node);
3735
3736 if (flags & SO_TOTAL)
3737 x = count_partial(n, count_total);
3738 else if (flags & SO_OBJECTS)
3739 x = count_partial(n, count_inuse);
Christoph Lameter81819f02007-05-06 14:49:36 -07003740 else
3741 x = n->nr_partial;
3742 total += x;
3743 nodes[node] += x;
3744 }
Christoph Lameter81819f02007-05-06 14:49:36 -07003745 }
Christoph Lameter81819f02007-05-06 14:49:36 -07003746 x = sprintf(buf, "%lu", total);
3747#ifdef CONFIG_NUMA
Christoph Lameterf64dc582007-10-16 01:25:33 -07003748 for_each_node_state(node, N_NORMAL_MEMORY)
Christoph Lameter81819f02007-05-06 14:49:36 -07003749 if (nodes[node])
3750 x += sprintf(buf + x, " N%d=%lu",
3751 node, nodes[node]);
3752#endif
3753 kfree(nodes);
3754 return x + sprintf(buf + x, "\n");
3755}
3756
3757static int any_slab_objects(struct kmem_cache *s)
3758{
3759 int node;
Christoph Lameterdfb4f092007-10-16 01:26:05 -07003760
3761 for_each_online_node(node) {
Christoph Lameter81819f02007-05-06 14:49:36 -07003762 struct kmem_cache_node *n = get_node(s, node);
3763
Christoph Lameterdfb4f092007-10-16 01:26:05 -07003764 if (!n)
3765 continue;
3766
Christoph Lameter31d33ba2008-04-14 19:11:41 +03003767 if (atomic_read(&n->total_objects))
Christoph Lameter81819f02007-05-06 14:49:36 -07003768 return 1;
3769 }
3770 return 0;
3771}
3772
3773#define to_slab_attr(n) container_of(n, struct slab_attribute, attr)
3774#define to_slab(n) container_of(n, struct kmem_cache, kobj);
3775
3776struct slab_attribute {
3777 struct attribute attr;
3778 ssize_t (*show)(struct kmem_cache *s, char *buf);
3779 ssize_t (*store)(struct kmem_cache *s, const char *x, size_t count);
3780};
3781
3782#define SLAB_ATTR_RO(_name) \
3783 static struct slab_attribute _name##_attr = __ATTR_RO(_name)
3784
3785#define SLAB_ATTR(_name) \
3786 static struct slab_attribute _name##_attr = \
3787 __ATTR(_name, 0644, _name##_show, _name##_store)
3788
Christoph Lameter81819f02007-05-06 14:49:36 -07003789static ssize_t slab_size_show(struct kmem_cache *s, char *buf)
3790{
3791 return sprintf(buf, "%d\n", s->size);
3792}
3793SLAB_ATTR_RO(slab_size);
3794
3795static ssize_t align_show(struct kmem_cache *s, char *buf)
3796{
3797 return sprintf(buf, "%d\n", s->align);
3798}
3799SLAB_ATTR_RO(align);
3800
3801static ssize_t object_size_show(struct kmem_cache *s, char *buf)
3802{
3803 return sprintf(buf, "%d\n", s->objsize);
3804}
3805SLAB_ATTR_RO(object_size);
3806
3807static ssize_t objs_per_slab_show(struct kmem_cache *s, char *buf)
3808{
Christoph Lameter834f3d12008-04-14 19:11:31 +03003809 return sprintf(buf, "%d\n", oo_objects(s->oo));
Christoph Lameter81819f02007-05-06 14:49:36 -07003810}
3811SLAB_ATTR_RO(objs_per_slab);
3812
Christoph Lameter06b285d2008-04-14 19:11:41 +03003813static ssize_t order_store(struct kmem_cache *s,
3814 const char *buf, size_t length)
3815{
3816 int order = simple_strtoul(buf, NULL, 10);
3817
3818 if (order > slub_max_order || order < slub_min_order)
3819 return -EINVAL;
3820
3821 calculate_sizes(s, order);
3822 return length;
3823}
3824
Christoph Lameter81819f02007-05-06 14:49:36 -07003825static ssize_t order_show(struct kmem_cache *s, char *buf)
3826{
Christoph Lameter834f3d12008-04-14 19:11:31 +03003827 return sprintf(buf, "%d\n", oo_order(s->oo));
Christoph Lameter81819f02007-05-06 14:49:36 -07003828}
Christoph Lameter06b285d2008-04-14 19:11:41 +03003829SLAB_ATTR(order);
Christoph Lameter81819f02007-05-06 14:49:36 -07003830
3831static ssize_t ctor_show(struct kmem_cache *s, char *buf)
3832{
3833 if (s->ctor) {
3834 int n = sprint_symbol(buf, (unsigned long)s->ctor);
3835
3836 return n + sprintf(buf + n, "\n");
3837 }
3838 return 0;
3839}
3840SLAB_ATTR_RO(ctor);
3841
Christoph Lameter81819f02007-05-06 14:49:36 -07003842static ssize_t aliases_show(struct kmem_cache *s, char *buf)
3843{
3844 return sprintf(buf, "%d\n", s->refcount - 1);
3845}
3846SLAB_ATTR_RO(aliases);
3847
3848static ssize_t slabs_show(struct kmem_cache *s, char *buf)
3849{
Christoph Lameter205ab992008-04-14 19:11:40 +03003850 return show_slab_objects(s, buf, SO_ALL);
Christoph Lameter81819f02007-05-06 14:49:36 -07003851}
3852SLAB_ATTR_RO(slabs);
3853
3854static ssize_t partial_show(struct kmem_cache *s, char *buf)
3855{
Christoph Lameterd9acf4b2008-02-15 15:22:21 -08003856 return show_slab_objects(s, buf, SO_PARTIAL);
Christoph Lameter81819f02007-05-06 14:49:36 -07003857}
3858SLAB_ATTR_RO(partial);
3859
3860static ssize_t cpu_slabs_show(struct kmem_cache *s, char *buf)
3861{
Christoph Lameterd9acf4b2008-02-15 15:22:21 -08003862 return show_slab_objects(s, buf, SO_CPU);
Christoph Lameter81819f02007-05-06 14:49:36 -07003863}
3864SLAB_ATTR_RO(cpu_slabs);
3865
3866static ssize_t objects_show(struct kmem_cache *s, char *buf)
3867{
Christoph Lameter205ab992008-04-14 19:11:40 +03003868 return show_slab_objects(s, buf, SO_ALL|SO_OBJECTS);
Christoph Lameter81819f02007-05-06 14:49:36 -07003869}
3870SLAB_ATTR_RO(objects);
3871
Christoph Lameter205ab992008-04-14 19:11:40 +03003872static ssize_t objects_partial_show(struct kmem_cache *s, char *buf)
3873{
3874 return show_slab_objects(s, buf, SO_PARTIAL|SO_OBJECTS);
3875}
3876SLAB_ATTR_RO(objects_partial);
3877
3878static ssize_t total_objects_show(struct kmem_cache *s, char *buf)
3879{
3880 return show_slab_objects(s, buf, SO_ALL|SO_TOTAL);
3881}
3882SLAB_ATTR_RO(total_objects);
3883
Christoph Lameter81819f02007-05-06 14:49:36 -07003884static ssize_t sanity_checks_show(struct kmem_cache *s, char *buf)
3885{
3886 return sprintf(buf, "%d\n", !!(s->flags & SLAB_DEBUG_FREE));
3887}
3888
3889static ssize_t sanity_checks_store(struct kmem_cache *s,
3890 const char *buf, size_t length)
3891{
3892 s->flags &= ~SLAB_DEBUG_FREE;
3893 if (buf[0] == '1')
3894 s->flags |= SLAB_DEBUG_FREE;
3895 return length;
3896}
3897SLAB_ATTR(sanity_checks);
3898
3899static ssize_t trace_show(struct kmem_cache *s, char *buf)
3900{
3901 return sprintf(buf, "%d\n", !!(s->flags & SLAB_TRACE));
3902}
3903
3904static ssize_t trace_store(struct kmem_cache *s, const char *buf,
3905 size_t length)
3906{
3907 s->flags &= ~SLAB_TRACE;
3908 if (buf[0] == '1')
3909 s->flags |= SLAB_TRACE;
3910 return length;
3911}
3912SLAB_ATTR(trace);
3913
3914static ssize_t reclaim_account_show(struct kmem_cache *s, char *buf)
3915{
3916 return sprintf(buf, "%d\n", !!(s->flags & SLAB_RECLAIM_ACCOUNT));
3917}
3918
3919static ssize_t reclaim_account_store(struct kmem_cache *s,
3920 const char *buf, size_t length)
3921{
3922 s->flags &= ~SLAB_RECLAIM_ACCOUNT;
3923 if (buf[0] == '1')
3924 s->flags |= SLAB_RECLAIM_ACCOUNT;
3925 return length;
3926}
3927SLAB_ATTR(reclaim_account);
3928
3929static ssize_t hwcache_align_show(struct kmem_cache *s, char *buf)
3930{
Christoph Lameter5af60832007-05-06 14:49:56 -07003931 return sprintf(buf, "%d\n", !!(s->flags & SLAB_HWCACHE_ALIGN));
Christoph Lameter81819f02007-05-06 14:49:36 -07003932}
3933SLAB_ATTR_RO(hwcache_align);
3934
3935#ifdef CONFIG_ZONE_DMA
3936static ssize_t cache_dma_show(struct kmem_cache *s, char *buf)
3937{
3938 return sprintf(buf, "%d\n", !!(s->flags & SLAB_CACHE_DMA));
3939}
3940SLAB_ATTR_RO(cache_dma);
3941#endif
3942
3943static ssize_t destroy_by_rcu_show(struct kmem_cache *s, char *buf)
3944{
3945 return sprintf(buf, "%d\n", !!(s->flags & SLAB_DESTROY_BY_RCU));
3946}
3947SLAB_ATTR_RO(destroy_by_rcu);
3948
3949static ssize_t red_zone_show(struct kmem_cache *s, char *buf)
3950{
3951 return sprintf(buf, "%d\n", !!(s->flags & SLAB_RED_ZONE));
3952}
3953
3954static ssize_t red_zone_store(struct kmem_cache *s,
3955 const char *buf, size_t length)
3956{
3957 if (any_slab_objects(s))
3958 return -EBUSY;
3959
3960 s->flags &= ~SLAB_RED_ZONE;
3961 if (buf[0] == '1')
3962 s->flags |= SLAB_RED_ZONE;
Christoph Lameter06b285d2008-04-14 19:11:41 +03003963 calculate_sizes(s, -1);
Christoph Lameter81819f02007-05-06 14:49:36 -07003964 return length;
3965}
3966SLAB_ATTR(red_zone);
3967
3968static ssize_t poison_show(struct kmem_cache *s, char *buf)
3969{
3970 return sprintf(buf, "%d\n", !!(s->flags & SLAB_POISON));
3971}
3972
3973static ssize_t poison_store(struct kmem_cache *s,
3974 const char *buf, size_t length)
3975{
3976 if (any_slab_objects(s))
3977 return -EBUSY;
3978
3979 s->flags &= ~SLAB_POISON;
3980 if (buf[0] == '1')
3981 s->flags |= SLAB_POISON;
Christoph Lameter06b285d2008-04-14 19:11:41 +03003982 calculate_sizes(s, -1);
Christoph Lameter81819f02007-05-06 14:49:36 -07003983 return length;
3984}
3985SLAB_ATTR(poison);
3986
3987static ssize_t store_user_show(struct kmem_cache *s, char *buf)
3988{
3989 return sprintf(buf, "%d\n", !!(s->flags & SLAB_STORE_USER));
3990}
3991
3992static ssize_t store_user_store(struct kmem_cache *s,
3993 const char *buf, size_t length)
3994{
3995 if (any_slab_objects(s))
3996 return -EBUSY;
3997
3998 s->flags &= ~SLAB_STORE_USER;
3999 if (buf[0] == '1')
4000 s->flags |= SLAB_STORE_USER;
Christoph Lameter06b285d2008-04-14 19:11:41 +03004001 calculate_sizes(s, -1);
Christoph Lameter81819f02007-05-06 14:49:36 -07004002 return length;
4003}
4004SLAB_ATTR(store_user);
4005
Christoph Lameter53e15af2007-05-06 14:49:43 -07004006static ssize_t validate_show(struct kmem_cache *s, char *buf)
4007{
4008 return 0;
4009}
4010
4011static ssize_t validate_store(struct kmem_cache *s,
4012 const char *buf, size_t length)
4013{
Christoph Lameter434e2452007-07-17 04:03:30 -07004014 int ret = -EINVAL;
4015
4016 if (buf[0] == '1') {
4017 ret = validate_slab_cache(s);
4018 if (ret >= 0)
4019 ret = length;
4020 }
4021 return ret;
Christoph Lameter53e15af2007-05-06 14:49:43 -07004022}
4023SLAB_ATTR(validate);
4024
Christoph Lameter2086d262007-05-06 14:49:46 -07004025static ssize_t shrink_show(struct kmem_cache *s, char *buf)
4026{
4027 return 0;
4028}
4029
4030static ssize_t shrink_store(struct kmem_cache *s,
4031 const char *buf, size_t length)
4032{
4033 if (buf[0] == '1') {
4034 int rc = kmem_cache_shrink(s);
4035
4036 if (rc)
4037 return rc;
4038 } else
4039 return -EINVAL;
4040 return length;
4041}
4042SLAB_ATTR(shrink);
4043
Christoph Lameter88a420e2007-05-06 14:49:45 -07004044static ssize_t alloc_calls_show(struct kmem_cache *s, char *buf)
4045{
4046 if (!(s->flags & SLAB_STORE_USER))
4047 return -ENOSYS;
4048 return list_locations(s, buf, TRACK_ALLOC);
4049}
4050SLAB_ATTR_RO(alloc_calls);
4051
4052static ssize_t free_calls_show(struct kmem_cache *s, char *buf)
4053{
4054 if (!(s->flags & SLAB_STORE_USER))
4055 return -ENOSYS;
4056 return list_locations(s, buf, TRACK_FREE);
4057}
4058SLAB_ATTR_RO(free_calls);
4059
Christoph Lameter81819f02007-05-06 14:49:36 -07004060#ifdef CONFIG_NUMA
Christoph Lameter98246012008-01-07 23:20:26 -08004061static ssize_t remote_node_defrag_ratio_show(struct kmem_cache *s, char *buf)
Christoph Lameter81819f02007-05-06 14:49:36 -07004062{
Christoph Lameter98246012008-01-07 23:20:26 -08004063 return sprintf(buf, "%d\n", s->remote_node_defrag_ratio / 10);
Christoph Lameter81819f02007-05-06 14:49:36 -07004064}
4065
Christoph Lameter98246012008-01-07 23:20:26 -08004066static ssize_t remote_node_defrag_ratio_store(struct kmem_cache *s,
Christoph Lameter81819f02007-05-06 14:49:36 -07004067 const char *buf, size_t length)
4068{
4069 int n = simple_strtoul(buf, NULL, 10);
4070
4071 if (n < 100)
Christoph Lameter98246012008-01-07 23:20:26 -08004072 s->remote_node_defrag_ratio = n * 10;
Christoph Lameter81819f02007-05-06 14:49:36 -07004073 return length;
4074}
Christoph Lameter98246012008-01-07 23:20:26 -08004075SLAB_ATTR(remote_node_defrag_ratio);
Christoph Lameter81819f02007-05-06 14:49:36 -07004076#endif
4077
Christoph Lameter8ff12cf2008-02-07 17:47:41 -08004078#ifdef CONFIG_SLUB_STATS
Christoph Lameter8ff12cf2008-02-07 17:47:41 -08004079static int show_stat(struct kmem_cache *s, char *buf, enum stat_item si)
4080{
4081 unsigned long sum = 0;
4082 int cpu;
4083 int len;
4084 int *data = kmalloc(nr_cpu_ids * sizeof(int), GFP_KERNEL);
4085
4086 if (!data)
4087 return -ENOMEM;
4088
4089 for_each_online_cpu(cpu) {
4090 unsigned x = get_cpu_slab(s, cpu)->stat[si];
4091
4092 data[cpu] = x;
4093 sum += x;
4094 }
4095
4096 len = sprintf(buf, "%lu", sum);
4097
Christoph Lameter50ef37b2008-04-14 18:52:05 +03004098#ifdef CONFIG_SMP
Christoph Lameter8ff12cf2008-02-07 17:47:41 -08004099 for_each_online_cpu(cpu) {
4100 if (data[cpu] && len < PAGE_SIZE - 20)
Christoph Lameter50ef37b2008-04-14 18:52:05 +03004101 len += sprintf(buf + len, " C%d=%u", cpu, data[cpu]);
Christoph Lameter8ff12cf2008-02-07 17:47:41 -08004102 }
Christoph Lameter50ef37b2008-04-14 18:52:05 +03004103#endif
Christoph Lameter8ff12cf2008-02-07 17:47:41 -08004104 kfree(data);
4105 return len + sprintf(buf + len, "\n");
4106}
4107
4108#define STAT_ATTR(si, text) \
4109static ssize_t text##_show(struct kmem_cache *s, char *buf) \
4110{ \
4111 return show_stat(s, buf, si); \
4112} \
4113SLAB_ATTR_RO(text); \
4114
4115STAT_ATTR(ALLOC_FASTPATH, alloc_fastpath);
4116STAT_ATTR(ALLOC_SLOWPATH, alloc_slowpath);
4117STAT_ATTR(FREE_FASTPATH, free_fastpath);
4118STAT_ATTR(FREE_SLOWPATH, free_slowpath);
4119STAT_ATTR(FREE_FROZEN, free_frozen);
4120STAT_ATTR(FREE_ADD_PARTIAL, free_add_partial);
4121STAT_ATTR(FREE_REMOVE_PARTIAL, free_remove_partial);
4122STAT_ATTR(ALLOC_FROM_PARTIAL, alloc_from_partial);
4123STAT_ATTR(ALLOC_SLAB, alloc_slab);
4124STAT_ATTR(ALLOC_REFILL, alloc_refill);
4125STAT_ATTR(FREE_SLAB, free_slab);
4126STAT_ATTR(CPUSLAB_FLUSH, cpuslab_flush);
4127STAT_ATTR(DEACTIVATE_FULL, deactivate_full);
4128STAT_ATTR(DEACTIVATE_EMPTY, deactivate_empty);
4129STAT_ATTR(DEACTIVATE_TO_HEAD, deactivate_to_head);
4130STAT_ATTR(DEACTIVATE_TO_TAIL, deactivate_to_tail);
4131STAT_ATTR(DEACTIVATE_REMOTE_FREES, deactivate_remote_frees);
Christoph Lameter65c33762008-04-14 19:11:40 +03004132STAT_ATTR(ORDER_FALLBACK, order_fallback);
Christoph Lameter8ff12cf2008-02-07 17:47:41 -08004133#endif
4134
Pekka Enberg06428782008-01-07 23:20:27 -08004135static struct attribute *slab_attrs[] = {
Christoph Lameter81819f02007-05-06 14:49:36 -07004136 &slab_size_attr.attr,
4137 &object_size_attr.attr,
4138 &objs_per_slab_attr.attr,
4139 &order_attr.attr,
4140 &objects_attr.attr,
Christoph Lameter205ab992008-04-14 19:11:40 +03004141 &objects_partial_attr.attr,
4142 &total_objects_attr.attr,
Christoph Lameter81819f02007-05-06 14:49:36 -07004143 &slabs_attr.attr,
4144 &partial_attr.attr,
4145 &cpu_slabs_attr.attr,
4146 &ctor_attr.attr,
Christoph Lameter81819f02007-05-06 14:49:36 -07004147 &aliases_attr.attr,
4148 &align_attr.attr,
4149 &sanity_checks_attr.attr,
4150 &trace_attr.attr,
4151 &hwcache_align_attr.attr,
4152 &reclaim_account_attr.attr,
4153 &destroy_by_rcu_attr.attr,
4154 &red_zone_attr.attr,
4155 &poison_attr.attr,
4156 &store_user_attr.attr,
Christoph Lameter53e15af2007-05-06 14:49:43 -07004157 &validate_attr.attr,
Christoph Lameter2086d262007-05-06 14:49:46 -07004158 &shrink_attr.attr,
Christoph Lameter88a420e2007-05-06 14:49:45 -07004159 &alloc_calls_attr.attr,
4160 &free_calls_attr.attr,
Christoph Lameter81819f02007-05-06 14:49:36 -07004161#ifdef CONFIG_ZONE_DMA
4162 &cache_dma_attr.attr,
4163#endif
4164#ifdef CONFIG_NUMA
Christoph Lameter98246012008-01-07 23:20:26 -08004165 &remote_node_defrag_ratio_attr.attr,
Christoph Lameter81819f02007-05-06 14:49:36 -07004166#endif
Christoph Lameter8ff12cf2008-02-07 17:47:41 -08004167#ifdef CONFIG_SLUB_STATS
4168 &alloc_fastpath_attr.attr,
4169 &alloc_slowpath_attr.attr,
4170 &free_fastpath_attr.attr,
4171 &free_slowpath_attr.attr,
4172 &free_frozen_attr.attr,
4173 &free_add_partial_attr.attr,
4174 &free_remove_partial_attr.attr,
4175 &alloc_from_partial_attr.attr,
4176 &alloc_slab_attr.attr,
4177 &alloc_refill_attr.attr,
4178 &free_slab_attr.attr,
4179 &cpuslab_flush_attr.attr,
4180 &deactivate_full_attr.attr,
4181 &deactivate_empty_attr.attr,
4182 &deactivate_to_head_attr.attr,
4183 &deactivate_to_tail_attr.attr,
4184 &deactivate_remote_frees_attr.attr,
Christoph Lameter65c33762008-04-14 19:11:40 +03004185 &order_fallback_attr.attr,
Christoph Lameter8ff12cf2008-02-07 17:47:41 -08004186#endif
Christoph Lameter81819f02007-05-06 14:49:36 -07004187 NULL
4188};
4189
4190static struct attribute_group slab_attr_group = {
4191 .attrs = slab_attrs,
4192};
4193
4194static ssize_t slab_attr_show(struct kobject *kobj,
4195 struct attribute *attr,
4196 char *buf)
4197{
4198 struct slab_attribute *attribute;
4199 struct kmem_cache *s;
4200 int err;
4201
4202 attribute = to_slab_attr(attr);
4203 s = to_slab(kobj);
4204
4205 if (!attribute->show)
4206 return -EIO;
4207
4208 err = attribute->show(s, buf);
4209
4210 return err;
4211}
4212
4213static ssize_t slab_attr_store(struct kobject *kobj,
4214 struct attribute *attr,
4215 const char *buf, size_t len)
4216{
4217 struct slab_attribute *attribute;
4218 struct kmem_cache *s;
4219 int err;
4220
4221 attribute = to_slab_attr(attr);
4222 s = to_slab(kobj);
4223
4224 if (!attribute->store)
4225 return -EIO;
4226
4227 err = attribute->store(s, buf, len);
4228
4229 return err;
4230}
4231
Christoph Lameter151c6022008-01-07 22:29:05 -08004232static void kmem_cache_release(struct kobject *kobj)
4233{
4234 struct kmem_cache *s = to_slab(kobj);
4235
4236 kfree(s);
4237}
4238
Christoph Lameter81819f02007-05-06 14:49:36 -07004239static struct sysfs_ops slab_sysfs_ops = {
4240 .show = slab_attr_show,
4241 .store = slab_attr_store,
4242};
4243
4244static struct kobj_type slab_ktype = {
4245 .sysfs_ops = &slab_sysfs_ops,
Christoph Lameter151c6022008-01-07 22:29:05 -08004246 .release = kmem_cache_release
Christoph Lameter81819f02007-05-06 14:49:36 -07004247};
4248
4249static int uevent_filter(struct kset *kset, struct kobject *kobj)
4250{
4251 struct kobj_type *ktype = get_ktype(kobj);
4252
4253 if (ktype == &slab_ktype)
4254 return 1;
4255 return 0;
4256}
4257
4258static struct kset_uevent_ops slab_uevent_ops = {
4259 .filter = uevent_filter,
4260};
4261
Greg Kroah-Hartman27c3a312007-11-01 09:29:06 -06004262static struct kset *slab_kset;
Christoph Lameter81819f02007-05-06 14:49:36 -07004263
4264#define ID_STR_LENGTH 64
4265
4266/* Create a unique string id for a slab cache:
Christoph Lameter6446faa2008-02-15 23:45:26 -08004267 *
4268 * Format :[flags-]size
Christoph Lameter81819f02007-05-06 14:49:36 -07004269 */
4270static char *create_unique_id(struct kmem_cache *s)
4271{
4272 char *name = kmalloc(ID_STR_LENGTH, GFP_KERNEL);
4273 char *p = name;
4274
4275 BUG_ON(!name);
4276
4277 *p++ = ':';
4278 /*
4279 * First flags affecting slabcache operations. We will only
4280 * get here for aliasable slabs so we do not need to support
4281 * too many flags. The flags here must cover all flags that
4282 * are matched during merging to guarantee that the id is
4283 * unique.
4284 */
4285 if (s->flags & SLAB_CACHE_DMA)
4286 *p++ = 'd';
4287 if (s->flags & SLAB_RECLAIM_ACCOUNT)
4288 *p++ = 'a';
4289 if (s->flags & SLAB_DEBUG_FREE)
4290 *p++ = 'F';
4291 if (p != name + 1)
4292 *p++ = '-';
4293 p += sprintf(p, "%07d", s->size);
4294 BUG_ON(p > name + ID_STR_LENGTH - 1);
4295 return name;
4296}
4297
4298static int sysfs_slab_add(struct kmem_cache *s)
4299{
4300 int err;
4301 const char *name;
4302 int unmergeable;
4303
4304 if (slab_state < SYSFS)
4305 /* Defer until later */
4306 return 0;
4307
4308 unmergeable = slab_unmergeable(s);
4309 if (unmergeable) {
4310 /*
4311 * Slabcache can never be merged so we can use the name proper.
4312 * This is typically the case for debug situations. In that
4313 * case we can catch duplicate names easily.
4314 */
Greg Kroah-Hartman27c3a312007-11-01 09:29:06 -06004315 sysfs_remove_link(&slab_kset->kobj, s->name);
Christoph Lameter81819f02007-05-06 14:49:36 -07004316 name = s->name;
4317 } else {
4318 /*
4319 * Create a unique name for the slab as a target
4320 * for the symlinks.
4321 */
4322 name = create_unique_id(s);
4323 }
4324
Greg Kroah-Hartman27c3a312007-11-01 09:29:06 -06004325 s->kobj.kset = slab_kset;
Greg Kroah-Hartman1eada112007-12-17 23:05:35 -07004326 err = kobject_init_and_add(&s->kobj, &slab_ktype, NULL, name);
4327 if (err) {
4328 kobject_put(&s->kobj);
Christoph Lameter81819f02007-05-06 14:49:36 -07004329 return err;
Greg Kroah-Hartman1eada112007-12-17 23:05:35 -07004330 }
Christoph Lameter81819f02007-05-06 14:49:36 -07004331
4332 err = sysfs_create_group(&s->kobj, &slab_attr_group);
4333 if (err)
4334 return err;
4335 kobject_uevent(&s->kobj, KOBJ_ADD);
4336 if (!unmergeable) {
4337 /* Setup first alias */
4338 sysfs_slab_alias(s, s->name);
4339 kfree(name);
4340 }
4341 return 0;
4342}
4343
4344static void sysfs_slab_remove(struct kmem_cache *s)
4345{
4346 kobject_uevent(&s->kobj, KOBJ_REMOVE);
4347 kobject_del(&s->kobj);
Christoph Lameter151c6022008-01-07 22:29:05 -08004348 kobject_put(&s->kobj);
Christoph Lameter81819f02007-05-06 14:49:36 -07004349}
4350
4351/*
4352 * Need to buffer aliases during bootup until sysfs becomes
4353 * available lest we loose that information.
4354 */
4355struct saved_alias {
4356 struct kmem_cache *s;
4357 const char *name;
4358 struct saved_alias *next;
4359};
4360
Adrian Bunk5af328a2007-07-17 04:03:27 -07004361static struct saved_alias *alias_list;
Christoph Lameter81819f02007-05-06 14:49:36 -07004362
4363static int sysfs_slab_alias(struct kmem_cache *s, const char *name)
4364{
4365 struct saved_alias *al;
4366
4367 if (slab_state == SYSFS) {
4368 /*
4369 * If we have a leftover link then remove it.
4370 */
Greg Kroah-Hartman27c3a312007-11-01 09:29:06 -06004371 sysfs_remove_link(&slab_kset->kobj, name);
4372 return sysfs_create_link(&slab_kset->kobj, &s->kobj, name);
Christoph Lameter81819f02007-05-06 14:49:36 -07004373 }
4374
4375 al = kmalloc(sizeof(struct saved_alias), GFP_KERNEL);
4376 if (!al)
4377 return -ENOMEM;
4378
4379 al->s = s;
4380 al->name = name;
4381 al->next = alias_list;
4382 alias_list = al;
4383 return 0;
4384}
4385
4386static int __init slab_sysfs_init(void)
4387{
Christoph Lameter5b95a4a2007-07-17 04:03:19 -07004388 struct kmem_cache *s;
Christoph Lameter81819f02007-05-06 14:49:36 -07004389 int err;
4390
Greg Kroah-Hartman0ff21e42007-11-06 10:36:58 -08004391 slab_kset = kset_create_and_add("slab", &slab_uevent_ops, kernel_kobj);
Greg Kroah-Hartman27c3a312007-11-01 09:29:06 -06004392 if (!slab_kset) {
Christoph Lameter81819f02007-05-06 14:49:36 -07004393 printk(KERN_ERR "Cannot register slab subsystem.\n");
4394 return -ENOSYS;
4395 }
4396
Christoph Lameter26a7bd02007-05-09 02:32:39 -07004397 slab_state = SYSFS;
4398
Christoph Lameter5b95a4a2007-07-17 04:03:19 -07004399 list_for_each_entry(s, &slab_caches, list) {
Christoph Lameter26a7bd02007-05-09 02:32:39 -07004400 err = sysfs_slab_add(s);
Christoph Lameter5d540fb2007-08-30 23:56:26 -07004401 if (err)
4402 printk(KERN_ERR "SLUB: Unable to add boot slab %s"
4403 " to sysfs\n", s->name);
Christoph Lameter26a7bd02007-05-09 02:32:39 -07004404 }
Christoph Lameter81819f02007-05-06 14:49:36 -07004405
4406 while (alias_list) {
4407 struct saved_alias *al = alias_list;
4408
4409 alias_list = alias_list->next;
4410 err = sysfs_slab_alias(al->s, al->name);
Christoph Lameter5d540fb2007-08-30 23:56:26 -07004411 if (err)
4412 printk(KERN_ERR "SLUB: Unable to add boot slab alias"
4413 " %s to sysfs\n", s->name);
Christoph Lameter81819f02007-05-06 14:49:36 -07004414 kfree(al);
4415 }
4416
4417 resiliency_test();
4418 return 0;
4419}
4420
4421__initcall(slab_sysfs_init);
Christoph Lameter81819f02007-05-06 14:49:36 -07004422#endif
Pekka J Enberg57ed3ed2008-01-01 17:23:28 +01004423
4424/*
4425 * The /proc/slabinfo ABI
4426 */
Linus Torvalds158a9622008-01-02 13:04:48 -08004427#ifdef CONFIG_SLABINFO
4428
4429ssize_t slabinfo_write(struct file *file, const char __user * buffer,
4430 size_t count, loff_t *ppos)
4431{
4432 return -EINVAL;
4433}
4434
Pekka J Enberg57ed3ed2008-01-01 17:23:28 +01004435
4436static void print_slabinfo_header(struct seq_file *m)
4437{
4438 seq_puts(m, "slabinfo - version: 2.1\n");
4439 seq_puts(m, "# name <active_objs> <num_objs> <objsize> "
4440 "<objperslab> <pagesperslab>");
4441 seq_puts(m, " : tunables <limit> <batchcount> <sharedfactor>");
4442 seq_puts(m, " : slabdata <active_slabs> <num_slabs> <sharedavail>");
4443 seq_putc(m, '\n');
4444}
4445
4446static void *s_start(struct seq_file *m, loff_t *pos)
4447{
4448 loff_t n = *pos;
4449
4450 down_read(&slub_lock);
4451 if (!n)
4452 print_slabinfo_header(m);
4453
4454 return seq_list_start(&slab_caches, *pos);
4455}
4456
4457static void *s_next(struct seq_file *m, void *p, loff_t *pos)
4458{
4459 return seq_list_next(p, &slab_caches, pos);
4460}
4461
4462static void s_stop(struct seq_file *m, void *p)
4463{
4464 up_read(&slub_lock);
4465}
4466
4467static int s_show(struct seq_file *m, void *p)
4468{
4469 unsigned long nr_partials = 0;
4470 unsigned long nr_slabs = 0;
4471 unsigned long nr_inuse = 0;
Christoph Lameter205ab992008-04-14 19:11:40 +03004472 unsigned long nr_objs = 0;
4473 unsigned long nr_free = 0;
Pekka J Enberg57ed3ed2008-01-01 17:23:28 +01004474 struct kmem_cache *s;
4475 int node;
4476
4477 s = list_entry(p, struct kmem_cache, list);
4478
4479 for_each_online_node(node) {
4480 struct kmem_cache_node *n = get_node(s, node);
4481
4482 if (!n)
4483 continue;
4484
4485 nr_partials += n->nr_partial;
4486 nr_slabs += atomic_long_read(&n->nr_slabs);
Christoph Lameter205ab992008-04-14 19:11:40 +03004487 nr_objs += atomic_long_read(&n->total_objects);
4488 nr_free += count_partial(n, count_free);
Pekka J Enberg57ed3ed2008-01-01 17:23:28 +01004489 }
4490
Christoph Lameter205ab992008-04-14 19:11:40 +03004491 nr_inuse = nr_objs - nr_free;
Pekka J Enberg57ed3ed2008-01-01 17:23:28 +01004492
4493 seq_printf(m, "%-17s %6lu %6lu %6u %4u %4d", s->name, nr_inuse,
Christoph Lameter834f3d12008-04-14 19:11:31 +03004494 nr_objs, s->size, oo_objects(s->oo),
4495 (1 << oo_order(s->oo)));
Pekka J Enberg57ed3ed2008-01-01 17:23:28 +01004496 seq_printf(m, " : tunables %4u %4u %4u", 0, 0, 0);
4497 seq_printf(m, " : slabdata %6lu %6lu %6lu", nr_slabs, nr_slabs,
4498 0UL);
4499 seq_putc(m, '\n');
4500 return 0;
4501}
4502
4503const struct seq_operations slabinfo_op = {
4504 .start = s_start,
4505 .next = s_next,
4506 .stop = s_stop,
4507 .show = s_show,
4508};
4509
Linus Torvalds158a9622008-01-02 13:04:48 -08004510#endif /* CONFIG_SLABINFO */