slab_def.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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. * We also need to guarantee that the list is able to accomodate a
  80. * pointer for each node since "nodelists" uses the remainder of
  81. * available pointers.
  82. */
  83. struct kmem_list3 **nodelists;
  84. struct array_cache *array[NR_CPUS + MAX_NUMNODES];
  85. /*
  86. * Do not add fields after array[]
  87. */
  88. };
  89. /* Size description struct for general caches. */
  90. struct cache_sizes {
  91. size_t cs_size;
  92. struct kmem_cache *cs_cachep;
  93. #ifdef CONFIG_ZONE_DMA
  94. struct kmem_cache *cs_dmacachep;
  95. #endif
  96. };
  97. extern struct cache_sizes malloc_sizes[];
  98. void *kmem_cache_alloc(struct kmem_cache *, gfp_t);
  99. void *__kmalloc(size_t size, gfp_t flags);
  100. #ifdef CONFIG_TRACING
  101. extern void *kmem_cache_alloc_trace(struct kmem_cache *, gfp_t, size_t);
  102. #else
  103. static __always_inline void *
  104. kmem_cache_alloc_trace(struct kmem_cache *cachep, gfp_t flags, size_t size)
  105. {
  106. return kmem_cache_alloc(cachep, flags);
  107. }
  108. #endif
  109. static __always_inline void *kmalloc(size_t size, gfp_t flags)
  110. {
  111. struct kmem_cache *cachep;
  112. void *ret;
  113. if (__builtin_constant_p(size)) {
  114. int i = 0;
  115. if (!size)
  116. return ZERO_SIZE_PTR;
  117. #define CACHE(x) \
  118. if (size <= x) \
  119. goto found; \
  120. else \
  121. i++;
  122. #include <linux/kmalloc_sizes.h>
  123. #undef CACHE
  124. return NULL;
  125. found:
  126. #ifdef CONFIG_ZONE_DMA
  127. if (flags & GFP_DMA)
  128. cachep = malloc_sizes[i].cs_dmacachep;
  129. else
  130. #endif
  131. cachep = malloc_sizes[i].cs_cachep;
  132. ret = kmem_cache_alloc_trace(cachep, flags, size);
  133. return ret;
  134. }
  135. return __kmalloc(size, flags);
  136. }
  137. #ifdef CONFIG_NUMA
  138. extern void *__kmalloc_node(size_t size, gfp_t flags, int node);
  139. extern void *kmem_cache_alloc_node(struct kmem_cache *, gfp_t flags, int node);
  140. #ifdef CONFIG_TRACING
  141. extern void *kmem_cache_alloc_node_trace(struct kmem_cache *cachep,
  142. gfp_t flags,
  143. int nodeid,
  144. size_t size);
  145. #else
  146. static __always_inline void *
  147. kmem_cache_alloc_node_trace(struct kmem_cache *cachep,
  148. gfp_t flags,
  149. int nodeid,
  150. size_t size)
  151. {
  152. return kmem_cache_alloc_node(cachep, flags, nodeid);
  153. }
  154. #endif
  155. static __always_inline void *kmalloc_node(size_t size, gfp_t flags, int node)
  156. {
  157. struct kmem_cache *cachep;
  158. if (__builtin_constant_p(size)) {
  159. int i = 0;
  160. if (!size)
  161. return ZERO_SIZE_PTR;
  162. #define CACHE(x) \
  163. if (size <= x) \
  164. goto found; \
  165. else \
  166. i++;
  167. #include <linux/kmalloc_sizes.h>
  168. #undef CACHE
  169. return NULL;
  170. found:
  171. #ifdef CONFIG_ZONE_DMA
  172. if (flags & GFP_DMA)
  173. cachep = malloc_sizes[i].cs_dmacachep;
  174. else
  175. #endif
  176. cachep = malloc_sizes[i].cs_cachep;
  177. return kmem_cache_alloc_node_trace(cachep, flags, node, size);
  178. }
  179. return __kmalloc_node(size, flags, node);
  180. }
  181. #endif /* CONFIG_NUMA */
  182. #endif /* _LINUX_SLAB_DEF_H */