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