slab_def.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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. #include <linux/kmemtrace.h>
  16. /*
  17. * struct kmem_cache
  18. *
  19. * manages a cache.
  20. */
  21. struct kmem_cache {
  22. /* 1) per-cpu data, touched during every alloc/free */
  23. struct array_cache *array[NR_CPUS];
  24. /* 2) Cache tunables. Protected by cache_chain_mutex */
  25. unsigned int batchcount;
  26. unsigned int limit;
  27. unsigned int shared;
  28. unsigned int buffer_size;
  29. u32 reciprocal_buffer_size;
  30. /* 3) touched by every alloc & free from the backend */
  31. unsigned int flags; /* constant flags */
  32. unsigned int num; /* # of objs per slab */
  33. /* 4) cache_grow/shrink */
  34. /* order of pgs per slab (2^n) */
  35. unsigned int gfporder;
  36. /* force GFP flags, e.g. GFP_DMA */
  37. gfp_t gfpflags;
  38. size_t colour; /* cache colouring range */
  39. unsigned int colour_off; /* colour offset */
  40. struct kmem_cache *slabp_cache;
  41. unsigned int slab_size;
  42. unsigned int dflags; /* dynamic flags */
  43. /* constructor func */
  44. void (*ctor)(void *obj);
  45. /* 5) cache creation/removal */
  46. const char *name;
  47. struct list_head next;
  48. /* 6) statistics */
  49. #ifdef CONFIG_DEBUG_SLAB
  50. unsigned long num_active;
  51. unsigned long num_allocations;
  52. unsigned long high_mark;
  53. unsigned long grown;
  54. unsigned long reaped;
  55. unsigned long errors;
  56. unsigned long max_freeable;
  57. unsigned long node_allocs;
  58. unsigned long node_frees;
  59. unsigned long node_overflow;
  60. atomic_t allochit;
  61. atomic_t allocmiss;
  62. atomic_t freehit;
  63. atomic_t freemiss;
  64. /*
  65. * If debugging is enabled, then the allocator can add additional
  66. * fields and/or padding to every object. buffer_size contains the total
  67. * object size including these internal fields, the following two
  68. * variables contain the offset to the user object and its size.
  69. */
  70. int obj_offset;
  71. int obj_size;
  72. #endif /* CONFIG_DEBUG_SLAB */
  73. /*
  74. * We put nodelists[] at the end of kmem_cache, because we want to size
  75. * this array to nr_node_ids slots instead of MAX_NUMNODES
  76. * (see kmem_cache_init())
  77. * We still use [MAX_NUMNODES] and not [1] or [0] because cache_cache
  78. * is statically defined, so we reserve the max number of nodes.
  79. */
  80. struct kmem_list3 *nodelists[MAX_NUMNODES];
  81. /*
  82. * Do not add fields after nodelists[]
  83. */
  84. };
  85. /* Size description struct for general caches. */
  86. struct cache_sizes {
  87. size_t cs_size;
  88. struct kmem_cache *cs_cachep;
  89. #ifdef CONFIG_ZONE_DMA
  90. struct kmem_cache *cs_dmacachep;
  91. #endif
  92. };
  93. extern struct cache_sizes malloc_sizes[];
  94. void *kmem_cache_alloc(struct kmem_cache *, gfp_t);
  95. void *__kmalloc(size_t size, gfp_t flags);
  96. #ifdef CONFIG_TRACING
  97. extern void *kmem_cache_alloc_notrace(struct kmem_cache *cachep, gfp_t flags);
  98. extern size_t slab_buffer_size(struct kmem_cache *cachep);
  99. #else
  100. static __always_inline void *
  101. kmem_cache_alloc_notrace(struct kmem_cache *cachep, gfp_t flags)
  102. {
  103. return kmem_cache_alloc(cachep, flags);
  104. }
  105. static inline size_t slab_buffer_size(struct kmem_cache *cachep)
  106. {
  107. return 0;
  108. }
  109. #endif
  110. static __always_inline void *kmalloc(size_t size, gfp_t flags)
  111. {
  112. struct kmem_cache *cachep;
  113. void *ret;
  114. if (__builtin_constant_p(size)) {
  115. int i = 0;
  116. if (!size)
  117. return ZERO_SIZE_PTR;
  118. #define CACHE(x) \
  119. if (size <= x) \
  120. goto found; \
  121. else \
  122. i++;
  123. #include <linux/kmalloc_sizes.h>
  124. #undef CACHE
  125. return NULL;
  126. found:
  127. #ifdef CONFIG_ZONE_DMA
  128. if (flags & GFP_DMA)
  129. cachep = malloc_sizes[i].cs_dmacachep;
  130. else
  131. #endif
  132. cachep = malloc_sizes[i].cs_cachep;
  133. ret = kmem_cache_alloc_notrace(cachep, flags);
  134. trace_kmalloc(_THIS_IP_, ret,
  135. size, slab_buffer_size(cachep), flags);
  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_notrace(struct kmem_cache *cachep,
  145. gfp_t flags,
  146. int nodeid);
  147. #else
  148. static __always_inline void *
  149. kmem_cache_alloc_node_notrace(struct kmem_cache *cachep,
  150. gfp_t flags,
  151. int nodeid)
  152. {
  153. return kmem_cache_alloc_node(cachep, flags, nodeid);
  154. }
  155. #endif
  156. static __always_inline void *kmalloc_node(size_t size, gfp_t flags, int node)
  157. {
  158. struct kmem_cache *cachep;
  159. void *ret;
  160. if (__builtin_constant_p(size)) {
  161. int i = 0;
  162. if (!size)
  163. return ZERO_SIZE_PTR;
  164. #define CACHE(x) \
  165. if (size <= x) \
  166. goto found; \
  167. else \
  168. i++;
  169. #include <linux/kmalloc_sizes.h>
  170. #undef CACHE
  171. return NULL;
  172. found:
  173. #ifdef CONFIG_ZONE_DMA
  174. if (flags & GFP_DMA)
  175. cachep = malloc_sizes[i].cs_dmacachep;
  176. else
  177. #endif
  178. cachep = malloc_sizes[i].cs_cachep;
  179. ret = kmem_cache_alloc_node_notrace(cachep, flags, node);
  180. trace_kmalloc_node(_THIS_IP_, ret,
  181. size, slab_buffer_size(cachep),
  182. flags, node);
  183. return ret;
  184. }
  185. return __kmalloc_node(size, flags, node);
  186. }
  187. #endif /* CONFIG_NUMA */
  188. #endif /* _LINUX_SLAB_DEF_H */