blob: c2a8661f8b81c89888e6a9b0140b28558a1083f9 [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
Vladimir Davydov33a690c2014-10-09 15:28:43 -0700119static int memcg_alloc_cache_params(struct mem_cgroup *memcg,
120 struct kmem_cache *s, struct kmem_cache *root_cache)
121{
122 size_t size;
123
124 if (!memcg_kmem_enabled())
125 return 0;
126
127 if (!memcg) {
128 size = offsetof(struct memcg_cache_params, memcg_caches);
129 size += memcg_limited_groups_array_size * sizeof(void *);
130 } else
131 size = sizeof(struct memcg_cache_params);
132
133 s->memcg_params = kzalloc(size, GFP_KERNEL);
134 if (!s->memcg_params)
135 return -ENOMEM;
136
137 if (memcg) {
138 s->memcg_params->memcg = memcg;
139 s->memcg_params->root_cache = root_cache;
140 } else
141 s->memcg_params->is_root_cache = true;
142
143 return 0;
144}
145
146static void memcg_free_cache_params(struct kmem_cache *s)
147{
148 kfree(s->memcg_params);
149}
150
Glauber Costa55007d82012-12-18 14:22:38 -0800151int memcg_update_all_caches(int num_memcgs)
152{
153 struct kmem_cache *s;
154 int ret = 0;
155 mutex_lock(&slab_mutex);
156
157 list_for_each_entry(s, &slab_caches, list) {
158 if (!is_root_cache(s))
159 continue;
160
161 ret = memcg_update_cache_size(s, num_memcgs);
162 /*
163 * See comment in memcontrol.c, memcg_update_cache_size:
164 * Instead of freeing the memory, we'll just leave the caches
165 * up to this point in an updated state.
166 */
167 if (ret)
168 goto out;
169 }
170
171 memcg_update_array_size(num_memcgs);
172out:
173 mutex_unlock(&slab_mutex);
174 return ret;
175}
Vladimir Davydov33a690c2014-10-09 15:28:43 -0700176#else
177static inline int memcg_alloc_cache_params(struct mem_cgroup *memcg,
178 struct kmem_cache *s, struct kmem_cache *root_cache)
179{
180 return 0;
181}
182
183static inline void memcg_free_cache_params(struct kmem_cache *s)
184{
185}
186#endif /* CONFIG_MEMCG_KMEM */
Glauber Costa55007d82012-12-18 14:22:38 -0800187
Christoph Lameter039363f2012-07-06 15:25:10 -0500188/*
Joonsoo Kim423c9292014-10-09 15:26:22 -0700189 * Find a mergeable slab cache
190 */
191int slab_unmergeable(struct kmem_cache *s)
192{
193 if (slab_nomerge || (s->flags & SLAB_NEVER_MERGE))
194 return 1;
195
196 if (!is_root_cache(s))
197 return 1;
198
199 if (s->ctor)
200 return 1;
201
202 /*
203 * We may have set a slab to be unmergeable during bootstrap.
204 */
205 if (s->refcount < 0)
206 return 1;
207
208 return 0;
209}
210
211struct kmem_cache *find_mergeable(size_t size, size_t align,
212 unsigned long flags, const char *name, void (*ctor)(void *))
213{
214 struct kmem_cache *s;
215
216 if (slab_nomerge || (flags & SLAB_NEVER_MERGE))
217 return NULL;
218
219 if (ctor)
220 return NULL;
221
222 size = ALIGN(size, sizeof(void *));
223 align = calculate_alignment(flags, align, size);
224 size = ALIGN(size, align);
225 flags = kmem_cache_flags(size, flags, name, NULL);
226
227 list_for_each_entry(s, &slab_caches, list) {
228 if (slab_unmergeable(s))
229 continue;
230
231 if (size > s->size)
232 continue;
233
234 if ((flags & SLAB_MERGE_SAME) != (s->flags & SLAB_MERGE_SAME))
235 continue;
236 /*
237 * Check if alignment is compatible.
238 * Courtesy of Adrian Drzewiecki
239 */
240 if ((s->size & ~(align - 1)) != s->size)
241 continue;
242
243 if (s->size - size >= sizeof(void *))
244 continue;
245
246 return s;
247 }
248 return NULL;
249}
250
251/*
Christoph Lameter45906852012-11-28 16:23:16 +0000252 * Figure out what the alignment of the objects will be given a set of
253 * flags, a user specified alignment and the size of the objects.
254 */
255unsigned long calculate_alignment(unsigned long flags,
256 unsigned long align, unsigned long size)
257{
258 /*
259 * If the user wants hardware cache aligned objects then follow that
260 * suggestion if the object is sufficiently large.
261 *
262 * The hardware cache alignment cannot override the specified
263 * alignment though. If that is greater then use it.
264 */
265 if (flags & SLAB_HWCACHE_ALIGN) {
266 unsigned long ralign = cache_line_size();
267 while (size <= ralign / 2)
268 ralign /= 2;
269 align = max(align, ralign);
270 }
271
272 if (align < ARCH_SLAB_MINALIGN)
273 align = ARCH_SLAB_MINALIGN;
274
275 return ALIGN(align, sizeof(void *));
276}
277
Vladimir Davydov794b1242014-04-07 15:39:26 -0700278static struct kmem_cache *
279do_kmem_cache_create(char *name, size_t object_size, size_t size, size_t align,
280 unsigned long flags, void (*ctor)(void *),
281 struct mem_cgroup *memcg, struct kmem_cache *root_cache)
282{
283 struct kmem_cache *s;
284 int err;
285
286 err = -ENOMEM;
287 s = kmem_cache_zalloc(kmem_cache, GFP_KERNEL);
288 if (!s)
289 goto out;
290
291 s->name = name;
292 s->object_size = object_size;
293 s->size = size;
294 s->align = align;
295 s->ctor = ctor;
296
297 err = memcg_alloc_cache_params(memcg, s, root_cache);
298 if (err)
299 goto out_free_cache;
300
301 err = __kmem_cache_create(s, flags);
302 if (err)
303 goto out_free_cache;
304
305 s->refcount = 1;
306 list_add(&s->list, &slab_caches);
Vladimir Davydov794b1242014-04-07 15:39:26 -0700307out:
308 if (err)
309 return ERR_PTR(err);
310 return s;
311
312out_free_cache:
313 memcg_free_cache_params(s);
314 kfree(s);
315 goto out;
316}
Christoph Lameter45906852012-11-28 16:23:16 +0000317
318/*
Christoph Lameter039363f2012-07-06 15:25:10 -0500319 * kmem_cache_create - Create a cache.
320 * @name: A string which is used in /proc/slabinfo to identify this cache.
321 * @size: The size of objects to be created in this cache.
322 * @align: The required alignment for the objects.
323 * @flags: SLAB flags
324 * @ctor: A constructor for the objects.
325 *
326 * Returns a ptr to the cache on success, NULL on failure.
327 * Cannot be called within a interrupt, but can be interrupted.
328 * The @ctor is run when new pages are allocated by the cache.
329 *
330 * The flags are
331 *
332 * %SLAB_POISON - Poison the slab with a known test pattern (a5a5a5a5)
333 * to catch references to uninitialised memory.
334 *
335 * %SLAB_RED_ZONE - Insert `Red' zones around the allocated memory to check
336 * for buffer overruns.
337 *
338 * %SLAB_HWCACHE_ALIGN - Align the objects in this cache to a hardware
339 * cacheline. This can be beneficial if you're counting cycles as closely
340 * as davem.
341 */
Glauber Costa2633d7a2012-12-18 14:22:34 -0800342struct kmem_cache *
Vladimir Davydov794b1242014-04-07 15:39:26 -0700343kmem_cache_create(const char *name, size_t size, size_t align,
344 unsigned long flags, void (*ctor)(void *))
Christoph Lameter039363f2012-07-06 15:25:10 -0500345{
Vladimir Davydov794b1242014-04-07 15:39:26 -0700346 struct kmem_cache *s;
347 char *cache_name;
Vladimir Davydov3965fc32014-01-23 15:52:55 -0800348 int err;
Christoph Lameter039363f2012-07-06 15:25:10 -0500349
Pekka Enbergb9205362012-08-16 10:12:18 +0300350 get_online_cpus();
Vladimir Davydov03afc0e2014-06-04 16:07:20 -0700351 get_online_mems();
352
Pekka Enbergb9205362012-08-16 10:12:18 +0300353 mutex_lock(&slab_mutex);
Christoph Lameter686d5502012-09-05 00:20:33 +0000354
Vladimir Davydov794b1242014-04-07 15:39:26 -0700355 err = kmem_cache_sanity_check(name, size);
Andrew Morton3aa24f52014-10-09 15:25:58 -0700356 if (err) {
357 s = NULL; /* suppress uninit var warning */
Vladimir Davydov3965fc32014-01-23 15:52:55 -0800358 goto out_unlock;
Andrew Morton3aa24f52014-10-09 15:25:58 -0700359 }
Christoph Lameter686d5502012-09-05 00:20:33 +0000360
Glauber Costad8843922012-10-17 15:36:51 +0400361 /*
362 * Some allocators will constraint the set of valid flags to a subset
363 * of all flags. We expect them to define CACHE_CREATE_MASK in this
364 * case, and we'll just provide them with a sanitized version of the
365 * passed flags.
366 */
367 flags &= CACHE_CREATE_MASK;
Christoph Lameter686d5502012-09-05 00:20:33 +0000368
Vladimir Davydov794b1242014-04-07 15:39:26 -0700369 s = __kmem_cache_alias(name, size, align, flags, ctor);
370 if (s)
Vladimir Davydov3965fc32014-01-23 15:52:55 -0800371 goto out_unlock;
Glauber Costa2633d7a2012-12-18 14:22:34 -0800372
Vladimir Davydov794b1242014-04-07 15:39:26 -0700373 cache_name = kstrdup(name, GFP_KERNEL);
374 if (!cache_name) {
375 err = -ENOMEM;
376 goto out_unlock;
377 }
Glauber Costa2633d7a2012-12-18 14:22:34 -0800378
Vladimir Davydov794b1242014-04-07 15:39:26 -0700379 s = do_kmem_cache_create(cache_name, size, size,
380 calculate_alignment(flags, align, size),
381 flags, ctor, NULL, NULL);
382 if (IS_ERR(s)) {
383 err = PTR_ERR(s);
384 kfree(cache_name);
385 }
Vladimir Davydov3965fc32014-01-23 15:52:55 -0800386
387out_unlock:
Christoph Lameter20cea962012-07-06 15:25:13 -0500388 mutex_unlock(&slab_mutex);
Vladimir Davydov03afc0e2014-06-04 16:07:20 -0700389
390 put_online_mems();
Christoph Lameter20cea962012-07-06 15:25:13 -0500391 put_online_cpus();
392
Dave Jonesba3253c72014-01-29 14:05:48 -0800393 if (err) {
Christoph Lameter686d5502012-09-05 00:20:33 +0000394 if (flags & SLAB_PANIC)
395 panic("kmem_cache_create: Failed to create slab '%s'. Error %d\n",
396 name, err);
397 else {
398 printk(KERN_WARNING "kmem_cache_create(%s) failed with error %d",
399 name, err);
400 dump_stack();
401 }
Christoph Lameter686d5502012-09-05 00:20:33 +0000402 return NULL;
403 }
Christoph Lameter039363f2012-07-06 15:25:10 -0500404 return s;
Glauber Costa2633d7a2012-12-18 14:22:34 -0800405}
Christoph Lameter039363f2012-07-06 15:25:10 -0500406EXPORT_SYMBOL(kmem_cache_create);
Christoph Lameter97d06602012-07-06 15:25:11 -0500407
Vladimir Davydov794b1242014-04-07 15:39:26 -0700408#ifdef CONFIG_MEMCG_KMEM
409/*
Vladimir Davydov776ed0f2014-06-04 16:10:02 -0700410 * memcg_create_kmem_cache - Create a cache for a memory cgroup.
Vladimir Davydov794b1242014-04-07 15:39:26 -0700411 * @memcg: The memory cgroup the new cache is for.
412 * @root_cache: The parent of the new cache.
Vladimir Davydov073ee1c2014-06-04 16:08:23 -0700413 * @memcg_name: The name of the memory cgroup (used for naming the new cache).
Vladimir Davydov794b1242014-04-07 15:39:26 -0700414 *
415 * This function attempts to create a kmem cache that will serve allocation
416 * requests going from @memcg to @root_cache. The new cache inherits properties
417 * from its parent.
418 */
Vladimir Davydov776ed0f2014-06-04 16:10:02 -0700419struct kmem_cache *memcg_create_kmem_cache(struct mem_cgroup *memcg,
Vladimir Davydov073ee1c2014-06-04 16:08:23 -0700420 struct kmem_cache *root_cache,
421 const char *memcg_name)
Vladimir Davydov794b1242014-04-07 15:39:26 -0700422{
Vladimir Davydovbd673142014-06-04 16:07:40 -0700423 struct kmem_cache *s = NULL;
Vladimir Davydov794b1242014-04-07 15:39:26 -0700424 char *cache_name;
425
426 get_online_cpus();
Vladimir Davydov03afc0e2014-06-04 16:07:20 -0700427 get_online_mems();
428
Vladimir Davydov794b1242014-04-07 15:39:26 -0700429 mutex_lock(&slab_mutex);
430
Vladimir Davydov073ee1c2014-06-04 16:08:23 -0700431 cache_name = kasprintf(GFP_KERNEL, "%s(%d:%s)", root_cache->name,
432 memcg_cache_id(memcg), memcg_name);
Vladimir Davydov794b1242014-04-07 15:39:26 -0700433 if (!cache_name)
434 goto out_unlock;
435
436 s = do_kmem_cache_create(cache_name, root_cache->object_size,
437 root_cache->size, root_cache->align,
438 root_cache->flags, root_cache->ctor,
439 memcg, root_cache);
Vladimir Davydovbd673142014-06-04 16:07:40 -0700440 if (IS_ERR(s)) {
Vladimir Davydov794b1242014-04-07 15:39:26 -0700441 kfree(cache_name);
Vladimir Davydovbd673142014-06-04 16:07:40 -0700442 s = NULL;
443 }
Vladimir Davydov794b1242014-04-07 15:39:26 -0700444
445out_unlock:
446 mutex_unlock(&slab_mutex);
Vladimir Davydov03afc0e2014-06-04 16:07:20 -0700447
448 put_online_mems();
Vladimir Davydov794b1242014-04-07 15:39:26 -0700449 put_online_cpus();
Vladimir Davydovbd673142014-06-04 16:07:40 -0700450
451 return s;
Vladimir Davydov794b1242014-04-07 15:39:26 -0700452}
Vladimir Davydovb8529902014-04-07 15:39:28 -0700453
Vladimir Davydov776ed0f2014-06-04 16:10:02 -0700454static int memcg_cleanup_cache_params(struct kmem_cache *s)
Vladimir Davydovb8529902014-04-07 15:39:28 -0700455{
456 int rc;
457
458 if (!s->memcg_params ||
459 !s->memcg_params->is_root_cache)
460 return 0;
461
462 mutex_unlock(&slab_mutex);
Vladimir Davydov776ed0f2014-06-04 16:10:02 -0700463 rc = __memcg_cleanup_cache_params(s);
Vladimir Davydovb8529902014-04-07 15:39:28 -0700464 mutex_lock(&slab_mutex);
465
466 return rc;
467}
468#else
Vladimir Davydov776ed0f2014-06-04 16:10:02 -0700469static int memcg_cleanup_cache_params(struct kmem_cache *s)
Vladimir Davydovb8529902014-04-07 15:39:28 -0700470{
471 return 0;
472}
Vladimir Davydov794b1242014-04-07 15:39:26 -0700473#endif /* CONFIG_MEMCG_KMEM */
474
Christoph Lameter41a21282014-05-06 12:50:08 -0700475void slab_kmem_cache_release(struct kmem_cache *s)
476{
477 kfree(s->name);
478 kmem_cache_free(kmem_cache, s);
479}
480
Christoph Lameter945cf2b2012-09-04 23:18:33 +0000481void kmem_cache_destroy(struct kmem_cache *s)
482{
483 get_online_cpus();
Vladimir Davydov03afc0e2014-06-04 16:07:20 -0700484 get_online_mems();
485
Christoph Lameter945cf2b2012-09-04 23:18:33 +0000486 mutex_lock(&slab_mutex);
Vladimir Davydovb8529902014-04-07 15:39:28 -0700487
Christoph Lameter945cf2b2012-09-04 23:18:33 +0000488 s->refcount--;
Vladimir Davydovb8529902014-04-07 15:39:28 -0700489 if (s->refcount)
490 goto out_unlock;
Christoph Lameter945cf2b2012-09-04 23:18:33 +0000491
Vladimir Davydov776ed0f2014-06-04 16:10:02 -0700492 if (memcg_cleanup_cache_params(s) != 0)
Vladimir Davydovb8529902014-04-07 15:39:28 -0700493 goto out_unlock;
Christoph Lameter945cf2b2012-09-04 23:18:33 +0000494
Vladimir Davydovb8529902014-04-07 15:39:28 -0700495 if (__kmem_cache_shutdown(s) != 0) {
Vladimir Davydovb8529902014-04-07 15:39:28 -0700496 printk(KERN_ERR "kmem_cache_destroy %s: "
497 "Slab cache still has objects\n", s->name);
498 dump_stack();
499 goto out_unlock;
Christoph Lameter945cf2b2012-09-04 23:18:33 +0000500 }
Vladimir Davydovb8529902014-04-07 15:39:28 -0700501
Vladimir Davydov0bd62b12014-06-04 16:10:03 -0700502 list_del(&s->list);
503
Vladimir Davydovb8529902014-04-07 15:39:28 -0700504 mutex_unlock(&slab_mutex);
505 if (s->flags & SLAB_DESTROY_BY_RCU)
506 rcu_barrier();
507
508 memcg_free_cache_params(s);
Christoph Lameter41a21282014-05-06 12:50:08 -0700509#ifdef SLAB_SUPPORTS_SYSFS
510 sysfs_slab_remove(s);
511#else
512 slab_kmem_cache_release(s);
513#endif
Vladimir Davydov03afc0e2014-06-04 16:07:20 -0700514 goto out;
Vladimir Davydovb8529902014-04-07 15:39:28 -0700515
516out_unlock:
517 mutex_unlock(&slab_mutex);
Vladimir Davydov03afc0e2014-06-04 16:07:20 -0700518out:
519 put_online_mems();
Christoph Lameter945cf2b2012-09-04 23:18:33 +0000520 put_online_cpus();
521}
522EXPORT_SYMBOL(kmem_cache_destroy);
523
Vladimir Davydov03afc0e2014-06-04 16:07:20 -0700524/**
525 * kmem_cache_shrink - Shrink a cache.
526 * @cachep: The cache to shrink.
527 *
528 * Releases as many slabs as possible for a cache.
529 * To help debugging, a zero exit status indicates all slabs were released.
530 */
531int kmem_cache_shrink(struct kmem_cache *cachep)
532{
533 int ret;
534
535 get_online_cpus();
536 get_online_mems();
537 ret = __kmem_cache_shrink(cachep);
538 put_online_mems();
539 put_online_cpus();
540 return ret;
541}
542EXPORT_SYMBOL(kmem_cache_shrink);
543
Christoph Lameter97d06602012-07-06 15:25:11 -0500544int slab_is_available(void)
545{
546 return slab_state >= UP;
547}
Glauber Costab7454ad2012-10-19 18:20:25 +0400548
Christoph Lameter45530c42012-11-28 16:23:07 +0000549#ifndef CONFIG_SLOB
550/* Create a cache during boot when no slab services are available yet */
551void __init create_boot_cache(struct kmem_cache *s, const char *name, size_t size,
552 unsigned long flags)
553{
554 int err;
555
556 s->name = name;
557 s->size = s->object_size = size;
Christoph Lameter45906852012-11-28 16:23:16 +0000558 s->align = calculate_alignment(flags, ARCH_KMALLOC_MINALIGN, size);
Christoph Lameter45530c42012-11-28 16:23:07 +0000559 err = __kmem_cache_create(s, flags);
560
561 if (err)
Christoph Lameter31ba7342013-01-10 19:00:53 +0000562 panic("Creation of kmalloc slab %s size=%zu failed. Reason %d\n",
Christoph Lameter45530c42012-11-28 16:23:07 +0000563 name, size, err);
564
565 s->refcount = -1; /* Exempt from merging for now */
566}
567
568struct kmem_cache *__init create_kmalloc_cache(const char *name, size_t size,
569 unsigned long flags)
570{
571 struct kmem_cache *s = kmem_cache_zalloc(kmem_cache, GFP_NOWAIT);
572
573 if (!s)
574 panic("Out of memory when creating slab %s\n", name);
575
576 create_boot_cache(s, name, size, flags);
577 list_add(&s->list, &slab_caches);
578 s->refcount = 1;
579 return s;
580}
581
Christoph Lameter9425c582013-01-10 19:12:17 +0000582struct kmem_cache *kmalloc_caches[KMALLOC_SHIFT_HIGH + 1];
583EXPORT_SYMBOL(kmalloc_caches);
584
585#ifdef CONFIG_ZONE_DMA
586struct kmem_cache *kmalloc_dma_caches[KMALLOC_SHIFT_HIGH + 1];
587EXPORT_SYMBOL(kmalloc_dma_caches);
588#endif
589
Christoph Lameterf97d5f62013-01-10 19:12:17 +0000590/*
Christoph Lameter2c59dd62013-01-10 19:14:19 +0000591 * Conversion table for small slabs sizes / 8 to the index in the
592 * kmalloc array. This is necessary for slabs < 192 since we have non power
593 * of two cache sizes there. The size of larger slabs can be determined using
594 * fls.
595 */
596static s8 size_index[24] = {
597 3, /* 8 */
598 4, /* 16 */
599 5, /* 24 */
600 5, /* 32 */
601 6, /* 40 */
602 6, /* 48 */
603 6, /* 56 */
604 6, /* 64 */
605 1, /* 72 */
606 1, /* 80 */
607 1, /* 88 */
608 1, /* 96 */
609 7, /* 104 */
610 7, /* 112 */
611 7, /* 120 */
612 7, /* 128 */
613 2, /* 136 */
614 2, /* 144 */
615 2, /* 152 */
616 2, /* 160 */
617 2, /* 168 */
618 2, /* 176 */
619 2, /* 184 */
620 2 /* 192 */
621};
622
623static inline int size_index_elem(size_t bytes)
624{
625 return (bytes - 1) / 8;
626}
627
628/*
629 * Find the kmem_cache structure that serves a given size of
630 * allocation
631 */
632struct kmem_cache *kmalloc_slab(size_t size, gfp_t flags)
633{
634 int index;
635
Joonsoo Kim9de1bc82013-08-02 11:02:42 +0900636 if (unlikely(size > KMALLOC_MAX_SIZE)) {
Sasha Levin907985f2013-06-10 15:18:00 -0400637 WARN_ON_ONCE(!(flags & __GFP_NOWARN));
Christoph Lameter6286ae92013-05-03 15:43:18 +0000638 return NULL;
Sasha Levin907985f2013-06-10 15:18:00 -0400639 }
Christoph Lameter6286ae92013-05-03 15:43:18 +0000640
Christoph Lameter2c59dd62013-01-10 19:14:19 +0000641 if (size <= 192) {
642 if (!size)
643 return ZERO_SIZE_PTR;
644
645 index = size_index[size_index_elem(size)];
646 } else
647 index = fls(size - 1);
648
649#ifdef CONFIG_ZONE_DMA
Joonsoo Kimb1e05412013-02-04 23:46:46 +0900650 if (unlikely((flags & GFP_DMA)))
Christoph Lameter2c59dd62013-01-10 19:14:19 +0000651 return kmalloc_dma_caches[index];
652
653#endif
654 return kmalloc_caches[index];
655}
656
657/*
Christoph Lameterf97d5f62013-01-10 19:12:17 +0000658 * Create the kmalloc array. Some of the regular kmalloc arrays
659 * may already have been created because they were needed to
660 * enable allocations for slab creation.
661 */
662void __init create_kmalloc_caches(unsigned long flags)
663{
664 int i;
665
Christoph Lameter2c59dd62013-01-10 19:14:19 +0000666 /*
667 * Patch up the size_index table if we have strange large alignment
668 * requirements for the kmalloc array. This is only the case for
669 * MIPS it seems. The standard arches will not generate any code here.
670 *
671 * Largest permitted alignment is 256 bytes due to the way we
672 * handle the index determination for the smaller caches.
673 *
674 * Make sure that nothing crazy happens if someone starts tinkering
675 * around with ARCH_KMALLOC_MINALIGN
676 */
677 BUILD_BUG_ON(KMALLOC_MIN_SIZE > 256 ||
678 (KMALLOC_MIN_SIZE & (KMALLOC_MIN_SIZE - 1)));
679
680 for (i = 8; i < KMALLOC_MIN_SIZE; i += 8) {
681 int elem = size_index_elem(i);
682
683 if (elem >= ARRAY_SIZE(size_index))
684 break;
685 size_index[elem] = KMALLOC_SHIFT_LOW;
686 }
687
688 if (KMALLOC_MIN_SIZE >= 64) {
689 /*
690 * The 96 byte size cache is not used if the alignment
691 * is 64 byte.
692 */
693 for (i = 64 + 8; i <= 96; i += 8)
694 size_index[size_index_elem(i)] = 7;
695
696 }
697
698 if (KMALLOC_MIN_SIZE >= 128) {
699 /*
700 * The 192 byte sized cache is not used if the alignment
701 * is 128 byte. Redirect kmalloc to use the 256 byte cache
702 * instead.
703 */
704 for (i = 128 + 8; i <= 192; i += 8)
705 size_index[size_index_elem(i)] = 8;
706 }
Christoph Lameter8a965b32013-05-03 18:04:18 +0000707 for (i = KMALLOC_SHIFT_LOW; i <= KMALLOC_SHIFT_HIGH; i++) {
708 if (!kmalloc_caches[i]) {
Christoph Lameterf97d5f62013-01-10 19:12:17 +0000709 kmalloc_caches[i] = create_kmalloc_cache(NULL,
710 1 << i, flags);
Christoph Lameter8a965b32013-05-03 18:04:18 +0000711 }
Chris Mason956e46e2013-05-08 15:56:28 -0400712
713 /*
714 * Caches that are not of the two-to-the-power-of size.
715 * These have to be created immediately after the
716 * earlier power of two caches
717 */
718 if (KMALLOC_MIN_SIZE <= 32 && !kmalloc_caches[1] && i == 6)
719 kmalloc_caches[1] = create_kmalloc_cache(NULL, 96, flags);
720
721 if (KMALLOC_MIN_SIZE <= 64 && !kmalloc_caches[2] && i == 7)
722 kmalloc_caches[2] = create_kmalloc_cache(NULL, 192, flags);
Christoph Lameter8a965b32013-05-03 18:04:18 +0000723 }
724
Christoph Lameterf97d5f62013-01-10 19:12:17 +0000725 /* Kmalloc array is now usable */
726 slab_state = UP;
727
728 for (i = 0; i <= KMALLOC_SHIFT_HIGH; i++) {
729 struct kmem_cache *s = kmalloc_caches[i];
730 char *n;
731
732 if (s) {
733 n = kasprintf(GFP_NOWAIT, "kmalloc-%d", kmalloc_size(i));
734
735 BUG_ON(!n);
736 s->name = n;
737 }
738 }
739
740#ifdef CONFIG_ZONE_DMA
741 for (i = 0; i <= KMALLOC_SHIFT_HIGH; i++) {
742 struct kmem_cache *s = kmalloc_caches[i];
743
744 if (s) {
745 int size = kmalloc_size(i);
746 char *n = kasprintf(GFP_NOWAIT,
747 "dma-kmalloc-%d", size);
748
749 BUG_ON(!n);
750 kmalloc_dma_caches[i] = create_kmalloc_cache(n,
751 size, SLAB_CACHE_DMA | flags);
752 }
753 }
754#endif
755}
Christoph Lameter45530c42012-11-28 16:23:07 +0000756#endif /* !CONFIG_SLOB */
757
Vladimir Davydovcea371f2014-06-04 16:07:04 -0700758/*
759 * To avoid unnecessary overhead, we pass through large allocation requests
760 * directly to the page allocator. We use __GFP_COMP, because we will need to
761 * know the allocation order to free the pages properly in kfree.
762 */
Vladimir Davydov52383432014-06-04 16:06:39 -0700763void *kmalloc_order(size_t size, gfp_t flags, unsigned int order)
764{
765 void *ret;
766 struct page *page;
767
768 flags |= __GFP_COMP;
769 page = alloc_kmem_pages(flags, order);
770 ret = page ? page_address(page) : NULL;
771 kmemleak_alloc(ret, size, 1, flags);
772 return ret;
773}
774EXPORT_SYMBOL(kmalloc_order);
775
Christoph Lameterf1b6eb62013-09-04 16:35:34 +0000776#ifdef CONFIG_TRACING
777void *kmalloc_order_trace(size_t size, gfp_t flags, unsigned int order)
778{
779 void *ret = kmalloc_order(size, flags, order);
780 trace_kmalloc(_RET_IP_, ret, size, PAGE_SIZE << order, flags);
781 return ret;
782}
783EXPORT_SYMBOL(kmalloc_order_trace);
784#endif
Christoph Lameter45530c42012-11-28 16:23:07 +0000785
Glauber Costab7454ad2012-10-19 18:20:25 +0400786#ifdef CONFIG_SLABINFO
Wanpeng Lie9b4db22013-07-04 08:33:24 +0800787
788#ifdef CONFIG_SLAB
789#define SLABINFO_RIGHTS (S_IWUSR | S_IRUSR)
790#else
791#define SLABINFO_RIGHTS S_IRUSR
792#endif
793
Glauber Costa749c5412012-12-18 14:23:01 -0800794void print_slabinfo_header(struct seq_file *m)
Glauber Costabcee6e22012-10-19 18:20:26 +0400795{
796 /*
797 * Output format version, so at least we can change it
798 * without _too_ many complaints.
799 */
800#ifdef CONFIG_DEBUG_SLAB
801 seq_puts(m, "slabinfo - version: 2.1 (statistics)\n");
802#else
803 seq_puts(m, "slabinfo - version: 2.1\n");
804#endif
805 seq_puts(m, "# name <active_objs> <num_objs> <objsize> "
806 "<objperslab> <pagesperslab>");
807 seq_puts(m, " : tunables <limit> <batchcount> <sharedfactor>");
808 seq_puts(m, " : slabdata <active_slabs> <num_slabs> <sharedavail>");
809#ifdef CONFIG_DEBUG_SLAB
810 seq_puts(m, " : globalstat <listallocs> <maxobjs> <grown> <reaped> "
811 "<error> <maxfreeable> <nodeallocs> <remotefrees> <alienoverflow>");
812 seq_puts(m, " : cpustat <allochit> <allocmiss> <freehit> <freemiss>");
813#endif
814 seq_putc(m, '\n');
815}
816
Glauber Costab7454ad2012-10-19 18:20:25 +0400817static void *s_start(struct seq_file *m, loff_t *pos)
818{
819 loff_t n = *pos;
820
821 mutex_lock(&slab_mutex);
822 if (!n)
823 print_slabinfo_header(m);
824
825 return seq_list_start(&slab_caches, *pos);
826}
827
Wanpeng Li276a2432013-07-08 08:08:28 +0800828void *slab_next(struct seq_file *m, void *p, loff_t *pos)
Glauber Costab7454ad2012-10-19 18:20:25 +0400829{
830 return seq_list_next(p, &slab_caches, pos);
831}
832
Wanpeng Li276a2432013-07-08 08:08:28 +0800833void slab_stop(struct seq_file *m, void *p)
Glauber Costab7454ad2012-10-19 18:20:25 +0400834{
835 mutex_unlock(&slab_mutex);
836}
837
Glauber Costa749c5412012-12-18 14:23:01 -0800838static void
839memcg_accumulate_slabinfo(struct kmem_cache *s, struct slabinfo *info)
Glauber Costab7454ad2012-10-19 18:20:25 +0400840{
Glauber Costa749c5412012-12-18 14:23:01 -0800841 struct kmem_cache *c;
842 struct slabinfo sinfo;
843 int i;
844
845 if (!is_root_cache(s))
846 return;
847
848 for_each_memcg_cache_index(i) {
Qiang Huang2ade4de2013-11-12 15:08:23 -0800849 c = cache_from_memcg_idx(s, i);
Glauber Costa749c5412012-12-18 14:23:01 -0800850 if (!c)
851 continue;
852
853 memset(&sinfo, 0, sizeof(sinfo));
854 get_slabinfo(c, &sinfo);
855
856 info->active_slabs += sinfo.active_slabs;
857 info->num_slabs += sinfo.num_slabs;
858 info->shared_avail += sinfo.shared_avail;
859 info->active_objs += sinfo.active_objs;
860 info->num_objs += sinfo.num_objs;
861 }
862}
863
864int cache_show(struct kmem_cache *s, struct seq_file *m)
865{
Glauber Costa0d7561c2012-10-19 18:20:27 +0400866 struct slabinfo sinfo;
867
868 memset(&sinfo, 0, sizeof(sinfo));
869 get_slabinfo(s, &sinfo);
870
Glauber Costa749c5412012-12-18 14:23:01 -0800871 memcg_accumulate_slabinfo(s, &sinfo);
872
Glauber Costa0d7561c2012-10-19 18:20:27 +0400873 seq_printf(m, "%-17s %6lu %6lu %6u %4u %4d",
Glauber Costa749c5412012-12-18 14:23:01 -0800874 cache_name(s), sinfo.active_objs, sinfo.num_objs, s->size,
Glauber Costa0d7561c2012-10-19 18:20:27 +0400875 sinfo.objects_per_slab, (1 << sinfo.cache_order));
876
877 seq_printf(m, " : tunables %4u %4u %4u",
878 sinfo.limit, sinfo.batchcount, sinfo.shared);
879 seq_printf(m, " : slabdata %6lu %6lu %6lu",
880 sinfo.active_slabs, sinfo.num_slabs, sinfo.shared_avail);
881 slabinfo_show_stats(m, s);
882 seq_putc(m, '\n');
883 return 0;
Glauber Costab7454ad2012-10-19 18:20:25 +0400884}
885
Glauber Costa749c5412012-12-18 14:23:01 -0800886static int s_show(struct seq_file *m, void *p)
887{
888 struct kmem_cache *s = list_entry(p, struct kmem_cache, list);
889
890 if (!is_root_cache(s))
891 return 0;
892 return cache_show(s, m);
893}
894
Glauber Costab7454ad2012-10-19 18:20:25 +0400895/*
896 * slabinfo_op - iterator that generates /proc/slabinfo
897 *
898 * Output layout:
899 * cache-name
900 * num-active-objs
901 * total-objs
902 * object size
903 * num-active-slabs
904 * total-slabs
905 * num-pages-per-slab
906 * + further values on SMP and with statistics enabled
907 */
908static const struct seq_operations slabinfo_op = {
909 .start = s_start,
Wanpeng Li276a2432013-07-08 08:08:28 +0800910 .next = slab_next,
911 .stop = slab_stop,
Glauber Costab7454ad2012-10-19 18:20:25 +0400912 .show = s_show,
913};
914
915static int slabinfo_open(struct inode *inode, struct file *file)
916{
917 return seq_open(file, &slabinfo_op);
918}
919
920static const struct file_operations proc_slabinfo_operations = {
921 .open = slabinfo_open,
922 .read = seq_read,
923 .write = slabinfo_write,
924 .llseek = seq_lseek,
925 .release = seq_release,
926};
927
928static int __init slab_proc_init(void)
929{
Wanpeng Lie9b4db22013-07-04 08:33:24 +0800930 proc_create("slabinfo", SLABINFO_RIGHTS, NULL,
931 &proc_slabinfo_operations);
Glauber Costab7454ad2012-10-19 18:20:25 +0400932 return 0;
933}
934module_init(slab_proc_init);
935#endif /* CONFIG_SLABINFO */
Andrey Ryabinin928cec92014-08-06 16:04:44 -0700936
937static __always_inline void *__do_krealloc(const void *p, size_t new_size,
938 gfp_t flags)
939{
940 void *ret;
941 size_t ks = 0;
942
943 if (p)
944 ks = ksize(p);
945
946 if (ks >= new_size)
947 return (void *)p;
948
949 ret = kmalloc_track_caller(new_size, flags);
950 if (ret && p)
951 memcpy(ret, p, ks);
952
953 return ret;
954}
955
956/**
957 * __krealloc - like krealloc() but don't free @p.
958 * @p: object to reallocate memory for.
959 * @new_size: how many bytes of memory are required.
960 * @flags: the type of memory to allocate.
961 *
962 * This function is like krealloc() except it never frees the originally
963 * allocated buffer. Use this if you don't want to free the buffer immediately
964 * like, for example, with RCU.
965 */
966void *__krealloc(const void *p, size_t new_size, gfp_t flags)
967{
968 if (unlikely(!new_size))
969 return ZERO_SIZE_PTR;
970
971 return __do_krealloc(p, new_size, flags);
972
973}
974EXPORT_SYMBOL(__krealloc);
975
976/**
977 * krealloc - reallocate memory. The contents will remain unchanged.
978 * @p: object to reallocate memory for.
979 * @new_size: how many bytes of memory are required.
980 * @flags: the type of memory to allocate.
981 *
982 * The contents of the object pointed to are preserved up to the
983 * lesser of the new and old sizes. If @p is %NULL, krealloc()
984 * behaves exactly like kmalloc(). If @new_size is 0 and @p is not a
985 * %NULL pointer, the object pointed to is freed.
986 */
987void *krealloc(const void *p, size_t new_size, gfp_t flags)
988{
989 void *ret;
990
991 if (unlikely(!new_size)) {
992 kfree(p);
993 return ZERO_SIZE_PTR;
994 }
995
996 ret = __do_krealloc(p, new_size, flags);
997 if (ret && p != ret)
998 kfree(p);
999
1000 return ret;
1001}
1002EXPORT_SYMBOL(krealloc);
1003
1004/**
1005 * kzfree - like kfree but zero memory
1006 * @p: object to free memory of
1007 *
1008 * The memory of the object @p points to is zeroed before freed.
1009 * If @p is %NULL, kzfree() does nothing.
1010 *
1011 * Note: this function zeroes the whole allocated buffer which can be a good
1012 * deal bigger than the requested buffer size passed to kmalloc(). So be
1013 * careful when using this function in performance sensitive code.
1014 */
1015void kzfree(const void *p)
1016{
1017 size_t ks;
1018 void *mem = (void *)p;
1019
1020 if (unlikely(ZERO_OR_NULL_PTR(mem)))
1021 return;
1022 ks = ksize(mem);
1023 memset(mem, 0, ks);
1024 kfree(mem);
1025}
1026EXPORT_SYMBOL(kzfree);
1027
1028/* Tracepoints definitions. */
1029EXPORT_TRACEPOINT_SYMBOL(kmalloc);
1030EXPORT_TRACEPOINT_SYMBOL(kmem_cache_alloc);
1031EXPORT_TRACEPOINT_SYMBOL(kmalloc_node);
1032EXPORT_TRACEPOINT_SYMBOL(kmem_cache_alloc_node);
1033EXPORT_TRACEPOINT_SYMBOL(kfree);
1034EXPORT_TRACEPOINT_SYMBOL(kmem_cache_free);