blob: b854ebb0b28c42cda8a429ac439662da82207d9b [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 Tarreaubaaee002006-06-26 02:48:02 +020030
Willy Tarreau2dd0d472006-06-29 17:53:05 +020031#include <common/config.h>
Willy Tarreau50e608d2007-05-13 18:26:08 +020032#include <common/mini-clist.h>
Christopher Fauletb349e482017-08-29 09:52:38 +020033#include <common/hathreads.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020034
Willy Tarreaua84dcb82015-10-28 12:04:02 +010035#ifndef DEBUG_DONT_SHARE_POOLS
Willy Tarreau50e608d2007-05-13 18:26:08 +020036#define MEM_F_SHARED 0x1
Willy Tarreaua84dcb82015-10-28 12:04:02 +010037#else
38#define MEM_F_SHARED 0
39#endif
Willy Tarreau581bf812016-01-25 02:19:13 +010040#define MEM_F_EXACT 0x2
Willy Tarreau50e608d2007-05-13 18:26:08 +020041
Willy Tarreauac421112015-10-28 15:09:29 +010042/* reserve an extra void* at the end of a pool for linking */
43#ifdef DEBUG_MEMORY_POOLS
44#define POOL_EXTRA (sizeof(void *))
45#define POOL_LINK(pool, item) (void **)(((char *)item) + (pool->size))
46#else
47#define POOL_EXTRA (0)
48#define POOL_LINK(pool, item) ((void **)(item))
49#endif
50
Willy Tarreau0a93b642018-10-16 07:58:39 +020051#define MAX_BASE_POOLS 32
52
Willy Tarreaue18db9e2018-10-16 10:28:54 +020053struct pool_cache_head {
54 struct list list; /* head of objects in this pool */
55 size_t size; /* size of an object */
56 unsigned int count; /* number of objects in this pool */
57};
58
59struct pool_cache_item {
60 struct list by_pool; /* link to objects in this pool */
61 struct list by_lru; /* link to objects by LRU order */
62};
63
64extern THREAD_LOCAL struct pool_cache_head pool_cache[MAX_BASE_POOLS];
65extern THREAD_LOCAL struct list pool_lru_head; /* oldest objects */
66extern THREAD_LOCAL size_t pool_cache_bytes; /* total cache size */
67extern THREAD_LOCAL size_t pool_cache_count; /* #cache objects */
68
Willy Tarreauf161d0f2018-02-22 14:05:55 +010069#ifdef CONFIG_HAP_LOCKLESS_POOLS
Olivier Houchardcf975d42018-01-24 18:38:31 +010070struct pool_free_list {
71 void **free_list;
72 uintptr_t seq;
73};
74#endif
75
Willy Tarreau50e608d2007-05-13 18:26:08 +020076struct pool_head {
Willy Tarreau1ca1b702017-11-26 10:50:36 +010077 void **free_list;
Willy Tarreauf161d0f2018-02-22 14:05:55 +010078#ifdef CONFIG_HAP_LOCKLESS_POOLS
Olivier Houchardcf975d42018-01-24 18:38:31 +010079 uintptr_t seq;
80#else
81 __decl_hathreads(HA_SPINLOCK_T lock); /* the spin lock */
82#endif
Willy Tarreau50e608d2007-05-13 18:26:08 +020083 unsigned int used; /* how many chunks are currently in use */
84 unsigned int allocated; /* how many chunks have been allocated */
85 unsigned int limit; /* hard limit on the number of chunks */
86 unsigned int minavail; /* how many chunks are expected to be used */
87 unsigned int size; /* chunk size */
88 unsigned int flags; /* MEM_F_* */
Willy Tarreau7dcd46d2007-05-14 00:16:13 +020089 unsigned int users; /* number of pools sharing this zone */
Willy Tarreau58102cf2015-10-28 16:24:21 +010090 unsigned int failed; /* failed allocations */
Olivier Houchardcf975d42018-01-24 18:38:31 +010091 struct list list; /* list of all known pools */
Willy Tarreau7dcd46d2007-05-14 00:16:13 +020092 char name[12]; /* name of the pool */
Willy Tarreau1ca1b702017-11-26 10:50:36 +010093} __attribute__((aligned(64)));
Willy Tarreau50e608d2007-05-13 18:26:08 +020094
Willy Tarreau0a93b642018-10-16 07:58:39 +020095
96extern struct pool_head pool_base_start[MAX_BASE_POOLS];
97extern unsigned int pool_base_count;
98
Willy Tarreau067ac9f2015-10-08 14:12:13 +020099/* poison each newly allocated area with this byte if >= 0 */
100extern int mem_poison_byte;
Willy Tarreau50e608d2007-05-13 18:26:08 +0200101
Willy Tarreaua885f6d2014-12-03 15:25:28 +0100102/* Allocates new entries for pool <pool> until there are at least <avail> + 1
103 * available, then returns the last one for immediate use, so that at least
104 * <avail> are left available in the pool upon return. NULL is returned if the
105 * last entry could not be allocated. It's important to note that at least one
106 * allocation is always performed even if there are enough entries in the pool.
107 * A call to the garbage collector is performed at most once in case malloc()
108 * returns an error, before returning NULL.
Willy Tarreau50e608d2007-05-13 18:26:08 +0200109 */
Christopher Fauletb349e482017-08-29 09:52:38 +0200110void *__pool_refill_alloc(struct pool_head *pool, unsigned int avail);
Willy Tarreaua885f6d2014-12-03 15:25:28 +0100111void *pool_refill_alloc(struct pool_head *pool, unsigned int avail);
Willy Tarreau50e608d2007-05-13 18:26:08 +0200112
113/* Try to find an existing shared pool with the same characteristics and
114 * returns it, otherwise creates this one. NULL is returned if no memory
115 * is available for a new creation.
116 */
117struct pool_head *create_pool(char *name, unsigned int size, unsigned int flags);
118
119/* Dump statistics on pools usage.
120 */
Willy Tarreau12833bb2014-01-28 16:49:56 +0100121void dump_pools_to_trash();
Willy Tarreau50e608d2007-05-13 18:26:08 +0200122void dump_pools(void);
Willy Tarreau58102cf2015-10-28 16:24:21 +0100123int pool_total_failures();
124unsigned long pool_total_allocated();
125unsigned long pool_total_used();
Willy Tarreau50e608d2007-05-13 18:26:08 +0200126
127/*
Willy Tarreaue6ce59d2007-05-13 19:38:49 +0200128 * This function frees whatever can be freed in pool <pool>.
129 */
Willy Tarreaubafbe012017-11-24 17:34:44 +0100130void pool_flush(struct pool_head *pool);
Willy Tarreaue6ce59d2007-05-13 19:38:49 +0200131
132/*
133 * This function frees whatever can be freed in all pools, but respecting
134 * the minimum thresholds imposed by owners.
Christopher Fauletb349e482017-08-29 09:52:38 +0200135 *
Willy Tarreaubafbe012017-11-24 17:34:44 +0100136 * <pool_ctx> is used when pool_gc is called to release resources to allocate
Christopher Fauletb349e482017-08-29 09:52:38 +0200137 * an element in __pool_refill_alloc. It is important because <pool_ctx> is
138 * already locked, so we need to skip the lock here.
Willy Tarreaue6ce59d2007-05-13 19:38:49 +0200139 */
Willy Tarreaubafbe012017-11-24 17:34:44 +0100140void pool_gc(struct pool_head *pool_ctx);
Willy Tarreaue6ce59d2007-05-13 19:38:49 +0200141
142/*
143 * This function destroys a pull by freeing it completely.
144 * This should be called only under extreme circumstances.
145 */
Willy Tarreaubafbe012017-11-24 17:34:44 +0100146void *pool_destroy(struct pool_head *pool);
Willy Tarreaue6ce59d2007-05-13 19:38:49 +0200147
Willy Tarreau0a93b642018-10-16 07:58:39 +0200148/* returns the pool index for pool <pool>, or -1 if this pool has no index */
149static inline ssize_t pool_get_index(const struct pool_head *pool)
150{
151 size_t idx;
152
153 idx = pool - pool_base_start;
154 if (idx >= MAX_BASE_POOLS)
155 return -1;
156 return idx;
157}
158
Willy Tarreauf161d0f2018-02-22 14:05:55 +0100159#ifdef CONFIG_HAP_LOCKLESS_POOLS
Willy Tarreaue18db9e2018-10-16 10:28:54 +0200160
161/* Tries to retrieve an object from the local pool cache corresponding to pool
162 * <pool>. Returns NULL if none is available.
163 */
164static inline void *__pool_get_from_cache(struct pool_head *pool)
165{
166 ssize_t idx = pool_get_index(pool);
167 struct pool_cache_item *item;
168
169 /* pool not in cache */
170 if (idx < 0)
171 return NULL;
172
173 /* never allocated or empty */
174 if (pool_cache[idx].list.n == NULL || LIST_ISEMPTY(&pool_cache[idx].list))
175 return NULL;
176
177 item = LIST_NEXT(&pool_cache[idx].list, typeof(item), by_pool);
178 pool_cache[idx].count--;
179 pool_cache_bytes -= pool_cache[idx].size;
180 pool_cache_count--;
181 LIST_DEL(&item->by_pool);
182 LIST_DEL(&item->by_lru);
Willy Tarreau8e9f4532018-10-28 20:09:12 +0100183#ifdef DEBUG_MEMORY_POOLS
184 /* keep track of where the element was allocated from */
185 *POOL_LINK(pool, item) = (void *)pool;
186#endif
Willy Tarreaue18db9e2018-10-16 10:28:54 +0200187 return item;
188}
189
Olivier Houchardcf975d42018-01-24 18:38:31 +0100190/*
191 * Returns a pointer to type <type> taken from the pool <pool_type> if
192 * available, otherwise returns NULL. No malloc() is attempted, and poisonning
193 * is never performed. The purpose is to get the fastest possible allocation.
194 */
195static inline void *__pool_get_first(struct pool_head *pool)
196{
197 struct pool_free_list cmp, new;
Willy Tarreaue18db9e2018-10-16 10:28:54 +0200198 void *ret = __pool_get_from_cache(pool);
199
200 if (ret)
201 return ret;
Olivier Houchardcf975d42018-01-24 18:38:31 +0100202
203 cmp.seq = pool->seq;
204 __ha_barrier_load();
205
206 cmp.free_list = pool->free_list;
207 do {
208 if (cmp.free_list == NULL)
209 return NULL;
210 new.seq = cmp.seq + 1;
211 __ha_barrier_load();
212 new.free_list = *POOL_LINK(pool, cmp.free_list);
213 } while (__ha_cas_dw((void *)&pool->free_list, (void *)&cmp, (void *)&new) == 0);
Tim Duesterhus05f6a432018-02-20 00:49:46 +0100214
Olivier Houchardcf975d42018-01-24 18:38:31 +0100215 HA_ATOMIC_ADD(&pool->used, 1);
216#ifdef DEBUG_MEMORY_POOLS
217 /* keep track of where the element was allocated from */
218 *POOL_LINK(pool, cmp.free_list) = (void *)pool;
219#endif
220 return cmp.free_list;
221}
222
223static inline void *pool_get_first(struct pool_head *pool)
224{
225 void *ret;
226
227 ret = __pool_get_first(pool);
228 return ret;
229}
230/*
231 * Returns a pointer to type <type> taken from the pool <pool_type> or
232 * dynamically allocated. In the first case, <pool_type> is updated to point to
233 * the next element in the list. No memory poisonning is ever performed on the
234 * returned area.
235 */
236static inline void *pool_alloc_dirty(struct pool_head *pool)
237{
238 void *p;
239
240 if ((p = __pool_get_first(pool)) == NULL)
241 p = __pool_refill_alloc(pool, 0);
242 return p;
243}
244
Willy Tarreaue6ce59d2007-05-13 19:38:49 +0200245/*
Olivier Houchardcf975d42018-01-24 18:38:31 +0100246 * Returns a pointer to type <type> taken from the pool <pool_type> or
247 * dynamically allocated. In the first case, <pool_type> is updated to point to
248 * the next element in the list. Memory poisonning is performed if enabled.
249 */
250static inline void *pool_alloc(struct pool_head *pool)
251{
252 void *p;
253
254 p = pool_alloc_dirty(pool);
Olivier Houchardcf975d42018-01-24 18:38:31 +0100255 if (p && mem_poison_byte >= 0) {
256 memset(p, mem_poison_byte, pool->size);
257 }
258
259 return p;
260}
261
Willy Tarreau146794d2018-10-16 08:55:15 +0200262/* Locklessly add item <ptr> to pool <pool>, then update the pool used count.
263 * Both the pool and the pointer must be valid. Use pool_free() for normal
264 * operations.
265 */
266static inline void __pool_free(struct pool_head *pool, void *ptr)
267{
Willy Tarreau7a6ad882018-10-20 17:37:38 +0200268 void **free_list = pool->free_list;
Willy Tarreau146794d2018-10-16 08:55:15 +0200269
270 do {
271 *POOL_LINK(pool, ptr) = (void *)free_list;
272 __ha_barrier_store();
Willy Tarreau7a6ad882018-10-20 17:37:38 +0200273 } while (!HA_ATOMIC_CAS(&pool->free_list, &free_list, ptr));
Willy Tarreau146794d2018-10-16 08:55:15 +0200274 HA_ATOMIC_SUB(&pool->used, 1);
275}
276
Willy Tarreaue18db9e2018-10-16 10:28:54 +0200277/* frees an object to the local cache, possibly pushing oldest objects to the
278 * global pool.
279 */
280void __pool_put_to_cache(struct pool_head *pool, void *ptr, ssize_t idx);
281static inline void pool_put_to_cache(struct pool_head *pool, void *ptr)
282{
283 ssize_t idx = pool_get_index(pool);
284
285 /* pool not in cache or too many objects for this pool (more than
286 * half of the cache is used and this pool uses more than 1/8 of
287 * the cache size).
288 */
289 if (idx < 0 ||
290 (pool_cache_bytes > CONFIG_HAP_POOL_CACHE_SIZE * 3 / 4 &&
291 pool_cache[idx].count >= 16 + pool_cache_count / 8)) {
292 __pool_free(pool, ptr);
293 return;
294 }
295 __pool_put_to_cache(pool, ptr, idx);
296}
297
Olivier Houchardcf975d42018-01-24 18:38:31 +0100298/*
299 * Puts a memory area back to the corresponding pool.
300 * Items are chained directly through a pointer that
301 * is written in the beginning of the memory area, so
302 * there's no need for any carrier cell. This implies
303 * that each memory area is at least as big as one
304 * pointer. Just like with the libc's free(), nothing
305 * is done if <ptr> is NULL.
306 */
307static inline void pool_free(struct pool_head *pool, void *ptr)
308{
309 if (likely(ptr != NULL)) {
Olivier Houchardcf975d42018-01-24 18:38:31 +0100310#ifdef DEBUG_MEMORY_POOLS
311 /* we'll get late corruption if we refill to the wrong pool or double-free */
312 if (*POOL_LINK(pool, ptr) != (void *)pool)
313 *(volatile int *)0 = 0;
314#endif
Willy Tarreaue18db9e2018-10-16 10:28:54 +0200315 pool_put_to_cache(pool, ptr);
Olivier Houchardcf975d42018-01-24 18:38:31 +0100316 }
317}
318
Willy Tarreauf161d0f2018-02-22 14:05:55 +0100319#else /* CONFIG_HAP_LOCKLESS_POOLS */
Olivier Houchardcf975d42018-01-24 18:38:31 +0100320/*
Willy Tarreau02622412014-12-08 16:35:23 +0100321 * Returns a pointer to type <type> taken from the pool <pool_type> if
322 * available, otherwise returns NULL. No malloc() is attempted, and poisonning
323 * is never performed. The purpose is to get the fastest possible allocation.
Willy Tarreau50e608d2007-05-13 18:26:08 +0200324 */
Christopher Fauletb349e482017-08-29 09:52:38 +0200325static inline void *__pool_get_first(struct pool_head *pool)
Willy Tarreaue430e772014-12-23 14:13:16 +0100326{
327 void *p;
328
Willy Tarreau02622412014-12-08 16:35:23 +0100329 if ((p = pool->free_list) != NULL) {
Willy Tarreauac421112015-10-28 15:09:29 +0100330 pool->free_list = *POOL_LINK(pool, p);
Willy Tarreaue430e772014-12-23 14:13:16 +0100331 pool->used++;
Willy Tarreaude30a682015-10-28 15:23:51 +0100332#ifdef DEBUG_MEMORY_POOLS
333 /* keep track of where the element was allocated from */
334 *POOL_LINK(pool, p) = (void *)pool;
335#endif
Willy Tarreaue430e772014-12-23 14:13:16 +0100336 }
337 return p;
338}
Willy Tarreau50e608d2007-05-13 18:26:08 +0200339
Christopher Fauletb349e482017-08-29 09:52:38 +0200340static inline void *pool_get_first(struct pool_head *pool)
341{
342 void *ret;
343
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100344 HA_SPIN_LOCK(POOL_LOCK, &pool->lock);
Christopher Fauletb349e482017-08-29 09:52:38 +0200345 ret = __pool_get_first(pool);
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100346 HA_SPIN_UNLOCK(POOL_LOCK, &pool->lock);
Christopher Fauletb349e482017-08-29 09:52:38 +0200347 return ret;
348}
Willy Tarreau50e608d2007-05-13 18:26:08 +0200349/*
Willy Tarreau02622412014-12-08 16:35:23 +0100350 * Returns a pointer to type <type> taken from the pool <pool_type> or
351 * dynamically allocated. In the first case, <pool_type> is updated to point to
352 * the next element in the list. No memory poisonning is ever performed on the
353 * returned area.
354 */
355static inline void *pool_alloc_dirty(struct pool_head *pool)
356{
357 void *p;
358
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100359 HA_SPIN_LOCK(POOL_LOCK, &pool->lock);
Christopher Fauletb349e482017-08-29 09:52:38 +0200360 if ((p = __pool_get_first(pool)) == NULL)
361 p = __pool_refill_alloc(pool, 0);
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100362 HA_SPIN_UNLOCK(POOL_LOCK, &pool->lock);
Willy Tarreau02622412014-12-08 16:35:23 +0100363 return p;
364}
365
Willy Tarreau158fa752017-11-22 15:47:29 +0100366#ifndef DEBUG_UAF /* normal allocator */
367
Willy Tarreauf13322e2017-11-22 10:50:54 +0100368/* allocates an area of size <size> and returns it. The semantics are similar
369 * to those of malloc().
370 */
371static inline void *pool_alloc_area(size_t size)
372{
373 return malloc(size);
374}
375
376/* frees an area <area> of size <size> allocated by pool_alloc_area(). The
377 * semantics are identical to free() except that the size is specified and
378 * may be ignored.
379 */
380static inline void pool_free_area(void *area, size_t __maybe_unused size)
381{
382 free(area);
383}
384
Willy Tarreau158fa752017-11-22 15:47:29 +0100385#else /* use-after-free detector */
386
387/* allocates an area of size <size> and returns it. The semantics are similar
388 * to those of malloc(). However the allocation is rounded up to 4kB so that a
389 * full page is allocated. This ensures the object can be freed alone so that
390 * future dereferences are easily detected. The returned object is always
Willy Tarreau364d7452018-02-22 14:14:23 +0100391 * 16-bytes aligned to avoid issues with unaligned structure objects. In case
392 * some padding is added, the area's start address is copied at the end of the
393 * padding to help detect underflows.
Willy Tarreau158fa752017-11-22 15:47:29 +0100394 */
Olivier Houchard62975a72018-10-21 01:33:11 +0200395#include <errno.h>
Willy Tarreau158fa752017-11-22 15:47:29 +0100396static inline void *pool_alloc_area(size_t size)
397{
398 size_t pad = (4096 - size) & 0xFF0;
Willy Tarreau5a9cce42018-02-22 11:39:23 +0100399 void *ret;
Willy Tarreau158fa752017-11-22 15:47:29 +0100400
Olivier Houchard62975a72018-10-21 01:33:11 +0200401 ret = mmap(NULL, (size + 4095) & -4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
Willy Tarreau364d7452018-02-22 14:14:23 +0100402 if (ret == MAP_FAILED)
403 return NULL;
404 if (pad >= sizeof(void *))
405 *(void **)(ret + pad - sizeof(void *)) = ret + pad;
406 return ret + pad;
Willy Tarreau158fa752017-11-22 15:47:29 +0100407}
408
409/* frees an area <area> of size <size> allocated by pool_alloc_area(). The
410 * semantics are identical to free() except that the size must absolutely match
Willy Tarreau364d7452018-02-22 14:14:23 +0100411 * the one passed to pool_alloc_area(). In case some padding is added, the
412 * area's start address is compared to the one at the end of the padding, and
413 * a segfault is triggered if they don't match, indicating an underflow.
Willy Tarreau158fa752017-11-22 15:47:29 +0100414 */
415static inline void pool_free_area(void *area, size_t size)
416{
417 size_t pad = (4096 - size) & 0xFF0;
418
Willy Tarreau364d7452018-02-22 14:14:23 +0100419 if (pad >= sizeof(void *) && *(void **)(area - sizeof(void *)) != area)
420 *(volatile int *)0 = 0;
421
Willy Tarreau158fa752017-11-22 15:47:29 +0100422 munmap(area - pad, (size + 4095) & -4096);
423}
424
425#endif /* DEBUG_UAF */
426
Willy Tarreau02622412014-12-08 16:35:23 +0100427/*
428 * Returns a pointer to type <type> taken from the pool <pool_type> or
429 * dynamically allocated. In the first case, <pool_type> is updated to point to
430 * the next element in the list. Memory poisonning is performed if enabled.
431 */
Willy Tarreaubafbe012017-11-24 17:34:44 +0100432static inline void *pool_alloc(struct pool_head *pool)
Willy Tarreau02622412014-12-08 16:35:23 +0100433{
434 void *p;
435
436 p = pool_alloc_dirty(pool);
Willy Tarreaude30a682015-10-28 15:23:51 +0100437 if (p && mem_poison_byte >= 0) {
Willy Tarreau02622412014-12-08 16:35:23 +0100438 memset(p, mem_poison_byte, pool->size);
Willy Tarreaude30a682015-10-28 15:23:51 +0100439 }
440
Willy Tarreau02622412014-12-08 16:35:23 +0100441 return p;
442}
443
444/*
Willy Tarreau50e608d2007-05-13 18:26:08 +0200445 * Puts a memory area back to the corresponding pool.
446 * Items are chained directly through a pointer that
447 * is written in the beginning of the memory area, so
448 * there's no need for any carrier cell. This implies
449 * that each memory area is at least as big as one
Willy Tarreau48d63db2008-08-03 17:41:33 +0200450 * pointer. Just like with the libc's free(), nothing
451 * is done if <ptr> is NULL.
Willy Tarreau50e608d2007-05-13 18:26:08 +0200452 */
Willy Tarreaubafbe012017-11-24 17:34:44 +0100453static inline void pool_free(struct pool_head *pool, void *ptr)
Willy Tarreaue430e772014-12-23 14:13:16 +0100454{
455 if (likely(ptr != NULL)) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100456 HA_SPIN_LOCK(POOL_LOCK, &pool->lock);
Willy Tarreaude30a682015-10-28 15:23:51 +0100457#ifdef DEBUG_MEMORY_POOLS
458 /* we'll get late corruption if we refill to the wrong pool or double-free */
459 if (*POOL_LINK(pool, ptr) != (void *)pool)
460 *(int *)0 = 0;
461#endif
Willy Tarreau158fa752017-11-22 15:47:29 +0100462
463#ifndef DEBUG_UAF /* normal pool behaviour */
Willy Tarreauac421112015-10-28 15:09:29 +0100464 *POOL_LINK(pool, ptr) = (void *)pool->free_list;
Willy Tarreaue430e772014-12-23 14:13:16 +0100465 pool->free_list = (void *)ptr;
Willy Tarreau158fa752017-11-22 15:47:29 +0100466#else /* release the entry for real to detect use after free */
467 /* ensure we crash on double free or free of a const area*/
468 *(uint32_t *)ptr = 0xDEADADD4;
469 pool_free_area(ptr, pool->size + POOL_EXTRA);
470 pool->allocated--;
471#endif /* DEBUG_UAF */
Willy Tarreaue430e772014-12-23 14:13:16 +0100472 pool->used--;
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100473 HA_SPIN_UNLOCK(POOL_LOCK, &pool->lock);
Willy Tarreaue430e772014-12-23 14:13:16 +0100474 }
475}
Willy Tarreauf161d0f2018-02-22 14:05:55 +0100476#endif /* CONFIG_HAP_LOCKLESS_POOLS */
Willy Tarreau2dd0d472006-06-29 17:53:05 +0200477#endif /* _COMMON_MEMORY_H */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200478
479/*
480 * Local variables:
481 * c-indent-level: 8
482 * c-basic-offset: 8
483 * End:
484 */