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