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