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