slab_def.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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 buffer_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 gfpflags;
  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 next;
  45. /* 5) statistics */
  46. #ifdef CONFIG_DEBUG_SLAB
  47. unsigned long num_active;
  48. unsigned long num_allocations;
  49. unsigned long high_mark;
  50. unsigned long grown;
  51. unsigned long reaped;
  52. unsigned long errors;
  53. unsigned long max_freeable;
  54. unsigned long node_allocs;
  55. unsigned long node_frees;
  56. unsigned long node_overflow;
  57. atomic_t allochit;
  58. atomic_t allocmiss;
  59. atomic_t freehit;
  60. atomic_t freemiss;
  61. /*
  62. * If debugging is enabled, then the allocator can add additional
  63. * fields and/or padding to every object. buffer_size contains the total
  64. * object size including these internal fields, the following two
  65. * variables contain the offset to the user object and its size.
  66. */
  67. int obj_offset;
  68. int obj_size;
  69. #endif /* CONFIG_DEBUG_SLAB */
  70. /* 6) per-cpu/per-node data, touched during every alloc/free */
  71. /*
  72. * We put array[] at the end of kmem_cache, because we want to size
  73. * this array to nr_cpu_ids slots instead of NR_CPUS
  74. * (see kmem_cache_init())
  75. * We still use [NR_CPUS] and not [1] or [0] because cache_cache
  76. * is statically defined, so we reserve the max number of cpus.
  77. */
  78. struct kmem_list3 **nodelists;
  79. struct array_cache *array[NR_CPUS];
  80. /*
  81. * Do not add fields after array[]
  82. */
  83. };
  84. /* Size description struct for general caches. */
  85. struct cache_sizes {
  86. size_t cs_size;
  87. struct kmem_cache *cs_cachep;
  88. #ifdef CONFIG_ZONE_DMA
  89. struct kmem_cache *cs_dmacachep;
  90. #endif
  91. };
  92. extern struct cache_sizes malloc_sizes[];
  93. void *kmem_cache_alloc(struct kmem_cache *, gfp_t);
  94. void *__kmalloc(size_t size, gfp_t flags);
  95. #ifdef CONFIG_TRACING
  96. extern void *kmem_cache_alloc_trace(size_t size,
  97. 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_trace(size_t size, 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_trace(size, cachep, flags);
  134. return ret;
  135. }
  136. return __kmalloc(size, flags);
  137. }
  138. #ifdef CONFIG_NUMA
  139. extern void *__kmalloc_node(size_t size, gfp_t flags, int node);
  140. extern void *kmem_cache_alloc_node(struct kmem_cache *, gfp_t flags, int node);
  141. #ifdef CONFIG_TRACING
  142. extern void *kmem_cache_alloc_node_trace(size_t size,
  143. struct kmem_cache *cachep,
  144. gfp_t flags,
  145. int nodeid);
  146. #else
  147. static __always_inline void *
  148. kmem_cache_alloc_node_trace(size_t size,
  149. 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. if (__builtin_constant_p(size)) {
  160. int i = 0;
  161. if (!size)
  162. return ZERO_SIZE_PTR;
  163. #define CACHE(x) \
  164. if (size <= x) \
  165. goto found; \
  166. else \
  167. i++;
  168. #include <linux/kmalloc_sizes.h>
  169. #undef CACHE
  170. return NULL;
  171. found:
  172. #ifdef CONFIG_ZONE_DMA
  173. if (flags & GFP_DMA)
  174. cachep = malloc_sizes[i].cs_dmacachep;
  175. else
  176. #endif
  177. cachep = malloc_sizes[i].cs_cachep;
  178. return kmem_cache_alloc_node_trace(size, cachep, flags, node);
  179. }
  180. return __kmalloc_node(size, flags, node);
  181. }
  182. #endif /* CONFIG_NUMA */
  183. #endif /* _LINUX_SLAB_DEF_H */