Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1 | /* |
Willy Tarreau | 62405a2 | 2014-12-23 13:51:28 +0100 | [diff] [blame] | 2 | * include/common/memory.h |
| 3 | * Memory management definitions.. |
| 4 | * |
| 5 | * Copyright (C) 2000-2014 Willy Tarreau - w@1wt.eu |
| 6 | * |
| 7 | * This library is free software; you can redistribute it and/or |
| 8 | * modify it under the terms of the GNU Lesser General Public |
| 9 | * License as published by the Free Software Foundation, version 2.1 |
| 10 | * exclusively. |
| 11 | * |
| 12 | * This library is distributed in the hope that it will be useful, |
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 15 | * Lesser General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU Lesser General Public |
| 18 | * License along with this library; if not, write to the Free Software |
| 19 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 20 | */ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 21 | |
Willy Tarreau | 2dd0d47 | 2006-06-29 17:53:05 +0200 | [diff] [blame] | 22 | #ifndef _COMMON_MEMORY_H |
| 23 | #define _COMMON_MEMORY_H |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 24 | |
Willy Tarreau | 158fa75 | 2017-11-22 15:47:29 +0100 | [diff] [blame] | 25 | #include <sys/mman.h> |
| 26 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 27 | #include <stdlib.h> |
Willy Tarreau | e430e77 | 2014-12-23 14:13:16 +0100 | [diff] [blame] | 28 | #include <string.h> |
David Carlier | 4ee76d0 | 2018-02-18 19:36:42 +0000 | [diff] [blame] | 29 | #include <stdint.h> |
Willy Tarreau | a7280a1 | 2018-11-26 19:41:40 +0100 | [diff] [blame] | 30 | #include <unistd.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 31 | |
Willy Tarreau | 2dd0d47 | 2006-06-29 17:53:05 +0200 | [diff] [blame] | 32 | #include <common/config.h> |
Willy Tarreau | 50e608d | 2007-05-13 18:26:08 +0200 | [diff] [blame] | 33 | #include <common/mini-clist.h> |
Christopher Faulet | b349e48 | 2017-08-29 09:52:38 +0200 | [diff] [blame] | 34 | #include <common/hathreads.h> |
Willy Tarreau | 7107c8b | 2018-11-26 11:44:35 +0100 | [diff] [blame^] | 35 | #include <common/initcall.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 36 | |
Willy Tarreau | a84dcb8 | 2015-10-28 12:04:02 +0100 | [diff] [blame] | 37 | #ifndef DEBUG_DONT_SHARE_POOLS |
Willy Tarreau | 50e608d | 2007-05-13 18:26:08 +0200 | [diff] [blame] | 38 | #define MEM_F_SHARED 0x1 |
Willy Tarreau | a84dcb8 | 2015-10-28 12:04:02 +0100 | [diff] [blame] | 39 | #else |
| 40 | #define MEM_F_SHARED 0 |
| 41 | #endif |
Willy Tarreau | 581bf81 | 2016-01-25 02:19:13 +0100 | [diff] [blame] | 42 | #define MEM_F_EXACT 0x2 |
Willy Tarreau | 50e608d | 2007-05-13 18:26:08 +0200 | [diff] [blame] | 43 | |
Willy Tarreau | ac42111 | 2015-10-28 15:09:29 +0100 | [diff] [blame] | 44 | /* reserve an extra void* at the end of a pool for linking */ |
| 45 | #ifdef DEBUG_MEMORY_POOLS |
| 46 | #define POOL_EXTRA (sizeof(void *)) |
| 47 | #define POOL_LINK(pool, item) (void **)(((char *)item) + (pool->size)) |
| 48 | #else |
| 49 | #define POOL_EXTRA (0) |
| 50 | #define POOL_LINK(pool, item) ((void **)(item)) |
| 51 | #endif |
| 52 | |
Willy Tarreau | 0a93b64 | 2018-10-16 07:58:39 +0200 | [diff] [blame] | 53 | #define MAX_BASE_POOLS 32 |
| 54 | |
Willy Tarreau | e18db9e | 2018-10-16 10:28:54 +0200 | [diff] [blame] | 55 | struct pool_cache_head { |
| 56 | struct list list; /* head of objects in this pool */ |
| 57 | size_t size; /* size of an object */ |
| 58 | unsigned int count; /* number of objects in this pool */ |
| 59 | }; |
| 60 | |
| 61 | struct pool_cache_item { |
| 62 | struct list by_pool; /* link to objects in this pool */ |
| 63 | struct list by_lru; /* link to objects by LRU order */ |
| 64 | }; |
| 65 | |
| 66 | extern THREAD_LOCAL struct pool_cache_head pool_cache[MAX_BASE_POOLS]; |
| 67 | extern THREAD_LOCAL struct list pool_lru_head; /* oldest objects */ |
| 68 | extern THREAD_LOCAL size_t pool_cache_bytes; /* total cache size */ |
| 69 | extern THREAD_LOCAL size_t pool_cache_count; /* #cache objects */ |
| 70 | |
Willy Tarreau | f161d0f | 2018-02-22 14:05:55 +0100 | [diff] [blame] | 71 | #ifdef CONFIG_HAP_LOCKLESS_POOLS |
Olivier Houchard | cf975d4 | 2018-01-24 18:38:31 +0100 | [diff] [blame] | 72 | struct pool_free_list { |
| 73 | void **free_list; |
| 74 | uintptr_t seq; |
| 75 | }; |
| 76 | #endif |
| 77 | |
Willy Tarreau | 50e608d | 2007-05-13 18:26:08 +0200 | [diff] [blame] | 78 | struct pool_head { |
Willy Tarreau | 1ca1b70 | 2017-11-26 10:50:36 +0100 | [diff] [blame] | 79 | void **free_list; |
Willy Tarreau | f161d0f | 2018-02-22 14:05:55 +0100 | [diff] [blame] | 80 | #ifdef CONFIG_HAP_LOCKLESS_POOLS |
Olivier Houchard | cf975d4 | 2018-01-24 18:38:31 +0100 | [diff] [blame] | 81 | uintptr_t seq; |
| 82 | #else |
| 83 | __decl_hathreads(HA_SPINLOCK_T lock); /* the spin lock */ |
| 84 | #endif |
Willy Tarreau | 50e608d | 2007-05-13 18:26:08 +0200 | [diff] [blame] | 85 | unsigned int used; /* how many chunks are currently in use */ |
| 86 | unsigned int allocated; /* how many chunks have been allocated */ |
| 87 | unsigned int limit; /* hard limit on the number of chunks */ |
| 88 | unsigned int minavail; /* how many chunks are expected to be used */ |
| 89 | unsigned int size; /* chunk size */ |
| 90 | unsigned int flags; /* MEM_F_* */ |
Willy Tarreau | 7dcd46d | 2007-05-14 00:16:13 +0200 | [diff] [blame] | 91 | unsigned int users; /* number of pools sharing this zone */ |
Willy Tarreau | 58102cf | 2015-10-28 16:24:21 +0100 | [diff] [blame] | 92 | unsigned int failed; /* failed allocations */ |
Olivier Houchard | cf975d4 | 2018-01-24 18:38:31 +0100 | [diff] [blame] | 93 | struct list list; /* list of all known pools */ |
Willy Tarreau | 7dcd46d | 2007-05-14 00:16:13 +0200 | [diff] [blame] | 94 | char name[12]; /* name of the pool */ |
Willy Tarreau | 1ca1b70 | 2017-11-26 10:50:36 +0100 | [diff] [blame] | 95 | } __attribute__((aligned(64))); |
Willy Tarreau | 50e608d | 2007-05-13 18:26:08 +0200 | [diff] [blame] | 96 | |
Willy Tarreau | 0a93b64 | 2018-10-16 07:58:39 +0200 | [diff] [blame] | 97 | |
| 98 | extern struct pool_head pool_base_start[MAX_BASE_POOLS]; |
| 99 | extern unsigned int pool_base_count; |
| 100 | |
Willy Tarreau | 067ac9f | 2015-10-08 14:12:13 +0200 | [diff] [blame] | 101 | /* poison each newly allocated area with this byte if >= 0 */ |
| 102 | extern int mem_poison_byte; |
Willy Tarreau | 50e608d | 2007-05-13 18:26:08 +0200 | [diff] [blame] | 103 | |
Willy Tarreau | a885f6d | 2014-12-03 15:25:28 +0100 | [diff] [blame] | 104 | /* Allocates new entries for pool <pool> until there are at least <avail> + 1 |
| 105 | * available, then returns the last one for immediate use, so that at least |
| 106 | * <avail> are left available in the pool upon return. NULL is returned if the |
| 107 | * last entry could not be allocated. It's important to note that at least one |
| 108 | * allocation is always performed even if there are enough entries in the pool. |
| 109 | * A call to the garbage collector is performed at most once in case malloc() |
| 110 | * returns an error, before returning NULL. |
Willy Tarreau | 50e608d | 2007-05-13 18:26:08 +0200 | [diff] [blame] | 111 | */ |
Christopher Faulet | b349e48 | 2017-08-29 09:52:38 +0200 | [diff] [blame] | 112 | void *__pool_refill_alloc(struct pool_head *pool, unsigned int avail); |
Willy Tarreau | a885f6d | 2014-12-03 15:25:28 +0100 | [diff] [blame] | 113 | void *pool_refill_alloc(struct pool_head *pool, unsigned int avail); |
Willy Tarreau | 50e608d | 2007-05-13 18:26:08 +0200 | [diff] [blame] | 114 | |
| 115 | /* Try to find an existing shared pool with the same characteristics and |
| 116 | * returns it, otherwise creates this one. NULL is returned if no memory |
| 117 | * is available for a new creation. |
| 118 | */ |
| 119 | struct pool_head *create_pool(char *name, unsigned int size, unsigned int flags); |
Willy Tarreau | 7107c8b | 2018-11-26 11:44:35 +0100 | [diff] [blame^] | 120 | void create_pool_callback(struct pool_head **ptr, char *name, unsigned int size); |
| 121 | |
| 122 | /* This registers a call to create_pool_callback(ptr, name, size) */ |
| 123 | #define REGISTER_POOL(ptr, name, size) \ |
| 124 | INITCALL3(STG_POOL, create_pool_callback, (ptr), (name), (size)) |
| 125 | |
| 126 | /* This macro declares a pool head <ptr> and registers its creation */ |
| 127 | #define DECLARE_POOL(ptr, name, size) \ |
| 128 | struct pool_head *(ptr) = NULL; \ |
| 129 | REGISTER_POOL(&ptr, name, size) |
| 130 | |
| 131 | /* This macro declares a static pool head <ptr> and registers its creation */ |
| 132 | #define DECLARE_STATIC_POOL(ptr, name, size) \ |
| 133 | static struct pool_head *(ptr); \ |
| 134 | REGISTER_POOL(&ptr, name, size) |
Willy Tarreau | 50e608d | 2007-05-13 18:26:08 +0200 | [diff] [blame] | 135 | |
| 136 | /* Dump statistics on pools usage. |
| 137 | */ |
Willy Tarreau | 12833bb | 2014-01-28 16:49:56 +0100 | [diff] [blame] | 138 | void dump_pools_to_trash(); |
Willy Tarreau | 50e608d | 2007-05-13 18:26:08 +0200 | [diff] [blame] | 139 | void dump_pools(void); |
Willy Tarreau | 58102cf | 2015-10-28 16:24:21 +0100 | [diff] [blame] | 140 | int pool_total_failures(); |
| 141 | unsigned long pool_total_allocated(); |
| 142 | unsigned long pool_total_used(); |
Willy Tarreau | 50e608d | 2007-05-13 18:26:08 +0200 | [diff] [blame] | 143 | |
| 144 | /* |
Willy Tarreau | e6ce59d | 2007-05-13 19:38:49 +0200 | [diff] [blame] | 145 | * This function frees whatever can be freed in pool <pool>. |
| 146 | */ |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 147 | void pool_flush(struct pool_head *pool); |
Willy Tarreau | e6ce59d | 2007-05-13 19:38:49 +0200 | [diff] [blame] | 148 | |
| 149 | /* |
| 150 | * This function frees whatever can be freed in all pools, but respecting |
| 151 | * the minimum thresholds imposed by owners. |
Christopher Faulet | b349e48 | 2017-08-29 09:52:38 +0200 | [diff] [blame] | 152 | * |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 153 | * <pool_ctx> is used when pool_gc is called to release resources to allocate |
Christopher Faulet | b349e48 | 2017-08-29 09:52:38 +0200 | [diff] [blame] | 154 | * an element in __pool_refill_alloc. It is important because <pool_ctx> is |
| 155 | * already locked, so we need to skip the lock here. |
Willy Tarreau | e6ce59d | 2007-05-13 19:38:49 +0200 | [diff] [blame] | 156 | */ |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 157 | void pool_gc(struct pool_head *pool_ctx); |
Willy Tarreau | e6ce59d | 2007-05-13 19:38:49 +0200 | [diff] [blame] | 158 | |
| 159 | /* |
| 160 | * This function destroys a pull by freeing it completely. |
| 161 | * This should be called only under extreme circumstances. |
| 162 | */ |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 163 | void *pool_destroy(struct pool_head *pool); |
Willy Tarreau | e6ce59d | 2007-05-13 19:38:49 +0200 | [diff] [blame] | 164 | |
Willy Tarreau | 0a93b64 | 2018-10-16 07:58:39 +0200 | [diff] [blame] | 165 | /* returns the pool index for pool <pool>, or -1 if this pool has no index */ |
| 166 | static inline ssize_t pool_get_index(const struct pool_head *pool) |
| 167 | { |
| 168 | size_t idx; |
| 169 | |
| 170 | idx = pool - pool_base_start; |
| 171 | if (idx >= MAX_BASE_POOLS) |
| 172 | return -1; |
| 173 | return idx; |
| 174 | } |
| 175 | |
Willy Tarreau | f161d0f | 2018-02-22 14:05:55 +0100 | [diff] [blame] | 176 | #ifdef CONFIG_HAP_LOCKLESS_POOLS |
Willy Tarreau | e18db9e | 2018-10-16 10:28:54 +0200 | [diff] [blame] | 177 | |
| 178 | /* Tries to retrieve an object from the local pool cache corresponding to pool |
| 179 | * <pool>. Returns NULL if none is available. |
| 180 | */ |
| 181 | static inline void *__pool_get_from_cache(struct pool_head *pool) |
| 182 | { |
| 183 | ssize_t idx = pool_get_index(pool); |
| 184 | struct pool_cache_item *item; |
| 185 | |
| 186 | /* pool not in cache */ |
| 187 | if (idx < 0) |
| 188 | return NULL; |
| 189 | |
| 190 | /* never allocated or empty */ |
| 191 | if (pool_cache[idx].list.n == NULL || LIST_ISEMPTY(&pool_cache[idx].list)) |
| 192 | return NULL; |
| 193 | |
| 194 | item = LIST_NEXT(&pool_cache[idx].list, typeof(item), by_pool); |
| 195 | pool_cache[idx].count--; |
| 196 | pool_cache_bytes -= pool_cache[idx].size; |
| 197 | pool_cache_count--; |
| 198 | LIST_DEL(&item->by_pool); |
| 199 | LIST_DEL(&item->by_lru); |
Willy Tarreau | 8e9f453 | 2018-10-28 20:09:12 +0100 | [diff] [blame] | 200 | #ifdef DEBUG_MEMORY_POOLS |
| 201 | /* keep track of where the element was allocated from */ |
| 202 | *POOL_LINK(pool, item) = (void *)pool; |
| 203 | #endif |
Willy Tarreau | e18db9e | 2018-10-16 10:28:54 +0200 | [diff] [blame] | 204 | return item; |
| 205 | } |
| 206 | |
Olivier Houchard | cf975d4 | 2018-01-24 18:38:31 +0100 | [diff] [blame] | 207 | /* |
| 208 | * Returns a pointer to type <type> taken from the pool <pool_type> if |
| 209 | * available, otherwise returns NULL. No malloc() is attempted, and poisonning |
| 210 | * is never performed. The purpose is to get the fastest possible allocation. |
| 211 | */ |
| 212 | static inline void *__pool_get_first(struct pool_head *pool) |
| 213 | { |
| 214 | struct pool_free_list cmp, new; |
Willy Tarreau | e18db9e | 2018-10-16 10:28:54 +0200 | [diff] [blame] | 215 | void *ret = __pool_get_from_cache(pool); |
| 216 | |
| 217 | if (ret) |
| 218 | return ret; |
Olivier Houchard | cf975d4 | 2018-01-24 18:38:31 +0100 | [diff] [blame] | 219 | |
| 220 | cmp.seq = pool->seq; |
| 221 | __ha_barrier_load(); |
| 222 | |
| 223 | cmp.free_list = pool->free_list; |
| 224 | do { |
| 225 | if (cmp.free_list == NULL) |
| 226 | return NULL; |
| 227 | new.seq = cmp.seq + 1; |
| 228 | __ha_barrier_load(); |
| 229 | new.free_list = *POOL_LINK(pool, cmp.free_list); |
| 230 | } while (__ha_cas_dw((void *)&pool->free_list, (void *)&cmp, (void *)&new) == 0); |
Tim Duesterhus | 05f6a43 | 2018-02-20 00:49:46 +0100 | [diff] [blame] | 231 | |
Olivier Houchard | cf975d4 | 2018-01-24 18:38:31 +0100 | [diff] [blame] | 232 | HA_ATOMIC_ADD(&pool->used, 1); |
| 233 | #ifdef DEBUG_MEMORY_POOLS |
| 234 | /* keep track of where the element was allocated from */ |
| 235 | *POOL_LINK(pool, cmp.free_list) = (void *)pool; |
| 236 | #endif |
| 237 | return cmp.free_list; |
| 238 | } |
| 239 | |
| 240 | static inline void *pool_get_first(struct pool_head *pool) |
| 241 | { |
| 242 | void *ret; |
| 243 | |
| 244 | ret = __pool_get_first(pool); |
| 245 | return ret; |
| 246 | } |
| 247 | /* |
| 248 | * Returns a pointer to type <type> taken from the pool <pool_type> or |
| 249 | * dynamically allocated. In the first case, <pool_type> is updated to point to |
| 250 | * the next element in the list. No memory poisonning is ever performed on the |
| 251 | * returned area. |
| 252 | */ |
| 253 | static inline void *pool_alloc_dirty(struct pool_head *pool) |
| 254 | { |
| 255 | void *p; |
| 256 | |
| 257 | if ((p = __pool_get_first(pool)) == NULL) |
| 258 | p = __pool_refill_alloc(pool, 0); |
| 259 | return p; |
| 260 | } |
| 261 | |
Willy Tarreau | e6ce59d | 2007-05-13 19:38:49 +0200 | [diff] [blame] | 262 | /* |
Olivier Houchard | cf975d4 | 2018-01-24 18:38:31 +0100 | [diff] [blame] | 263 | * Returns a pointer to type <type> taken from the pool <pool_type> or |
| 264 | * dynamically allocated. In the first case, <pool_type> is updated to point to |
| 265 | * the next element in the list. Memory poisonning is performed if enabled. |
| 266 | */ |
| 267 | static inline void *pool_alloc(struct pool_head *pool) |
| 268 | { |
| 269 | void *p; |
| 270 | |
| 271 | p = pool_alloc_dirty(pool); |
Olivier Houchard | cf975d4 | 2018-01-24 18:38:31 +0100 | [diff] [blame] | 272 | if (p && mem_poison_byte >= 0) { |
| 273 | memset(p, mem_poison_byte, pool->size); |
| 274 | } |
| 275 | |
| 276 | return p; |
| 277 | } |
| 278 | |
Willy Tarreau | 146794d | 2018-10-16 08:55:15 +0200 | [diff] [blame] | 279 | /* Locklessly add item <ptr> to pool <pool>, then update the pool used count. |
| 280 | * Both the pool and the pointer must be valid. Use pool_free() for normal |
| 281 | * operations. |
| 282 | */ |
| 283 | static inline void __pool_free(struct pool_head *pool, void *ptr) |
| 284 | { |
Willy Tarreau | 7a6ad88 | 2018-10-20 17:37:38 +0200 | [diff] [blame] | 285 | void **free_list = pool->free_list; |
Willy Tarreau | 146794d | 2018-10-16 08:55:15 +0200 | [diff] [blame] | 286 | |
| 287 | do { |
| 288 | *POOL_LINK(pool, ptr) = (void *)free_list; |
| 289 | __ha_barrier_store(); |
Willy Tarreau | 7a6ad88 | 2018-10-20 17:37:38 +0200 | [diff] [blame] | 290 | } while (!HA_ATOMIC_CAS(&pool->free_list, &free_list, ptr)); |
Willy Tarreau | 146794d | 2018-10-16 08:55:15 +0200 | [diff] [blame] | 291 | HA_ATOMIC_SUB(&pool->used, 1); |
| 292 | } |
| 293 | |
Willy Tarreau | e18db9e | 2018-10-16 10:28:54 +0200 | [diff] [blame] | 294 | /* frees an object to the local cache, possibly pushing oldest objects to the |
| 295 | * global pool. |
| 296 | */ |
| 297 | void __pool_put_to_cache(struct pool_head *pool, void *ptr, ssize_t idx); |
| 298 | static inline void pool_put_to_cache(struct pool_head *pool, void *ptr) |
| 299 | { |
| 300 | ssize_t idx = pool_get_index(pool); |
| 301 | |
| 302 | /* pool not in cache or too many objects for this pool (more than |
| 303 | * half of the cache is used and this pool uses more than 1/8 of |
| 304 | * the cache size). |
| 305 | */ |
| 306 | if (idx < 0 || |
| 307 | (pool_cache_bytes > CONFIG_HAP_POOL_CACHE_SIZE * 3 / 4 && |
| 308 | pool_cache[idx].count >= 16 + pool_cache_count / 8)) { |
| 309 | __pool_free(pool, ptr); |
| 310 | return; |
| 311 | } |
| 312 | __pool_put_to_cache(pool, ptr, idx); |
| 313 | } |
| 314 | |
Olivier Houchard | cf975d4 | 2018-01-24 18:38:31 +0100 | [diff] [blame] | 315 | /* |
| 316 | * Puts a memory area back to the corresponding pool. |
| 317 | * Items are chained directly through a pointer that |
| 318 | * is written in the beginning of the memory area, so |
| 319 | * there's no need for any carrier cell. This implies |
| 320 | * that each memory area is at least as big as one |
| 321 | * pointer. Just like with the libc's free(), nothing |
| 322 | * is done if <ptr> is NULL. |
| 323 | */ |
| 324 | static inline void pool_free(struct pool_head *pool, void *ptr) |
| 325 | { |
| 326 | if (likely(ptr != NULL)) { |
Olivier Houchard | cf975d4 | 2018-01-24 18:38:31 +0100 | [diff] [blame] | 327 | #ifdef DEBUG_MEMORY_POOLS |
| 328 | /* we'll get late corruption if we refill to the wrong pool or double-free */ |
| 329 | if (*POOL_LINK(pool, ptr) != (void *)pool) |
| 330 | *(volatile int *)0 = 0; |
| 331 | #endif |
Willy Tarreau | e18db9e | 2018-10-16 10:28:54 +0200 | [diff] [blame] | 332 | pool_put_to_cache(pool, ptr); |
Olivier Houchard | cf975d4 | 2018-01-24 18:38:31 +0100 | [diff] [blame] | 333 | } |
| 334 | } |
| 335 | |
Willy Tarreau | f161d0f | 2018-02-22 14:05:55 +0100 | [diff] [blame] | 336 | #else /* CONFIG_HAP_LOCKLESS_POOLS */ |
Olivier Houchard | cf975d4 | 2018-01-24 18:38:31 +0100 | [diff] [blame] | 337 | /* |
Willy Tarreau | 0262241 | 2014-12-08 16:35:23 +0100 | [diff] [blame] | 338 | * Returns a pointer to type <type> taken from the pool <pool_type> if |
| 339 | * available, otherwise returns NULL. No malloc() is attempted, and poisonning |
| 340 | * is never performed. The purpose is to get the fastest possible allocation. |
Willy Tarreau | 50e608d | 2007-05-13 18:26:08 +0200 | [diff] [blame] | 341 | */ |
Christopher Faulet | b349e48 | 2017-08-29 09:52:38 +0200 | [diff] [blame] | 342 | static inline void *__pool_get_first(struct pool_head *pool) |
Willy Tarreau | e430e77 | 2014-12-23 14:13:16 +0100 | [diff] [blame] | 343 | { |
| 344 | void *p; |
| 345 | |
Willy Tarreau | 0262241 | 2014-12-08 16:35:23 +0100 | [diff] [blame] | 346 | if ((p = pool->free_list) != NULL) { |
Willy Tarreau | ac42111 | 2015-10-28 15:09:29 +0100 | [diff] [blame] | 347 | pool->free_list = *POOL_LINK(pool, p); |
Willy Tarreau | e430e77 | 2014-12-23 14:13:16 +0100 | [diff] [blame] | 348 | pool->used++; |
Willy Tarreau | de30a68 | 2015-10-28 15:23:51 +0100 | [diff] [blame] | 349 | #ifdef DEBUG_MEMORY_POOLS |
| 350 | /* keep track of where the element was allocated from */ |
| 351 | *POOL_LINK(pool, p) = (void *)pool; |
| 352 | #endif |
Willy Tarreau | e430e77 | 2014-12-23 14:13:16 +0100 | [diff] [blame] | 353 | } |
| 354 | return p; |
| 355 | } |
Willy Tarreau | 50e608d | 2007-05-13 18:26:08 +0200 | [diff] [blame] | 356 | |
Christopher Faulet | b349e48 | 2017-08-29 09:52:38 +0200 | [diff] [blame] | 357 | static inline void *pool_get_first(struct pool_head *pool) |
| 358 | { |
| 359 | void *ret; |
| 360 | |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 361 | HA_SPIN_LOCK(POOL_LOCK, &pool->lock); |
Christopher Faulet | b349e48 | 2017-08-29 09:52:38 +0200 | [diff] [blame] | 362 | ret = __pool_get_first(pool); |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 363 | HA_SPIN_UNLOCK(POOL_LOCK, &pool->lock); |
Christopher Faulet | b349e48 | 2017-08-29 09:52:38 +0200 | [diff] [blame] | 364 | return ret; |
| 365 | } |
Willy Tarreau | 50e608d | 2007-05-13 18:26:08 +0200 | [diff] [blame] | 366 | /* |
Willy Tarreau | 0262241 | 2014-12-08 16:35:23 +0100 | [diff] [blame] | 367 | * Returns a pointer to type <type> taken from the pool <pool_type> or |
| 368 | * dynamically allocated. In the first case, <pool_type> is updated to point to |
| 369 | * the next element in the list. No memory poisonning is ever performed on the |
| 370 | * returned area. |
| 371 | */ |
| 372 | static inline void *pool_alloc_dirty(struct pool_head *pool) |
| 373 | { |
| 374 | void *p; |
| 375 | |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 376 | HA_SPIN_LOCK(POOL_LOCK, &pool->lock); |
Christopher Faulet | b349e48 | 2017-08-29 09:52:38 +0200 | [diff] [blame] | 377 | if ((p = __pool_get_first(pool)) == NULL) |
| 378 | p = __pool_refill_alloc(pool, 0); |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 379 | HA_SPIN_UNLOCK(POOL_LOCK, &pool->lock); |
Willy Tarreau | 0262241 | 2014-12-08 16:35:23 +0100 | [diff] [blame] | 380 | return p; |
| 381 | } |
| 382 | |
Willy Tarreau | 158fa75 | 2017-11-22 15:47:29 +0100 | [diff] [blame] | 383 | #ifndef DEBUG_UAF /* normal allocator */ |
| 384 | |
Willy Tarreau | f13322e | 2017-11-22 10:50:54 +0100 | [diff] [blame] | 385 | /* allocates an area of size <size> and returns it. The semantics are similar |
| 386 | * to those of malloc(). |
| 387 | */ |
| 388 | static inline void *pool_alloc_area(size_t size) |
| 389 | { |
| 390 | return malloc(size); |
| 391 | } |
| 392 | |
| 393 | /* frees an area <area> of size <size> allocated by pool_alloc_area(). The |
| 394 | * semantics are identical to free() except that the size is specified and |
| 395 | * may be ignored. |
| 396 | */ |
| 397 | static inline void pool_free_area(void *area, size_t __maybe_unused size) |
| 398 | { |
| 399 | free(area); |
| 400 | } |
| 401 | |
Willy Tarreau | 158fa75 | 2017-11-22 15:47:29 +0100 | [diff] [blame] | 402 | #else /* use-after-free detector */ |
| 403 | |
| 404 | /* allocates an area of size <size> and returns it. The semantics are similar |
| 405 | * to those of malloc(). However the allocation is rounded up to 4kB so that a |
| 406 | * full page is allocated. This ensures the object can be freed alone so that |
| 407 | * future dereferences are easily detected. The returned object is always |
Willy Tarreau | 364d745 | 2018-02-22 14:14:23 +0100 | [diff] [blame] | 408 | * 16-bytes aligned to avoid issues with unaligned structure objects. In case |
| 409 | * some padding is added, the area's start address is copied at the end of the |
| 410 | * padding to help detect underflows. |
Willy Tarreau | 158fa75 | 2017-11-22 15:47:29 +0100 | [diff] [blame] | 411 | */ |
Olivier Houchard | 62975a7 | 2018-10-21 01:33:11 +0200 | [diff] [blame] | 412 | #include <errno.h> |
Willy Tarreau | 158fa75 | 2017-11-22 15:47:29 +0100 | [diff] [blame] | 413 | static inline void *pool_alloc_area(size_t size) |
| 414 | { |
| 415 | size_t pad = (4096 - size) & 0xFF0; |
Willy Tarreau | 5a9cce4 | 2018-02-22 11:39:23 +0100 | [diff] [blame] | 416 | void *ret; |
Willy Tarreau | 158fa75 | 2017-11-22 15:47:29 +0100 | [diff] [blame] | 417 | |
Olivier Houchard | 62975a7 | 2018-10-21 01:33:11 +0200 | [diff] [blame] | 418 | ret = mmap(NULL, (size + 4095) & -4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0); |
Willy Tarreau | 364d745 | 2018-02-22 14:14:23 +0100 | [diff] [blame] | 419 | if (ret == MAP_FAILED) |
| 420 | return NULL; |
| 421 | if (pad >= sizeof(void *)) |
| 422 | *(void **)(ret + pad - sizeof(void *)) = ret + pad; |
| 423 | return ret + pad; |
Willy Tarreau | 158fa75 | 2017-11-22 15:47:29 +0100 | [diff] [blame] | 424 | } |
| 425 | |
| 426 | /* frees an area <area> of size <size> allocated by pool_alloc_area(). The |
| 427 | * semantics are identical to free() except that the size must absolutely match |
Willy Tarreau | 364d745 | 2018-02-22 14:14:23 +0100 | [diff] [blame] | 428 | * the one passed to pool_alloc_area(). In case some padding is added, the |
| 429 | * area's start address is compared to the one at the end of the padding, and |
| 430 | * a segfault is triggered if they don't match, indicating an underflow. |
Willy Tarreau | 158fa75 | 2017-11-22 15:47:29 +0100 | [diff] [blame] | 431 | */ |
| 432 | static inline void pool_free_area(void *area, size_t size) |
| 433 | { |
| 434 | size_t pad = (4096 - size) & 0xFF0; |
| 435 | |
Willy Tarreau | 364d745 | 2018-02-22 14:14:23 +0100 | [diff] [blame] | 436 | if (pad >= sizeof(void *) && *(void **)(area - sizeof(void *)) != area) |
| 437 | *(volatile int *)0 = 0; |
| 438 | |
Willy Tarreau | 158fa75 | 2017-11-22 15:47:29 +0100 | [diff] [blame] | 439 | munmap(area - pad, (size + 4095) & -4096); |
| 440 | } |
| 441 | |
| 442 | #endif /* DEBUG_UAF */ |
| 443 | |
Willy Tarreau | 0262241 | 2014-12-08 16:35:23 +0100 | [diff] [blame] | 444 | /* |
| 445 | * Returns a pointer to type <type> taken from the pool <pool_type> or |
| 446 | * dynamically allocated. In the first case, <pool_type> is updated to point to |
| 447 | * the next element in the list. Memory poisonning is performed if enabled. |
| 448 | */ |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 449 | static inline void *pool_alloc(struct pool_head *pool) |
Willy Tarreau | 0262241 | 2014-12-08 16:35:23 +0100 | [diff] [blame] | 450 | { |
| 451 | void *p; |
| 452 | |
| 453 | p = pool_alloc_dirty(pool); |
Willy Tarreau | de30a68 | 2015-10-28 15:23:51 +0100 | [diff] [blame] | 454 | if (p && mem_poison_byte >= 0) { |
Willy Tarreau | 0262241 | 2014-12-08 16:35:23 +0100 | [diff] [blame] | 455 | memset(p, mem_poison_byte, pool->size); |
Willy Tarreau | de30a68 | 2015-10-28 15:23:51 +0100 | [diff] [blame] | 456 | } |
| 457 | |
Willy Tarreau | 0262241 | 2014-12-08 16:35:23 +0100 | [diff] [blame] | 458 | return p; |
| 459 | } |
| 460 | |
| 461 | /* |
Willy Tarreau | 50e608d | 2007-05-13 18:26:08 +0200 | [diff] [blame] | 462 | * Puts a memory area back to the corresponding pool. |
| 463 | * Items are chained directly through a pointer that |
| 464 | * is written in the beginning of the memory area, so |
| 465 | * there's no need for any carrier cell. This implies |
| 466 | * that each memory area is at least as big as one |
Willy Tarreau | 48d63db | 2008-08-03 17:41:33 +0200 | [diff] [blame] | 467 | * pointer. Just like with the libc's free(), nothing |
| 468 | * is done if <ptr> is NULL. |
Willy Tarreau | 50e608d | 2007-05-13 18:26:08 +0200 | [diff] [blame] | 469 | */ |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 470 | static inline void pool_free(struct pool_head *pool, void *ptr) |
Willy Tarreau | e430e77 | 2014-12-23 14:13:16 +0100 | [diff] [blame] | 471 | { |
| 472 | if (likely(ptr != NULL)) { |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 473 | HA_SPIN_LOCK(POOL_LOCK, &pool->lock); |
Willy Tarreau | de30a68 | 2015-10-28 15:23:51 +0100 | [diff] [blame] | 474 | #ifdef DEBUG_MEMORY_POOLS |
| 475 | /* we'll get late corruption if we refill to the wrong pool or double-free */ |
| 476 | if (*POOL_LINK(pool, ptr) != (void *)pool) |
| 477 | *(int *)0 = 0; |
| 478 | #endif |
Willy Tarreau | 158fa75 | 2017-11-22 15:47:29 +0100 | [diff] [blame] | 479 | |
| 480 | #ifndef DEBUG_UAF /* normal pool behaviour */ |
Willy Tarreau | ac42111 | 2015-10-28 15:09:29 +0100 | [diff] [blame] | 481 | *POOL_LINK(pool, ptr) = (void *)pool->free_list; |
Willy Tarreau | e430e77 | 2014-12-23 14:13:16 +0100 | [diff] [blame] | 482 | pool->free_list = (void *)ptr; |
Willy Tarreau | 158fa75 | 2017-11-22 15:47:29 +0100 | [diff] [blame] | 483 | #else /* release the entry for real to detect use after free */ |
| 484 | /* ensure we crash on double free or free of a const area*/ |
| 485 | *(uint32_t *)ptr = 0xDEADADD4; |
| 486 | pool_free_area(ptr, pool->size + POOL_EXTRA); |
| 487 | pool->allocated--; |
| 488 | #endif /* DEBUG_UAF */ |
Willy Tarreau | e430e77 | 2014-12-23 14:13:16 +0100 | [diff] [blame] | 489 | pool->used--; |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 490 | HA_SPIN_UNLOCK(POOL_LOCK, &pool->lock); |
Willy Tarreau | e430e77 | 2014-12-23 14:13:16 +0100 | [diff] [blame] | 491 | } |
| 492 | } |
Willy Tarreau | f161d0f | 2018-02-22 14:05:55 +0100 | [diff] [blame] | 493 | #endif /* CONFIG_HAP_LOCKLESS_POOLS */ |
Willy Tarreau | 2dd0d47 | 2006-06-29 17:53:05 +0200 | [diff] [blame] | 494 | #endif /* _COMMON_MEMORY_H */ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 495 | |
| 496 | /* |
| 497 | * Local variables: |
| 498 | * c-indent-level: 8 |
| 499 | * c-basic-offset: 8 |
| 500 | * End: |
| 501 | */ |