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