slab_def.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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. /* 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(struct kmem_cache *, gfp_t, size_t);
  98. #else
  99. static __always_inline void *
  100. kmem_cache_alloc_trace(struct kmem_cache *cachep, gfp_t flags, size_t size)
  101. {
  102. return kmem_cache_alloc(cachep, flags);
  103. }
  104. #endif
  105. static __always_inline void *kmalloc(size_t size, gfp_t flags)
  106. {
  107. struct kmem_cache *cachep;
  108. void *ret;
  109. if (__builtin_constant_p(size)) {
  110. int i = 0;
  111. if (!size)
  112. return ZERO_SIZE_PTR;
  113. #define CACHE(x) \
  114. if (size <= x) \
  115. goto found; \
  116. else \
  117. i++;
  118. #include <linux/kmalloc_sizes.h>
  119. #undef CACHE
  120. return NULL;
  121. found:
  122. #ifdef CONFIG_ZONE_DMA
  123. if (flags & GFP_DMA)
  124. cachep = malloc_sizes[i].cs_dmacachep;
  125. else
  126. #endif
  127. cachep = malloc_sizes[i].cs_cachep;
  128. ret = kmem_cache_alloc_trace(cachep, flags, size);
  129. return ret;
  130. }
  131. return __kmalloc(size, flags);
  132. }
  133. #ifdef CONFIG_NUMA
  134. extern void *__kmalloc_node(size_t size, gfp_t flags, int node);
  135. extern void *kmem_cache_alloc_node(struct kmem_cache *, gfp_t flags, int node);
  136. #ifdef CONFIG_TRACING
  137. extern void *kmem_cache_alloc_node_trace(struct kmem_cache *cachep,
  138. gfp_t flags,
  139. int nodeid,
  140. size_t size);
  141. #else
  142. static __always_inline void *
  143. kmem_cache_alloc_node_trace(struct kmem_cache *cachep,
  144. gfp_t flags,
  145. int nodeid,
  146. size_t size)
  147. {
  148. return kmem_cache_alloc_node(cachep, flags, nodeid);
  149. }
  150. #endif
  151. static __always_inline void *kmalloc_node(size_t size, gfp_t flags, int node)
  152. {
  153. struct kmem_cache *cachep;
  154. if (__builtin_constant_p(size)) {
  155. int i = 0;
  156. if (!size)
  157. return ZERO_SIZE_PTR;
  158. #define CACHE(x) \
  159. if (size <= x) \
  160. goto found; \
  161. else \
  162. i++;
  163. #include <linux/kmalloc_sizes.h>
  164. #undef CACHE
  165. return NULL;
  166. found:
  167. #ifdef CONFIG_ZONE_DMA
  168. if (flags & GFP_DMA)
  169. cachep = malloc_sizes[i].cs_dmacachep;
  170. else
  171. #endif
  172. cachep = malloc_sizes[i].cs_cachep;
  173. return kmem_cache_alloc_node_trace(cachep, flags, node, size);
  174. }
  175. return __kmalloc_node(size, flags, node);
  176. }
  177. #endif /* CONFIG_NUMA */
  178. #endif /* _LINUX_SLAB_DEF_H */