slab.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448
  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 *), struct kmem_cache *);
  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. struct seq_file;
  205. int cache_show(struct kmem_cache *s, struct seq_file *m);
  206. void print_slabinfo_header(struct seq_file *m);
  207. /*
  208. * Common kmalloc functions provided by all allocators
  209. */
  210. void * __must_check __krealloc(const void *, size_t, gfp_t);
  211. void * __must_check krealloc(const void *, size_t, gfp_t);
  212. void kfree(const void *);
  213. void kzfree(const void *);
  214. size_t ksize(const void *);
  215. /*
  216. * Allocator specific definitions. These are mainly used to establish optimized
  217. * ways to convert kmalloc() calls to kmem_cache_alloc() invocations by
  218. * selecting the appropriate general cache at compile time.
  219. *
  220. * Allocators must define at least:
  221. *
  222. * kmem_cache_alloc()
  223. * __kmalloc()
  224. * kmalloc()
  225. *
  226. * Those wishing to support NUMA must also define:
  227. *
  228. * kmem_cache_alloc_node()
  229. * kmalloc_node()
  230. *
  231. * See each allocator definition file for additional comments and
  232. * implementation notes.
  233. */
  234. #ifdef CONFIG_SLUB
  235. #include <linux/slub_def.h>
  236. #elif defined(CONFIG_SLOB)
  237. #include <linux/slob_def.h>
  238. #else
  239. #include <linux/slab_def.h>
  240. #endif
  241. /**
  242. * kmalloc_array - allocate memory for an array.
  243. * @n: number of elements.
  244. * @size: element size.
  245. * @flags: the type of memory to allocate.
  246. *
  247. * The @flags argument may be one of:
  248. *
  249. * %GFP_USER - Allocate memory on behalf of user. May sleep.
  250. *
  251. * %GFP_KERNEL - Allocate normal kernel ram. May sleep.
  252. *
  253. * %GFP_ATOMIC - Allocation will not sleep. May use emergency pools.
  254. * For example, use this inside interrupt handlers.
  255. *
  256. * %GFP_HIGHUSER - Allocate pages from high memory.
  257. *
  258. * %GFP_NOIO - Do not do any I/O at all while trying to get memory.
  259. *
  260. * %GFP_NOFS - Do not make any fs calls while trying to get memory.
  261. *
  262. * %GFP_NOWAIT - Allocation will not sleep.
  263. *
  264. * %GFP_THISNODE - Allocate node-local memory only.
  265. *
  266. * %GFP_DMA - Allocation suitable for DMA.
  267. * Should only be used for kmalloc() caches. Otherwise, use a
  268. * slab created with SLAB_DMA.
  269. *
  270. * Also it is possible to set different flags by OR'ing
  271. * in one or more of the following additional @flags:
  272. *
  273. * %__GFP_COLD - Request cache-cold pages instead of
  274. * trying to return cache-warm pages.
  275. *
  276. * %__GFP_HIGH - This allocation has high priority and may use emergency pools.
  277. *
  278. * %__GFP_NOFAIL - Indicate that this allocation is in no way allowed to fail
  279. * (think twice before using).
  280. *
  281. * %__GFP_NORETRY - If memory is not immediately available,
  282. * then give up at once.
  283. *
  284. * %__GFP_NOWARN - If allocation fails, don't issue any warnings.
  285. *
  286. * %__GFP_REPEAT - If allocation fails initially, try once more before failing.
  287. *
  288. * There are other flags available as well, but these are not intended
  289. * for general use, and so are not documented here. For a full list of
  290. * potential flags, always refer to linux/gfp.h.
  291. */
  292. static inline void *kmalloc_array(size_t n, size_t size, gfp_t flags)
  293. {
  294. if (size != 0 && n > SIZE_MAX / size)
  295. return NULL;
  296. return __kmalloc(n * size, flags);
  297. }
  298. /**
  299. * kcalloc - allocate memory for an array. The memory is set to zero.
  300. * @n: number of elements.
  301. * @size: element size.
  302. * @flags: the type of memory to allocate (see kmalloc).
  303. */
  304. static inline void *kcalloc(size_t n, size_t size, gfp_t flags)
  305. {
  306. return kmalloc_array(n, size, flags | __GFP_ZERO);
  307. }
  308. #if !defined(CONFIG_NUMA) && !defined(CONFIG_SLOB)
  309. /**
  310. * kmalloc_node - allocate memory from a specific node
  311. * @size: how many bytes of memory are required.
  312. * @flags: the type of memory to allocate (see kcalloc).
  313. * @node: node to allocate from.
  314. *
  315. * kmalloc() for non-local nodes, used to allocate from a specific node
  316. * if available. Equivalent to kmalloc() in the non-NUMA single-node
  317. * case.
  318. */
  319. static inline void *kmalloc_node(size_t size, gfp_t flags, int node)
  320. {
  321. return kmalloc(size, flags);
  322. }
  323. static inline void *__kmalloc_node(size_t size, gfp_t flags, int node)
  324. {
  325. return __kmalloc(size, flags);
  326. }
  327. void *kmem_cache_alloc(struct kmem_cache *, gfp_t);
  328. static inline void *kmem_cache_alloc_node(struct kmem_cache *cachep,
  329. gfp_t flags, int node)
  330. {
  331. return kmem_cache_alloc(cachep, flags);
  332. }
  333. #endif /* !CONFIG_NUMA && !CONFIG_SLOB */
  334. /*
  335. * kmalloc_track_caller is a special version of kmalloc that records the
  336. * calling function of the routine calling it for slab leak tracking instead
  337. * of just the calling function (confusing, eh?).
  338. * It's useful when the call to kmalloc comes from a widely-used standard
  339. * allocator where we care about the real place the memory allocation
  340. * request comes from.
  341. */
  342. #if defined(CONFIG_DEBUG_SLAB) || defined(CONFIG_SLUB) || \
  343. (defined(CONFIG_SLAB) && defined(CONFIG_TRACING)) || \
  344. (defined(CONFIG_SLOB) && defined(CONFIG_TRACING))
  345. extern void *__kmalloc_track_caller(size_t, gfp_t, unsigned long);
  346. #define kmalloc_track_caller(size, flags) \
  347. __kmalloc_track_caller(size, flags, _RET_IP_)
  348. #else
  349. #define kmalloc_track_caller(size, flags) \
  350. __kmalloc(size, flags)
  351. #endif /* DEBUG_SLAB */
  352. #ifdef CONFIG_NUMA
  353. /*
  354. * kmalloc_node_track_caller is a special version of kmalloc_node that
  355. * records the calling function of the routine calling it for slab leak
  356. * tracking instead of just the calling function (confusing, eh?).
  357. * It's useful when the call to kmalloc_node comes from a widely-used
  358. * standard allocator where we care about the real place the memory
  359. * allocation request comes from.
  360. */
  361. #if defined(CONFIG_DEBUG_SLAB) || defined(CONFIG_SLUB) || \
  362. (defined(CONFIG_SLAB) && defined(CONFIG_TRACING)) || \
  363. (defined(CONFIG_SLOB) && defined(CONFIG_TRACING))
  364. extern void *__kmalloc_node_track_caller(size_t, gfp_t, int, unsigned long);
  365. #define kmalloc_node_track_caller(size, flags, node) \
  366. __kmalloc_node_track_caller(size, flags, node, \
  367. _RET_IP_)
  368. #else
  369. #define kmalloc_node_track_caller(size, flags, node) \
  370. __kmalloc_node(size, flags, node)
  371. #endif
  372. #else /* CONFIG_NUMA */
  373. #define kmalloc_node_track_caller(size, flags, node) \
  374. kmalloc_track_caller(size, flags)
  375. #endif /* CONFIG_NUMA */
  376. /*
  377. * Shortcuts
  378. */
  379. static inline void *kmem_cache_zalloc(struct kmem_cache *k, gfp_t flags)
  380. {
  381. return kmem_cache_alloc(k, flags | __GFP_ZERO);
  382. }
  383. /**
  384. * kzalloc - allocate memory. The memory is set to zero.
  385. * @size: how many bytes of memory are required.
  386. * @flags: the type of memory to allocate (see kmalloc).
  387. */
  388. static inline void *kzalloc(size_t size, gfp_t flags)
  389. {
  390. return kmalloc(size, flags | __GFP_ZERO);
  391. }
  392. /**
  393. * kzalloc_node - allocate zeroed memory from a particular memory node.
  394. * @size: how many bytes of memory are required.
  395. * @flags: the type of memory to allocate (see kmalloc).
  396. * @node: memory node from which to allocate
  397. */
  398. static inline void *kzalloc_node(size_t size, gfp_t flags, int node)
  399. {
  400. return kmalloc_node(size, flags | __GFP_ZERO, node);
  401. }
  402. /*
  403. * Determine the size of a slab object
  404. */
  405. static inline unsigned int kmem_cache_size(struct kmem_cache *s)
  406. {
  407. return s->object_size;
  408. }
  409. void __init kmem_cache_init_late(void);
  410. #endif /* _LINUX_SLAB_H */