Merge tag 'gfs2-v6.4-rc5-fixes' of git://git.kernel.org/pub/scm/linux/kernel/git...
[linux-2.6-microblaze.git] / include / linux / slab.h
1 /* SPDX-License-Identifier: GPL-2.0 */
2 /*
3  * Written by Mark Hemment, 1996 (markhe@nextd.demon.co.uk).
4  *
5  * (C) SGI 2006, Christoph Lameter
6  *      Cleaned up and restructured to ease the addition of alternative
7  *      implementations of SLAB allocators.
8  * (C) Linux Foundation 2008-2013
9  *      Unified interface for all slab allocators
10  */
11
12 #ifndef _LINUX_SLAB_H
13 #define _LINUX_SLAB_H
14
15 #include <linux/cache.h>
16 #include <linux/gfp.h>
17 #include <linux/overflow.h>
18 #include <linux/types.h>
19 #include <linux/workqueue.h>
20 #include <linux/percpu-refcount.h>
21
22
23 /*
24  * Flags to pass to kmem_cache_create().
25  * The ones marked DEBUG are only valid if CONFIG_DEBUG_SLAB is set.
26  */
27 /* DEBUG: Perform (expensive) checks on alloc/free */
28 #define SLAB_CONSISTENCY_CHECKS ((slab_flags_t __force)0x00000100U)
29 /* DEBUG: Red zone objs in a cache */
30 #define SLAB_RED_ZONE           ((slab_flags_t __force)0x00000400U)
31 /* DEBUG: Poison objects */
32 #define SLAB_POISON             ((slab_flags_t __force)0x00000800U)
33 /* Indicate a kmalloc slab */
34 #define SLAB_KMALLOC            ((slab_flags_t __force)0x00001000U)
35 /* Align objs on cache lines */
36 #define SLAB_HWCACHE_ALIGN      ((slab_flags_t __force)0x00002000U)
37 /* Use GFP_DMA memory */
38 #define SLAB_CACHE_DMA          ((slab_flags_t __force)0x00004000U)
39 /* Use GFP_DMA32 memory */
40 #define SLAB_CACHE_DMA32        ((slab_flags_t __force)0x00008000U)
41 /* DEBUG: Store the last owner for bug hunting */
42 #define SLAB_STORE_USER         ((slab_flags_t __force)0x00010000U)
43 /* Panic if kmem_cache_create() fails */
44 #define SLAB_PANIC              ((slab_flags_t __force)0x00040000U)
45 /*
46  * SLAB_TYPESAFE_BY_RCU - **WARNING** READ THIS!
47  *
48  * This delays freeing the SLAB page by a grace period, it does _NOT_
49  * delay object freeing. This means that if you do kmem_cache_free()
50  * that memory location is free to be reused at any time. Thus it may
51  * be possible to see another object there in the same RCU grace period.
52  *
53  * This feature only ensures the memory location backing the object
54  * stays valid, the trick to using this is relying on an independent
55  * object validation pass. Something like:
56  *
57  * begin:
58  *  rcu_read_lock();
59  *  obj = lockless_lookup(key);
60  *  if (obj) {
61  *    if (!try_get_ref(obj)) // might fail for free objects
62  *      rcu_read_unlock();
63  *      goto begin;
64  *
65  *    if (obj->key != key) { // not the object we expected
66  *      put_ref(obj);
67  *      rcu_read_unlock();
68  *      goto begin;
69  *    }
70  *  }
71  *  rcu_read_unlock();
72  *
73  * This is useful if we need to approach a kernel structure obliquely,
74  * from its address obtained without the usual locking. We can lock
75  * the structure to stabilize it and check it's still at the given address,
76  * only if we can be sure that the memory has not been meanwhile reused
77  * for some other kind of object (which our subsystem's lock might corrupt).
78  *
79  * rcu_read_lock before reading the address, then rcu_read_unlock after
80  * taking the spinlock within the structure expected at that address.
81  *
82  * Note that it is not possible to acquire a lock within a structure
83  * allocated with SLAB_TYPESAFE_BY_RCU without first acquiring a reference
84  * as described above.  The reason is that SLAB_TYPESAFE_BY_RCU pages
85  * are not zeroed before being given to the slab, which means that any
86  * locks must be initialized after each and every kmem_struct_alloc().
87  * Alternatively, make the ctor passed to kmem_cache_create() initialize
88  * the locks at page-allocation time, as is done in __i915_request_ctor(),
89  * sighand_ctor(), and anon_vma_ctor().  Such a ctor permits readers
90  * to safely acquire those ctor-initialized locks under rcu_read_lock()
91  * protection.
92  *
93  * Note that SLAB_TYPESAFE_BY_RCU was originally named SLAB_DESTROY_BY_RCU.
94  */
95 /* Defer freeing slabs to RCU */
96 #define SLAB_TYPESAFE_BY_RCU    ((slab_flags_t __force)0x00080000U)
97 /* Spread some memory over cpuset */
98 #define SLAB_MEM_SPREAD         ((slab_flags_t __force)0x00100000U)
99 /* Trace allocations and frees */
100 #define SLAB_TRACE              ((slab_flags_t __force)0x00200000U)
101
102 /* Flag to prevent checks on free */
103 #ifdef CONFIG_DEBUG_OBJECTS
104 # define SLAB_DEBUG_OBJECTS     ((slab_flags_t __force)0x00400000U)
105 #else
106 # define SLAB_DEBUG_OBJECTS     0
107 #endif
108
109 /* Avoid kmemleak tracing */
110 #define SLAB_NOLEAKTRACE        ((slab_flags_t __force)0x00800000U)
111
112 /*
113  * Prevent merging with compatible kmem caches. This flag should be used
114  * cautiously. Valid use cases:
115  *
116  * - caches created for self-tests (e.g. kunit)
117  * - general caches created and used by a subsystem, only when a
118  *   (subsystem-specific) debug option is enabled
119  * - performance critical caches, should be very rare and consulted with slab
120  *   maintainers, and not used together with CONFIG_SLUB_TINY
121  */
122 #define SLAB_NO_MERGE           ((slab_flags_t __force)0x01000000U)
123
124 /* Fault injection mark */
125 #ifdef CONFIG_FAILSLAB
126 # define SLAB_FAILSLAB          ((slab_flags_t __force)0x02000000U)
127 #else
128 # define SLAB_FAILSLAB          0
129 #endif
130 /* Account to memcg */
131 #ifdef CONFIG_MEMCG_KMEM
132 # define SLAB_ACCOUNT           ((slab_flags_t __force)0x04000000U)
133 #else
134 # define SLAB_ACCOUNT           0
135 #endif
136
137 #ifdef CONFIG_KASAN_GENERIC
138 #define SLAB_KASAN              ((slab_flags_t __force)0x08000000U)
139 #else
140 #define SLAB_KASAN              0
141 #endif
142
143 /*
144  * Ignore user specified debugging flags.
145  * Intended for caches created for self-tests so they have only flags
146  * specified in the code and other flags are ignored.
147  */
148 #define SLAB_NO_USER_FLAGS      ((slab_flags_t __force)0x10000000U)
149
150 #ifdef CONFIG_KFENCE
151 #define SLAB_SKIP_KFENCE        ((slab_flags_t __force)0x20000000U)
152 #else
153 #define SLAB_SKIP_KFENCE        0
154 #endif
155
156 /* The following flags affect the page allocator grouping pages by mobility */
157 /* Objects are reclaimable */
158 #ifndef CONFIG_SLUB_TINY
159 #define SLAB_RECLAIM_ACCOUNT    ((slab_flags_t __force)0x00020000U)
160 #else
161 #define SLAB_RECLAIM_ACCOUNT    ((slab_flags_t __force)0)
162 #endif
163 #define SLAB_TEMPORARY          SLAB_RECLAIM_ACCOUNT    /* Objects are short-lived */
164
165 /*
166  * ZERO_SIZE_PTR will be returned for zero sized kmalloc requests.
167  *
168  * Dereferencing ZERO_SIZE_PTR will lead to a distinct access fault.
169  *
170  * ZERO_SIZE_PTR can be passed to kfree though in the same way that NULL can.
171  * Both make kfree a no-op.
172  */
173 #define ZERO_SIZE_PTR ((void *)16)
174
175 #define ZERO_OR_NULL_PTR(x) ((unsigned long)(x) <= \
176                                 (unsigned long)ZERO_SIZE_PTR)
177
178 #include <linux/kasan.h>
179
180 struct list_lru;
181 struct mem_cgroup;
182 /*
183  * struct kmem_cache related prototypes
184  */
185 bool slab_is_available(void);
186
187 struct kmem_cache *kmem_cache_create(const char *name, unsigned int size,
188                         unsigned int align, slab_flags_t flags,
189                         void (*ctor)(void *));
190 struct kmem_cache *kmem_cache_create_usercopy(const char *name,
191                         unsigned int size, unsigned int align,
192                         slab_flags_t flags,
193                         unsigned int useroffset, unsigned int usersize,
194                         void (*ctor)(void *));
195 void kmem_cache_destroy(struct kmem_cache *s);
196 int kmem_cache_shrink(struct kmem_cache *s);
197
198 /*
199  * Please use this macro to create slab caches. Simply specify the
200  * name of the structure and maybe some flags that are listed above.
201  *
202  * The alignment of the struct determines object alignment. If you
203  * f.e. add ____cacheline_aligned_in_smp to the struct declaration
204  * then the objects will be properly aligned in SMP configurations.
205  */
206 #define KMEM_CACHE(__struct, __flags)                                   \
207                 kmem_cache_create(#__struct, sizeof(struct __struct),   \
208                         __alignof__(struct __struct), (__flags), NULL)
209
210 /*
211  * To whitelist a single field for copying to/from usercopy, use this
212  * macro instead for KMEM_CACHE() above.
213  */
214 #define KMEM_CACHE_USERCOPY(__struct, __flags, __field)                 \
215                 kmem_cache_create_usercopy(#__struct,                   \
216                         sizeof(struct __struct),                        \
217                         __alignof__(struct __struct), (__flags),        \
218                         offsetof(struct __struct, __field),             \
219                         sizeof_field(struct __struct, __field), NULL)
220
221 /*
222  * Common kmalloc functions provided by all allocators
223  */
224 void * __must_check krealloc(const void *objp, size_t new_size, gfp_t flags) __realloc_size(2);
225 void kfree(const void *objp);
226 void kfree_sensitive(const void *objp);
227 size_t __ksize(const void *objp);
228
229 /**
230  * ksize - Report actual allocation size of associated object
231  *
232  * @objp: Pointer returned from a prior kmalloc()-family allocation.
233  *
234  * This should not be used for writing beyond the originally requested
235  * allocation size. Either use krealloc() or round up the allocation size
236  * with kmalloc_size_roundup() prior to allocation. If this is used to
237  * access beyond the originally requested allocation size, UBSAN_BOUNDS
238  * and/or FORTIFY_SOURCE may trip, since they only know about the
239  * originally allocated size via the __alloc_size attribute.
240  */
241 size_t ksize(const void *objp);
242
243 #ifdef CONFIG_PRINTK
244 bool kmem_valid_obj(void *object);
245 void kmem_dump_obj(void *object);
246 #endif
247
248 /*
249  * Some archs want to perform DMA into kmalloc caches and need a guaranteed
250  * alignment larger than the alignment of a 64-bit integer.
251  * Setting ARCH_DMA_MINALIGN in arch headers allows that.
252  */
253 #ifdef ARCH_HAS_DMA_MINALIGN
254 #if ARCH_DMA_MINALIGN > 8 && !defined(ARCH_KMALLOC_MINALIGN)
255 #define ARCH_KMALLOC_MINALIGN ARCH_DMA_MINALIGN
256 #endif
257 #endif
258
259 #ifndef ARCH_KMALLOC_MINALIGN
260 #define ARCH_KMALLOC_MINALIGN __alignof__(unsigned long long)
261 #elif ARCH_KMALLOC_MINALIGN > 8
262 #define KMALLOC_MIN_SIZE ARCH_KMALLOC_MINALIGN
263 #define KMALLOC_SHIFT_LOW ilog2(KMALLOC_MIN_SIZE)
264 #endif
265
266 /*
267  * Setting ARCH_SLAB_MINALIGN in arch headers allows a different alignment.
268  * Intended for arches that get misalignment faults even for 64 bit integer
269  * aligned buffers.
270  */
271 #ifndef ARCH_SLAB_MINALIGN
272 #define ARCH_SLAB_MINALIGN __alignof__(unsigned long long)
273 #endif
274
275 /*
276  * Arches can define this function if they want to decide the minimum slab
277  * alignment at runtime. The value returned by the function must be a power
278  * of two and >= ARCH_SLAB_MINALIGN.
279  */
280 #ifndef arch_slab_minalign
281 static inline unsigned int arch_slab_minalign(void)
282 {
283         return ARCH_SLAB_MINALIGN;
284 }
285 #endif
286
287 /*
288  * kmem_cache_alloc and friends return pointers aligned to ARCH_SLAB_MINALIGN.
289  * kmalloc and friends return pointers aligned to both ARCH_KMALLOC_MINALIGN
290  * and ARCH_SLAB_MINALIGN, but here we only assume the former alignment.
291  */
292 #define __assume_kmalloc_alignment __assume_aligned(ARCH_KMALLOC_MINALIGN)
293 #define __assume_slab_alignment __assume_aligned(ARCH_SLAB_MINALIGN)
294 #define __assume_page_alignment __assume_aligned(PAGE_SIZE)
295
296 /*
297  * Kmalloc array related definitions
298  */
299
300 #ifdef CONFIG_SLAB
301 /*
302  * SLAB and SLUB directly allocates requests fitting in to an order-1 page
303  * (PAGE_SIZE*2).  Larger requests are passed to the page allocator.
304  */
305 #define KMALLOC_SHIFT_HIGH      (PAGE_SHIFT + 1)
306 #define KMALLOC_SHIFT_MAX       (MAX_ORDER + PAGE_SHIFT)
307 #ifndef KMALLOC_SHIFT_LOW
308 #define KMALLOC_SHIFT_LOW       5
309 #endif
310 #endif
311
312 #ifdef CONFIG_SLUB
313 #define KMALLOC_SHIFT_HIGH      (PAGE_SHIFT + 1)
314 #define KMALLOC_SHIFT_MAX       (MAX_ORDER + PAGE_SHIFT)
315 #ifndef KMALLOC_SHIFT_LOW
316 #define KMALLOC_SHIFT_LOW       3
317 #endif
318 #endif
319
320 /* Maximum allocatable size */
321 #define KMALLOC_MAX_SIZE        (1UL << KMALLOC_SHIFT_MAX)
322 /* Maximum size for which we actually use a slab cache */
323 #define KMALLOC_MAX_CACHE_SIZE  (1UL << KMALLOC_SHIFT_HIGH)
324 /* Maximum order allocatable via the slab allocator */
325 #define KMALLOC_MAX_ORDER       (KMALLOC_SHIFT_MAX - PAGE_SHIFT)
326
327 /*
328  * Kmalloc subsystem.
329  */
330 #ifndef KMALLOC_MIN_SIZE
331 #define KMALLOC_MIN_SIZE (1 << KMALLOC_SHIFT_LOW)
332 #endif
333
334 /*
335  * This restriction comes from byte sized index implementation.
336  * Page size is normally 2^12 bytes and, in this case, if we want to use
337  * byte sized index which can represent 2^8 entries, the size of the object
338  * should be equal or greater to 2^12 / 2^8 = 2^4 = 16.
339  * If minimum size of kmalloc is less than 16, we use it as minimum object
340  * size and give up to use byte sized index.
341  */
342 #define SLAB_OBJ_MIN_SIZE      (KMALLOC_MIN_SIZE < 16 ? \
343                                (KMALLOC_MIN_SIZE) : 16)
344
345 /*
346  * Whenever changing this, take care of that kmalloc_type() and
347  * create_kmalloc_caches() still work as intended.
348  *
349  * KMALLOC_NORMAL can contain only unaccounted objects whereas KMALLOC_CGROUP
350  * is for accounted but unreclaimable and non-dma objects. All the other
351  * kmem caches can have both accounted and unaccounted objects.
352  */
353 enum kmalloc_cache_type {
354         KMALLOC_NORMAL = 0,
355 #ifndef CONFIG_ZONE_DMA
356         KMALLOC_DMA = KMALLOC_NORMAL,
357 #endif
358 #ifndef CONFIG_MEMCG_KMEM
359         KMALLOC_CGROUP = KMALLOC_NORMAL,
360 #endif
361 #ifdef CONFIG_SLUB_TINY
362         KMALLOC_RECLAIM = KMALLOC_NORMAL,
363 #else
364         KMALLOC_RECLAIM,
365 #endif
366 #ifdef CONFIG_ZONE_DMA
367         KMALLOC_DMA,
368 #endif
369 #ifdef CONFIG_MEMCG_KMEM
370         KMALLOC_CGROUP,
371 #endif
372         NR_KMALLOC_TYPES
373 };
374
375 extern struct kmem_cache *
376 kmalloc_caches[NR_KMALLOC_TYPES][KMALLOC_SHIFT_HIGH + 1];
377
378 /*
379  * Define gfp bits that should not be set for KMALLOC_NORMAL.
380  */
381 #define KMALLOC_NOT_NORMAL_BITS                                 \
382         (__GFP_RECLAIMABLE |                                    \
383         (IS_ENABLED(CONFIG_ZONE_DMA)   ? __GFP_DMA : 0) |       \
384         (IS_ENABLED(CONFIG_MEMCG_KMEM) ? __GFP_ACCOUNT : 0))
385
386 static __always_inline enum kmalloc_cache_type kmalloc_type(gfp_t flags)
387 {
388         /*
389          * The most common case is KMALLOC_NORMAL, so test for it
390          * with a single branch for all the relevant flags.
391          */
392         if (likely((flags & KMALLOC_NOT_NORMAL_BITS) == 0))
393                 return KMALLOC_NORMAL;
394
395         /*
396          * At least one of the flags has to be set. Their priorities in
397          * decreasing order are:
398          *  1) __GFP_DMA
399          *  2) __GFP_RECLAIMABLE
400          *  3) __GFP_ACCOUNT
401          */
402         if (IS_ENABLED(CONFIG_ZONE_DMA) && (flags & __GFP_DMA))
403                 return KMALLOC_DMA;
404         if (!IS_ENABLED(CONFIG_MEMCG_KMEM) || (flags & __GFP_RECLAIMABLE))
405                 return KMALLOC_RECLAIM;
406         else
407                 return KMALLOC_CGROUP;
408 }
409
410 /*
411  * Figure out which kmalloc slab an allocation of a certain size
412  * belongs to.
413  * 0 = zero alloc
414  * 1 =  65 .. 96 bytes
415  * 2 = 129 .. 192 bytes
416  * n = 2^(n-1)+1 .. 2^n
417  *
418  * Note: __kmalloc_index() is compile-time optimized, and not runtime optimized;
419  * typical usage is via kmalloc_index() and therefore evaluated at compile-time.
420  * Callers where !size_is_constant should only be test modules, where runtime
421  * overheads of __kmalloc_index() can be tolerated.  Also see kmalloc_slab().
422  */
423 static __always_inline unsigned int __kmalloc_index(size_t size,
424                                                     bool size_is_constant)
425 {
426         if (!size)
427                 return 0;
428
429         if (size <= KMALLOC_MIN_SIZE)
430                 return KMALLOC_SHIFT_LOW;
431
432         if (KMALLOC_MIN_SIZE <= 32 && size > 64 && size <= 96)
433                 return 1;
434         if (KMALLOC_MIN_SIZE <= 64 && size > 128 && size <= 192)
435                 return 2;
436         if (size <=          8) return 3;
437         if (size <=         16) return 4;
438         if (size <=         32) return 5;
439         if (size <=         64) return 6;
440         if (size <=        128) return 7;
441         if (size <=        256) return 8;
442         if (size <=        512) return 9;
443         if (size <=       1024) return 10;
444         if (size <=   2 * 1024) return 11;
445         if (size <=   4 * 1024) return 12;
446         if (size <=   8 * 1024) return 13;
447         if (size <=  16 * 1024) return 14;
448         if (size <=  32 * 1024) return 15;
449         if (size <=  64 * 1024) return 16;
450         if (size <= 128 * 1024) return 17;
451         if (size <= 256 * 1024) return 18;
452         if (size <= 512 * 1024) return 19;
453         if (size <= 1024 * 1024) return 20;
454         if (size <=  2 * 1024 * 1024) return 21;
455
456         if (!IS_ENABLED(CONFIG_PROFILE_ALL_BRANCHES) && size_is_constant)
457                 BUILD_BUG_ON_MSG(1, "unexpected size in kmalloc_index()");
458         else
459                 BUG();
460
461         /* Will never be reached. Needed because the compiler may complain */
462         return -1;
463 }
464 static_assert(PAGE_SHIFT <= 20);
465 #define kmalloc_index(s) __kmalloc_index(s, true)
466
467 void *__kmalloc(size_t size, gfp_t flags) __assume_kmalloc_alignment __alloc_size(1);
468
469 /**
470  * kmem_cache_alloc - Allocate an object
471  * @cachep: The cache to allocate from.
472  * @flags: See kmalloc().
473  *
474  * Allocate an object from this cache.
475  * See kmem_cache_zalloc() for a shortcut of adding __GFP_ZERO to flags.
476  *
477  * Return: pointer to the new object or %NULL in case of error
478  */
479 void *kmem_cache_alloc(struct kmem_cache *cachep, gfp_t flags) __assume_slab_alignment __malloc;
480 void *kmem_cache_alloc_lru(struct kmem_cache *s, struct list_lru *lru,
481                            gfp_t gfpflags) __assume_slab_alignment __malloc;
482 void kmem_cache_free(struct kmem_cache *s, void *objp);
483
484 /*
485  * Bulk allocation and freeing operations. These are accelerated in an
486  * allocator specific way to avoid taking locks repeatedly or building
487  * metadata structures unnecessarily.
488  *
489  * Note that interrupts must be enabled when calling these functions.
490  */
491 void kmem_cache_free_bulk(struct kmem_cache *s, size_t size, void **p);
492 int kmem_cache_alloc_bulk(struct kmem_cache *s, gfp_t flags, size_t size, void **p);
493
494 static __always_inline void kfree_bulk(size_t size, void **p)
495 {
496         kmem_cache_free_bulk(NULL, size, p);
497 }
498
499 void *__kmalloc_node(size_t size, gfp_t flags, int node) __assume_kmalloc_alignment
500                                                          __alloc_size(1);
501 void *kmem_cache_alloc_node(struct kmem_cache *s, gfp_t flags, int node) __assume_slab_alignment
502                                                                          __malloc;
503
504 void *kmalloc_trace(struct kmem_cache *s, gfp_t flags, size_t size)
505                     __assume_kmalloc_alignment __alloc_size(3);
506
507 void *kmalloc_node_trace(struct kmem_cache *s, gfp_t gfpflags,
508                          int node, size_t size) __assume_kmalloc_alignment
509                                                 __alloc_size(4);
510 void *kmalloc_large(size_t size, gfp_t flags) __assume_page_alignment
511                                               __alloc_size(1);
512
513 void *kmalloc_large_node(size_t size, gfp_t flags, int node) __assume_page_alignment
514                                                              __alloc_size(1);
515
516 /**
517  * kmalloc - allocate kernel memory
518  * @size: how many bytes of memory are required.
519  * @flags: describe the allocation context
520  *
521  * kmalloc is the normal method of allocating memory
522  * for objects smaller than page size in the kernel.
523  *
524  * The allocated object address is aligned to at least ARCH_KMALLOC_MINALIGN
525  * bytes. For @size of power of two bytes, the alignment is also guaranteed
526  * to be at least to the size.
527  *
528  * The @flags argument may be one of the GFP flags defined at
529  * include/linux/gfp_types.h and described at
530  * :ref:`Documentation/core-api/mm-api.rst <mm-api-gfp-flags>`
531  *
532  * The recommended usage of the @flags is described at
533  * :ref:`Documentation/core-api/memory-allocation.rst <memory_allocation>`
534  *
535  * Below is a brief outline of the most useful GFP flags
536  *
537  * %GFP_KERNEL
538  *      Allocate normal kernel ram. May sleep.
539  *
540  * %GFP_NOWAIT
541  *      Allocation will not sleep.
542  *
543  * %GFP_ATOMIC
544  *      Allocation will not sleep.  May use emergency pools.
545  *
546  * Also it is possible to set different flags by OR'ing
547  * in one or more of the following additional @flags:
548  *
549  * %__GFP_ZERO
550  *      Zero the allocated memory before returning. Also see kzalloc().
551  *
552  * %__GFP_HIGH
553  *      This allocation has high priority and may use emergency pools.
554  *
555  * %__GFP_NOFAIL
556  *      Indicate that this allocation is in no way allowed to fail
557  *      (think twice before using).
558  *
559  * %__GFP_NORETRY
560  *      If memory is not immediately available,
561  *      then give up at once.
562  *
563  * %__GFP_NOWARN
564  *      If allocation fails, don't issue any warnings.
565  *
566  * %__GFP_RETRY_MAYFAIL
567  *      Try really hard to succeed the allocation but fail
568  *      eventually.
569  */
570 static __always_inline __alloc_size(1) void *kmalloc(size_t size, gfp_t flags)
571 {
572         if (__builtin_constant_p(size) && size) {
573                 unsigned int index;
574
575                 if (size > KMALLOC_MAX_CACHE_SIZE)
576                         return kmalloc_large(size, flags);
577
578                 index = kmalloc_index(size);
579                 return kmalloc_trace(
580                                 kmalloc_caches[kmalloc_type(flags)][index],
581                                 flags, size);
582         }
583         return __kmalloc(size, flags);
584 }
585
586 static __always_inline __alloc_size(1) void *kmalloc_node(size_t size, gfp_t flags, int node)
587 {
588         if (__builtin_constant_p(size) && size) {
589                 unsigned int index;
590
591                 if (size > KMALLOC_MAX_CACHE_SIZE)
592                         return kmalloc_large_node(size, flags, node);
593
594                 index = kmalloc_index(size);
595                 return kmalloc_node_trace(
596                                 kmalloc_caches[kmalloc_type(flags)][index],
597                                 flags, node, size);
598         }
599         return __kmalloc_node(size, flags, node);
600 }
601
602 /**
603  * kmalloc_array - allocate memory for an array.
604  * @n: number of elements.
605  * @size: element size.
606  * @flags: the type of memory to allocate (see kmalloc).
607  */
608 static inline __alloc_size(1, 2) void *kmalloc_array(size_t n, size_t size, gfp_t flags)
609 {
610         size_t bytes;
611
612         if (unlikely(check_mul_overflow(n, size, &bytes)))
613                 return NULL;
614         if (__builtin_constant_p(n) && __builtin_constant_p(size))
615                 return kmalloc(bytes, flags);
616         return __kmalloc(bytes, flags);
617 }
618
619 /**
620  * krealloc_array - reallocate memory for an array.
621  * @p: pointer to the memory chunk to reallocate
622  * @new_n: new number of elements to alloc
623  * @new_size: new size of a single member of the array
624  * @flags: the type of memory to allocate (see kmalloc)
625  */
626 static inline __realloc_size(2, 3) void * __must_check krealloc_array(void *p,
627                                                                       size_t new_n,
628                                                                       size_t new_size,
629                                                                       gfp_t flags)
630 {
631         size_t bytes;
632
633         if (unlikely(check_mul_overflow(new_n, new_size, &bytes)))
634                 return NULL;
635
636         return krealloc(p, bytes, flags);
637 }
638
639 /**
640  * kcalloc - allocate memory for an array. The memory is set to zero.
641  * @n: number of elements.
642  * @size: element size.
643  * @flags: the type of memory to allocate (see kmalloc).
644  */
645 static inline __alloc_size(1, 2) void *kcalloc(size_t n, size_t size, gfp_t flags)
646 {
647         return kmalloc_array(n, size, flags | __GFP_ZERO);
648 }
649
650 void *__kmalloc_node_track_caller(size_t size, gfp_t flags, int node,
651                                   unsigned long caller) __alloc_size(1);
652 #define kmalloc_node_track_caller(size, flags, node) \
653         __kmalloc_node_track_caller(size, flags, node, \
654                                     _RET_IP_)
655
656 /*
657  * kmalloc_track_caller is a special version of kmalloc that records the
658  * calling function of the routine calling it for slab leak tracking instead
659  * of just the calling function (confusing, eh?).
660  * It's useful when the call to kmalloc comes from a widely-used standard
661  * allocator where we care about the real place the memory allocation
662  * request comes from.
663  */
664 #define kmalloc_track_caller(size, flags) \
665         __kmalloc_node_track_caller(size, flags, \
666                                     NUMA_NO_NODE, _RET_IP_)
667
668 static inline __alloc_size(1, 2) void *kmalloc_array_node(size_t n, size_t size, gfp_t flags,
669                                                           int node)
670 {
671         size_t bytes;
672
673         if (unlikely(check_mul_overflow(n, size, &bytes)))
674                 return NULL;
675         if (__builtin_constant_p(n) && __builtin_constant_p(size))
676                 return kmalloc_node(bytes, flags, node);
677         return __kmalloc_node(bytes, flags, node);
678 }
679
680 static inline __alloc_size(1, 2) void *kcalloc_node(size_t n, size_t size, gfp_t flags, int node)
681 {
682         return kmalloc_array_node(n, size, flags | __GFP_ZERO, node);
683 }
684
685 /*
686  * Shortcuts
687  */
688 static inline void *kmem_cache_zalloc(struct kmem_cache *k, gfp_t flags)
689 {
690         return kmem_cache_alloc(k, flags | __GFP_ZERO);
691 }
692
693 /**
694  * kzalloc - allocate memory. The memory is set to zero.
695  * @size: how many bytes of memory are required.
696  * @flags: the type of memory to allocate (see kmalloc).
697  */
698 static inline __alloc_size(1) void *kzalloc(size_t size, gfp_t flags)
699 {
700         return kmalloc(size, flags | __GFP_ZERO);
701 }
702
703 /**
704  * kzalloc_node - allocate zeroed memory from a particular memory node.
705  * @size: how many bytes of memory are required.
706  * @flags: the type of memory to allocate (see kmalloc).
707  * @node: memory node from which to allocate
708  */
709 static inline __alloc_size(1) void *kzalloc_node(size_t size, gfp_t flags, int node)
710 {
711         return kmalloc_node(size, flags | __GFP_ZERO, node);
712 }
713
714 extern void *kvmalloc_node(size_t size, gfp_t flags, int node) __alloc_size(1);
715 static inline __alloc_size(1) void *kvmalloc(size_t size, gfp_t flags)
716 {
717         return kvmalloc_node(size, flags, NUMA_NO_NODE);
718 }
719 static inline __alloc_size(1) void *kvzalloc_node(size_t size, gfp_t flags, int node)
720 {
721         return kvmalloc_node(size, flags | __GFP_ZERO, node);
722 }
723 static inline __alloc_size(1) void *kvzalloc(size_t size, gfp_t flags)
724 {
725         return kvmalloc(size, flags | __GFP_ZERO);
726 }
727
728 static inline __alloc_size(1, 2) void *kvmalloc_array(size_t n, size_t size, gfp_t flags)
729 {
730         size_t bytes;
731
732         if (unlikely(check_mul_overflow(n, size, &bytes)))
733                 return NULL;
734
735         return kvmalloc(bytes, flags);
736 }
737
738 static inline __alloc_size(1, 2) void *kvcalloc(size_t n, size_t size, gfp_t flags)
739 {
740         return kvmalloc_array(n, size, flags | __GFP_ZERO);
741 }
742
743 extern void *kvrealloc(const void *p, size_t oldsize, size_t newsize, gfp_t flags)
744                       __realloc_size(3);
745 extern void kvfree(const void *addr);
746 extern void kvfree_sensitive(const void *addr, size_t len);
747
748 unsigned int kmem_cache_size(struct kmem_cache *s);
749
750 /**
751  * kmalloc_size_roundup - Report allocation bucket size for the given size
752  *
753  * @size: Number of bytes to round up from.
754  *
755  * This returns the number of bytes that would be available in a kmalloc()
756  * allocation of @size bytes. For example, a 126 byte request would be
757  * rounded up to the next sized kmalloc bucket, 128 bytes. (This is strictly
758  * for the general-purpose kmalloc()-based allocations, and is not for the
759  * pre-sized kmem_cache_alloc()-based allocations.)
760  *
761  * Use this to kmalloc() the full bucket size ahead of time instead of using
762  * ksize() to query the size after an allocation.
763  */
764 size_t kmalloc_size_roundup(size_t size);
765
766 void __init kmem_cache_init_late(void);
767
768 #if defined(CONFIG_SMP) && defined(CONFIG_SLAB)
769 int slab_prepare_cpu(unsigned int cpu);
770 int slab_dead_cpu(unsigned int cpu);
771 #else
772 #define slab_prepare_cpu        NULL
773 #define slab_dead_cpu           NULL
774 #endif
775
776 #endif  /* _LINUX_SLAB_H */