blob: 98f9db26f4a033b7aab3f23870b86575b20d63c3 [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 Tarreaua84dcb82015-10-28 12:04:02 +010037#ifndef DEBUG_DONT_SHARE_POOLS
Willy Tarreau50e608d2007-05-13 18:26:08 +020038#define MEM_F_SHARED 0x1
Willy Tarreaua84dcb82015-10-28 12:04:02 +010039#else
40#define MEM_F_SHARED 0
41#endif
Willy Tarreau581bf812016-01-25 02:19:13 +010042#define MEM_F_EXACT 0x2
Willy Tarreau50e608d2007-05-13 18:26:08 +020043
Willy Tarreauac421112015-10-28 15:09:29 +010044/* reserve an extra void* at the end of a pool for linking */
45#ifdef DEBUG_MEMORY_POOLS
46#define POOL_EXTRA (sizeof(void *))
47#define POOL_LINK(pool, item) (void **)(((char *)item) + (pool->size))
48#else
49#define POOL_EXTRA (0)
50#define POOL_LINK(pool, item) ((void **)(item))
51#endif
52
Willy Tarreaud3a82112020-06-30 14:29:02 +020053#ifndef MAX_BASE_POOLS
54#define MAX_BASE_POOLS 64
55#endif
Willy Tarreau0a93b642018-10-16 07:58:39 +020056
Willy Tarreaue18db9e2018-10-16 10:28:54 +020057struct pool_cache_head {
58 struct list list; /* head of objects in this pool */
59 size_t size; /* size of an object */
60 unsigned int count; /* number of objects in this pool */
61};
62
63struct pool_cache_item {
64 struct list by_pool; /* link to objects in this pool */
65 struct list by_lru; /* link to objects by LRU order */
66};
67
Willy Tarreau7f0165e2018-11-26 17:09:46 +010068extern struct pool_cache_head pool_cache[][MAX_BASE_POOLS];
Willy Tarreaue18db9e2018-10-16 10:28:54 +020069extern THREAD_LOCAL size_t pool_cache_bytes; /* total cache size */
70extern THREAD_LOCAL size_t pool_cache_count; /* #cache objects */
71
Willy Tarreauf161d0f2018-02-22 14:05:55 +010072#ifdef CONFIG_HAP_LOCKLESS_POOLS
Olivier Houchardcf975d42018-01-24 18:38:31 +010073struct pool_free_list {
74 void **free_list;
75 uintptr_t seq;
76};
77#endif
78
Willy Tarreau50e608d2007-05-13 18:26:08 +020079struct pool_head {
Willy Tarreau1ca1b702017-11-26 10:50:36 +010080 void **free_list;
Willy Tarreauf161d0f2018-02-22 14:05:55 +010081#ifdef CONFIG_HAP_LOCKLESS_POOLS
Olivier Houchardcf975d42018-01-24 18:38:31 +010082 uintptr_t seq;
Olivier Houchard5dfbf2e2020-03-18 15:48:29 +010083 HA_SPINLOCK_T flush_lock;
Olivier Houchardcf975d42018-01-24 18:38:31 +010084#else
85 __decl_hathreads(HA_SPINLOCK_T lock); /* the spin lock */
86#endif
Willy Tarreau50e608d2007-05-13 18:26:08 +020087 unsigned int used; /* how many chunks are currently in use */
88 unsigned int allocated; /* how many chunks have been allocated */
89 unsigned int limit; /* hard limit on the number of chunks */
90 unsigned int minavail; /* how many chunks are expected to be used */
91 unsigned int size; /* chunk size */
92 unsigned int flags; /* MEM_F_* */
Willy Tarreau7dcd46d2007-05-14 00:16:13 +020093 unsigned int users; /* number of pools sharing this zone */
Willy Tarreau58102cf2015-10-28 16:24:21 +010094 unsigned int failed; /* failed allocations */
Olivier Houchardcf975d42018-01-24 18:38:31 +010095 struct list list; /* list of all known pools */
Willy Tarreau7dcd46d2007-05-14 00:16:13 +020096 char name[12]; /* name of the pool */
Willy Tarreau1ca1b702017-11-26 10:50:36 +010097} __attribute__((aligned(64)));
Willy Tarreau50e608d2007-05-13 18:26:08 +020098
Willy Tarreau0a93b642018-10-16 07:58:39 +020099
100extern struct pool_head pool_base_start[MAX_BASE_POOLS];
101extern unsigned int pool_base_count;
102
Willy Tarreau067ac9f2015-10-08 14:12:13 +0200103/* poison each newly allocated area with this byte if >= 0 */
104extern int mem_poison_byte;
Willy Tarreau50e608d2007-05-13 18:26:08 +0200105
Willy Tarreaua885f6d2014-12-03 15:25:28 +0100106/* Allocates new entries for pool <pool> until there are at least <avail> + 1
107 * available, then returns the last one for immediate use, so that at least
108 * <avail> are left available in the pool upon return. NULL is returned if the
109 * last entry could not be allocated. It's important to note that at least one
110 * allocation is always performed even if there are enough entries in the pool.
111 * A call to the garbage collector is performed at most once in case malloc()
112 * returns an error, before returning NULL.
Willy Tarreau50e608d2007-05-13 18:26:08 +0200113 */
Christopher Fauletb349e482017-08-29 09:52:38 +0200114void *__pool_refill_alloc(struct pool_head *pool, unsigned int avail);
Willy Tarreaua885f6d2014-12-03 15:25:28 +0100115void *pool_refill_alloc(struct pool_head *pool, unsigned int avail);
Willy Tarreau50e608d2007-05-13 18:26:08 +0200116
117/* Try to find an existing shared pool with the same characteristics and
118 * returns it, otherwise creates this one. NULL is returned if no memory
119 * is available for a new creation.
120 */
121struct pool_head *create_pool(char *name, unsigned int size, unsigned int flags);
Willy Tarreau7107c8b2018-11-26 11:44:35 +0100122void create_pool_callback(struct pool_head **ptr, char *name, unsigned int size);
123
124/* This registers a call to create_pool_callback(ptr, name, size) */
125#define REGISTER_POOL(ptr, name, size) \
126 INITCALL3(STG_POOL, create_pool_callback, (ptr), (name), (size))
127
128/* This macro declares a pool head <ptr> and registers its creation */
129#define DECLARE_POOL(ptr, name, size) \
130 struct pool_head *(ptr) = NULL; \
131 REGISTER_POOL(&ptr, name, size)
132
133/* This macro declares a static pool head <ptr> and registers its creation */
134#define DECLARE_STATIC_POOL(ptr, name, size) \
135 static struct pool_head *(ptr); \
136 REGISTER_POOL(&ptr, name, size)
Willy Tarreau50e608d2007-05-13 18:26:08 +0200137
138/* Dump statistics on pools usage.
139 */
Willy Tarreau12833bb2014-01-28 16:49:56 +0100140void dump_pools_to_trash();
Willy Tarreau50e608d2007-05-13 18:26:08 +0200141void dump_pools(void);
Willy Tarreau58102cf2015-10-28 16:24:21 +0100142int pool_total_failures();
143unsigned long pool_total_allocated();
144unsigned long pool_total_used();
Willy Tarreau50e608d2007-05-13 18:26:08 +0200145
146/*
Willy Tarreaue6ce59d2007-05-13 19:38:49 +0200147 * This function frees whatever can be freed in pool <pool>.
148 */
Willy Tarreaubafbe012017-11-24 17:34:44 +0100149void pool_flush(struct pool_head *pool);
Willy Tarreaue6ce59d2007-05-13 19:38:49 +0200150
151/*
152 * This function frees whatever can be freed in all pools, but respecting
153 * the minimum thresholds imposed by owners.
Christopher Fauletb349e482017-08-29 09:52:38 +0200154 *
Willy Tarreaubafbe012017-11-24 17:34:44 +0100155 * <pool_ctx> is used when pool_gc is called to release resources to allocate
Christopher Fauletb349e482017-08-29 09:52:38 +0200156 * an element in __pool_refill_alloc. It is important because <pool_ctx> is
157 * already locked, so we need to skip the lock here.
Willy Tarreaue6ce59d2007-05-13 19:38:49 +0200158 */
Willy Tarreaubafbe012017-11-24 17:34:44 +0100159void pool_gc(struct pool_head *pool_ctx);
Willy Tarreaue6ce59d2007-05-13 19:38:49 +0200160
161/*
162 * This function destroys a pull by freeing it completely.
163 * This should be called only under extreme circumstances.
164 */
Willy Tarreaubafbe012017-11-24 17:34:44 +0100165void *pool_destroy(struct pool_head *pool);
Willy Tarreau2455ceb2018-11-26 15:57:34 +0100166void pool_destroy_all();
Willy Tarreaue6ce59d2007-05-13 19:38:49 +0200167
Willy Tarreau0a93b642018-10-16 07:58:39 +0200168/* returns the pool index for pool <pool>, or -1 if this pool has no index */
169static inline ssize_t pool_get_index(const struct pool_head *pool)
170{
171 size_t idx;
172
173 idx = pool - pool_base_start;
174 if (idx >= MAX_BASE_POOLS)
175 return -1;
176 return idx;
177}
178
Willy Tarreauf161d0f2018-02-22 14:05:55 +0100179#ifdef CONFIG_HAP_LOCKLESS_POOLS
Willy Tarreaue18db9e2018-10-16 10:28:54 +0200180
181/* Tries to retrieve an object from the local pool cache corresponding to pool
182 * <pool>. Returns NULL if none is available.
183 */
184static inline void *__pool_get_from_cache(struct pool_head *pool)
185{
186 ssize_t idx = pool_get_index(pool);
187 struct pool_cache_item *item;
Willy Tarreau7f0165e2018-11-26 17:09:46 +0100188 struct pool_cache_head *ph;
Willy Tarreaue18db9e2018-10-16 10:28:54 +0200189
190 /* pool not in cache */
191 if (idx < 0)
192 return NULL;
193
Willy Tarreau7f0165e2018-11-26 17:09:46 +0100194 ph = &pool_cache[tid][idx];
195 if (LIST_ISEMPTY(&ph->list))
196 return NULL; // empty
Willy Tarreaue18db9e2018-10-16 10:28:54 +0200197
Willy Tarreau7f0165e2018-11-26 17:09:46 +0100198 item = LIST_NEXT(&ph->list, typeof(item), by_pool);
199 ph->count--;
200 pool_cache_bytes -= ph->size;
Willy Tarreaue18db9e2018-10-16 10:28:54 +0200201 pool_cache_count--;
202 LIST_DEL(&item->by_pool);
203 LIST_DEL(&item->by_lru);
Willy Tarreau8e9f4532018-10-28 20:09:12 +0100204#ifdef DEBUG_MEMORY_POOLS
205 /* keep track of where the element was allocated from */
206 *POOL_LINK(pool, item) = (void *)pool;
207#endif
Willy Tarreaue18db9e2018-10-16 10:28:54 +0200208 return item;
209}
210
Olivier Houchardcf975d42018-01-24 18:38:31 +0100211/*
212 * Returns a pointer to type <type> taken from the pool <pool_type> if
213 * available, otherwise returns NULL. No malloc() is attempted, and poisonning
214 * is never performed. The purpose is to get the fastest possible allocation.
215 */
216static inline void *__pool_get_first(struct pool_head *pool)
217{
218 struct pool_free_list cmp, new;
Willy Tarreaue18db9e2018-10-16 10:28:54 +0200219 void *ret = __pool_get_from_cache(pool);
220
221 if (ret)
222 return ret;
Olivier Houchardcf975d42018-01-24 18:38:31 +0100223
224 cmp.seq = pool->seq;
225 __ha_barrier_load();
226
227 cmp.free_list = pool->free_list;
228 do {
Olivier Houchard5dfbf2e2020-03-18 15:48:29 +0100229 if (cmp.free_list == NULL)
Olivier Houchardcf975d42018-01-24 18:38:31 +0100230 return NULL;
231 new.seq = cmp.seq + 1;
232 __ha_barrier_load();
233 new.free_list = *POOL_LINK(pool, cmp.free_list);
Willy Tarreau6a38b322019-05-11 18:04:24 +0200234 } while (HA_ATOMIC_DWCAS((void *)&pool->free_list, (void *)&cmp, (void *)&new) == 0);
Olivier Houchard20872762019-03-08 18:53:35 +0100235 __ha_barrier_atomic_store();
Tim Duesterhus05f6a432018-02-20 00:49:46 +0100236
Olivier Houchard20872762019-03-08 18:53:35 +0100237 _HA_ATOMIC_ADD(&pool->used, 1);
Olivier Houchardcf975d42018-01-24 18:38:31 +0100238#ifdef DEBUG_MEMORY_POOLS
239 /* keep track of where the element was allocated from */
240 *POOL_LINK(pool, cmp.free_list) = (void *)pool;
241#endif
242 return cmp.free_list;
243}
244
245static inline void *pool_get_first(struct pool_head *pool)
246{
247 void *ret;
248
249 ret = __pool_get_first(pool);
250 return ret;
251}
252/*
253 * Returns a pointer to type <type> taken from the pool <pool_type> or
254 * dynamically allocated. In the first case, <pool_type> is updated to point to
255 * the next element in the list. No memory poisonning is ever performed on the
256 * returned area.
257 */
258static inline void *pool_alloc_dirty(struct pool_head *pool)
259{
260 void *p;
261
262 if ((p = __pool_get_first(pool)) == NULL)
263 p = __pool_refill_alloc(pool, 0);
264 return p;
265}
266
Willy Tarreaue6ce59d2007-05-13 19:38:49 +0200267/*
Olivier Houchardcf975d42018-01-24 18:38:31 +0100268 * Returns a pointer to type <type> taken from the pool <pool_type> or
269 * dynamically allocated. In the first case, <pool_type> is updated to point to
270 * the next element in the list. Memory poisonning is performed if enabled.
271 */
272static inline void *pool_alloc(struct pool_head *pool)
273{
274 void *p;
275
276 p = pool_alloc_dirty(pool);
Olivier Houchardcf975d42018-01-24 18:38:31 +0100277 if (p && mem_poison_byte >= 0) {
278 memset(p, mem_poison_byte, pool->size);
279 }
280
281 return p;
282}
283
Willy Tarreau146794d2018-10-16 08:55:15 +0200284/* Locklessly add item <ptr> to pool <pool>, then update the pool used count.
285 * Both the pool and the pointer must be valid. Use pool_free() for normal
286 * operations.
287 */
288static inline void __pool_free(struct pool_head *pool, void *ptr)
289{
Willy Tarreau7a6ad882018-10-20 17:37:38 +0200290 void **free_list = pool->free_list;
Willy Tarreau146794d2018-10-16 08:55:15 +0200291
292 do {
293 *POOL_LINK(pool, ptr) = (void *)free_list;
294 __ha_barrier_store();
Olivier Houchard20872762019-03-08 18:53:35 +0100295 } while (!_HA_ATOMIC_CAS(&pool->free_list, &free_list, ptr));
296 __ha_barrier_atomic_store();
297 _HA_ATOMIC_SUB(&pool->used, 1);
Willy Tarreau146794d2018-10-16 08:55:15 +0200298}
299
Willy Tarreaue18db9e2018-10-16 10:28:54 +0200300/* frees an object to the local cache, possibly pushing oldest objects to the
301 * global pool.
302 */
303void __pool_put_to_cache(struct pool_head *pool, void *ptr, ssize_t idx);
304static inline void pool_put_to_cache(struct pool_head *pool, void *ptr)
305{
306 ssize_t idx = pool_get_index(pool);
307
308 /* pool not in cache or too many objects for this pool (more than
309 * half of the cache is used and this pool uses more than 1/8 of
310 * the cache size).
311 */
312 if (idx < 0 ||
313 (pool_cache_bytes > CONFIG_HAP_POOL_CACHE_SIZE * 3 / 4 &&
Willy Tarreau7f0165e2018-11-26 17:09:46 +0100314 pool_cache[tid][idx].count >= 16 + pool_cache_count / 8)) {
Willy Tarreaue18db9e2018-10-16 10:28:54 +0200315 __pool_free(pool, ptr);
316 return;
317 }
318 __pool_put_to_cache(pool, ptr, idx);
319}
320
Olivier Houchardcf975d42018-01-24 18:38:31 +0100321/*
322 * Puts a memory area back to the corresponding pool.
323 * Items are chained directly through a pointer that
324 * is written in the beginning of the memory area, so
325 * there's no need for any carrier cell. This implies
326 * that each memory area is at least as big as one
327 * pointer. Just like with the libc's free(), nothing
328 * is done if <ptr> is NULL.
329 */
330static inline void pool_free(struct pool_head *pool, void *ptr)
331{
332 if (likely(ptr != NULL)) {
Olivier Houchardcf975d42018-01-24 18:38:31 +0100333#ifdef DEBUG_MEMORY_POOLS
334 /* we'll get late corruption if we refill to the wrong pool or double-free */
335 if (*POOL_LINK(pool, ptr) != (void *)pool)
336 *(volatile int *)0 = 0;
337#endif
Willy Tarreaubbcce3d2019-11-15 06:59:54 +0100338 if (mem_poison_byte >= 0)
339 memset(ptr, mem_poison_byte, pool->size);
Willy Tarreaue18db9e2018-10-16 10:28:54 +0200340 pool_put_to_cache(pool, ptr);
Olivier Houchardcf975d42018-01-24 18:38:31 +0100341 }
342}
343
Willy Tarreauf161d0f2018-02-22 14:05:55 +0100344#else /* CONFIG_HAP_LOCKLESS_POOLS */
Olivier Houchardcf975d42018-01-24 18:38:31 +0100345/*
Willy Tarreau02622412014-12-08 16:35:23 +0100346 * Returns a pointer to type <type> taken from the pool <pool_type> if
347 * available, otherwise returns NULL. No malloc() is attempted, and poisonning
348 * is never performed. The purpose is to get the fastest possible allocation.
Willy Tarreau50e608d2007-05-13 18:26:08 +0200349 */
Christopher Fauletb349e482017-08-29 09:52:38 +0200350static inline void *__pool_get_first(struct pool_head *pool)
Willy Tarreaue430e772014-12-23 14:13:16 +0100351{
352 void *p;
353
Willy Tarreau02622412014-12-08 16:35:23 +0100354 if ((p = pool->free_list) != NULL) {
Willy Tarreauac421112015-10-28 15:09:29 +0100355 pool->free_list = *POOL_LINK(pool, p);
Willy Tarreaue430e772014-12-23 14:13:16 +0100356 pool->used++;
Willy Tarreaude30a682015-10-28 15:23:51 +0100357#ifdef DEBUG_MEMORY_POOLS
358 /* keep track of where the element was allocated from */
359 *POOL_LINK(pool, p) = (void *)pool;
360#endif
Willy Tarreaue430e772014-12-23 14:13:16 +0100361 }
362 return p;
363}
Willy Tarreau50e608d2007-05-13 18:26:08 +0200364
Christopher Fauletb349e482017-08-29 09:52:38 +0200365static inline void *pool_get_first(struct pool_head *pool)
366{
367 void *ret;
368
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100369 HA_SPIN_LOCK(POOL_LOCK, &pool->lock);
Christopher Fauletb349e482017-08-29 09:52:38 +0200370 ret = __pool_get_first(pool);
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100371 HA_SPIN_UNLOCK(POOL_LOCK, &pool->lock);
Christopher Fauletb349e482017-08-29 09:52:38 +0200372 return ret;
373}
Willy Tarreau50e608d2007-05-13 18:26:08 +0200374/*
Willy Tarreau02622412014-12-08 16:35:23 +0100375 * Returns a pointer to type <type> taken from the pool <pool_type> or
376 * dynamically allocated. In the first case, <pool_type> is updated to point to
377 * the next element in the list. No memory poisonning is ever performed on the
378 * returned area.
379 */
380static inline void *pool_alloc_dirty(struct pool_head *pool)
381{
382 void *p;
383
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100384 HA_SPIN_LOCK(POOL_LOCK, &pool->lock);
Christopher Fauletb349e482017-08-29 09:52:38 +0200385 if ((p = __pool_get_first(pool)) == NULL)
386 p = __pool_refill_alloc(pool, 0);
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100387 HA_SPIN_UNLOCK(POOL_LOCK, &pool->lock);
Willy Tarreau02622412014-12-08 16:35:23 +0100388 return p;
389}
390
Willy Tarreau158fa752017-11-22 15:47:29 +0100391#ifndef DEBUG_UAF /* normal allocator */
392
Willy Tarreauf13322e2017-11-22 10:50:54 +0100393/* allocates an area of size <size> and returns it. The semantics are similar
394 * to those of malloc().
395 */
396static inline void *pool_alloc_area(size_t size)
397{
398 return malloc(size);
399}
400
401/* frees an area <area> of size <size> allocated by pool_alloc_area(). The
402 * semantics are identical to free() except that the size is specified and
403 * may be ignored.
404 */
405static inline void pool_free_area(void *area, size_t __maybe_unused size)
406{
407 free(area);
408}
409
Willy Tarreau158fa752017-11-22 15:47:29 +0100410#else /* use-after-free detector */
411
412/* allocates an area of size <size> and returns it. The semantics are similar
413 * to those of malloc(). However the allocation is rounded up to 4kB so that a
414 * full page is allocated. This ensures the object can be freed alone so that
415 * future dereferences are easily detected. The returned object is always
Willy Tarreau364d7452018-02-22 14:14:23 +0100416 * 16-bytes aligned to avoid issues with unaligned structure objects. In case
417 * some padding is added, the area's start address is copied at the end of the
418 * padding to help detect underflows.
Willy Tarreau158fa752017-11-22 15:47:29 +0100419 */
Olivier Houchard62975a72018-10-21 01:33:11 +0200420#include <errno.h>
Willy Tarreau158fa752017-11-22 15:47:29 +0100421static inline void *pool_alloc_area(size_t size)
422{
423 size_t pad = (4096 - size) & 0xFF0;
Willy Tarreau5a9cce42018-02-22 11:39:23 +0100424 void *ret;
Willy Tarreau158fa752017-11-22 15:47:29 +0100425
Olivier Houchard62975a72018-10-21 01:33:11 +0200426 ret = mmap(NULL, (size + 4095) & -4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
Willy Tarreau364d7452018-02-22 14:14:23 +0100427 if (ret == MAP_FAILED)
428 return NULL;
429 if (pad >= sizeof(void *))
430 *(void **)(ret + pad - sizeof(void *)) = ret + pad;
431 return ret + pad;
Willy Tarreau158fa752017-11-22 15:47:29 +0100432}
433
434/* frees an area <area> of size <size> allocated by pool_alloc_area(). The
435 * semantics are identical to free() except that the size must absolutely match
Willy Tarreau364d7452018-02-22 14:14:23 +0100436 * the one passed to pool_alloc_area(). In case some padding is added, the
437 * area's start address is compared to the one at the end of the padding, and
438 * a segfault is triggered if they don't match, indicating an underflow.
Willy Tarreau158fa752017-11-22 15:47:29 +0100439 */
440static inline void pool_free_area(void *area, size_t size)
441{
442 size_t pad = (4096 - size) & 0xFF0;
443
Willy Tarreau364d7452018-02-22 14:14:23 +0100444 if (pad >= sizeof(void *) && *(void **)(area - sizeof(void *)) != area)
445 *(volatile int *)0 = 0;
446
Willy Tarreau158fa752017-11-22 15:47:29 +0100447 munmap(area - pad, (size + 4095) & -4096);
448}
449
450#endif /* DEBUG_UAF */
451
Willy Tarreau02622412014-12-08 16:35:23 +0100452/*
453 * Returns a pointer to type <type> taken from the pool <pool_type> or
454 * dynamically allocated. In the first case, <pool_type> is updated to point to
455 * the next element in the list. Memory poisonning is performed if enabled.
456 */
Willy Tarreaubafbe012017-11-24 17:34:44 +0100457static inline void *pool_alloc(struct pool_head *pool)
Willy Tarreau02622412014-12-08 16:35:23 +0100458{
459 void *p;
460
461 p = pool_alloc_dirty(pool);
Willy Tarreaude30a682015-10-28 15:23:51 +0100462 if (p && mem_poison_byte >= 0) {
Willy Tarreau02622412014-12-08 16:35:23 +0100463 memset(p, mem_poison_byte, pool->size);
Willy Tarreaude30a682015-10-28 15:23:51 +0100464 }
465
Willy Tarreau02622412014-12-08 16:35:23 +0100466 return p;
467}
468
469/*
Willy Tarreau50e608d2007-05-13 18:26:08 +0200470 * Puts a memory area back to the corresponding pool.
471 * Items are chained directly through a pointer that
472 * is written in the beginning of the memory area, so
473 * there's no need for any carrier cell. This implies
474 * that each memory area is at least as big as one
Willy Tarreau48d63db2008-08-03 17:41:33 +0200475 * pointer. Just like with the libc's free(), nothing
476 * is done if <ptr> is NULL.
Willy Tarreau50e608d2007-05-13 18:26:08 +0200477 */
Willy Tarreaubafbe012017-11-24 17:34:44 +0100478static inline void pool_free(struct pool_head *pool, void *ptr)
Willy Tarreaue430e772014-12-23 14:13:16 +0100479{
480 if (likely(ptr != NULL)) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100481 HA_SPIN_LOCK(POOL_LOCK, &pool->lock);
Willy Tarreaude30a682015-10-28 15:23:51 +0100482#ifdef DEBUG_MEMORY_POOLS
483 /* we'll get late corruption if we refill to the wrong pool or double-free */
484 if (*POOL_LINK(pool, ptr) != (void *)pool)
Olivier Houchard51e47412018-12-16 00:58:28 +0100485 *(volatile int *)0 = 0;
Willy Tarreaude30a682015-10-28 15:23:51 +0100486#endif
Willy Tarreau158fa752017-11-22 15:47:29 +0100487
488#ifndef DEBUG_UAF /* normal pool behaviour */
Willy Tarreauac421112015-10-28 15:09:29 +0100489 *POOL_LINK(pool, ptr) = (void *)pool->free_list;
Willy Tarreaue430e772014-12-23 14:13:16 +0100490 pool->free_list = (void *)ptr;
Willy Tarreau158fa752017-11-22 15:47:29 +0100491#else /* release the entry for real to detect use after free */
492 /* ensure we crash on double free or free of a const area*/
493 *(uint32_t *)ptr = 0xDEADADD4;
494 pool_free_area(ptr, pool->size + POOL_EXTRA);
495 pool->allocated--;
496#endif /* DEBUG_UAF */
Willy Tarreaue430e772014-12-23 14:13:16 +0100497 pool->used--;
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100498 HA_SPIN_UNLOCK(POOL_LOCK, &pool->lock);
Willy Tarreaue430e772014-12-23 14:13:16 +0100499 }
500}
Willy Tarreauf161d0f2018-02-22 14:05:55 +0100501#endif /* CONFIG_HAP_LOCKLESS_POOLS */
Willy Tarreau2dd0d472006-06-29 17:53:05 +0200502#endif /* _COMMON_MEMORY_H */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200503
504/*
505 * Local variables:
506 * c-indent-level: 8
507 * c-basic-offset: 8
508 * End:
509 */