slab: introduce helper functions to get/set free object
[deliverable/linux.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
e5c58dfd 2551static inline unsigned int get_free_obj(struct page *page, unsigned int idx)
1da177e4 2552{
e5c58dfd
JK
2553 return ((unsigned int *)page->freelist)[idx];
2554}
2555
2556static inline void set_free_obj(struct page *page,
2557 unsigned int idx, unsigned int val)
2558{
2559 ((unsigned int *)(page->freelist))[idx] = val;
1da177e4
LT
2560}
2561
343e0d7a 2562static void cache_init_objs(struct kmem_cache *cachep,
8456a648 2563 struct page *page)
1da177e4
LT
2564{
2565 int i;
2566
2567 for (i = 0; i < cachep->num; i++) {
8456a648 2568 void *objp = index_to_obj(cachep, page, i);
1da177e4
LT
2569#if DEBUG
2570 /* need to poison the objs? */
2571 if (cachep->flags & SLAB_POISON)
2572 poison_obj(cachep, objp, POISON_FREE);
2573 if (cachep->flags & SLAB_STORE_USER)
2574 *dbg_userword(cachep, objp) = NULL;
2575
2576 if (cachep->flags & SLAB_RED_ZONE) {
2577 *dbg_redzone1(cachep, objp) = RED_INACTIVE;
2578 *dbg_redzone2(cachep, objp) = RED_INACTIVE;
2579 }
2580 /*
a737b3e2
AM
2581 * Constructors are not allowed to allocate memory from the same
2582 * cache which they are a constructor for. Otherwise, deadlock.
2583 * They must also be threaded.
1da177e4
LT
2584 */
2585 if (cachep->ctor && !(cachep->flags & SLAB_POISON))
51cc5068 2586 cachep->ctor(objp + obj_offset(cachep));
1da177e4
LT
2587
2588 if (cachep->flags & SLAB_RED_ZONE) {
2589 if (*dbg_redzone2(cachep, objp) != RED_INACTIVE)
2590 slab_error(cachep, "constructor overwrote the"
b28a02de 2591 " end of an object");
1da177e4
LT
2592 if (*dbg_redzone1(cachep, objp) != RED_INACTIVE)
2593 slab_error(cachep, "constructor overwrote the"
b28a02de 2594 " start of an object");
1da177e4 2595 }
3b0efdfa 2596 if ((cachep->size % PAGE_SIZE) == 0 &&
a737b3e2 2597 OFF_SLAB(cachep) && cachep->flags & SLAB_POISON)
b28a02de 2598 kernel_map_pages(virt_to_page(objp),
3b0efdfa 2599 cachep->size / PAGE_SIZE, 0);
1da177e4
LT
2600#else
2601 if (cachep->ctor)
51cc5068 2602 cachep->ctor(objp);
1da177e4 2603#endif
e5c58dfd 2604 set_free_obj(page, i, i);
1da177e4 2605 }
1da177e4
LT
2606}
2607
343e0d7a 2608static void kmem_flagcheck(struct kmem_cache *cachep, gfp_t flags)
1da177e4 2609{
4b51d669
CL
2610 if (CONFIG_ZONE_DMA_FLAG) {
2611 if (flags & GFP_DMA)
a618e89f 2612 BUG_ON(!(cachep->allocflags & GFP_DMA));
4b51d669 2613 else
a618e89f 2614 BUG_ON(cachep->allocflags & GFP_DMA);
4b51d669 2615 }
1da177e4
LT
2616}
2617
8456a648 2618static void *slab_get_obj(struct kmem_cache *cachep, struct page *page,
a737b3e2 2619 int nodeid)
78d382d7 2620{
b1cb0982 2621 void *objp;
78d382d7 2622
e5c58dfd 2623 objp = index_to_obj(cachep, page, get_free_obj(page, page->active));
8456a648 2624 page->active++;
78d382d7 2625#if DEBUG
1ea991b0 2626 WARN_ON(page_to_nid(virt_to_page(objp)) != nodeid);
78d382d7 2627#endif
78d382d7
MD
2628
2629 return objp;
2630}
2631
8456a648 2632static void slab_put_obj(struct kmem_cache *cachep, struct page *page,
a737b3e2 2633 void *objp, int nodeid)
78d382d7 2634{
8456a648 2635 unsigned int objnr = obj_to_index(cachep, page, objp);
78d382d7 2636#if DEBUG
16025177 2637 unsigned int i;
b1cb0982 2638
78d382d7 2639 /* Verify that the slab belongs to the intended node */
1ea991b0 2640 WARN_ON(page_to_nid(virt_to_page(objp)) != nodeid);
78d382d7 2641
b1cb0982 2642 /* Verify double free bug */
8456a648 2643 for (i = page->active; i < cachep->num; i++) {
e5c58dfd 2644 if (get_free_obj(page, i) == objnr) {
b1cb0982
JK
2645 printk(KERN_ERR "slab: double free detected in cache "
2646 "'%s', objp %p\n", cachep->name, objp);
2647 BUG();
2648 }
78d382d7
MD
2649 }
2650#endif
8456a648 2651 page->active--;
e5c58dfd 2652 set_free_obj(page, page->active, objnr);
78d382d7
MD
2653}
2654
4776874f
PE
2655/*
2656 * Map pages beginning at addr to the given cache and slab. This is required
2657 * for the slab allocator to be able to lookup the cache and slab of a
ccd35fb9 2658 * virtual address for kfree, ksize, and slab debugging.
4776874f 2659 */
8456a648 2660static void slab_map_pages(struct kmem_cache *cache, struct page *page,
7e007355 2661 void *freelist)
1da177e4 2662{
a57a4988 2663 page->slab_cache = cache;
8456a648 2664 page->freelist = freelist;
1da177e4
LT
2665}
2666
2667/*
2668 * Grow (by 1) the number of slabs within a cache. This is called by
2669 * kmem_cache_alloc() when there are no active objs left in a cache.
2670 */
3c517a61 2671static int cache_grow(struct kmem_cache *cachep,
0c3aa83e 2672 gfp_t flags, int nodeid, struct page *page)
1da177e4 2673{
7e007355 2674 void *freelist;
b28a02de
PE
2675 size_t offset;
2676 gfp_t local_flags;
ce8eb6c4 2677 struct kmem_cache_node *n;
1da177e4 2678
a737b3e2
AM
2679 /*
2680 * Be lazy and only check for valid flags here, keeping it out of the
2681 * critical path in kmem_cache_alloc().
1da177e4 2682 */
6cb06229
CL
2683 BUG_ON(flags & GFP_SLAB_BUG_MASK);
2684 local_flags = flags & (GFP_CONSTRAINT_MASK|GFP_RECLAIM_MASK);
1da177e4 2685
ce8eb6c4 2686 /* Take the node list lock to change the colour_next on this node */
1da177e4 2687 check_irq_off();
ce8eb6c4
CL
2688 n = cachep->node[nodeid];
2689 spin_lock(&n->list_lock);
1da177e4
LT
2690
2691 /* Get colour for the slab, and cal the next value. */
ce8eb6c4
CL
2692 offset = n->colour_next;
2693 n->colour_next++;
2694 if (n->colour_next >= cachep->colour)
2695 n->colour_next = 0;
2696 spin_unlock(&n->list_lock);
1da177e4 2697
2e1217cf 2698 offset *= cachep->colour_off;
1da177e4
LT
2699
2700 if (local_flags & __GFP_WAIT)
2701 local_irq_enable();
2702
2703 /*
2704 * The test for missing atomic flag is performed here, rather than
2705 * the more obvious place, simply to reduce the critical path length
2706 * in kmem_cache_alloc(). If a caller is seriously mis-behaving they
2707 * will eventually be caught here (where it matters).
2708 */
2709 kmem_flagcheck(cachep, flags);
2710
a737b3e2
AM
2711 /*
2712 * Get mem for the objs. Attempt to allocate a physical page from
2713 * 'nodeid'.
e498be7d 2714 */
0c3aa83e
JK
2715 if (!page)
2716 page = kmem_getpages(cachep, local_flags, nodeid);
2717 if (!page)
1da177e4
LT
2718 goto failed;
2719
2720 /* Get slab management. */
8456a648 2721 freelist = alloc_slabmgmt(cachep, page, offset,
6cb06229 2722 local_flags & ~GFP_CONSTRAINT_MASK, nodeid);
8456a648 2723 if (!freelist)
1da177e4
LT
2724 goto opps1;
2725
8456a648 2726 slab_map_pages(cachep, page, freelist);
1da177e4 2727
8456a648 2728 cache_init_objs(cachep, page);
1da177e4
LT
2729
2730 if (local_flags & __GFP_WAIT)
2731 local_irq_disable();
2732 check_irq_off();
ce8eb6c4 2733 spin_lock(&n->list_lock);
1da177e4
LT
2734
2735 /* Make slab active. */
8456a648 2736 list_add_tail(&page->lru, &(n->slabs_free));
1da177e4 2737 STATS_INC_GROWN(cachep);
ce8eb6c4
CL
2738 n->free_objects += cachep->num;
2739 spin_unlock(&n->list_lock);
1da177e4 2740 return 1;
a737b3e2 2741opps1:
0c3aa83e 2742 kmem_freepages(cachep, page);
a737b3e2 2743failed:
1da177e4
LT
2744 if (local_flags & __GFP_WAIT)
2745 local_irq_disable();
2746 return 0;
2747}
2748
2749#if DEBUG
2750
2751/*
2752 * Perform extra freeing checks:
2753 * - detect bad pointers.
2754 * - POISON/RED_ZONE checking
1da177e4
LT
2755 */
2756static void kfree_debugcheck(const void *objp)
2757{
1da177e4
LT
2758 if (!virt_addr_valid(objp)) {
2759 printk(KERN_ERR "kfree_debugcheck: out of range ptr %lxh.\n",
b28a02de
PE
2760 (unsigned long)objp);
2761 BUG();
1da177e4 2762 }
1da177e4
LT
2763}
2764
58ce1fd5
PE
2765static inline void verify_redzone_free(struct kmem_cache *cache, void *obj)
2766{
b46b8f19 2767 unsigned long long redzone1, redzone2;
58ce1fd5
PE
2768
2769 redzone1 = *dbg_redzone1(cache, obj);
2770 redzone2 = *dbg_redzone2(cache, obj);
2771
2772 /*
2773 * Redzone is ok.
2774 */
2775 if (redzone1 == RED_ACTIVE && redzone2 == RED_ACTIVE)
2776 return;
2777
2778 if (redzone1 == RED_INACTIVE && redzone2 == RED_INACTIVE)
2779 slab_error(cache, "double free detected");
2780 else
2781 slab_error(cache, "memory outside object was overwritten");
2782
b46b8f19 2783 printk(KERN_ERR "%p: redzone 1:0x%llx, redzone 2:0x%llx.\n",
58ce1fd5
PE
2784 obj, redzone1, redzone2);
2785}
2786
343e0d7a 2787static void *cache_free_debugcheck(struct kmem_cache *cachep, void *objp,
7c0cb9c6 2788 unsigned long caller)
1da177e4 2789{
1da177e4 2790 unsigned int objnr;
8456a648 2791 struct page *page;
1da177e4 2792
80cbd911
MW
2793 BUG_ON(virt_to_cache(objp) != cachep);
2794
3dafccf2 2795 objp -= obj_offset(cachep);
1da177e4 2796 kfree_debugcheck(objp);
b49af68f 2797 page = virt_to_head_page(objp);
1da177e4 2798
1da177e4 2799 if (cachep->flags & SLAB_RED_ZONE) {
58ce1fd5 2800 verify_redzone_free(cachep, objp);
1da177e4
LT
2801 *dbg_redzone1(cachep, objp) = RED_INACTIVE;
2802 *dbg_redzone2(cachep, objp) = RED_INACTIVE;
2803 }
2804 if (cachep->flags & SLAB_STORE_USER)
7c0cb9c6 2805 *dbg_userword(cachep, objp) = (void *)caller;
1da177e4 2806
8456a648 2807 objnr = obj_to_index(cachep, page, objp);
1da177e4
LT
2808
2809 BUG_ON(objnr >= cachep->num);
8456a648 2810 BUG_ON(objp != index_to_obj(cachep, page, objnr));
1da177e4 2811
1da177e4
LT
2812 if (cachep->flags & SLAB_POISON) {
2813#ifdef CONFIG_DEBUG_PAGEALLOC
3b0efdfa 2814 if ((cachep->size % PAGE_SIZE)==0 && OFF_SLAB(cachep)) {
7c0cb9c6 2815 store_stackinfo(cachep, objp, caller);
b28a02de 2816 kernel_map_pages(virt_to_page(objp),
3b0efdfa 2817 cachep->size / PAGE_SIZE, 0);
1da177e4
LT
2818 } else {
2819 poison_obj(cachep, objp, POISON_FREE);
2820 }
2821#else
2822 poison_obj(cachep, objp, POISON_FREE);
2823#endif
2824 }
2825 return objp;
2826}
2827
1da177e4
LT
2828#else
2829#define kfree_debugcheck(x) do { } while(0)
2830#define cache_free_debugcheck(x,objp,z) (objp)
1da177e4
LT
2831#endif
2832
072bb0aa
MG
2833static void *cache_alloc_refill(struct kmem_cache *cachep, gfp_t flags,
2834 bool force_refill)
1da177e4
LT
2835{
2836 int batchcount;
ce8eb6c4 2837 struct kmem_cache_node *n;
1da177e4 2838 struct array_cache *ac;
1ca4cb24
PE
2839 int node;
2840
1da177e4 2841 check_irq_off();
7d6e6d09 2842 node = numa_mem_id();
072bb0aa
MG
2843 if (unlikely(force_refill))
2844 goto force_grow;
2845retry:
9a2dba4b 2846 ac = cpu_cache_get(cachep);
1da177e4
LT
2847 batchcount = ac->batchcount;
2848 if (!ac->touched && batchcount > BATCHREFILL_LIMIT) {
a737b3e2
AM
2849 /*
2850 * If there was little recent activity on this cache, then
2851 * perform only a partial refill. Otherwise we could generate
2852 * refill bouncing.
1da177e4
LT
2853 */
2854 batchcount = BATCHREFILL_LIMIT;
2855 }
ce8eb6c4 2856 n = cachep->node[node];
e498be7d 2857
ce8eb6c4
CL
2858 BUG_ON(ac->avail > 0 || !n);
2859 spin_lock(&n->list_lock);
1da177e4 2860
3ded175a 2861 /* See if we can refill from the shared array */
ce8eb6c4
CL
2862 if (n->shared && transfer_objects(ac, n->shared, batchcount)) {
2863 n->shared->touched = 1;
3ded175a 2864 goto alloc_done;
44b57f1c 2865 }
3ded175a 2866
1da177e4
LT
2867 while (batchcount > 0) {
2868 struct list_head *entry;
8456a648 2869 struct page *page;
1da177e4 2870 /* Get slab alloc is to come from. */
ce8eb6c4
CL
2871 entry = n->slabs_partial.next;
2872 if (entry == &n->slabs_partial) {
2873 n->free_touched = 1;
2874 entry = n->slabs_free.next;
2875 if (entry == &n->slabs_free)
1da177e4
LT
2876 goto must_grow;
2877 }
2878
8456a648 2879 page = list_entry(entry, struct page, lru);
1da177e4 2880 check_spinlock_acquired(cachep);
714b8171
PE
2881
2882 /*
2883 * The slab was either on partial or free list so
2884 * there must be at least one object available for
2885 * allocation.
2886 */
8456a648 2887 BUG_ON(page->active >= cachep->num);
714b8171 2888
8456a648 2889 while (page->active < cachep->num && batchcount--) {
1da177e4
LT
2890 STATS_INC_ALLOCED(cachep);
2891 STATS_INC_ACTIVE(cachep);
2892 STATS_SET_HIGH(cachep);
2893
8456a648 2894 ac_put_obj(cachep, ac, slab_get_obj(cachep, page,
072bb0aa 2895 node));
1da177e4 2896 }
1da177e4
LT
2897
2898 /* move slabp to correct slabp list: */
8456a648
JK
2899 list_del(&page->lru);
2900 if (page->active == cachep->num)
2901 list_add(&page->list, &n->slabs_full);
1da177e4 2902 else
8456a648 2903 list_add(&page->list, &n->slabs_partial);
1da177e4
LT
2904 }
2905
a737b3e2 2906must_grow:
ce8eb6c4 2907 n->free_objects -= ac->avail;
a737b3e2 2908alloc_done:
ce8eb6c4 2909 spin_unlock(&n->list_lock);
1da177e4
LT
2910
2911 if (unlikely(!ac->avail)) {
2912 int x;
072bb0aa 2913force_grow:
3c517a61 2914 x = cache_grow(cachep, flags | GFP_THISNODE, node, NULL);
e498be7d 2915
a737b3e2 2916 /* cache_grow can reenable interrupts, then ac could change. */
9a2dba4b 2917 ac = cpu_cache_get(cachep);
51cd8e6f 2918 node = numa_mem_id();
072bb0aa
MG
2919
2920 /* no objects in sight? abort */
2921 if (!x && (ac->avail == 0 || force_refill))
1da177e4
LT
2922 return NULL;
2923
a737b3e2 2924 if (!ac->avail) /* objects refilled by interrupt? */
1da177e4
LT
2925 goto retry;
2926 }
2927 ac->touched = 1;
072bb0aa
MG
2928
2929 return ac_get_obj(cachep, ac, flags, force_refill);
1da177e4
LT
2930}
2931
a737b3e2
AM
2932static inline void cache_alloc_debugcheck_before(struct kmem_cache *cachep,
2933 gfp_t flags)
1da177e4
LT
2934{
2935 might_sleep_if(flags & __GFP_WAIT);
2936#if DEBUG
2937 kmem_flagcheck(cachep, flags);
2938#endif
2939}
2940
2941#if DEBUG
a737b3e2 2942static void *cache_alloc_debugcheck_after(struct kmem_cache *cachep,
7c0cb9c6 2943 gfp_t flags, void *objp, unsigned long caller)
1da177e4 2944{
b28a02de 2945 if (!objp)
1da177e4 2946 return objp;
b28a02de 2947 if (cachep->flags & SLAB_POISON) {
1da177e4 2948#ifdef CONFIG_DEBUG_PAGEALLOC
3b0efdfa 2949 if ((cachep->size % PAGE_SIZE) == 0 && OFF_SLAB(cachep))
b28a02de 2950 kernel_map_pages(virt_to_page(objp),
3b0efdfa 2951 cachep->size / PAGE_SIZE, 1);
1da177e4
LT
2952 else
2953 check_poison_obj(cachep, objp);
2954#else
2955 check_poison_obj(cachep, objp);
2956#endif
2957 poison_obj(cachep, objp, POISON_INUSE);
2958 }
2959 if (cachep->flags & SLAB_STORE_USER)
7c0cb9c6 2960 *dbg_userword(cachep, objp) = (void *)caller;
1da177e4
LT
2961
2962 if (cachep->flags & SLAB_RED_ZONE) {
a737b3e2
AM
2963 if (*dbg_redzone1(cachep, objp) != RED_INACTIVE ||
2964 *dbg_redzone2(cachep, objp) != RED_INACTIVE) {
2965 slab_error(cachep, "double free, or memory outside"
2966 " object was overwritten");
b28a02de 2967 printk(KERN_ERR
b46b8f19 2968 "%p: redzone 1:0x%llx, redzone 2:0x%llx\n",
a737b3e2
AM
2969 objp, *dbg_redzone1(cachep, objp),
2970 *dbg_redzone2(cachep, objp));
1da177e4
LT
2971 }
2972 *dbg_redzone1(cachep, objp) = RED_ACTIVE;
2973 *dbg_redzone2(cachep, objp) = RED_ACTIVE;
2974 }
3dafccf2 2975 objp += obj_offset(cachep);
4f104934 2976 if (cachep->ctor && cachep->flags & SLAB_POISON)
51cc5068 2977 cachep->ctor(objp);
7ea466f2
TH
2978 if (ARCH_SLAB_MINALIGN &&
2979 ((unsigned long)objp & (ARCH_SLAB_MINALIGN-1))) {
a44b56d3 2980 printk(KERN_ERR "0x%p: not aligned to ARCH_SLAB_MINALIGN=%d\n",
c225150b 2981 objp, (int)ARCH_SLAB_MINALIGN);
a44b56d3 2982 }
1da177e4
LT
2983 return objp;
2984}
2985#else
2986#define cache_alloc_debugcheck_after(a,b,objp,d) (objp)
2987#endif
2988
773ff60e 2989static bool slab_should_failslab(struct kmem_cache *cachep, gfp_t flags)
8a8b6502 2990{
9b030cb8 2991 if (cachep == kmem_cache)
773ff60e 2992 return false;
8a8b6502 2993
8c138bc0 2994 return should_failslab(cachep->object_size, flags, cachep->flags);
8a8b6502
AM
2995}
2996
343e0d7a 2997static inline void *____cache_alloc(struct kmem_cache *cachep, gfp_t flags)
1da177e4 2998{
b28a02de 2999 void *objp;
1da177e4 3000 struct array_cache *ac;
072bb0aa 3001 bool force_refill = false;
1da177e4 3002
5c382300 3003 check_irq_off();
8a8b6502 3004
9a2dba4b 3005 ac = cpu_cache_get(cachep);
1da177e4 3006 if (likely(ac->avail)) {
1da177e4 3007 ac->touched = 1;
072bb0aa
MG
3008 objp = ac_get_obj(cachep, ac, flags, false);
3009
ddbf2e83 3010 /*
072bb0aa
MG
3011 * Allow for the possibility all avail objects are not allowed
3012 * by the current flags
ddbf2e83 3013 */
072bb0aa
MG
3014 if (objp) {
3015 STATS_INC_ALLOCHIT(cachep);
3016 goto out;
3017 }
3018 force_refill = true;
1da177e4 3019 }
072bb0aa
MG
3020
3021 STATS_INC_ALLOCMISS(cachep);
3022 objp = cache_alloc_refill(cachep, flags, force_refill);
3023 /*
3024 * the 'ac' may be updated by cache_alloc_refill(),
3025 * and kmemleak_erase() requires its correct value.
3026 */
3027 ac = cpu_cache_get(cachep);
3028
3029out:
d5cff635
CM
3030 /*
3031 * To avoid a false negative, if an object that is in one of the
3032 * per-CPU caches is leaked, we need to make sure kmemleak doesn't
3033 * treat the array pointers as a reference to the object.
3034 */
f3d8b53a
O
3035 if (objp)
3036 kmemleak_erase(&ac->entry[ac->avail]);
5c382300
AK
3037 return objp;
3038}
3039
e498be7d 3040#ifdef CONFIG_NUMA
c61afb18 3041/*
b2455396 3042 * Try allocating on another node if PF_SPREAD_SLAB|PF_MEMPOLICY.
c61afb18
PJ
3043 *
3044 * If we are in_interrupt, then process context, including cpusets and
3045 * mempolicy, may not apply and should not be used for allocation policy.
3046 */
3047static void *alternate_node_alloc(struct kmem_cache *cachep, gfp_t flags)
3048{
3049 int nid_alloc, nid_here;
3050
765c4507 3051 if (in_interrupt() || (flags & __GFP_THISNODE))
c61afb18 3052 return NULL;
7d6e6d09 3053 nid_alloc = nid_here = numa_mem_id();
c61afb18 3054 if (cpuset_do_slab_mem_spread() && (cachep->flags & SLAB_MEM_SPREAD))
6adef3eb 3055 nid_alloc = cpuset_slab_spread_node();
c61afb18 3056 else if (current->mempolicy)
e7b691b0 3057 nid_alloc = slab_node();
c61afb18 3058 if (nid_alloc != nid_here)
8b98c169 3059 return ____cache_alloc_node(cachep, flags, nid_alloc);
c61afb18
PJ
3060 return NULL;
3061}
3062
765c4507
CL
3063/*
3064 * Fallback function if there was no memory available and no objects on a
3c517a61 3065 * certain node and fall back is permitted. First we scan all the
6a67368c 3066 * available node for available objects. If that fails then we
3c517a61
CL
3067 * perform an allocation without specifying a node. This allows the page
3068 * allocator to do its reclaim / fallback magic. We then insert the
3069 * slab into the proper nodelist and then allocate from it.
765c4507 3070 */
8c8cc2c1 3071static void *fallback_alloc(struct kmem_cache *cache, gfp_t flags)
765c4507 3072{
8c8cc2c1
PE
3073 struct zonelist *zonelist;
3074 gfp_t local_flags;
dd1a239f 3075 struct zoneref *z;
54a6eb5c
MG
3076 struct zone *zone;
3077 enum zone_type high_zoneidx = gfp_zone(flags);
765c4507 3078 void *obj = NULL;
3c517a61 3079 int nid;
cc9a6c87 3080 unsigned int cpuset_mems_cookie;
8c8cc2c1
PE
3081
3082 if (flags & __GFP_THISNODE)
3083 return NULL;
3084
6cb06229 3085 local_flags = flags & (GFP_CONSTRAINT_MASK|GFP_RECLAIM_MASK);
765c4507 3086
cc9a6c87
MG
3087retry_cpuset:
3088 cpuset_mems_cookie = get_mems_allowed();
e7b691b0 3089 zonelist = node_zonelist(slab_node(), flags);
cc9a6c87 3090
3c517a61
CL
3091retry:
3092 /*
3093 * Look through allowed nodes for objects available
3094 * from existing per node queues.
3095 */
54a6eb5c
MG
3096 for_each_zone_zonelist(zone, z, zonelist, high_zoneidx) {
3097 nid = zone_to_nid(zone);
aedb0eb1 3098
54a6eb5c 3099 if (cpuset_zone_allowed_hardwall(zone, flags) &&
6a67368c
CL
3100 cache->node[nid] &&
3101 cache->node[nid]->free_objects) {
3c517a61
CL
3102 obj = ____cache_alloc_node(cache,
3103 flags | GFP_THISNODE, nid);
481c5346
CL
3104 if (obj)
3105 break;
3106 }
3c517a61
CL
3107 }
3108
cfce6604 3109 if (!obj) {
3c517a61
CL
3110 /*
3111 * This allocation will be performed within the constraints
3112 * of the current cpuset / memory policy requirements.
3113 * We may trigger various forms of reclaim on the allowed
3114 * set and go into memory reserves if necessary.
3115 */
0c3aa83e
JK
3116 struct page *page;
3117
dd47ea75
CL
3118 if (local_flags & __GFP_WAIT)
3119 local_irq_enable();
3120 kmem_flagcheck(cache, flags);
0c3aa83e 3121 page = kmem_getpages(cache, local_flags, numa_mem_id());
dd47ea75
CL
3122 if (local_flags & __GFP_WAIT)
3123 local_irq_disable();
0c3aa83e 3124 if (page) {
3c517a61
CL
3125 /*
3126 * Insert into the appropriate per node queues
3127 */
0c3aa83e
JK
3128 nid = page_to_nid(page);
3129 if (cache_grow(cache, flags, nid, page)) {
3c517a61
CL
3130 obj = ____cache_alloc_node(cache,
3131 flags | GFP_THISNODE, nid);
3132 if (!obj)
3133 /*
3134 * Another processor may allocate the
3135 * objects in the slab since we are
3136 * not holding any locks.
3137 */
3138 goto retry;
3139 } else {
b6a60451 3140 /* cache_grow already freed obj */
3c517a61
CL
3141 obj = NULL;
3142 }
3143 }
aedb0eb1 3144 }
cc9a6c87
MG
3145
3146 if (unlikely(!put_mems_allowed(cpuset_mems_cookie) && !obj))
3147 goto retry_cpuset;
765c4507
CL
3148 return obj;
3149}
3150
e498be7d
CL
3151/*
3152 * A interface to enable slab creation on nodeid
1da177e4 3153 */
8b98c169 3154static void *____cache_alloc_node(struct kmem_cache *cachep, gfp_t flags,
a737b3e2 3155 int nodeid)
e498be7d
CL
3156{
3157 struct list_head *entry;
8456a648 3158 struct page *page;
ce8eb6c4 3159 struct kmem_cache_node *n;
b28a02de 3160 void *obj;
b28a02de
PE
3161 int x;
3162
14e50c6a 3163 VM_BUG_ON(nodeid > num_online_nodes());
ce8eb6c4
CL
3164 n = cachep->node[nodeid];
3165 BUG_ON(!n);
b28a02de 3166
a737b3e2 3167retry:
ca3b9b91 3168 check_irq_off();
ce8eb6c4
CL
3169 spin_lock(&n->list_lock);
3170 entry = n->slabs_partial.next;
3171 if (entry == &n->slabs_partial) {
3172 n->free_touched = 1;
3173 entry = n->slabs_free.next;
3174 if (entry == &n->slabs_free)
b28a02de
PE
3175 goto must_grow;
3176 }
3177
8456a648 3178 page = list_entry(entry, struct page, lru);
b28a02de 3179 check_spinlock_acquired_node(cachep, nodeid);
b28a02de
PE
3180
3181 STATS_INC_NODEALLOCS(cachep);
3182 STATS_INC_ACTIVE(cachep);
3183 STATS_SET_HIGH(cachep);
3184
8456a648 3185 BUG_ON(page->active == cachep->num);
b28a02de 3186
8456a648 3187 obj = slab_get_obj(cachep, page, nodeid);
ce8eb6c4 3188 n->free_objects--;
b28a02de 3189 /* move slabp to correct slabp list: */
8456a648 3190 list_del(&page->lru);
b28a02de 3191
8456a648
JK
3192 if (page->active == cachep->num)
3193 list_add(&page->lru, &n->slabs_full);
a737b3e2 3194 else
8456a648 3195 list_add(&page->lru, &n->slabs_partial);
e498be7d 3196
ce8eb6c4 3197 spin_unlock(&n->list_lock);
b28a02de 3198 goto done;
e498be7d 3199
a737b3e2 3200must_grow:
ce8eb6c4 3201 spin_unlock(&n->list_lock);
3c517a61 3202 x = cache_grow(cachep, flags | GFP_THISNODE, nodeid, NULL);
765c4507
CL
3203 if (x)
3204 goto retry;
1da177e4 3205
8c8cc2c1 3206 return fallback_alloc(cachep, flags);
e498be7d 3207
a737b3e2 3208done:
b28a02de 3209 return obj;
e498be7d 3210}
8c8cc2c1 3211
8c8cc2c1 3212static __always_inline void *
48356303 3213slab_alloc_node(struct kmem_cache *cachep, gfp_t flags, int nodeid,
7c0cb9c6 3214 unsigned long caller)
8c8cc2c1
PE
3215{
3216 unsigned long save_flags;
3217 void *ptr;
7d6e6d09 3218 int slab_node = numa_mem_id();
8c8cc2c1 3219
dcce284a 3220 flags &= gfp_allowed_mask;
7e85ee0c 3221
cf40bd16
NP
3222 lockdep_trace_alloc(flags);
3223
773ff60e 3224 if (slab_should_failslab(cachep, flags))
824ebef1
AM
3225 return NULL;
3226
d79923fa
GC
3227 cachep = memcg_kmem_get_cache(cachep, flags);
3228
8c8cc2c1
PE
3229 cache_alloc_debugcheck_before(cachep, flags);
3230 local_irq_save(save_flags);
3231
eacbbae3 3232 if (nodeid == NUMA_NO_NODE)
7d6e6d09 3233 nodeid = slab_node;
8c8cc2c1 3234
6a67368c 3235 if (unlikely(!cachep->node[nodeid])) {
8c8cc2c1
PE
3236 /* Node not bootstrapped yet */
3237 ptr = fallback_alloc(cachep, flags);
3238 goto out;
3239 }
3240
7d6e6d09 3241 if (nodeid == slab_node) {
8c8cc2c1
PE
3242 /*
3243 * Use the locally cached objects if possible.
3244 * However ____cache_alloc does not allow fallback
3245 * to other nodes. It may fail while we still have
3246 * objects on other nodes available.
3247 */
3248 ptr = ____cache_alloc(cachep, flags);
3249 if (ptr)
3250 goto out;
3251 }
3252 /* ___cache_alloc_node can fall back to other nodes */
3253 ptr = ____cache_alloc_node(cachep, flags, nodeid);
3254 out:
3255 local_irq_restore(save_flags);
3256 ptr = cache_alloc_debugcheck_after(cachep, flags, ptr, caller);
8c138bc0 3257 kmemleak_alloc_recursive(ptr, cachep->object_size, 1, cachep->flags,
d5cff635 3258 flags);
8c8cc2c1 3259
c175eea4 3260 if (likely(ptr))
8c138bc0 3261 kmemcheck_slab_alloc(cachep, flags, ptr, cachep->object_size);
c175eea4 3262
d07dbea4 3263 if (unlikely((flags & __GFP_ZERO) && ptr))
8c138bc0 3264 memset(ptr, 0, cachep->object_size);
d07dbea4 3265
8c8cc2c1
PE
3266 return ptr;
3267}
3268
3269static __always_inline void *
3270__do_cache_alloc(struct kmem_cache *cache, gfp_t flags)
3271{
3272 void *objp;
3273
3274 if (unlikely(current->flags & (PF_SPREAD_SLAB | PF_MEMPOLICY))) {
3275 objp = alternate_node_alloc(cache, flags);
3276 if (objp)
3277 goto out;
3278 }
3279 objp = ____cache_alloc(cache, flags);
3280
3281 /*
3282 * We may just have run out of memory on the local node.
3283 * ____cache_alloc_node() knows how to locate memory on other nodes
3284 */
7d6e6d09
LS
3285 if (!objp)
3286 objp = ____cache_alloc_node(cache, flags, numa_mem_id());
8c8cc2c1
PE
3287
3288 out:
3289 return objp;
3290}
3291#else
3292
3293static __always_inline void *
3294__do_cache_alloc(struct kmem_cache *cachep, gfp_t flags)
3295{
3296 return ____cache_alloc(cachep, flags);
3297}
3298
3299#endif /* CONFIG_NUMA */
3300
3301static __always_inline void *
48356303 3302slab_alloc(struct kmem_cache *cachep, gfp_t flags, unsigned long caller)
8c8cc2c1
PE
3303{
3304 unsigned long save_flags;
3305 void *objp;
3306
dcce284a 3307 flags &= gfp_allowed_mask;
7e85ee0c 3308
cf40bd16
NP
3309 lockdep_trace_alloc(flags);
3310
773ff60e 3311 if (slab_should_failslab(cachep, flags))
824ebef1
AM
3312 return NULL;
3313
d79923fa
GC
3314 cachep = memcg_kmem_get_cache(cachep, flags);
3315
8c8cc2c1
PE
3316 cache_alloc_debugcheck_before(cachep, flags);
3317 local_irq_save(save_flags);
3318 objp = __do_cache_alloc(cachep, flags);
3319 local_irq_restore(save_flags);
3320 objp = cache_alloc_debugcheck_after(cachep, flags, objp, caller);
8c138bc0 3321 kmemleak_alloc_recursive(objp, cachep->object_size, 1, cachep->flags,
d5cff635 3322 flags);
8c8cc2c1
PE
3323 prefetchw(objp);
3324
c175eea4 3325 if (likely(objp))
8c138bc0 3326 kmemcheck_slab_alloc(cachep, flags, objp, cachep->object_size);
c175eea4 3327
d07dbea4 3328 if (unlikely((flags & __GFP_ZERO) && objp))
8c138bc0 3329 memset(objp, 0, cachep->object_size);
d07dbea4 3330
8c8cc2c1
PE
3331 return objp;
3332}
e498be7d
CL
3333
3334/*
3335 * Caller needs to acquire correct kmem_list's list_lock
3336 */
343e0d7a 3337static void free_block(struct kmem_cache *cachep, void **objpp, int nr_objects,
b28a02de 3338 int node)
1da177e4
LT
3339{
3340 int i;
ce8eb6c4 3341 struct kmem_cache_node *n;
1da177e4
LT
3342
3343 for (i = 0; i < nr_objects; i++) {
072bb0aa 3344 void *objp;
8456a648 3345 struct page *page;
1da177e4 3346
072bb0aa
MG
3347 clear_obj_pfmemalloc(&objpp[i]);
3348 objp = objpp[i];
3349
8456a648 3350 page = virt_to_head_page(objp);
ce8eb6c4 3351 n = cachep->node[node];
8456a648 3352 list_del(&page->lru);
ff69416e 3353 check_spinlock_acquired_node(cachep, node);
8456a648 3354 slab_put_obj(cachep, page, objp, node);
1da177e4 3355 STATS_DEC_ACTIVE(cachep);
ce8eb6c4 3356 n->free_objects++;
1da177e4
LT
3357
3358 /* fixup slab chains */
8456a648 3359 if (page->active == 0) {
ce8eb6c4
CL
3360 if (n->free_objects > n->free_limit) {
3361 n->free_objects -= cachep->num;
e5ac9c5a
RT
3362 /* No need to drop any previously held
3363 * lock here, even if we have a off-slab slab
3364 * descriptor it is guaranteed to come from
3365 * a different cache, refer to comments before
3366 * alloc_slabmgmt.
3367 */
8456a648 3368 slab_destroy(cachep, page);
1da177e4 3369 } else {
8456a648 3370 list_add(&page->lru, &n->slabs_free);
1da177e4
LT
3371 }
3372 } else {
3373 /* Unconditionally move a slab to the end of the
3374 * partial list on free - maximum time for the
3375 * other objects to be freed, too.
3376 */
8456a648 3377 list_add_tail(&page->lru, &n->slabs_partial);
1da177e4
LT
3378 }
3379 }
3380}
3381
343e0d7a 3382static void cache_flusharray(struct kmem_cache *cachep, struct array_cache *ac)
1da177e4
LT
3383{
3384 int batchcount;
ce8eb6c4 3385 struct kmem_cache_node *n;
7d6e6d09 3386 int node = numa_mem_id();
1da177e4
LT
3387
3388 batchcount = ac->batchcount;
3389#if DEBUG
3390 BUG_ON(!batchcount || batchcount > ac->avail);
3391#endif
3392 check_irq_off();
ce8eb6c4
CL
3393 n = cachep->node[node];
3394 spin_lock(&n->list_lock);
3395 if (n->shared) {
3396 struct array_cache *shared_array = n->shared;
b28a02de 3397 int max = shared_array->limit - shared_array->avail;
1da177e4
LT
3398 if (max) {
3399 if (batchcount > max)
3400 batchcount = max;
e498be7d 3401 memcpy(&(shared_array->entry[shared_array->avail]),
b28a02de 3402 ac->entry, sizeof(void *) * batchcount);
1da177e4
LT
3403 shared_array->avail += batchcount;
3404 goto free_done;
3405 }
3406 }
3407
ff69416e 3408 free_block(cachep, ac->entry, batchcount, node);
a737b3e2 3409free_done:
1da177e4
LT
3410#if STATS
3411 {
3412 int i = 0;
3413 struct list_head *p;
3414
ce8eb6c4
CL
3415 p = n->slabs_free.next;
3416 while (p != &(n->slabs_free)) {
8456a648 3417 struct page *page;
1da177e4 3418
8456a648
JK
3419 page = list_entry(p, struct page, lru);
3420 BUG_ON(page->active);
1da177e4
LT
3421
3422 i++;
3423 p = p->next;
3424 }
3425 STATS_SET_FREEABLE(cachep, i);
3426 }
3427#endif
ce8eb6c4 3428 spin_unlock(&n->list_lock);
1da177e4 3429 ac->avail -= batchcount;
a737b3e2 3430 memmove(ac->entry, &(ac->entry[batchcount]), sizeof(void *)*ac->avail);
1da177e4
LT
3431}
3432
3433/*
a737b3e2
AM
3434 * Release an obj back to its cache. If the obj has a constructed state, it must
3435 * be in this state _before_ it is released. Called with disabled ints.
1da177e4 3436 */
a947eb95 3437static inline void __cache_free(struct kmem_cache *cachep, void *objp,
7c0cb9c6 3438 unsigned long caller)
1da177e4 3439{
9a2dba4b 3440 struct array_cache *ac = cpu_cache_get(cachep);
1da177e4
LT
3441
3442 check_irq_off();
d5cff635 3443 kmemleak_free_recursive(objp, cachep->flags);
a947eb95 3444 objp = cache_free_debugcheck(cachep, objp, caller);
1da177e4 3445
8c138bc0 3446 kmemcheck_slab_free(cachep, objp, cachep->object_size);
c175eea4 3447
1807a1aa
SS
3448 /*
3449 * Skip calling cache_free_alien() when the platform is not numa.
3450 * This will avoid cache misses that happen while accessing slabp (which
3451 * is per page memory reference) to get nodeid. Instead use a global
3452 * variable to skip the call, which is mostly likely to be present in
3453 * the cache.
3454 */
b6e68bc1 3455 if (nr_online_nodes > 1 && cache_free_alien(cachep, objp))
729bd0b7
PE
3456 return;
3457
1da177e4
LT
3458 if (likely(ac->avail < ac->limit)) {
3459 STATS_INC_FREEHIT(cachep);
1da177e4
LT
3460 } else {
3461 STATS_INC_FREEMISS(cachep);
3462 cache_flusharray(cachep, ac);
1da177e4 3463 }
42c8c99c 3464
072bb0aa 3465 ac_put_obj(cachep, ac, objp);
1da177e4
LT
3466}
3467
3468/**
3469 * kmem_cache_alloc - Allocate an object
3470 * @cachep: The cache to allocate from.
3471 * @flags: See kmalloc().
3472 *
3473 * Allocate an object from this cache. The flags are only relevant
3474 * if the cache has no available objects.
3475 */
343e0d7a 3476void *kmem_cache_alloc(struct kmem_cache *cachep, gfp_t flags)
1da177e4 3477{
48356303 3478 void *ret = slab_alloc(cachep, flags, _RET_IP_);
36555751 3479
ca2b84cb 3480 trace_kmem_cache_alloc(_RET_IP_, ret,
8c138bc0 3481 cachep->object_size, cachep->size, flags);
36555751
EGM
3482
3483 return ret;
1da177e4
LT
3484}
3485EXPORT_SYMBOL(kmem_cache_alloc);
3486
0f24f128 3487#ifdef CONFIG_TRACING
85beb586 3488void *
4052147c 3489kmem_cache_alloc_trace(struct kmem_cache *cachep, gfp_t flags, size_t size)
36555751 3490{
85beb586
SR
3491 void *ret;
3492
48356303 3493 ret = slab_alloc(cachep, flags, _RET_IP_);
85beb586
SR
3494
3495 trace_kmalloc(_RET_IP_, ret,
ff4fcd01 3496 size, cachep->size, flags);
85beb586 3497 return ret;
36555751 3498}
85beb586 3499EXPORT_SYMBOL(kmem_cache_alloc_trace);
36555751
EGM
3500#endif
3501
1da177e4 3502#ifdef CONFIG_NUMA
d0d04b78
ZL
3503/**
3504 * kmem_cache_alloc_node - Allocate an object on the specified node
3505 * @cachep: The cache to allocate from.
3506 * @flags: See kmalloc().
3507 * @nodeid: node number of the target node.
3508 *
3509 * Identical to kmem_cache_alloc but it will allocate memory on the given
3510 * node, which can improve the performance for cpu bound structures.
3511 *
3512 * Fallback to other node is possible if __GFP_THISNODE is not set.
3513 */
8b98c169
CH
3514void *kmem_cache_alloc_node(struct kmem_cache *cachep, gfp_t flags, int nodeid)
3515{
48356303 3516 void *ret = slab_alloc_node(cachep, flags, nodeid, _RET_IP_);
36555751 3517
ca2b84cb 3518 trace_kmem_cache_alloc_node(_RET_IP_, ret,
8c138bc0 3519 cachep->object_size, cachep->size,
ca2b84cb 3520 flags, nodeid);
36555751
EGM
3521
3522 return ret;
8b98c169 3523}
1da177e4
LT
3524EXPORT_SYMBOL(kmem_cache_alloc_node);
3525
0f24f128 3526#ifdef CONFIG_TRACING
4052147c 3527void *kmem_cache_alloc_node_trace(struct kmem_cache *cachep,
85beb586 3528 gfp_t flags,
4052147c
EG
3529 int nodeid,
3530 size_t size)
36555751 3531{
85beb586
SR
3532 void *ret;
3533
592f4145 3534 ret = slab_alloc_node(cachep, flags, nodeid, _RET_IP_);
7c0cb9c6 3535
85beb586 3536 trace_kmalloc_node(_RET_IP_, ret,
ff4fcd01 3537 size, cachep->size,
85beb586
SR
3538 flags, nodeid);
3539 return ret;
36555751 3540}
85beb586 3541EXPORT_SYMBOL(kmem_cache_alloc_node_trace);
36555751
EGM
3542#endif
3543
8b98c169 3544static __always_inline void *
7c0cb9c6 3545__do_kmalloc_node(size_t size, gfp_t flags, int node, unsigned long caller)
97e2bde4 3546{
343e0d7a 3547 struct kmem_cache *cachep;
97e2bde4 3548
2c59dd65 3549 cachep = kmalloc_slab(size, flags);
6cb8f913
CL
3550 if (unlikely(ZERO_OR_NULL_PTR(cachep)))
3551 return cachep;
4052147c 3552 return kmem_cache_alloc_node_trace(cachep, flags, node, size);
97e2bde4 3553}
8b98c169 3554
0bb38a5c 3555#if defined(CONFIG_DEBUG_SLAB) || defined(CONFIG_TRACING)
8b98c169
CH
3556void *__kmalloc_node(size_t size, gfp_t flags, int node)
3557{
7c0cb9c6 3558 return __do_kmalloc_node(size, flags, node, _RET_IP_);
8b98c169 3559}
dbe5e69d 3560EXPORT_SYMBOL(__kmalloc_node);
8b98c169
CH
3561
3562void *__kmalloc_node_track_caller(size_t size, gfp_t flags,
ce71e27c 3563 int node, unsigned long caller)
8b98c169 3564{
7c0cb9c6 3565 return __do_kmalloc_node(size, flags, node, caller);
8b98c169
CH
3566}
3567EXPORT_SYMBOL(__kmalloc_node_track_caller);
3568#else
3569void *__kmalloc_node(size_t size, gfp_t flags, int node)
3570{
7c0cb9c6 3571 return __do_kmalloc_node(size, flags, node, 0);
8b98c169
CH
3572}
3573EXPORT_SYMBOL(__kmalloc_node);
0bb38a5c 3574#endif /* CONFIG_DEBUG_SLAB || CONFIG_TRACING */
8b98c169 3575#endif /* CONFIG_NUMA */
1da177e4
LT
3576
3577/**
800590f5 3578 * __do_kmalloc - allocate memory
1da177e4 3579 * @size: how many bytes of memory are required.
800590f5 3580 * @flags: the type of memory to allocate (see kmalloc).
911851e6 3581 * @caller: function caller for debug tracking of the caller
1da177e4 3582 */
7fd6b141 3583static __always_inline void *__do_kmalloc(size_t size, gfp_t flags,
7c0cb9c6 3584 unsigned long caller)
1da177e4 3585{
343e0d7a 3586 struct kmem_cache *cachep;
36555751 3587 void *ret;
1da177e4 3588
97e2bde4
MS
3589 /* If you want to save a few bytes .text space: replace
3590 * __ with kmem_.
3591 * Then kmalloc uses the uninlined functions instead of the inline
3592 * functions.
3593 */
2c59dd65 3594 cachep = kmalloc_slab(size, flags);
a5c96d8a
LT
3595 if (unlikely(ZERO_OR_NULL_PTR(cachep)))
3596 return cachep;
48356303 3597 ret = slab_alloc(cachep, flags, caller);
36555751 3598
7c0cb9c6 3599 trace_kmalloc(caller, ret,
3b0efdfa 3600 size, cachep->size, flags);
36555751
EGM
3601
3602 return ret;
7fd6b141
PE
3603}
3604
7fd6b141 3605
0bb38a5c 3606#if defined(CONFIG_DEBUG_SLAB) || defined(CONFIG_TRACING)
7fd6b141
PE
3607void *__kmalloc(size_t size, gfp_t flags)
3608{
7c0cb9c6 3609 return __do_kmalloc(size, flags, _RET_IP_);
1da177e4
LT
3610}
3611EXPORT_SYMBOL(__kmalloc);
3612
ce71e27c 3613void *__kmalloc_track_caller(size_t size, gfp_t flags, unsigned long caller)
7fd6b141 3614{
7c0cb9c6 3615 return __do_kmalloc(size, flags, caller);
7fd6b141
PE
3616}
3617EXPORT_SYMBOL(__kmalloc_track_caller);
1d2c8eea
CH
3618
3619#else
3620void *__kmalloc(size_t size, gfp_t flags)
3621{
7c0cb9c6 3622 return __do_kmalloc(size, flags, 0);
1d2c8eea
CH
3623}
3624EXPORT_SYMBOL(__kmalloc);
7fd6b141
PE
3625#endif
3626
1da177e4
LT
3627/**
3628 * kmem_cache_free - Deallocate an object
3629 * @cachep: The cache the allocation was from.
3630 * @objp: The previously allocated object.
3631 *
3632 * Free an object which was previously allocated from this
3633 * cache.
3634 */
343e0d7a 3635void kmem_cache_free(struct kmem_cache *cachep, void *objp)
1da177e4
LT
3636{
3637 unsigned long flags;
b9ce5ef4
GC
3638 cachep = cache_from_obj(cachep, objp);
3639 if (!cachep)
3640 return;
1da177e4
LT
3641
3642 local_irq_save(flags);
d97d476b 3643 debug_check_no_locks_freed(objp, cachep->object_size);
3ac7fe5a 3644 if (!(cachep->flags & SLAB_DEBUG_OBJECTS))
8c138bc0 3645 debug_check_no_obj_freed(objp, cachep->object_size);
7c0cb9c6 3646 __cache_free(cachep, objp, _RET_IP_);
1da177e4 3647 local_irq_restore(flags);
36555751 3648
ca2b84cb 3649 trace_kmem_cache_free(_RET_IP_, objp);
1da177e4
LT
3650}
3651EXPORT_SYMBOL(kmem_cache_free);
3652
1da177e4
LT
3653/**
3654 * kfree - free previously allocated memory
3655 * @objp: pointer returned by kmalloc.
3656 *
80e93eff
PE
3657 * If @objp is NULL, no operation is performed.
3658 *
1da177e4
LT
3659 * Don't free memory not originally allocated by kmalloc()
3660 * or you will run into trouble.
3661 */
3662void kfree(const void *objp)
3663{
343e0d7a 3664 struct kmem_cache *c;
1da177e4
LT
3665 unsigned long flags;
3666
2121db74
PE
3667 trace_kfree(_RET_IP_, objp);
3668
6cb8f913 3669 if (unlikely(ZERO_OR_NULL_PTR(objp)))
1da177e4
LT
3670 return;
3671 local_irq_save(flags);
3672 kfree_debugcheck(objp);
6ed5eb22 3673 c = virt_to_cache(objp);
8c138bc0
CL
3674 debug_check_no_locks_freed(objp, c->object_size);
3675
3676 debug_check_no_obj_freed(objp, c->object_size);
7c0cb9c6 3677 __cache_free(c, (void *)objp, _RET_IP_);
1da177e4
LT
3678 local_irq_restore(flags);
3679}
3680EXPORT_SYMBOL(kfree);
3681
e498be7d 3682/*
ce8eb6c4 3683 * This initializes kmem_cache_node or resizes various caches for all nodes.
e498be7d 3684 */
83b519e8 3685static int alloc_kmemlist(struct kmem_cache *cachep, gfp_t gfp)
e498be7d
CL
3686{
3687 int node;
ce8eb6c4 3688 struct kmem_cache_node *n;
cafeb02e 3689 struct array_cache *new_shared;
3395ee05 3690 struct array_cache **new_alien = NULL;
e498be7d 3691
9c09a95c 3692 for_each_online_node(node) {
cafeb02e 3693
3395ee05 3694 if (use_alien_caches) {
83b519e8 3695 new_alien = alloc_alien_cache(node, cachep->limit, gfp);
3395ee05
PM
3696 if (!new_alien)
3697 goto fail;
3698 }
cafeb02e 3699
63109846
ED
3700 new_shared = NULL;
3701 if (cachep->shared) {
3702 new_shared = alloc_arraycache(node,
0718dc2a 3703 cachep->shared*cachep->batchcount,
83b519e8 3704 0xbaadf00d, gfp);
63109846
ED
3705 if (!new_shared) {
3706 free_alien_cache(new_alien);
3707 goto fail;
3708 }
0718dc2a 3709 }
cafeb02e 3710
ce8eb6c4
CL
3711 n = cachep->node[node];
3712 if (n) {
3713 struct array_cache *shared = n->shared;
cafeb02e 3714
ce8eb6c4 3715 spin_lock_irq(&n->list_lock);
e498be7d 3716
cafeb02e 3717 if (shared)
0718dc2a
CL
3718 free_block(cachep, shared->entry,
3719 shared->avail, node);
e498be7d 3720
ce8eb6c4
CL
3721 n->shared = new_shared;
3722 if (!n->alien) {
3723 n->alien = new_alien;
e498be7d
CL
3724 new_alien = NULL;
3725 }
ce8eb6c4 3726 n->free_limit = (1 + nr_cpus_node(node)) *
a737b3e2 3727 cachep->batchcount + cachep->num;
ce8eb6c4 3728 spin_unlock_irq(&n->list_lock);
cafeb02e 3729 kfree(shared);
e498be7d
CL
3730 free_alien_cache(new_alien);
3731 continue;
3732 }
ce8eb6c4
CL
3733 n = kmalloc_node(sizeof(struct kmem_cache_node), gfp, node);
3734 if (!n) {
0718dc2a
CL
3735 free_alien_cache(new_alien);
3736 kfree(new_shared);
e498be7d 3737 goto fail;
0718dc2a 3738 }
e498be7d 3739
ce8eb6c4
CL
3740 kmem_cache_node_init(n);
3741 n->next_reap = jiffies + REAPTIMEOUT_LIST3 +
a737b3e2 3742 ((unsigned long)cachep) % REAPTIMEOUT_LIST3;
ce8eb6c4
CL
3743 n->shared = new_shared;
3744 n->alien = new_alien;
3745 n->free_limit = (1 + nr_cpus_node(node)) *
a737b3e2 3746 cachep->batchcount + cachep->num;
ce8eb6c4 3747 cachep->node[node] = n;
e498be7d 3748 }
cafeb02e 3749 return 0;
0718dc2a 3750
a737b3e2 3751fail:
3b0efdfa 3752 if (!cachep->list.next) {
0718dc2a
CL
3753 /* Cache is not active yet. Roll back what we did */
3754 node--;
3755 while (node >= 0) {
6a67368c 3756 if (cachep->node[node]) {
ce8eb6c4 3757 n = cachep->node[node];
0718dc2a 3758
ce8eb6c4
CL
3759 kfree(n->shared);
3760 free_alien_cache(n->alien);
3761 kfree(n);
6a67368c 3762 cachep->node[node] = NULL;
0718dc2a
CL
3763 }
3764 node--;
3765 }
3766 }
cafeb02e 3767 return -ENOMEM;
e498be7d
CL
3768}
3769
1da177e4 3770struct ccupdate_struct {
343e0d7a 3771 struct kmem_cache *cachep;
acfe7d74 3772 struct array_cache *new[0];
1da177e4
LT
3773};
3774
3775static void do_ccupdate_local(void *info)
3776{
a737b3e2 3777 struct ccupdate_struct *new = info;
1da177e4
LT
3778 struct array_cache *old;
3779
3780 check_irq_off();
9a2dba4b 3781 old = cpu_cache_get(new->cachep);
e498be7d 3782
1da177e4
LT
3783 new->cachep->array[smp_processor_id()] = new->new[smp_processor_id()];
3784 new->new[smp_processor_id()] = old;
3785}
3786
18004c5d 3787/* Always called with the slab_mutex held */
943a451a 3788static int __do_tune_cpucache(struct kmem_cache *cachep, int limit,
83b519e8 3789 int batchcount, int shared, gfp_t gfp)
1da177e4 3790{
d2e7b7d0 3791 struct ccupdate_struct *new;
2ed3a4ef 3792 int i;
1da177e4 3793
acfe7d74
ED
3794 new = kzalloc(sizeof(*new) + nr_cpu_ids * sizeof(struct array_cache *),
3795 gfp);
d2e7b7d0
SS
3796 if (!new)
3797 return -ENOMEM;
3798
e498be7d 3799 for_each_online_cpu(i) {
7d6e6d09 3800 new->new[i] = alloc_arraycache(cpu_to_mem(i), limit,
83b519e8 3801 batchcount, gfp);
d2e7b7d0 3802 if (!new->new[i]) {
b28a02de 3803 for (i--; i >= 0; i--)
d2e7b7d0
SS
3804 kfree(new->new[i]);
3805 kfree(new);
e498be7d 3806 return -ENOMEM;
1da177e4
LT
3807 }
3808 }
d2e7b7d0 3809 new->cachep = cachep;
1da177e4 3810
15c8b6c1 3811 on_each_cpu(do_ccupdate_local, (void *)new, 1);
e498be7d 3812
1da177e4 3813 check_irq_on();
1da177e4
LT
3814 cachep->batchcount = batchcount;
3815 cachep->limit = limit;
e498be7d 3816 cachep->shared = shared;
1da177e4 3817
e498be7d 3818 for_each_online_cpu(i) {
d2e7b7d0 3819 struct array_cache *ccold = new->new[i];
1da177e4
LT
3820 if (!ccold)
3821 continue;
6a67368c 3822 spin_lock_irq(&cachep->node[cpu_to_mem(i)]->list_lock);
7d6e6d09 3823 free_block(cachep, ccold->entry, ccold->avail, cpu_to_mem(i));
6a67368c 3824 spin_unlock_irq(&cachep->node[cpu_to_mem(i)]->list_lock);
1da177e4
LT
3825 kfree(ccold);
3826 }
d2e7b7d0 3827 kfree(new);
83b519e8 3828 return alloc_kmemlist(cachep, gfp);
1da177e4
LT
3829}
3830
943a451a
GC
3831static int do_tune_cpucache(struct kmem_cache *cachep, int limit,
3832 int batchcount, int shared, gfp_t gfp)
3833{
3834 int ret;
3835 struct kmem_cache *c = NULL;
3836 int i = 0;
3837
3838 ret = __do_tune_cpucache(cachep, limit, batchcount, shared, gfp);
3839
3840 if (slab_state < FULL)
3841 return ret;
3842
3843 if ((ret < 0) || !is_root_cache(cachep))
3844 return ret;
3845
ebe945c2 3846 VM_BUG_ON(!mutex_is_locked(&slab_mutex));
943a451a 3847 for_each_memcg_cache_index(i) {
2ade4de8 3848 c = cache_from_memcg_idx(cachep, i);
943a451a
GC
3849 if (c)
3850 /* return value determined by the parent cache only */
3851 __do_tune_cpucache(c, limit, batchcount, shared, gfp);
3852 }
3853
3854 return ret;
3855}
3856
18004c5d 3857/* Called with slab_mutex held always */
83b519e8 3858static int enable_cpucache(struct kmem_cache *cachep, gfp_t gfp)
1da177e4
LT
3859{
3860 int err;
943a451a
GC
3861 int limit = 0;
3862 int shared = 0;
3863 int batchcount = 0;
3864
3865 if (!is_root_cache(cachep)) {
3866 struct kmem_cache *root = memcg_root_cache(cachep);
3867 limit = root->limit;
3868 shared = root->shared;
3869 batchcount = root->batchcount;
3870 }
1da177e4 3871
943a451a
GC
3872 if (limit && shared && batchcount)
3873 goto skip_setup;
a737b3e2
AM
3874 /*
3875 * The head array serves three purposes:
1da177e4
LT
3876 * - create a LIFO ordering, i.e. return objects that are cache-warm
3877 * - reduce the number of spinlock operations.
a737b3e2 3878 * - reduce the number of linked list operations on the slab and
1da177e4
LT
3879 * bufctl chains: array operations are cheaper.
3880 * The numbers are guessed, we should auto-tune as described by
3881 * Bonwick.
3882 */
3b0efdfa 3883 if (cachep->size > 131072)
1da177e4 3884 limit = 1;
3b0efdfa 3885 else if (cachep->size > PAGE_SIZE)
1da177e4 3886 limit = 8;
3b0efdfa 3887 else if (cachep->size > 1024)
1da177e4 3888 limit = 24;
3b0efdfa 3889 else if (cachep->size > 256)
1da177e4
LT
3890 limit = 54;
3891 else
3892 limit = 120;
3893
a737b3e2
AM
3894 /*
3895 * CPU bound tasks (e.g. network routing) can exhibit cpu bound
1da177e4
LT
3896 * allocation behaviour: Most allocs on one cpu, most free operations
3897 * on another cpu. For these cases, an efficient object passing between
3898 * cpus is necessary. This is provided by a shared array. The array
3899 * replaces Bonwick's magazine layer.
3900 * On uniprocessor, it's functionally equivalent (but less efficient)
3901 * to a larger limit. Thus disabled by default.
3902 */
3903 shared = 0;
3b0efdfa 3904 if (cachep->size <= PAGE_SIZE && num_possible_cpus() > 1)
1da177e4 3905 shared = 8;
1da177e4
LT
3906
3907#if DEBUG
a737b3e2
AM
3908 /*
3909 * With debugging enabled, large batchcount lead to excessively long
3910 * periods with disabled local interrupts. Limit the batchcount
1da177e4
LT
3911 */
3912 if (limit > 32)
3913 limit = 32;
3914#endif
943a451a
GC
3915 batchcount = (limit + 1) / 2;
3916skip_setup:
3917 err = do_tune_cpucache(cachep, limit, batchcount, shared, gfp);
1da177e4
LT
3918 if (err)
3919 printk(KERN_ERR "enable_cpucache failed for %s, error %d.\n",
b28a02de 3920 cachep->name, -err);
2ed3a4ef 3921 return err;
1da177e4
LT
3922}
3923
1b55253a 3924/*
ce8eb6c4
CL
3925 * Drain an array if it contains any elements taking the node lock only if
3926 * necessary. Note that the node listlock also protects the array_cache
b18e7e65 3927 * if drain_array() is used on the shared array.
1b55253a 3928 */
ce8eb6c4 3929static void drain_array(struct kmem_cache *cachep, struct kmem_cache_node *n,
1b55253a 3930 struct array_cache *ac, int force, int node)
1da177e4
LT
3931{
3932 int tofree;
3933
1b55253a
CL
3934 if (!ac || !ac->avail)
3935 return;
1da177e4
LT
3936 if (ac->touched && !force) {
3937 ac->touched = 0;
b18e7e65 3938 } else {
ce8eb6c4 3939 spin_lock_irq(&n->list_lock);
b18e7e65
CL
3940 if (ac->avail) {
3941 tofree = force ? ac->avail : (ac->limit + 4) / 5;
3942 if (tofree > ac->avail)
3943 tofree = (ac->avail + 1) / 2;
3944 free_block(cachep, ac->entry, tofree, node);
3945 ac->avail -= tofree;
3946 memmove(ac->entry, &(ac->entry[tofree]),
3947 sizeof(void *) * ac->avail);
3948 }
ce8eb6c4 3949 spin_unlock_irq(&n->list_lock);
1da177e4
LT
3950 }
3951}
3952
3953/**
3954 * cache_reap - Reclaim memory from caches.
05fb6bf0 3955 * @w: work descriptor
1da177e4
LT
3956 *
3957 * Called from workqueue/eventd every few seconds.
3958 * Purpose:
3959 * - clear the per-cpu caches for this CPU.
3960 * - return freeable pages to the main free memory pool.
3961 *
a737b3e2
AM
3962 * If we cannot acquire the cache chain mutex then just give up - we'll try
3963 * again on the next iteration.
1da177e4 3964 */
7c5cae36 3965static void cache_reap(struct work_struct *w)
1da177e4 3966{
7a7c381d 3967 struct kmem_cache *searchp;
ce8eb6c4 3968 struct kmem_cache_node *n;
7d6e6d09 3969 int node = numa_mem_id();
bf6aede7 3970 struct delayed_work *work = to_delayed_work(w);
1da177e4 3971
18004c5d 3972 if (!mutex_trylock(&slab_mutex))
1da177e4 3973 /* Give up. Setup the next iteration. */
7c5cae36 3974 goto out;
1da177e4 3975
18004c5d 3976 list_for_each_entry(searchp, &slab_caches, list) {
1da177e4
LT
3977 check_irq_on();
3978
35386e3b 3979 /*
ce8eb6c4 3980 * We only take the node lock if absolutely necessary and we
35386e3b
CL
3981 * have established with reasonable certainty that
3982 * we can do some work if the lock was obtained.
3983 */
ce8eb6c4 3984 n = searchp->node[node];
35386e3b 3985
ce8eb6c4 3986 reap_alien(searchp, n);
1da177e4 3987
ce8eb6c4 3988 drain_array(searchp, n, cpu_cache_get(searchp), 0, node);
1da177e4 3989
35386e3b
CL
3990 /*
3991 * These are racy checks but it does not matter
3992 * if we skip one check or scan twice.
3993 */
ce8eb6c4 3994 if (time_after(n->next_reap, jiffies))
35386e3b 3995 goto next;
1da177e4 3996
ce8eb6c4 3997 n->next_reap = jiffies + REAPTIMEOUT_LIST3;
1da177e4 3998
ce8eb6c4 3999 drain_array(searchp, n, n->shared, 0, node);
1da177e4 4000
ce8eb6c4
CL
4001 if (n->free_touched)
4002 n->free_touched = 0;
ed11d9eb
CL
4003 else {
4004 int freed;
1da177e4 4005
ce8eb6c4 4006 freed = drain_freelist(searchp, n, (n->free_limit +
ed11d9eb
CL
4007 5 * searchp->num - 1) / (5 * searchp->num));
4008 STATS_ADD_REAPED(searchp, freed);
4009 }
35386e3b 4010next:
1da177e4
LT
4011 cond_resched();
4012 }
4013 check_irq_on();
18004c5d 4014 mutex_unlock(&slab_mutex);
8fce4d8e 4015 next_reap_node();
7c5cae36 4016out:
a737b3e2 4017 /* Set up the next iteration */
7c5cae36 4018 schedule_delayed_work(work, round_jiffies_relative(REAPTIMEOUT_CPUC));
1da177e4
LT
4019}
4020
158a9624 4021#ifdef CONFIG_SLABINFO
0d7561c6 4022void get_slabinfo(struct kmem_cache *cachep, struct slabinfo *sinfo)
1da177e4 4023{
8456a648 4024 struct page *page;
b28a02de
PE
4025 unsigned long active_objs;
4026 unsigned long num_objs;
4027 unsigned long active_slabs = 0;
4028 unsigned long num_slabs, free_objects = 0, shared_avail = 0;
e498be7d 4029 const char *name;
1da177e4 4030 char *error = NULL;
e498be7d 4031 int node;
ce8eb6c4 4032 struct kmem_cache_node *n;
1da177e4 4033
1da177e4
LT
4034 active_objs = 0;
4035 num_slabs = 0;
e498be7d 4036 for_each_online_node(node) {
ce8eb6c4
CL
4037 n = cachep->node[node];
4038 if (!n)
e498be7d
CL
4039 continue;
4040
ca3b9b91 4041 check_irq_on();
ce8eb6c4 4042 spin_lock_irq(&n->list_lock);
e498be7d 4043
8456a648
JK
4044 list_for_each_entry(page, &n->slabs_full, lru) {
4045 if (page->active != cachep->num && !error)
e498be7d
CL
4046 error = "slabs_full accounting error";
4047 active_objs += cachep->num;
4048 active_slabs++;
4049 }
8456a648
JK
4050 list_for_each_entry(page, &n->slabs_partial, lru) {
4051 if (page->active == cachep->num && !error)
106a74e1 4052 error = "slabs_partial accounting error";
8456a648 4053 if (!page->active && !error)
106a74e1 4054 error = "slabs_partial accounting error";
8456a648 4055 active_objs += page->active;
e498be7d
CL
4056 active_slabs++;
4057 }
8456a648
JK
4058 list_for_each_entry(page, &n->slabs_free, lru) {
4059 if (page->active && !error)
106a74e1 4060 error = "slabs_free accounting error";
e498be7d
CL
4061 num_slabs++;
4062 }
ce8eb6c4
CL
4063 free_objects += n->free_objects;
4064 if (n->shared)
4065 shared_avail += n->shared->avail;
e498be7d 4066
ce8eb6c4 4067 spin_unlock_irq(&n->list_lock);
1da177e4 4068 }
b28a02de
PE
4069 num_slabs += active_slabs;
4070 num_objs = num_slabs * cachep->num;
e498be7d 4071 if (num_objs - active_objs != free_objects && !error)
1da177e4
LT
4072 error = "free_objects accounting error";
4073
b28a02de 4074 name = cachep->name;
1da177e4
LT
4075 if (error)
4076 printk(KERN_ERR "slab: cache %s error: %s\n", name, error);
4077
0d7561c6
GC
4078 sinfo->active_objs = active_objs;
4079 sinfo->num_objs = num_objs;
4080 sinfo->active_slabs = active_slabs;
4081 sinfo->num_slabs = num_slabs;
4082 sinfo->shared_avail = shared_avail;
4083 sinfo->limit = cachep->limit;
4084 sinfo->batchcount = cachep->batchcount;
4085 sinfo->shared = cachep->shared;
4086 sinfo->objects_per_slab = cachep->num;
4087 sinfo->cache_order = cachep->gfporder;
4088}
4089
4090void slabinfo_show_stats(struct seq_file *m, struct kmem_cache *cachep)
4091{
1da177e4 4092#if STATS
ce8eb6c4 4093 { /* node stats */
1da177e4
LT
4094 unsigned long high = cachep->high_mark;
4095 unsigned long allocs = cachep->num_allocations;
4096 unsigned long grown = cachep->grown;
4097 unsigned long reaped = cachep->reaped;
4098 unsigned long errors = cachep->errors;
4099 unsigned long max_freeable = cachep->max_freeable;
1da177e4 4100 unsigned long node_allocs = cachep->node_allocs;
e498be7d 4101 unsigned long node_frees = cachep->node_frees;
fb7faf33 4102 unsigned long overflows = cachep->node_overflow;
1da177e4 4103
e92dd4fd
JP
4104 seq_printf(m, " : globalstat %7lu %6lu %5lu %4lu "
4105 "%4lu %4lu %4lu %4lu %4lu",
4106 allocs, high, grown,
4107 reaped, errors, max_freeable, node_allocs,
4108 node_frees, overflows);
1da177e4
LT
4109 }
4110 /* cpu stats */
4111 {
4112 unsigned long allochit = atomic_read(&cachep->allochit);
4113 unsigned long allocmiss = atomic_read(&cachep->allocmiss);
4114 unsigned long freehit = atomic_read(&cachep->freehit);
4115 unsigned long freemiss = atomic_read(&cachep->freemiss);
4116
4117 seq_printf(m, " : cpustat %6lu %6lu %6lu %6lu",
b28a02de 4118 allochit, allocmiss, freehit, freemiss);
1da177e4
LT
4119 }
4120#endif
1da177e4
LT
4121}
4122
1da177e4
LT
4123#define MAX_SLABINFO_WRITE 128
4124/**
4125 * slabinfo_write - Tuning for the slab allocator
4126 * @file: unused
4127 * @buffer: user buffer
4128 * @count: data length
4129 * @ppos: unused
4130 */
b7454ad3 4131ssize_t slabinfo_write(struct file *file, const char __user *buffer,
b28a02de 4132 size_t count, loff_t *ppos)
1da177e4 4133{
b28a02de 4134 char kbuf[MAX_SLABINFO_WRITE + 1], *tmp;
1da177e4 4135 int limit, batchcount, shared, res;
7a7c381d 4136 struct kmem_cache *cachep;
b28a02de 4137
1da177e4
LT
4138 if (count > MAX_SLABINFO_WRITE)
4139 return -EINVAL;
4140 if (copy_from_user(&kbuf, buffer, count))
4141 return -EFAULT;
b28a02de 4142 kbuf[MAX_SLABINFO_WRITE] = '\0';
1da177e4
LT
4143
4144 tmp = strchr(kbuf, ' ');
4145 if (!tmp)
4146 return -EINVAL;
4147 *tmp = '\0';
4148 tmp++;
4149 if (sscanf(tmp, " %d %d %d", &limit, &batchcount, &shared) != 3)
4150 return -EINVAL;
4151
4152 /* Find the cache in the chain of caches. */
18004c5d 4153 mutex_lock(&slab_mutex);
1da177e4 4154 res = -EINVAL;
18004c5d 4155 list_for_each_entry(cachep, &slab_caches, list) {
1da177e4 4156 if (!strcmp(cachep->name, kbuf)) {
a737b3e2
AM
4157 if (limit < 1 || batchcount < 1 ||
4158 batchcount > limit || shared < 0) {
e498be7d 4159 res = 0;
1da177e4 4160 } else {
e498be7d 4161 res = do_tune_cpucache(cachep, limit,
83b519e8
PE
4162 batchcount, shared,
4163 GFP_KERNEL);
1da177e4
LT
4164 }
4165 break;
4166 }
4167 }
18004c5d 4168 mutex_unlock(&slab_mutex);
1da177e4
LT
4169 if (res >= 0)
4170 res = count;
4171 return res;
4172}
871751e2
AV
4173
4174#ifdef CONFIG_DEBUG_SLAB_LEAK
4175
4176static void *leaks_start(struct seq_file *m, loff_t *pos)
4177{
18004c5d
CL
4178 mutex_lock(&slab_mutex);
4179 return seq_list_start(&slab_caches, *pos);
871751e2
AV
4180}
4181
4182static inline int add_caller(unsigned long *n, unsigned long v)
4183{
4184 unsigned long *p;
4185 int l;
4186 if (!v)
4187 return 1;
4188 l = n[1];
4189 p = n + 2;
4190 while (l) {
4191 int i = l/2;
4192 unsigned long *q = p + 2 * i;
4193 if (*q == v) {
4194 q[1]++;
4195 return 1;
4196 }
4197 if (*q > v) {
4198 l = i;
4199 } else {
4200 p = q + 2;
4201 l -= i + 1;
4202 }
4203 }
4204 if (++n[1] == n[0])
4205 return 0;
4206 memmove(p + 2, p, n[1] * 2 * sizeof(unsigned long) - ((void *)p - (void *)n));
4207 p[0] = v;
4208 p[1] = 1;
4209 return 1;
4210}
4211
8456a648
JK
4212static void handle_slab(unsigned long *n, struct kmem_cache *c,
4213 struct page *page)
871751e2
AV
4214{
4215 void *p;
b1cb0982
JK
4216 int i, j;
4217
871751e2
AV
4218 if (n[0] == n[1])
4219 return;
8456a648 4220 for (i = 0, p = page->s_mem; i < c->num; i++, p += c->size) {
b1cb0982
JK
4221 bool active = true;
4222
8456a648 4223 for (j = page->active; j < c->num; j++) {
b1cb0982 4224 /* Skip freed item */
e5c58dfd 4225 if (get_free_obj(page, j) == i) {
b1cb0982
JK
4226 active = false;
4227 break;
4228 }
4229 }
4230 if (!active)
871751e2 4231 continue;
b1cb0982 4232
871751e2
AV
4233 if (!add_caller(n, (unsigned long)*dbg_userword(c, p)))
4234 return;
4235 }
4236}
4237
4238static void show_symbol(struct seq_file *m, unsigned long address)
4239{
4240#ifdef CONFIG_KALLSYMS
871751e2 4241 unsigned long offset, size;
9281acea 4242 char modname[MODULE_NAME_LEN], name[KSYM_NAME_LEN];
871751e2 4243
a5c43dae 4244 if (lookup_symbol_attrs(address, &size, &offset, modname, name) == 0) {
871751e2 4245 seq_printf(m, "%s+%#lx/%#lx", name, offset, size);
a5c43dae 4246 if (modname[0])
871751e2
AV
4247 seq_printf(m, " [%s]", modname);
4248 return;
4249 }
4250#endif
4251 seq_printf(m, "%p", (void *)address);
4252}
4253
4254static int leaks_show(struct seq_file *m, void *p)
4255{
0672aa7c 4256 struct kmem_cache *cachep = list_entry(p, struct kmem_cache, list);
8456a648 4257 struct page *page;
ce8eb6c4 4258 struct kmem_cache_node *n;
871751e2 4259 const char *name;
db845067 4260 unsigned long *x = m->private;
871751e2
AV
4261 int node;
4262 int i;
4263
4264 if (!(cachep->flags & SLAB_STORE_USER))
4265 return 0;
4266 if (!(cachep->flags & SLAB_RED_ZONE))
4267 return 0;
4268
4269 /* OK, we can do it */
4270
db845067 4271 x[1] = 0;
871751e2
AV
4272
4273 for_each_online_node(node) {
ce8eb6c4
CL
4274 n = cachep->node[node];
4275 if (!n)
871751e2
AV
4276 continue;
4277
4278 check_irq_on();
ce8eb6c4 4279 spin_lock_irq(&n->list_lock);
871751e2 4280
8456a648
JK
4281 list_for_each_entry(page, &n->slabs_full, lru)
4282 handle_slab(x, cachep, page);
4283 list_for_each_entry(page, &n->slabs_partial, lru)
4284 handle_slab(x, cachep, page);
ce8eb6c4 4285 spin_unlock_irq(&n->list_lock);
871751e2
AV
4286 }
4287 name = cachep->name;
db845067 4288 if (x[0] == x[1]) {
871751e2 4289 /* Increase the buffer size */
18004c5d 4290 mutex_unlock(&slab_mutex);
db845067 4291 m->private = kzalloc(x[0] * 4 * sizeof(unsigned long), GFP_KERNEL);
871751e2
AV
4292 if (!m->private) {
4293 /* Too bad, we are really out */
db845067 4294 m->private = x;
18004c5d 4295 mutex_lock(&slab_mutex);
871751e2
AV
4296 return -ENOMEM;
4297 }
db845067
CL
4298 *(unsigned long *)m->private = x[0] * 2;
4299 kfree(x);
18004c5d 4300 mutex_lock(&slab_mutex);
871751e2
AV
4301 /* Now make sure this entry will be retried */
4302 m->count = m->size;
4303 return 0;
4304 }
db845067
CL
4305 for (i = 0; i < x[1]; i++) {
4306 seq_printf(m, "%s: %lu ", name, x[2*i+3]);
4307 show_symbol(m, x[2*i+2]);
871751e2
AV
4308 seq_putc(m, '\n');
4309 }
d2e7b7d0 4310
871751e2
AV
4311 return 0;
4312}
4313
a0ec95a8 4314static const struct seq_operations slabstats_op = {
871751e2 4315 .start = leaks_start,
276a2439
WL
4316 .next = slab_next,
4317 .stop = slab_stop,
871751e2
AV
4318 .show = leaks_show,
4319};
a0ec95a8
AD
4320
4321static int slabstats_open(struct inode *inode, struct file *file)
4322{
4323 unsigned long *n = kzalloc(PAGE_SIZE, GFP_KERNEL);
4324 int ret = -ENOMEM;
4325 if (n) {
4326 ret = seq_open(file, &slabstats_op);
4327 if (!ret) {
4328 struct seq_file *m = file->private_data;
4329 *n = PAGE_SIZE / (2 * sizeof(unsigned long));
4330 m->private = n;
4331 n = NULL;
4332 }
4333 kfree(n);
4334 }
4335 return ret;
4336}
4337
4338static const struct file_operations proc_slabstats_operations = {
4339 .open = slabstats_open,
4340 .read = seq_read,
4341 .llseek = seq_lseek,
4342 .release = seq_release_private,
4343};
4344#endif
4345
4346static int __init slab_proc_init(void)
4347{
4348#ifdef CONFIG_DEBUG_SLAB_LEAK
4349 proc_create("slab_allocators", 0, NULL, &proc_slabstats_operations);
871751e2 4350#endif
a0ec95a8
AD
4351 return 0;
4352}
4353module_init(slab_proc_init);
1da177e4
LT
4354#endif
4355
00e145b6
MS
4356/**
4357 * ksize - get the actual amount of memory allocated for a given object
4358 * @objp: Pointer to the object
4359 *
4360 * kmalloc may internally round up allocations and return more memory
4361 * than requested. ksize() can be used to determine the actual amount of
4362 * memory allocated. The caller may use this additional memory, even though
4363 * a smaller amount of memory was initially specified with the kmalloc call.
4364 * The caller must guarantee that objp points to a valid object previously
4365 * allocated with either kmalloc() or kmem_cache_alloc(). The object
4366 * must not be freed during the duration of the call.
4367 */
fd76bab2 4368size_t ksize(const void *objp)
1da177e4 4369{
ef8b4520
CL
4370 BUG_ON(!objp);
4371 if (unlikely(objp == ZERO_SIZE_PTR))
00e145b6 4372 return 0;
1da177e4 4373
8c138bc0 4374 return virt_to_cache(objp)->object_size;
1da177e4 4375}
b1aabecd 4376EXPORT_SYMBOL(ksize);
This page took 1.366844 seconds and 5 git commands to generate.