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 | 3bc4e8b | 2020-05-09 09:02:35 +0200 | [diff] [blame] | 37 | /* On architectures supporting threads and double-word CAS, we can implement |
| 38 | * lock-less memory pools. This isn't supported for debugging modes however. |
| 39 | */ |
| 40 | #if defined(USE_THREAD) && defined(HA_HAVE_CAS_DW) && !defined(DEBUG_NO_LOCKLESS_POOLS) && !defined(DEBUG_UAF) && !defined(DEBUG_FAIL_ALLOC) |
| 41 | #define CONFIG_HAP_LOCKLESS_POOLS |
| 42 | #endif |
| 43 | |
Willy Tarreau | a84dcb8 | 2015-10-28 12:04:02 +0100 | [diff] [blame] | 44 | #ifndef DEBUG_DONT_SHARE_POOLS |
Willy Tarreau | 50e608d | 2007-05-13 18:26:08 +0200 | [diff] [blame] | 45 | #define MEM_F_SHARED 0x1 |
Willy Tarreau | a84dcb8 | 2015-10-28 12:04:02 +0100 | [diff] [blame] | 46 | #else |
| 47 | #define MEM_F_SHARED 0 |
| 48 | #endif |
Willy Tarreau | 581bf81 | 2016-01-25 02:19:13 +0100 | [diff] [blame] | 49 | #define MEM_F_EXACT 0x2 |
Willy Tarreau | 50e608d | 2007-05-13 18:26:08 +0200 | [diff] [blame] | 50 | |
Willy Tarreau | ac42111 | 2015-10-28 15:09:29 +0100 | [diff] [blame] | 51 | /* reserve an extra void* at the end of a pool for linking */ |
| 52 | #ifdef DEBUG_MEMORY_POOLS |
| 53 | #define POOL_EXTRA (sizeof(void *)) |
| 54 | #define POOL_LINK(pool, item) (void **)(((char *)item) + (pool->size)) |
| 55 | #else |
| 56 | #define POOL_EXTRA (0) |
| 57 | #define POOL_LINK(pool, item) ((void **)(item)) |
| 58 | #endif |
| 59 | |
Willy Tarreau | 0a93b64 | 2018-10-16 07:58:39 +0200 | [diff] [blame] | 60 | #define MAX_BASE_POOLS 32 |
| 61 | |
Willy Tarreau | e18db9e | 2018-10-16 10:28:54 +0200 | [diff] [blame] | 62 | struct pool_cache_head { |
| 63 | struct list list; /* head of objects in this pool */ |
| 64 | size_t size; /* size of an object */ |
| 65 | unsigned int count; /* number of objects in this pool */ |
| 66 | }; |
| 67 | |
| 68 | struct pool_cache_item { |
| 69 | struct list by_pool; /* link to objects in this pool */ |
| 70 | struct list by_lru; /* link to objects by LRU order */ |
| 71 | }; |
| 72 | |
Willy Tarreau | 7f0165e | 2018-11-26 17:09:46 +0100 | [diff] [blame] | 73 | extern struct pool_cache_head pool_cache[][MAX_BASE_POOLS]; |
Willy Tarreau | e18db9e | 2018-10-16 10:28:54 +0200 | [diff] [blame] | 74 | extern THREAD_LOCAL size_t pool_cache_bytes; /* total cache size */ |
| 75 | extern THREAD_LOCAL size_t pool_cache_count; /* #cache objects */ |
| 76 | |
Willy Tarreau | f161d0f | 2018-02-22 14:05:55 +0100 | [diff] [blame] | 77 | #ifdef CONFIG_HAP_LOCKLESS_POOLS |
Olivier Houchard | cf975d4 | 2018-01-24 18:38:31 +0100 | [diff] [blame] | 78 | struct pool_free_list { |
| 79 | void **free_list; |
| 80 | uintptr_t seq; |
| 81 | }; |
| 82 | #endif |
| 83 | |
Willy Tarreau | 50e608d | 2007-05-13 18:26:08 +0200 | [diff] [blame] | 84 | struct pool_head { |
Willy Tarreau | 1ca1b70 | 2017-11-26 10:50:36 +0100 | [diff] [blame] | 85 | void **free_list; |
Willy Tarreau | f161d0f | 2018-02-22 14:05:55 +0100 | [diff] [blame] | 86 | #ifdef CONFIG_HAP_LOCKLESS_POOLS |
Olivier Houchard | cf975d4 | 2018-01-24 18:38:31 +0100 | [diff] [blame] | 87 | uintptr_t seq; |
Olivier Houchard | 899fb8a | 2020-03-18 15:48:29 +0100 | [diff] [blame] | 88 | HA_SPINLOCK_T flush_lock; |
Olivier Houchard | cf975d4 | 2018-01-24 18:38:31 +0100 | [diff] [blame] | 89 | #else |
| 90 | __decl_hathreads(HA_SPINLOCK_T lock); /* the spin lock */ |
| 91 | #endif |
Willy Tarreau | 50e608d | 2007-05-13 18:26:08 +0200 | [diff] [blame] | 92 | unsigned int used; /* how many chunks are currently in use */ |
Willy Tarreau | a1e4f8c | 2020-05-08 08:31:56 +0200 | [diff] [blame] | 93 | unsigned int needed_avg;/* floating indicator between used and allocated */ |
Willy Tarreau | 50e608d | 2007-05-13 18:26:08 +0200 | [diff] [blame] | 94 | unsigned int allocated; /* how many chunks have been allocated */ |
| 95 | unsigned int limit; /* hard limit on the number of chunks */ |
| 96 | unsigned int minavail; /* how many chunks are expected to be used */ |
| 97 | unsigned int size; /* chunk size */ |
| 98 | unsigned int flags; /* MEM_F_* */ |
Willy Tarreau | 7dcd46d | 2007-05-14 00:16:13 +0200 | [diff] [blame] | 99 | unsigned int users; /* number of pools sharing this zone */ |
Willy Tarreau | 58102cf | 2015-10-28 16:24:21 +0100 | [diff] [blame] | 100 | unsigned int failed; /* failed allocations */ |
Olivier Houchard | cf975d4 | 2018-01-24 18:38:31 +0100 | [diff] [blame] | 101 | struct list list; /* list of all known pools */ |
Willy Tarreau | 7dcd46d | 2007-05-14 00:16:13 +0200 | [diff] [blame] | 102 | char name[12]; /* name of the pool */ |
Willy Tarreau | 1ca1b70 | 2017-11-26 10:50:36 +0100 | [diff] [blame] | 103 | } __attribute__((aligned(64))); |
Willy Tarreau | 50e608d | 2007-05-13 18:26:08 +0200 | [diff] [blame] | 104 | |
Willy Tarreau | 0a93b64 | 2018-10-16 07:58:39 +0200 | [diff] [blame] | 105 | |
| 106 | extern struct pool_head pool_base_start[MAX_BASE_POOLS]; |
| 107 | extern unsigned int pool_base_count; |
| 108 | |
Willy Tarreau | 067ac9f | 2015-10-08 14:12:13 +0200 | [diff] [blame] | 109 | /* poison each newly allocated area with this byte if >= 0 */ |
| 110 | extern int mem_poison_byte; |
Willy Tarreau | 50e608d | 2007-05-13 18:26:08 +0200 | [diff] [blame] | 111 | |
Willy Tarreau | a885f6d | 2014-12-03 15:25:28 +0100 | [diff] [blame] | 112 | /* Allocates new entries for pool <pool> until there are at least <avail> + 1 |
| 113 | * available, then returns the last one for immediate use, so that at least |
| 114 | * <avail> are left available in the pool upon return. NULL is returned if the |
| 115 | * last entry could not be allocated. It's important to note that at least one |
| 116 | * allocation is always performed even if there are enough entries in the pool. |
| 117 | * A call to the garbage collector is performed at most once in case malloc() |
| 118 | * returns an error, before returning NULL. |
Willy Tarreau | 50e608d | 2007-05-13 18:26:08 +0200 | [diff] [blame] | 119 | */ |
Christopher Faulet | b349e48 | 2017-08-29 09:52:38 +0200 | [diff] [blame] | 120 | void *__pool_refill_alloc(struct pool_head *pool, unsigned int avail); |
Willy Tarreau | a885f6d | 2014-12-03 15:25:28 +0100 | [diff] [blame] | 121 | void *pool_refill_alloc(struct pool_head *pool, unsigned int avail); |
Willy Tarreau | 50e608d | 2007-05-13 18:26:08 +0200 | [diff] [blame] | 122 | |
| 123 | /* Try to find an existing shared pool with the same characteristics and |
| 124 | * returns it, otherwise creates this one. NULL is returned if no memory |
| 125 | * is available for a new creation. |
| 126 | */ |
| 127 | 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] | 128 | void create_pool_callback(struct pool_head **ptr, char *name, unsigned int size); |
| 129 | |
| 130 | /* This registers a call to create_pool_callback(ptr, name, size) */ |
| 131 | #define REGISTER_POOL(ptr, name, size) \ |
| 132 | INITCALL3(STG_POOL, create_pool_callback, (ptr), (name), (size)) |
| 133 | |
| 134 | /* This macro declares a pool head <ptr> and registers its creation */ |
| 135 | #define DECLARE_POOL(ptr, name, size) \ |
| 136 | struct pool_head *(ptr) = NULL; \ |
| 137 | REGISTER_POOL(&ptr, name, size) |
| 138 | |
| 139 | /* This macro declares a static pool head <ptr> and registers its creation */ |
| 140 | #define DECLARE_STATIC_POOL(ptr, name, size) \ |
| 141 | static struct pool_head *(ptr); \ |
| 142 | REGISTER_POOL(&ptr, name, size) |
Willy Tarreau | 50e608d | 2007-05-13 18:26:08 +0200 | [diff] [blame] | 143 | |
| 144 | /* Dump statistics on pools usage. |
| 145 | */ |
Willy Tarreau | 12833bb | 2014-01-28 16:49:56 +0100 | [diff] [blame] | 146 | void dump_pools_to_trash(); |
Willy Tarreau | 50e608d | 2007-05-13 18:26:08 +0200 | [diff] [blame] | 147 | void dump_pools(void); |
Willy Tarreau | 58102cf | 2015-10-28 16:24:21 +0100 | [diff] [blame] | 148 | int pool_total_failures(); |
| 149 | unsigned long pool_total_allocated(); |
| 150 | unsigned long pool_total_used(); |
Willy Tarreau | 50e608d | 2007-05-13 18:26:08 +0200 | [diff] [blame] | 151 | |
| 152 | /* |
Willy Tarreau | e6ce59d | 2007-05-13 19:38:49 +0200 | [diff] [blame] | 153 | * This function frees whatever can be freed in pool <pool>. |
| 154 | */ |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 155 | void pool_flush(struct pool_head *pool); |
Willy Tarreau | e6ce59d | 2007-05-13 19:38:49 +0200 | [diff] [blame] | 156 | |
| 157 | /* |
| 158 | * This function frees whatever can be freed in all pools, but respecting |
| 159 | * the minimum thresholds imposed by owners. |
Christopher Faulet | b349e48 | 2017-08-29 09:52:38 +0200 | [diff] [blame] | 160 | * |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 161 | * <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] | 162 | * an element in __pool_refill_alloc. It is important because <pool_ctx> is |
| 163 | * already locked, so we need to skip the lock here. |
Willy Tarreau | e6ce59d | 2007-05-13 19:38:49 +0200 | [diff] [blame] | 164 | */ |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 165 | void pool_gc(struct pool_head *pool_ctx); |
Willy Tarreau | e6ce59d | 2007-05-13 19:38:49 +0200 | [diff] [blame] | 166 | |
| 167 | /* |
| 168 | * This function destroys a pull by freeing it completely. |
| 169 | * This should be called only under extreme circumstances. |
| 170 | */ |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 171 | void *pool_destroy(struct pool_head *pool); |
Willy Tarreau | 2455ceb | 2018-11-26 15:57:34 +0100 | [diff] [blame] | 172 | void pool_destroy_all(); |
Willy Tarreau | e6ce59d | 2007-05-13 19:38:49 +0200 | [diff] [blame] | 173 | |
Willy Tarreau | 0a93b64 | 2018-10-16 07:58:39 +0200 | [diff] [blame] | 174 | /* returns the pool index for pool <pool>, or -1 if this pool has no index */ |
| 175 | static inline ssize_t pool_get_index(const struct pool_head *pool) |
| 176 | { |
| 177 | size_t idx; |
| 178 | |
| 179 | idx = pool - pool_base_start; |
| 180 | if (idx >= MAX_BASE_POOLS) |
| 181 | return -1; |
| 182 | return idx; |
| 183 | } |
| 184 | |
Willy Tarreau | a1e4f8c | 2020-05-08 08:31:56 +0200 | [diff] [blame] | 185 | /* The two functions below were copied from freq_ctr.h's swrate_add, impossible |
| 186 | * to use here due to include dependency hell again! |
| 187 | */ |
| 188 | #define POOL_AVG_SAMPLES 1024 |
| 189 | |
| 190 | static inline unsigned int pool_avg_add(unsigned int *sum, unsigned int v) |
| 191 | { |
| 192 | unsigned int new_sum, old_sum; |
| 193 | unsigned int n = POOL_AVG_SAMPLES; |
| 194 | |
| 195 | old_sum = *sum; |
| 196 | do { |
| 197 | new_sum = old_sum - (old_sum + n - 1) / n + v; |
| 198 | } while (!_HA_ATOMIC_CAS(sum, &old_sum, new_sum)); |
| 199 | return new_sum; |
| 200 | } |
| 201 | |
| 202 | /* make the new value <v> count for 1/4 of the total sum */ |
| 203 | static inline unsigned int pool_avg_bump(unsigned int *sum, unsigned int v) |
| 204 | { |
| 205 | unsigned int new_sum, old_sum; |
| 206 | unsigned int n = POOL_AVG_SAMPLES; |
| 207 | |
| 208 | old_sum = *sum; |
| 209 | do { |
| 210 | new_sum = old_sum - (old_sum + 3) / 4; |
| 211 | new_sum += (n * v + 3) / 4; |
| 212 | } while (!_HA_ATOMIC_CAS(sum, &old_sum, new_sum)); |
| 213 | return new_sum; |
| 214 | } |
| 215 | |
| 216 | static inline unsigned int pool_avg(unsigned int sum) |
| 217 | { |
| 218 | unsigned int n = POOL_AVG_SAMPLES; |
| 219 | |
| 220 | return (sum + n - 1) / n; |
| 221 | } |
| 222 | |
Willy Tarreau | 63a8738 | 2020-05-08 08:38:24 +0200 | [diff] [blame] | 223 | /* returns true if the pool is considered to have too many free objects */ |
| 224 | static inline int pool_is_crowded(const struct pool_head *pool) |
| 225 | { |
| 226 | return pool->allocated >= pool_avg(pool->needed_avg + pool->needed_avg / 4) && |
| 227 | (int)(pool->allocated - pool->used) >= pool->minavail; |
| 228 | } |
| 229 | |
Willy Tarreau | f161d0f | 2018-02-22 14:05:55 +0100 | [diff] [blame] | 230 | #ifdef CONFIG_HAP_LOCKLESS_POOLS |
Willy Tarreau | e18db9e | 2018-10-16 10:28:54 +0200 | [diff] [blame] | 231 | |
| 232 | /* Tries to retrieve an object from the local pool cache corresponding to pool |
| 233 | * <pool>. Returns NULL if none is available. |
| 234 | */ |
| 235 | static inline void *__pool_get_from_cache(struct pool_head *pool) |
| 236 | { |
| 237 | ssize_t idx = pool_get_index(pool); |
| 238 | struct pool_cache_item *item; |
Willy Tarreau | 7f0165e | 2018-11-26 17:09:46 +0100 | [diff] [blame] | 239 | struct pool_cache_head *ph; |
Willy Tarreau | e18db9e | 2018-10-16 10:28:54 +0200 | [diff] [blame] | 240 | |
| 241 | /* pool not in cache */ |
| 242 | if (idx < 0) |
| 243 | return NULL; |
| 244 | |
Willy Tarreau | 7f0165e | 2018-11-26 17:09:46 +0100 | [diff] [blame] | 245 | ph = &pool_cache[tid][idx]; |
| 246 | if (LIST_ISEMPTY(&ph->list)) |
| 247 | return NULL; // empty |
Willy Tarreau | e18db9e | 2018-10-16 10:28:54 +0200 | [diff] [blame] | 248 | |
Willy Tarreau | 7f0165e | 2018-11-26 17:09:46 +0100 | [diff] [blame] | 249 | item = LIST_NEXT(&ph->list, typeof(item), by_pool); |
| 250 | ph->count--; |
| 251 | pool_cache_bytes -= ph->size; |
Willy Tarreau | e18db9e | 2018-10-16 10:28:54 +0200 | [diff] [blame] | 252 | pool_cache_count--; |
| 253 | LIST_DEL(&item->by_pool); |
| 254 | LIST_DEL(&item->by_lru); |
Willy Tarreau | 8e9f453 | 2018-10-28 20:09:12 +0100 | [diff] [blame] | 255 | #ifdef DEBUG_MEMORY_POOLS |
| 256 | /* keep track of where the element was allocated from */ |
| 257 | *POOL_LINK(pool, item) = (void *)pool; |
| 258 | #endif |
Willy Tarreau | e18db9e | 2018-10-16 10:28:54 +0200 | [diff] [blame] | 259 | return item; |
| 260 | } |
| 261 | |
Olivier Houchard | cf975d4 | 2018-01-24 18:38:31 +0100 | [diff] [blame] | 262 | /* |
| 263 | * Returns a pointer to type <type> taken from the pool <pool_type> if |
| 264 | * available, otherwise returns NULL. No malloc() is attempted, and poisonning |
| 265 | * is never performed. The purpose is to get the fastest possible allocation. |
| 266 | */ |
| 267 | static inline void *__pool_get_first(struct pool_head *pool) |
| 268 | { |
| 269 | struct pool_free_list cmp, new; |
Willy Tarreau | e18db9e | 2018-10-16 10:28:54 +0200 | [diff] [blame] | 270 | void *ret = __pool_get_from_cache(pool); |
| 271 | |
| 272 | if (ret) |
| 273 | return ret; |
Olivier Houchard | cf975d4 | 2018-01-24 18:38:31 +0100 | [diff] [blame] | 274 | |
| 275 | cmp.seq = pool->seq; |
| 276 | __ha_barrier_load(); |
| 277 | |
| 278 | cmp.free_list = pool->free_list; |
| 279 | do { |
Olivier Houchard | 899fb8a | 2020-03-18 15:48:29 +0100 | [diff] [blame] | 280 | if (cmp.free_list == NULL) |
Olivier Houchard | cf975d4 | 2018-01-24 18:38:31 +0100 | [diff] [blame] | 281 | return NULL; |
| 282 | new.seq = cmp.seq + 1; |
| 283 | __ha_barrier_load(); |
| 284 | new.free_list = *POOL_LINK(pool, cmp.free_list); |
Willy Tarreau | 6a38b32 | 2019-05-11 18:04:24 +0200 | [diff] [blame] | 285 | } 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] | 286 | __ha_barrier_atomic_store(); |
Tim Duesterhus | 05f6a43 | 2018-02-20 00:49:46 +0100 | [diff] [blame] | 287 | |
Olivier Houchard | 2087276 | 2019-03-08 18:53:35 +0100 | [diff] [blame] | 288 | _HA_ATOMIC_ADD(&pool->used, 1); |
Olivier Houchard | cf975d4 | 2018-01-24 18:38:31 +0100 | [diff] [blame] | 289 | #ifdef DEBUG_MEMORY_POOLS |
| 290 | /* keep track of where the element was allocated from */ |
| 291 | *POOL_LINK(pool, cmp.free_list) = (void *)pool; |
| 292 | #endif |
| 293 | return cmp.free_list; |
| 294 | } |
| 295 | |
| 296 | static inline void *pool_get_first(struct pool_head *pool) |
| 297 | { |
| 298 | void *ret; |
| 299 | |
| 300 | ret = __pool_get_first(pool); |
| 301 | return ret; |
| 302 | } |
| 303 | /* |
| 304 | * Returns a pointer to type <type> taken from the pool <pool_type> or |
| 305 | * dynamically allocated. In the first case, <pool_type> is updated to point to |
| 306 | * the next element in the list. No memory poisonning is ever performed on the |
| 307 | * returned area. |
| 308 | */ |
| 309 | static inline void *pool_alloc_dirty(struct pool_head *pool) |
| 310 | { |
| 311 | void *p; |
| 312 | |
| 313 | if ((p = __pool_get_first(pool)) == NULL) |
| 314 | p = __pool_refill_alloc(pool, 0); |
| 315 | return p; |
| 316 | } |
| 317 | |
Willy Tarreau | e6ce59d | 2007-05-13 19:38:49 +0200 | [diff] [blame] | 318 | /* |
Olivier Houchard | cf975d4 | 2018-01-24 18:38:31 +0100 | [diff] [blame] | 319 | * Returns a pointer to type <type> taken from the pool <pool_type> or |
| 320 | * dynamically allocated. In the first case, <pool_type> is updated to point to |
| 321 | * the next element in the list. Memory poisonning is performed if enabled. |
| 322 | */ |
| 323 | static inline void *pool_alloc(struct pool_head *pool) |
| 324 | { |
| 325 | void *p; |
| 326 | |
| 327 | p = pool_alloc_dirty(pool); |
Olivier Houchard | cf975d4 | 2018-01-24 18:38:31 +0100 | [diff] [blame] | 328 | if (p && mem_poison_byte >= 0) { |
| 329 | memset(p, mem_poison_byte, pool->size); |
| 330 | } |
| 331 | |
| 332 | return p; |
| 333 | } |
| 334 | |
Willy Tarreau | 146794d | 2018-10-16 08:55:15 +0200 | [diff] [blame] | 335 | /* Locklessly add item <ptr> to pool <pool>, then update the pool used count. |
| 336 | * Both the pool and the pointer must be valid. Use pool_free() for normal |
| 337 | * operations. |
| 338 | */ |
| 339 | static inline void __pool_free(struct pool_head *pool, void *ptr) |
| 340 | { |
Willy Tarreau | 7a6ad88 | 2018-10-20 17:37:38 +0200 | [diff] [blame] | 341 | void **free_list = pool->free_list; |
Willy Tarreau | 146794d | 2018-10-16 08:55:15 +0200 | [diff] [blame] | 342 | |
Olivier Houchard | 2087276 | 2019-03-08 18:53:35 +0100 | [diff] [blame] | 343 | _HA_ATOMIC_SUB(&pool->used, 1); |
Willy Tarreau | 63a8738 | 2020-05-08 08:38:24 +0200 | [diff] [blame] | 344 | |
| 345 | if (unlikely(pool_is_crowded(pool))) { |
| 346 | free(ptr); |
| 347 | _HA_ATOMIC_SUB(&pool->allocated, 1); |
| 348 | } else { |
| 349 | do { |
| 350 | *POOL_LINK(pool, ptr) = (void *)free_list; |
| 351 | __ha_barrier_store(); |
| 352 | } while (!_HA_ATOMIC_CAS(&pool->free_list, &free_list, ptr)); |
| 353 | __ha_barrier_atomic_store(); |
| 354 | } |
Willy Tarreau | a1e4f8c | 2020-05-08 08:31:56 +0200 | [diff] [blame] | 355 | pool_avg_add(&pool->needed_avg, pool->used); |
Willy Tarreau | 146794d | 2018-10-16 08:55:15 +0200 | [diff] [blame] | 356 | } |
| 357 | |
Willy Tarreau | e18db9e | 2018-10-16 10:28:54 +0200 | [diff] [blame] | 358 | /* frees an object to the local cache, possibly pushing oldest objects to the |
| 359 | * global pool. |
| 360 | */ |
| 361 | void __pool_put_to_cache(struct pool_head *pool, void *ptr, ssize_t idx); |
| 362 | static inline void pool_put_to_cache(struct pool_head *pool, void *ptr) |
| 363 | { |
| 364 | ssize_t idx = pool_get_index(pool); |
| 365 | |
| 366 | /* pool not in cache or too many objects for this pool (more than |
| 367 | * half of the cache is used and this pool uses more than 1/8 of |
| 368 | * the cache size). |
| 369 | */ |
| 370 | if (idx < 0 || |
| 371 | (pool_cache_bytes > CONFIG_HAP_POOL_CACHE_SIZE * 3 / 4 && |
Willy Tarreau | 7f0165e | 2018-11-26 17:09:46 +0100 | [diff] [blame] | 372 | pool_cache[tid][idx].count >= 16 + pool_cache_count / 8)) { |
Willy Tarreau | e18db9e | 2018-10-16 10:28:54 +0200 | [diff] [blame] | 373 | __pool_free(pool, ptr); |
| 374 | return; |
| 375 | } |
| 376 | __pool_put_to_cache(pool, ptr, idx); |
| 377 | } |
| 378 | |
Olivier Houchard | cf975d4 | 2018-01-24 18:38:31 +0100 | [diff] [blame] | 379 | /* |
| 380 | * Puts a memory area back to the corresponding pool. |
| 381 | * Items are chained directly through a pointer that |
| 382 | * is written in the beginning of the memory area, so |
| 383 | * there's no need for any carrier cell. This implies |
| 384 | * that each memory area is at least as big as one |
| 385 | * pointer. Just like with the libc's free(), nothing |
| 386 | * is done if <ptr> is NULL. |
| 387 | */ |
| 388 | static inline void pool_free(struct pool_head *pool, void *ptr) |
| 389 | { |
| 390 | if (likely(ptr != NULL)) { |
Olivier Houchard | cf975d4 | 2018-01-24 18:38:31 +0100 | [diff] [blame] | 391 | #ifdef DEBUG_MEMORY_POOLS |
| 392 | /* we'll get late corruption if we refill to the wrong pool or double-free */ |
| 393 | if (*POOL_LINK(pool, ptr) != (void *)pool) |
Willy Tarreau | e4d4255 | 2020-03-14 11:08:16 +0100 | [diff] [blame] | 394 | *DISGUISE((volatile int *)0) = 0; |
Olivier Houchard | cf975d4 | 2018-01-24 18:38:31 +0100 | [diff] [blame] | 395 | #endif |
Willy Tarreau | da52035 | 2019-11-15 06:59:54 +0100 | [diff] [blame] | 396 | if (mem_poison_byte >= 0) |
| 397 | memset(ptr, mem_poison_byte, pool->size); |
Willy Tarreau | e18db9e | 2018-10-16 10:28:54 +0200 | [diff] [blame] | 398 | pool_put_to_cache(pool, ptr); |
Olivier Houchard | cf975d4 | 2018-01-24 18:38:31 +0100 | [diff] [blame] | 399 | } |
| 400 | } |
| 401 | |
Willy Tarreau | f161d0f | 2018-02-22 14:05:55 +0100 | [diff] [blame] | 402 | #else /* CONFIG_HAP_LOCKLESS_POOLS */ |
Olivier Houchard | cf975d4 | 2018-01-24 18:38:31 +0100 | [diff] [blame] | 403 | /* |
Willy Tarreau | 0262241 | 2014-12-08 16:35:23 +0100 | [diff] [blame] | 404 | * Returns a pointer to type <type> taken from the pool <pool_type> if |
| 405 | * available, otherwise returns NULL. No malloc() is attempted, and poisonning |
| 406 | * is never performed. The purpose is to get the fastest possible allocation. |
Willy Tarreau | 50e608d | 2007-05-13 18:26:08 +0200 | [diff] [blame] | 407 | */ |
Christopher Faulet | b349e48 | 2017-08-29 09:52:38 +0200 | [diff] [blame] | 408 | static inline void *__pool_get_first(struct pool_head *pool) |
Willy Tarreau | e430e77 | 2014-12-23 14:13:16 +0100 | [diff] [blame] | 409 | { |
| 410 | void *p; |
| 411 | |
Willy Tarreau | 0262241 | 2014-12-08 16:35:23 +0100 | [diff] [blame] | 412 | if ((p = pool->free_list) != NULL) { |
Willy Tarreau | ac42111 | 2015-10-28 15:09:29 +0100 | [diff] [blame] | 413 | pool->free_list = *POOL_LINK(pool, p); |
Willy Tarreau | e430e77 | 2014-12-23 14:13:16 +0100 | [diff] [blame] | 414 | pool->used++; |
Willy Tarreau | de30a68 | 2015-10-28 15:23:51 +0100 | [diff] [blame] | 415 | #ifdef DEBUG_MEMORY_POOLS |
| 416 | /* keep track of where the element was allocated from */ |
| 417 | *POOL_LINK(pool, p) = (void *)pool; |
| 418 | #endif |
Willy Tarreau | e430e77 | 2014-12-23 14:13:16 +0100 | [diff] [blame] | 419 | } |
| 420 | return p; |
| 421 | } |
Willy Tarreau | 50e608d | 2007-05-13 18:26:08 +0200 | [diff] [blame] | 422 | |
Christopher Faulet | b349e48 | 2017-08-29 09:52:38 +0200 | [diff] [blame] | 423 | static inline void *pool_get_first(struct pool_head *pool) |
| 424 | { |
| 425 | void *ret; |
| 426 | |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 427 | HA_SPIN_LOCK(POOL_LOCK, &pool->lock); |
Christopher Faulet | b349e48 | 2017-08-29 09:52:38 +0200 | [diff] [blame] | 428 | ret = __pool_get_first(pool); |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 429 | HA_SPIN_UNLOCK(POOL_LOCK, &pool->lock); |
Christopher Faulet | b349e48 | 2017-08-29 09:52:38 +0200 | [diff] [blame] | 430 | return ret; |
| 431 | } |
Willy Tarreau | 50e608d | 2007-05-13 18:26:08 +0200 | [diff] [blame] | 432 | /* |
Willy Tarreau | 0262241 | 2014-12-08 16:35:23 +0100 | [diff] [blame] | 433 | * Returns a pointer to type <type> taken from the pool <pool_type> or |
| 434 | * dynamically allocated. In the first case, <pool_type> is updated to point to |
| 435 | * the next element in the list. No memory poisonning is ever performed on the |
| 436 | * returned area. |
| 437 | */ |
| 438 | static inline void *pool_alloc_dirty(struct pool_head *pool) |
| 439 | { |
| 440 | void *p; |
| 441 | |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 442 | HA_SPIN_LOCK(POOL_LOCK, &pool->lock); |
Christopher Faulet | b349e48 | 2017-08-29 09:52:38 +0200 | [diff] [blame] | 443 | if ((p = __pool_get_first(pool)) == NULL) |
| 444 | p = __pool_refill_alloc(pool, 0); |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 445 | HA_SPIN_UNLOCK(POOL_LOCK, &pool->lock); |
Willy Tarreau | 0262241 | 2014-12-08 16:35:23 +0100 | [diff] [blame] | 446 | return p; |
| 447 | } |
| 448 | |
Willy Tarreau | 158fa75 | 2017-11-22 15:47:29 +0100 | [diff] [blame] | 449 | #ifndef DEBUG_UAF /* normal allocator */ |
| 450 | |
Willy Tarreau | f13322e | 2017-11-22 10:50:54 +0100 | [diff] [blame] | 451 | /* allocates an area of size <size> and returns it. The semantics are similar |
| 452 | * to those of malloc(). |
| 453 | */ |
| 454 | static inline void *pool_alloc_area(size_t size) |
| 455 | { |
| 456 | return malloc(size); |
| 457 | } |
| 458 | |
| 459 | /* frees an area <area> of size <size> allocated by pool_alloc_area(). The |
| 460 | * semantics are identical to free() except that the size is specified and |
| 461 | * may be ignored. |
| 462 | */ |
| 463 | static inline void pool_free_area(void *area, size_t __maybe_unused size) |
| 464 | { |
| 465 | free(area); |
| 466 | } |
| 467 | |
Willy Tarreau | 158fa75 | 2017-11-22 15:47:29 +0100 | [diff] [blame] | 468 | #else /* use-after-free detector */ |
| 469 | |
| 470 | /* allocates an area of size <size> and returns it. The semantics are similar |
| 471 | * to those of malloc(). However the allocation is rounded up to 4kB so that a |
| 472 | * full page is allocated. This ensures the object can be freed alone so that |
| 473 | * future dereferences are easily detected. The returned object is always |
Willy Tarreau | 364d745 | 2018-02-22 14:14:23 +0100 | [diff] [blame] | 474 | * 16-bytes aligned to avoid issues with unaligned structure objects. In case |
| 475 | * some padding is added, the area's start address is copied at the end of the |
| 476 | * padding to help detect underflows. |
Willy Tarreau | 158fa75 | 2017-11-22 15:47:29 +0100 | [diff] [blame] | 477 | */ |
Olivier Houchard | 62975a7 | 2018-10-21 01:33:11 +0200 | [diff] [blame] | 478 | #include <errno.h> |
Willy Tarreau | 158fa75 | 2017-11-22 15:47:29 +0100 | [diff] [blame] | 479 | static inline void *pool_alloc_area(size_t size) |
| 480 | { |
| 481 | size_t pad = (4096 - size) & 0xFF0; |
Willy Tarreau | 229e739 | 2019-08-08 07:38:19 +0200 | [diff] [blame] | 482 | int isolated; |
Willy Tarreau | 5a9cce4 | 2018-02-22 11:39:23 +0100 | [diff] [blame] | 483 | void *ret; |
Willy Tarreau | 158fa75 | 2017-11-22 15:47:29 +0100 | [diff] [blame] | 484 | |
Willy Tarreau | 229e739 | 2019-08-08 07:38:19 +0200 | [diff] [blame] | 485 | isolated = thread_isolated(); |
| 486 | if (!isolated) |
| 487 | thread_harmless_now(); |
Olivier Houchard | 62975a7 | 2018-10-21 01:33:11 +0200 | [diff] [blame] | 488 | 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] | 489 | if (ret != MAP_FAILED) { |
| 490 | /* let's dereference the page before returning so that the real |
| 491 | * allocation in the system is performed without holding the lock. |
| 492 | */ |
| 493 | *(int *)ret = 0; |
| 494 | if (pad >= sizeof(void *)) |
| 495 | *(void **)(ret + pad - sizeof(void *)) = ret + pad; |
| 496 | ret += pad; |
| 497 | } else { |
| 498 | ret = NULL; |
| 499 | } |
Willy Tarreau | 229e739 | 2019-08-08 07:38:19 +0200 | [diff] [blame] | 500 | if (!isolated) |
| 501 | thread_harmless_end(); |
Willy Tarreau | 85b2cae | 2019-07-04 16:18:23 +0200 | [diff] [blame] | 502 | return ret; |
Willy Tarreau | 158fa75 | 2017-11-22 15:47:29 +0100 | [diff] [blame] | 503 | } |
| 504 | |
| 505 | /* frees an area <area> of size <size> allocated by pool_alloc_area(). The |
| 506 | * semantics are identical to free() except that the size must absolutely match |
Willy Tarreau | 364d745 | 2018-02-22 14:14:23 +0100 | [diff] [blame] | 507 | * the one passed to pool_alloc_area(). In case some padding is added, the |
| 508 | * area's start address is compared to the one at the end of the padding, and |
| 509 | * a segfault is triggered if they don't match, indicating an underflow. |
Willy Tarreau | 158fa75 | 2017-11-22 15:47:29 +0100 | [diff] [blame] | 510 | */ |
| 511 | static inline void pool_free_area(void *area, size_t size) |
| 512 | { |
| 513 | size_t pad = (4096 - size) & 0xFF0; |
| 514 | |
Willy Tarreau | 364d745 | 2018-02-22 14:14:23 +0100 | [diff] [blame] | 515 | if (pad >= sizeof(void *) && *(void **)(area - sizeof(void *)) != area) |
Willy Tarreau | e4d4255 | 2020-03-14 11:08:16 +0100 | [diff] [blame] | 516 | *DISGUISE((volatile int *)0) = 0; |
Willy Tarreau | 364d745 | 2018-02-22 14:14:23 +0100 | [diff] [blame] | 517 | |
Willy Tarreau | 85b2cae | 2019-07-04 16:18:23 +0200 | [diff] [blame] | 518 | thread_harmless_now(); |
Willy Tarreau | 158fa75 | 2017-11-22 15:47:29 +0100 | [diff] [blame] | 519 | munmap(area - pad, (size + 4095) & -4096); |
Willy Tarreau | 85b2cae | 2019-07-04 16:18:23 +0200 | [diff] [blame] | 520 | thread_harmless_end(); |
Willy Tarreau | 158fa75 | 2017-11-22 15:47:29 +0100 | [diff] [blame] | 521 | } |
| 522 | |
| 523 | #endif /* DEBUG_UAF */ |
| 524 | |
Willy Tarreau | 0262241 | 2014-12-08 16:35:23 +0100 | [diff] [blame] | 525 | /* |
| 526 | * Returns a pointer to type <type> taken from the pool <pool_type> or |
| 527 | * dynamically allocated. In the first case, <pool_type> is updated to point to |
| 528 | * the next element in the list. Memory poisonning is performed if enabled. |
| 529 | */ |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 530 | static inline void *pool_alloc(struct pool_head *pool) |
Willy Tarreau | 0262241 | 2014-12-08 16:35:23 +0100 | [diff] [blame] | 531 | { |
| 532 | void *p; |
| 533 | |
| 534 | p = pool_alloc_dirty(pool); |
Willy Tarreau | de30a68 | 2015-10-28 15:23:51 +0100 | [diff] [blame] | 535 | if (p && mem_poison_byte >= 0) { |
Willy Tarreau | 0262241 | 2014-12-08 16:35:23 +0100 | [diff] [blame] | 536 | memset(p, mem_poison_byte, pool->size); |
Willy Tarreau | de30a68 | 2015-10-28 15:23:51 +0100 | [diff] [blame] | 537 | } |
| 538 | |
Willy Tarreau | 0262241 | 2014-12-08 16:35:23 +0100 | [diff] [blame] | 539 | return p; |
| 540 | } |
| 541 | |
| 542 | /* |
Willy Tarreau | 50e608d | 2007-05-13 18:26:08 +0200 | [diff] [blame] | 543 | * Puts a memory area back to the corresponding pool. |
| 544 | * Items are chained directly through a pointer that |
| 545 | * is written in the beginning of the memory area, so |
| 546 | * there's no need for any carrier cell. This implies |
| 547 | * that each memory area is at least as big as one |
Willy Tarreau | 48d63db | 2008-08-03 17:41:33 +0200 | [diff] [blame] | 548 | * pointer. Just like with the libc's free(), nothing |
| 549 | * is done if <ptr> is NULL. |
Willy Tarreau | 50e608d | 2007-05-13 18:26:08 +0200 | [diff] [blame] | 550 | */ |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 551 | static inline void pool_free(struct pool_head *pool, void *ptr) |
Willy Tarreau | e430e77 | 2014-12-23 14:13:16 +0100 | [diff] [blame] | 552 | { |
| 553 | if (likely(ptr != NULL)) { |
Willy Tarreau | de30a68 | 2015-10-28 15:23:51 +0100 | [diff] [blame] | 554 | #ifdef DEBUG_MEMORY_POOLS |
| 555 | /* we'll get late corruption if we refill to the wrong pool or double-free */ |
| 556 | if (*POOL_LINK(pool, ptr) != (void *)pool) |
Willy Tarreau | e4d4255 | 2020-03-14 11:08:16 +0100 | [diff] [blame] | 557 | *DISGUISE((volatile int *)0) = 0; |
Willy Tarreau | de30a68 | 2015-10-28 15:23:51 +0100 | [diff] [blame] | 558 | #endif |
Willy Tarreau | 158fa75 | 2017-11-22 15:47:29 +0100 | [diff] [blame] | 559 | |
| 560 | #ifndef DEBUG_UAF /* normal pool behaviour */ |
Willy Tarreau | 3e853ea | 2019-07-04 11:30:00 +0200 | [diff] [blame] | 561 | HA_SPIN_LOCK(POOL_LOCK, &pool->lock); |
Willy Tarreau | 3e853ea | 2019-07-04 11:30:00 +0200 | [diff] [blame] | 562 | pool->used--; |
Willy Tarreau | 63a8738 | 2020-05-08 08:38:24 +0200 | [diff] [blame] | 563 | if (pool_is_crowded(pool)) { |
| 564 | free(ptr); |
| 565 | pool->allocated--; |
| 566 | } else { |
| 567 | *POOL_LINK(pool, ptr) = (void *)pool->free_list; |
| 568 | pool->free_list = (void *)ptr; |
| 569 | } |
Willy Tarreau | a1e4f8c | 2020-05-08 08:31:56 +0200 | [diff] [blame] | 570 | pool_avg_add(&pool->needed_avg, pool->used); |
Willy Tarreau | 3e853ea | 2019-07-04 11:30:00 +0200 | [diff] [blame] | 571 | HA_SPIN_UNLOCK(POOL_LOCK, &pool->lock); |
Willy Tarreau | 158fa75 | 2017-11-22 15:47:29 +0100 | [diff] [blame] | 572 | #else /* release the entry for real to detect use after free */ |
| 573 | /* ensure we crash on double free or free of a const area*/ |
| 574 | *(uint32_t *)ptr = 0xDEADADD4; |
| 575 | pool_free_area(ptr, pool->size + POOL_EXTRA); |
Willy Tarreau | 3e853ea | 2019-07-04 11:30:00 +0200 | [diff] [blame] | 576 | HA_SPIN_LOCK(POOL_LOCK, &pool->lock); |
Willy Tarreau | 158fa75 | 2017-11-22 15:47:29 +0100 | [diff] [blame] | 577 | pool->allocated--; |
Willy Tarreau | 3e853ea | 2019-07-04 11:30:00 +0200 | [diff] [blame] | 578 | pool->used--; |
Willy Tarreau | a1e4f8c | 2020-05-08 08:31:56 +0200 | [diff] [blame] | 579 | pool_avg_add(&pool->needed_avg, pool->used); |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 580 | HA_SPIN_UNLOCK(POOL_LOCK, &pool->lock); |
Willy Tarreau | 3e853ea | 2019-07-04 11:30:00 +0200 | [diff] [blame] | 581 | #endif /* DEBUG_UAF */ |
Willy Tarreau | e430e77 | 2014-12-23 14:13:16 +0100 | [diff] [blame] | 582 | } |
| 583 | } |
Willy Tarreau | f161d0f | 2018-02-22 14:05:55 +0100 | [diff] [blame] | 584 | #endif /* CONFIG_HAP_LOCKLESS_POOLS */ |
Willy Tarreau | 2dd0d47 | 2006-06-29 17:53:05 +0200 | [diff] [blame] | 585 | #endif /* _COMMON_MEMORY_H */ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 586 | |
| 587 | /* |
| 588 | * Local variables: |
| 589 | * c-indent-level: 8 |
| 590 | * c-basic-offset: 8 |
| 591 | * End: |
| 592 | */ |