slab.h 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. /*
  2. * linux/include/linux/slab.h
  3. * Written by Mark Hemment, 1996.
  4. * (markhe@nextd.demon.co.uk)
  5. */
  6. #ifndef _LINUX_SLAB_H
  7. #define _LINUX_SLAB_H
  8. #if defined(__KERNEL__)
  9. /* kmem_cache_t exists for legacy reasons and is not used by code in mm */
  10. typedef struct kmem_cache kmem_cache_t;
  11. #include <linux/gfp.h>
  12. #include <linux/init.h>
  13. #include <linux/types.h>
  14. #include <asm/page.h> /* kmalloc_sizes.h needs PAGE_SIZE */
  15. #include <asm/cache.h> /* kmalloc_sizes.h needs L1_CACHE_BYTES */
  16. /* flags for kmem_cache_alloc() */
  17. #define SLAB_KERNEL GFP_KERNEL
  18. #define SLAB_DMA GFP_DMA
  19. /* flags to pass to kmem_cache_create().
  20. * The first 3 are only valid when the allocator as been build
  21. * SLAB_DEBUG_SUPPORT.
  22. */
  23. #define SLAB_DEBUG_FREE 0x00000100UL /* Peform (expensive) checks on free */
  24. #define SLAB_DEBUG_INITIAL 0x00000200UL /* Call constructor (as verifier) */
  25. #define SLAB_RED_ZONE 0x00000400UL /* Red zone objs in a cache */
  26. #define SLAB_POISON 0x00000800UL /* Poison objects */
  27. #define SLAB_HWCACHE_ALIGN 0x00002000UL /* align objs on a h/w cache lines */
  28. #define SLAB_CACHE_DMA 0x00004000UL /* use GFP_DMA memory */
  29. #define SLAB_MUST_HWCACHE_ALIGN 0x00008000UL /* force alignment */
  30. #define SLAB_STORE_USER 0x00010000UL /* store the last owner for bug hunting */
  31. #define SLAB_RECLAIM_ACCOUNT 0x00020000UL /* track pages allocated to indicate
  32. what is reclaimable later*/
  33. #define SLAB_PANIC 0x00040000UL /* panic if kmem_cache_create() fails */
  34. #define SLAB_DESTROY_BY_RCU 0x00080000UL /* defer freeing pages to RCU */
  35. #define SLAB_MEM_SPREAD 0x00100000UL /* Spread some memory over cpuset */
  36. /* flags passed to a constructor func */
  37. #define SLAB_CTOR_CONSTRUCTOR 0x001UL /* if not set, then deconstructor */
  38. #define SLAB_CTOR_ATOMIC 0x002UL /* tell constructor it can't sleep */
  39. #define SLAB_CTOR_VERIFY 0x004UL /* tell constructor it's a verify call */
  40. #ifndef CONFIG_SLOB
  41. /* prototypes */
  42. extern void __init kmem_cache_init(void);
  43. extern struct kmem_cache *kmem_cache_create(const char *, size_t, size_t,
  44. unsigned long,
  45. void (*)(void *, struct kmem_cache *, unsigned long),
  46. void (*)(void *, struct kmem_cache *, unsigned long));
  47. extern void kmem_cache_destroy(struct kmem_cache *);
  48. extern int kmem_cache_shrink(struct kmem_cache *);
  49. extern void *kmem_cache_alloc(struct kmem_cache *, gfp_t);
  50. extern void *kmem_cache_zalloc(struct kmem_cache *, gfp_t);
  51. extern void kmem_cache_free(struct kmem_cache *, void *);
  52. extern unsigned int kmem_cache_size(struct kmem_cache *);
  53. extern const char *kmem_cache_name(struct kmem_cache *);
  54. /* Size description struct for general caches. */
  55. struct cache_sizes {
  56. size_t cs_size;
  57. struct kmem_cache *cs_cachep;
  58. struct kmem_cache *cs_dmacachep;
  59. };
  60. extern struct cache_sizes malloc_sizes[];
  61. extern void *__kmalloc(size_t, gfp_t);
  62. /**
  63. * kmalloc - allocate memory
  64. * @size: how many bytes of memory are required.
  65. * @flags: the type of memory to allocate.
  66. *
  67. * kmalloc is the normal method of allocating memory
  68. * in the kernel.
  69. *
  70. * The @flags argument may be one of:
  71. *
  72. * %GFP_USER - Allocate memory on behalf of user. May sleep.
  73. *
  74. * %GFP_KERNEL - Allocate normal kernel ram. May sleep.
  75. *
  76. * %GFP_ATOMIC - Allocation will not sleep.
  77. * For example, use this inside interrupt handlers.
  78. *
  79. * %GFP_HIGHUSER - Allocate pages from high memory.
  80. *
  81. * %GFP_NOIO - Do not do any I/O at all while trying to get memory.
  82. *
  83. * %GFP_NOFS - Do not make any fs calls while trying to get memory.
  84. *
  85. * Also it is possible to set different flags by OR'ing
  86. * in one or more of the following additional @flags:
  87. *
  88. * %__GFP_COLD - Request cache-cold pages instead of
  89. * trying to return cache-warm pages.
  90. *
  91. * %__GFP_DMA - Request memory from the DMA-capable zone.
  92. *
  93. * %__GFP_HIGH - This allocation has high priority and may use emergency pools.
  94. *
  95. * %__GFP_HIGHMEM - Allocated memory may be from highmem.
  96. *
  97. * %__GFP_NOFAIL - Indicate that this allocation is in no way allowed to fail
  98. * (think twice before using).
  99. *
  100. * %__GFP_NORETRY - If memory is not immediately available,
  101. * then give up at once.
  102. *
  103. * %__GFP_NOWARN - If allocation fails, don't issue any warnings.
  104. *
  105. * %__GFP_REPEAT - If allocation fails initially, try once more before failing.
  106. */
  107. static inline void *kmalloc(size_t size, gfp_t flags)
  108. {
  109. if (__builtin_constant_p(size)) {
  110. int i = 0;
  111. #define CACHE(x) \
  112. if (size <= x) \
  113. goto found; \
  114. else \
  115. i++;
  116. #include "kmalloc_sizes.h"
  117. #undef CACHE
  118. {
  119. extern void __you_cannot_kmalloc_that_much(void);
  120. __you_cannot_kmalloc_that_much();
  121. }
  122. found:
  123. return kmem_cache_alloc((flags & GFP_DMA) ?
  124. malloc_sizes[i].cs_dmacachep :
  125. malloc_sizes[i].cs_cachep, flags);
  126. }
  127. return __kmalloc(size, flags);
  128. }
  129. /*
  130. * kmalloc_track_caller is a special version of kmalloc that records the
  131. * calling function of the routine calling it for slab leak tracking instead
  132. * of just the calling function (confusing, eh?).
  133. * It's useful when the call to kmalloc comes from a widely-used standard
  134. * allocator where we care about the real place the memory allocation
  135. * request comes from.
  136. */
  137. #ifndef CONFIG_DEBUG_SLAB
  138. #define kmalloc_track_caller(size, flags) \
  139. __kmalloc(size, flags)
  140. #else
  141. extern void *__kmalloc_track_caller(size_t, gfp_t, void*);
  142. #define kmalloc_track_caller(size, flags) \
  143. __kmalloc_track_caller(size, flags, __builtin_return_address(0))
  144. #endif
  145. extern void *__kzalloc(size_t, gfp_t);
  146. /**
  147. * kzalloc - allocate memory. The memory is set to zero.
  148. * @size: how many bytes of memory are required.
  149. * @flags: the type of memory to allocate (see kmalloc).
  150. */
  151. static inline void *kzalloc(size_t size, gfp_t flags)
  152. {
  153. if (__builtin_constant_p(size)) {
  154. int i = 0;
  155. #define CACHE(x) \
  156. if (size <= x) \
  157. goto found; \
  158. else \
  159. i++;
  160. #include "kmalloc_sizes.h"
  161. #undef CACHE
  162. {
  163. extern void __you_cannot_kzalloc_that_much(void);
  164. __you_cannot_kzalloc_that_much();
  165. }
  166. found:
  167. return kmem_cache_zalloc((flags & GFP_DMA) ?
  168. malloc_sizes[i].cs_dmacachep :
  169. malloc_sizes[i].cs_cachep, flags);
  170. }
  171. return __kzalloc(size, flags);
  172. }
  173. /**
  174. * kcalloc - allocate memory for an array. The memory is set to zero.
  175. * @n: number of elements.
  176. * @size: element size.
  177. * @flags: the type of memory to allocate.
  178. */
  179. static inline void *kcalloc(size_t n, size_t size, gfp_t flags)
  180. {
  181. if (n != 0 && size > ULONG_MAX / n)
  182. return NULL;
  183. return kzalloc(n * size, flags);
  184. }
  185. extern void kfree(const void *);
  186. extern unsigned int ksize(const void *);
  187. extern int slab_is_available(void);
  188. #ifdef CONFIG_NUMA
  189. extern void *kmem_cache_alloc_node(struct kmem_cache *, gfp_t flags, int node);
  190. extern void *__kmalloc_node(size_t size, gfp_t flags, int node);
  191. static inline void *kmalloc_node(size_t size, gfp_t flags, int node)
  192. {
  193. if (__builtin_constant_p(size)) {
  194. int i = 0;
  195. #define CACHE(x) \
  196. if (size <= x) \
  197. goto found; \
  198. else \
  199. i++;
  200. #include "kmalloc_sizes.h"
  201. #undef CACHE
  202. {
  203. extern void __you_cannot_kmalloc_that_much(void);
  204. __you_cannot_kmalloc_that_much();
  205. }
  206. found:
  207. return kmem_cache_alloc_node((flags & GFP_DMA) ?
  208. malloc_sizes[i].cs_dmacachep :
  209. malloc_sizes[i].cs_cachep, flags, node);
  210. }
  211. return __kmalloc_node(size, flags, node);
  212. }
  213. /*
  214. * kmalloc_node_track_caller is a special version of kmalloc_node that
  215. * records the calling function of the routine calling it for slab leak
  216. * tracking instead of just the calling function (confusing, eh?).
  217. * It's useful when the call to kmalloc_node comes from a widely-used
  218. * standard allocator where we care about the real place the memory
  219. * allocation request comes from.
  220. */
  221. #ifndef CONFIG_DEBUG_SLAB
  222. #define kmalloc_node_track_caller(size, flags, node) \
  223. __kmalloc_node(size, flags, node)
  224. #else
  225. extern void *__kmalloc_node_track_caller(size_t, gfp_t, int, void *);
  226. #define kmalloc_node_track_caller(size, flags, node) \
  227. __kmalloc_node_track_caller(size, flags, node, \
  228. __builtin_return_address(0))
  229. #endif
  230. #else /* CONFIG_NUMA */
  231. static inline void *kmem_cache_alloc_node(struct kmem_cache *cachep,
  232. gfp_t flags, int node)
  233. {
  234. return kmem_cache_alloc(cachep, flags);
  235. }
  236. static inline void *kmalloc_node(size_t size, gfp_t flags, int node)
  237. {
  238. return kmalloc(size, flags);
  239. }
  240. #define kmalloc_node_track_caller(size, flags, node) \
  241. kmalloc_track_caller(size, flags)
  242. #endif
  243. extern int FASTCALL(kmem_cache_reap(int));
  244. extern int FASTCALL(kmem_ptr_validate(struct kmem_cache *cachep, void *ptr));
  245. #else /* CONFIG_SLOB */
  246. /* SLOB allocator routines */
  247. void kmem_cache_init(void);
  248. struct kmem_cache *kmem_cache_create(const char *c, size_t, size_t,
  249. unsigned long,
  250. void (*)(void *, struct kmem_cache *, unsigned long),
  251. void (*)(void *, struct kmem_cache *, unsigned long));
  252. void kmem_cache_destroy(struct kmem_cache *c);
  253. void *kmem_cache_alloc(struct kmem_cache *c, gfp_t flags);
  254. void *kmem_cache_zalloc(struct kmem_cache *, gfp_t);
  255. void kmem_cache_free(struct kmem_cache *c, void *b);
  256. const char *kmem_cache_name(struct kmem_cache *);
  257. void *kmalloc(size_t size, gfp_t flags);
  258. void *__kzalloc(size_t size, gfp_t flags);
  259. void kfree(const void *m);
  260. unsigned int ksize(const void *m);
  261. unsigned int kmem_cache_size(struct kmem_cache *c);
  262. static inline void *kcalloc(size_t n, size_t size, gfp_t flags)
  263. {
  264. return __kzalloc(n * size, flags);
  265. }
  266. #define kmem_cache_shrink(d) (0)
  267. #define kmem_cache_reap(a)
  268. #define kmem_ptr_validate(a, b) (0)
  269. #define kmem_cache_alloc_node(c, f, n) kmem_cache_alloc(c, f)
  270. #define kmalloc_node(s, f, n) kmalloc(s, f)
  271. #define kzalloc(s, f) __kzalloc(s, f)
  272. #define kmalloc_track_caller kmalloc
  273. #define kmalloc_node_track_caller kmalloc_node
  274. #endif /* CONFIG_SLOB */
  275. #endif /* __KERNEL__ */
  276. #endif /* _LINUX_SLAB_H */