89 #include <linux/slab.h>
92 #include <linux/poison.h>
97 #include <linux/compiler.h>
104 #include <linux/sysctl.h>
105 #include <linux/module.h>
107 #include <linux/string.h>
111 #include <linux/mempolicy.h>
119 #include <linux/prefetch.h>
123 #include <asm/cacheflush.h>
124 #include <asm/tlbflush.h>
125 #include <asm/page.h>
141 #ifdef CONFIG_DEBUG_SLAB
144 #define FORCED_DEBUG 1
148 #define FORCED_DEBUG 0
152 #define BYTES_PER_WORD sizeof(void *)
153 #define REDZONE_ALIGN max(BYTES_PER_WORD, __alignof__(unsigned long long))
155 #ifndef ARCH_KMALLOC_FLAGS
156 #define ARCH_KMALLOC_FLAGS SLAB_HWCACHE_ALIGN
167 # define CREATE_MASK (SLAB_RED_ZONE | \
168 SLAB_POISON | SLAB_HWCACHE_ALIGN | \
171 SLAB_RECLAIM_ACCOUNT | SLAB_PANIC | \
172 SLAB_DESTROY_BY_RCU | SLAB_MEM_SPREAD | \
173 SLAB_DEBUG_OBJECTS | SLAB_NOLEAKTRACE | SLAB_NOTRACK)
175 # define CREATE_MASK (SLAB_HWCACHE_ALIGN | \
177 SLAB_RECLAIM_ACCOUNT | SLAB_PANIC | \
178 SLAB_DESTROY_BY_RCU | SLAB_MEM_SPREAD | \
179 SLAB_DEBUG_OBJECTS | SLAB_NOLEAKTRACE | SLAB_NOTRACK)
202 #define BUFCTL_END (((kmem_bufctl_t)(~0U))-0)
203 #define BUFCTL_FREE (((kmem_bufctl_t)(~0U))-1)
204 #define BUFCTL_ACTIVE (((kmem_bufctl_t)(~0U))-2)
205 #define SLAB_LIMIT (((kmem_bufctl_t)(~0U))-3)
277 #define SLAB_OBJ_PFMEMALLOC 1
278 static inline bool is_obj_pfmemalloc(
void *objp)
283 static inline void set_obj_pfmemalloc(
void **objp)
289 static inline void clear_obj_pfmemalloc(
void **objp)
298 #define BOOT_CPUCACHE_ENTRIES 1
324 #define NUM_INIT_LISTS (3 * MAX_NUMNODES)
326 #define CACHE_CACHE 0
327 #define SIZE_AC MAX_NUMNODES
328 #define SIZE_L3 (2 * MAX_NUMNODES)
332 static void free_block(
struct kmem_cache *cachep,
void **objpp,
int len,
343 extern void __bad_size(
void);
345 if (__builtin_constant_p(size)) {
361 static int slab_early_init = 1;
363 #define INDEX_AC index_of(sizeof(struct arraycache_init))
364 #define INDEX_L3 index_of(sizeof(struct kmem_list3))
366 static void kmem_list3_init(
struct kmem_list3 *parent)
379 #define MAKE_LIST(cachep, listp, slab, nodeid) \
381 INIT_LIST_HEAD(listp); \
382 list_splice(&(cachep->nodelists[nodeid]->slab), listp); \
385 #define MAKE_ALL_LISTS(cachep, ptr, nodeid) \
387 MAKE_LIST((cachep), (&(ptr)->slabs_full), slabs_full, nodeid); \
388 MAKE_LIST((cachep), (&(ptr)->slabs_partial), slabs_partial, nodeid); \
389 MAKE_LIST((cachep), (&(ptr)->slabs_free), slabs_free, nodeid); \
392 #define CFLGS_OFF_SLAB (0x80000000UL)
393 #define OFF_SLAB(x) ((x)->flags & CFLGS_OFF_SLAB)
395 #define BATCHREFILL_LIMIT 16
403 #define REAPTIMEOUT_CPUC (2*HZ)
404 #define REAPTIMEOUT_LIST3 (4*HZ)
407 #define STATS_INC_ACTIVE(x) ((x)->num_active++)
408 #define STATS_DEC_ACTIVE(x) ((x)->num_active--)
409 #define STATS_INC_ALLOCED(x) ((x)->num_allocations++)
410 #define STATS_INC_GROWN(x) ((x)->grown++)
411 #define STATS_ADD_REAPED(x,y) ((x)->reaped += (y))
412 #define STATS_SET_HIGH(x) \
414 if ((x)->num_active > (x)->high_mark) \
415 (x)->high_mark = (x)->num_active; \
417 #define STATS_INC_ERR(x) ((x)->errors++)
418 #define STATS_INC_NODEALLOCS(x) ((x)->node_allocs++)
419 #define STATS_INC_NODEFREES(x) ((x)->node_frees++)
420 #define STATS_INC_ACOVERFLOW(x) ((x)->node_overflow++)
421 #define STATS_SET_FREEABLE(x, i) \
423 if ((x)->max_freeable < i) \
424 (x)->max_freeable = i; \
426 #define STATS_INC_ALLOCHIT(x) atomic_inc(&(x)->allochit)
427 #define STATS_INC_ALLOCMISS(x) atomic_inc(&(x)->allocmiss)
428 #define STATS_INC_FREEHIT(x) atomic_inc(&(x)->freehit)
429 #define STATS_INC_FREEMISS(x) atomic_inc(&(x)->freemiss)
431 #define STATS_INC_ACTIVE(x) do { } while (0)
432 #define STATS_DEC_ACTIVE(x) do { } while (0)
433 #define STATS_INC_ALLOCED(x) do { } while (0)
434 #define STATS_INC_GROWN(x) do { } while (0)
435 #define STATS_ADD_REAPED(x,y) do { (void)(y); } while (0)
436 #define STATS_SET_HIGH(x) do { } while (0)
437 #define STATS_INC_ERR(x) do { } while (0)
438 #define STATS_INC_NODEALLOCS(x) do { } while (0)
439 #define STATS_INC_NODEFREES(x) do { } while (0)
440 #define STATS_INC_ACOVERFLOW(x) do { } while (0)
441 #define STATS_SET_FREEABLE(x, i) do { } while (0)
442 #define STATS_INC_ALLOCHIT(x) do { } while (0)
443 #define STATS_INC_ALLOCMISS(x) do { } while (0)
444 #define STATS_INC_FREEHIT(x) do { } while (0)
445 #define STATS_INC_FREEMISS(x) do { } while (0)
465 return cachep->obj_offset;
471 return (
unsigned long long*) (objp +
obj_offset(cachep) -
472 sizeof(
unsigned long long));
479 return (
unsigned long long *)(objp + cachep->
size -
480 sizeof(
unsigned long long) -
482 return (
unsigned long long *) (objp + cachep->
size -
483 sizeof(
unsigned long long));
494 #define obj_offset(x) 0
495 #define dbg_redzone1(cachep, objp) ({BUG(); (unsigned long long *)NULL;})
496 #define dbg_redzone2(cachep, objp) ({BUG(); (unsigned long long *)NULL;})
497 #define dbg_userword(cachep, objp) ({BUG(); (void **)NULL;})
505 #define SLAB_MAX_ORDER_HI 1
506 #define SLAB_MAX_ORDER_LO 0
510 static inline struct kmem_cache *virt_to_cache(
const void *obj)
512 struct page *
page = virt_to_head_page(obj);
516 static inline struct slab *virt_to_slab(
const void *obj)
518 struct page *
page = virt_to_head_page(obj);
536 static inline unsigned int obj_to_index(
const struct kmem_cache *
cache,
547 #define CACHE(x) { .cs_size = (x) },
561 #define CACHE(x) { .name = "size-" #x, .name_dma = "size-" #x "(DMA)" },
575 .nodelists = kmem_cache_nodelists,
580 .
name =
"kmem_cache",
583 #define BAD_ALIEN_MAGIC 0x01020304ul
585 #ifdef CONFIG_LOCKDEP
604 static void slab_set_lock_classes(
struct kmem_cache *cachep,
633 static void slab_set_debugobj_lock_classes_node(
struct kmem_cache *cachep,
int node)
635 slab_set_lock_classes(cachep, &debugobj_l3_key, &debugobj_alc_key, node);
638 static void slab_set_debugobj_lock_classes(
struct kmem_cache *cachep)
643 slab_set_debugobj_lock_classes_node(cachep, node);
646 static
void init_node_lock_keys(
int q)
660 slab_set_lock_classes(s->
cs_cachep, &on_slab_l3_key,
661 &on_slab_alc_key, q);
665 static inline void init_lock_keys(
void)
670 init_node_lock_keys(node);
673 static void init_node_lock_keys(
int q)
677 static inline void init_lock_keys(
void)
681 static void slab_set_debugobj_lock_classes_node(
struct kmem_cache *cachep,
int node)
685 static void slab_set_debugobj_lock_classes(
struct kmem_cache *cachep)
697 static inline struct kmem_cache *__find_general_cachep(
size_t size,
720 #ifdef CONFIG_ZONE_DMA
722 return csizep->cs_dmacachep;
727 static struct kmem_cache *kmem_find_general_cachep(
size_t size,
gfp_t gfpflags)
729 return __find_general_cachep(size, gfpflags);
732 static size_t slab_mgmt_size(
size_t nr_objs,
size_t align)
740 static void cache_estimate(
unsigned long gfporder,
size_t buffer_size,
741 size_t align,
int flags,
size_t *left_over,
765 nr_objs = slab_size / buffer_size;
778 nr_objs = (slab_size -
sizeof(
struct slab)) /
785 if (slab_mgmt_size(nr_objs, align) + nr_objs*buffer_size
792 mgmt_size = slab_mgmt_size(nr_objs, align);
795 *left_over = slab_size - nr_objs*buffer_size - mgmt_size;
799 #define slab_error(cachep, msg) __slab_error(__func__, cachep, msg)
801 static void __slab_error(
const char *
function,
struct kmem_cache *cachep,
805 function, cachep->
name, msg);
820 static int __init noaliencache_setup(
char *s)
822 use_alien_caches = 0;
825 __setup(
"noaliencache", noaliencache_setup);
827 static int __init slab_max_order_setup(
char *
str)
830 slab_max_order = slab_max_order < 0 ? 0 :
832 slab_max_order_set =
true;
836 __setup(
"slab_max_order=", slab_max_order_setup);
869 #define init_reap_node(cpu) do { } while (0)
870 #define next_reap_node(void) do { } while (0)
880 static void __cpuinit start_cpu_timer(
int cpu)
903 nc = kmalloc_node(memsize, gfp, node);
922 static inline bool is_slab_pfmemalloc(
struct slab *slabp)
926 return PageSlabPfmemalloc(page);
930 static void recheck_pfmemalloc_active(
struct kmem_cache *cachep,
937 if (!pfmemalloc_active)
942 if (is_slab_pfmemalloc(slabp))
946 if (is_slab_pfmemalloc(slabp))
950 if (is_slab_pfmemalloc(slabp))
953 pfmemalloc_active =
false;
955 spin_unlock_irqrestore(&l3->
list_lock, flags);
959 gfp_t flags,
bool force_refill)
962 void *objp = ac->entry[--ac->avail];
965 if (
unlikely(is_obj_pfmemalloc(objp))) {
969 clear_obj_pfmemalloc(&objp);
974 for (i = 0; i < ac->avail; i++) {
976 if (!is_obj_pfmemalloc(ac->entry[i])) {
978 ac->entry[
i] = ac->entry[ac->avail];
979 ac->entry[ac->avail] = objp;
988 l3 = cachep->nodelists[numa_mem_id()];
989 if (!list_empty(&l3->
slabs_free) && force_refill) {
990 struct slab *slabp = virt_to_slab(objp);
991 ClearPageSlabPfmemalloc(virt_to_head_page(slabp->
s_mem));
992 clear_obj_pfmemalloc(&objp);
993 recheck_pfmemalloc_active(cachep, ac);
1005 static inline void *ac_get_obj(
struct kmem_cache *cachep,
1011 objp = __ac_get_obj(cachep, ac, flags, force_refill);
1023 struct page *page = virt_to_head_page(objp);
1024 if (PageSlabPfmemalloc(page))
1025 set_obj_pfmemalloc(&objp);
1035 objp = __ac_put_obj(cachep, ac, objp);
1046 static int transfer_objects(
struct array_cache *to,
1056 sizeof(
void *) *nr);
1065 #define drain_alien_cache(cachep, alien) do { } while (0)
1066 #define reap_alien(cachep, l3) do { } while (0)
1073 static inline void free_alien_cache(
struct array_cache **ac_ptr)
1077 static inline int cache_free_alien(
struct kmem_cache *cachep,
void *objp)
1082 static inline void *alternate_node_alloc(
struct kmem_cache *cachep,
1088 static inline void *____cache_alloc_node(
struct kmem_cache *cachep,
1089 gfp_t flags,
int nodeid)
1107 ac_ptr = kzalloc_node(memsize, gfp, node);
1112 ac_ptr[
i] = alloc_arraycache(node, limit, 0xbaadf00d, gfp);
1114 for (i--; i >= 0; i--)
1124 static void free_alien_cache(
struct array_cache **ac_ptr)
1148 transfer_objects(rl3->
shared, ac, ac->limit);
1150 free_block(cachep, ac->entry, ac->avail, node);
1166 if (ac && ac->
avail && spin_trylock_irq(&ac->
lock)) {
1167 __drain_alien_cache(cachep, ac, node);
1168 spin_unlock_irq(&ac->
lock);
1178 unsigned long flags;
1184 __drain_alien_cache(cachep, ac, i);
1185 spin_unlock_irqrestore(&ac->
lock, flags);
1190 static inline int cache_free_alien(
struct kmem_cache *cachep,
void *objp)
1192 struct slab *slabp = virt_to_slab(objp);
1198 node = numa_mem_id();
1210 alien = l3->
alien[nodeid];
1211 spin_lock(&alien->
lock);
1214 __drain_alien_cache(cachep, alien, nodeid);
1216 ac_put_obj(cachep, alien, objp);
1217 spin_unlock(&alien->
lock);
1219 spin_lock(&(cachep->
nodelists[nodeid])->list_lock);
1220 free_block(cachep, &objp, 1, nodeid);
1221 spin_unlock(&(cachep->
nodelists[nodeid])->list_lock);
1236 static int init_cache_nodelists_node(
int node)
1240 const int memsize =
sizeof(
struct kmem_list3);
1249 l3 = kmalloc_node(memsize,
GFP_KERNEL, node);
1252 kmem_list3_init(l3);
1264 spin_lock_irq(&cachep->
nodelists[node]->list_lock);
1268 spin_unlock_irq(&cachep->
nodelists[node]->list_lock);
1273 static void __cpuinit cpuup_canceled(
long cpu)
1291 goto free_array_cache;
1298 free_block(cachep, nc->
entry, nc->
avail, node);
1300 if (!cpumask_empty(mask)) {
1302 goto free_array_cache;
1307 free_block(cachep, shared->
entry,
1308 shared->
avail, node);
1320 free_alien_cache(alien);
1338 static int __cpuinit cpuup_prepare(
long cpu)
1351 err = init_cache_nodelists_node(node);
1364 nc = alloc_arraycache(node, cachep->
limit,
1369 shared = alloc_arraycache(node,
1377 if (use_alien_caches) {
1406 free_alien_cache(alien);
1408 slab_set_debugobj_lock_classes_node(cachep, node);
1410 init_node_lock_keys(node);
1414 cpuup_canceled(cpu);
1419 unsigned long action,
void *hcpu)
1421 long cpu = (
long)hcpu;
1428 err = cpuup_prepare(cpu);
1433 start_cpu_timer(cpu);
1435 #ifdef CONFIG_HOTPLUG_CPU
1450 start_cpu_timer(cpu);
1467 cpuup_canceled(cpu);
1471 return notifier_from_errno(err);
1475 &cpuup_callback,
NULL, 0
1478 #if defined(CONFIG_NUMA) && defined(CONFIG_MEMORY_HOTPLUG)
1486 static int __meminit drain_cache_nodelists_node(
int node)
1510 unsigned long action,
void *
arg)
1523 ret = init_cache_nodelists_node(nid);
1528 ret = drain_cache_nodelists_node(nid);
1538 return notifier_from_errno(ret);
1587 struct cache_names *names;
1595 use_alien_caches = 0;
1598 kmem_list3_init(&initkmem_list3[i]);
1609 if (!slab_max_order_set && totalram_pages > (32 << 20) >>
PAGE_SHIFT)
1632 node = numa_mem_id();
1652 for (order = 0; order <
MAX_ORDER; order++) {
1666 names = cache_names;
1692 slab_early_init = 0;
1711 #ifdef CONFIG_ZONE_DMA
1713 sizes->cs_dmacachep->name = names->
name_dma;
1714 sizes->cs_dmacachep->size = sizes->
cs_size;
1715 sizes->cs_dmacachep->object_size = sizes->
cs_size;
1719 list_add(&sizes->cs_dmacachep->list, &
slab_caches);
1742 BUG_ON(cpu_cache_get(malloc_sizes[INDEX_AC].cs_cachep)
1743 != &initarray_generic.
cache);
1744 memcpy(ptr, cpu_cache_get(malloc_sizes[INDEX_AC].cs_cachep),
1761 init_list(malloc_sizes[INDEX_AC].cs_cachep,
1762 &initkmem_list3[
SIZE_AC + nid], nid);
1766 &initkmem_list3[
SIZE_L3 + nid], nid);
1797 register_cpu_notifier(&cpucache_notifier);
1813 static int __init cpucache_init(
void)
1821 start_cpu_timer(cpu);
1834 unsigned long flags;
1838 "SLAB: Unable to allocate memory on node %d (gfp=0x%x)\n",
1841 cachep->name, cachep->size, cachep->gfporder);
1844 unsigned long active_objs = 0, num_objs = 0, free_objects = 0;
1845 unsigned long active_slabs = 0, num_slabs = 0;
1847 l3 = cachep->nodelists[
node];
1853 active_objs += cachep->num;
1857 active_objs += slabp->
inuse;
1863 free_objects += l3->free_objects;
1864 spin_unlock_irqrestore(&l3->list_lock, flags);
1866 num_slabs += active_slabs;
1867 num_objs = num_slabs * cachep->num;
1870 node, active_slabs, num_slabs, active_objs, num_objs,
1896 flags |= cachep->allocflags;
1900 page = alloc_pages_exact_node(nodeid, flags |
__GFP_NOTRACK, cachep->gfporder);
1903 slab_out_of_memory(cachep, flags, nodeid);
1909 pfmemalloc_active =
true;
1911 nr_pages = (1 << cachep->gfporder);
1918 for (i = 0; i < nr_pages; i++) {
1919 __SetPageSlab(page + i);
1922 SetPageSlabPfmemalloc(page + i);
1940 static void kmem_freepages(
struct kmem_cache *cachep,
void *
addr)
1942 unsigned long i = (1 << cachep->
gfporder);
1944 const unsigned long nr_freed =
i;
1956 __ClearPageSlabPfmemalloc(page);
1957 __ClearPageSlab(page);
1961 current->reclaim_state->reclaimed_slab += nr_freed;
1970 kmem_freepages(cachep, slab_rcu->
addr);
1977 #ifdef CONFIG_DEBUG_PAGEALLOC
1978 static void store_stackinfo(
struct kmem_cache *cachep,
unsigned long *addr,
1985 if (size < 5 *
sizeof(
unsigned long))
1988 *addr++ = 0x12345678;
1991 size -= 3 *
sizeof(
unsigned long);
1993 unsigned long *sptr = &
caller;
1994 unsigned long svalue;
1996 while (!kstack_end(sptr)) {
2000 size -=
sizeof(
unsigned long);
2001 if (size <=
sizeof(
unsigned long))
2007 *addr++ = 0x87654321;
2011 static void poison_obj(
struct kmem_cache *cachep,
void *addr,
unsigned char val)
2017 *(
unsigned char *)(addr + size - 1) =
POISON_END;
2020 static void dump_line(
char *
data,
int offset,
int limit)
2023 unsigned char error = 0;
2027 for (i = 0; i <
limit; i++) {
2029 error = data[offset +
i];
2034 &data[offset], limit, 1);
2036 if (bad_count == 1) {
2038 if (!(error & (error - 1))) {
2054 static void print_objinfo(
struct kmem_cache *cachep,
void *objp,
int lines)
2068 print_symbol(
"(%s)",
2074 for (i = 0; i < size && lines; i += 16, lines--) {
2077 if (i + limit > size)
2079 dump_line(realobj, i, limit);
2083 static void check_poison_obj(
struct kmem_cache *cachep,
void *objp)
2092 for (i = 0; i <
size; i++) {
2096 if (realobj[i] != exp) {
2102 "Slab corruption (%s): %s start=%p, len=%d\n",
2104 print_objinfo(cachep, objp, 0);
2109 if (i + limit > size)
2111 dump_line(realobj, i, limit);
2123 struct slab *slabp = virt_to_slab(objp);
2126 objnr = obj_to_index(cachep, slabp, objp);
2128 objp = index_to_obj(cachep, slabp, objnr - 1);
2132 print_objinfo(cachep, objp, 2);
2134 if (objnr + 1 < cachep->
num) {
2135 objp = index_to_obj(cachep, slabp, objnr + 1);
2139 print_objinfo(cachep, objp, 2);
2146 static void slab_destroy_debugcheck(
struct kmem_cache *cachep,
struct slab *slabp)
2149 for (i = 0; i < cachep->
num; i++) {
2150 void *objp = index_to_obj(cachep, slabp, i);
2153 #ifdef CONFIG_DEBUG_PAGEALLOC
2159 check_poison_obj(cachep, objp);
2161 check_poison_obj(cachep, objp);
2166 slab_error(cachep,
"start of a freed object "
2169 slab_error(cachep,
"end of a freed object "
2175 static void slab_destroy_debugcheck(
struct kmem_cache *cachep,
struct slab *slabp)
2189 static void slab_destroy(
struct kmem_cache *cachep,
struct slab *slabp)
2193 slab_destroy_debugcheck(cachep, slabp);
2195 struct slab_rcu *slab_rcu;
2197 slab_rcu = (
struct slab_rcu *)slabp;
2202 kmem_freepages(cachep, addr);
2221 static size_t calculate_slab_order(
struct kmem_cache *cachep,
2222 size_t size,
size_t align,
unsigned long flags)
2224 unsigned long offslab_limit;
2225 size_t left_over = 0;
2232 cache_estimate(gfporder, size, align, flags, &remainder, &num);
2236 if (flags & CFLGS_OFF_SLAB) {
2242 offslab_limit = size -
sizeof(
struct slab);
2245 if (num > offslab_limit)
2252 left_over = remainder;
2266 if (gfporder >= slab_max_order)
2272 if (left_over * 8 <= (
PAGE_SIZE << gfporder))
2281 return enable_cpucache(cachep, gfp);
2296 set_up_list3s(cachep,
SIZE_AC);
2306 set_up_list3s(cachep,
SIZE_L3);
2315 kmem_list3_init(cachep->
nodelists[node]);
2319 cachep->
nodelists[numa_mem_id()]->next_reap =
2323 cpu_cache_get(cachep)->avail = 0;
2325 cpu_cache_get(cachep)->batchcount = 1;
2326 cpu_cache_get(cachep)->touched = 0;
2359 size_t left_over, slab_size, ralign;
2362 size_t size = cachep->
size;
2372 if (size < 4096 || fls(size - 1) == fls(size-1 +
REDZONE_ALIGN +
2373 2 *
sizeof(
unsigned long long)))
2378 if (flags & SLAB_DESTROY_BY_RCU)
2407 while (size <= ralign / 2)
2434 if (ralign < cachep->align) {
2435 ralign = cachep->
align;
2438 if (ralign > __alignof__(
unsigned long long))
2439 flags &= ~(SLAB_RED_ZONE | SLAB_STORE_USER);
2443 cachep->
align = ralign;
2457 if (flags & SLAB_RED_ZONE) {
2459 cachep->obj_offset +=
sizeof(
unsigned long long);
2460 size += 2 *
sizeof(
unsigned long long);
2462 if (flags & SLAB_STORE_USER) {
2467 if (flags & SLAB_RED_ZONE)
2472 #if FORCED_DEBUG && defined(CONFIG_DEBUG_PAGEALLOC)
2473 if (size >= malloc_sizes[
INDEX_L3 + 1].cs_size
2488 if ((size >= (
PAGE_SIZE >> 3)) && !slab_early_init &&
2498 left_over = calculate_slab_order(cachep, size, cachep->
align, flags);
2510 if (flags & CFLGS_OFF_SLAB && left_over >= slab_size) {
2511 flags &= ~CFLGS_OFF_SLAB;
2512 left_over -= slab_size;
2515 if (flags & CFLGS_OFF_SLAB) {
2520 #ifdef CONFIG_PAGE_POISONING
2525 if (size %
PAGE_SIZE == 0 && flags & SLAB_POISON)
2543 if (flags & CFLGS_OFF_SLAB) {
2544 cachep->
slabp_cache = kmem_find_general_cachep(slab_size, 0
u);
2555 err = setup_cpu_cache(cachep, gfp);
2568 slab_set_debugobj_lock_classes(cachep);
2602 #define check_irq_off() do { } while(0)
2603 #define check_irq_on() do { } while(0)
2604 #define check_spinlock_acquired(x) do { } while(0)
2605 #define check_spinlock_acquired_node(x, y) do { } while(0)
2610 int force,
int node);
2612 static void do_drain(
void *arg)
2616 int node = numa_mem_id();
2619 ac = cpu_cache_get(cachep);
2620 spin_lock(&cachep->
nodelists[node]->list_lock);
2621 free_block(cachep, ac->
entry, ac->
avail, node);
2622 spin_unlock(&cachep->
nodelists[node]->list_lock);
2626 static void drain_cpu_caches(
struct kmem_cache *cachep)
2635 if (l3 && l3->
alien)
2642 drain_array(cachep, l3, l3->
shared, 1, node);
2660 while (nr_freed < tofree && !list_empty(&l3->
slabs_free)) {
2680 slab_destroy(cache, slabp);
2688 static int __cache_shrink(
struct kmem_cache *cachep)
2693 drain_cpu_caches(cachep);
2706 return (ret ? 1 : 0);
2723 ret = __cache_shrink(cachep);
2734 int rc = __cache_shrink(cachep);
2747 free_alien_cache(l3->
alien);
2765 static struct slab *alloc_slabmgmt(
struct kmem_cache *cachep,
void *objp,
2766 int colour_off,
gfp_t local_flags,
2774 local_flags, nodeid);
2786 slabp = objp + colour_off;
2791 slabp->
s_mem = objp + colour_off;
2802 static void cache_init_objs(
struct kmem_cache *cachep,
2807 for (i = 0; i < cachep->
num; i++) {
2808 void *objp = index_to_obj(cachep, slabp, i);
2830 slab_error(cachep,
"constructor overwrote the"
2831 " end of an object");
2833 slab_error(cachep,
"constructor overwrote the"
2834 " start of an object");
2844 slab_bufctl(slabp)[
i] = i + 1;
2851 if (CONFIG_ZONE_DMA_FLAG) {
2859 static void *slab_get_obj(
struct kmem_cache *cachep,
struct slab *slabp,
2862 void *objp = index_to_obj(cachep, slabp, slabp->
free);
2866 next = slab_bufctl(slabp)[slabp->
free];
2876 static void slab_put_obj(
struct kmem_cache *cachep,
struct slab *slabp,
2877 void *objp,
int nodeid)
2879 unsigned int objnr = obj_to_index(cachep, slabp, objp);
2885 if (slab_bufctl(slabp)[objnr] + 1 <=
SLAB_LIMIT + 1) {
2887 "'%s', objp %p\n", cachep->
name, objp);
2891 slab_bufctl(slabp)[objnr] = slabp->
free;
2892 slabp->
free = objnr;
2910 if (
likely(!PageCompound(page)))
2917 }
while (--nr_pages);
2924 static int cache_grow(
struct kmem_cache *cachep,
2925 gfp_t flags,
int nodeid,
void *objp)
2962 kmem_flagcheck(cachep, flags);
2969 objp = kmem_getpages(cachep, local_flags, nodeid);
2974 slabp = alloc_slabmgmt(cachep, objp, offset,
2979 slab_map_pages(cachep, slabp, objp);
2981 cache_init_objs(cachep, slabp);
2983 if (local_flags & __GFP_WAIT)
2995 kmem_freepages(cachep, objp);
2997 if (local_flags & __GFP_WAIT)
3013 (
unsigned long)objp);
3018 static inline void verify_redzone_free(
struct kmem_cache *cache,
void *obj)
3020 unsigned long long redzone1, redzone2;
3032 slab_error(cache,
"double free detected");
3034 slab_error(cache,
"memory outside object was overwritten");
3037 obj, redzone1, redzone2);
3041 unsigned long caller)
3047 BUG_ON(virt_to_cache(objp) != cachep);
3051 page = virt_to_head_page(objp);
3056 verify_redzone_free(cachep, objp);
3063 objnr = obj_to_index(cachep, slabp, objp);
3066 BUG_ON(objp != index_to_obj(cachep, slabp, objnr));
3068 #ifdef CONFIG_DEBUG_SLAB_LEAK
3072 #ifdef CONFIG_DEBUG_PAGEALLOC
3074 store_stackinfo(cachep, objp, caller);
3093 for (i = slabp->
free; i !=
BUFCTL_END; i = slab_bufctl(slabp)[i]) {
3095 if (entries > cachep->
num || i >= cachep->
num)
3098 if (entries != cachep->
num - slabp->
inuse) {
3101 "cache '%s'(%d), slabp %p(%d). Tainted(%s). Hexdump:\n",
3111 #define kfree_debugcheck(x) do { } while(0)
3112 #define cache_free_debugcheck(x,objp,z) (objp)
3113 #define check_slabp(x,y) do { } while(0)
3116 static void *cache_alloc_refill(
struct kmem_cache *cachep,
gfp_t flags,
3125 node = numa_mem_id();
3129 ac = cpu_cache_get(cachep);
3145 if (l3->
shared && transfer_objects(ac, l3->
shared, batchcount)) {
3150 while (batchcount > 0) {
3162 slabp =
list_entry(entry,
struct slab, list);
3173 while (slabp->
inuse < cachep->
num && batchcount--) {
3178 ac_put_obj(cachep, ac, slab_get_obj(cachep, slabp,
3202 ac = cpu_cache_get(cachep);
3203 node = numa_mem_id();
3206 if (!x && (ac->
avail == 0 || force_refill))
3214 return ac_get_obj(cachep, ac, flags, force_refill);
3217 static inline void cache_alloc_debugcheck_before(
struct kmem_cache *cachep,
3222 kmem_flagcheck(cachep, flags);
3228 gfp_t flags,
void *objp,
unsigned long caller)
3233 #ifdef CONFIG_DEBUG_PAGEALLOC
3238 check_poison_obj(cachep, objp);
3240 check_poison_obj(cachep, objp);
3250 slab_error(cachep,
"double free, or memory outside"
3251 " object was overwritten");
3253 "%p: redzone 1:0x%llx, redzone 2:0x%llx\n",
3260 #ifdef CONFIG_DEBUG_SLAB_LEAK
3265 slabp = virt_to_head_page(objp)->slab_page;
3266 objnr = (unsigned)(objp - slabp->
s_mem) / cachep->
size;
3281 #define cache_alloc_debugcheck_after(a,b,objp,d) (objp)
3284 static bool slab_should_failslab(
struct kmem_cache *cachep,
gfp_t flags)
3292 static inline void *____cache_alloc(
struct kmem_cache *cachep,
gfp_t flags)
3296 bool force_refill =
false;
3300 ac = cpu_cache_get(cachep);
3303 objp = ac_get_obj(cachep, ac, flags,
false);
3313 force_refill =
true;
3317 objp = cache_alloc_refill(cachep, flags, force_refill);
3322 ac = cpu_cache_get(cachep);
3342 static void *alternate_node_alloc(
struct kmem_cache *cachep,
gfp_t flags)
3344 int nid_alloc, nid_here;
3348 nid_alloc = nid_here = numa_mem_id();
3353 if (nid_alloc != nid_here)
3354 return ____cache_alloc_node(cachep, flags, nid_alloc);
3372 enum zone_type high_zoneidx = gfp_zone(flags);
3375 unsigned int cpuset_mems_cookie;
3377 if (flags & __GFP_THISNODE)
3383 cpuset_mems_cookie = get_mems_allowed();
3384 zonelist = node_zonelist(
slab_node(), flags);
3392 nid = zone_to_nid(zone);
3394 if (cpuset_zone_allowed_hardwall(zone, flags) &&
3397 obj = ____cache_alloc_node(cache,
3411 if (local_flags & __GFP_WAIT)
3413 kmem_flagcheck(cache, flags);
3414 obj = kmem_getpages(cache, local_flags, numa_mem_id());
3415 if (local_flags & __GFP_WAIT)
3422 if (cache_grow(cache, flags, nid, obj)) {
3423 obj = ____cache_alloc_node(cache,
3439 if (
unlikely(!put_mems_allowed(cpuset_mems_cookie) && !obj))
3447 static void *____cache_alloc_node(
struct kmem_cache *cachep,
gfp_t flags,
3470 slabp =
list_entry(entry,
struct slab, list);
3480 obj = slab_get_obj(cachep, slabp, nodeid);
3500 return fallback_alloc(cachep, flags);
3520 unsigned long caller)
3522 unsigned long save_flags;
3530 if (slab_should_failslab(cachep, flags))
3533 cache_alloc_debugcheck_before(cachep, flags);
3541 ptr = fallback_alloc(cachep, flags);
3545 if (nodeid == slab_node) {
3552 ptr = ____cache_alloc(cachep, flags);
3557 ptr = ____cache_alloc_node(cachep, flags, nodeid);
3579 objp = alternate_node_alloc(cache, flags);
3583 objp = ____cache_alloc(cache, flags);
3590 objp = ____cache_alloc_node(cache, flags, numa_mem_id());
3600 return ____cache_alloc(cachep, flags);
3606 slab_alloc(
struct kmem_cache *cachep,
gfp_t flags,
unsigned long caller)
3608 unsigned long save_flags;
3615 if (slab_should_failslab(cachep, flags))
3618 cache_alloc_debugcheck_before(cachep, flags);
3620 objp = __do_cache_alloc(cachep, flags);
3639 static void free_block(
struct kmem_cache *cachep,
void **objpp,
int nr_objects,
3645 for (i = 0; i < nr_objects; i++) {
3649 clear_obj_pfmemalloc(&objpp[i]);
3652 slabp = virt_to_slab(objp);
3657 slab_put_obj(cachep, slabp, objp, node);
3663 if (slabp->
inuse == 0) {
3672 slab_destroy(cachep, slabp);
3690 int node = numa_mem_id();
3701 int max = shared_array->
limit - shared_array->
avail;
3703 if (batchcount > max)
3706 ac->
entry,
sizeof(
void *) * batchcount);
3712 free_block(cachep, ac->
entry, batchcount, node);
3733 ac->
avail -= batchcount;
3741 static inline void __cache_free(
struct kmem_cache *cachep,
void *objp,
3742 unsigned long caller)
3747 kmemleak_free_recursive(objp, cachep->
flags);
3766 cache_flusharray(cachep, ac);
3769 ac_put_obj(cachep, ac, objp);
3782 void *ret = slab_alloc(cachep, flags,
_RET_IP_);
3784 trace_kmem_cache_alloc(
_RET_IP_, ret,
3791 #ifdef CONFIG_TRACING
3793 kmem_cache_alloc_trace(
struct kmem_cache *cachep,
gfp_t flags,
size_t size)
3797 ret = slab_alloc(cachep, flags,
_RET_IP_);
3800 size, cachep->
size, flags);
3809 void *ret = slab_alloc_node(cachep, flags, nodeid,
_RET_IP_);
3811 trace_kmem_cache_alloc_node(
_RET_IP_, ret,
3819 #ifdef CONFIG_TRACING
3820 void *kmem_cache_alloc_node_trace(
struct kmem_cache *cachep,
3827 ret = slab_alloc_node(cachep, flags, nodeid,
_RET_IP_);
3838 __do_kmalloc_node(
size_t size,
gfp_t flags,
int node,
unsigned long caller)
3842 cachep = kmem_find_general_cachep(size, flags);
3845 return kmem_cache_alloc_node_trace(cachep, flags, node, size);
3848 #if defined(CONFIG_DEBUG_SLAB) || defined(CONFIG_TRACING)
3851 return __do_kmalloc_node(size, flags, node,
_RET_IP_);
3855 void *__kmalloc_node_track_caller(
size_t size,
gfp_t flags,
3856 int node,
unsigned long caller)
3858 return __do_kmalloc_node(size, flags, node, caller);
3864 return __do_kmalloc_node(size, flags, node, 0);
3877 unsigned long caller)
3887 cachep = __find_general_cachep(size, flags);
3890 ret = slab_alloc(cachep, flags, caller);
3892 trace_kmalloc(caller, ret,
3893 size, cachep->
size, flags);
3899 #if defined(CONFIG_DEBUG_SLAB) || defined(CONFIG_TRACING)
3902 return __do_kmalloc(size, flags,
_RET_IP_);
3908 return __do_kmalloc(size, flags, caller);
3915 return __do_kmalloc(size, flags, 0);
3930 unsigned long flags;
3935 debug_check_no_obj_freed(objp, cachep->
object_size);
3936 __cache_free(cachep, objp,
_RET_IP_);
3939 trace_kmem_cache_free(
_RET_IP_, objp);
3955 unsigned long flags;
3963 c = virt_to_cache(objp);
3967 __cache_free(c, (
void *)objp,
_RET_IP_);
3990 if (use_alien_caches) {
3991 new_alien = alloc_alien_cache(node, cachep->
limit, gfp);
3998 new_shared = alloc_arraycache(node,
4002 free_alien_cache(new_alien);
4014 free_block(cachep, shared->
entry,
4015 shared->
avail, node);
4019 l3->
alien = new_alien;
4026 free_alien_cache(new_alien);
4029 l3 = kmalloc_node(
sizeof(
struct kmem_list3), gfp, node);
4031 free_alien_cache(new_alien);
4036 kmem_list3_init(l3);
4040 l3->
alien = new_alien;
4048 if (!cachep->
list.next) {
4056 free_alien_cache(l3->
alien);
4071 static void do_ccupdate_local(
void *
info)
4077 old = cpu_cache_get(new->cachep);
4084 static int do_tune_cpucache(
struct kmem_cache *cachep,
int limit,
4085 int batchcount,
int shared,
gfp_t gfp)
4090 new = kzalloc(
sizeof(*
new) + nr_cpu_ids *
sizeof(
struct array_cache *),
4096 new->new[
i] = alloc_arraycache(
cpu_to_mem(i), limit,
4099 for (i--; i >= 0; i--)
4124 return alloc_kmemlist(cachep, gfp);
4142 if (cachep->
size > 131072)
4146 else if (cachep->
size > 1024)
4148 else if (cachep->
size > 256)
4174 err = do_tune_cpucache(cachep, limit, (limit + 1) / 2, shared, gfp);
4177 cachep->
name, -err);
4191 if (!ac || !ac->
avail)
4198 tofree = force ? ac->
avail : (ac->
limit + 4) / 5;
4199 if (tofree > ac->
avail)
4200 tofree = (ac->
avail + 1) / 2;
4201 free_block(cachep, ac->
entry, tofree, node);
4202 ac->
avail -= tofree;
4204 sizeof(
void *) * ac->
avail);
4226 int node = numa_mem_id();
4245 drain_array(searchp, l3, cpu_cache_get(searchp), 0, node);
4256 drain_array(searchp, l3, l3->
shared, 0, node);
4263 freed = drain_freelist(searchp, l3, (l3->
free_limit +
4264 5 * searchp->
num - 1) / (5 * searchp->
num));
4278 #ifdef CONFIG_SLABINFO
4280 static void print_slabinfo_header(
struct seq_file *
m)
4287 seq_puts(m,
"slabinfo - version: 2.1 (statistics)\n");
4289 seq_puts(m,
"slabinfo - version: 2.1\n");
4291 seq_puts(m,
"# name <active_objs> <num_objs> <objsize> "
4292 "<objperslab> <pagesperslab>");
4293 seq_puts(m,
" : tunables <limit> <batchcount> <sharedfactor>");
4294 seq_puts(m,
" : slabdata <active_slabs> <num_slabs> <sharedavail>");
4296 seq_puts(m,
" : globalstat <listallocs> <maxobjs> <grown> <reaped> "
4297 "<error> <maxfreeable> <nodeallocs> <remotefrees> <alienoverflow>");
4298 seq_puts(m,
" : cpustat <allochit> <allocmiss> <freehit> <freemiss>");
4309 print_slabinfo_header(m);
4314 static void *s_next(
struct seq_file *m,
void *p, loff_t *pos)
4319 static void s_stop(
struct seq_file *m,
void *p)
4324 static int s_show(
struct seq_file *m,
void *p)
4328 unsigned long active_objs;
4329 unsigned long num_objs;
4330 unsigned long active_slabs = 0;
4331 unsigned long num_slabs, free_objects = 0, shared_avail = 0;
4348 if (slabp->
inuse != cachep->
num && !error)
4349 error =
"slabs_full accounting error";
4350 active_objs += cachep->
num;
4354 if (slabp->
inuse == cachep->
num && !error)
4355 error =
"slabs_partial inuse accounting error";
4356 if (!slabp->
inuse && !error)
4357 error =
"slabs_partial/inuse accounting error";
4358 active_objs += slabp->
inuse;
4362 if (slabp->
inuse && !error)
4363 error =
"slabs_free/inuse accounting error";
4368 shared_avail += l3->
shared->avail;
4372 num_slabs += active_slabs;
4373 num_objs = num_slabs * cachep->
num;
4374 if (num_objs - active_objs != free_objects && !error)
4375 error =
"free_objects accounting error";
4377 name = cachep->
name;
4382 name, active_objs, num_objs, cachep->
size,
4387 active_slabs, num_slabs, shared_avail);
4390 unsigned long high = cachep->high_mark;
4391 unsigned long allocs = cachep->num_allocations;
4392 unsigned long grown = cachep->grown;
4393 unsigned long reaped = cachep->reaped;
4394 unsigned long errors = cachep->errors;
4395 unsigned long max_freeable = cachep->max_freeable;
4396 unsigned long node_allocs = cachep->node_allocs;
4397 unsigned long node_frees = cachep->node_frees;
4398 unsigned long overflows = cachep->node_overflow;
4400 seq_printf(m,
" : globalstat %7lu %6lu %5lu %4lu "
4401 "%4lu %4lu %4lu %4lu %4lu",
4402 allocs, high, grown,
4403 reaped, errors, max_freeable, node_allocs,
4404 node_frees, overflows);
4408 unsigned long allochit =
atomic_read(&cachep->allochit);
4409 unsigned long allocmiss =
atomic_read(&cachep->allocmiss);
4410 unsigned long freehit =
atomic_read(&cachep->freehit);
4411 unsigned long freemiss =
atomic_read(&cachep->freemiss);
4413 seq_printf(m,
" : cpustat %6lu %6lu %6lu %6lu",
4414 allochit, allocmiss, freehit, freemiss);
4442 #define MAX_SLABINFO_WRITE 128
4451 size_t count, loff_t *ppos)
4453 char kbuf[MAX_SLABINFO_WRITE + 1], *
tmp;
4457 if (count > MAX_SLABINFO_WRITE)
4461 kbuf[MAX_SLABINFO_WRITE] =
'\0';
4468 if (
sscanf(tmp,
" %d %d %d", &limit, &batchcount, &shared) != 3)
4476 if (limit < 1 || batchcount < 1 ||
4477 batchcount > limit || shared < 0) {
4480 res = do_tune_cpucache(cachep, limit,
4493 static int slabinfo_open(
struct inode *
inode,
struct file *file)
4495 return seq_open(file, &slabinfo_op);
4499 .
open = slabinfo_open,
4501 .write = slabinfo_write,
4506 #ifdef CONFIG_DEBUG_SLAB_LEAK
4508 static void *leaks_start(
struct seq_file *m, loff_t *pos)
4514 static inline int add_caller(
unsigned long *n,
unsigned long v)
4524 unsigned long *q = p + 2 *
i;
4538 memmove(p + 2, p, n[1] * 2 *
sizeof(
unsigned long) - ((
void *)p - (
void *)n));
4544 static void handle_slab(
unsigned long *n,
struct kmem_cache *
c,
struct slab *s)
4550 for (i = 0, p = s->
s_mem; i < c->num; i++, p += c->
size) {
4553 if (!add_caller(n, (
unsigned long)*
dbg_userword(c, p)))
4560 #ifdef CONFIG_KALLSYMS
4565 seq_printf(m,
"%s+%#lx/%#lx", name, offset, size);
4574 static int leaks_show(
struct seq_file *m,
void *p)
4580 unsigned long *n = m->
private;
4602 handle_slab(n, cachep, slabp);
4604 handle_slab(n, cachep, slabp);
4605 spin_unlock_irq(&l3->list_lock);
4607 name = cachep->name;
4618 *(
unsigned long *)m->
private = n[0] * 2;
4625 for (i = 0; i < n[1]; i++) {
4627 show_symbol(m, n[2*i+2]);
4635 .
start = leaks_start,
4641 static int slabstats_open(
struct inode *
inode,
struct file *file)
4646 ret =
seq_open(file, &slabstats_op);
4659 .
open = slabstats_open,
4666 static int __init slab_proc_init(
void)
4669 #ifdef CONFIG_DEBUG_SLAB_LEAK
4670 proc_create(
"slab_allocators", 0,
NULL, &proc_slabstats_operations);
4695 return virt_to_cache(objp)->object_size;