linux.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  1. // SPDX-License-Identifier: GPL-2.0
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <malloc.h>
  5. #include <pthread.h>
  6. #include <unistd.h>
  7. #include <assert.h>
  8. #include <linux/gfp.h>
  9. #include <linux/poison.h>
  10. #include <linux/slab.h>
  11. #include <linux/radix-tree.h>
  12. #include <urcu/uatomic.h>
  13. int nr_allocated;
  14. int preempt_count;
  15. int test_verbose;
  16. void kmem_cache_set_callback(struct kmem_cache *cachep, void (*callback)(void *))
  17. {
  18. cachep->callback = callback;
  19. }
  20. void kmem_cache_set_private(struct kmem_cache *cachep, void *private)
  21. {
  22. cachep->private = private;
  23. }
  24. void kmem_cache_set_non_kernel(struct kmem_cache *cachep, unsigned int val)
  25. {
  26. cachep->non_kernel = val;
  27. }
  28. unsigned long kmem_cache_get_alloc(struct kmem_cache *cachep)
  29. {
  30. return cachep->size * cachep->nr_allocated;
  31. }
  32. unsigned long kmem_cache_nr_allocated(struct kmem_cache *cachep)
  33. {
  34. return cachep->nr_allocated;
  35. }
  36. unsigned long kmem_cache_nr_tallocated(struct kmem_cache *cachep)
  37. {
  38. return cachep->nr_tallocated;
  39. }
  40. void kmem_cache_zero_nr_tallocated(struct kmem_cache *cachep)
  41. {
  42. cachep->nr_tallocated = 0;
  43. }
  44. void *kmem_cache_alloc_lru(struct kmem_cache *cachep, struct list_lru *lru,
  45. int gfp)
  46. {
  47. void *p;
  48. if (cachep->exec_callback) {
  49. if (cachep->callback)
  50. cachep->callback(cachep->private);
  51. cachep->exec_callback = false;
  52. }
  53. if (!(gfp & __GFP_DIRECT_RECLAIM)) {
  54. if (!cachep->non_kernel) {
  55. if (cachep->callback)
  56. cachep->exec_callback = true;
  57. return NULL;
  58. }
  59. cachep->non_kernel--;
  60. }
  61. pthread_mutex_lock(&cachep->lock);
  62. if (cachep->nr_objs) {
  63. struct radix_tree_node *node = cachep->objs;
  64. cachep->nr_objs--;
  65. cachep->objs = node->parent;
  66. pthread_mutex_unlock(&cachep->lock);
  67. node->parent = NULL;
  68. p = node;
  69. } else {
  70. pthread_mutex_unlock(&cachep->lock);
  71. if (cachep->align) {
  72. if (posix_memalign(&p, cachep->align, cachep->size) < 0)
  73. return NULL;
  74. } else {
  75. p = malloc(cachep->size);
  76. }
  77. if (cachep->ctor)
  78. cachep->ctor(p);
  79. else if (gfp & __GFP_ZERO)
  80. memset(p, 0, cachep->size);
  81. }
  82. uatomic_inc(&cachep->nr_allocated);
  83. uatomic_inc(&nr_allocated);
  84. uatomic_inc(&cachep->nr_tallocated);
  85. if (kmalloc_verbose)
  86. printf("Allocating %p from slab\n", p);
  87. return p;
  88. }
  89. void __kmem_cache_free_locked(struct kmem_cache *cachep, void *objp)
  90. {
  91. assert(objp);
  92. if (cachep->nr_objs > 10 || cachep->align) {
  93. memset(objp, POISON_FREE, cachep->size);
  94. free(objp);
  95. } else {
  96. struct radix_tree_node *node = objp;
  97. cachep->nr_objs++;
  98. node->parent = cachep->objs;
  99. cachep->objs = node;
  100. }
  101. }
  102. void kmem_cache_free_locked(struct kmem_cache *cachep, void *objp)
  103. {
  104. uatomic_dec(&nr_allocated);
  105. uatomic_dec(&cachep->nr_allocated);
  106. if (kmalloc_verbose)
  107. printf("Freeing %p to slab\n", objp);
  108. __kmem_cache_free_locked(cachep, objp);
  109. }
  110. void kmem_cache_free(struct kmem_cache *cachep, void *objp)
  111. {
  112. pthread_mutex_lock(&cachep->lock);
  113. kmem_cache_free_locked(cachep, objp);
  114. pthread_mutex_unlock(&cachep->lock);
  115. }
  116. void kmem_cache_free_bulk(struct kmem_cache *cachep, size_t size, void **list)
  117. {
  118. if (kmalloc_verbose)
  119. pr_debug("Bulk free %p[0-%zu]\n", list, size - 1);
  120. if (cachep->exec_callback) {
  121. if (cachep->callback)
  122. cachep->callback(cachep->private);
  123. cachep->exec_callback = false;
  124. }
  125. pthread_mutex_lock(&cachep->lock);
  126. for (int i = 0; i < size; i++)
  127. kmem_cache_free_locked(cachep, list[i]);
  128. pthread_mutex_unlock(&cachep->lock);
  129. }
  130. void kmem_cache_shrink(struct kmem_cache *cachep)
  131. {
  132. }
  133. int kmem_cache_alloc_bulk(struct kmem_cache *cachep, gfp_t gfp, size_t size,
  134. void **p)
  135. {
  136. size_t i;
  137. if (kmalloc_verbose)
  138. pr_debug("Bulk alloc %zu\n", size);
  139. pthread_mutex_lock(&cachep->lock);
  140. if (cachep->nr_objs >= size) {
  141. struct radix_tree_node *node;
  142. for (i = 0; i < size; i++) {
  143. if (!(gfp & __GFP_DIRECT_RECLAIM)) {
  144. if (!cachep->non_kernel)
  145. break;
  146. cachep->non_kernel--;
  147. }
  148. node = cachep->objs;
  149. cachep->nr_objs--;
  150. cachep->objs = node->parent;
  151. p[i] = node;
  152. node->parent = NULL;
  153. }
  154. pthread_mutex_unlock(&cachep->lock);
  155. } else {
  156. pthread_mutex_unlock(&cachep->lock);
  157. for (i = 0; i < size; i++) {
  158. if (!(gfp & __GFP_DIRECT_RECLAIM)) {
  159. if (!cachep->non_kernel)
  160. break;
  161. cachep->non_kernel--;
  162. }
  163. if (cachep->align) {
  164. if (posix_memalign(&p[i], cachep->align,
  165. cachep->size) < 0)
  166. break;
  167. } else {
  168. p[i] = malloc(cachep->size);
  169. if (!p[i])
  170. break;
  171. }
  172. if (cachep->ctor)
  173. cachep->ctor(p[i]);
  174. else if (gfp & __GFP_ZERO)
  175. memset(p[i], 0, cachep->size);
  176. }
  177. }
  178. if (i < size) {
  179. size = i;
  180. pthread_mutex_lock(&cachep->lock);
  181. for (i = 0; i < size; i++)
  182. __kmem_cache_free_locked(cachep, p[i]);
  183. pthread_mutex_unlock(&cachep->lock);
  184. if (cachep->callback)
  185. cachep->exec_callback = true;
  186. return 0;
  187. }
  188. for (i = 0; i < size; i++) {
  189. uatomic_inc(&nr_allocated);
  190. uatomic_inc(&cachep->nr_allocated);
  191. uatomic_inc(&cachep->nr_tallocated);
  192. if (kmalloc_verbose)
  193. printf("Allocating %p from slab\n", p[i]);
  194. }
  195. return size;
  196. }
  197. struct kmem_cache *
  198. __kmem_cache_create_args(const char *name, unsigned int size,
  199. struct kmem_cache_args *args,
  200. unsigned int flags)
  201. {
  202. struct kmem_cache *ret = malloc(sizeof(*ret));
  203. pthread_mutex_init(&ret->lock, NULL);
  204. ret->size = size;
  205. ret->align = args->align;
  206. ret->sheaf_capacity = args->sheaf_capacity;
  207. ret->nr_objs = 0;
  208. ret->nr_allocated = 0;
  209. ret->nr_tallocated = 0;
  210. ret->objs = NULL;
  211. ret->ctor = args->ctor;
  212. ret->non_kernel = 0;
  213. ret->exec_callback = false;
  214. ret->callback = NULL;
  215. ret->private = NULL;
  216. return ret;
  217. }
  218. struct slab_sheaf *
  219. kmem_cache_prefill_sheaf(struct kmem_cache *s, gfp_t gfp, unsigned int size)
  220. {
  221. struct slab_sheaf *sheaf;
  222. unsigned int capacity;
  223. if (s->exec_callback) {
  224. if (s->callback)
  225. s->callback(s->private);
  226. s->exec_callback = false;
  227. }
  228. capacity = max(size, s->sheaf_capacity);
  229. sheaf = calloc(1, sizeof(*sheaf) + sizeof(void *) * capacity);
  230. if (!sheaf)
  231. return NULL;
  232. sheaf->cache = s;
  233. sheaf->capacity = capacity;
  234. sheaf->size = kmem_cache_alloc_bulk(s, gfp, size, sheaf->objects);
  235. if (!sheaf->size) {
  236. free(sheaf);
  237. return NULL;
  238. }
  239. return sheaf;
  240. }
  241. int kmem_cache_refill_sheaf(struct kmem_cache *s, gfp_t gfp,
  242. struct slab_sheaf **sheafp, unsigned int size)
  243. {
  244. struct slab_sheaf *sheaf = *sheafp;
  245. int refill;
  246. if (sheaf->size >= size)
  247. return 0;
  248. if (size > sheaf->capacity) {
  249. sheaf = kmem_cache_prefill_sheaf(s, gfp, size);
  250. if (!sheaf)
  251. return -ENOMEM;
  252. kmem_cache_return_sheaf(s, gfp, *sheafp);
  253. *sheafp = sheaf;
  254. return 0;
  255. }
  256. refill = kmem_cache_alloc_bulk(s, gfp, size - sheaf->size,
  257. &sheaf->objects[sheaf->size]);
  258. if (!refill)
  259. return -ENOMEM;
  260. sheaf->size += refill;
  261. return 0;
  262. }
  263. void kmem_cache_return_sheaf(struct kmem_cache *s, gfp_t gfp,
  264. struct slab_sheaf *sheaf)
  265. {
  266. if (sheaf->size)
  267. kmem_cache_free_bulk(s, sheaf->size, &sheaf->objects[0]);
  268. free(sheaf);
  269. }
  270. void *
  271. kmem_cache_alloc_from_sheaf(struct kmem_cache *s, gfp_t gfp,
  272. struct slab_sheaf *sheaf)
  273. {
  274. void *obj;
  275. if (sheaf->size == 0) {
  276. printf("Nothing left in sheaf!\n");
  277. return NULL;
  278. }
  279. obj = sheaf->objects[--sheaf->size];
  280. sheaf->objects[sheaf->size] = NULL;
  281. return obj;
  282. }
  283. /*
  284. * Test the test infrastructure for kem_cache_alloc/free and bulk counterparts.
  285. */
  286. void test_kmem_cache_bulk(void)
  287. {
  288. int i;
  289. void *list[12];
  290. static struct kmem_cache *test_cache, *test_cache2;
  291. /*
  292. * Testing the bulk allocators without aligned kmem_cache to force the
  293. * bulk alloc/free to reuse
  294. */
  295. test_cache = kmem_cache_create("test_cache", 256, 0, SLAB_PANIC, NULL);
  296. for (i = 0; i < 5; i++)
  297. list[i] = kmem_cache_alloc(test_cache, __GFP_DIRECT_RECLAIM);
  298. for (i = 0; i < 5; i++)
  299. kmem_cache_free(test_cache, list[i]);
  300. assert(test_cache->nr_objs == 5);
  301. kmem_cache_alloc_bulk(test_cache, __GFP_DIRECT_RECLAIM, 5, list);
  302. kmem_cache_free_bulk(test_cache, 5, list);
  303. for (i = 0; i < 12 ; i++)
  304. list[i] = kmem_cache_alloc(test_cache, __GFP_DIRECT_RECLAIM);
  305. for (i = 0; i < 12; i++)
  306. kmem_cache_free(test_cache, list[i]);
  307. /* The last free will not be kept around */
  308. assert(test_cache->nr_objs == 11);
  309. /* Aligned caches will immediately free */
  310. test_cache2 = kmem_cache_create("test_cache2", 128, 128, SLAB_PANIC, NULL);
  311. kmem_cache_alloc_bulk(test_cache2, __GFP_DIRECT_RECLAIM, 10, list);
  312. kmem_cache_free_bulk(test_cache2, 10, list);
  313. assert(!test_cache2->nr_objs);
  314. }