blob: 94d2a33a866e153830bfd1dbb285e26006262f6f [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 Lameter81819f02007-05-06 14:49:36 -0700134/*
Christoph Lameter2086d262007-05-06 14:49:46 -0700135 * Mininum number of partial slabs. These will be left on the partial
136 * lists even if they are empty. kmem_cache_shrink may reclaim them.
137 */
Christoph Lameter76be8952007-12-21 14:37:37 -0800138#define MIN_PARTIAL 5
Christoph Lametere95eed52007-05-06 14:49:44 -0700139
Christoph Lameter2086d262007-05-06 14:49:46 -0700140/*
141 * Maximum number of desirable partial slabs.
142 * The existence of more partial slabs makes kmem_cache_shrink
143 * sort the partial list by the number of objects in the.
144 */
145#define MAX_PARTIAL 10
146
Christoph Lameter81819f02007-05-06 14:49:36 -0700147#define DEBUG_DEFAULT_FLAGS (SLAB_DEBUG_FREE | SLAB_RED_ZONE | \
148 SLAB_POISON | SLAB_STORE_USER)
Christoph Lameter672bba32007-05-09 02:32:39 -0700149
Christoph Lameter81819f02007-05-06 14:49:36 -0700150/*
David Rientjes3de47212009-07-27 18:30:35 -0700151 * Debugging flags that require metadata to be stored in the slab. These get
152 * disabled when slub_debug=O is used and a cache's min order increases with
153 * metadata.
David Rientjesfa5ec8a2009-07-07 00:14:14 -0700154 */
David Rientjes3de47212009-07-27 18:30:35 -0700155#define DEBUG_METADATA_FLAGS (SLAB_RED_ZONE | SLAB_POISON | SLAB_STORE_USER)
David Rientjesfa5ec8a2009-07-07 00:14:14 -0700156
157/*
Christoph Lameter81819f02007-05-06 14:49:36 -0700158 * Set of flags that will prevent slab merging
159 */
160#define SLUB_NEVER_MERGE (SLAB_RED_ZONE | SLAB_POISON | SLAB_STORE_USER | \
Dmitry Monakhov4c13dd32010-02-26 09:36:12 +0300161 SLAB_TRACE | SLAB_DESTROY_BY_RCU | SLAB_NOLEAKTRACE | \
162 SLAB_FAILSLAB)
Christoph Lameter81819f02007-05-06 14:49:36 -0700163
164#define SLUB_MERGE_SAME (SLAB_DEBUG_FREE | SLAB_RECLAIM_ACCOUNT | \
Vegard Nossum5a896d92008-04-04 00:54:48 +0200165 SLAB_CACHE_DMA | SLAB_NOTRACK)
Christoph Lameter81819f02007-05-06 14:49:36 -0700166
Cyrill Gorcunov210b5c02008-10-22 23:00:38 +0400167#define OO_SHIFT 16
168#define OO_MASK ((1 << OO_SHIFT) - 1)
169#define MAX_OBJS_PER_PAGE 65535 /* since page.objects is u16 */
170
Christoph Lameter81819f02007-05-06 14:49:36 -0700171/* Internal SLUB flags */
Christoph Lameterf90ec392010-07-09 14:07:11 -0500172#define __OBJECT_POISON 0x80000000UL /* Poison object */
Christoph Lameter81819f02007-05-06 14:49:36 -0700173
174static int kmem_size = sizeof(struct kmem_cache);
175
176#ifdef CONFIG_SMP
177static struct notifier_block slab_notifier;
178#endif
179
180static enum {
181 DOWN, /* No slab functionality available */
Christoph Lameter51df1142010-08-20 12:37:15 -0500182 PARTIAL, /* Kmem_cache_node works */
Christoph Lameter672bba32007-05-09 02:32:39 -0700183 UP, /* Everything works but does not show up in sysfs */
Christoph Lameter81819f02007-05-06 14:49:36 -0700184 SYSFS /* Sysfs up */
185} slab_state = DOWN;
186
187/* A list of all slab caches on the system */
188static DECLARE_RWSEM(slub_lock);
Adrian Bunk5af328a2007-07-17 04:03:27 -0700189static LIST_HEAD(slab_caches);
Christoph Lameter81819f02007-05-06 14:49:36 -0700190
Christoph Lameter02cbc872007-05-09 02:32:43 -0700191/*
192 * Tracking user of a slab.
193 */
194struct track {
Eduard - Gabriel Munteanuce71e272008-08-19 20:43:25 +0300195 unsigned long addr; /* Called from address */
Christoph Lameter02cbc872007-05-09 02:32:43 -0700196 int cpu; /* Was running on cpu */
197 int pid; /* Pid context */
198 unsigned long when; /* When did the operation occur */
199};
200
201enum track_item { TRACK_ALLOC, TRACK_FREE };
202
Christoph Lameterab4d5ed2010-10-05 13:57:26 -0500203#ifdef CONFIG_SYSFS
Christoph Lameter81819f02007-05-06 14:49:36 -0700204static int sysfs_slab_add(struct kmem_cache *);
205static int sysfs_slab_alias(struct kmem_cache *, const char *);
206static void sysfs_slab_remove(struct kmem_cache *);
Christoph Lameter8ff12cf2008-02-07 17:47:41 -0800207
Christoph Lameter81819f02007-05-06 14:49:36 -0700208#else
Christoph Lameter0c710012007-07-17 04:03:24 -0700209static inline int sysfs_slab_add(struct kmem_cache *s) { return 0; }
210static inline int sysfs_slab_alias(struct kmem_cache *s, const char *p)
211 { return 0; }
Christoph Lameter151c6022008-01-07 22:29:05 -0800212static inline void sysfs_slab_remove(struct kmem_cache *s)
213{
Pekka Enberg84c1cf62010-09-14 23:21:12 +0300214 kfree(s->name);
Christoph Lameter151c6022008-01-07 22:29:05 -0800215 kfree(s);
216}
Christoph Lameter8ff12cf2008-02-07 17:47:41 -0800217
Christoph Lameter81819f02007-05-06 14:49:36 -0700218#endif
219
Christoph Lameter4fdccdf2011-03-22 13:35:00 -0500220static inline void stat(const struct kmem_cache *s, enum stat_item si)
Christoph Lameter8ff12cf2008-02-07 17:47:41 -0800221{
222#ifdef CONFIG_SLUB_STATS
Christoph Lameter84e554e62009-12-18 16:26:23 -0600223 __this_cpu_inc(s->cpu_slab->stat[si]);
Christoph Lameter8ff12cf2008-02-07 17:47:41 -0800224#endif
225}
226
Christoph Lameter81819f02007-05-06 14:49:36 -0700227/********************************************************************
228 * Core slab cache functions
229 *******************************************************************/
230
231int slab_is_available(void)
232{
233 return slab_state >= UP;
234}
235
236static inline struct kmem_cache_node *get_node(struct kmem_cache *s, int node)
237{
Christoph Lameter81819f02007-05-06 14:49:36 -0700238 return s->node[node];
Christoph Lameter81819f02007-05-06 14:49:36 -0700239}
240
Christoph Lameter6446faa2008-02-15 23:45:26 -0800241/* Verify that a pointer has an address that is valid within a slab page */
Christoph Lameter02cbc872007-05-09 02:32:43 -0700242static inline int check_valid_pointer(struct kmem_cache *s,
243 struct page *page, const void *object)
244{
245 void *base;
246
Christoph Lametera973e9d2008-03-01 13:40:44 -0800247 if (!object)
Christoph Lameter02cbc872007-05-09 02:32:43 -0700248 return 1;
249
Christoph Lametera973e9d2008-03-01 13:40:44 -0800250 base = page_address(page);
Christoph Lameter39b26462008-04-14 19:11:30 +0300251 if (object < base || object >= base + page->objects * s->size ||
Christoph Lameter02cbc872007-05-09 02:32:43 -0700252 (object - base) % s->size) {
253 return 0;
254 }
255
256 return 1;
257}
258
Christoph Lameter7656c722007-05-09 02:32:40 -0700259static inline void *get_freepointer(struct kmem_cache *s, void *object)
260{
261 return *(void **)(object + s->offset);
262}
263
264static inline void set_freepointer(struct kmem_cache *s, void *object, void *fp)
265{
266 *(void **)(object + s->offset) = fp;
267}
268
269/* Loop over all objects in a slab */
Christoph Lameter224a88b2008-04-14 19:11:31 +0300270#define for_each_object(__p, __s, __addr, __objects) \
271 for (__p = (__addr); __p < (__addr) + (__objects) * (__s)->size;\
Christoph Lameter7656c722007-05-09 02:32:40 -0700272 __p += (__s)->size)
273
274/* Scan freelist */
275#define for_each_free_object(__p, __s, __free) \
Christoph Lametera973e9d2008-03-01 13:40:44 -0800276 for (__p = (__free); __p; __p = get_freepointer((__s), __p))
Christoph Lameter7656c722007-05-09 02:32:40 -0700277
278/* Determine object index from a given position */
279static inline int slab_index(void *p, struct kmem_cache *s, void *addr)
280{
281 return (p - addr) / s->size;
282}
283
Mariusz Kozlowskid71f6062011-02-26 20:10:26 +0100284static inline size_t slab_ksize(const struct kmem_cache *s)
285{
286#ifdef CONFIG_SLUB_DEBUG
287 /*
288 * Debugging requires use of the padding between object
289 * and whatever may come after it.
290 */
291 if (s->flags & (SLAB_RED_ZONE | SLAB_POISON))
292 return s->objsize;
293
294#endif
295 /*
296 * If we have the need to store the freelist pointer
297 * back there or track user information then we can
298 * only use the space before that information.
299 */
300 if (s->flags & (SLAB_DESTROY_BY_RCU | SLAB_STORE_USER))
301 return s->inuse;
302 /*
303 * Else we can use all the padding etc for the allocation
304 */
305 return s->size;
306}
307
Lai Jiangshanab9a0f12011-03-10 15:21:48 +0800308static inline int order_objects(int order, unsigned long size, int reserved)
309{
310 return ((PAGE_SIZE << order) - reserved) / size;
311}
312
Christoph Lameter834f3d12008-04-14 19:11:31 +0300313static inline struct kmem_cache_order_objects oo_make(int order,
Lai Jiangshanab9a0f12011-03-10 15:21:48 +0800314 unsigned long size, int reserved)
Christoph Lameter834f3d12008-04-14 19:11:31 +0300315{
316 struct kmem_cache_order_objects x = {
Lai Jiangshanab9a0f12011-03-10 15:21:48 +0800317 (order << OO_SHIFT) + order_objects(order, size, reserved)
Christoph Lameter834f3d12008-04-14 19:11:31 +0300318 };
319
320 return x;
321}
322
323static inline int oo_order(struct kmem_cache_order_objects x)
324{
Cyrill Gorcunov210b5c02008-10-22 23:00:38 +0400325 return x.x >> OO_SHIFT;
Christoph Lameter834f3d12008-04-14 19:11:31 +0300326}
327
328static inline int oo_objects(struct kmem_cache_order_objects x)
329{
Cyrill Gorcunov210b5c02008-10-22 23:00:38 +0400330 return x.x & OO_MASK;
Christoph Lameter834f3d12008-04-14 19:11:31 +0300331}
332
Christoph Lameter41ecc552007-05-09 02:32:44 -0700333#ifdef CONFIG_SLUB_DEBUG
334/*
335 * Debug settings:
336 */
Christoph Lameterf0630ff2007-07-15 23:38:14 -0700337#ifdef CONFIG_SLUB_DEBUG_ON
338static int slub_debug = DEBUG_DEFAULT_FLAGS;
339#else
Christoph Lameter41ecc552007-05-09 02:32:44 -0700340static int slub_debug;
Christoph Lameterf0630ff2007-07-15 23:38:14 -0700341#endif
Christoph Lameter41ecc552007-05-09 02:32:44 -0700342
343static char *slub_debug_slabs;
David Rientjesfa5ec8a2009-07-07 00:14:14 -0700344static int disable_higher_order_debug;
Christoph Lameter41ecc552007-05-09 02:32:44 -0700345
Christoph Lameter7656c722007-05-09 02:32:40 -0700346/*
Christoph Lameter81819f02007-05-06 14:49:36 -0700347 * Object debugging
348 */
349static void print_section(char *text, u8 *addr, unsigned int length)
350{
351 int i, offset;
352 int newline = 1;
353 char ascii[17];
354
355 ascii[16] = 0;
356
357 for (i = 0; i < length; i++) {
358 if (newline) {
Christoph Lameter24922682007-07-17 04:03:18 -0700359 printk(KERN_ERR "%8s 0x%p: ", text, addr + i);
Christoph Lameter81819f02007-05-06 14:49:36 -0700360 newline = 0;
361 }
Pekka Enberg06428782008-01-07 23:20:27 -0800362 printk(KERN_CONT " %02x", addr[i]);
Christoph Lameter81819f02007-05-06 14:49:36 -0700363 offset = i % 16;
364 ascii[offset] = isgraph(addr[i]) ? addr[i] : '.';
365 if (offset == 15) {
Pekka Enberg06428782008-01-07 23:20:27 -0800366 printk(KERN_CONT " %s\n", ascii);
Christoph Lameter81819f02007-05-06 14:49:36 -0700367 newline = 1;
368 }
369 }
370 if (!newline) {
371 i %= 16;
372 while (i < 16) {
Pekka Enberg06428782008-01-07 23:20:27 -0800373 printk(KERN_CONT " ");
Christoph Lameter81819f02007-05-06 14:49:36 -0700374 ascii[i] = ' ';
375 i++;
376 }
Pekka Enberg06428782008-01-07 23:20:27 -0800377 printk(KERN_CONT " %s\n", ascii);
Christoph Lameter81819f02007-05-06 14:49:36 -0700378 }
379}
380
Christoph Lameter81819f02007-05-06 14:49:36 -0700381static struct track *get_track(struct kmem_cache *s, void *object,
382 enum track_item alloc)
383{
384 struct track *p;
385
386 if (s->offset)
387 p = object + s->offset + sizeof(void *);
388 else
389 p = object + s->inuse;
390
391 return p + alloc;
392}
393
394static void set_track(struct kmem_cache *s, void *object,
Eduard - Gabriel Munteanuce71e272008-08-19 20:43:25 +0300395 enum track_item alloc, unsigned long addr)
Christoph Lameter81819f02007-05-06 14:49:36 -0700396{
Akinobu Mita1a00df42009-03-07 00:36:21 +0900397 struct track *p = get_track(s, object, alloc);
Christoph Lameter81819f02007-05-06 14:49:36 -0700398
Christoph Lameter81819f02007-05-06 14:49:36 -0700399 if (addr) {
400 p->addr = addr;
401 p->cpu = smp_processor_id();
Alexey Dobriyan88e4ccf2008-06-23 02:58:37 +0400402 p->pid = current->pid;
Christoph Lameter81819f02007-05-06 14:49:36 -0700403 p->when = jiffies;
404 } else
405 memset(p, 0, sizeof(struct track));
406}
407
Christoph Lameter81819f02007-05-06 14:49:36 -0700408static void init_tracking(struct kmem_cache *s, void *object)
409{
Christoph Lameter24922682007-07-17 04:03:18 -0700410 if (!(s->flags & SLAB_STORE_USER))
411 return;
412
Eduard - Gabriel Munteanuce71e272008-08-19 20:43:25 +0300413 set_track(s, object, TRACK_FREE, 0UL);
414 set_track(s, object, TRACK_ALLOC, 0UL);
Christoph Lameter81819f02007-05-06 14:49:36 -0700415}
416
417static void print_track(const char *s, struct track *t)
418{
419 if (!t->addr)
420 return;
421
Linus Torvalds7daf7052008-07-14 12:12:53 -0700422 printk(KERN_ERR "INFO: %s in %pS age=%lu cpu=%u pid=%d\n",
Eduard - Gabriel Munteanuce71e272008-08-19 20:43:25 +0300423 s, (void *)t->addr, jiffies - t->when, t->cpu, t->pid);
Christoph Lameter81819f02007-05-06 14:49:36 -0700424}
425
Christoph Lameter24922682007-07-17 04:03:18 -0700426static void print_tracking(struct kmem_cache *s, void *object)
427{
428 if (!(s->flags & SLAB_STORE_USER))
429 return;
430
431 print_track("Allocated", get_track(s, object, TRACK_ALLOC));
432 print_track("Freed", get_track(s, object, TRACK_FREE));
433}
434
435static void print_page_info(struct page *page)
436{
Christoph Lameter39b26462008-04-14 19:11:30 +0300437 printk(KERN_ERR "INFO: Slab 0x%p objects=%u used=%u fp=0x%p flags=0x%04lx\n",
438 page, page->objects, page->inuse, page->freelist, page->flags);
Christoph Lameter24922682007-07-17 04:03:18 -0700439
440}
441
442static void slab_bug(struct kmem_cache *s, char *fmt, ...)
443{
444 va_list args;
445 char buf[100];
446
447 va_start(args, fmt);
448 vsnprintf(buf, sizeof(buf), fmt, args);
449 va_end(args);
450 printk(KERN_ERR "========================================"
451 "=====================================\n");
452 printk(KERN_ERR "BUG %s: %s\n", s->name, buf);
453 printk(KERN_ERR "----------------------------------------"
454 "-------------------------------------\n\n");
455}
456
457static void slab_fix(struct kmem_cache *s, char *fmt, ...)
458{
459 va_list args;
460 char buf[100];
461
462 va_start(args, fmt);
463 vsnprintf(buf, sizeof(buf), fmt, args);
464 va_end(args);
465 printk(KERN_ERR "FIX %s: %s\n", s->name, buf);
466}
467
468static void print_trailer(struct kmem_cache *s, struct page *page, u8 *p)
Christoph Lameter81819f02007-05-06 14:49:36 -0700469{
470 unsigned int off; /* Offset of last byte */
Christoph Lametera973e9d2008-03-01 13:40:44 -0800471 u8 *addr = page_address(page);
Christoph Lameter24922682007-07-17 04:03:18 -0700472
473 print_tracking(s, p);
474
475 print_page_info(page);
476
477 printk(KERN_ERR "INFO: Object 0x%p @offset=%tu fp=0x%p\n\n",
478 p, p - addr, get_freepointer(s, p));
479
480 if (p > addr + 16)
481 print_section("Bytes b4", p - 16, 16);
482
Pekka Enberg0ebd6522008-07-19 14:17:22 +0300483 print_section("Object", p, min_t(unsigned long, s->objsize, PAGE_SIZE));
Christoph Lameter81819f02007-05-06 14:49:36 -0700484
485 if (s->flags & SLAB_RED_ZONE)
486 print_section("Redzone", p + s->objsize,
487 s->inuse - s->objsize);
488
Christoph Lameter81819f02007-05-06 14:49:36 -0700489 if (s->offset)
490 off = s->offset + sizeof(void *);
491 else
492 off = s->inuse;
493
Christoph Lameter24922682007-07-17 04:03:18 -0700494 if (s->flags & SLAB_STORE_USER)
Christoph Lameter81819f02007-05-06 14:49:36 -0700495 off += 2 * sizeof(struct track);
Christoph Lameter81819f02007-05-06 14:49:36 -0700496
497 if (off != s->size)
498 /* Beginning of the filler is the free pointer */
Christoph Lameter24922682007-07-17 04:03:18 -0700499 print_section("Padding", p + off, s->size - off);
500
501 dump_stack();
Christoph Lameter81819f02007-05-06 14:49:36 -0700502}
503
504static void object_err(struct kmem_cache *s, struct page *page,
505 u8 *object, char *reason)
506{
Christoph Lameter3dc50632008-04-23 12:28:01 -0700507 slab_bug(s, "%s", reason);
Christoph Lameter24922682007-07-17 04:03:18 -0700508 print_trailer(s, page, object);
Christoph Lameter81819f02007-05-06 14:49:36 -0700509}
510
Christoph Lameter24922682007-07-17 04:03:18 -0700511static void slab_err(struct kmem_cache *s, struct page *page, char *fmt, ...)
Christoph Lameter81819f02007-05-06 14:49:36 -0700512{
513 va_list args;
514 char buf[100];
515
Christoph Lameter24922682007-07-17 04:03:18 -0700516 va_start(args, fmt);
517 vsnprintf(buf, sizeof(buf), fmt, args);
Christoph Lameter81819f02007-05-06 14:49:36 -0700518 va_end(args);
Christoph Lameter3dc50632008-04-23 12:28:01 -0700519 slab_bug(s, "%s", buf);
Christoph Lameter24922682007-07-17 04:03:18 -0700520 print_page_info(page);
Christoph Lameter81819f02007-05-06 14:49:36 -0700521 dump_stack();
522}
523
Christoph Lameterf7cb1932010-09-29 07:15:01 -0500524static void init_object(struct kmem_cache *s, void *object, u8 val)
Christoph Lameter81819f02007-05-06 14:49:36 -0700525{
526 u8 *p = object;
527
528 if (s->flags & __OBJECT_POISON) {
529 memset(p, POISON_FREE, s->objsize - 1);
Pekka Enberg06428782008-01-07 23:20:27 -0800530 p[s->objsize - 1] = POISON_END;
Christoph Lameter81819f02007-05-06 14:49:36 -0700531 }
532
533 if (s->flags & SLAB_RED_ZONE)
Christoph Lameterf7cb1932010-09-29 07:15:01 -0500534 memset(p + s->objsize, val, s->inuse - s->objsize);
Christoph Lameter81819f02007-05-06 14:49:36 -0700535}
536
Christoph Lameter24922682007-07-17 04:03:18 -0700537static u8 *check_bytes(u8 *start, unsigned int value, unsigned int bytes)
Christoph Lameter81819f02007-05-06 14:49:36 -0700538{
539 while (bytes) {
540 if (*start != (u8)value)
Christoph Lameter24922682007-07-17 04:03:18 -0700541 return start;
Christoph Lameter81819f02007-05-06 14:49:36 -0700542 start++;
543 bytes--;
544 }
Christoph Lameter24922682007-07-17 04:03:18 -0700545 return NULL;
546}
547
548static void restore_bytes(struct kmem_cache *s, char *message, u8 data,
549 void *from, void *to)
550{
551 slab_fix(s, "Restoring 0x%p-0x%p=0x%x\n", from, to - 1, data);
552 memset(from, data, to - from);
553}
554
555static int check_bytes_and_report(struct kmem_cache *s, struct page *page,
556 u8 *object, char *what,
Pekka Enberg06428782008-01-07 23:20:27 -0800557 u8 *start, unsigned int value, unsigned int bytes)
Christoph Lameter24922682007-07-17 04:03:18 -0700558{
559 u8 *fault;
560 u8 *end;
561
562 fault = check_bytes(start, value, bytes);
563 if (!fault)
564 return 1;
565
566 end = start + bytes;
567 while (end > fault && end[-1] == value)
568 end--;
569
570 slab_bug(s, "%s overwritten", what);
571 printk(KERN_ERR "INFO: 0x%p-0x%p. First byte 0x%x instead of 0x%x\n",
572 fault, end - 1, fault[0], value);
573 print_trailer(s, page, object);
574
575 restore_bytes(s, what, value, fault, end);
576 return 0;
Christoph Lameter81819f02007-05-06 14:49:36 -0700577}
578
Christoph Lameter81819f02007-05-06 14:49:36 -0700579/*
580 * Object layout:
581 *
582 * object address
583 * Bytes of the object to be managed.
584 * If the freepointer may overlay the object then the free
585 * pointer is the first word of the object.
Christoph Lameter672bba32007-05-09 02:32:39 -0700586 *
Christoph Lameter81819f02007-05-06 14:49:36 -0700587 * Poisoning uses 0x6b (POISON_FREE) and the last byte is
588 * 0xa5 (POISON_END)
589 *
590 * object + s->objsize
591 * Padding to reach word boundary. This is also used for Redzoning.
Christoph Lameter672bba32007-05-09 02:32:39 -0700592 * Padding is extended by another word if Redzoning is enabled and
593 * objsize == inuse.
594 *
Christoph Lameter81819f02007-05-06 14:49:36 -0700595 * We fill with 0xbb (RED_INACTIVE) for inactive objects and with
596 * 0xcc (RED_ACTIVE) for objects in use.
597 *
598 * object + s->inuse
Christoph Lameter672bba32007-05-09 02:32:39 -0700599 * Meta data starts here.
600 *
Christoph Lameter81819f02007-05-06 14:49:36 -0700601 * A. Free pointer (if we cannot overwrite object on free)
602 * B. Tracking data for SLAB_STORE_USER
Christoph Lameter672bba32007-05-09 02:32:39 -0700603 * C. Padding to reach required alignment boundary or at mininum
Christoph Lameter6446faa2008-02-15 23:45:26 -0800604 * one word if debugging is on to be able to detect writes
Christoph Lameter672bba32007-05-09 02:32:39 -0700605 * before the word boundary.
606 *
607 * Padding is done using 0x5a (POISON_INUSE)
Christoph Lameter81819f02007-05-06 14:49:36 -0700608 *
609 * object + s->size
Christoph Lameter672bba32007-05-09 02:32:39 -0700610 * Nothing is used beyond s->size.
Christoph Lameter81819f02007-05-06 14:49:36 -0700611 *
Christoph Lameter672bba32007-05-09 02:32:39 -0700612 * If slabcaches are merged then the objsize and inuse boundaries are mostly
613 * ignored. And therefore no slab options that rely on these boundaries
Christoph Lameter81819f02007-05-06 14:49:36 -0700614 * may be used with merged slabcaches.
615 */
616
Christoph Lameter81819f02007-05-06 14:49:36 -0700617static int check_pad_bytes(struct kmem_cache *s, struct page *page, u8 *p)
618{
619 unsigned long off = s->inuse; /* The end of info */
620
621 if (s->offset)
622 /* Freepointer is placed after the object. */
623 off += sizeof(void *);
624
625 if (s->flags & SLAB_STORE_USER)
626 /* We also have user information there */
627 off += 2 * sizeof(struct track);
628
629 if (s->size == off)
630 return 1;
631
Christoph Lameter24922682007-07-17 04:03:18 -0700632 return check_bytes_and_report(s, page, p, "Object padding",
633 p + off, POISON_INUSE, s->size - off);
Christoph Lameter81819f02007-05-06 14:49:36 -0700634}
635
Christoph Lameter39b26462008-04-14 19:11:30 +0300636/* Check the pad bytes at the end of a slab page */
Christoph Lameter81819f02007-05-06 14:49:36 -0700637static int slab_pad_check(struct kmem_cache *s, struct page *page)
638{
Christoph Lameter24922682007-07-17 04:03:18 -0700639 u8 *start;
640 u8 *fault;
641 u8 *end;
642 int length;
643 int remainder;
Christoph Lameter81819f02007-05-06 14:49:36 -0700644
645 if (!(s->flags & SLAB_POISON))
646 return 1;
647
Christoph Lametera973e9d2008-03-01 13:40:44 -0800648 start = page_address(page);
Lai Jiangshanab9a0f12011-03-10 15:21:48 +0800649 length = (PAGE_SIZE << compound_order(page)) - s->reserved;
Christoph Lameter39b26462008-04-14 19:11:30 +0300650 end = start + length;
651 remainder = length % s->size;
Christoph Lameter81819f02007-05-06 14:49:36 -0700652 if (!remainder)
653 return 1;
654
Christoph Lameter39b26462008-04-14 19:11:30 +0300655 fault = check_bytes(end - remainder, POISON_INUSE, remainder);
Christoph Lameter24922682007-07-17 04:03:18 -0700656 if (!fault)
657 return 1;
658 while (end > fault && end[-1] == POISON_INUSE)
659 end--;
660
661 slab_err(s, page, "Padding overwritten. 0x%p-0x%p", fault, end - 1);
Christoph Lameter39b26462008-04-14 19:11:30 +0300662 print_section("Padding", end - remainder, remainder);
Christoph Lameter24922682007-07-17 04:03:18 -0700663
Eric Dumazet8a3d2712009-09-03 16:08:06 +0200664 restore_bytes(s, "slab padding", POISON_INUSE, end - remainder, end);
Christoph Lameter24922682007-07-17 04:03:18 -0700665 return 0;
Christoph Lameter81819f02007-05-06 14:49:36 -0700666}
667
668static int check_object(struct kmem_cache *s, struct page *page,
Christoph Lameterf7cb1932010-09-29 07:15:01 -0500669 void *object, u8 val)
Christoph Lameter81819f02007-05-06 14:49:36 -0700670{
671 u8 *p = object;
672 u8 *endobject = object + s->objsize;
673
674 if (s->flags & SLAB_RED_ZONE) {
Christoph Lameter24922682007-07-17 04:03:18 -0700675 if (!check_bytes_and_report(s, page, object, "Redzone",
Christoph Lameterf7cb1932010-09-29 07:15:01 -0500676 endobject, val, s->inuse - s->objsize))
Christoph Lameter81819f02007-05-06 14:49:36 -0700677 return 0;
Christoph Lameter81819f02007-05-06 14:49:36 -0700678 } else {
Ingo Molnar3adbefe2008-02-05 17:57:39 -0800679 if ((s->flags & SLAB_POISON) && s->objsize < s->inuse) {
680 check_bytes_and_report(s, page, p, "Alignment padding",
681 endobject, POISON_INUSE, s->inuse - s->objsize);
682 }
Christoph Lameter81819f02007-05-06 14:49:36 -0700683 }
684
685 if (s->flags & SLAB_POISON) {
Christoph Lameterf7cb1932010-09-29 07:15:01 -0500686 if (val != SLUB_RED_ACTIVE && (s->flags & __OBJECT_POISON) &&
Christoph Lameter24922682007-07-17 04:03:18 -0700687 (!check_bytes_and_report(s, page, p, "Poison", p,
688 POISON_FREE, s->objsize - 1) ||
689 !check_bytes_and_report(s, page, p, "Poison",
Pekka Enberg06428782008-01-07 23:20:27 -0800690 p + s->objsize - 1, POISON_END, 1)))
Christoph Lameter81819f02007-05-06 14:49:36 -0700691 return 0;
Christoph Lameter81819f02007-05-06 14:49:36 -0700692 /*
693 * check_pad_bytes cleans up on its own.
694 */
695 check_pad_bytes(s, page, p);
696 }
697
Christoph Lameterf7cb1932010-09-29 07:15:01 -0500698 if (!s->offset && val == SLUB_RED_ACTIVE)
Christoph Lameter81819f02007-05-06 14:49:36 -0700699 /*
700 * Object and freepointer overlap. Cannot check
701 * freepointer while object is allocated.
702 */
703 return 1;
704
705 /* Check free pointer validity */
706 if (!check_valid_pointer(s, page, get_freepointer(s, p))) {
707 object_err(s, page, p, "Freepointer corrupt");
708 /*
Nick Andrew9f6c708e2008-12-05 14:08:08 +1100709 * No choice but to zap it and thus lose the remainder
Christoph Lameter81819f02007-05-06 14:49:36 -0700710 * of the free objects in this slab. May cause
Christoph Lameter672bba32007-05-09 02:32:39 -0700711 * another error because the object count is now wrong.
Christoph Lameter81819f02007-05-06 14:49:36 -0700712 */
Christoph Lametera973e9d2008-03-01 13:40:44 -0800713 set_freepointer(s, p, NULL);
Christoph Lameter81819f02007-05-06 14:49:36 -0700714 return 0;
715 }
716 return 1;
717}
718
719static int check_slab(struct kmem_cache *s, struct page *page)
720{
Christoph Lameter39b26462008-04-14 19:11:30 +0300721 int maxobj;
722
Christoph Lameter81819f02007-05-06 14:49:36 -0700723 VM_BUG_ON(!irqs_disabled());
724
725 if (!PageSlab(page)) {
Christoph Lameter24922682007-07-17 04:03:18 -0700726 slab_err(s, page, "Not a valid slab page");
Christoph Lameter81819f02007-05-06 14:49:36 -0700727 return 0;
728 }
Christoph Lameter39b26462008-04-14 19:11:30 +0300729
Lai Jiangshanab9a0f12011-03-10 15:21:48 +0800730 maxobj = order_objects(compound_order(page), s->size, s->reserved);
Christoph Lameter39b26462008-04-14 19:11:30 +0300731 if (page->objects > maxobj) {
732 slab_err(s, page, "objects %u > max %u",
733 s->name, page->objects, maxobj);
734 return 0;
735 }
736 if (page->inuse > page->objects) {
Christoph Lameter24922682007-07-17 04:03:18 -0700737 slab_err(s, page, "inuse %u > max %u",
Christoph Lameter39b26462008-04-14 19:11:30 +0300738 s->name, page->inuse, page->objects);
Christoph Lameter81819f02007-05-06 14:49:36 -0700739 return 0;
740 }
741 /* Slab_pad_check fixes things up after itself */
742 slab_pad_check(s, page);
743 return 1;
744}
745
746/*
Christoph Lameter672bba32007-05-09 02:32:39 -0700747 * Determine if a certain object on a page is on the freelist. Must hold the
748 * slab lock to guarantee that the chains are in a consistent state.
Christoph Lameter81819f02007-05-06 14:49:36 -0700749 */
750static int on_freelist(struct kmem_cache *s, struct page *page, void *search)
751{
752 int nr = 0;
753 void *fp = page->freelist;
754 void *object = NULL;
Christoph Lameter224a88b2008-04-14 19:11:31 +0300755 unsigned long max_objects;
Christoph Lameter81819f02007-05-06 14:49:36 -0700756
Christoph Lameter39b26462008-04-14 19:11:30 +0300757 while (fp && nr <= page->objects) {
Christoph Lameter81819f02007-05-06 14:49:36 -0700758 if (fp == search)
759 return 1;
760 if (!check_valid_pointer(s, page, fp)) {
761 if (object) {
762 object_err(s, page, object,
763 "Freechain corrupt");
Christoph Lametera973e9d2008-03-01 13:40:44 -0800764 set_freepointer(s, object, NULL);
Christoph Lameter81819f02007-05-06 14:49:36 -0700765 break;
766 } else {
Christoph Lameter24922682007-07-17 04:03:18 -0700767 slab_err(s, page, "Freepointer corrupt");
Christoph Lametera973e9d2008-03-01 13:40:44 -0800768 page->freelist = NULL;
Christoph Lameter39b26462008-04-14 19:11:30 +0300769 page->inuse = page->objects;
Christoph Lameter24922682007-07-17 04:03:18 -0700770 slab_fix(s, "Freelist cleared");
Christoph Lameter81819f02007-05-06 14:49:36 -0700771 return 0;
772 }
773 break;
774 }
775 object = fp;
776 fp = get_freepointer(s, object);
777 nr++;
778 }
779
Lai Jiangshanab9a0f12011-03-10 15:21:48 +0800780 max_objects = order_objects(compound_order(page), s->size, s->reserved);
Cyrill Gorcunov210b5c02008-10-22 23:00:38 +0400781 if (max_objects > MAX_OBJS_PER_PAGE)
782 max_objects = MAX_OBJS_PER_PAGE;
Christoph Lameter224a88b2008-04-14 19:11:31 +0300783
784 if (page->objects != max_objects) {
785 slab_err(s, page, "Wrong number of objects. Found %d but "
786 "should be %d", page->objects, max_objects);
787 page->objects = max_objects;
788 slab_fix(s, "Number of objects adjusted.");
789 }
Christoph Lameter39b26462008-04-14 19:11:30 +0300790 if (page->inuse != page->objects - nr) {
Christoph Lameter70d71222007-05-06 14:49:47 -0700791 slab_err(s, page, "Wrong object count. Counter is %d but "
Christoph Lameter39b26462008-04-14 19:11:30 +0300792 "counted were %d", page->inuse, page->objects - nr);
793 page->inuse = page->objects - nr;
Christoph Lameter24922682007-07-17 04:03:18 -0700794 slab_fix(s, "Object count adjusted.");
Christoph Lameter81819f02007-05-06 14:49:36 -0700795 }
796 return search == NULL;
797}
798
Christoph Lameter0121c6192008-04-29 16:11:12 -0700799static void trace(struct kmem_cache *s, struct page *page, void *object,
800 int alloc)
Christoph Lameter3ec09742007-05-16 22:11:00 -0700801{
802 if (s->flags & SLAB_TRACE) {
803 printk(KERN_INFO "TRACE %s %s 0x%p inuse=%d fp=0x%p\n",
804 s->name,
805 alloc ? "alloc" : "free",
806 object, page->inuse,
807 page->freelist);
808
809 if (!alloc)
810 print_section("Object", (void *)object, s->objsize);
811
812 dump_stack();
813 }
814}
815
Christoph Lameter643b1132007-05-06 14:49:42 -0700816/*
Christoph Lameterc016b0b2010-08-20 12:37:16 -0500817 * Hooks for other subsystems that check memory allocations. In a typical
818 * production configuration these hooks all should produce no code at all.
819 */
820static inline int slab_pre_alloc_hook(struct kmem_cache *s, gfp_t flags)
821{
Christoph Lameterc1d50832010-08-20 12:37:17 -0500822 flags &= gfp_allowed_mask;
Christoph Lameterc016b0b2010-08-20 12:37:16 -0500823 lockdep_trace_alloc(flags);
824 might_sleep_if(flags & __GFP_WAIT);
825
826 return should_failslab(s->objsize, flags, s->flags);
827}
828
829static inline void slab_post_alloc_hook(struct kmem_cache *s, gfp_t flags, void *object)
830{
Christoph Lameterc1d50832010-08-20 12:37:17 -0500831 flags &= gfp_allowed_mask;
Eric Dumazetb3d41882011-02-14 18:35:22 +0100832 kmemcheck_slab_alloc(s, flags, object, slab_ksize(s));
Christoph Lameterc016b0b2010-08-20 12:37:16 -0500833 kmemleak_alloc_recursive(object, s->objsize, 1, s->flags, flags);
834}
835
836static inline void slab_free_hook(struct kmem_cache *s, void *x)
837{
838 kmemleak_free_recursive(x, s->flags);
Christoph Lameterc016b0b2010-08-20 12:37:16 -0500839
Christoph Lameterd3f661d2011-02-25 11:38:52 -0600840 /*
841 * Trouble is that we may no longer disable interupts in the fast path
842 * So in order to make the debug calls that expect irqs to be
843 * disabled we need to disable interrupts temporarily.
844 */
845#if defined(CONFIG_KMEMCHECK) || defined(CONFIG_LOCKDEP)
846 {
847 unsigned long flags;
848
849 local_irq_save(flags);
850 kmemcheck_slab_free(s, x, s->objsize);
851 debug_check_no_locks_freed(x, s->objsize);
Christoph Lameterd3f661d2011-02-25 11:38:52 -0600852 local_irq_restore(flags);
853 }
854#endif
Thomas Gleixnerf9b615d2011-03-24 21:26:46 +0200855 if (!(s->flags & SLAB_DEBUG_OBJECTS))
856 debug_check_no_obj_freed(x, s->objsize);
Christoph Lameterc016b0b2010-08-20 12:37:16 -0500857}
858
859/*
Christoph Lameter672bba32007-05-09 02:32:39 -0700860 * Tracking of fully allocated slabs for debugging purposes.
Christoph Lameter643b1132007-05-06 14:49:42 -0700861 */
Christoph Lametere95eed52007-05-06 14:49:44 -0700862static void add_full(struct kmem_cache_node *n, struct page *page)
Christoph Lameter643b1132007-05-06 14:49:42 -0700863{
Christoph Lameter643b1132007-05-06 14:49:42 -0700864 spin_lock(&n->list_lock);
865 list_add(&page->lru, &n->full);
866 spin_unlock(&n->list_lock);
867}
868
869static void remove_full(struct kmem_cache *s, struct page *page)
870{
871 struct kmem_cache_node *n;
872
873 if (!(s->flags & SLAB_STORE_USER))
874 return;
875
876 n = get_node(s, page_to_nid(page));
877
878 spin_lock(&n->list_lock);
879 list_del(&page->lru);
880 spin_unlock(&n->list_lock);
881}
882
Christoph Lameter0f389ec2008-04-14 18:53:02 +0300883/* Tracking of the number of slabs for debugging purposes */
884static inline unsigned long slabs_node(struct kmem_cache *s, int node)
885{
886 struct kmem_cache_node *n = get_node(s, node);
887
888 return atomic_long_read(&n->nr_slabs);
889}
890
Alexander Beregalov26c02cf2009-06-11 14:08:48 +0400891static inline unsigned long node_nr_slabs(struct kmem_cache_node *n)
892{
893 return atomic_long_read(&n->nr_slabs);
894}
895
Christoph Lameter205ab992008-04-14 19:11:40 +0300896static inline void inc_slabs_node(struct kmem_cache *s, int node, int objects)
Christoph Lameter0f389ec2008-04-14 18:53:02 +0300897{
898 struct kmem_cache_node *n = get_node(s, node);
899
900 /*
901 * May be called early in order to allocate a slab for the
902 * kmem_cache_node structure. Solve the chicken-egg
903 * dilemma by deferring the increment of the count during
904 * bootstrap (see early_kmem_cache_node_alloc).
905 */
Christoph Lameter7340cc82010-09-28 08:10:26 -0500906 if (n) {
Christoph Lameter0f389ec2008-04-14 18:53:02 +0300907 atomic_long_inc(&n->nr_slabs);
Christoph Lameter205ab992008-04-14 19:11:40 +0300908 atomic_long_add(objects, &n->total_objects);
909 }
Christoph Lameter0f389ec2008-04-14 18:53:02 +0300910}
Christoph Lameter205ab992008-04-14 19:11:40 +0300911static inline void dec_slabs_node(struct kmem_cache *s, int node, int objects)
Christoph Lameter0f389ec2008-04-14 18:53:02 +0300912{
913 struct kmem_cache_node *n = get_node(s, node);
914
915 atomic_long_dec(&n->nr_slabs);
Christoph Lameter205ab992008-04-14 19:11:40 +0300916 atomic_long_sub(objects, &n->total_objects);
Christoph Lameter0f389ec2008-04-14 18:53:02 +0300917}
918
919/* Object debug checks for alloc/free paths */
Christoph Lameter3ec09742007-05-16 22:11:00 -0700920static void setup_object_debug(struct kmem_cache *s, struct page *page,
921 void *object)
922{
923 if (!(s->flags & (SLAB_STORE_USER|SLAB_RED_ZONE|__OBJECT_POISON)))
924 return;
925
Christoph Lameterf7cb1932010-09-29 07:15:01 -0500926 init_object(s, object, SLUB_RED_INACTIVE);
Christoph Lameter3ec09742007-05-16 22:11:00 -0700927 init_tracking(s, object);
928}
929
Christoph Lameter15370662010-08-20 12:37:12 -0500930static noinline int alloc_debug_processing(struct kmem_cache *s, struct page *page,
Eduard - Gabriel Munteanuce71e272008-08-19 20:43:25 +0300931 void *object, unsigned long addr)
Christoph Lameter81819f02007-05-06 14:49:36 -0700932{
933 if (!check_slab(s, page))
934 goto bad;
935
Christoph Lameterd692ef62008-02-15 23:45:24 -0800936 if (!on_freelist(s, page, object)) {
Christoph Lameter24922682007-07-17 04:03:18 -0700937 object_err(s, page, object, "Object already allocated");
Christoph Lameter70d71222007-05-06 14:49:47 -0700938 goto bad;
Christoph Lameter81819f02007-05-06 14:49:36 -0700939 }
940
941 if (!check_valid_pointer(s, page, object)) {
942 object_err(s, page, object, "Freelist Pointer check fails");
Christoph Lameter70d71222007-05-06 14:49:47 -0700943 goto bad;
Christoph Lameter81819f02007-05-06 14:49:36 -0700944 }
945
Christoph Lameterf7cb1932010-09-29 07:15:01 -0500946 if (!check_object(s, page, object, SLUB_RED_INACTIVE))
Christoph Lameter81819f02007-05-06 14:49:36 -0700947 goto bad;
Christoph Lameter81819f02007-05-06 14:49:36 -0700948
Christoph Lameter3ec09742007-05-16 22:11:00 -0700949 /* Success perform special debug activities for allocs */
950 if (s->flags & SLAB_STORE_USER)
951 set_track(s, object, TRACK_ALLOC, addr);
952 trace(s, page, object, 1);
Christoph Lameterf7cb1932010-09-29 07:15:01 -0500953 init_object(s, object, SLUB_RED_ACTIVE);
Christoph Lameter81819f02007-05-06 14:49:36 -0700954 return 1;
Christoph Lameter3ec09742007-05-16 22:11:00 -0700955
Christoph Lameter81819f02007-05-06 14:49:36 -0700956bad:
957 if (PageSlab(page)) {
958 /*
959 * If this is a slab page then lets do the best we can
960 * to avoid issues in the future. Marking all objects
Christoph Lameter672bba32007-05-09 02:32:39 -0700961 * as used avoids touching the remaining objects.
Christoph Lameter81819f02007-05-06 14:49:36 -0700962 */
Christoph Lameter24922682007-07-17 04:03:18 -0700963 slab_fix(s, "Marking all objects used");
Christoph Lameter39b26462008-04-14 19:11:30 +0300964 page->inuse = page->objects;
Christoph Lametera973e9d2008-03-01 13:40:44 -0800965 page->freelist = NULL;
Christoph Lameter81819f02007-05-06 14:49:36 -0700966 }
967 return 0;
968}
969
Christoph Lameter15370662010-08-20 12:37:12 -0500970static noinline int free_debug_processing(struct kmem_cache *s,
971 struct page *page, void *object, unsigned long addr)
Christoph Lameter81819f02007-05-06 14:49:36 -0700972{
973 if (!check_slab(s, page))
974 goto fail;
975
976 if (!check_valid_pointer(s, page, object)) {
Christoph Lameter70d71222007-05-06 14:49:47 -0700977 slab_err(s, page, "Invalid object pointer 0x%p", object);
Christoph Lameter81819f02007-05-06 14:49:36 -0700978 goto fail;
979 }
980
981 if (on_freelist(s, page, object)) {
Christoph Lameter24922682007-07-17 04:03:18 -0700982 object_err(s, page, object, "Object already free");
Christoph Lameter81819f02007-05-06 14:49:36 -0700983 goto fail;
984 }
985
Christoph Lameterf7cb1932010-09-29 07:15:01 -0500986 if (!check_object(s, page, object, SLUB_RED_ACTIVE))
Christoph Lameter81819f02007-05-06 14:49:36 -0700987 return 0;
988
989 if (unlikely(s != page->slab)) {
Ingo Molnar3adbefe2008-02-05 17:57:39 -0800990 if (!PageSlab(page)) {
Christoph Lameter70d71222007-05-06 14:49:47 -0700991 slab_err(s, page, "Attempt to free object(0x%p) "
992 "outside of slab", object);
Ingo Molnar3adbefe2008-02-05 17:57:39 -0800993 } else if (!page->slab) {
Christoph Lameter81819f02007-05-06 14:49:36 -0700994 printk(KERN_ERR
Christoph Lameter70d71222007-05-06 14:49:47 -0700995 "SLUB <none>: no slab for object 0x%p.\n",
Christoph Lameter81819f02007-05-06 14:49:36 -0700996 object);
Christoph Lameter70d71222007-05-06 14:49:47 -0700997 dump_stack();
Pekka Enberg06428782008-01-07 23:20:27 -0800998 } else
Christoph Lameter24922682007-07-17 04:03:18 -0700999 object_err(s, page, object,
1000 "page slab pointer corrupt.");
Christoph Lameter81819f02007-05-06 14:49:36 -07001001 goto fail;
1002 }
Christoph Lameter3ec09742007-05-16 22:11:00 -07001003
1004 /* Special debug activities for freeing objects */
Andy Whitcroft8a380822008-07-23 21:27:18 -07001005 if (!PageSlubFrozen(page) && !page->freelist)
Christoph Lameter3ec09742007-05-16 22:11:00 -07001006 remove_full(s, page);
1007 if (s->flags & SLAB_STORE_USER)
1008 set_track(s, object, TRACK_FREE, addr);
1009 trace(s, page, object, 0);
Christoph Lameterf7cb1932010-09-29 07:15:01 -05001010 init_object(s, object, SLUB_RED_INACTIVE);
Christoph Lameter81819f02007-05-06 14:49:36 -07001011 return 1;
Christoph Lameter3ec09742007-05-16 22:11:00 -07001012
Christoph Lameter81819f02007-05-06 14:49:36 -07001013fail:
Christoph Lameter24922682007-07-17 04:03:18 -07001014 slab_fix(s, "Object at 0x%p not freed", object);
Christoph Lameter81819f02007-05-06 14:49:36 -07001015 return 0;
1016}
1017
Christoph Lameter41ecc552007-05-09 02:32:44 -07001018static int __init setup_slub_debug(char *str)
1019{
Christoph Lameterf0630ff2007-07-15 23:38:14 -07001020 slub_debug = DEBUG_DEFAULT_FLAGS;
1021 if (*str++ != '=' || !*str)
1022 /*
1023 * No options specified. Switch on full debugging.
1024 */
1025 goto out;
Christoph Lameter41ecc552007-05-09 02:32:44 -07001026
1027 if (*str == ',')
Christoph Lameterf0630ff2007-07-15 23:38:14 -07001028 /*
1029 * No options but restriction on slabs. This means full
1030 * debugging for slabs matching a pattern.
1031 */
1032 goto check_slabs;
1033
David Rientjesfa5ec8a2009-07-07 00:14:14 -07001034 if (tolower(*str) == 'o') {
1035 /*
1036 * Avoid enabling debugging on caches if its minimum order
1037 * would increase as a result.
1038 */
1039 disable_higher_order_debug = 1;
1040 goto out;
1041 }
1042
Christoph Lameterf0630ff2007-07-15 23:38:14 -07001043 slub_debug = 0;
1044 if (*str == '-')
1045 /*
1046 * Switch off all debugging measures.
1047 */
1048 goto out;
1049
1050 /*
1051 * Determine which debug features should be switched on
1052 */
Pekka Enberg06428782008-01-07 23:20:27 -08001053 for (; *str && *str != ','; str++) {
Christoph Lameterf0630ff2007-07-15 23:38:14 -07001054 switch (tolower(*str)) {
1055 case 'f':
1056 slub_debug |= SLAB_DEBUG_FREE;
1057 break;
1058 case 'z':
1059 slub_debug |= SLAB_RED_ZONE;
1060 break;
1061 case 'p':
1062 slub_debug |= SLAB_POISON;
1063 break;
1064 case 'u':
1065 slub_debug |= SLAB_STORE_USER;
1066 break;
1067 case 't':
1068 slub_debug |= SLAB_TRACE;
1069 break;
Dmitry Monakhov4c13dd32010-02-26 09:36:12 +03001070 case 'a':
1071 slub_debug |= SLAB_FAILSLAB;
1072 break;
Christoph Lameterf0630ff2007-07-15 23:38:14 -07001073 default:
1074 printk(KERN_ERR "slub_debug option '%c' "
Pekka Enberg06428782008-01-07 23:20:27 -08001075 "unknown. skipped\n", *str);
Christoph Lameterf0630ff2007-07-15 23:38:14 -07001076 }
1077 }
1078
1079check_slabs:
1080 if (*str == ',')
Christoph Lameter41ecc552007-05-09 02:32:44 -07001081 slub_debug_slabs = str + 1;
Christoph Lameterf0630ff2007-07-15 23:38:14 -07001082out:
Christoph Lameter41ecc552007-05-09 02:32:44 -07001083 return 1;
1084}
1085
1086__setup("slub_debug", setup_slub_debug);
1087
Christoph Lameterba0268a2007-09-11 15:24:11 -07001088static unsigned long kmem_cache_flags(unsigned long objsize,
1089 unsigned long flags, const char *name,
Alexey Dobriyan51cc5062008-07-25 19:45:34 -07001090 void (*ctor)(void *))
Christoph Lameter41ecc552007-05-09 02:32:44 -07001091{
1092 /*
Christoph Lametere1533622008-02-15 23:45:24 -08001093 * Enable debugging if selected on the kernel commandline.
Christoph Lameter41ecc552007-05-09 02:32:44 -07001094 */
Christoph Lametere1533622008-02-15 23:45:24 -08001095 if (slub_debug && (!slub_debug_slabs ||
David Rientjes3de47212009-07-27 18:30:35 -07001096 !strncmp(slub_debug_slabs, name, strlen(slub_debug_slabs))))
1097 flags |= slub_debug;
Christoph Lameterba0268a2007-09-11 15:24:11 -07001098
1099 return flags;
Christoph Lameter41ecc552007-05-09 02:32:44 -07001100}
1101#else
Christoph Lameter3ec09742007-05-16 22:11:00 -07001102static inline void setup_object_debug(struct kmem_cache *s,
1103 struct page *page, void *object) {}
Christoph Lameter41ecc552007-05-09 02:32:44 -07001104
Christoph Lameter3ec09742007-05-16 22:11:00 -07001105static inline int alloc_debug_processing(struct kmem_cache *s,
Eduard - Gabriel Munteanuce71e272008-08-19 20:43:25 +03001106 struct page *page, void *object, unsigned long addr) { return 0; }
Christoph Lameter41ecc552007-05-09 02:32:44 -07001107
Christoph Lameter3ec09742007-05-16 22:11:00 -07001108static inline int free_debug_processing(struct kmem_cache *s,
Eduard - Gabriel Munteanuce71e272008-08-19 20:43:25 +03001109 struct page *page, void *object, unsigned long addr) { return 0; }
Christoph Lameter41ecc552007-05-09 02:32:44 -07001110
Christoph Lameter41ecc552007-05-09 02:32:44 -07001111static inline int slab_pad_check(struct kmem_cache *s, struct page *page)
1112 { return 1; }
1113static inline int check_object(struct kmem_cache *s, struct page *page,
Christoph Lameterf7cb1932010-09-29 07:15:01 -05001114 void *object, u8 val) { return 1; }
Christoph Lameter3ec09742007-05-16 22:11:00 -07001115static inline void add_full(struct kmem_cache_node *n, struct page *page) {}
Christoph Lameterba0268a2007-09-11 15:24:11 -07001116static inline unsigned long kmem_cache_flags(unsigned long objsize,
1117 unsigned long flags, const char *name,
Alexey Dobriyan51cc5062008-07-25 19:45:34 -07001118 void (*ctor)(void *))
Christoph Lameterba0268a2007-09-11 15:24:11 -07001119{
1120 return flags;
1121}
Christoph Lameter41ecc552007-05-09 02:32:44 -07001122#define slub_debug 0
Christoph Lameter0f389ec2008-04-14 18:53:02 +03001123
Ingo Molnarfdaa45e2009-09-15 11:00:26 +02001124#define disable_higher_order_debug 0
1125
Christoph Lameter0f389ec2008-04-14 18:53:02 +03001126static inline unsigned long slabs_node(struct kmem_cache *s, int node)
1127 { return 0; }
Alexander Beregalov26c02cf2009-06-11 14:08:48 +04001128static inline unsigned long node_nr_slabs(struct kmem_cache_node *n)
1129 { return 0; }
Christoph Lameter205ab992008-04-14 19:11:40 +03001130static inline void inc_slabs_node(struct kmem_cache *s, int node,
1131 int objects) {}
1132static inline void dec_slabs_node(struct kmem_cache *s, int node,
1133 int objects) {}
Christoph Lameter7d550c52010-08-25 14:07:16 -05001134
1135static inline int slab_pre_alloc_hook(struct kmem_cache *s, gfp_t flags)
1136 { return 0; }
1137
1138static inline void slab_post_alloc_hook(struct kmem_cache *s, gfp_t flags,
1139 void *object) {}
1140
1141static inline void slab_free_hook(struct kmem_cache *s, void *x) {}
1142
Christoph Lameterab4d5ed2010-10-05 13:57:26 -05001143#endif /* CONFIG_SLUB_DEBUG */
Christoph Lameter205ab992008-04-14 19:11:40 +03001144
Christoph Lameter81819f02007-05-06 14:49:36 -07001145/*
1146 * Slab allocation and freeing
1147 */
Christoph Lameter65c33762008-04-14 19:11:40 +03001148static inline struct page *alloc_slab_page(gfp_t flags, int node,
1149 struct kmem_cache_order_objects oo)
1150{
1151 int order = oo_order(oo);
1152
Vegard Nossumb1eeab62008-11-25 16:55:53 +01001153 flags |= __GFP_NOTRACK;
1154
Christoph Lameter2154a332010-07-09 14:07:10 -05001155 if (node == NUMA_NO_NODE)
Christoph Lameter65c33762008-04-14 19:11:40 +03001156 return alloc_pages(flags, order);
1157 else
Minchan Kim6b65aaf2010-04-14 23:58:36 +09001158 return alloc_pages_exact_node(node, flags, order);
Christoph Lameter65c33762008-04-14 19:11:40 +03001159}
1160
Christoph Lameter81819f02007-05-06 14:49:36 -07001161static struct page *allocate_slab(struct kmem_cache *s, gfp_t flags, int node)
1162{
Pekka Enberg06428782008-01-07 23:20:27 -08001163 struct page *page;
Christoph Lameter834f3d12008-04-14 19:11:31 +03001164 struct kmem_cache_order_objects oo = s->oo;
Pekka Enbergba522702009-06-24 21:59:51 +03001165 gfp_t alloc_gfp;
Christoph Lameter81819f02007-05-06 14:49:36 -07001166
Christoph Lameterb7a49f02008-02-14 14:21:32 -08001167 flags |= s->allocflags;
Mel Gormane12ba742007-10-16 01:25:52 -07001168
Pekka Enbergba522702009-06-24 21:59:51 +03001169 /*
1170 * Let the initial higher-order allocation fail under memory pressure
1171 * so we fall-back to the minimum order allocation.
1172 */
1173 alloc_gfp = (flags | __GFP_NOWARN | __GFP_NORETRY) & ~__GFP_NOFAIL;
1174
1175 page = alloc_slab_page(alloc_gfp, node, oo);
Christoph Lameter65c33762008-04-14 19:11:40 +03001176 if (unlikely(!page)) {
1177 oo = s->min;
1178 /*
1179 * Allocation may have failed due to fragmentation.
1180 * Try a lower order alloc if possible
1181 */
1182 page = alloc_slab_page(flags, node, oo);
1183 if (!page)
1184 return NULL;
Christoph Lameter81819f02007-05-06 14:49:36 -07001185
Christoph Lameter84e554e62009-12-18 16:26:23 -06001186 stat(s, ORDER_FALLBACK);
Christoph Lameter65c33762008-04-14 19:11:40 +03001187 }
Vegard Nossum5a896d92008-04-04 00:54:48 +02001188
1189 if (kmemcheck_enabled
Amerigo Wang5086c389c2009-08-19 21:44:13 +03001190 && !(s->flags & (SLAB_NOTRACK | DEBUG_DEFAULT_FLAGS))) {
Vegard Nossumb1eeab62008-11-25 16:55:53 +01001191 int pages = 1 << oo_order(oo);
1192
1193 kmemcheck_alloc_shadow(page, oo_order(oo), flags, node);
1194
1195 /*
1196 * Objects from caches that have a constructor don't get
1197 * cleared when they're allocated, so we need to do it here.
1198 */
1199 if (s->ctor)
1200 kmemcheck_mark_uninitialized_pages(page, pages);
1201 else
1202 kmemcheck_mark_unallocated_pages(page, pages);
Vegard Nossum5a896d92008-04-04 00:54:48 +02001203 }
1204
Christoph Lameter834f3d12008-04-14 19:11:31 +03001205 page->objects = oo_objects(oo);
Christoph Lameter81819f02007-05-06 14:49:36 -07001206 mod_zone_page_state(page_zone(page),
1207 (s->flags & SLAB_RECLAIM_ACCOUNT) ?
1208 NR_SLAB_RECLAIMABLE : NR_SLAB_UNRECLAIMABLE,
Christoph Lameter65c33762008-04-14 19:11:40 +03001209 1 << oo_order(oo));
Christoph Lameter81819f02007-05-06 14:49:36 -07001210
1211 return page;
1212}
1213
1214static void setup_object(struct kmem_cache *s, struct page *page,
1215 void *object)
1216{
Christoph Lameter3ec09742007-05-16 22:11:00 -07001217 setup_object_debug(s, page, object);
Christoph Lameter4f104932007-05-06 14:50:17 -07001218 if (unlikely(s->ctor))
Alexey Dobriyan51cc5062008-07-25 19:45:34 -07001219 s->ctor(object);
Christoph Lameter81819f02007-05-06 14:49:36 -07001220}
1221
1222static struct page *new_slab(struct kmem_cache *s, gfp_t flags, int node)
1223{
1224 struct page *page;
Christoph Lameter81819f02007-05-06 14:49:36 -07001225 void *start;
Christoph Lameter81819f02007-05-06 14:49:36 -07001226 void *last;
1227 void *p;
1228
Christoph Lameter6cb06222007-10-16 01:25:41 -07001229 BUG_ON(flags & GFP_SLAB_BUG_MASK);
Christoph Lameter81819f02007-05-06 14:49:36 -07001230
Christoph Lameter6cb06222007-10-16 01:25:41 -07001231 page = allocate_slab(s,
1232 flags & (GFP_RECLAIM_MASK | GFP_CONSTRAINT_MASK), node);
Christoph Lameter81819f02007-05-06 14:49:36 -07001233 if (!page)
1234 goto out;
1235
Christoph Lameter205ab992008-04-14 19:11:40 +03001236 inc_slabs_node(s, page_to_nid(page), page->objects);
Christoph Lameter81819f02007-05-06 14:49:36 -07001237 page->slab = s;
1238 page->flags |= 1 << PG_slab;
Christoph Lameter81819f02007-05-06 14:49:36 -07001239
1240 start = page_address(page);
Christoph Lameter81819f02007-05-06 14:49:36 -07001241
1242 if (unlikely(s->flags & SLAB_POISON))
Christoph Lameter834f3d12008-04-14 19:11:31 +03001243 memset(start, POISON_INUSE, PAGE_SIZE << compound_order(page));
Christoph Lameter81819f02007-05-06 14:49:36 -07001244
1245 last = start;
Christoph Lameter224a88b2008-04-14 19:11:31 +03001246 for_each_object(p, s, start, page->objects) {
Christoph Lameter81819f02007-05-06 14:49:36 -07001247 setup_object(s, page, last);
1248 set_freepointer(s, last, p);
1249 last = p;
1250 }
1251 setup_object(s, page, last);
Christoph Lametera973e9d2008-03-01 13:40:44 -08001252 set_freepointer(s, last, NULL);
Christoph Lameter81819f02007-05-06 14:49:36 -07001253
1254 page->freelist = start;
1255 page->inuse = 0;
1256out:
Christoph Lameter81819f02007-05-06 14:49:36 -07001257 return page;
1258}
1259
1260static void __free_slab(struct kmem_cache *s, struct page *page)
1261{
Christoph Lameter834f3d12008-04-14 19:11:31 +03001262 int order = compound_order(page);
1263 int pages = 1 << order;
Christoph Lameter81819f02007-05-06 14:49:36 -07001264
Christoph Lameteraf537b02010-07-09 14:07:14 -05001265 if (kmem_cache_debug(s)) {
Christoph Lameter81819f02007-05-06 14:49:36 -07001266 void *p;
1267
1268 slab_pad_check(s, page);
Christoph Lameter224a88b2008-04-14 19:11:31 +03001269 for_each_object(p, s, page_address(page),
1270 page->objects)
Christoph Lameterf7cb1932010-09-29 07:15:01 -05001271 check_object(s, page, p, SLUB_RED_INACTIVE);
Christoph Lameter81819f02007-05-06 14:49:36 -07001272 }
1273
Vegard Nossumb1eeab62008-11-25 16:55:53 +01001274 kmemcheck_free_shadow(page, compound_order(page));
Vegard Nossum5a896d92008-04-04 00:54:48 +02001275
Christoph Lameter81819f02007-05-06 14:49:36 -07001276 mod_zone_page_state(page_zone(page),
1277 (s->flags & SLAB_RECLAIM_ACCOUNT) ?
1278 NR_SLAB_RECLAIMABLE : NR_SLAB_UNRECLAIMABLE,
Pekka Enberg06428782008-01-07 23:20:27 -08001279 -pages);
Christoph Lameter81819f02007-05-06 14:49:36 -07001280
Christoph Lameter49bd5222008-04-14 18:52:18 +03001281 __ClearPageSlab(page);
1282 reset_page_mapcount(page);
Nick Piggin1eb5ac62009-05-05 19:13:44 +10001283 if (current->reclaim_state)
1284 current->reclaim_state->reclaimed_slab += pages;
Christoph Lameter834f3d12008-04-14 19:11:31 +03001285 __free_pages(page, order);
Christoph Lameter81819f02007-05-06 14:49:36 -07001286}
1287
Lai Jiangshanda9a6382011-03-10 15:22:00 +08001288#define need_reserve_slab_rcu \
1289 (sizeof(((struct page *)NULL)->lru) < sizeof(struct rcu_head))
1290
Christoph Lameter81819f02007-05-06 14:49:36 -07001291static void rcu_free_slab(struct rcu_head *h)
1292{
1293 struct page *page;
1294
Lai Jiangshanda9a6382011-03-10 15:22:00 +08001295 if (need_reserve_slab_rcu)
1296 page = virt_to_head_page(h);
1297 else
1298 page = container_of((struct list_head *)h, struct page, lru);
1299
Christoph Lameter81819f02007-05-06 14:49:36 -07001300 __free_slab(page->slab, page);
1301}
1302
1303static void free_slab(struct kmem_cache *s, struct page *page)
1304{
1305 if (unlikely(s->flags & SLAB_DESTROY_BY_RCU)) {
Lai Jiangshanda9a6382011-03-10 15:22:00 +08001306 struct rcu_head *head;
1307
1308 if (need_reserve_slab_rcu) {
1309 int order = compound_order(page);
1310 int offset = (PAGE_SIZE << order) - s->reserved;
1311
1312 VM_BUG_ON(s->reserved != sizeof(*head));
1313 head = page_address(page) + offset;
1314 } else {
1315 /*
1316 * RCU free overloads the RCU head over the LRU
1317 */
1318 head = (void *)&page->lru;
1319 }
Christoph Lameter81819f02007-05-06 14:49:36 -07001320
1321 call_rcu(head, rcu_free_slab);
1322 } else
1323 __free_slab(s, page);
1324}
1325
1326static void discard_slab(struct kmem_cache *s, struct page *page)
1327{
Christoph Lameter205ab992008-04-14 19:11:40 +03001328 dec_slabs_node(s, page_to_nid(page), page->objects);
Christoph Lameter81819f02007-05-06 14:49:36 -07001329 free_slab(s, page);
1330}
1331
1332/*
1333 * Per slab locking using the pagelock
1334 */
1335static __always_inline void slab_lock(struct page *page)
1336{
1337 bit_spin_lock(PG_locked, &page->flags);
1338}
1339
1340static __always_inline void slab_unlock(struct page *page)
1341{
Nick Piggina76d3542008-01-07 23:20:27 -08001342 __bit_spin_unlock(PG_locked, &page->flags);
Christoph Lameter81819f02007-05-06 14:49:36 -07001343}
1344
1345static __always_inline int slab_trylock(struct page *page)
1346{
1347 int rc = 1;
1348
1349 rc = bit_spin_trylock(PG_locked, &page->flags);
1350 return rc;
1351}
1352
1353/*
1354 * Management of partially allocated slabs
1355 */
Christoph Lameter7c2e1322008-01-07 23:20:27 -08001356static void add_partial(struct kmem_cache_node *n,
1357 struct page *page, int tail)
Christoph Lameter81819f02007-05-06 14:49:36 -07001358{
Christoph Lametere95eed52007-05-06 14:49:44 -07001359 spin_lock(&n->list_lock);
1360 n->nr_partial++;
Christoph Lameter7c2e1322008-01-07 23:20:27 -08001361 if (tail)
1362 list_add_tail(&page->lru, &n->partial);
1363 else
1364 list_add(&page->lru, &n->partial);
Christoph Lameter81819f02007-05-06 14:49:36 -07001365 spin_unlock(&n->list_lock);
1366}
1367
Christoph Lameter62e346a2010-09-28 08:10:28 -05001368static inline void __remove_partial(struct kmem_cache_node *n,
1369 struct page *page)
1370{
1371 list_del(&page->lru);
1372 n->nr_partial--;
1373}
1374
Christoph Lameter0121c6192008-04-29 16:11:12 -07001375static void remove_partial(struct kmem_cache *s, struct page *page)
Christoph Lameter81819f02007-05-06 14:49:36 -07001376{
1377 struct kmem_cache_node *n = get_node(s, page_to_nid(page));
1378
1379 spin_lock(&n->list_lock);
Christoph Lameter62e346a2010-09-28 08:10:28 -05001380 __remove_partial(n, page);
Christoph Lameter81819f02007-05-06 14:49:36 -07001381 spin_unlock(&n->list_lock);
1382}
1383
1384/*
Christoph Lameter672bba32007-05-09 02:32:39 -07001385 * Lock slab and remove from the partial list.
Christoph Lameter81819f02007-05-06 14:49:36 -07001386 *
Christoph Lameter672bba32007-05-09 02:32:39 -07001387 * Must hold list_lock.
Christoph Lameter81819f02007-05-06 14:49:36 -07001388 */
Christoph Lameter0121c6192008-04-29 16:11:12 -07001389static inline int lock_and_freeze_slab(struct kmem_cache_node *n,
1390 struct page *page)
Christoph Lameter81819f02007-05-06 14:49:36 -07001391{
1392 if (slab_trylock(page)) {
Christoph Lameter62e346a2010-09-28 08:10:28 -05001393 __remove_partial(n, page);
Andy Whitcroft8a380822008-07-23 21:27:18 -07001394 __SetPageSlubFrozen(page);
Christoph Lameter81819f02007-05-06 14:49:36 -07001395 return 1;
1396 }
1397 return 0;
1398}
1399
1400/*
Christoph Lameter672bba32007-05-09 02:32:39 -07001401 * Try to allocate a partial slab from a specific node.
Christoph Lameter81819f02007-05-06 14:49:36 -07001402 */
1403static struct page *get_partial_node(struct kmem_cache_node *n)
1404{
1405 struct page *page;
1406
1407 /*
1408 * Racy check. If we mistakenly see no partial slabs then we
1409 * just allocate an empty slab. If we mistakenly try to get a
Christoph Lameter672bba32007-05-09 02:32:39 -07001410 * partial slab and there is none available then get_partials()
1411 * will return NULL.
Christoph Lameter81819f02007-05-06 14:49:36 -07001412 */
1413 if (!n || !n->nr_partial)
1414 return NULL;
1415
1416 spin_lock(&n->list_lock);
1417 list_for_each_entry(page, &n->partial, lru)
Christoph Lameter4b6f0752007-05-16 22:10:53 -07001418 if (lock_and_freeze_slab(n, page))
Christoph Lameter81819f02007-05-06 14:49:36 -07001419 goto out;
1420 page = NULL;
1421out:
1422 spin_unlock(&n->list_lock);
1423 return page;
1424}
1425
1426/*
Christoph Lameter672bba32007-05-09 02:32:39 -07001427 * Get a page from somewhere. Search in increasing NUMA distances.
Christoph Lameter81819f02007-05-06 14:49:36 -07001428 */
1429static struct page *get_any_partial(struct kmem_cache *s, gfp_t flags)
1430{
1431#ifdef CONFIG_NUMA
1432 struct zonelist *zonelist;
Mel Gormandd1a2392008-04-28 02:12:17 -07001433 struct zoneref *z;
Mel Gorman54a6eb52008-04-28 02:12:16 -07001434 struct zone *zone;
1435 enum zone_type high_zoneidx = gfp_zone(flags);
Christoph Lameter81819f02007-05-06 14:49:36 -07001436 struct page *page;
1437
1438 /*
Christoph Lameter672bba32007-05-09 02:32:39 -07001439 * The defrag ratio allows a configuration of the tradeoffs between
1440 * inter node defragmentation and node local allocations. A lower
1441 * defrag_ratio increases the tendency to do local allocations
1442 * instead of attempting to obtain partial slabs from other nodes.
Christoph Lameter81819f02007-05-06 14:49:36 -07001443 *
Christoph Lameter672bba32007-05-09 02:32:39 -07001444 * If the defrag_ratio is set to 0 then kmalloc() always
1445 * returns node local objects. If the ratio is higher then kmalloc()
1446 * may return off node objects because partial slabs are obtained
1447 * from other nodes and filled up.
Christoph Lameter81819f02007-05-06 14:49:36 -07001448 *
Christoph Lameter6446faa2008-02-15 23:45:26 -08001449 * If /sys/kernel/slab/xx/defrag_ratio is set to 100 (which makes
Christoph Lameter672bba32007-05-09 02:32:39 -07001450 * defrag_ratio = 1000) then every (well almost) allocation will
1451 * first attempt to defrag slab caches on other nodes. This means
1452 * scanning over all nodes to look for partial slabs which may be
1453 * expensive if we do it every time we are trying to find a slab
1454 * with available objects.
Christoph Lameter81819f02007-05-06 14:49:36 -07001455 */
Christoph Lameter98246012008-01-07 23:20:26 -08001456 if (!s->remote_node_defrag_ratio ||
1457 get_cycles() % 1024 > s->remote_node_defrag_ratio)
Christoph Lameter81819f02007-05-06 14:49:36 -07001458 return NULL;
1459
Miao Xiec0ff7452010-05-24 14:32:08 -07001460 get_mems_allowed();
Mel Gorman0e884602008-04-28 02:12:14 -07001461 zonelist = node_zonelist(slab_node(current->mempolicy), flags);
Mel Gorman54a6eb52008-04-28 02:12:16 -07001462 for_each_zone_zonelist(zone, z, zonelist, high_zoneidx) {
Christoph Lameter81819f02007-05-06 14:49:36 -07001463 struct kmem_cache_node *n;
1464
Mel Gorman54a6eb52008-04-28 02:12:16 -07001465 n = get_node(s, zone_to_nid(zone));
Christoph Lameter81819f02007-05-06 14:49:36 -07001466
Mel Gorman54a6eb52008-04-28 02:12:16 -07001467 if (n && cpuset_zone_allowed_hardwall(zone, flags) &&
David Rientjes3b89d7d2009-02-22 17:40:07 -08001468 n->nr_partial > s->min_partial) {
Christoph Lameter81819f02007-05-06 14:49:36 -07001469 page = get_partial_node(n);
Miao Xiec0ff7452010-05-24 14:32:08 -07001470 if (page) {
1471 put_mems_allowed();
Christoph Lameter81819f02007-05-06 14:49:36 -07001472 return page;
Miao Xiec0ff7452010-05-24 14:32:08 -07001473 }
Christoph Lameter81819f02007-05-06 14:49:36 -07001474 }
1475 }
Miao Xiec0ff7452010-05-24 14:32:08 -07001476 put_mems_allowed();
Christoph Lameter81819f02007-05-06 14:49:36 -07001477#endif
1478 return NULL;
1479}
1480
1481/*
1482 * Get a partial page, lock it and return it.
1483 */
1484static struct page *get_partial(struct kmem_cache *s, gfp_t flags, int node)
1485{
1486 struct page *page;
Christoph Lameter2154a332010-07-09 14:07:10 -05001487 int searchnode = (node == NUMA_NO_NODE) ? numa_node_id() : node;
Christoph Lameter81819f02007-05-06 14:49:36 -07001488
1489 page = get_partial_node(get_node(s, searchnode));
Christoph Lameterbc6488e2010-07-26 10:41:14 -05001490 if (page || node != -1)
Christoph Lameter81819f02007-05-06 14:49:36 -07001491 return page;
1492
1493 return get_any_partial(s, flags);
1494}
1495
1496/*
1497 * Move a page back to the lists.
1498 *
1499 * Must be called with the slab lock held.
1500 *
1501 * On exit the slab lock will have been dropped.
1502 */
Christoph Lameter7c2e1322008-01-07 23:20:27 -08001503static void unfreeze_slab(struct kmem_cache *s, struct page *page, int tail)
Namhyung Kim34789732010-09-29 21:02:14 +09001504 __releases(bitlock)
Christoph Lameter81819f02007-05-06 14:49:36 -07001505{
Christoph Lametere95eed52007-05-06 14:49:44 -07001506 struct kmem_cache_node *n = get_node(s, page_to_nid(page));
1507
Andy Whitcroft8a380822008-07-23 21:27:18 -07001508 __ClearPageSlubFrozen(page);
Christoph Lameter81819f02007-05-06 14:49:36 -07001509 if (page->inuse) {
Christoph Lametere95eed52007-05-06 14:49:44 -07001510
Christoph Lametera973e9d2008-03-01 13:40:44 -08001511 if (page->freelist) {
Christoph Lameter7c2e1322008-01-07 23:20:27 -08001512 add_partial(n, page, tail);
Christoph Lameter84e554e62009-12-18 16:26:23 -06001513 stat(s, tail ? DEACTIVATE_TO_TAIL : DEACTIVATE_TO_HEAD);
Christoph Lameter8ff12cf2008-02-07 17:47:41 -08001514 } else {
Christoph Lameter84e554e62009-12-18 16:26:23 -06001515 stat(s, DEACTIVATE_FULL);
Christoph Lameteraf537b02010-07-09 14:07:14 -05001516 if (kmem_cache_debug(s) && (s->flags & SLAB_STORE_USER))
Christoph Lameter8ff12cf2008-02-07 17:47:41 -08001517 add_full(n, page);
1518 }
Christoph Lameter81819f02007-05-06 14:49:36 -07001519 slab_unlock(page);
1520 } else {
Christoph Lameter84e554e62009-12-18 16:26:23 -06001521 stat(s, DEACTIVATE_EMPTY);
David Rientjes3b89d7d2009-02-22 17:40:07 -08001522 if (n->nr_partial < s->min_partial) {
Christoph Lametere95eed52007-05-06 14:49:44 -07001523 /*
Christoph Lameter672bba32007-05-09 02:32:39 -07001524 * Adding an empty slab to the partial slabs in order
1525 * to avoid page allocator overhead. This slab needs
1526 * to come after the other slabs with objects in
Christoph Lameter6446faa2008-02-15 23:45:26 -08001527 * so that the others get filled first. That way the
1528 * size of the partial list stays small.
1529 *
Christoph Lameter0121c6192008-04-29 16:11:12 -07001530 * kmem_cache_shrink can reclaim any empty slabs from
1531 * the partial list.
Christoph Lametere95eed52007-05-06 14:49:44 -07001532 */
Christoph Lameter7c2e1322008-01-07 23:20:27 -08001533 add_partial(n, page, 1);
Christoph Lametere95eed52007-05-06 14:49:44 -07001534 slab_unlock(page);
1535 } else {
1536 slab_unlock(page);
Christoph Lameter84e554e62009-12-18 16:26:23 -06001537 stat(s, FREE_SLAB);
Christoph Lametere95eed52007-05-06 14:49:44 -07001538 discard_slab(s, page);
1539 }
Christoph Lameter81819f02007-05-06 14:49:36 -07001540 }
1541}
1542
Christoph Lameter8a5ec0b2011-02-25 11:38:54 -06001543#ifdef CONFIG_CMPXCHG_LOCAL
1544#ifdef CONFIG_PREEMPT
1545/*
1546 * Calculate the next globally unique transaction for disambiguiation
1547 * during cmpxchg. The transactions start with the cpu number and are then
1548 * incremented by CONFIG_NR_CPUS.
1549 */
1550#define TID_STEP roundup_pow_of_two(CONFIG_NR_CPUS)
1551#else
1552/*
1553 * No preemption supported therefore also no need to check for
1554 * different cpus.
1555 */
1556#define TID_STEP 1
1557#endif
1558
1559static inline unsigned long next_tid(unsigned long tid)
1560{
1561 return tid + TID_STEP;
1562}
1563
1564static inline unsigned int tid_to_cpu(unsigned long tid)
1565{
1566 return tid % TID_STEP;
1567}
1568
1569static inline unsigned long tid_to_event(unsigned long tid)
1570{
1571 return tid / TID_STEP;
1572}
1573
1574static inline unsigned int init_tid(int cpu)
1575{
1576 return cpu;
1577}
1578
1579static inline void note_cmpxchg_failure(const char *n,
1580 const struct kmem_cache *s, unsigned long tid)
1581{
1582#ifdef SLUB_DEBUG_CMPXCHG
1583 unsigned long actual_tid = __this_cpu_read(s->cpu_slab->tid);
1584
1585 printk(KERN_INFO "%s %s: cmpxchg redo ", n, s->name);
1586
1587#ifdef CONFIG_PREEMPT
1588 if (tid_to_cpu(tid) != tid_to_cpu(actual_tid))
1589 printk("due to cpu change %d -> %d\n",
1590 tid_to_cpu(tid), tid_to_cpu(actual_tid));
1591 else
1592#endif
1593 if (tid_to_event(tid) != tid_to_event(actual_tid))
1594 printk("due to cpu running other code. Event %ld->%ld\n",
1595 tid_to_event(tid), tid_to_event(actual_tid));
1596 else
1597 printk("for unknown reason: actual=%lx was=%lx target=%lx\n",
1598 actual_tid, tid, next_tid(tid));
1599#endif
Christoph Lameter4fdccdf2011-03-22 13:35:00 -05001600 stat(s, CMPXCHG_DOUBLE_CPU_FAIL);
Christoph Lameter8a5ec0b2011-02-25 11:38:54 -06001601}
1602
1603#endif
1604
1605void init_kmem_cache_cpus(struct kmem_cache *s)
1606{
Christoph Lameterb8c4c962011-03-24 14:51:38 -05001607#ifdef CONFIG_CMPXCHG_LOCAL
Christoph Lameter8a5ec0b2011-02-25 11:38:54 -06001608 int cpu;
1609
1610 for_each_possible_cpu(cpu)
1611 per_cpu_ptr(s->cpu_slab, cpu)->tid = init_tid(cpu);
1612#endif
1613
1614}
Christoph Lameter81819f02007-05-06 14:49:36 -07001615/*
1616 * Remove the cpu slab
1617 */
Christoph Lameterdfb4f092007-10-16 01:26:05 -07001618static void deactivate_slab(struct kmem_cache *s, struct kmem_cache_cpu *c)
Namhyung Kim34789732010-09-29 21:02:14 +09001619 __releases(bitlock)
Christoph Lameter81819f02007-05-06 14:49:36 -07001620{
Christoph Lameterdfb4f092007-10-16 01:26:05 -07001621 struct page *page = c->page;
Christoph Lameter7c2e1322008-01-07 23:20:27 -08001622 int tail = 1;
Christoph Lameter8ff12cf2008-02-07 17:47:41 -08001623
Christoph Lameterb773ad72008-03-04 11:10:17 -08001624 if (page->freelist)
Christoph Lameter84e554e62009-12-18 16:26:23 -06001625 stat(s, DEACTIVATE_REMOTE_FREES);
Christoph Lameter894b8782007-05-10 03:15:16 -07001626 /*
Christoph Lameter6446faa2008-02-15 23:45:26 -08001627 * Merge cpu freelist into slab freelist. Typically we get here
Christoph Lameter894b8782007-05-10 03:15:16 -07001628 * because both freelists are empty. So this is unlikely
1629 * to occur.
1630 */
Christoph Lametera973e9d2008-03-01 13:40:44 -08001631 while (unlikely(c->freelist)) {
Christoph Lameter894b8782007-05-10 03:15:16 -07001632 void **object;
1633
Christoph Lameter7c2e1322008-01-07 23:20:27 -08001634 tail = 0; /* Hot objects. Put the slab first */
1635
Christoph Lameter894b8782007-05-10 03:15:16 -07001636 /* Retrieve object from cpu_freelist */
Christoph Lameterdfb4f092007-10-16 01:26:05 -07001637 object = c->freelist;
Christoph Lameterff120592009-12-18 16:26:22 -06001638 c->freelist = get_freepointer(s, c->freelist);
Christoph Lameter894b8782007-05-10 03:15:16 -07001639
1640 /* And put onto the regular freelist */
Christoph Lameterff120592009-12-18 16:26:22 -06001641 set_freepointer(s, object, page->freelist);
Christoph Lameter894b8782007-05-10 03:15:16 -07001642 page->freelist = object;
1643 page->inuse--;
1644 }
Christoph Lameterdfb4f092007-10-16 01:26:05 -07001645 c->page = NULL;
Christoph Lameter8a5ec0b2011-02-25 11:38:54 -06001646#ifdef CONFIG_CMPXCHG_LOCAL
1647 c->tid = next_tid(c->tid);
1648#endif
Christoph Lameter7c2e1322008-01-07 23:20:27 -08001649 unfreeze_slab(s, page, tail);
Christoph Lameter81819f02007-05-06 14:49:36 -07001650}
1651
Christoph Lameterdfb4f092007-10-16 01:26:05 -07001652static inline void flush_slab(struct kmem_cache *s, struct kmem_cache_cpu *c)
Christoph Lameter81819f02007-05-06 14:49:36 -07001653{
Christoph Lameter84e554e62009-12-18 16:26:23 -06001654 stat(s, CPUSLAB_FLUSH);
Christoph Lameterdfb4f092007-10-16 01:26:05 -07001655 slab_lock(c->page);
1656 deactivate_slab(s, c);
Christoph Lameter81819f02007-05-06 14:49:36 -07001657}
1658
1659/*
1660 * Flush cpu slab.
Christoph Lameter6446faa2008-02-15 23:45:26 -08001661 *
Christoph Lameter81819f02007-05-06 14:49:36 -07001662 * Called from IPI handler with interrupts disabled.
1663 */
Christoph Lameter0c710012007-07-17 04:03:24 -07001664static inline void __flush_cpu_slab(struct kmem_cache *s, int cpu)
Christoph Lameter81819f02007-05-06 14:49:36 -07001665{
Christoph Lameter9dfc6e62009-12-18 16:26:20 -06001666 struct kmem_cache_cpu *c = per_cpu_ptr(s->cpu_slab, cpu);
Christoph Lameter81819f02007-05-06 14:49:36 -07001667
Christoph Lameterdfb4f092007-10-16 01:26:05 -07001668 if (likely(c && c->page))
1669 flush_slab(s, c);
Christoph Lameter81819f02007-05-06 14:49:36 -07001670}
1671
1672static void flush_cpu_slab(void *d)
1673{
1674 struct kmem_cache *s = d;
Christoph Lameter81819f02007-05-06 14:49:36 -07001675
Christoph Lameterdfb4f092007-10-16 01:26:05 -07001676 __flush_cpu_slab(s, smp_processor_id());
Christoph Lameter81819f02007-05-06 14:49:36 -07001677}
1678
1679static void flush_all(struct kmem_cache *s)
1680{
Jens Axboe15c8b6c2008-05-09 09:39:44 +02001681 on_each_cpu(flush_cpu_slab, s, 1);
Christoph Lameter81819f02007-05-06 14:49:36 -07001682}
1683
1684/*
Christoph Lameterdfb4f092007-10-16 01:26:05 -07001685 * Check if the objects in a per cpu structure fit numa
1686 * locality expectations.
1687 */
1688static inline int node_match(struct kmem_cache_cpu *c, int node)
1689{
1690#ifdef CONFIG_NUMA
Christoph Lameter2154a332010-07-09 14:07:10 -05001691 if (node != NUMA_NO_NODE && c->node != node)
Christoph Lameterdfb4f092007-10-16 01:26:05 -07001692 return 0;
1693#endif
1694 return 1;
1695}
1696
Pekka Enberg781b2ba2009-06-10 18:50:32 +03001697static int count_free(struct page *page)
1698{
1699 return page->objects - page->inuse;
1700}
1701
1702static unsigned long count_partial(struct kmem_cache_node *n,
1703 int (*get_count)(struct page *))
1704{
1705 unsigned long flags;
1706 unsigned long x = 0;
1707 struct page *page;
1708
1709 spin_lock_irqsave(&n->list_lock, flags);
1710 list_for_each_entry(page, &n->partial, lru)
1711 x += get_count(page);
1712 spin_unlock_irqrestore(&n->list_lock, flags);
1713 return x;
1714}
1715
Alexander Beregalov26c02cf2009-06-11 14:08:48 +04001716static inline unsigned long node_nr_objs(struct kmem_cache_node *n)
1717{
1718#ifdef CONFIG_SLUB_DEBUG
1719 return atomic_long_read(&n->total_objects);
1720#else
1721 return 0;
1722#endif
1723}
1724
Pekka Enberg781b2ba2009-06-10 18:50:32 +03001725static noinline void
1726slab_out_of_memory(struct kmem_cache *s, gfp_t gfpflags, int nid)
1727{
1728 int node;
1729
1730 printk(KERN_WARNING
1731 "SLUB: Unable to allocate memory on node %d (gfp=0x%x)\n",
1732 nid, gfpflags);
1733 printk(KERN_WARNING " cache: %s, object size: %d, buffer size: %d, "
1734 "default order: %d, min order: %d\n", s->name, s->objsize,
1735 s->size, oo_order(s->oo), oo_order(s->min));
1736
David Rientjesfa5ec8a2009-07-07 00:14:14 -07001737 if (oo_order(s->min) > get_order(s->objsize))
1738 printk(KERN_WARNING " %s debugging increased min order, use "
1739 "slub_debug=O to disable.\n", s->name);
1740
Pekka Enberg781b2ba2009-06-10 18:50:32 +03001741 for_each_online_node(node) {
1742 struct kmem_cache_node *n = get_node(s, node);
1743 unsigned long nr_slabs;
1744 unsigned long nr_objs;
1745 unsigned long nr_free;
1746
1747 if (!n)
1748 continue;
1749
Alexander Beregalov26c02cf2009-06-11 14:08:48 +04001750 nr_free = count_partial(n, count_free);
1751 nr_slabs = node_nr_slabs(n);
1752 nr_objs = node_nr_objs(n);
Pekka Enberg781b2ba2009-06-10 18:50:32 +03001753
1754 printk(KERN_WARNING
1755 " node %d: slabs: %ld, objs: %ld, free: %ld\n",
1756 node, nr_slabs, nr_objs, nr_free);
1757 }
1758}
1759
Christoph Lameterdfb4f092007-10-16 01:26:05 -07001760/*
Christoph Lameter894b8782007-05-10 03:15:16 -07001761 * Slow path. The lockless freelist is empty or we need to perform
1762 * debugging duties.
Christoph Lameter81819f02007-05-06 14:49:36 -07001763 *
Christoph Lameter894b8782007-05-10 03:15:16 -07001764 * Interrupts are disabled.
Christoph Lameter81819f02007-05-06 14:49:36 -07001765 *
Christoph Lameter894b8782007-05-10 03:15:16 -07001766 * Processing is still very fast if new objects have been freed to the
1767 * regular freelist. In that case we simply take over the regular freelist
1768 * as the lockless freelist and zap the regular freelist.
Christoph Lameter81819f02007-05-06 14:49:36 -07001769 *
Christoph Lameter894b8782007-05-10 03:15:16 -07001770 * If that is not working then we fall back to the partial lists. We take the
1771 * first element of the freelist as the object to allocate now and move the
1772 * rest of the freelist to the lockless freelist.
1773 *
1774 * And if we were unable to get a new slab from the partial slab lists then
Christoph Lameter6446faa2008-02-15 23:45:26 -08001775 * we need to allocate a new slab. This is the slowest path since it involves
1776 * a call to the page allocator and the setup of a new slab.
Christoph Lameter81819f02007-05-06 14:49:36 -07001777 */
Eduard - Gabriel Munteanuce71e272008-08-19 20:43:25 +03001778static void *__slab_alloc(struct kmem_cache *s, gfp_t gfpflags, int node,
1779 unsigned long addr, struct kmem_cache_cpu *c)
Christoph Lameter81819f02007-05-06 14:49:36 -07001780{
Christoph Lameter81819f02007-05-06 14:49:36 -07001781 void **object;
Christoph Lameterdfb4f092007-10-16 01:26:05 -07001782 struct page *new;
Christoph Lameter8a5ec0b2011-02-25 11:38:54 -06001783#ifdef CONFIG_CMPXCHG_LOCAL
1784 unsigned long flags;
1785
1786 local_irq_save(flags);
1787#ifdef CONFIG_PREEMPT
1788 /*
1789 * We may have been preempted and rescheduled on a different
1790 * cpu before disabling interrupts. Need to reload cpu area
1791 * pointer.
1792 */
1793 c = this_cpu_ptr(s->cpu_slab);
1794#endif
1795#endif
Christoph Lameter81819f02007-05-06 14:49:36 -07001796
Linus Torvaldse72e9c22008-03-27 20:56:33 -07001797 /* We handle __GFP_ZERO in the caller */
1798 gfpflags &= ~__GFP_ZERO;
1799
Christoph Lameterdfb4f092007-10-16 01:26:05 -07001800 if (!c->page)
Christoph Lameter81819f02007-05-06 14:49:36 -07001801 goto new_slab;
1802
Christoph Lameterdfb4f092007-10-16 01:26:05 -07001803 slab_lock(c->page);
1804 if (unlikely(!node_match(c, node)))
Christoph Lameter81819f02007-05-06 14:49:36 -07001805 goto another_slab;
Christoph Lameter6446faa2008-02-15 23:45:26 -08001806
Christoph Lameter84e554e62009-12-18 16:26:23 -06001807 stat(s, ALLOC_REFILL);
Christoph Lameter6446faa2008-02-15 23:45:26 -08001808
Christoph Lameter894b8782007-05-10 03:15:16 -07001809load_freelist:
Christoph Lameterdfb4f092007-10-16 01:26:05 -07001810 object = c->page->freelist;
Christoph Lametera973e9d2008-03-01 13:40:44 -08001811 if (unlikely(!object))
Christoph Lameter81819f02007-05-06 14:49:36 -07001812 goto another_slab;
Christoph Lameteraf537b02010-07-09 14:07:14 -05001813 if (kmem_cache_debug(s))
Christoph Lameter81819f02007-05-06 14:49:36 -07001814 goto debug;
1815
Christoph Lameterff120592009-12-18 16:26:22 -06001816 c->freelist = get_freepointer(s, object);
Christoph Lameter39b26462008-04-14 19:11:30 +03001817 c->page->inuse = c->page->objects;
Christoph Lametera973e9d2008-03-01 13:40:44 -08001818 c->page->freelist = NULL;
Christoph Lameterdfb4f092007-10-16 01:26:05 -07001819 c->node = page_to_nid(c->page);
Christoph Lameter1f842602008-01-07 23:20:30 -08001820unlock_out:
Christoph Lameterdfb4f092007-10-16 01:26:05 -07001821 slab_unlock(c->page);
Christoph Lameter8a5ec0b2011-02-25 11:38:54 -06001822#ifdef CONFIG_CMPXCHG_LOCAL
1823 c->tid = next_tid(c->tid);
1824 local_irq_restore(flags);
1825#endif
Christoph Lameter84e554e62009-12-18 16:26:23 -06001826 stat(s, ALLOC_SLOWPATH);
Christoph Lameter81819f02007-05-06 14:49:36 -07001827 return object;
1828
1829another_slab:
Christoph Lameterdfb4f092007-10-16 01:26:05 -07001830 deactivate_slab(s, c);
Christoph Lameter81819f02007-05-06 14:49:36 -07001831
1832new_slab:
Christoph Lameterdfb4f092007-10-16 01:26:05 -07001833 new = get_partial(s, gfpflags, node);
1834 if (new) {
1835 c->page = new;
Christoph Lameter84e554e62009-12-18 16:26:23 -06001836 stat(s, ALLOC_FROM_PARTIAL);
Christoph Lameter894b8782007-05-10 03:15:16 -07001837 goto load_freelist;
Christoph Lameter81819f02007-05-06 14:49:36 -07001838 }
1839
Christoph Lameterc1d50832010-08-20 12:37:17 -05001840 gfpflags &= gfp_allowed_mask;
Christoph Lameterb811c202007-10-16 23:25:51 -07001841 if (gfpflags & __GFP_WAIT)
1842 local_irq_enable();
1843
Christoph Lameterdfb4f092007-10-16 01:26:05 -07001844 new = new_slab(s, gfpflags, node);
Christoph Lameterb811c202007-10-16 23:25:51 -07001845
1846 if (gfpflags & __GFP_WAIT)
1847 local_irq_disable();
1848
Christoph Lameterdfb4f092007-10-16 01:26:05 -07001849 if (new) {
Christoph Lameter9dfc6e62009-12-18 16:26:20 -06001850 c = __this_cpu_ptr(s->cpu_slab);
Christoph Lameter84e554e62009-12-18 16:26:23 -06001851 stat(s, ALLOC_SLAB);
Christoph Lameter05aa3452007-11-05 11:31:58 -08001852 if (c->page)
Christoph Lameterdfb4f092007-10-16 01:26:05 -07001853 flush_slab(s, c);
Christoph Lameterdfb4f092007-10-16 01:26:05 -07001854 slab_lock(new);
Andy Whitcroft8a380822008-07-23 21:27:18 -07001855 __SetPageSlubFrozen(new);
Christoph Lameterdfb4f092007-10-16 01:26:05 -07001856 c->page = new;
Christoph Lameter4b6f0752007-05-16 22:10:53 -07001857 goto load_freelist;
Christoph Lameter81819f02007-05-06 14:49:36 -07001858 }
Pekka Enberg95f85982009-06-11 16:18:09 +03001859 if (!(gfpflags & __GFP_NOWARN) && printk_ratelimit())
1860 slab_out_of_memory(s, gfpflags, node);
Christoph Lameter2fd66c52011-03-22 13:32:53 -05001861#ifdef CONFIG_CMPXCHG_LOCAL
1862 local_irq_restore(flags);
1863#endif
Christoph Lameter71c7a062008-02-14 14:28:01 -08001864 return NULL;
Christoph Lameter81819f02007-05-06 14:49:36 -07001865debug:
Christoph Lameterdfb4f092007-10-16 01:26:05 -07001866 if (!alloc_debug_processing(s, c->page, object, addr))
Christoph Lameter81819f02007-05-06 14:49:36 -07001867 goto another_slab;
Christoph Lameter894b8782007-05-10 03:15:16 -07001868
Christoph Lameterdfb4f092007-10-16 01:26:05 -07001869 c->page->inuse++;
Christoph Lameterff120592009-12-18 16:26:22 -06001870 c->page->freelist = get_freepointer(s, object);
Pekka Enberg15b7c512010-10-02 11:32:32 +03001871 c->node = NUMA_NO_NODE;
Christoph Lameter1f842602008-01-07 23:20:30 -08001872 goto unlock_out;
Christoph Lameter894b8782007-05-10 03:15:16 -07001873}
1874
1875/*
1876 * Inlined fastpath so that allocation functions (kmalloc, kmem_cache_alloc)
1877 * have the fastpath folded into their functions. So no function call
1878 * overhead for requests that can be satisfied on the fastpath.
1879 *
1880 * The fastpath works by first checking if the lockless freelist can be used.
1881 * If not then __slab_alloc is called for slow processing.
1882 *
1883 * Otherwise we can simply pick the next object from the lockless free list.
1884 */
Pekka Enberg06428782008-01-07 23:20:27 -08001885static __always_inline void *slab_alloc(struct kmem_cache *s,
Eduard - Gabriel Munteanuce71e272008-08-19 20:43:25 +03001886 gfp_t gfpflags, int node, unsigned long addr)
Christoph Lameter894b8782007-05-10 03:15:16 -07001887{
Christoph Lameter894b8782007-05-10 03:15:16 -07001888 void **object;
Christoph Lameterdfb4f092007-10-16 01:26:05 -07001889 struct kmem_cache_cpu *c;
Christoph Lameter8a5ec0b2011-02-25 11:38:54 -06001890#ifdef CONFIG_CMPXCHG_LOCAL
1891 unsigned long tid;
1892#else
Christoph Lameter1f842602008-01-07 23:20:30 -08001893 unsigned long flags;
Christoph Lameter8a5ec0b2011-02-25 11:38:54 -06001894#endif
Christoph Lameter1f842602008-01-07 23:20:30 -08001895
Christoph Lameterc016b0b2010-08-20 12:37:16 -05001896 if (slab_pre_alloc_hook(s, gfpflags))
Akinobu Mita773ff602008-12-23 19:37:01 +09001897 return NULL;
1898
Christoph Lameter8a5ec0b2011-02-25 11:38:54 -06001899#ifndef CONFIG_CMPXCHG_LOCAL
Christoph Lameter894b8782007-05-10 03:15:16 -07001900 local_irq_save(flags);
Christoph Lameter8a5ec0b2011-02-25 11:38:54 -06001901#else
1902redo:
1903#endif
1904
1905 /*
1906 * Must read kmem_cache cpu data via this cpu ptr. Preemption is
1907 * enabled. We may switch back and forth between cpus while
1908 * reading from one cpu area. That does not matter as long
1909 * as we end up on the original cpu again when doing the cmpxchg.
1910 */
Christoph Lameter9dfc6e62009-12-18 16:26:20 -06001911 c = __this_cpu_ptr(s->cpu_slab);
Christoph Lameter8a5ec0b2011-02-25 11:38:54 -06001912
1913#ifdef CONFIG_CMPXCHG_LOCAL
1914 /*
1915 * The transaction ids are globally unique per cpu and per operation on
1916 * a per cpu queue. Thus they can be guarantee that the cmpxchg_double
1917 * occurs on the right processor and that there was no operation on the
1918 * linked list in between.
1919 */
1920 tid = c->tid;
1921 barrier();
1922#endif
1923
Christoph Lameter9dfc6e62009-12-18 16:26:20 -06001924 object = c->freelist;
Christoph Lameter9dfc6e62009-12-18 16:26:20 -06001925 if (unlikely(!object || !node_match(c, node)))
Christoph Lameter894b8782007-05-10 03:15:16 -07001926
Christoph Lameterdfb4f092007-10-16 01:26:05 -07001927 object = __slab_alloc(s, gfpflags, node, addr, c);
Christoph Lameter894b8782007-05-10 03:15:16 -07001928
1929 else {
Christoph Lameter8a5ec0b2011-02-25 11:38:54 -06001930#ifdef CONFIG_CMPXCHG_LOCAL
1931 /*
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001932 * The cmpxchg will only match if there was no additional
Christoph Lameter8a5ec0b2011-02-25 11:38:54 -06001933 * operation and if we are on the right processor.
1934 *
1935 * The cmpxchg does the following atomically (without lock semantics!)
1936 * 1. Relocate first pointer to the current per cpu area.
1937 * 2. Verify that tid and freelist have not been changed
1938 * 3. If they were not changed replace tid and freelist
1939 *
1940 * Since this is without lock semantics the protection is only against
1941 * code executing on this cpu *not* from access by other cpus.
1942 */
1943 if (unlikely(!this_cpu_cmpxchg_double(
1944 s->cpu_slab->freelist, s->cpu_slab->tid,
1945 object, tid,
1946 get_freepointer(s, object), next_tid(tid)))) {
1947
1948 note_cmpxchg_failure("slab_alloc", s, tid);
1949 goto redo;
1950 }
1951#else
Christoph Lameterff120592009-12-18 16:26:22 -06001952 c->freelist = get_freepointer(s, object);
Christoph Lameter8a5ec0b2011-02-25 11:38:54 -06001953#endif
Christoph Lameter84e554e62009-12-18 16:26:23 -06001954 stat(s, ALLOC_FASTPATH);
Christoph Lameter894b8782007-05-10 03:15:16 -07001955 }
Christoph Lameter8a5ec0b2011-02-25 11:38:54 -06001956
1957#ifndef CONFIG_CMPXCHG_LOCAL
Christoph Lameter894b8782007-05-10 03:15:16 -07001958 local_irq_restore(flags);
Christoph Lameter8a5ec0b2011-02-25 11:38:54 -06001959#endif
Christoph Lameterd07dbea2007-07-17 04:03:23 -07001960
Pekka Enberg74e21342009-11-25 20:14:48 +02001961 if (unlikely(gfpflags & __GFP_ZERO) && object)
Christoph Lameterff120592009-12-18 16:26:22 -06001962 memset(object, 0, s->objsize);
Christoph Lameterd07dbea2007-07-17 04:03:23 -07001963
Christoph Lameterc016b0b2010-08-20 12:37:16 -05001964 slab_post_alloc_hook(s, gfpflags, object);
Vegard Nossum5a896d92008-04-04 00:54:48 +02001965
Christoph Lameter894b8782007-05-10 03:15:16 -07001966 return object;
Christoph Lameter81819f02007-05-06 14:49:36 -07001967}
1968
1969void *kmem_cache_alloc(struct kmem_cache *s, gfp_t gfpflags)
1970{
Christoph Lameter2154a332010-07-09 14:07:10 -05001971 void *ret = slab_alloc(s, gfpflags, NUMA_NO_NODE, _RET_IP_);
Eduard - Gabriel Munteanu5b882be2008-08-19 20:43:26 +03001972
Eduard - Gabriel Munteanuca2b84cb2009-03-23 15:12:24 +02001973 trace_kmem_cache_alloc(_RET_IP_, ret, s->objsize, s->size, gfpflags);
Eduard - Gabriel Munteanu5b882be2008-08-19 20:43:26 +03001974
1975 return ret;
Christoph Lameter81819f02007-05-06 14:49:36 -07001976}
1977EXPORT_SYMBOL(kmem_cache_alloc);
1978
Li Zefan0f24f122009-12-11 15:45:30 +08001979#ifdef CONFIG_TRACING
Richard Kennedy4a923792010-10-21 10:29:19 +01001980void *kmem_cache_alloc_trace(struct kmem_cache *s, gfp_t gfpflags, size_t size)
Eduard - Gabriel Munteanu5b882be2008-08-19 20:43:26 +03001981{
Richard Kennedy4a923792010-10-21 10:29:19 +01001982 void *ret = slab_alloc(s, gfpflags, NUMA_NO_NODE, _RET_IP_);
1983 trace_kmalloc(_RET_IP_, ret, size, s->size, gfpflags);
1984 return ret;
Eduard - Gabriel Munteanu5b882be2008-08-19 20:43:26 +03001985}
Richard Kennedy4a923792010-10-21 10:29:19 +01001986EXPORT_SYMBOL(kmem_cache_alloc_trace);
1987
1988void *kmalloc_order_trace(size_t size, gfp_t flags, unsigned int order)
1989{
1990 void *ret = kmalloc_order(size, flags, order);
1991 trace_kmalloc(_RET_IP_, ret, size, PAGE_SIZE << order, flags);
1992 return ret;
1993}
1994EXPORT_SYMBOL(kmalloc_order_trace);
Eduard - Gabriel Munteanu5b882be2008-08-19 20:43:26 +03001995#endif
1996
Christoph Lameter81819f02007-05-06 14:49:36 -07001997#ifdef CONFIG_NUMA
1998void *kmem_cache_alloc_node(struct kmem_cache *s, gfp_t gfpflags, int node)
1999{
Eduard - Gabriel Munteanu5b882be2008-08-19 20:43:26 +03002000 void *ret = slab_alloc(s, gfpflags, node, _RET_IP_);
2001
Eduard - Gabriel Munteanuca2b84cb2009-03-23 15:12:24 +02002002 trace_kmem_cache_alloc_node(_RET_IP_, ret,
2003 s->objsize, s->size, gfpflags, node);
Eduard - Gabriel Munteanu5b882be2008-08-19 20:43:26 +03002004
2005 return ret;
Christoph Lameter81819f02007-05-06 14:49:36 -07002006}
2007EXPORT_SYMBOL(kmem_cache_alloc_node);
Christoph Lameter81819f02007-05-06 14:49:36 -07002008
Li Zefan0f24f122009-12-11 15:45:30 +08002009#ifdef CONFIG_TRACING
Richard Kennedy4a923792010-10-21 10:29:19 +01002010void *kmem_cache_alloc_node_trace(struct kmem_cache *s,
Eduard - Gabriel Munteanu5b882be2008-08-19 20:43:26 +03002011 gfp_t gfpflags,
Richard Kennedy4a923792010-10-21 10:29:19 +01002012 int node, size_t size)
Eduard - Gabriel Munteanu5b882be2008-08-19 20:43:26 +03002013{
Richard Kennedy4a923792010-10-21 10:29:19 +01002014 void *ret = slab_alloc(s, gfpflags, node, _RET_IP_);
2015
2016 trace_kmalloc_node(_RET_IP_, ret,
2017 size, s->size, gfpflags, node);
2018 return ret;
Eduard - Gabriel Munteanu5b882be2008-08-19 20:43:26 +03002019}
Richard Kennedy4a923792010-10-21 10:29:19 +01002020EXPORT_SYMBOL(kmem_cache_alloc_node_trace);
Eduard - Gabriel Munteanu5b882be2008-08-19 20:43:26 +03002021#endif
Namhyung Kim5d1f57e2010-09-29 21:02:15 +09002022#endif
Eduard - Gabriel Munteanu5b882be2008-08-19 20:43:26 +03002023
Christoph Lameter81819f02007-05-06 14:49:36 -07002024/*
Christoph Lameter894b8782007-05-10 03:15:16 -07002025 * Slow patch handling. This may still be called frequently since objects
2026 * have a longer lifetime than the cpu slabs in most processing loads.
Christoph Lameter81819f02007-05-06 14:49:36 -07002027 *
Christoph Lameter894b8782007-05-10 03:15:16 -07002028 * So we still attempt to reduce cache line usage. Just take the slab
2029 * lock and free the item. If there is no additional partial page
2030 * handling required then we can return immediately.
Christoph Lameter81819f02007-05-06 14:49:36 -07002031 */
Christoph Lameter894b8782007-05-10 03:15:16 -07002032static void __slab_free(struct kmem_cache *s, struct page *page,
Christoph Lameterff120592009-12-18 16:26:22 -06002033 void *x, unsigned long addr)
Christoph Lameter81819f02007-05-06 14:49:36 -07002034{
2035 void *prior;
2036 void **object = (void *)x;
Christoph Lameter8a5ec0b2011-02-25 11:38:54 -06002037#ifdef CONFIG_CMPXCHG_LOCAL
2038 unsigned long flags;
Christoph Lameter81819f02007-05-06 14:49:36 -07002039
Christoph Lameter8a5ec0b2011-02-25 11:38:54 -06002040 local_irq_save(flags);
2041#endif
Christoph Lameter81819f02007-05-06 14:49:36 -07002042 slab_lock(page);
Christoph Lameter8a5ec0b2011-02-25 11:38:54 -06002043 stat(s, FREE_SLOWPATH);
Christoph Lameter81819f02007-05-06 14:49:36 -07002044
Christoph Lameteraf537b02010-07-09 14:07:14 -05002045 if (kmem_cache_debug(s))
Christoph Lameter81819f02007-05-06 14:49:36 -07002046 goto debug;
Christoph Lameter6446faa2008-02-15 23:45:26 -08002047
Christoph Lameter81819f02007-05-06 14:49:36 -07002048checks_ok:
Christoph Lameterff120592009-12-18 16:26:22 -06002049 prior = page->freelist;
2050 set_freepointer(s, object, prior);
Christoph Lameter81819f02007-05-06 14:49:36 -07002051 page->freelist = object;
2052 page->inuse--;
2053
Andy Whitcroft8a380822008-07-23 21:27:18 -07002054 if (unlikely(PageSlubFrozen(page))) {
Christoph Lameter84e554e62009-12-18 16:26:23 -06002055 stat(s, FREE_FROZEN);
Christoph Lameter81819f02007-05-06 14:49:36 -07002056 goto out_unlock;
Christoph Lameter8ff12cf2008-02-07 17:47:41 -08002057 }
Christoph Lameter81819f02007-05-06 14:49:36 -07002058
2059 if (unlikely(!page->inuse))
2060 goto slab_empty;
2061
2062 /*
Christoph Lameter6446faa2008-02-15 23:45:26 -08002063 * Objects left in the slab. If it was not on the partial list before
Christoph Lameter81819f02007-05-06 14:49:36 -07002064 * then add it.
2065 */
Christoph Lametera973e9d2008-03-01 13:40:44 -08002066 if (unlikely(!prior)) {
Christoph Lameter7c2e1322008-01-07 23:20:27 -08002067 add_partial(get_node(s, page_to_nid(page)), page, 1);
Christoph Lameter84e554e62009-12-18 16:26:23 -06002068 stat(s, FREE_ADD_PARTIAL);
Christoph Lameter8ff12cf2008-02-07 17:47:41 -08002069 }
Christoph Lameter81819f02007-05-06 14:49:36 -07002070
2071out_unlock:
2072 slab_unlock(page);
Christoph Lameter8a5ec0b2011-02-25 11:38:54 -06002073#ifdef CONFIG_CMPXCHG_LOCAL
2074 local_irq_restore(flags);
2075#endif
Christoph Lameter81819f02007-05-06 14:49:36 -07002076 return;
2077
2078slab_empty:
Christoph Lametera973e9d2008-03-01 13:40:44 -08002079 if (prior) {
Christoph Lameter81819f02007-05-06 14:49:36 -07002080 /*
Christoph Lameter672bba32007-05-09 02:32:39 -07002081 * Slab still on the partial list.
Christoph Lameter81819f02007-05-06 14:49:36 -07002082 */
2083 remove_partial(s, page);
Christoph Lameter84e554e62009-12-18 16:26:23 -06002084 stat(s, FREE_REMOVE_PARTIAL);
Christoph Lameter8ff12cf2008-02-07 17:47:41 -08002085 }
Christoph Lameter81819f02007-05-06 14:49:36 -07002086 slab_unlock(page);
Christoph Lameter8a5ec0b2011-02-25 11:38:54 -06002087#ifdef CONFIG_CMPXCHG_LOCAL
2088 local_irq_restore(flags);
2089#endif
Christoph Lameter84e554e62009-12-18 16:26:23 -06002090 stat(s, FREE_SLAB);
Christoph Lameter81819f02007-05-06 14:49:36 -07002091 discard_slab(s, page);
Christoph Lameter81819f02007-05-06 14:49:36 -07002092 return;
2093
2094debug:
Christoph Lameter3ec09742007-05-16 22:11:00 -07002095 if (!free_debug_processing(s, page, x, addr))
Christoph Lameter77c5e2d2007-05-06 14:49:42 -07002096 goto out_unlock;
Christoph Lameter77c5e2d2007-05-06 14:49:42 -07002097 goto checks_ok;
Christoph Lameter81819f02007-05-06 14:49:36 -07002098}
2099
Christoph Lameter894b8782007-05-10 03:15:16 -07002100/*
2101 * Fastpath with forced inlining to produce a kfree and kmem_cache_free that
2102 * can perform fastpath freeing without additional function calls.
2103 *
2104 * The fastpath is only possible if we are freeing to the current cpu slab
2105 * of this processor. This typically the case if we have just allocated
2106 * the item before.
2107 *
2108 * If fastpath is not possible then fall back to __slab_free where we deal
2109 * with all sorts of special processing.
2110 */
Pekka Enberg06428782008-01-07 23:20:27 -08002111static __always_inline void slab_free(struct kmem_cache *s,
Eduard - Gabriel Munteanuce71e272008-08-19 20:43:25 +03002112 struct page *page, void *x, unsigned long addr)
Christoph Lameter894b8782007-05-10 03:15:16 -07002113{
2114 void **object = (void *)x;
Christoph Lameterdfb4f092007-10-16 01:26:05 -07002115 struct kmem_cache_cpu *c;
Christoph Lameter8a5ec0b2011-02-25 11:38:54 -06002116#ifdef CONFIG_CMPXCHG_LOCAL
2117 unsigned long tid;
2118#else
Christoph Lameter1f842602008-01-07 23:20:30 -08002119 unsigned long flags;
Christoph Lameter8a5ec0b2011-02-25 11:38:54 -06002120#endif
Christoph Lameter1f842602008-01-07 23:20:30 -08002121
Christoph Lameterc016b0b2010-08-20 12:37:16 -05002122 slab_free_hook(s, x);
2123
Christoph Lameter8a5ec0b2011-02-25 11:38:54 -06002124#ifndef CONFIG_CMPXCHG_LOCAL
Christoph Lameter894b8782007-05-10 03:15:16 -07002125 local_irq_save(flags);
Christoph Lametera24c5a02011-03-15 12:45:21 -05002126
2127#else
2128redo:
Christoph Lameter8a5ec0b2011-02-25 11:38:54 -06002129#endif
2130
Christoph Lameter8a5ec0b2011-02-25 11:38:54 -06002131 /*
2132 * Determine the currently cpus per cpu slab.
2133 * The cpu may change afterward. However that does not matter since
2134 * data is retrieved via this pointer. If we are on the same cpu
2135 * during the cmpxchg then the free will succedd.
2136 */
Christoph Lameter9dfc6e62009-12-18 16:26:20 -06002137 c = __this_cpu_ptr(s->cpu_slab);
Christoph Lameterc016b0b2010-08-20 12:37:16 -05002138
Christoph Lameter8a5ec0b2011-02-25 11:38:54 -06002139#ifdef CONFIG_CMPXCHG_LOCAL
2140 tid = c->tid;
2141 barrier();
2142#endif
Christoph Lameterc016b0b2010-08-20 12:37:16 -05002143
Pekka Enberg15b7c512010-10-02 11:32:32 +03002144 if (likely(page == c->page && c->node != NUMA_NO_NODE)) {
Christoph Lameterff120592009-12-18 16:26:22 -06002145 set_freepointer(s, object, c->freelist);
Christoph Lameter8a5ec0b2011-02-25 11:38:54 -06002146
2147#ifdef CONFIG_CMPXCHG_LOCAL
2148 if (unlikely(!this_cpu_cmpxchg_double(
2149 s->cpu_slab->freelist, s->cpu_slab->tid,
2150 c->freelist, tid,
2151 object, next_tid(tid)))) {
2152
2153 note_cmpxchg_failure("slab_free", s, tid);
2154 goto redo;
2155 }
2156#else
Christoph Lameterdfb4f092007-10-16 01:26:05 -07002157 c->freelist = object;
Christoph Lameter8a5ec0b2011-02-25 11:38:54 -06002158#endif
Christoph Lameter84e554e62009-12-18 16:26:23 -06002159 stat(s, FREE_FASTPATH);
Christoph Lameter894b8782007-05-10 03:15:16 -07002160 } else
Christoph Lameterff120592009-12-18 16:26:22 -06002161 __slab_free(s, page, x, addr);
Christoph Lameter894b8782007-05-10 03:15:16 -07002162
Christoph Lameter8a5ec0b2011-02-25 11:38:54 -06002163#ifndef CONFIG_CMPXCHG_LOCAL
Christoph Lameter894b8782007-05-10 03:15:16 -07002164 local_irq_restore(flags);
Christoph Lameter8a5ec0b2011-02-25 11:38:54 -06002165#endif
Christoph Lameter894b8782007-05-10 03:15:16 -07002166}
2167
Christoph Lameter81819f02007-05-06 14:49:36 -07002168void kmem_cache_free(struct kmem_cache *s, void *x)
2169{
Christoph Lameter77c5e2d2007-05-06 14:49:42 -07002170 struct page *page;
Christoph Lameter81819f02007-05-06 14:49:36 -07002171
Christoph Lameterb49af682007-05-06 14:49:41 -07002172 page = virt_to_head_page(x);
Christoph Lameter81819f02007-05-06 14:49:36 -07002173
Eduard - Gabriel Munteanuce71e272008-08-19 20:43:25 +03002174 slab_free(s, page, x, _RET_IP_);
Eduard - Gabriel Munteanu5b882be2008-08-19 20:43:26 +03002175
Eduard - Gabriel Munteanuca2b84cb2009-03-23 15:12:24 +02002176 trace_kmem_cache_free(_RET_IP_, x);
Christoph Lameter81819f02007-05-06 14:49:36 -07002177}
2178EXPORT_SYMBOL(kmem_cache_free);
2179
Christoph Lameter81819f02007-05-06 14:49:36 -07002180/*
Christoph Lameter672bba32007-05-09 02:32:39 -07002181 * Object placement in a slab is made very easy because we always start at
2182 * offset 0. If we tune the size of the object to the alignment then we can
2183 * get the required alignment by putting one properly sized object after
2184 * another.
Christoph Lameter81819f02007-05-06 14:49:36 -07002185 *
2186 * Notice that the allocation order determines the sizes of the per cpu
2187 * caches. Each processor has always one slab available for allocations.
2188 * Increasing the allocation order reduces the number of times that slabs
Christoph Lameter672bba32007-05-09 02:32:39 -07002189 * must be moved on and off the partial lists and is therefore a factor in
Christoph Lameter81819f02007-05-06 14:49:36 -07002190 * locking overhead.
Christoph Lameter81819f02007-05-06 14:49:36 -07002191 */
2192
2193/*
2194 * Mininum / Maximum order of slab pages. This influences locking overhead
2195 * and slab fragmentation. A higher order reduces the number of partial slabs
2196 * and increases the number of allocations possible without having to
2197 * take the list_lock.
2198 */
2199static int slub_min_order;
Christoph Lameter114e9e82008-04-14 19:11:41 +03002200static int slub_max_order = PAGE_ALLOC_COSTLY_ORDER;
Christoph Lameter9b2cd502008-04-14 19:11:41 +03002201static int slub_min_objects;
Christoph Lameter81819f02007-05-06 14:49:36 -07002202
2203/*
2204 * Merge control. If this is set then no merging of slab caches will occur.
Christoph Lameter672bba32007-05-09 02:32:39 -07002205 * (Could be removed. This was introduced to pacify the merge skeptics.)
Christoph Lameter81819f02007-05-06 14:49:36 -07002206 */
2207static int slub_nomerge;
2208
2209/*
Christoph Lameter81819f02007-05-06 14:49:36 -07002210 * Calculate the order of allocation given an slab object size.
2211 *
Christoph Lameter672bba32007-05-09 02:32:39 -07002212 * The order of allocation has significant impact on performance and other
2213 * system components. Generally order 0 allocations should be preferred since
2214 * order 0 does not cause fragmentation in the page allocator. Larger objects
2215 * be problematic to put into order 0 slabs because there may be too much
Christoph Lameterc124f5b2008-04-14 19:13:29 +03002216 * unused space left. We go to a higher order if more than 1/16th of the slab
Christoph Lameter672bba32007-05-09 02:32:39 -07002217 * would be wasted.
Christoph Lameter81819f02007-05-06 14:49:36 -07002218 *
Christoph Lameter672bba32007-05-09 02:32:39 -07002219 * In order to reach satisfactory performance we must ensure that a minimum
2220 * number of objects is in one slab. Otherwise we may generate too much
2221 * activity on the partial lists which requires taking the list_lock. This is
2222 * less a concern for large slabs though which are rarely used.
Christoph Lameter81819f02007-05-06 14:49:36 -07002223 *
Christoph Lameter672bba32007-05-09 02:32:39 -07002224 * slub_max_order specifies the order where we begin to stop considering the
2225 * number of objects in a slab as critical. If we reach slub_max_order then
2226 * we try to keep the page order as low as possible. So we accept more waste
2227 * of space in favor of a small page order.
2228 *
2229 * Higher order allocations also allow the placement of more objects in a
2230 * slab and thereby reduce object handling overhead. If the user has
2231 * requested a higher mininum order then we start with that one instead of
2232 * the smallest order which will fit the object.
Christoph Lameter81819f02007-05-06 14:49:36 -07002233 */
Christoph Lameter5e6d4442007-05-09 02:32:46 -07002234static inline int slab_order(int size, int min_objects,
Lai Jiangshanab9a0f12011-03-10 15:21:48 +08002235 int max_order, int fract_leftover, int reserved)
Christoph Lameter81819f02007-05-06 14:49:36 -07002236{
2237 int order;
2238 int rem;
Christoph Lameter6300ea72007-07-17 04:03:20 -07002239 int min_order = slub_min_order;
Christoph Lameter81819f02007-05-06 14:49:36 -07002240
Lai Jiangshanab9a0f12011-03-10 15:21:48 +08002241 if (order_objects(min_order, size, reserved) > MAX_OBJS_PER_PAGE)
Cyrill Gorcunov210b5c02008-10-22 23:00:38 +04002242 return get_order(size * MAX_OBJS_PER_PAGE) - 1;
Christoph Lameter39b26462008-04-14 19:11:30 +03002243
Christoph Lameter6300ea72007-07-17 04:03:20 -07002244 for (order = max(min_order,
Christoph Lameter5e6d4442007-05-09 02:32:46 -07002245 fls(min_objects * size - 1) - PAGE_SHIFT);
2246 order <= max_order; order++) {
2247
Christoph Lameter81819f02007-05-06 14:49:36 -07002248 unsigned long slab_size = PAGE_SIZE << order;
2249
Lai Jiangshanab9a0f12011-03-10 15:21:48 +08002250 if (slab_size < min_objects * size + reserved)
Christoph Lameter81819f02007-05-06 14:49:36 -07002251 continue;
2252
Lai Jiangshanab9a0f12011-03-10 15:21:48 +08002253 rem = (slab_size - reserved) % size;
Christoph Lameter81819f02007-05-06 14:49:36 -07002254
Christoph Lameter5e6d4442007-05-09 02:32:46 -07002255 if (rem <= slab_size / fract_leftover)
Christoph Lameter81819f02007-05-06 14:49:36 -07002256 break;
2257
2258 }
Christoph Lameter672bba32007-05-09 02:32:39 -07002259
Christoph Lameter81819f02007-05-06 14:49:36 -07002260 return order;
2261}
2262
Lai Jiangshanab9a0f12011-03-10 15:21:48 +08002263static inline int calculate_order(int size, int reserved)
Christoph Lameter5e6d4442007-05-09 02:32:46 -07002264{
2265 int order;
2266 int min_objects;
2267 int fraction;
Zhang Yanmine8120ff2009-02-12 18:00:17 +02002268 int max_objects;
Christoph Lameter5e6d4442007-05-09 02:32:46 -07002269
2270 /*
2271 * Attempt to find best configuration for a slab. This
2272 * works by first attempting to generate a layout with
2273 * the best configuration and backing off gradually.
2274 *
2275 * First we reduce the acceptable waste in a slab. Then
2276 * we reduce the minimum objects required in a slab.
2277 */
2278 min_objects = slub_min_objects;
Christoph Lameter9b2cd502008-04-14 19:11:41 +03002279 if (!min_objects)
2280 min_objects = 4 * (fls(nr_cpu_ids) + 1);
Lai Jiangshanab9a0f12011-03-10 15:21:48 +08002281 max_objects = order_objects(slub_max_order, size, reserved);
Zhang Yanmine8120ff2009-02-12 18:00:17 +02002282 min_objects = min(min_objects, max_objects);
2283
Christoph Lameter5e6d4442007-05-09 02:32:46 -07002284 while (min_objects > 1) {
Christoph Lameterc124f5b2008-04-14 19:13:29 +03002285 fraction = 16;
Christoph Lameter5e6d4442007-05-09 02:32:46 -07002286 while (fraction >= 4) {
2287 order = slab_order(size, min_objects,
Lai Jiangshanab9a0f12011-03-10 15:21:48 +08002288 slub_max_order, fraction, reserved);
Christoph Lameter5e6d4442007-05-09 02:32:46 -07002289 if (order <= slub_max_order)
2290 return order;
2291 fraction /= 2;
2292 }
Amerigo Wang5086c389c2009-08-19 21:44:13 +03002293 min_objects--;
Christoph Lameter5e6d4442007-05-09 02:32:46 -07002294 }
2295
2296 /*
2297 * We were unable to place multiple objects in a slab. Now
2298 * lets see if we can place a single object there.
2299 */
Lai Jiangshanab9a0f12011-03-10 15:21:48 +08002300 order = slab_order(size, 1, slub_max_order, 1, reserved);
Christoph Lameter5e6d4442007-05-09 02:32:46 -07002301 if (order <= slub_max_order)
2302 return order;
2303
2304 /*
2305 * Doh this slab cannot be placed using slub_max_order.
2306 */
Lai Jiangshanab9a0f12011-03-10 15:21:48 +08002307 order = slab_order(size, 1, MAX_ORDER, 1, reserved);
David Rientjes818cf592009-04-23 09:58:22 +03002308 if (order < MAX_ORDER)
Christoph Lameter5e6d4442007-05-09 02:32:46 -07002309 return order;
2310 return -ENOSYS;
2311}
2312
Christoph Lameter81819f02007-05-06 14:49:36 -07002313/*
Christoph Lameter672bba32007-05-09 02:32:39 -07002314 * Figure out what the alignment of the objects will be.
Christoph Lameter81819f02007-05-06 14:49:36 -07002315 */
2316static unsigned long calculate_alignment(unsigned long flags,
2317 unsigned long align, unsigned long size)
2318{
2319 /*
Christoph Lameter6446faa2008-02-15 23:45:26 -08002320 * If the user wants hardware cache aligned objects then follow that
2321 * suggestion if the object is sufficiently large.
Christoph Lameter81819f02007-05-06 14:49:36 -07002322 *
Christoph Lameter6446faa2008-02-15 23:45:26 -08002323 * The hardware cache alignment cannot override the specified
2324 * alignment though. If that is greater then use it.
Christoph Lameter81819f02007-05-06 14:49:36 -07002325 */
Nick Pigginb6210382008-03-05 14:05:56 -08002326 if (flags & SLAB_HWCACHE_ALIGN) {
2327 unsigned long ralign = cache_line_size();
2328 while (size <= ralign / 2)
2329 ralign /= 2;
2330 align = max(align, ralign);
2331 }
Christoph Lameter81819f02007-05-06 14:49:36 -07002332
2333 if (align < ARCH_SLAB_MINALIGN)
Nick Pigginb6210382008-03-05 14:05:56 -08002334 align = ARCH_SLAB_MINALIGN;
Christoph Lameter81819f02007-05-06 14:49:36 -07002335
2336 return ALIGN(align, sizeof(void *));
2337}
2338
Pekka Enberg5595cff2008-08-05 09:28:47 +03002339static void
2340init_kmem_cache_node(struct kmem_cache_node *n, struct kmem_cache *s)
Christoph Lameter81819f02007-05-06 14:49:36 -07002341{
2342 n->nr_partial = 0;
Christoph Lameter81819f02007-05-06 14:49:36 -07002343 spin_lock_init(&n->list_lock);
2344 INIT_LIST_HEAD(&n->partial);
Christoph Lameter8ab13722007-07-17 04:03:32 -07002345#ifdef CONFIG_SLUB_DEBUG
Christoph Lameter0f389ec2008-04-14 18:53:02 +03002346 atomic_long_set(&n->nr_slabs, 0);
Salman Qazi02b71b72008-09-11 12:25:41 -07002347 atomic_long_set(&n->total_objects, 0);
Christoph Lameter643b1132007-05-06 14:49:42 -07002348 INIT_LIST_HEAD(&n->full);
Christoph Lameter8ab13722007-07-17 04:03:32 -07002349#endif
Christoph Lameter81819f02007-05-06 14:49:36 -07002350}
2351
Christoph Lameter55136592010-08-20 12:37:13 -05002352static inline int alloc_kmem_cache_cpus(struct kmem_cache *s)
Christoph Lameter4c93c3552007-10-16 01:26:08 -07002353{
Christoph Lameter6c182dc2010-08-20 12:37:14 -05002354 BUILD_BUG_ON(PERCPU_DYNAMIC_EARLY_SIZE <
2355 SLUB_PAGE_SHIFT * sizeof(struct kmem_cache_cpu));
Christoph Lameter9dfc6e62009-12-18 16:26:20 -06002356
Christoph Lameter8a5ec0b2011-02-25 11:38:54 -06002357#ifdef CONFIG_CMPXCHG_LOCAL
2358 /*
2359 * Must align to double word boundary for the double cmpxchg instructions
2360 * to work.
2361 */
2362 s->cpu_slab = __alloc_percpu(sizeof(struct kmem_cache_cpu), 2 * sizeof(void *));
2363#else
2364 /* Regular alignment is sufficient */
Christoph Lameter6c182dc2010-08-20 12:37:14 -05002365 s->cpu_slab = alloc_percpu(struct kmem_cache_cpu);
Christoph Lameter8a5ec0b2011-02-25 11:38:54 -06002366#endif
Christoph Lameter9dfc6e62009-12-18 16:26:20 -06002367
Christoph Lameter8a5ec0b2011-02-25 11:38:54 -06002368 if (!s->cpu_slab)
2369 return 0;
2370
2371 init_kmem_cache_cpus(s);
2372
2373 return 1;
Christoph Lameter4c93c3552007-10-16 01:26:08 -07002374}
Christoph Lameter4c93c3552007-10-16 01:26:08 -07002375
Christoph Lameter51df1142010-08-20 12:37:15 -05002376static struct kmem_cache *kmem_cache_node;
2377
Christoph Lameter81819f02007-05-06 14:49:36 -07002378/*
2379 * No kmalloc_node yet so do it by hand. We know that this is the first
2380 * slab on the node for this slabcache. There are no concurrent accesses
2381 * possible.
2382 *
2383 * Note that this function only works on the kmalloc_node_cache
Christoph Lameter4c93c3552007-10-16 01:26:08 -07002384 * when allocating for the kmalloc_node_cache. This is used for bootstrapping
2385 * memory on a fresh node that has no slab structures yet.
Christoph Lameter81819f02007-05-06 14:49:36 -07002386 */
Christoph Lameter55136592010-08-20 12:37:13 -05002387static void early_kmem_cache_node_alloc(int node)
Christoph Lameter81819f02007-05-06 14:49:36 -07002388{
2389 struct page *page;
2390 struct kmem_cache_node *n;
rootba84c732008-01-07 23:20:28 -08002391 unsigned long flags;
Christoph Lameter81819f02007-05-06 14:49:36 -07002392
Christoph Lameter51df1142010-08-20 12:37:15 -05002393 BUG_ON(kmem_cache_node->size < sizeof(struct kmem_cache_node));
Christoph Lameter81819f02007-05-06 14:49:36 -07002394
Christoph Lameter51df1142010-08-20 12:37:15 -05002395 page = new_slab(kmem_cache_node, GFP_NOWAIT, node);
Christoph Lameter81819f02007-05-06 14:49:36 -07002396
2397 BUG_ON(!page);
Christoph Lametera2f92ee2007-08-22 14:01:57 -07002398 if (page_to_nid(page) != node) {
2399 printk(KERN_ERR "SLUB: Unable to allocate memory from "
2400 "node %d\n", node);
2401 printk(KERN_ERR "SLUB: Allocating a useless per node structure "
2402 "in order to be able to continue\n");
2403 }
2404
Christoph Lameter81819f02007-05-06 14:49:36 -07002405 n = page->freelist;
2406 BUG_ON(!n);
Christoph Lameter51df1142010-08-20 12:37:15 -05002407 page->freelist = get_freepointer(kmem_cache_node, n);
Christoph Lameter81819f02007-05-06 14:49:36 -07002408 page->inuse++;
Christoph Lameter51df1142010-08-20 12:37:15 -05002409 kmem_cache_node->node[node] = n;
Christoph Lameter8ab13722007-07-17 04:03:32 -07002410#ifdef CONFIG_SLUB_DEBUG
Christoph Lameterf7cb1932010-09-29 07:15:01 -05002411 init_object(kmem_cache_node, n, SLUB_RED_ACTIVE);
Christoph Lameter51df1142010-08-20 12:37:15 -05002412 init_tracking(kmem_cache_node, n);
Christoph Lameter8ab13722007-07-17 04:03:32 -07002413#endif
Christoph Lameter51df1142010-08-20 12:37:15 -05002414 init_kmem_cache_node(n, kmem_cache_node);
2415 inc_slabs_node(kmem_cache_node, node, page->objects);
Christoph Lameter6446faa2008-02-15 23:45:26 -08002416
rootba84c732008-01-07 23:20:28 -08002417 /*
2418 * lockdep requires consistent irq usage for each lock
2419 * so even though there cannot be a race this early in
2420 * the boot sequence, we still disable irqs.
2421 */
2422 local_irq_save(flags);
Christoph Lameter7c2e1322008-01-07 23:20:27 -08002423 add_partial(n, page, 0);
rootba84c732008-01-07 23:20:28 -08002424 local_irq_restore(flags);
Christoph Lameter81819f02007-05-06 14:49:36 -07002425}
2426
2427static void free_kmem_cache_nodes(struct kmem_cache *s)
2428{
2429 int node;
2430
Christoph Lameterf64dc582007-10-16 01:25:33 -07002431 for_each_node_state(node, N_NORMAL_MEMORY) {
Christoph Lameter81819f02007-05-06 14:49:36 -07002432 struct kmem_cache_node *n = s->node[node];
Christoph Lameter51df1142010-08-20 12:37:15 -05002433
Alexander Duyck73367bd2010-05-21 14:41:35 -07002434 if (n)
Christoph Lameter51df1142010-08-20 12:37:15 -05002435 kmem_cache_free(kmem_cache_node, n);
2436
Christoph Lameter81819f02007-05-06 14:49:36 -07002437 s->node[node] = NULL;
2438 }
2439}
2440
Christoph Lameter55136592010-08-20 12:37:13 -05002441static int init_kmem_cache_nodes(struct kmem_cache *s)
Christoph Lameter81819f02007-05-06 14:49:36 -07002442{
2443 int node;
Christoph Lameter81819f02007-05-06 14:49:36 -07002444
Christoph Lameterf64dc582007-10-16 01:25:33 -07002445 for_each_node_state(node, N_NORMAL_MEMORY) {
Christoph Lameter81819f02007-05-06 14:49:36 -07002446 struct kmem_cache_node *n;
2447
Alexander Duyck73367bd2010-05-21 14:41:35 -07002448 if (slab_state == DOWN) {
Christoph Lameter55136592010-08-20 12:37:13 -05002449 early_kmem_cache_node_alloc(node);
Alexander Duyck73367bd2010-05-21 14:41:35 -07002450 continue;
Christoph Lameter81819f02007-05-06 14:49:36 -07002451 }
Christoph Lameter51df1142010-08-20 12:37:15 -05002452 n = kmem_cache_alloc_node(kmem_cache_node,
Christoph Lameter55136592010-08-20 12:37:13 -05002453 GFP_KERNEL, node);
Alexander Duyck73367bd2010-05-21 14:41:35 -07002454
2455 if (!n) {
2456 free_kmem_cache_nodes(s);
2457 return 0;
2458 }
2459
Christoph Lameter81819f02007-05-06 14:49:36 -07002460 s->node[node] = n;
Pekka Enberg5595cff2008-08-05 09:28:47 +03002461 init_kmem_cache_node(n, s);
Christoph Lameter81819f02007-05-06 14:49:36 -07002462 }
2463 return 1;
2464}
Christoph Lameter81819f02007-05-06 14:49:36 -07002465
David Rientjesc0bdb232009-02-25 09:16:35 +02002466static void set_min_partial(struct kmem_cache *s, unsigned long min)
David Rientjes3b89d7d2009-02-22 17:40:07 -08002467{
2468 if (min < MIN_PARTIAL)
2469 min = MIN_PARTIAL;
2470 else if (min > MAX_PARTIAL)
2471 min = MAX_PARTIAL;
2472 s->min_partial = min;
2473}
2474
Christoph Lameter81819f02007-05-06 14:49:36 -07002475/*
2476 * calculate_sizes() determines the order and the distribution of data within
2477 * a slab object.
2478 */
Christoph Lameter06b285d2008-04-14 19:11:41 +03002479static int calculate_sizes(struct kmem_cache *s, int forced_order)
Christoph Lameter81819f02007-05-06 14:49:36 -07002480{
2481 unsigned long flags = s->flags;
2482 unsigned long size = s->objsize;
2483 unsigned long align = s->align;
Christoph Lameter834f3d12008-04-14 19:11:31 +03002484 int order;
Christoph Lameter81819f02007-05-06 14:49:36 -07002485
2486 /*
Christoph Lameterd8b42bf2008-02-15 23:45:25 -08002487 * Round up object size to the next word boundary. We can only
2488 * place the free pointer at word boundaries and this determines
2489 * the possible location of the free pointer.
2490 */
2491 size = ALIGN(size, sizeof(void *));
2492
2493#ifdef CONFIG_SLUB_DEBUG
2494 /*
Christoph Lameter81819f02007-05-06 14:49:36 -07002495 * Determine if we can poison the object itself. If the user of
2496 * the slab may touch the object after free or before allocation
2497 * then we should never poison the object itself.
2498 */
2499 if ((flags & SLAB_POISON) && !(flags & SLAB_DESTROY_BY_RCU) &&
Christoph Lameterc59def92007-05-16 22:10:50 -07002500 !s->ctor)
Christoph Lameter81819f02007-05-06 14:49:36 -07002501 s->flags |= __OBJECT_POISON;
2502 else
2503 s->flags &= ~__OBJECT_POISON;
2504
Christoph Lameter81819f02007-05-06 14:49:36 -07002505
2506 /*
Christoph Lameter672bba32007-05-09 02:32:39 -07002507 * If we are Redzoning then check if there is some space between the
Christoph Lameter81819f02007-05-06 14:49:36 -07002508 * end of the object and the free pointer. If not then add an
Christoph Lameter672bba32007-05-09 02:32:39 -07002509 * additional word to have some bytes to store Redzone information.
Christoph Lameter81819f02007-05-06 14:49:36 -07002510 */
2511 if ((flags & SLAB_RED_ZONE) && size == s->objsize)
2512 size += sizeof(void *);
Christoph Lameter41ecc552007-05-09 02:32:44 -07002513#endif
Christoph Lameter81819f02007-05-06 14:49:36 -07002514
2515 /*
Christoph Lameter672bba32007-05-09 02:32:39 -07002516 * With that we have determined the number of bytes in actual use
2517 * by the object. This is the potential offset to the free pointer.
Christoph Lameter81819f02007-05-06 14:49:36 -07002518 */
2519 s->inuse = size;
2520
2521 if (((flags & (SLAB_DESTROY_BY_RCU | SLAB_POISON)) ||
Christoph Lameterc59def92007-05-16 22:10:50 -07002522 s->ctor)) {
Christoph Lameter81819f02007-05-06 14:49:36 -07002523 /*
2524 * Relocate free pointer after the object if it is not
2525 * permitted to overwrite the first word of the object on
2526 * kmem_cache_free.
2527 *
2528 * This is the case if we do RCU, have a constructor or
2529 * destructor or are poisoning the objects.
2530 */
2531 s->offset = size;
2532 size += sizeof(void *);
2533 }
2534
Christoph Lameterc12b3c62007-05-23 13:57:31 -07002535#ifdef CONFIG_SLUB_DEBUG
Christoph Lameter81819f02007-05-06 14:49:36 -07002536 if (flags & SLAB_STORE_USER)
2537 /*
2538 * Need to store information about allocs and frees after
2539 * the object.
2540 */
2541 size += 2 * sizeof(struct track);
2542
Christoph Lameterbe7b3fb2007-05-09 02:32:36 -07002543 if (flags & SLAB_RED_ZONE)
Christoph Lameter81819f02007-05-06 14:49:36 -07002544 /*
2545 * Add some empty padding so that we can catch
2546 * overwrites from earlier objects rather than let
2547 * tracking information or the free pointer be
Frederik Schwarzer0211a9c2008-12-29 22:14:56 +01002548 * corrupted if a user writes before the start
Christoph Lameter81819f02007-05-06 14:49:36 -07002549 * of the object.
2550 */
2551 size += sizeof(void *);
Christoph Lameter41ecc552007-05-09 02:32:44 -07002552#endif
Christoph Lameter672bba32007-05-09 02:32:39 -07002553
Christoph Lameter81819f02007-05-06 14:49:36 -07002554 /*
2555 * Determine the alignment based on various parameters that the
Christoph Lameter65c02d42007-05-09 02:32:35 -07002556 * user specified and the dynamic determination of cache line size
2557 * on bootup.
Christoph Lameter81819f02007-05-06 14:49:36 -07002558 */
2559 align = calculate_alignment(flags, align, s->objsize);
Zhang, Yanmindcb0ce12009-07-30 11:28:11 +08002560 s->align = align;
Christoph Lameter81819f02007-05-06 14:49:36 -07002561
2562 /*
2563 * SLUB stores one object immediately after another beginning from
2564 * offset 0. In order to align the objects we have to simply size
2565 * each object to conform to the alignment.
2566 */
2567 size = ALIGN(size, align);
2568 s->size = size;
Christoph Lameter06b285d2008-04-14 19:11:41 +03002569 if (forced_order >= 0)
2570 order = forced_order;
2571 else
Lai Jiangshanab9a0f12011-03-10 15:21:48 +08002572 order = calculate_order(size, s->reserved);
Christoph Lameter81819f02007-05-06 14:49:36 -07002573
Christoph Lameter834f3d12008-04-14 19:11:31 +03002574 if (order < 0)
Christoph Lameter81819f02007-05-06 14:49:36 -07002575 return 0;
2576
Christoph Lameterb7a49f02008-02-14 14:21:32 -08002577 s->allocflags = 0;
Christoph Lameter834f3d12008-04-14 19:11:31 +03002578 if (order)
Christoph Lameterb7a49f02008-02-14 14:21:32 -08002579 s->allocflags |= __GFP_COMP;
2580
2581 if (s->flags & SLAB_CACHE_DMA)
2582 s->allocflags |= SLUB_DMA;
2583
2584 if (s->flags & SLAB_RECLAIM_ACCOUNT)
2585 s->allocflags |= __GFP_RECLAIMABLE;
2586
Christoph Lameter81819f02007-05-06 14:49:36 -07002587 /*
2588 * Determine the number of objects per slab
2589 */
Lai Jiangshanab9a0f12011-03-10 15:21:48 +08002590 s->oo = oo_make(order, size, s->reserved);
2591 s->min = oo_make(get_order(size), size, s->reserved);
Christoph Lameter205ab992008-04-14 19:11:40 +03002592 if (oo_objects(s->oo) > oo_objects(s->max))
2593 s->max = s->oo;
Christoph Lameter81819f02007-05-06 14:49:36 -07002594
Christoph Lameter834f3d12008-04-14 19:11:31 +03002595 return !!oo_objects(s->oo);
Christoph Lameter81819f02007-05-06 14:49:36 -07002596
2597}
2598
Christoph Lameter55136592010-08-20 12:37:13 -05002599static int kmem_cache_open(struct kmem_cache *s,
Christoph Lameter81819f02007-05-06 14:49:36 -07002600 const char *name, size_t size,
2601 size_t align, unsigned long flags,
Alexey Dobriyan51cc5062008-07-25 19:45:34 -07002602 void (*ctor)(void *))
Christoph Lameter81819f02007-05-06 14:49:36 -07002603{
2604 memset(s, 0, kmem_size);
2605 s->name = name;
2606 s->ctor = ctor;
Christoph Lameter81819f02007-05-06 14:49:36 -07002607 s->objsize = size;
Christoph Lameter81819f02007-05-06 14:49:36 -07002608 s->align = align;
Christoph Lameterba0268a2007-09-11 15:24:11 -07002609 s->flags = kmem_cache_flags(size, flags, name, ctor);
Lai Jiangshanab9a0f12011-03-10 15:21:48 +08002610 s->reserved = 0;
Christoph Lameter81819f02007-05-06 14:49:36 -07002611
Lai Jiangshanda9a6382011-03-10 15:22:00 +08002612 if (need_reserve_slab_rcu && (s->flags & SLAB_DESTROY_BY_RCU))
2613 s->reserved = sizeof(struct rcu_head);
Christoph Lameter81819f02007-05-06 14:49:36 -07002614
Christoph Lameter06b285d2008-04-14 19:11:41 +03002615 if (!calculate_sizes(s, -1))
Christoph Lameter81819f02007-05-06 14:49:36 -07002616 goto error;
David Rientjes3de47212009-07-27 18:30:35 -07002617 if (disable_higher_order_debug) {
2618 /*
2619 * Disable debugging flags that store metadata if the min slab
2620 * order increased.
2621 */
2622 if (get_order(s->size) > get_order(s->objsize)) {
2623 s->flags &= ~DEBUG_METADATA_FLAGS;
2624 s->offset = 0;
2625 if (!calculate_sizes(s, -1))
2626 goto error;
2627 }
2628 }
Christoph Lameter81819f02007-05-06 14:49:36 -07002629
David Rientjes3b89d7d2009-02-22 17:40:07 -08002630 /*
2631 * The larger the object size is, the more pages we want on the partial
2632 * list to avoid pounding the page allocator excessively.
2633 */
David Rientjesc0bdb232009-02-25 09:16:35 +02002634 set_min_partial(s, ilog2(s->size));
Christoph Lameter81819f02007-05-06 14:49:36 -07002635 s->refcount = 1;
2636#ifdef CONFIG_NUMA
Christoph Lametere2cb96b2008-08-19 08:51:22 -05002637 s->remote_node_defrag_ratio = 1000;
Christoph Lameter81819f02007-05-06 14:49:36 -07002638#endif
Christoph Lameter55136592010-08-20 12:37:13 -05002639 if (!init_kmem_cache_nodes(s))
Christoph Lameterdfb4f092007-10-16 01:26:05 -07002640 goto error;
Christoph Lameter81819f02007-05-06 14:49:36 -07002641
Christoph Lameter55136592010-08-20 12:37:13 -05002642 if (alloc_kmem_cache_cpus(s))
Christoph Lameter81819f02007-05-06 14:49:36 -07002643 return 1;
Christoph Lameterff120592009-12-18 16:26:22 -06002644
Christoph Lameter4c93c3552007-10-16 01:26:08 -07002645 free_kmem_cache_nodes(s);
Christoph Lameter81819f02007-05-06 14:49:36 -07002646error:
2647 if (flags & SLAB_PANIC)
2648 panic("Cannot create slab %s size=%lu realsize=%u "
2649 "order=%u offset=%u flags=%lx\n",
Christoph Lameter834f3d12008-04-14 19:11:31 +03002650 s->name, (unsigned long)size, s->size, oo_order(s->oo),
Christoph Lameter81819f02007-05-06 14:49:36 -07002651 s->offset, flags);
2652 return 0;
2653}
Christoph Lameter81819f02007-05-06 14:49:36 -07002654
2655/*
Christoph Lameter81819f02007-05-06 14:49:36 -07002656 * Determine the size of a slab object
2657 */
2658unsigned int kmem_cache_size(struct kmem_cache *s)
2659{
2660 return s->objsize;
2661}
2662EXPORT_SYMBOL(kmem_cache_size);
2663
Christoph Lameter33b12c32008-04-25 12:22:43 -07002664static void list_slab_objects(struct kmem_cache *s, struct page *page,
2665 const char *text)
Christoph Lameter81819f02007-05-06 14:49:36 -07002666{
Christoph Lameter33b12c32008-04-25 12:22:43 -07002667#ifdef CONFIG_SLUB_DEBUG
2668 void *addr = page_address(page);
2669 void *p;
Namhyung Kima5dd5c12010-09-29 21:02:13 +09002670 unsigned long *map = kzalloc(BITS_TO_LONGS(page->objects) *
2671 sizeof(long), GFP_ATOMIC);
Eric Dumazetbbd7d572010-03-24 22:25:47 +01002672 if (!map)
2673 return;
Christoph Lameter33b12c32008-04-25 12:22:43 -07002674 slab_err(s, page, "%s", text);
2675 slab_lock(page);
2676 for_each_free_object(p, s, page->freelist)
2677 set_bit(slab_index(p, s, addr), map);
2678
2679 for_each_object(p, s, addr, page->objects) {
2680
2681 if (!test_bit(slab_index(p, s, addr), map)) {
2682 printk(KERN_ERR "INFO: Object 0x%p @offset=%tu\n",
2683 p, p - addr);
2684 print_tracking(s, p);
2685 }
2686 }
2687 slab_unlock(page);
Eric Dumazetbbd7d572010-03-24 22:25:47 +01002688 kfree(map);
Christoph Lameter33b12c32008-04-25 12:22:43 -07002689#endif
2690}
2691
Christoph Lameter81819f02007-05-06 14:49:36 -07002692/*
Christoph Lameter599870b2008-04-23 12:36:52 -07002693 * Attempt to free all partial slabs on a node.
Christoph Lameter81819f02007-05-06 14:49:36 -07002694 */
Christoph Lameter599870b2008-04-23 12:36:52 -07002695static void free_partial(struct kmem_cache *s, struct kmem_cache_node *n)
Christoph Lameter81819f02007-05-06 14:49:36 -07002696{
Christoph Lameter81819f02007-05-06 14:49:36 -07002697 unsigned long flags;
2698 struct page *page, *h;
2699
2700 spin_lock_irqsave(&n->list_lock, flags);
Christoph Lameter33b12c32008-04-25 12:22:43 -07002701 list_for_each_entry_safe(page, h, &n->partial, lru) {
Christoph Lameter81819f02007-05-06 14:49:36 -07002702 if (!page->inuse) {
Christoph Lameter62e346a2010-09-28 08:10:28 -05002703 __remove_partial(n, page);
Christoph Lameter81819f02007-05-06 14:49:36 -07002704 discard_slab(s, page);
Christoph Lameter33b12c32008-04-25 12:22:43 -07002705 } else {
2706 list_slab_objects(s, page,
2707 "Objects remaining on kmem_cache_close()");
Christoph Lameter599870b2008-04-23 12:36:52 -07002708 }
Christoph Lameter33b12c32008-04-25 12:22:43 -07002709 }
Christoph Lameter81819f02007-05-06 14:49:36 -07002710 spin_unlock_irqrestore(&n->list_lock, flags);
Christoph Lameter81819f02007-05-06 14:49:36 -07002711}
2712
2713/*
Christoph Lameter672bba32007-05-09 02:32:39 -07002714 * Release all resources used by a slab cache.
Christoph Lameter81819f02007-05-06 14:49:36 -07002715 */
Christoph Lameter0c710012007-07-17 04:03:24 -07002716static inline int kmem_cache_close(struct kmem_cache *s)
Christoph Lameter81819f02007-05-06 14:49:36 -07002717{
2718 int node;
2719
2720 flush_all(s);
Christoph Lameter9dfc6e62009-12-18 16:26:20 -06002721 free_percpu(s->cpu_slab);
Christoph Lameter81819f02007-05-06 14:49:36 -07002722 /* Attempt to free all objects */
Christoph Lameterf64dc582007-10-16 01:25:33 -07002723 for_each_node_state(node, N_NORMAL_MEMORY) {
Christoph Lameter81819f02007-05-06 14:49:36 -07002724 struct kmem_cache_node *n = get_node(s, node);
2725
Christoph Lameter599870b2008-04-23 12:36:52 -07002726 free_partial(s, n);
2727 if (n->nr_partial || slabs_node(s, node))
Christoph Lameter81819f02007-05-06 14:49:36 -07002728 return 1;
2729 }
2730 free_kmem_cache_nodes(s);
2731 return 0;
2732}
2733
2734/*
2735 * Close a cache and release the kmem_cache structure
2736 * (must be used for caches created using kmem_cache_create)
2737 */
2738void kmem_cache_destroy(struct kmem_cache *s)
2739{
2740 down_write(&slub_lock);
2741 s->refcount--;
2742 if (!s->refcount) {
2743 list_del(&s->list);
Pekka Enbergd629d812008-04-23 22:31:08 +03002744 if (kmem_cache_close(s)) {
2745 printk(KERN_ERR "SLUB %s: %s called for cache that "
2746 "still has objects.\n", s->name, __func__);
2747 dump_stack();
2748 }
Eric Dumazetd76b1592009-09-03 22:38:59 +03002749 if (s->flags & SLAB_DESTROY_BY_RCU)
2750 rcu_barrier();
Christoph Lameter81819f02007-05-06 14:49:36 -07002751 sysfs_slab_remove(s);
Christoph Lameter2bce6482010-07-19 11:39:11 -05002752 }
2753 up_write(&slub_lock);
Christoph Lameter81819f02007-05-06 14:49:36 -07002754}
2755EXPORT_SYMBOL(kmem_cache_destroy);
2756
2757/********************************************************************
2758 * Kmalloc subsystem
2759 *******************************************************************/
2760
Christoph Lameter51df1142010-08-20 12:37:15 -05002761struct kmem_cache *kmalloc_caches[SLUB_PAGE_SHIFT];
Christoph Lameter81819f02007-05-06 14:49:36 -07002762EXPORT_SYMBOL(kmalloc_caches);
2763
Christoph Lameter51df1142010-08-20 12:37:15 -05002764static struct kmem_cache *kmem_cache;
2765
Christoph Lameter55136592010-08-20 12:37:13 -05002766#ifdef CONFIG_ZONE_DMA
Christoph Lameter51df1142010-08-20 12:37:15 -05002767static struct kmem_cache *kmalloc_dma_caches[SLUB_PAGE_SHIFT];
Christoph Lameter55136592010-08-20 12:37:13 -05002768#endif
2769
Christoph Lameter81819f02007-05-06 14:49:36 -07002770static int __init setup_slub_min_order(char *str)
2771{
Pekka Enberg06428782008-01-07 23:20:27 -08002772 get_option(&str, &slub_min_order);
Christoph Lameter81819f02007-05-06 14:49:36 -07002773
2774 return 1;
2775}
2776
2777__setup("slub_min_order=", setup_slub_min_order);
2778
2779static int __init setup_slub_max_order(char *str)
2780{
Pekka Enberg06428782008-01-07 23:20:27 -08002781 get_option(&str, &slub_max_order);
David Rientjes818cf592009-04-23 09:58:22 +03002782 slub_max_order = min(slub_max_order, MAX_ORDER - 1);
Christoph Lameter81819f02007-05-06 14:49:36 -07002783
2784 return 1;
2785}
2786
2787__setup("slub_max_order=", setup_slub_max_order);
2788
2789static int __init setup_slub_min_objects(char *str)
2790{
Pekka Enberg06428782008-01-07 23:20:27 -08002791 get_option(&str, &slub_min_objects);
Christoph Lameter81819f02007-05-06 14:49:36 -07002792
2793 return 1;
2794}
2795
2796__setup("slub_min_objects=", setup_slub_min_objects);
2797
2798static int __init setup_slub_nomerge(char *str)
2799{
2800 slub_nomerge = 1;
2801 return 1;
2802}
2803
2804__setup("slub_nomerge", setup_slub_nomerge);
2805
Christoph Lameter51df1142010-08-20 12:37:15 -05002806static struct kmem_cache *__init create_kmalloc_cache(const char *name,
2807 int size, unsigned int flags)
Christoph Lameter81819f02007-05-06 14:49:36 -07002808{
Christoph Lameter51df1142010-08-20 12:37:15 -05002809 struct kmem_cache *s;
2810
2811 s = kmem_cache_alloc(kmem_cache, GFP_NOWAIT);
2812
Pekka Enberg83b519e2009-06-10 19:40:04 +03002813 /*
2814 * This function is called with IRQs disabled during early-boot on
2815 * single CPU so there's no need to take slub_lock here.
2816 */
Christoph Lameter55136592010-08-20 12:37:13 -05002817 if (!kmem_cache_open(s, name, size, ARCH_KMALLOC_MINALIGN,
Christoph Lameter319d1e22008-04-14 19:11:41 +03002818 flags, NULL))
Christoph Lameter81819f02007-05-06 14:49:36 -07002819 goto panic;
2820
2821 list_add(&s->list, &slab_caches);
Christoph Lameter51df1142010-08-20 12:37:15 -05002822 return s;
Christoph Lameter81819f02007-05-06 14:49:36 -07002823
2824panic:
2825 panic("Creation of kmalloc slab %s size=%d failed.\n", name, size);
Christoph Lameter51df1142010-08-20 12:37:15 -05002826 return NULL;
Christoph Lameter81819f02007-05-06 14:49:36 -07002827}
2828
Christoph Lameterf1b26332007-07-17 04:03:26 -07002829/*
2830 * Conversion table for small slabs sizes / 8 to the index in the
2831 * kmalloc array. This is necessary for slabs < 192 since we have non power
2832 * of two cache sizes there. The size of larger slabs can be determined using
2833 * fls.
2834 */
2835static s8 size_index[24] = {
2836 3, /* 8 */
2837 4, /* 16 */
2838 5, /* 24 */
2839 5, /* 32 */
2840 6, /* 40 */
2841 6, /* 48 */
2842 6, /* 56 */
2843 6, /* 64 */
2844 1, /* 72 */
2845 1, /* 80 */
2846 1, /* 88 */
2847 1, /* 96 */
2848 7, /* 104 */
2849 7, /* 112 */
2850 7, /* 120 */
2851 7, /* 128 */
2852 2, /* 136 */
2853 2, /* 144 */
2854 2, /* 152 */
2855 2, /* 160 */
2856 2, /* 168 */
2857 2, /* 176 */
2858 2, /* 184 */
2859 2 /* 192 */
2860};
2861
Aaro Koskinenacdfcd02009-08-28 14:28:54 +03002862static inline int size_index_elem(size_t bytes)
2863{
2864 return (bytes - 1) / 8;
2865}
2866
Christoph Lameter81819f02007-05-06 14:49:36 -07002867static struct kmem_cache *get_slab(size_t size, gfp_t flags)
2868{
Christoph Lameterf1b26332007-07-17 04:03:26 -07002869 int index;
Christoph Lameter81819f02007-05-06 14:49:36 -07002870
Christoph Lameterf1b26332007-07-17 04:03:26 -07002871 if (size <= 192) {
2872 if (!size)
2873 return ZERO_SIZE_PTR;
Christoph Lameter81819f02007-05-06 14:49:36 -07002874
Aaro Koskinenacdfcd02009-08-28 14:28:54 +03002875 index = size_index[size_index_elem(size)];
Christoph Lameteraadb4bc2007-10-16 01:24:38 -07002876 } else
Christoph Lameterf1b26332007-07-17 04:03:26 -07002877 index = fls(size - 1);
Christoph Lameter81819f02007-05-06 14:49:36 -07002878
2879#ifdef CONFIG_ZONE_DMA
Christoph Lameterf1b26332007-07-17 04:03:26 -07002880 if (unlikely((flags & SLUB_DMA)))
Christoph Lameter51df1142010-08-20 12:37:15 -05002881 return kmalloc_dma_caches[index];
Christoph Lameterf1b26332007-07-17 04:03:26 -07002882
Christoph Lameter81819f02007-05-06 14:49:36 -07002883#endif
Christoph Lameter51df1142010-08-20 12:37:15 -05002884 return kmalloc_caches[index];
Christoph Lameter81819f02007-05-06 14:49:36 -07002885}
2886
2887void *__kmalloc(size_t size, gfp_t flags)
2888{
Christoph Lameteraadb4bc2007-10-16 01:24:38 -07002889 struct kmem_cache *s;
Eduard - Gabriel Munteanu5b882be2008-08-19 20:43:26 +03002890 void *ret;
Christoph Lameter81819f02007-05-06 14:49:36 -07002891
Christoph Lameterffadd4d2009-02-17 12:05:07 -05002892 if (unlikely(size > SLUB_MAX_SIZE))
Pekka Enbergeada35e2008-02-11 22:47:46 +02002893 return kmalloc_large(size, flags);
Christoph Lameteraadb4bc2007-10-16 01:24:38 -07002894
2895 s = get_slab(size, flags);
2896
2897 if (unlikely(ZERO_OR_NULL_PTR(s)))
Christoph Lameter6cb8f912007-07-17 04:03:22 -07002898 return s;
2899
Christoph Lameter2154a332010-07-09 14:07:10 -05002900 ret = slab_alloc(s, flags, NUMA_NO_NODE, _RET_IP_);
Eduard - Gabriel Munteanu5b882be2008-08-19 20:43:26 +03002901
Eduard - Gabriel Munteanuca2b84cb2009-03-23 15:12:24 +02002902 trace_kmalloc(_RET_IP_, ret, size, s->size, flags);
Eduard - Gabriel Munteanu5b882be2008-08-19 20:43:26 +03002903
2904 return ret;
Christoph Lameter81819f02007-05-06 14:49:36 -07002905}
2906EXPORT_SYMBOL(__kmalloc);
2907
Namhyung Kim5d1f57e2010-09-29 21:02:15 +09002908#ifdef CONFIG_NUMA
Christoph Lameterf619cfe2008-03-01 13:56:40 -08002909static void *kmalloc_large_node(size_t size, gfp_t flags, int node)
2910{
Vegard Nossumb1eeab62008-11-25 16:55:53 +01002911 struct page *page;
Catalin Marinase4f7c0b42009-07-07 10:32:59 +01002912 void *ptr = NULL;
Christoph Lameterf619cfe2008-03-01 13:56:40 -08002913
Vegard Nossumb1eeab62008-11-25 16:55:53 +01002914 flags |= __GFP_COMP | __GFP_NOTRACK;
2915 page = alloc_pages_node(node, flags, get_order(size));
Christoph Lameterf619cfe2008-03-01 13:56:40 -08002916 if (page)
Catalin Marinase4f7c0b42009-07-07 10:32:59 +01002917 ptr = page_address(page);
2918
2919 kmemleak_alloc(ptr, size, 1, flags);
2920 return ptr;
Christoph Lameterf619cfe2008-03-01 13:56:40 -08002921}
2922
Christoph Lameter81819f02007-05-06 14:49:36 -07002923void *__kmalloc_node(size_t size, gfp_t flags, int node)
2924{
Christoph Lameteraadb4bc2007-10-16 01:24:38 -07002925 struct kmem_cache *s;
Eduard - Gabriel Munteanu5b882be2008-08-19 20:43:26 +03002926 void *ret;
Christoph Lameter81819f02007-05-06 14:49:36 -07002927
Ingo Molnar057685c2009-02-20 12:15:30 +01002928 if (unlikely(size > SLUB_MAX_SIZE)) {
Eduard - Gabriel Munteanu5b882be2008-08-19 20:43:26 +03002929 ret = kmalloc_large_node(size, flags, node);
2930
Eduard - Gabriel Munteanuca2b84cb2009-03-23 15:12:24 +02002931 trace_kmalloc_node(_RET_IP_, ret,
2932 size, PAGE_SIZE << get_order(size),
2933 flags, node);
Eduard - Gabriel Munteanu5b882be2008-08-19 20:43:26 +03002934
2935 return ret;
2936 }
Christoph Lameteraadb4bc2007-10-16 01:24:38 -07002937
2938 s = get_slab(size, flags);
2939
2940 if (unlikely(ZERO_OR_NULL_PTR(s)))
Christoph Lameter6cb8f912007-07-17 04:03:22 -07002941 return s;
2942
Eduard - Gabriel Munteanu5b882be2008-08-19 20:43:26 +03002943 ret = slab_alloc(s, flags, node, _RET_IP_);
2944
Eduard - Gabriel Munteanuca2b84cb2009-03-23 15:12:24 +02002945 trace_kmalloc_node(_RET_IP_, ret, size, s->size, flags, node);
Eduard - Gabriel Munteanu5b882be2008-08-19 20:43:26 +03002946
2947 return ret;
Christoph Lameter81819f02007-05-06 14:49:36 -07002948}
2949EXPORT_SYMBOL(__kmalloc_node);
2950#endif
2951
2952size_t ksize(const void *object)
2953{
Christoph Lameter272c1d22007-06-08 13:46:49 -07002954 struct page *page;
Christoph Lameter81819f02007-05-06 14:49:36 -07002955
Christoph Lameteref8b4522007-10-16 01:24:46 -07002956 if (unlikely(object == ZERO_SIZE_PTR))
Christoph Lameter272c1d22007-06-08 13:46:49 -07002957 return 0;
2958
Vegard Nossum294a80a2007-12-04 23:45:30 -08002959 page = virt_to_head_page(object);
Vegard Nossum294a80a2007-12-04 23:45:30 -08002960
Pekka Enberg76994412008-05-22 19:22:25 +03002961 if (unlikely(!PageSlab(page))) {
2962 WARN_ON(!PageCompound(page));
Vegard Nossum294a80a2007-12-04 23:45:30 -08002963 return PAGE_SIZE << compound_order(page);
Pekka Enberg76994412008-05-22 19:22:25 +03002964 }
Christoph Lameter81819f02007-05-06 14:49:36 -07002965
Eric Dumazetb3d41882011-02-14 18:35:22 +01002966 return slab_ksize(page->slab);
Christoph Lameter81819f02007-05-06 14:49:36 -07002967}
Kirill A. Shutemovb1aabec2009-02-10 15:21:44 +02002968EXPORT_SYMBOL(ksize);
Christoph Lameter81819f02007-05-06 14:49:36 -07002969
2970void kfree(const void *x)
2971{
Christoph Lameter81819f02007-05-06 14:49:36 -07002972 struct page *page;
Christoph Lameter5bb983b2008-02-07 17:47:41 -08002973 void *object = (void *)x;
Christoph Lameter81819f02007-05-06 14:49:36 -07002974
Pekka Enberg2121db72009-03-25 11:05:57 +02002975 trace_kfree(_RET_IP_, x);
2976
Satyam Sharma2408c552007-10-16 01:24:44 -07002977 if (unlikely(ZERO_OR_NULL_PTR(x)))
Christoph Lameter81819f02007-05-06 14:49:36 -07002978 return;
2979
Christoph Lameterb49af682007-05-06 14:49:41 -07002980 page = virt_to_head_page(x);
Christoph Lameteraadb4bc2007-10-16 01:24:38 -07002981 if (unlikely(!PageSlab(page))) {
Christoph Lameter09375022008-05-28 10:32:22 -07002982 BUG_ON(!PageCompound(page));
Catalin Marinase4f7c0b42009-07-07 10:32:59 +01002983 kmemleak_free(x);
Christoph Lameteraadb4bc2007-10-16 01:24:38 -07002984 put_page(page);
2985 return;
2986 }
Eduard - Gabriel Munteanuce71e272008-08-19 20:43:25 +03002987 slab_free(page->slab, page, object, _RET_IP_);
Christoph Lameter81819f02007-05-06 14:49:36 -07002988}
2989EXPORT_SYMBOL(kfree);
2990
Christoph Lameter2086d262007-05-06 14:49:46 -07002991/*
Christoph Lameter672bba32007-05-09 02:32:39 -07002992 * kmem_cache_shrink removes empty slabs from the partial lists and sorts
2993 * the remaining slabs by the number of items in use. The slabs with the
2994 * most items in use come first. New allocations will then fill those up
2995 * and thus they can be removed from the partial lists.
2996 *
2997 * The slabs with the least items are placed last. This results in them
2998 * being allocated from last increasing the chance that the last objects
2999 * are freed in them.
Christoph Lameter2086d262007-05-06 14:49:46 -07003000 */
3001int kmem_cache_shrink(struct kmem_cache *s)
3002{
3003 int node;
3004 int i;
3005 struct kmem_cache_node *n;
3006 struct page *page;
3007 struct page *t;
Christoph Lameter205ab992008-04-14 19:11:40 +03003008 int objects = oo_objects(s->max);
Christoph Lameter2086d262007-05-06 14:49:46 -07003009 struct list_head *slabs_by_inuse =
Christoph Lameter834f3d12008-04-14 19:11:31 +03003010 kmalloc(sizeof(struct list_head) * objects, GFP_KERNEL);
Christoph Lameter2086d262007-05-06 14:49:46 -07003011 unsigned long flags;
3012
3013 if (!slabs_by_inuse)
3014 return -ENOMEM;
3015
3016 flush_all(s);
Christoph Lameterf64dc582007-10-16 01:25:33 -07003017 for_each_node_state(node, N_NORMAL_MEMORY) {
Christoph Lameter2086d262007-05-06 14:49:46 -07003018 n = get_node(s, node);
3019
3020 if (!n->nr_partial)
3021 continue;
3022
Christoph Lameter834f3d12008-04-14 19:11:31 +03003023 for (i = 0; i < objects; i++)
Christoph Lameter2086d262007-05-06 14:49:46 -07003024 INIT_LIST_HEAD(slabs_by_inuse + i);
3025
3026 spin_lock_irqsave(&n->list_lock, flags);
3027
3028 /*
Christoph Lameter672bba32007-05-09 02:32:39 -07003029 * Build lists indexed by the items in use in each slab.
Christoph Lameter2086d262007-05-06 14:49:46 -07003030 *
Christoph Lameter672bba32007-05-09 02:32:39 -07003031 * Note that concurrent frees may occur while we hold the
3032 * list_lock. page->inuse here is the upper limit.
Christoph Lameter2086d262007-05-06 14:49:46 -07003033 */
3034 list_for_each_entry_safe(page, t, &n->partial, lru) {
3035 if (!page->inuse && slab_trylock(page)) {
3036 /*
3037 * Must hold slab lock here because slab_free
3038 * may have freed the last object and be
3039 * waiting to release the slab.
3040 */
Christoph Lameter62e346a2010-09-28 08:10:28 -05003041 __remove_partial(n, page);
Christoph Lameter2086d262007-05-06 14:49:46 -07003042 slab_unlock(page);
3043 discard_slab(s, page);
3044 } else {
Christoph Lameterfcda3d82007-07-30 13:06:46 -07003045 list_move(&page->lru,
3046 slabs_by_inuse + page->inuse);
Christoph Lameter2086d262007-05-06 14:49:46 -07003047 }
3048 }
3049
Christoph Lameter2086d262007-05-06 14:49:46 -07003050 /*
Christoph Lameter672bba32007-05-09 02:32:39 -07003051 * Rebuild the partial list with the slabs filled up most
3052 * first and the least used slabs at the end.
Christoph Lameter2086d262007-05-06 14:49:46 -07003053 */
Christoph Lameter834f3d12008-04-14 19:11:31 +03003054 for (i = objects - 1; i >= 0; i--)
Christoph Lameter2086d262007-05-06 14:49:46 -07003055 list_splice(slabs_by_inuse + i, n->partial.prev);
3056
Christoph Lameter2086d262007-05-06 14:49:46 -07003057 spin_unlock_irqrestore(&n->list_lock, flags);
3058 }
3059
3060 kfree(slabs_by_inuse);
3061 return 0;
3062}
3063EXPORT_SYMBOL(kmem_cache_shrink);
3064
Pekka Enberg92a5bbc2010-10-06 16:58:16 +03003065#if defined(CONFIG_MEMORY_HOTPLUG)
Yasunori Gotob9049e22007-10-21 16:41:37 -07003066static int slab_mem_going_offline_callback(void *arg)
3067{
3068 struct kmem_cache *s;
3069
3070 down_read(&slub_lock);
3071 list_for_each_entry(s, &slab_caches, list)
3072 kmem_cache_shrink(s);
3073 up_read(&slub_lock);
3074
3075 return 0;
3076}
3077
3078static void slab_mem_offline_callback(void *arg)
3079{
3080 struct kmem_cache_node *n;
3081 struct kmem_cache *s;
3082 struct memory_notify *marg = arg;
3083 int offline_node;
3084
3085 offline_node = marg->status_change_nid;
3086
3087 /*
3088 * If the node still has available memory. we need kmem_cache_node
3089 * for it yet.
3090 */
3091 if (offline_node < 0)
3092 return;
3093
3094 down_read(&slub_lock);
3095 list_for_each_entry(s, &slab_caches, list) {
3096 n = get_node(s, offline_node);
3097 if (n) {
3098 /*
3099 * if n->nr_slabs > 0, slabs still exist on the node
3100 * that is going down. We were unable to free them,
Adam Buchbinderc9404c92009-12-18 15:40:42 -05003101 * and offline_pages() function shouldn't call this
Yasunori Gotob9049e22007-10-21 16:41:37 -07003102 * callback. So, we must fail.
3103 */
Christoph Lameter0f389ec2008-04-14 18:53:02 +03003104 BUG_ON(slabs_node(s, offline_node));
Yasunori Gotob9049e22007-10-21 16:41:37 -07003105
3106 s->node[offline_node] = NULL;
Christoph Lameter8de66a02010-08-25 14:51:14 -05003107 kmem_cache_free(kmem_cache_node, n);
Yasunori Gotob9049e22007-10-21 16:41:37 -07003108 }
3109 }
3110 up_read(&slub_lock);
3111}
3112
3113static int slab_mem_going_online_callback(void *arg)
3114{
3115 struct kmem_cache_node *n;
3116 struct kmem_cache *s;
3117 struct memory_notify *marg = arg;
3118 int nid = marg->status_change_nid;
3119 int ret = 0;
3120
3121 /*
3122 * If the node's memory is already available, then kmem_cache_node is
3123 * already created. Nothing to do.
3124 */
3125 if (nid < 0)
3126 return 0;
3127
3128 /*
Christoph Lameter0121c6192008-04-29 16:11:12 -07003129 * We are bringing a node online. No memory is available yet. We must
Yasunori Gotob9049e22007-10-21 16:41:37 -07003130 * allocate a kmem_cache_node structure in order to bring the node
3131 * online.
3132 */
3133 down_read(&slub_lock);
3134 list_for_each_entry(s, &slab_caches, list) {
3135 /*
3136 * XXX: kmem_cache_alloc_node will fallback to other nodes
3137 * since memory is not yet available from the node that
3138 * is brought up.
3139 */
Christoph Lameter8de66a02010-08-25 14:51:14 -05003140 n = kmem_cache_alloc(kmem_cache_node, GFP_KERNEL);
Yasunori Gotob9049e22007-10-21 16:41:37 -07003141 if (!n) {
3142 ret = -ENOMEM;
3143 goto out;
3144 }
Pekka Enberg5595cff2008-08-05 09:28:47 +03003145 init_kmem_cache_node(n, s);
Yasunori Gotob9049e22007-10-21 16:41:37 -07003146 s->node[nid] = n;
3147 }
3148out:
3149 up_read(&slub_lock);
3150 return ret;
3151}
3152
3153static int slab_memory_callback(struct notifier_block *self,
3154 unsigned long action, void *arg)
3155{
3156 int ret = 0;
3157
3158 switch (action) {
3159 case MEM_GOING_ONLINE:
3160 ret = slab_mem_going_online_callback(arg);
3161 break;
3162 case MEM_GOING_OFFLINE:
3163 ret = slab_mem_going_offline_callback(arg);
3164 break;
3165 case MEM_OFFLINE:
3166 case MEM_CANCEL_ONLINE:
3167 slab_mem_offline_callback(arg);
3168 break;
3169 case MEM_ONLINE:
3170 case MEM_CANCEL_OFFLINE:
3171 break;
3172 }
KAMEZAWA Hiroyukidc19f9d2008-12-01 13:13:48 -08003173 if (ret)
3174 ret = notifier_from_errno(ret);
3175 else
3176 ret = NOTIFY_OK;
Yasunori Gotob9049e22007-10-21 16:41:37 -07003177 return ret;
3178}
3179
3180#endif /* CONFIG_MEMORY_HOTPLUG */
3181
Christoph Lameter81819f02007-05-06 14:49:36 -07003182/********************************************************************
3183 * Basic setup of slabs
3184 *******************************************************************/
3185
Christoph Lameter51df1142010-08-20 12:37:15 -05003186/*
3187 * Used for early kmem_cache structures that were allocated using
3188 * the page allocator
3189 */
3190
3191static void __init kmem_cache_bootstrap_fixup(struct kmem_cache *s)
3192{
3193 int node;
3194
3195 list_add(&s->list, &slab_caches);
3196 s->refcount = -1;
3197
3198 for_each_node_state(node, N_NORMAL_MEMORY) {
3199 struct kmem_cache_node *n = get_node(s, node);
3200 struct page *p;
3201
3202 if (n) {
3203 list_for_each_entry(p, &n->partial, lru)
3204 p->slab = s;
3205
3206#ifdef CONFIG_SLAB_DEBUG
3207 list_for_each_entry(p, &n->full, lru)
3208 p->slab = s;
3209#endif
3210 }
3211 }
3212}
3213
Christoph Lameter81819f02007-05-06 14:49:36 -07003214void __init kmem_cache_init(void)
3215{
3216 int i;
Christoph Lameter4b356be2007-06-16 10:16:13 -07003217 int caches = 0;
Christoph Lameter51df1142010-08-20 12:37:15 -05003218 struct kmem_cache *temp_kmem_cache;
3219 int order;
Christoph Lameter51df1142010-08-20 12:37:15 -05003220 struct kmem_cache *temp_kmem_cache_node;
3221 unsigned long kmalloc_size;
3222
3223 kmem_size = offsetof(struct kmem_cache, node) +
3224 nr_node_ids * sizeof(struct kmem_cache_node *);
3225
3226 /* Allocate two kmem_caches from the page allocator */
3227 kmalloc_size = ALIGN(kmem_size, cache_line_size());
3228 order = get_order(2 * kmalloc_size);
3229 kmem_cache = (void *)__get_free_pages(GFP_NOWAIT, order);
3230
Christoph Lameter81819f02007-05-06 14:49:36 -07003231 /*
3232 * Must first have the slab cache available for the allocations of the
Christoph Lameter672bba32007-05-09 02:32:39 -07003233 * struct kmem_cache_node's. There is special bootstrap code in
Christoph Lameter81819f02007-05-06 14:49:36 -07003234 * kmem_cache_open for slab_state == DOWN.
3235 */
Christoph Lameter51df1142010-08-20 12:37:15 -05003236 kmem_cache_node = (void *)kmem_cache + kmalloc_size;
3237
3238 kmem_cache_open(kmem_cache_node, "kmem_cache_node",
3239 sizeof(struct kmem_cache_node),
3240 0, SLAB_HWCACHE_ALIGN | SLAB_PANIC, NULL);
Yasunori Gotob9049e22007-10-21 16:41:37 -07003241
Nadia Derbey0c40ba42008-04-29 01:00:41 -07003242 hotplug_memory_notifier(slab_memory_callback, SLAB_CALLBACK_PRI);
Christoph Lameter81819f02007-05-06 14:49:36 -07003243
3244 /* Able to allocate the per node structures */
3245 slab_state = PARTIAL;
3246
Christoph Lameter51df1142010-08-20 12:37:15 -05003247 temp_kmem_cache = kmem_cache;
3248 kmem_cache_open(kmem_cache, "kmem_cache", kmem_size,
3249 0, SLAB_HWCACHE_ALIGN | SLAB_PANIC, NULL);
3250 kmem_cache = kmem_cache_alloc(kmem_cache, GFP_NOWAIT);
3251 memcpy(kmem_cache, temp_kmem_cache, kmem_size);
Christoph Lameter81819f02007-05-06 14:49:36 -07003252
Christoph Lameter51df1142010-08-20 12:37:15 -05003253 /*
3254 * Allocate kmem_cache_node properly from the kmem_cache slab.
3255 * kmem_cache_node is separately allocated so no need to
3256 * update any list pointers.
3257 */
3258 temp_kmem_cache_node = kmem_cache_node;
Christoph Lameter81819f02007-05-06 14:49:36 -07003259
Christoph Lameter51df1142010-08-20 12:37:15 -05003260 kmem_cache_node = kmem_cache_alloc(kmem_cache, GFP_NOWAIT);
3261 memcpy(kmem_cache_node, temp_kmem_cache_node, kmem_size);
3262
3263 kmem_cache_bootstrap_fixup(kmem_cache_node);
3264
3265 caches++;
Christoph Lameter51df1142010-08-20 12:37:15 -05003266 kmem_cache_bootstrap_fixup(kmem_cache);
3267 caches++;
3268 /* Free temporary boot structure */
3269 free_pages((unsigned long)temp_kmem_cache, order);
3270
3271 /* Now we can use the kmem_cache to allocate kmalloc slabs */
Christoph Lameterf1b26332007-07-17 04:03:26 -07003272
3273 /*
3274 * Patch up the size_index table if we have strange large alignment
3275 * requirements for the kmalloc array. This is only the case for
Christoph Lameter6446faa2008-02-15 23:45:26 -08003276 * MIPS it seems. The standard arches will not generate any code here.
Christoph Lameterf1b26332007-07-17 04:03:26 -07003277 *
3278 * Largest permitted alignment is 256 bytes due to the way we
3279 * handle the index determination for the smaller caches.
3280 *
3281 * Make sure that nothing crazy happens if someone starts tinkering
3282 * around with ARCH_KMALLOC_MINALIGN
3283 */
3284 BUILD_BUG_ON(KMALLOC_MIN_SIZE > 256 ||
3285 (KMALLOC_MIN_SIZE & (KMALLOC_MIN_SIZE - 1)));
3286
Aaro Koskinenacdfcd02009-08-28 14:28:54 +03003287 for (i = 8; i < KMALLOC_MIN_SIZE; i += 8) {
3288 int elem = size_index_elem(i);
3289 if (elem >= ARRAY_SIZE(size_index))
3290 break;
3291 size_index[elem] = KMALLOC_SHIFT_LOW;
3292 }
Christoph Lameterf1b26332007-07-17 04:03:26 -07003293
Aaro Koskinenacdfcd02009-08-28 14:28:54 +03003294 if (KMALLOC_MIN_SIZE == 64) {
3295 /*
3296 * The 96 byte size cache is not used if the alignment
3297 * is 64 byte.
3298 */
3299 for (i = 64 + 8; i <= 96; i += 8)
3300 size_index[size_index_elem(i)] = 7;
3301 } else if (KMALLOC_MIN_SIZE == 128) {
Christoph Lameter41d54d32008-07-03 09:14:26 -05003302 /*
3303 * The 192 byte sized cache is not used if the alignment
3304 * is 128 byte. Redirect kmalloc to use the 256 byte cache
3305 * instead.
3306 */
3307 for (i = 128 + 8; i <= 192; i += 8)
Aaro Koskinenacdfcd02009-08-28 14:28:54 +03003308 size_index[size_index_elem(i)] = 8;
Christoph Lameter41d54d32008-07-03 09:14:26 -05003309 }
3310
Christoph Lameter51df1142010-08-20 12:37:15 -05003311 /* Caches that are not of the two-to-the-power-of size */
3312 if (KMALLOC_MIN_SIZE <= 32) {
3313 kmalloc_caches[1] = create_kmalloc_cache("kmalloc-96", 96, 0);
3314 caches++;
3315 }
3316
3317 if (KMALLOC_MIN_SIZE <= 64) {
3318 kmalloc_caches[2] = create_kmalloc_cache("kmalloc-192", 192, 0);
3319 caches++;
3320 }
3321
3322 for (i = KMALLOC_SHIFT_LOW; i < SLUB_PAGE_SHIFT; i++) {
3323 kmalloc_caches[i] = create_kmalloc_cache("kmalloc", 1 << i, 0);
3324 caches++;
3325 }
3326
Christoph Lameter81819f02007-05-06 14:49:36 -07003327 slab_state = UP;
3328
3329 /* Provide the correct kmalloc names now that the caches are up */
Pekka Enberg84c1cf62010-09-14 23:21:12 +03003330 if (KMALLOC_MIN_SIZE <= 32) {
3331 kmalloc_caches[1]->name = kstrdup(kmalloc_caches[1]->name, GFP_NOWAIT);
3332 BUG_ON(!kmalloc_caches[1]->name);
3333 }
3334
3335 if (KMALLOC_MIN_SIZE <= 64) {
3336 kmalloc_caches[2]->name = kstrdup(kmalloc_caches[2]->name, GFP_NOWAIT);
3337 BUG_ON(!kmalloc_caches[2]->name);
3338 }
3339
Christoph Lameterd7278bd2010-07-09 14:07:12 -05003340 for (i = KMALLOC_SHIFT_LOW; i < SLUB_PAGE_SHIFT; i++) {
3341 char *s = kasprintf(GFP_NOWAIT, "kmalloc-%d", 1 << i);
3342
3343 BUG_ON(!s);
Christoph Lameter51df1142010-08-20 12:37:15 -05003344 kmalloc_caches[i]->name = s;
Christoph Lameterd7278bd2010-07-09 14:07:12 -05003345 }
Christoph Lameter81819f02007-05-06 14:49:36 -07003346
3347#ifdef CONFIG_SMP
3348 register_cpu_notifier(&slab_notifier);
Christoph Lameter9dfc6e62009-12-18 16:26:20 -06003349#endif
Christoph Lameter81819f02007-05-06 14:49:36 -07003350
Christoph Lameter55136592010-08-20 12:37:13 -05003351#ifdef CONFIG_ZONE_DMA
Christoph Lameter51df1142010-08-20 12:37:15 -05003352 for (i = 0; i < SLUB_PAGE_SHIFT; i++) {
3353 struct kmem_cache *s = kmalloc_caches[i];
Christoph Lameter55136592010-08-20 12:37:13 -05003354
Christoph Lameter51df1142010-08-20 12:37:15 -05003355 if (s && s->size) {
Christoph Lameter55136592010-08-20 12:37:13 -05003356 char *name = kasprintf(GFP_NOWAIT,
3357 "dma-kmalloc-%d", s->objsize);
3358
3359 BUG_ON(!name);
Christoph Lameter51df1142010-08-20 12:37:15 -05003360 kmalloc_dma_caches[i] = create_kmalloc_cache(name,
3361 s->objsize, SLAB_CACHE_DMA);
Christoph Lameter55136592010-08-20 12:37:13 -05003362 }
3363 }
3364#endif
Ingo Molnar3adbefe2008-02-05 17:57:39 -08003365 printk(KERN_INFO
3366 "SLUB: Genslabs=%d, HWalign=%d, Order=%d-%d, MinObjects=%d,"
Christoph Lameter4b356be2007-06-16 10:16:13 -07003367 " CPUs=%d, Nodes=%d\n",
3368 caches, cache_line_size(),
Christoph Lameter81819f02007-05-06 14:49:36 -07003369 slub_min_order, slub_max_order, slub_min_objects,
3370 nr_cpu_ids, nr_node_ids);
3371}
3372
Pekka Enberg7e85ee02009-06-12 14:03:06 +03003373void __init kmem_cache_init_late(void)
3374{
Pekka Enberg7e85ee02009-06-12 14:03:06 +03003375}
3376
Christoph Lameter81819f02007-05-06 14:49:36 -07003377/*
3378 * Find a mergeable slab cache
3379 */
3380static int slab_unmergeable(struct kmem_cache *s)
3381{
3382 if (slub_nomerge || (s->flags & SLUB_NEVER_MERGE))
3383 return 1;
3384
Christoph Lameterc59def92007-05-16 22:10:50 -07003385 if (s->ctor)
Christoph Lameter81819f02007-05-06 14:49:36 -07003386 return 1;
3387
Christoph Lameter8ffa6872007-05-31 00:40:51 -07003388 /*
3389 * We may have set a slab to be unmergeable during bootstrap.
3390 */
3391 if (s->refcount < 0)
3392 return 1;
3393
Christoph Lameter81819f02007-05-06 14:49:36 -07003394 return 0;
3395}
3396
3397static struct kmem_cache *find_mergeable(size_t size,
Christoph Lameterba0268a2007-09-11 15:24:11 -07003398 size_t align, unsigned long flags, const char *name,
Alexey Dobriyan51cc5062008-07-25 19:45:34 -07003399 void (*ctor)(void *))
Christoph Lameter81819f02007-05-06 14:49:36 -07003400{
Christoph Lameter5b95a4ac2007-07-17 04:03:19 -07003401 struct kmem_cache *s;
Christoph Lameter81819f02007-05-06 14:49:36 -07003402
3403 if (slub_nomerge || (flags & SLUB_NEVER_MERGE))
3404 return NULL;
3405
Christoph Lameterc59def92007-05-16 22:10:50 -07003406 if (ctor)
Christoph Lameter81819f02007-05-06 14:49:36 -07003407 return NULL;
3408
3409 size = ALIGN(size, sizeof(void *));
3410 align = calculate_alignment(flags, align, size);
3411 size = ALIGN(size, align);
Christoph Lameterba0268a2007-09-11 15:24:11 -07003412 flags = kmem_cache_flags(size, flags, name, NULL);
Christoph Lameter81819f02007-05-06 14:49:36 -07003413
Christoph Lameter5b95a4ac2007-07-17 04:03:19 -07003414 list_for_each_entry(s, &slab_caches, list) {
Christoph Lameter81819f02007-05-06 14:49:36 -07003415 if (slab_unmergeable(s))
3416 continue;
3417
3418 if (size > s->size)
3419 continue;
3420
Christoph Lameterba0268a2007-09-11 15:24:11 -07003421 if ((flags & SLUB_MERGE_SAME) != (s->flags & SLUB_MERGE_SAME))
Christoph Lameter81819f02007-05-06 14:49:36 -07003422 continue;
3423 /*
3424 * Check if alignment is compatible.
3425 * Courtesy of Adrian Drzewiecki
3426 */
Pekka Enberg06428782008-01-07 23:20:27 -08003427 if ((s->size & ~(align - 1)) != s->size)
Christoph Lameter81819f02007-05-06 14:49:36 -07003428 continue;
3429
3430 if (s->size - size >= sizeof(void *))
3431 continue;
3432
3433 return s;
3434 }
3435 return NULL;
3436}
3437
3438struct kmem_cache *kmem_cache_create(const char *name, size_t size,
Alexey Dobriyan51cc5062008-07-25 19:45:34 -07003439 size_t align, unsigned long flags, void (*ctor)(void *))
Christoph Lameter81819f02007-05-06 14:49:36 -07003440{
3441 struct kmem_cache *s;
Pekka Enberg84c1cf62010-09-14 23:21:12 +03003442 char *n;
Christoph Lameter81819f02007-05-06 14:49:36 -07003443
Benjamin Herrenschmidtfe1ff492009-09-21 17:02:30 -07003444 if (WARN_ON(!name))
3445 return NULL;
3446
Christoph Lameter81819f02007-05-06 14:49:36 -07003447 down_write(&slub_lock);
Christoph Lameterba0268a2007-09-11 15:24:11 -07003448 s = find_mergeable(size, align, flags, name, ctor);
Christoph Lameter81819f02007-05-06 14:49:36 -07003449 if (s) {
3450 s->refcount++;
3451 /*
3452 * Adjust the object sizes so that we clear
3453 * the complete object on kzalloc.
3454 */
3455 s->objsize = max(s->objsize, (int)size);
3456 s->inuse = max_t(int, s->inuse, ALIGN(size, sizeof(void *)));
Christoph Lameter6446faa2008-02-15 23:45:26 -08003457
David Rientjes7b8f3b62008-12-17 22:09:46 -08003458 if (sysfs_slab_alias(s, name)) {
David Rientjes7b8f3b62008-12-17 22:09:46 -08003459 s->refcount--;
Christoph Lameter81819f02007-05-06 14:49:36 -07003460 goto err;
David Rientjes7b8f3b62008-12-17 22:09:46 -08003461 }
Christoph Lameter2bce6482010-07-19 11:39:11 -05003462 up_write(&slub_lock);
Christoph Lametera0e1d1b2007-07-17 04:03:31 -07003463 return s;
3464 }
Christoph Lameter6446faa2008-02-15 23:45:26 -08003465
Pekka Enberg84c1cf62010-09-14 23:21:12 +03003466 n = kstrdup(name, GFP_KERNEL);
3467 if (!n)
3468 goto err;
3469
Christoph Lametera0e1d1b2007-07-17 04:03:31 -07003470 s = kmalloc(kmem_size, GFP_KERNEL);
3471 if (s) {
Pekka Enberg84c1cf62010-09-14 23:21:12 +03003472 if (kmem_cache_open(s, n,
Christoph Lameterc59def92007-05-16 22:10:50 -07003473 size, align, flags, ctor)) {
Christoph Lameter81819f02007-05-06 14:49:36 -07003474 list_add(&s->list, &slab_caches);
David Rientjes7b8f3b62008-12-17 22:09:46 -08003475 if (sysfs_slab_add(s)) {
David Rientjes7b8f3b62008-12-17 22:09:46 -08003476 list_del(&s->list);
Pekka Enberg84c1cf62010-09-14 23:21:12 +03003477 kfree(n);
David Rientjes7b8f3b62008-12-17 22:09:46 -08003478 kfree(s);
Christoph Lametera0e1d1b2007-07-17 04:03:31 -07003479 goto err;
David Rientjes7b8f3b62008-12-17 22:09:46 -08003480 }
Christoph Lameter2bce6482010-07-19 11:39:11 -05003481 up_write(&slub_lock);
Christoph Lametera0e1d1b2007-07-17 04:03:31 -07003482 return s;
3483 }
Pekka Enberg84c1cf62010-09-14 23:21:12 +03003484 kfree(n);
Christoph Lametera0e1d1b2007-07-17 04:03:31 -07003485 kfree(s);
Christoph Lameter81819f02007-05-06 14:49:36 -07003486 }
Pavel Emelyanov68cee4f12010-10-28 13:50:37 +04003487err:
Christoph Lameter81819f02007-05-06 14:49:36 -07003488 up_write(&slub_lock);
Christoph Lameter81819f02007-05-06 14:49:36 -07003489
Christoph Lameter81819f02007-05-06 14:49:36 -07003490 if (flags & SLAB_PANIC)
3491 panic("Cannot create slabcache %s\n", name);
3492 else
3493 s = NULL;
3494 return s;
3495}
3496EXPORT_SYMBOL(kmem_cache_create);
3497
Christoph Lameter81819f02007-05-06 14:49:36 -07003498#ifdef CONFIG_SMP
Christoph Lameter27390bc2007-06-01 00:47:09 -07003499/*
Christoph Lameter672bba32007-05-09 02:32:39 -07003500 * Use the cpu notifier to insure that the cpu slabs are flushed when
3501 * necessary.
Christoph Lameter81819f02007-05-06 14:49:36 -07003502 */
3503static int __cpuinit slab_cpuup_callback(struct notifier_block *nfb,
3504 unsigned long action, void *hcpu)
3505{
3506 long cpu = (long)hcpu;
Christoph Lameter5b95a4ac2007-07-17 04:03:19 -07003507 struct kmem_cache *s;
3508 unsigned long flags;
Christoph Lameter81819f02007-05-06 14:49:36 -07003509
3510 switch (action) {
3511 case CPU_UP_CANCELED:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07003512 case CPU_UP_CANCELED_FROZEN:
Christoph Lameter81819f02007-05-06 14:49:36 -07003513 case CPU_DEAD:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07003514 case CPU_DEAD_FROZEN:
Christoph Lameter5b95a4ac2007-07-17 04:03:19 -07003515 down_read(&slub_lock);
3516 list_for_each_entry(s, &slab_caches, list) {
3517 local_irq_save(flags);
3518 __flush_cpu_slab(s, cpu);
3519 local_irq_restore(flags);
3520 }
3521 up_read(&slub_lock);
Christoph Lameter81819f02007-05-06 14:49:36 -07003522 break;
3523 default:
3524 break;
3525 }
3526 return NOTIFY_OK;
3527}
3528
Pekka Enberg06428782008-01-07 23:20:27 -08003529static struct notifier_block __cpuinitdata slab_notifier = {
Ingo Molnar3adbefe2008-02-05 17:57:39 -08003530 .notifier_call = slab_cpuup_callback
Pekka Enberg06428782008-01-07 23:20:27 -08003531};
Christoph Lameter81819f02007-05-06 14:49:36 -07003532
3533#endif
3534
Eduard - Gabriel Munteanuce71e272008-08-19 20:43:25 +03003535void *__kmalloc_track_caller(size_t size, gfp_t gfpflags, unsigned long caller)
Christoph Lameter81819f02007-05-06 14:49:36 -07003536{
Christoph Lameteraadb4bc2007-10-16 01:24:38 -07003537 struct kmem_cache *s;
Eduard - Gabriel Munteanu94b528d2008-08-24 20:49:35 +03003538 void *ret;
Christoph Lameteraadb4bc2007-10-16 01:24:38 -07003539
Christoph Lameterffadd4d2009-02-17 12:05:07 -05003540 if (unlikely(size > SLUB_MAX_SIZE))
Pekka Enbergeada35e2008-02-11 22:47:46 +02003541 return kmalloc_large(size, gfpflags);
3542
Christoph Lameteraadb4bc2007-10-16 01:24:38 -07003543 s = get_slab(size, gfpflags);
Christoph Lameter81819f02007-05-06 14:49:36 -07003544
Satyam Sharma2408c552007-10-16 01:24:44 -07003545 if (unlikely(ZERO_OR_NULL_PTR(s)))
Christoph Lameter6cb8f912007-07-17 04:03:22 -07003546 return s;
Christoph Lameter81819f02007-05-06 14:49:36 -07003547
Christoph Lameter2154a332010-07-09 14:07:10 -05003548 ret = slab_alloc(s, gfpflags, NUMA_NO_NODE, caller);
Eduard - Gabriel Munteanu94b528d2008-08-24 20:49:35 +03003549
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003550 /* Honor the call site pointer we received. */
Eduard - Gabriel Munteanuca2b84cb2009-03-23 15:12:24 +02003551 trace_kmalloc(caller, ret, size, s->size, gfpflags);
Eduard - Gabriel Munteanu94b528d2008-08-24 20:49:35 +03003552
3553 return ret;
Christoph Lameter81819f02007-05-06 14:49:36 -07003554}
3555
Namhyung Kim5d1f57e2010-09-29 21:02:15 +09003556#ifdef CONFIG_NUMA
Christoph Lameter81819f02007-05-06 14:49:36 -07003557void *__kmalloc_node_track_caller(size_t size, gfp_t gfpflags,
Eduard - Gabriel Munteanuce71e272008-08-19 20:43:25 +03003558 int node, unsigned long caller)
Christoph Lameter81819f02007-05-06 14:49:36 -07003559{
Christoph Lameteraadb4bc2007-10-16 01:24:38 -07003560 struct kmem_cache *s;
Eduard - Gabriel Munteanu94b528d2008-08-24 20:49:35 +03003561 void *ret;
Christoph Lameteraadb4bc2007-10-16 01:24:38 -07003562
Xiaotian Fengd3e14aa2010-04-08 17:26:44 +08003563 if (unlikely(size > SLUB_MAX_SIZE)) {
3564 ret = kmalloc_large_node(size, gfpflags, node);
3565
3566 trace_kmalloc_node(caller, ret,
3567 size, PAGE_SIZE << get_order(size),
3568 gfpflags, node);
3569
3570 return ret;
3571 }
Pekka Enbergeada35e2008-02-11 22:47:46 +02003572
Christoph Lameteraadb4bc2007-10-16 01:24:38 -07003573 s = get_slab(size, gfpflags);
Christoph Lameter81819f02007-05-06 14:49:36 -07003574
Satyam Sharma2408c552007-10-16 01:24:44 -07003575 if (unlikely(ZERO_OR_NULL_PTR(s)))
Christoph Lameter6cb8f912007-07-17 04:03:22 -07003576 return s;
Christoph Lameter81819f02007-05-06 14:49:36 -07003577
Eduard - Gabriel Munteanu94b528d2008-08-24 20:49:35 +03003578 ret = slab_alloc(s, gfpflags, node, caller);
3579
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003580 /* Honor the call site pointer we received. */
Eduard - Gabriel Munteanuca2b84cb2009-03-23 15:12:24 +02003581 trace_kmalloc_node(caller, ret, size, s->size, gfpflags, node);
Eduard - Gabriel Munteanu94b528d2008-08-24 20:49:35 +03003582
3583 return ret;
Christoph Lameter81819f02007-05-06 14:49:36 -07003584}
Namhyung Kim5d1f57e2010-09-29 21:02:15 +09003585#endif
Christoph Lameter81819f02007-05-06 14:49:36 -07003586
Christoph Lameterab4d5ed2010-10-05 13:57:26 -05003587#ifdef CONFIG_SYSFS
Christoph Lameter205ab992008-04-14 19:11:40 +03003588static int count_inuse(struct page *page)
3589{
3590 return page->inuse;
3591}
3592
3593static int count_total(struct page *page)
3594{
3595 return page->objects;
3596}
Christoph Lameterab4d5ed2010-10-05 13:57:26 -05003597#endif
Christoph Lameter205ab992008-04-14 19:11:40 +03003598
Christoph Lameterab4d5ed2010-10-05 13:57:26 -05003599#ifdef CONFIG_SLUB_DEBUG
Christoph Lameter434e2452007-07-17 04:03:30 -07003600static int validate_slab(struct kmem_cache *s, struct page *page,
3601 unsigned long *map)
Christoph Lameter53e15af2007-05-06 14:49:43 -07003602{
3603 void *p;
Christoph Lametera973e9d2008-03-01 13:40:44 -08003604 void *addr = page_address(page);
Christoph Lameter53e15af2007-05-06 14:49:43 -07003605
3606 if (!check_slab(s, page) ||
3607 !on_freelist(s, page, NULL))
3608 return 0;
3609
3610 /* Now we know that a valid freelist exists */
Christoph Lameter39b26462008-04-14 19:11:30 +03003611 bitmap_zero(map, page->objects);
Christoph Lameter53e15af2007-05-06 14:49:43 -07003612
Christoph Lameter7656c722007-05-09 02:32:40 -07003613 for_each_free_object(p, s, page->freelist) {
3614 set_bit(slab_index(p, s, addr), map);
Tero Roponen37d57442010-12-01 20:04:20 +02003615 if (!check_object(s, page, p, SLUB_RED_INACTIVE))
Christoph Lameter53e15af2007-05-06 14:49:43 -07003616 return 0;
3617 }
3618
Christoph Lameter224a88b2008-04-14 19:11:31 +03003619 for_each_object(p, s, addr, page->objects)
Christoph Lameter7656c722007-05-09 02:32:40 -07003620 if (!test_bit(slab_index(p, s, addr), map))
Tero Roponen37d57442010-12-01 20:04:20 +02003621 if (!check_object(s, page, p, SLUB_RED_ACTIVE))
Christoph Lameter53e15af2007-05-06 14:49:43 -07003622 return 0;
3623 return 1;
3624}
3625
Christoph Lameter434e2452007-07-17 04:03:30 -07003626static void validate_slab_slab(struct kmem_cache *s, struct page *page,
3627 unsigned long *map)
Christoph Lameter53e15af2007-05-06 14:49:43 -07003628{
3629 if (slab_trylock(page)) {
Christoph Lameter434e2452007-07-17 04:03:30 -07003630 validate_slab(s, page, map);
Christoph Lameter53e15af2007-05-06 14:49:43 -07003631 slab_unlock(page);
3632 } else
3633 printk(KERN_INFO "SLUB %s: Skipped busy slab 0x%p\n",
3634 s->name, page);
Christoph Lameter53e15af2007-05-06 14:49:43 -07003635}
3636
Christoph Lameter434e2452007-07-17 04:03:30 -07003637static int validate_slab_node(struct kmem_cache *s,
3638 struct kmem_cache_node *n, unsigned long *map)
Christoph Lameter53e15af2007-05-06 14:49:43 -07003639{
3640 unsigned long count = 0;
3641 struct page *page;
3642 unsigned long flags;
3643
3644 spin_lock_irqsave(&n->list_lock, flags);
3645
3646 list_for_each_entry(page, &n->partial, lru) {
Christoph Lameter434e2452007-07-17 04:03:30 -07003647 validate_slab_slab(s, page, map);
Christoph Lameter53e15af2007-05-06 14:49:43 -07003648 count++;
3649 }
3650 if (count != n->nr_partial)
3651 printk(KERN_ERR "SLUB %s: %ld partial slabs counted but "
3652 "counter=%ld\n", s->name, count, n->nr_partial);
3653
3654 if (!(s->flags & SLAB_STORE_USER))
3655 goto out;
3656
3657 list_for_each_entry(page, &n->full, lru) {
Christoph Lameter434e2452007-07-17 04:03:30 -07003658 validate_slab_slab(s, page, map);
Christoph Lameter53e15af2007-05-06 14:49:43 -07003659 count++;
3660 }
3661 if (count != atomic_long_read(&n->nr_slabs))
3662 printk(KERN_ERR "SLUB: %s %ld slabs counted but "
3663 "counter=%ld\n", s->name, count,
3664 atomic_long_read(&n->nr_slabs));
3665
3666out:
3667 spin_unlock_irqrestore(&n->list_lock, flags);
3668 return count;
3669}
3670
Christoph Lameter434e2452007-07-17 04:03:30 -07003671static long validate_slab_cache(struct kmem_cache *s)
Christoph Lameter53e15af2007-05-06 14:49:43 -07003672{
3673 int node;
3674 unsigned long count = 0;
Christoph Lameter205ab992008-04-14 19:11:40 +03003675 unsigned long *map = kmalloc(BITS_TO_LONGS(oo_objects(s->max)) *
Christoph Lameter434e2452007-07-17 04:03:30 -07003676 sizeof(unsigned long), GFP_KERNEL);
3677
3678 if (!map)
3679 return -ENOMEM;
Christoph Lameter53e15af2007-05-06 14:49:43 -07003680
3681 flush_all(s);
Christoph Lameterf64dc582007-10-16 01:25:33 -07003682 for_each_node_state(node, N_NORMAL_MEMORY) {
Christoph Lameter53e15af2007-05-06 14:49:43 -07003683 struct kmem_cache_node *n = get_node(s, node);
3684
Christoph Lameter434e2452007-07-17 04:03:30 -07003685 count += validate_slab_node(s, n, map);
Christoph Lameter53e15af2007-05-06 14:49:43 -07003686 }
Christoph Lameter434e2452007-07-17 04:03:30 -07003687 kfree(map);
Christoph Lameter53e15af2007-05-06 14:49:43 -07003688 return count;
3689}
Christoph Lameter88a420e2007-05-06 14:49:45 -07003690/*
Christoph Lameter672bba32007-05-09 02:32:39 -07003691 * Generate lists of code addresses where slabcache objects are allocated
Christoph Lameter88a420e2007-05-06 14:49:45 -07003692 * and freed.
3693 */
3694
3695struct location {
3696 unsigned long count;
Eduard - Gabriel Munteanuce71e272008-08-19 20:43:25 +03003697 unsigned long addr;
Christoph Lameter45edfa52007-05-09 02:32:45 -07003698 long long sum_time;
3699 long min_time;
3700 long max_time;
3701 long min_pid;
3702 long max_pid;
Rusty Russell174596a2009-01-01 10:12:29 +10303703 DECLARE_BITMAP(cpus, NR_CPUS);
Christoph Lameter45edfa52007-05-09 02:32:45 -07003704 nodemask_t nodes;
Christoph Lameter88a420e2007-05-06 14:49:45 -07003705};
3706
3707struct loc_track {
3708 unsigned long max;
3709 unsigned long count;
3710 struct location *loc;
3711};
3712
3713static void free_loc_track(struct loc_track *t)
3714{
3715 if (t->max)
3716 free_pages((unsigned long)t->loc,
3717 get_order(sizeof(struct location) * t->max));
3718}
3719
Christoph Lameter68dff6a2007-07-17 04:03:20 -07003720static int alloc_loc_track(struct loc_track *t, unsigned long max, gfp_t flags)
Christoph Lameter88a420e2007-05-06 14:49:45 -07003721{
3722 struct location *l;
3723 int order;
3724
Christoph Lameter88a420e2007-05-06 14:49:45 -07003725 order = get_order(sizeof(struct location) * max);
3726
Christoph Lameter68dff6a2007-07-17 04:03:20 -07003727 l = (void *)__get_free_pages(flags, order);
Christoph Lameter88a420e2007-05-06 14:49:45 -07003728 if (!l)
3729 return 0;
3730
3731 if (t->count) {
3732 memcpy(l, t->loc, sizeof(struct location) * t->count);
3733 free_loc_track(t);
3734 }
3735 t->max = max;
3736 t->loc = l;
3737 return 1;
3738}
3739
3740static int add_location(struct loc_track *t, struct kmem_cache *s,
Christoph Lameter45edfa52007-05-09 02:32:45 -07003741 const struct track *track)
Christoph Lameter88a420e2007-05-06 14:49:45 -07003742{
3743 long start, end, pos;
3744 struct location *l;
Eduard - Gabriel Munteanuce71e272008-08-19 20:43:25 +03003745 unsigned long caddr;
Christoph Lameter45edfa52007-05-09 02:32:45 -07003746 unsigned long age = jiffies - track->when;
Christoph Lameter88a420e2007-05-06 14:49:45 -07003747
3748 start = -1;
3749 end = t->count;
3750
3751 for ( ; ; ) {
3752 pos = start + (end - start + 1) / 2;
3753
3754 /*
3755 * There is nothing at "end". If we end up there
3756 * we need to add something to before end.
3757 */
3758 if (pos == end)
3759 break;
3760
3761 caddr = t->loc[pos].addr;
Christoph Lameter45edfa52007-05-09 02:32:45 -07003762 if (track->addr == caddr) {
3763
3764 l = &t->loc[pos];
3765 l->count++;
3766 if (track->when) {
3767 l->sum_time += age;
3768 if (age < l->min_time)
3769 l->min_time = age;
3770 if (age > l->max_time)
3771 l->max_time = age;
3772
3773 if (track->pid < l->min_pid)
3774 l->min_pid = track->pid;
3775 if (track->pid > l->max_pid)
3776 l->max_pid = track->pid;
3777
Rusty Russell174596a2009-01-01 10:12:29 +10303778 cpumask_set_cpu(track->cpu,
3779 to_cpumask(l->cpus));
Christoph Lameter45edfa52007-05-09 02:32:45 -07003780 }
3781 node_set(page_to_nid(virt_to_page(track)), l->nodes);
Christoph Lameter88a420e2007-05-06 14:49:45 -07003782 return 1;
3783 }
3784
Christoph Lameter45edfa52007-05-09 02:32:45 -07003785 if (track->addr < caddr)
Christoph Lameter88a420e2007-05-06 14:49:45 -07003786 end = pos;
3787 else
3788 start = pos;
3789 }
3790
3791 /*
Christoph Lameter672bba32007-05-09 02:32:39 -07003792 * Not found. Insert new tracking element.
Christoph Lameter88a420e2007-05-06 14:49:45 -07003793 */
Christoph Lameter68dff6a2007-07-17 04:03:20 -07003794 if (t->count >= t->max && !alloc_loc_track(t, 2 * t->max, GFP_ATOMIC))
Christoph Lameter88a420e2007-05-06 14:49:45 -07003795 return 0;
3796
3797 l = t->loc + pos;
3798 if (pos < t->count)
3799 memmove(l + 1, l,
3800 (t->count - pos) * sizeof(struct location));
3801 t->count++;
3802 l->count = 1;
Christoph Lameter45edfa52007-05-09 02:32:45 -07003803 l->addr = track->addr;
3804 l->sum_time = age;
3805 l->min_time = age;
3806 l->max_time = age;
3807 l->min_pid = track->pid;
3808 l->max_pid = track->pid;
Rusty Russell174596a2009-01-01 10:12:29 +10303809 cpumask_clear(to_cpumask(l->cpus));
3810 cpumask_set_cpu(track->cpu, to_cpumask(l->cpus));
Christoph Lameter45edfa52007-05-09 02:32:45 -07003811 nodes_clear(l->nodes);
3812 node_set(page_to_nid(virt_to_page(track)), l->nodes);
Christoph Lameter88a420e2007-05-06 14:49:45 -07003813 return 1;
3814}
3815
3816static void process_slab(struct loc_track *t, struct kmem_cache *s,
Eric Dumazetbbd7d572010-03-24 22:25:47 +01003817 struct page *page, enum track_item alloc,
Namhyung Kima5dd5c12010-09-29 21:02:13 +09003818 unsigned long *map)
Christoph Lameter88a420e2007-05-06 14:49:45 -07003819{
Christoph Lametera973e9d2008-03-01 13:40:44 -08003820 void *addr = page_address(page);
Christoph Lameter88a420e2007-05-06 14:49:45 -07003821 void *p;
3822
Christoph Lameter39b26462008-04-14 19:11:30 +03003823 bitmap_zero(map, page->objects);
Christoph Lameter7656c722007-05-09 02:32:40 -07003824 for_each_free_object(p, s, page->freelist)
3825 set_bit(slab_index(p, s, addr), map);
Christoph Lameter88a420e2007-05-06 14:49:45 -07003826
Christoph Lameter224a88b2008-04-14 19:11:31 +03003827 for_each_object(p, s, addr, page->objects)
Christoph Lameter45edfa52007-05-09 02:32:45 -07003828 if (!test_bit(slab_index(p, s, addr), map))
3829 add_location(t, s, get_track(s, p, alloc));
Christoph Lameter88a420e2007-05-06 14:49:45 -07003830}
3831
3832static int list_locations(struct kmem_cache *s, char *buf,
3833 enum track_item alloc)
3834{
Harvey Harrisone374d482008-01-31 15:20:50 -08003835 int len = 0;
Christoph Lameter88a420e2007-05-06 14:49:45 -07003836 unsigned long i;
Christoph Lameter68dff6a2007-07-17 04:03:20 -07003837 struct loc_track t = { 0, 0, NULL };
Christoph Lameter88a420e2007-05-06 14:49:45 -07003838 int node;
Eric Dumazetbbd7d572010-03-24 22:25:47 +01003839 unsigned long *map = kmalloc(BITS_TO_LONGS(oo_objects(s->max)) *
3840 sizeof(unsigned long), GFP_KERNEL);
Christoph Lameter88a420e2007-05-06 14:49:45 -07003841
Eric Dumazetbbd7d572010-03-24 22:25:47 +01003842 if (!map || !alloc_loc_track(&t, PAGE_SIZE / sizeof(struct location),
3843 GFP_TEMPORARY)) {
3844 kfree(map);
Christoph Lameter68dff6a2007-07-17 04:03:20 -07003845 return sprintf(buf, "Out of memory\n");
Eric Dumazetbbd7d572010-03-24 22:25:47 +01003846 }
Christoph Lameter88a420e2007-05-06 14:49:45 -07003847 /* Push back cpu slabs */
3848 flush_all(s);
3849
Christoph Lameterf64dc582007-10-16 01:25:33 -07003850 for_each_node_state(node, N_NORMAL_MEMORY) {
Christoph Lameter88a420e2007-05-06 14:49:45 -07003851 struct kmem_cache_node *n = get_node(s, node);
3852 unsigned long flags;
3853 struct page *page;
3854
Christoph Lameter9e869432007-08-22 14:01:56 -07003855 if (!atomic_long_read(&n->nr_slabs))
Christoph Lameter88a420e2007-05-06 14:49:45 -07003856 continue;
3857
3858 spin_lock_irqsave(&n->list_lock, flags);
3859 list_for_each_entry(page, &n->partial, lru)
Eric Dumazetbbd7d572010-03-24 22:25:47 +01003860 process_slab(&t, s, page, alloc, map);
Christoph Lameter88a420e2007-05-06 14:49:45 -07003861 list_for_each_entry(page, &n->full, lru)
Eric Dumazetbbd7d572010-03-24 22:25:47 +01003862 process_slab(&t, s, page, alloc, map);
Christoph Lameter88a420e2007-05-06 14:49:45 -07003863 spin_unlock_irqrestore(&n->list_lock, flags);
3864 }
3865
3866 for (i = 0; i < t.count; i++) {
Christoph Lameter45edfa52007-05-09 02:32:45 -07003867 struct location *l = &t.loc[i];
Christoph Lameter88a420e2007-05-06 14:49:45 -07003868
Hugh Dickins9c246242008-12-09 13:14:27 -08003869 if (len > PAGE_SIZE - KSYM_SYMBOL_LEN - 100)
Christoph Lameter88a420e2007-05-06 14:49:45 -07003870 break;
Harvey Harrisone374d482008-01-31 15:20:50 -08003871 len += sprintf(buf + len, "%7ld ", l->count);
Christoph Lameter45edfa52007-05-09 02:32:45 -07003872
3873 if (l->addr)
Joe Perches62c70bc2011-01-13 15:45:52 -08003874 len += sprintf(buf + len, "%pS", (void *)l->addr);
Christoph Lameter88a420e2007-05-06 14:49:45 -07003875 else
Harvey Harrisone374d482008-01-31 15:20:50 -08003876 len += sprintf(buf + len, "<not-available>");
Christoph Lameter45edfa52007-05-09 02:32:45 -07003877
3878 if (l->sum_time != l->min_time) {
Harvey Harrisone374d482008-01-31 15:20:50 -08003879 len += sprintf(buf + len, " age=%ld/%ld/%ld",
Roman Zippelf8bd2252008-05-01 04:34:31 -07003880 l->min_time,
3881 (long)div_u64(l->sum_time, l->count),
3882 l->max_time);
Christoph Lameter45edfa52007-05-09 02:32:45 -07003883 } else
Harvey Harrisone374d482008-01-31 15:20:50 -08003884 len += sprintf(buf + len, " age=%ld",
Christoph Lameter45edfa52007-05-09 02:32:45 -07003885 l->min_time);
3886
3887 if (l->min_pid != l->max_pid)
Harvey Harrisone374d482008-01-31 15:20:50 -08003888 len += sprintf(buf + len, " pid=%ld-%ld",
Christoph Lameter45edfa52007-05-09 02:32:45 -07003889 l->min_pid, l->max_pid);
3890 else
Harvey Harrisone374d482008-01-31 15:20:50 -08003891 len += sprintf(buf + len, " pid=%ld",
Christoph Lameter45edfa52007-05-09 02:32:45 -07003892 l->min_pid);
3893
Rusty Russell174596a2009-01-01 10:12:29 +10303894 if (num_online_cpus() > 1 &&
3895 !cpumask_empty(to_cpumask(l->cpus)) &&
Harvey Harrisone374d482008-01-31 15:20:50 -08003896 len < PAGE_SIZE - 60) {
3897 len += sprintf(buf + len, " cpus=");
3898 len += cpulist_scnprintf(buf + len, PAGE_SIZE - len - 50,
Rusty Russell174596a2009-01-01 10:12:29 +10303899 to_cpumask(l->cpus));
Christoph Lameter45edfa52007-05-09 02:32:45 -07003900 }
3901
Christoph Lameter62bc62a2009-06-16 15:32:15 -07003902 if (nr_online_nodes > 1 && !nodes_empty(l->nodes) &&
Harvey Harrisone374d482008-01-31 15:20:50 -08003903 len < PAGE_SIZE - 60) {
3904 len += sprintf(buf + len, " nodes=");
3905 len += nodelist_scnprintf(buf + len, PAGE_SIZE - len - 50,
Christoph Lameter45edfa52007-05-09 02:32:45 -07003906 l->nodes);
3907 }
3908
Harvey Harrisone374d482008-01-31 15:20:50 -08003909 len += sprintf(buf + len, "\n");
Christoph Lameter88a420e2007-05-06 14:49:45 -07003910 }
3911
3912 free_loc_track(&t);
Eric Dumazetbbd7d572010-03-24 22:25:47 +01003913 kfree(map);
Christoph Lameter88a420e2007-05-06 14:49:45 -07003914 if (!t.count)
Harvey Harrisone374d482008-01-31 15:20:50 -08003915 len += sprintf(buf, "No data\n");
3916 return len;
Christoph Lameter88a420e2007-05-06 14:49:45 -07003917}
Christoph Lameterab4d5ed2010-10-05 13:57:26 -05003918#endif
Christoph Lameter88a420e2007-05-06 14:49:45 -07003919
Christoph Lametera5a84752010-10-05 13:57:27 -05003920#ifdef SLUB_RESILIENCY_TEST
3921static void resiliency_test(void)
3922{
3923 u8 *p;
3924
3925 BUILD_BUG_ON(KMALLOC_MIN_SIZE > 16 || SLUB_PAGE_SHIFT < 10);
3926
3927 printk(KERN_ERR "SLUB resiliency testing\n");
3928 printk(KERN_ERR "-----------------------\n");
3929 printk(KERN_ERR "A. Corruption after allocation\n");
3930
3931 p = kzalloc(16, GFP_KERNEL);
3932 p[16] = 0x12;
3933 printk(KERN_ERR "\n1. kmalloc-16: Clobber Redzone/next pointer"
3934 " 0x12->0x%p\n\n", p + 16);
3935
3936 validate_slab_cache(kmalloc_caches[4]);
3937
3938 /* Hmmm... The next two are dangerous */
3939 p = kzalloc(32, GFP_KERNEL);
3940 p[32 + sizeof(void *)] = 0x34;
3941 printk(KERN_ERR "\n2. kmalloc-32: Clobber next pointer/next slab"
3942 " 0x34 -> -0x%p\n", p);
3943 printk(KERN_ERR
3944 "If allocated object is overwritten then not detectable\n\n");
3945
3946 validate_slab_cache(kmalloc_caches[5]);
3947 p = kzalloc(64, GFP_KERNEL);
3948 p += 64 + (get_cycles() & 0xff) * sizeof(void *);
3949 *p = 0x56;
3950 printk(KERN_ERR "\n3. kmalloc-64: corrupting random byte 0x56->0x%p\n",
3951 p);
3952 printk(KERN_ERR
3953 "If allocated object is overwritten then not detectable\n\n");
3954 validate_slab_cache(kmalloc_caches[6]);
3955
3956 printk(KERN_ERR "\nB. Corruption after free\n");
3957 p = kzalloc(128, GFP_KERNEL);
3958 kfree(p);
3959 *p = 0x78;
3960 printk(KERN_ERR "1. kmalloc-128: Clobber first word 0x78->0x%p\n\n", p);
3961 validate_slab_cache(kmalloc_caches[7]);
3962
3963 p = kzalloc(256, GFP_KERNEL);
3964 kfree(p);
3965 p[50] = 0x9a;
3966 printk(KERN_ERR "\n2. kmalloc-256: Clobber 50th byte 0x9a->0x%p\n\n",
3967 p);
3968 validate_slab_cache(kmalloc_caches[8]);
3969
3970 p = kzalloc(512, GFP_KERNEL);
3971 kfree(p);
3972 p[512] = 0xab;
3973 printk(KERN_ERR "\n3. kmalloc-512: Clobber redzone 0xab->0x%p\n\n", p);
3974 validate_slab_cache(kmalloc_caches[9]);
3975}
3976#else
3977#ifdef CONFIG_SYSFS
3978static void resiliency_test(void) {};
3979#endif
3980#endif
3981
Christoph Lameterab4d5ed2010-10-05 13:57:26 -05003982#ifdef CONFIG_SYSFS
Christoph Lameter81819f02007-05-06 14:49:36 -07003983enum slab_stat_type {
Christoph Lameter205ab992008-04-14 19:11:40 +03003984 SL_ALL, /* All slabs */
3985 SL_PARTIAL, /* Only partially allocated slabs */
3986 SL_CPU, /* Only slabs used for cpu caches */
3987 SL_OBJECTS, /* Determine allocated objects not slabs */
3988 SL_TOTAL /* Determine object capacity not slabs */
Christoph Lameter81819f02007-05-06 14:49:36 -07003989};
3990
Christoph Lameter205ab992008-04-14 19:11:40 +03003991#define SO_ALL (1 << SL_ALL)
Christoph Lameter81819f02007-05-06 14:49:36 -07003992#define SO_PARTIAL (1 << SL_PARTIAL)
3993#define SO_CPU (1 << SL_CPU)
3994#define SO_OBJECTS (1 << SL_OBJECTS)
Christoph Lameter205ab992008-04-14 19:11:40 +03003995#define SO_TOTAL (1 << SL_TOTAL)
Christoph Lameter81819f02007-05-06 14:49:36 -07003996
Cyrill Gorcunov62e5c4b2008-03-02 23:28:24 +03003997static ssize_t show_slab_objects(struct kmem_cache *s,
3998 char *buf, unsigned long flags)
Christoph Lameter81819f02007-05-06 14:49:36 -07003999{
4000 unsigned long total = 0;
Christoph Lameter81819f02007-05-06 14:49:36 -07004001 int node;
4002 int x;
4003 unsigned long *nodes;
4004 unsigned long *per_cpu;
4005
4006 nodes = kzalloc(2 * sizeof(unsigned long) * nr_node_ids, GFP_KERNEL);
Cyrill Gorcunov62e5c4b2008-03-02 23:28:24 +03004007 if (!nodes)
4008 return -ENOMEM;
Christoph Lameter81819f02007-05-06 14:49:36 -07004009 per_cpu = nodes + nr_node_ids;
4010
Christoph Lameter205ab992008-04-14 19:11:40 +03004011 if (flags & SO_CPU) {
4012 int cpu;
Christoph Lameter81819f02007-05-06 14:49:36 -07004013
Christoph Lameter205ab992008-04-14 19:11:40 +03004014 for_each_possible_cpu(cpu) {
Christoph Lameter9dfc6e62009-12-18 16:26:20 -06004015 struct kmem_cache_cpu *c = per_cpu_ptr(s->cpu_slab, cpu);
Christoph Lameterdfb4f092007-10-16 01:26:05 -07004016
Christoph Lameter205ab992008-04-14 19:11:40 +03004017 if (!c || c->node < 0)
4018 continue;
4019
4020 if (c->page) {
4021 if (flags & SO_TOTAL)
4022 x = c->page->objects;
4023 else if (flags & SO_OBJECTS)
4024 x = c->page->inuse;
Christoph Lameter81819f02007-05-06 14:49:36 -07004025 else
4026 x = 1;
Christoph Lameter205ab992008-04-14 19:11:40 +03004027
Christoph Lameter81819f02007-05-06 14:49:36 -07004028 total += x;
Christoph Lameter205ab992008-04-14 19:11:40 +03004029 nodes[c->node] += x;
Christoph Lameter81819f02007-05-06 14:49:36 -07004030 }
Christoph Lameter205ab992008-04-14 19:11:40 +03004031 per_cpu[c->node]++;
Christoph Lameter81819f02007-05-06 14:49:36 -07004032 }
4033 }
4034
Christoph Lameter04d94872011-01-10 10:15:15 -06004035 lock_memory_hotplug();
Christoph Lameterab4d5ed2010-10-05 13:57:26 -05004036#ifdef CONFIG_SLUB_DEBUG
Christoph Lameter205ab992008-04-14 19:11:40 +03004037 if (flags & SO_ALL) {
4038 for_each_node_state(node, N_NORMAL_MEMORY) {
4039 struct kmem_cache_node *n = get_node(s, node);
Christoph Lameter81819f02007-05-06 14:49:36 -07004040
Christoph Lameter205ab992008-04-14 19:11:40 +03004041 if (flags & SO_TOTAL)
4042 x = atomic_long_read(&n->total_objects);
4043 else if (flags & SO_OBJECTS)
4044 x = atomic_long_read(&n->total_objects) -
4045 count_partial(n, count_free);
4046
4047 else
4048 x = atomic_long_read(&n->nr_slabs);
4049 total += x;
4050 nodes[node] += x;
4051 }
4052
Christoph Lameterab4d5ed2010-10-05 13:57:26 -05004053 } else
4054#endif
4055 if (flags & SO_PARTIAL) {
Christoph Lameter205ab992008-04-14 19:11:40 +03004056 for_each_node_state(node, N_NORMAL_MEMORY) {
4057 struct kmem_cache_node *n = get_node(s, node);
4058
4059 if (flags & SO_TOTAL)
4060 x = count_partial(n, count_total);
4061 else if (flags & SO_OBJECTS)
4062 x = count_partial(n, count_inuse);
Christoph Lameter81819f02007-05-06 14:49:36 -07004063 else
4064 x = n->nr_partial;
4065 total += x;
4066 nodes[node] += x;
4067 }
Christoph Lameter81819f02007-05-06 14:49:36 -07004068 }
Christoph Lameter81819f02007-05-06 14:49:36 -07004069 x = sprintf(buf, "%lu", total);
4070#ifdef CONFIG_NUMA
Christoph Lameterf64dc582007-10-16 01:25:33 -07004071 for_each_node_state(node, N_NORMAL_MEMORY)
Christoph Lameter81819f02007-05-06 14:49:36 -07004072 if (nodes[node])
4073 x += sprintf(buf + x, " N%d=%lu",
4074 node, nodes[node]);
4075#endif
Christoph Lameter04d94872011-01-10 10:15:15 -06004076 unlock_memory_hotplug();
Christoph Lameter81819f02007-05-06 14:49:36 -07004077 kfree(nodes);
4078 return x + sprintf(buf + x, "\n");
4079}
4080
Christoph Lameterab4d5ed2010-10-05 13:57:26 -05004081#ifdef CONFIG_SLUB_DEBUG
Christoph Lameter81819f02007-05-06 14:49:36 -07004082static int any_slab_objects(struct kmem_cache *s)
4083{
4084 int node;
Christoph Lameterdfb4f092007-10-16 01:26:05 -07004085
4086 for_each_online_node(node) {
Christoph Lameter81819f02007-05-06 14:49:36 -07004087 struct kmem_cache_node *n = get_node(s, node);
4088
Christoph Lameterdfb4f092007-10-16 01:26:05 -07004089 if (!n)
4090 continue;
4091
Benjamin Herrenschmidt4ea33e22008-05-06 20:42:39 -07004092 if (atomic_long_read(&n->total_objects))
Christoph Lameter81819f02007-05-06 14:49:36 -07004093 return 1;
4094 }
4095 return 0;
4096}
Christoph Lameterab4d5ed2010-10-05 13:57:26 -05004097#endif
Christoph Lameter81819f02007-05-06 14:49:36 -07004098
4099#define to_slab_attr(n) container_of(n, struct slab_attribute, attr)
4100#define to_slab(n) container_of(n, struct kmem_cache, kobj);
4101
4102struct slab_attribute {
4103 struct attribute attr;
4104 ssize_t (*show)(struct kmem_cache *s, char *buf);
4105 ssize_t (*store)(struct kmem_cache *s, const char *x, size_t count);
4106};
4107
4108#define SLAB_ATTR_RO(_name) \
4109 static struct slab_attribute _name##_attr = __ATTR_RO(_name)
4110
4111#define SLAB_ATTR(_name) \
4112 static struct slab_attribute _name##_attr = \
4113 __ATTR(_name, 0644, _name##_show, _name##_store)
4114
Christoph Lameter81819f02007-05-06 14:49:36 -07004115static ssize_t slab_size_show(struct kmem_cache *s, char *buf)
4116{
4117 return sprintf(buf, "%d\n", s->size);
4118}
4119SLAB_ATTR_RO(slab_size);
4120
4121static ssize_t align_show(struct kmem_cache *s, char *buf)
4122{
4123 return sprintf(buf, "%d\n", s->align);
4124}
4125SLAB_ATTR_RO(align);
4126
4127static ssize_t object_size_show(struct kmem_cache *s, char *buf)
4128{
4129 return sprintf(buf, "%d\n", s->objsize);
4130}
4131SLAB_ATTR_RO(object_size);
4132
4133static ssize_t objs_per_slab_show(struct kmem_cache *s, char *buf)
4134{
Christoph Lameter834f3d12008-04-14 19:11:31 +03004135 return sprintf(buf, "%d\n", oo_objects(s->oo));
Christoph Lameter81819f02007-05-06 14:49:36 -07004136}
4137SLAB_ATTR_RO(objs_per_slab);
4138
Christoph Lameter06b285d2008-04-14 19:11:41 +03004139static ssize_t order_store(struct kmem_cache *s,
4140 const char *buf, size_t length)
4141{
Christoph Lameter0121c6192008-04-29 16:11:12 -07004142 unsigned long order;
4143 int err;
4144
4145 err = strict_strtoul(buf, 10, &order);
4146 if (err)
4147 return err;
Christoph Lameter06b285d2008-04-14 19:11:41 +03004148
4149 if (order > slub_max_order || order < slub_min_order)
4150 return -EINVAL;
4151
4152 calculate_sizes(s, order);
4153 return length;
4154}
4155
Christoph Lameter81819f02007-05-06 14:49:36 -07004156static ssize_t order_show(struct kmem_cache *s, char *buf)
4157{
Christoph Lameter834f3d12008-04-14 19:11:31 +03004158 return sprintf(buf, "%d\n", oo_order(s->oo));
Christoph Lameter81819f02007-05-06 14:49:36 -07004159}
Christoph Lameter06b285d2008-04-14 19:11:41 +03004160SLAB_ATTR(order);
Christoph Lameter81819f02007-05-06 14:49:36 -07004161
David Rientjes73d342b2009-02-22 17:40:09 -08004162static ssize_t min_partial_show(struct kmem_cache *s, char *buf)
4163{
4164 return sprintf(buf, "%lu\n", s->min_partial);
4165}
4166
4167static ssize_t min_partial_store(struct kmem_cache *s, const char *buf,
4168 size_t length)
4169{
4170 unsigned long min;
4171 int err;
4172
4173 err = strict_strtoul(buf, 10, &min);
4174 if (err)
4175 return err;
4176
David Rientjesc0bdb232009-02-25 09:16:35 +02004177 set_min_partial(s, min);
David Rientjes73d342b2009-02-22 17:40:09 -08004178 return length;
4179}
4180SLAB_ATTR(min_partial);
4181
Christoph Lameter81819f02007-05-06 14:49:36 -07004182static ssize_t ctor_show(struct kmem_cache *s, char *buf)
4183{
Joe Perches62c70bc2011-01-13 15:45:52 -08004184 if (!s->ctor)
4185 return 0;
4186 return sprintf(buf, "%pS\n", s->ctor);
Christoph Lameter81819f02007-05-06 14:49:36 -07004187}
4188SLAB_ATTR_RO(ctor);
4189
Christoph Lameter81819f02007-05-06 14:49:36 -07004190static ssize_t aliases_show(struct kmem_cache *s, char *buf)
4191{
4192 return sprintf(buf, "%d\n", s->refcount - 1);
4193}
4194SLAB_ATTR_RO(aliases);
4195
Christoph Lameter81819f02007-05-06 14:49:36 -07004196static ssize_t partial_show(struct kmem_cache *s, char *buf)
4197{
Christoph Lameterd9acf4b2008-02-15 15:22:21 -08004198 return show_slab_objects(s, buf, SO_PARTIAL);
Christoph Lameter81819f02007-05-06 14:49:36 -07004199}
4200SLAB_ATTR_RO(partial);
4201
4202static ssize_t cpu_slabs_show(struct kmem_cache *s, char *buf)
4203{
Christoph Lameterd9acf4b2008-02-15 15:22:21 -08004204 return show_slab_objects(s, buf, SO_CPU);
Christoph Lameter81819f02007-05-06 14:49:36 -07004205}
4206SLAB_ATTR_RO(cpu_slabs);
4207
4208static ssize_t objects_show(struct kmem_cache *s, char *buf)
4209{
Christoph Lameter205ab992008-04-14 19:11:40 +03004210 return show_slab_objects(s, buf, SO_ALL|SO_OBJECTS);
Christoph Lameter81819f02007-05-06 14:49:36 -07004211}
4212SLAB_ATTR_RO(objects);
4213
Christoph Lameter205ab992008-04-14 19:11:40 +03004214static ssize_t objects_partial_show(struct kmem_cache *s, char *buf)
4215{
4216 return show_slab_objects(s, buf, SO_PARTIAL|SO_OBJECTS);
4217}
4218SLAB_ATTR_RO(objects_partial);
4219
Christoph Lameter81819f02007-05-06 14:49:36 -07004220static ssize_t reclaim_account_show(struct kmem_cache *s, char *buf)
4221{
4222 return sprintf(buf, "%d\n", !!(s->flags & SLAB_RECLAIM_ACCOUNT));
4223}
4224
4225static ssize_t reclaim_account_store(struct kmem_cache *s,
4226 const char *buf, size_t length)
4227{
4228 s->flags &= ~SLAB_RECLAIM_ACCOUNT;
4229 if (buf[0] == '1')
4230 s->flags |= SLAB_RECLAIM_ACCOUNT;
4231 return length;
4232}
4233SLAB_ATTR(reclaim_account);
4234
4235static ssize_t hwcache_align_show(struct kmem_cache *s, char *buf)
4236{
Christoph Lameter5af60832007-05-06 14:49:56 -07004237 return sprintf(buf, "%d\n", !!(s->flags & SLAB_HWCACHE_ALIGN));
Christoph Lameter81819f02007-05-06 14:49:36 -07004238}
4239SLAB_ATTR_RO(hwcache_align);
4240
4241#ifdef CONFIG_ZONE_DMA
4242static ssize_t cache_dma_show(struct kmem_cache *s, char *buf)
4243{
4244 return sprintf(buf, "%d\n", !!(s->flags & SLAB_CACHE_DMA));
4245}
4246SLAB_ATTR_RO(cache_dma);
4247#endif
4248
4249static ssize_t destroy_by_rcu_show(struct kmem_cache *s, char *buf)
4250{
4251 return sprintf(buf, "%d\n", !!(s->flags & SLAB_DESTROY_BY_RCU));
4252}
4253SLAB_ATTR_RO(destroy_by_rcu);
4254
Lai Jiangshanab9a0f12011-03-10 15:21:48 +08004255static ssize_t reserved_show(struct kmem_cache *s, char *buf)
4256{
4257 return sprintf(buf, "%d\n", s->reserved);
4258}
4259SLAB_ATTR_RO(reserved);
4260
Christoph Lameterab4d5ed2010-10-05 13:57:26 -05004261#ifdef CONFIG_SLUB_DEBUG
Christoph Lametera5a84752010-10-05 13:57:27 -05004262static ssize_t slabs_show(struct kmem_cache *s, char *buf)
4263{
4264 return show_slab_objects(s, buf, SO_ALL);
4265}
4266SLAB_ATTR_RO(slabs);
4267
4268static ssize_t total_objects_show(struct kmem_cache *s, char *buf)
4269{
4270 return show_slab_objects(s, buf, SO_ALL|SO_TOTAL);
4271}
4272SLAB_ATTR_RO(total_objects);
4273
4274static ssize_t sanity_checks_show(struct kmem_cache *s, char *buf)
4275{
4276 return sprintf(buf, "%d\n", !!(s->flags & SLAB_DEBUG_FREE));
4277}
4278
4279static ssize_t sanity_checks_store(struct kmem_cache *s,
4280 const char *buf, size_t length)
4281{
4282 s->flags &= ~SLAB_DEBUG_FREE;
4283 if (buf[0] == '1')
4284 s->flags |= SLAB_DEBUG_FREE;
4285 return length;
4286}
4287SLAB_ATTR(sanity_checks);
4288
4289static ssize_t trace_show(struct kmem_cache *s, char *buf)
4290{
4291 return sprintf(buf, "%d\n", !!(s->flags & SLAB_TRACE));
4292}
4293
4294static ssize_t trace_store(struct kmem_cache *s, const char *buf,
4295 size_t length)
4296{
4297 s->flags &= ~SLAB_TRACE;
4298 if (buf[0] == '1')
4299 s->flags |= SLAB_TRACE;
4300 return length;
4301}
4302SLAB_ATTR(trace);
4303
Christoph Lameter81819f02007-05-06 14:49:36 -07004304static ssize_t red_zone_show(struct kmem_cache *s, char *buf)
4305{
4306 return sprintf(buf, "%d\n", !!(s->flags & SLAB_RED_ZONE));
4307}
4308
4309static ssize_t red_zone_store(struct kmem_cache *s,
4310 const char *buf, size_t length)
4311{
4312 if (any_slab_objects(s))
4313 return -EBUSY;
4314
4315 s->flags &= ~SLAB_RED_ZONE;
4316 if (buf[0] == '1')
4317 s->flags |= SLAB_RED_ZONE;
Christoph Lameter06b285d2008-04-14 19:11:41 +03004318 calculate_sizes(s, -1);
Christoph Lameter81819f02007-05-06 14:49:36 -07004319 return length;
4320}
4321SLAB_ATTR(red_zone);
4322
4323static ssize_t poison_show(struct kmem_cache *s, char *buf)
4324{
4325 return sprintf(buf, "%d\n", !!(s->flags & SLAB_POISON));
4326}
4327
4328static ssize_t poison_store(struct kmem_cache *s,
4329 const char *buf, size_t length)
4330{
4331 if (any_slab_objects(s))
4332 return -EBUSY;
4333
4334 s->flags &= ~SLAB_POISON;
4335 if (buf[0] == '1')
4336 s->flags |= SLAB_POISON;
Christoph Lameter06b285d2008-04-14 19:11:41 +03004337 calculate_sizes(s, -1);
Christoph Lameter81819f02007-05-06 14:49:36 -07004338 return length;
4339}
4340SLAB_ATTR(poison);
4341
4342static ssize_t store_user_show(struct kmem_cache *s, char *buf)
4343{
4344 return sprintf(buf, "%d\n", !!(s->flags & SLAB_STORE_USER));
4345}
4346
4347static ssize_t store_user_store(struct kmem_cache *s,
4348 const char *buf, size_t length)
4349{
4350 if (any_slab_objects(s))
4351 return -EBUSY;
4352
4353 s->flags &= ~SLAB_STORE_USER;
4354 if (buf[0] == '1')
4355 s->flags |= SLAB_STORE_USER;
Christoph Lameter06b285d2008-04-14 19:11:41 +03004356 calculate_sizes(s, -1);
Christoph Lameter81819f02007-05-06 14:49:36 -07004357 return length;
4358}
4359SLAB_ATTR(store_user);
4360
Christoph Lameter53e15af2007-05-06 14:49:43 -07004361static ssize_t validate_show(struct kmem_cache *s, char *buf)
4362{
4363 return 0;
4364}
4365
4366static ssize_t validate_store(struct kmem_cache *s,
4367 const char *buf, size_t length)
4368{
Christoph Lameter434e2452007-07-17 04:03:30 -07004369 int ret = -EINVAL;
4370
4371 if (buf[0] == '1') {
4372 ret = validate_slab_cache(s);
4373 if (ret >= 0)
4374 ret = length;
4375 }
4376 return ret;
Christoph Lameter53e15af2007-05-06 14:49:43 -07004377}
4378SLAB_ATTR(validate);
Christoph Lametera5a84752010-10-05 13:57:27 -05004379
4380static ssize_t alloc_calls_show(struct kmem_cache *s, char *buf)
4381{
4382 if (!(s->flags & SLAB_STORE_USER))
4383 return -ENOSYS;
4384 return list_locations(s, buf, TRACK_ALLOC);
4385}
4386SLAB_ATTR_RO(alloc_calls);
4387
4388static ssize_t free_calls_show(struct kmem_cache *s, char *buf)
4389{
4390 if (!(s->flags & SLAB_STORE_USER))
4391 return -ENOSYS;
4392 return list_locations(s, buf, TRACK_FREE);
4393}
4394SLAB_ATTR_RO(free_calls);
4395#endif /* CONFIG_SLUB_DEBUG */
4396
4397#ifdef CONFIG_FAILSLAB
4398static ssize_t failslab_show(struct kmem_cache *s, char *buf)
4399{
4400 return sprintf(buf, "%d\n", !!(s->flags & SLAB_FAILSLAB));
4401}
4402
4403static ssize_t failslab_store(struct kmem_cache *s, const char *buf,
4404 size_t length)
4405{
4406 s->flags &= ~SLAB_FAILSLAB;
4407 if (buf[0] == '1')
4408 s->flags |= SLAB_FAILSLAB;
4409 return length;
4410}
4411SLAB_ATTR(failslab);
Christoph Lameterab4d5ed2010-10-05 13:57:26 -05004412#endif
Christoph Lameter53e15af2007-05-06 14:49:43 -07004413
Christoph Lameter2086d262007-05-06 14:49:46 -07004414static ssize_t shrink_show(struct kmem_cache *s, char *buf)
4415{
4416 return 0;
4417}
4418
4419static ssize_t shrink_store(struct kmem_cache *s,
4420 const char *buf, size_t length)
4421{
4422 if (buf[0] == '1') {
4423 int rc = kmem_cache_shrink(s);
4424
4425 if (rc)
4426 return rc;
4427 } else
4428 return -EINVAL;
4429 return length;
4430}
4431SLAB_ATTR(shrink);
4432
Christoph Lameter81819f02007-05-06 14:49:36 -07004433#ifdef CONFIG_NUMA
Christoph Lameter98246012008-01-07 23:20:26 -08004434static ssize_t remote_node_defrag_ratio_show(struct kmem_cache *s, char *buf)
Christoph Lameter81819f02007-05-06 14:49:36 -07004435{
Christoph Lameter98246012008-01-07 23:20:26 -08004436 return sprintf(buf, "%d\n", s->remote_node_defrag_ratio / 10);
Christoph Lameter81819f02007-05-06 14:49:36 -07004437}
4438
Christoph Lameter98246012008-01-07 23:20:26 -08004439static ssize_t remote_node_defrag_ratio_store(struct kmem_cache *s,
Christoph Lameter81819f02007-05-06 14:49:36 -07004440 const char *buf, size_t length)
4441{
Christoph Lameter0121c6192008-04-29 16:11:12 -07004442 unsigned long ratio;
4443 int err;
Christoph Lameter81819f02007-05-06 14:49:36 -07004444
Christoph Lameter0121c6192008-04-29 16:11:12 -07004445 err = strict_strtoul(buf, 10, &ratio);
4446 if (err)
4447 return err;
4448
Christoph Lametere2cb96b2008-08-19 08:51:22 -05004449 if (ratio <= 100)
Christoph Lameter0121c6192008-04-29 16:11:12 -07004450 s->remote_node_defrag_ratio = ratio * 10;
4451
Christoph Lameter81819f02007-05-06 14:49:36 -07004452 return length;
4453}
Christoph Lameter98246012008-01-07 23:20:26 -08004454SLAB_ATTR(remote_node_defrag_ratio);
Christoph Lameter81819f02007-05-06 14:49:36 -07004455#endif
4456
Christoph Lameter8ff12cf2008-02-07 17:47:41 -08004457#ifdef CONFIG_SLUB_STATS
Christoph Lameter8ff12cf2008-02-07 17:47:41 -08004458static int show_stat(struct kmem_cache *s, char *buf, enum stat_item si)
4459{
4460 unsigned long sum = 0;
4461 int cpu;
4462 int len;
4463 int *data = kmalloc(nr_cpu_ids * sizeof(int), GFP_KERNEL);
4464
4465 if (!data)
4466 return -ENOMEM;
4467
4468 for_each_online_cpu(cpu) {
Christoph Lameter9dfc6e62009-12-18 16:26:20 -06004469 unsigned x = per_cpu_ptr(s->cpu_slab, cpu)->stat[si];
Christoph Lameter8ff12cf2008-02-07 17:47:41 -08004470
4471 data[cpu] = x;
4472 sum += x;
4473 }
4474
4475 len = sprintf(buf, "%lu", sum);
4476
Christoph Lameter50ef37b2008-04-14 18:52:05 +03004477#ifdef CONFIG_SMP
Christoph Lameter8ff12cf2008-02-07 17:47:41 -08004478 for_each_online_cpu(cpu) {
4479 if (data[cpu] && len < PAGE_SIZE - 20)
Christoph Lameter50ef37b2008-04-14 18:52:05 +03004480 len += sprintf(buf + len, " C%d=%u", cpu, data[cpu]);
Christoph Lameter8ff12cf2008-02-07 17:47:41 -08004481 }
Christoph Lameter50ef37b2008-04-14 18:52:05 +03004482#endif
Christoph Lameter8ff12cf2008-02-07 17:47:41 -08004483 kfree(data);
4484 return len + sprintf(buf + len, "\n");
4485}
4486
David Rientjes78eb00c2009-10-15 02:20:22 -07004487static void clear_stat(struct kmem_cache *s, enum stat_item si)
4488{
4489 int cpu;
4490
4491 for_each_online_cpu(cpu)
Christoph Lameter9dfc6e62009-12-18 16:26:20 -06004492 per_cpu_ptr(s->cpu_slab, cpu)->stat[si] = 0;
David Rientjes78eb00c2009-10-15 02:20:22 -07004493}
4494
Christoph Lameter8ff12cf2008-02-07 17:47:41 -08004495#define STAT_ATTR(si, text) \
4496static ssize_t text##_show(struct kmem_cache *s, char *buf) \
4497{ \
4498 return show_stat(s, buf, si); \
4499} \
David Rientjes78eb00c2009-10-15 02:20:22 -07004500static ssize_t text##_store(struct kmem_cache *s, \
4501 const char *buf, size_t length) \
4502{ \
4503 if (buf[0] != '0') \
4504 return -EINVAL; \
4505 clear_stat(s, si); \
4506 return length; \
4507} \
4508SLAB_ATTR(text); \
Christoph Lameter8ff12cf2008-02-07 17:47:41 -08004509
4510STAT_ATTR(ALLOC_FASTPATH, alloc_fastpath);
4511STAT_ATTR(ALLOC_SLOWPATH, alloc_slowpath);
4512STAT_ATTR(FREE_FASTPATH, free_fastpath);
4513STAT_ATTR(FREE_SLOWPATH, free_slowpath);
4514STAT_ATTR(FREE_FROZEN, free_frozen);
4515STAT_ATTR(FREE_ADD_PARTIAL, free_add_partial);
4516STAT_ATTR(FREE_REMOVE_PARTIAL, free_remove_partial);
4517STAT_ATTR(ALLOC_FROM_PARTIAL, alloc_from_partial);
4518STAT_ATTR(ALLOC_SLAB, alloc_slab);
4519STAT_ATTR(ALLOC_REFILL, alloc_refill);
4520STAT_ATTR(FREE_SLAB, free_slab);
4521STAT_ATTR(CPUSLAB_FLUSH, cpuslab_flush);
4522STAT_ATTR(DEACTIVATE_FULL, deactivate_full);
4523STAT_ATTR(DEACTIVATE_EMPTY, deactivate_empty);
4524STAT_ATTR(DEACTIVATE_TO_HEAD, deactivate_to_head);
4525STAT_ATTR(DEACTIVATE_TO_TAIL, deactivate_to_tail);
4526STAT_ATTR(DEACTIVATE_REMOTE_FREES, deactivate_remote_frees);
Christoph Lameter65c33762008-04-14 19:11:40 +03004527STAT_ATTR(ORDER_FALLBACK, order_fallback);
Christoph Lameter8ff12cf2008-02-07 17:47:41 -08004528#endif
4529
Pekka Enberg06428782008-01-07 23:20:27 -08004530static struct attribute *slab_attrs[] = {
Christoph Lameter81819f02007-05-06 14:49:36 -07004531 &slab_size_attr.attr,
4532 &object_size_attr.attr,
4533 &objs_per_slab_attr.attr,
4534 &order_attr.attr,
David Rientjes73d342b2009-02-22 17:40:09 -08004535 &min_partial_attr.attr,
Christoph Lameter81819f02007-05-06 14:49:36 -07004536 &objects_attr.attr,
Christoph Lameter205ab992008-04-14 19:11:40 +03004537 &objects_partial_attr.attr,
Christoph Lameter81819f02007-05-06 14:49:36 -07004538 &partial_attr.attr,
4539 &cpu_slabs_attr.attr,
4540 &ctor_attr.attr,
Christoph Lameter81819f02007-05-06 14:49:36 -07004541 &aliases_attr.attr,
4542 &align_attr.attr,
Christoph Lameter81819f02007-05-06 14:49:36 -07004543 &hwcache_align_attr.attr,
4544 &reclaim_account_attr.attr,
4545 &destroy_by_rcu_attr.attr,
Christoph Lametera5a84752010-10-05 13:57:27 -05004546 &shrink_attr.attr,
Lai Jiangshanab9a0f12011-03-10 15:21:48 +08004547 &reserved_attr.attr,
Christoph Lameterab4d5ed2010-10-05 13:57:26 -05004548#ifdef CONFIG_SLUB_DEBUG
Christoph Lametera5a84752010-10-05 13:57:27 -05004549 &total_objects_attr.attr,
4550 &slabs_attr.attr,
4551 &sanity_checks_attr.attr,
4552 &trace_attr.attr,
Christoph Lameter81819f02007-05-06 14:49:36 -07004553 &red_zone_attr.attr,
4554 &poison_attr.attr,
4555 &store_user_attr.attr,
Christoph Lameter53e15af2007-05-06 14:49:43 -07004556 &validate_attr.attr,
Christoph Lameter88a420e2007-05-06 14:49:45 -07004557 &alloc_calls_attr.attr,
4558 &free_calls_attr.attr,
Christoph Lameterab4d5ed2010-10-05 13:57:26 -05004559#endif
Christoph Lameter81819f02007-05-06 14:49:36 -07004560#ifdef CONFIG_ZONE_DMA
4561 &cache_dma_attr.attr,
4562#endif
4563#ifdef CONFIG_NUMA
Christoph Lameter98246012008-01-07 23:20:26 -08004564 &remote_node_defrag_ratio_attr.attr,
Christoph Lameter81819f02007-05-06 14:49:36 -07004565#endif
Christoph Lameter8ff12cf2008-02-07 17:47:41 -08004566#ifdef CONFIG_SLUB_STATS
4567 &alloc_fastpath_attr.attr,
4568 &alloc_slowpath_attr.attr,
4569 &free_fastpath_attr.attr,
4570 &free_slowpath_attr.attr,
4571 &free_frozen_attr.attr,
4572 &free_add_partial_attr.attr,
4573 &free_remove_partial_attr.attr,
4574 &alloc_from_partial_attr.attr,
4575 &alloc_slab_attr.attr,
4576 &alloc_refill_attr.attr,
4577 &free_slab_attr.attr,
4578 &cpuslab_flush_attr.attr,
4579 &deactivate_full_attr.attr,
4580 &deactivate_empty_attr.attr,
4581 &deactivate_to_head_attr.attr,
4582 &deactivate_to_tail_attr.attr,
4583 &deactivate_remote_frees_attr.attr,
Christoph Lameter65c33762008-04-14 19:11:40 +03004584 &order_fallback_attr.attr,
Christoph Lameter8ff12cf2008-02-07 17:47:41 -08004585#endif
Dmitry Monakhov4c13dd32010-02-26 09:36:12 +03004586#ifdef CONFIG_FAILSLAB
4587 &failslab_attr.attr,
4588#endif
4589
Christoph Lameter81819f02007-05-06 14:49:36 -07004590 NULL
4591};
4592
4593static struct attribute_group slab_attr_group = {
4594 .attrs = slab_attrs,
4595};
4596
4597static ssize_t slab_attr_show(struct kobject *kobj,
4598 struct attribute *attr,
4599 char *buf)
4600{
4601 struct slab_attribute *attribute;
4602 struct kmem_cache *s;
4603 int err;
4604
4605 attribute = to_slab_attr(attr);
4606 s = to_slab(kobj);
4607
4608 if (!attribute->show)
4609 return -EIO;
4610
4611 err = attribute->show(s, buf);
4612
4613 return err;
4614}
4615
4616static ssize_t slab_attr_store(struct kobject *kobj,
4617 struct attribute *attr,
4618 const char *buf, size_t len)
4619{
4620 struct slab_attribute *attribute;
4621 struct kmem_cache *s;
4622 int err;
4623
4624 attribute = to_slab_attr(attr);
4625 s = to_slab(kobj);
4626
4627 if (!attribute->store)
4628 return -EIO;
4629
4630 err = attribute->store(s, buf, len);
4631
4632 return err;
4633}
4634
Christoph Lameter151c6022008-01-07 22:29:05 -08004635static void kmem_cache_release(struct kobject *kobj)
4636{
4637 struct kmem_cache *s = to_slab(kobj);
4638
Pekka Enberg84c1cf62010-09-14 23:21:12 +03004639 kfree(s->name);
Christoph Lameter151c6022008-01-07 22:29:05 -08004640 kfree(s);
4641}
4642
Emese Revfy52cf25d2010-01-19 02:58:23 +01004643static const struct sysfs_ops slab_sysfs_ops = {
Christoph Lameter81819f02007-05-06 14:49:36 -07004644 .show = slab_attr_show,
4645 .store = slab_attr_store,
4646};
4647
4648static struct kobj_type slab_ktype = {
4649 .sysfs_ops = &slab_sysfs_ops,
Christoph Lameter151c6022008-01-07 22:29:05 -08004650 .release = kmem_cache_release
Christoph Lameter81819f02007-05-06 14:49:36 -07004651};
4652
4653static int uevent_filter(struct kset *kset, struct kobject *kobj)
4654{
4655 struct kobj_type *ktype = get_ktype(kobj);
4656
4657 if (ktype == &slab_ktype)
4658 return 1;
4659 return 0;
4660}
4661
Emese Revfy9cd43612009-12-31 14:52:51 +01004662static const struct kset_uevent_ops slab_uevent_ops = {
Christoph Lameter81819f02007-05-06 14:49:36 -07004663 .filter = uevent_filter,
4664};
4665
Greg Kroah-Hartman27c3a312007-11-01 09:29:06 -06004666static struct kset *slab_kset;
Christoph Lameter81819f02007-05-06 14:49:36 -07004667
4668#define ID_STR_LENGTH 64
4669
4670/* Create a unique string id for a slab cache:
Christoph Lameter6446faa2008-02-15 23:45:26 -08004671 *
4672 * Format :[flags-]size
Christoph Lameter81819f02007-05-06 14:49:36 -07004673 */
4674static char *create_unique_id(struct kmem_cache *s)
4675{
4676 char *name = kmalloc(ID_STR_LENGTH, GFP_KERNEL);
4677 char *p = name;
4678
4679 BUG_ON(!name);
4680
4681 *p++ = ':';
4682 /*
4683 * First flags affecting slabcache operations. We will only
4684 * get here for aliasable slabs so we do not need to support
4685 * too many flags. The flags here must cover all flags that
4686 * are matched during merging to guarantee that the id is
4687 * unique.
4688 */
4689 if (s->flags & SLAB_CACHE_DMA)
4690 *p++ = 'd';
4691 if (s->flags & SLAB_RECLAIM_ACCOUNT)
4692 *p++ = 'a';
4693 if (s->flags & SLAB_DEBUG_FREE)
4694 *p++ = 'F';
Vegard Nossum5a896d92008-04-04 00:54:48 +02004695 if (!(s->flags & SLAB_NOTRACK))
4696 *p++ = 't';
Christoph Lameter81819f02007-05-06 14:49:36 -07004697 if (p != name + 1)
4698 *p++ = '-';
4699 p += sprintf(p, "%07d", s->size);
4700 BUG_ON(p > name + ID_STR_LENGTH - 1);
4701 return name;
4702}
4703
4704static int sysfs_slab_add(struct kmem_cache *s)
4705{
4706 int err;
4707 const char *name;
4708 int unmergeable;
4709
4710 if (slab_state < SYSFS)
4711 /* Defer until later */
4712 return 0;
4713
4714 unmergeable = slab_unmergeable(s);
4715 if (unmergeable) {
4716 /*
4717 * Slabcache can never be merged so we can use the name proper.
4718 * This is typically the case for debug situations. In that
4719 * case we can catch duplicate names easily.
4720 */
Greg Kroah-Hartman27c3a312007-11-01 09:29:06 -06004721 sysfs_remove_link(&slab_kset->kobj, s->name);
Christoph Lameter81819f02007-05-06 14:49:36 -07004722 name = s->name;
4723 } else {
4724 /*
4725 * Create a unique name for the slab as a target
4726 * for the symlinks.
4727 */
4728 name = create_unique_id(s);
4729 }
4730
Greg Kroah-Hartman27c3a312007-11-01 09:29:06 -06004731 s->kobj.kset = slab_kset;
Greg Kroah-Hartman1eada112007-12-17 23:05:35 -07004732 err = kobject_init_and_add(&s->kobj, &slab_ktype, NULL, name);
4733 if (err) {
4734 kobject_put(&s->kobj);
Christoph Lameter81819f02007-05-06 14:49:36 -07004735 return err;
Greg Kroah-Hartman1eada112007-12-17 23:05:35 -07004736 }
Christoph Lameter81819f02007-05-06 14:49:36 -07004737
4738 err = sysfs_create_group(&s->kobj, &slab_attr_group);
Xiaotian Feng5788d8a2009-07-22 11:28:53 +08004739 if (err) {
4740 kobject_del(&s->kobj);
4741 kobject_put(&s->kobj);
Christoph Lameter81819f02007-05-06 14:49:36 -07004742 return err;
Xiaotian Feng5788d8a2009-07-22 11:28:53 +08004743 }
Christoph Lameter81819f02007-05-06 14:49:36 -07004744 kobject_uevent(&s->kobj, KOBJ_ADD);
4745 if (!unmergeable) {
4746 /* Setup first alias */
4747 sysfs_slab_alias(s, s->name);
4748 kfree(name);
4749 }
4750 return 0;
4751}
4752
4753static void sysfs_slab_remove(struct kmem_cache *s)
4754{
Christoph Lameter2bce6482010-07-19 11:39:11 -05004755 if (slab_state < SYSFS)
4756 /*
4757 * Sysfs has not been setup yet so no need to remove the
4758 * cache from sysfs.
4759 */
4760 return;
4761
Christoph Lameter81819f02007-05-06 14:49:36 -07004762 kobject_uevent(&s->kobj, KOBJ_REMOVE);
4763 kobject_del(&s->kobj);
Christoph Lameter151c6022008-01-07 22:29:05 -08004764 kobject_put(&s->kobj);
Christoph Lameter81819f02007-05-06 14:49:36 -07004765}
4766
4767/*
4768 * Need to buffer aliases during bootup until sysfs becomes
Nick Andrew9f6c708e2008-12-05 14:08:08 +11004769 * available lest we lose that information.
Christoph Lameter81819f02007-05-06 14:49:36 -07004770 */
4771struct saved_alias {
4772 struct kmem_cache *s;
4773 const char *name;
4774 struct saved_alias *next;
4775};
4776
Adrian Bunk5af328a2007-07-17 04:03:27 -07004777static struct saved_alias *alias_list;
Christoph Lameter81819f02007-05-06 14:49:36 -07004778
4779static int sysfs_slab_alias(struct kmem_cache *s, const char *name)
4780{
4781 struct saved_alias *al;
4782
4783 if (slab_state == SYSFS) {
4784 /*
4785 * If we have a leftover link then remove it.
4786 */
Greg Kroah-Hartman27c3a312007-11-01 09:29:06 -06004787 sysfs_remove_link(&slab_kset->kobj, name);
4788 return sysfs_create_link(&slab_kset->kobj, &s->kobj, name);
Christoph Lameter81819f02007-05-06 14:49:36 -07004789 }
4790
4791 al = kmalloc(sizeof(struct saved_alias), GFP_KERNEL);
4792 if (!al)
4793 return -ENOMEM;
4794
4795 al->s = s;
4796 al->name = name;
4797 al->next = alias_list;
4798 alias_list = al;
4799 return 0;
4800}
4801
4802static int __init slab_sysfs_init(void)
4803{
Christoph Lameter5b95a4ac2007-07-17 04:03:19 -07004804 struct kmem_cache *s;
Christoph Lameter81819f02007-05-06 14:49:36 -07004805 int err;
4806
Christoph Lameter2bce6482010-07-19 11:39:11 -05004807 down_write(&slub_lock);
4808
Greg Kroah-Hartman0ff21e42007-11-06 10:36:58 -08004809 slab_kset = kset_create_and_add("slab", &slab_uevent_ops, kernel_kobj);
Greg Kroah-Hartman27c3a312007-11-01 09:29:06 -06004810 if (!slab_kset) {
Christoph Lameter2bce6482010-07-19 11:39:11 -05004811 up_write(&slub_lock);
Christoph Lameter81819f02007-05-06 14:49:36 -07004812 printk(KERN_ERR "Cannot register slab subsystem.\n");
4813 return -ENOSYS;
4814 }
4815
Christoph Lameter26a7bd02007-05-09 02:32:39 -07004816 slab_state = SYSFS;
4817
Christoph Lameter5b95a4ac2007-07-17 04:03:19 -07004818 list_for_each_entry(s, &slab_caches, list) {
Christoph Lameter26a7bd02007-05-09 02:32:39 -07004819 err = sysfs_slab_add(s);
Christoph Lameter5d540fb2007-08-30 23:56:26 -07004820 if (err)
4821 printk(KERN_ERR "SLUB: Unable to add boot slab %s"
4822 " to sysfs\n", s->name);
Christoph Lameter26a7bd02007-05-09 02:32:39 -07004823 }
Christoph Lameter81819f02007-05-06 14:49:36 -07004824
4825 while (alias_list) {
4826 struct saved_alias *al = alias_list;
4827
4828 alias_list = alias_list->next;
4829 err = sysfs_slab_alias(al->s, al->name);
Christoph Lameter5d540fb2007-08-30 23:56:26 -07004830 if (err)
4831 printk(KERN_ERR "SLUB: Unable to add boot slab alias"
4832 " %s to sysfs\n", s->name);
Christoph Lameter81819f02007-05-06 14:49:36 -07004833 kfree(al);
4834 }
4835
Christoph Lameter2bce6482010-07-19 11:39:11 -05004836 up_write(&slub_lock);
Christoph Lameter81819f02007-05-06 14:49:36 -07004837 resiliency_test();
4838 return 0;
4839}
4840
4841__initcall(slab_sysfs_init);
Christoph Lameterab4d5ed2010-10-05 13:57:26 -05004842#endif /* CONFIG_SYSFS */
Pekka J Enberg57ed3ed2008-01-01 17:23:28 +01004843
4844/*
4845 * The /proc/slabinfo ABI
4846 */
Linus Torvalds158a9622008-01-02 13:04:48 -08004847#ifdef CONFIG_SLABINFO
Pekka J Enberg57ed3ed2008-01-01 17:23:28 +01004848static void print_slabinfo_header(struct seq_file *m)
4849{
4850 seq_puts(m, "slabinfo - version: 2.1\n");
4851 seq_puts(m, "# name <active_objs> <num_objs> <objsize> "
4852 "<objperslab> <pagesperslab>");
4853 seq_puts(m, " : tunables <limit> <batchcount> <sharedfactor>");
4854 seq_puts(m, " : slabdata <active_slabs> <num_slabs> <sharedavail>");
4855 seq_putc(m, '\n');
4856}
4857
4858static void *s_start(struct seq_file *m, loff_t *pos)
4859{
4860 loff_t n = *pos;
4861
4862 down_read(&slub_lock);
4863 if (!n)
4864 print_slabinfo_header(m);
4865
4866 return seq_list_start(&slab_caches, *pos);
4867}
4868
4869static void *s_next(struct seq_file *m, void *p, loff_t *pos)
4870{
4871 return seq_list_next(p, &slab_caches, pos);
4872}
4873
4874static void s_stop(struct seq_file *m, void *p)
4875{
4876 up_read(&slub_lock);
4877}
4878
4879static int s_show(struct seq_file *m, void *p)
4880{
4881 unsigned long nr_partials = 0;
4882 unsigned long nr_slabs = 0;
4883 unsigned long nr_inuse = 0;
Christoph Lameter205ab992008-04-14 19:11:40 +03004884 unsigned long nr_objs = 0;
4885 unsigned long nr_free = 0;
Pekka J Enberg57ed3ed2008-01-01 17:23:28 +01004886 struct kmem_cache *s;
4887 int node;
4888
4889 s = list_entry(p, struct kmem_cache, list);
4890
4891 for_each_online_node(node) {
4892 struct kmem_cache_node *n = get_node(s, node);
4893
4894 if (!n)
4895 continue;
4896
4897 nr_partials += n->nr_partial;
4898 nr_slabs += atomic_long_read(&n->nr_slabs);
Christoph Lameter205ab992008-04-14 19:11:40 +03004899 nr_objs += atomic_long_read(&n->total_objects);
4900 nr_free += count_partial(n, count_free);
Pekka J Enberg57ed3ed2008-01-01 17:23:28 +01004901 }
4902
Christoph Lameter205ab992008-04-14 19:11:40 +03004903 nr_inuse = nr_objs - nr_free;
Pekka J Enberg57ed3ed2008-01-01 17:23:28 +01004904
4905 seq_printf(m, "%-17s %6lu %6lu %6u %4u %4d", s->name, nr_inuse,
Christoph Lameter834f3d12008-04-14 19:11:31 +03004906 nr_objs, s->size, oo_objects(s->oo),
4907 (1 << oo_order(s->oo)));
Pekka J Enberg57ed3ed2008-01-01 17:23:28 +01004908 seq_printf(m, " : tunables %4u %4u %4u", 0, 0, 0);
4909 seq_printf(m, " : slabdata %6lu %6lu %6lu", nr_slabs, nr_slabs,
4910 0UL);
4911 seq_putc(m, '\n');
4912 return 0;
4913}
4914
Alexey Dobriyan7b3c3a52008-10-06 02:42:17 +04004915static const struct seq_operations slabinfo_op = {
Pekka J Enberg57ed3ed2008-01-01 17:23:28 +01004916 .start = s_start,
4917 .next = s_next,
4918 .stop = s_stop,
4919 .show = s_show,
4920};
4921
Alexey Dobriyan7b3c3a52008-10-06 02:42:17 +04004922static int slabinfo_open(struct inode *inode, struct file *file)
4923{
4924 return seq_open(file, &slabinfo_op);
4925}
4926
4927static const struct file_operations proc_slabinfo_operations = {
4928 .open = slabinfo_open,
4929 .read = seq_read,
4930 .llseek = seq_lseek,
4931 .release = seq_release,
4932};
4933
4934static int __init slab_proc_init(void)
4935{
WANG Congcf5d1132009-08-18 19:11:40 +03004936 proc_create("slabinfo", S_IRUGO, NULL, &proc_slabinfo_operations);
Alexey Dobriyan7b3c3a52008-10-06 02:42:17 +04004937 return 0;
4938}
4939module_init(slab_proc_init);
Linus Torvalds158a9622008-01-02 13:04:48 -08004940#endif /* CONFIG_SLABINFO */