blob: 8d8a3a641f0b05cf1f2c2a7701b7027cc43350a8 [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 *
Christoph Lameter881db7f2011-06-01 12:25:53 -05005 * The allocator synchronizes using per slab locks or atomic operatios
6 * and only uses a centralized lock to manage a pool of partial slabs.
Christoph Lameter81819f02007-05-06 14:49:36 -07007 *
Christoph Lametercde53532008-07-04 09:59:22 -07008 * (C) 2007 SGI, Christoph Lameter
Christoph Lameter881db7f2011-06-01 12:25:53 -05009 * (C) 2011 Linux Foundation, Christoph Lameter
Christoph Lameter81819f02007-05-06 14:49:36 -070010 */
11
12#include <linux/mm.h>
Nick Piggin1eb5ac62009-05-05 19:13:44 +100013#include <linux/swap.h> /* struct reclaim_state */
Christoph Lameter81819f02007-05-06 14:49:36 -070014#include <linux/module.h>
15#include <linux/bit_spinlock.h>
16#include <linux/interrupt.h>
17#include <linux/bitops.h>
18#include <linux/slab.h>
Christoph Lameter97d06602012-07-06 15:25:11 -050019#include "slab.h"
Alexey Dobriyan7b3c3a52008-10-06 02:42:17 +040020#include <linux/proc_fs.h>
Andrew Morton3ac38fa2013-04-29 15:08:06 -070021#include <linux/notifier.h>
Christoph Lameter81819f02007-05-06 14:49:36 -070022#include <linux/seq_file.h>
Vegard Nossum5a896d92008-04-04 00:54:48 +020023#include <linux/kmemcheck.h>
Christoph Lameter81819f02007-05-06 14:49:36 -070024#include <linux/cpu.h>
25#include <linux/cpuset.h>
26#include <linux/mempolicy.h>
27#include <linux/ctype.h>
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -070028#include <linux/debugobjects.h>
Christoph Lameter81819f02007-05-06 14:49:36 -070029#include <linux/kallsyms.h>
Yasunori Gotob9049e22007-10-21 16:41:37 -070030#include <linux/memory.h>
Roman Zippelf8bd2252008-05-01 04:34:31 -070031#include <linux/math64.h>
Akinobu Mita773ff602008-12-23 19:37:01 +090032#include <linux/fault-inject.h>
Pekka Enbergbfa71452011-07-07 22:47:01 +030033#include <linux/stacktrace.h>
Christoph Lameter4de900b2012-01-30 15:53:51 -060034#include <linux/prefetch.h>
Glauber Costa2633d7a2012-12-18 14:22:34 -080035#include <linux/memcontrol.h>
Christoph Lameter81819f02007-05-06 14:49:36 -070036
Richard Kennedy4a923792010-10-21 10:29:19 +010037#include <trace/events/kmem.h>
38
Mel Gorman072bb0a2012-07-31 16:43:58 -070039#include "internal.h"
40
Christoph Lameter81819f02007-05-06 14:49:36 -070041/*
42 * Lock order:
Christoph Lameter18004c52012-07-06 15:25:12 -050043 * 1. slab_mutex (Global Mutex)
Christoph Lameter881db7f2011-06-01 12:25:53 -050044 * 2. node->list_lock
45 * 3. slab_lock(page) (Only on some arches and for debugging)
Christoph Lameter81819f02007-05-06 14:49:36 -070046 *
Christoph Lameter18004c52012-07-06 15:25:12 -050047 * slab_mutex
Christoph Lameter881db7f2011-06-01 12:25:53 -050048 *
Christoph Lameter18004c52012-07-06 15:25:12 -050049 * The role of the slab_mutex is to protect the list of all the slabs
Christoph Lameter881db7f2011-06-01 12:25:53 -050050 * and to synchronize major metadata changes to slab cache structures.
51 *
52 * The slab_lock is only used for debugging and on arches that do not
53 * have the ability to do a cmpxchg_double. It only protects the second
54 * double word in the page struct. Meaning
55 * A. page->freelist -> List of object free in a page
56 * B. page->counters -> Counters of objects
57 * C. page->frozen -> frozen state
58 *
59 * If a slab is frozen then it is exempt from list management. It is not
60 * on any list. The processor that froze the slab is the one who can
61 * perform list operations on the page. Other processors may put objects
62 * onto the freelist but the processor that froze the slab is the only
63 * one that can retrieve the objects from the page's freelist.
Christoph Lameter81819f02007-05-06 14:49:36 -070064 *
65 * The list_lock protects the partial and full list on each node and
66 * the partial slab counter. If taken then no new slabs may be added or
67 * removed from the lists nor make the number of partial slabs be modified.
68 * (Note that the total number of slabs is an atomic value that may be
69 * modified without taking the list lock).
70 *
71 * The list_lock is a centralized lock and thus we avoid taking it as
72 * much as possible. As long as SLUB does not have to handle partial
73 * slabs, operations can continue without any centralized lock. F.e.
74 * allocating a long series of objects that fill up slabs does not require
75 * the list lock.
Christoph Lameter81819f02007-05-06 14:49:36 -070076 * Interrupts are disabled during allocation and deallocation in order to
77 * make the slab allocator safe to use in the context of an irq. In addition
78 * interrupts are disabled to ensure that the processor does not change
79 * while handling per_cpu slabs, due to kernel preemption.
80 *
81 * SLUB assigns one slab for allocation to each processor.
82 * Allocations only occur from these slabs called cpu slabs.
83 *
Christoph Lameter672bba32007-05-09 02:32:39 -070084 * Slabs with free elements are kept on a partial list and during regular
85 * operations no list for full slabs is used. If an object in a full slab is
Christoph Lameter81819f02007-05-06 14:49:36 -070086 * freed then the slab will show up again on the partial lists.
Christoph Lameter672bba32007-05-09 02:32:39 -070087 * We track full slabs for debugging purposes though because otherwise we
88 * cannot scan all objects.
Christoph Lameter81819f02007-05-06 14:49:36 -070089 *
90 * Slabs are freed when they become empty. Teardown and setup is
91 * minimal so we rely on the page allocators per cpu caches for
92 * fast frees and allocs.
93 *
94 * Overloading of page flags that are otherwise used for LRU management.
95 *
Christoph Lameter4b6f0752007-05-16 22:10:53 -070096 * PageActive The slab is frozen and exempt from list processing.
97 * This means that the slab is dedicated to a purpose
98 * such as satisfying allocations for a specific
99 * processor. Objects may be freed in the slab while
100 * it is frozen but slab_free will then skip the usual
101 * list operations. It is up to the processor holding
102 * the slab to integrate the slab into the slab lists
103 * when the slab is no longer needed.
104 *
105 * One use of this flag is to mark slabs that are
106 * used for allocations. Then such a slab becomes a cpu
107 * slab. The cpu slab may be equipped with an additional
Christoph Lameterdfb4f092007-10-16 01:26:05 -0700108 * freelist that allows lockless access to
Christoph Lameter894b8782007-05-10 03:15:16 -0700109 * free objects in addition to the regular freelist
110 * that requires the slab lock.
Christoph Lameter81819f02007-05-06 14:49:36 -0700111 *
112 * PageError Slab requires special handling due to debug
113 * options set. This moves slab handling out of
Christoph Lameter894b8782007-05-10 03:15:16 -0700114 * the fast path and disables lockless freelists.
Christoph Lameter81819f02007-05-06 14:49:36 -0700115 */
116
Christoph Lameteraf537b02010-07-09 14:07:14 -0500117static inline int kmem_cache_debug(struct kmem_cache *s)
118{
Christoph Lameter5577bd82007-05-16 22:10:56 -0700119#ifdef CONFIG_SLUB_DEBUG
Christoph Lameteraf537b02010-07-09 14:07:14 -0500120 return unlikely(s->flags & SLAB_DEBUG_FLAGS);
Christoph Lameter5577bd82007-05-16 22:10:56 -0700121#else
Christoph Lameteraf537b02010-07-09 14:07:14 -0500122 return 0;
Christoph Lameter5577bd82007-05-16 22:10:56 -0700123#endif
Christoph Lameteraf537b02010-07-09 14:07:14 -0500124}
Christoph Lameter5577bd82007-05-16 22:10:56 -0700125
Christoph Lameter81819f02007-05-06 14:49:36 -0700126/*
127 * Issues still to be resolved:
128 *
Christoph Lameter81819f02007-05-06 14:49:36 -0700129 * - Support PAGE_ALLOC_DEBUG. Should be easy to do.
130 *
Christoph Lameter81819f02007-05-06 14:49:36 -0700131 * - Variable sizing of the per node arrays
132 */
133
134/* Enable to test recovery from slab corruption on boot */
135#undef SLUB_RESILIENCY_TEST
136
Christoph Lameterb789ef52011-06-01 12:25:49 -0500137/* Enable to log cmpxchg failures */
138#undef SLUB_DEBUG_CMPXCHG
139
Christoph Lameter81819f02007-05-06 14:49:36 -0700140/*
Christoph Lameter2086d262007-05-06 14:49:46 -0700141 * Mininum number of partial slabs. These will be left on the partial
142 * lists even if they are empty. kmem_cache_shrink may reclaim them.
143 */
Christoph Lameter76be8952007-12-21 14:37:37 -0800144#define MIN_PARTIAL 5
Christoph Lametere95eed52007-05-06 14:49:44 -0700145
Christoph Lameter2086d262007-05-06 14:49:46 -0700146/*
147 * Maximum number of desirable partial slabs.
148 * The existence of more partial slabs makes kmem_cache_shrink
149 * sort the partial list by the number of objects in the.
150 */
151#define MAX_PARTIAL 10
152
Christoph Lameter81819f02007-05-06 14:49:36 -0700153#define DEBUG_DEFAULT_FLAGS (SLAB_DEBUG_FREE | SLAB_RED_ZONE | \
154 SLAB_POISON | SLAB_STORE_USER)
Christoph Lameter672bba32007-05-09 02:32:39 -0700155
Christoph Lameter81819f02007-05-06 14:49:36 -0700156/*
David Rientjes3de472132009-07-27 18:30:35 -0700157 * Debugging flags that require metadata to be stored in the slab. These get
158 * disabled when slub_debug=O is used and a cache's min order increases with
159 * metadata.
David Rientjesfa5ec8a2009-07-07 00:14:14 -0700160 */
David Rientjes3de472132009-07-27 18:30:35 -0700161#define DEBUG_METADATA_FLAGS (SLAB_RED_ZONE | SLAB_POISON | SLAB_STORE_USER)
David Rientjesfa5ec8a2009-07-07 00:14:14 -0700162
163/*
Christoph Lameter81819f02007-05-06 14:49:36 -0700164 * Set of flags that will prevent slab merging
165 */
166#define SLUB_NEVER_MERGE (SLAB_RED_ZONE | SLAB_POISON | SLAB_STORE_USER | \
Dmitry Monakhov4c13dd32010-02-26 09:36:12 +0300167 SLAB_TRACE | SLAB_DESTROY_BY_RCU | SLAB_NOLEAKTRACE | \
168 SLAB_FAILSLAB)
Christoph Lameter81819f02007-05-06 14:49:36 -0700169
170#define SLUB_MERGE_SAME (SLAB_DEBUG_FREE | SLAB_RECLAIM_ACCOUNT | \
Vegard Nossum5a896d92008-04-04 00:54:48 +0200171 SLAB_CACHE_DMA | SLAB_NOTRACK)
Christoph Lameter81819f02007-05-06 14:49:36 -0700172
Cyrill Gorcunov210b5c02008-10-22 23:00:38 +0400173#define OO_SHIFT 16
174#define OO_MASK ((1 << OO_SHIFT) - 1)
Christoph Lameter50d5c412011-06-01 12:25:45 -0500175#define MAX_OBJS_PER_PAGE 32767 /* since page.objects is u15 */
Cyrill Gorcunov210b5c02008-10-22 23:00:38 +0400176
Christoph Lameter81819f02007-05-06 14:49:36 -0700177/* Internal SLUB flags */
Christoph Lameterf90ec392010-07-09 14:07:11 -0500178#define __OBJECT_POISON 0x80000000UL /* Poison object */
Christoph Lameterb789ef52011-06-01 12:25:49 -0500179#define __CMPXCHG_DOUBLE 0x40000000UL /* Use cmpxchg_double */
Christoph Lameter81819f02007-05-06 14:49:36 -0700180
Christoph Lameter81819f02007-05-06 14:49:36 -0700181#ifdef CONFIG_SMP
182static struct notifier_block slab_notifier;
183#endif
184
Christoph Lameter02cbc872007-05-09 02:32:43 -0700185/*
186 * Tracking user of a slab.
187 */
Ben Greeard6543e32011-07-07 11:36:36 -0700188#define TRACK_ADDRS_COUNT 16
Christoph Lameter02cbc872007-05-09 02:32:43 -0700189struct track {
Eduard - Gabriel Munteanuce71e272008-08-19 20:43:25 +0300190 unsigned long addr; /* Called from address */
Ben Greeard6543e32011-07-07 11:36:36 -0700191#ifdef CONFIG_STACKTRACE
192 unsigned long addrs[TRACK_ADDRS_COUNT]; /* Called from address */
193#endif
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 Lameterab4d5ed2010-10-05 13:57:26 -0500201#ifdef CONFIG_SYSFS
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 *);
Glauber Costa107dab52012-12-18 14:23:05 -0800205static void memcg_propagate_slab_attrs(struct kmem_cache *s);
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 Lameterdb265ec2012-09-04 23:18:33 +0000210static inline void sysfs_slab_remove(struct kmem_cache *s) { }
Christoph Lameter8ff12cf2008-02-07 17:47:41 -0800211
Glauber Costa107dab52012-12-18 14:23:05 -0800212static inline void memcg_propagate_slab_attrs(struct kmem_cache *s) { }
Christoph Lameter81819f02007-05-06 14:49:36 -0700213#endif
214
Christoph Lameter4fdccdf2011-03-22 13:35:00 -0500215static inline void stat(const struct kmem_cache *s, enum stat_item si)
Christoph Lameter8ff12cf2008-02-07 17:47:41 -0800216{
217#ifdef CONFIG_SLUB_STATS
Christoph Lameter84e554e62009-12-18 16:26:23 -0600218 __this_cpu_inc(s->cpu_slab->stat[si]);
Christoph Lameter8ff12cf2008-02-07 17:47:41 -0800219#endif
220}
221
Christoph Lameter81819f02007-05-06 14:49:36 -0700222/********************************************************************
223 * Core slab cache functions
224 *******************************************************************/
225
Christoph Lameter81819f02007-05-06 14:49:36 -0700226static inline struct kmem_cache_node *get_node(struct kmem_cache *s, int node)
227{
Christoph Lameter81819f02007-05-06 14:49:36 -0700228 return s->node[node];
Christoph Lameter81819f02007-05-06 14:49:36 -0700229}
230
Christoph Lameter6446faa2008-02-15 23:45:26 -0800231/* Verify that a pointer has an address that is valid within a slab page */
Christoph Lameter02cbc872007-05-09 02:32:43 -0700232static inline int check_valid_pointer(struct kmem_cache *s,
233 struct page *page, const void *object)
234{
235 void *base;
236
Christoph Lametera973e9d2008-03-01 13:40:44 -0800237 if (!object)
Christoph Lameter02cbc872007-05-09 02:32:43 -0700238 return 1;
239
Christoph Lametera973e9d2008-03-01 13:40:44 -0800240 base = page_address(page);
Christoph Lameter39b26462008-04-14 19:11:30 +0300241 if (object < base || object >= base + page->objects * s->size ||
Christoph Lameter02cbc872007-05-09 02:32:43 -0700242 (object - base) % s->size) {
243 return 0;
244 }
245
246 return 1;
247}
248
Christoph Lameter7656c722007-05-09 02:32:40 -0700249static inline void *get_freepointer(struct kmem_cache *s, void *object)
250{
251 return *(void **)(object + s->offset);
252}
253
Eric Dumazet0ad95002011-12-16 16:25:34 +0100254static void prefetch_freepointer(const struct kmem_cache *s, void *object)
255{
256 prefetch(object + s->offset);
257}
258
Christoph Lameter1393d9a2011-05-16 15:26:08 -0500259static inline void *get_freepointer_safe(struct kmem_cache *s, void *object)
260{
261 void *p;
262
263#ifdef CONFIG_DEBUG_PAGEALLOC
264 probe_kernel_read(&p, (void **)(object + s->offset), sizeof(p));
265#else
266 p = get_freepointer(s, object);
267#endif
268 return p;
269}
270
Christoph Lameter7656c722007-05-09 02:32:40 -0700271static inline void set_freepointer(struct kmem_cache *s, void *object, void *fp)
272{
273 *(void **)(object + s->offset) = fp;
274}
275
276/* Loop over all objects in a slab */
Christoph Lameter224a88b2008-04-14 19:11:31 +0300277#define for_each_object(__p, __s, __addr, __objects) \
278 for (__p = (__addr); __p < (__addr) + (__objects) * (__s)->size;\
Christoph Lameter7656c722007-05-09 02:32:40 -0700279 __p += (__s)->size)
280
Christoph Lameter7656c722007-05-09 02:32:40 -0700281/* Determine object index from a given position */
282static inline int slab_index(void *p, struct kmem_cache *s, void *addr)
283{
284 return (p - addr) / s->size;
285}
286
Mariusz Kozlowskid71f6062011-02-26 20:10:26 +0100287static inline size_t slab_ksize(const struct kmem_cache *s)
288{
289#ifdef CONFIG_SLUB_DEBUG
290 /*
291 * Debugging requires use of the padding between object
292 * and whatever may come after it.
293 */
294 if (s->flags & (SLAB_RED_ZONE | SLAB_POISON))
Christoph Lameter3b0efdf2012-06-13 10:24:57 -0500295 return s->object_size;
Mariusz Kozlowskid71f6062011-02-26 20:10:26 +0100296
297#endif
298 /*
299 * If we have the need to store the freelist pointer
300 * back there or track user information then we can
301 * only use the space before that information.
302 */
303 if (s->flags & (SLAB_DESTROY_BY_RCU | SLAB_STORE_USER))
304 return s->inuse;
305 /*
306 * Else we can use all the padding etc for the allocation
307 */
308 return s->size;
309}
310
Lai Jiangshanab9a0f12011-03-10 15:21:48 +0800311static inline int order_objects(int order, unsigned long size, int reserved)
312{
313 return ((PAGE_SIZE << order) - reserved) / size;
314}
315
Christoph Lameter834f3d12008-04-14 19:11:31 +0300316static inline struct kmem_cache_order_objects oo_make(int order,
Lai Jiangshanab9a0f12011-03-10 15:21:48 +0800317 unsigned long size, int reserved)
Christoph Lameter834f3d12008-04-14 19:11:31 +0300318{
319 struct kmem_cache_order_objects x = {
Lai Jiangshanab9a0f12011-03-10 15:21:48 +0800320 (order << OO_SHIFT) + order_objects(order, size, reserved)
Christoph Lameter834f3d12008-04-14 19:11:31 +0300321 };
322
323 return x;
324}
325
326static inline int oo_order(struct kmem_cache_order_objects x)
327{
Cyrill Gorcunov210b5c02008-10-22 23:00:38 +0400328 return x.x >> OO_SHIFT;
Christoph Lameter834f3d12008-04-14 19:11:31 +0300329}
330
331static inline int oo_objects(struct kmem_cache_order_objects x)
332{
Cyrill Gorcunov210b5c02008-10-22 23:00:38 +0400333 return x.x & OO_MASK;
Christoph Lameter834f3d12008-04-14 19:11:31 +0300334}
335
Christoph Lameter881db7f2011-06-01 12:25:53 -0500336/*
337 * Per slab locking using the pagelock
338 */
339static __always_inline void slab_lock(struct page *page)
340{
341 bit_spin_lock(PG_locked, &page->flags);
342}
343
344static __always_inline void slab_unlock(struct page *page)
345{
346 __bit_spin_unlock(PG_locked, &page->flags);
347}
348
Christoph Lameter1d071712011-07-14 12:49:12 -0500349/* Interrupts must be disabled (for the fallback code to work right) */
350static inline bool __cmpxchg_double_slab(struct kmem_cache *s, struct page *page,
Christoph Lameterb789ef52011-06-01 12:25:49 -0500351 void *freelist_old, unsigned long counters_old,
352 void *freelist_new, unsigned long counters_new,
353 const char *n)
354{
Christoph Lameter1d071712011-07-14 12:49:12 -0500355 VM_BUG_ON(!irqs_disabled());
Heiko Carstens25654092012-01-12 17:17:33 -0800356#if defined(CONFIG_HAVE_CMPXCHG_DOUBLE) && \
357 defined(CONFIG_HAVE_ALIGNED_STRUCT_PAGE)
Christoph Lameterb789ef52011-06-01 12:25:49 -0500358 if (s->flags & __CMPXCHG_DOUBLE) {
Jan Beulichcdcd6292012-01-02 17:02:18 +0000359 if (cmpxchg_double(&page->freelist, &page->counters,
Christoph Lameterb789ef52011-06-01 12:25:49 -0500360 freelist_old, counters_old,
361 freelist_new, counters_new))
362 return 1;
363 } else
364#endif
365 {
Christoph Lameter881db7f2011-06-01 12:25:53 -0500366 slab_lock(page);
Christoph Lameterb789ef52011-06-01 12:25:49 -0500367 if (page->freelist == freelist_old && page->counters == counters_old) {
368 page->freelist = freelist_new;
369 page->counters = counters_new;
Christoph Lameter881db7f2011-06-01 12:25:53 -0500370 slab_unlock(page);
Christoph Lameterb789ef52011-06-01 12:25:49 -0500371 return 1;
372 }
Christoph Lameter881db7f2011-06-01 12:25:53 -0500373 slab_unlock(page);
Christoph Lameterb789ef52011-06-01 12:25:49 -0500374 }
375
376 cpu_relax();
377 stat(s, CMPXCHG_DOUBLE_FAIL);
378
379#ifdef SLUB_DEBUG_CMPXCHG
380 printk(KERN_INFO "%s %s: cmpxchg double redo ", n, s->name);
381#endif
382
383 return 0;
384}
385
Christoph Lameter1d071712011-07-14 12:49:12 -0500386static inline bool cmpxchg_double_slab(struct kmem_cache *s, struct page *page,
387 void *freelist_old, unsigned long counters_old,
388 void *freelist_new, unsigned long counters_new,
389 const char *n)
390{
Heiko Carstens25654092012-01-12 17:17:33 -0800391#if defined(CONFIG_HAVE_CMPXCHG_DOUBLE) && \
392 defined(CONFIG_HAVE_ALIGNED_STRUCT_PAGE)
Christoph Lameter1d071712011-07-14 12:49:12 -0500393 if (s->flags & __CMPXCHG_DOUBLE) {
Jan Beulichcdcd6292012-01-02 17:02:18 +0000394 if (cmpxchg_double(&page->freelist, &page->counters,
Christoph Lameter1d071712011-07-14 12:49:12 -0500395 freelist_old, counters_old,
396 freelist_new, counters_new))
397 return 1;
398 } else
399#endif
400 {
401 unsigned long flags;
402
403 local_irq_save(flags);
404 slab_lock(page);
405 if (page->freelist == freelist_old && page->counters == counters_old) {
406 page->freelist = freelist_new;
407 page->counters = counters_new;
408 slab_unlock(page);
409 local_irq_restore(flags);
410 return 1;
411 }
412 slab_unlock(page);
413 local_irq_restore(flags);
414 }
415
416 cpu_relax();
417 stat(s, CMPXCHG_DOUBLE_FAIL);
418
419#ifdef SLUB_DEBUG_CMPXCHG
420 printk(KERN_INFO "%s %s: cmpxchg double redo ", n, s->name);
421#endif
422
423 return 0;
424}
425
Christoph Lameter41ecc552007-05-09 02:32:44 -0700426#ifdef CONFIG_SLUB_DEBUG
427/*
Christoph Lameter5f80b132011-04-15 14:48:13 -0500428 * Determine a map of object in use on a page.
429 *
Christoph Lameter881db7f2011-06-01 12:25:53 -0500430 * Node listlock must be held to guarantee that the page does
Christoph Lameter5f80b132011-04-15 14:48:13 -0500431 * not vanish from under us.
432 */
433static void get_map(struct kmem_cache *s, struct page *page, unsigned long *map)
434{
435 void *p;
436 void *addr = page_address(page);
437
438 for (p = page->freelist; p; p = get_freepointer(s, p))
439 set_bit(slab_index(p, s, addr), map);
440}
441
Christoph Lameter41ecc552007-05-09 02:32:44 -0700442/*
443 * Debug settings:
444 */
Christoph Lameterf0630ff2007-07-15 23:38:14 -0700445#ifdef CONFIG_SLUB_DEBUG_ON
446static int slub_debug = DEBUG_DEFAULT_FLAGS;
447#else
Christoph Lameter41ecc552007-05-09 02:32:44 -0700448static int slub_debug;
Christoph Lameterf0630ff2007-07-15 23:38:14 -0700449#endif
Christoph Lameter41ecc552007-05-09 02:32:44 -0700450
451static char *slub_debug_slabs;
David Rientjesfa5ec8a2009-07-07 00:14:14 -0700452static int disable_higher_order_debug;
Christoph Lameter41ecc552007-05-09 02:32:44 -0700453
Christoph Lameter7656c722007-05-09 02:32:40 -0700454/*
Christoph Lameter81819f02007-05-06 14:49:36 -0700455 * Object debugging
456 */
457static void print_section(char *text, u8 *addr, unsigned int length)
458{
Sebastian Andrzej Siewiorffc79d22011-07-29 14:10:20 +0200459 print_hex_dump(KERN_ERR, text, DUMP_PREFIX_ADDRESS, 16, 1, addr,
460 length, 1);
Christoph Lameter81819f02007-05-06 14:49:36 -0700461}
462
Christoph Lameter81819f02007-05-06 14:49:36 -0700463static struct track *get_track(struct kmem_cache *s, void *object,
464 enum track_item alloc)
465{
466 struct track *p;
467
468 if (s->offset)
469 p = object + s->offset + sizeof(void *);
470 else
471 p = object + s->inuse;
472
473 return p + alloc;
474}
475
476static void set_track(struct kmem_cache *s, void *object,
Eduard - Gabriel Munteanuce71e272008-08-19 20:43:25 +0300477 enum track_item alloc, unsigned long addr)
Christoph Lameter81819f02007-05-06 14:49:36 -0700478{
Akinobu Mita1a00df42009-03-07 00:36:21 +0900479 struct track *p = get_track(s, object, alloc);
Christoph Lameter81819f02007-05-06 14:49:36 -0700480
Christoph Lameter81819f02007-05-06 14:49:36 -0700481 if (addr) {
Ben Greeard6543e32011-07-07 11:36:36 -0700482#ifdef CONFIG_STACKTRACE
483 struct stack_trace trace;
484 int i;
485
486 trace.nr_entries = 0;
487 trace.max_entries = TRACK_ADDRS_COUNT;
488 trace.entries = p->addrs;
489 trace.skip = 3;
490 save_stack_trace(&trace);
491
492 /* See rant in lockdep.c */
493 if (trace.nr_entries != 0 &&
494 trace.entries[trace.nr_entries - 1] == ULONG_MAX)
495 trace.nr_entries--;
496
497 for (i = trace.nr_entries; i < TRACK_ADDRS_COUNT; i++)
498 p->addrs[i] = 0;
499#endif
Christoph Lameter81819f02007-05-06 14:49:36 -0700500 p->addr = addr;
501 p->cpu = smp_processor_id();
Alexey Dobriyan88e4ccf2008-06-23 02:58:37 +0400502 p->pid = current->pid;
Christoph Lameter81819f02007-05-06 14:49:36 -0700503 p->when = jiffies;
504 } else
505 memset(p, 0, sizeof(struct track));
506}
507
Christoph Lameter81819f02007-05-06 14:49:36 -0700508static void init_tracking(struct kmem_cache *s, void *object)
509{
Christoph Lameter24922682007-07-17 04:03:18 -0700510 if (!(s->flags & SLAB_STORE_USER))
511 return;
512
Eduard - Gabriel Munteanuce71e272008-08-19 20:43:25 +0300513 set_track(s, object, TRACK_FREE, 0UL);
514 set_track(s, object, TRACK_ALLOC, 0UL);
Christoph Lameter81819f02007-05-06 14:49:36 -0700515}
516
517static void print_track(const char *s, struct track *t)
518{
519 if (!t->addr)
520 return;
521
Linus Torvalds7daf7052008-07-14 12:12:53 -0700522 printk(KERN_ERR "INFO: %s in %pS age=%lu cpu=%u pid=%d\n",
Eduard - Gabriel Munteanuce71e272008-08-19 20:43:25 +0300523 s, (void *)t->addr, jiffies - t->when, t->cpu, t->pid);
Ben Greeard6543e32011-07-07 11:36:36 -0700524#ifdef CONFIG_STACKTRACE
525 {
526 int i;
527 for (i = 0; i < TRACK_ADDRS_COUNT; i++)
528 if (t->addrs[i])
529 printk(KERN_ERR "\t%pS\n", (void *)t->addrs[i]);
530 else
531 break;
532 }
533#endif
Christoph Lameter81819f02007-05-06 14:49:36 -0700534}
535
Christoph Lameter24922682007-07-17 04:03:18 -0700536static void print_tracking(struct kmem_cache *s, void *object)
537{
538 if (!(s->flags & SLAB_STORE_USER))
539 return;
540
541 print_track("Allocated", get_track(s, object, TRACK_ALLOC));
542 print_track("Freed", get_track(s, object, TRACK_FREE));
543}
544
545static void print_page_info(struct page *page)
546{
Christoph Lameter39b26462008-04-14 19:11:30 +0300547 printk(KERN_ERR "INFO: Slab 0x%p objects=%u used=%u fp=0x%p flags=0x%04lx\n",
548 page, page->objects, page->inuse, page->freelist, page->flags);
Christoph Lameter24922682007-07-17 04:03:18 -0700549
550}
551
552static void slab_bug(struct kmem_cache *s, char *fmt, ...)
553{
554 va_list args;
555 char buf[100];
556
557 va_start(args, fmt);
558 vsnprintf(buf, sizeof(buf), fmt, args);
559 va_end(args);
560 printk(KERN_ERR "========================================"
561 "=====================================\n");
Dave Jones265d47e2011-11-15 15:04:00 -0800562 printk(KERN_ERR "BUG %s (%s): %s\n", s->name, print_tainted(), buf);
Christoph Lameter24922682007-07-17 04:03:18 -0700563 printk(KERN_ERR "----------------------------------------"
564 "-------------------------------------\n\n");
Dave Jones645df232012-09-18 15:54:12 -0400565
Rusty Russell373d4d02013-01-21 17:17:39 +1030566 add_taint(TAINT_BAD_PAGE, LOCKDEP_NOW_UNRELIABLE);
Christoph Lameter24922682007-07-17 04:03:18 -0700567}
568
569static void slab_fix(struct kmem_cache *s, char *fmt, ...)
570{
571 va_list args;
572 char buf[100];
573
574 va_start(args, fmt);
575 vsnprintf(buf, sizeof(buf), fmt, args);
576 va_end(args);
577 printk(KERN_ERR "FIX %s: %s\n", s->name, buf);
578}
579
580static void print_trailer(struct kmem_cache *s, struct page *page, u8 *p)
Christoph Lameter81819f02007-05-06 14:49:36 -0700581{
582 unsigned int off; /* Offset of last byte */
Christoph Lametera973e9d2008-03-01 13:40:44 -0800583 u8 *addr = page_address(page);
Christoph Lameter24922682007-07-17 04:03:18 -0700584
585 print_tracking(s, p);
586
587 print_page_info(page);
588
589 printk(KERN_ERR "INFO: Object 0x%p @offset=%tu fp=0x%p\n\n",
590 p, p - addr, get_freepointer(s, p));
591
592 if (p > addr + 16)
Sebastian Andrzej Siewiorffc79d22011-07-29 14:10:20 +0200593 print_section("Bytes b4 ", p - 16, 16);
Christoph Lameter24922682007-07-17 04:03:18 -0700594
Christoph Lameter3b0efdf2012-06-13 10:24:57 -0500595 print_section("Object ", p, min_t(unsigned long, s->object_size,
Sebastian Andrzej Siewiorffc79d22011-07-29 14:10:20 +0200596 PAGE_SIZE));
Christoph Lameter81819f02007-05-06 14:49:36 -0700597 if (s->flags & SLAB_RED_ZONE)
Christoph Lameter3b0efdf2012-06-13 10:24:57 -0500598 print_section("Redzone ", p + s->object_size,
599 s->inuse - s->object_size);
Christoph Lameter81819f02007-05-06 14:49:36 -0700600
Christoph Lameter81819f02007-05-06 14:49:36 -0700601 if (s->offset)
602 off = s->offset + sizeof(void *);
603 else
604 off = s->inuse;
605
Christoph Lameter24922682007-07-17 04:03:18 -0700606 if (s->flags & SLAB_STORE_USER)
Christoph Lameter81819f02007-05-06 14:49:36 -0700607 off += 2 * sizeof(struct track);
Christoph Lameter81819f02007-05-06 14:49:36 -0700608
609 if (off != s->size)
610 /* Beginning of the filler is the free pointer */
Sebastian Andrzej Siewiorffc79d22011-07-29 14:10:20 +0200611 print_section("Padding ", p + off, s->size - off);
Christoph Lameter24922682007-07-17 04:03:18 -0700612
613 dump_stack();
Christoph Lameter81819f02007-05-06 14:49:36 -0700614}
615
616static void object_err(struct kmem_cache *s, struct page *page,
617 u8 *object, char *reason)
618{
Christoph Lameter3dc50632008-04-23 12:28:01 -0700619 slab_bug(s, "%s", reason);
Christoph Lameter24922682007-07-17 04:03:18 -0700620 print_trailer(s, page, object);
Christoph Lameter81819f02007-05-06 14:49:36 -0700621}
622
Christoph Lameter945cf2b2012-09-04 23:18:33 +0000623static void slab_err(struct kmem_cache *s, struct page *page, const char *fmt, ...)
Christoph Lameter81819f02007-05-06 14:49:36 -0700624{
625 va_list args;
626 char buf[100];
627
Christoph Lameter24922682007-07-17 04:03:18 -0700628 va_start(args, fmt);
629 vsnprintf(buf, sizeof(buf), fmt, args);
Christoph Lameter81819f02007-05-06 14:49:36 -0700630 va_end(args);
Christoph Lameter3dc50632008-04-23 12:28:01 -0700631 slab_bug(s, "%s", buf);
Christoph Lameter24922682007-07-17 04:03:18 -0700632 print_page_info(page);
Christoph Lameter81819f02007-05-06 14:49:36 -0700633 dump_stack();
634}
635
Christoph Lameterf7cb1932010-09-29 07:15:01 -0500636static void init_object(struct kmem_cache *s, void *object, u8 val)
Christoph Lameter81819f02007-05-06 14:49:36 -0700637{
638 u8 *p = object;
639
640 if (s->flags & __OBJECT_POISON) {
Christoph Lameter3b0efdf2012-06-13 10:24:57 -0500641 memset(p, POISON_FREE, s->object_size - 1);
642 p[s->object_size - 1] = POISON_END;
Christoph Lameter81819f02007-05-06 14:49:36 -0700643 }
644
645 if (s->flags & SLAB_RED_ZONE)
Christoph Lameter3b0efdf2012-06-13 10:24:57 -0500646 memset(p + s->object_size, val, s->inuse - s->object_size);
Christoph Lameter81819f02007-05-06 14:49:36 -0700647}
648
Christoph Lameter24922682007-07-17 04:03:18 -0700649static void restore_bytes(struct kmem_cache *s, char *message, u8 data,
650 void *from, void *to)
651{
652 slab_fix(s, "Restoring 0x%p-0x%p=0x%x\n", from, to - 1, data);
653 memset(from, data, to - from);
654}
655
656static int check_bytes_and_report(struct kmem_cache *s, struct page *page,
657 u8 *object, char *what,
Pekka Enberg06428782008-01-07 23:20:27 -0800658 u8 *start, unsigned int value, unsigned int bytes)
Christoph Lameter24922682007-07-17 04:03:18 -0700659{
660 u8 *fault;
661 u8 *end;
662
Akinobu Mita798248202011-10-31 17:08:07 -0700663 fault = memchr_inv(start, value, bytes);
Christoph Lameter24922682007-07-17 04:03:18 -0700664 if (!fault)
665 return 1;
666
667 end = start + bytes;
668 while (end > fault && end[-1] == value)
669 end--;
670
671 slab_bug(s, "%s overwritten", what);
672 printk(KERN_ERR "INFO: 0x%p-0x%p. First byte 0x%x instead of 0x%x\n",
673 fault, end - 1, fault[0], value);
674 print_trailer(s, page, object);
675
676 restore_bytes(s, what, value, fault, end);
677 return 0;
Christoph Lameter81819f02007-05-06 14:49:36 -0700678}
679
Christoph Lameter81819f02007-05-06 14:49:36 -0700680/*
681 * Object layout:
682 *
683 * object address
684 * Bytes of the object to be managed.
685 * If the freepointer may overlay the object then the free
686 * pointer is the first word of the object.
Christoph Lameter672bba32007-05-09 02:32:39 -0700687 *
Christoph Lameter81819f02007-05-06 14:49:36 -0700688 * Poisoning uses 0x6b (POISON_FREE) and the last byte is
689 * 0xa5 (POISON_END)
690 *
Christoph Lameter3b0efdf2012-06-13 10:24:57 -0500691 * object + s->object_size
Christoph Lameter81819f02007-05-06 14:49:36 -0700692 * Padding to reach word boundary. This is also used for Redzoning.
Christoph Lameter672bba32007-05-09 02:32:39 -0700693 * Padding is extended by another word if Redzoning is enabled and
Christoph Lameter3b0efdf2012-06-13 10:24:57 -0500694 * object_size == inuse.
Christoph Lameter672bba32007-05-09 02:32:39 -0700695 *
Christoph Lameter81819f02007-05-06 14:49:36 -0700696 * We fill with 0xbb (RED_INACTIVE) for inactive objects and with
697 * 0xcc (RED_ACTIVE) for objects in use.
698 *
699 * object + s->inuse
Christoph Lameter672bba32007-05-09 02:32:39 -0700700 * Meta data starts here.
701 *
Christoph Lameter81819f02007-05-06 14:49:36 -0700702 * A. Free pointer (if we cannot overwrite object on free)
703 * B. Tracking data for SLAB_STORE_USER
Christoph Lameter672bba32007-05-09 02:32:39 -0700704 * C. Padding to reach required alignment boundary or at mininum
Christoph Lameter6446faa2008-02-15 23:45:26 -0800705 * one word if debugging is on to be able to detect writes
Christoph Lameter672bba32007-05-09 02:32:39 -0700706 * before the word boundary.
707 *
708 * Padding is done using 0x5a (POISON_INUSE)
Christoph Lameter81819f02007-05-06 14:49:36 -0700709 *
710 * object + s->size
Christoph Lameter672bba32007-05-09 02:32:39 -0700711 * Nothing is used beyond s->size.
Christoph Lameter81819f02007-05-06 14:49:36 -0700712 *
Christoph Lameter3b0efdf2012-06-13 10:24:57 -0500713 * If slabcaches are merged then the object_size and inuse boundaries are mostly
Christoph Lameter672bba32007-05-09 02:32:39 -0700714 * ignored. And therefore no slab options that rely on these boundaries
Christoph Lameter81819f02007-05-06 14:49:36 -0700715 * may be used with merged slabcaches.
716 */
717
Christoph Lameter81819f02007-05-06 14:49:36 -0700718static int check_pad_bytes(struct kmem_cache *s, struct page *page, u8 *p)
719{
720 unsigned long off = s->inuse; /* The end of info */
721
722 if (s->offset)
723 /* Freepointer is placed after the object. */
724 off += sizeof(void *);
725
726 if (s->flags & SLAB_STORE_USER)
727 /* We also have user information there */
728 off += 2 * sizeof(struct track);
729
730 if (s->size == off)
731 return 1;
732
Christoph Lameter24922682007-07-17 04:03:18 -0700733 return check_bytes_and_report(s, page, p, "Object padding",
734 p + off, POISON_INUSE, s->size - off);
Christoph Lameter81819f02007-05-06 14:49:36 -0700735}
736
Christoph Lameter39b26462008-04-14 19:11:30 +0300737/* Check the pad bytes at the end of a slab page */
Christoph Lameter81819f02007-05-06 14:49:36 -0700738static int slab_pad_check(struct kmem_cache *s, struct page *page)
739{
Christoph Lameter24922682007-07-17 04:03:18 -0700740 u8 *start;
741 u8 *fault;
742 u8 *end;
743 int length;
744 int remainder;
Christoph Lameter81819f02007-05-06 14:49:36 -0700745
746 if (!(s->flags & SLAB_POISON))
747 return 1;
748
Christoph Lametera973e9d2008-03-01 13:40:44 -0800749 start = page_address(page);
Lai Jiangshanab9a0f12011-03-10 15:21:48 +0800750 length = (PAGE_SIZE << compound_order(page)) - s->reserved;
Christoph Lameter39b26462008-04-14 19:11:30 +0300751 end = start + length;
752 remainder = length % s->size;
Christoph Lameter81819f02007-05-06 14:49:36 -0700753 if (!remainder)
754 return 1;
755
Akinobu Mita798248202011-10-31 17:08:07 -0700756 fault = memchr_inv(end - remainder, POISON_INUSE, remainder);
Christoph Lameter24922682007-07-17 04:03:18 -0700757 if (!fault)
758 return 1;
759 while (end > fault && end[-1] == POISON_INUSE)
760 end--;
761
762 slab_err(s, page, "Padding overwritten. 0x%p-0x%p", fault, end - 1);
Sebastian Andrzej Siewiorffc79d22011-07-29 14:10:20 +0200763 print_section("Padding ", end - remainder, remainder);
Christoph Lameter24922682007-07-17 04:03:18 -0700764
Eric Dumazet8a3d2712009-09-03 16:08:06 +0200765 restore_bytes(s, "slab padding", POISON_INUSE, end - remainder, end);
Christoph Lameter24922682007-07-17 04:03:18 -0700766 return 0;
Christoph Lameter81819f02007-05-06 14:49:36 -0700767}
768
769static int check_object(struct kmem_cache *s, struct page *page,
Christoph Lameterf7cb1932010-09-29 07:15:01 -0500770 void *object, u8 val)
Christoph Lameter81819f02007-05-06 14:49:36 -0700771{
772 u8 *p = object;
Christoph Lameter3b0efdf2012-06-13 10:24:57 -0500773 u8 *endobject = object + s->object_size;
Christoph Lameter81819f02007-05-06 14:49:36 -0700774
775 if (s->flags & SLAB_RED_ZONE) {
Christoph Lameter24922682007-07-17 04:03:18 -0700776 if (!check_bytes_and_report(s, page, object, "Redzone",
Christoph Lameter3b0efdf2012-06-13 10:24:57 -0500777 endobject, val, s->inuse - s->object_size))
Christoph Lameter81819f02007-05-06 14:49:36 -0700778 return 0;
Christoph Lameter81819f02007-05-06 14:49:36 -0700779 } else {
Christoph Lameter3b0efdf2012-06-13 10:24:57 -0500780 if ((s->flags & SLAB_POISON) && s->object_size < s->inuse) {
Ingo Molnar3adbefe2008-02-05 17:57:39 -0800781 check_bytes_and_report(s, page, p, "Alignment padding",
Christoph Lameter3b0efdf2012-06-13 10:24:57 -0500782 endobject, POISON_INUSE, s->inuse - s->object_size);
Ingo Molnar3adbefe2008-02-05 17:57:39 -0800783 }
Christoph Lameter81819f02007-05-06 14:49:36 -0700784 }
785
786 if (s->flags & SLAB_POISON) {
Christoph Lameterf7cb1932010-09-29 07:15:01 -0500787 if (val != SLUB_RED_ACTIVE && (s->flags & __OBJECT_POISON) &&
Christoph Lameter24922682007-07-17 04:03:18 -0700788 (!check_bytes_and_report(s, page, p, "Poison", p,
Christoph Lameter3b0efdf2012-06-13 10:24:57 -0500789 POISON_FREE, s->object_size - 1) ||
Christoph Lameter24922682007-07-17 04:03:18 -0700790 !check_bytes_and_report(s, page, p, "Poison",
Christoph Lameter3b0efdf2012-06-13 10:24:57 -0500791 p + s->object_size - 1, POISON_END, 1)))
Christoph Lameter81819f02007-05-06 14:49:36 -0700792 return 0;
Christoph Lameter81819f02007-05-06 14:49:36 -0700793 /*
794 * check_pad_bytes cleans up on its own.
795 */
796 check_pad_bytes(s, page, p);
797 }
798
Christoph Lameterf7cb1932010-09-29 07:15:01 -0500799 if (!s->offset && val == SLUB_RED_ACTIVE)
Christoph Lameter81819f02007-05-06 14:49:36 -0700800 /*
801 * Object and freepointer overlap. Cannot check
802 * freepointer while object is allocated.
803 */
804 return 1;
805
806 /* Check free pointer validity */
807 if (!check_valid_pointer(s, page, get_freepointer(s, p))) {
808 object_err(s, page, p, "Freepointer corrupt");
809 /*
Nick Andrew9f6c708e2008-12-05 14:08:08 +1100810 * No choice but to zap it and thus lose the remainder
Christoph Lameter81819f02007-05-06 14:49:36 -0700811 * of the free objects in this slab. May cause
Christoph Lameter672bba32007-05-09 02:32:39 -0700812 * another error because the object count is now wrong.
Christoph Lameter81819f02007-05-06 14:49:36 -0700813 */
Christoph Lametera973e9d2008-03-01 13:40:44 -0800814 set_freepointer(s, p, NULL);
Christoph Lameter81819f02007-05-06 14:49:36 -0700815 return 0;
816 }
817 return 1;
818}
819
820static int check_slab(struct kmem_cache *s, struct page *page)
821{
Christoph Lameter39b26462008-04-14 19:11:30 +0300822 int maxobj;
823
Christoph Lameter81819f02007-05-06 14:49:36 -0700824 VM_BUG_ON(!irqs_disabled());
825
826 if (!PageSlab(page)) {
Christoph Lameter24922682007-07-17 04:03:18 -0700827 slab_err(s, page, "Not a valid slab page");
Christoph Lameter81819f02007-05-06 14:49:36 -0700828 return 0;
829 }
Christoph Lameter39b26462008-04-14 19:11:30 +0300830
Lai Jiangshanab9a0f12011-03-10 15:21:48 +0800831 maxobj = order_objects(compound_order(page), s->size, s->reserved);
Christoph Lameter39b26462008-04-14 19:11:30 +0300832 if (page->objects > maxobj) {
833 slab_err(s, page, "objects %u > max %u",
834 s->name, page->objects, maxobj);
835 return 0;
836 }
837 if (page->inuse > page->objects) {
Christoph Lameter24922682007-07-17 04:03:18 -0700838 slab_err(s, page, "inuse %u > max %u",
Christoph Lameter39b26462008-04-14 19:11:30 +0300839 s->name, page->inuse, page->objects);
Christoph Lameter81819f02007-05-06 14:49:36 -0700840 return 0;
841 }
842 /* Slab_pad_check fixes things up after itself */
843 slab_pad_check(s, page);
844 return 1;
845}
846
847/*
Christoph Lameter672bba32007-05-09 02:32:39 -0700848 * Determine if a certain object on a page is on the freelist. Must hold the
849 * slab lock to guarantee that the chains are in a consistent state.
Christoph Lameter81819f02007-05-06 14:49:36 -0700850 */
851static int on_freelist(struct kmem_cache *s, struct page *page, void *search)
852{
853 int nr = 0;
Christoph Lameter881db7f2011-06-01 12:25:53 -0500854 void *fp;
Christoph Lameter81819f02007-05-06 14:49:36 -0700855 void *object = NULL;
Christoph Lameter224a88b2008-04-14 19:11:31 +0300856 unsigned long max_objects;
Christoph Lameter81819f02007-05-06 14:49:36 -0700857
Christoph Lameter881db7f2011-06-01 12:25:53 -0500858 fp = page->freelist;
Christoph Lameter39b26462008-04-14 19:11:30 +0300859 while (fp && nr <= page->objects) {
Christoph Lameter81819f02007-05-06 14:49:36 -0700860 if (fp == search)
861 return 1;
862 if (!check_valid_pointer(s, page, fp)) {
863 if (object) {
864 object_err(s, page, object,
865 "Freechain corrupt");
Christoph Lametera973e9d2008-03-01 13:40:44 -0800866 set_freepointer(s, object, NULL);
Christoph Lameter81819f02007-05-06 14:49:36 -0700867 break;
868 } else {
Christoph Lameter24922682007-07-17 04:03:18 -0700869 slab_err(s, page, "Freepointer corrupt");
Christoph Lametera973e9d2008-03-01 13:40:44 -0800870 page->freelist = NULL;
Christoph Lameter39b26462008-04-14 19:11:30 +0300871 page->inuse = page->objects;
Christoph Lameter24922682007-07-17 04:03:18 -0700872 slab_fix(s, "Freelist cleared");
Christoph Lameter81819f02007-05-06 14:49:36 -0700873 return 0;
874 }
875 break;
876 }
877 object = fp;
878 fp = get_freepointer(s, object);
879 nr++;
880 }
881
Lai Jiangshanab9a0f12011-03-10 15:21:48 +0800882 max_objects = order_objects(compound_order(page), s->size, s->reserved);
Cyrill Gorcunov210b5c02008-10-22 23:00:38 +0400883 if (max_objects > MAX_OBJS_PER_PAGE)
884 max_objects = MAX_OBJS_PER_PAGE;
Christoph Lameter224a88b2008-04-14 19:11:31 +0300885
886 if (page->objects != max_objects) {
887 slab_err(s, page, "Wrong number of objects. Found %d but "
888 "should be %d", page->objects, max_objects);
889 page->objects = max_objects;
890 slab_fix(s, "Number of objects adjusted.");
891 }
Christoph Lameter39b26462008-04-14 19:11:30 +0300892 if (page->inuse != page->objects - nr) {
Christoph Lameter70d71222007-05-06 14:49:47 -0700893 slab_err(s, page, "Wrong object count. Counter is %d but "
Christoph Lameter39b26462008-04-14 19:11:30 +0300894 "counted were %d", page->inuse, page->objects - nr);
895 page->inuse = page->objects - nr;
Christoph Lameter24922682007-07-17 04:03:18 -0700896 slab_fix(s, "Object count adjusted.");
Christoph Lameter81819f02007-05-06 14:49:36 -0700897 }
898 return search == NULL;
899}
900
Christoph Lameter0121c6192008-04-29 16:11:12 -0700901static void trace(struct kmem_cache *s, struct page *page, void *object,
902 int alloc)
Christoph Lameter3ec09742007-05-16 22:11:00 -0700903{
904 if (s->flags & SLAB_TRACE) {
905 printk(KERN_INFO "TRACE %s %s 0x%p inuse=%d fp=0x%p\n",
906 s->name,
907 alloc ? "alloc" : "free",
908 object, page->inuse,
909 page->freelist);
910
911 if (!alloc)
Christoph Lameter3b0efdf2012-06-13 10:24:57 -0500912 print_section("Object ", (void *)object, s->object_size);
Christoph Lameter3ec09742007-05-16 22:11:00 -0700913
914 dump_stack();
915 }
916}
917
Christoph Lameter643b1132007-05-06 14:49:42 -0700918/*
Christoph Lameterc016b0b2010-08-20 12:37:16 -0500919 * Hooks for other subsystems that check memory allocations. In a typical
920 * production configuration these hooks all should produce no code at all.
921 */
922static inline int slab_pre_alloc_hook(struct kmem_cache *s, gfp_t flags)
923{
Christoph Lameterc1d50832010-08-20 12:37:17 -0500924 flags &= gfp_allowed_mask;
Christoph Lameterc016b0b2010-08-20 12:37:16 -0500925 lockdep_trace_alloc(flags);
926 might_sleep_if(flags & __GFP_WAIT);
927
Christoph Lameter3b0efdf2012-06-13 10:24:57 -0500928 return should_failslab(s->object_size, flags, s->flags);
Christoph Lameterc016b0b2010-08-20 12:37:16 -0500929}
930
931static inline void slab_post_alloc_hook(struct kmem_cache *s, gfp_t flags, void *object)
932{
Christoph Lameterc1d50832010-08-20 12:37:17 -0500933 flags &= gfp_allowed_mask;
Eric Dumazetb3d41882011-02-14 18:35:22 +0100934 kmemcheck_slab_alloc(s, flags, object, slab_ksize(s));
Christoph Lameter3b0efdf2012-06-13 10:24:57 -0500935 kmemleak_alloc_recursive(object, s->object_size, 1, s->flags, flags);
Christoph Lameterc016b0b2010-08-20 12:37:16 -0500936}
937
938static inline void slab_free_hook(struct kmem_cache *s, void *x)
939{
940 kmemleak_free_recursive(x, s->flags);
Christoph Lameterc016b0b2010-08-20 12:37:16 -0500941
Christoph Lameterd3f661d2011-02-25 11:38:52 -0600942 /*
943 * Trouble is that we may no longer disable interupts in the fast path
944 * So in order to make the debug calls that expect irqs to be
945 * disabled we need to disable interrupts temporarily.
946 */
947#if defined(CONFIG_KMEMCHECK) || defined(CONFIG_LOCKDEP)
948 {
949 unsigned long flags;
950
951 local_irq_save(flags);
Christoph Lameter3b0efdf2012-06-13 10:24:57 -0500952 kmemcheck_slab_free(s, x, s->object_size);
953 debug_check_no_locks_freed(x, s->object_size);
Christoph Lameterd3f661d2011-02-25 11:38:52 -0600954 local_irq_restore(flags);
955 }
956#endif
Thomas Gleixnerf9b615d2011-03-24 21:26:46 +0200957 if (!(s->flags & SLAB_DEBUG_OBJECTS))
Christoph Lameter3b0efdf2012-06-13 10:24:57 -0500958 debug_check_no_obj_freed(x, s->object_size);
Christoph Lameterc016b0b2010-08-20 12:37:16 -0500959}
960
961/*
Christoph Lameter672bba32007-05-09 02:32:39 -0700962 * Tracking of fully allocated slabs for debugging purposes.
Christoph Lameter5cc6eee2011-06-01 12:25:50 -0500963 *
964 * list_lock must be held.
Christoph Lameter643b1132007-05-06 14:49:42 -0700965 */
Christoph Lameter5cc6eee2011-06-01 12:25:50 -0500966static void add_full(struct kmem_cache *s,
967 struct kmem_cache_node *n, struct page *page)
Christoph Lameter643b1132007-05-06 14:49:42 -0700968{
Christoph Lameter643b1132007-05-06 14:49:42 -0700969 if (!(s->flags & SLAB_STORE_USER))
970 return;
971
Christoph Lameter5cc6eee2011-06-01 12:25:50 -0500972 list_add(&page->lru, &n->full);
973}
Christoph Lameter643b1132007-05-06 14:49:42 -0700974
Christoph Lameter5cc6eee2011-06-01 12:25:50 -0500975/*
976 * list_lock must be held.
977 */
978static void remove_full(struct kmem_cache *s, struct page *page)
979{
980 if (!(s->flags & SLAB_STORE_USER))
981 return;
982
Christoph Lameter643b1132007-05-06 14:49:42 -0700983 list_del(&page->lru);
Christoph Lameter643b1132007-05-06 14:49:42 -0700984}
985
Christoph Lameter0f389ec2008-04-14 18:53:02 +0300986/* Tracking of the number of slabs for debugging purposes */
987static inline unsigned long slabs_node(struct kmem_cache *s, int node)
988{
989 struct kmem_cache_node *n = get_node(s, node);
990
991 return atomic_long_read(&n->nr_slabs);
992}
993
Alexander Beregalov26c02cf2009-06-11 14:08:48 +0400994static inline unsigned long node_nr_slabs(struct kmem_cache_node *n)
995{
996 return atomic_long_read(&n->nr_slabs);
997}
998
Christoph Lameter205ab992008-04-14 19:11:40 +0300999static inline void inc_slabs_node(struct kmem_cache *s, int node, int objects)
Christoph Lameter0f389ec2008-04-14 18:53:02 +03001000{
1001 struct kmem_cache_node *n = get_node(s, node);
1002
1003 /*
1004 * May be called early in order to allocate a slab for the
1005 * kmem_cache_node structure. Solve the chicken-egg
1006 * dilemma by deferring the increment of the count during
1007 * bootstrap (see early_kmem_cache_node_alloc).
1008 */
Joonsoo Kim338b2642013-01-21 17:01:27 +09001009 if (likely(n)) {
Christoph Lameter0f389ec2008-04-14 18:53:02 +03001010 atomic_long_inc(&n->nr_slabs);
Christoph Lameter205ab992008-04-14 19:11:40 +03001011 atomic_long_add(objects, &n->total_objects);
1012 }
Christoph Lameter0f389ec2008-04-14 18:53:02 +03001013}
Christoph Lameter205ab992008-04-14 19:11:40 +03001014static inline void dec_slabs_node(struct kmem_cache *s, int node, int objects)
Christoph Lameter0f389ec2008-04-14 18:53:02 +03001015{
1016 struct kmem_cache_node *n = get_node(s, node);
1017
1018 atomic_long_dec(&n->nr_slabs);
Christoph Lameter205ab992008-04-14 19:11:40 +03001019 atomic_long_sub(objects, &n->total_objects);
Christoph Lameter0f389ec2008-04-14 18:53:02 +03001020}
1021
1022/* Object debug checks for alloc/free paths */
Christoph Lameter3ec09742007-05-16 22:11:00 -07001023static void setup_object_debug(struct kmem_cache *s, struct page *page,
1024 void *object)
1025{
1026 if (!(s->flags & (SLAB_STORE_USER|SLAB_RED_ZONE|__OBJECT_POISON)))
1027 return;
1028
Christoph Lameterf7cb1932010-09-29 07:15:01 -05001029 init_object(s, object, SLUB_RED_INACTIVE);
Christoph Lameter3ec09742007-05-16 22:11:00 -07001030 init_tracking(s, object);
1031}
1032
Christoph Lameter15370662010-08-20 12:37:12 -05001033static noinline int alloc_debug_processing(struct kmem_cache *s, struct page *page,
Eduard - Gabriel Munteanuce71e272008-08-19 20:43:25 +03001034 void *object, unsigned long addr)
Christoph Lameter81819f02007-05-06 14:49:36 -07001035{
1036 if (!check_slab(s, page))
1037 goto bad;
1038
Christoph Lameter81819f02007-05-06 14:49:36 -07001039 if (!check_valid_pointer(s, page, object)) {
1040 object_err(s, page, object, "Freelist Pointer check fails");
Christoph Lameter70d71222007-05-06 14:49:47 -07001041 goto bad;
Christoph Lameter81819f02007-05-06 14:49:36 -07001042 }
1043
Christoph Lameterf7cb1932010-09-29 07:15:01 -05001044 if (!check_object(s, page, object, SLUB_RED_INACTIVE))
Christoph Lameter81819f02007-05-06 14:49:36 -07001045 goto bad;
Christoph Lameter81819f02007-05-06 14:49:36 -07001046
Christoph Lameter3ec09742007-05-16 22:11:00 -07001047 /* Success perform special debug activities for allocs */
1048 if (s->flags & SLAB_STORE_USER)
1049 set_track(s, object, TRACK_ALLOC, addr);
1050 trace(s, page, object, 1);
Christoph Lameterf7cb1932010-09-29 07:15:01 -05001051 init_object(s, object, SLUB_RED_ACTIVE);
Christoph Lameter81819f02007-05-06 14:49:36 -07001052 return 1;
Christoph Lameter3ec09742007-05-16 22:11:00 -07001053
Christoph Lameter81819f02007-05-06 14:49:36 -07001054bad:
1055 if (PageSlab(page)) {
1056 /*
1057 * If this is a slab page then lets do the best we can
1058 * to avoid issues in the future. Marking all objects
Christoph Lameter672bba32007-05-09 02:32:39 -07001059 * as used avoids touching the remaining objects.
Christoph Lameter81819f02007-05-06 14:49:36 -07001060 */
Christoph Lameter24922682007-07-17 04:03:18 -07001061 slab_fix(s, "Marking all objects used");
Christoph Lameter39b26462008-04-14 19:11:30 +03001062 page->inuse = page->objects;
Christoph Lametera973e9d2008-03-01 13:40:44 -08001063 page->freelist = NULL;
Christoph Lameter81819f02007-05-06 14:49:36 -07001064 }
1065 return 0;
1066}
1067
Christoph Lameter19c7ff92012-05-30 12:54:46 -05001068static noinline struct kmem_cache_node *free_debug_processing(
1069 struct kmem_cache *s, struct page *page, void *object,
1070 unsigned long addr, unsigned long *flags)
Christoph Lameter81819f02007-05-06 14:49:36 -07001071{
Christoph Lameter19c7ff92012-05-30 12:54:46 -05001072 struct kmem_cache_node *n = get_node(s, page_to_nid(page));
Christoph Lameter5c2e4bb2011-06-01 12:25:54 -05001073
Thomas Gleixner57be9db2012-10-25 10:32:35 +01001074 raw_spin_lock_irqsave(&n->list_lock, *flags);
Christoph Lameter881db7f2011-06-01 12:25:53 -05001075 slab_lock(page);
1076
Christoph Lameter81819f02007-05-06 14:49:36 -07001077 if (!check_slab(s, page))
1078 goto fail;
1079
1080 if (!check_valid_pointer(s, page, object)) {
Christoph Lameter70d71222007-05-06 14:49:47 -07001081 slab_err(s, page, "Invalid object pointer 0x%p", object);
Christoph Lameter81819f02007-05-06 14:49:36 -07001082 goto fail;
1083 }
1084
1085 if (on_freelist(s, page, object)) {
Christoph Lameter24922682007-07-17 04:03:18 -07001086 object_err(s, page, object, "Object already free");
Christoph Lameter81819f02007-05-06 14:49:36 -07001087 goto fail;
1088 }
1089
Christoph Lameterf7cb1932010-09-29 07:15:01 -05001090 if (!check_object(s, page, object, SLUB_RED_ACTIVE))
Christoph Lameter5c2e4bb2011-06-01 12:25:54 -05001091 goto out;
Christoph Lameter81819f02007-05-06 14:49:36 -07001092
Glauber Costa1b4f59e32012-10-22 18:05:36 +04001093 if (unlikely(s != page->slab_cache)) {
Ingo Molnar3adbefe2008-02-05 17:57:39 -08001094 if (!PageSlab(page)) {
Christoph Lameter70d71222007-05-06 14:49:47 -07001095 slab_err(s, page, "Attempt to free object(0x%p) "
1096 "outside of slab", object);
Glauber Costa1b4f59e32012-10-22 18:05:36 +04001097 } else if (!page->slab_cache) {
Christoph Lameter81819f02007-05-06 14:49:36 -07001098 printk(KERN_ERR
Christoph Lameter70d71222007-05-06 14:49:47 -07001099 "SLUB <none>: no slab for object 0x%p.\n",
Christoph Lameter81819f02007-05-06 14:49:36 -07001100 object);
Christoph Lameter70d71222007-05-06 14:49:47 -07001101 dump_stack();
Pekka Enberg06428782008-01-07 23:20:27 -08001102 } else
Christoph Lameter24922682007-07-17 04:03:18 -07001103 object_err(s, page, object,
1104 "page slab pointer corrupt.");
Christoph Lameter81819f02007-05-06 14:49:36 -07001105 goto fail;
1106 }
Christoph Lameter3ec09742007-05-16 22:11:00 -07001107
Christoph Lameter3ec09742007-05-16 22:11:00 -07001108 if (s->flags & SLAB_STORE_USER)
1109 set_track(s, object, TRACK_FREE, addr);
1110 trace(s, page, object, 0);
Christoph Lameterf7cb1932010-09-29 07:15:01 -05001111 init_object(s, object, SLUB_RED_INACTIVE);
Christoph Lameter5c2e4bb2011-06-01 12:25:54 -05001112out:
Christoph Lameter881db7f2011-06-01 12:25:53 -05001113 slab_unlock(page);
Christoph Lameter19c7ff92012-05-30 12:54:46 -05001114 /*
1115 * Keep node_lock to preserve integrity
1116 * until the object is actually freed
1117 */
1118 return n;
Christoph Lameter3ec09742007-05-16 22:11:00 -07001119
Christoph Lameter81819f02007-05-06 14:49:36 -07001120fail:
Christoph Lameter19c7ff92012-05-30 12:54:46 -05001121 slab_unlock(page);
Thomas Gleixner57be9db2012-10-25 10:32:35 +01001122 raw_spin_unlock_irqrestore(&n->list_lock, *flags);
Christoph Lameter24922682007-07-17 04:03:18 -07001123 slab_fix(s, "Object at 0x%p not freed", object);
Christoph Lameter19c7ff92012-05-30 12:54:46 -05001124 return NULL;
Christoph Lameter81819f02007-05-06 14:49:36 -07001125}
1126
Christoph Lameter41ecc552007-05-09 02:32:44 -07001127static int __init setup_slub_debug(char *str)
1128{
Christoph Lameterf0630ff2007-07-15 23:38:14 -07001129 slub_debug = DEBUG_DEFAULT_FLAGS;
1130 if (*str++ != '=' || !*str)
1131 /*
1132 * No options specified. Switch on full debugging.
1133 */
1134 goto out;
Christoph Lameter41ecc552007-05-09 02:32:44 -07001135
1136 if (*str == ',')
Christoph Lameterf0630ff2007-07-15 23:38:14 -07001137 /*
1138 * No options but restriction on slabs. This means full
1139 * debugging for slabs matching a pattern.
1140 */
1141 goto check_slabs;
1142
David Rientjesfa5ec8a2009-07-07 00:14:14 -07001143 if (tolower(*str) == 'o') {
1144 /*
1145 * Avoid enabling debugging on caches if its minimum order
1146 * would increase as a result.
1147 */
1148 disable_higher_order_debug = 1;
1149 goto out;
1150 }
1151
Christoph Lameterf0630ff2007-07-15 23:38:14 -07001152 slub_debug = 0;
1153 if (*str == '-')
1154 /*
1155 * Switch off all debugging measures.
1156 */
1157 goto out;
1158
1159 /*
1160 * Determine which debug features should be switched on
1161 */
Pekka Enberg06428782008-01-07 23:20:27 -08001162 for (; *str && *str != ','; str++) {
Christoph Lameterf0630ff2007-07-15 23:38:14 -07001163 switch (tolower(*str)) {
1164 case 'f':
1165 slub_debug |= SLAB_DEBUG_FREE;
1166 break;
1167 case 'z':
1168 slub_debug |= SLAB_RED_ZONE;
1169 break;
1170 case 'p':
1171 slub_debug |= SLAB_POISON;
1172 break;
1173 case 'u':
1174 slub_debug |= SLAB_STORE_USER;
1175 break;
1176 case 't':
1177 slub_debug |= SLAB_TRACE;
1178 break;
Dmitry Monakhov4c13dd32010-02-26 09:36:12 +03001179 case 'a':
1180 slub_debug |= SLAB_FAILSLAB;
1181 break;
Christoph Lameterf0630ff2007-07-15 23:38:14 -07001182 default:
1183 printk(KERN_ERR "slub_debug option '%c' "
Pekka Enberg06428782008-01-07 23:20:27 -08001184 "unknown. skipped\n", *str);
Christoph Lameterf0630ff2007-07-15 23:38:14 -07001185 }
1186 }
1187
1188check_slabs:
1189 if (*str == ',')
Christoph Lameter41ecc552007-05-09 02:32:44 -07001190 slub_debug_slabs = str + 1;
Christoph Lameterf0630ff2007-07-15 23:38:14 -07001191out:
Christoph Lameter41ecc552007-05-09 02:32:44 -07001192 return 1;
1193}
1194
1195__setup("slub_debug", setup_slub_debug);
1196
Christoph Lameter3b0efdf2012-06-13 10:24:57 -05001197static unsigned long kmem_cache_flags(unsigned long object_size,
Christoph Lameterba0268a2007-09-11 15:24:11 -07001198 unsigned long flags, const char *name,
Alexey Dobriyan51cc5062008-07-25 19:45:34 -07001199 void (*ctor)(void *))
Christoph Lameter41ecc552007-05-09 02:32:44 -07001200{
1201 /*
Christoph Lametere1533622008-02-15 23:45:24 -08001202 * Enable debugging if selected on the kernel commandline.
Christoph Lameter41ecc552007-05-09 02:32:44 -07001203 */
Christoph Lameterfe764072013-11-07 16:29:15 +00001204 if (slub_debug && (!slub_debug_slabs || (name &&
1205 !strncmp(slub_debug_slabs, name, strlen(slub_debug_slabs)))))
David Rientjes3de472132009-07-27 18:30:35 -07001206 flags |= slub_debug;
Christoph Lameterba0268a2007-09-11 15:24:11 -07001207
1208 return flags;
Christoph Lameter41ecc552007-05-09 02:32:44 -07001209}
1210#else
Christoph Lameter3ec09742007-05-16 22:11:00 -07001211static inline void setup_object_debug(struct kmem_cache *s,
1212 struct page *page, void *object) {}
Christoph Lameter41ecc552007-05-09 02:32:44 -07001213
Christoph Lameter3ec09742007-05-16 22:11:00 -07001214static inline int alloc_debug_processing(struct kmem_cache *s,
Eduard - Gabriel Munteanuce71e272008-08-19 20:43:25 +03001215 struct page *page, void *object, unsigned long addr) { return 0; }
Christoph Lameter41ecc552007-05-09 02:32:44 -07001216
Christoph Lameter19c7ff92012-05-30 12:54:46 -05001217static inline struct kmem_cache_node *free_debug_processing(
1218 struct kmem_cache *s, struct page *page, void *object,
1219 unsigned long addr, unsigned long *flags) { return NULL; }
Christoph Lameter41ecc552007-05-09 02:32:44 -07001220
Christoph Lameter41ecc552007-05-09 02:32:44 -07001221static inline int slab_pad_check(struct kmem_cache *s, struct page *page)
1222 { return 1; }
1223static inline int check_object(struct kmem_cache *s, struct page *page,
Christoph Lameterf7cb1932010-09-29 07:15:01 -05001224 void *object, u8 val) { return 1; }
Christoph Lameter5cc6eee2011-06-01 12:25:50 -05001225static inline void add_full(struct kmem_cache *s, struct kmem_cache_node *n,
1226 struct page *page) {}
Christoph Lameter2cfb7452011-06-01 12:25:52 -05001227static inline void remove_full(struct kmem_cache *s, struct page *page) {}
Christoph Lameter3b0efdf2012-06-13 10:24:57 -05001228static inline unsigned long kmem_cache_flags(unsigned long object_size,
Christoph Lameterba0268a2007-09-11 15:24:11 -07001229 unsigned long flags, const char *name,
Alexey Dobriyan51cc5062008-07-25 19:45:34 -07001230 void (*ctor)(void *))
Christoph Lameterba0268a2007-09-11 15:24:11 -07001231{
1232 return flags;
1233}
Christoph Lameter41ecc552007-05-09 02:32:44 -07001234#define slub_debug 0
Christoph Lameter0f389ec2008-04-14 18:53:02 +03001235
Ingo Molnarfdaa45e2009-09-15 11:00:26 +02001236#define disable_higher_order_debug 0
1237
Christoph Lameter0f389ec2008-04-14 18:53:02 +03001238static inline unsigned long slabs_node(struct kmem_cache *s, int node)
1239 { return 0; }
Alexander Beregalov26c02cf2009-06-11 14:08:48 +04001240static inline unsigned long node_nr_slabs(struct kmem_cache_node *n)
1241 { return 0; }
Christoph Lameter205ab992008-04-14 19:11:40 +03001242static inline void inc_slabs_node(struct kmem_cache *s, int node,
1243 int objects) {}
1244static inline void dec_slabs_node(struct kmem_cache *s, int node,
1245 int objects) {}
Christoph Lameter7d550c52010-08-25 14:07:16 -05001246
1247static inline int slab_pre_alloc_hook(struct kmem_cache *s, gfp_t flags)
1248 { return 0; }
1249
1250static inline void slab_post_alloc_hook(struct kmem_cache *s, gfp_t flags,
1251 void *object) {}
1252
1253static inline void slab_free_hook(struct kmem_cache *s, void *x) {}
1254
Christoph Lameterab4d5ed2010-10-05 13:57:26 -05001255#endif /* CONFIG_SLUB_DEBUG */
Christoph Lameter205ab992008-04-14 19:11:40 +03001256
Thomas Gleixner57be9db2012-10-25 10:32:35 +01001257struct slub_free_list {
1258 raw_spinlock_t lock;
1259 struct list_head list;
1260};
1261static DEFINE_PER_CPU(struct slub_free_list, slub_free_list);
1262
Christoph Lameter81819f02007-05-06 14:49:36 -07001263/*
1264 * Slab allocation and freeing
1265 */
Christoph Lameter65c33762008-04-14 19:11:40 +03001266static inline struct page *alloc_slab_page(gfp_t flags, int node,
1267 struct kmem_cache_order_objects oo)
1268{
1269 int order = oo_order(oo);
1270
Vegard Nossumb1eeab62008-11-25 16:55:53 +01001271 flags |= __GFP_NOTRACK;
1272
Christoph Lameter2154a332010-07-09 14:07:10 -05001273 if (node == NUMA_NO_NODE)
Christoph Lameter65c33762008-04-14 19:11:40 +03001274 return alloc_pages(flags, order);
1275 else
Minchan Kim6b65aaf2010-04-14 23:58:36 +09001276 return alloc_pages_exact_node(node, flags, order);
Christoph Lameter65c33762008-04-14 19:11:40 +03001277}
1278
Christoph Lameter81819f02007-05-06 14:49:36 -07001279static struct page *allocate_slab(struct kmem_cache *s, gfp_t flags, int node)
1280{
Pekka Enberg06428782008-01-07 23:20:27 -08001281 struct page *page;
Christoph Lameter834f3d12008-04-14 19:11:31 +03001282 struct kmem_cache_order_objects oo = s->oo;
Pekka Enbergba522702009-06-24 21:59:51 +03001283 gfp_t alloc_gfp;
Thomas Gleixnerc7475ec2013-01-09 12:08:15 +01001284 bool enableirqs;
Christoph Lameter81819f02007-05-06 14:49:36 -07001285
Christoph Lameter7e0528d2011-06-01 12:25:44 -05001286 flags &= gfp_allowed_mask;
1287
Thomas Gleixnerc7475ec2013-01-09 12:08:15 +01001288 enableirqs = (flags & __GFP_WAIT) != 0;
Thomas Gleixner57be9db2012-10-25 10:32:35 +01001289#ifdef CONFIG_PREEMPT_RT_FULL
Thomas Gleixnerc7475ec2013-01-09 12:08:15 +01001290 enableirqs |= system_state == SYSTEM_RUNNING;
Thomas Gleixner57be9db2012-10-25 10:32:35 +01001291#endif
Thomas Gleixnerc7475ec2013-01-09 12:08:15 +01001292 if (enableirqs)
Christoph Lameter7e0528d2011-06-01 12:25:44 -05001293 local_irq_enable();
1294
Christoph Lameterb7a49f02008-02-14 14:21:32 -08001295 flags |= s->allocflags;
Mel Gormane12ba742007-10-16 01:25:52 -07001296
Pekka Enbergba522702009-06-24 21:59:51 +03001297 /*
1298 * Let the initial higher-order allocation fail under memory pressure
1299 * so we fall-back to the minimum order allocation.
1300 */
1301 alloc_gfp = (flags | __GFP_NOWARN | __GFP_NORETRY) & ~__GFP_NOFAIL;
1302
1303 page = alloc_slab_page(alloc_gfp, node, oo);
Christoph Lameter65c33762008-04-14 19:11:40 +03001304 if (unlikely(!page)) {
1305 oo = s->min;
1306 /*
1307 * Allocation may have failed due to fragmentation.
1308 * Try a lower order alloc if possible
1309 */
1310 page = alloc_slab_page(flags, node, oo);
Christoph Lameter81819f02007-05-06 14:49:36 -07001311
Christoph Lameter7e0528d2011-06-01 12:25:44 -05001312 if (page)
1313 stat(s, ORDER_FALLBACK);
Christoph Lameter65c33762008-04-14 19:11:40 +03001314 }
Vegard Nossum5a896d92008-04-04 00:54:48 +02001315
David Rientjes737b7192012-07-09 14:00:38 -07001316 if (kmemcheck_enabled && page
Amerigo Wang5086c389c2009-08-19 21:44:13 +03001317 && !(s->flags & (SLAB_NOTRACK | DEBUG_DEFAULT_FLAGS))) {
Vegard Nossumb1eeab62008-11-25 16:55:53 +01001318 int pages = 1 << oo_order(oo);
1319
1320 kmemcheck_alloc_shadow(page, oo_order(oo), flags, node);
1321
1322 /*
1323 * Objects from caches that have a constructor don't get
1324 * cleared when they're allocated, so we need to do it here.
1325 */
1326 if (s->ctor)
1327 kmemcheck_mark_uninitialized_pages(page, pages);
1328 else
1329 kmemcheck_mark_unallocated_pages(page, pages);
Vegard Nossum5a896d92008-04-04 00:54:48 +02001330 }
1331
Thomas Gleixnerc7475ec2013-01-09 12:08:15 +01001332 if (enableirqs)
David Rientjes737b7192012-07-09 14:00:38 -07001333 local_irq_disable();
1334 if (!page)
1335 return NULL;
1336
Christoph Lameter834f3d12008-04-14 19:11:31 +03001337 page->objects = oo_objects(oo);
Christoph Lameter81819f02007-05-06 14:49:36 -07001338 mod_zone_page_state(page_zone(page),
1339 (s->flags & SLAB_RECLAIM_ACCOUNT) ?
1340 NR_SLAB_RECLAIMABLE : NR_SLAB_UNRECLAIMABLE,
Christoph Lameter65c33762008-04-14 19:11:40 +03001341 1 << oo_order(oo));
Christoph Lameter81819f02007-05-06 14:49:36 -07001342
1343 return page;
1344}
1345
1346static void setup_object(struct kmem_cache *s, struct page *page,
1347 void *object)
1348{
Christoph Lameter3ec09742007-05-16 22:11:00 -07001349 setup_object_debug(s, page, object);
Sebastian Andrzej Siewior2c12a532013-11-13 11:21:04 -05001350#ifndef CONFIG_PREEMPT_RT_FULL
Christoph Lameter4f104932007-05-06 14:50:17 -07001351 if (unlikely(s->ctor))
Alexey Dobriyan51cc5062008-07-25 19:45:34 -07001352 s->ctor(object);
Sebastian Andrzej Siewior2c12a532013-11-13 11:21:04 -05001353#endif
Christoph Lameter81819f02007-05-06 14:49:36 -07001354}
1355
1356static struct page *new_slab(struct kmem_cache *s, gfp_t flags, int node)
1357{
1358 struct page *page;
Christoph Lameter81819f02007-05-06 14:49:36 -07001359 void *start;
Christoph Lameter81819f02007-05-06 14:49:36 -07001360 void *last;
1361 void *p;
Glauber Costa1f458cb2012-12-18 14:22:50 -08001362 int order;
Christoph Lameter81819f02007-05-06 14:49:36 -07001363
Christoph Lameter6cb06222007-10-16 01:25:41 -07001364 BUG_ON(flags & GFP_SLAB_BUG_MASK);
Christoph Lameter81819f02007-05-06 14:49:36 -07001365
Christoph Lameter6cb06222007-10-16 01:25:41 -07001366 page = allocate_slab(s,
1367 flags & (GFP_RECLAIM_MASK | GFP_CONSTRAINT_MASK), node);
Christoph Lameter81819f02007-05-06 14:49:36 -07001368 if (!page)
1369 goto out;
1370
Glauber Costa1f458cb2012-12-18 14:22:50 -08001371 order = compound_order(page);
Christoph Lameter205ab992008-04-14 19:11:40 +03001372 inc_slabs_node(s, page_to_nid(page), page->objects);
Glauber Costa1f458cb2012-12-18 14:22:50 -08001373 memcg_bind_pages(s, order);
Glauber Costa1b4f59e32012-10-22 18:05:36 +04001374 page->slab_cache = s;
Joonsoo Kimc03f94c2012-05-18 00:47:47 +09001375 __SetPageSlab(page);
Mel Gorman072bb0a2012-07-31 16:43:58 -07001376 if (page->pfmemalloc)
1377 SetPageSlabPfmemalloc(page);
Christoph Lameter81819f02007-05-06 14:49:36 -07001378
1379 start = page_address(page);
Christoph Lameter81819f02007-05-06 14:49:36 -07001380
1381 if (unlikely(s->flags & SLAB_POISON))
Glauber Costa1f458cb2012-12-18 14:22:50 -08001382 memset(start, POISON_INUSE, PAGE_SIZE << order);
Christoph Lameter81819f02007-05-06 14:49:36 -07001383
1384 last = start;
Christoph Lameter224a88b2008-04-14 19:11:31 +03001385 for_each_object(p, s, start, page->objects) {
Christoph Lameter81819f02007-05-06 14:49:36 -07001386 setup_object(s, page, last);
1387 set_freepointer(s, last, p);
1388 last = p;
1389 }
1390 setup_object(s, page, last);
Christoph Lametera973e9d2008-03-01 13:40:44 -08001391 set_freepointer(s, last, NULL);
Christoph Lameter81819f02007-05-06 14:49:36 -07001392
1393 page->freelist = start;
Christoph Lametere6e82ea2011-08-09 16:12:24 -05001394 page->inuse = page->objects;
Christoph Lameter8cb0a502011-06-01 12:25:46 -05001395 page->frozen = 1;
Christoph Lameter81819f02007-05-06 14:49:36 -07001396out:
Christoph Lameter81819f02007-05-06 14:49:36 -07001397 return page;
1398}
1399
1400static void __free_slab(struct kmem_cache *s, struct page *page)
1401{
Christoph Lameter834f3d12008-04-14 19:11:31 +03001402 int order = compound_order(page);
1403 int pages = 1 << order;
Christoph Lameter81819f02007-05-06 14:49:36 -07001404
Christoph Lameteraf537b02010-07-09 14:07:14 -05001405 if (kmem_cache_debug(s)) {
Christoph Lameter81819f02007-05-06 14:49:36 -07001406 void *p;
1407
1408 slab_pad_check(s, page);
Christoph Lameter224a88b2008-04-14 19:11:31 +03001409 for_each_object(p, s, page_address(page),
1410 page->objects)
Christoph Lameterf7cb1932010-09-29 07:15:01 -05001411 check_object(s, page, p, SLUB_RED_INACTIVE);
Christoph Lameter81819f02007-05-06 14:49:36 -07001412 }
1413
Vegard Nossumb1eeab62008-11-25 16:55:53 +01001414 kmemcheck_free_shadow(page, compound_order(page));
Vegard Nossum5a896d92008-04-04 00:54:48 +02001415
Christoph Lameter81819f02007-05-06 14:49:36 -07001416 mod_zone_page_state(page_zone(page),
1417 (s->flags & SLAB_RECLAIM_ACCOUNT) ?
1418 NR_SLAB_RECLAIMABLE : NR_SLAB_UNRECLAIMABLE,
Pekka Enberg06428782008-01-07 23:20:27 -08001419 -pages);
Christoph Lameter81819f02007-05-06 14:49:36 -07001420
Mel Gorman072bb0a2012-07-31 16:43:58 -07001421 __ClearPageSlabPfmemalloc(page);
Christoph Lameter49bd5222008-04-14 18:52:18 +03001422 __ClearPageSlab(page);
Glauber Costa1f458cb2012-12-18 14:22:50 -08001423
1424 memcg_release_pages(s, order);
Mel Gorman22b751c2013-02-22 16:34:59 -08001425 page_mapcount_reset(page);
Nick Piggin1eb5ac62009-05-05 19:13:44 +10001426 if (current->reclaim_state)
1427 current->reclaim_state->reclaimed_slab += pages;
Glauber Costad79923f2012-12-18 14:22:48 -08001428 __free_memcg_kmem_pages(page, order);
Christoph Lameter81819f02007-05-06 14:49:36 -07001429}
1430
Sebastian Andrzej Siewior7dd7d9e2013-11-08 12:01:18 +01001431static void free_delayed(struct list_head *h)
Thomas Gleixner57be9db2012-10-25 10:32:35 +01001432{
1433 while(!list_empty(h)) {
1434 struct page *page = list_first_entry(h, struct page, lru);
1435
1436 list_del(&page->lru);
Sebastian Andrzej Siewior7dd7d9e2013-11-08 12:01:18 +01001437 __free_slab(page->slab_cache, page);
Thomas Gleixner57be9db2012-10-25 10:32:35 +01001438 }
1439}
1440
Lai Jiangshanda9a6382011-03-10 15:22:00 +08001441#define need_reserve_slab_rcu \
1442 (sizeof(((struct page *)NULL)->lru) < sizeof(struct rcu_head))
1443
Christoph Lameter81819f02007-05-06 14:49:36 -07001444static void rcu_free_slab(struct rcu_head *h)
1445{
1446 struct page *page;
1447
Lai Jiangshanda9a6382011-03-10 15:22:00 +08001448 if (need_reserve_slab_rcu)
1449 page = virt_to_head_page(h);
1450 else
1451 page = container_of((struct list_head *)h, struct page, lru);
1452
Glauber Costa1b4f59e32012-10-22 18:05:36 +04001453 __free_slab(page->slab_cache, page);
Christoph Lameter81819f02007-05-06 14:49:36 -07001454}
1455
1456static void free_slab(struct kmem_cache *s, struct page *page)
1457{
1458 if (unlikely(s->flags & SLAB_DESTROY_BY_RCU)) {
Lai Jiangshanda9a6382011-03-10 15:22:00 +08001459 struct rcu_head *head;
1460
1461 if (need_reserve_slab_rcu) {
1462 int order = compound_order(page);
1463 int offset = (PAGE_SIZE << order) - s->reserved;
1464
1465 VM_BUG_ON(s->reserved != sizeof(*head));
1466 head = page_address(page) + offset;
1467 } else {
1468 /*
1469 * RCU free overloads the RCU head over the LRU
1470 */
1471 head = (void *)&page->lru;
1472 }
Christoph Lameter81819f02007-05-06 14:49:36 -07001473
1474 call_rcu(head, rcu_free_slab);
Thomas Gleixner57be9db2012-10-25 10:32:35 +01001475 } else if (irqs_disabled()) {
1476 struct slub_free_list *f = &__get_cpu_var(slub_free_list);
1477
1478 raw_spin_lock(&f->lock);
1479 list_add(&page->lru, &f->list);
1480 raw_spin_unlock(&f->lock);
Christoph Lameter81819f02007-05-06 14:49:36 -07001481 } else
1482 __free_slab(s, page);
1483}
1484
1485static void discard_slab(struct kmem_cache *s, struct page *page)
1486{
Christoph Lameter205ab992008-04-14 19:11:40 +03001487 dec_slabs_node(s, page_to_nid(page), page->objects);
Christoph Lameter81819f02007-05-06 14:49:36 -07001488 free_slab(s, page);
1489}
1490
1491/*
Christoph Lameter5cc6eee2011-06-01 12:25:50 -05001492 * Management of partially allocated slabs.
1493 *
1494 * list_lock must be held.
Christoph Lameter81819f02007-05-06 14:49:36 -07001495 */
Christoph Lameter5cc6eee2011-06-01 12:25:50 -05001496static inline void add_partial(struct kmem_cache_node *n,
Christoph Lameter7c2e1322008-01-07 23:20:27 -08001497 struct page *page, int tail)
Christoph Lameter81819f02007-05-06 14:49:36 -07001498{
Christoph Lametere95eed52007-05-06 14:49:44 -07001499 n->nr_partial++;
Shaohua Li136333d2011-08-24 08:57:52 +08001500 if (tail == DEACTIVATE_TO_TAIL)
Christoph Lameter7c2e1322008-01-07 23:20:27 -08001501 list_add_tail(&page->lru, &n->partial);
1502 else
1503 list_add(&page->lru, &n->partial);
Christoph Lameter81819f02007-05-06 14:49:36 -07001504}
1505
Christoph Lameter5cc6eee2011-06-01 12:25:50 -05001506/*
1507 * list_lock must be held.
1508 */
1509static inline void remove_partial(struct kmem_cache_node *n,
Christoph Lameter62e346a2010-09-28 08:10:28 -05001510 struct page *page)
1511{
1512 list_del(&page->lru);
1513 n->nr_partial--;
1514}
1515
Christoph Lameter81819f02007-05-06 14:49:36 -07001516/*
Christoph Lameter7ced3712012-05-09 10:09:53 -05001517 * Remove slab from the partial list, freeze it and
1518 * return the pointer to the freelist.
Christoph Lameter81819f02007-05-06 14:49:36 -07001519 *
Christoph Lameter497b66f2011-08-09 16:12:26 -05001520 * Returns a list of objects or NULL if it fails.
1521 *
Christoph Lameter7ced3712012-05-09 10:09:53 -05001522 * Must hold list_lock since we modify the partial list.
Christoph Lameter81819f02007-05-06 14:49:36 -07001523 */
Christoph Lameter497b66f2011-08-09 16:12:26 -05001524static inline void *acquire_slab(struct kmem_cache *s,
Christoph Lameteracd19fd2011-08-09 16:12:25 -05001525 struct kmem_cache_node *n, struct page *page,
Joonsoo Kim633b0762013-01-21 17:01:25 +09001526 int mode, int *objects)
Christoph Lameter81819f02007-05-06 14:49:36 -07001527{
Christoph Lameter2cfb7452011-06-01 12:25:52 -05001528 void *freelist;
1529 unsigned long counters;
1530 struct page new;
1531
Christoph Lameter2cfb7452011-06-01 12:25:52 -05001532 /*
1533 * Zap the freelist and set the frozen bit.
1534 * The old freelist is the list of objects for the
1535 * per cpu allocation list.
1536 */
Christoph Lameter7ced3712012-05-09 10:09:53 -05001537 freelist = page->freelist;
1538 counters = page->counters;
1539 new.counters = counters;
Joonsoo Kim633b0762013-01-21 17:01:25 +09001540 *objects = new.objects - new.inuse;
Pekka Enberg23910c52012-06-04 10:14:58 +03001541 if (mode) {
Christoph Lameter7ced3712012-05-09 10:09:53 -05001542 new.inuse = page->objects;
Pekka Enberg23910c52012-06-04 10:14:58 +03001543 new.freelist = NULL;
1544 } else {
1545 new.freelist = freelist;
1546 }
Christoph Lameter2cfb7452011-06-01 12:25:52 -05001547
Christoph Lameter7ced3712012-05-09 10:09:53 -05001548 VM_BUG_ON(new.frozen);
1549 new.frozen = 1;
Christoph Lameter2cfb7452011-06-01 12:25:52 -05001550
Christoph Lameter7ced3712012-05-09 10:09:53 -05001551 if (!__cmpxchg_double_slab(s, page,
Christoph Lameter2cfb7452011-06-01 12:25:52 -05001552 freelist, counters,
Joonsoo Kim02d76332012-05-17 00:13:02 +09001553 new.freelist, new.counters,
Christoph Lameter7ced3712012-05-09 10:09:53 -05001554 "acquire_slab"))
Christoph Lameter7ced3712012-05-09 10:09:53 -05001555 return NULL;
Christoph Lameter2cfb7452011-06-01 12:25:52 -05001556
1557 remove_partial(n, page);
Christoph Lameter7ced3712012-05-09 10:09:53 -05001558 WARN_ON(!freelist);
Christoph Lameter49e22582011-08-09 16:12:27 -05001559 return freelist;
Christoph Lameter81819f02007-05-06 14:49:36 -07001560}
1561
Joonsoo Kim633b0762013-01-21 17:01:25 +09001562static void put_cpu_partial(struct kmem_cache *s, struct page *page, int drain);
Joonsoo Kim8ba00bb2012-09-17 14:09:09 -07001563static inline bool pfmemalloc_match(struct page *page, gfp_t gfpflags);
Christoph Lameter49e22582011-08-09 16:12:27 -05001564
Christoph Lameter81819f02007-05-06 14:49:36 -07001565/*
Christoph Lameter672bba32007-05-09 02:32:39 -07001566 * Try to allocate a partial slab from a specific node.
Christoph Lameter81819f02007-05-06 14:49:36 -07001567 */
Joonsoo Kim8ba00bb2012-09-17 14:09:09 -07001568static void *get_partial_node(struct kmem_cache *s, struct kmem_cache_node *n,
1569 struct kmem_cache_cpu *c, gfp_t flags)
Christoph Lameter81819f02007-05-06 14:49:36 -07001570{
Christoph Lameter49e22582011-08-09 16:12:27 -05001571 struct page *page, *page2;
1572 void *object = NULL;
Joonsoo Kim633b0762013-01-21 17:01:25 +09001573 int available = 0;
1574 int objects;
Christoph Lameter81819f02007-05-06 14:49:36 -07001575
1576 /*
1577 * Racy check. If we mistakenly see no partial slabs then we
1578 * just allocate an empty slab. If we mistakenly try to get a
Christoph Lameter672bba32007-05-09 02:32:39 -07001579 * partial slab and there is none available then get_partials()
1580 * will return NULL.
Christoph Lameter81819f02007-05-06 14:49:36 -07001581 */
1582 if (!n || !n->nr_partial)
1583 return NULL;
1584
Thomas Gleixner57be9db2012-10-25 10:32:35 +01001585 raw_spin_lock(&n->list_lock);
Christoph Lameter49e22582011-08-09 16:12:27 -05001586 list_for_each_entry_safe(page, page2, &n->partial, lru) {
Joonsoo Kim8ba00bb2012-09-17 14:09:09 -07001587 void *t;
Christoph Lameter49e22582011-08-09 16:12:27 -05001588
Joonsoo Kim8ba00bb2012-09-17 14:09:09 -07001589 if (!pfmemalloc_match(page, flags))
1590 continue;
1591
Joonsoo Kim633b0762013-01-21 17:01:25 +09001592 t = acquire_slab(s, n, page, object == NULL, &objects);
Christoph Lameter49e22582011-08-09 16:12:27 -05001593 if (!t)
1594 break;
1595
Joonsoo Kim633b0762013-01-21 17:01:25 +09001596 available += objects;
Alex,Shi12d79632011-09-07 10:26:36 +08001597 if (!object) {
Christoph Lameter49e22582011-08-09 16:12:27 -05001598 c->page = page;
Christoph Lameter49e22582011-08-09 16:12:27 -05001599 stat(s, ALLOC_FROM_PARTIAL);
Christoph Lameter49e22582011-08-09 16:12:27 -05001600 object = t;
Christoph Lameter49e22582011-08-09 16:12:27 -05001601 } else {
Joonsoo Kim633b0762013-01-21 17:01:25 +09001602 put_cpu_partial(s, page, 0);
Alex Shi8028dce2012-02-03 23:34:56 +08001603 stat(s, CPU_PARTIAL_NODE);
Christoph Lameter49e22582011-08-09 16:12:27 -05001604 }
1605 if (kmem_cache_debug(s) || available > s->cpu_partial / 2)
1606 break;
1607
Christoph Lameter497b66f2011-08-09 16:12:26 -05001608 }
Thomas Gleixner57be9db2012-10-25 10:32:35 +01001609 raw_spin_unlock(&n->list_lock);
Christoph Lameter497b66f2011-08-09 16:12:26 -05001610 return object;
Christoph Lameter81819f02007-05-06 14:49:36 -07001611}
1612
1613/*
Christoph Lameter672bba32007-05-09 02:32:39 -07001614 * Get a page from somewhere. Search in increasing NUMA distances.
Christoph Lameter81819f02007-05-06 14:49:36 -07001615 */
Joonsoo Kimde3ec032012-01-27 00:12:23 -08001616static void *get_any_partial(struct kmem_cache *s, gfp_t flags,
Christoph Lameteracd19fd2011-08-09 16:12:25 -05001617 struct kmem_cache_cpu *c)
Christoph Lameter81819f02007-05-06 14:49:36 -07001618{
1619#ifdef CONFIG_NUMA
1620 struct zonelist *zonelist;
Mel Gormandd1a2392008-04-28 02:12:17 -07001621 struct zoneref *z;
Mel Gorman54a6eb52008-04-28 02:12:16 -07001622 struct zone *zone;
1623 enum zone_type high_zoneidx = gfp_zone(flags);
Christoph Lameter497b66f2011-08-09 16:12:26 -05001624 void *object;
Mel Gormancc9a6c82012-03-21 16:34:11 -07001625 unsigned int cpuset_mems_cookie;
Christoph Lameter81819f02007-05-06 14:49:36 -07001626
1627 /*
Christoph Lameter672bba32007-05-09 02:32:39 -07001628 * The defrag ratio allows a configuration of the tradeoffs between
1629 * inter node defragmentation and node local allocations. A lower
1630 * defrag_ratio increases the tendency to do local allocations
1631 * instead of attempting to obtain partial slabs from other nodes.
Christoph Lameter81819f02007-05-06 14:49:36 -07001632 *
Christoph Lameter672bba32007-05-09 02:32:39 -07001633 * If the defrag_ratio is set to 0 then kmalloc() always
1634 * returns node local objects. If the ratio is higher then kmalloc()
1635 * may return off node objects because partial slabs are obtained
1636 * from other nodes and filled up.
Christoph Lameter81819f02007-05-06 14:49:36 -07001637 *
Christoph Lameter6446faa2008-02-15 23:45:26 -08001638 * If /sys/kernel/slab/xx/defrag_ratio is set to 100 (which makes
Christoph Lameter672bba32007-05-09 02:32:39 -07001639 * defrag_ratio = 1000) then every (well almost) allocation will
1640 * first attempt to defrag slab caches on other nodes. This means
1641 * scanning over all nodes to look for partial slabs which may be
1642 * expensive if we do it every time we are trying to find a slab
1643 * with available objects.
Christoph Lameter81819f02007-05-06 14:49:36 -07001644 */
Christoph Lameter98246012008-01-07 23:20:26 -08001645 if (!s->remote_node_defrag_ratio ||
1646 get_cycles() % 1024 > s->remote_node_defrag_ratio)
Christoph Lameter81819f02007-05-06 14:49:36 -07001647 return NULL;
1648
Mel Gormancc9a6c82012-03-21 16:34:11 -07001649 do {
1650 cpuset_mems_cookie = get_mems_allowed();
Andi Kleene7b691b2012-06-09 02:40:03 -07001651 zonelist = node_zonelist(slab_node(), flags);
Mel Gormancc9a6c82012-03-21 16:34:11 -07001652 for_each_zone_zonelist(zone, z, zonelist, high_zoneidx) {
1653 struct kmem_cache_node *n;
Christoph Lameter81819f02007-05-06 14:49:36 -07001654
Mel Gormancc9a6c82012-03-21 16:34:11 -07001655 n = get_node(s, zone_to_nid(zone));
Christoph Lameter81819f02007-05-06 14:49:36 -07001656
Mel Gormancc9a6c82012-03-21 16:34:11 -07001657 if (n && cpuset_zone_allowed_hardwall(zone, flags) &&
1658 n->nr_partial > s->min_partial) {
Joonsoo Kim8ba00bb2012-09-17 14:09:09 -07001659 object = get_partial_node(s, n, c, flags);
Mel Gormancc9a6c82012-03-21 16:34:11 -07001660 if (object) {
1661 /*
1662 * Return the object even if
1663 * put_mems_allowed indicated that
1664 * the cpuset mems_allowed was
1665 * updated in parallel. It's a
1666 * harmless race between the alloc
1667 * and the cpuset update.
1668 */
1669 put_mems_allowed(cpuset_mems_cookie);
1670 return object;
1671 }
Miao Xiec0ff7452010-05-24 14:32:08 -07001672 }
Christoph Lameter81819f02007-05-06 14:49:36 -07001673 }
Mel Gormancc9a6c82012-03-21 16:34:11 -07001674 } while (!put_mems_allowed(cpuset_mems_cookie));
Christoph Lameter81819f02007-05-06 14:49:36 -07001675#endif
1676 return NULL;
1677}
1678
1679/*
1680 * Get a partial page, lock it and return it.
1681 */
Christoph Lameter497b66f2011-08-09 16:12:26 -05001682static void *get_partial(struct kmem_cache *s, gfp_t flags, int node,
Christoph Lameteracd19fd2011-08-09 16:12:25 -05001683 struct kmem_cache_cpu *c)
Christoph Lameter81819f02007-05-06 14:49:36 -07001684{
Christoph Lameter497b66f2011-08-09 16:12:26 -05001685 void *object;
Christoph Lameter2154a332010-07-09 14:07:10 -05001686 int searchnode = (node == NUMA_NO_NODE) ? numa_node_id() : node;
Christoph Lameter81819f02007-05-06 14:49:36 -07001687
Joonsoo Kim8ba00bb2012-09-17 14:09:09 -07001688 object = get_partial_node(s, get_node(s, searchnode), c, flags);
Christoph Lameter497b66f2011-08-09 16:12:26 -05001689 if (object || node != NUMA_NO_NODE)
1690 return object;
Christoph Lameter81819f02007-05-06 14:49:36 -07001691
Christoph Lameteracd19fd2011-08-09 16:12:25 -05001692 return get_any_partial(s, flags, c);
Christoph Lameter81819f02007-05-06 14:49:36 -07001693}
1694
Christoph Lameter8a5ec0b2011-02-25 11:38:54 -06001695#ifdef CONFIG_PREEMPT
1696/*
1697 * Calculate the next globally unique transaction for disambiguiation
1698 * during cmpxchg. The transactions start with the cpu number and are then
1699 * incremented by CONFIG_NR_CPUS.
1700 */
1701#define TID_STEP roundup_pow_of_two(CONFIG_NR_CPUS)
1702#else
1703/*
1704 * No preemption supported therefore also no need to check for
1705 * different cpus.
1706 */
1707#define TID_STEP 1
1708#endif
1709
1710static inline unsigned long next_tid(unsigned long tid)
1711{
1712 return tid + TID_STEP;
1713}
1714
1715static inline unsigned int tid_to_cpu(unsigned long tid)
1716{
1717 return tid % TID_STEP;
1718}
1719
1720static inline unsigned long tid_to_event(unsigned long tid)
1721{
1722 return tid / TID_STEP;
1723}
1724
1725static inline unsigned int init_tid(int cpu)
1726{
1727 return cpu;
1728}
1729
1730static inline void note_cmpxchg_failure(const char *n,
1731 const struct kmem_cache *s, unsigned long tid)
1732{
1733#ifdef SLUB_DEBUG_CMPXCHG
1734 unsigned long actual_tid = __this_cpu_read(s->cpu_slab->tid);
1735
1736 printk(KERN_INFO "%s %s: cmpxchg redo ", n, s->name);
1737
1738#ifdef CONFIG_PREEMPT
1739 if (tid_to_cpu(tid) != tid_to_cpu(actual_tid))
1740 printk("due to cpu change %d -> %d\n",
1741 tid_to_cpu(tid), tid_to_cpu(actual_tid));
1742 else
1743#endif
1744 if (tid_to_event(tid) != tid_to_event(actual_tid))
1745 printk("due to cpu running other code. Event %ld->%ld\n",
1746 tid_to_event(tid), tid_to_event(actual_tid));
1747 else
1748 printk("for unknown reason: actual=%lx was=%lx target=%lx\n",
1749 actual_tid, tid, next_tid(tid));
1750#endif
Christoph Lameter4fdccdf2011-03-22 13:35:00 -05001751 stat(s, CMPXCHG_DOUBLE_CPU_FAIL);
Christoph Lameter8a5ec0b2011-02-25 11:38:54 -06001752}
1753
Fengguang Wu788e1aa2012-09-28 16:34:05 +08001754static void init_kmem_cache_cpus(struct kmem_cache *s)
Christoph Lameter8a5ec0b2011-02-25 11:38:54 -06001755{
Christoph Lameter8a5ec0b2011-02-25 11:38:54 -06001756 int cpu;
1757
1758 for_each_possible_cpu(cpu)
1759 per_cpu_ptr(s->cpu_slab, cpu)->tid = init_tid(cpu);
Christoph Lameter8a5ec0b2011-02-25 11:38:54 -06001760}
Christoph Lameter2cfb7452011-06-01 12:25:52 -05001761
1762/*
1763 * Remove the cpu slab
1764 */
Christoph Lameterc17dda42012-05-09 10:09:57 -05001765static void deactivate_slab(struct kmem_cache *s, struct page *page, void *freelist)
Christoph Lameter81819f02007-05-06 14:49:36 -07001766{
Christoph Lameter2cfb7452011-06-01 12:25:52 -05001767 enum slab_modes { M_NONE, M_PARTIAL, M_FULL, M_FREE };
Christoph Lameter2cfb7452011-06-01 12:25:52 -05001768 struct kmem_cache_node *n = get_node(s, page_to_nid(page));
1769 int lock = 0;
1770 enum slab_modes l = M_NONE, m = M_NONE;
Christoph Lameter2cfb7452011-06-01 12:25:52 -05001771 void *nextfree;
Shaohua Li136333d2011-08-24 08:57:52 +08001772 int tail = DEACTIVATE_TO_HEAD;
Christoph Lameter2cfb7452011-06-01 12:25:52 -05001773 struct page new;
1774 struct page old;
Christoph Lameter8ff12cf2008-02-07 17:47:41 -08001775
Christoph Lameter2cfb7452011-06-01 12:25:52 -05001776 if (page->freelist) {
Christoph Lameter84e554e62009-12-18 16:26:23 -06001777 stat(s, DEACTIVATE_REMOTE_FREES);
Shaohua Li136333d2011-08-24 08:57:52 +08001778 tail = DEACTIVATE_TO_TAIL;
Christoph Lameter894b8782007-05-10 03:15:16 -07001779 }
Christoph Lameter2cfb7452011-06-01 12:25:52 -05001780
Christoph Lameter2cfb7452011-06-01 12:25:52 -05001781 /*
1782 * Stage one: Free all available per cpu objects back
1783 * to the page freelist while it is still frozen. Leave the
1784 * last one.
1785 *
1786 * There is no need to take the list->lock because the page
1787 * is still frozen.
1788 */
1789 while (freelist && (nextfree = get_freepointer(s, freelist))) {
1790 void *prior;
1791 unsigned long counters;
1792
1793 do {
1794 prior = page->freelist;
1795 counters = page->counters;
1796 set_freepointer(s, freelist, prior);
1797 new.counters = counters;
1798 new.inuse--;
1799 VM_BUG_ON(!new.frozen);
1800
Christoph Lameter1d071712011-07-14 12:49:12 -05001801 } while (!__cmpxchg_double_slab(s, page,
Christoph Lameter2cfb7452011-06-01 12:25:52 -05001802 prior, counters,
1803 freelist, new.counters,
1804 "drain percpu freelist"));
1805
1806 freelist = nextfree;
1807 }
1808
1809 /*
1810 * Stage two: Ensure that the page is unfrozen while the
1811 * list presence reflects the actual number of objects
1812 * during unfreeze.
1813 *
1814 * We setup the list membership and then perform a cmpxchg
1815 * with the count. If there is a mismatch then the page
1816 * is not unfrozen but the page is on the wrong list.
1817 *
1818 * Then we restart the process which may have to remove
1819 * the page from the list that we just put it on again
1820 * because the number of objects in the slab may have
1821 * changed.
1822 */
1823redo:
1824
1825 old.freelist = page->freelist;
1826 old.counters = page->counters;
1827 VM_BUG_ON(!old.frozen);
1828
1829 /* Determine target state of the slab */
1830 new.counters = old.counters;
1831 if (freelist) {
1832 new.inuse--;
1833 set_freepointer(s, freelist, old.freelist);
1834 new.freelist = freelist;
1835 } else
1836 new.freelist = old.freelist;
1837
1838 new.frozen = 0;
1839
Christoph Lameter81107182011-08-09 13:01:32 -05001840 if (!new.inuse && n->nr_partial > s->min_partial)
Christoph Lameter2cfb7452011-06-01 12:25:52 -05001841 m = M_FREE;
1842 else if (new.freelist) {
1843 m = M_PARTIAL;
1844 if (!lock) {
1845 lock = 1;
1846 /*
1847 * Taking the spinlock removes the possiblity
1848 * that acquire_slab() will see a slab page that
1849 * is frozen
1850 */
Thomas Gleixner57be9db2012-10-25 10:32:35 +01001851 raw_spin_lock(&n->list_lock);
Christoph Lameter2cfb7452011-06-01 12:25:52 -05001852 }
1853 } else {
1854 m = M_FULL;
1855 if (kmem_cache_debug(s) && !lock) {
1856 lock = 1;
1857 /*
1858 * This also ensures that the scanning of full
1859 * slabs from diagnostic functions will not see
1860 * any frozen slabs.
1861 */
Thomas Gleixner57be9db2012-10-25 10:32:35 +01001862 raw_spin_lock(&n->list_lock);
Christoph Lameter2cfb7452011-06-01 12:25:52 -05001863 }
1864 }
1865
1866 if (l != m) {
1867
1868 if (l == M_PARTIAL)
1869
1870 remove_partial(n, page);
1871
1872 else if (l == M_FULL)
1873
1874 remove_full(s, page);
1875
1876 if (m == M_PARTIAL) {
1877
1878 add_partial(n, page, tail);
Shaohua Li136333d2011-08-24 08:57:52 +08001879 stat(s, tail);
Christoph Lameter2cfb7452011-06-01 12:25:52 -05001880
1881 } else if (m == M_FULL) {
1882
1883 stat(s, DEACTIVATE_FULL);
1884 add_full(s, n, page);
1885
1886 }
1887 }
1888
1889 l = m;
Christoph Lameter1d071712011-07-14 12:49:12 -05001890 if (!__cmpxchg_double_slab(s, page,
Christoph Lameter2cfb7452011-06-01 12:25:52 -05001891 old.freelist, old.counters,
1892 new.freelist, new.counters,
1893 "unfreezing slab"))
1894 goto redo;
1895
Christoph Lameter2cfb7452011-06-01 12:25:52 -05001896 if (lock)
Thomas Gleixner57be9db2012-10-25 10:32:35 +01001897 raw_spin_unlock(&n->list_lock);
Christoph Lameter2cfb7452011-06-01 12:25:52 -05001898
1899 if (m == M_FREE) {
1900 stat(s, DEACTIVATE_EMPTY);
1901 discard_slab(s, page);
1902 stat(s, FREE_SLAB);
1903 }
Christoph Lameter81819f02007-05-06 14:49:36 -07001904}
1905
Joonsoo Kimd24ac772012-05-18 22:01:17 +09001906/*
1907 * Unfreeze all the cpu partial slabs.
1908 *
Christoph Lameter59a09912012-11-28 16:23:00 +00001909 * This function must be called with interrupts disabled
1910 * for the cpu using c (or some other guarantee must be there
1911 * to guarantee no concurrent accesses).
Joonsoo Kimd24ac772012-05-18 22:01:17 +09001912 */
Christoph Lameter59a09912012-11-28 16:23:00 +00001913static void unfreeze_partials(struct kmem_cache *s,
1914 struct kmem_cache_cpu *c)
Christoph Lameter49e22582011-08-09 16:12:27 -05001915{
Joonsoo Kim43d77862012-06-09 02:23:16 +09001916 struct kmem_cache_node *n = NULL, *n2 = NULL;
Shaohua Li9ada1932011-11-14 13:34:13 +08001917 struct page *page, *discard_page = NULL;
Christoph Lameter49e22582011-08-09 16:12:27 -05001918
1919 while ((page = c->partial)) {
Christoph Lameter49e22582011-08-09 16:12:27 -05001920 struct page new;
1921 struct page old;
1922
1923 c->partial = page->next;
Joonsoo Kim43d77862012-06-09 02:23:16 +09001924
1925 n2 = get_node(s, page_to_nid(page));
1926 if (n != n2) {
1927 if (n)
Thomas Gleixner57be9db2012-10-25 10:32:35 +01001928 raw_spin_unlock(&n->list_lock);
Joonsoo Kim43d77862012-06-09 02:23:16 +09001929
1930 n = n2;
Thomas Gleixner57be9db2012-10-25 10:32:35 +01001931 raw_spin_lock(&n->list_lock);
Joonsoo Kim43d77862012-06-09 02:23:16 +09001932 }
Christoph Lameter49e22582011-08-09 16:12:27 -05001933
1934 do {
1935
1936 old.freelist = page->freelist;
1937 old.counters = page->counters;
1938 VM_BUG_ON(!old.frozen);
1939
1940 new.counters = old.counters;
1941 new.freelist = old.freelist;
1942
1943 new.frozen = 0;
1944
Joonsoo Kimd24ac772012-05-18 22:01:17 +09001945 } while (!__cmpxchg_double_slab(s, page,
Christoph Lameter49e22582011-08-09 16:12:27 -05001946 old.freelist, old.counters,
1947 new.freelist, new.counters,
1948 "unfreezing slab"));
1949
Joonsoo Kim43d77862012-06-09 02:23:16 +09001950 if (unlikely(!new.inuse && n->nr_partial > s->min_partial)) {
Shaohua Li9ada1932011-11-14 13:34:13 +08001951 page->next = discard_page;
1952 discard_page = page;
Joonsoo Kim43d77862012-06-09 02:23:16 +09001953 } else {
1954 add_partial(n, page, DEACTIVATE_TO_TAIL);
1955 stat(s, FREE_ADD_PARTIAL);
Christoph Lameter49e22582011-08-09 16:12:27 -05001956 }
1957 }
1958
1959 if (n)
Thomas Gleixner57be9db2012-10-25 10:32:35 +01001960 raw_spin_unlock(&n->list_lock);
Shaohua Li9ada1932011-11-14 13:34:13 +08001961
1962 while (discard_page) {
1963 page = discard_page;
1964 discard_page = discard_page->next;
1965
1966 stat(s, DEACTIVATE_EMPTY);
1967 discard_slab(s, page);
1968 stat(s, FREE_SLAB);
1969 }
Christoph Lameter49e22582011-08-09 16:12:27 -05001970}
1971
1972/*
1973 * Put a page that was just frozen (in __slab_free) into a partial page
1974 * slot if available. This is done without interrupts disabled and without
1975 * preemption disabled. The cmpxchg is racy and may put the partial page
1976 * onto a random cpus partial slot.
1977 *
1978 * If we did not find a slot then simply move all the partials to the
1979 * per node partial list.
1980 */
Joonsoo Kim633b0762013-01-21 17:01:25 +09001981static void put_cpu_partial(struct kmem_cache *s, struct page *page, int drain)
Christoph Lameter49e22582011-08-09 16:12:27 -05001982{
1983 struct page *oldpage;
1984 int pages;
1985 int pobjects;
1986
1987 do {
1988 pages = 0;
1989 pobjects = 0;
1990 oldpage = this_cpu_read(s->cpu_slab->partial);
1991
1992 if (oldpage) {
1993 pobjects = oldpage->pobjects;
1994 pages = oldpage->pages;
1995 if (drain && pobjects > s->cpu_partial) {
Thomas Gleixner57be9db2012-10-25 10:32:35 +01001996 struct slub_free_list *f;
Christoph Lameter49e22582011-08-09 16:12:27 -05001997 unsigned long flags;
Thomas Gleixner57be9db2012-10-25 10:32:35 +01001998 LIST_HEAD(tofree);
Christoph Lameter49e22582011-08-09 16:12:27 -05001999 /*
2000 * partial array is full. Move the existing
2001 * set to the per node partial list.
2002 */
2003 local_irq_save(flags);
Christoph Lameter59a09912012-11-28 16:23:00 +00002004 unfreeze_partials(s, this_cpu_ptr(s->cpu_slab));
Thomas Gleixner57be9db2012-10-25 10:32:35 +01002005 f = &__get_cpu_var(slub_free_list);
2006 raw_spin_lock(&f->lock);
2007 list_splice_init(&f->list, &tofree);
2008 raw_spin_unlock(&f->lock);
Christoph Lameter49e22582011-08-09 16:12:27 -05002009 local_irq_restore(flags);
Sebastian Andrzej Siewior7dd7d9e2013-11-08 12:01:18 +01002010 free_delayed(&tofree);
Joonsoo Kime24fc412012-06-23 03:22:38 +09002011 oldpage = NULL;
Christoph Lameter49e22582011-08-09 16:12:27 -05002012 pobjects = 0;
2013 pages = 0;
Alex Shi8028dce2012-02-03 23:34:56 +08002014 stat(s, CPU_PARTIAL_DRAIN);
Christoph Lameter49e22582011-08-09 16:12:27 -05002015 }
2016 }
2017
2018 pages++;
2019 pobjects += page->objects - page->inuse;
2020
2021 page->pages = pages;
2022 page->pobjects = pobjects;
2023 page->next = oldpage;
2024
Christoph Lameter933393f2011-12-22 11:58:51 -06002025 } while (this_cpu_cmpxchg(s->cpu_slab->partial, oldpage, page) != oldpage);
Christoph Lameter49e22582011-08-09 16:12:27 -05002026}
2027
Christoph Lameterdfb4f092007-10-16 01:26:05 -07002028static inline void flush_slab(struct kmem_cache *s, struct kmem_cache_cpu *c)
Christoph Lameter81819f02007-05-06 14:49:36 -07002029{
Christoph Lameter84e554e62009-12-18 16:26:23 -06002030 stat(s, CPUSLAB_FLUSH);
Christoph Lameterc17dda42012-05-09 10:09:57 -05002031 deactivate_slab(s, c->page, c->freelist);
2032
2033 c->tid = next_tid(c->tid);
2034 c->page = NULL;
2035 c->freelist = NULL;
Christoph Lameter81819f02007-05-06 14:49:36 -07002036}
2037
2038/*
2039 * Flush cpu slab.
Christoph Lameter6446faa2008-02-15 23:45:26 -08002040 *
Christoph Lameter81819f02007-05-06 14:49:36 -07002041 * Called from IPI handler with interrupts disabled.
2042 */
Christoph Lameter0c710012007-07-17 04:03:24 -07002043static inline void __flush_cpu_slab(struct kmem_cache *s, int cpu)
Christoph Lameter81819f02007-05-06 14:49:36 -07002044{
Christoph Lameter9dfc6e62009-12-18 16:26:20 -06002045 struct kmem_cache_cpu *c = per_cpu_ptr(s->cpu_slab, cpu);
Christoph Lameter81819f02007-05-06 14:49:36 -07002046
Christoph Lameter49e22582011-08-09 16:12:27 -05002047 if (likely(c)) {
2048 if (c->page)
2049 flush_slab(s, c);
2050
Christoph Lameter59a09912012-11-28 16:23:00 +00002051 unfreeze_partials(s, c);
Christoph Lameter49e22582011-08-09 16:12:27 -05002052 }
Christoph Lameter81819f02007-05-06 14:49:36 -07002053}
2054
2055static void flush_cpu_slab(void *d)
2056{
2057 struct kmem_cache *s = d;
Christoph Lameter81819f02007-05-06 14:49:36 -07002058
Christoph Lameterdfb4f092007-10-16 01:26:05 -07002059 __flush_cpu_slab(s, smp_processor_id());
Christoph Lameter81819f02007-05-06 14:49:36 -07002060}
2061
Gilad Ben-Yossefa8364d52012-03-28 14:42:44 -07002062static bool has_cpu_slab(int cpu, void *info)
2063{
2064 struct kmem_cache *s = info;
2065 struct kmem_cache_cpu *c = per_cpu_ptr(s->cpu_slab, cpu);
2066
majianpeng02e1a9c2012-05-17 17:03:26 -07002067 return c->page || c->partial;
Gilad Ben-Yossefa8364d52012-03-28 14:42:44 -07002068}
2069
Christoph Lameter81819f02007-05-06 14:49:36 -07002070static void flush_all(struct kmem_cache *s)
2071{
Thomas Gleixner57be9db2012-10-25 10:32:35 +01002072 LIST_HEAD(tofree);
2073 int cpu;
2074
Gilad Ben-Yossefa8364d52012-03-28 14:42:44 -07002075 on_each_cpu_cond(has_cpu_slab, flush_cpu_slab, s, 1, GFP_ATOMIC);
Thomas Gleixner57be9db2012-10-25 10:32:35 +01002076 for_each_online_cpu(cpu) {
2077 struct slub_free_list *f;
2078
2079 if (!has_cpu_slab(cpu, s))
2080 continue;
2081
2082 f = &per_cpu(slub_free_list, cpu);
2083 raw_spin_lock_irq(&f->lock);
2084 list_splice_init(&f->list, &tofree);
2085 raw_spin_unlock_irq(&f->lock);
Sebastian Andrzej Siewior7dd7d9e2013-11-08 12:01:18 +01002086 free_delayed(&tofree);
Thomas Gleixner57be9db2012-10-25 10:32:35 +01002087 }
Christoph Lameter81819f02007-05-06 14:49:36 -07002088}
2089
2090/*
Christoph Lameterdfb4f092007-10-16 01:26:05 -07002091 * Check if the objects in a per cpu structure fit numa
2092 * locality expectations.
2093 */
Christoph Lameter57d437d2012-05-09 10:09:59 -05002094static inline int node_match(struct page *page, int node)
Christoph Lameterdfb4f092007-10-16 01:26:05 -07002095{
2096#ifdef CONFIG_NUMA
Christoph Lameter4d7868e2013-01-23 21:45:47 +00002097 if (!page || (node != NUMA_NO_NODE && page_to_nid(page) != node))
Christoph Lameterdfb4f092007-10-16 01:26:05 -07002098 return 0;
2099#endif
2100 return 1;
2101}
2102
Pekka Enberg781b2ba2009-06-10 18:50:32 +03002103static int count_free(struct page *page)
2104{
2105 return page->objects - page->inuse;
2106}
2107
2108static unsigned long count_partial(struct kmem_cache_node *n,
2109 int (*get_count)(struct page *))
2110{
2111 unsigned long flags;
2112 unsigned long x = 0;
2113 struct page *page;
2114
Thomas Gleixner57be9db2012-10-25 10:32:35 +01002115 raw_spin_lock_irqsave(&n->list_lock, flags);
Pekka Enberg781b2ba2009-06-10 18:50:32 +03002116 list_for_each_entry(page, &n->partial, lru)
2117 x += get_count(page);
Thomas Gleixner57be9db2012-10-25 10:32:35 +01002118 raw_spin_unlock_irqrestore(&n->list_lock, flags);
Pekka Enberg781b2ba2009-06-10 18:50:32 +03002119 return x;
2120}
2121
Alexander Beregalov26c02cf2009-06-11 14:08:48 +04002122static inline unsigned long node_nr_objs(struct kmem_cache_node *n)
2123{
2124#ifdef CONFIG_SLUB_DEBUG
2125 return atomic_long_read(&n->total_objects);
2126#else
2127 return 0;
2128#endif
2129}
2130
Pekka Enberg781b2ba2009-06-10 18:50:32 +03002131static noinline void
2132slab_out_of_memory(struct kmem_cache *s, gfp_t gfpflags, int nid)
2133{
2134 int node;
2135
2136 printk(KERN_WARNING
2137 "SLUB: Unable to allocate memory on node %d (gfp=0x%x)\n",
2138 nid, gfpflags);
2139 printk(KERN_WARNING " cache: %s, object size: %d, buffer size: %d, "
Christoph Lameter3b0efdf2012-06-13 10:24:57 -05002140 "default order: %d, min order: %d\n", s->name, s->object_size,
Pekka Enberg781b2ba2009-06-10 18:50:32 +03002141 s->size, oo_order(s->oo), oo_order(s->min));
2142
Christoph Lameter3b0efdf2012-06-13 10:24:57 -05002143 if (oo_order(s->min) > get_order(s->object_size))
David Rientjesfa5ec8a2009-07-07 00:14:14 -07002144 printk(KERN_WARNING " %s debugging increased min order, use "
2145 "slub_debug=O to disable.\n", s->name);
2146
Pekka Enberg781b2ba2009-06-10 18:50:32 +03002147 for_each_online_node(node) {
2148 struct kmem_cache_node *n = get_node(s, node);
2149 unsigned long nr_slabs;
2150 unsigned long nr_objs;
2151 unsigned long nr_free;
2152
2153 if (!n)
2154 continue;
2155
Alexander Beregalov26c02cf2009-06-11 14:08:48 +04002156 nr_free = count_partial(n, count_free);
2157 nr_slabs = node_nr_slabs(n);
2158 nr_objs = node_nr_objs(n);
Pekka Enberg781b2ba2009-06-10 18:50:32 +03002159
2160 printk(KERN_WARNING
2161 " node %d: slabs: %ld, objs: %ld, free: %ld\n",
2162 node, nr_slabs, nr_objs, nr_free);
2163 }
2164}
2165
Christoph Lameter497b66f2011-08-09 16:12:26 -05002166static inline void *new_slab_objects(struct kmem_cache *s, gfp_t flags,
2167 int node, struct kmem_cache_cpu **pc)
2168{
Christoph Lameter6faa6832012-05-09 10:09:51 -05002169 void *freelist;
Christoph Lameter188fd062012-05-09 10:09:55 -05002170 struct kmem_cache_cpu *c = *pc;
2171 struct page *page;
Christoph Lameter497b66f2011-08-09 16:12:26 -05002172
Christoph Lameter188fd062012-05-09 10:09:55 -05002173 freelist = get_partial(s, flags, node, c);
2174
2175 if (freelist)
2176 return freelist;
2177
2178 page = new_slab(s, flags, node);
Christoph Lameter497b66f2011-08-09 16:12:26 -05002179 if (page) {
2180 c = __this_cpu_ptr(s->cpu_slab);
2181 if (c->page)
2182 flush_slab(s, c);
2183
2184 /*
2185 * No other reference to the page yet so we can
2186 * muck around with it freely without cmpxchg
2187 */
Christoph Lameter6faa6832012-05-09 10:09:51 -05002188 freelist = page->freelist;
Christoph Lameter497b66f2011-08-09 16:12:26 -05002189 page->freelist = NULL;
2190
2191 stat(s, ALLOC_SLAB);
Christoph Lameter497b66f2011-08-09 16:12:26 -05002192 c->page = page;
2193 *pc = c;
2194 } else
Christoph Lameter6faa6832012-05-09 10:09:51 -05002195 freelist = NULL;
Christoph Lameter497b66f2011-08-09 16:12:26 -05002196
Christoph Lameter6faa6832012-05-09 10:09:51 -05002197 return freelist;
Christoph Lameter497b66f2011-08-09 16:12:26 -05002198}
2199
Mel Gorman072bb0a2012-07-31 16:43:58 -07002200static inline bool pfmemalloc_match(struct page *page, gfp_t gfpflags)
2201{
2202 if (unlikely(PageSlabPfmemalloc(page)))
2203 return gfp_pfmemalloc_allowed(gfpflags);
2204
2205 return true;
2206}
2207
Christoph Lameterdfb4f092007-10-16 01:26:05 -07002208/*
Christoph Lameter213eeb92011-11-11 14:07:14 -06002209 * Check the page->freelist of a page and either transfer the freelist to the per cpu freelist
2210 * or deactivate the page.
2211 *
2212 * The page is still frozen if the return value is not NULL.
2213 *
2214 * If this function returns NULL then the page has been unfrozen.
Joonsoo Kimd24ac772012-05-18 22:01:17 +09002215 *
2216 * This function must be called with interrupt disabled.
Christoph Lameter213eeb92011-11-11 14:07:14 -06002217 */
2218static inline void *get_freelist(struct kmem_cache *s, struct page *page)
2219{
2220 struct page new;
2221 unsigned long counters;
2222 void *freelist;
2223
2224 do {
2225 freelist = page->freelist;
2226 counters = page->counters;
Christoph Lameter6faa6832012-05-09 10:09:51 -05002227
Christoph Lameter213eeb92011-11-11 14:07:14 -06002228 new.counters = counters;
2229 VM_BUG_ON(!new.frozen);
2230
2231 new.inuse = page->objects;
2232 new.frozen = freelist != NULL;
2233
Joonsoo Kimd24ac772012-05-18 22:01:17 +09002234 } while (!__cmpxchg_double_slab(s, page,
Christoph Lameter213eeb92011-11-11 14:07:14 -06002235 freelist, counters,
2236 NULL, new.counters,
2237 "get_freelist"));
2238
2239 return freelist;
2240}
2241
2242/*
Christoph Lameter894b8782007-05-10 03:15:16 -07002243 * Slow path. The lockless freelist is empty or we need to perform
2244 * debugging duties.
Christoph Lameter81819f02007-05-06 14:49:36 -07002245 *
Christoph Lameter894b8782007-05-10 03:15:16 -07002246 * Processing is still very fast if new objects have been freed to the
2247 * regular freelist. In that case we simply take over the regular freelist
2248 * as the lockless freelist and zap the regular freelist.
Christoph Lameter81819f02007-05-06 14:49:36 -07002249 *
Christoph Lameter894b8782007-05-10 03:15:16 -07002250 * If that is not working then we fall back to the partial lists. We take the
2251 * first element of the freelist as the object to allocate now and move the
2252 * rest of the freelist to the lockless freelist.
2253 *
2254 * And if we were unable to get a new slab from the partial slab lists then
Christoph Lameter6446faa2008-02-15 23:45:26 -08002255 * we need to allocate a new slab. This is the slowest path since it involves
2256 * a call to the page allocator and the setup of a new slab.
Christoph Lameter81819f02007-05-06 14:49:36 -07002257 */
Eduard - Gabriel Munteanuce71e272008-08-19 20:43:25 +03002258static void *__slab_alloc(struct kmem_cache *s, gfp_t gfpflags, int node,
2259 unsigned long addr, struct kmem_cache_cpu *c)
Christoph Lameter81819f02007-05-06 14:49:36 -07002260{
Thomas Gleixner57be9db2012-10-25 10:32:35 +01002261 struct slub_free_list *f;
Christoph Lameter6faa6832012-05-09 10:09:51 -05002262 void *freelist;
Christoph Lameterf6e7def2012-05-09 10:09:58 -05002263 struct page *page;
Christoph Lameter8a5ec0b2011-02-25 11:38:54 -06002264 unsigned long flags;
Thomas Gleixner57be9db2012-10-25 10:32:35 +01002265 LIST_HEAD(tofree);
Christoph Lameter8a5ec0b2011-02-25 11:38:54 -06002266
2267 local_irq_save(flags);
2268#ifdef CONFIG_PREEMPT
2269 /*
2270 * We may have been preempted and rescheduled on a different
2271 * cpu before disabling interrupts. Need to reload cpu area
2272 * pointer.
2273 */
2274 c = this_cpu_ptr(s->cpu_slab);
2275#endif
Christoph Lameter81819f02007-05-06 14:49:36 -07002276
Christoph Lameterf6e7def2012-05-09 10:09:58 -05002277 page = c->page;
2278 if (!page)
Christoph Lameter81819f02007-05-06 14:49:36 -07002279 goto new_slab;
Christoph Lameter49e22582011-08-09 16:12:27 -05002280redo:
Christoph Lameter6faa6832012-05-09 10:09:51 -05002281
Christoph Lameter57d437d2012-05-09 10:09:59 -05002282 if (unlikely(!node_match(page, node))) {
Christoph Lametere36a2652011-06-01 12:25:57 -05002283 stat(s, ALLOC_NODE_MISMATCH);
Christoph Lameterf6e7def2012-05-09 10:09:58 -05002284 deactivate_slab(s, page, c->freelist);
Christoph Lameterc17dda42012-05-09 10:09:57 -05002285 c->page = NULL;
2286 c->freelist = NULL;
Christoph Lameterfc59c052011-06-01 12:25:56 -05002287 goto new_slab;
2288 }
Christoph Lameter6446faa2008-02-15 23:45:26 -08002289
Mel Gorman072bb0a2012-07-31 16:43:58 -07002290 /*
2291 * By rights, we should be searching for a slab page that was
2292 * PFMEMALLOC but right now, we are losing the pfmemalloc
2293 * information when the page leaves the per-cpu allocator
2294 */
2295 if (unlikely(!pfmemalloc_match(page, gfpflags))) {
2296 deactivate_slab(s, page, c->freelist);
2297 c->page = NULL;
2298 c->freelist = NULL;
2299 goto new_slab;
2300 }
2301
Eric Dumazet73736e02011-12-13 04:57:06 +01002302 /* must check again c->freelist in case of cpu migration or IRQ */
Christoph Lameter6faa6832012-05-09 10:09:51 -05002303 freelist = c->freelist;
2304 if (freelist)
Eric Dumazet73736e02011-12-13 04:57:06 +01002305 goto load_freelist;
2306
Christoph Lameter2cfb7452011-06-01 12:25:52 -05002307 stat(s, ALLOC_SLOWPATH);
2308
Christoph Lameterf6e7def2012-05-09 10:09:58 -05002309 freelist = get_freelist(s, page);
Christoph Lameter6446faa2008-02-15 23:45:26 -08002310
Christoph Lameter6faa6832012-05-09 10:09:51 -05002311 if (!freelist) {
Christoph Lameter03e404a2011-06-01 12:25:58 -05002312 c->page = NULL;
2313 stat(s, DEACTIVATE_BYPASS);
Christoph Lameterfc59c052011-06-01 12:25:56 -05002314 goto new_slab;
Christoph Lameter03e404a2011-06-01 12:25:58 -05002315 }
Christoph Lameterdfb4f092007-10-16 01:26:05 -07002316
Christoph Lameter81819f02007-05-06 14:49:36 -07002317 stat(s, ALLOC_REFILL);
Christoph Lameter8ff12cf2008-02-07 17:47:41 -08002318
Christoph Lameter894b8782007-05-10 03:15:16 -07002319load_freelist:
Christoph Lameter507effe2012-05-09 10:09:52 -05002320 /*
2321 * freelist is pointing to the list of objects to be used.
2322 * page is pointing to the page from which the objects are obtained.
2323 * That page must be frozen for per cpu allocations to work.
2324 */
2325 VM_BUG_ON(!c->page->frozen);
Christoph Lameter6faa6832012-05-09 10:09:51 -05002326 c->freelist = get_freepointer(s, freelist);
Christoph Lameter8a5ec0b2011-02-25 11:38:54 -06002327 c->tid = next_tid(c->tid);
Thomas Gleixner57be9db2012-10-25 10:32:35 +01002328out:
2329 f = &__get_cpu_var(slub_free_list);
2330 raw_spin_lock(&f->lock);
2331 list_splice_init(&f->list, &tofree);
2332 raw_spin_unlock(&f->lock);
Christoph Lameter8a5ec0b2011-02-25 11:38:54 -06002333 local_irq_restore(flags);
Sebastian Andrzej Siewior7dd7d9e2013-11-08 12:01:18 +01002334 free_delayed(&tofree);
Christoph Lameter6faa6832012-05-09 10:09:51 -05002335 return freelist;
Christoph Lameter81819f02007-05-06 14:49:36 -07002336
Christoph Lameter81819f02007-05-06 14:49:36 -07002337new_slab:
Christoph Lameter2cfb7452011-06-01 12:25:52 -05002338
Christoph Lameter49e22582011-08-09 16:12:27 -05002339 if (c->partial) {
Christoph Lameterf6e7def2012-05-09 10:09:58 -05002340 page = c->page = c->partial;
2341 c->partial = page->next;
Christoph Lameter49e22582011-08-09 16:12:27 -05002342 stat(s, CPU_PARTIAL_ALLOC);
2343 c->freelist = NULL;
2344 goto redo;
Christoph Lameter81819f02007-05-06 14:49:36 -07002345 }
2346
Christoph Lameter188fd062012-05-09 10:09:55 -05002347 freelist = new_slab_objects(s, gfpflags, node, &c);
Christoph Lameterb811c202007-10-16 23:25:51 -07002348
Christoph Lameterf46974362012-05-09 10:09:54 -05002349 if (unlikely(!freelist)) {
2350 if (!(gfpflags & __GFP_NOWARN) && printk_ratelimit())
2351 slab_out_of_memory(s, gfpflags, node);
Thomas Gleixner57be9db2012-10-25 10:32:35 +01002352 goto out;
Christoph Lameter81819f02007-05-06 14:49:36 -07002353 }
Christoph Lameter894b8782007-05-10 03:15:16 -07002354
Christoph Lameterf6e7def2012-05-09 10:09:58 -05002355 page = c->page;
Christoph Lameter5091b742012-07-31 16:44:00 -07002356 if (likely(!kmem_cache_debug(s) && pfmemalloc_match(page, gfpflags)))
Christoph Lameter81819f02007-05-06 14:49:36 -07002357 goto load_freelist;
Christoph Lameter894b8782007-05-10 03:15:16 -07002358
Christoph Lameter497b66f2011-08-09 16:12:26 -05002359 /* Only entered in the debug case */
Christoph Lameter5091b742012-07-31 16:44:00 -07002360 if (kmem_cache_debug(s) && !alloc_debug_processing(s, page, freelist, addr))
Christoph Lameter497b66f2011-08-09 16:12:26 -05002361 goto new_slab; /* Slab failed checks. Next slab needed */
Christoph Lameter2cfb7452011-06-01 12:25:52 -05002362
Christoph Lameterf6e7def2012-05-09 10:09:58 -05002363 deactivate_slab(s, page, get_freepointer(s, freelist));
Christoph Lameterc17dda42012-05-09 10:09:57 -05002364 c->page = NULL;
2365 c->freelist = NULL;
Thomas Gleixner57be9db2012-10-25 10:32:35 +01002366 goto out;
Christoph Lameter894b8782007-05-10 03:15:16 -07002367}
2368
2369/*
2370 * Inlined fastpath so that allocation functions (kmalloc, kmem_cache_alloc)
2371 * have the fastpath folded into their functions. So no function call
2372 * overhead for requests that can be satisfied on the fastpath.
2373 *
2374 * The fastpath works by first checking if the lockless freelist can be used.
2375 * If not then __slab_alloc is called for slow processing.
2376 *
2377 * Otherwise we can simply pick the next object from the lockless free list.
2378 */
Ezequiel Garcia2b847c32012-09-08 17:47:58 -03002379static __always_inline void *slab_alloc_node(struct kmem_cache *s,
Eduard - Gabriel Munteanuce71e272008-08-19 20:43:25 +03002380 gfp_t gfpflags, int node, unsigned long addr)
Christoph Lameter894b8782007-05-10 03:15:16 -07002381{
Christoph Lameter894b8782007-05-10 03:15:16 -07002382 void **object;
Christoph Lameterdfb4f092007-10-16 01:26:05 -07002383 struct kmem_cache_cpu *c;
Christoph Lameter57d437d2012-05-09 10:09:59 -05002384 struct page *page;
Christoph Lameter8a5ec0b2011-02-25 11:38:54 -06002385 unsigned long tid;
Christoph Lameter1f842602008-01-07 23:20:30 -08002386
Christoph Lameterc016b0b2010-08-20 12:37:16 -05002387 if (slab_pre_alloc_hook(s, gfpflags))
Akinobu Mita773ff602008-12-23 19:37:01 +09002388 return NULL;
2389
Glauber Costad79923f2012-12-18 14:22:48 -08002390 s = memcg_kmem_get_cache(s, gfpflags);
Christoph Lameter8a5ec0b2011-02-25 11:38:54 -06002391redo:
Christoph Lameter8a5ec0b2011-02-25 11:38:54 -06002392 /*
2393 * Must read kmem_cache cpu data via this cpu ptr. Preemption is
2394 * enabled. We may switch back and forth between cpus while
2395 * reading from one cpu area. That does not matter as long
2396 * as we end up on the original cpu again when doing the cmpxchg.
Christoph Lameter7cccd802013-01-23 21:45:48 +00002397 *
2398 * Preemption is disabled for the retrieval of the tid because that
2399 * must occur from the current processor. We cannot allow rescheduling
2400 * on a different processor between the determination of the pointer
2401 * and the retrieval of the tid.
Christoph Lameter8a5ec0b2011-02-25 11:38:54 -06002402 */
Christoph Lameter7cccd802013-01-23 21:45:48 +00002403 preempt_disable();
Christoph Lameter9dfc6e62009-12-18 16:26:20 -06002404 c = __this_cpu_ptr(s->cpu_slab);
Christoph Lameter8a5ec0b2011-02-25 11:38:54 -06002405
Christoph Lameter8a5ec0b2011-02-25 11:38:54 -06002406 /*
2407 * The transaction ids are globally unique per cpu and per operation on
2408 * a per cpu queue. Thus they can be guarantee that the cmpxchg_double
2409 * occurs on the right processor and that there was no operation on the
2410 * linked list in between.
2411 */
2412 tid = c->tid;
Christoph Lameter7cccd802013-01-23 21:45:48 +00002413 preempt_enable();
Christoph Lameter8a5ec0b2011-02-25 11:38:54 -06002414
Christoph Lameter9dfc6e62009-12-18 16:26:20 -06002415 object = c->freelist;
Christoph Lameter57d437d2012-05-09 10:09:59 -05002416 page = c->page;
Christoph Lameter5091b742012-07-31 16:44:00 -07002417 if (unlikely(!object || !node_match(page, node)))
Christoph Lameterdfb4f092007-10-16 01:26:05 -07002418 object = __slab_alloc(s, gfpflags, node, addr, c);
Christoph Lameter894b8782007-05-10 03:15:16 -07002419
2420 else {
Eric Dumazet0ad95002011-12-16 16:25:34 +01002421 void *next_object = get_freepointer_safe(s, object);
2422
Christoph Lameter8a5ec0b2011-02-25 11:38:54 -06002423 /*
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002424 * The cmpxchg will only match if there was no additional
Christoph Lameter8a5ec0b2011-02-25 11:38:54 -06002425 * operation and if we are on the right processor.
2426 *
2427 * The cmpxchg does the following atomically (without lock semantics!)
2428 * 1. Relocate first pointer to the current per cpu area.
2429 * 2. Verify that tid and freelist have not been changed
2430 * 3. If they were not changed replace tid and freelist
2431 *
2432 * Since this is without lock semantics the protection is only against
2433 * code executing on this cpu *not* from access by other cpus.
2434 */
Christoph Lameter933393f2011-12-22 11:58:51 -06002435 if (unlikely(!this_cpu_cmpxchg_double(
Christoph Lameter8a5ec0b2011-02-25 11:38:54 -06002436 s->cpu_slab->freelist, s->cpu_slab->tid,
2437 object, tid,
Eric Dumazet0ad95002011-12-16 16:25:34 +01002438 next_object, next_tid(tid)))) {
Christoph Lameter8a5ec0b2011-02-25 11:38:54 -06002439
2440 note_cmpxchg_failure("slab_alloc", s, tid);
2441 goto redo;
2442 }
Eric Dumazet0ad95002011-12-16 16:25:34 +01002443 prefetch_freepointer(s, next_object);
Christoph Lameter84e554e62009-12-18 16:26:23 -06002444 stat(s, ALLOC_FASTPATH);
Christoph Lameter894b8782007-05-10 03:15:16 -07002445 }
Christoph Lameter8a5ec0b2011-02-25 11:38:54 -06002446
Pekka Enberg74e21342009-11-25 20:14:48 +02002447 if (unlikely(gfpflags & __GFP_ZERO) && object)
Christoph Lameter3b0efdf2012-06-13 10:24:57 -05002448 memset(object, 0, s->object_size);
Sebastian Andrzej Siewior2c12a532013-11-13 11:21:04 -05002449#ifdef CONFIG_PREEMPT_RT_FULL
2450 if (unlikely(s->ctor) && object)
2451 s->ctor(object);
2452#endif
Christoph Lameterd07dbea2007-07-17 04:03:23 -07002453
Christoph Lameterc016b0b2010-08-20 12:37:16 -05002454 slab_post_alloc_hook(s, gfpflags, object);
Vegard Nossum5a896d92008-04-04 00:54:48 +02002455
Christoph Lameter894b8782007-05-10 03:15:16 -07002456 return object;
Christoph Lameter81819f02007-05-06 14:49:36 -07002457}
2458
Ezequiel Garcia2b847c32012-09-08 17:47:58 -03002459static __always_inline void *slab_alloc(struct kmem_cache *s,
2460 gfp_t gfpflags, unsigned long addr)
2461{
2462 return slab_alloc_node(s, gfpflags, NUMA_NO_NODE, addr);
2463}
2464
Christoph Lameter81819f02007-05-06 14:49:36 -07002465void *kmem_cache_alloc(struct kmem_cache *s, gfp_t gfpflags)
2466{
Ezequiel Garcia2b847c32012-09-08 17:47:58 -03002467 void *ret = slab_alloc(s, gfpflags, _RET_IP_);
Eduard - Gabriel Munteanu5b882be2008-08-19 20:43:26 +03002468
Christoph Lameter3b0efdf2012-06-13 10:24:57 -05002469 trace_kmem_cache_alloc(_RET_IP_, ret, s->object_size, s->size, gfpflags);
Eduard - Gabriel Munteanu5b882be2008-08-19 20:43:26 +03002470
2471 return ret;
Christoph Lameter81819f02007-05-06 14:49:36 -07002472}
2473EXPORT_SYMBOL(kmem_cache_alloc);
2474
Li Zefan0f24f122009-12-11 15:45:30 +08002475#ifdef CONFIG_TRACING
Richard Kennedy4a923792010-10-21 10:29:19 +01002476void *kmem_cache_alloc_trace(struct kmem_cache *s, gfp_t gfpflags, size_t size)
Eduard - Gabriel Munteanu5b882be2008-08-19 20:43:26 +03002477{
Ezequiel Garcia2b847c32012-09-08 17:47:58 -03002478 void *ret = slab_alloc(s, gfpflags, _RET_IP_);
Richard Kennedy4a923792010-10-21 10:29:19 +01002479 trace_kmalloc(_RET_IP_, ret, size, s->size, gfpflags);
2480 return ret;
Eduard - Gabriel Munteanu5b882be2008-08-19 20:43:26 +03002481}
Richard Kennedy4a923792010-10-21 10:29:19 +01002482EXPORT_SYMBOL(kmem_cache_alloc_trace);
2483
2484void *kmalloc_order_trace(size_t size, gfp_t flags, unsigned int order)
2485{
2486 void *ret = kmalloc_order(size, flags, order);
2487 trace_kmalloc(_RET_IP_, ret, size, PAGE_SIZE << order, flags);
2488 return ret;
2489}
2490EXPORT_SYMBOL(kmalloc_order_trace);
Eduard - Gabriel Munteanu5b882be2008-08-19 20:43:26 +03002491#endif
2492
Christoph Lameter81819f02007-05-06 14:49:36 -07002493#ifdef CONFIG_NUMA
2494void *kmem_cache_alloc_node(struct kmem_cache *s, gfp_t gfpflags, int node)
2495{
Ezequiel Garcia2b847c32012-09-08 17:47:58 -03002496 void *ret = slab_alloc_node(s, gfpflags, node, _RET_IP_);
Eduard - Gabriel Munteanu5b882be2008-08-19 20:43:26 +03002497
Eduard - Gabriel Munteanuca2b84cb2009-03-23 15:12:24 +02002498 trace_kmem_cache_alloc_node(_RET_IP_, ret,
Christoph Lameter3b0efdf2012-06-13 10:24:57 -05002499 s->object_size, s->size, gfpflags, node);
Eduard - Gabriel Munteanu5b882be2008-08-19 20:43:26 +03002500
2501 return ret;
Christoph Lameter81819f02007-05-06 14:49:36 -07002502}
2503EXPORT_SYMBOL(kmem_cache_alloc_node);
Christoph Lameter81819f02007-05-06 14:49:36 -07002504
Li Zefan0f24f122009-12-11 15:45:30 +08002505#ifdef CONFIG_TRACING
Richard Kennedy4a923792010-10-21 10:29:19 +01002506void *kmem_cache_alloc_node_trace(struct kmem_cache *s,
Eduard - Gabriel Munteanu5b882be2008-08-19 20:43:26 +03002507 gfp_t gfpflags,
Richard Kennedy4a923792010-10-21 10:29:19 +01002508 int node, size_t size)
Eduard - Gabriel Munteanu5b882be2008-08-19 20:43:26 +03002509{
Ezequiel Garcia2b847c32012-09-08 17:47:58 -03002510 void *ret = slab_alloc_node(s, gfpflags, node, _RET_IP_);
Richard Kennedy4a923792010-10-21 10:29:19 +01002511
2512 trace_kmalloc_node(_RET_IP_, ret,
2513 size, s->size, gfpflags, node);
2514 return ret;
Eduard - Gabriel Munteanu5b882be2008-08-19 20:43:26 +03002515}
Richard Kennedy4a923792010-10-21 10:29:19 +01002516EXPORT_SYMBOL(kmem_cache_alloc_node_trace);
Eduard - Gabriel Munteanu5b882be2008-08-19 20:43:26 +03002517#endif
Namhyung Kim5d1f57e2010-09-29 21:02:15 +09002518#endif
Eduard - Gabriel Munteanu5b882be2008-08-19 20:43:26 +03002519
Christoph Lameter81819f02007-05-06 14:49:36 -07002520/*
Christoph Lameter894b8782007-05-10 03:15:16 -07002521 * Slow patch handling. This may still be called frequently since objects
2522 * have a longer lifetime than the cpu slabs in most processing loads.
Christoph Lameter81819f02007-05-06 14:49:36 -07002523 *
Christoph Lameter894b8782007-05-10 03:15:16 -07002524 * So we still attempt to reduce cache line usage. Just take the slab
2525 * lock and free the item. If there is no additional partial page
2526 * handling required then we can return immediately.
Christoph Lameter81819f02007-05-06 14:49:36 -07002527 */
Christoph Lameter894b8782007-05-10 03:15:16 -07002528static void __slab_free(struct kmem_cache *s, struct page *page,
Christoph Lameterff120592009-12-18 16:26:22 -06002529 void *x, unsigned long addr)
Christoph Lameter81819f02007-05-06 14:49:36 -07002530{
2531 void *prior;
2532 void **object = (void *)x;
Christoph Lameter2cfb7452011-06-01 12:25:52 -05002533 int was_frozen;
Christoph Lameter2cfb7452011-06-01 12:25:52 -05002534 struct page new;
2535 unsigned long counters;
2536 struct kmem_cache_node *n = NULL;
Christoph Lameter61728d12011-06-01 12:25:51 -05002537 unsigned long uninitialized_var(flags);
Christoph Lameter81819f02007-05-06 14:49:36 -07002538
Christoph Lameter8a5ec0b2011-02-25 11:38:54 -06002539 stat(s, FREE_SLOWPATH);
Christoph Lameter81819f02007-05-06 14:49:36 -07002540
Christoph Lameter19c7ff92012-05-30 12:54:46 -05002541 if (kmem_cache_debug(s) &&
2542 !(n = free_debug_processing(s, page, x, addr, &flags)))
Christoph Lameter80f08c12011-06-01 12:25:55 -05002543 return;
Christoph Lameter6446faa2008-02-15 23:45:26 -08002544
Christoph Lameter2cfb7452011-06-01 12:25:52 -05002545 do {
Joonsoo Kim837d6782012-08-16 00:02:40 +09002546 if (unlikely(n)) {
Thomas Gleixner57be9db2012-10-25 10:32:35 +01002547 raw_spin_unlock_irqrestore(&n->list_lock, flags);
Joonsoo Kim837d6782012-08-16 00:02:40 +09002548 n = NULL;
2549 }
Christoph Lameter2cfb7452011-06-01 12:25:52 -05002550 prior = page->freelist;
2551 counters = page->counters;
2552 set_freepointer(s, object, prior);
2553 new.counters = counters;
2554 was_frozen = new.frozen;
2555 new.inuse--;
Joonsoo Kim837d6782012-08-16 00:02:40 +09002556 if ((!new.inuse || !prior) && !was_frozen) {
Christoph Lameter49e22582011-08-09 16:12:27 -05002557
2558 if (!kmem_cache_debug(s) && !prior)
2559
2560 /*
2561 * Slab was on no list before and will be partially empty
2562 * We can defer the list move and instead freeze it.
2563 */
2564 new.frozen = 1;
2565
2566 else { /* Needs to be taken off a list */
2567
2568 n = get_node(s, page_to_nid(page));
2569 /*
2570 * Speculatively acquire the list_lock.
2571 * If the cmpxchg does not succeed then we may
2572 * drop the list_lock without any processing.
2573 *
2574 * Otherwise the list_lock will synchronize with
2575 * other processors updating the list of slabs.
2576 */
Thomas Gleixner57be9db2012-10-25 10:32:35 +01002577 raw_spin_lock_irqsave(&n->list_lock, flags);
Christoph Lameter49e22582011-08-09 16:12:27 -05002578
2579 }
Christoph Lameter2cfb7452011-06-01 12:25:52 -05002580 }
Christoph Lameter81819f02007-05-06 14:49:36 -07002581
Christoph Lameter2cfb7452011-06-01 12:25:52 -05002582 } while (!cmpxchg_double_slab(s, page,
2583 prior, counters,
2584 object, new.counters,
2585 "__slab_free"));
Christoph Lameter81819f02007-05-06 14:49:36 -07002586
Christoph Lameter2cfb7452011-06-01 12:25:52 -05002587 if (likely(!n)) {
Christoph Lameter49e22582011-08-09 16:12:27 -05002588
2589 /*
2590 * If we just froze the page then put it onto the
2591 * per cpu partial list.
2592 */
Alex Shi8028dce2012-02-03 23:34:56 +08002593 if (new.frozen && !was_frozen) {
Christoph Lameter49e22582011-08-09 16:12:27 -05002594 put_cpu_partial(s, page, 1);
Alex Shi8028dce2012-02-03 23:34:56 +08002595 stat(s, CPU_PARTIAL_FREE);
2596 }
Christoph Lameter49e22582011-08-09 16:12:27 -05002597 /*
Christoph Lameter2cfb7452011-06-01 12:25:52 -05002598 * The list lock was not taken therefore no list
2599 * activity can be necessary.
2600 */
2601 if (was_frozen)
2602 stat(s, FREE_FROZEN);
Christoph Lameter80f08c12011-06-01 12:25:55 -05002603 return;
Christoph Lameter2cfb7452011-06-01 12:25:52 -05002604 }
Christoph Lameter81819f02007-05-06 14:49:36 -07002605
Joonsoo Kim837d6782012-08-16 00:02:40 +09002606 if (unlikely(!new.inuse && n->nr_partial > s->min_partial))
2607 goto slab_empty;
Christoph Lameter81819f02007-05-06 14:49:36 -07002608
Joonsoo Kim837d6782012-08-16 00:02:40 +09002609 /*
2610 * Objects left in the slab. If it was not on the partial list before
2611 * then add it.
2612 */
2613 if (kmem_cache_debug(s) && unlikely(!prior)) {
2614 remove_full(s, page);
2615 add_partial(n, page, DEACTIVATE_TO_TAIL);
2616 stat(s, FREE_ADD_PARTIAL);
Christoph Lameter81819f02007-05-06 14:49:36 -07002617 }
Thomas Gleixner57be9db2012-10-25 10:32:35 +01002618 raw_spin_unlock_irqrestore(&n->list_lock, flags);
Christoph Lameter81819f02007-05-06 14:49:36 -07002619 return;
2620
2621slab_empty:
Christoph Lametera973e9d2008-03-01 13:40:44 -08002622 if (prior) {
Christoph Lameter81819f02007-05-06 14:49:36 -07002623 /*
Christoph Lameter6fbabb22011-08-08 11:16:56 -05002624 * Slab on the partial list.
Christoph Lameter81819f02007-05-06 14:49:36 -07002625 */
Christoph Lameter5cc6eee2011-06-01 12:25:50 -05002626 remove_partial(n, page);
Christoph Lameter84e554e62009-12-18 16:26:23 -06002627 stat(s, FREE_REMOVE_PARTIAL);
Christoph Lameter6fbabb22011-08-08 11:16:56 -05002628 } else
2629 /* Slab must be on the full list */
2630 remove_full(s, page);
Christoph Lameter2cfb7452011-06-01 12:25:52 -05002631
Thomas Gleixner57be9db2012-10-25 10:32:35 +01002632 raw_spin_unlock_irqrestore(&n->list_lock, flags);
Christoph Lameter84e554e62009-12-18 16:26:23 -06002633 stat(s, FREE_SLAB);
Christoph Lameter81819f02007-05-06 14:49:36 -07002634 discard_slab(s, page);
Christoph Lameter81819f02007-05-06 14:49:36 -07002635}
2636
Christoph Lameter894b8782007-05-10 03:15:16 -07002637/*
2638 * Fastpath with forced inlining to produce a kfree and kmem_cache_free that
2639 * can perform fastpath freeing without additional function calls.
2640 *
2641 * The fastpath is only possible if we are freeing to the current cpu slab
2642 * of this processor. This typically the case if we have just allocated
2643 * the item before.
2644 *
2645 * If fastpath is not possible then fall back to __slab_free where we deal
2646 * with all sorts of special processing.
2647 */
Pekka Enberg06428782008-01-07 23:20:27 -08002648static __always_inline void slab_free(struct kmem_cache *s,
Eduard - Gabriel Munteanuce71e272008-08-19 20:43:25 +03002649 struct page *page, void *x, unsigned long addr)
Christoph Lameter894b8782007-05-10 03:15:16 -07002650{
2651 void **object = (void *)x;
Christoph Lameterdfb4f092007-10-16 01:26:05 -07002652 struct kmem_cache_cpu *c;
Christoph Lameter8a5ec0b2011-02-25 11:38:54 -06002653 unsigned long tid;
Christoph Lameter1f842602008-01-07 23:20:30 -08002654
Christoph Lameterc016b0b2010-08-20 12:37:16 -05002655 slab_free_hook(s, x);
2656
Christoph Lametera24c5a02011-03-15 12:45:21 -05002657redo:
Christoph Lameter8a5ec0b2011-02-25 11:38:54 -06002658 /*
2659 * Determine the currently cpus per cpu slab.
2660 * The cpu may change afterward. However that does not matter since
2661 * data is retrieved via this pointer. If we are on the same cpu
2662 * during the cmpxchg then the free will succedd.
2663 */
Christoph Lameter7cccd802013-01-23 21:45:48 +00002664 preempt_disable();
Christoph Lameter9dfc6e62009-12-18 16:26:20 -06002665 c = __this_cpu_ptr(s->cpu_slab);
Christoph Lameterc016b0b2010-08-20 12:37:16 -05002666
Christoph Lameter8a5ec0b2011-02-25 11:38:54 -06002667 tid = c->tid;
Christoph Lameter7cccd802013-01-23 21:45:48 +00002668 preempt_enable();
Christoph Lameterc016b0b2010-08-20 12:37:16 -05002669
Christoph Lameter442b06b2011-05-17 16:29:31 -05002670 if (likely(page == c->page)) {
Christoph Lameterff120592009-12-18 16:26:22 -06002671 set_freepointer(s, object, c->freelist);
Christoph Lameter8a5ec0b2011-02-25 11:38:54 -06002672
Christoph Lameter933393f2011-12-22 11:58:51 -06002673 if (unlikely(!this_cpu_cmpxchg_double(
Christoph Lameter8a5ec0b2011-02-25 11:38:54 -06002674 s->cpu_slab->freelist, s->cpu_slab->tid,
2675 c->freelist, tid,
2676 object, next_tid(tid)))) {
2677
2678 note_cmpxchg_failure("slab_free", s, tid);
2679 goto redo;
2680 }
Christoph Lameter84e554e62009-12-18 16:26:23 -06002681 stat(s, FREE_FASTPATH);
Christoph Lameter894b8782007-05-10 03:15:16 -07002682 } else
Christoph Lameterff120592009-12-18 16:26:22 -06002683 __slab_free(s, page, x, addr);
Christoph Lameter894b8782007-05-10 03:15:16 -07002684
Christoph Lameter894b8782007-05-10 03:15:16 -07002685}
2686
Christoph Lameter81819f02007-05-06 14:49:36 -07002687void kmem_cache_free(struct kmem_cache *s, void *x)
2688{
Glauber Costab9ce5ef2012-12-18 14:22:46 -08002689 s = cache_from_obj(s, x);
2690 if (!s)
Christoph Lameter79576102012-09-04 23:06:14 +00002691 return;
Glauber Costab9ce5ef2012-12-18 14:22:46 -08002692 slab_free(s, virt_to_head_page(x), x, _RET_IP_);
Eduard - Gabriel Munteanuca2b84cb2009-03-23 15:12:24 +02002693 trace_kmem_cache_free(_RET_IP_, x);
Christoph Lameter81819f02007-05-06 14:49:36 -07002694}
2695EXPORT_SYMBOL(kmem_cache_free);
2696
Christoph Lameter81819f02007-05-06 14:49:36 -07002697/*
Christoph Lameter672bba32007-05-09 02:32:39 -07002698 * Object placement in a slab is made very easy because we always start at
2699 * offset 0. If we tune the size of the object to the alignment then we can
2700 * get the required alignment by putting one properly sized object after
2701 * another.
Christoph Lameter81819f02007-05-06 14:49:36 -07002702 *
2703 * Notice that the allocation order determines the sizes of the per cpu
2704 * caches. Each processor has always one slab available for allocations.
2705 * Increasing the allocation order reduces the number of times that slabs
Christoph Lameter672bba32007-05-09 02:32:39 -07002706 * must be moved on and off the partial lists and is therefore a factor in
Christoph Lameter81819f02007-05-06 14:49:36 -07002707 * locking overhead.
Christoph Lameter81819f02007-05-06 14:49:36 -07002708 */
2709
2710/*
2711 * Mininum / Maximum order of slab pages. This influences locking overhead
2712 * and slab fragmentation. A higher order reduces the number of partial slabs
2713 * and increases the number of allocations possible without having to
2714 * take the list_lock.
2715 */
2716static int slub_min_order;
Christoph Lameter114e9e82008-04-14 19:11:41 +03002717static int slub_max_order = PAGE_ALLOC_COSTLY_ORDER;
Christoph Lameter9b2cd502008-04-14 19:11:41 +03002718static int slub_min_objects;
Christoph Lameter81819f02007-05-06 14:49:36 -07002719
2720/*
2721 * Merge control. If this is set then no merging of slab caches will occur.
Christoph Lameter672bba32007-05-09 02:32:39 -07002722 * (Could be removed. This was introduced to pacify the merge skeptics.)
Christoph Lameter81819f02007-05-06 14:49:36 -07002723 */
2724static int slub_nomerge;
2725
2726/*
Christoph Lameter81819f02007-05-06 14:49:36 -07002727 * Calculate the order of allocation given an slab object size.
2728 *
Christoph Lameter672bba32007-05-09 02:32:39 -07002729 * The order of allocation has significant impact on performance and other
2730 * system components. Generally order 0 allocations should be preferred since
2731 * order 0 does not cause fragmentation in the page allocator. Larger objects
2732 * be problematic to put into order 0 slabs because there may be too much
Christoph Lameterc124f5b2008-04-14 19:13:29 +03002733 * unused space left. We go to a higher order if more than 1/16th of the slab
Christoph Lameter672bba32007-05-09 02:32:39 -07002734 * would be wasted.
Christoph Lameter81819f02007-05-06 14:49:36 -07002735 *
Christoph Lameter672bba32007-05-09 02:32:39 -07002736 * In order to reach satisfactory performance we must ensure that a minimum
2737 * number of objects is in one slab. Otherwise we may generate too much
2738 * activity on the partial lists which requires taking the list_lock. This is
2739 * less a concern for large slabs though which are rarely used.
Christoph Lameter81819f02007-05-06 14:49:36 -07002740 *
Christoph Lameter672bba32007-05-09 02:32:39 -07002741 * slub_max_order specifies the order where we begin to stop considering the
2742 * number of objects in a slab as critical. If we reach slub_max_order then
2743 * we try to keep the page order as low as possible. So we accept more waste
2744 * of space in favor of a small page order.
2745 *
2746 * Higher order allocations also allow the placement of more objects in a
2747 * slab and thereby reduce object handling overhead. If the user has
2748 * requested a higher mininum order then we start with that one instead of
2749 * the smallest order which will fit the object.
Christoph Lameter81819f02007-05-06 14:49:36 -07002750 */
Christoph Lameter5e6d4442007-05-09 02:32:46 -07002751static inline int slab_order(int size, int min_objects,
Lai Jiangshanab9a0f12011-03-10 15:21:48 +08002752 int max_order, int fract_leftover, int reserved)
Christoph Lameter81819f02007-05-06 14:49:36 -07002753{
2754 int order;
2755 int rem;
Christoph Lameter6300ea72007-07-17 04:03:20 -07002756 int min_order = slub_min_order;
Christoph Lameter81819f02007-05-06 14:49:36 -07002757
Lai Jiangshanab9a0f12011-03-10 15:21:48 +08002758 if (order_objects(min_order, size, reserved) > MAX_OBJS_PER_PAGE)
Cyrill Gorcunov210b5c02008-10-22 23:00:38 +04002759 return get_order(size * MAX_OBJS_PER_PAGE) - 1;
Christoph Lameter39b26462008-04-14 19:11:30 +03002760
Christoph Lameter6300ea72007-07-17 04:03:20 -07002761 for (order = max(min_order,
Christoph Lameter5e6d4442007-05-09 02:32:46 -07002762 fls(min_objects * size - 1) - PAGE_SHIFT);
2763 order <= max_order; order++) {
2764
Christoph Lameter81819f02007-05-06 14:49:36 -07002765 unsigned long slab_size = PAGE_SIZE << order;
2766
Lai Jiangshanab9a0f12011-03-10 15:21:48 +08002767 if (slab_size < min_objects * size + reserved)
Christoph Lameter81819f02007-05-06 14:49:36 -07002768 continue;
2769
Lai Jiangshanab9a0f12011-03-10 15:21:48 +08002770 rem = (slab_size - reserved) % size;
Christoph Lameter81819f02007-05-06 14:49:36 -07002771
Christoph Lameter5e6d4442007-05-09 02:32:46 -07002772 if (rem <= slab_size / fract_leftover)
Christoph Lameter81819f02007-05-06 14:49:36 -07002773 break;
2774
2775 }
Christoph Lameter672bba32007-05-09 02:32:39 -07002776
Christoph Lameter81819f02007-05-06 14:49:36 -07002777 return order;
2778}
2779
Lai Jiangshanab9a0f12011-03-10 15:21:48 +08002780static inline int calculate_order(int size, int reserved)
Christoph Lameter5e6d4442007-05-09 02:32:46 -07002781{
2782 int order;
2783 int min_objects;
2784 int fraction;
Zhang Yanmine8120ff2009-02-12 18:00:17 +02002785 int max_objects;
Christoph Lameter5e6d4442007-05-09 02:32:46 -07002786
2787 /*
2788 * Attempt to find best configuration for a slab. This
2789 * works by first attempting to generate a layout with
2790 * the best configuration and backing off gradually.
2791 *
2792 * First we reduce the acceptable waste in a slab. Then
2793 * we reduce the minimum objects required in a slab.
2794 */
2795 min_objects = slub_min_objects;
Christoph Lameter9b2cd502008-04-14 19:11:41 +03002796 if (!min_objects)
2797 min_objects = 4 * (fls(nr_cpu_ids) + 1);
Lai Jiangshanab9a0f12011-03-10 15:21:48 +08002798 max_objects = order_objects(slub_max_order, size, reserved);
Zhang Yanmine8120ff2009-02-12 18:00:17 +02002799 min_objects = min(min_objects, max_objects);
2800
Christoph Lameter5e6d4442007-05-09 02:32:46 -07002801 while (min_objects > 1) {
Christoph Lameterc124f5b2008-04-14 19:13:29 +03002802 fraction = 16;
Christoph Lameter5e6d4442007-05-09 02:32:46 -07002803 while (fraction >= 4) {
2804 order = slab_order(size, min_objects,
Lai Jiangshanab9a0f12011-03-10 15:21:48 +08002805 slub_max_order, fraction, reserved);
Christoph Lameter5e6d4442007-05-09 02:32:46 -07002806 if (order <= slub_max_order)
2807 return order;
2808 fraction /= 2;
2809 }
Amerigo Wang5086c389c2009-08-19 21:44:13 +03002810 min_objects--;
Christoph Lameter5e6d4442007-05-09 02:32:46 -07002811 }
2812
2813 /*
2814 * We were unable to place multiple objects in a slab. Now
2815 * lets see if we can place a single object there.
2816 */
Lai Jiangshanab9a0f12011-03-10 15:21:48 +08002817 order = slab_order(size, 1, slub_max_order, 1, reserved);
Christoph Lameter5e6d4442007-05-09 02:32:46 -07002818 if (order <= slub_max_order)
2819 return order;
2820
2821 /*
2822 * Doh this slab cannot be placed using slub_max_order.
2823 */
Lai Jiangshanab9a0f12011-03-10 15:21:48 +08002824 order = slab_order(size, 1, MAX_ORDER, 1, reserved);
David Rientjes818cf592009-04-23 09:58:22 +03002825 if (order < MAX_ORDER)
Christoph Lameter5e6d4442007-05-09 02:32:46 -07002826 return order;
2827 return -ENOSYS;
2828}
2829
Pekka Enberg5595cff2008-08-05 09:28:47 +03002830static void
Joonsoo Kim40534972012-05-11 00:50:47 +09002831init_kmem_cache_node(struct kmem_cache_node *n)
Christoph Lameter81819f02007-05-06 14:49:36 -07002832{
2833 n->nr_partial = 0;
Thomas Gleixner57be9db2012-10-25 10:32:35 +01002834 raw_spin_lock_init(&n->list_lock);
Christoph Lameter81819f02007-05-06 14:49:36 -07002835 INIT_LIST_HEAD(&n->partial);
Christoph Lameter8ab13722007-07-17 04:03:32 -07002836#ifdef CONFIG_SLUB_DEBUG
Christoph Lameter0f389ec2008-04-14 18:53:02 +03002837 atomic_long_set(&n->nr_slabs, 0);
Salman Qazi02b71b72008-09-11 12:25:41 -07002838 atomic_long_set(&n->total_objects, 0);
Christoph Lameter643b1132007-05-06 14:49:42 -07002839 INIT_LIST_HEAD(&n->full);
Christoph Lameter8ab13722007-07-17 04:03:32 -07002840#endif
Christoph Lameter81819f02007-05-06 14:49:36 -07002841}
2842
Christoph Lameter55136592010-08-20 12:37:13 -05002843static inline int alloc_kmem_cache_cpus(struct kmem_cache *s)
Christoph Lameter4c93c3552007-10-16 01:26:08 -07002844{
Christoph Lameter6c182dc2010-08-20 12:37:14 -05002845 BUILD_BUG_ON(PERCPU_DYNAMIC_EARLY_SIZE <
Christoph Lameter95a05b42013-01-10 19:14:19 +00002846 KMALLOC_SHIFT_HIGH * sizeof(struct kmem_cache_cpu));
Christoph Lameter9dfc6e62009-12-18 16:26:20 -06002847
Christoph Lameter8a5ec0b2011-02-25 11:38:54 -06002848 /*
Chris Metcalfd4d84fe2011-06-02 10:19:41 -04002849 * Must align to double word boundary for the double cmpxchg
2850 * instructions to work; see __pcpu_double_call_return_bool().
Christoph Lameter8a5ec0b2011-02-25 11:38:54 -06002851 */
Chris Metcalfd4d84fe2011-06-02 10:19:41 -04002852 s->cpu_slab = __alloc_percpu(sizeof(struct kmem_cache_cpu),
2853 2 * sizeof(void *));
Christoph Lameter9dfc6e62009-12-18 16:26:20 -06002854
Christoph Lameter8a5ec0b2011-02-25 11:38:54 -06002855 if (!s->cpu_slab)
2856 return 0;
2857
2858 init_kmem_cache_cpus(s);
2859
2860 return 1;
Christoph Lameter4c93c3552007-10-16 01:26:08 -07002861}
Christoph Lameter4c93c3552007-10-16 01:26:08 -07002862
Christoph Lameter51df1142010-08-20 12:37:15 -05002863static struct kmem_cache *kmem_cache_node;
2864
Christoph Lameter81819f02007-05-06 14:49:36 -07002865/*
2866 * No kmalloc_node yet so do it by hand. We know that this is the first
2867 * slab on the node for this slabcache. There are no concurrent accesses
2868 * possible.
2869 *
2870 * Note that this function only works on the kmalloc_node_cache
Christoph Lameter4c93c3552007-10-16 01:26:08 -07002871 * when allocating for the kmalloc_node_cache. This is used for bootstrapping
2872 * memory on a fresh node that has no slab structures yet.
Christoph Lameter81819f02007-05-06 14:49:36 -07002873 */
Christoph Lameter55136592010-08-20 12:37:13 -05002874static void early_kmem_cache_node_alloc(int node)
Christoph Lameter81819f02007-05-06 14:49:36 -07002875{
2876 struct page *page;
2877 struct kmem_cache_node *n;
2878
Christoph Lameter51df1142010-08-20 12:37:15 -05002879 BUG_ON(kmem_cache_node->size < sizeof(struct kmem_cache_node));
Christoph Lameter81819f02007-05-06 14:49:36 -07002880
Christoph Lameter51df1142010-08-20 12:37:15 -05002881 page = new_slab(kmem_cache_node, GFP_NOWAIT, node);
Christoph Lameter81819f02007-05-06 14:49:36 -07002882
2883 BUG_ON(!page);
Christoph Lametera2f92ee2007-08-22 14:01:57 -07002884 if (page_to_nid(page) != node) {
2885 printk(KERN_ERR "SLUB: Unable to allocate memory from "
2886 "node %d\n", node);
2887 printk(KERN_ERR "SLUB: Allocating a useless per node structure "
2888 "in order to be able to continue\n");
2889 }
2890
Christoph Lameter81819f02007-05-06 14:49:36 -07002891 n = page->freelist;
2892 BUG_ON(!n);
Christoph Lameter51df1142010-08-20 12:37:15 -05002893 page->freelist = get_freepointer(kmem_cache_node, n);
Christoph Lametere6e82ea2011-08-09 16:12:24 -05002894 page->inuse = 1;
Christoph Lameter8cb0a502011-06-01 12:25:46 -05002895 page->frozen = 0;
Christoph Lameter51df1142010-08-20 12:37:15 -05002896 kmem_cache_node->node[node] = n;
Christoph Lameter8ab13722007-07-17 04:03:32 -07002897#ifdef CONFIG_SLUB_DEBUG
Christoph Lameterf7cb1932010-09-29 07:15:01 -05002898 init_object(kmem_cache_node, n, SLUB_RED_ACTIVE);
Christoph Lameter51df1142010-08-20 12:37:15 -05002899 init_tracking(kmem_cache_node, n);
Christoph Lameter8ab13722007-07-17 04:03:32 -07002900#endif
Joonsoo Kim40534972012-05-11 00:50:47 +09002901 init_kmem_cache_node(n);
Christoph Lameter51df1142010-08-20 12:37:15 -05002902 inc_slabs_node(kmem_cache_node, node, page->objects);
Christoph Lameter6446faa2008-02-15 23:45:26 -08002903
Shaohua Li136333d2011-08-24 08:57:52 +08002904 add_partial(n, page, DEACTIVATE_TO_HEAD);
Christoph Lameter81819f02007-05-06 14:49:36 -07002905}
2906
2907static void free_kmem_cache_nodes(struct kmem_cache *s)
2908{
2909 int node;
2910
Christoph Lameterf64dc582007-10-16 01:25:33 -07002911 for_each_node_state(node, N_NORMAL_MEMORY) {
Christoph Lameter81819f02007-05-06 14:49:36 -07002912 struct kmem_cache_node *n = s->node[node];
Christoph Lameter51df1142010-08-20 12:37:15 -05002913
Alexander Duyck73367bd2010-05-21 14:41:35 -07002914 if (n)
Christoph Lameter51df1142010-08-20 12:37:15 -05002915 kmem_cache_free(kmem_cache_node, n);
2916
Christoph Lameter81819f02007-05-06 14:49:36 -07002917 s->node[node] = NULL;
2918 }
2919}
2920
Christoph Lameter55136592010-08-20 12:37:13 -05002921static int init_kmem_cache_nodes(struct kmem_cache *s)
Christoph Lameter81819f02007-05-06 14:49:36 -07002922{
2923 int node;
Christoph Lameter81819f02007-05-06 14:49:36 -07002924
Christoph Lameterf64dc582007-10-16 01:25:33 -07002925 for_each_node_state(node, N_NORMAL_MEMORY) {
Christoph Lameter81819f02007-05-06 14:49:36 -07002926 struct kmem_cache_node *n;
2927
Alexander Duyck73367bd2010-05-21 14:41:35 -07002928 if (slab_state == DOWN) {
Christoph Lameter55136592010-08-20 12:37:13 -05002929 early_kmem_cache_node_alloc(node);
Alexander Duyck73367bd2010-05-21 14:41:35 -07002930 continue;
Christoph Lameter81819f02007-05-06 14:49:36 -07002931 }
Christoph Lameter51df1142010-08-20 12:37:15 -05002932 n = kmem_cache_alloc_node(kmem_cache_node,
Christoph Lameter55136592010-08-20 12:37:13 -05002933 GFP_KERNEL, node);
Alexander Duyck73367bd2010-05-21 14:41:35 -07002934
2935 if (!n) {
2936 free_kmem_cache_nodes(s);
2937 return 0;
2938 }
2939
Christoph Lameter81819f02007-05-06 14:49:36 -07002940 s->node[node] = n;
Joonsoo Kim40534972012-05-11 00:50:47 +09002941 init_kmem_cache_node(n);
Christoph Lameter81819f02007-05-06 14:49:36 -07002942 }
2943 return 1;
2944}
Christoph Lameter81819f02007-05-06 14:49:36 -07002945
David Rientjesc0bdb232009-02-25 09:16:35 +02002946static void set_min_partial(struct kmem_cache *s, unsigned long min)
David Rientjes3b89d7d2009-02-22 17:40:07 -08002947{
2948 if (min < MIN_PARTIAL)
2949 min = MIN_PARTIAL;
2950 else if (min > MAX_PARTIAL)
2951 min = MAX_PARTIAL;
2952 s->min_partial = min;
2953}
2954
Christoph Lameter81819f02007-05-06 14:49:36 -07002955/*
2956 * calculate_sizes() determines the order and the distribution of data within
2957 * a slab object.
2958 */
Christoph Lameter06b285d2008-04-14 19:11:41 +03002959static int calculate_sizes(struct kmem_cache *s, int forced_order)
Christoph Lameter81819f02007-05-06 14:49:36 -07002960{
2961 unsigned long flags = s->flags;
Christoph Lameter3b0efdf2012-06-13 10:24:57 -05002962 unsigned long size = s->object_size;
Christoph Lameter834f3d12008-04-14 19:11:31 +03002963 int order;
Christoph Lameter81819f02007-05-06 14:49:36 -07002964
2965 /*
Christoph Lameterd8b42bf2008-02-15 23:45:25 -08002966 * Round up object size to the next word boundary. We can only
2967 * place the free pointer at word boundaries and this determines
2968 * the possible location of the free pointer.
2969 */
2970 size = ALIGN(size, sizeof(void *));
2971
2972#ifdef CONFIG_SLUB_DEBUG
2973 /*
Christoph Lameter81819f02007-05-06 14:49:36 -07002974 * Determine if we can poison the object itself. If the user of
2975 * the slab may touch the object after free or before allocation
2976 * then we should never poison the object itself.
2977 */
2978 if ((flags & SLAB_POISON) && !(flags & SLAB_DESTROY_BY_RCU) &&
Christoph Lameterc59def92007-05-16 22:10:50 -07002979 !s->ctor)
Christoph Lameter81819f02007-05-06 14:49:36 -07002980 s->flags |= __OBJECT_POISON;
2981 else
2982 s->flags &= ~__OBJECT_POISON;
2983
Christoph Lameter81819f02007-05-06 14:49:36 -07002984
2985 /*
Christoph Lameter672bba32007-05-09 02:32:39 -07002986 * If we are Redzoning then check if there is some space between the
Christoph Lameter81819f02007-05-06 14:49:36 -07002987 * end of the object and the free pointer. If not then add an
Christoph Lameter672bba32007-05-09 02:32:39 -07002988 * additional word to have some bytes to store Redzone information.
Christoph Lameter81819f02007-05-06 14:49:36 -07002989 */
Christoph Lameter3b0efdf2012-06-13 10:24:57 -05002990 if ((flags & SLAB_RED_ZONE) && size == s->object_size)
Christoph Lameter81819f02007-05-06 14:49:36 -07002991 size += sizeof(void *);
Christoph Lameter41ecc552007-05-09 02:32:44 -07002992#endif
Christoph Lameter81819f02007-05-06 14:49:36 -07002993
2994 /*
Christoph Lameter672bba32007-05-09 02:32:39 -07002995 * With that we have determined the number of bytes in actual use
2996 * by the object. This is the potential offset to the free pointer.
Christoph Lameter81819f02007-05-06 14:49:36 -07002997 */
2998 s->inuse = size;
2999
3000 if (((flags & (SLAB_DESTROY_BY_RCU | SLAB_POISON)) ||
Christoph Lameterc59def92007-05-16 22:10:50 -07003001 s->ctor)) {
Christoph Lameter81819f02007-05-06 14:49:36 -07003002 /*
3003 * Relocate free pointer after the object if it is not
3004 * permitted to overwrite the first word of the object on
3005 * kmem_cache_free.
3006 *
3007 * This is the case if we do RCU, have a constructor or
3008 * destructor or are poisoning the objects.
3009 */
3010 s->offset = size;
3011 size += sizeof(void *);
3012 }
3013
Christoph Lameterc12b3c62007-05-23 13:57:31 -07003014#ifdef CONFIG_SLUB_DEBUG
Christoph Lameter81819f02007-05-06 14:49:36 -07003015 if (flags & SLAB_STORE_USER)
3016 /*
3017 * Need to store information about allocs and frees after
3018 * the object.
3019 */
3020 size += 2 * sizeof(struct track);
3021
Christoph Lameterbe7b3fb2007-05-09 02:32:36 -07003022 if (flags & SLAB_RED_ZONE)
Christoph Lameter81819f02007-05-06 14:49:36 -07003023 /*
3024 * Add some empty padding so that we can catch
3025 * overwrites from earlier objects rather than let
3026 * tracking information or the free pointer be
Frederik Schwarzer0211a9c2008-12-29 22:14:56 +01003027 * corrupted if a user writes before the start
Christoph Lameter81819f02007-05-06 14:49:36 -07003028 * of the object.
3029 */
3030 size += sizeof(void *);
Christoph Lameter41ecc552007-05-09 02:32:44 -07003031#endif
Christoph Lameter672bba32007-05-09 02:32:39 -07003032
Christoph Lameter81819f02007-05-06 14:49:36 -07003033 /*
Christoph Lameter81819f02007-05-06 14:49:36 -07003034 * SLUB stores one object immediately after another beginning from
3035 * offset 0. In order to align the objects we have to simply size
3036 * each object to conform to the alignment.
3037 */
Christoph Lameter45906852012-11-28 16:23:16 +00003038 size = ALIGN(size, s->align);
Christoph Lameter81819f02007-05-06 14:49:36 -07003039 s->size = size;
Christoph Lameter06b285d2008-04-14 19:11:41 +03003040 if (forced_order >= 0)
3041 order = forced_order;
3042 else
Lai Jiangshanab9a0f12011-03-10 15:21:48 +08003043 order = calculate_order(size, s->reserved);
Christoph Lameter81819f02007-05-06 14:49:36 -07003044
Christoph Lameter834f3d12008-04-14 19:11:31 +03003045 if (order < 0)
Christoph Lameter81819f02007-05-06 14:49:36 -07003046 return 0;
3047
Christoph Lameterb7a49f02008-02-14 14:21:32 -08003048 s->allocflags = 0;
Christoph Lameter834f3d12008-04-14 19:11:31 +03003049 if (order)
Christoph Lameterb7a49f02008-02-14 14:21:32 -08003050 s->allocflags |= __GFP_COMP;
3051
3052 if (s->flags & SLAB_CACHE_DMA)
Christoph Lameter2c59dd62013-01-10 19:14:19 +00003053 s->allocflags |= GFP_DMA;
Christoph Lameterb7a49f02008-02-14 14:21:32 -08003054
3055 if (s->flags & SLAB_RECLAIM_ACCOUNT)
3056 s->allocflags |= __GFP_RECLAIMABLE;
3057
Christoph Lameter81819f02007-05-06 14:49:36 -07003058 /*
3059 * Determine the number of objects per slab
3060 */
Lai Jiangshanab9a0f12011-03-10 15:21:48 +08003061 s->oo = oo_make(order, size, s->reserved);
3062 s->min = oo_make(get_order(size), size, s->reserved);
Christoph Lameter205ab992008-04-14 19:11:40 +03003063 if (oo_objects(s->oo) > oo_objects(s->max))
3064 s->max = s->oo;
Christoph Lameter81819f02007-05-06 14:49:36 -07003065
Christoph Lameter834f3d12008-04-14 19:11:31 +03003066 return !!oo_objects(s->oo);
Christoph Lameter81819f02007-05-06 14:49:36 -07003067}
3068
Christoph Lameter8a13a4c2012-09-04 23:18:33 +00003069static int kmem_cache_open(struct kmem_cache *s, unsigned long flags)
Christoph Lameter81819f02007-05-06 14:49:36 -07003070{
Christoph Lameter8a13a4c2012-09-04 23:18:33 +00003071 s->flags = kmem_cache_flags(s->size, flags, s->name, s->ctor);
Lai Jiangshanab9a0f12011-03-10 15:21:48 +08003072 s->reserved = 0;
Christoph Lameter81819f02007-05-06 14:49:36 -07003073
Lai Jiangshanda9a6382011-03-10 15:22:00 +08003074 if (need_reserve_slab_rcu && (s->flags & SLAB_DESTROY_BY_RCU))
3075 s->reserved = sizeof(struct rcu_head);
Christoph Lameter81819f02007-05-06 14:49:36 -07003076
Christoph Lameter06b285d2008-04-14 19:11:41 +03003077 if (!calculate_sizes(s, -1))
Christoph Lameter81819f02007-05-06 14:49:36 -07003078 goto error;
David Rientjes3de472132009-07-27 18:30:35 -07003079 if (disable_higher_order_debug) {
3080 /*
3081 * Disable debugging flags that store metadata if the min slab
3082 * order increased.
3083 */
Christoph Lameter3b0efdf2012-06-13 10:24:57 -05003084 if (get_order(s->size) > get_order(s->object_size)) {
David Rientjes3de472132009-07-27 18:30:35 -07003085 s->flags &= ~DEBUG_METADATA_FLAGS;
3086 s->offset = 0;
3087 if (!calculate_sizes(s, -1))
3088 goto error;
3089 }
3090 }
Christoph Lameter81819f02007-05-06 14:49:36 -07003091
Heiko Carstens25654092012-01-12 17:17:33 -08003092#if defined(CONFIG_HAVE_CMPXCHG_DOUBLE) && \
3093 defined(CONFIG_HAVE_ALIGNED_STRUCT_PAGE)
Christoph Lameterb789ef52011-06-01 12:25:49 -05003094 if (system_has_cmpxchg_double() && (s->flags & SLAB_DEBUG_FLAGS) == 0)
3095 /* Enable fast mode */
3096 s->flags |= __CMPXCHG_DOUBLE;
3097#endif
3098
David Rientjes3b89d7d2009-02-22 17:40:07 -08003099 /*
3100 * The larger the object size is, the more pages we want on the partial
3101 * list to avoid pounding the page allocator excessively.
3102 */
Christoph Lameter49e22582011-08-09 16:12:27 -05003103 set_min_partial(s, ilog2(s->size) / 2);
3104
3105 /*
3106 * cpu_partial determined the maximum number of objects kept in the
3107 * per cpu partial lists of a processor.
3108 *
3109 * Per cpu partial lists mainly contain slabs that just have one
3110 * object freed. If they are used for allocation then they can be
3111 * filled up again with minimal effort. The slab will never hit the
3112 * per node partial lists and therefore no locking will be required.
3113 *
3114 * This setting also determines
3115 *
3116 * A) The number of objects from per cpu partial slabs dumped to the
3117 * per node list when we reach the limit.
Alex Shi9f264902011-09-01 11:32:18 +08003118 * B) The number of objects in cpu partial slabs to extract from the
Christoph Lameter49e22582011-08-09 16:12:27 -05003119 * per node list when we run out of per cpu objects. We only fetch 50%
3120 * to keep some capacity around for frees.
3121 */
Christoph Lameter8f1e33d2011-11-23 09:24:27 -06003122 if (kmem_cache_debug(s))
3123 s->cpu_partial = 0;
3124 else if (s->size >= PAGE_SIZE)
Christoph Lameter49e22582011-08-09 16:12:27 -05003125 s->cpu_partial = 2;
3126 else if (s->size >= 1024)
3127 s->cpu_partial = 6;
3128 else if (s->size >= 256)
3129 s->cpu_partial = 13;
3130 else
3131 s->cpu_partial = 30;
3132
Christoph Lameter81819f02007-05-06 14:49:36 -07003133#ifdef CONFIG_NUMA
Christoph Lametere2cb96b2008-08-19 08:51:22 -05003134 s->remote_node_defrag_ratio = 1000;
Christoph Lameter81819f02007-05-06 14:49:36 -07003135#endif
Christoph Lameter55136592010-08-20 12:37:13 -05003136 if (!init_kmem_cache_nodes(s))
Christoph Lameterdfb4f092007-10-16 01:26:05 -07003137 goto error;
Christoph Lameter81819f02007-05-06 14:49:36 -07003138
Christoph Lameter55136592010-08-20 12:37:13 -05003139 if (alloc_kmem_cache_cpus(s))
Christoph Lameter278b1bb2012-09-05 00:20:34 +00003140 return 0;
Christoph Lameterff120592009-12-18 16:26:22 -06003141
Christoph Lameter4c93c3552007-10-16 01:26:08 -07003142 free_kmem_cache_nodes(s);
Christoph Lameter81819f02007-05-06 14:49:36 -07003143error:
3144 if (flags & SLAB_PANIC)
3145 panic("Cannot create slab %s size=%lu realsize=%u "
3146 "order=%u offset=%u flags=%lx\n",
Christoph Lameter8a13a4c2012-09-04 23:18:33 +00003147 s->name, (unsigned long)s->size, s->size, oo_order(s->oo),
Christoph Lameter81819f02007-05-06 14:49:36 -07003148 s->offset, flags);
Christoph Lameter278b1bb2012-09-05 00:20:34 +00003149 return -EINVAL;
Christoph Lameter81819f02007-05-06 14:49:36 -07003150}
Christoph Lameter81819f02007-05-06 14:49:36 -07003151
Christoph Lameter33b12c32008-04-25 12:22:43 -07003152static void list_slab_objects(struct kmem_cache *s, struct page *page,
3153 const char *text)
Christoph Lameter81819f02007-05-06 14:49:36 -07003154{
Christoph Lameter33b12c32008-04-25 12:22:43 -07003155#ifdef CONFIG_SLUB_DEBUG
3156 void *addr = page_address(page);
3157 void *p;
Namhyung Kima5dd5c12010-09-29 21:02:13 +09003158 unsigned long *map = kzalloc(BITS_TO_LONGS(page->objects) *
3159 sizeof(long), GFP_ATOMIC);
Eric Dumazetbbd7d572010-03-24 22:25:47 +01003160 if (!map)
3161 return;
Christoph Lameter945cf2b2012-09-04 23:18:33 +00003162 slab_err(s, page, text, s->name);
Christoph Lameter33b12c32008-04-25 12:22:43 -07003163 slab_lock(page);
Christoph Lameter33b12c32008-04-25 12:22:43 -07003164
Christoph Lameter5f80b132011-04-15 14:48:13 -05003165 get_map(s, page, map);
Christoph Lameter33b12c32008-04-25 12:22:43 -07003166 for_each_object(p, s, addr, page->objects) {
3167
3168 if (!test_bit(slab_index(p, s, addr), map)) {
3169 printk(KERN_ERR "INFO: Object 0x%p @offset=%tu\n",
3170 p, p - addr);
3171 print_tracking(s, p);
3172 }
3173 }
3174 slab_unlock(page);
Eric Dumazetbbd7d572010-03-24 22:25:47 +01003175 kfree(map);
Christoph Lameter33b12c32008-04-25 12:22:43 -07003176#endif
3177}
3178
Christoph Lameter81819f02007-05-06 14:49:36 -07003179/*
Christoph Lameter599870b2008-04-23 12:36:52 -07003180 * Attempt to free all partial slabs on a node.
Christoph Lameter69cb8e62011-08-09 16:12:22 -05003181 * This is called from kmem_cache_close(). We must be the last thread
3182 * using the cache and therefore we do not need to lock anymore.
Christoph Lameter81819f02007-05-06 14:49:36 -07003183 */
Christoph Lameter599870b2008-04-23 12:36:52 -07003184static void free_partial(struct kmem_cache *s, struct kmem_cache_node *n)
Christoph Lameter81819f02007-05-06 14:49:36 -07003185{
Christoph Lameter81819f02007-05-06 14:49:36 -07003186 struct page *page, *h;
3187
Christoph Lameter33b12c32008-04-25 12:22:43 -07003188 list_for_each_entry_safe(page, h, &n->partial, lru) {
Christoph Lameter81819f02007-05-06 14:49:36 -07003189 if (!page->inuse) {
Christoph Lameter5cc6eee2011-06-01 12:25:50 -05003190 remove_partial(n, page);
Christoph Lameter81819f02007-05-06 14:49:36 -07003191 discard_slab(s, page);
Christoph Lameter33b12c32008-04-25 12:22:43 -07003192 } else {
3193 list_slab_objects(s, page,
Christoph Lameter945cf2b2012-09-04 23:18:33 +00003194 "Objects remaining in %s on kmem_cache_close()");
Christoph Lameter599870b2008-04-23 12:36:52 -07003195 }
Christoph Lameter33b12c32008-04-25 12:22:43 -07003196 }
Christoph Lameter81819f02007-05-06 14:49:36 -07003197}
3198
3199/*
Christoph Lameter672bba32007-05-09 02:32:39 -07003200 * Release all resources used by a slab cache.
Christoph Lameter81819f02007-05-06 14:49:36 -07003201 */
Christoph Lameter0c710012007-07-17 04:03:24 -07003202static inline int kmem_cache_close(struct kmem_cache *s)
Christoph Lameter81819f02007-05-06 14:49:36 -07003203{
3204 int node;
3205
3206 flush_all(s);
Christoph Lameter81819f02007-05-06 14:49:36 -07003207 /* Attempt to free all objects */
Christoph Lameterf64dc582007-10-16 01:25:33 -07003208 for_each_node_state(node, N_NORMAL_MEMORY) {
Christoph Lameter81819f02007-05-06 14:49:36 -07003209 struct kmem_cache_node *n = get_node(s, node);
3210
Christoph Lameter599870b2008-04-23 12:36:52 -07003211 free_partial(s, n);
3212 if (n->nr_partial || slabs_node(s, node))
Christoph Lameter81819f02007-05-06 14:49:36 -07003213 return 1;
3214 }
Christoph Lameter945cf2b2012-09-04 23:18:33 +00003215 free_percpu(s->cpu_slab);
Christoph Lameter81819f02007-05-06 14:49:36 -07003216 free_kmem_cache_nodes(s);
3217 return 0;
3218}
3219
Christoph Lameter945cf2b2012-09-04 23:18:33 +00003220int __kmem_cache_shutdown(struct kmem_cache *s)
Christoph Lameter81819f02007-05-06 14:49:36 -07003221{
Christoph Lameter12c36672012-09-04 23:38:33 +00003222 int rc = kmem_cache_close(s);
Christoph Lameter945cf2b2012-09-04 23:18:33 +00003223
Glauber Costa5413dfb2012-12-18 14:23:13 -08003224 if (!rc) {
3225 /*
3226 * We do the same lock strategy around sysfs_slab_add, see
3227 * __kmem_cache_create. Because this is pretty much the last
3228 * operation we do and the lock will be released shortly after
3229 * that in slab_common.c, we could just move sysfs_slab_remove
3230 * to a later point in common code. We should do that when we
3231 * have a common sysfs framework for all allocators.
3232 */
3233 mutex_unlock(&slab_mutex);
Christoph Lameter81819f02007-05-06 14:49:36 -07003234 sysfs_slab_remove(s);
Glauber Costa5413dfb2012-12-18 14:23:13 -08003235 mutex_lock(&slab_mutex);
3236 }
Christoph Lameter12c36672012-09-04 23:38:33 +00003237
3238 return rc;
Christoph Lameter81819f02007-05-06 14:49:36 -07003239}
Christoph Lameter81819f02007-05-06 14:49:36 -07003240
3241/********************************************************************
3242 * Kmalloc subsystem
3243 *******************************************************************/
3244
Christoph Lameter81819f02007-05-06 14:49:36 -07003245static int __init setup_slub_min_order(char *str)
3246{
Pekka Enberg06428782008-01-07 23:20:27 -08003247 get_option(&str, &slub_min_order);
Christoph Lameter81819f02007-05-06 14:49:36 -07003248
3249 return 1;
3250}
3251
3252__setup("slub_min_order=", setup_slub_min_order);
3253
3254static int __init setup_slub_max_order(char *str)
3255{
Pekka Enberg06428782008-01-07 23:20:27 -08003256 get_option(&str, &slub_max_order);
David Rientjes818cf592009-04-23 09:58:22 +03003257 slub_max_order = min(slub_max_order, MAX_ORDER - 1);
Christoph Lameter81819f02007-05-06 14:49:36 -07003258
3259 return 1;
3260}
3261
3262__setup("slub_max_order=", setup_slub_max_order);
3263
3264static int __init setup_slub_min_objects(char *str)
3265{
Pekka Enberg06428782008-01-07 23:20:27 -08003266 get_option(&str, &slub_min_objects);
Christoph Lameter81819f02007-05-06 14:49:36 -07003267
3268 return 1;
3269}
3270
3271__setup("slub_min_objects=", setup_slub_min_objects);
3272
3273static int __init setup_slub_nomerge(char *str)
3274{
3275 slub_nomerge = 1;
3276 return 1;
3277}
3278
3279__setup("slub_nomerge", setup_slub_nomerge);
3280
Christoph Lameter81819f02007-05-06 14:49:36 -07003281void *__kmalloc(size_t size, gfp_t flags)
3282{
Christoph Lameteraadb4bc2007-10-16 01:24:38 -07003283 struct kmem_cache *s;
Eduard - Gabriel Munteanu5b882be2008-08-19 20:43:26 +03003284 void *ret;
Christoph Lameter81819f02007-05-06 14:49:36 -07003285
Christoph Lameter95a05b42013-01-10 19:14:19 +00003286 if (unlikely(size > KMALLOC_MAX_CACHE_SIZE))
Pekka Enbergeada35e2008-02-11 22:47:46 +02003287 return kmalloc_large(size, flags);
Christoph Lameteraadb4bc2007-10-16 01:24:38 -07003288
Christoph Lameter2c59dd62013-01-10 19:14:19 +00003289 s = kmalloc_slab(size, flags);
Christoph Lameteraadb4bc2007-10-16 01:24:38 -07003290
3291 if (unlikely(ZERO_OR_NULL_PTR(s)))
Christoph Lameter6cb8f912007-07-17 04:03:22 -07003292 return s;
3293
Ezequiel Garcia2b847c32012-09-08 17:47:58 -03003294 ret = slab_alloc(s, flags, _RET_IP_);
Eduard - Gabriel Munteanu5b882be2008-08-19 20:43:26 +03003295
Eduard - Gabriel Munteanuca2b84cb2009-03-23 15:12:24 +02003296 trace_kmalloc(_RET_IP_, ret, size, s->size, flags);
Eduard - Gabriel Munteanu5b882be2008-08-19 20:43:26 +03003297
3298 return ret;
Christoph Lameter81819f02007-05-06 14:49:36 -07003299}
3300EXPORT_SYMBOL(__kmalloc);
3301
Namhyung Kim5d1f57e2010-09-29 21:02:15 +09003302#ifdef CONFIG_NUMA
Christoph Lameterf619cfe2008-03-01 13:56:40 -08003303static void *kmalloc_large_node(size_t size, gfp_t flags, int node)
3304{
Vegard Nossumb1eeab62008-11-25 16:55:53 +01003305 struct page *page;
Catalin Marinase4f7c0b42009-07-07 10:32:59 +01003306 void *ptr = NULL;
Christoph Lameterf619cfe2008-03-01 13:56:40 -08003307
Glauber Costad79923f2012-12-18 14:22:48 -08003308 flags |= __GFP_COMP | __GFP_NOTRACK | __GFP_KMEMCG;
Vegard Nossumb1eeab62008-11-25 16:55:53 +01003309 page = alloc_pages_node(node, flags, get_order(size));
Christoph Lameterf619cfe2008-03-01 13:56:40 -08003310 if (page)
Catalin Marinase4f7c0b42009-07-07 10:32:59 +01003311 ptr = page_address(page);
3312
3313 kmemleak_alloc(ptr, size, 1, flags);
3314 return ptr;
Christoph Lameterf619cfe2008-03-01 13:56:40 -08003315}
3316
Christoph Lameter81819f02007-05-06 14:49:36 -07003317void *__kmalloc_node(size_t size, gfp_t flags, int node)
3318{
Christoph Lameteraadb4bc2007-10-16 01:24:38 -07003319 struct kmem_cache *s;
Eduard - Gabriel Munteanu5b882be2008-08-19 20:43:26 +03003320 void *ret;
Christoph Lameter81819f02007-05-06 14:49:36 -07003321
Christoph Lameter95a05b42013-01-10 19:14:19 +00003322 if (unlikely(size > KMALLOC_MAX_CACHE_SIZE)) {
Eduard - Gabriel Munteanu5b882be2008-08-19 20:43:26 +03003323 ret = kmalloc_large_node(size, flags, node);
3324
Eduard - Gabriel Munteanuca2b84cb2009-03-23 15:12:24 +02003325 trace_kmalloc_node(_RET_IP_, ret,
3326 size, PAGE_SIZE << get_order(size),
3327 flags, node);
Eduard - Gabriel Munteanu5b882be2008-08-19 20:43:26 +03003328
3329 return ret;
3330 }
Christoph Lameteraadb4bc2007-10-16 01:24:38 -07003331
Christoph Lameter2c59dd62013-01-10 19:14:19 +00003332 s = kmalloc_slab(size, flags);
Christoph Lameteraadb4bc2007-10-16 01:24:38 -07003333
3334 if (unlikely(ZERO_OR_NULL_PTR(s)))
Christoph Lameter6cb8f912007-07-17 04:03:22 -07003335 return s;
3336
Ezequiel Garcia2b847c32012-09-08 17:47:58 -03003337 ret = slab_alloc_node(s, flags, node, _RET_IP_);
Eduard - Gabriel Munteanu5b882be2008-08-19 20:43:26 +03003338
Eduard - Gabriel Munteanuca2b84cb2009-03-23 15:12:24 +02003339 trace_kmalloc_node(_RET_IP_, ret, size, s->size, flags, node);
Eduard - Gabriel Munteanu5b882be2008-08-19 20:43:26 +03003340
3341 return ret;
Christoph Lameter81819f02007-05-06 14:49:36 -07003342}
3343EXPORT_SYMBOL(__kmalloc_node);
3344#endif
3345
3346size_t ksize(const void *object)
3347{
Christoph Lameter272c1d22007-06-08 13:46:49 -07003348 struct page *page;
Christoph Lameter81819f02007-05-06 14:49:36 -07003349
Christoph Lameteref8b4522007-10-16 01:24:46 -07003350 if (unlikely(object == ZERO_SIZE_PTR))
Christoph Lameter272c1d22007-06-08 13:46:49 -07003351 return 0;
3352
Vegard Nossum294a80a2007-12-04 23:45:30 -08003353 page = virt_to_head_page(object);
Vegard Nossum294a80a2007-12-04 23:45:30 -08003354
Pekka Enberg76994412008-05-22 19:22:25 +03003355 if (unlikely(!PageSlab(page))) {
3356 WARN_ON(!PageCompound(page));
Vegard Nossum294a80a2007-12-04 23:45:30 -08003357 return PAGE_SIZE << compound_order(page);
Pekka Enberg76994412008-05-22 19:22:25 +03003358 }
Christoph Lameter81819f02007-05-06 14:49:36 -07003359
Glauber Costa1b4f59e32012-10-22 18:05:36 +04003360 return slab_ksize(page->slab_cache);
Christoph Lameter81819f02007-05-06 14:49:36 -07003361}
Kirill A. Shutemovb1aabec2009-02-10 15:21:44 +02003362EXPORT_SYMBOL(ksize);
Christoph Lameter81819f02007-05-06 14:49:36 -07003363
Ben Greeard18a90d2011-07-07 11:36:37 -07003364#ifdef CONFIG_SLUB_DEBUG
3365bool verify_mem_not_deleted(const void *x)
3366{
3367 struct page *page;
3368 void *object = (void *)x;
3369 unsigned long flags;
3370 bool rv;
3371
3372 if (unlikely(ZERO_OR_NULL_PTR(x)))
3373 return false;
3374
3375 local_irq_save(flags);
3376
3377 page = virt_to_head_page(x);
3378 if (unlikely(!PageSlab(page))) {
3379 /* maybe it was from stack? */
3380 rv = true;
3381 goto out_unlock;
3382 }
3383
3384 slab_lock(page);
Glauber Costa1b4f59e32012-10-22 18:05:36 +04003385 if (on_freelist(page->slab_cache, page, object)) {
3386 object_err(page->slab_cache, page, object, "Object is on free-list");
Ben Greeard18a90d2011-07-07 11:36:37 -07003387 rv = false;
3388 } else {
3389 rv = true;
3390 }
3391 slab_unlock(page);
3392
3393out_unlock:
3394 local_irq_restore(flags);
3395 return rv;
3396}
3397EXPORT_SYMBOL(verify_mem_not_deleted);
3398#endif
3399
Christoph Lameter81819f02007-05-06 14:49:36 -07003400void kfree(const void *x)
3401{
Christoph Lameter81819f02007-05-06 14:49:36 -07003402 struct page *page;
Christoph Lameter5bb983b2008-02-07 17:47:41 -08003403 void *object = (void *)x;
Christoph Lameter81819f02007-05-06 14:49:36 -07003404
Pekka Enberg2121db72009-03-25 11:05:57 +02003405 trace_kfree(_RET_IP_, x);
3406
Satyam Sharma2408c552007-10-16 01:24:44 -07003407 if (unlikely(ZERO_OR_NULL_PTR(x)))
Christoph Lameter81819f02007-05-06 14:49:36 -07003408 return;
3409
Christoph Lameterb49af682007-05-06 14:49:41 -07003410 page = virt_to_head_page(x);
Christoph Lameteraadb4bc2007-10-16 01:24:38 -07003411 if (unlikely(!PageSlab(page))) {
Christoph Lameter09375022008-05-28 10:32:22 -07003412 BUG_ON(!PageCompound(page));
Catalin Marinase4f7c0b42009-07-07 10:32:59 +01003413 kmemleak_free(x);
Glauber Costad79923f2012-12-18 14:22:48 -08003414 __free_memcg_kmem_pages(page, compound_order(page));
Christoph Lameteraadb4bc2007-10-16 01:24:38 -07003415 return;
3416 }
Glauber Costa1b4f59e32012-10-22 18:05:36 +04003417 slab_free(page->slab_cache, page, object, _RET_IP_);
Christoph Lameter81819f02007-05-06 14:49:36 -07003418}
3419EXPORT_SYMBOL(kfree);
3420
Christoph Lameter2086d262007-05-06 14:49:46 -07003421/*
Christoph Lameter672bba32007-05-09 02:32:39 -07003422 * kmem_cache_shrink removes empty slabs from the partial lists and sorts
3423 * the remaining slabs by the number of items in use. The slabs with the
3424 * most items in use come first. New allocations will then fill those up
3425 * and thus they can be removed from the partial lists.
3426 *
3427 * The slabs with the least items are placed last. This results in them
3428 * being allocated from last increasing the chance that the last objects
3429 * are freed in them.
Christoph Lameter2086d262007-05-06 14:49:46 -07003430 */
3431int kmem_cache_shrink(struct kmem_cache *s)
3432{
3433 int node;
3434 int i;
3435 struct kmem_cache_node *n;
3436 struct page *page;
3437 struct page *t;
Christoph Lameter205ab992008-04-14 19:11:40 +03003438 int objects = oo_objects(s->max);
Christoph Lameter2086d262007-05-06 14:49:46 -07003439 struct list_head *slabs_by_inuse =
Christoph Lameter834f3d12008-04-14 19:11:31 +03003440 kmalloc(sizeof(struct list_head) * objects, GFP_KERNEL);
Christoph Lameter2086d262007-05-06 14:49:46 -07003441 unsigned long flags;
3442
3443 if (!slabs_by_inuse)
3444 return -ENOMEM;
3445
3446 flush_all(s);
Christoph Lameterf64dc582007-10-16 01:25:33 -07003447 for_each_node_state(node, N_NORMAL_MEMORY) {
Christoph Lameter2086d262007-05-06 14:49:46 -07003448 n = get_node(s, node);
3449
3450 if (!n->nr_partial)
3451 continue;
3452
Christoph Lameter834f3d12008-04-14 19:11:31 +03003453 for (i = 0; i < objects; i++)
Christoph Lameter2086d262007-05-06 14:49:46 -07003454 INIT_LIST_HEAD(slabs_by_inuse + i);
3455
Thomas Gleixner57be9db2012-10-25 10:32:35 +01003456 raw_spin_lock_irqsave(&n->list_lock, flags);
Christoph Lameter2086d262007-05-06 14:49:46 -07003457
3458 /*
Christoph Lameter672bba32007-05-09 02:32:39 -07003459 * Build lists indexed by the items in use in each slab.
Christoph Lameter2086d262007-05-06 14:49:46 -07003460 *
Christoph Lameter672bba32007-05-09 02:32:39 -07003461 * Note that concurrent frees may occur while we hold the
3462 * list_lock. page->inuse here is the upper limit.
Christoph Lameter2086d262007-05-06 14:49:46 -07003463 */
3464 list_for_each_entry_safe(page, t, &n->partial, lru) {
Christoph Lameter69cb8e62011-08-09 16:12:22 -05003465 list_move(&page->lru, slabs_by_inuse + page->inuse);
3466 if (!page->inuse)
3467 n->nr_partial--;
Christoph Lameter2086d262007-05-06 14:49:46 -07003468 }
3469
Christoph Lameter2086d262007-05-06 14:49:46 -07003470 /*
Christoph Lameter672bba32007-05-09 02:32:39 -07003471 * Rebuild the partial list with the slabs filled up most
3472 * first and the least used slabs at the end.
Christoph Lameter2086d262007-05-06 14:49:46 -07003473 */
Christoph Lameter69cb8e62011-08-09 16:12:22 -05003474 for (i = objects - 1; i > 0; i--)
Christoph Lameter2086d262007-05-06 14:49:46 -07003475 list_splice(slabs_by_inuse + i, n->partial.prev);
3476
Thomas Gleixner57be9db2012-10-25 10:32:35 +01003477 raw_spin_unlock_irqrestore(&n->list_lock, flags);
Christoph Lameter69cb8e62011-08-09 16:12:22 -05003478
3479 /* Release empty slabs */
3480 list_for_each_entry_safe(page, t, slabs_by_inuse, lru)
3481 discard_slab(s, page);
Christoph Lameter2086d262007-05-06 14:49:46 -07003482 }
3483
3484 kfree(slabs_by_inuse);
3485 return 0;
3486}
3487EXPORT_SYMBOL(kmem_cache_shrink);
3488
Yasunori Gotob9049e22007-10-21 16:41:37 -07003489static int slab_mem_going_offline_callback(void *arg)
3490{
3491 struct kmem_cache *s;
3492
Christoph Lameter18004c52012-07-06 15:25:12 -05003493 mutex_lock(&slab_mutex);
Yasunori Gotob9049e22007-10-21 16:41:37 -07003494 list_for_each_entry(s, &slab_caches, list)
3495 kmem_cache_shrink(s);
Christoph Lameter18004c52012-07-06 15:25:12 -05003496 mutex_unlock(&slab_mutex);
Yasunori Gotob9049e22007-10-21 16:41:37 -07003497
3498 return 0;
3499}
3500
3501static void slab_mem_offline_callback(void *arg)
3502{
3503 struct kmem_cache_node *n;
3504 struct kmem_cache *s;
3505 struct memory_notify *marg = arg;
3506 int offline_node;
3507
Lai Jiangshanb9d5ab22012-12-11 16:01:05 -08003508 offline_node = marg->status_change_nid_normal;
Yasunori Gotob9049e22007-10-21 16:41:37 -07003509
3510 /*
3511 * If the node still has available memory. we need kmem_cache_node
3512 * for it yet.
3513 */
3514 if (offline_node < 0)
3515 return;
3516
Christoph Lameter18004c52012-07-06 15:25:12 -05003517 mutex_lock(&slab_mutex);
Yasunori Gotob9049e22007-10-21 16:41:37 -07003518 list_for_each_entry(s, &slab_caches, list) {
3519 n = get_node(s, offline_node);
3520 if (n) {
3521 /*
3522 * if n->nr_slabs > 0, slabs still exist on the node
3523 * that is going down. We were unable to free them,
Adam Buchbinderc9404c92009-12-18 15:40:42 -05003524 * and offline_pages() function shouldn't call this
Yasunori Gotob9049e22007-10-21 16:41:37 -07003525 * callback. So, we must fail.
3526 */
Christoph Lameter0f389ec2008-04-14 18:53:02 +03003527 BUG_ON(slabs_node(s, offline_node));
Yasunori Gotob9049e22007-10-21 16:41:37 -07003528
3529 s->node[offline_node] = NULL;
Christoph Lameter8de66a02010-08-25 14:51:14 -05003530 kmem_cache_free(kmem_cache_node, n);
Yasunori Gotob9049e22007-10-21 16:41:37 -07003531 }
3532 }
Christoph Lameter18004c52012-07-06 15:25:12 -05003533 mutex_unlock(&slab_mutex);
Yasunori Gotob9049e22007-10-21 16:41:37 -07003534}
3535
3536static int slab_mem_going_online_callback(void *arg)
3537{
3538 struct kmem_cache_node *n;
3539 struct kmem_cache *s;
3540 struct memory_notify *marg = arg;
Lai Jiangshanb9d5ab22012-12-11 16:01:05 -08003541 int nid = marg->status_change_nid_normal;
Yasunori Gotob9049e22007-10-21 16:41:37 -07003542 int ret = 0;
3543
3544 /*
3545 * If the node's memory is already available, then kmem_cache_node is
3546 * already created. Nothing to do.
3547 */
3548 if (nid < 0)
3549 return 0;
3550
3551 /*
Christoph Lameter0121c6192008-04-29 16:11:12 -07003552 * We are bringing a node online. No memory is available yet. We must
Yasunori Gotob9049e22007-10-21 16:41:37 -07003553 * allocate a kmem_cache_node structure in order to bring the node
3554 * online.
3555 */
Christoph Lameter18004c52012-07-06 15:25:12 -05003556 mutex_lock(&slab_mutex);
Yasunori Gotob9049e22007-10-21 16:41:37 -07003557 list_for_each_entry(s, &slab_caches, list) {
3558 /*
3559 * XXX: kmem_cache_alloc_node will fallback to other nodes
3560 * since memory is not yet available from the node that
3561 * is brought up.
3562 */
Christoph Lameter8de66a02010-08-25 14:51:14 -05003563 n = kmem_cache_alloc(kmem_cache_node, GFP_KERNEL);
Yasunori Gotob9049e22007-10-21 16:41:37 -07003564 if (!n) {
3565 ret = -ENOMEM;
3566 goto out;
3567 }
Joonsoo Kim40534972012-05-11 00:50:47 +09003568 init_kmem_cache_node(n);
Yasunori Gotob9049e22007-10-21 16:41:37 -07003569 s->node[nid] = n;
3570 }
3571out:
Christoph Lameter18004c52012-07-06 15:25:12 -05003572 mutex_unlock(&slab_mutex);
Yasunori Gotob9049e22007-10-21 16:41:37 -07003573 return ret;
3574}
3575
3576static int slab_memory_callback(struct notifier_block *self,
3577 unsigned long action, void *arg)
3578{
3579 int ret = 0;
3580
3581 switch (action) {
3582 case MEM_GOING_ONLINE:
3583 ret = slab_mem_going_online_callback(arg);
3584 break;
3585 case MEM_GOING_OFFLINE:
3586 ret = slab_mem_going_offline_callback(arg);
3587 break;
3588 case MEM_OFFLINE:
3589 case MEM_CANCEL_ONLINE:
3590 slab_mem_offline_callback(arg);
3591 break;
3592 case MEM_ONLINE:
3593 case MEM_CANCEL_OFFLINE:
3594 break;
3595 }
KAMEZAWA Hiroyukidc19f9d2008-12-01 13:13:48 -08003596 if (ret)
3597 ret = notifier_from_errno(ret);
3598 else
3599 ret = NOTIFY_OK;
Yasunori Gotob9049e22007-10-21 16:41:37 -07003600 return ret;
3601}
3602
Andrew Morton3ac38fa2013-04-29 15:08:06 -07003603static struct notifier_block slab_memory_callback_nb = {
3604 .notifier_call = slab_memory_callback,
3605 .priority = SLAB_CALLBACK_PRI,
3606};
Yasunori Gotob9049e22007-10-21 16:41:37 -07003607
Christoph Lameter81819f02007-05-06 14:49:36 -07003608/********************************************************************
3609 * Basic setup of slabs
3610 *******************************************************************/
3611
Christoph Lameter51df1142010-08-20 12:37:15 -05003612/*
3613 * Used for early kmem_cache structures that were allocated using
Christoph Lameterdffb4d62012-11-28 16:23:07 +00003614 * the page allocator. Allocate them properly then fix up the pointers
3615 * that may be pointing to the wrong kmem_cache structure.
Christoph Lameter51df1142010-08-20 12:37:15 -05003616 */
3617
Christoph Lameterdffb4d62012-11-28 16:23:07 +00003618static struct kmem_cache * __init bootstrap(struct kmem_cache *static_cache)
Christoph Lameter51df1142010-08-20 12:37:15 -05003619{
3620 int node;
Christoph Lameterdffb4d62012-11-28 16:23:07 +00003621 struct kmem_cache *s = kmem_cache_zalloc(kmem_cache, GFP_NOWAIT);
Christoph Lameter51df1142010-08-20 12:37:15 -05003622
Christoph Lameterdffb4d62012-11-28 16:23:07 +00003623 memcpy(s, static_cache, kmem_cache->object_size);
Christoph Lameter51df1142010-08-20 12:37:15 -05003624
Glauber Costa7d557b32013-02-22 20:20:00 +04003625 /*
3626 * This runs very early, and only the boot processor is supposed to be
3627 * up. Even if it weren't true, IRQs are not up so we couldn't fire
3628 * IPIs around.
3629 */
3630 __flush_cpu_slab(s, smp_processor_id());
Christoph Lameter51df1142010-08-20 12:37:15 -05003631 for_each_node_state(node, N_NORMAL_MEMORY) {
3632 struct kmem_cache_node *n = get_node(s, node);
3633 struct page *p;
3634
3635 if (n) {
3636 list_for_each_entry(p, &n->partial, lru)
Glauber Costa1b4f59e32012-10-22 18:05:36 +04003637 p->slab_cache = s;
Christoph Lameter51df1142010-08-20 12:37:15 -05003638
Li Zefan607bf322011-04-12 15:22:26 +08003639#ifdef CONFIG_SLUB_DEBUG
Christoph Lameter51df1142010-08-20 12:37:15 -05003640 list_for_each_entry(p, &n->full, lru)
Glauber Costa1b4f59e32012-10-22 18:05:36 +04003641 p->slab_cache = s;
Christoph Lameter51df1142010-08-20 12:37:15 -05003642#endif
3643 }
3644 }
Christoph Lameterdffb4d62012-11-28 16:23:07 +00003645 list_add(&s->list, &slab_caches);
3646 return s;
Christoph Lameter51df1142010-08-20 12:37:15 -05003647}
3648
Christoph Lameter81819f02007-05-06 14:49:36 -07003649void __init kmem_cache_init(void)
3650{
Christoph Lameterdffb4d62012-11-28 16:23:07 +00003651 static __initdata struct kmem_cache boot_kmem_cache,
3652 boot_kmem_cache_node;
Thomas Gleixner57be9db2012-10-25 10:32:35 +01003653 int cpu;
3654
3655 for_each_possible_cpu(cpu) {
3656 raw_spin_lock_init(&per_cpu(slub_free_list, cpu).lock);
3657 INIT_LIST_HEAD(&per_cpu(slub_free_list, cpu).list);
3658 }
Christoph Lameter51df1142010-08-20 12:37:15 -05003659
Stanislaw Gruszkafc8d8622012-01-10 15:07:32 -08003660 if (debug_guardpage_minorder())
3661 slub_max_order = 0;
3662
Christoph Lameterdffb4d62012-11-28 16:23:07 +00003663 kmem_cache_node = &boot_kmem_cache_node;
3664 kmem_cache = &boot_kmem_cache;
Christoph Lameter51df1142010-08-20 12:37:15 -05003665
Christoph Lameterdffb4d62012-11-28 16:23:07 +00003666 create_boot_cache(kmem_cache_node, "kmem_cache_node",
3667 sizeof(struct kmem_cache_node), SLAB_HWCACHE_ALIGN);
Yasunori Gotob9049e22007-10-21 16:41:37 -07003668
Andrew Morton3ac38fa2013-04-29 15:08:06 -07003669 register_hotmemory_notifier(&slab_memory_callback_nb);
Christoph Lameter81819f02007-05-06 14:49:36 -07003670
3671 /* Able to allocate the per node structures */
3672 slab_state = PARTIAL;
3673
Christoph Lameterdffb4d62012-11-28 16:23:07 +00003674 create_boot_cache(kmem_cache, "kmem_cache",
3675 offsetof(struct kmem_cache, node) +
3676 nr_node_ids * sizeof(struct kmem_cache_node *),
3677 SLAB_HWCACHE_ALIGN);
Christoph Lameter8a13a4c2012-09-04 23:18:33 +00003678
Christoph Lameterdffb4d62012-11-28 16:23:07 +00003679 kmem_cache = bootstrap(&boot_kmem_cache);
Christoph Lameter81819f02007-05-06 14:49:36 -07003680
Christoph Lameter51df1142010-08-20 12:37:15 -05003681 /*
3682 * Allocate kmem_cache_node properly from the kmem_cache slab.
3683 * kmem_cache_node is separately allocated so no need to
3684 * update any list pointers.
3685 */
Christoph Lameterdffb4d62012-11-28 16:23:07 +00003686 kmem_cache_node = bootstrap(&boot_kmem_cache_node);
Christoph Lameter51df1142010-08-20 12:37:15 -05003687
3688 /* Now we can use the kmem_cache to allocate kmalloc slabs */
Christoph Lameterf97d5f62013-01-10 19:12:17 +00003689 create_kmalloc_caches(0);
Christoph Lameter81819f02007-05-06 14:49:36 -07003690
3691#ifdef CONFIG_SMP
3692 register_cpu_notifier(&slab_notifier);
Christoph Lameter9dfc6e62009-12-18 16:26:20 -06003693#endif
Christoph Lameter81819f02007-05-06 14:49:36 -07003694
Ingo Molnar3adbefe2008-02-05 17:57:39 -08003695 printk(KERN_INFO
Christoph Lameterf97d5f62013-01-10 19:12:17 +00003696 "SLUB: HWalign=%d, Order=%d-%d, MinObjects=%d,"
Christoph Lameter4b356be2007-06-16 10:16:13 -07003697 " CPUs=%d, Nodes=%d\n",
Christoph Lameterf97d5f62013-01-10 19:12:17 +00003698 cache_line_size(),
Christoph Lameter81819f02007-05-06 14:49:36 -07003699 slub_min_order, slub_max_order, slub_min_objects,
3700 nr_cpu_ids, nr_node_ids);
3701}
3702
Pekka Enberg7e85ee02009-06-12 14:03:06 +03003703void __init kmem_cache_init_late(void)
3704{
Pekka Enberg7e85ee02009-06-12 14:03:06 +03003705}
3706
Christoph Lameter81819f02007-05-06 14:49:36 -07003707/*
3708 * Find a mergeable slab cache
3709 */
3710static int slab_unmergeable(struct kmem_cache *s)
3711{
3712 if (slub_nomerge || (s->flags & SLUB_NEVER_MERGE))
3713 return 1;
3714
Christoph Lameterc59def92007-05-16 22:10:50 -07003715 if (s->ctor)
Christoph Lameter81819f02007-05-06 14:49:36 -07003716 return 1;
3717
Christoph Lameter8ffa6872007-05-31 00:40:51 -07003718 /*
3719 * We may have set a slab to be unmergeable during bootstrap.
3720 */
3721 if (s->refcount < 0)
3722 return 1;
3723
Christoph Lameter81819f02007-05-06 14:49:36 -07003724 return 0;
3725}
3726
Glauber Costa2633d7a2012-12-18 14:22:34 -08003727static struct kmem_cache *find_mergeable(struct mem_cgroup *memcg, size_t size,
Christoph Lameterba0268a2007-09-11 15:24:11 -07003728 size_t align, unsigned long flags, const char *name,
Alexey Dobriyan51cc5062008-07-25 19:45:34 -07003729 void (*ctor)(void *))
Christoph Lameter81819f02007-05-06 14:49:36 -07003730{
Christoph Lameter5b95a4ac2007-07-17 04:03:19 -07003731 struct kmem_cache *s;
Christoph Lameter81819f02007-05-06 14:49:36 -07003732
3733 if (slub_nomerge || (flags & SLUB_NEVER_MERGE))
3734 return NULL;
3735
Christoph Lameterc59def92007-05-16 22:10:50 -07003736 if (ctor)
Christoph Lameter81819f02007-05-06 14:49:36 -07003737 return NULL;
3738
3739 size = ALIGN(size, sizeof(void *));
3740 align = calculate_alignment(flags, align, size);
3741 size = ALIGN(size, align);
Christoph Lameterba0268a2007-09-11 15:24:11 -07003742 flags = kmem_cache_flags(size, flags, name, NULL);
Christoph Lameter81819f02007-05-06 14:49:36 -07003743
Christoph Lameter5b95a4ac2007-07-17 04:03:19 -07003744 list_for_each_entry(s, &slab_caches, list) {
Christoph Lameter81819f02007-05-06 14:49:36 -07003745 if (slab_unmergeable(s))
3746 continue;
3747
3748 if (size > s->size)
3749 continue;
3750
Christoph Lameterba0268a2007-09-11 15:24:11 -07003751 if ((flags & SLUB_MERGE_SAME) != (s->flags & SLUB_MERGE_SAME))
Christoph Lameter81819f02007-05-06 14:49:36 -07003752 continue;
3753 /*
3754 * Check if alignment is compatible.
3755 * Courtesy of Adrian Drzewiecki
3756 */
Pekka Enberg06428782008-01-07 23:20:27 -08003757 if ((s->size & ~(align - 1)) != s->size)
Christoph Lameter81819f02007-05-06 14:49:36 -07003758 continue;
3759
3760 if (s->size - size >= sizeof(void *))
3761 continue;
3762
Glauber Costa2633d7a2012-12-18 14:22:34 -08003763 if (!cache_match_memcg(s, memcg))
3764 continue;
3765
Christoph Lameter81819f02007-05-06 14:49:36 -07003766 return s;
3767 }
3768 return NULL;
3769}
3770
Glauber Costa2633d7a2012-12-18 14:22:34 -08003771struct kmem_cache *
3772__kmem_cache_alias(struct mem_cgroup *memcg, const char *name, size_t size,
3773 size_t align, unsigned long flags, void (*ctor)(void *))
Christoph Lameter81819f02007-05-06 14:49:36 -07003774{
3775 struct kmem_cache *s;
3776
Glauber Costa2633d7a2012-12-18 14:22:34 -08003777 s = find_mergeable(memcg, size, align, flags, name, ctor);
Christoph Lameter81819f02007-05-06 14:49:36 -07003778 if (s) {
3779 s->refcount++;
3780 /*
3781 * Adjust the object sizes so that we clear
3782 * the complete object on kzalloc.
3783 */
Christoph Lameter3b0efdf2012-06-13 10:24:57 -05003784 s->object_size = max(s->object_size, (int)size);
Christoph Lameter81819f02007-05-06 14:49:36 -07003785 s->inuse = max_t(int, s->inuse, ALIGN(size, sizeof(void *)));
Christoph Lameter6446faa2008-02-15 23:45:26 -08003786
David Rientjes7b8f3b62008-12-17 22:09:46 -08003787 if (sysfs_slab_alias(s, name)) {
David Rientjes7b8f3b62008-12-17 22:09:46 -08003788 s->refcount--;
Christoph Lametercbb79692012-09-05 00:18:32 +00003789 s = NULL;
David Rientjes7b8f3b62008-12-17 22:09:46 -08003790 }
Christoph Lametera0e1d1b2007-07-17 04:03:31 -07003791 }
Christoph Lameter6446faa2008-02-15 23:45:26 -08003792
Christoph Lametercbb79692012-09-05 00:18:32 +00003793 return s;
3794}
Pekka Enberg84c1cf62010-09-14 23:21:12 +03003795
Christoph Lameter8a13a4c2012-09-04 23:18:33 +00003796int __kmem_cache_create(struct kmem_cache *s, unsigned long flags)
Christoph Lametercbb79692012-09-05 00:18:32 +00003797{
Pekka Enbergaac3a162012-09-05 12:07:44 +03003798 int err;
Christoph Lameter20cea962012-07-06 15:25:13 -05003799
Pekka Enbergaac3a162012-09-05 12:07:44 +03003800 err = kmem_cache_open(s, flags);
3801 if (err)
3802 return err;
Christoph Lameter20cea962012-07-06 15:25:13 -05003803
Christoph Lameter45530c42012-11-28 16:23:07 +00003804 /* Mutex is not taken during early boot */
3805 if (slab_state <= UP)
3806 return 0;
3807
Glauber Costa107dab52012-12-18 14:23:05 -08003808 memcg_propagate_slab_attrs(s);
Pekka Enbergaac3a162012-09-05 12:07:44 +03003809 mutex_unlock(&slab_mutex);
3810 err = sysfs_slab_add(s);
3811 mutex_lock(&slab_mutex);
Christoph Lameter20cea962012-07-06 15:25:13 -05003812
Pekka Enbergaac3a162012-09-05 12:07:44 +03003813 if (err)
3814 kmem_cache_close(s);
3815
3816 return err;
Christoph Lameter81819f02007-05-06 14:49:36 -07003817}
Christoph Lameter81819f02007-05-06 14:49:36 -07003818
Christoph Lameter81819f02007-05-06 14:49:36 -07003819#ifdef CONFIG_SMP
Christoph Lameter27390bc2007-06-01 00:47:09 -07003820/*
Christoph Lameter672bba32007-05-09 02:32:39 -07003821 * Use the cpu notifier to insure that the cpu slabs are flushed when
3822 * necessary.
Christoph Lameter81819f02007-05-06 14:49:36 -07003823 */
3824static int __cpuinit slab_cpuup_callback(struct notifier_block *nfb,
3825 unsigned long action, void *hcpu)
3826{
3827 long cpu = (long)hcpu;
Christoph Lameter5b95a4ac2007-07-17 04:03:19 -07003828 struct kmem_cache *s;
3829 unsigned long flags;
Christoph Lameter81819f02007-05-06 14:49:36 -07003830
3831 switch (action) {
3832 case CPU_UP_CANCELED:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07003833 case CPU_UP_CANCELED_FROZEN:
Christoph Lameter81819f02007-05-06 14:49:36 -07003834 case CPU_DEAD:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07003835 case CPU_DEAD_FROZEN:
Christoph Lameter18004c52012-07-06 15:25:12 -05003836 mutex_lock(&slab_mutex);
Christoph Lameter5b95a4ac2007-07-17 04:03:19 -07003837 list_for_each_entry(s, &slab_caches, list) {
3838 local_irq_save(flags);
3839 __flush_cpu_slab(s, cpu);
3840 local_irq_restore(flags);
3841 }
Christoph Lameter18004c52012-07-06 15:25:12 -05003842 mutex_unlock(&slab_mutex);
Christoph Lameter81819f02007-05-06 14:49:36 -07003843 break;
3844 default:
3845 break;
3846 }
3847 return NOTIFY_OK;
3848}
3849
Pekka Enberg06428782008-01-07 23:20:27 -08003850static struct notifier_block __cpuinitdata slab_notifier = {
Ingo Molnar3adbefe2008-02-05 17:57:39 -08003851 .notifier_call = slab_cpuup_callback
Pekka Enberg06428782008-01-07 23:20:27 -08003852};
Christoph Lameter81819f02007-05-06 14:49:36 -07003853
3854#endif
3855
Eduard - Gabriel Munteanuce71e272008-08-19 20:43:25 +03003856void *__kmalloc_track_caller(size_t size, gfp_t gfpflags, unsigned long caller)
Christoph Lameter81819f02007-05-06 14:49:36 -07003857{
Christoph Lameteraadb4bc2007-10-16 01:24:38 -07003858 struct kmem_cache *s;
Eduard - Gabriel Munteanu94b528d2008-08-24 20:49:35 +03003859 void *ret;
Christoph Lameteraadb4bc2007-10-16 01:24:38 -07003860
Christoph Lameter95a05b42013-01-10 19:14:19 +00003861 if (unlikely(size > KMALLOC_MAX_CACHE_SIZE))
Pekka Enbergeada35e2008-02-11 22:47:46 +02003862 return kmalloc_large(size, gfpflags);
3863
Christoph Lameter2c59dd62013-01-10 19:14:19 +00003864 s = kmalloc_slab(size, gfpflags);
Christoph Lameter81819f02007-05-06 14:49:36 -07003865
Satyam Sharma2408c552007-10-16 01:24:44 -07003866 if (unlikely(ZERO_OR_NULL_PTR(s)))
Christoph Lameter6cb8f912007-07-17 04:03:22 -07003867 return s;
Christoph Lameter81819f02007-05-06 14:49:36 -07003868
Ezequiel Garcia2b847c32012-09-08 17:47:58 -03003869 ret = slab_alloc(s, gfpflags, caller);
Eduard - Gabriel Munteanu94b528d2008-08-24 20:49:35 +03003870
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003871 /* Honor the call site pointer we received. */
Eduard - Gabriel Munteanuca2b84cb2009-03-23 15:12:24 +02003872 trace_kmalloc(caller, ret, size, s->size, gfpflags);
Eduard - Gabriel Munteanu94b528d2008-08-24 20:49:35 +03003873
3874 return ret;
Christoph Lameter81819f02007-05-06 14:49:36 -07003875}
3876
Namhyung Kim5d1f57e2010-09-29 21:02:15 +09003877#ifdef CONFIG_NUMA
Christoph Lameter81819f02007-05-06 14:49:36 -07003878void *__kmalloc_node_track_caller(size_t size, gfp_t gfpflags,
Eduard - Gabriel Munteanuce71e272008-08-19 20:43:25 +03003879 int node, unsigned long caller)
Christoph Lameter81819f02007-05-06 14:49:36 -07003880{
Christoph Lameteraadb4bc2007-10-16 01:24:38 -07003881 struct kmem_cache *s;
Eduard - Gabriel Munteanu94b528d2008-08-24 20:49:35 +03003882 void *ret;
Christoph Lameteraadb4bc2007-10-16 01:24:38 -07003883
Christoph Lameter95a05b42013-01-10 19:14:19 +00003884 if (unlikely(size > KMALLOC_MAX_CACHE_SIZE)) {
Xiaotian Fengd3e14aa2010-04-08 17:26:44 +08003885 ret = kmalloc_large_node(size, gfpflags, node);
3886
3887 trace_kmalloc_node(caller, ret,
3888 size, PAGE_SIZE << get_order(size),
3889 gfpflags, node);
3890
3891 return ret;
3892 }
Pekka Enbergeada35e2008-02-11 22:47:46 +02003893
Christoph Lameter2c59dd62013-01-10 19:14:19 +00003894 s = kmalloc_slab(size, gfpflags);
Christoph Lameter81819f02007-05-06 14:49:36 -07003895
Satyam Sharma2408c552007-10-16 01:24:44 -07003896 if (unlikely(ZERO_OR_NULL_PTR(s)))
Christoph Lameter6cb8f912007-07-17 04:03:22 -07003897 return s;
Christoph Lameter81819f02007-05-06 14:49:36 -07003898
Ezequiel Garcia2b847c32012-09-08 17:47:58 -03003899 ret = slab_alloc_node(s, gfpflags, node, caller);
Eduard - Gabriel Munteanu94b528d2008-08-24 20:49:35 +03003900
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003901 /* Honor the call site pointer we received. */
Eduard - Gabriel Munteanuca2b84cb2009-03-23 15:12:24 +02003902 trace_kmalloc_node(caller, ret, size, s->size, gfpflags, node);
Eduard - Gabriel Munteanu94b528d2008-08-24 20:49:35 +03003903
3904 return ret;
Christoph Lameter81819f02007-05-06 14:49:36 -07003905}
Namhyung Kim5d1f57e2010-09-29 21:02:15 +09003906#endif
Christoph Lameter81819f02007-05-06 14:49:36 -07003907
Christoph Lameterab4d5ed2010-10-05 13:57:26 -05003908#ifdef CONFIG_SYSFS
Christoph Lameter205ab992008-04-14 19:11:40 +03003909static int count_inuse(struct page *page)
3910{
3911 return page->inuse;
3912}
3913
3914static int count_total(struct page *page)
3915{
3916 return page->objects;
3917}
Christoph Lameterab4d5ed2010-10-05 13:57:26 -05003918#endif
Christoph Lameter205ab992008-04-14 19:11:40 +03003919
Christoph Lameterab4d5ed2010-10-05 13:57:26 -05003920#ifdef CONFIG_SLUB_DEBUG
Christoph Lameter434e2452007-07-17 04:03:30 -07003921static int validate_slab(struct kmem_cache *s, struct page *page,
3922 unsigned long *map)
Christoph Lameter53e15af2007-05-06 14:49:43 -07003923{
3924 void *p;
Christoph Lametera973e9d2008-03-01 13:40:44 -08003925 void *addr = page_address(page);
Christoph Lameter53e15af2007-05-06 14:49:43 -07003926
3927 if (!check_slab(s, page) ||
3928 !on_freelist(s, page, NULL))
3929 return 0;
3930
3931 /* Now we know that a valid freelist exists */
Christoph Lameter39b26462008-04-14 19:11:30 +03003932 bitmap_zero(map, page->objects);
Christoph Lameter53e15af2007-05-06 14:49:43 -07003933
Christoph Lameter5f80b132011-04-15 14:48:13 -05003934 get_map(s, page, map);
3935 for_each_object(p, s, addr, page->objects) {
3936 if (test_bit(slab_index(p, s, addr), map))
3937 if (!check_object(s, page, p, SLUB_RED_INACTIVE))
3938 return 0;
Christoph Lameter53e15af2007-05-06 14:49:43 -07003939 }
3940
Christoph Lameter224a88b2008-04-14 19:11:31 +03003941 for_each_object(p, s, addr, page->objects)
Christoph Lameter7656c722007-05-09 02:32:40 -07003942 if (!test_bit(slab_index(p, s, addr), map))
Tero Roponen37d57442010-12-01 20:04:20 +02003943 if (!check_object(s, page, p, SLUB_RED_ACTIVE))
Christoph Lameter53e15af2007-05-06 14:49:43 -07003944 return 0;
3945 return 1;
3946}
3947
Christoph Lameter434e2452007-07-17 04:03:30 -07003948static void validate_slab_slab(struct kmem_cache *s, struct page *page,
3949 unsigned long *map)
Christoph Lameter53e15af2007-05-06 14:49:43 -07003950{
Christoph Lameter881db7f2011-06-01 12:25:53 -05003951 slab_lock(page);
3952 validate_slab(s, page, map);
3953 slab_unlock(page);
Christoph Lameter53e15af2007-05-06 14:49:43 -07003954}
3955
Christoph Lameter434e2452007-07-17 04:03:30 -07003956static int validate_slab_node(struct kmem_cache *s,
3957 struct kmem_cache_node *n, unsigned long *map)
Christoph Lameter53e15af2007-05-06 14:49:43 -07003958{
3959 unsigned long count = 0;
3960 struct page *page;
3961 unsigned long flags;
3962
Thomas Gleixner57be9db2012-10-25 10:32:35 +01003963 raw_spin_lock_irqsave(&n->list_lock, flags);
Christoph Lameter53e15af2007-05-06 14:49:43 -07003964
3965 list_for_each_entry(page, &n->partial, lru) {
Christoph Lameter434e2452007-07-17 04:03:30 -07003966 validate_slab_slab(s, page, map);
Christoph Lameter53e15af2007-05-06 14:49:43 -07003967 count++;
3968 }
3969 if (count != n->nr_partial)
3970 printk(KERN_ERR "SLUB %s: %ld partial slabs counted but "
3971 "counter=%ld\n", s->name, count, n->nr_partial);
3972
3973 if (!(s->flags & SLAB_STORE_USER))
3974 goto out;
3975
3976 list_for_each_entry(page, &n->full, lru) {
Christoph Lameter434e2452007-07-17 04:03:30 -07003977 validate_slab_slab(s, page, map);
Christoph Lameter53e15af2007-05-06 14:49:43 -07003978 count++;
3979 }
3980 if (count != atomic_long_read(&n->nr_slabs))
3981 printk(KERN_ERR "SLUB: %s %ld slabs counted but "
3982 "counter=%ld\n", s->name, count,
3983 atomic_long_read(&n->nr_slabs));
3984
3985out:
Thomas Gleixner57be9db2012-10-25 10:32:35 +01003986 raw_spin_unlock_irqrestore(&n->list_lock, flags);
Christoph Lameter53e15af2007-05-06 14:49:43 -07003987 return count;
3988}
3989
Christoph Lameter434e2452007-07-17 04:03:30 -07003990static long validate_slab_cache(struct kmem_cache *s)
Christoph Lameter53e15af2007-05-06 14:49:43 -07003991{
3992 int node;
3993 unsigned long count = 0;
Christoph Lameter205ab992008-04-14 19:11:40 +03003994 unsigned long *map = kmalloc(BITS_TO_LONGS(oo_objects(s->max)) *
Christoph Lameter434e2452007-07-17 04:03:30 -07003995 sizeof(unsigned long), GFP_KERNEL);
3996
3997 if (!map)
3998 return -ENOMEM;
Christoph Lameter53e15af2007-05-06 14:49:43 -07003999
4000 flush_all(s);
Christoph Lameterf64dc582007-10-16 01:25:33 -07004001 for_each_node_state(node, N_NORMAL_MEMORY) {
Christoph Lameter53e15af2007-05-06 14:49:43 -07004002 struct kmem_cache_node *n = get_node(s, node);
4003
Christoph Lameter434e2452007-07-17 04:03:30 -07004004 count += validate_slab_node(s, n, map);
Christoph Lameter53e15af2007-05-06 14:49:43 -07004005 }
Christoph Lameter434e2452007-07-17 04:03:30 -07004006 kfree(map);
Christoph Lameter53e15af2007-05-06 14:49:43 -07004007 return count;
4008}
Christoph Lameter88a420e2007-05-06 14:49:45 -07004009/*
Christoph Lameter672bba32007-05-09 02:32:39 -07004010 * Generate lists of code addresses where slabcache objects are allocated
Christoph Lameter88a420e2007-05-06 14:49:45 -07004011 * and freed.
4012 */
4013
4014struct location {
4015 unsigned long count;
Eduard - Gabriel Munteanuce71e272008-08-19 20:43:25 +03004016 unsigned long addr;
Christoph Lameter45edfa52007-05-09 02:32:45 -07004017 long long sum_time;
4018 long min_time;
4019 long max_time;
4020 long min_pid;
4021 long max_pid;
Rusty Russell174596a2009-01-01 10:12:29 +10304022 DECLARE_BITMAP(cpus, NR_CPUS);
Christoph Lameter45edfa52007-05-09 02:32:45 -07004023 nodemask_t nodes;
Christoph Lameter88a420e2007-05-06 14:49:45 -07004024};
4025
4026struct loc_track {
4027 unsigned long max;
4028 unsigned long count;
4029 struct location *loc;
4030};
4031
4032static void free_loc_track(struct loc_track *t)
4033{
4034 if (t->max)
4035 free_pages((unsigned long)t->loc,
4036 get_order(sizeof(struct location) * t->max));
4037}
4038
Christoph Lameter68dff6a2007-07-17 04:03:20 -07004039static int alloc_loc_track(struct loc_track *t, unsigned long max, gfp_t flags)
Christoph Lameter88a420e2007-05-06 14:49:45 -07004040{
4041 struct location *l;
4042 int order;
4043
Christoph Lameter88a420e2007-05-06 14:49:45 -07004044 order = get_order(sizeof(struct location) * max);
4045
Christoph Lameter68dff6a2007-07-17 04:03:20 -07004046 l = (void *)__get_free_pages(flags, order);
Christoph Lameter88a420e2007-05-06 14:49:45 -07004047 if (!l)
4048 return 0;
4049
4050 if (t->count) {
4051 memcpy(l, t->loc, sizeof(struct location) * t->count);
4052 free_loc_track(t);
4053 }
4054 t->max = max;
4055 t->loc = l;
4056 return 1;
4057}
4058
4059static int add_location(struct loc_track *t, struct kmem_cache *s,
Christoph Lameter45edfa52007-05-09 02:32:45 -07004060 const struct track *track)
Christoph Lameter88a420e2007-05-06 14:49:45 -07004061{
4062 long start, end, pos;
4063 struct location *l;
Eduard - Gabriel Munteanuce71e272008-08-19 20:43:25 +03004064 unsigned long caddr;
Christoph Lameter45edfa52007-05-09 02:32:45 -07004065 unsigned long age = jiffies - track->when;
Christoph Lameter88a420e2007-05-06 14:49:45 -07004066
4067 start = -1;
4068 end = t->count;
4069
4070 for ( ; ; ) {
4071 pos = start + (end - start + 1) / 2;
4072
4073 /*
4074 * There is nothing at "end". If we end up there
4075 * we need to add something to before end.
4076 */
4077 if (pos == end)
4078 break;
4079
4080 caddr = t->loc[pos].addr;
Christoph Lameter45edfa52007-05-09 02:32:45 -07004081 if (track->addr == caddr) {
4082
4083 l = &t->loc[pos];
4084 l->count++;
4085 if (track->when) {
4086 l->sum_time += age;
4087 if (age < l->min_time)
4088 l->min_time = age;
4089 if (age > l->max_time)
4090 l->max_time = age;
4091
4092 if (track->pid < l->min_pid)
4093 l->min_pid = track->pid;
4094 if (track->pid > l->max_pid)
4095 l->max_pid = track->pid;
4096
Rusty Russell174596a2009-01-01 10:12:29 +10304097 cpumask_set_cpu(track->cpu,
4098 to_cpumask(l->cpus));
Christoph Lameter45edfa52007-05-09 02:32:45 -07004099 }
4100 node_set(page_to_nid(virt_to_page(track)), l->nodes);
Christoph Lameter88a420e2007-05-06 14:49:45 -07004101 return 1;
4102 }
4103
Christoph Lameter45edfa52007-05-09 02:32:45 -07004104 if (track->addr < caddr)
Christoph Lameter88a420e2007-05-06 14:49:45 -07004105 end = pos;
4106 else
4107 start = pos;
4108 }
4109
4110 /*
Christoph Lameter672bba32007-05-09 02:32:39 -07004111 * Not found. Insert new tracking element.
Christoph Lameter88a420e2007-05-06 14:49:45 -07004112 */
Christoph Lameter68dff6a2007-07-17 04:03:20 -07004113 if (t->count >= t->max && !alloc_loc_track(t, 2 * t->max, GFP_ATOMIC))
Christoph Lameter88a420e2007-05-06 14:49:45 -07004114 return 0;
4115
4116 l = t->loc + pos;
4117 if (pos < t->count)
4118 memmove(l + 1, l,
4119 (t->count - pos) * sizeof(struct location));
4120 t->count++;
4121 l->count = 1;
Christoph Lameter45edfa52007-05-09 02:32:45 -07004122 l->addr = track->addr;
4123 l->sum_time = age;
4124 l->min_time = age;
4125 l->max_time = age;
4126 l->min_pid = track->pid;
4127 l->max_pid = track->pid;
Rusty Russell174596a2009-01-01 10:12:29 +10304128 cpumask_clear(to_cpumask(l->cpus));
4129 cpumask_set_cpu(track->cpu, to_cpumask(l->cpus));
Christoph Lameter45edfa52007-05-09 02:32:45 -07004130 nodes_clear(l->nodes);
4131 node_set(page_to_nid(virt_to_page(track)), l->nodes);
Christoph Lameter88a420e2007-05-06 14:49:45 -07004132 return 1;
4133}
4134
4135static void process_slab(struct loc_track *t, struct kmem_cache *s,
Eric Dumazetbbd7d572010-03-24 22:25:47 +01004136 struct page *page, enum track_item alloc,
Namhyung Kima5dd5c12010-09-29 21:02:13 +09004137 unsigned long *map)
Christoph Lameter88a420e2007-05-06 14:49:45 -07004138{
Christoph Lametera973e9d2008-03-01 13:40:44 -08004139 void *addr = page_address(page);
Christoph Lameter88a420e2007-05-06 14:49:45 -07004140 void *p;
4141
Christoph Lameter39b26462008-04-14 19:11:30 +03004142 bitmap_zero(map, page->objects);
Christoph Lameter5f80b132011-04-15 14:48:13 -05004143 get_map(s, page, map);
Christoph Lameter88a420e2007-05-06 14:49:45 -07004144
Christoph Lameter224a88b2008-04-14 19:11:31 +03004145 for_each_object(p, s, addr, page->objects)
Christoph Lameter45edfa52007-05-09 02:32:45 -07004146 if (!test_bit(slab_index(p, s, addr), map))
4147 add_location(t, s, get_track(s, p, alloc));
Christoph Lameter88a420e2007-05-06 14:49:45 -07004148}
4149
4150static int list_locations(struct kmem_cache *s, char *buf,
4151 enum track_item alloc)
4152{
Harvey Harrisone374d482008-01-31 15:20:50 -08004153 int len = 0;
Christoph Lameter88a420e2007-05-06 14:49:45 -07004154 unsigned long i;
Christoph Lameter68dff6a2007-07-17 04:03:20 -07004155 struct loc_track t = { 0, 0, NULL };
Christoph Lameter88a420e2007-05-06 14:49:45 -07004156 int node;
Eric Dumazetbbd7d572010-03-24 22:25:47 +01004157 unsigned long *map = kmalloc(BITS_TO_LONGS(oo_objects(s->max)) *
4158 sizeof(unsigned long), GFP_KERNEL);
Christoph Lameter88a420e2007-05-06 14:49:45 -07004159
Eric Dumazetbbd7d572010-03-24 22:25:47 +01004160 if (!map || !alloc_loc_track(&t, PAGE_SIZE / sizeof(struct location),
4161 GFP_TEMPORARY)) {
4162 kfree(map);
Christoph Lameter68dff6a2007-07-17 04:03:20 -07004163 return sprintf(buf, "Out of memory\n");
Eric Dumazetbbd7d572010-03-24 22:25:47 +01004164 }
Christoph Lameter88a420e2007-05-06 14:49:45 -07004165 /* Push back cpu slabs */
4166 flush_all(s);
4167
Christoph Lameterf64dc582007-10-16 01:25:33 -07004168 for_each_node_state(node, N_NORMAL_MEMORY) {
Christoph Lameter88a420e2007-05-06 14:49:45 -07004169 struct kmem_cache_node *n = get_node(s, node);
4170 unsigned long flags;
4171 struct page *page;
4172
Christoph Lameter9e869432007-08-22 14:01:56 -07004173 if (!atomic_long_read(&n->nr_slabs))
Christoph Lameter88a420e2007-05-06 14:49:45 -07004174 continue;
4175
Thomas Gleixner57be9db2012-10-25 10:32:35 +01004176 raw_spin_lock_irqsave(&n->list_lock, flags);
Christoph Lameter88a420e2007-05-06 14:49:45 -07004177 list_for_each_entry(page, &n->partial, lru)
Eric Dumazetbbd7d572010-03-24 22:25:47 +01004178 process_slab(&t, s, page, alloc, map);
Christoph Lameter88a420e2007-05-06 14:49:45 -07004179 list_for_each_entry(page, &n->full, lru)
Eric Dumazetbbd7d572010-03-24 22:25:47 +01004180 process_slab(&t, s, page, alloc, map);
Thomas Gleixner57be9db2012-10-25 10:32:35 +01004181 raw_spin_unlock_irqrestore(&n->list_lock, flags);
Christoph Lameter88a420e2007-05-06 14:49:45 -07004182 }
4183
4184 for (i = 0; i < t.count; i++) {
Christoph Lameter45edfa52007-05-09 02:32:45 -07004185 struct location *l = &t.loc[i];
Christoph Lameter88a420e2007-05-06 14:49:45 -07004186
Hugh Dickins9c246242008-12-09 13:14:27 -08004187 if (len > PAGE_SIZE - KSYM_SYMBOL_LEN - 100)
Christoph Lameter88a420e2007-05-06 14:49:45 -07004188 break;
Harvey Harrisone374d482008-01-31 15:20:50 -08004189 len += sprintf(buf + len, "%7ld ", l->count);
Christoph Lameter45edfa52007-05-09 02:32:45 -07004190
4191 if (l->addr)
Joe Perches62c70bc2011-01-13 15:45:52 -08004192 len += sprintf(buf + len, "%pS", (void *)l->addr);
Christoph Lameter88a420e2007-05-06 14:49:45 -07004193 else
Harvey Harrisone374d482008-01-31 15:20:50 -08004194 len += sprintf(buf + len, "<not-available>");
Christoph Lameter45edfa52007-05-09 02:32:45 -07004195
4196 if (l->sum_time != l->min_time) {
Harvey Harrisone374d482008-01-31 15:20:50 -08004197 len += sprintf(buf + len, " age=%ld/%ld/%ld",
Roman Zippelf8bd2252008-05-01 04:34:31 -07004198 l->min_time,
4199 (long)div_u64(l->sum_time, l->count),
4200 l->max_time);
Christoph Lameter45edfa52007-05-09 02:32:45 -07004201 } else
Harvey Harrisone374d482008-01-31 15:20:50 -08004202 len += sprintf(buf + len, " age=%ld",
Christoph Lameter45edfa52007-05-09 02:32:45 -07004203 l->min_time);
4204
4205 if (l->min_pid != l->max_pid)
Harvey Harrisone374d482008-01-31 15:20:50 -08004206 len += sprintf(buf + len, " pid=%ld-%ld",
Christoph Lameter45edfa52007-05-09 02:32:45 -07004207 l->min_pid, l->max_pid);
4208 else
Harvey Harrisone374d482008-01-31 15:20:50 -08004209 len += sprintf(buf + len, " pid=%ld",
Christoph Lameter45edfa52007-05-09 02:32:45 -07004210 l->min_pid);
4211
Rusty Russell174596a2009-01-01 10:12:29 +10304212 if (num_online_cpus() > 1 &&
4213 !cpumask_empty(to_cpumask(l->cpus)) &&
Harvey Harrisone374d482008-01-31 15:20:50 -08004214 len < PAGE_SIZE - 60) {
4215 len += sprintf(buf + len, " cpus=");
4216 len += cpulist_scnprintf(buf + len, PAGE_SIZE - len - 50,
Rusty Russell174596a2009-01-01 10:12:29 +10304217 to_cpumask(l->cpus));
Christoph Lameter45edfa52007-05-09 02:32:45 -07004218 }
4219
Christoph Lameter62bc62a2009-06-16 15:32:15 -07004220 if (nr_online_nodes > 1 && !nodes_empty(l->nodes) &&
Harvey Harrisone374d482008-01-31 15:20:50 -08004221 len < PAGE_SIZE - 60) {
4222 len += sprintf(buf + len, " nodes=");
4223 len += nodelist_scnprintf(buf + len, PAGE_SIZE - len - 50,
Christoph Lameter45edfa52007-05-09 02:32:45 -07004224 l->nodes);
4225 }
4226
Harvey Harrisone374d482008-01-31 15:20:50 -08004227 len += sprintf(buf + len, "\n");
Christoph Lameter88a420e2007-05-06 14:49:45 -07004228 }
4229
4230 free_loc_track(&t);
Eric Dumazetbbd7d572010-03-24 22:25:47 +01004231 kfree(map);
Christoph Lameter88a420e2007-05-06 14:49:45 -07004232 if (!t.count)
Harvey Harrisone374d482008-01-31 15:20:50 -08004233 len += sprintf(buf, "No data\n");
4234 return len;
Christoph Lameter88a420e2007-05-06 14:49:45 -07004235}
Christoph Lameterab4d5ed2010-10-05 13:57:26 -05004236#endif
Christoph Lameter88a420e2007-05-06 14:49:45 -07004237
Christoph Lametera5a84752010-10-05 13:57:27 -05004238#ifdef SLUB_RESILIENCY_TEST
4239static void resiliency_test(void)
4240{
4241 u8 *p;
4242
Christoph Lameter95a05b42013-01-10 19:14:19 +00004243 BUILD_BUG_ON(KMALLOC_MIN_SIZE > 16 || KMALLOC_SHIFT_HIGH < 10);
Christoph Lametera5a84752010-10-05 13:57:27 -05004244
4245 printk(KERN_ERR "SLUB resiliency testing\n");
4246 printk(KERN_ERR "-----------------------\n");
4247 printk(KERN_ERR "A. Corruption after allocation\n");
4248
4249 p = kzalloc(16, GFP_KERNEL);
4250 p[16] = 0x12;
4251 printk(KERN_ERR "\n1. kmalloc-16: Clobber Redzone/next pointer"
4252 " 0x12->0x%p\n\n", p + 16);
4253
4254 validate_slab_cache(kmalloc_caches[4]);
4255
4256 /* Hmmm... The next two are dangerous */
4257 p = kzalloc(32, GFP_KERNEL);
4258 p[32 + sizeof(void *)] = 0x34;
4259 printk(KERN_ERR "\n2. kmalloc-32: Clobber next pointer/next slab"
4260 " 0x34 -> -0x%p\n", p);
4261 printk(KERN_ERR
4262 "If allocated object is overwritten then not detectable\n\n");
4263
4264 validate_slab_cache(kmalloc_caches[5]);
4265 p = kzalloc(64, GFP_KERNEL);
4266 p += 64 + (get_cycles() & 0xff) * sizeof(void *);
4267 *p = 0x56;
4268 printk(KERN_ERR "\n3. kmalloc-64: corrupting random byte 0x56->0x%p\n",
4269 p);
4270 printk(KERN_ERR
4271 "If allocated object is overwritten then not detectable\n\n");
4272 validate_slab_cache(kmalloc_caches[6]);
4273
4274 printk(KERN_ERR "\nB. Corruption after free\n");
4275 p = kzalloc(128, GFP_KERNEL);
4276 kfree(p);
4277 *p = 0x78;
4278 printk(KERN_ERR "1. kmalloc-128: Clobber first word 0x78->0x%p\n\n", p);
4279 validate_slab_cache(kmalloc_caches[7]);
4280
4281 p = kzalloc(256, GFP_KERNEL);
4282 kfree(p);
4283 p[50] = 0x9a;
4284 printk(KERN_ERR "\n2. kmalloc-256: Clobber 50th byte 0x9a->0x%p\n\n",
4285 p);
4286 validate_slab_cache(kmalloc_caches[8]);
4287
4288 p = kzalloc(512, GFP_KERNEL);
4289 kfree(p);
4290 p[512] = 0xab;
4291 printk(KERN_ERR "\n3. kmalloc-512: Clobber redzone 0xab->0x%p\n\n", p);
4292 validate_slab_cache(kmalloc_caches[9]);
4293}
4294#else
4295#ifdef CONFIG_SYSFS
4296static void resiliency_test(void) {};
4297#endif
4298#endif
4299
Christoph Lameterab4d5ed2010-10-05 13:57:26 -05004300#ifdef CONFIG_SYSFS
Christoph Lameter81819f02007-05-06 14:49:36 -07004301enum slab_stat_type {
Christoph Lameter205ab992008-04-14 19:11:40 +03004302 SL_ALL, /* All slabs */
4303 SL_PARTIAL, /* Only partially allocated slabs */
4304 SL_CPU, /* Only slabs used for cpu caches */
4305 SL_OBJECTS, /* Determine allocated objects not slabs */
4306 SL_TOTAL /* Determine object capacity not slabs */
Christoph Lameter81819f02007-05-06 14:49:36 -07004307};
4308
Christoph Lameter205ab992008-04-14 19:11:40 +03004309#define SO_ALL (1 << SL_ALL)
Christoph Lameter81819f02007-05-06 14:49:36 -07004310#define SO_PARTIAL (1 << SL_PARTIAL)
4311#define SO_CPU (1 << SL_CPU)
4312#define SO_OBJECTS (1 << SL_OBJECTS)
Christoph Lameter205ab992008-04-14 19:11:40 +03004313#define SO_TOTAL (1 << SL_TOTAL)
Christoph Lameter81819f02007-05-06 14:49:36 -07004314
Cyrill Gorcunov62e5c4b2008-03-02 23:28:24 +03004315static ssize_t show_slab_objects(struct kmem_cache *s,
4316 char *buf, unsigned long flags)
Christoph Lameter81819f02007-05-06 14:49:36 -07004317{
4318 unsigned long total = 0;
Christoph Lameter81819f02007-05-06 14:49:36 -07004319 int node;
4320 int x;
4321 unsigned long *nodes;
4322 unsigned long *per_cpu;
4323
4324 nodes = kzalloc(2 * sizeof(unsigned long) * nr_node_ids, GFP_KERNEL);
Cyrill Gorcunov62e5c4b2008-03-02 23:28:24 +03004325 if (!nodes)
4326 return -ENOMEM;
Christoph Lameter81819f02007-05-06 14:49:36 -07004327 per_cpu = nodes + nr_node_ids;
4328
Christoph Lameter205ab992008-04-14 19:11:40 +03004329 if (flags & SO_CPU) {
4330 int cpu;
Christoph Lameter81819f02007-05-06 14:49:36 -07004331
Christoph Lameter205ab992008-04-14 19:11:40 +03004332 for_each_possible_cpu(cpu) {
Christoph Lameter9dfc6e62009-12-18 16:26:20 -06004333 struct kmem_cache_cpu *c = per_cpu_ptr(s->cpu_slab, cpu);
Christoph Lameterec3ab082012-05-09 10:09:56 -05004334 int node;
Christoph Lameter49e22582011-08-09 16:12:27 -05004335 struct page *page;
Christoph Lameterdfb4f092007-10-16 01:26:05 -07004336
Eric Dumazetbc6697d2011-11-22 16:02:02 +01004337 page = ACCESS_ONCE(c->page);
Christoph Lameterec3ab082012-05-09 10:09:56 -05004338 if (!page)
4339 continue;
Christoph Lameter205ab992008-04-14 19:11:40 +03004340
Christoph Lameterec3ab082012-05-09 10:09:56 -05004341 node = page_to_nid(page);
4342 if (flags & SO_TOTAL)
4343 x = page->objects;
4344 else if (flags & SO_OBJECTS)
4345 x = page->inuse;
4346 else
4347 x = 1;
Christoph Lameter49e22582011-08-09 16:12:27 -05004348
Christoph Lameterec3ab082012-05-09 10:09:56 -05004349 total += x;
4350 nodes[node] += x;
4351
4352 page = ACCESS_ONCE(c->partial);
Christoph Lameter49e22582011-08-09 16:12:27 -05004353 if (page) {
Li Zefand6d76c62013-09-10 11:43:37 +08004354 node = page_to_nid(page);
4355 if (flags & SO_TOTAL)
4356 WARN_ON_ONCE(1);
4357 else if (flags & SO_OBJECTS)
4358 WARN_ON_ONCE(1);
4359 else
4360 x = page->pages;
Eric Dumazetbc6697d2011-11-22 16:02:02 +01004361 total += x;
4362 nodes[node] += x;
Christoph Lameter49e22582011-08-09 16:12:27 -05004363 }
Christoph Lameterec3ab082012-05-09 10:09:56 -05004364
Eric Dumazetbc6697d2011-11-22 16:02:02 +01004365 per_cpu[node]++;
Christoph Lameter81819f02007-05-06 14:49:36 -07004366 }
4367 }
4368
Christoph Lameter04d94872011-01-10 10:15:15 -06004369 lock_memory_hotplug();
Christoph Lameterab4d5ed2010-10-05 13:57:26 -05004370#ifdef CONFIG_SLUB_DEBUG
Christoph Lameter205ab992008-04-14 19:11:40 +03004371 if (flags & SO_ALL) {
4372 for_each_node_state(node, N_NORMAL_MEMORY) {
4373 struct kmem_cache_node *n = get_node(s, node);
Christoph Lameter81819f02007-05-06 14:49:36 -07004374
Christoph Lameter205ab992008-04-14 19:11:40 +03004375 if (flags & SO_TOTAL)
4376 x = atomic_long_read(&n->total_objects);
4377 else if (flags & SO_OBJECTS)
4378 x = atomic_long_read(&n->total_objects) -
4379 count_partial(n, count_free);
4380
4381 else
4382 x = atomic_long_read(&n->nr_slabs);
4383 total += x;
4384 nodes[node] += x;
4385 }
4386
Christoph Lameterab4d5ed2010-10-05 13:57:26 -05004387 } else
4388#endif
4389 if (flags & SO_PARTIAL) {
Christoph Lameter205ab992008-04-14 19:11:40 +03004390 for_each_node_state(node, N_NORMAL_MEMORY) {
4391 struct kmem_cache_node *n = get_node(s, node);
4392
4393 if (flags & SO_TOTAL)
4394 x = count_partial(n, count_total);
4395 else if (flags & SO_OBJECTS)
4396 x = count_partial(n, count_inuse);
Christoph Lameter81819f02007-05-06 14:49:36 -07004397 else
4398 x = n->nr_partial;
4399 total += x;
4400 nodes[node] += x;
4401 }
Christoph Lameter81819f02007-05-06 14:49:36 -07004402 }
Christoph Lameter81819f02007-05-06 14:49:36 -07004403 x = sprintf(buf, "%lu", total);
4404#ifdef CONFIG_NUMA
Christoph Lameterf64dc582007-10-16 01:25:33 -07004405 for_each_node_state(node, N_NORMAL_MEMORY)
Christoph Lameter81819f02007-05-06 14:49:36 -07004406 if (nodes[node])
4407 x += sprintf(buf + x, " N%d=%lu",
4408 node, nodes[node]);
4409#endif
Christoph Lameter04d94872011-01-10 10:15:15 -06004410 unlock_memory_hotplug();
Christoph Lameter81819f02007-05-06 14:49:36 -07004411 kfree(nodes);
4412 return x + sprintf(buf + x, "\n");
4413}
4414
Christoph Lameterab4d5ed2010-10-05 13:57:26 -05004415#ifdef CONFIG_SLUB_DEBUG
Christoph Lameter81819f02007-05-06 14:49:36 -07004416static int any_slab_objects(struct kmem_cache *s)
4417{
4418 int node;
Christoph Lameterdfb4f092007-10-16 01:26:05 -07004419
4420 for_each_online_node(node) {
Christoph Lameter81819f02007-05-06 14:49:36 -07004421 struct kmem_cache_node *n = get_node(s, node);
4422
Christoph Lameterdfb4f092007-10-16 01:26:05 -07004423 if (!n)
4424 continue;
4425
Benjamin Herrenschmidt4ea33e22008-05-06 20:42:39 -07004426 if (atomic_long_read(&n->total_objects))
Christoph Lameter81819f02007-05-06 14:49:36 -07004427 return 1;
4428 }
4429 return 0;
4430}
Christoph Lameterab4d5ed2010-10-05 13:57:26 -05004431#endif
Christoph Lameter81819f02007-05-06 14:49:36 -07004432
4433#define to_slab_attr(n) container_of(n, struct slab_attribute, attr)
Phil Carmody497888c2011-07-14 15:07:13 +03004434#define to_slab(n) container_of(n, struct kmem_cache, kobj)
Christoph Lameter81819f02007-05-06 14:49:36 -07004435
4436struct slab_attribute {
4437 struct attribute attr;
4438 ssize_t (*show)(struct kmem_cache *s, char *buf);
4439 ssize_t (*store)(struct kmem_cache *s, const char *x, size_t count);
4440};
4441
4442#define SLAB_ATTR_RO(_name) \
Vasiliy Kulikovab067e92011-09-27 21:54:53 +04004443 static struct slab_attribute _name##_attr = \
4444 __ATTR(_name, 0400, _name##_show, NULL)
Christoph Lameter81819f02007-05-06 14:49:36 -07004445
4446#define SLAB_ATTR(_name) \
4447 static struct slab_attribute _name##_attr = \
Vasiliy Kulikovab067e92011-09-27 21:54:53 +04004448 __ATTR(_name, 0600, _name##_show, _name##_store)
Christoph Lameter81819f02007-05-06 14:49:36 -07004449
Christoph Lameter81819f02007-05-06 14:49:36 -07004450static ssize_t slab_size_show(struct kmem_cache *s, char *buf)
4451{
4452 return sprintf(buf, "%d\n", s->size);
4453}
4454SLAB_ATTR_RO(slab_size);
4455
4456static ssize_t align_show(struct kmem_cache *s, char *buf)
4457{
4458 return sprintf(buf, "%d\n", s->align);
4459}
4460SLAB_ATTR_RO(align);
4461
4462static ssize_t object_size_show(struct kmem_cache *s, char *buf)
4463{
Christoph Lameter3b0efdf2012-06-13 10:24:57 -05004464 return sprintf(buf, "%d\n", s->object_size);
Christoph Lameter81819f02007-05-06 14:49:36 -07004465}
4466SLAB_ATTR_RO(object_size);
4467
4468static ssize_t objs_per_slab_show(struct kmem_cache *s, char *buf)
4469{
Christoph Lameter834f3d12008-04-14 19:11:31 +03004470 return sprintf(buf, "%d\n", oo_objects(s->oo));
Christoph Lameter81819f02007-05-06 14:49:36 -07004471}
4472SLAB_ATTR_RO(objs_per_slab);
4473
Christoph Lameter06b285d2008-04-14 19:11:41 +03004474static ssize_t order_store(struct kmem_cache *s,
4475 const char *buf, size_t length)
4476{
Christoph Lameter0121c6192008-04-29 16:11:12 -07004477 unsigned long order;
4478 int err;
4479
4480 err = strict_strtoul(buf, 10, &order);
4481 if (err)
4482 return err;
Christoph Lameter06b285d2008-04-14 19:11:41 +03004483
4484 if (order > slub_max_order || order < slub_min_order)
4485 return -EINVAL;
4486
4487 calculate_sizes(s, order);
4488 return length;
4489}
4490
Christoph Lameter81819f02007-05-06 14:49:36 -07004491static ssize_t order_show(struct kmem_cache *s, char *buf)
4492{
Christoph Lameter834f3d12008-04-14 19:11:31 +03004493 return sprintf(buf, "%d\n", oo_order(s->oo));
Christoph Lameter81819f02007-05-06 14:49:36 -07004494}
Christoph Lameter06b285d2008-04-14 19:11:41 +03004495SLAB_ATTR(order);
Christoph Lameter81819f02007-05-06 14:49:36 -07004496
David Rientjes73d342b2009-02-22 17:40:09 -08004497static ssize_t min_partial_show(struct kmem_cache *s, char *buf)
4498{
4499 return sprintf(buf, "%lu\n", s->min_partial);
4500}
4501
4502static ssize_t min_partial_store(struct kmem_cache *s, const char *buf,
4503 size_t length)
4504{
4505 unsigned long min;
4506 int err;
4507
4508 err = strict_strtoul(buf, 10, &min);
4509 if (err)
4510 return err;
4511
David Rientjesc0bdb232009-02-25 09:16:35 +02004512 set_min_partial(s, min);
David Rientjes73d342b2009-02-22 17:40:09 -08004513 return length;
4514}
4515SLAB_ATTR(min_partial);
4516
Christoph Lameter49e22582011-08-09 16:12:27 -05004517static ssize_t cpu_partial_show(struct kmem_cache *s, char *buf)
4518{
4519 return sprintf(buf, "%u\n", s->cpu_partial);
4520}
4521
4522static ssize_t cpu_partial_store(struct kmem_cache *s, const char *buf,
4523 size_t length)
4524{
4525 unsigned long objects;
4526 int err;
4527
4528 err = strict_strtoul(buf, 10, &objects);
4529 if (err)
4530 return err;
David Rientjes74ee4ef2012-01-09 13:19:45 -08004531 if (objects && kmem_cache_debug(s))
4532 return -EINVAL;
Christoph Lameter49e22582011-08-09 16:12:27 -05004533
4534 s->cpu_partial = objects;
4535 flush_all(s);
4536 return length;
4537}
4538SLAB_ATTR(cpu_partial);
4539
Christoph Lameter81819f02007-05-06 14:49:36 -07004540static ssize_t ctor_show(struct kmem_cache *s, char *buf)
4541{
Joe Perches62c70bc2011-01-13 15:45:52 -08004542 if (!s->ctor)
4543 return 0;
4544 return sprintf(buf, "%pS\n", s->ctor);
Christoph Lameter81819f02007-05-06 14:49:36 -07004545}
4546SLAB_ATTR_RO(ctor);
4547
Christoph Lameter81819f02007-05-06 14:49:36 -07004548static ssize_t aliases_show(struct kmem_cache *s, char *buf)
4549{
4550 return sprintf(buf, "%d\n", s->refcount - 1);
4551}
4552SLAB_ATTR_RO(aliases);
4553
Christoph Lameter81819f02007-05-06 14:49:36 -07004554static ssize_t partial_show(struct kmem_cache *s, char *buf)
4555{
Christoph Lameterd9acf4b2008-02-15 15:22:21 -08004556 return show_slab_objects(s, buf, SO_PARTIAL);
Christoph Lameter81819f02007-05-06 14:49:36 -07004557}
4558SLAB_ATTR_RO(partial);
4559
4560static ssize_t cpu_slabs_show(struct kmem_cache *s, char *buf)
4561{
Christoph Lameterd9acf4b2008-02-15 15:22:21 -08004562 return show_slab_objects(s, buf, SO_CPU);
Christoph Lameter81819f02007-05-06 14:49:36 -07004563}
4564SLAB_ATTR_RO(cpu_slabs);
4565
4566static ssize_t objects_show(struct kmem_cache *s, char *buf)
4567{
Christoph Lameter205ab992008-04-14 19:11:40 +03004568 return show_slab_objects(s, buf, SO_ALL|SO_OBJECTS);
Christoph Lameter81819f02007-05-06 14:49:36 -07004569}
4570SLAB_ATTR_RO(objects);
4571
Christoph Lameter205ab992008-04-14 19:11:40 +03004572static ssize_t objects_partial_show(struct kmem_cache *s, char *buf)
4573{
4574 return show_slab_objects(s, buf, SO_PARTIAL|SO_OBJECTS);
4575}
4576SLAB_ATTR_RO(objects_partial);
4577
Christoph Lameter49e22582011-08-09 16:12:27 -05004578static ssize_t slabs_cpu_partial_show(struct kmem_cache *s, char *buf)
4579{
4580 int objects = 0;
4581 int pages = 0;
4582 int cpu;
4583 int len;
4584
4585 for_each_online_cpu(cpu) {
4586 struct page *page = per_cpu_ptr(s->cpu_slab, cpu)->partial;
4587
4588 if (page) {
4589 pages += page->pages;
4590 objects += page->pobjects;
4591 }
4592 }
4593
4594 len = sprintf(buf, "%d(%d)", objects, pages);
4595
4596#ifdef CONFIG_SMP
4597 for_each_online_cpu(cpu) {
4598 struct page *page = per_cpu_ptr(s->cpu_slab, cpu) ->partial;
4599
4600 if (page && len < PAGE_SIZE - 20)
4601 len += sprintf(buf + len, " C%d=%d(%d)", cpu,
4602 page->pobjects, page->pages);
4603 }
4604#endif
4605 return len + sprintf(buf + len, "\n");
4606}
4607SLAB_ATTR_RO(slabs_cpu_partial);
4608
Christoph Lameter81819f02007-05-06 14:49:36 -07004609static ssize_t reclaim_account_show(struct kmem_cache *s, char *buf)
4610{
4611 return sprintf(buf, "%d\n", !!(s->flags & SLAB_RECLAIM_ACCOUNT));
4612}
4613
4614static ssize_t reclaim_account_store(struct kmem_cache *s,
4615 const char *buf, size_t length)
4616{
4617 s->flags &= ~SLAB_RECLAIM_ACCOUNT;
4618 if (buf[0] == '1')
4619 s->flags |= SLAB_RECLAIM_ACCOUNT;
4620 return length;
4621}
4622SLAB_ATTR(reclaim_account);
4623
4624static ssize_t hwcache_align_show(struct kmem_cache *s, char *buf)
4625{
Christoph Lameter5af60832007-05-06 14:49:56 -07004626 return sprintf(buf, "%d\n", !!(s->flags & SLAB_HWCACHE_ALIGN));
Christoph Lameter81819f02007-05-06 14:49:36 -07004627}
4628SLAB_ATTR_RO(hwcache_align);
4629
4630#ifdef CONFIG_ZONE_DMA
4631static ssize_t cache_dma_show(struct kmem_cache *s, char *buf)
4632{
4633 return sprintf(buf, "%d\n", !!(s->flags & SLAB_CACHE_DMA));
4634}
4635SLAB_ATTR_RO(cache_dma);
4636#endif
4637
4638static ssize_t destroy_by_rcu_show(struct kmem_cache *s, char *buf)
4639{
4640 return sprintf(buf, "%d\n", !!(s->flags & SLAB_DESTROY_BY_RCU));
4641}
4642SLAB_ATTR_RO(destroy_by_rcu);
4643
Lai Jiangshanab9a0f12011-03-10 15:21:48 +08004644static ssize_t reserved_show(struct kmem_cache *s, char *buf)
4645{
4646 return sprintf(buf, "%d\n", s->reserved);
4647}
4648SLAB_ATTR_RO(reserved);
4649
Christoph Lameterab4d5ed2010-10-05 13:57:26 -05004650#ifdef CONFIG_SLUB_DEBUG
Christoph Lametera5a84752010-10-05 13:57:27 -05004651static ssize_t slabs_show(struct kmem_cache *s, char *buf)
4652{
4653 return show_slab_objects(s, buf, SO_ALL);
4654}
4655SLAB_ATTR_RO(slabs);
4656
4657static ssize_t total_objects_show(struct kmem_cache *s, char *buf)
4658{
4659 return show_slab_objects(s, buf, SO_ALL|SO_TOTAL);
4660}
4661SLAB_ATTR_RO(total_objects);
4662
4663static ssize_t sanity_checks_show(struct kmem_cache *s, char *buf)
4664{
4665 return sprintf(buf, "%d\n", !!(s->flags & SLAB_DEBUG_FREE));
4666}
4667
4668static ssize_t sanity_checks_store(struct kmem_cache *s,
4669 const char *buf, size_t length)
4670{
4671 s->flags &= ~SLAB_DEBUG_FREE;
Christoph Lameterb789ef52011-06-01 12:25:49 -05004672 if (buf[0] == '1') {
4673 s->flags &= ~__CMPXCHG_DOUBLE;
Christoph Lametera5a84752010-10-05 13:57:27 -05004674 s->flags |= SLAB_DEBUG_FREE;
Christoph Lameterb789ef52011-06-01 12:25:49 -05004675 }
Christoph Lametera5a84752010-10-05 13:57:27 -05004676 return length;
4677}
4678SLAB_ATTR(sanity_checks);
4679
4680static ssize_t trace_show(struct kmem_cache *s, char *buf)
4681{
4682 return sprintf(buf, "%d\n", !!(s->flags & SLAB_TRACE));
4683}
4684
4685static ssize_t trace_store(struct kmem_cache *s, const char *buf,
4686 size_t length)
4687{
4688 s->flags &= ~SLAB_TRACE;
Christoph Lameterb789ef52011-06-01 12:25:49 -05004689 if (buf[0] == '1') {
4690 s->flags &= ~__CMPXCHG_DOUBLE;
Christoph Lametera5a84752010-10-05 13:57:27 -05004691 s->flags |= SLAB_TRACE;
Christoph Lameterb789ef52011-06-01 12:25:49 -05004692 }
Christoph Lametera5a84752010-10-05 13:57:27 -05004693 return length;
4694}
4695SLAB_ATTR(trace);
4696
Christoph Lameter81819f02007-05-06 14:49:36 -07004697static ssize_t red_zone_show(struct kmem_cache *s, char *buf)
4698{
4699 return sprintf(buf, "%d\n", !!(s->flags & SLAB_RED_ZONE));
4700}
4701
4702static ssize_t red_zone_store(struct kmem_cache *s,
4703 const char *buf, size_t length)
4704{
4705 if (any_slab_objects(s))
4706 return -EBUSY;
4707
4708 s->flags &= ~SLAB_RED_ZONE;
Christoph Lameterb789ef52011-06-01 12:25:49 -05004709 if (buf[0] == '1') {
4710 s->flags &= ~__CMPXCHG_DOUBLE;
Christoph Lameter81819f02007-05-06 14:49:36 -07004711 s->flags |= SLAB_RED_ZONE;
Christoph Lameterb789ef52011-06-01 12:25:49 -05004712 }
Christoph Lameter06b285d2008-04-14 19:11:41 +03004713 calculate_sizes(s, -1);
Christoph Lameter81819f02007-05-06 14:49:36 -07004714 return length;
4715}
4716SLAB_ATTR(red_zone);
4717
4718static ssize_t poison_show(struct kmem_cache *s, char *buf)
4719{
4720 return sprintf(buf, "%d\n", !!(s->flags & SLAB_POISON));
4721}
4722
4723static ssize_t poison_store(struct kmem_cache *s,
4724 const char *buf, size_t length)
4725{
4726 if (any_slab_objects(s))
4727 return -EBUSY;
4728
4729 s->flags &= ~SLAB_POISON;
Christoph Lameterb789ef52011-06-01 12:25:49 -05004730 if (buf[0] == '1') {
4731 s->flags &= ~__CMPXCHG_DOUBLE;
Christoph Lameter81819f02007-05-06 14:49:36 -07004732 s->flags |= SLAB_POISON;
Christoph Lameterb789ef52011-06-01 12:25:49 -05004733 }
Christoph Lameter06b285d2008-04-14 19:11:41 +03004734 calculate_sizes(s, -1);
Christoph Lameter81819f02007-05-06 14:49:36 -07004735 return length;
4736}
4737SLAB_ATTR(poison);
4738
4739static ssize_t store_user_show(struct kmem_cache *s, char *buf)
4740{
4741 return sprintf(buf, "%d\n", !!(s->flags & SLAB_STORE_USER));
4742}
4743
4744static ssize_t store_user_store(struct kmem_cache *s,
4745 const char *buf, size_t length)
4746{
4747 if (any_slab_objects(s))
4748 return -EBUSY;
4749
4750 s->flags &= ~SLAB_STORE_USER;
Christoph Lameterb789ef52011-06-01 12:25:49 -05004751 if (buf[0] == '1') {
4752 s->flags &= ~__CMPXCHG_DOUBLE;
Christoph Lameter81819f02007-05-06 14:49:36 -07004753 s->flags |= SLAB_STORE_USER;
Christoph Lameterb789ef52011-06-01 12:25:49 -05004754 }
Christoph Lameter06b285d2008-04-14 19:11:41 +03004755 calculate_sizes(s, -1);
Christoph Lameter81819f02007-05-06 14:49:36 -07004756 return length;
4757}
4758SLAB_ATTR(store_user);
4759
Christoph Lameter53e15af2007-05-06 14:49:43 -07004760static ssize_t validate_show(struct kmem_cache *s, char *buf)
4761{
4762 return 0;
4763}
4764
4765static ssize_t validate_store(struct kmem_cache *s,
4766 const char *buf, size_t length)
4767{
Christoph Lameter434e2452007-07-17 04:03:30 -07004768 int ret = -EINVAL;
4769
4770 if (buf[0] == '1') {
4771 ret = validate_slab_cache(s);
4772 if (ret >= 0)
4773 ret = length;
4774 }
4775 return ret;
Christoph Lameter53e15af2007-05-06 14:49:43 -07004776}
4777SLAB_ATTR(validate);
Christoph Lametera5a84752010-10-05 13:57:27 -05004778
4779static ssize_t alloc_calls_show(struct kmem_cache *s, char *buf)
4780{
4781 if (!(s->flags & SLAB_STORE_USER))
4782 return -ENOSYS;
4783 return list_locations(s, buf, TRACK_ALLOC);
4784}
4785SLAB_ATTR_RO(alloc_calls);
4786
4787static ssize_t free_calls_show(struct kmem_cache *s, char *buf)
4788{
4789 if (!(s->flags & SLAB_STORE_USER))
4790 return -ENOSYS;
4791 return list_locations(s, buf, TRACK_FREE);
4792}
4793SLAB_ATTR_RO(free_calls);
4794#endif /* CONFIG_SLUB_DEBUG */
4795
4796#ifdef CONFIG_FAILSLAB
4797static ssize_t failslab_show(struct kmem_cache *s, char *buf)
4798{
4799 return sprintf(buf, "%d\n", !!(s->flags & SLAB_FAILSLAB));
4800}
4801
4802static ssize_t failslab_store(struct kmem_cache *s, const char *buf,
4803 size_t length)
4804{
4805 s->flags &= ~SLAB_FAILSLAB;
4806 if (buf[0] == '1')
4807 s->flags |= SLAB_FAILSLAB;
4808 return length;
4809}
4810SLAB_ATTR(failslab);
Christoph Lameterab4d5ed2010-10-05 13:57:26 -05004811#endif
Christoph Lameter53e15af2007-05-06 14:49:43 -07004812
Christoph Lameter2086d262007-05-06 14:49:46 -07004813static ssize_t shrink_show(struct kmem_cache *s, char *buf)
4814{
4815 return 0;
4816}
4817
4818static ssize_t shrink_store(struct kmem_cache *s,
4819 const char *buf, size_t length)
4820{
4821 if (buf[0] == '1') {
4822 int rc = kmem_cache_shrink(s);
4823
4824 if (rc)
4825 return rc;
4826 } else
4827 return -EINVAL;
4828 return length;
4829}
4830SLAB_ATTR(shrink);
4831
Christoph Lameter81819f02007-05-06 14:49:36 -07004832#ifdef CONFIG_NUMA
Christoph Lameter98246012008-01-07 23:20:26 -08004833static ssize_t remote_node_defrag_ratio_show(struct kmem_cache *s, char *buf)
Christoph Lameter81819f02007-05-06 14:49:36 -07004834{
Christoph Lameter98246012008-01-07 23:20:26 -08004835 return sprintf(buf, "%d\n", s->remote_node_defrag_ratio / 10);
Christoph Lameter81819f02007-05-06 14:49:36 -07004836}
4837
Christoph Lameter98246012008-01-07 23:20:26 -08004838static ssize_t remote_node_defrag_ratio_store(struct kmem_cache *s,
Christoph Lameter81819f02007-05-06 14:49:36 -07004839 const char *buf, size_t length)
4840{
Christoph Lameter0121c6192008-04-29 16:11:12 -07004841 unsigned long ratio;
4842 int err;
Christoph Lameter81819f02007-05-06 14:49:36 -07004843
Christoph Lameter0121c6192008-04-29 16:11:12 -07004844 err = strict_strtoul(buf, 10, &ratio);
4845 if (err)
4846 return err;
4847
Christoph Lametere2cb96b2008-08-19 08:51:22 -05004848 if (ratio <= 100)
Christoph Lameter0121c6192008-04-29 16:11:12 -07004849 s->remote_node_defrag_ratio = ratio * 10;
4850
Christoph Lameter81819f02007-05-06 14:49:36 -07004851 return length;
4852}
Christoph Lameter98246012008-01-07 23:20:26 -08004853SLAB_ATTR(remote_node_defrag_ratio);
Christoph Lameter81819f02007-05-06 14:49:36 -07004854#endif
4855
Christoph Lameter8ff12cf2008-02-07 17:47:41 -08004856#ifdef CONFIG_SLUB_STATS
Christoph Lameter8ff12cf2008-02-07 17:47:41 -08004857static int show_stat(struct kmem_cache *s, char *buf, enum stat_item si)
4858{
4859 unsigned long sum = 0;
4860 int cpu;
4861 int len;
4862 int *data = kmalloc(nr_cpu_ids * sizeof(int), GFP_KERNEL);
4863
4864 if (!data)
4865 return -ENOMEM;
4866
4867 for_each_online_cpu(cpu) {
Christoph Lameter9dfc6e62009-12-18 16:26:20 -06004868 unsigned x = per_cpu_ptr(s->cpu_slab, cpu)->stat[si];
Christoph Lameter8ff12cf2008-02-07 17:47:41 -08004869
4870 data[cpu] = x;
4871 sum += x;
4872 }
4873
4874 len = sprintf(buf, "%lu", sum);
4875
Christoph Lameter50ef37b2008-04-14 18:52:05 +03004876#ifdef CONFIG_SMP
Christoph Lameter8ff12cf2008-02-07 17:47:41 -08004877 for_each_online_cpu(cpu) {
4878 if (data[cpu] && len < PAGE_SIZE - 20)
Christoph Lameter50ef37b2008-04-14 18:52:05 +03004879 len += sprintf(buf + len, " C%d=%u", cpu, data[cpu]);
Christoph Lameter8ff12cf2008-02-07 17:47:41 -08004880 }
Christoph Lameter50ef37b2008-04-14 18:52:05 +03004881#endif
Christoph Lameter8ff12cf2008-02-07 17:47:41 -08004882 kfree(data);
4883 return len + sprintf(buf + len, "\n");
4884}
4885
David Rientjes78eb00c2009-10-15 02:20:22 -07004886static void clear_stat(struct kmem_cache *s, enum stat_item si)
4887{
4888 int cpu;
4889
4890 for_each_online_cpu(cpu)
Christoph Lameter9dfc6e62009-12-18 16:26:20 -06004891 per_cpu_ptr(s->cpu_slab, cpu)->stat[si] = 0;
David Rientjes78eb00c2009-10-15 02:20:22 -07004892}
4893
Christoph Lameter8ff12cf2008-02-07 17:47:41 -08004894#define STAT_ATTR(si, text) \
4895static ssize_t text##_show(struct kmem_cache *s, char *buf) \
4896{ \
4897 return show_stat(s, buf, si); \
4898} \
David Rientjes78eb00c2009-10-15 02:20:22 -07004899static ssize_t text##_store(struct kmem_cache *s, \
4900 const char *buf, size_t length) \
4901{ \
4902 if (buf[0] != '0') \
4903 return -EINVAL; \
4904 clear_stat(s, si); \
4905 return length; \
4906} \
4907SLAB_ATTR(text); \
Christoph Lameter8ff12cf2008-02-07 17:47:41 -08004908
4909STAT_ATTR(ALLOC_FASTPATH, alloc_fastpath);
4910STAT_ATTR(ALLOC_SLOWPATH, alloc_slowpath);
4911STAT_ATTR(FREE_FASTPATH, free_fastpath);
4912STAT_ATTR(FREE_SLOWPATH, free_slowpath);
4913STAT_ATTR(FREE_FROZEN, free_frozen);
4914STAT_ATTR(FREE_ADD_PARTIAL, free_add_partial);
4915STAT_ATTR(FREE_REMOVE_PARTIAL, free_remove_partial);
4916STAT_ATTR(ALLOC_FROM_PARTIAL, alloc_from_partial);
4917STAT_ATTR(ALLOC_SLAB, alloc_slab);
4918STAT_ATTR(ALLOC_REFILL, alloc_refill);
Christoph Lametere36a2652011-06-01 12:25:57 -05004919STAT_ATTR(ALLOC_NODE_MISMATCH, alloc_node_mismatch);
Christoph Lameter8ff12cf2008-02-07 17:47:41 -08004920STAT_ATTR(FREE_SLAB, free_slab);
4921STAT_ATTR(CPUSLAB_FLUSH, cpuslab_flush);
4922STAT_ATTR(DEACTIVATE_FULL, deactivate_full);
4923STAT_ATTR(DEACTIVATE_EMPTY, deactivate_empty);
4924STAT_ATTR(DEACTIVATE_TO_HEAD, deactivate_to_head);
4925STAT_ATTR(DEACTIVATE_TO_TAIL, deactivate_to_tail);
4926STAT_ATTR(DEACTIVATE_REMOTE_FREES, deactivate_remote_frees);
Christoph Lameter03e404a2011-06-01 12:25:58 -05004927STAT_ATTR(DEACTIVATE_BYPASS, deactivate_bypass);
Christoph Lameter65c33762008-04-14 19:11:40 +03004928STAT_ATTR(ORDER_FALLBACK, order_fallback);
Christoph Lameterb789ef52011-06-01 12:25:49 -05004929STAT_ATTR(CMPXCHG_DOUBLE_CPU_FAIL, cmpxchg_double_cpu_fail);
4930STAT_ATTR(CMPXCHG_DOUBLE_FAIL, cmpxchg_double_fail);
Christoph Lameter49e22582011-08-09 16:12:27 -05004931STAT_ATTR(CPU_PARTIAL_ALLOC, cpu_partial_alloc);
4932STAT_ATTR(CPU_PARTIAL_FREE, cpu_partial_free);
Alex Shi8028dce2012-02-03 23:34:56 +08004933STAT_ATTR(CPU_PARTIAL_NODE, cpu_partial_node);
4934STAT_ATTR(CPU_PARTIAL_DRAIN, cpu_partial_drain);
Christoph Lameter8ff12cf2008-02-07 17:47:41 -08004935#endif
4936
Pekka Enberg06428782008-01-07 23:20:27 -08004937static struct attribute *slab_attrs[] = {
Christoph Lameter81819f02007-05-06 14:49:36 -07004938 &slab_size_attr.attr,
4939 &object_size_attr.attr,
4940 &objs_per_slab_attr.attr,
4941 &order_attr.attr,
David Rientjes73d342b2009-02-22 17:40:09 -08004942 &min_partial_attr.attr,
Christoph Lameter49e22582011-08-09 16:12:27 -05004943 &cpu_partial_attr.attr,
Christoph Lameter81819f02007-05-06 14:49:36 -07004944 &objects_attr.attr,
Christoph Lameter205ab992008-04-14 19:11:40 +03004945 &objects_partial_attr.attr,
Christoph Lameter81819f02007-05-06 14:49:36 -07004946 &partial_attr.attr,
4947 &cpu_slabs_attr.attr,
4948 &ctor_attr.attr,
Christoph Lameter81819f02007-05-06 14:49:36 -07004949 &aliases_attr.attr,
4950 &align_attr.attr,
Christoph Lameter81819f02007-05-06 14:49:36 -07004951 &hwcache_align_attr.attr,
4952 &reclaim_account_attr.attr,
4953 &destroy_by_rcu_attr.attr,
Christoph Lametera5a84752010-10-05 13:57:27 -05004954 &shrink_attr.attr,
Lai Jiangshanab9a0f12011-03-10 15:21:48 +08004955 &reserved_attr.attr,
Christoph Lameter49e22582011-08-09 16:12:27 -05004956 &slabs_cpu_partial_attr.attr,
Christoph Lameterab4d5ed2010-10-05 13:57:26 -05004957#ifdef CONFIG_SLUB_DEBUG
Christoph Lametera5a84752010-10-05 13:57:27 -05004958 &total_objects_attr.attr,
4959 &slabs_attr.attr,
4960 &sanity_checks_attr.attr,
4961 &trace_attr.attr,
Christoph Lameter81819f02007-05-06 14:49:36 -07004962 &red_zone_attr.attr,
4963 &poison_attr.attr,
4964 &store_user_attr.attr,
Christoph Lameter53e15af2007-05-06 14:49:43 -07004965 &validate_attr.attr,
Christoph Lameter88a420e2007-05-06 14:49:45 -07004966 &alloc_calls_attr.attr,
4967 &free_calls_attr.attr,
Christoph Lameterab4d5ed2010-10-05 13:57:26 -05004968#endif
Christoph Lameter81819f02007-05-06 14:49:36 -07004969#ifdef CONFIG_ZONE_DMA
4970 &cache_dma_attr.attr,
4971#endif
4972#ifdef CONFIG_NUMA
Christoph Lameter98246012008-01-07 23:20:26 -08004973 &remote_node_defrag_ratio_attr.attr,
Christoph Lameter81819f02007-05-06 14:49:36 -07004974#endif
Christoph Lameter8ff12cf2008-02-07 17:47:41 -08004975#ifdef CONFIG_SLUB_STATS
4976 &alloc_fastpath_attr.attr,
4977 &alloc_slowpath_attr.attr,
4978 &free_fastpath_attr.attr,
4979 &free_slowpath_attr.attr,
4980 &free_frozen_attr.attr,
4981 &free_add_partial_attr.attr,
4982 &free_remove_partial_attr.attr,
4983 &alloc_from_partial_attr.attr,
4984 &alloc_slab_attr.attr,
4985 &alloc_refill_attr.attr,
Christoph Lametere36a2652011-06-01 12:25:57 -05004986 &alloc_node_mismatch_attr.attr,
Christoph Lameter8ff12cf2008-02-07 17:47:41 -08004987 &free_slab_attr.attr,
4988 &cpuslab_flush_attr.attr,
4989 &deactivate_full_attr.attr,
4990 &deactivate_empty_attr.attr,
4991 &deactivate_to_head_attr.attr,
4992 &deactivate_to_tail_attr.attr,
4993 &deactivate_remote_frees_attr.attr,
Christoph Lameter03e404a2011-06-01 12:25:58 -05004994 &deactivate_bypass_attr.attr,
Christoph Lameter65c33762008-04-14 19:11:40 +03004995 &order_fallback_attr.attr,
Christoph Lameterb789ef52011-06-01 12:25:49 -05004996 &cmpxchg_double_fail_attr.attr,
4997 &cmpxchg_double_cpu_fail_attr.attr,
Christoph Lameter49e22582011-08-09 16:12:27 -05004998 &cpu_partial_alloc_attr.attr,
4999 &cpu_partial_free_attr.attr,
Alex Shi8028dce2012-02-03 23:34:56 +08005000 &cpu_partial_node_attr.attr,
5001 &cpu_partial_drain_attr.attr,
Christoph Lameter8ff12cf2008-02-07 17:47:41 -08005002#endif
Dmitry Monakhov4c13dd32010-02-26 09:36:12 +03005003#ifdef CONFIG_FAILSLAB
5004 &failslab_attr.attr,
5005#endif
5006
Christoph Lameter81819f02007-05-06 14:49:36 -07005007 NULL
5008};
5009
5010static struct attribute_group slab_attr_group = {
5011 .attrs = slab_attrs,
5012};
5013
5014static ssize_t slab_attr_show(struct kobject *kobj,
5015 struct attribute *attr,
5016 char *buf)
5017{
5018 struct slab_attribute *attribute;
5019 struct kmem_cache *s;
5020 int err;
5021
5022 attribute = to_slab_attr(attr);
5023 s = to_slab(kobj);
5024
5025 if (!attribute->show)
5026 return -EIO;
5027
5028 err = attribute->show(s, buf);
5029
5030 return err;
5031}
5032
5033static ssize_t slab_attr_store(struct kobject *kobj,
5034 struct attribute *attr,
5035 const char *buf, size_t len)
5036{
5037 struct slab_attribute *attribute;
5038 struct kmem_cache *s;
5039 int err;
5040
5041 attribute = to_slab_attr(attr);
5042 s = to_slab(kobj);
5043
5044 if (!attribute->store)
5045 return -EIO;
5046
5047 err = attribute->store(s, buf, len);
Glauber Costa107dab52012-12-18 14:23:05 -08005048#ifdef CONFIG_MEMCG_KMEM
5049 if (slab_state >= FULL && err >= 0 && is_root_cache(s)) {
5050 int i;
Christoph Lameter81819f02007-05-06 14:49:36 -07005051
Glauber Costa107dab52012-12-18 14:23:05 -08005052 mutex_lock(&slab_mutex);
5053 if (s->max_attr_size < len)
5054 s->max_attr_size = len;
5055
Glauber Costaebe945c2012-12-18 14:23:10 -08005056 /*
5057 * This is a best effort propagation, so this function's return
5058 * value will be determined by the parent cache only. This is
5059 * basically because not all attributes will have a well
5060 * defined semantics for rollbacks - most of the actions will
5061 * have permanent effects.
5062 *
5063 * Returning the error value of any of the children that fail
5064 * is not 100 % defined, in the sense that users seeing the
5065 * error code won't be able to know anything about the state of
5066 * the cache.
5067 *
5068 * Only returning the error code for the parent cache at least
5069 * has well defined semantics. The cache being written to
5070 * directly either failed or succeeded, in which case we loop
5071 * through the descendants with best-effort propagation.
5072 */
Glauber Costa107dab52012-12-18 14:23:05 -08005073 for_each_memcg_cache_index(i) {
5074 struct kmem_cache *c = cache_from_memcg(s, i);
Glauber Costa107dab52012-12-18 14:23:05 -08005075 if (c)
5076 attribute->store(c, buf, len);
5077 }
5078 mutex_unlock(&slab_mutex);
5079 }
5080#endif
Christoph Lameter81819f02007-05-06 14:49:36 -07005081 return err;
5082}
5083
Glauber Costa107dab52012-12-18 14:23:05 -08005084static void memcg_propagate_slab_attrs(struct kmem_cache *s)
5085{
5086#ifdef CONFIG_MEMCG_KMEM
5087 int i;
5088 char *buffer = NULL;
5089
5090 if (!is_root_cache(s))
5091 return;
5092
5093 /*
5094 * This mean this cache had no attribute written. Therefore, no point
5095 * in copying default values around
5096 */
5097 if (!s->max_attr_size)
5098 return;
5099
5100 for (i = 0; i < ARRAY_SIZE(slab_attrs); i++) {
5101 char mbuf[64];
5102 char *buf;
5103 struct slab_attribute *attr = to_slab_attr(slab_attrs[i]);
5104
5105 if (!attr || !attr->store || !attr->show)
5106 continue;
5107
5108 /*
5109 * It is really bad that we have to allocate here, so we will
5110 * do it only as a fallback. If we actually allocate, though,
5111 * we can just use the allocated buffer until the end.
5112 *
5113 * Most of the slub attributes will tend to be very small in
5114 * size, but sysfs allows buffers up to a page, so they can
5115 * theoretically happen.
5116 */
5117 if (buffer)
5118 buf = buffer;
5119 else if (s->max_attr_size < ARRAY_SIZE(mbuf))
5120 buf = mbuf;
5121 else {
5122 buffer = (char *) get_zeroed_page(GFP_KERNEL);
5123 if (WARN_ON(!buffer))
5124 continue;
5125 buf = buffer;
5126 }
5127
5128 attr->show(s->memcg_params->root_cache, buf);
5129 attr->store(s, buf, strlen(buf));
5130 }
5131
5132 if (buffer)
5133 free_page((unsigned long)buffer);
5134#endif
5135}
5136
Emese Revfy52cf25d2010-01-19 02:58:23 +01005137static const struct sysfs_ops slab_sysfs_ops = {
Christoph Lameter81819f02007-05-06 14:49:36 -07005138 .show = slab_attr_show,
5139 .store = slab_attr_store,
5140};
5141
5142static struct kobj_type slab_ktype = {
5143 .sysfs_ops = &slab_sysfs_ops,
5144};
5145
5146static int uevent_filter(struct kset *kset, struct kobject *kobj)
5147{
5148 struct kobj_type *ktype = get_ktype(kobj);
5149
5150 if (ktype == &slab_ktype)
5151 return 1;
5152 return 0;
5153}
5154
Emese Revfy9cd43612009-12-31 14:52:51 +01005155static const struct kset_uevent_ops slab_uevent_ops = {
Christoph Lameter81819f02007-05-06 14:49:36 -07005156 .filter = uevent_filter,
5157};
5158
Greg Kroah-Hartman27c3a312007-11-01 09:29:06 -06005159static struct kset *slab_kset;
Christoph Lameter81819f02007-05-06 14:49:36 -07005160
5161#define ID_STR_LENGTH 64
5162
5163/* Create a unique string id for a slab cache:
Christoph Lameter6446faa2008-02-15 23:45:26 -08005164 *
5165 * Format :[flags-]size
Christoph Lameter81819f02007-05-06 14:49:36 -07005166 */
5167static char *create_unique_id(struct kmem_cache *s)
5168{
5169 char *name = kmalloc(ID_STR_LENGTH, GFP_KERNEL);
5170 char *p = name;
5171
5172 BUG_ON(!name);
5173
5174 *p++ = ':';
5175 /*
5176 * First flags affecting slabcache operations. We will only
5177 * get here for aliasable slabs so we do not need to support
5178 * too many flags. The flags here must cover all flags that
5179 * are matched during merging to guarantee that the id is
5180 * unique.
5181 */
5182 if (s->flags & SLAB_CACHE_DMA)
5183 *p++ = 'd';
5184 if (s->flags & SLAB_RECLAIM_ACCOUNT)
5185 *p++ = 'a';
5186 if (s->flags & SLAB_DEBUG_FREE)
5187 *p++ = 'F';
Vegard Nossum5a896d92008-04-04 00:54:48 +02005188 if (!(s->flags & SLAB_NOTRACK))
5189 *p++ = 't';
Christoph Lameter81819f02007-05-06 14:49:36 -07005190 if (p != name + 1)
5191 *p++ = '-';
5192 p += sprintf(p, "%07d", s->size);
Glauber Costa2633d7a2012-12-18 14:22:34 -08005193
5194#ifdef CONFIG_MEMCG_KMEM
5195 if (!is_root_cache(s))
5196 p += sprintf(p, "-%08d", memcg_cache_id(s->memcg_params->memcg));
5197#endif
5198
Christoph Lameter81819f02007-05-06 14:49:36 -07005199 BUG_ON(p > name + ID_STR_LENGTH - 1);
5200 return name;
5201}
5202
5203static int sysfs_slab_add(struct kmem_cache *s)
5204{
5205 int err;
5206 const char *name;
Christoph Lameter45530c42012-11-28 16:23:07 +00005207 int unmergeable = slab_unmergeable(s);
Christoph Lameter81819f02007-05-06 14:49:36 -07005208
Christoph Lameter81819f02007-05-06 14:49:36 -07005209 if (unmergeable) {
5210 /*
5211 * Slabcache can never be merged so we can use the name proper.
5212 * This is typically the case for debug situations. In that
5213 * case we can catch duplicate names easily.
5214 */
Greg Kroah-Hartman27c3a312007-11-01 09:29:06 -06005215 sysfs_remove_link(&slab_kset->kobj, s->name);
Christoph Lameter81819f02007-05-06 14:49:36 -07005216 name = s->name;
5217 } else {
5218 /*
5219 * Create a unique name for the slab as a target
5220 * for the symlinks.
5221 */
5222 name = create_unique_id(s);
5223 }
5224
Greg Kroah-Hartman27c3a312007-11-01 09:29:06 -06005225 s->kobj.kset = slab_kset;
Greg Kroah-Hartman1eada112007-12-17 23:05:35 -07005226 err = kobject_init_and_add(&s->kobj, &slab_ktype, NULL, name);
5227 if (err) {
5228 kobject_put(&s->kobj);
Christoph Lameter81819f02007-05-06 14:49:36 -07005229 return err;
Greg Kroah-Hartman1eada112007-12-17 23:05:35 -07005230 }
Christoph Lameter81819f02007-05-06 14:49:36 -07005231
5232 err = sysfs_create_group(&s->kobj, &slab_attr_group);
Xiaotian Feng5788d8a2009-07-22 11:28:53 +08005233 if (err) {
5234 kobject_del(&s->kobj);
5235 kobject_put(&s->kobj);
Christoph Lameter81819f02007-05-06 14:49:36 -07005236 return err;
Xiaotian Feng5788d8a2009-07-22 11:28:53 +08005237 }
Christoph Lameter81819f02007-05-06 14:49:36 -07005238 kobject_uevent(&s->kobj, KOBJ_ADD);
5239 if (!unmergeable) {
5240 /* Setup first alias */
5241 sysfs_slab_alias(s, s->name);
5242 kfree(name);
5243 }
5244 return 0;
5245}
5246
5247static void sysfs_slab_remove(struct kmem_cache *s)
5248{
Christoph Lameter97d06602012-07-06 15:25:11 -05005249 if (slab_state < FULL)
Christoph Lameter2bce64852010-07-19 11:39:11 -05005250 /*
5251 * Sysfs has not been setup yet so no need to remove the
5252 * cache from sysfs.
5253 */
5254 return;
5255
Christoph Lameter81819f02007-05-06 14:49:36 -07005256 kobject_uevent(&s->kobj, KOBJ_REMOVE);
5257 kobject_del(&s->kobj);
Christoph Lameter151c6022008-01-07 22:29:05 -08005258 kobject_put(&s->kobj);
Christoph Lameter81819f02007-05-06 14:49:36 -07005259}
5260
5261/*
5262 * Need to buffer aliases during bootup until sysfs becomes
Nick Andrew9f6c708e2008-12-05 14:08:08 +11005263 * available lest we lose that information.
Christoph Lameter81819f02007-05-06 14:49:36 -07005264 */
5265struct saved_alias {
5266 struct kmem_cache *s;
5267 const char *name;
5268 struct saved_alias *next;
5269};
5270
Adrian Bunk5af328a2007-07-17 04:03:27 -07005271static struct saved_alias *alias_list;
Christoph Lameter81819f02007-05-06 14:49:36 -07005272
5273static int sysfs_slab_alias(struct kmem_cache *s, const char *name)
5274{
5275 struct saved_alias *al;
5276
Christoph Lameter97d06602012-07-06 15:25:11 -05005277 if (slab_state == FULL) {
Christoph Lameter81819f02007-05-06 14:49:36 -07005278 /*
5279 * If we have a leftover link then remove it.
5280 */
Greg Kroah-Hartman27c3a312007-11-01 09:29:06 -06005281 sysfs_remove_link(&slab_kset->kobj, name);
5282 return sysfs_create_link(&slab_kset->kobj, &s->kobj, name);
Christoph Lameter81819f02007-05-06 14:49:36 -07005283 }
5284
5285 al = kmalloc(sizeof(struct saved_alias), GFP_KERNEL);
5286 if (!al)
5287 return -ENOMEM;
5288
5289 al->s = s;
5290 al->name = name;
5291 al->next = alias_list;
5292 alias_list = al;
5293 return 0;
5294}
5295
5296static int __init slab_sysfs_init(void)
5297{
Christoph Lameter5b95a4ac2007-07-17 04:03:19 -07005298 struct kmem_cache *s;
Christoph Lameter81819f02007-05-06 14:49:36 -07005299 int err;
5300
Christoph Lameter18004c52012-07-06 15:25:12 -05005301 mutex_lock(&slab_mutex);
Christoph Lameter2bce64852010-07-19 11:39:11 -05005302
Greg Kroah-Hartman0ff21e42007-11-06 10:36:58 -08005303 slab_kset = kset_create_and_add("slab", &slab_uevent_ops, kernel_kobj);
Greg Kroah-Hartman27c3a312007-11-01 09:29:06 -06005304 if (!slab_kset) {
Christoph Lameter18004c52012-07-06 15:25:12 -05005305 mutex_unlock(&slab_mutex);
Christoph Lameter81819f02007-05-06 14:49:36 -07005306 printk(KERN_ERR "Cannot register slab subsystem.\n");
5307 return -ENOSYS;
5308 }
5309
Christoph Lameter97d06602012-07-06 15:25:11 -05005310 slab_state = FULL;
Christoph Lameter26a7bd02007-05-09 02:32:39 -07005311
Christoph Lameter5b95a4ac2007-07-17 04:03:19 -07005312 list_for_each_entry(s, &slab_caches, list) {
Christoph Lameter26a7bd02007-05-09 02:32:39 -07005313 err = sysfs_slab_add(s);
Christoph Lameter5d540fb2007-08-30 23:56:26 -07005314 if (err)
5315 printk(KERN_ERR "SLUB: Unable to add boot slab %s"
5316 " to sysfs\n", s->name);
Christoph Lameter26a7bd02007-05-09 02:32:39 -07005317 }
Christoph Lameter81819f02007-05-06 14:49:36 -07005318
5319 while (alias_list) {
5320 struct saved_alias *al = alias_list;
5321
5322 alias_list = alias_list->next;
5323 err = sysfs_slab_alias(al->s, al->name);
Christoph Lameter5d540fb2007-08-30 23:56:26 -07005324 if (err)
5325 printk(KERN_ERR "SLUB: Unable to add boot slab alias"
Julia Lawall068ce412012-07-08 13:37:40 +02005326 " %s to sysfs\n", al->name);
Christoph Lameter81819f02007-05-06 14:49:36 -07005327 kfree(al);
5328 }
5329
Christoph Lameter18004c52012-07-06 15:25:12 -05005330 mutex_unlock(&slab_mutex);
Christoph Lameter81819f02007-05-06 14:49:36 -07005331 resiliency_test();
5332 return 0;
5333}
5334
5335__initcall(slab_sysfs_init);
Christoph Lameterab4d5ed2010-10-05 13:57:26 -05005336#endif /* CONFIG_SYSFS */
Pekka J Enberg57ed3ed2008-01-01 17:23:28 +01005337
5338/*
5339 * The /proc/slabinfo ABI
5340 */
Linus Torvalds158a9622008-01-02 13:04:48 -08005341#ifdef CONFIG_SLABINFO
Glauber Costa0d7561c2012-10-19 18:20:27 +04005342void get_slabinfo(struct kmem_cache *s, struct slabinfo *sinfo)
Pekka J Enberg57ed3ed2008-01-01 17:23:28 +01005343{
5344 unsigned long nr_partials = 0;
5345 unsigned long nr_slabs = 0;
Christoph Lameter205ab992008-04-14 19:11:40 +03005346 unsigned long nr_objs = 0;
5347 unsigned long nr_free = 0;
Pekka J Enberg57ed3ed2008-01-01 17:23:28 +01005348 int node;
5349
Pekka J Enberg57ed3ed2008-01-01 17:23:28 +01005350 for_each_online_node(node) {
5351 struct kmem_cache_node *n = get_node(s, node);
5352
5353 if (!n)
5354 continue;
5355
5356 nr_partials += n->nr_partial;
5357 nr_slabs += atomic_long_read(&n->nr_slabs);
Christoph Lameter205ab992008-04-14 19:11:40 +03005358 nr_objs += atomic_long_read(&n->total_objects);
5359 nr_free += count_partial(n, count_free);
Pekka J Enberg57ed3ed2008-01-01 17:23:28 +01005360 }
5361
Glauber Costa0d7561c2012-10-19 18:20:27 +04005362 sinfo->active_objs = nr_objs - nr_free;
5363 sinfo->num_objs = nr_objs;
5364 sinfo->active_slabs = nr_slabs;
5365 sinfo->num_slabs = nr_slabs;
5366 sinfo->objects_per_slab = oo_objects(s->oo);
5367 sinfo->cache_order = oo_order(s->oo);
Pekka J Enberg57ed3ed2008-01-01 17:23:28 +01005368}
5369
Glauber Costa0d7561c2012-10-19 18:20:27 +04005370void slabinfo_show_stats(struct seq_file *m, struct kmem_cache *s)
Alexey Dobriyan7b3c3a52008-10-06 02:42:17 +04005371{
Alexey Dobriyan7b3c3a52008-10-06 02:42:17 +04005372}
5373
Glauber Costab7454ad2012-10-19 18:20:25 +04005374ssize_t slabinfo_write(struct file *file, const char __user *buffer,
5375 size_t count, loff_t *ppos)
Alexey Dobriyan7b3c3a52008-10-06 02:42:17 +04005376{
Glauber Costab7454ad2012-10-19 18:20:25 +04005377 return -EIO;
Alexey Dobriyan7b3c3a52008-10-06 02:42:17 +04005378}
Linus Torvalds158a9622008-01-02 13:04:48 -08005379#endif /* CONFIG_SLABINFO */