Willy Tarreau | 50e608d | 2007-05-13 18:26:08 +0200 | [diff] [blame] | 1 | /* |
| 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 Tarreau | f14d190 | 2021-10-05 18:14:11 +0200 | [diff] [blame] | 12 | |
| 13 | #include <sys/mman.h> |
Willy Tarreau | 7107c8b | 2018-11-26 11:44:35 +0100 | [diff] [blame] | 14 | #include <errno.h> |
Willy Tarreau | 50e608d | 2007-05-13 18:26:08 +0200 | [diff] [blame] | 15 | |
Willy Tarreau | 5d9ddc5 | 2021-10-06 19:54:09 +0200 | [diff] [blame] | 16 | #include <haproxy/activity.h> |
Willy Tarreau | 4c7e4b7 | 2020-05-27 12:58:42 +0200 | [diff] [blame] | 17 | #include <haproxy/api.h> |
Willy Tarreau | b255105 | 2020-06-09 09:07:15 +0200 | [diff] [blame] | 18 | #include <haproxy/applet-t.h> |
Willy Tarreau | 6be7849 | 2020-06-05 00:00:29 +0200 | [diff] [blame] | 19 | #include <haproxy/cfgparse.h> |
Willy Tarreau | f1d32c4 | 2020-06-04 21:07:02 +0200 | [diff] [blame] | 20 | #include <haproxy/channel.h> |
Willy Tarreau | 83487a8 | 2020-06-04 20:19:54 +0200 | [diff] [blame] | 21 | #include <haproxy/cli.h> |
Willy Tarreau | 36979d9 | 2020-06-05 17:27:29 +0200 | [diff] [blame] | 22 | #include <haproxy/errors.h> |
Willy Tarreau | f268ee8 | 2020-06-04 17:05:57 +0200 | [diff] [blame] | 23 | #include <haproxy/global.h> |
Willy Tarreau | 853b297 | 2020-05-27 18:01:47 +0200 | [diff] [blame] | 24 | #include <haproxy/list.h> |
Willy Tarreau | b255105 | 2020-06-09 09:07:15 +0200 | [diff] [blame] | 25 | #include <haproxy/pool.h> |
Willy Tarreau | 5edca2f | 2022-05-27 09:25:10 +0200 | [diff] [blame] | 26 | #include <haproxy/sc_strm.h> |
Willy Tarreau | 2eec9b5 | 2020-06-04 19:58:55 +0200 | [diff] [blame] | 27 | #include <haproxy/stats-t.h> |
Willy Tarreau | cb086c6 | 2022-05-27 09:47:12 +0200 | [diff] [blame] | 28 | #include <haproxy/stconn.h> |
Willy Tarreau | b255105 | 2020-06-09 09:07:15 +0200 | [diff] [blame] | 29 | #include <haproxy/thread.h> |
Willy Tarreau | 48fbcae | 2020-06-03 18:09:46 +0200 | [diff] [blame] | 30 | #include <haproxy/tools.h> |
Willy Tarreau | 50e608d | 2007-05-13 18:26:08 +0200 | [diff] [blame] | 31 | |
Willy Tarreau | 50e608d | 2007-05-13 18:26:08 +0200 | [diff] [blame] | 32 | |
Willy Tarreau | 7f0165e | 2018-11-26 17:09:46 +0100 | [diff] [blame] | 33 | /* These ones are initialized per-thread on startup by init_pools() */ |
Willy Tarreau | e18db9e | 2018-10-16 10:28:54 +0200 | [diff] [blame] | 34 | THREAD_LOCAL size_t pool_cache_bytes = 0; /* total cache size */ |
| 35 | THREAD_LOCAL size_t pool_cache_count = 0; /* #cache objects */ |
| 36 | |
Willy Tarreau | 9f69995 | 2022-02-18 18:31:53 +0100 | [diff] [blame] | 37 | static struct list pools __read_mostly = LIST_HEAD_INIT(pools); |
Willy Tarreau | ef301b7 | 2022-02-23 14:15:18 +0100 | [diff] [blame] | 38 | int mem_poison_byte __read_mostly = 'P'; |
Willy Tarreau | 8d0273e | 2022-02-21 17:16:22 +0100 | [diff] [blame] | 39 | uint pool_debugging __read_mostly = /* set of POOL_DBG_* flags */ |
Olivier Houchard | dc21ff7 | 2019-01-29 15:20:16 +0100 | [diff] [blame] | 40 | #ifdef DEBUG_FAIL_ALLOC |
Willy Tarreau | 8d0273e | 2022-02-21 17:16:22 +0100 | [diff] [blame] | 41 | POOL_DBG_FAIL_ALLOC | |
Olivier Houchard | dc21ff7 | 2019-01-29 15:20:16 +0100 | [diff] [blame] | 42 | #endif |
Willy Tarreau | fd8b737 | 2022-02-21 17:31:50 +0100 | [diff] [blame] | 43 | #ifdef DEBUG_DONT_SHARE_POOLS |
| 44 | POOL_DBG_DONT_MERGE | |
| 45 | #endif |
Willy Tarreau | d3470e1 | 2022-02-21 18:30:25 +0100 | [diff] [blame] | 46 | #ifdef DEBUG_POOL_INTEGRITY |
| 47 | POOL_DBG_COLD_FIRST | |
| 48 | #endif |
Willy Tarreau | 6f3c7f6 | 2022-02-21 18:42:53 +0100 | [diff] [blame] | 49 | #ifdef DEBUG_POOL_INTEGRITY |
| 50 | POOL_DBG_INTEGRITY | |
| 51 | #endif |
Willy Tarreau | dff3b06 | 2022-02-22 09:21:13 +0100 | [diff] [blame] | 52 | #ifdef CONFIG_HAP_NO_GLOBAL_POOLS |
| 53 | POOL_DBG_NO_GLOBAL | |
| 54 | #endif |
Willy Tarreau | e981631 | 2022-02-22 16:23:09 +0100 | [diff] [blame] | 55 | #ifndef CONFIG_HAP_POOLS |
| 56 | POOL_DBG_NO_CACHE | |
| 57 | #endif |
Willy Tarreau | 0271822 | 2022-02-23 10:10:33 +0100 | [diff] [blame] | 58 | #if defined(DEBUG_POOL_TRACING) |
| 59 | POOL_DBG_CALLER | |
| 60 | #endif |
Willy Tarreau | 13d7775 | 2022-02-23 10:20:37 +0100 | [diff] [blame] | 61 | #if defined(DEBUG_MEMORY_POOLS) |
| 62 | POOL_DBG_TAG | |
| 63 | #endif |
Willy Tarreau | 8d0273e | 2022-02-21 17:16:22 +0100 | [diff] [blame] | 64 | 0; |
Olivier Houchard | dc21ff7 | 2019-01-29 15:20:16 +0100 | [diff] [blame] | 65 | |
Willy Tarreau | f4b79c4 | 2022-02-23 15:20:53 +0100 | [diff] [blame] | 66 | static 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 Tarreau | 224adf2 | 2022-11-21 09:02:41 +0100 | [diff] [blame] | 85 | /* describes a snapshot of a pool line about to be dumped by "show pools" */ |
| 86 | struct 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 Tarreau | 2fba08f | 2022-11-21 09:34:02 +0100 | [diff] [blame] | 96 | /* context used by "show pools" */ |
| 97 | struct show_pools_ctx { |
Willy Tarreau | 7583c36 | 2022-11-21 10:02:29 +0100 | [diff] [blame] | 98 | char *prefix; /* if non-null, match this prefix name for the pool */ |
Willy Tarreau | 2fba08f | 2022-11-21 09:34:02 +0100 | [diff] [blame] | 99 | 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 Tarreau | 8d0273e | 2022-02-21 17:16:22 +0100 | [diff] [blame] | 103 | static int mem_fail_rate __read_mostly = 0; |
Willy Tarreau | 9f69995 | 2022-02-18 18:31:53 +0100 | [diff] [blame] | 104 | static int using_default_allocator __read_mostly = 1; |
Willy Tarreau | c4e56dc | 2022-03-08 10:41:40 +0100 | [diff] [blame] | 105 | static int disable_trim __read_mostly = 0; |
David Carlier | ed23214 | 2021-11-25 16:09:45 +0000 | [diff] [blame] | 106 | static int(*my_mallctl)(const char *, void *, size_t *, void *, size_t) = NULL; |
Willy Tarreau | 157e393 | 2021-09-15 10:05:48 +0200 | [diff] [blame] | 107 | |
Willy Tarreau | 0d93a81 | 2021-12-23 09:26:30 +0100 | [diff] [blame] | 108 | /* 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 Tarreau | ea3323f | 2021-09-15 10:38:21 +0200 | [diff] [blame] | 114 | static void trim_all_pools(void) |
| 115 | { |
Willy Tarreau | 0d93a81 | 2021-12-23 09:26:30 +0100 | [diff] [blame] | 116 | int isolated = thread_isolated(); |
| 117 | |
Willy Tarreau | c4e56dc | 2022-03-08 10:41:40 +0100 | [diff] [blame] | 118 | if (disable_trim) |
| 119 | return; |
| 120 | |
Willy Tarreau | 0d93a81 | 2021-12-23 09:26:30 +0100 | [diff] [blame] | 121 | if (!isolated) |
| 122 | thread_isolate(); |
| 123 | |
David Carlier | d450ff6 | 2021-11-25 16:14:38 +0000 | [diff] [blame] | 124 | 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 Carlier | ed23214 | 2021-11-25 16:09:45 +0000 | [diff] [blame] | 136 | #if defined(HA_HAVE_MALLOC_TRIM) |
David Carlier | d450ff6 | 2021-11-25 16:14:38 +0000 | [diff] [blame] | 137 | if (using_default_allocator) |
| 138 | malloc_trim(0); |
David CARLIER | b1e190a | 2021-11-26 20:44:44 +0000 | [diff] [blame] | 139 | #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 Carlier | ed23214 | 2021-11-25 16:09:45 +0000 | [diff] [blame] | 154 | #endif |
David Carlier | d450ff6 | 2021-11-25 16:14:38 +0000 | [diff] [blame] | 155 | } |
Willy Tarreau | 0d93a81 | 2021-12-23 09:26:30 +0100 | [diff] [blame] | 156 | |
| 157 | if (!isolated) |
| 158 | thread_release(); |
Willy Tarreau | ea3323f | 2021-09-15 10:38:21 +0200 | [diff] [blame] | 159 | } |
| 160 | |
Willy Tarreau | 157e393 | 2021-09-15 10:05:48 +0200 | [diff] [blame] | 161 | /* 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 Carlier | ed23214 | 2021-11-25 16:09:45 +0000 | [diff] [blame] | 165 | * 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 Tarreau | 157e393 | 2021-09-15 10:05:48 +0200 | [diff] [blame] | 168 | */ |
| 169 | static void detect_allocator(void) |
| 170 | { |
Willy Tarreau | 781f07a | 2021-11-26 15:55:55 +0100 | [diff] [blame] | 171 | #if defined(__ELF__) |
David Carlier | ed23214 | 2021-11-25 16:09:45 +0000 | [diff] [blame] | 172 | extern int mallctl(const char *, void *, size_t *, void *, size_t) __attribute__((weak)); |
| 173 | |
| 174 | my_mallctl = mallctl; |
Willy Tarreau | 781f07a | 2021-11-26 15:55:55 +0100 | [diff] [blame] | 175 | #endif |
David Carlier | ed23214 | 2021-11-25 16:09:45 +0000 | [diff] [blame] | 176 | |
| 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 Tarreau | c2afb86 | 2021-09-16 09:18:21 +0200 | [diff] [blame] | 184 | #ifdef HA_HAVE_MALLINFO2 |
David Carlier | ed23214 | 2021-11-25 16:09:45 +0000 | [diff] [blame] | 185 | struct mallinfo2 mi1, mi2; |
Willy Tarreau | c2afb86 | 2021-09-16 09:18:21 +0200 | [diff] [blame] | 186 | #else |
David Carlier | ed23214 | 2021-11-25 16:09:45 +0000 | [diff] [blame] | 187 | struct mallinfo mi1, mi2; |
Willy Tarreau | c2afb86 | 2021-09-16 09:18:21 +0200 | [diff] [blame] | 188 | #endif |
David Carlier | ed23214 | 2021-11-25 16:09:45 +0000 | [diff] [blame] | 189 | void *ptr; |
Willy Tarreau | 157e393 | 2021-09-15 10:05:48 +0200 | [diff] [blame] | 190 | |
Willy Tarreau | c2afb86 | 2021-09-16 09:18:21 +0200 | [diff] [blame] | 191 | #ifdef HA_HAVE_MALLINFO2 |
David Carlier | ed23214 | 2021-11-25 16:09:45 +0000 | [diff] [blame] | 192 | mi1 = mallinfo2(); |
Willy Tarreau | c2afb86 | 2021-09-16 09:18:21 +0200 | [diff] [blame] | 193 | #else |
David Carlier | ed23214 | 2021-11-25 16:09:45 +0000 | [diff] [blame] | 194 | mi1 = mallinfo(); |
Willy Tarreau | c2afb86 | 2021-09-16 09:18:21 +0200 | [diff] [blame] | 195 | #endif |
David Carlier | ed23214 | 2021-11-25 16:09:45 +0000 | [diff] [blame] | 196 | ptr = DISGUISE(malloc(1)); |
Willy Tarreau | c2afb86 | 2021-09-16 09:18:21 +0200 | [diff] [blame] | 197 | #ifdef HA_HAVE_MALLINFO2 |
David Carlier | ed23214 | 2021-11-25 16:09:45 +0000 | [diff] [blame] | 198 | mi2 = mallinfo2(); |
Willy Tarreau | c2afb86 | 2021-09-16 09:18:21 +0200 | [diff] [blame] | 199 | #else |
David Carlier | ed23214 | 2021-11-25 16:09:45 +0000 | [diff] [blame] | 200 | mi2 = mallinfo(); |
Willy Tarreau | c2afb86 | 2021-09-16 09:18:21 +0200 | [diff] [blame] | 201 | #endif |
David Carlier | ed23214 | 2021-11-25 16:09:45 +0000 | [diff] [blame] | 202 | free(DISGUISE(ptr)); |
Willy Tarreau | ea3323f | 2021-09-15 10:38:21 +0200 | [diff] [blame] | 203 | |
David Carlier | ed23214 | 2021-11-25 16:09:45 +0000 | [diff] [blame] | 204 | using_default_allocator = !!memcmp(&mi1, &mi2, sizeof(mi1)); |
David CARLIER | b1e190a | 2021-11-26 20:44:44 +0000 | [diff] [blame] | 205 | #elif defined(HA_HAVE_MALLOC_ZONE) |
| 206 | using_default_allocator = (malloc_default_zone() != NULL); |
David Carlier | ed23214 | 2021-11-25 16:09:45 +0000 | [diff] [blame] | 207 | #endif |
| 208 | } |
Willy Tarreau | 845b560 | 2021-09-15 10:41:24 +0200 | [diff] [blame] | 209 | } |
| 210 | |
| 211 | static int is_trim_enabled(void) |
| 212 | { |
David Carlier | ed23214 | 2021-11-25 16:09:45 +0000 | [diff] [blame] | 213 | return using_default_allocator; |
Willy Tarreau | 157e393 | 2021-09-15 10:05:48 +0200 | [diff] [blame] | 214 | } |
Willy Tarreau | ea3323f | 2021-09-15 10:38:21 +0200 | [diff] [blame] | 215 | |
Willy Tarreau | 8d0273e | 2022-02-21 17:16:22 +0100 | [diff] [blame] | 216 | static 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 Tarreau | 50e608d | 2007-05-13 18:26:08 +0200 | [diff] [blame] | 229 | /* 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 Tarreau | 581bf81 | 2016-01-25 02:19:13 +0100 | [diff] [blame] | 231 | * 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 Tarreau | 50e608d | 2007-05-13 18:26:08 +0200 | [diff] [blame] | 234 | */ |
| 235 | struct pool_head *create_pool(char *name, unsigned int size, unsigned int flags) |
| 236 | { |
Willy Tarreau | 42705d0 | 2022-02-23 10:03:11 +0100 | [diff] [blame] | 237 | unsigned int extra_mark, extra_caller, extra; |
Willy Tarreau | 50e608d | 2007-05-13 18:26:08 +0200 | [diff] [blame] | 238 | struct pool_head *pool; |
Willy Tarreau | 7dcd46d | 2007-05-14 00:16:13 +0200 | [diff] [blame] | 239 | struct pool_head *entry; |
| 240 | struct list *start; |
Willy Tarreau | 50e608d | 2007-05-13 18:26:08 +0200 | [diff] [blame] | 241 | unsigned int align; |
Willy Tarreau | 9f3129e | 2021-04-17 00:31:38 +0200 | [diff] [blame] | 242 | int thr __maybe_unused; |
Willy Tarreau | 50e608d | 2007-05-13 18:26:08 +0200 | [diff] [blame] | 243 | |
Willy Tarreau | ac42111 | 2015-10-28 15:09:29 +0100 | [diff] [blame] | 244 | /* We need to store a (void *) at the end of the chunks. Since we know |
Willy Tarreau | 50e608d | 2007-05-13 18:26:08 +0200 | [diff] [blame] | 245 | * 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 Tarreau | ac42111 | 2015-10-28 15:09:29 +0100 | [diff] [blame] | 248 | * This extra (void *) is not accounted for in the size computation |
| 249 | * so that the visible parts outside are not affected. |
Willy Tarreau | 30f931e | 2018-10-23 14:40:23 +0200 | [diff] [blame] | 250 | * |
| 251 | * Note: for the LRU cache, we need to store 2 doubly-linked lists. |
Willy Tarreau | 50e608d | 2007-05-13 18:26:08 +0200 | [diff] [blame] | 252 | */ |
| 253 | |
Willy Tarreau | 13d7775 | 2022-02-23 10:20:37 +0100 | [diff] [blame] | 254 | extra_mark = (pool_debugging & POOL_DBG_TAG) ? POOL_EXTRA_MARK : 0; |
Willy Tarreau | 0271822 | 2022-02-23 10:10:33 +0100 | [diff] [blame] | 255 | extra_caller = (pool_debugging & POOL_DBG_CALLER) ? POOL_EXTRA_CALLER : 0; |
Willy Tarreau | 42705d0 | 2022-02-23 10:03:11 +0100 | [diff] [blame] | 256 | extra = extra_mark + extra_caller; |
| 257 | |
Willy Tarreau | 581bf81 | 2016-01-25 02:19:13 +0100 | [diff] [blame] | 258 | if (!(flags & MEM_F_EXACT)) { |
Willy Tarreau | 30f931e | 2018-10-23 14:40:23 +0200 | [diff] [blame] | 259 | align = 4 * sizeof(void *); // 2 lists = 4 pointers min |
Willy Tarreau | 42705d0 | 2022-02-23 10:03:11 +0100 | [diff] [blame] | 260 | size = ((size + extra + align - 1) & -align) - extra; |
Willy Tarreau | 581bf81 | 2016-01-25 02:19:13 +0100 | [diff] [blame] | 261 | } |
Willy Tarreau | 50e608d | 2007-05-13 18:26:08 +0200 | [diff] [blame] | 262 | |
Willy Tarreau | e981631 | 2022-02-22 16:23:09 +0100 | [diff] [blame] | 263 | 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 Tarreau | 42705d0 | 2022-02-23 10:03:11 +0100 | [diff] [blame] | 269 | if (size + extra - extra_caller < sizeof(struct pool_cache_item)) |
| 270 | size = sizeof(struct pool_cache_item) + extra_caller - extra; |
Willy Tarreau | e981631 | 2022-02-22 16:23:09 +0100 | [diff] [blame] | 271 | } |
| 272 | |
Christopher Faulet | b349e48 | 2017-08-29 09:52:38 +0200 | [diff] [blame] | 273 | /* TODO: thread: we do not lock pool list for now because all pools are |
| 274 | * created during HAProxy startup (so before threads creation) */ |
Willy Tarreau | 7dcd46d | 2007-05-14 00:16:13 +0200 | [diff] [blame] | 275 | start = &pools; |
Willy Tarreau | 50e608d | 2007-05-13 18:26:08 +0200 | [diff] [blame] | 276 | pool = NULL; |
Willy Tarreau | 7dcd46d | 2007-05-14 00:16:13 +0200 | [diff] [blame] | 277 | |
| 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 Shipitsin | 47d1718 | 2020-06-21 21:42:57 +0500 | [diff] [blame] | 281 | * we look for a shareable one or for the next position |
Willy Tarreau | 7dcd46d | 2007-05-14 00:16:13 +0200 | [diff] [blame] | 282 | * before which we will insert a new one. |
| 283 | */ |
Willy Tarreau | fd8b737 | 2022-02-21 17:31:50 +0100 | [diff] [blame] | 284 | if ((flags & entry->flags & MEM_F_SHARED) && |
| 285 | (!(pool_debugging & POOL_DBG_DONT_MERGE) || |
| 286 | strcmp(name, entry->name) == 0)) { |
Willy Tarreau | 7dcd46d | 2007-05-14 00:16:13 +0200 | [diff] [blame] | 287 | /* we can share this one */ |
Willy Tarreau | 50e608d | 2007-05-13 18:26:08 +0200 | [diff] [blame] | 288 | pool = entry; |
Krzysztof Piotr Oledzki | a643baf | 2008-05-29 23:53:44 +0200 | [diff] [blame] | 289 | DPRINTF(stderr, "Sharing %s with %s\n", name, pool->name); |
Willy Tarreau | 50e608d | 2007-05-13 18:26:08 +0200 | [diff] [blame] | 290 | break; |
| 291 | } |
| 292 | } |
Willy Tarreau | 7dcd46d | 2007-05-14 00:16:13 +0200 | [diff] [blame] | 293 | else if (entry->size > size) { |
| 294 | /* insert before this one */ |
| 295 | start = &entry->list; |
| 296 | break; |
| 297 | } |
Willy Tarreau | 50e608d | 2007-05-13 18:26:08 +0200 | [diff] [blame] | 298 | } |
| 299 | |
| 300 | if (!pool) { |
Willy Tarreau | e81248c | 2022-03-02 17:59:04 +0100 | [diff] [blame] | 301 | void *pool_addr; |
Willy Tarreau | 0a93b64 | 2018-10-16 07:58:39 +0200 | [diff] [blame] | 302 | |
Willy Tarreau | e81248c | 2022-03-02 17:59:04 +0100 | [diff] [blame] | 303 | pool_addr = calloc(1, sizeof(*pool) + __alignof__(*pool)); |
| 304 | if (!pool_addr) |
Willy Tarreau | 50e608d | 2007-05-13 18:26:08 +0200 | [diff] [blame] | 305 | return NULL; |
Willy Tarreau | e81248c | 2022-03-02 17:59:04 +0100 | [diff] [blame] | 306 | |
| 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 Tarreau | 50e608d | 2007-05-13 18:26:08 +0200 | [diff] [blame] | 311 | if (name) |
| 312 | strlcpy2(pool->name, name, sizeof(pool->name)); |
Willy Tarreau | 42705d0 | 2022-02-23 10:03:11 +0100 | [diff] [blame] | 313 | pool->alloc_sz = size + extra; |
Willy Tarreau | 50e608d | 2007-05-13 18:26:08 +0200 | [diff] [blame] | 314 | pool->size = size; |
| 315 | pool->flags = flags; |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 316 | LIST_APPEND(start, &pool->list); |
Christopher Faulet | 2f6d3c0 | 2019-06-25 21:45:59 +0200 | [diff] [blame] | 317 | |
Willy Tarreau | e981631 | 2022-02-22 16:23:09 +0100 | [diff] [blame] | 318 | 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 Faulet | 2f6d3c0 | 2019-06-25 21:45:59 +0200 | [diff] [blame] | 325 | } |
Olivier Houchard | 8af97eb | 2020-02-01 17:45:32 +0100 | [diff] [blame] | 326 | } |
| 327 | pool->users++; |
Willy Tarreau | 50e608d | 2007-05-13 18:26:08 +0200 | [diff] [blame] | 328 | return pool; |
| 329 | } |
Olivier Houchard | cf975d4 | 2018-01-24 18:38:31 +0100 | [diff] [blame] | 330 | |
Willy Tarreau | 1384364 | 2021-04-17 16:57:25 +0200 | [diff] [blame] | 331 | /* 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 Tarreau | 8715dec | 2021-06-10 17:31:48 +0200 | [diff] [blame] | 333 | * but no other checks are performed. |
Willy Tarreau | 1384364 | 2021-04-17 16:57:25 +0200 | [diff] [blame] | 334 | */ |
| 335 | void *pool_get_from_os(struct pool_head *pool) |
| 336 | { |
| 337 | if (!pool->limit || pool->allocated < pool->limit) { |
Willy Tarreau | 96d5bc7 | 2022-02-23 08:57:59 +0100 | [diff] [blame] | 338 | void *ptr = pool_alloc_area(pool->alloc_sz); |
Willy Tarreau | 1384364 | 2021-04-17 16:57:25 +0200 | [diff] [blame] | 339 | 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 Tarreau | 45e4e28 | 2021-04-17 17:48:40 +0200 | [diff] [blame] | 350 | /* Releases a pool item back to the operating system and atomically updates |
| 351 | * the allocation counter. |
| 352 | */ |
| 353 | void pool_put_to_os(struct pool_head *pool, void *ptr) |
| 354 | { |
Willy Tarreau | 9a7aa3b | 2021-06-10 17:20:19 +0200 | [diff] [blame] | 355 | #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 Tarreau | 96d5bc7 | 2022-02-23 08:57:59 +0100 | [diff] [blame] | 363 | pool_free_area(ptr, pool->alloc_sz); |
Willy Tarreau | 45e4e28 | 2021-04-17 17:48:40 +0200 | [diff] [blame] | 364 | _HA_ATOMIC_DEC(&pool->allocated); |
| 365 | } |
| 366 | |
Willy Tarreau | 8fe726f | 2021-04-15 18:20:12 +0200 | [diff] [blame] | 367 | /* 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 Tarreau | 8fe726f | 2021-04-15 18:20:12 +0200 | [diff] [blame] | 370 | */ |
| 371 | void *pool_alloc_nocache(struct pool_head *pool) |
Willy Tarreau | 0bae075 | 2021-03-02 20:05:09 +0100 | [diff] [blame] | 372 | { |
Willy Tarreau | 0bae075 | 2021-03-02 20:05:09 +0100 | [diff] [blame] | 373 | void *ptr = NULL; |
| 374 | |
Willy Tarreau | 1384364 | 2021-04-17 16:57:25 +0200 | [diff] [blame] | 375 | ptr = pool_get_from_os(pool); |
| 376 | if (!ptr) |
Willy Tarreau | 0bae075 | 2021-03-02 20:05:09 +0100 | [diff] [blame] | 377 | return NULL; |
Willy Tarreau | 0bae075 | 2021-03-02 20:05:09 +0100 | [diff] [blame] | 378 | |
Willy Tarreau | 1384364 | 2021-04-17 16:57:25 +0200 | [diff] [blame] | 379 | swrate_add_scaled(&pool->needed_avg, POOL_AVG_SAMPLES, pool->used, POOL_AVG_SAMPLES/4); |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 380 | _HA_ATOMIC_INC(&pool->used); |
Willy Tarreau | 0bae075 | 2021-03-02 20:05:09 +0100 | [diff] [blame] | 381 | |
Willy Tarreau | 0bae075 | 2021-03-02 20:05:09 +0100 | [diff] [blame] | 382 | /* keep track of where the element was allocated from */ |
Willy Tarreau | 8c49270 | 2022-01-01 17:10:50 +0100 | [diff] [blame] | 383 | POOL_DEBUG_SET_MARK(pool, ptr); |
Willy Tarreau | f70fdde | 2022-01-25 15:56:50 +0100 | [diff] [blame] | 384 | POOL_DEBUG_TRACE_CALLER(pool, (struct pool_cache_item *)ptr, NULL); |
Willy Tarreau | 0bae075 | 2021-03-02 20:05:09 +0100 | [diff] [blame] | 385 | return ptr; |
| 386 | } |
| 387 | |
Willy Tarreau | 45e4e28 | 2021-04-17 17:48:40 +0200 | [diff] [blame] | 388 | /* 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 | */ |
| 392 | void 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 Tarreau | b8498e9 | 2021-04-18 10:23:02 +0200 | [diff] [blame] | 399 | |
Willy Tarreau | 6f3c7f6 | 2022-02-21 18:42:53 +0100 | [diff] [blame] | 400 | /* 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 | */ |
| 403 | void 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 | */ |
| 425 | void 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 Tarreau | a0b5831 | 2022-01-02 17:19:14 +0100 | [diff] [blame] | 444 | /* 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 Tarreau | e981631 | 2022-02-22 16:23:09 +0100 | [diff] [blame] | 448 | * the local cache. The counters are automatically updated. Must not be used |
| 449 | * with pools disabled. |
Willy Tarreau | 8721203 | 2021-04-19 08:14:03 +0200 | [diff] [blame] | 450 | */ |
Willy Tarreau | a0b5831 | 2022-01-02 17:19:14 +0100 | [diff] [blame] | 451 | static void pool_evict_last_items(struct pool_head *pool, struct pool_cache_head *ph, uint count) |
Willy Tarreau | 8721203 | 2021-04-19 08:14:03 +0200 | [diff] [blame] | 452 | { |
Willy Tarreau | 8721203 | 2021-04-19 08:14:03 +0200 | [diff] [blame] | 453 | struct pool_cache_item *item; |
Willy Tarreau | 1513c54 | 2022-01-02 17:53:02 +0100 | [diff] [blame] | 454 | struct pool_item *pi, *head = NULL; |
Willy Tarreau | a0b5831 | 2022-01-02 17:19:14 +0100 | [diff] [blame] | 455 | uint released = 0; |
Willy Tarreau | 1513c54 | 2022-01-02 17:53:02 +0100 | [diff] [blame] | 456 | uint cluster = 0; |
Willy Tarreau | 361e31e | 2022-01-02 00:27:06 +0100 | [diff] [blame] | 457 | uint to_free_max; |
| 458 | |
Willy Tarreau | e981631 | 2022-02-22 16:23:09 +0100 | [diff] [blame] | 459 | BUG_ON(pool_debugging & POOL_DBG_NO_CACHE); |
| 460 | |
Willy Tarreau | dff3b06 | 2022-02-22 09:21:13 +0100 | [diff] [blame] | 461 | /* Note: this will be zero when global pools are disabled */ |
Willy Tarreau | 361e31e | 2022-01-02 00:27:06 +0100 | [diff] [blame] | 462 | to_free_max = pool_releasable(pool); |
Willy Tarreau | 8721203 | 2021-04-19 08:14:03 +0200 | [diff] [blame] | 463 | |
Willy Tarreau | a0b5831 | 2022-01-02 17:19:14 +0100 | [diff] [blame] | 464 | while (released < count && !LIST_ISEMPTY(&ph->list)) { |
Willy Tarreau | d5ec100 | 2022-01-02 12:40:14 +0100 | [diff] [blame] | 465 | item = LIST_PREV(&ph->list, typeof(item), by_pool); |
Willy Tarreau | e2830ad | 2022-02-09 16:23:55 +0100 | [diff] [blame] | 466 | BUG_ON(&item->by_pool == &ph->list); |
Willy Tarreau | 6f3c7f6 | 2022-02-21 18:42:53 +0100 | [diff] [blame] | 467 | if (unlikely(pool_debugging & POOL_DBG_INTEGRITY)) |
| 468 | pool_check_pattern(ph, item, pool->size); |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 469 | LIST_DELETE(&item->by_pool); |
| 470 | LIST_DELETE(&item->by_lru); |
Willy Tarreau | b46674a | 2021-12-30 17:37:33 +0100 | [diff] [blame] | 471 | |
Willy Tarreau | 1513c54 | 2022-01-02 17:53:02 +0100 | [diff] [blame] | 472 | if (to_free_max > released || cluster) { |
Willy Tarreau | dff3b06 | 2022-02-22 09:21:13 +0100 | [diff] [blame] | 473 | /* will never match when global pools are disabled */ |
Willy Tarreau | 361e31e | 2022-01-02 00:27:06 +0100 | [diff] [blame] | 474 | pi = (struct pool_item *)item; |
Willy Tarreau | 1513c54 | 2022-01-02 17:53:02 +0100 | [diff] [blame] | 475 | 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 Tarreau | 361e31e | 2022-01-02 00:27:06 +0100 | [diff] [blame] | 485 | } else |
Willy Tarreau | b46674a | 2021-12-30 17:37:33 +0100 | [diff] [blame] | 486 | pool_free_nocache(pool, item); |
Willy Tarreau | 1513c54 | 2022-01-02 17:53:02 +0100 | [diff] [blame] | 487 | |
| 488 | released++; |
Willy Tarreau | 361e31e | 2022-01-02 00:27:06 +0100 | [diff] [blame] | 489 | } |
| 490 | |
Willy Tarreau | 1513c54 | 2022-01-02 17:53:02 +0100 | [diff] [blame] | 491 | /* incomplete cluster left */ |
| 492 | if (cluster) |
| 493 | pool_put_to_shared_cache(pool, head, cluster); |
| 494 | |
Willy Tarreau | a0b5831 | 2022-01-02 17:19:14 +0100 | [diff] [blame] | 495 | 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 Tarreau | c895c44 | 2022-02-09 16:19:24 +0100 | [diff] [blame] | 504 | * list is scanned in reverse. If <full> is non-null, all objects are evicted. |
Willy Tarreau | e981631 | 2022-02-22 16:23:09 +0100 | [diff] [blame] | 505 | * Must not be used when pools are disabled. |
Willy Tarreau | a0b5831 | 2022-01-02 17:19:14 +0100 | [diff] [blame] | 506 | */ |
Willy Tarreau | c895c44 | 2022-02-09 16:19:24 +0100 | [diff] [blame] | 507 | void pool_evict_from_local_cache(struct pool_head *pool, int full) |
Willy Tarreau | a0b5831 | 2022-01-02 17:19:14 +0100 | [diff] [blame] | 508 | { |
| 509 | struct pool_cache_head *ph = &pool->cache[tid]; |
| 510 | |
Willy Tarreau | e981631 | 2022-02-22 16:23:09 +0100 | [diff] [blame] | 511 | BUG_ON(pool_debugging & POOL_DBG_NO_CACHE); |
| 512 | |
Willy Tarreau | c895c44 | 2022-02-09 16:19:24 +0100 | [diff] [blame] | 513 | 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 Tarreau | 43937e9 | 2022-01-02 17:24:55 +0100 | [diff] [blame] | 517 | pool_evict_last_items(pool, ph, CONFIG_HAP_POOL_CLUSTER_SIZE); |
Willy Tarreau | 8721203 | 2021-04-19 08:14:03 +0200 | [diff] [blame] | 518 | } |
| 519 | } |
| 520 | |
Willy Tarreau | b8498e9 | 2021-04-18 10:23:02 +0200 | [diff] [blame] | 521 | /* Evicts some of the oldest objects from the local cache, pushing them to the |
Willy Tarreau | e981631 | 2022-02-22 16:23:09 +0100 | [diff] [blame] | 522 | * global pool. Must not be used when pools are disabled. |
Willy Tarreau | b8498e9 | 2021-04-18 10:23:02 +0200 | [diff] [blame] | 523 | */ |
| 524 | void 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 Tarreau | e981631 | 2022-02-22 16:23:09 +0100 | [diff] [blame] | 530 | BUG_ON(pool_debugging & POOL_DBG_NO_CACHE); |
| 531 | |
Willy Tarreau | b8498e9 | 2021-04-18 10:23:02 +0200 | [diff] [blame] | 532 | do { |
Willy Tarreau | b4e3476 | 2021-09-30 19:02:18 +0200 | [diff] [blame] | 533 | item = LIST_PREV(&th_ctx->pool_lru_head, struct pool_cache_item *, by_lru); |
Willy Tarreau | e2830ad | 2022-02-09 16:23:55 +0100 | [diff] [blame] | 534 | BUG_ON(&item->by_lru == &th_ctx->pool_lru_head); |
Willy Tarreau | b8498e9 | 2021-04-18 10:23:02 +0200 | [diff] [blame] | 535 | /* 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 Tarreau | 49bb5d4 | 2022-02-09 16:33:22 +0100 | [diff] [blame] | 539 | BUG_ON(ph->tid != tid); |
| 540 | |
Willy Tarreau | b8498e9 | 2021-04-18 10:23:02 +0200 | [diff] [blame] | 541 | pool = container_of(ph - tid, struct pool_head, cache); |
Willy Tarreau | 49bb5d4 | 2022-02-09 16:33:22 +0100 | [diff] [blame] | 542 | BUG_ON(pool != ph->pool); |
| 543 | |
Willy Tarreau | 43937e9 | 2022-01-02 17:24:55 +0100 | [diff] [blame] | 544 | pool_evict_last_items(pool, ph, CONFIG_HAP_POOL_CLUSTER_SIZE); |
Willy Tarreau | b8498e9 | 2021-04-18 10:23:02 +0200 | [diff] [blame] | 545 | } while (pool_cache_bytes > CONFIG_HAP_POOL_CACHE_SIZE * 7 / 8); |
| 546 | } |
Willy Tarreau | 0bae075 | 2021-03-02 20:05:09 +0100 | [diff] [blame] | 547 | |
Willy Tarreau | b2a853d | 2021-04-19 11:49:26 +0200 | [diff] [blame] | 548 | /* 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 Tarreau | add43fa | 2022-01-24 15:52:51 +0100 | [diff] [blame] | 551 | * guaranteed to be released from the users' perpective. A caller address may |
Willy Tarreau | e981631 | 2022-02-22 16:23:09 +0100 | [diff] [blame] | 552 | * be passed and stored into the area when DEBUG_POOL_TRACING is set. Must not |
| 553 | * be used with pools disabled. |
Willy Tarreau | b2a853d | 2021-04-19 11:49:26 +0200 | [diff] [blame] | 554 | */ |
Willy Tarreau | 0e2a5b4 | 2022-01-24 15:51:50 +0100 | [diff] [blame] | 555 | void pool_put_to_cache(struct pool_head *pool, void *ptr, const void *caller) |
Willy Tarreau | b2a853d | 2021-04-19 11:49:26 +0200 | [diff] [blame] | 556 | { |
| 557 | struct pool_cache_item *item = (struct pool_cache_item *)ptr; |
| 558 | struct pool_cache_head *ph = &pool->cache[tid]; |
| 559 | |
Willy Tarreau | e981631 | 2022-02-22 16:23:09 +0100 | [diff] [blame] | 560 | BUG_ON(pool_debugging & POOL_DBG_NO_CACHE); |
| 561 | |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 562 | LIST_INSERT(&ph->list, &item->by_pool); |
Willy Tarreau | b4e3476 | 2021-09-30 19:02:18 +0200 | [diff] [blame] | 563 | LIST_INSERT(&th_ctx->pool_lru_head, &item->by_lru); |
Willy Tarreau | add43fa | 2022-01-24 15:52:51 +0100 | [diff] [blame] | 564 | POOL_DEBUG_TRACE_CALLER(pool, item, caller); |
Willy Tarreau | b2a853d | 2021-04-19 11:49:26 +0200 | [diff] [blame] | 565 | ph->count++; |
Willy Tarreau | 6f3c7f6 | 2022-02-21 18:42:53 +0100 | [diff] [blame] | 566 | if (unlikely(pool_debugging & POOL_DBG_INTEGRITY)) |
| 567 | pool_fill_pattern(ph, item, pool->size); |
Willy Tarreau | b2a853d | 2021-04-19 11:49:26 +0200 | [diff] [blame] | 568 | pool_cache_count++; |
| 569 | pool_cache_bytes += pool->size; |
| 570 | |
| 571 | if (unlikely(pool_cache_bytes > CONFIG_HAP_POOL_CACHE_SIZE * 3 / 4)) { |
Willy Tarreau | 43937e9 | 2022-01-02 17:24:55 +0100 | [diff] [blame] | 572 | if (ph->count >= 16 + pool_cache_count / 8 + CONFIG_HAP_POOL_CLUSTER_SIZE) |
Willy Tarreau | c895c44 | 2022-02-09 16:19:24 +0100 | [diff] [blame] | 573 | pool_evict_from_local_cache(pool, 0); |
Willy Tarreau | b2a853d | 2021-04-19 11:49:26 +0200 | [diff] [blame] | 574 | if (pool_cache_bytes > CONFIG_HAP_POOL_CACHE_SIZE) |
| 575 | pool_evict_from_local_caches(); |
| 576 | } |
| 577 | } |
| 578 | |
Willy Tarreau | afe2c4a | 2021-12-30 17:09:31 +0100 | [diff] [blame] | 579 | /* 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 Tarreau | e981631 | 2022-02-22 16:23:09 +0100 | [diff] [blame] | 583 | * the cache has enough objects for its use. Must not be used when pools are |
| 584 | * disabled. |
Willy Tarreau | afe2c4a | 2021-12-30 17:09:31 +0100 | [diff] [blame] | 585 | */ |
| 586 | void pool_refill_local_from_shared(struct pool_head *pool, struct pool_cache_head *pch) |
| 587 | { |
| 588 | struct pool_cache_item *item; |
Willy Tarreau | 148160b | 2022-01-02 14:35:57 +0100 | [diff] [blame] | 589 | struct pool_item *ret, *down; |
| 590 | uint count; |
Willy Tarreau | afe2c4a | 2021-12-30 17:09:31 +0100 | [diff] [blame] | 591 | |
Willy Tarreau | e981631 | 2022-02-22 16:23:09 +0100 | [diff] [blame] | 592 | BUG_ON(pool_debugging & POOL_DBG_NO_CACHE); |
| 593 | |
Willy Tarreau | afe2c4a | 2021-12-30 17:09:31 +0100 | [diff] [blame] | 594 | /* 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 Tarreau | c16ed3b | 2022-01-01 18:22:20 +0100 | [diff] [blame] | 614 | HA_ATOMIC_STORE(&pool->free_list, ret->next); |
Willy Tarreau | afe2c4a | 2021-12-30 17:09:31 +0100 | [diff] [blame] | 615 | |
Willy Tarreau | 148160b | 2022-01-02 14:35:57 +0100 | [diff] [blame] | 616 | /* now store the retrieved object(s) into the local cache */ |
| 617 | count = 0; |
| 618 | for (; ret; ret = down) { |
| 619 | down = ret->down; |
Willy Tarreau | 148160b | 2022-01-02 14:35:57 +0100 | [diff] [blame] | 620 | item = (struct pool_cache_item *)ret; |
Willy Tarreau | f70fdde | 2022-01-25 15:56:50 +0100 | [diff] [blame] | 621 | POOL_DEBUG_TRACE_CALLER(pool, item, NULL); |
Willy Tarreau | 148160b | 2022-01-02 14:35:57 +0100 | [diff] [blame] | 622 | LIST_INSERT(&pch->list, &item->by_pool); |
| 623 | LIST_INSERT(&th_ctx->pool_lru_head, &item->by_lru); |
| 624 | count++; |
Willy Tarreau | 6f3c7f6 | 2022-02-21 18:42:53 +0100 | [diff] [blame] | 625 | if (unlikely(pool_debugging & POOL_DBG_INTEGRITY)) |
| 626 | pool_fill_pattern(pch, item, pool->size); |
Willy Tarreau | 148160b | 2022-01-02 14:35:57 +0100 | [diff] [blame] | 627 | } |
| 628 | HA_ATOMIC_ADD(&pool->used, count); |
| 629 | pch->count += count; |
| 630 | pool_cache_count += count; |
| 631 | pool_cache_bytes += count * pool->size; |
Willy Tarreau | afe2c4a | 2021-12-30 17:09:31 +0100 | [diff] [blame] | 632 | } |
| 633 | |
Willy Tarreau | 337410c | 2022-01-02 15:15:54 +0100 | [diff] [blame] | 634 | /* 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 Tarreau | b46674a | 2021-12-30 17:37:33 +0100 | [diff] [blame] | 638 | */ |
Willy Tarreau | 337410c | 2022-01-02 15:15:54 +0100 | [diff] [blame] | 639 | void pool_put_to_shared_cache(struct pool_head *pool, struct pool_item *item, uint count) |
Willy Tarreau | b46674a | 2021-12-30 17:37:33 +0100 | [diff] [blame] | 640 | { |
Willy Tarreau | c16ed3b | 2022-01-01 18:22:20 +0100 | [diff] [blame] | 641 | struct pool_item *free_list; |
Willy Tarreau | b46674a | 2021-12-30 17:37:33 +0100 | [diff] [blame] | 642 | |
Willy Tarreau | 337410c | 2022-01-02 15:15:54 +0100 | [diff] [blame] | 643 | _HA_ATOMIC_SUB(&pool->used, count); |
Willy Tarreau | b46674a | 2021-12-30 17:37:33 +0100 | [diff] [blame] | 644 | 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 Tarreau | c16ed3b | 2022-01-01 18:22:20 +0100 | [diff] [blame] | 650 | _HA_ATOMIC_STORE(&item->next, free_list); |
Willy Tarreau | b46674a | 2021-12-30 17:37:33 +0100 | [diff] [blame] | 651 | __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 Houchard | cf975d4 | 2018-01-24 18:38:31 +0100 | [diff] [blame] | 657 | /* |
| 658 | * This function frees whatever can be freed in pool <pool>. |
| 659 | */ |
| 660 | void pool_flush(struct pool_head *pool) |
| 661 | { |
Willy Tarreau | 148160b | 2022-01-02 14:35:57 +0100 | [diff] [blame] | 662 | struct pool_item *next, *temp, *down; |
Olivier Houchard | cf975d4 | 2018-01-24 18:38:31 +0100 | [diff] [blame] | 663 | |
Willy Tarreau | e981631 | 2022-02-22 16:23:09 +0100 | [diff] [blame] | 664 | if (!pool || (pool_debugging & (POOL_DBG_NO_CACHE|POOL_DBG_NO_GLOBAL))) |
Olivier Houchard | cf975d4 | 2018-01-24 18:38:31 +0100 | [diff] [blame] | 665 | return; |
Willy Tarreau | 2a4523f | 2021-06-09 18:59:58 +0200 | [diff] [blame] | 666 | |
| 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 Houchard | cf975d4 | 2018-01-24 18:38:31 +0100 | [diff] [blame] | 671 | do { |
Willy Tarreau | 2a4523f | 2021-06-09 18:59:58 +0200 | [diff] [blame] | 672 | 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 Houchard | 2087276 | 2019-03-08 18:53:35 +0100 | [diff] [blame] | 680 | __ha_barrier_atomic_store(); |
Willy Tarreau | 2a4523f | 2021-06-09 18:59:58 +0200 | [diff] [blame] | 681 | |
Olivier Houchard | cf975d4 | 2018-01-24 18:38:31 +0100 | [diff] [blame] | 682 | while (next) { |
| 683 | temp = next; |
Willy Tarreau | c16ed3b | 2022-01-01 18:22:20 +0100 | [diff] [blame] | 684 | next = temp->next; |
Willy Tarreau | 148160b | 2022-01-02 14:35:57 +0100 | [diff] [blame] | 685 | for (; temp; temp = down) { |
| 686 | down = temp->down; |
| 687 | pool_put_to_os(pool, temp); |
| 688 | } |
Olivier Houchard | cf975d4 | 2018-01-24 18:38:31 +0100 | [diff] [blame] | 689 | } |
Willy Tarreau | c239cde | 2021-06-10 06:54:22 +0200 | [diff] [blame] | 690 | /* here, we should have pool->allocated == pool->used */ |
Olivier Houchard | cf975d4 | 2018-01-24 18:38:31 +0100 | [diff] [blame] | 691 | } |
| 692 | |
Willy Tarreau | e6ce59d | 2007-05-13 19:38:49 +0200 | [diff] [blame] | 693 | /* |
Willy Tarreau | e6ce59d | 2007-05-13 19:38:49 +0200 | [diff] [blame] | 694 | * This function frees whatever can be freed in all pools, but respecting |
Willy Tarreau | c0e2ff2 | 2020-04-24 06:15:24 +0200 | [diff] [blame] | 695 | * the minimum thresholds imposed by owners. It makes sure to be alone to |
| 696 | * run by using thread_isolate(). <pool_ctx> is unused. |
Willy Tarreau | e6ce59d | 2007-05-13 19:38:49 +0200 | [diff] [blame] | 697 | */ |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 698 | void pool_gc(struct pool_head *pool_ctx) |
Willy Tarreau | e6ce59d | 2007-05-13 19:38:49 +0200 | [diff] [blame] | 699 | { |
| 700 | struct pool_head *entry; |
Willy Tarreau | c0e2ff2 | 2020-04-24 06:15:24 +0200 | [diff] [blame] | 701 | int isolated = thread_isolated(); |
Willy Tarreau | b7f9d12 | 2009-04-21 02:17:45 +0200 | [diff] [blame] | 702 | |
Willy Tarreau | c0e2ff2 | 2020-04-24 06:15:24 +0200 | [diff] [blame] | 703 | if (!isolated) |
| 704 | thread_isolate(); |
Willy Tarreau | b7f9d12 | 2009-04-21 02:17:45 +0200 | [diff] [blame] | 705 | |
Willy Tarreau | e6ce59d | 2007-05-13 19:38:49 +0200 | [diff] [blame] | 706 | list_for_each_entry(entry, &pools, list) { |
Willy Tarreau | 148160b | 2022-01-02 14:35:57 +0100 | [diff] [blame] | 707 | struct pool_item *temp, *down; |
Willy Tarreau | c16ed3b | 2022-01-01 18:22:20 +0100 | [diff] [blame] | 708 | |
Olivier Houchard | 51d9339 | 2020-03-12 19:05:39 +0100 | [diff] [blame] | 709 | while (entry->free_list && |
Willy Tarreau | 57767b8 | 2014-12-22 21:40:55 +0100 | [diff] [blame] | 710 | (int)(entry->allocated - entry->used) > (int)entry->minavail) { |
Olivier Houchard | 51d9339 | 2020-03-12 19:05:39 +0100 | [diff] [blame] | 711 | temp = entry->free_list; |
Willy Tarreau | c16ed3b | 2022-01-01 18:22:20 +0100 | [diff] [blame] | 712 | entry->free_list = temp->next; |
Willy Tarreau | 148160b | 2022-01-02 14:35:57 +0100 | [diff] [blame] | 713 | for (; temp; temp = down) { |
| 714 | down = temp->down; |
| 715 | pool_put_to_os(entry, temp); |
| 716 | } |
Willy Tarreau | e6ce59d | 2007-05-13 19:38:49 +0200 | [diff] [blame] | 717 | } |
Willy Tarreau | e6ce59d | 2007-05-13 19:38:49 +0200 | [diff] [blame] | 718 | } |
Christopher Faulet | b349e48 | 2017-08-29 09:52:38 +0200 | [diff] [blame] | 719 | |
Willy Tarreau | ea3323f | 2021-09-15 10:38:21 +0200 | [diff] [blame] | 720 | trim_all_pools(); |
Willy Tarreau | 26ed183 | 2021-06-10 08:40:16 +0200 | [diff] [blame] | 721 | |
Willy Tarreau | c0e2ff2 | 2020-04-24 06:15:24 +0200 | [diff] [blame] | 722 | if (!isolated) |
| 723 | thread_release(); |
Willy Tarreau | e6ce59d | 2007-05-13 19:38:49 +0200 | [diff] [blame] | 724 | } |
Willy Tarreau | b8498e9 | 2021-04-18 10:23:02 +0200 | [diff] [blame] | 725 | |
Willy Tarreau | 15c322c | 2022-01-24 11:51:43 +0100 | [diff] [blame] | 726 | /* |
Willy Tarreau | d392973 | 2022-01-24 16:09:29 +0100 | [diff] [blame] | 727 | * 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 | */ |
| 732 | void *__pool_alloc(struct pool_head *pool, unsigned int flags) |
| 733 | { |
| 734 | void *p = NULL; |
Willy Tarreau | 0271822 | 2022-02-23 10:10:33 +0100 | [diff] [blame] | 735 | void *caller = __builtin_return_address(0); |
Willy Tarreau | d392973 | 2022-01-24 16:09:29 +0100 | [diff] [blame] | 736 | |
Willy Tarreau | 8d0273e | 2022-02-21 17:16:22 +0100 | [diff] [blame] | 737 | if (unlikely(pool_debugging & POOL_DBG_FAIL_ALLOC)) |
| 738 | if (!(flags & POOL_F_NO_FAIL) && mem_should_fail(pool)) |
| 739 | return NULL; |
Willy Tarreau | d392973 | 2022-01-24 16:09:29 +0100 | [diff] [blame] | 740 | |
Willy Tarreau | e981631 | 2022-02-22 16:23:09 +0100 | [diff] [blame] | 741 | if (likely(!(pool_debugging & POOL_DBG_NO_CACHE)) && !p) |
Willy Tarreau | 0e2a5b4 | 2022-01-24 15:51:50 +0100 | [diff] [blame] | 742 | p = pool_get_from_cache(pool, caller); |
Willy Tarreau | e981631 | 2022-02-22 16:23:09 +0100 | [diff] [blame] | 743 | |
Willy Tarreau | d392973 | 2022-01-24 16:09:29 +0100 | [diff] [blame] | 744 | if (unlikely(!p)) |
| 745 | p = pool_alloc_nocache(pool); |
| 746 | |
| 747 | if (likely(p)) { |
Willy Tarreau | facfad2 | 2022-08-17 09:12:53 +0200 | [diff] [blame] | 748 | #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 Tarreau | 42b180d | 2022-08-17 09:35:16 +0200 | [diff] [blame] | 755 | _HA_ATOMIC_STORE(&bin->info, pool); |
Willy Tarreau | facfad2 | 2022-08-17 09:12:53 +0200 | [diff] [blame] | 756 | } |
| 757 | #endif |
Willy Tarreau | d392973 | 2022-01-24 16:09:29 +0100 | [diff] [blame] | 758 | if (unlikely(flags & POOL_F_MUST_ZERO)) |
| 759 | memset(p, 0, pool->size); |
Willy Tarreau | ef301b7 | 2022-02-23 14:15:18 +0100 | [diff] [blame] | 760 | else if (unlikely(!(flags & POOL_F_NO_POISON) && (pool_debugging & POOL_DBG_POISON))) |
Willy Tarreau | d392973 | 2022-01-24 16:09:29 +0100 | [diff] [blame] | 761 | memset(p, mem_poison_byte, pool->size); |
| 762 | } |
| 763 | return p; |
| 764 | } |
| 765 | |
| 766 | /* |
Willy Tarreau | 15c322c | 2022-01-24 11:51:43 +0100 | [diff] [blame] | 767 | * Puts a memory area back to the corresponding pool. <ptr> be valid. Using |
| 768 | * pool_free() is preferred. |
| 769 | */ |
| 770 | void __pool_free(struct pool_head *pool, void *ptr) |
| 771 | { |
Willy Tarreau | 0271822 | 2022-02-23 10:10:33 +0100 | [diff] [blame] | 772 | const void *caller = __builtin_return_address(0); |
Willy Tarreau | 0e2a5b4 | 2022-01-24 15:51:50 +0100 | [diff] [blame] | 773 | |
Willy Tarreau | 15c322c | 2022-01-24 11:51:43 +0100 | [diff] [blame] | 774 | /* we'll get late corruption if we refill to the wrong pool or double-free */ |
| 775 | POOL_DEBUG_CHECK_MARK(pool, ptr); |
Willy Tarreau | 27c8da1 | 2022-02-09 16:49:16 +0100 | [diff] [blame] | 776 | POOL_DEBUG_RESET_MARK(pool, ptr); |
Willy Tarreau | e981631 | 2022-02-22 16:23:09 +0100 | [diff] [blame] | 777 | |
Willy Tarreau | facfad2 | 2022-08-17 09:12:53 +0200 | [diff] [blame] | 778 | #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 Tarreau | 42b180d | 2022-08-17 09:35:16 +0200 | [diff] [blame] | 785 | _HA_ATOMIC_STORE(&bin->info, pool); |
Willy Tarreau | facfad2 | 2022-08-17 09:12:53 +0200 | [diff] [blame] | 786 | } |
| 787 | #endif |
| 788 | |
Willy Tarreau | e981631 | 2022-02-22 16:23:09 +0100 | [diff] [blame] | 789 | if (unlikely(pool_debugging & POOL_DBG_NO_CACHE)) { |
| 790 | pool_free_nocache(pool, ptr); |
| 791 | return; |
| 792 | } |
| 793 | |
Willy Tarreau | 0e2a5b4 | 2022-01-24 15:51:50 +0100 | [diff] [blame] | 794 | pool_put_to_cache(pool, ptr, caller); |
Willy Tarreau | 15c322c | 2022-01-24 11:51:43 +0100 | [diff] [blame] | 795 | } |
| 796 | |
Willy Tarreau | f14d190 | 2021-10-05 18:14:11 +0200 | [diff] [blame] | 797 | |
| 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 | */ |
| 810 | void *pool_alloc_area_uaf(size_t size) |
| 811 | { |
| 812 | size_t pad = (4096 - size) & 0xFF0; |
Willy Tarreau | f14d190 | 2021-10-05 18:14:11 +0200 | [diff] [blame] | 813 | void *ret; |
| 814 | |
Willy Tarreau | f14d190 | 2021-10-05 18:14:11 +0200 | [diff] [blame] | 815 | 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 Tarreau | f14d190 | 2021-10-05 18:14:11 +0200 | [diff] [blame] | 827 | 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 | */ |
| 836 | void 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 Tarreau | f14d190 | 2021-10-05 18:14:11 +0200 | [diff] [blame] | 843 | munmap(area - pad, (size + 4095) & -4096); |
Willy Tarreau | f14d190 | 2021-10-05 18:14:11 +0200 | [diff] [blame] | 844 | } |
| 845 | |
| 846 | #endif /* DEBUG_UAF */ |
| 847 | |
Willy Tarreau | e6ce59d | 2007-05-13 19:38:49 +0200 | [diff] [blame] | 848 | /* |
Willy Tarreau | dae4aa8 | 2007-06-16 23:19:53 +0200 | [diff] [blame] | 849 | * 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 Tarreau | e6ce59d | 2007-05-13 19:38:49 +0200 | [diff] [blame] | 854 | */ |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 855 | void *pool_destroy(struct pool_head *pool) |
Willy Tarreau | e6ce59d | 2007-05-13 19:38:49 +0200 | [diff] [blame] | 856 | { |
Willy Tarreau | 4d2d098 | 2007-05-14 00:39:29 +0200 | [diff] [blame] | 857 | if (pool) { |
Willy Tarreau | e981631 | 2022-02-22 16:23:09 +0100 | [diff] [blame] | 858 | if (!(pool_debugging & POOL_DBG_NO_CACHE)) |
| 859 | pool_evict_from_local_cache(pool, 1); |
| 860 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 861 | pool_flush(pool); |
Willy Tarreau | dae4aa8 | 2007-06-16 23:19:53 +0200 | [diff] [blame] | 862 | if (pool->used) |
| 863 | return pool; |
| 864 | pool->users--; |
| 865 | if (!pool->users) { |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 866 | LIST_DELETE(&pool->list); |
Willy Tarreau | 9f3129e | 2021-04-17 00:31:38 +0200 | [diff] [blame] | 867 | /* note that if used == 0, the cache is empty */ |
Willy Tarreau | f9eba78 | 2022-03-03 18:31:54 +0100 | [diff] [blame] | 868 | free(pool->base_addr); |
Willy Tarreau | dae4aa8 | 2007-06-16 23:19:53 +0200 | [diff] [blame] | 869 | } |
Willy Tarreau | 4d2d098 | 2007-05-14 00:39:29 +0200 | [diff] [blame] | 870 | } |
| 871 | return NULL; |
Willy Tarreau | e6ce59d | 2007-05-13 19:38:49 +0200 | [diff] [blame] | 872 | } |
| 873 | |
Willy Tarreau | 2455ceb | 2018-11-26 15:57:34 +0100 | [diff] [blame] | 874 | /* This destroys all pools on exit. It is *not* thread safe. */ |
| 875 | void pool_destroy_all() |
| 876 | { |
| 877 | struct pool_head *entry, *back; |
| 878 | |
Willy Tarreau | 243e68b | 2022-04-27 11:33:13 +0200 | [diff] [blame] | 879 | 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 Tarreau | 2455ceb | 2018-11-26 15:57:34 +0100 | [diff] [blame] | 885 | pool_destroy(entry); |
Willy Tarreau | 243e68b | 2022-04-27 11:33:13 +0200 | [diff] [blame] | 886 | } |
Willy Tarreau | 2455ceb | 2018-11-26 15:57:34 +0100 | [diff] [blame] | 887 | } |
| 888 | |
Willy Tarreau | 2fba08f | 2022-11-21 09:34:02 +0100 | [diff] [blame] | 889 | /* used by qsort in "show pools" to sort by name */ |
| 890 | static 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 */ |
| 899 | static 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 */ |
| 913 | static 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 Tarreau | 224adf2 | 2022-11-21 09:02:41 +0100 | [diff] [blame] | 926 | /* 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 Tarreau | 2fba08f | 2022-11-21 09:34:02 +0100 | [diff] [blame] | 931 | /* 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 Tarreau | 7583c36 | 2022-11-21 10:02:29 +0100 | [diff] [blame] | 933 | * 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 Tarreau | 2fba08f | 2022-11-21 09:34:02 +0100 | [diff] [blame] | 935 | */ |
Willy Tarreau | 7583c36 | 2022-11-21 10:02:29 +0100 | [diff] [blame] | 936 | void dump_pools_to_trash(int by_what, int max, const char *pfx) |
Willy Tarreau | 50e608d | 2007-05-13 18:26:08 +0200 | [diff] [blame] | 937 | { |
Willy Tarreau | 224adf2 | 2022-11-21 09:02:41 +0100 | [diff] [blame] | 938 | struct pool_dump_info pool_info[POOLS_MAX_DUMPED_ENTRIES]; |
Willy Tarreau | 50e608d | 2007-05-13 18:26:08 +0200 | [diff] [blame] | 939 | struct pool_head *entry; |
Willy Tarreau | 0c5e989 | 2022-11-17 11:08:03 +0100 | [diff] [blame] | 940 | unsigned long long allocated, used; |
Willy Tarreau | 224adf2 | 2022-11-21 09:02:41 +0100 | [diff] [blame] | 941 | int nbpools, i; |
Willy Tarreau | 0c5e989 | 2022-11-17 11:08:03 +0100 | [diff] [blame] | 942 | unsigned long long cached_bytes = 0; |
Willy Tarreau | 1b4a714 | 2021-10-07 16:29:31 +0200 | [diff] [blame] | 943 | uint cached = 0; |
Willy Tarreau | 50e608d | 2007-05-13 18:26:08 +0200 | [diff] [blame] | 944 | |
| 945 | allocated = used = nbpools = 0; |
Willy Tarreau | 224adf2 | 2022-11-21 09:02:41 +0100 | [diff] [blame] | 946 | |
Willy Tarreau | 50e608d | 2007-05-13 18:26:08 +0200 | [diff] [blame] | 947 | list_for_each_entry(entry, &pools, list) { |
Willy Tarreau | 224adf2 | 2022-11-21 09:02:41 +0100 | [diff] [blame] | 948 | if (nbpools >= POOLS_MAX_DUMPED_ENTRIES) |
| 949 | break; |
| 950 | |
Willy Tarreau | 2fba08f | 2022-11-21 09:34:02 +0100 | [diff] [blame] | 951 | /* do not dump unused entries when sorting by usage */ |
| 952 | if (by_what == 3 && !entry->allocated) |
| 953 | continue; |
| 954 | |
Willy Tarreau | 7583c36 | 2022-11-21 10:02:29 +0100 | [diff] [blame] | 955 | /* verify the pool name if a prefix is requested */ |
| 956 | if (pfx && strncmp(entry->name, pfx, strlen(pfx)) != 0) |
| 957 | continue; |
| 958 | |
Willy Tarreau | e981631 | 2022-02-22 16:23:09 +0100 | [diff] [blame] | 959 | if (!(pool_debugging & POOL_DBG_NO_CACHE)) { |
Willy Tarreau | e981631 | 2022-02-22 16:23:09 +0100 | [diff] [blame] | 960 | for (cached = i = 0; i < global.nbthread; i++) |
| 961 | cached += entry->cache[i].count; |
Willy Tarreau | e981631 | 2022-02-22 16:23:09 +0100 | [diff] [blame] | 962 | } |
Willy Tarreau | 224adf2 | 2022-11-21 09:02:41 +0100 | [diff] [blame] | 963 | 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 Tarreau | 50e608d | 2007-05-13 18:26:08 +0200 | [diff] [blame] | 970 | nbpools++; |
| 971 | } |
Willy Tarreau | 224adf2 | 2022-11-21 09:02:41 +0100 | [diff] [blame] | 972 | |
Willy Tarreau | 2fba08f | 2022-11-21 09:34:02 +0100 | [diff] [blame] | 973 | 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 Tarreau | 224adf2 | 2022-11-21 09:02:41 +0100 | [diff] [blame] | 980 | chunk_printf(&trash, "Dumping pools usage"); |
Willy Tarreau | 2fba08f | 2022-11-21 09:34:02 +0100 | [diff] [blame] | 981 | 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 Tarreau | 224adf2 | 2022-11-21 09:02:41 +0100 | [diff] [blame] | 985 | chunk_appendf(&trash, ". Use SIGQUIT to flush them.\n"); |
| 986 | |
Willy Tarreau | 2fba08f | 2022-11-21 09:34:02 +0100 | [diff] [blame] | 987 | for (i = 0; i < nbpools && i < max; i++) { |
Willy Tarreau | 224adf2 | 2022-11-21 09:02:41 +0100 | [diff] [blame] | 988 | 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 Tarreau | 0c5e989 | 2022-11-17 11:08:03 +0100 | [diff] [blame] | 1003 | chunk_appendf(&trash, "Total: %d pools, %llu bytes allocated, %llu used" |
| 1004 | " (~%llu by thread caches)" |
Willy Tarreau | 1b4a714 | 2021-10-07 16:29:31 +0200 | [diff] [blame] | 1005 | ".\n", |
Willy Tarreau | e981631 | 2022-02-22 16:23:09 +0100 | [diff] [blame] | 1006 | nbpools, allocated, used, cached_bytes |
Willy Tarreau | 1b4a714 | 2021-10-07 16:29:31 +0200 | [diff] [blame] | 1007 | ); |
Willy Tarreau | 50e608d | 2007-05-13 18:26:08 +0200 | [diff] [blame] | 1008 | } |
| 1009 | |
Willy Tarreau | 12833bb | 2014-01-28 16:49:56 +0100 | [diff] [blame] | 1010 | /* Dump statistics on pools usage. */ |
| 1011 | void dump_pools(void) |
| 1012 | { |
Willy Tarreau | 7583c36 | 2022-11-21 10:02:29 +0100 | [diff] [blame] | 1013 | dump_pools_to_trash(0, 0, NULL); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1014 | qfprintf(stderr, "%s", trash.area); |
Willy Tarreau | 12833bb | 2014-01-28 16:49:56 +0100 | [diff] [blame] | 1015 | } |
| 1016 | |
Willy Tarreau | 58102cf | 2015-10-28 16:24:21 +0100 | [diff] [blame] | 1017 | /* This function returns the total number of failed pool allocations */ |
| 1018 | int 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) */ |
| 1029 | unsigned 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) */ |
| 1040 | unsigned 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 Tarreau | 1408b1f | 2022-02-18 18:54:40 +0100 | [diff] [blame] | 1050 | /* 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 | */ |
| 1059 | int pool_parse_debugging(const char *str, char **err) |
| 1060 | { |
Willy Tarreau | f4b79c4 | 2022-02-23 15:20:53 +0100 | [diff] [blame] | 1061 | struct ist args; |
Willy Tarreau | 1408b1f | 2022-02-18 18:54:40 +0100 | [diff] [blame] | 1062 | char *end; |
Willy Tarreau | f4b79c4 | 2022-02-23 15:20:53 +0100 | [diff] [blame] | 1063 | uint new_dbg; |
Willy Tarreau | 1408b1f | 2022-02-18 18:54:40 +0100 | [diff] [blame] | 1064 | 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 Tarreau | f4b79c4 | 2022-02-23 15:20:53 +0100 | [diff] [blame] | 1077 | |
| 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 Tarreau | 1408b1f | 2022-02-18 18:54:40 +0100 | [diff] [blame] | 1126 | return 1; |
| 1127 | } |
| 1128 | |
Willy Tarreau | 2fba08f | 2022-11-21 09:34:02 +0100 | [diff] [blame] | 1129 | /* parse a "show pools" command. It returns 1 on failure, 0 if it starts to dump. */ |
| 1130 | static 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 Tarreau | 7583c36 | 2022-11-21 10:02:29 +0100 | [diff] [blame] | 1145 | 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 Tarreau | 2fba08f | 2022-11-21 09:34:02 +0100 | [diff] [blame] | 1149 | else if (isdigit((unsigned char)*args[arg])) { |
| 1150 | ctx->maxcnt = atoi(args[arg]); // number of entries to dump |
| 1151 | } |
| 1152 | else |
Willy Tarreau | 7583c36 | 2022-11-21 10:02:29 +0100 | [diff] [blame] | 1153 | return cli_err(appctx, "Expects either 'byname', 'bysize', 'byusage', 'match <pfx>', or a max number of output lines.\n"); |
Willy Tarreau | 2fba08f | 2022-11-21 09:34:02 +0100 | [diff] [blame] | 1154 | } |
| 1155 | return 0; |
| 1156 | } |
| 1157 | |
Willy Tarreau | 7583c36 | 2022-11-21 10:02:29 +0100 | [diff] [blame] | 1158 | /* release the "show pools" context */ |
| 1159 | static 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 Tarreau | 4596fe2 | 2022-05-17 19:07:51 +0200 | [diff] [blame] | 1166 | /* This function dumps memory usage information onto the stream connector's |
William Lallemand | e7ed885 | 2016-11-19 02:25:36 +0100 | [diff] [blame] | 1167 | * read buffer. It returns 0 as long as it does not complete, non-zero upon |
| 1168 | * completion. No state is used. |
| 1169 | */ |
| 1170 | static int cli_io_handler_dump_pools(struct appctx *appctx) |
| 1171 | { |
Willy Tarreau | 2fba08f | 2022-11-21 09:34:02 +0100 | [diff] [blame] | 1172 | struct show_pools_ctx *ctx = appctx->svcctx; |
| 1173 | |
Willy Tarreau | 7583c36 | 2022-11-21 10:02:29 +0100 | [diff] [blame] | 1174 | dump_pools_to_trash(ctx->by_what, ctx->maxcnt, ctx->prefix); |
Willy Tarreau | d0a06d5 | 2022-05-18 15:07:19 +0200 | [diff] [blame] | 1175 | if (applet_putchk(appctx, &trash) == -1) |
William Lallemand | e7ed885 | 2016-11-19 02:25:36 +0100 | [diff] [blame] | 1176 | return 0; |
William Lallemand | e7ed885 | 2016-11-19 02:25:36 +0100 | [diff] [blame] | 1177 | return 1; |
| 1178 | } |
| 1179 | |
Willy Tarreau | 7107c8b | 2018-11-26 11:44:35 +0100 | [diff] [blame] | 1180 | /* 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 | */ |
| 1184 | void 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 Tarreau | 7f0165e | 2018-11-26 17:09:46 +0100 | [diff] [blame] | 1194 | /* Initializes all per-thread arrays on startup */ |
| 1195 | static void init_pools() |
| 1196 | { |
Willy Tarreau | 9f3129e | 2021-04-17 00:31:38 +0200 | [diff] [blame] | 1197 | int thr; |
Willy Tarreau | 7f0165e | 2018-11-26 17:09:46 +0100 | [diff] [blame] | 1198 | |
| 1199 | for (thr = 0; thr < MAX_THREADS; thr++) { |
Willy Tarreau | b4e3476 | 2021-09-30 19:02:18 +0200 | [diff] [blame] | 1200 | LIST_INIT(&ha_thread_ctx[thr].pool_lru_head); |
Willy Tarreau | 7f0165e | 2018-11-26 17:09:46 +0100 | [diff] [blame] | 1201 | } |
Willy Tarreau | e981631 | 2022-02-22 16:23:09 +0100 | [diff] [blame] | 1202 | |
Willy Tarreau | 157e393 | 2021-09-15 10:05:48 +0200 | [diff] [blame] | 1203 | detect_allocator(); |
Willy Tarreau | 7f0165e | 2018-11-26 17:09:46 +0100 | [diff] [blame] | 1204 | } |
| 1205 | |
| 1206 | INITCALL0(STG_PREPARE, init_pools); |
Willy Tarreau | 7107c8b | 2018-11-26 11:44:35 +0100 | [diff] [blame] | 1207 | |
Willy Tarreau | 845b560 | 2021-09-15 10:41:24 +0200 | [diff] [blame] | 1208 | /* Report in build options if trim is supported */ |
| 1209 | static 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 | } |
| 1217 | INITCALL0(STG_REGISTER, pools_register_build_options); |
| 1218 | |
William Lallemand | e7ed885 | 2016-11-19 02:25:36 +0100 | [diff] [blame] | 1219 | /* register cli keywords */ |
| 1220 | static struct cli_kw_list cli_kws = {{ },{ |
Willy Tarreau | 7583c36 | 2022-11-21 10:02:29 +0100 | [diff] [blame] | 1221 | { { "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 Lallemand | e7ed885 | 2016-11-19 02:25:36 +0100 | [diff] [blame] | 1222 | {{},} |
| 1223 | }}; |
| 1224 | |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 1225 | INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws); |
William Lallemand | e7ed885 | 2016-11-19 02:25:36 +0100 | [diff] [blame] | 1226 | |
Olivier Houchard | dc21ff7 | 2019-01-29 15:20:16 +0100 | [diff] [blame] | 1227 | |
| 1228 | /* config parser for global "tune.fail-alloc" */ |
| 1229 | static int mem_parse_global_fail_alloc(char **args, int section_type, struct proxy *curpx, |
Amaury Denoyelle | 3b1c9a3 | 2021-03-22 11:21:36 +0100 | [diff] [blame] | 1230 | const struct proxy *defpx, const char *file, int line, |
| 1231 | char **err) |
Olivier Houchard | dc21ff7 | 2019-01-29 15:20:16 +0100 | [diff] [blame] | 1232 | { |
| 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 Houchard | dc21ff7 | 2019-01-29 15:20:16 +0100 | [diff] [blame] | 1242 | |
Willy Tarreau | c4e56dc | 2022-03-08 10:41:40 +0100 | [diff] [blame] | 1243 | /* config parser for global "no-memory-trimming" */ |
| 1244 | static 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 Houchard | dc21ff7 | 2019-01-29 15:20:16 +0100 | [diff] [blame] | 1254 | /* register global config keywords */ |
| 1255 | static struct cfg_kw_list mem_cfg_kws = {ILH, { |
Olivier Houchard | dc21ff7 | 2019-01-29 15:20:16 +0100 | [diff] [blame] | 1256 | { CFG_GLOBAL, "tune.fail-alloc", mem_parse_global_fail_alloc }, |
Willy Tarreau | c4e56dc | 2022-03-08 10:41:40 +0100 | [diff] [blame] | 1257 | { CFG_GLOBAL, "no-memory-trimming", mem_parse_global_no_mem_trim }, |
Olivier Houchard | dc21ff7 | 2019-01-29 15:20:16 +0100 | [diff] [blame] | 1258 | { 0, NULL, NULL } |
| 1259 | }}; |
| 1260 | |
| 1261 | INITCALL1(STG_REGISTER, cfg_register_keywords, &mem_cfg_kws); |
| 1262 | |
Willy Tarreau | 50e608d | 2007-05-13 18:26:08 +0200 | [diff] [blame] | 1263 | /* |
| 1264 | * Local variables: |
| 1265 | * c-indent-level: 8 |
| 1266 | * c-basic-offset: 8 |
| 1267 | * End: |
| 1268 | */ |