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 #include <linux/cleanup.h>
22 #include <linux/hash.h>
23 
24 enum _slab_flag_bits {
25 	_SLAB_CONSISTENCY_CHECKS,
26 	_SLAB_RED_ZONE,
27 	_SLAB_POISON,
28 	_SLAB_KMALLOC,
29 	_SLAB_HWCACHE_ALIGN,
30 	_SLAB_CACHE_DMA,
31 	_SLAB_CACHE_DMA32,
32 	_SLAB_STORE_USER,
33 	_SLAB_PANIC,
34 	_SLAB_TYPESAFE_BY_RCU,
35 	_SLAB_TRACE,
36 #ifdef CONFIG_DEBUG_OBJECTS
37 	_SLAB_DEBUG_OBJECTS,
38 #endif
39 	_SLAB_NOLEAKTRACE,
40 	_SLAB_NO_MERGE,
41 #ifdef CONFIG_FAILSLAB
42 	_SLAB_FAILSLAB,
43 #endif
44 #ifdef CONFIG_MEMCG
45 	_SLAB_ACCOUNT,
46 #endif
47 #ifdef CONFIG_KASAN_GENERIC
48 	_SLAB_KASAN,
49 #endif
50 	_SLAB_NO_USER_FLAGS,
51 #ifdef CONFIG_KFENCE
52 	_SLAB_SKIP_KFENCE,
53 #endif
54 #ifndef CONFIG_SLUB_TINY
55 	_SLAB_RECLAIM_ACCOUNT,
56 #endif
57 	_SLAB_OBJECT_POISON,
58 	_SLAB_CMPXCHG_DOUBLE,
59 #ifdef CONFIG_SLAB_OBJ_EXT
60 	_SLAB_NO_OBJ_EXT,
61 #endif
62 	_SLAB_FLAGS_LAST_BIT
63 };
64 
65 #define __SLAB_FLAG_BIT(nr)	((slab_flags_t __force)(1U << (nr)))
66 #define __SLAB_FLAG_UNUSED	((slab_flags_t __force)(0U))
67 
68 /*
69  * Flags to pass to kmem_cache_create().
70  * The ones marked DEBUG need CONFIG_SLUB_DEBUG enabled, otherwise are no-op
71  */
72 /* DEBUG: Perform (expensive) checks on alloc/free */
73 #define SLAB_CONSISTENCY_CHECKS	__SLAB_FLAG_BIT(_SLAB_CONSISTENCY_CHECKS)
74 /* DEBUG: Red zone objs in a cache */
75 #define SLAB_RED_ZONE		__SLAB_FLAG_BIT(_SLAB_RED_ZONE)
76 /* DEBUG: Poison objects */
77 #define SLAB_POISON		__SLAB_FLAG_BIT(_SLAB_POISON)
78 /* Indicate a kmalloc slab */
79 #define SLAB_KMALLOC		__SLAB_FLAG_BIT(_SLAB_KMALLOC)
80 /* Align objs on cache lines */
81 #define SLAB_HWCACHE_ALIGN	__SLAB_FLAG_BIT(_SLAB_HWCACHE_ALIGN)
82 /* Use GFP_DMA memory */
83 #define SLAB_CACHE_DMA		__SLAB_FLAG_BIT(_SLAB_CACHE_DMA)
84 /* Use GFP_DMA32 memory */
85 #define SLAB_CACHE_DMA32	__SLAB_FLAG_BIT(_SLAB_CACHE_DMA32)
86 /* DEBUG: Store the last owner for bug hunting */
87 #define SLAB_STORE_USER		__SLAB_FLAG_BIT(_SLAB_STORE_USER)
88 /* Panic if kmem_cache_create() fails */
89 #define SLAB_PANIC		__SLAB_FLAG_BIT(_SLAB_PANIC)
90 /*
91  * SLAB_TYPESAFE_BY_RCU - **WARNING** READ THIS!
92  *
93  * This delays freeing the SLAB page by a grace period, it does _NOT_
94  * delay object freeing. This means that if you do kmem_cache_free()
95  * that memory location is free to be reused at any time. Thus it may
96  * be possible to see another object there in the same RCU grace period.
97  *
98  * This feature only ensures the memory location backing the object
99  * stays valid, the trick to using this is relying on an independent
100  * object validation pass. Something like:
101  *
102  * begin:
103  *  rcu_read_lock();
104  *  obj = lockless_lookup(key);
105  *  if (obj) {
106  *    if (!try_get_ref(obj)) // might fail for free objects
107  *      rcu_read_unlock();
108  *      goto begin;
109  *
110  *    if (obj->key != key) { // not the object we expected
111  *      put_ref(obj);
112  *      rcu_read_unlock();
113  *      goto begin;
114  *    }
115  *  }
116  *  rcu_read_unlock();
117  *
118  * This is useful if we need to approach a kernel structure obliquely,
119  * from its address obtained without the usual locking. We can lock
120  * the structure to stabilize it and check it's still at the given address,
121  * only if we can be sure that the memory has not been meanwhile reused
122  * for some other kind of object (which our subsystem's lock might corrupt).
123  *
124  * rcu_read_lock before reading the address, then rcu_read_unlock after
125  * taking the spinlock within the structure expected at that address.
126  *
127  * Note that object identity check has to be done *after* acquiring a
128  * reference, therefore user has to ensure proper ordering for loads.
129  * Similarly, when initializing objects allocated with SLAB_TYPESAFE_BY_RCU,
130  * the newly allocated object has to be fully initialized *before* its
131  * refcount gets initialized and proper ordering for stores is required.
132  * refcount_{add|inc}_not_zero_acquire() and refcount_set_release() are
133  * designed with the proper fences required for reference counting objects
134  * allocated with SLAB_TYPESAFE_BY_RCU.
135  *
136  * Note that it is not possible to acquire a lock within a structure
137  * allocated with SLAB_TYPESAFE_BY_RCU without first acquiring a reference
138  * as described above.  The reason is that SLAB_TYPESAFE_BY_RCU pages
139  * are not zeroed before being given to the slab, which means that any
140  * locks must be initialized after each and every kmem_struct_alloc().
141  * Alternatively, make the ctor passed to kmem_cache_create() initialize
142  * the locks at page-allocation time, as is done in __i915_request_ctor(),
143  * sighand_ctor(), and anon_vma_ctor().  Such a ctor permits readers
144  * to safely acquire those ctor-initialized locks under rcu_read_lock()
145  * protection.
146  *
147  * Note that SLAB_TYPESAFE_BY_RCU was originally named SLAB_DESTROY_BY_RCU.
148  */
149 /* Defer freeing slabs to RCU */
150 #define SLAB_TYPESAFE_BY_RCU	__SLAB_FLAG_BIT(_SLAB_TYPESAFE_BY_RCU)
151 /* Trace allocations and frees */
152 #define SLAB_TRACE		__SLAB_FLAG_BIT(_SLAB_TRACE)
153 
154 /* Flag to prevent checks on free */
155 #ifdef CONFIG_DEBUG_OBJECTS
156 # define SLAB_DEBUG_OBJECTS	__SLAB_FLAG_BIT(_SLAB_DEBUG_OBJECTS)
157 #else
158 # define SLAB_DEBUG_OBJECTS	__SLAB_FLAG_UNUSED
159 #endif
160 
161 /* Avoid kmemleak tracing */
162 #define SLAB_NOLEAKTRACE	__SLAB_FLAG_BIT(_SLAB_NOLEAKTRACE)
163 
164 /*
165  * Prevent merging with compatible kmem caches. This flag should be used
166  * cautiously. Valid use cases:
167  *
168  * - caches created for self-tests (e.g. kunit)
169  * - general caches created and used by a subsystem, only when a
170  *   (subsystem-specific) debug option is enabled
171  * - performance critical caches, should be very rare and consulted with slab
172  *   maintainers, and not used together with CONFIG_SLUB_TINY
173  */
174 #define SLAB_NO_MERGE		__SLAB_FLAG_BIT(_SLAB_NO_MERGE)
175 
176 /* Fault injection mark */
177 #ifdef CONFIG_FAILSLAB
178 # define SLAB_FAILSLAB		__SLAB_FLAG_BIT(_SLAB_FAILSLAB)
179 #else
180 # define SLAB_FAILSLAB		__SLAB_FLAG_UNUSED
181 #endif
182 /* Account to memcg */
183 #ifdef CONFIG_MEMCG
184 # define SLAB_ACCOUNT		__SLAB_FLAG_BIT(_SLAB_ACCOUNT)
185 #else
186 # define SLAB_ACCOUNT		__SLAB_FLAG_UNUSED
187 #endif
188 
189 #ifdef CONFIG_KASAN_GENERIC
190 #define SLAB_KASAN		__SLAB_FLAG_BIT(_SLAB_KASAN)
191 #else
192 #define SLAB_KASAN		__SLAB_FLAG_UNUSED
193 #endif
194 
195 /*
196  * Ignore user specified debugging flags.
197  * Intended for caches created for self-tests so they have only flags
198  * specified in the code and other flags are ignored.
199  */
200 #define SLAB_NO_USER_FLAGS	__SLAB_FLAG_BIT(_SLAB_NO_USER_FLAGS)
201 
202 #ifdef CONFIG_KFENCE
203 #define SLAB_SKIP_KFENCE	__SLAB_FLAG_BIT(_SLAB_SKIP_KFENCE)
204 #else
205 #define SLAB_SKIP_KFENCE	__SLAB_FLAG_UNUSED
206 #endif
207 
208 /* The following flags affect the page allocator grouping pages by mobility */
209 /* Objects are reclaimable */
210 #ifndef CONFIG_SLUB_TINY
211 #define SLAB_RECLAIM_ACCOUNT	__SLAB_FLAG_BIT(_SLAB_RECLAIM_ACCOUNT)
212 #else
213 #define SLAB_RECLAIM_ACCOUNT	__SLAB_FLAG_UNUSED
214 #endif
215 #define SLAB_TEMPORARY		SLAB_RECLAIM_ACCOUNT	/* Objects are short-lived */
216 
217 /* Slab created using create_boot_cache */
218 #ifdef CONFIG_SLAB_OBJ_EXT
219 #define SLAB_NO_OBJ_EXT		__SLAB_FLAG_BIT(_SLAB_NO_OBJ_EXT)
220 #else
221 #define SLAB_NO_OBJ_EXT		__SLAB_FLAG_UNUSED
222 #endif
223 
224 /*
225  * ZERO_SIZE_PTR will be returned for zero sized kmalloc requests.
226  *
227  * Dereferencing ZERO_SIZE_PTR will lead to a distinct access fault.
228  *
229  * ZERO_SIZE_PTR can be passed to kfree though in the same way that NULL can.
230  * Both make kfree a no-op.
231  */
232 #define ZERO_SIZE_PTR ((void *)16)
233 
234 #define ZERO_OR_NULL_PTR(x) ((unsigned long)(x) <= \
235 				(unsigned long)ZERO_SIZE_PTR)
236 
237 #include <linux/kasan.h>
238 
239 struct list_lru;
240 struct mem_cgroup;
241 /*
242  * struct kmem_cache related prototypes
243  */
244 bool slab_is_available(void);
245 
246 /**
247  * struct kmem_cache_args - Less common arguments for kmem_cache_create()
248  *
249  * Any uninitialized fields of the structure are interpreted as unused. The
250  * exception is @freeptr_offset where %0 is a valid value, so
251  * @use_freeptr_offset must be also set to %true in order to interpret the field
252  * as used. For @useroffset %0 is also valid, but only with non-%0
253  * @usersize.
254  *
255  * When %NULL args is passed to kmem_cache_create(), it is equivalent to all
256  * fields unused.
257  */
258 struct kmem_cache_args {
259 	/**
260 	 * @align: The required alignment for the objects.
261 	 *
262 	 * %0 means no specific alignment is requested.
263 	 */
264 	unsigned int align;
265 	/**
266 	 * @useroffset: Usercopy region offset.
267 	 *
268 	 * %0 is a valid offset, when @usersize is non-%0
269 	 */
270 	unsigned int useroffset;
271 	/**
272 	 * @usersize: Usercopy region size.
273 	 *
274 	 * %0 means no usercopy region is specified.
275 	 */
276 	unsigned int usersize;
277 	/**
278 	 * @freeptr_offset: Custom offset for the free pointer
279 	 * in &SLAB_TYPESAFE_BY_RCU caches
280 	 *
281 	 * By default &SLAB_TYPESAFE_BY_RCU caches place the free pointer
282 	 * outside of the object. This might cause the object to grow in size.
283 	 * Cache creators that have a reason to avoid this can specify a custom
284 	 * free pointer offset in their struct where the free pointer will be
285 	 * placed.
286 	 *
287 	 * Note that placing the free pointer inside the object requires the
288 	 * caller to ensure that no fields are invalidated that are required to
289 	 * guard against object recycling (See &SLAB_TYPESAFE_BY_RCU for
290 	 * details).
291 	 *
292 	 * Using %0 as a value for @freeptr_offset is valid. If @freeptr_offset
293 	 * is specified, %use_freeptr_offset must be set %true.
294 	 *
295 	 * Note that @ctor currently isn't supported with custom free pointers
296 	 * as a @ctor requires an external free pointer.
297 	 */
298 	unsigned int freeptr_offset;
299 	/**
300 	 * @use_freeptr_offset: Whether a @freeptr_offset is used.
301 	 */
302 	bool use_freeptr_offset;
303 	/**
304 	 * @ctor: A constructor for the objects.
305 	 *
306 	 * The constructor is invoked for each object in a newly allocated slab
307 	 * page. It is the cache user's responsibility to free object in the
308 	 * same state as after calling the constructor, or deal appropriately
309 	 * with any differences between a freshly constructed and a reallocated
310 	 * object.
311 	 *
312 	 * %NULL means no constructor.
313 	 */
314 	void (*ctor)(void *);
315 };
316 
317 struct kmem_cache *__kmem_cache_create_args(const char *name,
318 					    unsigned int object_size,
319 					    struct kmem_cache_args *args,
320 					    slab_flags_t flags);
321 static inline struct kmem_cache *
__kmem_cache_create(const char * name,unsigned int size,unsigned int align,slab_flags_t flags,void (* ctor)(void *))322 __kmem_cache_create(const char *name, unsigned int size, unsigned int align,
323 		    slab_flags_t flags, void (*ctor)(void *))
324 {
325 	struct kmem_cache_args kmem_args = {
326 		.align	= align,
327 		.ctor	= ctor,
328 	};
329 
330 	return __kmem_cache_create_args(name, size, &kmem_args, flags);
331 }
332 
333 /**
334  * kmem_cache_create_usercopy - Create a kmem cache with a region suitable
335  * for copying to userspace.
336  * @name: A string which is used in /proc/slabinfo to identify this cache.
337  * @size: The size of objects to be created in this cache.
338  * @align: The required alignment for the objects.
339  * @flags: SLAB flags
340  * @useroffset: Usercopy region offset
341  * @usersize: Usercopy region size
342  * @ctor: A constructor for the objects, or %NULL.
343  *
344  * This is a legacy wrapper, new code should use either KMEM_CACHE_USERCOPY()
345  * if whitelisting a single field is sufficient, or kmem_cache_create() with
346  * the necessary parameters passed via the args parameter (see
347  * &struct kmem_cache_args)
348  *
349  * Return: a pointer to the cache on success, NULL on failure.
350  */
351 static inline struct kmem_cache *
kmem_cache_create_usercopy(const char * name,unsigned int size,unsigned int align,slab_flags_t flags,unsigned int useroffset,unsigned int usersize,void (* ctor)(void *))352 kmem_cache_create_usercopy(const char *name, unsigned int size,
353 			   unsigned int align, slab_flags_t flags,
354 			   unsigned int useroffset, unsigned int usersize,
355 			   void (*ctor)(void *))
356 {
357 	struct kmem_cache_args kmem_args = {
358 		.align		= align,
359 		.ctor		= ctor,
360 		.useroffset	= useroffset,
361 		.usersize	= usersize,
362 	};
363 
364 	return __kmem_cache_create_args(name, size, &kmem_args, flags);
365 }
366 
367 /* If NULL is passed for @args, use this variant with default arguments. */
368 static inline struct kmem_cache *
__kmem_cache_default_args(const char * name,unsigned int size,struct kmem_cache_args * args,slab_flags_t flags)369 __kmem_cache_default_args(const char *name, unsigned int size,
370 			  struct kmem_cache_args *args,
371 			  slab_flags_t flags)
372 {
373 	struct kmem_cache_args kmem_default_args = {};
374 
375 	/* Make sure we don't get passed garbage. */
376 	if (WARN_ON_ONCE(args))
377 		return ERR_PTR(-EINVAL);
378 
379 	return __kmem_cache_create_args(name, size, &kmem_default_args, flags);
380 }
381 
382 /**
383  * kmem_cache_create - Create a kmem cache.
384  * @__name: A string which is used in /proc/slabinfo to identify this cache.
385  * @__object_size: The size of objects to be created in this cache.
386  * @__args: Optional arguments, see &struct kmem_cache_args. Passing %NULL
387  *	    means defaults will be used for all the arguments.
388  *
389  * This is currently implemented as a macro using ``_Generic()`` to call
390  * either the new variant of the function, or a legacy one.
391  *
392  * The new variant has 4 parameters:
393  * ``kmem_cache_create(name, object_size, args, flags)``
394  *
395  * See __kmem_cache_create_args() which implements this.
396  *
397  * The legacy variant has 5 parameters:
398  * ``kmem_cache_create(name, object_size, align, flags, ctor)``
399  *
400  * The align and ctor parameters map to the respective fields of
401  * &struct kmem_cache_args
402  *
403  * Context: Cannot be called within a interrupt, but can be interrupted.
404  *
405  * Return: a pointer to the cache on success, NULL on failure.
406  */
407 #define kmem_cache_create(__name, __object_size, __args, ...)           \
408 	_Generic((__args),                                              \
409 		struct kmem_cache_args *: __kmem_cache_create_args,	\
410 		void *: __kmem_cache_default_args,			\
411 		default: __kmem_cache_create)(__name, __object_size, __args, __VA_ARGS__)
412 
413 void kmem_cache_destroy(struct kmem_cache *s);
414 int kmem_cache_shrink(struct kmem_cache *s);
415 
416 /*
417  * Please use this macro to create slab caches. Simply specify the
418  * name of the structure and maybe some flags that are listed above.
419  *
420  * The alignment of the struct determines object alignment. If you
421  * f.e. add ____cacheline_aligned_in_smp to the struct declaration
422  * then the objects will be properly aligned in SMP configurations.
423  */
424 #define KMEM_CACHE(__struct, __flags)                                   \
425 	__kmem_cache_create_args(#__struct, sizeof(struct __struct),    \
426 			&(struct kmem_cache_args) {			\
427 				.align	= __alignof__(struct __struct), \
428 			}, (__flags))
429 
430 /*
431  * To whitelist a single field for copying to/from usercopy, use this
432  * macro instead for KMEM_CACHE() above.
433  */
434 #define KMEM_CACHE_USERCOPY(__struct, __flags, __field)						\
435 	__kmem_cache_create_args(#__struct, sizeof(struct __struct),				\
436 			&(struct kmem_cache_args) {						\
437 				.align		= __alignof__(struct __struct),			\
438 				.useroffset	= offsetof(struct __struct, __field),		\
439 				.usersize	= sizeof_field(struct __struct, __field),	\
440 			}, (__flags))
441 
442 /*
443  * Common kmalloc functions provided by all allocators
444  */
445 void * __must_check krealloc_noprof(const void *objp, size_t new_size,
446 				    gfp_t flags) __realloc_size(2);
447 #define krealloc(...)				alloc_hooks(krealloc_noprof(__VA_ARGS__))
448 
449 void kfree(const void *objp);
450 void kfree_sensitive(const void *objp);
451 size_t __ksize(const void *objp);
452 
453 DEFINE_FREE(kfree, void *, if (!IS_ERR_OR_NULL(_T)) kfree(_T))
454 
455 /**
456  * ksize - Report actual allocation size of associated object
457  *
458  * @objp: Pointer returned from a prior kmalloc()-family allocation.
459  *
460  * This should not be used for writing beyond the originally requested
461  * allocation size. Either use krealloc() or round up the allocation size
462  * with kmalloc_size_roundup() prior to allocation. If this is used to
463  * access beyond the originally requested allocation size, UBSAN_BOUNDS
464  * and/or FORTIFY_SOURCE may trip, since they only know about the
465  * originally allocated size via the __alloc_size attribute.
466  */
467 size_t ksize(const void *objp);
468 
469 #ifdef CONFIG_PRINTK
470 bool kmem_dump_obj(void *object);
471 #else
kmem_dump_obj(void * object)472 static inline bool kmem_dump_obj(void *object) { return false; }
473 #endif
474 
475 /*
476  * Some archs want to perform DMA into kmalloc caches and need a guaranteed
477  * alignment larger than the alignment of a 64-bit integer.
478  * Setting ARCH_DMA_MINALIGN in arch headers allows that.
479  */
480 #ifdef ARCH_HAS_DMA_MINALIGN
481 #if ARCH_DMA_MINALIGN > 8 && !defined(ARCH_KMALLOC_MINALIGN)
482 #define ARCH_KMALLOC_MINALIGN ARCH_DMA_MINALIGN
483 #endif
484 #endif
485 
486 #ifndef ARCH_KMALLOC_MINALIGN
487 #define ARCH_KMALLOC_MINALIGN __alignof__(unsigned long long)
488 #elif ARCH_KMALLOC_MINALIGN > 8
489 #define KMALLOC_MIN_SIZE ARCH_KMALLOC_MINALIGN
490 #define KMALLOC_SHIFT_LOW ilog2(KMALLOC_MIN_SIZE)
491 #endif
492 
493 /*
494  * Setting ARCH_SLAB_MINALIGN in arch headers allows a different alignment.
495  * Intended for arches that get misalignment faults even for 64 bit integer
496  * aligned buffers.
497  */
498 #ifndef ARCH_SLAB_MINALIGN
499 #define ARCH_SLAB_MINALIGN __alignof__(unsigned long long)
500 #endif
501 
502 /*
503  * Arches can define this function if they want to decide the minimum slab
504  * alignment at runtime. The value returned by the function must be a power
505  * of two and >= ARCH_SLAB_MINALIGN.
506  */
507 #ifndef arch_slab_minalign
arch_slab_minalign(void)508 static inline unsigned int arch_slab_minalign(void)
509 {
510 	return ARCH_SLAB_MINALIGN;
511 }
512 #endif
513 
514 /*
515  * kmem_cache_alloc and friends return pointers aligned to ARCH_SLAB_MINALIGN.
516  * kmalloc and friends return pointers aligned to both ARCH_KMALLOC_MINALIGN
517  * and ARCH_SLAB_MINALIGN, but here we only assume the former alignment.
518  */
519 #define __assume_kmalloc_alignment __assume_aligned(ARCH_KMALLOC_MINALIGN)
520 #define __assume_slab_alignment __assume_aligned(ARCH_SLAB_MINALIGN)
521 #define __assume_page_alignment __assume_aligned(PAGE_SIZE)
522 
523 /*
524  * Kmalloc array related definitions
525  */
526 
527 /*
528  * SLUB directly allocates requests fitting in to an order-1 page
529  * (PAGE_SIZE*2).  Larger requests are passed to the page allocator.
530  */
531 #define KMALLOC_SHIFT_HIGH	(PAGE_SHIFT + 1)
532 #define KMALLOC_SHIFT_MAX	(MAX_PAGE_ORDER + PAGE_SHIFT)
533 #ifndef KMALLOC_SHIFT_LOW
534 #define KMALLOC_SHIFT_LOW	3
535 #endif
536 
537 /* Maximum allocatable size */
538 #define KMALLOC_MAX_SIZE	(1UL << KMALLOC_SHIFT_MAX)
539 /* Maximum size for which we actually use a slab cache */
540 #define KMALLOC_MAX_CACHE_SIZE	(1UL << KMALLOC_SHIFT_HIGH)
541 /* Maximum order allocatable via the slab allocator */
542 #define KMALLOC_MAX_ORDER	(KMALLOC_SHIFT_MAX - PAGE_SHIFT)
543 
544 /*
545  * Kmalloc subsystem.
546  */
547 #ifndef KMALLOC_MIN_SIZE
548 #define KMALLOC_MIN_SIZE (1 << KMALLOC_SHIFT_LOW)
549 #endif
550 
551 /*
552  * This restriction comes from byte sized index implementation.
553  * Page size is normally 2^12 bytes and, in this case, if we want to use
554  * byte sized index which can represent 2^8 entries, the size of the object
555  * should be equal or greater to 2^12 / 2^8 = 2^4 = 16.
556  * If minimum size of kmalloc is less than 16, we use it as minimum object
557  * size and give up to use byte sized index.
558  */
559 #define SLAB_OBJ_MIN_SIZE      (KMALLOC_MIN_SIZE < 16 ? \
560                                (KMALLOC_MIN_SIZE) : 16)
561 
562 #ifdef CONFIG_RANDOM_KMALLOC_CACHES
563 #define RANDOM_KMALLOC_CACHES_NR	15 // # of cache copies
564 #else
565 #define RANDOM_KMALLOC_CACHES_NR	0
566 #endif
567 
568 /*
569  * Whenever changing this, take care of that kmalloc_type() and
570  * create_kmalloc_caches() still work as intended.
571  *
572  * KMALLOC_NORMAL can contain only unaccounted objects whereas KMALLOC_CGROUP
573  * is for accounted but unreclaimable and non-dma objects. All the other
574  * kmem caches can have both accounted and unaccounted objects.
575  */
576 enum kmalloc_cache_type {
577 	KMALLOC_NORMAL = 0,
578 #ifndef CONFIG_ZONE_DMA
579 	KMALLOC_DMA = KMALLOC_NORMAL,
580 #endif
581 #ifndef CONFIG_MEMCG
582 	KMALLOC_CGROUP = KMALLOC_NORMAL,
583 #endif
584 	KMALLOC_RANDOM_START = KMALLOC_NORMAL,
585 	KMALLOC_RANDOM_END = KMALLOC_RANDOM_START + RANDOM_KMALLOC_CACHES_NR,
586 #ifdef CONFIG_SLUB_TINY
587 	KMALLOC_RECLAIM = KMALLOC_NORMAL,
588 #else
589 	KMALLOC_RECLAIM,
590 #endif
591 #ifdef CONFIG_ZONE_DMA
592 	KMALLOC_DMA,
593 #endif
594 #ifdef CONFIG_MEMCG
595 	KMALLOC_CGROUP,
596 #endif
597 	NR_KMALLOC_TYPES
598 };
599 
600 typedef struct kmem_cache * kmem_buckets[KMALLOC_SHIFT_HIGH + 1];
601 
602 extern kmem_buckets kmalloc_caches[NR_KMALLOC_TYPES];
603 
604 /*
605  * Define gfp bits that should not be set for KMALLOC_NORMAL.
606  */
607 #define KMALLOC_NOT_NORMAL_BITS					\
608 	(__GFP_RECLAIMABLE |					\
609 	(IS_ENABLED(CONFIG_ZONE_DMA)   ? __GFP_DMA : 0) |	\
610 	(IS_ENABLED(CONFIG_MEMCG) ? __GFP_ACCOUNT : 0))
611 
612 extern unsigned long random_kmalloc_seed;
613 
kmalloc_type(gfp_t flags,unsigned long caller)614 static __always_inline enum kmalloc_cache_type kmalloc_type(gfp_t flags, unsigned long caller)
615 {
616 	/*
617 	 * The most common case is KMALLOC_NORMAL, so test for it
618 	 * with a single branch for all the relevant flags.
619 	 */
620 	if (likely((flags & KMALLOC_NOT_NORMAL_BITS) == 0))
621 #ifdef CONFIG_RANDOM_KMALLOC_CACHES
622 		/* RANDOM_KMALLOC_CACHES_NR (=15) copies + the KMALLOC_NORMAL */
623 		return KMALLOC_RANDOM_START + hash_64(caller ^ random_kmalloc_seed,
624 						      ilog2(RANDOM_KMALLOC_CACHES_NR + 1));
625 #else
626 		return KMALLOC_NORMAL;
627 #endif
628 
629 	/*
630 	 * At least one of the flags has to be set. Their priorities in
631 	 * decreasing order are:
632 	 *  1) __GFP_DMA
633 	 *  2) __GFP_RECLAIMABLE
634 	 *  3) __GFP_ACCOUNT
635 	 */
636 	if (IS_ENABLED(CONFIG_ZONE_DMA) && (flags & __GFP_DMA))
637 		return KMALLOC_DMA;
638 	if (!IS_ENABLED(CONFIG_MEMCG) || (flags & __GFP_RECLAIMABLE))
639 		return KMALLOC_RECLAIM;
640 	else
641 		return KMALLOC_CGROUP;
642 }
643 
644 /*
645  * Figure out which kmalloc slab an allocation of a certain size
646  * belongs to.
647  * 0 = zero alloc
648  * 1 =  65 .. 96 bytes
649  * 2 = 129 .. 192 bytes
650  * n = 2^(n-1)+1 .. 2^n
651  *
652  * Note: __kmalloc_index() is compile-time optimized, and not runtime optimized;
653  * typical usage is via kmalloc_index() and therefore evaluated at compile-time.
654  * Callers where !size_is_constant should only be test modules, where runtime
655  * overheads of __kmalloc_index() can be tolerated.  Also see kmalloc_slab().
656  */
__kmalloc_index(size_t size,bool size_is_constant)657 static __always_inline unsigned int __kmalloc_index(size_t size,
658 						    bool size_is_constant)
659 {
660 	if (!size)
661 		return 0;
662 
663 	if (size <= KMALLOC_MIN_SIZE)
664 		return KMALLOC_SHIFT_LOW;
665 
666 	if (KMALLOC_MIN_SIZE <= 32 && size > 64 && size <= 96)
667 		return 1;
668 	if (KMALLOC_MIN_SIZE <= 64 && size > 128 && size <= 192)
669 		return 2;
670 	if (size <=          8) return 3;
671 	if (size <=         16) return 4;
672 	if (size <=         32) return 5;
673 	if (size <=         64) return 6;
674 	if (size <=        128) return 7;
675 	if (size <=        256) return 8;
676 	if (size <=        512) return 9;
677 	if (size <=       1024) return 10;
678 	if (size <=   2 * 1024) return 11;
679 	if (size <=   4 * 1024) return 12;
680 	if (size <=   8 * 1024) return 13;
681 	if (size <=  16 * 1024) return 14;
682 	if (size <=  32 * 1024) return 15;
683 	if (size <=  64 * 1024) return 16;
684 	if (size <= 128 * 1024) return 17;
685 	if (size <= 256 * 1024) return 18;
686 	if (size <= 512 * 1024) return 19;
687 	if (size <= 1024 * 1024) return 20;
688 	if (size <=  2 * 1024 * 1024) return 21;
689 
690 	if (!IS_ENABLED(CONFIG_PROFILE_ALL_BRANCHES) && size_is_constant)
691 		BUILD_BUG_ON_MSG(1, "unexpected size in kmalloc_index()");
692 	else
693 		BUG();
694 
695 	/* Will never be reached. Needed because the compiler may complain */
696 	return -1;
697 }
698 static_assert(PAGE_SHIFT <= 20);
699 #define kmalloc_index(s) __kmalloc_index(s, true)
700 
701 #include <linux/alloc_tag.h>
702 
703 /**
704  * kmem_cache_alloc - Allocate an object
705  * @cachep: The cache to allocate from.
706  * @flags: See kmalloc().
707  *
708  * Allocate an object from this cache.
709  * See kmem_cache_zalloc() for a shortcut of adding __GFP_ZERO to flags.
710  *
711  * Return: pointer to the new object or %NULL in case of error
712  */
713 void *kmem_cache_alloc_noprof(struct kmem_cache *cachep,
714 			      gfp_t flags) __assume_slab_alignment __malloc;
715 #define kmem_cache_alloc(...)			alloc_hooks(kmem_cache_alloc_noprof(__VA_ARGS__))
716 
717 void *kmem_cache_alloc_lru_noprof(struct kmem_cache *s, struct list_lru *lru,
718 			    gfp_t gfpflags) __assume_slab_alignment __malloc;
719 #define kmem_cache_alloc_lru(...)	alloc_hooks(kmem_cache_alloc_lru_noprof(__VA_ARGS__))
720 
721 /**
722  * kmem_cache_charge - memcg charge an already allocated slab memory
723  * @objp: address of the slab object to memcg charge
724  * @gfpflags: describe the allocation context
725  *
726  * kmem_cache_charge allows charging a slab object to the current memcg,
727  * primarily in cases where charging at allocation time might not be possible
728  * because the target memcg is not known (i.e. softirq context)
729  *
730  * The objp should be pointer returned by the slab allocator functions like
731  * kmalloc (with __GFP_ACCOUNT in flags) or kmem_cache_alloc. The memcg charge
732  * behavior can be controlled through gfpflags parameter, which affects how the
733  * necessary internal metadata can be allocated. Including __GFP_NOFAIL denotes
734  * that overcharging is requested instead of failure, but is not applied for the
735  * internal metadata allocation.
736  *
737  * There are several cases where it will return true even if the charging was
738  * not done:
739  * More specifically:
740  *
741  * 1. For !CONFIG_MEMCG or cgroup_disable=memory systems.
742  * 2. Already charged slab objects.
743  * 3. For slab objects from KMALLOC_NORMAL caches - allocated by kmalloc()
744  *    without __GFP_ACCOUNT
745  * 4. Allocating internal metadata has failed
746  *
747  * Return: true if charge was successful otherwise false.
748  */
749 bool kmem_cache_charge(void *objp, gfp_t gfpflags);
750 void kmem_cache_free(struct kmem_cache *s, void *objp);
751 
752 kmem_buckets *kmem_buckets_create(const char *name, slab_flags_t flags,
753 				  unsigned int useroffset, unsigned int usersize,
754 				  void (*ctor)(void *));
755 
756 /*
757  * Bulk allocation and freeing operations. These are accelerated in an
758  * allocator specific way to avoid taking locks repeatedly or building
759  * metadata structures unnecessarily.
760  *
761  * Note that interrupts must be enabled when calling these functions.
762  */
763 void kmem_cache_free_bulk(struct kmem_cache *s, size_t size, void **p);
764 
765 int kmem_cache_alloc_bulk_noprof(struct kmem_cache *s, gfp_t flags, size_t size, void **p);
766 #define kmem_cache_alloc_bulk(...)	alloc_hooks(kmem_cache_alloc_bulk_noprof(__VA_ARGS__))
767 
kfree_bulk(size_t size,void ** p)768 static __always_inline void kfree_bulk(size_t size, void **p)
769 {
770 	kmem_cache_free_bulk(NULL, size, p);
771 }
772 
773 void *kmem_cache_alloc_node_noprof(struct kmem_cache *s, gfp_t flags,
774 				   int node) __assume_slab_alignment __malloc;
775 #define kmem_cache_alloc_node(...)	alloc_hooks(kmem_cache_alloc_node_noprof(__VA_ARGS__))
776 
777 /*
778  * These macros allow declaring a kmem_buckets * parameter alongside size, which
779  * can be compiled out with CONFIG_SLAB_BUCKETS=n so that a large number of call
780  * sites don't have to pass NULL.
781  */
782 #ifdef CONFIG_SLAB_BUCKETS
783 #define DECL_BUCKET_PARAMS(_size, _b)	size_t (_size), kmem_buckets *(_b)
784 #define PASS_BUCKET_PARAMS(_size, _b)	(_size), (_b)
785 #define PASS_BUCKET_PARAM(_b)		(_b)
786 #else
787 #define DECL_BUCKET_PARAMS(_size, _b)	size_t (_size)
788 #define PASS_BUCKET_PARAMS(_size, _b)	(_size)
789 #define PASS_BUCKET_PARAM(_b)		NULL
790 #endif
791 
792 /*
793  * The following functions are not to be used directly and are intended only
794  * for internal use from kmalloc() and kmalloc_node()
795  * with the exception of kunit tests
796  */
797 
798 void *__kmalloc_noprof(size_t size, gfp_t flags)
799 				__assume_kmalloc_alignment __alloc_size(1);
800 
801 void *__kmalloc_node_noprof(DECL_BUCKET_PARAMS(size, b), gfp_t flags, int node)
802 				__assume_kmalloc_alignment __alloc_size(1);
803 
804 void *__kmalloc_cache_noprof(struct kmem_cache *s, gfp_t flags, size_t size)
805 				__assume_kmalloc_alignment __alloc_size(3);
806 
807 void *__kmalloc_cache_node_noprof(struct kmem_cache *s, gfp_t gfpflags,
808 				  int node, size_t size)
809 				__assume_kmalloc_alignment __alloc_size(4);
810 
811 void *__kmalloc_large_noprof(size_t size, gfp_t flags)
812 				__assume_page_alignment __alloc_size(1);
813 
814 void *__kmalloc_large_node_noprof(size_t size, gfp_t flags, int node)
815 				__assume_page_alignment __alloc_size(1);
816 
817 /**
818  * kmalloc - allocate kernel memory
819  * @size: how many bytes of memory are required.
820  * @flags: describe the allocation context
821  *
822  * kmalloc is the normal method of allocating memory
823  * for objects smaller than page size in the kernel.
824  *
825  * The allocated object address is aligned to at least ARCH_KMALLOC_MINALIGN
826  * bytes. For @size of power of two bytes, the alignment is also guaranteed
827  * to be at least to the size. For other sizes, the alignment is guaranteed to
828  * be at least the largest power-of-two divisor of @size.
829  *
830  * The @flags argument may be one of the GFP flags defined at
831  * include/linux/gfp_types.h and described at
832  * :ref:`Documentation/core-api/mm-api.rst <mm-api-gfp-flags>`
833  *
834  * The recommended usage of the @flags is described at
835  * :ref:`Documentation/core-api/memory-allocation.rst <memory_allocation>`
836  *
837  * Below is a brief outline of the most useful GFP flags
838  *
839  * %GFP_KERNEL
840  *	Allocate normal kernel ram. May sleep.
841  *
842  * %GFP_NOWAIT
843  *	Allocation will not sleep.
844  *
845  * %GFP_ATOMIC
846  *	Allocation will not sleep.  May use emergency pools.
847  *
848  * Also it is possible to set different flags by OR'ing
849  * in one or more of the following additional @flags:
850  *
851  * %__GFP_ZERO
852  *	Zero the allocated memory before returning. Also see kzalloc().
853  *
854  * %__GFP_HIGH
855  *	This allocation has high priority and may use emergency pools.
856  *
857  * %__GFP_NOFAIL
858  *	Indicate that this allocation is in no way allowed to fail
859  *	(think twice before using).
860  *
861  * %__GFP_NORETRY
862  *	If memory is not immediately available,
863  *	then give up at once.
864  *
865  * %__GFP_NOWARN
866  *	If allocation fails, don't issue any warnings.
867  *
868  * %__GFP_RETRY_MAYFAIL
869  *	Try really hard to succeed the allocation but fail
870  *	eventually.
871  */
kmalloc_noprof(size_t size,gfp_t flags)872 static __always_inline __alloc_size(1) void *kmalloc_noprof(size_t size, gfp_t flags)
873 {
874 	if (__builtin_constant_p(size) && size) {
875 		unsigned int index;
876 
877 		if (size > KMALLOC_MAX_CACHE_SIZE)
878 			return __kmalloc_large_noprof(size, flags);
879 
880 		index = kmalloc_index(size);
881 		return __kmalloc_cache_noprof(
882 				kmalloc_caches[kmalloc_type(flags, _RET_IP_)][index],
883 				flags, size);
884 	}
885 	return __kmalloc_noprof(size, flags);
886 }
887 #define kmalloc(...)				alloc_hooks(kmalloc_noprof(__VA_ARGS__))
888 
889 #define kmem_buckets_alloc(_b, _size, _flags)	\
890 	alloc_hooks(__kmalloc_node_noprof(PASS_BUCKET_PARAMS(_size, _b), _flags, NUMA_NO_NODE))
891 
892 #define kmem_buckets_alloc_track_caller(_b, _size, _flags)	\
893 	alloc_hooks(__kmalloc_node_track_caller_noprof(PASS_BUCKET_PARAMS(_size, _b), _flags, NUMA_NO_NODE, _RET_IP_))
894 
kmalloc_node_noprof(size_t size,gfp_t flags,int node)895 static __always_inline __alloc_size(1) void *kmalloc_node_noprof(size_t size, gfp_t flags, int node)
896 {
897 	if (__builtin_constant_p(size) && size) {
898 		unsigned int index;
899 
900 		if (size > KMALLOC_MAX_CACHE_SIZE)
901 			return __kmalloc_large_node_noprof(size, flags, node);
902 
903 		index = kmalloc_index(size);
904 		return __kmalloc_cache_node_noprof(
905 				kmalloc_caches[kmalloc_type(flags, _RET_IP_)][index],
906 				flags, node, size);
907 	}
908 	return __kmalloc_node_noprof(PASS_BUCKET_PARAMS(size, NULL), flags, node);
909 }
910 #define kmalloc_node(...)			alloc_hooks(kmalloc_node_noprof(__VA_ARGS__))
911 
912 /**
913  * kmalloc_array - allocate memory for an array.
914  * @n: number of elements.
915  * @size: element size.
916  * @flags: the type of memory to allocate (see kmalloc).
917  */
kmalloc_array_noprof(size_t n,size_t size,gfp_t flags)918 static inline __alloc_size(1, 2) void *kmalloc_array_noprof(size_t n, size_t size, gfp_t flags)
919 {
920 	size_t bytes;
921 
922 	if (unlikely(check_mul_overflow(n, size, &bytes)))
923 		return NULL;
924 	if (__builtin_constant_p(n) && __builtin_constant_p(size))
925 		return kmalloc_noprof(bytes, flags);
926 	return kmalloc_noprof(bytes, flags);
927 }
928 #define kmalloc_array(...)			alloc_hooks(kmalloc_array_noprof(__VA_ARGS__))
929 
930 /**
931  * krealloc_array - reallocate memory for an array.
932  * @p: pointer to the memory chunk to reallocate
933  * @new_n: new number of elements to alloc
934  * @new_size: new size of a single member of the array
935  * @flags: the type of memory to allocate (see kmalloc)
936  *
937  * If __GFP_ZERO logic is requested, callers must ensure that, starting with the
938  * initial memory allocation, every subsequent call to this API for the same
939  * memory allocation is flagged with __GFP_ZERO. Otherwise, it is possible that
940  * __GFP_ZERO is not fully honored by this API.
941  *
942  * See krealloc_noprof() for further details.
943  *
944  * In any case, the contents of the object pointed to are preserved up to the
945  * lesser of the new and old sizes.
946  */
krealloc_array_noprof(void * p,size_t new_n,size_t new_size,gfp_t flags)947 static inline __realloc_size(2, 3) void * __must_check krealloc_array_noprof(void *p,
948 								       size_t new_n,
949 								       size_t new_size,
950 								       gfp_t flags)
951 {
952 	size_t bytes;
953 
954 	if (unlikely(check_mul_overflow(new_n, new_size, &bytes)))
955 		return NULL;
956 
957 	return krealloc_noprof(p, bytes, flags);
958 }
959 #define krealloc_array(...)			alloc_hooks(krealloc_array_noprof(__VA_ARGS__))
960 
961 /**
962  * kcalloc - allocate memory for an array. The memory is set to zero.
963  * @n: number of elements.
964  * @size: element size.
965  * @flags: the type of memory to allocate (see kmalloc).
966  */
967 #define kcalloc(n, size, flags)		kmalloc_array(n, size, (flags) | __GFP_ZERO)
968 
969 void *__kmalloc_node_track_caller_noprof(DECL_BUCKET_PARAMS(size, b), gfp_t flags, int node,
970 					 unsigned long caller) __alloc_size(1);
971 #define kmalloc_node_track_caller_noprof(size, flags, node, caller) \
972 	__kmalloc_node_track_caller_noprof(PASS_BUCKET_PARAMS(size, NULL), flags, node, caller)
973 #define kmalloc_node_track_caller(...)		\
974 	alloc_hooks(kmalloc_node_track_caller_noprof(__VA_ARGS__, _RET_IP_))
975 
976 /*
977  * kmalloc_track_caller is a special version of kmalloc that records the
978  * calling function of the routine calling it for slab leak tracking instead
979  * of just the calling function (confusing, eh?).
980  * It's useful when the call to kmalloc comes from a widely-used standard
981  * allocator where we care about the real place the memory allocation
982  * request comes from.
983  */
984 #define kmalloc_track_caller(...)		kmalloc_node_track_caller(__VA_ARGS__, NUMA_NO_NODE)
985 
986 #define kmalloc_track_caller_noprof(...)	\
987 		kmalloc_node_track_caller_noprof(__VA_ARGS__, NUMA_NO_NODE, _RET_IP_)
988 
kmalloc_array_node_noprof(size_t n,size_t size,gfp_t flags,int node)989 static inline __alloc_size(1, 2) void *kmalloc_array_node_noprof(size_t n, size_t size, gfp_t flags,
990 							  int node)
991 {
992 	size_t bytes;
993 
994 	if (unlikely(check_mul_overflow(n, size, &bytes)))
995 		return NULL;
996 	if (__builtin_constant_p(n) && __builtin_constant_p(size))
997 		return kmalloc_node_noprof(bytes, flags, node);
998 	return __kmalloc_node_noprof(PASS_BUCKET_PARAMS(bytes, NULL), flags, node);
999 }
1000 #define kmalloc_array_node(...)			alloc_hooks(kmalloc_array_node_noprof(__VA_ARGS__))
1001 
1002 #define kcalloc_node(_n, _size, _flags, _node)	\
1003 	kmalloc_array_node(_n, _size, (_flags) | __GFP_ZERO, _node)
1004 
1005 /*
1006  * Shortcuts
1007  */
1008 #define kmem_cache_zalloc(_k, _flags)		kmem_cache_alloc(_k, (_flags)|__GFP_ZERO)
1009 
1010 /**
1011  * kzalloc - allocate memory. The memory is set to zero.
1012  * @size: how many bytes of memory are required.
1013  * @flags: the type of memory to allocate (see kmalloc).
1014  */
kzalloc_noprof(size_t size,gfp_t flags)1015 static inline __alloc_size(1) void *kzalloc_noprof(size_t size, gfp_t flags)
1016 {
1017 	return kmalloc_noprof(size, flags | __GFP_ZERO);
1018 }
1019 #define kzalloc(...)				alloc_hooks(kzalloc_noprof(__VA_ARGS__))
1020 #define kzalloc_node(_size, _flags, _node)	kmalloc_node(_size, (_flags)|__GFP_ZERO, _node)
1021 
1022 void *__kvmalloc_node_noprof(DECL_BUCKET_PARAMS(size, b), gfp_t flags, int node) __alloc_size(1);
1023 #define kvmalloc_node_noprof(size, flags, node)	\
1024 	__kvmalloc_node_noprof(PASS_BUCKET_PARAMS(size, NULL), flags, node)
1025 #define kvmalloc_node(...)			alloc_hooks(kvmalloc_node_noprof(__VA_ARGS__))
1026 
1027 #define kvmalloc(_size, _flags)			kvmalloc_node(_size, _flags, NUMA_NO_NODE)
1028 #define kvmalloc_noprof(_size, _flags)		kvmalloc_node_noprof(_size, _flags, NUMA_NO_NODE)
1029 #define kvzalloc(_size, _flags)			kvmalloc(_size, (_flags)|__GFP_ZERO)
1030 
1031 #define kvzalloc_node(_size, _flags, _node)	kvmalloc_node(_size, (_flags)|__GFP_ZERO, _node)
1032 #define kmem_buckets_valloc(_b, _size, _flags)	\
1033 	alloc_hooks(__kvmalloc_node_noprof(PASS_BUCKET_PARAMS(_size, _b), _flags, NUMA_NO_NODE))
1034 
1035 static inline __alloc_size(1, 2) void *
kvmalloc_array_node_noprof(size_t n,size_t size,gfp_t flags,int node)1036 kvmalloc_array_node_noprof(size_t n, size_t size, gfp_t flags, int node)
1037 {
1038 	size_t bytes;
1039 
1040 	if (unlikely(check_mul_overflow(n, size, &bytes)))
1041 		return NULL;
1042 
1043 	return kvmalloc_node_noprof(bytes, flags, node);
1044 }
1045 
1046 #define kvmalloc_array_noprof(...)		kvmalloc_array_node_noprof(__VA_ARGS__, NUMA_NO_NODE)
1047 #define kvcalloc_node_noprof(_n,_s,_f,_node)	kvmalloc_array_node_noprof(_n,_s,(_f)|__GFP_ZERO,_node)
1048 #define kvcalloc_noprof(...)			kvcalloc_node_noprof(__VA_ARGS__, NUMA_NO_NODE)
1049 
1050 #define kvmalloc_array(...)			alloc_hooks(kvmalloc_array_noprof(__VA_ARGS__))
1051 #define kvcalloc_node(...)			alloc_hooks(kvcalloc_node_noprof(__VA_ARGS__))
1052 #define kvcalloc(...)				alloc_hooks(kvcalloc_noprof(__VA_ARGS__))
1053 
1054 void *kvrealloc_noprof(const void *p, size_t size, gfp_t flags)
1055 		__realloc_size(2);
1056 #define kvrealloc(...)				alloc_hooks(kvrealloc_noprof(__VA_ARGS__))
1057 
1058 extern void kvfree(const void *addr);
1059 DEFINE_FREE(kvfree, void *, if (!IS_ERR_OR_NULL(_T)) kvfree(_T))
1060 
1061 extern void kvfree_sensitive(const void *addr, size_t len);
1062 
1063 unsigned int kmem_cache_size(struct kmem_cache *s);
1064 
1065 /**
1066  * kmalloc_size_roundup - Report allocation bucket size for the given size
1067  *
1068  * @size: Number of bytes to round up from.
1069  *
1070  * This returns the number of bytes that would be available in a kmalloc()
1071  * allocation of @size bytes. For example, a 126 byte request would be
1072  * rounded up to the next sized kmalloc bucket, 128 bytes. (This is strictly
1073  * for the general-purpose kmalloc()-based allocations, and is not for the
1074  * pre-sized kmem_cache_alloc()-based allocations.)
1075  *
1076  * Use this to kmalloc() the full bucket size ahead of time instead of using
1077  * ksize() to query the size after an allocation.
1078  */
1079 size_t kmalloc_size_roundup(size_t size);
1080 
1081 void __init kmem_cache_init_late(void);
1082 
1083 #endif	/* _LINUX_SLAB_H */
1084