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