xref: /linux/include/linux/slab.h (revision fab183d632628381b466a41479489541ac0e29a0)
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/bug.h>
16 #include <linux/cache.h>
17 #include <linux/gfp.h>
18 #include <linux/overflow.h>
19 #include <linux/types.h>
20 #include <linux/rcupdate.h>
21 #include <linux/workqueue.h>
22 #include <linux/percpu-refcount.h>
23 #include <linux/cleanup.h>
24 #include <linux/hash.h>
25 
26 enum _slab_flag_bits {
27 	_SLAB_CONSISTENCY_CHECKS,
28 	_SLAB_RED_ZONE,
29 	_SLAB_POISON,
30 	_SLAB_KMALLOC,
31 	_SLAB_HWCACHE_ALIGN,
32 	_SLAB_CACHE_DMA,
33 	_SLAB_CACHE_DMA32,
34 	_SLAB_STORE_USER,
35 	_SLAB_PANIC,
36 	_SLAB_TYPESAFE_BY_RCU,
37 	_SLAB_TRACE,
38 #ifdef CONFIG_DEBUG_OBJECTS
39 	_SLAB_DEBUG_OBJECTS,
40 #endif
41 	_SLAB_NOLEAKTRACE,
42 	_SLAB_NO_MERGE,
43 #ifdef CONFIG_FAILSLAB
44 	_SLAB_FAILSLAB,
45 #endif
46 #ifdef CONFIG_MEMCG
47 	_SLAB_ACCOUNT,
48 #endif
49 #ifdef CONFIG_KASAN_GENERIC
50 	_SLAB_KASAN,
51 #endif
52 	_SLAB_NO_USER_FLAGS,
53 #ifdef CONFIG_KFENCE
54 	_SLAB_SKIP_KFENCE,
55 #endif
56 #ifndef CONFIG_SLUB_TINY
57 	_SLAB_RECLAIM_ACCOUNT,
58 #endif
59 	_SLAB_OBJECT_POISON,
60 	_SLAB_CMPXCHG_DOUBLE,
61 #ifdef CONFIG_SLAB_OBJ_EXT
62 	_SLAB_NO_OBJ_EXT,
63 #ifdef CONFIG_64BIT
64 	_SLAB_OBJ_EXT_IN_OBJ,
65 #endif
66 #endif
67 	_SLAB_NO_SHEAVES,
68 	_SLAB_FLAGS_LAST_BIT
69 };
70 
71 #define __SLAB_FLAG_BIT(nr)	((slab_flags_t __force)(1U << (nr)))
72 #define __SLAB_FLAG_UNUSED	((slab_flags_t __force)(0U))
73 
74 /*
75  * Flags to pass to kmem_cache_create().
76  * The ones marked DEBUG need CONFIG_SLUB_DEBUG enabled, otherwise are no-op
77  */
78 /* DEBUG: Perform (expensive) checks on alloc/free */
79 #define SLAB_CONSISTENCY_CHECKS	__SLAB_FLAG_BIT(_SLAB_CONSISTENCY_CHECKS)
80 /* DEBUG: Red zone objs in a cache */
81 #define SLAB_RED_ZONE		__SLAB_FLAG_BIT(_SLAB_RED_ZONE)
82 /* DEBUG: Poison objects */
83 #define SLAB_POISON		__SLAB_FLAG_BIT(_SLAB_POISON)
84 /* Indicate a kmalloc slab */
85 #define SLAB_KMALLOC		__SLAB_FLAG_BIT(_SLAB_KMALLOC)
86 /**
87  * define SLAB_HWCACHE_ALIGN - Align objects on cache line boundaries.
88  *
89  * Sufficiently large objects are aligned on cache line boundary. For object
90  * size smaller than a half of cache line size, the alignment is on the half of
91  * cache line size. In general, if object size is smaller than 1/2^n of cache
92  * line size, the alignment is adjusted to 1/2^n.
93  *
94  * If explicit alignment is also requested by the respective
95  * &struct kmem_cache_args field, the greater of both is alignments is applied.
96  */
97 #define SLAB_HWCACHE_ALIGN	__SLAB_FLAG_BIT(_SLAB_HWCACHE_ALIGN)
98 /* Use GFP_DMA memory */
99 #define SLAB_CACHE_DMA		__SLAB_FLAG_BIT(_SLAB_CACHE_DMA)
100 /* Use GFP_DMA32 memory */
101 #define SLAB_CACHE_DMA32	__SLAB_FLAG_BIT(_SLAB_CACHE_DMA32)
102 /* DEBUG: Store the last owner for bug hunting */
103 #define SLAB_STORE_USER		__SLAB_FLAG_BIT(_SLAB_STORE_USER)
104 /* Panic if kmem_cache_create() fails */
105 #define SLAB_PANIC		__SLAB_FLAG_BIT(_SLAB_PANIC)
106 /**
107  * define SLAB_TYPESAFE_BY_RCU - **WARNING** READ THIS!
108  *
109  * This delays freeing the SLAB page by a grace period, it does _NOT_
110  * delay object freeing. This means that if you do kmem_cache_free()
111  * that memory location is free to be reused at any time. Thus it may
112  * be possible to see another object there in the same RCU grace period.
113  *
114  * This feature only ensures the memory location backing the object
115  * stays valid, the trick to using this is relying on an independent
116  * object validation pass. Something like:
117  *
118  * ::
119  *
120  *  begin:
121  *   rcu_read_lock();
122  *   obj = lockless_lookup(key);
123  *   if (obj) {
124  *     if (!try_get_ref(obj)) // might fail for free objects
125  *       rcu_read_unlock();
126  *       goto begin;
127  *
128  *     if (obj->key != key) { // not the object we expected
129  *       put_ref(obj);
130  *       rcu_read_unlock();
131  *       goto begin;
132  *     }
133  *   }
134  *  rcu_read_unlock();
135  *
136  * This is useful if we need to approach a kernel structure obliquely,
137  * from its address obtained without the usual locking. We can lock
138  * the structure to stabilize it and check it's still at the given address,
139  * only if we can be sure that the memory has not been meanwhile reused
140  * for some other kind of object (which our subsystem's lock might corrupt).
141  *
142  * rcu_read_lock before reading the address, then rcu_read_unlock after
143  * taking the spinlock within the structure expected at that address.
144  *
145  * Note that object identity check has to be done *after* acquiring a
146  * reference, therefore user has to ensure proper ordering for loads.
147  * Similarly, when initializing objects allocated with SLAB_TYPESAFE_BY_RCU,
148  * the newly allocated object has to be fully initialized *before* its
149  * refcount gets initialized and proper ordering for stores is required.
150  * refcount_{add|inc}_not_zero_acquire() and refcount_set_release() are
151  * designed with the proper fences required for reference counting objects
152  * allocated with SLAB_TYPESAFE_BY_RCU.
153  *
154  * Note that it is not possible to acquire a lock within a structure
155  * allocated with SLAB_TYPESAFE_BY_RCU without first acquiring a reference
156  * as described above.  The reason is that SLAB_TYPESAFE_BY_RCU pages
157  * are not zeroed before being given to the slab, which means that any
158  * locks must be initialized after each and every kmem_struct_alloc().
159  * Alternatively, make the ctor passed to kmem_cache_create() initialize
160  * the locks at page-allocation time, as is done in __i915_request_ctor(),
161  * sighand_ctor(), and anon_vma_ctor().  Such a ctor permits readers
162  * to safely acquire those ctor-initialized locks under rcu_read_lock()
163  * protection.
164  *
165  * Note that SLAB_TYPESAFE_BY_RCU was originally named SLAB_DESTROY_BY_RCU.
166  */
167 #define SLAB_TYPESAFE_BY_RCU	__SLAB_FLAG_BIT(_SLAB_TYPESAFE_BY_RCU)
168 /* Trace allocations and frees */
169 #define SLAB_TRACE		__SLAB_FLAG_BIT(_SLAB_TRACE)
170 
171 /* Flag to prevent checks on free */
172 #ifdef CONFIG_DEBUG_OBJECTS
173 # define SLAB_DEBUG_OBJECTS	__SLAB_FLAG_BIT(_SLAB_DEBUG_OBJECTS)
174 #else
175 # define SLAB_DEBUG_OBJECTS	__SLAB_FLAG_UNUSED
176 #endif
177 
178 /* Avoid kmemleak tracing */
179 #define SLAB_NOLEAKTRACE	__SLAB_FLAG_BIT(_SLAB_NOLEAKTRACE)
180 
181 /*
182  * Prevent merging with compatible kmem caches. This flag should be used
183  * cautiously. Valid use cases:
184  *
185  * - caches created for self-tests (e.g. kunit)
186  * - general caches created and used by a subsystem, only when a
187  *   (subsystem-specific) debug option is enabled
188  * - performance critical caches, should be very rare and consulted with slab
189  *   maintainers, and not used together with CONFIG_SLUB_TINY
190  */
191 #define SLAB_NO_MERGE		__SLAB_FLAG_BIT(_SLAB_NO_MERGE)
192 
193 /* Fault injection mark */
194 #ifdef CONFIG_FAILSLAB
195 # define SLAB_FAILSLAB		__SLAB_FLAG_BIT(_SLAB_FAILSLAB)
196 #else
197 # define SLAB_FAILSLAB		__SLAB_FLAG_UNUSED
198 #endif
199 /**
200  * define SLAB_ACCOUNT - Account allocations to memcg.
201  *
202  * All object allocations from this cache will be memcg accounted, regardless of
203  * __GFP_ACCOUNT being or not being passed to individual allocations.
204  */
205 #ifdef CONFIG_MEMCG
206 # define SLAB_ACCOUNT		__SLAB_FLAG_BIT(_SLAB_ACCOUNT)
207 #else
208 # define SLAB_ACCOUNT		__SLAB_FLAG_UNUSED
209 #endif
210 
211 #ifdef CONFIG_KASAN_GENERIC
212 #define SLAB_KASAN		__SLAB_FLAG_BIT(_SLAB_KASAN)
213 #else
214 #define SLAB_KASAN		__SLAB_FLAG_UNUSED
215 #endif
216 
217 /*
218  * Ignore user specified debugging flags.
219  * Intended for caches created for self-tests so they have only flags
220  * specified in the code and other flags are ignored.
221  */
222 #define SLAB_NO_USER_FLAGS	__SLAB_FLAG_BIT(_SLAB_NO_USER_FLAGS)
223 
224 #ifdef CONFIG_KFENCE
225 #define SLAB_SKIP_KFENCE	__SLAB_FLAG_BIT(_SLAB_SKIP_KFENCE)
226 #else
227 #define SLAB_SKIP_KFENCE	__SLAB_FLAG_UNUSED
228 #endif
229 
230 /* The following flags affect the page allocator grouping pages by mobility */
231 /**
232  * define SLAB_RECLAIM_ACCOUNT - Objects are reclaimable.
233  *
234  * Use this flag for caches that have an associated shrinker. As a result, slab
235  * pages are allocated with __GFP_RECLAIMABLE, which affects grouping pages by
236  * mobility, and are accounted in SReclaimable counter in /proc/meminfo
237  */
238 #ifndef CONFIG_SLUB_TINY
239 #define SLAB_RECLAIM_ACCOUNT	__SLAB_FLAG_BIT(_SLAB_RECLAIM_ACCOUNT)
240 #else
241 #define SLAB_RECLAIM_ACCOUNT	__SLAB_FLAG_UNUSED
242 #endif
243 #define SLAB_TEMPORARY		SLAB_RECLAIM_ACCOUNT	/* Objects are short-lived */
244 
245 /* Slab caches without obj_exts array */
246 #ifdef CONFIG_SLAB_OBJ_EXT
247 #define SLAB_NO_OBJ_EXT		__SLAB_FLAG_BIT(_SLAB_NO_OBJ_EXT)
248 #else
249 #define SLAB_NO_OBJ_EXT		__SLAB_FLAG_UNUSED
250 #endif
251 
252 #define SLAB_NO_SHEAVES		__SLAB_FLAG_BIT(_SLAB_NO_SHEAVES)
253 
254 #if defined(CONFIG_SLAB_OBJ_EXT) && defined(CONFIG_64BIT)
255 #define SLAB_OBJ_EXT_IN_OBJ	__SLAB_FLAG_BIT(_SLAB_OBJ_EXT_IN_OBJ)
256 #else
257 #define SLAB_OBJ_EXT_IN_OBJ	__SLAB_FLAG_UNUSED
258 #endif
259 
260 /*
261  * ZERO_SIZE_PTR will be returned for zero sized kmalloc requests.
262  *
263  * Dereferencing ZERO_SIZE_PTR will lead to a distinct access fault.
264  *
265  * ZERO_SIZE_PTR can be passed to kfree though in the same way that NULL can.
266  * Both make kfree a no-op.
267  */
268 #define ZERO_SIZE_PTR ((void *)16)
269 
270 #define ZERO_OR_NULL_PTR(x) ((unsigned long)(x) <= \
271 				(unsigned long)ZERO_SIZE_PTR)
272 
273 #include <linux/kasan.h>
274 
275 struct list_lru;
276 struct mem_cgroup;
277 /*
278  * struct kmem_cache related prototypes
279  */
280 bool slab_is_available(void);
281 
282 /**
283  * struct kmem_cache_args - Less common arguments for kmem_cache_create()
284  *
285  * Any uninitialized fields of the structure are interpreted as unused. The
286  * exception is @freeptr_offset where %0 is a valid value, so
287  * @use_freeptr_offset must be also set to %true in order to interpret the field
288  * as used. For @useroffset %0 is also valid, but only with non-%0
289  * @usersize.
290  *
291  * When %NULL args is passed to kmem_cache_create(), it is equivalent to all
292  * fields unused.
293  */
294 struct kmem_cache_args {
295 	/**
296 	 * @align: The required alignment for the objects.
297 	 *
298 	 * %0 means no specific alignment is requested.
299 	 */
300 	unsigned int align;
301 	/**
302 	 * @useroffset: Usercopy region offset.
303 	 *
304 	 * %0 is a valid offset, when @usersize is non-%0
305 	 */
306 	unsigned int useroffset;
307 	/**
308 	 * @usersize: Usercopy region size.
309 	 *
310 	 * %0 means no usercopy region is specified.
311 	 */
312 	unsigned int usersize;
313 	/**
314 	 * @freeptr_offset: Custom offset for the free pointer
315 	 * in caches with &SLAB_TYPESAFE_BY_RCU or @ctor
316 	 *
317 	 * By default, &SLAB_TYPESAFE_BY_RCU and @ctor caches place the free
318 	 * pointer outside of the object. This might cause the object to grow
319 	 * in size. Cache creators that have a reason to avoid this can specify
320 	 * a custom free pointer offset in their data structure where the free
321 	 * pointer will be placed.
322 	 *
323 	 * For caches with &SLAB_TYPESAFE_BY_RCU, the caller must ensure that
324 	 * the free pointer does not overlay fields required to guard against
325 	 * object recycling (See &SLAB_TYPESAFE_BY_RCU for details).
326 	 *
327 	 * For caches with @ctor, the caller must ensure that the free pointer
328 	 * does not overlay fields initialized by the constructor.
329 	 *
330 	 * Currently, only caches with &SLAB_TYPESAFE_BY_RCU or @ctor
331 	 * may specify @freeptr_offset.
332 	 *
333 	 * Using %0 as a value for @freeptr_offset is valid. If @freeptr_offset
334 	 * is specified, @use_freeptr_offset must be set %true.
335 	 */
336 	unsigned int freeptr_offset;
337 	/**
338 	 * @use_freeptr_offset: Whether a @freeptr_offset is used.
339 	 */
340 	bool use_freeptr_offset;
341 	/**
342 	 * @ctor: A constructor for the objects.
343 	 *
344 	 * The constructor is invoked for each object in a newly allocated slab
345 	 * page. It is the cache user's responsibility to free object in the
346 	 * same state as after calling the constructor, or deal appropriately
347 	 * with any differences between a freshly constructed and a reallocated
348 	 * object.
349 	 *
350 	 * %NULL means no constructor.
351 	 */
352 	void (*ctor)(void *);
353 	/**
354 	 * @sheaf_capacity: Enable sheaves of given capacity for the cache.
355 	 *
356 	 * With a non-zero value, allocations from the cache go through caching
357 	 * arrays called sheaves. Each cpu has a main sheaf that's always
358 	 * present, and a spare sheaf that may be not present. When both become
359 	 * empty, there's an attempt to replace an empty sheaf with a full sheaf
360 	 * from the per-node barn.
361 	 *
362 	 * When no full sheaf is available, and gfp flags allow blocking, a
363 	 * sheaf is allocated and filled from slab(s) using bulk allocation.
364 	 * Otherwise the allocation falls back to the normal operation
365 	 * allocating a single object from a slab.
366 	 *
367 	 * Analogically when freeing and both percpu sheaves are full, the barn
368 	 * may replace it with an empty sheaf, unless it's over capacity. In
369 	 * that case a sheaf is bulk freed to slab pages.
370 	 *
371 	 * The sheaves do not enforce NUMA placement of objects, so allocations
372 	 * via kmem_cache_alloc_node() with a node specified other than
373 	 * NUMA_NO_NODE will bypass them.
374 	 *
375 	 * Bulk allocation and free operations also try to use the cpu sheaves
376 	 * and barn, but fallback to using slab pages directly.
377 	 *
378 	 * When slub_debug is enabled for the cache, the sheaf_capacity argument
379 	 * is ignored.
380 	 *
381 	 * %0 means no sheaves will be created.
382 	 */
383 	unsigned int sheaf_capacity;
384 };
385 
386 struct kmem_cache *__kmem_cache_create_args(const char *name,
387 					    unsigned int object_size,
388 					    struct kmem_cache_args *args,
389 					    slab_flags_t flags);
390 static inline struct kmem_cache *
__kmem_cache_create(const char * name,unsigned int size,unsigned int align,slab_flags_t flags,void (* ctor)(void *))391 __kmem_cache_create(const char *name, unsigned int size, unsigned int align,
392 		    slab_flags_t flags, void (*ctor)(void *))
393 {
394 	struct kmem_cache_args kmem_args = {
395 		.align	= align,
396 		.ctor	= ctor,
397 	};
398 
399 	return __kmem_cache_create_args(name, size, &kmem_args, flags);
400 }
401 
402 /**
403  * kmem_cache_create_usercopy - Create a kmem cache with a region suitable
404  * for copying to userspace.
405  * @name: A string which is used in /proc/slabinfo to identify this cache.
406  * @size: The size of objects to be created in this cache.
407  * @align: The required alignment for the objects.
408  * @flags: SLAB flags
409  * @useroffset: Usercopy region offset
410  * @usersize: Usercopy region size
411  * @ctor: A constructor for the objects, or %NULL.
412  *
413  * This is a legacy wrapper, new code should use either KMEM_CACHE_USERCOPY()
414  * if whitelisting a single field is sufficient, or kmem_cache_create() with
415  * the necessary parameters passed via the args parameter (see
416  * &struct kmem_cache_args)
417  *
418  * Return: a pointer to the cache on success, NULL on failure.
419  */
420 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 *))421 kmem_cache_create_usercopy(const char *name, unsigned int size,
422 			   unsigned int align, slab_flags_t flags,
423 			   unsigned int useroffset, unsigned int usersize,
424 			   void (*ctor)(void *))
425 {
426 	struct kmem_cache_args kmem_args = {
427 		.align		= align,
428 		.ctor		= ctor,
429 		.useroffset	= useroffset,
430 		.usersize	= usersize,
431 	};
432 
433 	return __kmem_cache_create_args(name, size, &kmem_args, flags);
434 }
435 
436 /* If NULL is passed for @args, use this variant with default arguments. */
437 static inline struct kmem_cache *
__kmem_cache_default_args(const char * name,unsigned int size,struct kmem_cache_args * args,slab_flags_t flags)438 __kmem_cache_default_args(const char *name, unsigned int size,
439 			  struct kmem_cache_args *args,
440 			  slab_flags_t flags)
441 {
442 	struct kmem_cache_args kmem_default_args = {};
443 
444 	/* Make sure we don't get passed garbage. */
445 	if (WARN_ON_ONCE(args))
446 		return ERR_PTR(-EINVAL);
447 
448 	return __kmem_cache_create_args(name, size, &kmem_default_args, flags);
449 }
450 
451 /**
452  * kmem_cache_create - Create a kmem cache.
453  * @__name: A string which is used in /proc/slabinfo to identify this cache.
454  * @__object_size: The size of objects to be created in this cache.
455  * @__args: Optional arguments, see &struct kmem_cache_args. Passing %NULL
456  *	    means defaults will be used for all the arguments.
457  *
458  * This is currently implemented as a macro using ``_Generic()`` to call
459  * either the new variant of the function, or a legacy one.
460  *
461  * The new variant has 4 parameters:
462  * ``kmem_cache_create(name, object_size, args, flags)``
463  *
464  * See __kmem_cache_create_args() which implements this.
465  *
466  * The legacy variant has 5 parameters:
467  * ``kmem_cache_create(name, object_size, align, flags, ctor)``
468  *
469  * The align and ctor parameters map to the respective fields of
470  * &struct kmem_cache_args
471  *
472  * Context: Cannot be called within a interrupt, but can be interrupted.
473  *
474  * Return: a pointer to the cache on success, NULL on failure.
475  */
476 #define kmem_cache_create(__name, __object_size, __args, ...)           \
477 	_Generic((__args),                                              \
478 		struct kmem_cache_args *: __kmem_cache_create_args,	\
479 		void *: __kmem_cache_default_args,			\
480 		default: __kmem_cache_create)(__name, __object_size, __args, __VA_ARGS__)
481 
482 void kmem_cache_destroy(struct kmem_cache *s);
483 int kmem_cache_shrink(struct kmem_cache *s);
484 
485 /*
486  * Please use this macro to create slab caches. Simply specify the
487  * name of the structure and maybe some flags that are listed above.
488  *
489  * The alignment of the struct determines object alignment. If you
490  * f.e. add ____cacheline_aligned_in_smp to the struct declaration
491  * then the objects will be properly aligned in SMP configurations.
492  */
493 #define KMEM_CACHE(__struct, __flags)                                   \
494 	__kmem_cache_create_args(#__struct, sizeof(struct __struct),    \
495 			&(struct kmem_cache_args) {			\
496 				.align	= __alignof__(struct __struct), \
497 			}, (__flags))
498 
499 /*
500  * To whitelist a single field for copying to/from usercopy, use this
501  * macro instead for KMEM_CACHE() above.
502  */
503 #define KMEM_CACHE_USERCOPY(__struct, __flags, __field)						\
504 	__kmem_cache_create_args(#__struct, sizeof(struct __struct),				\
505 			&(struct kmem_cache_args) {						\
506 				.align		= __alignof__(struct __struct),			\
507 				.useroffset	= offsetof(struct __struct, __field),		\
508 				.usersize	= sizeof_field(struct __struct, __field),	\
509 			}, (__flags))
510 
511 #ifdef CONFIG_KMALLOC_PARTITION_CACHES
512 typedef struct { unsigned long v; } kmalloc_token_t;
513 #ifdef CONFIG_KMALLOC_PARTITION_RANDOM
514 extern unsigned long random_kmalloc_seed;
515 #define __kmalloc_token(...) ((kmalloc_token_t){ .v = _CODE_LOCATION_ })
516 #elif defined(CONFIG_KMALLOC_PARTITION_TYPED)
517 #ifdef __CHECKER__
518 #define __kmalloc_token(...) ((kmalloc_token_t){ .v = 0 })
519 #else /* !__CHECKER__ */
520 #define __kmalloc_token(...) ((kmalloc_token_t){ .v = __builtin_infer_alloc_token(__VA_ARGS__) })
521 #endif /* __CHECKER__ */
522 #endif /* CONFIG_KMALLOC_PARTITION_TYPED */
523 #define DECL_TOKEN_PARAM(_token)	, kmalloc_token_t (_token)
524 #define _PASS_TOKEN_PARAM(_token)	, (_token)
525 #define PASS_TOKEN_PARAM(_token)	(_token)
526 #define DECL_TOKEN_PARAMS(_size, _token) size_t (_size), kmalloc_token_t (_token)
527 #define PASS_TOKEN_PARAMS(_size, _token) (_size), (_token)
528 #else /* !CONFIG_KMALLOC_PARTITION_CACHES */
529 typedef struct {} kmalloc_token_t;
530 #define __kmalloc_token(...) ((kmalloc_token_t){}) /* no-op */
531 #define DECL_TOKEN_PARAM(_token)
532 #define _PASS_TOKEN_PARAM(_token)
533 #define PASS_TOKEN_PARAM(_token)	((kmalloc_token_t){})
534 #define DECL_TOKEN_PARAMS(_size, _token) size_t (_size)
535 #define PASS_TOKEN_PARAMS(_size, _token) (_size)
536 #endif /* CONFIG_KMALLOC_PARTITION_CACHES */
537 
538 /*
539  * Common kmalloc functions provided by all allocators
540  */
541 void * __must_check krealloc_node_align_noprof(const void *objp,
542 					       DECL_TOKEN_PARAMS(new_size, token),
543 					       unsigned long align,
544 					       gfp_t flags, int nid) __realloc_size(2);
545 #define krealloc_noprof(_o, _s, _f)	krealloc_node_align_noprof(_o, PASS_TOKEN_PARAMS(_s, __kmalloc_token(_s)), 1, _f, NUMA_NO_NODE)
546 #if 0 /* kernel-doc */
547 /**
548  * krealloc_node_align - reallocate memory. The contents will remain unchanged.
549  * @p: object to reallocate memory for.
550  * @new_size: how many bytes of memory are required.
551  * @align: desired alignment.
552  * @flags: the type of memory to allocate.
553  * @nid: NUMA node or NUMA_NO_NODE
554  *
555  * If @p is %NULL, krealloc() behaves exactly like kmalloc().  If @new_size
556  * is 0 and @p is not a %NULL pointer, the object pointed to is freed.
557  *
558  * Only alignments up to those guaranteed by kmalloc() will be honored. Please see
559  * Documentation/core-api/memory-allocation.rst for more details.
560  *
561  * If __GFP_ZERO logic is requested, callers must ensure that, starting with the
562  * initial memory allocation, every subsequent call to this API for the same
563  * memory allocation is flagged with __GFP_ZERO. Otherwise, it is possible that
564  * __GFP_ZERO is not fully honored by this API.
565  *
566  * When slub_debug_orig_size() is off, krealloc() only knows about the bucket
567  * size of an allocation (but not the exact size it was allocated with) and
568  * hence implements the following semantics for shrinking and growing buffers
569  * with __GFP_ZERO::
570  *
571  *           new             bucket
572  *   0       size             size
573  *   |--------|----------------|
574  *   |  keep  |      zero      |
575  *
576  * Otherwise, the original allocation size 'orig_size' could be used to
577  * precisely clear the requested size, and the new size will also be stored
578  * as the new 'orig_size'.
579  *
580  * In any case, the contents of the object pointed to are preserved up to the
581  * lesser of the new and old sizes.
582  *
583  * Return: pointer to the allocated memory or %NULL in case of error
584  */
585 void *krealloc_node_align(const void *p, size_t new_size, unsigned long align, gfp_t flags, int nid);
586 #endif
587 #define krealloc_node_align(p, new_size, align, flags, nid) \
588 	alloc_hooks(krealloc_node_align_noprof(p, PASS_TOKEN_PARAMS(new_size, __kmalloc_token(new_size)), align, flags, nid))
589 #define krealloc_node(_o, _s, _f, _n)	krealloc_node_align(_o, _s, 1, _f, _n)
590 #define krealloc(...)			krealloc_node(__VA_ARGS__, NUMA_NO_NODE)
591 
592 void kfree(const void *objp);
593 void kfree_nolock(const void *objp);
594 void kfree_sensitive(const void *objp);
595 
596 DEFINE_FREE(kfree, void *, if (!IS_ERR_OR_NULL(_T)) kfree(_T))
597 DEFINE_FREE(kfree_sensitive, void *, if (_T) kfree_sensitive(_T))
598 
599 size_t ksize(const void *objp);
600 
601 #ifdef CONFIG_PRINTK
602 bool kmem_dump_obj(void *object);
603 #else
kmem_dump_obj(void * object)604 static inline bool kmem_dump_obj(void *object) { return false; }
605 #endif
606 
607 /*
608  * Some archs want to perform DMA into kmalloc caches and need a guaranteed
609  * alignment larger than the alignment of a 64-bit integer.
610  * Setting ARCH_DMA_MINALIGN in arch headers allows that.
611  */
612 #ifdef ARCH_HAS_DMA_MINALIGN
613 #if ARCH_DMA_MINALIGN > 8 && !defined(ARCH_KMALLOC_MINALIGN)
614 #define ARCH_KMALLOC_MINALIGN ARCH_DMA_MINALIGN
615 #endif
616 #endif
617 
618 #ifndef ARCH_KMALLOC_MINALIGN
619 #define ARCH_KMALLOC_MINALIGN __alignof__(unsigned long long)
620 #elif ARCH_KMALLOC_MINALIGN > 8
621 #define KMALLOC_MIN_SIZE ARCH_KMALLOC_MINALIGN
622 #define KMALLOC_SHIFT_LOW ilog2(KMALLOC_MIN_SIZE)
623 #endif
624 
625 /*
626  * Setting ARCH_SLAB_MINALIGN in arch headers allows a different alignment.
627  * Intended for arches that get misalignment faults even for 64 bit integer
628  * aligned buffers.
629  */
630 #ifndef ARCH_SLAB_MINALIGN
631 #define ARCH_SLAB_MINALIGN __alignof__(unsigned long long)
632 #endif
633 
634 /*
635  * Arches can define this function if they want to decide the minimum slab
636  * alignment at runtime. The value returned by the function must be a power
637  * of two and >= ARCH_SLAB_MINALIGN.
638  */
639 #ifndef arch_slab_minalign
arch_slab_minalign(void)640 static inline unsigned int arch_slab_minalign(void)
641 {
642 	return ARCH_SLAB_MINALIGN;
643 }
644 #endif
645 
646 /*
647  * kmem_cache_alloc and friends return pointers aligned to ARCH_SLAB_MINALIGN.
648  * kmalloc and friends return pointers aligned to both ARCH_KMALLOC_MINALIGN
649  * and ARCH_SLAB_MINALIGN, but here we only assume the former alignment.
650  */
651 #define __assume_kmalloc_alignment __assume_aligned(ARCH_KMALLOC_MINALIGN)
652 #define __assume_slab_alignment __assume_aligned(ARCH_SLAB_MINALIGN)
653 #define __assume_page_alignment __assume_aligned(PAGE_SIZE)
654 
655 /*
656  * Kmalloc array related definitions
657  */
658 
659 /*
660  * SLUB directly allocates requests fitting in to an order-1 page
661  * (PAGE_SIZE*2).  Larger requests are passed to the page allocator.
662  */
663 #define KMALLOC_SHIFT_HIGH	(PAGE_SHIFT + 1)
664 #define KMALLOC_SHIFT_MAX	(MAX_PAGE_ORDER + PAGE_SHIFT)
665 #ifndef KMALLOC_SHIFT_LOW
666 #define KMALLOC_SHIFT_LOW	3
667 #endif
668 
669 /* Maximum allocatable size */
670 #define KMALLOC_MAX_SIZE	(1UL << KMALLOC_SHIFT_MAX)
671 /* Maximum size for which we actually use a slab cache */
672 #define KMALLOC_MAX_CACHE_SIZE	(1UL << KMALLOC_SHIFT_HIGH)
673 /* Maximum order allocatable via the slab allocator */
674 #define KMALLOC_MAX_ORDER	(KMALLOC_SHIFT_MAX - PAGE_SHIFT)
675 
676 /*
677  * Kmalloc subsystem.
678  */
679 #ifndef KMALLOC_MIN_SIZE
680 #define KMALLOC_MIN_SIZE (1 << KMALLOC_SHIFT_LOW)
681 #endif
682 
683 /*
684  * This restriction comes from byte sized index implementation.
685  * Page size is normally 2^12 bytes and, in this case, if we want to use
686  * byte sized index which can represent 2^8 entries, the size of the object
687  * should be equal or greater to 2^12 / 2^8 = 2^4 = 16.
688  * If minimum size of kmalloc is less than 16, we use it as minimum object
689  * size and give up to use byte sized index.
690  */
691 #define SLAB_OBJ_MIN_SIZE      (KMALLOC_MIN_SIZE < 16 ? \
692                                (KMALLOC_MIN_SIZE) : 16)
693 
694 #ifdef CONFIG_KMALLOC_PARTITION_CACHES
695 #define KMALLOC_PARTITION_CACHES_NR	15 // # of cache copies
696 #else
697 #define KMALLOC_PARTITION_CACHES_NR	0
698 #endif
699 
700 /*
701  * Whenever changing this, take care of that kmalloc_type() and
702  * create_kmalloc_caches() still work as intended.
703  *
704  * KMALLOC_NORMAL can contain only unaccounted objects whereas KMALLOC_CGROUP
705  * is for accounted but unreclaimable and non-dma objects. All the other
706  * kmem caches can have both accounted and unaccounted objects.
707  */
708 enum kmalloc_cache_type {
709 	KMALLOC_NORMAL = 0,
710 #ifndef CONFIG_ZONE_DMA
711 	KMALLOC_DMA = KMALLOC_NORMAL,
712 #endif
713 #ifndef CONFIG_MEMCG
714 	KMALLOC_CGROUP = KMALLOC_NORMAL,
715 #endif
716 #ifndef CONFIG_SLAB_OBJ_EXT
717 	KMALLOC_NO_OBJ_EXT = KMALLOC_NORMAL,
718 #endif
719 	KMALLOC_PARTITION_START = KMALLOC_NORMAL,
720 	KMALLOC_PARTITION_END = KMALLOC_PARTITION_START + KMALLOC_PARTITION_CACHES_NR,
721 #ifdef CONFIG_SLUB_TINY
722 	KMALLOC_RECLAIM = KMALLOC_NORMAL,
723 #else
724 	KMALLOC_RECLAIM,
725 #endif
726 #ifdef CONFIG_ZONE_DMA
727 	KMALLOC_DMA,
728 #endif
729 #ifdef CONFIG_MEMCG
730 	KMALLOC_CGROUP,
731 #endif
732 #ifdef CONFIG_SLAB_OBJ_EXT
733 	KMALLOC_NO_OBJ_EXT,
734 #endif
735 	NR_KMALLOC_TYPES
736 };
737 
738 typedef struct kmem_cache * kmem_buckets[KMALLOC_SHIFT_HIGH + 1];
739 
740 extern kmem_buckets kmalloc_caches[NR_KMALLOC_TYPES];
741 
742 /*
743  * Define gfp bits that should not be set for KMALLOC_NORMAL.
744  */
745 #define KMALLOC_NOT_NORMAL_BITS					\
746 	(__GFP_RECLAIMABLE |					\
747 	(IS_ENABLED(CONFIG_ZONE_DMA)   ? __GFP_DMA : 0) |	\
748 	(IS_ENABLED(CONFIG_MEMCG) ? __GFP_ACCOUNT : 0))
749 
kmalloc_type(gfp_t flags,kmalloc_token_t token)750 static __always_inline enum kmalloc_cache_type kmalloc_type(gfp_t flags, kmalloc_token_t token)
751 {
752 	/*
753 	 * The most common case is KMALLOC_NORMAL, so test for it
754 	 * with a single branch for all the relevant flags.
755 	 */
756 	if (likely((flags & KMALLOC_NOT_NORMAL_BITS) == 0))
757 #ifdef CONFIG_KMALLOC_PARTITION_RANDOM
758 		/* KMALLOC_PARTITION_CACHES_NR (=15) copies + the KMALLOC_NORMAL */
759 		return KMALLOC_PARTITION_START + hash_64(token.v ^ random_kmalloc_seed,
760 							 ilog2(KMALLOC_PARTITION_CACHES_NR + 1));
761 #elif defined(CONFIG_KMALLOC_PARTITION_TYPED)
762 		return KMALLOC_PARTITION_START + token.v;
763 #else
764 		return KMALLOC_NORMAL;
765 #endif
766 
767 	/*
768 	 * At least one of the flags has to be set. Their priorities in
769 	 * decreasing order are:
770 	 *  1) __GFP_DMA
771 	 *  2) __GFP_RECLAIMABLE
772 	 *  3) __GFP_ACCOUNT
773 	 */
774 	if (IS_ENABLED(CONFIG_ZONE_DMA) && (flags & __GFP_DMA))
775 		return KMALLOC_DMA;
776 	if (!IS_ENABLED(CONFIG_MEMCG) || (flags & __GFP_RECLAIMABLE))
777 		return KMALLOC_RECLAIM;
778 	else
779 		return KMALLOC_CGROUP;
780 }
781 
782 /*
783  * Figure out which kmalloc slab an allocation of a certain size
784  * belongs to.
785  * 0 = zero alloc
786  * 1 =  65 .. 96 bytes
787  * 2 = 129 .. 192 bytes
788  * n = 2^(n-1)+1 .. 2^n
789  *
790  * Note: __kmalloc_index() is compile-time optimized, and not runtime optimized;
791  * typical usage is via kmalloc_index() and therefore evaluated at compile-time.
792  * Callers where !size_is_constant should only be test modules, where runtime
793  * overheads of __kmalloc_index() can be tolerated.  Also see kmalloc_slab().
794  */
__kmalloc_index(size_t size,bool size_is_constant)795 static __always_inline unsigned int __kmalloc_index(size_t size,
796 						    bool size_is_constant)
797 {
798 	if (!size)
799 		return 0;
800 
801 	if (size <= KMALLOC_MIN_SIZE)
802 		return KMALLOC_SHIFT_LOW;
803 
804 	if (KMALLOC_MIN_SIZE <= 32 && size > 64 && size <= 96)
805 		return 1;
806 	if (KMALLOC_MIN_SIZE <= 64 && size > 128 && size <= 192)
807 		return 2;
808 	if (size <=          8) return 3;
809 	if (size <=         16) return 4;
810 	if (size <=         32) return 5;
811 	if (size <=         64) return 6;
812 	if (size <=        128) return 7;
813 	if (size <=        256) return 8;
814 	if (size <=        512) return 9;
815 	if (size <=       1024) return 10;
816 	if (size <=   2 * 1024) return 11;
817 	if (size <=   4 * 1024) return 12;
818 	if (size <=   8 * 1024) return 13;
819 	if (size <=  16 * 1024) return 14;
820 	if (size <=  32 * 1024) return 15;
821 	if (size <=  64 * 1024) return 16;
822 	if (size <= 128 * 1024) return 17;
823 	if (size <= 256 * 1024) return 18;
824 	if (size <= 512 * 1024) return 19;
825 	if (size <= 1024 * 1024) return 20;
826 	if (size <=  2 * 1024 * 1024) return 21;
827 
828 	if (!IS_ENABLED(CONFIG_PROFILE_ALL_BRANCHES) && size_is_constant)
829 		BUILD_BUG_ON_MSG(1, "unexpected size in kmalloc_index()");
830 	else
831 		BUG();
832 
833 	/* Will never be reached. Needed because the compiler may complain */
834 	return -1;
835 }
836 static_assert(PAGE_SHIFT <= 20);
837 #define kmalloc_index(s) __kmalloc_index(s, true)
838 
839 #include <linux/alloc_tag.h>
840 
841 /**
842  * kmem_cache_alloc - Allocate an object
843  * @cachep: The cache to allocate from.
844  * @flags: See kmalloc().
845  *
846  * Allocate an object from this cache.
847  * See kmem_cache_zalloc() for a shortcut of adding __GFP_ZERO to flags.
848  *
849  * Return: pointer to the new object or %NULL in case of error
850  */
851 void *kmem_cache_alloc_noprof(struct kmem_cache *cachep,
852 			      gfp_t flags) __assume_slab_alignment __malloc;
853 #define kmem_cache_alloc(...)			alloc_hooks(kmem_cache_alloc_noprof(__VA_ARGS__))
854 
855 void *kmem_cache_alloc_lru_noprof(struct kmem_cache *s, struct list_lru *lru,
856 			    gfp_t gfpflags) __assume_slab_alignment __malloc;
857 #define kmem_cache_alloc_lru(...)	alloc_hooks(kmem_cache_alloc_lru_noprof(__VA_ARGS__))
858 
859 /**
860  * kmem_cache_charge - memcg charge an already allocated slab memory
861  * @objp: address of the slab object to memcg charge
862  * @gfpflags: describe the allocation context
863  *
864  * kmem_cache_charge allows charging a slab object to the current memcg,
865  * primarily in cases where charging at allocation time might not be possible
866  * because the target memcg is not known (i.e. softirq context)
867  *
868  * The objp should be pointer returned by the slab allocator functions like
869  * kmalloc (with __GFP_ACCOUNT in flags) or kmem_cache_alloc. The memcg charge
870  * behavior can be controlled through gfpflags parameter, which affects how the
871  * necessary internal metadata can be allocated. Including __GFP_NOFAIL denotes
872  * that overcharging is requested instead of failure, but is not applied for the
873  * internal metadata allocation.
874  *
875  * There are several cases where it will return true even if the charging was
876  * not done:
877  * More specifically:
878  *
879  * 1. For !CONFIG_MEMCG or cgroup_disable=memory systems.
880  * 2. Already charged slab objects.
881  * 3. For slab objects from KMALLOC_NORMAL caches - allocated by kmalloc()
882  *    without __GFP_ACCOUNT
883  * 4. Allocating internal metadata has failed
884  *
885  * Return: true if charge was successful otherwise false.
886  */
887 bool kmem_cache_charge(void *objp, gfp_t gfpflags);
888 void kmem_cache_free(struct kmem_cache *s, void *objp);
889 
890 kmem_buckets *kmem_buckets_create(const char *name, slab_flags_t flags,
891 				  unsigned int useroffset, unsigned int usersize,
892 				  void (*ctor)(void *));
893 
894 /*
895  * Bulk allocation and freeing operations. These are accelerated in an
896  * allocator specific way to avoid taking locks repeatedly or building
897  * metadata structures unnecessarily.
898  *
899  * Note that interrupts must be enabled when calling these functions.
900  */
901 void kmem_cache_free_bulk(struct kmem_cache *s, size_t size, void **p);
902 
903 bool kmem_cache_alloc_bulk_noprof(struct kmem_cache *s, gfp_t flags,
904 		size_t size, void **p);
905 #define kmem_cache_alloc_bulk(...) \
906 	alloc_hooks(kmem_cache_alloc_bulk_noprof(__VA_ARGS__))
907 
kfree_bulk(size_t size,void ** p)908 static __always_inline void kfree_bulk(size_t size, void **p)
909 {
910 	kmem_cache_free_bulk(NULL, size, p);
911 }
912 
913 void *kmem_cache_alloc_node_noprof(struct kmem_cache *s, gfp_t flags,
914 				   int node) __assume_slab_alignment __malloc;
915 #define kmem_cache_alloc_node(...)	alloc_hooks(kmem_cache_alloc_node_noprof(__VA_ARGS__))
916 
917 struct slab_sheaf *
918 kmem_cache_prefill_sheaf(struct kmem_cache *s, gfp_t gfp, unsigned int size);
919 
920 int kmem_cache_refill_sheaf(struct kmem_cache *s, gfp_t gfp,
921 		struct slab_sheaf **sheafp, unsigned int size);
922 
923 void kmem_cache_return_sheaf(struct kmem_cache *s, gfp_t gfp,
924 				       struct slab_sheaf *sheaf);
925 
926 void *kmem_cache_alloc_from_sheaf_noprof(struct kmem_cache *cachep, gfp_t gfp,
927 			struct slab_sheaf *sheaf) __assume_slab_alignment __malloc;
928 #define kmem_cache_alloc_from_sheaf(...)	\
929 			alloc_hooks(kmem_cache_alloc_from_sheaf_noprof(__VA_ARGS__))
930 
931 unsigned int kmem_cache_sheaf_size(struct slab_sheaf *sheaf);
932 
933 /*
934  * These macros allow declaring a kmem_buckets * parameter alongside size, which
935  * can be compiled out with CONFIG_SLAB_BUCKETS=n so that a large number of call
936  * sites don't have to pass NULL.
937  */
938 #ifdef CONFIG_SLAB_BUCKETS
939 #define DECL_BUCKET_PARAMS(_size, _b)	size_t (_size), kmem_buckets *(_b)
940 #define PASS_BUCKET_PARAMS(_size, _b)	(_size), (_b)
941 #define PASS_BUCKET_PARAM(_b)		(_b)
942 #else
943 #define DECL_BUCKET_PARAMS(_size, _b)	size_t (_size)
944 #define PASS_BUCKET_PARAMS(_size, _b)	(_size)
945 #define PASS_BUCKET_PARAM(_b)		NULL
946 #endif
947 
948 #define DECL_KMALLOC_PARAMS(_size, _b, _token) DECL_BUCKET_PARAMS(_size, _b) \
949 					       DECL_TOKEN_PARAM(_token)
950 
951 #define PASS_KMALLOC_PARAMS(_size, _b, _token) PASS_BUCKET_PARAMS(_size, _b) \
952 					       _PASS_TOKEN_PARAM(_token)
953 
954 /*
955  * The following functions are not to be used directly and are intended only
956  * for internal use from kmalloc() and kmalloc_node()
957  * with the exception of kunit tests
958  */
959 
960 void *__kmalloc_noprof(DECL_TOKEN_PARAMS(size, token), gfp_t flags)
961 				__assume_kmalloc_alignment __alloc_size(1);
962 
963 void *__kmalloc_node_noprof(DECL_KMALLOC_PARAMS(size, b, token), gfp_t flags, int node)
964 				__assume_kmalloc_alignment __alloc_size(1);
965 
966 void *__kmalloc_cache_noprof(struct kmem_cache *s, gfp_t flags, size_t size)
967 				__assume_kmalloc_alignment __alloc_size(3);
968 
969 void *__kmalloc_cache_node_noprof(struct kmem_cache *s, gfp_t gfpflags,
970 				  int node, size_t size)
971 				__assume_kmalloc_alignment __alloc_size(4);
972 
973 void *__kmalloc_large_noprof(size_t size, gfp_t flags)
974 				__assume_page_alignment __alloc_size(1);
975 
976 void *__kmalloc_large_node_noprof(size_t size, gfp_t flags, int node)
977 				__assume_page_alignment __alloc_size(1);
978 
_kmalloc_noprof(size_t size,gfp_t flags,kmalloc_token_t token)979 static __always_inline __alloc_size(1) void *_kmalloc_noprof(size_t size, gfp_t flags, kmalloc_token_t token)
980 {
981 	if (__builtin_constant_p(size) && size) {
982 		unsigned int index;
983 
984 		if (size > KMALLOC_MAX_CACHE_SIZE)
985 			return __kmalloc_large_noprof(size, flags);
986 
987 		index = kmalloc_index(size);
988 		return __kmalloc_cache_noprof(
989 				kmalloc_caches[kmalloc_type(flags, token)][index],
990 				flags, size);
991 	}
992 	return __kmalloc_noprof(PASS_TOKEN_PARAMS(size, token), flags);
993 }
994 #define kmalloc_noprof(...)			_kmalloc_noprof(__VA_ARGS__, __kmalloc_token(__VA_ARGS__))
995 #if 0 /* kernel-doc */
996 /**
997  * kmalloc - allocate kernel memory
998  * @size: how many bytes of memory are required.
999  * @flags: describe the allocation context
1000  *
1001  * kmalloc is the normal method of allocating memory
1002  * for objects smaller than page size in the kernel.
1003  *
1004  * The allocated object address is aligned to at least ARCH_KMALLOC_MINALIGN
1005  * bytes. For @size of power of two bytes, the alignment is also guaranteed
1006  * to be at least to the size. For other sizes, the alignment is guaranteed to
1007  * be at least the largest power-of-two divisor of @size.
1008  *
1009  * The @flags argument may be one of the GFP flags defined at
1010  * include/linux/gfp_types.h and described at
1011  * :ref:`Documentation/core-api/mm-api.rst <mm-api-gfp-flags>`
1012  *
1013  * The recommended usage of the @flags is described at
1014  * :ref:`Documentation/core-api/memory-allocation.rst <memory_allocation>`
1015  *
1016  * Below is a brief outline of the most useful GFP flags
1017  *
1018  * %GFP_KERNEL
1019  *	Allocate normal kernel ram. May sleep.
1020  *
1021  * %GFP_NOWAIT
1022  *	Allocation will not sleep.
1023  *
1024  * %GFP_ATOMIC
1025  *	Allocation will not sleep.  May use emergency pools.
1026  *
1027  * Also it is possible to set different flags by OR'ing
1028  * in one or more of the following additional @flags:
1029  *
1030  * %__GFP_ZERO
1031  *	Zero the allocated memory before returning. Also see kzalloc().
1032  *
1033  * %__GFP_HIGH
1034  *	This allocation has high priority and may use emergency pools.
1035  *
1036  * %__GFP_NOFAIL
1037  *	Indicate that this allocation is in no way allowed to fail
1038  *	(think twice before using).
1039  *
1040  * %__GFP_NORETRY
1041  *	If memory is not immediately available,
1042  *	then give up at once.
1043  *
1044  * %__GFP_NOWARN
1045  *	If allocation fails, don't issue any warnings.
1046  *
1047  * %__GFP_RETRY_MAYFAIL
1048  *	Try really hard to succeed the allocation but fail
1049  *	eventually.
1050  */
1051 void *kmalloc(size_t size, gfp_t flags);
1052 #endif
1053 #define kmalloc(size, flags)			alloc_hooks(kmalloc_noprof(size, flags))
1054 
1055 void *_kmalloc_nolock_noprof(DECL_TOKEN_PARAMS(size, token), gfp_t gfp_flags, int node);
1056 #define kmalloc_nolock_noprof(_s, _f, _n)	_kmalloc_nolock_noprof(PASS_TOKEN_PARAMS(_s, __kmalloc_token(_s)), _f, _n)
1057 #if 0 /* kernel-doc */
1058 /**
1059  * kmalloc_nolock - Allocate an object of given size from any context.
1060  * @size: size to allocate
1061  * @gfp_flags: GFP flags. Only __GFP_ACCOUNT and __GFP_ZERO allowed.  Also
1062  * __GFP_NOWARN and __GFP_NOMEMALLOC are allowed but added internally thus not
1063  * necessary.
1064  * @node: node number of the target node.
1065  *
1066  * Return: pointer to the new object or NULL in case of error.
1067  * NULL does not mean EBUSY or EAGAIN. It means ENOMEM.
1068  * There is no reason to call it again and expect !NULL.
1069  */
1070 void *kmalloc_nolock(size_t size, gfp_t gfp_flags, int node);
1071 #endif
1072 #define kmalloc_nolock(size, gfp_flags, node)	alloc_hooks(kmalloc_nolock_noprof(size, gfp_flags, node))
1073 
1074 /**
1075  * __alloc_objs - Allocate objects of a given type using
1076  * @KMALLOC: which size-based kmalloc wrapper to allocate with.
1077  * @GFP: GFP flags for the allocation.
1078  * @TYPE: type to allocate space for.
1079  * @COUNT: how many @TYPE objects to allocate.
1080  *
1081  * Returns: Newly allocated pointer to (first) @TYPE of @COUNT-many
1082  * allocated @TYPE objects, or NULL on failure.
1083  */
1084 #define __alloc_objs(KMALLOC, GFP, TYPE, COUNT)				\
1085 ({									\
1086 	const size_t __obj_size = size_mul(sizeof(TYPE), COUNT);	\
1087 	(TYPE *)KMALLOC(__obj_size, GFP);				\
1088 })
1089 
1090 /**
1091  * __alloc_flex - Allocate an object that has a trailing flexible array
1092  * @KMALLOC: kmalloc wrapper function to use for allocation.
1093  * @GFP: GFP flags for the allocation.
1094  * @TYPE: type of structure to allocate space for.
1095  * @FAM: The name of the flexible array member of @TYPE structure.
1096  * @COUNT: how many @FAM elements to allocate space for.
1097  *
1098  * Returns: Newly allocated pointer to @TYPE with @COUNT-many trailing
1099  * @FAM elements, or NULL on failure or if @COUNT cannot be represented
1100  * by the member of @TYPE that counts the @FAM elements (annotated via
1101  * __counted_by()).
1102  */
1103 #define __alloc_flex(KMALLOC, GFP, TYPE, FAM, COUNT)			\
1104 ({									\
1105 	const size_t __count = (COUNT);					\
1106 	const size_t __obj_size = struct_size_t(TYPE, FAM, __count);	\
1107 	TYPE *__obj_ptr = KMALLOC(__obj_size, GFP);			\
1108 	if (__obj_ptr)							\
1109 		__set_flex_counter(__obj_ptr->FAM, __count);		\
1110 	__obj_ptr;							\
1111 })
1112 
1113 /**
1114  * kmalloc_obj - Allocate a single instance of the given type
1115  * @VAR_OR_TYPE: Variable or type to allocate.
1116  * @...: optional GFP flags for the allocation (GFP_KERNEL when not specified).
1117  *
1118  * Returns: newly allocated pointer to a @VAR_OR_TYPE on success, or NULL
1119  * on failure.
1120  */
1121 #define kmalloc_obj(VAR_OR_TYPE, ...) \
1122 	__alloc_objs(kmalloc, default_gfp(__VA_ARGS__), typeof(VAR_OR_TYPE), 1)
1123 
1124 /**
1125  * kmalloc_objs - Allocate an array of the given type
1126  * @VAR_OR_TYPE: Variable or type to allocate an array of.
1127  * @COUNT: How many elements in the array.
1128  * @...: optional GFP flags for the allocation (GFP_KERNEL when not specified).
1129  *
1130  * Returns: newly allocated pointer to array of @VAR_OR_TYPE on success,
1131  * or NULL on failure.
1132  */
1133 #define kmalloc_objs(VAR_OR_TYPE, COUNT, ...) \
1134 	__alloc_objs(kmalloc, default_gfp(__VA_ARGS__), typeof(VAR_OR_TYPE), COUNT)
1135 
1136 /**
1137  * kmalloc_flex - Allocate a single instance of the given flexible structure
1138  * @VAR_OR_TYPE: Variable or type to allocate (with its flex array).
1139  * @FAM: The name of the flexible array member of the structure.
1140  * @COUNT: How many flexible array member elements are desired.
1141  * @...: optional GFP flags for the allocation (GFP_KERNEL when not specified).
1142  *
1143  * Returns: newly allocated pointer to @VAR_OR_TYPE on success, NULL on
1144  * failure. If @FAM has been annotated with __counted_by(), the allocation
1145  * will immediately fail if @COUNT is larger than what the type of the
1146  * struct's counter variable can represent.
1147  */
1148 #define kmalloc_flex(VAR_OR_TYPE, FAM, COUNT, ...) \
1149 	__alloc_flex(kmalloc, default_gfp(__VA_ARGS__), typeof(VAR_OR_TYPE), FAM, COUNT)
1150 
1151 /* All kzalloc aliases for kmalloc_(obj|objs|flex). */
1152 #define kzalloc_obj(P, ...) \
1153 	__alloc_objs(kzalloc, default_gfp(__VA_ARGS__), typeof(P), 1)
1154 #define kzalloc_objs(P, COUNT, ...) \
1155 	__alloc_objs(kzalloc, default_gfp(__VA_ARGS__), typeof(P), COUNT)
1156 #define kzalloc_flex(P, FAM, COUNT, ...)		\
1157 	__alloc_flex(kzalloc, default_gfp(__VA_ARGS__), typeof(P), FAM, COUNT)
1158 
1159 /* All kvmalloc aliases for kmalloc_(obj|objs|flex). */
1160 #define kvmalloc_obj(P, ...) \
1161 	__alloc_objs(kvmalloc, default_gfp(__VA_ARGS__), typeof(P), 1)
1162 #define kvmalloc_objs(P, COUNT, ...) \
1163 	__alloc_objs(kvmalloc, default_gfp(__VA_ARGS__), typeof(P), COUNT)
1164 #define kvmalloc_flex(P, FAM, COUNT, ...) \
1165 	__alloc_flex(kvmalloc, default_gfp(__VA_ARGS__), typeof(P), FAM, COUNT)
1166 
1167 /* All kvzalloc aliases for kmalloc_(obj|objs|flex). */
1168 #define kvzalloc_obj(P, ...) \
1169 	__alloc_objs(kvzalloc, default_gfp(__VA_ARGS__), typeof(P), 1)
1170 #define kvzalloc_objs(P, COUNT, ...) \
1171 	__alloc_objs(kvzalloc, default_gfp(__VA_ARGS__), typeof(P), COUNT)
1172 #define kvzalloc_flex(P, FAM, COUNT, ...) \
1173 	__alloc_flex(kvzalloc, default_gfp(__VA_ARGS__), typeof(P), FAM, COUNT)
1174 
1175 #define kmem_buckets_alloc(_b, _size, _flags)	\
1176 	alloc_hooks(__kmalloc_node_noprof(PASS_KMALLOC_PARAMS(_size, _b, __kmalloc_token(_size)), _flags, NUMA_NO_NODE))
1177 
1178 #define kmem_buckets_alloc_node_track_caller(_b, _size, _flags, _node)	\
1179 	alloc_hooks(__kmalloc_node_track_caller_noprof(PASS_KMALLOC_PARAMS(_size, _b, __kmalloc_token(_size)), _flags, _node, _RET_IP_))
1180 
1181 #define kmem_buckets_alloc_track_caller(_b, _size, _flags) \
1182 	kmem_buckets_alloc_node_track_caller(_b, _size, _flags, NUMA_NO_NODE)
1183 
_kmalloc_node_noprof(size_t size,gfp_t flags,int node,kmalloc_token_t token)1184 static __always_inline __alloc_size(1) void *_kmalloc_node_noprof(size_t size, gfp_t flags, int node, kmalloc_token_t token)
1185 {
1186 	if (__builtin_constant_p(size) && size) {
1187 		unsigned int index;
1188 
1189 		if (size > KMALLOC_MAX_CACHE_SIZE)
1190 			return __kmalloc_large_node_noprof(size, flags, node);
1191 
1192 		index = kmalloc_index(size);
1193 		return __kmalloc_cache_node_noprof(
1194 				kmalloc_caches[kmalloc_type(flags, token)][index],
1195 				flags, node, size);
1196 	}
1197 	return __kmalloc_node_noprof(PASS_KMALLOC_PARAMS(size, NULL, token), flags, node);
1198 }
1199 #define kmalloc_node_noprof(...)		_kmalloc_node_noprof(__VA_ARGS__, __kmalloc_token(__VA_ARGS__))
1200 #define kmalloc_node(...)			alloc_hooks(kmalloc_node_noprof(__VA_ARGS__))
1201 
_kmalloc_array_noprof(size_t n,size_t size,gfp_t flags,kmalloc_token_t token)1202 static inline __alloc_size(1, 2) void *_kmalloc_array_noprof(size_t n, size_t size, gfp_t flags, kmalloc_token_t token)
1203 {
1204 	size_t bytes;
1205 
1206 	if (unlikely(check_mul_overflow(n, size, &bytes)))
1207 		return NULL;
1208 	return _kmalloc_noprof(bytes, flags, token);
1209 }
1210 #define kmalloc_array_noprof(...)		_kmalloc_array_noprof(__VA_ARGS__, __kmalloc_token(__VA_ARGS__))
1211 #if 0 /* kernel-doc */
1212 /**
1213  * kmalloc_array - allocate memory for an array.
1214  * @n: number of elements.
1215  * @size: element size.
1216  * @flags: the type of memory to allocate (see kmalloc).
1217  */
1218 void *kmalloc_array(size_t n, size_t size, gfp_t flags);
1219 #endif
1220 #define kmalloc_array(n, size, flags)		alloc_hooks(kmalloc_array_noprof(n, size, flags))
1221 
_krealloc_array_noprof(void * p,size_t new_n,size_t new_size,gfp_t flags,kmalloc_token_t token)1222 static inline __realloc_size(2, 3) void * __must_check _krealloc_array_noprof(void *p,
1223 								       size_t new_n,
1224 								       size_t new_size,
1225 								       gfp_t flags, kmalloc_token_t token)
1226 {
1227 	size_t bytes;
1228 
1229 	if (unlikely(check_mul_overflow(new_n, new_size, &bytes)))
1230 		return NULL;
1231 
1232 	return krealloc_node_align_noprof(p, PASS_TOKEN_PARAMS(bytes, token), 1, flags, NUMA_NO_NODE);
1233 }
1234 #define krealloc_array_noprof(...)		_krealloc_array_noprof(__VA_ARGS__, __kmalloc_token(__VA_ARGS__))
1235 #if 0 /* kernel-doc */
1236 /**
1237  * krealloc_array - reallocate memory for an array.
1238  * @p: pointer to the memory chunk to reallocate
1239  * @new_n: new number of elements to alloc
1240  * @new_size: new size of a single member of the array
1241  * @flags: the type of memory to allocate (see kmalloc)
1242  *
1243  * If __GFP_ZERO logic is requested, callers must ensure that, starting with the
1244  * initial memory allocation, every subsequent call to this API for the same
1245  * memory allocation is flagged with __GFP_ZERO. Otherwise, it is possible that
1246  * __GFP_ZERO is not fully honored by this API.
1247  *
1248  * See krealloc_noprof() for further details.
1249  *
1250  * In any case, the contents of the object pointed to are preserved up to the
1251  * lesser of the new and old sizes.
1252  */
1253 void *krealloc_array(void *p, size_t new_n, size_t new_size, gfp_t flags);
1254 #endif
1255 #define krealloc_array(p, new_n, new_size, flags) alloc_hooks(krealloc_array_noprof(p, new_n, new_size, flags))
1256 
1257 /**
1258  * kcalloc - allocate memory for an array. The memory is set to zero.
1259  * @n: number of elements.
1260  * @size: element size.
1261  * @flags: the type of memory to allocate (see kmalloc).
1262  */
1263 #define kcalloc(n, size, flags)		kmalloc_array(n, size, (flags) | __GFP_ZERO)
1264 
1265 void *__kmalloc_node_track_caller_noprof(DECL_KMALLOC_PARAMS(size, b, token), gfp_t flags, int node,
1266 					 unsigned long caller) __alloc_size(1);
1267 #define kmalloc_node_track_caller_noprof(size, flags, node, caller) \
1268 	__kmalloc_node_track_caller_noprof(PASS_KMALLOC_PARAMS(size, NULL, __kmalloc_token(size)), flags, node, caller)
1269 #define kmalloc_node_track_caller(...)		\
1270 	alloc_hooks(kmalloc_node_track_caller_noprof(__VA_ARGS__, _RET_IP_))
1271 
1272 /*
1273  * kmalloc_track_caller is a special version of kmalloc that records the
1274  * calling function of the routine calling it for slab leak tracking instead
1275  * of just the calling function (confusing, eh?).
1276  * It's useful when the call to kmalloc comes from a widely-used standard
1277  * allocator where we care about the real place the memory allocation
1278  * request comes from.
1279  */
1280 #define kmalloc_track_caller(...)		kmalloc_node_track_caller(__VA_ARGS__, NUMA_NO_NODE)
1281 
1282 #define kmalloc_track_caller_noprof(...)	\
1283 		kmalloc_node_track_caller_noprof(__VA_ARGS__, NUMA_NO_NODE, _RET_IP_)
1284 
_kmalloc_array_node_noprof(size_t n,size_t size,gfp_t flags,int node,kmalloc_token_t token)1285 static inline __alloc_size(1, 2) void *_kmalloc_array_node_noprof(size_t n, size_t size, gfp_t flags,
1286 								  int node, kmalloc_token_t token)
1287 {
1288 	size_t bytes;
1289 
1290 	if (unlikely(check_mul_overflow(n, size, &bytes)))
1291 		return NULL;
1292 	if (__builtin_constant_p(n) && __builtin_constant_p(size))
1293 		return _kmalloc_node_noprof(bytes, flags, node, token);
1294 	return __kmalloc_node_noprof(PASS_KMALLOC_PARAMS(bytes, NULL, token), flags, node);
1295 }
1296 #define kmalloc_array_node_noprof(...)		_kmalloc_array_node_noprof(__VA_ARGS__, __kmalloc_token(__VA_ARGS__))
1297 #define kmalloc_array_node(...)			alloc_hooks(kmalloc_array_node_noprof(__VA_ARGS__))
1298 
1299 #define kcalloc_node(_n, _size, _flags, _node)	\
1300 	kmalloc_array_node(_n, _size, (_flags) | __GFP_ZERO, _node)
1301 
1302 /*
1303  * Shortcuts
1304  */
1305 #define kmem_cache_zalloc(_k, _flags)		kmem_cache_alloc(_k, (_flags)|__GFP_ZERO)
1306 
_kzalloc_noprof(size_t size,gfp_t flags,kmalloc_token_t token)1307 static inline __alloc_size(1) void *_kzalloc_noprof(size_t size, gfp_t flags, kmalloc_token_t token)
1308 {
1309 	return _kmalloc_noprof(size, flags | __GFP_ZERO, token);
1310 }
1311 #define kzalloc_noprof(...)			_kzalloc_noprof(__VA_ARGS__, __kmalloc_token(__VA_ARGS__))
1312 #if 0 /* kernel-doc */
1313 /**
1314  * kzalloc - allocate memory. The memory is set to zero.
1315  * @size: how many bytes of memory are required.
1316  * @flags: the type of memory to allocate (see kmalloc).
1317  */
1318 void *kzalloc(size_t size, gfp_t flags);
1319 #endif
1320 #define kzalloc(size, flags)			alloc_hooks(kzalloc_noprof(size, flags))
1321 #define kzalloc_node(_size, _flags, _node)	kmalloc_node(_size, (_flags)|__GFP_ZERO, _node)
1322 
1323 void *__kvmalloc_node_noprof(DECL_KMALLOC_PARAMS(size, b, token), unsigned long align,
1324 			     gfp_t flags, int node) __alloc_size(1);
1325 #define kvmalloc_node_align_noprof(_size, _align, _flags, _node)	\
1326 	__kvmalloc_node_noprof(PASS_KMALLOC_PARAMS(_size, NULL, __kmalloc_token(_size)), _align, _flags, _node)
1327 #define kvmalloc_node_align(...)		\
1328 	alloc_hooks(kvmalloc_node_align_noprof(__VA_ARGS__))
1329 #if 0 /* kernel-doc */
1330 /**
1331  * kvmalloc_node - attempt to allocate physically contiguous memory, but upon
1332  * failure, fall back to non-contiguous (vmalloc) allocation.
1333  * @size: size of the request.
1334  * @flags: gfp mask for the allocation - must be compatible (superset) with GFP_KERNEL.
1335  * @node: numa node to allocate from
1336  *
1337  * Only alignments up to those guaranteed by kmalloc() will be honored. Please see
1338  * Documentation/core-api/memory-allocation.rst for more details.
1339  *
1340  * Uses kmalloc to get the memory but if the allocation fails then falls back
1341  * to the vmalloc allocator. Use kvfree for freeing the memory.
1342  *
1343  * GFP_NOWAIT and GFP_ATOMIC are supported, the __GFP_NORETRY modifier is not.
1344  * __GFP_RETRY_MAYFAIL is supported, and it should be used only if kmalloc is
1345  * preferable to the vmalloc fallback, due to visible performance drawbacks.
1346  *
1347  * Return: pointer to the allocated memory of %NULL in case of failure
1348  */
1349 void *kvmalloc_node(size_t size, gfp_t flags, int node);
1350 #endif
1351 #define kvmalloc_node(size, flags, node)	kvmalloc_node_align(size, 1, flags, node)
1352 #define kvmalloc_node_noprof(size, flags, node)	\
1353 	kvmalloc_node_align_noprof(size, 1, flags, node)
1354 #define kvmalloc(...)				kvmalloc_node(__VA_ARGS__, NUMA_NO_NODE)
1355 #define kvmalloc_noprof(_size, _flags)		kvmalloc_node_noprof(_size, _flags, NUMA_NO_NODE)
1356 #define kvzalloc(_size, _flags)			kvmalloc(_size, (_flags)|__GFP_ZERO)
1357 
1358 #define kvzalloc_node(_size, _flags, _node)	kvmalloc_node(_size, (_flags)|__GFP_ZERO, _node)
1359 
1360 #define kmem_buckets_valloc(_b, _size, _flags)	\
1361 	alloc_hooks(__kvmalloc_node_noprof(PASS_KMALLOC_PARAMS(_size, _b, __kmalloc_token(_size)), 1, _flags, NUMA_NO_NODE))
1362 
1363 static inline __alloc_size(1, 2) void *
_kvmalloc_array_node_noprof(size_t n,size_t size,gfp_t flags,int node,kmalloc_token_t token)1364 _kvmalloc_array_node_noprof(size_t n, size_t size, gfp_t flags, int node, kmalloc_token_t token)
1365 {
1366 	size_t bytes;
1367 
1368 	if (unlikely(check_mul_overflow(n, size, &bytes)))
1369 		return NULL;
1370 
1371 	return __kvmalloc_node_noprof(PASS_KMALLOC_PARAMS(bytes, NULL, token), 1, flags, node);
1372 }
1373 #define kvmalloc_array_node_noprof(...)		_kvmalloc_array_node_noprof(__VA_ARGS__, __kmalloc_token(__VA_ARGS__))
1374 #define kvmalloc_array_noprof(...)		kvmalloc_array_node_noprof(__VA_ARGS__, NUMA_NO_NODE)
1375 #define kvcalloc_node_noprof(_n,_s,_f,_node)	kvmalloc_array_node_noprof(_n,_s,(_f)|__GFP_ZERO,_node)
1376 #define kvcalloc_noprof(...)			kvcalloc_node_noprof(__VA_ARGS__, NUMA_NO_NODE)
1377 
1378 #define kvmalloc_array(...)			alloc_hooks(kvmalloc_array_noprof(__VA_ARGS__))
1379 #define kvcalloc_node(...)			alloc_hooks(kvcalloc_node_noprof(__VA_ARGS__))
1380 #define kvcalloc(...)				alloc_hooks(kvcalloc_noprof(__VA_ARGS__))
1381 
1382 void *kvrealloc_node_align_noprof(const void *p, DECL_TOKEN_PARAMS(size, token), unsigned long align,
1383 				  gfp_t flags, int nid) __realloc_size(2);
1384 #if 0 /* kernel-doc */
1385 /**
1386  * kvrealloc_node_align - reallocate memory; contents remain unchanged
1387  * @p: object to reallocate memory for
1388  * @size: the size to reallocate
1389  * @align: desired alignment
1390  * @flags: the flags for the page level allocator
1391  * @nid: NUMA node id
1392  *
1393  * If @p is %NULL, kvrealloc() behaves exactly like kvmalloc(). If @size is 0
1394  * and @p is not a %NULL pointer, the object pointed to is freed.
1395  *
1396  * Only alignments up to those guaranteed by kmalloc() will be honored. Please see
1397  * Documentation/core-api/memory-allocation.rst for more details.
1398  *
1399  * If __GFP_ZERO logic is requested, callers must ensure that, starting with the
1400  * initial memory allocation, every subsequent call to this API for the same
1401  * memory allocation is flagged with __GFP_ZERO. Otherwise, it is possible that
1402  * __GFP_ZERO is not fully honored by this API.
1403  *
1404  * In any case, the contents of the object pointed to are preserved up to the
1405  * lesser of the new and old sizes.
1406  *
1407  * This function must not be called concurrently with itself or kvfree() for the
1408  * same memory allocation.
1409  *
1410  * Return: pointer to the allocated memory or %NULL in case of error
1411  */
1412 void *kvrealloc_node_align(const void *p, size_t size, unsigned long align, gfp_t flags, int nid);
1413 #endif
1414 #define kvrealloc_node_align(p, size, align, flags, nid)	\
1415 	alloc_hooks(kvrealloc_node_align_noprof(p, PASS_TOKEN_PARAMS(size, __kmalloc_token(size)), align, flags, nid))
1416 #define kvrealloc_node(_p, _s, _f, _n)		kvrealloc_node_align(_p, _s, 1, _f, _n)
1417 #define kvrealloc(...)				kvrealloc_node(__VA_ARGS__, NUMA_NO_NODE)
1418 
1419 extern void kvfree(const void *addr);
1420 DEFINE_FREE(kvfree, void *, if (!IS_ERR_OR_NULL(_T)) kvfree(_T))
1421 
1422 extern void kvfree_atomic(const void *addr);
1423 DEFINE_FREE(kvfree_atomic, void *, if (!IS_ERR_OR_NULL(_T)) kvfree_atomic(_T))
1424 
1425 extern void kvfree_sensitive(const void *addr, size_t len);
1426 
1427 unsigned int kmem_cache_size(struct kmem_cache *s);
1428 
1429 #ifndef CONFIG_KVFREE_RCU_BATCHED
kvfree_rcu_barrier(void)1430 static inline void kvfree_rcu_barrier(void)
1431 {
1432 	rcu_barrier();
1433 }
1434 
kvfree_rcu_barrier_on_cache(struct kmem_cache * s)1435 static inline void kvfree_rcu_barrier_on_cache(struct kmem_cache *s)
1436 {
1437 	rcu_barrier();
1438 }
1439 
kfree_rcu_scheduler_running(void)1440 static inline void kfree_rcu_scheduler_running(void) { }
1441 #else
1442 void kvfree_rcu_barrier(void);
1443 
1444 void kvfree_rcu_barrier_on_cache(struct kmem_cache *s);
1445 
1446 void kfree_rcu_scheduler_running(void);
1447 #endif
1448 
1449 /**
1450  * kmalloc_size_roundup - Report allocation bucket size for the given size
1451  *
1452  * @size: Number of bytes to round up from.
1453  *
1454  * This returns the number of bytes that would be available in a kmalloc()
1455  * allocation of @size bytes. For example, a 126 byte request would be
1456  * rounded up to the next sized kmalloc bucket, 128 bytes. (This is strictly
1457  * for the general-purpose kmalloc()-based allocations, and is not for the
1458  * pre-sized kmem_cache_alloc()-based allocations.)
1459  *
1460  * Use this to kmalloc() the full bucket size ahead of time instead of using
1461  * ksize() to query the size after an allocation.
1462  */
1463 size_t kmalloc_size_roundup(size_t size);
1464 
1465 void __init kmem_cache_init_late(void);
1466 void __init kvfree_rcu_init(void);
1467 
1468 #endif	/* _LINUX_SLAB_H */
1469