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