kmem.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. /*
  2. * Copyright (c) 2010-2019 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. *
  18. * Object caching and general purpose memory allocator.
  19. */
  20. #ifndef KERN_KMEM_H
  21. #define KERN_KMEM_H
  22. #include <stddef.h>
  23. #include <kern/adaptive_lock.h>
  24. #include <kern/init.h>
  25. #include <kern/list.h>
  26. #include <kern/stream.h>
  27. #include <machine/cpu.h>
  28. /*
  29. * Type for constructor functions.
  30. *
  31. * The pre-constructed state of an object is supposed to include only
  32. * elements such as e.g. linked lists, locks, reference counters. Therefore
  33. * constructors are expected to 1) never block, 2) never fail and 3) not
  34. * need any user-provided data. As a result, object construction never
  35. * performs dynamic resource allocation, which removes the need for
  36. * destructors.
  37. */
  38. typedef void (*kmem_ctor_fn_t) (void *);
  39. #if defined (CONFIG_SMP) && !defined (CONFIG_KMEM_NO_CPU_LAYER)
  40. #define KMEM_USE_CPU_LAYER
  41. #endif
  42. #ifdef KMEM_USE_CPU_LAYER
  43. /*
  44. * Per-processor cache of pre-constructed objects.
  45. *
  46. * The flags member is a read-only CPU-local copy of the parent cache flags.
  47. */
  48. struct kmem_cpu_pool
  49. {
  50. __cacheline_aligned struct adaptive_lock lock;
  51. int flags;
  52. int size;
  53. int transfer_size;
  54. int nr_objs;
  55. void **array;
  56. };
  57. /*
  58. * When a cache is created, its CPU pool type is determined from the buffer
  59. * size. For small buffer sizes, many objects can be cached in a CPU pool.
  60. * Conversely, for large buffer sizes, this would incur much overhead, so only
  61. * a few objects are stored in a CPU pool.
  62. */
  63. struct kmem_cpu_pool_type
  64. {
  65. size_t buf_size;
  66. int array_size;
  67. size_t array_align;
  68. struct kmem_cache *array_cache;
  69. };
  70. #endif // KMEM_USE_CPU_LAYER
  71. // Cache name buffer size.
  72. #define KMEM_NAME_SIZE 32
  73. /*
  74. * Cache of objects.
  75. *
  76. * Locking order : cpu_pool -> cache. CPU pools locking is ordered by CPU ID.
  77. */
  78. struct kmem_cache
  79. {
  80. #ifdef KMEM_USE_CPU_LAYER
  81. // CPU pool layer.
  82. struct kmem_cpu_pool cpu_pools[CONFIG_MAX_CPUS];
  83. struct kmem_cpu_pool_type *cpu_pool_type;
  84. #endif
  85. // Slab layer.
  86. struct adaptive_lock lock;
  87. struct list node; // Cache list linkage.
  88. struct list partial_slabs;
  89. struct list free_slabs;
  90. int flags;
  91. size_t obj_size; // User-provided size.
  92. size_t align;
  93. size_t buf_size; // Aligned object size.
  94. size_t bufctl_dist; // Distance from buffer to bufctl.
  95. size_t slab_size;
  96. size_t color;
  97. size_t color_max;
  98. size_t bufs_per_slab;
  99. size_t nr_objs; // Number of allocated objects.
  100. size_t nr_bufs; // Total number of buffers.
  101. size_t nr_slabs;
  102. size_t nr_free_slabs;
  103. kmem_ctor_fn_t ctor;
  104. char name[KMEM_NAME_SIZE];
  105. size_t buftag_dist; // Distance from buffer to buftag.
  106. size_t redzone_pad; // Bytes from end of object to redzone word.
  107. };
  108. // Cache creation flags.
  109. #define KMEM_CACHE_NOOFFSLAB 0x1 // Don't allocate external slab data.
  110. #define KMEM_CACHE_PAGE_ONLY 0x2 // Allocate slabs from the page allocator.
  111. #define KMEM_CACHE_VERIFY 0x4 // Use debugging facilities.
  112. // Cache allocation flags.
  113. #define KMEM_ALLOC_SLEEP 0x1
  114. /*
  115. * Initialize a cache.
  116. *
  117. * Slabs may be allocated either from the page allocator or from kernel
  118. * virtual memory, unless KMEM_CACHE_PAGE_ONLY is set.
  119. */
  120. void kmem_cache_init (struct kmem_cache *cache, const char *name,
  121. size_t obj_size, size_t align,
  122. kmem_ctor_fn_t ctor, int flags);
  123. // Allocate an object from a cache.
  124. void* kmem_cache_alloc2 (struct kmem_cache *cache, unsigned int flags);
  125. static inline void*
  126. kmem_cache_alloc (struct kmem_cache *cache)
  127. {
  128. return (kmem_cache_alloc2 (cache, 0));
  129. }
  130. // Release an object to its cache.
  131. void kmem_cache_free (struct kmem_cache *cache, void *obj);
  132. /*
  133. * Display internal cache information.
  134. *
  135. * If cache is NULL, this function displays all managed caches.
  136. */
  137. void kmem_cache_info (struct kmem_cache *cache, struct stream *stream);
  138. // Allocate size bytes of uninitialized memory.
  139. void* kmem_alloc2 (size_t size, unsigned int flags);
  140. static inline void*
  141. kmem_alloc (size_t size)
  142. {
  143. return (kmem_alloc2 (size, 0));
  144. }
  145. // Allocate size bytes of zeroed memory.
  146. void* kmem_zalloc (size_t size);
  147. /*
  148. * Release memory obtained with kmem_alloc() or kmem_zalloc().
  149. *
  150. * The size argument must strictly match the value given at allocation time.
  151. */
  152. void kmem_free (void *ptr, size_t size);
  153. // Display global kernel memory information.
  154. void kmem_info (struct stream *stream);
  155. /*
  156. * This init operation provides :
  157. * - allocation from caches backed by the page allocator
  158. */
  159. INIT_OP_DECLARE (kmem_bootstrap);
  160. /*
  161. * This init operation provides :
  162. * - allocation from all caches
  163. */
  164. INIT_OP_DECLARE (kmem_setup);
  165. #endif