slab.h 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. /*
  2. * Copyright (c) 2011 Free Software Foundation.
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 2 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License along
  15. * with this program; if not, write to the Free Software Foundation, Inc.,
  16. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  17. */
  18. /*
  19. * Copyright (c) 2010, 2011 Richard Braun.
  20. * All rights reserved.
  21. *
  22. * Redistribution and use in source and binary forms, with or without
  23. * modification, are permitted provided that the following conditions
  24. * are met:
  25. * 1. Redistributions of source code must retain the above copyright
  26. * notice, this list of conditions and the following disclaimer.
  27. * 2. Redistributions in binary form must reproduce the above copyright
  28. * notice, this list of conditions and the following disclaimer in the
  29. * documentation and/or other materials provided with the distribution.
  30. *
  31. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  32. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  33. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  34. * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
  35. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  36. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  37. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  38. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  39. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  40. * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  41. *
  42. *
  43. * Object caching memory allocator.
  44. */
  45. #ifndef _KERN_SLAB_H
  46. #define _KERN_SLAB_H
  47. #include <cache.h>
  48. #include <kern/cpu_number.h>
  49. #include <kern/lock.h>
  50. #include <kern/list.h>
  51. #include <kern/rbtree.h>
  52. #include <mach/machine/vm_types.h>
  53. #include <sys/types.h>
  54. #include <vm/vm_types.h>
  55. struct gnu_kmem_cache;
  56. #if SLAB_USE_CPU_POOLS
  57. /*
  58. * Per-processor cache of pre-constructed objects.
  59. *
  60. * The flags member is a read-only CPU-local copy of the parent cache flags.
  61. */
  62. struct gnu_kmem_cpu_pool {
  63. simple_lock_data_t lock;
  64. int flags;
  65. int size;
  66. int transfer_size;
  67. int nr_objs;
  68. void **array;
  69. } __attribute__((aligned(CPU_L1_SIZE)));
  70. /*
  71. * When a cache is created, its CPU pool type is determined from the buffer
  72. * size. For small buffer sizes, many objects can be cached in a CPU pool.
  73. * Conversely, for large buffer sizes, this would incur much overhead, so only
  74. * a few objects are stored in a CPU pool.
  75. */
  76. struct gnu_kmem_cpu_pool_type {
  77. size_t buf_size;
  78. int array_size;
  79. size_t array_align;
  80. struct gnu_kmem_cache *array_cache;
  81. };
  82. #endif /* SLAB_USE_CPU_POOLS */
  83. /*
  84. * Buffer descriptor.
  85. *
  86. * For normal caches (i.e. without SLAB_CF_VERIFY), bufctls are located at the
  87. * end of (but inside) each buffer. If SLAB_CF_VERIFY is set, bufctls are
  88. * located after each buffer.
  89. *
  90. * When an object is allocated to a client, its bufctl isn't used. This memory
  91. * is instead used for redzoning if cache debugging is in effect.
  92. */
  93. union gnu_kmem_bufctl {
  94. union gnu_kmem_bufctl *next;
  95. unsigned long redzone;
  96. };
  97. /*
  98. * Buffer tag.
  99. *
  100. * This structure is only used for SLAB_CF_VERIFY caches. It is located after
  101. * the bufctl and includes information about the state of the buffer it
  102. * describes (allocated or not). It should be thought of as a debugging
  103. * extension of the bufctl.
  104. */
  105. struct gnu_kmem_buftag {
  106. unsigned long state;
  107. };
  108. /*
  109. * Page-aligned collection of unconstructed buffers.
  110. */
  111. struct gnu_kmem_slab {
  112. struct gnu_kmem_cache *cache;
  113. struct list list_node;
  114. struct rbtree_node tree_node;
  115. unsigned long nr_refs;
  116. union gnu_kmem_bufctl *first_free;
  117. void *addr;
  118. };
  119. /*
  120. * Cache name buffer size. The size is chosen so that struct
  121. * gnu_kmem_cache fits into two cache lines. The size of a cache line on
  122. * a typical CPU is 64 bytes.
  123. */
  124. #define GNU_KMEM_CACHE_NAME_SIZE 24
  125. /*
  126. * Cache of objects.
  127. *
  128. * Locking order : cpu_pool -> cache. CPU pools locking is ordered by CPU ID.
  129. *
  130. * Currently, SLAB_USE_CPU_POOLS is not defined. GNU_KMEM_CACHE_NAME_SIZE
  131. * is chosen so that the struct fits into two cache lines. The first
  132. * cache line contains all hot fields.
  133. */
  134. #if 0
  135. struct gnu_kmem_cache {
  136. #if SLAB_USE_CPU_POOLS
  137. /* CPU pool layer */
  138. struct gnu_kmem_cpu_pool cpu_pools[NCPUS];
  139. struct gnu_kmem_cpu_pool_type *cpu_pool_type;
  140. #endif /* SLAB_USE_CPU_POOLS */
  141. /* Slab layer */
  142. simple_lock_data_t lock;
  143. struct list node; /* Cache list linkage */
  144. struct list partial_slabs;
  145. struct list free_slabs;
  146. struct rbtree active_slabs;
  147. int flags;
  148. size_t bufctl_dist; /* Distance from buffer to bufctl */
  149. size_t slab_size;
  150. unsigned long bufs_per_slab;
  151. unsigned long nr_objs; /* Number of allocated objects */
  152. unsigned long nr_free_slabs;
  153. gnu_kmem_cache_ctor_t ctor;
  154. /* All fields below are cold */
  155. size_t obj_size; /* User-provided size */
  156. /* Assuming ! SLAB_USE_CPU_POOLS, here is the cacheline boundary */
  157. size_t align;
  158. size_t buf_size; /* Aligned object size */
  159. size_t color;
  160. size_t color_max;
  161. unsigned long nr_bufs; /* Total number of buffers */
  162. unsigned long nr_slabs;
  163. char name[GNU_KMEM_CACHE_NAME_SIZE];
  164. size_t buftag_dist; /* Distance from buffer to buftag */
  165. size_t redzone_pad; /* Bytes from end of object to redzone word */
  166. } __cacheline_aligned;
  167. #endif
  168. /*
  169. * Mach-style declarations for struct gnu_kmem_cache.
  170. */
  171. typedef struct gnu_kmem_cache *gnu_kmem_cache_t;
  172. #define GNU_KMEM_CACHE_NULL ((gnu_kmem_cache_t) 0)
  173. /*
  174. * Cache initialization flags.
  175. */
  176. #define GNU_KMEM_CACHE_NOOFFSLAB 0x1 /* Don't allocate external slab data */
  177. #define GNU_KMEM_CACHE_PHYSMEM 0x2 /* Allocate from physical memory */
  178. #define GNU_KMEM_CACHE_VERIFY 0x4 /* Use debugging facilities */
  179. /*
  180. * Initialize a cache.
  181. */
  182. void gnu_kmem_cache_init(struct gnu_kmem_cache *cache, const char *name,
  183. size_t obj_size, size_t align,
  184. gnu_kmem_cache_ctor_t ctor, int flags);
  185. /*
  186. * Allocate an object from a cache.
  187. */
  188. vm_offset_t gnu_kmem_cache_alloc(struct gnu_kmem_cache *cache);
  189. /*
  190. * Release an object to its cache.
  191. */
  192. void gnu_kmem_cache_free(struct gnu_kmem_cache *cache, vm_offset_t obj);
  193. /*
  194. * Initialize the memory allocator module.
  195. */
  196. void slab_bootstrap(void);
  197. void slab_init(void);
  198. /*
  199. * Release free slabs to the VM system.
  200. */
  201. void slab_collect(void);
  202. /*
  203. * Display a summary of all kernel caches.
  204. */
  205. void slab_info(void);
  206. #if MACH_KDB
  207. void db_show_slab_info(void);
  208. #endif /* MACH_KDB */
  209. #endif /* _KERN_SLAB_H */