Christoph Lameter | 039363f | 2012-07-06 15:25:10 -0500 | [diff] [blame] | 1 | /* |
| 2 | * Slab allocator functions that are independent of the allocator strategy |
| 3 | * |
| 4 | * (C) 2012 Christoph Lameter <cl@linux.com> |
| 5 | */ |
| 6 | #include <linux/slab.h> |
| 7 | |
| 8 | #include <linux/mm.h> |
| 9 | #include <linux/poison.h> |
| 10 | #include <linux/interrupt.h> |
| 11 | #include <linux/memory.h> |
| 12 | #include <linux/compiler.h> |
| 13 | #include <linux/module.h> |
Christoph Lameter | 20cea96 | 2012-07-06 15:25:13 -0500 | [diff] [blame] | 14 | #include <linux/cpu.h> |
| 15 | #include <linux/uaccess.h> |
Glauber Costa | b7454ad | 2012-10-19 18:20:25 +0400 | [diff] [blame] | 16 | #include <linux/seq_file.h> |
| 17 | #include <linux/proc_fs.h> |
Christoph Lameter | 039363f | 2012-07-06 15:25:10 -0500 | [diff] [blame] | 18 | #include <asm/cacheflush.h> |
| 19 | #include <asm/tlbflush.h> |
| 20 | #include <asm/page.h> |
Glauber Costa | 2633d7a | 2012-12-18 14:22:34 -0800 | [diff] [blame] | 21 | #include <linux/memcontrol.h> |
Andrey Ryabinin | 928cec9 | 2014-08-06 16:04:44 -0700 | [diff] [blame] | 22 | |
| 23 | #define CREATE_TRACE_POINTS |
Christoph Lameter | f1b6eb6 | 2013-09-04 16:35:34 +0000 | [diff] [blame] | 24 | #include <trace/events/kmem.h> |
Christoph Lameter | 039363f | 2012-07-06 15:25:10 -0500 | [diff] [blame] | 25 | |
Christoph Lameter | 97d0660 | 2012-07-06 15:25:11 -0500 | [diff] [blame] | 26 | #include "slab.h" |
| 27 | |
| 28 | enum slab_state slab_state; |
Christoph Lameter | 18004c5 | 2012-07-06 15:25:12 -0500 | [diff] [blame] | 29 | LIST_HEAD(slab_caches); |
| 30 | DEFINE_MUTEX(slab_mutex); |
Christoph Lameter | 9b030cb | 2012-09-05 00:20:33 +0000 | [diff] [blame] | 31 | struct kmem_cache *kmem_cache; |
Christoph Lameter | 97d0660 | 2012-07-06 15:25:11 -0500 | [diff] [blame] | 32 | |
Joonsoo Kim | 07f361b | 2014-10-09 15:26:00 -0700 | [diff] [blame] | 33 | /* |
Joonsoo Kim | 423c929 | 2014-10-09 15:26:22 -0700 | [diff] [blame] | 34 | * Set of flags that will prevent slab merging |
| 35 | */ |
| 36 | #define SLAB_NEVER_MERGE (SLAB_RED_ZONE | SLAB_POISON | SLAB_STORE_USER | \ |
| 37 | SLAB_TRACE | SLAB_DESTROY_BY_RCU | SLAB_NOLEAKTRACE | \ |
| 38 | SLAB_FAILSLAB) |
| 39 | |
| 40 | #define SLAB_MERGE_SAME (SLAB_DEBUG_FREE | SLAB_RECLAIM_ACCOUNT | \ |
| 41 | SLAB_CACHE_DMA | SLAB_NOTRACK) |
| 42 | |
| 43 | /* |
| 44 | * Merge control. If this is set then no merging of slab caches will occur. |
| 45 | * (Could be removed. This was introduced to pacify the merge skeptics.) |
| 46 | */ |
| 47 | static int slab_nomerge; |
| 48 | |
| 49 | static int __init setup_slab_nomerge(char *str) |
| 50 | { |
| 51 | slab_nomerge = 1; |
| 52 | return 1; |
| 53 | } |
| 54 | |
| 55 | #ifdef CONFIG_SLUB |
| 56 | __setup_param("slub_nomerge", slub_nomerge, setup_slab_nomerge, 0); |
| 57 | #endif |
| 58 | |
| 59 | __setup("slab_nomerge", setup_slab_nomerge); |
| 60 | |
| 61 | /* |
Joonsoo Kim | 07f361b | 2014-10-09 15:26:00 -0700 | [diff] [blame] | 62 | * Determine the size of a slab object |
| 63 | */ |
| 64 | unsigned int kmem_cache_size(struct kmem_cache *s) |
| 65 | { |
| 66 | return s->object_size; |
| 67 | } |
| 68 | EXPORT_SYMBOL(kmem_cache_size); |
| 69 | |
Shuah Khan | 77be4b1 | 2012-08-16 00:09:46 -0700 | [diff] [blame] | 70 | #ifdef CONFIG_DEBUG_VM |
Vladimir Davydov | 794b124 | 2014-04-07 15:39:26 -0700 | [diff] [blame] | 71 | static int kmem_cache_sanity_check(const char *name, size_t size) |
Shuah Khan | 77be4b1 | 2012-08-16 00:09:46 -0700 | [diff] [blame] | 72 | { |
| 73 | struct kmem_cache *s = NULL; |
| 74 | |
| 75 | if (!name || in_interrupt() || size < sizeof(void *) || |
| 76 | size > KMALLOC_MAX_SIZE) { |
| 77 | pr_err("kmem_cache_create(%s) integrity check failed\n", name); |
| 78 | return -EINVAL; |
| 79 | } |
| 80 | |
| 81 | list_for_each_entry(s, &slab_caches, list) { |
| 82 | char tmp; |
| 83 | int res; |
| 84 | |
| 85 | /* |
| 86 | * This happens when the module gets unloaded and doesn't |
| 87 | * destroy its slab cache and no-one else reuses the vmalloc |
| 88 | * area of the module. Print a warning. |
| 89 | */ |
| 90 | res = probe_kernel_address(s->name, tmp); |
| 91 | if (res) { |
| 92 | pr_err("Slab cache with size %d has lost its name\n", |
| 93 | s->object_size); |
| 94 | continue; |
| 95 | } |
| 96 | |
Mikulas Patocka | 6946174 | 2014-03-04 17:13:47 -0500 | [diff] [blame] | 97 | #if !defined(CONFIG_SLUB) |
Vladimir Davydov | 794b124 | 2014-04-07 15:39:26 -0700 | [diff] [blame] | 98 | if (!strcmp(s->name, name)) { |
Shuah Khan | 77be4b1 | 2012-08-16 00:09:46 -0700 | [diff] [blame] | 99 | pr_err("%s (%s): Cache name already exists.\n", |
| 100 | __func__, name); |
| 101 | dump_stack(); |
| 102 | s = NULL; |
| 103 | return -EINVAL; |
| 104 | } |
Christoph Lameter | 3e37491 | 2013-09-21 21:56:34 +0000 | [diff] [blame] | 105 | #endif |
Shuah Khan | 77be4b1 | 2012-08-16 00:09:46 -0700 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | WARN_ON(strchr(name, ' ')); /* It confuses parsers */ |
| 109 | return 0; |
| 110 | } |
| 111 | #else |
Vladimir Davydov | 794b124 | 2014-04-07 15:39:26 -0700 | [diff] [blame] | 112 | static inline int kmem_cache_sanity_check(const char *name, size_t size) |
Shuah Khan | 77be4b1 | 2012-08-16 00:09:46 -0700 | [diff] [blame] | 113 | { |
| 114 | return 0; |
| 115 | } |
| 116 | #endif |
| 117 | |
Glauber Costa | 55007d8 | 2012-12-18 14:22:38 -0800 | [diff] [blame] | 118 | #ifdef CONFIG_MEMCG_KMEM |
| 119 | int memcg_update_all_caches(int num_memcgs) |
| 120 | { |
| 121 | struct kmem_cache *s; |
| 122 | int ret = 0; |
| 123 | mutex_lock(&slab_mutex); |
| 124 | |
| 125 | list_for_each_entry(s, &slab_caches, list) { |
| 126 | if (!is_root_cache(s)) |
| 127 | continue; |
| 128 | |
| 129 | ret = memcg_update_cache_size(s, num_memcgs); |
| 130 | /* |
| 131 | * See comment in memcontrol.c, memcg_update_cache_size: |
| 132 | * Instead of freeing the memory, we'll just leave the caches |
| 133 | * up to this point in an updated state. |
| 134 | */ |
| 135 | if (ret) |
| 136 | goto out; |
| 137 | } |
| 138 | |
| 139 | memcg_update_array_size(num_memcgs); |
| 140 | out: |
| 141 | mutex_unlock(&slab_mutex); |
| 142 | return ret; |
| 143 | } |
| 144 | #endif |
| 145 | |
Christoph Lameter | 039363f | 2012-07-06 15:25:10 -0500 | [diff] [blame] | 146 | /* |
Joonsoo Kim | 423c929 | 2014-10-09 15:26:22 -0700 | [diff] [blame] | 147 | * Find a mergeable slab cache |
| 148 | */ |
| 149 | int slab_unmergeable(struct kmem_cache *s) |
| 150 | { |
| 151 | if (slab_nomerge || (s->flags & SLAB_NEVER_MERGE)) |
| 152 | return 1; |
| 153 | |
| 154 | if (!is_root_cache(s)) |
| 155 | return 1; |
| 156 | |
| 157 | if (s->ctor) |
| 158 | return 1; |
| 159 | |
| 160 | /* |
| 161 | * We may have set a slab to be unmergeable during bootstrap. |
| 162 | */ |
| 163 | if (s->refcount < 0) |
| 164 | return 1; |
| 165 | |
| 166 | return 0; |
| 167 | } |
| 168 | |
| 169 | struct kmem_cache *find_mergeable(size_t size, size_t align, |
| 170 | unsigned long flags, const char *name, void (*ctor)(void *)) |
| 171 | { |
| 172 | struct kmem_cache *s; |
| 173 | |
| 174 | if (slab_nomerge || (flags & SLAB_NEVER_MERGE)) |
| 175 | return NULL; |
| 176 | |
| 177 | if (ctor) |
| 178 | return NULL; |
| 179 | |
| 180 | size = ALIGN(size, sizeof(void *)); |
| 181 | align = calculate_alignment(flags, align, size); |
| 182 | size = ALIGN(size, align); |
| 183 | flags = kmem_cache_flags(size, flags, name, NULL); |
| 184 | |
| 185 | list_for_each_entry(s, &slab_caches, list) { |
| 186 | if (slab_unmergeable(s)) |
| 187 | continue; |
| 188 | |
| 189 | if (size > s->size) |
| 190 | continue; |
| 191 | |
| 192 | if ((flags & SLAB_MERGE_SAME) != (s->flags & SLAB_MERGE_SAME)) |
| 193 | continue; |
| 194 | /* |
| 195 | * Check if alignment is compatible. |
| 196 | * Courtesy of Adrian Drzewiecki |
| 197 | */ |
| 198 | if ((s->size & ~(align - 1)) != s->size) |
| 199 | continue; |
| 200 | |
| 201 | if (s->size - size >= sizeof(void *)) |
| 202 | continue; |
| 203 | |
| 204 | return s; |
| 205 | } |
| 206 | return NULL; |
| 207 | } |
| 208 | |
| 209 | /* |
Christoph Lameter | 4590685 | 2012-11-28 16:23:16 +0000 | [diff] [blame] | 210 | * Figure out what the alignment of the objects will be given a set of |
| 211 | * flags, a user specified alignment and the size of the objects. |
| 212 | */ |
| 213 | unsigned long calculate_alignment(unsigned long flags, |
| 214 | unsigned long align, unsigned long size) |
| 215 | { |
| 216 | /* |
| 217 | * If the user wants hardware cache aligned objects then follow that |
| 218 | * suggestion if the object is sufficiently large. |
| 219 | * |
| 220 | * The hardware cache alignment cannot override the specified |
| 221 | * alignment though. If that is greater then use it. |
| 222 | */ |
| 223 | if (flags & SLAB_HWCACHE_ALIGN) { |
| 224 | unsigned long ralign = cache_line_size(); |
| 225 | while (size <= ralign / 2) |
| 226 | ralign /= 2; |
| 227 | align = max(align, ralign); |
| 228 | } |
| 229 | |
| 230 | if (align < ARCH_SLAB_MINALIGN) |
| 231 | align = ARCH_SLAB_MINALIGN; |
| 232 | |
| 233 | return ALIGN(align, sizeof(void *)); |
| 234 | } |
| 235 | |
Vladimir Davydov | 794b124 | 2014-04-07 15:39:26 -0700 | [diff] [blame] | 236 | static struct kmem_cache * |
| 237 | do_kmem_cache_create(char *name, size_t object_size, size_t size, size_t align, |
| 238 | unsigned long flags, void (*ctor)(void *), |
| 239 | struct mem_cgroup *memcg, struct kmem_cache *root_cache) |
| 240 | { |
| 241 | struct kmem_cache *s; |
| 242 | int err; |
| 243 | |
| 244 | err = -ENOMEM; |
| 245 | s = kmem_cache_zalloc(kmem_cache, GFP_KERNEL); |
| 246 | if (!s) |
| 247 | goto out; |
| 248 | |
| 249 | s->name = name; |
| 250 | s->object_size = object_size; |
| 251 | s->size = size; |
| 252 | s->align = align; |
| 253 | s->ctor = ctor; |
| 254 | |
| 255 | err = memcg_alloc_cache_params(memcg, s, root_cache); |
| 256 | if (err) |
| 257 | goto out_free_cache; |
| 258 | |
| 259 | err = __kmem_cache_create(s, flags); |
| 260 | if (err) |
| 261 | goto out_free_cache; |
| 262 | |
| 263 | s->refcount = 1; |
| 264 | list_add(&s->list, &slab_caches); |
Vladimir Davydov | 794b124 | 2014-04-07 15:39:26 -0700 | [diff] [blame] | 265 | out: |
| 266 | if (err) |
| 267 | return ERR_PTR(err); |
| 268 | return s; |
| 269 | |
| 270 | out_free_cache: |
| 271 | memcg_free_cache_params(s); |
| 272 | kfree(s); |
| 273 | goto out; |
| 274 | } |
Christoph Lameter | 4590685 | 2012-11-28 16:23:16 +0000 | [diff] [blame] | 275 | |
| 276 | /* |
Christoph Lameter | 039363f | 2012-07-06 15:25:10 -0500 | [diff] [blame] | 277 | * kmem_cache_create - Create a cache. |
| 278 | * @name: A string which is used in /proc/slabinfo to identify this cache. |
| 279 | * @size: The size of objects to be created in this cache. |
| 280 | * @align: The required alignment for the objects. |
| 281 | * @flags: SLAB flags |
| 282 | * @ctor: A constructor for the objects. |
| 283 | * |
| 284 | * Returns a ptr to the cache on success, NULL on failure. |
| 285 | * Cannot be called within a interrupt, but can be interrupted. |
| 286 | * The @ctor is run when new pages are allocated by the cache. |
| 287 | * |
| 288 | * The flags are |
| 289 | * |
| 290 | * %SLAB_POISON - Poison the slab with a known test pattern (a5a5a5a5) |
| 291 | * to catch references to uninitialised memory. |
| 292 | * |
| 293 | * %SLAB_RED_ZONE - Insert `Red' zones around the allocated memory to check |
| 294 | * for buffer overruns. |
| 295 | * |
| 296 | * %SLAB_HWCACHE_ALIGN - Align the objects in this cache to a hardware |
| 297 | * cacheline. This can be beneficial if you're counting cycles as closely |
| 298 | * as davem. |
| 299 | */ |
Glauber Costa | 2633d7a | 2012-12-18 14:22:34 -0800 | [diff] [blame] | 300 | struct kmem_cache * |
Vladimir Davydov | 794b124 | 2014-04-07 15:39:26 -0700 | [diff] [blame] | 301 | kmem_cache_create(const char *name, size_t size, size_t align, |
| 302 | unsigned long flags, void (*ctor)(void *)) |
Christoph Lameter | 039363f | 2012-07-06 15:25:10 -0500 | [diff] [blame] | 303 | { |
Vladimir Davydov | 794b124 | 2014-04-07 15:39:26 -0700 | [diff] [blame] | 304 | struct kmem_cache *s; |
| 305 | char *cache_name; |
Vladimir Davydov | 3965fc3 | 2014-01-23 15:52:55 -0800 | [diff] [blame] | 306 | int err; |
Christoph Lameter | 039363f | 2012-07-06 15:25:10 -0500 | [diff] [blame] | 307 | |
Pekka Enberg | b920536 | 2012-08-16 10:12:18 +0300 | [diff] [blame] | 308 | get_online_cpus(); |
Vladimir Davydov | 03afc0e | 2014-06-04 16:07:20 -0700 | [diff] [blame] | 309 | get_online_mems(); |
| 310 | |
Pekka Enberg | b920536 | 2012-08-16 10:12:18 +0300 | [diff] [blame] | 311 | mutex_lock(&slab_mutex); |
Christoph Lameter | 686d550 | 2012-09-05 00:20:33 +0000 | [diff] [blame] | 312 | |
Vladimir Davydov | 794b124 | 2014-04-07 15:39:26 -0700 | [diff] [blame] | 313 | err = kmem_cache_sanity_check(name, size); |
Andrew Morton | 3aa24f5 | 2014-10-09 15:25:58 -0700 | [diff] [blame] | 314 | if (err) { |
| 315 | s = NULL; /* suppress uninit var warning */ |
Vladimir Davydov | 3965fc3 | 2014-01-23 15:52:55 -0800 | [diff] [blame] | 316 | goto out_unlock; |
Andrew Morton | 3aa24f5 | 2014-10-09 15:25:58 -0700 | [diff] [blame] | 317 | } |
Christoph Lameter | 686d550 | 2012-09-05 00:20:33 +0000 | [diff] [blame] | 318 | |
Glauber Costa | d884392 | 2012-10-17 15:36:51 +0400 | [diff] [blame] | 319 | /* |
| 320 | * Some allocators will constraint the set of valid flags to a subset |
| 321 | * of all flags. We expect them to define CACHE_CREATE_MASK in this |
| 322 | * case, and we'll just provide them with a sanitized version of the |
| 323 | * passed flags. |
| 324 | */ |
| 325 | flags &= CACHE_CREATE_MASK; |
Christoph Lameter | 686d550 | 2012-09-05 00:20:33 +0000 | [diff] [blame] | 326 | |
Vladimir Davydov | 794b124 | 2014-04-07 15:39:26 -0700 | [diff] [blame] | 327 | s = __kmem_cache_alias(name, size, align, flags, ctor); |
| 328 | if (s) |
Vladimir Davydov | 3965fc3 | 2014-01-23 15:52:55 -0800 | [diff] [blame] | 329 | goto out_unlock; |
Glauber Costa | 2633d7a | 2012-12-18 14:22:34 -0800 | [diff] [blame] | 330 | |
Vladimir Davydov | 794b124 | 2014-04-07 15:39:26 -0700 | [diff] [blame] | 331 | cache_name = kstrdup(name, GFP_KERNEL); |
| 332 | if (!cache_name) { |
| 333 | err = -ENOMEM; |
| 334 | goto out_unlock; |
| 335 | } |
Glauber Costa | 2633d7a | 2012-12-18 14:22:34 -0800 | [diff] [blame] | 336 | |
Vladimir Davydov | 794b124 | 2014-04-07 15:39:26 -0700 | [diff] [blame] | 337 | s = do_kmem_cache_create(cache_name, size, size, |
| 338 | calculate_alignment(flags, align, size), |
| 339 | flags, ctor, NULL, NULL); |
| 340 | if (IS_ERR(s)) { |
| 341 | err = PTR_ERR(s); |
| 342 | kfree(cache_name); |
| 343 | } |
Vladimir Davydov | 3965fc3 | 2014-01-23 15:52:55 -0800 | [diff] [blame] | 344 | |
| 345 | out_unlock: |
Christoph Lameter | 20cea96 | 2012-07-06 15:25:13 -0500 | [diff] [blame] | 346 | mutex_unlock(&slab_mutex); |
Vladimir Davydov | 03afc0e | 2014-06-04 16:07:20 -0700 | [diff] [blame] | 347 | |
| 348 | put_online_mems(); |
Christoph Lameter | 20cea96 | 2012-07-06 15:25:13 -0500 | [diff] [blame] | 349 | put_online_cpus(); |
| 350 | |
Dave Jones | ba3253c | 2014-01-29 14:05:48 -0800 | [diff] [blame] | 351 | if (err) { |
Christoph Lameter | 686d550 | 2012-09-05 00:20:33 +0000 | [diff] [blame] | 352 | if (flags & SLAB_PANIC) |
| 353 | panic("kmem_cache_create: Failed to create slab '%s'. Error %d\n", |
| 354 | name, err); |
| 355 | else { |
| 356 | printk(KERN_WARNING "kmem_cache_create(%s) failed with error %d", |
| 357 | name, err); |
| 358 | dump_stack(); |
| 359 | } |
Christoph Lameter | 686d550 | 2012-09-05 00:20:33 +0000 | [diff] [blame] | 360 | return NULL; |
| 361 | } |
Christoph Lameter | 039363f | 2012-07-06 15:25:10 -0500 | [diff] [blame] | 362 | return s; |
Glauber Costa | 2633d7a | 2012-12-18 14:22:34 -0800 | [diff] [blame] | 363 | } |
Christoph Lameter | 039363f | 2012-07-06 15:25:10 -0500 | [diff] [blame] | 364 | EXPORT_SYMBOL(kmem_cache_create); |
Christoph Lameter | 97d0660 | 2012-07-06 15:25:11 -0500 | [diff] [blame] | 365 | |
Vladimir Davydov | 794b124 | 2014-04-07 15:39:26 -0700 | [diff] [blame] | 366 | #ifdef CONFIG_MEMCG_KMEM |
| 367 | /* |
Vladimir Davydov | 776ed0f | 2014-06-04 16:10:02 -0700 | [diff] [blame] | 368 | * memcg_create_kmem_cache - Create a cache for a memory cgroup. |
Vladimir Davydov | 794b124 | 2014-04-07 15:39:26 -0700 | [diff] [blame] | 369 | * @memcg: The memory cgroup the new cache is for. |
| 370 | * @root_cache: The parent of the new cache. |
Vladimir Davydov | 073ee1c | 2014-06-04 16:08:23 -0700 | [diff] [blame] | 371 | * @memcg_name: The name of the memory cgroup (used for naming the new cache). |
Vladimir Davydov | 794b124 | 2014-04-07 15:39:26 -0700 | [diff] [blame] | 372 | * |
| 373 | * This function attempts to create a kmem cache that will serve allocation |
| 374 | * requests going from @memcg to @root_cache. The new cache inherits properties |
| 375 | * from its parent. |
| 376 | */ |
Vladimir Davydov | 776ed0f | 2014-06-04 16:10:02 -0700 | [diff] [blame] | 377 | struct kmem_cache *memcg_create_kmem_cache(struct mem_cgroup *memcg, |
Vladimir Davydov | 073ee1c | 2014-06-04 16:08:23 -0700 | [diff] [blame] | 378 | struct kmem_cache *root_cache, |
| 379 | const char *memcg_name) |
Vladimir Davydov | 794b124 | 2014-04-07 15:39:26 -0700 | [diff] [blame] | 380 | { |
Vladimir Davydov | bd67314 | 2014-06-04 16:07:40 -0700 | [diff] [blame] | 381 | struct kmem_cache *s = NULL; |
Vladimir Davydov | 794b124 | 2014-04-07 15:39:26 -0700 | [diff] [blame] | 382 | char *cache_name; |
| 383 | |
| 384 | get_online_cpus(); |
Vladimir Davydov | 03afc0e | 2014-06-04 16:07:20 -0700 | [diff] [blame] | 385 | get_online_mems(); |
| 386 | |
Vladimir Davydov | 794b124 | 2014-04-07 15:39:26 -0700 | [diff] [blame] | 387 | mutex_lock(&slab_mutex); |
| 388 | |
Vladimir Davydov | 073ee1c | 2014-06-04 16:08:23 -0700 | [diff] [blame] | 389 | cache_name = kasprintf(GFP_KERNEL, "%s(%d:%s)", root_cache->name, |
| 390 | memcg_cache_id(memcg), memcg_name); |
Vladimir Davydov | 794b124 | 2014-04-07 15:39:26 -0700 | [diff] [blame] | 391 | if (!cache_name) |
| 392 | goto out_unlock; |
| 393 | |
| 394 | s = do_kmem_cache_create(cache_name, root_cache->object_size, |
| 395 | root_cache->size, root_cache->align, |
| 396 | root_cache->flags, root_cache->ctor, |
| 397 | memcg, root_cache); |
Vladimir Davydov | bd67314 | 2014-06-04 16:07:40 -0700 | [diff] [blame] | 398 | if (IS_ERR(s)) { |
Vladimir Davydov | 794b124 | 2014-04-07 15:39:26 -0700 | [diff] [blame] | 399 | kfree(cache_name); |
Vladimir Davydov | bd67314 | 2014-06-04 16:07:40 -0700 | [diff] [blame] | 400 | s = NULL; |
| 401 | } |
Vladimir Davydov | 794b124 | 2014-04-07 15:39:26 -0700 | [diff] [blame] | 402 | |
| 403 | out_unlock: |
| 404 | mutex_unlock(&slab_mutex); |
Vladimir Davydov | 03afc0e | 2014-06-04 16:07:20 -0700 | [diff] [blame] | 405 | |
| 406 | put_online_mems(); |
Vladimir Davydov | 794b124 | 2014-04-07 15:39:26 -0700 | [diff] [blame] | 407 | put_online_cpus(); |
Vladimir Davydov | bd67314 | 2014-06-04 16:07:40 -0700 | [diff] [blame] | 408 | |
| 409 | return s; |
Vladimir Davydov | 794b124 | 2014-04-07 15:39:26 -0700 | [diff] [blame] | 410 | } |
Vladimir Davydov | b852990 | 2014-04-07 15:39:28 -0700 | [diff] [blame] | 411 | |
Vladimir Davydov | 776ed0f | 2014-06-04 16:10:02 -0700 | [diff] [blame] | 412 | static int memcg_cleanup_cache_params(struct kmem_cache *s) |
Vladimir Davydov | b852990 | 2014-04-07 15:39:28 -0700 | [diff] [blame] | 413 | { |
| 414 | int rc; |
| 415 | |
| 416 | if (!s->memcg_params || |
| 417 | !s->memcg_params->is_root_cache) |
| 418 | return 0; |
| 419 | |
| 420 | mutex_unlock(&slab_mutex); |
Vladimir Davydov | 776ed0f | 2014-06-04 16:10:02 -0700 | [diff] [blame] | 421 | rc = __memcg_cleanup_cache_params(s); |
Vladimir Davydov | b852990 | 2014-04-07 15:39:28 -0700 | [diff] [blame] | 422 | mutex_lock(&slab_mutex); |
| 423 | |
| 424 | return rc; |
| 425 | } |
| 426 | #else |
Vladimir Davydov | 776ed0f | 2014-06-04 16:10:02 -0700 | [diff] [blame] | 427 | static int memcg_cleanup_cache_params(struct kmem_cache *s) |
Vladimir Davydov | b852990 | 2014-04-07 15:39:28 -0700 | [diff] [blame] | 428 | { |
| 429 | return 0; |
| 430 | } |
Vladimir Davydov | 794b124 | 2014-04-07 15:39:26 -0700 | [diff] [blame] | 431 | #endif /* CONFIG_MEMCG_KMEM */ |
| 432 | |
Christoph Lameter | 41a2128 | 2014-05-06 12:50:08 -0700 | [diff] [blame] | 433 | void slab_kmem_cache_release(struct kmem_cache *s) |
| 434 | { |
| 435 | kfree(s->name); |
| 436 | kmem_cache_free(kmem_cache, s); |
| 437 | } |
| 438 | |
Christoph Lameter | 945cf2b | 2012-09-04 23:18:33 +0000 | [diff] [blame] | 439 | void kmem_cache_destroy(struct kmem_cache *s) |
| 440 | { |
| 441 | get_online_cpus(); |
Vladimir Davydov | 03afc0e | 2014-06-04 16:07:20 -0700 | [diff] [blame] | 442 | get_online_mems(); |
| 443 | |
Christoph Lameter | 945cf2b | 2012-09-04 23:18:33 +0000 | [diff] [blame] | 444 | mutex_lock(&slab_mutex); |
Vladimir Davydov | b852990 | 2014-04-07 15:39:28 -0700 | [diff] [blame] | 445 | |
Christoph Lameter | 945cf2b | 2012-09-04 23:18:33 +0000 | [diff] [blame] | 446 | s->refcount--; |
Vladimir Davydov | b852990 | 2014-04-07 15:39:28 -0700 | [diff] [blame] | 447 | if (s->refcount) |
| 448 | goto out_unlock; |
Christoph Lameter | 945cf2b | 2012-09-04 23:18:33 +0000 | [diff] [blame] | 449 | |
Vladimir Davydov | 776ed0f | 2014-06-04 16:10:02 -0700 | [diff] [blame] | 450 | if (memcg_cleanup_cache_params(s) != 0) |
Vladimir Davydov | b852990 | 2014-04-07 15:39:28 -0700 | [diff] [blame] | 451 | goto out_unlock; |
Christoph Lameter | 945cf2b | 2012-09-04 23:18:33 +0000 | [diff] [blame] | 452 | |
Vladimir Davydov | b852990 | 2014-04-07 15:39:28 -0700 | [diff] [blame] | 453 | if (__kmem_cache_shutdown(s) != 0) { |
Vladimir Davydov | b852990 | 2014-04-07 15:39:28 -0700 | [diff] [blame] | 454 | printk(KERN_ERR "kmem_cache_destroy %s: " |
| 455 | "Slab cache still has objects\n", s->name); |
| 456 | dump_stack(); |
| 457 | goto out_unlock; |
Christoph Lameter | 945cf2b | 2012-09-04 23:18:33 +0000 | [diff] [blame] | 458 | } |
Vladimir Davydov | b852990 | 2014-04-07 15:39:28 -0700 | [diff] [blame] | 459 | |
Vladimir Davydov | 0bd62b1 | 2014-06-04 16:10:03 -0700 | [diff] [blame] | 460 | list_del(&s->list); |
| 461 | |
Vladimir Davydov | b852990 | 2014-04-07 15:39:28 -0700 | [diff] [blame] | 462 | mutex_unlock(&slab_mutex); |
| 463 | if (s->flags & SLAB_DESTROY_BY_RCU) |
| 464 | rcu_barrier(); |
| 465 | |
| 466 | memcg_free_cache_params(s); |
Christoph Lameter | 41a2128 | 2014-05-06 12:50:08 -0700 | [diff] [blame] | 467 | #ifdef SLAB_SUPPORTS_SYSFS |
| 468 | sysfs_slab_remove(s); |
| 469 | #else |
| 470 | slab_kmem_cache_release(s); |
| 471 | #endif |
Vladimir Davydov | 03afc0e | 2014-06-04 16:07:20 -0700 | [diff] [blame] | 472 | goto out; |
Vladimir Davydov | b852990 | 2014-04-07 15:39:28 -0700 | [diff] [blame] | 473 | |
| 474 | out_unlock: |
| 475 | mutex_unlock(&slab_mutex); |
Vladimir Davydov | 03afc0e | 2014-06-04 16:07:20 -0700 | [diff] [blame] | 476 | out: |
| 477 | put_online_mems(); |
Christoph Lameter | 945cf2b | 2012-09-04 23:18:33 +0000 | [diff] [blame] | 478 | put_online_cpus(); |
| 479 | } |
| 480 | EXPORT_SYMBOL(kmem_cache_destroy); |
| 481 | |
Vladimir Davydov | 03afc0e | 2014-06-04 16:07:20 -0700 | [diff] [blame] | 482 | /** |
| 483 | * kmem_cache_shrink - Shrink a cache. |
| 484 | * @cachep: The cache to shrink. |
| 485 | * |
| 486 | * Releases as many slabs as possible for a cache. |
| 487 | * To help debugging, a zero exit status indicates all slabs were released. |
| 488 | */ |
| 489 | int kmem_cache_shrink(struct kmem_cache *cachep) |
| 490 | { |
| 491 | int ret; |
| 492 | |
| 493 | get_online_cpus(); |
| 494 | get_online_mems(); |
| 495 | ret = __kmem_cache_shrink(cachep); |
| 496 | put_online_mems(); |
| 497 | put_online_cpus(); |
| 498 | return ret; |
| 499 | } |
| 500 | EXPORT_SYMBOL(kmem_cache_shrink); |
| 501 | |
Christoph Lameter | 97d0660 | 2012-07-06 15:25:11 -0500 | [diff] [blame] | 502 | int slab_is_available(void) |
| 503 | { |
| 504 | return slab_state >= UP; |
| 505 | } |
Glauber Costa | b7454ad | 2012-10-19 18:20:25 +0400 | [diff] [blame] | 506 | |
Christoph Lameter | 45530c4 | 2012-11-28 16:23:07 +0000 | [diff] [blame] | 507 | #ifndef CONFIG_SLOB |
| 508 | /* Create a cache during boot when no slab services are available yet */ |
| 509 | void __init create_boot_cache(struct kmem_cache *s, const char *name, size_t size, |
| 510 | unsigned long flags) |
| 511 | { |
| 512 | int err; |
| 513 | |
| 514 | s->name = name; |
| 515 | s->size = s->object_size = size; |
Christoph Lameter | 4590685 | 2012-11-28 16:23:16 +0000 | [diff] [blame] | 516 | s->align = calculate_alignment(flags, ARCH_KMALLOC_MINALIGN, size); |
Christoph Lameter | 45530c4 | 2012-11-28 16:23:07 +0000 | [diff] [blame] | 517 | err = __kmem_cache_create(s, flags); |
| 518 | |
| 519 | if (err) |
Christoph Lameter | 31ba734 | 2013-01-10 19:00:53 +0000 | [diff] [blame] | 520 | panic("Creation of kmalloc slab %s size=%zu failed. Reason %d\n", |
Christoph Lameter | 45530c4 | 2012-11-28 16:23:07 +0000 | [diff] [blame] | 521 | name, size, err); |
| 522 | |
| 523 | s->refcount = -1; /* Exempt from merging for now */ |
| 524 | } |
| 525 | |
| 526 | struct kmem_cache *__init create_kmalloc_cache(const char *name, size_t size, |
| 527 | unsigned long flags) |
| 528 | { |
| 529 | struct kmem_cache *s = kmem_cache_zalloc(kmem_cache, GFP_NOWAIT); |
| 530 | |
| 531 | if (!s) |
| 532 | panic("Out of memory when creating slab %s\n", name); |
| 533 | |
| 534 | create_boot_cache(s, name, size, flags); |
| 535 | list_add(&s->list, &slab_caches); |
| 536 | s->refcount = 1; |
| 537 | return s; |
| 538 | } |
| 539 | |
Christoph Lameter | 9425c58 | 2013-01-10 19:12:17 +0000 | [diff] [blame] | 540 | struct kmem_cache *kmalloc_caches[KMALLOC_SHIFT_HIGH + 1]; |
| 541 | EXPORT_SYMBOL(kmalloc_caches); |
| 542 | |
| 543 | #ifdef CONFIG_ZONE_DMA |
| 544 | struct kmem_cache *kmalloc_dma_caches[KMALLOC_SHIFT_HIGH + 1]; |
| 545 | EXPORT_SYMBOL(kmalloc_dma_caches); |
| 546 | #endif |
| 547 | |
Christoph Lameter | f97d5f6 | 2013-01-10 19:12:17 +0000 | [diff] [blame] | 548 | /* |
Christoph Lameter | 2c59dd6 | 2013-01-10 19:14:19 +0000 | [diff] [blame] | 549 | * Conversion table for small slabs sizes / 8 to the index in the |
| 550 | * kmalloc array. This is necessary for slabs < 192 since we have non power |
| 551 | * of two cache sizes there. The size of larger slabs can be determined using |
| 552 | * fls. |
| 553 | */ |
| 554 | static s8 size_index[24] = { |
| 555 | 3, /* 8 */ |
| 556 | 4, /* 16 */ |
| 557 | 5, /* 24 */ |
| 558 | 5, /* 32 */ |
| 559 | 6, /* 40 */ |
| 560 | 6, /* 48 */ |
| 561 | 6, /* 56 */ |
| 562 | 6, /* 64 */ |
| 563 | 1, /* 72 */ |
| 564 | 1, /* 80 */ |
| 565 | 1, /* 88 */ |
| 566 | 1, /* 96 */ |
| 567 | 7, /* 104 */ |
| 568 | 7, /* 112 */ |
| 569 | 7, /* 120 */ |
| 570 | 7, /* 128 */ |
| 571 | 2, /* 136 */ |
| 572 | 2, /* 144 */ |
| 573 | 2, /* 152 */ |
| 574 | 2, /* 160 */ |
| 575 | 2, /* 168 */ |
| 576 | 2, /* 176 */ |
| 577 | 2, /* 184 */ |
| 578 | 2 /* 192 */ |
| 579 | }; |
| 580 | |
| 581 | static inline int size_index_elem(size_t bytes) |
| 582 | { |
| 583 | return (bytes - 1) / 8; |
| 584 | } |
| 585 | |
| 586 | /* |
| 587 | * Find the kmem_cache structure that serves a given size of |
| 588 | * allocation |
| 589 | */ |
| 590 | struct kmem_cache *kmalloc_slab(size_t size, gfp_t flags) |
| 591 | { |
| 592 | int index; |
| 593 | |
Joonsoo Kim | 9de1bc8 | 2013-08-02 11:02:42 +0900 | [diff] [blame] | 594 | if (unlikely(size > KMALLOC_MAX_SIZE)) { |
Sasha Levin | 907985f | 2013-06-10 15:18:00 -0400 | [diff] [blame] | 595 | WARN_ON_ONCE(!(flags & __GFP_NOWARN)); |
Christoph Lameter | 6286ae9 | 2013-05-03 15:43:18 +0000 | [diff] [blame] | 596 | return NULL; |
Sasha Levin | 907985f | 2013-06-10 15:18:00 -0400 | [diff] [blame] | 597 | } |
Christoph Lameter | 6286ae9 | 2013-05-03 15:43:18 +0000 | [diff] [blame] | 598 | |
Christoph Lameter | 2c59dd6 | 2013-01-10 19:14:19 +0000 | [diff] [blame] | 599 | if (size <= 192) { |
| 600 | if (!size) |
| 601 | return ZERO_SIZE_PTR; |
| 602 | |
| 603 | index = size_index[size_index_elem(size)]; |
| 604 | } else |
| 605 | index = fls(size - 1); |
| 606 | |
| 607 | #ifdef CONFIG_ZONE_DMA |
Joonsoo Kim | b1e0541 | 2013-02-04 23:46:46 +0900 | [diff] [blame] | 608 | if (unlikely((flags & GFP_DMA))) |
Christoph Lameter | 2c59dd6 | 2013-01-10 19:14:19 +0000 | [diff] [blame] | 609 | return kmalloc_dma_caches[index]; |
| 610 | |
| 611 | #endif |
| 612 | return kmalloc_caches[index]; |
| 613 | } |
| 614 | |
| 615 | /* |
Christoph Lameter | f97d5f6 | 2013-01-10 19:12:17 +0000 | [diff] [blame] | 616 | * Create the kmalloc array. Some of the regular kmalloc arrays |
| 617 | * may already have been created because they were needed to |
| 618 | * enable allocations for slab creation. |
| 619 | */ |
| 620 | void __init create_kmalloc_caches(unsigned long flags) |
| 621 | { |
| 622 | int i; |
| 623 | |
Christoph Lameter | 2c59dd6 | 2013-01-10 19:14:19 +0000 | [diff] [blame] | 624 | /* |
| 625 | * Patch up the size_index table if we have strange large alignment |
| 626 | * requirements for the kmalloc array. This is only the case for |
| 627 | * MIPS it seems. The standard arches will not generate any code here. |
| 628 | * |
| 629 | * Largest permitted alignment is 256 bytes due to the way we |
| 630 | * handle the index determination for the smaller caches. |
| 631 | * |
| 632 | * Make sure that nothing crazy happens if someone starts tinkering |
| 633 | * around with ARCH_KMALLOC_MINALIGN |
| 634 | */ |
| 635 | BUILD_BUG_ON(KMALLOC_MIN_SIZE > 256 || |
| 636 | (KMALLOC_MIN_SIZE & (KMALLOC_MIN_SIZE - 1))); |
| 637 | |
| 638 | for (i = 8; i < KMALLOC_MIN_SIZE; i += 8) { |
| 639 | int elem = size_index_elem(i); |
| 640 | |
| 641 | if (elem >= ARRAY_SIZE(size_index)) |
| 642 | break; |
| 643 | size_index[elem] = KMALLOC_SHIFT_LOW; |
| 644 | } |
| 645 | |
| 646 | if (KMALLOC_MIN_SIZE >= 64) { |
| 647 | /* |
| 648 | * The 96 byte size cache is not used if the alignment |
| 649 | * is 64 byte. |
| 650 | */ |
| 651 | for (i = 64 + 8; i <= 96; i += 8) |
| 652 | size_index[size_index_elem(i)] = 7; |
| 653 | |
| 654 | } |
| 655 | |
| 656 | if (KMALLOC_MIN_SIZE >= 128) { |
| 657 | /* |
| 658 | * The 192 byte sized cache is not used if the alignment |
| 659 | * is 128 byte. Redirect kmalloc to use the 256 byte cache |
| 660 | * instead. |
| 661 | */ |
| 662 | for (i = 128 + 8; i <= 192; i += 8) |
| 663 | size_index[size_index_elem(i)] = 8; |
| 664 | } |
Christoph Lameter | 8a965b3 | 2013-05-03 18:04:18 +0000 | [diff] [blame] | 665 | for (i = KMALLOC_SHIFT_LOW; i <= KMALLOC_SHIFT_HIGH; i++) { |
| 666 | if (!kmalloc_caches[i]) { |
Christoph Lameter | f97d5f6 | 2013-01-10 19:12:17 +0000 | [diff] [blame] | 667 | kmalloc_caches[i] = create_kmalloc_cache(NULL, |
| 668 | 1 << i, flags); |
Christoph Lameter | 8a965b3 | 2013-05-03 18:04:18 +0000 | [diff] [blame] | 669 | } |
Chris Mason | 956e46e | 2013-05-08 15:56:28 -0400 | [diff] [blame] | 670 | |
| 671 | /* |
| 672 | * Caches that are not of the two-to-the-power-of size. |
| 673 | * These have to be created immediately after the |
| 674 | * earlier power of two caches |
| 675 | */ |
| 676 | if (KMALLOC_MIN_SIZE <= 32 && !kmalloc_caches[1] && i == 6) |
| 677 | kmalloc_caches[1] = create_kmalloc_cache(NULL, 96, flags); |
| 678 | |
| 679 | if (KMALLOC_MIN_SIZE <= 64 && !kmalloc_caches[2] && i == 7) |
| 680 | kmalloc_caches[2] = create_kmalloc_cache(NULL, 192, flags); |
Christoph Lameter | 8a965b3 | 2013-05-03 18:04:18 +0000 | [diff] [blame] | 681 | } |
| 682 | |
Christoph Lameter | f97d5f6 | 2013-01-10 19:12:17 +0000 | [diff] [blame] | 683 | /* Kmalloc array is now usable */ |
| 684 | slab_state = UP; |
| 685 | |
| 686 | for (i = 0; i <= KMALLOC_SHIFT_HIGH; i++) { |
| 687 | struct kmem_cache *s = kmalloc_caches[i]; |
| 688 | char *n; |
| 689 | |
| 690 | if (s) { |
| 691 | n = kasprintf(GFP_NOWAIT, "kmalloc-%d", kmalloc_size(i)); |
| 692 | |
| 693 | BUG_ON(!n); |
| 694 | s->name = n; |
| 695 | } |
| 696 | } |
| 697 | |
| 698 | #ifdef CONFIG_ZONE_DMA |
| 699 | for (i = 0; i <= KMALLOC_SHIFT_HIGH; i++) { |
| 700 | struct kmem_cache *s = kmalloc_caches[i]; |
| 701 | |
| 702 | if (s) { |
| 703 | int size = kmalloc_size(i); |
| 704 | char *n = kasprintf(GFP_NOWAIT, |
| 705 | "dma-kmalloc-%d", size); |
| 706 | |
| 707 | BUG_ON(!n); |
| 708 | kmalloc_dma_caches[i] = create_kmalloc_cache(n, |
| 709 | size, SLAB_CACHE_DMA | flags); |
| 710 | } |
| 711 | } |
| 712 | #endif |
| 713 | } |
Christoph Lameter | 45530c4 | 2012-11-28 16:23:07 +0000 | [diff] [blame] | 714 | #endif /* !CONFIG_SLOB */ |
| 715 | |
Vladimir Davydov | cea371f | 2014-06-04 16:07:04 -0700 | [diff] [blame] | 716 | /* |
| 717 | * To avoid unnecessary overhead, we pass through large allocation requests |
| 718 | * directly to the page allocator. We use __GFP_COMP, because we will need to |
| 719 | * know the allocation order to free the pages properly in kfree. |
| 720 | */ |
Vladimir Davydov | 5238343 | 2014-06-04 16:06:39 -0700 | [diff] [blame] | 721 | void *kmalloc_order(size_t size, gfp_t flags, unsigned int order) |
| 722 | { |
| 723 | void *ret; |
| 724 | struct page *page; |
| 725 | |
| 726 | flags |= __GFP_COMP; |
| 727 | page = alloc_kmem_pages(flags, order); |
| 728 | ret = page ? page_address(page) : NULL; |
| 729 | kmemleak_alloc(ret, size, 1, flags); |
| 730 | return ret; |
| 731 | } |
| 732 | EXPORT_SYMBOL(kmalloc_order); |
| 733 | |
Christoph Lameter | f1b6eb6 | 2013-09-04 16:35:34 +0000 | [diff] [blame] | 734 | #ifdef CONFIG_TRACING |
| 735 | void *kmalloc_order_trace(size_t size, gfp_t flags, unsigned int order) |
| 736 | { |
| 737 | void *ret = kmalloc_order(size, flags, order); |
| 738 | trace_kmalloc(_RET_IP_, ret, size, PAGE_SIZE << order, flags); |
| 739 | return ret; |
| 740 | } |
| 741 | EXPORT_SYMBOL(kmalloc_order_trace); |
| 742 | #endif |
Christoph Lameter | 45530c4 | 2012-11-28 16:23:07 +0000 | [diff] [blame] | 743 | |
Glauber Costa | b7454ad | 2012-10-19 18:20:25 +0400 | [diff] [blame] | 744 | #ifdef CONFIG_SLABINFO |
Wanpeng Li | e9b4db2 | 2013-07-04 08:33:24 +0800 | [diff] [blame] | 745 | |
| 746 | #ifdef CONFIG_SLAB |
| 747 | #define SLABINFO_RIGHTS (S_IWUSR | S_IRUSR) |
| 748 | #else |
| 749 | #define SLABINFO_RIGHTS S_IRUSR |
| 750 | #endif |
| 751 | |
Glauber Costa | 749c541 | 2012-12-18 14:23:01 -0800 | [diff] [blame] | 752 | void print_slabinfo_header(struct seq_file *m) |
Glauber Costa | bcee6e2 | 2012-10-19 18:20:26 +0400 | [diff] [blame] | 753 | { |
| 754 | /* |
| 755 | * Output format version, so at least we can change it |
| 756 | * without _too_ many complaints. |
| 757 | */ |
| 758 | #ifdef CONFIG_DEBUG_SLAB |
| 759 | seq_puts(m, "slabinfo - version: 2.1 (statistics)\n"); |
| 760 | #else |
| 761 | seq_puts(m, "slabinfo - version: 2.1\n"); |
| 762 | #endif |
| 763 | seq_puts(m, "# name <active_objs> <num_objs> <objsize> " |
| 764 | "<objperslab> <pagesperslab>"); |
| 765 | seq_puts(m, " : tunables <limit> <batchcount> <sharedfactor>"); |
| 766 | seq_puts(m, " : slabdata <active_slabs> <num_slabs> <sharedavail>"); |
| 767 | #ifdef CONFIG_DEBUG_SLAB |
| 768 | seq_puts(m, " : globalstat <listallocs> <maxobjs> <grown> <reaped> " |
| 769 | "<error> <maxfreeable> <nodeallocs> <remotefrees> <alienoverflow>"); |
| 770 | seq_puts(m, " : cpustat <allochit> <allocmiss> <freehit> <freemiss>"); |
| 771 | #endif |
| 772 | seq_putc(m, '\n'); |
| 773 | } |
| 774 | |
Glauber Costa | b7454ad | 2012-10-19 18:20:25 +0400 | [diff] [blame] | 775 | static void *s_start(struct seq_file *m, loff_t *pos) |
| 776 | { |
| 777 | loff_t n = *pos; |
| 778 | |
| 779 | mutex_lock(&slab_mutex); |
| 780 | if (!n) |
| 781 | print_slabinfo_header(m); |
| 782 | |
| 783 | return seq_list_start(&slab_caches, *pos); |
| 784 | } |
| 785 | |
Wanpeng Li | 276a243 | 2013-07-08 08:08:28 +0800 | [diff] [blame] | 786 | void *slab_next(struct seq_file *m, void *p, loff_t *pos) |
Glauber Costa | b7454ad | 2012-10-19 18:20:25 +0400 | [diff] [blame] | 787 | { |
| 788 | return seq_list_next(p, &slab_caches, pos); |
| 789 | } |
| 790 | |
Wanpeng Li | 276a243 | 2013-07-08 08:08:28 +0800 | [diff] [blame] | 791 | void slab_stop(struct seq_file *m, void *p) |
Glauber Costa | b7454ad | 2012-10-19 18:20:25 +0400 | [diff] [blame] | 792 | { |
| 793 | mutex_unlock(&slab_mutex); |
| 794 | } |
| 795 | |
Glauber Costa | 749c541 | 2012-12-18 14:23:01 -0800 | [diff] [blame] | 796 | static void |
| 797 | memcg_accumulate_slabinfo(struct kmem_cache *s, struct slabinfo *info) |
Glauber Costa | b7454ad | 2012-10-19 18:20:25 +0400 | [diff] [blame] | 798 | { |
Glauber Costa | 749c541 | 2012-12-18 14:23:01 -0800 | [diff] [blame] | 799 | struct kmem_cache *c; |
| 800 | struct slabinfo sinfo; |
| 801 | int i; |
| 802 | |
| 803 | if (!is_root_cache(s)) |
| 804 | return; |
| 805 | |
| 806 | for_each_memcg_cache_index(i) { |
Qiang Huang | 2ade4de | 2013-11-12 15:08:23 -0800 | [diff] [blame] | 807 | c = cache_from_memcg_idx(s, i); |
Glauber Costa | 749c541 | 2012-12-18 14:23:01 -0800 | [diff] [blame] | 808 | if (!c) |
| 809 | continue; |
| 810 | |
| 811 | memset(&sinfo, 0, sizeof(sinfo)); |
| 812 | get_slabinfo(c, &sinfo); |
| 813 | |
| 814 | info->active_slabs += sinfo.active_slabs; |
| 815 | info->num_slabs += sinfo.num_slabs; |
| 816 | info->shared_avail += sinfo.shared_avail; |
| 817 | info->active_objs += sinfo.active_objs; |
| 818 | info->num_objs += sinfo.num_objs; |
| 819 | } |
| 820 | } |
| 821 | |
| 822 | int cache_show(struct kmem_cache *s, struct seq_file *m) |
| 823 | { |
Glauber Costa | 0d7561c | 2012-10-19 18:20:27 +0400 | [diff] [blame] | 824 | struct slabinfo sinfo; |
| 825 | |
| 826 | memset(&sinfo, 0, sizeof(sinfo)); |
| 827 | get_slabinfo(s, &sinfo); |
| 828 | |
Glauber Costa | 749c541 | 2012-12-18 14:23:01 -0800 | [diff] [blame] | 829 | memcg_accumulate_slabinfo(s, &sinfo); |
| 830 | |
Glauber Costa | 0d7561c | 2012-10-19 18:20:27 +0400 | [diff] [blame] | 831 | seq_printf(m, "%-17s %6lu %6lu %6u %4u %4d", |
Glauber Costa | 749c541 | 2012-12-18 14:23:01 -0800 | [diff] [blame] | 832 | cache_name(s), sinfo.active_objs, sinfo.num_objs, s->size, |
Glauber Costa | 0d7561c | 2012-10-19 18:20:27 +0400 | [diff] [blame] | 833 | sinfo.objects_per_slab, (1 << sinfo.cache_order)); |
| 834 | |
| 835 | seq_printf(m, " : tunables %4u %4u %4u", |
| 836 | sinfo.limit, sinfo.batchcount, sinfo.shared); |
| 837 | seq_printf(m, " : slabdata %6lu %6lu %6lu", |
| 838 | sinfo.active_slabs, sinfo.num_slabs, sinfo.shared_avail); |
| 839 | slabinfo_show_stats(m, s); |
| 840 | seq_putc(m, '\n'); |
| 841 | return 0; |
Glauber Costa | b7454ad | 2012-10-19 18:20:25 +0400 | [diff] [blame] | 842 | } |
| 843 | |
Glauber Costa | 749c541 | 2012-12-18 14:23:01 -0800 | [diff] [blame] | 844 | static int s_show(struct seq_file *m, void *p) |
| 845 | { |
| 846 | struct kmem_cache *s = list_entry(p, struct kmem_cache, list); |
| 847 | |
| 848 | if (!is_root_cache(s)) |
| 849 | return 0; |
| 850 | return cache_show(s, m); |
| 851 | } |
| 852 | |
Glauber Costa | b7454ad | 2012-10-19 18:20:25 +0400 | [diff] [blame] | 853 | /* |
| 854 | * slabinfo_op - iterator that generates /proc/slabinfo |
| 855 | * |
| 856 | * Output layout: |
| 857 | * cache-name |
| 858 | * num-active-objs |
| 859 | * total-objs |
| 860 | * object size |
| 861 | * num-active-slabs |
| 862 | * total-slabs |
| 863 | * num-pages-per-slab |
| 864 | * + further values on SMP and with statistics enabled |
| 865 | */ |
| 866 | static const struct seq_operations slabinfo_op = { |
| 867 | .start = s_start, |
Wanpeng Li | 276a243 | 2013-07-08 08:08:28 +0800 | [diff] [blame] | 868 | .next = slab_next, |
| 869 | .stop = slab_stop, |
Glauber Costa | b7454ad | 2012-10-19 18:20:25 +0400 | [diff] [blame] | 870 | .show = s_show, |
| 871 | }; |
| 872 | |
| 873 | static int slabinfo_open(struct inode *inode, struct file *file) |
| 874 | { |
| 875 | return seq_open(file, &slabinfo_op); |
| 876 | } |
| 877 | |
| 878 | static const struct file_operations proc_slabinfo_operations = { |
| 879 | .open = slabinfo_open, |
| 880 | .read = seq_read, |
| 881 | .write = slabinfo_write, |
| 882 | .llseek = seq_lseek, |
| 883 | .release = seq_release, |
| 884 | }; |
| 885 | |
| 886 | static int __init slab_proc_init(void) |
| 887 | { |
Wanpeng Li | e9b4db2 | 2013-07-04 08:33:24 +0800 | [diff] [blame] | 888 | proc_create("slabinfo", SLABINFO_RIGHTS, NULL, |
| 889 | &proc_slabinfo_operations); |
Glauber Costa | b7454ad | 2012-10-19 18:20:25 +0400 | [diff] [blame] | 890 | return 0; |
| 891 | } |
| 892 | module_init(slab_proc_init); |
| 893 | #endif /* CONFIG_SLABINFO */ |
Andrey Ryabinin | 928cec9 | 2014-08-06 16:04:44 -0700 | [diff] [blame] | 894 | |
| 895 | static __always_inline void *__do_krealloc(const void *p, size_t new_size, |
| 896 | gfp_t flags) |
| 897 | { |
| 898 | void *ret; |
| 899 | size_t ks = 0; |
| 900 | |
| 901 | if (p) |
| 902 | ks = ksize(p); |
| 903 | |
| 904 | if (ks >= new_size) |
| 905 | return (void *)p; |
| 906 | |
| 907 | ret = kmalloc_track_caller(new_size, flags); |
| 908 | if (ret && p) |
| 909 | memcpy(ret, p, ks); |
| 910 | |
| 911 | return ret; |
| 912 | } |
| 913 | |
| 914 | /** |
| 915 | * __krealloc - like krealloc() but don't free @p. |
| 916 | * @p: object to reallocate memory for. |
| 917 | * @new_size: how many bytes of memory are required. |
| 918 | * @flags: the type of memory to allocate. |
| 919 | * |
| 920 | * This function is like krealloc() except it never frees the originally |
| 921 | * allocated buffer. Use this if you don't want to free the buffer immediately |
| 922 | * like, for example, with RCU. |
| 923 | */ |
| 924 | void *__krealloc(const void *p, size_t new_size, gfp_t flags) |
| 925 | { |
| 926 | if (unlikely(!new_size)) |
| 927 | return ZERO_SIZE_PTR; |
| 928 | |
| 929 | return __do_krealloc(p, new_size, flags); |
| 930 | |
| 931 | } |
| 932 | EXPORT_SYMBOL(__krealloc); |
| 933 | |
| 934 | /** |
| 935 | * krealloc - reallocate memory. The contents will remain unchanged. |
| 936 | * @p: object to reallocate memory for. |
| 937 | * @new_size: how many bytes of memory are required. |
| 938 | * @flags: the type of memory to allocate. |
| 939 | * |
| 940 | * The contents of the object pointed to are preserved up to the |
| 941 | * lesser of the new and old sizes. If @p is %NULL, krealloc() |
| 942 | * behaves exactly like kmalloc(). If @new_size is 0 and @p is not a |
| 943 | * %NULL pointer, the object pointed to is freed. |
| 944 | */ |
| 945 | void *krealloc(const void *p, size_t new_size, gfp_t flags) |
| 946 | { |
| 947 | void *ret; |
| 948 | |
| 949 | if (unlikely(!new_size)) { |
| 950 | kfree(p); |
| 951 | return ZERO_SIZE_PTR; |
| 952 | } |
| 953 | |
| 954 | ret = __do_krealloc(p, new_size, flags); |
| 955 | if (ret && p != ret) |
| 956 | kfree(p); |
| 957 | |
| 958 | return ret; |
| 959 | } |
| 960 | EXPORT_SYMBOL(krealloc); |
| 961 | |
| 962 | /** |
| 963 | * kzfree - like kfree but zero memory |
| 964 | * @p: object to free memory of |
| 965 | * |
| 966 | * The memory of the object @p points to is zeroed before freed. |
| 967 | * If @p is %NULL, kzfree() does nothing. |
| 968 | * |
| 969 | * Note: this function zeroes the whole allocated buffer which can be a good |
| 970 | * deal bigger than the requested buffer size passed to kmalloc(). So be |
| 971 | * careful when using this function in performance sensitive code. |
| 972 | */ |
| 973 | void kzfree(const void *p) |
| 974 | { |
| 975 | size_t ks; |
| 976 | void *mem = (void *)p; |
| 977 | |
| 978 | if (unlikely(ZERO_OR_NULL_PTR(mem))) |
| 979 | return; |
| 980 | ks = ksize(mem); |
| 981 | memset(mem, 0, ks); |
| 982 | kfree(mem); |
| 983 | } |
| 984 | EXPORT_SYMBOL(kzfree); |
| 985 | |
| 986 | /* Tracepoints definitions. */ |
| 987 | EXPORT_TRACEPOINT_SYMBOL(kmalloc); |
| 988 | EXPORT_TRACEPOINT_SYMBOL(kmem_cache_alloc); |
| 989 | EXPORT_TRACEPOINT_SYMBOL(kmalloc_node); |
| 990 | EXPORT_TRACEPOINT_SYMBOL(kmem_cache_alloc_node); |
| 991 | EXPORT_TRACEPOINT_SYMBOL(kfree); |
| 992 | EXPORT_TRACEPOINT_SYMBOL(kmem_cache_free); |