blob: 42ce1730427508646787822bc6a1281cf8ddae97 [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
31/*
32 * Lock order:
33 * 1. slab_lock(page)
34 * 2. slab->list_lock
35 *
36 * The slab_lock protects operations on the object of a particular
37 * slab and its metadata in the page struct. If the slab lock
38 * has been taken then no allocations nor frees can be performed
39 * on the objects in the slab nor can the slab be added or removed
40 * from the partial or full lists since this would mean modifying
41 * the page_struct of the slab.
42 *
43 * The list_lock protects the partial and full list on each node and
44 * the partial slab counter. If taken then no new slabs may be added or
45 * removed from the lists nor make the number of partial slabs be modified.
46 * (Note that the total number of slabs is an atomic value that may be
47 * modified without taking the list lock).
48 *
49 * The list_lock is a centralized lock and thus we avoid taking it as
50 * much as possible. As long as SLUB does not have to handle partial
51 * slabs, operations can continue without any centralized lock. F.e.
52 * allocating a long series of objects that fill up slabs does not require
53 * the list lock.
54 *
55 * The lock order is sometimes inverted when we are trying to get a slab
56 * off a list. We take the list_lock and then look for a page on the list
57 * to use. While we do that objects in the slabs may be freed. We can
58 * only operate on the slab if we have also taken the slab_lock. So we use
59 * a slab_trylock() on the slab. If trylock was successful then no frees
60 * can occur anymore and we can use the slab for allocations etc. If the
61 * slab_trylock() does not succeed then frees are in progress in the slab and
62 * we must stay away from it for a while since we may cause a bouncing
63 * cacheline if we try to acquire the lock. So go onto the next slab.
64 * If all pages are busy then we may allocate a new slab instead of reusing
65 * a partial slab. A new slab has noone operating on it and thus there is
66 * no danger of cacheline contention.
67 *
68 * Interrupts are disabled during allocation and deallocation in order to
69 * make the slab allocator safe to use in the context of an irq. In addition
70 * interrupts are disabled to ensure that the processor does not change
71 * while handling per_cpu slabs, due to kernel preemption.
72 *
73 * SLUB assigns one slab for allocation to each processor.
74 * Allocations only occur from these slabs called cpu slabs.
75 *
Christoph Lameter672bba32007-05-09 02:32:39 -070076 * Slabs with free elements are kept on a partial list and during regular
77 * operations no list for full slabs is used. If an object in a full slab is
Christoph Lameter81819f02007-05-06 14:49:36 -070078 * freed then the slab will show up again on the partial lists.
Christoph Lameter672bba32007-05-09 02:32:39 -070079 * We track full slabs for debugging purposes though because otherwise we
80 * cannot scan all objects.
Christoph Lameter81819f02007-05-06 14:49:36 -070081 *
82 * Slabs are freed when they become empty. Teardown and setup is
83 * minimal so we rely on the page allocators per cpu caches for
84 * fast frees and allocs.
85 *
86 * Overloading of page flags that are otherwise used for LRU management.
87 *
Christoph Lameter4b6f0752007-05-16 22:10:53 -070088 * PageActive The slab is frozen and exempt from list processing.
89 * This means that the slab is dedicated to a purpose
90 * such as satisfying allocations for a specific
91 * processor. Objects may be freed in the slab while
92 * it is frozen but slab_free will then skip the usual
93 * list operations. It is up to the processor holding
94 * the slab to integrate the slab into the slab lists
95 * when the slab is no longer needed.
96 *
97 * One use of this flag is to mark slabs that are
98 * used for allocations. Then such a slab becomes a cpu
99 * slab. The cpu slab may be equipped with an additional
Christoph Lameterdfb4f092007-10-16 01:26:05 -0700100 * freelist that allows lockless access to
Christoph Lameter894b8782007-05-10 03:15:16 -0700101 * free objects in addition to the regular freelist
102 * that requires the slab lock.
Christoph Lameter81819f02007-05-06 14:49:36 -0700103 *
104 * PageError Slab requires special handling due to debug
105 * options set. This moves slab handling out of
Christoph Lameter894b8782007-05-10 03:15:16 -0700106 * the fast path and disables lockless freelists.
Christoph Lameter81819f02007-05-06 14:49:36 -0700107 */
108
Christoph Lameteraf537b02010-07-09 14:07:14 -0500109#define SLAB_DEBUG_FLAGS (SLAB_RED_ZONE | SLAB_POISON | SLAB_STORE_USER | \
110 SLAB_TRACE | SLAB_DEBUG_FREE)
111
112static inline int kmem_cache_debug(struct kmem_cache *s)
113{
Christoph Lameter5577bd82007-05-16 22:10:56 -0700114#ifdef CONFIG_SLUB_DEBUG
Christoph Lameteraf537b02010-07-09 14:07:14 -0500115 return unlikely(s->flags & SLAB_DEBUG_FLAGS);
Christoph Lameter5577bd82007-05-16 22:10:56 -0700116#else
Christoph Lameteraf537b02010-07-09 14:07:14 -0500117 return 0;
Christoph Lameter5577bd82007-05-16 22:10:56 -0700118#endif
Christoph Lameteraf537b02010-07-09 14:07:14 -0500119}
Christoph Lameter5577bd82007-05-16 22:10:56 -0700120
Christoph Lameter81819f02007-05-06 14:49:36 -0700121/*
122 * Issues still to be resolved:
123 *
Christoph Lameter81819f02007-05-06 14:49:36 -0700124 * - Support PAGE_ALLOC_DEBUG. Should be easy to do.
125 *
Christoph Lameter81819f02007-05-06 14:49:36 -0700126 * - Variable sizing of the per node arrays
127 */
128
129/* Enable to test recovery from slab corruption on boot */
130#undef SLUB_RESILIENCY_TEST
131
Christoph Lameter81819f02007-05-06 14:49:36 -0700132/*
Christoph Lameter2086d262007-05-06 14:49:46 -0700133 * Mininum number of partial slabs. These will be left on the partial
134 * lists even if they are empty. kmem_cache_shrink may reclaim them.
135 */
Christoph Lameter76be8952007-12-21 14:37:37 -0800136#define MIN_PARTIAL 5
Christoph Lametere95eed52007-05-06 14:49:44 -0700137
Christoph Lameter2086d262007-05-06 14:49:46 -0700138/*
139 * Maximum number of desirable partial slabs.
140 * The existence of more partial slabs makes kmem_cache_shrink
141 * sort the partial list by the number of objects in the.
142 */
143#define MAX_PARTIAL 10
144
Christoph Lameter81819f02007-05-06 14:49:36 -0700145#define DEBUG_DEFAULT_FLAGS (SLAB_DEBUG_FREE | SLAB_RED_ZONE | \
146 SLAB_POISON | SLAB_STORE_USER)
Christoph Lameter672bba32007-05-09 02:32:39 -0700147
Christoph Lameter81819f02007-05-06 14:49:36 -0700148/*
David Rientjes3de47212009-07-27 18:30:35 -0700149 * Debugging flags that require metadata to be stored in the slab. These get
150 * disabled when slub_debug=O is used and a cache's min order increases with
151 * metadata.
David Rientjesfa5ec8a2009-07-07 00:14:14 -0700152 */
David Rientjes3de47212009-07-27 18:30:35 -0700153#define DEBUG_METADATA_FLAGS (SLAB_RED_ZONE | SLAB_POISON | SLAB_STORE_USER)
David Rientjesfa5ec8a2009-07-07 00:14:14 -0700154
155/*
Christoph Lameter81819f02007-05-06 14:49:36 -0700156 * Set of flags that will prevent slab merging
157 */
158#define SLUB_NEVER_MERGE (SLAB_RED_ZONE | SLAB_POISON | SLAB_STORE_USER | \
Dmitry Monakhov4c13dd32010-02-26 09:36:12 +0300159 SLAB_TRACE | SLAB_DESTROY_BY_RCU | SLAB_NOLEAKTRACE | \
160 SLAB_FAILSLAB)
Christoph Lameter81819f02007-05-06 14:49:36 -0700161
162#define SLUB_MERGE_SAME (SLAB_DEBUG_FREE | SLAB_RECLAIM_ACCOUNT | \
Vegard Nossum5a896d92008-04-04 00:54:48 +0200163 SLAB_CACHE_DMA | SLAB_NOTRACK)
Christoph Lameter81819f02007-05-06 14:49:36 -0700164
Cyrill Gorcunov210b5c02008-10-22 23:00:38 +0400165#define OO_SHIFT 16
166#define OO_MASK ((1 << OO_SHIFT) - 1)
167#define MAX_OBJS_PER_PAGE 65535 /* since page.objects is u16 */
168
Christoph Lameter81819f02007-05-06 14:49:36 -0700169/* Internal SLUB flags */
Christoph Lameterf90ec392010-07-09 14:07:11 -0500170#define __OBJECT_POISON 0x80000000UL /* Poison object */
Christoph Lameter81819f02007-05-06 14:49:36 -0700171
172static int kmem_size = sizeof(struct kmem_cache);
173
174#ifdef CONFIG_SMP
175static struct notifier_block slab_notifier;
176#endif
177
178static enum {
179 DOWN, /* No slab functionality available */
Christoph Lameter51df1142010-08-20 12:37:15 -0500180 PARTIAL, /* Kmem_cache_node works */
Christoph Lameter672bba32007-05-09 02:32:39 -0700181 UP, /* Everything works but does not show up in sysfs */
Christoph Lameter81819f02007-05-06 14:49:36 -0700182 SYSFS /* Sysfs up */
183} slab_state = DOWN;
184
185/* A list of all slab caches on the system */
186static DECLARE_RWSEM(slub_lock);
Adrian Bunk5af328a2007-07-17 04:03:27 -0700187static LIST_HEAD(slab_caches);
Christoph Lameter81819f02007-05-06 14:49:36 -0700188
Christoph Lameter02cbc872007-05-09 02:32:43 -0700189/*
190 * Tracking user of a slab.
191 */
192struct track {
Eduard - Gabriel Munteanuce71e272008-08-19 20:43:25 +0300193 unsigned long addr; /* Called from address */
Christoph Lameter02cbc872007-05-09 02:32:43 -0700194 int cpu; /* Was running on cpu */
195 int pid; /* Pid context */
196 unsigned long when; /* When did the operation occur */
197};
198
199enum track_item { TRACK_ALLOC, TRACK_FREE };
200
Christoph Lameterf6acb632008-04-29 16:16:06 -0700201#ifdef CONFIG_SLUB_DEBUG
Christoph Lameter81819f02007-05-06 14:49:36 -0700202static int sysfs_slab_add(struct kmem_cache *);
203static int sysfs_slab_alias(struct kmem_cache *, const char *);
204static void sysfs_slab_remove(struct kmem_cache *);
Christoph Lameter8ff12cf2008-02-07 17:47:41 -0800205
Christoph Lameter81819f02007-05-06 14:49:36 -0700206#else
Christoph Lameter0c710012007-07-17 04:03:24 -0700207static inline int sysfs_slab_add(struct kmem_cache *s) { return 0; }
208static inline int sysfs_slab_alias(struct kmem_cache *s, const char *p)
209 { return 0; }
Christoph Lameter151c6022008-01-07 22:29:05 -0800210static inline void sysfs_slab_remove(struct kmem_cache *s)
211{
Pekka Enberg84c1cf62010-09-14 23:21:12 +0300212 kfree(s->name);
Christoph Lameter151c6022008-01-07 22:29:05 -0800213 kfree(s);
214}
Christoph Lameter8ff12cf2008-02-07 17:47:41 -0800215
Christoph Lameter81819f02007-05-06 14:49:36 -0700216#endif
217
Christoph Lameter84e554e2009-12-18 16:26:23 -0600218static inline void stat(struct kmem_cache *s, enum stat_item si)
Christoph Lameter8ff12cf2008-02-07 17:47:41 -0800219{
220#ifdef CONFIG_SLUB_STATS
Christoph Lameter84e554e2009-12-18 16:26:23 -0600221 __this_cpu_inc(s->cpu_slab->stat[si]);
Christoph Lameter8ff12cf2008-02-07 17:47:41 -0800222#endif
223}
224
Christoph Lameter81819f02007-05-06 14:49:36 -0700225/********************************************************************
226 * Core slab cache functions
227 *******************************************************************/
228
229int slab_is_available(void)
230{
231 return slab_state >= UP;
232}
233
234static inline struct kmem_cache_node *get_node(struct kmem_cache *s, int node)
235{
236#ifdef CONFIG_NUMA
237 return s->node[node];
238#else
239 return &s->local_node;
240#endif
241}
242
Christoph Lameter6446faa2008-02-15 23:45:26 -0800243/* Verify that a pointer has an address that is valid within a slab page */
Christoph Lameter02cbc872007-05-09 02:32:43 -0700244static inline int check_valid_pointer(struct kmem_cache *s,
245 struct page *page, const void *object)
246{
247 void *base;
248
Christoph Lametera973e9d2008-03-01 13:40:44 -0800249 if (!object)
Christoph Lameter02cbc872007-05-09 02:32:43 -0700250 return 1;
251
Christoph Lametera973e9d2008-03-01 13:40:44 -0800252 base = page_address(page);
Christoph Lameter39b26462008-04-14 19:11:30 +0300253 if (object < base || object >= base + page->objects * s->size ||
Christoph Lameter02cbc872007-05-09 02:32:43 -0700254 (object - base) % s->size) {
255 return 0;
256 }
257
258 return 1;
259}
260
Christoph Lameter7656c722007-05-09 02:32:40 -0700261static inline void *get_freepointer(struct kmem_cache *s, void *object)
262{
263 return *(void **)(object + s->offset);
264}
265
266static inline void set_freepointer(struct kmem_cache *s, void *object, void *fp)
267{
268 *(void **)(object + s->offset) = fp;
269}
270
271/* Loop over all objects in a slab */
Christoph Lameter224a88b2008-04-14 19:11:31 +0300272#define for_each_object(__p, __s, __addr, __objects) \
273 for (__p = (__addr); __p < (__addr) + (__objects) * (__s)->size;\
Christoph Lameter7656c722007-05-09 02:32:40 -0700274 __p += (__s)->size)
275
276/* Scan freelist */
277#define for_each_free_object(__p, __s, __free) \
Christoph Lametera973e9d2008-03-01 13:40:44 -0800278 for (__p = (__free); __p; __p = get_freepointer((__s), __p))
Christoph Lameter7656c722007-05-09 02:32:40 -0700279
280/* Determine object index from a given position */
281static inline int slab_index(void *p, struct kmem_cache *s, void *addr)
282{
283 return (p - addr) / s->size;
284}
285
Christoph Lameter834f3d12008-04-14 19:11:31 +0300286static inline struct kmem_cache_order_objects oo_make(int order,
287 unsigned long size)
288{
289 struct kmem_cache_order_objects x = {
Cyrill Gorcunov210b5c02008-10-22 23:00:38 +0400290 (order << OO_SHIFT) + (PAGE_SIZE << order) / size
Christoph Lameter834f3d12008-04-14 19:11:31 +0300291 };
292
293 return x;
294}
295
296static inline int oo_order(struct kmem_cache_order_objects x)
297{
Cyrill Gorcunov210b5c02008-10-22 23:00:38 +0400298 return x.x >> OO_SHIFT;
Christoph Lameter834f3d12008-04-14 19:11:31 +0300299}
300
301static inline int oo_objects(struct kmem_cache_order_objects x)
302{
Cyrill Gorcunov210b5c02008-10-22 23:00:38 +0400303 return x.x & OO_MASK;
Christoph Lameter834f3d12008-04-14 19:11:31 +0300304}
305
Christoph Lameter41ecc552007-05-09 02:32:44 -0700306#ifdef CONFIG_SLUB_DEBUG
307/*
308 * Debug settings:
309 */
Christoph Lameterf0630ff2007-07-15 23:38:14 -0700310#ifdef CONFIG_SLUB_DEBUG_ON
311static int slub_debug = DEBUG_DEFAULT_FLAGS;
312#else
Christoph Lameter41ecc552007-05-09 02:32:44 -0700313static int slub_debug;
Christoph Lameterf0630ff2007-07-15 23:38:14 -0700314#endif
Christoph Lameter41ecc552007-05-09 02:32:44 -0700315
316static char *slub_debug_slabs;
David Rientjesfa5ec8a2009-07-07 00:14:14 -0700317static int disable_higher_order_debug;
Christoph Lameter41ecc552007-05-09 02:32:44 -0700318
Christoph Lameter7656c722007-05-09 02:32:40 -0700319/*
Christoph Lameter81819f02007-05-06 14:49:36 -0700320 * Object debugging
321 */
322static void print_section(char *text, u8 *addr, unsigned int length)
323{
324 int i, offset;
325 int newline = 1;
326 char ascii[17];
327
328 ascii[16] = 0;
329
330 for (i = 0; i < length; i++) {
331 if (newline) {
Christoph Lameter24922682007-07-17 04:03:18 -0700332 printk(KERN_ERR "%8s 0x%p: ", text, addr + i);
Christoph Lameter81819f02007-05-06 14:49:36 -0700333 newline = 0;
334 }
Pekka Enberg06428782008-01-07 23:20:27 -0800335 printk(KERN_CONT " %02x", addr[i]);
Christoph Lameter81819f02007-05-06 14:49:36 -0700336 offset = i % 16;
337 ascii[offset] = isgraph(addr[i]) ? addr[i] : '.';
338 if (offset == 15) {
Pekka Enberg06428782008-01-07 23:20:27 -0800339 printk(KERN_CONT " %s\n", ascii);
Christoph Lameter81819f02007-05-06 14:49:36 -0700340 newline = 1;
341 }
342 }
343 if (!newline) {
344 i %= 16;
345 while (i < 16) {
Pekka Enberg06428782008-01-07 23:20:27 -0800346 printk(KERN_CONT " ");
Christoph Lameter81819f02007-05-06 14:49:36 -0700347 ascii[i] = ' ';
348 i++;
349 }
Pekka Enberg06428782008-01-07 23:20:27 -0800350 printk(KERN_CONT " %s\n", ascii);
Christoph Lameter81819f02007-05-06 14:49:36 -0700351 }
352}
353
Christoph Lameter81819f02007-05-06 14:49:36 -0700354static struct track *get_track(struct kmem_cache *s, void *object,
355 enum track_item alloc)
356{
357 struct track *p;
358
359 if (s->offset)
360 p = object + s->offset + sizeof(void *);
361 else
362 p = object + s->inuse;
363
364 return p + alloc;
365}
366
367static void set_track(struct kmem_cache *s, void *object,
Eduard - Gabriel Munteanuce71e272008-08-19 20:43:25 +0300368 enum track_item alloc, unsigned long addr)
Christoph Lameter81819f02007-05-06 14:49:36 -0700369{
Akinobu Mita1a00df42009-03-07 00:36:21 +0900370 struct track *p = get_track(s, object, alloc);
Christoph Lameter81819f02007-05-06 14:49:36 -0700371
Christoph Lameter81819f02007-05-06 14:49:36 -0700372 if (addr) {
373 p->addr = addr;
374 p->cpu = smp_processor_id();
Alexey Dobriyan88e4ccf2008-06-23 02:58:37 +0400375 p->pid = current->pid;
Christoph Lameter81819f02007-05-06 14:49:36 -0700376 p->when = jiffies;
377 } else
378 memset(p, 0, sizeof(struct track));
379}
380
Christoph Lameter81819f02007-05-06 14:49:36 -0700381static void init_tracking(struct kmem_cache *s, void *object)
382{
Christoph Lameter24922682007-07-17 04:03:18 -0700383 if (!(s->flags & SLAB_STORE_USER))
384 return;
385
Eduard - Gabriel Munteanuce71e272008-08-19 20:43:25 +0300386 set_track(s, object, TRACK_FREE, 0UL);
387 set_track(s, object, TRACK_ALLOC, 0UL);
Christoph Lameter81819f02007-05-06 14:49:36 -0700388}
389
390static void print_track(const char *s, struct track *t)
391{
392 if (!t->addr)
393 return;
394
Linus Torvalds7daf7052008-07-14 12:12:53 -0700395 printk(KERN_ERR "INFO: %s in %pS age=%lu cpu=%u pid=%d\n",
Eduard - Gabriel Munteanuce71e272008-08-19 20:43:25 +0300396 s, (void *)t->addr, jiffies - t->when, t->cpu, t->pid);
Christoph Lameter81819f02007-05-06 14:49:36 -0700397}
398
Christoph Lameter24922682007-07-17 04:03:18 -0700399static void print_tracking(struct kmem_cache *s, void *object)
400{
401 if (!(s->flags & SLAB_STORE_USER))
402 return;
403
404 print_track("Allocated", get_track(s, object, TRACK_ALLOC));
405 print_track("Freed", get_track(s, object, TRACK_FREE));
406}
407
408static void print_page_info(struct page *page)
409{
Christoph Lameter39b26462008-04-14 19:11:30 +0300410 printk(KERN_ERR "INFO: Slab 0x%p objects=%u used=%u fp=0x%p flags=0x%04lx\n",
411 page, page->objects, page->inuse, page->freelist, page->flags);
Christoph Lameter24922682007-07-17 04:03:18 -0700412
413}
414
415static void slab_bug(struct kmem_cache *s, char *fmt, ...)
416{
417 va_list args;
418 char buf[100];
419
420 va_start(args, fmt);
421 vsnprintf(buf, sizeof(buf), fmt, args);
422 va_end(args);
423 printk(KERN_ERR "========================================"
424 "=====================================\n");
425 printk(KERN_ERR "BUG %s: %s\n", s->name, buf);
426 printk(KERN_ERR "----------------------------------------"
427 "-------------------------------------\n\n");
428}
429
430static void slab_fix(struct kmem_cache *s, char *fmt, ...)
431{
432 va_list args;
433 char buf[100];
434
435 va_start(args, fmt);
436 vsnprintf(buf, sizeof(buf), fmt, args);
437 va_end(args);
438 printk(KERN_ERR "FIX %s: %s\n", s->name, buf);
439}
440
441static void print_trailer(struct kmem_cache *s, struct page *page, u8 *p)
Christoph Lameter81819f02007-05-06 14:49:36 -0700442{
443 unsigned int off; /* Offset of last byte */
Christoph Lametera973e9d2008-03-01 13:40:44 -0800444 u8 *addr = page_address(page);
Christoph Lameter24922682007-07-17 04:03:18 -0700445
446 print_tracking(s, p);
447
448 print_page_info(page);
449
450 printk(KERN_ERR "INFO: Object 0x%p @offset=%tu fp=0x%p\n\n",
451 p, p - addr, get_freepointer(s, p));
452
453 if (p > addr + 16)
454 print_section("Bytes b4", p - 16, 16);
455
Pekka Enberg0ebd6522008-07-19 14:17:22 +0300456 print_section("Object", p, min_t(unsigned long, s->objsize, PAGE_SIZE));
Christoph Lameter81819f02007-05-06 14:49:36 -0700457
458 if (s->flags & SLAB_RED_ZONE)
459 print_section("Redzone", p + s->objsize,
460 s->inuse - s->objsize);
461
Christoph Lameter81819f02007-05-06 14:49:36 -0700462 if (s->offset)
463 off = s->offset + sizeof(void *);
464 else
465 off = s->inuse;
466
Christoph Lameter24922682007-07-17 04:03:18 -0700467 if (s->flags & SLAB_STORE_USER)
Christoph Lameter81819f02007-05-06 14:49:36 -0700468 off += 2 * sizeof(struct track);
Christoph Lameter81819f02007-05-06 14:49:36 -0700469
470 if (off != s->size)
471 /* Beginning of the filler is the free pointer */
Christoph Lameter24922682007-07-17 04:03:18 -0700472 print_section("Padding", p + off, s->size - off);
473
474 dump_stack();
Christoph Lameter81819f02007-05-06 14:49:36 -0700475}
476
477static void object_err(struct kmem_cache *s, struct page *page,
478 u8 *object, char *reason)
479{
Christoph Lameter3dc50632008-04-23 12:28:01 -0700480 slab_bug(s, "%s", reason);
Christoph Lameter24922682007-07-17 04:03:18 -0700481 print_trailer(s, page, object);
Christoph Lameter81819f02007-05-06 14:49:36 -0700482}
483
Christoph Lameter24922682007-07-17 04:03:18 -0700484static void slab_err(struct kmem_cache *s, struct page *page, char *fmt, ...)
Christoph Lameter81819f02007-05-06 14:49:36 -0700485{
486 va_list args;
487 char buf[100];
488
Christoph Lameter24922682007-07-17 04:03:18 -0700489 va_start(args, fmt);
490 vsnprintf(buf, sizeof(buf), fmt, args);
Christoph Lameter81819f02007-05-06 14:49:36 -0700491 va_end(args);
Christoph Lameter3dc50632008-04-23 12:28:01 -0700492 slab_bug(s, "%s", buf);
Christoph Lameter24922682007-07-17 04:03:18 -0700493 print_page_info(page);
Christoph Lameter81819f02007-05-06 14:49:36 -0700494 dump_stack();
495}
496
497static void init_object(struct kmem_cache *s, void *object, int active)
498{
499 u8 *p = object;
500
501 if (s->flags & __OBJECT_POISON) {
502 memset(p, POISON_FREE, s->objsize - 1);
Pekka Enberg06428782008-01-07 23:20:27 -0800503 p[s->objsize - 1] = POISON_END;
Christoph Lameter81819f02007-05-06 14:49:36 -0700504 }
505
506 if (s->flags & SLAB_RED_ZONE)
507 memset(p + s->objsize,
508 active ? SLUB_RED_ACTIVE : SLUB_RED_INACTIVE,
509 s->inuse - s->objsize);
510}
511
Christoph Lameter24922682007-07-17 04:03:18 -0700512static u8 *check_bytes(u8 *start, unsigned int value, unsigned int bytes)
Christoph Lameter81819f02007-05-06 14:49:36 -0700513{
514 while (bytes) {
515 if (*start != (u8)value)
Christoph Lameter24922682007-07-17 04:03:18 -0700516 return start;
Christoph Lameter81819f02007-05-06 14:49:36 -0700517 start++;
518 bytes--;
519 }
Christoph Lameter24922682007-07-17 04:03:18 -0700520 return NULL;
521}
522
523static void restore_bytes(struct kmem_cache *s, char *message, u8 data,
524 void *from, void *to)
525{
526 slab_fix(s, "Restoring 0x%p-0x%p=0x%x\n", from, to - 1, data);
527 memset(from, data, to - from);
528}
529
530static int check_bytes_and_report(struct kmem_cache *s, struct page *page,
531 u8 *object, char *what,
Pekka Enberg06428782008-01-07 23:20:27 -0800532 u8 *start, unsigned int value, unsigned int bytes)
Christoph Lameter24922682007-07-17 04:03:18 -0700533{
534 u8 *fault;
535 u8 *end;
536
537 fault = check_bytes(start, value, bytes);
538 if (!fault)
539 return 1;
540
541 end = start + bytes;
542 while (end > fault && end[-1] == value)
543 end--;
544
545 slab_bug(s, "%s overwritten", what);
546 printk(KERN_ERR "INFO: 0x%p-0x%p. First byte 0x%x instead of 0x%x\n",
547 fault, end - 1, fault[0], value);
548 print_trailer(s, page, object);
549
550 restore_bytes(s, what, value, fault, end);
551 return 0;
Christoph Lameter81819f02007-05-06 14:49:36 -0700552}
553
Christoph Lameter81819f02007-05-06 14:49:36 -0700554/*
555 * Object layout:
556 *
557 * object address
558 * Bytes of the object to be managed.
559 * If the freepointer may overlay the object then the free
560 * pointer is the first word of the object.
Christoph Lameter672bba32007-05-09 02:32:39 -0700561 *
Christoph Lameter81819f02007-05-06 14:49:36 -0700562 * Poisoning uses 0x6b (POISON_FREE) and the last byte is
563 * 0xa5 (POISON_END)
564 *
565 * object + s->objsize
566 * Padding to reach word boundary. This is also used for Redzoning.
Christoph Lameter672bba32007-05-09 02:32:39 -0700567 * Padding is extended by another word if Redzoning is enabled and
568 * objsize == inuse.
569 *
Christoph Lameter81819f02007-05-06 14:49:36 -0700570 * We fill with 0xbb (RED_INACTIVE) for inactive objects and with
571 * 0xcc (RED_ACTIVE) for objects in use.
572 *
573 * object + s->inuse
Christoph Lameter672bba32007-05-09 02:32:39 -0700574 * Meta data starts here.
575 *
Christoph Lameter81819f02007-05-06 14:49:36 -0700576 * A. Free pointer (if we cannot overwrite object on free)
577 * B. Tracking data for SLAB_STORE_USER
Christoph Lameter672bba32007-05-09 02:32:39 -0700578 * C. Padding to reach required alignment boundary or at mininum
Christoph Lameter6446faa2008-02-15 23:45:26 -0800579 * one word if debugging is on to be able to detect writes
Christoph Lameter672bba32007-05-09 02:32:39 -0700580 * before the word boundary.
581 *
582 * Padding is done using 0x5a (POISON_INUSE)
Christoph Lameter81819f02007-05-06 14:49:36 -0700583 *
584 * object + s->size
Christoph Lameter672bba32007-05-09 02:32:39 -0700585 * Nothing is used beyond s->size.
Christoph Lameter81819f02007-05-06 14:49:36 -0700586 *
Christoph Lameter672bba32007-05-09 02:32:39 -0700587 * If slabcaches are merged then the objsize and inuse boundaries are mostly
588 * ignored. And therefore no slab options that rely on these boundaries
Christoph Lameter81819f02007-05-06 14:49:36 -0700589 * may be used with merged slabcaches.
590 */
591
Christoph Lameter81819f02007-05-06 14:49:36 -0700592static int check_pad_bytes(struct kmem_cache *s, struct page *page, u8 *p)
593{
594 unsigned long off = s->inuse; /* The end of info */
595
596 if (s->offset)
597 /* Freepointer is placed after the object. */
598 off += sizeof(void *);
599
600 if (s->flags & SLAB_STORE_USER)
601 /* We also have user information there */
602 off += 2 * sizeof(struct track);
603
604 if (s->size == off)
605 return 1;
606
Christoph Lameter24922682007-07-17 04:03:18 -0700607 return check_bytes_and_report(s, page, p, "Object padding",
608 p + off, POISON_INUSE, s->size - off);
Christoph Lameter81819f02007-05-06 14:49:36 -0700609}
610
Christoph Lameter39b26462008-04-14 19:11:30 +0300611/* Check the pad bytes at the end of a slab page */
Christoph Lameter81819f02007-05-06 14:49:36 -0700612static int slab_pad_check(struct kmem_cache *s, struct page *page)
613{
Christoph Lameter24922682007-07-17 04:03:18 -0700614 u8 *start;
615 u8 *fault;
616 u8 *end;
617 int length;
618 int remainder;
Christoph Lameter81819f02007-05-06 14:49:36 -0700619
620 if (!(s->flags & SLAB_POISON))
621 return 1;
622
Christoph Lametera973e9d2008-03-01 13:40:44 -0800623 start = page_address(page);
Christoph Lameter834f3d12008-04-14 19:11:31 +0300624 length = (PAGE_SIZE << compound_order(page));
Christoph Lameter39b26462008-04-14 19:11:30 +0300625 end = start + length;
626 remainder = length % s->size;
Christoph Lameter81819f02007-05-06 14:49:36 -0700627 if (!remainder)
628 return 1;
629
Christoph Lameter39b26462008-04-14 19:11:30 +0300630 fault = check_bytes(end - remainder, POISON_INUSE, remainder);
Christoph Lameter24922682007-07-17 04:03:18 -0700631 if (!fault)
632 return 1;
633 while (end > fault && end[-1] == POISON_INUSE)
634 end--;
635
636 slab_err(s, page, "Padding overwritten. 0x%p-0x%p", fault, end - 1);
Christoph Lameter39b26462008-04-14 19:11:30 +0300637 print_section("Padding", end - remainder, remainder);
Christoph Lameter24922682007-07-17 04:03:18 -0700638
Eric Dumazet8a3d2712009-09-03 16:08:06 +0200639 restore_bytes(s, "slab padding", POISON_INUSE, end - remainder, end);
Christoph Lameter24922682007-07-17 04:03:18 -0700640 return 0;
Christoph Lameter81819f02007-05-06 14:49:36 -0700641}
642
643static int check_object(struct kmem_cache *s, struct page *page,
644 void *object, int active)
645{
646 u8 *p = object;
647 u8 *endobject = object + s->objsize;
648
649 if (s->flags & SLAB_RED_ZONE) {
650 unsigned int red =
651 active ? SLUB_RED_ACTIVE : SLUB_RED_INACTIVE;
652
Christoph Lameter24922682007-07-17 04:03:18 -0700653 if (!check_bytes_and_report(s, page, object, "Redzone",
654 endobject, red, s->inuse - s->objsize))
Christoph Lameter81819f02007-05-06 14:49:36 -0700655 return 0;
Christoph Lameter81819f02007-05-06 14:49:36 -0700656 } else {
Ingo Molnar3adbefe2008-02-05 17:57:39 -0800657 if ((s->flags & SLAB_POISON) && s->objsize < s->inuse) {
658 check_bytes_and_report(s, page, p, "Alignment padding",
659 endobject, POISON_INUSE, s->inuse - s->objsize);
660 }
Christoph Lameter81819f02007-05-06 14:49:36 -0700661 }
662
663 if (s->flags & SLAB_POISON) {
664 if (!active && (s->flags & __OBJECT_POISON) &&
Christoph Lameter24922682007-07-17 04:03:18 -0700665 (!check_bytes_and_report(s, page, p, "Poison", p,
666 POISON_FREE, s->objsize - 1) ||
667 !check_bytes_and_report(s, page, p, "Poison",
Pekka Enberg06428782008-01-07 23:20:27 -0800668 p + s->objsize - 1, POISON_END, 1)))
Christoph Lameter81819f02007-05-06 14:49:36 -0700669 return 0;
Christoph Lameter81819f02007-05-06 14:49:36 -0700670 /*
671 * check_pad_bytes cleans up on its own.
672 */
673 check_pad_bytes(s, page, p);
674 }
675
676 if (!s->offset && active)
677 /*
678 * Object and freepointer overlap. Cannot check
679 * freepointer while object is allocated.
680 */
681 return 1;
682
683 /* Check free pointer validity */
684 if (!check_valid_pointer(s, page, get_freepointer(s, p))) {
685 object_err(s, page, p, "Freepointer corrupt");
686 /*
Nick Andrew9f6c708e2008-12-05 14:08:08 +1100687 * No choice but to zap it and thus lose the remainder
Christoph Lameter81819f02007-05-06 14:49:36 -0700688 * of the free objects in this slab. May cause
Christoph Lameter672bba32007-05-09 02:32:39 -0700689 * another error because the object count is now wrong.
Christoph Lameter81819f02007-05-06 14:49:36 -0700690 */
Christoph Lametera973e9d2008-03-01 13:40:44 -0800691 set_freepointer(s, p, NULL);
Christoph Lameter81819f02007-05-06 14:49:36 -0700692 return 0;
693 }
694 return 1;
695}
696
697static int check_slab(struct kmem_cache *s, struct page *page)
698{
Christoph Lameter39b26462008-04-14 19:11:30 +0300699 int maxobj;
700
Christoph Lameter81819f02007-05-06 14:49:36 -0700701 VM_BUG_ON(!irqs_disabled());
702
703 if (!PageSlab(page)) {
Christoph Lameter24922682007-07-17 04:03:18 -0700704 slab_err(s, page, "Not a valid slab page");
Christoph Lameter81819f02007-05-06 14:49:36 -0700705 return 0;
706 }
Christoph Lameter39b26462008-04-14 19:11:30 +0300707
708 maxobj = (PAGE_SIZE << compound_order(page)) / s->size;
709 if (page->objects > maxobj) {
710 slab_err(s, page, "objects %u > max %u",
711 s->name, page->objects, maxobj);
712 return 0;
713 }
714 if (page->inuse > page->objects) {
Christoph Lameter24922682007-07-17 04:03:18 -0700715 slab_err(s, page, "inuse %u > max %u",
Christoph Lameter39b26462008-04-14 19:11:30 +0300716 s->name, page->inuse, page->objects);
Christoph Lameter81819f02007-05-06 14:49:36 -0700717 return 0;
718 }
719 /* Slab_pad_check fixes things up after itself */
720 slab_pad_check(s, page);
721 return 1;
722}
723
724/*
Christoph Lameter672bba32007-05-09 02:32:39 -0700725 * Determine if a certain object on a page is on the freelist. Must hold the
726 * slab lock to guarantee that the chains are in a consistent state.
Christoph Lameter81819f02007-05-06 14:49:36 -0700727 */
728static int on_freelist(struct kmem_cache *s, struct page *page, void *search)
729{
730 int nr = 0;
731 void *fp = page->freelist;
732 void *object = NULL;
Christoph Lameter224a88b2008-04-14 19:11:31 +0300733 unsigned long max_objects;
Christoph Lameter81819f02007-05-06 14:49:36 -0700734
Christoph Lameter39b26462008-04-14 19:11:30 +0300735 while (fp && nr <= page->objects) {
Christoph Lameter81819f02007-05-06 14:49:36 -0700736 if (fp == search)
737 return 1;
738 if (!check_valid_pointer(s, page, fp)) {
739 if (object) {
740 object_err(s, page, object,
741 "Freechain corrupt");
Christoph Lametera973e9d2008-03-01 13:40:44 -0800742 set_freepointer(s, object, NULL);
Christoph Lameter81819f02007-05-06 14:49:36 -0700743 break;
744 } else {
Christoph Lameter24922682007-07-17 04:03:18 -0700745 slab_err(s, page, "Freepointer corrupt");
Christoph Lametera973e9d2008-03-01 13:40:44 -0800746 page->freelist = NULL;
Christoph Lameter39b26462008-04-14 19:11:30 +0300747 page->inuse = page->objects;
Christoph Lameter24922682007-07-17 04:03:18 -0700748 slab_fix(s, "Freelist cleared");
Christoph Lameter81819f02007-05-06 14:49:36 -0700749 return 0;
750 }
751 break;
752 }
753 object = fp;
754 fp = get_freepointer(s, object);
755 nr++;
756 }
757
Christoph Lameter224a88b2008-04-14 19:11:31 +0300758 max_objects = (PAGE_SIZE << compound_order(page)) / s->size;
Cyrill Gorcunov210b5c02008-10-22 23:00:38 +0400759 if (max_objects > MAX_OBJS_PER_PAGE)
760 max_objects = MAX_OBJS_PER_PAGE;
Christoph Lameter224a88b2008-04-14 19:11:31 +0300761
762 if (page->objects != max_objects) {
763 slab_err(s, page, "Wrong number of objects. Found %d but "
764 "should be %d", page->objects, max_objects);
765 page->objects = max_objects;
766 slab_fix(s, "Number of objects adjusted.");
767 }
Christoph Lameter39b26462008-04-14 19:11:30 +0300768 if (page->inuse != page->objects - nr) {
Christoph Lameter70d71222007-05-06 14:49:47 -0700769 slab_err(s, page, "Wrong object count. Counter is %d but "
Christoph Lameter39b26462008-04-14 19:11:30 +0300770 "counted were %d", page->inuse, page->objects - nr);
771 page->inuse = page->objects - nr;
Christoph Lameter24922682007-07-17 04:03:18 -0700772 slab_fix(s, "Object count adjusted.");
Christoph Lameter81819f02007-05-06 14:49:36 -0700773 }
774 return search == NULL;
775}
776
Christoph Lameter0121c6192008-04-29 16:11:12 -0700777static void trace(struct kmem_cache *s, struct page *page, void *object,
778 int alloc)
Christoph Lameter3ec09742007-05-16 22:11:00 -0700779{
780 if (s->flags & SLAB_TRACE) {
781 printk(KERN_INFO "TRACE %s %s 0x%p inuse=%d fp=0x%p\n",
782 s->name,
783 alloc ? "alloc" : "free",
784 object, page->inuse,
785 page->freelist);
786
787 if (!alloc)
788 print_section("Object", (void *)object, s->objsize);
789
790 dump_stack();
791 }
792}
793
Christoph Lameter643b1132007-05-06 14:49:42 -0700794/*
Christoph Lameterc016b0b2010-08-20 12:37:16 -0500795 * Hooks for other subsystems that check memory allocations. In a typical
796 * production configuration these hooks all should produce no code at all.
797 */
798static inline int slab_pre_alloc_hook(struct kmem_cache *s, gfp_t flags)
799{
Christoph Lameterc1d50832010-08-20 12:37:17 -0500800 flags &= gfp_allowed_mask;
Christoph Lameterc016b0b2010-08-20 12:37:16 -0500801 lockdep_trace_alloc(flags);
802 might_sleep_if(flags & __GFP_WAIT);
803
804 return should_failslab(s->objsize, flags, s->flags);
805}
806
807static inline void slab_post_alloc_hook(struct kmem_cache *s, gfp_t flags, void *object)
808{
Christoph Lameterc1d50832010-08-20 12:37:17 -0500809 flags &= gfp_allowed_mask;
Christoph Lameterc016b0b2010-08-20 12:37:16 -0500810 kmemcheck_slab_alloc(s, flags, object, s->objsize);
811 kmemleak_alloc_recursive(object, s->objsize, 1, s->flags, flags);
812}
813
814static inline void slab_free_hook(struct kmem_cache *s, void *x)
815{
816 kmemleak_free_recursive(x, s->flags);
817}
818
819static inline void slab_free_hook_irq(struct kmem_cache *s, void *object)
820{
821 kmemcheck_slab_free(s, object, s->objsize);
822 debug_check_no_locks_freed(object, s->objsize);
823 if (!(s->flags & SLAB_DEBUG_OBJECTS))
824 debug_check_no_obj_freed(object, s->objsize);
825}
826
827/*
Christoph Lameter672bba32007-05-09 02:32:39 -0700828 * Tracking of fully allocated slabs for debugging purposes.
Christoph Lameter643b1132007-05-06 14:49:42 -0700829 */
Christoph Lametere95eed52007-05-06 14:49:44 -0700830static void add_full(struct kmem_cache_node *n, struct page *page)
Christoph Lameter643b1132007-05-06 14:49:42 -0700831{
Christoph Lameter643b1132007-05-06 14:49:42 -0700832 spin_lock(&n->list_lock);
833 list_add(&page->lru, &n->full);
834 spin_unlock(&n->list_lock);
835}
836
837static void remove_full(struct kmem_cache *s, struct page *page)
838{
839 struct kmem_cache_node *n;
840
841 if (!(s->flags & SLAB_STORE_USER))
842 return;
843
844 n = get_node(s, page_to_nid(page));
845
846 spin_lock(&n->list_lock);
847 list_del(&page->lru);
848 spin_unlock(&n->list_lock);
849}
850
Christoph Lameter0f389ec2008-04-14 18:53:02 +0300851/* Tracking of the number of slabs for debugging purposes */
852static inline unsigned long slabs_node(struct kmem_cache *s, int node)
853{
854 struct kmem_cache_node *n = get_node(s, node);
855
856 return atomic_long_read(&n->nr_slabs);
857}
858
Alexander Beregalov26c02cf2009-06-11 14:08:48 +0400859static inline unsigned long node_nr_slabs(struct kmem_cache_node *n)
860{
861 return atomic_long_read(&n->nr_slabs);
862}
863
Christoph Lameter205ab992008-04-14 19:11:40 +0300864static inline void inc_slabs_node(struct kmem_cache *s, int node, int objects)
Christoph Lameter0f389ec2008-04-14 18:53:02 +0300865{
866 struct kmem_cache_node *n = get_node(s, node);
867
868 /*
869 * May be called early in order to allocate a slab for the
870 * kmem_cache_node structure. Solve the chicken-egg
871 * dilemma by deferring the increment of the count during
872 * bootstrap (see early_kmem_cache_node_alloc).
873 */
Christoph Lameter205ab992008-04-14 19:11:40 +0300874 if (!NUMA_BUILD || n) {
Christoph Lameter0f389ec2008-04-14 18:53:02 +0300875 atomic_long_inc(&n->nr_slabs);
Christoph Lameter205ab992008-04-14 19:11:40 +0300876 atomic_long_add(objects, &n->total_objects);
877 }
Christoph Lameter0f389ec2008-04-14 18:53:02 +0300878}
Christoph Lameter205ab992008-04-14 19:11:40 +0300879static inline void dec_slabs_node(struct kmem_cache *s, int node, int objects)
Christoph Lameter0f389ec2008-04-14 18:53:02 +0300880{
881 struct kmem_cache_node *n = get_node(s, node);
882
883 atomic_long_dec(&n->nr_slabs);
Christoph Lameter205ab992008-04-14 19:11:40 +0300884 atomic_long_sub(objects, &n->total_objects);
Christoph Lameter0f389ec2008-04-14 18:53:02 +0300885}
886
887/* Object debug checks for alloc/free paths */
Christoph Lameter3ec09742007-05-16 22:11:00 -0700888static void setup_object_debug(struct kmem_cache *s, struct page *page,
889 void *object)
890{
891 if (!(s->flags & (SLAB_STORE_USER|SLAB_RED_ZONE|__OBJECT_POISON)))
892 return;
893
894 init_object(s, object, 0);
895 init_tracking(s, object);
896}
897
Christoph Lameter15370662010-08-20 12:37:12 -0500898static noinline int alloc_debug_processing(struct kmem_cache *s, struct page *page,
Eduard - Gabriel Munteanuce71e272008-08-19 20:43:25 +0300899 void *object, unsigned long addr)
Christoph Lameter81819f02007-05-06 14:49:36 -0700900{
901 if (!check_slab(s, page))
902 goto bad;
903
Christoph Lameterd692ef62008-02-15 23:45:24 -0800904 if (!on_freelist(s, page, object)) {
Christoph Lameter24922682007-07-17 04:03:18 -0700905 object_err(s, page, object, "Object already allocated");
Christoph Lameter70d71222007-05-06 14:49:47 -0700906 goto bad;
Christoph Lameter81819f02007-05-06 14:49:36 -0700907 }
908
909 if (!check_valid_pointer(s, page, object)) {
910 object_err(s, page, object, "Freelist Pointer check fails");
Christoph Lameter70d71222007-05-06 14:49:47 -0700911 goto bad;
Christoph Lameter81819f02007-05-06 14:49:36 -0700912 }
913
Christoph Lameterd692ef62008-02-15 23:45:24 -0800914 if (!check_object(s, page, object, 0))
Christoph Lameter81819f02007-05-06 14:49:36 -0700915 goto bad;
Christoph Lameter81819f02007-05-06 14:49:36 -0700916
Christoph Lameter3ec09742007-05-16 22:11:00 -0700917 /* Success perform special debug activities for allocs */
918 if (s->flags & SLAB_STORE_USER)
919 set_track(s, object, TRACK_ALLOC, addr);
920 trace(s, page, object, 1);
921 init_object(s, object, 1);
Christoph Lameter81819f02007-05-06 14:49:36 -0700922 return 1;
Christoph Lameter3ec09742007-05-16 22:11:00 -0700923
Christoph Lameter81819f02007-05-06 14:49:36 -0700924bad:
925 if (PageSlab(page)) {
926 /*
927 * If this is a slab page then lets do the best we can
928 * to avoid issues in the future. Marking all objects
Christoph Lameter672bba32007-05-09 02:32:39 -0700929 * as used avoids touching the remaining objects.
Christoph Lameter81819f02007-05-06 14:49:36 -0700930 */
Christoph Lameter24922682007-07-17 04:03:18 -0700931 slab_fix(s, "Marking all objects used");
Christoph Lameter39b26462008-04-14 19:11:30 +0300932 page->inuse = page->objects;
Christoph Lametera973e9d2008-03-01 13:40:44 -0800933 page->freelist = NULL;
Christoph Lameter81819f02007-05-06 14:49:36 -0700934 }
935 return 0;
936}
937
Christoph Lameter15370662010-08-20 12:37:12 -0500938static noinline int free_debug_processing(struct kmem_cache *s,
939 struct page *page, void *object, unsigned long addr)
Christoph Lameter81819f02007-05-06 14:49:36 -0700940{
941 if (!check_slab(s, page))
942 goto fail;
943
944 if (!check_valid_pointer(s, page, object)) {
Christoph Lameter70d71222007-05-06 14:49:47 -0700945 slab_err(s, page, "Invalid object pointer 0x%p", object);
Christoph Lameter81819f02007-05-06 14:49:36 -0700946 goto fail;
947 }
948
949 if (on_freelist(s, page, object)) {
Christoph Lameter24922682007-07-17 04:03:18 -0700950 object_err(s, page, object, "Object already free");
Christoph Lameter81819f02007-05-06 14:49:36 -0700951 goto fail;
952 }
953
954 if (!check_object(s, page, object, 1))
955 return 0;
956
957 if (unlikely(s != page->slab)) {
Ingo Molnar3adbefe2008-02-05 17:57:39 -0800958 if (!PageSlab(page)) {
Christoph Lameter70d71222007-05-06 14:49:47 -0700959 slab_err(s, page, "Attempt to free object(0x%p) "
960 "outside of slab", object);
Ingo Molnar3adbefe2008-02-05 17:57:39 -0800961 } else if (!page->slab) {
Christoph Lameter81819f02007-05-06 14:49:36 -0700962 printk(KERN_ERR
Christoph Lameter70d71222007-05-06 14:49:47 -0700963 "SLUB <none>: no slab for object 0x%p.\n",
Christoph Lameter81819f02007-05-06 14:49:36 -0700964 object);
Christoph Lameter70d71222007-05-06 14:49:47 -0700965 dump_stack();
Pekka Enberg06428782008-01-07 23:20:27 -0800966 } else
Christoph Lameter24922682007-07-17 04:03:18 -0700967 object_err(s, page, object,
968 "page slab pointer corrupt.");
Christoph Lameter81819f02007-05-06 14:49:36 -0700969 goto fail;
970 }
Christoph Lameter3ec09742007-05-16 22:11:00 -0700971
972 /* Special debug activities for freeing objects */
Andy Whitcroft8a380822008-07-23 21:27:18 -0700973 if (!PageSlubFrozen(page) && !page->freelist)
Christoph Lameter3ec09742007-05-16 22:11:00 -0700974 remove_full(s, page);
975 if (s->flags & SLAB_STORE_USER)
976 set_track(s, object, TRACK_FREE, addr);
977 trace(s, page, object, 0);
978 init_object(s, object, 0);
Christoph Lameter81819f02007-05-06 14:49:36 -0700979 return 1;
Christoph Lameter3ec09742007-05-16 22:11:00 -0700980
Christoph Lameter81819f02007-05-06 14:49:36 -0700981fail:
Christoph Lameter24922682007-07-17 04:03:18 -0700982 slab_fix(s, "Object at 0x%p not freed", object);
Christoph Lameter81819f02007-05-06 14:49:36 -0700983 return 0;
984}
985
Christoph Lameter41ecc552007-05-09 02:32:44 -0700986static int __init setup_slub_debug(char *str)
987{
Christoph Lameterf0630ff2007-07-15 23:38:14 -0700988 slub_debug = DEBUG_DEFAULT_FLAGS;
989 if (*str++ != '=' || !*str)
990 /*
991 * No options specified. Switch on full debugging.
992 */
993 goto out;
Christoph Lameter41ecc552007-05-09 02:32:44 -0700994
995 if (*str == ',')
Christoph Lameterf0630ff2007-07-15 23:38:14 -0700996 /*
997 * No options but restriction on slabs. This means full
998 * debugging for slabs matching a pattern.
999 */
1000 goto check_slabs;
1001
David Rientjesfa5ec8a2009-07-07 00:14:14 -07001002 if (tolower(*str) == 'o') {
1003 /*
1004 * Avoid enabling debugging on caches if its minimum order
1005 * would increase as a result.
1006 */
1007 disable_higher_order_debug = 1;
1008 goto out;
1009 }
1010
Christoph Lameterf0630ff2007-07-15 23:38:14 -07001011 slub_debug = 0;
1012 if (*str == '-')
1013 /*
1014 * Switch off all debugging measures.
1015 */
1016 goto out;
1017
1018 /*
1019 * Determine which debug features should be switched on
1020 */
Pekka Enberg06428782008-01-07 23:20:27 -08001021 for (; *str && *str != ','; str++) {
Christoph Lameterf0630ff2007-07-15 23:38:14 -07001022 switch (tolower(*str)) {
1023 case 'f':
1024 slub_debug |= SLAB_DEBUG_FREE;
1025 break;
1026 case 'z':
1027 slub_debug |= SLAB_RED_ZONE;
1028 break;
1029 case 'p':
1030 slub_debug |= SLAB_POISON;
1031 break;
1032 case 'u':
1033 slub_debug |= SLAB_STORE_USER;
1034 break;
1035 case 't':
1036 slub_debug |= SLAB_TRACE;
1037 break;
Dmitry Monakhov4c13dd32010-02-26 09:36:12 +03001038 case 'a':
1039 slub_debug |= SLAB_FAILSLAB;
1040 break;
Christoph Lameterf0630ff2007-07-15 23:38:14 -07001041 default:
1042 printk(KERN_ERR "slub_debug option '%c' "
Pekka Enberg06428782008-01-07 23:20:27 -08001043 "unknown. skipped\n", *str);
Christoph Lameterf0630ff2007-07-15 23:38:14 -07001044 }
1045 }
1046
1047check_slabs:
1048 if (*str == ',')
Christoph Lameter41ecc552007-05-09 02:32:44 -07001049 slub_debug_slabs = str + 1;
Christoph Lameterf0630ff2007-07-15 23:38:14 -07001050out:
Christoph Lameter41ecc552007-05-09 02:32:44 -07001051 return 1;
1052}
1053
1054__setup("slub_debug", setup_slub_debug);
1055
Christoph Lameterba0268a2007-09-11 15:24:11 -07001056static unsigned long kmem_cache_flags(unsigned long objsize,
1057 unsigned long flags, const char *name,
Alexey Dobriyan51cc5062008-07-25 19:45:34 -07001058 void (*ctor)(void *))
Christoph Lameter41ecc552007-05-09 02:32:44 -07001059{
1060 /*
Christoph Lametere1533622008-02-15 23:45:24 -08001061 * Enable debugging if selected on the kernel commandline.
Christoph Lameter41ecc552007-05-09 02:32:44 -07001062 */
Christoph Lametere1533622008-02-15 23:45:24 -08001063 if (slub_debug && (!slub_debug_slabs ||
David Rientjes3de47212009-07-27 18:30:35 -07001064 !strncmp(slub_debug_slabs, name, strlen(slub_debug_slabs))))
1065 flags |= slub_debug;
Christoph Lameterba0268a2007-09-11 15:24:11 -07001066
1067 return flags;
Christoph Lameter41ecc552007-05-09 02:32:44 -07001068}
1069#else
Christoph Lameter3ec09742007-05-16 22:11:00 -07001070static inline void setup_object_debug(struct kmem_cache *s,
1071 struct page *page, void *object) {}
Christoph Lameter41ecc552007-05-09 02:32:44 -07001072
Christoph Lameter3ec09742007-05-16 22:11:00 -07001073static inline int alloc_debug_processing(struct kmem_cache *s,
Eduard - Gabriel Munteanuce71e272008-08-19 20:43:25 +03001074 struct page *page, void *object, unsigned long addr) { return 0; }
Christoph Lameter41ecc552007-05-09 02:32:44 -07001075
Christoph Lameter3ec09742007-05-16 22:11:00 -07001076static inline int free_debug_processing(struct kmem_cache *s,
Eduard - Gabriel Munteanuce71e272008-08-19 20:43:25 +03001077 struct page *page, void *object, unsigned long addr) { return 0; }
Christoph Lameter41ecc552007-05-09 02:32:44 -07001078
Christoph Lameter41ecc552007-05-09 02:32:44 -07001079static inline int slab_pad_check(struct kmem_cache *s, struct page *page)
1080 { return 1; }
1081static inline int check_object(struct kmem_cache *s, struct page *page,
1082 void *object, int active) { return 1; }
Christoph Lameter3ec09742007-05-16 22:11:00 -07001083static inline void add_full(struct kmem_cache_node *n, struct page *page) {}
Christoph Lameterba0268a2007-09-11 15:24:11 -07001084static inline unsigned long kmem_cache_flags(unsigned long objsize,
1085 unsigned long flags, const char *name,
Alexey Dobriyan51cc5062008-07-25 19:45:34 -07001086 void (*ctor)(void *))
Christoph Lameterba0268a2007-09-11 15:24:11 -07001087{
1088 return flags;
1089}
Christoph Lameter41ecc552007-05-09 02:32:44 -07001090#define slub_debug 0
Christoph Lameter0f389ec2008-04-14 18:53:02 +03001091
Ingo Molnarfdaa45e2009-09-15 11:00:26 +02001092#define disable_higher_order_debug 0
1093
Christoph Lameter0f389ec2008-04-14 18:53:02 +03001094static inline unsigned long slabs_node(struct kmem_cache *s, int node)
1095 { return 0; }
Alexander Beregalov26c02cf2009-06-11 14:08:48 +04001096static inline unsigned long node_nr_slabs(struct kmem_cache_node *n)
1097 { return 0; }
Christoph Lameter205ab992008-04-14 19:11:40 +03001098static inline void inc_slabs_node(struct kmem_cache *s, int node,
1099 int objects) {}
1100static inline void dec_slabs_node(struct kmem_cache *s, int node,
1101 int objects) {}
Christoph Lameter7d550c52010-08-25 14:07:16 -05001102
1103static inline int slab_pre_alloc_hook(struct kmem_cache *s, gfp_t flags)
1104 { return 0; }
1105
1106static inline void slab_post_alloc_hook(struct kmem_cache *s, gfp_t flags,
1107 void *object) {}
1108
1109static inline void slab_free_hook(struct kmem_cache *s, void *x) {}
1110
1111static inline void slab_free_hook_irq(struct kmem_cache *s,
1112 void *object) {}
1113
Christoph Lameter41ecc552007-05-09 02:32:44 -07001114#endif
Christoph Lameter205ab992008-04-14 19:11:40 +03001115
Christoph Lameter81819f02007-05-06 14:49:36 -07001116/*
1117 * Slab allocation and freeing
1118 */
Christoph Lameter65c33762008-04-14 19:11:40 +03001119static inline struct page *alloc_slab_page(gfp_t flags, int node,
1120 struct kmem_cache_order_objects oo)
1121{
1122 int order = oo_order(oo);
1123
Vegard Nossumb1eeab62008-11-25 16:55:53 +01001124 flags |= __GFP_NOTRACK;
1125
Christoph Lameter2154a332010-07-09 14:07:10 -05001126 if (node == NUMA_NO_NODE)
Christoph Lameter65c33762008-04-14 19:11:40 +03001127 return alloc_pages(flags, order);
1128 else
Minchan Kim6b65aaf2010-04-14 23:58:36 +09001129 return alloc_pages_exact_node(node, flags, order);
Christoph Lameter65c33762008-04-14 19:11:40 +03001130}
1131
Christoph Lameter81819f02007-05-06 14:49:36 -07001132static struct page *allocate_slab(struct kmem_cache *s, gfp_t flags, int node)
1133{
Pekka Enberg06428782008-01-07 23:20:27 -08001134 struct page *page;
Christoph Lameter834f3d12008-04-14 19:11:31 +03001135 struct kmem_cache_order_objects oo = s->oo;
Pekka Enbergba522702009-06-24 21:59:51 +03001136 gfp_t alloc_gfp;
Christoph Lameter81819f02007-05-06 14:49:36 -07001137
Christoph Lameterb7a49f02008-02-14 14:21:32 -08001138 flags |= s->allocflags;
Mel Gormane12ba742007-10-16 01:25:52 -07001139
Pekka Enbergba522702009-06-24 21:59:51 +03001140 /*
1141 * Let the initial higher-order allocation fail under memory pressure
1142 * so we fall-back to the minimum order allocation.
1143 */
1144 alloc_gfp = (flags | __GFP_NOWARN | __GFP_NORETRY) & ~__GFP_NOFAIL;
1145
1146 page = alloc_slab_page(alloc_gfp, node, oo);
Christoph Lameter65c33762008-04-14 19:11:40 +03001147 if (unlikely(!page)) {
1148 oo = s->min;
1149 /*
1150 * Allocation may have failed due to fragmentation.
1151 * Try a lower order alloc if possible
1152 */
1153 page = alloc_slab_page(flags, node, oo);
1154 if (!page)
1155 return NULL;
Christoph Lameter81819f02007-05-06 14:49:36 -07001156
Christoph Lameter84e554e2009-12-18 16:26:23 -06001157 stat(s, ORDER_FALLBACK);
Christoph Lameter65c33762008-04-14 19:11:40 +03001158 }
Vegard Nossum5a896d92008-04-04 00:54:48 +02001159
1160 if (kmemcheck_enabled
Amerigo Wang5086c382009-08-19 21:44:13 +03001161 && !(s->flags & (SLAB_NOTRACK | DEBUG_DEFAULT_FLAGS))) {
Vegard Nossumb1eeab62008-11-25 16:55:53 +01001162 int pages = 1 << oo_order(oo);
1163
1164 kmemcheck_alloc_shadow(page, oo_order(oo), flags, node);
1165
1166 /*
1167 * Objects from caches that have a constructor don't get
1168 * cleared when they're allocated, so we need to do it here.
1169 */
1170 if (s->ctor)
1171 kmemcheck_mark_uninitialized_pages(page, pages);
1172 else
1173 kmemcheck_mark_unallocated_pages(page, pages);
Vegard Nossum5a896d92008-04-04 00:54:48 +02001174 }
1175
Christoph Lameter834f3d12008-04-14 19:11:31 +03001176 page->objects = oo_objects(oo);
Christoph Lameter81819f02007-05-06 14:49:36 -07001177 mod_zone_page_state(page_zone(page),
1178 (s->flags & SLAB_RECLAIM_ACCOUNT) ?
1179 NR_SLAB_RECLAIMABLE : NR_SLAB_UNRECLAIMABLE,
Christoph Lameter65c33762008-04-14 19:11:40 +03001180 1 << oo_order(oo));
Christoph Lameter81819f02007-05-06 14:49:36 -07001181
1182 return page;
1183}
1184
1185static void setup_object(struct kmem_cache *s, struct page *page,
1186 void *object)
1187{
Christoph Lameter3ec09742007-05-16 22:11:00 -07001188 setup_object_debug(s, page, object);
Christoph Lameter4f104932007-05-06 14:50:17 -07001189 if (unlikely(s->ctor))
Alexey Dobriyan51cc5062008-07-25 19:45:34 -07001190 s->ctor(object);
Christoph Lameter81819f02007-05-06 14:49:36 -07001191}
1192
1193static struct page *new_slab(struct kmem_cache *s, gfp_t flags, int node)
1194{
1195 struct page *page;
Christoph Lameter81819f02007-05-06 14:49:36 -07001196 void *start;
Christoph Lameter81819f02007-05-06 14:49:36 -07001197 void *last;
1198 void *p;
1199
Christoph Lameter6cb06222007-10-16 01:25:41 -07001200 BUG_ON(flags & GFP_SLAB_BUG_MASK);
Christoph Lameter81819f02007-05-06 14:49:36 -07001201
Christoph Lameter6cb06222007-10-16 01:25:41 -07001202 page = allocate_slab(s,
1203 flags & (GFP_RECLAIM_MASK | GFP_CONSTRAINT_MASK), node);
Christoph Lameter81819f02007-05-06 14:49:36 -07001204 if (!page)
1205 goto out;
1206
Christoph Lameter205ab992008-04-14 19:11:40 +03001207 inc_slabs_node(s, page_to_nid(page), page->objects);
Christoph Lameter81819f02007-05-06 14:49:36 -07001208 page->slab = s;
1209 page->flags |= 1 << PG_slab;
Christoph Lameter81819f02007-05-06 14:49:36 -07001210
1211 start = page_address(page);
Christoph Lameter81819f02007-05-06 14:49:36 -07001212
1213 if (unlikely(s->flags & SLAB_POISON))
Christoph Lameter834f3d12008-04-14 19:11:31 +03001214 memset(start, POISON_INUSE, PAGE_SIZE << compound_order(page));
Christoph Lameter81819f02007-05-06 14:49:36 -07001215
1216 last = start;
Christoph Lameter224a88b2008-04-14 19:11:31 +03001217 for_each_object(p, s, start, page->objects) {
Christoph Lameter81819f02007-05-06 14:49:36 -07001218 setup_object(s, page, last);
1219 set_freepointer(s, last, p);
1220 last = p;
1221 }
1222 setup_object(s, page, last);
Christoph Lametera973e9d2008-03-01 13:40:44 -08001223 set_freepointer(s, last, NULL);
Christoph Lameter81819f02007-05-06 14:49:36 -07001224
1225 page->freelist = start;
1226 page->inuse = 0;
1227out:
Christoph Lameter81819f02007-05-06 14:49:36 -07001228 return page;
1229}
1230
1231static void __free_slab(struct kmem_cache *s, struct page *page)
1232{
Christoph Lameter834f3d12008-04-14 19:11:31 +03001233 int order = compound_order(page);
1234 int pages = 1 << order;
Christoph Lameter81819f02007-05-06 14:49:36 -07001235
Christoph Lameteraf537b02010-07-09 14:07:14 -05001236 if (kmem_cache_debug(s)) {
Christoph Lameter81819f02007-05-06 14:49:36 -07001237 void *p;
1238
1239 slab_pad_check(s, page);
Christoph Lameter224a88b2008-04-14 19:11:31 +03001240 for_each_object(p, s, page_address(page),
1241 page->objects)
Christoph Lameter81819f02007-05-06 14:49:36 -07001242 check_object(s, page, p, 0);
Christoph Lameter81819f02007-05-06 14:49:36 -07001243 }
1244
Vegard Nossumb1eeab62008-11-25 16:55:53 +01001245 kmemcheck_free_shadow(page, compound_order(page));
Vegard Nossum5a896d92008-04-04 00:54:48 +02001246
Christoph Lameter81819f02007-05-06 14:49:36 -07001247 mod_zone_page_state(page_zone(page),
1248 (s->flags & SLAB_RECLAIM_ACCOUNT) ?
1249 NR_SLAB_RECLAIMABLE : NR_SLAB_UNRECLAIMABLE,
Pekka Enberg06428782008-01-07 23:20:27 -08001250 -pages);
Christoph Lameter81819f02007-05-06 14:49:36 -07001251
Christoph Lameter49bd5222008-04-14 18:52:18 +03001252 __ClearPageSlab(page);
1253 reset_page_mapcount(page);
Nick Piggin1eb5ac62009-05-05 19:13:44 +10001254 if (current->reclaim_state)
1255 current->reclaim_state->reclaimed_slab += pages;
Christoph Lameter834f3d12008-04-14 19:11:31 +03001256 __free_pages(page, order);
Christoph Lameter81819f02007-05-06 14:49:36 -07001257}
1258
1259static void rcu_free_slab(struct rcu_head *h)
1260{
1261 struct page *page;
1262
1263 page = container_of((struct list_head *)h, struct page, lru);
1264 __free_slab(page->slab, page);
1265}
1266
1267static void free_slab(struct kmem_cache *s, struct page *page)
1268{
1269 if (unlikely(s->flags & SLAB_DESTROY_BY_RCU)) {
1270 /*
1271 * RCU free overloads the RCU head over the LRU
1272 */
1273 struct rcu_head *head = (void *)&page->lru;
1274
1275 call_rcu(head, rcu_free_slab);
1276 } else
1277 __free_slab(s, page);
1278}
1279
1280static void discard_slab(struct kmem_cache *s, struct page *page)
1281{
Christoph Lameter205ab992008-04-14 19:11:40 +03001282 dec_slabs_node(s, page_to_nid(page), page->objects);
Christoph Lameter81819f02007-05-06 14:49:36 -07001283 free_slab(s, page);
1284}
1285
1286/*
1287 * Per slab locking using the pagelock
1288 */
1289static __always_inline void slab_lock(struct page *page)
1290{
1291 bit_spin_lock(PG_locked, &page->flags);
1292}
1293
1294static __always_inline void slab_unlock(struct page *page)
1295{
Nick Piggina76d3542008-01-07 23:20:27 -08001296 __bit_spin_unlock(PG_locked, &page->flags);
Christoph Lameter81819f02007-05-06 14:49:36 -07001297}
1298
1299static __always_inline int slab_trylock(struct page *page)
1300{
1301 int rc = 1;
1302
1303 rc = bit_spin_trylock(PG_locked, &page->flags);
1304 return rc;
1305}
1306
1307/*
1308 * Management of partially allocated slabs
1309 */
Christoph Lameter7c2e1322008-01-07 23:20:27 -08001310static void add_partial(struct kmem_cache_node *n,
1311 struct page *page, int tail)
Christoph Lameter81819f02007-05-06 14:49:36 -07001312{
Christoph Lametere95eed52007-05-06 14:49:44 -07001313 spin_lock(&n->list_lock);
1314 n->nr_partial++;
Christoph Lameter7c2e1322008-01-07 23:20:27 -08001315 if (tail)
1316 list_add_tail(&page->lru, &n->partial);
1317 else
1318 list_add(&page->lru, &n->partial);
Christoph Lameter81819f02007-05-06 14:49:36 -07001319 spin_unlock(&n->list_lock);
1320}
1321
Christoph Lameter0121c6192008-04-29 16:11:12 -07001322static void remove_partial(struct kmem_cache *s, struct page *page)
Christoph Lameter81819f02007-05-06 14:49:36 -07001323{
1324 struct kmem_cache_node *n = get_node(s, page_to_nid(page));
1325
1326 spin_lock(&n->list_lock);
1327 list_del(&page->lru);
1328 n->nr_partial--;
1329 spin_unlock(&n->list_lock);
1330}
1331
1332/*
Christoph Lameter672bba32007-05-09 02:32:39 -07001333 * Lock slab and remove from the partial list.
Christoph Lameter81819f02007-05-06 14:49:36 -07001334 *
Christoph Lameter672bba32007-05-09 02:32:39 -07001335 * Must hold list_lock.
Christoph Lameter81819f02007-05-06 14:49:36 -07001336 */
Christoph Lameter0121c6192008-04-29 16:11:12 -07001337static inline int lock_and_freeze_slab(struct kmem_cache_node *n,
1338 struct page *page)
Christoph Lameter81819f02007-05-06 14:49:36 -07001339{
1340 if (slab_trylock(page)) {
1341 list_del(&page->lru);
1342 n->nr_partial--;
Andy Whitcroft8a380822008-07-23 21:27:18 -07001343 __SetPageSlubFrozen(page);
Christoph Lameter81819f02007-05-06 14:49:36 -07001344 return 1;
1345 }
1346 return 0;
1347}
1348
1349/*
Christoph Lameter672bba32007-05-09 02:32:39 -07001350 * Try to allocate a partial slab from a specific node.
Christoph Lameter81819f02007-05-06 14:49:36 -07001351 */
1352static struct page *get_partial_node(struct kmem_cache_node *n)
1353{
1354 struct page *page;
1355
1356 /*
1357 * Racy check. If we mistakenly see no partial slabs then we
1358 * just allocate an empty slab. If we mistakenly try to get a
Christoph Lameter672bba32007-05-09 02:32:39 -07001359 * partial slab and there is none available then get_partials()
1360 * will return NULL.
Christoph Lameter81819f02007-05-06 14:49:36 -07001361 */
1362 if (!n || !n->nr_partial)
1363 return NULL;
1364
1365 spin_lock(&n->list_lock);
1366 list_for_each_entry(page, &n->partial, lru)
Christoph Lameter4b6f0752007-05-16 22:10:53 -07001367 if (lock_and_freeze_slab(n, page))
Christoph Lameter81819f02007-05-06 14:49:36 -07001368 goto out;
1369 page = NULL;
1370out:
1371 spin_unlock(&n->list_lock);
1372 return page;
1373}
1374
1375/*
Christoph Lameter672bba32007-05-09 02:32:39 -07001376 * Get a page from somewhere. Search in increasing NUMA distances.
Christoph Lameter81819f02007-05-06 14:49:36 -07001377 */
1378static struct page *get_any_partial(struct kmem_cache *s, gfp_t flags)
1379{
1380#ifdef CONFIG_NUMA
1381 struct zonelist *zonelist;
Mel Gormandd1a2392008-04-28 02:12:17 -07001382 struct zoneref *z;
Mel Gorman54a6eb52008-04-28 02:12:16 -07001383 struct zone *zone;
1384 enum zone_type high_zoneidx = gfp_zone(flags);
Christoph Lameter81819f02007-05-06 14:49:36 -07001385 struct page *page;
1386
1387 /*
Christoph Lameter672bba32007-05-09 02:32:39 -07001388 * The defrag ratio allows a configuration of the tradeoffs between
1389 * inter node defragmentation and node local allocations. A lower
1390 * defrag_ratio increases the tendency to do local allocations
1391 * instead of attempting to obtain partial slabs from other nodes.
Christoph Lameter81819f02007-05-06 14:49:36 -07001392 *
Christoph Lameter672bba32007-05-09 02:32:39 -07001393 * If the defrag_ratio is set to 0 then kmalloc() always
1394 * returns node local objects. If the ratio is higher then kmalloc()
1395 * may return off node objects because partial slabs are obtained
1396 * from other nodes and filled up.
Christoph Lameter81819f02007-05-06 14:49:36 -07001397 *
Christoph Lameter6446faa2008-02-15 23:45:26 -08001398 * If /sys/kernel/slab/xx/defrag_ratio is set to 100 (which makes
Christoph Lameter672bba32007-05-09 02:32:39 -07001399 * defrag_ratio = 1000) then every (well almost) allocation will
1400 * first attempt to defrag slab caches on other nodes. This means
1401 * scanning over all nodes to look for partial slabs which may be
1402 * expensive if we do it every time we are trying to find a slab
1403 * with available objects.
Christoph Lameter81819f02007-05-06 14:49:36 -07001404 */
Christoph Lameter98246012008-01-07 23:20:26 -08001405 if (!s->remote_node_defrag_ratio ||
1406 get_cycles() % 1024 > s->remote_node_defrag_ratio)
Christoph Lameter81819f02007-05-06 14:49:36 -07001407 return NULL;
1408
Miao Xiec0ff7452010-05-24 14:32:08 -07001409 get_mems_allowed();
Mel Gorman0e884602008-04-28 02:12:14 -07001410 zonelist = node_zonelist(slab_node(current->mempolicy), flags);
Mel Gorman54a6eb52008-04-28 02:12:16 -07001411 for_each_zone_zonelist(zone, z, zonelist, high_zoneidx) {
Christoph Lameter81819f02007-05-06 14:49:36 -07001412 struct kmem_cache_node *n;
1413
Mel Gorman54a6eb52008-04-28 02:12:16 -07001414 n = get_node(s, zone_to_nid(zone));
Christoph Lameter81819f02007-05-06 14:49:36 -07001415
Mel Gorman54a6eb52008-04-28 02:12:16 -07001416 if (n && cpuset_zone_allowed_hardwall(zone, flags) &&
David Rientjes3b89d7d2009-02-22 17:40:07 -08001417 n->nr_partial > s->min_partial) {
Christoph Lameter81819f02007-05-06 14:49:36 -07001418 page = get_partial_node(n);
Miao Xiec0ff7452010-05-24 14:32:08 -07001419 if (page) {
1420 put_mems_allowed();
Christoph Lameter81819f02007-05-06 14:49:36 -07001421 return page;
Miao Xiec0ff7452010-05-24 14:32:08 -07001422 }
Christoph Lameter81819f02007-05-06 14:49:36 -07001423 }
1424 }
Miao Xiec0ff7452010-05-24 14:32:08 -07001425 put_mems_allowed();
Christoph Lameter81819f02007-05-06 14:49:36 -07001426#endif
1427 return NULL;
1428}
1429
1430/*
1431 * Get a partial page, lock it and return it.
1432 */
1433static struct page *get_partial(struct kmem_cache *s, gfp_t flags, int node)
1434{
1435 struct page *page;
Christoph Lameter2154a332010-07-09 14:07:10 -05001436 int searchnode = (node == NUMA_NO_NODE) ? numa_node_id() : node;
Christoph Lameter81819f02007-05-06 14:49:36 -07001437
1438 page = get_partial_node(get_node(s, searchnode));
Christoph Lameterbc6488e2010-07-26 10:41:14 -05001439 if (page || node != -1)
Christoph Lameter81819f02007-05-06 14:49:36 -07001440 return page;
1441
1442 return get_any_partial(s, flags);
1443}
1444
1445/*
1446 * Move a page back to the lists.
1447 *
1448 * Must be called with the slab lock held.
1449 *
1450 * On exit the slab lock will have been dropped.
1451 */
Christoph Lameter7c2e1322008-01-07 23:20:27 -08001452static void unfreeze_slab(struct kmem_cache *s, struct page *page, int tail)
Christoph Lameter81819f02007-05-06 14:49:36 -07001453{
Christoph Lametere95eed52007-05-06 14:49:44 -07001454 struct kmem_cache_node *n = get_node(s, page_to_nid(page));
1455
Andy Whitcroft8a380822008-07-23 21:27:18 -07001456 __ClearPageSlubFrozen(page);
Christoph Lameter81819f02007-05-06 14:49:36 -07001457 if (page->inuse) {
Christoph Lametere95eed52007-05-06 14:49:44 -07001458
Christoph Lametera973e9d2008-03-01 13:40:44 -08001459 if (page->freelist) {
Christoph Lameter7c2e1322008-01-07 23:20:27 -08001460 add_partial(n, page, tail);
Christoph Lameter84e554e2009-12-18 16:26:23 -06001461 stat(s, tail ? DEACTIVATE_TO_TAIL : DEACTIVATE_TO_HEAD);
Christoph Lameter8ff12cf2008-02-07 17:47:41 -08001462 } else {
Christoph Lameter84e554e2009-12-18 16:26:23 -06001463 stat(s, DEACTIVATE_FULL);
Christoph Lameteraf537b02010-07-09 14:07:14 -05001464 if (kmem_cache_debug(s) && (s->flags & SLAB_STORE_USER))
Christoph Lameter8ff12cf2008-02-07 17:47:41 -08001465 add_full(n, page);
1466 }
Christoph Lameter81819f02007-05-06 14:49:36 -07001467 slab_unlock(page);
1468 } else {
Christoph Lameter84e554e2009-12-18 16:26:23 -06001469 stat(s, DEACTIVATE_EMPTY);
David Rientjes3b89d7d2009-02-22 17:40:07 -08001470 if (n->nr_partial < s->min_partial) {
Christoph Lametere95eed52007-05-06 14:49:44 -07001471 /*
Christoph Lameter672bba32007-05-09 02:32:39 -07001472 * Adding an empty slab to the partial slabs in order
1473 * to avoid page allocator overhead. This slab needs
1474 * to come after the other slabs with objects in
Christoph Lameter6446faa2008-02-15 23:45:26 -08001475 * so that the others get filled first. That way the
1476 * size of the partial list stays small.
1477 *
Christoph Lameter0121c6192008-04-29 16:11:12 -07001478 * kmem_cache_shrink can reclaim any empty slabs from
1479 * the partial list.
Christoph Lametere95eed52007-05-06 14:49:44 -07001480 */
Christoph Lameter7c2e1322008-01-07 23:20:27 -08001481 add_partial(n, page, 1);
Christoph Lametere95eed52007-05-06 14:49:44 -07001482 slab_unlock(page);
1483 } else {
1484 slab_unlock(page);
Christoph Lameter84e554e2009-12-18 16:26:23 -06001485 stat(s, FREE_SLAB);
Christoph Lametere95eed52007-05-06 14:49:44 -07001486 discard_slab(s, page);
1487 }
Christoph Lameter81819f02007-05-06 14:49:36 -07001488 }
1489}
1490
1491/*
1492 * Remove the cpu slab
1493 */
Christoph Lameterdfb4f092007-10-16 01:26:05 -07001494static void deactivate_slab(struct kmem_cache *s, struct kmem_cache_cpu *c)
Christoph Lameter81819f02007-05-06 14:49:36 -07001495{
Christoph Lameterdfb4f092007-10-16 01:26:05 -07001496 struct page *page = c->page;
Christoph Lameter7c2e1322008-01-07 23:20:27 -08001497 int tail = 1;
Christoph Lameter8ff12cf2008-02-07 17:47:41 -08001498
Christoph Lameterb773ad72008-03-04 11:10:17 -08001499 if (page->freelist)
Christoph Lameter84e554e2009-12-18 16:26:23 -06001500 stat(s, DEACTIVATE_REMOTE_FREES);
Christoph Lameter894b8782007-05-10 03:15:16 -07001501 /*
Christoph Lameter6446faa2008-02-15 23:45:26 -08001502 * Merge cpu freelist into slab freelist. Typically we get here
Christoph Lameter894b8782007-05-10 03:15:16 -07001503 * because both freelists are empty. So this is unlikely
1504 * to occur.
1505 */
Christoph Lametera973e9d2008-03-01 13:40:44 -08001506 while (unlikely(c->freelist)) {
Christoph Lameter894b8782007-05-10 03:15:16 -07001507 void **object;
1508
Christoph Lameter7c2e1322008-01-07 23:20:27 -08001509 tail = 0; /* Hot objects. Put the slab first */
1510
Christoph Lameter894b8782007-05-10 03:15:16 -07001511 /* Retrieve object from cpu_freelist */
Christoph Lameterdfb4f092007-10-16 01:26:05 -07001512 object = c->freelist;
Christoph Lameterff120592009-12-18 16:26:22 -06001513 c->freelist = get_freepointer(s, c->freelist);
Christoph Lameter894b8782007-05-10 03:15:16 -07001514
1515 /* And put onto the regular freelist */
Christoph Lameterff120592009-12-18 16:26:22 -06001516 set_freepointer(s, object, page->freelist);
Christoph Lameter894b8782007-05-10 03:15:16 -07001517 page->freelist = object;
1518 page->inuse--;
1519 }
Christoph Lameterdfb4f092007-10-16 01:26:05 -07001520 c->page = NULL;
Christoph Lameter7c2e1322008-01-07 23:20:27 -08001521 unfreeze_slab(s, page, tail);
Christoph Lameter81819f02007-05-06 14:49:36 -07001522}
1523
Christoph Lameterdfb4f092007-10-16 01:26:05 -07001524static inline void flush_slab(struct kmem_cache *s, struct kmem_cache_cpu *c)
Christoph Lameter81819f02007-05-06 14:49:36 -07001525{
Christoph Lameter84e554e2009-12-18 16:26:23 -06001526 stat(s, CPUSLAB_FLUSH);
Christoph Lameterdfb4f092007-10-16 01:26:05 -07001527 slab_lock(c->page);
1528 deactivate_slab(s, c);
Christoph Lameter81819f02007-05-06 14:49:36 -07001529}
1530
1531/*
1532 * Flush cpu slab.
Christoph Lameter6446faa2008-02-15 23:45:26 -08001533 *
Christoph Lameter81819f02007-05-06 14:49:36 -07001534 * Called from IPI handler with interrupts disabled.
1535 */
Christoph Lameter0c710012007-07-17 04:03:24 -07001536static inline void __flush_cpu_slab(struct kmem_cache *s, int cpu)
Christoph Lameter81819f02007-05-06 14:49:36 -07001537{
Christoph Lameter9dfc6e62009-12-18 16:26:20 -06001538 struct kmem_cache_cpu *c = per_cpu_ptr(s->cpu_slab, cpu);
Christoph Lameter81819f02007-05-06 14:49:36 -07001539
Christoph Lameterdfb4f092007-10-16 01:26:05 -07001540 if (likely(c && c->page))
1541 flush_slab(s, c);
Christoph Lameter81819f02007-05-06 14:49:36 -07001542}
1543
1544static void flush_cpu_slab(void *d)
1545{
1546 struct kmem_cache *s = d;
Christoph Lameter81819f02007-05-06 14:49:36 -07001547
Christoph Lameterdfb4f092007-10-16 01:26:05 -07001548 __flush_cpu_slab(s, smp_processor_id());
Christoph Lameter81819f02007-05-06 14:49:36 -07001549}
1550
1551static void flush_all(struct kmem_cache *s)
1552{
Jens Axboe15c8b6c2008-05-09 09:39:44 +02001553 on_each_cpu(flush_cpu_slab, s, 1);
Christoph Lameter81819f02007-05-06 14:49:36 -07001554}
1555
1556/*
Christoph Lameterdfb4f092007-10-16 01:26:05 -07001557 * Check if the objects in a per cpu structure fit numa
1558 * locality expectations.
1559 */
1560static inline int node_match(struct kmem_cache_cpu *c, int node)
1561{
1562#ifdef CONFIG_NUMA
Christoph Lameter2154a332010-07-09 14:07:10 -05001563 if (node != NUMA_NO_NODE && c->node != node)
Christoph Lameterdfb4f092007-10-16 01:26:05 -07001564 return 0;
1565#endif
1566 return 1;
1567}
1568
Pekka Enberg781b2ba2009-06-10 18:50:32 +03001569static int count_free(struct page *page)
1570{
1571 return page->objects - page->inuse;
1572}
1573
1574static unsigned long count_partial(struct kmem_cache_node *n,
1575 int (*get_count)(struct page *))
1576{
1577 unsigned long flags;
1578 unsigned long x = 0;
1579 struct page *page;
1580
1581 spin_lock_irqsave(&n->list_lock, flags);
1582 list_for_each_entry(page, &n->partial, lru)
1583 x += get_count(page);
1584 spin_unlock_irqrestore(&n->list_lock, flags);
1585 return x;
1586}
1587
Alexander Beregalov26c02cf2009-06-11 14:08:48 +04001588static inline unsigned long node_nr_objs(struct kmem_cache_node *n)
1589{
1590#ifdef CONFIG_SLUB_DEBUG
1591 return atomic_long_read(&n->total_objects);
1592#else
1593 return 0;
1594#endif
1595}
1596
Pekka Enberg781b2ba2009-06-10 18:50:32 +03001597static noinline void
1598slab_out_of_memory(struct kmem_cache *s, gfp_t gfpflags, int nid)
1599{
1600 int node;
1601
1602 printk(KERN_WARNING
1603 "SLUB: Unable to allocate memory on node %d (gfp=0x%x)\n",
1604 nid, gfpflags);
1605 printk(KERN_WARNING " cache: %s, object size: %d, buffer size: %d, "
1606 "default order: %d, min order: %d\n", s->name, s->objsize,
1607 s->size, oo_order(s->oo), oo_order(s->min));
1608
David Rientjesfa5ec8a2009-07-07 00:14:14 -07001609 if (oo_order(s->min) > get_order(s->objsize))
1610 printk(KERN_WARNING " %s debugging increased min order, use "
1611 "slub_debug=O to disable.\n", s->name);
1612
Pekka Enberg781b2ba2009-06-10 18:50:32 +03001613 for_each_online_node(node) {
1614 struct kmem_cache_node *n = get_node(s, node);
1615 unsigned long nr_slabs;
1616 unsigned long nr_objs;
1617 unsigned long nr_free;
1618
1619 if (!n)
1620 continue;
1621
Alexander Beregalov26c02cf2009-06-11 14:08:48 +04001622 nr_free = count_partial(n, count_free);
1623 nr_slabs = node_nr_slabs(n);
1624 nr_objs = node_nr_objs(n);
Pekka Enberg781b2ba2009-06-10 18:50:32 +03001625
1626 printk(KERN_WARNING
1627 " node %d: slabs: %ld, objs: %ld, free: %ld\n",
1628 node, nr_slabs, nr_objs, nr_free);
1629 }
1630}
1631
Christoph Lameterdfb4f092007-10-16 01:26:05 -07001632/*
Christoph Lameter894b8782007-05-10 03:15:16 -07001633 * Slow path. The lockless freelist is empty or we need to perform
1634 * debugging duties.
Christoph Lameter81819f02007-05-06 14:49:36 -07001635 *
Christoph Lameter894b8782007-05-10 03:15:16 -07001636 * Interrupts are disabled.
Christoph Lameter81819f02007-05-06 14:49:36 -07001637 *
Christoph Lameter894b8782007-05-10 03:15:16 -07001638 * Processing is still very fast if new objects have been freed to the
1639 * regular freelist. In that case we simply take over the regular freelist
1640 * as the lockless freelist and zap the regular freelist.
Christoph Lameter81819f02007-05-06 14:49:36 -07001641 *
Christoph Lameter894b8782007-05-10 03:15:16 -07001642 * If that is not working then we fall back to the partial lists. We take the
1643 * first element of the freelist as the object to allocate now and move the
1644 * rest of the freelist to the lockless freelist.
1645 *
1646 * And if we were unable to get a new slab from the partial slab lists then
Christoph Lameter6446faa2008-02-15 23:45:26 -08001647 * we need to allocate a new slab. This is the slowest path since it involves
1648 * a call to the page allocator and the setup of a new slab.
Christoph Lameter81819f02007-05-06 14:49:36 -07001649 */
Eduard - Gabriel Munteanuce71e272008-08-19 20:43:25 +03001650static void *__slab_alloc(struct kmem_cache *s, gfp_t gfpflags, int node,
1651 unsigned long addr, struct kmem_cache_cpu *c)
Christoph Lameter81819f02007-05-06 14:49:36 -07001652{
Christoph Lameter81819f02007-05-06 14:49:36 -07001653 void **object;
Christoph Lameterdfb4f092007-10-16 01:26:05 -07001654 struct page *new;
Christoph Lameter81819f02007-05-06 14:49:36 -07001655
Linus Torvaldse72e9c22008-03-27 20:56:33 -07001656 /* We handle __GFP_ZERO in the caller */
1657 gfpflags &= ~__GFP_ZERO;
1658
Christoph Lameterdfb4f092007-10-16 01:26:05 -07001659 if (!c->page)
Christoph Lameter81819f02007-05-06 14:49:36 -07001660 goto new_slab;
1661
Christoph Lameterdfb4f092007-10-16 01:26:05 -07001662 slab_lock(c->page);
1663 if (unlikely(!node_match(c, node)))
Christoph Lameter81819f02007-05-06 14:49:36 -07001664 goto another_slab;
Christoph Lameter6446faa2008-02-15 23:45:26 -08001665
Christoph Lameter84e554e2009-12-18 16:26:23 -06001666 stat(s, ALLOC_REFILL);
Christoph Lameter6446faa2008-02-15 23:45:26 -08001667
Christoph Lameter894b8782007-05-10 03:15:16 -07001668load_freelist:
Christoph Lameterdfb4f092007-10-16 01:26:05 -07001669 object = c->page->freelist;
Christoph Lametera973e9d2008-03-01 13:40:44 -08001670 if (unlikely(!object))
Christoph Lameter81819f02007-05-06 14:49:36 -07001671 goto another_slab;
Christoph Lameteraf537b02010-07-09 14:07:14 -05001672 if (kmem_cache_debug(s))
Christoph Lameter81819f02007-05-06 14:49:36 -07001673 goto debug;
1674
Christoph Lameterff120592009-12-18 16:26:22 -06001675 c->freelist = get_freepointer(s, object);
Christoph Lameter39b26462008-04-14 19:11:30 +03001676 c->page->inuse = c->page->objects;
Christoph Lametera973e9d2008-03-01 13:40:44 -08001677 c->page->freelist = NULL;
Christoph Lameterdfb4f092007-10-16 01:26:05 -07001678 c->node = page_to_nid(c->page);
Christoph Lameter1f842602008-01-07 23:20:30 -08001679unlock_out:
Christoph Lameterdfb4f092007-10-16 01:26:05 -07001680 slab_unlock(c->page);
Christoph Lameter84e554e2009-12-18 16:26:23 -06001681 stat(s, ALLOC_SLOWPATH);
Christoph Lameter81819f02007-05-06 14:49:36 -07001682 return object;
1683
1684another_slab:
Christoph Lameterdfb4f092007-10-16 01:26:05 -07001685 deactivate_slab(s, c);
Christoph Lameter81819f02007-05-06 14:49:36 -07001686
1687new_slab:
Christoph Lameterdfb4f092007-10-16 01:26:05 -07001688 new = get_partial(s, gfpflags, node);
1689 if (new) {
1690 c->page = new;
Christoph Lameter84e554e2009-12-18 16:26:23 -06001691 stat(s, ALLOC_FROM_PARTIAL);
Christoph Lameter894b8782007-05-10 03:15:16 -07001692 goto load_freelist;
Christoph Lameter81819f02007-05-06 14:49:36 -07001693 }
1694
Christoph Lameterc1d50832010-08-20 12:37:17 -05001695 gfpflags &= gfp_allowed_mask;
Christoph Lameterb811c202007-10-16 23:25:51 -07001696 if (gfpflags & __GFP_WAIT)
1697 local_irq_enable();
1698
Christoph Lameterdfb4f092007-10-16 01:26:05 -07001699 new = new_slab(s, gfpflags, node);
Christoph Lameterb811c202007-10-16 23:25:51 -07001700
1701 if (gfpflags & __GFP_WAIT)
1702 local_irq_disable();
1703
Christoph Lameterdfb4f092007-10-16 01:26:05 -07001704 if (new) {
Christoph Lameter9dfc6e62009-12-18 16:26:20 -06001705 c = __this_cpu_ptr(s->cpu_slab);
Christoph Lameter84e554e2009-12-18 16:26:23 -06001706 stat(s, ALLOC_SLAB);
Christoph Lameter05aa3452007-11-05 11:31:58 -08001707 if (c->page)
Christoph Lameterdfb4f092007-10-16 01:26:05 -07001708 flush_slab(s, c);
Christoph Lameterdfb4f092007-10-16 01:26:05 -07001709 slab_lock(new);
Andy Whitcroft8a380822008-07-23 21:27:18 -07001710 __SetPageSlubFrozen(new);
Christoph Lameterdfb4f092007-10-16 01:26:05 -07001711 c->page = new;
Christoph Lameter4b6f0752007-05-16 22:10:53 -07001712 goto load_freelist;
Christoph Lameter81819f02007-05-06 14:49:36 -07001713 }
Pekka Enberg95f85982009-06-11 16:18:09 +03001714 if (!(gfpflags & __GFP_NOWARN) && printk_ratelimit())
1715 slab_out_of_memory(s, gfpflags, node);
Christoph Lameter71c7a062008-02-14 14:28:01 -08001716 return NULL;
Christoph Lameter81819f02007-05-06 14:49:36 -07001717debug:
Christoph Lameterdfb4f092007-10-16 01:26:05 -07001718 if (!alloc_debug_processing(s, c->page, object, addr))
Christoph Lameter81819f02007-05-06 14:49:36 -07001719 goto another_slab;
Christoph Lameter894b8782007-05-10 03:15:16 -07001720
Christoph Lameterdfb4f092007-10-16 01:26:05 -07001721 c->page->inuse++;
Christoph Lameterff120592009-12-18 16:26:22 -06001722 c->page->freelist = get_freepointer(s, object);
Christoph Lameteree3c72a2007-10-16 01:26:07 -07001723 c->node = -1;
Christoph Lameter1f842602008-01-07 23:20:30 -08001724 goto unlock_out;
Christoph Lameter894b8782007-05-10 03:15:16 -07001725}
1726
1727/*
1728 * Inlined fastpath so that allocation functions (kmalloc, kmem_cache_alloc)
1729 * have the fastpath folded into their functions. So no function call
1730 * overhead for requests that can be satisfied on the fastpath.
1731 *
1732 * The fastpath works by first checking if the lockless freelist can be used.
1733 * If not then __slab_alloc is called for slow processing.
1734 *
1735 * Otherwise we can simply pick the next object from the lockless free list.
1736 */
Pekka Enberg06428782008-01-07 23:20:27 -08001737static __always_inline void *slab_alloc(struct kmem_cache *s,
Eduard - Gabriel Munteanuce71e272008-08-19 20:43:25 +03001738 gfp_t gfpflags, int node, unsigned long addr)
Christoph Lameter894b8782007-05-10 03:15:16 -07001739{
Christoph Lameter894b8782007-05-10 03:15:16 -07001740 void **object;
Christoph Lameterdfb4f092007-10-16 01:26:05 -07001741 struct kmem_cache_cpu *c;
Christoph Lameter1f842602008-01-07 23:20:30 -08001742 unsigned long flags;
1743
Christoph Lameterc016b0b2010-08-20 12:37:16 -05001744 if (slab_pre_alloc_hook(s, gfpflags))
Akinobu Mita773ff602008-12-23 19:37:01 +09001745 return NULL;
1746
Christoph Lameter894b8782007-05-10 03:15:16 -07001747 local_irq_save(flags);
Christoph Lameter9dfc6e62009-12-18 16:26:20 -06001748 c = __this_cpu_ptr(s->cpu_slab);
1749 object = c->freelist;
Christoph Lameter9dfc6e62009-12-18 16:26:20 -06001750 if (unlikely(!object || !node_match(c, node)))
Christoph Lameter894b8782007-05-10 03:15:16 -07001751
Christoph Lameterdfb4f092007-10-16 01:26:05 -07001752 object = __slab_alloc(s, gfpflags, node, addr, c);
Christoph Lameter894b8782007-05-10 03:15:16 -07001753
1754 else {
Christoph Lameterff120592009-12-18 16:26:22 -06001755 c->freelist = get_freepointer(s, object);
Christoph Lameter84e554e2009-12-18 16:26:23 -06001756 stat(s, ALLOC_FASTPATH);
Christoph Lameter894b8782007-05-10 03:15:16 -07001757 }
1758 local_irq_restore(flags);
Christoph Lameterd07dbea2007-07-17 04:03:23 -07001759
Pekka Enberg74e21342009-11-25 20:14:48 +02001760 if (unlikely(gfpflags & __GFP_ZERO) && object)
Christoph Lameterff120592009-12-18 16:26:22 -06001761 memset(object, 0, s->objsize);
Christoph Lameterd07dbea2007-07-17 04:03:23 -07001762
Christoph Lameterc016b0b2010-08-20 12:37:16 -05001763 slab_post_alloc_hook(s, gfpflags, object);
Vegard Nossum5a896d92008-04-04 00:54:48 +02001764
Christoph Lameter894b8782007-05-10 03:15:16 -07001765 return object;
Christoph Lameter81819f02007-05-06 14:49:36 -07001766}
1767
1768void *kmem_cache_alloc(struct kmem_cache *s, gfp_t gfpflags)
1769{
Christoph Lameter2154a332010-07-09 14:07:10 -05001770 void *ret = slab_alloc(s, gfpflags, NUMA_NO_NODE, _RET_IP_);
Eduard - Gabriel Munteanu5b882be2008-08-19 20:43:26 +03001771
Eduard - Gabriel Munteanuca2b84c2009-03-23 15:12:24 +02001772 trace_kmem_cache_alloc(_RET_IP_, ret, s->objsize, s->size, gfpflags);
Eduard - Gabriel Munteanu5b882be2008-08-19 20:43:26 +03001773
1774 return ret;
Christoph Lameter81819f02007-05-06 14:49:36 -07001775}
1776EXPORT_SYMBOL(kmem_cache_alloc);
1777
Li Zefan0f24f122009-12-11 15:45:30 +08001778#ifdef CONFIG_TRACING
Eduard - Gabriel Munteanu5b882be2008-08-19 20:43:26 +03001779void *kmem_cache_alloc_notrace(struct kmem_cache *s, gfp_t gfpflags)
1780{
Christoph Lameter2154a332010-07-09 14:07:10 -05001781 return slab_alloc(s, gfpflags, NUMA_NO_NODE, _RET_IP_);
Eduard - Gabriel Munteanu5b882be2008-08-19 20:43:26 +03001782}
1783EXPORT_SYMBOL(kmem_cache_alloc_notrace);
1784#endif
1785
Christoph Lameter81819f02007-05-06 14:49:36 -07001786#ifdef CONFIG_NUMA
1787void *kmem_cache_alloc_node(struct kmem_cache *s, gfp_t gfpflags, int node)
1788{
Eduard - Gabriel Munteanu5b882be2008-08-19 20:43:26 +03001789 void *ret = slab_alloc(s, gfpflags, node, _RET_IP_);
1790
Eduard - Gabriel Munteanuca2b84c2009-03-23 15:12:24 +02001791 trace_kmem_cache_alloc_node(_RET_IP_, ret,
1792 s->objsize, s->size, gfpflags, node);
Eduard - Gabriel Munteanu5b882be2008-08-19 20:43:26 +03001793
1794 return ret;
Christoph Lameter81819f02007-05-06 14:49:36 -07001795}
1796EXPORT_SYMBOL(kmem_cache_alloc_node);
1797#endif
1798
Li Zefan0f24f122009-12-11 15:45:30 +08001799#ifdef CONFIG_TRACING
Eduard - Gabriel Munteanu5b882be2008-08-19 20:43:26 +03001800void *kmem_cache_alloc_node_notrace(struct kmem_cache *s,
1801 gfp_t gfpflags,
1802 int node)
1803{
1804 return slab_alloc(s, gfpflags, node, _RET_IP_);
1805}
1806EXPORT_SYMBOL(kmem_cache_alloc_node_notrace);
1807#endif
1808
Christoph Lameter81819f02007-05-06 14:49:36 -07001809/*
Christoph Lameter894b8782007-05-10 03:15:16 -07001810 * Slow patch handling. This may still be called frequently since objects
1811 * have a longer lifetime than the cpu slabs in most processing loads.
Christoph Lameter81819f02007-05-06 14:49:36 -07001812 *
Christoph Lameter894b8782007-05-10 03:15:16 -07001813 * So we still attempt to reduce cache line usage. Just take the slab
1814 * lock and free the item. If there is no additional partial page
1815 * handling required then we can return immediately.
Christoph Lameter81819f02007-05-06 14:49:36 -07001816 */
Christoph Lameter894b8782007-05-10 03:15:16 -07001817static void __slab_free(struct kmem_cache *s, struct page *page,
Christoph Lameterff120592009-12-18 16:26:22 -06001818 void *x, unsigned long addr)
Christoph Lameter81819f02007-05-06 14:49:36 -07001819{
1820 void *prior;
1821 void **object = (void *)x;
Christoph Lameter81819f02007-05-06 14:49:36 -07001822
Christoph Lameter84e554e2009-12-18 16:26:23 -06001823 stat(s, FREE_SLOWPATH);
Christoph Lameter81819f02007-05-06 14:49:36 -07001824 slab_lock(page);
1825
Christoph Lameteraf537b02010-07-09 14:07:14 -05001826 if (kmem_cache_debug(s))
Christoph Lameter81819f02007-05-06 14:49:36 -07001827 goto debug;
Christoph Lameter6446faa2008-02-15 23:45:26 -08001828
Christoph Lameter81819f02007-05-06 14:49:36 -07001829checks_ok:
Christoph Lameterff120592009-12-18 16:26:22 -06001830 prior = page->freelist;
1831 set_freepointer(s, object, prior);
Christoph Lameter81819f02007-05-06 14:49:36 -07001832 page->freelist = object;
1833 page->inuse--;
1834
Andy Whitcroft8a380822008-07-23 21:27:18 -07001835 if (unlikely(PageSlubFrozen(page))) {
Christoph Lameter84e554e2009-12-18 16:26:23 -06001836 stat(s, FREE_FROZEN);
Christoph Lameter81819f02007-05-06 14:49:36 -07001837 goto out_unlock;
Christoph Lameter8ff12cf2008-02-07 17:47:41 -08001838 }
Christoph Lameter81819f02007-05-06 14:49:36 -07001839
1840 if (unlikely(!page->inuse))
1841 goto slab_empty;
1842
1843 /*
Christoph Lameter6446faa2008-02-15 23:45:26 -08001844 * Objects left in the slab. If it was not on the partial list before
Christoph Lameter81819f02007-05-06 14:49:36 -07001845 * then add it.
1846 */
Christoph Lametera973e9d2008-03-01 13:40:44 -08001847 if (unlikely(!prior)) {
Christoph Lameter7c2e1322008-01-07 23:20:27 -08001848 add_partial(get_node(s, page_to_nid(page)), page, 1);
Christoph Lameter84e554e2009-12-18 16:26:23 -06001849 stat(s, FREE_ADD_PARTIAL);
Christoph Lameter8ff12cf2008-02-07 17:47:41 -08001850 }
Christoph Lameter81819f02007-05-06 14:49:36 -07001851
1852out_unlock:
1853 slab_unlock(page);
Christoph Lameter81819f02007-05-06 14:49:36 -07001854 return;
1855
1856slab_empty:
Christoph Lametera973e9d2008-03-01 13:40:44 -08001857 if (prior) {
Christoph Lameter81819f02007-05-06 14:49:36 -07001858 /*
Christoph Lameter672bba32007-05-09 02:32:39 -07001859 * Slab still on the partial list.
Christoph Lameter81819f02007-05-06 14:49:36 -07001860 */
1861 remove_partial(s, page);
Christoph Lameter84e554e2009-12-18 16:26:23 -06001862 stat(s, FREE_REMOVE_PARTIAL);
Christoph Lameter8ff12cf2008-02-07 17:47:41 -08001863 }
Christoph Lameter81819f02007-05-06 14:49:36 -07001864 slab_unlock(page);
Christoph Lameter84e554e2009-12-18 16:26:23 -06001865 stat(s, FREE_SLAB);
Christoph Lameter81819f02007-05-06 14:49:36 -07001866 discard_slab(s, page);
Christoph Lameter81819f02007-05-06 14:49:36 -07001867 return;
1868
1869debug:
Christoph Lameter3ec09742007-05-16 22:11:00 -07001870 if (!free_debug_processing(s, page, x, addr))
Christoph Lameter77c5e2d2007-05-06 14:49:42 -07001871 goto out_unlock;
Christoph Lameter77c5e2d2007-05-06 14:49:42 -07001872 goto checks_ok;
Christoph Lameter81819f02007-05-06 14:49:36 -07001873}
1874
Christoph Lameter894b8782007-05-10 03:15:16 -07001875/*
1876 * Fastpath with forced inlining to produce a kfree and kmem_cache_free that
1877 * can perform fastpath freeing without additional function calls.
1878 *
1879 * The fastpath is only possible if we are freeing to the current cpu slab
1880 * of this processor. This typically the case if we have just allocated
1881 * the item before.
1882 *
1883 * If fastpath is not possible then fall back to __slab_free where we deal
1884 * with all sorts of special processing.
1885 */
Pekka Enberg06428782008-01-07 23:20:27 -08001886static __always_inline void slab_free(struct kmem_cache *s,
Eduard - Gabriel Munteanuce71e272008-08-19 20:43:25 +03001887 struct page *page, void *x, unsigned long addr)
Christoph Lameter894b8782007-05-10 03:15:16 -07001888{
1889 void **object = (void *)x;
Christoph Lameterdfb4f092007-10-16 01:26:05 -07001890 struct kmem_cache_cpu *c;
Christoph Lameter1f842602008-01-07 23:20:30 -08001891 unsigned long flags;
1892
Christoph Lameterc016b0b2010-08-20 12:37:16 -05001893 slab_free_hook(s, x);
1894
Christoph Lameter894b8782007-05-10 03:15:16 -07001895 local_irq_save(flags);
Christoph Lameter9dfc6e62009-12-18 16:26:20 -06001896 c = __this_cpu_ptr(s->cpu_slab);
Christoph Lameterc016b0b2010-08-20 12:37:16 -05001897
1898 slab_free_hook_irq(s, x);
1899
Christoph Lameteree3c72a2007-10-16 01:26:07 -07001900 if (likely(page == c->page && c->node >= 0)) {
Christoph Lameterff120592009-12-18 16:26:22 -06001901 set_freepointer(s, object, c->freelist);
Christoph Lameterdfb4f092007-10-16 01:26:05 -07001902 c->freelist = object;
Christoph Lameter84e554e2009-12-18 16:26:23 -06001903 stat(s, FREE_FASTPATH);
Christoph Lameter894b8782007-05-10 03:15:16 -07001904 } else
Christoph Lameterff120592009-12-18 16:26:22 -06001905 __slab_free(s, page, x, addr);
Christoph Lameter894b8782007-05-10 03:15:16 -07001906
1907 local_irq_restore(flags);
1908}
1909
Christoph Lameter81819f02007-05-06 14:49:36 -07001910void kmem_cache_free(struct kmem_cache *s, void *x)
1911{
Christoph Lameter77c5e2d2007-05-06 14:49:42 -07001912 struct page *page;
Christoph Lameter81819f02007-05-06 14:49:36 -07001913
Christoph Lameterb49af682007-05-06 14:49:41 -07001914 page = virt_to_head_page(x);
Christoph Lameter81819f02007-05-06 14:49:36 -07001915
Eduard - Gabriel Munteanuce71e272008-08-19 20:43:25 +03001916 slab_free(s, page, x, _RET_IP_);
Eduard - Gabriel Munteanu5b882be2008-08-19 20:43:26 +03001917
Eduard - Gabriel Munteanuca2b84c2009-03-23 15:12:24 +02001918 trace_kmem_cache_free(_RET_IP_, x);
Christoph Lameter81819f02007-05-06 14:49:36 -07001919}
1920EXPORT_SYMBOL(kmem_cache_free);
1921
Cyrill Gorcunove9beef12008-10-28 22:02:26 +03001922/* Figure out on which slab page the object resides */
Christoph Lameter81819f02007-05-06 14:49:36 -07001923static struct page *get_object_page(const void *x)
1924{
Christoph Lameterb49af682007-05-06 14:49:41 -07001925 struct page *page = virt_to_head_page(x);
Christoph Lameter81819f02007-05-06 14:49:36 -07001926
1927 if (!PageSlab(page))
1928 return NULL;
1929
1930 return page;
1931}
1932
1933/*
Christoph Lameter672bba32007-05-09 02:32:39 -07001934 * Object placement in a slab is made very easy because we always start at
1935 * offset 0. If we tune the size of the object to the alignment then we can
1936 * get the required alignment by putting one properly sized object after
1937 * another.
Christoph Lameter81819f02007-05-06 14:49:36 -07001938 *
1939 * Notice that the allocation order determines the sizes of the per cpu
1940 * caches. Each processor has always one slab available for allocations.
1941 * Increasing the allocation order reduces the number of times that slabs
Christoph Lameter672bba32007-05-09 02:32:39 -07001942 * must be moved on and off the partial lists and is therefore a factor in
Christoph Lameter81819f02007-05-06 14:49:36 -07001943 * locking overhead.
Christoph Lameter81819f02007-05-06 14:49:36 -07001944 */
1945
1946/*
1947 * Mininum / Maximum order of slab pages. This influences locking overhead
1948 * and slab fragmentation. A higher order reduces the number of partial slabs
1949 * and increases the number of allocations possible without having to
1950 * take the list_lock.
1951 */
1952static int slub_min_order;
Christoph Lameter114e9e82008-04-14 19:11:41 +03001953static int slub_max_order = PAGE_ALLOC_COSTLY_ORDER;
Christoph Lameter9b2cd502008-04-14 19:11:41 +03001954static int slub_min_objects;
Christoph Lameter81819f02007-05-06 14:49:36 -07001955
1956/*
1957 * Merge control. If this is set then no merging of slab caches will occur.
Christoph Lameter672bba32007-05-09 02:32:39 -07001958 * (Could be removed. This was introduced to pacify the merge skeptics.)
Christoph Lameter81819f02007-05-06 14:49:36 -07001959 */
1960static int slub_nomerge;
1961
1962/*
Christoph Lameter81819f02007-05-06 14:49:36 -07001963 * Calculate the order of allocation given an slab object size.
1964 *
Christoph Lameter672bba32007-05-09 02:32:39 -07001965 * The order of allocation has significant impact on performance and other
1966 * system components. Generally order 0 allocations should be preferred since
1967 * order 0 does not cause fragmentation in the page allocator. Larger objects
1968 * be problematic to put into order 0 slabs because there may be too much
Christoph Lameterc124f5b2008-04-14 19:13:29 +03001969 * unused space left. We go to a higher order if more than 1/16th of the slab
Christoph Lameter672bba32007-05-09 02:32:39 -07001970 * would be wasted.
Christoph Lameter81819f02007-05-06 14:49:36 -07001971 *
Christoph Lameter672bba32007-05-09 02:32:39 -07001972 * In order to reach satisfactory performance we must ensure that a minimum
1973 * number of objects is in one slab. Otherwise we may generate too much
1974 * activity on the partial lists which requires taking the list_lock. This is
1975 * less a concern for large slabs though which are rarely used.
Christoph Lameter81819f02007-05-06 14:49:36 -07001976 *
Christoph Lameter672bba32007-05-09 02:32:39 -07001977 * slub_max_order specifies the order where we begin to stop considering the
1978 * number of objects in a slab as critical. If we reach slub_max_order then
1979 * we try to keep the page order as low as possible. So we accept more waste
1980 * of space in favor of a small page order.
1981 *
1982 * Higher order allocations also allow the placement of more objects in a
1983 * slab and thereby reduce object handling overhead. If the user has
1984 * requested a higher mininum order then we start with that one instead of
1985 * the smallest order which will fit the object.
Christoph Lameter81819f02007-05-06 14:49:36 -07001986 */
Christoph Lameter5e6d4442007-05-09 02:32:46 -07001987static inline int slab_order(int size, int min_objects,
1988 int max_order, int fract_leftover)
Christoph Lameter81819f02007-05-06 14:49:36 -07001989{
1990 int order;
1991 int rem;
Christoph Lameter6300ea72007-07-17 04:03:20 -07001992 int min_order = slub_min_order;
Christoph Lameter81819f02007-05-06 14:49:36 -07001993
Cyrill Gorcunov210b5c02008-10-22 23:00:38 +04001994 if ((PAGE_SIZE << min_order) / size > MAX_OBJS_PER_PAGE)
1995 return get_order(size * MAX_OBJS_PER_PAGE) - 1;
Christoph Lameter39b26462008-04-14 19:11:30 +03001996
Christoph Lameter6300ea72007-07-17 04:03:20 -07001997 for (order = max(min_order,
Christoph Lameter5e6d4442007-05-09 02:32:46 -07001998 fls(min_objects * size - 1) - PAGE_SHIFT);
1999 order <= max_order; order++) {
2000
Christoph Lameter81819f02007-05-06 14:49:36 -07002001 unsigned long slab_size = PAGE_SIZE << order;
2002
Christoph Lameter5e6d4442007-05-09 02:32:46 -07002003 if (slab_size < min_objects * size)
Christoph Lameter81819f02007-05-06 14:49:36 -07002004 continue;
2005
Christoph Lameter81819f02007-05-06 14:49:36 -07002006 rem = slab_size % size;
2007
Christoph Lameter5e6d4442007-05-09 02:32:46 -07002008 if (rem <= slab_size / fract_leftover)
Christoph Lameter81819f02007-05-06 14:49:36 -07002009 break;
2010
2011 }
Christoph Lameter672bba32007-05-09 02:32:39 -07002012
Christoph Lameter81819f02007-05-06 14:49:36 -07002013 return order;
2014}
2015
Christoph Lameter5e6d4442007-05-09 02:32:46 -07002016static inline int calculate_order(int size)
2017{
2018 int order;
2019 int min_objects;
2020 int fraction;
Zhang Yanmine8120ff2009-02-12 18:00:17 +02002021 int max_objects;
Christoph Lameter5e6d4442007-05-09 02:32:46 -07002022
2023 /*
2024 * Attempt to find best configuration for a slab. This
2025 * works by first attempting to generate a layout with
2026 * the best configuration and backing off gradually.
2027 *
2028 * First we reduce the acceptable waste in a slab. Then
2029 * we reduce the minimum objects required in a slab.
2030 */
2031 min_objects = slub_min_objects;
Christoph Lameter9b2cd502008-04-14 19:11:41 +03002032 if (!min_objects)
2033 min_objects = 4 * (fls(nr_cpu_ids) + 1);
Zhang Yanmine8120ff2009-02-12 18:00:17 +02002034 max_objects = (PAGE_SIZE << slub_max_order)/size;
2035 min_objects = min(min_objects, max_objects);
2036
Christoph Lameter5e6d4442007-05-09 02:32:46 -07002037 while (min_objects > 1) {
Christoph Lameterc124f5b2008-04-14 19:13:29 +03002038 fraction = 16;
Christoph Lameter5e6d4442007-05-09 02:32:46 -07002039 while (fraction >= 4) {
2040 order = slab_order(size, min_objects,
2041 slub_max_order, fraction);
2042 if (order <= slub_max_order)
2043 return order;
2044 fraction /= 2;
2045 }
Amerigo Wang5086c382009-08-19 21:44:13 +03002046 min_objects--;
Christoph Lameter5e6d4442007-05-09 02:32:46 -07002047 }
2048
2049 /*
2050 * We were unable to place multiple objects in a slab. Now
2051 * lets see if we can place a single object there.
2052 */
2053 order = slab_order(size, 1, slub_max_order, 1);
2054 if (order <= slub_max_order)
2055 return order;
2056
2057 /*
2058 * Doh this slab cannot be placed using slub_max_order.
2059 */
2060 order = slab_order(size, 1, MAX_ORDER, 1);
David Rientjes818cf592009-04-23 09:58:22 +03002061 if (order < MAX_ORDER)
Christoph Lameter5e6d4442007-05-09 02:32:46 -07002062 return order;
2063 return -ENOSYS;
2064}
2065
Christoph Lameter81819f02007-05-06 14:49:36 -07002066/*
Christoph Lameter672bba32007-05-09 02:32:39 -07002067 * Figure out what the alignment of the objects will be.
Christoph Lameter81819f02007-05-06 14:49:36 -07002068 */
2069static unsigned long calculate_alignment(unsigned long flags,
2070 unsigned long align, unsigned long size)
2071{
2072 /*
Christoph Lameter6446faa2008-02-15 23:45:26 -08002073 * If the user wants hardware cache aligned objects then follow that
2074 * suggestion if the object is sufficiently large.
Christoph Lameter81819f02007-05-06 14:49:36 -07002075 *
Christoph Lameter6446faa2008-02-15 23:45:26 -08002076 * The hardware cache alignment cannot override the specified
2077 * alignment though. If that is greater then use it.
Christoph Lameter81819f02007-05-06 14:49:36 -07002078 */
Nick Pigginb6210382008-03-05 14:05:56 -08002079 if (flags & SLAB_HWCACHE_ALIGN) {
2080 unsigned long ralign = cache_line_size();
2081 while (size <= ralign / 2)
2082 ralign /= 2;
2083 align = max(align, ralign);
2084 }
Christoph Lameter81819f02007-05-06 14:49:36 -07002085
2086 if (align < ARCH_SLAB_MINALIGN)
Nick Pigginb6210382008-03-05 14:05:56 -08002087 align = ARCH_SLAB_MINALIGN;
Christoph Lameter81819f02007-05-06 14:49:36 -07002088
2089 return ALIGN(align, sizeof(void *));
2090}
2091
Pekka Enberg5595cff2008-08-05 09:28:47 +03002092static void
2093init_kmem_cache_node(struct kmem_cache_node *n, struct kmem_cache *s)
Christoph Lameter81819f02007-05-06 14:49:36 -07002094{
2095 n->nr_partial = 0;
Christoph Lameter81819f02007-05-06 14:49:36 -07002096 spin_lock_init(&n->list_lock);
2097 INIT_LIST_HEAD(&n->partial);
Christoph Lameter8ab13722007-07-17 04:03:32 -07002098#ifdef CONFIG_SLUB_DEBUG
Christoph Lameter0f389ec2008-04-14 18:53:02 +03002099 atomic_long_set(&n->nr_slabs, 0);
Salman Qazi02b71b72008-09-11 12:25:41 -07002100 atomic_long_set(&n->total_objects, 0);
Christoph Lameter643b1132007-05-06 14:49:42 -07002101 INIT_LIST_HEAD(&n->full);
Christoph Lameter8ab13722007-07-17 04:03:32 -07002102#endif
Christoph Lameter81819f02007-05-06 14:49:36 -07002103}
2104
Christoph Lameter55136592010-08-20 12:37:13 -05002105static inline int alloc_kmem_cache_cpus(struct kmem_cache *s)
Christoph Lameter4c93c3552007-10-16 01:26:08 -07002106{
Christoph Lameterdb210e72010-08-26 09:41:19 -05002107#ifdef CONFIG_SMP
2108 /*
2109 * Will use reserve that does not require slab operation during
2110 * early boot.
2111 */
Christoph Lameter6c182dc2010-08-20 12:37:14 -05002112 BUILD_BUG_ON(PERCPU_DYNAMIC_EARLY_SIZE <
2113 SLUB_PAGE_SHIFT * sizeof(struct kmem_cache_cpu));
Christoph Lameterdb210e72010-08-26 09:41:19 -05002114#else
2115 /*
2116 * Special hack for UP mode. allocpercpu() falls back to kmalloc
2117 * operations. So we cannot use that before the slab allocator is up
2118 * Simply get the smallest possible compound page. The page will be
2119 * released via kfree() when the cpu caches are resized later.
2120 */
2121 if (slab_state < UP)
2122 s->cpu_slab = (__percpu void *)kmalloc_large(PAGE_SIZE << 1, GFP_NOWAIT);
2123 else
2124#endif
Christoph Lameter9dfc6e62009-12-18 16:26:20 -06002125
Christoph Lameter6c182dc2010-08-20 12:37:14 -05002126 s->cpu_slab = alloc_percpu(struct kmem_cache_cpu);
Christoph Lameter9dfc6e62009-12-18 16:26:20 -06002127
Christoph Lameter6c182dc2010-08-20 12:37:14 -05002128 return s->cpu_slab != NULL;
Christoph Lameter4c93c3552007-10-16 01:26:08 -07002129}
Christoph Lameter4c93c3552007-10-16 01:26:08 -07002130
Christoph Lameter81819f02007-05-06 14:49:36 -07002131#ifdef CONFIG_NUMA
Christoph Lameter51df1142010-08-20 12:37:15 -05002132static struct kmem_cache *kmem_cache_node;
2133
Christoph Lameter81819f02007-05-06 14:49:36 -07002134/*
2135 * No kmalloc_node yet so do it by hand. We know that this is the first
2136 * slab on the node for this slabcache. There are no concurrent accesses
2137 * possible.
2138 *
2139 * Note that this function only works on the kmalloc_node_cache
Christoph Lameter4c93c3552007-10-16 01:26:08 -07002140 * when allocating for the kmalloc_node_cache. This is used for bootstrapping
2141 * memory on a fresh node that has no slab structures yet.
Christoph Lameter81819f02007-05-06 14:49:36 -07002142 */
Christoph Lameter55136592010-08-20 12:37:13 -05002143static void early_kmem_cache_node_alloc(int node)
Christoph Lameter81819f02007-05-06 14:49:36 -07002144{
2145 struct page *page;
2146 struct kmem_cache_node *n;
rootba84c732008-01-07 23:20:28 -08002147 unsigned long flags;
Christoph Lameter81819f02007-05-06 14:49:36 -07002148
Christoph Lameter51df1142010-08-20 12:37:15 -05002149 BUG_ON(kmem_cache_node->size < sizeof(struct kmem_cache_node));
Christoph Lameter81819f02007-05-06 14:49:36 -07002150
Christoph Lameter51df1142010-08-20 12:37:15 -05002151 page = new_slab(kmem_cache_node, GFP_NOWAIT, node);
Christoph Lameter81819f02007-05-06 14:49:36 -07002152
2153 BUG_ON(!page);
Christoph Lametera2f92ee2007-08-22 14:01:57 -07002154 if (page_to_nid(page) != node) {
2155 printk(KERN_ERR "SLUB: Unable to allocate memory from "
2156 "node %d\n", node);
2157 printk(KERN_ERR "SLUB: Allocating a useless per node structure "
2158 "in order to be able to continue\n");
2159 }
2160
Christoph Lameter81819f02007-05-06 14:49:36 -07002161 n = page->freelist;
2162 BUG_ON(!n);
Christoph Lameter51df1142010-08-20 12:37:15 -05002163 page->freelist = get_freepointer(kmem_cache_node, n);
Christoph Lameter81819f02007-05-06 14:49:36 -07002164 page->inuse++;
Christoph Lameter51df1142010-08-20 12:37:15 -05002165 kmem_cache_node->node[node] = n;
Christoph Lameter8ab13722007-07-17 04:03:32 -07002166#ifdef CONFIG_SLUB_DEBUG
Christoph Lameter51df1142010-08-20 12:37:15 -05002167 init_object(kmem_cache_node, n, 1);
2168 init_tracking(kmem_cache_node, n);
Christoph Lameter8ab13722007-07-17 04:03:32 -07002169#endif
Christoph Lameter51df1142010-08-20 12:37:15 -05002170 init_kmem_cache_node(n, kmem_cache_node);
2171 inc_slabs_node(kmem_cache_node, node, page->objects);
Christoph Lameter6446faa2008-02-15 23:45:26 -08002172
rootba84c732008-01-07 23:20:28 -08002173 /*
2174 * lockdep requires consistent irq usage for each lock
2175 * so even though there cannot be a race this early in
2176 * the boot sequence, we still disable irqs.
2177 */
2178 local_irq_save(flags);
Christoph Lameter7c2e1322008-01-07 23:20:27 -08002179 add_partial(n, page, 0);
rootba84c732008-01-07 23:20:28 -08002180 local_irq_restore(flags);
Christoph Lameter81819f02007-05-06 14:49:36 -07002181}
2182
2183static void free_kmem_cache_nodes(struct kmem_cache *s)
2184{
2185 int node;
2186
Christoph Lameterf64dc582007-10-16 01:25:33 -07002187 for_each_node_state(node, N_NORMAL_MEMORY) {
Christoph Lameter81819f02007-05-06 14:49:36 -07002188 struct kmem_cache_node *n = s->node[node];
Christoph Lameter51df1142010-08-20 12:37:15 -05002189
Alexander Duyck73367bd2010-05-21 14:41:35 -07002190 if (n)
Christoph Lameter51df1142010-08-20 12:37:15 -05002191 kmem_cache_free(kmem_cache_node, n);
2192
Christoph Lameter81819f02007-05-06 14:49:36 -07002193 s->node[node] = NULL;
2194 }
2195}
2196
Christoph Lameter55136592010-08-20 12:37:13 -05002197static int init_kmem_cache_nodes(struct kmem_cache *s)
Christoph Lameter81819f02007-05-06 14:49:36 -07002198{
2199 int node;
Christoph Lameter81819f02007-05-06 14:49:36 -07002200
Christoph Lameterf64dc582007-10-16 01:25:33 -07002201 for_each_node_state(node, N_NORMAL_MEMORY) {
Christoph Lameter81819f02007-05-06 14:49:36 -07002202 struct kmem_cache_node *n;
2203
Alexander Duyck73367bd2010-05-21 14:41:35 -07002204 if (slab_state == DOWN) {
Christoph Lameter55136592010-08-20 12:37:13 -05002205 early_kmem_cache_node_alloc(node);
Alexander Duyck73367bd2010-05-21 14:41:35 -07002206 continue;
Christoph Lameter81819f02007-05-06 14:49:36 -07002207 }
Christoph Lameter51df1142010-08-20 12:37:15 -05002208 n = kmem_cache_alloc_node(kmem_cache_node,
Christoph Lameter55136592010-08-20 12:37:13 -05002209 GFP_KERNEL, node);
Alexander Duyck73367bd2010-05-21 14:41:35 -07002210
2211 if (!n) {
2212 free_kmem_cache_nodes(s);
2213 return 0;
2214 }
2215
Christoph Lameter81819f02007-05-06 14:49:36 -07002216 s->node[node] = n;
Pekka Enberg5595cff2008-08-05 09:28:47 +03002217 init_kmem_cache_node(n, s);
Christoph Lameter81819f02007-05-06 14:49:36 -07002218 }
2219 return 1;
2220}
2221#else
2222static void free_kmem_cache_nodes(struct kmem_cache *s)
2223{
2224}
2225
Christoph Lameter55136592010-08-20 12:37:13 -05002226static int init_kmem_cache_nodes(struct kmem_cache *s)
Christoph Lameter81819f02007-05-06 14:49:36 -07002227{
Pekka Enberg5595cff2008-08-05 09:28:47 +03002228 init_kmem_cache_node(&s->local_node, s);
Christoph Lameter81819f02007-05-06 14:49:36 -07002229 return 1;
2230}
2231#endif
2232
David Rientjesc0bdb232009-02-25 09:16:35 +02002233static void set_min_partial(struct kmem_cache *s, unsigned long min)
David Rientjes3b89d7d2009-02-22 17:40:07 -08002234{
2235 if (min < MIN_PARTIAL)
2236 min = MIN_PARTIAL;
2237 else if (min > MAX_PARTIAL)
2238 min = MAX_PARTIAL;
2239 s->min_partial = min;
2240}
2241
Christoph Lameter81819f02007-05-06 14:49:36 -07002242/*
2243 * calculate_sizes() determines the order and the distribution of data within
2244 * a slab object.
2245 */
Christoph Lameter06b285d2008-04-14 19:11:41 +03002246static int calculate_sizes(struct kmem_cache *s, int forced_order)
Christoph Lameter81819f02007-05-06 14:49:36 -07002247{
2248 unsigned long flags = s->flags;
2249 unsigned long size = s->objsize;
2250 unsigned long align = s->align;
Christoph Lameter834f3d12008-04-14 19:11:31 +03002251 int order;
Christoph Lameter81819f02007-05-06 14:49:36 -07002252
2253 /*
Christoph Lameterd8b42bf2008-02-15 23:45:25 -08002254 * Round up object size to the next word boundary. We can only
2255 * place the free pointer at word boundaries and this determines
2256 * the possible location of the free pointer.
2257 */
2258 size = ALIGN(size, sizeof(void *));
2259
2260#ifdef CONFIG_SLUB_DEBUG
2261 /*
Christoph Lameter81819f02007-05-06 14:49:36 -07002262 * Determine if we can poison the object itself. If the user of
2263 * the slab may touch the object after free or before allocation
2264 * then we should never poison the object itself.
2265 */
2266 if ((flags & SLAB_POISON) && !(flags & SLAB_DESTROY_BY_RCU) &&
Christoph Lameterc59def9f2007-05-16 22:10:50 -07002267 !s->ctor)
Christoph Lameter81819f02007-05-06 14:49:36 -07002268 s->flags |= __OBJECT_POISON;
2269 else
2270 s->flags &= ~__OBJECT_POISON;
2271
Christoph Lameter81819f02007-05-06 14:49:36 -07002272
2273 /*
Christoph Lameter672bba32007-05-09 02:32:39 -07002274 * If we are Redzoning then check if there is some space between the
Christoph Lameter81819f02007-05-06 14:49:36 -07002275 * end of the object and the free pointer. If not then add an
Christoph Lameter672bba32007-05-09 02:32:39 -07002276 * additional word to have some bytes to store Redzone information.
Christoph Lameter81819f02007-05-06 14:49:36 -07002277 */
2278 if ((flags & SLAB_RED_ZONE) && size == s->objsize)
2279 size += sizeof(void *);
Christoph Lameter41ecc552007-05-09 02:32:44 -07002280#endif
Christoph Lameter81819f02007-05-06 14:49:36 -07002281
2282 /*
Christoph Lameter672bba32007-05-09 02:32:39 -07002283 * With that we have determined the number of bytes in actual use
2284 * by the object. This is the potential offset to the free pointer.
Christoph Lameter81819f02007-05-06 14:49:36 -07002285 */
2286 s->inuse = size;
2287
2288 if (((flags & (SLAB_DESTROY_BY_RCU | SLAB_POISON)) ||
Christoph Lameterc59def9f2007-05-16 22:10:50 -07002289 s->ctor)) {
Christoph Lameter81819f02007-05-06 14:49:36 -07002290 /*
2291 * Relocate free pointer after the object if it is not
2292 * permitted to overwrite the first word of the object on
2293 * kmem_cache_free.
2294 *
2295 * This is the case if we do RCU, have a constructor or
2296 * destructor or are poisoning the objects.
2297 */
2298 s->offset = size;
2299 size += sizeof(void *);
2300 }
2301
Christoph Lameterc12b3c62007-05-23 13:57:31 -07002302#ifdef CONFIG_SLUB_DEBUG
Christoph Lameter81819f02007-05-06 14:49:36 -07002303 if (flags & SLAB_STORE_USER)
2304 /*
2305 * Need to store information about allocs and frees after
2306 * the object.
2307 */
2308 size += 2 * sizeof(struct track);
2309
Christoph Lameterbe7b3fb2007-05-09 02:32:36 -07002310 if (flags & SLAB_RED_ZONE)
Christoph Lameter81819f02007-05-06 14:49:36 -07002311 /*
2312 * Add some empty padding so that we can catch
2313 * overwrites from earlier objects rather than let
2314 * tracking information or the free pointer be
Frederik Schwarzer0211a9c2008-12-29 22:14:56 +01002315 * corrupted if a user writes before the start
Christoph Lameter81819f02007-05-06 14:49:36 -07002316 * of the object.
2317 */
2318 size += sizeof(void *);
Christoph Lameter41ecc552007-05-09 02:32:44 -07002319#endif
Christoph Lameter672bba32007-05-09 02:32:39 -07002320
Christoph Lameter81819f02007-05-06 14:49:36 -07002321 /*
2322 * Determine the alignment based on various parameters that the
Christoph Lameter65c02d42007-05-09 02:32:35 -07002323 * user specified and the dynamic determination of cache line size
2324 * on bootup.
Christoph Lameter81819f02007-05-06 14:49:36 -07002325 */
2326 align = calculate_alignment(flags, align, s->objsize);
Zhang, Yanmindcb0ce12009-07-30 11:28:11 +08002327 s->align = align;
Christoph Lameter81819f02007-05-06 14:49:36 -07002328
2329 /*
2330 * SLUB stores one object immediately after another beginning from
2331 * offset 0. In order to align the objects we have to simply size
2332 * each object to conform to the alignment.
2333 */
2334 size = ALIGN(size, align);
2335 s->size = size;
Christoph Lameter06b285d2008-04-14 19:11:41 +03002336 if (forced_order >= 0)
2337 order = forced_order;
2338 else
2339 order = calculate_order(size);
Christoph Lameter81819f02007-05-06 14:49:36 -07002340
Christoph Lameter834f3d12008-04-14 19:11:31 +03002341 if (order < 0)
Christoph Lameter81819f02007-05-06 14:49:36 -07002342 return 0;
2343
Christoph Lameterb7a49f02008-02-14 14:21:32 -08002344 s->allocflags = 0;
Christoph Lameter834f3d12008-04-14 19:11:31 +03002345 if (order)
Christoph Lameterb7a49f02008-02-14 14:21:32 -08002346 s->allocflags |= __GFP_COMP;
2347
2348 if (s->flags & SLAB_CACHE_DMA)
2349 s->allocflags |= SLUB_DMA;
2350
2351 if (s->flags & SLAB_RECLAIM_ACCOUNT)
2352 s->allocflags |= __GFP_RECLAIMABLE;
2353
Christoph Lameter81819f02007-05-06 14:49:36 -07002354 /*
2355 * Determine the number of objects per slab
2356 */
Christoph Lameter834f3d12008-04-14 19:11:31 +03002357 s->oo = oo_make(order, size);
Christoph Lameter65c33762008-04-14 19:11:40 +03002358 s->min = oo_make(get_order(size), size);
Christoph Lameter205ab992008-04-14 19:11:40 +03002359 if (oo_objects(s->oo) > oo_objects(s->max))
2360 s->max = s->oo;
Christoph Lameter81819f02007-05-06 14:49:36 -07002361
Christoph Lameter834f3d12008-04-14 19:11:31 +03002362 return !!oo_objects(s->oo);
Christoph Lameter81819f02007-05-06 14:49:36 -07002363
2364}
2365
Christoph Lameter55136592010-08-20 12:37:13 -05002366static int kmem_cache_open(struct kmem_cache *s,
Christoph Lameter81819f02007-05-06 14:49:36 -07002367 const char *name, size_t size,
2368 size_t align, unsigned long flags,
Alexey Dobriyan51cc5062008-07-25 19:45:34 -07002369 void (*ctor)(void *))
Christoph Lameter81819f02007-05-06 14:49:36 -07002370{
2371 memset(s, 0, kmem_size);
2372 s->name = name;
2373 s->ctor = ctor;
Christoph Lameter81819f02007-05-06 14:49:36 -07002374 s->objsize = size;
Christoph Lameter81819f02007-05-06 14:49:36 -07002375 s->align = align;
Christoph Lameterba0268a2007-09-11 15:24:11 -07002376 s->flags = kmem_cache_flags(size, flags, name, ctor);
Christoph Lameter81819f02007-05-06 14:49:36 -07002377
Christoph Lameter06b285d2008-04-14 19:11:41 +03002378 if (!calculate_sizes(s, -1))
Christoph Lameter81819f02007-05-06 14:49:36 -07002379 goto error;
David Rientjes3de47212009-07-27 18:30:35 -07002380 if (disable_higher_order_debug) {
2381 /*
2382 * Disable debugging flags that store metadata if the min slab
2383 * order increased.
2384 */
2385 if (get_order(s->size) > get_order(s->objsize)) {
2386 s->flags &= ~DEBUG_METADATA_FLAGS;
2387 s->offset = 0;
2388 if (!calculate_sizes(s, -1))
2389 goto error;
2390 }
2391 }
Christoph Lameter81819f02007-05-06 14:49:36 -07002392
David Rientjes3b89d7d2009-02-22 17:40:07 -08002393 /*
2394 * The larger the object size is, the more pages we want on the partial
2395 * list to avoid pounding the page allocator excessively.
2396 */
David Rientjesc0bdb232009-02-25 09:16:35 +02002397 set_min_partial(s, ilog2(s->size));
Christoph Lameter81819f02007-05-06 14:49:36 -07002398 s->refcount = 1;
2399#ifdef CONFIG_NUMA
Christoph Lametere2cb96b2008-08-19 08:51:22 -05002400 s->remote_node_defrag_ratio = 1000;
Christoph Lameter81819f02007-05-06 14:49:36 -07002401#endif
Christoph Lameter55136592010-08-20 12:37:13 -05002402 if (!init_kmem_cache_nodes(s))
Christoph Lameterdfb4f092007-10-16 01:26:05 -07002403 goto error;
Christoph Lameter81819f02007-05-06 14:49:36 -07002404
Christoph Lameter55136592010-08-20 12:37:13 -05002405 if (alloc_kmem_cache_cpus(s))
Christoph Lameter81819f02007-05-06 14:49:36 -07002406 return 1;
Christoph Lameterff120592009-12-18 16:26:22 -06002407
Christoph Lameter4c93c3552007-10-16 01:26:08 -07002408 free_kmem_cache_nodes(s);
Christoph Lameter81819f02007-05-06 14:49:36 -07002409error:
2410 if (flags & SLAB_PANIC)
2411 panic("Cannot create slab %s size=%lu realsize=%u "
2412 "order=%u offset=%u flags=%lx\n",
Christoph Lameter834f3d12008-04-14 19:11:31 +03002413 s->name, (unsigned long)size, s->size, oo_order(s->oo),
Christoph Lameter81819f02007-05-06 14:49:36 -07002414 s->offset, flags);
2415 return 0;
2416}
Christoph Lameter81819f02007-05-06 14:49:36 -07002417
2418/*
2419 * Check if a given pointer is valid
2420 */
2421int kmem_ptr_validate(struct kmem_cache *s, const void *object)
2422{
Pekka Enberg06428782008-01-07 23:20:27 -08002423 struct page *page;
Christoph Lameter81819f02007-05-06 14:49:36 -07002424
Pekka Enbergd3e06e22010-04-07 19:23:41 +03002425 if (!kern_ptr_validate(object, s->size))
2426 return 0;
2427
Christoph Lameter81819f02007-05-06 14:49:36 -07002428 page = get_object_page(object);
2429
2430 if (!page || s != page->slab)
2431 /* No slab or wrong slab */
2432 return 0;
2433
Christoph Lameterabcd08a2007-05-09 02:32:37 -07002434 if (!check_valid_pointer(s, page, object))
Christoph Lameter81819f02007-05-06 14:49:36 -07002435 return 0;
2436
2437 /*
2438 * We could also check if the object is on the slabs freelist.
2439 * But this would be too expensive and it seems that the main
Christoph Lameter6446faa2008-02-15 23:45:26 -08002440 * purpose of kmem_ptr_valid() is to check if the object belongs
Christoph Lameter81819f02007-05-06 14:49:36 -07002441 * to a certain slab.
2442 */
2443 return 1;
2444}
2445EXPORT_SYMBOL(kmem_ptr_validate);
2446
2447/*
2448 * Determine the size of a slab object
2449 */
2450unsigned int kmem_cache_size(struct kmem_cache *s)
2451{
2452 return s->objsize;
2453}
2454EXPORT_SYMBOL(kmem_cache_size);
2455
2456const char *kmem_cache_name(struct kmem_cache *s)
2457{
2458 return s->name;
2459}
2460EXPORT_SYMBOL(kmem_cache_name);
2461
Christoph Lameter33b12c32008-04-25 12:22:43 -07002462static void list_slab_objects(struct kmem_cache *s, struct page *page,
2463 const char *text)
Christoph Lameter81819f02007-05-06 14:49:36 -07002464{
Christoph Lameter33b12c32008-04-25 12:22:43 -07002465#ifdef CONFIG_SLUB_DEBUG
2466 void *addr = page_address(page);
2467 void *p;
Eric Dumazetbbd7d572010-03-24 22:25:47 +01002468 long *map = kzalloc(BITS_TO_LONGS(page->objects) * sizeof(long),
2469 GFP_ATOMIC);
Christoph Lameter33b12c32008-04-25 12:22:43 -07002470
Eric Dumazetbbd7d572010-03-24 22:25:47 +01002471 if (!map)
2472 return;
Christoph Lameter33b12c32008-04-25 12:22:43 -07002473 slab_err(s, page, "%s", text);
2474 slab_lock(page);
2475 for_each_free_object(p, s, page->freelist)
2476 set_bit(slab_index(p, s, addr), map);
2477
2478 for_each_object(p, s, addr, page->objects) {
2479
2480 if (!test_bit(slab_index(p, s, addr), map)) {
2481 printk(KERN_ERR "INFO: Object 0x%p @offset=%tu\n",
2482 p, p - addr);
2483 print_tracking(s, p);
2484 }
2485 }
2486 slab_unlock(page);
Eric Dumazetbbd7d572010-03-24 22:25:47 +01002487 kfree(map);
Christoph Lameter33b12c32008-04-25 12:22:43 -07002488#endif
2489}
2490
Christoph Lameter81819f02007-05-06 14:49:36 -07002491/*
Christoph Lameter599870b2008-04-23 12:36:52 -07002492 * Attempt to free all partial slabs on a node.
Christoph Lameter81819f02007-05-06 14:49:36 -07002493 */
Christoph Lameter599870b2008-04-23 12:36:52 -07002494static void free_partial(struct kmem_cache *s, struct kmem_cache_node *n)
Christoph Lameter81819f02007-05-06 14:49:36 -07002495{
Christoph Lameter81819f02007-05-06 14:49:36 -07002496 unsigned long flags;
2497 struct page *page, *h;
2498
2499 spin_lock_irqsave(&n->list_lock, flags);
Christoph Lameter33b12c32008-04-25 12:22:43 -07002500 list_for_each_entry_safe(page, h, &n->partial, lru) {
Christoph Lameter81819f02007-05-06 14:49:36 -07002501 if (!page->inuse) {
2502 list_del(&page->lru);
2503 discard_slab(s, page);
Christoph Lameter599870b2008-04-23 12:36:52 -07002504 n->nr_partial--;
Christoph Lameter33b12c32008-04-25 12:22:43 -07002505 } else {
2506 list_slab_objects(s, page,
2507 "Objects remaining on kmem_cache_close()");
Christoph Lameter599870b2008-04-23 12:36:52 -07002508 }
Christoph Lameter33b12c32008-04-25 12:22:43 -07002509 }
Christoph Lameter81819f02007-05-06 14:49:36 -07002510 spin_unlock_irqrestore(&n->list_lock, flags);
Christoph Lameter81819f02007-05-06 14:49:36 -07002511}
2512
2513/*
Christoph Lameter672bba32007-05-09 02:32:39 -07002514 * Release all resources used by a slab cache.
Christoph Lameter81819f02007-05-06 14:49:36 -07002515 */
Christoph Lameter0c710012007-07-17 04:03:24 -07002516static inline int kmem_cache_close(struct kmem_cache *s)
Christoph Lameter81819f02007-05-06 14:49:36 -07002517{
2518 int node;
2519
2520 flush_all(s);
Christoph Lameter9dfc6e62009-12-18 16:26:20 -06002521 free_percpu(s->cpu_slab);
Christoph Lameter81819f02007-05-06 14:49:36 -07002522 /* Attempt to free all objects */
Christoph Lameterf64dc582007-10-16 01:25:33 -07002523 for_each_node_state(node, N_NORMAL_MEMORY) {
Christoph Lameter81819f02007-05-06 14:49:36 -07002524 struct kmem_cache_node *n = get_node(s, node);
2525
Christoph Lameter599870b2008-04-23 12:36:52 -07002526 free_partial(s, n);
2527 if (n->nr_partial || slabs_node(s, node))
Christoph Lameter81819f02007-05-06 14:49:36 -07002528 return 1;
2529 }
2530 free_kmem_cache_nodes(s);
2531 return 0;
2532}
2533
2534/*
2535 * Close a cache and release the kmem_cache structure
2536 * (must be used for caches created using kmem_cache_create)
2537 */
2538void kmem_cache_destroy(struct kmem_cache *s)
2539{
2540 down_write(&slub_lock);
2541 s->refcount--;
2542 if (!s->refcount) {
2543 list_del(&s->list);
Pekka Enbergd629d812008-04-23 22:31:08 +03002544 if (kmem_cache_close(s)) {
2545 printk(KERN_ERR "SLUB %s: %s called for cache that "
2546 "still has objects.\n", s->name, __func__);
2547 dump_stack();
2548 }
Eric Dumazetd76b1592009-09-03 22:38:59 +03002549 if (s->flags & SLAB_DESTROY_BY_RCU)
2550 rcu_barrier();
Christoph Lameter81819f02007-05-06 14:49:36 -07002551 sysfs_slab_remove(s);
Christoph Lameter2bce6482010-07-19 11:39:11 -05002552 }
2553 up_write(&slub_lock);
Christoph Lameter81819f02007-05-06 14:49:36 -07002554}
2555EXPORT_SYMBOL(kmem_cache_destroy);
2556
2557/********************************************************************
2558 * Kmalloc subsystem
2559 *******************************************************************/
2560
Christoph Lameter51df1142010-08-20 12:37:15 -05002561struct kmem_cache *kmalloc_caches[SLUB_PAGE_SHIFT];
Christoph Lameter81819f02007-05-06 14:49:36 -07002562EXPORT_SYMBOL(kmalloc_caches);
2563
Christoph Lameter51df1142010-08-20 12:37:15 -05002564static struct kmem_cache *kmem_cache;
2565
Christoph Lameter55136592010-08-20 12:37:13 -05002566#ifdef CONFIG_ZONE_DMA
Christoph Lameter51df1142010-08-20 12:37:15 -05002567static struct kmem_cache *kmalloc_dma_caches[SLUB_PAGE_SHIFT];
Christoph Lameter55136592010-08-20 12:37:13 -05002568#endif
2569
Christoph Lameter81819f02007-05-06 14:49:36 -07002570static int __init setup_slub_min_order(char *str)
2571{
Pekka Enberg06428782008-01-07 23:20:27 -08002572 get_option(&str, &slub_min_order);
Christoph Lameter81819f02007-05-06 14:49:36 -07002573
2574 return 1;
2575}
2576
2577__setup("slub_min_order=", setup_slub_min_order);
2578
2579static int __init setup_slub_max_order(char *str)
2580{
Pekka Enberg06428782008-01-07 23:20:27 -08002581 get_option(&str, &slub_max_order);
David Rientjes818cf592009-04-23 09:58:22 +03002582 slub_max_order = min(slub_max_order, MAX_ORDER - 1);
Christoph Lameter81819f02007-05-06 14:49:36 -07002583
2584 return 1;
2585}
2586
2587__setup("slub_max_order=", setup_slub_max_order);
2588
2589static int __init setup_slub_min_objects(char *str)
2590{
Pekka Enberg06428782008-01-07 23:20:27 -08002591 get_option(&str, &slub_min_objects);
Christoph Lameter81819f02007-05-06 14:49:36 -07002592
2593 return 1;
2594}
2595
2596__setup("slub_min_objects=", setup_slub_min_objects);
2597
2598static int __init setup_slub_nomerge(char *str)
2599{
2600 slub_nomerge = 1;
2601 return 1;
2602}
2603
2604__setup("slub_nomerge", setup_slub_nomerge);
2605
Christoph Lameter51df1142010-08-20 12:37:15 -05002606static struct kmem_cache *__init create_kmalloc_cache(const char *name,
2607 int size, unsigned int flags)
Christoph Lameter81819f02007-05-06 14:49:36 -07002608{
Christoph Lameter51df1142010-08-20 12:37:15 -05002609 struct kmem_cache *s;
2610
2611 s = kmem_cache_alloc(kmem_cache, GFP_NOWAIT);
2612
Pekka Enberg83b519e2009-06-10 19:40:04 +03002613 /*
2614 * This function is called with IRQs disabled during early-boot on
2615 * single CPU so there's no need to take slub_lock here.
2616 */
Christoph Lameter55136592010-08-20 12:37:13 -05002617 if (!kmem_cache_open(s, name, size, ARCH_KMALLOC_MINALIGN,
Christoph Lameter319d1e22008-04-14 19:11:41 +03002618 flags, NULL))
Christoph Lameter81819f02007-05-06 14:49:36 -07002619 goto panic;
2620
2621 list_add(&s->list, &slab_caches);
Christoph Lameter51df1142010-08-20 12:37:15 -05002622 return s;
Christoph Lameter81819f02007-05-06 14:49:36 -07002623
2624panic:
2625 panic("Creation of kmalloc slab %s size=%d failed.\n", name, size);
Christoph Lameter51df1142010-08-20 12:37:15 -05002626 return NULL;
Christoph Lameter81819f02007-05-06 14:49:36 -07002627}
2628
Christoph Lameterf1b26332007-07-17 04:03:26 -07002629/*
2630 * Conversion table for small slabs sizes / 8 to the index in the
2631 * kmalloc array. This is necessary for slabs < 192 since we have non power
2632 * of two cache sizes there. The size of larger slabs can be determined using
2633 * fls.
2634 */
2635static s8 size_index[24] = {
2636 3, /* 8 */
2637 4, /* 16 */
2638 5, /* 24 */
2639 5, /* 32 */
2640 6, /* 40 */
2641 6, /* 48 */
2642 6, /* 56 */
2643 6, /* 64 */
2644 1, /* 72 */
2645 1, /* 80 */
2646 1, /* 88 */
2647 1, /* 96 */
2648 7, /* 104 */
2649 7, /* 112 */
2650 7, /* 120 */
2651 7, /* 128 */
2652 2, /* 136 */
2653 2, /* 144 */
2654 2, /* 152 */
2655 2, /* 160 */
2656 2, /* 168 */
2657 2, /* 176 */
2658 2, /* 184 */
2659 2 /* 192 */
2660};
2661
Aaro Koskinenacdfcd02009-08-28 14:28:54 +03002662static inline int size_index_elem(size_t bytes)
2663{
2664 return (bytes - 1) / 8;
2665}
2666
Christoph Lameter81819f02007-05-06 14:49:36 -07002667static struct kmem_cache *get_slab(size_t size, gfp_t flags)
2668{
Christoph Lameterf1b26332007-07-17 04:03:26 -07002669 int index;
Christoph Lameter81819f02007-05-06 14:49:36 -07002670
Christoph Lameterf1b26332007-07-17 04:03:26 -07002671 if (size <= 192) {
2672 if (!size)
2673 return ZERO_SIZE_PTR;
Christoph Lameter81819f02007-05-06 14:49:36 -07002674
Aaro Koskinenacdfcd02009-08-28 14:28:54 +03002675 index = size_index[size_index_elem(size)];
Christoph Lameteraadb4bc2007-10-16 01:24:38 -07002676 } else
Christoph Lameterf1b26332007-07-17 04:03:26 -07002677 index = fls(size - 1);
Christoph Lameter81819f02007-05-06 14:49:36 -07002678
2679#ifdef CONFIG_ZONE_DMA
Christoph Lameterf1b26332007-07-17 04:03:26 -07002680 if (unlikely((flags & SLUB_DMA)))
Christoph Lameter51df1142010-08-20 12:37:15 -05002681 return kmalloc_dma_caches[index];
Christoph Lameterf1b26332007-07-17 04:03:26 -07002682
Christoph Lameter81819f02007-05-06 14:49:36 -07002683#endif
Christoph Lameter51df1142010-08-20 12:37:15 -05002684 return kmalloc_caches[index];
Christoph Lameter81819f02007-05-06 14:49:36 -07002685}
2686
2687void *__kmalloc(size_t size, gfp_t flags)
2688{
Christoph Lameteraadb4bc2007-10-16 01:24:38 -07002689 struct kmem_cache *s;
Eduard - Gabriel Munteanu5b882be2008-08-19 20:43:26 +03002690 void *ret;
Christoph Lameter81819f02007-05-06 14:49:36 -07002691
Christoph Lameterffadd4d2009-02-17 12:05:07 -05002692 if (unlikely(size > SLUB_MAX_SIZE))
Pekka Enbergeada35e2008-02-11 22:47:46 +02002693 return kmalloc_large(size, flags);
Christoph Lameteraadb4bc2007-10-16 01:24:38 -07002694
2695 s = get_slab(size, flags);
2696
2697 if (unlikely(ZERO_OR_NULL_PTR(s)))
Christoph Lameter6cb8f912007-07-17 04:03:22 -07002698 return s;
2699
Christoph Lameter2154a332010-07-09 14:07:10 -05002700 ret = slab_alloc(s, flags, NUMA_NO_NODE, _RET_IP_);
Eduard - Gabriel Munteanu5b882be2008-08-19 20:43:26 +03002701
Eduard - Gabriel Munteanuca2b84c2009-03-23 15:12:24 +02002702 trace_kmalloc(_RET_IP_, ret, size, s->size, flags);
Eduard - Gabriel Munteanu5b882be2008-08-19 20:43:26 +03002703
2704 return ret;
Christoph Lameter81819f02007-05-06 14:49:36 -07002705}
2706EXPORT_SYMBOL(__kmalloc);
2707
Christoph Lameterf619cfe2008-03-01 13:56:40 -08002708static void *kmalloc_large_node(size_t size, gfp_t flags, int node)
2709{
Vegard Nossumb1eeab62008-11-25 16:55:53 +01002710 struct page *page;
Catalin Marinase4f7c0b42009-07-07 10:32:59 +01002711 void *ptr = NULL;
Christoph Lameterf619cfe2008-03-01 13:56:40 -08002712
Vegard Nossumb1eeab62008-11-25 16:55:53 +01002713 flags |= __GFP_COMP | __GFP_NOTRACK;
2714 page = alloc_pages_node(node, flags, get_order(size));
Christoph Lameterf619cfe2008-03-01 13:56:40 -08002715 if (page)
Catalin Marinase4f7c0b42009-07-07 10:32:59 +01002716 ptr = page_address(page);
2717
2718 kmemleak_alloc(ptr, size, 1, flags);
2719 return ptr;
Christoph Lameterf619cfe2008-03-01 13:56:40 -08002720}
2721
Christoph Lameter81819f02007-05-06 14:49:36 -07002722#ifdef CONFIG_NUMA
2723void *__kmalloc_node(size_t size, gfp_t flags, int node)
2724{
Christoph Lameteraadb4bc2007-10-16 01:24:38 -07002725 struct kmem_cache *s;
Eduard - Gabriel Munteanu5b882be2008-08-19 20:43:26 +03002726 void *ret;
Christoph Lameter81819f02007-05-06 14:49:36 -07002727
Ingo Molnar057685c2009-02-20 12:15:30 +01002728 if (unlikely(size > SLUB_MAX_SIZE)) {
Eduard - Gabriel Munteanu5b882be2008-08-19 20:43:26 +03002729 ret = kmalloc_large_node(size, flags, node);
2730
Eduard - Gabriel Munteanuca2b84c2009-03-23 15:12:24 +02002731 trace_kmalloc_node(_RET_IP_, ret,
2732 size, PAGE_SIZE << get_order(size),
2733 flags, node);
Eduard - Gabriel Munteanu5b882be2008-08-19 20:43:26 +03002734
2735 return ret;
2736 }
Christoph Lameteraadb4bc2007-10-16 01:24:38 -07002737
2738 s = get_slab(size, flags);
2739
2740 if (unlikely(ZERO_OR_NULL_PTR(s)))
Christoph Lameter6cb8f912007-07-17 04:03:22 -07002741 return s;
2742
Eduard - Gabriel Munteanu5b882be2008-08-19 20:43:26 +03002743 ret = slab_alloc(s, flags, node, _RET_IP_);
2744
Eduard - Gabriel Munteanuca2b84c2009-03-23 15:12:24 +02002745 trace_kmalloc_node(_RET_IP_, ret, size, s->size, flags, node);
Eduard - Gabriel Munteanu5b882be2008-08-19 20:43:26 +03002746
2747 return ret;
Christoph Lameter81819f02007-05-06 14:49:36 -07002748}
2749EXPORT_SYMBOL(__kmalloc_node);
2750#endif
2751
2752size_t ksize(const void *object)
2753{
Christoph Lameter272c1d22007-06-08 13:46:49 -07002754 struct page *page;
Christoph Lameter81819f02007-05-06 14:49:36 -07002755 struct kmem_cache *s;
2756
Christoph Lameteref8b4522007-10-16 01:24:46 -07002757 if (unlikely(object == ZERO_SIZE_PTR))
Christoph Lameter272c1d22007-06-08 13:46:49 -07002758 return 0;
2759
Vegard Nossum294a80a2007-12-04 23:45:30 -08002760 page = virt_to_head_page(object);
Vegard Nossum294a80a2007-12-04 23:45:30 -08002761
Pekka Enberg76994412008-05-22 19:22:25 +03002762 if (unlikely(!PageSlab(page))) {
2763 WARN_ON(!PageCompound(page));
Vegard Nossum294a80a2007-12-04 23:45:30 -08002764 return PAGE_SIZE << compound_order(page);
Pekka Enberg76994412008-05-22 19:22:25 +03002765 }
Christoph Lameter81819f02007-05-06 14:49:36 -07002766 s = page->slab;
Christoph Lameter81819f02007-05-06 14:49:36 -07002767
Christoph Lameterae20bfd2008-02-15 23:45:25 -08002768#ifdef CONFIG_SLUB_DEBUG
Christoph Lameter81819f02007-05-06 14:49:36 -07002769 /*
2770 * Debugging requires use of the padding between object
2771 * and whatever may come after it.
2772 */
2773 if (s->flags & (SLAB_RED_ZONE | SLAB_POISON))
2774 return s->objsize;
2775
Christoph Lameterae20bfd2008-02-15 23:45:25 -08002776#endif
Christoph Lameter81819f02007-05-06 14:49:36 -07002777 /*
2778 * If we have the need to store the freelist pointer
2779 * back there or track user information then we can
2780 * only use the space before that information.
2781 */
2782 if (s->flags & (SLAB_DESTROY_BY_RCU | SLAB_STORE_USER))
2783 return s->inuse;
Christoph Lameter81819f02007-05-06 14:49:36 -07002784 /*
2785 * Else we can use all the padding etc for the allocation
2786 */
2787 return s->size;
2788}
Kirill A. Shutemovb1aabec2009-02-10 15:21:44 +02002789EXPORT_SYMBOL(ksize);
Christoph Lameter81819f02007-05-06 14:49:36 -07002790
2791void kfree(const void *x)
2792{
Christoph Lameter81819f02007-05-06 14:49:36 -07002793 struct page *page;
Christoph Lameter5bb983b2008-02-07 17:47:41 -08002794 void *object = (void *)x;
Christoph Lameter81819f02007-05-06 14:49:36 -07002795
Pekka Enberg2121db72009-03-25 11:05:57 +02002796 trace_kfree(_RET_IP_, x);
2797
Satyam Sharma2408c552007-10-16 01:24:44 -07002798 if (unlikely(ZERO_OR_NULL_PTR(x)))
Christoph Lameter81819f02007-05-06 14:49:36 -07002799 return;
2800
Christoph Lameterb49af682007-05-06 14:49:41 -07002801 page = virt_to_head_page(x);
Christoph Lameteraadb4bc2007-10-16 01:24:38 -07002802 if (unlikely(!PageSlab(page))) {
Christoph Lameter09375022008-05-28 10:32:22 -07002803 BUG_ON(!PageCompound(page));
Catalin Marinase4f7c0b42009-07-07 10:32:59 +01002804 kmemleak_free(x);
Christoph Lameteraadb4bc2007-10-16 01:24:38 -07002805 put_page(page);
2806 return;
2807 }
Eduard - Gabriel Munteanuce71e272008-08-19 20:43:25 +03002808 slab_free(page->slab, page, object, _RET_IP_);
Christoph Lameter81819f02007-05-06 14:49:36 -07002809}
2810EXPORT_SYMBOL(kfree);
2811
Christoph Lameter2086d262007-05-06 14:49:46 -07002812/*
Christoph Lameter672bba32007-05-09 02:32:39 -07002813 * kmem_cache_shrink removes empty slabs from the partial lists and sorts
2814 * the remaining slabs by the number of items in use. The slabs with the
2815 * most items in use come first. New allocations will then fill those up
2816 * and thus they can be removed from the partial lists.
2817 *
2818 * The slabs with the least items are placed last. This results in them
2819 * being allocated from last increasing the chance that the last objects
2820 * are freed in them.
Christoph Lameter2086d262007-05-06 14:49:46 -07002821 */
2822int kmem_cache_shrink(struct kmem_cache *s)
2823{
2824 int node;
2825 int i;
2826 struct kmem_cache_node *n;
2827 struct page *page;
2828 struct page *t;
Christoph Lameter205ab992008-04-14 19:11:40 +03002829 int objects = oo_objects(s->max);
Christoph Lameter2086d262007-05-06 14:49:46 -07002830 struct list_head *slabs_by_inuse =
Christoph Lameter834f3d12008-04-14 19:11:31 +03002831 kmalloc(sizeof(struct list_head) * objects, GFP_KERNEL);
Christoph Lameter2086d262007-05-06 14:49:46 -07002832 unsigned long flags;
2833
2834 if (!slabs_by_inuse)
2835 return -ENOMEM;
2836
2837 flush_all(s);
Christoph Lameterf64dc582007-10-16 01:25:33 -07002838 for_each_node_state(node, N_NORMAL_MEMORY) {
Christoph Lameter2086d262007-05-06 14:49:46 -07002839 n = get_node(s, node);
2840
2841 if (!n->nr_partial)
2842 continue;
2843
Christoph Lameter834f3d12008-04-14 19:11:31 +03002844 for (i = 0; i < objects; i++)
Christoph Lameter2086d262007-05-06 14:49:46 -07002845 INIT_LIST_HEAD(slabs_by_inuse + i);
2846
2847 spin_lock_irqsave(&n->list_lock, flags);
2848
2849 /*
Christoph Lameter672bba32007-05-09 02:32:39 -07002850 * Build lists indexed by the items in use in each slab.
Christoph Lameter2086d262007-05-06 14:49:46 -07002851 *
Christoph Lameter672bba32007-05-09 02:32:39 -07002852 * Note that concurrent frees may occur while we hold the
2853 * list_lock. page->inuse here is the upper limit.
Christoph Lameter2086d262007-05-06 14:49:46 -07002854 */
2855 list_for_each_entry_safe(page, t, &n->partial, lru) {
2856 if (!page->inuse && slab_trylock(page)) {
2857 /*
2858 * Must hold slab lock here because slab_free
2859 * may have freed the last object and be
2860 * waiting to release the slab.
2861 */
2862 list_del(&page->lru);
2863 n->nr_partial--;
2864 slab_unlock(page);
2865 discard_slab(s, page);
2866 } else {
Christoph Lameterfcda3d82007-07-30 13:06:46 -07002867 list_move(&page->lru,
2868 slabs_by_inuse + page->inuse);
Christoph Lameter2086d262007-05-06 14:49:46 -07002869 }
2870 }
2871
Christoph Lameter2086d262007-05-06 14:49:46 -07002872 /*
Christoph Lameter672bba32007-05-09 02:32:39 -07002873 * Rebuild the partial list with the slabs filled up most
2874 * first and the least used slabs at the end.
Christoph Lameter2086d262007-05-06 14:49:46 -07002875 */
Christoph Lameter834f3d12008-04-14 19:11:31 +03002876 for (i = objects - 1; i >= 0; i--)
Christoph Lameter2086d262007-05-06 14:49:46 -07002877 list_splice(slabs_by_inuse + i, n->partial.prev);
2878
Christoph Lameter2086d262007-05-06 14:49:46 -07002879 spin_unlock_irqrestore(&n->list_lock, flags);
2880 }
2881
2882 kfree(slabs_by_inuse);
2883 return 0;
2884}
2885EXPORT_SYMBOL(kmem_cache_shrink);
2886
Yasunori Gotob9049e22007-10-21 16:41:37 -07002887#if defined(CONFIG_NUMA) && defined(CONFIG_MEMORY_HOTPLUG)
2888static int slab_mem_going_offline_callback(void *arg)
2889{
2890 struct kmem_cache *s;
2891
2892 down_read(&slub_lock);
2893 list_for_each_entry(s, &slab_caches, list)
2894 kmem_cache_shrink(s);
2895 up_read(&slub_lock);
2896
2897 return 0;
2898}
2899
2900static void slab_mem_offline_callback(void *arg)
2901{
2902 struct kmem_cache_node *n;
2903 struct kmem_cache *s;
2904 struct memory_notify *marg = arg;
2905 int offline_node;
2906
2907 offline_node = marg->status_change_nid;
2908
2909 /*
2910 * If the node still has available memory. we need kmem_cache_node
2911 * for it yet.
2912 */
2913 if (offline_node < 0)
2914 return;
2915
2916 down_read(&slub_lock);
2917 list_for_each_entry(s, &slab_caches, list) {
2918 n = get_node(s, offline_node);
2919 if (n) {
2920 /*
2921 * if n->nr_slabs > 0, slabs still exist on the node
2922 * that is going down. We were unable to free them,
Adam Buchbinderc9404c92009-12-18 15:40:42 -05002923 * and offline_pages() function shouldn't call this
Yasunori Gotob9049e22007-10-21 16:41:37 -07002924 * callback. So, we must fail.
2925 */
Christoph Lameter0f389ec2008-04-14 18:53:02 +03002926 BUG_ON(slabs_node(s, offline_node));
Yasunori Gotob9049e22007-10-21 16:41:37 -07002927
2928 s->node[offline_node] = NULL;
Christoph Lameter8de66a02010-08-25 14:51:14 -05002929 kmem_cache_free(kmem_cache_node, n);
Yasunori Gotob9049e22007-10-21 16:41:37 -07002930 }
2931 }
2932 up_read(&slub_lock);
2933}
2934
2935static int slab_mem_going_online_callback(void *arg)
2936{
2937 struct kmem_cache_node *n;
2938 struct kmem_cache *s;
2939 struct memory_notify *marg = arg;
2940 int nid = marg->status_change_nid;
2941 int ret = 0;
2942
2943 /*
2944 * If the node's memory is already available, then kmem_cache_node is
2945 * already created. Nothing to do.
2946 */
2947 if (nid < 0)
2948 return 0;
2949
2950 /*
Christoph Lameter0121c6192008-04-29 16:11:12 -07002951 * We are bringing a node online. No memory is available yet. We must
Yasunori Gotob9049e22007-10-21 16:41:37 -07002952 * allocate a kmem_cache_node structure in order to bring the node
2953 * online.
2954 */
2955 down_read(&slub_lock);
2956 list_for_each_entry(s, &slab_caches, list) {
2957 /*
2958 * XXX: kmem_cache_alloc_node will fallback to other nodes
2959 * since memory is not yet available from the node that
2960 * is brought up.
2961 */
Christoph Lameter8de66a02010-08-25 14:51:14 -05002962 n = kmem_cache_alloc(kmem_cache_node, GFP_KERNEL);
Yasunori Gotob9049e22007-10-21 16:41:37 -07002963 if (!n) {
2964 ret = -ENOMEM;
2965 goto out;
2966 }
Pekka Enberg5595cff2008-08-05 09:28:47 +03002967 init_kmem_cache_node(n, s);
Yasunori Gotob9049e22007-10-21 16:41:37 -07002968 s->node[nid] = n;
2969 }
2970out:
2971 up_read(&slub_lock);
2972 return ret;
2973}
2974
2975static int slab_memory_callback(struct notifier_block *self,
2976 unsigned long action, void *arg)
2977{
2978 int ret = 0;
2979
2980 switch (action) {
2981 case MEM_GOING_ONLINE:
2982 ret = slab_mem_going_online_callback(arg);
2983 break;
2984 case MEM_GOING_OFFLINE:
2985 ret = slab_mem_going_offline_callback(arg);
2986 break;
2987 case MEM_OFFLINE:
2988 case MEM_CANCEL_ONLINE:
2989 slab_mem_offline_callback(arg);
2990 break;
2991 case MEM_ONLINE:
2992 case MEM_CANCEL_OFFLINE:
2993 break;
2994 }
KAMEZAWA Hiroyukidc19f9d2008-12-01 13:13:48 -08002995 if (ret)
2996 ret = notifier_from_errno(ret);
2997 else
2998 ret = NOTIFY_OK;
Yasunori Gotob9049e22007-10-21 16:41:37 -07002999 return ret;
3000}
3001
3002#endif /* CONFIG_MEMORY_HOTPLUG */
3003
Christoph Lameter81819f02007-05-06 14:49:36 -07003004/********************************************************************
3005 * Basic setup of slabs
3006 *******************************************************************/
3007
Christoph Lameter51df1142010-08-20 12:37:15 -05003008/*
3009 * Used for early kmem_cache structures that were allocated using
3010 * the page allocator
3011 */
3012
3013static void __init kmem_cache_bootstrap_fixup(struct kmem_cache *s)
3014{
3015 int node;
3016
3017 list_add(&s->list, &slab_caches);
3018 s->refcount = -1;
3019
3020 for_each_node_state(node, N_NORMAL_MEMORY) {
3021 struct kmem_cache_node *n = get_node(s, node);
3022 struct page *p;
3023
3024 if (n) {
3025 list_for_each_entry(p, &n->partial, lru)
3026 p->slab = s;
3027
3028#ifdef CONFIG_SLAB_DEBUG
3029 list_for_each_entry(p, &n->full, lru)
3030 p->slab = s;
3031#endif
3032 }
3033 }
3034}
3035
Christoph Lameter81819f02007-05-06 14:49:36 -07003036void __init kmem_cache_init(void)
3037{
3038 int i;
Christoph Lameter4b356be2007-06-16 10:16:13 -07003039 int caches = 0;
Christoph Lameter51df1142010-08-20 12:37:15 -05003040 struct kmem_cache *temp_kmem_cache;
3041 int order;
Christoph Lameter81819f02007-05-06 14:49:36 -07003042
3043#ifdef CONFIG_NUMA
Christoph Lameter51df1142010-08-20 12:37:15 -05003044 struct kmem_cache *temp_kmem_cache_node;
3045 unsigned long kmalloc_size;
3046
3047 kmem_size = offsetof(struct kmem_cache, node) +
3048 nr_node_ids * sizeof(struct kmem_cache_node *);
3049
3050 /* Allocate two kmem_caches from the page allocator */
3051 kmalloc_size = ALIGN(kmem_size, cache_line_size());
3052 order = get_order(2 * kmalloc_size);
3053 kmem_cache = (void *)__get_free_pages(GFP_NOWAIT, order);
3054
Christoph Lameter81819f02007-05-06 14:49:36 -07003055 /*
3056 * Must first have the slab cache available for the allocations of the
Christoph Lameter672bba32007-05-09 02:32:39 -07003057 * struct kmem_cache_node's. There is special bootstrap code in
Christoph Lameter81819f02007-05-06 14:49:36 -07003058 * kmem_cache_open for slab_state == DOWN.
3059 */
Christoph Lameter51df1142010-08-20 12:37:15 -05003060 kmem_cache_node = (void *)kmem_cache + kmalloc_size;
3061
3062 kmem_cache_open(kmem_cache_node, "kmem_cache_node",
3063 sizeof(struct kmem_cache_node),
3064 0, SLAB_HWCACHE_ALIGN | SLAB_PANIC, NULL);
Yasunori Gotob9049e22007-10-21 16:41:37 -07003065
Nadia Derbey0c40ba42008-04-29 01:00:41 -07003066 hotplug_memory_notifier(slab_memory_callback, SLAB_CALLBACK_PRI);
Christoph Lameter51df1142010-08-20 12:37:15 -05003067#else
3068 /* Allocate a single kmem_cache from the page allocator */
3069 kmem_size = sizeof(struct kmem_cache);
3070 order = get_order(kmem_size);
3071 kmem_cache = (void *)__get_free_pages(GFP_NOWAIT, order);
Christoph Lameter81819f02007-05-06 14:49:36 -07003072#endif
3073
3074 /* Able to allocate the per node structures */
3075 slab_state = PARTIAL;
3076
Christoph Lameter51df1142010-08-20 12:37:15 -05003077 temp_kmem_cache = kmem_cache;
3078 kmem_cache_open(kmem_cache, "kmem_cache", kmem_size,
3079 0, SLAB_HWCACHE_ALIGN | SLAB_PANIC, NULL);
3080 kmem_cache = kmem_cache_alloc(kmem_cache, GFP_NOWAIT);
3081 memcpy(kmem_cache, temp_kmem_cache, kmem_size);
Christoph Lameter81819f02007-05-06 14:49:36 -07003082
Christoph Lameter51df1142010-08-20 12:37:15 -05003083#ifdef CONFIG_NUMA
3084 /*
3085 * Allocate kmem_cache_node properly from the kmem_cache slab.
3086 * kmem_cache_node is separately allocated so no need to
3087 * update any list pointers.
3088 */
3089 temp_kmem_cache_node = kmem_cache_node;
Christoph Lameter81819f02007-05-06 14:49:36 -07003090
Christoph Lameter51df1142010-08-20 12:37:15 -05003091 kmem_cache_node = kmem_cache_alloc(kmem_cache, GFP_NOWAIT);
3092 memcpy(kmem_cache_node, temp_kmem_cache_node, kmem_size);
3093
3094 kmem_cache_bootstrap_fixup(kmem_cache_node);
3095
3096 caches++;
3097#else
3098 /*
3099 * kmem_cache has kmem_cache_node embedded and we moved it!
3100 * Update the list heads
3101 */
3102 INIT_LIST_HEAD(&kmem_cache->local_node.partial);
3103 list_splice(&temp_kmem_cache->local_node.partial, &kmem_cache->local_node.partial);
3104#ifdef CONFIG_SLUB_DEBUG
3105 INIT_LIST_HEAD(&kmem_cache->local_node.full);
3106 list_splice(&temp_kmem_cache->local_node.full, &kmem_cache->local_node.full);
3107#endif
3108#endif
3109 kmem_cache_bootstrap_fixup(kmem_cache);
3110 caches++;
3111 /* Free temporary boot structure */
3112 free_pages((unsigned long)temp_kmem_cache, order);
3113
3114 /* Now we can use the kmem_cache to allocate kmalloc slabs */
Christoph Lameterf1b26332007-07-17 04:03:26 -07003115
3116 /*
3117 * Patch up the size_index table if we have strange large alignment
3118 * requirements for the kmalloc array. This is only the case for
Christoph Lameter6446faa2008-02-15 23:45:26 -08003119 * MIPS it seems. The standard arches will not generate any code here.
Christoph Lameterf1b26332007-07-17 04:03:26 -07003120 *
3121 * Largest permitted alignment is 256 bytes due to the way we
3122 * handle the index determination for the smaller caches.
3123 *
3124 * Make sure that nothing crazy happens if someone starts tinkering
3125 * around with ARCH_KMALLOC_MINALIGN
3126 */
3127 BUILD_BUG_ON(KMALLOC_MIN_SIZE > 256 ||
3128 (KMALLOC_MIN_SIZE & (KMALLOC_MIN_SIZE - 1)));
3129
Aaro Koskinenacdfcd02009-08-28 14:28:54 +03003130 for (i = 8; i < KMALLOC_MIN_SIZE; i += 8) {
3131 int elem = size_index_elem(i);
3132 if (elem >= ARRAY_SIZE(size_index))
3133 break;
3134 size_index[elem] = KMALLOC_SHIFT_LOW;
3135 }
Christoph Lameterf1b26332007-07-17 04:03:26 -07003136
Aaro Koskinenacdfcd02009-08-28 14:28:54 +03003137 if (KMALLOC_MIN_SIZE == 64) {
3138 /*
3139 * The 96 byte size cache is not used if the alignment
3140 * is 64 byte.
3141 */
3142 for (i = 64 + 8; i <= 96; i += 8)
3143 size_index[size_index_elem(i)] = 7;
3144 } else if (KMALLOC_MIN_SIZE == 128) {
Christoph Lameter41d54d32008-07-03 09:14:26 -05003145 /*
3146 * The 192 byte sized cache is not used if the alignment
3147 * is 128 byte. Redirect kmalloc to use the 256 byte cache
3148 * instead.
3149 */
3150 for (i = 128 + 8; i <= 192; i += 8)
Aaro Koskinenacdfcd02009-08-28 14:28:54 +03003151 size_index[size_index_elem(i)] = 8;
Christoph Lameter41d54d32008-07-03 09:14:26 -05003152 }
3153
Christoph Lameter51df1142010-08-20 12:37:15 -05003154 /* Caches that are not of the two-to-the-power-of size */
3155 if (KMALLOC_MIN_SIZE <= 32) {
3156 kmalloc_caches[1] = create_kmalloc_cache("kmalloc-96", 96, 0);
3157 caches++;
3158 }
3159
3160 if (KMALLOC_MIN_SIZE <= 64) {
3161 kmalloc_caches[2] = create_kmalloc_cache("kmalloc-192", 192, 0);
3162 caches++;
3163 }
3164
3165 for (i = KMALLOC_SHIFT_LOW; i < SLUB_PAGE_SHIFT; i++) {
3166 kmalloc_caches[i] = create_kmalloc_cache("kmalloc", 1 << i, 0);
3167 caches++;
3168 }
3169
Christoph Lameter81819f02007-05-06 14:49:36 -07003170 slab_state = UP;
3171
3172 /* Provide the correct kmalloc names now that the caches are up */
Pekka Enberg84c1cf62010-09-14 23:21:12 +03003173 if (KMALLOC_MIN_SIZE <= 32) {
3174 kmalloc_caches[1]->name = kstrdup(kmalloc_caches[1]->name, GFP_NOWAIT);
3175 BUG_ON(!kmalloc_caches[1]->name);
3176 }
3177
3178 if (KMALLOC_MIN_SIZE <= 64) {
3179 kmalloc_caches[2]->name = kstrdup(kmalloc_caches[2]->name, GFP_NOWAIT);
3180 BUG_ON(!kmalloc_caches[2]->name);
3181 }
3182
Christoph Lameterd7278bd2010-07-09 14:07:12 -05003183 for (i = KMALLOC_SHIFT_LOW; i < SLUB_PAGE_SHIFT; i++) {
3184 char *s = kasprintf(GFP_NOWAIT, "kmalloc-%d", 1 << i);
3185
3186 BUG_ON(!s);
Christoph Lameter51df1142010-08-20 12:37:15 -05003187 kmalloc_caches[i]->name = s;
Christoph Lameterd7278bd2010-07-09 14:07:12 -05003188 }
Christoph Lameter81819f02007-05-06 14:49:36 -07003189
3190#ifdef CONFIG_SMP
3191 register_cpu_notifier(&slab_notifier);
Christoph Lameter9dfc6e62009-12-18 16:26:20 -06003192#endif
Christoph Lameter81819f02007-05-06 14:49:36 -07003193
Christoph Lameter55136592010-08-20 12:37:13 -05003194#ifdef CONFIG_ZONE_DMA
Christoph Lameter51df1142010-08-20 12:37:15 -05003195 for (i = 0; i < SLUB_PAGE_SHIFT; i++) {
3196 struct kmem_cache *s = kmalloc_caches[i];
Christoph Lameter55136592010-08-20 12:37:13 -05003197
Christoph Lameter51df1142010-08-20 12:37:15 -05003198 if (s && s->size) {
Christoph Lameter55136592010-08-20 12:37:13 -05003199 char *name = kasprintf(GFP_NOWAIT,
3200 "dma-kmalloc-%d", s->objsize);
3201
3202 BUG_ON(!name);
Christoph Lameter51df1142010-08-20 12:37:15 -05003203 kmalloc_dma_caches[i] = create_kmalloc_cache(name,
3204 s->objsize, SLAB_CACHE_DMA);
Christoph Lameter55136592010-08-20 12:37:13 -05003205 }
3206 }
3207#endif
Ingo Molnar3adbefe2008-02-05 17:57:39 -08003208 printk(KERN_INFO
3209 "SLUB: Genslabs=%d, HWalign=%d, Order=%d-%d, MinObjects=%d,"
Christoph Lameter4b356be2007-06-16 10:16:13 -07003210 " CPUs=%d, Nodes=%d\n",
3211 caches, cache_line_size(),
Christoph Lameter81819f02007-05-06 14:49:36 -07003212 slub_min_order, slub_max_order, slub_min_objects,
3213 nr_cpu_ids, nr_node_ids);
3214}
3215
Pekka Enberg7e85ee02009-06-12 14:03:06 +03003216void __init kmem_cache_init_late(void)
3217{
Pekka Enberg7e85ee02009-06-12 14:03:06 +03003218}
3219
Christoph Lameter81819f02007-05-06 14:49:36 -07003220/*
3221 * Find a mergeable slab cache
3222 */
3223static int slab_unmergeable(struct kmem_cache *s)
3224{
3225 if (slub_nomerge || (s->flags & SLUB_NEVER_MERGE))
3226 return 1;
3227
Christoph Lameterc59def9f2007-05-16 22:10:50 -07003228 if (s->ctor)
Christoph Lameter81819f02007-05-06 14:49:36 -07003229 return 1;
3230
Christoph Lameter8ffa6872007-05-31 00:40:51 -07003231 /*
3232 * We may have set a slab to be unmergeable during bootstrap.
3233 */
3234 if (s->refcount < 0)
3235 return 1;
3236
Christoph Lameter81819f02007-05-06 14:49:36 -07003237 return 0;
3238}
3239
3240static struct kmem_cache *find_mergeable(size_t size,
Christoph Lameterba0268a2007-09-11 15:24:11 -07003241 size_t align, unsigned long flags, const char *name,
Alexey Dobriyan51cc5062008-07-25 19:45:34 -07003242 void (*ctor)(void *))
Christoph Lameter81819f02007-05-06 14:49:36 -07003243{
Christoph Lameter5b95a4a2007-07-17 04:03:19 -07003244 struct kmem_cache *s;
Christoph Lameter81819f02007-05-06 14:49:36 -07003245
3246 if (slub_nomerge || (flags & SLUB_NEVER_MERGE))
3247 return NULL;
3248
Christoph Lameterc59def9f2007-05-16 22:10:50 -07003249 if (ctor)
Christoph Lameter81819f02007-05-06 14:49:36 -07003250 return NULL;
3251
3252 size = ALIGN(size, sizeof(void *));
3253 align = calculate_alignment(flags, align, size);
3254 size = ALIGN(size, align);
Christoph Lameterba0268a2007-09-11 15:24:11 -07003255 flags = kmem_cache_flags(size, flags, name, NULL);
Christoph Lameter81819f02007-05-06 14:49:36 -07003256
Christoph Lameter5b95a4a2007-07-17 04:03:19 -07003257 list_for_each_entry(s, &slab_caches, list) {
Christoph Lameter81819f02007-05-06 14:49:36 -07003258 if (slab_unmergeable(s))
3259 continue;
3260
3261 if (size > s->size)
3262 continue;
3263
Christoph Lameterba0268a2007-09-11 15:24:11 -07003264 if ((flags & SLUB_MERGE_SAME) != (s->flags & SLUB_MERGE_SAME))
Christoph Lameter81819f02007-05-06 14:49:36 -07003265 continue;
3266 /*
3267 * Check if alignment is compatible.
3268 * Courtesy of Adrian Drzewiecki
3269 */
Pekka Enberg06428782008-01-07 23:20:27 -08003270 if ((s->size & ~(align - 1)) != s->size)
Christoph Lameter81819f02007-05-06 14:49:36 -07003271 continue;
3272
3273 if (s->size - size >= sizeof(void *))
3274 continue;
3275
3276 return s;
3277 }
3278 return NULL;
3279}
3280
3281struct kmem_cache *kmem_cache_create(const char *name, size_t size,
Alexey Dobriyan51cc5062008-07-25 19:45:34 -07003282 size_t align, unsigned long flags, void (*ctor)(void *))
Christoph Lameter81819f02007-05-06 14:49:36 -07003283{
3284 struct kmem_cache *s;
Pekka Enberg84c1cf62010-09-14 23:21:12 +03003285 char *n;
Christoph Lameter81819f02007-05-06 14:49:36 -07003286
Benjamin Herrenschmidtfe1ff492009-09-21 17:02:30 -07003287 if (WARN_ON(!name))
3288 return NULL;
3289
Christoph Lameter81819f02007-05-06 14:49:36 -07003290 down_write(&slub_lock);
Christoph Lameterba0268a2007-09-11 15:24:11 -07003291 s = find_mergeable(size, align, flags, name, ctor);
Christoph Lameter81819f02007-05-06 14:49:36 -07003292 if (s) {
3293 s->refcount++;
3294 /*
3295 * Adjust the object sizes so that we clear
3296 * the complete object on kzalloc.
3297 */
3298 s->objsize = max(s->objsize, (int)size);
3299 s->inuse = max_t(int, s->inuse, ALIGN(size, sizeof(void *)));
Christoph Lameter6446faa2008-02-15 23:45:26 -08003300
David Rientjes7b8f3b62008-12-17 22:09:46 -08003301 if (sysfs_slab_alias(s, name)) {
David Rientjes7b8f3b62008-12-17 22:09:46 -08003302 s->refcount--;
Christoph Lameter81819f02007-05-06 14:49:36 -07003303 goto err;
David Rientjes7b8f3b62008-12-17 22:09:46 -08003304 }
Christoph Lameter2bce6482010-07-19 11:39:11 -05003305 up_write(&slub_lock);
Christoph Lametera0e1d1b2007-07-17 04:03:31 -07003306 return s;
3307 }
Christoph Lameter6446faa2008-02-15 23:45:26 -08003308
Pekka Enberg84c1cf62010-09-14 23:21:12 +03003309 n = kstrdup(name, GFP_KERNEL);
3310 if (!n)
3311 goto err;
3312
Christoph Lametera0e1d1b2007-07-17 04:03:31 -07003313 s = kmalloc(kmem_size, GFP_KERNEL);
3314 if (s) {
Pekka Enberg84c1cf62010-09-14 23:21:12 +03003315 if (kmem_cache_open(s, n,
Christoph Lameterc59def9f2007-05-16 22:10:50 -07003316 size, align, flags, ctor)) {
Christoph Lameter81819f02007-05-06 14:49:36 -07003317 list_add(&s->list, &slab_caches);
David Rientjes7b8f3b62008-12-17 22:09:46 -08003318 if (sysfs_slab_add(s)) {
David Rientjes7b8f3b62008-12-17 22:09:46 -08003319 list_del(&s->list);
Pekka Enberg84c1cf62010-09-14 23:21:12 +03003320 kfree(n);
David Rientjes7b8f3b62008-12-17 22:09:46 -08003321 kfree(s);
Christoph Lametera0e1d1b2007-07-17 04:03:31 -07003322 goto err;
David Rientjes7b8f3b62008-12-17 22:09:46 -08003323 }
Christoph Lameter2bce6482010-07-19 11:39:11 -05003324 up_write(&slub_lock);
Christoph Lametera0e1d1b2007-07-17 04:03:31 -07003325 return s;
3326 }
Pekka Enberg84c1cf62010-09-14 23:21:12 +03003327 kfree(n);
Christoph Lametera0e1d1b2007-07-17 04:03:31 -07003328 kfree(s);
Christoph Lameter81819f02007-05-06 14:49:36 -07003329 }
3330 up_write(&slub_lock);
Christoph Lameter81819f02007-05-06 14:49:36 -07003331
3332err:
Christoph Lameter81819f02007-05-06 14:49:36 -07003333 if (flags & SLAB_PANIC)
3334 panic("Cannot create slabcache %s\n", name);
3335 else
3336 s = NULL;
3337 return s;
3338}
3339EXPORT_SYMBOL(kmem_cache_create);
3340
Christoph Lameter81819f02007-05-06 14:49:36 -07003341#ifdef CONFIG_SMP
Christoph Lameter27390bc2007-06-01 00:47:09 -07003342/*
Christoph Lameter672bba32007-05-09 02:32:39 -07003343 * Use the cpu notifier to insure that the cpu slabs are flushed when
3344 * necessary.
Christoph Lameter81819f02007-05-06 14:49:36 -07003345 */
3346static int __cpuinit slab_cpuup_callback(struct notifier_block *nfb,
3347 unsigned long action, void *hcpu)
3348{
3349 long cpu = (long)hcpu;
Christoph Lameter5b95a4a2007-07-17 04:03:19 -07003350 struct kmem_cache *s;
3351 unsigned long flags;
Christoph Lameter81819f02007-05-06 14:49:36 -07003352
3353 switch (action) {
3354 case CPU_UP_CANCELED:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07003355 case CPU_UP_CANCELED_FROZEN:
Christoph Lameter81819f02007-05-06 14:49:36 -07003356 case CPU_DEAD:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07003357 case CPU_DEAD_FROZEN:
Christoph Lameter5b95a4a2007-07-17 04:03:19 -07003358 down_read(&slub_lock);
3359 list_for_each_entry(s, &slab_caches, list) {
3360 local_irq_save(flags);
3361 __flush_cpu_slab(s, cpu);
3362 local_irq_restore(flags);
3363 }
3364 up_read(&slub_lock);
Christoph Lameter81819f02007-05-06 14:49:36 -07003365 break;
3366 default:
3367 break;
3368 }
3369 return NOTIFY_OK;
3370}
3371
Pekka Enberg06428782008-01-07 23:20:27 -08003372static struct notifier_block __cpuinitdata slab_notifier = {
Ingo Molnar3adbefe2008-02-05 17:57:39 -08003373 .notifier_call = slab_cpuup_callback
Pekka Enberg06428782008-01-07 23:20:27 -08003374};
Christoph Lameter81819f02007-05-06 14:49:36 -07003375
3376#endif
3377
Eduard - Gabriel Munteanuce71e272008-08-19 20:43:25 +03003378void *__kmalloc_track_caller(size_t size, gfp_t gfpflags, unsigned long caller)
Christoph Lameter81819f02007-05-06 14:49:36 -07003379{
Christoph Lameteraadb4bc2007-10-16 01:24:38 -07003380 struct kmem_cache *s;
Eduard - Gabriel Munteanu94b528d2008-08-24 20:49:35 +03003381 void *ret;
Christoph Lameteraadb4bc2007-10-16 01:24:38 -07003382
Christoph Lameterffadd4d2009-02-17 12:05:07 -05003383 if (unlikely(size > SLUB_MAX_SIZE))
Pekka Enbergeada35e2008-02-11 22:47:46 +02003384 return kmalloc_large(size, gfpflags);
3385
Christoph Lameteraadb4bc2007-10-16 01:24:38 -07003386 s = get_slab(size, gfpflags);
Christoph Lameter81819f02007-05-06 14:49:36 -07003387
Satyam Sharma2408c552007-10-16 01:24:44 -07003388 if (unlikely(ZERO_OR_NULL_PTR(s)))
Christoph Lameter6cb8f912007-07-17 04:03:22 -07003389 return s;
Christoph Lameter81819f02007-05-06 14:49:36 -07003390
Christoph Lameter2154a332010-07-09 14:07:10 -05003391 ret = slab_alloc(s, gfpflags, NUMA_NO_NODE, caller);
Eduard - Gabriel Munteanu94b528d2008-08-24 20:49:35 +03003392
3393 /* Honor the call site pointer we recieved. */
Eduard - Gabriel Munteanuca2b84c2009-03-23 15:12:24 +02003394 trace_kmalloc(caller, ret, size, s->size, gfpflags);
Eduard - Gabriel Munteanu94b528d2008-08-24 20:49:35 +03003395
3396 return ret;
Christoph Lameter81819f02007-05-06 14:49:36 -07003397}
3398
3399void *__kmalloc_node_track_caller(size_t size, gfp_t gfpflags,
Eduard - Gabriel Munteanuce71e272008-08-19 20:43:25 +03003400 int node, unsigned long caller)
Christoph Lameter81819f02007-05-06 14:49:36 -07003401{
Christoph Lameteraadb4bc2007-10-16 01:24:38 -07003402 struct kmem_cache *s;
Eduard - Gabriel Munteanu94b528d2008-08-24 20:49:35 +03003403 void *ret;
Christoph Lameteraadb4bc2007-10-16 01:24:38 -07003404
Xiaotian Fengd3e14aa2010-04-08 17:26:44 +08003405 if (unlikely(size > SLUB_MAX_SIZE)) {
3406 ret = kmalloc_large_node(size, gfpflags, node);
3407
3408 trace_kmalloc_node(caller, ret,
3409 size, PAGE_SIZE << get_order(size),
3410 gfpflags, node);
3411
3412 return ret;
3413 }
Pekka Enbergeada35e2008-02-11 22:47:46 +02003414
Christoph Lameteraadb4bc2007-10-16 01:24:38 -07003415 s = get_slab(size, gfpflags);
Christoph Lameter81819f02007-05-06 14:49:36 -07003416
Satyam Sharma2408c552007-10-16 01:24:44 -07003417 if (unlikely(ZERO_OR_NULL_PTR(s)))
Christoph Lameter6cb8f912007-07-17 04:03:22 -07003418 return s;
Christoph Lameter81819f02007-05-06 14:49:36 -07003419
Eduard - Gabriel Munteanu94b528d2008-08-24 20:49:35 +03003420 ret = slab_alloc(s, gfpflags, node, caller);
3421
3422 /* Honor the call site pointer we recieved. */
Eduard - Gabriel Munteanuca2b84c2009-03-23 15:12:24 +02003423 trace_kmalloc_node(caller, ret, size, s->size, gfpflags, node);
Eduard - Gabriel Munteanu94b528d2008-08-24 20:49:35 +03003424
3425 return ret;
Christoph Lameter81819f02007-05-06 14:49:36 -07003426}
3427
Christoph Lameterf6acb632008-04-29 16:16:06 -07003428#ifdef CONFIG_SLUB_DEBUG
Christoph Lameter205ab992008-04-14 19:11:40 +03003429static int count_inuse(struct page *page)
3430{
3431 return page->inuse;
3432}
3433
3434static int count_total(struct page *page)
3435{
3436 return page->objects;
3437}
3438
Christoph Lameter434e2452007-07-17 04:03:30 -07003439static int validate_slab(struct kmem_cache *s, struct page *page,
3440 unsigned long *map)
Christoph Lameter53e15af2007-05-06 14:49:43 -07003441{
3442 void *p;
Christoph Lametera973e9d2008-03-01 13:40:44 -08003443 void *addr = page_address(page);
Christoph Lameter53e15af2007-05-06 14:49:43 -07003444
3445 if (!check_slab(s, page) ||
3446 !on_freelist(s, page, NULL))
3447 return 0;
3448
3449 /* Now we know that a valid freelist exists */
Christoph Lameter39b26462008-04-14 19:11:30 +03003450 bitmap_zero(map, page->objects);
Christoph Lameter53e15af2007-05-06 14:49:43 -07003451
Christoph Lameter7656c722007-05-09 02:32:40 -07003452 for_each_free_object(p, s, page->freelist) {
3453 set_bit(slab_index(p, s, addr), map);
Christoph Lameter53e15af2007-05-06 14:49:43 -07003454 if (!check_object(s, page, p, 0))
3455 return 0;
3456 }
3457
Christoph Lameter224a88b2008-04-14 19:11:31 +03003458 for_each_object(p, s, addr, page->objects)
Christoph Lameter7656c722007-05-09 02:32:40 -07003459 if (!test_bit(slab_index(p, s, addr), map))
Christoph Lameter53e15af2007-05-06 14:49:43 -07003460 if (!check_object(s, page, p, 1))
3461 return 0;
3462 return 1;
3463}
3464
Christoph Lameter434e2452007-07-17 04:03:30 -07003465static void validate_slab_slab(struct kmem_cache *s, struct page *page,
3466 unsigned long *map)
Christoph Lameter53e15af2007-05-06 14:49:43 -07003467{
3468 if (slab_trylock(page)) {
Christoph Lameter434e2452007-07-17 04:03:30 -07003469 validate_slab(s, page, map);
Christoph Lameter53e15af2007-05-06 14:49:43 -07003470 slab_unlock(page);
3471 } else
3472 printk(KERN_INFO "SLUB %s: Skipped busy slab 0x%p\n",
3473 s->name, page);
Christoph Lameter53e15af2007-05-06 14:49:43 -07003474}
3475
Christoph Lameter434e2452007-07-17 04:03:30 -07003476static int validate_slab_node(struct kmem_cache *s,
3477 struct kmem_cache_node *n, unsigned long *map)
Christoph Lameter53e15af2007-05-06 14:49:43 -07003478{
3479 unsigned long count = 0;
3480 struct page *page;
3481 unsigned long flags;
3482
3483 spin_lock_irqsave(&n->list_lock, flags);
3484
3485 list_for_each_entry(page, &n->partial, lru) {
Christoph Lameter434e2452007-07-17 04:03:30 -07003486 validate_slab_slab(s, page, map);
Christoph Lameter53e15af2007-05-06 14:49:43 -07003487 count++;
3488 }
3489 if (count != n->nr_partial)
3490 printk(KERN_ERR "SLUB %s: %ld partial slabs counted but "
3491 "counter=%ld\n", s->name, count, n->nr_partial);
3492
3493 if (!(s->flags & SLAB_STORE_USER))
3494 goto out;
3495
3496 list_for_each_entry(page, &n->full, lru) {
Christoph Lameter434e2452007-07-17 04:03:30 -07003497 validate_slab_slab(s, page, map);
Christoph Lameter53e15af2007-05-06 14:49:43 -07003498 count++;
3499 }
3500 if (count != atomic_long_read(&n->nr_slabs))
3501 printk(KERN_ERR "SLUB: %s %ld slabs counted but "
3502 "counter=%ld\n", s->name, count,
3503 atomic_long_read(&n->nr_slabs));
3504
3505out:
3506 spin_unlock_irqrestore(&n->list_lock, flags);
3507 return count;
3508}
3509
Christoph Lameter434e2452007-07-17 04:03:30 -07003510static long validate_slab_cache(struct kmem_cache *s)
Christoph Lameter53e15af2007-05-06 14:49:43 -07003511{
3512 int node;
3513 unsigned long count = 0;
Christoph Lameter205ab992008-04-14 19:11:40 +03003514 unsigned long *map = kmalloc(BITS_TO_LONGS(oo_objects(s->max)) *
Christoph Lameter434e2452007-07-17 04:03:30 -07003515 sizeof(unsigned long), GFP_KERNEL);
3516
3517 if (!map)
3518 return -ENOMEM;
Christoph Lameter53e15af2007-05-06 14:49:43 -07003519
3520 flush_all(s);
Christoph Lameterf64dc582007-10-16 01:25:33 -07003521 for_each_node_state(node, N_NORMAL_MEMORY) {
Christoph Lameter53e15af2007-05-06 14:49:43 -07003522 struct kmem_cache_node *n = get_node(s, node);
3523
Christoph Lameter434e2452007-07-17 04:03:30 -07003524 count += validate_slab_node(s, n, map);
Christoph Lameter53e15af2007-05-06 14:49:43 -07003525 }
Christoph Lameter434e2452007-07-17 04:03:30 -07003526 kfree(map);
Christoph Lameter53e15af2007-05-06 14:49:43 -07003527 return count;
3528}
3529
Christoph Lameterb3459702007-05-09 02:32:41 -07003530#ifdef SLUB_RESILIENCY_TEST
3531static void resiliency_test(void)
3532{
3533 u8 *p;
3534
David Rientjesa0164712010-08-25 16:32:27 -07003535 BUILD_BUG_ON(KMALLOC_MIN_SIZE > 16 || SLUB_PAGE_SHIFT < 10);
3536
Christoph Lameterb3459702007-05-09 02:32:41 -07003537 printk(KERN_ERR "SLUB resiliency testing\n");
3538 printk(KERN_ERR "-----------------------\n");
3539 printk(KERN_ERR "A. Corruption after allocation\n");
3540
3541 p = kzalloc(16, GFP_KERNEL);
3542 p[16] = 0x12;
3543 printk(KERN_ERR "\n1. kmalloc-16: Clobber Redzone/next pointer"
3544 " 0x12->0x%p\n\n", p + 16);
3545
David Rientjesa0164712010-08-25 16:32:27 -07003546 validate_slab_cache(kmalloc_caches[4]);
Christoph Lameterb3459702007-05-09 02:32:41 -07003547
3548 /* Hmmm... The next two are dangerous */
3549 p = kzalloc(32, GFP_KERNEL);
3550 p[32 + sizeof(void *)] = 0x34;
3551 printk(KERN_ERR "\n2. kmalloc-32: Clobber next pointer/next slab"
Ingo Molnar3adbefe2008-02-05 17:57:39 -08003552 " 0x34 -> -0x%p\n", p);
3553 printk(KERN_ERR
3554 "If allocated object is overwritten then not detectable\n\n");
Christoph Lameterb3459702007-05-09 02:32:41 -07003555
David Rientjesa0164712010-08-25 16:32:27 -07003556 validate_slab_cache(kmalloc_caches[5]);
Christoph Lameterb3459702007-05-09 02:32:41 -07003557 p = kzalloc(64, GFP_KERNEL);
3558 p += 64 + (get_cycles() & 0xff) * sizeof(void *);
3559 *p = 0x56;
3560 printk(KERN_ERR "\n3. kmalloc-64: corrupting random byte 0x56->0x%p\n",
3561 p);
Ingo Molnar3adbefe2008-02-05 17:57:39 -08003562 printk(KERN_ERR
3563 "If allocated object is overwritten then not detectable\n\n");
David Rientjesa0164712010-08-25 16:32:27 -07003564 validate_slab_cache(kmalloc_caches[6]);
Christoph Lameterb3459702007-05-09 02:32:41 -07003565
3566 printk(KERN_ERR "\nB. Corruption after free\n");
3567 p = kzalloc(128, GFP_KERNEL);
3568 kfree(p);
3569 *p = 0x78;
3570 printk(KERN_ERR "1. kmalloc-128: Clobber first word 0x78->0x%p\n\n", p);
David Rientjesa0164712010-08-25 16:32:27 -07003571 validate_slab_cache(kmalloc_caches[7]);
Christoph Lameterb3459702007-05-09 02:32:41 -07003572
3573 p = kzalloc(256, GFP_KERNEL);
3574 kfree(p);
3575 p[50] = 0x9a;
Ingo Molnar3adbefe2008-02-05 17:57:39 -08003576 printk(KERN_ERR "\n2. kmalloc-256: Clobber 50th byte 0x9a->0x%p\n\n",
3577 p);
David Rientjesa0164712010-08-25 16:32:27 -07003578 validate_slab_cache(kmalloc_caches[8]);
Christoph Lameterb3459702007-05-09 02:32:41 -07003579
3580 p = kzalloc(512, GFP_KERNEL);
3581 kfree(p);
3582 p[512] = 0xab;
3583 printk(KERN_ERR "\n3. kmalloc-512: Clobber redzone 0xab->0x%p\n\n", p);
David Rientjesa0164712010-08-25 16:32:27 -07003584 validate_slab_cache(kmalloc_caches[9]);
Christoph Lameterb3459702007-05-09 02:32:41 -07003585}
3586#else
3587static void resiliency_test(void) {};
3588#endif
3589
Christoph Lameter88a420e2007-05-06 14:49:45 -07003590/*
Christoph Lameter672bba32007-05-09 02:32:39 -07003591 * Generate lists of code addresses where slabcache objects are allocated
Christoph Lameter88a420e2007-05-06 14:49:45 -07003592 * and freed.
3593 */
3594
3595struct location {
3596 unsigned long count;
Eduard - Gabriel Munteanuce71e272008-08-19 20:43:25 +03003597 unsigned long addr;
Christoph Lameter45edfa52007-05-09 02:32:45 -07003598 long long sum_time;
3599 long min_time;
3600 long max_time;
3601 long min_pid;
3602 long max_pid;
Rusty Russell174596a2009-01-01 10:12:29 +10303603 DECLARE_BITMAP(cpus, NR_CPUS);
Christoph Lameter45edfa52007-05-09 02:32:45 -07003604 nodemask_t nodes;
Christoph Lameter88a420e2007-05-06 14:49:45 -07003605};
3606
3607struct loc_track {
3608 unsigned long max;
3609 unsigned long count;
3610 struct location *loc;
3611};
3612
3613static void free_loc_track(struct loc_track *t)
3614{
3615 if (t->max)
3616 free_pages((unsigned long)t->loc,
3617 get_order(sizeof(struct location) * t->max));
3618}
3619
Christoph Lameter68dff6a2007-07-17 04:03:20 -07003620static int alloc_loc_track(struct loc_track *t, unsigned long max, gfp_t flags)
Christoph Lameter88a420e2007-05-06 14:49:45 -07003621{
3622 struct location *l;
3623 int order;
3624
Christoph Lameter88a420e2007-05-06 14:49:45 -07003625 order = get_order(sizeof(struct location) * max);
3626
Christoph Lameter68dff6a2007-07-17 04:03:20 -07003627 l = (void *)__get_free_pages(flags, order);
Christoph Lameter88a420e2007-05-06 14:49:45 -07003628 if (!l)
3629 return 0;
3630
3631 if (t->count) {
3632 memcpy(l, t->loc, sizeof(struct location) * t->count);
3633 free_loc_track(t);
3634 }
3635 t->max = max;
3636 t->loc = l;
3637 return 1;
3638}
3639
3640static int add_location(struct loc_track *t, struct kmem_cache *s,
Christoph Lameter45edfa52007-05-09 02:32:45 -07003641 const struct track *track)
Christoph Lameter88a420e2007-05-06 14:49:45 -07003642{
3643 long start, end, pos;
3644 struct location *l;
Eduard - Gabriel Munteanuce71e272008-08-19 20:43:25 +03003645 unsigned long caddr;
Christoph Lameter45edfa52007-05-09 02:32:45 -07003646 unsigned long age = jiffies - track->when;
Christoph Lameter88a420e2007-05-06 14:49:45 -07003647
3648 start = -1;
3649 end = t->count;
3650
3651 for ( ; ; ) {
3652 pos = start + (end - start + 1) / 2;
3653
3654 /*
3655 * There is nothing at "end". If we end up there
3656 * we need to add something to before end.
3657 */
3658 if (pos == end)
3659 break;
3660
3661 caddr = t->loc[pos].addr;
Christoph Lameter45edfa52007-05-09 02:32:45 -07003662 if (track->addr == caddr) {
3663
3664 l = &t->loc[pos];
3665 l->count++;
3666 if (track->when) {
3667 l->sum_time += age;
3668 if (age < l->min_time)
3669 l->min_time = age;
3670 if (age > l->max_time)
3671 l->max_time = age;
3672
3673 if (track->pid < l->min_pid)
3674 l->min_pid = track->pid;
3675 if (track->pid > l->max_pid)
3676 l->max_pid = track->pid;
3677
Rusty Russell174596a2009-01-01 10:12:29 +10303678 cpumask_set_cpu(track->cpu,
3679 to_cpumask(l->cpus));
Christoph Lameter45edfa52007-05-09 02:32:45 -07003680 }
3681 node_set(page_to_nid(virt_to_page(track)), l->nodes);
Christoph Lameter88a420e2007-05-06 14:49:45 -07003682 return 1;
3683 }
3684
Christoph Lameter45edfa52007-05-09 02:32:45 -07003685 if (track->addr < caddr)
Christoph Lameter88a420e2007-05-06 14:49:45 -07003686 end = pos;
3687 else
3688 start = pos;
3689 }
3690
3691 /*
Christoph Lameter672bba32007-05-09 02:32:39 -07003692 * Not found. Insert new tracking element.
Christoph Lameter88a420e2007-05-06 14:49:45 -07003693 */
Christoph Lameter68dff6a2007-07-17 04:03:20 -07003694 if (t->count >= t->max && !alloc_loc_track(t, 2 * t->max, GFP_ATOMIC))
Christoph Lameter88a420e2007-05-06 14:49:45 -07003695 return 0;
3696
3697 l = t->loc + pos;
3698 if (pos < t->count)
3699 memmove(l + 1, l,
3700 (t->count - pos) * sizeof(struct location));
3701 t->count++;
3702 l->count = 1;
Christoph Lameter45edfa52007-05-09 02:32:45 -07003703 l->addr = track->addr;
3704 l->sum_time = age;
3705 l->min_time = age;
3706 l->max_time = age;
3707 l->min_pid = track->pid;
3708 l->max_pid = track->pid;
Rusty Russell174596a2009-01-01 10:12:29 +10303709 cpumask_clear(to_cpumask(l->cpus));
3710 cpumask_set_cpu(track->cpu, to_cpumask(l->cpus));
Christoph Lameter45edfa52007-05-09 02:32:45 -07003711 nodes_clear(l->nodes);
3712 node_set(page_to_nid(virt_to_page(track)), l->nodes);
Christoph Lameter88a420e2007-05-06 14:49:45 -07003713 return 1;
3714}
3715
3716static void process_slab(struct loc_track *t, struct kmem_cache *s,
Eric Dumazetbbd7d572010-03-24 22:25:47 +01003717 struct page *page, enum track_item alloc,
3718 long *map)
Christoph Lameter88a420e2007-05-06 14:49:45 -07003719{
Christoph Lametera973e9d2008-03-01 13:40:44 -08003720 void *addr = page_address(page);
Christoph Lameter88a420e2007-05-06 14:49:45 -07003721 void *p;
3722
Christoph Lameter39b26462008-04-14 19:11:30 +03003723 bitmap_zero(map, page->objects);
Christoph Lameter7656c722007-05-09 02:32:40 -07003724 for_each_free_object(p, s, page->freelist)
3725 set_bit(slab_index(p, s, addr), map);
Christoph Lameter88a420e2007-05-06 14:49:45 -07003726
Christoph Lameter224a88b2008-04-14 19:11:31 +03003727 for_each_object(p, s, addr, page->objects)
Christoph Lameter45edfa52007-05-09 02:32:45 -07003728 if (!test_bit(slab_index(p, s, addr), map))
3729 add_location(t, s, get_track(s, p, alloc));
Christoph Lameter88a420e2007-05-06 14:49:45 -07003730}
3731
3732static int list_locations(struct kmem_cache *s, char *buf,
3733 enum track_item alloc)
3734{
Harvey Harrisone374d482008-01-31 15:20:50 -08003735 int len = 0;
Christoph Lameter88a420e2007-05-06 14:49:45 -07003736 unsigned long i;
Christoph Lameter68dff6a2007-07-17 04:03:20 -07003737 struct loc_track t = { 0, 0, NULL };
Christoph Lameter88a420e2007-05-06 14:49:45 -07003738 int node;
Eric Dumazetbbd7d572010-03-24 22:25:47 +01003739 unsigned long *map = kmalloc(BITS_TO_LONGS(oo_objects(s->max)) *
3740 sizeof(unsigned long), GFP_KERNEL);
Christoph Lameter88a420e2007-05-06 14:49:45 -07003741
Eric Dumazetbbd7d572010-03-24 22:25:47 +01003742 if (!map || !alloc_loc_track(&t, PAGE_SIZE / sizeof(struct location),
3743 GFP_TEMPORARY)) {
3744 kfree(map);
Christoph Lameter68dff6a2007-07-17 04:03:20 -07003745 return sprintf(buf, "Out of memory\n");
Eric Dumazetbbd7d572010-03-24 22:25:47 +01003746 }
Christoph Lameter88a420e2007-05-06 14:49:45 -07003747 /* Push back cpu slabs */
3748 flush_all(s);
3749
Christoph Lameterf64dc582007-10-16 01:25:33 -07003750 for_each_node_state(node, N_NORMAL_MEMORY) {
Christoph Lameter88a420e2007-05-06 14:49:45 -07003751 struct kmem_cache_node *n = get_node(s, node);
3752 unsigned long flags;
3753 struct page *page;
3754
Christoph Lameter9e869432007-08-22 14:01:56 -07003755 if (!atomic_long_read(&n->nr_slabs))
Christoph Lameter88a420e2007-05-06 14:49:45 -07003756 continue;
3757
3758 spin_lock_irqsave(&n->list_lock, flags);
3759 list_for_each_entry(page, &n->partial, lru)
Eric Dumazetbbd7d572010-03-24 22:25:47 +01003760 process_slab(&t, s, page, alloc, map);
Christoph Lameter88a420e2007-05-06 14:49:45 -07003761 list_for_each_entry(page, &n->full, lru)
Eric Dumazetbbd7d572010-03-24 22:25:47 +01003762 process_slab(&t, s, page, alloc, map);
Christoph Lameter88a420e2007-05-06 14:49:45 -07003763 spin_unlock_irqrestore(&n->list_lock, flags);
3764 }
3765
3766 for (i = 0; i < t.count; i++) {
Christoph Lameter45edfa52007-05-09 02:32:45 -07003767 struct location *l = &t.loc[i];
Christoph Lameter88a420e2007-05-06 14:49:45 -07003768
Hugh Dickins9c246242008-12-09 13:14:27 -08003769 if (len > PAGE_SIZE - KSYM_SYMBOL_LEN - 100)
Christoph Lameter88a420e2007-05-06 14:49:45 -07003770 break;
Harvey Harrisone374d482008-01-31 15:20:50 -08003771 len += sprintf(buf + len, "%7ld ", l->count);
Christoph Lameter45edfa52007-05-09 02:32:45 -07003772
3773 if (l->addr)
Harvey Harrisone374d482008-01-31 15:20:50 -08003774 len += sprint_symbol(buf + len, (unsigned long)l->addr);
Christoph Lameter88a420e2007-05-06 14:49:45 -07003775 else
Harvey Harrisone374d482008-01-31 15:20:50 -08003776 len += sprintf(buf + len, "<not-available>");
Christoph Lameter45edfa52007-05-09 02:32:45 -07003777
3778 if (l->sum_time != l->min_time) {
Harvey Harrisone374d482008-01-31 15:20:50 -08003779 len += sprintf(buf + len, " age=%ld/%ld/%ld",
Roman Zippelf8bd2252008-05-01 04:34:31 -07003780 l->min_time,
3781 (long)div_u64(l->sum_time, l->count),
3782 l->max_time);
Christoph Lameter45edfa52007-05-09 02:32:45 -07003783 } else
Harvey Harrisone374d482008-01-31 15:20:50 -08003784 len += sprintf(buf + len, " age=%ld",
Christoph Lameter45edfa52007-05-09 02:32:45 -07003785 l->min_time);
3786
3787 if (l->min_pid != l->max_pid)
Harvey Harrisone374d482008-01-31 15:20:50 -08003788 len += sprintf(buf + len, " pid=%ld-%ld",
Christoph Lameter45edfa52007-05-09 02:32:45 -07003789 l->min_pid, l->max_pid);
3790 else
Harvey Harrisone374d482008-01-31 15:20:50 -08003791 len += sprintf(buf + len, " pid=%ld",
Christoph Lameter45edfa52007-05-09 02:32:45 -07003792 l->min_pid);
3793
Rusty Russell174596a2009-01-01 10:12:29 +10303794 if (num_online_cpus() > 1 &&
3795 !cpumask_empty(to_cpumask(l->cpus)) &&
Harvey Harrisone374d482008-01-31 15:20:50 -08003796 len < PAGE_SIZE - 60) {
3797 len += sprintf(buf + len, " cpus=");
3798 len += cpulist_scnprintf(buf + len, PAGE_SIZE - len - 50,
Rusty Russell174596a2009-01-01 10:12:29 +10303799 to_cpumask(l->cpus));
Christoph Lameter45edfa52007-05-09 02:32:45 -07003800 }
3801
Christoph Lameter62bc62a2009-06-16 15:32:15 -07003802 if (nr_online_nodes > 1 && !nodes_empty(l->nodes) &&
Harvey Harrisone374d482008-01-31 15:20:50 -08003803 len < PAGE_SIZE - 60) {
3804 len += sprintf(buf + len, " nodes=");
3805 len += nodelist_scnprintf(buf + len, PAGE_SIZE - len - 50,
Christoph Lameter45edfa52007-05-09 02:32:45 -07003806 l->nodes);
3807 }
3808
Harvey Harrisone374d482008-01-31 15:20:50 -08003809 len += sprintf(buf + len, "\n");
Christoph Lameter88a420e2007-05-06 14:49:45 -07003810 }
3811
3812 free_loc_track(&t);
Eric Dumazetbbd7d572010-03-24 22:25:47 +01003813 kfree(map);
Christoph Lameter88a420e2007-05-06 14:49:45 -07003814 if (!t.count)
Harvey Harrisone374d482008-01-31 15:20:50 -08003815 len += sprintf(buf, "No data\n");
3816 return len;
Christoph Lameter88a420e2007-05-06 14:49:45 -07003817}
3818
Christoph Lameter81819f02007-05-06 14:49:36 -07003819enum slab_stat_type {
Christoph Lameter205ab992008-04-14 19:11:40 +03003820 SL_ALL, /* All slabs */
3821 SL_PARTIAL, /* Only partially allocated slabs */
3822 SL_CPU, /* Only slabs used for cpu caches */
3823 SL_OBJECTS, /* Determine allocated objects not slabs */
3824 SL_TOTAL /* Determine object capacity not slabs */
Christoph Lameter81819f02007-05-06 14:49:36 -07003825};
3826
Christoph Lameter205ab992008-04-14 19:11:40 +03003827#define SO_ALL (1 << SL_ALL)
Christoph Lameter81819f02007-05-06 14:49:36 -07003828#define SO_PARTIAL (1 << SL_PARTIAL)
3829#define SO_CPU (1 << SL_CPU)
3830#define SO_OBJECTS (1 << SL_OBJECTS)
Christoph Lameter205ab992008-04-14 19:11:40 +03003831#define SO_TOTAL (1 << SL_TOTAL)
Christoph Lameter81819f02007-05-06 14:49:36 -07003832
Cyrill Gorcunov62e5c4b2008-03-02 23:28:24 +03003833static ssize_t show_slab_objects(struct kmem_cache *s,
3834 char *buf, unsigned long flags)
Christoph Lameter81819f02007-05-06 14:49:36 -07003835{
3836 unsigned long total = 0;
Christoph Lameter81819f02007-05-06 14:49:36 -07003837 int node;
3838 int x;
3839 unsigned long *nodes;
3840 unsigned long *per_cpu;
3841
3842 nodes = kzalloc(2 * sizeof(unsigned long) * nr_node_ids, GFP_KERNEL);
Cyrill Gorcunov62e5c4b2008-03-02 23:28:24 +03003843 if (!nodes)
3844 return -ENOMEM;
Christoph Lameter81819f02007-05-06 14:49:36 -07003845 per_cpu = nodes + nr_node_ids;
3846
Christoph Lameter205ab992008-04-14 19:11:40 +03003847 if (flags & SO_CPU) {
3848 int cpu;
Christoph Lameter81819f02007-05-06 14:49:36 -07003849
Christoph Lameter205ab992008-04-14 19:11:40 +03003850 for_each_possible_cpu(cpu) {
Christoph Lameter9dfc6e62009-12-18 16:26:20 -06003851 struct kmem_cache_cpu *c = per_cpu_ptr(s->cpu_slab, cpu);
Christoph Lameterdfb4f092007-10-16 01:26:05 -07003852
Christoph Lameter205ab992008-04-14 19:11:40 +03003853 if (!c || c->node < 0)
3854 continue;
3855
3856 if (c->page) {
3857 if (flags & SO_TOTAL)
3858 x = c->page->objects;
3859 else if (flags & SO_OBJECTS)
3860 x = c->page->inuse;
Christoph Lameter81819f02007-05-06 14:49:36 -07003861 else
3862 x = 1;
Christoph Lameter205ab992008-04-14 19:11:40 +03003863
Christoph Lameter81819f02007-05-06 14:49:36 -07003864 total += x;
Christoph Lameter205ab992008-04-14 19:11:40 +03003865 nodes[c->node] += x;
Christoph Lameter81819f02007-05-06 14:49:36 -07003866 }
Christoph Lameter205ab992008-04-14 19:11:40 +03003867 per_cpu[c->node]++;
Christoph Lameter81819f02007-05-06 14:49:36 -07003868 }
3869 }
3870
Christoph Lameter205ab992008-04-14 19:11:40 +03003871 if (flags & SO_ALL) {
3872 for_each_node_state(node, N_NORMAL_MEMORY) {
3873 struct kmem_cache_node *n = get_node(s, node);
Christoph Lameter81819f02007-05-06 14:49:36 -07003874
Christoph Lameter205ab992008-04-14 19:11:40 +03003875 if (flags & SO_TOTAL)
3876 x = atomic_long_read(&n->total_objects);
3877 else if (flags & SO_OBJECTS)
3878 x = atomic_long_read(&n->total_objects) -
3879 count_partial(n, count_free);
3880
3881 else
3882 x = atomic_long_read(&n->nr_slabs);
3883 total += x;
3884 nodes[node] += x;
3885 }
3886
3887 } else if (flags & SO_PARTIAL) {
3888 for_each_node_state(node, N_NORMAL_MEMORY) {
3889 struct kmem_cache_node *n = get_node(s, node);
3890
3891 if (flags & SO_TOTAL)
3892 x = count_partial(n, count_total);
3893 else if (flags & SO_OBJECTS)
3894 x = count_partial(n, count_inuse);
Christoph Lameter81819f02007-05-06 14:49:36 -07003895 else
3896 x = n->nr_partial;
3897 total += x;
3898 nodes[node] += x;
3899 }
Christoph Lameter81819f02007-05-06 14:49:36 -07003900 }
Christoph Lameter81819f02007-05-06 14:49:36 -07003901 x = sprintf(buf, "%lu", total);
3902#ifdef CONFIG_NUMA
Christoph Lameterf64dc582007-10-16 01:25:33 -07003903 for_each_node_state(node, N_NORMAL_MEMORY)
Christoph Lameter81819f02007-05-06 14:49:36 -07003904 if (nodes[node])
3905 x += sprintf(buf + x, " N%d=%lu",
3906 node, nodes[node]);
3907#endif
3908 kfree(nodes);
3909 return x + sprintf(buf + x, "\n");
3910}
3911
3912static int any_slab_objects(struct kmem_cache *s)
3913{
3914 int node;
Christoph Lameterdfb4f092007-10-16 01:26:05 -07003915
3916 for_each_online_node(node) {
Christoph Lameter81819f02007-05-06 14:49:36 -07003917 struct kmem_cache_node *n = get_node(s, node);
3918
Christoph Lameterdfb4f092007-10-16 01:26:05 -07003919 if (!n)
3920 continue;
3921
Benjamin Herrenschmidt4ea33e22008-05-06 20:42:39 -07003922 if (atomic_long_read(&n->total_objects))
Christoph Lameter81819f02007-05-06 14:49:36 -07003923 return 1;
3924 }
3925 return 0;
3926}
3927
3928#define to_slab_attr(n) container_of(n, struct slab_attribute, attr)
3929#define to_slab(n) container_of(n, struct kmem_cache, kobj);
3930
3931struct slab_attribute {
3932 struct attribute attr;
3933 ssize_t (*show)(struct kmem_cache *s, char *buf);
3934 ssize_t (*store)(struct kmem_cache *s, const char *x, size_t count);
3935};
3936
3937#define SLAB_ATTR_RO(_name) \
3938 static struct slab_attribute _name##_attr = __ATTR_RO(_name)
3939
3940#define SLAB_ATTR(_name) \
3941 static struct slab_attribute _name##_attr = \
3942 __ATTR(_name, 0644, _name##_show, _name##_store)
3943
Christoph Lameter81819f02007-05-06 14:49:36 -07003944static ssize_t slab_size_show(struct kmem_cache *s, char *buf)
3945{
3946 return sprintf(buf, "%d\n", s->size);
3947}
3948SLAB_ATTR_RO(slab_size);
3949
3950static ssize_t align_show(struct kmem_cache *s, char *buf)
3951{
3952 return sprintf(buf, "%d\n", s->align);
3953}
3954SLAB_ATTR_RO(align);
3955
3956static ssize_t object_size_show(struct kmem_cache *s, char *buf)
3957{
3958 return sprintf(buf, "%d\n", s->objsize);
3959}
3960SLAB_ATTR_RO(object_size);
3961
3962static ssize_t objs_per_slab_show(struct kmem_cache *s, char *buf)
3963{
Christoph Lameter834f3d12008-04-14 19:11:31 +03003964 return sprintf(buf, "%d\n", oo_objects(s->oo));
Christoph Lameter81819f02007-05-06 14:49:36 -07003965}
3966SLAB_ATTR_RO(objs_per_slab);
3967
Christoph Lameter06b285d2008-04-14 19:11:41 +03003968static ssize_t order_store(struct kmem_cache *s,
3969 const char *buf, size_t length)
3970{
Christoph Lameter0121c6192008-04-29 16:11:12 -07003971 unsigned long order;
3972 int err;
3973
3974 err = strict_strtoul(buf, 10, &order);
3975 if (err)
3976 return err;
Christoph Lameter06b285d2008-04-14 19:11:41 +03003977
3978 if (order > slub_max_order || order < slub_min_order)
3979 return -EINVAL;
3980
3981 calculate_sizes(s, order);
3982 return length;
3983}
3984
Christoph Lameter81819f02007-05-06 14:49:36 -07003985static ssize_t order_show(struct kmem_cache *s, char *buf)
3986{
Christoph Lameter834f3d12008-04-14 19:11:31 +03003987 return sprintf(buf, "%d\n", oo_order(s->oo));
Christoph Lameter81819f02007-05-06 14:49:36 -07003988}
Christoph Lameter06b285d2008-04-14 19:11:41 +03003989SLAB_ATTR(order);
Christoph Lameter81819f02007-05-06 14:49:36 -07003990
David Rientjes73d342b2009-02-22 17:40:09 -08003991static ssize_t min_partial_show(struct kmem_cache *s, char *buf)
3992{
3993 return sprintf(buf, "%lu\n", s->min_partial);
3994}
3995
3996static ssize_t min_partial_store(struct kmem_cache *s, const char *buf,
3997 size_t length)
3998{
3999 unsigned long min;
4000 int err;
4001
4002 err = strict_strtoul(buf, 10, &min);
4003 if (err)
4004 return err;
4005
David Rientjesc0bdb232009-02-25 09:16:35 +02004006 set_min_partial(s, min);
David Rientjes73d342b2009-02-22 17:40:09 -08004007 return length;
4008}
4009SLAB_ATTR(min_partial);
4010
Christoph Lameter81819f02007-05-06 14:49:36 -07004011static ssize_t ctor_show(struct kmem_cache *s, char *buf)
4012{
4013 if (s->ctor) {
4014 int n = sprint_symbol(buf, (unsigned long)s->ctor);
4015
4016 return n + sprintf(buf + n, "\n");
4017 }
4018 return 0;
4019}
4020SLAB_ATTR_RO(ctor);
4021
Christoph Lameter81819f02007-05-06 14:49:36 -07004022static ssize_t aliases_show(struct kmem_cache *s, char *buf)
4023{
4024 return sprintf(buf, "%d\n", s->refcount - 1);
4025}
4026SLAB_ATTR_RO(aliases);
4027
4028static ssize_t slabs_show(struct kmem_cache *s, char *buf)
4029{
Christoph Lameter205ab992008-04-14 19:11:40 +03004030 return show_slab_objects(s, buf, SO_ALL);
Christoph Lameter81819f02007-05-06 14:49:36 -07004031}
4032SLAB_ATTR_RO(slabs);
4033
4034static ssize_t partial_show(struct kmem_cache *s, char *buf)
4035{
Christoph Lameterd9acf4b2008-02-15 15:22:21 -08004036 return show_slab_objects(s, buf, SO_PARTIAL);
Christoph Lameter81819f02007-05-06 14:49:36 -07004037}
4038SLAB_ATTR_RO(partial);
4039
4040static ssize_t cpu_slabs_show(struct kmem_cache *s, char *buf)
4041{
Christoph Lameterd9acf4b2008-02-15 15:22:21 -08004042 return show_slab_objects(s, buf, SO_CPU);
Christoph Lameter81819f02007-05-06 14:49:36 -07004043}
4044SLAB_ATTR_RO(cpu_slabs);
4045
4046static ssize_t objects_show(struct kmem_cache *s, char *buf)
4047{
Christoph Lameter205ab992008-04-14 19:11:40 +03004048 return show_slab_objects(s, buf, SO_ALL|SO_OBJECTS);
Christoph Lameter81819f02007-05-06 14:49:36 -07004049}
4050SLAB_ATTR_RO(objects);
4051
Christoph Lameter205ab992008-04-14 19:11:40 +03004052static ssize_t objects_partial_show(struct kmem_cache *s, char *buf)
4053{
4054 return show_slab_objects(s, buf, SO_PARTIAL|SO_OBJECTS);
4055}
4056SLAB_ATTR_RO(objects_partial);
4057
4058static ssize_t total_objects_show(struct kmem_cache *s, char *buf)
4059{
4060 return show_slab_objects(s, buf, SO_ALL|SO_TOTAL);
4061}
4062SLAB_ATTR_RO(total_objects);
4063
Christoph Lameter81819f02007-05-06 14:49:36 -07004064static ssize_t sanity_checks_show(struct kmem_cache *s, char *buf)
4065{
4066 return sprintf(buf, "%d\n", !!(s->flags & SLAB_DEBUG_FREE));
4067}
4068
4069static ssize_t sanity_checks_store(struct kmem_cache *s,
4070 const char *buf, size_t length)
4071{
4072 s->flags &= ~SLAB_DEBUG_FREE;
4073 if (buf[0] == '1')
4074 s->flags |= SLAB_DEBUG_FREE;
4075 return length;
4076}
4077SLAB_ATTR(sanity_checks);
4078
4079static ssize_t trace_show(struct kmem_cache *s, char *buf)
4080{
4081 return sprintf(buf, "%d\n", !!(s->flags & SLAB_TRACE));
4082}
4083
4084static ssize_t trace_store(struct kmem_cache *s, const char *buf,
4085 size_t length)
4086{
4087 s->flags &= ~SLAB_TRACE;
4088 if (buf[0] == '1')
4089 s->flags |= SLAB_TRACE;
4090 return length;
4091}
4092SLAB_ATTR(trace);
4093
Dmitry Monakhov4c13dd32010-02-26 09:36:12 +03004094#ifdef CONFIG_FAILSLAB
4095static ssize_t failslab_show(struct kmem_cache *s, char *buf)
4096{
4097 return sprintf(buf, "%d\n", !!(s->flags & SLAB_FAILSLAB));
4098}
4099
4100static ssize_t failslab_store(struct kmem_cache *s, const char *buf,
4101 size_t length)
4102{
4103 s->flags &= ~SLAB_FAILSLAB;
4104 if (buf[0] == '1')
4105 s->flags |= SLAB_FAILSLAB;
4106 return length;
4107}
4108SLAB_ATTR(failslab);
4109#endif
4110
Christoph Lameter81819f02007-05-06 14:49:36 -07004111static ssize_t reclaim_account_show(struct kmem_cache *s, char *buf)
4112{
4113 return sprintf(buf, "%d\n", !!(s->flags & SLAB_RECLAIM_ACCOUNT));
4114}
4115
4116static ssize_t reclaim_account_store(struct kmem_cache *s,
4117 const char *buf, size_t length)
4118{
4119 s->flags &= ~SLAB_RECLAIM_ACCOUNT;
4120 if (buf[0] == '1')
4121 s->flags |= SLAB_RECLAIM_ACCOUNT;
4122 return length;
4123}
4124SLAB_ATTR(reclaim_account);
4125
4126static ssize_t hwcache_align_show(struct kmem_cache *s, char *buf)
4127{
Christoph Lameter5af60832007-05-06 14:49:56 -07004128 return sprintf(buf, "%d\n", !!(s->flags & SLAB_HWCACHE_ALIGN));
Christoph Lameter81819f02007-05-06 14:49:36 -07004129}
4130SLAB_ATTR_RO(hwcache_align);
4131
4132#ifdef CONFIG_ZONE_DMA
4133static ssize_t cache_dma_show(struct kmem_cache *s, char *buf)
4134{
4135 return sprintf(buf, "%d\n", !!(s->flags & SLAB_CACHE_DMA));
4136}
4137SLAB_ATTR_RO(cache_dma);
4138#endif
4139
4140static ssize_t destroy_by_rcu_show(struct kmem_cache *s, char *buf)
4141{
4142 return sprintf(buf, "%d\n", !!(s->flags & SLAB_DESTROY_BY_RCU));
4143}
4144SLAB_ATTR_RO(destroy_by_rcu);
4145
4146static ssize_t red_zone_show(struct kmem_cache *s, char *buf)
4147{
4148 return sprintf(buf, "%d\n", !!(s->flags & SLAB_RED_ZONE));
4149}
4150
4151static ssize_t red_zone_store(struct kmem_cache *s,
4152 const char *buf, size_t length)
4153{
4154 if (any_slab_objects(s))
4155 return -EBUSY;
4156
4157 s->flags &= ~SLAB_RED_ZONE;
4158 if (buf[0] == '1')
4159 s->flags |= SLAB_RED_ZONE;
Christoph Lameter06b285d2008-04-14 19:11:41 +03004160 calculate_sizes(s, -1);
Christoph Lameter81819f02007-05-06 14:49:36 -07004161 return length;
4162}
4163SLAB_ATTR(red_zone);
4164
4165static ssize_t poison_show(struct kmem_cache *s, char *buf)
4166{
4167 return sprintf(buf, "%d\n", !!(s->flags & SLAB_POISON));
4168}
4169
4170static ssize_t poison_store(struct kmem_cache *s,
4171 const char *buf, size_t length)
4172{
4173 if (any_slab_objects(s))
4174 return -EBUSY;
4175
4176 s->flags &= ~SLAB_POISON;
4177 if (buf[0] == '1')
4178 s->flags |= SLAB_POISON;
Christoph Lameter06b285d2008-04-14 19:11:41 +03004179 calculate_sizes(s, -1);
Christoph Lameter81819f02007-05-06 14:49:36 -07004180 return length;
4181}
4182SLAB_ATTR(poison);
4183
4184static ssize_t store_user_show(struct kmem_cache *s, char *buf)
4185{
4186 return sprintf(buf, "%d\n", !!(s->flags & SLAB_STORE_USER));
4187}
4188
4189static ssize_t store_user_store(struct kmem_cache *s,
4190 const char *buf, size_t length)
4191{
4192 if (any_slab_objects(s))
4193 return -EBUSY;
4194
4195 s->flags &= ~SLAB_STORE_USER;
4196 if (buf[0] == '1')
4197 s->flags |= SLAB_STORE_USER;
Christoph Lameter06b285d2008-04-14 19:11:41 +03004198 calculate_sizes(s, -1);
Christoph Lameter81819f02007-05-06 14:49:36 -07004199 return length;
4200}
4201SLAB_ATTR(store_user);
4202
Christoph Lameter53e15af2007-05-06 14:49:43 -07004203static ssize_t validate_show(struct kmem_cache *s, char *buf)
4204{
4205 return 0;
4206}
4207
4208static ssize_t validate_store(struct kmem_cache *s,
4209 const char *buf, size_t length)
4210{
Christoph Lameter434e2452007-07-17 04:03:30 -07004211 int ret = -EINVAL;
4212
4213 if (buf[0] == '1') {
4214 ret = validate_slab_cache(s);
4215 if (ret >= 0)
4216 ret = length;
4217 }
4218 return ret;
Christoph Lameter53e15af2007-05-06 14:49:43 -07004219}
4220SLAB_ATTR(validate);
4221
Christoph Lameter2086d262007-05-06 14:49:46 -07004222static ssize_t shrink_show(struct kmem_cache *s, char *buf)
4223{
4224 return 0;
4225}
4226
4227static ssize_t shrink_store(struct kmem_cache *s,
4228 const char *buf, size_t length)
4229{
4230 if (buf[0] == '1') {
4231 int rc = kmem_cache_shrink(s);
4232
4233 if (rc)
4234 return rc;
4235 } else
4236 return -EINVAL;
4237 return length;
4238}
4239SLAB_ATTR(shrink);
4240
Christoph Lameter88a420e2007-05-06 14:49:45 -07004241static ssize_t alloc_calls_show(struct kmem_cache *s, char *buf)
4242{
4243 if (!(s->flags & SLAB_STORE_USER))
4244 return -ENOSYS;
4245 return list_locations(s, buf, TRACK_ALLOC);
4246}
4247SLAB_ATTR_RO(alloc_calls);
4248
4249static ssize_t free_calls_show(struct kmem_cache *s, char *buf)
4250{
4251 if (!(s->flags & SLAB_STORE_USER))
4252 return -ENOSYS;
4253 return list_locations(s, buf, TRACK_FREE);
4254}
4255SLAB_ATTR_RO(free_calls);
4256
Christoph Lameter81819f02007-05-06 14:49:36 -07004257#ifdef CONFIG_NUMA
Christoph Lameter98246012008-01-07 23:20:26 -08004258static ssize_t remote_node_defrag_ratio_show(struct kmem_cache *s, char *buf)
Christoph Lameter81819f02007-05-06 14:49:36 -07004259{
Christoph Lameter98246012008-01-07 23:20:26 -08004260 return sprintf(buf, "%d\n", s->remote_node_defrag_ratio / 10);
Christoph Lameter81819f02007-05-06 14:49:36 -07004261}
4262
Christoph Lameter98246012008-01-07 23:20:26 -08004263static ssize_t remote_node_defrag_ratio_store(struct kmem_cache *s,
Christoph Lameter81819f02007-05-06 14:49:36 -07004264 const char *buf, size_t length)
4265{
Christoph Lameter0121c6192008-04-29 16:11:12 -07004266 unsigned long ratio;
4267 int err;
Christoph Lameter81819f02007-05-06 14:49:36 -07004268
Christoph Lameter0121c6192008-04-29 16:11:12 -07004269 err = strict_strtoul(buf, 10, &ratio);
4270 if (err)
4271 return err;
4272
Christoph Lametere2cb96b2008-08-19 08:51:22 -05004273 if (ratio <= 100)
Christoph Lameter0121c6192008-04-29 16:11:12 -07004274 s->remote_node_defrag_ratio = ratio * 10;
4275
Christoph Lameter81819f02007-05-06 14:49:36 -07004276 return length;
4277}
Christoph Lameter98246012008-01-07 23:20:26 -08004278SLAB_ATTR(remote_node_defrag_ratio);
Christoph Lameter81819f02007-05-06 14:49:36 -07004279#endif
4280
Christoph Lameter8ff12cf2008-02-07 17:47:41 -08004281#ifdef CONFIG_SLUB_STATS
Christoph Lameter8ff12cf2008-02-07 17:47:41 -08004282static int show_stat(struct kmem_cache *s, char *buf, enum stat_item si)
4283{
4284 unsigned long sum = 0;
4285 int cpu;
4286 int len;
4287 int *data = kmalloc(nr_cpu_ids * sizeof(int), GFP_KERNEL);
4288
4289 if (!data)
4290 return -ENOMEM;
4291
4292 for_each_online_cpu(cpu) {
Christoph Lameter9dfc6e62009-12-18 16:26:20 -06004293 unsigned x = per_cpu_ptr(s->cpu_slab, cpu)->stat[si];
Christoph Lameter8ff12cf2008-02-07 17:47:41 -08004294
4295 data[cpu] = x;
4296 sum += x;
4297 }
4298
4299 len = sprintf(buf, "%lu", sum);
4300
Christoph Lameter50ef37b2008-04-14 18:52:05 +03004301#ifdef CONFIG_SMP
Christoph Lameter8ff12cf2008-02-07 17:47:41 -08004302 for_each_online_cpu(cpu) {
4303 if (data[cpu] && len < PAGE_SIZE - 20)
Christoph Lameter50ef37b2008-04-14 18:52:05 +03004304 len += sprintf(buf + len, " C%d=%u", cpu, data[cpu]);
Christoph Lameter8ff12cf2008-02-07 17:47:41 -08004305 }
Christoph Lameter50ef37b2008-04-14 18:52:05 +03004306#endif
Christoph Lameter8ff12cf2008-02-07 17:47:41 -08004307 kfree(data);
4308 return len + sprintf(buf + len, "\n");
4309}
4310
David Rientjes78eb00c2009-10-15 02:20:22 -07004311static void clear_stat(struct kmem_cache *s, enum stat_item si)
4312{
4313 int cpu;
4314
4315 for_each_online_cpu(cpu)
Christoph Lameter9dfc6e62009-12-18 16:26:20 -06004316 per_cpu_ptr(s->cpu_slab, cpu)->stat[si] = 0;
David Rientjes78eb00c2009-10-15 02:20:22 -07004317}
4318
Christoph Lameter8ff12cf2008-02-07 17:47:41 -08004319#define STAT_ATTR(si, text) \
4320static ssize_t text##_show(struct kmem_cache *s, char *buf) \
4321{ \
4322 return show_stat(s, buf, si); \
4323} \
David Rientjes78eb00c2009-10-15 02:20:22 -07004324static ssize_t text##_store(struct kmem_cache *s, \
4325 const char *buf, size_t length) \
4326{ \
4327 if (buf[0] != '0') \
4328 return -EINVAL; \
4329 clear_stat(s, si); \
4330 return length; \
4331} \
4332SLAB_ATTR(text); \
Christoph Lameter8ff12cf2008-02-07 17:47:41 -08004333
4334STAT_ATTR(ALLOC_FASTPATH, alloc_fastpath);
4335STAT_ATTR(ALLOC_SLOWPATH, alloc_slowpath);
4336STAT_ATTR(FREE_FASTPATH, free_fastpath);
4337STAT_ATTR(FREE_SLOWPATH, free_slowpath);
4338STAT_ATTR(FREE_FROZEN, free_frozen);
4339STAT_ATTR(FREE_ADD_PARTIAL, free_add_partial);
4340STAT_ATTR(FREE_REMOVE_PARTIAL, free_remove_partial);
4341STAT_ATTR(ALLOC_FROM_PARTIAL, alloc_from_partial);
4342STAT_ATTR(ALLOC_SLAB, alloc_slab);
4343STAT_ATTR(ALLOC_REFILL, alloc_refill);
4344STAT_ATTR(FREE_SLAB, free_slab);
4345STAT_ATTR(CPUSLAB_FLUSH, cpuslab_flush);
4346STAT_ATTR(DEACTIVATE_FULL, deactivate_full);
4347STAT_ATTR(DEACTIVATE_EMPTY, deactivate_empty);
4348STAT_ATTR(DEACTIVATE_TO_HEAD, deactivate_to_head);
4349STAT_ATTR(DEACTIVATE_TO_TAIL, deactivate_to_tail);
4350STAT_ATTR(DEACTIVATE_REMOTE_FREES, deactivate_remote_frees);
Christoph Lameter65c33762008-04-14 19:11:40 +03004351STAT_ATTR(ORDER_FALLBACK, order_fallback);
Christoph Lameter8ff12cf2008-02-07 17:47:41 -08004352#endif
4353
Pekka Enberg06428782008-01-07 23:20:27 -08004354static struct attribute *slab_attrs[] = {
Christoph Lameter81819f02007-05-06 14:49:36 -07004355 &slab_size_attr.attr,
4356 &object_size_attr.attr,
4357 &objs_per_slab_attr.attr,
4358 &order_attr.attr,
David Rientjes73d342b2009-02-22 17:40:09 -08004359 &min_partial_attr.attr,
Christoph Lameter81819f02007-05-06 14:49:36 -07004360 &objects_attr.attr,
Christoph Lameter205ab992008-04-14 19:11:40 +03004361 &objects_partial_attr.attr,
4362 &total_objects_attr.attr,
Christoph Lameter81819f02007-05-06 14:49:36 -07004363 &slabs_attr.attr,
4364 &partial_attr.attr,
4365 &cpu_slabs_attr.attr,
4366 &ctor_attr.attr,
Christoph Lameter81819f02007-05-06 14:49:36 -07004367 &aliases_attr.attr,
4368 &align_attr.attr,
4369 &sanity_checks_attr.attr,
4370 &trace_attr.attr,
4371 &hwcache_align_attr.attr,
4372 &reclaim_account_attr.attr,
4373 &destroy_by_rcu_attr.attr,
4374 &red_zone_attr.attr,
4375 &poison_attr.attr,
4376 &store_user_attr.attr,
Christoph Lameter53e15af2007-05-06 14:49:43 -07004377 &validate_attr.attr,
Christoph Lameter2086d262007-05-06 14:49:46 -07004378 &shrink_attr.attr,
Christoph Lameter88a420e2007-05-06 14:49:45 -07004379 &alloc_calls_attr.attr,
4380 &free_calls_attr.attr,
Christoph Lameter81819f02007-05-06 14:49:36 -07004381#ifdef CONFIG_ZONE_DMA
4382 &cache_dma_attr.attr,
4383#endif
4384#ifdef CONFIG_NUMA
Christoph Lameter98246012008-01-07 23:20:26 -08004385 &remote_node_defrag_ratio_attr.attr,
Christoph Lameter81819f02007-05-06 14:49:36 -07004386#endif
Christoph Lameter8ff12cf2008-02-07 17:47:41 -08004387#ifdef CONFIG_SLUB_STATS
4388 &alloc_fastpath_attr.attr,
4389 &alloc_slowpath_attr.attr,
4390 &free_fastpath_attr.attr,
4391 &free_slowpath_attr.attr,
4392 &free_frozen_attr.attr,
4393 &free_add_partial_attr.attr,
4394 &free_remove_partial_attr.attr,
4395 &alloc_from_partial_attr.attr,
4396 &alloc_slab_attr.attr,
4397 &alloc_refill_attr.attr,
4398 &free_slab_attr.attr,
4399 &cpuslab_flush_attr.attr,
4400 &deactivate_full_attr.attr,
4401 &deactivate_empty_attr.attr,
4402 &deactivate_to_head_attr.attr,
4403 &deactivate_to_tail_attr.attr,
4404 &deactivate_remote_frees_attr.attr,
Christoph Lameter65c33762008-04-14 19:11:40 +03004405 &order_fallback_attr.attr,
Christoph Lameter8ff12cf2008-02-07 17:47:41 -08004406#endif
Dmitry Monakhov4c13dd32010-02-26 09:36:12 +03004407#ifdef CONFIG_FAILSLAB
4408 &failslab_attr.attr,
4409#endif
4410
Christoph Lameter81819f02007-05-06 14:49:36 -07004411 NULL
4412};
4413
4414static struct attribute_group slab_attr_group = {
4415 .attrs = slab_attrs,
4416};
4417
4418static ssize_t slab_attr_show(struct kobject *kobj,
4419 struct attribute *attr,
4420 char *buf)
4421{
4422 struct slab_attribute *attribute;
4423 struct kmem_cache *s;
4424 int err;
4425
4426 attribute = to_slab_attr(attr);
4427 s = to_slab(kobj);
4428
4429 if (!attribute->show)
4430 return -EIO;
4431
4432 err = attribute->show(s, buf);
4433
4434 return err;
4435}
4436
4437static ssize_t slab_attr_store(struct kobject *kobj,
4438 struct attribute *attr,
4439 const char *buf, size_t len)
4440{
4441 struct slab_attribute *attribute;
4442 struct kmem_cache *s;
4443 int err;
4444
4445 attribute = to_slab_attr(attr);
4446 s = to_slab(kobj);
4447
4448 if (!attribute->store)
4449 return -EIO;
4450
4451 err = attribute->store(s, buf, len);
4452
4453 return err;
4454}
4455
Christoph Lameter151c6022008-01-07 22:29:05 -08004456static void kmem_cache_release(struct kobject *kobj)
4457{
4458 struct kmem_cache *s = to_slab(kobj);
4459
Pekka Enberg84c1cf62010-09-14 23:21:12 +03004460 kfree(s->name);
Christoph Lameter151c6022008-01-07 22:29:05 -08004461 kfree(s);
4462}
4463
Emese Revfy52cf25d2010-01-19 02:58:23 +01004464static const struct sysfs_ops slab_sysfs_ops = {
Christoph Lameter81819f02007-05-06 14:49:36 -07004465 .show = slab_attr_show,
4466 .store = slab_attr_store,
4467};
4468
4469static struct kobj_type slab_ktype = {
4470 .sysfs_ops = &slab_sysfs_ops,
Christoph Lameter151c6022008-01-07 22:29:05 -08004471 .release = kmem_cache_release
Christoph Lameter81819f02007-05-06 14:49:36 -07004472};
4473
4474static int uevent_filter(struct kset *kset, struct kobject *kobj)
4475{
4476 struct kobj_type *ktype = get_ktype(kobj);
4477
4478 if (ktype == &slab_ktype)
4479 return 1;
4480 return 0;
4481}
4482
Emese Revfy9cd43612009-12-31 14:52:51 +01004483static const struct kset_uevent_ops slab_uevent_ops = {
Christoph Lameter81819f02007-05-06 14:49:36 -07004484 .filter = uevent_filter,
4485};
4486
Greg Kroah-Hartman27c3a312007-11-01 09:29:06 -06004487static struct kset *slab_kset;
Christoph Lameter81819f02007-05-06 14:49:36 -07004488
4489#define ID_STR_LENGTH 64
4490
4491/* Create a unique string id for a slab cache:
Christoph Lameter6446faa2008-02-15 23:45:26 -08004492 *
4493 * Format :[flags-]size
Christoph Lameter81819f02007-05-06 14:49:36 -07004494 */
4495static char *create_unique_id(struct kmem_cache *s)
4496{
4497 char *name = kmalloc(ID_STR_LENGTH, GFP_KERNEL);
4498 char *p = name;
4499
4500 BUG_ON(!name);
4501
4502 *p++ = ':';
4503 /*
4504 * First flags affecting slabcache operations. We will only
4505 * get here for aliasable slabs so we do not need to support
4506 * too many flags. The flags here must cover all flags that
4507 * are matched during merging to guarantee that the id is
4508 * unique.
4509 */
4510 if (s->flags & SLAB_CACHE_DMA)
4511 *p++ = 'd';
4512 if (s->flags & SLAB_RECLAIM_ACCOUNT)
4513 *p++ = 'a';
4514 if (s->flags & SLAB_DEBUG_FREE)
4515 *p++ = 'F';
Vegard Nossum5a896d92008-04-04 00:54:48 +02004516 if (!(s->flags & SLAB_NOTRACK))
4517 *p++ = 't';
Christoph Lameter81819f02007-05-06 14:49:36 -07004518 if (p != name + 1)
4519 *p++ = '-';
4520 p += sprintf(p, "%07d", s->size);
4521 BUG_ON(p > name + ID_STR_LENGTH - 1);
4522 return name;
4523}
4524
4525static int sysfs_slab_add(struct kmem_cache *s)
4526{
4527 int err;
4528 const char *name;
4529 int unmergeable;
4530
4531 if (slab_state < SYSFS)
4532 /* Defer until later */
4533 return 0;
4534
4535 unmergeable = slab_unmergeable(s);
4536 if (unmergeable) {
4537 /*
4538 * Slabcache can never be merged so we can use the name proper.
4539 * This is typically the case for debug situations. In that
4540 * case we can catch duplicate names easily.
4541 */
Greg Kroah-Hartman27c3a312007-11-01 09:29:06 -06004542 sysfs_remove_link(&slab_kset->kobj, s->name);
Christoph Lameter81819f02007-05-06 14:49:36 -07004543 name = s->name;
4544 } else {
4545 /*
4546 * Create a unique name for the slab as a target
4547 * for the symlinks.
4548 */
4549 name = create_unique_id(s);
4550 }
4551
Greg Kroah-Hartman27c3a312007-11-01 09:29:06 -06004552 s->kobj.kset = slab_kset;
Greg Kroah-Hartman1eada112007-12-17 23:05:35 -07004553 err = kobject_init_and_add(&s->kobj, &slab_ktype, NULL, name);
4554 if (err) {
4555 kobject_put(&s->kobj);
Christoph Lameter81819f02007-05-06 14:49:36 -07004556 return err;
Greg Kroah-Hartman1eada112007-12-17 23:05:35 -07004557 }
Christoph Lameter81819f02007-05-06 14:49:36 -07004558
4559 err = sysfs_create_group(&s->kobj, &slab_attr_group);
Xiaotian Feng5788d8a2009-07-22 11:28:53 +08004560 if (err) {
4561 kobject_del(&s->kobj);
4562 kobject_put(&s->kobj);
Christoph Lameter81819f02007-05-06 14:49:36 -07004563 return err;
Xiaotian Feng5788d8a2009-07-22 11:28:53 +08004564 }
Christoph Lameter81819f02007-05-06 14:49:36 -07004565 kobject_uevent(&s->kobj, KOBJ_ADD);
4566 if (!unmergeable) {
4567 /* Setup first alias */
4568 sysfs_slab_alias(s, s->name);
4569 kfree(name);
4570 }
4571 return 0;
4572}
4573
4574static void sysfs_slab_remove(struct kmem_cache *s)
4575{
Christoph Lameter2bce6482010-07-19 11:39:11 -05004576 if (slab_state < SYSFS)
4577 /*
4578 * Sysfs has not been setup yet so no need to remove the
4579 * cache from sysfs.
4580 */
4581 return;
4582
Christoph Lameter81819f02007-05-06 14:49:36 -07004583 kobject_uevent(&s->kobj, KOBJ_REMOVE);
4584 kobject_del(&s->kobj);
Christoph Lameter151c6022008-01-07 22:29:05 -08004585 kobject_put(&s->kobj);
Christoph Lameter81819f02007-05-06 14:49:36 -07004586}
4587
4588/*
4589 * Need to buffer aliases during bootup until sysfs becomes
Nick Andrew9f6c708e2008-12-05 14:08:08 +11004590 * available lest we lose that information.
Christoph Lameter81819f02007-05-06 14:49:36 -07004591 */
4592struct saved_alias {
4593 struct kmem_cache *s;
4594 const char *name;
4595 struct saved_alias *next;
4596};
4597
Adrian Bunk5af328a2007-07-17 04:03:27 -07004598static struct saved_alias *alias_list;
Christoph Lameter81819f02007-05-06 14:49:36 -07004599
4600static int sysfs_slab_alias(struct kmem_cache *s, const char *name)
4601{
4602 struct saved_alias *al;
4603
4604 if (slab_state == SYSFS) {
4605 /*
4606 * If we have a leftover link then remove it.
4607 */
Greg Kroah-Hartman27c3a312007-11-01 09:29:06 -06004608 sysfs_remove_link(&slab_kset->kobj, name);
4609 return sysfs_create_link(&slab_kset->kobj, &s->kobj, name);
Christoph Lameter81819f02007-05-06 14:49:36 -07004610 }
4611
4612 al = kmalloc(sizeof(struct saved_alias), GFP_KERNEL);
4613 if (!al)
4614 return -ENOMEM;
4615
4616 al->s = s;
4617 al->name = name;
4618 al->next = alias_list;
4619 alias_list = al;
4620 return 0;
4621}
4622
4623static int __init slab_sysfs_init(void)
4624{
Christoph Lameter5b95a4a2007-07-17 04:03:19 -07004625 struct kmem_cache *s;
Christoph Lameter81819f02007-05-06 14:49:36 -07004626 int err;
4627
Christoph Lameter2bce6482010-07-19 11:39:11 -05004628 down_write(&slub_lock);
4629
Greg Kroah-Hartman0ff21e42007-11-06 10:36:58 -08004630 slab_kset = kset_create_and_add("slab", &slab_uevent_ops, kernel_kobj);
Greg Kroah-Hartman27c3a312007-11-01 09:29:06 -06004631 if (!slab_kset) {
Christoph Lameter2bce6482010-07-19 11:39:11 -05004632 up_write(&slub_lock);
Christoph Lameter81819f02007-05-06 14:49:36 -07004633 printk(KERN_ERR "Cannot register slab subsystem.\n");
4634 return -ENOSYS;
4635 }
4636
Christoph Lameter26a7bd02007-05-09 02:32:39 -07004637 slab_state = SYSFS;
4638
Christoph Lameter5b95a4a2007-07-17 04:03:19 -07004639 list_for_each_entry(s, &slab_caches, list) {
Christoph Lameter26a7bd02007-05-09 02:32:39 -07004640 err = sysfs_slab_add(s);
Christoph Lameter5d540fb2007-08-30 23:56:26 -07004641 if (err)
4642 printk(KERN_ERR "SLUB: Unable to add boot slab %s"
4643 " to sysfs\n", s->name);
Christoph Lameter26a7bd02007-05-09 02:32:39 -07004644 }
Christoph Lameter81819f02007-05-06 14:49:36 -07004645
4646 while (alias_list) {
4647 struct saved_alias *al = alias_list;
4648
4649 alias_list = alias_list->next;
4650 err = sysfs_slab_alias(al->s, al->name);
Christoph Lameter5d540fb2007-08-30 23:56:26 -07004651 if (err)
4652 printk(KERN_ERR "SLUB: Unable to add boot slab alias"
4653 " %s to sysfs\n", s->name);
Christoph Lameter81819f02007-05-06 14:49:36 -07004654 kfree(al);
4655 }
4656
Christoph Lameter2bce6482010-07-19 11:39:11 -05004657 up_write(&slub_lock);
Christoph Lameter81819f02007-05-06 14:49:36 -07004658 resiliency_test();
4659 return 0;
4660}
4661
4662__initcall(slab_sysfs_init);
Christoph Lameter81819f02007-05-06 14:49:36 -07004663#endif
Pekka J Enberg57ed3ed2008-01-01 17:23:28 +01004664
4665/*
4666 * The /proc/slabinfo ABI
4667 */
Linus Torvalds158a9622008-01-02 13:04:48 -08004668#ifdef CONFIG_SLABINFO
Pekka J Enberg57ed3ed2008-01-01 17:23:28 +01004669static void print_slabinfo_header(struct seq_file *m)
4670{
4671 seq_puts(m, "slabinfo - version: 2.1\n");
4672 seq_puts(m, "# name <active_objs> <num_objs> <objsize> "
4673 "<objperslab> <pagesperslab>");
4674 seq_puts(m, " : tunables <limit> <batchcount> <sharedfactor>");
4675 seq_puts(m, " : slabdata <active_slabs> <num_slabs> <sharedavail>");
4676 seq_putc(m, '\n');
4677}
4678
4679static void *s_start(struct seq_file *m, loff_t *pos)
4680{
4681 loff_t n = *pos;
4682
4683 down_read(&slub_lock);
4684 if (!n)
4685 print_slabinfo_header(m);
4686
4687 return seq_list_start(&slab_caches, *pos);
4688}
4689
4690static void *s_next(struct seq_file *m, void *p, loff_t *pos)
4691{
4692 return seq_list_next(p, &slab_caches, pos);
4693}
4694
4695static void s_stop(struct seq_file *m, void *p)
4696{
4697 up_read(&slub_lock);
4698}
4699
4700static int s_show(struct seq_file *m, void *p)
4701{
4702 unsigned long nr_partials = 0;
4703 unsigned long nr_slabs = 0;
4704 unsigned long nr_inuse = 0;
Christoph Lameter205ab992008-04-14 19:11:40 +03004705 unsigned long nr_objs = 0;
4706 unsigned long nr_free = 0;
Pekka J Enberg57ed3ed2008-01-01 17:23:28 +01004707 struct kmem_cache *s;
4708 int node;
4709
4710 s = list_entry(p, struct kmem_cache, list);
4711
4712 for_each_online_node(node) {
4713 struct kmem_cache_node *n = get_node(s, node);
4714
4715 if (!n)
4716 continue;
4717
4718 nr_partials += n->nr_partial;
4719 nr_slabs += atomic_long_read(&n->nr_slabs);
Christoph Lameter205ab992008-04-14 19:11:40 +03004720 nr_objs += atomic_long_read(&n->total_objects);
4721 nr_free += count_partial(n, count_free);
Pekka J Enberg57ed3ed2008-01-01 17:23:28 +01004722 }
4723
Christoph Lameter205ab992008-04-14 19:11:40 +03004724 nr_inuse = nr_objs - nr_free;
Pekka J Enberg57ed3ed2008-01-01 17:23:28 +01004725
4726 seq_printf(m, "%-17s %6lu %6lu %6u %4u %4d", s->name, nr_inuse,
Christoph Lameter834f3d12008-04-14 19:11:31 +03004727 nr_objs, s->size, oo_objects(s->oo),
4728 (1 << oo_order(s->oo)));
Pekka J Enberg57ed3ed2008-01-01 17:23:28 +01004729 seq_printf(m, " : tunables %4u %4u %4u", 0, 0, 0);
4730 seq_printf(m, " : slabdata %6lu %6lu %6lu", nr_slabs, nr_slabs,
4731 0UL);
4732 seq_putc(m, '\n');
4733 return 0;
4734}
4735
Alexey Dobriyan7b3c3a52008-10-06 02:42:17 +04004736static const struct seq_operations slabinfo_op = {
Pekka J Enberg57ed3ed2008-01-01 17:23:28 +01004737 .start = s_start,
4738 .next = s_next,
4739 .stop = s_stop,
4740 .show = s_show,
4741};
4742
Alexey Dobriyan7b3c3a52008-10-06 02:42:17 +04004743static int slabinfo_open(struct inode *inode, struct file *file)
4744{
4745 return seq_open(file, &slabinfo_op);
4746}
4747
4748static const struct file_operations proc_slabinfo_operations = {
4749 .open = slabinfo_open,
4750 .read = seq_read,
4751 .llseek = seq_lseek,
4752 .release = seq_release,
4753};
4754
4755static int __init slab_proc_init(void)
4756{
WANG Congcf5d1132009-08-18 19:11:40 +03004757 proc_create("slabinfo", S_IRUGO, NULL, &proc_slabinfo_operations);
Alexey Dobriyan7b3c3a52008-10-06 02:42:17 +04004758 return 0;
4759}
4760module_init(slab_proc_init);
Linus Torvalds158a9622008-01-02 13:04:48 -08004761#endif /* CONFIG_SLABINFO */