Jens Axboe | d24c33a | 2008-03-01 18:27:36 +0100 | [diff] [blame] | 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> |
Bruce Cran | e43606c | 2012-02-20 09:34:24 +0100 | [diff] [blame] | 11 | #include <inttypes.h> |
Jens Axboe | d24c33a | 2008-03-01 18:27:36 +0100 | [diff] [blame] | 12 | #include <sys/types.h> |
| 13 | #include <limits.h> |
Greg Edwards | 3a8600b | 2010-06-25 09:24:19 -0600 | [diff] [blame] | 14 | #include <fcntl.h> |
Jens Axboe | d24c33a | 2008-03-01 18:27:36 +0100 | [diff] [blame] | 15 | |
Jens Axboe | 6548f47 | 2008-06-13 08:38:31 +0200 | [diff] [blame] | 16 | #include "mutex.h" |
Jens Axboe | b3268b9 | 2008-06-02 09:59:32 +0200 | [diff] [blame] | 17 | #include "arch/arch.h" |
Greg Edwards | 3a8600b | 2010-06-25 09:24:19 -0600 | [diff] [blame] | 18 | #include "os/os.h" |
Jens Axboe | 10aa136 | 2014-04-01 21:10:36 -0600 | [diff] [blame] | 19 | #include "smalloc.h" |
Jens Axboe | f5c3fce | 2014-10-13 11:53:55 -0600 | [diff] [blame] | 20 | #include "log.h" |
Jens Axboe | d24c33a | 2008-03-01 18:27:36 +0100 | [diff] [blame] | 21 | |
Jens Axboe | 55f6491 | 2008-05-26 09:37:21 +0200 | [diff] [blame] | 22 | #define SMALLOC_REDZONE /* define to detect memory corruption */ |
Jens Axboe | d24c33a | 2008-03-01 18:27:36 +0100 | [diff] [blame] | 23 | |
Jens Axboe | ec996e9 | 2008-05-27 11:39:39 +0200 | [diff] [blame] | 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 | |
Jens Axboe | 016f32f | 2015-01-13 21:35:31 -0700 | [diff] [blame] | 28 | #define INITIAL_SIZE 16*1024*1024 /* new pool size */ |
Jens Axboe | d19a46f | 2015-03-03 15:13:17 -0700 | [diff] [blame] | 29 | #define MAX_POOLS 8 /* maximum number of pools to setup */ |
Jens Axboe | d24c33a | 2008-03-01 18:27:36 +0100 | [diff] [blame] | 30 | |
Jens Axboe | 55f6491 | 2008-05-26 09:37:21 +0200 | [diff] [blame] | 31 | #define SMALLOC_PRE_RED 0xdeadbeefU |
| 32 | #define SMALLOC_POST_RED 0x5aa55aa5U |
Jens Axboe | 55f6491 | 2008-05-26 09:37:21 +0200 | [diff] [blame] | 33 | |
Jens Axboe | 2b386d2 | 2008-03-26 10:32:57 +0100 | [diff] [blame] | 34 | unsigned int smalloc_pool_size = INITIAL_SIZE; |
Jens Axboe | 10aa136 | 2014-04-01 21:10:36 -0600 | [diff] [blame] | 35 | static const int int_mask = sizeof(int) - 1; |
Jens Axboe | 2b386d2 | 2008-03-26 10:32:57 +0100 | [diff] [blame] | 36 | |
Jens Axboe | d24c33a | 2008-03-01 18:27:36 +0100 | [diff] [blame] | 37 | struct pool { |
Jens Axboe | 6548f47 | 2008-06-13 08:38:31 +0200 | [diff] [blame] | 38 | struct fio_mutex *lock; /* protects this pool */ |
Jens Axboe | d24c33a | 2008-03-01 18:27:36 +0100 | [diff] [blame] | 39 | void *map; /* map of blocks */ |
Jens Axboe | ec996e9 | 2008-05-27 11:39:39 +0200 | [diff] [blame] | 40 | unsigned int *bitmap; /* blocks free/busy map */ |
Jens Axboe | a3ebe7e | 2012-11-02 16:47:03 +0100 | [diff] [blame] | 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; |
Jens Axboe | ec996e9 | 2008-05-27 11:39:39 +0200 | [diff] [blame] | 45 | }; |
| 46 | |
| 47 | struct block_hdr { |
Jens Axboe | a3ebe7e | 2012-11-02 16:47:03 +0100 | [diff] [blame] | 48 | size_t size; |
Jens Axboe | ec996e9 | 2008-05-27 11:39:39 +0200 | [diff] [blame] | 49 | #ifdef SMALLOC_REDZONE |
| 50 | unsigned int prered; |
| 51 | #endif |
Jens Axboe | d24c33a | 2008-03-01 18:27:36 +0100 | [diff] [blame] | 52 | }; |
| 53 | |
| 54 | static struct pool mp[MAX_POOLS]; |
| 55 | static unsigned int nr_pools; |
| 56 | static unsigned int last_pool; |
Jens Axboe | d7df1d1 | 2013-03-20 19:57:01 -0600 | [diff] [blame] | 57 | static struct fio_rwlock *lock; |
Jens Axboe | d24c33a | 2008-03-01 18:27:36 +0100 | [diff] [blame] | 58 | |
Jens Axboe | d24c33a | 2008-03-01 18:27:36 +0100 | [diff] [blame] | 59 | static inline void pool_lock(struct pool *pool) |
| 60 | { |
Jens Axboe | 2e3e31e | 2009-01-06 11:30:08 +0100 | [diff] [blame] | 61 | fio_mutex_down(pool->lock); |
Jens Axboe | d24c33a | 2008-03-01 18:27:36 +0100 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | static inline void pool_unlock(struct pool *pool) |
| 65 | { |
Jens Axboe | 2e3e31e | 2009-01-06 11:30:08 +0100 | [diff] [blame] | 66 | fio_mutex_up(pool->lock); |
Jens Axboe | d24c33a | 2008-03-01 18:27:36 +0100 | [diff] [blame] | 67 | } |
| 68 | |
Jens Axboe | 65864cf | 2008-03-03 10:36:36 +0100 | [diff] [blame] | 69 | static inline void global_read_lock(void) |
Jens Axboe | d24c33a | 2008-03-01 18:27:36 +0100 | [diff] [blame] | 70 | { |
Jens Axboe | d7df1d1 | 2013-03-20 19:57:01 -0600 | [diff] [blame] | 71 | fio_rwlock_read(lock); |
Jens Axboe | d24c33a | 2008-03-01 18:27:36 +0100 | [diff] [blame] | 72 | } |
| 73 | |
Jens Axboe | 65864cf | 2008-03-03 10:36:36 +0100 | [diff] [blame] | 74 | static inline void global_read_unlock(void) |
Jens Axboe | d24c33a | 2008-03-01 18:27:36 +0100 | [diff] [blame] | 75 | { |
Jens Axboe | d7df1d1 | 2013-03-20 19:57:01 -0600 | [diff] [blame] | 76 | fio_rwlock_unlock(lock); |
Jens Axboe | 65864cf | 2008-03-03 10:36:36 +0100 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | static inline void global_write_lock(void) |
| 80 | { |
Jens Axboe | d7df1d1 | 2013-03-20 19:57:01 -0600 | [diff] [blame] | 81 | fio_rwlock_write(lock); |
Jens Axboe | 65864cf | 2008-03-03 10:36:36 +0100 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | static inline void global_write_unlock(void) |
| 85 | { |
Jens Axboe | d7df1d1 | 2013-03-20 19:57:01 -0600 | [diff] [blame] | 86 | fio_rwlock_unlock(lock); |
Jens Axboe | d24c33a | 2008-03-01 18:27:36 +0100 | [diff] [blame] | 87 | } |
| 88 | |
Jens Axboe | d24c33a | 2008-03-01 18:27:36 +0100 | [diff] [blame] | 89 | static inline int ptr_valid(struct pool *pool, void *ptr) |
| 90 | { |
Jens Axboe | dcb6909 | 2008-05-27 20:35:18 +0200 | [diff] [blame] | 91 | unsigned int pool_size = pool->nr_blocks * SMALLOC_BPL; |
Jens Axboe | ec996e9 | 2008-05-27 11:39:39 +0200 | [diff] [blame] | 92 | |
| 93 | return (ptr >= pool->map) && (ptr < pool->map + pool_size); |
Jens Axboe | d24c33a | 2008-03-01 18:27:36 +0100 | [diff] [blame] | 94 | } |
| 95 | |
Jens Axboe | a3ebe7e | 2012-11-02 16:47:03 +0100 | [diff] [blame] | 96 | static inline size_t size_to_blocks(size_t size) |
Jens Axboe | 808e9ea | 2008-05-27 14:12:45 +0200 | [diff] [blame] | 97 | { |
| 98 | return (size + SMALLOC_BPB - 1) / SMALLOC_BPB; |
| 99 | } |
| 100 | |
Jens Axboe | dcb6909 | 2008-05-27 20:35:18 +0200 | [diff] [blame] | 101 | static int blocks_iter(struct pool *pool, unsigned int pool_idx, |
Jens Axboe | a3ebe7e | 2012-11-02 16:47:03 +0100 | [diff] [blame] | 102 | unsigned int idx, size_t nr_blocks, |
Jens Axboe | ec996e9 | 2008-05-27 11:39:39 +0200 | [diff] [blame] | 103 | int (*func)(unsigned int *map, unsigned int mask)) |
Jens Axboe | d24c33a | 2008-03-01 18:27:36 +0100 | [diff] [blame] | 104 | { |
Jens Axboe | dcb6909 | 2008-05-27 20:35:18 +0200 | [diff] [blame] | 105 | |
Jens Axboe | ec996e9 | 2008-05-27 11:39:39 +0200 | [diff] [blame] | 106 | while (nr_blocks) { |
| 107 | unsigned int this_blocks, mask; |
Jens Axboe | dcb6909 | 2008-05-27 20:35:18 +0200 | [diff] [blame] | 108 | unsigned int *map; |
| 109 | |
| 110 | if (pool_idx >= pool->nr_blocks) |
| 111 | return 0; |
| 112 | |
| 113 | map = &pool->bitmap[pool_idx]; |
Jens Axboe | d24c33a | 2008-03-01 18:27:36 +0100 | [diff] [blame] | 114 | |
Jens Axboe | ec996e9 | 2008-05-27 11:39:39 +0200 | [diff] [blame] | 115 | this_blocks = nr_blocks; |
| 116 | if (this_blocks + idx > SMALLOC_BPI) { |
| 117 | this_blocks = SMALLOC_BPI - idx; |
| 118 | idx = SMALLOC_BPI - this_blocks; |
Jens Axboe | d24c33a | 2008-03-01 18:27:36 +0100 | [diff] [blame] | 119 | } |
Jens Axboe | ec996e9 | 2008-05-27 11:39:39 +0200 | [diff] [blame] | 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; |
Jens Axboe | dcb6909 | 2008-05-27 20:35:18 +0200 | [diff] [blame] | 131 | pool_idx++; |
Jens Axboe | d24c33a | 2008-03-01 18:27:36 +0100 | [diff] [blame] | 132 | } |
| 133 | |
Jens Axboe | ec996e9 | 2008-05-27 11:39:39 +0200 | [diff] [blame] | 134 | return 1; |
Jens Axboe | ec996e9 | 2008-05-27 11:39:39 +0200 | [diff] [blame] | 135 | } |
| 136 | |
| 137 | static int mask_cmp(unsigned int *map, unsigned int mask) |
| 138 | { |
| 139 | return !(*map & mask); |
| 140 | } |
| 141 | |
| 142 | static int mask_clear(unsigned int *map, unsigned int mask) |
| 143 | { |
Jens Axboe | dcb6909 | 2008-05-27 20:35:18 +0200 | [diff] [blame] | 144 | assert((*map & mask) == mask); |
Jens Axboe | ec996e9 | 2008-05-27 11:39:39 +0200 | [diff] [blame] | 145 | *map &= ~mask; |
| 146 | return 1; |
| 147 | } |
| 148 | |
| 149 | static int mask_set(unsigned int *map, unsigned int mask) |
| 150 | { |
Jens Axboe | dcb6909 | 2008-05-27 20:35:18 +0200 | [diff] [blame] | 151 | assert(!(*map & mask)); |
Jens Axboe | ec996e9 | 2008-05-27 11:39:39 +0200 | [diff] [blame] | 152 | *map |= mask; |
| 153 | return 1; |
| 154 | } |
| 155 | |
Jens Axboe | dcb6909 | 2008-05-27 20:35:18 +0200 | [diff] [blame] | 156 | static int blocks_free(struct pool *pool, unsigned int pool_idx, |
Jens Axboe | a3ebe7e | 2012-11-02 16:47:03 +0100 | [diff] [blame] | 157 | unsigned int idx, size_t nr_blocks) |
Jens Axboe | ec996e9 | 2008-05-27 11:39:39 +0200 | [diff] [blame] | 158 | { |
Jens Axboe | dcb6909 | 2008-05-27 20:35:18 +0200 | [diff] [blame] | 159 | return blocks_iter(pool, pool_idx, idx, nr_blocks, mask_cmp); |
Jens Axboe | ec996e9 | 2008-05-27 11:39:39 +0200 | [diff] [blame] | 160 | } |
| 161 | |
Jens Axboe | dcb6909 | 2008-05-27 20:35:18 +0200 | [diff] [blame] | 162 | static void set_blocks(struct pool *pool, unsigned int pool_idx, |
Jens Axboe | a3ebe7e | 2012-11-02 16:47:03 +0100 | [diff] [blame] | 163 | unsigned int idx, size_t nr_blocks) |
Jens Axboe | ec996e9 | 2008-05-27 11:39:39 +0200 | [diff] [blame] | 164 | { |
Jens Axboe | dcb6909 | 2008-05-27 20:35:18 +0200 | [diff] [blame] | 165 | blocks_iter(pool, pool_idx, idx, nr_blocks, mask_set); |
Jens Axboe | ec996e9 | 2008-05-27 11:39:39 +0200 | [diff] [blame] | 166 | } |
| 167 | |
Jens Axboe | dcb6909 | 2008-05-27 20:35:18 +0200 | [diff] [blame] | 168 | static void clear_blocks(struct pool *pool, unsigned int pool_idx, |
Jens Axboe | a3ebe7e | 2012-11-02 16:47:03 +0100 | [diff] [blame] | 169 | unsigned int idx, size_t nr_blocks) |
Jens Axboe | ec996e9 | 2008-05-27 11:39:39 +0200 | [diff] [blame] | 170 | { |
Jens Axboe | dcb6909 | 2008-05-27 20:35:18 +0200 | [diff] [blame] | 171 | blocks_iter(pool, pool_idx, idx, nr_blocks, mask_clear); |
Jens Axboe | ec996e9 | 2008-05-27 11:39:39 +0200 | [diff] [blame] | 172 | } |
| 173 | |
Jens Axboe | ec996e9 | 2008-05-27 11:39:39 +0200 | [diff] [blame] | 174 | static int find_next_zero(int word, int start) |
| 175 | { |
| 176 | assert(word != -1U); |
Jiri Horky | 271067a | 2011-08-15 15:18:03 +0200 | [diff] [blame] | 177 | word >>= start; |
| 178 | return ffz(word) + start; |
Jens Axboe | d24c33a | 2008-03-01 18:27:36 +0100 | [diff] [blame] | 179 | } |
| 180 | |
Jens Axboe | adf5709 | 2008-04-16 19:43:17 +0200 | [diff] [blame] | 181 | static int add_pool(struct pool *pool, unsigned int alloc_size) |
Jens Axboe | d24c33a | 2008-03-01 18:27:36 +0100 | [diff] [blame] | 182 | { |
Jens Axboe | 8d5844e | 2011-06-29 09:50:08 +0200 | [diff] [blame] | 183 | int bitmap_blocks; |
Jens Axboe | ef7035a | 2014-06-18 15:30:09 -0700 | [diff] [blame] | 184 | int mmap_flags; |
Jens Axboe | b8a6582 | 2008-12-04 14:33:41 +0100 | [diff] [blame] | 185 | void *ptr; |
Jens Axboe | ec996e9 | 2008-05-27 11:39:39 +0200 | [diff] [blame] | 186 | |
Jens Axboe | 55f6491 | 2008-05-26 09:37:21 +0200 | [diff] [blame] | 187 | #ifdef SMALLOC_REDZONE |
Jens Axboe | ec996e9 | 2008-05-27 11:39:39 +0200 | [diff] [blame] | 188 | alloc_size += sizeof(unsigned int); |
Jens Axboe | 55f6491 | 2008-05-26 09:37:21 +0200 | [diff] [blame] | 189 | #endif |
Jens Axboe | ec996e9 | 2008-05-27 11:39:39 +0200 | [diff] [blame] | 190 | alloc_size += sizeof(struct block_hdr); |
| 191 | if (alloc_size < INITIAL_SIZE) |
| 192 | alloc_size = INITIAL_SIZE; |
Jens Axboe | adf5709 | 2008-04-16 19:43:17 +0200 | [diff] [blame] | 193 | |
Jens Axboe | ec996e9 | 2008-05-27 11:39:39 +0200 | [diff] [blame] | 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; |
Jens Axboe | 0b9d69e | 2009-09-11 22:29:54 +0200 | [diff] [blame] | 199 | |
Jens Axboe | ec996e9 | 2008-05-27 11:39:39 +0200 | [diff] [blame] | 200 | pool->nr_blocks = bitmap_blocks; |
| 201 | pool->free_blocks = bitmap_blocks * SMALLOC_BPB; |
| 202 | |
Jens Axboe | ef7035a | 2014-06-18 15:30:09 -0700 | [diff] [blame] | 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 | |
Jens Axboe | d24c33a | 2008-03-01 18:27:36 +0100 | [diff] [blame] | 211 | if (ptr == MAP_FAILED) |
Jens Axboe | 8d5844e | 2011-06-29 09:50:08 +0200 | [diff] [blame] | 212 | goto out_fail; |
Jens Axboe | d24c33a | 2008-03-01 18:27:36 +0100 | [diff] [blame] | 213 | |
Jens Axboe | ec996e9 | 2008-05-27 11:39:39 +0200 | [diff] [blame] | 214 | memset(ptr, 0, alloc_size); |
| 215 | pool->map = ptr; |
| 216 | pool->bitmap = (void *) ptr + (pool->nr_blocks * SMALLOC_BPL); |
Jens Axboe | d24c33a | 2008-03-01 18:27:36 +0100 | [diff] [blame] | 217 | |
Jens Axboe | 521da52 | 2012-08-02 11:21:36 +0200 | [diff] [blame] | 218 | pool->lock = fio_mutex_init(FIO_MUTEX_UNLOCKED); |
Jens Axboe | d24c33a | 2008-03-01 18:27:36 +0100 | [diff] [blame] | 219 | if (!pool->lock) |
Jens Axboe | 8d5844e | 2011-06-29 09:50:08 +0200 | [diff] [blame] | 220 | goto out_fail; |
Jens Axboe | d24c33a | 2008-03-01 18:27:36 +0100 | [diff] [blame] | 221 | |
Jens Axboe | d24c33a | 2008-03-01 18:27:36 +0100 | [diff] [blame] | 222 | nr_pools++; |
| 223 | return 0; |
Jens Axboe | 8d5844e | 2011-06-29 09:50:08 +0200 | [diff] [blame] | 224 | out_fail: |
Jens Axboe | f5c3fce | 2014-10-13 11:53:55 -0600 | [diff] [blame] | 225 | log_err("smalloc: failed adding pool\n"); |
Jens Axboe | d24c33a | 2008-03-01 18:27:36 +0100 | [diff] [blame] | 226 | if (pool->map) |
Jens Axboe | ec996e9 | 2008-05-27 11:39:39 +0200 | [diff] [blame] | 227 | munmap(pool->map, pool->mmap_size); |
Jens Axboe | d24c33a | 2008-03-01 18:27:36 +0100 | [diff] [blame] | 228 | return 1; |
| 229 | } |
| 230 | |
| 231 | void sinit(void) |
| 232 | { |
Jens Axboe | d19a46f | 2015-03-03 15:13:17 -0700 | [diff] [blame] | 233 | int i, ret; |
Jens Axboe | d24c33a | 2008-03-01 18:27:36 +0100 | [diff] [blame] | 234 | |
Jens Axboe | d7df1d1 | 2013-03-20 19:57:01 -0600 | [diff] [blame] | 235 | lock = fio_rwlock_init(); |
Jens Axboe | d19a46f | 2015-03-03 15:13:17 -0700 | [diff] [blame] | 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); |
Jens Axboe | d24c33a | 2008-03-01 18:27:36 +0100 | [diff] [blame] | 248 | } |
| 249 | |
| 250 | static void cleanup_pool(struct pool *pool) |
| 251 | { |
Jens Axboe | 443bb11 | 2008-12-04 14:30:13 +0100 | [diff] [blame] | 252 | /* |
| 253 | * This will also remove the temporary file we used as a backing |
| 254 | * store, it was already unlinked |
| 255 | */ |
Jens Axboe | ec996e9 | 2008-05-27 11:39:39 +0200 | [diff] [blame] | 256 | munmap(pool->map, pool->mmap_size); |
Jens Axboe | 6548f47 | 2008-06-13 08:38:31 +0200 | [diff] [blame] | 257 | |
| 258 | if (pool->lock) |
| 259 | fio_mutex_remove(pool->lock); |
Jens Axboe | d24c33a | 2008-03-01 18:27:36 +0100 | [diff] [blame] | 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 | |
Jens Axboe | 6548f47 | 2008-06-13 08:38:31 +0200 | [diff] [blame] | 269 | if (lock) |
Jens Axboe | d7df1d1 | 2013-03-20 19:57:01 -0600 | [diff] [blame] | 270 | fio_rwlock_remove(lock); |
Jens Axboe | d24c33a | 2008-03-01 18:27:36 +0100 | [diff] [blame] | 271 | } |
| 272 | |
Jens Axboe | 89da54e | 2008-05-27 20:49:29 +0200 | [diff] [blame] | 273 | #ifdef SMALLOC_REDZONE |
Jens Axboe | cf98708 | 2009-07-07 17:43:11 +0200 | [diff] [blame] | 274 | static void *postred_ptr(struct block_hdr *hdr) |
| 275 | { |
Bruce Cran | e43606c | 2012-02-20 09:34:24 +0100 | [diff] [blame] | 276 | uintptr_t ptr; |
Jens Axboe | cf98708 | 2009-07-07 17:43:11 +0200 | [diff] [blame] | 277 | |
Bruce Cran | e43606c | 2012-02-20 09:34:24 +0100 | [diff] [blame] | 278 | ptr = (uintptr_t) hdr + hdr->size - sizeof(unsigned int); |
Jens Axboe | cf98708 | 2009-07-07 17:43:11 +0200 | [diff] [blame] | 279 | ptr = (ptr + int_mask) & ~int_mask; |
| 280 | |
| 281 | return (void *) ptr; |
| 282 | } |
| 283 | |
Jens Axboe | ec996e9 | 2008-05-27 11:39:39 +0200 | [diff] [blame] | 284 | static void fill_redzone(struct block_hdr *hdr) |
Jens Axboe | 55f6491 | 2008-05-26 09:37:21 +0200 | [diff] [blame] | 285 | { |
Jens Axboe | cf98708 | 2009-07-07 17:43:11 +0200 | [diff] [blame] | 286 | unsigned int *postred = postred_ptr(hdr); |
Jens Axboe | 55f6491 | 2008-05-26 09:37:21 +0200 | [diff] [blame] | 287 | |
Jens Axboe | ec996e9 | 2008-05-27 11:39:39 +0200 | [diff] [blame] | 288 | hdr->prered = SMALLOC_PRE_RED; |
| 289 | *postred = SMALLOC_POST_RED; |
Jens Axboe | ec996e9 | 2008-05-27 11:39:39 +0200 | [diff] [blame] | 290 | } |
Jens Axboe | 55f6491 | 2008-05-26 09:37:21 +0200 | [diff] [blame] | 291 | |
Jens Axboe | ec996e9 | 2008-05-27 11:39:39 +0200 | [diff] [blame] | 292 | static void sfree_check_redzone(struct block_hdr *hdr) |
| 293 | { |
Jens Axboe | cf98708 | 2009-07-07 17:43:11 +0200 | [diff] [blame] | 294 | unsigned int *postred = postred_ptr(hdr); |
Jens Axboe | ec996e9 | 2008-05-27 11:39:39 +0200 | [diff] [blame] | 295 | |
| 296 | if (hdr->prered != SMALLOC_PRE_RED) { |
Jens Axboe | f5c3fce | 2014-10-13 11:53:55 -0600 | [diff] [blame] | 297 | log_err("smalloc pre redzone destroyed!\n" |
| 298 | " ptr=%p, prered=%x, expected %x\n", |
Jens Axboe | ec996e9 | 2008-05-27 11:39:39 +0200 | [diff] [blame] | 299 | hdr, hdr->prered, SMALLOC_PRE_RED); |
Jens Axboe | 55f6491 | 2008-05-26 09:37:21 +0200 | [diff] [blame] | 300 | assert(0); |
| 301 | } |
| 302 | if (*postred != SMALLOC_POST_RED) { |
Jens Axboe | f5c3fce | 2014-10-13 11:53:55 -0600 | [diff] [blame] | 303 | log_err("smalloc post redzone destroyed!\n" |
| 304 | " ptr=%p, postred=%x, expected %x\n", |
Jens Axboe | ec996e9 | 2008-05-27 11:39:39 +0200 | [diff] [blame] | 305 | hdr, *postred, SMALLOC_POST_RED); |
Jens Axboe | 55f6491 | 2008-05-26 09:37:21 +0200 | [diff] [blame] | 306 | assert(0); |
| 307 | } |
Jens Axboe | 55f6491 | 2008-05-26 09:37:21 +0200 | [diff] [blame] | 308 | } |
Jens Axboe | 89da54e | 2008-05-27 20:49:29 +0200 | [diff] [blame] | 309 | #else |
| 310 | static void fill_redzone(struct block_hdr *hdr) |
| 311 | { |
| 312 | } |
| 313 | |
| 314 | static void sfree_check_redzone(struct block_hdr *hdr) |
| 315 | { |
| 316 | } |
| 317 | #endif |
Jens Axboe | 55f6491 | 2008-05-26 09:37:21 +0200 | [diff] [blame] | 318 | |
Jens Axboe | d24c33a | 2008-03-01 18:27:36 +0100 | [diff] [blame] | 319 | static void sfree_pool(struct pool *pool, void *ptr) |
| 320 | { |
Jens Axboe | ec996e9 | 2008-05-27 11:39:39 +0200 | [diff] [blame] | 321 | struct block_hdr *hdr; |
Jens Axboe | 179446e | 2008-05-27 14:11:56 +0200 | [diff] [blame] | 322 | unsigned int i, idx; |
Jens Axboe | ec996e9 | 2008-05-27 11:39:39 +0200 | [diff] [blame] | 323 | unsigned long offset; |
Jens Axboe | d24c33a | 2008-03-01 18:27:36 +0100 | [diff] [blame] | 324 | |
| 325 | if (!ptr) |
| 326 | return; |
| 327 | |
Jens Axboe | ec996e9 | 2008-05-27 11:39:39 +0200 | [diff] [blame] | 328 | ptr -= sizeof(*hdr); |
| 329 | hdr = ptr; |
Jens Axboe | 55f6491 | 2008-05-26 09:37:21 +0200 | [diff] [blame] | 330 | |
Jens Axboe | d24c33a | 2008-03-01 18:27:36 +0100 | [diff] [blame] | 331 | assert(ptr_valid(pool, ptr)); |
| 332 | |
Jens Axboe | ec996e9 | 2008-05-27 11:39:39 +0200 | [diff] [blame] | 333 | sfree_check_redzone(hdr); |
| 334 | |
| 335 | offset = ptr - pool->map; |
| 336 | i = offset / SMALLOC_BPL; |
| 337 | idx = (offset % SMALLOC_BPL) / SMALLOC_BPB; |
| 338 | |
Jens Axboe | d24c33a | 2008-03-01 18:27:36 +0100 | [diff] [blame] | 339 | pool_lock(pool); |
Jens Axboe | dcb6909 | 2008-05-27 20:35:18 +0200 | [diff] [blame] | 340 | clear_blocks(pool, i, idx, size_to_blocks(hdr->size)); |
Jens Axboe | ec996e9 | 2008-05-27 11:39:39 +0200 | [diff] [blame] | 341 | if (i < pool->next_non_full) |
| 342 | pool->next_non_full = i; |
Jens Axboe | 179446e | 2008-05-27 14:11:56 +0200 | [diff] [blame] | 343 | pool->free_blocks += size_to_blocks(hdr->size); |
Jens Axboe | d24c33a | 2008-03-01 18:27:36 +0100 | [diff] [blame] | 344 | pool_unlock(pool); |
| 345 | } |
| 346 | |
| 347 | void sfree(void *ptr) |
| 348 | { |
| 349 | struct pool *pool = NULL; |
| 350 | unsigned int i; |
| 351 | |
Jens Axboe | 8e5732e | 2008-04-16 19:51:46 +0200 | [diff] [blame] | 352 | if (!ptr) |
| 353 | return; |
| 354 | |
Jens Axboe | 65864cf | 2008-03-03 10:36:36 +0100 | [diff] [blame] | 355 | global_read_lock(); |
Jens Axboe | d24c33a | 2008-03-01 18:27:36 +0100 | [diff] [blame] | 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 | |
Jens Axboe | 65864cf | 2008-03-03 10:36:36 +0100 | [diff] [blame] | 364 | global_read_unlock(); |
Jens Axboe | d24c33a | 2008-03-01 18:27:36 +0100 | [diff] [blame] | 365 | |
| 366 | assert(pool); |
| 367 | sfree_pool(pool, ptr); |
| 368 | } |
| 369 | |
Jens Axboe | a3ebe7e | 2012-11-02 16:47:03 +0100 | [diff] [blame] | 370 | static void *__smalloc_pool(struct pool *pool, size_t size) |
Jens Axboe | d24c33a | 2008-03-01 18:27:36 +0100 | [diff] [blame] | 371 | { |
Jens Axboe | a3ebe7e | 2012-11-02 16:47:03 +0100 | [diff] [blame] | 372 | size_t nr_blocks; |
Jens Axboe | ec996e9 | 2008-05-27 11:39:39 +0200 | [diff] [blame] | 373 | unsigned int i; |
| 374 | unsigned int offset; |
| 375 | unsigned int last_idx; |
| 376 | void *ret = NULL; |
Jens Axboe | d24c33a | 2008-03-01 18:27:36 +0100 | [diff] [blame] | 377 | |
Jens Axboe | d24c33a | 2008-03-01 18:27:36 +0100 | [diff] [blame] | 378 | pool_lock(pool); |
Jens Axboe | 179446e | 2008-05-27 14:11:56 +0200 | [diff] [blame] | 379 | |
| 380 | nr_blocks = size_to_blocks(size); |
Jens Axboe | ec996e9 | 2008-05-27 11:39:39 +0200 | [diff] [blame] | 381 | if (nr_blocks > pool->free_blocks) |
Jens Axboe | d24c33a | 2008-03-01 18:27:36 +0100 | [diff] [blame] | 382 | goto fail; |
| 383 | |
Jens Axboe | ec996e9 | 2008-05-27 11:39:39 +0200 | [diff] [blame] | 384 | i = pool->next_non_full; |
| 385 | last_idx = 0; |
| 386 | offset = -1U; |
| 387 | while (i < pool->nr_blocks) { |
| 388 | unsigned int idx; |
Jens Axboe | d24c33a | 2008-03-01 18:27:36 +0100 | [diff] [blame] | 389 | |
Jens Axboe | ec996e9 | 2008-05-27 11:39:39 +0200 | [diff] [blame] | 390 | if (pool->bitmap[i] == -1U) { |
| 391 | i++; |
| 392 | pool->next_non_full = i; |
| 393 | last_idx = 0; |
| 394 | continue; |
Jens Axboe | d24c33a | 2008-03-01 18:27:36 +0100 | [diff] [blame] | 395 | } |
Jens Axboe | ec996e9 | 2008-05-27 11:39:39 +0200 | [diff] [blame] | 396 | |
| 397 | idx = find_next_zero(pool->bitmap[i], last_idx); |
Jens Axboe | dcb6909 | 2008-05-27 20:35:18 +0200 | [diff] [blame] | 398 | if (!blocks_free(pool, i, idx, nr_blocks)) { |
Jens Axboe | ec996e9 | 2008-05-27 11:39:39 +0200 | [diff] [blame] | 399 | idx += nr_blocks; |
| 400 | if (idx < SMALLOC_BPI) |
| 401 | last_idx = idx; |
| 402 | else { |
| 403 | last_idx = 0; |
| 404 | while (idx >= SMALLOC_BPI) { |
| 405 | i++; |
| 406 | idx -= SMALLOC_BPI; |
| 407 | } |
| 408 | } |
| 409 | continue; |
| 410 | } |
Jens Axboe | dcb6909 | 2008-05-27 20:35:18 +0200 | [diff] [blame] | 411 | set_blocks(pool, i, idx, nr_blocks); |
Jens Axboe | ec996e9 | 2008-05-27 11:39:39 +0200 | [diff] [blame] | 412 | offset = i * SMALLOC_BPL + idx * SMALLOC_BPB; |
| 413 | break; |
Jens Axboe | d24c33a | 2008-03-01 18:27:36 +0100 | [diff] [blame] | 414 | } |
Jens Axboe | ec996e9 | 2008-05-27 11:39:39 +0200 | [diff] [blame] | 415 | |
| 416 | if (i < pool->nr_blocks) { |
| 417 | pool->free_blocks -= nr_blocks; |
| 418 | ret = pool->map + offset; |
| 419 | } |
| 420 | fail: |
Jens Axboe | d24c33a | 2008-03-01 18:27:36 +0100 | [diff] [blame] | 421 | pool_unlock(pool); |
Jens Axboe | ec996e9 | 2008-05-27 11:39:39 +0200 | [diff] [blame] | 422 | return ret; |
Jens Axboe | d24c33a | 2008-03-01 18:27:36 +0100 | [diff] [blame] | 423 | } |
| 424 | |
Jens Axboe | a3ebe7e | 2012-11-02 16:47:03 +0100 | [diff] [blame] | 425 | static void *smalloc_pool(struct pool *pool, size_t size) |
Jens Axboe | 55f6491 | 2008-05-26 09:37:21 +0200 | [diff] [blame] | 426 | { |
Jens Axboe | a3ebe7e | 2012-11-02 16:47:03 +0100 | [diff] [blame] | 427 | size_t alloc_size = size + sizeof(struct block_hdr); |
Jens Axboe | 55f6491 | 2008-05-26 09:37:21 +0200 | [diff] [blame] | 428 | void *ptr; |
| 429 | |
Jens Axboe | cf98708 | 2009-07-07 17:43:11 +0200 | [diff] [blame] | 430 | /* |
Jens Axboe | 122426d | 2009-07-07 23:03:06 +0200 | [diff] [blame] | 431 | * Round to int alignment, so that the postred pointer will |
| 432 | * be naturally aligned as well. |
Jens Axboe | cf98708 | 2009-07-07 17:43:11 +0200 | [diff] [blame] | 433 | */ |
Jens Axboe | ec996e9 | 2008-05-27 11:39:39 +0200 | [diff] [blame] | 434 | #ifdef SMALLOC_REDZONE |
Jens Axboe | 122426d | 2009-07-07 23:03:06 +0200 | [diff] [blame] | 435 | alloc_size += sizeof(unsigned int); |
| 436 | alloc_size = (alloc_size + int_mask) & ~int_mask; |
Jens Axboe | 55f6491 | 2008-05-26 09:37:21 +0200 | [diff] [blame] | 437 | #endif |
Jens Axboe | ec996e9 | 2008-05-27 11:39:39 +0200 | [diff] [blame] | 438 | |
| 439 | ptr = __smalloc_pool(pool, alloc_size); |
Jens Axboe | 89da54e | 2008-05-27 20:49:29 +0200 | [diff] [blame] | 440 | if (ptr) { |
| 441 | struct block_hdr *hdr = ptr; |
Jens Axboe | ec996e9 | 2008-05-27 11:39:39 +0200 | [diff] [blame] | 442 | |
Jens Axboe | 89da54e | 2008-05-27 20:49:29 +0200 | [diff] [blame] | 443 | hdr->size = alloc_size; |
| 444 | fill_redzone(hdr); |
Jens Axboe | ec996e9 | 2008-05-27 11:39:39 +0200 | [diff] [blame] | 445 | |
Jens Axboe | 89da54e | 2008-05-27 20:49:29 +0200 | [diff] [blame] | 446 | ptr += sizeof(*hdr); |
| 447 | memset(ptr, 0, size); |
| 448 | } |
Jens Axboe | ec996e9 | 2008-05-27 11:39:39 +0200 | [diff] [blame] | 449 | |
Jens Axboe | ec996e9 | 2008-05-27 11:39:39 +0200 | [diff] [blame] | 450 | return ptr; |
Jens Axboe | 55f6491 | 2008-05-26 09:37:21 +0200 | [diff] [blame] | 451 | } |
| 452 | |
Jens Axboe | 7982aa7 | 2012-11-02 16:35:18 +0100 | [diff] [blame] | 453 | void *smalloc(size_t size) |
Jens Axboe | d24c33a | 2008-03-01 18:27:36 +0100 | [diff] [blame] | 454 | { |
Jens Axboe | d19a46f | 2015-03-03 15:13:17 -0700 | [diff] [blame] | 455 | unsigned int i, end_pool; |
Jens Axboe | d24c33a | 2008-03-01 18:27:36 +0100 | [diff] [blame] | 456 | |
Jens Axboe | 7982aa7 | 2012-11-02 16:35:18 +0100 | [diff] [blame] | 457 | if (size != (unsigned int) size) |
| 458 | return NULL; |
| 459 | |
Shaozhi Shawn Ye | d1271dc | 2008-09-10 09:02:51 +0200 | [diff] [blame] | 460 | global_write_lock(); |
Jens Axboe | d24c33a | 2008-03-01 18:27:36 +0100 | [diff] [blame] | 461 | i = last_pool; |
Jens Axboe | d19a46f | 2015-03-03 15:13:17 -0700 | [diff] [blame] | 462 | end_pool = nr_pools; |
Jens Axboe | d24c33a | 2008-03-01 18:27:36 +0100 | [diff] [blame] | 463 | |
| 464 | do { |
Jens Axboe | d19a46f | 2015-03-03 15:13:17 -0700 | [diff] [blame] | 465 | for (; i < end_pool; i++) { |
Jens Axboe | d24c33a | 2008-03-01 18:27:36 +0100 | [diff] [blame] | 466 | void *ptr = smalloc_pool(&mp[i], size); |
| 467 | |
| 468 | if (ptr) { |
| 469 | last_pool = i; |
Shaozhi Shawn Ye | d1271dc | 2008-09-10 09:02:51 +0200 | [diff] [blame] | 470 | global_write_unlock(); |
Jens Axboe | d24c33a | 2008-03-01 18:27:36 +0100 | [diff] [blame] | 471 | return ptr; |
| 472 | } |
| 473 | } |
| 474 | if (last_pool) { |
Jens Axboe | d19a46f | 2015-03-03 15:13:17 -0700 | [diff] [blame] | 475 | end_pool = last_pool; |
| 476 | last_pool = i = 0; |
Jens Axboe | d24c33a | 2008-03-01 18:27:36 +0100 | [diff] [blame] | 477 | continue; |
| 478 | } |
| 479 | |
Jens Axboe | d19a46f | 2015-03-03 15:13:17 -0700 | [diff] [blame] | 480 | break; |
Jens Axboe | d24c33a | 2008-03-01 18:27:36 +0100 | [diff] [blame] | 481 | } while (1); |
| 482 | |
Shaozhi Shawn Ye | d1271dc | 2008-09-10 09:02:51 +0200 | [diff] [blame] | 483 | global_write_unlock(); |
Jens Axboe | d24c33a | 2008-03-01 18:27:36 +0100 | [diff] [blame] | 484 | return NULL; |
| 485 | } |
| 486 | |
Jens Axboe | dc068d5 | 2014-11-06 15:19:31 -0700 | [diff] [blame] | 487 | void *scalloc(size_t nmemb, size_t size) |
| 488 | { |
| 489 | void *ret; |
| 490 | |
| 491 | ret = smalloc(nmemb * size); |
| 492 | if (ret) |
| 493 | memset(ret, 0, nmemb * size); |
| 494 | |
| 495 | return ret; |
| 496 | } |
| 497 | |
Jens Axboe | d24c33a | 2008-03-01 18:27:36 +0100 | [diff] [blame] | 498 | char *smalloc_strdup(const char *str) |
| 499 | { |
Christian Ehrhardt | eb433fd | 2015-03-03 12:44:46 +0100 | [diff] [blame] | 500 | char *ptr = NULL; |
Jens Axboe | d24c33a | 2008-03-01 18:27:36 +0100 | [diff] [blame] | 501 | |
| 502 | ptr = smalloc(strlen(str) + 1); |
Christian Ehrhardt | eb433fd | 2015-03-03 12:44:46 +0100 | [diff] [blame] | 503 | if (ptr) |
| 504 | strcpy(ptr, str); |
Jens Axboe | d24c33a | 2008-03-01 18:27:36 +0100 | [diff] [blame] | 505 | return ptr; |
| 506 | } |