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> |
Willy Tarreau | a1bd1fa | 2019-03-29 17:26:33 +0100 | [diff] [blame] | 29 | #include <inttypes.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 | |
Willy Tarreau | 7f0165e | 2018-11-26 17:09:46 +0100 | [diff] [blame] | 66 | extern struct pool_cache_head pool_cache[][MAX_BASE_POOLS]; |
Willy Tarreau | e18db9e | 2018-10-16 10:28:54 +0200 | [diff] [blame] | 67 | extern THREAD_LOCAL size_t pool_cache_bytes; /* total cache size */ |
| 68 | extern THREAD_LOCAL size_t pool_cache_count; /* #cache objects */ |
| 69 | |
Willy Tarreau | f161d0f | 2018-02-22 14:05:55 +0100 | [diff] [blame] | 70 | #ifdef CONFIG_HAP_LOCKLESS_POOLS |
Olivier Houchard | cf975d4 | 2018-01-24 18:38:31 +0100 | [diff] [blame] | 71 | struct pool_free_list { |
| 72 | void **free_list; |
| 73 | uintptr_t seq; |
| 74 | }; |
| 75 | #endif |
| 76 | |
Willy Tarreau | 50e608d | 2007-05-13 18:26:08 +0200 | [diff] [blame] | 77 | struct pool_head { |
Willy Tarreau | 1ca1b70 | 2017-11-26 10:50:36 +0100 | [diff] [blame] | 78 | void **free_list; |
Willy Tarreau | f161d0f | 2018-02-22 14:05:55 +0100 | [diff] [blame] | 79 | #ifdef CONFIG_HAP_LOCKLESS_POOLS |
Olivier Houchard | cf975d4 | 2018-01-24 18:38:31 +0100 | [diff] [blame] | 80 | uintptr_t seq; |
Olivier Houchard | 899fb8a | 2020-03-18 15:48:29 +0100 | [diff] [blame] | 81 | HA_SPINLOCK_T flush_lock; |
Olivier Houchard | cf975d4 | 2018-01-24 18:38:31 +0100 | [diff] [blame] | 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 | 2455ceb | 2018-11-26 15:57:34 +0100 | [diff] [blame] | 164 | void pool_destroy_all(); |
Willy Tarreau | e6ce59d | 2007-05-13 19:38:49 +0200 | [diff] [blame] | 165 | |
Willy Tarreau | 0a93b64 | 2018-10-16 07:58:39 +0200 | [diff] [blame] | 166 | /* returns the pool index for pool <pool>, or -1 if this pool has no index */ |
| 167 | static inline ssize_t pool_get_index(const struct pool_head *pool) |
| 168 | { |
| 169 | size_t idx; |
| 170 | |
| 171 | idx = pool - pool_base_start; |
| 172 | if (idx >= MAX_BASE_POOLS) |
| 173 | return -1; |
| 174 | return idx; |
| 175 | } |
| 176 | |
Willy Tarreau | f161d0f | 2018-02-22 14:05:55 +0100 | [diff] [blame] | 177 | #ifdef CONFIG_HAP_LOCKLESS_POOLS |
Willy Tarreau | e18db9e | 2018-10-16 10:28:54 +0200 | [diff] [blame] | 178 | |
| 179 | /* Tries to retrieve an object from the local pool cache corresponding to pool |
| 180 | * <pool>. Returns NULL if none is available. |
| 181 | */ |
| 182 | static inline void *__pool_get_from_cache(struct pool_head *pool) |
| 183 | { |
| 184 | ssize_t idx = pool_get_index(pool); |
| 185 | struct pool_cache_item *item; |
Willy Tarreau | 7f0165e | 2018-11-26 17:09:46 +0100 | [diff] [blame] | 186 | struct pool_cache_head *ph; |
Willy Tarreau | e18db9e | 2018-10-16 10:28:54 +0200 | [diff] [blame] | 187 | |
| 188 | /* pool not in cache */ |
| 189 | if (idx < 0) |
| 190 | return NULL; |
| 191 | |
Willy Tarreau | 7f0165e | 2018-11-26 17:09:46 +0100 | [diff] [blame] | 192 | ph = &pool_cache[tid][idx]; |
| 193 | if (LIST_ISEMPTY(&ph->list)) |
| 194 | return NULL; // empty |
Willy Tarreau | e18db9e | 2018-10-16 10:28:54 +0200 | [diff] [blame] | 195 | |
Willy Tarreau | 7f0165e | 2018-11-26 17:09:46 +0100 | [diff] [blame] | 196 | item = LIST_NEXT(&ph->list, typeof(item), by_pool); |
| 197 | ph->count--; |
| 198 | pool_cache_bytes -= ph->size; |
Willy Tarreau | e18db9e | 2018-10-16 10:28:54 +0200 | [diff] [blame] | 199 | pool_cache_count--; |
| 200 | LIST_DEL(&item->by_pool); |
| 201 | LIST_DEL(&item->by_lru); |
Willy Tarreau | 8e9f453 | 2018-10-28 20:09:12 +0100 | [diff] [blame] | 202 | #ifdef DEBUG_MEMORY_POOLS |
| 203 | /* keep track of where the element was allocated from */ |
| 204 | *POOL_LINK(pool, item) = (void *)pool; |
| 205 | #endif |
Willy Tarreau | e18db9e | 2018-10-16 10:28:54 +0200 | [diff] [blame] | 206 | return item; |
| 207 | } |
| 208 | |
Olivier Houchard | cf975d4 | 2018-01-24 18:38:31 +0100 | [diff] [blame] | 209 | /* |
| 210 | * Returns a pointer to type <type> taken from the pool <pool_type> if |
| 211 | * available, otherwise returns NULL. No malloc() is attempted, and poisonning |
| 212 | * is never performed. The purpose is to get the fastest possible allocation. |
| 213 | */ |
| 214 | static inline void *__pool_get_first(struct pool_head *pool) |
| 215 | { |
| 216 | struct pool_free_list cmp, new; |
Willy Tarreau | e18db9e | 2018-10-16 10:28:54 +0200 | [diff] [blame] | 217 | void *ret = __pool_get_from_cache(pool); |
| 218 | |
| 219 | if (ret) |
| 220 | return ret; |
Olivier Houchard | cf975d4 | 2018-01-24 18:38:31 +0100 | [diff] [blame] | 221 | |
| 222 | cmp.seq = pool->seq; |
| 223 | __ha_barrier_load(); |
| 224 | |
| 225 | cmp.free_list = pool->free_list; |
| 226 | do { |
Olivier Houchard | 899fb8a | 2020-03-18 15:48:29 +0100 | [diff] [blame] | 227 | if (cmp.free_list == NULL) |
Olivier Houchard | cf975d4 | 2018-01-24 18:38:31 +0100 | [diff] [blame] | 228 | return NULL; |
| 229 | new.seq = cmp.seq + 1; |
| 230 | __ha_barrier_load(); |
| 231 | new.free_list = *POOL_LINK(pool, cmp.free_list); |
Willy Tarreau | 6a38b32 | 2019-05-11 18:04:24 +0200 | [diff] [blame] | 232 | } while (HA_ATOMIC_DWCAS((void *)&pool->free_list, (void *)&cmp, (void *)&new) == 0); |
Olivier Houchard | 2087276 | 2019-03-08 18:53:35 +0100 | [diff] [blame] | 233 | __ha_barrier_atomic_store(); |
Tim Duesterhus | 05f6a43 | 2018-02-20 00:49:46 +0100 | [diff] [blame] | 234 | |
Olivier Houchard | 2087276 | 2019-03-08 18:53:35 +0100 | [diff] [blame] | 235 | _HA_ATOMIC_ADD(&pool->used, 1); |
Olivier Houchard | cf975d4 | 2018-01-24 18:38:31 +0100 | [diff] [blame] | 236 | #ifdef DEBUG_MEMORY_POOLS |
| 237 | /* keep track of where the element was allocated from */ |
| 238 | *POOL_LINK(pool, cmp.free_list) = (void *)pool; |
| 239 | #endif |
| 240 | return cmp.free_list; |
| 241 | } |
| 242 | |
| 243 | static inline void *pool_get_first(struct pool_head *pool) |
| 244 | { |
| 245 | void *ret; |
| 246 | |
| 247 | ret = __pool_get_first(pool); |
| 248 | return ret; |
| 249 | } |
| 250 | /* |
| 251 | * Returns a pointer to type <type> taken from the pool <pool_type> or |
| 252 | * dynamically allocated. In the first case, <pool_type> is updated to point to |
| 253 | * the next element in the list. No memory poisonning is ever performed on the |
| 254 | * returned area. |
| 255 | */ |
| 256 | static inline void *pool_alloc_dirty(struct pool_head *pool) |
| 257 | { |
| 258 | void *p; |
| 259 | |
| 260 | if ((p = __pool_get_first(pool)) == NULL) |
| 261 | p = __pool_refill_alloc(pool, 0); |
| 262 | return p; |
| 263 | } |
| 264 | |
Willy Tarreau | e6ce59d | 2007-05-13 19:38:49 +0200 | [diff] [blame] | 265 | /* |
Olivier Houchard | cf975d4 | 2018-01-24 18:38:31 +0100 | [diff] [blame] | 266 | * Returns a pointer to type <type> taken from the pool <pool_type> or |
| 267 | * dynamically allocated. In the first case, <pool_type> is updated to point to |
| 268 | * the next element in the list. Memory poisonning is performed if enabled. |
| 269 | */ |
| 270 | static inline void *pool_alloc(struct pool_head *pool) |
| 271 | { |
| 272 | void *p; |
| 273 | |
| 274 | p = pool_alloc_dirty(pool); |
Olivier Houchard | cf975d4 | 2018-01-24 18:38:31 +0100 | [diff] [blame] | 275 | if (p && mem_poison_byte >= 0) { |
| 276 | memset(p, mem_poison_byte, pool->size); |
| 277 | } |
| 278 | |
| 279 | return p; |
| 280 | } |
| 281 | |
Willy Tarreau | 146794d | 2018-10-16 08:55:15 +0200 | [diff] [blame] | 282 | /* Locklessly add item <ptr> to pool <pool>, then update the pool used count. |
| 283 | * Both the pool and the pointer must be valid. Use pool_free() for normal |
| 284 | * operations. |
| 285 | */ |
| 286 | static inline void __pool_free(struct pool_head *pool, void *ptr) |
| 287 | { |
Willy Tarreau | 7a6ad88 | 2018-10-20 17:37:38 +0200 | [diff] [blame] | 288 | void **free_list = pool->free_list; |
Willy Tarreau | 146794d | 2018-10-16 08:55:15 +0200 | [diff] [blame] | 289 | |
| 290 | do { |
| 291 | *POOL_LINK(pool, ptr) = (void *)free_list; |
| 292 | __ha_barrier_store(); |
Olivier Houchard | 2087276 | 2019-03-08 18:53:35 +0100 | [diff] [blame] | 293 | } while (!_HA_ATOMIC_CAS(&pool->free_list, &free_list, ptr)); |
| 294 | __ha_barrier_atomic_store(); |
| 295 | _HA_ATOMIC_SUB(&pool->used, 1); |
Willy Tarreau | 146794d | 2018-10-16 08:55:15 +0200 | [diff] [blame] | 296 | } |
| 297 | |
Willy Tarreau | e18db9e | 2018-10-16 10:28:54 +0200 | [diff] [blame] | 298 | /* frees an object to the local cache, possibly pushing oldest objects to the |
| 299 | * global pool. |
| 300 | */ |
| 301 | void __pool_put_to_cache(struct pool_head *pool, void *ptr, ssize_t idx); |
| 302 | static inline void pool_put_to_cache(struct pool_head *pool, void *ptr) |
| 303 | { |
| 304 | ssize_t idx = pool_get_index(pool); |
| 305 | |
| 306 | /* pool not in cache or too many objects for this pool (more than |
| 307 | * half of the cache is used and this pool uses more than 1/8 of |
| 308 | * the cache size). |
| 309 | */ |
| 310 | if (idx < 0 || |
| 311 | (pool_cache_bytes > CONFIG_HAP_POOL_CACHE_SIZE * 3 / 4 && |
Willy Tarreau | 7f0165e | 2018-11-26 17:09:46 +0100 | [diff] [blame] | 312 | pool_cache[tid][idx].count >= 16 + pool_cache_count / 8)) { |
Willy Tarreau | e18db9e | 2018-10-16 10:28:54 +0200 | [diff] [blame] | 313 | __pool_free(pool, ptr); |
| 314 | return; |
| 315 | } |
| 316 | __pool_put_to_cache(pool, ptr, idx); |
| 317 | } |
| 318 | |
Olivier Houchard | cf975d4 | 2018-01-24 18:38:31 +0100 | [diff] [blame] | 319 | /* |
| 320 | * Puts a memory area back to the corresponding pool. |
| 321 | * Items are chained directly through a pointer that |
| 322 | * is written in the beginning of the memory area, so |
| 323 | * there's no need for any carrier cell. This implies |
| 324 | * that each memory area is at least as big as one |
| 325 | * pointer. Just like with the libc's free(), nothing |
| 326 | * is done if <ptr> is NULL. |
| 327 | */ |
| 328 | static inline void pool_free(struct pool_head *pool, void *ptr) |
| 329 | { |
| 330 | if (likely(ptr != NULL)) { |
Olivier Houchard | cf975d4 | 2018-01-24 18:38:31 +0100 | [diff] [blame] | 331 | #ifdef DEBUG_MEMORY_POOLS |
| 332 | /* we'll get late corruption if we refill to the wrong pool or double-free */ |
| 333 | if (*POOL_LINK(pool, ptr) != (void *)pool) |
Willy Tarreau | e4d4255 | 2020-03-14 11:08:16 +0100 | [diff] [blame] | 334 | *DISGUISE((volatile int *)0) = 0; |
Olivier Houchard | cf975d4 | 2018-01-24 18:38:31 +0100 | [diff] [blame] | 335 | #endif |
Willy Tarreau | da52035 | 2019-11-15 06:59:54 +0100 | [diff] [blame] | 336 | if (mem_poison_byte >= 0) |
| 337 | memset(ptr, mem_poison_byte, pool->size); |
Willy Tarreau | e18db9e | 2018-10-16 10:28:54 +0200 | [diff] [blame] | 338 | pool_put_to_cache(pool, ptr); |
Olivier Houchard | cf975d4 | 2018-01-24 18:38:31 +0100 | [diff] [blame] | 339 | } |
| 340 | } |
| 341 | |
Willy Tarreau | f161d0f | 2018-02-22 14:05:55 +0100 | [diff] [blame] | 342 | #else /* CONFIG_HAP_LOCKLESS_POOLS */ |
Olivier Houchard | cf975d4 | 2018-01-24 18:38:31 +0100 | [diff] [blame] | 343 | /* |
Willy Tarreau | 0262241 | 2014-12-08 16:35:23 +0100 | [diff] [blame] | 344 | * Returns a pointer to type <type> taken from the pool <pool_type> if |
| 345 | * available, otherwise returns NULL. No malloc() is attempted, and poisonning |
| 346 | * is never performed. The purpose is to get the fastest possible allocation. |
Willy Tarreau | 50e608d | 2007-05-13 18:26:08 +0200 | [diff] [blame] | 347 | */ |
Christopher Faulet | b349e48 | 2017-08-29 09:52:38 +0200 | [diff] [blame] | 348 | static inline void *__pool_get_first(struct pool_head *pool) |
Willy Tarreau | e430e77 | 2014-12-23 14:13:16 +0100 | [diff] [blame] | 349 | { |
| 350 | void *p; |
| 351 | |
Willy Tarreau | 0262241 | 2014-12-08 16:35:23 +0100 | [diff] [blame] | 352 | if ((p = pool->free_list) != NULL) { |
Willy Tarreau | ac42111 | 2015-10-28 15:09:29 +0100 | [diff] [blame] | 353 | pool->free_list = *POOL_LINK(pool, p); |
Willy Tarreau | e430e77 | 2014-12-23 14:13:16 +0100 | [diff] [blame] | 354 | pool->used++; |
Willy Tarreau | de30a68 | 2015-10-28 15:23:51 +0100 | [diff] [blame] | 355 | #ifdef DEBUG_MEMORY_POOLS |
| 356 | /* keep track of where the element was allocated from */ |
| 357 | *POOL_LINK(pool, p) = (void *)pool; |
| 358 | #endif |
Willy Tarreau | e430e77 | 2014-12-23 14:13:16 +0100 | [diff] [blame] | 359 | } |
| 360 | return p; |
| 361 | } |
Willy Tarreau | 50e608d | 2007-05-13 18:26:08 +0200 | [diff] [blame] | 362 | |
Christopher Faulet | b349e48 | 2017-08-29 09:52:38 +0200 | [diff] [blame] | 363 | static inline void *pool_get_first(struct pool_head *pool) |
| 364 | { |
| 365 | void *ret; |
| 366 | |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 367 | HA_SPIN_LOCK(POOL_LOCK, &pool->lock); |
Christopher Faulet | b349e48 | 2017-08-29 09:52:38 +0200 | [diff] [blame] | 368 | ret = __pool_get_first(pool); |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 369 | HA_SPIN_UNLOCK(POOL_LOCK, &pool->lock); |
Christopher Faulet | b349e48 | 2017-08-29 09:52:38 +0200 | [diff] [blame] | 370 | return ret; |
| 371 | } |
Willy Tarreau | 50e608d | 2007-05-13 18:26:08 +0200 | [diff] [blame] | 372 | /* |
Willy Tarreau | 0262241 | 2014-12-08 16:35:23 +0100 | [diff] [blame] | 373 | * Returns a pointer to type <type> taken from the pool <pool_type> or |
| 374 | * dynamically allocated. In the first case, <pool_type> is updated to point to |
| 375 | * the next element in the list. No memory poisonning is ever performed on the |
| 376 | * returned area. |
| 377 | */ |
| 378 | static inline void *pool_alloc_dirty(struct pool_head *pool) |
| 379 | { |
| 380 | void *p; |
| 381 | |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 382 | HA_SPIN_LOCK(POOL_LOCK, &pool->lock); |
Christopher Faulet | b349e48 | 2017-08-29 09:52:38 +0200 | [diff] [blame] | 383 | if ((p = __pool_get_first(pool)) == NULL) |
| 384 | p = __pool_refill_alloc(pool, 0); |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 385 | HA_SPIN_UNLOCK(POOL_LOCK, &pool->lock); |
Willy Tarreau | 0262241 | 2014-12-08 16:35:23 +0100 | [diff] [blame] | 386 | return p; |
| 387 | } |
| 388 | |
Willy Tarreau | 158fa75 | 2017-11-22 15:47:29 +0100 | [diff] [blame] | 389 | #ifndef DEBUG_UAF /* normal allocator */ |
| 390 | |
Willy Tarreau | f13322e | 2017-11-22 10:50:54 +0100 | [diff] [blame] | 391 | /* allocates an area of size <size> and returns it. The semantics are similar |
| 392 | * to those of malloc(). |
| 393 | */ |
| 394 | static inline void *pool_alloc_area(size_t size) |
| 395 | { |
| 396 | return malloc(size); |
| 397 | } |
| 398 | |
| 399 | /* frees an area <area> of size <size> allocated by pool_alloc_area(). The |
| 400 | * semantics are identical to free() except that the size is specified and |
| 401 | * may be ignored. |
| 402 | */ |
| 403 | static inline void pool_free_area(void *area, size_t __maybe_unused size) |
| 404 | { |
| 405 | free(area); |
| 406 | } |
| 407 | |
Willy Tarreau | 158fa75 | 2017-11-22 15:47:29 +0100 | [diff] [blame] | 408 | #else /* use-after-free detector */ |
| 409 | |
| 410 | /* allocates an area of size <size> and returns it. The semantics are similar |
| 411 | * to those of malloc(). However the allocation is rounded up to 4kB so that a |
| 412 | * full page is allocated. This ensures the object can be freed alone so that |
| 413 | * future dereferences are easily detected. The returned object is always |
Willy Tarreau | 364d745 | 2018-02-22 14:14:23 +0100 | [diff] [blame] | 414 | * 16-bytes aligned to avoid issues with unaligned structure objects. In case |
| 415 | * some padding is added, the area's start address is copied at the end of the |
| 416 | * padding to help detect underflows. |
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 | #include <errno.h> |
Willy Tarreau | 158fa75 | 2017-11-22 15:47:29 +0100 | [diff] [blame] | 419 | static inline void *pool_alloc_area(size_t size) |
| 420 | { |
| 421 | size_t pad = (4096 - size) & 0xFF0; |
Willy Tarreau | 229e739 | 2019-08-08 07:38:19 +0200 | [diff] [blame] | 422 | int isolated; |
Willy Tarreau | 5a9cce4 | 2018-02-22 11:39:23 +0100 | [diff] [blame] | 423 | void *ret; |
Willy Tarreau | 158fa75 | 2017-11-22 15:47:29 +0100 | [diff] [blame] | 424 | |
Willy Tarreau | 229e739 | 2019-08-08 07:38:19 +0200 | [diff] [blame] | 425 | isolated = thread_isolated(); |
| 426 | if (!isolated) |
| 427 | thread_harmless_now(); |
Olivier Houchard | 62975a7 | 2018-10-21 01:33:11 +0200 | [diff] [blame] | 428 | ret = mmap(NULL, (size + 4095) & -4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0); |
Willy Tarreau | 85b2cae | 2019-07-04 16:18:23 +0200 | [diff] [blame] | 429 | if (ret != MAP_FAILED) { |
| 430 | /* let's dereference the page before returning so that the real |
| 431 | * allocation in the system is performed without holding the lock. |
| 432 | */ |
| 433 | *(int *)ret = 0; |
| 434 | if (pad >= sizeof(void *)) |
| 435 | *(void **)(ret + pad - sizeof(void *)) = ret + pad; |
| 436 | ret += pad; |
| 437 | } else { |
| 438 | ret = NULL; |
| 439 | } |
Willy Tarreau | 229e739 | 2019-08-08 07:38:19 +0200 | [diff] [blame] | 440 | if (!isolated) |
| 441 | thread_harmless_end(); |
Willy Tarreau | 85b2cae | 2019-07-04 16:18:23 +0200 | [diff] [blame] | 442 | return ret; |
Willy Tarreau | 158fa75 | 2017-11-22 15:47:29 +0100 | [diff] [blame] | 443 | } |
| 444 | |
| 445 | /* frees an area <area> of size <size> allocated by pool_alloc_area(). The |
| 446 | * semantics are identical to free() except that the size must absolutely match |
Willy Tarreau | 364d745 | 2018-02-22 14:14:23 +0100 | [diff] [blame] | 447 | * the one passed to pool_alloc_area(). In case some padding is added, the |
| 448 | * area's start address is compared to the one at the end of the padding, and |
| 449 | * a segfault is triggered if they don't match, indicating an underflow. |
Willy Tarreau | 158fa75 | 2017-11-22 15:47:29 +0100 | [diff] [blame] | 450 | */ |
| 451 | static inline void pool_free_area(void *area, size_t size) |
| 452 | { |
| 453 | size_t pad = (4096 - size) & 0xFF0; |
| 454 | |
Willy Tarreau | 364d745 | 2018-02-22 14:14:23 +0100 | [diff] [blame] | 455 | if (pad >= sizeof(void *) && *(void **)(area - sizeof(void *)) != area) |
Willy Tarreau | e4d4255 | 2020-03-14 11:08:16 +0100 | [diff] [blame] | 456 | *DISGUISE((volatile int *)0) = 0; |
Willy Tarreau | 364d745 | 2018-02-22 14:14:23 +0100 | [diff] [blame] | 457 | |
Willy Tarreau | 85b2cae | 2019-07-04 16:18:23 +0200 | [diff] [blame] | 458 | thread_harmless_now(); |
Willy Tarreau | 158fa75 | 2017-11-22 15:47:29 +0100 | [diff] [blame] | 459 | munmap(area - pad, (size + 4095) & -4096); |
Willy Tarreau | 85b2cae | 2019-07-04 16:18:23 +0200 | [diff] [blame] | 460 | thread_harmless_end(); |
Willy Tarreau | 158fa75 | 2017-11-22 15:47:29 +0100 | [diff] [blame] | 461 | } |
| 462 | |
| 463 | #endif /* DEBUG_UAF */ |
| 464 | |
Willy Tarreau | 0262241 | 2014-12-08 16:35:23 +0100 | [diff] [blame] | 465 | /* |
| 466 | * Returns a pointer to type <type> taken from the pool <pool_type> or |
| 467 | * dynamically allocated. In the first case, <pool_type> is updated to point to |
| 468 | * the next element in the list. Memory poisonning is performed if enabled. |
| 469 | */ |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 470 | static inline void *pool_alloc(struct pool_head *pool) |
Willy Tarreau | 0262241 | 2014-12-08 16:35:23 +0100 | [diff] [blame] | 471 | { |
| 472 | void *p; |
| 473 | |
| 474 | p = pool_alloc_dirty(pool); |
Willy Tarreau | de30a68 | 2015-10-28 15:23:51 +0100 | [diff] [blame] | 475 | if (p && mem_poison_byte >= 0) { |
Willy Tarreau | 0262241 | 2014-12-08 16:35:23 +0100 | [diff] [blame] | 476 | memset(p, mem_poison_byte, pool->size); |
Willy Tarreau | de30a68 | 2015-10-28 15:23:51 +0100 | [diff] [blame] | 477 | } |
| 478 | |
Willy Tarreau | 0262241 | 2014-12-08 16:35:23 +0100 | [diff] [blame] | 479 | return p; |
| 480 | } |
| 481 | |
| 482 | /* |
Willy Tarreau | 50e608d | 2007-05-13 18:26:08 +0200 | [diff] [blame] | 483 | * Puts a memory area back to the corresponding pool. |
| 484 | * Items are chained directly through a pointer that |
| 485 | * is written in the beginning of the memory area, so |
| 486 | * there's no need for any carrier cell. This implies |
| 487 | * that each memory area is at least as big as one |
Willy Tarreau | 48d63db | 2008-08-03 17:41:33 +0200 | [diff] [blame] | 488 | * pointer. Just like with the libc's free(), nothing |
| 489 | * is done if <ptr> is NULL. |
Willy Tarreau | 50e608d | 2007-05-13 18:26:08 +0200 | [diff] [blame] | 490 | */ |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 491 | static inline void pool_free(struct pool_head *pool, void *ptr) |
Willy Tarreau | e430e77 | 2014-12-23 14:13:16 +0100 | [diff] [blame] | 492 | { |
| 493 | if (likely(ptr != NULL)) { |
Willy Tarreau | de30a68 | 2015-10-28 15:23:51 +0100 | [diff] [blame] | 494 | #ifdef DEBUG_MEMORY_POOLS |
| 495 | /* we'll get late corruption if we refill to the wrong pool or double-free */ |
| 496 | if (*POOL_LINK(pool, ptr) != (void *)pool) |
Willy Tarreau | e4d4255 | 2020-03-14 11:08:16 +0100 | [diff] [blame] | 497 | *DISGUISE((volatile int *)0) = 0; |
Willy Tarreau | de30a68 | 2015-10-28 15:23:51 +0100 | [diff] [blame] | 498 | #endif |
Willy Tarreau | 158fa75 | 2017-11-22 15:47:29 +0100 | [diff] [blame] | 499 | |
| 500 | #ifndef DEBUG_UAF /* normal pool behaviour */ |
Willy Tarreau | 3e853ea | 2019-07-04 11:30:00 +0200 | [diff] [blame] | 501 | HA_SPIN_LOCK(POOL_LOCK, &pool->lock); |
Willy Tarreau | ac42111 | 2015-10-28 15:09:29 +0100 | [diff] [blame] | 502 | *POOL_LINK(pool, ptr) = (void *)pool->free_list; |
Willy Tarreau | 3e853ea | 2019-07-04 11:30:00 +0200 | [diff] [blame] | 503 | pool->free_list = (void *)ptr; |
| 504 | pool->used--; |
| 505 | HA_SPIN_UNLOCK(POOL_LOCK, &pool->lock); |
Willy Tarreau | 158fa75 | 2017-11-22 15:47:29 +0100 | [diff] [blame] | 506 | #else /* release the entry for real to detect use after free */ |
| 507 | /* ensure we crash on double free or free of a const area*/ |
| 508 | *(uint32_t *)ptr = 0xDEADADD4; |
| 509 | pool_free_area(ptr, pool->size + POOL_EXTRA); |
Willy Tarreau | 3e853ea | 2019-07-04 11:30:00 +0200 | [diff] [blame] | 510 | HA_SPIN_LOCK(POOL_LOCK, &pool->lock); |
Willy Tarreau | 158fa75 | 2017-11-22 15:47:29 +0100 | [diff] [blame] | 511 | pool->allocated--; |
Willy Tarreau | 3e853ea | 2019-07-04 11:30:00 +0200 | [diff] [blame] | 512 | pool->used--; |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 513 | HA_SPIN_UNLOCK(POOL_LOCK, &pool->lock); |
Willy Tarreau | 3e853ea | 2019-07-04 11:30:00 +0200 | [diff] [blame] | 514 | #endif /* DEBUG_UAF */ |
Willy Tarreau | e430e77 | 2014-12-23 14:13:16 +0100 | [diff] [blame] | 515 | } |
| 516 | } |
Willy Tarreau | f161d0f | 2018-02-22 14:05:55 +0100 | [diff] [blame] | 517 | #endif /* CONFIG_HAP_LOCKLESS_POOLS */ |
Willy Tarreau | 2dd0d47 | 2006-06-29 17:53:05 +0200 | [diff] [blame] | 518 | #endif /* _COMMON_MEMORY_H */ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 519 | |
| 520 | /* |
| 521 | * Local variables: |
| 522 | * c-indent-level: 8 |
| 523 | * c-basic-offset: 8 |
| 524 | * End: |
| 525 | */ |