blob: d3950ca5df2c739d9bbcc68628e3e4462733ace2 [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 Tarreau21072b92020-05-29 17:23:05 +020084/* Note below, in case of lockless pools, we still need the lock only for
85 * the flush() operation.
86 */
Willy Tarreau50e608d2007-05-13 18:26:08 +020087struct pool_head {
Willy Tarreau1ca1b702017-11-26 10:50:36 +010088 void **free_list;
Willy Tarreauf161d0f2018-02-22 14:05:55 +010089#ifdef CONFIG_HAP_LOCKLESS_POOLS
Olivier Houchardcf975d42018-01-24 18:38:31 +010090 uintptr_t seq;
Olivier Houchardcf975d42018-01-24 18:38:31 +010091#endif
Willy Tarreau21072b92020-05-29 17:23:05 +020092 __decl_hathreads(HA_SPINLOCK_T lock); /* the spin lock */
Willy Tarreau50e608d2007-05-13 18:26:08 +020093 unsigned int used; /* how many chunks are currently in use */
Willy Tarreaua1e4f8c2020-05-08 08:31:56 +020094 unsigned int needed_avg;/* floating indicator between used and allocated */
Willy Tarreau50e608d2007-05-13 18:26:08 +020095 unsigned int allocated; /* how many chunks have been allocated */
96 unsigned int limit; /* hard limit on the number of chunks */
97 unsigned int minavail; /* how many chunks are expected to be used */
98 unsigned int size; /* chunk size */
99 unsigned int flags; /* MEM_F_* */
Willy Tarreau7dcd46d2007-05-14 00:16:13 +0200100 unsigned int users; /* number of pools sharing this zone */
Willy Tarreau58102cf2015-10-28 16:24:21 +0100101 unsigned int failed; /* failed allocations */
Olivier Houchardcf975d42018-01-24 18:38:31 +0100102 struct list list; /* list of all known pools */
Willy Tarreau7dcd46d2007-05-14 00:16:13 +0200103 char name[12]; /* name of the pool */
Willy Tarreau1ca1b702017-11-26 10:50:36 +0100104} __attribute__((aligned(64)));
Willy Tarreau50e608d2007-05-13 18:26:08 +0200105
Willy Tarreau0a93b642018-10-16 07:58:39 +0200106
107extern struct pool_head pool_base_start[MAX_BASE_POOLS];
108extern unsigned int pool_base_count;
109
Willy Tarreau067ac9f2015-10-08 14:12:13 +0200110/* poison each newly allocated area with this byte if >= 0 */
111extern int mem_poison_byte;
Willy Tarreau50e608d2007-05-13 18:26:08 +0200112
Willy Tarreaua885f6d2014-12-03 15:25:28 +0100113/* Allocates new entries for pool <pool> until there are at least <avail> + 1
114 * available, then returns the last one for immediate use, so that at least
115 * <avail> are left available in the pool upon return. NULL is returned if the
116 * last entry could not be allocated. It's important to note that at least one
117 * allocation is always performed even if there are enough entries in the pool.
118 * A call to the garbage collector is performed at most once in case malloc()
119 * returns an error, before returning NULL.
Willy Tarreau50e608d2007-05-13 18:26:08 +0200120 */
Christopher Fauletb349e482017-08-29 09:52:38 +0200121void *__pool_refill_alloc(struct pool_head *pool, unsigned int avail);
Willy Tarreaua885f6d2014-12-03 15:25:28 +0100122void *pool_refill_alloc(struct pool_head *pool, unsigned int avail);
Willy Tarreau50e608d2007-05-13 18:26:08 +0200123
124/* Try to find an existing shared pool with the same characteristics and
125 * returns it, otherwise creates this one. NULL is returned if no memory
126 * is available for a new creation.
127 */
128struct pool_head *create_pool(char *name, unsigned int size, unsigned int flags);
Willy Tarreau7107c8b2018-11-26 11:44:35 +0100129void create_pool_callback(struct pool_head **ptr, char *name, unsigned int size);
130
131/* This registers a call to create_pool_callback(ptr, name, size) */
132#define REGISTER_POOL(ptr, name, size) \
133 INITCALL3(STG_POOL, create_pool_callback, (ptr), (name), (size))
134
135/* This macro declares a pool head <ptr> and registers its creation */
136#define DECLARE_POOL(ptr, name, size) \
137 struct pool_head *(ptr) = NULL; \
138 REGISTER_POOL(&ptr, name, size)
139
140/* This macro declares a static pool head <ptr> and registers its creation */
141#define DECLARE_STATIC_POOL(ptr, name, size) \
142 static struct pool_head *(ptr); \
143 REGISTER_POOL(&ptr, name, size)
Willy Tarreau50e608d2007-05-13 18:26:08 +0200144
145/* Dump statistics on pools usage.
146 */
Willy Tarreau12833bb2014-01-28 16:49:56 +0100147void dump_pools_to_trash();
Willy Tarreau50e608d2007-05-13 18:26:08 +0200148void dump_pools(void);
Willy Tarreau58102cf2015-10-28 16:24:21 +0100149int pool_total_failures();
150unsigned long pool_total_allocated();
151unsigned long pool_total_used();
Willy Tarreau50e608d2007-05-13 18:26:08 +0200152
153/*
Willy Tarreaue6ce59d2007-05-13 19:38:49 +0200154 * This function frees whatever can be freed in pool <pool>.
155 */
Willy Tarreaubafbe012017-11-24 17:34:44 +0100156void pool_flush(struct pool_head *pool);
Willy Tarreaue6ce59d2007-05-13 19:38:49 +0200157
158/*
159 * This function frees whatever can be freed in all pools, but respecting
160 * the minimum thresholds imposed by owners.
Christopher Fauletb349e482017-08-29 09:52:38 +0200161 *
Willy Tarreaubafbe012017-11-24 17:34:44 +0100162 * <pool_ctx> is used when pool_gc is called to release resources to allocate
Christopher Fauletb349e482017-08-29 09:52:38 +0200163 * an element in __pool_refill_alloc. It is important because <pool_ctx> is
164 * already locked, so we need to skip the lock here.
Willy Tarreaue6ce59d2007-05-13 19:38:49 +0200165 */
Willy Tarreaubafbe012017-11-24 17:34:44 +0100166void pool_gc(struct pool_head *pool_ctx);
Willy Tarreaue6ce59d2007-05-13 19:38:49 +0200167
168/*
169 * This function destroys a pull by freeing it completely.
170 * This should be called only under extreme circumstances.
171 */
Willy Tarreaubafbe012017-11-24 17:34:44 +0100172void *pool_destroy(struct pool_head *pool);
Willy Tarreau2455ceb2018-11-26 15:57:34 +0100173void pool_destroy_all();
Willy Tarreaue6ce59d2007-05-13 19:38:49 +0200174
Willy Tarreau0a93b642018-10-16 07:58:39 +0200175/* returns the pool index for pool <pool>, or -1 if this pool has no index */
176static inline ssize_t pool_get_index(const struct pool_head *pool)
177{
178 size_t idx;
179
180 idx = pool - pool_base_start;
181 if (idx >= MAX_BASE_POOLS)
182 return -1;
183 return idx;
184}
185
Willy Tarreaua1e4f8c2020-05-08 08:31:56 +0200186/* The two functions below were copied from freq_ctr.h's swrate_add, impossible
187 * to use here due to include dependency hell again!
188 */
189#define POOL_AVG_SAMPLES 1024
190
191static inline unsigned int pool_avg_add(unsigned int *sum, unsigned int v)
192{
193 unsigned int new_sum, old_sum;
194 unsigned int n = POOL_AVG_SAMPLES;
195
196 old_sum = *sum;
197 do {
198 new_sum = old_sum - (old_sum + n - 1) / n + v;
199 } while (!_HA_ATOMIC_CAS(sum, &old_sum, new_sum));
200 return new_sum;
201}
202
203/* make the new value <v> count for 1/4 of the total sum */
204static inline unsigned int pool_avg_bump(unsigned int *sum, unsigned int v)
205{
206 unsigned int new_sum, old_sum;
207 unsigned int n = POOL_AVG_SAMPLES;
208
209 old_sum = *sum;
210 do {
211 new_sum = old_sum - (old_sum + 3) / 4;
212 new_sum += (n * v + 3) / 4;
213 } while (!_HA_ATOMIC_CAS(sum, &old_sum, new_sum));
214 return new_sum;
215}
216
217static inline unsigned int pool_avg(unsigned int sum)
218{
219 unsigned int n = POOL_AVG_SAMPLES;
220
221 return (sum + n - 1) / n;
222}
223
Willy Tarreau63a87382020-05-08 08:38:24 +0200224/* returns true if the pool is considered to have too many free objects */
225static inline int pool_is_crowded(const struct pool_head *pool)
226{
227 return pool->allocated >= pool_avg(pool->needed_avg + pool->needed_avg / 4) &&
228 (int)(pool->allocated - pool->used) >= pool->minavail;
229}
230
Willy Tarreauf161d0f2018-02-22 14:05:55 +0100231#ifdef CONFIG_HAP_LOCKLESS_POOLS
Willy Tarreaue18db9e2018-10-16 10:28:54 +0200232
233/* Tries to retrieve an object from the local pool cache corresponding to pool
234 * <pool>. Returns NULL if none is available.
235 */
236static inline void *__pool_get_from_cache(struct pool_head *pool)
237{
238 ssize_t idx = pool_get_index(pool);
239 struct pool_cache_item *item;
Willy Tarreau7f0165e2018-11-26 17:09:46 +0100240 struct pool_cache_head *ph;
Willy Tarreaue18db9e2018-10-16 10:28:54 +0200241
242 /* pool not in cache */
243 if (idx < 0)
244 return NULL;
245
Willy Tarreau7f0165e2018-11-26 17:09:46 +0100246 ph = &pool_cache[tid][idx];
247 if (LIST_ISEMPTY(&ph->list))
248 return NULL; // empty
Willy Tarreaue18db9e2018-10-16 10:28:54 +0200249
Willy Tarreau7f0165e2018-11-26 17:09:46 +0100250 item = LIST_NEXT(&ph->list, typeof(item), by_pool);
251 ph->count--;
252 pool_cache_bytes -= ph->size;
Willy Tarreaue18db9e2018-10-16 10:28:54 +0200253 pool_cache_count--;
254 LIST_DEL(&item->by_pool);
255 LIST_DEL(&item->by_lru);
Willy Tarreau8e9f4532018-10-28 20:09:12 +0100256#ifdef DEBUG_MEMORY_POOLS
257 /* keep track of where the element was allocated from */
258 *POOL_LINK(pool, item) = (void *)pool;
259#endif
Willy Tarreaue18db9e2018-10-16 10:28:54 +0200260 return item;
261}
262
Olivier Houchardcf975d42018-01-24 18:38:31 +0100263/*
264 * Returns a pointer to type <type> taken from the pool <pool_type> if
265 * available, otherwise returns NULL. No malloc() is attempted, and poisonning
266 * is never performed. The purpose is to get the fastest possible allocation.
267 */
268static inline void *__pool_get_first(struct pool_head *pool)
269{
270 struct pool_free_list cmp, new;
Willy Tarreaue18db9e2018-10-16 10:28:54 +0200271 void *ret = __pool_get_from_cache(pool);
272
273 if (ret)
274 return ret;
Olivier Houchardcf975d42018-01-24 18:38:31 +0100275
276 cmp.seq = pool->seq;
277 __ha_barrier_load();
278
279 cmp.free_list = pool->free_list;
280 do {
Olivier Houchard899fb8a2020-03-18 15:48:29 +0100281 if (cmp.free_list == NULL)
Olivier Houchardcf975d42018-01-24 18:38:31 +0100282 return NULL;
283 new.seq = cmp.seq + 1;
284 __ha_barrier_load();
285 new.free_list = *POOL_LINK(pool, cmp.free_list);
Willy Tarreau6a38b322019-05-11 18:04:24 +0200286 } while (HA_ATOMIC_DWCAS((void *)&pool->free_list, (void *)&cmp, (void *)&new) == 0);
Olivier Houchard20872762019-03-08 18:53:35 +0100287 __ha_barrier_atomic_store();
Tim Duesterhus05f6a432018-02-20 00:49:46 +0100288
Olivier Houchard20872762019-03-08 18:53:35 +0100289 _HA_ATOMIC_ADD(&pool->used, 1);
Olivier Houchardcf975d42018-01-24 18:38:31 +0100290#ifdef DEBUG_MEMORY_POOLS
291 /* keep track of where the element was allocated from */
292 *POOL_LINK(pool, cmp.free_list) = (void *)pool;
293#endif
294 return cmp.free_list;
295}
296
297static inline void *pool_get_first(struct pool_head *pool)
298{
299 void *ret;
300
301 ret = __pool_get_first(pool);
302 return ret;
303}
304/*
305 * Returns a pointer to type <type> taken from the pool <pool_type> or
306 * dynamically allocated. In the first case, <pool_type> is updated to point to
307 * the next element in the list. No memory poisonning is ever performed on the
308 * returned area.
309 */
310static inline void *pool_alloc_dirty(struct pool_head *pool)
311{
312 void *p;
313
314 if ((p = __pool_get_first(pool)) == NULL)
315 p = __pool_refill_alloc(pool, 0);
316 return p;
317}
318
Willy Tarreaue6ce59d2007-05-13 19:38:49 +0200319/*
Olivier Houchardcf975d42018-01-24 18:38:31 +0100320 * Returns a pointer to type <type> taken from the pool <pool_type> or
321 * dynamically allocated. In the first case, <pool_type> is updated to point to
322 * the next element in the list. Memory poisonning is performed if enabled.
323 */
324static inline void *pool_alloc(struct pool_head *pool)
325{
326 void *p;
327
328 p = pool_alloc_dirty(pool);
Olivier Houchardcf975d42018-01-24 18:38:31 +0100329 if (p && mem_poison_byte >= 0) {
330 memset(p, mem_poison_byte, pool->size);
331 }
332
333 return p;
334}
335
Willy Tarreau146794d2018-10-16 08:55:15 +0200336/* Locklessly add item <ptr> to pool <pool>, then update the pool used count.
337 * Both the pool and the pointer must be valid. Use pool_free() for normal
338 * operations.
339 */
340static inline void __pool_free(struct pool_head *pool, void *ptr)
341{
Willy Tarreau7a6ad882018-10-20 17:37:38 +0200342 void **free_list = pool->free_list;
Willy Tarreau146794d2018-10-16 08:55:15 +0200343
Olivier Houchard20872762019-03-08 18:53:35 +0100344 _HA_ATOMIC_SUB(&pool->used, 1);
Willy Tarreau63a87382020-05-08 08:38:24 +0200345
346 if (unlikely(pool_is_crowded(pool))) {
347 free(ptr);
348 _HA_ATOMIC_SUB(&pool->allocated, 1);
349 } else {
350 do {
351 *POOL_LINK(pool, ptr) = (void *)free_list;
352 __ha_barrier_store();
353 } while (!_HA_ATOMIC_CAS(&pool->free_list, &free_list, ptr));
354 __ha_barrier_atomic_store();
355 }
Willy Tarreaua1e4f8c2020-05-08 08:31:56 +0200356 pool_avg_add(&pool->needed_avg, pool->used);
Willy Tarreau146794d2018-10-16 08:55:15 +0200357}
358
Willy Tarreaue18db9e2018-10-16 10:28:54 +0200359/* frees an object to the local cache, possibly pushing oldest objects to the
360 * global pool.
361 */
362void __pool_put_to_cache(struct pool_head *pool, void *ptr, ssize_t idx);
363static inline void pool_put_to_cache(struct pool_head *pool, void *ptr)
364{
365 ssize_t idx = pool_get_index(pool);
366
367 /* pool not in cache or too many objects for this pool (more than
368 * half of the cache is used and this pool uses more than 1/8 of
369 * the cache size).
370 */
371 if (idx < 0 ||
372 (pool_cache_bytes > CONFIG_HAP_POOL_CACHE_SIZE * 3 / 4 &&
Willy Tarreau7f0165e2018-11-26 17:09:46 +0100373 pool_cache[tid][idx].count >= 16 + pool_cache_count / 8)) {
Willy Tarreaue18db9e2018-10-16 10:28:54 +0200374 __pool_free(pool, ptr);
375 return;
376 }
377 __pool_put_to_cache(pool, ptr, idx);
378}
379
Olivier Houchardcf975d42018-01-24 18:38:31 +0100380/*
381 * Puts a memory area back to the corresponding pool.
382 * Items are chained directly through a pointer that
383 * is written in the beginning of the memory area, so
384 * there's no need for any carrier cell. This implies
385 * that each memory area is at least as big as one
386 * pointer. Just like with the libc's free(), nothing
387 * is done if <ptr> is NULL.
388 */
389static inline void pool_free(struct pool_head *pool, void *ptr)
390{
391 if (likely(ptr != NULL)) {
Olivier Houchardcf975d42018-01-24 18:38:31 +0100392#ifdef DEBUG_MEMORY_POOLS
393 /* we'll get late corruption if we refill to the wrong pool or double-free */
394 if (*POOL_LINK(pool, ptr) != (void *)pool)
Willy Tarreaue4d42552020-03-14 11:08:16 +0100395 *DISGUISE((volatile int *)0) = 0;
Olivier Houchardcf975d42018-01-24 18:38:31 +0100396#endif
Willy Tarreauda520352019-11-15 06:59:54 +0100397 if (mem_poison_byte >= 0)
398 memset(ptr, mem_poison_byte, pool->size);
Willy Tarreaue18db9e2018-10-16 10:28:54 +0200399 pool_put_to_cache(pool, ptr);
Olivier Houchardcf975d42018-01-24 18:38:31 +0100400 }
401}
402
Willy Tarreauf161d0f2018-02-22 14:05:55 +0100403#else /* CONFIG_HAP_LOCKLESS_POOLS */
Olivier Houchardcf975d42018-01-24 18:38:31 +0100404/*
Willy Tarreau02622412014-12-08 16:35:23 +0100405 * Returns a pointer to type <type> taken from the pool <pool_type> if
406 * available, otherwise returns NULL. No malloc() is attempted, and poisonning
407 * is never performed. The purpose is to get the fastest possible allocation.
Willy Tarreau50e608d2007-05-13 18:26:08 +0200408 */
Christopher Fauletb349e482017-08-29 09:52:38 +0200409static inline void *__pool_get_first(struct pool_head *pool)
Willy Tarreaue430e772014-12-23 14:13:16 +0100410{
411 void *p;
412
Willy Tarreau02622412014-12-08 16:35:23 +0100413 if ((p = pool->free_list) != NULL) {
Willy Tarreauac421112015-10-28 15:09:29 +0100414 pool->free_list = *POOL_LINK(pool, p);
Willy Tarreaue430e772014-12-23 14:13:16 +0100415 pool->used++;
Willy Tarreaude30a682015-10-28 15:23:51 +0100416#ifdef DEBUG_MEMORY_POOLS
417 /* keep track of where the element was allocated from */
418 *POOL_LINK(pool, p) = (void *)pool;
419#endif
Willy Tarreaue430e772014-12-23 14:13:16 +0100420 }
421 return p;
422}
Willy Tarreau50e608d2007-05-13 18:26:08 +0200423
Christopher Fauletb349e482017-08-29 09:52:38 +0200424static inline void *pool_get_first(struct pool_head *pool)
425{
426 void *ret;
427
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100428 HA_SPIN_LOCK(POOL_LOCK, &pool->lock);
Christopher Fauletb349e482017-08-29 09:52:38 +0200429 ret = __pool_get_first(pool);
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100430 HA_SPIN_UNLOCK(POOL_LOCK, &pool->lock);
Christopher Fauletb349e482017-08-29 09:52:38 +0200431 return ret;
432}
Willy Tarreau50e608d2007-05-13 18:26:08 +0200433/*
Willy Tarreau02622412014-12-08 16:35:23 +0100434 * Returns a pointer to type <type> taken from the pool <pool_type> or
435 * dynamically allocated. In the first case, <pool_type> is updated to point to
436 * the next element in the list. No memory poisonning is ever performed on the
437 * returned area.
438 */
439static inline void *pool_alloc_dirty(struct pool_head *pool)
440{
441 void *p;
442
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100443 HA_SPIN_LOCK(POOL_LOCK, &pool->lock);
Christopher Fauletb349e482017-08-29 09:52:38 +0200444 if ((p = __pool_get_first(pool)) == NULL)
445 p = __pool_refill_alloc(pool, 0);
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100446 HA_SPIN_UNLOCK(POOL_LOCK, &pool->lock);
Willy Tarreau02622412014-12-08 16:35:23 +0100447 return p;
448}
449
Willy Tarreau158fa752017-11-22 15:47:29 +0100450#ifndef DEBUG_UAF /* normal allocator */
451
Willy Tarreauf13322e2017-11-22 10:50:54 +0100452/* allocates an area of size <size> and returns it. The semantics are similar
453 * to those of malloc().
454 */
455static inline void *pool_alloc_area(size_t size)
456{
457 return malloc(size);
458}
459
460/* frees an area <area> of size <size> allocated by pool_alloc_area(). The
461 * semantics are identical to free() except that the size is specified and
462 * may be ignored.
463 */
464static inline void pool_free_area(void *area, size_t __maybe_unused size)
465{
466 free(area);
467}
468
Willy Tarreau158fa752017-11-22 15:47:29 +0100469#else /* use-after-free detector */
470
471/* allocates an area of size <size> and returns it. The semantics are similar
472 * to those of malloc(). However the allocation is rounded up to 4kB so that a
473 * full page is allocated. This ensures the object can be freed alone so that
474 * future dereferences are easily detected. The returned object is always
Willy Tarreau364d7452018-02-22 14:14:23 +0100475 * 16-bytes aligned to avoid issues with unaligned structure objects. In case
476 * some padding is added, the area's start address is copied at the end of the
477 * padding to help detect underflows.
Willy Tarreau158fa752017-11-22 15:47:29 +0100478 */
Olivier Houchard62975a72018-10-21 01:33:11 +0200479#include <errno.h>
Willy Tarreau158fa752017-11-22 15:47:29 +0100480static inline void *pool_alloc_area(size_t size)
481{
482 size_t pad = (4096 - size) & 0xFF0;
Willy Tarreau229e7392019-08-08 07:38:19 +0200483 int isolated;
Willy Tarreau5a9cce42018-02-22 11:39:23 +0100484 void *ret;
Willy Tarreau158fa752017-11-22 15:47:29 +0100485
Willy Tarreau229e7392019-08-08 07:38:19 +0200486 isolated = thread_isolated();
487 if (!isolated)
488 thread_harmless_now();
Olivier Houchard62975a72018-10-21 01:33:11 +0200489 ret = mmap(NULL, (size + 4095) & -4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
Willy Tarreau85b2cae2019-07-04 16:18:23 +0200490 if (ret != MAP_FAILED) {
491 /* let's dereference the page before returning so that the real
492 * allocation in the system is performed without holding the lock.
493 */
494 *(int *)ret = 0;
495 if (pad >= sizeof(void *))
496 *(void **)(ret + pad - sizeof(void *)) = ret + pad;
497 ret += pad;
498 } else {
499 ret = NULL;
500 }
Willy Tarreau229e7392019-08-08 07:38:19 +0200501 if (!isolated)
502 thread_harmless_end();
Willy Tarreau85b2cae2019-07-04 16:18:23 +0200503 return ret;
Willy Tarreau158fa752017-11-22 15:47:29 +0100504}
505
506/* frees an area <area> of size <size> allocated by pool_alloc_area(). The
507 * semantics are identical to free() except that the size must absolutely match
Willy Tarreau364d7452018-02-22 14:14:23 +0100508 * the one passed to pool_alloc_area(). In case some padding is added, the
509 * area's start address is compared to the one at the end of the padding, and
510 * a segfault is triggered if they don't match, indicating an underflow.
Willy Tarreau158fa752017-11-22 15:47:29 +0100511 */
512static inline void pool_free_area(void *area, size_t size)
513{
514 size_t pad = (4096 - size) & 0xFF0;
515
Willy Tarreau364d7452018-02-22 14:14:23 +0100516 if (pad >= sizeof(void *) && *(void **)(area - sizeof(void *)) != area)
Willy Tarreaue4d42552020-03-14 11:08:16 +0100517 *DISGUISE((volatile int *)0) = 0;
Willy Tarreau364d7452018-02-22 14:14:23 +0100518
Willy Tarreau85b2cae2019-07-04 16:18:23 +0200519 thread_harmless_now();
Willy Tarreau158fa752017-11-22 15:47:29 +0100520 munmap(area - pad, (size + 4095) & -4096);
Willy Tarreau85b2cae2019-07-04 16:18:23 +0200521 thread_harmless_end();
Willy Tarreau158fa752017-11-22 15:47:29 +0100522}
523
524#endif /* DEBUG_UAF */
525
Willy Tarreau02622412014-12-08 16:35:23 +0100526/*
527 * Returns a pointer to type <type> taken from the pool <pool_type> or
528 * dynamically allocated. In the first case, <pool_type> is updated to point to
529 * the next element in the list. Memory poisonning is performed if enabled.
530 */
Willy Tarreaubafbe012017-11-24 17:34:44 +0100531static inline void *pool_alloc(struct pool_head *pool)
Willy Tarreau02622412014-12-08 16:35:23 +0100532{
533 void *p;
534
535 p = pool_alloc_dirty(pool);
Willy Tarreaude30a682015-10-28 15:23:51 +0100536 if (p && mem_poison_byte >= 0) {
Willy Tarreau02622412014-12-08 16:35:23 +0100537 memset(p, mem_poison_byte, pool->size);
Willy Tarreaude30a682015-10-28 15:23:51 +0100538 }
539
Willy Tarreau02622412014-12-08 16:35:23 +0100540 return p;
541}
542
543/*
Willy Tarreau50e608d2007-05-13 18:26:08 +0200544 * Puts a memory area back to the corresponding pool.
545 * Items are chained directly through a pointer that
546 * is written in the beginning of the memory area, so
547 * there's no need for any carrier cell. This implies
548 * that each memory area is at least as big as one
Willy Tarreau48d63db2008-08-03 17:41:33 +0200549 * pointer. Just like with the libc's free(), nothing
550 * is done if <ptr> is NULL.
Willy Tarreau50e608d2007-05-13 18:26:08 +0200551 */
Willy Tarreaubafbe012017-11-24 17:34:44 +0100552static inline void pool_free(struct pool_head *pool, void *ptr)
Willy Tarreaue430e772014-12-23 14:13:16 +0100553{
554 if (likely(ptr != NULL)) {
Willy Tarreaude30a682015-10-28 15:23:51 +0100555#ifdef DEBUG_MEMORY_POOLS
556 /* we'll get late corruption if we refill to the wrong pool or double-free */
557 if (*POOL_LINK(pool, ptr) != (void *)pool)
Willy Tarreaue4d42552020-03-14 11:08:16 +0100558 *DISGUISE((volatile int *)0) = 0;
Willy Tarreaude30a682015-10-28 15:23:51 +0100559#endif
Willy Tarreau158fa752017-11-22 15:47:29 +0100560
561#ifndef DEBUG_UAF /* normal pool behaviour */
Willy Tarreau3e853ea2019-07-04 11:30:00 +0200562 HA_SPIN_LOCK(POOL_LOCK, &pool->lock);
Willy Tarreau3e853ea2019-07-04 11:30:00 +0200563 pool->used--;
Willy Tarreau63a87382020-05-08 08:38:24 +0200564 if (pool_is_crowded(pool)) {
565 free(ptr);
566 pool->allocated--;
567 } else {
568 *POOL_LINK(pool, ptr) = (void *)pool->free_list;
569 pool->free_list = (void *)ptr;
570 }
Willy Tarreaua1e4f8c2020-05-08 08:31:56 +0200571 pool_avg_add(&pool->needed_avg, pool->used);
Willy Tarreau3e853ea2019-07-04 11:30:00 +0200572 HA_SPIN_UNLOCK(POOL_LOCK, &pool->lock);
Willy Tarreau158fa752017-11-22 15:47:29 +0100573#else /* release the entry for real to detect use after free */
574 /* ensure we crash on double free or free of a const area*/
575 *(uint32_t *)ptr = 0xDEADADD4;
576 pool_free_area(ptr, pool->size + POOL_EXTRA);
Willy Tarreau3e853ea2019-07-04 11:30:00 +0200577 HA_SPIN_LOCK(POOL_LOCK, &pool->lock);
Willy Tarreau158fa752017-11-22 15:47:29 +0100578 pool->allocated--;
Willy Tarreau3e853ea2019-07-04 11:30:00 +0200579 pool->used--;
Willy Tarreaua1e4f8c2020-05-08 08:31:56 +0200580 pool_avg_add(&pool->needed_avg, pool->used);
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100581 HA_SPIN_UNLOCK(POOL_LOCK, &pool->lock);
Willy Tarreau3e853ea2019-07-04 11:30:00 +0200582#endif /* DEBUG_UAF */
Willy Tarreaue430e772014-12-23 14:13:16 +0100583 }
584}
Willy Tarreauf161d0f2018-02-22 14:05:55 +0100585#endif /* CONFIG_HAP_LOCKLESS_POOLS */
Willy Tarreau2dd0d472006-06-29 17:53:05 +0200586#endif /* _COMMON_MEMORY_H */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200587
588/*
589 * Local variables:
590 * c-indent-level: 8
591 * c-basic-offset: 8
592 * End:
593 */