slub_def.h 4.8 KB

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