slub_def.h 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. #ifndef _LINUX_SLUB_DEF_H
  2. #define _LINUX_SLUB_DEF_H
  3. /*
  4. * SLUB : A Slab allocator without object queues.
  5. *
  6. * (C) 2007 SGI, Christoph Lameter
  7. */
  8. #include <linux/types.h>
  9. #include <linux/gfp.h>
  10. #include <linux/bug.h>
  11. #include <linux/workqueue.h>
  12. #include <linux/kobject.h>
  13. #include <linux/kmemleak.h>
  14. enum stat_item {
  15. ALLOC_FASTPATH, /* Allocation from cpu slab */
  16. ALLOC_SLOWPATH, /* Allocation by getting a new cpu slab */
  17. FREE_FASTPATH, /* Free to cpu slub */
  18. FREE_SLOWPATH, /* Freeing not to cpu slab */
  19. FREE_FROZEN, /* Freeing to frozen slab */
  20. FREE_ADD_PARTIAL, /* Freeing moves slab to partial list */
  21. FREE_REMOVE_PARTIAL, /* Freeing removes last object */
  22. ALLOC_FROM_PARTIAL, /* Cpu slab acquired from node partial list */
  23. ALLOC_SLAB, /* Cpu slab acquired from page allocator */
  24. ALLOC_REFILL, /* Refill cpu slab from slab freelist */
  25. ALLOC_NODE_MISMATCH, /* Switching cpu slab */
  26. FREE_SLAB, /* Slab freed to the page allocator */
  27. CPUSLAB_FLUSH, /* Abandoning of the cpu slab */
  28. DEACTIVATE_FULL, /* Cpu slab was full when deactivated */
  29. DEACTIVATE_EMPTY, /* Cpu slab was empty when deactivated */
  30. DEACTIVATE_TO_HEAD, /* Cpu slab was moved to the head of partials */
  31. DEACTIVATE_TO_TAIL, /* Cpu slab was moved to the tail of partials */
  32. DEACTIVATE_REMOTE_FREES,/* Slab contained remotely freed objects */
  33. DEACTIVATE_BYPASS, /* Implicit deactivation */
  34. ORDER_FALLBACK, /* Number of times fallback was necessary */
  35. CMPXCHG_DOUBLE_CPU_FAIL,/* Failure of this_cpu_cmpxchg_double */
  36. CMPXCHG_DOUBLE_FAIL, /* Number of times that cmpxchg double did not match */
  37. CPU_PARTIAL_ALLOC, /* Used cpu partial on alloc */
  38. CPU_PARTIAL_FREE, /* Refill cpu partial on free */
  39. CPU_PARTIAL_NODE, /* Refill cpu partial from node partial */
  40. CPU_PARTIAL_DRAIN, /* Drain cpu partial to node partial */
  41. NR_SLUB_STAT_ITEMS };
  42. struct kmem_cache_cpu {
  43. void **freelist; /* Pointer to next available object */
  44. unsigned long tid; /* Globally unique transaction id */
  45. struct page *page; /* The slab from which we are allocating */
  46. struct page *partial; /* Partially allocated frozen slabs */
  47. int node; /* The node of the page (or -1 for debug) */
  48. #ifdef CONFIG_SLUB_STATS
  49. unsigned stat[NR_SLUB_STAT_ITEMS];
  50. #endif
  51. };
  52. struct kmem_cache_node {
  53. spinlock_t list_lock; /* Protect partial list and nr_partial */
  54. unsigned long nr_partial;
  55. struct list_head partial;
  56. #ifdef CONFIG_SLUB_DEBUG
  57. atomic_long_t nr_slabs;
  58. atomic_long_t total_objects;
  59. struct list_head full;
  60. #endif
  61. };
  62. /*
  63. * Word size structure that can be atomically updated or read and that
  64. * contains both the order and the number of objects that a slab of the
  65. * given order would contain.
  66. */
  67. struct kmem_cache_order_objects {
  68. unsigned long x;
  69. };
  70. /*
  71. * Slab cache management.
  72. */
  73. struct kmem_cache {
  74. struct kmem_cache_cpu __percpu *cpu_slab;
  75. /* Used for retriving partial slabs etc */
  76. unsigned long flags;
  77. unsigned long min_partial;
  78. int size; /* The size of an object including meta data */
  79. int objsize; /* The size of an object without meta data */
  80. int offset; /* Free pointer offset. */
  81. int cpu_partial; /* Number of per cpu partial objects to keep around */
  82. struct kmem_cache_order_objects oo;
  83. /* Allocation and freeing of slabs */
  84. struct kmem_cache_order_objects max;
  85. struct kmem_cache_order_objects min;
  86. gfp_t allocflags; /* gfp flags to use on each alloc */
  87. int refcount; /* Refcount for slab cache destroy */
  88. void (*ctor)(void *);
  89. int inuse; /* Offset to metadata */
  90. int align; /* Alignment */
  91. int reserved; /* Reserved bytes at the end of slabs */
  92. const char *name; /* Name (only for display!) */
  93. struct list_head list; /* List of slab caches */
  94. #ifdef CONFIG_SYSFS
  95. struct kobject kobj; /* For sysfs */
  96. #endif
  97. #ifdef CONFIG_NUMA
  98. /*
  99. * Defragmentation by allocating from a remote node.
  100. */
  101. int remote_node_defrag_ratio;
  102. #endif
  103. struct kmem_cache_node *node[MAX_NUMNODES];
  104. };
  105. /*
  106. * Kmalloc subsystem.
  107. */
  108. #if defined(ARCH_DMA_MINALIGN) && ARCH_DMA_MINALIGN > 8
  109. #define KMALLOC_MIN_SIZE ARCH_DMA_MINALIGN
  110. #else
  111. #define KMALLOC_MIN_SIZE 8
  112. #endif
  113. #define KMALLOC_SHIFT_LOW ilog2(KMALLOC_MIN_SIZE)
  114. /*
  115. * Maximum kmalloc object size handled by SLUB. Larger object allocations
  116. * are passed through to the page allocator. The page allocator "fastpath"
  117. * is relatively slow so we need this value sufficiently high so that
  118. * performance critical objects are allocated through the SLUB fastpath.
  119. *
  120. * This should be dropped to PAGE_SIZE / 2 once the page allocator
  121. * "fastpath" becomes competitive with the slab allocator fastpaths.
  122. */
  123. #define SLUB_MAX_SIZE (2 * PAGE_SIZE)
  124. #define SLUB_PAGE_SHIFT (PAGE_SHIFT + 2)
  125. #ifdef CONFIG_ZONE_DMA
  126. #define SLUB_DMA __GFP_DMA
  127. #else
  128. /* Disable DMA functionality */
  129. #define SLUB_DMA (__force gfp_t)0
  130. #endif
  131. /*
  132. * We keep the general caches in an array of slab caches that are used for
  133. * 2^x bytes of allocations.
  134. */
  135. extern struct kmem_cache *kmalloc_caches[SLUB_PAGE_SHIFT];
  136. /*
  137. * Sorry that the following has to be that ugly but some versions of GCC
  138. * have trouble with constant propagation and loops.
  139. */
  140. static __always_inline int kmalloc_index(size_t size)
  141. {
  142. if (!size)
  143. return 0;
  144. if (size <= KMALLOC_MIN_SIZE)
  145. return KMALLOC_SHIFT_LOW;
  146. if (KMALLOC_MIN_SIZE <= 32 && size > 64 && size <= 96)
  147. return 1;
  148. if (KMALLOC_MIN_SIZE <= 64 && size > 128 && size <= 192)
  149. return 2;
  150. if (size <= 8) return 3;
  151. if (size <= 16) return 4;
  152. if (size <= 32) return 5;
  153. if (size <= 64) return 6;
  154. if (size <= 128) return 7;
  155. if (size <= 256) return 8;
  156. if (size <= 512) return 9;
  157. if (size <= 1024) return 10;
  158. if (size <= 2 * 1024) return 11;
  159. if (size <= 4 * 1024) return 12;
  160. /*
  161. * The following is only needed to support architectures with a larger page
  162. * size than 4k. We need to support 2 * PAGE_SIZE here. So for a 64k page
  163. * size we would have to go up to 128k.
  164. */
  165. if (size <= 8 * 1024) return 13;
  166. if (size <= 16 * 1024) return 14;
  167. if (size <= 32 * 1024) return 15;
  168. if (size <= 64 * 1024) return 16;
  169. if (size <= 128 * 1024) return 17;
  170. if (size <= 256 * 1024) return 18;
  171. if (size <= 512 * 1024) return 19;
  172. if (size <= 1024 * 1024) return 20;
  173. if (size <= 2 * 1024 * 1024) return 21;
  174. BUG();
  175. return -1; /* Will never be reached */
  176. /*
  177. * What we really wanted to do and cannot do because of compiler issues is:
  178. * int i;
  179. * for (i = KMALLOC_SHIFT_LOW; i <= KMALLOC_SHIFT_HIGH; i++)
  180. * if (size <= (1 << i))
  181. * return i;
  182. */
  183. }
  184. /*
  185. * Find the slab cache for a given combination of allocation flags and size.
  186. *
  187. * This ought to end up with a global pointer to the right cache
  188. * in kmalloc_caches.
  189. */
  190. static __always_inline struct kmem_cache *kmalloc_slab(size_t size)
  191. {
  192. int index = kmalloc_index(size);
  193. if (index == 0)
  194. return NULL;
  195. return kmalloc_caches[index];
  196. }
  197. void *kmem_cache_alloc(struct kmem_cache *, gfp_t);
  198. void *__kmalloc(size_t size, gfp_t flags);
  199. static __always_inline void *
  200. kmalloc_order(size_t size, gfp_t flags, unsigned int order)
  201. {
  202. void *ret = (void *) __get_free_pages(flags | __GFP_COMP, order);
  203. kmemleak_alloc(ret, size, 1, flags);
  204. return ret;
  205. }
  206. /**
  207. * Calling this on allocated memory will check that the memory
  208. * is expected to be in use, and print warnings if not.
  209. */
  210. #ifdef CONFIG_SLUB_DEBUG
  211. extern bool verify_mem_not_deleted(const void *x);
  212. #else
  213. static inline bool verify_mem_not_deleted(const void *x)
  214. {
  215. return true;
  216. }
  217. #endif
  218. #ifdef CONFIG_TRACING
  219. extern void *
  220. kmem_cache_alloc_trace(struct kmem_cache *s, gfp_t gfpflags, size_t size);
  221. extern void *kmalloc_order_trace(size_t size, gfp_t flags, unsigned int order);
  222. #else
  223. static __always_inline void *
  224. kmem_cache_alloc_trace(struct kmem_cache *s, gfp_t gfpflags, size_t size)
  225. {
  226. return kmem_cache_alloc(s, gfpflags);
  227. }
  228. static __always_inline void *
  229. kmalloc_order_trace(size_t size, gfp_t flags, unsigned int order)
  230. {
  231. return kmalloc_order(size, flags, order);
  232. }
  233. #endif
  234. static __always_inline void *kmalloc_large(size_t size, gfp_t flags)
  235. {
  236. unsigned int order = get_order(size);
  237. return kmalloc_order_trace(size, flags, order);
  238. }
  239. static __always_inline void *kmalloc(size_t size, gfp_t flags)
  240. {
  241. if (__builtin_constant_p(size)) {
  242. if (size > SLUB_MAX_SIZE)
  243. return kmalloc_large(size, flags);
  244. if (!(flags & SLUB_DMA)) {
  245. struct kmem_cache *s = kmalloc_slab(size);
  246. if (!s)
  247. return ZERO_SIZE_PTR;
  248. return kmem_cache_alloc_trace(s, flags, size);
  249. }
  250. }
  251. return __kmalloc(size, flags);
  252. }
  253. #ifdef CONFIG_NUMA
  254. void *__kmalloc_node(size_t size, gfp_t flags, int node);
  255. void *kmem_cache_alloc_node(struct kmem_cache *, gfp_t flags, int node);
  256. #ifdef CONFIG_TRACING
  257. extern void *kmem_cache_alloc_node_trace(struct kmem_cache *s,
  258. gfp_t gfpflags,
  259. int node, size_t size);
  260. #else
  261. static __always_inline void *
  262. kmem_cache_alloc_node_trace(struct kmem_cache *s,
  263. gfp_t gfpflags,
  264. int node, size_t size)
  265. {
  266. return kmem_cache_alloc_node(s, gfpflags, node);
  267. }
  268. #endif
  269. static __always_inline void *kmalloc_node(size_t size, gfp_t flags, int node)
  270. {
  271. if (__builtin_constant_p(size) &&
  272. size <= SLUB_MAX_SIZE && !(flags & SLUB_DMA)) {
  273. struct kmem_cache *s = kmalloc_slab(size);
  274. if (!s)
  275. return ZERO_SIZE_PTR;
  276. return kmem_cache_alloc_node_trace(s, flags, node, size);
  277. }
  278. return __kmalloc_node(size, flags, node);
  279. }
  280. #endif
  281. #endif /* _LINUX_SLUB_DEF_H */