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