slab.h 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. /*
  2. * Written by Mark Hemment, 1996 (markhe@nextd.demon.co.uk).
  3. *
  4. * (C) SGI 2006, Christoph Lameter
  5. * Cleaned up and restructured to ease the addition of alternative
  6. * implementations of SLAB allocators.
  7. */
  8. #ifndef _LINUX_SLAB_H
  9. #define _LINUX_SLAB_H
  10. #include <linux/gfp.h>
  11. #include <linux/types.h>
  12. /*
  13. * Flags to pass to kmem_cache_create().
  14. * The ones marked DEBUG are only valid if CONFIG_SLAB_DEBUG is set.
  15. */
  16. #define SLAB_DEBUG_FREE 0x00000100UL /* DEBUG: Perform (expensive) checks on free */
  17. #define SLAB_RED_ZONE 0x00000400UL /* DEBUG: Red zone objs in a cache */
  18. #define SLAB_POISON 0x00000800UL /* DEBUG: Poison objects */
  19. #define SLAB_HWCACHE_ALIGN 0x00002000UL /* Align objs on cache lines */
  20. #define SLAB_CACHE_DMA 0x00004000UL /* Use GFP_DMA memory */
  21. #define SLAB_STORE_USER 0x00010000UL /* DEBUG: Store the last owner for bug hunting */
  22. #define SLAB_PANIC 0x00040000UL /* Panic if kmem_cache_create() fails */
  23. #define SLAB_DESTROY_BY_RCU 0x00080000UL /* Defer freeing slabs to RCU */
  24. #define SLAB_MEM_SPREAD 0x00100000UL /* Spread some memory over cpuset */
  25. #define SLAB_TRACE 0x00200000UL /* Trace allocations and frees */
  26. /* Flag to prevent checks on free */
  27. #ifdef CONFIG_DEBUG_OBJECTS
  28. # define SLAB_DEBUG_OBJECTS 0x00400000UL
  29. #else
  30. # define SLAB_DEBUG_OBJECTS 0x00000000UL
  31. #endif
  32. /* The following flags affect the page allocator grouping pages by mobility */
  33. #define SLAB_RECLAIM_ACCOUNT 0x00020000UL /* Objects are reclaimable */
  34. #define SLAB_TEMPORARY SLAB_RECLAIM_ACCOUNT /* Objects are short-lived */
  35. /*
  36. * ZERO_SIZE_PTR will be returned for zero sized kmalloc requests.
  37. *
  38. * Dereferencing ZERO_SIZE_PTR will lead to a distinct access fault.
  39. *
  40. * ZERO_SIZE_PTR can be passed to kfree though in the same way that NULL can.
  41. * Both make kfree a no-op.
  42. */
  43. #define ZERO_SIZE_PTR ((void *)16)
  44. #define ZERO_OR_NULL_PTR(x) ((unsigned long)(x) <= \
  45. (unsigned long)ZERO_SIZE_PTR)
  46. /*
  47. * struct kmem_cache related prototypes
  48. */
  49. void __init kmem_cache_init(void);
  50. int slab_is_available(void);
  51. struct kmem_cache *kmem_cache_create(const char *, size_t, size_t,
  52. unsigned long,
  53. void (*)(void *));
  54. void kmem_cache_destroy(struct kmem_cache *);
  55. int kmem_cache_shrink(struct kmem_cache *);
  56. void kmem_cache_free(struct kmem_cache *, void *);
  57. unsigned int kmem_cache_size(struct kmem_cache *);
  58. const char *kmem_cache_name(struct kmem_cache *);
  59. int kmem_ptr_validate(struct kmem_cache *cachep, const void *ptr);
  60. /*
  61. * Please use this macro to create slab caches. Simply specify the
  62. * name of the structure and maybe some flags that are listed above.
  63. *
  64. * The alignment of the struct determines object alignment. If you
  65. * f.e. add ____cacheline_aligned_in_smp to the struct declaration
  66. * then the objects will be properly aligned in SMP configurations.
  67. */
  68. #define KMEM_CACHE(__struct, __flags) kmem_cache_create(#__struct,\
  69. sizeof(struct __struct), __alignof__(struct __struct),\
  70. (__flags), NULL)
  71. /*
  72. * The largest kmalloc size supported by the slab allocators is
  73. * 32 megabyte (2^25) or the maximum allocatable page order if that is
  74. * less than 32 MB.
  75. *
  76. * WARNING: Its not easy to increase this value since the allocators have
  77. * to do various tricks to work around compiler limitations in order to
  78. * ensure proper constant folding.
  79. */
  80. #define KMALLOC_SHIFT_HIGH ((MAX_ORDER + PAGE_SHIFT - 1) <= 25 ? \
  81. (MAX_ORDER + PAGE_SHIFT - 1) : 25)
  82. #define KMALLOC_MAX_SIZE (1UL << KMALLOC_SHIFT_HIGH)
  83. #define KMALLOC_MAX_ORDER (KMALLOC_SHIFT_HIGH - PAGE_SHIFT)
  84. /*
  85. * Common kmalloc functions provided by all allocators
  86. */
  87. void * __must_check __krealloc(const void *, size_t, gfp_t);
  88. void * __must_check krealloc(const void *, size_t, gfp_t);
  89. void kfree(const void *);
  90. size_t ksize(const void *);
  91. /*
  92. * Allocator specific definitions. These are mainly used to establish optimized
  93. * ways to convert kmalloc() calls to kmem_cache_alloc() invocations by
  94. * selecting the appropriate general cache at compile time.
  95. *
  96. * Allocators must define at least:
  97. *
  98. * kmem_cache_alloc()
  99. * __kmalloc()
  100. * kmalloc()
  101. *
  102. * Those wishing to support NUMA must also define:
  103. *
  104. * kmem_cache_alloc_node()
  105. * kmalloc_node()
  106. *
  107. * See each allocator definition file for additional comments and
  108. * implementation notes.
  109. */
  110. #ifdef CONFIG_SLUB
  111. #include <linux/slub_def.h>
  112. #elif defined(CONFIG_SLOB)
  113. #include <linux/slob_def.h>
  114. #else
  115. #include <linux/slab_def.h>
  116. #endif
  117. /**
  118. * kcalloc - allocate memory for an array. The memory is set to zero.
  119. * @n: number of elements.
  120. * @size: element size.
  121. * @flags: the type of memory to allocate.
  122. *
  123. * The @flags argument may be one of:
  124. *
  125. * %GFP_USER - Allocate memory on behalf of user. May sleep.
  126. *
  127. * %GFP_KERNEL - Allocate normal kernel ram. May sleep.
  128. *
  129. * %GFP_ATOMIC - Allocation will not sleep. May use emergency pools.
  130. * For example, use this inside interrupt handlers.
  131. *
  132. * %GFP_HIGHUSER - Allocate pages from high memory.
  133. *
  134. * %GFP_NOIO - Do not do any I/O at all while trying to get memory.
  135. *
  136. * %GFP_NOFS - Do not make any fs calls while trying to get memory.
  137. *
  138. * %GFP_NOWAIT - Allocation will not sleep.
  139. *
  140. * %GFP_THISNODE - Allocate node-local memory only.
  141. *
  142. * %GFP_DMA - Allocation suitable for DMA.
  143. * Should only be used for kmalloc() caches. Otherwise, use a
  144. * slab created with SLAB_DMA.
  145. *
  146. * Also it is possible to set different flags by OR'ing
  147. * in one or more of the following additional @flags:
  148. *
  149. * %__GFP_COLD - Request cache-cold pages instead of
  150. * trying to return cache-warm pages.
  151. *
  152. * %__GFP_HIGH - This allocation has high priority and may use emergency pools.
  153. *
  154. * %__GFP_NOFAIL - Indicate that this allocation is in no way allowed to fail
  155. * (think twice before using).
  156. *
  157. * %__GFP_NORETRY - If memory is not immediately available,
  158. * then give up at once.
  159. *
  160. * %__GFP_NOWARN - If allocation fails, don't issue any warnings.
  161. *
  162. * %__GFP_REPEAT - If allocation fails initially, try once more before failing.
  163. *
  164. * There are other flags available as well, but these are not intended
  165. * for general use, and so are not documented here. For a full list of
  166. * potential flags, always refer to linux/gfp.h.
  167. */
  168. static inline void *kcalloc(size_t n, size_t size, gfp_t flags)
  169. {
  170. if (size != 0 && n > ULONG_MAX / size)
  171. return NULL;
  172. return __kmalloc(n * size, flags | __GFP_ZERO);
  173. }
  174. #if !defined(CONFIG_NUMA) && !defined(CONFIG_SLOB)
  175. /**
  176. * kmalloc_node - allocate memory from a specific node
  177. * @size: how many bytes of memory are required.
  178. * @flags: the type of memory to allocate (see kcalloc).
  179. * @node: node to allocate from.
  180. *
  181. * kmalloc() for non-local nodes, used to allocate from a specific node
  182. * if available. Equivalent to kmalloc() in the non-NUMA single-node
  183. * case.
  184. */
  185. static inline void *kmalloc_node(size_t size, gfp_t flags, int node)
  186. {
  187. return kmalloc(size, flags);
  188. }
  189. static inline void *__kmalloc_node(size_t size, gfp_t flags, int node)
  190. {
  191. return __kmalloc(size, flags);
  192. }
  193. void *kmem_cache_alloc(struct kmem_cache *, gfp_t);
  194. static inline void *kmem_cache_alloc_node(struct kmem_cache *cachep,
  195. gfp_t flags, int node)
  196. {
  197. return kmem_cache_alloc(cachep, flags);
  198. }
  199. #endif /* !CONFIG_NUMA && !CONFIG_SLOB */
  200. /*
  201. * kmalloc_track_caller is a special version of kmalloc that records the
  202. * calling function of the routine calling it for slab leak tracking instead
  203. * of just the calling function (confusing, eh?).
  204. * It's useful when the call to kmalloc comes from a widely-used standard
  205. * allocator where we care about the real place the memory allocation
  206. * request comes from.
  207. */
  208. #if defined(CONFIG_DEBUG_SLAB) || defined(CONFIG_SLUB)
  209. extern void *__kmalloc_track_caller(size_t, gfp_t, void*);
  210. #define kmalloc_track_caller(size, flags) \
  211. __kmalloc_track_caller(size, flags, __builtin_return_address(0))
  212. #else
  213. #define kmalloc_track_caller(size, flags) \
  214. __kmalloc(size, flags)
  215. #endif /* DEBUG_SLAB */
  216. #ifdef CONFIG_NUMA
  217. /*
  218. * kmalloc_node_track_caller is a special version of kmalloc_node that
  219. * records the calling function of the routine calling it for slab leak
  220. * tracking instead of just the calling function (confusing, eh?).
  221. * It's useful when the call to kmalloc_node comes from a widely-used
  222. * standard allocator where we care about the real place the memory
  223. * allocation request comes from.
  224. */
  225. #if defined(CONFIG_DEBUG_SLAB) || defined(CONFIG_SLUB)
  226. extern void *__kmalloc_node_track_caller(size_t, gfp_t, int, void *);
  227. #define kmalloc_node_track_caller(size, flags, node) \
  228. __kmalloc_node_track_caller(size, flags, node, \
  229. __builtin_return_address(0))
  230. #else
  231. #define kmalloc_node_track_caller(size, flags, node) \
  232. __kmalloc_node(size, flags, node)
  233. #endif
  234. #else /* CONFIG_NUMA */
  235. #define kmalloc_node_track_caller(size, flags, node) \
  236. kmalloc_track_caller(size, flags)
  237. #endif /* DEBUG_SLAB */
  238. /*
  239. * Shortcuts
  240. */
  241. static inline void *kmem_cache_zalloc(struct kmem_cache *k, gfp_t flags)
  242. {
  243. return kmem_cache_alloc(k, flags | __GFP_ZERO);
  244. }
  245. /**
  246. * kzalloc - allocate memory. The memory is set to zero.
  247. * @size: how many bytes of memory are required.
  248. * @flags: the type of memory to allocate (see kmalloc).
  249. */
  250. static inline void *kzalloc(size_t size, gfp_t flags)
  251. {
  252. return kmalloc(size, flags | __GFP_ZERO);
  253. }
  254. /**
  255. * kzalloc_node - allocate zeroed memory from a particular memory node.
  256. * @size: how many bytes of memory are required.
  257. * @flags: the type of memory to allocate (see kmalloc).
  258. * @node: memory node from which to allocate
  259. */
  260. static inline void *kzalloc_node(size_t size, gfp_t flags, int node)
  261. {
  262. return kmalloc_node(size, flags | __GFP_ZERO, node);
  263. }
  264. #endif /* _LINUX_SLAB_H */