blob: 1c177cafd29bf7b95fe955901978c468bd25b368 [file] [log] [blame]
Willy Tarreau50e608d2007-05-13 18:26:08 +02001/*
2 * Memory management functions.
3 *
4 * Copyright 2000-2007 Willy Tarreau <w@1wt.eu>
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 *
11 */
Willy Tarreauf14d1902021-10-05 18:14:11 +020012
13#include <sys/mman.h>
Willy Tarreau7107c8b2018-11-26 11:44:35 +010014#include <errno.h>
Willy Tarreau50e608d2007-05-13 18:26:08 +020015
Willy Tarreau5d9ddc52021-10-06 19:54:09 +020016#include <haproxy/activity.h>
Willy Tarreau4c7e4b72020-05-27 12:58:42 +020017#include <haproxy/api.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020018#include <haproxy/applet-t.h>
Willy Tarreau6be78492020-06-05 00:00:29 +020019#include <haproxy/cfgparse.h>
Willy Tarreauf1d32c42020-06-04 21:07:02 +020020#include <haproxy/channel.h>
Willy Tarreau83487a82020-06-04 20:19:54 +020021#include <haproxy/cli.h>
Willy Tarreau36979d92020-06-05 17:27:29 +020022#include <haproxy/errors.h>
Willy Tarreauf268ee82020-06-04 17:05:57 +020023#include <haproxy/global.h>
Willy Tarreau853b2972020-05-27 18:01:47 +020024#include <haproxy/list.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020025#include <haproxy/pool.h>
Willy Tarreau5edca2f2022-05-27 09:25:10 +020026#include <haproxy/sc_strm.h>
Willy Tarreau2eec9b52020-06-04 19:58:55 +020027#include <haproxy/stats-t.h>
Willy Tarreaucb086c62022-05-27 09:47:12 +020028#include <haproxy/stconn.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020029#include <haproxy/thread.h>
Willy Tarreau48fbcae2020-06-03 18:09:46 +020030#include <haproxy/tools.h>
Willy Tarreau50e608d2007-05-13 18:26:08 +020031
Willy Tarreau50e608d2007-05-13 18:26:08 +020032
Willy Tarreau7f0165e2018-11-26 17:09:46 +010033/* These ones are initialized per-thread on startup by init_pools() */
Willy Tarreaue18db9e2018-10-16 10:28:54 +020034THREAD_LOCAL size_t pool_cache_bytes = 0; /* total cache size */
35THREAD_LOCAL size_t pool_cache_count = 0; /* #cache objects */
36
Willy Tarreau9f699952022-02-18 18:31:53 +010037static struct list pools __read_mostly = LIST_HEAD_INIT(pools);
Willy Tarreauef301b72022-02-23 14:15:18 +010038int mem_poison_byte __read_mostly = 'P';
Willy Tarreau8d0273e2022-02-21 17:16:22 +010039uint pool_debugging __read_mostly = /* set of POOL_DBG_* flags */
Olivier Houcharddc21ff72019-01-29 15:20:16 +010040#ifdef DEBUG_FAIL_ALLOC
Willy Tarreau8d0273e2022-02-21 17:16:22 +010041 POOL_DBG_FAIL_ALLOC |
Olivier Houcharddc21ff72019-01-29 15:20:16 +010042#endif
Willy Tarreaufd8b7372022-02-21 17:31:50 +010043#ifdef DEBUG_DONT_SHARE_POOLS
44 POOL_DBG_DONT_MERGE |
45#endif
Willy Tarreaud3470e12022-02-21 18:30:25 +010046#ifdef DEBUG_POOL_INTEGRITY
47 POOL_DBG_COLD_FIRST |
48#endif
Willy Tarreau6f3c7f62022-02-21 18:42:53 +010049#ifdef DEBUG_POOL_INTEGRITY
50 POOL_DBG_INTEGRITY |
51#endif
Willy Tarreaudff3b062022-02-22 09:21:13 +010052#ifdef CONFIG_HAP_NO_GLOBAL_POOLS
53 POOL_DBG_NO_GLOBAL |
54#endif
Willy Tarreaue9816312022-02-22 16:23:09 +010055#ifndef CONFIG_HAP_POOLS
56 POOL_DBG_NO_CACHE |
57#endif
Willy Tarreau02718222022-02-23 10:10:33 +010058#if defined(DEBUG_POOL_TRACING)
59 POOL_DBG_CALLER |
60#endif
Willy Tarreau13d77752022-02-23 10:20:37 +010061#if defined(DEBUG_MEMORY_POOLS)
62 POOL_DBG_TAG |
63#endif
Willy Tarreau8d0273e2022-02-21 17:16:22 +010064 0;
Olivier Houcharddc21ff72019-01-29 15:20:16 +010065
Willy Tarreauf4b79c42022-02-23 15:20:53 +010066static const struct {
67 uint flg;
68 const char *set;
69 const char *clr;
70 const char *hlp;
71} dbg_options[] = {
72 /* flg, set, clr, hlp */
73 { POOL_DBG_FAIL_ALLOC, "fail", "no-fail", "randomly fail allocations" },
74 { POOL_DBG_DONT_MERGE, "no-merge", "merge", "disable merging of similar pools" },
75 { POOL_DBG_COLD_FIRST, "cold-first", "hot-first", "pick cold objects first" },
76 { POOL_DBG_INTEGRITY, "integrity", "no-integrity", "enable cache integrity checks" },
77 { POOL_DBG_NO_GLOBAL, "no-global", "global", "disable global shared cache" },
78 { POOL_DBG_NO_CACHE, "no-cache", "cache", "disable thread-local cache" },
79 { POOL_DBG_CALLER, "caller", "no-caller", "save caller information in cache" },
80 { POOL_DBG_TAG, "tag", "no-tag", "add tag at end of allocated objects" },
81 { POOL_DBG_POISON, "poison", "no-poison", "poison newly allocated objects" },
82 { 0 /* end */ }
83};
84
Willy Tarreau224adf22022-11-21 09:02:41 +010085/* describes a snapshot of a pool line about to be dumped by "show pools" */
86struct pool_dump_info {
87 const struct pool_head *entry;
88 ulong alloc_items;
89 ulong alloc_bytes;
90 ulong used_items;
91 ulong cached_items;
92 ulong need_avg;
93 ulong failed_items;
94};
95
Willy Tarreau2fba08f2022-11-21 09:34:02 +010096/* context used by "show pools" */
97struct show_pools_ctx {
Willy Tarreau7583c362022-11-21 10:02:29 +010098 char *prefix; /* if non-null, match this prefix name for the pool */
Willy Tarreau2fba08f2022-11-21 09:34:02 +010099 int by_what; /* 0=no sort, 1=by name, 2=by item size, 3=by total alloc */
100 int maxcnt; /* 0=no limit, other=max number of output entries */
101};
102
Willy Tarreau8d0273e2022-02-21 17:16:22 +0100103static int mem_fail_rate __read_mostly = 0;
Willy Tarreau9f699952022-02-18 18:31:53 +0100104static int using_default_allocator __read_mostly = 1;
Willy Tarreauc4e56dc2022-03-08 10:41:40 +0100105static int disable_trim __read_mostly = 0;
David Carliered232142021-11-25 16:09:45 +0000106static int(*my_mallctl)(const char *, void *, size_t *, void *, size_t) = NULL;
Willy Tarreau157e3932021-09-15 10:05:48 +0200107
Willy Tarreau0d93a812021-12-23 09:26:30 +0100108/* ask the allocator to trim memory pools.
109 * This must run under thread isolation so that competing threads trying to
110 * allocate or release memory do not prevent the allocator from completing
111 * its job. We just have to be careful as callers might already be isolated
112 * themselves.
113 */
Willy Tarreauea3323f2021-09-15 10:38:21 +0200114static void trim_all_pools(void)
115{
Willy Tarreau0d93a812021-12-23 09:26:30 +0100116 int isolated = thread_isolated();
117
Willy Tarreauc4e56dc2022-03-08 10:41:40 +0100118 if (disable_trim)
119 return;
120
Willy Tarreau0d93a812021-12-23 09:26:30 +0100121 if (!isolated)
122 thread_isolate();
123
David Carlierd450ff62021-11-25 16:14:38 +0000124 if (my_mallctl) {
125 unsigned int i, narenas = 0;
126 size_t len = sizeof(narenas);
127
128 if (my_mallctl("arenas.narenas", &narenas, &len, NULL, 0) == 0) {
129 for (i = 0; i < narenas; i ++) {
130 char mib[32] = {0};
131 snprintf(mib, sizeof(mib), "arena.%u.purge", i);
132 (void)my_mallctl(mib, NULL, NULL, NULL, 0);
133 }
134 }
135 } else {
David Carliered232142021-11-25 16:09:45 +0000136#if defined(HA_HAVE_MALLOC_TRIM)
David Carlierd450ff62021-11-25 16:14:38 +0000137 if (using_default_allocator)
138 malloc_trim(0);
David CARLIERb1e190a2021-11-26 20:44:44 +0000139#elif defined(HA_HAVE_MALLOC_ZONE)
140 if (using_default_allocator) {
141 vm_address_t *zones;
142 unsigned int i, nzones;
143
144 if (malloc_get_all_zones(0, NULL, &zones, &nzones) == KERN_SUCCESS) {
145 for (i = 0; i < nzones; i ++) {
146 malloc_zone_t *zone = (malloc_zone_t *)zones[i];
147
148 /* we cannot purge anonymous zones */
149 if (zone->zone_name)
150 malloc_zone_pressure_relief(zone, 0);
151 }
152 }
153 }
David Carliered232142021-11-25 16:09:45 +0000154#endif
David Carlierd450ff62021-11-25 16:14:38 +0000155 }
Willy Tarreau0d93a812021-12-23 09:26:30 +0100156
157 if (!isolated)
158 thread_release();
Willy Tarreauea3323f2021-09-15 10:38:21 +0200159}
160
Willy Tarreau157e3932021-09-15 10:05:48 +0200161/* check if we're using the same allocator as the one that provides
162 * malloc_trim() and mallinfo(). The principle is that on glibc, both
163 * malloc_trim() and mallinfo() are provided, and using mallinfo() we
164 * can check if malloc() is performed through glibc or any other one
David Carliered232142021-11-25 16:09:45 +0000165 * the executable was linked against (e.g. jemalloc). Prior to this we
166 * have to check whether we're running on jemalloc by verifying if the
167 * mallctl() function is provided. Its pointer will be used later.
Willy Tarreau157e3932021-09-15 10:05:48 +0200168 */
169static void detect_allocator(void)
170{
Willy Tarreau781f07a2021-11-26 15:55:55 +0100171#if defined(__ELF__)
David Carliered232142021-11-25 16:09:45 +0000172 extern int mallctl(const char *, void *, size_t *, void *, size_t) __attribute__((weak));
173
174 my_mallctl = mallctl;
Willy Tarreau781f07a2021-11-26 15:55:55 +0100175#endif
David Carliered232142021-11-25 16:09:45 +0000176
177 if (!my_mallctl) {
178 my_mallctl = get_sym_curr_addr("mallctl");
179 using_default_allocator = (my_mallctl == NULL);
180 }
181
182 if (!my_mallctl) {
183#if defined(HA_HAVE_MALLOC_TRIM)
Willy Tarreauc2afb862021-09-16 09:18:21 +0200184#ifdef HA_HAVE_MALLINFO2
David Carliered232142021-11-25 16:09:45 +0000185 struct mallinfo2 mi1, mi2;
Willy Tarreauc2afb862021-09-16 09:18:21 +0200186#else
David Carliered232142021-11-25 16:09:45 +0000187 struct mallinfo mi1, mi2;
Willy Tarreauc2afb862021-09-16 09:18:21 +0200188#endif
David Carliered232142021-11-25 16:09:45 +0000189 void *ptr;
Willy Tarreau157e3932021-09-15 10:05:48 +0200190
Willy Tarreauc2afb862021-09-16 09:18:21 +0200191#ifdef HA_HAVE_MALLINFO2
David Carliered232142021-11-25 16:09:45 +0000192 mi1 = mallinfo2();
Willy Tarreauc2afb862021-09-16 09:18:21 +0200193#else
David Carliered232142021-11-25 16:09:45 +0000194 mi1 = mallinfo();
Willy Tarreauc2afb862021-09-16 09:18:21 +0200195#endif
David Carliered232142021-11-25 16:09:45 +0000196 ptr = DISGUISE(malloc(1));
Willy Tarreauc2afb862021-09-16 09:18:21 +0200197#ifdef HA_HAVE_MALLINFO2
David Carliered232142021-11-25 16:09:45 +0000198 mi2 = mallinfo2();
Willy Tarreauc2afb862021-09-16 09:18:21 +0200199#else
David Carliered232142021-11-25 16:09:45 +0000200 mi2 = mallinfo();
Willy Tarreauc2afb862021-09-16 09:18:21 +0200201#endif
David Carliered232142021-11-25 16:09:45 +0000202 free(DISGUISE(ptr));
Willy Tarreauea3323f2021-09-15 10:38:21 +0200203
David Carliered232142021-11-25 16:09:45 +0000204 using_default_allocator = !!memcmp(&mi1, &mi2, sizeof(mi1));
David CARLIERb1e190a2021-11-26 20:44:44 +0000205#elif defined(HA_HAVE_MALLOC_ZONE)
206 using_default_allocator = (malloc_default_zone() != NULL);
David Carliered232142021-11-25 16:09:45 +0000207#endif
208 }
Willy Tarreau845b5602021-09-15 10:41:24 +0200209}
210
211static int is_trim_enabled(void)
212{
David Carliered232142021-11-25 16:09:45 +0000213 return using_default_allocator;
Willy Tarreau157e3932021-09-15 10:05:48 +0200214}
Willy Tarreauea3323f2021-09-15 10:38:21 +0200215
Willy Tarreau8d0273e2022-02-21 17:16:22 +0100216static int mem_should_fail(const struct pool_head *pool)
217{
218 int ret = 0;
219
220 if (mem_fail_rate > 0 && !(global.mode & MODE_STARTING)) {
221 if (mem_fail_rate > statistical_prng_range(100))
222 ret = 1;
223 else
224 ret = 0;
225 }
226 return ret;
227}
228
Willy Tarreau50e608d2007-05-13 18:26:08 +0200229/* Try to find an existing shared pool with the same characteristics and
230 * returns it, otherwise creates this one. NULL is returned if no memory
Willy Tarreau581bf812016-01-25 02:19:13 +0100231 * is available for a new creation. Two flags are supported :
232 * - MEM_F_SHARED to indicate that the pool may be shared with other users
233 * - MEM_F_EXACT to indicate that the size must not be rounded up
Willy Tarreau50e608d2007-05-13 18:26:08 +0200234 */
235struct pool_head *create_pool(char *name, unsigned int size, unsigned int flags)
236{
Willy Tarreau42705d02022-02-23 10:03:11 +0100237 unsigned int extra_mark, extra_caller, extra;
Willy Tarreau50e608d2007-05-13 18:26:08 +0200238 struct pool_head *pool;
Willy Tarreau7dcd46d2007-05-14 00:16:13 +0200239 struct pool_head *entry;
240 struct list *start;
Willy Tarreau50e608d2007-05-13 18:26:08 +0200241 unsigned int align;
Willy Tarreau9f3129e2021-04-17 00:31:38 +0200242 int thr __maybe_unused;
Willy Tarreau50e608d2007-05-13 18:26:08 +0200243
Willy Tarreauac421112015-10-28 15:09:29 +0100244 /* We need to store a (void *) at the end of the chunks. Since we know
Willy Tarreau50e608d2007-05-13 18:26:08 +0200245 * that the malloc() function will never return such a small size,
246 * let's round the size up to something slightly bigger, in order to
247 * ease merging of entries. Note that the rounding is a power of two.
Willy Tarreauac421112015-10-28 15:09:29 +0100248 * This extra (void *) is not accounted for in the size computation
249 * so that the visible parts outside are not affected.
Willy Tarreau30f931e2018-10-23 14:40:23 +0200250 *
251 * Note: for the LRU cache, we need to store 2 doubly-linked lists.
Willy Tarreau50e608d2007-05-13 18:26:08 +0200252 */
253
Willy Tarreau13d77752022-02-23 10:20:37 +0100254 extra_mark = (pool_debugging & POOL_DBG_TAG) ? POOL_EXTRA_MARK : 0;
Willy Tarreau02718222022-02-23 10:10:33 +0100255 extra_caller = (pool_debugging & POOL_DBG_CALLER) ? POOL_EXTRA_CALLER : 0;
Willy Tarreau42705d02022-02-23 10:03:11 +0100256 extra = extra_mark + extra_caller;
257
Willy Tarreau581bf812016-01-25 02:19:13 +0100258 if (!(flags & MEM_F_EXACT)) {
Willy Tarreau30f931e2018-10-23 14:40:23 +0200259 align = 4 * sizeof(void *); // 2 lists = 4 pointers min
Willy Tarreau42705d02022-02-23 10:03:11 +0100260 size = ((size + extra + align - 1) & -align) - extra;
Willy Tarreau581bf812016-01-25 02:19:13 +0100261 }
Willy Tarreau50e608d2007-05-13 18:26:08 +0200262
Willy Tarreaue9816312022-02-22 16:23:09 +0100263 if (!(pool_debugging & POOL_DBG_NO_CACHE)) {
264 /* we'll store two lists there, we need the room for this. This is
265 * guaranteed by the test above, except if MEM_F_EXACT is set, or if
266 * the only EXTRA part is in fact the one that's stored in the cache
267 * in addition to the pci struct.
268 */
Willy Tarreau42705d02022-02-23 10:03:11 +0100269 if (size + extra - extra_caller < sizeof(struct pool_cache_item))
270 size = sizeof(struct pool_cache_item) + extra_caller - extra;
Willy Tarreaue9816312022-02-22 16:23:09 +0100271 }
272
Christopher Fauletb349e482017-08-29 09:52:38 +0200273 /* TODO: thread: we do not lock pool list for now because all pools are
274 * created during HAProxy startup (so before threads creation) */
Willy Tarreau7dcd46d2007-05-14 00:16:13 +0200275 start = &pools;
Willy Tarreau50e608d2007-05-13 18:26:08 +0200276 pool = NULL;
Willy Tarreau7dcd46d2007-05-14 00:16:13 +0200277
278 list_for_each_entry(entry, &pools, list) {
279 if (entry->size == size) {
280 /* either we can share this place and we take it, or
Ilya Shipitsin47d17182020-06-21 21:42:57 +0500281 * we look for a shareable one or for the next position
Willy Tarreau7dcd46d2007-05-14 00:16:13 +0200282 * before which we will insert a new one.
283 */
Willy Tarreaufd8b7372022-02-21 17:31:50 +0100284 if ((flags & entry->flags & MEM_F_SHARED) &&
285 (!(pool_debugging & POOL_DBG_DONT_MERGE) ||
286 strcmp(name, entry->name) == 0)) {
Willy Tarreau7dcd46d2007-05-14 00:16:13 +0200287 /* we can share this one */
Willy Tarreau50e608d2007-05-13 18:26:08 +0200288 pool = entry;
Krzysztof Piotr Oledzkia643baf2008-05-29 23:53:44 +0200289 DPRINTF(stderr, "Sharing %s with %s\n", name, pool->name);
Willy Tarreau50e608d2007-05-13 18:26:08 +0200290 break;
291 }
292 }
Willy Tarreau7dcd46d2007-05-14 00:16:13 +0200293 else if (entry->size > size) {
294 /* insert before this one */
295 start = &entry->list;
296 break;
297 }
Willy Tarreau50e608d2007-05-13 18:26:08 +0200298 }
299
300 if (!pool) {
Willy Tarreaue81248c2022-03-02 17:59:04 +0100301 void *pool_addr;
Willy Tarreau0a93b642018-10-16 07:58:39 +0200302
Willy Tarreaue81248c2022-03-02 17:59:04 +0100303 pool_addr = calloc(1, sizeof(*pool) + __alignof__(*pool));
304 if (!pool_addr)
Willy Tarreau50e608d2007-05-13 18:26:08 +0200305 return NULL;
Willy Tarreaue81248c2022-03-02 17:59:04 +0100306
307 /* always provide an aligned pool */
308 pool = (struct pool_head*)((((size_t)pool_addr) + __alignof__(*pool)) & -(size_t)__alignof__(*pool));
309 pool->base_addr = pool_addr; // keep it, it's the address to free later
310
Willy Tarreau50e608d2007-05-13 18:26:08 +0200311 if (name)
312 strlcpy2(pool->name, name, sizeof(pool->name));
Willy Tarreau42705d02022-02-23 10:03:11 +0100313 pool->alloc_sz = size + extra;
Willy Tarreau50e608d2007-05-13 18:26:08 +0200314 pool->size = size;
315 pool->flags = flags;
Willy Tarreau2b718102021-04-21 07:32:39 +0200316 LIST_APPEND(start, &pool->list);
Christopher Faulet2f6d3c02019-06-25 21:45:59 +0200317
Willy Tarreaue9816312022-02-22 16:23:09 +0100318 if (!(pool_debugging & POOL_DBG_NO_CACHE)) {
319 /* update per-thread pool cache if necessary */
320 for (thr = 0; thr < MAX_THREADS; thr++) {
321 LIST_INIT(&pool->cache[thr].list);
322 pool->cache[thr].tid = thr;
323 pool->cache[thr].pool = pool;
324 }
Christopher Faulet2f6d3c02019-06-25 21:45:59 +0200325 }
Olivier Houchard8af97eb2020-02-01 17:45:32 +0100326 }
327 pool->users++;
Willy Tarreau50e608d2007-05-13 18:26:08 +0200328 return pool;
329}
Olivier Houchardcf975d42018-01-24 18:38:31 +0100330
Willy Tarreau13843642021-04-17 16:57:25 +0200331/* Tries to allocate an object for the pool <pool> using the system's allocator
332 * and directly returns it. The pool's allocated counter is checked and updated,
Willy Tarreau8715dec2021-06-10 17:31:48 +0200333 * but no other checks are performed.
Willy Tarreau13843642021-04-17 16:57:25 +0200334 */
335void *pool_get_from_os(struct pool_head *pool)
336{
337 if (!pool->limit || pool->allocated < pool->limit) {
Willy Tarreau96d5bc72022-02-23 08:57:59 +0100338 void *ptr = pool_alloc_area(pool->alloc_sz);
Willy Tarreau13843642021-04-17 16:57:25 +0200339 if (ptr) {
340 _HA_ATOMIC_INC(&pool->allocated);
341 return ptr;
342 }
343 _HA_ATOMIC_INC(&pool->failed);
344 }
345 activity[tid].pool_fail++;
346 return NULL;
347
348}
349
Willy Tarreau45e4e282021-04-17 17:48:40 +0200350/* Releases a pool item back to the operating system and atomically updates
351 * the allocation counter.
352 */
353void pool_put_to_os(struct pool_head *pool, void *ptr)
354{
Willy Tarreau9a7aa3b2021-06-10 17:20:19 +0200355#ifdef DEBUG_UAF
356 /* This object will be released for real in order to detect a use after
357 * free. We also force a write to the area to ensure we crash on double
358 * free or free of a const area.
359 */
360 *(uint32_t *)ptr = 0xDEADADD4;
361#endif /* DEBUG_UAF */
362
Willy Tarreau96d5bc72022-02-23 08:57:59 +0100363 pool_free_area(ptr, pool->alloc_sz);
Willy Tarreau45e4e282021-04-17 17:48:40 +0200364 _HA_ATOMIC_DEC(&pool->allocated);
365}
366
Willy Tarreau8fe726f2021-04-15 18:20:12 +0200367/* Tries to allocate an object for the pool <pool> using the system's allocator
368 * and directly returns it. The pool's counters are updated but the object is
369 * never cached, so this is usable with and without local or shared caches.
Willy Tarreau8fe726f2021-04-15 18:20:12 +0200370 */
371void *pool_alloc_nocache(struct pool_head *pool)
Willy Tarreau0bae0752021-03-02 20:05:09 +0100372{
Willy Tarreau0bae0752021-03-02 20:05:09 +0100373 void *ptr = NULL;
374
Willy Tarreau13843642021-04-17 16:57:25 +0200375 ptr = pool_get_from_os(pool);
376 if (!ptr)
Willy Tarreau0bae0752021-03-02 20:05:09 +0100377 return NULL;
Willy Tarreau0bae0752021-03-02 20:05:09 +0100378
Willy Tarreau13843642021-04-17 16:57:25 +0200379 swrate_add_scaled(&pool->needed_avg, POOL_AVG_SAMPLES, pool->used, POOL_AVG_SAMPLES/4);
Willy Tarreau4781b152021-04-06 13:53:36 +0200380 _HA_ATOMIC_INC(&pool->used);
Willy Tarreau0bae0752021-03-02 20:05:09 +0100381
Willy Tarreau0bae0752021-03-02 20:05:09 +0100382 /* keep track of where the element was allocated from */
Willy Tarreau8c492702022-01-01 17:10:50 +0100383 POOL_DEBUG_SET_MARK(pool, ptr);
Willy Tarreauf70fdde2022-01-25 15:56:50 +0100384 POOL_DEBUG_TRACE_CALLER(pool, (struct pool_cache_item *)ptr, NULL);
Willy Tarreau0bae0752021-03-02 20:05:09 +0100385 return ptr;
386}
387
Willy Tarreau45e4e282021-04-17 17:48:40 +0200388/* Release a pool item back to the OS and keeps the pool's counters up to date.
389 * This is always defined even when pools are not enabled (their usage stats
390 * are maintained).
391 */
392void pool_free_nocache(struct pool_head *pool, void *ptr)
393{
394 _HA_ATOMIC_DEC(&pool->used);
395 swrate_add(&pool->needed_avg, POOL_AVG_SAMPLES, pool->used);
396 pool_put_to_os(pool, ptr);
397}
398
Willy Tarreaub8498e92021-04-18 10:23:02 +0200399
Willy Tarreau6f3c7f62022-02-21 18:42:53 +0100400/* Updates <pch>'s fill_pattern and fills the free area after <item> with it,
401 * up to <size> bytes. The item part is left untouched.
402 */
403void pool_fill_pattern(struct pool_cache_head *pch, struct pool_cache_item *item, uint size)
404{
405 ulong *ptr = (ulong *)item;
406 uint ofs;
407 ulong u;
408
409 if (size <= sizeof(*item))
410 return;
411
412 /* Upgrade the fill_pattern to change about half of the bits
413 * (to be sure to catch static flag corruption), and apply it.
414 */
415 u = pch->fill_pattern += ~0UL / 3; // 0x55...55
416 ofs = sizeof(*item) / sizeof(*ptr);
417 while (ofs < size / sizeof(*ptr))
418 ptr[ofs++] = u;
419}
420
421/* check for a pool_cache_item integrity after extracting it from the cache. It
422 * must have been previously initialized using pool_fill_pattern(). If any
423 * corruption is detected, the function provokes an immediate crash.
424 */
425void pool_check_pattern(struct pool_cache_head *pch, struct pool_cache_item *item, uint size)
426{
427 const ulong *ptr = (const ulong *)item;
428 uint ofs;
429 ulong u;
430
431 if (size <= sizeof(*item))
432 return;
433
434 /* let's check that all words past *item are equal */
435 ofs = sizeof(*item) / sizeof(*ptr);
436 u = ptr[ofs++];
437 while (ofs < size / sizeof(*ptr)) {
438 if (unlikely(ptr[ofs] != u))
439 ABORT_NOW();
440 ofs++;
441 }
442}
443
Willy Tarreaua0b58312022-01-02 17:19:14 +0100444/* removes up to <count> items from the end of the local pool cache <ph> for
445 * pool <pool>. The shared pool is refilled with these objects in the limit
446 * of the number of acceptable objects, and the rest will be released to the
447 * OS. It is not a problem is <count> is larger than the number of objects in
Willy Tarreaue9816312022-02-22 16:23:09 +0100448 * the local cache. The counters are automatically updated. Must not be used
449 * with pools disabled.
Willy Tarreau87212032021-04-19 08:14:03 +0200450 */
Willy Tarreaua0b58312022-01-02 17:19:14 +0100451static void pool_evict_last_items(struct pool_head *pool, struct pool_cache_head *ph, uint count)
Willy Tarreau87212032021-04-19 08:14:03 +0200452{
Willy Tarreau87212032021-04-19 08:14:03 +0200453 struct pool_cache_item *item;
Willy Tarreau1513c542022-01-02 17:53:02 +0100454 struct pool_item *pi, *head = NULL;
Willy Tarreaua0b58312022-01-02 17:19:14 +0100455 uint released = 0;
Willy Tarreau1513c542022-01-02 17:53:02 +0100456 uint cluster = 0;
Willy Tarreau361e31e2022-01-02 00:27:06 +0100457 uint to_free_max;
458
Willy Tarreaue9816312022-02-22 16:23:09 +0100459 BUG_ON(pool_debugging & POOL_DBG_NO_CACHE);
460
Willy Tarreaudff3b062022-02-22 09:21:13 +0100461 /* Note: this will be zero when global pools are disabled */
Willy Tarreau361e31e2022-01-02 00:27:06 +0100462 to_free_max = pool_releasable(pool);
Willy Tarreau87212032021-04-19 08:14:03 +0200463
Willy Tarreaua0b58312022-01-02 17:19:14 +0100464 while (released < count && !LIST_ISEMPTY(&ph->list)) {
Willy Tarreaud5ec1002022-01-02 12:40:14 +0100465 item = LIST_PREV(&ph->list, typeof(item), by_pool);
Willy Tarreaue2830ad2022-02-09 16:23:55 +0100466 BUG_ON(&item->by_pool == &ph->list);
Willy Tarreau6f3c7f62022-02-21 18:42:53 +0100467 if (unlikely(pool_debugging & POOL_DBG_INTEGRITY))
468 pool_check_pattern(ph, item, pool->size);
Willy Tarreau2b718102021-04-21 07:32:39 +0200469 LIST_DELETE(&item->by_pool);
470 LIST_DELETE(&item->by_lru);
Willy Tarreaub46674a2021-12-30 17:37:33 +0100471
Willy Tarreau1513c542022-01-02 17:53:02 +0100472 if (to_free_max > released || cluster) {
Willy Tarreaudff3b062022-02-22 09:21:13 +0100473 /* will never match when global pools are disabled */
Willy Tarreau361e31e2022-01-02 00:27:06 +0100474 pi = (struct pool_item *)item;
Willy Tarreau1513c542022-01-02 17:53:02 +0100475 pi->next = NULL;
476 pi->down = head;
477 head = pi;
478 cluster++;
479 if (cluster >= CONFIG_HAP_POOL_CLUSTER_SIZE) {
480 /* enough to make a cluster */
481 pool_put_to_shared_cache(pool, head, cluster);
482 cluster = 0;
483 head = NULL;
484 }
Willy Tarreau361e31e2022-01-02 00:27:06 +0100485 } else
Willy Tarreaub46674a2021-12-30 17:37:33 +0100486 pool_free_nocache(pool, item);
Willy Tarreau1513c542022-01-02 17:53:02 +0100487
488 released++;
Willy Tarreau361e31e2022-01-02 00:27:06 +0100489 }
490
Willy Tarreau1513c542022-01-02 17:53:02 +0100491 /* incomplete cluster left */
492 if (cluster)
493 pool_put_to_shared_cache(pool, head, cluster);
494
Willy Tarreaua0b58312022-01-02 17:19:14 +0100495 ph->count -= released;
496 pool_cache_count -= released;
497 pool_cache_bytes -= released * pool->size;
498}
499
500/* Evicts some of the oldest objects from one local cache, until its number of
501 * objects is no more than 16+1/8 of the total number of locally cached objects
502 * or the total size of the local cache is no more than 75% of its maximum (i.e.
503 * we don't want a single cache to use all the cache for itself). For this, the
Willy Tarreauc895c442022-02-09 16:19:24 +0100504 * list is scanned in reverse. If <full> is non-null, all objects are evicted.
Willy Tarreaue9816312022-02-22 16:23:09 +0100505 * Must not be used when pools are disabled.
Willy Tarreaua0b58312022-01-02 17:19:14 +0100506 */
Willy Tarreauc895c442022-02-09 16:19:24 +0100507void pool_evict_from_local_cache(struct pool_head *pool, int full)
Willy Tarreaua0b58312022-01-02 17:19:14 +0100508{
509 struct pool_cache_head *ph = &pool->cache[tid];
510
Willy Tarreaue9816312022-02-22 16:23:09 +0100511 BUG_ON(pool_debugging & POOL_DBG_NO_CACHE);
512
Willy Tarreauc895c442022-02-09 16:19:24 +0100513 while ((ph->count && full) ||
514 (ph->count >= CONFIG_HAP_POOL_CLUSTER_SIZE &&
515 ph->count >= 16 + pool_cache_count / 8 &&
516 pool_cache_bytes > CONFIG_HAP_POOL_CACHE_SIZE * 3 / 4)) {
Willy Tarreau43937e92022-01-02 17:24:55 +0100517 pool_evict_last_items(pool, ph, CONFIG_HAP_POOL_CLUSTER_SIZE);
Willy Tarreau87212032021-04-19 08:14:03 +0200518 }
519}
520
Willy Tarreaub8498e92021-04-18 10:23:02 +0200521/* Evicts some of the oldest objects from the local cache, pushing them to the
Willy Tarreaue9816312022-02-22 16:23:09 +0100522 * global pool. Must not be used when pools are disabled.
Willy Tarreaub8498e92021-04-18 10:23:02 +0200523 */
524void pool_evict_from_local_caches()
525{
526 struct pool_cache_item *item;
527 struct pool_cache_head *ph;
528 struct pool_head *pool;
529
Willy Tarreaue9816312022-02-22 16:23:09 +0100530 BUG_ON(pool_debugging & POOL_DBG_NO_CACHE);
531
Willy Tarreaub8498e92021-04-18 10:23:02 +0200532 do {
Willy Tarreaub4e34762021-09-30 19:02:18 +0200533 item = LIST_PREV(&th_ctx->pool_lru_head, struct pool_cache_item *, by_lru);
Willy Tarreaue2830ad2022-02-09 16:23:55 +0100534 BUG_ON(&item->by_lru == &th_ctx->pool_lru_head);
Willy Tarreaub8498e92021-04-18 10:23:02 +0200535 /* note: by definition we remove oldest objects so they also are the
536 * oldest in their own pools, thus their next is the pool's head.
537 */
538 ph = LIST_NEXT(&item->by_pool, struct pool_cache_head *, list);
Willy Tarreau49bb5d42022-02-09 16:33:22 +0100539 BUG_ON(ph->tid != tid);
540
Willy Tarreaub8498e92021-04-18 10:23:02 +0200541 pool = container_of(ph - tid, struct pool_head, cache);
Willy Tarreau49bb5d42022-02-09 16:33:22 +0100542 BUG_ON(pool != ph->pool);
543
Willy Tarreau43937e92022-01-02 17:24:55 +0100544 pool_evict_last_items(pool, ph, CONFIG_HAP_POOL_CLUSTER_SIZE);
Willy Tarreaub8498e92021-04-18 10:23:02 +0200545 } while (pool_cache_bytes > CONFIG_HAP_POOL_CACHE_SIZE * 7 / 8);
546}
Willy Tarreau0bae0752021-03-02 20:05:09 +0100547
Willy Tarreaub2a853d2021-04-19 11:49:26 +0200548/* Frees an object to the local cache, possibly pushing oldest objects to the
549 * shared cache, which itself may decide to release some of them to the OS.
550 * While it is unspecified what the object becomes past this point, it is
Willy Tarreauadd43fa2022-01-24 15:52:51 +0100551 * guaranteed to be released from the users' perpective. A caller address may
Willy Tarreaue9816312022-02-22 16:23:09 +0100552 * be passed and stored into the area when DEBUG_POOL_TRACING is set. Must not
553 * be used with pools disabled.
Willy Tarreaub2a853d2021-04-19 11:49:26 +0200554 */
Willy Tarreau0e2a5b42022-01-24 15:51:50 +0100555void pool_put_to_cache(struct pool_head *pool, void *ptr, const void *caller)
Willy Tarreaub2a853d2021-04-19 11:49:26 +0200556{
557 struct pool_cache_item *item = (struct pool_cache_item *)ptr;
558 struct pool_cache_head *ph = &pool->cache[tid];
559
Willy Tarreaue9816312022-02-22 16:23:09 +0100560 BUG_ON(pool_debugging & POOL_DBG_NO_CACHE);
561
Willy Tarreau2b718102021-04-21 07:32:39 +0200562 LIST_INSERT(&ph->list, &item->by_pool);
Willy Tarreaub4e34762021-09-30 19:02:18 +0200563 LIST_INSERT(&th_ctx->pool_lru_head, &item->by_lru);
Willy Tarreauadd43fa2022-01-24 15:52:51 +0100564 POOL_DEBUG_TRACE_CALLER(pool, item, caller);
Willy Tarreaub2a853d2021-04-19 11:49:26 +0200565 ph->count++;
Willy Tarreau6f3c7f62022-02-21 18:42:53 +0100566 if (unlikely(pool_debugging & POOL_DBG_INTEGRITY))
567 pool_fill_pattern(ph, item, pool->size);
Willy Tarreaub2a853d2021-04-19 11:49:26 +0200568 pool_cache_count++;
569 pool_cache_bytes += pool->size;
570
571 if (unlikely(pool_cache_bytes > CONFIG_HAP_POOL_CACHE_SIZE * 3 / 4)) {
Willy Tarreau43937e92022-01-02 17:24:55 +0100572 if (ph->count >= 16 + pool_cache_count / 8 + CONFIG_HAP_POOL_CLUSTER_SIZE)
Willy Tarreauc895c442022-02-09 16:19:24 +0100573 pool_evict_from_local_cache(pool, 0);
Willy Tarreaub2a853d2021-04-19 11:49:26 +0200574 if (pool_cache_bytes > CONFIG_HAP_POOL_CACHE_SIZE)
575 pool_evict_from_local_caches();
576 }
577}
578
Willy Tarreauafe2c4a2021-12-30 17:09:31 +0100579/* Tries to refill the local cache <pch> from the shared one for pool <pool>.
580 * This is only used when pools are in use and shared pools are enabled. No
581 * malloc() is attempted, and poisonning is never performed. The purpose is to
582 * get the fastest possible refilling so that the caller can easily check if
Willy Tarreaue9816312022-02-22 16:23:09 +0100583 * the cache has enough objects for its use. Must not be used when pools are
584 * disabled.
Willy Tarreauafe2c4a2021-12-30 17:09:31 +0100585 */
586void pool_refill_local_from_shared(struct pool_head *pool, struct pool_cache_head *pch)
587{
588 struct pool_cache_item *item;
Willy Tarreau148160b2022-01-02 14:35:57 +0100589 struct pool_item *ret, *down;
590 uint count;
Willy Tarreauafe2c4a2021-12-30 17:09:31 +0100591
Willy Tarreaue9816312022-02-22 16:23:09 +0100592 BUG_ON(pool_debugging & POOL_DBG_NO_CACHE);
593
Willy Tarreauafe2c4a2021-12-30 17:09:31 +0100594 /* we'll need to reference the first element to figure the next one. We
595 * must temporarily lock it so that nobody allocates then releases it,
596 * or the dereference could fail.
597 */
598 ret = _HA_ATOMIC_LOAD(&pool->free_list);
599 do {
600 while (unlikely(ret == POOL_BUSY)) {
601 __ha_cpu_relax();
602 ret = _HA_ATOMIC_LOAD(&pool->free_list);
603 }
604 if (ret == NULL)
605 return;
606 } while (unlikely((ret = _HA_ATOMIC_XCHG(&pool->free_list, POOL_BUSY)) == POOL_BUSY));
607
608 if (unlikely(ret == NULL)) {
609 HA_ATOMIC_STORE(&pool->free_list, NULL);
610 return;
611 }
612
613 /* this releases the lock */
Willy Tarreauc16ed3b2022-01-01 18:22:20 +0100614 HA_ATOMIC_STORE(&pool->free_list, ret->next);
Willy Tarreauafe2c4a2021-12-30 17:09:31 +0100615
Willy Tarreau148160b2022-01-02 14:35:57 +0100616 /* now store the retrieved object(s) into the local cache */
617 count = 0;
618 for (; ret; ret = down) {
619 down = ret->down;
Willy Tarreau148160b2022-01-02 14:35:57 +0100620 item = (struct pool_cache_item *)ret;
Willy Tarreauf70fdde2022-01-25 15:56:50 +0100621 POOL_DEBUG_TRACE_CALLER(pool, item, NULL);
Willy Tarreau148160b2022-01-02 14:35:57 +0100622 LIST_INSERT(&pch->list, &item->by_pool);
623 LIST_INSERT(&th_ctx->pool_lru_head, &item->by_lru);
624 count++;
Willy Tarreau6f3c7f62022-02-21 18:42:53 +0100625 if (unlikely(pool_debugging & POOL_DBG_INTEGRITY))
626 pool_fill_pattern(pch, item, pool->size);
Willy Tarreau148160b2022-01-02 14:35:57 +0100627 }
628 HA_ATOMIC_ADD(&pool->used, count);
629 pch->count += count;
630 pool_cache_count += count;
631 pool_cache_bytes += count * pool->size;
Willy Tarreauafe2c4a2021-12-30 17:09:31 +0100632}
633
Willy Tarreau337410c2022-01-02 15:15:54 +0100634/* Adds pool item cluster <item> to the shared cache, which contains <count>
635 * elements. The caller is advised to first check using pool_releasable() if
636 * it's wise to add this series of objects there. Both the pool and the item's
637 * head must be valid.
Willy Tarreaub46674a2021-12-30 17:37:33 +0100638 */
Willy Tarreau337410c2022-01-02 15:15:54 +0100639void pool_put_to_shared_cache(struct pool_head *pool, struct pool_item *item, uint count)
Willy Tarreaub46674a2021-12-30 17:37:33 +0100640{
Willy Tarreauc16ed3b2022-01-01 18:22:20 +0100641 struct pool_item *free_list;
Willy Tarreaub46674a2021-12-30 17:37:33 +0100642
Willy Tarreau337410c2022-01-02 15:15:54 +0100643 _HA_ATOMIC_SUB(&pool->used, count);
Willy Tarreaub46674a2021-12-30 17:37:33 +0100644 free_list = _HA_ATOMIC_LOAD(&pool->free_list);
645 do {
646 while (unlikely(free_list == POOL_BUSY)) {
647 __ha_cpu_relax();
648 free_list = _HA_ATOMIC_LOAD(&pool->free_list);
649 }
Willy Tarreauc16ed3b2022-01-01 18:22:20 +0100650 _HA_ATOMIC_STORE(&item->next, free_list);
Willy Tarreaub46674a2021-12-30 17:37:33 +0100651 __ha_barrier_atomic_store();
652 } while (!_HA_ATOMIC_CAS(&pool->free_list, &free_list, item));
653 __ha_barrier_atomic_store();
654 swrate_add(&pool->needed_avg, POOL_AVG_SAMPLES, pool->used);
655}
656
Olivier Houchardcf975d42018-01-24 18:38:31 +0100657/*
658 * This function frees whatever can be freed in pool <pool>.
659 */
660void pool_flush(struct pool_head *pool)
661{
Willy Tarreau148160b2022-01-02 14:35:57 +0100662 struct pool_item *next, *temp, *down;
Olivier Houchardcf975d42018-01-24 18:38:31 +0100663
Willy Tarreaue9816312022-02-22 16:23:09 +0100664 if (!pool || (pool_debugging & (POOL_DBG_NO_CACHE|POOL_DBG_NO_GLOBAL)))
Olivier Houchardcf975d42018-01-24 18:38:31 +0100665 return;
Willy Tarreau2a4523f2021-06-09 18:59:58 +0200666
667 /* The loop below atomically detaches the head of the free list and
668 * replaces it with a NULL. Then the list can be released.
669 */
670 next = pool->free_list;
Olivier Houchardcf975d42018-01-24 18:38:31 +0100671 do {
Willy Tarreau2a4523f2021-06-09 18:59:58 +0200672 while (unlikely(next == POOL_BUSY)) {
673 __ha_cpu_relax();
674 next = _HA_ATOMIC_LOAD(&pool->free_list);
675 }
676 if (next == NULL)
677 return;
678 } while (unlikely((next = _HA_ATOMIC_XCHG(&pool->free_list, POOL_BUSY)) == POOL_BUSY));
679 _HA_ATOMIC_STORE(&pool->free_list, NULL);
Olivier Houchard20872762019-03-08 18:53:35 +0100680 __ha_barrier_atomic_store();
Willy Tarreau2a4523f2021-06-09 18:59:58 +0200681
Olivier Houchardcf975d42018-01-24 18:38:31 +0100682 while (next) {
683 temp = next;
Willy Tarreauc16ed3b2022-01-01 18:22:20 +0100684 next = temp->next;
Willy Tarreau148160b2022-01-02 14:35:57 +0100685 for (; temp; temp = down) {
686 down = temp->down;
687 pool_put_to_os(pool, temp);
688 }
Olivier Houchardcf975d42018-01-24 18:38:31 +0100689 }
Willy Tarreauc239cde2021-06-10 06:54:22 +0200690 /* here, we should have pool->allocated == pool->used */
Olivier Houchardcf975d42018-01-24 18:38:31 +0100691}
692
Willy Tarreaue6ce59d2007-05-13 19:38:49 +0200693/*
Willy Tarreaue6ce59d2007-05-13 19:38:49 +0200694 * This function frees whatever can be freed in all pools, but respecting
Willy Tarreauc0e2ff22020-04-24 06:15:24 +0200695 * the minimum thresholds imposed by owners. It makes sure to be alone to
696 * run by using thread_isolate(). <pool_ctx> is unused.
Willy Tarreaue6ce59d2007-05-13 19:38:49 +0200697 */
Willy Tarreaubafbe012017-11-24 17:34:44 +0100698void pool_gc(struct pool_head *pool_ctx)
Willy Tarreaue6ce59d2007-05-13 19:38:49 +0200699{
700 struct pool_head *entry;
Willy Tarreauc0e2ff22020-04-24 06:15:24 +0200701 int isolated = thread_isolated();
Willy Tarreaub7f9d122009-04-21 02:17:45 +0200702
Willy Tarreauc0e2ff22020-04-24 06:15:24 +0200703 if (!isolated)
704 thread_isolate();
Willy Tarreaub7f9d122009-04-21 02:17:45 +0200705
Willy Tarreaue6ce59d2007-05-13 19:38:49 +0200706 list_for_each_entry(entry, &pools, list) {
Willy Tarreau148160b2022-01-02 14:35:57 +0100707 struct pool_item *temp, *down;
Willy Tarreauc16ed3b2022-01-01 18:22:20 +0100708
Olivier Houchard51d93392020-03-12 19:05:39 +0100709 while (entry->free_list &&
Willy Tarreau57767b82014-12-22 21:40:55 +0100710 (int)(entry->allocated - entry->used) > (int)entry->minavail) {
Olivier Houchard51d93392020-03-12 19:05:39 +0100711 temp = entry->free_list;
Willy Tarreauc16ed3b2022-01-01 18:22:20 +0100712 entry->free_list = temp->next;
Willy Tarreau148160b2022-01-02 14:35:57 +0100713 for (; temp; temp = down) {
714 down = temp->down;
715 pool_put_to_os(entry, temp);
716 }
Willy Tarreaue6ce59d2007-05-13 19:38:49 +0200717 }
Willy Tarreaue6ce59d2007-05-13 19:38:49 +0200718 }
Christopher Fauletb349e482017-08-29 09:52:38 +0200719
Willy Tarreauea3323f2021-09-15 10:38:21 +0200720 trim_all_pools();
Willy Tarreau26ed1832021-06-10 08:40:16 +0200721
Willy Tarreauc0e2ff22020-04-24 06:15:24 +0200722 if (!isolated)
723 thread_release();
Willy Tarreaue6ce59d2007-05-13 19:38:49 +0200724}
Willy Tarreaub8498e92021-04-18 10:23:02 +0200725
Willy Tarreau15c322c2022-01-24 11:51:43 +0100726/*
Willy Tarreaud3929732022-01-24 16:09:29 +0100727 * Returns a pointer to type <type> taken from the pool <pool_type> or
728 * dynamically allocated. In the first case, <pool_type> is updated to point to
729 * the next element in the list. <flags> is a binary-OR of POOL_F_* flags.
730 * Prefer using pool_alloc() which does the right thing without flags.
731 */
732void *__pool_alloc(struct pool_head *pool, unsigned int flags)
733{
734 void *p = NULL;
Willy Tarreau02718222022-02-23 10:10:33 +0100735 void *caller = __builtin_return_address(0);
Willy Tarreaud3929732022-01-24 16:09:29 +0100736
Willy Tarreau8d0273e2022-02-21 17:16:22 +0100737 if (unlikely(pool_debugging & POOL_DBG_FAIL_ALLOC))
738 if (!(flags & POOL_F_NO_FAIL) && mem_should_fail(pool))
739 return NULL;
Willy Tarreaud3929732022-01-24 16:09:29 +0100740
Willy Tarreaue9816312022-02-22 16:23:09 +0100741 if (likely(!(pool_debugging & POOL_DBG_NO_CACHE)) && !p)
Willy Tarreau0e2a5b42022-01-24 15:51:50 +0100742 p = pool_get_from_cache(pool, caller);
Willy Tarreaue9816312022-02-22 16:23:09 +0100743
Willy Tarreaud3929732022-01-24 16:09:29 +0100744 if (unlikely(!p))
745 p = pool_alloc_nocache(pool);
746
747 if (likely(p)) {
Willy Tarreaufacfad22022-08-17 09:12:53 +0200748#ifdef USE_MEMORY_PROFILING
749 if (unlikely(profiling & HA_PROF_MEMORY)) {
750 struct memprof_stats *bin;
751
752 bin = memprof_get_bin(__builtin_return_address(0), MEMPROF_METH_P_ALLOC);
753 _HA_ATOMIC_ADD(&bin->alloc_calls, 1);
754 _HA_ATOMIC_ADD(&bin->alloc_tot, pool->size);
Willy Tarreau42b180d2022-08-17 09:35:16 +0200755 _HA_ATOMIC_STORE(&bin->info, pool);
Willy Tarreaufacfad22022-08-17 09:12:53 +0200756 }
757#endif
Willy Tarreaud3929732022-01-24 16:09:29 +0100758 if (unlikely(flags & POOL_F_MUST_ZERO))
759 memset(p, 0, pool->size);
Willy Tarreauef301b72022-02-23 14:15:18 +0100760 else if (unlikely(!(flags & POOL_F_NO_POISON) && (pool_debugging & POOL_DBG_POISON)))
Willy Tarreaud3929732022-01-24 16:09:29 +0100761 memset(p, mem_poison_byte, pool->size);
762 }
763 return p;
764}
765
766/*
Willy Tarreau15c322c2022-01-24 11:51:43 +0100767 * Puts a memory area back to the corresponding pool. <ptr> be valid. Using
768 * pool_free() is preferred.
769 */
770void __pool_free(struct pool_head *pool, void *ptr)
771{
Willy Tarreau02718222022-02-23 10:10:33 +0100772 const void *caller = __builtin_return_address(0);
Willy Tarreau0e2a5b42022-01-24 15:51:50 +0100773
Willy Tarreau15c322c2022-01-24 11:51:43 +0100774 /* we'll get late corruption if we refill to the wrong pool or double-free */
775 POOL_DEBUG_CHECK_MARK(pool, ptr);
Willy Tarreau27c8da12022-02-09 16:49:16 +0100776 POOL_DEBUG_RESET_MARK(pool, ptr);
Willy Tarreaue9816312022-02-22 16:23:09 +0100777
Willy Tarreaufacfad22022-08-17 09:12:53 +0200778#ifdef USE_MEMORY_PROFILING
779 if (unlikely(profiling & HA_PROF_MEMORY) && ptr) {
780 struct memprof_stats *bin;
781
782 bin = memprof_get_bin(__builtin_return_address(0), MEMPROF_METH_P_FREE);
783 _HA_ATOMIC_ADD(&bin->free_calls, 1);
784 _HA_ATOMIC_ADD(&bin->free_tot, pool->size);
Willy Tarreau42b180d2022-08-17 09:35:16 +0200785 _HA_ATOMIC_STORE(&bin->info, pool);
Willy Tarreaufacfad22022-08-17 09:12:53 +0200786 }
787#endif
788
Willy Tarreaue9816312022-02-22 16:23:09 +0100789 if (unlikely(pool_debugging & POOL_DBG_NO_CACHE)) {
790 pool_free_nocache(pool, ptr);
791 return;
792 }
793
Willy Tarreau0e2a5b42022-01-24 15:51:50 +0100794 pool_put_to_cache(pool, ptr, caller);
Willy Tarreau15c322c2022-01-24 11:51:43 +0100795}
796
Willy Tarreauf14d1902021-10-05 18:14:11 +0200797
798#ifdef DEBUG_UAF
799
800/************* use-after-free allocator *************/
801
802/* allocates an area of size <size> and returns it. The semantics are similar
803 * to those of malloc(). However the allocation is rounded up to 4kB so that a
804 * full page is allocated. This ensures the object can be freed alone so that
805 * future dereferences are easily detected. The returned object is always
806 * 16-bytes aligned to avoid issues with unaligned structure objects. In case
807 * some padding is added, the area's start address is copied at the end of the
808 * padding to help detect underflows.
809 */
810void *pool_alloc_area_uaf(size_t size)
811{
812 size_t pad = (4096 - size) & 0xFF0;
Willy Tarreauf14d1902021-10-05 18:14:11 +0200813 void *ret;
814
Willy Tarreauf14d1902021-10-05 18:14:11 +0200815 ret = mmap(NULL, (size + 4095) & -4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
816 if (ret != MAP_FAILED) {
817 /* let's dereference the page before returning so that the real
818 * allocation in the system is performed without holding the lock.
819 */
820 *(int *)ret = 0;
821 if (pad >= sizeof(void *))
822 *(void **)(ret + pad - sizeof(void *)) = ret + pad;
823 ret += pad;
824 } else {
825 ret = NULL;
826 }
Willy Tarreauf14d1902021-10-05 18:14:11 +0200827 return ret;
828}
829
830/* frees an area <area> of size <size> allocated by pool_alloc_area(). The
831 * semantics are identical to free() except that the size must absolutely match
832 * the one passed to pool_alloc_area(). In case some padding is added, the
833 * area's start address is compared to the one at the end of the padding, and
834 * a segfault is triggered if they don't match, indicating an underflow.
835 */
836void pool_free_area_uaf(void *area, size_t size)
837{
838 size_t pad = (4096 - size) & 0xFF0;
839
840 if (pad >= sizeof(void *) && *(void **)(area - sizeof(void *)) != area)
841 ABORT_NOW();
842
Willy Tarreauf14d1902021-10-05 18:14:11 +0200843 munmap(area - pad, (size + 4095) & -4096);
Willy Tarreauf14d1902021-10-05 18:14:11 +0200844}
845
846#endif /* DEBUG_UAF */
847
Willy Tarreaue6ce59d2007-05-13 19:38:49 +0200848/*
Willy Tarreaudae4aa82007-06-16 23:19:53 +0200849 * This function destroys a pool by freeing it completely, unless it's still
850 * in use. This should be called only under extreme circumstances. It always
851 * returns NULL if the resulting pool is empty, easing the clearing of the old
852 * pointer, otherwise it returns the pool.
853 * .
Willy Tarreaue6ce59d2007-05-13 19:38:49 +0200854 */
Willy Tarreaubafbe012017-11-24 17:34:44 +0100855void *pool_destroy(struct pool_head *pool)
Willy Tarreaue6ce59d2007-05-13 19:38:49 +0200856{
Willy Tarreau4d2d0982007-05-14 00:39:29 +0200857 if (pool) {
Willy Tarreaue9816312022-02-22 16:23:09 +0100858 if (!(pool_debugging & POOL_DBG_NO_CACHE))
859 pool_evict_from_local_cache(pool, 1);
860
Willy Tarreaubafbe012017-11-24 17:34:44 +0100861 pool_flush(pool);
Willy Tarreaudae4aa82007-06-16 23:19:53 +0200862 if (pool->used)
863 return pool;
864 pool->users--;
865 if (!pool->users) {
Willy Tarreau2b718102021-04-21 07:32:39 +0200866 LIST_DELETE(&pool->list);
Willy Tarreau9f3129e2021-04-17 00:31:38 +0200867 /* note that if used == 0, the cache is empty */
Willy Tarreauf9eba782022-03-03 18:31:54 +0100868 free(pool->base_addr);
Willy Tarreaudae4aa82007-06-16 23:19:53 +0200869 }
Willy Tarreau4d2d0982007-05-14 00:39:29 +0200870 }
871 return NULL;
Willy Tarreaue6ce59d2007-05-13 19:38:49 +0200872}
873
Willy Tarreau2455ceb2018-11-26 15:57:34 +0100874/* This destroys all pools on exit. It is *not* thread safe. */
875void pool_destroy_all()
876{
877 struct pool_head *entry, *back;
878
Willy Tarreau243e68b2022-04-27 11:33:13 +0200879 list_for_each_entry_safe(entry, back, &pools, list) {
880 /* there's only one occurrence of each pool in the list,
881 * and we're existing instead of looping on the whole
882 * list just to decrement users, force it to 1 here.
883 */
884 entry->users = 1;
Willy Tarreau2455ceb2018-11-26 15:57:34 +0100885 pool_destroy(entry);
Willy Tarreau243e68b2022-04-27 11:33:13 +0200886 }
Willy Tarreau2455ceb2018-11-26 15:57:34 +0100887}
888
Willy Tarreau2fba08f2022-11-21 09:34:02 +0100889/* used by qsort in "show pools" to sort by name */
890static int cmp_dump_pools_name(const void *a, const void *b)
891{
892 const struct pool_dump_info *l = (const struct pool_dump_info *)a;
893 const struct pool_dump_info *r = (const struct pool_dump_info *)b;
894
895 return strcmp(l->entry->name, r->entry->name);
896}
897
898/* used by qsort in "show pools" to sort by item size */
899static int cmp_dump_pools_size(const void *a, const void *b)
900{
901 const struct pool_dump_info *l = (const struct pool_dump_info *)a;
902 const struct pool_dump_info *r = (const struct pool_dump_info *)b;
903
904 if (l->entry->size > r->entry->size)
905 return -1;
906 else if (l->entry->size < r->entry->size)
907 return 1;
908 else
909 return 0;
910}
911
912/* used by qsort in "show pools" to sort by usage */
913static int cmp_dump_pools_usage(const void *a, const void *b)
914{
915 const struct pool_dump_info *l = (const struct pool_dump_info *)a;
916 const struct pool_dump_info *r = (const struct pool_dump_info *)b;
917
918 if (l->alloc_bytes > r->alloc_bytes)
919 return -1;
920 else if (l->alloc_bytes < r->alloc_bytes)
921 return 1;
922 else
923 return 0;
924}
925
Willy Tarreau224adf22022-11-21 09:02:41 +0100926/* will not dump more than this number of entries. Anything beyond this will
927 * likely not fit into a regular output buffer anyway.
928 */
929#define POOLS_MAX_DUMPED_ENTRIES 1024
930
Willy Tarreau2fba08f2022-11-21 09:34:02 +0100931/* This function dumps memory usage information into the trash buffer.
932 * It may sort by a criterion if <by_what> is non-zero, and limit the
Willy Tarreau7583c362022-11-21 10:02:29 +0100933 * number of output lines if <max> is non-zero. It may limit only to
934 * pools whose names start with <pfx> if <pfx> is non-null.
Willy Tarreau2fba08f2022-11-21 09:34:02 +0100935 */
Willy Tarreau7583c362022-11-21 10:02:29 +0100936void dump_pools_to_trash(int by_what, int max, const char *pfx)
Willy Tarreau50e608d2007-05-13 18:26:08 +0200937{
Willy Tarreau224adf22022-11-21 09:02:41 +0100938 struct pool_dump_info pool_info[POOLS_MAX_DUMPED_ENTRIES];
Willy Tarreau50e608d2007-05-13 18:26:08 +0200939 struct pool_head *entry;
Willy Tarreau0c5e9892022-11-17 11:08:03 +0100940 unsigned long long allocated, used;
Willy Tarreau224adf22022-11-21 09:02:41 +0100941 int nbpools, i;
Willy Tarreau0c5e9892022-11-17 11:08:03 +0100942 unsigned long long cached_bytes = 0;
Willy Tarreau1b4a7142021-10-07 16:29:31 +0200943 uint cached = 0;
Willy Tarreau50e608d2007-05-13 18:26:08 +0200944
945 allocated = used = nbpools = 0;
Willy Tarreau224adf22022-11-21 09:02:41 +0100946
Willy Tarreau50e608d2007-05-13 18:26:08 +0200947 list_for_each_entry(entry, &pools, list) {
Willy Tarreau224adf22022-11-21 09:02:41 +0100948 if (nbpools >= POOLS_MAX_DUMPED_ENTRIES)
949 break;
950
Willy Tarreau2fba08f2022-11-21 09:34:02 +0100951 /* do not dump unused entries when sorting by usage */
952 if (by_what == 3 && !entry->allocated)
953 continue;
954
Willy Tarreau7583c362022-11-21 10:02:29 +0100955 /* verify the pool name if a prefix is requested */
956 if (pfx && strncmp(entry->name, pfx, strlen(pfx)) != 0)
957 continue;
958
Willy Tarreaue9816312022-02-22 16:23:09 +0100959 if (!(pool_debugging & POOL_DBG_NO_CACHE)) {
Willy Tarreaue9816312022-02-22 16:23:09 +0100960 for (cached = i = 0; i < global.nbthread; i++)
961 cached += entry->cache[i].count;
Willy Tarreaue9816312022-02-22 16:23:09 +0100962 }
Willy Tarreau224adf22022-11-21 09:02:41 +0100963 pool_info[nbpools].entry = entry;
964 pool_info[nbpools].alloc_items = entry->allocated;
965 pool_info[nbpools].alloc_bytes = (ulong)entry->size * entry->allocated;
966 pool_info[nbpools].used_items = entry->used;
967 pool_info[nbpools].cached_items = cached;
968 pool_info[nbpools].need_avg = swrate_avg(entry->needed_avg, POOL_AVG_SAMPLES);
969 pool_info[nbpools].failed_items = entry->failed;
Willy Tarreau50e608d2007-05-13 18:26:08 +0200970 nbpools++;
971 }
Willy Tarreau224adf22022-11-21 09:02:41 +0100972
Willy Tarreau2fba08f2022-11-21 09:34:02 +0100973 if (by_what == 1) /* sort by name */
974 qsort(pool_info, nbpools, sizeof(pool_info[0]), cmp_dump_pools_name);
975 else if (by_what == 2) /* sort by item size */
976 qsort(pool_info, nbpools, sizeof(pool_info[0]), cmp_dump_pools_size);
977 else if (by_what == 3) /* sort by total usage */
978 qsort(pool_info, nbpools, sizeof(pool_info[0]), cmp_dump_pools_usage);
979
Willy Tarreau224adf22022-11-21 09:02:41 +0100980 chunk_printf(&trash, "Dumping pools usage");
Willy Tarreau2fba08f2022-11-21 09:34:02 +0100981 if (!max || max >= POOLS_MAX_DUMPED_ENTRIES)
982 max = POOLS_MAX_DUMPED_ENTRIES;
983 if (nbpools >= max)
984 chunk_appendf(&trash, " (limited to the first %u entries)", max);
Willy Tarreau224adf22022-11-21 09:02:41 +0100985 chunk_appendf(&trash, ". Use SIGQUIT to flush them.\n");
986
Willy Tarreau2fba08f2022-11-21 09:34:02 +0100987 for (i = 0; i < nbpools && i < max; i++) {
Willy Tarreau224adf22022-11-21 09:02:41 +0100988 chunk_appendf(&trash, " - Pool %s (%lu bytes) : %lu allocated (%lu bytes), %lu used"
989 " (~%lu by thread caches)"
990 ", needed_avg %lu, %lu failures, %u users, @%p%s\n",
991 pool_info[i].entry->name, (ulong)pool_info[i].entry->size,
992 pool_info[i].alloc_items, pool_info[i].alloc_bytes,
993 pool_info[i].used_items, pool_info[i].cached_items,
994 pool_info[i].need_avg, pool_info[i].failed_items,
995 pool_info[i].entry->users, pool_info[i].entry,
996 (pool_info[i].entry->flags & MEM_F_SHARED) ? " [SHARED]" : "");
997
998 cached_bytes += pool_info[i].cached_items * (ulong)pool_info[i].entry->size;
999 allocated += pool_info[i].alloc_items * (ulong)pool_info[i].entry->size;
1000 used += pool_info[i].used_items * (ulong)pool_info[i].entry->size;
1001 }
1002
Willy Tarreau0c5e9892022-11-17 11:08:03 +01001003 chunk_appendf(&trash, "Total: %d pools, %llu bytes allocated, %llu used"
1004 " (~%llu by thread caches)"
Willy Tarreau1b4a7142021-10-07 16:29:31 +02001005 ".\n",
Willy Tarreaue9816312022-02-22 16:23:09 +01001006 nbpools, allocated, used, cached_bytes
Willy Tarreau1b4a7142021-10-07 16:29:31 +02001007 );
Willy Tarreau50e608d2007-05-13 18:26:08 +02001008}
1009
Willy Tarreau12833bb2014-01-28 16:49:56 +01001010/* Dump statistics on pools usage. */
1011void dump_pools(void)
1012{
Willy Tarreau7583c362022-11-21 10:02:29 +01001013 dump_pools_to_trash(0, 0, NULL);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001014 qfprintf(stderr, "%s", trash.area);
Willy Tarreau12833bb2014-01-28 16:49:56 +01001015}
1016
Willy Tarreau58102cf2015-10-28 16:24:21 +01001017/* This function returns the total number of failed pool allocations */
1018int pool_total_failures()
1019{
1020 struct pool_head *entry;
1021 int failed = 0;
1022
1023 list_for_each_entry(entry, &pools, list)
1024 failed += entry->failed;
1025 return failed;
1026}
1027
1028/* This function returns the total amount of memory allocated in pools (in bytes) */
1029unsigned long pool_total_allocated()
1030{
1031 struct pool_head *entry;
1032 unsigned long allocated = 0;
1033
1034 list_for_each_entry(entry, &pools, list)
1035 allocated += entry->allocated * entry->size;
1036 return allocated;
1037}
1038
1039/* This function returns the total amount of memory used in pools (in bytes) */
1040unsigned long pool_total_used()
1041{
1042 struct pool_head *entry;
1043 unsigned long used = 0;
1044
1045 list_for_each_entry(entry, &pools, list)
1046 used += entry->used * entry->size;
1047 return used;
1048}
1049
Willy Tarreau1408b1f2022-02-18 18:54:40 +01001050/* This function parses a string made of a set of debugging features as
1051 * specified after -dM on the command line, and will set pool_debugging
1052 * accordingly. On success it returns a strictly positive value. It may zero
1053 * with the first warning in <err>, -1 with a help message in <err>, or -2 with
1054 * the first error in <err> return the first error in <err>. <err> is undefined
1055 * on success, and will be non-null and locally allocated on help/error/warning.
1056 * The caller must free it. Warnings are used to report features that were not
1057 * enabled at build time, and errors are used to report unknown features.
1058 */
1059int pool_parse_debugging(const char *str, char **err)
1060{
Willy Tarreauf4b79c42022-02-23 15:20:53 +01001061 struct ist args;
Willy Tarreau1408b1f2022-02-18 18:54:40 +01001062 char *end;
Willy Tarreauf4b79c42022-02-23 15:20:53 +01001063 uint new_dbg;
Willy Tarreau1408b1f2022-02-18 18:54:40 +01001064 int v;
1065
1066
1067 /* if it's empty or starts with a number, it's the mem poisonning byte */
1068 v = strtol(str, &end, 0);
1069 if (!*end || *end == ',') {
1070 mem_poison_byte = *str ? v : 'P';
1071 if (mem_poison_byte >= 0)
1072 pool_debugging |= POOL_DBG_POISON;
1073 else
1074 pool_debugging &= ~POOL_DBG_POISON;
1075 str = end;
1076 }
Willy Tarreauf4b79c42022-02-23 15:20:53 +01001077
1078 new_dbg = pool_debugging;
1079
1080 for (args = ist(str); istlen(args); args = istadv(istfind(args, ','), 1)) {
1081 struct ist feat = iststop(args, ',');
1082
1083 if (!istlen(feat))
1084 continue;
1085
1086 if (isteq(feat, ist("help"))) {
1087 ha_free(err);
1088 memprintf(err,
1089 "-dM alone enables memory poisonning with byte 0x50 on allocation. A numeric\n"
1090 "value may be appended immediately after -dM to use another value (0 supported).\n"
1091 "Then an optional list of comma-delimited keywords may be appended to set or\n"
1092 "clear some debugging options ('*' marks the current setting):\n\n"
1093 " set clear description\n"
1094 " -----------------+-----------------+-----------------------------------------\n");
1095
1096 for (v = 0; dbg_options[v].flg; v++) {
1097 memprintf(err, "%s %c %-15s|%c %-15s| %s\n",
1098 *err,
1099 (pool_debugging & dbg_options[v].flg) ? '*' : ' ',
1100 dbg_options[v].set,
1101 (pool_debugging & dbg_options[v].flg) ? ' ' : '*',
1102 dbg_options[v].clr,
1103 dbg_options[v].hlp);
1104 }
1105 return -1;
1106 }
1107
1108 for (v = 0; dbg_options[v].flg; v++) {
1109 if (isteq(feat, ist(dbg_options[v].set))) {
1110 new_dbg |= dbg_options[v].flg;
1111 break;
1112 }
1113 else if (isteq(feat, ist(dbg_options[v].clr))) {
1114 new_dbg &= ~dbg_options[v].flg;
1115 break;
1116 }
1117 }
1118
1119 if (!dbg_options[v].flg) {
1120 memprintf(err, "unknown pool debugging feature <%.*s>", (int)istlen(feat), istptr(feat));
1121 return -2;
1122 }
1123 }
1124
1125 pool_debugging = new_dbg;
Willy Tarreau1408b1f2022-02-18 18:54:40 +01001126 return 1;
1127}
1128
Willy Tarreau2fba08f2022-11-21 09:34:02 +01001129/* parse a "show pools" command. It returns 1 on failure, 0 if it starts to dump. */
1130static int cli_parse_show_pools(char **args, char *payload, struct appctx *appctx, void *private)
1131{
1132 struct show_pools_ctx *ctx = applet_reserve_svcctx(appctx, sizeof(*ctx));
1133 int arg;
1134
1135 for (arg = 2; *args[arg]; arg++) {
1136 if (strcmp(args[arg], "byname") == 0) {
1137 ctx->by_what = 1; // sort output by name
1138 }
1139 else if (strcmp(args[arg], "bysize") == 0) {
1140 ctx->by_what = 2; // sort output by item size
1141 }
1142 else if (strcmp(args[arg], "byusage") == 0) {
1143 ctx->by_what = 3; // sort output by total allocated size
1144 }
Willy Tarreau7583c362022-11-21 10:02:29 +01001145 else if (strcmp(args[arg], "match") == 0 && *args[arg+1]) {
1146 ctx->prefix = strdup(args[arg+1]); // only pools starting with this
1147 arg++;
1148 }
Willy Tarreau2fba08f2022-11-21 09:34:02 +01001149 else if (isdigit((unsigned char)*args[arg])) {
1150 ctx->maxcnt = atoi(args[arg]); // number of entries to dump
1151 }
1152 else
Willy Tarreau7583c362022-11-21 10:02:29 +01001153 return cli_err(appctx, "Expects either 'byname', 'bysize', 'byusage', 'match <pfx>', or a max number of output lines.\n");
Willy Tarreau2fba08f2022-11-21 09:34:02 +01001154 }
1155 return 0;
1156}
1157
Willy Tarreau7583c362022-11-21 10:02:29 +01001158/* release the "show pools" context */
1159static void cli_release_show_pools(struct appctx *appctx)
1160{
1161 struct show_pools_ctx *ctx = appctx->svcctx;
1162
1163 ha_free(&ctx->prefix);
1164}
1165
Willy Tarreau4596fe22022-05-17 19:07:51 +02001166/* This function dumps memory usage information onto the stream connector's
William Lallemande7ed8852016-11-19 02:25:36 +01001167 * read buffer. It returns 0 as long as it does not complete, non-zero upon
1168 * completion. No state is used.
1169 */
1170static int cli_io_handler_dump_pools(struct appctx *appctx)
1171{
Willy Tarreau2fba08f2022-11-21 09:34:02 +01001172 struct show_pools_ctx *ctx = appctx->svcctx;
1173
Willy Tarreau7583c362022-11-21 10:02:29 +01001174 dump_pools_to_trash(ctx->by_what, ctx->maxcnt, ctx->prefix);
Willy Tarreaud0a06d52022-05-18 15:07:19 +02001175 if (applet_putchk(appctx, &trash) == -1)
William Lallemande7ed8852016-11-19 02:25:36 +01001176 return 0;
William Lallemande7ed8852016-11-19 02:25:36 +01001177 return 1;
1178}
1179
Willy Tarreau7107c8b2018-11-26 11:44:35 +01001180/* callback used to create early pool <name> of size <size> and store the
1181 * resulting pointer into <ptr>. If the allocation fails, it quits with after
1182 * emitting an error message.
1183 */
1184void create_pool_callback(struct pool_head **ptr, char *name, unsigned int size)
1185{
1186 *ptr = create_pool(name, size, MEM_F_SHARED);
1187 if (!*ptr) {
1188 ha_alert("Failed to allocate pool '%s' of size %u : %s. Aborting.\n",
1189 name, size, strerror(errno));
1190 exit(1);
1191 }
1192}
1193
Willy Tarreau7f0165e2018-11-26 17:09:46 +01001194/* Initializes all per-thread arrays on startup */
1195static void init_pools()
1196{
Willy Tarreau9f3129e2021-04-17 00:31:38 +02001197 int thr;
Willy Tarreau7f0165e2018-11-26 17:09:46 +01001198
1199 for (thr = 0; thr < MAX_THREADS; thr++) {
Willy Tarreaub4e34762021-09-30 19:02:18 +02001200 LIST_INIT(&ha_thread_ctx[thr].pool_lru_head);
Willy Tarreau7f0165e2018-11-26 17:09:46 +01001201 }
Willy Tarreaue9816312022-02-22 16:23:09 +01001202
Willy Tarreau157e3932021-09-15 10:05:48 +02001203 detect_allocator();
Willy Tarreau7f0165e2018-11-26 17:09:46 +01001204}
1205
1206INITCALL0(STG_PREPARE, init_pools);
Willy Tarreau7107c8b2018-11-26 11:44:35 +01001207
Willy Tarreau845b5602021-09-15 10:41:24 +02001208/* Report in build options if trim is supported */
1209static void pools_register_build_options(void)
1210{
1211 if (is_trim_enabled()) {
1212 char *ptr = NULL;
1213 memprintf(&ptr, "Support for malloc_trim() is enabled.");
1214 hap_register_build_opts(ptr, 1);
1215 }
1216}
1217INITCALL0(STG_REGISTER, pools_register_build_options);
1218
William Lallemande7ed8852016-11-19 02:25:36 +01001219/* register cli keywords */
1220static struct cli_kw_list cli_kws = {{ },{
Willy Tarreau7583c362022-11-21 10:02:29 +01001221 { { "show", "pools", NULL }, "show pools [by*] [match <pfx>] [nb] : report information about the memory pools usage", cli_parse_show_pools, cli_io_handler_dump_pools, cli_release_show_pools },
William Lallemande7ed8852016-11-19 02:25:36 +01001222 {{},}
1223}};
1224
Willy Tarreau0108d902018-11-25 19:14:37 +01001225INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws);
William Lallemande7ed8852016-11-19 02:25:36 +01001226
Olivier Houcharddc21ff72019-01-29 15:20:16 +01001227
1228/* config parser for global "tune.fail-alloc" */
1229static int mem_parse_global_fail_alloc(char **args, int section_type, struct proxy *curpx,
Amaury Denoyelle3b1c9a32021-03-22 11:21:36 +01001230 const struct proxy *defpx, const char *file, int line,
1231 char **err)
Olivier Houcharddc21ff72019-01-29 15:20:16 +01001232{
1233 if (too_many_args(1, args, err, NULL))
1234 return -1;
1235 mem_fail_rate = atoi(args[1]);
1236 if (mem_fail_rate < 0 || mem_fail_rate > 100) {
1237 memprintf(err, "'%s' expects a numeric value between 0 and 100.", args[0]);
1238 return -1;
1239 }
1240 return 0;
1241}
Olivier Houcharddc21ff72019-01-29 15:20:16 +01001242
Willy Tarreauc4e56dc2022-03-08 10:41:40 +01001243/* config parser for global "no-memory-trimming" */
1244static int mem_parse_global_no_mem_trim(char **args, int section_type, struct proxy *curpx,
1245 const struct proxy *defpx, const char *file, int line,
1246 char **err)
1247{
1248 if (too_many_args(0, args, err, NULL))
1249 return -1;
1250 disable_trim = 1;
1251 return 0;
1252}
1253
Olivier Houcharddc21ff72019-01-29 15:20:16 +01001254/* register global config keywords */
1255static struct cfg_kw_list mem_cfg_kws = {ILH, {
Olivier Houcharddc21ff72019-01-29 15:20:16 +01001256 { CFG_GLOBAL, "tune.fail-alloc", mem_parse_global_fail_alloc },
Willy Tarreauc4e56dc2022-03-08 10:41:40 +01001257 { CFG_GLOBAL, "no-memory-trimming", mem_parse_global_no_mem_trim },
Olivier Houcharddc21ff72019-01-29 15:20:16 +01001258 { 0, NULL, NULL }
1259}};
1260
1261INITCALL1(STG_REGISTER, cfg_register_keywords, &mem_cfg_kws);
1262
Willy Tarreau50e608d2007-05-13 18:26:08 +02001263/*
1264 * Local variables:
1265 * c-indent-level: 8
1266 * c-basic-offset: 8
1267 * End:
1268 */