slab.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444
  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. #include <linux/workqueue.h>
  13. /*
  14. * Flags to pass to kmem_cache_create().
  15. * The ones marked DEBUG are only valid if CONFIG_SLAB_DEBUG is set.
  16. */
  17. #define SLAB_DEBUG_FREE 0x00000100UL /* DEBUG: Perform (expensive) checks on free */
  18. #define SLAB_RED_ZONE 0x00000400UL /* DEBUG: Red zone objs in a cache */
  19. #define SLAB_POISON 0x00000800UL /* DEBUG: Poison objects */
  20. #define SLAB_HWCACHE_ALIGN 0x00002000UL /* Align objs on cache lines */
  21. #define SLAB_CACHE_DMA 0x00004000UL /* Use GFP_DMA memory */
  22. #define SLAB_STORE_USER 0x00010000UL /* DEBUG: Store the last owner for bug hunting */
  23. #define SLAB_PANIC 0x00040000UL /* Panic if kmem_cache_create() fails */
  24. /*
  25. * SLAB_DESTROY_BY_RCU - **WARNING** READ THIS!
  26. *
  27. * This delays freeing the SLAB page by a grace period, it does _NOT_
  28. * delay object freeing. This means that if you do kmem_cache_free()
  29. * that memory location is free to be reused at any time. Thus it may
  30. * be possible to see another object there in the same RCU grace period.
  31. *
  32. * This feature only ensures the memory location backing the object
  33. * stays valid, the trick to using this is relying on an independent
  34. * object validation pass. Something like:
  35. *
  36. * rcu_read_lock()
  37. * again:
  38. * obj = lockless_lookup(key);
  39. * if (obj) {
  40. * if (!try_get_ref(obj)) // might fail for free objects
  41. * goto again;
  42. *
  43. * if (obj->key != key) { // not the object we expected
  44. * put_ref(obj);
  45. * goto again;
  46. * }
  47. * }
  48. * rcu_read_unlock();
  49. *
  50. * See also the comment on struct slab_rcu in mm/slab.c.
  51. */
  52. #define SLAB_DESTROY_BY_RCU 0x00080000UL /* Defer freeing slabs to RCU */
  53. #define SLAB_MEM_SPREAD 0x00100000UL /* Spread some memory over cpuset */
  54. #define SLAB_TRACE 0x00200000UL /* Trace allocations and frees */
  55. /* Flag to prevent checks on free */
  56. #ifdef CONFIG_DEBUG_OBJECTS
  57. # define SLAB_DEBUG_OBJECTS 0x00400000UL
  58. #else
  59. # define SLAB_DEBUG_OBJECTS 0x00000000UL
  60. #endif
  61. #define SLAB_NOLEAKTRACE 0x00800000UL /* Avoid kmemleak tracing */
  62. /* Don't track use of uninitialized memory */
  63. #ifdef CONFIG_KMEMCHECK
  64. # define SLAB_NOTRACK 0x01000000UL
  65. #else
  66. # define SLAB_NOTRACK 0x00000000UL
  67. #endif
  68. #ifdef CONFIG_FAILSLAB
  69. # define SLAB_FAILSLAB 0x02000000UL /* Fault injection mark */
  70. #else
  71. # define SLAB_FAILSLAB 0x00000000UL
  72. #endif
  73. /* The following flags affect the page allocator grouping pages by mobility */
  74. #define SLAB_RECLAIM_ACCOUNT 0x00020000UL /* Objects are reclaimable */
  75. #define SLAB_TEMPORARY SLAB_RECLAIM_ACCOUNT /* Objects are short-lived */
  76. /*
  77. * ZERO_SIZE_PTR will be returned for zero sized kmalloc requests.
  78. *
  79. * Dereferencing ZERO_SIZE_PTR will lead to a distinct access fault.
  80. *
  81. * ZERO_SIZE_PTR can be passed to kfree though in the same way that NULL can.
  82. * Both make kfree a no-op.
  83. */
  84. #define ZERO_SIZE_PTR ((void *)16)
  85. #define ZERO_OR_NULL_PTR(x) ((unsigned long)(x) <= \
  86. (unsigned long)ZERO_SIZE_PTR)
  87. /*
  88. * Common fields provided in kmem_cache by all slab allocators
  89. * This struct is either used directly by the allocator (SLOB)
  90. * or the allocator must include definitions for all fields
  91. * provided in kmem_cache_common in their definition of kmem_cache.
  92. *
  93. * Once we can do anonymous structs (C11 standard) we could put a
  94. * anonymous struct definition in these allocators so that the
  95. * separate allocations in the kmem_cache structure of SLAB and
  96. * SLUB is no longer needed.
  97. */
  98. #ifdef CONFIG_SLOB
  99. struct kmem_cache {
  100. unsigned int object_size;/* The original size of the object */
  101. unsigned int size; /* The aligned/padded/added on size */
  102. unsigned int align; /* Alignment as calculated */
  103. unsigned long flags; /* Active flags on the slab */
  104. const char *name; /* Slab name for sysfs */
  105. int refcount; /* Use counter */
  106. void (*ctor)(void *); /* Called on object slot creation */
  107. struct list_head list; /* List of all slab caches on the system */
  108. };
  109. #endif
  110. struct mem_cgroup;
  111. /*
  112. * struct kmem_cache related prototypes
  113. */
  114. void __init kmem_cache_init(void);
  115. int slab_is_available(void);
  116. struct kmem_cache *kmem_cache_create(const char *, size_t, size_t,
  117. unsigned long,
  118. void (*)(void *));
  119. struct kmem_cache *
  120. kmem_cache_create_memcg(struct mem_cgroup *, const char *, size_t, size_t,
  121. unsigned long, void (*)(void *));
  122. void kmem_cache_destroy(struct kmem_cache *);
  123. int kmem_cache_shrink(struct kmem_cache *);
  124. void kmem_cache_free(struct kmem_cache *, void *);
  125. /*
  126. * Please use this macro to create slab caches. Simply specify the
  127. * name of the structure and maybe some flags that are listed above.
  128. *
  129. * The alignment of the struct determines object alignment. If you
  130. * f.e. add ____cacheline_aligned_in_smp to the struct declaration
  131. * then the objects will be properly aligned in SMP configurations.
  132. */
  133. #define KMEM_CACHE(__struct, __flags) kmem_cache_create(#__struct,\
  134. sizeof(struct __struct), __alignof__(struct __struct),\
  135. (__flags), NULL)
  136. /*
  137. * The largest kmalloc size supported by the slab allocators is
  138. * 32 megabyte (2^25) or the maximum allocatable page order if that is
  139. * less than 32 MB.
  140. *
  141. * WARNING: Its not easy to increase this value since the allocators have
  142. * to do various tricks to work around compiler limitations in order to
  143. * ensure proper constant folding.
  144. */
  145. #define KMALLOC_SHIFT_HIGH ((MAX_ORDER + PAGE_SHIFT - 1) <= 25 ? \
  146. (MAX_ORDER + PAGE_SHIFT - 1) : 25)
  147. #define KMALLOC_MAX_SIZE (1UL << KMALLOC_SHIFT_HIGH)
  148. #define KMALLOC_MAX_ORDER (KMALLOC_SHIFT_HIGH - PAGE_SHIFT)
  149. /*
  150. * Some archs want to perform DMA into kmalloc caches and need a guaranteed
  151. * alignment larger than the alignment of a 64-bit integer.
  152. * Setting ARCH_KMALLOC_MINALIGN in arch headers allows that.
  153. */
  154. #ifdef ARCH_DMA_MINALIGN
  155. #define ARCH_KMALLOC_MINALIGN ARCH_DMA_MINALIGN
  156. #else
  157. #define ARCH_KMALLOC_MINALIGN __alignof__(unsigned long long)
  158. #endif
  159. /*
  160. * Setting ARCH_SLAB_MINALIGN in arch headers allows a different alignment.
  161. * Intended for arches that get misalignment faults even for 64 bit integer
  162. * aligned buffers.
  163. */
  164. #ifndef ARCH_SLAB_MINALIGN
  165. #define ARCH_SLAB_MINALIGN __alignof__(unsigned long long)
  166. #endif
  167. /*
  168. * This is the main placeholder for memcg-related information in kmem caches.
  169. * struct kmem_cache will hold a pointer to it, so the memory cost while
  170. * disabled is 1 pointer. The runtime cost while enabled, gets bigger than it
  171. * would otherwise be if that would be bundled in kmem_cache: we'll need an
  172. * extra pointer chase. But the trade off clearly lays in favor of not
  173. * penalizing non-users.
  174. *
  175. * Both the root cache and the child caches will have it. For the root cache,
  176. * this will hold a dynamically allocated array large enough to hold
  177. * information about the currently limited memcgs in the system.
  178. *
  179. * Child caches will hold extra metadata needed for its operation. Fields are:
  180. *
  181. * @memcg: pointer to the memcg this cache belongs to
  182. * @list: list_head for the list of all caches in this memcg
  183. * @root_cache: pointer to the global, root cache, this cache was derived from
  184. * @dead: set to true after the memcg dies; the cache may still be around.
  185. * @nr_pages: number of pages that belongs to this cache.
  186. * @destroy: worker to be called whenever we are ready, or believe we may be
  187. * ready, to destroy this cache.
  188. */
  189. struct memcg_cache_params {
  190. bool is_root_cache;
  191. union {
  192. struct kmem_cache *memcg_caches[0];
  193. struct {
  194. struct mem_cgroup *memcg;
  195. struct list_head list;
  196. struct kmem_cache *root_cache;
  197. bool dead;
  198. atomic_t nr_pages;
  199. struct work_struct destroy;
  200. };
  201. };
  202. };
  203. int memcg_update_all_caches(int num_memcgs);
  204. /*
  205. * Common kmalloc functions provided by all allocators
  206. */
  207. void * __must_check __krealloc(const void *, size_t, gfp_t);
  208. void * __must_check krealloc(const void *, size_t, gfp_t);
  209. void kfree(const void *);
  210. void kzfree(const void *);
  211. size_t ksize(const void *);
  212. /*
  213. * Allocator specific definitions. These are mainly used to establish optimized
  214. * ways to convert kmalloc() calls to kmem_cache_alloc() invocations by
  215. * selecting the appropriate general cache at compile time.
  216. *
  217. * Allocators must define at least:
  218. *
  219. * kmem_cache_alloc()
  220. * __kmalloc()
  221. * kmalloc()
  222. *
  223. * Those wishing to support NUMA must also define:
  224. *
  225. * kmem_cache_alloc_node()
  226. * kmalloc_node()
  227. *
  228. * See each allocator definition file for additional comments and
  229. * implementation notes.
  230. */
  231. #ifdef CONFIG_SLUB
  232. #include <linux/slub_def.h>
  233. #elif defined(CONFIG_SLOB)
  234. #include <linux/slob_def.h>
  235. #else
  236. #include <linux/slab_def.h>
  237. #endif
  238. /**
  239. * kmalloc_array - allocate memory for an array.
  240. * @n: number of elements.
  241. * @size: element size.
  242. * @flags: the type of memory to allocate.
  243. *
  244. * The @flags argument may be one of:
  245. *
  246. * %GFP_USER - Allocate memory on behalf of user. May sleep.
  247. *
  248. * %GFP_KERNEL - Allocate normal kernel ram. May sleep.
  249. *
  250. * %GFP_ATOMIC - Allocation will not sleep. May use emergency pools.
  251. * For example, use this inside interrupt handlers.
  252. *
  253. * %GFP_HIGHUSER - Allocate pages from high memory.
  254. *
  255. * %GFP_NOIO - Do not do any I/O at all while trying to get memory.
  256. *
  257. * %GFP_NOFS - Do not make any fs calls while trying to get memory.
  258. *
  259. * %GFP_NOWAIT - Allocation will not sleep.
  260. *
  261. * %GFP_THISNODE - Allocate node-local memory only.
  262. *
  263. * %GFP_DMA - Allocation suitable for DMA.
  264. * Should only be used for kmalloc() caches. Otherwise, use a
  265. * slab created with SLAB_DMA.
  266. *
  267. * Also it is possible to set different flags by OR'ing
  268. * in one or more of the following additional @flags:
  269. *
  270. * %__GFP_COLD - Request cache-cold pages instead of
  271. * trying to return cache-warm pages.
  272. *
  273. * %__GFP_HIGH - This allocation has high priority and may use emergency pools.
  274. *
  275. * %__GFP_NOFAIL - Indicate that this allocation is in no way allowed to fail
  276. * (think twice before using).
  277. *
  278. * %__GFP_NORETRY - If memory is not immediately available,
  279. * then give up at once.
  280. *
  281. * %__GFP_NOWARN - If allocation fails, don't issue any warnings.
  282. *
  283. * %__GFP_REPEAT - If allocation fails initially, try once more before failing.
  284. *
  285. * There are other flags available as well, but these are not intended
  286. * for general use, and so are not documented here. For a full list of
  287. * potential flags, always refer to linux/gfp.h.
  288. */
  289. static inline void *kmalloc_array(size_t n, size_t size, gfp_t flags)
  290. {
  291. if (size != 0 && n > SIZE_MAX / size)
  292. return NULL;
  293. return __kmalloc(n * size, flags);
  294. }
  295. /**
  296. * kcalloc - allocate memory for an array. The memory is set to zero.
  297. * @n: number of elements.
  298. * @size: element size.
  299. * @flags: the type of memory to allocate (see kmalloc).
  300. */
  301. static inline void *kcalloc(size_t n, size_t size, gfp_t flags)
  302. {
  303. return kmalloc_array(n, size, flags | __GFP_ZERO);
  304. }
  305. #if !defined(CONFIG_NUMA) && !defined(CONFIG_SLOB)
  306. /**
  307. * kmalloc_node - allocate memory from a specific node
  308. * @size: how many bytes of memory are required.
  309. * @flags: the type of memory to allocate (see kcalloc).
  310. * @node: node to allocate from.
  311. *
  312. * kmalloc() for non-local nodes, used to allocate from a specific node
  313. * if available. Equivalent to kmalloc() in the non-NUMA single-node
  314. * case.
  315. */
  316. static inline void *kmalloc_node(size_t size, gfp_t flags, int node)
  317. {
  318. return kmalloc(size, flags);
  319. }
  320. static inline void *__kmalloc_node(size_t size, gfp_t flags, int node)
  321. {
  322. return __kmalloc(size, flags);
  323. }
  324. void *kmem_cache_alloc(struct kmem_cache *, gfp_t);
  325. static inline void *kmem_cache_alloc_node(struct kmem_cache *cachep,
  326. gfp_t flags, int node)
  327. {
  328. return kmem_cache_alloc(cachep, flags);
  329. }
  330. #endif /* !CONFIG_NUMA && !CONFIG_SLOB */
  331. /*
  332. * kmalloc_track_caller is a special version of kmalloc that records the
  333. * calling function of the routine calling it for slab leak tracking instead
  334. * of just the calling function (confusing, eh?).
  335. * It's useful when the call to kmalloc comes from a widely-used standard
  336. * allocator where we care about the real place the memory allocation
  337. * request comes from.
  338. */
  339. #if defined(CONFIG_DEBUG_SLAB) || defined(CONFIG_SLUB) || \
  340. (defined(CONFIG_SLAB) && defined(CONFIG_TRACING)) || \
  341. (defined(CONFIG_SLOB) && defined(CONFIG_TRACING))
  342. extern void *__kmalloc_track_caller(size_t, gfp_t, unsigned long);
  343. #define kmalloc_track_caller(size, flags) \
  344. __kmalloc_track_caller(size, flags, _RET_IP_)
  345. #else
  346. #define kmalloc_track_caller(size, flags) \
  347. __kmalloc(size, flags)
  348. #endif /* DEBUG_SLAB */
  349. #ifdef CONFIG_NUMA
  350. /*
  351. * kmalloc_node_track_caller is a special version of kmalloc_node that
  352. * records the calling function of the routine calling it for slab leak
  353. * tracking instead of just the calling function (confusing, eh?).
  354. * It's useful when the call to kmalloc_node comes from a widely-used
  355. * standard allocator where we care about the real place the memory
  356. * allocation request comes from.
  357. */
  358. #if defined(CONFIG_DEBUG_SLAB) || defined(CONFIG_SLUB) || \
  359. (defined(CONFIG_SLAB) && defined(CONFIG_TRACING)) || \
  360. (defined(CONFIG_SLOB) && defined(CONFIG_TRACING))
  361. extern void *__kmalloc_node_track_caller(size_t, gfp_t, int, unsigned long);
  362. #define kmalloc_node_track_caller(size, flags, node) \
  363. __kmalloc_node_track_caller(size, flags, node, \
  364. _RET_IP_)
  365. #else
  366. #define kmalloc_node_track_caller(size, flags, node) \
  367. __kmalloc_node(size, flags, node)
  368. #endif
  369. #else /* CONFIG_NUMA */
  370. #define kmalloc_node_track_caller(size, flags, node) \
  371. kmalloc_track_caller(size, flags)
  372. #endif /* CONFIG_NUMA */
  373. /*
  374. * Shortcuts
  375. */
  376. static inline void *kmem_cache_zalloc(struct kmem_cache *k, gfp_t flags)
  377. {
  378. return kmem_cache_alloc(k, flags | __GFP_ZERO);
  379. }
  380. /**
  381. * kzalloc - allocate memory. The memory is set to zero.
  382. * @size: how many bytes of memory are required.
  383. * @flags: the type of memory to allocate (see kmalloc).
  384. */
  385. static inline void *kzalloc(size_t size, gfp_t flags)
  386. {
  387. return kmalloc(size, flags | __GFP_ZERO);
  388. }
  389. /**
  390. * kzalloc_node - allocate zeroed memory from a particular memory node.
  391. * @size: how many bytes of memory are required.
  392. * @flags: the type of memory to allocate (see kmalloc).
  393. * @node: memory node from which to allocate
  394. */
  395. static inline void *kzalloc_node(size_t size, gfp_t flags, int node)
  396. {
  397. return kmalloc_node(size, flags | __GFP_ZERO, node);
  398. }
  399. /*
  400. * Determine the size of a slab object
  401. */
  402. static inline unsigned int kmem_cache_size(struct kmem_cache *s)
  403. {
  404. return s->object_size;
  405. }
  406. void __init kmem_cache_init_late(void);
  407. #endif /* _LINUX_SLAB_H */