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