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. unsigned int dflags; /* dynamic flags */
  40. /* constructor func */
  41. void (*ctor)(void *obj);
  42. /* 4) cache creation/removal */
  43. const char *name;
  44. struct list_head list;
  45. int refcount;
  46. int object_size;
  47. int align;
  48. /* 5) 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. 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. #endif /* CONFIG_DEBUG_SLAB */
  72. /* 6) per-cpu/per-node data, touched during every alloc/free */
  73. /*
  74. * We put array[] at the end of kmem_cache, because we want to size
  75. * this array to nr_cpu_ids slots instead of NR_CPUS
  76. * (see kmem_cache_init())
  77. * We still use [NR_CPUS] and not [1] or [0] because cache_cache
  78. * is statically defined, so we reserve the max number of cpus.
  79. */
  80. struct kmem_list3 **nodelists;
  81. struct array_cache *array[NR_CPUS];
  82. /*
  83. * Do not add fields after array[]
  84. */
  85. };
  86. /* Size description struct for general caches. */
  87. struct cache_sizes {
  88. size_t cs_size;
  89. struct kmem_cache *cs_cachep;
  90. #ifdef CONFIG_ZONE_DMA
  91. struct kmem_cache *cs_dmacachep;
  92. #endif
  93. };
  94. extern struct cache_sizes malloc_sizes[];
  95. void *kmem_cache_alloc(struct kmem_cache *, gfp_t);
  96. void *__kmalloc(size_t size, gfp_t flags);
  97. #ifdef CONFIG_TRACING
  98. extern void *kmem_cache_alloc_trace(size_t size,
  99. struct kmem_cache *cachep, gfp_t flags);
  100. extern size_t slab_buffer_size(struct kmem_cache *cachep);
  101. #else
  102. static __always_inline void *
  103. kmem_cache_alloc_trace(size_t size, struct kmem_cache *cachep, gfp_t flags)
  104. {
  105. return kmem_cache_alloc(cachep, flags);
  106. }
  107. static inline size_t slab_buffer_size(struct kmem_cache *cachep)
  108. {
  109. return 0;
  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(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_trace(size_t size,
  145. struct kmem_cache *cachep,
  146. gfp_t flags,
  147. int nodeid);
  148. #else
  149. static __always_inline void *
  150. kmem_cache_alloc_node_trace(size_t size,
  151. struct kmem_cache *cachep,
  152. gfp_t flags,
  153. int nodeid)
  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(size, cachep, flags, node);
  181. }
  182. return __kmalloc_node(size, flags, node);
  183. }
  184. #endif /* CONFIG_NUMA */
  185. #endif /* _LINUX_SLAB_DEF_H */