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