blob: 57d6ba63e69f20f76a293fa48a1d5e8ca61a8343 [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>
David Carlier4ee76d02018-02-18 19:36:42 +000029#include <stdint.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 Tarreaubaaee002006-06-26 02:48:02 +020035
Willy Tarreaua84dcb82015-10-28 12:04:02 +010036#ifndef DEBUG_DONT_SHARE_POOLS
Willy Tarreau50e608d2007-05-13 18:26:08 +020037#define MEM_F_SHARED 0x1
Willy Tarreaua84dcb82015-10-28 12:04:02 +010038#else
39#define MEM_F_SHARED 0
40#endif
Willy Tarreau581bf812016-01-25 02:19:13 +010041#define MEM_F_EXACT 0x2
Willy Tarreau50e608d2007-05-13 18:26:08 +020042
Willy Tarreauac421112015-10-28 15:09:29 +010043/* reserve an extra void* at the end of a pool for linking */
44#ifdef DEBUG_MEMORY_POOLS
45#define POOL_EXTRA (sizeof(void *))
46#define POOL_LINK(pool, item) (void **)(((char *)item) + (pool->size))
47#else
48#define POOL_EXTRA (0)
49#define POOL_LINK(pool, item) ((void **)(item))
50#endif
51
Willy Tarreau0a93b642018-10-16 07:58:39 +020052#define MAX_BASE_POOLS 32
53
Willy Tarreaue18db9e2018-10-16 10:28:54 +020054struct pool_cache_head {
55 struct list list; /* head of objects in this pool */
56 size_t size; /* size of an object */
57 unsigned int count; /* number of objects in this pool */
58};
59
60struct pool_cache_item {
61 struct list by_pool; /* link to objects in this pool */
62 struct list by_lru; /* link to objects by LRU order */
63};
64
65extern THREAD_LOCAL struct pool_cache_head pool_cache[MAX_BASE_POOLS];
66extern THREAD_LOCAL struct list pool_lru_head; /* oldest objects */
67extern THREAD_LOCAL size_t pool_cache_bytes; /* total cache size */
68extern THREAD_LOCAL size_t pool_cache_count; /* #cache objects */
69
Willy Tarreauf161d0f2018-02-22 14:05:55 +010070#ifdef CONFIG_HAP_LOCKLESS_POOLS
Olivier Houchardcf975d42018-01-24 18:38:31 +010071struct pool_free_list {
72 void **free_list;
73 uintptr_t seq;
74};
75#endif
76
Willy Tarreau50e608d2007-05-13 18:26:08 +020077struct pool_head {
Willy Tarreau1ca1b702017-11-26 10:50:36 +010078 void **free_list;
Willy Tarreauf161d0f2018-02-22 14:05:55 +010079#ifdef CONFIG_HAP_LOCKLESS_POOLS
Olivier Houchardcf975d42018-01-24 18:38:31 +010080 uintptr_t seq;
81#else
82 __decl_hathreads(HA_SPINLOCK_T lock); /* the spin lock */
83#endif
Willy Tarreau50e608d2007-05-13 18:26:08 +020084 unsigned int used; /* how many chunks are currently in use */
85 unsigned int allocated; /* how many chunks have been allocated */
86 unsigned int limit; /* hard limit on the number of chunks */
87 unsigned int minavail; /* how many chunks are expected to be used */
88 unsigned int size; /* chunk size */
89 unsigned int flags; /* MEM_F_* */
Willy Tarreau7dcd46d2007-05-14 00:16:13 +020090 unsigned int users; /* number of pools sharing this zone */
Willy Tarreau58102cf2015-10-28 16:24:21 +010091 unsigned int failed; /* failed allocations */
Olivier Houchardcf975d42018-01-24 18:38:31 +010092 struct list list; /* list of all known pools */
Willy Tarreau7dcd46d2007-05-14 00:16:13 +020093 char name[12]; /* name of the pool */
Willy Tarreau1ca1b702017-11-26 10:50:36 +010094} __attribute__((aligned(64)));
Willy Tarreau50e608d2007-05-13 18:26:08 +020095
Willy Tarreau0a93b642018-10-16 07:58:39 +020096
97extern struct pool_head pool_base_start[MAX_BASE_POOLS];
98extern unsigned int pool_base_count;
99
Willy Tarreau067ac9f2015-10-08 14:12:13 +0200100/* poison each newly allocated area with this byte if >= 0 */
101extern int mem_poison_byte;
Willy Tarreau50e608d2007-05-13 18:26:08 +0200102
Willy Tarreaua885f6d2014-12-03 15:25:28 +0100103/* Allocates new entries for pool <pool> until there are at least <avail> + 1
104 * available, then returns the last one for immediate use, so that at least
105 * <avail> are left available in the pool upon return. NULL is returned if the
106 * last entry could not be allocated. It's important to note that at least one
107 * allocation is always performed even if there are enough entries in the pool.
108 * A call to the garbage collector is performed at most once in case malloc()
109 * returns an error, before returning NULL.
Willy Tarreau50e608d2007-05-13 18:26:08 +0200110 */
Christopher Fauletb349e482017-08-29 09:52:38 +0200111void *__pool_refill_alloc(struct pool_head *pool, unsigned int avail);
Willy Tarreaua885f6d2014-12-03 15:25:28 +0100112void *pool_refill_alloc(struct pool_head *pool, unsigned int avail);
Willy Tarreau50e608d2007-05-13 18:26:08 +0200113
114/* Try to find an existing shared pool with the same characteristics and
115 * returns it, otherwise creates this one. NULL is returned if no memory
116 * is available for a new creation.
117 */
118struct pool_head *create_pool(char *name, unsigned int size, unsigned int flags);
119
120/* Dump statistics on pools usage.
121 */
Willy Tarreau12833bb2014-01-28 16:49:56 +0100122void dump_pools_to_trash();
Willy Tarreau50e608d2007-05-13 18:26:08 +0200123void dump_pools(void);
Willy Tarreau58102cf2015-10-28 16:24:21 +0100124int pool_total_failures();
125unsigned long pool_total_allocated();
126unsigned long pool_total_used();
Willy Tarreau50e608d2007-05-13 18:26:08 +0200127
128/*
Willy Tarreaue6ce59d2007-05-13 19:38:49 +0200129 * This function frees whatever can be freed in pool <pool>.
130 */
Willy Tarreaubafbe012017-11-24 17:34:44 +0100131void pool_flush(struct pool_head *pool);
Willy Tarreaue6ce59d2007-05-13 19:38:49 +0200132
133/*
134 * This function frees whatever can be freed in all pools, but respecting
135 * the minimum thresholds imposed by owners.
Christopher Fauletb349e482017-08-29 09:52:38 +0200136 *
Willy Tarreaubafbe012017-11-24 17:34:44 +0100137 * <pool_ctx> is used when pool_gc is called to release resources to allocate
Christopher Fauletb349e482017-08-29 09:52:38 +0200138 * an element in __pool_refill_alloc. It is important because <pool_ctx> is
139 * already locked, so we need to skip the lock here.
Willy Tarreaue6ce59d2007-05-13 19:38:49 +0200140 */
Willy Tarreaubafbe012017-11-24 17:34:44 +0100141void pool_gc(struct pool_head *pool_ctx);
Willy Tarreaue6ce59d2007-05-13 19:38:49 +0200142
143/*
144 * This function destroys a pull by freeing it completely.
145 * This should be called only under extreme circumstances.
146 */
Willy Tarreaubafbe012017-11-24 17:34:44 +0100147void *pool_destroy(struct pool_head *pool);
Willy Tarreaue6ce59d2007-05-13 19:38:49 +0200148
Willy Tarreau0a93b642018-10-16 07:58:39 +0200149/* returns the pool index for pool <pool>, or -1 if this pool has no index */
150static inline ssize_t pool_get_index(const struct pool_head *pool)
151{
152 size_t idx;
153
154 idx = pool - pool_base_start;
155 if (idx >= MAX_BASE_POOLS)
156 return -1;
157 return idx;
158}
159
Willy Tarreauf161d0f2018-02-22 14:05:55 +0100160#ifdef CONFIG_HAP_LOCKLESS_POOLS
Willy Tarreaue18db9e2018-10-16 10:28:54 +0200161
162/* Tries to retrieve an object from the local pool cache corresponding to pool
163 * <pool>. Returns NULL if none is available.
164 */
165static inline void *__pool_get_from_cache(struct pool_head *pool)
166{
167 ssize_t idx = pool_get_index(pool);
168 struct pool_cache_item *item;
169
170 /* pool not in cache */
171 if (idx < 0)
172 return NULL;
173
174 /* never allocated or empty */
175 if (pool_cache[idx].list.n == NULL || LIST_ISEMPTY(&pool_cache[idx].list))
176 return NULL;
177
178 item = LIST_NEXT(&pool_cache[idx].list, typeof(item), by_pool);
179 pool_cache[idx].count--;
180 pool_cache_bytes -= pool_cache[idx].size;
181 pool_cache_count--;
182 LIST_DEL(&item->by_pool);
183 LIST_DEL(&item->by_lru);
Willy Tarreau8e9f4532018-10-28 20:09:12 +0100184#ifdef DEBUG_MEMORY_POOLS
185 /* keep track of where the element was allocated from */
186 *POOL_LINK(pool, item) = (void *)pool;
187#endif
Willy Tarreaue18db9e2018-10-16 10:28:54 +0200188 return item;
189}
190
Olivier Houchardcf975d42018-01-24 18:38:31 +0100191/*
192 * Returns a pointer to type <type> taken from the pool <pool_type> if
193 * available, otherwise returns NULL. No malloc() is attempted, and poisonning
194 * is never performed. The purpose is to get the fastest possible allocation.
195 */
196static inline void *__pool_get_first(struct pool_head *pool)
197{
198 struct pool_free_list cmp, new;
Willy Tarreaue18db9e2018-10-16 10:28:54 +0200199 void *ret = __pool_get_from_cache(pool);
200
201 if (ret)
202 return ret;
Olivier Houchardcf975d42018-01-24 18:38:31 +0100203
204 cmp.seq = pool->seq;
205 __ha_barrier_load();
206
207 cmp.free_list = pool->free_list;
208 do {
209 if (cmp.free_list == NULL)
210 return NULL;
211 new.seq = cmp.seq + 1;
212 __ha_barrier_load();
213 new.free_list = *POOL_LINK(pool, cmp.free_list);
214 } while (__ha_cas_dw((void *)&pool->free_list, (void *)&cmp, (void *)&new) == 0);
Tim Duesterhus05f6a432018-02-20 00:49:46 +0100215
Olivier Houchardcf975d42018-01-24 18:38:31 +0100216 HA_ATOMIC_ADD(&pool->used, 1);
217#ifdef DEBUG_MEMORY_POOLS
218 /* keep track of where the element was allocated from */
219 *POOL_LINK(pool, cmp.free_list) = (void *)pool;
220#endif
221 return cmp.free_list;
222}
223
224static inline void *pool_get_first(struct pool_head *pool)
225{
226 void *ret;
227
228 ret = __pool_get_first(pool);
229 return ret;
230}
231/*
232 * Returns a pointer to type <type> taken from the pool <pool_type> or
233 * dynamically allocated. In the first case, <pool_type> is updated to point to
234 * the next element in the list. No memory poisonning is ever performed on the
235 * returned area.
236 */
237static inline void *pool_alloc_dirty(struct pool_head *pool)
238{
239 void *p;
240
241 if ((p = __pool_get_first(pool)) == NULL)
242 p = __pool_refill_alloc(pool, 0);
243 return p;
244}
245
Willy Tarreaue6ce59d2007-05-13 19:38:49 +0200246/*
Olivier Houchardcf975d42018-01-24 18:38:31 +0100247 * Returns a pointer to type <type> taken from the pool <pool_type> or
248 * dynamically allocated. In the first case, <pool_type> is updated to point to
249 * the next element in the list. Memory poisonning is performed if enabled.
250 */
251static inline void *pool_alloc(struct pool_head *pool)
252{
253 void *p;
254
255 p = pool_alloc_dirty(pool);
Olivier Houchardcf975d42018-01-24 18:38:31 +0100256 if (p && mem_poison_byte >= 0) {
257 memset(p, mem_poison_byte, pool->size);
258 }
259
260 return p;
261}
262
Willy Tarreau146794d2018-10-16 08:55:15 +0200263/* Locklessly add item <ptr> to pool <pool>, then update the pool used count.
264 * Both the pool and the pointer must be valid. Use pool_free() for normal
265 * operations.
266 */
267static inline void __pool_free(struct pool_head *pool, void *ptr)
268{
Willy Tarreau7a6ad882018-10-20 17:37:38 +0200269 void **free_list = pool->free_list;
Willy Tarreau146794d2018-10-16 08:55:15 +0200270
271 do {
272 *POOL_LINK(pool, ptr) = (void *)free_list;
273 __ha_barrier_store();
Willy Tarreau7a6ad882018-10-20 17:37:38 +0200274 } while (!HA_ATOMIC_CAS(&pool->free_list, &free_list, ptr));
Willy Tarreau146794d2018-10-16 08:55:15 +0200275 HA_ATOMIC_SUB(&pool->used, 1);
276}
277
Willy Tarreaue18db9e2018-10-16 10:28:54 +0200278/* frees an object to the local cache, possibly pushing oldest objects to the
279 * global pool.
280 */
281void __pool_put_to_cache(struct pool_head *pool, void *ptr, ssize_t idx);
282static inline void pool_put_to_cache(struct pool_head *pool, void *ptr)
283{
284 ssize_t idx = pool_get_index(pool);
285
286 /* pool not in cache or too many objects for this pool (more than
287 * half of the cache is used and this pool uses more than 1/8 of
288 * the cache size).
289 */
290 if (idx < 0 ||
291 (pool_cache_bytes > CONFIG_HAP_POOL_CACHE_SIZE * 3 / 4 &&
292 pool_cache[idx].count >= 16 + pool_cache_count / 8)) {
293 __pool_free(pool, ptr);
294 return;
295 }
296 __pool_put_to_cache(pool, ptr, idx);
297}
298
Olivier Houchardcf975d42018-01-24 18:38:31 +0100299/*
300 * Puts a memory area back to the corresponding pool.
301 * Items are chained directly through a pointer that
302 * is written in the beginning of the memory area, so
303 * there's no need for any carrier cell. This implies
304 * that each memory area is at least as big as one
305 * pointer. Just like with the libc's free(), nothing
306 * is done if <ptr> is NULL.
307 */
308static inline void pool_free(struct pool_head *pool, void *ptr)
309{
310 if (likely(ptr != NULL)) {
Olivier Houchardcf975d42018-01-24 18:38:31 +0100311#ifdef DEBUG_MEMORY_POOLS
312 /* we'll get late corruption if we refill to the wrong pool or double-free */
313 if (*POOL_LINK(pool, ptr) != (void *)pool)
314 *(volatile int *)0 = 0;
315#endif
Willy Tarreaue18db9e2018-10-16 10:28:54 +0200316 pool_put_to_cache(pool, ptr);
Olivier Houchardcf975d42018-01-24 18:38:31 +0100317 }
318}
319
Willy Tarreauf161d0f2018-02-22 14:05:55 +0100320#else /* CONFIG_HAP_LOCKLESS_POOLS */
Olivier Houchardcf975d42018-01-24 18:38:31 +0100321/*
Willy Tarreau02622412014-12-08 16:35:23 +0100322 * Returns a pointer to type <type> taken from the pool <pool_type> if
323 * available, otherwise returns NULL. No malloc() is attempted, and poisonning
324 * is never performed. The purpose is to get the fastest possible allocation.
Willy Tarreau50e608d2007-05-13 18:26:08 +0200325 */
Christopher Fauletb349e482017-08-29 09:52:38 +0200326static inline void *__pool_get_first(struct pool_head *pool)
Willy Tarreaue430e772014-12-23 14:13:16 +0100327{
328 void *p;
329
Willy Tarreau02622412014-12-08 16:35:23 +0100330 if ((p = pool->free_list) != NULL) {
Willy Tarreauac421112015-10-28 15:09:29 +0100331 pool->free_list = *POOL_LINK(pool, p);
Willy Tarreaue430e772014-12-23 14:13:16 +0100332 pool->used++;
Willy Tarreaude30a682015-10-28 15:23:51 +0100333#ifdef DEBUG_MEMORY_POOLS
334 /* keep track of where the element was allocated from */
335 *POOL_LINK(pool, p) = (void *)pool;
336#endif
Willy Tarreaue430e772014-12-23 14:13:16 +0100337 }
338 return p;
339}
Willy Tarreau50e608d2007-05-13 18:26:08 +0200340
Christopher Fauletb349e482017-08-29 09:52:38 +0200341static inline void *pool_get_first(struct pool_head *pool)
342{
343 void *ret;
344
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100345 HA_SPIN_LOCK(POOL_LOCK, &pool->lock);
Christopher Fauletb349e482017-08-29 09:52:38 +0200346 ret = __pool_get_first(pool);
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100347 HA_SPIN_UNLOCK(POOL_LOCK, &pool->lock);
Christopher Fauletb349e482017-08-29 09:52:38 +0200348 return ret;
349}
Willy Tarreau50e608d2007-05-13 18:26:08 +0200350/*
Willy Tarreau02622412014-12-08 16:35:23 +0100351 * Returns a pointer to type <type> taken from the pool <pool_type> or
352 * dynamically allocated. In the first case, <pool_type> is updated to point to
353 * the next element in the list. No memory poisonning is ever performed on the
354 * returned area.
355 */
356static inline void *pool_alloc_dirty(struct pool_head *pool)
357{
358 void *p;
359
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100360 HA_SPIN_LOCK(POOL_LOCK, &pool->lock);
Christopher Fauletb349e482017-08-29 09:52:38 +0200361 if ((p = __pool_get_first(pool)) == NULL)
362 p = __pool_refill_alloc(pool, 0);
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100363 HA_SPIN_UNLOCK(POOL_LOCK, &pool->lock);
Willy Tarreau02622412014-12-08 16:35:23 +0100364 return p;
365}
366
Willy Tarreau158fa752017-11-22 15:47:29 +0100367#ifndef DEBUG_UAF /* normal allocator */
368
Willy Tarreauf13322e2017-11-22 10:50:54 +0100369/* allocates an area of size <size> and returns it. The semantics are similar
370 * to those of malloc().
371 */
372static inline void *pool_alloc_area(size_t size)
373{
374 return malloc(size);
375}
376
377/* frees an area <area> of size <size> allocated by pool_alloc_area(). The
378 * semantics are identical to free() except that the size is specified and
379 * may be ignored.
380 */
381static inline void pool_free_area(void *area, size_t __maybe_unused size)
382{
383 free(area);
384}
385
Willy Tarreau158fa752017-11-22 15:47:29 +0100386#else /* use-after-free detector */
387
388/* allocates an area of size <size> and returns it. The semantics are similar
389 * to those of malloc(). However the allocation is rounded up to 4kB so that a
390 * full page is allocated. This ensures the object can be freed alone so that
391 * future dereferences are easily detected. The returned object is always
Willy Tarreau364d7452018-02-22 14:14:23 +0100392 * 16-bytes aligned to avoid issues with unaligned structure objects. In case
393 * some padding is added, the area's start address is copied at the end of the
394 * padding to help detect underflows.
Willy Tarreau158fa752017-11-22 15:47:29 +0100395 */
Olivier Houchard62975a72018-10-21 01:33:11 +0200396#include <errno.h>
Willy Tarreau158fa752017-11-22 15:47:29 +0100397static inline void *pool_alloc_area(size_t size)
398{
399 size_t pad = (4096 - size) & 0xFF0;
Willy Tarreau5a9cce42018-02-22 11:39:23 +0100400 void *ret;
Willy Tarreau158fa752017-11-22 15:47:29 +0100401
Olivier Houchard62975a72018-10-21 01:33:11 +0200402 ret = mmap(NULL, (size + 4095) & -4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
Willy Tarreau364d7452018-02-22 14:14:23 +0100403 if (ret == MAP_FAILED)
404 return NULL;
405 if (pad >= sizeof(void *))
406 *(void **)(ret + pad - sizeof(void *)) = ret + pad;
407 return ret + pad;
Willy Tarreau158fa752017-11-22 15:47:29 +0100408}
409
410/* frees an area <area> of size <size> allocated by pool_alloc_area(). The
411 * semantics are identical to free() except that the size must absolutely match
Willy Tarreau364d7452018-02-22 14:14:23 +0100412 * the one passed to pool_alloc_area(). In case some padding is added, the
413 * area's start address is compared to the one at the end of the padding, and
414 * a segfault is triggered if they don't match, indicating an underflow.
Willy Tarreau158fa752017-11-22 15:47:29 +0100415 */
416static inline void pool_free_area(void *area, size_t size)
417{
418 size_t pad = (4096 - size) & 0xFF0;
419
Willy Tarreau364d7452018-02-22 14:14:23 +0100420 if (pad >= sizeof(void *) && *(void **)(area - sizeof(void *)) != area)
421 *(volatile int *)0 = 0;
422
Willy Tarreau158fa752017-11-22 15:47:29 +0100423 munmap(area - pad, (size + 4095) & -4096);
424}
425
426#endif /* DEBUG_UAF */
427
Willy Tarreau02622412014-12-08 16:35:23 +0100428/*
429 * Returns a pointer to type <type> taken from the pool <pool_type> or
430 * dynamically allocated. In the first case, <pool_type> is updated to point to
431 * the next element in the list. Memory poisonning is performed if enabled.
432 */
Willy Tarreaubafbe012017-11-24 17:34:44 +0100433static inline void *pool_alloc(struct pool_head *pool)
Willy Tarreau02622412014-12-08 16:35:23 +0100434{
435 void *p;
436
437 p = pool_alloc_dirty(pool);
Willy Tarreaude30a682015-10-28 15:23:51 +0100438 if (p && mem_poison_byte >= 0) {
Willy Tarreau02622412014-12-08 16:35:23 +0100439 memset(p, mem_poison_byte, pool->size);
Willy Tarreaude30a682015-10-28 15:23:51 +0100440 }
441
Willy Tarreau02622412014-12-08 16:35:23 +0100442 return p;
443}
444
445/*
Willy Tarreau50e608d2007-05-13 18:26:08 +0200446 * Puts a memory area back to the corresponding pool.
447 * Items are chained directly through a pointer that
448 * is written in the beginning of the memory area, so
449 * there's no need for any carrier cell. This implies
450 * that each memory area is at least as big as one
Willy Tarreau48d63db2008-08-03 17:41:33 +0200451 * pointer. Just like with the libc's free(), nothing
452 * is done if <ptr> is NULL.
Willy Tarreau50e608d2007-05-13 18:26:08 +0200453 */
Willy Tarreaubafbe012017-11-24 17:34:44 +0100454static inline void pool_free(struct pool_head *pool, void *ptr)
Willy Tarreaue430e772014-12-23 14:13:16 +0100455{
456 if (likely(ptr != NULL)) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100457 HA_SPIN_LOCK(POOL_LOCK, &pool->lock);
Willy Tarreaude30a682015-10-28 15:23:51 +0100458#ifdef DEBUG_MEMORY_POOLS
459 /* we'll get late corruption if we refill to the wrong pool or double-free */
460 if (*POOL_LINK(pool, ptr) != (void *)pool)
461 *(int *)0 = 0;
462#endif
Willy Tarreau158fa752017-11-22 15:47:29 +0100463
464#ifndef DEBUG_UAF /* normal pool behaviour */
Willy Tarreauac421112015-10-28 15:09:29 +0100465 *POOL_LINK(pool, ptr) = (void *)pool->free_list;
Willy Tarreaue430e772014-12-23 14:13:16 +0100466 pool->free_list = (void *)ptr;
Willy Tarreau158fa752017-11-22 15:47:29 +0100467#else /* release the entry for real to detect use after free */
468 /* ensure we crash on double free or free of a const area*/
469 *(uint32_t *)ptr = 0xDEADADD4;
470 pool_free_area(ptr, pool->size + POOL_EXTRA);
471 pool->allocated--;
472#endif /* DEBUG_UAF */
Willy Tarreaue430e772014-12-23 14:13:16 +0100473 pool->used--;
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100474 HA_SPIN_UNLOCK(POOL_LOCK, &pool->lock);
Willy Tarreaue430e772014-12-23 14:13:16 +0100475 }
476}
Willy Tarreauf161d0f2018-02-22 14:05:55 +0100477#endif /* CONFIG_HAP_LOCKLESS_POOLS */
Willy Tarreau2dd0d472006-06-29 17:53:05 +0200478#endif /* _COMMON_MEMORY_H */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200479
480/*
481 * Local variables:
482 * c-indent-level: 8
483 * c-basic-offset: 8
484 * End:
485 */