GNU Linux-libre 4.14.266-gnu1
[releases.git] / include / linux / slab.h
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/types.h>
17 #include <linux/workqueue.h>
18
19
20 /*
21  * Flags to pass to kmem_cache_create().
22  * The ones marked DEBUG are only valid if CONFIG_DEBUG_SLAB is set.
23  */
24 #define SLAB_CONSISTENCY_CHECKS 0x00000100UL    /* DEBUG: Perform (expensive) checks on alloc/free */
25 #define SLAB_RED_ZONE           0x00000400UL    /* DEBUG: Red zone objs in a cache */
26 #define SLAB_POISON             0x00000800UL    /* DEBUG: Poison objects */
27 #define SLAB_HWCACHE_ALIGN      0x00002000UL    /* Align objs on cache lines */
28 #define SLAB_CACHE_DMA          0x00004000UL    /* Use GFP_DMA memory */
29 #define SLAB_STORE_USER         0x00010000UL    /* DEBUG: Store the last owner for bug hunting */
30 #define SLAB_PANIC              0x00040000UL    /* Panic if kmem_cache_create() fails */
31 /*
32  * SLAB_TYPESAFE_BY_RCU - **WARNING** READ THIS!
33  *
34  * This delays freeing the SLAB page by a grace period, it does _NOT_
35  * delay object freeing. This means that if you do kmem_cache_free()
36  * that memory location is free to be reused at any time. Thus it may
37  * be possible to see another object there in the same RCU grace period.
38  *
39  * This feature only ensures the memory location backing the object
40  * stays valid, the trick to using this is relying on an independent
41  * object validation pass. Something like:
42  *
43  *  rcu_read_lock()
44  * again:
45  *  obj = lockless_lookup(key);
46  *  if (obj) {
47  *    if (!try_get_ref(obj)) // might fail for free objects
48  *      goto again;
49  *
50  *    if (obj->key != key) { // not the object we expected
51  *      put_ref(obj);
52  *      goto again;
53  *    }
54  *  }
55  *  rcu_read_unlock();
56  *
57  * This is useful if we need to approach a kernel structure obliquely,
58  * from its address obtained without the usual locking. We can lock
59  * the structure to stabilize it and check it's still at the given address,
60  * only if we can be sure that the memory has not been meanwhile reused
61  * for some other kind of object (which our subsystem's lock might corrupt).
62  *
63  * rcu_read_lock before reading the address, then rcu_read_unlock after
64  * taking the spinlock within the structure expected at that address.
65  *
66  * Note that SLAB_TYPESAFE_BY_RCU was originally named SLAB_DESTROY_BY_RCU.
67  */
68 #define SLAB_TYPESAFE_BY_RCU    0x00080000UL    /* Defer freeing slabs to RCU */
69 #define SLAB_MEM_SPREAD         0x00100000UL    /* Spread some memory over cpuset */
70 #define SLAB_TRACE              0x00200000UL    /* Trace allocations and frees */
71
72 /* Flag to prevent checks on free */
73 #ifdef CONFIG_DEBUG_OBJECTS
74 # define SLAB_DEBUG_OBJECTS     0x00400000UL
75 #else
76 # define SLAB_DEBUG_OBJECTS     0x00000000UL
77 #endif
78
79 #define SLAB_NOLEAKTRACE        0x00800000UL    /* Avoid kmemleak tracing */
80
81 #ifdef CONFIG_FAILSLAB
82 # define SLAB_FAILSLAB          0x02000000UL    /* Fault injection mark */
83 #else
84 # define SLAB_FAILSLAB          0x00000000UL
85 #endif
86 #if defined(CONFIG_MEMCG) && !defined(CONFIG_SLOB)
87 # define SLAB_ACCOUNT           0x04000000UL    /* Account to memcg */
88 #else
89 # define SLAB_ACCOUNT           0x00000000UL
90 #endif
91
92 #ifdef CONFIG_KASAN
93 #define SLAB_KASAN              0x08000000UL
94 #else
95 #define SLAB_KASAN              0x00000000UL
96 #endif
97
98 /* The following flags affect the page allocator grouping pages by mobility */
99 #define SLAB_RECLAIM_ACCOUNT    0x00020000UL            /* Objects are reclaimable */
100 #define SLAB_TEMPORARY          SLAB_RECLAIM_ACCOUNT    /* Objects are short-lived */
101 /*
102  * ZERO_SIZE_PTR will be returned for zero sized kmalloc requests.
103  *
104  * Dereferencing ZERO_SIZE_PTR will lead to a distinct access fault.
105  *
106  * ZERO_SIZE_PTR can be passed to kfree though in the same way that NULL can.
107  * Both make kfree a no-op.
108  */
109 #define ZERO_SIZE_PTR ((void *)16)
110
111 #define ZERO_OR_NULL_PTR(x) ((unsigned long)(x) <= \
112                                 (unsigned long)ZERO_SIZE_PTR)
113
114 #include <linux/kmemleak.h>
115 #include <linux/kasan.h>
116
117 struct mem_cgroup;
118 /*
119  * struct kmem_cache related prototypes
120  */
121 void __init kmem_cache_init(void);
122 bool slab_is_available(void);
123
124 struct kmem_cache *kmem_cache_create(const char *, size_t, size_t,
125                         unsigned long,
126                         void (*)(void *));
127 void kmem_cache_destroy(struct kmem_cache *);
128 int kmem_cache_shrink(struct kmem_cache *);
129
130 void memcg_create_kmem_cache(struct mem_cgroup *, struct kmem_cache *);
131 void memcg_deactivate_kmem_caches(struct mem_cgroup *);
132 void memcg_destroy_kmem_caches(struct mem_cgroup *);
133
134 /*
135  * Please use this macro to create slab caches. Simply specify the
136  * name of the structure and maybe some flags that are listed above.
137  *
138  * The alignment of the struct determines object alignment. If you
139  * f.e. add ____cacheline_aligned_in_smp to the struct declaration
140  * then the objects will be properly aligned in SMP configurations.
141  */
142 #define KMEM_CACHE(__struct, __flags) kmem_cache_create(#__struct,\
143                 sizeof(struct __struct), __alignof__(struct __struct),\
144                 (__flags), NULL)
145
146 /*
147  * Common kmalloc functions provided by all allocators
148  */
149 void * __must_check __krealloc(const void *, size_t, gfp_t);
150 void * __must_check krealloc(const void *, size_t, gfp_t);
151 void kfree(const void *);
152 void kzfree(const void *);
153 size_t ksize(const void *);
154
155 #ifdef CONFIG_HAVE_HARDENED_USERCOPY_ALLOCATOR
156 const char *__check_heap_object(const void *ptr, unsigned long n,
157                                 struct page *page);
158 #else
159 static inline const char *__check_heap_object(const void *ptr,
160                                               unsigned long n,
161                                               struct page *page)
162 {
163         return NULL;
164 }
165 #endif
166
167 /*
168  * Some archs want to perform DMA into kmalloc caches and need a guaranteed
169  * alignment larger than the alignment of a 64-bit integer.
170  * Setting ARCH_KMALLOC_MINALIGN in arch headers allows that.
171  */
172 #if defined(ARCH_DMA_MINALIGN) && ARCH_DMA_MINALIGN > 8
173 #define ARCH_KMALLOC_MINALIGN ARCH_DMA_MINALIGN
174 #define KMALLOC_MIN_SIZE ARCH_DMA_MINALIGN
175 #define KMALLOC_SHIFT_LOW ilog2(ARCH_DMA_MINALIGN)
176 #else
177 #define ARCH_KMALLOC_MINALIGN __alignof__(unsigned long long)
178 #endif
179
180 /*
181  * Setting ARCH_SLAB_MINALIGN in arch headers allows a different alignment.
182  * Intended for arches that get misalignment faults even for 64 bit integer
183  * aligned buffers.
184  */
185 #ifndef ARCH_SLAB_MINALIGN
186 #define ARCH_SLAB_MINALIGN __alignof__(unsigned long long)
187 #endif
188
189 /*
190  * kmalloc and friends return ARCH_KMALLOC_MINALIGN aligned
191  * pointers. kmem_cache_alloc and friends return ARCH_SLAB_MINALIGN
192  * aligned pointers.
193  */
194 #define __assume_kmalloc_alignment __assume_aligned(ARCH_KMALLOC_MINALIGN)
195 #define __assume_slab_alignment __assume_aligned(ARCH_SLAB_MINALIGN)
196 #define __assume_page_alignment __assume_aligned(PAGE_SIZE)
197
198 /*
199  * Kmalloc array related definitions
200  */
201
202 #ifdef CONFIG_SLAB
203 /*
204  * The largest kmalloc size supported by the SLAB allocators is
205  * 32 megabyte (2^25) or the maximum allocatable page order if that is
206  * less than 32 MB.
207  *
208  * WARNING: Its not easy to increase this value since the allocators have
209  * to do various tricks to work around compiler limitations in order to
210  * ensure proper constant folding.
211  */
212 #define KMALLOC_SHIFT_HIGH      ((MAX_ORDER + PAGE_SHIFT - 1) <= 25 ? \
213                                 (MAX_ORDER + PAGE_SHIFT - 1) : 25)
214 #define KMALLOC_SHIFT_MAX       KMALLOC_SHIFT_HIGH
215 #ifndef KMALLOC_SHIFT_LOW
216 #define KMALLOC_SHIFT_LOW       5
217 #endif
218 #endif
219
220 #ifdef CONFIG_SLUB
221 /*
222  * SLUB directly allocates requests fitting in to an order-1 page
223  * (PAGE_SIZE*2).  Larger requests are passed to the page allocator.
224  */
225 #define KMALLOC_SHIFT_HIGH      (PAGE_SHIFT + 1)
226 #define KMALLOC_SHIFT_MAX       (MAX_ORDER + PAGE_SHIFT - 1)
227 #ifndef KMALLOC_SHIFT_LOW
228 #define KMALLOC_SHIFT_LOW       3
229 #endif
230 #endif
231
232 #ifdef CONFIG_SLOB
233 /*
234  * SLOB passes all requests larger than one page to the page allocator.
235  * No kmalloc array is necessary since objects of different sizes can
236  * be allocated from the same page.
237  */
238 #define KMALLOC_SHIFT_HIGH      PAGE_SHIFT
239 #define KMALLOC_SHIFT_MAX       (MAX_ORDER + PAGE_SHIFT - 1)
240 #ifndef KMALLOC_SHIFT_LOW
241 #define KMALLOC_SHIFT_LOW       3
242 #endif
243 #endif
244
245 /* Maximum allocatable size */
246 #define KMALLOC_MAX_SIZE        (1UL << KMALLOC_SHIFT_MAX)
247 /* Maximum size for which we actually use a slab cache */
248 #define KMALLOC_MAX_CACHE_SIZE  (1UL << KMALLOC_SHIFT_HIGH)
249 /* Maximum order allocatable via the slab allocagtor */
250 #define KMALLOC_MAX_ORDER       (KMALLOC_SHIFT_MAX - PAGE_SHIFT)
251
252 /*
253  * Kmalloc subsystem.
254  */
255 #ifndef KMALLOC_MIN_SIZE
256 #define KMALLOC_MIN_SIZE (1 << KMALLOC_SHIFT_LOW)
257 #endif
258
259 /*
260  * This restriction comes from byte sized index implementation.
261  * Page size is normally 2^12 bytes and, in this case, if we want to use
262  * byte sized index which can represent 2^8 entries, the size of the object
263  * should be equal or greater to 2^12 / 2^8 = 2^4 = 16.
264  * If minimum size of kmalloc is less than 16, we use it as minimum object
265  * size and give up to use byte sized index.
266  */
267 #define SLAB_OBJ_MIN_SIZE      (KMALLOC_MIN_SIZE < 16 ? \
268                                (KMALLOC_MIN_SIZE) : 16)
269
270 #ifndef CONFIG_SLOB
271 extern struct kmem_cache *kmalloc_caches[KMALLOC_SHIFT_HIGH + 1];
272 #ifdef CONFIG_ZONE_DMA
273 extern struct kmem_cache *kmalloc_dma_caches[KMALLOC_SHIFT_HIGH + 1];
274 #endif
275
276 /*
277  * Figure out which kmalloc slab an allocation of a certain size
278  * belongs to.
279  * 0 = zero alloc
280  * 1 =  65 .. 96 bytes
281  * 2 = 129 .. 192 bytes
282  * n = 2^(n-1)+1 .. 2^n
283  */
284 static __always_inline int kmalloc_index(size_t size)
285 {
286         if (!size)
287                 return 0;
288
289         if (size <= KMALLOC_MIN_SIZE)
290                 return KMALLOC_SHIFT_LOW;
291
292         if (KMALLOC_MIN_SIZE <= 32 && size > 64 && size <= 96)
293                 return 1;
294         if (KMALLOC_MIN_SIZE <= 64 && size > 128 && size <= 192)
295                 return 2;
296         if (size <=          8) return 3;
297         if (size <=         16) return 4;
298         if (size <=         32) return 5;
299         if (size <=         64) return 6;
300         if (size <=        128) return 7;
301         if (size <=        256) return 8;
302         if (size <=        512) return 9;
303         if (size <=       1024) return 10;
304         if (size <=   2 * 1024) return 11;
305         if (size <=   4 * 1024) return 12;
306         if (size <=   8 * 1024) return 13;
307         if (size <=  16 * 1024) return 14;
308         if (size <=  32 * 1024) return 15;
309         if (size <=  64 * 1024) return 16;
310         if (size <= 128 * 1024) return 17;
311         if (size <= 256 * 1024) return 18;
312         if (size <= 512 * 1024) return 19;
313         if (size <= 1024 * 1024) return 20;
314         if (size <=  2 * 1024 * 1024) return 21;
315         if (size <=  4 * 1024 * 1024) return 22;
316         if (size <=  8 * 1024 * 1024) return 23;
317         if (size <=  16 * 1024 * 1024) return 24;
318         if (size <=  32 * 1024 * 1024) return 25;
319         if (size <=  64 * 1024 * 1024) return 26;
320         BUG();
321
322         /* Will never be reached. Needed because the compiler may complain */
323         return -1;
324 }
325 #endif /* !CONFIG_SLOB */
326
327 void *__kmalloc(size_t size, gfp_t flags) __assume_kmalloc_alignment __malloc;
328 void *kmem_cache_alloc(struct kmem_cache *, gfp_t flags) __assume_slab_alignment __malloc;
329 void kmem_cache_free(struct kmem_cache *, void *);
330
331 /*
332  * Bulk allocation and freeing operations. These are accelerated in an
333  * allocator specific way to avoid taking locks repeatedly or building
334  * metadata structures unnecessarily.
335  *
336  * Note that interrupts must be enabled when calling these functions.
337  */
338 void kmem_cache_free_bulk(struct kmem_cache *, size_t, void **);
339 int kmem_cache_alloc_bulk(struct kmem_cache *, gfp_t, size_t, void **);
340
341 /*
342  * Caller must not use kfree_bulk() on memory not originally allocated
343  * by kmalloc(), because the SLOB allocator cannot handle this.
344  */
345 static __always_inline void kfree_bulk(size_t size, void **p)
346 {
347         kmem_cache_free_bulk(NULL, size, p);
348 }
349
350 #ifdef CONFIG_NUMA
351 void *__kmalloc_node(size_t size, gfp_t flags, int node) __assume_kmalloc_alignment __malloc;
352 void *kmem_cache_alloc_node(struct kmem_cache *, gfp_t flags, int node) __assume_slab_alignment __malloc;
353 #else
354 static __always_inline void *__kmalloc_node(size_t size, gfp_t flags, int node)
355 {
356         return __kmalloc(size, flags);
357 }
358
359 static __always_inline void *kmem_cache_alloc_node(struct kmem_cache *s, gfp_t flags, int node)
360 {
361         return kmem_cache_alloc(s, flags);
362 }
363 #endif
364
365 #ifdef CONFIG_TRACING
366 extern void *kmem_cache_alloc_trace(struct kmem_cache *, gfp_t, size_t) __assume_slab_alignment __malloc;
367
368 #ifdef CONFIG_NUMA
369 extern void *kmem_cache_alloc_node_trace(struct kmem_cache *s,
370                                            gfp_t gfpflags,
371                                            int node, size_t size) __assume_slab_alignment __malloc;
372 #else
373 static __always_inline void *
374 kmem_cache_alloc_node_trace(struct kmem_cache *s,
375                               gfp_t gfpflags,
376                               int node, size_t size)
377 {
378         return kmem_cache_alloc_trace(s, gfpflags, size);
379 }
380 #endif /* CONFIG_NUMA */
381
382 #else /* CONFIG_TRACING */
383 static __always_inline void *kmem_cache_alloc_trace(struct kmem_cache *s,
384                 gfp_t flags, size_t size)
385 {
386         void *ret = kmem_cache_alloc(s, flags);
387
388         kasan_kmalloc(s, ret, size, flags);
389         return ret;
390 }
391
392 static __always_inline void *
393 kmem_cache_alloc_node_trace(struct kmem_cache *s,
394                               gfp_t gfpflags,
395                               int node, size_t size)
396 {
397         void *ret = kmem_cache_alloc_node(s, gfpflags, node);
398
399         kasan_kmalloc(s, ret, size, gfpflags);
400         return ret;
401 }
402 #endif /* CONFIG_TRACING */
403
404 extern void *kmalloc_order(size_t size, gfp_t flags, unsigned int order) __assume_page_alignment __malloc;
405
406 #ifdef CONFIG_TRACING
407 extern void *kmalloc_order_trace(size_t size, gfp_t flags, unsigned int order) __assume_page_alignment __malloc;
408 #else
409 static __always_inline void *
410 kmalloc_order_trace(size_t size, gfp_t flags, unsigned int order)
411 {
412         return kmalloc_order(size, flags, order);
413 }
414 #endif
415
416 static __always_inline void *kmalloc_large(size_t size, gfp_t flags)
417 {
418         unsigned int order = get_order(size);
419         return kmalloc_order_trace(size, flags, order);
420 }
421
422 /**
423  * kmalloc - allocate memory
424  * @size: how many bytes of memory are required.
425  * @flags: the type of memory to allocate.
426  *
427  * kmalloc is the normal method of allocating memory
428  * for objects smaller than page size in the kernel.
429  *
430  * The @flags argument may be one of:
431  *
432  * %GFP_USER - Allocate memory on behalf of user.  May sleep.
433  *
434  * %GFP_KERNEL - Allocate normal kernel ram.  May sleep.
435  *
436  * %GFP_ATOMIC - Allocation will not sleep.  May use emergency pools.
437  *   For example, use this inside interrupt handlers.
438  *
439  * %GFP_HIGHUSER - Allocate pages from high memory.
440  *
441  * %GFP_NOIO - Do not do any I/O at all while trying to get memory.
442  *
443  * %GFP_NOFS - Do not make any fs calls while trying to get memory.
444  *
445  * %GFP_NOWAIT - Allocation will not sleep.
446  *
447  * %__GFP_THISNODE - Allocate node-local memory only.
448  *
449  * %GFP_DMA - Allocation suitable for DMA.
450  *   Should only be used for kmalloc() caches. Otherwise, use a
451  *   slab created with SLAB_DMA.
452  *
453  * Also it is possible to set different flags by OR'ing
454  * in one or more of the following additional @flags:
455  *
456  * %__GFP_COLD - Request cache-cold pages instead of
457  *   trying to return cache-warm pages.
458  *
459  * %__GFP_HIGH - This allocation has high priority and may use emergency pools.
460  *
461  * %__GFP_NOFAIL - Indicate that this allocation is in no way allowed to fail
462  *   (think twice before using).
463  *
464  * %__GFP_NORETRY - If memory is not immediately available,
465  *   then give up at once.
466  *
467  * %__GFP_NOWARN - If allocation fails, don't issue any warnings.
468  *
469  * %__GFP_RETRY_MAYFAIL - Try really hard to succeed the allocation but fail
470  *   eventually.
471  *
472  * There are other flags available as well, but these are not intended
473  * for general use, and so are not documented here. For a full list of
474  * potential flags, always refer to linux/gfp.h.
475  */
476 static __always_inline void *kmalloc(size_t size, gfp_t flags)
477 {
478         if (__builtin_constant_p(size)) {
479                 if (size > KMALLOC_MAX_CACHE_SIZE)
480                         return kmalloc_large(size, flags);
481 #ifndef CONFIG_SLOB
482                 if (!(flags & GFP_DMA)) {
483                         int index = kmalloc_index(size);
484
485                         if (!index)
486                                 return ZERO_SIZE_PTR;
487
488                         return kmem_cache_alloc_trace(kmalloc_caches[index],
489                                         flags, size);
490                 }
491 #endif
492         }
493         return __kmalloc(size, flags);
494 }
495
496 /*
497  * Determine size used for the nth kmalloc cache.
498  * return size or 0 if a kmalloc cache for that
499  * size does not exist
500  */
501 static __always_inline int kmalloc_size(int n)
502 {
503 #ifndef CONFIG_SLOB
504         if (n > 2)
505                 return 1 << n;
506
507         if (n == 1 && KMALLOC_MIN_SIZE <= 32)
508                 return 96;
509
510         if (n == 2 && KMALLOC_MIN_SIZE <= 64)
511                 return 192;
512 #endif
513         return 0;
514 }
515
516 static __always_inline void *kmalloc_node(size_t size, gfp_t flags, int node)
517 {
518 #ifndef CONFIG_SLOB
519         if (__builtin_constant_p(size) &&
520                 size <= KMALLOC_MAX_CACHE_SIZE && !(flags & GFP_DMA)) {
521                 int i = kmalloc_index(size);
522
523                 if (!i)
524                         return ZERO_SIZE_PTR;
525
526                 return kmem_cache_alloc_node_trace(kmalloc_caches[i],
527                                                 flags, node, size);
528         }
529 #endif
530         return __kmalloc_node(size, flags, node);
531 }
532
533 struct memcg_cache_array {
534         struct rcu_head rcu;
535         struct kmem_cache *entries[0];
536 };
537
538 /*
539  * This is the main placeholder for memcg-related information in kmem caches.
540  * Both the root cache and the child caches will have it. For the root cache,
541  * this will hold a dynamically allocated array large enough to hold
542  * information about the currently limited memcgs in the system. To allow the
543  * array to be accessed without taking any locks, on relocation we free the old
544  * version only after a grace period.
545  *
546  * Root and child caches hold different metadata.
547  *
548  * @root_cache: Common to root and child caches.  NULL for root, pointer to
549  *              the root cache for children.
550  *
551  * The following fields are specific to root caches.
552  *
553  * @memcg_caches: kmemcg ID indexed table of child caches.  This table is
554  *              used to index child cachces during allocation and cleared
555  *              early during shutdown.
556  *
557  * @root_caches_node: List node for slab_root_caches list.
558  *
559  * @children:   List of all child caches.  While the child caches are also
560  *              reachable through @memcg_caches, a child cache remains on
561  *              this list until it is actually destroyed.
562  *
563  * The following fields are specific to child caches.
564  *
565  * @memcg:      Pointer to the memcg this cache belongs to.
566  *
567  * @children_node: List node for @root_cache->children list.
568  *
569  * @kmem_caches_node: List node for @memcg->kmem_caches list.
570  */
571 struct memcg_cache_params {
572         struct kmem_cache *root_cache;
573         union {
574                 struct {
575                         struct memcg_cache_array __rcu *memcg_caches;
576                         struct list_head __root_caches_node;
577                         struct list_head children;
578                 };
579                 struct {
580                         struct mem_cgroup *memcg;
581                         struct list_head children_node;
582                         struct list_head kmem_caches_node;
583
584                         void (*deact_fn)(struct kmem_cache *);
585                         union {
586                                 struct rcu_head deact_rcu_head;
587                                 struct work_struct deact_work;
588                         };
589                 };
590         };
591 };
592
593 int memcg_update_all_caches(int num_memcgs);
594
595 /**
596  * kmalloc_array - allocate memory for an array.
597  * @n: number of elements.
598  * @size: element size.
599  * @flags: the type of memory to allocate (see kmalloc).
600  */
601 static inline void *kmalloc_array(size_t n, size_t size, gfp_t flags)
602 {
603         if (size != 0 && n > SIZE_MAX / size)
604                 return NULL;
605         if (__builtin_constant_p(n) && __builtin_constant_p(size))
606                 return kmalloc(n * size, flags);
607         return __kmalloc(n * size, flags);
608 }
609
610 /**
611  * kcalloc - allocate memory for an array. The memory is set to zero.
612  * @n: number of elements.
613  * @size: element size.
614  * @flags: the type of memory to allocate (see kmalloc).
615  */
616 static inline void *kcalloc(size_t n, size_t size, gfp_t flags)
617 {
618         return kmalloc_array(n, size, flags | __GFP_ZERO);
619 }
620
621 /*
622  * kmalloc_track_caller is a special version of kmalloc that records the
623  * calling function of the routine calling it for slab leak tracking instead
624  * of just the calling function (confusing, eh?).
625  * It's useful when the call to kmalloc comes from a widely-used standard
626  * allocator where we care about the real place the memory allocation
627  * request comes from.
628  */
629 extern void *__kmalloc_track_caller(size_t, gfp_t, unsigned long);
630 #define kmalloc_track_caller(size, flags) \
631         __kmalloc_track_caller(size, flags, _RET_IP_)
632
633 #ifdef CONFIG_NUMA
634 extern void *__kmalloc_node_track_caller(size_t, gfp_t, int, unsigned long);
635 #define kmalloc_node_track_caller(size, flags, node) \
636         __kmalloc_node_track_caller(size, flags, node, \
637                         _RET_IP_)
638
639 #else /* CONFIG_NUMA */
640
641 #define kmalloc_node_track_caller(size, flags, node) \
642         kmalloc_track_caller(size, flags)
643
644 #endif /* CONFIG_NUMA */
645
646 /*
647  * Shortcuts
648  */
649 static inline void *kmem_cache_zalloc(struct kmem_cache *k, gfp_t flags)
650 {
651         return kmem_cache_alloc(k, flags | __GFP_ZERO);
652 }
653
654 /**
655  * kzalloc - allocate memory. The memory is set to zero.
656  * @size: how many bytes of memory are required.
657  * @flags: the type of memory to allocate (see kmalloc).
658  */
659 static inline void *kzalloc(size_t size, gfp_t flags)
660 {
661         return kmalloc(size, flags | __GFP_ZERO);
662 }
663
664 /**
665  * kzalloc_node - allocate zeroed memory from a particular memory node.
666  * @size: how many bytes of memory are required.
667  * @flags: the type of memory to allocate (see kmalloc).
668  * @node: memory node from which to allocate
669  */
670 static inline void *kzalloc_node(size_t size, gfp_t flags, int node)
671 {
672         return kmalloc_node(size, flags | __GFP_ZERO, node);
673 }
674
675 unsigned int kmem_cache_size(struct kmem_cache *s);
676 void __init kmem_cache_init_late(void);
677
678 #if defined(CONFIG_SMP) && defined(CONFIG_SLAB)
679 int slab_prepare_cpu(unsigned int cpu);
680 int slab_dead_cpu(unsigned int cpu);
681 #else
682 #define slab_prepare_cpu        NULL
683 #define slab_dead_cpu           NULL
684 #endif
685
686 #endif  /* _LINUX_SLAB_H */