Blame view

mm/slab.c 114 KB
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
  /*
   * linux/mm/slab.c
   * Written by Mark Hemment, 1996/97.
   * (markhe@nextd.demon.co.uk)
   *
   * kmem_cache_destroy() + some cleanup - 1999 Andrea Arcangeli
   *
   * Major cleanup, different bufctl logic, per-cpu arrays
   *	(c) 2000 Manfred Spraul
   *
   * Cleanup, make the head arrays unconditional, preparation for NUMA
   * 	(c) 2002 Manfred Spraul
   *
   * An implementation of the Slab Allocator as described in outline in;
   *	UNIX Internals: The New Frontiers by Uresh Vahalia
   *	Pub: Prentice Hall	ISBN 0-13-101908-2
   * or with a little more detail in;
   *	The Slab Allocator: An Object-Caching Kernel Memory Allocator
   *	Jeff Bonwick (Sun Microsystems).
   *	Presented at: USENIX Summer 1994 Technical Conference
   *
   * The memory is organized in caches, one cache for each object type.
   * (e.g. inode_cache, dentry_cache, buffer_head, vm_area_struct)
   * Each cache consists out of many slabs (they are small (usually one
   * page long) and always contiguous), and each slab contains multiple
   * initialized objects.
   *
   * This means, that your constructor is used only for newly allocated
183ff22bb   Simon Arlott   spelling fixes: mm/
29
   * slabs and you must pass objects with the same initializations to
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
   * kmem_cache_free.
   *
   * Each cache can only support one memory type (GFP_DMA, GFP_HIGHMEM,
   * normal). If you need a special memory type, then must create a new
   * cache for that memory type.
   *
   * In order to reduce fragmentation, the slabs are sorted in 3 groups:
   *   full slabs with 0 free objects
   *   partial slabs
   *   empty slabs with no allocated objects
   *
   * If partial slabs exist, then new allocations come from these slabs,
   * otherwise from empty slabs or new slabs are allocated.
   *
   * kmem_cache_destroy() CAN CRASH if you try to allocate from the cache
   * during kmem_cache_destroy(). The caller must prevent concurrent allocs.
   *
   * Each cache has a short per-cpu head array, most allocs
   * and frees go into that array, and if that array overflows, then 1/2
   * of the entries in the array are given back into the global cache.
   * The head array is strictly LIFO and should improve the cache hit rates.
   * On SMP, it additionally reduces the spinlock operations.
   *
a737b3e2f   Andrew Morton   [PATCH] slab cleanup
53
   * The c_cpuarray may not be read with enabled local interrupts -
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
54
55
56
57
   * it's changed with a smp_call_function().
   *
   * SMP synchronization:
   *  constructors and destructors are called without any locking.
343e0d7a9   Pekka Enberg   [PATCH] slab: rep...
58
   *  Several members in struct kmem_cache and struct slab never change, they
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
59
60
61
62
63
64
65
66
67
68
69
70
   *	are accessed without any locking.
   *  The per-cpu arrays are never accessed from the wrong cpu, no locking,
   *  	and local interrupts are disabled so slab code is preempt-safe.
   *  The non-constant members are protected with a per-cache irq spinlock.
   *
   * Many thanks to Mark Hemment, who wrote another per-cpu slab patch
   * in 2000 - many ideas in the current implementation are derived from
   * his patch.
   *
   * Further notes from the original documentation:
   *
   * 11 April '97.  Started multi-threading - markhe
18004c5d4   Christoph Lameter   mm, sl[aou]b: Use...
71
   *	The global cache-chain is protected by the mutex 'slab_mutex'.
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
72
73
74
75
76
77
   *	The sem is only needed when accessing/extending the cache-chain, which
   *	can never happen inside an interrupt (kmem_cache_create(),
   *	kmem_cache_shrink() and kmem_cache_reap()).
   *
   *	At present, each engine can be growing a cache.  This should be blocked.
   *
e498be7da   Christoph Lameter   [PATCH] Numa-awar...
78
79
80
81
82
83
84
85
86
   * 15 March 2005. NUMA slab allocator.
   *	Shai Fultheim <shai@scalex86.org>.
   *	Shobhit Dayal <shobhit@calsoftinc.com>
   *	Alok N Kataria <alokk@calsoftinc.com>
   *	Christoph Lameter <christoph@lameter.com>
   *
   *	Modified the slab allocator to be node aware on NUMA systems.
   *	Each node has its own list of partial, free and full slabs.
   *	All object allocations for a node occur from node specific slab lists.
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
87
   */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
88
89
  #include	<linux/slab.h>
  #include	<linux/mm.h>
c9cf55285   Randy Dunlap   [PATCH] add poiso...
90
  #include	<linux/poison.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
91
92
93
94
95
  #include	<linux/swap.h>
  #include	<linux/cache.h>
  #include	<linux/interrupt.h>
  #include	<linux/init.h>
  #include	<linux/compiler.h>
101a50019   Paul Jackson   [PATCH] cpuset me...
96
  #include	<linux/cpuset.h>
a0ec95a8e   Alexey Dobriyan   proc: move /proc/...
97
  #include	<linux/proc_fs.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
98
99
100
101
102
103
104
  #include	<linux/seq_file.h>
  #include	<linux/notifier.h>
  #include	<linux/kallsyms.h>
  #include	<linux/cpu.h>
  #include	<linux/sysctl.h>
  #include	<linux/module.h>
  #include	<linux/rcupdate.h>
543537bd9   Paulo Marques   [PATCH] create a ...
105
  #include	<linux/string.h>
138ae6631   Andrew Morton   [PATCH] slab: use...
106
  #include	<linux/uaccess.h>
e498be7da   Christoph Lameter   [PATCH] Numa-awar...
107
  #include	<linux/nodemask.h>
d5cff6352   Catalin Marinas   kmemleak: Add the...
108
  #include	<linux/kmemleak.h>
dc85da15d   Christoph Lameter   [PATCH] NUMA poli...
109
  #include	<linux/mempolicy.h>
fc0abb145   Ingo Molnar   [PATCH] sem2mutex...
110
  #include	<linux/mutex.h>
8a8b6502f   Akinobu Mita   [PATCH] fault-inj...
111
  #include	<linux/fault-inject.h>
e7eebaf6a   Ingo Molnar   [PATCH] pi-futex:...
112
  #include	<linux/rtmutex.h>
6a2d7a955   Eric Dumazet   [PATCH] SLAB: use...
113
  #include	<linux/reciprocal_div.h>
3ac7fe5a4   Thomas Gleixner   infrastructure to...
114
  #include	<linux/debugobjects.h>
c175eea46   Pekka Enberg   slab: add hooks f...
115
  #include	<linux/kmemcheck.h>
8f9f8d9e8   David Rientjes   slab: add memory ...
116
  #include	<linux/memory.h>
268bb0ce3   Linus Torvalds   sanitize <linux/p...
117
  #include	<linux/prefetch.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
118

381760ead   Mel Gorman   mm: micro-optimis...
119
  #include	<net/sock.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
120
121
122
  #include	<asm/cacheflush.h>
  #include	<asm/tlbflush.h>
  #include	<asm/page.h>
4dee6b64e   Steven Rostedt   tracing/mm: Move ...
123
  #include <trace/events/kmem.h>
072bb0aa5   Mel Gorman   mm: sl[au]b: add ...
124
  #include	"internal.h"
b9ce5ef49   Glauber Costa   sl[au]b: always g...
125
  #include	"slab.h"
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
126
  /*
50953fe9e   Christoph Lameter   slab allocators: ...
127
   * DEBUG	- 1 for kmem_cache_create() to honour; SLAB_RED_ZONE & SLAB_POISON.
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
   *		  0 for faster, smaller code (especially in the critical paths).
   *
   * STATS	- 1 to collect stats for /proc/slabinfo.
   *		  0 for faster, smaller code (especially in the critical paths).
   *
   * FORCED_DEBUG	- 1 enables SLAB_RED_ZONE and SLAB_POISON (if possible)
   */
  
  #ifdef CONFIG_DEBUG_SLAB
  #define	DEBUG		1
  #define	STATS		1
  #define	FORCED_DEBUG	1
  #else
  #define	DEBUG		0
  #define	STATS		0
  #define	FORCED_DEBUG	0
  #endif
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
145
146
  /* Shouldn't this be in a header file somewhere? */
  #define	BYTES_PER_WORD		sizeof(void *)
87a927c71   David Woodhouse   Fix slab redzone ...
147
  #define	REDZONE_ALIGN		max(BYTES_PER_WORD, __alignof__(unsigned long long))
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
148

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
149
150
151
  #ifndef ARCH_KMALLOC_FLAGS
  #define ARCH_KMALLOC_FLAGS SLAB_HWCACHE_ALIGN
  #endif
072bb0aa5   Mel Gorman   mm: sl[au]b: add ...
152
153
154
155
156
  /*
   * true if a page was allocated from pfmemalloc reserves for network-based
   * swap
   */
  static bool pfmemalloc_active __read_mostly;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
  /*
   * kmem_bufctl_t:
   *
   * Bufctl's are used for linking objs within a slab
   * linked offsets.
   *
   * This implementation relies on "struct page" for locating the cache &
   * slab an object belongs to.
   * This allows the bufctl structure to be small (one int), but limits
   * the number of objects a slab (not a cache) can contain when off-slab
   * bufctls are used. The limit is the size of the largest general cache
   * that does not use off-slab slabs.
   * For 32bit archs with 4 kB pages, is this 56.
   * This is not serious, as it is only for large objects, when it is unwise
   * to have too many per slab.
   * Note: This limit can be raised by introducing a general cache whose size
   * is less than 512 (PAGE_SIZE<<3), but greater than 256.
   */
fa5b08d5f   Kyle Moffett   [PATCH] sab: cons...
175
  typedef unsigned int kmem_bufctl_t;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
176
177
  #define BUFCTL_END	(((kmem_bufctl_t)(~0U))-0)
  #define BUFCTL_FREE	(((kmem_bufctl_t)(~0U))-1)
871751e25   Al Viro   [PATCH] slab: imp...
178
179
  #define	BUFCTL_ACTIVE	(((kmem_bufctl_t)(~0U))-2)
  #define	SLAB_LIMIT	(((kmem_bufctl_t)(~0U))-3)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
180

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
181
  /*
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
182
183
184
185
186
187
188
189
190
191
192
193
   * struct slab_rcu
   *
   * slab_destroy on a SLAB_DESTROY_BY_RCU cache uses this structure to
   * arrange for kmem_freepages to be called via RCU.  This is useful if
   * we need to approach a kernel structure obliquely, from its address
   * obtained without the usual locking.  We can lock the structure to
   * stabilize it and check it's still at the given address, only if we
   * can be sure that the memory has not been meanwhile reused for some
   * other kind of object (which our subsystem's lock might corrupt).
   *
   * rcu_read_lock before reading the address, then rcu_read_unlock after
   * taking the spinlock within the structure expected at that address.
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
194
195
   */
  struct slab_rcu {
b28a02de8   Pekka Enberg   [PATCH] slab: fix...
196
  	struct rcu_head head;
343e0d7a9   Pekka Enberg   [PATCH] slab: rep...
197
  	struct kmem_cache *cachep;
b28a02de8   Pekka Enberg   [PATCH] slab: fix...
198
  	void *addr;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
199
200
201
  };
  
  /*
5bfe53a77   Lai Jiangshan   slab,rcu: don't a...
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
   * struct slab
   *
   * Manages the objs in a slab. Placed either at the beginning of mem allocated
   * for a slab, or allocated from an general cache.
   * Slabs are chained into three list: fully used, partial, fully free slabs.
   */
  struct slab {
  	union {
  		struct {
  			struct list_head list;
  			unsigned long colouroff;
  			void *s_mem;		/* including colour offset */
  			unsigned int inuse;	/* num of objs active in slab */
  			kmem_bufctl_t free;
  			unsigned short nodeid;
  		};
  		struct slab_rcu __slab_cover_slab_rcu;
  	};
  };
  
  /*
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
223
224
   * struct array_cache
   *
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
225
226
227
228
229
230
231
232
233
234
235
236
237
238
   * Purpose:
   * - LIFO ordering, to hand out cache-warm objects from _alloc
   * - reduce the number of linked list operations
   * - reduce spinlock operations
   *
   * The limit is stored in the per-cpu structure to reduce the data cache
   * footprint.
   *
   */
  struct array_cache {
  	unsigned int avail;
  	unsigned int limit;
  	unsigned int batchcount;
  	unsigned int touched;
e498be7da   Christoph Lameter   [PATCH] Numa-awar...
239
  	spinlock_t lock;
bda5b655f   Robert P. J. Day   Delete gcc-2.95 c...
240
  	void *entry[];	/*
a737b3e2f   Andrew Morton   [PATCH] slab cleanup
241
242
243
  			 * Must have this definition in here for the proper
  			 * alignment of array_cache. Also simplifies accessing
  			 * the entries.
072bb0aa5   Mel Gorman   mm: sl[au]b: add ...
244
245
246
247
  			 *
  			 * Entries should not be directly dereferenced as
  			 * entries belonging to slabs marked pfmemalloc will
  			 * have the lower bits set SLAB_OBJ_PFMEMALLOC
a737b3e2f   Andrew Morton   [PATCH] slab cleanup
248
  			 */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
249
  };
072bb0aa5   Mel Gorman   mm: sl[au]b: add ...
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
  #define SLAB_OBJ_PFMEMALLOC	1
  static inline bool is_obj_pfmemalloc(void *objp)
  {
  	return (unsigned long)objp & SLAB_OBJ_PFMEMALLOC;
  }
  
  static inline void set_obj_pfmemalloc(void **objp)
  {
  	*objp = (void *)((unsigned long)*objp | SLAB_OBJ_PFMEMALLOC);
  	return;
  }
  
  static inline void clear_obj_pfmemalloc(void **objp)
  {
  	*objp = (void *)((unsigned long)*objp & ~SLAB_OBJ_PFMEMALLOC);
  }
a737b3e2f   Andrew Morton   [PATCH] slab cleanup
266
267
268
  /*
   * bootstrap: The caches do not work without cpuarrays anymore, but the
   * cpuarrays are allocated from the generic caches...
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
269
270
271
272
   */
  #define BOOT_CPUCACHE_ENTRIES	1
  struct arraycache_init {
  	struct array_cache cache;
b28a02de8   Pekka Enberg   [PATCH] slab: fix...
273
  	void *entries[BOOT_CPUCACHE_ENTRIES];
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
274
275
276
  };
  
  /*
e498be7da   Christoph Lameter   [PATCH] Numa-awar...
277
278
   * Need this for bootstrapping a per node allocator.
   */
556a169da   Pekka Enberg   slab: fix bootstr...
279
  #define NUM_INIT_LISTS (3 * MAX_NUMNODES)
ce8eb6c42   Christoph Lameter   slab: Rename list...
280
  static struct kmem_cache_node __initdata init_kmem_cache_node[NUM_INIT_LISTS];
e498be7da   Christoph Lameter   [PATCH] Numa-awar...
281
  #define	CACHE_CACHE 0
556a169da   Pekka Enberg   slab: fix bootstr...
282
  #define	SIZE_AC MAX_NUMNODES
ce8eb6c42   Christoph Lameter   slab: Rename list...
283
  #define	SIZE_NODE (2 * MAX_NUMNODES)
e498be7da   Christoph Lameter   [PATCH] Numa-awar...
284

ed11d9eb2   Christoph Lameter   [PATCH] slab: con...
285
  static int drain_freelist(struct kmem_cache *cache,
ce8eb6c42   Christoph Lameter   slab: Rename list...
286
  			struct kmem_cache_node *n, int tofree);
ed11d9eb2   Christoph Lameter   [PATCH] slab: con...
287
288
  static void free_block(struct kmem_cache *cachep, void **objpp, int len,
  			int node);
83b519e8b   Pekka Enberg   slab: setup alloc...
289
  static int enable_cpucache(struct kmem_cache *cachep, gfp_t gfp);
65f27f384   David Howells   WorkStruct: Pass ...
290
  static void cache_reap(struct work_struct *unused);
ed11d9eb2   Christoph Lameter   [PATCH] slab: con...
291

e0a427267   Ingo Molnar   [PATCH] mm/slab.c...
292
  static int slab_early_init = 1;
e33660165   Christoph Lameter   slab: Use common ...
293
  #define INDEX_AC kmalloc_index(sizeof(struct arraycache_init))
ce8eb6c42   Christoph Lameter   slab: Rename list...
294
  #define INDEX_NODE kmalloc_index(sizeof(struct kmem_cache_node))
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
295

ce8eb6c42   Christoph Lameter   slab: Rename list...
296
  static void kmem_cache_node_init(struct kmem_cache_node *parent)
e498be7da   Christoph Lameter   [PATCH] Numa-awar...
297
298
299
300
301
302
  {
  	INIT_LIST_HEAD(&parent->slabs_full);
  	INIT_LIST_HEAD(&parent->slabs_partial);
  	INIT_LIST_HEAD(&parent->slabs_free);
  	parent->shared = NULL;
  	parent->alien = NULL;
2e1217cf9   Ravikiran G Thirumalai   [PATCH] NUMA slab...
303
  	parent->colour_next = 0;
e498be7da   Christoph Lameter   [PATCH] Numa-awar...
304
305
306
307
  	spin_lock_init(&parent->list_lock);
  	parent->free_objects = 0;
  	parent->free_touched = 0;
  }
a737b3e2f   Andrew Morton   [PATCH] slab cleanup
308
309
310
  #define MAKE_LIST(cachep, listp, slab, nodeid)				\
  	do {								\
  		INIT_LIST_HEAD(listp);					\
6a67368c3   Christoph Lameter   slab: Rename node...
311
  		list_splice(&(cachep->node[nodeid]->slab), listp);	\
e498be7da   Christoph Lameter   [PATCH] Numa-awar...
312
  	} while (0)
a737b3e2f   Andrew Morton   [PATCH] slab cleanup
313
314
  #define	MAKE_ALL_LISTS(cachep, ptr, nodeid)				\
  	do {								\
e498be7da   Christoph Lameter   [PATCH] Numa-awar...
315
316
317
318
  	MAKE_LIST((cachep), (&(ptr)->slabs_full), slabs_full, nodeid);	\
  	MAKE_LIST((cachep), (&(ptr)->slabs_partial), slabs_partial, nodeid); \
  	MAKE_LIST((cachep), (&(ptr)->slabs_free), slabs_free, nodeid);	\
  	} while (0)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
319

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
320
321
322
323
  #define CFLGS_OFF_SLAB		(0x80000000UL)
  #define	OFF_SLAB(x)	((x)->flags & CFLGS_OFF_SLAB)
  
  #define BATCHREFILL_LIMIT	16
a737b3e2f   Andrew Morton   [PATCH] slab cleanup
324
325
326
  /*
   * Optimization question: fewer reaps means less probability for unnessary
   * cpucache drain/refill cycles.
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
327
   *
dc6f3f276   Adrian Bunk   mm/slab.c: fix a ...
328
   * OTOH the cpuarrays can contain lots of objects,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
329
330
331
332
333
334
335
336
337
338
   * which could lock up otherwise freeable slabs.
   */
  #define REAPTIMEOUT_CPUC	(2*HZ)
  #define REAPTIMEOUT_LIST3	(4*HZ)
  
  #if STATS
  #define	STATS_INC_ACTIVE(x)	((x)->num_active++)
  #define	STATS_DEC_ACTIVE(x)	((x)->num_active--)
  #define	STATS_INC_ALLOCED(x)	((x)->num_allocations++)
  #define	STATS_INC_GROWN(x)	((x)->grown++)
ed11d9eb2   Christoph Lameter   [PATCH] slab: con...
339
  #define	STATS_ADD_REAPED(x,y)	((x)->reaped += (y))
a737b3e2f   Andrew Morton   [PATCH] slab cleanup
340
341
342
343
344
  #define	STATS_SET_HIGH(x)						\
  	do {								\
  		if ((x)->num_active > (x)->high_mark)			\
  			(x)->high_mark = (x)->num_active;		\
  	} while (0)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
345
346
  #define	STATS_INC_ERR(x)	((x)->errors++)
  #define	STATS_INC_NODEALLOCS(x)	((x)->node_allocs++)
e498be7da   Christoph Lameter   [PATCH] Numa-awar...
347
  #define	STATS_INC_NODEFREES(x)	((x)->node_frees++)
fb7faf331   Ravikiran G Thirumalai   [PATCH] slab: add...
348
  #define STATS_INC_ACOVERFLOW(x)   ((x)->node_overflow++)
a737b3e2f   Andrew Morton   [PATCH] slab cleanup
349
350
351
352
353
  #define	STATS_SET_FREEABLE(x, i)					\
  	do {								\
  		if ((x)->max_freeable < i)				\
  			(x)->max_freeable = i;				\
  	} while (0)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
354
355
356
357
358
359
360
361
362
  #define STATS_INC_ALLOCHIT(x)	atomic_inc(&(x)->allochit)
  #define STATS_INC_ALLOCMISS(x)	atomic_inc(&(x)->allocmiss)
  #define STATS_INC_FREEHIT(x)	atomic_inc(&(x)->freehit)
  #define STATS_INC_FREEMISS(x)	atomic_inc(&(x)->freemiss)
  #else
  #define	STATS_INC_ACTIVE(x)	do { } while (0)
  #define	STATS_DEC_ACTIVE(x)	do { } while (0)
  #define	STATS_INC_ALLOCED(x)	do { } while (0)
  #define	STATS_INC_GROWN(x)	do { } while (0)
4e60c86bd   Andi Kleen   gcc-4.6: mm: fix ...
363
  #define	STATS_ADD_REAPED(x,y)	do { (void)(y); } while (0)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
364
365
366
  #define	STATS_SET_HIGH(x)	do { } while (0)
  #define	STATS_INC_ERR(x)	do { } while (0)
  #define	STATS_INC_NODEALLOCS(x)	do { } while (0)
e498be7da   Christoph Lameter   [PATCH] Numa-awar...
367
  #define	STATS_INC_NODEFREES(x)	do { } while (0)
fb7faf331   Ravikiran G Thirumalai   [PATCH] slab: add...
368
  #define STATS_INC_ACOVERFLOW(x)   do { } while (0)
a737b3e2f   Andrew Morton   [PATCH] slab cleanup
369
  #define	STATS_SET_FREEABLE(x, i) do { } while (0)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
370
371
372
373
374
375
376
  #define STATS_INC_ALLOCHIT(x)	do { } while (0)
  #define STATS_INC_ALLOCMISS(x)	do { } while (0)
  #define STATS_INC_FREEHIT(x)	do { } while (0)
  #define STATS_INC_FREEMISS(x)	do { } while (0)
  #endif
  
  #if DEBUG
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
377

a737b3e2f   Andrew Morton   [PATCH] slab cleanup
378
379
  /*
   * memory layout of objects:
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
380
   * 0		: objp
3dafccf22   Manfred Spraul   [PATCH] slab: dis...
381
   * 0 .. cachep->obj_offset - BYTES_PER_WORD - 1: padding. This ensures that
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
382
383
   * 		the end of an object is aligned with the end of the real
   * 		allocation. Catches writes behind the end of the allocation.
3dafccf22   Manfred Spraul   [PATCH] slab: dis...
384
   * cachep->obj_offset - BYTES_PER_WORD .. cachep->obj_offset - 1:
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
385
   * 		redzone word.
3dafccf22   Manfred Spraul   [PATCH] slab: dis...
386
   * cachep->obj_offset: The real object.
3b0efdfa1   Christoph Lameter   mm, sl[aou]b: Ext...
387
388
   * cachep->size - 2* BYTES_PER_WORD: redzone word [BYTES_PER_WORD long]
   * cachep->size - 1* BYTES_PER_WORD: last caller address
a737b3e2f   Andrew Morton   [PATCH] slab cleanup
389
   *					[BYTES_PER_WORD long]
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
390
   */
343e0d7a9   Pekka Enberg   [PATCH] slab: rep...
391
  static int obj_offset(struct kmem_cache *cachep)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
392
  {
3dafccf22   Manfred Spraul   [PATCH] slab: dis...
393
  	return cachep->obj_offset;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
394
  }
b46b8f19c   David Woodhouse   Increase slab red...
395
  static unsigned long long *dbg_redzone1(struct kmem_cache *cachep, void *objp)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
396
397
  {
  	BUG_ON(!(cachep->flags & SLAB_RED_ZONE));
b46b8f19c   David Woodhouse   Increase slab red...
398
399
  	return (unsigned long long*) (objp + obj_offset(cachep) -
  				      sizeof(unsigned long long));
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
400
  }
b46b8f19c   David Woodhouse   Increase slab red...
401
  static unsigned long long *dbg_redzone2(struct kmem_cache *cachep, void *objp)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
402
403
404
  {
  	BUG_ON(!(cachep->flags & SLAB_RED_ZONE));
  	if (cachep->flags & SLAB_STORE_USER)
3b0efdfa1   Christoph Lameter   mm, sl[aou]b: Ext...
405
  		return (unsigned long long *)(objp + cachep->size -
b46b8f19c   David Woodhouse   Increase slab red...
406
  					      sizeof(unsigned long long) -
87a927c71   David Woodhouse   Fix slab redzone ...
407
  					      REDZONE_ALIGN);
3b0efdfa1   Christoph Lameter   mm, sl[aou]b: Ext...
408
  	return (unsigned long long *) (objp + cachep->size -
b46b8f19c   David Woodhouse   Increase slab red...
409
  				       sizeof(unsigned long long));
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
410
  }
343e0d7a9   Pekka Enberg   [PATCH] slab: rep...
411
  static void **dbg_userword(struct kmem_cache *cachep, void *objp)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
412
413
  {
  	BUG_ON(!(cachep->flags & SLAB_STORE_USER));
3b0efdfa1   Christoph Lameter   mm, sl[aou]b: Ext...
414
  	return (void **)(objp + cachep->size - BYTES_PER_WORD);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
415
416
417
  }
  
  #else
3dafccf22   Manfred Spraul   [PATCH] slab: dis...
418
  #define obj_offset(x)			0
b46b8f19c   David Woodhouse   Increase slab red...
419
420
  #define dbg_redzone1(cachep, objp)	({BUG(); (unsigned long long *)NULL;})
  #define dbg_redzone2(cachep, objp)	({BUG(); (unsigned long long *)NULL;})
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
421
422
423
424
425
  #define dbg_userword(cachep, objp)	({BUG(); (void **)NULL;})
  
  #endif
  
  /*
3df1cccdf   David Rientjes   slab: introduce s...
426
427
   * Do not go above this order unless 0 objects fit into the slab or
   * overridden on the command line.
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
428
   */
543585cc5   David Rientjes   slab: rename slab...
429
430
431
  #define	SLAB_MAX_ORDER_HI	1
  #define	SLAB_MAX_ORDER_LO	0
  static int slab_max_order = SLAB_MAX_ORDER_LO;
3df1cccdf   David Rientjes   slab: introduce s...
432
  static bool slab_max_order_set __initdata;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
433

6ed5eb221   Pekka Enberg   [PATCH] slab: ext...
434
435
  static inline struct kmem_cache *virt_to_cache(const void *obj)
  {
b49af68ff   Christoph Lameter   Add virt_to_head_...
436
  	struct page *page = virt_to_head_page(obj);
350260889   Christoph Lameter   slab: Remove some...
437
  	return page->slab_cache;
6ed5eb221   Pekka Enberg   [PATCH] slab: ext...
438
439
440
441
  }
  
  static inline struct slab *virt_to_slab(const void *obj)
  {
b49af68ff   Christoph Lameter   Add virt_to_head_...
442
  	struct page *page = virt_to_head_page(obj);
350260889   Christoph Lameter   slab: Remove some...
443
444
445
  
  	VM_BUG_ON(!PageSlab(page));
  	return page->slab_page;
6ed5eb221   Pekka Enberg   [PATCH] slab: ext...
446
  }
8fea4e96a   Pekka Enberg   [PATCH] slab: obj...
447
448
449
  static inline void *index_to_obj(struct kmem_cache *cache, struct slab *slab,
  				 unsigned int idx)
  {
3b0efdfa1   Christoph Lameter   mm, sl[aou]b: Ext...
450
  	return slab->s_mem + cache->size * idx;
8fea4e96a   Pekka Enberg   [PATCH] slab: obj...
451
  }
6a2d7a955   Eric Dumazet   [PATCH] SLAB: use...
452
  /*
3b0efdfa1   Christoph Lameter   mm, sl[aou]b: Ext...
453
454
455
   * We want to avoid an expensive divide : (offset / cache->size)
   *   Using the fact that size is a constant for a particular cache,
   *   we can replace (offset / cache->size) by
6a2d7a955   Eric Dumazet   [PATCH] SLAB: use...
456
457
458
459
   *   reciprocal_divide(offset, cache->reciprocal_buffer_size)
   */
  static inline unsigned int obj_to_index(const struct kmem_cache *cache,
  					const struct slab *slab, void *obj)
