Christoph Lameter | 2e892f4 | 2006-12-13 00:34:23 -0800 | [diff] [blame] | 1 | #ifndef _LINUX_SLAB_DEF_H |
| 2 | #define _LINUX_SLAB_DEF_H |
| 3 | |
| 4 | /* |
| 5 | * Definitions unique to the original Linux SLAB allocator. |
| 6 | * |
| 7 | * What we provide here is a way to optimize the frequent kmalloc |
| 8 | * calls in the kernel by selecting the appropriate general cache |
| 9 | * if kmalloc was called with a size that can be established at |
| 10 | * compile time. |
| 11 | */ |
| 12 | |
| 13 | #include <linux/init.h> |
| 14 | #include <asm/page.h> /* kmalloc_sizes.h needs PAGE_SIZE */ |
| 15 | #include <asm/cache.h> /* kmalloc_sizes.h needs L1_CACHE_BYTES */ |
| 16 | #include <linux/compiler.h> |
Li Zefan | 039ca4e | 2010-05-26 17:22:17 +0800 | [diff] [blame^] | 17 | |
| 18 | #include <trace/events/kmem.h> |
Christoph Lameter | 2e892f4 | 2006-12-13 00:34:23 -0800 | [diff] [blame] | 19 | |
David Woodhouse | 1f0ce8b3 | 2010-05-19 12:01:42 +0100 | [diff] [blame] | 20 | #ifndef ARCH_KMALLOC_MINALIGN |
| 21 | /* |
| 22 | * Enforce a minimum alignment for the kmalloc caches. |
| 23 | * Usually, the kmalloc caches are cache_line_size() aligned, except when |
| 24 | * DEBUG and FORCED_DEBUG are enabled, then they are BYTES_PER_WORD aligned. |
| 25 | * Some archs want to perform DMA into kmalloc caches and need a guaranteed |
| 26 | * alignment larger than the alignment of a 64-bit integer. |
| 27 | * ARCH_KMALLOC_MINALIGN allows that. |
| 28 | * Note that increasing this value may disable some debug features. |
| 29 | */ |
| 30 | #define ARCH_KMALLOC_MINALIGN __alignof__(unsigned long long) |
| 31 | #endif |
| 32 | |
| 33 | #ifndef ARCH_SLAB_MINALIGN |
| 34 | /* |
| 35 | * Enforce a minimum alignment for all caches. |
| 36 | * Intended for archs that get misalignment faults even for BYTES_PER_WORD |
| 37 | * aligned buffers. Includes ARCH_KMALLOC_MINALIGN. |
| 38 | * If possible: Do not enable this flag for CONFIG_DEBUG_SLAB, it disables |
| 39 | * some debug features. |
| 40 | */ |
| 41 | #define ARCH_SLAB_MINALIGN 0 |
| 42 | #endif |
| 43 | |
Pekka Enberg | 8eae985 | 2008-05-09 20:32:44 +0200 | [diff] [blame] | 44 | /* |
| 45 | * struct kmem_cache |
| 46 | * |
| 47 | * manages a cache. |
| 48 | */ |
| 49 | |
| 50 | struct kmem_cache { |
| 51 | /* 1) per-cpu data, touched during every alloc/free */ |
| 52 | struct array_cache *array[NR_CPUS]; |
| 53 | /* 2) Cache tunables. Protected by cache_chain_mutex */ |
| 54 | unsigned int batchcount; |
| 55 | unsigned int limit; |
| 56 | unsigned int shared; |
| 57 | |
| 58 | unsigned int buffer_size; |
| 59 | u32 reciprocal_buffer_size; |
| 60 | /* 3) touched by every alloc & free from the backend */ |
| 61 | |
| 62 | unsigned int flags; /* constant flags */ |
| 63 | unsigned int num; /* # of objs per slab */ |
| 64 | |
| 65 | /* 4) cache_grow/shrink */ |
| 66 | /* order of pgs per slab (2^n) */ |
| 67 | unsigned int gfporder; |
| 68 | |
| 69 | /* force GFP flags, e.g. GFP_DMA */ |
| 70 | gfp_t gfpflags; |
| 71 | |
| 72 | size_t colour; /* cache colouring range */ |
| 73 | unsigned int colour_off; /* colour offset */ |
| 74 | struct kmem_cache *slabp_cache; |
| 75 | unsigned int slab_size; |
| 76 | unsigned int dflags; /* dynamic flags */ |
| 77 | |
| 78 | /* constructor func */ |
| 79 | void (*ctor)(void *obj); |
| 80 | |
| 81 | /* 5) cache creation/removal */ |
| 82 | const char *name; |
| 83 | struct list_head next; |
| 84 | |
| 85 | /* 6) statistics */ |
| 86 | #ifdef CONFIG_DEBUG_SLAB |
| 87 | unsigned long num_active; |
| 88 | unsigned long num_allocations; |
| 89 | unsigned long high_mark; |
| 90 | unsigned long grown; |
| 91 | unsigned long reaped; |
| 92 | unsigned long errors; |
| 93 | unsigned long max_freeable; |
| 94 | unsigned long node_allocs; |
| 95 | unsigned long node_frees; |
| 96 | unsigned long node_overflow; |
| 97 | atomic_t allochit; |
| 98 | atomic_t allocmiss; |
| 99 | atomic_t freehit; |
| 100 | atomic_t freemiss; |
| 101 | |
| 102 | /* |
| 103 | * If debugging is enabled, then the allocator can add additional |
| 104 | * fields and/or padding to every object. buffer_size contains the total |
| 105 | * object size including these internal fields, the following two |
| 106 | * variables contain the offset to the user object and its size. |
| 107 | */ |
| 108 | int obj_offset; |
| 109 | int obj_size; |
| 110 | #endif /* CONFIG_DEBUG_SLAB */ |
| 111 | |
| 112 | /* |
| 113 | * We put nodelists[] at the end of kmem_cache, because we want to size |
| 114 | * this array to nr_node_ids slots instead of MAX_NUMNODES |
| 115 | * (see kmem_cache_init()) |
| 116 | * We still use [MAX_NUMNODES] and not [1] or [0] because cache_cache |
| 117 | * is statically defined, so we reserve the max number of nodes. |
| 118 | */ |
| 119 | struct kmem_list3 *nodelists[MAX_NUMNODES]; |
| 120 | /* |
| 121 | * Do not add fields after nodelists[] |
| 122 | */ |
| 123 | }; |
| 124 | |
Christoph Lameter | 2e892f4 | 2006-12-13 00:34:23 -0800 | [diff] [blame] | 125 | /* Size description struct for general caches. */ |
| 126 | struct cache_sizes { |
| 127 | size_t cs_size; |
| 128 | struct kmem_cache *cs_cachep; |
Christoph Lameter | 4b51d66 | 2007-02-10 01:43:10 -0800 | [diff] [blame] | 129 | #ifdef CONFIG_ZONE_DMA |
Christoph Lameter | 2e892f4 | 2006-12-13 00:34:23 -0800 | [diff] [blame] | 130 | struct kmem_cache *cs_dmacachep; |
Christoph Lameter | 4b51d66 | 2007-02-10 01:43:10 -0800 | [diff] [blame] | 131 | #endif |
Christoph Lameter | 2e892f4 | 2006-12-13 00:34:23 -0800 | [diff] [blame] | 132 | }; |
| 133 | extern struct cache_sizes malloc_sizes[]; |
| 134 | |
Paul Mundt | 6193a2f | 2007-07-15 23:38:22 -0700 | [diff] [blame] | 135 | void *kmem_cache_alloc(struct kmem_cache *, gfp_t); |
| 136 | void *__kmalloc(size_t size, gfp_t flags); |
| 137 | |
Li Zefan | 0f24f12 | 2009-12-11 15:45:30 +0800 | [diff] [blame] | 138 | #ifdef CONFIG_TRACING |
Eduard - Gabriel Munteanu | 3655575 | 2008-08-10 20:14:05 +0300 | [diff] [blame] | 139 | extern void *kmem_cache_alloc_notrace(struct kmem_cache *cachep, gfp_t flags); |
| 140 | extern size_t slab_buffer_size(struct kmem_cache *cachep); |
| 141 | #else |
| 142 | static __always_inline void * |
| 143 | kmem_cache_alloc_notrace(struct kmem_cache *cachep, gfp_t flags) |
Christoph Lameter | 2e892f4 | 2006-12-13 00:34:23 -0800 | [diff] [blame] | 144 | { |
Eduard - Gabriel Munteanu | 3655575 | 2008-08-10 20:14:05 +0300 | [diff] [blame] | 145 | return kmem_cache_alloc(cachep, flags); |
| 146 | } |
| 147 | static inline size_t slab_buffer_size(struct kmem_cache *cachep) |
| 148 | { |
| 149 | return 0; |
| 150 | } |
| 151 | #endif |
| 152 | |
| 153 | static __always_inline void *kmalloc(size_t size, gfp_t flags) |
| 154 | { |
| 155 | struct kmem_cache *cachep; |
| 156 | void *ret; |
| 157 | |
Christoph Lameter | 2e892f4 | 2006-12-13 00:34:23 -0800 | [diff] [blame] | 158 | if (__builtin_constant_p(size)) { |
| 159 | int i = 0; |
Christoph Lameter | 6cb8f91 | 2007-07-17 04:03:22 -0700 | [diff] [blame] | 160 | |
| 161 | if (!size) |
| 162 | return ZERO_SIZE_PTR; |
| 163 | |
Christoph Lameter | 2e892f4 | 2006-12-13 00:34:23 -0800 | [diff] [blame] | 164 | #define CACHE(x) \ |
| 165 | if (size <= x) \ |
| 166 | goto found; \ |
| 167 | else \ |
| 168 | i++; |
Joe Perches | 1c61fc4 | 2008-03-05 13:58:17 -0800 | [diff] [blame] | 169 | #include <linux/kmalloc_sizes.h> |
Christoph Lameter | 2e892f4 | 2006-12-13 00:34:23 -0800 | [diff] [blame] | 170 | #undef CACHE |
Jeff Mahoney | 1cf3eb2 | 2009-01-27 23:48:59 +0200 | [diff] [blame] | 171 | return NULL; |
Christoph Lameter | 2e892f4 | 2006-12-13 00:34:23 -0800 | [diff] [blame] | 172 | found: |
Christoph Lameter | 4b51d66 | 2007-02-10 01:43:10 -0800 | [diff] [blame] | 173 | #ifdef CONFIG_ZONE_DMA |
| 174 | if (flags & GFP_DMA) |
Eduard - Gabriel Munteanu | 3655575 | 2008-08-10 20:14:05 +0300 | [diff] [blame] | 175 | cachep = malloc_sizes[i].cs_dmacachep; |
| 176 | else |
Christoph Lameter | 4b51d66 | 2007-02-10 01:43:10 -0800 | [diff] [blame] | 177 | #endif |
Eduard - Gabriel Munteanu | 3655575 | 2008-08-10 20:14:05 +0300 | [diff] [blame] | 178 | cachep = malloc_sizes[i].cs_cachep; |
| 179 | |
| 180 | ret = kmem_cache_alloc_notrace(cachep, flags); |
| 181 | |
Eduard - Gabriel Munteanu | ca2b84c | 2009-03-23 15:12:24 +0200 | [diff] [blame] | 182 | trace_kmalloc(_THIS_IP_, ret, |
| 183 | size, slab_buffer_size(cachep), flags); |
Eduard - Gabriel Munteanu | 3655575 | 2008-08-10 20:14:05 +0300 | [diff] [blame] | 184 | |
| 185 | return ret; |
Christoph Lameter | 2e892f4 | 2006-12-13 00:34:23 -0800 | [diff] [blame] | 186 | } |
| 187 | return __kmalloc(size, flags); |
| 188 | } |
| 189 | |
Christoph Lameter | 2e892f4 | 2006-12-13 00:34:23 -0800 | [diff] [blame] | 190 | #ifdef CONFIG_NUMA |
| 191 | extern void *__kmalloc_node(size_t size, gfp_t flags, int node); |
Paul Mundt | 6193a2f | 2007-07-15 23:38:22 -0700 | [diff] [blame] | 192 | extern void *kmem_cache_alloc_node(struct kmem_cache *, gfp_t flags, int node); |
Christoph Lameter | 2e892f4 | 2006-12-13 00:34:23 -0800 | [diff] [blame] | 193 | |
Li Zefan | 0f24f12 | 2009-12-11 15:45:30 +0800 | [diff] [blame] | 194 | #ifdef CONFIG_TRACING |
Eduard - Gabriel Munteanu | 3655575 | 2008-08-10 20:14:05 +0300 | [diff] [blame] | 195 | extern void *kmem_cache_alloc_node_notrace(struct kmem_cache *cachep, |
| 196 | gfp_t flags, |
| 197 | int nodeid); |
| 198 | #else |
| 199 | static __always_inline void * |
| 200 | kmem_cache_alloc_node_notrace(struct kmem_cache *cachep, |
| 201 | gfp_t flags, |
| 202 | int nodeid) |
Christoph Lameter | 2e892f4 | 2006-12-13 00:34:23 -0800 | [diff] [blame] | 203 | { |
Eduard - Gabriel Munteanu | 3655575 | 2008-08-10 20:14:05 +0300 | [diff] [blame] | 204 | return kmem_cache_alloc_node(cachep, flags, nodeid); |
| 205 | } |
| 206 | #endif |
| 207 | |
| 208 | static __always_inline void *kmalloc_node(size_t size, gfp_t flags, int node) |
| 209 | { |
| 210 | struct kmem_cache *cachep; |
| 211 | void *ret; |
| 212 | |
Christoph Lameter | 2e892f4 | 2006-12-13 00:34:23 -0800 | [diff] [blame] | 213 | if (__builtin_constant_p(size)) { |
| 214 | int i = 0; |
Christoph Lameter | 6cb8f91 | 2007-07-17 04:03:22 -0700 | [diff] [blame] | 215 | |
| 216 | if (!size) |
| 217 | return ZERO_SIZE_PTR; |
| 218 | |
Christoph Lameter | 2e892f4 | 2006-12-13 00:34:23 -0800 | [diff] [blame] | 219 | #define CACHE(x) \ |
| 220 | if (size <= x) \ |
| 221 | goto found; \ |
| 222 | else \ |
| 223 | i++; |
Joe Perches | 1c61fc4 | 2008-03-05 13:58:17 -0800 | [diff] [blame] | 224 | #include <linux/kmalloc_sizes.h> |
Christoph Lameter | 2e892f4 | 2006-12-13 00:34:23 -0800 | [diff] [blame] | 225 | #undef CACHE |
Jeff Mahoney | 1cf3eb2 | 2009-01-27 23:48:59 +0200 | [diff] [blame] | 226 | return NULL; |
Christoph Lameter | 2e892f4 | 2006-12-13 00:34:23 -0800 | [diff] [blame] | 227 | found: |
Christoph Lameter | 4b51d66 | 2007-02-10 01:43:10 -0800 | [diff] [blame] | 228 | #ifdef CONFIG_ZONE_DMA |
| 229 | if (flags & GFP_DMA) |
Eduard - Gabriel Munteanu | 3655575 | 2008-08-10 20:14:05 +0300 | [diff] [blame] | 230 | cachep = malloc_sizes[i].cs_dmacachep; |
| 231 | else |
Christoph Lameter | 4b51d66 | 2007-02-10 01:43:10 -0800 | [diff] [blame] | 232 | #endif |
Eduard - Gabriel Munteanu | 3655575 | 2008-08-10 20:14:05 +0300 | [diff] [blame] | 233 | cachep = malloc_sizes[i].cs_cachep; |
| 234 | |
| 235 | ret = kmem_cache_alloc_node_notrace(cachep, flags, node); |
| 236 | |
Eduard - Gabriel Munteanu | ca2b84c | 2009-03-23 15:12:24 +0200 | [diff] [blame] | 237 | trace_kmalloc_node(_THIS_IP_, ret, |
| 238 | size, slab_buffer_size(cachep), |
| 239 | flags, node); |
Eduard - Gabriel Munteanu | 3655575 | 2008-08-10 20:14:05 +0300 | [diff] [blame] | 240 | |
| 241 | return ret; |
Christoph Lameter | 2e892f4 | 2006-12-13 00:34:23 -0800 | [diff] [blame] | 242 | } |
| 243 | return __kmalloc_node(size, flags, node); |
| 244 | } |
| 245 | |
| 246 | #endif /* CONFIG_NUMA */ |
| 247 | |
Christoph Lameter | 2e892f4 | 2006-12-13 00:34:23 -0800 | [diff] [blame] | 248 | #endif /* _LINUX_SLAB_DEF_H */ |