blob: 7bcef94ea7a29822b259ec00872f50d240c14978 [file] [log] [blame]
Willy Tarreaubaaee002006-06-26 02:48:02 +02001/*
Willy Tarreau62405a22014-12-23 13:51:28 +01002 * 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 Tarreaubaaee002006-06-26 02:48:02 +020021
Willy Tarreau2dd0d472006-06-29 17:53:05 +020022#ifndef _COMMON_MEMORY_H
23#define _COMMON_MEMORY_H
Willy Tarreaubaaee002006-06-26 02:48:02 +020024
Willy Tarreau158fa752017-11-22 15:47:29 +010025#include <sys/mman.h>
26
Willy Tarreaubaaee002006-06-26 02:48:02 +020027#include <stdlib.h>
Willy Tarreaue430e772014-12-23 14:13:16 +010028#include <string.h>
Willy Tarreaua1bd1fa2019-03-29 17:26:33 +010029#include <inttypes.h>
Willy Tarreaua7280a12018-11-26 19:41:40 +010030#include <unistd.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020031
Willy Tarreau2dd0d472006-06-29 17:53:05 +020032#include <common/config.h>
Willy Tarreau50e608d2007-05-13 18:26:08 +020033#include <common/mini-clist.h>
Christopher Fauletb349e482017-08-29 09:52:38 +020034#include <common/hathreads.h>
Willy Tarreau7107c8b2018-11-26 11:44:35 +010035#include <common/initcall.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020036
Willy Tarreau3bc4e8b2020-05-09 09:02:35 +020037/* 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 Tarreaua84dcb82015-10-28 12:04:02 +010044#ifndef DEBUG_DONT_SHARE_POOLS
Willy Tarreau50e608d2007-05-13 18:26:08 +020045#define MEM_F_SHARED 0x1
Willy Tarreaua84dcb82015-10-28 12:04:02 +010046#else
47#define MEM_F_SHARED 0
48#endif
Willy Tarreau581bf812016-01-25 02:19:13 +010049#define MEM_F_EXACT 0x2
Willy Tarreau50e608d2007-05-13 18:26:08 +020050
Willy Tarreauac421112015-10-28 15:09:29 +010051/* 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 Tarreau0a93b642018-10-16 07:58:39 +020060#define MAX_BASE_POOLS 32
61
Willy Tarreaue18db9e2018-10-16 10:28:54 +020062struct 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
68struct 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 Tarreau7f0165e2018-11-26 17:09:46 +010073extern struct pool_cache_head pool_cache[][MAX_BASE_POOLS];
Willy Tarreaue18db9e2018-10-16 10:28:54 +020074extern THREAD_LOCAL size_t pool_cache_bytes; /* total cache size */
75extern THREAD_LOCAL size_t pool_cache_count; /* #cache objects */
76
Willy Tarreauf161d0f2018-02-22 14:05:55 +010077#ifdef CONFIG_HAP_LOCKLESS_POOLS
Olivier Houchardcf975d42018-01-24 18:38:31 +010078struct pool_free_list {
79 void **free_list;
80 uintptr_t seq;
81};
82#endif
83
Willy Tarreau50e608d2007-05-13 18:26:08 +020084struct pool_head {
Willy Tarreau1ca1b702017-11-26 10:50:36 +010085 void **free_list;
Willy Tarreauf161d0f2018-02-22 14:05:55 +010086#ifdef CONFIG_HAP_LOCKLESS_POOLS
Olivier Houchardcf975d42018-01-24 18:38:31 +010087 uintptr_t seq;
Olivier Houchard899fb8a2020-03-18 15:48:29 +010088 HA_SPINLOCK_T flush_lock;
Olivier Houchardcf975d42018-01-24 18:38:31 +010089#else
90 __decl_hathreads(HA_SPINLOCK_T lock); /* the spin lock */
91#endif
Willy Tarreau50e608d2007-05-13 18:26:08 +020092 unsigned int used; /* how many chunks are currently in use */
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 Tarreau7dcd46d2007-05-14 00:16:13 +020098 unsigned int users; /* number of pools sharing this zone */
Willy Tarreau58102cf2015-10-28 16:24:21 +010099 unsigned int failed; /* failed allocations */
Olivier Houchardcf975d42018-01-24 18:38:31 +0100100 struct list list; /* list of all known pools */
Willy Tarreau7dcd46d2007-05-14 00:16:13 +0200101 char name[12]; /* name of the pool */
Willy Tarreau1ca1b702017-11-26 10:50:36 +0100102} __attribute__((aligned(64)));
Willy Tarreau50e608d2007-05-13 18:26:08 +0200103
Willy Tarreau0a93b642018-10-16 07:58:39 +0200104
105extern struct pool_head pool_base_start[MAX_BASE_POOLS];
106extern unsigned int pool_base_count;
107
Willy Tarreau067ac9f2015-10-08 14:12:13 +0200108/* poison each newly allocated area with this byte if >= 0 */
109extern int mem_poison_byte;
Willy Tarreau50e608d2007-05-13 18:26:08 +0200110
Willy Tarreaua885f6d2014-12-03 15:25:28 +0100111/* 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 Tarreau50e608d2007-05-13 18:26:08 +0200118 */
Christopher Fauletb349e482017-08-29 09:52:38 +0200119void *__pool_refill_alloc(struct pool_head *pool, unsigned int avail);
Willy Tarreaua885f6d2014-12-03 15:25:28 +0100120void *pool_refill_alloc(struct pool_head *pool, unsigned int avail);
Willy Tarreau50e608d2007-05-13 18:26:08 +0200121
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 */
126struct pool_head *create_pool(char *name, unsigned int size, unsigned int flags);
Willy Tarreau7107c8b2018-11-26 11:44:35 +0100127void 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 Tarreau50e608d2007-05-13 18:26:08 +0200142
143/* Dump statistics on pools usage.
144 */
Willy Tarreau12833bb2014-01-28 16:49:56 +0100145void dump_pools_to_trash();
Willy Tarreau50e608d2007-05-13 18:26:08 +0200146void dump_pools(void);
Willy Tarreau58102cf2015-10-28 16:24:21 +0100147int pool_total_failures();
148unsigned long pool_total_allocated();
149unsigned long pool_total_used();
Willy Tarreau50e608d2007-05-13 18:26:08 +0200150
151/*
Willy Tarreaue6ce59d2007-05-13 19:38:49 +0200152 * This function frees whatever can be freed in pool <pool>.
153 */
Willy Tarreaubafbe012017-11-24 17:34:44 +0100154void pool_flush(struct pool_head *pool);
Willy Tarreaue6ce59d2007-05-13 19:38:49 +0200155
156/*
157 * This function frees whatever can be freed in all pools, but respecting
158 * the minimum thresholds imposed by owners.
Christopher Fauletb349e482017-08-29 09:52:38 +0200159 *
Willy Tarreaubafbe012017-11-24 17:34:44 +0100160 * <pool_ctx> is used when pool_gc is called to release resources to allocate
Christopher Fauletb349e482017-08-29 09:52:38 +0200161 * 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 Tarreaue6ce59d2007-05-13 19:38:49 +0200163 */
Willy Tarreaubafbe012017-11-24 17:34:44 +0100164void pool_gc(struct pool_head *pool_ctx);
Willy Tarreaue6ce59d2007-05-13 19:38:49 +0200165
166/*
167 * This function destroys a pull by freeing it completely.
168 * This should be called only under extreme circumstances.
169 */
Willy Tarreaubafbe012017-11-24 17:34:44 +0100170void *pool_destroy(struct pool_head *pool);
Willy Tarreau2455ceb2018-11-26 15:57:34 +0100171void pool_destroy_all();
Willy Tarreaue6ce59d2007-05-13 19:38:49 +0200172
Willy Tarreau0a93b642018-10-16 07:58:39 +0200173/* returns the pool index for pool <pool>, or -1 if this pool has no index */
174static 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 Tarreauf161d0f2018-02-22 14:05:55 +0100184#ifdef CONFIG_HAP_LOCKLESS_POOLS
Willy Tarreaue18db9e2018-10-16 10:28:54 +0200185
186/* Tries to retrieve an object from the local pool cache corresponding to pool
187 * <pool>. Returns NULL if none is available.
188 */
189static inline void *__pool_get_from_cache(struct pool_head *pool)
190{
191 ssize_t idx = pool_get_index(pool);
192 struct pool_cache_item *item;
Willy Tarreau7f0165e2018-11-26 17:09:46 +0100193 struct pool_cache_head *ph;
Willy Tarreaue18db9e2018-10-16 10:28:54 +0200194
195 /* pool not in cache */
196 if (idx < 0)
197 return NULL;
198
Willy Tarreau7f0165e2018-11-26 17:09:46 +0100199 ph = &pool_cache[tid][idx];
200 if (LIST_ISEMPTY(&ph->list))
201 return NULL; // empty
Willy Tarreaue18db9e2018-10-16 10:28:54 +0200202
Willy Tarreau7f0165e2018-11-26 17:09:46 +0100203 item = LIST_NEXT(&ph->list, typeof(item), by_pool);
204 ph->count--;
205 pool_cache_bytes -= ph->size;
Willy Tarreaue18db9e2018-10-16 10:28:54 +0200206 pool_cache_count--;
207 LIST_DEL(&item->by_pool);
208 LIST_DEL(&item->by_lru);
Willy Tarreau8e9f4532018-10-28 20:09:12 +0100209#ifdef DEBUG_MEMORY_POOLS
210 /* keep track of where the element was allocated from */
211 *POOL_LINK(pool, item) = (void *)pool;
212#endif
Willy Tarreaue18db9e2018-10-16 10:28:54 +0200213 return item;
214}
215
Olivier Houchardcf975d42018-01-24 18:38:31 +0100216/*
217 * Returns a pointer to type <type> taken from the pool <pool_type> if
218 * available, otherwise returns NULL. No malloc() is attempted, and poisonning
219 * is never performed. The purpose is to get the fastest possible allocation.
220 */
221static inline void *__pool_get_first(struct pool_head *pool)
222{
223 struct pool_free_list cmp, new;
Willy Tarreaue18db9e2018-10-16 10:28:54 +0200224 void *ret = __pool_get_from_cache(pool);
225
226 if (ret)
227 return ret;
Olivier Houchardcf975d42018-01-24 18:38:31 +0100228
229 cmp.seq = pool->seq;
230 __ha_barrier_load();
231
232 cmp.free_list = pool->free_list;
233 do {
Olivier Houchard899fb8a2020-03-18 15:48:29 +0100234 if (cmp.free_list == NULL)
Olivier Houchardcf975d42018-01-24 18:38:31 +0100235 return NULL;
236 new.seq = cmp.seq + 1;
237 __ha_barrier_load();
238 new.free_list = *POOL_LINK(pool, cmp.free_list);
Willy Tarreau6a38b322019-05-11 18:04:24 +0200239 } while (HA_ATOMIC_DWCAS((void *)&pool->free_list, (void *)&cmp, (void *)&new) == 0);
Olivier Houchard20872762019-03-08 18:53:35 +0100240 __ha_barrier_atomic_store();
Tim Duesterhus05f6a432018-02-20 00:49:46 +0100241
Olivier Houchard20872762019-03-08 18:53:35 +0100242 _HA_ATOMIC_ADD(&pool->used, 1);
Olivier Houchardcf975d42018-01-24 18:38:31 +0100243#ifdef DEBUG_MEMORY_POOLS
244 /* keep track of where the element was allocated from */
245 *POOL_LINK(pool, cmp.free_list) = (void *)pool;
246#endif
247 return cmp.free_list;
248}
249
250static inline void *pool_get_first(struct pool_head *pool)
251{
252 void *ret;
253
254 ret = __pool_get_first(pool);
255 return ret;
256}
257/*
258 * Returns a pointer to type <type> taken from the pool <pool_type> or
259 * dynamically allocated. In the first case, <pool_type> is updated to point to
260 * the next element in the list. No memory poisonning is ever performed on the
261 * returned area.
262 */
263static inline void *pool_alloc_dirty(struct pool_head *pool)
264{
265 void *p;
266
267 if ((p = __pool_get_first(pool)) == NULL)
268 p = __pool_refill_alloc(pool, 0);
269 return p;
270}
271
Willy Tarreaue6ce59d2007-05-13 19:38:49 +0200272/*
Olivier Houchardcf975d42018-01-24 18:38:31 +0100273 * Returns a pointer to type <type> taken from the pool <pool_type> or
274 * dynamically allocated. In the first case, <pool_type> is updated to point to
275 * the next element in the list. Memory poisonning is performed if enabled.
276 */
277static inline void *pool_alloc(struct pool_head *pool)
278{
279 void *p;
280
281 p = pool_alloc_dirty(pool);
Olivier Houchardcf975d42018-01-24 18:38:31 +0100282 if (p && mem_poison_byte >= 0) {
283 memset(p, mem_poison_byte, pool->size);
284 }
285
286 return p;
287}
288
Willy Tarreau146794d2018-10-16 08:55:15 +0200289/* Locklessly add item <ptr> to pool <pool>, then update the pool used count.
290 * Both the pool and the pointer must be valid. Use pool_free() for normal
291 * operations.
292 */
293static inline void __pool_free(struct pool_head *pool, void *ptr)
294{
Willy Tarreau7a6ad882018-10-20 17:37:38 +0200295 void **free_list = pool->free_list;
Willy Tarreau146794d2018-10-16 08:55:15 +0200296
297 do {
298 *POOL_LINK(pool, ptr) = (void *)free_list;
299 __ha_barrier_store();
Olivier Houchard20872762019-03-08 18:53:35 +0100300 } while (!_HA_ATOMIC_CAS(&pool->free_list, &free_list, ptr));
301 __ha_barrier_atomic_store();
302 _HA_ATOMIC_SUB(&pool->used, 1);
Willy Tarreau146794d2018-10-16 08:55:15 +0200303}
304
Willy Tarreaue18db9e2018-10-16 10:28:54 +0200305/* frees an object to the local cache, possibly pushing oldest objects to the
306 * global pool.
307 */
308void __pool_put_to_cache(struct pool_head *pool, void *ptr, ssize_t idx);
309static inline void pool_put_to_cache(struct pool_head *pool, void *ptr)
310{
311 ssize_t idx = pool_get_index(pool);
312
313 /* pool not in cache or too many objects for this pool (more than
314 * half of the cache is used and this pool uses more than 1/8 of
315 * the cache size).
316 */
317 if (idx < 0 ||
318 (pool_cache_bytes > CONFIG_HAP_POOL_CACHE_SIZE * 3 / 4 &&
Willy Tarreau7f0165e2018-11-26 17:09:46 +0100319 pool_cache[tid][idx].count >= 16 + pool_cache_count / 8)) {
Willy Tarreaue18db9e2018-10-16 10:28:54 +0200320 __pool_free(pool, ptr);
321 return;
322 }
323 __pool_put_to_cache(pool, ptr, idx);
324}
325
Olivier Houchardcf975d42018-01-24 18:38:31 +0100326/*
327 * Puts a memory area back to the corresponding pool.
328 * Items are chained directly through a pointer that
329 * is written in the beginning of the memory area, so
330 * there's no need for any carrier cell. This implies
331 * that each memory area is at least as big as one
332 * pointer. Just like with the libc's free(), nothing
333 * is done if <ptr> is NULL.
334 */
335static inline void pool_free(struct pool_head *pool, void *ptr)
336{
337 if (likely(ptr != NULL)) {
Olivier Houchardcf975d42018-01-24 18:38:31 +0100338#ifdef DEBUG_MEMORY_POOLS
339 /* we'll get late corruption if we refill to the wrong pool or double-free */
340 if (*POOL_LINK(pool, ptr) != (void *)pool)
Willy Tarreaue4d42552020-03-14 11:08:16 +0100341 *DISGUISE((volatile int *)0) = 0;
Olivier Houchardcf975d42018-01-24 18:38:31 +0100342#endif
Willy Tarreauda520352019-11-15 06:59:54 +0100343 if (mem_poison_byte >= 0)
344 memset(ptr, mem_poison_byte, pool->size);
Willy Tarreaue18db9e2018-10-16 10:28:54 +0200345 pool_put_to_cache(pool, ptr);
Olivier Houchardcf975d42018-01-24 18:38:31 +0100346 }
347}
348
Willy Tarreauf161d0f2018-02-22 14:05:55 +0100349#else /* CONFIG_HAP_LOCKLESS_POOLS */
Olivier Houchardcf975d42018-01-24 18:38:31 +0100350/*
Willy Tarreau02622412014-12-08 16:35:23 +0100351 * Returns a pointer to type <type> taken from the pool <pool_type> if
352 * available, otherwise returns NULL. No malloc() is attempted, and poisonning
353 * is never performed. The purpose is to get the fastest possible allocation.
Willy Tarreau50e608d2007-05-13 18:26:08 +0200354 */
Christopher Fauletb349e482017-08-29 09:52:38 +0200355static inline void *__pool_get_first(struct pool_head *pool)
Willy Tarreaue430e772014-12-23 14:13:16 +0100356{
357 void *p;
358
Willy Tarreau02622412014-12-08 16:35:23 +0100359 if ((p = pool->free_list) != NULL) {
Willy Tarreauac421112015-10-28 15:09:29 +0100360 pool->free_list = *POOL_LINK(pool, p);
Willy Tarreaue430e772014-12-23 14:13:16 +0100361 pool->used++;
Willy Tarreaude30a682015-10-28 15:23:51 +0100362#ifdef DEBUG_MEMORY_POOLS
363 /* keep track of where the element was allocated from */
364 *POOL_LINK(pool, p) = (void *)pool;
365#endif
Willy Tarreaue430e772014-12-23 14:13:16 +0100366 }
367 return p;
368}
Willy Tarreau50e608d2007-05-13 18:26:08 +0200369
Christopher Fauletb349e482017-08-29 09:52:38 +0200370static inline void *pool_get_first(struct pool_head *pool)
371{
372 void *ret;
373
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100374 HA_SPIN_LOCK(POOL_LOCK, &pool->lock);
Christopher Fauletb349e482017-08-29 09:52:38 +0200375 ret = __pool_get_first(pool);
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100376 HA_SPIN_UNLOCK(POOL_LOCK, &pool->lock);
Christopher Fauletb349e482017-08-29 09:52:38 +0200377 return ret;
378}
Willy Tarreau50e608d2007-05-13 18:26:08 +0200379/*
Willy Tarreau02622412014-12-08 16:35:23 +0100380 * Returns a pointer to type <type> taken from the pool <pool_type> or
381 * dynamically allocated. In the first case, <pool_type> is updated to point to
382 * the next element in the list. No memory poisonning is ever performed on the
383 * returned area.
384 */
385static inline void *pool_alloc_dirty(struct pool_head *pool)
386{
387 void *p;
388
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100389 HA_SPIN_LOCK(POOL_LOCK, &pool->lock);
Christopher Fauletb349e482017-08-29 09:52:38 +0200390 if ((p = __pool_get_first(pool)) == NULL)
391 p = __pool_refill_alloc(pool, 0);
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100392 HA_SPIN_UNLOCK(POOL_LOCK, &pool->lock);
Willy Tarreau02622412014-12-08 16:35:23 +0100393 return p;
394}
395
Willy Tarreau158fa752017-11-22 15:47:29 +0100396#ifndef DEBUG_UAF /* normal allocator */
397
Willy Tarreauf13322e2017-11-22 10:50:54 +0100398/* allocates an area of size <size> and returns it. The semantics are similar
399 * to those of malloc().
400 */
401static inline void *pool_alloc_area(size_t size)
402{
403 return malloc(size);
404}
405
406/* frees an area <area> of size <size> allocated by pool_alloc_area(). The
407 * semantics are identical to free() except that the size is specified and
408 * may be ignored.
409 */
410static inline void pool_free_area(void *area, size_t __maybe_unused size)
411{
412 free(area);
413}
414
Willy Tarreau158fa752017-11-22 15:47:29 +0100415#else /* use-after-free detector */
416
417/* allocates an area of size <size> and returns it. The semantics are similar
418 * to those of malloc(). However the allocation is rounded up to 4kB so that a
419 * full page is allocated. This ensures the object can be freed alone so that
420 * future dereferences are easily detected. The returned object is always
Willy Tarreau364d7452018-02-22 14:14:23 +0100421 * 16-bytes aligned to avoid issues with unaligned structure objects. In case
422 * some padding is added, the area's start address is copied at the end of the
423 * padding to help detect underflows.
Willy Tarreau158fa752017-11-22 15:47:29 +0100424 */
Olivier Houchard62975a72018-10-21 01:33:11 +0200425#include <errno.h>
Willy Tarreau158fa752017-11-22 15:47:29 +0100426static inline void *pool_alloc_area(size_t size)
427{
428 size_t pad = (4096 - size) & 0xFF0;
Willy Tarreau229e7392019-08-08 07:38:19 +0200429 int isolated;
Willy Tarreau5a9cce42018-02-22 11:39:23 +0100430 void *ret;
Willy Tarreau158fa752017-11-22 15:47:29 +0100431
Willy Tarreau229e7392019-08-08 07:38:19 +0200432 isolated = thread_isolated();
433 if (!isolated)
434 thread_harmless_now();
Olivier Houchard62975a72018-10-21 01:33:11 +0200435 ret = mmap(NULL, (size + 4095) & -4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
Willy Tarreau85b2cae2019-07-04 16:18:23 +0200436 if (ret != MAP_FAILED) {
437 /* let's dereference the page before returning so that the real
438 * allocation in the system is performed without holding the lock.
439 */
440 *(int *)ret = 0;
441 if (pad >= sizeof(void *))
442 *(void **)(ret + pad - sizeof(void *)) = ret + pad;
443 ret += pad;
444 } else {
445 ret = NULL;
446 }
Willy Tarreau229e7392019-08-08 07:38:19 +0200447 if (!isolated)
448 thread_harmless_end();
Willy Tarreau85b2cae2019-07-04 16:18:23 +0200449 return ret;
Willy Tarreau158fa752017-11-22 15:47:29 +0100450}
451
452/* frees an area <area> of size <size> allocated by pool_alloc_area(). The
453 * semantics are identical to free() except that the size must absolutely match
Willy Tarreau364d7452018-02-22 14:14:23 +0100454 * the one passed to pool_alloc_area(). In case some padding is added, the
455 * area's start address is compared to the one at the end of the padding, and
456 * a segfault is triggered if they don't match, indicating an underflow.
Willy Tarreau158fa752017-11-22 15:47:29 +0100457 */
458static inline void pool_free_area(void *area, size_t size)
459{
460 size_t pad = (4096 - size) & 0xFF0;
461
Willy Tarreau364d7452018-02-22 14:14:23 +0100462 if (pad >= sizeof(void *) && *(void **)(area - sizeof(void *)) != area)
Willy Tarreaue4d42552020-03-14 11:08:16 +0100463 *DISGUISE((volatile int *)0) = 0;
Willy Tarreau364d7452018-02-22 14:14:23 +0100464
Willy Tarreau85b2cae2019-07-04 16:18:23 +0200465 thread_harmless_now();
Willy Tarreau158fa752017-11-22 15:47:29 +0100466 munmap(area - pad, (size + 4095) & -4096);
Willy Tarreau85b2cae2019-07-04 16:18:23 +0200467 thread_harmless_end();
Willy Tarreau158fa752017-11-22 15:47:29 +0100468}
469
470#endif /* DEBUG_UAF */
471
Willy Tarreau02622412014-12-08 16:35:23 +0100472/*
473 * Returns a pointer to type <type> taken from the pool <pool_type> or
474 * dynamically allocated. In the first case, <pool_type> is updated to point to
475 * the next element in the list. Memory poisonning is performed if enabled.
476 */
Willy Tarreaubafbe012017-11-24 17:34:44 +0100477static inline void *pool_alloc(struct pool_head *pool)
Willy Tarreau02622412014-12-08 16:35:23 +0100478{
479 void *p;
480
481 p = pool_alloc_dirty(pool);
Willy Tarreaude30a682015-10-28 15:23:51 +0100482 if (p && mem_poison_byte >= 0) {
Willy Tarreau02622412014-12-08 16:35:23 +0100483 memset(p, mem_poison_byte, pool->size);
Willy Tarreaude30a682015-10-28 15:23:51 +0100484 }
485
Willy Tarreau02622412014-12-08 16:35:23 +0100486 return p;
487}
488
489/*
Willy Tarreau50e608d2007-05-13 18:26:08 +0200490 * Puts a memory area back to the corresponding pool.
491 * Items are chained directly through a pointer that
492 * is written in the beginning of the memory area, so
493 * there's no need for any carrier cell. This implies
494 * that each memory area is at least as big as one
Willy Tarreau48d63db2008-08-03 17:41:33 +0200495 * pointer. Just like with the libc's free(), nothing
496 * is done if <ptr> is NULL.
Willy Tarreau50e608d2007-05-13 18:26:08 +0200497 */
Willy Tarreaubafbe012017-11-24 17:34:44 +0100498static inline void pool_free(struct pool_head *pool, void *ptr)
Willy Tarreaue430e772014-12-23 14:13:16 +0100499{
500 if (likely(ptr != NULL)) {
Willy Tarreaude30a682015-10-28 15:23:51 +0100501#ifdef DEBUG_MEMORY_POOLS
502 /* we'll get late corruption if we refill to the wrong pool or double-free */
503 if (*POOL_LINK(pool, ptr) != (void *)pool)
Willy Tarreaue4d42552020-03-14 11:08:16 +0100504 *DISGUISE((volatile int *)0) = 0;
Willy Tarreaude30a682015-10-28 15:23:51 +0100505#endif
Willy Tarreau158fa752017-11-22 15:47:29 +0100506
507#ifndef DEBUG_UAF /* normal pool behaviour */
Willy Tarreau3e853ea2019-07-04 11:30:00 +0200508 HA_SPIN_LOCK(POOL_LOCK, &pool->lock);
Willy Tarreauac421112015-10-28 15:09:29 +0100509 *POOL_LINK(pool, ptr) = (void *)pool->free_list;
Willy Tarreau3e853ea2019-07-04 11:30:00 +0200510 pool->free_list = (void *)ptr;
511 pool->used--;
512 HA_SPIN_UNLOCK(POOL_LOCK, &pool->lock);
Willy Tarreau158fa752017-11-22 15:47:29 +0100513#else /* release the entry for real to detect use after free */
514 /* ensure we crash on double free or free of a const area*/
515 *(uint32_t *)ptr = 0xDEADADD4;
516 pool_free_area(ptr, pool->size + POOL_EXTRA);
Willy Tarreau3e853ea2019-07-04 11:30:00 +0200517 HA_SPIN_LOCK(POOL_LOCK, &pool->lock);
Willy Tarreau158fa752017-11-22 15:47:29 +0100518 pool->allocated--;
Willy Tarreau3e853ea2019-07-04 11:30:00 +0200519 pool->used--;
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100520 HA_SPIN_UNLOCK(POOL_LOCK, &pool->lock);
Willy Tarreau3e853ea2019-07-04 11:30:00 +0200521#endif /* DEBUG_UAF */
Willy Tarreaue430e772014-12-23 14:13:16 +0100522 }
523}
Willy Tarreauf161d0f2018-02-22 14:05:55 +0100524#endif /* CONFIG_HAP_LOCKLESS_POOLS */
Willy Tarreau2dd0d472006-06-29 17:53:05 +0200525#endif /* _COMMON_MEMORY_H */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200526
527/*
528 * Local variables:
529 * c-indent-level: 8
530 * c-basic-offset: 8
531 * End:
532 */