slab_def.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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 <linux/compiler.h>
  13. /*
  14. * struct kmem_cache
  15. *
  16. * manages a cache.
  17. */
  18. struct kmem_cache {
  19. /* 1) Cache tunables. Protected by cache_chain_mutex */
  20. unsigned int batchcount;
  21. unsigned int limit;
  22. unsigned int shared;
  23. unsigned int size;
  24. u32 reciprocal_buffer_size;
  25. /* 2) touched by every alloc & free from the backend */
  26. unsigned int flags; /* constant flags */
  27. unsigned int num; /* # of objs per slab */
  28. /* 3) cache_grow/shrink */
  29. /* order of pgs per slab (2^n) */
  30. unsigned int gfporder;
  31. /* force GFP flags, e.g. GFP_DMA */
  32. gfp_t allocflags;
  33. size_t colour; /* cache colouring range */
  34. unsigned int colour_off; /* colour offset */
  35. struct kmem_cache *slabp_cache;
  36. unsigned int slab_size;
  37. /* constructor func */
  38. void (*ctor)(void *obj);
  39. /* 4) cache creation/removal */
  40. const char *name;
  41. struct list_head list;
  42. int refcount;
  43. int object_size;
  44. int align;
  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. 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. #endif /* CONFIG_DEBUG_SLAB */
  69. #ifdef CONFIG_MEMCG_KMEM
  70. struct memcg_cache_params *memcg_params;
  71. #endif
  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. * We also need to guarantee that the list is able to accomodate a
  81. * pointer for each node since "nodelists" uses the remainder of
  82. * available pointers.
  83. */
  84. struct kmem_cache_node **node;
  85. struct array_cache *array[NR_CPUS + MAX_NUMNODES];
  86. /*
  87. * Do not add fields after array[]
  88. */
  89. };
  90. void *kmem_cache_alloc(struct kmem_cache *, gfp_t);
  91. void *__kmalloc(size_t size, gfp_t flags);
  92. #ifdef CONFIG_TRACING
  93. extern void *kmem_cache_alloc_trace(struct kmem_cache *, gfp_t, size_t);
  94. #else
  95. static __always_inline void *
  96. kmem_cache_alloc_trace(struct kmem_cache *cachep, gfp_t flags, size_t size)
  97. {
  98. return kmem_cache_alloc(cachep, flags);
  99. }
  100. #endif
  101. static __always_inline void *kmalloc(size_t size, gfp_t flags)
  102. {
  103. struct kmem_cache *cachep;
  104. void *ret;
  105. if (__builtin_constant_p(size)) {
  106. int i;
  107. if (!size)
  108. return ZERO_SIZE_PTR;
  109. if (WARN_ON_ONCE(size > KMALLOC_MAX_SIZE))
  110. return NULL;
  111. i = kmalloc_index(size);
  112. #ifdef CONFIG_ZONE_DMA
  113. if (flags & GFP_DMA)
  114. cachep = kmalloc_dma_caches[i];
  115. else
  116. #endif
  117. cachep = kmalloc_caches[i];
  118. ret = kmem_cache_alloc_trace(cachep, flags, size);
  119. return ret;
  120. }
  121. return __kmalloc(size, flags);
  122. }
  123. #ifdef CONFIG_NUMA
  124. extern void *__kmalloc_node(size_t size, gfp_t flags, int node);
  125. extern void *kmem_cache_alloc_node(struct kmem_cache *, gfp_t flags, int node);
  126. #ifdef CONFIG_TRACING
  127. extern void *kmem_cache_alloc_node_trace(struct kmem_cache *cachep,
  128. gfp_t flags,
  129. int nodeid,
  130. size_t size);
  131. #else
  132. static __always_inline void *
  133. kmem_cache_alloc_node_trace(struct kmem_cache *cachep,
  134. gfp_t flags,
  135. int nodeid,
  136. size_t size)
  137. {
  138. return kmem_cache_alloc_node(cachep, flags, nodeid);
  139. }
  140. #endif
  141. static __always_inline void *kmalloc_node(size_t size, gfp_t flags, int node)
  142. {
  143. struct kmem_cache *cachep;
  144. if (__builtin_constant_p(size)) {
  145. int i;
  146. if (!size)
  147. return ZERO_SIZE_PTR;
  148. if (WARN_ON_ONCE(size > KMALLOC_MAX_SIZE))
  149. return NULL;
  150. i = kmalloc_index(size);
  151. #ifdef CONFIG_ZONE_DMA
  152. if (flags & GFP_DMA)
  153. cachep = kmalloc_dma_caches[i];
  154. else
  155. #endif
  156. cachep = kmalloc_caches[i];
  157. return kmem_cache_alloc_node_trace(cachep, flags, node, size);
  158. }
  159. return __kmalloc_node(size, flags, node);
  160. }
  161. #endif /* CONFIG_NUMA */
  162. #endif /* _LINUX_SLAB_DEF_H */