8fea4e96a   Pekka Enberg   [PATCH] slab: obj...
460
  {
6a2d7a955   Eric Dumazet   [PATCH] SLAB: use...
461
462
  	u32 offset = (obj - slab->s_mem);
  	return reciprocal_divide(offset, cache->reciprocal_buffer_size);
8fea4e96a   Pekka Enberg   [PATCH] slab: obj...
463
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
464
  static struct arraycache_init initarray_generic =
b28a02de8   Pekka Enberg   [PATCH] slab: fix...
465
      { {0, BOOT_CPUCACHE_ENTRIES, 1, 0} };
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
466
467
  
  /* internal cache of cache description objs */
9b030cb86   Christoph Lameter   mm/sl[aou]b: Use ...
468
  static struct kmem_cache kmem_cache_boot = {
b28a02de8   Pekka Enberg   [PATCH] slab: fix...
469
470
471
  	.batchcount = 1,
  	.limit = BOOT_CPUCACHE_ENTRIES,
  	.shared = 1,
3b0efdfa1   Christoph Lameter   mm, sl[aou]b: Ext...
472
  	.size = sizeof(struct kmem_cache),
b28a02de8   Pekka Enberg   [PATCH] slab: fix...
473
  	.name = "kmem_cache",
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
474
  };
056c62418   Ravikiran G Thirumalai   [PATCH] slab: fix...
475
  #define BAD_ALIEN_MAGIC 0x01020304ul
f1aaee53f   Arjan van de Ven   [PATCH] lockdep: ...
476
477
478
479
480
481
482
483
  #ifdef CONFIG_LOCKDEP
  
  /*
   * Slab sometimes uses the kmalloc slabs to store the slab headers
   * for other slabs "off slab".
   * The locking for this is tricky in that it nests within the locks
   * of all other slabs in a few places; to deal with this special
   * locking we put on-slab caches into a separate lock-class.
056c62418   Ravikiran G Thirumalai   [PATCH] slab: fix...
484
485
486
487
   *
   * We set lock class for alien array caches which are up during init.
   * The lock annotation will be lost if all cpus of a node goes down and
   * then comes back up during hotplug
f1aaee53f   Arjan van de Ven   [PATCH] lockdep: ...
488
   */
056c62418   Ravikiran G Thirumalai   [PATCH] slab: fix...
489
490
  static struct lock_class_key on_slab_l3_key;
  static struct lock_class_key on_slab_alc_key;
83835b3d9   Peter Zijlstra   slab, lockdep: An...
491
492
493
494
495
496
497
498
  static struct lock_class_key debugobj_l3_key;
  static struct lock_class_key debugobj_alc_key;
  
  static void slab_set_lock_classes(struct kmem_cache *cachep,
  		struct lock_class_key *l3_key, struct lock_class_key *alc_key,
  		int q)
  {
  	struct array_cache **alc;
ce8eb6c42   Christoph Lameter   slab: Rename list...
499
  	struct kmem_cache_node *n;
83835b3d9   Peter Zijlstra   slab, lockdep: An...
500
  	int r;
ce8eb6c42   Christoph Lameter   slab: Rename list...
501
502
  	n = cachep->node[q];
  	if (!n)
83835b3d9   Peter Zijlstra   slab, lockdep: An...
503
  		return;
ce8eb6c42   Christoph Lameter   slab: Rename list...
504
505
  	lockdep_set_class(&n->list_lock, l3_key);
  	alc = n->alien;
83835b3d9   Peter Zijlstra   slab, lockdep: An...
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
  	/*
  	 * FIXME: This check for BAD_ALIEN_MAGIC
  	 * should go away when common slab code is taught to
  	 * work even without alien caches.
  	 * Currently, non NUMA code returns BAD_ALIEN_MAGIC
  	 * for alloc_alien_cache,
  	 */
  	if (!alc || (unsigned long)alc == BAD_ALIEN_MAGIC)
  		return;
  	for_each_node(r) {
  		if (alc[r])
  			lockdep_set_class(&alc[r]->lock, alc_key);
  	}
  }
  
  static void slab_set_debugobj_lock_classes_node(struct kmem_cache *cachep, int node)
  {
  	slab_set_lock_classes(cachep, &debugobj_l3_key, &debugobj_alc_key, node);
  }
  
  static void slab_set_debugobj_lock_classes(struct kmem_cache *cachep)
  {
  	int node;
  
  	for_each_online_node(node)
  		slab_set_debugobj_lock_classes_node(cachep, node);
  }
ce79ddc8e   Pekka Enberg   SLAB: Fix lockdep...
533
  static void init_node_lock_keys(int q)
f1aaee53f   Arjan van de Ven   [PATCH] lockdep: ...
534
  {
e33660165   Christoph Lameter   slab: Use common ...
535
  	int i;
056c62418   Ravikiran G Thirumalai   [PATCH] slab: fix...
536

97d066091   Christoph Lameter   mm, sl[aou]b: Com...
537
  	if (slab_state < UP)
ce79ddc8e   Pekka Enberg   SLAB: Fix lockdep...
538
  		return;
002b98a84   Christoph Lameter   slab: fix init_lo...
539
  	for (i = 1; i <= KMALLOC_SHIFT_HIGH; i++) {
ce8eb6c42   Christoph Lameter   slab: Rename list...
540
  		struct kmem_cache_node *n;
e33660165   Christoph Lameter   slab: Use common ...
541
542
543
544
  		struct kmem_cache *cache = kmalloc_caches[i];
  
  		if (!cache)
  			continue;
ce79ddc8e   Pekka Enberg   SLAB: Fix lockdep...
545

ce8eb6c42   Christoph Lameter   slab: Rename list...
546
547
  		n = cache->node[q];
  		if (!n || OFF_SLAB(cache))
00afa7580   Pekka Enberg   SLAB: Fix lockdep...
548
  			continue;
83835b3d9   Peter Zijlstra   slab, lockdep: An...
549

e33660165   Christoph Lameter   slab: Use common ...
550
  		slab_set_lock_classes(cache, &on_slab_l3_key,
83835b3d9   Peter Zijlstra   slab, lockdep: An...
551
  				&on_slab_alc_key, q);
f1aaee53f   Arjan van de Ven   [PATCH] lockdep: ...
552
553
  	}
  }
ce79ddc8e   Pekka Enberg   SLAB: Fix lockdep...
554

6ccfb5bcf   Glauber Costa   slab: annotate on...
555
556
  static void on_slab_lock_classes_node(struct kmem_cache *cachep, int q)
  {
6a67368c3   Christoph Lameter   slab: Rename node...
557
  	if (!cachep->node[q])
6ccfb5bcf   Glauber Costa   slab: annotate on...
558
559
560
561
562
563
564
565
566
567
568
569
570
571
  		return;
  
  	slab_set_lock_classes(cachep, &on_slab_l3_key,
  			&on_slab_alc_key, q);
  }
  
  static inline void on_slab_lock_classes(struct kmem_cache *cachep)
  {
  	int node;
  
  	VM_BUG_ON(OFF_SLAB(cachep));
  	for_each_node(node)
  		on_slab_lock_classes_node(cachep, node);
  }
ce79ddc8e   Pekka Enberg   SLAB: Fix lockdep...
572
573
574
575
576
577
578
  static inline void init_lock_keys(void)
  {
  	int node;
  
  	for_each_node(node)
  		init_node_lock_keys(node);
  }
f1aaee53f   Arjan van de Ven   [PATCH] lockdep: ...
579
  #else
ce79ddc8e   Pekka Enberg   SLAB: Fix lockdep...
580
581
582
  static void init_node_lock_keys(int q)
  {
  }
056c62418   Ravikiran G Thirumalai   [PATCH] slab: fix...
583
  static inline void init_lock_keys(void)
f1aaee53f   Arjan van de Ven   [PATCH] lockdep: ...
584
585
  {
  }
83835b3d9   Peter Zijlstra   slab, lockdep: An...
586

6ccfb5bcf   Glauber Costa   slab: annotate on...
587
588
589
590
591
592
593
  static inline void on_slab_lock_classes(struct kmem_cache *cachep)
  {
  }
  
  static inline void on_slab_lock_classes_node(struct kmem_cache *cachep, int node)
  {
  }
83835b3d9   Peter Zijlstra   slab, lockdep: An...
594
595
596
597
598
599
600
  static void slab_set_debugobj_lock_classes_node(struct kmem_cache *cachep, int node)
  {
  }
  
  static void slab_set_debugobj_lock_classes(struct kmem_cache *cachep)
  {
  }
f1aaee53f   Arjan van de Ven   [PATCH] lockdep: ...
601
  #endif
1871e52c7   Tejun Heo   percpu: make perc...
602
  static DEFINE_PER_CPU(struct delayed_work, slab_reap_work);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
603

343e0d7a9   Pekka Enberg   [PATCH] slab: rep...
604
  static inline struct array_cache *cpu_cache_get(struct kmem_cache *cachep)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
605
606
607
  {
  	return cachep->array[smp_processor_id()];
  }
fbaccacff   Steven Rostedt   [PATCH] slab: cac...
608
  static size_t slab_mgmt_size(size_t nr_objs, size_t align)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
609
  {
fbaccacff   Steven Rostedt   [PATCH] slab: cac...
610
611
  	return ALIGN(sizeof(struct slab)+nr_objs*sizeof(kmem_bufctl_t), align);
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
612

a737b3e2f   Andrew Morton   [PATCH] slab cleanup
613
614
615
  /*
   * Calculate the number of objects and left-over bytes for a given buffer size.
   */
fbaccacff   Steven Rostedt   [PATCH] slab: cac...
616
617
618
619
620
621
622
  static void cache_estimate(unsigned long gfporder, size_t buffer_size,
  			   size_t align, int flags, size_t *left_over,
  			   unsigned int *num)
  {
  	int nr_objs;
  	size_t mgmt_size;
  	size_t slab_size = PAGE_SIZE << gfporder;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
623

fbaccacff   Steven Rostedt   [PATCH] slab: cac...
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
  	/*
  	 * The slab management structure can be either off the slab or
  	 * on it. For the latter case, the memory allocated for a
  	 * slab is used for:
  	 *
  	 * - The struct slab
  	 * - One kmem_bufctl_t for each object
  	 * - Padding to respect alignment of @align
  	 * - @buffer_size bytes for each object
  	 *
  	 * If the slab management structure is off the slab, then the
  	 * alignment will already be calculated into the size. Because
  	 * the slabs are all pages aligned, the objects will be at the
  	 * correct alignment when allocated.
  	 */
  	if (flags & CFLGS_OFF_SLAB) {
  		mgmt_size = 0;
  		nr_objs = slab_size / buffer_size;
  
  		if (nr_objs > SLAB_LIMIT)
  			nr_objs = SLAB_LIMIT;
  	} else {
  		/*
  		 * Ignore padding for the initial guess. The padding
  		 * is at most @align-1 bytes, and @buffer_size is at
  		 * least @align. In the worst case, this result will
  		 * be one greater than the number of objects that fit
  		 * into the memory allocation when taking the padding
  		 * into account.
  		 */
  		nr_objs = (slab_size - sizeof(struct slab)) /
  			  (buffer_size + sizeof(kmem_bufctl_t));
  
  		/*
  		 * This calculated number will be either the right
  		 * amount, or one greater than what we want.
  		 */
  		if (slab_mgmt_size(nr_objs, align) + nr_objs*buffer_size
  		       > slab_size)
  			nr_objs--;
  
  		if (nr_objs > SLAB_LIMIT)
  			nr_objs = SLAB_LIMIT;
  
  		mgmt_size = slab_mgmt_size(nr_objs, align);
  	}
  	*num = nr_objs;
  	*left_over = slab_size - nr_objs*buffer_size - mgmt_size;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
672
  }
f28510d30   Christoph Lameter   slab: Only define...
673
  #if DEBUG
d40cee245   Harvey Harrison   mm: remove remain...
674
  #define slab_error(cachep, msg) __slab_error(__func__, cachep, msg)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
675

a737b3e2f   Andrew Morton   [PATCH] slab cleanup
676
677
  static void __slab_error(const char *function, struct kmem_cache *cachep,
  			char *msg)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
678
679
680
  {
  	printk(KERN_ERR "slab error in %s(): cache `%s': %s
  ",
b28a02de8   Pekka Enberg   [PATCH] slab: fix...
681
  	       function, cachep->name, msg);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
682
  	dump_stack();
373d4d099   Rusty Russell   taint: add explic...
683
  	add_taint(TAINT_BAD_PAGE, LOCKDEP_NOW_UNRELIABLE);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
684
  }
f28510d30   Christoph Lameter   slab: Only define...
685
  #endif
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
686

3395ee058   Paul Menage   [PATCH] mm: add n...
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
  /*
   * By default on NUMA we use alien caches to stage the freeing of
   * objects allocated from other nodes. This causes massive memory
   * inefficiencies when using fake NUMA setup to split memory into a
   * large number of small nodes, so it can be disabled on the command
   * line
    */
  
  static int use_alien_caches __read_mostly = 1;
  static int __init noaliencache_setup(char *s)
  {
  	use_alien_caches = 0;
  	return 1;
  }
  __setup("noaliencache", noaliencache_setup);
3df1cccdf   David Rientjes   slab: introduce s...
702
703
704
705
706
707
708
709
710
711
  static int __init slab_max_order_setup(char *str)
  {
  	get_option(&str, &slab_max_order);
  	slab_max_order = slab_max_order < 0 ? 0 :
  				min(slab_max_order, MAX_ORDER - 1);
  	slab_max_order_set = true;
  
  	return 1;
  }
  __setup("slab_max_order=", slab_max_order_setup);
8fce4d8e3   Christoph Lameter   [PATCH] slab: Nod...
712
713
714
715
716
717
718
  #ifdef CONFIG_NUMA
  /*
   * Special reaping functions for NUMA systems called from cache_reap().
   * These take care of doing round robin flushing of alien caches (containing
   * objects freed on different nodes from which they were allocated) and the
   * flushing of remote pcps by calling drain_node_pages.
   */
1871e52c7   Tejun Heo   percpu: make perc...
719
  static DEFINE_PER_CPU(unsigned long, slab_reap_node);
8fce4d8e3   Christoph Lameter   [PATCH] slab: Nod...
720
721
722
723
  
  static void init_reap_node(int cpu)
  {
  	int node;
7d6e6d09d   Lee Schermerhorn   numa: slab: use n...
724
  	node = next_node(cpu_to_mem(cpu), node_online_map);
8fce4d8e3   Christoph Lameter   [PATCH] slab: Nod...
725
  	if (node == MAX_NUMNODES)
442295c94   Paul Jackson   [PATCH] mm: slab ...
726
  		node = first_node(node_online_map);
8fce4d8e3   Christoph Lameter   [PATCH] slab: Nod...
727

1871e52c7   Tejun Heo   percpu: make perc...
728
  	per_cpu(slab_reap_node, cpu) = node;
8fce4d8e3   Christoph Lameter   [PATCH] slab: Nod...
729
730
731
732
  }
  
  static void next_reap_node(void)
  {
909ea9646   Christoph Lameter   core: Replace __g...
733
  	int node = __this_cpu_read(slab_reap_node);
8fce4d8e3   Christoph Lameter   [PATCH] slab: Nod...
734

8fce4d8e3   Christoph Lameter   [PATCH] slab: Nod...
735
736
737
  	node = next_node(node, node_online_map);
  	if (unlikely(node >= MAX_NUMNODES))
  		node = first_node(node_online_map);
909ea9646   Christoph Lameter   core: Replace __g...
738
  	__this_cpu_write(slab_reap_node, node);
8fce4d8e3   Christoph Lameter   [PATCH] slab: Nod...
739
740
741
742
743
744
  }
  
  #else
  #define init_reap_node(cpu) do { } while (0)
  #define next_reap_node(void) do { } while (0)
  #endif
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
745
746
747
748
749
750
751
  /*
   * Initiate the reap timer running on the target CPU.  We run at around 1 to 2Hz
   * via the workqueue/eventd.
   * Add the CPU number into the expiration time to minimize the possibility of
   * the CPUs getting into lockstep and contending for the global cache chain
   * lock.
   */
897e679b1   Adrian Bunk   mm/slab.c: start_...
752
  static void __cpuinit start_cpu_timer(int cpu)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
753
  {
1871e52c7   Tejun Heo   percpu: make perc...
754
  	struct delayed_work *reap_work = &per_cpu(slab_reap_work, cpu);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
755
756
757
758
759
760
  
  	/*
  	 * When this gets called from do_initcalls via cpucache_init(),
  	 * init_workqueues() has already run, so keventd will be setup
  	 * at that time.
  	 */
52bad64d9   David Howells   WorkStruct: Separ...
761
  	if (keventd_up() && reap_work->work.func == NULL) {
8fce4d8e3   Christoph Lameter   [PATCH] slab: Nod...
762
  		init_reap_node(cpu);
203b42f73   Tejun Heo   workqueue: make d...
763
  		INIT_DEFERRABLE_WORK(reap_work, cache_reap);
2b2842146   Arjan van de Ven   [PATCH] user of t...
764
765
  		schedule_delayed_work_on(cpu, reap_work,
  					__round_jiffies_relative(HZ, cpu));
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
766
767
  	}
  }
e498be7da   Christoph Lameter   [PATCH] Numa-awar...
768
  static struct array_cache *alloc_arraycache(int node, int entries,
83b519e8b   Pekka Enberg   slab: setup alloc...
769
  					    int batchcount, gfp_t gfp)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
770
  {
b28a02de8   Pekka Enberg   [PATCH] slab: fix...
771
  	int memsize = sizeof(void *) * entries + sizeof(struct array_cache);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
772
  	struct array_cache *nc = NULL;
83b519e8b   Pekka Enberg   slab: setup alloc...
773
  	nc = kmalloc_node(memsize, gfp, node);
d5cff6352   Catalin Marinas   kmemleak: Add the...
774
775
  	/*
  	 * The array_cache structures contain pointers to free object.
25985edce   Lucas De Marchi   Fix common misspe...
776
  	 * However, when such objects are allocated or transferred to another
d5cff6352   Catalin Marinas   kmemleak: Add the...
777
778
779
780
781
  	 * cache the pointers are not cleared and they could be counted as
  	 * valid references during a kmemleak scan. Therefore, kmemleak must
  	 * not scan such objects.
  	 */
  	kmemleak_no_scan(nc);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
782
783
784
785
786
  	if (nc) {
  		nc->avail = 0;
  		nc->limit = entries;
  		nc->batchcount = batchcount;
  		nc->touched = 0;
e498be7da   Christoph Lameter   [PATCH] Numa-awar...
787
  		spin_lock_init(&nc->lock);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
788
789
790
  	}
  	return nc;
  }
072bb0aa5   Mel Gorman   mm: sl[au]b: add ...
791
792
793
794
795
796
797
798
799
800
801
  static inline bool is_slab_pfmemalloc(struct slab *slabp)
  {
  	struct page *page = virt_to_page(slabp->s_mem);
  
  	return PageSlabPfmemalloc(page);
  }
  
  /* Clears pfmemalloc_active if no slabs have pfmalloc set */
  static void recheck_pfmemalloc_active(struct kmem_cache *cachep,
  						struct array_cache *ac)
  {
ce8eb6c42   Christoph Lameter   slab: Rename list...
802
  	struct kmem_cache_node *n = cachep->node[numa_mem_id()];
072bb0aa5   Mel Gorman   mm: sl[au]b: add ...
803
804
805
806
807
  	struct slab *slabp;
  	unsigned long flags;
  
  	if (!pfmemalloc_active)
  		return;
ce8eb6c42   Christoph Lameter   slab: Rename list...
808
809
  	spin_lock_irqsave(&n->list_lock, flags);
  	list_for_each_entry(slabp, &n->slabs_full, list)
072bb0aa5   Mel Gorman   mm: sl[au]b: add ...
810
811
  		if (is_slab_pfmemalloc(slabp))
  			goto out;
ce8eb6c42   Christoph Lameter   slab: Rename list...
812
  	list_for_each_entry(slabp, &n->slabs_partial, list)
072bb0aa5   Mel Gorman   mm: sl[au]b: add ...
813
814
  		if (is_slab_pfmemalloc(slabp))
  			goto out;
ce8eb6c42   Christoph Lameter   slab: Rename list...
815
  	list_for_each_entry(slabp, &n->slabs_free, list)
072bb0aa5   Mel Gorman   mm: sl[au]b: add ...
816
817
818
819
820
  		if (is_slab_pfmemalloc(slabp))
  			goto out;
  
  	pfmemalloc_active = false;
  out:
ce8eb6c42   Christoph Lameter   slab: Rename list...
821
  	spin_unlock_irqrestore(&n->list_lock, flags);
072bb0aa5   Mel Gorman   mm: sl[au]b: add ...
822
  }
381760ead   Mel Gorman   mm: micro-optimis...
823
  static void *__ac_get_obj(struct kmem_cache *cachep, struct array_cache *ac,
072bb0aa5   Mel Gorman   mm: sl[au]b: add ...
824
825
826
827
828
829
830
  						gfp_t flags, bool force_refill)
  {
  	int i;
  	void *objp = ac->entry[--ac->avail];
  
  	/* Ensure the caller is allowed to use objects from PFMEMALLOC slab */
  	if (unlikely(is_obj_pfmemalloc(objp))) {
ce8eb6c42   Christoph Lameter   slab: Rename list...
831
  		struct kmem_cache_node *n;
072bb0aa5   Mel Gorman   mm: sl[au]b: add ...
832
833
834
835
836
837
838
  
  		if (gfp_pfmemalloc_allowed(flags)) {
  			clear_obj_pfmemalloc(&objp);
  			return objp;
  		}
  
  		/* The caller cannot use PFMEMALLOC objects, find another one */
d014dc2ed   Joonsoo Kim   slab: fix startin...
839
  		for (i = 0; i < ac->avail; i++) {
072bb0aa5   Mel Gorman   mm: sl[au]b: add ...
840
841
842
843
844
845
846
847
848
849
850
851
852
  			/* If a !PFMEMALLOC object is found, swap them */
  			if (!is_obj_pfmemalloc(ac->entry[i])) {
  				objp = ac->entry[i];
  				ac->entry[i] = ac->entry[ac->avail];
  				ac->entry[ac->avail] = objp;
  				return objp;
  			}
  		}
  
  		/*
  		 * If there are empty slabs on the slabs_free list and we are
  		 * being forced to refill the cache, mark this one !pfmemalloc.
  		 */
ce8eb6c42   Christoph Lameter   slab: Rename list...
853
854
  		n = cachep->node[numa_mem_id()];
  		if (!list_empty(&n->slabs_free) && force_refill) {
072bb0aa5   Mel Gorman   mm: sl[au]b: add ...
855
  			struct slab *slabp = virt_to_slab(objp);
30c29bea6   Mel Gorman   slab: do ClearSla...
856
  			ClearPageSlabPfmemalloc(virt_to_head_page(slabp->s_mem));
072bb0aa5   Mel Gorman   mm: sl[au]b: add ...
857
858
859
860
861
862
863
864
865
866
867
868
  			clear_obj_pfmemalloc(&objp);
  			recheck_pfmemalloc_active(cachep, ac);
  			return objp;
  		}
  
  		/* No !PFMEMALLOC objects available */
  		ac->avail++;
  		objp = NULL;
  	}
  
  	return objp;
  }
381760ead   Mel Gorman   mm: micro-optimis...
869
870
871
872
873
874
875
876
877
878
879
880
881
882
  static inline void *ac_get_obj(struct kmem_cache *cachep,
  			struct array_cache *ac, gfp_t flags, bool force_refill)
  {
  	void *objp;
  
  	if (unlikely(sk_memalloc_socks()))
  		objp = __ac_get_obj(cachep, ac, flags, force_refill);
  	else
  		objp = ac->entry[--ac->avail];
  
  	return objp;
  }
  
  static void *__ac_put_obj(struct kmem_cache *cachep, struct array_cache *ac,
072bb0aa5   Mel Gorman   mm: sl[au]b: add ...
883
884
885
886
  								void *objp)
  {
  	if (unlikely(pfmemalloc_active)) {
  		/* Some pfmemalloc slabs exist, check if this is one */
30c29bea6   Mel Gorman   slab: do ClearSla...
887
  		struct page *page = virt_to_head_page(objp);
072bb0aa5   Mel Gorman   mm: sl[au]b: add ...
888
889
890
  		if (PageSlabPfmemalloc(page))
  			set_obj_pfmemalloc(&objp);
  	}
381760ead   Mel Gorman   mm: micro-optimis...
891
892
893
894
895
896
897
898
  	return objp;
  }
  
  static inline void ac_put_obj(struct kmem_cache *cachep, struct array_cache *ac,
  								void *objp)
  {
  	if (unlikely(sk_memalloc_socks()))
  		objp = __ac_put_obj(cachep, ac, objp);
072bb0aa5   Mel Gorman   mm: sl[au]b: add ...
899
900
  	ac->entry[ac->avail++] = objp;
  }
3ded175a4   Christoph Lameter   [PATCH] slab: add...
901
902
903
904
905
906
907
908
909
910
  /*
   * Transfer objects in one arraycache to another.
   * Locking must be handled by the caller.
   *
   * Return the number of entries transferred.
   */
  static int transfer_objects(struct array_cache *to,
  		struct array_cache *from, unsigned int max)
  {
  	/* Figure out how many entries to transfer */
732eacc05   Hagen Paul Pfeifer   replace nested ma...
911
  	int nr = min3(from->avail, max, to->limit - to->avail);
3ded175a4   Christoph Lameter   [PATCH] slab: add...
912
913
914
915
916
917
918
919
920
  
  	if (!nr)
  		return 0;
  
  	memcpy(to->entry + to->avail, from->entry + from->avail -nr,
  			sizeof(void *) *nr);
  
  	from->avail -= nr;
  	to->avail += nr;
3ded175a4   Christoph Lameter   [PATCH] slab: add...
921
922
  	return nr;
  }
765c4507a   Christoph Lameter   [PATCH] GFP_THISN...
923
924
925
  #ifndef CONFIG_NUMA
  
  #define drain_alien_cache(cachep, alien) do { } while (0)
ce8eb6c42   Christoph Lameter   slab: Rename list...
926
  #define reap_alien(cachep, n) do { } while (0)
765c4507a   Christoph Lameter   [PATCH] GFP_THISN...
927

83b519e8b   Pekka Enberg   slab: setup alloc...
928
  static inline struct array_cache **alloc_alien_cache(int node, int limit, gfp_t gfp)
765c4507a   Christoph Lameter   [PATCH] GFP_THISN...
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
  {
  	return (struct array_cache **)BAD_ALIEN_MAGIC;
  }
  
  static inline void free_alien_cache(struct array_cache **ac_ptr)
  {
  }
  
  static inline int cache_free_alien(struct kmem_cache *cachep, void *objp)
  {
  	return 0;
  }
  
  static inline void *alternate_node_alloc(struct kmem_cache *cachep,
  		gfp_t flags)
  {
  	return NULL;
  }
8b98c1699   Christoph Hellwig   [PATCH] leak trac...
947
  static inline void *____cache_alloc_node(struct kmem_cache *cachep,
765c4507a   Christoph Lameter   [PATCH] GFP_THISN...
948
949
950
951
952
953
  		 gfp_t flags, int nodeid)
  {
  	return NULL;
  }
  
  #else	/* CONFIG_NUMA */
8b98c1699   Christoph Hellwig   [PATCH] leak trac...
954
  static void *____cache_alloc_node(struct kmem_cache *, gfp_t, int);
c61afb181   Paul Jackson   [PATCH] cpuset me...
955
  static void *alternate_node_alloc(struct kmem_cache *, gfp_t);
dc85da15d   Christoph Lameter   [PATCH] NUMA poli...
956

83b519e8b   Pekka Enberg   slab: setup alloc...
957
  static struct array_cache **alloc_alien_cache(int node, int limit, gfp_t gfp)
e498be7da   Christoph Lameter   [PATCH] Numa-awar...
958
959
  {
  	struct array_cache **ac_ptr;
8ef828668   Christoph Lameter   [PATCH] slab: red...
960
  	int memsize = sizeof(void *) * nr_node_ids;
e498be7da   Christoph Lameter   [PATCH] Numa-awar...
961
962
963
964
  	int i;
  
  	if (limit > 1)
  		limit = 12;
f3186a9c5   Haicheng Li   slab: initialize ...
965
  	ac_ptr = kzalloc_node(memsize, gfp, node);
e498be7da   Christoph Lameter   [PATCH] Numa-awar...
966
967
  	if (ac_ptr) {
  		for_each_node(i) {
f3186a9c5   Haicheng Li   slab: initialize ...
968
  			if (i == node || !node_online(i))
e498be7da   Christoph Lameter   [PATCH] Numa-awar...
969
  				continue;
83b519e8b   Pekka Enberg   slab: setup alloc...
970
  			ac_ptr[i] = alloc_arraycache(node, limit, 0xbaadf00d, gfp);
e498be7da   Christoph Lameter   [PATCH] Numa-awar...
971
  			if (!ac_ptr[i]) {
cc550defe   Akinobu Mita   slab: fix typo in...
972
  				for (i--; i >= 0; i--)
e498be7da   Christoph Lameter   [PATCH] Numa-awar...
973
974
975
976
977
978
979
980
  					kfree(ac_ptr[i]);
  				kfree(ac_ptr);
  				return NULL;
  			}
  		}
  	}
  	return ac_ptr;
  }
5295a74cc   Pekka Enberg   [PATCH] slab: red...
981
  static void free_alien_cache(struct array_cache **ac_ptr)
e498be7da   Christoph Lameter   [PATCH] Numa-awar...
982
983
984
985
986
  {
  	int i;
  
  	if (!ac_ptr)
  		return;
e498be7da   Christoph Lameter   [PATCH] Numa-awar...
987
  	for_each_node(i)
b28a02de8   Pekka Enberg   [PATCH] slab: fix...
988
  	    kfree(ac_ptr[i]);
e498be7da   Christoph Lameter   [PATCH] Numa-awar...
989
990
  	kfree(ac_ptr);
  }
343e0d7a9   Pekka Enberg   [PATCH] slab: rep...
991
  static void __drain_alien_cache(struct kmem_cache *cachep,
5295a74cc   Pekka Enberg   [PATCH] slab: red...
992
  				struct array_cache *ac, int node)
e498be7da   Christoph Lameter   [PATCH] Numa-awar...
993
  {
ce8eb6c42   Christoph Lameter   slab: Rename list...
994
  	struct kmem_cache_node *n = cachep->node[node];
e498be7da   Christoph Lameter   [PATCH] Numa-awar...
995
996
  
  	if (ac->avail) {
ce8eb6c42   Christoph Lameter   slab: Rename list...
997
  		spin_lock(&n->list_lock);
e00946fe2   Christoph Lameter   [PATCH] slab: Byp...
998
999
1000
1001
1002
  		/*
  		 * Stuff objects into the remote nodes shared array first.
  		 * That way we could avoid the overhead of putting the objects
  		 * into the free lists and getting them back later.
  		 */
ce8eb6c42   Christoph Lameter   slab: Rename list...
1003
1004
  		if (n->shared)
  			transfer_objects(n->shared, ac, ac->limit);
e00946fe2   Christoph Lameter   [PATCH] slab: Byp...
1005

ff69416e6   Christoph Lameter   [PATCH] slab: fix...
1006
  		free_block(cachep, ac->entry, ac->avail, node);
e498be7da   Christoph Lameter   [PATCH] Numa-awar...
1007
  		ac->avail = 0;
ce8eb6c42   Christoph Lameter   slab: Rename list...
1008
  		spin_unlock(&n->list_lock);
e498be7da   Christoph Lameter   [PATCH] Numa-awar...
1009
1010
  	}
  }
8fce4d8e3   Christoph Lameter   [PATCH] slab: Nod...
1011
1012
1013
  /*
   * Called from cache_reap() to regularly drain alien caches round robin.
   */
ce8eb6c42   Christoph Lameter   slab: Rename list...
1014
  static void reap_alien(struct kmem_cache *cachep, struct kmem_cache_node *n)
8fce4d8e3   Christoph Lameter   [PATCH] slab: Nod...
1015
  {
909ea9646   Christoph Lameter   core: Replace __g...
1016
  	int node = __this_cpu_read(slab_reap_node);
8fce4d8e3   Christoph Lameter   [PATCH] slab: Nod...
1017

ce8eb6c42   Christoph Lameter   slab: Rename list...
1018
1019
  	if (n->alien) {
  		struct array_cache *ac = n->alien[node];
e00946fe2   Christoph Lameter   [PATCH] slab: Byp...
1020
1021
  
  		if (ac && ac->avail && spin_trylock_irq(&ac->lock)) {
8fce4d8e3   Christoph Lameter   [PATCH] slab: Nod...
1022
1023
1024
1025
1026
  			__drain_alien_cache(cachep, ac, node);
  			spin_unlock_irq(&ac->lock);
  		}
  	}
  }
a737b3e2f   Andrew Morton   [PATCH] slab cleanup
1027
1028
  static void drain_alien_cache(struct kmem_cache *cachep,
  				struct array_cache **alien)
e498be7da   Christoph Lameter   [PATCH] Numa-awar...
1029
  {
b28a02de8   Pekka Enberg   [PATCH] slab: fix...
1030
  	int i = 0;
e498be7da   Christoph Lameter   [PATCH] Numa-awar...
1031
1032
1033
1034
  	struct array_cache *ac;
  	unsigned long flags;
  
  	for_each_online_node(i) {
4484ebf12   Ravikiran G Thirumalai   [PATCH] NUMA slab...
1035
  		ac = alien[i];
e498be7da   Christoph Lameter   [PATCH] Numa-awar...
1036
1037
1038
1039
1040
1041
1042
  		if (ac) {
  			spin_lock_irqsave(&ac->lock, flags);
  			__drain_alien_cache(cachep, ac, i);
  			spin_unlock_irqrestore(&ac->lock, flags);
  		}
  	}
  }
729bd0b74   Pekka Enberg   [PATCH] slab: ext...
1043

873623dfa   Ingo Molnar   [PATCH] lockdep: ...
1044
  static inline int cache_free_alien(struct kmem_cache *cachep, void *objp)
729bd0b74   Pekka Enberg   [PATCH] slab: ext...
1045
1046
1047
  {
  	struct slab *slabp = virt_to_slab(objp);
  	int nodeid = slabp->nodeid;
ce8eb6c42   Christoph Lameter   slab: Rename list...
1048
  	struct kmem_cache_node *n;
729bd0b74   Pekka Enberg   [PATCH] slab: ext...
1049
  	struct array_cache *alien = NULL;
1ca4cb241   Pekka Enberg   [PATCH] slab: red...
1050
  	int node;
7d6e6d09d   Lee Schermerhorn   numa: slab: use n...
1051
  	node = numa_mem_id();
729bd0b74   Pekka Enberg   [PATCH] slab: ext...
1052
1053
1054
1055
1056
  
  	/*
  	 * Make sure we are not freeing a object from another node to the array
  	 * cache on this cpu.
  	 */
62918a036   Siddha, Suresh B   [PATCH] x86-64: s...
1057
  	if (likely(slabp->nodeid == node))
729bd0b74   Pekka Enberg   [PATCH] slab: ext...
1058
  		return 0;
ce8eb6c42   Christoph Lameter   slab: Rename list...
1059
  	n = cachep->node[node];
729bd0b74   Pekka Enberg   [PATCH] slab: ext...
1060
  	STATS_INC_NODEFREES(cachep);
ce8eb6c42   Christoph Lameter   slab: Rename list...
1061
1062
  	if (n->alien && n->alien[nodeid]) {
  		alien = n->alien[nodeid];
873623dfa   Ingo Molnar   [PATCH] lockdep: ...
1063
  		spin_lock(&alien->lock);
729bd0b74   Pekka Enberg   [PATCH] slab: ext...
1064
1065
1066
1067
  		if (unlikely(alien->avail == alien->limit)) {
  			STATS_INC_ACOVERFLOW(cachep);
  			__drain_alien_cache(cachep, alien, nodeid);
  		}
072bb0aa5   Mel Gorman   mm: sl[au]b: add ...
1068
  		ac_put_obj(cachep, alien, objp);
729bd0b74   Pekka Enberg   [PATCH] slab: ext...
1069
1070
  		spin_unlock(&alien->lock);
  	} else {
6a67368c3   Christoph Lameter   slab: Rename node...
1071
  		spin_lock(&(cachep->node[nodeid])->list_lock);
729bd0b74   Pekka Enberg   [PATCH] slab: ext...
1072
  		free_block(cachep, &objp, 1, nodeid);
6a67368c3   Christoph Lameter   slab: Rename node...
1073
  		spin_unlock(&(cachep->node[nodeid])->list_lock);
729bd0b74   Pekka Enberg   [PATCH] slab: ext...
1074
1075
1076
  	}
  	return 1;
  }
e498be7da   Christoph Lameter   [PATCH] Numa-awar...
1077
  #endif
8f9f8d9e8   David Rientjes   slab: add memory ...
1078
  /*
6a67368c3   Christoph Lameter   slab: Rename node...
1079
   * Allocates and initializes node for a node on each slab cache, used for
ce8eb6c42   Christoph Lameter   slab: Rename list...
1080
   * either memory or cpu hotplug.  If memory is being hot-added, the kmem_cache_node
8f9f8d9e8   David Rientjes   slab: add memory ...
1081
   * will be allocated off-node since memory is not yet online for the new node.
6a67368c3   Christoph Lameter   slab: Rename node...
1082
   * When hotplugging memory or a cpu, existing node are not replaced if
8f9f8d9e8   David Rientjes   slab: add memory ...
1083
1084
   * already in use.
   *
18004c5d4   Christoph Lameter   mm, sl[aou]b: Use...
1085
   * Must hold slab_mutex.
8f9f8d9e8   David Rientjes   slab: add memory ...
1086
   */
6a67368c3   Christoph Lameter   slab: Rename node...
1087
  static int init_cache_node_node(int node)
8f9f8d9e8   David Rientjes   slab: add memory ...
1088
1089
  {
  	struct kmem_cache *cachep;
ce8eb6c42   Christoph Lameter   slab: Rename list...
1090
  	struct kmem_cache_node *n;
6744f087b   Christoph Lameter   slab: Common name...
1091
  	const int memsize = sizeof(struct kmem_cache_node);
8f9f8d9e8   David Rientjes   slab: add memory ...
1092

18004c5d4   Christoph Lameter   mm, sl[aou]b: Use...
1093
  	list_for_each_entry(cachep, &slab_caches, list) {
8f9f8d9e8   David Rientjes   slab: add memory ...
1094
1095
1096
1097
1098
  		/*
  		 * Set up the size64 kmemlist for cpu before we can
  		 * begin anything. Make sure some other cpu on this
  		 * node has not already allocated this
  		 */
6a67368c3   Christoph Lameter   slab: Rename node...
1099
  		if (!cachep->node[node]) {
ce8eb6c42   Christoph Lameter   slab: Rename list...
1100
1101
  			n = kmalloc_node(memsize, GFP_KERNEL, node);
  			if (!n)
8f9f8d9e8   David Rientjes   slab: add memory ...
1102
  				return -ENOMEM;
ce8eb6c42   Christoph Lameter   slab: Rename list...
1103
1104
  			kmem_cache_node_init(n);
  			n->next_reap = jiffies + REAPTIMEOUT_LIST3 +
8f9f8d9e8   David Rientjes   slab: add memory ...
1105
1106
1107
1108
  			    ((unsigned long)cachep) % REAPTIMEOUT_LIST3;
  
  			/*
  			 * The l3s don't come and go as CPUs come and
18004c5d4   Christoph Lameter   mm, sl[aou]b: Use...
1109
  			 * go.  slab_mutex is sufficient
8f9f8d9e8   David Rientjes   slab: add memory ...
1110
1111
  			 * protection here.
  			 */
ce8eb6c42   Christoph Lameter   slab: Rename list...
1112
  			cachep->node[node] = n;
8f9f8d9e8   David Rientjes   slab: add memory ...
1113
  		}
6a67368c3   Christoph Lameter   slab: Rename node...
1114
1115
  		spin_lock_irq(&cachep->node[node]->list_lock);
  		cachep->node[node]->free_limit =
8f9f8d9e8   David Rientjes   slab: add memory ...
1116
1117
  			(1 + nr_cpus_node(node)) *
  			cachep->batchcount + cachep->num;
6a67368c3   Christoph Lameter   slab: Rename node...
1118
  		spin_unlock_irq(&cachep->node[node]->list_lock);
8f9f8d9e8   David Rientjes   slab: add memory ...
1119
1120
1121
  	}
  	return 0;
  }
fbf1e473b   Akinobu Mita   cpu hotplug: slab...
1122
1123
1124
  static void __cpuinit cpuup_canceled(long cpu)
  {
  	struct kmem_cache *cachep;
ce8eb6c42   Christoph Lameter   slab: Rename list...
1125
  	struct kmem_cache_node *n = NULL;
7d6e6d09d   Lee Schermerhorn   numa: slab: use n...
1126
  	int node = cpu_to_mem(cpu);
a70f73028   Rusty Russell   cpumask: replace ...
1127
  	const struct cpumask *mask = cpumask_of_node(node);
fbf1e473b   Akinobu Mita   cpu hotplug: slab...
1128

18004c5d4   Christoph Lameter   mm, sl[aou]b: Use...
1129
  	list_for_each_entry(cachep, &slab_caches, list) {
fbf1e473b   Akinobu Mita   cpu hotplug: slab...
1130
1131
1132
  		struct array_cache *nc;
  		struct array_cache *shared;
  		struct array_cache **alien;
fbf1e473b   Akinobu Mita   cpu hotplug: slab...
1133

fbf1e473b   Akinobu Mita   cpu hotplug: slab...
1134
1135
1136
  		/* cpu is dead; no one can alloc from it. */
  		nc = cachep->array[cpu];
  		cachep->array[cpu] = NULL;
ce8eb6c42   Christoph Lameter   slab: Rename list...
1137
  		n = cachep->node[node];
fbf1e473b   Akinobu Mita   cpu hotplug: slab...
1138

ce8eb6c42   Christoph Lameter   slab: Rename list...
1139
  		if (!n)
fbf1e473b   Akinobu Mita   cpu hotplug: slab...
1140
  			goto free_array_cache;
ce8eb6c42   Christoph Lameter   slab: Rename list...
1141
  		spin_lock_irq(&n->list_lock);
fbf1e473b   Akinobu Mita   cpu hotplug: slab...
1142

ce8eb6c42   Christoph Lameter   slab: Rename list...
1143
1144
  		/* Free limit for this kmem_cache_node */
  		n->free_limit -= cachep->batchcount;
fbf1e473b   Akinobu Mita   cpu hotplug: slab...
1145
1146
  		if (nc)
  			free_block(cachep, nc->entry, nc->avail, node);
58463c1fe   Rusty Russell   cpumask: avoid de...
1147
  		if (!cpumask_empty(mask)) {
ce8eb6c42   Christoph Lameter   slab: Rename list...
1148
  			spin_unlock_irq(&n->list_lock);
fbf1e473b   Akinobu Mita   cpu hotplug: slab...
1149
1150
  			goto free_array_cache;
  		}
ce8eb6c42   Christoph Lameter   slab: Rename list...
1151
  		shared = n->shared;
fbf1e473b   Akinobu Mita   cpu hotplug: slab...
1152
1153
1154
  		if (shared) {
  			free_block(cachep, shared->entry,
  				   shared->avail, node);
ce8eb6c42   Christoph Lameter   slab: Rename list...
1155
  			n->shared = NULL;
fbf1e473b   Akinobu Mita   cpu hotplug: slab...
1156
  		}
ce8eb6c42   Christoph Lameter   slab: Rename list...
1157
1158
  		alien = n->alien;
  		n->alien = NULL;
fbf1e473b   Akinobu Mita   cpu hotplug: slab...
1159

ce8eb6c42   Christoph Lameter   slab: Rename list...
1160
  		spin_unlock_irq(&n->list_lock);
fbf1e473b   Akinobu Mita   cpu hotplug: slab...
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
  
  		kfree(shared);
  		if (alien) {
  			drain_alien_cache(cachep, alien);
  			free_alien_cache(alien);
  		}
  free_array_cache:
  		kfree(nc);
  	}
  	/*
  	 * In the previous loop, all the objects were freed to
  	 * the respective cache's slabs,  now we can go ahead and
  	 * shrink each nodelist to its limit.
  	 */
18004c5d4   Christoph Lameter   mm, sl[aou]b: Use...
1175
  	list_for_each_entry(cachep, &slab_caches, list) {
ce8eb6c42   Christoph Lameter   slab: Rename list...
1176
1177
  		n = cachep->node[node];
  		if (!n)
fbf1e473b   Akinobu Mita   cpu hotplug: slab...
1178
  			continue;
ce8eb6c42   Christoph Lameter   slab: Rename list...
1179
  		drain_freelist(cachep, n, n->free_objects);
fbf1e473b   Akinobu Mita   cpu hotplug: slab...
1180
1181
1182
1183
  	}
  }
  
  static int __cpuinit cpuup_prepare(long cpu)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1184
  {
343e0d7a9   Pekka Enberg   [PATCH] slab: rep...
1185
  	struct kmem_cache *cachep;
ce8eb6c42   Christoph Lameter   slab: Rename list...
1186
  	struct kmem_cache_node *n = NULL;
7d6e6d09d   Lee Schermerhorn   numa: slab: use n...
1187
  	int node = cpu_to_mem(cpu);
8f9f8d9e8   David Rientjes   slab: add memory ...
1188
  	int err;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1189

fbf1e473b   Akinobu Mita   cpu hotplug: slab...
1190
1191
1192
1193
  	/*
  	 * We need to do this right in the beginning since
  	 * alloc_arraycache's are going to use this list.
  	 * kmalloc_node allows us to add the slab to the right
ce8eb6c42   Christoph Lameter   slab: Rename list...
1194
  	 * kmem_cache_node and not this cpu's kmem_cache_node
fbf1e473b   Akinobu Mita   cpu hotplug: slab...
1195
  	 */
6a67368c3   Christoph Lameter   slab: Rename node...
1196
  	err = init_cache_node_node(node);
8f9f8d9e8   David Rientjes   slab: add memory ...
1197
1198
  	if (err < 0)
  		goto bad;
fbf1e473b   Akinobu Mita   cpu hotplug: slab...
1199
1200
1201
1202
1203
  
  	/*
  	 * Now we can go ahead with allocating the shared arrays and
  	 * array caches
  	 */
18004c5d4   Christoph Lameter   mm, sl[aou]b: Use...
1204
  	list_for_each_entry(cachep, &slab_caches, list) {
fbf1e473b   Akinobu Mita   cpu hotplug: slab...
1205
1206
1207
1208
1209
  		struct array_cache *nc;
  		struct array_cache *shared = NULL;
  		struct array_cache **alien = NULL;
  
  		nc = alloc_arraycache(node, cachep->limit,
83b519e8b   Pekka Enberg   slab: setup alloc...
1210
  					cachep->batchcount, GFP_KERNEL);
fbf1e473b   Akinobu Mita   cpu hotplug: slab...
1211
1212
1213
1214
1215
  		if (!nc)
  			goto bad;
  		if (cachep->shared) {
  			shared = alloc_arraycache(node,
  				cachep->shared * cachep->batchcount,
83b519e8b   Pekka Enberg   slab: setup alloc...
1216
  				0xbaadf00d, GFP_KERNEL);
12d00f6a1   Akinobu Mita   cpu hotplug: slab...
1217
1218
  			if (!shared) {
  				kfree(nc);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1219
  				goto bad;
12d00f6a1   Akinobu Mita   cpu hotplug: slab...
1220
  			}
fbf1e473b   Akinobu Mita   cpu hotplug: slab...
1221
1222
  		}
  		if (use_alien_caches) {
83b519e8b   Pekka Enberg   slab: setup alloc...
1223
  			alien = alloc_alien_cache(node, cachep->limit, GFP_KERNEL);
12d00f6a1   Akinobu Mita   cpu hotplug: slab...
1224
1225
1226
  			if (!alien) {
  				kfree(shared);
  				kfree(nc);
fbf1e473b   Akinobu Mita   cpu hotplug: slab...
1227
  				goto bad;
12d00f6a1   Akinobu Mita   cpu hotplug: slab...
1228
  			}
fbf1e473b   Akinobu Mita   cpu hotplug: slab...
1229
1230
  		}
  		cachep->array[cpu] = nc;
ce8eb6c42   Christoph Lameter   slab: Rename list...
1231
1232
  		n = cachep->node[node];
  		BUG_ON(!n);
fbf1e473b   Akinobu Mita   cpu hotplug: slab...
1233

ce8eb6c42   Christoph Lameter   slab: Rename list...
1234
1235
  		spin_lock_irq(&n->list_lock);
  		if (!n->shared) {
fbf1e473b   Akinobu Mita   cpu hotplug: slab...
1236
1237
1238
1239
  			/*
  			 * We are serialised from CPU_DEAD or
  			 * CPU_UP_CANCELLED by the cpucontrol lock
  			 */
ce8eb6c42   Christoph Lameter   slab: Rename list...
1240
  			n->shared = shared;
fbf1e473b   Akinobu Mita   cpu hotplug: slab...
1241
1242
  			shared = NULL;
  		}
4484ebf12   Ravikiran G Thirumalai   [PATCH] NUMA slab...
1243
  #ifdef CONFIG_NUMA
ce8eb6c42   Christoph Lameter   slab: Rename list...
1244
1245
  		if (!n->alien) {
  			n->alien = alien;
fbf1e473b   Akinobu Mita   cpu hotplug: slab...
1246
  			alien = NULL;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1247
  		}
fbf1e473b   Akinobu Mita   cpu hotplug: slab...
1248
  #endif
ce8eb6c42   Christoph Lameter   slab: Rename list...
1249
  		spin_unlock_irq(&n->list_lock);
fbf1e473b   Akinobu Mita   cpu hotplug: slab...
1250
1251
  		kfree(shared);
  		free_alien_cache(alien);
83835b3d9   Peter Zijlstra   slab, lockdep: An...
1252
1253
  		if (cachep->flags & SLAB_DEBUG_OBJECTS)
  			slab_set_debugobj_lock_classes_node(cachep, node);
6ccfb5bcf   Glauber Costa   slab: annotate on...
1254
1255
1256
  		else if (!OFF_SLAB(cachep) &&
  			 !(cachep->flags & SLAB_DESTROY_BY_RCU))
  			on_slab_lock_classes_node(cachep, node);
fbf1e473b   Akinobu Mita   cpu hotplug: slab...
1257
  	}
ce79ddc8e   Pekka Enberg   SLAB: Fix lockdep...
1258
  	init_node_lock_keys(node);
fbf1e473b   Akinobu Mita   cpu hotplug: slab...
1259
1260
  	return 0;
  bad:
12d00f6a1   Akinobu Mita   cpu hotplug: slab...
1261
  	cpuup_canceled(cpu);
fbf1e473b   Akinobu Mita   cpu hotplug: slab...
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
  	return -ENOMEM;
  }
  
  static int __cpuinit cpuup_callback(struct notifier_block *nfb,
  				    unsigned long action, void *hcpu)
  {
  	long cpu = (long)hcpu;
  	int err = 0;
  
  	switch (action) {
fbf1e473b   Akinobu Mita   cpu hotplug: slab...
1272
1273
  	case CPU_UP_PREPARE:
  	case CPU_UP_PREPARE_FROZEN:
18004c5d4   Christoph Lameter   mm, sl[aou]b: Use...
1274
  		mutex_lock(&slab_mutex);
fbf1e473b   Akinobu Mita   cpu hotplug: slab...
1275
  		err = cpuup_prepare(cpu);
18004c5d4   Christoph Lameter   mm, sl[aou]b: Use...
1276
  		mutex_unlock(&slab_mutex);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1277
1278
  		break;
  	case CPU_ONLINE:
8bb784428   Rafael J. Wysocki   Add suspend-relat...
1279
  	case CPU_ONLINE_FROZEN:
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1280
1281
1282
  		start_cpu_timer(cpu);
  		break;
  #ifdef CONFIG_HOTPLUG_CPU
5830c5902   Christoph Lameter   slab: shut down c...
1283
    	case CPU_DOWN_PREPARE:
8bb784428   Rafael J. Wysocki   Add suspend-relat...
1284
    	case CPU_DOWN_PREPARE_FROZEN:
5830c5902   Christoph Lameter   slab: shut down c...
1285
  		/*
18004c5d4   Christoph Lameter   mm, sl[aou]b: Use...
1286
  		 * Shutdown cache reaper. Note that the slab_mutex is
5830c5902   Christoph Lameter   slab: shut down c...
1287
1288
1289
1290
  		 * held so that if cache_reap() is invoked it cannot do
  		 * anything expensive but will only modify reap_work
  		 * and reschedule the timer.
  		*/
afe2c511f   Tejun Heo   workqueue: conver...
1291
  		cancel_delayed_work_sync(&per_cpu(slab_reap_work, cpu));
5830c5902   Christoph Lameter   slab: shut down c...
1292
  		/* Now the cache_reaper is guaranteed to be not running. */
1871e52c7   Tejun Heo   percpu: make perc...
1293
  		per_cpu(slab_reap_work, cpu).work.func = NULL;
5830c5902   Christoph Lameter   slab: shut down c...
1294
1295
    		break;
    	case CPU_DOWN_FAILED:
8bb784428   Rafael J. Wysocki   Add suspend-relat...
1296
    	case CPU_DOWN_FAILED_FROZEN:
5830c5902   Christoph Lameter   slab: shut down c...
1297
1298
  		start_cpu_timer(cpu);
    		break;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1299
  	case CPU_DEAD:
8bb784428   Rafael J. Wysocki   Add suspend-relat...
1300
  	case CPU_DEAD_FROZEN:
4484ebf12   Ravikiran G Thirumalai   [PATCH] NUMA slab...
1301
1302
  		/*
  		 * Even if all the cpus of a node are down, we don't free the
ce8eb6c42   Christoph Lameter   slab: Rename list...
1303
  		 * kmem_cache_node of any cache. This to avoid a race between
4484ebf12   Ravikiran G Thirumalai   [PATCH] NUMA slab...
1304
  		 * cpu_down, and a kmalloc allocation from another cpu for
ce8eb6c42   Christoph Lameter   slab: Rename list...
1305
  		 * memory from the node of the cpu going down.  The node
4484ebf12   Ravikiran G Thirumalai   [PATCH] NUMA slab...
1306
1307
1308
  		 * structure is usually allocated from kmem_cache_create() and
  		 * gets destroyed at kmem_cache_destroy().
  		 */
183ff22bb   Simon Arlott   spelling fixes: mm/
1309
  		/* fall through */
8f5be20bf   Ravikiran G Thirumalai   [PATCH] mm: slab:...
1310
  #endif
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1311
  	case CPU_UP_CANCELED:
8bb784428   Rafael J. Wysocki   Add suspend-relat...
1312
  	case CPU_UP_CANCELED_FROZEN:
18004c5d4   Christoph Lameter   mm, sl[aou]b: Use...
1313
  		mutex_lock(&slab_mutex);
fbf1e473b   Akinobu Mita   cpu hotplug: slab...
1314
  		cpuup_canceled(cpu);
18004c5d4   Christoph Lameter   mm, sl[aou]b: Use...
1315
  		mutex_unlock(&slab_mutex);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1316
  		break;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1317
  	}
eac406801   Akinobu Mita   slab: convert cpu...
1318
  	return notifier_from_errno(err);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1319
  }
74b85f379   Chandra Seetharaman   [PATCH] cpu hotpl...
1320
1321
1322
  static struct notifier_block __cpuinitdata cpucache_notifier = {
  	&cpuup_callback, NULL, 0
  };
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1323

8f9f8d9e8   David Rientjes   slab: add memory ...
1324
1325
1326
1327
1328
1329
  #if defined(CONFIG_NUMA) && defined(CONFIG_MEMORY_HOTPLUG)
  /*
   * Drains freelist for a node on each slab cache, used for memory hot-remove.
   * Returns -EBUSY if all objects cannot be drained so that the node is not
   * removed.
   *
18004c5d4   Christoph Lameter   mm, sl[aou]b: Use...
1330
   * Must hold slab_mutex.
8f9f8d9e8   David Rientjes   slab: add memory ...
1331
   */
6a67368c3   Christoph Lameter   slab: Rename node...
1332
  static int __meminit drain_cache_node_node(int node)
8f9f8d9e8   David Rientjes   slab: add memory ...
1333
1334
1335
  {
  	struct kmem_cache *cachep;
  	int ret = 0;
18004c5d4   Christoph Lameter   mm, sl[aou]b: Use...
1336
  	list_for_each_entry(cachep, &slab_caches, list) {
ce8eb6c42   Christoph Lameter   slab: Rename list...
1337
  		struct kmem_cache_node *n;
8f9f8d9e8   David Rientjes   slab: add memory ...
1338

ce8eb6c42   Christoph Lameter   slab: Rename list...
1339
1340
  		n = cachep->node[node];
  		if (!n)
8f9f8d9e8   David Rientjes   slab: add memory ...
1341
  			continue;
ce8eb6c42   Christoph Lameter   slab: Rename list...
1342
  		drain_freelist(cachep, n, n->free_objects);
8f9f8d9e8   David Rientjes   slab: add memory ...
1343

ce8eb6c42   Christoph Lameter   slab: Rename list...
1344
1345
  		if (!list_empty(&n->slabs_full) ||
  		    !list_empty(&n->slabs_partial)) {
8f9f8d9e8   David Rientjes   slab: add memory ...
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
  			ret = -EBUSY;
  			break;
  		}
  	}
  	return ret;
  }
  
  static int __meminit slab_memory_callback(struct notifier_block *self,
  					unsigned long action, void *arg)
  {
  	struct memory_notify *mnb = arg;
  	int ret = 0;
  	int nid;
  
  	nid = mnb->status_change_nid;
  	if (nid < 0)
  		goto out;
  
  	switch (action) {
  	case MEM_GOING_ONLINE:
18004c5d4   Christoph Lameter   mm, sl[aou]b: Use...
1366
  		mutex_lock(&slab_mutex);
6a67368c3   Christoph Lameter   slab: Rename node...
1367
  		ret = init_cache_node_node(nid);
18004c5d4   Christoph Lameter   mm, sl[aou]b: Use...
1368
  		mutex_unlock(&slab_mutex);
8f9f8d9e8   David Rientjes   slab: add memory ...
1369
1370
  		break;
  	case MEM_GOING_OFFLINE:
18004c5d4   Christoph Lameter   mm, sl[aou]b: Use...
1371
  		mutex_lock(&slab_mutex);
6a67368c3   Christoph Lameter   slab: Rename node...
1372
  		ret = drain_cache_node_node(nid);
18004c5d4   Christoph Lameter   mm, sl[aou]b: Use...
1373
  		mutex_unlock(&slab_mutex);
8f9f8d9e8   David Rientjes   slab: add memory ...
1374
1375
1376
1377
1378
1379
1380
1381
  		break;
  	case MEM_ONLINE:
  	case MEM_OFFLINE:
  	case MEM_CANCEL_ONLINE:
  	case MEM_CANCEL_OFFLINE:
  		break;
  	}
  out:
5fda1bd5b   Prarit Bhargava   mm: notifier_from...
1382
  	return notifier_from_errno(ret);
8f9f8d9e8   David Rientjes   slab: add memory ...
1383
1384
  }
  #endif /* CONFIG_NUMA && CONFIG_MEMORY_HOTPLUG */
e498be7da   Christoph Lameter   [PATCH] Numa-awar...
1385
  /*
ce8eb6c42   Christoph Lameter   slab: Rename list...
1386
   * swap the static kmem_cache_node with kmalloced memory
e498be7da   Christoph Lameter   [PATCH] Numa-awar...
1387
   */
6744f087b   Christoph Lameter   slab: Common name...
1388
  static void __init init_list(struct kmem_cache *cachep, struct kmem_cache_node *list,
8f9f8d9e8   David Rientjes   slab: add memory ...
1389
  				int nodeid)
e498be7da   Christoph Lameter   [PATCH] Numa-awar...
1390
  {
6744f087b   Christoph Lameter   slab: Common name...
1391
  	struct kmem_cache_node *ptr;
e498be7da   Christoph Lameter   [PATCH] Numa-awar...
1392

6744f087b   Christoph Lameter   slab: Common name...
1393
  	ptr = kmalloc_node(sizeof(struct kmem_cache_node), GFP_NOWAIT, nodeid);
e498be7da   Christoph Lameter   [PATCH] Numa-awar...
1394
  	BUG_ON(!ptr);
6744f087b   Christoph Lameter   slab: Common name...
1395
  	memcpy(ptr, list, sizeof(struct kmem_cache_node));
2b2d5493e   Ingo Molnar   [PATCH] lockdep: ...
1396
1397
1398
1399
  	/*
  	 * Do not assume that spinlocks can be initialized via memcpy:
  	 */
  	spin_lock_init(&ptr->list_lock);
e498be7da   Christoph Lameter   [PATCH] Numa-awar...
1400
  	MAKE_ALL_LISTS(cachep, ptr, nodeid);
6a67368c3   Christoph Lameter   slab: Rename node...
1401
  	cachep->node[nodeid] = ptr;
e498be7da   Christoph Lameter   [PATCH] Numa-awar...
1402
  }
a737b3e2f   Andrew Morton   [PATCH] slab cleanup
1403
  /*
ce8eb6c42   Christoph Lameter   slab: Rename list...
1404
1405
   * For setting up all the kmem_cache_node for cache whose buffer_size is same as
   * size of kmem_cache_node.
556a169da   Pekka Enberg   slab: fix bootstr...
1406
   */
ce8eb6c42   Christoph Lameter   slab: Rename list...
1407
  static void __init set_up_node(struct kmem_cache *cachep, int index)
556a169da   Pekka Enberg   slab: fix bootstr...
1408
1409
1410
1411
  {
  	int node;
  
  	for_each_online_node(node) {
ce8eb6c42   Christoph Lameter   slab: Rename list...
1412
  		cachep->node[node] = &init_kmem_cache_node[index + node];
6a67368c3   Christoph Lameter   slab: Rename node...
1413
  		cachep->node[node]->next_reap = jiffies +
556a169da   Pekka Enberg   slab: fix bootstr...
1414
1415
1416
1417
1418
1419
  		    REAPTIMEOUT_LIST3 +
  		    ((unsigned long)cachep) % REAPTIMEOUT_LIST3;
  	}
  }
  
  /*
3c5834652   Christoph Lameter   slab: Simplify bo...
1420
   * The memory after the last cpu cache pointer is used for the
6a67368c3   Christoph Lameter   slab: Rename node...
1421
   * the node pointer.
3c5834652   Christoph Lameter   slab: Simplify bo...
1422
   */
6a67368c3   Christoph Lameter   slab: Rename node...
1423
  static void setup_node_pointer(struct kmem_cache *cachep)
3c5834652   Christoph Lameter   slab: Simplify bo...
1424
  {
6a67368c3   Christoph Lameter   slab: Rename node...
1425
  	cachep->node = (struct kmem_cache_node **)&cachep->array[nr_cpu_ids];
3c5834652   Christoph Lameter   slab: Simplify bo...
1426
1427
1428
  }
  
  /*
a737b3e2f   Andrew Morton   [PATCH] slab cleanup
1429
1430
   * Initialisation.  Called after the page allocator have been initialised and
   * before smp_init().
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1431
1432
1433
   */
  void __init kmem_cache_init(void)
  {
e498be7da   Christoph Lameter   [PATCH] Numa-awar...
1434
  	int i;
9b030cb86   Christoph Lameter   mm/sl[aou]b: Use ...
1435
  	kmem_cache = &kmem_cache_boot;
6a67368c3   Christoph Lameter   slab: Rename node...
1436
  	setup_node_pointer(kmem_cache);
9b030cb86   Christoph Lameter   mm/sl[aou]b: Use ...
1437

b6e68bc1b   Mel Gorman   page allocator: s...
1438
  	if (num_possible_nodes() == 1)
62918a036   Siddha, Suresh B   [PATCH] x86-64: s...
1439
  		use_alien_caches = 0;
3c5834652   Christoph Lameter   slab: Simplify bo...
1440
  	for (i = 0; i < NUM_INIT_LISTS; i++)
ce8eb6c42   Christoph Lameter   slab: Rename list...
1441
  		kmem_cache_node_init(&init_kmem_cache_node[i]);
3c5834652   Christoph Lameter   slab: Simplify bo...
1442

ce8eb6c42   Christoph Lameter   slab: Rename list...
1443
  	set_up_node(kmem_cache, CACHE_CACHE);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1444
1445
1446
  
  	/*
  	 * Fragmentation resistance on low memory - only use bigger
3df1cccdf   David Rientjes   slab: introduce s...
1447
1448
  	 * page orders on machines with more than 32MB of memory if
  	 * not overridden on the command line.
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1449
  	 */
3df1cccdf   David Rientjes   slab: introduce s...
1450
  	if (!slab_max_order_set && totalram_pages > (32 << 20) >> PAGE_SHIFT)
543585cc5   David Rientjes   slab: rename slab...
1451
  		slab_max_order = SLAB_MAX_ORDER_HI;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1452

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1453
1454
  	/* Bootstrap is tricky, because several objects are allocated
  	 * from caches that do not exist yet:
9b030cb86   Christoph Lameter   mm/sl[aou]b: Use ...
1455
1456
1457
  	 * 1) initialize the kmem_cache cache: it contains the struct
  	 *    kmem_cache structures of all caches, except kmem_cache itself:
  	 *    kmem_cache is statically allocated.
e498be7da   Christoph Lameter   [PATCH] Numa-awar...
1458
  	 *    Initially an __init data area is used for the head array and the
ce8eb6c42   Christoph Lameter   slab: Rename list...
1459
  	 *    kmem_cache_node structures, it's replaced with a kmalloc allocated
e498be7da   Christoph Lameter   [PATCH] Numa-awar...
1460
  	 *    array at the end of the bootstrap.
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1461
  	 * 2) Create the first kmalloc cache.
343e0d7a9   Pekka Enberg   [PATCH] slab: rep...
1462
  	 *    The struct kmem_cache for the new cache is allocated normally.
e498be7da   Christoph Lameter   [PATCH] Numa-awar...
1463
1464
1465
  	 *    An __init data area is used for the head array.
  	 * 3) Create the remaining kmalloc caches, with minimally sized
  	 *    head arrays.
9b030cb86   Christoph Lameter   mm/sl[aou]b: Use ...
1466
  	 * 4) Replace the __init data head arrays for kmem_cache and the first
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1467
  	 *    kmalloc cache with kmalloc allocated arrays.
ce8eb6c42   Christoph Lameter   slab: Rename list...
1468
  	 * 5) Replace the __init data for kmem_cache_node for kmem_cache and
e498be7da   Christoph Lameter   [PATCH] Numa-awar...
1469
1470
  	 *    the other cache's with kmalloc allocated memory.
  	 * 6) Resize the head arrays of the kmalloc caches to their final sizes.
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1471
  	 */
9b030cb86   Christoph Lameter   mm/sl[aou]b: Use ...
1472
  	/* 1) create the kmem_cache */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1473

8da3430d8   Eric Dumazet   slab: NUMA kmem_c...
1474
  	/*
b56efcf0a   Eric Dumazet   slab: shrink size...
1475
  	 * struct kmem_cache size depends on nr_node_ids & nr_cpu_ids
8da3430d8   Eric Dumazet   slab: NUMA kmem_c...
1476
  	 */
2f9baa9fc   Christoph Lameter   slab: Use the new...
1477
1478
  	create_boot_cache(kmem_cache, "kmem_cache",
  		offsetof(struct kmem_cache, array[nr_cpu_ids]) +
6744f087b   Christoph Lameter   slab: Common name...
1479
  				  nr_node_ids * sizeof(struct kmem_cache_node *),
2f9baa9fc   Christoph Lameter   slab: Use the new...
1480
1481
  				  SLAB_HWCACHE_ALIGN);
  	list_add(&kmem_cache->list, &slab_caches);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1482
1483
  
  	/* 2+3) create the kmalloc caches */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1484

a737b3e2f   Andrew Morton   [PATCH] slab cleanup
1485
1486
  	/*
  	 * Initialize the caches that provide memory for the array cache and the
ce8eb6c42   Christoph Lameter   slab: Rename list...
1487
  	 * kmem_cache_node structures first.  Without this, further allocations will
a737b3e2f   Andrew Morton   [PATCH] slab cleanup
1488
  	 * bug.
e498be7da   Christoph Lameter   [PATCH] Numa-awar...
1489
  	 */
e33660165   Christoph Lameter   slab: Use common ...
1490
1491
  	kmalloc_caches[INDEX_AC] = create_kmalloc_cache("kmalloc-ac",
  					kmalloc_size(INDEX_AC), ARCH_KMALLOC_FLAGS);
45530c447   Christoph Lameter   mm, sl[au]b: crea...
1492

ce8eb6c42   Christoph Lameter   slab: Rename list...
1493
1494
1495
1496
  	if (INDEX_AC != INDEX_NODE)
  		kmalloc_caches[INDEX_NODE] =
  			create_kmalloc_cache("kmalloc-node",
  				kmalloc_size(INDEX_NODE), ARCH_KMALLOC_FLAGS);
e498be7da   Christoph Lameter   [PATCH] Numa-awar...
1497

e0a427267   Ingo Molnar   [PATCH] mm/slab.c...
1498
  	slab_early_init = 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1499
1500
  	/* 4) Replace the bootstrap head arrays */
  	{
2b2d5493e   Ingo Molnar   [PATCH] lockdep: ...
1501
  		struct array_cache *ptr;
e498be7da   Christoph Lameter   [PATCH] Numa-awar...
1502

83b519e8b   Pekka Enberg   slab: setup alloc...
1503
  		ptr = kmalloc(sizeof(struct arraycache_init), GFP_NOWAIT);
e498be7da   Christoph Lameter   [PATCH] Numa-awar...
1504

9b030cb86   Christoph Lameter   mm/sl[aou]b: Use ...
1505
  		memcpy(ptr, cpu_cache_get(kmem_cache),
b28a02de8   Pekka Enberg   [PATCH] slab: fix...
1506
  		       sizeof(struct arraycache_init));
2b2d5493e   Ingo Molnar   [PATCH] lockdep: ...
1507
1508
1509
1510
  		/*
  		 * Do not assume that spinlocks can be initialized via memcpy:
  		 */
  		spin_lock_init(&ptr->lock);
9b030cb86   Christoph Lameter   mm/sl[aou]b: Use ...
1511
  		kmem_cache->array[smp_processor_id()] = ptr;
e498be7da   Christoph Lameter   [PATCH] Numa-awar...
1512

83b519e8b   Pekka Enberg   slab: setup alloc...
1513
  		ptr = kmalloc(sizeof(struct arraycache_init), GFP_NOWAIT);
e498be7da   Christoph Lameter   [PATCH] Numa-awar...
1514

e33660165   Christoph Lameter   slab: Use common ...
1515
  		BUG_ON(cpu_cache_get(kmalloc_caches[INDEX_AC])
b28a02de8   Pekka Enberg   [PATCH] slab: fix...
1516
  		       != &initarray_generic.cache);
e33660165   Christoph Lameter   slab: Use common ...
1517
  		memcpy(ptr, cpu_cache_get(kmalloc_caches[INDEX_AC]),
b28a02de8   Pekka Enberg   [PATCH] slab: fix...
1518
  		       sizeof(struct arraycache_init));
2b2d5493e   Ingo Molnar   [PATCH] lockdep: ...
1519
1520
1521
1522
  		/*
  		 * Do not assume that spinlocks can be initialized via memcpy:
  		 */
  		spin_lock_init(&ptr->lock);
e33660165   Christoph Lameter   slab: Use common ...
1523
  		kmalloc_caches[INDEX_AC]->array[smp_processor_id()] = ptr;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1524
  	}
ce8eb6c42   Christoph Lameter   slab: Rename list...
1525
  	/* 5) Replace the bootstrap kmem_cache_node */
e498be7da   Christoph Lameter   [PATCH] Numa-awar...
1526
  	{
1ca4cb241   Pekka Enberg   [PATCH] slab: red...
1527
  		int nid;
9c09a95cf   Mel Gorman   slab: partially r...
1528
  		for_each_online_node(nid) {
ce8eb6c42   Christoph Lameter   slab: Rename list...
1529
  			init_list(kmem_cache, &init_kmem_cache_node[CACHE_CACHE + nid], nid);
556a169da   Pekka Enberg   slab: fix bootstr...
1530

e33660165   Christoph Lameter   slab: Use common ...
1531
  			init_list(kmalloc_caches[INDEX_AC],
ce8eb6c42   Christoph Lameter   slab: Rename list...
1532
  				  &init_kmem_cache_node[SIZE_AC + nid], nid);
e498be7da   Christoph Lameter   [PATCH] Numa-awar...
1533

ce8eb6c42   Christoph Lameter   slab: Rename list...
1534
1535
1536
  			if (INDEX_AC != INDEX_NODE) {
  				init_list(kmalloc_caches[INDEX_NODE],
  					  &init_kmem_cache_node[SIZE_NODE + nid], nid);
e498be7da   Christoph Lameter   [PATCH] Numa-awar...
1537
1538
1539
  			}
  		}
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1540

f97d5f634   Christoph Lameter   slab: Common func...
1541
  	create_kmalloc_caches(ARCH_KMALLOC_FLAGS);
8429db5c6   Pekka Enberg   slab: setup cpu c...
1542
1543
1544
1545
1546
  }
  
  void __init kmem_cache_init_late(void)
  {
  	struct kmem_cache *cachep;
97d066091   Christoph Lameter   mm, sl[aou]b: Com...
1547
  	slab_state = UP;
52cef1891   Peter Zijlstra   slab, lockdep: Fi...
1548

8429db5c6   Pekka Enberg   slab: setup cpu c...
1549
  	/* 6) resize the head arrays to their final sizes */
18004c5d4   Christoph Lameter   mm, sl[aou]b: Use...
1550
1551
  	mutex_lock(&slab_mutex);
  	list_for_each_entry(cachep, &slab_caches, list)
8429db5c6   Pekka Enberg   slab: setup cpu c...
1552
1553
  		if (enable_cpucache(cachep, GFP_NOWAIT))
  			BUG();
18004c5d4   Christoph Lameter   mm, sl[aou]b: Use...
1554
  	mutex_unlock(&slab_mutex);
056c62418   Ravikiran G Thirumalai   [PATCH] slab: fix...
1555

947ca1856   Michael Wang   slab: fix the DEA...
1556
1557
  	/* Annotate slab for lockdep -- annotate the malloc caches */
  	init_lock_keys();
97d066091   Christoph Lameter   mm, sl[aou]b: Com...
1558
1559
  	/* Done! */
  	slab_state = FULL;
a737b3e2f   Andrew Morton   [PATCH] slab cleanup
1560
1561
1562
  	/*
  	 * Register a cpu startup notifier callback that initializes
  	 * cpu_cache_get for all new cpus
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1563
1564
  	 */
  	register_cpu_notifier(&cpucache_notifier);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1565

8f9f8d9e8   David Rientjes   slab: add memory ...
1566
1567
1568
  #ifdef CONFIG_NUMA
  	/*
  	 * Register a memory hotplug callback that initializes and frees
6a67368c3   Christoph Lameter   slab: Rename node...
1569
  	 * node.
8f9f8d9e8   David Rientjes   slab: add memory ...
1570
1571
1572
  	 */
  	hotplug_memory_notifier(slab_memory_callback, SLAB_CALLBACK_PRI);
  #endif
a737b3e2f   Andrew Morton   [PATCH] slab cleanup
1573
1574
1575
  	/*
  	 * The reap timers are started later, with a module init call: That part
  	 * of the kernel is not yet operational.
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1576
1577
1578
1579
1580
1581
  	 */
  }
  
  static int __init cpucache_init(void)
  {
  	int cpu;
a737b3e2f   Andrew Morton   [PATCH] slab cleanup
1582
1583
  	/*
  	 * Register the timers that return unneeded pages to the page allocator
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1584
  	 */
e498be7da   Christoph Lameter   [PATCH] Numa-awar...
1585
  	for_each_online_cpu(cpu)
a737b3e2f   Andrew Morton   [PATCH] slab cleanup
1586
  		start_cpu_timer(cpu);
a164f8962   Glauber Costa   slab: move FULL s...
1587
1588
  
  	/* Done! */
97d066091   Christoph Lameter   mm, sl[aou]b: Com...
1589
  	slab_state = FULL;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1590
1591
  	return 0;
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1592
  __initcall(cpucache_init);
8bdec192b   Rafael Aquini   mm: SLAB Out-of-m...
1593
1594
1595
  static noinline void
  slab_out_of_memory(struct kmem_cache *cachep, gfp_t gfpflags, int nodeid)
  {
ce8eb6c42   Christoph Lameter   slab: Rename list...
1596
  	struct kmem_cache_node *n;
8bdec192b   Rafael Aquini   mm: SLAB Out-of-m...
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
  	struct slab *slabp;
  	unsigned long flags;
  	int node;
  
  	printk(KERN_WARNING
  		"SLAB: Unable to allocate memory on node %d (gfp=0x%x)
  ",
  		nodeid, gfpflags);
  	printk(KERN_WARNING "  cache: %s, object size: %d, order: %d
  ",
3b0efdfa1   Christoph Lameter   mm, sl[aou]b: Ext...
1607
  		cachep->name, cachep->size, cachep->gfporder);
8bdec192b   Rafael Aquini   mm: SLAB Out-of-m...
1608
1609
1610
1611
  
  	for_each_online_node(node) {
  		unsigned long active_objs = 0, num_objs = 0, free_objects = 0;
  		unsigned long active_slabs = 0, num_slabs = 0;
ce8eb6c42   Christoph Lameter   slab: Rename list...
1612
1613
  		n = cachep->node[node];
  		if (!n)
8bdec192b   Rafael Aquini   mm: SLAB Out-of-m...
1614
  			continue;
ce8eb6c42   Christoph Lameter   slab: Rename list...
1615
1616
  		spin_lock_irqsave(&n->list_lock, flags);
  		list_for_each_entry(slabp, &n->slabs_full, list) {
8bdec192b   Rafael Aquini   mm: SLAB Out-of-m...
1617
1618
1619
  			active_objs += cachep->num;
  			active_slabs++;
  		}
ce8eb6c42   Christoph Lameter   slab: Rename list...
1620
  		list_for_each_entry(slabp, &n->slabs_partial, list) {
8bdec192b   Rafael Aquini   mm: SLAB Out-of-m...
1621
1622
1623
  			active_objs += slabp->inuse;
  			active_slabs++;
  		}
ce8eb6c42   Christoph Lameter   slab: Rename list...
1624
  		list_for_each_entry(slabp, &n->slabs_free, list)
8bdec192b   Rafael Aquini   mm: SLAB Out-of-m...
1625
  			num_slabs++;
ce8eb6c42   Christoph Lameter   slab: Rename list...
1626
1627
  		free_objects += n->free_objects;
  		spin_unlock_irqrestore(&n->list_lock, flags);
8bdec192b   Rafael Aquini   mm: SLAB Out-of-m...
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
  
  		num_slabs += active_slabs;
  		num_objs = num_slabs * cachep->num;
  		printk(KERN_WARNING
  			"  node %d: slabs: %ld/%ld, objs: %ld/%ld, free: %ld
  ",
  			node, active_slabs, num_slabs, active_objs, num_objs,
  			free_objects);
  	}
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1638
1639
1640
1641
1642
1643
1644
  /*
   * Interface to system's page allocator. No need to hold the cache-lock.
   *
   * If we requested dmaable memory, we will get it. Even if we
   * did not request dmaable memory, we might get it, but that
   * would be relatively rare and ignorable.
   */
343e0d7a9   Pekka Enberg   [PATCH] slab: rep...
1645
  static void *kmem_getpages(struct kmem_cache *cachep, gfp_t flags, int nodeid)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1646
1647
  {
  	struct page *page;
e1b6aa6f1   Christoph Hellwig   [PATCH] slab: cle...
1648
  	int nr_pages;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1649
  	int i;
d6fef9da1   Luke Yang   [PATCH] nommu: us...
1650
  #ifndef CONFIG_MMU
e1b6aa6f1   Christoph Hellwig   [PATCH] slab: cle...
1651
1652
1653
  	/*
  	 * Nommu uses slab's for process anonymous memory allocations, and thus
  	 * requires __GFP_COMP to properly refcount higher order allocations
d6fef9da1   Luke Yang   [PATCH] nommu: us...
1654
  	 */
e1b6aa6f1   Christoph Hellwig   [PATCH] slab: cle...
1655
  	flags |= __GFP_COMP;
d6fef9da1   Luke Yang   [PATCH] nommu: us...
1656
  #endif
765c4507a   Christoph Lameter   [PATCH] GFP_THISN...
1657

a618e89f1   Glauber Costa   slab: rename gfpf...
1658
  	flags |= cachep->allocflags;
e12ba74d8   Mel Gorman   Group short-lived...
1659
1660
  	if (cachep->flags & SLAB_RECLAIM_ACCOUNT)
  		flags |= __GFP_RECLAIMABLE;
e1b6aa6f1   Christoph Hellwig   [PATCH] slab: cle...
1661

517d08699   Linus Torvalds   Merge branch 'akpm'
1662
  	page = alloc_pages_exact_node(nodeid, flags | __GFP_NOTRACK, cachep->gfporder);
8bdec192b   Rafael Aquini   mm: SLAB Out-of-m...
1663
1664
1665
  	if (!page) {
  		if (!(flags & __GFP_NOWARN) && printk_ratelimit())
  			slab_out_of_memory(cachep, flags, nodeid);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1666
  		return NULL;
8bdec192b   Rafael Aquini   mm: SLAB Out-of-m...
1667
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1668

b37f1dd0f   Mel Gorman   mm: introduce __G...
1669
  	/* Record if ALLOC_NO_WATERMARKS was set when allocating the slab */
072bb0aa5   Mel Gorman   mm: sl[au]b: add ...
1670
1671
  	if (unlikely(page->pfmemalloc))
  		pfmemalloc_active = true;
e1b6aa6f1   Christoph Hellwig   [PATCH] slab: cle...
1672
  	nr_pages = (1 << cachep->gfporder);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1673
  	if (cachep->flags & SLAB_RECLAIM_ACCOUNT)
972d1a7b1   Christoph Lameter   [PATCH] ZVC: Supp...
1674
1675
1676
1677
1678
  		add_zone_page_state(page_zone(page),
  			NR_SLAB_RECLAIMABLE, nr_pages);
  	else
  		add_zone_page_state(page_zone(page),
  			NR_SLAB_UNRECLAIMABLE, nr_pages);
072bb0aa5   Mel Gorman   mm: sl[au]b: add ...
1679
  	for (i = 0; i < nr_pages; i++) {
e1b6aa6f1   Christoph Hellwig   [PATCH] slab: cle...
1680
  		__SetPageSlab(page + i);
c175eea46   Pekka Enberg   slab: add hooks f...
1681

072bb0aa5   Mel Gorman   mm: sl[au]b: add ...
1682
1683
1684
  		if (page->pfmemalloc)
  			SetPageSlabPfmemalloc(page + i);
  	}
1f458cbf1   Glauber Costa   memcg: destroy me...
1685
  	memcg_bind_pages(cachep, cachep->gfporder);
072bb0aa5   Mel Gorman   mm: sl[au]b: add ...
1686

b1eeab676   Vegard Nossum   kmemcheck: add ho...
1687
1688
1689
1690
1691
1692
1693
1694
  	if (kmemcheck_enabled && !(cachep->flags & SLAB_NOTRACK)) {
  		kmemcheck_alloc_shadow(page, cachep->gfporder, flags, nodeid);
  
  		if (cachep->ctor)
  			kmemcheck_mark_uninitialized_pages(page, nr_pages);
  		else
  			kmemcheck_mark_unallocated_pages(page, nr_pages);
  	}
c175eea46   Pekka Enberg   slab: add hooks f...
1695

e1b6aa6f1   Christoph Hellwig   [PATCH] slab: cle...
1696
  	return page_address(page);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1697
1698
1699
1700
1701
  }
  
  /*
   * Interface to system's page release.
   */
343e0d7a9   Pekka Enberg   [PATCH] slab: rep...
1702
  static void kmem_freepages(struct kmem_cache *cachep, void *addr)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1703
  {
b28a02de8   Pekka Enberg   [PATCH] slab: fix...
1704
  	unsigned long i = (1 << cachep->gfporder);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1705
1706
  	struct page *page = virt_to_page(addr);
  	const unsigned long nr_freed = i;
b1eeab676   Vegard Nossum   kmemcheck: add ho...
1707
  	kmemcheck_free_shadow(page, cachep->gfporder);
c175eea46   Pekka Enberg   slab: add hooks f...
1708

972d1a7b1   Christoph Lameter   [PATCH] ZVC: Supp...
1709
1710
1711
1712
1713
1714
  	if (cachep->flags & SLAB_RECLAIM_ACCOUNT)
  		sub_zone_page_state(page_zone(page),
  				NR_SLAB_RECLAIMABLE, nr_freed);
  	else
  		sub_zone_page_state(page_zone(page),
  				NR_SLAB_UNRECLAIMABLE, nr_freed);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1715
  	while (i--) {
f205b2fe6   Nick Piggin   [PATCH] mm: slab ...
1716
  		BUG_ON(!PageSlab(page));
072bb0aa5   Mel Gorman   mm: sl[au]b: add ...
1717
  		__ClearPageSlabPfmemalloc(page);
f205b2fe6   Nick Piggin   [PATCH] mm: slab ...
1718
  		__ClearPageSlab(page);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1719
1720
  		page++;
  	}
1f458cbf1   Glauber Costa   memcg: destroy me...
1721
1722
  
  	memcg_release_pages(cachep, cachep->gfporder);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1723
1724
  	if (current->reclaim_state)
  		current->reclaim_state->reclaimed_slab += nr_freed;
d79923fad   Glauber Costa   sl[au]b: allocate...
1725
  	free_memcg_kmem_pages((unsigned long)addr, cachep->gfporder);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1726
1727
1728
1729
  }
  
  static void kmem_rcu_free(struct rcu_head *head)
  {
b28a02de8   Pekka Enberg   [PATCH] slab: fix...
1730
  	struct slab_rcu *slab_rcu = (struct slab_rcu *)head;
343e0d7a9   Pekka Enberg   [PATCH] slab: rep...
1731
  	struct kmem_cache *cachep = slab_rcu->cachep;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1732
1733
1734
1735
1736
1737
1738
1739
1740
  
  	kmem_freepages(cachep, slab_rcu->addr);
  	if (OFF_SLAB(cachep))
  		kmem_cache_free(cachep->slabp_cache, slab_rcu);
  }
  
  #if DEBUG
  
  #ifdef CONFIG_DEBUG_PAGEALLOC
343e0d7a9   Pekka Enberg   [PATCH] slab: rep...
1741
  static void store_stackinfo(struct kmem_cache *cachep, unsigned long *addr,
b28a02de8   Pekka Enberg   [PATCH] slab: fix...
1742
  			    unsigned long caller)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1743
  {
8c138bc00   Christoph Lameter   slab: Get rid of ...
1744
  	int size = cachep->object_size;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1745

3dafccf22   Manfred Spraul   [PATCH] slab: dis...
1746
  	addr = (unsigned long *)&((char *)addr)[obj_offset(cachep)];
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1747

b28a02de8   Pekka Enberg   [PATCH] slab: fix...
1748
  	if (size < 5 * sizeof(unsigned long))
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1749
  		return;
b28a02de8   Pekka Enberg   [PATCH] slab: fix...
1750
1751
1752
1753
  	*addr++ = 0x12345678;
  	*addr++ = caller;
  	*addr++ = smp_processor_id();
  	size -= 3 * sizeof(unsigned long);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1754
1755
1756
1757
1758
1759
1760
  	{
  		unsigned long *sptr = &caller;
  		unsigned long svalue;
  
  		while (!kstack_end(sptr)) {
  			svalue = *sptr++;
  			if (kernel_text_address(svalue)) {
b28a02de8   Pekka Enberg   [PATCH] slab: fix...
1761
  				*addr++ = svalue;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1762
1763
1764
1765
1766
1767
1768
  				size -= sizeof(unsigned long);
  				if (size <= sizeof(unsigned long))
  					break;
  			}
  		}
  
  	}
b28a02de8   Pekka Enberg   [PATCH] slab: fix...
1769
  	*addr++ = 0x87654321;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1770
1771
  }
  #endif
343e0d7a9   Pekka Enberg   [PATCH] slab: rep...
1772
  static void poison_obj(struct kmem_cache *cachep, void *addr, unsigned char val)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1773
  {
8c138bc00   Christoph Lameter   slab: Get rid of ...
1774
  	int size = cachep->object_size;
3dafccf22   Manfred Spraul   [PATCH] slab: dis...
1775
  	addr = &((char *)addr)[obj_offset(cachep)];
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1776
1777
  
  	memset(addr, val, size);
b28a02de8   Pekka Enberg   [PATCH] slab: fix...
1778
  	*(unsigned char *)(addr + size - 1) = POISON_END;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1779
1780
1781
1782
1783
  }
  
  static void dump_line(char *data, int offset, int limit)
  {
  	int i;
aa83aa40e   Dave Jones   [PATCH] single bi...
1784
1785
  	unsigned char error = 0;
  	int bad_count = 0;
fdde6abb3   Sebastian Andrzej Siewior   slab: use print_h...
1786
  	printk(KERN_ERR "%03x: ", offset);
aa83aa40e   Dave Jones   [PATCH] single bi...
1787
1788
1789
1790
1791
  	for (i = 0; i < limit; i++) {
  		if (data[offset + i] != POISON_FREE) {
  			error = data[offset + i];
  			bad_count++;
  		}
aa83aa40e   Dave Jones   [PATCH] single bi...
1792
  	}
fdde6abb3   Sebastian Andrzej Siewior   slab: use print_h...
1793
1794
  	print_hex_dump(KERN_CONT, "", 0, 16, 1,
  			&data[offset], limit, 1);
aa83aa40e   Dave Jones   [PATCH] single bi...
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
  
  	if (bad_count == 1) {
  		error ^= POISON_FREE;
  		if (!(error & (error - 1))) {
  			printk(KERN_ERR "Single bit error detected. Probably "
  					"bad RAM.
  ");
  #ifdef CONFIG_X86
  			printk(KERN_ERR "Run memtest86+ or a similar memory "
  					"test tool.
  ");
  #else
  			printk(KERN_ERR "Run a memory test tool.
  ");
  #endif
  		}
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1812
1813
1814
1815
  }
  #endif
  
  #if DEBUG
343e0d7a9   Pekka Enberg   [PATCH] slab: rep...
1816
  static void print_objinfo(struct kmem_cache *cachep, void *objp, int lines)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1817
1818
1819
1820
1821
  {
  	int i, size;
  	char *realobj;
  
  	if (cachep->flags & SLAB_RED_ZONE) {
b46b8f19c   David Woodhouse   Increase slab red...
1822
1823
  		printk(KERN_ERR "Redzone: 0x%llx/0x%llx.
  ",
a737b3e2f   Andrew Morton   [PATCH] slab cleanup
1824
1825
  			*dbg_redzone1(cachep, objp),
  			*dbg_redzone2(cachep, objp));
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1826
1827
1828
  	}
  
  	if (cachep->flags & SLAB_STORE_USER) {
071361d34   Joe Perches   mm: Convert print...
1829
1830
1831
1832
  		printk(KERN_ERR "Last user: [<%p>](%pSR)
  ",
  		       *dbg_userword(cachep, objp),
  		       *dbg_userword(cachep, objp));
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1833
  	}
3dafccf22   Manfred Spraul   [PATCH] slab: dis...
1834
  	realobj = (char *)objp + obj_offset(cachep);
8c138bc00   Christoph Lameter   slab: Get rid of ...
1835
  	size = cachep->object_size;
b28a02de8   Pekka Enberg   [PATCH] slab: fix...
1836
  	for (i = 0; i < size && lines; i += 16, lines--) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1837
1838
  		int limit;
  		limit = 16;
b28a02de8   Pekka Enberg   [PATCH] slab: fix...
1839
1840
  		if (i + limit > size)
  			limit = size - i;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1841
1842
1843
  		dump_line(realobj, i, limit);
  	}
  }
343e0d7a9   Pekka Enberg   [PATCH] slab: rep...
1844
  static void check_poison_obj(struct kmem_cache *cachep, void *objp)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1845
1846
1847
1848
  {
  	char *realobj;
  	int size, i;
  	int lines = 0;
3dafccf22   Manfred Spraul   [PATCH] slab: dis...
1849
  	realobj = (char *)objp + obj_offset(cachep);
8c138bc00   Christoph Lameter   slab: Get rid of ...
1850
  	size = cachep->object_size;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1851

b28a02de8   Pekka Enberg   [PATCH] slab: fix...
1852
  	for (i = 0; i < size; i++) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1853
  		char exp = POISON_FREE;
b28a02de8   Pekka Enberg   [PATCH] slab: fix...
1854
  		if (i == size - 1)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1855
1856
1857
1858
1859
1860
  			exp = POISON_END;
  		if (realobj[i] != exp) {
  			int limit;
  			/* Mismatch ! */
  			/* Print header */
  			if (lines == 0) {
b28a02de8   Pekka Enberg   [PATCH] slab: fix...
1861
  				printk(KERN_ERR
face37f5e   Dave Jones   slab: add taint f...
1862
1863
1864
  					"Slab corruption (%s): %s start=%p, len=%d
  ",
  					print_tainted(), cachep->name, realobj, size);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1865
1866
1867
  				print_objinfo(cachep, objp, 0);
  			}
  			/* Hexdump the affected line */
b28a02de8   Pekka Enberg   [PATCH] slab: fix...
1868
  			i = (i / 16) * 16;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1869
  			limit = 16;
b28a02de8   Pekka Enberg   [PATCH] slab: fix...
1870
1871
  			if (i + limit > size)
  				limit = size - i;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
  			dump_line(realobj, i, limit);
  			i += 16;
  			lines++;
  			/* Limit to 5 lines */
  			if (lines > 5)
  				break;
  		}
  	}
  	if (lines != 0) {
  		/* Print some data about the neighboring objects, if they
  		 * exist:
  		 */
6ed5eb221   Pekka Enberg   [PATCH] slab: ext...
1884
  		struct slab *slabp = virt_to_slab(objp);
8fea4e96a   Pekka Enberg   [PATCH] slab: obj...
1885
  		unsigned int objnr;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1886

8fea4e96a   Pekka Enberg   [PATCH] slab: obj...
1887
  		objnr = obj_to_index(cachep, slabp, objp);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1888
  		if (objnr) {
8fea4e96a   Pekka Enberg   [PATCH] slab: obj...
1889
  			objp = index_to_obj(cachep, slabp, objnr - 1);
3dafccf22   Manfred Spraul   [PATCH] slab: dis...
1890
  			realobj = (char *)objp + obj_offset(cachep);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1891
1892
  			printk(KERN_ERR "Prev obj: start=%p, len=%d
  ",
b28a02de8   Pekka Enberg   [PATCH] slab: fix...
1893
  			       realobj, size);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1894
1895
  			print_objinfo(cachep, objp, 2);
  		}
b28a02de8   Pekka Enberg   [PATCH] slab: fix...
1896
  		if (objnr + 1 < cachep->num) {
8fea4e96a   Pekka Enberg   [PATCH] slab: obj...
1897
  			objp = index_to_obj(cachep, slabp, objnr + 1);
3dafccf22   Manfred Spraul   [PATCH] slab: dis...
1898
  			realobj = (char *)objp + obj_offset(cachep);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1899
1900
  			printk(KERN_ERR "Next obj: start=%p, len=%d
  ",
b28a02de8   Pekka Enberg   [PATCH] slab: fix...
1901
  			       realobj, size);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1902
1903
1904
1905
1906
  			print_objinfo(cachep, objp, 2);
  		}
  	}
  }
  #endif
12dd36fae   Matthew Dobson   [PATCH] slab: ext...
1907
  #if DEBUG
e79aec291   Rabin Vincent   slab: rename slab...
1908
  static void slab_destroy_debugcheck(struct kmem_cache *cachep, struct slab *slabp)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1909
  {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1910
1911
  	int i;
  	for (i = 0; i < cachep->num; i++) {
8fea4e96a   Pekka Enberg   [PATCH] slab: obj...
1912
  		void *objp = index_to_obj(cachep, slabp, i);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1913
1914
1915
  
  		if (cachep->flags & SLAB_POISON) {
  #ifdef CONFIG_DEBUG_PAGEALLOC
3b0efdfa1   Christoph Lameter   mm, sl[aou]b: Ext...
1916
  			if (cachep->size % PAGE_SIZE == 0 &&
a737b3e2f   Andrew Morton   [PATCH] slab cleanup
1917
  					OFF_SLAB(cachep))
b28a02de8   Pekka Enberg   [PATCH] slab: fix...
1918
  				kernel_map_pages(virt_to_page(objp),
3b0efdfa1   Christoph Lameter   mm, sl[aou]b: Ext...
1919
  					cachep->size / PAGE_SIZE, 1);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1920
1921
1922
1923
1924
1925
1926
1927
1928
  			else
  				check_poison_obj(cachep, objp);
  #else
  			check_poison_obj(cachep, objp);
  #endif
  		}
  		if (cachep->flags & SLAB_RED_ZONE) {
  			if (*dbg_redzone1(cachep, objp) != RED_INACTIVE)
  				slab_error(cachep, "start of a freed object "
b28a02de8   Pekka Enberg   [PATCH] slab: fix...
1929
  					   "was overwritten");
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1930
1931
  			if (*dbg_redzone2(cachep, objp) != RED_INACTIVE)
  				slab_error(cachep, "end of a freed object "
b28a02de8   Pekka Enberg   [PATCH] slab: fix...
1932
  					   "was overwritten");
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1933
  		}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1934
  	}
12dd36fae   Matthew Dobson   [PATCH] slab: ext...
1935
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1936
  #else
e79aec291   Rabin Vincent   slab: rename slab...
1937
  static void slab_destroy_debugcheck(struct kmem_cache *cachep, struct slab *slabp)
12dd36fae   Matthew Dobson   [PATCH] slab: ext...
1938
  {
12dd36fae   Matthew Dobson   [PATCH] slab: ext...
1939
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1940
  #endif
911851e6e   Randy Dunlap   [PATCH] slab: fix...
1941
1942
1943
1944
1945
  /**
   * slab_destroy - destroy and release all objects in a slab
   * @cachep: cache pointer being destroyed
   * @slabp: slab pointer being destroyed
   *
12dd36fae   Matthew Dobson   [PATCH] slab: ext...
1946
   * Destroy all the objs in a slab, and release the mem back to the system.
a737b3e2f   Andrew Morton   [PATCH] slab cleanup
1947
1948
   * Before calling the slab must have been unlinked from the cache.  The
   * cache-lock is not held/needed.
12dd36fae   Matthew Dobson   [PATCH] slab: ext...
1949
   */
343e0d7a9   Pekka Enberg   [PATCH] slab: rep...
1950
  static void slab_destroy(struct kmem_cache *cachep, struct slab *slabp)
12dd36fae   Matthew Dobson   [PATCH] slab: ext...
1951
1952
  {
  	void *addr = slabp->s_mem - slabp->colouroff;
e79aec291   Rabin Vincent   slab: rename slab...
1953
  	slab_destroy_debugcheck(cachep, slabp);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1954
1955
  	if (unlikely(cachep->flags & SLAB_DESTROY_BY_RCU)) {
  		struct slab_rcu *slab_rcu;
b28a02de8   Pekka Enberg   [PATCH] slab: fix...
1956
  		slab_rcu = (struct slab_rcu *)slabp;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1957
1958
1959
1960
1961
  		slab_rcu->cachep = cachep;
  		slab_rcu->addr = addr;
  		call_rcu(&slab_rcu->head, kmem_rcu_free);
  	} else {
  		kmem_freepages(cachep, addr);
873623dfa   Ingo Molnar   [PATCH] lockdep: ...
1962
1963
  		if (OFF_SLAB(cachep))
  			kmem_cache_free(cachep->slabp_cache, slabp);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1964
1965
1966
1967
  	}
  }
  
  /**
a70773ddb   Randy.Dunlap   [PATCH] mm/slab: ...
1968
1969
1970
1971
1972
1973
1974
   * calculate_slab_order - calculate size (page order) of slabs
   * @cachep: pointer to the cache that is being created
   * @size: size of objects to be created in this cache.
   * @align: required alignment for the objects.
   * @flags: slab allocation flags
   *
   * Also calculates the number of objects per slab.
4d268eba1   Pekka Enberg   [PATCH] slab: ext...
1975
1976
1977
1978
1979
   *
   * This could be made much more intelligent.  For now, try to avoid using
   * high order pages for slabs.  When the gfp() functions are more friendly
   * towards high-order requests, this should be changed.
   */
a737b3e2f   Andrew Morton   [PATCH] slab cleanup
1980
  static size_t calculate_slab_order(struct kmem_cache *cachep,
ee13d785e   Randy Dunlap   [PATCH] slab: fix...
1981
  			size_t size, size_t align, unsigned long flags)
4d268eba1   Pekka Enberg   [PATCH] slab: ext...
1982
  {
b1ab41c49   Ingo Molnar   [PATCH] slab.c: f...
1983
  	unsigned long offslab_limit;
4d268eba1   Pekka Enberg   [PATCH] slab: ext...
1984
  	size_t left_over = 0;
9888e6fa7   Linus Torvalds   slab: clarify and...
1985
  	int gfporder;
4d268eba1   Pekka Enberg   [PATCH] slab: ext...
1986

0aa817f07   Christoph Lameter   Slab allocators: ...
1987
  	for (gfporder = 0; gfporder <= KMALLOC_MAX_ORDER; gfporder++) {
4d268eba1   Pekka Enberg   [PATCH] slab: ext...
1988
1989
  		unsigned int num;
  		size_t remainder;
9888e6fa7   Linus Torvalds   slab: clarify and...
1990
  		cache_estimate(gfporder, size, align, flags, &remainder, &num);
4d268eba1   Pekka Enberg   [PATCH] slab: ext...
1991
1992
  		if (!num)
  			continue;
9888e6fa7   Linus Torvalds   slab: clarify and...
1993

b1ab41c49   Ingo Molnar   [PATCH] slab.c: f...
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
  		if (flags & CFLGS_OFF_SLAB) {
  			/*
  			 * Max number of objs-per-slab for caches which
  			 * use off-slab slabs. Needed to avoid a possible
  			 * looping condition in cache_grow().
  			 */
  			offslab_limit = size - sizeof(struct slab);
  			offslab_limit /= sizeof(kmem_bufctl_t);
  
   			if (num > offslab_limit)
  				break;
  		}
4d268eba1   Pekka Enberg   [PATCH] slab: ext...
2006

9888e6fa7   Linus Torvalds   slab: clarify and...
2007
  		/* Found something acceptable - save it away */
4d268eba1   Pekka Enberg   [PATCH] slab: ext...
2008
  		cachep->num = num;
9888e6fa7   Linus Torvalds   slab: clarify and...
2009
  		cachep->gfporder = gfporder;
4d268eba1   Pekka Enberg   [PATCH] slab: ext...
2010
2011
2012
  		left_over = remainder;
  
  		/*
f78bb8ad4   Linus Torvalds   slab: fix calcula...
2013
2014
2015
2016
2017
2018
2019
2020
  		 * A VFS-reclaimable slab tends to have most allocations
  		 * as GFP_NOFS and we really don't want to have to be allocating
  		 * higher-order pages when we are unable to shrink dcache.
  		 */
  		if (flags & SLAB_RECLAIM_ACCOUNT)
  			break;
  
  		/*
4d268eba1   Pekka Enberg   [PATCH] slab: ext...
2021
2022
2023
  		 * Large number of objects is good, but very large slabs are
  		 * currently bad for the gfp()s.
  		 */
543585cc5   David Rientjes   slab: rename slab...
2024
  		if (gfporder >= slab_max_order)
4d268eba1   Pekka Enberg   [PATCH] slab: ext...
2025
  			break;
9888e6fa7   Linus Torvalds   slab: clarify and...
2026
2027
2028
  		/*
  		 * Acceptable internal fragmentation?
  		 */
a737b3e2f   Andrew Morton   [PATCH] slab cleanup
2029
  		if (left_over * 8 <= (PAGE_SIZE << gfporder))
4d268eba1   Pekka Enberg   [PATCH] slab: ext...
2030
2031
2032
2033
  			break;
  	}
  	return left_over;
  }
83b519e8b   Pekka Enberg   slab: setup alloc...
2034
  static int __init_refok setup_cpu_cache(struct kmem_cache *cachep, gfp_t gfp)
f30cf7d13   Pekka Enberg   [PATCH] slab: ext...
2035
  {
97d066091   Christoph Lameter   mm, sl[aou]b: Com...
2036
  	if (slab_state >= FULL)
83b519e8b   Pekka Enberg   slab: setup alloc...
2037
  		return enable_cpucache(cachep, gfp);
2ed3a4ef9   Christoph Lameter   [PATCH] slab: do ...
2038

97d066091   Christoph Lameter   mm, sl[aou]b: Com...
2039
  	if (slab_state == DOWN) {
f30cf7d13   Pekka Enberg   [PATCH] slab: ext...
2040
  		/*
2f9baa9fc   Christoph Lameter   slab: Use the new...
2041
  		 * Note: Creation of first cache (kmem_cache).
ce8eb6c42   Christoph Lameter   slab: Rename list...
2042
  		 * The setup_node is taken care
2f9baa9fc   Christoph Lameter   slab: Use the new...
2043
2044
2045
2046
2047
2048
2049
  		 * of by the caller of __kmem_cache_create
  		 */
  		cachep->array[smp_processor_id()] = &initarray_generic.cache;
  		slab_state = PARTIAL;
  	} else if (slab_state == PARTIAL) {
  		/*
  		 * Note: the second kmem_cache_create must create the cache
f30cf7d13   Pekka Enberg   [PATCH] slab: ext...
2050
2051
2052
2053
2054
2055
  		 * that's used by kmalloc(24), otherwise the creation of
  		 * further caches will BUG().
  		 */
  		cachep->array[smp_processor_id()] = &initarray_generic.cache;
  
  		/*
ce8eb6c42   Christoph Lameter   slab: Rename list...
2056
2057
  		 * If the cache that's used by kmalloc(sizeof(kmem_cache_node)) is
  		 * the second cache, then we need to set up all its node/,
f30cf7d13   Pekka Enberg   [PATCH] slab: ext...
2058
2059
  		 * otherwise the creation of further caches will BUG().
  		 */
ce8eb6c42   Christoph Lameter   slab: Rename list...
2060
2061
2062
  		set_up_node(cachep, SIZE_AC);
  		if (INDEX_AC == INDEX_NODE)
  			slab_state = PARTIAL_NODE;
f30cf7d13   Pekka Enberg   [PATCH] slab: ext...
2063
  		else
97d066091   Christoph Lameter   mm, sl[aou]b: Com...
2064
  			slab_state = PARTIAL_ARRAYCACHE;
f30cf7d13   Pekka Enberg   [PATCH] slab: ext...
2065
  	} else {
2f9baa9fc   Christoph Lameter   slab: Use the new...
2066
  		/* Remaining boot caches */
f30cf7d13   Pekka Enberg   [PATCH] slab: ext...
2067
  		cachep->array[smp_processor_id()] =
83b519e8b   Pekka Enberg   slab: setup alloc...
2068
  			kmalloc(sizeof(struct arraycache_init), gfp);
f30cf7d13   Pekka Enberg   [PATCH] slab: ext...
2069

97d066091   Christoph Lameter   mm, sl[aou]b: Com...
2070
  		if (slab_state == PARTIAL_ARRAYCACHE) {
ce8eb6c42   Christoph Lameter   slab: Rename list...
2071
2072
  			set_up_node(cachep, SIZE_NODE);
  			slab_state = PARTIAL_NODE;
f30cf7d13   Pekka Enberg   [PATCH] slab: ext...
2073
2074
  		} else {
  			int node;
556a169da   Pekka Enberg   slab: fix bootstr...
2075
  			for_each_online_node(node) {
6a67368c3   Christoph Lameter   slab: Rename node...
2076
  				cachep->node[node] =
6744f087b   Christoph Lameter   slab: Common name...
2077
  				    kmalloc_node(sizeof(struct kmem_cache_node),
eb91f1d0a   Pekka Enberg   slab: fix gfp fla...
2078
  						gfp, node);
6a67368c3   Christoph Lameter   slab: Rename node...
2079
  				BUG_ON(!cachep->node[node]);
ce8eb6c42   Christoph Lameter   slab: Rename list...
2080
  				kmem_cache_node_init(cachep->node[node]);
f30cf7d13   Pekka Enberg   [PATCH] slab: ext...
2081
2082
2083
  			}
  		}
  	}
6a67368c3   Christoph Lameter   slab: Rename node...
2084
  	cachep->node[numa_mem_id()]->next_reap =
f30cf7d13   Pekka Enberg   [PATCH] slab: ext...
2085
2086
2087
2088
2089
2090
2091
2092
2093
  			jiffies + REAPTIMEOUT_LIST3 +
  			((unsigned long)cachep) % REAPTIMEOUT_LIST3;
  
  	cpu_cache_get(cachep)->avail = 0;
  	cpu_cache_get(cachep)->limit = BOOT_CPUCACHE_ENTRIES;
  	cpu_cache_get(cachep)->batchcount = 1;
  	cpu_cache_get(cachep)->touched = 0;
  	cachep->batchcount = 1;
  	cachep->limit = BOOT_CPUCACHE_ENTRIES;
2ed3a4ef9   Christoph Lameter   [PATCH] slab: do ...
2094
  	return 0;
f30cf7d13   Pekka Enberg   [PATCH] slab: ext...
2095
  }
4d268eba1   Pekka Enberg   [PATCH] slab: ext...
2096
  /**
039363f38   Christoph Lameter   mm, sl[aou]b: Ext...
2097
   * __kmem_cache_create - Create a cache.
a755b76ab   Randy Dunlap   mm: fix slab.c ke...
2098
   * @cachep: cache management descriptor
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2099
   * @flags: SLAB flags
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2100
2101
2102
   *
   * Returns a ptr to the cache on success, NULL on failure.
   * Cannot be called within a int, but can be interrupted.
20c2df83d   Paul Mundt   mm: Remove slab d...
2103
   * The @ctor is run when new pages are allocated by the cache.
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2104
   *
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2105
2106
2107
2108
2109
2110
2111
2112
   * The flags are
   *
   * %SLAB_POISON - Poison the slab with a known test pattern (a5a5a5a5)
   * to catch references to uninitialised memory.
   *
   * %SLAB_RED_ZONE - Insert `Red' zones around the allocated memory to check
   * for buffer overruns.
   *
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2113
2114
2115
2116
   * %SLAB_HWCACHE_ALIGN - Align the objects in this cache to a hardware
   * cacheline.  This can be beneficial if you're counting cycles as closely
   * as davem.
   */
278b1bb13   Christoph Lameter   mm/sl[aou]b: Move...
2117
  int
8a13a4cc8   Christoph Lameter   mm/sl[aou]b: Shri...
2118
  __kmem_cache_create (struct kmem_cache *cachep, unsigned long flags)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2119
2120
  {
  	size_t left_over, slab_size, ralign;
83b519e8b   Pekka Enberg   slab: setup alloc...
2121
  	gfp_t gfp;
278b1bb13   Christoph Lameter   mm/sl[aou]b: Move...
2122
  	int err;
8a13a4cc8   Christoph Lameter   mm/sl[aou]b: Shri...
2123
  	size_t size = cachep->size;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2124

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2125
  #if DEBUG
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2126
2127
2128
2129
2130
2131
2132
  #if FORCED_DEBUG
  	/*
  	 * Enable redzoning and last user accounting, except for caches with
  	 * large objects, if the increased size would increase the object size
  	 * above the next power of two: caches with object sizes just above a
  	 * power of two have a significant amount of internal fragmentation.
  	 */
87a927c71   David Woodhouse   Fix slab redzone ...
2133
2134
  	if (size < 4096 || fls(size - 1) == fls(size-1 + REDZONE_ALIGN +
  						2 * sizeof(unsigned long long)))
b28a02de8   Pekka Enberg   [PATCH] slab: fix...
2135
  		flags |= SLAB_RED_ZONE | SLAB_STORE_USER;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2136
2137
2138
2139
2140
2141
  	if (!(flags & SLAB_DESTROY_BY_RCU))
  		flags |= SLAB_POISON;
  #endif
  	if (flags & SLAB_DESTROY_BY_RCU)
  		BUG_ON(flags & SLAB_POISON);
  #endif
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2142

a737b3e2f   Andrew Morton   [PATCH] slab cleanup
2143
2144
  	/*
  	 * Check that size is in terms of words.  This is needed to avoid
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2145
2146
2147
  	 * unaligned accesses for some archs when redzoning is used, and makes
  	 * sure any on-slab bufctl's are also correctly aligned.
  	 */
b28a02de8   Pekka Enberg   [PATCH] slab: fix...
2148
2149
2150
  	if (size & (BYTES_PER_WORD - 1)) {
  		size += (BYTES_PER_WORD - 1);
  		size &= ~(BYTES_PER_WORD - 1);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2151
  	}
ca5f9703d   Pekka Enberg   [PATCH] slab: res...
2152
  	/*
87a927c71   David Woodhouse   Fix slab redzone ...
2153
2154
2155
  	 * Redzoning and user store require word alignment or possibly larger.
  	 * Note this will be overridden by architecture or caller mandated
  	 * alignment if either is greater than BYTES_PER_WORD.
ca5f9703d   Pekka Enberg   [PATCH] slab: res...
2156
  	 */
87a927c71   David Woodhouse   Fix slab redzone ...
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
  	if (flags & SLAB_STORE_USER)
  		ralign = BYTES_PER_WORD;
  
  	if (flags & SLAB_RED_ZONE) {
  		ralign = REDZONE_ALIGN;
  		/* If redzoning, ensure that the second redzone is suitably
  		 * aligned, by adjusting the object size accordingly. */
  		size += REDZONE_ALIGN - 1;
  		size &= ~(REDZONE_ALIGN - 1);
  	}
ca5f9703d   Pekka Enberg   [PATCH] slab: res...
2167

a44b56d35   Kevin Hilman   [PATCH] slab debu...
2168
  	/* 3) caller mandated alignment */
8a13a4cc8   Christoph Lameter   mm/sl[aou]b: Shri...
2169
2170
  	if (ralign < cachep->align) {
  		ralign = cachep->align;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2171
  	}
3ff84a7f3   Pekka Enberg   Revert "slab: Fix...
2172
2173
  	/* disable debug if necessary */
  	if (ralign > __alignof__(unsigned long long))
a44b56d35   Kevin Hilman   [PATCH] slab debu...
2174
  		flags &= ~(SLAB_RED_ZONE | SLAB_STORE_USER);
a737b3e2f   Andrew Morton   [PATCH] slab cleanup
2175
  	/*
ca5f9703d   Pekka Enberg   [PATCH] slab: res...
2176
  	 * 4) Store it.
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2177
  	 */
8a13a4cc8   Christoph Lameter   mm/sl[aou]b: Shri...
2178
  	cachep->align = ralign;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2179

83b519e8b   Pekka Enberg   slab: setup alloc...
2180
2181
2182
2183
  	if (slab_is_available())
  		gfp = GFP_KERNEL;
  	else
  		gfp = GFP_NOWAIT;
6a67368c3   Christoph Lameter   slab: Rename node...
2184
  	setup_node_pointer(cachep);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2185
  #if DEBUG
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2186

ca5f9703d   Pekka Enberg   [PATCH] slab: res...
2187
2188
2189
2190
  	/*
  	 * Both debugging options require word-alignment which is calculated
  	 * into align above.
  	 */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2191
  	if (flags & SLAB_RED_ZONE) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2192
  		/* add space for red zone words */
3ff84a7f3   Pekka Enberg   Revert "slab: Fix...
2193
2194
  		cachep->obj_offset += sizeof(unsigned long long);
  		size += 2 * sizeof(unsigned long long);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2195
2196
  	}
  	if (flags & SLAB_STORE_USER) {
ca5f9703d   Pekka Enberg   [PATCH] slab: res...
2197
  		/* user store requires one word storage behind the end of
87a927c71   David Woodhouse   Fix slab redzone ...
2198
2199
  		 * the real object. But if the second red zone needs to be
  		 * aligned to 64 bits, we must allow that much space.
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2200
  		 */
87a927c71   David Woodhouse   Fix slab redzone ...
2201
2202
2203
2204
  		if (flags & SLAB_RED_ZONE)
  			size += REDZONE_ALIGN;
  		else
  			size += BYTES_PER_WORD;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2205
2206
  	}
  #if FORCED_DEBUG && defined(CONFIG_DEBUG_PAGEALLOC)
ce8eb6c42   Christoph Lameter   slab: Rename list...
2207
  	if (size >= kmalloc_size(INDEX_NODE + 1)
608da7e3f   Tetsuo Handa   slab: Fix build f...
2208
2209
2210
  	    && cachep->object_size > cache_line_size()
  	    && ALIGN(size, cachep->align) < PAGE_SIZE) {
  		cachep->obj_offset += PAGE_SIZE - ALIGN(size, cachep->align);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2211
2212
2213
2214
  		size = PAGE_SIZE;
  	}
  #endif
  #endif
e0a427267   Ingo Molnar   [PATCH] mm/slab.c...
2215
2216
2217
  	/*
  	 * Determine if the slab management is 'on' or 'off' slab.
  	 * (bootstrapping cannot cope with offslab caches so don't do
e7cb55b94   Catalin Marinas   kmemleak: Do not ...
2218
2219
  	 * it too early on. Always use on-slab management when
  	 * SLAB_NOLEAKTRACE to avoid recursive calls into kmemleak)
e0a427267   Ingo Molnar   [PATCH] mm/slab.c...
2220
  	 */
e7cb55b94   Catalin Marinas   kmemleak: Do not ...
2221
2222
  	if ((size >= (PAGE_SIZE >> 3)) && !slab_early_init &&
  	    !(flags & SLAB_NOLEAKTRACE))
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2223
2224
2225
2226
2227
  		/*
  		 * Size is large, assume best to place the slab management obj
  		 * off-slab (should allow better packing of objs).
  		 */
  		flags |= CFLGS_OFF_SLAB;
8a13a4cc8   Christoph Lameter   mm/sl[aou]b: Shri...
2228
  	size = ALIGN(size, cachep->align);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2229

8a13a4cc8   Christoph Lameter   mm/sl[aou]b: Shri...
2230
  	left_over = calculate_slab_order(cachep, size, cachep->align, flags);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2231

8a13a4cc8   Christoph Lameter   mm/sl[aou]b: Shri...
2232
  	if (!cachep->num)
278b1bb13   Christoph Lameter   mm/sl[aou]b: Move...
2233
  		return -E2BIG;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2234

b28a02de8   Pekka Enberg   [PATCH] slab: fix...
2235
  	slab_size = ALIGN(cachep->num * sizeof(kmem_bufctl_t)
8a13a4cc8   Christoph Lameter   mm/sl[aou]b: Shri...
2236
  			  + sizeof(struct slab), cachep->align);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
  
  	/*
  	 * If the slab has been placed off-slab, and we have enough space then
  	 * move it on-slab. This is at the expense of any extra colouring.
  	 */
  	if (flags & CFLGS_OFF_SLAB && left_over >= slab_size) {
  		flags &= ~CFLGS_OFF_SLAB;
  		left_over -= slab_size;
  	}
  
  	if (flags & CFLGS_OFF_SLAB) {
  		/* really off slab. No need for manual alignment */
b28a02de8   Pekka Enberg   [PATCH] slab: fix...
2249
2250
  		slab_size =
  		    cachep->num * sizeof(kmem_bufctl_t) + sizeof(struct slab);
674613652   Ron Lee   slab: fix generic...
2251
2252
2253
2254
2255
2256
2257
2258
2259
  
  #ifdef CONFIG_PAGE_POISONING
  		/* If we're going to use the generic kernel_map_pages()
  		 * poisoning, then it's going to smash the contents of
  		 * the redzone and userword anyhow, so switch them off.
  		 */
  		if (size % PAGE_SIZE == 0 && flags & SLAB_POISON)
  			flags &= ~(SLAB_RED_ZONE | SLAB_STORE_USER);
  #endif
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2260
2261
2262
2263
  	}
  
  	cachep->colour_off = cache_line_size();
  	/* Offset must be a multiple of the alignment. */
8a13a4cc8   Christoph Lameter   mm/sl[aou]b: Shri...
2264
2265
  	if (cachep->colour_off < cachep->align)
  		cachep->colour_off = cachep->align;
b28a02de8   Pekka Enberg   [PATCH] slab: fix...
2266
  	cachep->colour = left_over / cachep->colour_off;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2267
2268
  	cachep->slab_size = slab_size;
  	cachep->flags = flags;
a618e89f1   Glauber Costa   slab: rename gfpf...
2269
  	cachep->allocflags = 0;
4b51d6698   Christoph Lameter   [PATCH] optional ...
2270
  	if (CONFIG_ZONE_DMA_FLAG && (flags & SLAB_CACHE_DMA))
a618e89f1   Glauber Costa   slab: rename gfpf...
2271
  		cachep->allocflags |= GFP_DMA;
3b0efdfa1   Christoph Lameter   mm, sl[aou]b: Ext...
2272
  	cachep->size = size;
6a2d7a955   Eric Dumazet   [PATCH] SLAB: use...
2273
  	cachep->reciprocal_buffer_size = reciprocal_value(size);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2274

e5ac9c5ae   Ravikiran G Thirumalai   [PATCH] Add some ...
2275
  	if (flags & CFLGS_OFF_SLAB) {
2c59dd654   Christoph Lameter   slab: Common Kmal...
2276
  		cachep->slabp_cache = kmalloc_slab(slab_size, 0u);
e5ac9c5ae   Ravikiran G Thirumalai   [PATCH] Add some ...
2277
2278
2279
2280
2281
2282
2283
  		/*
  		 * This is a possibility for one of the malloc_sizes caches.
  		 * But since we go off slab only for object size greater than
  		 * PAGE_SIZE/8, and malloc_sizes gets created in ascending order,
  		 * this should not happen at all.
  		 * But leave a BUG_ON for some lucky dude.
  		 */
6cb8f9132   Christoph Lameter   Slab allocators: ...
2284
  		BUG_ON(ZERO_OR_NULL_PTR(cachep->slabp_cache));
e5ac9c5ae   Ravikiran G Thirumalai   [PATCH] Add some ...
2285
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2286

278b1bb13   Christoph Lameter   mm/sl[aou]b: Move...
2287
2288
  	err = setup_cpu_cache(cachep, gfp);
  	if (err) {
12c3667fb   Christoph Lameter   mm/sl[aou]b: Get ...
2289
  		__kmem_cache_shutdown(cachep);
278b1bb13   Christoph Lameter   mm/sl[aou]b: Move...
2290
  		return err;
2ed3a4ef9   Christoph Lameter   [PATCH] slab: do ...
2291
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2292

83835b3d9   Peter Zijlstra   slab, lockdep: An...
2293
2294
2295
2296
2297
2298
2299
2300
  	if (flags & SLAB_DEBUG_OBJECTS) {
  		/*
  		 * Would deadlock through slab_destroy()->call_rcu()->
  		 * debug_object_activate()->kmem_cache_alloc().
  		 */
  		WARN_ON_ONCE(flags & SLAB_DESTROY_BY_RCU);
  
  		slab_set_debugobj_lock_classes(cachep);
6ccfb5bcf   Glauber Costa   slab: annotate on...
2301
2302
  	} else if (!OFF_SLAB(cachep) && !(flags & SLAB_DESTROY_BY_RCU))
  		on_slab_lock_classes(cachep);
83835b3d9   Peter Zijlstra   slab, lockdep: An...
2303

278b1bb13   Christoph Lameter   mm/sl[aou]b: Move...
2304
  	return 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2305
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
  
  #if DEBUG
  static void check_irq_off(void)
  {
  	BUG_ON(!irqs_disabled());
  }
  
  static void check_irq_on(void)
  {
  	BUG_ON(irqs_disabled());
  }
343e0d7a9   Pekka Enberg   [PATCH] slab: rep...
2317
  static void check_spinlock_acquired(struct kmem_cache *cachep)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2318
2319
2320
  {
  #ifdef CONFIG_SMP
  	check_irq_off();
6a67368c3   Christoph Lameter   slab: Rename node...
2321
  	assert_spin_locked(&cachep->node[numa_mem_id()]->list_lock);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2322
2323
  #endif
  }
e498be7da   Christoph Lameter   [PATCH] Numa-awar...
2324

343e0d7a9   Pekka Enberg   [PATCH] slab: rep...
2325
  static void check_spinlock_acquired_node(struct kmem_cache *cachep, int node)
e498be7da   Christoph Lameter   [PATCH] Numa-awar...
2326
2327
2328
  {
  #ifdef CONFIG_SMP
  	check_irq_off();
6a67368c3   Christoph Lameter   slab: Rename node...
2329
  	assert_spin_locked(&cachep->node[node]->list_lock);
e498be7da   Christoph Lameter   [PATCH] Numa-awar...
2330
2331
  #endif
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2332
2333
2334
2335
  #else
  #define check_irq_off()	do { } while(0)
  #define check_irq_on()	do { } while(0)
  #define check_spinlock_acquired(x) do { } while(0)
e498be7da   Christoph Lameter   [PATCH] Numa-awar...
2336
  #define check_spinlock_acquired_node(x, y) do { } while(0)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2337
  #endif
ce8eb6c42   Christoph Lameter   slab: Rename list...
2338
  static void drain_array(struct kmem_cache *cachep, struct kmem_cache_node *n,
aab2207cf   Christoph Lameter   [PATCH] slab: mak...
2339
2340
  			struct array_cache *ac,
  			int force, int node);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2341
2342
  static void do_drain(void *arg)
  {
a737b3e2f   Andrew Morton   [PATCH] slab cleanup
2343
  	struct kmem_cache *cachep = arg;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2344
  	struct array_cache *ac;
7d6e6d09d   Lee Schermerhorn   numa: slab: use n...
2345
  	int node = numa_mem_id();
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2346
2347
  
  	check_irq_off();
9a2dba4b4   Pekka Enberg   [PATCH] slab: ren...
2348
  	ac = cpu_cache_get(cachep);
6a67368c3   Christoph Lameter   slab: Rename node...
2349
  	spin_lock(&cachep->node[node]->list_lock);
ff69416e6   Christoph Lameter   [PATCH] slab: fix...
2350
  	free_block(cachep, ac->entry, ac->avail, node);
6a67368c3   Christoph Lameter   slab: Rename node...
2351
  	spin_unlock(&cachep->node[node]->list_lock);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2352
2353
  	ac->avail = 0;
  }
343e0d7a9   Pekka Enberg   [PATCH] slab: rep...
2354
  static void drain_cpu_caches(struct kmem_cache *cachep)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2355
  {
ce8eb6c42   Christoph Lameter   slab: Rename list...
2356
  	struct kmem_cache_node *n;
e498be7da   Christoph Lameter   [PATCH] Numa-awar...
2357
  	int node;
15c8b6c1a   Jens Axboe   on_each_cpu(): ki...
2358
  	on_each_cpu(do_drain, cachep, 1);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2359
  	check_irq_on();
b28a02de8   Pekka Enberg   [PATCH] slab: fix...
2360
  	for_each_online_node(node) {
ce8eb6c42   Christoph Lameter   slab: Rename list...
2361
2362
2363
  		n = cachep->node[node];
  		if (n && n->alien)
  			drain_alien_cache(cachep, n->alien);
a4523a8b3   Roland Dreier   [PATCH] slab: Fix...
2364
2365
2366
  	}
  
  	for_each_online_node(node) {
ce8eb6c42   Christoph Lameter   slab: Rename list...
2367
2368
2369
  		n = cachep->node[node];
  		if (n)
  			drain_array(cachep, n, n->shared, 1, node);
e498be7da   Christoph Lameter   [PATCH] Numa-awar...
2370
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2371
  }
ed11d9eb2   Christoph Lameter   [PATCH] slab: con...
2372
2373
2374
2375
2376
2377
2378
  /*
   * Remove slabs from the list of free slabs.
   * Specify the number of slabs to drain in tofree.
   *
   * Returns the actual number of slabs released.
   */
  static int drain_freelist(struct kmem_cache *cache,
ce8eb6c42   Christoph Lameter   slab: Rename list...
2379
  			struct kmem_cache_node *n, int tofree)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2380
  {
ed11d9eb2   Christoph Lameter   [PATCH] slab: con...
2381
2382
  	struct list_head *p;
  	int nr_freed;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2383
  	struct slab *slabp;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2384

ed11d9eb2   Christoph Lameter   [PATCH] slab: con...
2385
  	nr_freed = 0;
ce8eb6c42   Christoph Lameter   slab: Rename list...
2386
  	while (nr_freed < tofree && !list_empty(&n->slabs_free)) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2387

ce8eb6c42   Christoph Lameter   slab: Rename list...
2388
2389
2390
2391
  		spin_lock_irq(&n->list_lock);
  		p = n->slabs_free.prev;
  		if (p == &n->slabs_free) {
  			spin_unlock_irq(&n->list_lock);
ed11d9eb2   Christoph Lameter   [PATCH] slab: con...
2392
2393
  			goto out;
  		}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2394

ed11d9eb2   Christoph Lameter   [PATCH] slab: con...
2395
  		slabp = list_entry(p, struct slab, list);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2396
  #if DEBUG
40094fa65   Eric Sesterhenn   BUG_ON() Conversi...
2397
  		BUG_ON(slabp->inuse);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2398
2399
  #endif
  		list_del(&slabp->list);
ed11d9eb2   Christoph Lameter   [PATCH] slab: con...
2400
2401
2402
2403
  		/*
  		 * Safe to drop the lock. The slab is no longer linked
  		 * to the cache.
  		 */
ce8eb6c42   Christoph Lameter   slab: Rename list...
2404
2405
  		n->free_objects -= cache->num;
  		spin_unlock_irq(&n->list_lock);
ed11d9eb2   Christoph Lameter   [PATCH] slab: con...
2406
2407
  		slab_destroy(cache, slabp);
  		nr_freed++;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2408
  	}
ed11d9eb2   Christoph Lameter   [PATCH] slab: con...
2409
2410
  out:
  	return nr_freed;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2411
  }
18004c5d4   Christoph Lameter   mm, sl[aou]b: Use...
2412
  /* Called with slab_mutex held to protect against cpu hotplug */
343e0d7a9   Pekka Enberg   [PATCH] slab: rep...
2413
  static int __cache_shrink(struct kmem_cache *cachep)
e498be7da   Christoph Lameter   [PATCH] Numa-awar...
2414
2415
  {
  	int ret = 0, i = 0;
ce8eb6c42   Christoph Lameter   slab: Rename list...
2416
  	struct kmem_cache_node *n;
e498be7da   Christoph Lameter   [PATCH] Numa-awar...
2417
2418
2419
2420
2421
  
  	drain_cpu_caches(cachep);
  
  	check_irq_on();
  	for_each_online_node(i) {
ce8eb6c42   Christoph Lameter   slab: Rename list...
2422
2423
  		n = cachep->node[i];
  		if (!n)
ed11d9eb2   Christoph Lameter   [PATCH] slab: con...
2424
  			continue;
ce8eb6c42   Christoph Lameter   slab: Rename list...
2425
  		drain_freelist(cachep, n, n->free_objects);
ed11d9eb2   Christoph Lameter   [PATCH] slab: con...
2426

ce8eb6c42   Christoph Lameter   slab: Rename list...
2427
2428
  		ret += !list_empty(&n->slabs_full) ||
  			!list_empty(&n->slabs_partial);
e498be7da   Christoph Lameter   [PATCH] Numa-awar...
2429
2430
2431
  	}
  	return (ret ? 1 : 0);
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2432
2433
2434
2435
2436
2437
2438
  /**
   * kmem_cache_shrink - Shrink a cache.
   * @cachep: The cache to shrink.
   *
   * Releases as many slabs as possible for a cache.
   * To help debugging, a zero exit status indicates all slabs were released.
   */
343e0d7a9   Pekka Enberg   [PATCH] slab: rep...
2439
  int kmem_cache_shrink(struct kmem_cache *cachep)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2440
  {
8f5be20bf   Ravikiran G Thirumalai   [PATCH] mm: slab:...
2441
  	int ret;
40094fa65   Eric Sesterhenn   BUG_ON() Conversi...
2442
  	BUG_ON(!cachep || in_interrupt());
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2443

95402b382   Gautham R Shenoy   cpu-hotplug: repl...
2444
  	get_online_cpus();
18004c5d4   Christoph Lameter   mm, sl[aou]b: Use...
2445
  	mutex_lock(&slab_mutex);
8f5be20bf   Ravikiran G Thirumalai   [PATCH] mm: slab:...
2446
  	ret = __cache_shrink(cachep);
18004c5d4   Christoph Lameter   mm, sl[aou]b: Use...
2447
  	mutex_unlock(&slab_mutex);
95402b382   Gautham R Shenoy   cpu-hotplug: repl...
2448
  	put_online_cpus();
8f5be20bf   Ravikiran G Thirumalai   [PATCH] mm: slab:...
2449
  	return ret;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2450
2451
  }
  EXPORT_SYMBOL(kmem_cache_shrink);
945cf2b61   Christoph Lameter   mm/sl[aou]b: Extr...
2452
  int __kmem_cache_shutdown(struct kmem_cache *cachep)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2453
  {
12c3667fb   Christoph Lameter   mm/sl[aou]b: Get ...
2454
  	int i;
ce8eb6c42   Christoph Lameter   slab: Rename list...
2455
  	struct kmem_cache_node *n;
12c3667fb   Christoph Lameter   mm/sl[aou]b: Get ...
2456
  	int rc = __cache_shrink(cachep);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2457

12c3667fb   Christoph Lameter   mm/sl[aou]b: Get ...
2458
2459
  	if (rc)
  		return rc;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2460

12c3667fb   Christoph Lameter   mm/sl[aou]b: Get ...
2461
2462
  	for_each_online_cpu(i)
  	    kfree(cachep->array[i]);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2463

ce8eb6c42   Christoph Lameter   slab: Rename list...
2464
  	/* NUMA: free the node structures */
12c3667fb   Christoph Lameter   mm/sl[aou]b: Get ...
2465
  	for_each_online_node(i) {
ce8eb6c42   Christoph Lameter   slab: Rename list...
2466
2467
2468
2469
2470
  		n = cachep->node[i];
  		if (n) {
  			kfree(n->shared);
  			free_alien_cache(n->alien);
  			kfree(n);
12c3667fb   Christoph Lameter   mm/sl[aou]b: Get ...
2471
2472
2473
  		}
  	}
  	return 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2474
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2475

e5ac9c5ae   Ravikiran G Thirumalai   [PATCH] Add some ...
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
  /*
   * Get the memory for a slab management obj.
   * For a slab cache when the slab descriptor is off-slab, slab descriptors
   * always come from malloc_sizes caches.  The slab descriptor cannot
   * come from the same cache which is getting created because,
   * when we are searching for an appropriate cache for these
   * descriptors in kmem_cache_create, we search through the malloc_sizes array.
   * If we are creating a malloc_sizes cache here it would not be visible to
   * kmem_find_general_cachep till the initialization is complete.
   * Hence we cannot have slabp_cache same as the original cache.
   */
343e0d7a9   Pekka Enberg   [PATCH] slab: rep...
2487
  static struct slab *alloc_slabmgmt(struct kmem_cache *cachep, void *objp,
5b74ada7e   Ravikiran G Thirumalai   [PATCH] slab: all...
2488
2489
  				   int colour_off, gfp_t local_flags,
  				   int nodeid)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2490
2491
  {
  	struct slab *slabp;
b28a02de8   Pekka Enberg   [PATCH] slab: fix...
2492

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2493
2494
  	if (OFF_SLAB(cachep)) {
  		/* Slab management obj is off-slab. */
5b74ada7e   Ravikiran G Thirumalai   [PATCH] slab: all...
2495
  		slabp = kmem_cache_alloc_node(cachep->slabp_cache,
8759ec50a   Pekka Enberg   slab: remove GFP_...
2496
  					      local_flags, nodeid);
d5cff6352   Catalin Marinas   kmemleak: Add the...
2497
2498
2499
2500
2501
2502
  		/*
  		 * If the first object in the slab is leaked (it's allocated
  		 * but no one has a reference to it), we want to make sure
  		 * kmemleak does not treat the ->s_mem pointer as a reference
  		 * to the object. Otherwise we will not report the leak.
  		 */
c017b4be3   Catalin Marinas   kmemleak: Simplif...
2503
2504
  		kmemleak_scan_area(&slabp->list, sizeof(struct list_head),
  				   local_flags);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2505
2506
2507
  		if (!slabp)
  			return NULL;
  	} else {
b28a02de8   Pekka Enberg   [PATCH] slab: fix...
2508
  		slabp = objp + colour_off;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2509
2510
2511
2512
  		colour_off += cachep->slab_size;
  	}
  	slabp->inuse = 0;
  	slabp->colouroff = colour_off;
b28a02de8   Pekka Enberg   [PATCH] slab: fix...
2513
  	slabp->s_mem = objp + colour_off;
5b74ada7e   Ravikiran G Thirumalai   [PATCH] slab: all...
2514
  	slabp->nodeid = nodeid;
e51bfd0ad   Marcin Slusarz   slab: avoid doubl...
2515
  	slabp->free = 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2516
2517
2518
2519
2520
  	return slabp;
  }
  
  static inline kmem_bufctl_t *slab_bufctl(struct slab *slabp)
  {
b28a02de8   Pekka Enberg   [PATCH] slab: fix...
2521
  	return (kmem_bufctl_t *) (slabp + 1);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2522
  }
343e0d7a9   Pekka Enberg   [PATCH] slab: rep...
2523
  static void cache_init_objs(struct kmem_cache *cachep,
a35afb830   Christoph Lameter   Remove SLAB_CTOR_...
2524
  			    struct slab *slabp)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2525
2526
2527
2528
  {
  	int i;
  
  	for (i = 0; i < cachep->num; i++) {
8fea4e96a   Pekka Enberg   [PATCH] slab: obj...
2529
  		void *objp = index_to_obj(cachep, slabp, i);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
  #if DEBUG
  		/* need to poison the objs? */
  		if (cachep->flags & SLAB_POISON)
  			poison_obj(cachep, objp, POISON_FREE);
  		if (cachep->flags & SLAB_STORE_USER)
  			*dbg_userword(cachep, objp) = NULL;
  
  		if (cachep->flags & SLAB_RED_ZONE) {
  			*dbg_redzone1(cachep, objp) = RED_INACTIVE;
  			*dbg_redzone2(cachep, objp) = RED_INACTIVE;
  		}
  		/*
a737b3e2f   Andrew Morton   [PATCH] slab cleanup
2542
2543
2544
  		 * Constructors are not allowed to allocate memory from the same
  		 * cache which they are a constructor for.  Otherwise, deadlock.
  		 * They must also be threaded.
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2545
2546
  		 */
  		if (cachep->ctor && !(cachep->flags & SLAB_POISON))
51cc50685   Alexey Dobriyan   SL*B: drop kmem c...
2547
  			cachep->ctor(objp + obj_offset(cachep));
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2548
2549
2550
2551
  
  		if (cachep->flags & SLAB_RED_ZONE) {
  			if (*dbg_redzone2(cachep, objp) != RED_INACTIVE)
  				slab_error(cachep, "constructor overwrote the"
b28a02de8   Pekka Enberg   [PATCH] slab: fix...
2552
  					   " end of an object");
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2553
2554
  			if (*dbg_redzone1(cachep, objp) != RED_INACTIVE)
  				slab_error(cachep, "constructor overwrote the"
b28a02de8   Pekka Enberg   [PATCH] slab: fix...
2555
  					   " start of an object");
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2556
  		}
3b0efdfa1   Christoph Lameter   mm, sl[aou]b: Ext...
2557
  		if ((cachep->size % PAGE_SIZE) == 0 &&
a737b3e2f   Andrew Morton   [PATCH] slab cleanup
2558
  			    OFF_SLAB(cachep) && cachep->flags & SLAB_POISON)
b28a02de8   Pekka Enberg   [PATCH] slab: fix...
2559
  			kernel_map_pages(virt_to_page(objp),
3b0efdfa1   Christoph Lameter   mm, sl[aou]b: Ext...
2560
  					 cachep->size / PAGE_SIZE, 0);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2561
2562
  #else
  		if (cachep->ctor)
51cc50685   Alexey Dobriyan   SL*B: drop kmem c...
2563
  			cachep->ctor(objp);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2564
  #endif
b28a02de8   Pekka Enberg   [PATCH] slab: fix...
2565
  		slab_bufctl(slabp)[i] = i + 1;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2566
  	}
b28a02de8   Pekka Enberg   [PATCH] slab: fix...
2567
  	slab_bufctl(slabp)[i - 1] = BUFCTL_END;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2568
  }
343e0d7a9   Pekka Enberg   [PATCH] slab: rep...
2569
  static void kmem_flagcheck(struct kmem_cache *cachep, gfp_t flags)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2570
  {
4b51d6698   Christoph Lameter   [PATCH] optional ...
2571
2572
  	if (CONFIG_ZONE_DMA_FLAG) {
  		if (flags & GFP_DMA)
a618e89f1   Glauber Costa   slab: rename gfpf...
2573
  			BUG_ON(!(cachep->allocflags & GFP_DMA));
4b51d6698   Christoph Lameter   [PATCH] optional ...
2574
  		else
a618e89f1   Glauber Costa   slab: rename gfpf...
2575
  			BUG_ON(cachep->allocflags & GFP_DMA);
4b51d6698   Christoph Lameter   [PATCH] optional ...
2576
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2577
  }
a737b3e2f   Andrew Morton   [PATCH] slab cleanup
2578
2579
  static void *slab_get_obj(struct kmem_cache *cachep, struct slab *slabp,
  				int nodeid)
78d382d77   Matthew Dobson   [PATCH] slab: ext...
2580
  {
8fea4e96a   Pekka Enberg   [PATCH] slab: obj...
2581
  	void *objp = index_to_obj(cachep, slabp, slabp->free);
78d382d77   Matthew Dobson   [PATCH] slab: ext...
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
  	kmem_bufctl_t next;
  
  	slabp->inuse++;
  	next = slab_bufctl(slabp)[slabp->free];
  #if DEBUG
  	slab_bufctl(slabp)[slabp->free] = BUFCTL_FREE;
  	WARN_ON(slabp->nodeid != nodeid);
  #endif
  	slabp->free = next;
  
  	return objp;
  }
a737b3e2f   Andrew Morton   [PATCH] slab cleanup
2594
2595
  static void slab_put_obj(struct kmem_cache *cachep, struct slab *slabp,
  				void *objp, int nodeid)
78d382d77   Matthew Dobson   [PATCH] slab: ext...
2596
  {
8fea4e96a   Pekka Enberg   [PATCH] slab: obj...
2597
  	unsigned int objnr = obj_to_index(cachep, slabp, objp);
78d382d77   Matthew Dobson   [PATCH] slab: ext...
2598
2599
2600
2601
  
  #if DEBUG
  	/* Verify that the slab belongs to the intended node */
  	WARN_ON(slabp->nodeid != nodeid);
871751e25   Al Viro   [PATCH] slab: imp...
2602
  	if (slab_bufctl(slabp)[objnr] + 1 <= SLAB_LIMIT + 1) {
78d382d77   Matthew Dobson   [PATCH] slab: ext...
2603
  		printk(KERN_ERR "slab: double free detected in cache "
a737b3e2f   Andrew Morton   [PATCH] slab cleanup
2604
2605
  				"'%s', objp %p
  ", cachep->name, objp);
78d382d77   Matthew Dobson   [PATCH] slab: ext...
2606
2607
2608
2609
2610
2611
2612
  		BUG();
  	}
  #endif
  	slab_bufctl(slabp)[objnr] = slabp->free;
  	slabp->free = objnr;
  	slabp->inuse--;
  }
4776874ff   Pekka Enberg   [PATCH] slab: pag...
2613
2614
2615
  /*
   * Map pages beginning at addr to the given cache and slab. This is required
   * for the slab allocator to be able to lookup the cache and slab of a
ccd35fb9f   Nick Piggin   kernel: kmem_ptr_...
2616
   * virtual address for kfree, ksize, and slab debugging.
4776874ff   Pekka Enberg   [PATCH] slab: pag...
2617
2618
2619
   */
  static void slab_map_pages(struct kmem_cache *cache, struct slab *slab,
  			   void *addr)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2620
  {
4776874ff   Pekka Enberg   [PATCH] slab: pag...
2621
  	int nr_pages;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2622
  	struct page *page;
4776874ff   Pekka Enberg   [PATCH] slab: pag...
2623
  	page = virt_to_page(addr);
84097518d   Nick Piggin   [PATCH] mm: nommu...
2624

4776874ff   Pekka Enberg   [PATCH] slab: pag...
2625
  	nr_pages = 1;
84097518d   Nick Piggin   [PATCH] mm: nommu...
2626
  	if (likely(!PageCompound(page)))
4776874ff   Pekka Enberg   [PATCH] slab: pag...
2627
  		nr_pages <<= cache->gfporder;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2628
  	do {
350260889   Christoph Lameter   slab: Remove some...
2629
2630
  		page->slab_cache = cache;
  		page->slab_page = slab;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2631
  		page++;
4776874ff   Pekka Enberg   [PATCH] slab: pag...
2632
  	} while (--nr_pages);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2633
2634
2635
2636
2637
2638
  }
  
  /*
   * Grow (by 1) the number of slabs within a cache.  This is called by
   * kmem_cache_alloc() when there are no active objs left in a cache.
   */
3c517a613   Christoph Lameter   [PATCH] slab: bet...
2639
2640
  static int cache_grow(struct kmem_cache *cachep,
  		gfp_t flags, int nodeid, void *objp)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2641
  {
b28a02de8   Pekka Enberg   [PATCH] slab: fix...
2642
  	struct slab *slabp;
b28a02de8   Pekka Enberg   [PATCH] slab: fix...
2643
2644
  	size_t offset;
  	gfp_t local_flags;
ce8eb6c42   Christoph Lameter   slab: Rename list...
2645
  	struct kmem_cache_node *n;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2646

a737b3e2f   Andrew Morton   [PATCH] slab cleanup
2647
2648
2649
  	/*
  	 * Be lazy and only check for valid flags here,  keeping it out of the
  	 * critical path in kmem_cache_alloc().
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2650
  	 */
6cb062296   Christoph Lameter   Categorize GFP flags
2651
2652
  	BUG_ON(flags & GFP_SLAB_BUG_MASK);
  	local_flags = flags & (GFP_CONSTRAINT_MASK|GFP_RECLAIM_MASK);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2653

ce8eb6c42   Christoph Lameter   slab: Rename list...
2654
  	/* Take the node list lock to change the colour_next on this node */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2655
  	check_irq_off();
ce8eb6c42   Christoph Lameter   slab: Rename list...
2656
2657
  	n = cachep->node[nodeid];
  	spin_lock(&n->list_lock);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2658
2659
  
  	/* Get colour for the slab, and cal the next value. */
ce8eb6c42   Christoph Lameter   slab: Rename list...
2660
2661
2662
2663
2664
  	offset = n->colour_next;
  	n->colour_next++;
  	if (n->colour_next >= cachep->colour)
  		n->colour_next = 0;
  	spin_unlock(&n->list_lock);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2665

2e1217cf9   Ravikiran G Thirumalai   [PATCH] NUMA slab...
2666
  	offset *= cachep->colour_off;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
  
  	if (local_flags & __GFP_WAIT)
  		local_irq_enable();
  
  	/*
  	 * The test for missing atomic flag is performed here, rather than
  	 * the more obvious place, simply to reduce the critical path length
  	 * in kmem_cache_alloc(). If a caller is seriously mis-behaving they
  	 * will eventually be caught here (where it matters).
  	 */
  	kmem_flagcheck(cachep, flags);
a737b3e2f   Andrew Morton   [PATCH] slab cleanup
2678
2679
2680
  	/*
  	 * Get mem for the objs.  Attempt to allocate a physical page from
  	 * 'nodeid'.
e498be7da   Christoph Lameter   [PATCH] Numa-awar...
2681
  	 */
3c517a613   Christoph Lameter   [PATCH] slab: bet...
2682
  	if (!objp)
b8c1c5da1   Andrew Morton   slab: correctly h...
2683
  		objp = kmem_getpages(cachep, local_flags, nodeid);
a737b3e2f   Andrew Morton   [PATCH] slab cleanup
2684
  	if (!objp)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2685
2686
2687
  		goto failed;
  
  	/* Get slab management. */
3c517a613   Christoph Lameter   [PATCH] slab: bet...
2688
  	slabp = alloc_slabmgmt(cachep, objp, offset,
6cb062296   Christoph Lameter   Categorize GFP flags
2689
  			local_flags & ~GFP_CONSTRAINT_MASK, nodeid);
a737b3e2f   Andrew Morton   [PATCH] slab cleanup
2690
  	if (!slabp)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2691
  		goto opps1;
4776874ff   Pekka Enberg   [PATCH] slab: pag...
2692
  	slab_map_pages(cachep, slabp, objp);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2693

a35afb830   Christoph Lameter   Remove SLAB_CTOR_...
2694
  	cache_init_objs(cachep, slabp);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2695
2696
2697
2698
  
  	if (local_flags & __GFP_WAIT)
  		local_irq_disable();
  	check_irq_off();
ce8eb6c42   Christoph Lameter   slab: Rename list...
2699
  	spin_lock(&n->list_lock);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2700
2701
  
  	/* Make slab active. */
ce8eb6c42   Christoph Lameter   slab: Rename list...
2702
  	list_add_tail(&slabp->list, &(n->slabs_free));
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2703
  	STATS_INC_GROWN(cachep);
ce8eb6c42   Christoph Lameter   slab: Rename list...
2704
2705
  	n->free_objects += cachep->num;
  	spin_unlock(&n->list_lock);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2706
  	return 1;
a737b3e2f   Andrew Morton   [PATCH] slab cleanup
2707
  opps1:
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2708
  	kmem_freepages(cachep, objp);
a737b3e2f   Andrew Morton   [PATCH] slab cleanup
2709
  failed:
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
  	if (local_flags & __GFP_WAIT)
  		local_irq_disable();
  	return 0;
  }
  
  #if DEBUG
  
  /*
   * Perform extra freeing checks:
   * - detect bad pointers.
   * - POISON/RED_ZONE checking
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2721
2722
2723
   */
  static void kfree_debugcheck(const void *objp)
  {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2724
2725
2726
  	if (!virt_addr_valid(objp)) {
  		printk(KERN_ERR "kfree_debugcheck: out of range ptr %lxh.
  ",
b28a02de8   Pekka Enberg   [PATCH] slab: fix...
2727
2728
  		       (unsigned long)objp);
  		BUG();
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2729
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2730
  }
58ce1fd58   Pekka Enberg   [PATCH] slab: red...
2731
2732
  static inline void verify_redzone_free(struct kmem_cache *cache, void *obj)
  {
b46b8f19c   David Woodhouse   Increase slab red...
2733
  	unsigned long long redzone1, redzone2;
58ce1fd58   Pekka Enberg   [PATCH] slab: red...
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
  
  	redzone1 = *dbg_redzone1(cache, obj);
  	redzone2 = *dbg_redzone2(cache, obj);
  
  	/*
  	 * Redzone is ok.
  	 */
  	if (redzone1 == RED_ACTIVE && redzone2 == RED_ACTIVE)
  		return;
  
  	if (redzone1 == RED_INACTIVE && redzone2 == RED_INACTIVE)
  		slab_error(cache, "double free detected");
  	else
  		slab_error(cache, "memory outside object was overwritten");
b46b8f19c   David Woodhouse   Increase slab red...
2748
2749
  	printk(KERN_ERR "%p: redzone 1:0x%llx, redzone 2:0x%llx.
  ",
58ce1fd58   Pekka Enberg   [PATCH] slab: red...
2750
2751
  			obj, redzone1, redzone2);
  }
343e0d7a9   Pekka Enberg   [PATCH] slab: rep...
2752
  static void *cache_free_debugcheck(struct kmem_cache *cachep, void *objp,
7c0cb9c64   Ezequiel Garcia   mm, slab: Replace...
2753
  				   unsigned long caller)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2754
2755
2756
2757
  {
  	struct page *page;
  	unsigned int objnr;
  	struct slab *slabp;
80cbd911c   Matthew Wilcox   Fix kmem_cache_fr...
2758
  	BUG_ON(virt_to_cache(objp) != cachep);
3dafccf22   Manfred Spraul   [PATCH] slab: dis...
2759
  	objp -= obj_offset(cachep);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2760
  	kfree_debugcheck(objp);
b49af68ff   Christoph Lameter   Add virt_to_head_...
2761
  	page = virt_to_head_page(objp);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2762

350260889   Christoph Lameter   slab: Remove some...
2763
  	slabp = page->slab_page;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2764
2765
  
  	if (cachep->flags & SLAB_RED_ZONE) {
58ce1fd58   Pekka Enberg   [PATCH] slab: red...
2766
  		verify_redzone_free(cachep, objp);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2767
2768
2769
2770
  		*dbg_redzone1(cachep, objp) = RED_INACTIVE;
  		*dbg_redzone2(cachep, objp) = RED_INACTIVE;
  	}
  	if (cachep->flags & SLAB_STORE_USER)
7c0cb9c64   Ezequiel Garcia   mm, slab: Replace...
2771
  		*dbg_userword(cachep, objp) = (void *)caller;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2772

8fea4e96a   Pekka Enberg   [PATCH] slab: obj...
2773
  	objnr = obj_to_index(cachep, slabp, objp);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2774
2775
  
  	BUG_ON(objnr >= cachep->num);
8fea4e96a   Pekka Enberg   [PATCH] slab: obj...
2776
  	BUG_ON(objp != index_to_obj(cachep, slabp, objnr));
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2777

871751e25   Al Viro   [PATCH] slab: imp...
2778
2779
2780
  #ifdef CONFIG_DEBUG_SLAB_LEAK
  	slab_bufctl(slabp)[objnr] = BUFCTL_FREE;
  #endif
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2781
2782
  	if (cachep->flags & SLAB_POISON) {
  #ifdef CONFIG_DEBUG_PAGEALLOC
3b0efdfa1   Christoph Lameter   mm, sl[aou]b: Ext...
2783
  		if ((cachep->size % PAGE_SIZE)==0 && OFF_SLAB(cachep)) {
7c0cb9c64   Ezequiel Garcia   mm, slab: Replace...
2784
  			store_stackinfo(cachep, objp, caller);
b28a02de8   Pekka Enberg   [PATCH] slab: fix...
2785
  			kernel_map_pages(virt_to_page(objp),
3b0efdfa1   Christoph Lameter   mm, sl[aou]b: Ext...
2786
  					 cachep->size / PAGE_SIZE, 0);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2787
2788
2789
2790
2791
2792
2793
2794
2795
  		} else {
  			poison_obj(cachep, objp, POISON_FREE);
  		}
  #else
  		poison_obj(cachep, objp, POISON_FREE);
  #endif
  	}
  	return objp;
  }
343e0d7a9   Pekka Enberg   [PATCH] slab: rep...
2796
  static void check_slabp(struct kmem_cache *cachep, struct slab *slabp)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2797
2798
2799
  {
  	kmem_bufctl_t i;
  	int entries = 0;
b28a02de8   Pekka Enberg   [PATCH] slab: fix...
2800

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2801
2802
2803
2804
2805
2806
2807
  	/* Check slab's freelist to see if this obj is there. */
  	for (i = slabp->free; i != BUFCTL_END; i = slab_bufctl(slabp)[i]) {
  		entries++;
  		if (entries > cachep->num || i >= cachep->num)
  			goto bad;
  	}
  	if (entries != cachep->num - slabp->inuse) {
a737b3e2f   Andrew Morton   [PATCH] slab cleanup
2808
2809
  bad:
  		printk(KERN_ERR "slab: Internal list corruption detected in "
face37f5e   Dave Jones   slab: add taint f...
2810
2811
2812
2813
  			"cache '%s'(%d), slabp %p(%d). Tainted(%s). Hexdump:
  ",
  			cachep->name, cachep->num, slabp, slabp->inuse,
  			print_tainted());
fdde6abb3   Sebastian Andrzej Siewior   slab: use print_h...
2814
2815
2816
  		print_hex_dump(KERN_ERR, "", DUMP_PREFIX_OFFSET, 16, 1, slabp,
  			sizeof(*slabp) + cachep->num * sizeof(kmem_bufctl_t),
  			1);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2817
2818
2819
2820
2821
2822
2823
2824
  		BUG();
  	}
  }
  #else
  #define kfree_debugcheck(x) do { } while(0)
  #define cache_free_debugcheck(x,objp,z) (objp)
  #define check_slabp(x,y) do { } while(0)
  #endif
072bb0aa5   Mel Gorman   mm: sl[au]b: add ...
2825
2826
  static void *cache_alloc_refill(struct kmem_cache *cachep, gfp_t flags,
  							bool force_refill)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2827
2828
  {
  	int batchcount;
ce8eb6c42   Christoph Lameter   slab: Rename list...
2829
  	struct kmem_cache_node *n;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2830
  	struct array_cache *ac;
1ca4cb241   Pekka Enberg   [PATCH] slab: red...
2831
  	int node;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2832
  	check_irq_off();
7d6e6d09d   Lee Schermerhorn   numa: slab: use n...
2833
  	node = numa_mem_id();
072bb0aa5   Mel Gorman   mm: sl[au]b: add ...
2834
2835
2836
  	if (unlikely(force_refill))
  		goto force_grow;
  retry:
9a2dba4b4   Pekka Enberg   [PATCH] slab: ren...
2837
  	ac = cpu_cache_get(cachep);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2838
2839
  	batchcount = ac->batchcount;
  	if (!ac->touched && batchcount > BATCHREFILL_LIMIT) {
a737b3e2f   Andrew Morton   [PATCH] slab cleanup
2840
2841
2842
2843
  		/*
  		 * If there was little recent activity on this cache, then
  		 * perform only a partial refill.  Otherwise we could generate
  		 * refill bouncing.
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2844
2845
2846
  		 */
  		batchcount = BATCHREFILL_LIMIT;
  	}
ce8eb6c42   Christoph Lameter   slab: Rename list...
2847
  	n = cachep->node[node];
e498be7da   Christoph Lameter   [PATCH] Numa-awar...
2848

ce8eb6c42   Christoph Lameter   slab: Rename list...
2849
2850
  	BUG_ON(ac->avail > 0 || !n);
  	spin_lock(&n->list_lock);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2851

3ded175a4   Christoph Lameter   [PATCH] slab: add...
2852
  	/* See if we can refill from the shared array */
ce8eb6c42   Christoph Lameter   slab: Rename list...
2853
2854
  	if (n->shared && transfer_objects(ac, n->shared, batchcount)) {
  		n->shared->touched = 1;
3ded175a4   Christoph Lameter   [PATCH] slab: add...
2855
  		goto alloc_done;
44b57f1cc   Nick Piggin   slab: fix regress...
2856
  	}
3ded175a4   Christoph Lameter   [PATCH] slab: add...
2857

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2858
2859
2860
2861
  	while (batchcount > 0) {
  		struct list_head *entry;
  		struct slab *slabp;
  		/* Get slab alloc is to come from. */
ce8eb6c42   Christoph Lameter   slab: Rename list...
2862
2863
2864
2865
2866
  		entry = n->slabs_partial.next;
  		if (entry == &n->slabs_partial) {
  			n->free_touched = 1;
  			entry = n->slabs_free.next;
  			if (entry == &n->slabs_free)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2867
2868
2869
2870
2871
2872
  				goto must_grow;
  		}
  
  		slabp = list_entry(entry, struct slab, list);
  		check_slabp(cachep, slabp);
  		check_spinlock_acquired(cachep);
714b8171a   Pekka Enberg   slab: ensure cach...
2873
2874
2875
2876
2877
2878
  
  		/*
  		 * The slab was either on partial or free list so
  		 * there must be at least one object available for
  		 * allocation.
  		 */
249b9f331   roel kluin   slab: unsigned sl...
2879
  		BUG_ON(slabp->inuse >= cachep->num);
714b8171a   Pekka Enberg   slab: ensure cach...
2880

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2881
  		while (slabp->inuse < cachep->num && batchcount--) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2882
2883
2884
  			STATS_INC_ALLOCED(cachep);
  			STATS_INC_ACTIVE(cachep);
  			STATS_SET_HIGH(cachep);
072bb0aa5   Mel Gorman   mm: sl[au]b: add ...
2885
2886
  			ac_put_obj(cachep, ac, slab_get_obj(cachep, slabp,
  									node));
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2887
2888
2889
2890
2891
2892
  		}
  		check_slabp(cachep, slabp);
  
  		/* move slabp to correct slabp list: */
  		list_del(&slabp->list);
  		if (slabp->free == BUFCTL_END)
ce8eb6c42   Christoph Lameter   slab: Rename list...
2893
  			list_add(&slabp->list, &n->slabs_full);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2894
  		else
ce8eb6c42   Christoph Lameter   slab: Rename list...
2895
  			list_add(&slabp->list, &n->slabs_partial);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2896
  	}
a737b3e2f   Andrew Morton   [PATCH] slab cleanup
2897
  must_grow:
ce8eb6c42   Christoph Lameter   slab: Rename list...
2898
  	n->free_objects -= ac->avail;
a737b3e2f   Andrew Morton   [PATCH] slab cleanup
2899
  alloc_done:
ce8eb6c42   Christoph Lameter   slab: Rename list...
2900
  	spin_unlock(&n->list_lock);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2901
2902
2903
  
  	if (unlikely(!ac->avail)) {
  		int x;
072bb0aa5   Mel Gorman   mm: sl[au]b: add ...
2904
  force_grow:
3c517a613   Christoph Lameter   [PATCH] slab: bet...
2905
  		x = cache_grow(cachep, flags | GFP_THISNODE, node, NULL);
e498be7da   Christoph Lameter   [PATCH] Numa-awar...
2906

a737b3e2f   Andrew Morton   [PATCH] slab cleanup
2907
  		/* cache_grow can reenable interrupts, then ac could change. */
9a2dba4b4   Pekka Enberg   [PATCH] slab: ren...
2908
  		ac = cpu_cache_get(cachep);
51cd8e6ff   David Rientjes   mm, slab: lock th...
2909
  		node = numa_mem_id();
072bb0aa5   Mel Gorman   mm: sl[au]b: add ...
2910
2911
2912
  
  		/* no objects in sight? abort */
  		if (!x && (ac->avail == 0 || force_refill))
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2913
  			return NULL;
a737b3e2f   Andrew Morton   [PATCH] slab cleanup
2914
  		if (!ac->avail)		/* objects refilled by interrupt? */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2915
2916
2917
  			goto retry;
  	}
  	ac->touched = 1;
072bb0aa5   Mel Gorman   mm: sl[au]b: add ...
2918
2919
  
  	return ac_get_obj(cachep, ac, flags, force_refill);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2920
  }
a737b3e2f   Andrew Morton   [PATCH] slab cleanup
2921
2922
  static inline void cache_alloc_debugcheck_before(struct kmem_cache *cachep,
  						gfp_t flags)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2923
2924
2925
2926
2927
2928
2929
2930
  {
  	might_sleep_if(flags & __GFP_WAIT);
  #if DEBUG
  	kmem_flagcheck(cachep, flags);
  #endif
  }
  
  #if DEBUG
a737b3e2f   Andrew Morton   [PATCH] slab cleanup
2931
  static void *cache_alloc_debugcheck_after(struct kmem_cache *cachep,
7c0cb9c64   Ezequiel Garcia   mm, slab: Replace...
2932
  				gfp_t flags, void *objp, unsigned long caller)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2933
  {
b28a02de8   Pekka Enberg   [PATCH] slab: fix...
2934
  	if (!objp)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2935
  		return objp;
b28a02de8   Pekka Enberg   [PATCH] slab: fix...
2936
  	if (cachep->flags & SLAB_POISON) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2937
  #ifdef CONFIG_DEBUG_PAGEALLOC
3b0efdfa1   Christoph Lameter   mm, sl[aou]b: Ext...
2938
  		if ((cachep->size % PAGE_SIZE) == 0 && OFF_SLAB(cachep))
b28a02de8   Pekka Enberg   [PATCH] slab: fix...
2939
  			kernel_map_pages(virt_to_page(objp),
3b0efdfa1   Christoph Lameter   mm, sl[aou]b: Ext...
2940
  					 cachep->size / PAGE_SIZE, 1);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2941
2942
2943
2944
2945
2946
2947
2948
  		else
  			check_poison_obj(cachep, objp);
  #else
  		check_poison_obj(cachep, objp);
  #endif
  		poison_obj(cachep, objp, POISON_INUSE);
  	}
  	if (cachep->flags & SLAB_STORE_USER)
7c0cb9c64   Ezequiel Garcia   mm, slab: Replace...
2949
  		*dbg_userword(cachep, objp) = (void *)caller;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2950
2951
  
  	if (cachep->flags & SLAB_RED_ZONE) {
a737b3e2f   Andrew Morton   [PATCH] slab cleanup
2952
2953
2954
2955
  		if (*dbg_redzone1(cachep, objp) != RED_INACTIVE ||
  				*dbg_redzone2(cachep, objp) != RED_INACTIVE) {
  			slab_error(cachep, "double free, or memory outside"
  						" object was overwritten");
b28a02de8   Pekka Enberg   [PATCH] slab: fix...
2956
  			printk(KERN_ERR
b46b8f19c   David Woodhouse   Increase slab red...
2957
2958
  				"%p: redzone 1:0x%llx, redzone 2:0x%llx
  ",
a737b3e2f   Andrew Morton   [PATCH] slab cleanup
2959
2960
  				objp, *dbg_redzone1(cachep, objp),
  				*dbg_redzone2(cachep, objp));
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2961
2962
2963
2964
  		}
  		*dbg_redzone1(cachep, objp) = RED_ACTIVE;
  		*dbg_redzone2(cachep, objp) = RED_ACTIVE;
  	}
871751e25   Al Viro   [PATCH] slab: imp...
2965
2966
2967
2968
  #ifdef CONFIG_DEBUG_SLAB_LEAK
  	{
  		struct slab *slabp;
  		unsigned objnr;
350260889   Christoph Lameter   slab: Remove some...
2969
  		slabp = virt_to_head_page(objp)->slab_page;
3b0efdfa1   Christoph Lameter   mm, sl[aou]b: Ext...
2970
  		objnr = (unsigned)(objp - slabp->s_mem) / cachep->size;
871751e25   Al Viro   [PATCH] slab: imp...
2971
2972
2973
  		slab_bufctl(slabp)[objnr] = BUFCTL_ACTIVE;
  	}
  #endif
3dafccf22   Manfred Spraul   [PATCH] slab: dis...
2974
  	objp += obj_offset(cachep);
4f1049345   Christoph Lameter   slab allocators: ...
2975
  	if (cachep->ctor && cachep->flags & SLAB_POISON)
51cc50685   Alexey Dobriyan   SL*B: drop kmem c...
2976
  		cachep->ctor(objp);
7ea466f22   Tetsuo Handa   slab: fix DEBUG_S...
2977
2978
  	if (ARCH_SLAB_MINALIGN &&
  	    ((unsigned long)objp & (ARCH_SLAB_MINALIGN-1))) {
a44b56d35   Kevin Hilman   [PATCH] slab debu...
2979
2980
  		printk(KERN_ERR "0x%p: not aligned to ARCH_SLAB_MINALIGN=%d
  ",
c225150b8   Hugh Dickins   slab: fix DEBUG_S...
2981
  		       objp, (int)ARCH_SLAB_MINALIGN);
a44b56d35   Kevin Hilman   [PATCH] slab debu...
2982
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2983
2984
2985
2986
2987
  	return objp;
  }
  #else
  #define cache_alloc_debugcheck_after(a,b,objp,d) (objp)
  #endif
773ff60e8   Akinobu Mita   SLUB: failslab su...
2988
  static bool slab_should_failslab(struct kmem_cache *cachep, gfp_t flags)
8a8b6502f   Akinobu Mita   [PATCH] fault-inj...
2989
  {
9b030cb86   Christoph Lameter   mm/sl[aou]b: Use ...
2990
  	if (cachep == kmem_cache)
773ff60e8   Akinobu Mita   SLUB: failslab su...
2991
  		return false;
8a8b6502f   Akinobu Mita   [PATCH] fault-inj...
2992

8c138bc00   Christoph Lameter   slab: Get rid of ...
2993
  	return should_failslab(cachep->object_size, flags, cachep->flags);
8a8b6502f   Akinobu Mita   [PATCH] fault-inj...
2994
  }
343e0d7a9   Pekka Enberg   [PATCH] slab: rep...
2995
  static inline void *____cache_alloc(struct kmem_cache *cachep, gfp_t flags)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2996
  {
b28a02de8   Pekka Enberg   [PATCH] slab: fix...
2997
  	void *objp;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2998
  	struct array_cache *ac;
072bb0aa5   Mel Gorman   mm: sl[au]b: add ...
2999
  	bool force_refill = false;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3000

5c3823008   Alok N Kataria   [PATCH] kmalloc_n...
3001
  	check_irq_off();
8a8b6502f   Akinobu Mita   [PATCH] fault-inj...
3002

9a2dba4b4   Pekka Enberg   [PATCH] slab: ren...
3003
  	ac = cpu_cache_get(cachep);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3004
  	if (likely(ac->avail)) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3005
  		ac->touched = 1;
072bb0aa5   Mel Gorman   mm: sl[au]b: add ...
3006
  		objp = ac_get_obj(cachep, ac, flags, false);
ddbf2e836   J. R. Okajima   slab, kmemleak: p...
3007
  		/*
072bb0aa5   Mel Gorman   mm: sl[au]b: add ...
3008
3009
  		 * Allow for the possibility all avail objects are not allowed
  		 * by the current flags
ddbf2e836   J. R. Okajima   slab, kmemleak: p...
3010
  		 */
072bb0aa5   Mel Gorman   mm: sl[au]b: add ...
3011
3012
3013
3014
3015
  		if (objp) {
  			STATS_INC_ALLOCHIT(cachep);
  			goto out;
  		}
  		force_refill = true;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3016
  	}
072bb0aa5   Mel Gorman   mm: sl[au]b: add ...
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
  
  	STATS_INC_ALLOCMISS(cachep);
  	objp = cache_alloc_refill(cachep, flags, force_refill);
  	/*
  	 * the 'ac' may be updated by cache_alloc_refill(),
  	 * and kmemleak_erase() requires its correct value.
  	 */
  	ac = cpu_cache_get(cachep);
  
  out:
d5cff6352   Catalin Marinas   kmemleak: Add the...
3027
3028
3029
3030
3031
  	/*
  	 * To avoid a false negative, if an object that is in one of the
  	 * per-CPU caches is leaked, we need to make sure kmemleak doesn't
  	 * treat the array pointers as a reference to the object.
  	 */
f3d8b53a3   J. R. Okajima   slab, kmemleak: s...
3032
3033
  	if (objp)
  		kmemleak_erase(&ac->entry[ac->avail]);
5c3823008   Alok N Kataria   [PATCH] kmalloc_n...
3034
3035
  	return objp;
  }
e498be7da   Christoph Lameter   [PATCH] Numa-awar...
3036
3037
  #ifdef CONFIG_NUMA
  /*
b2455396b   Paul Jackson   [PATCH] cpuset: m...
3038
   * Try allocating on another node if PF_SPREAD_SLAB|PF_MEMPOLICY.
c61afb181   Paul Jackson   [PATCH] cpuset me...
3039
3040
3041
3042
3043
3044
3045
   *
   * If we are in_interrupt, then process context, including cpusets and
   * mempolicy, may not apply and should not be used for allocation policy.
   */
  static void *alternate_node_alloc(struct kmem_cache *cachep, gfp_t flags)
  {
  	int nid_alloc, nid_here;
765c4507a   Christoph Lameter   [PATCH] GFP_THISN...
3046
  	if (in_interrupt() || (flags & __GFP_THISNODE))
c61afb181   Paul Jackson   [PATCH] cpuset me...
3047
  		return NULL;
7d6e6d09d   Lee Schermerhorn   numa: slab: use n...
3048
  	nid_alloc = nid_here = numa_mem_id();
c61afb181   Paul Jackson   [PATCH] cpuset me...
3049
  	if (cpuset_do_slab_mem_spread() && (cachep->flags & SLAB_MEM_SPREAD))
6adef3ebe   Jack Steiner   cpusets: new roun...
3050
  		nid_alloc = cpuset_slab_spread_node();
c61afb181   Paul Jackson   [PATCH] cpuset me...
3051
  	else if (current->mempolicy)
e7b691b08   Andi Kleen   slab/mempolicy: a...
3052
  		nid_alloc = slab_node();
c61afb181   Paul Jackson   [PATCH] cpuset me...
3053
  	if (nid_alloc != nid_here)
8b98c1699   Christoph Hellwig   [PATCH] leak trac...
3054
  		return ____cache_alloc_node(cachep, flags, nid_alloc);
c61afb181   Paul Jackson   [PATCH] cpuset me...
3055
3056
3057
3058
  	return NULL;
  }
  
  /*
765c4507a   Christoph Lameter   [PATCH] GFP_THISN...
3059
   * Fallback function if there was no memory available and no objects on a
3c517a613   Christoph Lameter   [PATCH] slab: bet...
3060
   * certain node and fall back is permitted. First we scan all the
6a67368c3   Christoph Lameter   slab: Rename node...
3061
   * available node for available objects. If that fails then we
3c517a613   Christoph Lameter   [PATCH] slab: bet...
3062
3063
3064
   * perform an allocation without specifying a node. This allows the page
   * allocator to do its reclaim / fallback magic. We then insert the
   * slab into the proper nodelist and then allocate from it.
765c4507a   Christoph Lameter   [PATCH] GFP_THISN...
3065
   */
8c8cc2c10   Pekka Enberg   [PATCH] slab: cac...
3066
  static void *fallback_alloc(struct kmem_cache *cache, gfp_t flags)
765c4507a   Christoph Lameter   [PATCH] GFP_THISN...
3067
  {
8c8cc2c10   Pekka Enberg   [PATCH] slab: cac...
3068
3069
  	struct zonelist *zonelist;
  	gfp_t local_flags;
dd1a239f6   Mel Gorman   mm: have zonelist...
3070
  	struct zoneref *z;
54a6eb5c4   Mel Gorman   mm: use two zonel...
3071
3072
  	struct zone *zone;
  	enum zone_type high_zoneidx = gfp_zone(flags);
765c4507a   Christoph Lameter   [PATCH] GFP_THISN...
3073
  	void *obj = NULL;
3c517a613   Christoph Lameter   [PATCH] slab: bet...
3074
  	int nid;
cc9a6c877   Mel Gorman   cpuset: mm: reduc...
3075
  	unsigned int cpuset_mems_cookie;
8c8cc2c10   Pekka Enberg   [PATCH] slab: cac...
3076
3077
3078
  
  	if (flags & __GFP_THISNODE)
  		return NULL;
6cb062296   Christoph Lameter   Categorize GFP flags
3079
  	local_flags = flags & (GFP_CONSTRAINT_MASK|GFP_RECLAIM_MASK);
765c4507a   Christoph Lameter   [PATCH] GFP_THISN...
3080

cc9a6c877   Mel Gorman   cpuset: mm: reduc...
3081
3082
  retry_cpuset:
  	cpuset_mems_cookie = get_mems_allowed();
e7b691b08   Andi Kleen   slab/mempolicy: a...
3083
  	zonelist = node_zonelist(slab_node(), flags);
cc9a6c877   Mel Gorman   cpuset: mm: reduc...
3084

3c517a613   Christoph Lameter   [PATCH] slab: bet...
3085
3086
3087
3088
3089
  retry:
  	/*
  	 * Look through allowed nodes for objects available
  	 * from existing per node queues.
  	 */
54a6eb5c4   Mel Gorman   mm: use two zonel...
3090
3091
  	for_each_zone_zonelist(zone, z, zonelist, high_zoneidx) {
  		nid = zone_to_nid(zone);
aedb0eb10   Christoph Lameter   [PATCH] Slab: Do ...
3092

54a6eb5c4   Mel Gorman   mm: use two zonel...
3093
  		if (cpuset_zone_allowed_hardwall(zone, flags) &&
6a67368c3   Christoph Lameter   slab: Rename node...
3094
3095
  			cache->node[nid] &&
  			cache->node[nid]->free_objects) {
3c517a613   Christoph Lameter   [PATCH] slab: bet...
3096
3097
  				obj = ____cache_alloc_node(cache,
  					flags | GFP_THISNODE, nid);
481c5346d   Christoph Lameter   Slab: Fix memory ...
3098
3099
3100
  				if (obj)
  					break;
  		}
3c517a613   Christoph Lameter   [PATCH] slab: bet...
3101
  	}
cfce66047   Christoph Lameter   Slab allocators: ...
3102
  	if (!obj) {
3c517a613   Christoph Lameter   [PATCH] slab: bet...
3103
3104
3105
3106
3107
3108
  		/*
  		 * This allocation will be performed within the constraints
  		 * of the current cpuset / memory policy requirements.
  		 * We may trigger various forms of reclaim on the allowed
  		 * set and go into memory reserves if necessary.
  		 */
dd47ea755   Christoph Lameter   [PATCH] slab: fix...
3109
3110
3111
  		if (local_flags & __GFP_WAIT)
  			local_irq_enable();
  		kmem_flagcheck(cache, flags);
7d6e6d09d   Lee Schermerhorn   numa: slab: use n...
3112
  		obj = kmem_getpages(cache, local_flags, numa_mem_id());
dd47ea755   Christoph Lameter   [PATCH] slab: fix...
3113
3114
  		if (local_flags & __GFP_WAIT)
  			local_irq_disable();
3c517a613   Christoph Lameter   [PATCH] slab: bet...
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
  		if (obj) {
  			/*
  			 * Insert into the appropriate per node queues
  			 */
  			nid = page_to_nid(virt_to_page(obj));
  			if (cache_grow(cache, flags, nid, obj)) {
  				obj = ____cache_alloc_node(cache,
  					flags | GFP_THISNODE, nid);
  				if (!obj)
  					/*
  					 * Another processor may allocate the
  					 * objects in the slab since we are
  					 * not holding any locks.
  					 */
  					goto retry;
  			} else {
b6a604518   Hugh Dickins   [PATCH] fix BUG_O...
3131
  				/* cache_grow already freed obj */
3c517a613   Christoph Lameter   [PATCH] slab: bet...
3132
3133
3134
  				obj = NULL;
  			}
  		}
aedb0eb10   Christoph Lameter   [PATCH] Slab: Do ...
3135
  	}
cc9a6c877   Mel Gorman   cpuset: mm: reduc...
3136
3137
3138
  
  	if (unlikely(!put_mems_allowed(cpuset_mems_cookie) && !obj))
  		goto retry_cpuset;
765c4507a   Christoph Lameter   [PATCH] GFP_THISN...
3139
3140
3141
3142
  	return obj;
  }
  
  /*
e498be7da   Christoph Lameter   [PATCH] Numa-awar...
3143
   * A interface to enable slab creation on nodeid
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3144
   */
8b98c1699   Christoph Hellwig   [PATCH] leak trac...
3145
  static void *____cache_alloc_node(struct kmem_cache *cachep, gfp_t flags,
a737b3e2f   Andrew Morton   [PATCH] slab cleanup
3146
  				int nodeid)
e498be7da   Christoph Lameter   [PATCH] Numa-awar...
3147
3148
  {
  	struct list_head *entry;
b28a02de8   Pekka Enberg   [PATCH] slab: fix...
3149
  	struct slab *slabp;
ce8eb6c42   Christoph Lameter   slab: Rename list...
3150
  	struct kmem_cache_node *n;
b28a02de8   Pekka Enberg   [PATCH] slab: fix...
3151
  	void *obj;
b28a02de8   Pekka Enberg   [PATCH] slab: fix...
3152
  	int x;
14e50c6a9   Aaron Tomlin   mm: slab: Verify ...
3153
  	VM_BUG_ON(nodeid > num_online_nodes());
ce8eb6c42   Christoph Lameter   slab: Rename list...
3154
3155
  	n = cachep->node[nodeid];
  	BUG_ON(!n);
b28a02de8   Pekka Enberg   [PATCH] slab: fix...
3156

a737b3e2f   Andrew Morton   [PATCH] slab cleanup
3157
  retry:
ca3b9b917   Ravikiran G Thirumalai   [PATCH] NUMA slab...
3158
  	check_irq_off();
ce8eb6c42   Christoph Lameter   slab: Rename list...
3159
3160
3161
3162
3163
3164
  	spin_lock(&n->list_lock);
  	entry = n->slabs_partial.next;
  	if (entry == &n->slabs_partial) {
  		n->free_touched = 1;
  		entry = n->slabs_free.next;
  		if (entry == &n->slabs_free)
b28a02de8   Pekka Enberg   [PATCH] slab: fix...
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
  			goto must_grow;
  	}
  
  	slabp = list_entry(entry, struct slab, list);
  	check_spinlock_acquired_node(cachep, nodeid);
  	check_slabp(cachep, slabp);
  
  	STATS_INC_NODEALLOCS(cachep);
  	STATS_INC_ACTIVE(cachep);
  	STATS_SET_HIGH(cachep);
  
  	BUG_ON(slabp->inuse == cachep->num);
78d382d77   Matthew Dobson   [PATCH] slab: ext...
3177
  	obj = slab_get_obj(cachep, slabp, nodeid);
b28a02de8   Pekka Enberg   [PATCH] slab: fix...
3178
  	check_slabp(cachep, slabp);
ce8eb6c42   Christoph Lameter   slab: Rename list...
3179
  	n->free_objects--;
b28a02de8   Pekka Enberg   [PATCH] slab: fix...
3180
3181
  	/* move slabp to correct slabp list: */
  	list_del(&slabp->list);
a737b3e2f   Andrew Morton   [PATCH] slab cleanup
3182
  	if (slabp->free == BUFCTL_END)
ce8eb6c42   Christoph Lameter   slab: Rename list...
3183
  		list_add(&slabp->list, &n->slabs_full);
a737b3e2f   Andrew Morton   [PATCH] slab cleanup
3184
  	else
ce8eb6c42   Christoph Lameter   slab: Rename list...
3185
  		list_add(&slabp->list, &n->slabs_partial);
e498be7da   Christoph Lameter   [PATCH] Numa-awar...
3186

ce8eb6c42   Christoph Lameter   slab: Rename list...
3187
  	spin_unlock(&n->list_lock);
b28a02de8   Pekka Enberg   [PATCH] slab: fix...
3188
  	goto done;
e498be7da   Christoph Lameter   [PATCH] Numa-awar...
3189

a737b3e2f   Andrew Morton   [PATCH] slab cleanup
3190
  must_grow:
ce8eb6c42   Christoph Lameter   slab: Rename list...
3191
  	spin_unlock(&n->list_lock);
3c517a613   Christoph Lameter   [PATCH] slab: bet...
3192
  	x = cache_grow(cachep, flags | GFP_THISNODE, nodeid, NULL);
765c4507a   Christoph Lameter   [PATCH] GFP_THISN...
3193
3194
  	if (x)
  		goto retry;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3195

8c8cc2c10   Pekka Enberg   [PATCH] slab: cac...
3196
  	return fallback_alloc(cachep, flags);
e498be7da   Christoph Lameter   [PATCH] Numa-awar...
3197

a737b3e2f   Andrew Morton   [PATCH] slab cleanup
3198
  done:
b28a02de8   Pekka Enberg   [PATCH] slab: fix...
3199
  	return obj;
e498be7da   Christoph Lameter   [PATCH] Numa-awar...
3200
  }
8c8cc2c10   Pekka Enberg   [PATCH] slab: cac...
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
  
  /**
   * kmem_cache_alloc_node - Allocate an object on the specified node
   * @cachep: The cache to allocate from.
   * @flags: See kmalloc().
   * @nodeid: node number of the target node.
   * @caller: return address of caller, used for debug information
   *
   * Identical to kmem_cache_alloc but it will allocate memory on the given
   * node, which can improve the performance for cpu bound structures.
   *
   * Fallback to other node is possible if __GFP_THISNODE is not set.
   */
  static __always_inline void *
48356303f   Ezequiel Garcia   mm, slab: Rename ...
3215
  slab_alloc_node(struct kmem_cache *cachep, gfp_t flags, int nodeid,
7c0cb9c64   Ezequiel Garcia   mm, slab: Replace...
3216
  		   unsigned long caller)
8c8cc2c10   Pekka Enberg   [PATCH] slab: cac...
3217
3218
3219
  {
  	unsigned long save_flags;
  	void *ptr;
7d6e6d09d   Lee Schermerhorn   numa: slab: use n...
3220
  	int slab_node = numa_mem_id();
8c8cc2c10   Pekka Enberg   [PATCH] slab: cac...
3221

dcce284a2   Benjamin Herrenschmidt   mm: Extend gfp ma...
3222
  	flags &= gfp_allowed_mask;
7e85ee0c1   Pekka Enberg   slab,slub: don't ...
3223

cf40bd16f   Nick Piggin   lockdep: annotate...
3224
  	lockdep_trace_alloc(flags);
773ff60e8   Akinobu Mita   SLUB: failslab su...
3225
  	if (slab_should_failslab(cachep, flags))
824ebef12   Akinobu Mita   fault injection: ...
3226
  		return NULL;
d79923fad   Glauber Costa   sl[au]b: allocate...
3227
  	cachep = memcg_kmem_get_cache(cachep, flags);
8c8cc2c10   Pekka Enberg   [PATCH] slab: cac...
3228
3229
  	cache_alloc_debugcheck_before(cachep, flags);
  	local_irq_save(save_flags);
eacbbae38   Andrew Morton   slab: use NUMA_NO...
3230
  	if (nodeid == NUMA_NO_NODE)
7d6e6d09d   Lee Schermerhorn   numa: slab: use n...
3231
  		nodeid = slab_node;
8c8cc2c10   Pekka Enberg   [PATCH] slab: cac...
3232

6a67368c3   Christoph Lameter   slab: Rename node...
3233
  	if (unlikely(!cachep->node[nodeid])) {
8c8cc2c10   Pekka Enberg   [PATCH] slab: cac...
3234
3235
3236
3237
  		/* Node not bootstrapped yet */
  		ptr = fallback_alloc(cachep, flags);
  		goto out;
  	}
7d6e6d09d   Lee Schermerhorn   numa: slab: use n...
3238
  	if (nodeid == slab_node) {
8c8cc2c10   Pekka Enberg   [PATCH] slab: cac...
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
  		/*
  		 * Use the locally cached objects if possible.
  		 * However ____cache_alloc does not allow fallback
  		 * to other nodes. It may fail while we still have
  		 * objects on other nodes available.
  		 */
  		ptr = ____cache_alloc(cachep, flags);
  		if (ptr)
  			goto out;
  	}
  	/* ___cache_alloc_node can fall back to other nodes */
  	ptr = ____cache_alloc_node(cachep, flags, nodeid);
    out:
  	local_irq_restore(save_flags);
  	ptr = cache_alloc_debugcheck_after(cachep, flags, ptr, caller);
8c138bc00   Christoph Lameter   slab: Get rid of ...
3254
  	kmemleak_alloc_recursive(ptr, cachep->object_size, 1, cachep->flags,
d5cff6352   Catalin Marinas   kmemleak: Add the...
3255
  				 flags);
8c8cc2c10   Pekka Enberg   [PATCH] slab: cac...
3256

c175eea46   Pekka Enberg   slab: add hooks f...
3257
  	if (likely(ptr))
8c138bc00   Christoph Lameter   slab: Get rid of ...
3258
  		kmemcheck_slab_alloc(cachep, flags, ptr, cachep->object_size);
c175eea46   Pekka Enberg   slab: add hooks f...
3259

d07dbea46   Christoph Lameter   Slab allocators: ...
3260
  	if (unlikely((flags & __GFP_ZERO) && ptr))
8c138bc00   Christoph Lameter   slab: Get rid of ...
3261
  		memset(ptr, 0, cachep->object_size);
d07dbea46   Christoph Lameter   Slab allocators: ...
3262

8c8cc2c10   Pekka Enberg   [PATCH] slab: cac...
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
  	return ptr;
  }
  
  static __always_inline void *
  __do_cache_alloc(struct kmem_cache *cache, gfp_t flags)
  {
  	void *objp;
  
  	if (unlikely(current->flags & (PF_SPREAD_SLAB | PF_MEMPOLICY))) {
  		objp = alternate_node_alloc(cache, flags);
  		if (objp)
  			goto out;
  	}
  	objp = ____cache_alloc(cache, flags);
  
  	/*
  	 * We may just have run out of memory on the local node.
  	 * ____cache_alloc_node() knows how to locate memory on other nodes
  	 */
7d6e6d09d   Lee Schermerhorn   numa: slab: use n...
3282
3283
  	if (!objp)
  		objp = ____cache_alloc_node(cache, flags, numa_mem_id());
8c8cc2c10   Pekka Enberg   [PATCH] slab: cac...
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
  
    out:
  	return objp;
  }
  #else
  
  static __always_inline void *
  __do_cache_alloc(struct kmem_cache *cachep, gfp_t flags)
  {
  	return ____cache_alloc(cachep, flags);
  }
  
  #endif /* CONFIG_NUMA */
  
  static __always_inline void *
48356303f   Ezequiel Garcia   mm, slab: Rename ...
3299
  slab_alloc(struct kmem_cache *cachep, gfp_t flags, unsigned long caller)
8c8cc2c10   Pekka Enberg   [PATCH] slab: cac...
3300
3301
3302
  {
  	unsigned long save_flags;
  	void *objp;
dcce284a2   Benjamin Herrenschmidt   mm: Extend gfp ma...
3303
  	flags &= gfp_allowed_mask;
7e85ee0c1   Pekka Enberg   slab,slub: don't ...
3304

cf40bd16f   Nick Piggin   lockdep: annotate...
3305
  	lockdep_trace_alloc(flags);
773ff60e8   Akinobu Mita   SLUB: failslab su...
3306
  	if (slab_should_failslab(cachep, flags))
824ebef12   Akinobu Mita   fault injection: ...
3307
  		return NULL;
d79923fad   Glauber Costa   sl[au]b: allocate...
3308
  	cachep = memcg_kmem_get_cache(cachep, flags);
8c8cc2c10   Pekka Enberg   [PATCH] slab: cac...
3309
3310
3311
3312
3313
  	cache_alloc_debugcheck_before(cachep, flags);
  	local_irq_save(save_flags);
  	objp = __do_cache_alloc(cachep, flags);
  	local_irq_restore(save_flags);
  	objp = cache_alloc_debugcheck_after(cachep, flags, objp, caller);
8c138bc00   Christoph Lameter   slab: Get rid of ...
3314
  	kmemleak_alloc_recursive(objp, cachep->object_size, 1, cachep->flags,
d5cff6352   Catalin Marinas   kmemleak: Add the...
3315
  				 flags);
8c8cc2c10   Pekka Enberg   [PATCH] slab: cac...
3316
  	prefetchw(objp);
c175eea46   Pekka Enberg   slab: add hooks f...
3317
  	if (likely(objp))
8c138bc00   Christoph Lameter   slab: Get rid of ...
3318
  		kmemcheck_slab_alloc(cachep, flags, objp, cachep->object_size);
c175eea46   Pekka Enberg   slab: add hooks f...
3319

d07dbea46   Christoph Lameter   Slab allocators: ...
3320
  	if (unlikely((flags & __GFP_ZERO) && objp))
8c138bc00   Christoph Lameter   slab: Get rid of ...
3321
  		memset(objp, 0, cachep->object_size);
d07dbea46   Christoph Lameter   Slab allocators: ...
3322

8c8cc2c10   Pekka Enberg   [PATCH] slab: cac...
3323
3324
  	return objp;
  }
e498be7da   Christoph Lameter   [PATCH] Numa-awar...
3325
3326
3327
3328
  
  /*
   * Caller needs to acquire correct kmem_list's list_lock
   */
343e0d7a9   Pekka Enberg   [PATCH] slab: rep...
3329
  static void free_block(struct kmem_cache *cachep, void **objpp, int nr_objects,
b28a02de8   Pekka Enberg   [PATCH] slab: fix...
3330
  		       int node)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3331
3332
  {
  	int i;
ce8eb6c42   Christoph Lameter   slab: Rename list...
3333
  	struct kmem_cache_node *n;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3334
3335
  
  	for (i = 0; i < nr_objects; i++) {
072bb0aa5   Mel Gorman   mm: sl[au]b: add ...
3336
  		void *objp;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3337
  		struct slab *slabp;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3338

072bb0aa5   Mel Gorman   mm: sl[au]b: add ...
3339
3340
  		clear_obj_pfmemalloc(&objpp[i]);
  		objp = objpp[i];
6ed5eb221   Pekka Enberg   [PATCH] slab: ext...
3341
  		slabp = virt_to_slab(objp);
ce8eb6c42   Christoph Lameter   slab: Rename list...
3342
  		n = cachep->node[node];
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3343
  		list_del(&slabp->list);
ff69416e6   Christoph Lameter   [PATCH] slab: fix...
3344
  		check_spinlock_acquired_node(cachep, node);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3345
  		check_slabp(cachep, slabp);
78d382d77   Matthew Dobson   [PATCH] slab: ext...
3346
  		slab_put_obj(cachep, slabp, objp, node);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3347
  		STATS_DEC_ACTIVE(cachep);
ce8eb6c42   Christoph Lameter   slab: Rename list...
3348
  		n->free_objects++;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3349
3350
3351
3352
  		check_slabp(cachep, slabp);
  
  		/* fixup slab chains */
  		if (slabp->inuse == 0) {
ce8eb6c42   Christoph Lameter   slab: Rename list...
3353
3354
  			if (n->free_objects > n->free_limit) {
  				n->free_objects -= cachep->num;
e5ac9c5ae   Ravikiran G Thirumalai   [PATCH] Add some ...
3355
3356
3357
3358
3359
3360
  				/* No need to drop any previously held
  				 * lock here, even if we have a off-slab slab
  				 * descriptor it is guaranteed to come from
  				 * a different cache, refer to comments before
  				 * alloc_slabmgmt.
  				 */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3361
3362
  				slab_destroy(cachep, slabp);
  			} else {
ce8eb6c42   Christoph Lameter   slab: Rename list...
3363
  				list_add(&slabp->list, &n->slabs_free);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3364
3365
3366
3367
3368
3369
  			}
  		} else {
  			/* Unconditionally move a slab to the end of the
  			 * partial list on free - maximum time for the
  			 * other objects to be freed, too.
  			 */
ce8eb6c42   Christoph Lameter   slab: Rename list...
3370
  			list_add_tail(&slabp->list, &n->slabs_partial);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3371
3372
3373
  		}
  	}
  }
343e0d7a9   Pekka Enberg   [PATCH] slab: rep...
3374
  static void cache_flusharray(struct kmem_cache *cachep, struct array_cache *ac)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3375
3376
  {
  	int batchcount;
ce8eb6c42   Christoph Lameter   slab: Rename list...
3377
  	struct kmem_cache_node *n;
7d6e6d09d   Lee Schermerhorn   numa: slab: use n...
3378
  	int node = numa_mem_id();
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3379
3380
3381
3382
3383
3384
  
  	batchcount = ac->batchcount;
  #if DEBUG
  	BUG_ON(!batchcount || batchcount > ac->avail);
  #endif
  	check_irq_off();
ce8eb6c42   Christoph Lameter   slab: Rename list...
3385
3386
3387
3388
  	n = cachep->node[node];
  	spin_lock(&n->list_lock);
  	if (n->shared) {
  		struct array_cache *shared_array = n->shared;
b28a02de8   Pekka Enberg   [PATCH] slab: fix...
3389
  		int max = shared_array->limit - shared_array->avail;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3390
3391
3392
  		if (max) {
  			if (batchcount > max)
  				batchcount = max;
e498be7da   Christoph Lameter   [PATCH] Numa-awar...
3393
  			memcpy(&(shared_array->entry[shared_array->avail]),
b28a02de8   Pekka Enberg   [PATCH] slab: fix...
3394
  			       ac->entry, sizeof(void *) * batchcount);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3395
3396
3397
3398
  			shared_array->avail += batchcount;
  			goto free_done;
  		}
  	}
ff69416e6   Christoph Lameter   [PATCH] slab: fix...
3399
  	free_block(cachep, ac->entry, batchcount, node);
a737b3e2f   Andrew Morton   [PATCH] slab cleanup
3400
  free_done:
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3401
3402
3403
3404
  #if STATS
  	{
  		int i = 0;
  		struct list_head *p;
ce8eb6c42   Christoph Lameter   slab: Rename list...
3405
3406
  		p = n->slabs_free.next;
  		while (p != &(n->slabs_free)) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
  			struct slab *slabp;
  
  			slabp = list_entry(p, struct slab, list);
  			BUG_ON(slabp->inuse);
  
  			i++;
  			p = p->next;
  		}
  		STATS_SET_FREEABLE(cachep, i);
  	}
  #endif
ce8eb6c42   Christoph Lameter   slab: Rename list...
3418
  	spin_unlock(&n->list_lock);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3419
  	ac->avail -= batchcount;
a737b3e2f   Andrew Morton   [PATCH] slab cleanup
3420
  	memmove(ac->entry, &(ac->entry[batchcount]), sizeof(void *)*ac->avail);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3421
3422
3423
  }
  
  /*
a737b3e2f   Andrew Morton   [PATCH] slab cleanup
3424
3425
   * Release an obj back to its cache. If the obj has a constructed state, it must
   * be in this state _before_ it is released.  Called with disabled ints.
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3426
   */
a947eb95e   Suleiman Souhlal   SLAB: Record actu...
3427
  static inline void __cache_free(struct kmem_cache *cachep, void *objp,
7c0cb9c64   Ezequiel Garcia   mm, slab: Replace...
3428
  				unsigned long caller)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3429
  {
9a2dba4b4   Pekka Enberg   [PATCH] slab: ren...
3430
  	struct array_cache *ac = cpu_cache_get(cachep);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3431
3432
  
  	check_irq_off();
d5cff6352   Catalin Marinas   kmemleak: Add the...
3433
  	kmemleak_free_recursive(objp, cachep->flags);
a947eb95e   Suleiman Souhlal   SLAB: Record actu...
3434
  	objp = cache_free_debugcheck(cachep, objp, caller);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3435

8c138bc00   Christoph Lameter   slab: Get rid of ...
3436
  	kmemcheck_slab_free(cachep, objp, cachep->object_size);
c175eea46   Pekka Enberg   slab: add hooks f...
3437

1807a1aaf   Siddha, Suresh B   slab: skip callin...
3438
3439
3440
3441
3442
3443
3444
  	/*
  	 * Skip calling cache_free_alien() when the platform is not numa.
  	 * This will avoid cache misses that happen while accessing slabp (which
  	 * is per page memory  reference) to get nodeid. Instead use a global
  	 * variable to skip the call, which is mostly likely to be present in
  	 * the cache.
  	 */
b6e68bc1b   Mel Gorman   page allocator: s...
3445
  	if (nr_online_nodes > 1 && cache_free_alien(cachep, objp))
729bd0b74   Pekka Enberg   [PATCH] slab: ext...
3446
  		return;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3447
3448
  	if (likely(ac->avail < ac->limit)) {
  		STATS_INC_FREEHIT(cachep);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3449
3450
3451
  	} else {
  		STATS_INC_FREEMISS(cachep);
  		cache_flusharray(cachep, ac);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3452
  	}
42c8c99cd   Zhao Jin   slab, cleanup: re...
3453

072bb0aa5   Mel Gorman   mm: sl[au]b: add ...
3454
  	ac_put_obj(cachep, ac, objp);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
  }
  
  /**
   * kmem_cache_alloc - Allocate an object
   * @cachep: The cache to allocate from.
   * @flags: See kmalloc().
   *
   * Allocate an object from this cache.  The flags are only relevant
   * if the cache has no available objects.
   */
343e0d7a9   Pekka Enberg   [PATCH] slab: rep...
3465
  void *kmem_cache_alloc(struct kmem_cache *cachep, gfp_t flags)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3466
  {
48356303f   Ezequiel Garcia   mm, slab: Rename ...
3467
  	void *ret = slab_alloc(cachep, flags, _RET_IP_);
36555751c   Eduard - Gabriel Munteanu   kmemtrace: SLAB h...
3468

ca2b84cb3   Eduard - Gabriel Munteanu   kmemtrace: use tr...
3469
  	trace_kmem_cache_alloc(_RET_IP_, ret,
8c138bc00   Christoph Lameter   slab: Get rid of ...
3470
  			       cachep->object_size, cachep->size, flags);
36555751c   Eduard - Gabriel Munteanu   kmemtrace: SLAB h...
3471
3472
  
  	return ret;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3473
3474
  }
  EXPORT_SYMBOL(kmem_cache_alloc);
0f24f1287   Li Zefan   tracing, slab: De...
3475
  #ifdef CONFIG_TRACING
85beb5869   Steven Rostedt   tracing/slab: Mov...
3476
  void *
4052147c0   Ezequiel Garcia   mm, slab: Match S...
3477
  kmem_cache_alloc_trace(struct kmem_cache *cachep, gfp_t flags, size_t size)
36555751c   Eduard - Gabriel Munteanu   kmemtrace: SLAB h...
3478
  {
85beb5869   Steven Rostedt   tracing/slab: Mov...
3479
  	void *ret;
48356303f   Ezequiel Garcia   mm, slab: Rename ...
3480
  	ret = slab_alloc(cachep, flags, _RET_IP_);
85beb5869   Steven Rostedt   tracing/slab: Mov...
3481
3482
  
  	trace_kmalloc(_RET_IP_, ret,
ff4fcd01e   Ezequiel Garcia   mm, slab: Remove ...
3483
  		      size, cachep->size, flags);
85beb5869   Steven Rostedt   tracing/slab: Mov...
3484
  	return ret;
36555751c   Eduard - Gabriel Munteanu   kmemtrace: SLAB h...
3485
  }
85beb5869   Steven Rostedt   tracing/slab: Mov...
3486
  EXPORT_SYMBOL(kmem_cache_alloc_trace);
36555751c   Eduard - Gabriel Munteanu   kmemtrace: SLAB h...
3487
  #endif
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3488
  #ifdef CONFIG_NUMA
8b98c1699   Christoph Hellwig   [PATCH] leak trac...
3489
3490
  void *kmem_cache_alloc_node(struct kmem_cache *cachep, gfp_t flags, int nodeid)
  {
48356303f   Ezequiel Garcia   mm, slab: Rename ...
3491
  	void *ret = slab_alloc_node(cachep, flags, nodeid, _RET_IP_);
36555751c   Eduard - Gabriel Munteanu   kmemtrace: SLAB h...
3492

ca2b84cb3   Eduard - Gabriel Munteanu   kmemtrace: use tr...
3493
  	trace_kmem_cache_alloc_node(_RET_IP_, ret,
8c138bc00   Christoph Lameter   slab: Get rid of ...
3494
  				    cachep->object_size, cachep->size,
ca2b84cb3   Eduard - Gabriel Munteanu   kmemtrace: use tr...
3495
  				    flags, nodeid);
36555751c   Eduard - Gabriel Munteanu   kmemtrace: SLAB h...
3496
3497
  
  	return ret;
8b98c1699   Christoph Hellwig   [PATCH] leak trac...
3498
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3499
  EXPORT_SYMBOL(kmem_cache_alloc_node);
0f24f1287   Li Zefan   tracing, slab: De...
3500
  #ifdef CONFIG_TRACING
4052147c0   Ezequiel Garcia   mm, slab: Match S...
3501
  void *kmem_cache_alloc_node_trace(struct kmem_cache *cachep,
85beb5869   Steven Rostedt   tracing/slab: Mov...
3502
  				  gfp_t flags,
4052147c0   Ezequiel Garcia   mm, slab: Match S...
3503
3504
  				  int nodeid,
  				  size_t size)
36555751c   Eduard - Gabriel Munteanu   kmemtrace: SLAB h...
3505
  {
85beb5869   Steven Rostedt   tracing/slab: Mov...
3506
  	void *ret;
592f41450   Ezequiel Garcia   mm/slab: Fix typo...
3507
  	ret = slab_alloc_node(cachep, flags, nodeid, _RET_IP_);
7c0cb9c64   Ezequiel Garcia   mm, slab: Replace...
3508

85beb5869   Steven Rostedt   tracing/slab: Mov...
3509
  	trace_kmalloc_node(_RET_IP_, ret,
ff4fcd01e   Ezequiel Garcia   mm, slab: Remove ...
3510
  			   size, cachep->size,
85beb5869   Steven Rostedt   tracing/slab: Mov...
3511
3512
  			   flags, nodeid);
  	return ret;
36555751c   Eduard - Gabriel Munteanu   kmemtrace: SLAB h...
3513
  }
85beb5869   Steven Rostedt   tracing/slab: Mov...
3514
  EXPORT_SYMBOL(kmem_cache_alloc_node_trace);
36555751c   Eduard - Gabriel Munteanu   kmemtrace: SLAB h...
3515
  #endif
8b98c1699   Christoph Hellwig   [PATCH] leak trac...
3516
  static __always_inline void *
7c0cb9c64   Ezequiel Garcia   mm, slab: Replace...
3517
  __do_kmalloc_node(size_t size, gfp_t flags, int node, unsigned long caller)
97e2bde47   Manfred Spraul   [PATCH] add kmall...
3518
  {
343e0d7a9   Pekka Enberg   [PATCH] slab: rep...
3519
  	struct kmem_cache *cachep;
97e2bde47   Manfred Spraul   [PATCH] add kmall...
3520

2c59dd654   Christoph Lameter   slab: Common Kmal...
3521
  	cachep = kmalloc_slab(size, flags);
6cb8f9132   Christoph Lameter   Slab allocators: ...
3522
3523
  	if (unlikely(ZERO_OR_NULL_PTR(cachep)))
  		return cachep;
4052147c0   Ezequiel Garcia   mm, slab: Match S...
3524
  	return kmem_cache_alloc_node_trace(cachep, flags, node, size);
97e2bde47   Manfred Spraul   [PATCH] add kmall...
3525
  }
8b98c1699   Christoph Hellwig   [PATCH] leak trac...
3526

0bb38a5cd   Li Zefan   tracing, slab: Fi...
3527
  #if defined(CONFIG_DEBUG_SLAB) || defined(CONFIG_TRACING)
8b98c1699   Christoph Hellwig   [PATCH] leak trac...
3528
3529
  void *__kmalloc_node(size_t size, gfp_t flags, int node)
  {
7c0cb9c64   Ezequiel Garcia   mm, slab: Replace...
3530
  	return __do_kmalloc_node(size, flags, node, _RET_IP_);
8b98c1699   Christoph Hellwig   [PATCH] leak trac...
3531
  }
dbe5e69d2   Christoph Hellwig   [PATCH] slab: opt...
3532
  EXPORT_SYMBOL(__kmalloc_node);
8b98c1699   Christoph Hellwig   [PATCH] leak trac...
3533
3534
  
  void *__kmalloc_node_track_caller(size_t size, gfp_t flags,
ce71e27c6   Eduard - Gabriel Munteanu   SLUB: Replace __b...
3535
  		int node, unsigned long caller)
8b98c1699   Christoph Hellwig   [PATCH] leak trac...
3536
  {
7c0cb9c64   Ezequiel Garcia   mm, slab: Replace...
3537
  	return __do_kmalloc_node(size, flags, node, caller);
8b98c1699   Christoph Hellwig   [PATCH] leak trac...
3538
3539
3540
3541
3542
  }
  EXPORT_SYMBOL(__kmalloc_node_track_caller);
  #else
  void *__kmalloc_node(size_t size, gfp_t flags, int node)
  {
7c0cb9c64   Ezequiel Garcia   mm, slab: Replace...
3543
  	return __do_kmalloc_node(size, flags, node, 0);
8b98c1699   Christoph Hellwig   [PATCH] leak trac...
3544
3545
  }
  EXPORT_SYMBOL(__kmalloc_node);
0bb38a5cd   Li Zefan   tracing, slab: Fi...
3546
  #endif /* CONFIG_DEBUG_SLAB || CONFIG_TRACING */
8b98c1699   Christoph Hellwig   [PATCH] leak trac...
3547
  #endif /* CONFIG_NUMA */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3548
3549
  
  /**
800590f52   Paul Drynoff   [PATCH] slab: kma...
3550
   * __do_kmalloc - allocate memory
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3551
   * @size: how many bytes of memory are required.
800590f52   Paul Drynoff   [PATCH] slab: kma...
3552
   * @flags: the type of memory to allocate (see kmalloc).
911851e6e   Randy Dunlap   [PATCH] slab: fix...
3553
   * @caller: function caller for debug tracking of the caller
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3554
   */
7fd6b1413   Pekka Enberg   [PATCH] slab: fix...
3555
  static __always_inline void *__do_kmalloc(size_t size, gfp_t flags,
7c0cb9c64   Ezequiel Garcia   mm, slab: Replace...
3556
  					  unsigned long caller)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3557
  {
343e0d7a9   Pekka Enberg   [PATCH] slab: rep...
3558
  	struct kmem_cache *cachep;
36555751c   Eduard - Gabriel Munteanu   kmemtrace: SLAB h...
3559
  	void *ret;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3560

97e2bde47   Manfred Spraul   [PATCH] add kmall...
3561
3562
3563
3564
3565
  	/* If you want to save a few bytes .text space: replace
  	 * __ with kmem_.
  	 * Then kmalloc uses the uninlined functions instead of the inline
  	 * functions.
  	 */
2c59dd654   Christoph Lameter   slab: Common Kmal...
3566
  	cachep = kmalloc_slab(size, flags);
a5c96d8a1   Linus Torvalds   Fix up non-NUMA S...
3567
3568
  	if (unlikely(ZERO_OR_NULL_PTR(cachep)))
  		return cachep;
48356303f   Ezequiel Garcia   mm, slab: Rename ...
3569
  	ret = slab_alloc(cachep, flags, caller);
36555751c   Eduard - Gabriel Munteanu   kmemtrace: SLAB h...
3570

7c0cb9c64   Ezequiel Garcia   mm, slab: Replace...
3571
  	trace_kmalloc(caller, ret,
3b0efdfa1   Christoph Lameter   mm, sl[aou]b: Ext...
3572
  		      size, cachep->size, flags);
36555751c   Eduard - Gabriel Munteanu   kmemtrace: SLAB h...
3573
3574
  
  	return ret;
7fd6b1413   Pekka Enberg   [PATCH] slab: fix...
3575
  }
7fd6b1413   Pekka Enberg   [PATCH] slab: fix...
3576

0bb38a5cd   Li Zefan   tracing, slab: Fi...
3577
  #if defined(CONFIG_DEBUG_SLAB) || defined(CONFIG_TRACING)
7fd6b1413   Pekka Enberg   [PATCH] slab: fix...
3578
3579
  void *__kmalloc(size_t size, gfp_t flags)
  {
7c0cb9c64   Ezequiel Garcia   mm, slab: Replace...
3580
  	return __do_kmalloc(size, flags, _RET_IP_);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3581
3582
  }
  EXPORT_SYMBOL(__kmalloc);
ce71e27c6   Eduard - Gabriel Munteanu   SLUB: Replace __b...
3583
  void *__kmalloc_track_caller(size_t size, gfp_t flags, unsigned long caller)
7fd6b1413   Pekka Enberg   [PATCH] slab: fix...
3584
  {
7c0cb9c64   Ezequiel Garcia   mm, slab: Replace...
3585
  	return __do_kmalloc(size, flags, caller);
7fd6b1413   Pekka Enberg   [PATCH] slab: fix...
3586
3587
  }
  EXPORT_SYMBOL(__kmalloc_track_caller);
1d2c8eea6   Christoph Hellwig   [PATCH] slab: cle...
3588
3589
3590
3591
  
  #else
  void *__kmalloc(size_t size, gfp_t flags)
  {
7c0cb9c64   Ezequiel Garcia   mm, slab: Replace...
3592
  	return __do_kmalloc(size, flags, 0);
1d2c8eea6   Christoph Hellwig   [PATCH] slab: cle...
3593
3594
  }
  EXPORT_SYMBOL(__kmalloc);
7fd6b1413   Pekka Enberg   [PATCH] slab: fix...
3595
  #endif
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3596
3597
3598
3599
3600
3601
3602
3603
  /**
   * kmem_cache_free - Deallocate an object
   * @cachep: The cache the allocation was from.
   * @objp: The previously allocated object.
   *
   * Free an object which was previously allocated from this
   * cache.
   */
343e0d7a9   Pekka Enberg   [PATCH] slab: rep...
3604
  void kmem_cache_free(struct kmem_cache *cachep, void *objp)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3605
3606
  {
  	unsigned long flags;
b9ce5ef49   Glauber Costa   sl[au]b: always g...
3607
3608
3609
  	cachep = cache_from_obj(cachep, objp);
  	if (!cachep)
  		return;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3610
3611
  
  	local_irq_save(flags);
d97d476b1   Feng Tang   slab: Fix a typo ...
3612
  	debug_check_no_locks_freed(objp, cachep->object_size);
3ac7fe5a4   Thomas Gleixner   infrastructure to...
3613
  	if (!(cachep->flags & SLAB_DEBUG_OBJECTS))
8c138bc00   Christoph Lameter   slab: Get rid of ...
3614
  		debug_check_no_obj_freed(objp, cachep->object_size);
7c0cb9c64   Ezequiel Garcia   mm, slab: Replace...
3615
  	__cache_free(cachep, objp, _RET_IP_);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3616
  	local_irq_restore(flags);
36555751c   Eduard - Gabriel Munteanu   kmemtrace: SLAB h...
3617

ca2b84cb3   Eduard - Gabriel Munteanu   kmemtrace: use tr...
3618
  	trace_kmem_cache_free(_RET_IP_, objp);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3619
3620
3621
3622
  }
  EXPORT_SYMBOL(kmem_cache_free);
  
  /**
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3623
3624
3625
   * kfree - free previously allocated memory
   * @objp: pointer returned by kmalloc.
   *
80e93effc   Pekka Enberg   [PATCH] update kf...
3626
3627
   * If @objp is NULL, no operation is performed.
   *
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3628
3629
3630
3631
3632
   * Don't free memory not originally allocated by kmalloc()
   * or you will run into trouble.
   */
  void kfree(const void *objp)
  {
343e0d7a9   Pekka Enberg   [PATCH] slab: rep...
3633
  	struct kmem_cache *c;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3634
  	unsigned long flags;
2121db74b   Pekka Enberg   kmemtrace: trace ...
3635
  	trace_kfree(_RET_IP_, objp);
6cb8f9132   Christoph Lameter   Slab allocators: ...
3636
  	if (unlikely(ZERO_OR_NULL_PTR(objp)))
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3637
3638
3639
  		return;
  	local_irq_save(flags);
  	kfree_debugcheck(objp);
6ed5eb221   Pekka Enberg   [PATCH] slab: ext...
3640
  	c = virt_to_cache(objp);
8c138bc00   Christoph Lameter   slab: Get rid of ...
3641
3642
3643
  	debug_check_no_locks_freed(objp, c->object_size);
  
  	debug_check_no_obj_freed(objp, c->object_size);
7c0cb9c64   Ezequiel Garcia   mm, slab: Replace...
3644
  	__cache_free(c, (void *)objp, _RET_IP_);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3645
3646
3647
  	local_irq_restore(flags);
  }
  EXPORT_SYMBOL(kfree);
e498be7da   Christoph Lameter   [PATCH] Numa-awar...
3648
  /*
ce8eb6c42   Christoph Lameter   slab: Rename list...
3649
   * This initializes kmem_cache_node or resizes various caches for all nodes.
e498be7da   Christoph Lameter   [PATCH] Numa-awar...
3650
   */
83b519e8b   Pekka Enberg   slab: setup alloc...
3651
  static int alloc_kmemlist(struct kmem_cache *cachep, gfp_t gfp)
e498be7da   Christoph Lameter   [PATCH] Numa-awar...
3652
3653
  {
  	int node;
ce8eb6c42   Christoph Lameter   slab: Rename list...
3654
  	struct kmem_cache_node *n;
cafeb02e0   Christoph Lameter   [PATCH] alloc_kme...
3655
  	struct array_cache *new_shared;
3395ee058   Paul Menage   [PATCH] mm: add n...
3656
  	struct array_cache **new_alien = NULL;
e498be7da   Christoph Lameter   [PATCH] Numa-awar...
3657

9c09a95cf   Mel Gorman   slab: partially r...
3658
  	for_each_online_node(node) {
cafeb02e0   Christoph Lameter   [PATCH] alloc_kme...
3659

3395ee058   Paul Menage   [PATCH] mm: add n...
3660
                  if (use_alien_caches) {
83b519e8b   Pekka Enberg   slab: setup alloc...
3661
                          new_alien = alloc_alien_cache(node, cachep->limit, gfp);
3395ee058   Paul Menage   [PATCH] mm: add n...
3662
3663
3664
                          if (!new_alien)
                                  goto fail;
                  }
cafeb02e0   Christoph Lameter   [PATCH] alloc_kme...
3665

631098469   Eric Dumazet   SLAB: don't alloc...
3666
3667
3668
  		new_shared = NULL;
  		if (cachep->shared) {
  			new_shared = alloc_arraycache(node,
0718dc2a8   Christoph Lameter   [PATCH] slab: fix...
3669
  				cachep->shared*cachep->batchcount,
83b519e8b   Pekka Enberg   slab: setup alloc...
3670
  					0xbaadf00d, gfp);
631098469   Eric Dumazet   SLAB: don't alloc...
3671
3672
3673
3674
  			if (!new_shared) {
  				free_alien_cache(new_alien);
  				goto fail;
  			}
0718dc2a8   Christoph Lameter   [PATCH] slab: fix...
3675
  		}
cafeb02e0   Christoph Lameter   [PATCH] alloc_kme...
3676

ce8eb6c42   Christoph Lameter   slab: Rename list...
3677
3678
3679
  		n = cachep->node[node];
  		if (n) {
  			struct array_cache *shared = n->shared;
cafeb02e0   Christoph Lameter   [PATCH] alloc_kme...
3680

ce8eb6c42   Christoph Lameter   slab: Rename list...
3681
  			spin_lock_irq(&n->list_lock);
e498be7da   Christoph Lameter   [PATCH] Numa-awar...
3682

cafeb02e0   Christoph Lameter   [PATCH] alloc_kme...
3683
  			if (shared)
0718dc2a8   Christoph Lameter   [PATCH] slab: fix...
3684
3685
  				free_block(cachep, shared->entry,
  						shared->avail, node);
e498be7da   Christoph Lameter   [PATCH] Numa-awar...
3686

ce8eb6c42   Christoph Lameter   slab: Rename list...
3687
3688
3689
  			n->shared = new_shared;
  			if (!n->alien) {
  				n->alien = new_alien;
e498be7da   Christoph Lameter   [PATCH] Numa-awar...
3690
3691
  				new_alien = NULL;
  			}
ce8eb6c42   Christoph Lameter   slab: Rename list...
3692
  			n->free_limit = (1 + nr_cpus_node(node)) *
a737b3e2f   Andrew Morton   [PATCH] slab cleanup
3693
  					cachep->batchcount + cachep->num;
ce8eb6c42   Christoph Lameter   slab: Rename list...
3694
  			spin_unlock_irq(&n->list_lock);
cafeb02e0   Christoph Lameter   [PATCH] alloc_kme...
3695
  			kfree(shared);
e498be7da   Christoph Lameter   [PATCH] Numa-awar...
3696
3697
3698
  			free_alien_cache(new_alien);
  			continue;
  		}
ce8eb6c42   Christoph Lameter   slab: Rename list...
3699
3700
  		n = kmalloc_node(sizeof(struct kmem_cache_node), gfp, node);
  		if (!n) {
0718dc2a8   Christoph Lameter   [PATCH] slab: fix...
3701
3702
  			free_alien_cache(new_alien);
  			kfree(new_shared);
e498be7da   Christoph Lameter   [PATCH] Numa-awar...
3703
  			goto fail;
0718dc2a8   Christoph Lameter   [PATCH] slab: fix...
3704
  		}
e498be7da   Christoph Lameter   [PATCH] Numa-awar...
3705

ce8eb6c42   Christoph Lameter   slab: Rename list...
3706
3707
  		kmem_cache_node_init(n);
  		n->next_reap = jiffies + REAPTIMEOUT_LIST3 +
a737b3e2f   Andrew Morton   [PATCH] slab cleanup
3708
  				((unsigned long)cachep) % REAPTIMEOUT_LIST3;
ce8eb6c42   Christoph Lameter   slab: Rename list...
3709
3710
3711
  		n->shared = new_shared;
  		n->alien = new_alien;
  		n->free_limit = (1 + nr_cpus_node(node)) *
a737b3e2f   Andrew Morton   [PATCH] slab cleanup
3712
  					cachep->batchcount + cachep->num;
ce8eb6c42   Christoph Lameter   slab: Rename list...
3713
  		cachep->node[node] = n;
e498be7da   Christoph Lameter   [PATCH] Numa-awar...
3714
  	}
cafeb02e0   Christoph Lameter   [PATCH] alloc_kme...
3715
  	return 0;
0718dc2a8   Christoph Lameter   [PATCH] slab: fix...
3716

a737b3e2f   Andrew Morton   [PATCH] slab cleanup
3717
  fail:
3b0efdfa1   Christoph Lameter   mm, sl[aou]b: Ext...
3718
  	if (!cachep->list.next) {
0718dc2a8   Christoph Lameter   [PATCH] slab: fix...
3719
3720
3721
  		/* Cache is not active yet. Roll back what we did */
  		node--;
  		while (node >= 0) {
6a67368c3   Christoph Lameter   slab: Rename node...
3722
  			if (cachep->node[node]) {
ce8eb6c42   Christoph Lameter   slab: Rename list...
3723
  				n = cachep->node[node];
0718dc2a8   Christoph Lameter   [PATCH] slab: fix...
3724

ce8eb6c42   Christoph Lameter   slab: Rename list...
3725
3726
3727
  				kfree(n->shared);
  				free_alien_cache(n->alien);
  				kfree(n);
6a67368c3   Christoph Lameter   slab: Rename node...
3728
  				cachep->node[node] = NULL;
0718dc2a8   Christoph Lameter   [PATCH] slab: fix...
3729
3730
3731
3732
  			}
  			node--;
  		}
  	}
cafeb02e0   Christoph Lameter   [PATCH] alloc_kme...
3733
  	return -ENOMEM;
e498be7da   Christoph Lameter   [PATCH] Numa-awar...
3734
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3735
  struct ccupdate_struct {
343e0d7a9   Pekka Enberg   [PATCH] slab: rep...
3736
  	struct kmem_cache *cachep;
acfe7d744   Eric Dumazet   slab: remove one ...
3737
  	struct array_cache *new[0];
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3738
3739
3740
3741
  };
  
  static void do_ccupdate_local(void *info)
  {
a737b3e2f   Andrew Morton   [PATCH] slab cleanup
3742
  	struct ccupdate_struct *new = info;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3743
3744
3745
  	struct array_cache *old;
  
  	check_irq_off();
9a2dba4b4   Pekka Enberg   [PATCH] slab: ren...
3746
  	old = cpu_cache_get(new->cachep);
e498be7da   Christoph Lameter   [PATCH] Numa-awar...
3747

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3748
3749
3750
  	new->cachep->array[smp_processor_id()] = new->new[smp_processor_id()];
  	new->new[smp_processor_id()] = old;
  }
18004c5d4   Christoph Lameter   mm, sl[aou]b: Use...
3751
  /* Always called with the slab_mutex held */
943a451a8   Glauber Costa   slab: propagate t...
3752
  static int __do_tune_cpucache(struct kmem_cache *cachep, int limit,
83b519e8b   Pekka Enberg   slab: setup alloc...
3753
  				int batchcount, int shared, gfp_t gfp)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3754
  {
d2e7b7d0a   Siddha, Suresh B   [PATCH] fix poten...
3755
  	struct ccupdate_struct *new;
2ed3a4ef9   Christoph Lameter   [PATCH] slab: do ...
3756
  	int i;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3757

acfe7d744   Eric Dumazet   slab: remove one ...
3758
3759
  	new = kzalloc(sizeof(*new) + nr_cpu_ids * sizeof(struct array_cache *),
  		      gfp);
d2e7b7d0a   Siddha, Suresh B   [PATCH] fix poten...
3760
3761
  	if (!new)
  		return -ENOMEM;
e498be7da   Christoph Lameter   [PATCH] Numa-awar...
3762
  	for_each_online_cpu(i) {
7d6e6d09d   Lee Schermerhorn   numa: slab: use n...
3763
  		new->new[i] = alloc_arraycache(cpu_to_mem(i), limit,
83b519e8b   Pekka Enberg   slab: setup alloc...
3764
  						batchcount, gfp);
d2e7b7d0a   Siddha, Suresh B   [PATCH] fix poten...
3765
  		if (!new->new[i]) {
b28a02de8   Pekka Enberg   [PATCH] slab: fix...
3766
  			for (i--; i >= 0; i--)
d2e7b7d0a   Siddha, Suresh B   [PATCH] fix poten...
3767
3768
  				kfree(new->new[i]);
  			kfree(new);
e498be7da   Christoph Lameter   [PATCH] Numa-awar...
3769
  			return -ENOMEM;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3770
3771
  		}
  	}
d2e7b7d0a   Siddha, Suresh B   [PATCH] fix poten...
3772
  	new->cachep = cachep;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3773

15c8b6c1a   Jens Axboe   on_each_cpu(): ki...
3774
  	on_each_cpu(do_ccupdate_local, (void *)new, 1);
e498be7da   Christoph Lameter   [PATCH] Numa-awar...
3775

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3776
  	check_irq_on();
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3777
3778
  	cachep->batchcount = batchcount;
  	cachep->limit = limit;
e498be7da   Christoph Lameter   [PATCH] Numa-awar...
3779
  	cachep->shared = shared;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3780

e498be7da   Christoph Lameter   [PATCH] Numa-awar...
3781
  	for_each_online_cpu(i) {
d2e7b7d0a   Siddha, Suresh B   [PATCH] fix poten...
3782
  		struct array_cache *ccold = new->new[i];
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3783
3784
  		if (!ccold)
  			continue;
6a67368c3   Christoph Lameter   slab: Rename node...
3785
  		spin_lock_irq(&cachep->node[cpu_to_mem(i)]->list_lock);
7d6e6d09d   Lee Schermerhorn   numa: slab: use n...
3786
  		free_block(cachep, ccold->entry, ccold->avail, cpu_to_mem(i));
6a67368c3   Christoph Lameter   slab: Rename node...
3787
  		spin_unlock_irq(&cachep->node[cpu_to_mem(i)]->list_lock);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3788
3789
  		kfree(ccold);
  	}
d2e7b7d0a   Siddha, Suresh B   [PATCH] fix poten...
3790
  	kfree(new);
83b519e8b   Pekka Enberg   slab: setup alloc...
3791
  	return alloc_kmemlist(cachep, gfp);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3792
  }
943a451a8   Glauber Costa   slab: propagate t...
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
  static int do_tune_cpucache(struct kmem_cache *cachep, int limit,
  				int batchcount, int shared, gfp_t gfp)
  {
  	int ret;
  	struct kmem_cache *c = NULL;
  	int i = 0;
  
  	ret = __do_tune_cpucache(cachep, limit, batchcount, shared, gfp);
  
  	if (slab_state < FULL)
  		return ret;
  
  	if ((ret < 0) || !is_root_cache(cachep))
  		return ret;
ebe945c27   Glauber Costa   memcg: add commen...
3807
  	VM_BUG_ON(!mutex_is_locked(&slab_mutex));
943a451a8   Glauber Costa   slab: propagate t...
3808
3809
3810
3811
3812
3813
3814
3815
3816
  	for_each_memcg_cache_index(i) {
  		c = cache_from_memcg(cachep, i);
  		if (c)
  			/* return value determined by the parent cache only */
  			__do_tune_cpucache(c, limit, batchcount, shared, gfp);
  	}
  
  	return ret;
  }
18004c5d4   Christoph Lameter   mm, sl[aou]b: Use...
3817
  /* Called with slab_mutex held always */
83b519e8b   Pekka Enberg   slab: setup alloc...
3818
  static int enable_cpucache(struct kmem_cache *cachep, gfp_t gfp)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3819
3820
  {
  	int err;
943a451a8   Glauber Costa   slab: propagate t...
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
  	int limit = 0;
  	int shared = 0;
  	int batchcount = 0;
  
  	if (!is_root_cache(cachep)) {
  		struct kmem_cache *root = memcg_root_cache(cachep);
  		limit = root->limit;
  		shared = root->shared;
  		batchcount = root->batchcount;
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3831

943a451a8   Glauber Costa   slab: propagate t...
3832
3833
  	if (limit && shared && batchcount)
  		goto skip_setup;
a737b3e2f   Andrew Morton   [PATCH] slab cleanup
3834
3835
  	/*
  	 * The head array serves three purposes:
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3836
3837
  	 * - create a LIFO ordering, i.e. return objects that are cache-warm
  	 * - reduce the number of spinlock operations.
a737b3e2f   Andrew Morton   [PATCH] slab cleanup
3838
  	 * - reduce the number of linked list operations on the slab and
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3839
3840
3841
3842
  	 *   bufctl chains: array operations are cheaper.
  	 * The numbers are guessed, we should auto-tune as described by
  	 * Bonwick.
  	 */
3b0efdfa1   Christoph Lameter   mm, sl[aou]b: Ext...
3843
  	if (cachep->size > 131072)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3844
  		limit = 1;
3b0efdfa1   Christoph Lameter   mm, sl[aou]b: Ext...
3845
  	else if (cachep->size > PAGE_SIZE)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3846
  		limit = 8;
3b0efdfa1   Christoph Lameter   mm, sl[aou]b: Ext...
3847
  	else if (cachep->size > 1024)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3848
  		limit = 24;
3b0efdfa1   Christoph Lameter   mm, sl[aou]b: Ext...
3849
  	else if (cachep->size > 256)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3850
3851
3852
  		limit = 54;
  	else
  		limit = 120;
a737b3e2f   Andrew Morton   [PATCH] slab cleanup
3853
3854
  	/*
  	 * CPU bound tasks (e.g. network routing) can exhibit cpu bound
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3855
3856
3857
3858
3859
3860
3861
3862
  	 * allocation behaviour: Most allocs on one cpu, most free operations
  	 * on another cpu. For these cases, an efficient object passing between
  	 * cpus is necessary. This is provided by a shared array. The array
  	 * replaces Bonwick's magazine layer.
  	 * On uniprocessor, it's functionally equivalent (but less efficient)
  	 * to a larger limit. Thus disabled by default.
  	 */
  	shared = 0;
3b0efdfa1   Christoph Lameter   mm, sl[aou]b: Ext...
3863
  	if (cachep->size <= PAGE_SIZE && num_possible_cpus() > 1)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3864
  		shared = 8;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3865
3866
  
  #if DEBUG
a737b3e2f   Andrew Morton   [PATCH] slab cleanup
3867
3868
3869
  	/*
  	 * With debugging enabled, large batchcount lead to excessively long
  	 * periods with disabled local interrupts. Limit the batchcount
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3870
3871
3872
3873
  	 */
  	if (limit > 32)
  		limit = 32;
  #endif
943a451a8   Glauber Costa   slab: propagate t...
3874
3875
3876
  	batchcount = (limit + 1) / 2;
  skip_setup:
  	err = do_tune_cpucache(cachep, limit, batchcount, shared, gfp);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3877
3878
3879
  	if (err)
  		printk(KERN_ERR "enable_cpucache failed for %s, error %d.
  ",
b28a02de8   Pekka Enberg   [PATCH] slab: fix...
3880
  		       cachep->name, -err);
2ed3a4ef9   Christoph Lameter   [PATCH] slab: do ...
3881
  	return err;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3882
  }
1b55253a7   Christoph Lameter   [PATCH] slab: rem...
3883
  /*
ce8eb6c42   Christoph Lameter   slab: Rename list...
3884
3885
   * Drain an array if it contains any elements taking the node lock only if
   * necessary. Note that the node listlock also protects the array_cache
b18e7e654   Christoph Lameter   [PATCH] slab: fix...
3886
   * if drain_array() is used on the shared array.
1b55253a7   Christoph Lameter   [PATCH] slab: rem...
3887
   */
ce8eb6c42   Christoph Lameter   slab: Rename list...
3888
  static void drain_array(struct kmem_cache *cachep, struct kmem_cache_node *n,
1b55253a7   Christoph Lameter   [PATCH] slab: rem...
3889
  			 struct array_cache *ac, int force, int node)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3890
3891
  {
  	int tofree;
1b55253a7   Christoph Lameter   [PATCH] slab: rem...
3892
3893
  	if (!ac || !ac->avail)
  		return;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3894
3895
  	if (ac->touched && !force) {
  		ac->touched = 0;
b18e7e654   Christoph Lameter   [PATCH] slab: fix...
3896
  	} else {
ce8eb6c42   Christoph Lameter   slab: Rename list...
3897
  		spin_lock_irq(&n->list_lock);
b18e7e654   Christoph Lameter   [PATCH] slab: fix...
3898
3899
3900
3901
3902
3903
3904
3905
3906
  		if (ac->avail) {
  			tofree = force ? ac->avail : (ac->limit + 4) / 5;
  			if (tofree > ac->avail)
  				tofree = (ac->avail + 1) / 2;
  			free_block(cachep, ac->entry, tofree, node);
  			ac->avail -= tofree;
  			memmove(ac->entry, &(ac->entry[tofree]),
  				sizeof(void *) * ac->avail);
  		}
ce8eb6c42   Christoph Lameter   slab: Rename list...
3907
  		spin_unlock_irq(&n->list_lock);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3908
3909
3910
3911
3912
  	}
  }
  
  /**
   * cache_reap - Reclaim memory from caches.
05fb6bf0b   Randy Dunlap   [PATCH] kernel-do...
3913
   * @w: work descriptor
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3914
3915
3916
3917
3918
3919
   *
   * Called from workqueue/eventd every few seconds.
   * Purpose:
   * - clear the per-cpu caches for this CPU.
   * - return freeable pages to the main free memory pool.
   *
a737b3e2f   Andrew Morton   [PATCH] slab cleanup
3920
3921
   * If we cannot acquire the cache chain mutex then just give up - we'll try
   * again on the next iteration.
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3922
   */
7c5cae368   Christoph Lameter   [PATCH] slab: use...
3923
  static void cache_reap(struct work_struct *w)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3924
  {
7a7c381d2   Christoph Hellwig   [PATCH] slab: sto...
3925
  	struct kmem_cache *searchp;
ce8eb6c42   Christoph Lameter   slab: Rename list...
3926
  	struct kmem_cache_node *n;
7d6e6d09d   Lee Schermerhorn   numa: slab: use n...
3927
  	int node = numa_mem_id();
bf6aede71   Jean Delvare   workqueue: add to...
3928
  	struct delayed_work *work = to_delayed_work(w);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3929

18004c5d4   Christoph Lameter   mm, sl[aou]b: Use...
3930
  	if (!mutex_trylock(&slab_mutex))
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3931
  		/* Give up. Setup the next iteration. */
7c5cae368   Christoph Lameter   [PATCH] slab: use...
3932
  		goto out;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3933

18004c5d4   Christoph Lameter   mm, sl[aou]b: Use...
3934
  	list_for_each_entry(searchp, &slab_caches, list) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3935
  		check_irq_on();
35386e3b0   Christoph Lameter   [PATCH] slab: cac...
3936
  		/*
ce8eb6c42   Christoph Lameter   slab: Rename list...
3937
  		 * We only take the node lock if absolutely necessary and we
35386e3b0   Christoph Lameter   [PATCH] slab: cac...
3938
3939
3940
  		 * have established with reasonable certainty that
  		 * we can do some work if the lock was obtained.
  		 */
ce8eb6c42   Christoph Lameter   slab: Rename list...
3941
  		n = searchp->node[node];
35386e3b0   Christoph Lameter   [PATCH] slab: cac...
3942

ce8eb6c42   Christoph Lameter   slab: Rename list...
3943
  		reap_alien(searchp, n);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3944

ce8eb6c42   Christoph Lameter   slab: Rename list...
3945
  		drain_array(searchp, n, cpu_cache_get(searchp), 0, node);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3946

35386e3b0   Christoph Lameter   [PATCH] slab: cac...
3947
3948
3949
3950
  		/*
  		 * These are racy checks but it does not matter
  		 * if we skip one check or scan twice.
  		 */
ce8eb6c42   Christoph Lameter   slab: Rename list...
3951
  		if (time_after(n->next_reap, jiffies))
35386e3b0   Christoph Lameter   [PATCH] slab: cac...
3952
  			goto next;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3953

ce8eb6c42   Christoph Lameter   slab: Rename list...
3954
  		n->next_reap = jiffies + REAPTIMEOUT_LIST3;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3955

ce8eb6c42   Christoph Lameter   slab: Rename list...
3956
  		drain_array(searchp, n, n->shared, 0, node);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3957

ce8eb6c42   Christoph Lameter   slab: Rename list...
3958
3959
  		if (n->free_touched)
  			n->free_touched = 0;
ed11d9eb2   Christoph Lameter   [PATCH] slab: con...
3960
3961
  		else {
  			int freed;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3962

ce8eb6c42   Christoph Lameter   slab: Rename list...
3963
  			freed = drain_freelist(searchp, n, (n->free_limit +
ed11d9eb2   Christoph Lameter   [PATCH] slab: con...
3964
3965
3966
  				5 * searchp->num - 1) / (5 * searchp->num));
  			STATS_ADD_REAPED(searchp, freed);
  		}
35386e3b0   Christoph Lameter   [PATCH] slab: cac...
3967
  next:
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3968
3969
3970
  		cond_resched();
  	}
  	check_irq_on();
18004c5d4   Christoph Lameter   mm, sl[aou]b: Use...
3971
  	mutex_unlock(&slab_mutex);
8fce4d8e3   Christoph Lameter   [PATCH] slab: Nod...
3972
  	next_reap_node();
7c5cae368   Christoph Lameter   [PATCH] slab: use...
3973
  out:
a737b3e2f   Andrew Morton   [PATCH] slab cleanup
3974
  	/* Set up the next iteration */
7c5cae368   Christoph Lameter   [PATCH] slab: use...
3975
  	schedule_delayed_work(work, round_jiffies_relative(REAPTIMEOUT_CPUC));
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3976
  }
158a96242   Linus Torvalds   Unify /proc/slabi...
3977
  #ifdef CONFIG_SLABINFO
0d7561c61   Glauber Costa   sl[au]b: Process ...
3978
  void get_slabinfo(struct kmem_cache *cachep, struct slabinfo *sinfo)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3979
  {
b28a02de8   Pekka Enberg   [PATCH] slab: fix...
3980
3981
3982
3983
3984
  	struct slab *slabp;
  	unsigned long active_objs;
  	unsigned long num_objs;
  	unsigned long active_slabs = 0;
  	unsigned long num_slabs, free_objects = 0, shared_avail = 0;
e498be7da   Christoph Lameter   [PATCH] Numa-awar...
3985
  	const char *name;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3986
  	char *error = NULL;
e498be7da   Christoph Lameter   [PATCH] Numa-awar...
3987
  	int node;
ce8eb6c42   Christoph Lameter   slab: Rename list...
3988
  	struct kmem_cache_node *n;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3989

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3990
3991
  	active_objs = 0;
  	num_slabs = 0;
e498be7da   Christoph Lameter   [PATCH] Numa-awar...
3992
  	for_each_online_node(node) {
ce8eb6c42   Christoph Lameter   slab: Rename list...
3993
3994
  		n = cachep->node[node];
  		if (!n)
e498be7da   Christoph Lameter   [PATCH] Numa-awar...
3995
  			continue;
ca3b9b917   Ravikiran G Thirumalai   [PATCH] NUMA slab...
3996
  		check_irq_on();
ce8eb6c42   Christoph Lameter   slab: Rename list...
3997
  		spin_lock_irq(&n->list_lock);
e498be7da   Christoph Lameter   [PATCH] Numa-awar...
3998

ce8eb6c42   Christoph Lameter   slab: Rename list...
3999
  		list_for_each_entry(slabp, &n->slabs_full, list) {
e498be7da   Christoph Lameter   [PATCH] Numa-awar...
4000
4001
4002
4003
4004
  			if (slabp->inuse != cachep->num && !error)
  				error = "slabs_full accounting error";
  			active_objs += cachep->num;
  			active_slabs++;
  		}
ce8eb6c42   Christoph Lameter   slab: Rename list...
4005
  		list_for_each_entry(slabp, &n->slabs_partial, list) {
e498be7da   Christoph Lameter   [PATCH] Numa-awar...
4006
4007
4008
4009
4010
4011
4012
  			if (slabp->inuse == cachep->num && !error)
  				error = "slabs_partial inuse accounting error";
  			if (!slabp->inuse && !error)
  				error = "slabs_partial/inuse accounting error";
  			active_objs += slabp->inuse;
  			active_slabs++;
  		}
ce8eb6c42   Christoph Lameter   slab: Rename list...
4013
  		list_for_each_entry(slabp, &n->slabs_free, list) {
e498be7da   Christoph Lameter   [PATCH] Numa-awar...
4014
4015
4016
4017
  			if (slabp->inuse && !error)
  				error = "slabs_free/inuse accounting error";
  			num_slabs++;
  		}
ce8eb6c42   Christoph Lameter   slab: Rename list...
4018
4019
4020
  		free_objects += n->free_objects;
  		if (n->shared)
  			shared_avail += n->shared->avail;
e498be7da   Christoph Lameter   [PATCH] Numa-awar...
4021

ce8eb6c42   Christoph Lameter   slab: Rename list...
4022
  		spin_unlock_irq(&n->list_lock);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4023
  	}
b28a02de8   Pekka Enberg   [PATCH] slab: fix...
4024
4025
  	num_slabs += active_slabs;
  	num_objs = num_slabs * cachep->num;
e498be7da   Christoph Lameter   [PATCH] Numa-awar...
4026
  	if (num_objs - active_objs != free_objects && !error)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4027
  		error = "free_objects accounting error";
b28a02de8   Pekka Enberg   [PATCH] slab: fix...
4028
  	name = cachep->name;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4029
4030
4031
  	if (error)
  		printk(KERN_ERR "slab: cache %s error: %s
  ", name, error);
0d7561c61   Glauber Costa   sl[au]b: Process ...
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
  	sinfo->active_objs = active_objs;
  	sinfo->num_objs = num_objs;
  	sinfo->active_slabs = active_slabs;
  	sinfo->num_slabs = num_slabs;
  	sinfo->shared_avail = shared_avail;
  	sinfo->limit = cachep->limit;
  	sinfo->batchcount = cachep->batchcount;
  	sinfo->shared = cachep->shared;
  	sinfo->objects_per_slab = cachep->num;
  	sinfo->cache_order = cachep->gfporder;
  }
  
  void slabinfo_show_stats(struct seq_file *m, struct kmem_cache *cachep)
  {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4046
  #if STATS
ce8eb6c42   Christoph Lameter   slab: Rename list...
4047
  	{			/* node stats */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4048
4049
4050
4051
4052
4053
  		unsigned long high = cachep->high_mark;
  		unsigned long allocs = cachep->num_allocations;
  		unsigned long grown = cachep->grown;
  		unsigned long reaped = cachep->reaped;
  		unsigned long errors = cachep->errors;
  		unsigned long max_freeable = cachep->max_freeable;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4054
  		unsigned long node_allocs = cachep->node_allocs;
e498be7da   Christoph Lameter   [PATCH] Numa-awar...
4055
  		unsigned long node_frees = cachep->node_frees;
fb7faf331   Ravikiran G Thirumalai   [PATCH] slab: add...
4056
  		unsigned long overflows = cachep->node_overflow;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4057

e92dd4fd1   Joe Perches   slab: Fix continu...
4058
4059
4060
4061
4062
  		seq_printf(m, " : globalstat %7lu %6lu %5lu %4lu "
  			   "%4lu %4lu %4lu %4lu %4lu",
  			   allocs, high, grown,
  			   reaped, errors, max_freeable, node_allocs,
  			   node_frees, overflows);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4063
4064
4065
4066
4067
4068
4069
4070
4071
  	}
  	/* cpu stats */
  	{
  		unsigned long allochit = atomic_read(&cachep->allochit);
  		unsigned long allocmiss = atomic_read(&cachep->allocmiss);
  		unsigned long freehit = atomic_read(&cachep->freehit);
  		unsigned long freemiss = atomic_read(&cachep->freemiss);
  
  		seq_printf(m, " : cpustat %6lu %6lu %6lu %6lu",
b28a02de8   Pekka Enberg   [PATCH] slab: fix...
4072
  			   allochit, allocmiss, freehit, freemiss);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4073
4074
  	}
  #endif
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4075
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4076
4077
4078
4079
4080
4081
4082
4083
  #define MAX_SLABINFO_WRITE 128
  /**
   * slabinfo_write - Tuning for the slab allocator
   * @file: unused
   * @buffer: user buffer
   * @count: data length
   * @ppos: unused
   */
b7454ad3c   Glauber Costa   mm/sl[au]b: Move ...
4084
  ssize_t slabinfo_write(struct file *file, const char __user *buffer,
b28a02de8   Pekka Enberg   [PATCH] slab: fix...
4085
  		       size_t count, loff_t *ppos)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4086
  {
b28a02de8   Pekka Enberg   [PATCH] slab: fix...
4087
  	char kbuf[MAX_SLABINFO_WRITE + 1], *tmp;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4088
  	int limit, batchcount, shared, res;
7a7c381d2   Christoph Hellwig   [PATCH] slab: sto...
4089
  	struct kmem_cache *cachep;
b28a02de8   Pekka Enberg   [PATCH] slab: fix...
4090

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4091
4092
4093
4094
  	if (count > MAX_SLABINFO_WRITE)
  		return -EINVAL;
  	if (copy_from_user(&kbuf, buffer, count))
  		return -EFAULT;
b28a02de8   Pekka Enberg   [PATCH] slab: fix...
4095
  	kbuf[MAX_SLABINFO_WRITE] = '\0';
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
  
  	tmp = strchr(kbuf, ' ');
  	if (!tmp)
  		return -EINVAL;
  	*tmp = '\0';
  	tmp++;
  	if (sscanf(tmp, " %d %d %d", &limit, &batchcount, &shared) != 3)
  		return -EINVAL;
  
  	/* Find the cache in the chain of caches. */
18004c5d4   Christoph Lameter   mm, sl[aou]b: Use...
4106
  	mutex_lock(&slab_mutex);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4107
  	res = -EINVAL;
18004c5d4   Christoph Lameter   mm, sl[aou]b: Use...
4108
  	list_for_each_entry(cachep, &slab_caches, list) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4109
  		if (!strcmp(cachep->name, kbuf)) {
a737b3e2f   Andrew Morton   [PATCH] slab cleanup
4110
4111
  			if (limit < 1 || batchcount < 1 ||
  					batchcount > limit || shared < 0) {
e498be7da   Christoph Lameter   [PATCH] Numa-awar...
4112
  				res = 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4113
  			} else {
e498be7da   Christoph Lameter   [PATCH] Numa-awar...
4114
  				res = do_tune_cpucache(cachep, limit,
83b519e8b   Pekka Enberg   slab: setup alloc...
4115
4116
  						       batchcount, shared,
  						       GFP_KERNEL);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4117
4118
4119
4120
  			}
  			break;
  		}
  	}
18004c5d4   Christoph Lameter   mm, sl[aou]b: Use...
4121
  	mutex_unlock(&slab_mutex);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4122
4123
4124
4125
  	if (res >= 0)
  		res = count;
  	return res;
  }
871751e25   Al Viro   [PATCH] slab: imp...
4126
4127
4128
4129
4130
  
  #ifdef CONFIG_DEBUG_SLAB_LEAK
  
  static void *leaks_start(struct seq_file *m, loff_t *pos)
  {
18004c5d4   Christoph Lameter   mm, sl[aou]b: Use...
4131
4132
  	mutex_lock(&slab_mutex);
  	return seq_list_start(&slab_caches, *pos);
871751e25   Al Viro   [PATCH] slab: imp...
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
  }
  
  static inline int add_caller(unsigned long *n, unsigned long v)
  {
  	unsigned long *p;
  	int l;
  	if (!v)
  		return 1;
  	l = n[1];
  	p = n + 2;
  	while (l) {
  		int i = l/2;
  		unsigned long *q = p + 2 * i;
  		if (*q == v) {
  			q[1]++;
  			return 1;
  		}
  		if (*q > v) {
  			l = i;
  		} else {
  			p = q + 2;
  			l -= i + 1;
  		}
  	}
  	if (++n[1] == n[0])
  		return 0;
  	memmove(p + 2, p, n[1] * 2 * sizeof(unsigned long) - ((void *)p - (void *)n));
  	p[0] = v;
  	p[1] = 1;
  	return 1;
  }
  
  static void handle_slab(unsigned long *n, struct kmem_cache *c, struct slab *s)
  {
  	void *p;
  	int i;
  	if (n[0] == n[1])
  		return;
3b0efdfa1   Christoph Lameter   mm, sl[aou]b: Ext...
4171
  	for (i = 0, p = s->s_mem; i < c->num; i++, p += c->size) {
871751e25   Al Viro   [PATCH] slab: imp...
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
  		if (slab_bufctl(s)[i] != BUFCTL_ACTIVE)
  			continue;
  		if (!add_caller(n, (unsigned long)*dbg_userword(c, p)))
  			return;
  	}
  }
  
  static void show_symbol(struct seq_file *m, unsigned long address)
  {
  #ifdef CONFIG_KALLSYMS
871751e25   Al Viro   [PATCH] slab: imp...
4182
  	unsigned long offset, size;
9281acea6   Tejun Heo   kallsyms: make KS...
4183
  	char modname[MODULE_NAME_LEN], name[KSYM_NAME_LEN];
871751e25   Al Viro   [PATCH] slab: imp...
4184

a5c43dae7   Alexey Dobriyan   Fix race between ...
4185
  	if (lookup_symbol_attrs(address, &size, &offset, modname, name) == 0) {
871751e25   Al Viro   [PATCH] slab: imp...
4186
  		seq_printf(m, "%s+%#lx/%#lx", name, offset, size);
a5c43dae7   Alexey Dobriyan   Fix race between ...
4187
  		if (modname[0])
871751e25   Al Viro   [PATCH] slab: imp...
4188
4189
4190
4191
4192
4193
4194
4195
4196
  			seq_printf(m, " [%s]", modname);
  		return;
  	}
  #endif
  	seq_printf(m, "%p", (void *)address);
  }
  
  static int leaks_show(struct seq_file *m, void *p)
  {
0672aa7c2   Thierry Reding   mm, slab: Build f...
4197
  	struct kmem_cache *cachep = list_entry(p, struct kmem_cache, list);
871751e25   Al Viro   [PATCH] slab: imp...
4198
  	struct slab *slabp;
ce8eb6c42   Christoph Lameter   slab: Rename list...
4199
  	struct kmem_cache_node *n;
871751e25   Al Viro   [PATCH] slab: imp...
4200
  	const char *name;
db8450673   Christoph Lameter   slab: Fixup CONFI...
4201
  	unsigned long *x = m->private;
871751e25   Al Viro   [PATCH] slab: imp...
4202
4203
4204
4205
4206
4207
4208
4209
4210
  	int node;
  	int i;
  
  	if (!(cachep->flags & SLAB_STORE_USER))
  		return 0;
  	if (!(cachep->flags & SLAB_RED_ZONE))
  		return 0;
  
  	/* OK, we can do it */
db8450673   Christoph Lameter   slab: Fixup CONFI...
4211
  	x[1] = 0;
871751e25   Al Viro   [PATCH] slab: imp...
4212
4213
  
  	for_each_online_node(node) {
ce8eb6c42   Christoph Lameter   slab: Rename list...
4214
4215
  		n = cachep->node[node];
  		if (!n)
871751e25   Al Viro   [PATCH] slab: imp...
4216
4217
4218
  			continue;
  
  		check_irq_on();
ce8eb6c42   Christoph Lameter   slab: Rename list...
4219
  		spin_lock_irq(&n->list_lock);
871751e25   Al Viro   [PATCH] slab: imp...
4220

ce8eb6c42   Christoph Lameter   slab: Rename list...
4221
  		list_for_each_entry(slabp, &n->slabs_full, list)
db8450673   Christoph Lameter   slab: Fixup CONFI...
4222
  			handle_slab(x, cachep, slabp);
ce8eb6c42   Christoph Lameter   slab: Rename list...
4223
  		list_for_each_entry(slabp, &n->slabs_partial, list)
db8450673   Christoph Lameter   slab: Fixup CONFI...
4224
  			handle_slab(x, cachep, slabp);
ce8eb6c42   Christoph Lameter   slab: Rename list...
4225
  		spin_unlock_irq(&n->list_lock);
871751e25   Al Viro   [PATCH] slab: imp...
4226
4227
  	}
  	name = cachep->name;
db8450673   Christoph Lameter   slab: Fixup CONFI...
4228
  	if (x[0] == x[1]) {
871751e25   Al Viro   [PATCH] slab: imp...
4229
  		/* Increase the buffer size */
18004c5d4   Christoph Lameter   mm, sl[aou]b: Use...
4230
  		mutex_unlock(&slab_mutex);
db8450673   Christoph Lameter   slab: Fixup CONFI...
4231
  		m->private = kzalloc(x[0] * 4 * sizeof(unsigned long), GFP_KERNEL);
871751e25   Al Viro   [PATCH] slab: imp...
4232
4233
  		if (!m->private) {
  			/* Too bad, we are really out */
db8450673   Christoph Lameter   slab: Fixup CONFI...
4234
  			m->private = x;
18004c5d4   Christoph Lameter   mm, sl[aou]b: Use...
4235
  			mutex_lock(&slab_mutex);
871751e25   Al Viro   [PATCH] slab: imp...
4236
4237
  			return -ENOMEM;
  		}
db8450673   Christoph Lameter   slab: Fixup CONFI...
4238
4239
  		*(unsigned long *)m->private = x[0] * 2;
  		kfree(x);
18004c5d4   Christoph Lameter   mm, sl[aou]b: Use...
4240
  		mutex_lock(&slab_mutex);
871751e25   Al Viro   [PATCH] slab: imp...
4241
4242
4243
4244
  		/* Now make sure this entry will be retried */
  		m->count = m->size;
  		return 0;
  	}
db8450673   Christoph Lameter   slab: Fixup CONFI...
4245
4246
4247
  	for (i = 0; i < x[1]; i++) {
  		seq_printf(m, "%s: %lu ", name, x[2*i+3]);
  		show_symbol(m, x[2*i+2]);
871751e25   Al Viro   [PATCH] slab: imp...
4248
4249
4250
  		seq_putc(m, '
  ');
  	}
d2e7b7d0a   Siddha, Suresh B   [PATCH] fix poten...
4251

871751e25   Al Viro   [PATCH] slab: imp...
4252
4253
  	return 0;
  }
b7454ad3c   Glauber Costa   mm/sl[au]b: Move ...
4254
4255
4256
4257
4258
4259
4260
4261
4262
  static void *s_next(struct seq_file *m, void *p, loff_t *pos)
  {
  	return seq_list_next(p, &slab_caches, pos);
  }
  
  static void s_stop(struct seq_file *m, void *p)
  {
  	mutex_unlock(&slab_mutex);
  }
a0ec95a8e   Alexey Dobriyan   proc: move /proc/...
4263
  static const struct seq_operations slabstats_op = {
871751e25   Al Viro   [PATCH] slab: imp...
4264
4265
4266
4267
4268
  	.start = leaks_start,
  	.next = s_next,
  	.stop = s_stop,
  	.show = leaks_show,
  };
a0ec95a8e   Alexey Dobriyan   proc: move /proc/...
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
  
  static int slabstats_open(struct inode *inode, struct file *file)
  {
  	unsigned long *n = kzalloc(PAGE_SIZE, GFP_KERNEL);
  	int ret = -ENOMEM;
  	if (n) {
  		ret = seq_open(file, &slabstats_op);
  		if (!ret) {
  			struct seq_file *m = file->private_data;
  			*n = PAGE_SIZE / (2 * sizeof(unsigned long));
  			m->private = n;
  			n = NULL;
  		}
  		kfree(n);
  	}
  	return ret;
  }
  
  static const struct file_operations proc_slabstats_operations = {
  	.open		= slabstats_open,
  	.read		= seq_read,
  	.llseek		= seq_lseek,
  	.release	= seq_release_private,
  };
  #endif
  
  static int __init slab_proc_init(void)
  {
  #ifdef CONFIG_DEBUG_SLAB_LEAK
  	proc_create("slab_allocators", 0, NULL, &proc_slabstats_operations);
871751e25   Al Viro   [PATCH] slab: imp...
4299
  #endif
a0ec95a8e   Alexey Dobriyan   proc: move /proc/...
4300
4301
4302
  	return 0;
  }
  module_init(slab_proc_init);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4303
  #endif
00e145b6d   Manfred Spraul   [PATCH] slab: rem...
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
  /**
   * ksize - get the actual amount of memory allocated for a given object
   * @objp: Pointer to the object
   *
   * kmalloc may internally round up allocations and return more memory
   * than requested. ksize() can be used to determine the actual amount of
   * memory allocated. The caller may use this additional memory, even though
   * a smaller amount of memory was initially specified with the kmalloc call.
   * The caller must guarantee that objp points to a valid object previously
   * allocated with either kmalloc() or kmem_cache_alloc(). The object
   * must not be freed during the duration of the call.
   */
fd76bab2f   Pekka Enberg   slab: introduce k...
4316
  size_t ksize(const void *objp)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4317
  {
ef8b4520b   Christoph Lameter   Slab allocators: ...
4318
4319
  	BUG_ON(!objp);
  	if (unlikely(objp == ZERO_SIZE_PTR))
00e145b6d   Manfred Spraul   [PATCH] slab: rem...
4320
  		return 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4321

8c138bc00   Christoph Lameter   slab: Get rid of ...
4322
  	return virt_to_cache(objp)->object_size;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4323
  }
b1aabecd5   Kirill A. Shutemov   mm: Export symbol...
4324
  EXPORT_SYMBOL(ksize);