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