kmem_i.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. /*
  2. * Copyright (c) 2010-2018 Richard Braun.
  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 3 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
  15. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. */
  17. #ifndef KERN_KMEM_I_H
  18. #define KERN_KMEM_I_H
  19. #include <stdalign.h>
  20. #include <stddef.h>
  21. #include <kern/list.h>
  22. #include <kern/mutex.h>
  23. #include <machine/cpu.h>
  24. #if defined(CONFIG_SMP) && !defined(CONFIG_KMEM_NO_CPU_LAYER)
  25. #define KMEM_USE_CPU_LAYER
  26. #endif
  27. #ifdef KMEM_USE_CPU_LAYER
  28. /*
  29. * Per-processor cache of pre-constructed objects.
  30. *
  31. * The flags member is a read-only CPU-local copy of the parent cache flags.
  32. */
  33. struct kmem_cpu_pool {
  34. alignas(CPU_L1_SIZE) struct mutex lock;
  35. int flags;
  36. int size;
  37. int transfer_size;
  38. int nr_objs;
  39. void **array;
  40. };
  41. /*
  42. * When a cache is created, its CPU pool type is determined from the buffer
  43. * size. For small buffer sizes, many objects can be cached in a CPU pool.
  44. * Conversely, for large buffer sizes, this would incur much overhead, so only
  45. * a few objects are stored in a CPU pool.
  46. */
  47. struct kmem_cpu_pool_type {
  48. size_t buf_size;
  49. int array_size;
  50. size_t array_align;
  51. struct kmem_cache *array_cache;
  52. };
  53. #endif /* KMEM_USE_CPU_LAYER */
  54. /*
  55. * Buffer descriptor.
  56. *
  57. * For normal caches (i.e. without KMEM_CF_VERIFY), bufctls are located at the
  58. * end of (but inside) each buffer. If KMEM_CF_VERIFY is set, bufctls are
  59. * located after each buffer.
  60. *
  61. * When an object is allocated to a client, its bufctl isn't used. This memory
  62. * is instead used for redzoning if cache debugging is in effect.
  63. */
  64. union kmem_bufctl {
  65. union kmem_bufctl *next;
  66. unsigned long redzone;
  67. };
  68. /*
  69. * Redzone guard word.
  70. */
  71. #ifdef __LP64__
  72. #if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
  73. #define KMEM_REDZONE_WORD 0xfeedfacefeedfaceUL
  74. #else /* __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__ */
  75. #define KMEM_REDZONE_WORD 0xcefaedfecefaedfeUL
  76. #endif /* __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__ */
  77. #else /* __LP64__ */
  78. #if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
  79. #define KMEM_REDZONE_WORD 0xfeedfaceUL
  80. #else /* __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__ */
  81. #define KMEM_REDZONE_WORD 0xcefaedfeUL
  82. #endif /* __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__ */
  83. #endif /* __LP64__ */
  84. /*
  85. * Redzone byte for padding.
  86. */
  87. #define KMEM_REDZONE_BYTE 0xbb
  88. /*
  89. * Buffer tag.
  90. *
  91. * This structure is only used for KMEM_CF_VERIFY caches. It is located after
  92. * the bufctl and includes information about the state of the buffer it
  93. * describes (allocated or not). It should be thought of as a debugging
  94. * extension of the bufctl.
  95. */
  96. struct kmem_buftag {
  97. unsigned long state;
  98. };
  99. /*
  100. * Values the buftag state member can take.
  101. */
  102. #ifdef __LP64__
  103. #if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
  104. #define KMEM_BUFTAG_ALLOC 0xa110c8eda110c8edUL
  105. #define KMEM_BUFTAG_FREE 0xf4eeb10cf4eeb10cUL
  106. #else /* __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__ */
  107. #define KMEM_BUFTAG_ALLOC 0xedc810a1edc810a1UL
  108. #define KMEM_BUFTAG_FREE 0x0cb1eef40cb1eef4UL
  109. #endif /* __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__ */
  110. #else /* __LP64__ */
  111. #if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
  112. #define KMEM_BUFTAG_ALLOC 0xa110c8edUL
  113. #define KMEM_BUFTAG_FREE 0xf4eeb10cUL
  114. #else /* __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__ */
  115. #define KMEM_BUFTAG_ALLOC 0xedc810a1UL
  116. #define KMEM_BUFTAG_FREE 0x0cb1eef4UL
  117. #endif /* __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__ */
  118. #endif /* __LP64__ */
  119. /*
  120. * Free and uninitialized patterns.
  121. *
  122. * These values are unconditionally 64-bit wide since buffers are at least
  123. * 8-byte aligned.
  124. */
  125. #if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
  126. #define KMEM_FREE_PATTERN 0xdeadbeefdeadbeefULL
  127. #define KMEM_UNINIT_PATTERN 0xbaddcafebaddcafeULL
  128. #else /* __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__ */
  129. #define KMEM_FREE_PATTERN 0xefbeaddeefbeaddeULL
  130. #define KMEM_UNINIT_PATTERN 0xfecaddbafecaddbaULL
  131. #endif /* __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__ */
  132. /*
  133. * Page-aligned collection of unconstructed buffers.
  134. *
  135. * This structure is either allocated from the slab cache, or, when internal
  136. * fragmentation allows it, or if forced by the cache creator, from the slab
  137. * it describes.
  138. */
  139. struct kmem_slab {
  140. struct list node;
  141. unsigned long nr_refs;
  142. union kmem_bufctl *first_free;
  143. void *addr;
  144. };
  145. /*
  146. * Cache name buffer size.
  147. */
  148. #define KMEM_NAME_SIZE 32
  149. /*
  150. * Cache flags.
  151. *
  152. * The flags don't change once set and can be tested without locking.
  153. */
  154. #define KMEM_CF_SLAB_EXTERNAL 0x1 /* Slab data is off slab */
  155. #define KMEM_CF_VERIFY 0x2 /* Debugging facilities enabled */
  156. /*
  157. * Cache of objects.
  158. *
  159. * Locking order : cpu_pool -> cache. CPU pools locking is ordered by CPU ID.
  160. */
  161. struct kmem_cache {
  162. #ifdef KMEM_USE_CPU_LAYER
  163. /* CPU pool layer */
  164. struct kmem_cpu_pool cpu_pools[CONFIG_MAX_CPUS];
  165. struct kmem_cpu_pool_type *cpu_pool_type;
  166. #endif /* KMEM_USE_CPU_LAYER */
  167. /* Slab layer */
  168. struct mutex lock;
  169. struct list node; /* Cache list linkage */
  170. struct list partial_slabs;
  171. struct list free_slabs;
  172. int flags;
  173. size_t obj_size; /* User-provided size */
  174. size_t align;
  175. size_t buf_size; /* Aligned object size */
  176. size_t bufctl_dist; /* Distance from buffer to bufctl */
  177. size_t slab_size;
  178. size_t color;
  179. size_t color_max;
  180. unsigned long bufs_per_slab;
  181. unsigned long nr_objs; /* Number of allocated objects */
  182. unsigned long nr_bufs; /* Total number of buffers */
  183. unsigned long nr_slabs;
  184. unsigned long nr_free_slabs;
  185. kmem_ctor_fn_t ctor;
  186. char name[KMEM_NAME_SIZE];
  187. size_t buftag_dist; /* Distance from buffer to buftag */
  188. size_t redzone_pad; /* Bytes from end of object to redzone word */
  189. };
  190. #endif /* KERN_KMEM_I_H */