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