slab_def.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. #ifndef _LINUX_SLAB_DEF_H
  2. #define _LINUX_SLAB_DEF_H
  3. /*
  4. * Definitions unique to the original Linux SLAB allocator.
  5. *
  6. * What we provide here is a way to optimize the frequent kmalloc
  7. * calls in the kernel by selecting the appropriate general cache
  8. * if kmalloc was called with a size that can be established at
  9. * compile time.
  10. */
  11. #include <linux/init.h>
  12. #include <asm/page.h> /* kmalloc_sizes.h needs PAGE_SIZE */
  13. #include <asm/cache.h> /* kmalloc_sizes.h needs L1_CACHE_BYTES */
  14. #include <linux/compiler.h>
  15. /*
  16. * struct kmem_cache
  17. *
  18. * manages a cache.
  19. */
  20. struct kmem_cache {
  21. /* 1) Cache tunables. Protected by cache_chain_mutex */
  22. unsigned int batchcount;
  23. unsigned int limit;
  24. unsigned int shared;
  25. unsigned int size;
  26. u32 reciprocal_buffer_size;
  27. /* 2) touched by every alloc & free from the backend */
  28. unsigned int flags; /* constant flags */
  29. unsigned int num; /* # of objs per slab */
  30. /* 3) cache_grow/shrink */
  31. /* order of pgs per slab (2^n) */
  32. unsigned int gfporder;
  33. /* force GFP flags, e.g. GFP_DMA */
  34. gfp_t allocflags;
  35. size_t colour; /* cache colouring range */
  36. unsigned int colour_off; /* colour offset */
  37. struct kmem_cache *slabp_cache;
  38. unsigned int slab_size;
  39. /* constructor func */
  40. void (*ctor)(void *obj);
  41. /* 4) cache creation/removal */
  42. const char *name;
  43. struct list_head list;
  44. int refcount;
  45. int object_size;
  46. int align;
  47. /* 5) statistics */
  48. #ifdef CONFIG_DEBUG_SLAB
  49. unsigned long num_active;
  50. unsigned long num_allocations;
  51. unsigned long high_mark;
  52. unsigned long grown;
  53. unsigned long reaped;
  54. unsigned long errors;
  55. unsigned long max_freeable;
  56. unsigned long node_allocs;
  57. unsigned long node_frees;
  58. unsigned long node_overflow;
  59. atomic_t allochit;
  60. atomic_t allocmiss;
  61. atomic_t freehit;
  62. atomic_t freemiss;
  63. /*
  64. * If debugging is enabled, then the allocator can add additional
  65. * fields and/or padding to every object. size contains the total
  66. * object size including these internal fields, the following two
  67. * variables contain the offset to the user object and its size.
  68. */
  69. int obj_offset;
  70. #endif /* CONFIG_DEBUG_SLAB */
  71. #ifdef CONFIG_MEMCG_KMEM
  72. struct memcg_cache_params *memcg_params;
  73. #endif
  74. /* 6) per-cpu/per-node data, touched during every alloc/free */
  75. /*
  76. * We put array[] at the end of kmem_cache, because we want to size
  77. * this array to nr_cpu_ids slots instead of NR_CPUS
  78. * (see kmem_cache_init())
  79. * We still use [NR_CPUS] and not [1] or [0] because cache_cache
  80. * is statically defined, so we reserve the max number of cpus.
  81. *
  82. * We also need to guarantee that the list is able to accomodate a
  83. * pointer for each node since "nodelists" uses the remainder of
  84. * available pointers.
  85. */
  86. struct kmem_list3 **nodelists;
  87. struct array_cache *array[NR_CPUS + MAX_NUMNODES];
  88. /*
  89. * Do not add fields after array[]
  90. */
  91. };
  92. /* Size description struct for general caches. */
  93. struct cache_sizes {
  94. size_t cs_size;
  95. struct kmem_cache *cs_cachep;
  96. #ifdef CONFIG_ZONE_DMA
  97. struct kmem_cache *cs_dmacachep;
  98. #endif
  99. };
  100. extern struct cache_sizes malloc_sizes[];
  101. void *kmem_cache_alloc(struct kmem_cache *, gfp_t);
  102. void *__kmalloc(size_t size, gfp_t flags);
  103. #ifdef CONFIG_TRACING
  104. extern void *kmem_cache_alloc_trace(struct kmem_cache *, gfp_t, size_t);
  105. #else
  106. static __always_inline void *
  107. kmem_cache_alloc_trace(struct kmem_cache *cachep, gfp_t flags, size_t size)
  108. {
  109. return kmem_cache_alloc(cachep, flags);
  110. }
  111. #endif
  112. static __always_inline void *kmalloc(size_t size, gfp_t flags)
  113. {
  114. struct kmem_cache *cachep;
  115. void *ret;
  116. if (__builtin_constant_p(size)) {
  117. int i = 0;
  118. if (!size)
  119. return ZERO_SIZE_PTR;
  120. #define CACHE(x) \
  121. if (size <= x) \
  122. goto found; \
  123. else \
  124. i++;
  125. #include <linux/kmalloc_sizes.h>
  126. #undef CACHE
  127. return NULL;
  128. found:
  129. #ifdef CONFIG_ZONE_DMA
  130. if (flags & GFP_DMA)
  131. cachep = malloc_sizes[i].cs_dmacachep;
  132. else
  133. #endif
  134. cachep = malloc_sizes[i].cs_cachep;
  135. ret = kmem_cache_alloc_trace(cachep, flags, size);
  136. return ret;
  137. }
  138. return __kmalloc(size, flags);
  139. }
  140. #ifdef CONFIG_NUMA
  141. extern void *__kmalloc_node(size_t size, gfp_t flags, int node);
  142. extern void *kmem_cache_alloc_node(struct kmem_cache *, gfp_t flags, int node);
  143. #ifdef CONFIG_TRACING
  144. extern void *kmem_cache_alloc_node_trace(struct kmem_cache *cachep,
  145. gfp_t flags,
  146. int nodeid,
  147. size_t size);
  148. #else
  149. static __always_inline void *
  150. kmem_cache_alloc_node_trace(struct kmem_cache *cachep,
  151. gfp_t flags,
  152. int nodeid,
  153. size_t size)
  154. {
  155. return kmem_cache_alloc_node(cachep, flags, nodeid);
  156. }
  157. #endif
  158. static __always_inline void *kmalloc_node(size_t size, gfp_t flags, int node)
  159. {
  160. struct kmem_cache *cachep;
  161. if (__builtin_constant_p(size)) {
  162. int i = 0;
  163. if (!size)
  164. return ZERO_SIZE_PTR;
  165. #define CACHE(x) \
  166. if (size <= x) \
  167. goto found; \
  168. else \
  169. i++;
  170. #include <linux/kmalloc_sizes.h>
  171. #undef CACHE
  172. return NULL;
  173. found:
  174. #ifdef CONFIG_ZONE_DMA
  175. if (flags & GFP_DMA)
  176. cachep = malloc_sizes[i].cs_dmacachep;
  177. else
  178. #endif
  179. cachep = malloc_sizes[i].cs_cachep;
  180. return kmem_cache_alloc_node_trace(cachep, flags, node, size);
  181. }
  182. return __kmalloc_node(size, flags, node);
  183. }
  184. #endif /* CONFIG_NUMA */
  185. #endif /* _LINUX_SLAB_DEF_H */