blob: 5f0346c97c5f58bcca059fa5786dfc6e48778c85 [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>
Vegard Nossum5a896d92008-04-04 00:54:48 +020020#include <linux/kmemcheck.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
Richard Kennedy4a923792010-10-21 10:29:19 +010031#include <trace/events/kmem.h>
32
Christoph Lameter81819f02007-05-06 14:49:36 -070033/*
34 * Lock order:
35 * 1. slab_lock(page)
36 * 2. slab->list_lock
37 *
38 * The slab_lock protects operations on the object of a particular
39 * slab and its metadata in the page struct. If the slab lock
40 * has been taken then no allocations nor frees can be performed
41 * on the objects in the slab nor can the slab be added or removed
42 * from the partial or full lists since this would mean modifying
43 * the page_struct of the slab.
44 *
45 * The list_lock protects the partial and full list on each node and
46 * the partial slab counter. If taken then no new slabs may be added or
47 * removed from the lists nor make the number of partial slabs be modified.
48 * (Note that the total number of slabs is an atomic value that may be
49 * modified without taking the list lock).
50 *
51 * The list_lock is a centralized lock and thus we avoid taking it as
52 * much as possible. As long as SLUB does not have to handle partial
53 * slabs, operations can continue without any centralized lock. F.e.
54 * allocating a long series of objects that fill up slabs does not require
55 * the list lock.
56 *
57 * The lock order is sometimes inverted when we are trying to get a slab
58 * off a list. We take the list_lock and then look for a page on the list
59 * to use. While we do that objects in the slabs may be freed. We can
60 * only operate on the slab if we have also taken the slab_lock. So we use
61 * a slab_trylock() on the slab. If trylock was successful then no frees
62 * can occur anymore and we can use the slab for allocations etc. If the
63 * slab_trylock() does not succeed then frees are in progress in the slab and
64 * we must stay away from it for a while since we may cause a bouncing
65 * cacheline if we try to acquire the lock. So go onto the next slab.
66 * If all pages are busy then we may allocate a new slab instead of reusing
Lucas De Marchi25985ed2011-03-30 22:57:33 -030067 * a partial slab. A new slab has no one operating on it and thus there is
Christoph Lameter81819f02007-05-06 14:49:36 -070068 * no danger of cacheline contention.
69 *
70 * Interrupts are disabled during allocation and deallocation in order to
71 * make the slab allocator safe to use in the context of an irq. In addition
72 * interrupts are disabled to ensure that the processor does not change
73 * while handling per_cpu slabs, due to kernel preemption.
74 *
75 * SLUB assigns one slab for allocation to each processor.
76 * Allocations only occur from these slabs called cpu slabs.
77 *
Christoph Lameter672bba32007-05-09 02:32:39 -070078 * Slabs with free elements are kept on a partial list and during regular
79 * operations no list for full slabs is used. If an object in a full slab is
Christoph Lameter81819f02007-05-06 14:49:36 -070080 * freed then the slab will show up again on the partial lists.
Christoph Lameter672bba32007-05-09 02:32:39 -070081 * We track full slabs for debugging purposes though because otherwise we
82 * cannot scan all objects.
Christoph Lameter81819f02007-05-06 14:49:36 -070083 *
84 * Slabs are freed when they become empty. Teardown and setup is
85 * minimal so we rely on the page allocators per cpu caches for
86 * fast frees and allocs.
87 *
88 * Overloading of page flags that are otherwise used for LRU management.
89 *
Christoph Lameter4b6f0752007-05-16 22:10:53 -070090 * PageActive The slab is frozen and exempt from list processing.
91 * This means that the slab is dedicated to a purpose
92 * such as satisfying allocations for a specific
93 * processor. Objects may be freed in the slab while
94 * it is frozen but slab_free will then skip the usual
95 * list operations. It is up to the processor holding
96 * the slab to integrate the slab into the slab lists
97 * when the slab is no longer needed.
98 *
99 * One use of this flag is to mark slabs that are
100 * used for allocations. Then such a slab becomes a cpu
101 * slab. The cpu slab may be equipped with an additional
Christoph Lameterdfb4f092007-10-16 01:26:05 -0700102 * freelist that allows lockless access to
Christoph Lameter894b8782007-05-10 03:15:16 -0700103 * free objects in addition to the regular freelist
104 * that requires the slab lock.
Christoph Lameter81819f02007-05-06 14:49:36 -0700105 *
106 * PageError Slab requires special handling due to debug
107 * options set. This moves slab handling out of
Christoph Lameter894b8782007-05-10 03:15:16 -0700108 * the fast path and disables lockless freelists.
Christoph Lameter81819f02007-05-06 14:49:36 -0700109 */
110
Christoph Lameteraf537b02010-07-09 14:07:14 -0500111#define SLAB_DEBUG_FLAGS (SLAB_RED_ZONE | SLAB_POISON | SLAB_STORE_USER | \
112 SLAB_TRACE | SLAB_DEBUG_FREE)
113
114static inline int kmem_cache_debug(struct kmem_cache *s)
115{
Christoph Lameter5577bd82007-05-16 22:10:56 -0700116#ifdef CONFIG_SLUB_DEBUG
Christoph Lameteraf537b02010-07-09 14:07:14 -0500117 return unlikely(s->flags & SLAB_DEBUG_FLAGS);
Christoph Lameter5577bd82007-05-16 22:10:56 -0700118#else
Christoph Lameteraf537b02010-07-09 14:07:14 -0500119 return 0;
Christoph Lameter5577bd82007-05-16 22:10:56 -0700120#endif
Christoph Lameteraf537b02010-07-09 14:07:14 -0500121}
Christoph Lameter5577bd82007-05-16 22:10:56 -0700122
Christoph Lameter81819f02007-05-06 14:49:36 -0700123/*
124 * Issues still to be resolved:
125 *
Christoph Lameter81819f02007-05-06 14:49:36 -0700126 * - Support PAGE_ALLOC_DEBUG. Should be easy to do.
127 *
Christoph Lameter81819f02007-05-06 14:49:36 -0700128 * - Variable sizing of the per node arrays
129 */
130
131/* Enable to test recovery from slab corruption on boot */
132#undef SLUB_RESILIENCY_TEST
133
Christoph Lameterb789ef52011-06-01 12:25:49 -0500134/* Enable to log cmpxchg failures */
135#undef SLUB_DEBUG_CMPXCHG
136
Christoph Lameter81819f02007-05-06 14:49:36 -0700137/*
Christoph Lameter2086d262007-05-06 14:49:46 -0700138 * Mininum number of partial slabs. These will be left on the partial
139 * lists even if they are empty. kmem_cache_shrink may reclaim them.
140 */
Christoph Lameter76be8952007-12-21 14:37:37 -0800141#define MIN_PARTIAL 5
Christoph Lametere95eed52007-05-06 14:49:44 -0700142
Christoph Lameter2086d262007-05-06 14:49:46 -0700143/*
144 * Maximum number of desirable partial slabs.
145 * The existence of more partial slabs makes kmem_cache_shrink
146 * sort the partial list by the number of objects in the.
147 */
148#define MAX_PARTIAL 10
149
Christoph Lameter81819f02007-05-06 14:49:36 -0700150#define DEBUG_DEFAULT_FLAGS (SLAB_DEBUG_FREE | SLAB_RED_ZONE | \
151 SLAB_POISON | SLAB_STORE_USER)
Christoph Lameter672bba32007-05-09 02:32:39 -0700152
Christoph Lameter81819f02007-05-06 14:49:36 -0700153/*
David Rientjes3de47212009-07-27 18:30:35 -0700154 * Debugging flags that require metadata to be stored in the slab. These get
155 * disabled when slub_debug=O is used and a cache's min order increases with
156 * metadata.
David Rientjesfa5ec8a2009-07-07 00:14:14 -0700157 */
David Rientjes3de47212009-07-27 18:30:35 -0700158#define DEBUG_METADATA_FLAGS (SLAB_RED_ZONE | SLAB_POISON | SLAB_STORE_USER)
David Rientjesfa5ec8a2009-07-07 00:14:14 -0700159
160/*
Christoph Lameter81819f02007-05-06 14:49:36 -0700161 * Set of flags that will prevent slab merging
162 */
163#define SLUB_NEVER_MERGE (SLAB_RED_ZONE | SLAB_POISON | SLAB_STORE_USER | \
Dmitry Monakhov4c13dd32010-02-26 09:36:12 +0300164 SLAB_TRACE | SLAB_DESTROY_BY_RCU | SLAB_NOLEAKTRACE | \
165 SLAB_FAILSLAB)
Christoph Lameter81819f02007-05-06 14:49:36 -0700166
167#define SLUB_MERGE_SAME (SLAB_DEBUG_FREE | SLAB_RECLAIM_ACCOUNT | \
Vegard Nossum5a896d92008-04-04 00:54:48 +0200168 SLAB_CACHE_DMA | SLAB_NOTRACK)
Christoph Lameter81819f02007-05-06 14:49:36 -0700169
Cyrill Gorcunov210b5c02008-10-22 23:00:38 +0400170#define OO_SHIFT 16
171#define OO_MASK ((1 << OO_SHIFT) - 1)
Christoph Lameter50d5c412011-06-01 12:25:45 -0500172#define MAX_OBJS_PER_PAGE 32767 /* since page.objects is u15 */
Cyrill Gorcunov210b5c02008-10-22 23:00:38 +0400173
Christoph Lameter81819f02007-05-06 14:49:36 -0700174/* Internal SLUB flags */
Christoph Lameterf90ec392010-07-09 14:07:11 -0500175#define __OBJECT_POISON 0x80000000UL /* Poison object */
Christoph Lameterb789ef52011-06-01 12:25:49 -0500176#define __CMPXCHG_DOUBLE 0x40000000UL /* Use cmpxchg_double */
Christoph Lameter81819f02007-05-06 14:49:36 -0700177
178static int kmem_size = sizeof(struct kmem_cache);
179
180#ifdef CONFIG_SMP
181static struct notifier_block slab_notifier;
182#endif
183
184static enum {
185 DOWN, /* No slab functionality available */
Christoph Lameter51df1142010-08-20 12:37:15 -0500186 PARTIAL, /* Kmem_cache_node works */
Christoph Lameter672bba32007-05-09 02:32:39 -0700187 UP, /* Everything works but does not show up in sysfs */
Christoph Lameter81819f02007-05-06 14:49:36 -0700188 SYSFS /* Sysfs up */
189} slab_state = DOWN;
190
191/* A list of all slab caches on the system */
192static DECLARE_RWSEM(slub_lock);
Adrian Bunk5af328a2007-07-17 04:03:27 -0700193static LIST_HEAD(slab_caches);
Christoph Lameter81819f02007-05-06 14:49:36 -0700194
Christoph Lameter02cbc872007-05-09 02:32:43 -0700195/*
196 * Tracking user of a slab.
197 */
198struct track {
Eduard - Gabriel Munteanuce71e272008-08-19 20:43:25 +0300199 unsigned long addr; /* Called from address */
Christoph Lameter02cbc872007-05-09 02:32:43 -0700200 int cpu; /* Was running on cpu */
201 int pid; /* Pid context */
202 unsigned long when; /* When did the operation occur */
203};
204
205enum track_item { TRACK_ALLOC, TRACK_FREE };
206
Christoph Lameterab4d5ed2010-10-05 13:57:26 -0500207#ifdef CONFIG_SYSFS
Christoph Lameter81819f02007-05-06 14:49:36 -0700208static int sysfs_slab_add(struct kmem_cache *);
209static int sysfs_slab_alias(struct kmem_cache *, const char *);
210static void sysfs_slab_remove(struct kmem_cache *);
Christoph Lameter8ff12cf2008-02-07 17:47:41 -0800211
Christoph Lameter81819f02007-05-06 14:49:36 -0700212#else
Christoph Lameter0c710012007-07-17 04:03:24 -0700213static inline int sysfs_slab_add(struct kmem_cache *s) { return 0; }
214static inline int sysfs_slab_alias(struct kmem_cache *s, const char *p)
215 { return 0; }
Christoph Lameter151c6022008-01-07 22:29:05 -0800216static inline void sysfs_slab_remove(struct kmem_cache *s)
217{
Pekka Enberg84c1cf62010-09-14 23:21:12 +0300218 kfree(s->name);
Christoph Lameter151c6022008-01-07 22:29:05 -0800219 kfree(s);
220}
Christoph Lameter8ff12cf2008-02-07 17:47:41 -0800221
Christoph Lameter81819f02007-05-06 14:49:36 -0700222#endif
223
Christoph Lameter4fdccdf2011-03-22 13:35:00 -0500224static inline void stat(const struct kmem_cache *s, enum stat_item si)
Christoph Lameter8ff12cf2008-02-07 17:47:41 -0800225{
226#ifdef CONFIG_SLUB_STATS
Christoph Lameter84e554e2009-12-18 16:26:23 -0600227 __this_cpu_inc(s->cpu_slab->stat[si]);
Christoph Lameter8ff12cf2008-02-07 17:47:41 -0800228#endif
229}
230
Christoph Lameter81819f02007-05-06 14:49:36 -0700231/********************************************************************
232 * Core slab cache functions
233 *******************************************************************/
234
235int slab_is_available(void)
236{
237 return slab_state >= UP;
238}
239
240static inline struct kmem_cache_node *get_node(struct kmem_cache *s, int node)
241{
Christoph Lameter81819f02007-05-06 14:49:36 -0700242 return s->node[node];
Christoph Lameter81819f02007-05-06 14:49:36 -0700243}
244
Christoph Lameter6446faa2008-02-15 23:45:26 -0800245/* Verify that a pointer has an address that is valid within a slab page */
Christoph Lameter02cbc872007-05-09 02:32:43 -0700246static inline int check_valid_pointer(struct kmem_cache *s,
247 struct page *page, const void *object)
248{
249 void *base;
250
Christoph Lametera973e9d2008-03-01 13:40:44 -0800251 if (!object)
Christoph Lameter02cbc872007-05-09 02:32:43 -0700252 return 1;
253
Christoph Lametera973e9d2008-03-01 13:40:44 -0800254 base = page_address(page);
Christoph Lameter39b26462008-04-14 19:11:30 +0300255 if (object < base || object >= base + page->objects * s->size ||
Christoph Lameter02cbc872007-05-09 02:32:43 -0700256 (object - base) % s->size) {
257 return 0;
258 }
259
260 return 1;
261}
262
Christoph Lameter7656c722007-05-09 02:32:40 -0700263static inline void *get_freepointer(struct kmem_cache *s, void *object)
264{
265 return *(void **)(object + s->offset);
266}
267
Christoph Lameter1393d9a2011-05-16 15:26:08 -0500268static inline void *get_freepointer_safe(struct kmem_cache *s, void *object)
269{
270 void *p;
271
272#ifdef CONFIG_DEBUG_PAGEALLOC
273 probe_kernel_read(&p, (void **)(object + s->offset), sizeof(p));
274#else
275 p = get_freepointer(s, object);
276#endif
277 return p;
278}
279
Christoph Lameter7656c722007-05-09 02:32:40 -0700280static inline void set_freepointer(struct kmem_cache *s, void *object, void *fp)
281{
282 *(void **)(object + s->offset) = fp;
283}
284
285/* Loop over all objects in a slab */
Christoph Lameter224a88b2008-04-14 19:11:31 +0300286#define for_each_object(__p, __s, __addr, __objects) \
287 for (__p = (__addr); __p < (__addr) + (__objects) * (__s)->size;\
Christoph Lameter7656c722007-05-09 02:32:40 -0700288 __p += (__s)->size)
289
Christoph Lameter7656c722007-05-09 02:32:40 -0700290/* 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
Mariusz Kozlowskid71f6062011-02-26 20:10:26 +0100296static inline size_t slab_ksize(const struct kmem_cache *s)
297{
298#ifdef CONFIG_SLUB_DEBUG
299 /*
300 * Debugging requires use of the padding between object
301 * and whatever may come after it.
302 */
303 if (s->flags & (SLAB_RED_ZONE | SLAB_POISON))
304 return s->objsize;
305
306#endif
307 /*
308 * If we have the need to store the freelist pointer
309 * back there or track user information then we can
310 * only use the space before that information.
311 */
312 if (s->flags & (SLAB_DESTROY_BY_RCU | SLAB_STORE_USER))
313 return s->inuse;
314 /*
315 * Else we can use all the padding etc for the allocation
316 */
317 return s->size;
318}
319
Lai Jiangshanab9a0f12011-03-10 15:21:48 +0800320static inline int order_objects(int order, unsigned long size, int reserved)
321{
322 return ((PAGE_SIZE << order) - reserved) / size;
323}
324
Christoph Lameter834f3d12008-04-14 19:11:31 +0300325static inline struct kmem_cache_order_objects oo_make(int order,
Lai Jiangshanab9a0f12011-03-10 15:21:48 +0800326 unsigned long size, int reserved)
Christoph Lameter834f3d12008-04-14 19:11:31 +0300327{
328 struct kmem_cache_order_objects x = {
Lai Jiangshanab9a0f12011-03-10 15:21:48 +0800329 (order << OO_SHIFT) + order_objects(order, size, reserved)
Christoph Lameter834f3d12008-04-14 19:11:31 +0300330 };
331
332 return x;
333}
334
335static inline int oo_order(struct kmem_cache_order_objects x)
336{
Cyrill Gorcunov210b5c02008-10-22 23:00:38 +0400337 return x.x >> OO_SHIFT;
Christoph Lameter834f3d12008-04-14 19:11:31 +0300338}
339
340static inline int oo_objects(struct kmem_cache_order_objects x)
341{
Cyrill Gorcunov210b5c02008-10-22 23:00:38 +0400342 return x.x & OO_MASK;
Christoph Lameter834f3d12008-04-14 19:11:31 +0300343}
344
Christoph Lameterb789ef52011-06-01 12:25:49 -0500345static inline bool cmpxchg_double_slab(struct kmem_cache *s, struct page *page,
346 void *freelist_old, unsigned long counters_old,
347 void *freelist_new, unsigned long counters_new,
348 const char *n)
349{
350#ifdef CONFIG_CMPXCHG_DOUBLE
351 if (s->flags & __CMPXCHG_DOUBLE) {
352 if (cmpxchg_double(&page->freelist,
353 freelist_old, counters_old,
354 freelist_new, counters_new))
355 return 1;
356 } else
357#endif
358 {
359 if (page->freelist == freelist_old && page->counters == counters_old) {
360 page->freelist = freelist_new;
361 page->counters = counters_new;
362 return 1;
363 }
364 }
365
366 cpu_relax();
367 stat(s, CMPXCHG_DOUBLE_FAIL);
368
369#ifdef SLUB_DEBUG_CMPXCHG
370 printk(KERN_INFO "%s %s: cmpxchg double redo ", n, s->name);
371#endif
372
373 return 0;
374}
375
Christoph Lameter41ecc552007-05-09 02:32:44 -0700376#ifdef CONFIG_SLUB_DEBUG
377/*
Christoph Lameter5f80b132011-04-15 14:48:13 -0500378 * Determine a map of object in use on a page.
379 *
380 * Slab lock or node listlock must be held to guarantee that the page does
381 * not vanish from under us.
382 */
383static void get_map(struct kmem_cache *s, struct page *page, unsigned long *map)
384{
385 void *p;
386 void *addr = page_address(page);
387
388 for (p = page->freelist; p; p = get_freepointer(s, p))
389 set_bit(slab_index(p, s, addr), map);
390}
391
Christoph Lameter41ecc552007-05-09 02:32:44 -0700392/*
393 * Debug settings:
394 */
Christoph Lameterf0630ff2007-07-15 23:38:14 -0700395#ifdef CONFIG_SLUB_DEBUG_ON
396static int slub_debug = DEBUG_DEFAULT_FLAGS;
397#else
Christoph Lameter41ecc552007-05-09 02:32:44 -0700398static int slub_debug;
Christoph Lameterf0630ff2007-07-15 23:38:14 -0700399#endif
Christoph Lameter41ecc552007-05-09 02:32:44 -0700400
401static char *slub_debug_slabs;
David Rientjesfa5ec8a2009-07-07 00:14:14 -0700402static int disable_higher_order_debug;
Christoph Lameter41ecc552007-05-09 02:32:44 -0700403
Christoph Lameter7656c722007-05-09 02:32:40 -0700404/*
Christoph Lameter81819f02007-05-06 14:49:36 -0700405 * Object debugging
406 */
407static void print_section(char *text, u8 *addr, unsigned int length)
408{
409 int i, offset;
410 int newline = 1;
411 char ascii[17];
412
413 ascii[16] = 0;
414
415 for (i = 0; i < length; i++) {
416 if (newline) {
Christoph Lameter24922682007-07-17 04:03:18 -0700417 printk(KERN_ERR "%8s 0x%p: ", text, addr + i);
Christoph Lameter81819f02007-05-06 14:49:36 -0700418 newline = 0;
419 }
Pekka Enberg06428782008-01-07 23:20:27 -0800420 printk(KERN_CONT " %02x", addr[i]);
Christoph Lameter81819f02007-05-06 14:49:36 -0700421 offset = i % 16;
422 ascii[offset] = isgraph(addr[i]) ? addr[i] : '.';
423 if (offset == 15) {
Pekka Enberg06428782008-01-07 23:20:27 -0800424 printk(KERN_CONT " %s\n", ascii);
Christoph Lameter81819f02007-05-06 14:49:36 -0700425 newline = 1;
426 }
427 }
428 if (!newline) {
429 i %= 16;
430 while (i < 16) {
Pekka Enberg06428782008-01-07 23:20:27 -0800431 printk(KERN_CONT " ");
Christoph Lameter81819f02007-05-06 14:49:36 -0700432 ascii[i] = ' ';
433 i++;
434 }
Pekka Enberg06428782008-01-07 23:20:27 -0800435 printk(KERN_CONT " %s\n", ascii);
Christoph Lameter81819f02007-05-06 14:49:36 -0700436 }
437}
438
Christoph Lameter81819f02007-05-06 14:49:36 -0700439static struct track *get_track(struct kmem_cache *s, void *object,
440 enum track_item alloc)
441{
442 struct track *p;
443
444 if (s->offset)
445 p = object + s->offset + sizeof(void *);
446 else
447 p = object + s->inuse;
448
449 return p + alloc;
450}
451
452static void set_track(struct kmem_cache *s, void *object,
Eduard - Gabriel Munteanuce71e272008-08-19 20:43:25 +0300453 enum track_item alloc, unsigned long addr)
Christoph Lameter81819f02007-05-06 14:49:36 -0700454{
Akinobu Mita1a00df42009-03-07 00:36:21 +0900455 struct track *p = get_track(s, object, alloc);
Christoph Lameter81819f02007-05-06 14:49:36 -0700456
Christoph Lameter81819f02007-05-06 14:49:36 -0700457 if (addr) {
458 p->addr = addr;
459 p->cpu = smp_processor_id();
Alexey Dobriyan88e4ccf2008-06-23 02:58:37 +0400460 p->pid = current->pid;
Christoph Lameter81819f02007-05-06 14:49:36 -0700461 p->when = jiffies;
462 } else
463 memset(p, 0, sizeof(struct track));
464}
465
Christoph Lameter81819f02007-05-06 14:49:36 -0700466static void init_tracking(struct kmem_cache *s, void *object)
467{
Christoph Lameter24922682007-07-17 04:03:18 -0700468 if (!(s->flags & SLAB_STORE_USER))
469 return;
470
Eduard - Gabriel Munteanuce71e272008-08-19 20:43:25 +0300471 set_track(s, object, TRACK_FREE, 0UL);
472 set_track(s, object, TRACK_ALLOC, 0UL);
Christoph Lameter81819f02007-05-06 14:49:36 -0700473}
474
475static void print_track(const char *s, struct track *t)
476{
477 if (!t->addr)
478 return;
479
Linus Torvalds7daf7052008-07-14 12:12:53 -0700480 printk(KERN_ERR "INFO: %s in %pS age=%lu cpu=%u pid=%d\n",
Eduard - Gabriel Munteanuce71e272008-08-19 20:43:25 +0300481 s, (void *)t->addr, jiffies - t->when, t->cpu, t->pid);
Christoph Lameter81819f02007-05-06 14:49:36 -0700482}
483
Christoph Lameter24922682007-07-17 04:03:18 -0700484static void print_tracking(struct kmem_cache *s, void *object)
485{
486 if (!(s->flags & SLAB_STORE_USER))
487 return;
488
489 print_track("Allocated", get_track(s, object, TRACK_ALLOC));
490 print_track("Freed", get_track(s, object, TRACK_FREE));
491}
492
493static void print_page_info(struct page *page)
494{
Christoph Lameter39b26462008-04-14 19:11:30 +0300495 printk(KERN_ERR "INFO: Slab 0x%p objects=%u used=%u fp=0x%p flags=0x%04lx\n",
496 page, page->objects, page->inuse, page->freelist, page->flags);
Christoph Lameter24922682007-07-17 04:03:18 -0700497
498}
499
500static void slab_bug(struct kmem_cache *s, char *fmt, ...)
501{
502 va_list args;
503 char buf[100];
504
505 va_start(args, fmt);
506 vsnprintf(buf, sizeof(buf), fmt, args);
507 va_end(args);
508 printk(KERN_ERR "========================================"
509 "=====================================\n");
510 printk(KERN_ERR "BUG %s: %s\n", s->name, buf);
511 printk(KERN_ERR "----------------------------------------"
512 "-------------------------------------\n\n");
513}
514
515static void slab_fix(struct kmem_cache *s, char *fmt, ...)
516{
517 va_list args;
518 char buf[100];
519
520 va_start(args, fmt);
521 vsnprintf(buf, sizeof(buf), fmt, args);
522 va_end(args);
523 printk(KERN_ERR "FIX %s: %s\n", s->name, buf);
524}
525
526static void print_trailer(struct kmem_cache *s, struct page *page, u8 *p)
Christoph Lameter81819f02007-05-06 14:49:36 -0700527{
528 unsigned int off; /* Offset of last byte */
Christoph Lametera973e9d2008-03-01 13:40:44 -0800529 u8 *addr = page_address(page);
Christoph Lameter24922682007-07-17 04:03:18 -0700530
531 print_tracking(s, p);
532
533 print_page_info(page);
534
535 printk(KERN_ERR "INFO: Object 0x%p @offset=%tu fp=0x%p\n\n",
536 p, p - addr, get_freepointer(s, p));
537
538 if (p > addr + 16)
539 print_section("Bytes b4", p - 16, 16);
540
Pekka Enberg0ebd6522008-07-19 14:17:22 +0300541 print_section("Object", p, min_t(unsigned long, s->objsize, PAGE_SIZE));
Christoph Lameter81819f02007-05-06 14:49:36 -0700542
543 if (s->flags & SLAB_RED_ZONE)
544 print_section("Redzone", p + s->objsize,
545 s->inuse - s->objsize);
546
Christoph Lameter81819f02007-05-06 14:49:36 -0700547 if (s->offset)
548 off = s->offset + sizeof(void *);
549 else
550 off = s->inuse;
551
Christoph Lameter24922682007-07-17 04:03:18 -0700552 if (s->flags & SLAB_STORE_USER)
Christoph Lameter81819f02007-05-06 14:49:36 -0700553 off += 2 * sizeof(struct track);
Christoph Lameter81819f02007-05-06 14:49:36 -0700554
555 if (off != s->size)
556 /* Beginning of the filler is the free pointer */
Christoph Lameter24922682007-07-17 04:03:18 -0700557 print_section("Padding", p + off, s->size - off);
558
559 dump_stack();
Christoph Lameter81819f02007-05-06 14:49:36 -0700560}
561
562static void object_err(struct kmem_cache *s, struct page *page,
563 u8 *object, char *reason)
564{
Christoph Lameter3dc50632008-04-23 12:28:01 -0700565 slab_bug(s, "%s", reason);
Christoph Lameter24922682007-07-17 04:03:18 -0700566 print_trailer(s, page, object);
Christoph Lameter81819f02007-05-06 14:49:36 -0700567}
568
Christoph Lameter24922682007-07-17 04:03:18 -0700569static void slab_err(struct kmem_cache *s, struct page *page, char *fmt, ...)
Christoph Lameter81819f02007-05-06 14:49:36 -0700570{
571 va_list args;
572 char buf[100];
573
Christoph Lameter24922682007-07-17 04:03:18 -0700574 va_start(args, fmt);
575 vsnprintf(buf, sizeof(buf), fmt, args);
Christoph Lameter81819f02007-05-06 14:49:36 -0700576 va_end(args);
Christoph Lameter3dc50632008-04-23 12:28:01 -0700577 slab_bug(s, "%s", buf);
Christoph Lameter24922682007-07-17 04:03:18 -0700578 print_page_info(page);
Christoph Lameter81819f02007-05-06 14:49:36 -0700579 dump_stack();
580}
581
Christoph Lameterf7cb1932010-09-29 07:15:01 -0500582static void init_object(struct kmem_cache *s, void *object, u8 val)
Christoph Lameter81819f02007-05-06 14:49:36 -0700583{
584 u8 *p = object;
585
586 if (s->flags & __OBJECT_POISON) {
587 memset(p, POISON_FREE, s->objsize - 1);
Pekka Enberg06428782008-01-07 23:20:27 -0800588 p[s->objsize - 1] = POISON_END;
Christoph Lameter81819f02007-05-06 14:49:36 -0700589 }
590
591 if (s->flags & SLAB_RED_ZONE)
Christoph Lameterf7cb1932010-09-29 07:15:01 -0500592 memset(p + s->objsize, val, s->inuse - s->objsize);
Christoph Lameter81819f02007-05-06 14:49:36 -0700593}
594
Christoph Lameter24922682007-07-17 04:03:18 -0700595static u8 *check_bytes(u8 *start, unsigned int value, unsigned int bytes)
Christoph Lameter81819f02007-05-06 14:49:36 -0700596{
597 while (bytes) {
598 if (*start != (u8)value)
Christoph Lameter24922682007-07-17 04:03:18 -0700599 return start;
Christoph Lameter81819f02007-05-06 14:49:36 -0700600 start++;
601 bytes--;
602 }
Christoph Lameter24922682007-07-17 04:03:18 -0700603 return NULL;
604}
605
606static void restore_bytes(struct kmem_cache *s, char *message, u8 data,
607 void *from, void *to)
608{
609 slab_fix(s, "Restoring 0x%p-0x%p=0x%x\n", from, to - 1, data);
610 memset(from, data, to - from);
611}
612
613static int check_bytes_and_report(struct kmem_cache *s, struct page *page,
614 u8 *object, char *what,
Pekka Enberg06428782008-01-07 23:20:27 -0800615 u8 *start, unsigned int value, unsigned int bytes)
Christoph Lameter24922682007-07-17 04:03:18 -0700616{
617 u8 *fault;
618 u8 *end;
619
620 fault = check_bytes(start, value, bytes);
621 if (!fault)
622 return 1;
623
624 end = start + bytes;
625 while (end > fault && end[-1] == value)
626 end--;
627
628 slab_bug(s, "%s overwritten", what);
629 printk(KERN_ERR "INFO: 0x%p-0x%p. First byte 0x%x instead of 0x%x\n",
630 fault, end - 1, fault[0], value);
631 print_trailer(s, page, object);
632
633 restore_bytes(s, what, value, fault, end);
634 return 0;
Christoph Lameter81819f02007-05-06 14:49:36 -0700635}
636
Christoph Lameter81819f02007-05-06 14:49:36 -0700637/*
638 * Object layout:
639 *
640 * object address
641 * Bytes of the object to be managed.
642 * If the freepointer may overlay the object then the free
643 * pointer is the first word of the object.
Christoph Lameter672bba32007-05-09 02:32:39 -0700644 *
Christoph Lameter81819f02007-05-06 14:49:36 -0700645 * Poisoning uses 0x6b (POISON_FREE) and the last byte is
646 * 0xa5 (POISON_END)
647 *
648 * object + s->objsize
649 * Padding to reach word boundary. This is also used for Redzoning.
Christoph Lameter672bba32007-05-09 02:32:39 -0700650 * Padding is extended by another word if Redzoning is enabled and
651 * objsize == inuse.
652 *
Christoph Lameter81819f02007-05-06 14:49:36 -0700653 * We fill with 0xbb (RED_INACTIVE) for inactive objects and with
654 * 0xcc (RED_ACTIVE) for objects in use.
655 *
656 * object + s->inuse
Christoph Lameter672bba32007-05-09 02:32:39 -0700657 * Meta data starts here.
658 *
Christoph Lameter81819f02007-05-06 14:49:36 -0700659 * A. Free pointer (if we cannot overwrite object on free)
660 * B. Tracking data for SLAB_STORE_USER
Christoph Lameter672bba32007-05-09 02:32:39 -0700661 * C. Padding to reach required alignment boundary or at mininum
Christoph Lameter6446faa2008-02-15 23:45:26 -0800662 * one word if debugging is on to be able to detect writes
Christoph Lameter672bba32007-05-09 02:32:39 -0700663 * before the word boundary.
664 *
665 * Padding is done using 0x5a (POISON_INUSE)
Christoph Lameter81819f02007-05-06 14:49:36 -0700666 *
667 * object + s->size
Christoph Lameter672bba32007-05-09 02:32:39 -0700668 * Nothing is used beyond s->size.
Christoph Lameter81819f02007-05-06 14:49:36 -0700669 *
Christoph Lameter672bba32007-05-09 02:32:39 -0700670 * If slabcaches are merged then the objsize and inuse boundaries are mostly
671 * ignored. And therefore no slab options that rely on these boundaries
Christoph Lameter81819f02007-05-06 14:49:36 -0700672 * may be used with merged slabcaches.
673 */
674
Christoph Lameter81819f02007-05-06 14:49:36 -0700675static int check_pad_bytes(struct kmem_cache *s, struct page *page, u8 *p)
676{
677 unsigned long off = s->inuse; /* The end of info */
678
679 if (s->offset)
680 /* Freepointer is placed after the object. */
681 off += sizeof(void *);
682
683 if (s->flags & SLAB_STORE_USER)
684 /* We also have user information there */
685 off += 2 * sizeof(struct track);
686
687 if (s->size == off)
688 return 1;
689
Christoph Lameter24922682007-07-17 04:03:18 -0700690 return check_bytes_and_report(s, page, p, "Object padding",
691 p + off, POISON_INUSE, s->size - off);
Christoph Lameter81819f02007-05-06 14:49:36 -0700692}
693
Christoph Lameter39b26462008-04-14 19:11:30 +0300694/* Check the pad bytes at the end of a slab page */
Christoph Lameter81819f02007-05-06 14:49:36 -0700695static int slab_pad_check(struct kmem_cache *s, struct page *page)
696{
Christoph Lameter24922682007-07-17 04:03:18 -0700697 u8 *start;
698 u8 *fault;
699 u8 *end;
700 int length;
701 int remainder;
Christoph Lameter81819f02007-05-06 14:49:36 -0700702
703 if (!(s->flags & SLAB_POISON))
704 return 1;
705
Christoph Lametera973e9d2008-03-01 13:40:44 -0800706 start = page_address(page);
Lai Jiangshanab9a0f12011-03-10 15:21:48 +0800707 length = (PAGE_SIZE << compound_order(page)) - s->reserved;
Christoph Lameter39b26462008-04-14 19:11:30 +0300708 end = start + length;
709 remainder = length % s->size;
Christoph Lameter81819f02007-05-06 14:49:36 -0700710 if (!remainder)
711 return 1;
712
Christoph Lameter39b26462008-04-14 19:11:30 +0300713 fault = check_bytes(end - remainder, POISON_INUSE, remainder);
Christoph Lameter24922682007-07-17 04:03:18 -0700714 if (!fault)
715 return 1;
716 while (end > fault && end[-1] == POISON_INUSE)
717 end--;
718
719 slab_err(s, page, "Padding overwritten. 0x%p-0x%p", fault, end - 1);
Christoph Lameter39b26462008-04-14 19:11:30 +0300720 print_section("Padding", end - remainder, remainder);
Christoph Lameter24922682007-07-17 04:03:18 -0700721
Eric Dumazet8a3d2712009-09-03 16:08:06 +0200722 restore_bytes(s, "slab padding", POISON_INUSE, end - remainder, end);
Christoph Lameter24922682007-07-17 04:03:18 -0700723 return 0;
Christoph Lameter81819f02007-05-06 14:49:36 -0700724}
725
726static int check_object(struct kmem_cache *s, struct page *page,
Christoph Lameterf7cb1932010-09-29 07:15:01 -0500727 void *object, u8 val)
Christoph Lameter81819f02007-05-06 14:49:36 -0700728{
729 u8 *p = object;
730 u8 *endobject = object + s->objsize;
731
732 if (s->flags & SLAB_RED_ZONE) {
Christoph Lameter24922682007-07-17 04:03:18 -0700733 if (!check_bytes_and_report(s, page, object, "Redzone",
Christoph Lameterf7cb1932010-09-29 07:15:01 -0500734 endobject, val, s->inuse - s->objsize))
Christoph Lameter81819f02007-05-06 14:49:36 -0700735 return 0;
Christoph Lameter81819f02007-05-06 14:49:36 -0700736 } else {
Ingo Molnar3adbefe2008-02-05 17:57:39 -0800737 if ((s->flags & SLAB_POISON) && s->objsize < s->inuse) {
738 check_bytes_and_report(s, page, p, "Alignment padding",
739 endobject, POISON_INUSE, s->inuse - s->objsize);
740 }
Christoph Lameter81819f02007-05-06 14:49:36 -0700741 }
742
743 if (s->flags & SLAB_POISON) {
Christoph Lameterf7cb1932010-09-29 07:15:01 -0500744 if (val != SLUB_RED_ACTIVE && (s->flags & __OBJECT_POISON) &&
Christoph Lameter24922682007-07-17 04:03:18 -0700745 (!check_bytes_and_report(s, page, p, "Poison", p,
746 POISON_FREE, s->objsize - 1) ||
747 !check_bytes_and_report(s, page, p, "Poison",
Pekka Enberg06428782008-01-07 23:20:27 -0800748 p + s->objsize - 1, POISON_END, 1)))
Christoph Lameter81819f02007-05-06 14:49:36 -0700749 return 0;
Christoph Lameter81819f02007-05-06 14:49:36 -0700750 /*
751 * check_pad_bytes cleans up on its own.
752 */
753 check_pad_bytes(s, page, p);
754 }
755
Christoph Lameterf7cb1932010-09-29 07:15:01 -0500756 if (!s->offset && val == SLUB_RED_ACTIVE)
Christoph Lameter81819f02007-05-06 14:49:36 -0700757 /*
758 * Object and freepointer overlap. Cannot check
759 * freepointer while object is allocated.
760 */
761 return 1;
762
763 /* Check free pointer validity */
764 if (!check_valid_pointer(s, page, get_freepointer(s, p))) {
765 object_err(s, page, p, "Freepointer corrupt");
766 /*
Nick Andrew9f6c708e2008-12-05 14:08:08 +1100767 * No choice but to zap it and thus lose the remainder
Christoph Lameter81819f02007-05-06 14:49:36 -0700768 * of the free objects in this slab. May cause
Christoph Lameter672bba32007-05-09 02:32:39 -0700769 * another error because the object count is now wrong.
Christoph Lameter81819f02007-05-06 14:49:36 -0700770 */
Christoph Lametera973e9d2008-03-01 13:40:44 -0800771 set_freepointer(s, p, NULL);
Christoph Lameter81819f02007-05-06 14:49:36 -0700772 return 0;
773 }
774 return 1;
775}
776
777static int check_slab(struct kmem_cache *s, struct page *page)
778{
Christoph Lameter39b26462008-04-14 19:11:30 +0300779 int maxobj;
780
Christoph Lameter81819f02007-05-06 14:49:36 -0700781 VM_BUG_ON(!irqs_disabled());
782
783 if (!PageSlab(page)) {
Christoph Lameter24922682007-07-17 04:03:18 -0700784 slab_err(s, page, "Not a valid slab page");
Christoph Lameter81819f02007-05-06 14:49:36 -0700785 return 0;
786 }
Christoph Lameter39b26462008-04-14 19:11:30 +0300787
Lai Jiangshanab9a0f12011-03-10 15:21:48 +0800788 maxobj = order_objects(compound_order(page), s->size, s->reserved);
Christoph Lameter39b26462008-04-14 19:11:30 +0300789 if (page->objects > maxobj) {
790 slab_err(s, page, "objects %u > max %u",
791 s->name, page->objects, maxobj);
792 return 0;
793 }
794 if (page->inuse > page->objects) {
Christoph Lameter24922682007-07-17 04:03:18 -0700795 slab_err(s, page, "inuse %u > max %u",
Christoph Lameter39b26462008-04-14 19:11:30 +0300796 s->name, page->inuse, page->objects);
Christoph Lameter81819f02007-05-06 14:49:36 -0700797 return 0;
798 }
799 /* Slab_pad_check fixes things up after itself */
800 slab_pad_check(s, page);
801 return 1;
802}
803
804/*
Christoph Lameter672bba32007-05-09 02:32:39 -0700805 * Determine if a certain object on a page is on the freelist. Must hold the
806 * slab lock to guarantee that the chains are in a consistent state.
Christoph Lameter81819f02007-05-06 14:49:36 -0700807 */
808static int on_freelist(struct kmem_cache *s, struct page *page, void *search)
809{
810 int nr = 0;
811 void *fp = page->freelist;
812 void *object = NULL;
Christoph Lameter224a88b2008-04-14 19:11:31 +0300813 unsigned long max_objects;
Christoph Lameter81819f02007-05-06 14:49:36 -0700814
Christoph Lameter39b26462008-04-14 19:11:30 +0300815 while (fp && nr <= page->objects) {
Christoph Lameter81819f02007-05-06 14:49:36 -0700816 if (fp == search)
817 return 1;
818 if (!check_valid_pointer(s, page, fp)) {
819 if (object) {
820 object_err(s, page, object,
821 "Freechain corrupt");
Christoph Lametera973e9d2008-03-01 13:40:44 -0800822 set_freepointer(s, object, NULL);
Christoph Lameter81819f02007-05-06 14:49:36 -0700823 break;
824 } else {
Christoph Lameter24922682007-07-17 04:03:18 -0700825 slab_err(s, page, "Freepointer corrupt");
Christoph Lametera973e9d2008-03-01 13:40:44 -0800826 page->freelist = NULL;
Christoph Lameter39b26462008-04-14 19:11:30 +0300827 page->inuse = page->objects;
Christoph Lameter24922682007-07-17 04:03:18 -0700828 slab_fix(s, "Freelist cleared");
Christoph Lameter81819f02007-05-06 14:49:36 -0700829 return 0;
830 }
831 break;
832 }
833 object = fp;
834 fp = get_freepointer(s, object);
835 nr++;
836 }
837
Lai Jiangshanab9a0f12011-03-10 15:21:48 +0800838 max_objects = order_objects(compound_order(page), s->size, s->reserved);
Cyrill Gorcunov210b5c02008-10-22 23:00:38 +0400839 if (max_objects > MAX_OBJS_PER_PAGE)
840 max_objects = MAX_OBJS_PER_PAGE;
Christoph Lameter224a88b2008-04-14 19:11:31 +0300841
842 if (page->objects != max_objects) {
843 slab_err(s, page, "Wrong number of objects. Found %d but "
844 "should be %d", page->objects, max_objects);
845 page->objects = max_objects;
846 slab_fix(s, "Number of objects adjusted.");
847 }
Christoph Lameter39b26462008-04-14 19:11:30 +0300848 if (page->inuse != page->objects - nr) {
Christoph Lameter70d71222007-05-06 14:49:47 -0700849 slab_err(s, page, "Wrong object count. Counter is %d but "
Christoph Lameter39b26462008-04-14 19:11:30 +0300850 "counted were %d", page->inuse, page->objects - nr);
851 page->inuse = page->objects - nr;
Christoph Lameter24922682007-07-17 04:03:18 -0700852 slab_fix(s, "Object count adjusted.");
Christoph Lameter81819f02007-05-06 14:49:36 -0700853 }
854 return search == NULL;
855}
856
Christoph Lameter0121c6192008-04-29 16:11:12 -0700857static void trace(struct kmem_cache *s, struct page *page, void *object,
858 int alloc)
Christoph Lameter3ec09742007-05-16 22:11:00 -0700859{
860 if (s->flags & SLAB_TRACE) {
861 printk(KERN_INFO "TRACE %s %s 0x%p inuse=%d fp=0x%p\n",
862 s->name,
863 alloc ? "alloc" : "free",
864 object, page->inuse,
865 page->freelist);
866
867 if (!alloc)
868 print_section("Object", (void *)object, s->objsize);
869
870 dump_stack();
871 }
872}
873
Christoph Lameter643b1132007-05-06 14:49:42 -0700874/*
Christoph Lameterc016b0b2010-08-20 12:37:16 -0500875 * Hooks for other subsystems that check memory allocations. In a typical
876 * production configuration these hooks all should produce no code at all.
877 */
878static inline int slab_pre_alloc_hook(struct kmem_cache *s, gfp_t flags)
879{
Christoph Lameterc1d50832010-08-20 12:37:17 -0500880 flags &= gfp_allowed_mask;
Christoph Lameterc016b0b2010-08-20 12:37:16 -0500881 lockdep_trace_alloc(flags);
882 might_sleep_if(flags & __GFP_WAIT);
883
884 return should_failslab(s->objsize, flags, s->flags);
885}
886
887static inline void slab_post_alloc_hook(struct kmem_cache *s, gfp_t flags, void *object)
888{
Christoph Lameterc1d50832010-08-20 12:37:17 -0500889 flags &= gfp_allowed_mask;
Eric Dumazetb3d41882011-02-14 18:35:22 +0100890 kmemcheck_slab_alloc(s, flags, object, slab_ksize(s));
Christoph Lameterc016b0b2010-08-20 12:37:16 -0500891 kmemleak_alloc_recursive(object, s->objsize, 1, s->flags, flags);
892}
893
894static inline void slab_free_hook(struct kmem_cache *s, void *x)
895{
896 kmemleak_free_recursive(x, s->flags);
Christoph Lameterc016b0b2010-08-20 12:37:16 -0500897
Christoph Lameterd3f661d2011-02-25 11:38:52 -0600898 /*
899 * Trouble is that we may no longer disable interupts in the fast path
900 * So in order to make the debug calls that expect irqs to be
901 * disabled we need to disable interrupts temporarily.
902 */
903#if defined(CONFIG_KMEMCHECK) || defined(CONFIG_LOCKDEP)
904 {
905 unsigned long flags;
906
907 local_irq_save(flags);
908 kmemcheck_slab_free(s, x, s->objsize);
909 debug_check_no_locks_freed(x, s->objsize);
Christoph Lameterd3f661d2011-02-25 11:38:52 -0600910 local_irq_restore(flags);
911 }
912#endif
Thomas Gleixnerf9b615d2011-03-24 21:26:46 +0200913 if (!(s->flags & SLAB_DEBUG_OBJECTS))
914 debug_check_no_obj_freed(x, s->objsize);
Christoph Lameterc016b0b2010-08-20 12:37:16 -0500915}
916
917/*
Christoph Lameter672bba32007-05-09 02:32:39 -0700918 * Tracking of fully allocated slabs for debugging purposes.
Christoph Lameter5cc6eee2011-06-01 12:25:50 -0500919 *
920 * list_lock must be held.
Christoph Lameter643b1132007-05-06 14:49:42 -0700921 */
Christoph Lameter5cc6eee2011-06-01 12:25:50 -0500922static void add_full(struct kmem_cache *s,
923 struct kmem_cache_node *n, struct page *page)
Christoph Lameter643b1132007-05-06 14:49:42 -0700924{
Christoph Lameter643b1132007-05-06 14:49:42 -0700925 if (!(s->flags & SLAB_STORE_USER))
926 return;
927
Christoph Lameter5cc6eee2011-06-01 12:25:50 -0500928 list_add(&page->lru, &n->full);
929}
Christoph Lameter643b1132007-05-06 14:49:42 -0700930
Christoph Lameter5cc6eee2011-06-01 12:25:50 -0500931/*
932 * list_lock must be held.
933 */
934static void remove_full(struct kmem_cache *s, struct page *page)
935{
936 if (!(s->flags & SLAB_STORE_USER))
937 return;
938
Christoph Lameter643b1132007-05-06 14:49:42 -0700939 list_del(&page->lru);
Christoph Lameter643b1132007-05-06 14:49:42 -0700940}
941
Christoph Lameter0f389ec2008-04-14 18:53:02 +0300942/* Tracking of the number of slabs for debugging purposes */
943static inline unsigned long slabs_node(struct kmem_cache *s, int node)
944{
945 struct kmem_cache_node *n = get_node(s, node);
946
947 return atomic_long_read(&n->nr_slabs);
948}
949
Alexander Beregalov26c02cf2009-06-11 14:08:48 +0400950static inline unsigned long node_nr_slabs(struct kmem_cache_node *n)
951{
952 return atomic_long_read(&n->nr_slabs);
953}
954
Christoph Lameter205ab992008-04-14 19:11:40 +0300955static inline void inc_slabs_node(struct kmem_cache *s, int node, int objects)
Christoph Lameter0f389ec2008-04-14 18:53:02 +0300956{
957 struct kmem_cache_node *n = get_node(s, node);
958
959 /*
960 * May be called early in order to allocate a slab for the
961 * kmem_cache_node structure. Solve the chicken-egg
962 * dilemma by deferring the increment of the count during
963 * bootstrap (see early_kmem_cache_node_alloc).
964 */
Christoph Lameter7340cc82010-09-28 08:10:26 -0500965 if (n) {
Christoph Lameter0f389ec2008-04-14 18:53:02 +0300966 atomic_long_inc(&n->nr_slabs);
Christoph Lameter205ab992008-04-14 19:11:40 +0300967 atomic_long_add(objects, &n->total_objects);
968 }
Christoph Lameter0f389ec2008-04-14 18:53:02 +0300969}
Christoph Lameter205ab992008-04-14 19:11:40 +0300970static inline void dec_slabs_node(struct kmem_cache *s, int node, int objects)
Christoph Lameter0f389ec2008-04-14 18:53:02 +0300971{
972 struct kmem_cache_node *n = get_node(s, node);
973
974 atomic_long_dec(&n->nr_slabs);
Christoph Lameter205ab992008-04-14 19:11:40 +0300975 atomic_long_sub(objects, &n->total_objects);
Christoph Lameter0f389ec2008-04-14 18:53:02 +0300976}
977
978/* Object debug checks for alloc/free paths */
Christoph Lameter3ec09742007-05-16 22:11:00 -0700979static void setup_object_debug(struct kmem_cache *s, struct page *page,
980 void *object)
981{
982 if (!(s->flags & (SLAB_STORE_USER|SLAB_RED_ZONE|__OBJECT_POISON)))
983 return;
984
Christoph Lameterf7cb1932010-09-29 07:15:01 -0500985 init_object(s, object, SLUB_RED_INACTIVE);
Christoph Lameter3ec09742007-05-16 22:11:00 -0700986 init_tracking(s, object);
987}
988
Christoph Lameter15370662010-08-20 12:37:12 -0500989static noinline int alloc_debug_processing(struct kmem_cache *s, struct page *page,
Eduard - Gabriel Munteanuce71e272008-08-19 20:43:25 +0300990 void *object, unsigned long addr)
Christoph Lameter81819f02007-05-06 14:49:36 -0700991{
992 if (!check_slab(s, page))
993 goto bad;
994
Christoph Lameter81819f02007-05-06 14:49:36 -0700995 if (!check_valid_pointer(s, page, object)) {
996 object_err(s, page, object, "Freelist Pointer check fails");
Christoph Lameter70d71222007-05-06 14:49:47 -0700997 goto bad;
Christoph Lameter81819f02007-05-06 14:49:36 -0700998 }
999
Christoph Lameterf7cb1932010-09-29 07:15:01 -05001000 if (!check_object(s, page, object, SLUB_RED_INACTIVE))
Christoph Lameter81819f02007-05-06 14:49:36 -07001001 goto bad;
Christoph Lameter81819f02007-05-06 14:49:36 -07001002
Christoph Lameter3ec09742007-05-16 22:11:00 -07001003 /* Success perform special debug activities for allocs */
1004 if (s->flags & SLAB_STORE_USER)
1005 set_track(s, object, TRACK_ALLOC, addr);
1006 trace(s, page, object, 1);
Christoph Lameterf7cb1932010-09-29 07:15:01 -05001007 init_object(s, object, SLUB_RED_ACTIVE);
Christoph Lameter81819f02007-05-06 14:49:36 -07001008 return 1;
Christoph Lameter3ec09742007-05-16 22:11:00 -07001009
Christoph Lameter81819f02007-05-06 14:49:36 -07001010bad:
1011 if (PageSlab(page)) {
1012 /*
1013 * If this is a slab page then lets do the best we can
1014 * to avoid issues in the future. Marking all objects
Christoph Lameter672bba32007-05-09 02:32:39 -07001015 * as used avoids touching the remaining objects.
Christoph Lameter81819f02007-05-06 14:49:36 -07001016 */
Christoph Lameter24922682007-07-17 04:03:18 -07001017 slab_fix(s, "Marking all objects used");
Christoph Lameter39b26462008-04-14 19:11:30 +03001018 page->inuse = page->objects;
Christoph Lametera973e9d2008-03-01 13:40:44 -08001019 page->freelist = NULL;
Christoph Lameter81819f02007-05-06 14:49:36 -07001020 }
1021 return 0;
1022}
1023
Christoph Lameter15370662010-08-20 12:37:12 -05001024static noinline int free_debug_processing(struct kmem_cache *s,
1025 struct page *page, void *object, unsigned long addr)
Christoph Lameter81819f02007-05-06 14:49:36 -07001026{
1027 if (!check_slab(s, page))
1028 goto fail;
1029
1030 if (!check_valid_pointer(s, page, object)) {
Christoph Lameter70d71222007-05-06 14:49:47 -07001031 slab_err(s, page, "Invalid object pointer 0x%p", object);
Christoph Lameter81819f02007-05-06 14:49:36 -07001032 goto fail;
1033 }
1034
1035 if (on_freelist(s, page, object)) {
Christoph Lameter24922682007-07-17 04:03:18 -07001036 object_err(s, page, object, "Object already free");
Christoph Lameter81819f02007-05-06 14:49:36 -07001037 goto fail;
1038 }
1039
Christoph Lameterf7cb1932010-09-29 07:15:01 -05001040 if (!check_object(s, page, object, SLUB_RED_ACTIVE))
Christoph Lameter81819f02007-05-06 14:49:36 -07001041 return 0;
1042
1043 if (unlikely(s != page->slab)) {
Ingo Molnar3adbefe2008-02-05 17:57:39 -08001044 if (!PageSlab(page)) {
Christoph Lameter70d71222007-05-06 14:49:47 -07001045 slab_err(s, page, "Attempt to free object(0x%p) "
1046 "outside of slab", object);
Ingo Molnar3adbefe2008-02-05 17:57:39 -08001047 } else if (!page->slab) {
Christoph Lameter81819f02007-05-06 14:49:36 -07001048 printk(KERN_ERR
Christoph Lameter70d71222007-05-06 14:49:47 -07001049 "SLUB <none>: no slab for object 0x%p.\n",
Christoph Lameter81819f02007-05-06 14:49:36 -07001050 object);
Christoph Lameter70d71222007-05-06 14:49:47 -07001051 dump_stack();
Pekka Enberg06428782008-01-07 23:20:27 -08001052 } else
Christoph Lameter24922682007-07-17 04:03:18 -07001053 object_err(s, page, object,
1054 "page slab pointer corrupt.");
Christoph Lameter81819f02007-05-06 14:49:36 -07001055 goto fail;
1056 }
Christoph Lameter3ec09742007-05-16 22:11:00 -07001057
Christoph Lameter3ec09742007-05-16 22:11:00 -07001058 if (s->flags & SLAB_STORE_USER)
1059 set_track(s, object, TRACK_FREE, addr);
1060 trace(s, page, object, 0);
Christoph Lameterf7cb1932010-09-29 07:15:01 -05001061 init_object(s, object, SLUB_RED_INACTIVE);
Christoph Lameter81819f02007-05-06 14:49:36 -07001062 return 1;
Christoph Lameter3ec09742007-05-16 22:11:00 -07001063
Christoph Lameter81819f02007-05-06 14:49:36 -07001064fail:
Christoph Lameter24922682007-07-17 04:03:18 -07001065 slab_fix(s, "Object at 0x%p not freed", object);
Christoph Lameter81819f02007-05-06 14:49:36 -07001066 return 0;
1067}
1068
Christoph Lameter41ecc552007-05-09 02:32:44 -07001069static int __init setup_slub_debug(char *str)
1070{
Christoph Lameterf0630ff2007-07-15 23:38:14 -07001071 slub_debug = DEBUG_DEFAULT_FLAGS;
1072 if (*str++ != '=' || !*str)
1073 /*
1074 * No options specified. Switch on full debugging.
1075 */
1076 goto out;
Christoph Lameter41ecc552007-05-09 02:32:44 -07001077
1078 if (*str == ',')
Christoph Lameterf0630ff2007-07-15 23:38:14 -07001079 /*
1080 * No options but restriction on slabs. This means full
1081 * debugging for slabs matching a pattern.
1082 */
1083 goto check_slabs;
1084
David Rientjesfa5ec8a2009-07-07 00:14:14 -07001085 if (tolower(*str) == 'o') {
1086 /*
1087 * Avoid enabling debugging on caches if its minimum order
1088 * would increase as a result.
1089 */
1090 disable_higher_order_debug = 1;
1091 goto out;
1092 }
1093
Christoph Lameterf0630ff2007-07-15 23:38:14 -07001094 slub_debug = 0;
1095 if (*str == '-')
1096 /*
1097 * Switch off all debugging measures.
1098 */
1099 goto out;
1100
1101 /*
1102 * Determine which debug features should be switched on
1103 */
Pekka Enberg06428782008-01-07 23:20:27 -08001104 for (; *str && *str != ','; str++) {
Christoph Lameterf0630ff2007-07-15 23:38:14 -07001105 switch (tolower(*str)) {
1106 case 'f':
1107 slub_debug |= SLAB_DEBUG_FREE;
1108 break;
1109 case 'z':
1110 slub_debug |= SLAB_RED_ZONE;
1111 break;
1112 case 'p':
1113 slub_debug |= SLAB_POISON;
1114 break;
1115 case 'u':
1116 slub_debug |= SLAB_STORE_USER;
1117 break;
1118 case 't':
1119 slub_debug |= SLAB_TRACE;
1120 break;
Dmitry Monakhov4c13dd32010-02-26 09:36:12 +03001121 case 'a':
1122 slub_debug |= SLAB_FAILSLAB;
1123 break;
Christoph Lameterf0630ff2007-07-15 23:38:14 -07001124 default:
1125 printk(KERN_ERR "slub_debug option '%c' "
Pekka Enberg06428782008-01-07 23:20:27 -08001126 "unknown. skipped\n", *str);
Christoph Lameterf0630ff2007-07-15 23:38:14 -07001127 }
1128 }
1129
1130check_slabs:
1131 if (*str == ',')
Christoph Lameter41ecc552007-05-09 02:32:44 -07001132 slub_debug_slabs = str + 1;
Christoph Lameterf0630ff2007-07-15 23:38:14 -07001133out:
Christoph Lameter41ecc552007-05-09 02:32:44 -07001134 return 1;
1135}
1136
1137__setup("slub_debug", setup_slub_debug);
1138
Christoph Lameterba0268a2007-09-11 15:24:11 -07001139static unsigned long kmem_cache_flags(unsigned long objsize,
1140 unsigned long flags, const char *name,
Alexey Dobriyan51cc5062008-07-25 19:45:34 -07001141 void (*ctor)(void *))
Christoph Lameter41ecc552007-05-09 02:32:44 -07001142{
1143 /*
Christoph Lametere1533622008-02-15 23:45:24 -08001144 * Enable debugging if selected on the kernel commandline.
Christoph Lameter41ecc552007-05-09 02:32:44 -07001145 */
Christoph Lametere1533622008-02-15 23:45:24 -08001146 if (slub_debug && (!slub_debug_slabs ||
David Rientjes3de47212009-07-27 18:30:35 -07001147 !strncmp(slub_debug_slabs, name, strlen(slub_debug_slabs))))
1148 flags |= slub_debug;
Christoph Lameterba0268a2007-09-11 15:24:11 -07001149
1150 return flags;
Christoph Lameter41ecc552007-05-09 02:32:44 -07001151}
1152#else
Christoph Lameter3ec09742007-05-16 22:11:00 -07001153static inline void setup_object_debug(struct kmem_cache *s,
1154 struct page *page, void *object) {}
Christoph Lameter41ecc552007-05-09 02:32:44 -07001155
Christoph Lameter3ec09742007-05-16 22:11:00 -07001156static inline int alloc_debug_processing(struct kmem_cache *s,
Eduard - Gabriel Munteanuce71e272008-08-19 20:43:25 +03001157 struct page *page, void *object, unsigned long addr) { return 0; }
Christoph Lameter41ecc552007-05-09 02:32:44 -07001158
Christoph Lameter3ec09742007-05-16 22:11:00 -07001159static inline int free_debug_processing(struct kmem_cache *s,
Eduard - Gabriel Munteanuce71e272008-08-19 20:43:25 +03001160 struct page *page, void *object, unsigned long addr) { return 0; }
Christoph Lameter41ecc552007-05-09 02:32:44 -07001161
Christoph Lameter41ecc552007-05-09 02:32:44 -07001162static inline int slab_pad_check(struct kmem_cache *s, struct page *page)
1163 { return 1; }
1164static inline int check_object(struct kmem_cache *s, struct page *page,
Christoph Lameterf7cb1932010-09-29 07:15:01 -05001165 void *object, u8 val) { return 1; }
Christoph Lameter5cc6eee2011-06-01 12:25:50 -05001166static inline void add_full(struct kmem_cache *s, struct kmem_cache_node *n,
1167 struct page *page) {}
Christoph Lameter2cfb7452011-06-01 12:25:52 -05001168static inline void remove_full(struct kmem_cache *s, struct page *page) {}
Christoph Lameterba0268a2007-09-11 15:24:11 -07001169static inline unsigned long kmem_cache_flags(unsigned long objsize,
1170 unsigned long flags, const char *name,
Alexey Dobriyan51cc5062008-07-25 19:45:34 -07001171 void (*ctor)(void *))
Christoph Lameterba0268a2007-09-11 15:24:11 -07001172{
1173 return flags;
1174}
Christoph Lameter41ecc552007-05-09 02:32:44 -07001175#define slub_debug 0
Christoph Lameter0f389ec2008-04-14 18:53:02 +03001176
Ingo Molnarfdaa45e2009-09-15 11:00:26 +02001177#define disable_higher_order_debug 0
1178
Christoph Lameter0f389ec2008-04-14 18:53:02 +03001179static inline unsigned long slabs_node(struct kmem_cache *s, int node)
1180 { return 0; }
Alexander Beregalov26c02cf2009-06-11 14:08:48 +04001181static inline unsigned long node_nr_slabs(struct kmem_cache_node *n)
1182 { return 0; }
Christoph Lameter205ab992008-04-14 19:11:40 +03001183static inline void inc_slabs_node(struct kmem_cache *s, int node,
1184 int objects) {}
1185static inline void dec_slabs_node(struct kmem_cache *s, int node,
1186 int objects) {}
Christoph Lameter7d550c52010-08-25 14:07:16 -05001187
1188static inline int slab_pre_alloc_hook(struct kmem_cache *s, gfp_t flags)
1189 { return 0; }
1190
1191static inline void slab_post_alloc_hook(struct kmem_cache *s, gfp_t flags,
1192 void *object) {}
1193
1194static inline void slab_free_hook(struct kmem_cache *s, void *x) {}
1195
Christoph Lameterab4d5ed2010-10-05 13:57:26 -05001196#endif /* CONFIG_SLUB_DEBUG */
Christoph Lameter205ab992008-04-14 19:11:40 +03001197
Christoph Lameter81819f02007-05-06 14:49:36 -07001198/*
1199 * Slab allocation and freeing
1200 */
Christoph Lameter65c33762008-04-14 19:11:40 +03001201static inline struct page *alloc_slab_page(gfp_t flags, int node,
1202 struct kmem_cache_order_objects oo)
1203{
1204 int order = oo_order(oo);
1205
Vegard Nossumb1eeab62008-11-25 16:55:53 +01001206 flags |= __GFP_NOTRACK;
1207
Christoph Lameter2154a332010-07-09 14:07:10 -05001208 if (node == NUMA_NO_NODE)
Christoph Lameter65c33762008-04-14 19:11:40 +03001209 return alloc_pages(flags, order);
1210 else
Minchan Kim6b65aaf2010-04-14 23:58:36 +09001211 return alloc_pages_exact_node(node, flags, order);
Christoph Lameter65c33762008-04-14 19:11:40 +03001212}
1213
Christoph Lameter81819f02007-05-06 14:49:36 -07001214static struct page *allocate_slab(struct kmem_cache *s, gfp_t flags, int node)
1215{
Pekka Enberg06428782008-01-07 23:20:27 -08001216 struct page *page;
Christoph Lameter834f3d12008-04-14 19:11:31 +03001217 struct kmem_cache_order_objects oo = s->oo;
Pekka Enbergba522702009-06-24 21:59:51 +03001218 gfp_t alloc_gfp;
Christoph Lameter81819f02007-05-06 14:49:36 -07001219
Christoph Lameter7e0528d2011-06-01 12:25:44 -05001220 flags &= gfp_allowed_mask;
1221
1222 if (flags & __GFP_WAIT)
1223 local_irq_enable();
1224
Christoph Lameterb7a49f02008-02-14 14:21:32 -08001225 flags |= s->allocflags;
Mel Gormane12ba742007-10-16 01:25:52 -07001226
Pekka Enbergba522702009-06-24 21:59:51 +03001227 /*
1228 * Let the initial higher-order allocation fail under memory pressure
1229 * so we fall-back to the minimum order allocation.
1230 */
1231 alloc_gfp = (flags | __GFP_NOWARN | __GFP_NORETRY) & ~__GFP_NOFAIL;
1232
1233 page = alloc_slab_page(alloc_gfp, node, oo);
Christoph Lameter65c33762008-04-14 19:11:40 +03001234 if (unlikely(!page)) {
1235 oo = s->min;
1236 /*
1237 * Allocation may have failed due to fragmentation.
1238 * Try a lower order alloc if possible
1239 */
1240 page = alloc_slab_page(flags, node, oo);
Christoph Lameter81819f02007-05-06 14:49:36 -07001241
Christoph Lameter7e0528d2011-06-01 12:25:44 -05001242 if (page)
1243 stat(s, ORDER_FALLBACK);
Christoph Lameter65c33762008-04-14 19:11:40 +03001244 }
Vegard Nossum5a896d92008-04-04 00:54:48 +02001245
Christoph Lameter7e0528d2011-06-01 12:25:44 -05001246 if (flags & __GFP_WAIT)
1247 local_irq_disable();
1248
1249 if (!page)
1250 return NULL;
1251
Vegard Nossum5a896d92008-04-04 00:54:48 +02001252 if (kmemcheck_enabled
Amerigo Wang5086c382009-08-19 21:44:13 +03001253 && !(s->flags & (SLAB_NOTRACK | DEBUG_DEFAULT_FLAGS))) {
Vegard Nossumb1eeab62008-11-25 16:55:53 +01001254 int pages = 1 << oo_order(oo);
1255
1256 kmemcheck_alloc_shadow(page, oo_order(oo), flags, node);
1257
1258 /*
1259 * Objects from caches that have a constructor don't get
1260 * cleared when they're allocated, so we need to do it here.
1261 */
1262 if (s->ctor)
1263 kmemcheck_mark_uninitialized_pages(page, pages);
1264 else
1265 kmemcheck_mark_unallocated_pages(page, pages);
Vegard Nossum5a896d92008-04-04 00:54:48 +02001266 }
1267
Christoph Lameter834f3d12008-04-14 19:11:31 +03001268 page->objects = oo_objects(oo);
Christoph Lameter81819f02007-05-06 14:49:36 -07001269 mod_zone_page_state(page_zone(page),
1270 (s->flags & SLAB_RECLAIM_ACCOUNT) ?
1271 NR_SLAB_RECLAIMABLE : NR_SLAB_UNRECLAIMABLE,
Christoph Lameter65c33762008-04-14 19:11:40 +03001272 1 << oo_order(oo));
Christoph Lameter81819f02007-05-06 14:49:36 -07001273
1274 return page;
1275}
1276
1277static void setup_object(struct kmem_cache *s, struct page *page,
1278 void *object)
1279{
Christoph Lameter3ec09742007-05-16 22:11:00 -07001280 setup_object_debug(s, page, object);
Christoph Lameter4f104932007-05-06 14:50:17 -07001281 if (unlikely(s->ctor))
Alexey Dobriyan51cc5062008-07-25 19:45:34 -07001282 s->ctor(object);
Christoph Lameter81819f02007-05-06 14:49:36 -07001283}
1284
1285static struct page *new_slab(struct kmem_cache *s, gfp_t flags, int node)
1286{
1287 struct page *page;
Christoph Lameter81819f02007-05-06 14:49:36 -07001288 void *start;
Christoph Lameter81819f02007-05-06 14:49:36 -07001289 void *last;
1290 void *p;
1291
Christoph Lameter6cb06222007-10-16 01:25:41 -07001292 BUG_ON(flags & GFP_SLAB_BUG_MASK);
Christoph Lameter81819f02007-05-06 14:49:36 -07001293
Christoph Lameter6cb06222007-10-16 01:25:41 -07001294 page = allocate_slab(s,
1295 flags & (GFP_RECLAIM_MASK | GFP_CONSTRAINT_MASK), node);
Christoph Lameter81819f02007-05-06 14:49:36 -07001296 if (!page)
1297 goto out;
1298
Christoph Lameter205ab992008-04-14 19:11:40 +03001299 inc_slabs_node(s, page_to_nid(page), page->objects);
Christoph Lameter81819f02007-05-06 14:49:36 -07001300 page->slab = s;
1301 page->flags |= 1 << PG_slab;
Christoph Lameter81819f02007-05-06 14:49:36 -07001302
1303 start = page_address(page);
Christoph Lameter81819f02007-05-06 14:49:36 -07001304
1305 if (unlikely(s->flags & SLAB_POISON))
Christoph Lameter834f3d12008-04-14 19:11:31 +03001306 memset(start, POISON_INUSE, PAGE_SIZE << compound_order(page));
Christoph Lameter81819f02007-05-06 14:49:36 -07001307
1308 last = start;
Christoph Lameter224a88b2008-04-14 19:11:31 +03001309 for_each_object(p, s, start, page->objects) {
Christoph Lameter81819f02007-05-06 14:49:36 -07001310 setup_object(s, page, last);
1311 set_freepointer(s, last, p);
1312 last = p;
1313 }
1314 setup_object(s, page, last);
Christoph Lametera973e9d2008-03-01 13:40:44 -08001315 set_freepointer(s, last, NULL);
Christoph Lameter81819f02007-05-06 14:49:36 -07001316
1317 page->freelist = start;
1318 page->inuse = 0;
Christoph Lameter8cb0a502011-06-01 12:25:46 -05001319 page->frozen = 1;
Christoph Lameter81819f02007-05-06 14:49:36 -07001320out:
Christoph Lameter81819f02007-05-06 14:49:36 -07001321 return page;
1322}
1323
1324static void __free_slab(struct kmem_cache *s, struct page *page)
1325{
Christoph Lameter834f3d12008-04-14 19:11:31 +03001326 int order = compound_order(page);
1327 int pages = 1 << order;
Christoph Lameter81819f02007-05-06 14:49:36 -07001328
Christoph Lameteraf537b02010-07-09 14:07:14 -05001329 if (kmem_cache_debug(s)) {
Christoph Lameter81819f02007-05-06 14:49:36 -07001330 void *p;
1331
1332 slab_pad_check(s, page);
Christoph Lameter224a88b2008-04-14 19:11:31 +03001333 for_each_object(p, s, page_address(page),
1334 page->objects)
Christoph Lameterf7cb1932010-09-29 07:15:01 -05001335 check_object(s, page, p, SLUB_RED_INACTIVE);
Christoph Lameter81819f02007-05-06 14:49:36 -07001336 }
1337
Vegard Nossumb1eeab62008-11-25 16:55:53 +01001338 kmemcheck_free_shadow(page, compound_order(page));
Vegard Nossum5a896d92008-04-04 00:54:48 +02001339
Christoph Lameter81819f02007-05-06 14:49:36 -07001340 mod_zone_page_state(page_zone(page),
1341 (s->flags & SLAB_RECLAIM_ACCOUNT) ?
1342 NR_SLAB_RECLAIMABLE : NR_SLAB_UNRECLAIMABLE,
Pekka Enberg06428782008-01-07 23:20:27 -08001343 -pages);
Christoph Lameter81819f02007-05-06 14:49:36 -07001344
Christoph Lameter49bd5222008-04-14 18:52:18 +03001345 __ClearPageSlab(page);
1346 reset_page_mapcount(page);
Nick Piggin1eb5ac62009-05-05 19:13:44 +10001347 if (current->reclaim_state)
1348 current->reclaim_state->reclaimed_slab += pages;
Christoph Lameter834f3d12008-04-14 19:11:31 +03001349 __free_pages(page, order);
Christoph Lameter81819f02007-05-06 14:49:36 -07001350}
1351
Lai Jiangshanda9a6382011-03-10 15:22:00 +08001352#define need_reserve_slab_rcu \
1353 (sizeof(((struct page *)NULL)->lru) < sizeof(struct rcu_head))
1354
Christoph Lameter81819f02007-05-06 14:49:36 -07001355static void rcu_free_slab(struct rcu_head *h)
1356{
1357 struct page *page;
1358
Lai Jiangshanda9a6382011-03-10 15:22:00 +08001359 if (need_reserve_slab_rcu)
1360 page = virt_to_head_page(h);
1361 else
1362 page = container_of((struct list_head *)h, struct page, lru);
1363
Christoph Lameter81819f02007-05-06 14:49:36 -07001364 __free_slab(page->slab, page);
1365}
1366
1367static void free_slab(struct kmem_cache *s, struct page *page)
1368{
1369 if (unlikely(s->flags & SLAB_DESTROY_BY_RCU)) {
Lai Jiangshanda9a6382011-03-10 15:22:00 +08001370 struct rcu_head *head;
1371
1372 if (need_reserve_slab_rcu) {
1373 int order = compound_order(page);
1374 int offset = (PAGE_SIZE << order) - s->reserved;
1375
1376 VM_BUG_ON(s->reserved != sizeof(*head));
1377 head = page_address(page) + offset;
1378 } else {
1379 /*
1380 * RCU free overloads the RCU head over the LRU
1381 */
1382 head = (void *)&page->lru;
1383 }
Christoph Lameter81819f02007-05-06 14:49:36 -07001384
1385 call_rcu(head, rcu_free_slab);
1386 } else
1387 __free_slab(s, page);
1388}
1389
1390static void discard_slab(struct kmem_cache *s, struct page *page)
1391{
Christoph Lameter205ab992008-04-14 19:11:40 +03001392 dec_slabs_node(s, page_to_nid(page), page->objects);
Christoph Lameter81819f02007-05-06 14:49:36 -07001393 free_slab(s, page);
1394}
1395
1396/*
1397 * Per slab locking using the pagelock
1398 */
1399static __always_inline void slab_lock(struct page *page)
1400{
1401 bit_spin_lock(PG_locked, &page->flags);
1402}
1403
1404static __always_inline void slab_unlock(struct page *page)
1405{
Nick Piggina76d3542008-01-07 23:20:27 -08001406 __bit_spin_unlock(PG_locked, &page->flags);
Christoph Lameter81819f02007-05-06 14:49:36 -07001407}
1408
1409static __always_inline int slab_trylock(struct page *page)
1410{
1411 int rc = 1;
1412
1413 rc = bit_spin_trylock(PG_locked, &page->flags);
1414 return rc;
1415}
1416
1417/*
Christoph Lameter5cc6eee2011-06-01 12:25:50 -05001418 * Management of partially allocated slabs.
1419 *
1420 * list_lock must be held.
Christoph Lameter81819f02007-05-06 14:49:36 -07001421 */
Christoph Lameter5cc6eee2011-06-01 12:25:50 -05001422static inline void add_partial(struct kmem_cache_node *n,
Christoph Lameter7c2e1322008-01-07 23:20:27 -08001423 struct page *page, int tail)
Christoph Lameter81819f02007-05-06 14:49:36 -07001424{
Christoph Lametere95eed52007-05-06 14:49:44 -07001425 n->nr_partial++;
Christoph Lameter7c2e1322008-01-07 23:20:27 -08001426 if (tail)
1427 list_add_tail(&page->lru, &n->partial);
1428 else
1429 list_add(&page->lru, &n->partial);
Christoph Lameter81819f02007-05-06 14:49:36 -07001430}
1431
Christoph Lameter5cc6eee2011-06-01 12:25:50 -05001432/*
1433 * list_lock must be held.
1434 */
1435static inline void remove_partial(struct kmem_cache_node *n,
Christoph Lameter62e346a2010-09-28 08:10:28 -05001436 struct page *page)
1437{
1438 list_del(&page->lru);
1439 n->nr_partial--;
1440}
1441
Christoph Lameter81819f02007-05-06 14:49:36 -07001442/*
Christoph Lameter5cc6eee2011-06-01 12:25:50 -05001443 * Lock slab, remove from the partial list and put the object into the
1444 * per cpu freelist.
Christoph Lameter81819f02007-05-06 14:49:36 -07001445 *
Christoph Lameter672bba32007-05-09 02:32:39 -07001446 * Must hold list_lock.
Christoph Lameter81819f02007-05-06 14:49:36 -07001447 */
Christoph Lameter61728d12011-06-01 12:25:51 -05001448static inline int lock_and_freeze_slab(struct kmem_cache *s,
1449 struct kmem_cache_node *n, struct page *page)
Christoph Lameter81819f02007-05-06 14:49:36 -07001450{
Christoph Lameter2cfb7452011-06-01 12:25:52 -05001451 void *freelist;
1452 unsigned long counters;
1453 struct page new;
1454
1455
1456 if (!slab_trylock(page))
1457 return 0;
1458
1459 /*
1460 * Zap the freelist and set the frozen bit.
1461 * The old freelist is the list of objects for the
1462 * per cpu allocation list.
1463 */
1464 do {
1465 freelist = page->freelist;
1466 counters = page->counters;
1467 new.counters = counters;
1468 new.inuse = page->objects;
1469
1470 VM_BUG_ON(new.frozen);
1471 new.frozen = 1;
1472
1473 } while (!cmpxchg_double_slab(s, page,
1474 freelist, counters,
1475 NULL, new.counters,
1476 "lock and freeze"));
1477
1478 remove_partial(n, page);
1479
1480 if (freelist) {
1481 /* Populate the per cpu freelist */
1482 this_cpu_write(s->cpu_slab->freelist, freelist);
1483 this_cpu_write(s->cpu_slab->page, page);
1484 this_cpu_write(s->cpu_slab->node, page_to_nid(page));
Christoph Lameter81819f02007-05-06 14:49:36 -07001485 return 1;
Christoph Lameter2cfb7452011-06-01 12:25:52 -05001486 } else {
1487 /*
1488 * Slab page came from the wrong list. No object to allocate
1489 * from. Put it onto the correct list and continue partial
1490 * scan.
1491 */
1492 printk(KERN_ERR "SLUB: %s : Page without available objects on"
1493 " partial list\n", s->name);
1494 slab_unlock(page);
1495 return 0;
Christoph Lameter81819f02007-05-06 14:49:36 -07001496 }
Christoph Lameter81819f02007-05-06 14:49:36 -07001497}
1498
1499/*
Christoph Lameter672bba32007-05-09 02:32:39 -07001500 * Try to allocate a partial slab from a specific node.
Christoph Lameter81819f02007-05-06 14:49:36 -07001501 */
Christoph Lameter61728d12011-06-01 12:25:51 -05001502static struct page *get_partial_node(struct kmem_cache *s,
1503 struct kmem_cache_node *n)
Christoph Lameter81819f02007-05-06 14:49:36 -07001504{
1505 struct page *page;
1506
1507 /*
1508 * Racy check. If we mistakenly see no partial slabs then we
1509 * just allocate an empty slab. If we mistakenly try to get a
Christoph Lameter672bba32007-05-09 02:32:39 -07001510 * partial slab and there is none available then get_partials()
1511 * will return NULL.
Christoph Lameter81819f02007-05-06 14:49:36 -07001512 */
1513 if (!n || !n->nr_partial)
1514 return NULL;
1515
1516 spin_lock(&n->list_lock);
1517 list_for_each_entry(page, &n->partial, lru)
Christoph Lameter61728d12011-06-01 12:25:51 -05001518 if (lock_and_freeze_slab(s, n, page))
Christoph Lameter81819f02007-05-06 14:49:36 -07001519 goto out;
1520 page = NULL;
1521out:
1522 spin_unlock(&n->list_lock);
1523 return page;
1524}
1525
1526/*
Christoph Lameter672bba32007-05-09 02:32:39 -07001527 * Get a page from somewhere. Search in increasing NUMA distances.
Christoph Lameter81819f02007-05-06 14:49:36 -07001528 */
1529static struct page *get_any_partial(struct kmem_cache *s, gfp_t flags)
1530{
1531#ifdef CONFIG_NUMA
1532 struct zonelist *zonelist;
Mel Gormandd1a2392008-04-28 02:12:17 -07001533 struct zoneref *z;
Mel Gorman54a6eb52008-04-28 02:12:16 -07001534 struct zone *zone;
1535 enum zone_type high_zoneidx = gfp_zone(flags);
Christoph Lameter81819f02007-05-06 14:49:36 -07001536 struct page *page;
1537
1538 /*
Christoph Lameter672bba32007-05-09 02:32:39 -07001539 * The defrag ratio allows a configuration of the tradeoffs between
1540 * inter node defragmentation and node local allocations. A lower
1541 * defrag_ratio increases the tendency to do local allocations
1542 * instead of attempting to obtain partial slabs from other nodes.
Christoph Lameter81819f02007-05-06 14:49:36 -07001543 *
Christoph Lameter672bba32007-05-09 02:32:39 -07001544 * If the defrag_ratio is set to 0 then kmalloc() always
1545 * returns node local objects. If the ratio is higher then kmalloc()
1546 * may return off node objects because partial slabs are obtained
1547 * from other nodes and filled up.
Christoph Lameter81819f02007-05-06 14:49:36 -07001548 *
Christoph Lameter6446faa2008-02-15 23:45:26 -08001549 * If /sys/kernel/slab/xx/defrag_ratio is set to 100 (which makes
Christoph Lameter672bba32007-05-09 02:32:39 -07001550 * defrag_ratio = 1000) then every (well almost) allocation will
1551 * first attempt to defrag slab caches on other nodes. This means
1552 * scanning over all nodes to look for partial slabs which may be
1553 * expensive if we do it every time we are trying to find a slab
1554 * with available objects.
Christoph Lameter81819f02007-05-06 14:49:36 -07001555 */
Christoph Lameter98246012008-01-07 23:20:26 -08001556 if (!s->remote_node_defrag_ratio ||
1557 get_cycles() % 1024 > s->remote_node_defrag_ratio)
Christoph Lameter81819f02007-05-06 14:49:36 -07001558 return NULL;
1559
Miao Xiec0ff7452010-05-24 14:32:08 -07001560 get_mems_allowed();
Mel Gorman0e884602008-04-28 02:12:14 -07001561 zonelist = node_zonelist(slab_node(current->mempolicy), flags);
Mel Gorman54a6eb52008-04-28 02:12:16 -07001562 for_each_zone_zonelist(zone, z, zonelist, high_zoneidx) {
Christoph Lameter81819f02007-05-06 14:49:36 -07001563 struct kmem_cache_node *n;
1564
Mel Gorman54a6eb52008-04-28 02:12:16 -07001565 n = get_node(s, zone_to_nid(zone));
Christoph Lameter81819f02007-05-06 14:49:36 -07001566
Mel Gorman54a6eb52008-04-28 02:12:16 -07001567 if (n && cpuset_zone_allowed_hardwall(zone, flags) &&
David Rientjes3b89d7d2009-02-22 17:40:07 -08001568 n->nr_partial > s->min_partial) {
Christoph Lameter61728d12011-06-01 12:25:51 -05001569 page = get_partial_node(s, n);
Miao Xiec0ff7452010-05-24 14:32:08 -07001570 if (page) {
1571 put_mems_allowed();
Christoph Lameter81819f02007-05-06 14:49:36 -07001572 return page;
Miao Xiec0ff7452010-05-24 14:32:08 -07001573 }
Christoph Lameter81819f02007-05-06 14:49:36 -07001574 }
1575 }
Miao Xiec0ff7452010-05-24 14:32:08 -07001576 put_mems_allowed();
Christoph Lameter81819f02007-05-06 14:49:36 -07001577#endif
1578 return NULL;
1579}
1580
1581/*
1582 * Get a partial page, lock it and return it.
1583 */
1584static struct page *get_partial(struct kmem_cache *s, gfp_t flags, int node)
1585{
1586 struct page *page;
Christoph Lameter2154a332010-07-09 14:07:10 -05001587 int searchnode = (node == NUMA_NO_NODE) ? numa_node_id() : node;
Christoph Lameter81819f02007-05-06 14:49:36 -07001588
Christoph Lameter61728d12011-06-01 12:25:51 -05001589 page = get_partial_node(s, get_node(s, searchnode));
Christoph Lameter33de04e2011-04-15 14:48:12 -05001590 if (page || node != NUMA_NO_NODE)
Christoph Lameter81819f02007-05-06 14:49:36 -07001591 return page;
1592
1593 return get_any_partial(s, flags);
1594}
1595
Christoph Lameter8a5ec0b2011-02-25 11:38:54 -06001596#ifdef CONFIG_PREEMPT
1597/*
1598 * Calculate the next globally unique transaction for disambiguiation
1599 * during cmpxchg. The transactions start with the cpu number and are then
1600 * incremented by CONFIG_NR_CPUS.
1601 */
1602#define TID_STEP roundup_pow_of_two(CONFIG_NR_CPUS)
1603#else
1604/*
1605 * No preemption supported therefore also no need to check for
1606 * different cpus.
1607 */
1608#define TID_STEP 1
1609#endif
1610
1611static inline unsigned long next_tid(unsigned long tid)
1612{
1613 return tid + TID_STEP;
1614}
1615
1616static inline unsigned int tid_to_cpu(unsigned long tid)
1617{
1618 return tid % TID_STEP;
1619}
1620
1621static inline unsigned long tid_to_event(unsigned long tid)
1622{
1623 return tid / TID_STEP;
1624}
1625
1626static inline unsigned int init_tid(int cpu)
1627{
1628 return cpu;
1629}
1630
1631static inline void note_cmpxchg_failure(const char *n,
1632 const struct kmem_cache *s, unsigned long tid)
1633{
1634#ifdef SLUB_DEBUG_CMPXCHG
1635 unsigned long actual_tid = __this_cpu_read(s->cpu_slab->tid);
1636
1637 printk(KERN_INFO "%s %s: cmpxchg redo ", n, s->name);
1638
1639#ifdef CONFIG_PREEMPT
1640 if (tid_to_cpu(tid) != tid_to_cpu(actual_tid))
1641 printk("due to cpu change %d -> %d\n",
1642 tid_to_cpu(tid), tid_to_cpu(actual_tid));
1643 else
1644#endif
1645 if (tid_to_event(tid) != tid_to_event(actual_tid))
1646 printk("due to cpu running other code. Event %ld->%ld\n",
1647 tid_to_event(tid), tid_to_event(actual_tid));
1648 else
1649 printk("for unknown reason: actual=%lx was=%lx target=%lx\n",
1650 actual_tid, tid, next_tid(tid));
1651#endif
Christoph Lameter4fdccdf2011-03-22 13:35:00 -05001652 stat(s, CMPXCHG_DOUBLE_CPU_FAIL);
Christoph Lameter8a5ec0b2011-02-25 11:38:54 -06001653}
1654
Christoph Lameter8a5ec0b2011-02-25 11:38:54 -06001655void init_kmem_cache_cpus(struct kmem_cache *s)
1656{
Christoph Lameter8a5ec0b2011-02-25 11:38:54 -06001657 int cpu;
1658
1659 for_each_possible_cpu(cpu)
1660 per_cpu_ptr(s->cpu_slab, cpu)->tid = init_tid(cpu);
Christoph Lameter8a5ec0b2011-02-25 11:38:54 -06001661}
Christoph Lameter81819f02007-05-06 14:49:36 -07001662/*
1663 * Remove the cpu slab
1664 */
Christoph Lameter2cfb7452011-06-01 12:25:52 -05001665
1666/*
1667 * Remove the cpu slab
1668 */
Christoph Lameterdfb4f092007-10-16 01:26:05 -07001669static void deactivate_slab(struct kmem_cache *s, struct kmem_cache_cpu *c)
Christoph Lameter81819f02007-05-06 14:49:36 -07001670{
Christoph Lameter2cfb7452011-06-01 12:25:52 -05001671 enum slab_modes { M_NONE, M_PARTIAL, M_FULL, M_FREE };
Christoph Lameterdfb4f092007-10-16 01:26:05 -07001672 struct page *page = c->page;
Christoph Lameter2cfb7452011-06-01 12:25:52 -05001673 struct kmem_cache_node *n = get_node(s, page_to_nid(page));
1674 int lock = 0;
1675 enum slab_modes l = M_NONE, m = M_NONE;
1676 void *freelist;
1677 void *nextfree;
1678 int tail = 0;
1679 struct page new;
1680 struct page old;
Christoph Lameter8ff12cf2008-02-07 17:47:41 -08001681
Christoph Lameter2cfb7452011-06-01 12:25:52 -05001682 if (page->freelist) {
Christoph Lameter84e554e2009-12-18 16:26:23 -06001683 stat(s, DEACTIVATE_REMOTE_FREES);
Christoph Lameter2cfb7452011-06-01 12:25:52 -05001684 tail = 1;
Christoph Lameter894b8782007-05-10 03:15:16 -07001685 }
Christoph Lameter2cfb7452011-06-01 12:25:52 -05001686
Christoph Lameter8a5ec0b2011-02-25 11:38:54 -06001687 c->tid = next_tid(c->tid);
Christoph Lameter2cfb7452011-06-01 12:25:52 -05001688 c->page = NULL;
1689 freelist = c->freelist;
1690 c->freelist = NULL;
1691
1692 /*
1693 * Stage one: Free all available per cpu objects back
1694 * to the page freelist while it is still frozen. Leave the
1695 * last one.
1696 *
1697 * There is no need to take the list->lock because the page
1698 * is still frozen.
1699 */
1700 while (freelist && (nextfree = get_freepointer(s, freelist))) {
1701 void *prior;
1702 unsigned long counters;
1703
1704 do {
1705 prior = page->freelist;
1706 counters = page->counters;
1707 set_freepointer(s, freelist, prior);
1708 new.counters = counters;
1709 new.inuse--;
1710 VM_BUG_ON(!new.frozen);
1711
1712 } while (!cmpxchg_double_slab(s, page,
1713 prior, counters,
1714 freelist, new.counters,
1715 "drain percpu freelist"));
1716
1717 freelist = nextfree;
1718 }
1719
1720 /*
1721 * Stage two: Ensure that the page is unfrozen while the
1722 * list presence reflects the actual number of objects
1723 * during unfreeze.
1724 *
1725 * We setup the list membership and then perform a cmpxchg
1726 * with the count. If there is a mismatch then the page
1727 * is not unfrozen but the page is on the wrong list.
1728 *
1729 * Then we restart the process which may have to remove
1730 * the page from the list that we just put it on again
1731 * because the number of objects in the slab may have
1732 * changed.
1733 */
1734redo:
1735
1736 old.freelist = page->freelist;
1737 old.counters = page->counters;
1738 VM_BUG_ON(!old.frozen);
1739
1740 /* Determine target state of the slab */
1741 new.counters = old.counters;
1742 if (freelist) {
1743 new.inuse--;
1744 set_freepointer(s, freelist, old.freelist);
1745 new.freelist = freelist;
1746 } else
1747 new.freelist = old.freelist;
1748
1749 new.frozen = 0;
1750
1751 if (!new.inuse && n->nr_partial < s->min_partial)
1752 m = M_FREE;
1753 else if (new.freelist) {
1754 m = M_PARTIAL;
1755 if (!lock) {
1756 lock = 1;
1757 /*
1758 * Taking the spinlock removes the possiblity
1759 * that acquire_slab() will see a slab page that
1760 * is frozen
1761 */
1762 spin_lock(&n->list_lock);
1763 }
1764 } else {
1765 m = M_FULL;
1766 if (kmem_cache_debug(s) && !lock) {
1767 lock = 1;
1768 /*
1769 * This also ensures that the scanning of full
1770 * slabs from diagnostic functions will not see
1771 * any frozen slabs.
1772 */
1773 spin_lock(&n->list_lock);
1774 }
1775 }
1776
1777 if (l != m) {
1778
1779 if (l == M_PARTIAL)
1780
1781 remove_partial(n, page);
1782
1783 else if (l == M_FULL)
1784
1785 remove_full(s, page);
1786
1787 if (m == M_PARTIAL) {
1788
1789 add_partial(n, page, tail);
1790 stat(s, tail ? DEACTIVATE_TO_TAIL : DEACTIVATE_TO_HEAD);
1791
1792 } else if (m == M_FULL) {
1793
1794 stat(s, DEACTIVATE_FULL);
1795 add_full(s, n, page);
1796
1797 }
1798 }
1799
1800 l = m;
1801 if (!cmpxchg_double_slab(s, page,
1802 old.freelist, old.counters,
1803 new.freelist, new.counters,
1804 "unfreezing slab"))
1805 goto redo;
1806
1807 slab_unlock(page);
1808
1809 if (lock)
1810 spin_unlock(&n->list_lock);
1811
1812 if (m == M_FREE) {
1813 stat(s, DEACTIVATE_EMPTY);
1814 discard_slab(s, page);
1815 stat(s, FREE_SLAB);
1816 }
Christoph Lameter81819f02007-05-06 14:49:36 -07001817}
1818
Christoph Lameterdfb4f092007-10-16 01:26:05 -07001819static inline void flush_slab(struct kmem_cache *s, struct kmem_cache_cpu *c)
Christoph Lameter81819f02007-05-06 14:49:36 -07001820{
Christoph Lameter84e554e2009-12-18 16:26:23 -06001821 stat(s, CPUSLAB_FLUSH);
Christoph Lameterdfb4f092007-10-16 01:26:05 -07001822 slab_lock(c->page);
1823 deactivate_slab(s, c);
Christoph Lameter81819f02007-05-06 14:49:36 -07001824}
1825
1826/*
1827 * Flush cpu slab.
Christoph Lameter6446faa2008-02-15 23:45:26 -08001828 *
Christoph Lameter81819f02007-05-06 14:49:36 -07001829 * Called from IPI handler with interrupts disabled.
1830 */
Christoph Lameter0c710012007-07-17 04:03:24 -07001831static inline void __flush_cpu_slab(struct kmem_cache *s, int cpu)
Christoph Lameter81819f02007-05-06 14:49:36 -07001832{
Christoph Lameter9dfc6e62009-12-18 16:26:20 -06001833 struct kmem_cache_cpu *c = per_cpu_ptr(s->cpu_slab, cpu);
Christoph Lameter81819f02007-05-06 14:49:36 -07001834
Christoph Lameterdfb4f092007-10-16 01:26:05 -07001835 if (likely(c && c->page))
1836 flush_slab(s, c);
Christoph Lameter81819f02007-05-06 14:49:36 -07001837}
1838
1839static void flush_cpu_slab(void *d)
1840{
1841 struct kmem_cache *s = d;
Christoph Lameter81819f02007-05-06 14:49:36 -07001842
Christoph Lameterdfb4f092007-10-16 01:26:05 -07001843 __flush_cpu_slab(s, smp_processor_id());
Christoph Lameter81819f02007-05-06 14:49:36 -07001844}
1845
1846static void flush_all(struct kmem_cache *s)
1847{
Jens Axboe15c8b6c2008-05-09 09:39:44 +02001848 on_each_cpu(flush_cpu_slab, s, 1);
Christoph Lameter81819f02007-05-06 14:49:36 -07001849}
1850
1851/*
Christoph Lameterdfb4f092007-10-16 01:26:05 -07001852 * Check if the objects in a per cpu structure fit numa
1853 * locality expectations.
1854 */
1855static inline int node_match(struct kmem_cache_cpu *c, int node)
1856{
1857#ifdef CONFIG_NUMA
Christoph Lameter2154a332010-07-09 14:07:10 -05001858 if (node != NUMA_NO_NODE && c->node != node)
Christoph Lameterdfb4f092007-10-16 01:26:05 -07001859 return 0;
1860#endif
1861 return 1;
1862}
1863
Pekka Enberg781b2ba2009-06-10 18:50:32 +03001864static int count_free(struct page *page)
1865{
1866 return page->objects - page->inuse;
1867}
1868
1869static unsigned long count_partial(struct kmem_cache_node *n,
1870 int (*get_count)(struct page *))
1871{
1872 unsigned long flags;
1873 unsigned long x = 0;
1874 struct page *page;
1875
1876 spin_lock_irqsave(&n->list_lock, flags);
1877 list_for_each_entry(page, &n->partial, lru)
1878 x += get_count(page);
1879 spin_unlock_irqrestore(&n->list_lock, flags);
1880 return x;
1881}
1882
Alexander Beregalov26c02cf2009-06-11 14:08:48 +04001883static inline unsigned long node_nr_objs(struct kmem_cache_node *n)
1884{
1885#ifdef CONFIG_SLUB_DEBUG
1886 return atomic_long_read(&n->total_objects);
1887#else
1888 return 0;
1889#endif
1890}
1891
Pekka Enberg781b2ba2009-06-10 18:50:32 +03001892static noinline void
1893slab_out_of_memory(struct kmem_cache *s, gfp_t gfpflags, int nid)
1894{
1895 int node;
1896
1897 printk(KERN_WARNING
1898 "SLUB: Unable to allocate memory on node %d (gfp=0x%x)\n",
1899 nid, gfpflags);
1900 printk(KERN_WARNING " cache: %s, object size: %d, buffer size: %d, "
1901 "default order: %d, min order: %d\n", s->name, s->objsize,
1902 s->size, oo_order(s->oo), oo_order(s->min));
1903
David Rientjesfa5ec8a2009-07-07 00:14:14 -07001904 if (oo_order(s->min) > get_order(s->objsize))
1905 printk(KERN_WARNING " %s debugging increased min order, use "
1906 "slub_debug=O to disable.\n", s->name);
1907
Pekka Enberg781b2ba2009-06-10 18:50:32 +03001908 for_each_online_node(node) {
1909 struct kmem_cache_node *n = get_node(s, node);
1910 unsigned long nr_slabs;
1911 unsigned long nr_objs;
1912 unsigned long nr_free;
1913
1914 if (!n)
1915 continue;
1916
Alexander Beregalov26c02cf2009-06-11 14:08:48 +04001917 nr_free = count_partial(n, count_free);
1918 nr_slabs = node_nr_slabs(n);
1919 nr_objs = node_nr_objs(n);
Pekka Enberg781b2ba2009-06-10 18:50:32 +03001920
1921 printk(KERN_WARNING
1922 " node %d: slabs: %ld, objs: %ld, free: %ld\n",
1923 node, nr_slabs, nr_objs, nr_free);
1924 }
1925}
1926
Christoph Lameterdfb4f092007-10-16 01:26:05 -07001927/*
Christoph Lameter894b8782007-05-10 03:15:16 -07001928 * Slow path. The lockless freelist is empty or we need to perform
1929 * debugging duties.
Christoph Lameter81819f02007-05-06 14:49:36 -07001930 *
Christoph Lameter894b8782007-05-10 03:15:16 -07001931 * Interrupts are disabled.
Christoph Lameter81819f02007-05-06 14:49:36 -07001932 *
Christoph Lameter894b8782007-05-10 03:15:16 -07001933 * Processing is still very fast if new objects have been freed to the
1934 * regular freelist. In that case we simply take over the regular freelist
1935 * as the lockless freelist and zap the regular freelist.
Christoph Lameter81819f02007-05-06 14:49:36 -07001936 *
Christoph Lameter894b8782007-05-10 03:15:16 -07001937 * If that is not working then we fall back to the partial lists. We take the
1938 * first element of the freelist as the object to allocate now and move the
1939 * rest of the freelist to the lockless freelist.
1940 *
1941 * And if we were unable to get a new slab from the partial slab lists then
Christoph Lameter6446faa2008-02-15 23:45:26 -08001942 * we need to allocate a new slab. This is the slowest path since it involves
1943 * a call to the page allocator and the setup of a new slab.
Christoph Lameter81819f02007-05-06 14:49:36 -07001944 */
Eduard - Gabriel Munteanuce71e272008-08-19 20:43:25 +03001945static void *__slab_alloc(struct kmem_cache *s, gfp_t gfpflags, int node,
1946 unsigned long addr, struct kmem_cache_cpu *c)
Christoph Lameter81819f02007-05-06 14:49:36 -07001947{
Christoph Lameter81819f02007-05-06 14:49:36 -07001948 void **object;
Christoph Lameter01ad8a72011-04-15 14:48:14 -05001949 struct page *page;
Christoph Lameter8a5ec0b2011-02-25 11:38:54 -06001950 unsigned long flags;
Christoph Lameter2cfb7452011-06-01 12:25:52 -05001951 struct page new;
1952 unsigned long counters;
Christoph Lameter8a5ec0b2011-02-25 11:38:54 -06001953
1954 local_irq_save(flags);
1955#ifdef CONFIG_PREEMPT
1956 /*
1957 * We may have been preempted and rescheduled on a different
1958 * cpu before disabling interrupts. Need to reload cpu area
1959 * pointer.
1960 */
1961 c = this_cpu_ptr(s->cpu_slab);
1962#endif
Christoph Lameter81819f02007-05-06 14:49:36 -07001963
Linus Torvaldse72e9c22008-03-27 20:56:33 -07001964 /* We handle __GFP_ZERO in the caller */
1965 gfpflags &= ~__GFP_ZERO;
1966
Christoph Lameter01ad8a72011-04-15 14:48:14 -05001967 page = c->page;
1968 if (!page)
Christoph Lameter81819f02007-05-06 14:49:36 -07001969 goto new_slab;
1970
Christoph Lameter01ad8a72011-04-15 14:48:14 -05001971 slab_lock(page);
Christoph Lameterdfb4f092007-10-16 01:26:05 -07001972 if (unlikely(!node_match(c, node)))
Christoph Lameter81819f02007-05-06 14:49:36 -07001973 goto another_slab;
Christoph Lameter6446faa2008-02-15 23:45:26 -08001974
Christoph Lameter2cfb7452011-06-01 12:25:52 -05001975 stat(s, ALLOC_SLOWPATH);
1976
1977 do {
1978 object = page->freelist;
1979 counters = page->counters;
1980 new.counters = counters;
1981 new.inuse = page->objects;
1982 VM_BUG_ON(!new.frozen);
1983
1984 } while (!cmpxchg_double_slab(s, page,
1985 object, counters,
1986 NULL, new.counters,
1987 "__slab_alloc"));
Christoph Lameter6446faa2008-02-15 23:45:26 -08001988
Christoph Lameter894b8782007-05-10 03:15:16 -07001989load_freelist:
Christoph Lameter8cb0a502011-06-01 12:25:46 -05001990 VM_BUG_ON(!page->frozen);
1991
Christoph Lametera973e9d2008-03-01 13:40:44 -08001992 if (unlikely(!object))
Christoph Lameter81819f02007-05-06 14:49:36 -07001993 goto another_slab;
Christoph Lameter81819f02007-05-06 14:49:36 -07001994
Christoph Lameter2cfb7452011-06-01 12:25:52 -05001995 stat(s, ALLOC_REFILL);
Christoph Lameter01ad8a72011-04-15 14:48:14 -05001996
Christoph Lameter01ad8a72011-04-15 14:48:14 -05001997 slab_unlock(page);
Christoph Lameter2cfb7452011-06-01 12:25:52 -05001998
1999 c->freelist = get_freepointer(s, object);
Christoph Lameter8a5ec0b2011-02-25 11:38:54 -06002000 c->tid = next_tid(c->tid);
2001 local_irq_restore(flags);
Christoph Lameter81819f02007-05-06 14:49:36 -07002002 return object;
2003
2004another_slab:
Christoph Lameterdfb4f092007-10-16 01:26:05 -07002005 deactivate_slab(s, c);
Christoph Lameter81819f02007-05-06 14:49:36 -07002006
2007new_slab:
Christoph Lameter01ad8a72011-04-15 14:48:14 -05002008 page = get_partial(s, gfpflags, node);
2009 if (page) {
Christoph Lameter84e554e2009-12-18 16:26:23 -06002010 stat(s, ALLOC_FROM_PARTIAL);
Christoph Lameter2cfb7452011-06-01 12:25:52 -05002011 object = c->freelist;
2012
2013 if (kmem_cache_debug(s))
2014 goto debug;
Christoph Lameter894b8782007-05-10 03:15:16 -07002015 goto load_freelist;
Christoph Lameter81819f02007-05-06 14:49:36 -07002016 }
2017
Christoph Lameter01ad8a72011-04-15 14:48:14 -05002018 page = new_slab(s, gfpflags, node);
Christoph Lameterb811c202007-10-16 23:25:51 -07002019
Christoph Lameter01ad8a72011-04-15 14:48:14 -05002020 if (page) {
Christoph Lameter9dfc6e62009-12-18 16:26:20 -06002021 c = __this_cpu_ptr(s->cpu_slab);
Christoph Lameter05aa3452007-11-05 11:31:58 -08002022 if (c->page)
Christoph Lameterdfb4f092007-10-16 01:26:05 -07002023 flush_slab(s, c);
Christoph Lameter01ad8a72011-04-15 14:48:14 -05002024
Christoph Lameter2cfb7452011-06-01 12:25:52 -05002025 /*
2026 * No other reference to the page yet so we can
2027 * muck around with it freely without cmpxchg
2028 */
2029 object = page->freelist;
2030 page->freelist = NULL;
2031 page->inuse = page->objects;
2032
2033 stat(s, ALLOC_SLAB);
Christoph Lameter01ad8a72011-04-15 14:48:14 -05002034 slab_lock(page);
David Rientjesbd07d872011-05-12 13:10:49 -07002035 c->node = page_to_nid(page);
2036 c->page = page;
Christoph Lameter4b6f0752007-05-16 22:10:53 -07002037 goto load_freelist;
Christoph Lameter81819f02007-05-06 14:49:36 -07002038 }
Pekka Enberg95f85982009-06-11 16:18:09 +03002039 if (!(gfpflags & __GFP_NOWARN) && printk_ratelimit())
2040 slab_out_of_memory(s, gfpflags, node);
Christoph Lameter2fd66c52011-03-22 13:32:53 -05002041 local_irq_restore(flags);
Christoph Lameter71c7a062008-02-14 14:28:01 -08002042 return NULL;
Christoph Lameter894b8782007-05-10 03:15:16 -07002043
Christoph Lameter2cfb7452011-06-01 12:25:52 -05002044debug:
2045 if (!object || !alloc_debug_processing(s, page, object, addr))
2046 goto new_slab;
2047
2048 c->freelist = get_freepointer(s, object);
Christoph Lameter442b06b2011-05-17 16:29:31 -05002049 deactivate_slab(s, c);
2050 c->page = NULL;
Pekka Enberg15b7c512010-10-02 11:32:32 +03002051 c->node = NUMA_NO_NODE;
Christoph Lametera71ae472011-05-25 09:47:43 -05002052 local_irq_restore(flags);
2053 return object;
Christoph Lameter894b8782007-05-10 03:15:16 -07002054}
2055
2056/*
2057 * Inlined fastpath so that allocation functions (kmalloc, kmem_cache_alloc)
2058 * have the fastpath folded into their functions. So no function call
2059 * overhead for requests that can be satisfied on the fastpath.
2060 *
2061 * The fastpath works by first checking if the lockless freelist can be used.
2062 * If not then __slab_alloc is called for slow processing.
2063 *
2064 * Otherwise we can simply pick the next object from the lockless free list.
2065 */
Pekka Enberg06428782008-01-07 23:20:27 -08002066static __always_inline void *slab_alloc(struct kmem_cache *s,
Eduard - Gabriel Munteanuce71e272008-08-19 20:43:25 +03002067 gfp_t gfpflags, int node, unsigned long addr)
Christoph Lameter894b8782007-05-10 03:15:16 -07002068{
Christoph Lameter894b8782007-05-10 03:15:16 -07002069 void **object;
Christoph Lameterdfb4f092007-10-16 01:26:05 -07002070 struct kmem_cache_cpu *c;
Christoph Lameter8a5ec0b2011-02-25 11:38:54 -06002071 unsigned long tid;
Christoph Lameter1f842602008-01-07 23:20:30 -08002072
Christoph Lameterc016b0b2010-08-20 12:37:16 -05002073 if (slab_pre_alloc_hook(s, gfpflags))
Akinobu Mita773ff602008-12-23 19:37:01 +09002074 return NULL;
2075
Christoph Lameter8a5ec0b2011-02-25 11:38:54 -06002076redo:
Christoph Lameter8a5ec0b2011-02-25 11:38:54 -06002077
2078 /*
2079 * Must read kmem_cache cpu data via this cpu ptr. Preemption is
2080 * enabled. We may switch back and forth between cpus while
2081 * reading from one cpu area. That does not matter as long
2082 * as we end up on the original cpu again when doing the cmpxchg.
2083 */
Christoph Lameter9dfc6e62009-12-18 16:26:20 -06002084 c = __this_cpu_ptr(s->cpu_slab);
Christoph Lameter8a5ec0b2011-02-25 11:38:54 -06002085
Christoph Lameter8a5ec0b2011-02-25 11:38:54 -06002086 /*
2087 * The transaction ids are globally unique per cpu and per operation on
2088 * a per cpu queue. Thus they can be guarantee that the cmpxchg_double
2089 * occurs on the right processor and that there was no operation on the
2090 * linked list in between.
2091 */
2092 tid = c->tid;
2093 barrier();
Christoph Lameter8a5ec0b2011-02-25 11:38:54 -06002094
Christoph Lameter9dfc6e62009-12-18 16:26:20 -06002095 object = c->freelist;
Christoph Lameter9dfc6e62009-12-18 16:26:20 -06002096 if (unlikely(!object || !node_match(c, node)))
Christoph Lameter894b8782007-05-10 03:15:16 -07002097
Christoph Lameterdfb4f092007-10-16 01:26:05 -07002098 object = __slab_alloc(s, gfpflags, node, addr, c);
Christoph Lameter894b8782007-05-10 03:15:16 -07002099
2100 else {
Christoph Lameter8a5ec0b2011-02-25 11:38:54 -06002101 /*
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002102 * The cmpxchg will only match if there was no additional
Christoph Lameter8a5ec0b2011-02-25 11:38:54 -06002103 * operation and if we are on the right processor.
2104 *
2105 * The cmpxchg does the following atomically (without lock semantics!)
2106 * 1. Relocate first pointer to the current per cpu area.
2107 * 2. Verify that tid and freelist have not been changed
2108 * 3. If they were not changed replace tid and freelist
2109 *
2110 * Since this is without lock semantics the protection is only against
2111 * code executing on this cpu *not* from access by other cpus.
2112 */
Thomas Gleixner30106b82011-05-04 15:38:19 +02002113 if (unlikely(!irqsafe_cpu_cmpxchg_double(
Christoph Lameter8a5ec0b2011-02-25 11:38:54 -06002114 s->cpu_slab->freelist, s->cpu_slab->tid,
2115 object, tid,
Christoph Lameter1393d9a2011-05-16 15:26:08 -05002116 get_freepointer_safe(s, object), next_tid(tid)))) {
Christoph Lameter8a5ec0b2011-02-25 11:38:54 -06002117
2118 note_cmpxchg_failure("slab_alloc", s, tid);
2119 goto redo;
2120 }
Christoph Lameter84e554e2009-12-18 16:26:23 -06002121 stat(s, ALLOC_FASTPATH);
Christoph Lameter894b8782007-05-10 03:15:16 -07002122 }
Christoph Lameter8a5ec0b2011-02-25 11:38:54 -06002123
Pekka Enberg74e21342009-11-25 20:14:48 +02002124 if (unlikely(gfpflags & __GFP_ZERO) && object)
Christoph Lameterff120592009-12-18 16:26:22 -06002125 memset(object, 0, s->objsize);
Christoph Lameterd07dbea2007-07-17 04:03:23 -07002126
Christoph Lameterc016b0b2010-08-20 12:37:16 -05002127 slab_post_alloc_hook(s, gfpflags, object);
Vegard Nossum5a896d92008-04-04 00:54:48 +02002128
Christoph Lameter894b8782007-05-10 03:15:16 -07002129 return object;
Christoph Lameter81819f02007-05-06 14:49:36 -07002130}
2131
2132void *kmem_cache_alloc(struct kmem_cache *s, gfp_t gfpflags)
2133{
Christoph Lameter2154a332010-07-09 14:07:10 -05002134 void *ret = slab_alloc(s, gfpflags, NUMA_NO_NODE, _RET_IP_);
Eduard - Gabriel Munteanu5b882be2008-08-19 20:43:26 +03002135
Eduard - Gabriel Munteanuca2b84c2009-03-23 15:12:24 +02002136 trace_kmem_cache_alloc(_RET_IP_, ret, s->objsize, s->size, gfpflags);
Eduard - Gabriel Munteanu5b882be2008-08-19 20:43:26 +03002137
2138 return ret;
Christoph Lameter81819f02007-05-06 14:49:36 -07002139}
2140EXPORT_SYMBOL(kmem_cache_alloc);
2141
Li Zefan0f24f122009-12-11 15:45:30 +08002142#ifdef CONFIG_TRACING
Richard Kennedy4a923792010-10-21 10:29:19 +01002143void *kmem_cache_alloc_trace(struct kmem_cache *s, gfp_t gfpflags, size_t size)
Eduard - Gabriel Munteanu5b882be2008-08-19 20:43:26 +03002144{
Richard Kennedy4a923792010-10-21 10:29:19 +01002145 void *ret = slab_alloc(s, gfpflags, NUMA_NO_NODE, _RET_IP_);
2146 trace_kmalloc(_RET_IP_, ret, size, s->size, gfpflags);
2147 return ret;
Eduard - Gabriel Munteanu5b882be2008-08-19 20:43:26 +03002148}
Richard Kennedy4a923792010-10-21 10:29:19 +01002149EXPORT_SYMBOL(kmem_cache_alloc_trace);
2150
2151void *kmalloc_order_trace(size_t size, gfp_t flags, unsigned int order)
2152{
2153 void *ret = kmalloc_order(size, flags, order);
2154 trace_kmalloc(_RET_IP_, ret, size, PAGE_SIZE << order, flags);
2155 return ret;
2156}
2157EXPORT_SYMBOL(kmalloc_order_trace);
Eduard - Gabriel Munteanu5b882be2008-08-19 20:43:26 +03002158#endif
2159
Christoph Lameter81819f02007-05-06 14:49:36 -07002160#ifdef CONFIG_NUMA
2161void *kmem_cache_alloc_node(struct kmem_cache *s, gfp_t gfpflags, int node)
2162{
Eduard - Gabriel Munteanu5b882be2008-08-19 20:43:26 +03002163 void *ret = slab_alloc(s, gfpflags, node, _RET_IP_);
2164
Eduard - Gabriel Munteanuca2b84c2009-03-23 15:12:24 +02002165 trace_kmem_cache_alloc_node(_RET_IP_, ret,
2166 s->objsize, s->size, gfpflags, node);
Eduard - Gabriel Munteanu5b882be2008-08-19 20:43:26 +03002167
2168 return ret;
Christoph Lameter81819f02007-05-06 14:49:36 -07002169}
2170EXPORT_SYMBOL(kmem_cache_alloc_node);
Christoph Lameter81819f02007-05-06 14:49:36 -07002171
Li Zefan0f24f122009-12-11 15:45:30 +08002172#ifdef CONFIG_TRACING
Richard Kennedy4a923792010-10-21 10:29:19 +01002173void *kmem_cache_alloc_node_trace(struct kmem_cache *s,
Eduard - Gabriel Munteanu5b882be2008-08-19 20:43:26 +03002174 gfp_t gfpflags,
Richard Kennedy4a923792010-10-21 10:29:19 +01002175 int node, size_t size)
Eduard - Gabriel Munteanu5b882be2008-08-19 20:43:26 +03002176{
Richard Kennedy4a923792010-10-21 10:29:19 +01002177 void *ret = slab_alloc(s, gfpflags, node, _RET_IP_);
2178
2179 trace_kmalloc_node(_RET_IP_, ret,
2180 size, s->size, gfpflags, node);
2181 return ret;
Eduard - Gabriel Munteanu5b882be2008-08-19 20:43:26 +03002182}
Richard Kennedy4a923792010-10-21 10:29:19 +01002183EXPORT_SYMBOL(kmem_cache_alloc_node_trace);
Eduard - Gabriel Munteanu5b882be2008-08-19 20:43:26 +03002184#endif
Namhyung Kim5d1f57e2010-09-29 21:02:15 +09002185#endif
Eduard - Gabriel Munteanu5b882be2008-08-19 20:43:26 +03002186
Christoph Lameter81819f02007-05-06 14:49:36 -07002187/*
Christoph Lameter894b8782007-05-10 03:15:16 -07002188 * Slow patch handling. This may still be called frequently since objects
2189 * have a longer lifetime than the cpu slabs in most processing loads.
Christoph Lameter81819f02007-05-06 14:49:36 -07002190 *
Christoph Lameter894b8782007-05-10 03:15:16 -07002191 * So we still attempt to reduce cache line usage. Just take the slab
2192 * lock and free the item. If there is no additional partial page
2193 * handling required then we can return immediately.
Christoph Lameter81819f02007-05-06 14:49:36 -07002194 */
Christoph Lameter894b8782007-05-10 03:15:16 -07002195static void __slab_free(struct kmem_cache *s, struct page *page,
Christoph Lameterff120592009-12-18 16:26:22 -06002196 void *x, unsigned long addr)
Christoph Lameter81819f02007-05-06 14:49:36 -07002197{
2198 void *prior;
2199 void **object = (void *)x;
Christoph Lameter2cfb7452011-06-01 12:25:52 -05002200 int was_frozen;
2201 int inuse;
2202 struct page new;
2203 unsigned long counters;
2204 struct kmem_cache_node *n = NULL;
Christoph Lameter61728d12011-06-01 12:25:51 -05002205 unsigned long uninitialized_var(flags);
Christoph Lameter81819f02007-05-06 14:49:36 -07002206
Christoph Lameter8a5ec0b2011-02-25 11:38:54 -06002207 local_irq_save(flags);
Christoph Lameter81819f02007-05-06 14:49:36 -07002208 slab_lock(page);
Christoph Lameter8a5ec0b2011-02-25 11:38:54 -06002209 stat(s, FREE_SLOWPATH);
Christoph Lameter81819f02007-05-06 14:49:36 -07002210
Christoph Lameter8dc16c62011-04-15 14:48:16 -05002211 if (kmem_cache_debug(s) && !free_debug_processing(s, page, x, addr))
2212 goto out_unlock;
Christoph Lameter6446faa2008-02-15 23:45:26 -08002213
Christoph Lameter2cfb7452011-06-01 12:25:52 -05002214 do {
2215 prior = page->freelist;
2216 counters = page->counters;
2217 set_freepointer(s, object, prior);
2218 new.counters = counters;
2219 was_frozen = new.frozen;
2220 new.inuse--;
2221 if ((!new.inuse || !prior) && !was_frozen && !n) {
2222 n = get_node(s, page_to_nid(page));
2223 /*
2224 * Speculatively acquire the list_lock.
2225 * If the cmpxchg does not succeed then we may
2226 * drop the list_lock without any processing.
2227 *
2228 * Otherwise the list_lock will synchronize with
2229 * other processors updating the list of slabs.
2230 */
2231 spin_lock(&n->list_lock);
2232 }
2233 inuse = new.inuse;
Christoph Lameter81819f02007-05-06 14:49:36 -07002234
Christoph Lameter2cfb7452011-06-01 12:25:52 -05002235 } while (!cmpxchg_double_slab(s, page,
2236 prior, counters,
2237 object, new.counters,
2238 "__slab_free"));
Christoph Lameter81819f02007-05-06 14:49:36 -07002239
Christoph Lameter2cfb7452011-06-01 12:25:52 -05002240 if (likely(!n)) {
2241 /*
2242 * The list lock was not taken therefore no list
2243 * activity can be necessary.
2244 */
2245 if (was_frozen)
2246 stat(s, FREE_FROZEN);
2247 goto out_unlock;
2248 }
Christoph Lameter81819f02007-05-06 14:49:36 -07002249
2250 /*
Christoph Lameter2cfb7452011-06-01 12:25:52 -05002251 * was_frozen may have been set after we acquired the list_lock in
2252 * an earlier loop. So we need to check it here again.
Christoph Lameter81819f02007-05-06 14:49:36 -07002253 */
Christoph Lameter2cfb7452011-06-01 12:25:52 -05002254 if (was_frozen)
2255 stat(s, FREE_FROZEN);
2256 else {
2257 if (unlikely(!inuse && n->nr_partial > s->min_partial))
2258 goto slab_empty;
Christoph Lameter5cc6eee2011-06-01 12:25:50 -05002259
Christoph Lameter2cfb7452011-06-01 12:25:52 -05002260 /*
2261 * Objects left in the slab. If it was not on the partial list before
2262 * then add it.
2263 */
2264 if (unlikely(!prior)) {
2265 remove_full(s, page);
2266 add_partial(n, page, 0);
2267 stat(s, FREE_ADD_PARTIAL);
2268 }
Christoph Lameter8ff12cf2008-02-07 17:47:41 -08002269 }
Christoph Lameter81819f02007-05-06 14:49:36 -07002270
Christoph Lameter2cfb7452011-06-01 12:25:52 -05002271 spin_unlock(&n->list_lock);
2272
Christoph Lameter81819f02007-05-06 14:49:36 -07002273out_unlock:
2274 slab_unlock(page);
Christoph Lameter8a5ec0b2011-02-25 11:38:54 -06002275 local_irq_restore(flags);
Christoph Lameter81819f02007-05-06 14:49:36 -07002276 return;
2277
2278slab_empty:
Christoph Lametera973e9d2008-03-01 13:40:44 -08002279 if (prior) {
Christoph Lameter81819f02007-05-06 14:49:36 -07002280 /*
Christoph Lameter672bba32007-05-09 02:32:39 -07002281 * Slab still on the partial list.
Christoph Lameter81819f02007-05-06 14:49:36 -07002282 */
Christoph Lameter5cc6eee2011-06-01 12:25:50 -05002283 remove_partial(n, page);
Christoph Lameter84e554e2009-12-18 16:26:23 -06002284 stat(s, FREE_REMOVE_PARTIAL);
Christoph Lameter8ff12cf2008-02-07 17:47:41 -08002285 }
Christoph Lameter2cfb7452011-06-01 12:25:52 -05002286
2287 spin_unlock(&n->list_lock);
Christoph Lameter81819f02007-05-06 14:49:36 -07002288 slab_unlock(page);
Christoph Lameter8a5ec0b2011-02-25 11:38:54 -06002289 local_irq_restore(flags);
Christoph Lameter84e554e2009-12-18 16:26:23 -06002290 stat(s, FREE_SLAB);
Christoph Lameter81819f02007-05-06 14:49:36 -07002291 discard_slab(s, page);
Christoph Lameter81819f02007-05-06 14:49:36 -07002292}
2293
Christoph Lameter894b8782007-05-10 03:15:16 -07002294/*
2295 * Fastpath with forced inlining to produce a kfree and kmem_cache_free that
2296 * can perform fastpath freeing without additional function calls.
2297 *
2298 * The fastpath is only possible if we are freeing to the current cpu slab
2299 * of this processor. This typically the case if we have just allocated
2300 * the item before.
2301 *
2302 * If fastpath is not possible then fall back to __slab_free where we deal
2303 * with all sorts of special processing.
2304 */
Pekka Enberg06428782008-01-07 23:20:27 -08002305static __always_inline void slab_free(struct kmem_cache *s,
Eduard - Gabriel Munteanuce71e272008-08-19 20:43:25 +03002306 struct page *page, void *x, unsigned long addr)
Christoph Lameter894b8782007-05-10 03:15:16 -07002307{
2308 void **object = (void *)x;
Christoph Lameterdfb4f092007-10-16 01:26:05 -07002309 struct kmem_cache_cpu *c;
Christoph Lameter8a5ec0b2011-02-25 11:38:54 -06002310 unsigned long tid;
Christoph Lameter1f842602008-01-07 23:20:30 -08002311
Christoph Lameterc016b0b2010-08-20 12:37:16 -05002312 slab_free_hook(s, x);
2313
Christoph Lametera24c5a02011-03-15 12:45:21 -05002314redo:
Christoph Lameter8a5ec0b2011-02-25 11:38:54 -06002315
Christoph Lameter8a5ec0b2011-02-25 11:38:54 -06002316 /*
2317 * Determine the currently cpus per cpu slab.
2318 * The cpu may change afterward. However that does not matter since
2319 * data is retrieved via this pointer. If we are on the same cpu
2320 * during the cmpxchg then the free will succedd.
2321 */
Christoph Lameter9dfc6e62009-12-18 16:26:20 -06002322 c = __this_cpu_ptr(s->cpu_slab);
Christoph Lameterc016b0b2010-08-20 12:37:16 -05002323
Christoph Lameter8a5ec0b2011-02-25 11:38:54 -06002324 tid = c->tid;
2325 barrier();
Christoph Lameterc016b0b2010-08-20 12:37:16 -05002326
Christoph Lameter442b06b2011-05-17 16:29:31 -05002327 if (likely(page == c->page)) {
Christoph Lameterff120592009-12-18 16:26:22 -06002328 set_freepointer(s, object, c->freelist);
Christoph Lameter8a5ec0b2011-02-25 11:38:54 -06002329
Thomas Gleixner30106b82011-05-04 15:38:19 +02002330 if (unlikely(!irqsafe_cpu_cmpxchg_double(
Christoph Lameter8a5ec0b2011-02-25 11:38:54 -06002331 s->cpu_slab->freelist, s->cpu_slab->tid,
2332 c->freelist, tid,
2333 object, next_tid(tid)))) {
2334
2335 note_cmpxchg_failure("slab_free", s, tid);
2336 goto redo;
2337 }
Christoph Lameter84e554e2009-12-18 16:26:23 -06002338 stat(s, FREE_FASTPATH);
Christoph Lameter894b8782007-05-10 03:15:16 -07002339 } else
Christoph Lameterff120592009-12-18 16:26:22 -06002340 __slab_free(s, page, x, addr);
Christoph Lameter894b8782007-05-10 03:15:16 -07002341
Christoph Lameter894b8782007-05-10 03:15:16 -07002342}
2343
Christoph Lameter81819f02007-05-06 14:49:36 -07002344void kmem_cache_free(struct kmem_cache *s, void *x)
2345{
Christoph Lameter77c5e2d2007-05-06 14:49:42 -07002346 struct page *page;
Christoph Lameter81819f02007-05-06 14:49:36 -07002347
Christoph Lameterb49af682007-05-06 14:49:41 -07002348 page = virt_to_head_page(x);
Christoph Lameter81819f02007-05-06 14:49:36 -07002349
Eduard - Gabriel Munteanuce71e272008-08-19 20:43:25 +03002350 slab_free(s, page, x, _RET_IP_);
Eduard - Gabriel Munteanu5b882be2008-08-19 20:43:26 +03002351
Eduard - Gabriel Munteanuca2b84c2009-03-23 15:12:24 +02002352 trace_kmem_cache_free(_RET_IP_, x);
Christoph Lameter81819f02007-05-06 14:49:36 -07002353}
2354EXPORT_SYMBOL(kmem_cache_free);
2355
Christoph Lameter81819f02007-05-06 14:49:36 -07002356/*
Christoph Lameter672bba32007-05-09 02:32:39 -07002357 * Object placement in a slab is made very easy because we always start at
2358 * offset 0. If we tune the size of the object to the alignment then we can
2359 * get the required alignment by putting one properly sized object after
2360 * another.
Christoph Lameter81819f02007-05-06 14:49:36 -07002361 *
2362 * Notice that the allocation order determines the sizes of the per cpu
2363 * caches. Each processor has always one slab available for allocations.
2364 * Increasing the allocation order reduces the number of times that slabs
Christoph Lameter672bba32007-05-09 02:32:39 -07002365 * must be moved on and off the partial lists and is therefore a factor in
Christoph Lameter81819f02007-05-06 14:49:36 -07002366 * locking overhead.
Christoph Lameter81819f02007-05-06 14:49:36 -07002367 */
2368
2369/*
2370 * Mininum / Maximum order of slab pages. This influences locking overhead
2371 * and slab fragmentation. A higher order reduces the number of partial slabs
2372 * and increases the number of allocations possible without having to
2373 * take the list_lock.
2374 */
2375static int slub_min_order;
Christoph Lameter114e9e82008-04-14 19:11:41 +03002376static int slub_max_order = PAGE_ALLOC_COSTLY_ORDER;
Christoph Lameter9b2cd502008-04-14 19:11:41 +03002377static int slub_min_objects;
Christoph Lameter81819f02007-05-06 14:49:36 -07002378
2379/*
2380 * Merge control. If this is set then no merging of slab caches will occur.
Christoph Lameter672bba32007-05-09 02:32:39 -07002381 * (Could be removed. This was introduced to pacify the merge skeptics.)
Christoph Lameter81819f02007-05-06 14:49:36 -07002382 */
2383static int slub_nomerge;
2384
2385/*
Christoph Lameter81819f02007-05-06 14:49:36 -07002386 * Calculate the order of allocation given an slab object size.
2387 *
Christoph Lameter672bba32007-05-09 02:32:39 -07002388 * The order of allocation has significant impact on performance and other
2389 * system components. Generally order 0 allocations should be preferred since
2390 * order 0 does not cause fragmentation in the page allocator. Larger objects
2391 * be problematic to put into order 0 slabs because there may be too much
Christoph Lameterc124f5b2008-04-14 19:13:29 +03002392 * unused space left. We go to a higher order if more than 1/16th of the slab
Christoph Lameter672bba32007-05-09 02:32:39 -07002393 * would be wasted.
Christoph Lameter81819f02007-05-06 14:49:36 -07002394 *
Christoph Lameter672bba32007-05-09 02:32:39 -07002395 * In order to reach satisfactory performance we must ensure that a minimum
2396 * number of objects is in one slab. Otherwise we may generate too much
2397 * activity on the partial lists which requires taking the list_lock. This is
2398 * less a concern for large slabs though which are rarely used.
Christoph Lameter81819f02007-05-06 14:49:36 -07002399 *
Christoph Lameter672bba32007-05-09 02:32:39 -07002400 * slub_max_order specifies the order where we begin to stop considering the
2401 * number of objects in a slab as critical. If we reach slub_max_order then
2402 * we try to keep the page order as low as possible. So we accept more waste
2403 * of space in favor of a small page order.
2404 *
2405 * Higher order allocations also allow the placement of more objects in a
2406 * slab and thereby reduce object handling overhead. If the user has
2407 * requested a higher mininum order then we start with that one instead of
2408 * the smallest order which will fit the object.
Christoph Lameter81819f02007-05-06 14:49:36 -07002409 */
Christoph Lameter5e6d4442007-05-09 02:32:46 -07002410static inline int slab_order(int size, int min_objects,
Lai Jiangshanab9a0f12011-03-10 15:21:48 +08002411 int max_order, int fract_leftover, int reserved)
Christoph Lameter81819f02007-05-06 14:49:36 -07002412{
2413 int order;
2414 int rem;
Christoph Lameter6300ea72007-07-17 04:03:20 -07002415 int min_order = slub_min_order;
Christoph Lameter81819f02007-05-06 14:49:36 -07002416
Lai Jiangshanab9a0f12011-03-10 15:21:48 +08002417 if (order_objects(min_order, size, reserved) > MAX_OBJS_PER_PAGE)
Cyrill Gorcunov210b5c02008-10-22 23:00:38 +04002418 return get_order(size * MAX_OBJS_PER_PAGE) - 1;
Christoph Lameter39b26462008-04-14 19:11:30 +03002419
Christoph Lameter6300ea72007-07-17 04:03:20 -07002420 for (order = max(min_order,
Christoph Lameter5e6d4442007-05-09 02:32:46 -07002421 fls(min_objects * size - 1) - PAGE_SHIFT);
2422 order <= max_order; order++) {
2423
Christoph Lameter81819f02007-05-06 14:49:36 -07002424 unsigned long slab_size = PAGE_SIZE << order;
2425
Lai Jiangshanab9a0f12011-03-10 15:21:48 +08002426 if (slab_size < min_objects * size + reserved)
Christoph Lameter81819f02007-05-06 14:49:36 -07002427 continue;
2428
Lai Jiangshanab9a0f12011-03-10 15:21:48 +08002429 rem = (slab_size - reserved) % size;
Christoph Lameter81819f02007-05-06 14:49:36 -07002430
Christoph Lameter5e6d4442007-05-09 02:32:46 -07002431 if (rem <= slab_size / fract_leftover)
Christoph Lameter81819f02007-05-06 14:49:36 -07002432 break;
2433
2434 }
Christoph Lameter672bba32007-05-09 02:32:39 -07002435
Christoph Lameter81819f02007-05-06 14:49:36 -07002436 return order;
2437}
2438
Lai Jiangshanab9a0f12011-03-10 15:21:48 +08002439static inline int calculate_order(int size, int reserved)
Christoph Lameter5e6d4442007-05-09 02:32:46 -07002440{
2441 int order;
2442 int min_objects;
2443 int fraction;
Zhang Yanmine8120ff2009-02-12 18:00:17 +02002444 int max_objects;
Christoph Lameter5e6d4442007-05-09 02:32:46 -07002445
2446 /*
2447 * Attempt to find best configuration for a slab. This
2448 * works by first attempting to generate a layout with
2449 * the best configuration and backing off gradually.
2450 *
2451 * First we reduce the acceptable waste in a slab. Then
2452 * we reduce the minimum objects required in a slab.
2453 */
2454 min_objects = slub_min_objects;
Christoph Lameter9b2cd502008-04-14 19:11:41 +03002455 if (!min_objects)
2456 min_objects = 4 * (fls(nr_cpu_ids) + 1);
Lai Jiangshanab9a0f12011-03-10 15:21:48 +08002457 max_objects = order_objects(slub_max_order, size, reserved);
Zhang Yanmine8120ff2009-02-12 18:00:17 +02002458 min_objects = min(min_objects, max_objects);
2459
Christoph Lameter5e6d4442007-05-09 02:32:46 -07002460 while (min_objects > 1) {
Christoph Lameterc124f5b2008-04-14 19:13:29 +03002461 fraction = 16;
Christoph Lameter5e6d4442007-05-09 02:32:46 -07002462 while (fraction >= 4) {
2463 order = slab_order(size, min_objects,
Lai Jiangshanab9a0f12011-03-10 15:21:48 +08002464 slub_max_order, fraction, reserved);
Christoph Lameter5e6d4442007-05-09 02:32:46 -07002465 if (order <= slub_max_order)
2466 return order;
2467 fraction /= 2;
2468 }
Amerigo Wang5086c382009-08-19 21:44:13 +03002469 min_objects--;
Christoph Lameter5e6d4442007-05-09 02:32:46 -07002470 }
2471
2472 /*
2473 * We were unable to place multiple objects in a slab. Now
2474 * lets see if we can place a single object there.
2475 */
Lai Jiangshanab9a0f12011-03-10 15:21:48 +08002476 order = slab_order(size, 1, slub_max_order, 1, reserved);
Christoph Lameter5e6d4442007-05-09 02:32:46 -07002477 if (order <= slub_max_order)
2478 return order;
2479
2480 /*
2481 * Doh this slab cannot be placed using slub_max_order.
2482 */
Lai Jiangshanab9a0f12011-03-10 15:21:48 +08002483 order = slab_order(size, 1, MAX_ORDER, 1, reserved);
David Rientjes818cf592009-04-23 09:58:22 +03002484 if (order < MAX_ORDER)
Christoph Lameter5e6d4442007-05-09 02:32:46 -07002485 return order;
2486 return -ENOSYS;
2487}
2488
Christoph Lameter81819f02007-05-06 14:49:36 -07002489/*
Christoph Lameter672bba32007-05-09 02:32:39 -07002490 * Figure out what the alignment of the objects will be.
Christoph Lameter81819f02007-05-06 14:49:36 -07002491 */
2492static unsigned long calculate_alignment(unsigned long flags,
2493 unsigned long align, unsigned long size)
2494{
2495 /*
Christoph Lameter6446faa2008-02-15 23:45:26 -08002496 * If the user wants hardware cache aligned objects then follow that
2497 * suggestion if the object is sufficiently large.
Christoph Lameter81819f02007-05-06 14:49:36 -07002498 *
Christoph Lameter6446faa2008-02-15 23:45:26 -08002499 * The hardware cache alignment cannot override the specified
2500 * alignment though. If that is greater then use it.
Christoph Lameter81819f02007-05-06 14:49:36 -07002501 */
Nick Pigginb6210382008-03-05 14:05:56 -08002502 if (flags & SLAB_HWCACHE_ALIGN) {
2503 unsigned long ralign = cache_line_size();
2504 while (size <= ralign / 2)
2505 ralign /= 2;
2506 align = max(align, ralign);
2507 }
Christoph Lameter81819f02007-05-06 14:49:36 -07002508
2509 if (align < ARCH_SLAB_MINALIGN)
Nick Pigginb6210382008-03-05 14:05:56 -08002510 align = ARCH_SLAB_MINALIGN;
Christoph Lameter81819f02007-05-06 14:49:36 -07002511
2512 return ALIGN(align, sizeof(void *));
2513}
2514
Pekka Enberg5595cff2008-08-05 09:28:47 +03002515static void
2516init_kmem_cache_node(struct kmem_cache_node *n, struct kmem_cache *s)
Christoph Lameter81819f02007-05-06 14:49:36 -07002517{
2518 n->nr_partial = 0;
Christoph Lameter81819f02007-05-06 14:49:36 -07002519 spin_lock_init(&n->list_lock);
2520 INIT_LIST_HEAD(&n->partial);
Christoph Lameter8ab13722007-07-17 04:03:32 -07002521#ifdef CONFIG_SLUB_DEBUG
Christoph Lameter0f389ec2008-04-14 18:53:02 +03002522 atomic_long_set(&n->nr_slabs, 0);
Salman Qazi02b71b72008-09-11 12:25:41 -07002523 atomic_long_set(&n->total_objects, 0);
Christoph Lameter643b1132007-05-06 14:49:42 -07002524 INIT_LIST_HEAD(&n->full);
Christoph Lameter8ab13722007-07-17 04:03:32 -07002525#endif
Christoph Lameter81819f02007-05-06 14:49:36 -07002526}
2527
Christoph Lameter55136592010-08-20 12:37:13 -05002528static inline int alloc_kmem_cache_cpus(struct kmem_cache *s)
Christoph Lameter4c93c3552007-10-16 01:26:08 -07002529{
Christoph Lameter6c182dc2010-08-20 12:37:14 -05002530 BUILD_BUG_ON(PERCPU_DYNAMIC_EARLY_SIZE <
2531 SLUB_PAGE_SHIFT * sizeof(struct kmem_cache_cpu));
Christoph Lameter9dfc6e62009-12-18 16:26:20 -06002532
Christoph Lameter8a5ec0b2011-02-25 11:38:54 -06002533 /*
Chris Metcalfd4d84fe2011-06-02 10:19:41 -04002534 * Must align to double word boundary for the double cmpxchg
2535 * instructions to work; see __pcpu_double_call_return_bool().
Christoph Lameter8a5ec0b2011-02-25 11:38:54 -06002536 */
Chris Metcalfd4d84fe2011-06-02 10:19:41 -04002537 s->cpu_slab = __alloc_percpu(sizeof(struct kmem_cache_cpu),
2538 2 * sizeof(void *));
Christoph Lameter9dfc6e62009-12-18 16:26:20 -06002539
Christoph Lameter8a5ec0b2011-02-25 11:38:54 -06002540 if (!s->cpu_slab)
2541 return 0;
2542
2543 init_kmem_cache_cpus(s);
2544
2545 return 1;
Christoph Lameter4c93c3552007-10-16 01:26:08 -07002546}
Christoph Lameter4c93c3552007-10-16 01:26:08 -07002547
Christoph Lameter51df1142010-08-20 12:37:15 -05002548static struct kmem_cache *kmem_cache_node;
2549
Christoph Lameter81819f02007-05-06 14:49:36 -07002550/*
2551 * No kmalloc_node yet so do it by hand. We know that this is the first
2552 * slab on the node for this slabcache. There are no concurrent accesses
2553 * possible.
2554 *
2555 * Note that this function only works on the kmalloc_node_cache
Christoph Lameter4c93c3552007-10-16 01:26:08 -07002556 * when allocating for the kmalloc_node_cache. This is used for bootstrapping
2557 * memory on a fresh node that has no slab structures yet.
Christoph Lameter81819f02007-05-06 14:49:36 -07002558 */
Christoph Lameter55136592010-08-20 12:37:13 -05002559static void early_kmem_cache_node_alloc(int node)
Christoph Lameter81819f02007-05-06 14:49:36 -07002560{
2561 struct page *page;
2562 struct kmem_cache_node *n;
2563
Christoph Lameter51df1142010-08-20 12:37:15 -05002564 BUG_ON(kmem_cache_node->size < sizeof(struct kmem_cache_node));
Christoph Lameter81819f02007-05-06 14:49:36 -07002565
Christoph Lameter51df1142010-08-20 12:37:15 -05002566 page = new_slab(kmem_cache_node, GFP_NOWAIT, node);
Christoph Lameter81819f02007-05-06 14:49:36 -07002567
2568 BUG_ON(!page);
Christoph Lametera2f92ee2007-08-22 14:01:57 -07002569 if (page_to_nid(page) != node) {
2570 printk(KERN_ERR "SLUB: Unable to allocate memory from "
2571 "node %d\n", node);
2572 printk(KERN_ERR "SLUB: Allocating a useless per node structure "
2573 "in order to be able to continue\n");
2574 }
2575
Christoph Lameter81819f02007-05-06 14:49:36 -07002576 n = page->freelist;
2577 BUG_ON(!n);
Christoph Lameter51df1142010-08-20 12:37:15 -05002578 page->freelist = get_freepointer(kmem_cache_node, n);
Christoph Lameter81819f02007-05-06 14:49:36 -07002579 page->inuse++;
Christoph Lameter8cb0a502011-06-01 12:25:46 -05002580 page->frozen = 0;
Christoph Lameter51df1142010-08-20 12:37:15 -05002581 kmem_cache_node->node[node] = n;
Christoph Lameter8ab13722007-07-17 04:03:32 -07002582#ifdef CONFIG_SLUB_DEBUG
Christoph Lameterf7cb1932010-09-29 07:15:01 -05002583 init_object(kmem_cache_node, n, SLUB_RED_ACTIVE);
Christoph Lameter51df1142010-08-20 12:37:15 -05002584 init_tracking(kmem_cache_node, n);
Christoph Lameter8ab13722007-07-17 04:03:32 -07002585#endif
Christoph Lameter51df1142010-08-20 12:37:15 -05002586 init_kmem_cache_node(n, kmem_cache_node);
2587 inc_slabs_node(kmem_cache_node, node, page->objects);
Christoph Lameter6446faa2008-02-15 23:45:26 -08002588
Christoph Lameter7c2e1322008-01-07 23:20:27 -08002589 add_partial(n, page, 0);
Christoph Lameter81819f02007-05-06 14:49:36 -07002590}
2591
2592static void free_kmem_cache_nodes(struct kmem_cache *s)
2593{
2594 int node;
2595
Christoph Lameterf64dc582007-10-16 01:25:33 -07002596 for_each_node_state(node, N_NORMAL_MEMORY) {
Christoph Lameter81819f02007-05-06 14:49:36 -07002597 struct kmem_cache_node *n = s->node[node];
Christoph Lameter51df1142010-08-20 12:37:15 -05002598
Alexander Duyck73367bd2010-05-21 14:41:35 -07002599 if (n)
Christoph Lameter51df1142010-08-20 12:37:15 -05002600 kmem_cache_free(kmem_cache_node, n);
2601
Christoph Lameter81819f02007-05-06 14:49:36 -07002602 s->node[node] = NULL;
2603 }
2604}
2605
Christoph Lameter55136592010-08-20 12:37:13 -05002606static int init_kmem_cache_nodes(struct kmem_cache *s)
Christoph Lameter81819f02007-05-06 14:49:36 -07002607{
2608 int node;
Christoph Lameter81819f02007-05-06 14:49:36 -07002609
Christoph Lameterf64dc582007-10-16 01:25:33 -07002610 for_each_node_state(node, N_NORMAL_MEMORY) {
Christoph Lameter81819f02007-05-06 14:49:36 -07002611 struct kmem_cache_node *n;
2612
Alexander Duyck73367bd2010-05-21 14:41:35 -07002613 if (slab_state == DOWN) {
Christoph Lameter55136592010-08-20 12:37:13 -05002614 early_kmem_cache_node_alloc(node);
Alexander Duyck73367bd2010-05-21 14:41:35 -07002615 continue;
Christoph Lameter81819f02007-05-06 14:49:36 -07002616 }
Christoph Lameter51df1142010-08-20 12:37:15 -05002617 n = kmem_cache_alloc_node(kmem_cache_node,
Christoph Lameter55136592010-08-20 12:37:13 -05002618 GFP_KERNEL, node);
Alexander Duyck73367bd2010-05-21 14:41:35 -07002619
2620 if (!n) {
2621 free_kmem_cache_nodes(s);
2622 return 0;
2623 }
2624
Christoph Lameter81819f02007-05-06 14:49:36 -07002625 s->node[node] = n;
Pekka Enberg5595cff2008-08-05 09:28:47 +03002626 init_kmem_cache_node(n, s);
Christoph Lameter81819f02007-05-06 14:49:36 -07002627 }
2628 return 1;
2629}
Christoph Lameter81819f02007-05-06 14:49:36 -07002630
David Rientjesc0bdb232009-02-25 09:16:35 +02002631static void set_min_partial(struct kmem_cache *s, unsigned long min)
David Rientjes3b89d7d2009-02-22 17:40:07 -08002632{
2633 if (min < MIN_PARTIAL)
2634 min = MIN_PARTIAL;
2635 else if (min > MAX_PARTIAL)
2636 min = MAX_PARTIAL;
2637 s->min_partial = min;
2638}
2639
Christoph Lameter81819f02007-05-06 14:49:36 -07002640/*
2641 * calculate_sizes() determines the order and the distribution of data within
2642 * a slab object.
2643 */
Christoph Lameter06b285d2008-04-14 19:11:41 +03002644static int calculate_sizes(struct kmem_cache *s, int forced_order)
Christoph Lameter81819f02007-05-06 14:49:36 -07002645{
2646 unsigned long flags = s->flags;
2647 unsigned long size = s->objsize;
2648 unsigned long align = s->align;
Christoph Lameter834f3d12008-04-14 19:11:31 +03002649 int order;
Christoph Lameter81819f02007-05-06 14:49:36 -07002650
2651 /*
Christoph Lameterd8b42bf2008-02-15 23:45:25 -08002652 * Round up object size to the next word boundary. We can only
2653 * place the free pointer at word boundaries and this determines
2654 * the possible location of the free pointer.
2655 */
2656 size = ALIGN(size, sizeof(void *));
2657
2658#ifdef CONFIG_SLUB_DEBUG
2659 /*
Christoph Lameter81819f02007-05-06 14:49:36 -07002660 * Determine if we can poison the object itself. If the user of
2661 * the slab may touch the object after free or before allocation
2662 * then we should never poison the object itself.
2663 */
2664 if ((flags & SLAB_POISON) && !(flags & SLAB_DESTROY_BY_RCU) &&
Christoph Lameterc59def9f2007-05-16 22:10:50 -07002665 !s->ctor)
Christoph Lameter81819f02007-05-06 14:49:36 -07002666 s->flags |= __OBJECT_POISON;
2667 else
2668 s->flags &= ~__OBJECT_POISON;
2669
Christoph Lameter81819f02007-05-06 14:49:36 -07002670
2671 /*
Christoph Lameter672bba32007-05-09 02:32:39 -07002672 * If we are Redzoning then check if there is some space between the
Christoph Lameter81819f02007-05-06 14:49:36 -07002673 * end of the object and the free pointer. If not then add an
Christoph Lameter672bba32007-05-09 02:32:39 -07002674 * additional word to have some bytes to store Redzone information.
Christoph Lameter81819f02007-05-06 14:49:36 -07002675 */
2676 if ((flags & SLAB_RED_ZONE) && size == s->objsize)
2677 size += sizeof(void *);
Christoph Lameter41ecc552007-05-09 02:32:44 -07002678#endif
Christoph Lameter81819f02007-05-06 14:49:36 -07002679
2680 /*
Christoph Lameter672bba32007-05-09 02:32:39 -07002681 * With that we have determined the number of bytes in actual use
2682 * by the object. This is the potential offset to the free pointer.
Christoph Lameter81819f02007-05-06 14:49:36 -07002683 */
2684 s->inuse = size;
2685
2686 if (((flags & (SLAB_DESTROY_BY_RCU | SLAB_POISON)) ||
Christoph Lameterc59def9f2007-05-16 22:10:50 -07002687 s->ctor)) {
Christoph Lameter81819f02007-05-06 14:49:36 -07002688 /*
2689 * Relocate free pointer after the object if it is not
2690 * permitted to overwrite the first word of the object on
2691 * kmem_cache_free.
2692 *
2693 * This is the case if we do RCU, have a constructor or
2694 * destructor or are poisoning the objects.
2695 */
2696 s->offset = size;
2697 size += sizeof(void *);
2698 }
2699
Christoph Lameterc12b3c62007-05-23 13:57:31 -07002700#ifdef CONFIG_SLUB_DEBUG
Christoph Lameter81819f02007-05-06 14:49:36 -07002701 if (flags & SLAB_STORE_USER)
2702 /*
2703 * Need to store information about allocs and frees after
2704 * the object.
2705 */
2706 size += 2 * sizeof(struct track);
2707
Christoph Lameterbe7b3fb2007-05-09 02:32:36 -07002708 if (flags & SLAB_RED_ZONE)
Christoph Lameter81819f02007-05-06 14:49:36 -07002709 /*
2710 * Add some empty padding so that we can catch
2711 * overwrites from earlier objects rather than let
2712 * tracking information or the free pointer be
Frederik Schwarzer0211a9c2008-12-29 22:14:56 +01002713 * corrupted if a user writes before the start
Christoph Lameter81819f02007-05-06 14:49:36 -07002714 * of the object.
2715 */
2716 size += sizeof(void *);
Christoph Lameter41ecc552007-05-09 02:32:44 -07002717#endif
Christoph Lameter672bba32007-05-09 02:32:39 -07002718
Christoph Lameter81819f02007-05-06 14:49:36 -07002719 /*
2720 * Determine the alignment based on various parameters that the
Christoph Lameter65c02d42007-05-09 02:32:35 -07002721 * user specified and the dynamic determination of cache line size
2722 * on bootup.
Christoph Lameter81819f02007-05-06 14:49:36 -07002723 */
2724 align = calculate_alignment(flags, align, s->objsize);
Zhang, Yanmindcb0ce12009-07-30 11:28:11 +08002725 s->align = align;
Christoph Lameter81819f02007-05-06 14:49:36 -07002726
2727 /*
2728 * SLUB stores one object immediately after another beginning from
2729 * offset 0. In order to align the objects we have to simply size
2730 * each object to conform to the alignment.
2731 */
2732 size = ALIGN(size, align);
2733 s->size = size;
Christoph Lameter06b285d2008-04-14 19:11:41 +03002734 if (forced_order >= 0)
2735 order = forced_order;
2736 else
Lai Jiangshanab9a0f12011-03-10 15:21:48 +08002737 order = calculate_order(size, s->reserved);
Christoph Lameter81819f02007-05-06 14:49:36 -07002738
Christoph Lameter834f3d12008-04-14 19:11:31 +03002739 if (order < 0)
Christoph Lameter81819f02007-05-06 14:49:36 -07002740 return 0;
2741
Christoph Lameterb7a49f02008-02-14 14:21:32 -08002742 s->allocflags = 0;
Christoph Lameter834f3d12008-04-14 19:11:31 +03002743 if (order)
Christoph Lameterb7a49f02008-02-14 14:21:32 -08002744 s->allocflags |= __GFP_COMP;
2745
2746 if (s->flags & SLAB_CACHE_DMA)
2747 s->allocflags |= SLUB_DMA;
2748
2749 if (s->flags & SLAB_RECLAIM_ACCOUNT)
2750 s->allocflags |= __GFP_RECLAIMABLE;
2751
Christoph Lameter81819f02007-05-06 14:49:36 -07002752 /*
2753 * Determine the number of objects per slab
2754 */
Lai Jiangshanab9a0f12011-03-10 15:21:48 +08002755 s->oo = oo_make(order, size, s->reserved);
2756 s->min = oo_make(get_order(size), size, s->reserved);
Christoph Lameter205ab992008-04-14 19:11:40 +03002757 if (oo_objects(s->oo) > oo_objects(s->max))
2758 s->max = s->oo;
Christoph Lameter81819f02007-05-06 14:49:36 -07002759
Christoph Lameter834f3d12008-04-14 19:11:31 +03002760 return !!oo_objects(s->oo);
Christoph Lameter81819f02007-05-06 14:49:36 -07002761
2762}
2763
Christoph Lameter55136592010-08-20 12:37:13 -05002764static int kmem_cache_open(struct kmem_cache *s,
Christoph Lameter81819f02007-05-06 14:49:36 -07002765 const char *name, size_t size,
2766 size_t align, unsigned long flags,
Alexey Dobriyan51cc5062008-07-25 19:45:34 -07002767 void (*ctor)(void *))
Christoph Lameter81819f02007-05-06 14:49:36 -07002768{
2769 memset(s, 0, kmem_size);
2770 s->name = name;
2771 s->ctor = ctor;
Christoph Lameter81819f02007-05-06 14:49:36 -07002772 s->objsize = size;
Christoph Lameter81819f02007-05-06 14:49:36 -07002773 s->align = align;
Christoph Lameterba0268a2007-09-11 15:24:11 -07002774 s->flags = kmem_cache_flags(size, flags, name, ctor);
Lai Jiangshanab9a0f12011-03-10 15:21:48 +08002775 s->reserved = 0;
Christoph Lameter81819f02007-05-06 14:49:36 -07002776
Lai Jiangshanda9a6382011-03-10 15:22:00 +08002777 if (need_reserve_slab_rcu && (s->flags & SLAB_DESTROY_BY_RCU))
2778 s->reserved = sizeof(struct rcu_head);
Christoph Lameter81819f02007-05-06 14:49:36 -07002779
Christoph Lameter06b285d2008-04-14 19:11:41 +03002780 if (!calculate_sizes(s, -1))
Christoph Lameter81819f02007-05-06 14:49:36 -07002781 goto error;
David Rientjes3de47212009-07-27 18:30:35 -07002782 if (disable_higher_order_debug) {
2783 /*
2784 * Disable debugging flags that store metadata if the min slab
2785 * order increased.
2786 */
2787 if (get_order(s->size) > get_order(s->objsize)) {
2788 s->flags &= ~DEBUG_METADATA_FLAGS;
2789 s->offset = 0;
2790 if (!calculate_sizes(s, -1))
2791 goto error;
2792 }
2793 }
Christoph Lameter81819f02007-05-06 14:49:36 -07002794
Christoph Lameterb789ef52011-06-01 12:25:49 -05002795#ifdef CONFIG_CMPXCHG_DOUBLE
2796 if (system_has_cmpxchg_double() && (s->flags & SLAB_DEBUG_FLAGS) == 0)
2797 /* Enable fast mode */
2798 s->flags |= __CMPXCHG_DOUBLE;
2799#endif
2800
David Rientjes3b89d7d2009-02-22 17:40:07 -08002801 /*
2802 * The larger the object size is, the more pages we want on the partial
2803 * list to avoid pounding the page allocator excessively.
2804 */
David Rientjesc0bdb232009-02-25 09:16:35 +02002805 set_min_partial(s, ilog2(s->size));
Christoph Lameter81819f02007-05-06 14:49:36 -07002806 s->refcount = 1;
2807#ifdef CONFIG_NUMA
Christoph Lametere2cb96b2008-08-19 08:51:22 -05002808 s->remote_node_defrag_ratio = 1000;
Christoph Lameter81819f02007-05-06 14:49:36 -07002809#endif
Christoph Lameter55136592010-08-20 12:37:13 -05002810 if (!init_kmem_cache_nodes(s))
Christoph Lameterdfb4f092007-10-16 01:26:05 -07002811 goto error;
Christoph Lameter81819f02007-05-06 14:49:36 -07002812
Christoph Lameter55136592010-08-20 12:37:13 -05002813 if (alloc_kmem_cache_cpus(s))
Christoph Lameter81819f02007-05-06 14:49:36 -07002814 return 1;
Christoph Lameterff120592009-12-18 16:26:22 -06002815
Christoph Lameter4c93c3552007-10-16 01:26:08 -07002816 free_kmem_cache_nodes(s);
Christoph Lameter81819f02007-05-06 14:49:36 -07002817error:
2818 if (flags & SLAB_PANIC)
2819 panic("Cannot create slab %s size=%lu realsize=%u "
2820 "order=%u offset=%u flags=%lx\n",
Christoph Lameter834f3d12008-04-14 19:11:31 +03002821 s->name, (unsigned long)size, s->size, oo_order(s->oo),
Christoph Lameter81819f02007-05-06 14:49:36 -07002822 s->offset, flags);
2823 return 0;
2824}
Christoph Lameter81819f02007-05-06 14:49:36 -07002825
2826/*
Christoph Lameter81819f02007-05-06 14:49:36 -07002827 * Determine the size of a slab object
2828 */
2829unsigned int kmem_cache_size(struct kmem_cache *s)
2830{
2831 return s->objsize;
2832}
2833EXPORT_SYMBOL(kmem_cache_size);
2834
Christoph Lameter33b12c32008-04-25 12:22:43 -07002835static void list_slab_objects(struct kmem_cache *s, struct page *page,
2836 const char *text)
Christoph Lameter81819f02007-05-06 14:49:36 -07002837{
Christoph Lameter33b12c32008-04-25 12:22:43 -07002838#ifdef CONFIG_SLUB_DEBUG
2839 void *addr = page_address(page);
2840 void *p;
Namhyung Kima5dd5c12010-09-29 21:02:13 +09002841 unsigned long *map = kzalloc(BITS_TO_LONGS(page->objects) *
2842 sizeof(long), GFP_ATOMIC);
Eric Dumazetbbd7d572010-03-24 22:25:47 +01002843 if (!map)
2844 return;
Christoph Lameter33b12c32008-04-25 12:22:43 -07002845 slab_err(s, page, "%s", text);
2846 slab_lock(page);
Christoph Lameter33b12c32008-04-25 12:22:43 -07002847
Christoph Lameter5f80b132011-04-15 14:48:13 -05002848 get_map(s, page, map);
Christoph Lameter33b12c32008-04-25 12:22:43 -07002849 for_each_object(p, s, addr, page->objects) {
2850
2851 if (!test_bit(slab_index(p, s, addr), map)) {
2852 printk(KERN_ERR "INFO: Object 0x%p @offset=%tu\n",
2853 p, p - addr);
2854 print_tracking(s, p);
2855 }
2856 }
2857 slab_unlock(page);
Eric Dumazetbbd7d572010-03-24 22:25:47 +01002858 kfree(map);
Christoph Lameter33b12c32008-04-25 12:22:43 -07002859#endif
2860}
2861
Christoph Lameter81819f02007-05-06 14:49:36 -07002862/*
Christoph Lameter599870b2008-04-23 12:36:52 -07002863 * Attempt to free all partial slabs on a node.
Christoph Lameter81819f02007-05-06 14:49:36 -07002864 */
Christoph Lameter599870b2008-04-23 12:36:52 -07002865static void free_partial(struct kmem_cache *s, struct kmem_cache_node *n)
Christoph Lameter81819f02007-05-06 14:49:36 -07002866{
Christoph Lameter81819f02007-05-06 14:49:36 -07002867 unsigned long flags;
2868 struct page *page, *h;
2869
2870 spin_lock_irqsave(&n->list_lock, flags);
Christoph Lameter33b12c32008-04-25 12:22:43 -07002871 list_for_each_entry_safe(page, h, &n->partial, lru) {
Christoph Lameter81819f02007-05-06 14:49:36 -07002872 if (!page->inuse) {
Christoph Lameter5cc6eee2011-06-01 12:25:50 -05002873 remove_partial(n, page);
Christoph Lameter81819f02007-05-06 14:49:36 -07002874 discard_slab(s, page);
Christoph Lameter33b12c32008-04-25 12:22:43 -07002875 } else {
2876 list_slab_objects(s, page,
2877 "Objects remaining on kmem_cache_close()");
Christoph Lameter599870b2008-04-23 12:36:52 -07002878 }
Christoph Lameter33b12c32008-04-25 12:22:43 -07002879 }
Christoph Lameter81819f02007-05-06 14:49:36 -07002880 spin_unlock_irqrestore(&n->list_lock, flags);
Christoph Lameter81819f02007-05-06 14:49:36 -07002881}
2882
2883/*
Christoph Lameter672bba32007-05-09 02:32:39 -07002884 * Release all resources used by a slab cache.
Christoph Lameter81819f02007-05-06 14:49:36 -07002885 */
Christoph Lameter0c710012007-07-17 04:03:24 -07002886static inline int kmem_cache_close(struct kmem_cache *s)
Christoph Lameter81819f02007-05-06 14:49:36 -07002887{
2888 int node;
2889
2890 flush_all(s);
Christoph Lameter9dfc6e62009-12-18 16:26:20 -06002891 free_percpu(s->cpu_slab);
Christoph Lameter81819f02007-05-06 14:49:36 -07002892 /* Attempt to free all objects */
Christoph Lameterf64dc582007-10-16 01:25:33 -07002893 for_each_node_state(node, N_NORMAL_MEMORY) {
Christoph Lameter81819f02007-05-06 14:49:36 -07002894 struct kmem_cache_node *n = get_node(s, node);
2895
Christoph Lameter599870b2008-04-23 12:36:52 -07002896 free_partial(s, n);
2897 if (n->nr_partial || slabs_node(s, node))
Christoph Lameter81819f02007-05-06 14:49:36 -07002898 return 1;
2899 }
2900 free_kmem_cache_nodes(s);
2901 return 0;
2902}
2903
2904/*
2905 * Close a cache and release the kmem_cache structure
2906 * (must be used for caches created using kmem_cache_create)
2907 */
2908void kmem_cache_destroy(struct kmem_cache *s)
2909{
2910 down_write(&slub_lock);
2911 s->refcount--;
2912 if (!s->refcount) {
2913 list_del(&s->list);
Pekka Enbergd629d812008-04-23 22:31:08 +03002914 if (kmem_cache_close(s)) {
2915 printk(KERN_ERR "SLUB %s: %s called for cache that "
2916 "still has objects.\n", s->name, __func__);
2917 dump_stack();
2918 }
Eric Dumazetd76b1592009-09-03 22:38:59 +03002919 if (s->flags & SLAB_DESTROY_BY_RCU)
2920 rcu_barrier();
Christoph Lameter81819f02007-05-06 14:49:36 -07002921 sysfs_slab_remove(s);
Christoph Lameter2bce6482010-07-19 11:39:11 -05002922 }
2923 up_write(&slub_lock);
Christoph Lameter81819f02007-05-06 14:49:36 -07002924}
2925EXPORT_SYMBOL(kmem_cache_destroy);
2926
2927/********************************************************************
2928 * Kmalloc subsystem
2929 *******************************************************************/
2930
Christoph Lameter51df1142010-08-20 12:37:15 -05002931struct kmem_cache *kmalloc_caches[SLUB_PAGE_SHIFT];
Christoph Lameter81819f02007-05-06 14:49:36 -07002932EXPORT_SYMBOL(kmalloc_caches);
2933
Christoph Lameter51df1142010-08-20 12:37:15 -05002934static struct kmem_cache *kmem_cache;
2935
Christoph Lameter55136592010-08-20 12:37:13 -05002936#ifdef CONFIG_ZONE_DMA
Christoph Lameter51df1142010-08-20 12:37:15 -05002937static struct kmem_cache *kmalloc_dma_caches[SLUB_PAGE_SHIFT];
Christoph Lameter55136592010-08-20 12:37:13 -05002938#endif
2939
Christoph Lameter81819f02007-05-06 14:49:36 -07002940static int __init setup_slub_min_order(char *str)
2941{
Pekka Enberg06428782008-01-07 23:20:27 -08002942 get_option(&str, &slub_min_order);
Christoph Lameter81819f02007-05-06 14:49:36 -07002943
2944 return 1;
2945}
2946
2947__setup("slub_min_order=", setup_slub_min_order);
2948
2949static int __init setup_slub_max_order(char *str)
2950{
Pekka Enberg06428782008-01-07 23:20:27 -08002951 get_option(&str, &slub_max_order);
David Rientjes818cf592009-04-23 09:58:22 +03002952 slub_max_order = min(slub_max_order, MAX_ORDER - 1);
Christoph Lameter81819f02007-05-06 14:49:36 -07002953
2954 return 1;
2955}
2956
2957__setup("slub_max_order=", setup_slub_max_order);
2958
2959static int __init setup_slub_min_objects(char *str)
2960{
Pekka Enberg06428782008-01-07 23:20:27 -08002961 get_option(&str, &slub_min_objects);
Christoph Lameter81819f02007-05-06 14:49:36 -07002962
2963 return 1;
2964}
2965
2966__setup("slub_min_objects=", setup_slub_min_objects);
2967
2968static int __init setup_slub_nomerge(char *str)
2969{
2970 slub_nomerge = 1;
2971 return 1;
2972}
2973
2974__setup("slub_nomerge", setup_slub_nomerge);
2975
Christoph Lameter51df1142010-08-20 12:37:15 -05002976static struct kmem_cache *__init create_kmalloc_cache(const char *name,
2977 int size, unsigned int flags)
Christoph Lameter81819f02007-05-06 14:49:36 -07002978{
Christoph Lameter51df1142010-08-20 12:37:15 -05002979 struct kmem_cache *s;
2980
2981 s = kmem_cache_alloc(kmem_cache, GFP_NOWAIT);
2982
Pekka Enberg83b519e2009-06-10 19:40:04 +03002983 /*
2984 * This function is called with IRQs disabled during early-boot on
2985 * single CPU so there's no need to take slub_lock here.
2986 */
Christoph Lameter55136592010-08-20 12:37:13 -05002987 if (!kmem_cache_open(s, name, size, ARCH_KMALLOC_MINALIGN,
Christoph Lameter319d1e22008-04-14 19:11:41 +03002988 flags, NULL))
Christoph Lameter81819f02007-05-06 14:49:36 -07002989 goto panic;
2990
2991 list_add(&s->list, &slab_caches);
Christoph Lameter51df1142010-08-20 12:37:15 -05002992 return s;
Christoph Lameter81819f02007-05-06 14:49:36 -07002993
2994panic:
2995 panic("Creation of kmalloc slab %s size=%d failed.\n", name, size);
Christoph Lameter51df1142010-08-20 12:37:15 -05002996 return NULL;
Christoph Lameter81819f02007-05-06 14:49:36 -07002997}
2998
Christoph Lameterf1b26332007-07-17 04:03:26 -07002999/*
3000 * Conversion table for small slabs sizes / 8 to the index in the
3001 * kmalloc array. This is necessary for slabs < 192 since we have non power
3002 * of two cache sizes there. The size of larger slabs can be determined using
3003 * fls.
3004 */
3005static s8 size_index[24] = {
3006 3, /* 8 */
3007 4, /* 16 */
3008 5, /* 24 */
3009 5, /* 32 */
3010 6, /* 40 */
3011 6, /* 48 */
3012 6, /* 56 */
3013 6, /* 64 */
3014 1, /* 72 */
3015 1, /* 80 */
3016 1, /* 88 */
3017 1, /* 96 */
3018 7, /* 104 */
3019 7, /* 112 */
3020 7, /* 120 */
3021 7, /* 128 */
3022 2, /* 136 */
3023 2, /* 144 */
3024 2, /* 152 */
3025 2, /* 160 */
3026 2, /* 168 */
3027 2, /* 176 */
3028 2, /* 184 */
3029 2 /* 192 */
3030};
3031
Aaro Koskinenacdfcd02009-08-28 14:28:54 +03003032static inline int size_index_elem(size_t bytes)
3033{
3034 return (bytes - 1) / 8;
3035}
3036
Christoph Lameter81819f02007-05-06 14:49:36 -07003037static struct kmem_cache *get_slab(size_t size, gfp_t flags)
3038{
Christoph Lameterf1b26332007-07-17 04:03:26 -07003039 int index;
Christoph Lameter81819f02007-05-06 14:49:36 -07003040
Christoph Lameterf1b26332007-07-17 04:03:26 -07003041 if (size <= 192) {
3042 if (!size)
3043 return ZERO_SIZE_PTR;
Christoph Lameter81819f02007-05-06 14:49:36 -07003044
Aaro Koskinenacdfcd02009-08-28 14:28:54 +03003045 index = size_index[size_index_elem(size)];
Christoph Lameteraadb4bc2007-10-16 01:24:38 -07003046 } else
Christoph Lameterf1b26332007-07-17 04:03:26 -07003047 index = fls(size - 1);
Christoph Lameter81819f02007-05-06 14:49:36 -07003048
3049#ifdef CONFIG_ZONE_DMA
Christoph Lameterf1b26332007-07-17 04:03:26 -07003050 if (unlikely((flags & SLUB_DMA)))
Christoph Lameter51df1142010-08-20 12:37:15 -05003051 return kmalloc_dma_caches[index];
Christoph Lameterf1b26332007-07-17 04:03:26 -07003052
Christoph Lameter81819f02007-05-06 14:49:36 -07003053#endif
Christoph Lameter51df1142010-08-20 12:37:15 -05003054 return kmalloc_caches[index];
Christoph Lameter81819f02007-05-06 14:49:36 -07003055}
3056
3057void *__kmalloc(size_t size, gfp_t flags)
3058{
Christoph Lameteraadb4bc2007-10-16 01:24:38 -07003059 struct kmem_cache *s;
Eduard - Gabriel Munteanu5b882be2008-08-19 20:43:26 +03003060 void *ret;
Christoph Lameter81819f02007-05-06 14:49:36 -07003061
Christoph Lameterffadd4d2009-02-17 12:05:07 -05003062 if (unlikely(size > SLUB_MAX_SIZE))
Pekka Enbergeada35e2008-02-11 22:47:46 +02003063 return kmalloc_large(size, flags);
Christoph Lameteraadb4bc2007-10-16 01:24:38 -07003064
3065 s = get_slab(size, flags);
3066
3067 if (unlikely(ZERO_OR_NULL_PTR(s)))
Christoph Lameter6cb8f912007-07-17 04:03:22 -07003068 return s;
3069
Christoph Lameter2154a332010-07-09 14:07:10 -05003070 ret = slab_alloc(s, flags, NUMA_NO_NODE, _RET_IP_);
Eduard - Gabriel Munteanu5b882be2008-08-19 20:43:26 +03003071
Eduard - Gabriel Munteanuca2b84c2009-03-23 15:12:24 +02003072 trace_kmalloc(_RET_IP_, ret, size, s->size, flags);
Eduard - Gabriel Munteanu5b882be2008-08-19 20:43:26 +03003073
3074 return ret;
Christoph Lameter81819f02007-05-06 14:49:36 -07003075}
3076EXPORT_SYMBOL(__kmalloc);
3077
Namhyung Kim5d1f57e2010-09-29 21:02:15 +09003078#ifdef CONFIG_NUMA
Christoph Lameterf619cfe2008-03-01 13:56:40 -08003079static void *kmalloc_large_node(size_t size, gfp_t flags, int node)
3080{
Vegard Nossumb1eeab62008-11-25 16:55:53 +01003081 struct page *page;
Catalin Marinase4f7c0b42009-07-07 10:32:59 +01003082 void *ptr = NULL;
Christoph Lameterf619cfe2008-03-01 13:56:40 -08003083
Vegard Nossumb1eeab62008-11-25 16:55:53 +01003084 flags |= __GFP_COMP | __GFP_NOTRACK;
3085 page = alloc_pages_node(node, flags, get_order(size));
Christoph Lameterf619cfe2008-03-01 13:56:40 -08003086 if (page)
Catalin Marinase4f7c0b42009-07-07 10:32:59 +01003087 ptr = page_address(page);
3088
3089 kmemleak_alloc(ptr, size, 1, flags);
3090 return ptr;
Christoph Lameterf619cfe2008-03-01 13:56:40 -08003091}
3092
Christoph Lameter81819f02007-05-06 14:49:36 -07003093void *__kmalloc_node(size_t size, gfp_t flags, int node)
3094{
Christoph Lameteraadb4bc2007-10-16 01:24:38 -07003095 struct kmem_cache *s;
Eduard - Gabriel Munteanu5b882be2008-08-19 20:43:26 +03003096 void *ret;
Christoph Lameter81819f02007-05-06 14:49:36 -07003097
Ingo Molnar057685c2009-02-20 12:15:30 +01003098 if (unlikely(size > SLUB_MAX_SIZE)) {
Eduard - Gabriel Munteanu5b882be2008-08-19 20:43:26 +03003099 ret = kmalloc_large_node(size, flags, node);
3100
Eduard - Gabriel Munteanuca2b84c2009-03-23 15:12:24 +02003101 trace_kmalloc_node(_RET_IP_, ret,
3102 size, PAGE_SIZE << get_order(size),
3103 flags, node);
Eduard - Gabriel Munteanu5b882be2008-08-19 20:43:26 +03003104
3105 return ret;
3106 }
Christoph Lameteraadb4bc2007-10-16 01:24:38 -07003107
3108 s = get_slab(size, flags);
3109
3110 if (unlikely(ZERO_OR_NULL_PTR(s)))
Christoph Lameter6cb8f912007-07-17 04:03:22 -07003111 return s;
3112
Eduard - Gabriel Munteanu5b882be2008-08-19 20:43:26 +03003113 ret = slab_alloc(s, flags, node, _RET_IP_);
3114
Eduard - Gabriel Munteanuca2b84c2009-03-23 15:12:24 +02003115 trace_kmalloc_node(_RET_IP_, ret, size, s->size, flags, node);
Eduard - Gabriel Munteanu5b882be2008-08-19 20:43:26 +03003116
3117 return ret;
Christoph Lameter81819f02007-05-06 14:49:36 -07003118}
3119EXPORT_SYMBOL(__kmalloc_node);
3120#endif
3121
3122size_t ksize(const void *object)
3123{
Christoph Lameter272c1d22007-06-08 13:46:49 -07003124 struct page *page;
Christoph Lameter81819f02007-05-06 14:49:36 -07003125
Christoph Lameteref8b4522007-10-16 01:24:46 -07003126 if (unlikely(object == ZERO_SIZE_PTR))
Christoph Lameter272c1d22007-06-08 13:46:49 -07003127 return 0;
3128
Vegard Nossum294a80a2007-12-04 23:45:30 -08003129 page = virt_to_head_page(object);
Vegard Nossum294a80a2007-12-04 23:45:30 -08003130
Pekka Enberg76994412008-05-22 19:22:25 +03003131 if (unlikely(!PageSlab(page))) {
3132 WARN_ON(!PageCompound(page));
Vegard Nossum294a80a2007-12-04 23:45:30 -08003133 return PAGE_SIZE << compound_order(page);
Pekka Enberg76994412008-05-22 19:22:25 +03003134 }
Christoph Lameter81819f02007-05-06 14:49:36 -07003135
Eric Dumazetb3d41882011-02-14 18:35:22 +01003136 return slab_ksize(page->slab);
Christoph Lameter81819f02007-05-06 14:49:36 -07003137}
Kirill A. Shutemovb1aabec2009-02-10 15:21:44 +02003138EXPORT_SYMBOL(ksize);
Christoph Lameter81819f02007-05-06 14:49:36 -07003139
3140void kfree(const void *x)
3141{
Christoph Lameter81819f02007-05-06 14:49:36 -07003142 struct page *page;
Christoph Lameter5bb983b2008-02-07 17:47:41 -08003143 void *object = (void *)x;
Christoph Lameter81819f02007-05-06 14:49:36 -07003144
Pekka Enberg2121db72009-03-25 11:05:57 +02003145 trace_kfree(_RET_IP_, x);
3146
Satyam Sharma2408c552007-10-16 01:24:44 -07003147 if (unlikely(ZERO_OR_NULL_PTR(x)))
Christoph Lameter81819f02007-05-06 14:49:36 -07003148 return;
3149
Christoph Lameterb49af682007-05-06 14:49:41 -07003150 page = virt_to_head_page(x);
Christoph Lameteraadb4bc2007-10-16 01:24:38 -07003151 if (unlikely(!PageSlab(page))) {
Christoph Lameter09375022008-05-28 10:32:22 -07003152 BUG_ON(!PageCompound(page));
Catalin Marinase4f7c0b42009-07-07 10:32:59 +01003153 kmemleak_free(x);
Christoph Lameteraadb4bc2007-10-16 01:24:38 -07003154 put_page(page);
3155 return;
3156 }
Eduard - Gabriel Munteanuce71e272008-08-19 20:43:25 +03003157 slab_free(page->slab, page, object, _RET_IP_);
Christoph Lameter81819f02007-05-06 14:49:36 -07003158}
3159EXPORT_SYMBOL(kfree);
3160
Christoph Lameter2086d262007-05-06 14:49:46 -07003161/*
Christoph Lameter672bba32007-05-09 02:32:39 -07003162 * kmem_cache_shrink removes empty slabs from the partial lists and sorts
3163 * the remaining slabs by the number of items in use. The slabs with the
3164 * most items in use come first. New allocations will then fill those up
3165 * and thus they can be removed from the partial lists.
3166 *
3167 * The slabs with the least items are placed last. This results in them
3168 * being allocated from last increasing the chance that the last objects
3169 * are freed in them.
Christoph Lameter2086d262007-05-06 14:49:46 -07003170 */
3171int kmem_cache_shrink(struct kmem_cache *s)
3172{
3173 int node;
3174 int i;
3175 struct kmem_cache_node *n;
3176 struct page *page;
3177 struct page *t;
Christoph Lameter205ab992008-04-14 19:11:40 +03003178 int objects = oo_objects(s->max);
Christoph Lameter2086d262007-05-06 14:49:46 -07003179 struct list_head *slabs_by_inuse =
Christoph Lameter834f3d12008-04-14 19:11:31 +03003180 kmalloc(sizeof(struct list_head) * objects, GFP_KERNEL);
Christoph Lameter2086d262007-05-06 14:49:46 -07003181 unsigned long flags;
3182
3183 if (!slabs_by_inuse)
3184 return -ENOMEM;
3185
3186 flush_all(s);
Christoph Lameterf64dc582007-10-16 01:25:33 -07003187 for_each_node_state(node, N_NORMAL_MEMORY) {
Christoph Lameter2086d262007-05-06 14:49:46 -07003188 n = get_node(s, node);
3189
3190 if (!n->nr_partial)
3191 continue;
3192
Christoph Lameter834f3d12008-04-14 19:11:31 +03003193 for (i = 0; i < objects; i++)
Christoph Lameter2086d262007-05-06 14:49:46 -07003194 INIT_LIST_HEAD(slabs_by_inuse + i);
3195
3196 spin_lock_irqsave(&n->list_lock, flags);
3197
3198 /*
Christoph Lameter672bba32007-05-09 02:32:39 -07003199 * Build lists indexed by the items in use in each slab.
Christoph Lameter2086d262007-05-06 14:49:46 -07003200 *
Christoph Lameter672bba32007-05-09 02:32:39 -07003201 * Note that concurrent frees may occur while we hold the
3202 * list_lock. page->inuse here is the upper limit.
Christoph Lameter2086d262007-05-06 14:49:46 -07003203 */
3204 list_for_each_entry_safe(page, t, &n->partial, lru) {
3205 if (!page->inuse && slab_trylock(page)) {
3206 /*
3207 * Must hold slab lock here because slab_free
3208 * may have freed the last object and be
3209 * waiting to release the slab.
3210 */
Christoph Lameter5cc6eee2011-06-01 12:25:50 -05003211 remove_partial(n, page);
Christoph Lameter2086d262007-05-06 14:49:46 -07003212 slab_unlock(page);
3213 discard_slab(s, page);
3214 } else {
Christoph Lameterfcda3d82007-07-30 13:06:46 -07003215 list_move(&page->lru,
3216 slabs_by_inuse + page->inuse);
Christoph Lameter2086d262007-05-06 14:49:46 -07003217 }
3218 }
3219
Christoph Lameter2086d262007-05-06 14:49:46 -07003220 /*
Christoph Lameter672bba32007-05-09 02:32:39 -07003221 * Rebuild the partial list with the slabs filled up most
3222 * first and the least used slabs at the end.
Christoph Lameter2086d262007-05-06 14:49:46 -07003223 */
Christoph Lameter834f3d12008-04-14 19:11:31 +03003224 for (i = objects - 1; i >= 0; i--)
Christoph Lameter2086d262007-05-06 14:49:46 -07003225 list_splice(slabs_by_inuse + i, n->partial.prev);
3226
Christoph Lameter2086d262007-05-06 14:49:46 -07003227 spin_unlock_irqrestore(&n->list_lock, flags);
3228 }
3229
3230 kfree(slabs_by_inuse);
3231 return 0;
3232}
3233EXPORT_SYMBOL(kmem_cache_shrink);
3234
Pekka Enberg92a5bbc2010-10-06 16:58:16 +03003235#if defined(CONFIG_MEMORY_HOTPLUG)
Yasunori Gotob9049e22007-10-21 16:41:37 -07003236static int slab_mem_going_offline_callback(void *arg)
3237{
3238 struct kmem_cache *s;
3239
3240 down_read(&slub_lock);
3241 list_for_each_entry(s, &slab_caches, list)
3242 kmem_cache_shrink(s);
3243 up_read(&slub_lock);
3244
3245 return 0;
3246}
3247
3248static void slab_mem_offline_callback(void *arg)
3249{
3250 struct kmem_cache_node *n;
3251 struct kmem_cache *s;
3252 struct memory_notify *marg = arg;
3253 int offline_node;
3254
3255 offline_node = marg->status_change_nid;
3256
3257 /*
3258 * If the node still has available memory. we need kmem_cache_node
3259 * for it yet.
3260 */
3261 if (offline_node < 0)
3262 return;
3263
3264 down_read(&slub_lock);
3265 list_for_each_entry(s, &slab_caches, list) {
3266 n = get_node(s, offline_node);
3267 if (n) {
3268 /*
3269 * if n->nr_slabs > 0, slabs still exist on the node
3270 * that is going down. We were unable to free them,
Adam Buchbinderc9404c92009-12-18 15:40:42 -05003271 * and offline_pages() function shouldn't call this
Yasunori Gotob9049e22007-10-21 16:41:37 -07003272 * callback. So, we must fail.
3273 */
Christoph Lameter0f389ec2008-04-14 18:53:02 +03003274 BUG_ON(slabs_node(s, offline_node));
Yasunori Gotob9049e22007-10-21 16:41:37 -07003275
3276 s->node[offline_node] = NULL;
Christoph Lameter8de66a02010-08-25 14:51:14 -05003277 kmem_cache_free(kmem_cache_node, n);
Yasunori Gotob9049e22007-10-21 16:41:37 -07003278 }
3279 }
3280 up_read(&slub_lock);
3281}
3282
3283static int slab_mem_going_online_callback(void *arg)
3284{
3285 struct kmem_cache_node *n;
3286 struct kmem_cache *s;
3287 struct memory_notify *marg = arg;
3288 int nid = marg->status_change_nid;
3289 int ret = 0;
3290
3291 /*
3292 * If the node's memory is already available, then kmem_cache_node is
3293 * already created. Nothing to do.
3294 */
3295 if (nid < 0)
3296 return 0;
3297
3298 /*
Christoph Lameter0121c6192008-04-29 16:11:12 -07003299 * We are bringing a node online. No memory is available yet. We must
Yasunori Gotob9049e22007-10-21 16:41:37 -07003300 * allocate a kmem_cache_node structure in order to bring the node
3301 * online.
3302 */
3303 down_read(&slub_lock);
3304 list_for_each_entry(s, &slab_caches, list) {
3305 /*
3306 * XXX: kmem_cache_alloc_node will fallback to other nodes
3307 * since memory is not yet available from the node that
3308 * is brought up.
3309 */
Christoph Lameter8de66a02010-08-25 14:51:14 -05003310 n = kmem_cache_alloc(kmem_cache_node, GFP_KERNEL);
Yasunori Gotob9049e22007-10-21 16:41:37 -07003311 if (!n) {
3312 ret = -ENOMEM;
3313 goto out;
3314 }
Pekka Enberg5595cff2008-08-05 09:28:47 +03003315 init_kmem_cache_node(n, s);
Yasunori Gotob9049e22007-10-21 16:41:37 -07003316 s->node[nid] = n;
3317 }
3318out:
3319 up_read(&slub_lock);
3320 return ret;
3321}
3322
3323static int slab_memory_callback(struct notifier_block *self,
3324 unsigned long action, void *arg)
3325{
3326 int ret = 0;
3327
3328 switch (action) {
3329 case MEM_GOING_ONLINE:
3330 ret = slab_mem_going_online_callback(arg);
3331 break;
3332 case MEM_GOING_OFFLINE:
3333 ret = slab_mem_going_offline_callback(arg);
3334 break;
3335 case MEM_OFFLINE:
3336 case MEM_CANCEL_ONLINE:
3337 slab_mem_offline_callback(arg);
3338 break;
3339 case MEM_ONLINE:
3340 case MEM_CANCEL_OFFLINE:
3341 break;
3342 }
KAMEZAWA Hiroyukidc19f9d2008-12-01 13:13:48 -08003343 if (ret)
3344 ret = notifier_from_errno(ret);
3345 else
3346 ret = NOTIFY_OK;
Yasunori Gotob9049e22007-10-21 16:41:37 -07003347 return ret;
3348}
3349
3350#endif /* CONFIG_MEMORY_HOTPLUG */
3351
Christoph Lameter81819f02007-05-06 14:49:36 -07003352/********************************************************************
3353 * Basic setup of slabs
3354 *******************************************************************/
3355
Christoph Lameter51df1142010-08-20 12:37:15 -05003356/*
3357 * Used for early kmem_cache structures that were allocated using
3358 * the page allocator
3359 */
3360
3361static void __init kmem_cache_bootstrap_fixup(struct kmem_cache *s)
3362{
3363 int node;
3364
3365 list_add(&s->list, &slab_caches);
3366 s->refcount = -1;
3367
3368 for_each_node_state(node, N_NORMAL_MEMORY) {
3369 struct kmem_cache_node *n = get_node(s, node);
3370 struct page *p;
3371
3372 if (n) {
3373 list_for_each_entry(p, &n->partial, lru)
3374 p->slab = s;
3375
Li Zefan607bf322011-04-12 15:22:26 +08003376#ifdef CONFIG_SLUB_DEBUG
Christoph Lameter51df1142010-08-20 12:37:15 -05003377 list_for_each_entry(p, &n->full, lru)
3378 p->slab = s;
3379#endif
3380 }
3381 }
3382}
3383
Christoph Lameter81819f02007-05-06 14:49:36 -07003384void __init kmem_cache_init(void)
3385{
3386 int i;
Christoph Lameter4b356be2007-06-16 10:16:13 -07003387 int caches = 0;
Christoph Lameter51df1142010-08-20 12:37:15 -05003388 struct kmem_cache *temp_kmem_cache;
3389 int order;
Christoph Lameter51df1142010-08-20 12:37:15 -05003390 struct kmem_cache *temp_kmem_cache_node;
3391 unsigned long kmalloc_size;
3392
3393 kmem_size = offsetof(struct kmem_cache, node) +
3394 nr_node_ids * sizeof(struct kmem_cache_node *);
3395
3396 /* Allocate two kmem_caches from the page allocator */
3397 kmalloc_size = ALIGN(kmem_size, cache_line_size());
3398 order = get_order(2 * kmalloc_size);
3399 kmem_cache = (void *)__get_free_pages(GFP_NOWAIT, order);
3400
Christoph Lameter81819f02007-05-06 14:49:36 -07003401 /*
3402 * Must first have the slab cache available for the allocations of the
Christoph Lameter672bba32007-05-09 02:32:39 -07003403 * struct kmem_cache_node's. There is special bootstrap code in
Christoph Lameter81819f02007-05-06 14:49:36 -07003404 * kmem_cache_open for slab_state == DOWN.
3405 */
Christoph Lameter51df1142010-08-20 12:37:15 -05003406 kmem_cache_node = (void *)kmem_cache + kmalloc_size;
3407
3408 kmem_cache_open(kmem_cache_node, "kmem_cache_node",
3409 sizeof(struct kmem_cache_node),
3410 0, SLAB_HWCACHE_ALIGN | SLAB_PANIC, NULL);
Yasunori Gotob9049e22007-10-21 16:41:37 -07003411
Nadia Derbey0c40ba42008-04-29 01:00:41 -07003412 hotplug_memory_notifier(slab_memory_callback, SLAB_CALLBACK_PRI);
Christoph Lameter81819f02007-05-06 14:49:36 -07003413
3414 /* Able to allocate the per node structures */
3415 slab_state = PARTIAL;
3416
Christoph Lameter51df1142010-08-20 12:37:15 -05003417 temp_kmem_cache = kmem_cache;
3418 kmem_cache_open(kmem_cache, "kmem_cache", kmem_size,
3419 0, SLAB_HWCACHE_ALIGN | SLAB_PANIC, NULL);
3420 kmem_cache = kmem_cache_alloc(kmem_cache, GFP_NOWAIT);
3421 memcpy(kmem_cache, temp_kmem_cache, kmem_size);
Christoph Lameter81819f02007-05-06 14:49:36 -07003422
Christoph Lameter51df1142010-08-20 12:37:15 -05003423 /*
3424 * Allocate kmem_cache_node properly from the kmem_cache slab.
3425 * kmem_cache_node is separately allocated so no need to
3426 * update any list pointers.
3427 */
3428 temp_kmem_cache_node = kmem_cache_node;
Christoph Lameter81819f02007-05-06 14:49:36 -07003429
Christoph Lameter51df1142010-08-20 12:37:15 -05003430 kmem_cache_node = kmem_cache_alloc(kmem_cache, GFP_NOWAIT);
3431 memcpy(kmem_cache_node, temp_kmem_cache_node, kmem_size);
3432
3433 kmem_cache_bootstrap_fixup(kmem_cache_node);
3434
3435 caches++;
Christoph Lameter51df1142010-08-20 12:37:15 -05003436 kmem_cache_bootstrap_fixup(kmem_cache);
3437 caches++;
3438 /* Free temporary boot structure */
3439 free_pages((unsigned long)temp_kmem_cache, order);
3440
3441 /* Now we can use the kmem_cache to allocate kmalloc slabs */
Christoph Lameterf1b26332007-07-17 04:03:26 -07003442
3443 /*
3444 * Patch up the size_index table if we have strange large alignment
3445 * requirements for the kmalloc array. This is only the case for
Christoph Lameter6446faa2008-02-15 23:45:26 -08003446 * MIPS it seems. The standard arches will not generate any code here.
Christoph Lameterf1b26332007-07-17 04:03:26 -07003447 *
3448 * Largest permitted alignment is 256 bytes due to the way we
3449 * handle the index determination for the smaller caches.
3450 *
3451 * Make sure that nothing crazy happens if someone starts tinkering
3452 * around with ARCH_KMALLOC_MINALIGN
3453 */
3454 BUILD_BUG_ON(KMALLOC_MIN_SIZE > 256 ||
3455 (KMALLOC_MIN_SIZE & (KMALLOC_MIN_SIZE - 1)));
3456
Aaro Koskinenacdfcd02009-08-28 14:28:54 +03003457 for (i = 8; i < KMALLOC_MIN_SIZE; i += 8) {
3458 int elem = size_index_elem(i);
3459 if (elem >= ARRAY_SIZE(size_index))
3460 break;
3461 size_index[elem] = KMALLOC_SHIFT_LOW;
3462 }
Christoph Lameterf1b26332007-07-17 04:03:26 -07003463
Aaro Koskinenacdfcd02009-08-28 14:28:54 +03003464 if (KMALLOC_MIN_SIZE == 64) {
3465 /*
3466 * The 96 byte size cache is not used if the alignment
3467 * is 64 byte.
3468 */
3469 for (i = 64 + 8; i <= 96; i += 8)
3470 size_index[size_index_elem(i)] = 7;
3471 } else if (KMALLOC_MIN_SIZE == 128) {
Christoph Lameter41d54d32008-07-03 09:14:26 -05003472 /*
3473 * The 192 byte sized cache is not used if the alignment
3474 * is 128 byte. Redirect kmalloc to use the 256 byte cache
3475 * instead.
3476 */
3477 for (i = 128 + 8; i <= 192; i += 8)
Aaro Koskinenacdfcd02009-08-28 14:28:54 +03003478 size_index[size_index_elem(i)] = 8;
Christoph Lameter41d54d32008-07-03 09:14:26 -05003479 }
3480
Christoph Lameter51df1142010-08-20 12:37:15 -05003481 /* Caches that are not of the two-to-the-power-of size */
3482 if (KMALLOC_MIN_SIZE <= 32) {
3483 kmalloc_caches[1] = create_kmalloc_cache("kmalloc-96", 96, 0);
3484 caches++;
3485 }
3486
3487 if (KMALLOC_MIN_SIZE <= 64) {
3488 kmalloc_caches[2] = create_kmalloc_cache("kmalloc-192", 192, 0);
3489 caches++;
3490 }
3491
3492 for (i = KMALLOC_SHIFT_LOW; i < SLUB_PAGE_SHIFT; i++) {
3493 kmalloc_caches[i] = create_kmalloc_cache("kmalloc", 1 << i, 0);
3494 caches++;
3495 }
3496
Christoph Lameter81819f02007-05-06 14:49:36 -07003497 slab_state = UP;
3498
3499 /* Provide the correct kmalloc names now that the caches are up */
Pekka Enberg84c1cf62010-09-14 23:21:12 +03003500 if (KMALLOC_MIN_SIZE <= 32) {
3501 kmalloc_caches[1]->name = kstrdup(kmalloc_caches[1]->name, GFP_NOWAIT);
3502 BUG_ON(!kmalloc_caches[1]->name);
3503 }
3504
3505 if (KMALLOC_MIN_SIZE <= 64) {
3506 kmalloc_caches[2]->name = kstrdup(kmalloc_caches[2]->name, GFP_NOWAIT);
3507 BUG_ON(!kmalloc_caches[2]->name);
3508 }
3509
Christoph Lameterd7278bd2010-07-09 14:07:12 -05003510 for (i = KMALLOC_SHIFT_LOW; i < SLUB_PAGE_SHIFT; i++) {
3511 char *s = kasprintf(GFP_NOWAIT, "kmalloc-%d", 1 << i);
3512
3513 BUG_ON(!s);
Christoph Lameter51df1142010-08-20 12:37:15 -05003514 kmalloc_caches[i]->name = s;
Christoph Lameterd7278bd2010-07-09 14:07:12 -05003515 }
Christoph Lameter81819f02007-05-06 14:49:36 -07003516
3517#ifdef CONFIG_SMP
3518 register_cpu_notifier(&slab_notifier);
Christoph Lameter9dfc6e62009-12-18 16:26:20 -06003519#endif
Christoph Lameter81819f02007-05-06 14:49:36 -07003520
Christoph Lameter55136592010-08-20 12:37:13 -05003521#ifdef CONFIG_ZONE_DMA
Christoph Lameter51df1142010-08-20 12:37:15 -05003522 for (i = 0; i < SLUB_PAGE_SHIFT; i++) {
3523 struct kmem_cache *s = kmalloc_caches[i];
Christoph Lameter55136592010-08-20 12:37:13 -05003524
Christoph Lameter51df1142010-08-20 12:37:15 -05003525 if (s && s->size) {
Christoph Lameter55136592010-08-20 12:37:13 -05003526 char *name = kasprintf(GFP_NOWAIT,
3527 "dma-kmalloc-%d", s->objsize);
3528
3529 BUG_ON(!name);
Christoph Lameter51df1142010-08-20 12:37:15 -05003530 kmalloc_dma_caches[i] = create_kmalloc_cache(name,
3531 s->objsize, SLAB_CACHE_DMA);
Christoph Lameter55136592010-08-20 12:37:13 -05003532 }
3533 }
3534#endif
Ingo Molnar3adbefe2008-02-05 17:57:39 -08003535 printk(KERN_INFO
3536 "SLUB: Genslabs=%d, HWalign=%d, Order=%d-%d, MinObjects=%d,"
Christoph Lameter4b356be2007-06-16 10:16:13 -07003537 " CPUs=%d, Nodes=%d\n",
3538 caches, cache_line_size(),
Christoph Lameter81819f02007-05-06 14:49:36 -07003539 slub_min_order, slub_max_order, slub_min_objects,
3540 nr_cpu_ids, nr_node_ids);
3541}
3542
Pekka Enberg7e85ee02009-06-12 14:03:06 +03003543void __init kmem_cache_init_late(void)
3544{
Pekka Enberg7e85ee02009-06-12 14:03:06 +03003545}
3546
Christoph Lameter81819f02007-05-06 14:49:36 -07003547/*
3548 * Find a mergeable slab cache
3549 */
3550static int slab_unmergeable(struct kmem_cache *s)
3551{
3552 if (slub_nomerge || (s->flags & SLUB_NEVER_MERGE))
3553 return 1;
3554
Christoph Lameterc59def9f2007-05-16 22:10:50 -07003555 if (s->ctor)
Christoph Lameter81819f02007-05-06 14:49:36 -07003556 return 1;
3557
Christoph Lameter8ffa6872007-05-31 00:40:51 -07003558 /*
3559 * We may have set a slab to be unmergeable during bootstrap.
3560 */
3561 if (s->refcount < 0)
3562 return 1;
3563
Christoph Lameter81819f02007-05-06 14:49:36 -07003564 return 0;
3565}
3566
3567static struct kmem_cache *find_mergeable(size_t size,
Christoph Lameterba0268a2007-09-11 15:24:11 -07003568 size_t align, unsigned long flags, const char *name,
Alexey Dobriyan51cc5062008-07-25 19:45:34 -07003569 void (*ctor)(void *))
Christoph Lameter81819f02007-05-06 14:49:36 -07003570{
Christoph Lameter5b95a4a2007-07-17 04:03:19 -07003571 struct kmem_cache *s;
Christoph Lameter81819f02007-05-06 14:49:36 -07003572
3573 if (slub_nomerge || (flags & SLUB_NEVER_MERGE))
3574 return NULL;
3575
Christoph Lameterc59def9f2007-05-16 22:10:50 -07003576 if (ctor)
Christoph Lameter81819f02007-05-06 14:49:36 -07003577 return NULL;
3578
3579 size = ALIGN(size, sizeof(void *));
3580 align = calculate_alignment(flags, align, size);
3581 size = ALIGN(size, align);
Christoph Lameterba0268a2007-09-11 15:24:11 -07003582 flags = kmem_cache_flags(size, flags, name, NULL);
Christoph Lameter81819f02007-05-06 14:49:36 -07003583
Christoph Lameter5b95a4a2007-07-17 04:03:19 -07003584 list_for_each_entry(s, &slab_caches, list) {
Christoph Lameter81819f02007-05-06 14:49:36 -07003585 if (slab_unmergeable(s))
3586 continue;
3587
3588 if (size > s->size)
3589 continue;
3590
Christoph Lameterba0268a2007-09-11 15:24:11 -07003591 if ((flags & SLUB_MERGE_SAME) != (s->flags & SLUB_MERGE_SAME))
Christoph Lameter81819f02007-05-06 14:49:36 -07003592 continue;
3593 /*
3594 * Check if alignment is compatible.
3595 * Courtesy of Adrian Drzewiecki
3596 */
Pekka Enberg06428782008-01-07 23:20:27 -08003597 if ((s->size & ~(align - 1)) != s->size)
Christoph Lameter81819f02007-05-06 14:49:36 -07003598 continue;
3599
3600 if (s->size - size >= sizeof(void *))
3601 continue;
3602
3603 return s;
3604 }
3605 return NULL;
3606}
3607
3608struct kmem_cache *kmem_cache_create(const char *name, size_t size,
Alexey Dobriyan51cc5062008-07-25 19:45:34 -07003609 size_t align, unsigned long flags, void (*ctor)(void *))
Christoph Lameter81819f02007-05-06 14:49:36 -07003610{
3611 struct kmem_cache *s;
Pekka Enberg84c1cf62010-09-14 23:21:12 +03003612 char *n;
Christoph Lameter81819f02007-05-06 14:49:36 -07003613
Benjamin Herrenschmidtfe1ff492009-09-21 17:02:30 -07003614 if (WARN_ON(!name))
3615 return NULL;
3616
Christoph Lameter81819f02007-05-06 14:49:36 -07003617 down_write(&slub_lock);
Christoph Lameterba0268a2007-09-11 15:24:11 -07003618 s = find_mergeable(size, align, flags, name, ctor);
Christoph Lameter81819f02007-05-06 14:49:36 -07003619 if (s) {
3620 s->refcount++;
3621 /*
3622 * Adjust the object sizes so that we clear
3623 * the complete object on kzalloc.
3624 */
3625 s->objsize = max(s->objsize, (int)size);
3626 s->inuse = max_t(int, s->inuse, ALIGN(size, sizeof(void *)));
Christoph Lameter6446faa2008-02-15 23:45:26 -08003627
David Rientjes7b8f3b62008-12-17 22:09:46 -08003628 if (sysfs_slab_alias(s, name)) {
David Rientjes7b8f3b62008-12-17 22:09:46 -08003629 s->refcount--;
Christoph Lameter81819f02007-05-06 14:49:36 -07003630 goto err;
David Rientjes7b8f3b62008-12-17 22:09:46 -08003631 }
Christoph Lameter2bce6482010-07-19 11:39:11 -05003632 up_write(&slub_lock);
Christoph Lametera0e1d1b2007-07-17 04:03:31 -07003633 return s;
3634 }
Christoph Lameter6446faa2008-02-15 23:45:26 -08003635
Pekka Enberg84c1cf62010-09-14 23:21:12 +03003636 n = kstrdup(name, GFP_KERNEL);
3637 if (!n)
3638 goto err;
3639
Christoph Lametera0e1d1b2007-07-17 04:03:31 -07003640 s = kmalloc(kmem_size, GFP_KERNEL);
3641 if (s) {
Pekka Enberg84c1cf62010-09-14 23:21:12 +03003642 if (kmem_cache_open(s, n,
Christoph Lameterc59def9f2007-05-16 22:10:50 -07003643 size, align, flags, ctor)) {
Christoph Lameter81819f02007-05-06 14:49:36 -07003644 list_add(&s->list, &slab_caches);
David Rientjes7b8f3b62008-12-17 22:09:46 -08003645 if (sysfs_slab_add(s)) {
David Rientjes7b8f3b62008-12-17 22:09:46 -08003646 list_del(&s->list);
Pekka Enberg84c1cf62010-09-14 23:21:12 +03003647 kfree(n);
David Rientjes7b8f3b62008-12-17 22:09:46 -08003648 kfree(s);
Christoph Lametera0e1d1b2007-07-17 04:03:31 -07003649 goto err;
David Rientjes7b8f3b62008-12-17 22:09:46 -08003650 }
Christoph Lameter2bce6482010-07-19 11:39:11 -05003651 up_write(&slub_lock);
Christoph Lametera0e1d1b2007-07-17 04:03:31 -07003652 return s;
3653 }
Pekka Enberg84c1cf62010-09-14 23:21:12 +03003654 kfree(n);
Christoph Lametera0e1d1b2007-07-17 04:03:31 -07003655 kfree(s);
Christoph Lameter81819f02007-05-06 14:49:36 -07003656 }
Pavel Emelyanov68cee4f2010-10-28 13:50:37 +04003657err:
Christoph Lameter81819f02007-05-06 14:49:36 -07003658 up_write(&slub_lock);
Christoph Lameter81819f02007-05-06 14:49:36 -07003659
Christoph Lameter81819f02007-05-06 14:49:36 -07003660 if (flags & SLAB_PANIC)
3661 panic("Cannot create slabcache %s\n", name);
3662 else
3663 s = NULL;
3664 return s;
3665}
3666EXPORT_SYMBOL(kmem_cache_create);
3667
Christoph Lameter81819f02007-05-06 14:49:36 -07003668#ifdef CONFIG_SMP
Christoph Lameter27390bc2007-06-01 00:47:09 -07003669/*
Christoph Lameter672bba32007-05-09 02:32:39 -07003670 * Use the cpu notifier to insure that the cpu slabs are flushed when
3671 * necessary.
Christoph Lameter81819f02007-05-06 14:49:36 -07003672 */
3673static int __cpuinit slab_cpuup_callback(struct notifier_block *nfb,
3674 unsigned long action, void *hcpu)
3675{
3676 long cpu = (long)hcpu;
Christoph Lameter5b95a4a2007-07-17 04:03:19 -07003677 struct kmem_cache *s;
3678 unsigned long flags;
Christoph Lameter81819f02007-05-06 14:49:36 -07003679
3680 switch (action) {
3681 case CPU_UP_CANCELED:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07003682 case CPU_UP_CANCELED_FROZEN:
Christoph Lameter81819f02007-05-06 14:49:36 -07003683 case CPU_DEAD:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07003684 case CPU_DEAD_FROZEN:
Christoph Lameter5b95a4a2007-07-17 04:03:19 -07003685 down_read(&slub_lock);
3686 list_for_each_entry(s, &slab_caches, list) {
3687 local_irq_save(flags);
3688 __flush_cpu_slab(s, cpu);
3689 local_irq_restore(flags);
3690 }
3691 up_read(&slub_lock);
Christoph Lameter81819f02007-05-06 14:49:36 -07003692 break;
3693 default:
3694 break;
3695 }
3696 return NOTIFY_OK;
3697}
3698
Pekka Enberg06428782008-01-07 23:20:27 -08003699static struct notifier_block __cpuinitdata slab_notifier = {
Ingo Molnar3adbefe2008-02-05 17:57:39 -08003700 .notifier_call = slab_cpuup_callback
Pekka Enberg06428782008-01-07 23:20:27 -08003701};
Christoph Lameter81819f02007-05-06 14:49:36 -07003702
3703#endif
3704
Eduard - Gabriel Munteanuce71e272008-08-19 20:43:25 +03003705void *__kmalloc_track_caller(size_t size, gfp_t gfpflags, unsigned long caller)
Christoph Lameter81819f02007-05-06 14:49:36 -07003706{
Christoph Lameteraadb4bc2007-10-16 01:24:38 -07003707 struct kmem_cache *s;
Eduard - Gabriel Munteanu94b528d2008-08-24 20:49:35 +03003708 void *ret;
Christoph Lameteraadb4bc2007-10-16 01:24:38 -07003709
Christoph Lameterffadd4d2009-02-17 12:05:07 -05003710 if (unlikely(size > SLUB_MAX_SIZE))
Pekka Enbergeada35e2008-02-11 22:47:46 +02003711 return kmalloc_large(size, gfpflags);
3712
Christoph Lameteraadb4bc2007-10-16 01:24:38 -07003713 s = get_slab(size, gfpflags);
Christoph Lameter81819f02007-05-06 14:49:36 -07003714
Satyam Sharma2408c552007-10-16 01:24:44 -07003715 if (unlikely(ZERO_OR_NULL_PTR(s)))
Christoph Lameter6cb8f912007-07-17 04:03:22 -07003716 return s;
Christoph Lameter81819f02007-05-06 14:49:36 -07003717
Christoph Lameter2154a332010-07-09 14:07:10 -05003718 ret = slab_alloc(s, gfpflags, NUMA_NO_NODE, caller);
Eduard - Gabriel Munteanu94b528d2008-08-24 20:49:35 +03003719
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003720 /* Honor the call site pointer we received. */
Eduard - Gabriel Munteanuca2b84c2009-03-23 15:12:24 +02003721 trace_kmalloc(caller, ret, size, s->size, gfpflags);
Eduard - Gabriel Munteanu94b528d2008-08-24 20:49:35 +03003722
3723 return ret;
Christoph Lameter81819f02007-05-06 14:49:36 -07003724}
3725
Namhyung Kim5d1f57e2010-09-29 21:02:15 +09003726#ifdef CONFIG_NUMA
Christoph Lameter81819f02007-05-06 14:49:36 -07003727void *__kmalloc_node_track_caller(size_t size, gfp_t gfpflags,
Eduard - Gabriel Munteanuce71e272008-08-19 20:43:25 +03003728 int node, unsigned long caller)
Christoph Lameter81819f02007-05-06 14:49:36 -07003729{
Christoph Lameteraadb4bc2007-10-16 01:24:38 -07003730 struct kmem_cache *s;
Eduard - Gabriel Munteanu94b528d2008-08-24 20:49:35 +03003731 void *ret;
Christoph Lameteraadb4bc2007-10-16 01:24:38 -07003732
Xiaotian Fengd3e14aa2010-04-08 17:26:44 +08003733 if (unlikely(size > SLUB_MAX_SIZE)) {
3734 ret = kmalloc_large_node(size, gfpflags, node);
3735
3736 trace_kmalloc_node(caller, ret,
3737 size, PAGE_SIZE << get_order(size),
3738 gfpflags, node);
3739
3740 return ret;
3741 }
Pekka Enbergeada35e2008-02-11 22:47:46 +02003742
Christoph Lameteraadb4bc2007-10-16 01:24:38 -07003743 s = get_slab(size, gfpflags);
Christoph Lameter81819f02007-05-06 14:49:36 -07003744
Satyam Sharma2408c552007-10-16 01:24:44 -07003745 if (unlikely(ZERO_OR_NULL_PTR(s)))
Christoph Lameter6cb8f912007-07-17 04:03:22 -07003746 return s;
Christoph Lameter81819f02007-05-06 14:49:36 -07003747
Eduard - Gabriel Munteanu94b528d2008-08-24 20:49:35 +03003748 ret = slab_alloc(s, gfpflags, node, caller);
3749
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003750 /* Honor the call site pointer we received. */
Eduard - Gabriel Munteanuca2b84c2009-03-23 15:12:24 +02003751 trace_kmalloc_node(caller, ret, size, s->size, gfpflags, node);
Eduard - Gabriel Munteanu94b528d2008-08-24 20:49:35 +03003752
3753 return ret;
Christoph Lameter81819f02007-05-06 14:49:36 -07003754}
Namhyung Kim5d1f57e2010-09-29 21:02:15 +09003755#endif
Christoph Lameter81819f02007-05-06 14:49:36 -07003756
Christoph Lameterab4d5ed2010-10-05 13:57:26 -05003757#ifdef CONFIG_SYSFS
Christoph Lameter205ab992008-04-14 19:11:40 +03003758static int count_inuse(struct page *page)
3759{
3760 return page->inuse;
3761}
3762
3763static int count_total(struct page *page)
3764{
3765 return page->objects;
3766}
Christoph Lameterab4d5ed2010-10-05 13:57:26 -05003767#endif
Christoph Lameter205ab992008-04-14 19:11:40 +03003768
Christoph Lameterab4d5ed2010-10-05 13:57:26 -05003769#ifdef CONFIG_SLUB_DEBUG
Christoph Lameter434e2452007-07-17 04:03:30 -07003770static int validate_slab(struct kmem_cache *s, struct page *page,
3771 unsigned long *map)
Christoph Lameter53e15af2007-05-06 14:49:43 -07003772{
3773 void *p;
Christoph Lametera973e9d2008-03-01 13:40:44 -08003774 void *addr = page_address(page);
Christoph Lameter53e15af2007-05-06 14:49:43 -07003775
3776 if (!check_slab(s, page) ||
3777 !on_freelist(s, page, NULL))
3778 return 0;
3779
3780 /* Now we know that a valid freelist exists */
Christoph Lameter39b26462008-04-14 19:11:30 +03003781 bitmap_zero(map, page->objects);
Christoph Lameter53e15af2007-05-06 14:49:43 -07003782
Christoph Lameter5f80b132011-04-15 14:48:13 -05003783 get_map(s, page, map);
3784 for_each_object(p, s, addr, page->objects) {
3785 if (test_bit(slab_index(p, s, addr), map))
3786 if (!check_object(s, page, p, SLUB_RED_INACTIVE))
3787 return 0;
Christoph Lameter53e15af2007-05-06 14:49:43 -07003788 }
3789
Christoph Lameter224a88b2008-04-14 19:11:31 +03003790 for_each_object(p, s, addr, page->objects)
Christoph Lameter7656c722007-05-09 02:32:40 -07003791 if (!test_bit(slab_index(p, s, addr), map))
Tero Roponen37d57442010-12-01 20:04:20 +02003792 if (!check_object(s, page, p, SLUB_RED_ACTIVE))
Christoph Lameter53e15af2007-05-06 14:49:43 -07003793 return 0;
3794 return 1;
3795}
3796
Christoph Lameter434e2452007-07-17 04:03:30 -07003797static void validate_slab_slab(struct kmem_cache *s, struct page *page,
3798 unsigned long *map)
Christoph Lameter53e15af2007-05-06 14:49:43 -07003799{
3800 if (slab_trylock(page)) {
Christoph Lameter434e2452007-07-17 04:03:30 -07003801 validate_slab(s, page, map);
Christoph Lameter53e15af2007-05-06 14:49:43 -07003802 slab_unlock(page);
3803 } else
3804 printk(KERN_INFO "SLUB %s: Skipped busy slab 0x%p\n",
3805 s->name, page);
Christoph Lameter53e15af2007-05-06 14:49:43 -07003806}
3807
Christoph Lameter434e2452007-07-17 04:03:30 -07003808static int validate_slab_node(struct kmem_cache *s,
3809 struct kmem_cache_node *n, unsigned long *map)
Christoph Lameter53e15af2007-05-06 14:49:43 -07003810{
3811 unsigned long count = 0;
3812 struct page *page;
3813 unsigned long flags;
3814
3815 spin_lock_irqsave(&n->list_lock, flags);
3816
3817 list_for_each_entry(page, &n->partial, lru) {
Christoph Lameter434e2452007-07-17 04:03:30 -07003818 validate_slab_slab(s, page, map);
Christoph Lameter53e15af2007-05-06 14:49:43 -07003819 count++;
3820 }
3821 if (count != n->nr_partial)
3822 printk(KERN_ERR "SLUB %s: %ld partial slabs counted but "
3823 "counter=%ld\n", s->name, count, n->nr_partial);
3824
3825 if (!(s->flags & SLAB_STORE_USER))
3826 goto out;
3827
3828 list_for_each_entry(page, &n->full, lru) {
Christoph Lameter434e2452007-07-17 04:03:30 -07003829 validate_slab_slab(s, page, map);
Christoph Lameter53e15af2007-05-06 14:49:43 -07003830 count++;
3831 }
3832 if (count != atomic_long_read(&n->nr_slabs))
3833 printk(KERN_ERR "SLUB: %s %ld slabs counted but "
3834 "counter=%ld\n", s->name, count,
3835 atomic_long_read(&n->nr_slabs));
3836
3837out:
3838 spin_unlock_irqrestore(&n->list_lock, flags);
3839 return count;
3840}
3841
Christoph Lameter434e2452007-07-17 04:03:30 -07003842static long validate_slab_cache(struct kmem_cache *s)
Christoph Lameter53e15af2007-05-06 14:49:43 -07003843{
3844 int node;
3845 unsigned long count = 0;
Christoph Lameter205ab992008-04-14 19:11:40 +03003846 unsigned long *map = kmalloc(BITS_TO_LONGS(oo_objects(s->max)) *
Christoph Lameter434e2452007-07-17 04:03:30 -07003847 sizeof(unsigned long), GFP_KERNEL);
3848
3849 if (!map)
3850 return -ENOMEM;
Christoph Lameter53e15af2007-05-06 14:49:43 -07003851
3852 flush_all(s);
Christoph Lameterf64dc582007-10-16 01:25:33 -07003853 for_each_node_state(node, N_NORMAL_MEMORY) {
Christoph Lameter53e15af2007-05-06 14:49:43 -07003854 struct kmem_cache_node *n = get_node(s, node);
3855
Christoph Lameter434e2452007-07-17 04:03:30 -07003856 count += validate_slab_node(s, n, map);
Christoph Lameter53e15af2007-05-06 14:49:43 -07003857 }
Christoph Lameter434e2452007-07-17 04:03:30 -07003858 kfree(map);
Christoph Lameter53e15af2007-05-06 14:49:43 -07003859 return count;
3860}
Christoph Lameter88a420e2007-05-06 14:49:45 -07003861/*
Christoph Lameter672bba32007-05-09 02:32:39 -07003862 * Generate lists of code addresses where slabcache objects are allocated
Christoph Lameter88a420e2007-05-06 14:49:45 -07003863 * and freed.
3864 */
3865
3866struct location {
3867 unsigned long count;
Eduard - Gabriel Munteanuce71e272008-08-19 20:43:25 +03003868 unsigned long addr;
Christoph Lameter45edfa52007-05-09 02:32:45 -07003869 long long sum_time;
3870 long min_time;
3871 long max_time;
3872 long min_pid;
3873 long max_pid;
Rusty Russell174596a2009-01-01 10:12:29 +10303874 DECLARE_BITMAP(cpus, NR_CPUS);
Christoph Lameter45edfa52007-05-09 02:32:45 -07003875 nodemask_t nodes;
Christoph Lameter88a420e2007-05-06 14:49:45 -07003876};
3877
3878struct loc_track {
3879 unsigned long max;
3880 unsigned long count;
3881 struct location *loc;
3882};
3883
3884static void free_loc_track(struct loc_track *t)
3885{
3886 if (t->max)
3887 free_pages((unsigned long)t->loc,
3888 get_order(sizeof(struct location) * t->max));
3889}
3890
Christoph Lameter68dff6a2007-07-17 04:03:20 -07003891static int alloc_loc_track(struct loc_track *t, unsigned long max, gfp_t flags)
Christoph Lameter88a420e2007-05-06 14:49:45 -07003892{
3893 struct location *l;
3894 int order;
3895
Christoph Lameter88a420e2007-05-06 14:49:45 -07003896 order = get_order(sizeof(struct location) * max);
3897
Christoph Lameter68dff6a2007-07-17 04:03:20 -07003898 l = (void *)__get_free_pages(flags, order);
Christoph Lameter88a420e2007-05-06 14:49:45 -07003899 if (!l)
3900 return 0;
3901
3902 if (t->count) {
3903 memcpy(l, t->loc, sizeof(struct location) * t->count);
3904 free_loc_track(t);
3905 }
3906 t->max = max;
3907 t->loc = l;
3908 return 1;
3909}
3910
3911static int add_location(struct loc_track *t, struct kmem_cache *s,
Christoph Lameter45edfa52007-05-09 02:32:45 -07003912 const struct track *track)
Christoph Lameter88a420e2007-05-06 14:49:45 -07003913{
3914 long start, end, pos;
3915 struct location *l;
Eduard - Gabriel Munteanuce71e272008-08-19 20:43:25 +03003916 unsigned long caddr;
Christoph Lameter45edfa52007-05-09 02:32:45 -07003917 unsigned long age = jiffies - track->when;
Christoph Lameter88a420e2007-05-06 14:49:45 -07003918
3919 start = -1;
3920 end = t->count;
3921
3922 for ( ; ; ) {
3923 pos = start + (end - start + 1) / 2;
3924
3925 /*
3926 * There is nothing at "end". If we end up there
3927 * we need to add something to before end.
3928 */
3929 if (pos == end)
3930 break;
3931
3932 caddr = t->loc[pos].addr;
Christoph Lameter45edfa52007-05-09 02:32:45 -07003933 if (track->addr == caddr) {
3934
3935 l = &t->loc[pos];
3936 l->count++;
3937 if (track->when) {
3938 l->sum_time += age;
3939 if (age < l->min_time)
3940 l->min_time = age;
3941 if (age > l->max_time)
3942 l->max_time = age;
3943
3944 if (track->pid < l->min_pid)
3945 l->min_pid = track->pid;
3946 if (track->pid > l->max_pid)
3947 l->max_pid = track->pid;
3948
Rusty Russell174596a2009-01-01 10:12:29 +10303949 cpumask_set_cpu(track->cpu,
3950 to_cpumask(l->cpus));
Christoph Lameter45edfa52007-05-09 02:32:45 -07003951 }
3952 node_set(page_to_nid(virt_to_page(track)), l->nodes);
Christoph Lameter88a420e2007-05-06 14:49:45 -07003953 return 1;
3954 }
3955
Christoph Lameter45edfa52007-05-09 02:32:45 -07003956 if (track->addr < caddr)
Christoph Lameter88a420e2007-05-06 14:49:45 -07003957 end = pos;
3958 else
3959 start = pos;
3960 }
3961
3962 /*
Christoph Lameter672bba32007-05-09 02:32:39 -07003963 * Not found. Insert new tracking element.
Christoph Lameter88a420e2007-05-06 14:49:45 -07003964 */
Christoph Lameter68dff6a2007-07-17 04:03:20 -07003965 if (t->count >= t->max && !alloc_loc_track(t, 2 * t->max, GFP_ATOMIC))
Christoph Lameter88a420e2007-05-06 14:49:45 -07003966 return 0;
3967
3968 l = t->loc + pos;
3969 if (pos < t->count)
3970 memmove(l + 1, l,
3971 (t->count - pos) * sizeof(struct location));
3972 t->count++;
3973 l->count = 1;
Christoph Lameter45edfa52007-05-09 02:32:45 -07003974 l->addr = track->addr;
3975 l->sum_time = age;
3976 l->min_time = age;
3977 l->max_time = age;
3978 l->min_pid = track->pid;
3979 l->max_pid = track->pid;
Rusty Russell174596a2009-01-01 10:12:29 +10303980 cpumask_clear(to_cpumask(l->cpus));
3981 cpumask_set_cpu(track->cpu, to_cpumask(l->cpus));
Christoph Lameter45edfa52007-05-09 02:32:45 -07003982 nodes_clear(l->nodes);
3983 node_set(page_to_nid(virt_to_page(track)), l->nodes);
Christoph Lameter88a420e2007-05-06 14:49:45 -07003984 return 1;
3985}
3986
3987static void process_slab(struct loc_track *t, struct kmem_cache *s,
Eric Dumazetbbd7d572010-03-24 22:25:47 +01003988 struct page *page, enum track_item alloc,
Namhyung Kima5dd5c12010-09-29 21:02:13 +09003989 unsigned long *map)
Christoph Lameter88a420e2007-05-06 14:49:45 -07003990{
Christoph Lametera973e9d2008-03-01 13:40:44 -08003991 void *addr = page_address(page);
Christoph Lameter88a420e2007-05-06 14:49:45 -07003992 void *p;
3993
Christoph Lameter39b26462008-04-14 19:11:30 +03003994 bitmap_zero(map, page->objects);
Christoph Lameter5f80b132011-04-15 14:48:13 -05003995 get_map(s, page, map);
Christoph Lameter88a420e2007-05-06 14:49:45 -07003996
Christoph Lameter224a88b2008-04-14 19:11:31 +03003997 for_each_object(p, s, addr, page->objects)
Christoph Lameter45edfa52007-05-09 02:32:45 -07003998 if (!test_bit(slab_index(p, s, addr), map))
3999 add_location(t, s, get_track(s, p, alloc));
Christoph Lameter88a420e2007-05-06 14:49:45 -07004000}
4001
4002static int list_locations(struct kmem_cache *s, char *buf,
4003 enum track_item alloc)
4004{
Harvey Harrisone374d482008-01-31 15:20:50 -08004005 int len = 0;
Christoph Lameter88a420e2007-05-06 14:49:45 -07004006 unsigned long i;
Christoph Lameter68dff6a2007-07-17 04:03:20 -07004007 struct loc_track t = { 0, 0, NULL };
Christoph Lameter88a420e2007-05-06 14:49:45 -07004008 int node;
Eric Dumazetbbd7d572010-03-24 22:25:47 +01004009 unsigned long *map = kmalloc(BITS_TO_LONGS(oo_objects(s->max)) *
4010 sizeof(unsigned long), GFP_KERNEL);
Christoph Lameter88a420e2007-05-06 14:49:45 -07004011
Eric Dumazetbbd7d572010-03-24 22:25:47 +01004012 if (!map || !alloc_loc_track(&t, PAGE_SIZE / sizeof(struct location),
4013 GFP_TEMPORARY)) {
4014 kfree(map);
Christoph Lameter68dff6a2007-07-17 04:03:20 -07004015 return sprintf(buf, "Out of memory\n");
Eric Dumazetbbd7d572010-03-24 22:25:47 +01004016 }
Christoph Lameter88a420e2007-05-06 14:49:45 -07004017 /* Push back cpu slabs */
4018 flush_all(s);
4019
Christoph Lameterf64dc582007-10-16 01:25:33 -07004020 for_each_node_state(node, N_NORMAL_MEMORY) {
Christoph Lameter88a420e2007-05-06 14:49:45 -07004021 struct kmem_cache_node *n = get_node(s, node);
4022 unsigned long flags;
4023 struct page *page;
4024
Christoph Lameter9e869432007-08-22 14:01:56 -07004025 if (!atomic_long_read(&n->nr_slabs))
Christoph Lameter88a420e2007-05-06 14:49:45 -07004026 continue;
4027
4028 spin_lock_irqsave(&n->list_lock, flags);
4029 list_for_each_entry(page, &n->partial, lru)
Eric Dumazetbbd7d572010-03-24 22:25:47 +01004030 process_slab(&t, s, page, alloc, map);
Christoph Lameter88a420e2007-05-06 14:49:45 -07004031 list_for_each_entry(page, &n->full, lru)
Eric Dumazetbbd7d572010-03-24 22:25:47 +01004032 process_slab(&t, s, page, alloc, map);
Christoph Lameter88a420e2007-05-06 14:49:45 -07004033 spin_unlock_irqrestore(&n->list_lock, flags);
4034 }
4035
4036 for (i = 0; i < t.count; i++) {
Christoph Lameter45edfa52007-05-09 02:32:45 -07004037 struct location *l = &t.loc[i];
Christoph Lameter88a420e2007-05-06 14:49:45 -07004038
Hugh Dickins9c246242008-12-09 13:14:27 -08004039 if (len > PAGE_SIZE - KSYM_SYMBOL_LEN - 100)
Christoph Lameter88a420e2007-05-06 14:49:45 -07004040 break;
Harvey Harrisone374d482008-01-31 15:20:50 -08004041 len += sprintf(buf + len, "%7ld ", l->count);
Christoph Lameter45edfa52007-05-09 02:32:45 -07004042
4043 if (l->addr)
Joe Perches62c70bc2011-01-13 15:45:52 -08004044 len += sprintf(buf + len, "%pS", (void *)l->addr);
Christoph Lameter88a420e2007-05-06 14:49:45 -07004045 else
Harvey Harrisone374d482008-01-31 15:20:50 -08004046 len += sprintf(buf + len, "<not-available>");
Christoph Lameter45edfa52007-05-09 02:32:45 -07004047
4048 if (l->sum_time != l->min_time) {
Harvey Harrisone374d482008-01-31 15:20:50 -08004049 len += sprintf(buf + len, " age=%ld/%ld/%ld",
Roman Zippelf8bd2252008-05-01 04:34:31 -07004050 l->min_time,
4051 (long)div_u64(l->sum_time, l->count),
4052 l->max_time);
Christoph Lameter45edfa52007-05-09 02:32:45 -07004053 } else
Harvey Harrisone374d482008-01-31 15:20:50 -08004054 len += sprintf(buf + len, " age=%ld",
Christoph Lameter45edfa52007-05-09 02:32:45 -07004055 l->min_time);
4056
4057 if (l->min_pid != l->max_pid)
Harvey Harrisone374d482008-01-31 15:20:50 -08004058 len += sprintf(buf + len, " pid=%ld-%ld",
Christoph Lameter45edfa52007-05-09 02:32:45 -07004059 l->min_pid, l->max_pid);
4060 else
Harvey Harrisone374d482008-01-31 15:20:50 -08004061 len += sprintf(buf + len, " pid=%ld",
Christoph Lameter45edfa52007-05-09 02:32:45 -07004062 l->min_pid);
4063
Rusty Russell174596a2009-01-01 10:12:29 +10304064 if (num_online_cpus() > 1 &&
4065 !cpumask_empty(to_cpumask(l->cpus)) &&
Harvey Harrisone374d482008-01-31 15:20:50 -08004066 len < PAGE_SIZE - 60) {
4067 len += sprintf(buf + len, " cpus=");
4068 len += cpulist_scnprintf(buf + len, PAGE_SIZE - len - 50,
Rusty Russell174596a2009-01-01 10:12:29 +10304069 to_cpumask(l->cpus));
Christoph Lameter45edfa52007-05-09 02:32:45 -07004070 }
4071
Christoph Lameter62bc62a2009-06-16 15:32:15 -07004072 if (nr_online_nodes > 1 && !nodes_empty(l->nodes) &&
Harvey Harrisone374d482008-01-31 15:20:50 -08004073 len < PAGE_SIZE - 60) {
4074 len += sprintf(buf + len, " nodes=");
4075 len += nodelist_scnprintf(buf + len, PAGE_SIZE - len - 50,
Christoph Lameter45edfa52007-05-09 02:32:45 -07004076 l->nodes);
4077 }
4078
Harvey Harrisone374d482008-01-31 15:20:50 -08004079 len += sprintf(buf + len, "\n");
Christoph Lameter88a420e2007-05-06 14:49:45 -07004080 }
4081
4082 free_loc_track(&t);
Eric Dumazetbbd7d572010-03-24 22:25:47 +01004083 kfree(map);
Christoph Lameter88a420e2007-05-06 14:49:45 -07004084 if (!t.count)
Harvey Harrisone374d482008-01-31 15:20:50 -08004085 len += sprintf(buf, "No data\n");
4086 return len;
Christoph Lameter88a420e2007-05-06 14:49:45 -07004087}
Christoph Lameterab4d5ed2010-10-05 13:57:26 -05004088#endif
Christoph Lameter88a420e2007-05-06 14:49:45 -07004089
Christoph Lametera5a84752010-10-05 13:57:27 -05004090#ifdef SLUB_RESILIENCY_TEST
4091static void resiliency_test(void)
4092{
4093 u8 *p;
4094
4095 BUILD_BUG_ON(KMALLOC_MIN_SIZE > 16 || SLUB_PAGE_SHIFT < 10);
4096
4097 printk(KERN_ERR "SLUB resiliency testing\n");
4098 printk(KERN_ERR "-----------------------\n");
4099 printk(KERN_ERR "A. Corruption after allocation\n");
4100
4101 p = kzalloc(16, GFP_KERNEL);
4102 p[16] = 0x12;
4103 printk(KERN_ERR "\n1. kmalloc-16: Clobber Redzone/next pointer"
4104 " 0x12->0x%p\n\n", p + 16);
4105
4106 validate_slab_cache(kmalloc_caches[4]);
4107
4108 /* Hmmm... The next two are dangerous */
4109 p = kzalloc(32, GFP_KERNEL);
4110 p[32 + sizeof(void *)] = 0x34;
4111 printk(KERN_ERR "\n2. kmalloc-32: Clobber next pointer/next slab"
4112 " 0x34 -> -0x%p\n", p);
4113 printk(KERN_ERR
4114 "If allocated object is overwritten then not detectable\n\n");
4115
4116 validate_slab_cache(kmalloc_caches[5]);
4117 p = kzalloc(64, GFP_KERNEL);
4118 p += 64 + (get_cycles() & 0xff) * sizeof(void *);
4119 *p = 0x56;
4120 printk(KERN_ERR "\n3. kmalloc-64: corrupting random byte 0x56->0x%p\n",
4121 p);
4122 printk(KERN_ERR
4123 "If allocated object is overwritten then not detectable\n\n");
4124 validate_slab_cache(kmalloc_caches[6]);
4125
4126 printk(KERN_ERR "\nB. Corruption after free\n");
4127 p = kzalloc(128, GFP_KERNEL);
4128 kfree(p);
4129 *p = 0x78;
4130 printk(KERN_ERR "1. kmalloc-128: Clobber first word 0x78->0x%p\n\n", p);
4131 validate_slab_cache(kmalloc_caches[7]);
4132
4133 p = kzalloc(256, GFP_KERNEL);
4134 kfree(p);
4135 p[50] = 0x9a;
4136 printk(KERN_ERR "\n2. kmalloc-256: Clobber 50th byte 0x9a->0x%p\n\n",
4137 p);
4138 validate_slab_cache(kmalloc_caches[8]);
4139
4140 p = kzalloc(512, GFP_KERNEL);
4141 kfree(p);
4142 p[512] = 0xab;
4143 printk(KERN_ERR "\n3. kmalloc-512: Clobber redzone 0xab->0x%p\n\n", p);
4144 validate_slab_cache(kmalloc_caches[9]);
4145}
4146#else
4147#ifdef CONFIG_SYSFS
4148static void resiliency_test(void) {};
4149#endif
4150#endif
4151
Christoph Lameterab4d5ed2010-10-05 13:57:26 -05004152#ifdef CONFIG_SYSFS
Christoph Lameter81819f02007-05-06 14:49:36 -07004153enum slab_stat_type {
Christoph Lameter205ab992008-04-14 19:11:40 +03004154 SL_ALL, /* All slabs */
4155 SL_PARTIAL, /* Only partially allocated slabs */
4156 SL_CPU, /* Only slabs used for cpu caches */
4157 SL_OBJECTS, /* Determine allocated objects not slabs */
4158 SL_TOTAL /* Determine object capacity not slabs */
Christoph Lameter81819f02007-05-06 14:49:36 -07004159};
4160
Christoph Lameter205ab992008-04-14 19:11:40 +03004161#define SO_ALL (1 << SL_ALL)
Christoph Lameter81819f02007-05-06 14:49:36 -07004162#define SO_PARTIAL (1 << SL_PARTIAL)
4163#define SO_CPU (1 << SL_CPU)
4164#define SO_OBJECTS (1 << SL_OBJECTS)
Christoph Lameter205ab992008-04-14 19:11:40 +03004165#define SO_TOTAL (1 << SL_TOTAL)
Christoph Lameter81819f02007-05-06 14:49:36 -07004166
Cyrill Gorcunov62e5c4b2008-03-02 23:28:24 +03004167static ssize_t show_slab_objects(struct kmem_cache *s,
4168 char *buf, unsigned long flags)
Christoph Lameter81819f02007-05-06 14:49:36 -07004169{
4170 unsigned long total = 0;
Christoph Lameter81819f02007-05-06 14:49:36 -07004171 int node;
4172 int x;
4173 unsigned long *nodes;
4174 unsigned long *per_cpu;
4175
4176 nodes = kzalloc(2 * sizeof(unsigned long) * nr_node_ids, GFP_KERNEL);
Cyrill Gorcunov62e5c4b2008-03-02 23:28:24 +03004177 if (!nodes)
4178 return -ENOMEM;
Christoph Lameter81819f02007-05-06 14:49:36 -07004179 per_cpu = nodes + nr_node_ids;
4180
Christoph Lameter205ab992008-04-14 19:11:40 +03004181 if (flags & SO_CPU) {
4182 int cpu;
Christoph Lameter81819f02007-05-06 14:49:36 -07004183
Christoph Lameter205ab992008-04-14 19:11:40 +03004184 for_each_possible_cpu(cpu) {
Christoph Lameter9dfc6e62009-12-18 16:26:20 -06004185 struct kmem_cache_cpu *c = per_cpu_ptr(s->cpu_slab, cpu);
Christoph Lameterdfb4f092007-10-16 01:26:05 -07004186
Christoph Lameter205ab992008-04-14 19:11:40 +03004187 if (!c || c->node < 0)
4188 continue;
4189
4190 if (c->page) {
4191 if (flags & SO_TOTAL)
4192 x = c->page->objects;
4193 else if (flags & SO_OBJECTS)
4194 x = c->page->inuse;
Christoph Lameter81819f02007-05-06 14:49:36 -07004195 else
4196 x = 1;
Christoph Lameter205ab992008-04-14 19:11:40 +03004197
Christoph Lameter81819f02007-05-06 14:49:36 -07004198 total += x;
Christoph Lameter205ab992008-04-14 19:11:40 +03004199 nodes[c->node] += x;
Christoph Lameter81819f02007-05-06 14:49:36 -07004200 }
Christoph Lameter205ab992008-04-14 19:11:40 +03004201 per_cpu[c->node]++;
Christoph Lameter81819f02007-05-06 14:49:36 -07004202 }
4203 }
4204
Christoph Lameter04d94872011-01-10 10:15:15 -06004205 lock_memory_hotplug();
Christoph Lameterab4d5ed2010-10-05 13:57:26 -05004206#ifdef CONFIG_SLUB_DEBUG
Christoph Lameter205ab992008-04-14 19:11:40 +03004207 if (flags & SO_ALL) {
4208 for_each_node_state(node, N_NORMAL_MEMORY) {
4209 struct kmem_cache_node *n = get_node(s, node);
Christoph Lameter81819f02007-05-06 14:49:36 -07004210
Christoph Lameter205ab992008-04-14 19:11:40 +03004211 if (flags & SO_TOTAL)
4212 x = atomic_long_read(&n->total_objects);
4213 else if (flags & SO_OBJECTS)
4214 x = atomic_long_read(&n->total_objects) -
4215 count_partial(n, count_free);
4216
4217 else
4218 x = atomic_long_read(&n->nr_slabs);
4219 total += x;
4220 nodes[node] += x;
4221 }
4222
Christoph Lameterab4d5ed2010-10-05 13:57:26 -05004223 } else
4224#endif
4225 if (flags & SO_PARTIAL) {
Christoph Lameter205ab992008-04-14 19:11:40 +03004226 for_each_node_state(node, N_NORMAL_MEMORY) {
4227 struct kmem_cache_node *n = get_node(s, node);
4228
4229 if (flags & SO_TOTAL)
4230 x = count_partial(n, count_total);
4231 else if (flags & SO_OBJECTS)
4232 x = count_partial(n, count_inuse);
Christoph Lameter81819f02007-05-06 14:49:36 -07004233 else
4234 x = n->nr_partial;
4235 total += x;
4236 nodes[node] += x;
4237 }
Christoph Lameter81819f02007-05-06 14:49:36 -07004238 }
Christoph Lameter81819f02007-05-06 14:49:36 -07004239 x = sprintf(buf, "%lu", total);
4240#ifdef CONFIG_NUMA
Christoph Lameterf64dc582007-10-16 01:25:33 -07004241 for_each_node_state(node, N_NORMAL_MEMORY)
Christoph Lameter81819f02007-05-06 14:49:36 -07004242 if (nodes[node])
4243 x += sprintf(buf + x, " N%d=%lu",
4244 node, nodes[node]);
4245#endif
Christoph Lameter04d94872011-01-10 10:15:15 -06004246 unlock_memory_hotplug();
Christoph Lameter81819f02007-05-06 14:49:36 -07004247 kfree(nodes);
4248 return x + sprintf(buf + x, "\n");
4249}
4250
Christoph Lameterab4d5ed2010-10-05 13:57:26 -05004251#ifdef CONFIG_SLUB_DEBUG
Christoph Lameter81819f02007-05-06 14:49:36 -07004252static int any_slab_objects(struct kmem_cache *s)
4253{
4254 int node;
Christoph Lameterdfb4f092007-10-16 01:26:05 -07004255
4256 for_each_online_node(node) {
Christoph Lameter81819f02007-05-06 14:49:36 -07004257 struct kmem_cache_node *n = get_node(s, node);
4258
Christoph Lameterdfb4f092007-10-16 01:26:05 -07004259 if (!n)
4260 continue;
4261
Benjamin Herrenschmidt4ea33e22008-05-06 20:42:39 -07004262 if (atomic_long_read(&n->total_objects))
Christoph Lameter81819f02007-05-06 14:49:36 -07004263 return 1;
4264 }
4265 return 0;
4266}
Christoph Lameterab4d5ed2010-10-05 13:57:26 -05004267#endif
Christoph Lameter81819f02007-05-06 14:49:36 -07004268
4269#define to_slab_attr(n) container_of(n, struct slab_attribute, attr)
4270#define to_slab(n) container_of(n, struct kmem_cache, kobj);
4271
4272struct slab_attribute {
4273 struct attribute attr;
4274 ssize_t (*show)(struct kmem_cache *s, char *buf);
4275 ssize_t (*store)(struct kmem_cache *s, const char *x, size_t count);
4276};
4277
4278#define SLAB_ATTR_RO(_name) \
4279 static struct slab_attribute _name##_attr = __ATTR_RO(_name)
4280
4281#define SLAB_ATTR(_name) \
4282 static struct slab_attribute _name##_attr = \
4283 __ATTR(_name, 0644, _name##_show, _name##_store)
4284
Christoph Lameter81819f02007-05-06 14:49:36 -07004285static ssize_t slab_size_show(struct kmem_cache *s, char *buf)
4286{
4287 return sprintf(buf, "%d\n", s->size);
4288}
4289SLAB_ATTR_RO(slab_size);
4290
4291static ssize_t align_show(struct kmem_cache *s, char *buf)
4292{
4293 return sprintf(buf, "%d\n", s->align);
4294}
4295SLAB_ATTR_RO(align);
4296
4297static ssize_t object_size_show(struct kmem_cache *s, char *buf)
4298{
4299 return sprintf(buf, "%d\n", s->objsize);
4300}
4301SLAB_ATTR_RO(object_size);
4302
4303static ssize_t objs_per_slab_show(struct kmem_cache *s, char *buf)
4304{
Christoph Lameter834f3d12008-04-14 19:11:31 +03004305 return sprintf(buf, "%d\n", oo_objects(s->oo));
Christoph Lameter81819f02007-05-06 14:49:36 -07004306}
4307SLAB_ATTR_RO(objs_per_slab);
4308
Christoph Lameter06b285d2008-04-14 19:11:41 +03004309static ssize_t order_store(struct kmem_cache *s,
4310 const char *buf, size_t length)
4311{
Christoph Lameter0121c6192008-04-29 16:11:12 -07004312 unsigned long order;
4313 int err;
4314
4315 err = strict_strtoul(buf, 10, &order);
4316 if (err)
4317 return err;
Christoph Lameter06b285d2008-04-14 19:11:41 +03004318
4319 if (order > slub_max_order || order < slub_min_order)
4320 return -EINVAL;
4321
4322 calculate_sizes(s, order);
4323 return length;
4324}
4325
Christoph Lameter81819f02007-05-06 14:49:36 -07004326static ssize_t order_show(struct kmem_cache *s, char *buf)
4327{
Christoph Lameter834f3d12008-04-14 19:11:31 +03004328 return sprintf(buf, "%d\n", oo_order(s->oo));
Christoph Lameter81819f02007-05-06 14:49:36 -07004329}
Christoph Lameter06b285d2008-04-14 19:11:41 +03004330SLAB_ATTR(order);
Christoph Lameter81819f02007-05-06 14:49:36 -07004331
David Rientjes73d342b2009-02-22 17:40:09 -08004332static ssize_t min_partial_show(struct kmem_cache *s, char *buf)
4333{
4334 return sprintf(buf, "%lu\n", s->min_partial);
4335}
4336
4337static ssize_t min_partial_store(struct kmem_cache *s, const char *buf,
4338 size_t length)
4339{
4340 unsigned long min;
4341 int err;
4342
4343 err = strict_strtoul(buf, 10, &min);
4344 if (err)
4345 return err;
4346
David Rientjesc0bdb232009-02-25 09:16:35 +02004347 set_min_partial(s, min);
David Rientjes73d342b2009-02-22 17:40:09 -08004348 return length;
4349}
4350SLAB_ATTR(min_partial);
4351
Christoph Lameter81819f02007-05-06 14:49:36 -07004352static ssize_t ctor_show(struct kmem_cache *s, char *buf)
4353{
Joe Perches62c70bc2011-01-13 15:45:52 -08004354 if (!s->ctor)
4355 return 0;
4356 return sprintf(buf, "%pS\n", s->ctor);
Christoph Lameter81819f02007-05-06 14:49:36 -07004357}
4358SLAB_ATTR_RO(ctor);
4359
Christoph Lameter81819f02007-05-06 14:49:36 -07004360static ssize_t aliases_show(struct kmem_cache *s, char *buf)
4361{
4362 return sprintf(buf, "%d\n", s->refcount - 1);
4363}
4364SLAB_ATTR_RO(aliases);
4365
Christoph Lameter81819f02007-05-06 14:49:36 -07004366static ssize_t partial_show(struct kmem_cache *s, char *buf)
4367{
Christoph Lameterd9acf4b2008-02-15 15:22:21 -08004368 return show_slab_objects(s, buf, SO_PARTIAL);
Christoph Lameter81819f02007-05-06 14:49:36 -07004369}
4370SLAB_ATTR_RO(partial);
4371
4372static ssize_t cpu_slabs_show(struct kmem_cache *s, char *buf)
4373{
Christoph Lameterd9acf4b2008-02-15 15:22:21 -08004374 return show_slab_objects(s, buf, SO_CPU);
Christoph Lameter81819f02007-05-06 14:49:36 -07004375}
4376SLAB_ATTR_RO(cpu_slabs);
4377
4378static ssize_t objects_show(struct kmem_cache *s, char *buf)
4379{
Christoph Lameter205ab992008-04-14 19:11:40 +03004380 return show_slab_objects(s, buf, SO_ALL|SO_OBJECTS);
Christoph Lameter81819f02007-05-06 14:49:36 -07004381}
4382SLAB_ATTR_RO(objects);
4383
Christoph Lameter205ab992008-04-14 19:11:40 +03004384static ssize_t objects_partial_show(struct kmem_cache *s, char *buf)
4385{
4386 return show_slab_objects(s, buf, SO_PARTIAL|SO_OBJECTS);
4387}
4388SLAB_ATTR_RO(objects_partial);
4389
Christoph Lameter81819f02007-05-06 14:49:36 -07004390static ssize_t reclaim_account_show(struct kmem_cache *s, char *buf)
4391{
4392 return sprintf(buf, "%d\n", !!(s->flags & SLAB_RECLAIM_ACCOUNT));
4393}
4394
4395static ssize_t reclaim_account_store(struct kmem_cache *s,
4396 const char *buf, size_t length)
4397{
4398 s->flags &= ~SLAB_RECLAIM_ACCOUNT;
4399 if (buf[0] == '1')
4400 s->flags |= SLAB_RECLAIM_ACCOUNT;
4401 return length;
4402}
4403SLAB_ATTR(reclaim_account);
4404
4405static ssize_t hwcache_align_show(struct kmem_cache *s, char *buf)
4406{
Christoph Lameter5af60832007-05-06 14:49:56 -07004407 return sprintf(buf, "%d\n", !!(s->flags & SLAB_HWCACHE_ALIGN));
Christoph Lameter81819f02007-05-06 14:49:36 -07004408}
4409SLAB_ATTR_RO(hwcache_align);
4410
4411#ifdef CONFIG_ZONE_DMA
4412static ssize_t cache_dma_show(struct kmem_cache *s, char *buf)
4413{
4414 return sprintf(buf, "%d\n", !!(s->flags & SLAB_CACHE_DMA));
4415}
4416SLAB_ATTR_RO(cache_dma);
4417#endif
4418
4419static ssize_t destroy_by_rcu_show(struct kmem_cache *s, char *buf)
4420{
4421 return sprintf(buf, "%d\n", !!(s->flags & SLAB_DESTROY_BY_RCU));
4422}
4423SLAB_ATTR_RO(destroy_by_rcu);
4424
Lai Jiangshanab9a0f12011-03-10 15:21:48 +08004425static ssize_t reserved_show(struct kmem_cache *s, char *buf)
4426{
4427 return sprintf(buf, "%d\n", s->reserved);
4428}
4429SLAB_ATTR_RO(reserved);
4430
Christoph Lameterab4d5ed2010-10-05 13:57:26 -05004431#ifdef CONFIG_SLUB_DEBUG
Christoph Lametera5a84752010-10-05 13:57:27 -05004432static ssize_t slabs_show(struct kmem_cache *s, char *buf)
4433{
4434 return show_slab_objects(s, buf, SO_ALL);
4435}
4436SLAB_ATTR_RO(slabs);
4437
4438static ssize_t total_objects_show(struct kmem_cache *s, char *buf)
4439{
4440 return show_slab_objects(s, buf, SO_ALL|SO_TOTAL);
4441}
4442SLAB_ATTR_RO(total_objects);
4443
4444static ssize_t sanity_checks_show(struct kmem_cache *s, char *buf)
4445{
4446 return sprintf(buf, "%d\n", !!(s->flags & SLAB_DEBUG_FREE));
4447}
4448
4449static ssize_t sanity_checks_store(struct kmem_cache *s,
4450 const char *buf, size_t length)
4451{
4452 s->flags &= ~SLAB_DEBUG_FREE;
Christoph Lameterb789ef52011-06-01 12:25:49 -05004453 if (buf[0] == '1') {
4454 s->flags &= ~__CMPXCHG_DOUBLE;
Christoph Lametera5a84752010-10-05 13:57:27 -05004455 s->flags |= SLAB_DEBUG_FREE;
Christoph Lameterb789ef52011-06-01 12:25:49 -05004456 }
Christoph Lametera5a84752010-10-05 13:57:27 -05004457 return length;
4458}
4459SLAB_ATTR(sanity_checks);
4460
4461static ssize_t trace_show(struct kmem_cache *s, char *buf)
4462{
4463 return sprintf(buf, "%d\n", !!(s->flags & SLAB_TRACE));
4464}
4465
4466static ssize_t trace_store(struct kmem_cache *s, const char *buf,
4467 size_t length)
4468{
4469 s->flags &= ~SLAB_TRACE;
Christoph Lameterb789ef52011-06-01 12:25:49 -05004470 if (buf[0] == '1') {
4471 s->flags &= ~__CMPXCHG_DOUBLE;
Christoph Lametera5a84752010-10-05 13:57:27 -05004472 s->flags |= SLAB_TRACE;
Christoph Lameterb789ef52011-06-01 12:25:49 -05004473 }
Christoph Lametera5a84752010-10-05 13:57:27 -05004474 return length;
4475}
4476SLAB_ATTR(trace);
4477
Christoph Lameter81819f02007-05-06 14:49:36 -07004478static ssize_t red_zone_show(struct kmem_cache *s, char *buf)
4479{
4480 return sprintf(buf, "%d\n", !!(s->flags & SLAB_RED_ZONE));
4481}
4482
4483static ssize_t red_zone_store(struct kmem_cache *s,
4484 const char *buf, size_t length)
4485{
4486 if (any_slab_objects(s))
4487 return -EBUSY;
4488
4489 s->flags &= ~SLAB_RED_ZONE;
Christoph Lameterb789ef52011-06-01 12:25:49 -05004490 if (buf[0] == '1') {
4491 s->flags &= ~__CMPXCHG_DOUBLE;
Christoph Lameter81819f02007-05-06 14:49:36 -07004492 s->flags |= SLAB_RED_ZONE;
Christoph Lameterb789ef52011-06-01 12:25:49 -05004493 }
Christoph Lameter06b285d2008-04-14 19:11:41 +03004494 calculate_sizes(s, -1);
Christoph Lameter81819f02007-05-06 14:49:36 -07004495 return length;
4496}
4497SLAB_ATTR(red_zone);
4498
4499static ssize_t poison_show(struct kmem_cache *s, char *buf)
4500{
4501 return sprintf(buf, "%d\n", !!(s->flags & SLAB_POISON));
4502}
4503
4504static ssize_t poison_store(struct kmem_cache *s,
4505 const char *buf, size_t length)
4506{
4507 if (any_slab_objects(s))
4508 return -EBUSY;
4509
4510 s->flags &= ~SLAB_POISON;
Christoph Lameterb789ef52011-06-01 12:25:49 -05004511 if (buf[0] == '1') {
4512 s->flags &= ~__CMPXCHG_DOUBLE;
Christoph Lameter81819f02007-05-06 14:49:36 -07004513 s->flags |= SLAB_POISON;
Christoph Lameterb789ef52011-06-01 12:25:49 -05004514 }
Christoph Lameter06b285d2008-04-14 19:11:41 +03004515 calculate_sizes(s, -1);
Christoph Lameter81819f02007-05-06 14:49:36 -07004516 return length;
4517}
4518SLAB_ATTR(poison);
4519
4520static ssize_t store_user_show(struct kmem_cache *s, char *buf)
4521{
4522 return sprintf(buf, "%d\n", !!(s->flags & SLAB_STORE_USER));
4523}
4524
4525static ssize_t store_user_store(struct kmem_cache *s,
4526 const char *buf, size_t length)
4527{
4528 if (any_slab_objects(s))
4529 return -EBUSY;
4530
4531 s->flags &= ~SLAB_STORE_USER;
Christoph Lameterb789ef52011-06-01 12:25:49 -05004532 if (buf[0] == '1') {
4533 s->flags &= ~__CMPXCHG_DOUBLE;
Christoph Lameter81819f02007-05-06 14:49:36 -07004534 s->flags |= SLAB_STORE_USER;
Christoph Lameterb789ef52011-06-01 12:25:49 -05004535 }
Christoph Lameter06b285d2008-04-14 19:11:41 +03004536 calculate_sizes(s, -1);
Christoph Lameter81819f02007-05-06 14:49:36 -07004537 return length;
4538}
4539SLAB_ATTR(store_user);
4540
Christoph Lameter53e15af2007-05-06 14:49:43 -07004541static ssize_t validate_show(struct kmem_cache *s, char *buf)
4542{
4543 return 0;
4544}
4545
4546static ssize_t validate_store(struct kmem_cache *s,
4547 const char *buf, size_t length)
4548{
Christoph Lameter434e2452007-07-17 04:03:30 -07004549 int ret = -EINVAL;
4550
4551 if (buf[0] == '1') {
4552 ret = validate_slab_cache(s);
4553 if (ret >= 0)
4554 ret = length;
4555 }
4556 return ret;
Christoph Lameter53e15af2007-05-06 14:49:43 -07004557}
4558SLAB_ATTR(validate);
Christoph Lametera5a84752010-10-05 13:57:27 -05004559
4560static ssize_t alloc_calls_show(struct kmem_cache *s, char *buf)
4561{
4562 if (!(s->flags & SLAB_STORE_USER))
4563 return -ENOSYS;
4564 return list_locations(s, buf, TRACK_ALLOC);
4565}
4566SLAB_ATTR_RO(alloc_calls);
4567
4568static ssize_t free_calls_show(struct kmem_cache *s, char *buf)
4569{
4570 if (!(s->flags & SLAB_STORE_USER))
4571 return -ENOSYS;
4572 return list_locations(s, buf, TRACK_FREE);
4573}
4574SLAB_ATTR_RO(free_calls);
4575#endif /* CONFIG_SLUB_DEBUG */
4576
4577#ifdef CONFIG_FAILSLAB
4578static ssize_t failslab_show(struct kmem_cache *s, char *buf)
4579{
4580 return sprintf(buf, "%d\n", !!(s->flags & SLAB_FAILSLAB));
4581}
4582
4583static ssize_t failslab_store(struct kmem_cache *s, const char *buf,
4584 size_t length)
4585{
4586 s->flags &= ~SLAB_FAILSLAB;
4587 if (buf[0] == '1')
4588 s->flags |= SLAB_FAILSLAB;
4589 return length;
4590}
4591SLAB_ATTR(failslab);
Christoph Lameterab4d5ed2010-10-05 13:57:26 -05004592#endif
Christoph Lameter53e15af2007-05-06 14:49:43 -07004593
Christoph Lameter2086d262007-05-06 14:49:46 -07004594static ssize_t shrink_show(struct kmem_cache *s, char *buf)
4595{
4596 return 0;
4597}
4598
4599static ssize_t shrink_store(struct kmem_cache *s,
4600 const char *buf, size_t length)
4601{
4602 if (buf[0] == '1') {
4603 int rc = kmem_cache_shrink(s);
4604
4605 if (rc)
4606 return rc;
4607 } else
4608 return -EINVAL;
4609 return length;
4610}
4611SLAB_ATTR(shrink);
4612
Christoph Lameter81819f02007-05-06 14:49:36 -07004613#ifdef CONFIG_NUMA
Christoph Lameter98246012008-01-07 23:20:26 -08004614static ssize_t remote_node_defrag_ratio_show(struct kmem_cache *s, char *buf)
Christoph Lameter81819f02007-05-06 14:49:36 -07004615{
Christoph Lameter98246012008-01-07 23:20:26 -08004616 return sprintf(buf, "%d\n", s->remote_node_defrag_ratio / 10);
Christoph Lameter81819f02007-05-06 14:49:36 -07004617}
4618
Christoph Lameter98246012008-01-07 23:20:26 -08004619static ssize_t remote_node_defrag_ratio_store(struct kmem_cache *s,
Christoph Lameter81819f02007-05-06 14:49:36 -07004620 const char *buf, size_t length)
4621{
Christoph Lameter0121c6192008-04-29 16:11:12 -07004622 unsigned long ratio;
4623 int err;
Christoph Lameter81819f02007-05-06 14:49:36 -07004624
Christoph Lameter0121c6192008-04-29 16:11:12 -07004625 err = strict_strtoul(buf, 10, &ratio);
4626 if (err)
4627 return err;
4628
Christoph Lametere2cb96b2008-08-19 08:51:22 -05004629 if (ratio <= 100)
Christoph Lameter0121c6192008-04-29 16:11:12 -07004630 s->remote_node_defrag_ratio = ratio * 10;
4631
Christoph Lameter81819f02007-05-06 14:49:36 -07004632 return length;
4633}
Christoph Lameter98246012008-01-07 23:20:26 -08004634SLAB_ATTR(remote_node_defrag_ratio);
Christoph Lameter81819f02007-05-06 14:49:36 -07004635#endif
4636
Christoph Lameter8ff12cf2008-02-07 17:47:41 -08004637#ifdef CONFIG_SLUB_STATS
Christoph Lameter8ff12cf2008-02-07 17:47:41 -08004638static int show_stat(struct kmem_cache *s, char *buf, enum stat_item si)
4639{
4640 unsigned long sum = 0;
4641 int cpu;
4642 int len;
4643 int *data = kmalloc(nr_cpu_ids * sizeof(int), GFP_KERNEL);
4644
4645 if (!data)
4646 return -ENOMEM;
4647
4648 for_each_online_cpu(cpu) {
Christoph Lameter9dfc6e62009-12-18 16:26:20 -06004649 unsigned x = per_cpu_ptr(s->cpu_slab, cpu)->stat[si];
Christoph Lameter8ff12cf2008-02-07 17:47:41 -08004650
4651 data[cpu] = x;
4652 sum += x;
4653 }
4654
4655 len = sprintf(buf, "%lu", sum);
4656
Christoph Lameter50ef37b2008-04-14 18:52:05 +03004657#ifdef CONFIG_SMP
Christoph Lameter8ff12cf2008-02-07 17:47:41 -08004658 for_each_online_cpu(cpu) {
4659 if (data[cpu] && len < PAGE_SIZE - 20)
Christoph Lameter50ef37b2008-04-14 18:52:05 +03004660 len += sprintf(buf + len, " C%d=%u", cpu, data[cpu]);
Christoph Lameter8ff12cf2008-02-07 17:47:41 -08004661 }
Christoph Lameter50ef37b2008-04-14 18:52:05 +03004662#endif
Christoph Lameter8ff12cf2008-02-07 17:47:41 -08004663 kfree(data);
4664 return len + sprintf(buf + len, "\n");
4665}
4666
David Rientjes78eb00c2009-10-15 02:20:22 -07004667static void clear_stat(struct kmem_cache *s, enum stat_item si)
4668{
4669 int cpu;
4670
4671 for_each_online_cpu(cpu)
Christoph Lameter9dfc6e62009-12-18 16:26:20 -06004672 per_cpu_ptr(s->cpu_slab, cpu)->stat[si] = 0;
David Rientjes78eb00c2009-10-15 02:20:22 -07004673}
4674
Christoph Lameter8ff12cf2008-02-07 17:47:41 -08004675#define STAT_ATTR(si, text) \
4676static ssize_t text##_show(struct kmem_cache *s, char *buf) \
4677{ \
4678 return show_stat(s, buf, si); \
4679} \
David Rientjes78eb00c2009-10-15 02:20:22 -07004680static ssize_t text##_store(struct kmem_cache *s, \
4681 const char *buf, size_t length) \
4682{ \
4683 if (buf[0] != '0') \
4684 return -EINVAL; \
4685 clear_stat(s, si); \
4686 return length; \
4687} \
4688SLAB_ATTR(text); \
Christoph Lameter8ff12cf2008-02-07 17:47:41 -08004689
4690STAT_ATTR(ALLOC_FASTPATH, alloc_fastpath);
4691STAT_ATTR(ALLOC_SLOWPATH, alloc_slowpath);
4692STAT_ATTR(FREE_FASTPATH, free_fastpath);
4693STAT_ATTR(FREE_SLOWPATH, free_slowpath);
4694STAT_ATTR(FREE_FROZEN, free_frozen);
4695STAT_ATTR(FREE_ADD_PARTIAL, free_add_partial);
4696STAT_ATTR(FREE_REMOVE_PARTIAL, free_remove_partial);
4697STAT_ATTR(ALLOC_FROM_PARTIAL, alloc_from_partial);
4698STAT_ATTR(ALLOC_SLAB, alloc_slab);
4699STAT_ATTR(ALLOC_REFILL, alloc_refill);
4700STAT_ATTR(FREE_SLAB, free_slab);
4701STAT_ATTR(CPUSLAB_FLUSH, cpuslab_flush);
4702STAT_ATTR(DEACTIVATE_FULL, deactivate_full);
4703STAT_ATTR(DEACTIVATE_EMPTY, deactivate_empty);
4704STAT_ATTR(DEACTIVATE_TO_HEAD, deactivate_to_head);
4705STAT_ATTR(DEACTIVATE_TO_TAIL, deactivate_to_tail);
4706STAT_ATTR(DEACTIVATE_REMOTE_FREES, deactivate_remote_frees);
Christoph Lameter65c33762008-04-14 19:11:40 +03004707STAT_ATTR(ORDER_FALLBACK, order_fallback);
Christoph Lameterb789ef52011-06-01 12:25:49 -05004708STAT_ATTR(CMPXCHG_DOUBLE_CPU_FAIL, cmpxchg_double_cpu_fail);
4709STAT_ATTR(CMPXCHG_DOUBLE_FAIL, cmpxchg_double_fail);
Christoph Lameter8ff12cf2008-02-07 17:47:41 -08004710#endif
4711
Pekka Enberg06428782008-01-07 23:20:27 -08004712static struct attribute *slab_attrs[] = {
Christoph Lameter81819f02007-05-06 14:49:36 -07004713 &slab_size_attr.attr,
4714 &object_size_attr.attr,
4715 &objs_per_slab_attr.attr,
4716 &order_attr.attr,
David Rientjes73d342b2009-02-22 17:40:09 -08004717 &min_partial_attr.attr,
Christoph Lameter81819f02007-05-06 14:49:36 -07004718 &objects_attr.attr,
Christoph Lameter205ab992008-04-14 19:11:40 +03004719 &objects_partial_attr.attr,
Christoph Lameter81819f02007-05-06 14:49:36 -07004720 &partial_attr.attr,
4721 &cpu_slabs_attr.attr,
4722 &ctor_attr.attr,
Christoph Lameter81819f02007-05-06 14:49:36 -07004723 &aliases_attr.attr,
4724 &align_attr.attr,
Christoph Lameter81819f02007-05-06 14:49:36 -07004725 &hwcache_align_attr.attr,
4726 &reclaim_account_attr.attr,
4727 &destroy_by_rcu_attr.attr,
Christoph Lametera5a84752010-10-05 13:57:27 -05004728 &shrink_attr.attr,
Lai Jiangshanab9a0f12011-03-10 15:21:48 +08004729 &reserved_attr.attr,
Christoph Lameterab4d5ed2010-10-05 13:57:26 -05004730#ifdef CONFIG_SLUB_DEBUG
Christoph Lametera5a84752010-10-05 13:57:27 -05004731 &total_objects_attr.attr,
4732 &slabs_attr.attr,
4733 &sanity_checks_attr.attr,
4734 &trace_attr.attr,
Christoph Lameter81819f02007-05-06 14:49:36 -07004735 &red_zone_attr.attr,
4736 &poison_attr.attr,
4737 &store_user_attr.attr,
Christoph Lameter53e15af2007-05-06 14:49:43 -07004738 &validate_attr.attr,
Christoph Lameter88a420e2007-05-06 14:49:45 -07004739 &alloc_calls_attr.attr,
4740 &free_calls_attr.attr,
Christoph Lameterab4d5ed2010-10-05 13:57:26 -05004741#endif
Christoph Lameter81819f02007-05-06 14:49:36 -07004742#ifdef CONFIG_ZONE_DMA
4743 &cache_dma_attr.attr,
4744#endif
4745#ifdef CONFIG_NUMA
Christoph Lameter98246012008-01-07 23:20:26 -08004746 &remote_node_defrag_ratio_attr.attr,
Christoph Lameter81819f02007-05-06 14:49:36 -07004747#endif
Christoph Lameter8ff12cf2008-02-07 17:47:41 -08004748#ifdef CONFIG_SLUB_STATS
4749 &alloc_fastpath_attr.attr,
4750 &alloc_slowpath_attr.attr,
4751 &free_fastpath_attr.attr,
4752 &free_slowpath_attr.attr,
4753 &free_frozen_attr.attr,
4754 &free_add_partial_attr.attr,
4755 &free_remove_partial_attr.attr,
4756 &alloc_from_partial_attr.attr,
4757 &alloc_slab_attr.attr,
4758 &alloc_refill_attr.attr,
4759 &free_slab_attr.attr,
4760 &cpuslab_flush_attr.attr,
4761 &deactivate_full_attr.attr,
4762 &deactivate_empty_attr.attr,
4763 &deactivate_to_head_attr.attr,
4764 &deactivate_to_tail_attr.attr,
4765 &deactivate_remote_frees_attr.attr,
Christoph Lameter65c33762008-04-14 19:11:40 +03004766 &order_fallback_attr.attr,
Christoph Lameterb789ef52011-06-01 12:25:49 -05004767 &cmpxchg_double_fail_attr.attr,
4768 &cmpxchg_double_cpu_fail_attr.attr,
Christoph Lameter8ff12cf2008-02-07 17:47:41 -08004769#endif
Dmitry Monakhov4c13dd32010-02-26 09:36:12 +03004770#ifdef CONFIG_FAILSLAB
4771 &failslab_attr.attr,
4772#endif
4773
Christoph Lameter81819f02007-05-06 14:49:36 -07004774 NULL
4775};
4776
4777static struct attribute_group slab_attr_group = {
4778 .attrs = slab_attrs,
4779};
4780
4781static ssize_t slab_attr_show(struct kobject *kobj,
4782 struct attribute *attr,
4783 char *buf)
4784{
4785 struct slab_attribute *attribute;
4786 struct kmem_cache *s;
4787 int err;
4788
4789 attribute = to_slab_attr(attr);
4790 s = to_slab(kobj);
4791
4792 if (!attribute->show)
4793 return -EIO;
4794
4795 err = attribute->show(s, buf);
4796
4797 return err;
4798}
4799
4800static ssize_t slab_attr_store(struct kobject *kobj,
4801 struct attribute *attr,
4802 const char *buf, size_t len)
4803{
4804 struct slab_attribute *attribute;
4805 struct kmem_cache *s;
4806 int err;
4807
4808 attribute = to_slab_attr(attr);
4809 s = to_slab(kobj);
4810
4811 if (!attribute->store)
4812 return -EIO;
4813
4814 err = attribute->store(s, buf, len);
4815
4816 return err;
4817}
4818
Christoph Lameter151c6022008-01-07 22:29:05 -08004819static void kmem_cache_release(struct kobject *kobj)
4820{
4821 struct kmem_cache *s = to_slab(kobj);
4822
Pekka Enberg84c1cf62010-09-14 23:21:12 +03004823 kfree(s->name);
Christoph Lameter151c6022008-01-07 22:29:05 -08004824 kfree(s);
4825}
4826
Emese Revfy52cf25d2010-01-19 02:58:23 +01004827static const struct sysfs_ops slab_sysfs_ops = {
Christoph Lameter81819f02007-05-06 14:49:36 -07004828 .show = slab_attr_show,
4829 .store = slab_attr_store,
4830};
4831
4832static struct kobj_type slab_ktype = {
4833 .sysfs_ops = &slab_sysfs_ops,
Christoph Lameter151c6022008-01-07 22:29:05 -08004834 .release = kmem_cache_release
Christoph Lameter81819f02007-05-06 14:49:36 -07004835};
4836
4837static int uevent_filter(struct kset *kset, struct kobject *kobj)
4838{
4839 struct kobj_type *ktype = get_ktype(kobj);
4840
4841 if (ktype == &slab_ktype)
4842 return 1;
4843 return 0;
4844}
4845
Emese Revfy9cd43612009-12-31 14:52:51 +01004846static const struct kset_uevent_ops slab_uevent_ops = {
Christoph Lameter81819f02007-05-06 14:49:36 -07004847 .filter = uevent_filter,
4848};
4849
Greg Kroah-Hartman27c3a312007-11-01 09:29:06 -06004850static struct kset *slab_kset;
Christoph Lameter81819f02007-05-06 14:49:36 -07004851
4852#define ID_STR_LENGTH 64
4853
4854/* Create a unique string id for a slab cache:
Christoph Lameter6446faa2008-02-15 23:45:26 -08004855 *
4856 * Format :[flags-]size
Christoph Lameter81819f02007-05-06 14:49:36 -07004857 */
4858static char *create_unique_id(struct kmem_cache *s)
4859{
4860 char *name = kmalloc(ID_STR_LENGTH, GFP_KERNEL);
4861 char *p = name;
4862
4863 BUG_ON(!name);
4864
4865 *p++ = ':';
4866 /*
4867 * First flags affecting slabcache operations. We will only
4868 * get here for aliasable slabs so we do not need to support
4869 * too many flags. The flags here must cover all flags that
4870 * are matched during merging to guarantee that the id is
4871 * unique.
4872 */
4873 if (s->flags & SLAB_CACHE_DMA)
4874 *p++ = 'd';
4875 if (s->flags & SLAB_RECLAIM_ACCOUNT)
4876 *p++ = 'a';
4877 if (s->flags & SLAB_DEBUG_FREE)
4878 *p++ = 'F';
Vegard Nossum5a896d92008-04-04 00:54:48 +02004879 if (!(s->flags & SLAB_NOTRACK))
4880 *p++ = 't';
Christoph Lameter81819f02007-05-06 14:49:36 -07004881 if (p != name + 1)
4882 *p++ = '-';
4883 p += sprintf(p, "%07d", s->size);
4884 BUG_ON(p > name + ID_STR_LENGTH - 1);
4885 return name;
4886}
4887
4888static int sysfs_slab_add(struct kmem_cache *s)
4889{
4890 int err;
4891 const char *name;
4892 int unmergeable;
4893
4894 if (slab_state < SYSFS)
4895 /* Defer until later */
4896 return 0;
4897
4898 unmergeable = slab_unmergeable(s);
4899 if (unmergeable) {
4900 /*
4901 * Slabcache can never be merged so we can use the name proper.
4902 * This is typically the case for debug situations. In that
4903 * case we can catch duplicate names easily.
4904 */
Greg Kroah-Hartman27c3a312007-11-01 09:29:06 -06004905 sysfs_remove_link(&slab_kset->kobj, s->name);
Christoph Lameter81819f02007-05-06 14:49:36 -07004906 name = s->name;
4907 } else {
4908 /*
4909 * Create a unique name for the slab as a target
4910 * for the symlinks.
4911 */
4912 name = create_unique_id(s);
4913 }
4914
Greg Kroah-Hartman27c3a312007-11-01 09:29:06 -06004915 s->kobj.kset = slab_kset;
Greg Kroah-Hartman1eada112007-12-17 23:05:35 -07004916 err = kobject_init_and_add(&s->kobj, &slab_ktype, NULL, name);
4917 if (err) {
4918 kobject_put(&s->kobj);
Christoph Lameter81819f02007-05-06 14:49:36 -07004919 return err;
Greg Kroah-Hartman1eada112007-12-17 23:05:35 -07004920 }
Christoph Lameter81819f02007-05-06 14:49:36 -07004921
4922 err = sysfs_create_group(&s->kobj, &slab_attr_group);
Xiaotian Feng5788d8a2009-07-22 11:28:53 +08004923 if (err) {
4924 kobject_del(&s->kobj);
4925 kobject_put(&s->kobj);
Christoph Lameter81819f02007-05-06 14:49:36 -07004926 return err;
Xiaotian Feng5788d8a2009-07-22 11:28:53 +08004927 }
Christoph Lameter81819f02007-05-06 14:49:36 -07004928 kobject_uevent(&s->kobj, KOBJ_ADD);
4929 if (!unmergeable) {
4930 /* Setup first alias */
4931 sysfs_slab_alias(s, s->name);
4932 kfree(name);
4933 }
4934 return 0;
4935}
4936
4937static void sysfs_slab_remove(struct kmem_cache *s)
4938{
Christoph Lameter2bce6482010-07-19 11:39:11 -05004939 if (slab_state < SYSFS)
4940 /*
4941 * Sysfs has not been setup yet so no need to remove the
4942 * cache from sysfs.
4943 */
4944 return;
4945
Christoph Lameter81819f02007-05-06 14:49:36 -07004946 kobject_uevent(&s->kobj, KOBJ_REMOVE);
4947 kobject_del(&s->kobj);
Christoph Lameter151c6022008-01-07 22:29:05 -08004948 kobject_put(&s->kobj);
Christoph Lameter81819f02007-05-06 14:49:36 -07004949}
4950
4951/*
4952 * Need to buffer aliases during bootup until sysfs becomes
Nick Andrew9f6c708e2008-12-05 14:08:08 +11004953 * available lest we lose that information.
Christoph Lameter81819f02007-05-06 14:49:36 -07004954 */
4955struct saved_alias {
4956 struct kmem_cache *s;
4957 const char *name;
4958 struct saved_alias *next;
4959};
4960
Adrian Bunk5af328a2007-07-17 04:03:27 -07004961static struct saved_alias *alias_list;
Christoph Lameter81819f02007-05-06 14:49:36 -07004962
4963static int sysfs_slab_alias(struct kmem_cache *s, const char *name)
4964{
4965 struct saved_alias *al;
4966
4967 if (slab_state == SYSFS) {
4968 /*
4969 * If we have a leftover link then remove it.
4970 */
Greg Kroah-Hartman27c3a312007-11-01 09:29:06 -06004971 sysfs_remove_link(&slab_kset->kobj, name);
4972 return sysfs_create_link(&slab_kset->kobj, &s->kobj, name);
Christoph Lameter81819f02007-05-06 14:49:36 -07004973 }
4974
4975 al = kmalloc(sizeof(struct saved_alias), GFP_KERNEL);
4976 if (!al)
4977 return -ENOMEM;
4978
4979 al->s = s;
4980 al->name = name;
4981 al->next = alias_list;
4982 alias_list = al;
4983 return 0;
4984}
4985
4986static int __init slab_sysfs_init(void)
4987{
Christoph Lameter5b95a4a2007-07-17 04:03:19 -07004988 struct kmem_cache *s;
Christoph Lameter81819f02007-05-06 14:49:36 -07004989 int err;
4990
Christoph Lameter2bce6482010-07-19 11:39:11 -05004991 down_write(&slub_lock);
4992
Greg Kroah-Hartman0ff21e42007-11-06 10:36:58 -08004993 slab_kset = kset_create_and_add("slab", &slab_uevent_ops, kernel_kobj);
Greg Kroah-Hartman27c3a312007-11-01 09:29:06 -06004994 if (!slab_kset) {
Christoph Lameter2bce6482010-07-19 11:39:11 -05004995 up_write(&slub_lock);
Christoph Lameter81819f02007-05-06 14:49:36 -07004996 printk(KERN_ERR "Cannot register slab subsystem.\n");
4997 return -ENOSYS;
4998 }
4999
Christoph Lameter26a7bd02007-05-09 02:32:39 -07005000 slab_state = SYSFS;
5001
Christoph Lameter5b95a4a2007-07-17 04:03:19 -07005002 list_for_each_entry(s, &slab_caches, list) {
Christoph Lameter26a7bd02007-05-09 02:32:39 -07005003 err = sysfs_slab_add(s);
Christoph Lameter5d540fb2007-08-30 23:56:26 -07005004 if (err)
5005 printk(KERN_ERR "SLUB: Unable to add boot slab %s"
5006 " to sysfs\n", s->name);
Christoph Lameter26a7bd02007-05-09 02:32:39 -07005007 }
Christoph Lameter81819f02007-05-06 14:49:36 -07005008
5009 while (alias_list) {
5010 struct saved_alias *al = alias_list;
5011
5012 alias_list = alias_list->next;
5013 err = sysfs_slab_alias(al->s, al->name);
Christoph Lameter5d540fb2007-08-30 23:56:26 -07005014 if (err)
5015 printk(KERN_ERR "SLUB: Unable to add boot slab alias"
5016 " %s to sysfs\n", s->name);
Christoph Lameter81819f02007-05-06 14:49:36 -07005017 kfree(al);
5018 }
5019
Christoph Lameter2bce6482010-07-19 11:39:11 -05005020 up_write(&slub_lock);
Christoph Lameter81819f02007-05-06 14:49:36 -07005021 resiliency_test();
5022 return 0;
5023}
5024
5025__initcall(slab_sysfs_init);
Christoph Lameterab4d5ed2010-10-05 13:57:26 -05005026#endif /* CONFIG_SYSFS */
Pekka J Enberg57ed3ed2008-01-01 17:23:28 +01005027
5028/*
5029 * The /proc/slabinfo ABI
5030 */
Linus Torvalds158a9622008-01-02 13:04:48 -08005031#ifdef CONFIG_SLABINFO
Pekka J Enberg57ed3ed2008-01-01 17:23:28 +01005032static void print_slabinfo_header(struct seq_file *m)
5033{
5034 seq_puts(m, "slabinfo - version: 2.1\n");
5035 seq_puts(m, "# name <active_objs> <num_objs> <objsize> "
5036 "<objperslab> <pagesperslab>");
5037 seq_puts(m, " : tunables <limit> <batchcount> <sharedfactor>");
5038 seq_puts(m, " : slabdata <active_slabs> <num_slabs> <sharedavail>");
5039 seq_putc(m, '\n');
5040}
5041
5042static void *s_start(struct seq_file *m, loff_t *pos)
5043{
5044 loff_t n = *pos;
5045
5046 down_read(&slub_lock);
5047 if (!n)
5048 print_slabinfo_header(m);
5049
5050 return seq_list_start(&slab_caches, *pos);
5051}
5052
5053static void *s_next(struct seq_file *m, void *p, loff_t *pos)
5054{
5055 return seq_list_next(p, &slab_caches, pos);
5056}
5057
5058static void s_stop(struct seq_file *m, void *p)
5059{
5060 up_read(&slub_lock);
5061}
5062
5063static int s_show(struct seq_file *m, void *p)
5064{
5065 unsigned long nr_partials = 0;
5066 unsigned long nr_slabs = 0;
5067 unsigned long nr_inuse = 0;
Christoph Lameter205ab992008-04-14 19:11:40 +03005068 unsigned long nr_objs = 0;
5069 unsigned long nr_free = 0;
Pekka J Enberg57ed3ed2008-01-01 17:23:28 +01005070 struct kmem_cache *s;
5071 int node;
5072
5073 s = list_entry(p, struct kmem_cache, list);
5074
5075 for_each_online_node(node) {
5076 struct kmem_cache_node *n = get_node(s, node);
5077
5078 if (!n)
5079 continue;
5080
5081 nr_partials += n->nr_partial;
5082 nr_slabs += atomic_long_read(&n->nr_slabs);
Christoph Lameter205ab992008-04-14 19:11:40 +03005083 nr_objs += atomic_long_read(&n->total_objects);
5084 nr_free += count_partial(n, count_free);
Pekka J Enberg57ed3ed2008-01-01 17:23:28 +01005085 }
5086
Christoph Lameter205ab992008-04-14 19:11:40 +03005087 nr_inuse = nr_objs - nr_free;
Pekka J Enberg57ed3ed2008-01-01 17:23:28 +01005088
5089 seq_printf(m, "%-17s %6lu %6lu %6u %4u %4d", s->name, nr_inuse,
Christoph Lameter834f3d12008-04-14 19:11:31 +03005090 nr_objs, s->size, oo_objects(s->oo),
5091 (1 << oo_order(s->oo)));
Pekka J Enberg57ed3ed2008-01-01 17:23:28 +01005092 seq_printf(m, " : tunables %4u %4u %4u", 0, 0, 0);
5093 seq_printf(m, " : slabdata %6lu %6lu %6lu", nr_slabs, nr_slabs,
5094 0UL);
5095 seq_putc(m, '\n');
5096 return 0;
5097}
5098
Alexey Dobriyan7b3c3a52008-10-06 02:42:17 +04005099static const struct seq_operations slabinfo_op = {
Pekka J Enberg57ed3ed2008-01-01 17:23:28 +01005100 .start = s_start,
5101 .next = s_next,
5102 .stop = s_stop,
5103 .show = s_show,
5104};
5105
Alexey Dobriyan7b3c3a52008-10-06 02:42:17 +04005106static int slabinfo_open(struct inode *inode, struct file *file)
5107{
5108 return seq_open(file, &slabinfo_op);
5109}
5110
5111static const struct file_operations proc_slabinfo_operations = {
5112 .open = slabinfo_open,
5113 .read = seq_read,
5114 .llseek = seq_lseek,
5115 .release = seq_release,
5116};
5117
5118static int __init slab_proc_init(void)
5119{
WANG Congcf5d1132009-08-18 19:11:40 +03005120 proc_create("slabinfo", S_IRUGO, NULL, &proc_slabinfo_operations);
Alexey Dobriyan7b3c3a52008-10-06 02:42:17 +04005121 return 0;
5122}
5123module_init(slab_proc_init);
Linus Torvalds158a9622008-01-02 13:04:48 -08005124#endif /* CONFIG_SLABINFO */