1 /* SPDX-License-Identifier: GPL-2.0 */
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
15 #include <linux/gfp.h>
16 #include <linux/overflow.h>
17 #include <linux/types.h>
18 #include <linux/workqueue.h>
19 #include <linux/percpu-refcount.h>
23 * Flags to pass to kmem_cache_create().
24 * The ones marked DEBUG are only valid if CONFIG_DEBUG_SLAB is set.
26 /* DEBUG: Perform (expensive) checks on alloc/free */
27 #define SLAB_CONSISTENCY_CHECKS ((slab_flags_t __force)0x00000100U)
28 /* DEBUG: Red zone objs in a cache */
29 #define SLAB_RED_ZONE ((slab_flags_t __force)0x00000400U)
30 /* DEBUG: Poison objects */
31 #define SLAB_POISON ((slab_flags_t __force)0x00000800U)
32 /* Indicate a kmalloc slab */
33 #define SLAB_KMALLOC ((slab_flags_t __force)0x00001000U)
34 /* Align objs on cache lines */
35 #define SLAB_HWCACHE_ALIGN ((slab_flags_t __force)0x00002000U)
36 /* Use GFP_DMA memory */
37 #define SLAB_CACHE_DMA ((slab_flags_t __force)0x00004000U)
38 /* Use GFP_DMA32 memory */
39 #define SLAB_CACHE_DMA32 ((slab_flags_t __force)0x00008000U)
40 /* DEBUG: Store the last owner for bug hunting */
41 #define SLAB_STORE_USER ((slab_flags_t __force)0x00010000U)
42 /* Panic if kmem_cache_create() fails */
43 #define SLAB_PANIC ((slab_flags_t __force)0x00040000U)
45 * SLAB_TYPESAFE_BY_RCU - **WARNING** READ THIS!
47 * This delays freeing the SLAB page by a grace period, it does _NOT_
48 * delay object freeing. This means that if you do kmem_cache_free()
49 * that memory location is free to be reused at any time. Thus it may
50 * be possible to see another object there in the same RCU grace period.
52 * This feature only ensures the memory location backing the object
53 * stays valid, the trick to using this is relying on an independent
54 * object validation pass. Something like:
58 * obj = lockless_lookup(key);
60 * if (!try_get_ref(obj)) // might fail for free objects
63 * if (obj->key != key) { // not the object we expected
70 * This is useful if we need to approach a kernel structure obliquely,
71 * from its address obtained without the usual locking. We can lock
72 * the structure to stabilize it and check it's still at the given address,
73 * only if we can be sure that the memory has not been meanwhile reused
74 * for some other kind of object (which our subsystem's lock might corrupt).
76 * rcu_read_lock before reading the address, then rcu_read_unlock after
77 * taking the spinlock within the structure expected at that address.
79 * Note that it is not possible to acquire a lock within a structure
80 * allocated with SLAB_TYPESAFE_BY_RCU without first acquiring a reference
81 * as described above. The reason is that SLAB_TYPESAFE_BY_RCU pages
82 * are not zeroed before being given to the slab, which means that any
83 * locks must be initialized after each and every kmem_struct_alloc().
84 * Alternatively, make the ctor passed to kmem_cache_create() initialize
85 * the locks at page-allocation time, as is done in __i915_request_ctor(),
86 * sighand_ctor(), and anon_vma_ctor(). Such a ctor permits readers
87 * to safely acquire those ctor-initialized locks under rcu_read_lock()
90 * Note that SLAB_TYPESAFE_BY_RCU was originally named SLAB_DESTROY_BY_RCU.
92 /* Defer freeing slabs to RCU */
93 #define SLAB_TYPESAFE_BY_RCU ((slab_flags_t __force)0x00080000U)
94 /* Spread some memory over cpuset */
95 #define SLAB_MEM_SPREAD ((slab_flags_t __force)0x00100000U)
96 /* Trace allocations and frees */
97 #define SLAB_TRACE ((slab_flags_t __force)0x00200000U)
99 /* Flag to prevent checks on free */
100 #ifdef CONFIG_DEBUG_OBJECTS
101 # define SLAB_DEBUG_OBJECTS ((slab_flags_t __force)0x00400000U)
103 # define SLAB_DEBUG_OBJECTS 0
106 /* Avoid kmemleak tracing */
107 #define SLAB_NOLEAKTRACE ((slab_flags_t __force)0x00800000U)
109 /* Fault injection mark */
110 #ifdef CONFIG_FAILSLAB
111 # define SLAB_FAILSLAB ((slab_flags_t __force)0x02000000U)
113 # define SLAB_FAILSLAB 0
115 /* Account to memcg */
116 #ifdef CONFIG_MEMCG_KMEM
117 # define SLAB_ACCOUNT ((slab_flags_t __force)0x04000000U)
119 # define SLAB_ACCOUNT 0
122 #ifdef CONFIG_KASAN_GENERIC
123 #define SLAB_KASAN ((slab_flags_t __force)0x08000000U)
129 * Ignore user specified debugging flags.
130 * Intended for caches created for self-tests so they have only flags
131 * specified in the code and other flags are ignored.
133 #define SLAB_NO_USER_FLAGS ((slab_flags_t __force)0x10000000U)
136 #define SLAB_SKIP_KFENCE ((slab_flags_t __force)0x20000000U)
138 #define SLAB_SKIP_KFENCE 0
141 /* The following flags affect the page allocator grouping pages by mobility */
142 /* Objects are reclaimable */
143 #ifndef CONFIG_SLUB_TINY
144 #define SLAB_RECLAIM_ACCOUNT ((slab_flags_t __force)0x00020000U)
146 #define SLAB_RECLAIM_ACCOUNT ((slab_flags_t __force)0)
148 #define SLAB_TEMPORARY SLAB_RECLAIM_ACCOUNT /* Objects are short-lived */
151 * ZERO_SIZE_PTR will be returned for zero sized kmalloc requests.
153 * Dereferencing ZERO_SIZE_PTR will lead to a distinct access fault.
155 * ZERO_SIZE_PTR can be passed to kfree though in the same way that NULL can.
156 * Both make kfree a no-op.
158 #define ZERO_SIZE_PTR ((void *)16)
160 #define ZERO_OR_NULL_PTR(x) ((unsigned long)(x) <= \
161 (unsigned long)ZERO_SIZE_PTR)
163 #include <linux/kasan.h>
168 * struct kmem_cache related prototypes
170 void __init kmem_cache_init(void);
171 bool slab_is_available(void);
173 struct kmem_cache *kmem_cache_create(const char *name, unsigned int size,
174 unsigned int align, slab_flags_t flags,
175 void (*ctor)(void *));
176 struct kmem_cache *kmem_cache_create_usercopy(const char *name,
177 unsigned int size, unsigned int align,
179 unsigned int useroffset, unsigned int usersize,
180 void (*ctor)(void *));
181 void kmem_cache_destroy(struct kmem_cache *s);
182 int kmem_cache_shrink(struct kmem_cache *s);
185 * Please use this macro to create slab caches. Simply specify the
186 * name of the structure and maybe some flags that are listed above.
188 * The alignment of the struct determines object alignment. If you
189 * f.e. add ____cacheline_aligned_in_smp to the struct declaration
190 * then the objects will be properly aligned in SMP configurations.
192 #define KMEM_CACHE(__struct, __flags) \
193 kmem_cache_create(#__struct, sizeof(struct __struct), \
194 __alignof__(struct __struct), (__flags), NULL)
197 * To whitelist a single field for copying to/from usercopy, use this
198 * macro instead for KMEM_CACHE() above.
200 #define KMEM_CACHE_USERCOPY(__struct, __flags, __field) \
201 kmem_cache_create_usercopy(#__struct, \
202 sizeof(struct __struct), \
203 __alignof__(struct __struct), (__flags), \
204 offsetof(struct __struct, __field), \
205 sizeof_field(struct __struct, __field), NULL)
208 * Common kmalloc functions provided by all allocators
210 void * __must_check krealloc(const void *objp, size_t new_size, gfp_t flags) __realloc_size(2);
211 void kfree(const void *objp);
212 void kfree_sensitive(const void *objp);
213 size_t __ksize(const void *objp);
216 * ksize - Report actual allocation size of associated object
218 * @objp: Pointer returned from a prior kmalloc()-family allocation.
220 * This should not be used for writing beyond the originally requested
221 * allocation size. Either use krealloc() or round up the allocation size
222 * with kmalloc_size_roundup() prior to allocation. If this is used to
223 * access beyond the originally requested allocation size, UBSAN_BOUNDS
224 * and/or FORTIFY_SOURCE may trip, since they only know about the
225 * originally allocated size via the __alloc_size attribute.
227 size_t ksize(const void *objp);
230 bool kmem_valid_obj(void *object);
231 void kmem_dump_obj(void *object);
235 * Some archs want to perform DMA into kmalloc caches and need a guaranteed
236 * alignment larger than the alignment of a 64-bit integer.
237 * Setting ARCH_DMA_MINALIGN in arch headers allows that.
239 #if defined(ARCH_DMA_MINALIGN) && ARCH_DMA_MINALIGN > 8
240 #define ARCH_KMALLOC_MINALIGN ARCH_DMA_MINALIGN
241 #define KMALLOC_MIN_SIZE ARCH_DMA_MINALIGN
242 #define KMALLOC_SHIFT_LOW ilog2(ARCH_DMA_MINALIGN)
244 #define ARCH_KMALLOC_MINALIGN __alignof__(unsigned long long)
248 * Setting ARCH_SLAB_MINALIGN in arch headers allows a different alignment.
249 * Intended for arches that get misalignment faults even for 64 bit integer
252 #ifndef ARCH_SLAB_MINALIGN
253 #define ARCH_SLAB_MINALIGN __alignof__(unsigned long long)
257 * Arches can define this function if they want to decide the minimum slab
258 * alignment at runtime. The value returned by the function must be a power
259 * of two and >= ARCH_SLAB_MINALIGN.
261 #ifndef arch_slab_minalign
262 static inline unsigned int arch_slab_minalign(void)
264 return ARCH_SLAB_MINALIGN;
269 * kmem_cache_alloc and friends return pointers aligned to ARCH_SLAB_MINALIGN.
270 * kmalloc and friends return pointers aligned to both ARCH_KMALLOC_MINALIGN
271 * and ARCH_SLAB_MINALIGN, but here we only assume the former alignment.
273 #define __assume_kmalloc_alignment __assume_aligned(ARCH_KMALLOC_MINALIGN)
274 #define __assume_slab_alignment __assume_aligned(ARCH_SLAB_MINALIGN)
275 #define __assume_page_alignment __assume_aligned(PAGE_SIZE)
278 * Kmalloc array related definitions
283 * SLAB and SLUB directly allocates requests fitting in to an order-1 page
284 * (PAGE_SIZE*2). Larger requests are passed to the page allocator.
286 #define KMALLOC_SHIFT_HIGH (PAGE_SHIFT + 1)
287 #define KMALLOC_SHIFT_MAX (MAX_ORDER + PAGE_SHIFT - 1)
288 #ifndef KMALLOC_SHIFT_LOW
289 #define KMALLOC_SHIFT_LOW 5
294 #define KMALLOC_SHIFT_HIGH (PAGE_SHIFT + 1)
295 #define KMALLOC_SHIFT_MAX (MAX_ORDER + PAGE_SHIFT - 1)
296 #ifndef KMALLOC_SHIFT_LOW
297 #define KMALLOC_SHIFT_LOW 3
301 /* Maximum allocatable size */
302 #define KMALLOC_MAX_SIZE (1UL << KMALLOC_SHIFT_MAX)
303 /* Maximum size for which we actually use a slab cache */
304 #define KMALLOC_MAX_CACHE_SIZE (1UL << KMALLOC_SHIFT_HIGH)
305 /* Maximum order allocatable via the slab allocator */
306 #define KMALLOC_MAX_ORDER (KMALLOC_SHIFT_MAX - PAGE_SHIFT)
311 #ifndef KMALLOC_MIN_SIZE
312 #define KMALLOC_MIN_SIZE (1 << KMALLOC_SHIFT_LOW)
316 * This restriction comes from byte sized index implementation.
317 * Page size is normally 2^12 bytes and, in this case, if we want to use
318 * byte sized index which can represent 2^8 entries, the size of the object
319 * should be equal or greater to 2^12 / 2^8 = 2^4 = 16.
320 * If minimum size of kmalloc is less than 16, we use it as minimum object
321 * size and give up to use byte sized index.
323 #define SLAB_OBJ_MIN_SIZE (KMALLOC_MIN_SIZE < 16 ? \
324 (KMALLOC_MIN_SIZE) : 16)
327 * Whenever changing this, take care of that kmalloc_type() and
328 * create_kmalloc_caches() still work as intended.
330 * KMALLOC_NORMAL can contain only unaccounted objects whereas KMALLOC_CGROUP
331 * is for accounted but unreclaimable and non-dma objects. All the other
332 * kmem caches can have both accounted and unaccounted objects.
334 enum kmalloc_cache_type {
336 #ifndef CONFIG_ZONE_DMA
337 KMALLOC_DMA = KMALLOC_NORMAL,
339 #ifndef CONFIG_MEMCG_KMEM
340 KMALLOC_CGROUP = KMALLOC_NORMAL,
342 #ifdef CONFIG_SLUB_TINY
343 KMALLOC_RECLAIM = KMALLOC_NORMAL,
347 #ifdef CONFIG_ZONE_DMA
350 #ifdef CONFIG_MEMCG_KMEM
356 extern struct kmem_cache *
357 kmalloc_caches[NR_KMALLOC_TYPES][KMALLOC_SHIFT_HIGH + 1];
360 * Define gfp bits that should not be set for KMALLOC_NORMAL.
362 #define KMALLOC_NOT_NORMAL_BITS \
363 (__GFP_RECLAIMABLE | \
364 (IS_ENABLED(CONFIG_ZONE_DMA) ? __GFP_DMA : 0) | \
365 (IS_ENABLED(CONFIG_MEMCG_KMEM) ? __GFP_ACCOUNT : 0))
367 static __always_inline enum kmalloc_cache_type kmalloc_type(gfp_t flags)
370 * The most common case is KMALLOC_NORMAL, so test for it
371 * with a single branch for all the relevant flags.
373 if (likely((flags & KMALLOC_NOT_NORMAL_BITS) == 0))
374 return KMALLOC_NORMAL;
377 * At least one of the flags has to be set. Their priorities in
378 * decreasing order are:
380 * 2) __GFP_RECLAIMABLE
383 if (IS_ENABLED(CONFIG_ZONE_DMA) && (flags & __GFP_DMA))
385 if (!IS_ENABLED(CONFIG_MEMCG_KMEM) || (flags & __GFP_RECLAIMABLE))
386 return KMALLOC_RECLAIM;
388 return KMALLOC_CGROUP;
392 * Figure out which kmalloc slab an allocation of a certain size
396 * 2 = 129 .. 192 bytes
397 * n = 2^(n-1)+1 .. 2^n
399 * Note: __kmalloc_index() is compile-time optimized, and not runtime optimized;
400 * typical usage is via kmalloc_index() and therefore evaluated at compile-time.
401 * Callers where !size_is_constant should only be test modules, where runtime
402 * overheads of __kmalloc_index() can be tolerated. Also see kmalloc_slab().
404 static __always_inline unsigned int __kmalloc_index(size_t size,
405 bool size_is_constant)
410 if (size <= KMALLOC_MIN_SIZE)
411 return KMALLOC_SHIFT_LOW;
413 if (KMALLOC_MIN_SIZE <= 32 && size > 64 && size <= 96)
415 if (KMALLOC_MIN_SIZE <= 64 && size > 128 && size <= 192)
417 if (size <= 8) return 3;
418 if (size <= 16) return 4;
419 if (size <= 32) return 5;
420 if (size <= 64) return 6;
421 if (size <= 128) return 7;
422 if (size <= 256) return 8;
423 if (size <= 512) return 9;
424 if (size <= 1024) return 10;
425 if (size <= 2 * 1024) return 11;
426 if (size <= 4 * 1024) return 12;
427 if (size <= 8 * 1024) return 13;
428 if (size <= 16 * 1024) return 14;
429 if (size <= 32 * 1024) return 15;
430 if (size <= 64 * 1024) return 16;
431 if (size <= 128 * 1024) return 17;
432 if (size <= 256 * 1024) return 18;
433 if (size <= 512 * 1024) return 19;
434 if (size <= 1024 * 1024) return 20;
435 if (size <= 2 * 1024 * 1024) return 21;
437 if (!IS_ENABLED(CONFIG_PROFILE_ALL_BRANCHES) && size_is_constant)
438 BUILD_BUG_ON_MSG(1, "unexpected size in kmalloc_index()");
442 /* Will never be reached. Needed because the compiler may complain */
445 static_assert(PAGE_SHIFT <= 20);
446 #define kmalloc_index(s) __kmalloc_index(s, true)
448 void *__kmalloc(size_t size, gfp_t flags) __assume_kmalloc_alignment __alloc_size(1);
451 * kmem_cache_alloc - Allocate an object
452 * @cachep: The cache to allocate from.
453 * @flags: See kmalloc().
455 * Allocate an object from this cache.
456 * See kmem_cache_zalloc() for a shortcut of adding __GFP_ZERO to flags.
458 * Return: pointer to the new object or %NULL in case of error
460 void *kmem_cache_alloc(struct kmem_cache *cachep, gfp_t flags) __assume_slab_alignment __malloc;
461 void *kmem_cache_alloc_lru(struct kmem_cache *s, struct list_lru *lru,
462 gfp_t gfpflags) __assume_slab_alignment __malloc;
463 void kmem_cache_free(struct kmem_cache *s, void *objp);
466 * Bulk allocation and freeing operations. These are accelerated in an
467 * allocator specific way to avoid taking locks repeatedly or building
468 * metadata structures unnecessarily.
470 * Note that interrupts must be enabled when calling these functions.
472 void kmem_cache_free_bulk(struct kmem_cache *s, size_t size, void **p);
473 int kmem_cache_alloc_bulk(struct kmem_cache *s, gfp_t flags, size_t size, void **p);
475 static __always_inline void kfree_bulk(size_t size, void **p)
477 kmem_cache_free_bulk(NULL, size, p);
480 void *__kmalloc_node(size_t size, gfp_t flags, int node) __assume_kmalloc_alignment
482 void *kmem_cache_alloc_node(struct kmem_cache *s, gfp_t flags, int node) __assume_slab_alignment
485 void *kmalloc_trace(struct kmem_cache *s, gfp_t flags, size_t size)
486 __assume_kmalloc_alignment __alloc_size(3);
488 void *kmalloc_node_trace(struct kmem_cache *s, gfp_t gfpflags,
489 int node, size_t size) __assume_kmalloc_alignment
491 void *kmalloc_large(size_t size, gfp_t flags) __assume_page_alignment
494 void *kmalloc_large_node(size_t size, gfp_t flags, int node) __assume_page_alignment
498 * kmalloc - allocate kernel memory
499 * @size: how many bytes of memory are required.
500 * @flags: describe the allocation context
502 * kmalloc is the normal method of allocating memory
503 * for objects smaller than page size in the kernel.
505 * The allocated object address is aligned to at least ARCH_KMALLOC_MINALIGN
506 * bytes. For @size of power of two bytes, the alignment is also guaranteed
507 * to be at least to the size.
509 * The @flags argument may be one of the GFP flags defined at
510 * include/linux/gfp_types.h and described at
511 * :ref:`Documentation/core-api/mm-api.rst <mm-api-gfp-flags>`
513 * The recommended usage of the @flags is described at
514 * :ref:`Documentation/core-api/memory-allocation.rst <memory_allocation>`
516 * Below is a brief outline of the most useful GFP flags
519 * Allocate normal kernel ram. May sleep.
522 * Allocation will not sleep.
525 * Allocation will not sleep. May use emergency pools.
527 * Also it is possible to set different flags by OR'ing
528 * in one or more of the following additional @flags:
531 * Zero the allocated memory before returning. Also see kzalloc().
534 * This allocation has high priority and may use emergency pools.
537 * Indicate that this allocation is in no way allowed to fail
538 * (think twice before using).
541 * If memory is not immediately available,
542 * then give up at once.
545 * If allocation fails, don't issue any warnings.
547 * %__GFP_RETRY_MAYFAIL
548 * Try really hard to succeed the allocation but fail
551 static __always_inline __alloc_size(1) void *kmalloc(size_t size, gfp_t flags)
553 if (__builtin_constant_p(size) && size) {
556 if (size > KMALLOC_MAX_CACHE_SIZE)
557 return kmalloc_large(size, flags);
559 index = kmalloc_index(size);
560 return kmalloc_trace(
561 kmalloc_caches[kmalloc_type(flags)][index],
564 return __kmalloc(size, flags);
567 static __always_inline __alloc_size(1) void *kmalloc_node(size_t size, gfp_t flags, int node)
569 if (__builtin_constant_p(size) && size) {
572 if (size > KMALLOC_MAX_CACHE_SIZE)
573 return kmalloc_large_node(size, flags, node);
575 index = kmalloc_index(size);
576 return kmalloc_node_trace(
577 kmalloc_caches[kmalloc_type(flags)][index],
580 return __kmalloc_node(size, flags, node);
584 * kmalloc_array - allocate memory for an array.
585 * @n: number of elements.
586 * @size: element size.
587 * @flags: the type of memory to allocate (see kmalloc).
589 static inline __alloc_size(1, 2) void *kmalloc_array(size_t n, size_t size, gfp_t flags)
593 if (unlikely(check_mul_overflow(n, size, &bytes)))
595 if (__builtin_constant_p(n) && __builtin_constant_p(size))
596 return kmalloc(bytes, flags);
597 return __kmalloc(bytes, flags);
601 * krealloc_array - reallocate memory for an array.
602 * @p: pointer to the memory chunk to reallocate
603 * @new_n: new number of elements to alloc
604 * @new_size: new size of a single member of the array
605 * @flags: the type of memory to allocate (see kmalloc)
607 static inline __realloc_size(2, 3) void * __must_check krealloc_array(void *p,
614 if (unlikely(check_mul_overflow(new_n, new_size, &bytes)))
617 return krealloc(p, bytes, flags);
621 * kcalloc - allocate memory for an array. The memory is set to zero.
622 * @n: number of elements.
623 * @size: element size.
624 * @flags: the type of memory to allocate (see kmalloc).
626 static inline __alloc_size(1, 2) void *kcalloc(size_t n, size_t size, gfp_t flags)
628 return kmalloc_array(n, size, flags | __GFP_ZERO);
631 void *__kmalloc_node_track_caller(size_t size, gfp_t flags, int node,
632 unsigned long caller) __alloc_size(1);
633 #define kmalloc_node_track_caller(size, flags, node) \
634 __kmalloc_node_track_caller(size, flags, node, \
638 * kmalloc_track_caller is a special version of kmalloc that records the
639 * calling function of the routine calling it for slab leak tracking instead
640 * of just the calling function (confusing, eh?).
641 * It's useful when the call to kmalloc comes from a widely-used standard
642 * allocator where we care about the real place the memory allocation
643 * request comes from.
645 #define kmalloc_track_caller(size, flags) \
646 __kmalloc_node_track_caller(size, flags, \
647 NUMA_NO_NODE, _RET_IP_)
649 static inline __alloc_size(1, 2) void *kmalloc_array_node(size_t n, size_t size, gfp_t flags,
654 if (unlikely(check_mul_overflow(n, size, &bytes)))
656 if (__builtin_constant_p(n) && __builtin_constant_p(size))
657 return kmalloc_node(bytes, flags, node);
658 return __kmalloc_node(bytes, flags, node);
661 static inline __alloc_size(1, 2) void *kcalloc_node(size_t n, size_t size, gfp_t flags, int node)
663 return kmalloc_array_node(n, size, flags | __GFP_ZERO, node);
669 static inline void *kmem_cache_zalloc(struct kmem_cache *k, gfp_t flags)
671 return kmem_cache_alloc(k, flags | __GFP_ZERO);
675 * kzalloc - allocate memory. The memory is set to zero.
676 * @size: how many bytes of memory are required.
677 * @flags: the type of memory to allocate (see kmalloc).
679 static inline __alloc_size(1) void *kzalloc(size_t size, gfp_t flags)
681 return kmalloc(size, flags | __GFP_ZERO);
685 * kzalloc_node - allocate zeroed memory from a particular memory node.
686 * @size: how many bytes of memory are required.
687 * @flags: the type of memory to allocate (see kmalloc).
688 * @node: memory node from which to allocate
690 static inline __alloc_size(1) void *kzalloc_node(size_t size, gfp_t flags, int node)
692 return kmalloc_node(size, flags | __GFP_ZERO, node);
695 extern void *kvmalloc_node(size_t size, gfp_t flags, int node) __alloc_size(1);
696 static inline __alloc_size(1) void *kvmalloc(size_t size, gfp_t flags)
698 return kvmalloc_node(size, flags, NUMA_NO_NODE);
700 static inline __alloc_size(1) void *kvzalloc_node(size_t size, gfp_t flags, int node)
702 return kvmalloc_node(size, flags | __GFP_ZERO, node);
704 static inline __alloc_size(1) void *kvzalloc(size_t size, gfp_t flags)
706 return kvmalloc(size, flags | __GFP_ZERO);
709 static inline __alloc_size(1, 2) void *kvmalloc_array(size_t n, size_t size, gfp_t flags)
713 if (unlikely(check_mul_overflow(n, size, &bytes)))
716 return kvmalloc(bytes, flags);
719 static inline __alloc_size(1, 2) void *kvcalloc(size_t n, size_t size, gfp_t flags)
721 return kvmalloc_array(n, size, flags | __GFP_ZERO);
724 extern void *kvrealloc(const void *p, size_t oldsize, size_t newsize, gfp_t flags)
726 extern void kvfree(const void *addr);
727 extern void kvfree_sensitive(const void *addr, size_t len);
729 unsigned int kmem_cache_size(struct kmem_cache *s);
732 * kmalloc_size_roundup - Report allocation bucket size for the given size
734 * @size: Number of bytes to round up from.
736 * This returns the number of bytes that would be available in a kmalloc()
737 * allocation of @size bytes. For example, a 126 byte request would be
738 * rounded up to the next sized kmalloc bucket, 128 bytes. (This is strictly
739 * for the general-purpose kmalloc()-based allocations, and is not for the
740 * pre-sized kmem_cache_alloc()-based allocations.)
742 * Use this to kmalloc() the full bucket size ahead of time instead of using
743 * ksize() to query the size after an allocation.
745 size_t kmalloc_size_roundup(size_t size);
747 void __init kmem_cache_init_late(void);
749 #if defined(CONFIG_SMP) && defined(CONFIG_SLAB)
750 int slab_prepare_cpu(unsigned int cpu);
751 int slab_dead_cpu(unsigned int cpu);
753 #define slab_prepare_cpu NULL
754 #define slab_dead_cpu NULL
757 #endif /* _LINUX_SLAB_H */