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