Commit | Line | Data |
---|---|---|
d24c33a4 JA |
1 | /* |
2 | * simple memory allocator, backed by mmap() so that it hands out memory | |
3 | * that can be shared across processes and threads | |
4 | */ | |
5 | #include <sys/mman.h> | |
6 | #include <stdio.h> | |
7 | #include <stdlib.h> | |
8 | #include <assert.h> | |
9 | #include <string.h> | |
10 | #include <unistd.h> | |
e43606c2 | 11 | #include <inttypes.h> |
d24c33a4 JA |
12 | #include <sys/types.h> |
13 | #include <limits.h> | |
3a8600b4 | 14 | #include <fcntl.h> |
d24c33a4 | 15 | |
6548f47f | 16 | #include "mutex.h" |
b3268b92 | 17 | #include "arch/arch.h" |
3a8600b4 | 18 | #include "os/os.h" |
10aa136b | 19 | #include "smalloc.h" |
b0f0326a | 20 | #include "log.h" |
d24c33a4 | 21 | |
55f6491d | 22 | #define SMALLOC_REDZONE /* define to detect memory corruption */ |
d24c33a4 | 23 | |
ec996e9c JA |
24 | #define SMALLOC_BPB 32 /* block size, bytes-per-bit in bitmap */ |
25 | #define SMALLOC_BPI (sizeof(unsigned int) * 8) | |
26 | #define SMALLOC_BPL (SMALLOC_BPB * SMALLOC_BPI) | |
27 | ||
23bd40f9 | 28 | #define INITIAL_SIZE 16*1024*1024 /* new pool size */ |
85492cb8 | 29 | #define MAX_POOLS 8 /* maximum number of pools to setup */ |
d24c33a4 | 30 | |
55f6491d JA |
31 | #define SMALLOC_PRE_RED 0xdeadbeefU |
32 | #define SMALLOC_POST_RED 0x5aa55aa5U | |
55f6491d | 33 | |
2b386d25 | 34 | unsigned int smalloc_pool_size = INITIAL_SIZE; |
10aa136b | 35 | static const int int_mask = sizeof(int) - 1; |
2b386d25 | 36 | |
d24c33a4 | 37 | struct pool { |
6548f47f | 38 | struct fio_mutex *lock; /* protects this pool */ |
d24c33a4 | 39 | void *map; /* map of blocks */ |
ec996e9c | 40 | unsigned int *bitmap; /* blocks free/busy map */ |
a3ebe7e0 JA |
41 | size_t free_blocks; /* free blocks */ |
42 | size_t nr_blocks; /* total blocks */ | |
43 | size_t next_non_full; | |
44 | size_t mmap_size; | |
ec996e9c JA |
45 | }; |
46 | ||
47 | struct block_hdr { | |
a3ebe7e0 | 48 | size_t size; |
ec996e9c JA |
49 | #ifdef SMALLOC_REDZONE |
50 | unsigned int prered; | |
51 | #endif | |
d24c33a4 JA |
52 | }; |
53 | ||
54 | static struct pool mp[MAX_POOLS]; | |
55 | static unsigned int nr_pools; | |
56 | static unsigned int last_pool; | |
d7df1d13 | 57 | static struct fio_rwlock *lock; |
d24c33a4 | 58 | |
d24c33a4 JA |
59 | static inline void pool_lock(struct pool *pool) |
60 | { | |
2e3e31e3 | 61 | fio_mutex_down(pool->lock); |
d24c33a4 JA |
62 | } |
63 | ||
64 | static inline void pool_unlock(struct pool *pool) | |
65 | { | |
2e3e31e3 | 66 | fio_mutex_up(pool->lock); |
d24c33a4 JA |
67 | } |
68 | ||
65864cf7 | 69 | static inline void global_read_lock(void) |
d24c33a4 | 70 | { |
d7df1d13 | 71 | fio_rwlock_read(lock); |
d24c33a4 JA |
72 | } |
73 | ||
65864cf7 | 74 | static inline void global_read_unlock(void) |
d24c33a4 | 75 | { |
d7df1d13 | 76 | fio_rwlock_unlock(lock); |
65864cf7 JA |
77 | } |
78 | ||
79 | static inline void global_write_lock(void) | |
80 | { | |
d7df1d13 | 81 | fio_rwlock_write(lock); |
65864cf7 JA |
82 | } |
83 | ||
84 | static inline void global_write_unlock(void) | |
85 | { | |
d7df1d13 | 86 | fio_rwlock_unlock(lock); |
d24c33a4 JA |
87 | } |
88 | ||
d24c33a4 JA |
89 | static inline int ptr_valid(struct pool *pool, void *ptr) |
90 | { | |
dcb69098 | 91 | unsigned int pool_size = pool->nr_blocks * SMALLOC_BPL; |
ec996e9c JA |
92 | |
93 | return (ptr >= pool->map) && (ptr < pool->map + pool_size); | |
d24c33a4 JA |
94 | } |
95 | ||
a3ebe7e0 | 96 | static inline size_t size_to_blocks(size_t size) |
808e9ea8 JA |
97 | { |
98 | return (size + SMALLOC_BPB - 1) / SMALLOC_BPB; | |
99 | } | |
100 | ||
dcb69098 | 101 | static int blocks_iter(struct pool *pool, unsigned int pool_idx, |
a3ebe7e0 | 102 | unsigned int idx, size_t nr_blocks, |
ec996e9c | 103 | int (*func)(unsigned int *map, unsigned int mask)) |
d24c33a4 | 104 | { |
dcb69098 | 105 | |
ec996e9c JA |
106 | while (nr_blocks) { |
107 | unsigned int this_blocks, mask; | |
dcb69098 JA |
108 | unsigned int *map; |
109 | ||
110 | if (pool_idx >= pool->nr_blocks) | |
111 | return 0; | |
112 | ||
113 | map = &pool->bitmap[pool_idx]; | |
ec996e9c JA |
114 | |
115 | this_blocks = nr_blocks; | |
116 | if (this_blocks + idx > SMALLOC_BPI) { | |
117 | this_blocks = SMALLOC_BPI - idx; | |
118 | idx = SMALLOC_BPI - this_blocks; | |
119 | } | |
120 | ||
121 | if (this_blocks == SMALLOC_BPI) | |
122 | mask = -1U; | |
123 | else | |
124 | mask = ((1U << this_blocks) - 1) << idx; | |
125 | ||
126 | if (!func(map, mask)) | |
127 | return 0; | |
128 | ||
129 | nr_blocks -= this_blocks; | |
130 | idx = 0; | |
dcb69098 | 131 | pool_idx++; |
ec996e9c JA |
132 | } |
133 | ||
134 | return 1; | |
d24c33a4 JA |
135 | } |
136 | ||
ec996e9c | 137 | static int mask_cmp(unsigned int *map, unsigned int mask) |
d24c33a4 | 138 | { |
ec996e9c | 139 | return !(*map & mask); |
d24c33a4 JA |
140 | } |
141 | ||
ec996e9c | 142 | static int mask_clear(unsigned int *map, unsigned int mask) |
d24c33a4 | 143 | { |
dcb69098 | 144 | assert((*map & mask) == mask); |
ec996e9c JA |
145 | *map &= ~mask; |
146 | return 1; | |
d24c33a4 JA |
147 | } |
148 | ||
ec996e9c | 149 | static int mask_set(unsigned int *map, unsigned int mask) |
d24c33a4 | 150 | { |
dcb69098 | 151 | assert(!(*map & mask)); |
ec996e9c JA |
152 | *map |= mask; |
153 | return 1; | |
d24c33a4 JA |
154 | } |
155 | ||
dcb69098 | 156 | static int blocks_free(struct pool *pool, unsigned int pool_idx, |
a3ebe7e0 | 157 | unsigned int idx, size_t nr_blocks) |
d24c33a4 | 158 | { |
dcb69098 | 159 | return blocks_iter(pool, pool_idx, idx, nr_blocks, mask_cmp); |
d24c33a4 JA |
160 | } |
161 | ||
dcb69098 | 162 | static void set_blocks(struct pool *pool, unsigned int pool_idx, |
a3ebe7e0 | 163 | unsigned int idx, size_t nr_blocks) |
d24c33a4 | 164 | { |
dcb69098 | 165 | blocks_iter(pool, pool_idx, idx, nr_blocks, mask_set); |
d24c33a4 JA |
166 | } |
167 | ||
dcb69098 | 168 | static void clear_blocks(struct pool *pool, unsigned int pool_idx, |
a3ebe7e0 | 169 | unsigned int idx, size_t nr_blocks) |
d24c33a4 | 170 | { |
dcb69098 | 171 | blocks_iter(pool, pool_idx, idx, nr_blocks, mask_clear); |
d24c33a4 JA |
172 | } |
173 | ||
ec996e9c JA |
174 | static int find_next_zero(int word, int start) |
175 | { | |
176 | assert(word != -1U); | |
271067a6 JH |
177 | word >>= start; |
178 | return ffz(word) + start; | |
d24c33a4 JA |
179 | } |
180 | ||
adf57099 | 181 | static int add_pool(struct pool *pool, unsigned int alloc_size) |
d24c33a4 | 182 | { |
8d5844e9 | 183 | int bitmap_blocks; |
c8931876 | 184 | int mmap_flags; |
b8a6582e | 185 | void *ptr; |
ec996e9c | 186 | |
55f6491d | 187 | #ifdef SMALLOC_REDZONE |
ec996e9c | 188 | alloc_size += sizeof(unsigned int); |
55f6491d | 189 | #endif |
ec996e9c JA |
190 | alloc_size += sizeof(struct block_hdr); |
191 | if (alloc_size < INITIAL_SIZE) | |
192 | alloc_size = INITIAL_SIZE; | |
193 | ||
194 | /* round up to nearest full number of blocks */ | |
195 | alloc_size = (alloc_size + SMALLOC_BPL - 1) & ~(SMALLOC_BPL - 1); | |
196 | bitmap_blocks = alloc_size / SMALLOC_BPL; | |
197 | alloc_size += bitmap_blocks * sizeof(unsigned int); | |
198 | pool->mmap_size = alloc_size; | |
0b9d69ec | 199 | |
ec996e9c JA |
200 | pool->nr_blocks = bitmap_blocks; |
201 | pool->free_blocks = bitmap_blocks * SMALLOC_BPB; | |
adf57099 | 202 | |
c8931876 JA |
203 | mmap_flags = OS_MAP_ANON; |
204 | #ifdef CONFIG_ESX | |
205 | mmap_flags |= MAP_PRIVATE; | |
206 | #else | |
207 | mmap_flags |= MAP_SHARED; | |
208 | #endif | |
209 | ptr = mmap(NULL, alloc_size, PROT_READ|PROT_WRITE, mmap_flags, -1, 0); | |
210 | ||
d24c33a4 | 211 | if (ptr == MAP_FAILED) |
8d5844e9 | 212 | goto out_fail; |
d24c33a4 | 213 | |
ec996e9c JA |
214 | memset(ptr, 0, alloc_size); |
215 | pool->map = ptr; | |
216 | pool->bitmap = (void *) ptr + (pool->nr_blocks * SMALLOC_BPL); | |
d24c33a4 | 217 | |
521da527 | 218 | pool->lock = fio_mutex_init(FIO_MUTEX_UNLOCKED); |
d24c33a4 | 219 | if (!pool->lock) |
8d5844e9 | 220 | goto out_fail; |
d24c33a4 | 221 | |
d24c33a4 JA |
222 | nr_pools++; |
223 | return 0; | |
8d5844e9 | 224 | out_fail: |
b0f0326a | 225 | log_err("smalloc: failed adding pool\n"); |
d24c33a4 | 226 | if (pool->map) |
ec996e9c | 227 | munmap(pool->map, pool->mmap_size); |
d24c33a4 JA |
228 | return 1; |
229 | } | |
230 | ||
231 | void sinit(void) | |
232 | { | |
85492cb8 | 233 | int i, ret; |
d24c33a4 | 234 | |
d7df1d13 | 235 | lock = fio_rwlock_init(); |
85492cb8 JA |
236 | |
237 | for (i = 0; i < MAX_POOLS; i++) { | |
238 | ret = add_pool(&mp[i], INITIAL_SIZE); | |
239 | if (ret) | |
240 | break; | |
241 | } | |
242 | ||
243 | /* | |
244 | * If we added at least one pool, we should be OK for most | |
245 | * cases. | |
246 | */ | |
247 | assert(i); | |
d24c33a4 JA |
248 | } |
249 | ||
250 | static void cleanup_pool(struct pool *pool) | |
251 | { | |
443bb114 JA |
252 | /* |
253 | * This will also remove the temporary file we used as a backing | |
254 | * store, it was already unlinked | |
255 | */ | |
ec996e9c | 256 | munmap(pool->map, pool->mmap_size); |
6548f47f JA |
257 | |
258 | if (pool->lock) | |
259 | fio_mutex_remove(pool->lock); | |
d24c33a4 JA |
260 | } |
261 | ||
262 | void scleanup(void) | |
263 | { | |
264 | unsigned int i; | |
265 | ||
266 | for (i = 0; i < nr_pools; i++) | |
267 | cleanup_pool(&mp[i]); | |
268 | ||
6548f47f | 269 | if (lock) |
d7df1d13 | 270 | fio_rwlock_remove(lock); |
d24c33a4 JA |
271 | } |
272 | ||
89da54e8 | 273 | #ifdef SMALLOC_REDZONE |
cf98708d JA |
274 | static void *postred_ptr(struct block_hdr *hdr) |
275 | { | |
e43606c2 | 276 | uintptr_t ptr; |
cf98708d | 277 | |
e43606c2 | 278 | ptr = (uintptr_t) hdr + hdr->size - sizeof(unsigned int); |
cf98708d JA |
279 | ptr = (ptr + int_mask) & ~int_mask; |
280 | ||
281 | return (void *) ptr; | |
282 | } | |
283 | ||
ec996e9c | 284 | static void fill_redzone(struct block_hdr *hdr) |
55f6491d | 285 | { |
cf98708d | 286 | unsigned int *postred = postred_ptr(hdr); |
55f6491d | 287 | |
ec996e9c JA |
288 | hdr->prered = SMALLOC_PRE_RED; |
289 | *postred = SMALLOC_POST_RED; | |
ec996e9c | 290 | } |
55f6491d | 291 | |
ec996e9c JA |
292 | static void sfree_check_redzone(struct block_hdr *hdr) |
293 | { | |
cf98708d | 294 | unsigned int *postred = postred_ptr(hdr); |
ec996e9c JA |
295 | |
296 | if (hdr->prered != SMALLOC_PRE_RED) { | |
b0f0326a JA |
297 | log_err("smalloc pre redzone destroyed!\n" |
298 | " ptr=%p, prered=%x, expected %x\n", | |
ec996e9c | 299 | hdr, hdr->prered, SMALLOC_PRE_RED); |
55f6491d JA |
300 | assert(0); |
301 | } | |
302 | if (*postred != SMALLOC_POST_RED) { | |
b0f0326a JA |
303 | log_err("smalloc post redzone destroyed!\n" |
304 | " ptr=%p, postred=%x, expected %x\n", | |
ec996e9c | 305 | hdr, *postred, SMALLOC_POST_RED); |
55f6491d JA |
306 | assert(0); |
307 | } | |
89da54e8 JA |
308 | } |
309 | #else | |
310 | static void fill_redzone(struct block_hdr *hdr) | |
311 | { | |
55f6491d JA |
312 | } |
313 | ||
89da54e8 JA |
314 | static void sfree_check_redzone(struct block_hdr *hdr) |
315 | { | |
316 | } | |
317 | #endif | |
318 | ||
d24c33a4 JA |
319 | static void sfree_pool(struct pool *pool, void *ptr) |
320 | { | |
ec996e9c | 321 | struct block_hdr *hdr; |
179446e0 | 322 | unsigned int i, idx; |
ec996e9c | 323 | unsigned long offset; |
d24c33a4 JA |
324 | |
325 | if (!ptr) | |
326 | return; | |
327 | ||
ec996e9c JA |
328 | ptr -= sizeof(*hdr); |
329 | hdr = ptr; | |
55f6491d | 330 | |
d24c33a4 JA |
331 | assert(ptr_valid(pool, ptr)); |
332 | ||
ec996e9c | 333 | sfree_check_redzone(hdr); |
d24c33a4 | 334 | |
ec996e9c JA |
335 | offset = ptr - pool->map; |
336 | i = offset / SMALLOC_BPL; | |
337 | idx = (offset % SMALLOC_BPL) / SMALLOC_BPB; | |
d24c33a4 | 338 | |
ec996e9c | 339 | pool_lock(pool); |
dcb69098 | 340 | clear_blocks(pool, i, idx, size_to_blocks(hdr->size)); |
ec996e9c JA |
341 | if (i < pool->next_non_full) |
342 | pool->next_non_full = i; | |
179446e0 | 343 | pool->free_blocks += size_to_blocks(hdr->size); |
d24c33a4 JA |
344 | pool_unlock(pool); |
345 | } | |
346 | ||
347 | void sfree(void *ptr) | |
348 | { | |
349 | struct pool *pool = NULL; | |
350 | unsigned int i; | |
351 | ||
8e5732e5 JA |
352 | if (!ptr) |
353 | return; | |
354 | ||
65864cf7 | 355 | global_read_lock(); |
d24c33a4 JA |
356 | |
357 | for (i = 0; i < nr_pools; i++) { | |
358 | if (ptr_valid(&mp[i], ptr)) { | |
359 | pool = &mp[i]; | |
360 | break; | |
361 | } | |
362 | } | |
363 | ||
65864cf7 | 364 | global_read_unlock(); |
d24c33a4 | 365 | |
45a65144 JA |
366 | if (pool) { |
367 | sfree_pool(pool, ptr); | |
368 | return; | |
369 | } | |
370 | ||
371 | log_err("smalloc: ptr %p not from smalloc pool\n", ptr); | |
d24c33a4 JA |
372 | } |
373 | ||
a3ebe7e0 | 374 | static void *__smalloc_pool(struct pool *pool, size_t size) |
d24c33a4 | 375 | { |
a3ebe7e0 | 376 | size_t nr_blocks; |
ec996e9c JA |
377 | unsigned int i; |
378 | unsigned int offset; | |
379 | unsigned int last_idx; | |
380 | void *ret = NULL; | |
d24c33a4 | 381 | |
d24c33a4 | 382 | pool_lock(pool); |
179446e0 JA |
383 | |
384 | nr_blocks = size_to_blocks(size); | |
ec996e9c | 385 | if (nr_blocks > pool->free_blocks) |
8e5732e5 | 386 | goto fail; |
5ec10eaa | 387 | |
ec996e9c JA |
388 | i = pool->next_non_full; |
389 | last_idx = 0; | |
390 | offset = -1U; | |
391 | while (i < pool->nr_blocks) { | |
392 | unsigned int idx; | |
d24c33a4 | 393 | |
ec996e9c JA |
394 | if (pool->bitmap[i] == -1U) { |
395 | i++; | |
396 | pool->next_non_full = i; | |
397 | last_idx = 0; | |
398 | continue; | |
399 | } | |
d24c33a4 | 400 | |
ec996e9c | 401 | idx = find_next_zero(pool->bitmap[i], last_idx); |
dcb69098 | 402 | if (!blocks_free(pool, i, idx, nr_blocks)) { |
ec996e9c JA |
403 | idx += nr_blocks; |
404 | if (idx < SMALLOC_BPI) | |
405 | last_idx = idx; | |
406 | else { | |
407 | last_idx = 0; | |
408 | while (idx >= SMALLOC_BPI) { | |
409 | i++; | |
410 | idx -= SMALLOC_BPI; | |
411 | } | |
412 | } | |
413 | continue; | |
d24c33a4 | 414 | } |
dcb69098 | 415 | set_blocks(pool, i, idx, nr_blocks); |
ec996e9c JA |
416 | offset = i * SMALLOC_BPL + idx * SMALLOC_BPB; |
417 | break; | |
418 | } | |
419 | ||
420 | if (i < pool->nr_blocks) { | |
421 | pool->free_blocks -= nr_blocks; | |
422 | ret = pool->map + offset; | |
d24c33a4 | 423 | } |
ec996e9c | 424 | fail: |
d24c33a4 | 425 | pool_unlock(pool); |
ec996e9c | 426 | return ret; |
d24c33a4 JA |
427 | } |
428 | ||
a3ebe7e0 | 429 | static void *smalloc_pool(struct pool *pool, size_t size) |
55f6491d | 430 | { |
a3ebe7e0 | 431 | size_t alloc_size = size + sizeof(struct block_hdr); |
55f6491d JA |
432 | void *ptr; |
433 | ||
cf98708d | 434 | /* |
122426da JA |
435 | * Round to int alignment, so that the postred pointer will |
436 | * be naturally aligned as well. | |
cf98708d | 437 | */ |
ec996e9c | 438 | #ifdef SMALLOC_REDZONE |
122426da JA |
439 | alloc_size += sizeof(unsigned int); |
440 | alloc_size = (alloc_size + int_mask) & ~int_mask; | |
ec996e9c JA |
441 | #endif |
442 | ||
443 | ptr = __smalloc_pool(pool, alloc_size); | |
89da54e8 JA |
444 | if (ptr) { |
445 | struct block_hdr *hdr = ptr; | |
55f6491d | 446 | |
89da54e8 JA |
447 | hdr->size = alloc_size; |
448 | fill_redzone(hdr); | |
55f6491d | 449 | |
89da54e8 JA |
450 | ptr += sizeof(*hdr); |
451 | memset(ptr, 0, size); | |
452 | } | |
ec996e9c | 453 | |
55f6491d | 454 | return ptr; |
55f6491d JA |
455 | } |
456 | ||
7982aa7d | 457 | void *smalloc(size_t size) |
d24c33a4 | 458 | { |
85492cb8 | 459 | unsigned int i, end_pool; |
d24c33a4 | 460 | |
7982aa7d JA |
461 | if (size != (unsigned int) size) |
462 | return NULL; | |
463 | ||
d1271dc1 | 464 | global_write_lock(); |
d24c33a4 | 465 | i = last_pool; |
85492cb8 | 466 | end_pool = nr_pools; |
d24c33a4 JA |
467 | |
468 | do { | |
85492cb8 | 469 | for (; i < end_pool; i++) { |
d24c33a4 JA |
470 | void *ptr = smalloc_pool(&mp[i], size); |
471 | ||
472 | if (ptr) { | |
473 | last_pool = i; | |
d1271dc1 | 474 | global_write_unlock(); |
d24c33a4 JA |
475 | return ptr; |
476 | } | |
477 | } | |
478 | if (last_pool) { | |
85492cb8 JA |
479 | end_pool = last_pool; |
480 | last_pool = i = 0; | |
d24c33a4 JA |
481 | continue; |
482 | } | |
483 | ||
85492cb8 | 484 | break; |
d24c33a4 JA |
485 | } while (1); |
486 | ||
d1271dc1 | 487 | global_write_unlock(); |
d24c33a4 JA |
488 | return NULL; |
489 | } | |
490 | ||
544992f7 JA |
491 | void *scalloc(size_t nmemb, size_t size) |
492 | { | |
493 | void *ret; | |
494 | ||
495 | ret = smalloc(nmemb * size); | |
496 | if (ret) | |
497 | memset(ret, 0, nmemb * size); | |
498 | ||
499 | return ret; | |
500 | } | |
501 | ||
d24c33a4 JA |
502 | char *smalloc_strdup(const char *str) |
503 | { | |
2894a2d4 | 504 | char *ptr = NULL; |
d24c33a4 JA |
505 | |
506 | ptr = smalloc(strlen(str) + 1); | |
2894a2d4 CE |
507 | if (ptr) |
508 | strcpy(ptr, str); | |
d24c33a4 JA |
509 | return ptr; |
510 | } |