blob: a5a4ecf7e39350fcc39ef580aa667a2ef05df73e [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 *
Christoph Lametercde53532008-07-04 09:59:22 -07008 * (C) 2007 SGI, Christoph Lameter
Christoph Lameter81819f02007-05-06 14:49:36 -07009 */
10
11#include <linux/mm.h>
Nick Piggin1eb5ac62009-05-05 19:13:44 +100012#include <linux/swap.h> /* struct reclaim_state */
Christoph Lameter81819f02007-05-06 14:49:36 -070013#include <linux/module.h>
14#include <linux/bit_spinlock.h>
15#include <linux/interrupt.h>
16#include <linux/bitops.h>
17#include <linux/slab.h>
Alexey Dobriyan7b3c3a52008-10-06 02:42:17 +040018#include <linux/proc_fs.h>
Christoph Lameter81819f02007-05-06 14:49:36 -070019#include <linux/seq_file.h>
Frederic Weisbecker36994e52008-12-29 13:42:23 -080020#include <trace/kmemtrace.h>
Christoph Lameter81819f02007-05-06 14:49:36 -070021#include <linux/cpu.h>
22#include <linux/cpuset.h>
23#include <linux/mempolicy.h>
24#include <linux/ctype.h>
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -070025#include <linux/debugobjects.h>
Christoph Lameter81819f02007-05-06 14:49:36 -070026#include <linux/kallsyms.h>
Yasunori Gotob9049e22007-10-21 16:41:37 -070027#include <linux/memory.h>
Roman Zippelf8bd2252008-05-01 04:34:31 -070028#include <linux/math64.h>
Akinobu Mita773ff602008-12-23 19:37:01 +090029#include <linux/fault-inject.h>
Christoph Lameter81819f02007-05-06 14:49:36 -070030
31/*
32 * Lock order:
33 * 1. slab_lock(page)
34 * 2. slab->list_lock
35 *
36 * The slab_lock protects operations on the object of a particular
37 * slab and its metadata in the page struct. If the slab lock
38 * has been taken then no allocations nor frees can be performed
39 * on the objects in the slab nor can the slab be added or removed
40 * from the partial or full lists since this would mean modifying
41 * the page_struct of the slab.
42 *
43 * The list_lock protects the partial and full list on each node and
44 * the partial slab counter. If taken then no new slabs may be added or
45 * removed from the lists nor make the number of partial slabs be modified.
46 * (Note that the total number of slabs is an atomic value that may be
47 * modified without taking the list lock).
48 *
49 * The list_lock is a centralized lock and thus we avoid taking it as
50 * much as possible. As long as SLUB does not have to handle partial
51 * slabs, operations can continue without any centralized lock. F.e.
52 * allocating a long series of objects that fill up slabs does not require
53 * the list lock.
54 *
55 * The lock order is sometimes inverted when we are trying to get a slab
56 * off a list. We take the list_lock and then look for a page on the list
57 * to use. While we do that objects in the slabs may be freed. We can
58 * only operate on the slab if we have also taken the slab_lock. So we use
59 * a slab_trylock() on the slab. If trylock was successful then no frees
60 * can occur anymore and we can use the slab for allocations etc. If the
61 * slab_trylock() does not succeed then frees are in progress in the slab and
62 * we must stay away from it for a while since we may cause a bouncing
63 * cacheline if we try to acquire the lock. So go onto the next slab.
64 * If all pages are busy then we may allocate a new slab instead of reusing
65 * a partial slab. A new slab has noone operating on it and thus there is
66 * no danger of cacheline contention.
67 *
68 * Interrupts are disabled during allocation and deallocation in order to
69 * make the slab allocator safe to use in the context of an irq. In addition
70 * interrupts are disabled to ensure that the processor does not change
71 * while handling per_cpu slabs, due to kernel preemption.
72 *
73 * SLUB assigns one slab for allocation to each processor.
74 * Allocations only occur from these slabs called cpu slabs.
75 *
Christoph Lameter672bba32007-05-09 02:32:39 -070076 * Slabs with free elements are kept on a partial list and during regular
77 * operations no list for full slabs is used. If an object in a full slab is
Christoph Lameter81819f02007-05-06 14:49:36 -070078 * freed then the slab will show up again on the partial lists.
Christoph Lameter672bba32007-05-09 02:32:39 -070079 * We track full slabs for debugging purposes though because otherwise we
80 * cannot scan all objects.
Christoph Lameter81819f02007-05-06 14:49:36 -070081 *
82 * Slabs are freed when they become empty. Teardown and setup is
83 * minimal so we rely on the page allocators per cpu caches for
84 * fast frees and allocs.
85 *
86 * Overloading of page flags that are otherwise used for LRU management.
87 *
Christoph Lameter4b6f0752007-05-16 22:10:53 -070088 * PageActive The slab is frozen and exempt from list processing.
89 * This means that the slab is dedicated to a purpose
90 * such as satisfying allocations for a specific
91 * processor. Objects may be freed in the slab while
92 * it is frozen but slab_free will then skip the usual
93 * list operations. It is up to the processor holding
94 * the slab to integrate the slab into the slab lists
95 * when the slab is no longer needed.
96 *
97 * One use of this flag is to mark slabs that are
98 * used for allocations. Then such a slab becomes a cpu
99 * slab. The cpu slab may be equipped with an additional
Christoph Lameterdfb4f092007-10-16 01:26:05 -0700100 * freelist that allows lockless access to
Christoph Lameter894b8782007-05-10 03:15:16 -0700101 * free objects in addition to the regular freelist
102 * that requires the slab lock.
Christoph Lameter81819f02007-05-06 14:49:36 -0700103 *
104 * PageError Slab requires special handling due to debug
105 * options set. This moves slab handling out of
Christoph Lameter894b8782007-05-10 03:15:16 -0700106 * the fast path and disables lockless freelists.
Christoph Lameter81819f02007-05-06 14:49:36 -0700107 */
108
Christoph Lameter5577bd82007-05-16 22:10:56 -0700109#ifdef CONFIG_SLUB_DEBUG
Andy Whitcroft8a380822008-07-23 21:27:18 -0700110#define SLABDEBUG 1
Christoph Lameter5577bd82007-05-16 22:10:56 -0700111#else
112#define SLABDEBUG 0
113#endif
114
Christoph Lameter81819f02007-05-06 14:49:36 -0700115/*
116 * Issues still to be resolved:
117 *
Christoph Lameter81819f02007-05-06 14:49:36 -0700118 * - Support PAGE_ALLOC_DEBUG. Should be easy to do.
119 *
Christoph Lameter81819f02007-05-06 14:49:36 -0700120 * - Variable sizing of the per node arrays
121 */
122
123/* Enable to test recovery from slab corruption on boot */
124#undef SLUB_RESILIENCY_TEST
125
Christoph Lameter81819f02007-05-06 14:49:36 -0700126/*
Christoph Lameter2086d262007-05-06 14:49:46 -0700127 * Mininum number of partial slabs. These will be left on the partial
128 * lists even if they are empty. kmem_cache_shrink may reclaim them.
129 */
Christoph Lameter76be8952007-12-21 14:37:37 -0800130#define MIN_PARTIAL 5
Christoph Lametere95eed52007-05-06 14:49:44 -0700131
Christoph Lameter2086d262007-05-06 14:49:46 -0700132/*
133 * Maximum number of desirable partial slabs.
134 * The existence of more partial slabs makes kmem_cache_shrink
135 * sort the partial list by the number of objects in the.
136 */
137#define MAX_PARTIAL 10
138
Christoph Lameter81819f02007-05-06 14:49:36 -0700139#define DEBUG_DEFAULT_FLAGS (SLAB_DEBUG_FREE | SLAB_RED_ZONE | \
140 SLAB_POISON | SLAB_STORE_USER)
Christoph Lameter672bba32007-05-09 02:32:39 -0700141
Christoph Lameter81819f02007-05-06 14:49:36 -0700142/*
143 * Set of flags that will prevent slab merging
144 */
145#define SLUB_NEVER_MERGE (SLAB_RED_ZONE | SLAB_POISON | SLAB_STORE_USER | \
146 SLAB_TRACE | SLAB_DESTROY_BY_RCU)
147
148#define SLUB_MERGE_SAME (SLAB_DEBUG_FREE | SLAB_RECLAIM_ACCOUNT | \
149 SLAB_CACHE_DMA)
150
151#ifndef ARCH_KMALLOC_MINALIGN
Christoph Lameter47bfdc02007-05-06 14:49:37 -0700152#define ARCH_KMALLOC_MINALIGN __alignof__(unsigned long long)
Christoph Lameter81819f02007-05-06 14:49:36 -0700153#endif
154
155#ifndef ARCH_SLAB_MINALIGN
Christoph Lameter47bfdc02007-05-06 14:49:37 -0700156#define ARCH_SLAB_MINALIGN __alignof__(unsigned long long)
Christoph Lameter81819f02007-05-06 14:49:36 -0700157#endif
158
Cyrill Gorcunov210b5c02008-10-22 23:00:38 +0400159#define OO_SHIFT 16
160#define OO_MASK ((1 << OO_SHIFT) - 1)
161#define MAX_OBJS_PER_PAGE 65535 /* since page.objects is u16 */
162
Christoph Lameter81819f02007-05-06 14:49:36 -0700163/* Internal SLUB flags */
Christoph Lameter1ceef402007-08-07 15:11:48 -0700164#define __OBJECT_POISON 0x80000000 /* Poison object */
165#define __SYSFS_ADD_DEFERRED 0x40000000 /* Not yet visible via sysfs */
Christoph Lameter81819f02007-05-06 14:49:36 -0700166
167static int kmem_size = sizeof(struct kmem_cache);
168
169#ifdef CONFIG_SMP
170static struct notifier_block slab_notifier;
171#endif
172
173static enum {
174 DOWN, /* No slab functionality available */
175 PARTIAL, /* kmem_cache_open() works but kmalloc does not */
Christoph Lameter672bba32007-05-09 02:32:39 -0700176 UP, /* Everything works but does not show up in sysfs */
Christoph Lameter81819f02007-05-06 14:49:36 -0700177 SYSFS /* Sysfs up */
178} slab_state = DOWN;
179
180/* A list of all slab caches on the system */
181static DECLARE_RWSEM(slub_lock);
Adrian Bunk5af328a2007-07-17 04:03:27 -0700182static LIST_HEAD(slab_caches);
Christoph Lameter81819f02007-05-06 14:49:36 -0700183
Christoph Lameter02cbc872007-05-09 02:32:43 -0700184/*
185 * Tracking user of a slab.
186 */
187struct track {
Eduard - Gabriel Munteanuce71e272008-08-19 20:43:25 +0300188 unsigned long addr; /* Called from address */
Christoph Lameter02cbc872007-05-09 02:32:43 -0700189 int cpu; /* Was running on cpu */
190 int pid; /* Pid context */
191 unsigned long when; /* When did the operation occur */
192};
193
194enum track_item { TRACK_ALLOC, TRACK_FREE };
195
Christoph Lameterf6acb632008-04-29 16:16:06 -0700196#ifdef CONFIG_SLUB_DEBUG
Christoph Lameter81819f02007-05-06 14:49:36 -0700197static int sysfs_slab_add(struct kmem_cache *);
198static int sysfs_slab_alias(struct kmem_cache *, const char *);
199static void sysfs_slab_remove(struct kmem_cache *);
Christoph Lameter8ff12cf2008-02-07 17:47:41 -0800200
Christoph Lameter81819f02007-05-06 14:49:36 -0700201#else
Christoph Lameter0c710012007-07-17 04:03:24 -0700202static inline int sysfs_slab_add(struct kmem_cache *s) { return 0; }
203static inline int sysfs_slab_alias(struct kmem_cache *s, const char *p)
204 { return 0; }
Christoph Lameter151c6022008-01-07 22:29:05 -0800205static inline void sysfs_slab_remove(struct kmem_cache *s)
206{
207 kfree(s);
208}
Christoph Lameter8ff12cf2008-02-07 17:47:41 -0800209
Christoph Lameter81819f02007-05-06 14:49:36 -0700210#endif
211
Christoph Lameter8ff12cf2008-02-07 17:47:41 -0800212static inline void stat(struct kmem_cache_cpu *c, enum stat_item si)
213{
214#ifdef CONFIG_SLUB_STATS
215 c->stat[si]++;
216#endif
217}
218
Christoph Lameter81819f02007-05-06 14:49:36 -0700219/********************************************************************
220 * Core slab cache functions
221 *******************************************************************/
222
223int slab_is_available(void)
224{
225 return slab_state >= UP;
226}
227
228static inline struct kmem_cache_node *get_node(struct kmem_cache *s, int node)
229{
230#ifdef CONFIG_NUMA
231 return s->node[node];
232#else
233 return &s->local_node;
234#endif
235}
236
Christoph Lameterdfb4f092007-10-16 01:26:05 -0700237static inline struct kmem_cache_cpu *get_cpu_slab(struct kmem_cache *s, int cpu)
238{
Christoph Lameter4c93c3552007-10-16 01:26:08 -0700239#ifdef CONFIG_SMP
240 return s->cpu_slab[cpu];
241#else
242 return &s->cpu_slab;
243#endif
Christoph Lameterdfb4f092007-10-16 01:26:05 -0700244}
245
Christoph Lameter6446faa2008-02-15 23:45:26 -0800246/* Verify that a pointer has an address that is valid within a slab page */
Christoph Lameter02cbc872007-05-09 02:32:43 -0700247static inline int check_valid_pointer(struct kmem_cache *s,
248 struct page *page, const void *object)
249{
250 void *base;
251
Christoph Lametera973e9d2008-03-01 13:40:44 -0800252 if (!object)
Christoph Lameter02cbc872007-05-09 02:32:43 -0700253 return 1;
254
Christoph Lametera973e9d2008-03-01 13:40:44 -0800255 base = page_address(page);
Christoph Lameter39b26462008-04-14 19:11:30 +0300256 if (object < base || object >= base + page->objects * s->size ||
Christoph Lameter02cbc872007-05-09 02:32:43 -0700257 (object - base) % s->size) {
258 return 0;
259 }
260
261 return 1;
262}
263
Christoph Lameter81819f02007-05-06 14:49:36 -0700264/*
Christoph Lameter7656c722007-05-09 02:32:40 -0700265 * Slow version of get and set free pointer.
266 *
267 * This version requires touching the cache lines of kmem_cache which
268 * we avoid to do in the fast alloc free paths. There we obtain the offset
269 * from the page struct.
270 */
271static inline void *get_freepointer(struct kmem_cache *s, void *object)
272{
273 return *(void **)(object + s->offset);
274}
275
276static inline void set_freepointer(struct kmem_cache *s, void *object, void *fp)
277{
278 *(void **)(object + s->offset) = fp;
279}
280
281/* Loop over all objects in a slab */
Christoph Lameter224a88b2008-04-14 19:11:31 +0300282#define for_each_object(__p, __s, __addr, __objects) \
283 for (__p = (__addr); __p < (__addr) + (__objects) * (__s)->size;\
Christoph Lameter7656c722007-05-09 02:32:40 -0700284 __p += (__s)->size)
285
286/* Scan freelist */
287#define for_each_free_object(__p, __s, __free) \
Christoph Lametera973e9d2008-03-01 13:40:44 -0800288 for (__p = (__free); __p; __p = get_freepointer((__s), __p))
Christoph Lameter7656c722007-05-09 02:32:40 -0700289
290/* Determine object index from a given position */
291static inline int slab_index(void *p, struct kmem_cache *s, void *addr)
292{
293 return (p - addr) / s->size;
294}
295
Christoph Lameter834f3d12008-04-14 19:11:31 +0300296static inline struct kmem_cache_order_objects oo_make(int order,
297 unsigned long size)
298{
299 struct kmem_cache_order_objects x = {
Cyrill Gorcunov210b5c02008-10-22 23:00:38 +0400300 (order << OO_SHIFT) + (PAGE_SIZE << order) / size
Christoph Lameter834f3d12008-04-14 19:11:31 +0300301 };
302
303 return x;
304}
305
306static inline int oo_order(struct kmem_cache_order_objects x)
307{
Cyrill Gorcunov210b5c02008-10-22 23:00:38 +0400308 return x.x >> OO_SHIFT;
Christoph Lameter834f3d12008-04-14 19:11:31 +0300309}
310
311static inline int oo_objects(struct kmem_cache_order_objects x)
312{
Cyrill Gorcunov210b5c02008-10-22 23:00:38 +0400313 return x.x & OO_MASK;
Christoph Lameter834f3d12008-04-14 19:11:31 +0300314}
315
Christoph Lameter41ecc552007-05-09 02:32:44 -0700316#ifdef CONFIG_SLUB_DEBUG
317/*
318 * Debug settings:
319 */
Christoph Lameterf0630ff2007-07-15 23:38:14 -0700320#ifdef CONFIG_SLUB_DEBUG_ON
321static int slub_debug = DEBUG_DEFAULT_FLAGS;
322#else
Christoph Lameter41ecc552007-05-09 02:32:44 -0700323static int slub_debug;
Christoph Lameterf0630ff2007-07-15 23:38:14 -0700324#endif
Christoph Lameter41ecc552007-05-09 02:32:44 -0700325
326static char *slub_debug_slabs;
327
Christoph Lameter7656c722007-05-09 02:32:40 -0700328/*
Christoph Lameter81819f02007-05-06 14:49:36 -0700329 * Object debugging
330 */
331static void print_section(char *text, u8 *addr, unsigned int length)
332{
333 int i, offset;
334 int newline = 1;
335 char ascii[17];
336
337 ascii[16] = 0;
338
339 for (i = 0; i < length; i++) {
340 if (newline) {
Christoph Lameter24922682007-07-17 04:03:18 -0700341 printk(KERN_ERR "%8s 0x%p: ", text, addr + i);
Christoph Lameter81819f02007-05-06 14:49:36 -0700342 newline = 0;
343 }
Pekka Enberg06428782008-01-07 23:20:27 -0800344 printk(KERN_CONT " %02x", addr[i]);
Christoph Lameter81819f02007-05-06 14:49:36 -0700345 offset = i % 16;
346 ascii[offset] = isgraph(addr[i]) ? addr[i] : '.';
347 if (offset == 15) {
Pekka Enberg06428782008-01-07 23:20:27 -0800348 printk(KERN_CONT " %s\n", ascii);
Christoph Lameter81819f02007-05-06 14:49:36 -0700349 newline = 1;
350 }
351 }
352 if (!newline) {
353 i %= 16;
354 while (i < 16) {
Pekka Enberg06428782008-01-07 23:20:27 -0800355 printk(KERN_CONT " ");
Christoph Lameter81819f02007-05-06 14:49:36 -0700356 ascii[i] = ' ';
357 i++;
358 }
Pekka Enberg06428782008-01-07 23:20:27 -0800359 printk(KERN_CONT " %s\n", ascii);
Christoph Lameter81819f02007-05-06 14:49:36 -0700360 }
361}
362
Christoph Lameter81819f02007-05-06 14:49:36 -0700363static struct track *get_track(struct kmem_cache *s, void *object,
364 enum track_item alloc)
365{
366 struct track *p;
367
368 if (s->offset)
369 p = object + s->offset + sizeof(void *);
370 else
371 p = object + s->inuse;
372
373 return p + alloc;
374}
375
376static void set_track(struct kmem_cache *s, void *object,
Eduard - Gabriel Munteanuce71e272008-08-19 20:43:25 +0300377 enum track_item alloc, unsigned long addr)
Christoph Lameter81819f02007-05-06 14:49:36 -0700378{
Akinobu Mita1a00df42009-03-07 00:36:21 +0900379 struct track *p = get_track(s, object, alloc);
Christoph Lameter81819f02007-05-06 14:49:36 -0700380
Christoph Lameter81819f02007-05-06 14:49:36 -0700381 if (addr) {
382 p->addr = addr;
383 p->cpu = smp_processor_id();
Alexey Dobriyan88e4ccf2008-06-23 02:58:37 +0400384 p->pid = current->pid;
Christoph Lameter81819f02007-05-06 14:49:36 -0700385 p->when = jiffies;
386 } else
387 memset(p, 0, sizeof(struct track));
388}
389
Christoph Lameter81819f02007-05-06 14:49:36 -0700390static void init_tracking(struct kmem_cache *s, void *object)
391{
Christoph Lameter24922682007-07-17 04:03:18 -0700392 if (!(s->flags & SLAB_STORE_USER))
393 return;
394
Eduard - Gabriel Munteanuce71e272008-08-19 20:43:25 +0300395 set_track(s, object, TRACK_FREE, 0UL);
396 set_track(s, object, TRACK_ALLOC, 0UL);
Christoph Lameter81819f02007-05-06 14:49:36 -0700397}
398
399static void print_track(const char *s, struct track *t)
400{
401 if (!t->addr)
402 return;
403
Linus Torvalds7daf7052008-07-14 12:12:53 -0700404 printk(KERN_ERR "INFO: %s in %pS age=%lu cpu=%u pid=%d\n",
Eduard - Gabriel Munteanuce71e272008-08-19 20:43:25 +0300405 s, (void *)t->addr, jiffies - t->when, t->cpu, t->pid);
Christoph Lameter81819f02007-05-06 14:49:36 -0700406}
407
Christoph Lameter24922682007-07-17 04:03:18 -0700408static void print_tracking(struct kmem_cache *s, void *object)
409{
410 if (!(s->flags & SLAB_STORE_USER))
411 return;
412
413 print_track("Allocated", get_track(s, object, TRACK_ALLOC));
414 print_track("Freed", get_track(s, object, TRACK_FREE));
415}
416
417static void print_page_info(struct page *page)
418{
Christoph Lameter39b26462008-04-14 19:11:30 +0300419 printk(KERN_ERR "INFO: Slab 0x%p objects=%u used=%u fp=0x%p flags=0x%04lx\n",
420 page, page->objects, page->inuse, page->freelist, page->flags);
Christoph Lameter24922682007-07-17 04:03:18 -0700421
422}
423
424static void slab_bug(struct kmem_cache *s, char *fmt, ...)
425{
426 va_list args;
427 char buf[100];
428
429 va_start(args, fmt);
430 vsnprintf(buf, sizeof(buf), fmt, args);
431 va_end(args);
432 printk(KERN_ERR "========================================"
433 "=====================================\n");
434 printk(KERN_ERR "BUG %s: %s\n", s->name, buf);
435 printk(KERN_ERR "----------------------------------------"
436 "-------------------------------------\n\n");
437}
438
439static void slab_fix(struct kmem_cache *s, char *fmt, ...)
440{
441 va_list args;
442 char buf[100];
443
444 va_start(args, fmt);
445 vsnprintf(buf, sizeof(buf), fmt, args);
446 va_end(args);
447 printk(KERN_ERR "FIX %s: %s\n", s->name, buf);
448}
449
450static void print_trailer(struct kmem_cache *s, struct page *page, u8 *p)
Christoph Lameter81819f02007-05-06 14:49:36 -0700451{
452 unsigned int off; /* Offset of last byte */
Christoph Lametera973e9d2008-03-01 13:40:44 -0800453 u8 *addr = page_address(page);
Christoph Lameter24922682007-07-17 04:03:18 -0700454
455 print_tracking(s, p);
456
457 print_page_info(page);
458
459 printk(KERN_ERR "INFO: Object 0x%p @offset=%tu fp=0x%p\n\n",
460 p, p - addr, get_freepointer(s, p));
461
462 if (p > addr + 16)
463 print_section("Bytes b4", p - 16, 16);
464
Pekka Enberg0ebd6522008-07-19 14:17:22 +0300465 print_section("Object", p, min_t(unsigned long, s->objsize, PAGE_SIZE));
Christoph Lameter81819f02007-05-06 14:49:36 -0700466
467 if (s->flags & SLAB_RED_ZONE)
468 print_section("Redzone", p + s->objsize,
469 s->inuse - s->objsize);
470
Christoph Lameter81819f02007-05-06 14:49:36 -0700471 if (s->offset)
472 off = s->offset + sizeof(void *);
473 else
474 off = s->inuse;
475
Christoph Lameter24922682007-07-17 04:03:18 -0700476 if (s->flags & SLAB_STORE_USER)
Christoph Lameter81819f02007-05-06 14:49:36 -0700477 off += 2 * sizeof(struct track);
Christoph Lameter81819f02007-05-06 14:49:36 -0700478
479 if (off != s->size)
480 /* Beginning of the filler is the free pointer */
Christoph Lameter24922682007-07-17 04:03:18 -0700481 print_section("Padding", p + off, s->size - off);
482
483 dump_stack();
Christoph Lameter81819f02007-05-06 14:49:36 -0700484}
485
486static void object_err(struct kmem_cache *s, struct page *page,
487 u8 *object, char *reason)
488{
Christoph Lameter3dc50632008-04-23 12:28:01 -0700489 slab_bug(s, "%s", reason);
Christoph Lameter24922682007-07-17 04:03:18 -0700490 print_trailer(s, page, object);
Christoph Lameter81819f02007-05-06 14:49:36 -0700491}
492
Christoph Lameter24922682007-07-17 04:03:18 -0700493static void slab_err(struct kmem_cache *s, struct page *page, char *fmt, ...)
Christoph Lameter81819f02007-05-06 14:49:36 -0700494{
495 va_list args;
496 char buf[100];
497
Christoph Lameter24922682007-07-17 04:03:18 -0700498 va_start(args, fmt);
499 vsnprintf(buf, sizeof(buf), fmt, args);
Christoph Lameter81819f02007-05-06 14:49:36 -0700500 va_end(args);
Christoph Lameter3dc50632008-04-23 12:28:01 -0700501 slab_bug(s, "%s", buf);
Christoph Lameter24922682007-07-17 04:03:18 -0700502 print_page_info(page);
Christoph Lameter81819f02007-05-06 14:49:36 -0700503 dump_stack();
504}
505
506static void init_object(struct kmem_cache *s, void *object, int active)
507{
508 u8 *p = object;
509
510 if (s->flags & __OBJECT_POISON) {
511 memset(p, POISON_FREE, s->objsize - 1);
Pekka Enberg06428782008-01-07 23:20:27 -0800512 p[s->objsize - 1] = POISON_END;
Christoph Lameter81819f02007-05-06 14:49:36 -0700513 }
514
515 if (s->flags & SLAB_RED_ZONE)
516 memset(p + s->objsize,
517 active ? SLUB_RED_ACTIVE : SLUB_RED_INACTIVE,
518 s->inuse - s->objsize);
519}
520
Christoph Lameter24922682007-07-17 04:03:18 -0700521static u8 *check_bytes(u8 *start, unsigned int value, unsigned int bytes)
Christoph Lameter81819f02007-05-06 14:49:36 -0700522{
523 while (bytes) {
524 if (*start != (u8)value)
Christoph Lameter24922682007-07-17 04:03:18 -0700525 return start;
Christoph Lameter81819f02007-05-06 14:49:36 -0700526 start++;
527 bytes--;
528 }
Christoph Lameter24922682007-07-17 04:03:18 -0700529 return NULL;
530}
531
532static void restore_bytes(struct kmem_cache *s, char *message, u8 data,
533 void *from, void *to)
534{
535 slab_fix(s, "Restoring 0x%p-0x%p=0x%x\n", from, to - 1, data);
536 memset(from, data, to - from);
537}
538
539static int check_bytes_and_report(struct kmem_cache *s, struct page *page,
540 u8 *object, char *what,
Pekka Enberg06428782008-01-07 23:20:27 -0800541 u8 *start, unsigned int value, unsigned int bytes)
Christoph Lameter24922682007-07-17 04:03:18 -0700542{
543 u8 *fault;
544 u8 *end;
545
546 fault = check_bytes(start, value, bytes);
547 if (!fault)
548 return 1;
549
550 end = start + bytes;
551 while (end > fault && end[-1] == value)
552 end--;
553
554 slab_bug(s, "%s overwritten", what);
555 printk(KERN_ERR "INFO: 0x%p-0x%p. First byte 0x%x instead of 0x%x\n",
556 fault, end - 1, fault[0], value);
557 print_trailer(s, page, object);
558
559 restore_bytes(s, what, value, fault, end);
560 return 0;
Christoph Lameter81819f02007-05-06 14:49:36 -0700561}
562
Christoph Lameter81819f02007-05-06 14:49:36 -0700563/*
564 * Object layout:
565 *
566 * object address
567 * Bytes of the object to be managed.
568 * If the freepointer may overlay the object then the free
569 * pointer is the first word of the object.
Christoph Lameter672bba32007-05-09 02:32:39 -0700570 *
Christoph Lameter81819f02007-05-06 14:49:36 -0700571 * Poisoning uses 0x6b (POISON_FREE) and the last byte is
572 * 0xa5 (POISON_END)
573 *
574 * object + s->objsize
575 * Padding to reach word boundary. This is also used for Redzoning.
Christoph Lameter672bba32007-05-09 02:32:39 -0700576 * Padding is extended by another word if Redzoning is enabled and
577 * objsize == inuse.
578 *
Christoph Lameter81819f02007-05-06 14:49:36 -0700579 * We fill with 0xbb (RED_INACTIVE) for inactive objects and with
580 * 0xcc (RED_ACTIVE) for objects in use.
581 *
582 * object + s->inuse
Christoph Lameter672bba32007-05-09 02:32:39 -0700583 * Meta data starts here.
584 *
Christoph Lameter81819f02007-05-06 14:49:36 -0700585 * A. Free pointer (if we cannot overwrite object on free)
586 * B. Tracking data for SLAB_STORE_USER
Christoph Lameter672bba32007-05-09 02:32:39 -0700587 * C. Padding to reach required alignment boundary or at mininum
Christoph Lameter6446faa2008-02-15 23:45:26 -0800588 * one word if debugging is on to be able to detect writes
Christoph Lameter672bba32007-05-09 02:32:39 -0700589 * before the word boundary.
590 *
591 * Padding is done using 0x5a (POISON_INUSE)
Christoph Lameter81819f02007-05-06 14:49:36 -0700592 *
593 * object + s->size
Christoph Lameter672bba32007-05-09 02:32:39 -0700594 * Nothing is used beyond s->size.
Christoph Lameter81819f02007-05-06 14:49:36 -0700595 *
Christoph Lameter672bba32007-05-09 02:32:39 -0700596 * If slabcaches are merged then the objsize and inuse boundaries are mostly
597 * ignored. And therefore no slab options that rely on these boundaries
Christoph Lameter81819f02007-05-06 14:49:36 -0700598 * may be used with merged slabcaches.
599 */
600
Christoph Lameter81819f02007-05-06 14:49:36 -0700601static int check_pad_bytes(struct kmem_cache *s, struct page *page, u8 *p)
602{
603 unsigned long off = s->inuse; /* The end of info */
604
605 if (s->offset)
606 /* Freepointer is placed after the object. */
607 off += sizeof(void *);
608
609 if (s->flags & SLAB_STORE_USER)
610 /* We also have user information there */
611 off += 2 * sizeof(struct track);
612
613 if (s->size == off)
614 return 1;
615
Christoph Lameter24922682007-07-17 04:03:18 -0700616 return check_bytes_and_report(s, page, p, "Object padding",
617 p + off, POISON_INUSE, s->size - off);
Christoph Lameter81819f02007-05-06 14:49:36 -0700618}
619
Christoph Lameter39b26462008-04-14 19:11:30 +0300620/* Check the pad bytes at the end of a slab page */
Christoph Lameter81819f02007-05-06 14:49:36 -0700621static int slab_pad_check(struct kmem_cache *s, struct page *page)
622{
Christoph Lameter24922682007-07-17 04:03:18 -0700623 u8 *start;
624 u8 *fault;
625 u8 *end;
626 int length;
627 int remainder;
Christoph Lameter81819f02007-05-06 14:49:36 -0700628
629 if (!(s->flags & SLAB_POISON))
630 return 1;
631
Christoph Lametera973e9d2008-03-01 13:40:44 -0800632 start = page_address(page);
Christoph Lameter834f3d12008-04-14 19:11:31 +0300633 length = (PAGE_SIZE << compound_order(page));
Christoph Lameter39b26462008-04-14 19:11:30 +0300634 end = start + length;
635 remainder = length % s->size;
Christoph Lameter81819f02007-05-06 14:49:36 -0700636 if (!remainder)
637 return 1;
638
Christoph Lameter39b26462008-04-14 19:11:30 +0300639 fault = check_bytes(end - remainder, POISON_INUSE, remainder);
Christoph Lameter24922682007-07-17 04:03:18 -0700640 if (!fault)
641 return 1;
642 while (end > fault && end[-1] == POISON_INUSE)
643 end--;
644
645 slab_err(s, page, "Padding overwritten. 0x%p-0x%p", fault, end - 1);
Christoph Lameter39b26462008-04-14 19:11:30 +0300646 print_section("Padding", end - remainder, remainder);
Christoph Lameter24922682007-07-17 04:03:18 -0700647
648 restore_bytes(s, "slab padding", POISON_INUSE, start, end);
649 return 0;
Christoph Lameter81819f02007-05-06 14:49:36 -0700650}
651
652static int check_object(struct kmem_cache *s, struct page *page,
653 void *object, int active)
654{
655 u8 *p = object;
656 u8 *endobject = object + s->objsize;
657
658 if (s->flags & SLAB_RED_ZONE) {
659 unsigned int red =
660 active ? SLUB_RED_ACTIVE : SLUB_RED_INACTIVE;
661
Christoph Lameter24922682007-07-17 04:03:18 -0700662 if (!check_bytes_and_report(s, page, object, "Redzone",
663 endobject, red, s->inuse - s->objsize))
Christoph Lameter81819f02007-05-06 14:49:36 -0700664 return 0;
Christoph Lameter81819f02007-05-06 14:49:36 -0700665 } else {
Ingo Molnar3adbefe2008-02-05 17:57:39 -0800666 if ((s->flags & SLAB_POISON) && s->objsize < s->inuse) {
667 check_bytes_and_report(s, page, p, "Alignment padding",
668 endobject, POISON_INUSE, s->inuse - s->objsize);
669 }
Christoph Lameter81819f02007-05-06 14:49:36 -0700670 }
671
672 if (s->flags & SLAB_POISON) {
673 if (!active && (s->flags & __OBJECT_POISON) &&
Christoph Lameter24922682007-07-17 04:03:18 -0700674 (!check_bytes_and_report(s, page, p, "Poison", p,
675 POISON_FREE, s->objsize - 1) ||
676 !check_bytes_and_report(s, page, p, "Poison",
Pekka Enberg06428782008-01-07 23:20:27 -0800677 p + s->objsize - 1, POISON_END, 1)))
Christoph Lameter81819f02007-05-06 14:49:36 -0700678 return 0;
Christoph Lameter81819f02007-05-06 14:49:36 -0700679 /*
680 * check_pad_bytes cleans up on its own.
681 */
682 check_pad_bytes(s, page, p);
683 }
684
685 if (!s->offset && active)
686 /*
687 * Object and freepointer overlap. Cannot check
688 * freepointer while object is allocated.
689 */
690 return 1;
691
692 /* Check free pointer validity */
693 if (!check_valid_pointer(s, page, get_freepointer(s, p))) {
694 object_err(s, page, p, "Freepointer corrupt");
695 /*
Nick Andrew9f6c708e2008-12-05 14:08:08 +1100696 * No choice but to zap it and thus lose the remainder
Christoph Lameter81819f02007-05-06 14:49:36 -0700697 * of the free objects in this slab. May cause
Christoph Lameter672bba32007-05-09 02:32:39 -0700698 * another error because the object count is now wrong.
Christoph Lameter81819f02007-05-06 14:49:36 -0700699 */
Christoph Lametera973e9d2008-03-01 13:40:44 -0800700 set_freepointer(s, p, NULL);
Christoph Lameter81819f02007-05-06 14:49:36 -0700701 return 0;
702 }
703 return 1;
704}
705
706static int check_slab(struct kmem_cache *s, struct page *page)
707{
Christoph Lameter39b26462008-04-14 19:11:30 +0300708 int maxobj;
709
Christoph Lameter81819f02007-05-06 14:49:36 -0700710 VM_BUG_ON(!irqs_disabled());
711
712 if (!PageSlab(page)) {
Christoph Lameter24922682007-07-17 04:03:18 -0700713 slab_err(s, page, "Not a valid slab page");
Christoph Lameter81819f02007-05-06 14:49:36 -0700714 return 0;
715 }
Christoph Lameter39b26462008-04-14 19:11:30 +0300716
717 maxobj = (PAGE_SIZE << compound_order(page)) / s->size;
718 if (page->objects > maxobj) {
719 slab_err(s, page, "objects %u > max %u",
720 s->name, page->objects, maxobj);
721 return 0;
722 }
723 if (page->inuse > page->objects) {
Christoph Lameter24922682007-07-17 04:03:18 -0700724 slab_err(s, page, "inuse %u > max %u",
Christoph Lameter39b26462008-04-14 19:11:30 +0300725 s->name, page->inuse, page->objects);
Christoph Lameter81819f02007-05-06 14:49:36 -0700726 return 0;
727 }
728 /* Slab_pad_check fixes things up after itself */
729 slab_pad_check(s, page);
730 return 1;
731}
732
733/*
Christoph Lameter672bba32007-05-09 02:32:39 -0700734 * Determine if a certain object on a page is on the freelist. Must hold the
735 * slab lock to guarantee that the chains are in a consistent state.
Christoph Lameter81819f02007-05-06 14:49:36 -0700736 */
737static int on_freelist(struct kmem_cache *s, struct page *page, void *search)
738{
739 int nr = 0;
740 void *fp = page->freelist;
741 void *object = NULL;
Christoph Lameter224a88b2008-04-14 19:11:31 +0300742 unsigned long max_objects;
Christoph Lameter81819f02007-05-06 14:49:36 -0700743
Christoph Lameter39b26462008-04-14 19:11:30 +0300744 while (fp && nr <= page->objects) {
Christoph Lameter81819f02007-05-06 14:49:36 -0700745 if (fp == search)
746 return 1;
747 if (!check_valid_pointer(s, page, fp)) {
748 if (object) {
749 object_err(s, page, object,
750 "Freechain corrupt");
Christoph Lametera973e9d2008-03-01 13:40:44 -0800751 set_freepointer(s, object, NULL);
Christoph Lameter81819f02007-05-06 14:49:36 -0700752 break;
753 } else {
Christoph Lameter24922682007-07-17 04:03:18 -0700754 slab_err(s, page, "Freepointer corrupt");
Christoph Lametera973e9d2008-03-01 13:40:44 -0800755 page->freelist = NULL;
Christoph Lameter39b26462008-04-14 19:11:30 +0300756 page->inuse = page->objects;
Christoph Lameter24922682007-07-17 04:03:18 -0700757 slab_fix(s, "Freelist cleared");
Christoph Lameter81819f02007-05-06 14:49:36 -0700758 return 0;
759 }
760 break;
761 }
762 object = fp;
763 fp = get_freepointer(s, object);
764 nr++;
765 }
766
Christoph Lameter224a88b2008-04-14 19:11:31 +0300767 max_objects = (PAGE_SIZE << compound_order(page)) / s->size;
Cyrill Gorcunov210b5c02008-10-22 23:00:38 +0400768 if (max_objects > MAX_OBJS_PER_PAGE)
769 max_objects = MAX_OBJS_PER_PAGE;
Christoph Lameter224a88b2008-04-14 19:11:31 +0300770
771 if (page->objects != max_objects) {
772 slab_err(s, page, "Wrong number of objects. Found %d but "
773 "should be %d", page->objects, max_objects);
774 page->objects = max_objects;
775 slab_fix(s, "Number of objects adjusted.");
776 }
Christoph Lameter39b26462008-04-14 19:11:30 +0300777 if (page->inuse != page->objects - nr) {
Christoph Lameter70d71222007-05-06 14:49:47 -0700778 slab_err(s, page, "Wrong object count. Counter is %d but "
Christoph Lameter39b26462008-04-14 19:11:30 +0300779 "counted were %d", page->inuse, page->objects - nr);
780 page->inuse = page->objects - nr;
Christoph Lameter24922682007-07-17 04:03:18 -0700781 slab_fix(s, "Object count adjusted.");
Christoph Lameter81819f02007-05-06 14:49:36 -0700782 }
783 return search == NULL;
784}
785
Christoph Lameter0121c6192008-04-29 16:11:12 -0700786static void trace(struct kmem_cache *s, struct page *page, void *object,
787 int alloc)
Christoph Lameter3ec09742007-05-16 22:11:00 -0700788{
789 if (s->flags & SLAB_TRACE) {
790 printk(KERN_INFO "TRACE %s %s 0x%p inuse=%d fp=0x%p\n",
791 s->name,
792 alloc ? "alloc" : "free",
793 object, page->inuse,
794 page->freelist);
795
796 if (!alloc)
797 print_section("Object", (void *)object, s->objsize);
798
799 dump_stack();
800 }
801}
802
Christoph Lameter643b1132007-05-06 14:49:42 -0700803/*
Christoph Lameter672bba32007-05-09 02:32:39 -0700804 * Tracking of fully allocated slabs for debugging purposes.
Christoph Lameter643b1132007-05-06 14:49:42 -0700805 */
Christoph Lametere95eed52007-05-06 14:49:44 -0700806static void add_full(struct kmem_cache_node *n, struct page *page)
Christoph Lameter643b1132007-05-06 14:49:42 -0700807{
Christoph Lameter643b1132007-05-06 14:49:42 -0700808 spin_lock(&n->list_lock);
809 list_add(&page->lru, &n->full);
810 spin_unlock(&n->list_lock);
811}
812
813static void remove_full(struct kmem_cache *s, struct page *page)
814{
815 struct kmem_cache_node *n;
816
817 if (!(s->flags & SLAB_STORE_USER))
818 return;
819
820 n = get_node(s, page_to_nid(page));
821
822 spin_lock(&n->list_lock);
823 list_del(&page->lru);
824 spin_unlock(&n->list_lock);
825}
826
Christoph Lameter0f389ec2008-04-14 18:53:02 +0300827/* Tracking of the number of slabs for debugging purposes */
828static inline unsigned long slabs_node(struct kmem_cache *s, int node)
829{
830 struct kmem_cache_node *n = get_node(s, node);
831
832 return atomic_long_read(&n->nr_slabs);
833}
834
Christoph Lameter205ab992008-04-14 19:11:40 +0300835static inline void inc_slabs_node(struct kmem_cache *s, int node, int objects)
Christoph Lameter0f389ec2008-04-14 18:53:02 +0300836{
837 struct kmem_cache_node *n = get_node(s, node);
838
839 /*
840 * May be called early in order to allocate a slab for the
841 * kmem_cache_node structure. Solve the chicken-egg
842 * dilemma by deferring the increment of the count during
843 * bootstrap (see early_kmem_cache_node_alloc).
844 */
Christoph Lameter205ab992008-04-14 19:11:40 +0300845 if (!NUMA_BUILD || n) {
Christoph Lameter0f389ec2008-04-14 18:53:02 +0300846 atomic_long_inc(&n->nr_slabs);
Christoph Lameter205ab992008-04-14 19:11:40 +0300847 atomic_long_add(objects, &n->total_objects);
848 }
Christoph Lameter0f389ec2008-04-14 18:53:02 +0300849}
Christoph Lameter205ab992008-04-14 19:11:40 +0300850static inline void dec_slabs_node(struct kmem_cache *s, int node, int objects)
Christoph Lameter0f389ec2008-04-14 18:53:02 +0300851{
852 struct kmem_cache_node *n = get_node(s, node);
853
854 atomic_long_dec(&n->nr_slabs);
Christoph Lameter205ab992008-04-14 19:11:40 +0300855 atomic_long_sub(objects, &n->total_objects);
Christoph Lameter0f389ec2008-04-14 18:53:02 +0300856}
857
858/* Object debug checks for alloc/free paths */
Christoph Lameter3ec09742007-05-16 22:11:00 -0700859static void setup_object_debug(struct kmem_cache *s, struct page *page,
860 void *object)
861{
862 if (!(s->flags & (SLAB_STORE_USER|SLAB_RED_ZONE|__OBJECT_POISON)))
863 return;
864
865 init_object(s, object, 0);
866 init_tracking(s, object);
867}
868
869static int alloc_debug_processing(struct kmem_cache *s, struct page *page,
Eduard - Gabriel Munteanuce71e272008-08-19 20:43:25 +0300870 void *object, unsigned long addr)
Christoph Lameter81819f02007-05-06 14:49:36 -0700871{
872 if (!check_slab(s, page))
873 goto bad;
874
Christoph Lameterd692ef62008-02-15 23:45:24 -0800875 if (!on_freelist(s, page, object)) {
Christoph Lameter24922682007-07-17 04:03:18 -0700876 object_err(s, page, object, "Object already allocated");
Christoph Lameter70d71222007-05-06 14:49:47 -0700877 goto bad;
Christoph Lameter81819f02007-05-06 14:49:36 -0700878 }
879
880 if (!check_valid_pointer(s, page, object)) {
881 object_err(s, page, object, "Freelist Pointer check fails");
Christoph Lameter70d71222007-05-06 14:49:47 -0700882 goto bad;
Christoph Lameter81819f02007-05-06 14:49:36 -0700883 }
884
Christoph Lameterd692ef62008-02-15 23:45:24 -0800885 if (!check_object(s, page, object, 0))
Christoph Lameter81819f02007-05-06 14:49:36 -0700886 goto bad;
Christoph Lameter81819f02007-05-06 14:49:36 -0700887
Christoph Lameter3ec09742007-05-16 22:11:00 -0700888 /* Success perform special debug activities for allocs */
889 if (s->flags & SLAB_STORE_USER)
890 set_track(s, object, TRACK_ALLOC, addr);
891 trace(s, page, object, 1);
892 init_object(s, object, 1);
Christoph Lameter81819f02007-05-06 14:49:36 -0700893 return 1;
Christoph Lameter3ec09742007-05-16 22:11:00 -0700894
Christoph Lameter81819f02007-05-06 14:49:36 -0700895bad:
896 if (PageSlab(page)) {
897 /*
898 * If this is a slab page then lets do the best we can
899 * to avoid issues in the future. Marking all objects
Christoph Lameter672bba32007-05-09 02:32:39 -0700900 * as used avoids touching the remaining objects.
Christoph Lameter81819f02007-05-06 14:49:36 -0700901 */
Christoph Lameter24922682007-07-17 04:03:18 -0700902 slab_fix(s, "Marking all objects used");
Christoph Lameter39b26462008-04-14 19:11:30 +0300903 page->inuse = page->objects;
Christoph Lametera973e9d2008-03-01 13:40:44 -0800904 page->freelist = NULL;
Christoph Lameter81819f02007-05-06 14:49:36 -0700905 }
906 return 0;
907}
908
Christoph Lameter3ec09742007-05-16 22:11:00 -0700909static int free_debug_processing(struct kmem_cache *s, struct page *page,
Eduard - Gabriel Munteanuce71e272008-08-19 20:43:25 +0300910 void *object, unsigned long addr)
Christoph Lameter81819f02007-05-06 14:49:36 -0700911{
912 if (!check_slab(s, page))
913 goto fail;
914
915 if (!check_valid_pointer(s, page, object)) {
Christoph Lameter70d71222007-05-06 14:49:47 -0700916 slab_err(s, page, "Invalid object pointer 0x%p", object);
Christoph Lameter81819f02007-05-06 14:49:36 -0700917 goto fail;
918 }
919
920 if (on_freelist(s, page, object)) {
Christoph Lameter24922682007-07-17 04:03:18 -0700921 object_err(s, page, object, "Object already free");
Christoph Lameter81819f02007-05-06 14:49:36 -0700922 goto fail;
923 }
924
925 if (!check_object(s, page, object, 1))
926 return 0;
927
928 if (unlikely(s != page->slab)) {
Ingo Molnar3adbefe2008-02-05 17:57:39 -0800929 if (!PageSlab(page)) {
Christoph Lameter70d71222007-05-06 14:49:47 -0700930 slab_err(s, page, "Attempt to free object(0x%p) "
931 "outside of slab", object);
Ingo Molnar3adbefe2008-02-05 17:57:39 -0800932 } else if (!page->slab) {
Christoph Lameter81819f02007-05-06 14:49:36 -0700933 printk(KERN_ERR
Christoph Lameter70d71222007-05-06 14:49:47 -0700934 "SLUB <none>: no slab for object 0x%p.\n",
Christoph Lameter81819f02007-05-06 14:49:36 -0700935 object);
Christoph Lameter70d71222007-05-06 14:49:47 -0700936 dump_stack();
Pekka Enberg06428782008-01-07 23:20:27 -0800937 } else
Christoph Lameter24922682007-07-17 04:03:18 -0700938 object_err(s, page, object,
939 "page slab pointer corrupt.");
Christoph Lameter81819f02007-05-06 14:49:36 -0700940 goto fail;
941 }
Christoph Lameter3ec09742007-05-16 22:11:00 -0700942
943 /* Special debug activities for freeing objects */
Andy Whitcroft8a380822008-07-23 21:27:18 -0700944 if (!PageSlubFrozen(page) && !page->freelist)
Christoph Lameter3ec09742007-05-16 22:11:00 -0700945 remove_full(s, page);
946 if (s->flags & SLAB_STORE_USER)
947 set_track(s, object, TRACK_FREE, addr);
948 trace(s, page, object, 0);
949 init_object(s, object, 0);
Christoph Lameter81819f02007-05-06 14:49:36 -0700950 return 1;
Christoph Lameter3ec09742007-05-16 22:11:00 -0700951
Christoph Lameter81819f02007-05-06 14:49:36 -0700952fail:
Christoph Lameter24922682007-07-17 04:03:18 -0700953 slab_fix(s, "Object at 0x%p not freed", object);
Christoph Lameter81819f02007-05-06 14:49:36 -0700954 return 0;
955}
956
Christoph Lameter41ecc552007-05-09 02:32:44 -0700957static int __init setup_slub_debug(char *str)
958{
Christoph Lameterf0630ff2007-07-15 23:38:14 -0700959 slub_debug = DEBUG_DEFAULT_FLAGS;
960 if (*str++ != '=' || !*str)
961 /*
962 * No options specified. Switch on full debugging.
963 */
964 goto out;
Christoph Lameter41ecc552007-05-09 02:32:44 -0700965
966 if (*str == ',')
Christoph Lameterf0630ff2007-07-15 23:38:14 -0700967 /*
968 * No options but restriction on slabs. This means full
969 * debugging for slabs matching a pattern.
970 */
971 goto check_slabs;
972
973 slub_debug = 0;
974 if (*str == '-')
975 /*
976 * Switch off all debugging measures.
977 */
978 goto out;
979
980 /*
981 * Determine which debug features should be switched on
982 */
Pekka Enberg06428782008-01-07 23:20:27 -0800983 for (; *str && *str != ','; str++) {
Christoph Lameterf0630ff2007-07-15 23:38:14 -0700984 switch (tolower(*str)) {
985 case 'f':
986 slub_debug |= SLAB_DEBUG_FREE;
987 break;
988 case 'z':
989 slub_debug |= SLAB_RED_ZONE;
990 break;
991 case 'p':
992 slub_debug |= SLAB_POISON;
993 break;
994 case 'u':
995 slub_debug |= SLAB_STORE_USER;
996 break;
997 case 't':
998 slub_debug |= SLAB_TRACE;
999 break;
1000 default:
1001 printk(KERN_ERR "slub_debug option '%c' "
Pekka Enberg06428782008-01-07 23:20:27 -08001002 "unknown. skipped\n", *str);
Christoph Lameterf0630ff2007-07-15 23:38:14 -07001003 }
1004 }
1005
1006check_slabs:
1007 if (*str == ',')
Christoph Lameter41ecc552007-05-09 02:32:44 -07001008 slub_debug_slabs = str + 1;
Christoph Lameterf0630ff2007-07-15 23:38:14 -07001009out:
Christoph Lameter41ecc552007-05-09 02:32:44 -07001010 return 1;
1011}
1012
1013__setup("slub_debug", setup_slub_debug);
1014
Christoph Lameterba0268a2007-09-11 15:24:11 -07001015static unsigned long kmem_cache_flags(unsigned long objsize,
1016 unsigned long flags, const char *name,
Alexey Dobriyan51cc5062008-07-25 19:45:34 -07001017 void (*ctor)(void *))
Christoph Lameter41ecc552007-05-09 02:32:44 -07001018{
1019 /*
Christoph Lametere1533622008-02-15 23:45:24 -08001020 * Enable debugging if selected on the kernel commandline.
Christoph Lameter41ecc552007-05-09 02:32:44 -07001021 */
Christoph Lametere1533622008-02-15 23:45:24 -08001022 if (slub_debug && (!slub_debug_slabs ||
1023 strncmp(slub_debug_slabs, name, strlen(slub_debug_slabs)) == 0))
1024 flags |= slub_debug;
Christoph Lameterba0268a2007-09-11 15:24:11 -07001025
1026 return flags;
Christoph Lameter41ecc552007-05-09 02:32:44 -07001027}
1028#else
Christoph Lameter3ec09742007-05-16 22:11:00 -07001029static inline void setup_object_debug(struct kmem_cache *s,
1030 struct page *page, void *object) {}
Christoph Lameter41ecc552007-05-09 02:32:44 -07001031
Christoph Lameter3ec09742007-05-16 22:11:00 -07001032static inline int alloc_debug_processing(struct kmem_cache *s,
Eduard - Gabriel Munteanuce71e272008-08-19 20:43:25 +03001033 struct page *page, void *object, unsigned long addr) { return 0; }
Christoph Lameter41ecc552007-05-09 02:32:44 -07001034
Christoph Lameter3ec09742007-05-16 22:11:00 -07001035static inline int free_debug_processing(struct kmem_cache *s,
Eduard - Gabriel Munteanuce71e272008-08-19 20:43:25 +03001036 struct page *page, void *object, unsigned long addr) { return 0; }
Christoph Lameter41ecc552007-05-09 02:32:44 -07001037
Christoph Lameter41ecc552007-05-09 02:32:44 -07001038static inline int slab_pad_check(struct kmem_cache *s, struct page *page)
1039 { return 1; }
1040static inline int check_object(struct kmem_cache *s, struct page *page,
1041 void *object, int active) { return 1; }
Christoph Lameter3ec09742007-05-16 22:11:00 -07001042static inline void add_full(struct kmem_cache_node *n, struct page *page) {}
Christoph Lameterba0268a2007-09-11 15:24:11 -07001043static inline unsigned long kmem_cache_flags(unsigned long objsize,
1044 unsigned long flags, const char *name,
Alexey Dobriyan51cc5062008-07-25 19:45:34 -07001045 void (*ctor)(void *))
Christoph Lameterba0268a2007-09-11 15:24:11 -07001046{
1047 return flags;
1048}
Christoph Lameter41ecc552007-05-09 02:32:44 -07001049#define slub_debug 0
Christoph Lameter0f389ec2008-04-14 18:53:02 +03001050
1051static inline unsigned long slabs_node(struct kmem_cache *s, int node)
1052 { return 0; }
Christoph Lameter205ab992008-04-14 19:11:40 +03001053static inline void inc_slabs_node(struct kmem_cache *s, int node,
1054 int objects) {}
1055static inline void dec_slabs_node(struct kmem_cache *s, int node,
1056 int objects) {}
Christoph Lameter41ecc552007-05-09 02:32:44 -07001057#endif
Christoph Lameter205ab992008-04-14 19:11:40 +03001058
Christoph Lameter81819f02007-05-06 14:49:36 -07001059/*
1060 * Slab allocation and freeing
1061 */
Christoph Lameter65c33762008-04-14 19:11:40 +03001062static inline struct page *alloc_slab_page(gfp_t flags, int node,
1063 struct kmem_cache_order_objects oo)
1064{
1065 int order = oo_order(oo);
1066
1067 if (node == -1)
1068 return alloc_pages(flags, order);
1069 else
1070 return alloc_pages_node(node, flags, order);
1071}
1072
Christoph Lameter81819f02007-05-06 14:49:36 -07001073static struct page *allocate_slab(struct kmem_cache *s, gfp_t flags, int node)
1074{
Pekka Enberg06428782008-01-07 23:20:27 -08001075 struct page *page;
Christoph Lameter834f3d12008-04-14 19:11:31 +03001076 struct kmem_cache_order_objects oo = s->oo;
Christoph Lameter81819f02007-05-06 14:49:36 -07001077
Christoph Lameterb7a49f02008-02-14 14:21:32 -08001078 flags |= s->allocflags;
Mel Gormane12ba742007-10-16 01:25:52 -07001079
Christoph Lameter65c33762008-04-14 19:11:40 +03001080 page = alloc_slab_page(flags | __GFP_NOWARN | __GFP_NORETRY, node,
1081 oo);
1082 if (unlikely(!page)) {
1083 oo = s->min;
1084 /*
1085 * Allocation may have failed due to fragmentation.
1086 * Try a lower order alloc if possible
1087 */
1088 page = alloc_slab_page(flags, node, oo);
1089 if (!page)
1090 return NULL;
Christoph Lameter81819f02007-05-06 14:49:36 -07001091
Christoph Lameter65c33762008-04-14 19:11:40 +03001092 stat(get_cpu_slab(s, raw_smp_processor_id()), ORDER_FALLBACK);
1093 }
Christoph Lameter834f3d12008-04-14 19:11:31 +03001094 page->objects = oo_objects(oo);
Christoph Lameter81819f02007-05-06 14:49:36 -07001095 mod_zone_page_state(page_zone(page),
1096 (s->flags & SLAB_RECLAIM_ACCOUNT) ?
1097 NR_SLAB_RECLAIMABLE : NR_SLAB_UNRECLAIMABLE,
Christoph Lameter65c33762008-04-14 19:11:40 +03001098 1 << oo_order(oo));
Christoph Lameter81819f02007-05-06 14:49:36 -07001099
1100 return page;
1101}
1102
1103static void setup_object(struct kmem_cache *s, struct page *page,
1104 void *object)
1105{
Christoph Lameter3ec09742007-05-16 22:11:00 -07001106 setup_object_debug(s, page, object);
Christoph Lameter4f104932007-05-06 14:50:17 -07001107 if (unlikely(s->ctor))
Alexey Dobriyan51cc5062008-07-25 19:45:34 -07001108 s->ctor(object);
Christoph Lameter81819f02007-05-06 14:49:36 -07001109}
1110
1111static struct page *new_slab(struct kmem_cache *s, gfp_t flags, int node)
1112{
1113 struct page *page;
Christoph Lameter81819f02007-05-06 14:49:36 -07001114 void *start;
Christoph Lameter81819f02007-05-06 14:49:36 -07001115 void *last;
1116 void *p;
1117
Christoph Lameter6cb06222007-10-16 01:25:41 -07001118 BUG_ON(flags & GFP_SLAB_BUG_MASK);
Christoph Lameter81819f02007-05-06 14:49:36 -07001119
Christoph Lameter6cb06222007-10-16 01:25:41 -07001120 page = allocate_slab(s,
1121 flags & (GFP_RECLAIM_MASK | GFP_CONSTRAINT_MASK), node);
Christoph Lameter81819f02007-05-06 14:49:36 -07001122 if (!page)
1123 goto out;
1124
Christoph Lameter205ab992008-04-14 19:11:40 +03001125 inc_slabs_node(s, page_to_nid(page), page->objects);
Christoph Lameter81819f02007-05-06 14:49:36 -07001126 page->slab = s;
1127 page->flags |= 1 << PG_slab;
1128 if (s->flags & (SLAB_DEBUG_FREE | SLAB_RED_ZONE | SLAB_POISON |
1129 SLAB_STORE_USER | SLAB_TRACE))
Andy Whitcroft8a380822008-07-23 21:27:18 -07001130 __SetPageSlubDebug(page);
Christoph Lameter81819f02007-05-06 14:49:36 -07001131
1132 start = page_address(page);
Christoph Lameter81819f02007-05-06 14:49:36 -07001133
1134 if (unlikely(s->flags & SLAB_POISON))
Christoph Lameter834f3d12008-04-14 19:11:31 +03001135 memset(start, POISON_INUSE, PAGE_SIZE << compound_order(page));
Christoph Lameter81819f02007-05-06 14:49:36 -07001136
1137 last = start;
Christoph Lameter224a88b2008-04-14 19:11:31 +03001138 for_each_object(p, s, start, page->objects) {
Christoph Lameter81819f02007-05-06 14:49:36 -07001139 setup_object(s, page, last);
1140 set_freepointer(s, last, p);
1141 last = p;
1142 }
1143 setup_object(s, page, last);
Christoph Lametera973e9d2008-03-01 13:40:44 -08001144 set_freepointer(s, last, NULL);
Christoph Lameter81819f02007-05-06 14:49:36 -07001145
1146 page->freelist = start;
1147 page->inuse = 0;
1148out:
Christoph Lameter81819f02007-05-06 14:49:36 -07001149 return page;
1150}
1151
1152static void __free_slab(struct kmem_cache *s, struct page *page)
1153{
Christoph Lameter834f3d12008-04-14 19:11:31 +03001154 int order = compound_order(page);
1155 int pages = 1 << order;
Christoph Lameter81819f02007-05-06 14:49:36 -07001156
Andy Whitcroft8a380822008-07-23 21:27:18 -07001157 if (unlikely(SLABDEBUG && PageSlubDebug(page))) {
Christoph Lameter81819f02007-05-06 14:49:36 -07001158 void *p;
1159
1160 slab_pad_check(s, page);
Christoph Lameter224a88b2008-04-14 19:11:31 +03001161 for_each_object(p, s, page_address(page),
1162 page->objects)
Christoph Lameter81819f02007-05-06 14:49:36 -07001163 check_object(s, page, p, 0);
Andy Whitcroft8a380822008-07-23 21:27:18 -07001164 __ClearPageSlubDebug(page);
Christoph Lameter81819f02007-05-06 14:49:36 -07001165 }
1166
1167 mod_zone_page_state(page_zone(page),
1168 (s->flags & SLAB_RECLAIM_ACCOUNT) ?
1169 NR_SLAB_RECLAIMABLE : NR_SLAB_UNRECLAIMABLE,
Pekka Enberg06428782008-01-07 23:20:27 -08001170 -pages);
Christoph Lameter81819f02007-05-06 14:49:36 -07001171
Christoph Lameter49bd5222008-04-14 18:52:18 +03001172 __ClearPageSlab(page);
1173 reset_page_mapcount(page);
Nick Piggin1eb5ac62009-05-05 19:13:44 +10001174 if (current->reclaim_state)
1175 current->reclaim_state->reclaimed_slab += pages;
Christoph Lameter834f3d12008-04-14 19:11:31 +03001176 __free_pages(page, order);
Christoph Lameter81819f02007-05-06 14:49:36 -07001177}
1178
1179static void rcu_free_slab(struct rcu_head *h)
1180{
1181 struct page *page;
1182
1183 page = container_of((struct list_head *)h, struct page, lru);
1184 __free_slab(page->slab, page);
1185}
1186
1187static void free_slab(struct kmem_cache *s, struct page *page)
1188{
1189 if (unlikely(s->flags & SLAB_DESTROY_BY_RCU)) {
1190 /*
1191 * RCU free overloads the RCU head over the LRU
1192 */
1193 struct rcu_head *head = (void *)&page->lru;
1194
1195 call_rcu(head, rcu_free_slab);
1196 } else
1197 __free_slab(s, page);
1198}
1199
1200static void discard_slab(struct kmem_cache *s, struct page *page)
1201{
Christoph Lameter205ab992008-04-14 19:11:40 +03001202 dec_slabs_node(s, page_to_nid(page), page->objects);
Christoph Lameter81819f02007-05-06 14:49:36 -07001203 free_slab(s, page);
1204}
1205
1206/*
1207 * Per slab locking using the pagelock
1208 */
1209static __always_inline void slab_lock(struct page *page)
1210{
1211 bit_spin_lock(PG_locked, &page->flags);
1212}
1213
1214static __always_inline void slab_unlock(struct page *page)
1215{
Nick Piggina76d3542008-01-07 23:20:27 -08001216 __bit_spin_unlock(PG_locked, &page->flags);
Christoph Lameter81819f02007-05-06 14:49:36 -07001217}
1218
1219static __always_inline int slab_trylock(struct page *page)
1220{
1221 int rc = 1;
1222
1223 rc = bit_spin_trylock(PG_locked, &page->flags);
1224 return rc;
1225}
1226
1227/*
1228 * Management of partially allocated slabs
1229 */
Christoph Lameter7c2e1322008-01-07 23:20:27 -08001230static void add_partial(struct kmem_cache_node *n,
1231 struct page *page, int tail)
Christoph Lameter81819f02007-05-06 14:49:36 -07001232{
Christoph Lametere95eed52007-05-06 14:49:44 -07001233 spin_lock(&n->list_lock);
1234 n->nr_partial++;
Christoph Lameter7c2e1322008-01-07 23:20:27 -08001235 if (tail)
1236 list_add_tail(&page->lru, &n->partial);
1237 else
1238 list_add(&page->lru, &n->partial);
Christoph Lameter81819f02007-05-06 14:49:36 -07001239 spin_unlock(&n->list_lock);
1240}
1241
Christoph Lameter0121c6192008-04-29 16:11:12 -07001242static void remove_partial(struct kmem_cache *s, struct page *page)
Christoph Lameter81819f02007-05-06 14:49:36 -07001243{
1244 struct kmem_cache_node *n = get_node(s, page_to_nid(page));
1245
1246 spin_lock(&n->list_lock);
1247 list_del(&page->lru);
1248 n->nr_partial--;
1249 spin_unlock(&n->list_lock);
1250}
1251
1252/*
Christoph Lameter672bba32007-05-09 02:32:39 -07001253 * Lock slab and remove from the partial list.
Christoph Lameter81819f02007-05-06 14:49:36 -07001254 *
Christoph Lameter672bba32007-05-09 02:32:39 -07001255 * Must hold list_lock.
Christoph Lameter81819f02007-05-06 14:49:36 -07001256 */
Christoph Lameter0121c6192008-04-29 16:11:12 -07001257static inline int lock_and_freeze_slab(struct kmem_cache_node *n,
1258 struct page *page)
Christoph Lameter81819f02007-05-06 14:49:36 -07001259{
1260 if (slab_trylock(page)) {
1261 list_del(&page->lru);
1262 n->nr_partial--;
Andy Whitcroft8a380822008-07-23 21:27:18 -07001263 __SetPageSlubFrozen(page);
Christoph Lameter81819f02007-05-06 14:49:36 -07001264 return 1;
1265 }
1266 return 0;
1267}
1268
1269/*
Christoph Lameter672bba32007-05-09 02:32:39 -07001270 * Try to allocate a partial slab from a specific node.
Christoph Lameter81819f02007-05-06 14:49:36 -07001271 */
1272static struct page *get_partial_node(struct kmem_cache_node *n)
1273{
1274 struct page *page;
1275
1276 /*
1277 * Racy check. If we mistakenly see no partial slabs then we
1278 * just allocate an empty slab. If we mistakenly try to get a
Christoph Lameter672bba32007-05-09 02:32:39 -07001279 * partial slab and there is none available then get_partials()
1280 * will return NULL.
Christoph Lameter81819f02007-05-06 14:49:36 -07001281 */
1282 if (!n || !n->nr_partial)
1283 return NULL;
1284
1285 spin_lock(&n->list_lock);
1286 list_for_each_entry(page, &n->partial, lru)
Christoph Lameter4b6f0752007-05-16 22:10:53 -07001287 if (lock_and_freeze_slab(n, page))
Christoph Lameter81819f02007-05-06 14:49:36 -07001288 goto out;
1289 page = NULL;
1290out:
1291 spin_unlock(&n->list_lock);
1292 return page;
1293}
1294
1295/*
Christoph Lameter672bba32007-05-09 02:32:39 -07001296 * Get a page from somewhere. Search in increasing NUMA distances.
Christoph Lameter81819f02007-05-06 14:49:36 -07001297 */
1298static struct page *get_any_partial(struct kmem_cache *s, gfp_t flags)
1299{
1300#ifdef CONFIG_NUMA
1301 struct zonelist *zonelist;
Mel Gormandd1a2392008-04-28 02:12:17 -07001302 struct zoneref *z;
Mel Gorman54a6eb52008-04-28 02:12:16 -07001303 struct zone *zone;
1304 enum zone_type high_zoneidx = gfp_zone(flags);
Christoph Lameter81819f02007-05-06 14:49:36 -07001305 struct page *page;
1306
1307 /*
Christoph Lameter672bba32007-05-09 02:32:39 -07001308 * The defrag ratio allows a configuration of the tradeoffs between
1309 * inter node defragmentation and node local allocations. A lower
1310 * defrag_ratio increases the tendency to do local allocations
1311 * instead of attempting to obtain partial slabs from other nodes.
Christoph Lameter81819f02007-05-06 14:49:36 -07001312 *
Christoph Lameter672bba32007-05-09 02:32:39 -07001313 * If the defrag_ratio is set to 0 then kmalloc() always
1314 * returns node local objects. If the ratio is higher then kmalloc()
1315 * may return off node objects because partial slabs are obtained
1316 * from other nodes and filled up.
Christoph Lameter81819f02007-05-06 14:49:36 -07001317 *
Christoph Lameter6446faa2008-02-15 23:45:26 -08001318 * If /sys/kernel/slab/xx/defrag_ratio is set to 100 (which makes
Christoph Lameter672bba32007-05-09 02:32:39 -07001319 * defrag_ratio = 1000) then every (well almost) allocation will
1320 * first attempt to defrag slab caches on other nodes. This means
1321 * scanning over all nodes to look for partial slabs which may be
1322 * expensive if we do it every time we are trying to find a slab
1323 * with available objects.
Christoph Lameter81819f02007-05-06 14:49:36 -07001324 */
Christoph Lameter98246012008-01-07 23:20:26 -08001325 if (!s->remote_node_defrag_ratio ||
1326 get_cycles() % 1024 > s->remote_node_defrag_ratio)
Christoph Lameter81819f02007-05-06 14:49:36 -07001327 return NULL;
1328
Mel Gorman0e884602008-04-28 02:12:14 -07001329 zonelist = node_zonelist(slab_node(current->mempolicy), flags);
Mel Gorman54a6eb52008-04-28 02:12:16 -07001330 for_each_zone_zonelist(zone, z, zonelist, high_zoneidx) {
Christoph Lameter81819f02007-05-06 14:49:36 -07001331 struct kmem_cache_node *n;
1332
Mel Gorman54a6eb52008-04-28 02:12:16 -07001333 n = get_node(s, zone_to_nid(zone));
Christoph Lameter81819f02007-05-06 14:49:36 -07001334
Mel Gorman54a6eb52008-04-28 02:12:16 -07001335 if (n && cpuset_zone_allowed_hardwall(zone, flags) &&
David Rientjes3b89d7d2009-02-22 17:40:07 -08001336 n->nr_partial > s->min_partial) {
Christoph Lameter81819f02007-05-06 14:49:36 -07001337 page = get_partial_node(n);
1338 if (page)
1339 return page;
1340 }
1341 }
1342#endif
1343 return NULL;
1344}
1345
1346/*
1347 * Get a partial page, lock it and return it.
1348 */
1349static struct page *get_partial(struct kmem_cache *s, gfp_t flags, int node)
1350{
1351 struct page *page;
1352 int searchnode = (node == -1) ? numa_node_id() : node;
1353
1354 page = get_partial_node(get_node(s, searchnode));
1355 if (page || (flags & __GFP_THISNODE))
1356 return page;
1357
1358 return get_any_partial(s, flags);
1359}
1360
1361/*
1362 * Move a page back to the lists.
1363 *
1364 * Must be called with the slab lock held.
1365 *
1366 * On exit the slab lock will have been dropped.
1367 */
Christoph Lameter7c2e1322008-01-07 23:20:27 -08001368static void unfreeze_slab(struct kmem_cache *s, struct page *page, int tail)
Christoph Lameter81819f02007-05-06 14:49:36 -07001369{
Christoph Lametere95eed52007-05-06 14:49:44 -07001370 struct kmem_cache_node *n = get_node(s, page_to_nid(page));
Christoph Lameter8ff12cf2008-02-07 17:47:41 -08001371 struct kmem_cache_cpu *c = get_cpu_slab(s, smp_processor_id());
Christoph Lametere95eed52007-05-06 14:49:44 -07001372
Andy Whitcroft8a380822008-07-23 21:27:18 -07001373 __ClearPageSlubFrozen(page);
Christoph Lameter81819f02007-05-06 14:49:36 -07001374 if (page->inuse) {
Christoph Lametere95eed52007-05-06 14:49:44 -07001375
Christoph Lametera973e9d2008-03-01 13:40:44 -08001376 if (page->freelist) {
Christoph Lameter7c2e1322008-01-07 23:20:27 -08001377 add_partial(n, page, tail);
Christoph Lameter8ff12cf2008-02-07 17:47:41 -08001378 stat(c, tail ? DEACTIVATE_TO_TAIL : DEACTIVATE_TO_HEAD);
1379 } else {
1380 stat(c, DEACTIVATE_FULL);
Andy Whitcroft8a380822008-07-23 21:27:18 -07001381 if (SLABDEBUG && PageSlubDebug(page) &&
1382 (s->flags & SLAB_STORE_USER))
Christoph Lameter8ff12cf2008-02-07 17:47:41 -08001383 add_full(n, page);
1384 }
Christoph Lameter81819f02007-05-06 14:49:36 -07001385 slab_unlock(page);
1386 } else {
Christoph Lameter8ff12cf2008-02-07 17:47:41 -08001387 stat(c, DEACTIVATE_EMPTY);
David Rientjes3b89d7d2009-02-22 17:40:07 -08001388 if (n->nr_partial < s->min_partial) {
Christoph Lametere95eed52007-05-06 14:49:44 -07001389 /*
Christoph Lameter672bba32007-05-09 02:32:39 -07001390 * Adding an empty slab to the partial slabs in order
1391 * to avoid page allocator overhead. This slab needs
1392 * to come after the other slabs with objects in
Christoph Lameter6446faa2008-02-15 23:45:26 -08001393 * so that the others get filled first. That way the
1394 * size of the partial list stays small.
1395 *
Christoph Lameter0121c6192008-04-29 16:11:12 -07001396 * kmem_cache_shrink can reclaim any empty slabs from
1397 * the partial list.
Christoph Lametere95eed52007-05-06 14:49:44 -07001398 */
Christoph Lameter7c2e1322008-01-07 23:20:27 -08001399 add_partial(n, page, 1);
Christoph Lametere95eed52007-05-06 14:49:44 -07001400 slab_unlock(page);
1401 } else {
1402 slab_unlock(page);
Christoph Lameter8ff12cf2008-02-07 17:47:41 -08001403 stat(get_cpu_slab(s, raw_smp_processor_id()), FREE_SLAB);
Christoph Lametere95eed52007-05-06 14:49:44 -07001404 discard_slab(s, page);
1405 }
Christoph Lameter81819f02007-05-06 14:49:36 -07001406 }
1407}
1408
1409/*
1410 * Remove the cpu slab
1411 */
Christoph Lameterdfb4f092007-10-16 01:26:05 -07001412static void deactivate_slab(struct kmem_cache *s, struct kmem_cache_cpu *c)
Christoph Lameter81819f02007-05-06 14:49:36 -07001413{
Christoph Lameterdfb4f092007-10-16 01:26:05 -07001414 struct page *page = c->page;
Christoph Lameter7c2e1322008-01-07 23:20:27 -08001415 int tail = 1;
Christoph Lameter8ff12cf2008-02-07 17:47:41 -08001416
Christoph Lameterb773ad72008-03-04 11:10:17 -08001417 if (page->freelist)
Christoph Lameter8ff12cf2008-02-07 17:47:41 -08001418 stat(c, DEACTIVATE_REMOTE_FREES);
Christoph Lameter894b8782007-05-10 03:15:16 -07001419 /*
Christoph Lameter6446faa2008-02-15 23:45:26 -08001420 * Merge cpu freelist into slab freelist. Typically we get here
Christoph Lameter894b8782007-05-10 03:15:16 -07001421 * because both freelists are empty. So this is unlikely
1422 * to occur.
1423 */
Christoph Lametera973e9d2008-03-01 13:40:44 -08001424 while (unlikely(c->freelist)) {
Christoph Lameter894b8782007-05-10 03:15:16 -07001425 void **object;
1426
Christoph Lameter7c2e1322008-01-07 23:20:27 -08001427 tail = 0; /* Hot objects. Put the slab first */
1428
Christoph Lameter894b8782007-05-10 03:15:16 -07001429 /* Retrieve object from cpu_freelist */
Christoph Lameterdfb4f092007-10-16 01:26:05 -07001430 object = c->freelist;
Christoph Lameterb3fba8d2007-10-16 01:26:06 -07001431 c->freelist = c->freelist[c->offset];
Christoph Lameter894b8782007-05-10 03:15:16 -07001432
1433 /* And put onto the regular freelist */
Christoph Lameterb3fba8d2007-10-16 01:26:06 -07001434 object[c->offset] = page->freelist;
Christoph Lameter894b8782007-05-10 03:15:16 -07001435 page->freelist = object;
1436 page->inuse--;
1437 }
Christoph Lameterdfb4f092007-10-16 01:26:05 -07001438 c->page = NULL;
Christoph Lameter7c2e1322008-01-07 23:20:27 -08001439 unfreeze_slab(s, page, tail);
Christoph Lameter81819f02007-05-06 14:49:36 -07001440}
1441
Christoph Lameterdfb4f092007-10-16 01:26:05 -07001442static inline void flush_slab(struct kmem_cache *s, struct kmem_cache_cpu *c)
Christoph Lameter81819f02007-05-06 14:49:36 -07001443{
Christoph Lameter8ff12cf2008-02-07 17:47:41 -08001444 stat(c, CPUSLAB_FLUSH);
Christoph Lameterdfb4f092007-10-16 01:26:05 -07001445 slab_lock(c->page);
1446 deactivate_slab(s, c);
Christoph Lameter81819f02007-05-06 14:49:36 -07001447}
1448
1449/*
1450 * Flush cpu slab.
Christoph Lameter6446faa2008-02-15 23:45:26 -08001451 *
Christoph Lameter81819f02007-05-06 14:49:36 -07001452 * Called from IPI handler with interrupts disabled.
1453 */
Christoph Lameter0c710012007-07-17 04:03:24 -07001454static inline void __flush_cpu_slab(struct kmem_cache *s, int cpu)
Christoph Lameter81819f02007-05-06 14:49:36 -07001455{
Christoph Lameterdfb4f092007-10-16 01:26:05 -07001456 struct kmem_cache_cpu *c = get_cpu_slab(s, cpu);
Christoph Lameter81819f02007-05-06 14:49:36 -07001457
Christoph Lameterdfb4f092007-10-16 01:26:05 -07001458 if (likely(c && c->page))
1459 flush_slab(s, c);
Christoph Lameter81819f02007-05-06 14:49:36 -07001460}
1461
1462static void flush_cpu_slab(void *d)
1463{
1464 struct kmem_cache *s = d;
Christoph Lameter81819f02007-05-06 14:49:36 -07001465
Christoph Lameterdfb4f092007-10-16 01:26:05 -07001466 __flush_cpu_slab(s, smp_processor_id());
Christoph Lameter81819f02007-05-06 14:49:36 -07001467}
1468
1469static void flush_all(struct kmem_cache *s)
1470{
Jens Axboe15c8b6c2008-05-09 09:39:44 +02001471 on_each_cpu(flush_cpu_slab, s, 1);
Christoph Lameter81819f02007-05-06 14:49:36 -07001472}
1473
1474/*
Christoph Lameterdfb4f092007-10-16 01:26:05 -07001475 * Check if the objects in a per cpu structure fit numa
1476 * locality expectations.
1477 */
1478static inline int node_match(struct kmem_cache_cpu *c, int node)
1479{
1480#ifdef CONFIG_NUMA
1481 if (node != -1 && c->node != node)
1482 return 0;
1483#endif
1484 return 1;
1485}
1486
Pekka Enberg781b2ba2009-06-10 18:50:32 +03001487static int count_free(struct page *page)
1488{
1489 return page->objects - page->inuse;
1490}
1491
1492static unsigned long count_partial(struct kmem_cache_node *n,
1493 int (*get_count)(struct page *))
1494{
1495 unsigned long flags;
1496 unsigned long x = 0;
1497 struct page *page;
1498
1499 spin_lock_irqsave(&n->list_lock, flags);
1500 list_for_each_entry(page, &n->partial, lru)
1501 x += get_count(page);
1502 spin_unlock_irqrestore(&n->list_lock, flags);
1503 return x;
1504}
1505
1506static noinline void
1507slab_out_of_memory(struct kmem_cache *s, gfp_t gfpflags, int nid)
1508{
1509 int node;
1510
1511 printk(KERN_WARNING
1512 "SLUB: Unable to allocate memory on node %d (gfp=0x%x)\n",
1513 nid, gfpflags);
1514 printk(KERN_WARNING " cache: %s, object size: %d, buffer size: %d, "
1515 "default order: %d, min order: %d\n", s->name, s->objsize,
1516 s->size, oo_order(s->oo), oo_order(s->min));
1517
1518 for_each_online_node(node) {
1519 struct kmem_cache_node *n = get_node(s, node);
1520 unsigned long nr_slabs;
1521 unsigned long nr_objs;
1522 unsigned long nr_free;
1523
1524 if (!n)
1525 continue;
1526
1527 nr_slabs = atomic_long_read(&n->nr_slabs);
1528 nr_objs = atomic_long_read(&n->total_objects);
1529 nr_free = count_partial(n, count_free);
1530
1531 printk(KERN_WARNING
1532 " node %d: slabs: %ld, objs: %ld, free: %ld\n",
1533 node, nr_slabs, nr_objs, nr_free);
1534 }
1535}
1536
Christoph Lameterdfb4f092007-10-16 01:26:05 -07001537/*
Christoph Lameter894b8782007-05-10 03:15:16 -07001538 * Slow path. The lockless freelist is empty or we need to perform
1539 * debugging duties.
Christoph Lameter81819f02007-05-06 14:49:36 -07001540 *
Christoph Lameter894b8782007-05-10 03:15:16 -07001541 * Interrupts are disabled.
Christoph Lameter81819f02007-05-06 14:49:36 -07001542 *
Christoph Lameter894b8782007-05-10 03:15:16 -07001543 * Processing is still very fast if new objects have been freed to the
1544 * regular freelist. In that case we simply take over the regular freelist
1545 * as the lockless freelist and zap the regular freelist.
Christoph Lameter81819f02007-05-06 14:49:36 -07001546 *
Christoph Lameter894b8782007-05-10 03:15:16 -07001547 * If that is not working then we fall back to the partial lists. We take the
1548 * first element of the freelist as the object to allocate now and move the
1549 * rest of the freelist to the lockless freelist.
1550 *
1551 * And if we were unable to get a new slab from the partial slab lists then
Christoph Lameter6446faa2008-02-15 23:45:26 -08001552 * we need to allocate a new slab. This is the slowest path since it involves
1553 * a call to the page allocator and the setup of a new slab.
Christoph Lameter81819f02007-05-06 14:49:36 -07001554 */
Eduard - Gabriel Munteanuce71e272008-08-19 20:43:25 +03001555static void *__slab_alloc(struct kmem_cache *s, gfp_t gfpflags, int node,
1556 unsigned long addr, struct kmem_cache_cpu *c)
Christoph Lameter81819f02007-05-06 14:49:36 -07001557{
Christoph Lameter81819f02007-05-06 14:49:36 -07001558 void **object;
Christoph Lameterdfb4f092007-10-16 01:26:05 -07001559 struct page *new;
Christoph Lameter81819f02007-05-06 14:49:36 -07001560
Linus Torvaldse72e9c22008-03-27 20:56:33 -07001561 /* We handle __GFP_ZERO in the caller */
1562 gfpflags &= ~__GFP_ZERO;
1563
Christoph Lameterdfb4f092007-10-16 01:26:05 -07001564 if (!c->page)
Christoph Lameter81819f02007-05-06 14:49:36 -07001565 goto new_slab;
1566
Christoph Lameterdfb4f092007-10-16 01:26:05 -07001567 slab_lock(c->page);
1568 if (unlikely(!node_match(c, node)))
Christoph Lameter81819f02007-05-06 14:49:36 -07001569 goto another_slab;
Christoph Lameter6446faa2008-02-15 23:45:26 -08001570
Christoph Lameter8ff12cf2008-02-07 17:47:41 -08001571 stat(c, ALLOC_REFILL);
Christoph Lameter6446faa2008-02-15 23:45:26 -08001572
Christoph Lameter894b8782007-05-10 03:15:16 -07001573load_freelist:
Christoph Lameterdfb4f092007-10-16 01:26:05 -07001574 object = c->page->freelist;
Christoph Lametera973e9d2008-03-01 13:40:44 -08001575 if (unlikely(!object))
Christoph Lameter81819f02007-05-06 14:49:36 -07001576 goto another_slab;
Andy Whitcroft8a380822008-07-23 21:27:18 -07001577 if (unlikely(SLABDEBUG && PageSlubDebug(c->page)))
Christoph Lameter81819f02007-05-06 14:49:36 -07001578 goto debug;
1579
Christoph Lameterb3fba8d2007-10-16 01:26:06 -07001580 c->freelist = object[c->offset];
Christoph Lameter39b26462008-04-14 19:11:30 +03001581 c->page->inuse = c->page->objects;
Christoph Lametera973e9d2008-03-01 13:40:44 -08001582 c->page->freelist = NULL;
Christoph Lameterdfb4f092007-10-16 01:26:05 -07001583 c->node = page_to_nid(c->page);
Christoph Lameter1f842602008-01-07 23:20:30 -08001584unlock_out:
Christoph Lameterdfb4f092007-10-16 01:26:05 -07001585 slab_unlock(c->page);
Christoph Lameter8ff12cf2008-02-07 17:47:41 -08001586 stat(c, ALLOC_SLOWPATH);
Christoph Lameter81819f02007-05-06 14:49:36 -07001587 return object;
1588
1589another_slab:
Christoph Lameterdfb4f092007-10-16 01:26:05 -07001590 deactivate_slab(s, c);
Christoph Lameter81819f02007-05-06 14:49:36 -07001591
1592new_slab:
Christoph Lameterdfb4f092007-10-16 01:26:05 -07001593 new = get_partial(s, gfpflags, node);
1594 if (new) {
1595 c->page = new;
Christoph Lameter8ff12cf2008-02-07 17:47:41 -08001596 stat(c, ALLOC_FROM_PARTIAL);
Christoph Lameter894b8782007-05-10 03:15:16 -07001597 goto load_freelist;
Christoph Lameter81819f02007-05-06 14:49:36 -07001598 }
1599
Christoph Lameterb811c202007-10-16 23:25:51 -07001600 if (gfpflags & __GFP_WAIT)
1601 local_irq_enable();
1602
Christoph Lameterdfb4f092007-10-16 01:26:05 -07001603 new = new_slab(s, gfpflags, node);
Christoph Lameterb811c202007-10-16 23:25:51 -07001604
1605 if (gfpflags & __GFP_WAIT)
1606 local_irq_disable();
1607
Christoph Lameterdfb4f092007-10-16 01:26:05 -07001608 if (new) {
1609 c = get_cpu_slab(s, smp_processor_id());
Christoph Lameter8ff12cf2008-02-07 17:47:41 -08001610 stat(c, ALLOC_SLAB);
Christoph Lameter05aa3452007-11-05 11:31:58 -08001611 if (c->page)
Christoph Lameterdfb4f092007-10-16 01:26:05 -07001612 flush_slab(s, c);
Christoph Lameterdfb4f092007-10-16 01:26:05 -07001613 slab_lock(new);
Andy Whitcroft8a380822008-07-23 21:27:18 -07001614 __SetPageSlubFrozen(new);
Christoph Lameterdfb4f092007-10-16 01:26:05 -07001615 c->page = new;
Christoph Lameter4b6f0752007-05-16 22:10:53 -07001616 goto load_freelist;
Christoph Lameter81819f02007-05-06 14:49:36 -07001617 }
Pekka Enberg781b2ba2009-06-10 18:50:32 +03001618 slab_out_of_memory(s, gfpflags, node);
Christoph Lameter71c7a062008-02-14 14:28:01 -08001619 return NULL;
Christoph Lameter81819f02007-05-06 14:49:36 -07001620debug:
Christoph Lameterdfb4f092007-10-16 01:26:05 -07001621 if (!alloc_debug_processing(s, c->page, object, addr))
Christoph Lameter81819f02007-05-06 14:49:36 -07001622 goto another_slab;
Christoph Lameter894b8782007-05-10 03:15:16 -07001623
Christoph Lameterdfb4f092007-10-16 01:26:05 -07001624 c->page->inuse++;
Christoph Lameterb3fba8d2007-10-16 01:26:06 -07001625 c->page->freelist = object[c->offset];
Christoph Lameteree3c72a2007-10-16 01:26:07 -07001626 c->node = -1;
Christoph Lameter1f842602008-01-07 23:20:30 -08001627 goto unlock_out;
Christoph Lameter894b8782007-05-10 03:15:16 -07001628}
1629
1630/*
1631 * Inlined fastpath so that allocation functions (kmalloc, kmem_cache_alloc)
1632 * have the fastpath folded into their functions. So no function call
1633 * overhead for requests that can be satisfied on the fastpath.
1634 *
1635 * The fastpath works by first checking if the lockless freelist can be used.
1636 * If not then __slab_alloc is called for slow processing.
1637 *
1638 * Otherwise we can simply pick the next object from the lockless free list.
1639 */
Pekka Enberg06428782008-01-07 23:20:27 -08001640static __always_inline void *slab_alloc(struct kmem_cache *s,
Eduard - Gabriel Munteanuce71e272008-08-19 20:43:25 +03001641 gfp_t gfpflags, int node, unsigned long addr)
Christoph Lameter894b8782007-05-10 03:15:16 -07001642{
Christoph Lameter894b8782007-05-10 03:15:16 -07001643 void **object;
Christoph Lameterdfb4f092007-10-16 01:26:05 -07001644 struct kmem_cache_cpu *c;
Christoph Lameter1f842602008-01-07 23:20:30 -08001645 unsigned long flags;
Dmitry Adamushkobdb21922008-07-10 22:21:58 +02001646 unsigned int objsize;
Christoph Lameter1f842602008-01-07 23:20:30 -08001647
Nick Piggincf40bd12009-01-21 08:12:39 +01001648 lockdep_trace_alloc(gfpflags);
OGAWA Hirofumi89124d72008-11-19 21:23:59 +09001649 might_sleep_if(gfpflags & __GFP_WAIT);
Pekka Enberg3c506ef2008-12-29 11:47:05 +02001650
Akinobu Mita773ff602008-12-23 19:37:01 +09001651 if (should_failslab(s->objsize, gfpflags))
1652 return NULL;
1653
Christoph Lameter894b8782007-05-10 03:15:16 -07001654 local_irq_save(flags);
Christoph Lameterdfb4f092007-10-16 01:26:05 -07001655 c = get_cpu_slab(s, smp_processor_id());
Dmitry Adamushkobdb21922008-07-10 22:21:58 +02001656 objsize = c->objsize;
Christoph Lametera973e9d2008-03-01 13:40:44 -08001657 if (unlikely(!c->freelist || !node_match(c, node)))
Christoph Lameter894b8782007-05-10 03:15:16 -07001658
Christoph Lameterdfb4f092007-10-16 01:26:05 -07001659 object = __slab_alloc(s, gfpflags, node, addr, c);
Christoph Lameter894b8782007-05-10 03:15:16 -07001660
1661 else {
Christoph Lameterdfb4f092007-10-16 01:26:05 -07001662 object = c->freelist;
Christoph Lameterb3fba8d2007-10-16 01:26:06 -07001663 c->freelist = object[c->offset];
Christoph Lameter8ff12cf2008-02-07 17:47:41 -08001664 stat(c, ALLOC_FASTPATH);
Christoph Lameter894b8782007-05-10 03:15:16 -07001665 }
1666 local_irq_restore(flags);
Christoph Lameterd07dbea2007-07-17 04:03:23 -07001667
1668 if (unlikely((gfpflags & __GFP_ZERO) && object))
Dmitry Adamushkobdb21922008-07-10 22:21:58 +02001669 memset(object, 0, objsize);
Christoph Lameterd07dbea2007-07-17 04:03:23 -07001670
Christoph Lameter894b8782007-05-10 03:15:16 -07001671 return object;
Christoph Lameter81819f02007-05-06 14:49:36 -07001672}
1673
1674void *kmem_cache_alloc(struct kmem_cache *s, gfp_t gfpflags)
1675{
Eduard - Gabriel Munteanu5b882be2008-08-19 20:43:26 +03001676 void *ret = slab_alloc(s, gfpflags, -1, _RET_IP_);
1677
Eduard - Gabriel Munteanuca2b84c2009-03-23 15:12:24 +02001678 trace_kmem_cache_alloc(_RET_IP_, ret, s->objsize, s->size, gfpflags);
Eduard - Gabriel Munteanu5b882be2008-08-19 20:43:26 +03001679
1680 return ret;
Christoph Lameter81819f02007-05-06 14:49:36 -07001681}
1682EXPORT_SYMBOL(kmem_cache_alloc);
1683
Eduard - Gabriel Munteanu5b882be2008-08-19 20:43:26 +03001684#ifdef CONFIG_KMEMTRACE
1685void *kmem_cache_alloc_notrace(struct kmem_cache *s, gfp_t gfpflags)
1686{
1687 return slab_alloc(s, gfpflags, -1, _RET_IP_);
1688}
1689EXPORT_SYMBOL(kmem_cache_alloc_notrace);
1690#endif
1691
Christoph Lameter81819f02007-05-06 14:49:36 -07001692#ifdef CONFIG_NUMA
1693void *kmem_cache_alloc_node(struct kmem_cache *s, gfp_t gfpflags, int node)
1694{
Eduard - Gabriel Munteanu5b882be2008-08-19 20:43:26 +03001695 void *ret = slab_alloc(s, gfpflags, node, _RET_IP_);
1696
Eduard - Gabriel Munteanuca2b84c2009-03-23 15:12:24 +02001697 trace_kmem_cache_alloc_node(_RET_IP_, ret,
1698 s->objsize, s->size, gfpflags, node);
Eduard - Gabriel Munteanu5b882be2008-08-19 20:43:26 +03001699
1700 return ret;
Christoph Lameter81819f02007-05-06 14:49:36 -07001701}
1702EXPORT_SYMBOL(kmem_cache_alloc_node);
1703#endif
1704
Eduard - Gabriel Munteanu5b882be2008-08-19 20:43:26 +03001705#ifdef CONFIG_KMEMTRACE
1706void *kmem_cache_alloc_node_notrace(struct kmem_cache *s,
1707 gfp_t gfpflags,
1708 int node)
1709{
1710 return slab_alloc(s, gfpflags, node, _RET_IP_);
1711}
1712EXPORT_SYMBOL(kmem_cache_alloc_node_notrace);
1713#endif
1714
Christoph Lameter81819f02007-05-06 14:49:36 -07001715/*
Christoph Lameter894b8782007-05-10 03:15:16 -07001716 * Slow patch handling. This may still be called frequently since objects
1717 * have a longer lifetime than the cpu slabs in most processing loads.
Christoph Lameter81819f02007-05-06 14:49:36 -07001718 *
Christoph Lameter894b8782007-05-10 03:15:16 -07001719 * So we still attempt to reduce cache line usage. Just take the slab
1720 * lock and free the item. If there is no additional partial page
1721 * handling required then we can return immediately.
Christoph Lameter81819f02007-05-06 14:49:36 -07001722 */
Christoph Lameter894b8782007-05-10 03:15:16 -07001723static void __slab_free(struct kmem_cache *s, struct page *page,
Eduard - Gabriel Munteanuce71e272008-08-19 20:43:25 +03001724 void *x, unsigned long addr, unsigned int offset)
Christoph Lameter81819f02007-05-06 14:49:36 -07001725{
1726 void *prior;
1727 void **object = (void *)x;
Christoph Lameter8ff12cf2008-02-07 17:47:41 -08001728 struct kmem_cache_cpu *c;
Christoph Lameter81819f02007-05-06 14:49:36 -07001729
Christoph Lameter8ff12cf2008-02-07 17:47:41 -08001730 c = get_cpu_slab(s, raw_smp_processor_id());
1731 stat(c, FREE_SLOWPATH);
Christoph Lameter81819f02007-05-06 14:49:36 -07001732 slab_lock(page);
1733
Andy Whitcroft8a380822008-07-23 21:27:18 -07001734 if (unlikely(SLABDEBUG && PageSlubDebug(page)))
Christoph Lameter81819f02007-05-06 14:49:36 -07001735 goto debug;
Christoph Lameter6446faa2008-02-15 23:45:26 -08001736
Christoph Lameter81819f02007-05-06 14:49:36 -07001737checks_ok:
Christoph Lameterb3fba8d2007-10-16 01:26:06 -07001738 prior = object[offset] = page->freelist;
Christoph Lameter81819f02007-05-06 14:49:36 -07001739 page->freelist = object;
1740 page->inuse--;
1741
Andy Whitcroft8a380822008-07-23 21:27:18 -07001742 if (unlikely(PageSlubFrozen(page))) {
Christoph Lameter8ff12cf2008-02-07 17:47:41 -08001743 stat(c, FREE_FROZEN);
Christoph Lameter81819f02007-05-06 14:49:36 -07001744 goto out_unlock;
Christoph Lameter8ff12cf2008-02-07 17:47:41 -08001745 }
Christoph Lameter81819f02007-05-06 14:49:36 -07001746
1747 if (unlikely(!page->inuse))
1748 goto slab_empty;
1749
1750 /*
Christoph Lameter6446faa2008-02-15 23:45:26 -08001751 * Objects left in the slab. If it was not on the partial list before
Christoph Lameter81819f02007-05-06 14:49:36 -07001752 * then add it.
1753 */
Christoph Lametera973e9d2008-03-01 13:40:44 -08001754 if (unlikely(!prior)) {
Christoph Lameter7c2e1322008-01-07 23:20:27 -08001755 add_partial(get_node(s, page_to_nid(page)), page, 1);
Christoph Lameter8ff12cf2008-02-07 17:47:41 -08001756 stat(c, FREE_ADD_PARTIAL);
1757 }
Christoph Lameter81819f02007-05-06 14:49:36 -07001758
1759out_unlock:
1760 slab_unlock(page);
Christoph Lameter81819f02007-05-06 14:49:36 -07001761 return;
1762
1763slab_empty:
Christoph Lametera973e9d2008-03-01 13:40:44 -08001764 if (prior) {
Christoph Lameter81819f02007-05-06 14:49:36 -07001765 /*
Christoph Lameter672bba32007-05-09 02:32:39 -07001766 * Slab still on the partial list.
Christoph Lameter81819f02007-05-06 14:49:36 -07001767 */
1768 remove_partial(s, page);
Christoph Lameter8ff12cf2008-02-07 17:47:41 -08001769 stat(c, FREE_REMOVE_PARTIAL);
1770 }
Christoph Lameter81819f02007-05-06 14:49:36 -07001771 slab_unlock(page);
Christoph Lameter8ff12cf2008-02-07 17:47:41 -08001772 stat(c, FREE_SLAB);
Christoph Lameter81819f02007-05-06 14:49:36 -07001773 discard_slab(s, page);
Christoph Lameter81819f02007-05-06 14:49:36 -07001774 return;
1775
1776debug:
Christoph Lameter3ec09742007-05-16 22:11:00 -07001777 if (!free_debug_processing(s, page, x, addr))
Christoph Lameter77c5e2d2007-05-06 14:49:42 -07001778 goto out_unlock;
Christoph Lameter77c5e2d2007-05-06 14:49:42 -07001779 goto checks_ok;
Christoph Lameter81819f02007-05-06 14:49:36 -07001780}
1781
Christoph Lameter894b8782007-05-10 03:15:16 -07001782/*
1783 * Fastpath with forced inlining to produce a kfree and kmem_cache_free that
1784 * can perform fastpath freeing without additional function calls.
1785 *
1786 * The fastpath is only possible if we are freeing to the current cpu slab
1787 * of this processor. This typically the case if we have just allocated
1788 * the item before.
1789 *
1790 * If fastpath is not possible then fall back to __slab_free where we deal
1791 * with all sorts of special processing.
1792 */
Pekka Enberg06428782008-01-07 23:20:27 -08001793static __always_inline void slab_free(struct kmem_cache *s,
Eduard - Gabriel Munteanuce71e272008-08-19 20:43:25 +03001794 struct page *page, void *x, unsigned long addr)
Christoph Lameter894b8782007-05-10 03:15:16 -07001795{
1796 void **object = (void *)x;
Christoph Lameterdfb4f092007-10-16 01:26:05 -07001797 struct kmem_cache_cpu *c;
Christoph Lameter1f842602008-01-07 23:20:30 -08001798 unsigned long flags;
1799
Christoph Lameter894b8782007-05-10 03:15:16 -07001800 local_irq_save(flags);
Christoph Lameterdfb4f092007-10-16 01:26:05 -07001801 c = get_cpu_slab(s, smp_processor_id());
Christoph Lameter27d9e4e2008-02-15 23:45:25 -08001802 debug_check_no_locks_freed(object, c->objsize);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -07001803 if (!(s->flags & SLAB_DEBUG_OBJECTS))
Pekka Enberg6047a002009-01-14 12:22:25 +02001804 debug_check_no_obj_freed(object, c->objsize);
Christoph Lameteree3c72a2007-10-16 01:26:07 -07001805 if (likely(page == c->page && c->node >= 0)) {
Christoph Lameterb3fba8d2007-10-16 01:26:06 -07001806 object[c->offset] = c->freelist;
Christoph Lameterdfb4f092007-10-16 01:26:05 -07001807 c->freelist = object;
Christoph Lameter8ff12cf2008-02-07 17:47:41 -08001808 stat(c, FREE_FASTPATH);
Christoph Lameter894b8782007-05-10 03:15:16 -07001809 } else
Christoph Lameterb3fba8d2007-10-16 01:26:06 -07001810 __slab_free(s, page, x, addr, c->offset);
Christoph Lameter894b8782007-05-10 03:15:16 -07001811
1812 local_irq_restore(flags);
1813}
1814
Christoph Lameter81819f02007-05-06 14:49:36 -07001815void kmem_cache_free(struct kmem_cache *s, void *x)
1816{
Christoph Lameter77c5e2d2007-05-06 14:49:42 -07001817 struct page *page;
Christoph Lameter81819f02007-05-06 14:49:36 -07001818
Christoph Lameterb49af682007-05-06 14:49:41 -07001819 page = virt_to_head_page(x);
Christoph Lameter81819f02007-05-06 14:49:36 -07001820
Eduard - Gabriel Munteanuce71e272008-08-19 20:43:25 +03001821 slab_free(s, page, x, _RET_IP_);
Eduard - Gabriel Munteanu5b882be2008-08-19 20:43:26 +03001822
Eduard - Gabriel Munteanuca2b84c2009-03-23 15:12:24 +02001823 trace_kmem_cache_free(_RET_IP_, x);
Christoph Lameter81819f02007-05-06 14:49:36 -07001824}
1825EXPORT_SYMBOL(kmem_cache_free);
1826
Cyrill Gorcunove9beef12008-10-28 22:02:26 +03001827/* Figure out on which slab page the object resides */
Christoph Lameter81819f02007-05-06 14:49:36 -07001828static struct page *get_object_page(const void *x)
1829{
Christoph Lameterb49af682007-05-06 14:49:41 -07001830 struct page *page = virt_to_head_page(x);
Christoph Lameter81819f02007-05-06 14:49:36 -07001831
1832 if (!PageSlab(page))
1833 return NULL;
1834
1835 return page;
1836}
1837
1838/*
Christoph Lameter672bba32007-05-09 02:32:39 -07001839 * Object placement in a slab is made very easy because we always start at
1840 * offset 0. If we tune the size of the object to the alignment then we can
1841 * get the required alignment by putting one properly sized object after
1842 * another.
Christoph Lameter81819f02007-05-06 14:49:36 -07001843 *
1844 * Notice that the allocation order determines the sizes of the per cpu
1845 * caches. Each processor has always one slab available for allocations.
1846 * Increasing the allocation order reduces the number of times that slabs
Christoph Lameter672bba32007-05-09 02:32:39 -07001847 * must be moved on and off the partial lists and is therefore a factor in
Christoph Lameter81819f02007-05-06 14:49:36 -07001848 * locking overhead.
Christoph Lameter81819f02007-05-06 14:49:36 -07001849 */
1850
1851/*
1852 * Mininum / Maximum order of slab pages. This influences locking overhead
1853 * and slab fragmentation. A higher order reduces the number of partial slabs
1854 * and increases the number of allocations possible without having to
1855 * take the list_lock.
1856 */
1857static int slub_min_order;
Christoph Lameter114e9e82008-04-14 19:11:41 +03001858static int slub_max_order = PAGE_ALLOC_COSTLY_ORDER;
Christoph Lameter9b2cd502008-04-14 19:11:41 +03001859static int slub_min_objects;
Christoph Lameter81819f02007-05-06 14:49:36 -07001860
1861/*
1862 * Merge control. If this is set then no merging of slab caches will occur.
Christoph Lameter672bba32007-05-09 02:32:39 -07001863 * (Could be removed. This was introduced to pacify the merge skeptics.)
Christoph Lameter81819f02007-05-06 14:49:36 -07001864 */
1865static int slub_nomerge;
1866
1867/*
Christoph Lameter81819f02007-05-06 14:49:36 -07001868 * Calculate the order of allocation given an slab object size.
1869 *
Christoph Lameter672bba32007-05-09 02:32:39 -07001870 * The order of allocation has significant impact on performance and other
1871 * system components. Generally order 0 allocations should be preferred since
1872 * order 0 does not cause fragmentation in the page allocator. Larger objects
1873 * be problematic to put into order 0 slabs because there may be too much
Christoph Lameterc124f5b2008-04-14 19:13:29 +03001874 * unused space left. We go to a higher order if more than 1/16th of the slab
Christoph Lameter672bba32007-05-09 02:32:39 -07001875 * would be wasted.
Christoph Lameter81819f02007-05-06 14:49:36 -07001876 *
Christoph Lameter672bba32007-05-09 02:32:39 -07001877 * In order to reach satisfactory performance we must ensure that a minimum
1878 * number of objects is in one slab. Otherwise we may generate too much
1879 * activity on the partial lists which requires taking the list_lock. This is
1880 * less a concern for large slabs though which are rarely used.
Christoph Lameter81819f02007-05-06 14:49:36 -07001881 *
Christoph Lameter672bba32007-05-09 02:32:39 -07001882 * slub_max_order specifies the order where we begin to stop considering the
1883 * number of objects in a slab as critical. If we reach slub_max_order then
1884 * we try to keep the page order as low as possible. So we accept more waste
1885 * of space in favor of a small page order.
1886 *
1887 * Higher order allocations also allow the placement of more objects in a
1888 * slab and thereby reduce object handling overhead. If the user has
1889 * requested a higher mininum order then we start with that one instead of
1890 * the smallest order which will fit the object.
Christoph Lameter81819f02007-05-06 14:49:36 -07001891 */
Christoph Lameter5e6d4442007-05-09 02:32:46 -07001892static inline int slab_order(int size, int min_objects,
1893 int max_order, int fract_leftover)
Christoph Lameter81819f02007-05-06 14:49:36 -07001894{
1895 int order;
1896 int rem;
Christoph Lameter6300ea72007-07-17 04:03:20 -07001897 int min_order = slub_min_order;
Christoph Lameter81819f02007-05-06 14:49:36 -07001898
Cyrill Gorcunov210b5c02008-10-22 23:00:38 +04001899 if ((PAGE_SIZE << min_order) / size > MAX_OBJS_PER_PAGE)
1900 return get_order(size * MAX_OBJS_PER_PAGE) - 1;
Christoph Lameter39b26462008-04-14 19:11:30 +03001901
Christoph Lameter6300ea72007-07-17 04:03:20 -07001902 for (order = max(min_order,
Christoph Lameter5e6d4442007-05-09 02:32:46 -07001903 fls(min_objects * size - 1) - PAGE_SHIFT);
1904 order <= max_order; order++) {
1905
Christoph Lameter81819f02007-05-06 14:49:36 -07001906 unsigned long slab_size = PAGE_SIZE << order;
1907
Christoph Lameter5e6d4442007-05-09 02:32:46 -07001908 if (slab_size < min_objects * size)
Christoph Lameter81819f02007-05-06 14:49:36 -07001909 continue;
1910
Christoph Lameter81819f02007-05-06 14:49:36 -07001911 rem = slab_size % size;
1912
Christoph Lameter5e6d4442007-05-09 02:32:46 -07001913 if (rem <= slab_size / fract_leftover)
Christoph Lameter81819f02007-05-06 14:49:36 -07001914 break;
1915
1916 }
Christoph Lameter672bba32007-05-09 02:32:39 -07001917
Christoph Lameter81819f02007-05-06 14:49:36 -07001918 return order;
1919}
1920
Christoph Lameter5e6d4442007-05-09 02:32:46 -07001921static inline int calculate_order(int size)
1922{
1923 int order;
1924 int min_objects;
1925 int fraction;
Zhang Yanmine8120ff2009-02-12 18:00:17 +02001926 int max_objects;
Christoph Lameter5e6d4442007-05-09 02:32:46 -07001927
1928 /*
1929 * Attempt to find best configuration for a slab. This
1930 * works by first attempting to generate a layout with
1931 * the best configuration and backing off gradually.
1932 *
1933 * First we reduce the acceptable waste in a slab. Then
1934 * we reduce the minimum objects required in a slab.
1935 */
1936 min_objects = slub_min_objects;
Christoph Lameter9b2cd502008-04-14 19:11:41 +03001937 if (!min_objects)
1938 min_objects = 4 * (fls(nr_cpu_ids) + 1);
Zhang Yanmine8120ff2009-02-12 18:00:17 +02001939 max_objects = (PAGE_SIZE << slub_max_order)/size;
1940 min_objects = min(min_objects, max_objects);
1941
Christoph Lameter5e6d4442007-05-09 02:32:46 -07001942 while (min_objects > 1) {
Christoph Lameterc124f5b2008-04-14 19:13:29 +03001943 fraction = 16;
Christoph Lameter5e6d4442007-05-09 02:32:46 -07001944 while (fraction >= 4) {
1945 order = slab_order(size, min_objects,
1946 slub_max_order, fraction);
1947 if (order <= slub_max_order)
1948 return order;
1949 fraction /= 2;
1950 }
Zhang Yanmine8120ff2009-02-12 18:00:17 +02001951 min_objects --;
Christoph Lameter5e6d4442007-05-09 02:32:46 -07001952 }
1953
1954 /*
1955 * We were unable to place multiple objects in a slab. Now
1956 * lets see if we can place a single object there.
1957 */
1958 order = slab_order(size, 1, slub_max_order, 1);
1959 if (order <= slub_max_order)
1960 return order;
1961
1962 /*
1963 * Doh this slab cannot be placed using slub_max_order.
1964 */
1965 order = slab_order(size, 1, MAX_ORDER, 1);
David Rientjes818cf592009-04-23 09:58:22 +03001966 if (order < MAX_ORDER)
Christoph Lameter5e6d4442007-05-09 02:32:46 -07001967 return order;
1968 return -ENOSYS;
1969}
1970
Christoph Lameter81819f02007-05-06 14:49:36 -07001971/*
Christoph Lameter672bba32007-05-09 02:32:39 -07001972 * Figure out what the alignment of the objects will be.
Christoph Lameter81819f02007-05-06 14:49:36 -07001973 */
1974static unsigned long calculate_alignment(unsigned long flags,
1975 unsigned long align, unsigned long size)
1976{
1977 /*
Christoph Lameter6446faa2008-02-15 23:45:26 -08001978 * If the user wants hardware cache aligned objects then follow that
1979 * suggestion if the object is sufficiently large.
Christoph Lameter81819f02007-05-06 14:49:36 -07001980 *
Christoph Lameter6446faa2008-02-15 23:45:26 -08001981 * The hardware cache alignment cannot override the specified
1982 * alignment though. If that is greater then use it.
Christoph Lameter81819f02007-05-06 14:49:36 -07001983 */
Nick Pigginb6210382008-03-05 14:05:56 -08001984 if (flags & SLAB_HWCACHE_ALIGN) {
1985 unsigned long ralign = cache_line_size();
1986 while (size <= ralign / 2)
1987 ralign /= 2;
1988 align = max(align, ralign);
1989 }
Christoph Lameter81819f02007-05-06 14:49:36 -07001990
1991 if (align < ARCH_SLAB_MINALIGN)
Nick Pigginb6210382008-03-05 14:05:56 -08001992 align = ARCH_SLAB_MINALIGN;
Christoph Lameter81819f02007-05-06 14:49:36 -07001993
1994 return ALIGN(align, sizeof(void *));
1995}
1996
Christoph Lameterdfb4f092007-10-16 01:26:05 -07001997static void init_kmem_cache_cpu(struct kmem_cache *s,
1998 struct kmem_cache_cpu *c)
1999{
2000 c->page = NULL;
Christoph Lametera973e9d2008-03-01 13:40:44 -08002001 c->freelist = NULL;
Christoph Lameterdfb4f092007-10-16 01:26:05 -07002002 c->node = 0;
Christoph Lameter42a9fdb2007-10-16 01:26:09 -07002003 c->offset = s->offset / sizeof(void *);
2004 c->objsize = s->objsize;
Pekka Enberg62f75532008-04-14 18:50:44 +03002005#ifdef CONFIG_SLUB_STATS
2006 memset(c->stat, 0, NR_SLUB_STAT_ITEMS * sizeof(unsigned));
2007#endif
Christoph Lameterdfb4f092007-10-16 01:26:05 -07002008}
2009
Pekka Enberg5595cff2008-08-05 09:28:47 +03002010static void
2011init_kmem_cache_node(struct kmem_cache_node *n, struct kmem_cache *s)
Christoph Lameter81819f02007-05-06 14:49:36 -07002012{
2013 n->nr_partial = 0;
Christoph Lameter81819f02007-05-06 14:49:36 -07002014 spin_lock_init(&n->list_lock);
2015 INIT_LIST_HEAD(&n->partial);
Christoph Lameter8ab13722007-07-17 04:03:32 -07002016#ifdef CONFIG_SLUB_DEBUG
Christoph Lameter0f389ec2008-04-14 18:53:02 +03002017 atomic_long_set(&n->nr_slabs, 0);
Salman Qazi02b71b72008-09-11 12:25:41 -07002018 atomic_long_set(&n->total_objects, 0);
Christoph Lameter643b1132007-05-06 14:49:42 -07002019 INIT_LIST_HEAD(&n->full);
Christoph Lameter8ab13722007-07-17 04:03:32 -07002020#endif
Christoph Lameter81819f02007-05-06 14:49:36 -07002021}
2022
Christoph Lameter4c93c3552007-10-16 01:26:08 -07002023#ifdef CONFIG_SMP
2024/*
2025 * Per cpu array for per cpu structures.
2026 *
2027 * The per cpu array places all kmem_cache_cpu structures from one processor
2028 * close together meaning that it becomes possible that multiple per cpu
2029 * structures are contained in one cacheline. This may be particularly
2030 * beneficial for the kmalloc caches.
2031 *
2032 * A desktop system typically has around 60-80 slabs. With 100 here we are
2033 * likely able to get per cpu structures for all caches from the array defined
2034 * here. We must be able to cover all kmalloc caches during bootstrap.
2035 *
2036 * If the per cpu array is exhausted then fall back to kmalloc
2037 * of individual cachelines. No sharing is possible then.
2038 */
2039#define NR_KMEM_CACHE_CPU 100
2040
2041static DEFINE_PER_CPU(struct kmem_cache_cpu,
2042 kmem_cache_cpu)[NR_KMEM_CACHE_CPU];
2043
2044static DEFINE_PER_CPU(struct kmem_cache_cpu *, kmem_cache_cpu_free);
Rusty Russell174596a2009-01-01 10:12:29 +10302045static DECLARE_BITMAP(kmem_cach_cpu_free_init_once, CONFIG_NR_CPUS);
Christoph Lameter4c93c3552007-10-16 01:26:08 -07002046
2047static struct kmem_cache_cpu *alloc_kmem_cache_cpu(struct kmem_cache *s,
2048 int cpu, gfp_t flags)
2049{
2050 struct kmem_cache_cpu *c = per_cpu(kmem_cache_cpu_free, cpu);
2051
2052 if (c)
2053 per_cpu(kmem_cache_cpu_free, cpu) =
2054 (void *)c->freelist;
2055 else {
2056 /* Table overflow: So allocate ourselves */
2057 c = kmalloc_node(
2058 ALIGN(sizeof(struct kmem_cache_cpu), cache_line_size()),
2059 flags, cpu_to_node(cpu));
2060 if (!c)
2061 return NULL;
2062 }
2063
2064 init_kmem_cache_cpu(s, c);
2065 return c;
2066}
2067
2068static void free_kmem_cache_cpu(struct kmem_cache_cpu *c, int cpu)
2069{
2070 if (c < per_cpu(kmem_cache_cpu, cpu) ||
David Rientjes37189092009-01-27 18:59:46 -08002071 c >= per_cpu(kmem_cache_cpu, cpu) + NR_KMEM_CACHE_CPU) {
Christoph Lameter4c93c3552007-10-16 01:26:08 -07002072 kfree(c);
2073 return;
2074 }
2075 c->freelist = (void *)per_cpu(kmem_cache_cpu_free, cpu);
2076 per_cpu(kmem_cache_cpu_free, cpu) = c;
2077}
2078
2079static void free_kmem_cache_cpus(struct kmem_cache *s)
2080{
2081 int cpu;
2082
2083 for_each_online_cpu(cpu) {
2084 struct kmem_cache_cpu *c = get_cpu_slab(s, cpu);
2085
2086 if (c) {
2087 s->cpu_slab[cpu] = NULL;
2088 free_kmem_cache_cpu(c, cpu);
2089 }
2090 }
2091}
2092
2093static int alloc_kmem_cache_cpus(struct kmem_cache *s, gfp_t flags)
2094{
2095 int cpu;
2096
2097 for_each_online_cpu(cpu) {
2098 struct kmem_cache_cpu *c = get_cpu_slab(s, cpu);
2099
2100 if (c)
2101 continue;
2102
2103 c = alloc_kmem_cache_cpu(s, cpu, flags);
2104 if (!c) {
2105 free_kmem_cache_cpus(s);
2106 return 0;
2107 }
2108 s->cpu_slab[cpu] = c;
2109 }
2110 return 1;
2111}
2112
2113/*
2114 * Initialize the per cpu array.
2115 */
2116static void init_alloc_cpu_cpu(int cpu)
2117{
2118 int i;
2119
Rusty Russell174596a2009-01-01 10:12:29 +10302120 if (cpumask_test_cpu(cpu, to_cpumask(kmem_cach_cpu_free_init_once)))
Christoph Lameter4c93c3552007-10-16 01:26:08 -07002121 return;
2122
2123 for (i = NR_KMEM_CACHE_CPU - 1; i >= 0; i--)
2124 free_kmem_cache_cpu(&per_cpu(kmem_cache_cpu, cpu)[i], cpu);
2125
Rusty Russell174596a2009-01-01 10:12:29 +10302126 cpumask_set_cpu(cpu, to_cpumask(kmem_cach_cpu_free_init_once));
Christoph Lameter4c93c3552007-10-16 01:26:08 -07002127}
2128
2129static void __init init_alloc_cpu(void)
2130{
2131 int cpu;
2132
2133 for_each_online_cpu(cpu)
2134 init_alloc_cpu_cpu(cpu);
2135 }
2136
2137#else
2138static inline void free_kmem_cache_cpus(struct kmem_cache *s) {}
2139static inline void init_alloc_cpu(void) {}
2140
2141static inline int alloc_kmem_cache_cpus(struct kmem_cache *s, gfp_t flags)
2142{
2143 init_kmem_cache_cpu(s, &s->cpu_slab);
2144 return 1;
2145}
2146#endif
2147
Christoph Lameter81819f02007-05-06 14:49:36 -07002148#ifdef CONFIG_NUMA
2149/*
2150 * No kmalloc_node yet so do it by hand. We know that this is the first
2151 * slab on the node for this slabcache. There are no concurrent accesses
2152 * possible.
2153 *
2154 * Note that this function only works on the kmalloc_node_cache
Christoph Lameter4c93c3552007-10-16 01:26:08 -07002155 * when allocating for the kmalloc_node_cache. This is used for bootstrapping
2156 * memory on a fresh node that has no slab structures yet.
Christoph Lameter81819f02007-05-06 14:49:36 -07002157 */
David Rientjes0094de92008-11-25 19:14:19 -08002158static void early_kmem_cache_node_alloc(gfp_t gfpflags, int node)
Christoph Lameter81819f02007-05-06 14:49:36 -07002159{
2160 struct page *page;
2161 struct kmem_cache_node *n;
rootba84c732008-01-07 23:20:28 -08002162 unsigned long flags;
Christoph Lameter81819f02007-05-06 14:49:36 -07002163
2164 BUG_ON(kmalloc_caches->size < sizeof(struct kmem_cache_node));
2165
Christoph Lametera2f92ee2007-08-22 14:01:57 -07002166 page = new_slab(kmalloc_caches, gfpflags, node);
Christoph Lameter81819f02007-05-06 14:49:36 -07002167
2168 BUG_ON(!page);
Christoph Lametera2f92ee2007-08-22 14:01:57 -07002169 if (page_to_nid(page) != node) {
2170 printk(KERN_ERR "SLUB: Unable to allocate memory from "
2171 "node %d\n", node);
2172 printk(KERN_ERR "SLUB: Allocating a useless per node structure "
2173 "in order to be able to continue\n");
2174 }
2175
Christoph Lameter81819f02007-05-06 14:49:36 -07002176 n = page->freelist;
2177 BUG_ON(!n);
2178 page->freelist = get_freepointer(kmalloc_caches, n);
2179 page->inuse++;
2180 kmalloc_caches->node[node] = n;
Christoph Lameter8ab13722007-07-17 04:03:32 -07002181#ifdef CONFIG_SLUB_DEBUG
Christoph Lameterd45f39c2007-07-17 04:03:21 -07002182 init_object(kmalloc_caches, n, 1);
2183 init_tracking(kmalloc_caches, n);
Christoph Lameter8ab13722007-07-17 04:03:32 -07002184#endif
Pekka Enberg5595cff2008-08-05 09:28:47 +03002185 init_kmem_cache_node(n, kmalloc_caches);
Christoph Lameter205ab992008-04-14 19:11:40 +03002186 inc_slabs_node(kmalloc_caches, node, page->objects);
Christoph Lameter6446faa2008-02-15 23:45:26 -08002187
rootba84c732008-01-07 23:20:28 -08002188 /*
2189 * lockdep requires consistent irq usage for each lock
2190 * so even though there cannot be a race this early in
2191 * the boot sequence, we still disable irqs.
2192 */
2193 local_irq_save(flags);
Christoph Lameter7c2e1322008-01-07 23:20:27 -08002194 add_partial(n, page, 0);
rootba84c732008-01-07 23:20:28 -08002195 local_irq_restore(flags);
Christoph Lameter81819f02007-05-06 14:49:36 -07002196}
2197
2198static void free_kmem_cache_nodes(struct kmem_cache *s)
2199{
2200 int node;
2201
Christoph Lameterf64dc582007-10-16 01:25:33 -07002202 for_each_node_state(node, N_NORMAL_MEMORY) {
Christoph Lameter81819f02007-05-06 14:49:36 -07002203 struct kmem_cache_node *n = s->node[node];
2204 if (n && n != &s->local_node)
2205 kmem_cache_free(kmalloc_caches, n);
2206 s->node[node] = NULL;
2207 }
2208}
2209
2210static int init_kmem_cache_nodes(struct kmem_cache *s, gfp_t gfpflags)
2211{
2212 int node;
2213 int local_node;
2214
2215 if (slab_state >= UP)
2216 local_node = page_to_nid(virt_to_page(s));
2217 else
2218 local_node = 0;
2219
Christoph Lameterf64dc582007-10-16 01:25:33 -07002220 for_each_node_state(node, N_NORMAL_MEMORY) {
Christoph Lameter81819f02007-05-06 14:49:36 -07002221 struct kmem_cache_node *n;
2222
2223 if (local_node == node)
2224 n = &s->local_node;
2225 else {
2226 if (slab_state == DOWN) {
David Rientjes0094de92008-11-25 19:14:19 -08002227 early_kmem_cache_node_alloc(gfpflags, node);
Christoph Lameter81819f02007-05-06 14:49:36 -07002228 continue;
2229 }
2230 n = kmem_cache_alloc_node(kmalloc_caches,
2231 gfpflags, node);
2232
2233 if (!n) {
2234 free_kmem_cache_nodes(s);
2235 return 0;
2236 }
2237
2238 }
2239 s->node[node] = n;
Pekka Enberg5595cff2008-08-05 09:28:47 +03002240 init_kmem_cache_node(n, s);
Christoph Lameter81819f02007-05-06 14:49:36 -07002241 }
2242 return 1;
2243}
2244#else
2245static void free_kmem_cache_nodes(struct kmem_cache *s)
2246{
2247}
2248
2249static int init_kmem_cache_nodes(struct kmem_cache *s, gfp_t gfpflags)
2250{
Pekka Enberg5595cff2008-08-05 09:28:47 +03002251 init_kmem_cache_node(&s->local_node, s);
Christoph Lameter81819f02007-05-06 14:49:36 -07002252 return 1;
2253}
2254#endif
2255
David Rientjesc0bdb232009-02-25 09:16:35 +02002256static void set_min_partial(struct kmem_cache *s, unsigned long min)
David Rientjes3b89d7d2009-02-22 17:40:07 -08002257{
2258 if (min < MIN_PARTIAL)
2259 min = MIN_PARTIAL;
2260 else if (min > MAX_PARTIAL)
2261 min = MAX_PARTIAL;
2262 s->min_partial = min;
2263}
2264
Christoph Lameter81819f02007-05-06 14:49:36 -07002265/*
2266 * calculate_sizes() determines the order and the distribution of data within
2267 * a slab object.
2268 */
Christoph Lameter06b285d2008-04-14 19:11:41 +03002269static int calculate_sizes(struct kmem_cache *s, int forced_order)
Christoph Lameter81819f02007-05-06 14:49:36 -07002270{
2271 unsigned long flags = s->flags;
2272 unsigned long size = s->objsize;
2273 unsigned long align = s->align;
Christoph Lameter834f3d12008-04-14 19:11:31 +03002274 int order;
Christoph Lameter81819f02007-05-06 14:49:36 -07002275
2276 /*
Christoph Lameterd8b42bf2008-02-15 23:45:25 -08002277 * Round up object size to the next word boundary. We can only
2278 * place the free pointer at word boundaries and this determines
2279 * the possible location of the free pointer.
2280 */
2281 size = ALIGN(size, sizeof(void *));
2282
2283#ifdef CONFIG_SLUB_DEBUG
2284 /*
Christoph Lameter81819f02007-05-06 14:49:36 -07002285 * Determine if we can poison the object itself. If the user of
2286 * the slab may touch the object after free or before allocation
2287 * then we should never poison the object itself.
2288 */
2289 if ((flags & SLAB_POISON) && !(flags & SLAB_DESTROY_BY_RCU) &&
Christoph Lameterc59def9f2007-05-16 22:10:50 -07002290 !s->ctor)
Christoph Lameter81819f02007-05-06 14:49:36 -07002291 s->flags |= __OBJECT_POISON;
2292 else
2293 s->flags &= ~__OBJECT_POISON;
2294
Christoph Lameter81819f02007-05-06 14:49:36 -07002295
2296 /*
Christoph Lameter672bba32007-05-09 02:32:39 -07002297 * If we are Redzoning then check if there is some space between the
Christoph Lameter81819f02007-05-06 14:49:36 -07002298 * end of the object and the free pointer. If not then add an
Christoph Lameter672bba32007-05-09 02:32:39 -07002299 * additional word to have some bytes to store Redzone information.
Christoph Lameter81819f02007-05-06 14:49:36 -07002300 */
2301 if ((flags & SLAB_RED_ZONE) && size == s->objsize)
2302 size += sizeof(void *);
Christoph Lameter41ecc552007-05-09 02:32:44 -07002303#endif
Christoph Lameter81819f02007-05-06 14:49:36 -07002304
2305 /*
Christoph Lameter672bba32007-05-09 02:32:39 -07002306 * With that we have determined the number of bytes in actual use
2307 * by the object. This is the potential offset to the free pointer.
Christoph Lameter81819f02007-05-06 14:49:36 -07002308 */
2309 s->inuse = size;
2310
2311 if (((flags & (SLAB_DESTROY_BY_RCU | SLAB_POISON)) ||
Christoph Lameterc59def9f2007-05-16 22:10:50 -07002312 s->ctor)) {
Christoph Lameter81819f02007-05-06 14:49:36 -07002313 /*
2314 * Relocate free pointer after the object if it is not
2315 * permitted to overwrite the first word of the object on
2316 * kmem_cache_free.
2317 *
2318 * This is the case if we do RCU, have a constructor or
2319 * destructor or are poisoning the objects.
2320 */
2321 s->offset = size;
2322 size += sizeof(void *);
2323 }
2324
Christoph Lameterc12b3c62007-05-23 13:57:31 -07002325#ifdef CONFIG_SLUB_DEBUG
Christoph Lameter81819f02007-05-06 14:49:36 -07002326 if (flags & SLAB_STORE_USER)
2327 /*
2328 * Need to store information about allocs and frees after
2329 * the object.
2330 */
2331 size += 2 * sizeof(struct track);
2332
Christoph Lameterbe7b3fb2007-05-09 02:32:36 -07002333 if (flags & SLAB_RED_ZONE)
Christoph Lameter81819f02007-05-06 14:49:36 -07002334 /*
2335 * Add some empty padding so that we can catch
2336 * overwrites from earlier objects rather than let
2337 * tracking information or the free pointer be
Frederik Schwarzer0211a9c2008-12-29 22:14:56 +01002338 * corrupted if a user writes before the start
Christoph Lameter81819f02007-05-06 14:49:36 -07002339 * of the object.
2340 */
2341 size += sizeof(void *);
Christoph Lameter41ecc552007-05-09 02:32:44 -07002342#endif
Christoph Lameter672bba32007-05-09 02:32:39 -07002343
Christoph Lameter81819f02007-05-06 14:49:36 -07002344 /*
2345 * Determine the alignment based on various parameters that the
Christoph Lameter65c02d42007-05-09 02:32:35 -07002346 * user specified and the dynamic determination of cache line size
2347 * on bootup.
Christoph Lameter81819f02007-05-06 14:49:36 -07002348 */
2349 align = calculate_alignment(flags, align, s->objsize);
2350
2351 /*
2352 * SLUB stores one object immediately after another beginning from
2353 * offset 0. In order to align the objects we have to simply size
2354 * each object to conform to the alignment.
2355 */
2356 size = ALIGN(size, align);
2357 s->size = size;
Christoph Lameter06b285d2008-04-14 19:11:41 +03002358 if (forced_order >= 0)
2359 order = forced_order;
2360 else
2361 order = calculate_order(size);
Christoph Lameter81819f02007-05-06 14:49:36 -07002362
Christoph Lameter834f3d12008-04-14 19:11:31 +03002363 if (order < 0)
Christoph Lameter81819f02007-05-06 14:49:36 -07002364 return 0;
2365
Christoph Lameterb7a49f02008-02-14 14:21:32 -08002366 s->allocflags = 0;
Christoph Lameter834f3d12008-04-14 19:11:31 +03002367 if (order)
Christoph Lameterb7a49f02008-02-14 14:21:32 -08002368 s->allocflags |= __GFP_COMP;
2369
2370 if (s->flags & SLAB_CACHE_DMA)
2371 s->allocflags |= SLUB_DMA;
2372
2373 if (s->flags & SLAB_RECLAIM_ACCOUNT)
2374 s->allocflags |= __GFP_RECLAIMABLE;
2375
Christoph Lameter81819f02007-05-06 14:49:36 -07002376 /*
2377 * Determine the number of objects per slab
2378 */
Christoph Lameter834f3d12008-04-14 19:11:31 +03002379 s->oo = oo_make(order, size);
Christoph Lameter65c33762008-04-14 19:11:40 +03002380 s->min = oo_make(get_order(size), size);
Christoph Lameter205ab992008-04-14 19:11:40 +03002381 if (oo_objects(s->oo) > oo_objects(s->max))
2382 s->max = s->oo;
Christoph Lameter81819f02007-05-06 14:49:36 -07002383
Christoph Lameter834f3d12008-04-14 19:11:31 +03002384 return !!oo_objects(s->oo);
Christoph Lameter81819f02007-05-06 14:49:36 -07002385
2386}
2387
Christoph Lameter81819f02007-05-06 14:49:36 -07002388static int kmem_cache_open(struct kmem_cache *s, gfp_t gfpflags,
2389 const char *name, size_t size,
2390 size_t align, unsigned long flags,
Alexey Dobriyan51cc5062008-07-25 19:45:34 -07002391 void (*ctor)(void *))
Christoph Lameter81819f02007-05-06 14:49:36 -07002392{
2393 memset(s, 0, kmem_size);
2394 s->name = name;
2395 s->ctor = ctor;
Christoph Lameter81819f02007-05-06 14:49:36 -07002396 s->objsize = size;
Christoph Lameter81819f02007-05-06 14:49:36 -07002397 s->align = align;
Christoph Lameterba0268a2007-09-11 15:24:11 -07002398 s->flags = kmem_cache_flags(size, flags, name, ctor);
Christoph Lameter81819f02007-05-06 14:49:36 -07002399
Christoph Lameter06b285d2008-04-14 19:11:41 +03002400 if (!calculate_sizes(s, -1))
Christoph Lameter81819f02007-05-06 14:49:36 -07002401 goto error;
2402
David Rientjes3b89d7d2009-02-22 17:40:07 -08002403 /*
2404 * The larger the object size is, the more pages we want on the partial
2405 * list to avoid pounding the page allocator excessively.
2406 */
David Rientjesc0bdb232009-02-25 09:16:35 +02002407 set_min_partial(s, ilog2(s->size));
Christoph Lameter81819f02007-05-06 14:49:36 -07002408 s->refcount = 1;
2409#ifdef CONFIG_NUMA
Christoph Lametere2cb96b2008-08-19 08:51:22 -05002410 s->remote_node_defrag_ratio = 1000;
Christoph Lameter81819f02007-05-06 14:49:36 -07002411#endif
Christoph Lameterdfb4f092007-10-16 01:26:05 -07002412 if (!init_kmem_cache_nodes(s, gfpflags & ~SLUB_DMA))
2413 goto error;
Christoph Lameter81819f02007-05-06 14:49:36 -07002414
Christoph Lameterdfb4f092007-10-16 01:26:05 -07002415 if (alloc_kmem_cache_cpus(s, gfpflags & ~SLUB_DMA))
Christoph Lameter81819f02007-05-06 14:49:36 -07002416 return 1;
Christoph Lameter4c93c3552007-10-16 01:26:08 -07002417 free_kmem_cache_nodes(s);
Christoph Lameter81819f02007-05-06 14:49:36 -07002418error:
2419 if (flags & SLAB_PANIC)
2420 panic("Cannot create slab %s size=%lu realsize=%u "
2421 "order=%u offset=%u flags=%lx\n",
Christoph Lameter834f3d12008-04-14 19:11:31 +03002422 s->name, (unsigned long)size, s->size, oo_order(s->oo),
Christoph Lameter81819f02007-05-06 14:49:36 -07002423 s->offset, flags);
2424 return 0;
2425}
Christoph Lameter81819f02007-05-06 14:49:36 -07002426
2427/*
2428 * Check if a given pointer is valid
2429 */
2430int kmem_ptr_validate(struct kmem_cache *s, const void *object)
2431{
Pekka Enberg06428782008-01-07 23:20:27 -08002432 struct page *page;
Christoph Lameter81819f02007-05-06 14:49:36 -07002433
2434 page = get_object_page(object);
2435
2436 if (!page || s != page->slab)
2437 /* No slab or wrong slab */
2438 return 0;
2439
Christoph Lameterabcd08a2007-05-09 02:32:37 -07002440 if (!check_valid_pointer(s, page, object))
Christoph Lameter81819f02007-05-06 14:49:36 -07002441 return 0;
2442
2443 /*
2444 * We could also check if the object is on the slabs freelist.
2445 * But this would be too expensive and it seems that the main
Christoph Lameter6446faa2008-02-15 23:45:26 -08002446 * purpose of kmem_ptr_valid() is to check if the object belongs
Christoph Lameter81819f02007-05-06 14:49:36 -07002447 * to a certain slab.
2448 */
2449 return 1;
2450}
2451EXPORT_SYMBOL(kmem_ptr_validate);
2452
2453/*
2454 * Determine the size of a slab object
2455 */
2456unsigned int kmem_cache_size(struct kmem_cache *s)
2457{
2458 return s->objsize;
2459}
2460EXPORT_SYMBOL(kmem_cache_size);
2461
2462const char *kmem_cache_name(struct kmem_cache *s)
2463{
2464 return s->name;
2465}
2466EXPORT_SYMBOL(kmem_cache_name);
2467
Christoph Lameter33b12c32008-04-25 12:22:43 -07002468static void list_slab_objects(struct kmem_cache *s, struct page *page,
2469 const char *text)
Christoph Lameter81819f02007-05-06 14:49:36 -07002470{
Christoph Lameter33b12c32008-04-25 12:22:43 -07002471#ifdef CONFIG_SLUB_DEBUG
2472 void *addr = page_address(page);
2473 void *p;
2474 DECLARE_BITMAP(map, page->objects);
2475
2476 bitmap_zero(map, page->objects);
2477 slab_err(s, page, "%s", text);
2478 slab_lock(page);
2479 for_each_free_object(p, s, page->freelist)
2480 set_bit(slab_index(p, s, addr), map);
2481
2482 for_each_object(p, s, addr, page->objects) {
2483
2484 if (!test_bit(slab_index(p, s, addr), map)) {
2485 printk(KERN_ERR "INFO: Object 0x%p @offset=%tu\n",
2486 p, p - addr);
2487 print_tracking(s, p);
2488 }
2489 }
2490 slab_unlock(page);
2491#endif
2492}
2493
Christoph Lameter81819f02007-05-06 14:49:36 -07002494/*
Christoph Lameter599870b2008-04-23 12:36:52 -07002495 * Attempt to free all partial slabs on a node.
Christoph Lameter81819f02007-05-06 14:49:36 -07002496 */
Christoph Lameter599870b2008-04-23 12:36:52 -07002497static void free_partial(struct kmem_cache *s, struct kmem_cache_node *n)
Christoph Lameter81819f02007-05-06 14:49:36 -07002498{
Christoph Lameter81819f02007-05-06 14:49:36 -07002499 unsigned long flags;
2500 struct page *page, *h;
2501
2502 spin_lock_irqsave(&n->list_lock, flags);
Christoph Lameter33b12c32008-04-25 12:22:43 -07002503 list_for_each_entry_safe(page, h, &n->partial, lru) {
Christoph Lameter81819f02007-05-06 14:49:36 -07002504 if (!page->inuse) {
2505 list_del(&page->lru);
2506 discard_slab(s, page);
Christoph Lameter599870b2008-04-23 12:36:52 -07002507 n->nr_partial--;
Christoph Lameter33b12c32008-04-25 12:22:43 -07002508 } else {
2509 list_slab_objects(s, page,
2510 "Objects remaining on kmem_cache_close()");
Christoph Lameter599870b2008-04-23 12:36:52 -07002511 }
Christoph Lameter33b12c32008-04-25 12:22:43 -07002512 }
Christoph Lameter81819f02007-05-06 14:49:36 -07002513 spin_unlock_irqrestore(&n->list_lock, flags);
Christoph Lameter81819f02007-05-06 14:49:36 -07002514}
2515
2516/*
Christoph Lameter672bba32007-05-09 02:32:39 -07002517 * Release all resources used by a slab cache.
Christoph Lameter81819f02007-05-06 14:49:36 -07002518 */
Christoph Lameter0c710012007-07-17 04:03:24 -07002519static inline int kmem_cache_close(struct kmem_cache *s)
Christoph Lameter81819f02007-05-06 14:49:36 -07002520{
2521 int node;
2522
2523 flush_all(s);
2524
2525 /* Attempt to free all objects */
Christoph Lameter4c93c3552007-10-16 01:26:08 -07002526 free_kmem_cache_cpus(s);
Christoph Lameterf64dc582007-10-16 01:25:33 -07002527 for_each_node_state(node, N_NORMAL_MEMORY) {
Christoph Lameter81819f02007-05-06 14:49:36 -07002528 struct kmem_cache_node *n = get_node(s, node);
2529
Christoph Lameter599870b2008-04-23 12:36:52 -07002530 free_partial(s, n);
2531 if (n->nr_partial || slabs_node(s, node))
Christoph Lameter81819f02007-05-06 14:49:36 -07002532 return 1;
2533 }
2534 free_kmem_cache_nodes(s);
2535 return 0;
2536}
2537
2538/*
2539 * Close a cache and release the kmem_cache structure
2540 * (must be used for caches created using kmem_cache_create)
2541 */
2542void kmem_cache_destroy(struct kmem_cache *s)
2543{
2544 down_write(&slub_lock);
2545 s->refcount--;
2546 if (!s->refcount) {
2547 list_del(&s->list);
Christoph Lametera0e1d1b2007-07-17 04:03:31 -07002548 up_write(&slub_lock);
Pekka Enbergd629d812008-04-23 22:31:08 +03002549 if (kmem_cache_close(s)) {
2550 printk(KERN_ERR "SLUB %s: %s called for cache that "
2551 "still has objects.\n", s->name, __func__);
2552 dump_stack();
2553 }
Christoph Lameter81819f02007-05-06 14:49:36 -07002554 sysfs_slab_remove(s);
Christoph Lametera0e1d1b2007-07-17 04:03:31 -07002555 } else
2556 up_write(&slub_lock);
Christoph Lameter81819f02007-05-06 14:49:36 -07002557}
2558EXPORT_SYMBOL(kmem_cache_destroy);
2559
2560/********************************************************************
2561 * Kmalloc subsystem
2562 *******************************************************************/
2563
Christoph Lameterffadd4d2009-02-17 12:05:07 -05002564struct kmem_cache kmalloc_caches[SLUB_PAGE_SHIFT] __cacheline_aligned;
Christoph Lameter81819f02007-05-06 14:49:36 -07002565EXPORT_SYMBOL(kmalloc_caches);
2566
Christoph Lameter81819f02007-05-06 14:49:36 -07002567static int __init setup_slub_min_order(char *str)
2568{
Pekka Enberg06428782008-01-07 23:20:27 -08002569 get_option(&str, &slub_min_order);
Christoph Lameter81819f02007-05-06 14:49:36 -07002570
2571 return 1;
2572}
2573
2574__setup("slub_min_order=", setup_slub_min_order);
2575
2576static int __init setup_slub_max_order(char *str)
2577{
Pekka Enberg06428782008-01-07 23:20:27 -08002578 get_option(&str, &slub_max_order);
David Rientjes818cf592009-04-23 09:58:22 +03002579 slub_max_order = min(slub_max_order, MAX_ORDER - 1);
Christoph Lameter81819f02007-05-06 14:49:36 -07002580
2581 return 1;
2582}
2583
2584__setup("slub_max_order=", setup_slub_max_order);
2585
2586static int __init setup_slub_min_objects(char *str)
2587{
Pekka Enberg06428782008-01-07 23:20:27 -08002588 get_option(&str, &slub_min_objects);
Christoph Lameter81819f02007-05-06 14:49:36 -07002589
2590 return 1;
2591}
2592
2593__setup("slub_min_objects=", setup_slub_min_objects);
2594
2595static int __init setup_slub_nomerge(char *str)
2596{
2597 slub_nomerge = 1;
2598 return 1;
2599}
2600
2601__setup("slub_nomerge", setup_slub_nomerge);
2602
Christoph Lameter81819f02007-05-06 14:49:36 -07002603static struct kmem_cache *create_kmalloc_cache(struct kmem_cache *s,
2604 const char *name, int size, gfp_t gfp_flags)
2605{
2606 unsigned int flags = 0;
2607
2608 if (gfp_flags & SLUB_DMA)
2609 flags = SLAB_CACHE_DMA;
2610
2611 down_write(&slub_lock);
2612 if (!kmem_cache_open(s, gfp_flags, name, size, ARCH_KMALLOC_MINALIGN,
Christoph Lameter319d1e22008-04-14 19:11:41 +03002613 flags, NULL))
Christoph Lameter81819f02007-05-06 14:49:36 -07002614 goto panic;
2615
2616 list_add(&s->list, &slab_caches);
2617 up_write(&slub_lock);
2618 if (sysfs_slab_add(s))
2619 goto panic;
2620 return s;
2621
2622panic:
2623 panic("Creation of kmalloc slab %s size=%d failed.\n", name, size);
2624}
2625
Christoph Lameter2e443fd2007-07-17 04:03:24 -07002626#ifdef CONFIG_ZONE_DMA
Christoph Lameterffadd4d2009-02-17 12:05:07 -05002627static struct kmem_cache *kmalloc_caches_dma[SLUB_PAGE_SHIFT];
Christoph Lameter1ceef402007-08-07 15:11:48 -07002628
2629static void sysfs_add_func(struct work_struct *w)
2630{
2631 struct kmem_cache *s;
2632
2633 down_write(&slub_lock);
2634 list_for_each_entry(s, &slab_caches, list) {
2635 if (s->flags & __SYSFS_ADD_DEFERRED) {
2636 s->flags &= ~__SYSFS_ADD_DEFERRED;
2637 sysfs_slab_add(s);
2638 }
2639 }
2640 up_write(&slub_lock);
2641}
2642
2643static DECLARE_WORK(sysfs_add_work, sysfs_add_func);
2644
Christoph Lameter2e443fd2007-07-17 04:03:24 -07002645static noinline struct kmem_cache *dma_kmalloc_cache(int index, gfp_t flags)
2646{
2647 struct kmem_cache *s;
Christoph Lameter2e443fd2007-07-17 04:03:24 -07002648 char *text;
2649 size_t realsize;
2650
2651 s = kmalloc_caches_dma[index];
2652 if (s)
2653 return s;
2654
2655 /* Dynamically create dma cache */
Christoph Lameter1ceef402007-08-07 15:11:48 -07002656 if (flags & __GFP_WAIT)
2657 down_write(&slub_lock);
2658 else {
2659 if (!down_write_trylock(&slub_lock))
2660 goto out;
2661 }
2662
2663 if (kmalloc_caches_dma[index])
2664 goto unlock_out;
Christoph Lameter2e443fd2007-07-17 04:03:24 -07002665
Christoph Lameter7b55f622007-07-17 04:03:27 -07002666 realsize = kmalloc_caches[index].objsize;
Ingo Molnar3adbefe2008-02-05 17:57:39 -08002667 text = kasprintf(flags & ~SLUB_DMA, "kmalloc_dma-%d",
2668 (unsigned int)realsize);
Christoph Lameter1ceef402007-08-07 15:11:48 -07002669 s = kmalloc(kmem_size, flags & ~SLUB_DMA);
2670
2671 if (!s || !text || !kmem_cache_open(s, flags, text,
2672 realsize, ARCH_KMALLOC_MINALIGN,
2673 SLAB_CACHE_DMA|__SYSFS_ADD_DEFERRED, NULL)) {
2674 kfree(s);
2675 kfree(text);
2676 goto unlock_out;
Christoph Lameterdfce8642007-07-17 04:03:25 -07002677 }
Christoph Lameter1ceef402007-08-07 15:11:48 -07002678
2679 list_add(&s->list, &slab_caches);
2680 kmalloc_caches_dma[index] = s;
2681
2682 schedule_work(&sysfs_add_work);
2683
2684unlock_out:
Christoph Lameterdfce8642007-07-17 04:03:25 -07002685 up_write(&slub_lock);
Christoph Lameter1ceef402007-08-07 15:11:48 -07002686out:
Christoph Lameterdfce8642007-07-17 04:03:25 -07002687 return kmalloc_caches_dma[index];
Christoph Lameter2e443fd2007-07-17 04:03:24 -07002688}
2689#endif
2690
Christoph Lameterf1b26332007-07-17 04:03:26 -07002691/*
2692 * Conversion table for small slabs sizes / 8 to the index in the
2693 * kmalloc array. This is necessary for slabs < 192 since we have non power
2694 * of two cache sizes there. The size of larger slabs can be determined using
2695 * fls.
2696 */
2697static s8 size_index[24] = {
2698 3, /* 8 */
2699 4, /* 16 */
2700 5, /* 24 */
2701 5, /* 32 */
2702 6, /* 40 */
2703 6, /* 48 */
2704 6, /* 56 */
2705 6, /* 64 */
2706 1, /* 72 */
2707 1, /* 80 */
2708 1, /* 88 */
2709 1, /* 96 */
2710 7, /* 104 */
2711 7, /* 112 */
2712 7, /* 120 */
2713 7, /* 128 */
2714 2, /* 136 */
2715 2, /* 144 */
2716 2, /* 152 */
2717 2, /* 160 */
2718 2, /* 168 */
2719 2, /* 176 */
2720 2, /* 184 */
2721 2 /* 192 */
2722};
2723
Christoph Lameter81819f02007-05-06 14:49:36 -07002724static struct kmem_cache *get_slab(size_t size, gfp_t flags)
2725{
Christoph Lameterf1b26332007-07-17 04:03:26 -07002726 int index;
Christoph Lameter81819f02007-05-06 14:49:36 -07002727
Christoph Lameterf1b26332007-07-17 04:03:26 -07002728 if (size <= 192) {
2729 if (!size)
2730 return ZERO_SIZE_PTR;
Christoph Lameter81819f02007-05-06 14:49:36 -07002731
Christoph Lameterf1b26332007-07-17 04:03:26 -07002732 index = size_index[(size - 1) / 8];
Christoph Lameteraadb4bc2007-10-16 01:24:38 -07002733 } else
Christoph Lameterf1b26332007-07-17 04:03:26 -07002734 index = fls(size - 1);
Christoph Lameter81819f02007-05-06 14:49:36 -07002735
2736#ifdef CONFIG_ZONE_DMA
Christoph Lameterf1b26332007-07-17 04:03:26 -07002737 if (unlikely((flags & SLUB_DMA)))
Christoph Lameter2e443fd2007-07-17 04:03:24 -07002738 return dma_kmalloc_cache(index, flags);
Christoph Lameterf1b26332007-07-17 04:03:26 -07002739
Christoph Lameter81819f02007-05-06 14:49:36 -07002740#endif
2741 return &kmalloc_caches[index];
2742}
2743
2744void *__kmalloc(size_t size, gfp_t flags)
2745{
Christoph Lameteraadb4bc2007-10-16 01:24:38 -07002746 struct kmem_cache *s;
Eduard - Gabriel Munteanu5b882be2008-08-19 20:43:26 +03002747 void *ret;
Christoph Lameter81819f02007-05-06 14:49:36 -07002748
Christoph Lameterffadd4d2009-02-17 12:05:07 -05002749 if (unlikely(size > SLUB_MAX_SIZE))
Pekka Enbergeada35e2008-02-11 22:47:46 +02002750 return kmalloc_large(size, flags);
Christoph Lameteraadb4bc2007-10-16 01:24:38 -07002751
2752 s = get_slab(size, flags);
2753
2754 if (unlikely(ZERO_OR_NULL_PTR(s)))
Christoph Lameter6cb8f912007-07-17 04:03:22 -07002755 return s;
2756
Eduard - Gabriel Munteanu5b882be2008-08-19 20:43:26 +03002757 ret = slab_alloc(s, flags, -1, _RET_IP_);
2758
Eduard - Gabriel Munteanuca2b84c2009-03-23 15:12:24 +02002759 trace_kmalloc(_RET_IP_, ret, size, s->size, flags);
Eduard - Gabriel Munteanu5b882be2008-08-19 20:43:26 +03002760
2761 return ret;
Christoph Lameter81819f02007-05-06 14:49:36 -07002762}
2763EXPORT_SYMBOL(__kmalloc);
2764
Christoph Lameterf619cfe2008-03-01 13:56:40 -08002765static void *kmalloc_large_node(size_t size, gfp_t flags, int node)
2766{
2767 struct page *page = alloc_pages_node(node, flags | __GFP_COMP,
2768 get_order(size));
2769
2770 if (page)
2771 return page_address(page);
2772 else
2773 return NULL;
2774}
2775
Christoph Lameter81819f02007-05-06 14:49:36 -07002776#ifdef CONFIG_NUMA
2777void *__kmalloc_node(size_t size, gfp_t flags, int node)
2778{
Christoph Lameteraadb4bc2007-10-16 01:24:38 -07002779 struct kmem_cache *s;
Eduard - Gabriel Munteanu5b882be2008-08-19 20:43:26 +03002780 void *ret;
Christoph Lameter81819f02007-05-06 14:49:36 -07002781
Ingo Molnar057685c2009-02-20 12:15:30 +01002782 if (unlikely(size > SLUB_MAX_SIZE)) {
Eduard - Gabriel Munteanu5b882be2008-08-19 20:43:26 +03002783 ret = kmalloc_large_node(size, flags, node);
2784
Eduard - Gabriel Munteanuca2b84c2009-03-23 15:12:24 +02002785 trace_kmalloc_node(_RET_IP_, ret,
2786 size, PAGE_SIZE << get_order(size),
2787 flags, node);
Eduard - Gabriel Munteanu5b882be2008-08-19 20:43:26 +03002788
2789 return ret;
2790 }
Christoph Lameteraadb4bc2007-10-16 01:24:38 -07002791
2792 s = get_slab(size, flags);
2793
2794 if (unlikely(ZERO_OR_NULL_PTR(s)))
Christoph Lameter6cb8f912007-07-17 04:03:22 -07002795 return s;
2796
Eduard - Gabriel Munteanu5b882be2008-08-19 20:43:26 +03002797 ret = slab_alloc(s, flags, node, _RET_IP_);
2798
Eduard - Gabriel Munteanuca2b84c2009-03-23 15:12:24 +02002799 trace_kmalloc_node(_RET_IP_, ret, size, s->size, flags, node);
Eduard - Gabriel Munteanu5b882be2008-08-19 20:43:26 +03002800
2801 return ret;
Christoph Lameter81819f02007-05-06 14:49:36 -07002802}
2803EXPORT_SYMBOL(__kmalloc_node);
2804#endif
2805
2806size_t ksize(const void *object)
2807{
Christoph Lameter272c1d22007-06-08 13:46:49 -07002808 struct page *page;
Christoph Lameter81819f02007-05-06 14:49:36 -07002809 struct kmem_cache *s;
2810
Christoph Lameteref8b4522007-10-16 01:24:46 -07002811 if (unlikely(object == ZERO_SIZE_PTR))
Christoph Lameter272c1d22007-06-08 13:46:49 -07002812 return 0;
2813
Vegard Nossum294a80a2007-12-04 23:45:30 -08002814 page = virt_to_head_page(object);
Vegard Nossum294a80a2007-12-04 23:45:30 -08002815
Pekka Enberg76994412008-05-22 19:22:25 +03002816 if (unlikely(!PageSlab(page))) {
2817 WARN_ON(!PageCompound(page));
Vegard Nossum294a80a2007-12-04 23:45:30 -08002818 return PAGE_SIZE << compound_order(page);
Pekka Enberg76994412008-05-22 19:22:25 +03002819 }
Christoph Lameter81819f02007-05-06 14:49:36 -07002820 s = page->slab;
Christoph Lameter81819f02007-05-06 14:49:36 -07002821
Christoph Lameterae20bfd2008-02-15 23:45:25 -08002822#ifdef CONFIG_SLUB_DEBUG
Christoph Lameter81819f02007-05-06 14:49:36 -07002823 /*
2824 * Debugging requires use of the padding between object
2825 * and whatever may come after it.
2826 */
2827 if (s->flags & (SLAB_RED_ZONE | SLAB_POISON))
2828 return s->objsize;
2829
Christoph Lameterae20bfd2008-02-15 23:45:25 -08002830#endif
Christoph Lameter81819f02007-05-06 14:49:36 -07002831 /*
2832 * If we have the need to store the freelist pointer
2833 * back there or track user information then we can
2834 * only use the space before that information.
2835 */
2836 if (s->flags & (SLAB_DESTROY_BY_RCU | SLAB_STORE_USER))
2837 return s->inuse;
Christoph Lameter81819f02007-05-06 14:49:36 -07002838 /*
2839 * Else we can use all the padding etc for the allocation
2840 */
2841 return s->size;
2842}
Kirill A. Shutemovb1aabec2009-02-10 15:21:44 +02002843EXPORT_SYMBOL(ksize);
Christoph Lameter81819f02007-05-06 14:49:36 -07002844
2845void kfree(const void *x)
2846{
Christoph Lameter81819f02007-05-06 14:49:36 -07002847 struct page *page;
Christoph Lameter5bb983b2008-02-07 17:47:41 -08002848 void *object = (void *)x;
Christoph Lameter81819f02007-05-06 14:49:36 -07002849
Pekka Enberg2121db72009-03-25 11:05:57 +02002850 trace_kfree(_RET_IP_, x);
2851
Satyam Sharma2408c552007-10-16 01:24:44 -07002852 if (unlikely(ZERO_OR_NULL_PTR(x)))
Christoph Lameter81819f02007-05-06 14:49:36 -07002853 return;
2854
Christoph Lameterb49af682007-05-06 14:49:41 -07002855 page = virt_to_head_page(x);
Christoph Lameteraadb4bc2007-10-16 01:24:38 -07002856 if (unlikely(!PageSlab(page))) {
Christoph Lameter09375022008-05-28 10:32:22 -07002857 BUG_ON(!PageCompound(page));
Christoph Lameteraadb4bc2007-10-16 01:24:38 -07002858 put_page(page);
2859 return;
2860 }
Eduard - Gabriel Munteanuce71e272008-08-19 20:43:25 +03002861 slab_free(page->slab, page, object, _RET_IP_);
Christoph Lameter81819f02007-05-06 14:49:36 -07002862}
2863EXPORT_SYMBOL(kfree);
2864
Christoph Lameter2086d262007-05-06 14:49:46 -07002865/*
Christoph Lameter672bba32007-05-09 02:32:39 -07002866 * kmem_cache_shrink removes empty slabs from the partial lists and sorts
2867 * the remaining slabs by the number of items in use. The slabs with the
2868 * most items in use come first. New allocations will then fill those up
2869 * and thus they can be removed from the partial lists.
2870 *
2871 * The slabs with the least items are placed last. This results in them
2872 * being allocated from last increasing the chance that the last objects
2873 * are freed in them.
Christoph Lameter2086d262007-05-06 14:49:46 -07002874 */
2875int kmem_cache_shrink(struct kmem_cache *s)
2876{
2877 int node;
2878 int i;
2879 struct kmem_cache_node *n;
2880 struct page *page;
2881 struct page *t;
Christoph Lameter205ab992008-04-14 19:11:40 +03002882 int objects = oo_objects(s->max);
Christoph Lameter2086d262007-05-06 14:49:46 -07002883 struct list_head *slabs_by_inuse =
Christoph Lameter834f3d12008-04-14 19:11:31 +03002884 kmalloc(sizeof(struct list_head) * objects, GFP_KERNEL);
Christoph Lameter2086d262007-05-06 14:49:46 -07002885 unsigned long flags;
2886
2887 if (!slabs_by_inuse)
2888 return -ENOMEM;
2889
2890 flush_all(s);
Christoph Lameterf64dc582007-10-16 01:25:33 -07002891 for_each_node_state(node, N_NORMAL_MEMORY) {
Christoph Lameter2086d262007-05-06 14:49:46 -07002892 n = get_node(s, node);
2893
2894 if (!n->nr_partial)
2895 continue;
2896
Christoph Lameter834f3d12008-04-14 19:11:31 +03002897 for (i = 0; i < objects; i++)
Christoph Lameter2086d262007-05-06 14:49:46 -07002898 INIT_LIST_HEAD(slabs_by_inuse + i);
2899
2900 spin_lock_irqsave(&n->list_lock, flags);
2901
2902 /*
Christoph Lameter672bba32007-05-09 02:32:39 -07002903 * Build lists indexed by the items in use in each slab.
Christoph Lameter2086d262007-05-06 14:49:46 -07002904 *
Christoph Lameter672bba32007-05-09 02:32:39 -07002905 * Note that concurrent frees may occur while we hold the
2906 * list_lock. page->inuse here is the upper limit.
Christoph Lameter2086d262007-05-06 14:49:46 -07002907 */
2908 list_for_each_entry_safe(page, t, &n->partial, lru) {
2909 if (!page->inuse && slab_trylock(page)) {
2910 /*
2911 * Must hold slab lock here because slab_free
2912 * may have freed the last object and be
2913 * waiting to release the slab.
2914 */
2915 list_del(&page->lru);
2916 n->nr_partial--;
2917 slab_unlock(page);
2918 discard_slab(s, page);
2919 } else {
Christoph Lameterfcda3d82007-07-30 13:06:46 -07002920 list_move(&page->lru,
2921 slabs_by_inuse + page->inuse);
Christoph Lameter2086d262007-05-06 14:49:46 -07002922 }
2923 }
2924
Christoph Lameter2086d262007-05-06 14:49:46 -07002925 /*
Christoph Lameter672bba32007-05-09 02:32:39 -07002926 * Rebuild the partial list with the slabs filled up most
2927 * first and the least used slabs at the end.
Christoph Lameter2086d262007-05-06 14:49:46 -07002928 */
Christoph Lameter834f3d12008-04-14 19:11:31 +03002929 for (i = objects - 1; i >= 0; i--)
Christoph Lameter2086d262007-05-06 14:49:46 -07002930 list_splice(slabs_by_inuse + i, n->partial.prev);
2931
Christoph Lameter2086d262007-05-06 14:49:46 -07002932 spin_unlock_irqrestore(&n->list_lock, flags);
2933 }
2934
2935 kfree(slabs_by_inuse);
2936 return 0;
2937}
2938EXPORT_SYMBOL(kmem_cache_shrink);
2939
Yasunori Gotob9049e22007-10-21 16:41:37 -07002940#if defined(CONFIG_NUMA) && defined(CONFIG_MEMORY_HOTPLUG)
2941static int slab_mem_going_offline_callback(void *arg)
2942{
2943 struct kmem_cache *s;
2944
2945 down_read(&slub_lock);
2946 list_for_each_entry(s, &slab_caches, list)
2947 kmem_cache_shrink(s);
2948 up_read(&slub_lock);
2949
2950 return 0;
2951}
2952
2953static void slab_mem_offline_callback(void *arg)
2954{
2955 struct kmem_cache_node *n;
2956 struct kmem_cache *s;
2957 struct memory_notify *marg = arg;
2958 int offline_node;
2959
2960 offline_node = marg->status_change_nid;
2961
2962 /*
2963 * If the node still has available memory. we need kmem_cache_node
2964 * for it yet.
2965 */
2966 if (offline_node < 0)
2967 return;
2968
2969 down_read(&slub_lock);
2970 list_for_each_entry(s, &slab_caches, list) {
2971 n = get_node(s, offline_node);
2972 if (n) {
2973 /*
2974 * if n->nr_slabs > 0, slabs still exist on the node
2975 * that is going down. We were unable to free them,
2976 * and offline_pages() function shoudn't call this
2977 * callback. So, we must fail.
2978 */
Christoph Lameter0f389ec2008-04-14 18:53:02 +03002979 BUG_ON(slabs_node(s, offline_node));
Yasunori Gotob9049e22007-10-21 16:41:37 -07002980
2981 s->node[offline_node] = NULL;
2982 kmem_cache_free(kmalloc_caches, n);
2983 }
2984 }
2985 up_read(&slub_lock);
2986}
2987
2988static int slab_mem_going_online_callback(void *arg)
2989{
2990 struct kmem_cache_node *n;
2991 struct kmem_cache *s;
2992 struct memory_notify *marg = arg;
2993 int nid = marg->status_change_nid;
2994 int ret = 0;
2995
2996 /*
2997 * If the node's memory is already available, then kmem_cache_node is
2998 * already created. Nothing to do.
2999 */
3000 if (nid < 0)
3001 return 0;
3002
3003 /*
Christoph Lameter0121c6192008-04-29 16:11:12 -07003004 * We are bringing a node online. No memory is available yet. We must
Yasunori Gotob9049e22007-10-21 16:41:37 -07003005 * allocate a kmem_cache_node structure in order to bring the node
3006 * online.
3007 */
3008 down_read(&slub_lock);
3009 list_for_each_entry(s, &slab_caches, list) {
3010 /*
3011 * XXX: kmem_cache_alloc_node will fallback to other nodes
3012 * since memory is not yet available from the node that
3013 * is brought up.
3014 */
3015 n = kmem_cache_alloc(kmalloc_caches, GFP_KERNEL);
3016 if (!n) {
3017 ret = -ENOMEM;
3018 goto out;
3019 }
Pekka Enberg5595cff2008-08-05 09:28:47 +03003020 init_kmem_cache_node(n, s);
Yasunori Gotob9049e22007-10-21 16:41:37 -07003021 s->node[nid] = n;
3022 }
3023out:
3024 up_read(&slub_lock);
3025 return ret;
3026}
3027
3028static int slab_memory_callback(struct notifier_block *self,
3029 unsigned long action, void *arg)
3030{
3031 int ret = 0;
3032
3033 switch (action) {
3034 case MEM_GOING_ONLINE:
3035 ret = slab_mem_going_online_callback(arg);
3036 break;
3037 case MEM_GOING_OFFLINE:
3038 ret = slab_mem_going_offline_callback(arg);
3039 break;
3040 case MEM_OFFLINE:
3041 case MEM_CANCEL_ONLINE:
3042 slab_mem_offline_callback(arg);
3043 break;
3044 case MEM_ONLINE:
3045 case MEM_CANCEL_OFFLINE:
3046 break;
3047 }
KAMEZAWA Hiroyukidc19f9d2008-12-01 13:13:48 -08003048 if (ret)
3049 ret = notifier_from_errno(ret);
3050 else
3051 ret = NOTIFY_OK;
Yasunori Gotob9049e22007-10-21 16:41:37 -07003052 return ret;
3053}
3054
3055#endif /* CONFIG_MEMORY_HOTPLUG */
3056
Christoph Lameter81819f02007-05-06 14:49:36 -07003057/********************************************************************
3058 * Basic setup of slabs
3059 *******************************************************************/
3060
3061void __init kmem_cache_init(void)
3062{
3063 int i;
Christoph Lameter4b356be2007-06-16 10:16:13 -07003064 int caches = 0;
Christoph Lameter81819f02007-05-06 14:49:36 -07003065
Christoph Lameter4c93c3552007-10-16 01:26:08 -07003066 init_alloc_cpu();
3067
Christoph Lameter81819f02007-05-06 14:49:36 -07003068#ifdef CONFIG_NUMA
3069 /*
3070 * Must first have the slab cache available for the allocations of the
Christoph Lameter672bba32007-05-09 02:32:39 -07003071 * struct kmem_cache_node's. There is special bootstrap code in
Christoph Lameter81819f02007-05-06 14:49:36 -07003072 * kmem_cache_open for slab_state == DOWN.
3073 */
3074 create_kmalloc_cache(&kmalloc_caches[0], "kmem_cache_node",
3075 sizeof(struct kmem_cache_node), GFP_KERNEL);
Christoph Lameter8ffa6872007-05-31 00:40:51 -07003076 kmalloc_caches[0].refcount = -1;
Christoph Lameter4b356be2007-06-16 10:16:13 -07003077 caches++;
Yasunori Gotob9049e22007-10-21 16:41:37 -07003078
Nadia Derbey0c40ba42008-04-29 01:00:41 -07003079 hotplug_memory_notifier(slab_memory_callback, SLAB_CALLBACK_PRI);
Christoph Lameter81819f02007-05-06 14:49:36 -07003080#endif
3081
3082 /* Able to allocate the per node structures */
3083 slab_state = PARTIAL;
3084
3085 /* Caches that are not of the two-to-the-power-of size */
Christoph Lameter4b356be2007-06-16 10:16:13 -07003086 if (KMALLOC_MIN_SIZE <= 64) {
3087 create_kmalloc_cache(&kmalloc_caches[1],
Christoph Lameter81819f02007-05-06 14:49:36 -07003088 "kmalloc-96", 96, GFP_KERNEL);
Christoph Lameter4b356be2007-06-16 10:16:13 -07003089 caches++;
Christoph Lameter4b356be2007-06-16 10:16:13 -07003090 create_kmalloc_cache(&kmalloc_caches[2],
Christoph Lameter81819f02007-05-06 14:49:36 -07003091 "kmalloc-192", 192, GFP_KERNEL);
Christoph Lameter4b356be2007-06-16 10:16:13 -07003092 caches++;
3093 }
Christoph Lameter81819f02007-05-06 14:49:36 -07003094
Christoph Lameterffadd4d2009-02-17 12:05:07 -05003095 for (i = KMALLOC_SHIFT_LOW; i < SLUB_PAGE_SHIFT; i++) {
Christoph Lameter81819f02007-05-06 14:49:36 -07003096 create_kmalloc_cache(&kmalloc_caches[i],
3097 "kmalloc", 1 << i, GFP_KERNEL);
Christoph Lameter4b356be2007-06-16 10:16:13 -07003098 caches++;
3099 }
Christoph Lameter81819f02007-05-06 14:49:36 -07003100
Christoph Lameterf1b26332007-07-17 04:03:26 -07003101
3102 /*
3103 * Patch up the size_index table if we have strange large alignment
3104 * requirements for the kmalloc array. This is only the case for
Christoph Lameter6446faa2008-02-15 23:45:26 -08003105 * MIPS it seems. The standard arches will not generate any code here.
Christoph Lameterf1b26332007-07-17 04:03:26 -07003106 *
3107 * Largest permitted alignment is 256 bytes due to the way we
3108 * handle the index determination for the smaller caches.
3109 *
3110 * Make sure that nothing crazy happens if someone starts tinkering
3111 * around with ARCH_KMALLOC_MINALIGN
3112 */
3113 BUILD_BUG_ON(KMALLOC_MIN_SIZE > 256 ||
3114 (KMALLOC_MIN_SIZE & (KMALLOC_MIN_SIZE - 1)));
3115
Christoph Lameter12ad6842007-07-17 04:03:28 -07003116 for (i = 8; i < KMALLOC_MIN_SIZE; i += 8)
Christoph Lameterf1b26332007-07-17 04:03:26 -07003117 size_index[(i - 1) / 8] = KMALLOC_SHIFT_LOW;
3118
Christoph Lameter41d54d32008-07-03 09:14:26 -05003119 if (KMALLOC_MIN_SIZE == 128) {
3120 /*
3121 * The 192 byte sized cache is not used if the alignment
3122 * is 128 byte. Redirect kmalloc to use the 256 byte cache
3123 * instead.
3124 */
3125 for (i = 128 + 8; i <= 192; i += 8)
3126 size_index[(i - 1) / 8] = 8;
3127 }
3128
Christoph Lameter81819f02007-05-06 14:49:36 -07003129 slab_state = UP;
3130
3131 /* Provide the correct kmalloc names now that the caches are up */
Christoph Lameterffadd4d2009-02-17 12:05:07 -05003132 for (i = KMALLOC_SHIFT_LOW; i < SLUB_PAGE_SHIFT; i++)
Christoph Lameter81819f02007-05-06 14:49:36 -07003133 kmalloc_caches[i]. name =
3134 kasprintf(GFP_KERNEL, "kmalloc-%d", 1 << i);
3135
3136#ifdef CONFIG_SMP
3137 register_cpu_notifier(&slab_notifier);
Christoph Lameter4c93c3552007-10-16 01:26:08 -07003138 kmem_size = offsetof(struct kmem_cache, cpu_slab) +
3139 nr_cpu_ids * sizeof(struct kmem_cache_cpu *);
3140#else
3141 kmem_size = sizeof(struct kmem_cache);
Christoph Lameter81819f02007-05-06 14:49:36 -07003142#endif
3143
Ingo Molnar3adbefe2008-02-05 17:57:39 -08003144 printk(KERN_INFO
3145 "SLUB: Genslabs=%d, HWalign=%d, Order=%d-%d, MinObjects=%d,"
Christoph Lameter4b356be2007-06-16 10:16:13 -07003146 " CPUs=%d, Nodes=%d\n",
3147 caches, cache_line_size(),
Christoph Lameter81819f02007-05-06 14:49:36 -07003148 slub_min_order, slub_max_order, slub_min_objects,
3149 nr_cpu_ids, nr_node_ids);
3150}
3151
3152/*
3153 * Find a mergeable slab cache
3154 */
3155static int slab_unmergeable(struct kmem_cache *s)
3156{
3157 if (slub_nomerge || (s->flags & SLUB_NEVER_MERGE))
3158 return 1;
3159
Christoph Lameterc59def9f2007-05-16 22:10:50 -07003160 if (s->ctor)
Christoph Lameter81819f02007-05-06 14:49:36 -07003161 return 1;
3162
Christoph Lameter8ffa6872007-05-31 00:40:51 -07003163 /*
3164 * We may have set a slab to be unmergeable during bootstrap.
3165 */
3166 if (s->refcount < 0)
3167 return 1;
3168
Christoph Lameter81819f02007-05-06 14:49:36 -07003169 return 0;
3170}
3171
3172static struct kmem_cache *find_mergeable(size_t size,
Christoph Lameterba0268a2007-09-11 15:24:11 -07003173 size_t align, unsigned long flags, const char *name,
Alexey Dobriyan51cc5062008-07-25 19:45:34 -07003174 void (*ctor)(void *))
Christoph Lameter81819f02007-05-06 14:49:36 -07003175{
Christoph Lameter5b95a4a2007-07-17 04:03:19 -07003176 struct kmem_cache *s;
Christoph Lameter81819f02007-05-06 14:49:36 -07003177
3178 if (slub_nomerge || (flags & SLUB_NEVER_MERGE))
3179 return NULL;
3180
Christoph Lameterc59def9f2007-05-16 22:10:50 -07003181 if (ctor)
Christoph Lameter81819f02007-05-06 14:49:36 -07003182 return NULL;
3183
3184 size = ALIGN(size, sizeof(void *));
3185 align = calculate_alignment(flags, align, size);
3186 size = ALIGN(size, align);
Christoph Lameterba0268a2007-09-11 15:24:11 -07003187 flags = kmem_cache_flags(size, flags, name, NULL);
Christoph Lameter81819f02007-05-06 14:49:36 -07003188
Christoph Lameter5b95a4a2007-07-17 04:03:19 -07003189 list_for_each_entry(s, &slab_caches, list) {
Christoph Lameter81819f02007-05-06 14:49:36 -07003190 if (slab_unmergeable(s))
3191 continue;
3192
3193 if (size > s->size)
3194 continue;
3195
Christoph Lameterba0268a2007-09-11 15:24:11 -07003196 if ((flags & SLUB_MERGE_SAME) != (s->flags & SLUB_MERGE_SAME))
Christoph Lameter81819f02007-05-06 14:49:36 -07003197 continue;
3198 /*
3199 * Check if alignment is compatible.
3200 * Courtesy of Adrian Drzewiecki
3201 */
Pekka Enberg06428782008-01-07 23:20:27 -08003202 if ((s->size & ~(align - 1)) != s->size)
Christoph Lameter81819f02007-05-06 14:49:36 -07003203 continue;
3204
3205 if (s->size - size >= sizeof(void *))
3206 continue;
3207
3208 return s;
3209 }
3210 return NULL;
3211}
3212
3213struct kmem_cache *kmem_cache_create(const char *name, size_t size,
Alexey Dobriyan51cc5062008-07-25 19:45:34 -07003214 size_t align, unsigned long flags, void (*ctor)(void *))
Christoph Lameter81819f02007-05-06 14:49:36 -07003215{
3216 struct kmem_cache *s;
3217
3218 down_write(&slub_lock);
Christoph Lameterba0268a2007-09-11 15:24:11 -07003219 s = find_mergeable(size, align, flags, name, ctor);
Christoph Lameter81819f02007-05-06 14:49:36 -07003220 if (s) {
Christoph Lameter42a9fdb2007-10-16 01:26:09 -07003221 int cpu;
3222
Christoph Lameter81819f02007-05-06 14:49:36 -07003223 s->refcount++;
3224 /*
3225 * Adjust the object sizes so that we clear
3226 * the complete object on kzalloc.
3227 */
3228 s->objsize = max(s->objsize, (int)size);
Christoph Lameter42a9fdb2007-10-16 01:26:09 -07003229
3230 /*
3231 * And then we need to update the object size in the
3232 * per cpu structures
3233 */
3234 for_each_online_cpu(cpu)
3235 get_cpu_slab(s, cpu)->objsize = s->objsize;
Christoph Lameter6446faa2008-02-15 23:45:26 -08003236
Christoph Lameter81819f02007-05-06 14:49:36 -07003237 s->inuse = max_t(int, s->inuse, ALIGN(size, sizeof(void *)));
Christoph Lametera0e1d1b2007-07-17 04:03:31 -07003238 up_write(&slub_lock);
Christoph Lameter6446faa2008-02-15 23:45:26 -08003239
David Rientjes7b8f3b62008-12-17 22:09:46 -08003240 if (sysfs_slab_alias(s, name)) {
3241 down_write(&slub_lock);
3242 s->refcount--;
3243 up_write(&slub_lock);
Christoph Lameter81819f02007-05-06 14:49:36 -07003244 goto err;
David Rientjes7b8f3b62008-12-17 22:09:46 -08003245 }
Christoph Lametera0e1d1b2007-07-17 04:03:31 -07003246 return s;
3247 }
Christoph Lameter6446faa2008-02-15 23:45:26 -08003248
Christoph Lametera0e1d1b2007-07-17 04:03:31 -07003249 s = kmalloc(kmem_size, GFP_KERNEL);
3250 if (s) {
3251 if (kmem_cache_open(s, GFP_KERNEL, name,
Christoph Lameterc59def9f2007-05-16 22:10:50 -07003252 size, align, flags, ctor)) {
Christoph Lameter81819f02007-05-06 14:49:36 -07003253 list_add(&s->list, &slab_caches);
Christoph Lametera0e1d1b2007-07-17 04:03:31 -07003254 up_write(&slub_lock);
David Rientjes7b8f3b62008-12-17 22:09:46 -08003255 if (sysfs_slab_add(s)) {
3256 down_write(&slub_lock);
3257 list_del(&s->list);
3258 up_write(&slub_lock);
3259 kfree(s);
Christoph Lametera0e1d1b2007-07-17 04:03:31 -07003260 goto err;
David Rientjes7b8f3b62008-12-17 22:09:46 -08003261 }
Christoph Lametera0e1d1b2007-07-17 04:03:31 -07003262 return s;
3263 }
3264 kfree(s);
Christoph Lameter81819f02007-05-06 14:49:36 -07003265 }
3266 up_write(&slub_lock);
Christoph Lameter81819f02007-05-06 14:49:36 -07003267
3268err:
Christoph Lameter81819f02007-05-06 14:49:36 -07003269 if (flags & SLAB_PANIC)
3270 panic("Cannot create slabcache %s\n", name);
3271 else
3272 s = NULL;
3273 return s;
3274}
3275EXPORT_SYMBOL(kmem_cache_create);
3276
Christoph Lameter81819f02007-05-06 14:49:36 -07003277#ifdef CONFIG_SMP
Christoph Lameter27390bc2007-06-01 00:47:09 -07003278/*
Christoph Lameter672bba32007-05-09 02:32:39 -07003279 * Use the cpu notifier to insure that the cpu slabs are flushed when
3280 * necessary.
Christoph Lameter81819f02007-05-06 14:49:36 -07003281 */
3282static int __cpuinit slab_cpuup_callback(struct notifier_block *nfb,
3283 unsigned long action, void *hcpu)
3284{
3285 long cpu = (long)hcpu;
Christoph Lameter5b95a4a2007-07-17 04:03:19 -07003286 struct kmem_cache *s;
3287 unsigned long flags;
Christoph Lameter81819f02007-05-06 14:49:36 -07003288
3289 switch (action) {
Christoph Lameter4c93c3552007-10-16 01:26:08 -07003290 case CPU_UP_PREPARE:
3291 case CPU_UP_PREPARE_FROZEN:
3292 init_alloc_cpu_cpu(cpu);
3293 down_read(&slub_lock);
3294 list_for_each_entry(s, &slab_caches, list)
3295 s->cpu_slab[cpu] = alloc_kmem_cache_cpu(s, cpu,
3296 GFP_KERNEL);
3297 up_read(&slub_lock);
3298 break;
3299
Christoph Lameter81819f02007-05-06 14:49:36 -07003300 case CPU_UP_CANCELED:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07003301 case CPU_UP_CANCELED_FROZEN:
Christoph Lameter81819f02007-05-06 14:49:36 -07003302 case CPU_DEAD:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07003303 case CPU_DEAD_FROZEN:
Christoph Lameter5b95a4a2007-07-17 04:03:19 -07003304 down_read(&slub_lock);
3305 list_for_each_entry(s, &slab_caches, list) {
Christoph Lameter4c93c3552007-10-16 01:26:08 -07003306 struct kmem_cache_cpu *c = get_cpu_slab(s, cpu);
3307
Christoph Lameter5b95a4a2007-07-17 04:03:19 -07003308 local_irq_save(flags);
3309 __flush_cpu_slab(s, cpu);
3310 local_irq_restore(flags);
Christoph Lameter4c93c3552007-10-16 01:26:08 -07003311 free_kmem_cache_cpu(c, cpu);
3312 s->cpu_slab[cpu] = NULL;
Christoph Lameter5b95a4a2007-07-17 04:03:19 -07003313 }
3314 up_read(&slub_lock);
Christoph Lameter81819f02007-05-06 14:49:36 -07003315 break;
3316 default:
3317 break;
3318 }
3319 return NOTIFY_OK;
3320}
3321
Pekka Enberg06428782008-01-07 23:20:27 -08003322static struct notifier_block __cpuinitdata slab_notifier = {
Ingo Molnar3adbefe2008-02-05 17:57:39 -08003323 .notifier_call = slab_cpuup_callback
Pekka Enberg06428782008-01-07 23:20:27 -08003324};
Christoph Lameter81819f02007-05-06 14:49:36 -07003325
3326#endif
3327
Eduard - Gabriel Munteanuce71e272008-08-19 20:43:25 +03003328void *__kmalloc_track_caller(size_t size, gfp_t gfpflags, unsigned long caller)
Christoph Lameter81819f02007-05-06 14:49:36 -07003329{
Christoph Lameteraadb4bc2007-10-16 01:24:38 -07003330 struct kmem_cache *s;
Eduard - Gabriel Munteanu94b528d2008-08-24 20:49:35 +03003331 void *ret;
Christoph Lameteraadb4bc2007-10-16 01:24:38 -07003332
Christoph Lameterffadd4d2009-02-17 12:05:07 -05003333 if (unlikely(size > SLUB_MAX_SIZE))
Pekka Enbergeada35e2008-02-11 22:47:46 +02003334 return kmalloc_large(size, gfpflags);
3335
Christoph Lameteraadb4bc2007-10-16 01:24:38 -07003336 s = get_slab(size, gfpflags);
Christoph Lameter81819f02007-05-06 14:49:36 -07003337
Satyam Sharma2408c552007-10-16 01:24:44 -07003338 if (unlikely(ZERO_OR_NULL_PTR(s)))
Christoph Lameter6cb8f912007-07-17 04:03:22 -07003339 return s;
Christoph Lameter81819f02007-05-06 14:49:36 -07003340
Eduard - Gabriel Munteanu94b528d2008-08-24 20:49:35 +03003341 ret = slab_alloc(s, gfpflags, -1, caller);
3342
3343 /* Honor the call site pointer we recieved. */
Eduard - Gabriel Munteanuca2b84c2009-03-23 15:12:24 +02003344 trace_kmalloc(caller, ret, size, s->size, gfpflags);
Eduard - Gabriel Munteanu94b528d2008-08-24 20:49:35 +03003345
3346 return ret;
Christoph Lameter81819f02007-05-06 14:49:36 -07003347}
3348
3349void *__kmalloc_node_track_caller(size_t size, gfp_t gfpflags,
Eduard - Gabriel Munteanuce71e272008-08-19 20:43:25 +03003350 int node, unsigned long caller)
Christoph Lameter81819f02007-05-06 14:49:36 -07003351{
Christoph Lameteraadb4bc2007-10-16 01:24:38 -07003352 struct kmem_cache *s;
Eduard - Gabriel Munteanu94b528d2008-08-24 20:49:35 +03003353 void *ret;
Christoph Lameteraadb4bc2007-10-16 01:24:38 -07003354
Christoph Lameterffadd4d2009-02-17 12:05:07 -05003355 if (unlikely(size > SLUB_MAX_SIZE))
Christoph Lameterf619cfe2008-03-01 13:56:40 -08003356 return kmalloc_large_node(size, gfpflags, node);
Pekka Enbergeada35e2008-02-11 22:47:46 +02003357
Christoph Lameteraadb4bc2007-10-16 01:24:38 -07003358 s = get_slab(size, gfpflags);
Christoph Lameter81819f02007-05-06 14:49:36 -07003359
Satyam Sharma2408c552007-10-16 01:24:44 -07003360 if (unlikely(ZERO_OR_NULL_PTR(s)))
Christoph Lameter6cb8f912007-07-17 04:03:22 -07003361 return s;
Christoph Lameter81819f02007-05-06 14:49:36 -07003362
Eduard - Gabriel Munteanu94b528d2008-08-24 20:49:35 +03003363 ret = slab_alloc(s, gfpflags, node, caller);
3364
3365 /* Honor the call site pointer we recieved. */
Eduard - Gabriel Munteanuca2b84c2009-03-23 15:12:24 +02003366 trace_kmalloc_node(caller, ret, size, s->size, gfpflags, node);
Eduard - Gabriel Munteanu94b528d2008-08-24 20:49:35 +03003367
3368 return ret;
Christoph Lameter81819f02007-05-06 14:49:36 -07003369}
3370
Christoph Lameterf6acb632008-04-29 16:16:06 -07003371#ifdef CONFIG_SLUB_DEBUG
Christoph Lameter205ab992008-04-14 19:11:40 +03003372static int count_inuse(struct page *page)
3373{
3374 return page->inuse;
3375}
3376
3377static int count_total(struct page *page)
3378{
3379 return page->objects;
3380}
3381
Christoph Lameter434e2452007-07-17 04:03:30 -07003382static int validate_slab(struct kmem_cache *s, struct page *page,
3383 unsigned long *map)
Christoph Lameter53e15af2007-05-06 14:49:43 -07003384{
3385 void *p;
Christoph Lametera973e9d2008-03-01 13:40:44 -08003386 void *addr = page_address(page);
Christoph Lameter53e15af2007-05-06 14:49:43 -07003387
3388 if (!check_slab(s, page) ||
3389 !on_freelist(s, page, NULL))
3390 return 0;
3391
3392 /* Now we know that a valid freelist exists */
Christoph Lameter39b26462008-04-14 19:11:30 +03003393 bitmap_zero(map, page->objects);
Christoph Lameter53e15af2007-05-06 14:49:43 -07003394
Christoph Lameter7656c722007-05-09 02:32:40 -07003395 for_each_free_object(p, s, page->freelist) {
3396 set_bit(slab_index(p, s, addr), map);
Christoph Lameter53e15af2007-05-06 14:49:43 -07003397 if (!check_object(s, page, p, 0))
3398 return 0;
3399 }
3400
Christoph Lameter224a88b2008-04-14 19:11:31 +03003401 for_each_object(p, s, addr, page->objects)
Christoph Lameter7656c722007-05-09 02:32:40 -07003402 if (!test_bit(slab_index(p, s, addr), map))
Christoph Lameter53e15af2007-05-06 14:49:43 -07003403 if (!check_object(s, page, p, 1))
3404 return 0;
3405 return 1;
3406}
3407
Christoph Lameter434e2452007-07-17 04:03:30 -07003408static void validate_slab_slab(struct kmem_cache *s, struct page *page,
3409 unsigned long *map)
Christoph Lameter53e15af2007-05-06 14:49:43 -07003410{
3411 if (slab_trylock(page)) {
Christoph Lameter434e2452007-07-17 04:03:30 -07003412 validate_slab(s, page, map);
Christoph Lameter53e15af2007-05-06 14:49:43 -07003413 slab_unlock(page);
3414 } else
3415 printk(KERN_INFO "SLUB %s: Skipped busy slab 0x%p\n",
3416 s->name, page);
3417
3418 if (s->flags & DEBUG_DEFAULT_FLAGS) {
Andy Whitcroft8a380822008-07-23 21:27:18 -07003419 if (!PageSlubDebug(page))
3420 printk(KERN_ERR "SLUB %s: SlubDebug not set "
Christoph Lameter53e15af2007-05-06 14:49:43 -07003421 "on slab 0x%p\n", s->name, page);
3422 } else {
Andy Whitcroft8a380822008-07-23 21:27:18 -07003423 if (PageSlubDebug(page))
3424 printk(KERN_ERR "SLUB %s: SlubDebug set on "
Christoph Lameter53e15af2007-05-06 14:49:43 -07003425 "slab 0x%p\n", s->name, page);
3426 }
3427}
3428
Christoph Lameter434e2452007-07-17 04:03:30 -07003429static int validate_slab_node(struct kmem_cache *s,
3430 struct kmem_cache_node *n, unsigned long *map)
Christoph Lameter53e15af2007-05-06 14:49:43 -07003431{
3432 unsigned long count = 0;
3433 struct page *page;
3434 unsigned long flags;
3435
3436 spin_lock_irqsave(&n->list_lock, flags);
3437
3438 list_for_each_entry(page, &n->partial, lru) {
Christoph Lameter434e2452007-07-17 04:03:30 -07003439 validate_slab_slab(s, page, map);
Christoph Lameter53e15af2007-05-06 14:49:43 -07003440 count++;
3441 }
3442 if (count != n->nr_partial)
3443 printk(KERN_ERR "SLUB %s: %ld partial slabs counted but "
3444 "counter=%ld\n", s->name, count, n->nr_partial);
3445
3446 if (!(s->flags & SLAB_STORE_USER))
3447 goto out;
3448
3449 list_for_each_entry(page, &n->full, lru) {
Christoph Lameter434e2452007-07-17 04:03:30 -07003450 validate_slab_slab(s, page, map);
Christoph Lameter53e15af2007-05-06 14:49:43 -07003451 count++;
3452 }
3453 if (count != atomic_long_read(&n->nr_slabs))
3454 printk(KERN_ERR "SLUB: %s %ld slabs counted but "
3455 "counter=%ld\n", s->name, count,
3456 atomic_long_read(&n->nr_slabs));
3457
3458out:
3459 spin_unlock_irqrestore(&n->list_lock, flags);
3460 return count;
3461}
3462
Christoph Lameter434e2452007-07-17 04:03:30 -07003463static long validate_slab_cache(struct kmem_cache *s)
Christoph Lameter53e15af2007-05-06 14:49:43 -07003464{
3465 int node;
3466 unsigned long count = 0;
Christoph Lameter205ab992008-04-14 19:11:40 +03003467 unsigned long *map = kmalloc(BITS_TO_LONGS(oo_objects(s->max)) *
Christoph Lameter434e2452007-07-17 04:03:30 -07003468 sizeof(unsigned long), GFP_KERNEL);
3469
3470 if (!map)
3471 return -ENOMEM;
Christoph Lameter53e15af2007-05-06 14:49:43 -07003472
3473 flush_all(s);
Christoph Lameterf64dc582007-10-16 01:25:33 -07003474 for_each_node_state(node, N_NORMAL_MEMORY) {
Christoph Lameter53e15af2007-05-06 14:49:43 -07003475 struct kmem_cache_node *n = get_node(s, node);
3476
Christoph Lameter434e2452007-07-17 04:03:30 -07003477 count += validate_slab_node(s, n, map);
Christoph Lameter53e15af2007-05-06 14:49:43 -07003478 }
Christoph Lameter434e2452007-07-17 04:03:30 -07003479 kfree(map);
Christoph Lameter53e15af2007-05-06 14:49:43 -07003480 return count;
3481}
3482
Christoph Lameterb3459702007-05-09 02:32:41 -07003483#ifdef SLUB_RESILIENCY_TEST
3484static void resiliency_test(void)
3485{
3486 u8 *p;
3487
3488 printk(KERN_ERR "SLUB resiliency testing\n");
3489 printk(KERN_ERR "-----------------------\n");
3490 printk(KERN_ERR "A. Corruption after allocation\n");
3491
3492 p = kzalloc(16, GFP_KERNEL);
3493 p[16] = 0x12;
3494 printk(KERN_ERR "\n1. kmalloc-16: Clobber Redzone/next pointer"
3495 " 0x12->0x%p\n\n", p + 16);
3496
3497 validate_slab_cache(kmalloc_caches + 4);
3498
3499 /* Hmmm... The next two are dangerous */
3500 p = kzalloc(32, GFP_KERNEL);
3501 p[32 + sizeof(void *)] = 0x34;
3502 printk(KERN_ERR "\n2. kmalloc-32: Clobber next pointer/next slab"
Ingo Molnar3adbefe2008-02-05 17:57:39 -08003503 " 0x34 -> -0x%p\n", p);
3504 printk(KERN_ERR
3505 "If allocated object is overwritten then not detectable\n\n");
Christoph Lameterb3459702007-05-09 02:32:41 -07003506
3507 validate_slab_cache(kmalloc_caches + 5);
3508 p = kzalloc(64, GFP_KERNEL);
3509 p += 64 + (get_cycles() & 0xff) * sizeof(void *);
3510 *p = 0x56;
3511 printk(KERN_ERR "\n3. kmalloc-64: corrupting random byte 0x56->0x%p\n",
3512 p);
Ingo Molnar3adbefe2008-02-05 17:57:39 -08003513 printk(KERN_ERR
3514 "If allocated object is overwritten then not detectable\n\n");
Christoph Lameterb3459702007-05-09 02:32:41 -07003515 validate_slab_cache(kmalloc_caches + 6);
3516
3517 printk(KERN_ERR "\nB. Corruption after free\n");
3518 p = kzalloc(128, GFP_KERNEL);
3519 kfree(p);
3520 *p = 0x78;
3521 printk(KERN_ERR "1. kmalloc-128: Clobber first word 0x78->0x%p\n\n", p);
3522 validate_slab_cache(kmalloc_caches + 7);
3523
3524 p = kzalloc(256, GFP_KERNEL);
3525 kfree(p);
3526 p[50] = 0x9a;
Ingo Molnar3adbefe2008-02-05 17:57:39 -08003527 printk(KERN_ERR "\n2. kmalloc-256: Clobber 50th byte 0x9a->0x%p\n\n",
3528 p);
Christoph Lameterb3459702007-05-09 02:32:41 -07003529 validate_slab_cache(kmalloc_caches + 8);
3530
3531 p = kzalloc(512, GFP_KERNEL);
3532 kfree(p);
3533 p[512] = 0xab;
3534 printk(KERN_ERR "\n3. kmalloc-512: Clobber redzone 0xab->0x%p\n\n", p);
3535 validate_slab_cache(kmalloc_caches + 9);
3536}
3537#else
3538static void resiliency_test(void) {};
3539#endif
3540
Christoph Lameter88a420e2007-05-06 14:49:45 -07003541/*
Christoph Lameter672bba32007-05-09 02:32:39 -07003542 * Generate lists of code addresses where slabcache objects are allocated
Christoph Lameter88a420e2007-05-06 14:49:45 -07003543 * and freed.
3544 */
3545
3546struct location {
3547 unsigned long count;
Eduard - Gabriel Munteanuce71e272008-08-19 20:43:25 +03003548 unsigned long addr;
Christoph Lameter45edfa52007-05-09 02:32:45 -07003549 long long sum_time;
3550 long min_time;
3551 long max_time;
3552 long min_pid;
3553 long max_pid;
Rusty Russell174596a2009-01-01 10:12:29 +10303554 DECLARE_BITMAP(cpus, NR_CPUS);
Christoph Lameter45edfa52007-05-09 02:32:45 -07003555 nodemask_t nodes;
Christoph Lameter88a420e2007-05-06 14:49:45 -07003556};
3557
3558struct loc_track {
3559 unsigned long max;
3560 unsigned long count;
3561 struct location *loc;
3562};
3563
3564static void free_loc_track(struct loc_track *t)
3565{
3566 if (t->max)
3567 free_pages((unsigned long)t->loc,
3568 get_order(sizeof(struct location) * t->max));
3569}
3570
Christoph Lameter68dff6a2007-07-17 04:03:20 -07003571static int alloc_loc_track(struct loc_track *t, unsigned long max, gfp_t flags)
Christoph Lameter88a420e2007-05-06 14:49:45 -07003572{
3573 struct location *l;
3574 int order;
3575
Christoph Lameter88a420e2007-05-06 14:49:45 -07003576 order = get_order(sizeof(struct location) * max);
3577
Christoph Lameter68dff6a2007-07-17 04:03:20 -07003578 l = (void *)__get_free_pages(flags, order);
Christoph Lameter88a420e2007-05-06 14:49:45 -07003579 if (!l)
3580 return 0;
3581
3582 if (t->count) {
3583 memcpy(l, t->loc, sizeof(struct location) * t->count);
3584 free_loc_track(t);
3585 }
3586 t->max = max;
3587 t->loc = l;
3588 return 1;
3589}
3590
3591static int add_location(struct loc_track *t, struct kmem_cache *s,
Christoph Lameter45edfa52007-05-09 02:32:45 -07003592 const struct track *track)
Christoph Lameter88a420e2007-05-06 14:49:45 -07003593{
3594 long start, end, pos;
3595 struct location *l;
Eduard - Gabriel Munteanuce71e272008-08-19 20:43:25 +03003596 unsigned long caddr;
Christoph Lameter45edfa52007-05-09 02:32:45 -07003597 unsigned long age = jiffies - track->when;
Christoph Lameter88a420e2007-05-06 14:49:45 -07003598
3599 start = -1;
3600 end = t->count;
3601
3602 for ( ; ; ) {
3603 pos = start + (end - start + 1) / 2;
3604
3605 /*
3606 * There is nothing at "end". If we end up there
3607 * we need to add something to before end.
3608 */
3609 if (pos == end)
3610 break;
3611
3612 caddr = t->loc[pos].addr;
Christoph Lameter45edfa52007-05-09 02:32:45 -07003613 if (track->addr == caddr) {
3614
3615 l = &t->loc[pos];
3616 l->count++;
3617 if (track->when) {
3618 l->sum_time += age;
3619 if (age < l->min_time)
3620 l->min_time = age;
3621 if (age > l->max_time)
3622 l->max_time = age;
3623
3624 if (track->pid < l->min_pid)
3625 l->min_pid = track->pid;
3626 if (track->pid > l->max_pid)
3627 l->max_pid = track->pid;
3628
Rusty Russell174596a2009-01-01 10:12:29 +10303629 cpumask_set_cpu(track->cpu,
3630 to_cpumask(l->cpus));
Christoph Lameter45edfa52007-05-09 02:32:45 -07003631 }
3632 node_set(page_to_nid(virt_to_page(track)), l->nodes);
Christoph Lameter88a420e2007-05-06 14:49:45 -07003633 return 1;
3634 }
3635
Christoph Lameter45edfa52007-05-09 02:32:45 -07003636 if (track->addr < caddr)
Christoph Lameter88a420e2007-05-06 14:49:45 -07003637 end = pos;
3638 else
3639 start = pos;
3640 }
3641
3642 /*
Christoph Lameter672bba32007-05-09 02:32:39 -07003643 * Not found. Insert new tracking element.
Christoph Lameter88a420e2007-05-06 14:49:45 -07003644 */
Christoph Lameter68dff6a2007-07-17 04:03:20 -07003645 if (t->count >= t->max && !alloc_loc_track(t, 2 * t->max, GFP_ATOMIC))
Christoph Lameter88a420e2007-05-06 14:49:45 -07003646 return 0;
3647
3648 l = t->loc + pos;
3649 if (pos < t->count)
3650 memmove(l + 1, l,
3651 (t->count - pos) * sizeof(struct location));
3652 t->count++;
3653 l->count = 1;
Christoph Lameter45edfa52007-05-09 02:32:45 -07003654 l->addr = track->addr;
3655 l->sum_time = age;
3656 l->min_time = age;
3657 l->max_time = age;
3658 l->min_pid = track->pid;
3659 l->max_pid = track->pid;
Rusty Russell174596a2009-01-01 10:12:29 +10303660 cpumask_clear(to_cpumask(l->cpus));
3661 cpumask_set_cpu(track->cpu, to_cpumask(l->cpus));
Christoph Lameter45edfa52007-05-09 02:32:45 -07003662 nodes_clear(l->nodes);
3663 node_set(page_to_nid(virt_to_page(track)), l->nodes);
Christoph Lameter88a420e2007-05-06 14:49:45 -07003664 return 1;
3665}
3666
3667static void process_slab(struct loc_track *t, struct kmem_cache *s,
3668 struct page *page, enum track_item alloc)
3669{
Christoph Lametera973e9d2008-03-01 13:40:44 -08003670 void *addr = page_address(page);
Christoph Lameter39b26462008-04-14 19:11:30 +03003671 DECLARE_BITMAP(map, page->objects);
Christoph Lameter88a420e2007-05-06 14:49:45 -07003672 void *p;
3673
Christoph Lameter39b26462008-04-14 19:11:30 +03003674 bitmap_zero(map, page->objects);
Christoph Lameter7656c722007-05-09 02:32:40 -07003675 for_each_free_object(p, s, page->freelist)
3676 set_bit(slab_index(p, s, addr), map);
Christoph Lameter88a420e2007-05-06 14:49:45 -07003677
Christoph Lameter224a88b2008-04-14 19:11:31 +03003678 for_each_object(p, s, addr, page->objects)
Christoph Lameter45edfa52007-05-09 02:32:45 -07003679 if (!test_bit(slab_index(p, s, addr), map))
3680 add_location(t, s, get_track(s, p, alloc));
Christoph Lameter88a420e2007-05-06 14:49:45 -07003681}
3682
3683static int list_locations(struct kmem_cache *s, char *buf,
3684 enum track_item alloc)
3685{
Harvey Harrisone374d482008-01-31 15:20:50 -08003686 int len = 0;
Christoph Lameter88a420e2007-05-06 14:49:45 -07003687 unsigned long i;
Christoph Lameter68dff6a2007-07-17 04:03:20 -07003688 struct loc_track t = { 0, 0, NULL };
Christoph Lameter88a420e2007-05-06 14:49:45 -07003689 int node;
3690
Christoph Lameter68dff6a2007-07-17 04:03:20 -07003691 if (!alloc_loc_track(&t, PAGE_SIZE / sizeof(struct location),
Andrew Mortonea3061d2007-10-16 01:26:09 -07003692 GFP_TEMPORARY))
Christoph Lameter68dff6a2007-07-17 04:03:20 -07003693 return sprintf(buf, "Out of memory\n");
Christoph Lameter88a420e2007-05-06 14:49:45 -07003694
3695 /* Push back cpu slabs */
3696 flush_all(s);
3697
Christoph Lameterf64dc582007-10-16 01:25:33 -07003698 for_each_node_state(node, N_NORMAL_MEMORY) {
Christoph Lameter88a420e2007-05-06 14:49:45 -07003699 struct kmem_cache_node *n = get_node(s, node);
3700 unsigned long flags;
3701 struct page *page;
3702
Christoph Lameter9e869432007-08-22 14:01:56 -07003703 if (!atomic_long_read(&n->nr_slabs))
Christoph Lameter88a420e2007-05-06 14:49:45 -07003704 continue;
3705
3706 spin_lock_irqsave(&n->list_lock, flags);
3707 list_for_each_entry(page, &n->partial, lru)
3708 process_slab(&t, s, page, alloc);
3709 list_for_each_entry(page, &n->full, lru)
3710 process_slab(&t, s, page, alloc);
3711 spin_unlock_irqrestore(&n->list_lock, flags);
3712 }
3713
3714 for (i = 0; i < t.count; i++) {
Christoph Lameter45edfa52007-05-09 02:32:45 -07003715 struct location *l = &t.loc[i];
Christoph Lameter88a420e2007-05-06 14:49:45 -07003716
Hugh Dickins9c246242008-12-09 13:14:27 -08003717 if (len > PAGE_SIZE - KSYM_SYMBOL_LEN - 100)
Christoph Lameter88a420e2007-05-06 14:49:45 -07003718 break;
Harvey Harrisone374d482008-01-31 15:20:50 -08003719 len += sprintf(buf + len, "%7ld ", l->count);
Christoph Lameter45edfa52007-05-09 02:32:45 -07003720
3721 if (l->addr)
Harvey Harrisone374d482008-01-31 15:20:50 -08003722 len += sprint_symbol(buf + len, (unsigned long)l->addr);
Christoph Lameter88a420e2007-05-06 14:49:45 -07003723 else
Harvey Harrisone374d482008-01-31 15:20:50 -08003724 len += sprintf(buf + len, "<not-available>");
Christoph Lameter45edfa52007-05-09 02:32:45 -07003725
3726 if (l->sum_time != l->min_time) {
Harvey Harrisone374d482008-01-31 15:20:50 -08003727 len += sprintf(buf + len, " age=%ld/%ld/%ld",
Roman Zippelf8bd2252008-05-01 04:34:31 -07003728 l->min_time,
3729 (long)div_u64(l->sum_time, l->count),
3730 l->max_time);
Christoph Lameter45edfa52007-05-09 02:32:45 -07003731 } else
Harvey Harrisone374d482008-01-31 15:20:50 -08003732 len += sprintf(buf + len, " age=%ld",
Christoph Lameter45edfa52007-05-09 02:32:45 -07003733 l->min_time);
3734
3735 if (l->min_pid != l->max_pid)
Harvey Harrisone374d482008-01-31 15:20:50 -08003736 len += sprintf(buf + len, " pid=%ld-%ld",
Christoph Lameter45edfa52007-05-09 02:32:45 -07003737 l->min_pid, l->max_pid);
3738 else
Harvey Harrisone374d482008-01-31 15:20:50 -08003739 len += sprintf(buf + len, " pid=%ld",
Christoph Lameter45edfa52007-05-09 02:32:45 -07003740 l->min_pid);
3741
Rusty Russell174596a2009-01-01 10:12:29 +10303742 if (num_online_cpus() > 1 &&
3743 !cpumask_empty(to_cpumask(l->cpus)) &&
Harvey Harrisone374d482008-01-31 15:20:50 -08003744 len < PAGE_SIZE - 60) {
3745 len += sprintf(buf + len, " cpus=");
3746 len += cpulist_scnprintf(buf + len, PAGE_SIZE - len - 50,
Rusty Russell174596a2009-01-01 10:12:29 +10303747 to_cpumask(l->cpus));
Christoph Lameter45edfa52007-05-09 02:32:45 -07003748 }
3749
Christoph Lameter84966342007-06-23 17:16:32 -07003750 if (num_online_nodes() > 1 && !nodes_empty(l->nodes) &&
Harvey Harrisone374d482008-01-31 15:20:50 -08003751 len < PAGE_SIZE - 60) {
3752 len += sprintf(buf + len, " nodes=");
3753 len += nodelist_scnprintf(buf + len, PAGE_SIZE - len - 50,
Christoph Lameter45edfa52007-05-09 02:32:45 -07003754 l->nodes);
3755 }
3756
Harvey Harrisone374d482008-01-31 15:20:50 -08003757 len += sprintf(buf + len, "\n");
Christoph Lameter88a420e2007-05-06 14:49:45 -07003758 }
3759
3760 free_loc_track(&t);
3761 if (!t.count)
Harvey Harrisone374d482008-01-31 15:20:50 -08003762 len += sprintf(buf, "No data\n");
3763 return len;
Christoph Lameter88a420e2007-05-06 14:49:45 -07003764}
3765
Christoph Lameter81819f02007-05-06 14:49:36 -07003766enum slab_stat_type {
Christoph Lameter205ab992008-04-14 19:11:40 +03003767 SL_ALL, /* All slabs */
3768 SL_PARTIAL, /* Only partially allocated slabs */
3769 SL_CPU, /* Only slabs used for cpu caches */
3770 SL_OBJECTS, /* Determine allocated objects not slabs */
3771 SL_TOTAL /* Determine object capacity not slabs */
Christoph Lameter81819f02007-05-06 14:49:36 -07003772};
3773
Christoph Lameter205ab992008-04-14 19:11:40 +03003774#define SO_ALL (1 << SL_ALL)
Christoph Lameter81819f02007-05-06 14:49:36 -07003775#define SO_PARTIAL (1 << SL_PARTIAL)
3776#define SO_CPU (1 << SL_CPU)
3777#define SO_OBJECTS (1 << SL_OBJECTS)
Christoph Lameter205ab992008-04-14 19:11:40 +03003778#define SO_TOTAL (1 << SL_TOTAL)
Christoph Lameter81819f02007-05-06 14:49:36 -07003779
Cyrill Gorcunov62e5c4b2008-03-02 23:28:24 +03003780static ssize_t show_slab_objects(struct kmem_cache *s,
3781 char *buf, unsigned long flags)
Christoph Lameter81819f02007-05-06 14:49:36 -07003782{
3783 unsigned long total = 0;
Christoph Lameter81819f02007-05-06 14:49:36 -07003784 int node;
3785 int x;
3786 unsigned long *nodes;
3787 unsigned long *per_cpu;
3788
3789 nodes = kzalloc(2 * sizeof(unsigned long) * nr_node_ids, GFP_KERNEL);
Cyrill Gorcunov62e5c4b2008-03-02 23:28:24 +03003790 if (!nodes)
3791 return -ENOMEM;
Christoph Lameter81819f02007-05-06 14:49:36 -07003792 per_cpu = nodes + nr_node_ids;
3793
Christoph Lameter205ab992008-04-14 19:11:40 +03003794 if (flags & SO_CPU) {
3795 int cpu;
Christoph Lameter81819f02007-05-06 14:49:36 -07003796
Christoph Lameter205ab992008-04-14 19:11:40 +03003797 for_each_possible_cpu(cpu) {
3798 struct kmem_cache_cpu *c = get_cpu_slab(s, cpu);
Christoph Lameterdfb4f092007-10-16 01:26:05 -07003799
Christoph Lameter205ab992008-04-14 19:11:40 +03003800 if (!c || c->node < 0)
3801 continue;
3802
3803 if (c->page) {
3804 if (flags & SO_TOTAL)
3805 x = c->page->objects;
3806 else if (flags & SO_OBJECTS)
3807 x = c->page->inuse;
Christoph Lameter81819f02007-05-06 14:49:36 -07003808 else
3809 x = 1;
Christoph Lameter205ab992008-04-14 19:11:40 +03003810
Christoph Lameter81819f02007-05-06 14:49:36 -07003811 total += x;
Christoph Lameter205ab992008-04-14 19:11:40 +03003812 nodes[c->node] += x;
Christoph Lameter81819f02007-05-06 14:49:36 -07003813 }
Christoph Lameter205ab992008-04-14 19:11:40 +03003814 per_cpu[c->node]++;
Christoph Lameter81819f02007-05-06 14:49:36 -07003815 }
3816 }
3817
Christoph Lameter205ab992008-04-14 19:11:40 +03003818 if (flags & SO_ALL) {
3819 for_each_node_state(node, N_NORMAL_MEMORY) {
3820 struct kmem_cache_node *n = get_node(s, node);
Christoph Lameter81819f02007-05-06 14:49:36 -07003821
Christoph Lameter205ab992008-04-14 19:11:40 +03003822 if (flags & SO_TOTAL)
3823 x = atomic_long_read(&n->total_objects);
3824 else if (flags & SO_OBJECTS)
3825 x = atomic_long_read(&n->total_objects) -
3826 count_partial(n, count_free);
3827
3828 else
3829 x = atomic_long_read(&n->nr_slabs);
3830 total += x;
3831 nodes[node] += x;
3832 }
3833
3834 } else if (flags & SO_PARTIAL) {
3835 for_each_node_state(node, N_NORMAL_MEMORY) {
3836 struct kmem_cache_node *n = get_node(s, node);
3837
3838 if (flags & SO_TOTAL)
3839 x = count_partial(n, count_total);
3840 else if (flags & SO_OBJECTS)
3841 x = count_partial(n, count_inuse);
Christoph Lameter81819f02007-05-06 14:49:36 -07003842 else
3843 x = n->nr_partial;
3844 total += x;
3845 nodes[node] += x;
3846 }
Christoph Lameter81819f02007-05-06 14:49:36 -07003847 }
Christoph Lameter81819f02007-05-06 14:49:36 -07003848 x = sprintf(buf, "%lu", total);
3849#ifdef CONFIG_NUMA
Christoph Lameterf64dc582007-10-16 01:25:33 -07003850 for_each_node_state(node, N_NORMAL_MEMORY)
Christoph Lameter81819f02007-05-06 14:49:36 -07003851 if (nodes[node])
3852 x += sprintf(buf + x, " N%d=%lu",
3853 node, nodes[node]);
3854#endif
3855 kfree(nodes);
3856 return x + sprintf(buf + x, "\n");
3857}
3858
3859static int any_slab_objects(struct kmem_cache *s)
3860{
3861 int node;
Christoph Lameterdfb4f092007-10-16 01:26:05 -07003862
3863 for_each_online_node(node) {
Christoph Lameter81819f02007-05-06 14:49:36 -07003864 struct kmem_cache_node *n = get_node(s, node);
3865
Christoph Lameterdfb4f092007-10-16 01:26:05 -07003866 if (!n)
3867 continue;
3868
Benjamin Herrenschmidt4ea33e22008-05-06 20:42:39 -07003869 if (atomic_long_read(&n->total_objects))
Christoph Lameter81819f02007-05-06 14:49:36 -07003870 return 1;
3871 }
3872 return 0;
3873}
3874
3875#define to_slab_attr(n) container_of(n, struct slab_attribute, attr)
3876#define to_slab(n) container_of(n, struct kmem_cache, kobj);
3877
3878struct slab_attribute {
3879 struct attribute attr;
3880 ssize_t (*show)(struct kmem_cache *s, char *buf);
3881 ssize_t (*store)(struct kmem_cache *s, const char *x, size_t count);
3882};
3883
3884#define SLAB_ATTR_RO(_name) \
3885 static struct slab_attribute _name##_attr = __ATTR_RO(_name)
3886
3887#define SLAB_ATTR(_name) \
3888 static struct slab_attribute _name##_attr = \
3889 __ATTR(_name, 0644, _name##_show, _name##_store)
3890
Christoph Lameter81819f02007-05-06 14:49:36 -07003891static ssize_t slab_size_show(struct kmem_cache *s, char *buf)
3892{
3893 return sprintf(buf, "%d\n", s->size);
3894}
3895SLAB_ATTR_RO(slab_size);
3896
3897static ssize_t align_show(struct kmem_cache *s, char *buf)
3898{
3899 return sprintf(buf, "%d\n", s->align);
3900}
3901SLAB_ATTR_RO(align);
3902
3903static ssize_t object_size_show(struct kmem_cache *s, char *buf)
3904{
3905 return sprintf(buf, "%d\n", s->objsize);
3906}
3907SLAB_ATTR_RO(object_size);
3908
3909static ssize_t objs_per_slab_show(struct kmem_cache *s, char *buf)
3910{
Christoph Lameter834f3d12008-04-14 19:11:31 +03003911 return sprintf(buf, "%d\n", oo_objects(s->oo));
Christoph Lameter81819f02007-05-06 14:49:36 -07003912}
3913SLAB_ATTR_RO(objs_per_slab);
3914
Christoph Lameter06b285d2008-04-14 19:11:41 +03003915static ssize_t order_store(struct kmem_cache *s,
3916 const char *buf, size_t length)
3917{
Christoph Lameter0121c6192008-04-29 16:11:12 -07003918 unsigned long order;
3919 int err;
3920
3921 err = strict_strtoul(buf, 10, &order);
3922 if (err)
3923 return err;
Christoph Lameter06b285d2008-04-14 19:11:41 +03003924
3925 if (order > slub_max_order || order < slub_min_order)
3926 return -EINVAL;
3927
3928 calculate_sizes(s, order);
3929 return length;
3930}
3931
Christoph Lameter81819f02007-05-06 14:49:36 -07003932static ssize_t order_show(struct kmem_cache *s, char *buf)
3933{
Christoph Lameter834f3d12008-04-14 19:11:31 +03003934 return sprintf(buf, "%d\n", oo_order(s->oo));
Christoph Lameter81819f02007-05-06 14:49:36 -07003935}
Christoph Lameter06b285d2008-04-14 19:11:41 +03003936SLAB_ATTR(order);
Christoph Lameter81819f02007-05-06 14:49:36 -07003937
David Rientjes73d342b2009-02-22 17:40:09 -08003938static ssize_t min_partial_show(struct kmem_cache *s, char *buf)
3939{
3940 return sprintf(buf, "%lu\n", s->min_partial);
3941}
3942
3943static ssize_t min_partial_store(struct kmem_cache *s, const char *buf,
3944 size_t length)
3945{
3946 unsigned long min;
3947 int err;
3948
3949 err = strict_strtoul(buf, 10, &min);
3950 if (err)
3951 return err;
3952
David Rientjesc0bdb232009-02-25 09:16:35 +02003953 set_min_partial(s, min);
David Rientjes73d342b2009-02-22 17:40:09 -08003954 return length;
3955}
3956SLAB_ATTR(min_partial);
3957
Christoph Lameter81819f02007-05-06 14:49:36 -07003958static ssize_t ctor_show(struct kmem_cache *s, char *buf)
3959{
3960 if (s->ctor) {
3961 int n = sprint_symbol(buf, (unsigned long)s->ctor);
3962
3963 return n + sprintf(buf + n, "\n");
3964 }
3965 return 0;
3966}
3967SLAB_ATTR_RO(ctor);
3968
Christoph Lameter81819f02007-05-06 14:49:36 -07003969static ssize_t aliases_show(struct kmem_cache *s, char *buf)
3970{
3971 return sprintf(buf, "%d\n", s->refcount - 1);
3972}
3973SLAB_ATTR_RO(aliases);
3974
3975static ssize_t slabs_show(struct kmem_cache *s, char *buf)
3976{
Christoph Lameter205ab992008-04-14 19:11:40 +03003977 return show_slab_objects(s, buf, SO_ALL);
Christoph Lameter81819f02007-05-06 14:49:36 -07003978}
3979SLAB_ATTR_RO(slabs);
3980
3981static ssize_t partial_show(struct kmem_cache *s, char *buf)
3982{
Christoph Lameterd9acf4b2008-02-15 15:22:21 -08003983 return show_slab_objects(s, buf, SO_PARTIAL);
Christoph Lameter81819f02007-05-06 14:49:36 -07003984}
3985SLAB_ATTR_RO(partial);
3986
3987static ssize_t cpu_slabs_show(struct kmem_cache *s, char *buf)
3988{
Christoph Lameterd9acf4b2008-02-15 15:22:21 -08003989 return show_slab_objects(s, buf, SO_CPU);
Christoph Lameter81819f02007-05-06 14:49:36 -07003990}
3991SLAB_ATTR_RO(cpu_slabs);
3992
3993static ssize_t objects_show(struct kmem_cache *s, char *buf)
3994{
Christoph Lameter205ab992008-04-14 19:11:40 +03003995 return show_slab_objects(s, buf, SO_ALL|SO_OBJECTS);
Christoph Lameter81819f02007-05-06 14:49:36 -07003996}
3997SLAB_ATTR_RO(objects);
3998
Christoph Lameter205ab992008-04-14 19:11:40 +03003999static ssize_t objects_partial_show(struct kmem_cache *s, char *buf)
4000{
4001 return show_slab_objects(s, buf, SO_PARTIAL|SO_OBJECTS);
4002}
4003SLAB_ATTR_RO(objects_partial);
4004
4005static ssize_t total_objects_show(struct kmem_cache *s, char *buf)
4006{
4007 return show_slab_objects(s, buf, SO_ALL|SO_TOTAL);
4008}
4009SLAB_ATTR_RO(total_objects);
4010
Christoph Lameter81819f02007-05-06 14:49:36 -07004011static ssize_t sanity_checks_show(struct kmem_cache *s, char *buf)
4012{
4013 return sprintf(buf, "%d\n", !!(s->flags & SLAB_DEBUG_FREE));
4014}
4015
4016static ssize_t sanity_checks_store(struct kmem_cache *s,
4017 const char *buf, size_t length)
4018{
4019 s->flags &= ~SLAB_DEBUG_FREE;
4020 if (buf[0] == '1')
4021 s->flags |= SLAB_DEBUG_FREE;
4022 return length;
4023}
4024SLAB_ATTR(sanity_checks);
4025
4026static ssize_t trace_show(struct kmem_cache *s, char *buf)
4027{
4028 return sprintf(buf, "%d\n", !!(s->flags & SLAB_TRACE));
4029}
4030
4031static ssize_t trace_store(struct kmem_cache *s, const char *buf,
4032 size_t length)
4033{
4034 s->flags &= ~SLAB_TRACE;
4035 if (buf[0] == '1')
4036 s->flags |= SLAB_TRACE;
4037 return length;
4038}
4039SLAB_ATTR(trace);
4040
4041static ssize_t reclaim_account_show(struct kmem_cache *s, char *buf)
4042{
4043 return sprintf(buf, "%d\n", !!(s->flags & SLAB_RECLAIM_ACCOUNT));
4044}
4045
4046static ssize_t reclaim_account_store(struct kmem_cache *s,
4047 const char *buf, size_t length)
4048{
4049 s->flags &= ~SLAB_RECLAIM_ACCOUNT;
4050 if (buf[0] == '1')
4051 s->flags |= SLAB_RECLAIM_ACCOUNT;
4052 return length;
4053}
4054SLAB_ATTR(reclaim_account);
4055
4056static ssize_t hwcache_align_show(struct kmem_cache *s, char *buf)
4057{
Christoph Lameter5af60832007-05-06 14:49:56 -07004058 return sprintf(buf, "%d\n", !!(s->flags & SLAB_HWCACHE_ALIGN));
Christoph Lameter81819f02007-05-06 14:49:36 -07004059}
4060SLAB_ATTR_RO(hwcache_align);
4061
4062#ifdef CONFIG_ZONE_DMA
4063static ssize_t cache_dma_show(struct kmem_cache *s, char *buf)
4064{
4065 return sprintf(buf, "%d\n", !!(s->flags & SLAB_CACHE_DMA));
4066}
4067SLAB_ATTR_RO(cache_dma);
4068#endif
4069
4070static ssize_t destroy_by_rcu_show(struct kmem_cache *s, char *buf)
4071{
4072 return sprintf(buf, "%d\n", !!(s->flags & SLAB_DESTROY_BY_RCU));
4073}
4074SLAB_ATTR_RO(destroy_by_rcu);
4075
4076static ssize_t red_zone_show(struct kmem_cache *s, char *buf)
4077{
4078 return sprintf(buf, "%d\n", !!(s->flags & SLAB_RED_ZONE));
4079}
4080
4081static ssize_t red_zone_store(struct kmem_cache *s,
4082 const char *buf, size_t length)
4083{
4084 if (any_slab_objects(s))
4085 return -EBUSY;
4086
4087 s->flags &= ~SLAB_RED_ZONE;
4088 if (buf[0] == '1')
4089 s->flags |= SLAB_RED_ZONE;
Christoph Lameter06b285d2008-04-14 19:11:41 +03004090 calculate_sizes(s, -1);
Christoph Lameter81819f02007-05-06 14:49:36 -07004091 return length;
4092}
4093SLAB_ATTR(red_zone);
4094
4095static ssize_t poison_show(struct kmem_cache *s, char *buf)
4096{
4097 return sprintf(buf, "%d\n", !!(s->flags & SLAB_POISON));
4098}
4099
4100static ssize_t poison_store(struct kmem_cache *s,
4101 const char *buf, size_t length)
4102{
4103 if (any_slab_objects(s))
4104 return -EBUSY;
4105
4106 s->flags &= ~SLAB_POISON;
4107 if (buf[0] == '1')
4108 s->flags |= SLAB_POISON;
Christoph Lameter06b285d2008-04-14 19:11:41 +03004109 calculate_sizes(s, -1);
Christoph Lameter81819f02007-05-06 14:49:36 -07004110 return length;
4111}
4112SLAB_ATTR(poison);
4113
4114static ssize_t store_user_show(struct kmem_cache *s, char *buf)
4115{
4116 return sprintf(buf, "%d\n", !!(s->flags & SLAB_STORE_USER));
4117}
4118
4119static ssize_t store_user_store(struct kmem_cache *s,
4120 const char *buf, size_t length)
4121{
4122 if (any_slab_objects(s))
4123 return -EBUSY;
4124
4125 s->flags &= ~SLAB_STORE_USER;
4126 if (buf[0] == '1')
4127 s->flags |= SLAB_STORE_USER;
Christoph Lameter06b285d2008-04-14 19:11:41 +03004128 calculate_sizes(s, -1);
Christoph Lameter81819f02007-05-06 14:49:36 -07004129 return length;
4130}
4131SLAB_ATTR(store_user);
4132
Christoph Lameter53e15af2007-05-06 14:49:43 -07004133static ssize_t validate_show(struct kmem_cache *s, char *buf)
4134{
4135 return 0;
4136}
4137
4138static ssize_t validate_store(struct kmem_cache *s,
4139 const char *buf, size_t length)
4140{
Christoph Lameter434e2452007-07-17 04:03:30 -07004141 int ret = -EINVAL;
4142
4143 if (buf[0] == '1') {
4144 ret = validate_slab_cache(s);
4145 if (ret >= 0)
4146 ret = length;
4147 }
4148 return ret;
Christoph Lameter53e15af2007-05-06 14:49:43 -07004149}
4150SLAB_ATTR(validate);
4151
Christoph Lameter2086d262007-05-06 14:49:46 -07004152static ssize_t shrink_show(struct kmem_cache *s, char *buf)
4153{
4154 return 0;
4155}
4156
4157static ssize_t shrink_store(struct kmem_cache *s,
4158 const char *buf, size_t length)
4159{
4160 if (buf[0] == '1') {
4161 int rc = kmem_cache_shrink(s);
4162
4163 if (rc)
4164 return rc;
4165 } else
4166 return -EINVAL;
4167 return length;
4168}
4169SLAB_ATTR(shrink);
4170
Christoph Lameter88a420e2007-05-06 14:49:45 -07004171static ssize_t alloc_calls_show(struct kmem_cache *s, char *buf)
4172{
4173 if (!(s->flags & SLAB_STORE_USER))
4174 return -ENOSYS;
4175 return list_locations(s, buf, TRACK_ALLOC);
4176}
4177SLAB_ATTR_RO(alloc_calls);
4178
4179static ssize_t free_calls_show(struct kmem_cache *s, char *buf)
4180{
4181 if (!(s->flags & SLAB_STORE_USER))
4182 return -ENOSYS;
4183 return list_locations(s, buf, TRACK_FREE);
4184}
4185SLAB_ATTR_RO(free_calls);
4186
Christoph Lameter81819f02007-05-06 14:49:36 -07004187#ifdef CONFIG_NUMA
Christoph Lameter98246012008-01-07 23:20:26 -08004188static ssize_t remote_node_defrag_ratio_show(struct kmem_cache *s, char *buf)
Christoph Lameter81819f02007-05-06 14:49:36 -07004189{
Christoph Lameter98246012008-01-07 23:20:26 -08004190 return sprintf(buf, "%d\n", s->remote_node_defrag_ratio / 10);
Christoph Lameter81819f02007-05-06 14:49:36 -07004191}
4192
Christoph Lameter98246012008-01-07 23:20:26 -08004193static ssize_t remote_node_defrag_ratio_store(struct kmem_cache *s,
Christoph Lameter81819f02007-05-06 14:49:36 -07004194 const char *buf, size_t length)
4195{
Christoph Lameter0121c6192008-04-29 16:11:12 -07004196 unsigned long ratio;
4197 int err;
Christoph Lameter81819f02007-05-06 14:49:36 -07004198
Christoph Lameter0121c6192008-04-29 16:11:12 -07004199 err = strict_strtoul(buf, 10, &ratio);
4200 if (err)
4201 return err;
4202
Christoph Lametere2cb96b2008-08-19 08:51:22 -05004203 if (ratio <= 100)
Christoph Lameter0121c6192008-04-29 16:11:12 -07004204 s->remote_node_defrag_ratio = ratio * 10;
4205
Christoph Lameter81819f02007-05-06 14:49:36 -07004206 return length;
4207}
Christoph Lameter98246012008-01-07 23:20:26 -08004208SLAB_ATTR(remote_node_defrag_ratio);
Christoph Lameter81819f02007-05-06 14:49:36 -07004209#endif
4210
Christoph Lameter8ff12cf2008-02-07 17:47:41 -08004211#ifdef CONFIG_SLUB_STATS
Christoph Lameter8ff12cf2008-02-07 17:47:41 -08004212static int show_stat(struct kmem_cache *s, char *buf, enum stat_item si)
4213{
4214 unsigned long sum = 0;
4215 int cpu;
4216 int len;
4217 int *data = kmalloc(nr_cpu_ids * sizeof(int), GFP_KERNEL);
4218
4219 if (!data)
4220 return -ENOMEM;
4221
4222 for_each_online_cpu(cpu) {
4223 unsigned x = get_cpu_slab(s, cpu)->stat[si];
4224
4225 data[cpu] = x;
4226 sum += x;
4227 }
4228
4229 len = sprintf(buf, "%lu", sum);
4230
Christoph Lameter50ef37b2008-04-14 18:52:05 +03004231#ifdef CONFIG_SMP
Christoph Lameter8ff12cf2008-02-07 17:47:41 -08004232 for_each_online_cpu(cpu) {
4233 if (data[cpu] && len < PAGE_SIZE - 20)
Christoph Lameter50ef37b2008-04-14 18:52:05 +03004234 len += sprintf(buf + len, " C%d=%u", cpu, data[cpu]);
Christoph Lameter8ff12cf2008-02-07 17:47:41 -08004235 }
Christoph Lameter50ef37b2008-04-14 18:52:05 +03004236#endif
Christoph Lameter8ff12cf2008-02-07 17:47:41 -08004237 kfree(data);
4238 return len + sprintf(buf + len, "\n");
4239}
4240
4241#define STAT_ATTR(si, text) \
4242static ssize_t text##_show(struct kmem_cache *s, char *buf) \
4243{ \
4244 return show_stat(s, buf, si); \
4245} \
4246SLAB_ATTR_RO(text); \
4247
4248STAT_ATTR(ALLOC_FASTPATH, alloc_fastpath);
4249STAT_ATTR(ALLOC_SLOWPATH, alloc_slowpath);
4250STAT_ATTR(FREE_FASTPATH, free_fastpath);
4251STAT_ATTR(FREE_SLOWPATH, free_slowpath);
4252STAT_ATTR(FREE_FROZEN, free_frozen);
4253STAT_ATTR(FREE_ADD_PARTIAL, free_add_partial);
4254STAT_ATTR(FREE_REMOVE_PARTIAL, free_remove_partial);
4255STAT_ATTR(ALLOC_FROM_PARTIAL, alloc_from_partial);
4256STAT_ATTR(ALLOC_SLAB, alloc_slab);
4257STAT_ATTR(ALLOC_REFILL, alloc_refill);
4258STAT_ATTR(FREE_SLAB, free_slab);
4259STAT_ATTR(CPUSLAB_FLUSH, cpuslab_flush);
4260STAT_ATTR(DEACTIVATE_FULL, deactivate_full);
4261STAT_ATTR(DEACTIVATE_EMPTY, deactivate_empty);
4262STAT_ATTR(DEACTIVATE_TO_HEAD, deactivate_to_head);
4263STAT_ATTR(DEACTIVATE_TO_TAIL, deactivate_to_tail);
4264STAT_ATTR(DEACTIVATE_REMOTE_FREES, deactivate_remote_frees);
Christoph Lameter65c33762008-04-14 19:11:40 +03004265STAT_ATTR(ORDER_FALLBACK, order_fallback);
Christoph Lameter8ff12cf2008-02-07 17:47:41 -08004266#endif
4267
Pekka Enberg06428782008-01-07 23:20:27 -08004268static struct attribute *slab_attrs[] = {
Christoph Lameter81819f02007-05-06 14:49:36 -07004269 &slab_size_attr.attr,
4270 &object_size_attr.attr,
4271 &objs_per_slab_attr.attr,
4272 &order_attr.attr,
David Rientjes73d342b2009-02-22 17:40:09 -08004273 &min_partial_attr.attr,
Christoph Lameter81819f02007-05-06 14:49:36 -07004274 &objects_attr.attr,
Christoph Lameter205ab992008-04-14 19:11:40 +03004275 &objects_partial_attr.attr,
4276 &total_objects_attr.attr,
Christoph Lameter81819f02007-05-06 14:49:36 -07004277 &slabs_attr.attr,
4278 &partial_attr.attr,
4279 &cpu_slabs_attr.attr,
4280 &ctor_attr.attr,
Christoph Lameter81819f02007-05-06 14:49:36 -07004281 &aliases_attr.attr,
4282 &align_attr.attr,
4283 &sanity_checks_attr.attr,
4284 &trace_attr.attr,
4285 &hwcache_align_attr.attr,
4286 &reclaim_account_attr.attr,
4287 &destroy_by_rcu_attr.attr,
4288 &red_zone_attr.attr,
4289 &poison_attr.attr,
4290 &store_user_attr.attr,
Christoph Lameter53e15af2007-05-06 14:49:43 -07004291 &validate_attr.attr,
Christoph Lameter2086d262007-05-06 14:49:46 -07004292 &shrink_attr.attr,
Christoph Lameter88a420e2007-05-06 14:49:45 -07004293 &alloc_calls_attr.attr,
4294 &free_calls_attr.attr,
Christoph Lameter81819f02007-05-06 14:49:36 -07004295#ifdef CONFIG_ZONE_DMA
4296 &cache_dma_attr.attr,
4297#endif
4298#ifdef CONFIG_NUMA
Christoph Lameter98246012008-01-07 23:20:26 -08004299 &remote_node_defrag_ratio_attr.attr,
Christoph Lameter81819f02007-05-06 14:49:36 -07004300#endif
Christoph Lameter8ff12cf2008-02-07 17:47:41 -08004301#ifdef CONFIG_SLUB_STATS
4302 &alloc_fastpath_attr.attr,
4303 &alloc_slowpath_attr.attr,
4304 &free_fastpath_attr.attr,
4305 &free_slowpath_attr.attr,
4306 &free_frozen_attr.attr,
4307 &free_add_partial_attr.attr,
4308 &free_remove_partial_attr.attr,
4309 &alloc_from_partial_attr.attr,
4310 &alloc_slab_attr.attr,
4311 &alloc_refill_attr.attr,
4312 &free_slab_attr.attr,
4313 &cpuslab_flush_attr.attr,
4314 &deactivate_full_attr.attr,
4315 &deactivate_empty_attr.attr,
4316 &deactivate_to_head_attr.attr,
4317 &deactivate_to_tail_attr.attr,
4318 &deactivate_remote_frees_attr.attr,
Christoph Lameter65c33762008-04-14 19:11:40 +03004319 &order_fallback_attr.attr,
Christoph Lameter8ff12cf2008-02-07 17:47:41 -08004320#endif
Christoph Lameter81819f02007-05-06 14:49:36 -07004321 NULL
4322};
4323
4324static struct attribute_group slab_attr_group = {
4325 .attrs = slab_attrs,
4326};
4327
4328static ssize_t slab_attr_show(struct kobject *kobj,
4329 struct attribute *attr,
4330 char *buf)
4331{
4332 struct slab_attribute *attribute;
4333 struct kmem_cache *s;
4334 int err;
4335
4336 attribute = to_slab_attr(attr);
4337 s = to_slab(kobj);
4338
4339 if (!attribute->show)
4340 return -EIO;
4341
4342 err = attribute->show(s, buf);
4343
4344 return err;
4345}
4346
4347static ssize_t slab_attr_store(struct kobject *kobj,
4348 struct attribute *attr,
4349 const char *buf, size_t len)
4350{
4351 struct slab_attribute *attribute;
4352 struct kmem_cache *s;
4353 int err;
4354
4355 attribute = to_slab_attr(attr);
4356 s = to_slab(kobj);
4357
4358 if (!attribute->store)
4359 return -EIO;
4360
4361 err = attribute->store(s, buf, len);
4362
4363 return err;
4364}
4365
Christoph Lameter151c6022008-01-07 22:29:05 -08004366static void kmem_cache_release(struct kobject *kobj)
4367{
4368 struct kmem_cache *s = to_slab(kobj);
4369
4370 kfree(s);
4371}
4372
Christoph Lameter81819f02007-05-06 14:49:36 -07004373static struct sysfs_ops slab_sysfs_ops = {
4374 .show = slab_attr_show,
4375 .store = slab_attr_store,
4376};
4377
4378static struct kobj_type slab_ktype = {
4379 .sysfs_ops = &slab_sysfs_ops,
Christoph Lameter151c6022008-01-07 22:29:05 -08004380 .release = kmem_cache_release
Christoph Lameter81819f02007-05-06 14:49:36 -07004381};
4382
4383static int uevent_filter(struct kset *kset, struct kobject *kobj)
4384{
4385 struct kobj_type *ktype = get_ktype(kobj);
4386
4387 if (ktype == &slab_ktype)
4388 return 1;
4389 return 0;
4390}
4391
4392static struct kset_uevent_ops slab_uevent_ops = {
4393 .filter = uevent_filter,
4394};
4395
Greg Kroah-Hartman27c3a312007-11-01 09:29:06 -06004396static struct kset *slab_kset;
Christoph Lameter81819f02007-05-06 14:49:36 -07004397
4398#define ID_STR_LENGTH 64
4399
4400/* Create a unique string id for a slab cache:
Christoph Lameter6446faa2008-02-15 23:45:26 -08004401 *
4402 * Format :[flags-]size
Christoph Lameter81819f02007-05-06 14:49:36 -07004403 */
4404static char *create_unique_id(struct kmem_cache *s)
4405{
4406 char *name = kmalloc(ID_STR_LENGTH, GFP_KERNEL);
4407 char *p = name;
4408
4409 BUG_ON(!name);
4410
4411 *p++ = ':';
4412 /*
4413 * First flags affecting slabcache operations. We will only
4414 * get here for aliasable slabs so we do not need to support
4415 * too many flags. The flags here must cover all flags that
4416 * are matched during merging to guarantee that the id is
4417 * unique.
4418 */
4419 if (s->flags & SLAB_CACHE_DMA)
4420 *p++ = 'd';
4421 if (s->flags & SLAB_RECLAIM_ACCOUNT)
4422 *p++ = 'a';
4423 if (s->flags & SLAB_DEBUG_FREE)
4424 *p++ = 'F';
4425 if (p != name + 1)
4426 *p++ = '-';
4427 p += sprintf(p, "%07d", s->size);
4428 BUG_ON(p > name + ID_STR_LENGTH - 1);
4429 return name;
4430}
4431
4432static int sysfs_slab_add(struct kmem_cache *s)
4433{
4434 int err;
4435 const char *name;
4436 int unmergeable;
4437
4438 if (slab_state < SYSFS)
4439 /* Defer until later */
4440 return 0;
4441
4442 unmergeable = slab_unmergeable(s);
4443 if (unmergeable) {
4444 /*
4445 * Slabcache can never be merged so we can use the name proper.
4446 * This is typically the case for debug situations. In that
4447 * case we can catch duplicate names easily.
4448 */
Greg Kroah-Hartman27c3a312007-11-01 09:29:06 -06004449 sysfs_remove_link(&slab_kset->kobj, s->name);
Christoph Lameter81819f02007-05-06 14:49:36 -07004450 name = s->name;
4451 } else {
4452 /*
4453 * Create a unique name for the slab as a target
4454 * for the symlinks.
4455 */
4456 name = create_unique_id(s);
4457 }
4458
Greg Kroah-Hartman27c3a312007-11-01 09:29:06 -06004459 s->kobj.kset = slab_kset;
Greg Kroah-Hartman1eada112007-12-17 23:05:35 -07004460 err = kobject_init_and_add(&s->kobj, &slab_ktype, NULL, name);
4461 if (err) {
4462 kobject_put(&s->kobj);
Christoph Lameter81819f02007-05-06 14:49:36 -07004463 return err;
Greg Kroah-Hartman1eada112007-12-17 23:05:35 -07004464 }
Christoph Lameter81819f02007-05-06 14:49:36 -07004465
4466 err = sysfs_create_group(&s->kobj, &slab_attr_group);
4467 if (err)
4468 return err;
4469 kobject_uevent(&s->kobj, KOBJ_ADD);
4470 if (!unmergeable) {
4471 /* Setup first alias */
4472 sysfs_slab_alias(s, s->name);
4473 kfree(name);
4474 }
4475 return 0;
4476}
4477
4478static void sysfs_slab_remove(struct kmem_cache *s)
4479{
4480 kobject_uevent(&s->kobj, KOBJ_REMOVE);
4481 kobject_del(&s->kobj);
Christoph Lameter151c6022008-01-07 22:29:05 -08004482 kobject_put(&s->kobj);
Christoph Lameter81819f02007-05-06 14:49:36 -07004483}
4484
4485/*
4486 * Need to buffer aliases during bootup until sysfs becomes
Nick Andrew9f6c708e2008-12-05 14:08:08 +11004487 * available lest we lose that information.
Christoph Lameter81819f02007-05-06 14:49:36 -07004488 */
4489struct saved_alias {
4490 struct kmem_cache *s;
4491 const char *name;
4492 struct saved_alias *next;
4493};
4494
Adrian Bunk5af328a2007-07-17 04:03:27 -07004495static struct saved_alias *alias_list;
Christoph Lameter81819f02007-05-06 14:49:36 -07004496
4497static int sysfs_slab_alias(struct kmem_cache *s, const char *name)
4498{
4499 struct saved_alias *al;
4500
4501 if (slab_state == SYSFS) {
4502 /*
4503 * If we have a leftover link then remove it.
4504 */
Greg Kroah-Hartman27c3a312007-11-01 09:29:06 -06004505 sysfs_remove_link(&slab_kset->kobj, name);
4506 return sysfs_create_link(&slab_kset->kobj, &s->kobj, name);
Christoph Lameter81819f02007-05-06 14:49:36 -07004507 }
4508
4509 al = kmalloc(sizeof(struct saved_alias), GFP_KERNEL);
4510 if (!al)
4511 return -ENOMEM;
4512
4513 al->s = s;
4514 al->name = name;
4515 al->next = alias_list;
4516 alias_list = al;
4517 return 0;
4518}
4519
4520static int __init slab_sysfs_init(void)
4521{
Christoph Lameter5b95a4a2007-07-17 04:03:19 -07004522 struct kmem_cache *s;
Christoph Lameter81819f02007-05-06 14:49:36 -07004523 int err;
4524
Greg Kroah-Hartman0ff21e42007-11-06 10:36:58 -08004525 slab_kset = kset_create_and_add("slab", &slab_uevent_ops, kernel_kobj);
Greg Kroah-Hartman27c3a312007-11-01 09:29:06 -06004526 if (!slab_kset) {
Christoph Lameter81819f02007-05-06 14:49:36 -07004527 printk(KERN_ERR "Cannot register slab subsystem.\n");
4528 return -ENOSYS;
4529 }
4530
Christoph Lameter26a7bd02007-05-09 02:32:39 -07004531 slab_state = SYSFS;
4532
Christoph Lameter5b95a4a2007-07-17 04:03:19 -07004533 list_for_each_entry(s, &slab_caches, list) {
Christoph Lameter26a7bd02007-05-09 02:32:39 -07004534 err = sysfs_slab_add(s);
Christoph Lameter5d540fb2007-08-30 23:56:26 -07004535 if (err)
4536 printk(KERN_ERR "SLUB: Unable to add boot slab %s"
4537 " to sysfs\n", s->name);
Christoph Lameter26a7bd02007-05-09 02:32:39 -07004538 }
Christoph Lameter81819f02007-05-06 14:49:36 -07004539
4540 while (alias_list) {
4541 struct saved_alias *al = alias_list;
4542
4543 alias_list = alias_list->next;
4544 err = sysfs_slab_alias(al->s, al->name);
Christoph Lameter5d540fb2007-08-30 23:56:26 -07004545 if (err)
4546 printk(KERN_ERR "SLUB: Unable to add boot slab alias"
4547 " %s to sysfs\n", s->name);
Christoph Lameter81819f02007-05-06 14:49:36 -07004548 kfree(al);
4549 }
4550
4551 resiliency_test();
4552 return 0;
4553}
4554
4555__initcall(slab_sysfs_init);
Christoph Lameter81819f02007-05-06 14:49:36 -07004556#endif
Pekka J Enberg57ed3ed2008-01-01 17:23:28 +01004557
4558/*
4559 * The /proc/slabinfo ABI
4560 */
Linus Torvalds158a9622008-01-02 13:04:48 -08004561#ifdef CONFIG_SLABINFO
Pekka J Enberg57ed3ed2008-01-01 17:23:28 +01004562static void print_slabinfo_header(struct seq_file *m)
4563{
4564 seq_puts(m, "slabinfo - version: 2.1\n");
4565 seq_puts(m, "# name <active_objs> <num_objs> <objsize> "
4566 "<objperslab> <pagesperslab>");
4567 seq_puts(m, " : tunables <limit> <batchcount> <sharedfactor>");
4568 seq_puts(m, " : slabdata <active_slabs> <num_slabs> <sharedavail>");
4569 seq_putc(m, '\n');
4570}
4571
4572static void *s_start(struct seq_file *m, loff_t *pos)
4573{
4574 loff_t n = *pos;
4575
4576 down_read(&slub_lock);
4577 if (!n)
4578 print_slabinfo_header(m);
4579
4580 return seq_list_start(&slab_caches, *pos);
4581}
4582
4583static void *s_next(struct seq_file *m, void *p, loff_t *pos)
4584{
4585 return seq_list_next(p, &slab_caches, pos);
4586}
4587
4588static void s_stop(struct seq_file *m, void *p)
4589{
4590 up_read(&slub_lock);
4591}
4592
4593static int s_show(struct seq_file *m, void *p)
4594{
4595 unsigned long nr_partials = 0;
4596 unsigned long nr_slabs = 0;
4597 unsigned long nr_inuse = 0;
Christoph Lameter205ab992008-04-14 19:11:40 +03004598 unsigned long nr_objs = 0;
4599 unsigned long nr_free = 0;
Pekka J Enberg57ed3ed2008-01-01 17:23:28 +01004600 struct kmem_cache *s;
4601 int node;
4602
4603 s = list_entry(p, struct kmem_cache, list);
4604
4605 for_each_online_node(node) {
4606 struct kmem_cache_node *n = get_node(s, node);
4607
4608 if (!n)
4609 continue;
4610
4611 nr_partials += n->nr_partial;
4612 nr_slabs += atomic_long_read(&n->nr_slabs);
Christoph Lameter205ab992008-04-14 19:11:40 +03004613 nr_objs += atomic_long_read(&n->total_objects);
4614 nr_free += count_partial(n, count_free);
Pekka J Enberg57ed3ed2008-01-01 17:23:28 +01004615 }
4616
Christoph Lameter205ab992008-04-14 19:11:40 +03004617 nr_inuse = nr_objs - nr_free;
Pekka J Enberg57ed3ed2008-01-01 17:23:28 +01004618
4619 seq_printf(m, "%-17s %6lu %6lu %6u %4u %4d", s->name, nr_inuse,
Christoph Lameter834f3d12008-04-14 19:11:31 +03004620 nr_objs, s->size, oo_objects(s->oo),
4621 (1 << oo_order(s->oo)));
Pekka J Enberg57ed3ed2008-01-01 17:23:28 +01004622 seq_printf(m, " : tunables %4u %4u %4u", 0, 0, 0);
4623 seq_printf(m, " : slabdata %6lu %6lu %6lu", nr_slabs, nr_slabs,
4624 0UL);
4625 seq_putc(m, '\n');
4626 return 0;
4627}
4628
Alexey Dobriyan7b3c3a52008-10-06 02:42:17 +04004629static const struct seq_operations slabinfo_op = {
Pekka J Enberg57ed3ed2008-01-01 17:23:28 +01004630 .start = s_start,
4631 .next = s_next,
4632 .stop = s_stop,
4633 .show = s_show,
4634};
4635
Alexey Dobriyan7b3c3a52008-10-06 02:42:17 +04004636static int slabinfo_open(struct inode *inode, struct file *file)
4637{
4638 return seq_open(file, &slabinfo_op);
4639}
4640
4641static const struct file_operations proc_slabinfo_operations = {
4642 .open = slabinfo_open,
4643 .read = seq_read,
4644 .llseek = seq_lseek,
4645 .release = seq_release,
4646};
4647
4648static int __init slab_proc_init(void)
4649{
4650 proc_create("slabinfo",S_IWUSR|S_IRUGO,NULL,&proc_slabinfo_operations);
4651 return 0;
4652}
4653module_init(slab_proc_init);
Linus Torvalds158a9622008-01-02 13:04:48 -08004654#endif /* CONFIG_SLABINFO */