ad67a03c592ab8c4678021b77415532267a1b0be
[linux-2.6-microblaze.git] / mm / slab_common.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Slab allocator functions that are independent of the allocator strategy
4  *
5  * (C) 2012 Christoph Lameter <cl@linux.com>
6  */
7 #include <linux/slab.h>
8
9 #include <linux/mm.h>
10 #include <linux/poison.h>
11 #include <linux/interrupt.h>
12 #include <linux/memory.h>
13 #include <linux/cache.h>
14 #include <linux/compiler.h>
15 #include <linux/module.h>
16 #include <linux/cpu.h>
17 #include <linux/uaccess.h>
18 #include <linux/seq_file.h>
19 #include <linux/proc_fs.h>
20 #include <linux/debugfs.h>
21 #include <asm/cacheflush.h>
22 #include <asm/tlbflush.h>
23 #include <asm/page.h>
24 #include <linux/memcontrol.h>
25
26 #define CREATE_TRACE_POINTS
27 #include <trace/events/kmem.h>
28
29 #include "internal.h"
30
31 #include "slab.h"
32
33 enum slab_state slab_state;
34 LIST_HEAD(slab_caches);
35 DEFINE_MUTEX(slab_mutex);
36 struct kmem_cache *kmem_cache;
37
38 #ifdef CONFIG_HARDENED_USERCOPY
39 bool usercopy_fallback __ro_after_init =
40                 IS_ENABLED(CONFIG_HARDENED_USERCOPY_FALLBACK);
41 module_param(usercopy_fallback, bool, 0400);
42 MODULE_PARM_DESC(usercopy_fallback,
43                 "WARN instead of reject usercopy whitelist violations");
44 #endif
45
46 static LIST_HEAD(slab_caches_to_rcu_destroy);
47 static void slab_caches_to_rcu_destroy_workfn(struct work_struct *work);
48 static DECLARE_WORK(slab_caches_to_rcu_destroy_work,
49                     slab_caches_to_rcu_destroy_workfn);
50
51 /*
52  * Set of flags that will prevent slab merging
53  */
54 #define SLAB_NEVER_MERGE (SLAB_RED_ZONE | SLAB_POISON | SLAB_STORE_USER | \
55                 SLAB_TRACE | SLAB_TYPESAFE_BY_RCU | SLAB_NOLEAKTRACE | \
56                 SLAB_FAILSLAB | SLAB_KASAN)
57
58 #define SLAB_MERGE_SAME (SLAB_RECLAIM_ACCOUNT | SLAB_CACHE_DMA | \
59                          SLAB_CACHE_DMA32 | SLAB_ACCOUNT)
60
61 /*
62  * Merge control. If this is set then no merging of slab caches will occur.
63  */
64 static bool slab_nomerge = !IS_ENABLED(CONFIG_SLAB_MERGE_DEFAULT);
65
66 static int __init setup_slab_nomerge(char *str)
67 {
68         slab_nomerge = true;
69         return 1;
70 }
71
72 #ifdef CONFIG_SLUB
73 __setup_param("slub_nomerge", slub_nomerge, setup_slab_nomerge, 0);
74 #endif
75
76 __setup("slab_nomerge", setup_slab_nomerge);
77
78 /*
79  * Determine the size of a slab object
80  */
81 unsigned int kmem_cache_size(struct kmem_cache *s)
82 {
83         return s->object_size;
84 }
85 EXPORT_SYMBOL(kmem_cache_size);
86
87 #ifdef CONFIG_DEBUG_VM
88 static int kmem_cache_sanity_check(const char *name, unsigned int size)
89 {
90         if (!name || in_interrupt() || size < sizeof(void *) ||
91                 size > KMALLOC_MAX_SIZE) {
92                 pr_err("kmem_cache_create(%s) integrity check failed\n", name);
93                 return -EINVAL;
94         }
95
96         WARN_ON(strchr(name, ' '));     /* It confuses parsers */
97         return 0;
98 }
99 #else
100 static inline int kmem_cache_sanity_check(const char *name, unsigned int size)
101 {
102         return 0;
103 }
104 #endif
105
106 void __kmem_cache_free_bulk(struct kmem_cache *s, size_t nr, void **p)
107 {
108         size_t i;
109
110         for (i = 0; i < nr; i++) {
111                 if (s)
112                         kmem_cache_free(s, p[i]);
113                 else
114                         kfree(p[i]);
115         }
116 }
117
118 int __kmem_cache_alloc_bulk(struct kmem_cache *s, gfp_t flags, size_t nr,
119                                                                 void **p)
120 {
121         size_t i;
122
123         for (i = 0; i < nr; i++) {
124                 void *x = p[i] = kmem_cache_alloc(s, flags);
125                 if (!x) {
126                         __kmem_cache_free_bulk(s, i, p);
127                         return 0;
128                 }
129         }
130         return i;
131 }
132
133 #ifdef CONFIG_MEMCG_KMEM
134 static void memcg_kmem_cache_create_func(struct work_struct *work)
135 {
136         struct kmem_cache *cachep = container_of(work, struct kmem_cache,
137                                                  memcg_params.work);
138         memcg_create_kmem_cache(cachep);
139 }
140
141 void slab_init_memcg_params(struct kmem_cache *s)
142 {
143         s->memcg_params.root_cache = NULL;
144         s->memcg_params.memcg_cache = NULL;
145         INIT_WORK(&s->memcg_params.work, memcg_kmem_cache_create_func);
146 }
147
148 static void init_memcg_params(struct kmem_cache *s,
149                               struct kmem_cache *root_cache)
150 {
151         if (root_cache)
152                 s->memcg_params.root_cache = root_cache;
153         else
154                 slab_init_memcg_params(s);
155 }
156 #else
157 static inline void init_memcg_params(struct kmem_cache *s,
158                                      struct kmem_cache *root_cache)
159 {
160 }
161 #endif /* CONFIG_MEMCG_KMEM */
162
163 /*
164  * Figure out what the alignment of the objects will be given a set of
165  * flags, a user specified alignment and the size of the objects.
166  */
167 static unsigned int calculate_alignment(slab_flags_t flags,
168                 unsigned int align, unsigned int size)
169 {
170         /*
171          * If the user wants hardware cache aligned objects then follow that
172          * suggestion if the object is sufficiently large.
173          *
174          * The hardware cache alignment cannot override the specified
175          * alignment though. If that is greater then use it.
176          */
177         if (flags & SLAB_HWCACHE_ALIGN) {
178                 unsigned int ralign;
179
180                 ralign = cache_line_size();
181                 while (size <= ralign / 2)
182                         ralign /= 2;
183                 align = max(align, ralign);
184         }
185
186         if (align < ARCH_SLAB_MINALIGN)
187                 align = ARCH_SLAB_MINALIGN;
188
189         return ALIGN(align, sizeof(void *));
190 }
191
192 /*
193  * Find a mergeable slab cache
194  */
195 int slab_unmergeable(struct kmem_cache *s)
196 {
197         if (slab_nomerge || (s->flags & SLAB_NEVER_MERGE))
198                 return 1;
199
200         if (!is_root_cache(s))
201                 return 1;
202
203         if (s->ctor)
204                 return 1;
205
206         if (s->usersize)
207                 return 1;
208
209         /*
210          * We may have set a slab to be unmergeable during bootstrap.
211          */
212         if (s->refcount < 0)
213                 return 1;
214
215         return 0;
216 }
217
218 struct kmem_cache *find_mergeable(unsigned int size, unsigned int align,
219                 slab_flags_t flags, const char *name, void (*ctor)(void *))
220 {
221         struct kmem_cache *s;
222
223         if (slab_nomerge)
224                 return NULL;
225
226         if (ctor)
227                 return NULL;
228
229         size = ALIGN(size, sizeof(void *));
230         align = calculate_alignment(flags, align, size);
231         size = ALIGN(size, align);
232         flags = kmem_cache_flags(size, flags, name, NULL);
233
234         if (flags & SLAB_NEVER_MERGE)
235                 return NULL;
236
237         list_for_each_entry_reverse(s, &slab_caches, list) {
238                 if (slab_unmergeable(s))
239                         continue;
240
241                 if (size > s->size)
242                         continue;
243
244                 if ((flags & SLAB_MERGE_SAME) != (s->flags & SLAB_MERGE_SAME))
245                         continue;
246                 /*
247                  * Check if alignment is compatible.
248                  * Courtesy of Adrian Drzewiecki
249                  */
250                 if ((s->size & ~(align - 1)) != s->size)
251                         continue;
252
253                 if (s->size - size >= sizeof(void *))
254                         continue;
255
256                 if (IS_ENABLED(CONFIG_SLAB) && align &&
257                         (align > s->align || s->align % align))
258                         continue;
259
260                 return s;
261         }
262         return NULL;
263 }
264
265 static struct kmem_cache *create_cache(const char *name,
266                 unsigned int object_size, unsigned int align,
267                 slab_flags_t flags, unsigned int useroffset,
268                 unsigned int usersize, void (*ctor)(void *),
269                 struct kmem_cache *root_cache)
270 {
271         struct kmem_cache *s;
272         int err;
273
274         if (WARN_ON(useroffset + usersize > object_size))
275                 useroffset = usersize = 0;
276
277         err = -ENOMEM;
278         s = kmem_cache_zalloc(kmem_cache, GFP_KERNEL);
279         if (!s)
280                 goto out;
281
282         s->name = name;
283         s->size = s->object_size = object_size;
284         s->align = align;
285         s->ctor = ctor;
286         s->useroffset = useroffset;
287         s->usersize = usersize;
288
289         init_memcg_params(s, root_cache);
290         err = __kmem_cache_create(s, flags);
291         if (err)
292                 goto out_free_cache;
293
294         s->refcount = 1;
295         list_add(&s->list, &slab_caches);
296 out:
297         if (err)
298                 return ERR_PTR(err);
299         return s;
300
301 out_free_cache:
302         kmem_cache_free(kmem_cache, s);
303         goto out;
304 }
305
306 /**
307  * kmem_cache_create_usercopy - Create a cache with a region suitable
308  * for copying to userspace
309  * @name: A string which is used in /proc/slabinfo to identify this cache.
310  * @size: The size of objects to be created in this cache.
311  * @align: The required alignment for the objects.
312  * @flags: SLAB flags
313  * @useroffset: Usercopy region offset
314  * @usersize: Usercopy region size
315  * @ctor: A constructor for the objects.
316  *
317  * Cannot be called within a interrupt, but can be interrupted.
318  * The @ctor is run when new pages are allocated by the cache.
319  *
320  * The flags are
321  *
322  * %SLAB_POISON - Poison the slab with a known test pattern (a5a5a5a5)
323  * to catch references to uninitialised memory.
324  *
325  * %SLAB_RED_ZONE - Insert `Red` zones around the allocated memory to check
326  * for buffer overruns.
327  *
328  * %SLAB_HWCACHE_ALIGN - Align the objects in this cache to a hardware
329  * cacheline.  This can be beneficial if you're counting cycles as closely
330  * as davem.
331  *
332  * Return: a pointer to the cache on success, NULL on failure.
333  */
334 struct kmem_cache *
335 kmem_cache_create_usercopy(const char *name,
336                   unsigned int size, unsigned int align,
337                   slab_flags_t flags,
338                   unsigned int useroffset, unsigned int usersize,
339                   void (*ctor)(void *))
340 {
341         struct kmem_cache *s = NULL;
342         const char *cache_name;
343         int err;
344
345         get_online_cpus();
346         get_online_mems();
347         memcg_get_cache_ids();
348
349         mutex_lock(&slab_mutex);
350
351         err = kmem_cache_sanity_check(name, size);
352         if (err) {
353                 goto out_unlock;
354         }
355
356         /* Refuse requests with allocator specific flags */
357         if (flags & ~SLAB_FLAGS_PERMITTED) {
358                 err = -EINVAL;
359                 goto out_unlock;
360         }
361
362         /*
363          * Some allocators will constraint the set of valid flags to a subset
364          * of all flags. We expect them to define CACHE_CREATE_MASK in this
365          * case, and we'll just provide them with a sanitized version of the
366          * passed flags.
367          */
368         flags &= CACHE_CREATE_MASK;
369
370         /* Fail closed on bad usersize of useroffset values. */
371         if (WARN_ON(!usersize && useroffset) ||
372             WARN_ON(size < usersize || size - usersize < useroffset))
373                 usersize = useroffset = 0;
374
375         if (!usersize)
376                 s = __kmem_cache_alias(name, size, align, flags, ctor);
377         if (s)
378                 goto out_unlock;
379
380         cache_name = kstrdup_const(name, GFP_KERNEL);
381         if (!cache_name) {
382                 err = -ENOMEM;
383                 goto out_unlock;
384         }
385
386         s = create_cache(cache_name, size,
387                          calculate_alignment(flags, align, size),
388                          flags, useroffset, usersize, ctor, NULL);
389         if (IS_ERR(s)) {
390                 err = PTR_ERR(s);
391                 kfree_const(cache_name);
392         }
393
394 out_unlock:
395         mutex_unlock(&slab_mutex);
396
397         memcg_put_cache_ids();
398         put_online_mems();
399         put_online_cpus();
400
401         if (err) {
402                 if (flags & SLAB_PANIC)
403                         panic("kmem_cache_create: Failed to create slab '%s'. Error %d\n",
404                                 name, err);
405                 else {
406                         pr_warn("kmem_cache_create(%s) failed with error %d\n",
407                                 name, err);
408                         dump_stack();
409                 }
410                 return NULL;
411         }
412         return s;
413 }
414 EXPORT_SYMBOL(kmem_cache_create_usercopy);
415
416 /**
417  * kmem_cache_create - Create a cache.
418  * @name: A string which is used in /proc/slabinfo to identify this cache.
419  * @size: The size of objects to be created in this cache.
420  * @align: The required alignment for the objects.
421  * @flags: SLAB flags
422  * @ctor: A constructor for the objects.
423  *
424  * Cannot be called within a interrupt, but can be interrupted.
425  * The @ctor is run when new pages are allocated by the cache.
426  *
427  * The flags are
428  *
429  * %SLAB_POISON - Poison the slab with a known test pattern (a5a5a5a5)
430  * to catch references to uninitialised memory.
431  *
432  * %SLAB_RED_ZONE - Insert `Red` zones around the allocated memory to check
433  * for buffer overruns.
434  *
435  * %SLAB_HWCACHE_ALIGN - Align the objects in this cache to a hardware
436  * cacheline.  This can be beneficial if you're counting cycles as closely
437  * as davem.
438  *
439  * Return: a pointer to the cache on success, NULL on failure.
440  */
441 struct kmem_cache *
442 kmem_cache_create(const char *name, unsigned int size, unsigned int align,
443                 slab_flags_t flags, void (*ctor)(void *))
444 {
445         return kmem_cache_create_usercopy(name, size, align, flags, 0, 0,
446                                           ctor);
447 }
448 EXPORT_SYMBOL(kmem_cache_create);
449
450 static void slab_caches_to_rcu_destroy_workfn(struct work_struct *work)
451 {
452         LIST_HEAD(to_destroy);
453         struct kmem_cache *s, *s2;
454
455         /*
456          * On destruction, SLAB_TYPESAFE_BY_RCU kmem_caches are put on the
457          * @slab_caches_to_rcu_destroy list.  The slab pages are freed
458          * through RCU and and the associated kmem_cache are dereferenced
459          * while freeing the pages, so the kmem_caches should be freed only
460          * after the pending RCU operations are finished.  As rcu_barrier()
461          * is a pretty slow operation, we batch all pending destructions
462          * asynchronously.
463          */
464         mutex_lock(&slab_mutex);
465         list_splice_init(&slab_caches_to_rcu_destroy, &to_destroy);
466         mutex_unlock(&slab_mutex);
467
468         if (list_empty(&to_destroy))
469                 return;
470
471         rcu_barrier();
472
473         list_for_each_entry_safe(s, s2, &to_destroy, list) {
474 #ifdef SLAB_SUPPORTS_SYSFS
475                 sysfs_slab_release(s);
476 #else
477                 slab_kmem_cache_release(s);
478 #endif
479         }
480 }
481
482 static int shutdown_cache(struct kmem_cache *s)
483 {
484         /* free asan quarantined objects */
485         kasan_cache_shutdown(s);
486
487         if (__kmem_cache_shutdown(s) != 0)
488                 return -EBUSY;
489
490         list_del(&s->list);
491
492         if (s->flags & SLAB_TYPESAFE_BY_RCU) {
493 #ifdef SLAB_SUPPORTS_SYSFS
494                 sysfs_slab_unlink(s);
495 #endif
496                 list_add_tail(&s->list, &slab_caches_to_rcu_destroy);
497                 schedule_work(&slab_caches_to_rcu_destroy_work);
498         } else {
499 #ifdef SLAB_SUPPORTS_SYSFS
500                 sysfs_slab_unlink(s);
501                 sysfs_slab_release(s);
502 #else
503                 slab_kmem_cache_release(s);
504 #endif
505         }
506
507         return 0;
508 }
509
510 #ifdef CONFIG_MEMCG_KMEM
511 /*
512  * memcg_create_kmem_cache - Create a cache for non-root memory cgroups.
513  * @root_cache: The parent of the new cache.
514  *
515  * This function attempts to create a kmem cache that will serve allocation
516  * requests going all non-root memory cgroups to @root_cache. The new cache
517  * inherits properties from its parent.
518  */
519 void memcg_create_kmem_cache(struct kmem_cache *root_cache)
520 {
521         struct kmem_cache *s = NULL;
522         char *cache_name;
523
524         get_online_cpus();
525         get_online_mems();
526
527         mutex_lock(&slab_mutex);
528
529         if (root_cache->memcg_params.memcg_cache)
530                 goto out_unlock;
531
532         cache_name = kasprintf(GFP_KERNEL, "%s-memcg", root_cache->name);
533         if (!cache_name)
534                 goto out_unlock;
535
536         s = create_cache(cache_name, root_cache->object_size,
537                          root_cache->align,
538                          root_cache->flags & CACHE_CREATE_MASK,
539                          root_cache->useroffset, root_cache->usersize,
540                          root_cache->ctor, root_cache);
541         /*
542          * If we could not create a memcg cache, do not complain, because
543          * that's not critical at all as we can always proceed with the root
544          * cache.
545          */
546         if (IS_ERR(s)) {
547                 kfree(cache_name);
548                 goto out_unlock;
549         }
550
551         /*
552          * Since readers won't lock (see memcg_slab_pre_alloc_hook()), we need a
553          * barrier here to ensure nobody will see the kmem_cache partially
554          * initialized.
555          */
556         smp_wmb();
557         root_cache->memcg_params.memcg_cache = s;
558
559 out_unlock:
560         mutex_unlock(&slab_mutex);
561
562         put_online_mems();
563         put_online_cpus();
564 }
565
566 static int shutdown_memcg_caches(struct kmem_cache *s)
567 {
568         BUG_ON(!is_root_cache(s));
569
570         if (s->memcg_params.memcg_cache)
571                 WARN_ON(shutdown_cache(s->memcg_params.memcg_cache));
572
573         return 0;
574 }
575
576 static void cancel_memcg_cache_creation(struct kmem_cache *s)
577 {
578         cancel_work_sync(&s->memcg_params.work);
579 }
580 #else
581 static inline int shutdown_memcg_caches(struct kmem_cache *s)
582 {
583         return 0;
584 }
585
586 static inline void cancel_memcg_cache_creation(struct kmem_cache *s)
587 {
588 }
589 #endif /* CONFIG_MEMCG_KMEM */
590
591 void slab_kmem_cache_release(struct kmem_cache *s)
592 {
593         __kmem_cache_release(s);
594         kfree_const(s->name);
595         kmem_cache_free(kmem_cache, s);
596 }
597
598 void kmem_cache_destroy(struct kmem_cache *s)
599 {
600         int err;
601
602         if (unlikely(!s))
603                 return;
604
605         cancel_memcg_cache_creation(s);
606
607         get_online_cpus();
608         get_online_mems();
609
610         mutex_lock(&slab_mutex);
611
612         s->refcount--;
613         if (s->refcount)
614                 goto out_unlock;
615
616         err = shutdown_memcg_caches(s);
617         if (!err)
618                 err = shutdown_cache(s);
619
620         if (err) {
621                 pr_err("kmem_cache_destroy %s: Slab cache still has objects\n",
622                        s->name);
623                 dump_stack();
624         }
625 out_unlock:
626         mutex_unlock(&slab_mutex);
627
628         put_online_mems();
629         put_online_cpus();
630 }
631 EXPORT_SYMBOL(kmem_cache_destroy);
632
633 /**
634  * kmem_cache_shrink - Shrink a cache.
635  * @cachep: The cache to shrink.
636  *
637  * Releases as many slabs as possible for a cache.
638  * To help debugging, a zero exit status indicates all slabs were released.
639  *
640  * Return: %0 if all slabs were released, non-zero otherwise
641  */
642 int kmem_cache_shrink(struct kmem_cache *cachep)
643 {
644         int ret;
645
646         get_online_cpus();
647         get_online_mems();
648         kasan_cache_shrink(cachep);
649         ret = __kmem_cache_shrink(cachep);
650         put_online_mems();
651         put_online_cpus();
652         return ret;
653 }
654 EXPORT_SYMBOL(kmem_cache_shrink);
655
656 /**
657  * kmem_cache_shrink_all - shrink root and memcg caches
658  * @s: The cache pointer
659  */
660 void kmem_cache_shrink_all(struct kmem_cache *s)
661 {
662         struct kmem_cache *c;
663
664         if (!IS_ENABLED(CONFIG_MEMCG_KMEM) || !is_root_cache(s)) {
665                 kmem_cache_shrink(s);
666                 return;
667         }
668
669         get_online_cpus();
670         get_online_mems();
671         kasan_cache_shrink(s);
672         __kmem_cache_shrink(s);
673
674         c = memcg_cache(s);
675         if (c) {
676                 kasan_cache_shrink(c);
677                 __kmem_cache_shrink(c);
678         }
679         put_online_mems();
680         put_online_cpus();
681 }
682
683 bool slab_is_available(void)
684 {
685         return slab_state >= UP;
686 }
687
688 #ifndef CONFIG_SLOB
689 /* Create a cache during boot when no slab services are available yet */
690 void __init create_boot_cache(struct kmem_cache *s, const char *name,
691                 unsigned int size, slab_flags_t flags,
692                 unsigned int useroffset, unsigned int usersize)
693 {
694         int err;
695         unsigned int align = ARCH_KMALLOC_MINALIGN;
696
697         s->name = name;
698         s->size = s->object_size = size;
699
700         /*
701          * For power of two sizes, guarantee natural alignment for kmalloc
702          * caches, regardless of SL*B debugging options.
703          */
704         if (is_power_of_2(size))
705                 align = max(align, size);
706         s->align = calculate_alignment(flags, align, size);
707
708         s->useroffset = useroffset;
709         s->usersize = usersize;
710
711         slab_init_memcg_params(s);
712
713         err = __kmem_cache_create(s, flags);
714
715         if (err)
716                 panic("Creation of kmalloc slab %s size=%u failed. Reason %d\n",
717                                         name, size, err);
718
719         s->refcount = -1;       /* Exempt from merging for now */
720 }
721
722 struct kmem_cache *__init create_kmalloc_cache(const char *name,
723                 unsigned int size, slab_flags_t flags,
724                 unsigned int useroffset, unsigned int usersize)
725 {
726         struct kmem_cache *s = kmem_cache_zalloc(kmem_cache, GFP_NOWAIT);
727
728         if (!s)
729                 panic("Out of memory when creating slab %s\n", name);
730
731         create_boot_cache(s, name, size, flags, useroffset, usersize);
732         list_add(&s->list, &slab_caches);
733         s->refcount = 1;
734         return s;
735 }
736
737 struct kmem_cache *
738 kmalloc_caches[NR_KMALLOC_TYPES][KMALLOC_SHIFT_HIGH + 1] __ro_after_init =
739 { /* initialization for https://bugs.llvm.org/show_bug.cgi?id=42570 */ };
740 EXPORT_SYMBOL(kmalloc_caches);
741
742 /*
743  * Conversion table for small slabs sizes / 8 to the index in the
744  * kmalloc array. This is necessary for slabs < 192 since we have non power
745  * of two cache sizes there. The size of larger slabs can be determined using
746  * fls.
747  */
748 static u8 size_index[24] __ro_after_init = {
749         3,      /* 8 */
750         4,      /* 16 */
751         5,      /* 24 */
752         5,      /* 32 */
753         6,      /* 40 */
754         6,      /* 48 */
755         6,      /* 56 */
756         6,      /* 64 */
757         1,      /* 72 */
758         1,      /* 80 */
759         1,      /* 88 */
760         1,      /* 96 */
761         7,      /* 104 */
762         7,      /* 112 */
763         7,      /* 120 */
764         7,      /* 128 */
765         2,      /* 136 */
766         2,      /* 144 */
767         2,      /* 152 */
768         2,      /* 160 */
769         2,      /* 168 */
770         2,      /* 176 */
771         2,      /* 184 */
772         2       /* 192 */
773 };
774
775 static inline unsigned int size_index_elem(unsigned int bytes)
776 {
777         return (bytes - 1) / 8;
778 }
779
780 /*
781  * Find the kmem_cache structure that serves a given size of
782  * allocation
783  */
784 struct kmem_cache *kmalloc_slab(size_t size, gfp_t flags)
785 {
786         unsigned int index;
787
788         if (size <= 192) {
789                 if (!size)
790                         return ZERO_SIZE_PTR;
791
792                 index = size_index[size_index_elem(size)];
793         } else {
794                 if (WARN_ON_ONCE(size > KMALLOC_MAX_CACHE_SIZE))
795                         return NULL;
796                 index = fls(size - 1);
797         }
798
799         return kmalloc_caches[kmalloc_type(flags)][index];
800 }
801
802 #ifdef CONFIG_ZONE_DMA
803 #define INIT_KMALLOC_INFO(__size, __short_size)                 \
804 {                                                               \
805         .name[KMALLOC_NORMAL]  = "kmalloc-" #__short_size,      \
806         .name[KMALLOC_RECLAIM] = "kmalloc-rcl-" #__short_size,  \
807         .name[KMALLOC_DMA]     = "dma-kmalloc-" #__short_size,  \
808         .size = __size,                                         \
809 }
810 #else
811 #define INIT_KMALLOC_INFO(__size, __short_size)                 \
812 {                                                               \
813         .name[KMALLOC_NORMAL]  = "kmalloc-" #__short_size,      \
814         .name[KMALLOC_RECLAIM] = "kmalloc-rcl-" #__short_size,  \
815         .size = __size,                                         \
816 }
817 #endif
818
819 /*
820  * kmalloc_info[] is to make slub_debug=,kmalloc-xx option work at boot time.
821  * kmalloc_index() supports up to 2^26=64MB, so the final entry of the table is
822  * kmalloc-67108864.
823  */
824 const struct kmalloc_info_struct kmalloc_info[] __initconst = {
825         INIT_KMALLOC_INFO(0, 0),
826         INIT_KMALLOC_INFO(96, 96),
827         INIT_KMALLOC_INFO(192, 192),
828         INIT_KMALLOC_INFO(8, 8),
829         INIT_KMALLOC_INFO(16, 16),
830         INIT_KMALLOC_INFO(32, 32),
831         INIT_KMALLOC_INFO(64, 64),
832         INIT_KMALLOC_INFO(128, 128),
833         INIT_KMALLOC_INFO(256, 256),
834         INIT_KMALLOC_INFO(512, 512),
835         INIT_KMALLOC_INFO(1024, 1k),
836         INIT_KMALLOC_INFO(2048, 2k),
837         INIT_KMALLOC_INFO(4096, 4k),
838         INIT_KMALLOC_INFO(8192, 8k),
839         INIT_KMALLOC_INFO(16384, 16k),
840         INIT_KMALLOC_INFO(32768, 32k),
841         INIT_KMALLOC_INFO(65536, 64k),
842         INIT_KMALLOC_INFO(131072, 128k),
843         INIT_KMALLOC_INFO(262144, 256k),
844         INIT_KMALLOC_INFO(524288, 512k),
845         INIT_KMALLOC_INFO(1048576, 1M),
846         INIT_KMALLOC_INFO(2097152, 2M),
847         INIT_KMALLOC_INFO(4194304, 4M),
848         INIT_KMALLOC_INFO(8388608, 8M),
849         INIT_KMALLOC_INFO(16777216, 16M),
850         INIT_KMALLOC_INFO(33554432, 32M),
851         INIT_KMALLOC_INFO(67108864, 64M)
852 };
853
854 /*
855  * Patch up the size_index table if we have strange large alignment
856  * requirements for the kmalloc array. This is only the case for
857  * MIPS it seems. The standard arches will not generate any code here.
858  *
859  * Largest permitted alignment is 256 bytes due to the way we
860  * handle the index determination for the smaller caches.
861  *
862  * Make sure that nothing crazy happens if someone starts tinkering
863  * around with ARCH_KMALLOC_MINALIGN
864  */
865 void __init setup_kmalloc_cache_index_table(void)
866 {
867         unsigned int i;
868
869         BUILD_BUG_ON(KMALLOC_MIN_SIZE > 256 ||
870                 (KMALLOC_MIN_SIZE & (KMALLOC_MIN_SIZE - 1)));
871
872         for (i = 8; i < KMALLOC_MIN_SIZE; i += 8) {
873                 unsigned int elem = size_index_elem(i);
874
875                 if (elem >= ARRAY_SIZE(size_index))
876                         break;
877                 size_index[elem] = KMALLOC_SHIFT_LOW;
878         }
879
880         if (KMALLOC_MIN_SIZE >= 64) {
881                 /*
882                  * The 96 byte size cache is not used if the alignment
883                  * is 64 byte.
884                  */
885                 for (i = 64 + 8; i <= 96; i += 8)
886                         size_index[size_index_elem(i)] = 7;
887
888         }
889
890         if (KMALLOC_MIN_SIZE >= 128) {
891                 /*
892                  * The 192 byte sized cache is not used if the alignment
893                  * is 128 byte. Redirect kmalloc to use the 256 byte cache
894                  * instead.
895                  */
896                 for (i = 128 + 8; i <= 192; i += 8)
897                         size_index[size_index_elem(i)] = 8;
898         }
899 }
900
901 static void __init
902 new_kmalloc_cache(int idx, enum kmalloc_cache_type type, slab_flags_t flags)
903 {
904         if (type == KMALLOC_RECLAIM)
905                 flags |= SLAB_RECLAIM_ACCOUNT;
906
907         kmalloc_caches[type][idx] = create_kmalloc_cache(
908                                         kmalloc_info[idx].name[type],
909                                         kmalloc_info[idx].size, flags, 0,
910                                         kmalloc_info[idx].size);
911 }
912
913 /*
914  * Create the kmalloc array. Some of the regular kmalloc arrays
915  * may already have been created because they were needed to
916  * enable allocations for slab creation.
917  */
918 void __init create_kmalloc_caches(slab_flags_t flags)
919 {
920         int i;
921         enum kmalloc_cache_type type;
922
923         for (type = KMALLOC_NORMAL; type <= KMALLOC_RECLAIM; type++) {
924                 for (i = KMALLOC_SHIFT_LOW; i <= KMALLOC_SHIFT_HIGH; i++) {
925                         if (!kmalloc_caches[type][i])
926                                 new_kmalloc_cache(i, type, flags);
927
928                         /*
929                          * Caches that are not of the two-to-the-power-of size.
930                          * These have to be created immediately after the
931                          * earlier power of two caches
932                          */
933                         if (KMALLOC_MIN_SIZE <= 32 && i == 6 &&
934                                         !kmalloc_caches[type][1])
935                                 new_kmalloc_cache(1, type, flags);
936                         if (KMALLOC_MIN_SIZE <= 64 && i == 7 &&
937                                         !kmalloc_caches[type][2])
938                                 new_kmalloc_cache(2, type, flags);
939                 }
940         }
941
942         /* Kmalloc array is now usable */
943         slab_state = UP;
944
945 #ifdef CONFIG_ZONE_DMA
946         for (i = 0; i <= KMALLOC_SHIFT_HIGH; i++) {
947                 struct kmem_cache *s = kmalloc_caches[KMALLOC_NORMAL][i];
948
949                 if (s) {
950                         kmalloc_caches[KMALLOC_DMA][i] = create_kmalloc_cache(
951                                 kmalloc_info[i].name[KMALLOC_DMA],
952                                 kmalloc_info[i].size,
953                                 SLAB_CACHE_DMA | flags, 0,
954                                 kmalloc_info[i].size);
955                 }
956         }
957 #endif
958 }
959 #endif /* !CONFIG_SLOB */
960
961 gfp_t kmalloc_fix_flags(gfp_t flags)
962 {
963         gfp_t invalid_mask = flags & GFP_SLAB_BUG_MASK;
964
965         flags &= ~GFP_SLAB_BUG_MASK;
966         pr_warn("Unexpected gfp: %#x (%pGg). Fixing up to gfp: %#x (%pGg). Fix your code!\n",
967                         invalid_mask, &invalid_mask, flags, &flags);
968         dump_stack();
969
970         return flags;
971 }
972
973 /*
974  * To avoid unnecessary overhead, we pass through large allocation requests
975  * directly to the page allocator. We use __GFP_COMP, because we will need to
976  * know the allocation order to free the pages properly in kfree.
977  */
978 void *kmalloc_order(size_t size, gfp_t flags, unsigned int order)
979 {
980         void *ret = NULL;
981         struct page *page;
982
983         if (unlikely(flags & GFP_SLAB_BUG_MASK))
984                 flags = kmalloc_fix_flags(flags);
985
986         flags |= __GFP_COMP;
987         page = alloc_pages(flags, order);
988         if (likely(page)) {
989                 ret = page_address(page);
990                 mod_node_page_state(page_pgdat(page), NR_SLAB_UNRECLAIMABLE_B,
991                                     PAGE_SIZE << order);
992         }
993         ret = kasan_kmalloc_large(ret, size, flags);
994         /* As ret might get tagged, call kmemleak hook after KASAN. */
995         kmemleak_alloc(ret, size, 1, flags);
996         return ret;
997 }
998 EXPORT_SYMBOL(kmalloc_order);
999
1000 #ifdef CONFIG_TRACING
1001 void *kmalloc_order_trace(size_t size, gfp_t flags, unsigned int order)
1002 {
1003         void *ret = kmalloc_order(size, flags, order);
1004         trace_kmalloc(_RET_IP_, ret, size, PAGE_SIZE << order, flags);
1005         return ret;
1006 }
1007 EXPORT_SYMBOL(kmalloc_order_trace);
1008 #endif
1009
1010 #ifdef CONFIG_SLAB_FREELIST_RANDOM
1011 /* Randomize a generic freelist */
1012 static void freelist_randomize(struct rnd_state *state, unsigned int *list,
1013                                unsigned int count)
1014 {
1015         unsigned int rand;
1016         unsigned int i;
1017
1018         for (i = 0; i < count; i++)
1019                 list[i] = i;
1020
1021         /* Fisher-Yates shuffle */
1022         for (i = count - 1; i > 0; i--) {
1023                 rand = prandom_u32_state(state);
1024                 rand %= (i + 1);
1025                 swap(list[i], list[rand]);
1026         }
1027 }
1028
1029 /* Create a random sequence per cache */
1030 int cache_random_seq_create(struct kmem_cache *cachep, unsigned int count,
1031                                     gfp_t gfp)
1032 {
1033         struct rnd_state state;
1034
1035         if (count < 2 || cachep->random_seq)
1036                 return 0;
1037
1038         cachep->random_seq = kcalloc(count, sizeof(unsigned int), gfp);
1039         if (!cachep->random_seq)
1040                 return -ENOMEM;
1041
1042         /* Get best entropy at this stage of boot */
1043         prandom_seed_state(&state, get_random_long());
1044
1045         freelist_randomize(&state, cachep->random_seq, count);
1046         return 0;
1047 }
1048
1049 /* Destroy the per-cache random freelist sequence */
1050 void cache_random_seq_destroy(struct kmem_cache *cachep)
1051 {
1052         kfree(cachep->random_seq);
1053         cachep->random_seq = NULL;
1054 }
1055 #endif /* CONFIG_SLAB_FREELIST_RANDOM */
1056
1057 #if defined(CONFIG_SLAB) || defined(CONFIG_SLUB_DEBUG)
1058 #ifdef CONFIG_SLAB
1059 #define SLABINFO_RIGHTS (0600)
1060 #else
1061 #define SLABINFO_RIGHTS (0400)
1062 #endif
1063
1064 static void print_slabinfo_header(struct seq_file *m)
1065 {
1066         /*
1067          * Output format version, so at least we can change it
1068          * without _too_ many complaints.
1069          */
1070 #ifdef CONFIG_DEBUG_SLAB
1071         seq_puts(m, "slabinfo - version: 2.1 (statistics)\n");
1072 #else
1073         seq_puts(m, "slabinfo - version: 2.1\n");
1074 #endif
1075         seq_puts(m, "# name            <active_objs> <num_objs> <objsize> <objperslab> <pagesperslab>");
1076         seq_puts(m, " : tunables <limit> <batchcount> <sharedfactor>");
1077         seq_puts(m, " : slabdata <active_slabs> <num_slabs> <sharedavail>");
1078 #ifdef CONFIG_DEBUG_SLAB
1079         seq_puts(m, " : globalstat <listallocs> <maxobjs> <grown> <reaped> <error> <maxfreeable> <nodeallocs> <remotefrees> <alienoverflow>");
1080         seq_puts(m, " : cpustat <allochit> <allocmiss> <freehit> <freemiss>");
1081 #endif
1082         seq_putc(m, '\n');
1083 }
1084
1085 void *slab_start(struct seq_file *m, loff_t *pos)
1086 {
1087         mutex_lock(&slab_mutex);
1088         return seq_list_start(&slab_caches, *pos);
1089 }
1090
1091 void *slab_next(struct seq_file *m, void *p, loff_t *pos)
1092 {
1093         return seq_list_next(p, &slab_caches, pos);
1094 }
1095
1096 void slab_stop(struct seq_file *m, void *p)
1097 {
1098         mutex_unlock(&slab_mutex);
1099 }
1100
1101 static void
1102 memcg_accumulate_slabinfo(struct kmem_cache *s, struct slabinfo *info)
1103 {
1104         struct kmem_cache *c;
1105         struct slabinfo sinfo;
1106
1107         c = memcg_cache(s);
1108         if (c) {
1109                 memset(&sinfo, 0, sizeof(sinfo));
1110                 get_slabinfo(c, &sinfo);
1111
1112                 info->active_slabs += sinfo.active_slabs;
1113                 info->num_slabs += sinfo.num_slabs;
1114                 info->shared_avail += sinfo.shared_avail;
1115                 info->active_objs += sinfo.active_objs;
1116                 info->num_objs += sinfo.num_objs;
1117         }
1118 }
1119
1120 static void cache_show(struct kmem_cache *s, struct seq_file *m)
1121 {
1122         struct slabinfo sinfo;
1123
1124         memset(&sinfo, 0, sizeof(sinfo));
1125         get_slabinfo(s, &sinfo);
1126
1127         memcg_accumulate_slabinfo(s, &sinfo);
1128
1129         seq_printf(m, "%-17s %6lu %6lu %6u %4u %4d",
1130                    cache_name(s), sinfo.active_objs, sinfo.num_objs, s->size,
1131                    sinfo.objects_per_slab, (1 << sinfo.cache_order));
1132
1133         seq_printf(m, " : tunables %4u %4u %4u",
1134                    sinfo.limit, sinfo.batchcount, sinfo.shared);
1135         seq_printf(m, " : slabdata %6lu %6lu %6lu",
1136                    sinfo.active_slabs, sinfo.num_slabs, sinfo.shared_avail);
1137         slabinfo_show_stats(m, s);
1138         seq_putc(m, '\n');
1139 }
1140
1141 static int slab_show(struct seq_file *m, void *p)
1142 {
1143         struct kmem_cache *s = list_entry(p, struct kmem_cache, list);
1144
1145         if (p == slab_caches.next)
1146                 print_slabinfo_header(m);
1147         if (is_root_cache(s))
1148                 cache_show(s, m);
1149         return 0;
1150 }
1151
1152 void dump_unreclaimable_slab(void)
1153 {
1154         struct kmem_cache *s, *s2;
1155         struct slabinfo sinfo;
1156
1157         /*
1158          * Here acquiring slab_mutex is risky since we don't prefer to get
1159          * sleep in oom path. But, without mutex hold, it may introduce a
1160          * risk of crash.
1161          * Use mutex_trylock to protect the list traverse, dump nothing
1162          * without acquiring the mutex.
1163          */
1164         if (!mutex_trylock(&slab_mutex)) {
1165                 pr_warn("excessive unreclaimable slab but cannot dump stats\n");
1166                 return;
1167         }
1168
1169         pr_info("Unreclaimable slab info:\n");
1170         pr_info("Name                      Used          Total\n");
1171
1172         list_for_each_entry_safe(s, s2, &slab_caches, list) {
1173                 if (!is_root_cache(s) || (s->flags & SLAB_RECLAIM_ACCOUNT))
1174                         continue;
1175
1176                 get_slabinfo(s, &sinfo);
1177
1178                 if (sinfo.num_objs > 0)
1179                         pr_info("%-17s %10luKB %10luKB\n", cache_name(s),
1180                                 (sinfo.active_objs * s->size) / 1024,
1181                                 (sinfo.num_objs * s->size) / 1024);
1182         }
1183         mutex_unlock(&slab_mutex);
1184 }
1185
1186 #if defined(CONFIG_MEMCG_KMEM)
1187 int memcg_slab_show(struct seq_file *m, void *p)
1188 {
1189         /*
1190          * Deprecated.
1191          * Please, take a look at tools/cgroup/slabinfo.py .
1192          */
1193         return 0;
1194 }
1195 #endif
1196
1197 /*
1198  * slabinfo_op - iterator that generates /proc/slabinfo
1199  *
1200  * Output layout:
1201  * cache-name
1202  * num-active-objs
1203  * total-objs
1204  * object size
1205  * num-active-slabs
1206  * total-slabs
1207  * num-pages-per-slab
1208  * + further values on SMP and with statistics enabled
1209  */
1210 static const struct seq_operations slabinfo_op = {
1211         .start = slab_start,
1212         .next = slab_next,
1213         .stop = slab_stop,
1214         .show = slab_show,
1215 };
1216
1217 static int slabinfo_open(struct inode *inode, struct file *file)
1218 {
1219         return seq_open(file, &slabinfo_op);
1220 }
1221
1222 static const struct proc_ops slabinfo_proc_ops = {
1223         .proc_flags     = PROC_ENTRY_PERMANENT,
1224         .proc_open      = slabinfo_open,
1225         .proc_read      = seq_read,
1226         .proc_write     = slabinfo_write,
1227         .proc_lseek     = seq_lseek,
1228         .proc_release   = seq_release,
1229 };
1230
1231 static int __init slab_proc_init(void)
1232 {
1233         proc_create("slabinfo", SLABINFO_RIGHTS, NULL, &slabinfo_proc_ops);
1234         return 0;
1235 }
1236 module_init(slab_proc_init);
1237
1238 #if defined(CONFIG_DEBUG_FS) && defined(CONFIG_MEMCG_KMEM)
1239 /*
1240  * Display information about kmem caches that have memcg cache.
1241  */
1242 static int memcg_slabinfo_show(struct seq_file *m, void *unused)
1243 {
1244         struct kmem_cache *s, *c;
1245         struct slabinfo sinfo;
1246
1247         mutex_lock(&slab_mutex);
1248         seq_puts(m, "# <name> <css_id[:dead|deact]> <active_objs> <num_objs>");
1249         seq_puts(m, " <active_slabs> <num_slabs>\n");
1250         list_for_each_entry(s, &slab_caches, list) {
1251                 /*
1252                  * Skip kmem caches that don't have the memcg cache.
1253                  */
1254                 if (!s->memcg_params.memcg_cache)
1255                         continue;
1256
1257                 memset(&sinfo, 0, sizeof(sinfo));
1258                 get_slabinfo(s, &sinfo);
1259                 seq_printf(m, "%-17s root       %6lu %6lu %6lu %6lu\n",
1260                            cache_name(s), sinfo.active_objs, sinfo.num_objs,
1261                            sinfo.active_slabs, sinfo.num_slabs);
1262
1263                 c = s->memcg_params.memcg_cache;
1264                 memset(&sinfo, 0, sizeof(sinfo));
1265                 get_slabinfo(c, &sinfo);
1266                 seq_printf(m, "%-17s %4d %6lu %6lu %6lu %6lu\n",
1267                            cache_name(c), root_mem_cgroup->css.id,
1268                            sinfo.active_objs, sinfo.num_objs,
1269                            sinfo.active_slabs, sinfo.num_slabs);
1270         }
1271         mutex_unlock(&slab_mutex);
1272         return 0;
1273 }
1274 DEFINE_SHOW_ATTRIBUTE(memcg_slabinfo);
1275
1276 static int __init memcg_slabinfo_init(void)
1277 {
1278         debugfs_create_file("memcg_slabinfo", S_IFREG | S_IRUGO,
1279                             NULL, NULL, &memcg_slabinfo_fops);
1280         return 0;
1281 }
1282
1283 late_initcall(memcg_slabinfo_init);
1284 #endif /* CONFIG_DEBUG_FS && CONFIG_MEMCG_KMEM */
1285 #endif /* CONFIG_SLAB || CONFIG_SLUB_DEBUG */
1286
1287 static __always_inline void *__do_krealloc(const void *p, size_t new_size,
1288                                            gfp_t flags)
1289 {
1290         void *ret;
1291         size_t ks;
1292
1293         ks = ksize(p);
1294
1295         if (ks >= new_size) {
1296                 p = kasan_krealloc((void *)p, new_size, flags);
1297                 return (void *)p;
1298         }
1299
1300         ret = kmalloc_track_caller(new_size, flags);
1301         if (ret && p)
1302                 memcpy(ret, p, ks);
1303
1304         return ret;
1305 }
1306
1307 /**
1308  * krealloc - reallocate memory. The contents will remain unchanged.
1309  * @p: object to reallocate memory for.
1310  * @new_size: how many bytes of memory are required.
1311  * @flags: the type of memory to allocate.
1312  *
1313  * The contents of the object pointed to are preserved up to the
1314  * lesser of the new and old sizes.  If @p is %NULL, krealloc()
1315  * behaves exactly like kmalloc().  If @new_size is 0 and @p is not a
1316  * %NULL pointer, the object pointed to is freed.
1317  *
1318  * Return: pointer to the allocated memory or %NULL in case of error
1319  */
1320 void *krealloc(const void *p, size_t new_size, gfp_t flags)
1321 {
1322         void *ret;
1323
1324         if (unlikely(!new_size)) {
1325                 kfree(p);
1326                 return ZERO_SIZE_PTR;
1327         }
1328
1329         ret = __do_krealloc(p, new_size, flags);
1330         if (ret && kasan_reset_tag(p) != kasan_reset_tag(ret))
1331                 kfree(p);
1332
1333         return ret;
1334 }
1335 EXPORT_SYMBOL(krealloc);
1336
1337 /**
1338  * kfree_sensitive - Clear sensitive information in memory before freeing
1339  * @p: object to free memory of
1340  *
1341  * The memory of the object @p points to is zeroed before freed.
1342  * If @p is %NULL, kfree_sensitive() does nothing.
1343  *
1344  * Note: this function zeroes the whole allocated buffer which can be a good
1345  * deal bigger than the requested buffer size passed to kmalloc(). So be
1346  * careful when using this function in performance sensitive code.
1347  */
1348 void kfree_sensitive(const void *p)
1349 {
1350         size_t ks;
1351         void *mem = (void *)p;
1352
1353         ks = ksize(mem);
1354         if (ks)
1355                 memzero_explicit(mem, ks);
1356         kfree(mem);
1357 }
1358 EXPORT_SYMBOL(kfree_sensitive);
1359
1360 /**
1361  * ksize - get the actual amount of memory allocated for a given object
1362  * @objp: Pointer to the object
1363  *
1364  * kmalloc may internally round up allocations and return more memory
1365  * than requested. ksize() can be used to determine the actual amount of
1366  * memory allocated. The caller may use this additional memory, even though
1367  * a smaller amount of memory was initially specified with the kmalloc call.
1368  * The caller must guarantee that objp points to a valid object previously
1369  * allocated with either kmalloc() or kmem_cache_alloc(). The object
1370  * must not be freed during the duration of the call.
1371  *
1372  * Return: size of the actual memory used by @objp in bytes
1373  */
1374 size_t ksize(const void *objp)
1375 {
1376         size_t size;
1377
1378         /*
1379          * We need to check that the pointed to object is valid, and only then
1380          * unpoison the shadow memory below. We use __kasan_check_read(), to
1381          * generate a more useful report at the time ksize() is called (rather
1382          * than later where behaviour is undefined due to potential
1383          * use-after-free or double-free).
1384          *
1385          * If the pointed to memory is invalid we return 0, to avoid users of
1386          * ksize() writing to and potentially corrupting the memory region.
1387          *
1388          * We want to perform the check before __ksize(), to avoid potentially
1389          * crashing in __ksize() due to accessing invalid metadata.
1390          */
1391         if (unlikely(ZERO_OR_NULL_PTR(objp)) || !__kasan_check_read(objp, 1))
1392                 return 0;
1393
1394         size = __ksize(objp);
1395         /*
1396          * We assume that ksize callers could use whole allocated area,
1397          * so we need to unpoison this area.
1398          */
1399         kasan_unpoison_shadow(objp, size);
1400         return size;
1401 }
1402 EXPORT_SYMBOL(ksize);
1403
1404 /* Tracepoints definitions. */
1405 EXPORT_TRACEPOINT_SYMBOL(kmalloc);
1406 EXPORT_TRACEPOINT_SYMBOL(kmem_cache_alloc);
1407 EXPORT_TRACEPOINT_SYMBOL(kmalloc_node);
1408 EXPORT_TRACEPOINT_SYMBOL(kmem_cache_alloc_node);
1409 EXPORT_TRACEPOINT_SYMBOL(kfree);
1410 EXPORT_TRACEPOINT_SYMBOL(kmem_cache_free);
1411
1412 int should_failslab(struct kmem_cache *s, gfp_t gfpflags)
1413 {
1414         if (__should_failslab(s, gfpflags))
1415                 return -ENOMEM;
1416         return 0;
1417 }
1418 ALLOW_ERROR_INJECTION(should_failslab, ERRNO);