blob: f206cb10a544a64ed33606fbe6cae26309db87ad [file] [log] [blame]
Christoph Lameter039363f2012-07-06 15:25:10 -05001/*
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 Lameter20cea962012-07-06 15:25:13 -050014#include <linux/cpu.h>
15#include <linux/uaccess.h>
Glauber Costab7454ad2012-10-19 18:20:25 +040016#include <linux/seq_file.h>
17#include <linux/proc_fs.h>
Christoph Lameter039363f2012-07-06 15:25:10 -050018#include <asm/cacheflush.h>
19#include <asm/tlbflush.h>
20#include <asm/page.h>
Glauber Costa2633d7a2012-12-18 14:22:34 -080021#include <linux/memcontrol.h>
Andrey Ryabinin928cec92014-08-06 16:04:44 -070022
23#define CREATE_TRACE_POINTS
Christoph Lameterf1b6eb62013-09-04 16:35:34 +000024#include <trace/events/kmem.h>
Christoph Lameter039363f2012-07-06 15:25:10 -050025
Christoph Lameter97d06602012-07-06 15:25:11 -050026#include "slab.h"
27
28enum slab_state slab_state;
Christoph Lameter18004c52012-07-06 15:25:12 -050029LIST_HEAD(slab_caches);
30DEFINE_MUTEX(slab_mutex);
Christoph Lameter9b030cb2012-09-05 00:20:33 +000031struct kmem_cache *kmem_cache;
Christoph Lameter97d06602012-07-06 15:25:11 -050032
Joonsoo Kim07f361b2014-10-09 15:26:00 -070033/*
Joonsoo Kim423c9292014-10-09 15:26:22 -070034 * 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 */
47static int slab_nomerge;
48
49static 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 Kim07f361b2014-10-09 15:26:00 -070062 * Determine the size of a slab object
63 */
64unsigned int kmem_cache_size(struct kmem_cache *s)
65{
66 return s->object_size;
67}
68EXPORT_SYMBOL(kmem_cache_size);
69
Shuah Khan77be4b12012-08-16 00:09:46 -070070#ifdef CONFIG_DEBUG_VM
Vladimir Davydov794b1242014-04-07 15:39:26 -070071static int kmem_cache_sanity_check(const char *name, size_t size)
Shuah Khan77be4b12012-08-16 00:09:46 -070072{
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 Patocka69461742014-03-04 17:13:47 -050097#if !defined(CONFIG_SLUB)
Vladimir Davydov794b1242014-04-07 15:39:26 -070098 if (!strcmp(s->name, name)) {
Shuah Khan77be4b12012-08-16 00:09:46 -070099 pr_err("%s (%s): Cache name already exists.\n",
100 __func__, name);
101 dump_stack();
102 s = NULL;
103 return -EINVAL;
104 }
Christoph Lameter3e374912013-09-21 21:56:34 +0000105#endif
Shuah Khan77be4b12012-08-16 00:09:46 -0700106 }
107
108 WARN_ON(strchr(name, ' ')); /* It confuses parsers */
109 return 0;
110}
111#else
Vladimir Davydov794b1242014-04-07 15:39:26 -0700112static inline int kmem_cache_sanity_check(const char *name, size_t size)
Shuah Khan77be4b12012-08-16 00:09:46 -0700113{
114 return 0;
115}
116#endif
117
Glauber Costa55007d82012-12-18 14:22:38 -0800118#ifdef CONFIG_MEMCG_KMEM
119int 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);
140out:
141 mutex_unlock(&slab_mutex);
142 return ret;
143}
144#endif
145
Christoph Lameter039363f2012-07-06 15:25:10 -0500146/*
Joonsoo Kim423c9292014-10-09 15:26:22 -0700147 * Find a mergeable slab cache
148 */
149int 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
169struct 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 Lameter45906852012-11-28 16:23:16 +0000210 * 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 */
213unsigned 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 Davydov794b1242014-04-07 15:39:26 -0700236static struct kmem_cache *
237do_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 Davydov794b1242014-04-07 15:39:26 -0700265out:
266 if (err)
267 return ERR_PTR(err);
268 return s;
269
270out_free_cache:
271 memcg_free_cache_params(s);
272 kfree(s);
273 goto out;
274}
Christoph Lameter45906852012-11-28 16:23:16 +0000275
276/*
Christoph Lameter039363f2012-07-06 15:25:10 -0500277 * 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 Costa2633d7a2012-12-18 14:22:34 -0800300struct kmem_cache *
Vladimir Davydov794b1242014-04-07 15:39:26 -0700301kmem_cache_create(const char *name, size_t size, size_t align,
302 unsigned long flags, void (*ctor)(void *))
Christoph Lameter039363f2012-07-06 15:25:10 -0500303{
Vladimir Davydov794b1242014-04-07 15:39:26 -0700304 struct kmem_cache *s;
305 char *cache_name;
Vladimir Davydov3965fc32014-01-23 15:52:55 -0800306 int err;
Christoph Lameter039363f2012-07-06 15:25:10 -0500307
Pekka Enbergb9205362012-08-16 10:12:18 +0300308 get_online_cpus();
Vladimir Davydov03afc0e2014-06-04 16:07:20 -0700309 get_online_mems();
310
Pekka Enbergb9205362012-08-16 10:12:18 +0300311 mutex_lock(&slab_mutex);
Christoph Lameter686d5502012-09-05 00:20:33 +0000312
Vladimir Davydov794b1242014-04-07 15:39:26 -0700313 err = kmem_cache_sanity_check(name, size);
Andrew Morton3aa24f52014-10-09 15:25:58 -0700314 if (err) {
315 s = NULL; /* suppress uninit var warning */
Vladimir Davydov3965fc32014-01-23 15:52:55 -0800316 goto out_unlock;
Andrew Morton3aa24f52014-10-09 15:25:58 -0700317 }
Christoph Lameter686d5502012-09-05 00:20:33 +0000318
Glauber Costad8843922012-10-17 15:36:51 +0400319 /*
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 Lameter686d5502012-09-05 00:20:33 +0000326
Vladimir Davydov794b1242014-04-07 15:39:26 -0700327 s = __kmem_cache_alias(name, size, align, flags, ctor);
328 if (s)
Vladimir Davydov3965fc32014-01-23 15:52:55 -0800329 goto out_unlock;
Glauber Costa2633d7a2012-12-18 14:22:34 -0800330
Vladimir Davydov794b1242014-04-07 15:39:26 -0700331 cache_name = kstrdup(name, GFP_KERNEL);
332 if (!cache_name) {
333 err = -ENOMEM;
334 goto out_unlock;
335 }
Glauber Costa2633d7a2012-12-18 14:22:34 -0800336
Vladimir Davydov794b1242014-04-07 15:39:26 -0700337 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 Davydov3965fc32014-01-23 15:52:55 -0800344
345out_unlock:
Christoph Lameter20cea962012-07-06 15:25:13 -0500346 mutex_unlock(&slab_mutex);
Vladimir Davydov03afc0e2014-06-04 16:07:20 -0700347
348 put_online_mems();
Christoph Lameter20cea962012-07-06 15:25:13 -0500349 put_online_cpus();
350
Dave Jonesba3253c2014-01-29 14:05:48 -0800351 if (err) {
Christoph Lameter686d5502012-09-05 00:20:33 +0000352 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 Lameter686d5502012-09-05 00:20:33 +0000360 return NULL;
361 }
Christoph Lameter039363f2012-07-06 15:25:10 -0500362 return s;
Glauber Costa2633d7a2012-12-18 14:22:34 -0800363}
Christoph Lameter039363f2012-07-06 15:25:10 -0500364EXPORT_SYMBOL(kmem_cache_create);
Christoph Lameter97d06602012-07-06 15:25:11 -0500365
Vladimir Davydov794b1242014-04-07 15:39:26 -0700366#ifdef CONFIG_MEMCG_KMEM
367/*
Vladimir Davydov776ed0f2014-06-04 16:10:02 -0700368 * memcg_create_kmem_cache - Create a cache for a memory cgroup.
Vladimir Davydov794b1242014-04-07 15:39:26 -0700369 * @memcg: The memory cgroup the new cache is for.
370 * @root_cache: The parent of the new cache.
Vladimir Davydov073ee1c2014-06-04 16:08:23 -0700371 * @memcg_name: The name of the memory cgroup (used for naming the new cache).
Vladimir Davydov794b1242014-04-07 15:39:26 -0700372 *
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 Davydov776ed0f2014-06-04 16:10:02 -0700377struct kmem_cache *memcg_create_kmem_cache(struct mem_cgroup *memcg,
Vladimir Davydov073ee1c2014-06-04 16:08:23 -0700378 struct kmem_cache *root_cache,
379 const char *memcg_name)
Vladimir Davydov794b1242014-04-07 15:39:26 -0700380{
Vladimir Davydovbd673142014-06-04 16:07:40 -0700381 struct kmem_cache *s = NULL;
Vladimir Davydov794b1242014-04-07 15:39:26 -0700382 char *cache_name;
383
384 get_online_cpus();
Vladimir Davydov03afc0e2014-06-04 16:07:20 -0700385 get_online_mems();
386
Vladimir Davydov794b1242014-04-07 15:39:26 -0700387 mutex_lock(&slab_mutex);
388
Vladimir Davydov073ee1c2014-06-04 16:08:23 -0700389 cache_name = kasprintf(GFP_KERNEL, "%s(%d:%s)", root_cache->name,
390 memcg_cache_id(memcg), memcg_name);
Vladimir Davydov794b1242014-04-07 15:39:26 -0700391 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 Davydovbd673142014-06-04 16:07:40 -0700398 if (IS_ERR(s)) {
Vladimir Davydov794b1242014-04-07 15:39:26 -0700399 kfree(cache_name);
Vladimir Davydovbd673142014-06-04 16:07:40 -0700400 s = NULL;
401 }
Vladimir Davydov794b1242014-04-07 15:39:26 -0700402
403out_unlock:
404 mutex_unlock(&slab_mutex);
Vladimir Davydov03afc0e2014-06-04 16:07:20 -0700405
406 put_online_mems();
Vladimir Davydov794b1242014-04-07 15:39:26 -0700407 put_online_cpus();
Vladimir Davydovbd673142014-06-04 16:07:40 -0700408
409 return s;
Vladimir Davydov794b1242014-04-07 15:39:26 -0700410}
Vladimir Davydovb8529902014-04-07 15:39:28 -0700411
Vladimir Davydov776ed0f2014-06-04 16:10:02 -0700412static int memcg_cleanup_cache_params(struct kmem_cache *s)
Vladimir Davydovb8529902014-04-07 15:39:28 -0700413{
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 Davydov776ed0f2014-06-04 16:10:02 -0700421 rc = __memcg_cleanup_cache_params(s);
Vladimir Davydovb8529902014-04-07 15:39:28 -0700422 mutex_lock(&slab_mutex);
423
424 return rc;
425}
426#else
Vladimir Davydov776ed0f2014-06-04 16:10:02 -0700427static int memcg_cleanup_cache_params(struct kmem_cache *s)
Vladimir Davydovb8529902014-04-07 15:39:28 -0700428{
429 return 0;
430}
Vladimir Davydov794b1242014-04-07 15:39:26 -0700431#endif /* CONFIG_MEMCG_KMEM */
432
Christoph Lameter41a21282014-05-06 12:50:08 -0700433void slab_kmem_cache_release(struct kmem_cache *s)
434{
435 kfree(s->name);
436 kmem_cache_free(kmem_cache, s);
437}
438
Christoph Lameter945cf2b2012-09-04 23:18:33 +0000439void kmem_cache_destroy(struct kmem_cache *s)
440{
441 get_online_cpus();
Vladimir Davydov03afc0e2014-06-04 16:07:20 -0700442 get_online_mems();
443
Christoph Lameter945cf2b2012-09-04 23:18:33 +0000444 mutex_lock(&slab_mutex);
Vladimir Davydovb8529902014-04-07 15:39:28 -0700445
Christoph Lameter945cf2b2012-09-04 23:18:33 +0000446 s->refcount--;
Vladimir Davydovb8529902014-04-07 15:39:28 -0700447 if (s->refcount)
448 goto out_unlock;
Christoph Lameter945cf2b2012-09-04 23:18:33 +0000449
Vladimir Davydov776ed0f2014-06-04 16:10:02 -0700450 if (memcg_cleanup_cache_params(s) != 0)
Vladimir Davydovb8529902014-04-07 15:39:28 -0700451 goto out_unlock;
Christoph Lameter945cf2b2012-09-04 23:18:33 +0000452
Vladimir Davydovb8529902014-04-07 15:39:28 -0700453 if (__kmem_cache_shutdown(s) != 0) {
Vladimir Davydovb8529902014-04-07 15:39:28 -0700454 printk(KERN_ERR "kmem_cache_destroy %s: "
455 "Slab cache still has objects\n", s->name);
456 dump_stack();
457 goto out_unlock;
Christoph Lameter945cf2b2012-09-04 23:18:33 +0000458 }
Vladimir Davydovb8529902014-04-07 15:39:28 -0700459
Vladimir Davydov0bd62b12014-06-04 16:10:03 -0700460 list_del(&s->list);
461
Vladimir Davydovb8529902014-04-07 15:39:28 -0700462 mutex_unlock(&slab_mutex);
463 if (s->flags & SLAB_DESTROY_BY_RCU)
464 rcu_barrier();
465
466 memcg_free_cache_params(s);
Christoph Lameter41a21282014-05-06 12:50:08 -0700467#ifdef SLAB_SUPPORTS_SYSFS
468 sysfs_slab_remove(s);
469#else
470 slab_kmem_cache_release(s);
471#endif
Vladimir Davydov03afc0e2014-06-04 16:07:20 -0700472 goto out;
Vladimir Davydovb8529902014-04-07 15:39:28 -0700473
474out_unlock:
475 mutex_unlock(&slab_mutex);
Vladimir Davydov03afc0e2014-06-04 16:07:20 -0700476out:
477 put_online_mems();
Christoph Lameter945cf2b2012-09-04 23:18:33 +0000478 put_online_cpus();
479}
480EXPORT_SYMBOL(kmem_cache_destroy);
481
Vladimir Davydov03afc0e2014-06-04 16:07:20 -0700482/**
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 */
489int 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}
500EXPORT_SYMBOL(kmem_cache_shrink);
501
Christoph Lameter97d06602012-07-06 15:25:11 -0500502int slab_is_available(void)
503{
504 return slab_state >= UP;
505}
Glauber Costab7454ad2012-10-19 18:20:25 +0400506
Christoph Lameter45530c42012-11-28 16:23:07 +0000507#ifndef CONFIG_SLOB
508/* Create a cache during boot when no slab services are available yet */
509void __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 Lameter45906852012-11-28 16:23:16 +0000516 s->align = calculate_alignment(flags, ARCH_KMALLOC_MINALIGN, size);
Christoph Lameter45530c42012-11-28 16:23:07 +0000517 err = __kmem_cache_create(s, flags);
518
519 if (err)
Christoph Lameter31ba7342013-01-10 19:00:53 +0000520 panic("Creation of kmalloc slab %s size=%zu failed. Reason %d\n",
Christoph Lameter45530c42012-11-28 16:23:07 +0000521 name, size, err);
522
523 s->refcount = -1; /* Exempt from merging for now */
524}
525
526struct 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 Lameter9425c582013-01-10 19:12:17 +0000540struct kmem_cache *kmalloc_caches[KMALLOC_SHIFT_HIGH + 1];
541EXPORT_SYMBOL(kmalloc_caches);
542
543#ifdef CONFIG_ZONE_DMA
544struct kmem_cache *kmalloc_dma_caches[KMALLOC_SHIFT_HIGH + 1];
545EXPORT_SYMBOL(kmalloc_dma_caches);
546#endif
547
Christoph Lameterf97d5f62013-01-10 19:12:17 +0000548/*
Christoph Lameter2c59dd62013-01-10 19:14:19 +0000549 * 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 */
554static 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
581static 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 */
590struct kmem_cache *kmalloc_slab(size_t size, gfp_t flags)
591{
592 int index;
593
Joonsoo Kim9de1bc82013-08-02 11:02:42 +0900594 if (unlikely(size > KMALLOC_MAX_SIZE)) {
Sasha Levin907985f2013-06-10 15:18:00 -0400595 WARN_ON_ONCE(!(flags & __GFP_NOWARN));
Christoph Lameter6286ae92013-05-03 15:43:18 +0000596 return NULL;
Sasha Levin907985f2013-06-10 15:18:00 -0400597 }
Christoph Lameter6286ae92013-05-03 15:43:18 +0000598
Christoph Lameter2c59dd62013-01-10 19:14:19 +0000599 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 Kimb1e05412013-02-04 23:46:46 +0900608 if (unlikely((flags & GFP_DMA)))
Christoph Lameter2c59dd62013-01-10 19:14:19 +0000609 return kmalloc_dma_caches[index];
610
611#endif
612 return kmalloc_caches[index];
613}
614
615/*
Christoph Lameterf97d5f62013-01-10 19:12:17 +0000616 * 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 */
620void __init create_kmalloc_caches(unsigned long flags)
621{
622 int i;
623
Christoph Lameter2c59dd62013-01-10 19:14:19 +0000624 /*
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 Lameter8a965b32013-05-03 18:04:18 +0000665 for (i = KMALLOC_SHIFT_LOW; i <= KMALLOC_SHIFT_HIGH; i++) {
666 if (!kmalloc_caches[i]) {
Christoph Lameterf97d5f62013-01-10 19:12:17 +0000667 kmalloc_caches[i] = create_kmalloc_cache(NULL,
668 1 << i, flags);
Christoph Lameter8a965b32013-05-03 18:04:18 +0000669 }
Chris Mason956e46e2013-05-08 15:56:28 -0400670
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 Lameter8a965b32013-05-03 18:04:18 +0000681 }
682
Christoph Lameterf97d5f62013-01-10 19:12:17 +0000683 /* 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 Lameter45530c42012-11-28 16:23:07 +0000714#endif /* !CONFIG_SLOB */
715
Vladimir Davydovcea371f2014-06-04 16:07:04 -0700716/*
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 Davydov52383432014-06-04 16:06:39 -0700721void *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}
732EXPORT_SYMBOL(kmalloc_order);
733
Christoph Lameterf1b6eb62013-09-04 16:35:34 +0000734#ifdef CONFIG_TRACING
735void *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}
741EXPORT_SYMBOL(kmalloc_order_trace);
742#endif
Christoph Lameter45530c42012-11-28 16:23:07 +0000743
Glauber Costab7454ad2012-10-19 18:20:25 +0400744#ifdef CONFIG_SLABINFO
Wanpeng Lie9b4db22013-07-04 08:33:24 +0800745
746#ifdef CONFIG_SLAB
747#define SLABINFO_RIGHTS (S_IWUSR | S_IRUSR)
748#else
749#define SLABINFO_RIGHTS S_IRUSR
750#endif
751
Glauber Costa749c5412012-12-18 14:23:01 -0800752void print_slabinfo_header(struct seq_file *m)
Glauber Costabcee6e22012-10-19 18:20:26 +0400753{
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 Costab7454ad2012-10-19 18:20:25 +0400775static 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 Li276a2432013-07-08 08:08:28 +0800786void *slab_next(struct seq_file *m, void *p, loff_t *pos)
Glauber Costab7454ad2012-10-19 18:20:25 +0400787{
788 return seq_list_next(p, &slab_caches, pos);
789}
790
Wanpeng Li276a2432013-07-08 08:08:28 +0800791void slab_stop(struct seq_file *m, void *p)
Glauber Costab7454ad2012-10-19 18:20:25 +0400792{
793 mutex_unlock(&slab_mutex);
794}
795
Glauber Costa749c5412012-12-18 14:23:01 -0800796static void
797memcg_accumulate_slabinfo(struct kmem_cache *s, struct slabinfo *info)
Glauber Costab7454ad2012-10-19 18:20:25 +0400798{
Glauber Costa749c5412012-12-18 14:23:01 -0800799 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 Huang2ade4de2013-11-12 15:08:23 -0800807 c = cache_from_memcg_idx(s, i);
Glauber Costa749c5412012-12-18 14:23:01 -0800808 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
822int cache_show(struct kmem_cache *s, struct seq_file *m)
823{
Glauber Costa0d7561c2012-10-19 18:20:27 +0400824 struct slabinfo sinfo;
825
826 memset(&sinfo, 0, sizeof(sinfo));
827 get_slabinfo(s, &sinfo);
828
Glauber Costa749c5412012-12-18 14:23:01 -0800829 memcg_accumulate_slabinfo(s, &sinfo);
830
Glauber Costa0d7561c2012-10-19 18:20:27 +0400831 seq_printf(m, "%-17s %6lu %6lu %6u %4u %4d",
Glauber Costa749c5412012-12-18 14:23:01 -0800832 cache_name(s), sinfo.active_objs, sinfo.num_objs, s->size,
Glauber Costa0d7561c2012-10-19 18:20:27 +0400833 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 Costab7454ad2012-10-19 18:20:25 +0400842}
843
Glauber Costa749c5412012-12-18 14:23:01 -0800844static 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 Costab7454ad2012-10-19 18:20:25 +0400853/*
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 */
866static const struct seq_operations slabinfo_op = {
867 .start = s_start,
Wanpeng Li276a2432013-07-08 08:08:28 +0800868 .next = slab_next,
869 .stop = slab_stop,
Glauber Costab7454ad2012-10-19 18:20:25 +0400870 .show = s_show,
871};
872
873static int slabinfo_open(struct inode *inode, struct file *file)
874{
875 return seq_open(file, &slabinfo_op);
876}
877
878static 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
886static int __init slab_proc_init(void)
887{
Wanpeng Lie9b4db22013-07-04 08:33:24 +0800888 proc_create("slabinfo", SLABINFO_RIGHTS, NULL,
889 &proc_slabinfo_operations);
Glauber Costab7454ad2012-10-19 18:20:25 +0400890 return 0;
891}
892module_init(slab_proc_init);
893#endif /* CONFIG_SLABINFO */
Andrey Ryabinin928cec92014-08-06 16:04:44 -0700894
895static __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 */
924void *__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}
932EXPORT_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 */
945void *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}
960EXPORT_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 */
973void 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}
984EXPORT_SYMBOL(kzfree);
985
986/* Tracepoints definitions. */
987EXPORT_TRACEPOINT_SYMBOL(kmalloc);
988EXPORT_TRACEPOINT_SYMBOL(kmem_cache_alloc);
989EXPORT_TRACEPOINT_SYMBOL(kmalloc_node);
990EXPORT_TRACEPOINT_SYMBOL(kmem_cache_alloc_node);
991EXPORT_TRACEPOINT_SYMBOL(kfree);
992EXPORT_TRACEPOINT_SYMBOL(kmem_cache_free);