slub_def.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. #ifndef _LINUX_SLUB_DEF_H
  2. #define _LINUX_SLUB_DEF_H
  3. /*
  4. * SLUB : A Slab allocator without object queues.
  5. *
  6. * (C) 2007 SGI, Christoph Lameter <clameter@sgi.com>
  7. */
  8. #include <linux/types.h>
  9. #include <linux/gfp.h>
  10. #include <linux/workqueue.h>
  11. #include <linux/kobject.h>
  12. struct kmem_cache_node {
  13. spinlock_t list_lock; /* Protect partial list and nr_partial */
  14. unsigned long nr_partial;
  15. atomic_long_t nr_slabs;
  16. struct list_head partial;
  17. struct list_head full;
  18. };
  19. /*
  20. * Slab cache management.
  21. */
  22. struct kmem_cache {
  23. /* Used for retriving partial slabs etc */
  24. unsigned long flags;
  25. int size; /* The size of an object including meta data */
  26. int objsize; /* The size of an object without meta data */
  27. int offset; /* Free pointer offset. */
  28. unsigned int order;
  29. /*
  30. * Avoid an extra cache line for UP, SMP and for the node local to
  31. * struct kmem_cache.
  32. */
  33. struct kmem_cache_node local_node;
  34. /* Allocation and freeing of slabs */
  35. int objects; /* Number of objects in slab */
  36. int refcount; /* Refcount for slab cache destroy */
  37. void (*ctor)(void *, struct kmem_cache *, unsigned long);
  38. void (*dtor)(void *, struct kmem_cache *, unsigned long);
  39. int inuse; /* Offset to metadata */
  40. int align; /* Alignment */
  41. const char *name; /* Name (only for display!) */
  42. struct list_head list; /* List of slab caches */
  43. struct kobject kobj; /* For sysfs */
  44. #ifdef CONFIG_NUMA
  45. int defrag_ratio;
  46. struct kmem_cache_node *node[MAX_NUMNODES];
  47. #endif
  48. struct page *cpu_slab[NR_CPUS];
  49. };
  50. /*
  51. * Kmalloc subsystem.
  52. */
  53. #define KMALLOC_SHIFT_LOW 3
  54. #ifdef CONFIG_LARGE_ALLOCS
  55. #define KMALLOC_SHIFT_HIGH ((MAX_ORDER + PAGE_SHIFT) =< 25 ? \
  56. (MAX_ORDER + PAGE_SHIFT - 1) : 25)
  57. #else
  58. #if !defined(CONFIG_MMU) || NR_CPUS > 512 || MAX_NUMNODES > 256
  59. #define KMALLOC_SHIFT_HIGH 20
  60. #else
  61. #define KMALLOC_SHIFT_HIGH 18
  62. #endif
  63. #endif
  64. /*
  65. * We keep the general caches in an array of slab caches that are used for
  66. * 2^x bytes of allocations.
  67. */
  68. extern struct kmem_cache kmalloc_caches[KMALLOC_SHIFT_HIGH + 1];
  69. /*
  70. * Sorry that the following has to be that ugly but some versions of GCC
  71. * have trouble with constant propagation and loops.
  72. */
  73. static inline int kmalloc_index(int size)
  74. {
  75. /*
  76. * We should return 0 if size == 0 but we use the smallest object
  77. * here for SLAB legacy reasons.
  78. */
  79. WARN_ON_ONCE(size == 0);
  80. if (size >= (1 << KMALLOC_SHIFT_HIGH))
  81. return -1;
  82. if (size > 64 && size <= 96)
  83. return 1;
  84. if (size > 128 && size <= 192)
  85. return 2;
  86. if (size <= 8) return 3;
  87. if (size <= 16) return 4;
  88. if (size <= 32) return 5;
  89. if (size <= 64) return 6;
  90. if (size <= 128) return 7;
  91. if (size <= 256) return 8;
  92. if (size <= 512) return 9;
  93. if (size <= 1024) return 10;
  94. if (size <= 2 * 1024) return 11;
  95. if (size <= 4 * 1024) return 12;
  96. if (size <= 8 * 1024) return 13;
  97. if (size <= 16 * 1024) return 14;
  98. if (size <= 32 * 1024) return 15;
  99. if (size <= 64 * 1024) return 16;
  100. if (size <= 128 * 1024) return 17;
  101. if (size <= 256 * 1024) return 18;
  102. #if KMALLOC_SHIFT_HIGH > 18
  103. if (size <= 512 * 1024) return 19;
  104. if (size <= 1024 * 1024) return 20;
  105. #endif
  106. #if KMALLOC_SHIFT_HIGH > 20
  107. if (size <= 2 * 1024 * 1024) return 21;
  108. if (size <= 4 * 1024 * 1024) return 22;
  109. if (size <= 8 * 1024 * 1024) return 23;
  110. if (size <= 16 * 1024 * 1024) return 24;
  111. if (size <= 32 * 1024 * 1024) return 25;
  112. #endif
  113. return -1;
  114. /*
  115. * What we really wanted to do and cannot do because of compiler issues is:
  116. * int i;
  117. * for (i = KMALLOC_SHIFT_LOW; i <= KMALLOC_SHIFT_HIGH; i++)
  118. * if (size <= (1 << i))
  119. * return i;
  120. */
  121. }
  122. /*
  123. * Find the slab cache for a given combination of allocation flags and size.
  124. *
  125. * This ought to end up with a global pointer to the right cache
  126. * in kmalloc_caches.
  127. */
  128. static inline struct kmem_cache *kmalloc_slab(size_t size)
  129. {
  130. int index = kmalloc_index(size);
  131. if (index == 0)
  132. return NULL;
  133. if (index < 0) {
  134. /*
  135. * Generate a link failure. Would be great if we could
  136. * do something to stop the compile here.
  137. */
  138. extern void __kmalloc_size_too_large(void);
  139. __kmalloc_size_too_large();
  140. }
  141. return &kmalloc_caches[index];
  142. }
  143. #ifdef CONFIG_ZONE_DMA
  144. #define SLUB_DMA __GFP_DMA
  145. #else
  146. /* Disable DMA functionality */
  147. #define SLUB_DMA 0
  148. #endif
  149. static inline void *kmalloc(size_t size, gfp_t flags)
  150. {
  151. if (__builtin_constant_p(size) && !(flags & SLUB_DMA)) {
  152. struct kmem_cache *s = kmalloc_slab(size);
  153. if (!s)
  154. return NULL;
  155. return kmem_cache_alloc(s, flags);
  156. } else
  157. return __kmalloc(size, flags);
  158. }
  159. static inline void *kzalloc(size_t size, gfp_t flags)
  160. {
  161. if (__builtin_constant_p(size) && !(flags & SLUB_DMA)) {
  162. struct kmem_cache *s = kmalloc_slab(size);
  163. if (!s)
  164. return NULL;
  165. return kmem_cache_zalloc(s, flags);
  166. } else
  167. return __kzalloc(size, flags);
  168. }
  169. #ifdef CONFIG_NUMA
  170. extern void *__kmalloc_node(size_t size, gfp_t flags, int node);
  171. static inline void *kmalloc_node(size_t size, gfp_t flags, int node)
  172. {
  173. if (__builtin_constant_p(size) && !(flags & SLUB_DMA)) {
  174. struct kmem_cache *s = kmalloc_slab(size);
  175. if (!s)
  176. return NULL;
  177. return kmem_cache_alloc_node(s, flags, node);
  178. } else
  179. return __kmalloc_node(size, flags, node);
  180. }
  181. #endif
  182. #endif /* _LINUX_SLUB_DEF_H */