slab: factor out calculate nr objects in cache_estimate
[linux-2.6-block.git] / mm / slab.c
CommitLineData
1da177e4
LT
1/*
2 * linux/mm/slab.c
3 * Written by Mark Hemment, 1996/97.
4 * (markhe@nextd.demon.co.uk)
5 *
6 * kmem_cache_destroy() + some cleanup - 1999 Andrea Arcangeli
7 *
8 * Major cleanup, different bufctl logic, per-cpu arrays
9 * (c) 2000 Manfred Spraul
10 *
11 * Cleanup, make the head arrays unconditional, preparation for NUMA
12 * (c) 2002 Manfred Spraul
13 *
14 * An implementation of the Slab Allocator as described in outline in;
15 * UNIX Internals: The New Frontiers by Uresh Vahalia
16 * Pub: Prentice Hall ISBN 0-13-101908-2
17 * or with a little more detail in;
18 * The Slab Allocator: An Object-Caching Kernel Memory Allocator
19 * Jeff Bonwick (Sun Microsystems).
20 * Presented at: USENIX Summer 1994 Technical Conference
21 *
22 * The memory is organized in caches, one cache for each object type.
23 * (e.g. inode_cache, dentry_cache, buffer_head, vm_area_struct)
24 * Each cache consists out of many slabs (they are small (usually one
25 * page long) and always contiguous), and each slab contains multiple
26 * initialized objects.
27 *
28 * This means, that your constructor is used only for newly allocated
183ff22b 29 * slabs and you must pass objects with the same initializations to
1da177e4
LT
30 * kmem_cache_free.
31 *
32 * Each cache can only support one memory type (GFP_DMA, GFP_HIGHMEM,
33 * normal). If you need a special memory type, then must create a new
34 * cache for that memory type.
35 *
36 * In order to reduce fragmentation, the slabs are sorted in 3 groups:
37 * full slabs with 0 free objects
38 * partial slabs
39 * empty slabs with no allocated objects
40 *
41 * If partial slabs exist, then new allocations come from these slabs,
42 * otherwise from empty slabs or new slabs are allocated.
43 *
44 * kmem_cache_destroy() CAN CRASH if you try to allocate from the cache
45 * during kmem_cache_destroy(). The caller must prevent concurrent allocs.
46 *
47 * Each cache has a short per-cpu head array, most allocs
48 * and frees go into that array, and if that array overflows, then 1/2
49 * of the entries in the array are given back into the global cache.
50 * The head array is strictly LIFO and should improve the cache hit rates.
51 * On SMP, it additionally reduces the spinlock operations.
52 *
a737b3e2 53 * The c_cpuarray may not be read with enabled local interrupts -
1da177e4
LT
54 * it's changed with a smp_call_function().
55 *
56 * SMP synchronization:
57 * constructors and destructors are called without any locking.
343e0d7a 58 * Several members in struct kmem_cache and struct slab never change, they
1da177e4
LT
59 * are accessed without any locking.
60 * The per-cpu arrays are never accessed from the wrong cpu, no locking,
61 * and local interrupts are disabled so slab code is preempt-safe.
62 * The non-constant members are protected with a per-cache irq spinlock.
63 *
64 * Many thanks to Mark Hemment, who wrote another per-cpu slab patch
65 * in 2000 - many ideas in the current implementation are derived from
66 * his patch.
67 *
68 * Further notes from the original documentation:
69 *
70 * 11 April '97. Started multi-threading - markhe
18004c5d 71 * The global cache-chain is protected by the mutex 'slab_mutex'.
1da177e4
LT
72 * The sem is only needed when accessing/extending the cache-chain, which
73 * can never happen inside an interrupt (kmem_cache_create(),
74 * kmem_cache_shrink() and kmem_cache_reap()).
75 *
76 * At present, each engine can be growing a cache. This should be blocked.
77 *
e498be7d
CL
78 * 15 March 2005. NUMA slab allocator.
79 * Shai Fultheim <shai@scalex86.org>.
80 * Shobhit Dayal <shobhit@calsoftinc.com>
81 * Alok N Kataria <alokk@calsoftinc.com>
82 * Christoph Lameter <christoph@lameter.com>
83 *
84 * Modified the slab allocator to be node aware on NUMA systems.
85 * Each node has its own list of partial, free and full slabs.
86 * All object allocations for a node occur from node specific slab lists.
1da177e4
LT
87 */
88
1da177e4
LT
89#include <linux/slab.h>
90#include <linux/mm.h>
c9cf5528 91#include <linux/poison.h>
1da177e4
LT
92#include <linux/swap.h>
93#include <linux/cache.h>
94#include <linux/interrupt.h>
95#include <linux/init.h>
96#include <linux/compiler.h>
101a5001 97#include <linux/cpuset.h>
a0ec95a8 98#include <linux/proc_fs.h>
1da177e4
LT
99#include <linux/seq_file.h>
100#include <linux/notifier.h>
101#include <linux/kallsyms.h>
102#include <linux/cpu.h>
103#include <linux/sysctl.h>
104#include <linux/module.h>
105#include <linux/rcupdate.h>
543537bd 106#include <linux/string.h>
138ae663 107#include <linux/uaccess.h>
e498be7d 108#include <linux/nodemask.h>
d5cff635 109#include <linux/kmemleak.h>
dc85da15 110#include <linux/mempolicy.h>
fc0abb14 111#include <linux/mutex.h>
8a8b6502 112#include <linux/fault-inject.h>
e7eebaf6 113#include <linux/rtmutex.h>
6a2d7a95 114#include <linux/reciprocal_div.h>
3ac7fe5a 115#include <linux/debugobjects.h>
c175eea4 116#include <linux/kmemcheck.h>
8f9f8d9e 117#include <linux/memory.h>
268bb0ce 118#include <linux/prefetch.h>
1da177e4 119
381760ea
MG
120#include <net/sock.h>
121
1da177e4
LT
122#include <asm/cacheflush.h>
123#include <asm/tlbflush.h>
124#include <asm/page.h>
125
4dee6b64
SR
126#include <trace/events/kmem.h>
127
072bb0aa
MG
128#include "internal.h"
129
b9ce5ef4
GC
130#include "slab.h"
131
1da177e4 132/*
50953fe9 133 * DEBUG - 1 for kmem_cache_create() to honour; SLAB_RED_ZONE & SLAB_POISON.
1da177e4
LT
134 * 0 for faster, smaller code (especially in the critical paths).
135 *
136 * STATS - 1 to collect stats for /proc/slabinfo.
137 * 0 for faster, smaller code (especially in the critical paths).
138 *
139 * FORCED_DEBUG - 1 enables SLAB_RED_ZONE and SLAB_POISON (if possible)
140 */
141
142#ifdef CONFIG_DEBUG_SLAB
143#define DEBUG 1
144#define STATS 1
145#define FORCED_DEBUG 1
146#else
147#define DEBUG 0
148#define STATS 0
149#define FORCED_DEBUG 0
150#endif
151
1da177e4
LT
152/* Shouldn't this be in a header file somewhere? */
153#define BYTES_PER_WORD sizeof(void *)
87a927c7 154#define REDZONE_ALIGN max(BYTES_PER_WORD, __alignof__(unsigned long long))
1da177e4 155
1da177e4
LT
156#ifndef ARCH_KMALLOC_FLAGS
157#define ARCH_KMALLOC_FLAGS SLAB_HWCACHE_ALIGN
158#endif
159
072bb0aa
MG
160/*
161 * true if a page was allocated from pfmemalloc reserves for network-based
162 * swap
163 */
164static bool pfmemalloc_active __read_mostly;
165
1da177e4
LT
166/*
167 * struct array_cache
168 *
1da177e4
LT
169 * Purpose:
170 * - LIFO ordering, to hand out cache-warm objects from _alloc
171 * - reduce the number of linked list operations
172 * - reduce spinlock operations
173 *
174 * The limit is stored in the per-cpu structure to reduce the data cache
175 * footprint.
176 *
177 */
178struct array_cache {
179 unsigned int avail;
180 unsigned int limit;
181 unsigned int batchcount;
182 unsigned int touched;
e498be7d 183 spinlock_t lock;
bda5b655 184 void *entry[]; /*
a737b3e2
AM
185 * Must have this definition in here for the proper
186 * alignment of array_cache. Also simplifies accessing
187 * the entries.
072bb0aa
MG
188 *
189 * Entries should not be directly dereferenced as
190 * entries belonging to slabs marked pfmemalloc will
191 * have the lower bits set SLAB_OBJ_PFMEMALLOC
a737b3e2 192 */
1da177e4
LT
193};
194
072bb0aa
MG
195#define SLAB_OBJ_PFMEMALLOC 1
196static inline bool is_obj_pfmemalloc(void *objp)
197{
198 return (unsigned long)objp & SLAB_OBJ_PFMEMALLOC;
199}
200
201static inline void set_obj_pfmemalloc(void **objp)
202{
203 *objp = (void *)((unsigned long)*objp | SLAB_OBJ_PFMEMALLOC);
204 return;
205}
206
207static inline void clear_obj_pfmemalloc(void **objp)
208{
209 *objp = (void *)((unsigned long)*objp & ~SLAB_OBJ_PFMEMALLOC);
210}
211
a737b3e2
AM
212/*
213 * bootstrap: The caches do not work without cpuarrays anymore, but the
214 * cpuarrays are allocated from the generic caches...
1da177e4
LT
215 */
216#define BOOT_CPUCACHE_ENTRIES 1
217struct arraycache_init {
218 struct array_cache cache;
b28a02de 219 void *entries[BOOT_CPUCACHE_ENTRIES];
1da177e4
LT
220};
221
e498be7d
CL
222/*
223 * Need this for bootstrapping a per node allocator.
224 */
556a169d 225#define NUM_INIT_LISTS (3 * MAX_NUMNODES)
ce8eb6c4 226static struct kmem_cache_node __initdata init_kmem_cache_node[NUM_INIT_LISTS];
e498be7d 227#define CACHE_CACHE 0
556a169d 228#define SIZE_AC MAX_NUMNODES
ce8eb6c4 229#define SIZE_NODE (2 * MAX_NUMNODES)
e498be7d 230
ed11d9eb 231static int drain_freelist(struct kmem_cache *cache,
ce8eb6c4 232 struct kmem_cache_node *n, int tofree);
ed11d9eb
CL
233static void free_block(struct kmem_cache *cachep, void **objpp, int len,
234 int node);
83b519e8 235static int enable_cpucache(struct kmem_cache *cachep, gfp_t gfp);
65f27f38 236static void cache_reap(struct work_struct *unused);
ed11d9eb 237
e0a42726
IM
238static int slab_early_init = 1;
239
e3366016 240#define INDEX_AC kmalloc_index(sizeof(struct arraycache_init))
ce8eb6c4 241#define INDEX_NODE kmalloc_index(sizeof(struct kmem_cache_node))
1da177e4 242
ce8eb6c4 243static void kmem_cache_node_init(struct kmem_cache_node *parent)
e498be7d
CL
244{
245 INIT_LIST_HEAD(&parent->slabs_full);
246 INIT_LIST_HEAD(&parent->slabs_partial);
247 INIT_LIST_HEAD(&parent->slabs_free);
248 parent->shared = NULL;
249 parent->alien = NULL;
2e1217cf 250 parent->colour_next = 0;
e498be7d
CL
251 spin_lock_init(&parent->list_lock);
252 parent->free_objects = 0;
253 parent->free_touched = 0;
254}
255
a737b3e2
AM
256#define MAKE_LIST(cachep, listp, slab, nodeid) \
257 do { \
258 INIT_LIST_HEAD(listp); \
6a67368c 259 list_splice(&(cachep->node[nodeid]->slab), listp); \
e498be7d
CL
260 } while (0)
261
a737b3e2
AM
262#define MAKE_ALL_LISTS(cachep, ptr, nodeid) \
263 do { \
e498be7d
CL
264 MAKE_LIST((cachep), (&(ptr)->slabs_full), slabs_full, nodeid); \
265 MAKE_LIST((cachep), (&(ptr)->slabs_partial), slabs_partial, nodeid); \
266 MAKE_LIST((cachep), (&(ptr)->slabs_free), slabs_free, nodeid); \
267 } while (0)
1da177e4 268
1da177e4
LT
269#define CFLGS_OFF_SLAB (0x80000000UL)
270#define OFF_SLAB(x) ((x)->flags & CFLGS_OFF_SLAB)
271
272#define BATCHREFILL_LIMIT 16
a737b3e2
AM
273/*
274 * Optimization question: fewer reaps means less probability for unnessary
275 * cpucache drain/refill cycles.
1da177e4 276 *
dc6f3f27 277 * OTOH the cpuarrays can contain lots of objects,
1da177e4
LT
278 * which could lock up otherwise freeable slabs.
279 */
280#define REAPTIMEOUT_CPUC (2*HZ)
281#define REAPTIMEOUT_LIST3 (4*HZ)
282
283#if STATS
284#define STATS_INC_ACTIVE(x) ((x)->num_active++)
285#define STATS_DEC_ACTIVE(x) ((x)->num_active--)
286#define STATS_INC_ALLOCED(x) ((x)->num_allocations++)
287#define STATS_INC_GROWN(x) ((x)->grown++)
ed11d9eb 288#define STATS_ADD_REAPED(x,y) ((x)->reaped += (y))
a737b3e2
AM
289#define STATS_SET_HIGH(x) \
290 do { \
291 if ((x)->num_active > (x)->high_mark) \
292 (x)->high_mark = (x)->num_active; \
293 } while (0)
1da177e4
LT
294#define STATS_INC_ERR(x) ((x)->errors++)
295#define STATS_INC_NODEALLOCS(x) ((x)->node_allocs++)
e498be7d 296#define STATS_INC_NODEFREES(x) ((x)->node_frees++)
fb7faf33 297#define STATS_INC_ACOVERFLOW(x) ((x)->node_overflow++)
a737b3e2
AM
298#define STATS_SET_FREEABLE(x, i) \
299 do { \
300 if ((x)->max_freeable < i) \
301 (x)->max_freeable = i; \
302 } while (0)
1da177e4
LT
303#define STATS_INC_ALLOCHIT(x) atomic_inc(&(x)->allochit)
304#define STATS_INC_ALLOCMISS(x) atomic_inc(&(x)->allocmiss)
305#define STATS_INC_FREEHIT(x) atomic_inc(&(x)->freehit)
306#define STATS_INC_FREEMISS(x) atomic_inc(&(x)->freemiss)
307#else
308#define STATS_INC_ACTIVE(x) do { } while (0)
309#define STATS_DEC_ACTIVE(x) do { } while (0)
310#define STATS_INC_ALLOCED(x) do { } while (0)
311#define STATS_INC_GROWN(x) do { } while (0)
4e60c86b 312#define STATS_ADD_REAPED(x,y) do { (void)(y); } while (0)
1da177e4
LT
313#define STATS_SET_HIGH(x) do { } while (0)
314#define STATS_INC_ERR(x) do { } while (0)
315#define STATS_INC_NODEALLOCS(x) do { } while (0)
e498be7d 316#define STATS_INC_NODEFREES(x) do { } while (0)
fb7faf33 317#define STATS_INC_ACOVERFLOW(x) do { } while (0)
a737b3e2 318#define STATS_SET_FREEABLE(x, i) do { } while (0)
1da177e4
LT
319#define STATS_INC_ALLOCHIT(x) do { } while (0)
320#define STATS_INC_ALLOCMISS(x) do { } while (0)
321#define STATS_INC_FREEHIT(x) do { } while (0)
322#define STATS_INC_FREEMISS(x) do { } while (0)
323#endif
324
325#if DEBUG
1da177e4 326
a737b3e2
AM
327/*
328 * memory layout of objects:
1da177e4 329 * 0 : objp
3dafccf2 330 * 0 .. cachep->obj_offset - BYTES_PER_WORD - 1: padding. This ensures that
1da177e4
LT
331 * the end of an object is aligned with the end of the real
332 * allocation. Catches writes behind the end of the allocation.
3dafccf2 333 * cachep->obj_offset - BYTES_PER_WORD .. cachep->obj_offset - 1:
1da177e4 334 * redzone word.
3dafccf2 335 * cachep->obj_offset: The real object.
3b0efdfa
CL
336 * cachep->size - 2* BYTES_PER_WORD: redzone word [BYTES_PER_WORD long]
337 * cachep->size - 1* BYTES_PER_WORD: last caller address
a737b3e2 338 * [BYTES_PER_WORD long]
1da177e4 339 */
343e0d7a 340static int obj_offset(struct kmem_cache *cachep)
1da177e4 341{
3dafccf2 342 return cachep->obj_offset;
1da177e4
LT
343}
344
b46b8f19 345static unsigned long long *dbg_redzone1(struct kmem_cache *cachep, void *objp)
1da177e4
LT
346{
347 BUG_ON(!(cachep->flags & SLAB_RED_ZONE));
b46b8f19
DW
348 return (unsigned long long*) (objp + obj_offset(cachep) -
349 sizeof(unsigned long long));
1da177e4
LT
350}
351
b46b8f19 352static unsigned long long *dbg_redzone2(struct kmem_cache *cachep, void *objp)
1da177e4
LT
353{
354 BUG_ON(!(cachep->flags & SLAB_RED_ZONE));
355 if (cachep->flags & SLAB_STORE_USER)
3b0efdfa 356 return (unsigned long long *)(objp + cachep->size -
b46b8f19 357 sizeof(unsigned long long) -
87a927c7 358 REDZONE_ALIGN);
3b0efdfa 359 return (unsigned long long *) (objp + cachep->size -
b46b8f19 360 sizeof(unsigned long long));
1da177e4
LT
361}
362
343e0d7a 363static void **dbg_userword(struct kmem_cache *cachep, void *objp)
1da177e4
LT
364{
365 BUG_ON(!(cachep->flags & SLAB_STORE_USER));
3b0efdfa 366 return (void **)(objp + cachep->size - BYTES_PER_WORD);
1da177e4
LT
367}
368
369#else
370
3dafccf2 371#define obj_offset(x) 0
b46b8f19
DW
372#define dbg_redzone1(cachep, objp) ({BUG(); (unsigned long long *)NULL;})
373#define dbg_redzone2(cachep, objp) ({BUG(); (unsigned long long *)NULL;})
1da177e4
LT
374#define dbg_userword(cachep, objp) ({BUG(); (void **)NULL;})
375
376#endif
377
1da177e4 378/*
3df1cccd
DR
379 * Do not go above this order unless 0 objects fit into the slab or
380 * overridden on the command line.
1da177e4 381 */
543585cc
DR
382#define SLAB_MAX_ORDER_HI 1
383#define SLAB_MAX_ORDER_LO 0
384static int slab_max_order = SLAB_MAX_ORDER_LO;
3df1cccd 385static bool slab_max_order_set __initdata;
1da177e4 386
6ed5eb22
PE
387static inline struct kmem_cache *virt_to_cache(const void *obj)
388{
b49af68f 389 struct page *page = virt_to_head_page(obj);
35026088 390 return page->slab_cache;
6ed5eb22
PE
391}
392
8456a648 393static inline void *index_to_obj(struct kmem_cache *cache, struct page *page,
8fea4e96
PE
394 unsigned int idx)
395{
8456a648 396 return page->s_mem + cache->size * idx;
8fea4e96
PE
397}
398
6a2d7a95 399/*
3b0efdfa
CL
400 * We want to avoid an expensive divide : (offset / cache->size)
401 * Using the fact that size is a constant for a particular cache,
402 * we can replace (offset / cache->size) by
6a2d7a95
ED
403 * reciprocal_divide(offset, cache->reciprocal_buffer_size)
404 */
405static inline unsigned int obj_to_index(const struct kmem_cache *cache,
8456a648 406 const struct page *page, void *obj)
8fea4e96 407{
8456a648 408 u32 offset = (obj - page->s_mem);
6a2d7a95 409 return reciprocal_divide(offset, cache->reciprocal_buffer_size);
8fea4e96
PE
410}
411
1da177e4 412static struct arraycache_init initarray_generic =
b28a02de 413 { {0, BOOT_CPUCACHE_ENTRIES, 1, 0} };
1da177e4
LT
414
415/* internal cache of cache description objs */
9b030cb8 416static struct kmem_cache kmem_cache_boot = {
b28a02de
PE
417 .batchcount = 1,
418 .limit = BOOT_CPUCACHE_ENTRIES,
419 .shared = 1,
3b0efdfa 420 .size = sizeof(struct kmem_cache),
b28a02de 421 .name = "kmem_cache",
1da177e4
LT
422};
423
056c6241
RT
424#define BAD_ALIEN_MAGIC 0x01020304ul
425
f1aaee53
AV
426#ifdef CONFIG_LOCKDEP
427
428/*
429 * Slab sometimes uses the kmalloc slabs to store the slab headers
430 * for other slabs "off slab".
431 * The locking for this is tricky in that it nests within the locks
432 * of all other slabs in a few places; to deal with this special
433 * locking we put on-slab caches into a separate lock-class.
056c6241
RT
434 *
435 * We set lock class for alien array caches which are up during init.
436 * The lock annotation will be lost if all cpus of a node goes down and
437 * then comes back up during hotplug
f1aaee53 438 */
056c6241
RT
439static struct lock_class_key on_slab_l3_key;
440static struct lock_class_key on_slab_alc_key;
441
83835b3d
PZ
442static struct lock_class_key debugobj_l3_key;
443static struct lock_class_key debugobj_alc_key;
444
445static void slab_set_lock_classes(struct kmem_cache *cachep,
446 struct lock_class_key *l3_key, struct lock_class_key *alc_key,
447 int q)
448{
449 struct array_cache **alc;
ce8eb6c4 450 struct kmem_cache_node *n;
83835b3d
PZ
451 int r;
452
ce8eb6c4
CL
453 n = cachep->node[q];
454 if (!n)
83835b3d
PZ
455 return;
456
ce8eb6c4
CL
457 lockdep_set_class(&n->list_lock, l3_key);
458 alc = n->alien;
83835b3d
PZ
459 /*
460 * FIXME: This check for BAD_ALIEN_MAGIC
461 * should go away when common slab code is taught to
462 * work even without alien caches.
463 * Currently, non NUMA code returns BAD_ALIEN_MAGIC
464 * for alloc_alien_cache,
465 */
466 if (!alc || (unsigned long)alc == BAD_ALIEN_MAGIC)
467 return;
468 for_each_node(r) {
469 if (alc[r])
470 lockdep_set_class(&alc[r]->lock, alc_key);
471 }
472}
473
474static void slab_set_debugobj_lock_classes_node(struct kmem_cache *cachep, int node)
475{
476 slab_set_lock_classes(cachep, &debugobj_l3_key, &debugobj_alc_key, node);
477}
478
479static void slab_set_debugobj_lock_classes(struct kmem_cache *cachep)
480{
481 int node;
482
483 for_each_online_node(node)
484 slab_set_debugobj_lock_classes_node(cachep, node);
485}
486
ce79ddc8 487static void init_node_lock_keys(int q)
f1aaee53 488{
e3366016 489 int i;
056c6241 490
97d06609 491 if (slab_state < UP)
ce79ddc8
PE
492 return;
493
0f8f8094 494 for (i = 1; i <= KMALLOC_SHIFT_HIGH; i++) {
ce8eb6c4 495 struct kmem_cache_node *n;
e3366016
CL
496 struct kmem_cache *cache = kmalloc_caches[i];
497
498 if (!cache)
499 continue;
ce79ddc8 500
ce8eb6c4
CL
501 n = cache->node[q];
502 if (!n || OFF_SLAB(cache))
00afa758 503 continue;
83835b3d 504
e3366016 505 slab_set_lock_classes(cache, &on_slab_l3_key,
83835b3d 506 &on_slab_alc_key, q);
f1aaee53
AV
507 }
508}
ce79ddc8 509
6ccfb5bc
GC
510static void on_slab_lock_classes_node(struct kmem_cache *cachep, int q)
511{
6a67368c 512 if (!cachep->node[q])
6ccfb5bc
GC
513 return;
514
515 slab_set_lock_classes(cachep, &on_slab_l3_key,
516 &on_slab_alc_key, q);
517}
518
519static inline void on_slab_lock_classes(struct kmem_cache *cachep)
520{
521 int node;
522
523 VM_BUG_ON(OFF_SLAB(cachep));
524 for_each_node(node)
525 on_slab_lock_classes_node(cachep, node);
526}
527
ce79ddc8
PE
528static inline void init_lock_keys(void)
529{
530 int node;
531
532 for_each_node(node)
533 init_node_lock_keys(node);
534}
f1aaee53 535#else
ce79ddc8
PE
536static void init_node_lock_keys(int q)
537{
538}
539
056c6241 540static inline void init_lock_keys(void)
f1aaee53
AV
541{
542}
83835b3d 543
6ccfb5bc
GC
544static inline void on_slab_lock_classes(struct kmem_cache *cachep)
545{
546}
547
548static inline void on_slab_lock_classes_node(struct kmem_cache *cachep, int node)
549{
550}
551
83835b3d
PZ
552static void slab_set_debugobj_lock_classes_node(struct kmem_cache *cachep, int node)
553{
554}
555
556static void slab_set_debugobj_lock_classes(struct kmem_cache *cachep)
557{
558}
f1aaee53
AV
559#endif
560
1871e52c 561static DEFINE_PER_CPU(struct delayed_work, slab_reap_work);
1da177e4 562
343e0d7a 563static inline struct array_cache *cpu_cache_get(struct kmem_cache *cachep)
1da177e4
LT
564{
565 return cachep->array[smp_processor_id()];
566}
567
9cef2e2b
JK
568static int calculate_nr_objs(size_t slab_size, size_t buffer_size,
569 size_t idx_size, size_t align)
1da177e4 570{
9cef2e2b
JK
571 int nr_objs;
572 size_t freelist_size;
573
574 /*
575 * Ignore padding for the initial guess. The padding
576 * is at most @align-1 bytes, and @buffer_size is at
577 * least @align. In the worst case, this result will
578 * be one greater than the number of objects that fit
579 * into the memory allocation when taking the padding
580 * into account.
581 */
582 nr_objs = slab_size / (buffer_size + idx_size);
583
584 /*
585 * This calculated number will be either the right
586 * amount, or one greater than what we want.
587 */
588 freelist_size = slab_size - nr_objs * buffer_size;
589 if (freelist_size < ALIGN(nr_objs * idx_size, align))
590 nr_objs--;
591
592 return nr_objs;
fbaccacf 593}
1da177e4 594
a737b3e2
AM
595/*
596 * Calculate the number of objects and left-over bytes for a given buffer size.
597 */
fbaccacf
SR
598static void cache_estimate(unsigned long gfporder, size_t buffer_size,
599 size_t align, int flags, size_t *left_over,
600 unsigned int *num)
601{
602 int nr_objs;
603 size_t mgmt_size;
604 size_t slab_size = PAGE_SIZE << gfporder;
1da177e4 605
fbaccacf
SR
606 /*
607 * The slab management structure can be either off the slab or
608 * on it. For the latter case, the memory allocated for a
609 * slab is used for:
610 *
16025177 611 * - One unsigned int for each object
fbaccacf
SR
612 * - Padding to respect alignment of @align
613 * - @buffer_size bytes for each object
614 *
615 * If the slab management structure is off the slab, then the
616 * alignment will already be calculated into the size. Because
617 * the slabs are all pages aligned, the objects will be at the
618 * correct alignment when allocated.
619 */
620 if (flags & CFLGS_OFF_SLAB) {
621 mgmt_size = 0;
622 nr_objs = slab_size / buffer_size;
623
fbaccacf 624 } else {
9cef2e2b
JK
625 nr_objs = calculate_nr_objs(slab_size, buffer_size,
626 sizeof(unsigned int), align);
627 mgmt_size = ALIGN(nr_objs * sizeof(unsigned int), align);
fbaccacf
SR
628 }
629 *num = nr_objs;
630 *left_over = slab_size - nr_objs*buffer_size - mgmt_size;
1da177e4
LT
631}
632
f28510d3 633#if DEBUG
d40cee24 634#define slab_error(cachep, msg) __slab_error(__func__, cachep, msg)
1da177e4 635
a737b3e2
AM
636static void __slab_error(const char *function, struct kmem_cache *cachep,
637 char *msg)
1da177e4
LT
638{
639 printk(KERN_ERR "slab error in %s(): cache `%s': %s\n",
b28a02de 640 function, cachep->name, msg);
1da177e4 641 dump_stack();
373d4d09 642 add_taint(TAINT_BAD_PAGE, LOCKDEP_NOW_UNRELIABLE);
1da177e4 643}
f28510d3 644#endif
1da177e4 645
3395ee05
PM
646/*
647 * By default on NUMA we use alien caches to stage the freeing of
648 * objects allocated from other nodes. This causes massive memory
649 * inefficiencies when using fake NUMA setup to split memory into a
650 * large number of small nodes, so it can be disabled on the command
651 * line
652 */
653
654static int use_alien_caches __read_mostly = 1;
655static int __init noaliencache_setup(char *s)
656{
657 use_alien_caches = 0;
658 return 1;
659}
660__setup("noaliencache", noaliencache_setup);
661
3df1cccd
DR
662static int __init slab_max_order_setup(char *str)
663{
664 get_option(&str, &slab_max_order);
665 slab_max_order = slab_max_order < 0 ? 0 :
666 min(slab_max_order, MAX_ORDER - 1);
667 slab_max_order_set = true;
668
669 return 1;
670}
671__setup("slab_max_order=", slab_max_order_setup);
672
8fce4d8e
CL
673#ifdef CONFIG_NUMA
674/*
675 * Special reaping functions for NUMA systems called from cache_reap().
676 * These take care of doing round robin flushing of alien caches (containing
677 * objects freed on different nodes from which they were allocated) and the
678 * flushing of remote pcps by calling drain_node_pages.
679 */
1871e52c 680static DEFINE_PER_CPU(unsigned long, slab_reap_node);
8fce4d8e
CL
681
682static void init_reap_node(int cpu)
683{
684 int node;
685
7d6e6d09 686 node = next_node(cpu_to_mem(cpu), node_online_map);
8fce4d8e 687 if (node == MAX_NUMNODES)
442295c9 688 node = first_node(node_online_map);
8fce4d8e 689
1871e52c 690 per_cpu(slab_reap_node, cpu) = node;
8fce4d8e
CL
691}
692
693static void next_reap_node(void)
694{
909ea964 695 int node = __this_cpu_read(slab_reap_node);
8fce4d8e 696
8fce4d8e
CL
697 node = next_node(node, node_online_map);
698 if (unlikely(node >= MAX_NUMNODES))
699 node = first_node(node_online_map);
909ea964 700 __this_cpu_write(slab_reap_node, node);
8fce4d8e
CL
701}
702
703#else
704#define init_reap_node(cpu) do { } while (0)
705#define next_reap_node(void) do { } while (0)
706#endif
707
1da177e4
LT
708/*
709 * Initiate the reap timer running on the target CPU. We run at around 1 to 2Hz
710 * via the workqueue/eventd.
711 * Add the CPU number into the expiration time to minimize the possibility of
712 * the CPUs getting into lockstep and contending for the global cache chain
713 * lock.
714 */
0db0628d 715static void start_cpu_timer(int cpu)
1da177e4 716{
1871e52c 717 struct delayed_work *reap_work = &per_cpu(slab_reap_work, cpu);
1da177e4
LT
718
719 /*
720 * When this gets called from do_initcalls via cpucache_init(),
721 * init_workqueues() has already run, so keventd will be setup
722 * at that time.
723 */
52bad64d 724 if (keventd_up() && reap_work->work.func == NULL) {
8fce4d8e 725 init_reap_node(cpu);
203b42f7 726 INIT_DEFERRABLE_WORK(reap_work, cache_reap);
2b284214
AV
727 schedule_delayed_work_on(cpu, reap_work,
728 __round_jiffies_relative(HZ, cpu));
1da177e4
LT
729 }
730}
731
e498be7d 732static struct array_cache *alloc_arraycache(int node, int entries,
83b519e8 733 int batchcount, gfp_t gfp)
1da177e4 734{
b28a02de 735 int memsize = sizeof(void *) * entries + sizeof(struct array_cache);
1da177e4
LT
736 struct array_cache *nc = NULL;
737
83b519e8 738 nc = kmalloc_node(memsize, gfp, node);
d5cff635
CM
739 /*
740 * The array_cache structures contain pointers to free object.
25985edc 741 * However, when such objects are allocated or transferred to another
d5cff635
CM
742 * cache the pointers are not cleared and they could be counted as
743 * valid references during a kmemleak scan. Therefore, kmemleak must
744 * not scan such objects.
745 */
746 kmemleak_no_scan(nc);
1da177e4
LT
747 if (nc) {
748 nc->avail = 0;
749 nc->limit = entries;
750 nc->batchcount = batchcount;
751 nc->touched = 0;
e498be7d 752 spin_lock_init(&nc->lock);
1da177e4
LT
753 }
754 return nc;
755}
756
8456a648 757static inline bool is_slab_pfmemalloc(struct page *page)
072bb0aa 758{
072bb0aa
MG
759 return PageSlabPfmemalloc(page);
760}
761
762/* Clears pfmemalloc_active if no slabs have pfmalloc set */
763static void recheck_pfmemalloc_active(struct kmem_cache *cachep,
764 struct array_cache *ac)
765{
ce8eb6c4 766 struct kmem_cache_node *n = cachep->node[numa_mem_id()];
8456a648 767 struct page *page;
072bb0aa
MG
768 unsigned long flags;
769
770 if (!pfmemalloc_active)
771 return;
772
ce8eb6c4 773 spin_lock_irqsave(&n->list_lock, flags);
8456a648
JK
774 list_for_each_entry(page, &n->slabs_full, lru)
775 if (is_slab_pfmemalloc(page))
072bb0aa
MG
776 goto out;
777
8456a648
JK
778 list_for_each_entry(page, &n->slabs_partial, lru)
779 if (is_slab_pfmemalloc(page))
072bb0aa
MG
780 goto out;
781
8456a648
JK
782 list_for_each_entry(page, &n->slabs_free, lru)
783 if (is_slab_pfmemalloc(page))
072bb0aa
MG
784 goto out;
785
786 pfmemalloc_active = false;
787out:
ce8eb6c4 788 spin_unlock_irqrestore(&n->list_lock, flags);
072bb0aa
MG
789}
790
381760ea 791static void *__ac_get_obj(struct kmem_cache *cachep, struct array_cache *ac,
072bb0aa
MG
792 gfp_t flags, bool force_refill)
793{
794 int i;
795 void *objp = ac->entry[--ac->avail];
796
797 /* Ensure the caller is allowed to use objects from PFMEMALLOC slab */
798 if (unlikely(is_obj_pfmemalloc(objp))) {
ce8eb6c4 799 struct kmem_cache_node *n;
072bb0aa
MG
800
801 if (gfp_pfmemalloc_allowed(flags)) {
802 clear_obj_pfmemalloc(&objp);
803 return objp;
804 }
805
806 /* The caller cannot use PFMEMALLOC objects, find another one */
d014dc2e 807 for (i = 0; i < ac->avail; i++) {
072bb0aa
MG
808 /* If a !PFMEMALLOC object is found, swap them */
809 if (!is_obj_pfmemalloc(ac->entry[i])) {
810 objp = ac->entry[i];
811 ac->entry[i] = ac->entry[ac->avail];
812 ac->entry[ac->avail] = objp;
813 return objp;
814 }
815 }
816
817 /*
818 * If there are empty slabs on the slabs_free list and we are
819 * being forced to refill the cache, mark this one !pfmemalloc.
820 */
ce8eb6c4
CL
821 n = cachep->node[numa_mem_id()];
822 if (!list_empty(&n->slabs_free) && force_refill) {
8456a648 823 struct page *page = virt_to_head_page(objp);
7ecccf9d 824 ClearPageSlabPfmemalloc(page);
072bb0aa
MG
825 clear_obj_pfmemalloc(&objp);
826 recheck_pfmemalloc_active(cachep, ac);
827 return objp;
828 }
829
830 /* No !PFMEMALLOC objects available */
831 ac->avail++;
832 objp = NULL;
833 }
834
835 return objp;
836}
837
381760ea
MG
838static inline void *ac_get_obj(struct kmem_cache *cachep,
839 struct array_cache *ac, gfp_t flags, bool force_refill)
840{
841 void *objp;
842
843 if (unlikely(sk_memalloc_socks()))
844 objp = __ac_get_obj(cachep, ac, flags, force_refill);
845 else
846 objp = ac->entry[--ac->avail];
847
848 return objp;
849}
850
851static void *__ac_put_obj(struct kmem_cache *cachep, struct array_cache *ac,
072bb0aa
MG
852 void *objp)
853{
854 if (unlikely(pfmemalloc_active)) {
855 /* Some pfmemalloc slabs exist, check if this is one */
30c29bea 856 struct page *page = virt_to_head_page(objp);
072bb0aa
MG
857 if (PageSlabPfmemalloc(page))
858 set_obj_pfmemalloc(&objp);
859 }
860
381760ea
MG
861 return objp;
862}
863
864static inline void ac_put_obj(struct kmem_cache *cachep, struct array_cache *ac,
865 void *objp)
866{
867 if (unlikely(sk_memalloc_socks()))
868 objp = __ac_put_obj(cachep, ac, objp);
869
072bb0aa
MG
870 ac->entry[ac->avail++] = objp;
871}
872
3ded175a
CL
873/*
874 * Transfer objects in one arraycache to another.
875 * Locking must be handled by the caller.
876 *
877 * Return the number of entries transferred.
878 */
879static int transfer_objects(struct array_cache *to,
880 struct array_cache *from, unsigned int max)
881{
882 /* Figure out how many entries to transfer */
732eacc0 883 int nr = min3(from->avail, max, to->limit - to->avail);
3ded175a
CL
884
885 if (!nr)
886 return 0;
887
888 memcpy(to->entry + to->avail, from->entry + from->avail -nr,
889 sizeof(void *) *nr);
890
891 from->avail -= nr;
892 to->avail += nr;
3ded175a
CL
893 return nr;
894}
895
765c4507
CL
896#ifndef CONFIG_NUMA
897
898#define drain_alien_cache(cachep, alien) do { } while (0)
ce8eb6c4 899#define reap_alien(cachep, n) do { } while (0)
765c4507 900
83b519e8 901static inline struct array_cache **alloc_alien_cache(int node, int limit, gfp_t gfp)
765c4507
CL
902{
903 return (struct array_cache **)BAD_ALIEN_MAGIC;
904}
905
906static inline void free_alien_cache(struct array_cache **ac_ptr)
907{
908}
909
910static inline int cache_free_alien(struct kmem_cache *cachep, void *objp)
911{
912 return 0;
913}
914
915static inline void *alternate_node_alloc(struct kmem_cache *cachep,
916 gfp_t flags)
917{
918 return NULL;
919}
920
8b98c169 921static inline void *____cache_alloc_node(struct kmem_cache *cachep,
765c4507
CL
922 gfp_t flags, int nodeid)
923{
924 return NULL;
925}
926
927#else /* CONFIG_NUMA */
928
8b98c169 929static void *____cache_alloc_node(struct kmem_cache *, gfp_t, int);
c61afb18 930static void *alternate_node_alloc(struct kmem_cache *, gfp_t);
dc85da15 931
83b519e8 932static struct array_cache **alloc_alien_cache(int node, int limit, gfp_t gfp)
e498be7d
CL
933{
934 struct array_cache **ac_ptr;
8ef82866 935 int memsize = sizeof(void *) * nr_node_ids;
e498be7d
CL
936 int i;
937
938 if (limit > 1)
939 limit = 12;
f3186a9c 940 ac_ptr = kzalloc_node(memsize, gfp, node);
e498be7d
CL
941 if (ac_ptr) {
942 for_each_node(i) {
f3186a9c 943 if (i == node || !node_online(i))
e498be7d 944 continue;
83b519e8 945 ac_ptr[i] = alloc_arraycache(node, limit, 0xbaadf00d, gfp);
e498be7d 946 if (!ac_ptr[i]) {
cc550def 947 for (i--; i >= 0; i--)
e498be7d
CL
948 kfree(ac_ptr[i]);
949 kfree(ac_ptr);
950 return NULL;
951 }
952 }
953 }
954 return ac_ptr;
955}
956
5295a74c 957static void free_alien_cache(struct array_cache **ac_ptr)
e498be7d
CL
958{
959 int i;
960
961 if (!ac_ptr)
962 return;
e498be7d 963 for_each_node(i)
b28a02de 964 kfree(ac_ptr[i]);
e498be7d
CL
965 kfree(ac_ptr);
966}
967
343e0d7a 968static void __drain_alien_cache(struct kmem_cache *cachep,
5295a74c 969 struct array_cache *ac, int node)
e498be7d 970{
ce8eb6c4 971 struct kmem_cache_node *n = cachep->node[node];
e498be7d
CL
972
973 if (ac->avail) {
ce8eb6c4 974 spin_lock(&n->list_lock);
e00946fe
CL
975 /*
976 * Stuff objects into the remote nodes shared array first.
977 * That way we could avoid the overhead of putting the objects
978 * into the free lists and getting them back later.
979 */
ce8eb6c4
CL
980 if (n->shared)
981 transfer_objects(n->shared, ac, ac->limit);
e00946fe 982
ff69416e 983 free_block(cachep, ac->entry, ac->avail, node);
e498be7d 984 ac->avail = 0;
ce8eb6c4 985 spin_unlock(&n->list_lock);
e498be7d
CL
986 }
987}
988
8fce4d8e
CL
989/*
990 * Called from cache_reap() to regularly drain alien caches round robin.
991 */
ce8eb6c4 992static void reap_alien(struct kmem_cache *cachep, struct kmem_cache_node *n)
8fce4d8e 993{
909ea964 994 int node = __this_cpu_read(slab_reap_node);
8fce4d8e 995
ce8eb6c4
CL
996 if (n->alien) {
997 struct array_cache *ac = n->alien[node];
e00946fe
CL
998
999 if (ac && ac->avail && spin_trylock_irq(&ac->lock)) {
8fce4d8e
CL
1000 __drain_alien_cache(cachep, ac, node);
1001 spin_unlock_irq(&ac->lock);
1002 }
1003 }
1004}
1005
a737b3e2
AM
1006static void drain_alien_cache(struct kmem_cache *cachep,
1007 struct array_cache **alien)
e498be7d 1008{
b28a02de 1009 int i = 0;
e498be7d
CL
1010 struct array_cache *ac;
1011 unsigned long flags;
1012
1013 for_each_online_node(i) {
4484ebf1 1014 ac = alien[i];
e498be7d
CL
1015 if (ac) {
1016 spin_lock_irqsave(&ac->lock, flags);
1017 __drain_alien_cache(cachep, ac, i);
1018 spin_unlock_irqrestore(&ac->lock, flags);
1019 }
1020 }
1021}
729bd0b7 1022
873623df 1023static inline int cache_free_alien(struct kmem_cache *cachep, void *objp)
729bd0b7 1024{
1ea991b0 1025 int nodeid = page_to_nid(virt_to_page(objp));
ce8eb6c4 1026 struct kmem_cache_node *n;
729bd0b7 1027 struct array_cache *alien = NULL;
1ca4cb24
PE
1028 int node;
1029
7d6e6d09 1030 node = numa_mem_id();
729bd0b7
PE
1031
1032 /*
1033 * Make sure we are not freeing a object from another node to the array
1034 * cache on this cpu.
1035 */
1ea991b0 1036 if (likely(nodeid == node))
729bd0b7
PE
1037 return 0;
1038
ce8eb6c4 1039 n = cachep->node[node];
729bd0b7 1040 STATS_INC_NODEFREES(cachep);
ce8eb6c4
CL
1041 if (n->alien && n->alien[nodeid]) {
1042 alien = n->alien[nodeid];
873623df 1043 spin_lock(&alien->lock);
729bd0b7
PE
1044 if (unlikely(alien->avail == alien->limit)) {
1045 STATS_INC_ACOVERFLOW(cachep);
1046 __drain_alien_cache(cachep, alien, nodeid);
1047 }
072bb0aa 1048 ac_put_obj(cachep, alien, objp);
729bd0b7
PE
1049 spin_unlock(&alien->lock);
1050 } else {
6a67368c 1051 spin_lock(&(cachep->node[nodeid])->list_lock);
729bd0b7 1052 free_block(cachep, &objp, 1, nodeid);
6a67368c 1053 spin_unlock(&(cachep->node[nodeid])->list_lock);
729bd0b7
PE
1054 }
1055 return 1;
1056}
e498be7d
CL
1057#endif
1058
8f9f8d9e 1059/*
6a67368c 1060 * Allocates and initializes node for a node on each slab cache, used for
ce8eb6c4 1061 * either memory or cpu hotplug. If memory is being hot-added, the kmem_cache_node
8f9f8d9e 1062 * will be allocated off-node since memory is not yet online for the new node.
6a67368c 1063 * When hotplugging memory or a cpu, existing node are not replaced if
8f9f8d9e
DR
1064 * already in use.
1065 *
18004c5d 1066 * Must hold slab_mutex.
8f9f8d9e 1067 */
6a67368c 1068static int init_cache_node_node(int node)
8f9f8d9e
DR
1069{
1070 struct kmem_cache *cachep;
ce8eb6c4 1071 struct kmem_cache_node *n;
6744f087 1072 const int memsize = sizeof(struct kmem_cache_node);
8f9f8d9e 1073
18004c5d 1074 list_for_each_entry(cachep, &slab_caches, list) {
8f9f8d9e
DR
1075 /*
1076 * Set up the size64 kmemlist for cpu before we can
1077 * begin anything. Make sure some other cpu on this
1078 * node has not already allocated this
1079 */
6a67368c 1080 if (!cachep->node[node]) {
ce8eb6c4
CL
1081 n = kmalloc_node(memsize, GFP_KERNEL, node);
1082 if (!n)
8f9f8d9e 1083 return -ENOMEM;
ce8eb6c4
CL
1084 kmem_cache_node_init(n);
1085 n->next_reap = jiffies + REAPTIMEOUT_LIST3 +
8f9f8d9e
DR
1086 ((unsigned long)cachep) % REAPTIMEOUT_LIST3;
1087
1088 /*
1089 * The l3s don't come and go as CPUs come and
18004c5d 1090 * go. slab_mutex is sufficient
8f9f8d9e
DR
1091 * protection here.
1092 */
ce8eb6c4 1093 cachep->node[node] = n;
8f9f8d9e
DR
1094 }
1095
6a67368c
CL
1096 spin_lock_irq(&cachep->node[node]->list_lock);
1097 cachep->node[node]->free_limit =
8f9f8d9e
DR
1098 (1 + nr_cpus_node(node)) *
1099 cachep->batchcount + cachep->num;
6a67368c 1100 spin_unlock_irq(&cachep->node[node]->list_lock);
8f9f8d9e
DR
1101 }
1102 return 0;
1103}
1104
0fa8103b
WL
1105static inline int slabs_tofree(struct kmem_cache *cachep,
1106 struct kmem_cache_node *n)
1107{
1108 return (n->free_objects + cachep->num - 1) / cachep->num;
1109}
1110
0db0628d 1111static void cpuup_canceled(long cpu)
fbf1e473
AM
1112{
1113 struct kmem_cache *cachep;
ce8eb6c4 1114 struct kmem_cache_node *n = NULL;
7d6e6d09 1115 int node = cpu_to_mem(cpu);
a70f7302 1116 const struct cpumask *mask = cpumask_of_node(node);
fbf1e473 1117
18004c5d 1118 list_for_each_entry(cachep, &slab_caches, list) {
fbf1e473
AM
1119 struct array_cache *nc;
1120 struct array_cache *shared;
1121 struct array_cache **alien;
fbf1e473 1122
fbf1e473
AM
1123 /* cpu is dead; no one can alloc from it. */
1124 nc = cachep->array[cpu];
1125 cachep->array[cpu] = NULL;
ce8eb6c4 1126 n = cachep->node[node];
fbf1e473 1127
ce8eb6c4 1128 if (!n)
fbf1e473
AM
1129 goto free_array_cache;
1130
ce8eb6c4 1131 spin_lock_irq(&n->list_lock);
fbf1e473 1132
ce8eb6c4
CL
1133 /* Free limit for this kmem_cache_node */
1134 n->free_limit -= cachep->batchcount;
fbf1e473
AM
1135 if (nc)
1136 free_block(cachep, nc->entry, nc->avail, node);
1137
58463c1f 1138 if (!cpumask_empty(mask)) {
ce8eb6c4 1139 spin_unlock_irq(&n->list_lock);
fbf1e473
AM
1140 goto free_array_cache;
1141 }
1142
ce8eb6c4 1143 shared = n->shared;
fbf1e473
AM
1144 if (shared) {
1145 free_block(cachep, shared->entry,
1146 shared->avail, node);
ce8eb6c4 1147 n->shared = NULL;
fbf1e473
AM
1148 }
1149
ce8eb6c4
CL
1150 alien = n->alien;
1151 n->alien = NULL;
fbf1e473 1152
ce8eb6c4 1153 spin_unlock_irq(&n->list_lock);
fbf1e473
AM
1154
1155 kfree(shared);
1156 if (alien) {
1157 drain_alien_cache(cachep, alien);
1158 free_alien_cache(alien);
1159 }
1160free_array_cache:
1161 kfree(nc);
1162 }
1163 /*
1164 * In the previous loop, all the objects were freed to
1165 * the respective cache's slabs, now we can go ahead and
1166 * shrink each nodelist to its limit.
1167 */
18004c5d 1168 list_for_each_entry(cachep, &slab_caches, list) {
ce8eb6c4
CL
1169 n = cachep->node[node];
1170 if (!n)
fbf1e473 1171 continue;
0fa8103b 1172 drain_freelist(cachep, n, slabs_tofree(cachep, n));
fbf1e473
AM
1173 }
1174}
1175
0db0628d 1176static int cpuup_prepare(long cpu)
1da177e4 1177{
343e0d7a 1178 struct kmem_cache *cachep;
ce8eb6c4 1179 struct kmem_cache_node *n = NULL;
7d6e6d09 1180 int node = cpu_to_mem(cpu);
8f9f8d9e 1181 int err;
1da177e4 1182
fbf1e473
AM
1183 /*
1184 * We need to do this right in the beginning since
1185 * alloc_arraycache's are going to use this list.
1186 * kmalloc_node allows us to add the slab to the right
ce8eb6c4 1187 * kmem_cache_node and not this cpu's kmem_cache_node
fbf1e473 1188 */
6a67368c 1189 err = init_cache_node_node(node);
8f9f8d9e
DR
1190 if (err < 0)
1191 goto bad;
fbf1e473
AM
1192
1193 /*
1194 * Now we can go ahead with allocating the shared arrays and
1195 * array caches
1196 */
18004c5d 1197 list_for_each_entry(cachep, &slab_caches, list) {
fbf1e473
AM
1198 struct array_cache *nc;
1199 struct array_cache *shared = NULL;
1200 struct array_cache **alien = NULL;
1201
1202 nc = alloc_arraycache(node, cachep->limit,
83b519e8 1203 cachep->batchcount, GFP_KERNEL);
fbf1e473
AM
1204 if (!nc)
1205 goto bad;
1206 if (cachep->shared) {
1207 shared = alloc_arraycache(node,
1208 cachep->shared * cachep->batchcount,
83b519e8 1209 0xbaadf00d, GFP_KERNEL);
12d00f6a
AM
1210 if (!shared) {
1211 kfree(nc);
1da177e4 1212 goto bad;
12d00f6a 1213 }
fbf1e473
AM
1214 }
1215 if (use_alien_caches) {
83b519e8 1216 alien = alloc_alien_cache(node, cachep->limit, GFP_KERNEL);
12d00f6a
AM
1217 if (!alien) {
1218 kfree(shared);
1219 kfree(nc);
fbf1e473 1220 goto bad;
12d00f6a 1221 }
fbf1e473
AM
1222 }
1223 cachep->array[cpu] = nc;
ce8eb6c4
CL
1224 n = cachep->node[node];
1225 BUG_ON(!n);
fbf1e473 1226
ce8eb6c4
CL
1227 spin_lock_irq(&n->list_lock);
1228 if (!n->shared) {
fbf1e473
AM
1229 /*
1230 * We are serialised from CPU_DEAD or
1231 * CPU_UP_CANCELLED by the cpucontrol lock
1232 */
ce8eb6c4 1233 n->shared = shared;
fbf1e473
AM
1234 shared = NULL;
1235 }
4484ebf1 1236#ifdef CONFIG_NUMA
ce8eb6c4
CL
1237 if (!n->alien) {
1238 n->alien = alien;
fbf1e473 1239 alien = NULL;
1da177e4 1240 }
fbf1e473 1241#endif
ce8eb6c4 1242 spin_unlock_irq(&n->list_lock);
fbf1e473
AM
1243 kfree(shared);
1244 free_alien_cache(alien);
83835b3d
PZ
1245 if (cachep->flags & SLAB_DEBUG_OBJECTS)
1246 slab_set_debugobj_lock_classes_node(cachep, node);
6ccfb5bc
GC
1247 else if (!OFF_SLAB(cachep) &&
1248 !(cachep->flags & SLAB_DESTROY_BY_RCU))
1249 on_slab_lock_classes_node(cachep, node);
fbf1e473 1250 }
ce79ddc8
PE
1251 init_node_lock_keys(node);
1252
fbf1e473
AM
1253 return 0;
1254bad:
12d00f6a 1255 cpuup_canceled(cpu);
fbf1e473
AM
1256 return -ENOMEM;
1257}
1258
0db0628d 1259static int cpuup_callback(struct notifier_block *nfb,
fbf1e473
AM
1260 unsigned long action, void *hcpu)
1261{
1262 long cpu = (long)hcpu;
1263 int err = 0;
1264
1265 switch (action) {
fbf1e473
AM
1266 case CPU_UP_PREPARE:
1267 case CPU_UP_PREPARE_FROZEN:
18004c5d 1268 mutex_lock(&slab_mutex);
fbf1e473 1269 err = cpuup_prepare(cpu);
18004c5d 1270 mutex_unlock(&slab_mutex);
1da177e4
LT
1271 break;
1272 case CPU_ONLINE:
8bb78442 1273 case CPU_ONLINE_FROZEN:
1da177e4
LT
1274 start_cpu_timer(cpu);
1275 break;
1276#ifdef CONFIG_HOTPLUG_CPU
5830c590 1277 case CPU_DOWN_PREPARE:
8bb78442 1278 case CPU_DOWN_PREPARE_FROZEN:
5830c590 1279 /*
18004c5d 1280 * Shutdown cache reaper. Note that the slab_mutex is
5830c590
CL
1281 * held so that if cache_reap() is invoked it cannot do
1282 * anything expensive but will only modify reap_work
1283 * and reschedule the timer.
1284 */
afe2c511 1285 cancel_delayed_work_sync(&per_cpu(slab_reap_work, cpu));
5830c590 1286 /* Now the cache_reaper is guaranteed to be not running. */
1871e52c 1287 per_cpu(slab_reap_work, cpu).work.func = NULL;
5830c590
CL
1288 break;
1289 case CPU_DOWN_FAILED:
8bb78442 1290 case CPU_DOWN_FAILED_FROZEN:
5830c590
CL
1291 start_cpu_timer(cpu);
1292 break;
1da177e4 1293 case CPU_DEAD:
8bb78442 1294 case CPU_DEAD_FROZEN:
4484ebf1
RT
1295 /*
1296 * Even if all the cpus of a node are down, we don't free the
ce8eb6c4 1297 * kmem_cache_node of any cache. This to avoid a race between
4484ebf1 1298 * cpu_down, and a kmalloc allocation from another cpu for
ce8eb6c4 1299 * memory from the node of the cpu going down. The node
4484ebf1
RT
1300 * structure is usually allocated from kmem_cache_create() and
1301 * gets destroyed at kmem_cache_destroy().
1302 */
183ff22b 1303 /* fall through */
8f5be20b 1304#endif
1da177e4 1305 case CPU_UP_CANCELED:
8bb78442 1306 case CPU_UP_CANCELED_FROZEN:
18004c5d 1307 mutex_lock(&slab_mutex);
fbf1e473 1308 cpuup_canceled(cpu);
18004c5d 1309 mutex_unlock(&slab_mutex);
1da177e4 1310 break;
1da177e4 1311 }
eac40680 1312 return notifier_from_errno(err);
1da177e4
LT
1313}
1314
0db0628d 1315static struct notifier_block cpucache_notifier = {
74b85f37
CS
1316 &cpuup_callback, NULL, 0
1317};
1da177e4 1318
8f9f8d9e
DR
1319#if defined(CONFIG_NUMA) && defined(CONFIG_MEMORY_HOTPLUG)
1320/*
1321 * Drains freelist for a node on each slab cache, used for memory hot-remove.
1322 * Returns -EBUSY if all objects cannot be drained so that the node is not
1323 * removed.
1324 *
18004c5d 1325 * Must hold slab_mutex.
8f9f8d9e 1326 */
6a67368c 1327static int __meminit drain_cache_node_node(int node)
8f9f8d9e
DR
1328{
1329 struct kmem_cache *cachep;
1330 int ret = 0;
1331
18004c5d 1332 list_for_each_entry(cachep, &slab_caches, list) {
ce8eb6c4 1333 struct kmem_cache_node *n;
8f9f8d9e 1334
ce8eb6c4
CL
1335 n = cachep->node[node];
1336 if (!n)
8f9f8d9e
DR
1337 continue;
1338
0fa8103b 1339 drain_freelist(cachep, n, slabs_tofree(cachep, n));
8f9f8d9e 1340
ce8eb6c4
CL
1341 if (!list_empty(&n->slabs_full) ||
1342 !list_empty(&n->slabs_partial)) {
8f9f8d9e
DR
1343 ret = -EBUSY;
1344 break;
1345 }
1346 }
1347 return ret;
1348}
1349
1350static int __meminit slab_memory_callback(struct notifier_block *self,
1351 unsigned long action, void *arg)
1352{
1353 struct memory_notify *mnb = arg;
1354 int ret = 0;
1355 int nid;
1356
1357 nid = mnb->status_change_nid;
1358 if (nid < 0)
1359 goto out;
1360
1361 switch (action) {
1362 case MEM_GOING_ONLINE:
18004c5d 1363 mutex_lock(&slab_mutex);
6a67368c 1364 ret = init_cache_node_node(nid);
18004c5d 1365 mutex_unlock(&slab_mutex);
8f9f8d9e
DR
1366 break;
1367 case MEM_GOING_OFFLINE:
18004c5d 1368 mutex_lock(&slab_mutex);
6a67368c 1369 ret = drain_cache_node_node(nid);
18004c5d 1370 mutex_unlock(&slab_mutex);
8f9f8d9e
DR
1371 break;
1372 case MEM_ONLINE:
1373 case MEM_OFFLINE:
1374 case MEM_CANCEL_ONLINE:
1375 case MEM_CANCEL_OFFLINE:
1376 break;
1377 }
1378out:
5fda1bd5 1379 return notifier_from_errno(ret);
8f9f8d9e
DR
1380}
1381#endif /* CONFIG_NUMA && CONFIG_MEMORY_HOTPLUG */
1382
e498be7d 1383/*
ce8eb6c4 1384 * swap the static kmem_cache_node with kmalloced memory
e498be7d 1385 */
6744f087 1386static void __init init_list(struct kmem_cache *cachep, struct kmem_cache_node *list,
8f9f8d9e 1387 int nodeid)
e498be7d 1388{
6744f087 1389 struct kmem_cache_node *ptr;
e498be7d 1390
6744f087 1391 ptr = kmalloc_node(sizeof(struct kmem_cache_node), GFP_NOWAIT, nodeid);
e498be7d
CL
1392 BUG_ON(!ptr);
1393
6744f087 1394 memcpy(ptr, list, sizeof(struct kmem_cache_node));
2b2d5493
IM
1395 /*
1396 * Do not assume that spinlocks can be initialized via memcpy:
1397 */
1398 spin_lock_init(&ptr->list_lock);
1399
e498be7d 1400 MAKE_ALL_LISTS(cachep, ptr, nodeid);
6a67368c 1401 cachep->node[nodeid] = ptr;
e498be7d
CL
1402}
1403
556a169d 1404/*
ce8eb6c4
CL
1405 * For setting up all the kmem_cache_node for cache whose buffer_size is same as
1406 * size of kmem_cache_node.
556a169d 1407 */
ce8eb6c4 1408static void __init set_up_node(struct kmem_cache *cachep, int index)
556a169d
PE
1409{
1410 int node;
1411
1412 for_each_online_node(node) {
ce8eb6c4 1413 cachep->node[node] = &init_kmem_cache_node[index + node];
6a67368c 1414 cachep->node[node]->next_reap = jiffies +
556a169d
PE
1415 REAPTIMEOUT_LIST3 +
1416 ((unsigned long)cachep) % REAPTIMEOUT_LIST3;
1417 }
1418}
1419
3c583465
CL
1420/*
1421 * The memory after the last cpu cache pointer is used for the
6a67368c 1422 * the node pointer.
3c583465 1423 */
6a67368c 1424static void setup_node_pointer(struct kmem_cache *cachep)
3c583465 1425{
6a67368c 1426 cachep->node = (struct kmem_cache_node **)&cachep->array[nr_cpu_ids];
3c583465
CL
1427}
1428
a737b3e2
AM
1429/*
1430 * Initialisation. Called after the page allocator have been initialised and
1431 * before smp_init().
1da177e4
LT
1432 */
1433void __init kmem_cache_init(void)
1434{
e498be7d
CL
1435 int i;
1436
68126702
JK
1437 BUILD_BUG_ON(sizeof(((struct page *)NULL)->lru) <
1438 sizeof(struct rcu_head));
9b030cb8 1439 kmem_cache = &kmem_cache_boot;
6a67368c 1440 setup_node_pointer(kmem_cache);
9b030cb8 1441
b6e68bc1 1442 if (num_possible_nodes() == 1)
62918a03
SS
1443 use_alien_caches = 0;
1444
3c583465 1445 for (i = 0; i < NUM_INIT_LISTS; i++)
ce8eb6c4 1446 kmem_cache_node_init(&init_kmem_cache_node[i]);
3c583465 1447
ce8eb6c4 1448 set_up_node(kmem_cache, CACHE_CACHE);
1da177e4
LT
1449
1450 /*
1451 * Fragmentation resistance on low memory - only use bigger
3df1cccd
DR
1452 * page orders on machines with more than 32MB of memory if
1453 * not overridden on the command line.
1da177e4 1454 */
3df1cccd 1455 if (!slab_max_order_set && totalram_pages > (32 << 20) >> PAGE_SHIFT)
543585cc 1456 slab_max_order = SLAB_MAX_ORDER_HI;
1da177e4 1457
1da177e4
LT
1458 /* Bootstrap is tricky, because several objects are allocated
1459 * from caches that do not exist yet:
9b030cb8
CL
1460 * 1) initialize the kmem_cache cache: it contains the struct
1461 * kmem_cache structures of all caches, except kmem_cache itself:
1462 * kmem_cache is statically allocated.
e498be7d 1463 * Initially an __init data area is used for the head array and the
ce8eb6c4 1464 * kmem_cache_node structures, it's replaced with a kmalloc allocated
e498be7d 1465 * array at the end of the bootstrap.
1da177e4 1466 * 2) Create the first kmalloc cache.
343e0d7a 1467 * The struct kmem_cache for the new cache is allocated normally.
e498be7d
CL
1468 * An __init data area is used for the head array.
1469 * 3) Create the remaining kmalloc caches, with minimally sized
1470 * head arrays.
9b030cb8 1471 * 4) Replace the __init data head arrays for kmem_cache and the first
1da177e4 1472 * kmalloc cache with kmalloc allocated arrays.
ce8eb6c4 1473 * 5) Replace the __init data for kmem_cache_node for kmem_cache and
e498be7d
CL
1474 * the other cache's with kmalloc allocated memory.
1475 * 6) Resize the head arrays of the kmalloc caches to their final sizes.
1da177e4
LT
1476 */
1477
9b030cb8 1478 /* 1) create the kmem_cache */
1da177e4 1479
8da3430d 1480 /*
b56efcf0 1481 * struct kmem_cache size depends on nr_node_ids & nr_cpu_ids
8da3430d 1482 */
2f9baa9f
CL
1483 create_boot_cache(kmem_cache, "kmem_cache",
1484 offsetof(struct kmem_cache, array[nr_cpu_ids]) +
6744f087 1485 nr_node_ids * sizeof(struct kmem_cache_node *),
2f9baa9f
CL
1486 SLAB_HWCACHE_ALIGN);
1487 list_add(&kmem_cache->list, &slab_caches);
1da177e4
LT
1488
1489 /* 2+3) create the kmalloc caches */
1da177e4 1490
a737b3e2
AM
1491 /*
1492 * Initialize the caches that provide memory for the array cache and the
ce8eb6c4 1493 * kmem_cache_node structures first. Without this, further allocations will
a737b3e2 1494 * bug.
e498be7d
CL
1495 */
1496
e3366016
CL
1497 kmalloc_caches[INDEX_AC] = create_kmalloc_cache("kmalloc-ac",
1498 kmalloc_size(INDEX_AC), ARCH_KMALLOC_FLAGS);
45530c44 1499
ce8eb6c4
CL
1500 if (INDEX_AC != INDEX_NODE)
1501 kmalloc_caches[INDEX_NODE] =
1502 create_kmalloc_cache("kmalloc-node",
1503 kmalloc_size(INDEX_NODE), ARCH_KMALLOC_FLAGS);
e498be7d 1504
e0a42726
IM
1505 slab_early_init = 0;
1506
1da177e4
LT
1507 /* 4) Replace the bootstrap head arrays */
1508 {
2b2d5493 1509 struct array_cache *ptr;
e498be7d 1510
83b519e8 1511 ptr = kmalloc(sizeof(struct arraycache_init), GFP_NOWAIT);
e498be7d 1512
9b030cb8 1513 memcpy(ptr, cpu_cache_get(kmem_cache),
b28a02de 1514 sizeof(struct arraycache_init));
2b2d5493
IM
1515 /*
1516 * Do not assume that spinlocks can be initialized via memcpy:
1517 */
1518 spin_lock_init(&ptr->lock);
1519
9b030cb8 1520 kmem_cache->array[smp_processor_id()] = ptr;
e498be7d 1521
83b519e8 1522 ptr = kmalloc(sizeof(struct arraycache_init), GFP_NOWAIT);
e498be7d 1523
e3366016 1524 BUG_ON(cpu_cache_get(kmalloc_caches[INDEX_AC])
b28a02de 1525 != &initarray_generic.cache);
e3366016 1526 memcpy(ptr, cpu_cache_get(kmalloc_caches[INDEX_AC]),
b28a02de 1527 sizeof(struct arraycache_init));
2b2d5493
IM
1528 /*
1529 * Do not assume that spinlocks can be initialized via memcpy:
1530 */
1531 spin_lock_init(&ptr->lock);
1532
e3366016 1533 kmalloc_caches[INDEX_AC]->array[smp_processor_id()] = ptr;
1da177e4 1534 }
ce8eb6c4 1535 /* 5) Replace the bootstrap kmem_cache_node */
e498be7d 1536 {
1ca4cb24
PE
1537 int nid;
1538
9c09a95c 1539 for_each_online_node(nid) {
ce8eb6c4 1540 init_list(kmem_cache, &init_kmem_cache_node[CACHE_CACHE + nid], nid);
556a169d 1541
e3366016 1542 init_list(kmalloc_caches[INDEX_AC],
ce8eb6c4 1543 &init_kmem_cache_node[SIZE_AC + nid], nid);
e498be7d 1544
ce8eb6c4
CL
1545 if (INDEX_AC != INDEX_NODE) {
1546 init_list(kmalloc_caches[INDEX_NODE],
1547 &init_kmem_cache_node[SIZE_NODE + nid], nid);
e498be7d
CL
1548 }
1549 }
1550 }
1da177e4 1551
f97d5f63 1552 create_kmalloc_caches(ARCH_KMALLOC_FLAGS);
8429db5c
PE
1553}
1554
1555void __init kmem_cache_init_late(void)
1556{
1557 struct kmem_cache *cachep;
1558
97d06609 1559 slab_state = UP;
52cef189 1560
8429db5c 1561 /* 6) resize the head arrays to their final sizes */
18004c5d
CL
1562 mutex_lock(&slab_mutex);
1563 list_for_each_entry(cachep, &slab_caches, list)
8429db5c
PE
1564 if (enable_cpucache(cachep, GFP_NOWAIT))
1565 BUG();
18004c5d 1566 mutex_unlock(&slab_mutex);
056c6241 1567
947ca185
MW
1568 /* Annotate slab for lockdep -- annotate the malloc caches */
1569 init_lock_keys();
1570
97d06609
CL
1571 /* Done! */
1572 slab_state = FULL;
1573
a737b3e2
AM
1574 /*
1575 * Register a cpu startup notifier callback that initializes
1576 * cpu_cache_get for all new cpus
1da177e4
LT
1577 */
1578 register_cpu_notifier(&cpucache_notifier);
1da177e4 1579
8f9f8d9e
DR
1580#ifdef CONFIG_NUMA
1581 /*
1582 * Register a memory hotplug callback that initializes and frees
6a67368c 1583 * node.
8f9f8d9e
DR
1584 */
1585 hotplug_memory_notifier(slab_memory_callback, SLAB_CALLBACK_PRI);
1586#endif
1587
a737b3e2
AM
1588 /*
1589 * The reap timers are started later, with a module init call: That part
1590 * of the kernel is not yet operational.
1da177e4
LT
1591 */
1592}
1593
1594static int __init cpucache_init(void)
1595{
1596 int cpu;
1597
a737b3e2
AM
1598 /*
1599 * Register the timers that return unneeded pages to the page allocator
1da177e4 1600 */
e498be7d 1601 for_each_online_cpu(cpu)
a737b3e2 1602 start_cpu_timer(cpu);
a164f896
GC
1603
1604 /* Done! */
97d06609 1605 slab_state = FULL;
1da177e4
LT
1606 return 0;
1607}
1da177e4
LT
1608__initcall(cpucache_init);
1609
8bdec192
RA
1610static noinline void
1611slab_out_of_memory(struct kmem_cache *cachep, gfp_t gfpflags, int nodeid)
1612{
ce8eb6c4 1613 struct kmem_cache_node *n;
8456a648 1614 struct page *page;
8bdec192
RA
1615 unsigned long flags;
1616 int node;
1617
1618 printk(KERN_WARNING
1619 "SLAB: Unable to allocate memory on node %d (gfp=0x%x)\n",
1620 nodeid, gfpflags);
1621 printk(KERN_WARNING " cache: %s, object size: %d, order: %d\n",
3b0efdfa 1622 cachep->name, cachep->size, cachep->gfporder);
8bdec192
RA
1623
1624 for_each_online_node(node) {
1625 unsigned long active_objs = 0, num_objs = 0, free_objects = 0;
1626 unsigned long active_slabs = 0, num_slabs = 0;
1627
ce8eb6c4
CL
1628 n = cachep->node[node];
1629 if (!n)
8bdec192
RA
1630 continue;
1631
ce8eb6c4 1632 spin_lock_irqsave(&n->list_lock, flags);
8456a648 1633 list_for_each_entry(page, &n->slabs_full, lru) {
8bdec192
RA
1634 active_objs += cachep->num;
1635 active_slabs++;
1636 }
8456a648
JK
1637 list_for_each_entry(page, &n->slabs_partial, lru) {
1638 active_objs += page->active;
8bdec192
RA
1639 active_slabs++;
1640 }
8456a648 1641 list_for_each_entry(page, &n->slabs_free, lru)
8bdec192
RA
1642 num_slabs++;
1643
ce8eb6c4
CL
1644 free_objects += n->free_objects;
1645 spin_unlock_irqrestore(&n->list_lock, flags);
8bdec192
RA
1646
1647 num_slabs += active_slabs;
1648 num_objs = num_slabs * cachep->num;
1649 printk(KERN_WARNING
1650 " node %d: slabs: %ld/%ld, objs: %ld/%ld, free: %ld\n",
1651 node, active_slabs, num_slabs, active_objs, num_objs,
1652 free_objects);
1653 }
1654}
1655
1da177e4
LT
1656/*
1657 * Interface to system's page allocator. No need to hold the cache-lock.
1658 *
1659 * If we requested dmaable memory, we will get it. Even if we
1660 * did not request dmaable memory, we might get it, but that
1661 * would be relatively rare and ignorable.
1662 */
0c3aa83e
JK
1663static struct page *kmem_getpages(struct kmem_cache *cachep, gfp_t flags,
1664 int nodeid)
1da177e4
LT
1665{
1666 struct page *page;
e1b6aa6f 1667 int nr_pages;
765c4507 1668
a618e89f 1669 flags |= cachep->allocflags;
e12ba74d
MG
1670 if (cachep->flags & SLAB_RECLAIM_ACCOUNT)
1671 flags |= __GFP_RECLAIMABLE;
e1b6aa6f 1672
517d0869 1673 page = alloc_pages_exact_node(nodeid, flags | __GFP_NOTRACK, cachep->gfporder);
8bdec192
RA
1674 if (!page) {
1675 if (!(flags & __GFP_NOWARN) && printk_ratelimit())
1676 slab_out_of_memory(cachep, flags, nodeid);
1da177e4 1677 return NULL;
8bdec192 1678 }
1da177e4 1679
b37f1dd0 1680 /* Record if ALLOC_NO_WATERMARKS was set when allocating the slab */
072bb0aa
MG
1681 if (unlikely(page->pfmemalloc))
1682 pfmemalloc_active = true;
1683
e1b6aa6f 1684 nr_pages = (1 << cachep->gfporder);
1da177e4 1685 if (cachep->flags & SLAB_RECLAIM_ACCOUNT)
972d1a7b
CL
1686 add_zone_page_state(page_zone(page),
1687 NR_SLAB_RECLAIMABLE, nr_pages);
1688 else
1689 add_zone_page_state(page_zone(page),
1690 NR_SLAB_UNRECLAIMABLE, nr_pages);
a57a4988
JK
1691 __SetPageSlab(page);
1692 if (page->pfmemalloc)
1693 SetPageSlabPfmemalloc(page);
1f458cbf 1694 memcg_bind_pages(cachep, cachep->gfporder);
072bb0aa 1695
b1eeab67
VN
1696 if (kmemcheck_enabled && !(cachep->flags & SLAB_NOTRACK)) {
1697 kmemcheck_alloc_shadow(page, cachep->gfporder, flags, nodeid);
1698
1699 if (cachep->ctor)
1700 kmemcheck_mark_uninitialized_pages(page, nr_pages);
1701 else
1702 kmemcheck_mark_unallocated_pages(page, nr_pages);
1703 }
c175eea4 1704
0c3aa83e 1705 return page;
1da177e4
LT
1706}
1707
1708/*
1709 * Interface to system's page release.
1710 */
0c3aa83e 1711static void kmem_freepages(struct kmem_cache *cachep, struct page *page)
1da177e4 1712{
a57a4988 1713 const unsigned long nr_freed = (1 << cachep->gfporder);
1da177e4 1714
b1eeab67 1715 kmemcheck_free_shadow(page, cachep->gfporder);
c175eea4 1716
972d1a7b
CL
1717 if (cachep->flags & SLAB_RECLAIM_ACCOUNT)
1718 sub_zone_page_state(page_zone(page),
1719 NR_SLAB_RECLAIMABLE, nr_freed);
1720 else
1721 sub_zone_page_state(page_zone(page),
1722 NR_SLAB_UNRECLAIMABLE, nr_freed);
73293c2f 1723
a57a4988 1724 BUG_ON(!PageSlab(page));
73293c2f 1725 __ClearPageSlabPfmemalloc(page);
a57a4988 1726 __ClearPageSlab(page);
8456a648
JK
1727 page_mapcount_reset(page);
1728 page->mapping = NULL;
1f458cbf
GC
1729
1730 memcg_release_pages(cachep, cachep->gfporder);
1da177e4
LT
1731 if (current->reclaim_state)
1732 current->reclaim_state->reclaimed_slab += nr_freed;
0c3aa83e 1733 __free_memcg_kmem_pages(page, cachep->gfporder);
1da177e4
LT
1734}
1735
1736static void kmem_rcu_free(struct rcu_head *head)
1737{
68126702
JK
1738 struct kmem_cache *cachep;
1739 struct page *page;
1da177e4 1740
68126702
JK
1741 page = container_of(head, struct page, rcu_head);
1742 cachep = page->slab_cache;
1743
1744 kmem_freepages(cachep, page);
1da177e4
LT
1745}
1746
1747#if DEBUG
1748
1749#ifdef CONFIG_DEBUG_PAGEALLOC
343e0d7a 1750static void store_stackinfo(struct kmem_cache *cachep, unsigned long *addr,
b28a02de 1751 unsigned long caller)
1da177e4 1752{
8c138bc0 1753 int size = cachep->object_size;
1da177e4 1754
3dafccf2 1755 addr = (unsigned long *)&((char *)addr)[obj_offset(cachep)];
1da177e4 1756
b28a02de 1757 if (size < 5 * sizeof(unsigned long))
1da177e4
LT
1758 return;
1759
b28a02de
PE
1760 *addr++ = 0x12345678;
1761 *addr++ = caller;
1762 *addr++ = smp_processor_id();
1763 size -= 3 * sizeof(unsigned long);
1da177e4
LT
1764 {
1765 unsigned long *sptr = &caller;
1766 unsigned long svalue;
1767
1768 while (!kstack_end(sptr)) {
1769 svalue = *sptr++;
1770 if (kernel_text_address(svalue)) {
b28a02de 1771 *addr++ = svalue;
1da177e4
LT
1772 size -= sizeof(unsigned long);
1773 if (size <= sizeof(unsigned long))
1774 break;
1775 }
1776 }
1777
1778 }
b28a02de 1779 *addr++ = 0x87654321;
1da177e4
LT
1780}
1781#endif
1782
343e0d7a 1783static void poison_obj(struct kmem_cache *cachep, void *addr, unsigned char val)
1da177e4 1784{
8c138bc0 1785 int size = cachep->object_size;
3dafccf2 1786 addr = &((char *)addr)[obj_offset(cachep)];
1da177e4
LT
1787
1788 memset(addr, val, size);
b28a02de 1789 *(unsigned char *)(addr + size - 1) = POISON_END;
1da177e4
LT
1790}
1791
1792static void dump_line(char *data, int offset, int limit)
1793{
1794 int i;
aa83aa40
DJ
1795 unsigned char error = 0;
1796 int bad_count = 0;
1797
fdde6abb 1798 printk(KERN_ERR "%03x: ", offset);
aa83aa40
DJ
1799 for (i = 0; i < limit; i++) {
1800 if (data[offset + i] != POISON_FREE) {
1801 error = data[offset + i];
1802 bad_count++;
1803 }
aa83aa40 1804 }
fdde6abb
SAS
1805 print_hex_dump(KERN_CONT, "", 0, 16, 1,
1806 &data[offset], limit, 1);
aa83aa40
DJ
1807
1808 if (bad_count == 1) {
1809 error ^= POISON_FREE;
1810 if (!(error & (error - 1))) {
1811 printk(KERN_ERR "Single bit error detected. Probably "
1812 "bad RAM.\n");
1813#ifdef CONFIG_X86
1814 printk(KERN_ERR "Run memtest86+ or a similar memory "
1815 "test tool.\n");
1816#else
1817 printk(KERN_ERR "Run a memory test tool.\n");
1818#endif
1819 }
1820 }
1da177e4
LT
1821}
1822#endif
1823
1824#if DEBUG
1825
343e0d7a 1826static void print_objinfo(struct kmem_cache *cachep, void *objp, int lines)
1da177e4
LT
1827{
1828 int i, size;
1829 char *realobj;
1830
1831 if (cachep->flags & SLAB_RED_ZONE) {
b46b8f19 1832 printk(KERN_ERR "Redzone: 0x%llx/0x%llx.\n",
a737b3e2
AM
1833 *dbg_redzone1(cachep, objp),
1834 *dbg_redzone2(cachep, objp));
1da177e4
LT
1835 }
1836
1837 if (cachep->flags & SLAB_STORE_USER) {
071361d3
JP
1838 printk(KERN_ERR "Last user: [<%p>](%pSR)\n",
1839 *dbg_userword(cachep, objp),
1840 *dbg_userword(cachep, objp));
1da177e4 1841 }
3dafccf2 1842 realobj = (char *)objp + obj_offset(cachep);
8c138bc0 1843 size = cachep->object_size;
b28a02de 1844 for (i = 0; i < size && lines; i += 16, lines--) {
1da177e4
LT
1845 int limit;
1846 limit = 16;
b28a02de
PE
1847 if (i + limit > size)
1848 limit = size - i;
1da177e4
LT
1849 dump_line(realobj, i, limit);
1850 }
1851}
1852
343e0d7a 1853static void check_poison_obj(struct kmem_cache *cachep, void *objp)
1da177e4
LT
1854{
1855 char *realobj;
1856 int size, i;
1857 int lines = 0;
1858
3dafccf2 1859 realobj = (char *)objp + obj_offset(cachep);
8c138bc0 1860 size = cachep->object_size;
1da177e4 1861
b28a02de 1862 for (i = 0; i < size; i++) {
1da177e4 1863 char exp = POISON_FREE;
b28a02de 1864 if (i == size - 1)
1da177e4
LT
1865 exp = POISON_END;
1866 if (realobj[i] != exp) {
1867 int limit;
1868 /* Mismatch ! */
1869 /* Print header */
1870 if (lines == 0) {
b28a02de 1871 printk(KERN_ERR
face37f5
DJ
1872 "Slab corruption (%s): %s start=%p, len=%d\n",
1873 print_tainted(), cachep->name, realobj, size);
1da177e4
LT
1874 print_objinfo(cachep, objp, 0);
1875 }
1876 /* Hexdump the affected line */
b28a02de 1877 i = (i / 16) * 16;
1da177e4 1878 limit = 16;
b28a02de
PE
1879 if (i + limit > size)
1880 limit = size - i;
1da177e4
LT
1881 dump_line(realobj, i, limit);
1882 i += 16;
1883 lines++;
1884 /* Limit to 5 lines */
1885 if (lines > 5)
1886 break;
1887 }
1888 }
1889 if (lines != 0) {
1890 /* Print some data about the neighboring objects, if they
1891 * exist:
1892 */
8456a648 1893 struct page *page = virt_to_head_page(objp);
8fea4e96 1894 unsigned int objnr;
1da177e4 1895
8456a648 1896 objnr = obj_to_index(cachep, page, objp);
1da177e4 1897 if (objnr) {
8456a648 1898 objp = index_to_obj(cachep, page, objnr - 1);
3dafccf2 1899 realobj = (char *)objp + obj_offset(cachep);
1da177e4 1900 printk(KERN_ERR "Prev obj: start=%p, len=%d\n",
b28a02de 1901 realobj, size);
1da177e4
LT
1902 print_objinfo(cachep, objp, 2);
1903 }
b28a02de 1904 if (objnr + 1 < cachep->num) {
8456a648 1905 objp = index_to_obj(cachep, page, objnr + 1);
3dafccf2 1906 realobj = (char *)objp + obj_offset(cachep);
1da177e4 1907 printk(KERN_ERR "Next obj: start=%p, len=%d\n",
b28a02de 1908 realobj, size);
1da177e4
LT
1909 print_objinfo(cachep, objp, 2);
1910 }
1911 }
1912}
1913#endif
1914
12dd36fa 1915#if DEBUG
8456a648
JK
1916static void slab_destroy_debugcheck(struct kmem_cache *cachep,
1917 struct page *page)
1da177e4 1918{
1da177e4
LT
1919 int i;
1920 for (i = 0; i < cachep->num; i++) {
8456a648 1921 void *objp = index_to_obj(cachep, page, i);
1da177e4
LT
1922
1923 if (cachep->flags & SLAB_POISON) {
1924#ifdef CONFIG_DEBUG_PAGEALLOC
3b0efdfa 1925 if (cachep->size % PAGE_SIZE == 0 &&
a737b3e2 1926 OFF_SLAB(cachep))
b28a02de 1927 kernel_map_pages(virt_to_page(objp),
3b0efdfa 1928 cachep->size / PAGE_SIZE, 1);
1da177e4
LT
1929 else
1930 check_poison_obj(cachep, objp);
1931#else
1932 check_poison_obj(cachep, objp);
1933#endif
1934 }
1935 if (cachep->flags & SLAB_RED_ZONE) {
1936 if (*dbg_redzone1(cachep, objp) != RED_INACTIVE)
1937 slab_error(cachep, "start of a freed object "
b28a02de 1938 "was overwritten");
1da177e4
LT
1939 if (*dbg_redzone2(cachep, objp) != RED_INACTIVE)
1940 slab_error(cachep, "end of a freed object "
b28a02de 1941 "was overwritten");
1da177e4 1942 }
1da177e4 1943 }
12dd36fa 1944}
1da177e4 1945#else
8456a648
JK
1946static void slab_destroy_debugcheck(struct kmem_cache *cachep,
1947 struct page *page)
12dd36fa 1948{
12dd36fa 1949}
1da177e4
LT
1950#endif
1951
911851e6
RD
1952/**
1953 * slab_destroy - destroy and release all objects in a slab
1954 * @cachep: cache pointer being destroyed
cb8ee1a3 1955 * @page: page pointer being destroyed
911851e6 1956 *
12dd36fa 1957 * Destroy all the objs in a slab, and release the mem back to the system.
a737b3e2
AM
1958 * Before calling the slab must have been unlinked from the cache. The
1959 * cache-lock is not held/needed.
12dd36fa 1960 */
8456a648 1961static void slab_destroy(struct kmem_cache *cachep, struct page *page)
12dd36fa 1962{
7e007355 1963 void *freelist;
12dd36fa 1964
8456a648
JK
1965 freelist = page->freelist;
1966 slab_destroy_debugcheck(cachep, page);
1da177e4 1967 if (unlikely(cachep->flags & SLAB_DESTROY_BY_RCU)) {
68126702
JK
1968 struct rcu_head *head;
1969
1970 /*
1971 * RCU free overloads the RCU head over the LRU.
1972 * slab_page has been overloeaded over the LRU,
1973 * however it is not used from now on so that
1974 * we can use it safely.
1975 */
1976 head = (void *)&page->rcu_head;
1977 call_rcu(head, kmem_rcu_free);
1da177e4 1978
1da177e4 1979 } else {
0c3aa83e 1980 kmem_freepages(cachep, page);
1da177e4 1981 }
68126702
JK
1982
1983 /*
8456a648 1984 * From now on, we don't use freelist
68126702
JK
1985 * although actual page can be freed in rcu context
1986 */
1987 if (OFF_SLAB(cachep))
8456a648 1988 kmem_cache_free(cachep->freelist_cache, freelist);
1da177e4
LT
1989}
1990
4d268eba 1991/**
a70773dd
RD
1992 * calculate_slab_order - calculate size (page order) of slabs
1993 * @cachep: pointer to the cache that is being created
1994 * @size: size of objects to be created in this cache.
1995 * @align: required alignment for the objects.
1996 * @flags: slab allocation flags
1997 *
1998 * Also calculates the number of objects per slab.
4d268eba
PE
1999 *
2000 * This could be made much more intelligent. For now, try to avoid using
2001 * high order pages for slabs. When the gfp() functions are more friendly
2002 * towards high-order requests, this should be changed.
2003 */
a737b3e2 2004static size_t calculate_slab_order(struct kmem_cache *cachep,
ee13d785 2005 size_t size, size_t align, unsigned long flags)
4d268eba 2006{
b1ab41c4 2007 unsigned long offslab_limit;
4d268eba 2008 size_t left_over = 0;
9888e6fa 2009 int gfporder;
4d268eba 2010
0aa817f0 2011 for (gfporder = 0; gfporder <= KMALLOC_MAX_ORDER; gfporder++) {
4d268eba
PE
2012 unsigned int num;
2013 size_t remainder;
2014
9888e6fa 2015 cache_estimate(gfporder, size, align, flags, &remainder, &num);
4d268eba
PE
2016 if (!num)
2017 continue;
9888e6fa 2018
b1ab41c4
IM
2019 if (flags & CFLGS_OFF_SLAB) {
2020 /*
2021 * Max number of objs-per-slab for caches which
2022 * use off-slab slabs. Needed to avoid a possible
2023 * looping condition in cache_grow().
2024 */
8456a648 2025 offslab_limit = size;
16025177 2026 offslab_limit /= sizeof(unsigned int);
b1ab41c4
IM
2027
2028 if (num > offslab_limit)
2029 break;
2030 }
4d268eba 2031
9888e6fa 2032 /* Found something acceptable - save it away */
4d268eba 2033 cachep->num = num;
9888e6fa 2034 cachep->gfporder = gfporder;
4d268eba
PE
2035 left_over = remainder;
2036
f78bb8ad
LT
2037 /*
2038 * A VFS-reclaimable slab tends to have most allocations
2039 * as GFP_NOFS and we really don't want to have to be allocating
2040 * higher-order pages when we are unable to shrink dcache.
2041 */
2042 if (flags & SLAB_RECLAIM_ACCOUNT)
2043 break;
2044
4d268eba
PE
2045 /*
2046 * Large number of objects is good, but very large slabs are
2047 * currently bad for the gfp()s.
2048 */
543585cc 2049 if (gfporder >= slab_max_order)
4d268eba
PE
2050 break;
2051
9888e6fa
LT
2052 /*
2053 * Acceptable internal fragmentation?
2054 */
a737b3e2 2055 if (left_over * 8 <= (PAGE_SIZE << gfporder))
4d268eba
PE
2056 break;
2057 }
2058 return left_over;
2059}
2060
83b519e8 2061static int __init_refok setup_cpu_cache(struct kmem_cache *cachep, gfp_t gfp)
f30cf7d1 2062{
97d06609 2063 if (slab_state >= FULL)
83b519e8 2064 return enable_cpucache(cachep, gfp);
2ed3a4ef 2065
97d06609 2066 if (slab_state == DOWN) {
f30cf7d1 2067 /*
2f9baa9f 2068 * Note: Creation of first cache (kmem_cache).
ce8eb6c4 2069 * The setup_node is taken care
2f9baa9f
CL
2070 * of by the caller of __kmem_cache_create
2071 */
2072 cachep->array[smp_processor_id()] = &initarray_generic.cache;
2073 slab_state = PARTIAL;
2074 } else if (slab_state == PARTIAL) {
2075 /*
2076 * Note: the second kmem_cache_create must create the cache
f30cf7d1
PE
2077 * that's used by kmalloc(24), otherwise the creation of
2078 * further caches will BUG().
2079 */
2080 cachep->array[smp_processor_id()] = &initarray_generic.cache;
2081
2082 /*
ce8eb6c4
CL
2083 * If the cache that's used by kmalloc(sizeof(kmem_cache_node)) is
2084 * the second cache, then we need to set up all its node/,
f30cf7d1
PE
2085 * otherwise the creation of further caches will BUG().
2086 */
ce8eb6c4
CL
2087 set_up_node(cachep, SIZE_AC);
2088 if (INDEX_AC == INDEX_NODE)
2089 slab_state = PARTIAL_NODE;
f30cf7d1 2090 else
97d06609 2091 slab_state = PARTIAL_ARRAYCACHE;
f30cf7d1 2092 } else {
2f9baa9f 2093 /* Remaining boot caches */
f30cf7d1 2094 cachep->array[smp_processor_id()] =
83b519e8 2095 kmalloc(sizeof(struct arraycache_init), gfp);
f30cf7d1 2096
97d06609 2097 if (slab_state == PARTIAL_ARRAYCACHE) {
ce8eb6c4
CL
2098 set_up_node(cachep, SIZE_NODE);
2099 slab_state = PARTIAL_NODE;
f30cf7d1
PE
2100 } else {
2101 int node;
556a169d 2102 for_each_online_node(node) {
6a67368c 2103 cachep->node[node] =
6744f087 2104 kmalloc_node(sizeof(struct kmem_cache_node),
eb91f1d0 2105 gfp, node);
6a67368c 2106 BUG_ON(!cachep->node[node]);
ce8eb6c4 2107 kmem_cache_node_init(cachep->node[node]);
f30cf7d1
PE
2108 }
2109 }
2110 }
6a67368c 2111 cachep->node[numa_mem_id()]->next_reap =
f30cf7d1
PE
2112 jiffies + REAPTIMEOUT_LIST3 +
2113 ((unsigned long)cachep) % REAPTIMEOUT_LIST3;
2114
2115 cpu_cache_get(cachep)->avail = 0;
2116 cpu_cache_get(cachep)->limit = BOOT_CPUCACHE_ENTRIES;
2117 cpu_cache_get(cachep)->batchcount = 1;
2118 cpu_cache_get(cachep)->touched = 0;
2119 cachep->batchcount = 1;
2120 cachep->limit = BOOT_CPUCACHE_ENTRIES;
2ed3a4ef 2121 return 0;
f30cf7d1
PE
2122}
2123
1da177e4 2124/**
039363f3 2125 * __kmem_cache_create - Create a cache.
a755b76a 2126 * @cachep: cache management descriptor
1da177e4 2127 * @flags: SLAB flags
1da177e4
LT
2128 *
2129 * Returns a ptr to the cache on success, NULL on failure.
2130 * Cannot be called within a int, but can be interrupted.
20c2df83 2131 * The @ctor is run when new pages are allocated by the cache.
1da177e4 2132 *
1da177e4
LT
2133 * The flags are
2134 *
2135 * %SLAB_POISON - Poison the slab with a known test pattern (a5a5a5a5)
2136 * to catch references to uninitialised memory.
2137 *
2138 * %SLAB_RED_ZONE - Insert `Red' zones around the allocated memory to check
2139 * for buffer overruns.
2140 *
1da177e4
LT
2141 * %SLAB_HWCACHE_ALIGN - Align the objects in this cache to a hardware
2142 * cacheline. This can be beneficial if you're counting cycles as closely
2143 * as davem.
2144 */
278b1bb1 2145int
8a13a4cc 2146__kmem_cache_create (struct kmem_cache *cachep, unsigned long flags)
1da177e4 2147{
8456a648 2148 size_t left_over, freelist_size, ralign;
83b519e8 2149 gfp_t gfp;
278b1bb1 2150 int err;
8a13a4cc 2151 size_t size = cachep->size;
1da177e4 2152
1da177e4 2153#if DEBUG
1da177e4
LT
2154#if FORCED_DEBUG
2155 /*
2156 * Enable redzoning and last user accounting, except for caches with
2157 * large objects, if the increased size would increase the object size
2158 * above the next power of two: caches with object sizes just above a
2159 * power of two have a significant amount of internal fragmentation.
2160 */
87a927c7
DW
2161 if (size < 4096 || fls(size - 1) == fls(size-1 + REDZONE_ALIGN +
2162 2 * sizeof(unsigned long long)))
b28a02de 2163 flags |= SLAB_RED_ZONE | SLAB_STORE_USER;
1da177e4
LT
2164 if (!(flags & SLAB_DESTROY_BY_RCU))
2165 flags |= SLAB_POISON;
2166#endif
2167 if (flags & SLAB_DESTROY_BY_RCU)
2168 BUG_ON(flags & SLAB_POISON);
2169#endif
1da177e4 2170
a737b3e2
AM
2171 /*
2172 * Check that size is in terms of words. This is needed to avoid
1da177e4
LT
2173 * unaligned accesses for some archs when redzoning is used, and makes
2174 * sure any on-slab bufctl's are also correctly aligned.
2175 */
b28a02de
PE
2176 if (size & (BYTES_PER_WORD - 1)) {
2177 size += (BYTES_PER_WORD - 1);
2178 size &= ~(BYTES_PER_WORD - 1);
1da177e4
LT
2179 }
2180
ca5f9703 2181 /*
87a927c7
DW
2182 * Redzoning and user store require word alignment or possibly larger.
2183 * Note this will be overridden by architecture or caller mandated
2184 * alignment if either is greater than BYTES_PER_WORD.
ca5f9703 2185 */
87a927c7
DW
2186 if (flags & SLAB_STORE_USER)
2187 ralign = BYTES_PER_WORD;
2188
2189 if (flags & SLAB_RED_ZONE) {
2190 ralign = REDZONE_ALIGN;
2191 /* If redzoning, ensure that the second redzone is suitably
2192 * aligned, by adjusting the object size accordingly. */
2193 size += REDZONE_ALIGN - 1;
2194 size &= ~(REDZONE_ALIGN - 1);
2195 }
ca5f9703 2196
a44b56d3 2197 /* 3) caller mandated alignment */
8a13a4cc
CL
2198 if (ralign < cachep->align) {
2199 ralign = cachep->align;
1da177e4 2200 }
3ff84a7f
PE
2201 /* disable debug if necessary */
2202 if (ralign > __alignof__(unsigned long long))
a44b56d3 2203 flags &= ~(SLAB_RED_ZONE | SLAB_STORE_USER);
a737b3e2 2204 /*
ca5f9703 2205 * 4) Store it.
1da177e4 2206 */
8a13a4cc 2207 cachep->align = ralign;
1da177e4 2208
83b519e8
PE
2209 if (slab_is_available())
2210 gfp = GFP_KERNEL;
2211 else
2212 gfp = GFP_NOWAIT;
2213
6a67368c 2214 setup_node_pointer(cachep);
1da177e4 2215#if DEBUG
1da177e4 2216
ca5f9703
PE
2217 /*
2218 * Both debugging options require word-alignment which is calculated
2219 * into align above.
2220 */
1da177e4 2221 if (flags & SLAB_RED_ZONE) {
1da177e4 2222 /* add space for red zone words */
3ff84a7f
PE
2223 cachep->obj_offset += sizeof(unsigned long long);
2224 size += 2 * sizeof(unsigned long long);
1da177e4
LT
2225 }
2226 if (flags & SLAB_STORE_USER) {
ca5f9703 2227 /* user store requires one word storage behind the end of
87a927c7
DW
2228 * the real object. But if the second red zone needs to be
2229 * aligned to 64 bits, we must allow that much space.
1da177e4 2230 */
87a927c7
DW
2231 if (flags & SLAB_RED_ZONE)
2232 size += REDZONE_ALIGN;
2233 else
2234 size += BYTES_PER_WORD;
1da177e4
LT
2235 }
2236#if FORCED_DEBUG && defined(CONFIG_DEBUG_PAGEALLOC)
ce8eb6c4 2237 if (size >= kmalloc_size(INDEX_NODE + 1)
608da7e3
TH
2238 && cachep->object_size > cache_line_size()
2239 && ALIGN(size, cachep->align) < PAGE_SIZE) {
2240 cachep->obj_offset += PAGE_SIZE - ALIGN(size, cachep->align);
1da177e4
LT
2241 size = PAGE_SIZE;
2242 }
2243#endif
2244#endif
2245
e0a42726
IM
2246 /*
2247 * Determine if the slab management is 'on' or 'off' slab.
2248 * (bootstrapping cannot cope with offslab caches so don't do
e7cb55b9
CM
2249 * it too early on. Always use on-slab management when
2250 * SLAB_NOLEAKTRACE to avoid recursive calls into kmemleak)
e0a42726 2251 */
e7cb55b9
CM
2252 if ((size >= (PAGE_SIZE >> 3)) && !slab_early_init &&
2253 !(flags & SLAB_NOLEAKTRACE))
1da177e4
LT
2254 /*
2255 * Size is large, assume best to place the slab management obj
2256 * off-slab (should allow better packing of objs).
2257 */
2258 flags |= CFLGS_OFF_SLAB;
2259
8a13a4cc 2260 size = ALIGN(size, cachep->align);
1da177e4 2261
8a13a4cc 2262 left_over = calculate_slab_order(cachep, size, cachep->align, flags);
1da177e4 2263
8a13a4cc 2264 if (!cachep->num)
278b1bb1 2265 return -E2BIG;
1da177e4 2266
8456a648
JK
2267 freelist_size =
2268 ALIGN(cachep->num * sizeof(unsigned int), cachep->align);
1da177e4
LT
2269
2270 /*
2271 * If the slab has been placed off-slab, and we have enough space then
2272 * move it on-slab. This is at the expense of any extra colouring.
2273 */
8456a648 2274 if (flags & CFLGS_OFF_SLAB && left_over >= freelist_size) {
1da177e4 2275 flags &= ~CFLGS_OFF_SLAB;
8456a648 2276 left_over -= freelist_size;
1da177e4
LT
2277 }
2278
2279 if (flags & CFLGS_OFF_SLAB) {
2280 /* really off slab. No need for manual alignment */
8456a648 2281 freelist_size = cachep->num * sizeof(unsigned int);
67461365
RL
2282
2283#ifdef CONFIG_PAGE_POISONING
2284 /* If we're going to use the generic kernel_map_pages()
2285 * poisoning, then it's going to smash the contents of
2286 * the redzone and userword anyhow, so switch them off.
2287 */
2288 if (size % PAGE_SIZE == 0 && flags & SLAB_POISON)
2289 flags &= ~(SLAB_RED_ZONE | SLAB_STORE_USER);
2290#endif
1da177e4
LT
2291 }
2292
2293 cachep->colour_off = cache_line_size();
2294 /* Offset must be a multiple of the alignment. */
8a13a4cc
CL
2295 if (cachep->colour_off < cachep->align)
2296 cachep->colour_off = cachep->align;
b28a02de 2297 cachep->colour = left_over / cachep->colour_off;
8456a648 2298 cachep->freelist_size = freelist_size;
1da177e4 2299 cachep->flags = flags;
a57a4988 2300 cachep->allocflags = __GFP_COMP;
4b51d669 2301 if (CONFIG_ZONE_DMA_FLAG && (flags & SLAB_CACHE_DMA))
a618e89f 2302 cachep->allocflags |= GFP_DMA;
3b0efdfa 2303 cachep->size = size;
6a2d7a95 2304 cachep->reciprocal_buffer_size = reciprocal_value(size);
1da177e4 2305
e5ac9c5a 2306 if (flags & CFLGS_OFF_SLAB) {
8456a648 2307 cachep->freelist_cache = kmalloc_slab(freelist_size, 0u);
e5ac9c5a
RT
2308 /*
2309 * This is a possibility for one of the malloc_sizes caches.
2310 * But since we go off slab only for object size greater than
2311 * PAGE_SIZE/8, and malloc_sizes gets created in ascending order,
2312 * this should not happen at all.
2313 * But leave a BUG_ON for some lucky dude.
2314 */
8456a648 2315 BUG_ON(ZERO_OR_NULL_PTR(cachep->freelist_cache));
e5ac9c5a 2316 }
1da177e4 2317
278b1bb1
CL
2318 err = setup_cpu_cache(cachep, gfp);
2319 if (err) {
12c3667f 2320 __kmem_cache_shutdown(cachep);
278b1bb1 2321 return err;
2ed3a4ef 2322 }
1da177e4 2323
83835b3d
PZ
2324 if (flags & SLAB_DEBUG_OBJECTS) {
2325 /*
2326 * Would deadlock through slab_destroy()->call_rcu()->
2327 * debug_object_activate()->kmem_cache_alloc().
2328 */
2329 WARN_ON_ONCE(flags & SLAB_DESTROY_BY_RCU);
2330
2331 slab_set_debugobj_lock_classes(cachep);
6ccfb5bc
GC
2332 } else if (!OFF_SLAB(cachep) && !(flags & SLAB_DESTROY_BY_RCU))
2333 on_slab_lock_classes(cachep);
83835b3d 2334
278b1bb1 2335 return 0;
1da177e4 2336}
1da177e4
LT
2337
2338#if DEBUG
2339static void check_irq_off(void)
2340{
2341 BUG_ON(!irqs_disabled());
2342}
2343
2344static void check_irq_on(void)
2345{
2346 BUG_ON(irqs_disabled());
2347}
2348
343e0d7a 2349static void check_spinlock_acquired(struct kmem_cache *cachep)
1da177e4
LT
2350{
2351#ifdef CONFIG_SMP
2352 check_irq_off();
6a67368c 2353 assert_spin_locked(&cachep->node[numa_mem_id()]->list_lock);
1da177e4
LT
2354#endif
2355}
e498be7d 2356
343e0d7a 2357static void check_spinlock_acquired_node(struct kmem_cache *cachep, int node)
e498be7d
CL
2358{
2359#ifdef CONFIG_SMP
2360 check_irq_off();
6a67368c 2361 assert_spin_locked(&cachep->node[node]->list_lock);
e498be7d
CL
2362#endif
2363}
2364
1da177e4
LT
2365#else
2366#define check_irq_off() do { } while(0)
2367#define check_irq_on() do { } while(0)
2368#define check_spinlock_acquired(x) do { } while(0)
e498be7d 2369#define check_spinlock_acquired_node(x, y) do { } while(0)
1da177e4
LT
2370#endif
2371
ce8eb6c4 2372static void drain_array(struct kmem_cache *cachep, struct kmem_cache_node *n,
aab2207c
CL
2373 struct array_cache *ac,
2374 int force, int node);
2375
1da177e4
LT
2376static void do_drain(void *arg)
2377{
a737b3e2 2378 struct kmem_cache *cachep = arg;
1da177e4 2379 struct array_cache *ac;
7d6e6d09 2380 int node = numa_mem_id();
1da177e4
LT
2381
2382 check_irq_off();
9a2dba4b 2383 ac = cpu_cache_get(cachep);
6a67368c 2384 spin_lock(&cachep->node[node]->list_lock);
ff69416e 2385 free_block(cachep, ac->entry, ac->avail, node);
6a67368c 2386 spin_unlock(&cachep->node[node]->list_lock);
1da177e4
LT
2387 ac->avail = 0;
2388}
2389
343e0d7a 2390static void drain_cpu_caches(struct kmem_cache *cachep)
1da177e4 2391{
ce8eb6c4 2392 struct kmem_cache_node *n;
e498be7d
CL
2393 int node;
2394
15c8b6c1 2395 on_each_cpu(do_drain, cachep, 1);
1da177e4 2396 check_irq_on();
b28a02de 2397 for_each_online_node(node) {
ce8eb6c4
CL
2398 n = cachep->node[node];
2399 if (n && n->alien)
2400 drain_alien_cache(cachep, n->alien);
a4523a8b
RD
2401 }
2402
2403 for_each_online_node(node) {
ce8eb6c4
CL
2404 n = cachep->node[node];
2405 if (n)
2406 drain_array(cachep, n, n->shared, 1, node);
e498be7d 2407 }
1da177e4
LT
2408}
2409
ed11d9eb
CL
2410/*
2411 * Remove slabs from the list of free slabs.
2412 * Specify the number of slabs to drain in tofree.
2413 *
2414 * Returns the actual number of slabs released.
2415 */
2416static int drain_freelist(struct kmem_cache *cache,
ce8eb6c4 2417 struct kmem_cache_node *n, int tofree)
1da177e4 2418{
ed11d9eb
CL
2419 struct list_head *p;
2420 int nr_freed;
8456a648 2421 struct page *page;
1da177e4 2422
ed11d9eb 2423 nr_freed = 0;
ce8eb6c4 2424 while (nr_freed < tofree && !list_empty(&n->slabs_free)) {
1da177e4 2425
ce8eb6c4
CL
2426 spin_lock_irq(&n->list_lock);
2427 p = n->slabs_free.prev;
2428 if (p == &n->slabs_free) {
2429 spin_unlock_irq(&n->list_lock);
ed11d9eb
CL
2430 goto out;
2431 }
1da177e4 2432
8456a648 2433 page = list_entry(p, struct page, lru);
1da177e4 2434#if DEBUG
8456a648 2435 BUG_ON(page->active);
1da177e4 2436#endif
8456a648 2437 list_del(&page->lru);
ed11d9eb
CL
2438 /*
2439 * Safe to drop the lock. The slab is no longer linked
2440 * to the cache.
2441 */
ce8eb6c4
CL
2442 n->free_objects -= cache->num;
2443 spin_unlock_irq(&n->list_lock);
8456a648 2444 slab_destroy(cache, page);
ed11d9eb 2445 nr_freed++;
1da177e4 2446 }
ed11d9eb
CL
2447out:
2448 return nr_freed;
1da177e4
LT
2449}
2450
18004c5d 2451/* Called with slab_mutex held to protect against cpu hotplug */
343e0d7a 2452static int __cache_shrink(struct kmem_cache *cachep)
e498be7d
CL
2453{
2454 int ret = 0, i = 0;
ce8eb6c4 2455 struct kmem_cache_node *n;
e498be7d
CL
2456
2457 drain_cpu_caches(cachep);
2458
2459 check_irq_on();
2460 for_each_online_node(i) {
ce8eb6c4
CL
2461 n = cachep->node[i];
2462 if (!n)
ed11d9eb
CL
2463 continue;
2464
0fa8103b 2465 drain_freelist(cachep, n, slabs_tofree(cachep, n));
ed11d9eb 2466
ce8eb6c4
CL
2467 ret += !list_empty(&n->slabs_full) ||
2468 !list_empty(&n->slabs_partial);
e498be7d
CL
2469 }
2470 return (ret ? 1 : 0);
2471}
2472
1da177e4
LT
2473/**
2474 * kmem_cache_shrink - Shrink a cache.
2475 * @cachep: The cache to shrink.
2476 *
2477 * Releases as many slabs as possible for a cache.
2478 * To help debugging, a zero exit status indicates all slabs were released.
2479 */
343e0d7a 2480int kmem_cache_shrink(struct kmem_cache *cachep)
1da177e4 2481{
8f5be20b 2482 int ret;
40094fa6 2483 BUG_ON(!cachep || in_interrupt());
1da177e4 2484
95402b38 2485 get_online_cpus();
18004c5d 2486 mutex_lock(&slab_mutex);
8f5be20b 2487 ret = __cache_shrink(cachep);
18004c5d 2488 mutex_unlock(&slab_mutex);
95402b38 2489 put_online_cpus();
8f5be20b 2490 return ret;
1da177e4
LT
2491}
2492EXPORT_SYMBOL(kmem_cache_shrink);
2493
945cf2b6 2494int __kmem_cache_shutdown(struct kmem_cache *cachep)
1da177e4 2495{
12c3667f 2496 int i;
ce8eb6c4 2497 struct kmem_cache_node *n;
12c3667f 2498 int rc = __cache_shrink(cachep);
1da177e4 2499
12c3667f
CL
2500 if (rc)
2501 return rc;
1da177e4 2502
12c3667f
CL
2503 for_each_online_cpu(i)
2504 kfree(cachep->array[i]);
1da177e4 2505
ce8eb6c4 2506 /* NUMA: free the node structures */
12c3667f 2507 for_each_online_node(i) {
ce8eb6c4
CL
2508 n = cachep->node[i];
2509 if (n) {
2510 kfree(n->shared);
2511 free_alien_cache(n->alien);
2512 kfree(n);
12c3667f
CL
2513 }
2514 }
2515 return 0;
1da177e4 2516}
1da177e4 2517
e5ac9c5a
RT
2518/*
2519 * Get the memory for a slab management obj.
2520 * For a slab cache when the slab descriptor is off-slab, slab descriptors
2521 * always come from malloc_sizes caches. The slab descriptor cannot
2522 * come from the same cache which is getting created because,
2523 * when we are searching for an appropriate cache for these
2524 * descriptors in kmem_cache_create, we search through the malloc_sizes array.
2525 * If we are creating a malloc_sizes cache here it would not be visible to
2526 * kmem_find_general_cachep till the initialization is complete.
8456a648 2527 * Hence we cannot have freelist_cache same as the original cache.
e5ac9c5a 2528 */
7e007355 2529static void *alloc_slabmgmt(struct kmem_cache *cachep,
0c3aa83e
JK
2530 struct page *page, int colour_off,
2531 gfp_t local_flags, int nodeid)
1da177e4 2532{
7e007355 2533 void *freelist;
0c3aa83e 2534 void *addr = page_address(page);
b28a02de 2535
1da177e4
LT
2536 if (OFF_SLAB(cachep)) {
2537 /* Slab management obj is off-slab. */
8456a648 2538 freelist = kmem_cache_alloc_node(cachep->freelist_cache,
8759ec50 2539 local_flags, nodeid);
8456a648 2540 if (!freelist)
1da177e4
LT
2541 return NULL;
2542 } else {
8456a648
JK
2543 freelist = addr + colour_off;
2544 colour_off += cachep->freelist_size;
1da177e4 2545 }
8456a648
JK
2546 page->active = 0;
2547 page->s_mem = addr + colour_off;
2548 return freelist;
1da177e4
LT
2549}
2550
e7444d9b 2551static inline unsigned int *slab_freelist(struct page *page)
1da177e4 2552{
8456a648 2553 return (unsigned int *)(page->freelist);
1da177e4
LT
2554}
2555
343e0d7a 2556static void cache_init_objs(struct kmem_cache *cachep,
8456a648 2557 struct page *page)
1da177e4
LT
2558{
2559 int i;
2560
2561 for (i = 0; i < cachep->num; i++) {
8456a648 2562 void *objp = index_to_obj(cachep, page, i);
1da177e4
LT
2563#if DEBUG
2564 /* need to poison the objs? */
2565 if (cachep->flags & SLAB_POISON)
2566 poison_obj(cachep, objp, POISON_FREE);
2567 if (cachep->flags & SLAB_STORE_USER)
2568 *dbg_userword(cachep, objp) = NULL;
2569
2570 if (cachep->flags & SLAB_RED_ZONE) {
2571 *dbg_redzone1(cachep, objp) = RED_INACTIVE;
2572 *dbg_redzone2(cachep, objp) = RED_INACTIVE;
2573 }
2574 /*
a737b3e2
AM
2575 * Constructors are not allowed to allocate memory from the same
2576 * cache which they are a constructor for. Otherwise, deadlock.
2577 * They must also be threaded.
1da177e4
LT
2578 */
2579 if (cachep->ctor && !(cachep->flags & SLAB_POISON))
51cc5068 2580 cachep->ctor(objp + obj_offset(cachep));
1da177e4
LT
2581
2582 if (cachep->flags & SLAB_RED_ZONE) {
2583 if (*dbg_redzone2(cachep, objp) != RED_INACTIVE)
2584 slab_error(cachep, "constructor overwrote the"
b28a02de 2585 " end of an object");
1da177e4
LT
2586 if (*dbg_redzone1(cachep, objp) != RED_INACTIVE)
2587 slab_error(cachep, "constructor overwrote the"
b28a02de 2588 " start of an object");
1da177e4 2589 }
3b0efdfa 2590 if ((cachep->size % PAGE_SIZE) == 0 &&
a737b3e2 2591 OFF_SLAB(cachep) && cachep->flags & SLAB_POISON)
b28a02de 2592 kernel_map_pages(virt_to_page(objp),
3b0efdfa 2593 cachep->size / PAGE_SIZE, 0);
1da177e4
LT
2594#else
2595 if (cachep->ctor)
51cc5068 2596 cachep->ctor(objp);
1da177e4 2597#endif
e7444d9b 2598 slab_freelist(page)[i] = i;
1da177e4 2599 }
1da177e4
LT
2600}
2601
343e0d7a 2602static void kmem_flagcheck(struct kmem_cache *cachep, gfp_t flags)
1da177e4 2603{
4b51d669
CL
2604 if (CONFIG_ZONE_DMA_FLAG) {
2605 if (flags & GFP_DMA)
a618e89f 2606 BUG_ON(!(cachep->allocflags & GFP_DMA));
4b51d669 2607 else
a618e89f 2608 BUG_ON(cachep->allocflags & GFP_DMA);
4b51d669 2609 }
1da177e4
LT
2610}
2611
8456a648 2612static void *slab_get_obj(struct kmem_cache *cachep, struct page *page,
a737b3e2 2613 int nodeid)
78d382d7 2614{
b1cb0982 2615 void *objp;
78d382d7 2616
e7444d9b 2617 objp = index_to_obj(cachep, page, slab_freelist(page)[page->active]);
8456a648 2618 page->active++;
78d382d7 2619#if DEBUG
1ea991b0 2620 WARN_ON(page_to_nid(virt_to_page(objp)) != nodeid);
78d382d7 2621#endif
78d382d7
MD
2622
2623 return objp;
2624}
2625
8456a648 2626static void slab_put_obj(struct kmem_cache *cachep, struct page *page,
a737b3e2 2627 void *objp, int nodeid)
78d382d7 2628{
8456a648 2629 unsigned int objnr = obj_to_index(cachep, page, objp);
78d382d7 2630#if DEBUG
16025177 2631 unsigned int i;
b1cb0982 2632
78d382d7 2633 /* Verify that the slab belongs to the intended node */
1ea991b0 2634 WARN_ON(page_to_nid(virt_to_page(objp)) != nodeid);
78d382d7 2635
b1cb0982 2636 /* Verify double free bug */
8456a648 2637 for (i = page->active; i < cachep->num; i++) {
e7444d9b 2638 if (slab_freelist(page)[i] == objnr) {
b1cb0982
JK
2639 printk(KERN_ERR "slab: double free detected in cache "
2640 "'%s', objp %p\n", cachep->name, objp);
2641 BUG();
2642 }
78d382d7
MD
2643 }
2644#endif
8456a648 2645 page->active--;
e7444d9b 2646 slab_freelist(page)[page->active] = objnr;
78d382d7
MD
2647}
2648
4776874f
PE
2649/*
2650 * Map pages beginning at addr to the given cache and slab. This is required
2651 * for the slab allocator to be able to lookup the cache and slab of a
ccd35fb9 2652 * virtual address for kfree, ksize, and slab debugging.
4776874f 2653 */
8456a648 2654static void slab_map_pages(struct kmem_cache *cache, struct page *page,
7e007355 2655 void *freelist)
1da177e4 2656{
a57a4988 2657 page->slab_cache = cache;
8456a648 2658 page->freelist = freelist;
1da177e4
LT
2659}
2660
2661/*
2662 * Grow (by 1) the number of slabs within a cache. This is called by
2663 * kmem_cache_alloc() when there are no active objs left in a cache.
2664 */
3c517a61 2665static int cache_grow(struct kmem_cache *cachep,
0c3aa83e 2666 gfp_t flags, int nodeid, struct page *page)
1da177e4 2667{
7e007355 2668 void *freelist;
b28a02de
PE
2669 size_t offset;
2670 gfp_t local_flags;
ce8eb6c4 2671 struct kmem_cache_node *n;
1da177e4 2672
a737b3e2
AM
2673 /*
2674 * Be lazy and only check for valid flags here, keeping it out of the
2675 * critical path in kmem_cache_alloc().
1da177e4 2676 */
6cb06229
CL
2677 BUG_ON(flags & GFP_SLAB_BUG_MASK);
2678 local_flags = flags & (GFP_CONSTRAINT_MASK|GFP_RECLAIM_MASK);
1da177e4 2679
ce8eb6c4 2680 /* Take the node list lock to change the colour_next on this node */
1da177e4 2681 check_irq_off();
ce8eb6c4
CL
2682 n = cachep->node[nodeid];
2683 spin_lock(&n->list_lock);
1da177e4
LT
2684
2685 /* Get colour for the slab, and cal the next value. */
ce8eb6c4
CL
2686 offset = n->colour_next;
2687 n->colour_next++;
2688 if (n->colour_next >= cachep->colour)
2689 n->colour_next = 0;
2690 spin_unlock(&n->list_lock);
1da177e4 2691
2e1217cf 2692 offset *= cachep->colour_off;
1da177e4
LT
2693
2694 if (local_flags & __GFP_WAIT)
2695 local_irq_enable();
2696
2697 /*
2698 * The test for missing atomic flag is performed here, rather than
2699 * the more obvious place, simply to reduce the critical path length
2700 * in kmem_cache_alloc(). If a caller is seriously mis-behaving they
2701 * will eventually be caught here (where it matters).
2702 */
2703 kmem_flagcheck(cachep, flags);
2704
a737b3e2
AM
2705 /*
2706 * Get mem for the objs. Attempt to allocate a physical page from
2707 * 'nodeid'.
e498be7d 2708 */
0c3aa83e
JK
2709 if (!page)
2710 page = kmem_getpages(cachep, local_flags, nodeid);
2711 if (!page)
1da177e4
LT
2712 goto failed;
2713
2714 /* Get slab management. */
8456a648 2715 freelist = alloc_slabmgmt(cachep, page, offset,
6cb06229 2716 local_flags & ~GFP_CONSTRAINT_MASK, nodeid);
8456a648 2717 if (!freelist)
1da177e4
LT
2718 goto opps1;
2719
8456a648 2720 slab_map_pages(cachep, page, freelist);
1da177e4 2721
8456a648 2722 cache_init_objs(cachep, page);
1da177e4
LT
2723
2724 if (local_flags & __GFP_WAIT)
2725 local_irq_disable();
2726 check_irq_off();
ce8eb6c4 2727 spin_lock(&n->list_lock);
1da177e4
LT
2728
2729 /* Make slab active. */
8456a648 2730 list_add_tail(&page->lru, &(n->slabs_free));
1da177e4 2731 STATS_INC_GROWN(cachep);
ce8eb6c4
CL
2732 n->free_objects += cachep->num;
2733 spin_unlock(&n->list_lock);
1da177e4 2734 return 1;
a737b3e2 2735opps1:
0c3aa83e 2736 kmem_freepages(cachep, page);
a737b3e2 2737failed:
1da177e4
LT
2738 if (local_flags & __GFP_WAIT)
2739 local_irq_disable();
2740 return 0;
2741}
2742
2743#if DEBUG
2744
2745/*
2746 * Perform extra freeing checks:
2747 * - detect bad pointers.
2748 * - POISON/RED_ZONE checking
1da177e4
LT
2749 */
2750static void kfree_debugcheck(const void *objp)
2751{
1da177e4
LT
2752 if (!virt_addr_valid(objp)) {
2753 printk(KERN_ERR "kfree_debugcheck: out of range ptr %lxh.\n",
b28a02de
PE
2754 (unsigned long)objp);
2755 BUG();
1da177e4 2756 }
1da177e4
LT
2757}
2758
58ce1fd5
PE
2759static inline void verify_redzone_free(struct kmem_cache *cache, void *obj)
2760{
b46b8f19 2761 unsigned long long redzone1, redzone2;
58ce1fd5
PE
2762
2763 redzone1 = *dbg_redzone1(cache, obj);
2764 redzone2 = *dbg_redzone2(cache, obj);
2765
2766 /*
2767 * Redzone is ok.
2768 */
2769 if (redzone1 == RED_ACTIVE && redzone2 == RED_ACTIVE)
2770 return;
2771
2772 if (redzone1 == RED_INACTIVE && redzone2 == RED_INACTIVE)
2773 slab_error(cache, "double free detected");
2774 else
2775 slab_error(cache, "memory outside object was overwritten");
2776
b46b8f19 2777 printk(KERN_ERR "%p: redzone 1:0x%llx, redzone 2:0x%llx.\n",
58ce1fd5
PE
2778 obj, redzone1, redzone2);
2779}
2780
343e0d7a 2781static void *cache_free_debugcheck(struct kmem_cache *cachep, void *objp,
7c0cb9c6 2782 unsigned long caller)
1da177e4 2783{
1da177e4 2784 unsigned int objnr;
8456a648 2785 struct page *page;
1da177e4 2786
80cbd911
MW
2787 BUG_ON(virt_to_cache(objp) != cachep);
2788
3dafccf2 2789 objp -= obj_offset(cachep);
1da177e4 2790 kfree_debugcheck(objp);
b49af68f 2791 page = virt_to_head_page(objp);
1da177e4 2792
1da177e4 2793 if (cachep->flags & SLAB_RED_ZONE) {
58ce1fd5 2794 verify_redzone_free(cachep, objp);
1da177e4
LT
2795 *dbg_redzone1(cachep, objp) = RED_INACTIVE;
2796 *dbg_redzone2(cachep, objp) = RED_INACTIVE;
2797 }
2798 if (cachep->flags & SLAB_STORE_USER)
7c0cb9c6 2799 *dbg_userword(cachep, objp) = (void *)caller;
1da177e4 2800
8456a648 2801 objnr = obj_to_index(cachep, page, objp);
1da177e4
LT
2802
2803 BUG_ON(objnr >= cachep->num);
8456a648 2804 BUG_ON(objp != index_to_obj(cachep, page, objnr));
1da177e4 2805
1da177e4
LT
2806 if (cachep->flags & SLAB_POISON) {
2807#ifdef CONFIG_DEBUG_PAGEALLOC
3b0efdfa 2808 if ((cachep->size % PAGE_SIZE)==0 && OFF_SLAB(cachep)) {
7c0cb9c6 2809 store_stackinfo(cachep, objp, caller);
b28a02de 2810 kernel_map_pages(virt_to_page(objp),
3b0efdfa 2811 cachep->size / PAGE_SIZE, 0);
1da177e4
LT
2812 } else {
2813 poison_obj(cachep, objp, POISON_FREE);
2814 }
2815#else
2816 poison_obj(cachep, objp, POISON_FREE);
2817#endif
2818 }
2819 return objp;
2820}
2821
1da177e4
LT
2822#else
2823#define kfree_debugcheck(x) do { } while(0)
2824#define cache_free_debugcheck(x,objp,z) (objp)
1da177e4
LT
2825#endif
2826
072bb0aa
MG
2827static void *cache_alloc_refill(struct kmem_cache *cachep, gfp_t flags,
2828 bool force_refill)
1da177e4
LT
2829{
2830 int batchcount;
ce8eb6c4 2831 struct kmem_cache_node *n;
1da177e4 2832 struct array_cache *ac;
1ca4cb24
PE
2833 int node;
2834
1da177e4 2835 check_irq_off();
7d6e6d09 2836 node = numa_mem_id();
072bb0aa
MG
2837 if (unlikely(force_refill))
2838 goto force_grow;
2839retry:
9a2dba4b 2840 ac = cpu_cache_get(cachep);
1da177e4
LT
2841 batchcount = ac->batchcount;
2842 if (!ac->touched && batchcount > BATCHREFILL_LIMIT) {
a737b3e2
AM
2843 /*
2844 * If there was little recent activity on this cache, then
2845 * perform only a partial refill. Otherwise we could generate
2846 * refill bouncing.
1da177e4
LT
2847 */
2848 batchcount = BATCHREFILL_LIMIT;
2849 }
ce8eb6c4 2850 n = cachep->node[node];
e498be7d 2851
ce8eb6c4
CL
2852 BUG_ON(ac->avail > 0 || !n);
2853 spin_lock(&n->list_lock);
1da177e4 2854
3ded175a 2855 /* See if we can refill from the shared array */
ce8eb6c4
CL
2856 if (n->shared && transfer_objects(ac, n->shared, batchcount)) {
2857 n->shared->touched = 1;
3ded175a 2858 goto alloc_done;
44b57f1c 2859 }
3ded175a 2860
1da177e4
LT
2861 while (batchcount > 0) {
2862 struct list_head *entry;
8456a648 2863 struct page *page;
1da177e4 2864 /* Get slab alloc is to come from. */
ce8eb6c4
CL
2865 entry = n->slabs_partial.next;
2866 if (entry == &n->slabs_partial) {
2867 n->free_touched = 1;
2868 entry = n->slabs_free.next;
2869 if (entry == &n->slabs_free)
1da177e4
LT
2870 goto must_grow;
2871 }
2872
8456a648 2873 page = list_entry(entry, struct page, lru);
1da177e4 2874 check_spinlock_acquired(cachep);
714b8171
PE
2875
2876 /*
2877 * The slab was either on partial or free list so
2878 * there must be at least one object available for
2879 * allocation.
2880 */
8456a648 2881 BUG_ON(page->active >= cachep->num);
714b8171 2882
8456a648 2883 while (page->active < cachep->num && batchcount--) {
1da177e4
LT
2884 STATS_INC_ALLOCED(cachep);
2885 STATS_INC_ACTIVE(cachep);
2886 STATS_SET_HIGH(cachep);
2887
8456a648 2888 ac_put_obj(cachep, ac, slab_get_obj(cachep, page,
072bb0aa 2889 node));
1da177e4 2890 }
1da177e4
LT
2891
2892 /* move slabp to correct slabp list: */
8456a648
JK
2893 list_del(&page->lru);
2894 if (page->active == cachep->num)
2895 list_add(&page->list, &n->slabs_full);
1da177e4 2896 else
8456a648 2897 list_add(&page->list, &n->slabs_partial);
1da177e4
LT
2898 }
2899
a737b3e2 2900must_grow:
ce8eb6c4 2901 n->free_objects -= ac->avail;
a737b3e2 2902alloc_done:
ce8eb6c4 2903 spin_unlock(&n->list_lock);
1da177e4
LT
2904
2905 if (unlikely(!ac->avail)) {
2906 int x;
072bb0aa 2907force_grow:
3c517a61 2908 x = cache_grow(cachep, flags | GFP_THISNODE, node, NULL);
e498be7d 2909
a737b3e2 2910 /* cache_grow can reenable interrupts, then ac could change. */
9a2dba4b 2911 ac = cpu_cache_get(cachep);
51cd8e6f 2912 node = numa_mem_id();
072bb0aa
MG
2913
2914 /* no objects in sight? abort */
2915 if (!x && (ac->avail == 0 || force_refill))
1da177e4
LT
2916 return NULL;
2917
a737b3e2 2918 if (!ac->avail) /* objects refilled by interrupt? */
1da177e4
LT
2919 goto retry;
2920 }
2921 ac->touched = 1;
072bb0aa
MG
2922
2923 return ac_get_obj(cachep, ac, flags, force_refill);
1da177e4
LT
2924}
2925
a737b3e2
AM
2926static inline void cache_alloc_debugcheck_before(struct kmem_cache *cachep,
2927 gfp_t flags)
1da177e4
LT
2928{
2929 might_sleep_if(flags & __GFP_WAIT);
2930#if DEBUG
2931 kmem_flagcheck(cachep, flags);
2932#endif
2933}
2934
2935#if DEBUG
a737b3e2 2936static void *cache_alloc_debugcheck_after(struct kmem_cache *cachep,
7c0cb9c6 2937 gfp_t flags, void *objp, unsigned long caller)
1da177e4 2938{
b28a02de 2939 if (!objp)
1da177e4 2940 return objp;
b28a02de 2941 if (cachep->flags & SLAB_POISON) {
1da177e4 2942#ifdef CONFIG_DEBUG_PAGEALLOC
3b0efdfa 2943 if ((cachep->size % PAGE_SIZE) == 0 && OFF_SLAB(cachep))
b28a02de 2944 kernel_map_pages(virt_to_page(objp),
3b0efdfa 2945 cachep->size / PAGE_SIZE, 1);
1da177e4
LT
2946 else
2947 check_poison_obj(cachep, objp);
2948#else
2949 check_poison_obj(cachep, objp);
2950#endif
2951 poison_obj(cachep, objp, POISON_INUSE);
2952 }
2953 if (cachep->flags & SLAB_STORE_USER)
7c0cb9c6 2954 *dbg_userword(cachep, objp) = (void *)caller;
1da177e4
LT
2955
2956 if (cachep->flags & SLAB_RED_ZONE) {
a737b3e2
AM
2957 if (*dbg_redzone1(cachep, objp) != RED_INACTIVE ||
2958 *dbg_redzone2(cachep, objp) != RED_INACTIVE) {
2959 slab_error(cachep, "double free, or memory outside"
2960 " object was overwritten");
b28a02de 2961 printk(KERN_ERR
b46b8f19 2962 "%p: redzone 1:0x%llx, redzone 2:0x%llx\n",
a737b3e2
AM
2963 objp, *dbg_redzone1(cachep, objp),
2964 *dbg_redzone2(cachep, objp));
1da177e4
LT
2965 }
2966 *dbg_redzone1(cachep, objp) = RED_ACTIVE;
2967 *dbg_redzone2(cachep, objp) = RED_ACTIVE;
2968 }
3dafccf2 2969 objp += obj_offset(cachep);
4f104934 2970 if (cachep->ctor && cachep->flags & SLAB_POISON)
51cc5068 2971 cachep->ctor(objp);
7ea466f2
TH
2972 if (ARCH_SLAB_MINALIGN &&
2973 ((unsigned long)objp & (ARCH_SLAB_MINALIGN-1))) {
a44b56d3 2974 printk(KERN_ERR "0x%p: not aligned to ARCH_SLAB_MINALIGN=%d\n",
c225150b 2975 objp, (int)ARCH_SLAB_MINALIGN);
a44b56d3 2976 }
1da177e4
LT
2977 return objp;
2978}
2979#else
2980#define cache_alloc_debugcheck_after(a,b,objp,d) (objp)
2981#endif
2982
773ff60e 2983static bool slab_should_failslab(struct kmem_cache *cachep, gfp_t flags)
8a8b6502 2984{
9b030cb8 2985 if (cachep == kmem_cache)
773ff60e 2986 return false;
8a8b6502 2987
8c138bc0 2988 return should_failslab(cachep->object_size, flags, cachep->flags);
8a8b6502
AM
2989}
2990
343e0d7a 2991static inline void *____cache_alloc(struct kmem_cache *cachep, gfp_t flags)
1da177e4 2992{
b28a02de 2993 void *objp;
1da177e4 2994 struct array_cache *ac;
072bb0aa 2995 bool force_refill = false;
1da177e4 2996
5c382300 2997 check_irq_off();
8a8b6502 2998
9a2dba4b 2999 ac = cpu_cache_get(cachep);
1da177e4 3000 if (likely(ac->avail)) {
1da177e4 3001 ac->touched = 1;
072bb0aa
MG
3002 objp = ac_get_obj(cachep, ac, flags, false);
3003
ddbf2e83 3004 /*
072bb0aa
MG
3005 * Allow for the possibility all avail objects are not allowed
3006 * by the current flags
ddbf2e83 3007 */
072bb0aa
MG
3008 if (objp) {
3009 STATS_INC_ALLOCHIT(cachep);
3010 goto out;
3011 }
3012 force_refill = true;
1da177e4 3013 }
072bb0aa
MG
3014
3015 STATS_INC_ALLOCMISS(cachep);
3016 objp = cache_alloc_refill(cachep, flags, force_refill);
3017 /*
3018 * the 'ac' may be updated by cache_alloc_refill(),
3019 * and kmemleak_erase() requires its correct value.
3020 */
3021 ac = cpu_cache_get(cachep);
3022
3023out:
d5cff635
CM
3024 /*
3025 * To avoid a false negative, if an object that is in one of the
3026 * per-CPU caches is leaked, we need to make sure kmemleak doesn't
3027 * treat the array pointers as a reference to the object.
3028 */
f3d8b53a
O
3029 if (objp)
3030 kmemleak_erase(&ac->entry[ac->avail]);
5c382300
AK
3031 return objp;
3032}
3033
e498be7d 3034#ifdef CONFIG_NUMA
c61afb18 3035/*
b2455396 3036 * Try allocating on another node if PF_SPREAD_SLAB|PF_MEMPOLICY.
c61afb18
PJ
3037 *
3038 * If we are in_interrupt, then process context, including cpusets and
3039 * mempolicy, may not apply and should not be used for allocation policy.
3040 */
3041static void *alternate_node_alloc(struct kmem_cache *cachep, gfp_t flags)
3042{
3043 int nid_alloc, nid_here;
3044
765c4507 3045 if (in_interrupt() || (flags & __GFP_THISNODE))
c61afb18 3046 return NULL;
7d6e6d09 3047 nid_alloc = nid_here = numa_mem_id();
c61afb18 3048 if (cpuset_do_slab_mem_spread() && (cachep->flags & SLAB_MEM_SPREAD))
6adef3eb 3049 nid_alloc = cpuset_slab_spread_node();
c61afb18 3050 else if (current->mempolicy)
e7b691b0 3051 nid_alloc = slab_node();
c61afb18 3052 if (nid_alloc != nid_here)
8b98c169 3053 return ____cache_alloc_node(cachep, flags, nid_alloc);
c61afb18
PJ
3054 return NULL;
3055}
3056
765c4507
CL
3057/*
3058 * Fallback function if there was no memory available and no objects on a
3c517a61 3059 * certain node and fall back is permitted. First we scan all the
6a67368c 3060 * available node for available objects. If that fails then we
3c517a61
CL
3061 * perform an allocation without specifying a node. This allows the page
3062 * allocator to do its reclaim / fallback magic. We then insert the
3063 * slab into the proper nodelist and then allocate from it.
765c4507 3064 */
8c8cc2c1 3065static void *fallback_alloc(struct kmem_cache *cache, gfp_t flags)
765c4507 3066{
8c8cc2c1
PE
3067 struct zonelist *zonelist;
3068 gfp_t local_flags;
dd1a239f 3069 struct zoneref *z;
54a6eb5c
MG
3070 struct zone *zone;
3071 enum zone_type high_zoneidx = gfp_zone(flags);
765c4507 3072 void *obj = NULL;
3c517a61 3073 int nid;
cc9a6c87 3074 unsigned int cpuset_mems_cookie;
8c8cc2c1
PE
3075
3076 if (flags & __GFP_THISNODE)
3077 return NULL;
3078
6cb06229 3079 local_flags = flags & (GFP_CONSTRAINT_MASK|GFP_RECLAIM_MASK);
765c4507 3080
cc9a6c87
MG
3081retry_cpuset:
3082 cpuset_mems_cookie = get_mems_allowed();
e7b691b0 3083 zonelist = node_zonelist(slab_node(), flags);
cc9a6c87 3084
3c517a61
CL
3085retry:
3086 /*
3087 * Look through allowed nodes for objects available
3088 * from existing per node queues.
3089 */
54a6eb5c
MG
3090 for_each_zone_zonelist(zone, z, zonelist, high_zoneidx) {
3091 nid = zone_to_nid(zone);
aedb0eb1 3092
54a6eb5c 3093 if (cpuset_zone_allowed_hardwall(zone, flags) &&
6a67368c
CL
3094 cache->node[nid] &&
3095 cache->node[nid]->free_objects) {
3c517a61
CL
3096 obj = ____cache_alloc_node(cache,
3097 flags | GFP_THISNODE, nid);
481c5346
CL
3098 if (obj)
3099 break;
3100 }
3c517a61
CL
3101 }
3102
cfce6604 3103 if (!obj) {
3c517a61
CL
3104 /*
3105 * This allocation will be performed within the constraints
3106 * of the current cpuset / memory policy requirements.
3107 * We may trigger various forms of reclaim on the allowed
3108 * set and go into memory reserves if necessary.
3109 */
0c3aa83e
JK
3110 struct page *page;
3111
dd47ea75
CL
3112 if (local_flags & __GFP_WAIT)
3113 local_irq_enable();
3114 kmem_flagcheck(cache, flags);
0c3aa83e 3115 page = kmem_getpages(cache, local_flags, numa_mem_id());
dd47ea75
CL
3116 if (local_flags & __GFP_WAIT)
3117 local_irq_disable();
0c3aa83e 3118 if (page) {
3c517a61
CL
3119 /*
3120 * Insert into the appropriate per node queues
3121 */
0c3aa83e
JK
3122 nid = page_to_nid(page);
3123 if (cache_grow(cache, flags, nid, page)) {
3c517a61
CL
3124 obj = ____cache_alloc_node(cache,
3125 flags | GFP_THISNODE, nid);
3126 if (!obj)
3127 /*
3128 * Another processor may allocate the
3129 * objects in the slab since we are
3130 * not holding any locks.
3131 */
3132 goto retry;
3133 } else {
b6a60451 3134 /* cache_grow already freed obj */
3c517a61
CL
3135 obj = NULL;
3136 }
3137 }
aedb0eb1 3138 }
cc9a6c87
MG
3139
3140 if (unlikely(!put_mems_allowed(cpuset_mems_cookie) && !obj))
3141 goto retry_cpuset;
765c4507
CL
3142 return obj;
3143}
3144
e498be7d
CL
3145/*
3146 * A interface to enable slab creation on nodeid
1da177e4 3147 */
8b98c169 3148static void *____cache_alloc_node(struct kmem_cache *cachep, gfp_t flags,
a737b3e2 3149 int nodeid)
e498be7d
CL
3150{
3151 struct list_head *entry;
8456a648 3152 struct page *page;
ce8eb6c4 3153 struct kmem_cache_node *n;
b28a02de 3154 void *obj;
b28a02de
PE
3155 int x;
3156
14e50c6a 3157 VM_BUG_ON(nodeid > num_online_nodes());
ce8eb6c4
CL
3158 n = cachep->node[nodeid];
3159 BUG_ON(!n);
b28a02de 3160
a737b3e2 3161retry:
ca3b9b91 3162 check_irq_off();
ce8eb6c4
CL
3163 spin_lock(&n->list_lock);
3164 entry = n->slabs_partial.next;
3165 if (entry == &n->slabs_partial) {
3166 n->free_touched = 1;
3167 entry = n->slabs_free.next;
3168 if (entry == &n->slabs_free)
b28a02de
PE
3169 goto must_grow;
3170 }
3171
8456a648 3172 page = list_entry(entry, struct page, lru);
b28a02de 3173 check_spinlock_acquired_node(cachep, nodeid);
b28a02de
PE
3174
3175 STATS_INC_NODEALLOCS(cachep);
3176 STATS_INC_ACTIVE(cachep);
3177 STATS_SET_HIGH(cachep);
3178
8456a648 3179 BUG_ON(page->active == cachep->num);
b28a02de 3180
8456a648 3181 obj = slab_get_obj(cachep, page, nodeid);
ce8eb6c4 3182 n->free_objects--;
b28a02de 3183 /* move slabp to correct slabp list: */
8456a648 3184 list_del(&page->lru);
b28a02de 3185
8456a648
JK
3186 if (page->active == cachep->num)
3187 list_add(&page->lru, &n->slabs_full);
a737b3e2 3188 else
8456a648 3189 list_add(&page->lru, &n->slabs_partial);
e498be7d 3190
ce8eb6c4 3191 spin_unlock(&n->list_lock);
b28a02de 3192 goto done;
e498be7d 3193
a737b3e2 3194must_grow:
ce8eb6c4 3195 spin_unlock(&n->list_lock);
3c517a61 3196 x = cache_grow(cachep, flags | GFP_THISNODE, nodeid, NULL);
765c4507
CL
3197 if (x)
3198 goto retry;
1da177e4 3199
8c8cc2c1 3200 return fallback_alloc(cachep, flags);
e498be7d 3201
a737b3e2 3202done:
b28a02de 3203 return obj;
e498be7d 3204}
8c8cc2c1 3205
8c8cc2c1 3206static __always_inline void *
48356303 3207slab_alloc_node(struct kmem_cache *cachep, gfp_t flags, int nodeid,
7c0cb9c6 3208 unsigned long caller)
8c8cc2c1
PE
3209{
3210 unsigned long save_flags;
3211 void *ptr;
7d6e6d09 3212 int slab_node = numa_mem_id();
8c8cc2c1 3213
dcce284a 3214 flags &= gfp_allowed_mask;
7e85ee0c 3215
cf40bd16
NP
3216 lockdep_trace_alloc(flags);
3217
773ff60e 3218 if (slab_should_failslab(cachep, flags))
824ebef1
AM
3219 return NULL;
3220
d79923fa
GC
3221 cachep = memcg_kmem_get_cache(cachep, flags);
3222
8c8cc2c1
PE
3223 cache_alloc_debugcheck_before(cachep, flags);
3224 local_irq_save(save_flags);
3225
eacbbae3 3226 if (nodeid == NUMA_NO_NODE)
7d6e6d09 3227 nodeid = slab_node;
8c8cc2c1 3228
6a67368c 3229 if (unlikely(!cachep->node[nodeid])) {
8c8cc2c1
PE
3230 /* Node not bootstrapped yet */
3231 ptr = fallback_alloc(cachep, flags);
3232 goto out;
3233 }
3234
7d6e6d09 3235 if (nodeid == slab_node) {
8c8cc2c1
PE
3236 /*
3237 * Use the locally cached objects if possible.
3238 * However ____cache_alloc does not allow fallback
3239 * to other nodes. It may fail while we still have
3240 * objects on other nodes available.
3241 */
3242 ptr = ____cache_alloc(cachep, flags);
3243 if (ptr)
3244 goto out;
3245 }
3246 /* ___cache_alloc_node can fall back to other nodes */
3247 ptr = ____cache_alloc_node(cachep, flags, nodeid);
3248 out:
3249 local_irq_restore(save_flags);
3250 ptr = cache_alloc_debugcheck_after(cachep, flags, ptr, caller);
8c138bc0 3251 kmemleak_alloc_recursive(ptr, cachep->object_size, 1, cachep->flags,
d5cff635 3252 flags);
8c8cc2c1 3253
c175eea4 3254 if (likely(ptr))
8c138bc0 3255 kmemcheck_slab_alloc(cachep, flags, ptr, cachep->object_size);
c175eea4 3256
d07dbea4 3257 if (unlikely((flags & __GFP_ZERO) && ptr))
8c138bc0 3258 memset(ptr, 0, cachep->object_size);
d07dbea4 3259
8c8cc2c1
PE
3260 return ptr;
3261}
3262
3263static __always_inline void *
3264__do_cache_alloc(struct kmem_cache *cache, gfp_t flags)
3265{
3266 void *objp;
3267
3268 if (unlikely(current->flags & (PF_SPREAD_SLAB | PF_MEMPOLICY))) {
3269 objp = alternate_node_alloc(cache, flags);
3270 if (objp)
3271 goto out;
3272 }
3273 objp = ____cache_alloc(cache, flags);
3274
3275 /*
3276 * We may just have run out of memory on the local node.
3277 * ____cache_alloc_node() knows how to locate memory on other nodes
3278 */
7d6e6d09
LS
3279 if (!objp)
3280 objp = ____cache_alloc_node(cache, flags, numa_mem_id());
8c8cc2c1
PE
3281
3282 out:
3283 return objp;
3284}
3285#else
3286
3287static __always_inline void *
3288__do_cache_alloc(struct kmem_cache *cachep, gfp_t flags)
3289{
3290 return ____cache_alloc(cachep, flags);
3291}
3292
3293#endif /* CONFIG_NUMA */
3294
3295static __always_inline void *
48356303 3296slab_alloc(struct kmem_cache *cachep, gfp_t flags, unsigned long caller)
8c8cc2c1
PE
3297{
3298 unsigned long save_flags;
3299 void *objp;
3300
dcce284a 3301 flags &= gfp_allowed_mask;
7e85ee0c 3302
cf40bd16
NP
3303 lockdep_trace_alloc(flags);
3304
773ff60e 3305 if (slab_should_failslab(cachep, flags))
824ebef1
AM
3306 return NULL;
3307
d79923fa
GC
3308 cachep = memcg_kmem_get_cache(cachep, flags);
3309
8c8cc2c1
PE
3310 cache_alloc_debugcheck_before(cachep, flags);
3311 local_irq_save(save_flags);
3312 objp = __do_cache_alloc(cachep, flags);
3313 local_irq_restore(save_flags);
3314 objp = cache_alloc_debugcheck_after(cachep, flags, objp, caller);
8c138bc0 3315 kmemleak_alloc_recursive(objp, cachep->object_size, 1, cachep->flags,
d5cff635 3316 flags);
8c8cc2c1
PE
3317 prefetchw(objp);
3318
c175eea4 3319 if (likely(objp))
8c138bc0 3320 kmemcheck_slab_alloc(cachep, flags, objp, cachep->object_size);
c175eea4 3321
d07dbea4 3322 if (unlikely((flags & __GFP_ZERO) && objp))
8c138bc0 3323 memset(objp, 0, cachep->object_size);
d07dbea4 3324
8c8cc2c1
PE
3325 return objp;
3326}
e498be7d
CL
3327
3328/*
3329 * Caller needs to acquire correct kmem_list's list_lock
3330 */
343e0d7a 3331static void free_block(struct kmem_cache *cachep, void **objpp, int nr_objects,
b28a02de 3332 int node)
1da177e4
LT
3333{
3334 int i;
ce8eb6c4 3335 struct kmem_cache_node *n;
1da177e4
LT
3336
3337 for (i = 0; i < nr_objects; i++) {
072bb0aa 3338 void *objp;
8456a648 3339 struct page *page;
1da177e4 3340
072bb0aa
MG
3341 clear_obj_pfmemalloc(&objpp[i]);
3342 objp = objpp[i];
3343
8456a648 3344 page = virt_to_head_page(objp);
ce8eb6c4 3345 n = cachep->node[node];
8456a648 3346 list_del(&page->lru);
ff69416e 3347 check_spinlock_acquired_node(cachep, node);
8456a648 3348 slab_put_obj(cachep, page, objp, node);
1da177e4 3349 STATS_DEC_ACTIVE(cachep);
ce8eb6c4 3350 n->free_objects++;
1da177e4
LT
3351
3352 /* fixup slab chains */
8456a648 3353 if (page->active == 0) {
ce8eb6c4
CL
3354 if (n->free_objects > n->free_limit) {
3355 n->free_objects -= cachep->num;
e5ac9c5a
RT
3356 /* No need to drop any previously held
3357 * lock here, even if we have a off-slab slab
3358 * descriptor it is guaranteed to come from
3359 * a different cache, refer to comments before
3360 * alloc_slabmgmt.
3361 */
8456a648 3362 slab_destroy(cachep, page);
1da177e4 3363 } else {
8456a648 3364 list_add(&page->lru, &n->slabs_free);
1da177e4
LT
3365 }
3366 } else {
3367 /* Unconditionally move a slab to the end of the
3368 * partial list on free - maximum time for the
3369 * other objects to be freed, too.
3370 */
8456a648 3371 list_add_tail(&page->lru, &n->slabs_partial);
1da177e4
LT
3372 }
3373 }
3374}
3375
343e0d7a 3376static void cache_flusharray(struct kmem_cache *cachep, struct array_cache *ac)
1da177e4
LT
3377{
3378 int batchcount;
ce8eb6c4 3379 struct kmem_cache_node *n;
7d6e6d09 3380 int node = numa_mem_id();
1da177e4
LT
3381
3382 batchcount = ac->batchcount;
3383#if DEBUG
3384 BUG_ON(!batchcount || batchcount > ac->avail);
3385#endif
3386 check_irq_off();
ce8eb6c4
CL
3387 n = cachep->node[node];
3388 spin_lock(&n->list_lock);
3389 if (n->shared) {
3390 struct array_cache *shared_array = n->shared;
b28a02de 3391 int max = shared_array->limit - shared_array->avail;
1da177e4
LT
3392 if (max) {
3393 if (batchcount > max)
3394 batchcount = max;
e498be7d 3395 memcpy(&(shared_array->entry[shared_array->avail]),
b28a02de 3396 ac->entry, sizeof(void *) * batchcount);
1da177e4
LT
3397 shared_array->avail += batchcount;
3398 goto free_done;
3399 }
3400 }
3401
ff69416e 3402 free_block(cachep, ac->entry, batchcount, node);
a737b3e2 3403free_done:
1da177e4
LT
3404#if STATS
3405 {
3406 int i = 0;
3407 struct list_head *p;
3408
ce8eb6c4
CL
3409 p = n->slabs_free.next;
3410 while (p != &(n->slabs_free)) {
8456a648 3411 struct page *page;
1da177e4 3412
8456a648
JK
3413 page = list_entry(p, struct page, lru);
3414 BUG_ON(page->active);
1da177e4
LT
3415
3416 i++;
3417 p = p->next;
3418 }
3419 STATS_SET_FREEABLE(cachep, i);
3420 }
3421#endif
ce8eb6c4 3422 spin_unlock(&n->list_lock);
1da177e4 3423 ac->avail -= batchcount;
a737b3e2 3424 memmove(ac->entry, &(ac->entry[batchcount]), sizeof(void *)*ac->avail);
1da177e4
LT
3425}
3426
3427/*
a737b3e2
AM
3428 * Release an obj back to its cache. If the obj has a constructed state, it must
3429 * be in this state _before_ it is released. Called with disabled ints.
1da177e4 3430 */
a947eb95 3431static inline void __cache_free(struct kmem_cache *cachep, void *objp,
7c0cb9c6 3432 unsigned long caller)
1da177e4 3433{
9a2dba4b 3434 struct array_cache *ac = cpu_cache_get(cachep);
1da177e4
LT
3435
3436 check_irq_off();
d5cff635 3437 kmemleak_free_recursive(objp, cachep->flags);
a947eb95 3438 objp = cache_free_debugcheck(cachep, objp, caller);
1da177e4 3439
8c138bc0 3440 kmemcheck_slab_free(cachep, objp, cachep->object_size);
c175eea4 3441
1807a1aa
SS
3442 /*
3443 * Skip calling cache_free_alien() when the platform is not numa.
3444 * This will avoid cache misses that happen while accessing slabp (which
3445 * is per page memory reference) to get nodeid. Instead use a global
3446 * variable to skip the call, which is mostly likely to be present in
3447 * the cache.
3448 */
b6e68bc1 3449 if (nr_online_nodes > 1 && cache_free_alien(cachep, objp))
729bd0b7
PE
3450 return;
3451
1da177e4
LT
3452 if (likely(ac->avail < ac->limit)) {
3453 STATS_INC_FREEHIT(cachep);
1da177e4
LT
3454 } else {
3455 STATS_INC_FREEMISS(cachep);
3456 cache_flusharray(cachep, ac);
1da177e4 3457 }
42c8c99c 3458
072bb0aa 3459 ac_put_obj(cachep, ac, objp);
1da177e4
LT
3460}
3461
3462/**
3463 * kmem_cache_alloc - Allocate an object
3464 * @cachep: The cache to allocate from.
3465 * @flags: See kmalloc().
3466 *
3467 * Allocate an object from this cache. The flags are only relevant
3468 * if the cache has no available objects.
3469 */
343e0d7a 3470void *kmem_cache_alloc(struct kmem_cache *cachep, gfp_t flags)
1da177e4 3471{
48356303 3472 void *ret = slab_alloc(cachep, flags, _RET_IP_);
36555751 3473
ca2b84cb 3474 trace_kmem_cache_alloc(_RET_IP_, ret,
8c138bc0 3475 cachep->object_size, cachep->size, flags);
36555751
EGM
3476
3477 return ret;
1da177e4
LT
3478}
3479EXPORT_SYMBOL(kmem_cache_alloc);
3480
0f24f128 3481#ifdef CONFIG_TRACING
85beb586 3482void *
4052147c 3483kmem_cache_alloc_trace(struct kmem_cache *cachep, gfp_t flags, size_t size)
36555751 3484{
85beb586
SR
3485 void *ret;
3486
48356303 3487 ret = slab_alloc(cachep, flags, _RET_IP_);
85beb586
SR
3488
3489 trace_kmalloc(_RET_IP_, ret,
ff4fcd01 3490 size, cachep->size, flags);
85beb586 3491 return ret;
36555751 3492}
85beb586 3493EXPORT_SYMBOL(kmem_cache_alloc_trace);
36555751
EGM
3494#endif
3495
1da177e4 3496#ifdef CONFIG_NUMA
d0d04b78
ZL
3497/**
3498 * kmem_cache_alloc_node - Allocate an object on the specified node
3499 * @cachep: The cache to allocate from.
3500 * @flags: See kmalloc().
3501 * @nodeid: node number of the target node.
3502 *
3503 * Identical to kmem_cache_alloc but it will allocate memory on the given
3504 * node, which can improve the performance for cpu bound structures.
3505 *
3506 * Fallback to other node is possible if __GFP_THISNODE is not set.
3507 */
8b98c169
CH
3508void *kmem_cache_alloc_node(struct kmem_cache *cachep, gfp_t flags, int nodeid)
3509{
48356303 3510 void *ret = slab_alloc_node(cachep, flags, nodeid, _RET_IP_);
36555751 3511
ca2b84cb 3512 trace_kmem_cache_alloc_node(_RET_IP_, ret,
8c138bc0 3513 cachep->object_size, cachep->size,
ca2b84cb 3514 flags, nodeid);
36555751
EGM
3515
3516 return ret;
8b98c169 3517}
1da177e4
LT
3518EXPORT_SYMBOL(kmem_cache_alloc_node);
3519
0f24f128 3520#ifdef CONFIG_TRACING
4052147c 3521void *kmem_cache_alloc_node_trace(struct kmem_cache *cachep,
85beb586 3522 gfp_t flags,
4052147c
EG
3523 int nodeid,
3524 size_t size)
36555751 3525{
85beb586
SR
3526 void *ret;
3527
592f4145 3528 ret = slab_alloc_node(cachep, flags, nodeid, _RET_IP_);
7c0cb9c6 3529
85beb586 3530 trace_kmalloc_node(_RET_IP_, ret,
ff4fcd01 3531 size, cachep->size,
85beb586
SR
3532 flags, nodeid);
3533 return ret;
36555751 3534}
85beb586 3535EXPORT_SYMBOL(kmem_cache_alloc_node_trace);
36555751
EGM
3536#endif
3537
8b98c169 3538static __always_inline void *
7c0cb9c6 3539__do_kmalloc_node(size_t size, gfp_t flags, int node, unsigned long caller)
97e2bde4 3540{
343e0d7a 3541 struct kmem_cache *cachep;
97e2bde4 3542
2c59dd65 3543 cachep = kmalloc_slab(size, flags);
6cb8f913
CL
3544 if (unlikely(ZERO_OR_NULL_PTR(cachep)))
3545 return cachep;
4052147c 3546 return kmem_cache_alloc_node_trace(cachep, flags, node, size);
97e2bde4 3547}
8b98c169 3548
0bb38a5c 3549#if defined(CONFIG_DEBUG_SLAB) || defined(CONFIG_TRACING)
8b98c169
CH
3550void *__kmalloc_node(size_t size, gfp_t flags, int node)
3551{
7c0cb9c6 3552 return __do_kmalloc_node(size, flags, node, _RET_IP_);
8b98c169 3553}
dbe5e69d 3554EXPORT_SYMBOL(__kmalloc_node);
8b98c169
CH
3555
3556void *__kmalloc_node_track_caller(size_t size, gfp_t flags,
ce71e27c 3557 int node, unsigned long caller)
8b98c169 3558{
7c0cb9c6 3559 return __do_kmalloc_node(size, flags, node, caller);
8b98c169
CH
3560}
3561EXPORT_SYMBOL(__kmalloc_node_track_caller);
3562#else
3563void *__kmalloc_node(size_t size, gfp_t flags, int node)
3564{
7c0cb9c6 3565 return __do_kmalloc_node(size, flags, node, 0);
8b98c169
CH
3566}
3567EXPORT_SYMBOL(__kmalloc_node);
0bb38a5c 3568#endif /* CONFIG_DEBUG_SLAB || CONFIG_TRACING */
8b98c169 3569#endif /* CONFIG_NUMA */
1da177e4
LT
3570
3571/**
800590f5 3572 * __do_kmalloc - allocate memory
1da177e4 3573 * @size: how many bytes of memory are required.
800590f5 3574 * @flags: the type of memory to allocate (see kmalloc).
911851e6 3575 * @caller: function caller for debug tracking of the caller
1da177e4 3576 */
7fd6b141 3577static __always_inline void *__do_kmalloc(size_t size, gfp_t flags,
7c0cb9c6 3578 unsigned long caller)
1da177e4 3579{
343e0d7a 3580 struct kmem_cache *cachep;
36555751 3581 void *ret;
1da177e4 3582
97e2bde4
MS
3583 /* If you want to save a few bytes .text space: replace
3584 * __ with kmem_.
3585 * Then kmalloc uses the uninlined functions instead of the inline
3586 * functions.
3587 */
2c59dd65 3588 cachep = kmalloc_slab(size, flags);
a5c96d8a
LT
3589 if (unlikely(ZERO_OR_NULL_PTR(cachep)))
3590 return cachep;
48356303 3591 ret = slab_alloc(cachep, flags, caller);
36555751 3592
7c0cb9c6 3593 trace_kmalloc(caller, ret,
3b0efdfa 3594 size, cachep->size, flags);
36555751
EGM
3595
3596 return ret;
7fd6b141
PE
3597}
3598
7fd6b141 3599
0bb38a5c 3600#if defined(CONFIG_DEBUG_SLAB) || defined(CONFIG_TRACING)
7fd6b141
PE
3601void *__kmalloc(size_t size, gfp_t flags)
3602{
7c0cb9c6 3603 return __do_kmalloc(size, flags, _RET_IP_);
1da177e4
LT
3604}
3605EXPORT_SYMBOL(__kmalloc);
3606
ce71e27c 3607void *__kmalloc_track_caller(size_t size, gfp_t flags, unsigned long caller)
7fd6b141 3608{
7c0cb9c6 3609 return __do_kmalloc(size, flags, caller);
7fd6b141
PE
3610}
3611EXPORT_SYMBOL(__kmalloc_track_caller);
1d2c8eea
CH
3612
3613#else
3614void *__kmalloc(size_t size, gfp_t flags)
3615{
7c0cb9c6 3616 return __do_kmalloc(size, flags, 0);
1d2c8eea
CH
3617}
3618EXPORT_SYMBOL(__kmalloc);
7fd6b141
PE
3619#endif
3620
1da177e4
LT
3621/**
3622 * kmem_cache_free - Deallocate an object
3623 * @cachep: The cache the allocation was from.
3624 * @objp: The previously allocated object.
3625 *
3626 * Free an object which was previously allocated from this
3627 * cache.
3628 */
343e0d7a 3629void kmem_cache_free(struct kmem_cache *cachep, void *objp)
1da177e4
LT
3630{
3631 unsigned long flags;
b9ce5ef4
GC
3632 cachep = cache_from_obj(cachep, objp);
3633 if (!cachep)
3634 return;
1da177e4
LT
3635
3636 local_irq_save(flags);
d97d476b 3637 debug_check_no_locks_freed(objp, cachep->object_size);
3ac7fe5a 3638 if (!(cachep->flags & SLAB_DEBUG_OBJECTS))
8c138bc0 3639 debug_check_no_obj_freed(objp, cachep->object_size);
7c0cb9c6 3640 __cache_free(cachep, objp, _RET_IP_);
1da177e4 3641 local_irq_restore(flags);
36555751 3642
ca2b84cb 3643 trace_kmem_cache_free(_RET_IP_, objp);
1da177e4
LT
3644}
3645EXPORT_SYMBOL(kmem_cache_free);
3646
1da177e4
LT
3647/**
3648 * kfree - free previously allocated memory
3649 * @objp: pointer returned by kmalloc.
3650 *
80e93eff
PE
3651 * If @objp is NULL, no operation is performed.
3652 *
1da177e4
LT
3653 * Don't free memory not originally allocated by kmalloc()
3654 * or you will run into trouble.
3655 */
3656void kfree(const void *objp)
3657{
343e0d7a 3658 struct kmem_cache *c;
1da177e4
LT
3659 unsigned long flags;
3660
2121db74
PE
3661 trace_kfree(_RET_IP_, objp);
3662
6cb8f913 3663 if (unlikely(ZERO_OR_NULL_PTR(objp)))
1da177e4
LT
3664 return;
3665 local_irq_save(flags);
3666 kfree_debugcheck(objp);
6ed5eb22 3667 c = virt_to_cache(objp);
8c138bc0
CL
3668 debug_check_no_locks_freed(objp, c->object_size);
3669
3670 debug_check_no_obj_freed(objp, c->object_size);
7c0cb9c6 3671 __cache_free(c, (void *)objp, _RET_IP_);
1da177e4
LT
3672 local_irq_restore(flags);
3673}
3674EXPORT_SYMBOL(kfree);
3675
e498be7d 3676/*
ce8eb6c4 3677 * This initializes kmem_cache_node or resizes various caches for all nodes.
e498be7d 3678 */
83b519e8 3679static int alloc_kmemlist(struct kmem_cache *cachep, gfp_t gfp)
e498be7d
CL
3680{
3681 int node;
ce8eb6c4 3682 struct kmem_cache_node *n;
cafeb02e 3683 struct array_cache *new_shared;
3395ee05 3684 struct array_cache **new_alien = NULL;
e498be7d 3685
9c09a95c 3686 for_each_online_node(node) {
cafeb02e 3687
3395ee05 3688 if (use_alien_caches) {
83b519e8 3689 new_alien = alloc_alien_cache(node, cachep->limit, gfp);
3395ee05
PM
3690 if (!new_alien)
3691 goto fail;
3692 }
cafeb02e 3693
63109846
ED
3694 new_shared = NULL;
3695 if (cachep->shared) {
3696 new_shared = alloc_arraycache(node,
0718dc2a 3697 cachep->shared*cachep->batchcount,
83b519e8 3698 0xbaadf00d, gfp);
63109846
ED
3699 if (!new_shared) {
3700 free_alien_cache(new_alien);
3701 goto fail;
3702 }
0718dc2a 3703 }
cafeb02e 3704
ce8eb6c4
CL
3705 n = cachep->node[node];
3706 if (n) {
3707 struct array_cache *shared = n->shared;
cafeb02e 3708
ce8eb6c4 3709 spin_lock_irq(&n->list_lock);
e498be7d 3710
cafeb02e 3711 if (shared)
0718dc2a
CL
3712 free_block(cachep, shared->entry,
3713 shared->avail, node);
e498be7d 3714
ce8eb6c4
CL
3715 n->shared = new_shared;
3716 if (!n->alien) {
3717 n->alien = new_alien;
e498be7d
CL
3718 new_alien = NULL;
3719 }
ce8eb6c4 3720 n->free_limit = (1 + nr_cpus_node(node)) *
a737b3e2 3721 cachep->batchcount + cachep->num;
ce8eb6c4 3722 spin_unlock_irq(&n->list_lock);
cafeb02e 3723 kfree(shared);
e498be7d
CL
3724 free_alien_cache(new_alien);
3725 continue;
3726 }
ce8eb6c4
CL
3727 n = kmalloc_node(sizeof(struct kmem_cache_node), gfp, node);
3728 if (!n) {
0718dc2a
CL
3729 free_alien_cache(new_alien);
3730 kfree(new_shared);
e498be7d 3731 goto fail;
0718dc2a 3732 }
e498be7d 3733
ce8eb6c4
CL
3734 kmem_cache_node_init(n);
3735 n->next_reap = jiffies + REAPTIMEOUT_LIST3 +
a737b3e2 3736 ((unsigned long)cachep) % REAPTIMEOUT_LIST3;
ce8eb6c4
CL
3737 n->shared = new_shared;
3738 n->alien = new_alien;
3739 n->free_limit = (1 + nr_cpus_node(node)) *
a737b3e2 3740 cachep->batchcount + cachep->num;
ce8eb6c4 3741 cachep->node[node] = n;
e498be7d 3742 }
cafeb02e 3743 return 0;
0718dc2a 3744
a737b3e2 3745fail:
3b0efdfa 3746 if (!cachep->list.next) {
0718dc2a
CL
3747 /* Cache is not active yet. Roll back what we did */
3748 node--;
3749 while (node >= 0) {
6a67368c 3750 if (cachep->node[node]) {
ce8eb6c4 3751 n = cachep->node[node];
0718dc2a 3752
ce8eb6c4
CL
3753 kfree(n->shared);
3754 free_alien_cache(n->alien);
3755 kfree(n);
6a67368c 3756 cachep->node[node] = NULL;
0718dc2a
CL
3757 }
3758 node--;
3759 }
3760 }
cafeb02e 3761 return -ENOMEM;
e498be7d
CL
3762}
3763
1da177e4 3764struct ccupdate_struct {
343e0d7a 3765 struct kmem_cache *cachep;
acfe7d74 3766 struct array_cache *new[0];
1da177e4
LT
3767};
3768
3769static void do_ccupdate_local(void *info)
3770{
a737b3e2 3771 struct ccupdate_struct *new = info;
1da177e4
LT
3772 struct array_cache *old;
3773
3774 check_irq_off();
9a2dba4b 3775 old = cpu_cache_get(new->cachep);
e498be7d 3776
1da177e4
LT
3777 new->cachep->array[smp_processor_id()] = new->new[smp_processor_id()];
3778 new->new[smp_processor_id()] = old;
3779}
3780
18004c5d 3781/* Always called with the slab_mutex held */
943a451a 3782static int __do_tune_cpucache(struct kmem_cache *cachep, int limit,
83b519e8 3783 int batchcount, int shared, gfp_t gfp)
1da177e4 3784{
d2e7b7d0 3785 struct ccupdate_struct *new;
2ed3a4ef 3786 int i;
1da177e4 3787
acfe7d74
ED
3788 new = kzalloc(sizeof(*new) + nr_cpu_ids * sizeof(struct array_cache *),
3789 gfp);
d2e7b7d0
SS
3790 if (!new)
3791 return -ENOMEM;
3792
e498be7d 3793 for_each_online_cpu(i) {
7d6e6d09 3794 new->new[i] = alloc_arraycache(cpu_to_mem(i), limit,
83b519e8 3795 batchcount, gfp);
d2e7b7d0 3796 if (!new->new[i]) {
b28a02de 3797 for (i--; i >= 0; i--)
d2e7b7d0
SS
3798 kfree(new->new[i]);
3799 kfree(new);
e498be7d 3800 return -ENOMEM;
1da177e4
LT
3801 }
3802 }
d2e7b7d0 3803 new->cachep = cachep;
1da177e4 3804
15c8b6c1 3805 on_each_cpu(do_ccupdate_local, (void *)new, 1);
e498be7d 3806
1da177e4 3807 check_irq_on();
1da177e4
LT
3808 cachep->batchcount = batchcount;
3809 cachep->limit = limit;
e498be7d 3810 cachep->shared = shared;
1da177e4 3811
e498be7d 3812 for_each_online_cpu(i) {
d2e7b7d0 3813 struct array_cache *ccold = new->new[i];
1da177e4
LT
3814 if (!ccold)
3815 continue;
6a67368c 3816 spin_lock_irq(&cachep->node[cpu_to_mem(i)]->list_lock);
7d6e6d09 3817 free_block(cachep, ccold->entry, ccold->avail, cpu_to_mem(i));
6a67368c 3818 spin_unlock_irq(&cachep->node[cpu_to_mem(i)]->list_lock);
1da177e4
LT
3819 kfree(ccold);
3820 }
d2e7b7d0 3821 kfree(new);
83b519e8 3822 return alloc_kmemlist(cachep, gfp);
1da177e4
LT
3823}
3824
943a451a
GC
3825static int do_tune_cpucache(struct kmem_cache *cachep, int limit,
3826 int batchcount, int shared, gfp_t gfp)
3827{
3828 int ret;
3829 struct kmem_cache *c = NULL;
3830 int i = 0;
3831
3832 ret = __do_tune_cpucache(cachep, limit, batchcount, shared, gfp);
3833
3834 if (slab_state < FULL)
3835 return ret;
3836
3837 if ((ret < 0) || !is_root_cache(cachep))
3838 return ret;
3839
ebe945c2 3840 VM_BUG_ON(!mutex_is_locked(&slab_mutex));
943a451a 3841 for_each_memcg_cache_index(i) {
2ade4de8 3842 c = cache_from_memcg_idx(cachep, i);
943a451a
GC
3843 if (c)
3844 /* return value determined by the parent cache only */
3845 __do_tune_cpucache(c, limit, batchcount, shared, gfp);
3846 }
3847
3848 return ret;
3849}
3850
18004c5d 3851/* Called with slab_mutex held always */
83b519e8 3852static int enable_cpucache(struct kmem_cache *cachep, gfp_t gfp)
1da177e4
LT
3853{
3854 int err;
943a451a
GC
3855 int limit = 0;
3856 int shared = 0;
3857 int batchcount = 0;
3858
3859 if (!is_root_cache(cachep)) {
3860 struct kmem_cache *root = memcg_root_cache(cachep);
3861 limit = root->limit;
3862 shared = root->shared;
3863 batchcount = root->batchcount;
3864 }
1da177e4 3865
943a451a
GC
3866 if (limit && shared && batchcount)
3867 goto skip_setup;
a737b3e2
AM
3868 /*
3869 * The head array serves three purposes:
1da177e4
LT
3870 * - create a LIFO ordering, i.e. return objects that are cache-warm
3871 * - reduce the number of spinlock operations.
a737b3e2 3872 * - reduce the number of linked list operations on the slab and
1da177e4
LT
3873 * bufctl chains: array operations are cheaper.
3874 * The numbers are guessed, we should auto-tune as described by
3875 * Bonwick.
3876 */
3b0efdfa 3877 if (cachep->size > 131072)
1da177e4 3878 limit = 1;
3b0efdfa 3879 else if (cachep->size > PAGE_SIZE)
1da177e4 3880 limit = 8;
3b0efdfa 3881 else if (cachep->size > 1024)
1da177e4 3882 limit = 24;
3b0efdfa 3883 else if (cachep->size > 256)
1da177e4
LT
3884 limit = 54;
3885 else
3886 limit = 120;
3887
a737b3e2
AM
3888 /*
3889 * CPU bound tasks (e.g. network routing) can exhibit cpu bound
1da177e4
LT
3890 * allocation behaviour: Most allocs on one cpu, most free operations
3891 * on another cpu. For these cases, an efficient object passing between
3892 * cpus is necessary. This is provided by a shared array. The array
3893 * replaces Bonwick's magazine layer.
3894 * On uniprocessor, it's functionally equivalent (but less efficient)
3895 * to a larger limit. Thus disabled by default.
3896 */
3897 shared = 0;
3b0efdfa 3898 if (cachep->size <= PAGE_SIZE && num_possible_cpus() > 1)
1da177e4 3899 shared = 8;
1da177e4
LT
3900
3901#if DEBUG
a737b3e2
AM
3902 /*
3903 * With debugging enabled, large batchcount lead to excessively long
3904 * periods with disabled local interrupts. Limit the batchcount
1da177e4
LT
3905 */
3906 if (limit > 32)
3907 limit = 32;
3908#endif
943a451a
GC
3909 batchcount = (limit + 1) / 2;
3910skip_setup:
3911 err = do_tune_cpucache(cachep, limit, batchcount, shared, gfp);
1da177e4
LT
3912 if (err)
3913 printk(KERN_ERR "enable_cpucache failed for %s, error %d.\n",
b28a02de 3914 cachep->name, -err);
2ed3a4ef 3915 return err;
1da177e4
LT
3916}
3917
1b55253a 3918/*
ce8eb6c4
CL
3919 * Drain an array if it contains any elements taking the node lock only if
3920 * necessary. Note that the node listlock also protects the array_cache
b18e7e65 3921 * if drain_array() is used on the shared array.
1b55253a 3922 */
ce8eb6c4 3923static void drain_array(struct kmem_cache *cachep, struct kmem_cache_node *n,
1b55253a 3924 struct array_cache *ac, int force, int node)
1da177e4
LT
3925{
3926 int tofree;
3927
1b55253a
CL
3928 if (!ac || !ac->avail)
3929 return;
1da177e4
LT
3930 if (ac->touched && !force) {
3931 ac->touched = 0;
b18e7e65 3932 } else {
ce8eb6c4 3933 spin_lock_irq(&n->list_lock);
b18e7e65
CL
3934 if (ac->avail) {
3935 tofree = force ? ac->avail : (ac->limit + 4) / 5;
3936 if (tofree > ac->avail)
3937 tofree = (ac->avail + 1) / 2;
3938 free_block(cachep, ac->entry, tofree, node);
3939 ac->avail -= tofree;
3940 memmove(ac->entry, &(ac->entry[tofree]),
3941 sizeof(void *) * ac->avail);
3942 }
ce8eb6c4 3943 spin_unlock_irq(&n->list_lock);
1da177e4
LT
3944 }
3945}
3946
3947/**
3948 * cache_reap - Reclaim memory from caches.
05fb6bf0 3949 * @w: work descriptor
1da177e4
LT
3950 *
3951 * Called from workqueue/eventd every few seconds.
3952 * Purpose:
3953 * - clear the per-cpu caches for this CPU.
3954 * - return freeable pages to the main free memory pool.
3955 *
a737b3e2
AM
3956 * If we cannot acquire the cache chain mutex then just give up - we'll try
3957 * again on the next iteration.
1da177e4 3958 */
7c5cae36 3959static void cache_reap(struct work_struct *w)
1da177e4 3960{
7a7c381d 3961 struct kmem_cache *searchp;
ce8eb6c4 3962 struct kmem_cache_node *n;
7d6e6d09 3963 int node = numa_mem_id();
bf6aede7 3964 struct delayed_work *work = to_delayed_work(w);
1da177e4 3965
18004c5d 3966 if (!mutex_trylock(&slab_mutex))
1da177e4 3967 /* Give up. Setup the next iteration. */
7c5cae36 3968 goto out;
1da177e4 3969
18004c5d 3970 list_for_each_entry(searchp, &slab_caches, list) {
1da177e4
LT
3971 check_irq_on();
3972
35386e3b 3973 /*
ce8eb6c4 3974 * We only take the node lock if absolutely necessary and we
35386e3b
CL
3975 * have established with reasonable certainty that
3976 * we can do some work if the lock was obtained.
3977 */
ce8eb6c4 3978 n = searchp->node[node];
35386e3b 3979
ce8eb6c4 3980 reap_alien(searchp, n);
1da177e4 3981
ce8eb6c4 3982 drain_array(searchp, n, cpu_cache_get(searchp), 0, node);
1da177e4 3983
35386e3b
CL
3984 /*
3985 * These are racy checks but it does not matter
3986 * if we skip one check or scan twice.
3987 */
ce8eb6c4 3988 if (time_after(n->next_reap, jiffies))
35386e3b 3989 goto next;
1da177e4 3990
ce8eb6c4 3991 n->next_reap = jiffies + REAPTIMEOUT_LIST3;
1da177e4 3992
ce8eb6c4 3993 drain_array(searchp, n, n->shared, 0, node);
1da177e4 3994
ce8eb6c4
CL
3995 if (n->free_touched)
3996 n->free_touched = 0;
ed11d9eb
CL
3997 else {
3998 int freed;
1da177e4 3999
ce8eb6c4 4000 freed = drain_freelist(searchp, n, (n->free_limit +
ed11d9eb
CL
4001 5 * searchp->num - 1) / (5 * searchp->num));
4002 STATS_ADD_REAPED(searchp, freed);
4003 }
35386e3b 4004next:
1da177e4
LT
4005 cond_resched();
4006 }
4007 check_irq_on();
18004c5d 4008 mutex_unlock(&slab_mutex);
8fce4d8e 4009 next_reap_node();
7c5cae36 4010out:
a737b3e2 4011 /* Set up the next iteration */
7c5cae36 4012 schedule_delayed_work(work, round_jiffies_relative(REAPTIMEOUT_CPUC));
1da177e4
LT
4013}
4014
158a9624 4015#ifdef CONFIG_SLABINFO
0d7561c6 4016void get_slabinfo(struct kmem_cache *cachep, struct slabinfo *sinfo)
1da177e4 4017{
8456a648 4018 struct page *page;
b28a02de
PE
4019 unsigned long active_objs;
4020 unsigned long num_objs;
4021 unsigned long active_slabs = 0;
4022 unsigned long num_slabs, free_objects = 0, shared_avail = 0;
e498be7d 4023 const char *name;
1da177e4 4024 char *error = NULL;
e498be7d 4025 int node;
ce8eb6c4 4026 struct kmem_cache_node *n;
1da177e4 4027
1da177e4
LT
4028 active_objs = 0;
4029 num_slabs = 0;
e498be7d 4030 for_each_online_node(node) {
ce8eb6c4
CL
4031 n = cachep->node[node];
4032 if (!n)
e498be7d
CL
4033 continue;
4034
ca3b9b91 4035 check_irq_on();
ce8eb6c4 4036 spin_lock_irq(&n->list_lock);
e498be7d 4037
8456a648
JK
4038 list_for_each_entry(page, &n->slabs_full, lru) {
4039 if (page->active != cachep->num && !error)
e498be7d
CL
4040 error = "slabs_full accounting error";
4041 active_objs += cachep->num;
4042 active_slabs++;
4043 }
8456a648
JK
4044 list_for_each_entry(page, &n->slabs_partial, lru) {
4045 if (page->active == cachep->num && !error)
106a74e1 4046 error = "slabs_partial accounting error";
8456a648 4047 if (!page->active && !error)
106a74e1 4048 error = "slabs_partial accounting error";
8456a648 4049 active_objs += page->active;
e498be7d
CL
4050 active_slabs++;
4051 }
8456a648
JK
4052 list_for_each_entry(page, &n->slabs_free, lru) {
4053 if (page->active && !error)
106a74e1 4054 error = "slabs_free accounting error";
e498be7d
CL
4055 num_slabs++;
4056 }
ce8eb6c4
CL
4057 free_objects += n->free_objects;
4058 if (n->shared)
4059 shared_avail += n->shared->avail;
e498be7d 4060
ce8eb6c4 4061 spin_unlock_irq(&n->list_lock);
1da177e4 4062 }
b28a02de
PE
4063 num_slabs += active_slabs;
4064 num_objs = num_slabs * cachep->num;
e498be7d 4065 if (num_objs - active_objs != free_objects && !error)
1da177e4
LT
4066 error = "free_objects accounting error";
4067
b28a02de 4068 name = cachep->name;
1da177e4
LT
4069 if (error)
4070 printk(KERN_ERR "slab: cache %s error: %s\n", name, error);
4071
0d7561c6
GC
4072 sinfo->active_objs = active_objs;
4073 sinfo->num_objs = num_objs;
4074 sinfo->active_slabs = active_slabs;
4075 sinfo->num_slabs = num_slabs;
4076 sinfo->shared_avail = shared_avail;
4077 sinfo->limit = cachep->limit;
4078 sinfo->batchcount = cachep->batchcount;
4079 sinfo->shared = cachep->shared;
4080 sinfo->objects_per_slab = cachep->num;
4081 sinfo->cache_order = cachep->gfporder;
4082}
4083
4084void slabinfo_show_stats(struct seq_file *m, struct kmem_cache *cachep)
4085{
1da177e4 4086#if STATS
ce8eb6c4 4087 { /* node stats */
1da177e4
LT
4088 unsigned long high = cachep->high_mark;
4089 unsigned long allocs = cachep->num_allocations;
4090 unsigned long grown = cachep->grown;
4091 unsigned long reaped = cachep->reaped;
4092 unsigned long errors = cachep->errors;
4093 unsigned long max_freeable = cachep->max_freeable;
1da177e4 4094 unsigned long node_allocs = cachep->node_allocs;
e498be7d 4095 unsigned long node_frees = cachep->node_frees;
fb7faf33 4096 unsigned long overflows = cachep->node_overflow;
1da177e4 4097
e92dd4fd
JP
4098 seq_printf(m, " : globalstat %7lu %6lu %5lu %4lu "
4099 "%4lu %4lu %4lu %4lu %4lu",
4100 allocs, high, grown,
4101 reaped, errors, max_freeable, node_allocs,
4102 node_frees, overflows);
1da177e4
LT
4103 }
4104 /* cpu stats */
4105 {
4106 unsigned long allochit = atomic_read(&cachep->allochit);
4107 unsigned long allocmiss = atomic_read(&cachep->allocmiss);
4108 unsigned long freehit = atomic_read(&cachep->freehit);
4109 unsigned long freemiss = atomic_read(&cachep->freemiss);
4110
4111 seq_printf(m, " : cpustat %6lu %6lu %6lu %6lu",
b28a02de 4112 allochit, allocmiss, freehit, freemiss);
1da177e4
LT
4113 }
4114#endif
1da177e4
LT
4115}
4116
1da177e4
LT
4117#define MAX_SLABINFO_WRITE 128
4118/**
4119 * slabinfo_write - Tuning for the slab allocator
4120 * @file: unused
4121 * @buffer: user buffer
4122 * @count: data length
4123 * @ppos: unused
4124 */
b7454ad3 4125ssize_t slabinfo_write(struct file *file, const char __user *buffer,
b28a02de 4126 size_t count, loff_t *ppos)
1da177e4 4127{
b28a02de 4128 char kbuf[MAX_SLABINFO_WRITE + 1], *tmp;
1da177e4 4129 int limit, batchcount, shared, res;
7a7c381d 4130 struct kmem_cache *cachep;
b28a02de 4131
1da177e4
LT
4132 if (count > MAX_SLABINFO_WRITE)
4133 return -EINVAL;
4134 if (copy_from_user(&kbuf, buffer, count))
4135 return -EFAULT;
b28a02de 4136 kbuf[MAX_SLABINFO_WRITE] = '\0';
1da177e4
LT
4137
4138 tmp = strchr(kbuf, ' ');
4139 if (!tmp)
4140 return -EINVAL;
4141 *tmp = '\0';
4142 tmp++;
4143 if (sscanf(tmp, " %d %d %d", &limit, &batchcount, &shared) != 3)
4144 return -EINVAL;
4145
4146 /* Find the cache in the chain of caches. */
18004c5d 4147 mutex_lock(&slab_mutex);
1da177e4 4148 res = -EINVAL;
18004c5d 4149 list_for_each_entry(cachep, &slab_caches, list) {
1da177e4 4150 if (!strcmp(cachep->name, kbuf)) {
a737b3e2
AM
4151 if (limit < 1 || batchcount < 1 ||
4152 batchcount > limit || shared < 0) {
e498be7d 4153 res = 0;
1da177e4 4154 } else {
e498be7d 4155 res = do_tune_cpucache(cachep, limit,
83b519e8
PE
4156 batchcount, shared,
4157 GFP_KERNEL);
1da177e4
LT
4158 }
4159 break;
4160 }
4161 }
18004c5d 4162 mutex_unlock(&slab_mutex);
1da177e4
LT
4163 if (res >= 0)
4164 res = count;
4165 return res;
4166}
871751e2
AV
4167
4168#ifdef CONFIG_DEBUG_SLAB_LEAK
4169
4170static void *leaks_start(struct seq_file *m, loff_t *pos)
4171{
18004c5d
CL
4172 mutex_lock(&slab_mutex);
4173 return seq_list_start(&slab_caches, *pos);
871751e2
AV
4174}
4175
4176static inline int add_caller(unsigned long *n, unsigned long v)
4177{
4178 unsigned long *p;
4179 int l;
4180 if (!v)
4181 return 1;
4182 l = n[1];
4183 p = n + 2;
4184 while (l) {
4185 int i = l/2;
4186 unsigned long *q = p + 2 * i;
4187 if (*q == v) {
4188 q[1]++;
4189 return 1;
4190 }
4191 if (*q > v) {
4192 l = i;
4193 } else {
4194 p = q + 2;
4195 l -= i + 1;
4196 }
4197 }
4198 if (++n[1] == n[0])
4199 return 0;
4200 memmove(p + 2, p, n[1] * 2 * sizeof(unsigned long) - ((void *)p - (void *)n));
4201 p[0] = v;
4202 p[1] = 1;
4203 return 1;
4204}
4205
8456a648
JK
4206static void handle_slab(unsigned long *n, struct kmem_cache *c,
4207 struct page *page)
871751e2
AV
4208{
4209 void *p;
b1cb0982
JK
4210 int i, j;
4211
871751e2
AV
4212 if (n[0] == n[1])
4213 return;
8456a648 4214 for (i = 0, p = page->s_mem; i < c->num; i++, p += c->size) {
b1cb0982
JK
4215 bool active = true;
4216
8456a648 4217 for (j = page->active; j < c->num; j++) {
b1cb0982 4218 /* Skip freed item */
e7444d9b 4219 if (slab_freelist(page)[j] == i) {
b1cb0982
JK
4220 active = false;
4221 break;
4222 }
4223 }
4224 if (!active)
871751e2 4225 continue;
b1cb0982 4226
871751e2
AV
4227 if (!add_caller(n, (unsigned long)*dbg_userword(c, p)))
4228 return;
4229 }
4230}
4231
4232static void show_symbol(struct seq_file *m, unsigned long address)
4233{
4234#ifdef CONFIG_KALLSYMS
871751e2 4235 unsigned long offset, size;
9281acea 4236 char modname[MODULE_NAME_LEN], name[KSYM_NAME_LEN];
871751e2 4237
a5c43dae 4238 if (lookup_symbol_attrs(address, &size, &offset, modname, name) == 0) {
871751e2 4239 seq_printf(m, "%s+%#lx/%#lx", name, offset, size);
a5c43dae 4240 if (modname[0])
871751e2
AV
4241 seq_printf(m, " [%s]", modname);
4242 return;
4243 }
4244#endif
4245 seq_printf(m, "%p", (void *)address);
4246}
4247
4248static int leaks_show(struct seq_file *m, void *p)
4249{
0672aa7c 4250 struct kmem_cache *cachep = list_entry(p, struct kmem_cache, list);
8456a648 4251 struct page *page;
ce8eb6c4 4252 struct kmem_cache_node *n;
871751e2 4253 const char *name;
db845067 4254 unsigned long *x = m->private;
871751e2
AV
4255 int node;
4256 int i;
4257
4258 if (!(cachep->flags & SLAB_STORE_USER))
4259 return 0;
4260 if (!(cachep->flags & SLAB_RED_ZONE))
4261 return 0;
4262
4263 /* OK, we can do it */
4264
db845067 4265 x[1] = 0;
871751e2
AV
4266
4267 for_each_online_node(node) {
ce8eb6c4
CL
4268 n = cachep->node[node];
4269 if (!n)
871751e2
AV
4270 continue;
4271
4272 check_irq_on();
ce8eb6c4 4273 spin_lock_irq(&n->list_lock);
871751e2 4274
8456a648
JK
4275 list_for_each_entry(page, &n->slabs_full, lru)
4276 handle_slab(x, cachep, page);
4277 list_for_each_entry(page, &n->slabs_partial, lru)
4278 handle_slab(x, cachep, page);
ce8eb6c4 4279 spin_unlock_irq(&n->list_lock);
871751e2
AV
4280 }
4281 name = cachep->name;
db845067 4282 if (x[0] == x[1]) {
871751e2 4283 /* Increase the buffer size */
18004c5d 4284 mutex_unlock(&slab_mutex);
db845067 4285 m->private = kzalloc(x[0] * 4 * sizeof(unsigned long), GFP_KERNEL);
871751e2
AV
4286 if (!m->private) {
4287 /* Too bad, we are really out */
db845067 4288 m->private = x;
18004c5d 4289 mutex_lock(&slab_mutex);
871751e2
AV
4290 return -ENOMEM;
4291 }
db845067
CL
4292 *(unsigned long *)m->private = x[0] * 2;
4293 kfree(x);
18004c5d 4294 mutex_lock(&slab_mutex);
871751e2
AV
4295 /* Now make sure this entry will be retried */
4296 m->count = m->size;
4297 return 0;
4298 }
db845067
CL
4299 for (i = 0; i < x[1]; i++) {
4300 seq_printf(m, "%s: %lu ", name, x[2*i+3]);
4301 show_symbol(m, x[2*i+2]);
871751e2
AV
4302 seq_putc(m, '\n');
4303 }
d2e7b7d0 4304
871751e2
AV
4305 return 0;
4306}
4307
a0ec95a8 4308static const struct seq_operations slabstats_op = {
871751e2 4309 .start = leaks_start,
276a2439
WL
4310 .next = slab_next,
4311 .stop = slab_stop,
871751e2
AV
4312 .show = leaks_show,
4313};
a0ec95a8
AD
4314
4315static int slabstats_open(struct inode *inode, struct file *file)
4316{
4317 unsigned long *n = kzalloc(PAGE_SIZE, GFP_KERNEL);
4318 int ret = -ENOMEM;
4319 if (n) {
4320 ret = seq_open(file, &slabstats_op);
4321 if (!ret) {
4322 struct seq_file *m = file->private_data;
4323 *n = PAGE_SIZE / (2 * sizeof(unsigned long));
4324 m->private = n;
4325 n = NULL;
4326 }
4327 kfree(n);
4328 }
4329 return ret;
4330}
4331
4332static const struct file_operations proc_slabstats_operations = {
4333 .open = slabstats_open,
4334 .read = seq_read,
4335 .llseek = seq_lseek,
4336 .release = seq_release_private,
4337};
4338#endif
4339
4340static int __init slab_proc_init(void)
4341{
4342#ifdef CONFIG_DEBUG_SLAB_LEAK
4343 proc_create("slab_allocators", 0, NULL, &proc_slabstats_operations);
871751e2 4344#endif
a0ec95a8
AD
4345 return 0;
4346}
4347module_init(slab_proc_init);
1da177e4
LT
4348#endif
4349
00e145b6
MS
4350/**
4351 * ksize - get the actual amount of memory allocated for a given object
4352 * @objp: Pointer to the object
4353 *
4354 * kmalloc may internally round up allocations and return more memory
4355 * than requested. ksize() can be used to determine the actual amount of
4356 * memory allocated. The caller may use this additional memory, even though
4357 * a smaller amount of memory was initially specified with the kmalloc call.
4358 * The caller must guarantee that objp points to a valid object previously
4359 * allocated with either kmalloc() or kmem_cache_alloc(). The object
4360 * must not be freed during the duration of the call.
4361 */
fd76bab2 4362size_t ksize(const void *objp)
1da177e4 4363{
ef8b4520
CL
4364 BUG_ON(!objp);
4365 if (unlikely(objp == ZERO_SIZE_PTR))
00e145b6 4366 return 0;
1da177e4 4367
8c138bc0 4368 return virt_to_cache(objp)->object_size;
1da177e4 4369}
b1aabecd 4370EXPORT_SYMBOL(ksize);