2007-05-13 12:26:08 -04:00
/*
* Memory management functions .
*
* Copyright 2000 - 2007 Willy Tarreau < w @ 1 wt . eu >
*
* This program is free software ; you can redistribute it and / or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation ; either version
* 2 of the License , or ( at your option ) any later version .
*
*/
2021-10-05 12:14:11 -04:00
2018-11-26 05:44:35 -05:00
# include <errno.h>
2007-05-13 12:26:08 -04:00
2023-08-26 11:22:32 -04:00
# include <import/plock.h>
2021-10-06 13:54:09 -04:00
# include <haproxy/activity.h>
2020-05-27 06:58:42 -04:00
# include <haproxy/api.h>
2020-06-09 03:07:15 -04:00
# include <haproxy/applet-t.h>
2020-06-04 18:00:29 -04:00
# include <haproxy/cfgparse.h>
2020-06-04 15:07:02 -04:00
# include <haproxy/channel.h>
2020-06-04 14:19:54 -04:00
# include <haproxy/cli.h>
2020-06-05 11:27:29 -04:00
# include <haproxy/errors.h>
2020-06-04 11:05:57 -04:00
# include <haproxy/global.h>
2020-05-27 12:01:47 -04:00
# include <haproxy/list.h>
2020-06-09 03:07:15 -04:00
# include <haproxy/pool.h>
2022-12-08 11:26:50 -05:00
# include <haproxy/pool-os.h>
2022-05-27 03:25:10 -04:00
# include <haproxy/sc_strm.h>
2020-06-04 13:58:55 -04:00
# include <haproxy/stats-t.h>
2022-05-27 03:47:12 -04:00
# include <haproxy/stconn.h>
2020-06-09 03:07:15 -04:00
# include <haproxy/thread.h>
2020-06-03 12:09:46 -04:00
# include <haproxy/tools.h>
2007-05-13 12:26:08 -04:00
2018-11-26 11:09:46 -05:00
/* These ones are initialized per-thread on startup by init_pools() */
2018-10-16 04:28:54 -04:00
THREAD_LOCAL size_t pool_cache_bytes = 0 ; /* total cache size */
THREAD_LOCAL size_t pool_cache_count = 0 ; /* #cache objects */
2022-02-18 12:31:53 -05:00
static struct list pools __read_mostly = LIST_HEAD_INIT ( pools ) ;
2022-02-23 08:15:18 -05:00
int mem_poison_byte __read_mostly = ' P ' ;
2023-10-25 09:42:27 -04:00
int pool_trim_in_progress = 0 ;
2022-02-21 11:16:22 -05:00
uint pool_debugging __read_mostly = /* set of POOL_DBG_* flags */
2019-01-29 09:20:16 -05:00
# ifdef DEBUG_FAIL_ALLOC
2022-02-21 11:16:22 -05:00
POOL_DBG_FAIL_ALLOC |
2022-02-21 11:31:50 -05:00
# endif
# ifdef DEBUG_DONT_SHARE_POOLS
POOL_DBG_DONT_MERGE |
2022-02-21 12:30:25 -05:00
# endif
# ifdef DEBUG_POOL_INTEGRITY
POOL_DBG_COLD_FIRST |
2022-02-21 12:42:53 -05:00
# endif
# ifdef DEBUG_POOL_INTEGRITY
POOL_DBG_INTEGRITY |
2022-02-22 03:21:13 -05:00
# endif
# ifdef CONFIG_HAP_NO_GLOBAL_POOLS
POOL_DBG_NO_GLOBAL |
2022-02-22 10:23:09 -05:00
# endif
2022-12-08 11:45:08 -05:00
# if defined(DEBUG_NO_POOLS) || defined(DEBUG_UAF)
2022-02-22 10:23:09 -05:00
POOL_DBG_NO_CACHE |
2022-02-23 04:10:33 -05:00
# endif
# if defined(DEBUG_POOL_TRACING)
POOL_DBG_CALLER |
2022-02-23 04:20:37 -05:00
# endif
# if defined(DEBUG_MEMORY_POOLS)
POOL_DBG_TAG |
MINOR: pools: make DEBUG_UAF a runtime setting
Since the massive pools cleanup that happened in 2.6, the pools
architecture was made quite more hierarchical and many alternate code
blocks could be moved to runtime flags set by -dM. One of them had not
been converted by then, DEBUG_UAF. It's not much more difficult actually,
since it only acts on a pair of functions indirection on the slow path
(OS-level allocator) and a default setting for the cache activation.
This patch adds the "uaf" setting to the options permitted in -dM so
that it now becomes possible to set or unset UAF at boot time without
recompiling. This is particularly convenient, because every 3 months on
average, developers ask a user to recompile haproxy with DEBUG_UAF to
understand a bug. Now it will not be needed anymore, instead the user
will only have to disable pools and enable uaf using -dMuaf. Note that
-dMuaf only disables previously enabled pools, but it remains possible
to re-enable caching by specifying the cache after, like -dMuaf,cache.
A few tests with this mode show that it can be an interesting combination
which catches significantly less UAF but will do so with much less
overhead, so it might be compatible with some high-traffic deployments.
The change is very small and isolated. It could be helpful to backport
this at least to 2.7 once confirmed not to cause build issues on exotic
systems, and even to 2.6 a bit later as this has proven to be useful
over time, and could be even more if it did not require a rebuild. If
a backport is desired, the following patches are needed as well:
CLEANUP: pools: move the write before free to the uaf-only function
CLEANUP: pool: only include pool-os from pool.c not pool.h
REORG: pool: move all the OS specific code to pool-os.h
CLEANUP: pools: get rid of CONFIG_HAP_POOLS
DEBUG: pool: show a few examples in -dMhelp
2022-12-08 11:47:59 -05:00
# endif
# if defined(DEBUG_UAF)
POOL_DBG_UAF |
2019-01-29 09:20:16 -05:00
# endif
2022-02-21 11:16:22 -05:00
0 ;
2019-01-29 09:20:16 -05:00
2022-02-23 09:20:53 -05:00
static const struct {
uint flg ;
const char * set ;
const char * clr ;
const char * hlp ;
} dbg_options [ ] = {
/* flg, set, clr, hlp */
{ POOL_DBG_FAIL_ALLOC , " fail " , " no-fail " , " randomly fail allocations " } ,
{ POOL_DBG_DONT_MERGE , " no-merge " , " merge " , " disable merging of similar pools " } ,
{ POOL_DBG_COLD_FIRST , " cold-first " , " hot-first " , " pick cold objects first " } ,
{ POOL_DBG_INTEGRITY , " integrity " , " no-integrity " , " enable cache integrity checks " } ,
{ POOL_DBG_NO_GLOBAL , " no-global " , " global " , " disable global shared cache " } ,
{ POOL_DBG_NO_CACHE , " no-cache " , " cache " , " disable thread-local cache " } ,
{ POOL_DBG_CALLER , " caller " , " no-caller " , " save caller information in cache " } ,
{ POOL_DBG_TAG , " tag " , " no-tag " , " add tag at end of allocated objects " } ,
{ POOL_DBG_POISON , " poison " , " no-poison " , " poison newly allocated objects " } ,
MINOR: pools: make DEBUG_UAF a runtime setting
Since the massive pools cleanup that happened in 2.6, the pools
architecture was made quite more hierarchical and many alternate code
blocks could be moved to runtime flags set by -dM. One of them had not
been converted by then, DEBUG_UAF. It's not much more difficult actually,
since it only acts on a pair of functions indirection on the slow path
(OS-level allocator) and a default setting for the cache activation.
This patch adds the "uaf" setting to the options permitted in -dM so
that it now becomes possible to set or unset UAF at boot time without
recompiling. This is particularly convenient, because every 3 months on
average, developers ask a user to recompile haproxy with DEBUG_UAF to
understand a bug. Now it will not be needed anymore, instead the user
will only have to disable pools and enable uaf using -dMuaf. Note that
-dMuaf only disables previously enabled pools, but it remains possible
to re-enable caching by specifying the cache after, like -dMuaf,cache.
A few tests with this mode show that it can be an interesting combination
which catches significantly less UAF but will do so with much less
overhead, so it might be compatible with some high-traffic deployments.
The change is very small and isolated. It could be helpful to backport
this at least to 2.7 once confirmed not to cause build issues on exotic
systems, and even to 2.6 a bit later as this has proven to be useful
over time, and could be even more if it did not require a rebuild. If
a backport is desired, the following patches are needed as well:
CLEANUP: pools: move the write before free to the uaf-only function
CLEANUP: pool: only include pool-os from pool.c not pool.h
REORG: pool: move all the OS specific code to pool-os.h
CLEANUP: pools: get rid of CONFIG_HAP_POOLS
DEBUG: pool: show a few examples in -dMhelp
2022-12-08 11:47:59 -05:00
{ POOL_DBG_UAF , " uaf " , " no-uaf " , " enable use-after-free checks (slow) " } ,
2022-02-23 09:20:53 -05:00
{ 0 /* end */ }
} ;
2022-11-21 03:02:41 -05:00
/* describes a snapshot of a pool line about to be dumped by "show pools" */
struct pool_dump_info {
const struct pool_head * entry ;
ulong alloc_items ;
ulong alloc_bytes ;
ulong used_items ;
ulong cached_items ;
ulong need_avg ;
ulong failed_items ;
} ;
2022-11-21 03:34:02 -05:00
/* context used by "show pools" */
struct show_pools_ctx {
MINOR: cli/pools: add pool name filtering capability to "show pools"
Now it becomes possible to match a pool name's prefix, for example:
$ socat - /tmp/haproxy.sock <<< "show pools match quic byusage"
Dumping pools usage. Use SIGQUIT to flush them.
- Pool quic_conn_r (65560 bytes) : 1337 allocated (87653720 bytes), ...
- Pool quic_crypto (1048 bytes) : 6685 allocated (7005880 bytes), ...
- Pool quic_conn (4056 bytes) : 1337 allocated (5422872 bytes), ...
- Pool quic_rxbuf (262168 bytes) : 8 allocated (2097344 bytes), ...
- Pool quic_connne (184 bytes) : 9359 allocated (1722056 bytes), ...
- Pool quic_frame (184 bytes) : 7938 allocated (1460592 bytes), ...
- Pool quic_tx_pac (152 bytes) : 6454 allocated (981008 bytes), ...
- Pool quic_tls_ke (56 bytes) : 12033 allocated (673848 bytes), ...
- Pool quic_rx_pac (408 bytes) : 1596 allocated (651168 bytes), ...
- Pool quic_tls_se (88 bytes) : 6685 allocated (588280 bytes), ...
- Pool quic_cstrea (88 bytes) : 4011 allocated (352968 bytes), ...
- Pool quic_tls_iv (24 bytes) : 12033 allocated (288792 bytes), ...
- Pool quic_dgram (344 bytes) : 732 allocated (251808 bytes), ...
- Pool quic_arng (56 bytes) : 4011 allocated (224616 bytes), ...
- Pool quic_conn_c (152 bytes) : 1337 allocated (203224 bytes), ...
Total: 15 pools, 109578176 bytes allocated, 109578176 used ...
In this case the reported total only concerns the dumped ones.
2022-11-21 04:02:29 -05:00
char * prefix ; /* if non-null, match this prefix name for the pool */
2022-11-21 03:34:02 -05:00
int by_what ; /* 0=no sort, 1=by name, 2=by item size, 3=by total alloc */
int maxcnt ; /* 0=no limit, other=max number of output entries */
} ;
2022-02-21 11:16:22 -05:00
static int mem_fail_rate __read_mostly = 0 ;
2023-03-22 12:52:05 -04:00
static int using_default_allocator __read_mostly = 1 ; // linked-in allocator or LD_PRELOADed one ?
2022-03-08 04:41:40 -05:00
static int disable_trim __read_mostly = 0 ;
2021-11-25 11:09:45 -05:00
static int ( * my_mallctl ) ( const char * , void * , size_t * , void * , size_t ) = NULL ;
2023-03-22 09:55:25 -04:00
static int ( * _malloc_trim ) ( size_t ) = NULL ;
2021-09-15 04:05:48 -04:00
MINOR: pools: introduce the use of multiple buckets
On many threads and without the shared cache, there can be extreme
contention on the ->allocated counter, the ->free_list pointer, and
the ->used counter. It's possible to limit this contention by spreading
the counters a little bit over multiple entries, that are summed up when
a consultation is needed. The criterion used to spread the values cannot
be related to the thread ID due to migrations, since we need to keep
consistent stats (allocated vs used).
Instead we'll just hash the pointer, it provides an index that does the
job and that is consistent for the object. When having just a few entries
(16 here as it showed almost identical performance between global and
non-global pools) even iterations should be short enough during
measurements to not be a problem.
A pair of functions designed to ease pointer hash bucket calculation were
added, with one of them doing it for thread IDs because allocation failures
will be associated with a thread and not a pointer.
For now this patch only brings in the relevant parts of the infrastructure,
the CONFIG_HAP_POOL_BUCKETS_BITS macro that defaults to 6 bits when 512
threads or more are supported, 5 bits when 128 or more are supported, 4
bits when 16 or more are supported, otherwise 3 bits for small setups.
The array in the pool_head and the two utility functions are already
added. It should have no measurable impact beyond inflating the pool_head
structure.
2023-08-12 05:22:27 -04:00
/* returns the pool hash bucket an object should use based on its pointer.
* Objects will needed consistent bucket assignment so that they may be
* allocated on one thread and released on another one . Thus only the
* pointer is usable .
*/
2023-08-12 13:58:17 -04:00
static forceinline unsigned int pool_pbucket ( const void * ptr )
MINOR: pools: introduce the use of multiple buckets
On many threads and without the shared cache, there can be extreme
contention on the ->allocated counter, the ->free_list pointer, and
the ->used counter. It's possible to limit this contention by spreading
the counters a little bit over multiple entries, that are summed up when
a consultation is needed. The criterion used to spread the values cannot
be related to the thread ID due to migrations, since we need to keep
consistent stats (allocated vs used).
Instead we'll just hash the pointer, it provides an index that does the
job and that is consistent for the object. When having just a few entries
(16 here as it showed almost identical performance between global and
non-global pools) even iterations should be short enough during
measurements to not be a problem.
A pair of functions designed to ease pointer hash bucket calculation were
added, with one of them doing it for thread IDs because allocation failures
will be associated with a thread and not a pointer.
For now this patch only brings in the relevant parts of the infrastructure,
the CONFIG_HAP_POOL_BUCKETS_BITS macro that defaults to 6 bits when 512
threads or more are supported, 5 bits when 128 or more are supported, 4
bits when 16 or more are supported, otherwise 3 bits for small setups.
The array in the pool_head and the two utility functions are already
added. It should have no measurable impact beyond inflating the pool_head
structure.
2023-08-12 05:22:27 -04:00
{
return ptr_hash ( ptr , CONFIG_HAP_POOL_BUCKETS_BITS ) ;
}
/* returns the pool hash bucket to use for the current thread. This should only
* be used when no pointer is available ( e . g . count alloc failures ) .
*/
2023-08-12 13:58:17 -04:00
static forceinline unsigned int pool_tbucket ( void )
MINOR: pools: introduce the use of multiple buckets
On many threads and without the shared cache, there can be extreme
contention on the ->allocated counter, the ->free_list pointer, and
the ->used counter. It's possible to limit this contention by spreading
the counters a little bit over multiple entries, that are summed up when
a consultation is needed. The criterion used to spread the values cannot
be related to the thread ID due to migrations, since we need to keep
consistent stats (allocated vs used).
Instead we'll just hash the pointer, it provides an index that does the
job and that is consistent for the object. When having just a few entries
(16 here as it showed almost identical performance between global and
non-global pools) even iterations should be short enough during
measurements to not be a problem.
A pair of functions designed to ease pointer hash bucket calculation were
added, with one of them doing it for thread IDs because allocation failures
will be associated with a thread and not a pointer.
For now this patch only brings in the relevant parts of the infrastructure,
the CONFIG_HAP_POOL_BUCKETS_BITS macro that defaults to 6 bits when 512
threads or more are supported, 5 bits when 128 or more are supported, 4
bits when 16 or more are supported, otherwise 3 bits for small setups.
The array in the pool_head and the two utility functions are already
added. It should have no measurable impact beyond inflating the pool_head
structure.
2023-08-12 05:22:27 -04:00
{
return tid % CONFIG_HAP_POOL_BUCKETS ;
}
2021-12-23 03:26:30 -05:00
/* ask the allocator to trim memory pools.
* This must run under thread isolation so that competing threads trying to
* allocate or release memory do not prevent the allocator from completing
* its job . We just have to be careful as callers might already be isolated
* themselves .
*/
2023-03-22 10:36:29 -04:00
void trim_all_pools ( void )
2021-09-15 04:38:21 -04:00
{
2021-12-23 03:26:30 -05:00
int isolated = thread_isolated ( ) ;
if ( ! isolated )
thread_isolate ( ) ;
2023-03-22 10:32:58 -04:00
malloc_trim ( 0 ) ;
2021-12-23 03:26:30 -05:00
if ( ! isolated )
thread_release ( ) ;
2021-09-15 04:38:21 -04:00
}
2021-09-15 04:05:48 -04:00
/* check if we're using the same allocator as the one that provides
* malloc_trim ( ) and mallinfo ( ) . The principle is that on glibc , both
* malloc_trim ( ) and mallinfo ( ) are provided , and using mallinfo ( ) we
* can check if malloc ( ) is performed through glibc or any other one
2021-11-25 11:09:45 -05:00
* the executable was linked against ( e . g . jemalloc ) . Prior to this we
* have to check whether we ' re running on jemalloc by verifying if the
* mallctl ( ) function is provided . Its pointer will be used later .
2021-09-15 04:05:48 -04:00
*/
static void detect_allocator ( void )
{
2021-11-26 09:55:55 -05:00
# if defined(__ELF__)
2021-11-25 11:09:45 -05:00
extern int mallctl ( const char * , void * , size_t * , void * , size_t ) __attribute__ ( ( weak ) ) ;
my_mallctl = mallctl ;
2021-11-26 09:55:55 -05:00
# endif
2023-03-22 12:52:05 -04:00
if ( ! my_mallctl ) {
/* trick: we won't enter here if mallctl() is known at link
* time . This allows to detect if the symbol was changed since
* the program was linked , indicating it ' s not running on the
* expected allocator ( due to an LD_PRELOAD ) and that we must
* be extra cautious and avoid some optimizations that are
* known to break such as malloc_trim ( ) .
*/
2021-11-25 11:09:45 -05:00
my_mallctl = get_sym_curr_addr ( " mallctl " ) ;
2023-03-22 12:52:05 -04:00
using_default_allocator = ( my_mallctl = = NULL ) ;
}
2021-11-25 11:09:45 -05:00
if ( ! my_mallctl ) {
# if defined(HA_HAVE_MALLOC_TRIM)
2021-09-16 03:18:21 -04:00
# ifdef HA_HAVE_MALLINFO2
2021-11-25 11:09:45 -05:00
struct mallinfo2 mi1 , mi2 ;
2021-09-16 03:18:21 -04:00
# else
2021-11-25 11:09:45 -05:00
struct mallinfo mi1 , mi2 ;
2021-09-16 03:18:21 -04:00
# endif
2021-11-25 11:09:45 -05:00
void * ptr ;
2021-09-15 04:05:48 -04:00
2021-09-16 03:18:21 -04:00
# ifdef HA_HAVE_MALLINFO2
2021-11-25 11:09:45 -05:00
mi1 = mallinfo2 ( ) ;
2021-09-16 03:18:21 -04:00
# else
2021-11-25 11:09:45 -05:00
mi1 = mallinfo ( ) ;
2021-09-16 03:18:21 -04:00
# endif
2021-11-25 11:09:45 -05:00
ptr = DISGUISE ( malloc ( 1 ) ) ;
2021-09-16 03:18:21 -04:00
# ifdef HA_HAVE_MALLINFO2
2021-11-25 11:09:45 -05:00
mi2 = mallinfo2 ( ) ;
2021-09-16 03:18:21 -04:00
# else
2021-11-25 11:09:45 -05:00
mi2 = mallinfo ( ) ;
2021-09-16 03:18:21 -04:00
# endif
2021-11-25 11:09:45 -05:00
free ( DISGUISE ( ptr ) ) ;
2021-09-15 04:38:21 -04:00
2021-11-25 11:09:45 -05:00
using_default_allocator = ! ! memcmp ( & mi1 , & mi2 , sizeof ( mi1 ) ) ;
2021-11-26 15:44:44 -05:00
# elif defined(HA_HAVE_MALLOC_ZONE)
using_default_allocator = ( malloc_default_zone ( ) ! = NULL ) ;
2021-11-25 11:09:45 -05:00
# endif
}
2023-03-22 09:55:25 -04:00
/* detect presence of malloc_trim() */
_malloc_trim = get_sym_next_addr ( " malloc_trim " ) ;
2021-09-15 04:05:48 -04:00
}
2021-09-15 04:41:24 -04:00
2023-03-22 09:55:25 -04:00
/* replace the libc's malloc_trim() so that we can also intercept the calls
* from child libraries when the allocator is not the default one .
*/
int malloc_trim ( size_t pad )
{
int ret = 0 ;
if ( disable_trim )
return ret ;
2023-10-25 09:42:27 -04:00
HA_ATOMIC_INC ( & pool_trim_in_progress ) ;
2023-03-22 10:32:58 -04:00
if ( my_mallctl ) {
/* here we're on jemalloc and malloc_trim() is called either
* by haproxy or another dependency ( the worst case that
* normally crashes ) . Instead of just failing , we can actually
* emulate it so let ' s do it now .
*/
unsigned int i , narenas = 0 ;
size_t len = sizeof ( narenas ) ;
if ( my_mallctl ( " arenas.narenas " , & narenas , & len , NULL , 0 ) = = 0 ) {
for ( i = 0 ; i < narenas ; i + + ) {
char mib [ 32 ] = { 0 } ;
snprintf ( mib , sizeof ( mib ) , " arena.%u.purge " , i ) ;
( void ) my_mallctl ( mib , NULL , NULL , NULL , 0 ) ;
ret = 1 ; // success
}
}
}
else if ( ! using_default_allocator ) {
/* special allocators that can be LD_PRELOADed end here */
ret = 0 ; // did nothing
}
else if ( _malloc_trim ) {
2023-03-22 09:55:25 -04:00
/* we're typically on glibc and not overridden */
ret = _malloc_trim ( pad ) ;
}
2023-03-22 10:32:58 -04:00
# if defined(HA_HAVE_MALLOC_ZONE)
else {
/* we're on MacOS, there's an equivalent mechanism */
vm_address_t * zones ;
unsigned int i , nzones ;
if ( malloc_get_all_zones ( 0 , NULL , & zones , & nzones ) = = KERN_SUCCESS ) {
for ( i = 0 ; i < nzones ; i + + ) {
malloc_zone_t * zone = ( malloc_zone_t * ) zones [ i ] ;
/* we cannot purge anonymous zones */
if ( zone - > zone_name ) {
malloc_zone_pressure_relief ( zone , 0 ) ;
ret = 1 ; // success
}
}
}
}
# endif
2023-10-25 09:42:27 -04:00
HA_ATOMIC_DEC ( & pool_trim_in_progress ) ;
2023-03-22 10:32:58 -04:00
/* here we have ret=0 if nothing was release, or 1 if some were */
2023-03-22 09:55:25 -04:00
return ret ;
}
2022-02-21 11:16:22 -05:00
static int mem_should_fail ( const struct pool_head * pool )
{
int ret = 0 ;
if ( mem_fail_rate > 0 & & ! ( global . mode & MODE_STARTING ) ) {
if ( mem_fail_rate > statistical_prng_range ( 100 ) )
ret = 1 ;
else
ret = 0 ;
}
return ret ;
}
2007-05-13 12:26:08 -04:00
/* Try to find an existing shared pool with the same characteristics and
* returns it , otherwise creates this one . NULL is returned if no memory
2016-01-24 20:19:13 -05:00
* is available for a new creation . Two flags are supported :
* - MEM_F_SHARED to indicate that the pool may be shared with other users
* - MEM_F_EXACT to indicate that the size must not be rounded up
2007-05-13 12:26:08 -04:00
*/
struct pool_head * create_pool ( char * name , unsigned int size , unsigned int flags )
{
2022-02-23 04:03:11 -05:00
unsigned int extra_mark , extra_caller , extra ;
2007-05-13 12:26:08 -04:00
struct pool_head * pool ;
2007-05-13 18:16:13 -04:00
struct pool_head * entry ;
struct list * start ;
2007-05-13 12:26:08 -04:00
unsigned int align ;
2021-04-16 18:31:38 -04:00
int thr __maybe_unused ;
2007-05-13 12:26:08 -04:00
2022-02-23 04:20:37 -05:00
extra_mark = ( pool_debugging & POOL_DBG_TAG ) ? POOL_EXTRA_MARK : 0 ;
2022-02-23 04:10:33 -05:00
extra_caller = ( pool_debugging & POOL_DBG_CALLER ) ? POOL_EXTRA_CALLER : 0 ;
2022-02-23 04:03:11 -05:00
extra = extra_mark + extra_caller ;
2022-02-22 10:23:09 -05:00
if ( ! ( pool_debugging & POOL_DBG_NO_CACHE ) ) {
MEDIUM: pools: refine pool size rounding
The pools sizes were rounded up a little bit too much with commit
30f931ead ("BUG/MEDIUM: pools: fix the minimum allocation size"). The
goal was in fact to make sure they were always at least large enough to
store 2 list heads, and stuffing this into the alignment calculation
resulted in the size being always rounded up to this size. This is
problematic because it means that the appended tag at the end doesn't
always catch potential overflows since more bytes than needed are
allocated. Moreover, this test was later reinforced by commit b5ba09ed5
("BUG/MEDIUM: pools: ensure items are always large enough for the
pool_cache_item"), proving that the first test was not always sufficient.
This needs to be reworked to proceed correctly:
- the two lists are needed when the object is in the cache, hence
when we don't care about the tag, which means that the tag's size,
if any, can easily cover for the missing bytes to reach that size.
This is actually what was already being checked for.
- the rounding should not be performed (beyond the size of a word to
preserve pointer alignment) when pool tagging is enabled, otherwise
we don't detect small overflows. It means that there will be less
merging when proceeding like this. Tests show that we merge 93 pools
into 36 without tags and 43 with tags enabled.
- the rounding should not consider the extra size, since it's already
done when calculating the allocated size later (i.e. don't round up
twice). The difference is subtle but it's what makes sure the tag
immediately follows the area instead of starting from the end.
Thanks to this, now when writing one byte too many at the end of a struct
stream, the error is instantly caught.
2023-09-12 09:38:32 -04:00
/* we'll store two lists there, we need the room for this. Let's
* make sure it ' s always OK even when including the extra word
* that is stored after the pci struct .
2022-02-22 10:23:09 -05:00
*/
2022-02-23 04:03:11 -05:00
if ( size + extra - extra_caller < sizeof ( struct pool_cache_item ) )
size = sizeof ( struct pool_cache_item ) + extra_caller - extra ;
2022-02-22 10:23:09 -05:00
}
MEDIUM: pools: refine pool size rounding
The pools sizes were rounded up a little bit too much with commit
30f931ead ("BUG/MEDIUM: pools: fix the minimum allocation size"). The
goal was in fact to make sure they were always at least large enough to
store 2 list heads, and stuffing this into the alignment calculation
resulted in the size being always rounded up to this size. This is
problematic because it means that the appended tag at the end doesn't
always catch potential overflows since more bytes than needed are
allocated. Moreover, this test was later reinforced by commit b5ba09ed5
("BUG/MEDIUM: pools: ensure items are always large enough for the
pool_cache_item"), proving that the first test was not always sufficient.
This needs to be reworked to proceed correctly:
- the two lists are needed when the object is in the cache, hence
when we don't care about the tag, which means that the tag's size,
if any, can easily cover for the missing bytes to reach that size.
This is actually what was already being checked for.
- the rounding should not be performed (beyond the size of a word to
preserve pointer alignment) when pool tagging is enabled, otherwise
we don't detect small overflows. It means that there will be less
merging when proceeding like this. Tests show that we merge 93 pools
into 36 without tags and 43 with tags enabled.
- the rounding should not consider the extra size, since it's already
done when calculating the allocated size later (i.e. don't round up
twice). The difference is subtle but it's what makes sure the tag
immediately follows the area instead of starting from the end.
Thanks to this, now when writing one byte too many at the end of a struct
stream, the error is instantly caught.
2023-09-12 09:38:32 -04:00
/* Now we know our size is set to the strict minimum possible. It may
* be OK for elements allocated with an exact size ( e . g . buffers ) , but
* we ' re going to round the size up 16 bytes to merge almost identical
* pools together . We only round up however when we add the debugging
* tag since it ' s used to detect overflows . Otherwise we only round up
* to the size of a word to preserve alignment .
*/
if ( ! ( flags & MEM_F_EXACT ) ) {
align = ( pool_debugging & POOL_DBG_TAG ) ? sizeof ( void * ) : 16 ;
size = ( ( size + align - 1 ) & - align ) ;
}
2017-08-29 03:52:38 -04:00
/* TODO: thread: we do not lock pool list for now because all pools are
* created during HAProxy startup ( so before threads creation ) */
2007-05-13 18:16:13 -04:00
start = & pools ;
2007-05-13 12:26:08 -04:00
pool = NULL ;
2007-05-13 18:16:13 -04:00
list_for_each_entry ( entry , & pools , list ) {
if ( entry - > size = = size ) {
/* either we can share this place and we take it, or
2020-06-21 12:42:57 -04:00
* we look for a shareable one or for the next position
2007-05-13 18:16:13 -04:00
* before which we will insert a new one .
*/
2022-02-21 11:31:50 -05:00
if ( ( flags & entry - > flags & MEM_F_SHARED ) & &
( ! ( pool_debugging & POOL_DBG_DONT_MERGE ) | |
strcmp ( name , entry - > name ) = = 0 ) ) {
2007-05-13 18:16:13 -04:00
/* we can share this one */
2007-05-13 12:26:08 -04:00
pool = entry ;
[MEDIUM] Fix memory freeing at exit
New functions implemented:
- deinit_pollers: called at the end of deinit())
- prune_acl: called via list_for_each_entry_safe
Add missing pool_destroy2 calls:
- p->hdr_idx_pool
- pool2_tree64
Implement all task stopping:
- health-check: needs new "struct task" in the struct server
- queue processing: queue_mgt
- appsess_refresh: appsession_refresh
before (idle system):
==6079== LEAK SUMMARY:
==6079== definitely lost: 1,112 bytes in 75 blocks.
==6079== indirectly lost: 53,356 bytes in 2,090 blocks.
==6079== possibly lost: 52 bytes in 1 blocks.
==6079== still reachable: 150,996 bytes in 504 blocks.
==6079== suppressed: 0 bytes in 0 blocks.
after (idle system):
==6945== LEAK SUMMARY:
==6945== definitely lost: 7,644 bytes in 137 blocks.
==6945== indirectly lost: 9,913 bytes in 587 blocks.
==6945== possibly lost: 0 bytes in 0 blocks.
==6945== still reachable: 0 bytes in 0 blocks.
==6945== suppressed: 0 bytes in 0 blocks.
before (running system for ~2m):
==9343== LEAK SUMMARY:
==9343== definitely lost: 1,112 bytes in 75 blocks.
==9343== indirectly lost: 54,199 bytes in 2,122 blocks.
==9343== possibly lost: 52 bytes in 1 blocks.
==9343== still reachable: 151,128 bytes in 509 blocks.
==9343== suppressed: 0 bytes in 0 blocks.
after (running system for ~2m):
==11616== LEAK SUMMARY:
==11616== definitely lost: 7,644 bytes in 137 blocks.
==11616== indirectly lost: 9,981 bytes in 591 blocks.
==11616== possibly lost: 0 bytes in 0 blocks.
==11616== still reachable: 4 bytes in 1 blocks.
==11616== suppressed: 0 bytes in 0 blocks.
Still not perfect but significant improvement.
2008-05-29 17:53:44 -04:00
DPRINTF ( stderr , " Sharing %s with %s \n " , name , pool - > name ) ;
2007-05-13 12:26:08 -04:00
break ;
}
}
2007-05-13 18:16:13 -04:00
else if ( entry - > size > size ) {
/* insert before this one */
start = & entry - > list ;
break ;
}
2007-05-13 12:26:08 -04:00
}
if ( ! pool ) {
2022-03-02 11:59:04 -05:00
void * pool_addr ;
2018-10-16 01:58:39 -04:00
2022-03-02 11:59:04 -05:00
pool_addr = calloc ( 1 , sizeof ( * pool ) + __alignof__ ( * pool ) ) ;
if ( ! pool_addr )
2007-05-13 12:26:08 -04:00
return NULL ;
2022-03-02 11:59:04 -05:00
/* always provide an aligned pool */
pool = ( struct pool_head * ) ( ( ( ( size_t ) pool_addr ) + __alignof__ ( * pool ) ) & - ( size_t ) __alignof__ ( * pool ) ) ;
pool - > base_addr = pool_addr ; // keep it, it's the address to free later
2007-05-13 12:26:08 -04:00
if ( name )
strlcpy2 ( pool - > name , name , sizeof ( pool - > name ) ) ;
2022-02-23 04:03:11 -05:00
pool - > alloc_sz = size + extra ;
2007-05-13 12:26:08 -04:00
pool - > size = size ;
pool - > flags = flags ;
2021-04-21 01:32:39 -04:00
LIST_APPEND ( start , & pool - > list ) ;
2019-06-25 15:45:59 -04:00
2022-02-22 10:23:09 -05:00
if ( ! ( pool_debugging & POOL_DBG_NO_CACHE ) ) {
/* update per-thread pool cache if necessary */
for ( thr = 0 ; thr < MAX_THREADS ; thr + + ) {
LIST_INIT ( & pool - > cache [ thr ] . list ) ;
pool - > cache [ thr ] . tid = thr ;
pool - > cache [ thr ] . pool = pool ;
}
2019-06-25 15:45:59 -04:00
}
2020-02-01 11:45:32 -05:00
}
pool - > users + + ;
2007-05-13 12:26:08 -04:00
return pool ;
}
2021-04-17 10:57:25 -04:00
/* Tries to allocate an object for the pool <pool> using the system's allocator
2023-08-12 05:07:48 -04:00
* and directly returns it . The pool ' s allocated counter is checked but NOT
* updated , this is left to the caller , and but no other checks are performed .
2021-04-17 10:57:25 -04:00
*/
2023-08-12 05:07:48 -04:00
void * pool_get_from_os_noinc ( struct pool_head * pool )
2021-04-17 10:57:25 -04:00
{
2023-07-24 09:53:17 -04:00
if ( ! pool - > limit | | pool_allocated ( pool ) < pool - > limit ) {
2022-12-08 09:30:49 -05:00
void * ptr ;
MINOR: pools: make DEBUG_UAF a runtime setting
Since the massive pools cleanup that happened in 2.6, the pools
architecture was made quite more hierarchical and many alternate code
blocks could be moved to runtime flags set by -dM. One of them had not
been converted by then, DEBUG_UAF. It's not much more difficult actually,
since it only acts on a pair of functions indirection on the slow path
(OS-level allocator) and a default setting for the cache activation.
This patch adds the "uaf" setting to the options permitted in -dM so
that it now becomes possible to set or unset UAF at boot time without
recompiling. This is particularly convenient, because every 3 months on
average, developers ask a user to recompile haproxy with DEBUG_UAF to
understand a bug. Now it will not be needed anymore, instead the user
will only have to disable pools and enable uaf using -dMuaf. Note that
-dMuaf only disables previously enabled pools, but it remains possible
to re-enable caching by specifying the cache after, like -dMuaf,cache.
A few tests with this mode show that it can be an interesting combination
which catches significantly less UAF but will do so with much less
overhead, so it might be compatible with some high-traffic deployments.
The change is very small and isolated. It could be helpful to backport
this at least to 2.7 once confirmed not to cause build issues on exotic
systems, and even to 2.6 a bit later as this has proven to be useful
over time, and could be even more if it did not require a rebuild. If
a backport is desired, the following patches are needed as well:
CLEANUP: pools: move the write before free to the uaf-only function
CLEANUP: pool: only include pool-os from pool.c not pool.h
REORG: pool: move all the OS specific code to pool-os.h
CLEANUP: pools: get rid of CONFIG_HAP_POOLS
DEBUG: pool: show a few examples in -dMhelp
2022-12-08 11:47:59 -05:00
if ( pool_debugging & POOL_DBG_UAF )
ptr = pool_alloc_area_uaf ( pool - > alloc_sz ) ;
else
ptr = pool_alloc_area ( pool - > alloc_sz ) ;
2023-08-12 05:07:48 -04:00
if ( ptr )
2021-04-17 10:57:25 -04:00
return ptr ;
2023-07-24 10:38:09 -04:00
_HA_ATOMIC_INC ( & pool - > buckets [ pool_tbucket ( ) ] . failed ) ;
2021-04-17 10:57:25 -04:00
}
activity [ tid ] . pool_fail + + ;
return NULL ;
}
2023-08-12 05:07:48 -04:00
/* Releases a pool item back to the operating system but DOES NOT update
* the allocation counter , it ' s left to the caller to do it . It may be
* done before or after , it doesn ' t matter , the function does not use it .
2021-04-17 11:48:40 -04:00
*/
2023-08-12 05:07:48 -04:00
void pool_put_to_os_nodec ( struct pool_head * pool , void * ptr )
2021-04-17 11:48:40 -04:00
{
MINOR: pools: make DEBUG_UAF a runtime setting
Since the massive pools cleanup that happened in 2.6, the pools
architecture was made quite more hierarchical and many alternate code
blocks could be moved to runtime flags set by -dM. One of them had not
been converted by then, DEBUG_UAF. It's not much more difficult actually,
since it only acts on a pair of functions indirection on the slow path
(OS-level allocator) and a default setting for the cache activation.
This patch adds the "uaf" setting to the options permitted in -dM so
that it now becomes possible to set or unset UAF at boot time without
recompiling. This is particularly convenient, because every 3 months on
average, developers ask a user to recompile haproxy with DEBUG_UAF to
understand a bug. Now it will not be needed anymore, instead the user
will only have to disable pools and enable uaf using -dMuaf. Note that
-dMuaf only disables previously enabled pools, but it remains possible
to re-enable caching by specifying the cache after, like -dMuaf,cache.
A few tests with this mode show that it can be an interesting combination
which catches significantly less UAF but will do so with much less
overhead, so it might be compatible with some high-traffic deployments.
The change is very small and isolated. It could be helpful to backport
this at least to 2.7 once confirmed not to cause build issues on exotic
systems, and even to 2.6 a bit later as this has proven to be useful
over time, and could be even more if it did not require a rebuild. If
a backport is desired, the following patches are needed as well:
CLEANUP: pools: move the write before free to the uaf-only function
CLEANUP: pool: only include pool-os from pool.c not pool.h
REORG: pool: move all the OS specific code to pool-os.h
CLEANUP: pools: get rid of CONFIG_HAP_POOLS
DEBUG: pool: show a few examples in -dMhelp
2022-12-08 11:47:59 -05:00
if ( pool_debugging & POOL_DBG_UAF )
pool_free_area_uaf ( ptr , pool - > alloc_sz ) ;
else
pool_free_area ( ptr , pool - > alloc_sz ) ;
2021-04-17 11:48:40 -04:00
}
2021-04-15 12:20:12 -04:00
/* Tries to allocate an object for the pool <pool> using the system's allocator
* and directly returns it . The pool ' s counters are updated but the object is
* never cached , so this is usable with and without local or shared caches .
*/
2023-09-11 09:01:55 -04:00
void * pool_alloc_nocache ( struct pool_head * pool , const void * caller )
MEDIUM: pools: add CONFIG_HAP_NO_GLOBAL_POOLS and CONFIG_HAP_GLOBAL_POOLS
We've reached a point where the global pools represent a significant
bottleneck with threads. On a 64-core machine, the performance was
divided by 8 between 32 and 64 H2 connections only because there were
not enough entries in the local caches to avoid picking from the global
pools, and the contention on the list there was very high. It becomes
obvious that we need to have an array of lists, but that will require
more changes.
In parallel, standard memory allocators have improved, with tcmalloc
and jemalloc finding their ways through mainstream systems, and glibc
having upgraded to a thread-aware ptmalloc variant, keeping this level
of contention here isn't justified anymore when we have both the local
per-thread pool caches and a fast process-wide allocator.
For these reasons, this patch introduces a new compile time setting
CONFIG_HAP_NO_GLOBAL_POOLS which is set by default when threads are
enabled with thread local pool caches, and we know we have a fast
thread-aware memory allocator (currently set for glibc>=2.26). In this
case we entirely bypass the global pool and directly use the standard
memory allocator when missing objects from the local pools. It is also
possible to force it at compile time when a good allocator is used with
another setup.
It is still possible to re-enable the global pools using
CONFIG_HAP_GLOBAL_POOLS, if a corner case is discovered regarding the
operating system's default allocator, or when building with a recent
libc but a different allocator which provides other benefits but does
not scale well with threads.
2021-03-02 14:05:09 -05:00
{
void * ptr = NULL ;
2023-07-24 09:53:17 -04:00
uint bucket ;
MEDIUM: pools: add CONFIG_HAP_NO_GLOBAL_POOLS and CONFIG_HAP_GLOBAL_POOLS
We've reached a point where the global pools represent a significant
bottleneck with threads. On a 64-core machine, the performance was
divided by 8 between 32 and 64 H2 connections only because there were
not enough entries in the local caches to avoid picking from the global
pools, and the contention on the list there was very high. It becomes
obvious that we need to have an array of lists, but that will require
more changes.
In parallel, standard memory allocators have improved, with tcmalloc
and jemalloc finding their ways through mainstream systems, and glibc
having upgraded to a thread-aware ptmalloc variant, keeping this level
of contention here isn't justified anymore when we have both the local
per-thread pool caches and a fast process-wide allocator.
For these reasons, this patch introduces a new compile time setting
CONFIG_HAP_NO_GLOBAL_POOLS which is set by default when threads are
enabled with thread local pool caches, and we know we have a fast
thread-aware memory allocator (currently set for glibc>=2.26). In this
case we entirely bypass the global pool and directly use the standard
memory allocator when missing objects from the local pools. It is also
possible to force it at compile time when a good allocator is used with
another setup.
It is still possible to re-enable the global pools using
CONFIG_HAP_GLOBAL_POOLS, if a corner case is discovered regarding the
operating system's default allocator, or when building with a recent
libc but a different allocator which provides other benefits but does
not scale well with threads.
2021-03-02 14:05:09 -05:00
2023-08-12 05:07:48 -04:00
ptr = pool_get_from_os_noinc ( pool ) ;
2021-04-17 10:57:25 -04:00
if ( ! ptr )
MEDIUM: pools: add CONFIG_HAP_NO_GLOBAL_POOLS and CONFIG_HAP_GLOBAL_POOLS
We've reached a point where the global pools represent a significant
bottleneck with threads. On a 64-core machine, the performance was
divided by 8 between 32 and 64 H2 connections only because there were
not enough entries in the local caches to avoid picking from the global
pools, and the contention on the list there was very high. It becomes
obvious that we need to have an array of lists, but that will require
more changes.
In parallel, standard memory allocators have improved, with tcmalloc
and jemalloc finding their ways through mainstream systems, and glibc
having upgraded to a thread-aware ptmalloc variant, keeping this level
of contention here isn't justified anymore when we have both the local
per-thread pool caches and a fast process-wide allocator.
For these reasons, this patch introduces a new compile time setting
CONFIG_HAP_NO_GLOBAL_POOLS which is set by default when threads are
enabled with thread local pool caches, and we know we have a fast
thread-aware memory allocator (currently set for glibc>=2.26). In this
case we entirely bypass the global pool and directly use the standard
memory allocator when missing objects from the local pools. It is also
possible to force it at compile time when a good allocator is used with
another setup.
It is still possible to re-enable the global pools using
CONFIG_HAP_GLOBAL_POOLS, if a corner case is discovered regarding the
operating system's default allocator, or when building with a recent
libc but a different allocator which provides other benefits but does
not scale well with threads.
2021-03-02 14:05:09 -05:00
return NULL ;
2023-07-24 09:53:17 -04:00
bucket = pool_pbucket ( ptr ) ;
2023-07-24 10:18:25 -04:00
swrate_add_scaled_opportunistic ( & pool - > buckets [ bucket ] . needed_avg , POOL_AVG_SAMPLES , pool - > buckets [ bucket ] . used , POOL_AVG_SAMPLES / 4 ) ;
2023-07-24 09:53:17 -04:00
_HA_ATOMIC_INC ( & pool - > buckets [ bucket ] . allocated ) ;
2023-07-24 10:12:18 -04:00
_HA_ATOMIC_INC ( & pool - > buckets [ bucket ] . used ) ;
MEDIUM: pools: add CONFIG_HAP_NO_GLOBAL_POOLS and CONFIG_HAP_GLOBAL_POOLS
We've reached a point where the global pools represent a significant
bottleneck with threads. On a 64-core machine, the performance was
divided by 8 between 32 and 64 H2 connections only because there were
not enough entries in the local caches to avoid picking from the global
pools, and the contention on the list there was very high. It becomes
obvious that we need to have an array of lists, but that will require
more changes.
In parallel, standard memory allocators have improved, with tcmalloc
and jemalloc finding their ways through mainstream systems, and glibc
having upgraded to a thread-aware ptmalloc variant, keeping this level
of contention here isn't justified anymore when we have both the local
per-thread pool caches and a fast process-wide allocator.
For these reasons, this patch introduces a new compile time setting
CONFIG_HAP_NO_GLOBAL_POOLS which is set by default when threads are
enabled with thread local pool caches, and we know we have a fast
thread-aware memory allocator (currently set for glibc>=2.26). In this
case we entirely bypass the global pool and directly use the standard
memory allocator when missing objects from the local pools. It is also
possible to force it at compile time when a good allocator is used with
another setup.
It is still possible to re-enable the global pools using
CONFIG_HAP_GLOBAL_POOLS, if a corner case is discovered regarding the
operating system's default allocator, or when building with a recent
libc but a different allocator which provides other benefits but does
not scale well with threads.
2021-03-02 14:05:09 -05:00
/* keep track of where the element was allocated from */
2022-01-01 11:10:50 -05:00
POOL_DEBUG_SET_MARK ( pool , ptr ) ;
2023-09-11 09:01:55 -04:00
POOL_DEBUG_TRACE_CALLER ( pool , ( struct pool_cache_item * ) ptr , caller ) ;
MEDIUM: pools: add CONFIG_HAP_NO_GLOBAL_POOLS and CONFIG_HAP_GLOBAL_POOLS
We've reached a point where the global pools represent a significant
bottleneck with threads. On a 64-core machine, the performance was
divided by 8 between 32 and 64 H2 connections only because there were
not enough entries in the local caches to avoid picking from the global
pools, and the contention on the list there was very high. It becomes
obvious that we need to have an array of lists, but that will require
more changes.
In parallel, standard memory allocators have improved, with tcmalloc
and jemalloc finding their ways through mainstream systems, and glibc
having upgraded to a thread-aware ptmalloc variant, keeping this level
of contention here isn't justified anymore when we have both the local
per-thread pool caches and a fast process-wide allocator.
For these reasons, this patch introduces a new compile time setting
CONFIG_HAP_NO_GLOBAL_POOLS which is set by default when threads are
enabled with thread local pool caches, and we know we have a fast
thread-aware memory allocator (currently set for glibc>=2.26). In this
case we entirely bypass the global pool and directly use the standard
memory allocator when missing objects from the local pools. It is also
possible to force it at compile time when a good allocator is used with
another setup.
It is still possible to re-enable the global pools using
CONFIG_HAP_GLOBAL_POOLS, if a corner case is discovered regarding the
operating system's default allocator, or when building with a recent
libc but a different allocator which provides other benefits but does
not scale well with threads.
2021-03-02 14:05:09 -05:00
return ptr ;
}
2021-04-17 11:48:40 -04:00
/* Release a pool item back to the OS and keeps the pool's counters up to date.
* This is always defined even when pools are not enabled ( their usage stats
* are maintained ) .
*/
void pool_free_nocache ( struct pool_head * pool , void * ptr )
{
2023-07-24 09:53:17 -04:00
uint bucket = pool_pbucket ( ptr ) ;
2023-07-24 10:12:18 -04:00
_HA_ATOMIC_DEC ( & pool - > buckets [ bucket ] . used ) ;
2023-07-24 09:53:17 -04:00
_HA_ATOMIC_DEC ( & pool - > buckets [ bucket ] . allocated ) ;
2023-07-24 10:18:25 -04:00
swrate_add_opportunistic ( & pool - > buckets [ bucket ] . needed_avg , POOL_AVG_SAMPLES , pool - > buckets [ bucket ] . used ) ;
2023-07-24 09:53:17 -04:00
2023-08-12 05:07:48 -04:00
pool_put_to_os_nodec ( pool , ptr ) ;
2021-04-17 11:48:40 -04:00
}
MEDIUM: pools: add CONFIG_HAP_NO_GLOBAL_POOLS and CONFIG_HAP_GLOBAL_POOLS
We've reached a point where the global pools represent a significant
bottleneck with threads. On a 64-core machine, the performance was
divided by 8 between 32 and 64 H2 connections only because there were
not enough entries in the local caches to avoid picking from the global
pools, and the contention on the list there was very high. It becomes
obvious that we need to have an array of lists, but that will require
more changes.
In parallel, standard memory allocators have improved, with tcmalloc
and jemalloc finding their ways through mainstream systems, and glibc
having upgraded to a thread-aware ptmalloc variant, keeping this level
of contention here isn't justified anymore when we have both the local
per-thread pool caches and a fast process-wide allocator.
For these reasons, this patch introduces a new compile time setting
CONFIG_HAP_NO_GLOBAL_POOLS which is set by default when threads are
enabled with thread local pool caches, and we know we have a fast
thread-aware memory allocator (currently set for glibc>=2.26). In this
case we entirely bypass the global pool and directly use the standard
memory allocator when missing objects from the local pools. It is also
possible to force it at compile time when a good allocator is used with
another setup.
It is still possible to re-enable the global pools using
CONFIG_HAP_GLOBAL_POOLS, if a corner case is discovered regarding the
operating system's default allocator, or when building with a recent
libc but a different allocator which provides other benefits but does
not scale well with threads.
2021-03-02 14:05:09 -05:00
2022-02-21 12:42:53 -05:00
/* Updates <pch>'s fill_pattern and fills the free area after <item> with it,
* up to < size > bytes . The item part is left untouched .
*/
void pool_fill_pattern ( struct pool_cache_head * pch , struct pool_cache_item * item , uint size )
{
ulong * ptr = ( ulong * ) item ;
uint ofs ;
ulong u ;
if ( size < = sizeof ( * item ) )
return ;
/* Upgrade the fill_pattern to change about half of the bits
* ( to be sure to catch static flag corruption ) , and apply it .
*/
u = pch - > fill_pattern + = ~ 0UL / 3 ; // 0x55...55
ofs = sizeof ( * item ) / sizeof ( * ptr ) ;
while ( ofs < size / sizeof ( * ptr ) )
ptr [ ofs + + ] = u ;
}
/* check for a pool_cache_item integrity after extracting it from the cache. It
* must have been previously initialized using pool_fill_pattern ( ) . If any
* corruption is detected , the function provokes an immediate crash .
*/
2023-09-11 05:26:12 -04:00
void pool_check_pattern ( struct pool_cache_head * pch , struct pool_head * pool , struct pool_cache_item * item , const void * caller )
2022-02-21 12:42:53 -05:00
{
const ulong * ptr = ( const ulong * ) item ;
2023-09-11 05:26:12 -04:00
uint size = pool - > size ;
2022-02-21 12:42:53 -05:00
uint ofs ;
ulong u ;
if ( size < = sizeof ( * item ) )
return ;
/* let's check that all words past *item are equal */
ofs = sizeof ( * item ) / sizeof ( * ptr ) ;
u = ptr [ ofs + + ] ;
while ( ofs < size / sizeof ( * ptr ) ) {
DEBUG: pools: inspect pools on fatal error and dump information found
It's a bit frustrating sometimes to see pool checks catch a bug but not
provide exploitable information without a core.
Here we're adding a function "pool_inspect_item()" which is called just
before aborting in pool_check_pattern() and POOL_DEBUG_CHECK_MARK() and
which will display the error type, the pool's pointer and name, and will
try to check if the item's tag matches the pool, and if not, will iterate
over all pools to see if one would be a better candidate, then will try
to figure the last known caller and possibly other likely candidates if
the pool's tag is not sufficiently trusted. This typically helps better
diagnose corruption in use-after-free scenarios, or freeing to a pool
that differs from the one the object was allocated from, and will also
indicate calling points that may help figure where an object was last
released or allocated. The info is printed on stderr just before the
backtrace.
For example, the recent off-by-one test in the PPv2 changes would have
produced the following output in vtest logs:
*** h1 debug|FATAL: pool inconsistency detected in thread 1: tag mismatch on free().
*** h1 debug| caller: 0x62bb87 (conn_free+0x147/0x3c5)
*** h1 debug| pool: 0x2211ec0 ('pp_tlv_256', size 304, real 320, users 1)
*** h1 debug|Tag does not match. Possible origin pool(s):
*** h1 debug| tag: @0x2565530 = 0x2216740 (pp_tlv_128, size 176, real 192, users 1)
*** h1 debug|Recorded caller if pool 'pp_tlv_128':
*** h1 debug| @0x2565538 (+0184) = 0x62c76d (conn_recv_proxy+0x4cd/0xa24)
A mismatch in the allocated/released pool is already visible, and the
callers confirm it once resolved, where the allocator indeed allocates
from pp_tlv_128 and conn_free() releases to pp_tlv_256:
$ addr2line -spafe ./haproxy <<< $'0x62bb87\n0x62c76d'
0x000000000062bb87: conn_free at connection.c:568
0x000000000062c76d: conn_recv_proxy at connection.c:1177
2023-09-11 08:05:32 -04:00
if ( unlikely ( ptr [ ofs ] ! = u ) ) {
pool_inspect_item ( " cache corruption detected " , pool , item , caller ) ;
2022-02-21 12:42:53 -05:00
ABORT_NOW ( ) ;
DEBUG: pools: inspect pools on fatal error and dump information found
It's a bit frustrating sometimes to see pool checks catch a bug but not
provide exploitable information without a core.
Here we're adding a function "pool_inspect_item()" which is called just
before aborting in pool_check_pattern() and POOL_DEBUG_CHECK_MARK() and
which will display the error type, the pool's pointer and name, and will
try to check if the item's tag matches the pool, and if not, will iterate
over all pools to see if one would be a better candidate, then will try
to figure the last known caller and possibly other likely candidates if
the pool's tag is not sufficiently trusted. This typically helps better
diagnose corruption in use-after-free scenarios, or freeing to a pool
that differs from the one the object was allocated from, and will also
indicate calling points that may help figure where an object was last
released or allocated. The info is printed on stderr just before the
backtrace.
For example, the recent off-by-one test in the PPv2 changes would have
produced the following output in vtest logs:
*** h1 debug|FATAL: pool inconsistency detected in thread 1: tag mismatch on free().
*** h1 debug| caller: 0x62bb87 (conn_free+0x147/0x3c5)
*** h1 debug| pool: 0x2211ec0 ('pp_tlv_256', size 304, real 320, users 1)
*** h1 debug|Tag does not match. Possible origin pool(s):
*** h1 debug| tag: @0x2565530 = 0x2216740 (pp_tlv_128, size 176, real 192, users 1)
*** h1 debug|Recorded caller if pool 'pp_tlv_128':
*** h1 debug| @0x2565538 (+0184) = 0x62c76d (conn_recv_proxy+0x4cd/0xa24)
A mismatch in the allocated/released pool is already visible, and the
callers confirm it once resolved, where the allocator indeed allocates
from pp_tlv_128 and conn_free() releases to pp_tlv_256:
$ addr2line -spafe ./haproxy <<< $'0x62bb87\n0x62c76d'
0x000000000062bb87: conn_free at connection.c:568
0x000000000062c76d: conn_recv_proxy at connection.c:1177
2023-09-11 08:05:32 -04:00
}
2022-02-21 12:42:53 -05:00
ofs + + ;
}
}
2022-01-02 11:19:14 -05:00
/* removes up to <count> items from the end of the local pool cache <ph> for
* pool < pool > . The shared pool is refilled with these objects in the limit
* of the number of acceptable objects , and the rest will be released to the
* OS . It is not a problem is < count > is larger than the number of objects in
2022-02-22 10:23:09 -05:00
* the local cache . The counters are automatically updated . Must not be used
* with pools disabled .
2021-04-19 02:14:03 -04:00
*/
2022-01-02 11:19:14 -05:00
static void pool_evict_last_items ( struct pool_head * pool , struct pool_cache_head * ph , uint count )
2021-04-19 02:14:03 -04:00
{
struct pool_cache_item * item ;
2022-01-02 11:53:02 -05:00
struct pool_item * pi , * head = NULL ;
2023-09-11 05:14:43 -04:00
void * caller = __builtin_return_address ( 0 ) ;
2022-01-02 11:19:14 -05:00
uint released = 0 ;
2022-01-02 11:53:02 -05:00
uint cluster = 0 ;
2022-01-01 18:27:06 -05:00
uint to_free_max ;
2023-08-12 06:34:09 -04:00
uint bucket ;
2022-01-01 18:27:06 -05:00
2022-02-22 10:23:09 -05:00
BUG_ON ( pool_debugging & POOL_DBG_NO_CACHE ) ;
2022-02-22 03:21:13 -05:00
/* Note: this will be zero when global pools are disabled */
2022-01-01 18:27:06 -05:00
to_free_max = pool_releasable ( pool ) ;
2021-04-19 02:14:03 -04:00
2022-01-02 11:19:14 -05:00
while ( released < count & & ! LIST_ISEMPTY ( & ph - > list ) ) {
2022-01-02 06:40:14 -05:00
item = LIST_PREV ( & ph - > list , typeof ( item ) , by_pool ) ;
2022-02-09 10:23:55 -05:00
BUG_ON ( & item - > by_pool = = & ph - > list ) ;
2022-02-21 12:42:53 -05:00
if ( unlikely ( pool_debugging & POOL_DBG_INTEGRITY ) )
2023-09-11 05:26:12 -04:00
pool_check_pattern ( ph , pool , item , caller ) ;
2021-04-21 01:32:39 -04:00
LIST_DELETE ( & item - > by_pool ) ;
LIST_DELETE ( & item - > by_lru ) ;
2021-12-30 11:37:33 -05:00
2023-08-12 06:34:09 -04:00
bucket = pool_pbucket ( item ) ;
_HA_ATOMIC_DEC ( & pool - > buckets [ bucket ] . used ) ;
swrate_add_opportunistic ( & pool - > buckets [ bucket ] . needed_avg , POOL_AVG_SAMPLES , pool - > buckets [ bucket ] . used ) ;
2022-01-02 11:53:02 -05:00
if ( to_free_max > released | | cluster ) {
2022-02-22 03:21:13 -05:00
/* will never match when global pools are disabled */
2022-01-01 18:27:06 -05:00
pi = ( struct pool_item * ) item ;
2022-01-02 11:53:02 -05:00
pi - > next = NULL ;
pi - > down = head ;
head = pi ;
cluster + + ;
if ( cluster > = CONFIG_HAP_POOL_CLUSTER_SIZE ) {
/* enough to make a cluster */
2023-07-24 10:12:18 -04:00
pool_put_to_shared_cache ( pool , head ) ;
2022-01-02 11:53:02 -05:00
cluster = 0 ;
head = NULL ;
}
2023-08-12 06:34:09 -04:00
} else {
/* does pool_free_nocache() with a known bucket */
_HA_ATOMIC_DEC ( & pool - > buckets [ bucket ] . allocated ) ;
pool_put_to_os_nodec ( pool , item ) ;
}
2022-01-02 11:53:02 -05:00
released + + ;
2022-01-01 18:27:06 -05:00
}
2022-01-02 11:53:02 -05:00
/* incomplete cluster left */
if ( cluster )
2023-07-24 10:12:18 -04:00
pool_put_to_shared_cache ( pool , head ) ;
2022-01-02 11:53:02 -05:00
2022-01-02 11:19:14 -05:00
ph - > count - = released ;
pool_cache_count - = released ;
pool_cache_bytes - = released * pool - > size ;
}
/* Evicts some of the oldest objects from one local cache, until its number of
* objects is no more than 16 + 1 / 8 of the total number of locally cached objects
* or the total size of the local cache is no more than 75 % of its maximum ( i . e .
* we don ' t want a single cache to use all the cache for itself ) . For this , the
2022-02-09 10:19:24 -05:00
* list is scanned in reverse . If < full > is non - null , all objects are evicted .
2022-02-22 10:23:09 -05:00
* Must not be used when pools are disabled .
2022-01-02 11:19:14 -05:00
*/
2022-02-09 10:19:24 -05:00
void pool_evict_from_local_cache ( struct pool_head * pool , int full )
2022-01-02 11:19:14 -05:00
{
struct pool_cache_head * ph = & pool - > cache [ tid ] ;
2022-02-22 10:23:09 -05:00
BUG_ON ( pool_debugging & POOL_DBG_NO_CACHE ) ;
2022-02-09 10:19:24 -05:00
while ( ( ph - > count & & full ) | |
( ph - > count > = CONFIG_HAP_POOL_CLUSTER_SIZE & &
ph - > count > = 16 + pool_cache_count / 8 & &
2022-12-19 02:15:57 -05:00
pool_cache_bytes > global . tune . pool_cache_size * 3 / 4 ) ) {
2022-01-02 11:24:55 -05:00
pool_evict_last_items ( pool , ph , CONFIG_HAP_POOL_CLUSTER_SIZE ) ;
2021-04-19 02:14:03 -04:00
}
}
2021-04-18 04:23:02 -04:00
/* Evicts some of the oldest objects from the local cache, pushing them to the
2022-02-22 10:23:09 -05:00
* global pool . Must not be used when pools are disabled .
2021-04-18 04:23:02 -04:00
*/
void pool_evict_from_local_caches ( )
{
struct pool_cache_item * item ;
struct pool_cache_head * ph ;
struct pool_head * pool ;
2022-02-22 10:23:09 -05:00
BUG_ON ( pool_debugging & POOL_DBG_NO_CACHE ) ;
2021-04-18 04:23:02 -04:00
do {
2021-09-30 13:02:18 -04:00
item = LIST_PREV ( & th_ctx - > pool_lru_head , struct pool_cache_item * , by_lru ) ;
2022-02-09 10:23:55 -05:00
BUG_ON ( & item - > by_lru = = & th_ctx - > pool_lru_head ) ;
2021-04-18 04:23:02 -04:00
/* note: by definition we remove oldest objects so they also are the
* oldest in their own pools , thus their next is the pool ' s head .
*/
ph = LIST_NEXT ( & item - > by_pool , struct pool_cache_head * , list ) ;
2022-02-09 10:33:22 -05:00
BUG_ON ( ph - > tid ! = tid ) ;
2021-04-18 04:23:02 -04:00
pool = container_of ( ph - tid , struct pool_head , cache ) ;
2022-02-09 10:33:22 -05:00
BUG_ON ( pool ! = ph - > pool ) ;
2022-01-02 11:24:55 -05:00
pool_evict_last_items ( pool , ph , CONFIG_HAP_POOL_CLUSTER_SIZE ) ;
2022-12-19 02:15:57 -05:00
} while ( pool_cache_bytes > global . tune . pool_cache_size * 7 / 8 ) ;
2021-04-18 04:23:02 -04:00
}
2021-04-19 05:49:26 -04:00
/* Frees an object to the local cache, possibly pushing oldest objects to the
* shared cache , which itself may decide to release some of them to the OS .
* While it is unspecified what the object becomes past this point , it is
2023-11-21 13:54:16 -05:00
* guaranteed to be released from the users ' perspective . A caller address may
2022-02-22 10:23:09 -05:00
* be passed and stored into the area when DEBUG_POOL_TRACING is set . Must not
* be used with pools disabled .
2021-04-19 05:49:26 -04:00
*/
2022-01-24 09:51:50 -05:00
void pool_put_to_cache ( struct pool_head * pool , void * ptr , const void * caller )
2021-04-19 05:49:26 -04:00
{
struct pool_cache_item * item = ( struct pool_cache_item * ) ptr ;
struct pool_cache_head * ph = & pool - > cache [ tid ] ;
2022-02-22 10:23:09 -05:00
BUG_ON ( pool_debugging & POOL_DBG_NO_CACHE ) ;
2021-04-21 01:32:39 -04:00
LIST_INSERT ( & ph - > list , & item - > by_pool ) ;
2021-09-30 13:02:18 -04:00
LIST_INSERT ( & th_ctx - > pool_lru_head , & item - > by_lru ) ;
2022-01-24 09:52:51 -05:00
POOL_DEBUG_TRACE_CALLER ( pool , item , caller ) ;
2021-04-19 05:49:26 -04:00
ph - > count + + ;
2022-02-21 12:42:53 -05:00
if ( unlikely ( pool_debugging & POOL_DBG_INTEGRITY ) )
pool_fill_pattern ( ph , item , pool - > size ) ;
2021-04-19 05:49:26 -04:00
pool_cache_count + + ;
pool_cache_bytes + = pool - > size ;
2022-12-19 02:15:57 -05:00
if ( unlikely ( pool_cache_bytes > global . tune . pool_cache_size * 3 / 4 ) ) {
2022-01-02 11:24:55 -05:00
if ( ph - > count > = 16 + pool_cache_count / 8 + CONFIG_HAP_POOL_CLUSTER_SIZE )
2022-02-09 10:19:24 -05:00
pool_evict_from_local_cache ( pool , 0 ) ;
2022-12-19 02:15:57 -05:00
if ( pool_cache_bytes > global . tune . pool_cache_size )
2021-04-19 05:49:26 -04:00
pool_evict_from_local_caches ( ) ;
}
}
2021-12-30 11:09:31 -05:00
/* Tries to refill the local cache <pch> from the shared one for pool <pool>.
* This is only used when pools are in use and shared pools are enabled . No
* malloc ( ) is attempted , and poisonning is never performed . The purpose is to
* get the fastest possible refilling so that the caller can easily check if
2022-02-22 10:23:09 -05:00
* the cache has enough objects for its use . Must not be used when pools are
* disabled .
2021-12-30 11:09:31 -05:00
*/
void pool_refill_local_from_shared ( struct pool_head * pool , struct pool_cache_head * pch )
{
struct pool_cache_item * item ;
2022-01-02 08:35:57 -05:00
struct pool_item * ret , * down ;
2023-07-24 11:02:22 -04:00
uint bucket ;
2022-01-02 08:35:57 -05:00
uint count ;
2021-12-30 11:09:31 -05:00
2022-02-22 10:23:09 -05:00
BUG_ON ( pool_debugging & POOL_DBG_NO_CACHE ) ;
2021-12-30 11:09:31 -05:00
/* we'll need to reference the first element to figure the next one. We
* must temporarily lock it so that nobody allocates then releases it ,
2023-07-24 11:02:22 -04:00
* or the dereference could fail . In order to limit the locking ,
* threads start from a bucket that depends on their ID .
2021-12-30 11:09:31 -05:00
*/
2023-07-24 11:02:22 -04:00
bucket = pool_tbucket ( ) ;
ret = _HA_ATOMIC_LOAD ( & pool - > buckets [ bucket ] . free_list ) ;
2023-11-08 10:44:20 -05:00
count = 0 ;
2021-12-30 11:09:31 -05:00
do {
2023-11-08 10:44:20 -05:00
/* look for an apparently non-busy entry. If we hit a busy pool
* we retry with another random bucket . And if we encounter a
* NULL , we retry once with another random bucket . This is in
* order to prevent object accumulation in other buckets .
*/
while ( unlikely ( ret = = POOL_BUSY | | ( ret = = NULL & & count + + < 1 ) ) ) {
2023-11-08 10:44:20 -05:00
bucket = statistical_prng ( ) % CONFIG_HAP_POOL_BUCKETS ;
2023-07-24 11:02:22 -04:00
ret = _HA_ATOMIC_LOAD ( & pool - > buckets [ bucket ] . free_list ) ;
2021-12-30 11:09:31 -05:00
}
if ( ret = = NULL )
return ;
2023-07-24 11:02:22 -04:00
} while ( unlikely ( ( ret = _HA_ATOMIC_XCHG ( & pool - > buckets [ bucket ] . free_list , POOL_BUSY ) ) = = POOL_BUSY ) ) ;
2021-12-30 11:09:31 -05:00
if ( unlikely ( ret = = NULL ) ) {
2023-07-24 11:02:22 -04:00
HA_ATOMIC_STORE ( & pool - > buckets [ bucket ] . free_list , NULL ) ;
2021-12-30 11:09:31 -05:00
return ;
}
/* this releases the lock */
2023-07-24 11:02:22 -04:00
HA_ATOMIC_STORE ( & pool - > buckets [ bucket ] . free_list , ret - > next ) ;
2021-12-30 11:09:31 -05:00
2023-07-24 11:02:22 -04:00
/* now store the retrieved object(s) into the local cache. Note that
* they don ' t all have the same hash and that it doesn ' t necessarily
* match the one from the pool .
*/
2022-01-02 08:35:57 -05:00
count = 0 ;
for ( ; ret ; ret = down ) {
down = ret - > down ;
item = ( struct pool_cache_item * ) ret ;
2022-01-25 09:56:50 -05:00
POOL_DEBUG_TRACE_CALLER ( pool , item , NULL ) ;
2022-01-02 08:35:57 -05:00
LIST_INSERT ( & pch - > list , & item - > by_pool ) ;
LIST_INSERT ( & th_ctx - > pool_lru_head , & item - > by_lru ) ;
2023-07-24 10:12:18 -04:00
_HA_ATOMIC_INC ( & pool - > buckets [ pool_pbucket ( item ) ] . used ) ;
2022-01-02 08:35:57 -05:00
count + + ;
2022-02-21 12:42:53 -05:00
if ( unlikely ( pool_debugging & POOL_DBG_INTEGRITY ) )
pool_fill_pattern ( pch , item , pool - > size ) ;
2023-07-24 10:12:18 -04:00
2022-01-02 08:35:57 -05:00
}
pch - > count + = count ;
pool_cache_count + = count ;
pool_cache_bytes + = count * pool - > size ;
2021-12-30 11:09:31 -05:00
}
2022-01-02 09:15:54 -05:00
/* Adds pool item cluster <item> to the shared cache, which contains <count>
* elements . The caller is advised to first check using pool_releasable ( ) if
* it ' s wise to add this series of objects there . Both the pool and the item ' s
* head must be valid .
2021-12-30 11:37:33 -05:00
*/
2023-07-24 10:12:18 -04:00
void pool_put_to_shared_cache ( struct pool_head * pool , struct pool_item * item )
2021-12-30 11:37:33 -05:00
{
2022-01-01 12:22:20 -05:00
struct pool_item * free_list ;
2023-07-24 11:02:22 -04:00
uint bucket = pool_pbucket ( item ) ;
2021-12-30 11:37:33 -05:00
2023-07-24 11:02:22 -04:00
/* we prefer to put the item into the entry that corresponds to its own
* hash so that on return it remains in the right place , but that ' s not
* mandatory .
*/
free_list = _HA_ATOMIC_LOAD ( & pool - > buckets [ bucket ] . free_list ) ;
2021-12-30 11:37:33 -05:00
do {
2023-07-24 11:02:22 -04:00
/* look for an apparently non-busy entry */
2021-12-30 11:37:33 -05:00
while ( unlikely ( free_list = = POOL_BUSY ) ) {
2023-07-24 11:02:22 -04:00
bucket = ( bucket + 1 ) % CONFIG_HAP_POOL_BUCKETS ;
free_list = _HA_ATOMIC_LOAD ( & pool - > buckets [ bucket ] . free_list ) ;
2021-12-30 11:37:33 -05:00
}
2022-01-01 12:22:20 -05:00
_HA_ATOMIC_STORE ( & item - > next , free_list ) ;
2021-12-30 11:37:33 -05:00
__ha_barrier_atomic_store ( ) ;
2023-07-24 11:02:22 -04:00
} while ( ! _HA_ATOMIC_CAS ( & pool - > buckets [ bucket ] . free_list , & free_list , item ) ) ;
2021-12-30 11:37:33 -05:00
__ha_barrier_atomic_store ( ) ;
}
2018-01-24 12:38:31 -05:00
/*
* This function frees whatever can be freed in pool < pool > .
*/
void pool_flush ( struct pool_head * pool )
{
2022-01-02 08:35:57 -05:00
struct pool_item * next , * temp , * down ;
2023-07-24 11:02:22 -04:00
uint bucket ;
2018-01-24 12:38:31 -05:00
2022-02-22 10:23:09 -05:00
if ( ! pool | | ( pool_debugging & ( POOL_DBG_NO_CACHE | POOL_DBG_NO_GLOBAL ) ) )
2018-01-24 12:38:31 -05:00
return ;
BUG/MAJOR: pools: fix possible race with free() in the lockless variant
In GH issue #1275, Fabiano Nunes Parente provided a nicely detailed
report showing reproducible crashes under musl. Musl is one of the libs
coming with a simple allocator for which we prefer to keep the shared
cache. On x86 we have a DWCAS so the lockless implementation is enabled
for such libraries.
And this implementation has had a small race since day one: the allocator
will need to read the first object's <next> pointer to place it into the
free list's head. If another thread picks the same element and immediately
releases it, while both the local and the shared pools are too crowded, it
will be freed to the OS. If the libc's allocator immediately releases it,
the memory area is unmapped and we can have a crash while trying to read
that pointer. However there is no problem as long as the item remains
mapped in memory because whatever value found there will not be placed
into the head since the counter will have changed.
The probability for this to happen is extremely low, but as analyzed by
Fabiano, it increases with the buffer size. On 16 threads it's relatively
easy to reproduce with 2MB buffers above 200k req/s, where it should
happen within the first 20 seconds of traffic usually.
This is a structural issue for which there are two non-trivial solutions:
- place a read lock in the alloc call and a barrier made of lock/unlock
in the free() call to force to serialize operations; this will have
a big performance impact since free() is already one of the contention
points;
- change the allocator to use a self-locked head, similar to what is
done in the MT_LISTS. This requires two memory writes to the head
instead of a single one, thus the overhead is exactly one memory
write during alloc and one during free;
This patch implements the second option. A new POOL_DUMMY pointer was
defined for the locked pointer value, allowing to both read and lock it
with a single xchg call. The code was carefully optimized so that the
locked period remains the shortest possible and that bus writes are
avoided as much as possible whenever the lock is held.
Tests show that while a bit slower than the original lockless
implementation on large buffers (2MB), it's 2.6 times faster than both
the no-cache and the locked implementation on such large buffers, and
remains as fast or faster than the all implementations when buffers are
48k or higher. Tests were also run on arm64 with similar results.
Note that this code is not used on modern libcs featuring a fast allocator.
A nice benefit of this change is that since it removes a dependency on
the DWCAS, it will be possible to remove the locked implementation and
replace it with this one, that is then usable on all systems, thus
significantly increasing their performance with large buffers.
Given that lockless pools were introduced in 1.9 (not supported anymore),
this patch will have to be backported as far as 2.0. The code changed
several times in this area and is subject to many ifdefs which will
complicate the backport. What is important is to remove all the DWCAS
code from the shared cache alloc/free lockless code and replace it with
this one. The pool_flush() code is basically the same code as the
allocator, retrieving the whole list at once. If in doubt regarding what
barriers to use in older versions, it's safe to use the generic ones.
This patch depends on the following previous commits:
- MINOR: pools: do not maintain the lock during pool_flush()
- MINOR: pools: call malloc_trim() under thread isolation
- MEDIUM: pools: use a single pool_gc() function for locked and lockless
The last one also removes one occurrence of an unneeded DWCAS in the
code that was incompatible with this fix. The removal of the now unused
seq field will happen in a future patch.
Many thanks to Fabiano for his detailed report, and to Olivier for
his help on this issue.
2021-06-09 12:59:58 -04:00
/* The loop below atomically detaches the head of the free list and
* replaces it with a NULL . Then the list can be released .
*/
2023-07-24 11:02:22 -04:00
for ( bucket = 0 ; bucket < CONFIG_HAP_POOL_BUCKETS ; bucket + + ) {
next = pool - > buckets [ bucket ] . free_list ;
2024-02-10 06:29:53 -05:00
while ( 1 ) {
2023-08-17 03:04:35 -04:00
while ( unlikely ( next = = POOL_BUSY ) )
next = ( void * ) pl_wait_new_long ( ( ulong * ) & pool - > buckets [ bucket ] . free_list , ( ulong ) next ) ;
2023-07-24 11:02:22 -04:00
if ( next = = NULL )
break ;
2023-07-24 09:53:17 -04:00
2024-02-10 06:29:53 -05:00
next = _HA_ATOMIC_XCHG ( & pool - > buckets [ bucket ] . free_list , POOL_BUSY ) ;
if ( next ! = POOL_BUSY ) {
HA_ATOMIC_STORE ( & pool - > buckets [ bucket ] . free_list , NULL ) ;
break ;
}
2023-07-24 11:02:22 -04:00
}
2023-07-24 09:53:17 -04:00
2023-07-24 11:02:22 -04:00
while ( next ) {
temp = next ;
next = temp - > next ;
for ( ; temp ; temp = down ) {
down = temp - > down ;
_HA_ATOMIC_DEC ( & pool - > buckets [ pool_pbucket ( temp ) ] . allocated ) ;
pool_put_to_os_nodec ( pool , temp ) ;
}
2022-01-02 08:35:57 -05:00
}
2018-01-24 12:38:31 -05:00
}
2021-06-10 00:54:22 -04:00
/* here, we should have pool->allocated == pool->used */
2018-01-24 12:38:31 -05:00
}
2007-05-13 13:38:49 -04:00
/*
* This function frees whatever can be freed in all pools , but respecting
2020-04-24 00:15:24 -04:00
* the minimum thresholds imposed by owners . It makes sure to be alone to
* run by using thread_isolate ( ) . < pool_ctx > is unused .
2007-05-13 13:38:49 -04:00
*/
2017-11-24 11:34:44 -05:00
void pool_gc ( struct pool_head * pool_ctx )
2007-05-13 13:38:49 -04:00
{
struct pool_head * entry ;
2020-04-24 00:15:24 -04:00
int isolated = thread_isolated ( ) ;
2009-04-20 20:17:45 -04:00
2020-04-24 00:15:24 -04:00
if ( ! isolated )
thread_isolate ( ) ;
2009-04-20 20:17:45 -04:00
2007-05-13 13:38:49 -04:00
list_for_each_entry ( entry , & pools , list ) {
2022-01-02 08:35:57 -05:00
struct pool_item * temp , * down ;
2023-07-24 09:53:17 -04:00
uint allocated = pool_allocated ( entry ) ;
2023-07-24 10:12:18 -04:00
uint used = pool_used ( entry ) ;
2023-07-24 11:02:22 -04:00
int bucket = 0 ;
while ( ( int ) ( allocated - used ) > ( int ) entry - > minavail ) {
/* ok let's find next entry to evict */
while ( ! entry - > buckets [ bucket ] . free_list & & bucket < CONFIG_HAP_POOL_BUCKETS )
bucket + + ;
2023-10-02 12:54:29 -04:00
if ( bucket > = CONFIG_HAP_POOL_BUCKETS )
2023-07-24 11:02:22 -04:00
break ;
2022-01-01 12:22:20 -05:00
2023-07-24 11:02:22 -04:00
temp = entry - > buckets [ bucket ] . free_list ;
entry - > buckets [ bucket ] . free_list = temp - > next ;
2022-01-02 08:35:57 -05:00
for ( ; temp ; temp = down ) {
down = temp - > down ;
2023-07-24 09:53:17 -04:00
allocated - - ;
2023-07-24 11:02:22 -04:00
_HA_ATOMIC_DEC ( & entry - > buckets [ pool_pbucket ( temp ) ] . allocated ) ;
2023-08-12 05:07:48 -04:00
pool_put_to_os_nodec ( entry , temp ) ;
2022-01-02 08:35:57 -05:00
}
2007-05-13 13:38:49 -04:00
}
}
2017-08-29 03:52:38 -04:00
2021-09-15 04:38:21 -04:00
trim_all_pools ( ) ;
2021-06-10 02:40:16 -04:00
2020-04-24 00:15:24 -04:00
if ( ! isolated )
thread_release ( ) ;
2007-05-13 13:38:49 -04:00
}
2021-04-18 04:23:02 -04:00
2022-01-24 10:09:29 -05:00
/*
* Returns a pointer to type < type > taken from the pool < pool_type > or
* dynamically allocated . In the first case , < pool_type > is updated to point to
* the next element in the list . < flags > is a binary - OR of POOL_F_ * flags .
* Prefer using pool_alloc ( ) which does the right thing without flags .
*/
void * __pool_alloc ( struct pool_head * pool , unsigned int flags )
{
void * p = NULL ;
2022-02-23 04:10:33 -05:00
void * caller = __builtin_return_address ( 0 ) ;
2022-01-24 10:09:29 -05:00
2022-02-21 11:16:22 -05:00
if ( unlikely ( pool_debugging & POOL_DBG_FAIL_ALLOC ) )
if ( ! ( flags & POOL_F_NO_FAIL ) & & mem_should_fail ( pool ) )
return NULL ;
2022-01-24 10:09:29 -05:00
2022-02-22 10:23:09 -05:00
if ( likely ( ! ( pool_debugging & POOL_DBG_NO_CACHE ) ) & & ! p )
2022-01-24 09:51:50 -05:00
p = pool_get_from_cache ( pool , caller ) ;
2022-02-22 10:23:09 -05:00
2022-01-24 10:09:29 -05:00
if ( unlikely ( ! p ) )
2023-09-11 09:01:55 -04:00
p = pool_alloc_nocache ( pool , caller ) ;
2022-01-24 10:09:29 -05:00
if ( likely ( p ) ) {
2022-08-17 03:12:53 -04:00
# ifdef USE_MEMORY_PROFILING
if ( unlikely ( profiling & HA_PROF_MEMORY ) ) {
2023-10-17 05:13:00 -04:00
extern struct memprof_stats memprof_stats [ MEMPROF_HASH_BUCKETS + 1 ] ;
2022-08-17 03:12:53 -04:00
struct memprof_stats * bin ;
bin = memprof_get_bin ( __builtin_return_address ( 0 ) , MEMPROF_METH_P_ALLOC ) ;
_HA_ATOMIC_ADD ( & bin - > alloc_calls , 1 ) ;
_HA_ATOMIC_ADD ( & bin - > alloc_tot , pool - > size ) ;
2022-08-17 03:35:16 -04:00
_HA_ATOMIC_STORE ( & bin - > info , pool ) ;
2023-10-17 05:13:00 -04:00
/* replace the caller with the allocated bin: this way
* we ' ll the pool_free ( ) call will be able to update our
* entry . We only do it for non - colliding entries though ,
2023-11-21 13:54:16 -05:00
* since these ones store the true caller location .
2023-10-17 05:13:00 -04:00
*/
if ( bin > = & memprof_stats [ 0 ] & & bin < & memprof_stats [ MEMPROF_HASH_BUCKETS ] )
POOL_DEBUG_TRACE_CALLER ( pool , ( struct pool_cache_item * ) p , bin ) ;
2022-08-17 03:12:53 -04:00
}
# endif
2022-01-24 10:09:29 -05:00
if ( unlikely ( flags & POOL_F_MUST_ZERO ) )
memset ( p , 0 , pool - > size ) ;
2022-02-23 08:15:18 -05:00
else if ( unlikely ( ! ( flags & POOL_F_NO_POISON ) & & ( pool_debugging & POOL_DBG_POISON ) ) )
2022-01-24 10:09:29 -05:00
memset ( p , mem_poison_byte , pool - > size ) ;
}
return p ;
}
2022-01-24 05:51:43 -05:00
/*
* Puts a memory area back to the corresponding pool . < ptr > be valid . Using
* pool_free ( ) is preferred .
*/
void __pool_free ( struct pool_head * pool , void * ptr )
{
2022-02-23 04:10:33 -05:00
const void * caller = __builtin_return_address ( 0 ) ;
2022-01-24 09:51:50 -05:00
2022-01-24 05:51:43 -05:00
/* we'll get late corruption if we refill to the wrong pool or double-free */
2023-09-11 05:14:43 -04:00
POOL_DEBUG_CHECK_MARK ( pool , ptr , caller ) ;
2022-02-09 10:49:16 -05:00
POOL_DEBUG_RESET_MARK ( pool , ptr ) ;
2022-02-22 10:23:09 -05:00
2022-08-17 03:12:53 -04:00
# ifdef USE_MEMORY_PROFILING
if ( unlikely ( profiling & HA_PROF_MEMORY ) & & ptr ) {
2023-10-17 05:13:00 -04:00
extern struct memprof_stats memprof_stats [ MEMPROF_HASH_BUCKETS + 1 ] ;
2022-08-17 03:12:53 -04:00
struct memprof_stats * bin ;
bin = memprof_get_bin ( __builtin_return_address ( 0 ) , MEMPROF_METH_P_FREE ) ;
_HA_ATOMIC_ADD ( & bin - > free_calls , 1 ) ;
_HA_ATOMIC_ADD ( & bin - > free_tot , pool - > size ) ;
2022-08-17 03:35:16 -04:00
_HA_ATOMIC_STORE ( & bin - > info , pool ) ;
2023-10-17 05:13:00 -04:00
/* check if the caller is an allocator, and if so, let's update
* its free ( ) count .
*/
bin = * ( struct memprof_stats * * ) ( ( ( char * ) ptr ) + pool - > alloc_sz - sizeof ( void * ) ) ;
if ( bin > = & memprof_stats [ 0 ] & & bin < & memprof_stats [ MEMPROF_HASH_BUCKETS ] ) {
_HA_ATOMIC_ADD ( & bin - > free_calls , 1 ) ;
_HA_ATOMIC_ADD ( & bin - > free_tot , pool - > size ) ;
}
2022-08-17 03:12:53 -04:00
}
# endif
2022-12-19 02:15:57 -05:00
if ( unlikely ( ( pool_debugging & POOL_DBG_NO_CACHE ) | |
global . tune . pool_cache_size < pool - > size ) ) {
2022-02-22 10:23:09 -05:00
pool_free_nocache ( pool , ptr ) ;
return ;
}
2022-01-24 09:51:50 -05:00
pool_put_to_cache ( pool , ptr , caller ) ;
2022-01-24 05:51:43 -05:00
}
2007-05-13 13:38:49 -04:00
/*
2007-06-16 17:19:53 -04:00
* This function destroys a pool by freeing it completely , unless it ' s still
* in use . This should be called only under extreme circumstances . It always
* returns NULL if the resulting pool is empty , easing the clearing of the old
* pointer , otherwise it returns the pool .
* .
2007-05-13 13:38:49 -04:00
*/
2017-11-24 11:34:44 -05:00
void * pool_destroy ( struct pool_head * pool )
2007-05-13 13:38:49 -04:00
{
2007-05-13 18:39:29 -04:00
if ( pool ) {
2022-02-22 10:23:09 -05:00
if ( ! ( pool_debugging & POOL_DBG_NO_CACHE ) )
pool_evict_from_local_cache ( pool , 1 ) ;
2017-11-24 11:34:44 -05:00
pool_flush ( pool ) ;
2023-07-24 10:12:18 -04:00
if ( pool_used ( pool ) )
2007-06-16 17:19:53 -04:00
return pool ;
pool - > users - - ;
if ( ! pool - > users ) {
2021-04-21 01:32:39 -04:00
LIST_DELETE ( & pool - > list ) ;
2021-04-16 18:31:38 -04:00
/* note that if used == 0, the cache is empty */
2022-03-03 12:31:54 -05:00
free ( pool - > base_addr ) ;
2007-06-16 17:19:53 -04:00
}
2007-05-13 18:39:29 -04:00
}
return NULL ;
2007-05-13 13:38:49 -04:00
}
2018-11-26 09:57:34 -05:00
/* This destroys all pools on exit. It is *not* thread safe. */
void pool_destroy_all ( )
{
struct pool_head * entry , * back ;
2022-04-27 05:33:13 -04:00
list_for_each_entry_safe ( entry , back , & pools , list ) {
/* there's only one occurrence of each pool in the list,
* and we ' re existing instead of looping on the whole
* list just to decrement users , force it to 1 here .
*/
entry - > users = 1 ;
2018-11-26 09:57:34 -05:00
pool_destroy ( entry ) ;
2022-04-27 05:33:13 -04:00
}
2018-11-26 09:57:34 -05:00
}
DEBUG: pools: inspect pools on fatal error and dump information found
It's a bit frustrating sometimes to see pool checks catch a bug but not
provide exploitable information without a core.
Here we're adding a function "pool_inspect_item()" which is called just
before aborting in pool_check_pattern() and POOL_DEBUG_CHECK_MARK() and
which will display the error type, the pool's pointer and name, and will
try to check if the item's tag matches the pool, and if not, will iterate
over all pools to see if one would be a better candidate, then will try
to figure the last known caller and possibly other likely candidates if
the pool's tag is not sufficiently trusted. This typically helps better
diagnose corruption in use-after-free scenarios, or freeing to a pool
that differs from the one the object was allocated from, and will also
indicate calling points that may help figure where an object was last
released or allocated. The info is printed on stderr just before the
backtrace.
For example, the recent off-by-one test in the PPv2 changes would have
produced the following output in vtest logs:
*** h1 debug|FATAL: pool inconsistency detected in thread 1: tag mismatch on free().
*** h1 debug| caller: 0x62bb87 (conn_free+0x147/0x3c5)
*** h1 debug| pool: 0x2211ec0 ('pp_tlv_256', size 304, real 320, users 1)
*** h1 debug|Tag does not match. Possible origin pool(s):
*** h1 debug| tag: @0x2565530 = 0x2216740 (pp_tlv_128, size 176, real 192, users 1)
*** h1 debug|Recorded caller if pool 'pp_tlv_128':
*** h1 debug| @0x2565538 (+0184) = 0x62c76d (conn_recv_proxy+0x4cd/0xa24)
A mismatch in the allocated/released pool is already visible, and the
callers confirm it once resolved, where the allocator indeed allocates
from pp_tlv_128 and conn_free() releases to pp_tlv_256:
$ addr2line -spafe ./haproxy <<< $'0x62bb87\n0x62c76d'
0x000000000062bb87: conn_free at connection.c:568
0x000000000062c76d: conn_recv_proxy at connection.c:1177
2023-09-11 08:05:32 -04:00
/* carefully inspects an item upon fatal error and emit diagnostics */
void pool_inspect_item ( const char * msg , struct pool_head * pool , const void * item , const void * caller )
{
const struct pool_head * the_pool = NULL ;
chunk_printf ( & trash ,
" FATAL: pool inconsistency detected in thread %d: %s. \n "
" caller: %p ( " ,
tid + 1 , msg , caller ) ;
resolve_sym_name ( & trash , NULL , caller ) ;
chunk_appendf ( & trash ,
" ) \n "
2023-09-12 11:27:07 -04:00
" item: %p \n "
DEBUG: pools: inspect pools on fatal error and dump information found
It's a bit frustrating sometimes to see pool checks catch a bug but not
provide exploitable information without a core.
Here we're adding a function "pool_inspect_item()" which is called just
before aborting in pool_check_pattern() and POOL_DEBUG_CHECK_MARK() and
which will display the error type, the pool's pointer and name, and will
try to check if the item's tag matches the pool, and if not, will iterate
over all pools to see if one would be a better candidate, then will try
to figure the last known caller and possibly other likely candidates if
the pool's tag is not sufficiently trusted. This typically helps better
diagnose corruption in use-after-free scenarios, or freeing to a pool
that differs from the one the object was allocated from, and will also
indicate calling points that may help figure where an object was last
released or allocated. The info is printed on stderr just before the
backtrace.
For example, the recent off-by-one test in the PPv2 changes would have
produced the following output in vtest logs:
*** h1 debug|FATAL: pool inconsistency detected in thread 1: tag mismatch on free().
*** h1 debug| caller: 0x62bb87 (conn_free+0x147/0x3c5)
*** h1 debug| pool: 0x2211ec0 ('pp_tlv_256', size 304, real 320, users 1)
*** h1 debug|Tag does not match. Possible origin pool(s):
*** h1 debug| tag: @0x2565530 = 0x2216740 (pp_tlv_128, size 176, real 192, users 1)
*** h1 debug|Recorded caller if pool 'pp_tlv_128':
*** h1 debug| @0x2565538 (+0184) = 0x62c76d (conn_recv_proxy+0x4cd/0xa24)
A mismatch in the allocated/released pool is already visible, and the
callers confirm it once resolved, where the allocator indeed allocates
from pp_tlv_128 and conn_free() releases to pp_tlv_256:
$ addr2line -spafe ./haproxy <<< $'0x62bb87\n0x62c76d'
0x000000000062bb87: conn_free at connection.c:568
0x000000000062c76d: conn_recv_proxy at connection.c:1177
2023-09-11 08:05:32 -04:00
" pool: %p ('%s', size %u, real %u, users %u) \n " ,
2023-09-12 11:27:07 -04:00
item , pool , pool - > name , pool - > size , pool - > alloc_sz , pool - > users ) ;
DEBUG: pools: inspect pools on fatal error and dump information found
It's a bit frustrating sometimes to see pool checks catch a bug but not
provide exploitable information without a core.
Here we're adding a function "pool_inspect_item()" which is called just
before aborting in pool_check_pattern() and POOL_DEBUG_CHECK_MARK() and
which will display the error type, the pool's pointer and name, and will
try to check if the item's tag matches the pool, and if not, will iterate
over all pools to see if one would be a better candidate, then will try
to figure the last known caller and possibly other likely candidates if
the pool's tag is not sufficiently trusted. This typically helps better
diagnose corruption in use-after-free scenarios, or freeing to a pool
that differs from the one the object was allocated from, and will also
indicate calling points that may help figure where an object was last
released or allocated. The info is printed on stderr just before the
backtrace.
For example, the recent off-by-one test in the PPv2 changes would have
produced the following output in vtest logs:
*** h1 debug|FATAL: pool inconsistency detected in thread 1: tag mismatch on free().
*** h1 debug| caller: 0x62bb87 (conn_free+0x147/0x3c5)
*** h1 debug| pool: 0x2211ec0 ('pp_tlv_256', size 304, real 320, users 1)
*** h1 debug|Tag does not match. Possible origin pool(s):
*** h1 debug| tag: @0x2565530 = 0x2216740 (pp_tlv_128, size 176, real 192, users 1)
*** h1 debug|Recorded caller if pool 'pp_tlv_128':
*** h1 debug| @0x2565538 (+0184) = 0x62c76d (conn_recv_proxy+0x4cd/0xa24)
A mismatch in the allocated/released pool is already visible, and the
callers confirm it once resolved, where the allocator indeed allocates
from pp_tlv_128 and conn_free() releases to pp_tlv_256:
$ addr2line -spafe ./haproxy <<< $'0x62bb87\n0x62c76d'
0x000000000062bb87: conn_free at connection.c:568
0x000000000062c76d: conn_recv_proxy at connection.c:1177
2023-09-11 08:05:32 -04:00
if ( pool_debugging & POOL_DBG_TAG ) {
const void * * pool_mark ;
struct pool_head * ph ;
const void * tag ;
pool_mark = ( const void * * ) ( ( ( char * ) item ) + pool - > size ) ;
tag = may_access ( pool_mark ) ? * pool_mark : NULL ;
if ( tag = = pool ) {
chunk_appendf ( & trash , " tag: @%p = %p (%s) \n " , pool_mark , tag , pool - > name ) ;
the_pool = pool ;
}
else {
2023-09-12 11:29:57 -04:00
if ( ! may_access ( pool_mark ) )
chunk_appendf ( & trash , " Tag not accessible. " ) ;
else
chunk_appendf ( & trash , " Tag does not match (%p). " , tag ) ;
DEBUG: pools: inspect pools on fatal error and dump information found
It's a bit frustrating sometimes to see pool checks catch a bug but not
provide exploitable information without a core.
Here we're adding a function "pool_inspect_item()" which is called just
before aborting in pool_check_pattern() and POOL_DEBUG_CHECK_MARK() and
which will display the error type, the pool's pointer and name, and will
try to check if the item's tag matches the pool, and if not, will iterate
over all pools to see if one would be a better candidate, then will try
to figure the last known caller and possibly other likely candidates if
the pool's tag is not sufficiently trusted. This typically helps better
diagnose corruption in use-after-free scenarios, or freeing to a pool
that differs from the one the object was allocated from, and will also
indicate calling points that may help figure where an object was last
released or allocated. The info is printed on stderr just before the
backtrace.
For example, the recent off-by-one test in the PPv2 changes would have
produced the following output in vtest logs:
*** h1 debug|FATAL: pool inconsistency detected in thread 1: tag mismatch on free().
*** h1 debug| caller: 0x62bb87 (conn_free+0x147/0x3c5)
*** h1 debug| pool: 0x2211ec0 ('pp_tlv_256', size 304, real 320, users 1)
*** h1 debug|Tag does not match. Possible origin pool(s):
*** h1 debug| tag: @0x2565530 = 0x2216740 (pp_tlv_128, size 176, real 192, users 1)
*** h1 debug|Recorded caller if pool 'pp_tlv_128':
*** h1 debug| @0x2565538 (+0184) = 0x62c76d (conn_recv_proxy+0x4cd/0xa24)
A mismatch in the allocated/released pool is already visible, and the
callers confirm it once resolved, where the allocator indeed allocates
from pp_tlv_128 and conn_free() releases to pp_tlv_256:
$ addr2line -spafe ./haproxy <<< $'0x62bb87\n0x62c76d'
0x000000000062bb87: conn_free at connection.c:568
0x000000000062c76d: conn_recv_proxy at connection.c:1177
2023-09-11 08:05:32 -04:00
list_for_each_entry ( ph , & pools , list ) {
pool_mark = ( const void * * ) ( ( ( char * ) item ) + ph - > size ) ;
if ( ! may_access ( pool_mark ) )
continue ;
tag = * pool_mark ;
if ( tag = = ph ) {
2023-09-12 11:29:57 -04:00
if ( ! the_pool )
chunk_appendf ( & trash , " Possible origin pool(s): \n " ) ;
DEBUG: pools: inspect pools on fatal error and dump information found
It's a bit frustrating sometimes to see pool checks catch a bug but not
provide exploitable information without a core.
Here we're adding a function "pool_inspect_item()" which is called just
before aborting in pool_check_pattern() and POOL_DEBUG_CHECK_MARK() and
which will display the error type, the pool's pointer and name, and will
try to check if the item's tag matches the pool, and if not, will iterate
over all pools to see if one would be a better candidate, then will try
to figure the last known caller and possibly other likely candidates if
the pool's tag is not sufficiently trusted. This typically helps better
diagnose corruption in use-after-free scenarios, or freeing to a pool
that differs from the one the object was allocated from, and will also
indicate calling points that may help figure where an object was last
released or allocated. The info is printed on stderr just before the
backtrace.
For example, the recent off-by-one test in the PPv2 changes would have
produced the following output in vtest logs:
*** h1 debug|FATAL: pool inconsistency detected in thread 1: tag mismatch on free().
*** h1 debug| caller: 0x62bb87 (conn_free+0x147/0x3c5)
*** h1 debug| pool: 0x2211ec0 ('pp_tlv_256', size 304, real 320, users 1)
*** h1 debug|Tag does not match. Possible origin pool(s):
*** h1 debug| tag: @0x2565530 = 0x2216740 (pp_tlv_128, size 176, real 192, users 1)
*** h1 debug|Recorded caller if pool 'pp_tlv_128':
*** h1 debug| @0x2565538 (+0184) = 0x62c76d (conn_recv_proxy+0x4cd/0xa24)
A mismatch in the allocated/released pool is already visible, and the
callers confirm it once resolved, where the allocator indeed allocates
from pp_tlv_128 and conn_free() releases to pp_tlv_256:
$ addr2line -spafe ./haproxy <<< $'0x62bb87\n0x62c76d'
0x000000000062bb87: conn_free at connection.c:568
0x000000000062c76d: conn_recv_proxy at connection.c:1177
2023-09-11 08:05:32 -04:00
chunk_appendf ( & trash , " tag: @%p = %p (%s, size %u, real %u, users %u) \n " ,
pool_mark , tag , ph - > name , ph - > size , ph - > alloc_sz , ph - > users ) ;
if ( ! the_pool | | the_pool - > size < ph - > size )
the_pool = ph ;
}
}
DEBUG: pools: print the contents surrounding the expected tag location
When no tag matches a known pool, we can inspect around to help figure
what could have possibly overwritten memory. The contents are printed
one machine word per line in hex, then using printable characters, and
when they can be resolved to a pointer, either the pool's pointer name
or a resolvable symbol with offset. The goal here is to help recognize
what is easily identifiable in memory.
For example applying the following patch to stream_free():
- pool_free(pool_head_stream, s);
+ pool_free(pool_head_stream, (void*)s+1);
Causes the following dump to be emitted:
FATAL: pool inconsistency detected in thread 1: tag mismatch on free().
caller: 0x59e968 (stream_free+0x6d8/0xa0a)
item: 0x13df5c1
pool: 0x12782c0 ('stream', size 888, real 904, users 1)
Tag does not match (0x4f00000000012782). Tag does not match any other pool.
Contents around address 0x13df5c1+888=0x13df939:
0x13df918 [00 00 00 00 00 00 00 00] [........]
0x13df920 [00 00 00 00 00 00 00 00] [........]
0x13df928 [00 00 00 00 00 00 00 00] [........]
0x13df930 [00 00 00 00 00 00 00 00] [........]
0x13df938 [c0 82 27 01 00 00 00 00] [..'.....] [pool:stream]
0x13df940 [4f c0 59 00 00 00 00 00] [O.Y.....] [stream_new+0x4f/0xbec]
0x13df948 [49 46 49 43 41 54 45 2d] [IFICATE-]
0x13df950 [81 02 00 00 00 00 00 00] [........]
0x13df958 [df 13 00 00 00 00 00 00] [........]
Other possible callers:
(...)
We notice that the tag references pool_head_stream with the allocation
point in stream_new. Another benefit is that a caller may be figured
from the tag even if the "caller" feature is not enabled, because upon
a free() we always put the caller's location into the tag. This should
be sufficient to debug most cases that normally require gdb.
2023-09-12 11:30:54 -04:00
if ( ! the_pool ) {
const char * start , * end , * p ;
pool_mark = ( const void * * ) ( ( ( char * ) item ) + pool - > size ) ;
chunk_appendf ( & trash ,
" Tag does not match any other pool. \n "
" Contents around address %p+%lu=%p: \n " ,
item , ( ulong ) ( ( const void * ) pool_mark - ( const void * ) item ) ,
pool_mark ) ;
/* dump in word-sized blocks */
start = ( const void * ) ( ( ( uintptr_t ) pool_mark - 32 ) & - sizeof ( void * ) ) ;
end = ( const void * ) ( ( ( uintptr_t ) pool_mark + 32 + sizeof ( void * ) - 1 ) & - sizeof ( void * ) ) ;
while ( start < end ) {
dump_addr_and_bytes ( & trash , " " , start , sizeof ( void * ) ) ;
chunk_strcat ( & trash , " [ " ) ;
for ( p = start ; p < start + sizeof ( void * ) ; p + + ) {
if ( ! may_access ( p ) )
chunk_strcat ( & trash , " * " ) ;
else if ( isprint ( ( unsigned char ) * p ) )
chunk_appendf ( & trash , " %c " , * p ) ;
else
chunk_strcat ( & trash , " . " ) ;
}
if ( may_access ( start ) )
tag = * ( const void * * ) start ;
else
tag = NULL ;
if ( tag = = pool ) {
/* the pool can often be there so let's detect it */
chunk_appendf ( & trash , " ] [pool:%s " , pool - > name ) ;
}
else if ( tag ) {
/* print pointers that resolve to a symbol */
size_t back_data = trash . data ;
chunk_strcat ( & trash , " ] [ " ) ;
if ( ! resolve_sym_name ( & trash , NULL , tag ) )
trash . data = back_data ;
}
chunk_strcat ( & trash , " ] \n " ) ;
start = p ;
}
}
DEBUG: pools: inspect pools on fatal error and dump information found
It's a bit frustrating sometimes to see pool checks catch a bug but not
provide exploitable information without a core.
Here we're adding a function "pool_inspect_item()" which is called just
before aborting in pool_check_pattern() and POOL_DEBUG_CHECK_MARK() and
which will display the error type, the pool's pointer and name, and will
try to check if the item's tag matches the pool, and if not, will iterate
over all pools to see if one would be a better candidate, then will try
to figure the last known caller and possibly other likely candidates if
the pool's tag is not sufficiently trusted. This typically helps better
diagnose corruption in use-after-free scenarios, or freeing to a pool
that differs from the one the object was allocated from, and will also
indicate calling points that may help figure where an object was last
released or allocated. The info is printed on stderr just before the
backtrace.
For example, the recent off-by-one test in the PPv2 changes would have
produced the following output in vtest logs:
*** h1 debug|FATAL: pool inconsistency detected in thread 1: tag mismatch on free().
*** h1 debug| caller: 0x62bb87 (conn_free+0x147/0x3c5)
*** h1 debug| pool: 0x2211ec0 ('pp_tlv_256', size 304, real 320, users 1)
*** h1 debug|Tag does not match. Possible origin pool(s):
*** h1 debug| tag: @0x2565530 = 0x2216740 (pp_tlv_128, size 176, real 192, users 1)
*** h1 debug|Recorded caller if pool 'pp_tlv_128':
*** h1 debug| @0x2565538 (+0184) = 0x62c76d (conn_recv_proxy+0x4cd/0xa24)
A mismatch in the allocated/released pool is already visible, and the
callers confirm it once resolved, where the allocator indeed allocates
from pp_tlv_128 and conn_free() releases to pp_tlv_256:
$ addr2line -spafe ./haproxy <<< $'0x62bb87\n0x62c76d'
0x000000000062bb87: conn_free at connection.c:568
0x000000000062c76d: conn_recv_proxy at connection.c:1177
2023-09-11 08:05:32 -04:00
}
}
if ( pool_debugging & POOL_DBG_CALLER ) {
struct buffer * trash2 = get_trash_chunk ( ) ;
const struct pool_head * ph ;
const void * * pool_mark ;
const void * tag , * rec_tag ;
ph = the_pool ? the_pool : pool ;
pool_mark = ( const void * * ) ( ( ( char * ) item ) + ph - > alloc_sz - sizeof ( void * ) ) ;
rec_tag = may_access ( pool_mark ) ? * pool_mark : NULL ;
if ( rec_tag & & resolve_sym_name ( trash2 , NULL , rec_tag ) )
chunk_appendf ( & trash ,
" Recorded caller if pool '%s': \n @%p (+%04u) = %p (%s) \n " ,
ph - > name , pool_mark , ( uint ) ( ph - > alloc_sz - sizeof ( void * ) ) ,
rec_tag , trash2 - > area ) ;
if ( ! the_pool ) {
/* the pool couldn't be formally verified */
chunk_appendf ( & trash , " Other possible callers: \n " ) ;
list_for_each_entry ( ph , & pools , list ) {
2023-09-13 07:31:41 -04:00
if ( ph = = pool )
DEBUG: pools: inspect pools on fatal error and dump information found
It's a bit frustrating sometimes to see pool checks catch a bug but not
provide exploitable information without a core.
Here we're adding a function "pool_inspect_item()" which is called just
before aborting in pool_check_pattern() and POOL_DEBUG_CHECK_MARK() and
which will display the error type, the pool's pointer and name, and will
try to check if the item's tag matches the pool, and if not, will iterate
over all pools to see if one would be a better candidate, then will try
to figure the last known caller and possibly other likely candidates if
the pool's tag is not sufficiently trusted. This typically helps better
diagnose corruption in use-after-free scenarios, or freeing to a pool
that differs from the one the object was allocated from, and will also
indicate calling points that may help figure where an object was last
released or allocated. The info is printed on stderr just before the
backtrace.
For example, the recent off-by-one test in the PPv2 changes would have
produced the following output in vtest logs:
*** h1 debug|FATAL: pool inconsistency detected in thread 1: tag mismatch on free().
*** h1 debug| caller: 0x62bb87 (conn_free+0x147/0x3c5)
*** h1 debug| pool: 0x2211ec0 ('pp_tlv_256', size 304, real 320, users 1)
*** h1 debug|Tag does not match. Possible origin pool(s):
*** h1 debug| tag: @0x2565530 = 0x2216740 (pp_tlv_128, size 176, real 192, users 1)
*** h1 debug|Recorded caller if pool 'pp_tlv_128':
*** h1 debug| @0x2565538 (+0184) = 0x62c76d (conn_recv_proxy+0x4cd/0xa24)
A mismatch in the allocated/released pool is already visible, and the
callers confirm it once resolved, where the allocator indeed allocates
from pp_tlv_128 and conn_free() releases to pp_tlv_256:
$ addr2line -spafe ./haproxy <<< $'0x62bb87\n0x62c76d'
0x000000000062bb87: conn_free at connection.c:568
0x000000000062c76d: conn_recv_proxy at connection.c:1177
2023-09-11 08:05:32 -04:00
continue ;
pool_mark = ( const void * * ) ( ( ( char * ) item ) + ph - > alloc_sz - sizeof ( void * ) ) ;
if ( ! may_access ( pool_mark ) )
continue ;
tag = * pool_mark ;
if ( tag = = rec_tag )
continue ;
/* see if we can resolve something */
chunk_printf ( trash2 , " @%p (+%04u) = %p ( " , pool_mark , ( uint ) ( ph - > alloc_sz - sizeof ( void * ) ) , tag ) ;
if ( resolve_sym_name ( trash2 , NULL , tag ) ) {
chunk_appendf ( trash2 , " ) " ) ;
chunk_appendf ( & trash ,
" %s [as pool %s, size %u, real %u, users %u] \n " ,
trash2 - > area , ph - > name , ph - > size , ph - > alloc_sz , ph - > users ) ;
}
}
}
}
chunk_appendf ( & trash , " \n " ) ;
DISGUISE ( write ( 2 , trash . area , trash . data ) ) ;
}
2022-11-21 03:34:02 -05:00
/* used by qsort in "show pools" to sort by name */
static int cmp_dump_pools_name ( const void * a , const void * b )
{
const struct pool_dump_info * l = ( const struct pool_dump_info * ) a ;
const struct pool_dump_info * r = ( const struct pool_dump_info * ) b ;
return strcmp ( l - > entry - > name , r - > entry - > name ) ;
}
/* used by qsort in "show pools" to sort by item size */
static int cmp_dump_pools_size ( const void * a , const void * b )
{
const struct pool_dump_info * l = ( const struct pool_dump_info * ) a ;
const struct pool_dump_info * r = ( const struct pool_dump_info * ) b ;
if ( l - > entry - > size > r - > entry - > size )
return - 1 ;
else if ( l - > entry - > size < r - > entry - > size )
return 1 ;
else
return 0 ;
}
/* used by qsort in "show pools" to sort by usage */
static int cmp_dump_pools_usage ( const void * a , const void * b )
{
const struct pool_dump_info * l = ( const struct pool_dump_info * ) a ;
const struct pool_dump_info * r = ( const struct pool_dump_info * ) b ;
if ( l - > alloc_bytes > r - > alloc_bytes )
return - 1 ;
else if ( l - > alloc_bytes < r - > alloc_bytes )
return 1 ;
else
return 0 ;
}
2022-11-21 03:02:41 -05:00
/* will not dump more than this number of entries. Anything beyond this will
* likely not fit into a regular output buffer anyway .
*/
# define POOLS_MAX_DUMPED_ENTRIES 1024
2022-11-21 03:34:02 -05:00
/* This function dumps memory usage information into the trash buffer.
* It may sort by a criterion if < by_what > is non - zero , and limit the
MINOR: cli/pools: add pool name filtering capability to "show pools"
Now it becomes possible to match a pool name's prefix, for example:
$ socat - /tmp/haproxy.sock <<< "show pools match quic byusage"
Dumping pools usage. Use SIGQUIT to flush them.
- Pool quic_conn_r (65560 bytes) : 1337 allocated (87653720 bytes), ...
- Pool quic_crypto (1048 bytes) : 6685 allocated (7005880 bytes), ...
- Pool quic_conn (4056 bytes) : 1337 allocated (5422872 bytes), ...
- Pool quic_rxbuf (262168 bytes) : 8 allocated (2097344 bytes), ...
- Pool quic_connne (184 bytes) : 9359 allocated (1722056 bytes), ...
- Pool quic_frame (184 bytes) : 7938 allocated (1460592 bytes), ...
- Pool quic_tx_pac (152 bytes) : 6454 allocated (981008 bytes), ...
- Pool quic_tls_ke (56 bytes) : 12033 allocated (673848 bytes), ...
- Pool quic_rx_pac (408 bytes) : 1596 allocated (651168 bytes), ...
- Pool quic_tls_se (88 bytes) : 6685 allocated (588280 bytes), ...
- Pool quic_cstrea (88 bytes) : 4011 allocated (352968 bytes), ...
- Pool quic_tls_iv (24 bytes) : 12033 allocated (288792 bytes), ...
- Pool quic_dgram (344 bytes) : 732 allocated (251808 bytes), ...
- Pool quic_arng (56 bytes) : 4011 allocated (224616 bytes), ...
- Pool quic_conn_c (152 bytes) : 1337 allocated (203224 bytes), ...
Total: 15 pools, 109578176 bytes allocated, 109578176 used ...
In this case the reported total only concerns the dumped ones.
2022-11-21 04:02:29 -05:00
* number of output lines if < max > is non - zero . It may limit only to
* pools whose names start with < pfx > if < pfx > is non - null .
2022-11-21 03:34:02 -05:00
*/
MINOR: cli/pools: add pool name filtering capability to "show pools"
Now it becomes possible to match a pool name's prefix, for example:
$ socat - /tmp/haproxy.sock <<< "show pools match quic byusage"
Dumping pools usage. Use SIGQUIT to flush them.
- Pool quic_conn_r (65560 bytes) : 1337 allocated (87653720 bytes), ...
- Pool quic_crypto (1048 bytes) : 6685 allocated (7005880 bytes), ...
- Pool quic_conn (4056 bytes) : 1337 allocated (5422872 bytes), ...
- Pool quic_rxbuf (262168 bytes) : 8 allocated (2097344 bytes), ...
- Pool quic_connne (184 bytes) : 9359 allocated (1722056 bytes), ...
- Pool quic_frame (184 bytes) : 7938 allocated (1460592 bytes), ...
- Pool quic_tx_pac (152 bytes) : 6454 allocated (981008 bytes), ...
- Pool quic_tls_ke (56 bytes) : 12033 allocated (673848 bytes), ...
- Pool quic_rx_pac (408 bytes) : 1596 allocated (651168 bytes), ...
- Pool quic_tls_se (88 bytes) : 6685 allocated (588280 bytes), ...
- Pool quic_cstrea (88 bytes) : 4011 allocated (352968 bytes), ...
- Pool quic_tls_iv (24 bytes) : 12033 allocated (288792 bytes), ...
- Pool quic_dgram (344 bytes) : 732 allocated (251808 bytes), ...
- Pool quic_arng (56 bytes) : 4011 allocated (224616 bytes), ...
- Pool quic_conn_c (152 bytes) : 1337 allocated (203224 bytes), ...
Total: 15 pools, 109578176 bytes allocated, 109578176 used ...
In this case the reported total only concerns the dumped ones.
2022-11-21 04:02:29 -05:00
void dump_pools_to_trash ( int by_what , int max , const char * pfx )
2007-05-13 12:26:08 -04:00
{
2022-11-21 03:02:41 -05:00
struct pool_dump_info pool_info [ POOLS_MAX_DUMPED_ENTRIES ] ;
2007-05-13 12:26:08 -04:00
struct pool_head * entry ;
2022-11-17 05:08:03 -05:00
unsigned long long allocated , used ;
2022-11-21 03:02:41 -05:00
int nbpools , i ;
2022-11-17 05:08:03 -05:00
unsigned long long cached_bytes = 0 ;
2021-10-07 10:29:31 -04:00
uint cached = 0 ;
2023-07-24 09:53:17 -04:00
uint alloc_items ;
2007-05-13 12:26:08 -04:00
allocated = used = nbpools = 0 ;
2022-11-21 03:02:41 -05:00
2007-05-13 12:26:08 -04:00
list_for_each_entry ( entry , & pools , list ) {
2022-11-21 03:02:41 -05:00
if ( nbpools > = POOLS_MAX_DUMPED_ENTRIES )
break ;
2023-07-24 09:53:17 -04:00
alloc_items = pool_allocated ( entry ) ;
2022-11-21 03:34:02 -05:00
/* do not dump unused entries when sorting by usage */
2023-07-24 09:53:17 -04:00
if ( by_what = = 3 & & ! alloc_items )
2022-11-21 03:34:02 -05:00
continue ;
MINOR: cli/pools: add pool name filtering capability to "show pools"
Now it becomes possible to match a pool name's prefix, for example:
$ socat - /tmp/haproxy.sock <<< "show pools match quic byusage"
Dumping pools usage. Use SIGQUIT to flush them.
- Pool quic_conn_r (65560 bytes) : 1337 allocated (87653720 bytes), ...
- Pool quic_crypto (1048 bytes) : 6685 allocated (7005880 bytes), ...
- Pool quic_conn (4056 bytes) : 1337 allocated (5422872 bytes), ...
- Pool quic_rxbuf (262168 bytes) : 8 allocated (2097344 bytes), ...
- Pool quic_connne (184 bytes) : 9359 allocated (1722056 bytes), ...
- Pool quic_frame (184 bytes) : 7938 allocated (1460592 bytes), ...
- Pool quic_tx_pac (152 bytes) : 6454 allocated (981008 bytes), ...
- Pool quic_tls_ke (56 bytes) : 12033 allocated (673848 bytes), ...
- Pool quic_rx_pac (408 bytes) : 1596 allocated (651168 bytes), ...
- Pool quic_tls_se (88 bytes) : 6685 allocated (588280 bytes), ...
- Pool quic_cstrea (88 bytes) : 4011 allocated (352968 bytes), ...
- Pool quic_tls_iv (24 bytes) : 12033 allocated (288792 bytes), ...
- Pool quic_dgram (344 bytes) : 732 allocated (251808 bytes), ...
- Pool quic_arng (56 bytes) : 4011 allocated (224616 bytes), ...
- Pool quic_conn_c (152 bytes) : 1337 allocated (203224 bytes), ...
Total: 15 pools, 109578176 bytes allocated, 109578176 used ...
In this case the reported total only concerns the dumped ones.
2022-11-21 04:02:29 -05:00
/* verify the pool name if a prefix is requested */
if ( pfx & & strncmp ( entry - > name , pfx , strlen ( pfx ) ) ! = 0 )
continue ;
2022-02-22 10:23:09 -05:00
if ( ! ( pool_debugging & POOL_DBG_NO_CACHE ) ) {
for ( cached = i = 0 ; i < global . nbthread ; i + + )
cached + = entry - > cache [ i ] . count ;
}
2022-11-21 03:02:41 -05:00
pool_info [ nbpools ] . entry = entry ;
2023-07-24 09:53:17 -04:00
pool_info [ nbpools ] . alloc_items = alloc_items ;
pool_info [ nbpools ] . alloc_bytes = ( ulong ) entry - > size * alloc_items ;
2023-07-24 10:12:18 -04:00
pool_info [ nbpools ] . used_items = pool_used ( entry ) ;
2022-11-21 03:02:41 -05:00
pool_info [ nbpools ] . cached_items = cached ;
2023-07-24 10:18:25 -04:00
pool_info [ nbpools ] . need_avg = swrate_avg ( pool_needed_avg ( entry ) , POOL_AVG_SAMPLES ) ;
2023-07-24 10:38:09 -04:00
pool_info [ nbpools ] . failed_items = pool_failed ( entry ) ;
2007-05-13 12:26:08 -04:00
nbpools + + ;
}
2022-11-21 03:02:41 -05:00
2022-11-21 03:34:02 -05:00
if ( by_what = = 1 ) /* sort by name */
qsort ( pool_info , nbpools , sizeof ( pool_info [ 0 ] ) , cmp_dump_pools_name ) ;
else if ( by_what = = 2 ) /* sort by item size */
qsort ( pool_info , nbpools , sizeof ( pool_info [ 0 ] ) , cmp_dump_pools_size ) ;
else if ( by_what = = 3 ) /* sort by total usage */
qsort ( pool_info , nbpools , sizeof ( pool_info [ 0 ] ) , cmp_dump_pools_usage ) ;
2022-11-21 03:02:41 -05:00
chunk_printf ( & trash , " Dumping pools usage " ) ;
2022-11-21 03:34:02 -05:00
if ( ! max | | max > = POOLS_MAX_DUMPED_ENTRIES )
max = POOLS_MAX_DUMPED_ENTRIES ;
if ( nbpools > = max )
chunk_appendf ( & trash , " (limited to the first %u entries) " , max ) ;
2022-11-21 03:02:41 -05:00
chunk_appendf ( & trash , " . Use SIGQUIT to flush them. \n " ) ;
2022-11-21 03:34:02 -05:00
for ( i = 0 ; i < nbpools & & i < max ; i + + ) {
2022-11-21 03:02:41 -05:00
chunk_appendf ( & trash , " - Pool %s (%lu bytes) : %lu allocated (%lu bytes), %lu used "
" (~%lu by thread caches) "
" , needed_avg %lu, %lu failures, %u users, @%p%s \n " ,
pool_info [ i ] . entry - > name , ( ulong ) pool_info [ i ] . entry - > size ,
pool_info [ i ] . alloc_items , pool_info [ i ] . alloc_bytes ,
pool_info [ i ] . used_items , pool_info [ i ] . cached_items ,
pool_info [ i ] . need_avg , pool_info [ i ] . failed_items ,
pool_info [ i ] . entry - > users , pool_info [ i ] . entry ,
( pool_info [ i ] . entry - > flags & MEM_F_SHARED ) ? " [SHARED] " : " " ) ;
cached_bytes + = pool_info [ i ] . cached_items * ( ulong ) pool_info [ i ] . entry - > size ;
allocated + = pool_info [ i ] . alloc_items * ( ulong ) pool_info [ i ] . entry - > size ;
used + = pool_info [ i ] . used_items * ( ulong ) pool_info [ i ] . entry - > size ;
}
2022-11-17 05:08:03 -05:00
chunk_appendf ( & trash , " Total: %d pools, %llu bytes allocated, %llu used "
" (~%llu by thread caches) "
2021-10-07 10:29:31 -04:00
" . \n " ,
2022-02-22 10:23:09 -05:00
nbpools , allocated , used , cached_bytes
2021-10-07 10:29:31 -04:00
) ;
2007-05-13 12:26:08 -04:00
}
2014-01-28 10:49:56 -05:00
/* Dump statistics on pools usage. */
void dump_pools ( void )
{
MINOR: cli/pools: add pool name filtering capability to "show pools"
Now it becomes possible to match a pool name's prefix, for example:
$ socat - /tmp/haproxy.sock <<< "show pools match quic byusage"
Dumping pools usage. Use SIGQUIT to flush them.
- Pool quic_conn_r (65560 bytes) : 1337 allocated (87653720 bytes), ...
- Pool quic_crypto (1048 bytes) : 6685 allocated (7005880 bytes), ...
- Pool quic_conn (4056 bytes) : 1337 allocated (5422872 bytes), ...
- Pool quic_rxbuf (262168 bytes) : 8 allocated (2097344 bytes), ...
- Pool quic_connne (184 bytes) : 9359 allocated (1722056 bytes), ...
- Pool quic_frame (184 bytes) : 7938 allocated (1460592 bytes), ...
- Pool quic_tx_pac (152 bytes) : 6454 allocated (981008 bytes), ...
- Pool quic_tls_ke (56 bytes) : 12033 allocated (673848 bytes), ...
- Pool quic_rx_pac (408 bytes) : 1596 allocated (651168 bytes), ...
- Pool quic_tls_se (88 bytes) : 6685 allocated (588280 bytes), ...
- Pool quic_cstrea (88 bytes) : 4011 allocated (352968 bytes), ...
- Pool quic_tls_iv (24 bytes) : 12033 allocated (288792 bytes), ...
- Pool quic_dgram (344 bytes) : 732 allocated (251808 bytes), ...
- Pool quic_arng (56 bytes) : 4011 allocated (224616 bytes), ...
- Pool quic_conn_c (152 bytes) : 1337 allocated (203224 bytes), ...
Total: 15 pools, 109578176 bytes allocated, 109578176 used ...
In this case the reported total only concerns the dumped ones.
2022-11-21 04:02:29 -05:00
dump_pools_to_trash ( 0 , 0 , NULL ) ;
2018-07-13 04:54:26 -04:00
qfprintf ( stderr , " %s " , trash . area ) ;
2014-01-28 10:49:56 -05:00
}
2015-10-28 11:24:21 -04:00
/* This function returns the total number of failed pool allocations */
int pool_total_failures ( )
{
struct pool_head * entry ;
int failed = 0 ;
list_for_each_entry ( entry , & pools , list )
2023-07-24 10:38:09 -04:00
failed + = pool_failed ( entry ) ;
2015-10-28 11:24:21 -04:00
return failed ;
}
/* This function returns the total amount of memory allocated in pools (in bytes) */
2022-12-22 05:05:48 -05:00
unsigned long long pool_total_allocated ( )
2015-10-28 11:24:21 -04:00
{
struct pool_head * entry ;
2022-12-22 05:05:48 -05:00
unsigned long long allocated = 0 ;
2015-10-28 11:24:21 -04:00
list_for_each_entry ( entry , & pools , list )
2023-07-24 09:53:17 -04:00
allocated + = pool_allocated ( entry ) * ( ullong ) entry - > size ;
2015-10-28 11:24:21 -04:00
return allocated ;
}
/* This function returns the total amount of memory used in pools (in bytes) */
2022-12-22 05:05:48 -05:00
unsigned long long pool_total_used ( )
2015-10-28 11:24:21 -04:00
{
struct pool_head * entry ;
2022-12-22 05:05:48 -05:00
unsigned long long used = 0 ;
2015-10-28 11:24:21 -04:00
list_for_each_entry ( entry , & pools , list )
2023-07-24 10:12:18 -04:00
used + = pool_used ( entry ) * ( ullong ) entry - > size ;
2015-10-28 11:24:21 -04:00
return used ;
}
2022-02-18 12:54:40 -05:00
/* This function parses a string made of a set of debugging features as
* specified after - dM on the command line , and will set pool_debugging
* accordingly . On success it returns a strictly positive value . It may zero
* with the first warning in < err > , - 1 with a help message in < err > , or - 2 with
* the first error in < err > return the first error in < err > . < err > is undefined
* on success , and will be non - null and locally allocated on help / error / warning .
* The caller must free it . Warnings are used to report features that were not
* enabled at build time , and errors are used to report unknown features .
*/
int pool_parse_debugging ( const char * str , char * * err )
{
2022-02-23 09:20:53 -05:00
struct ist args ;
2022-02-18 12:54:40 -05:00
char * end ;
2022-02-23 09:20:53 -05:00
uint new_dbg ;
2022-02-18 12:54:40 -05:00
int v ;
/* if it's empty or starts with a number, it's the mem poisonning byte */
v = strtol ( str , & end , 0 ) ;
if ( ! * end | | * end = = ' , ' ) {
mem_poison_byte = * str ? v : ' P ' ;
if ( mem_poison_byte > = 0 )
pool_debugging | = POOL_DBG_POISON ;
else
pool_debugging & = ~ POOL_DBG_POISON ;
str = end ;
}
2022-02-23 09:20:53 -05:00
new_dbg = pool_debugging ;
for ( args = ist ( str ) ; istlen ( args ) ; args = istadv ( istfind ( args , ' , ' ) , 1 ) ) {
struct ist feat = iststop ( args , ' , ' ) ;
if ( ! istlen ( feat ) )
continue ;
if ( isteq ( feat , ist ( " help " ) ) ) {
ha_free ( err ) ;
memprintf ( err ,
" -dM alone enables memory poisonning with byte 0x50 on allocation. A numeric \n "
" value may be appended immediately after -dM to use another value (0 supported). \n "
" Then an optional list of comma-delimited keywords may be appended to set or \n "
" clear some debugging options ('*' marks the current setting): \n \n "
" set clear description \n "
" -----------------+-----------------+----------------------------------------- \n " ) ;
for ( v = 0 ; dbg_options [ v ] . flg ; v + + ) {
memprintf ( err , " %s %c %-15s|%c %-15s| %s \n " ,
* err ,
( pool_debugging & dbg_options [ v ] . flg ) ? ' * ' : ' ' ,
dbg_options [ v ] . set ,
( pool_debugging & dbg_options [ v ] . flg ) ? ' ' : ' * ' ,
dbg_options [ v ] . clr ,
dbg_options [ v ] . hlp ) ;
}
2022-12-08 12:42:51 -05:00
memprintf ( err ,
" %s -----------------+-----------------+----------------------------------------- \n "
" Examples: \n "
" Disable merging and enable poisonning with byte 'P': -dM0x50,no-merge \n "
" Randomly fail allocations: -dMfail \n "
" Detect out-of-bound corruptions: -dMno-merge,tag \n "
" Detect post-free cache corruptions: -dMno-merge,cold-first,integrity,caller \n "
" Detect all cache corruptions: -dMno-merge,cold-first,integrity,tag,caller \n "
MINOR: pools: make DEBUG_UAF a runtime setting
Since the massive pools cleanup that happened in 2.6, the pools
architecture was made quite more hierarchical and many alternate code
blocks could be moved to runtime flags set by -dM. One of them had not
been converted by then, DEBUG_UAF. It's not much more difficult actually,
since it only acts on a pair of functions indirection on the slow path
(OS-level allocator) and a default setting for the cache activation.
This patch adds the "uaf" setting to the options permitted in -dM so
that it now becomes possible to set or unset UAF at boot time without
recompiling. This is particularly convenient, because every 3 months on
average, developers ask a user to recompile haproxy with DEBUG_UAF to
understand a bug. Now it will not be needed anymore, instead the user
will only have to disable pools and enable uaf using -dMuaf. Note that
-dMuaf only disables previously enabled pools, but it remains possible
to re-enable caching by specifying the cache after, like -dMuaf,cache.
A few tests with this mode show that it can be an interesting combination
which catches significantly less UAF but will do so with much less
overhead, so it might be compatible with some high-traffic deployments.
The change is very small and isolated. It could be helpful to backport
this at least to 2.7 once confirmed not to cause build issues on exotic
systems, and even to 2.6 a bit later as this has proven to be useful
over time, and could be even more if it did not require a rebuild. If
a backport is desired, the following patches are needed as well:
CLEANUP: pools: move the write before free to the uaf-only function
CLEANUP: pool: only include pool-os from pool.c not pool.h
REORG: pool: move all the OS specific code to pool-os.h
CLEANUP: pools: get rid of CONFIG_HAP_POOLS
DEBUG: pool: show a few examples in -dMhelp
2022-12-08 11:47:59 -05:00
" Detect UAF (disables cache, very slow): -dMuaf \n "
" Detect post-cache UAF: -dMuaf,cache,no-merge,cold-first,integrity,tag,caller \n "
2022-12-08 12:42:51 -05:00
" Detect post-free cache corruptions: -dMno-merge,cold-first,integrity,caller \n " ,
* err ) ;
2022-02-23 09:20:53 -05:00
return - 1 ;
}
for ( v = 0 ; dbg_options [ v ] . flg ; v + + ) {
if ( isteq ( feat , ist ( dbg_options [ v ] . set ) ) ) {
new_dbg | = dbg_options [ v ] . flg ;
MINOR: pools: make DEBUG_UAF a runtime setting
Since the massive pools cleanup that happened in 2.6, the pools
architecture was made quite more hierarchical and many alternate code
blocks could be moved to runtime flags set by -dM. One of them had not
been converted by then, DEBUG_UAF. It's not much more difficult actually,
since it only acts on a pair of functions indirection on the slow path
(OS-level allocator) and a default setting for the cache activation.
This patch adds the "uaf" setting to the options permitted in -dM so
that it now becomes possible to set or unset UAF at boot time without
recompiling. This is particularly convenient, because every 3 months on
average, developers ask a user to recompile haproxy with DEBUG_UAF to
understand a bug. Now it will not be needed anymore, instead the user
will only have to disable pools and enable uaf using -dMuaf. Note that
-dMuaf only disables previously enabled pools, but it remains possible
to re-enable caching by specifying the cache after, like -dMuaf,cache.
A few tests with this mode show that it can be an interesting combination
which catches significantly less UAF but will do so with much less
overhead, so it might be compatible with some high-traffic deployments.
The change is very small and isolated. It could be helpful to backport
this at least to 2.7 once confirmed not to cause build issues on exotic
systems, and even to 2.6 a bit later as this has proven to be useful
over time, and could be even more if it did not require a rebuild. If
a backport is desired, the following patches are needed as well:
CLEANUP: pools: move the write before free to the uaf-only function
CLEANUP: pool: only include pool-os from pool.c not pool.h
REORG: pool: move all the OS specific code to pool-os.h
CLEANUP: pools: get rid of CONFIG_HAP_POOLS
DEBUG: pool: show a few examples in -dMhelp
2022-12-08 11:47:59 -05:00
/* UAF implicitly disables caching, but it's
* still possible to forcefully re - enable it .
*/
if ( dbg_options [ v ] . flg = = POOL_DBG_UAF )
new_dbg | = POOL_DBG_NO_CACHE ;
2023-03-21 04:24:53 -04:00
/* fail should preset the tune.fail-alloc ratio to 1% */
if ( dbg_options [ v ] . flg = = POOL_DBG_FAIL_ALLOC )
mem_fail_rate = 1 ;
2022-02-23 09:20:53 -05:00
break ;
}
else if ( isteq ( feat , ist ( dbg_options [ v ] . clr ) ) ) {
new_dbg & = ~ dbg_options [ v ] . flg ;
2023-03-21 04:24:53 -04:00
/* no-fail should reset the tune.fail-alloc ratio */
if ( dbg_options [ v ] . flg = = POOL_DBG_FAIL_ALLOC )
mem_fail_rate = 0 ;
2022-02-23 09:20:53 -05:00
break ;
}
}
if ( ! dbg_options [ v ] . flg ) {
memprintf ( err , " unknown pool debugging feature <%.*s> " , ( int ) istlen ( feat ) , istptr ( feat ) ) ;
return - 2 ;
}
}
pool_debugging = new_dbg ;
2022-02-18 12:54:40 -05:00
return 1 ;
}
2022-11-21 03:34:02 -05:00
/* parse a "show pools" command. It returns 1 on failure, 0 if it starts to dump. */
static int cli_parse_show_pools ( char * * args , char * payload , struct appctx * appctx , void * private )
{
struct show_pools_ctx * ctx = applet_reserve_svcctx ( appctx , sizeof ( * ctx ) ) ;
int arg ;
for ( arg = 2 ; * args [ arg ] ; arg + + ) {
if ( strcmp ( args [ arg ] , " byname " ) = = 0 ) {
ctx - > by_what = 1 ; // sort output by name
}
else if ( strcmp ( args [ arg ] , " bysize " ) = = 0 ) {
ctx - > by_what = 2 ; // sort output by item size
}
else if ( strcmp ( args [ arg ] , " byusage " ) = = 0 ) {
ctx - > by_what = 3 ; // sort output by total allocated size
}
MINOR: cli/pools: add pool name filtering capability to "show pools"
Now it becomes possible to match a pool name's prefix, for example:
$ socat - /tmp/haproxy.sock <<< "show pools match quic byusage"
Dumping pools usage. Use SIGQUIT to flush them.
- Pool quic_conn_r (65560 bytes) : 1337 allocated (87653720 bytes), ...
- Pool quic_crypto (1048 bytes) : 6685 allocated (7005880 bytes), ...
- Pool quic_conn (4056 bytes) : 1337 allocated (5422872 bytes), ...
- Pool quic_rxbuf (262168 bytes) : 8 allocated (2097344 bytes), ...
- Pool quic_connne (184 bytes) : 9359 allocated (1722056 bytes), ...
- Pool quic_frame (184 bytes) : 7938 allocated (1460592 bytes), ...
- Pool quic_tx_pac (152 bytes) : 6454 allocated (981008 bytes), ...
- Pool quic_tls_ke (56 bytes) : 12033 allocated (673848 bytes), ...
- Pool quic_rx_pac (408 bytes) : 1596 allocated (651168 bytes), ...
- Pool quic_tls_se (88 bytes) : 6685 allocated (588280 bytes), ...
- Pool quic_cstrea (88 bytes) : 4011 allocated (352968 bytes), ...
- Pool quic_tls_iv (24 bytes) : 12033 allocated (288792 bytes), ...
- Pool quic_dgram (344 bytes) : 732 allocated (251808 bytes), ...
- Pool quic_arng (56 bytes) : 4011 allocated (224616 bytes), ...
- Pool quic_conn_c (152 bytes) : 1337 allocated (203224 bytes), ...
Total: 15 pools, 109578176 bytes allocated, 109578176 used ...
In this case the reported total only concerns the dumped ones.
2022-11-21 04:02:29 -05:00
else if ( strcmp ( args [ arg ] , " match " ) = = 0 & & * args [ arg + 1 ] ) {
ctx - > prefix = strdup ( args [ arg + 1 ] ) ; // only pools starting with this
arg + + ;
}
2022-11-21 03:34:02 -05:00
else if ( isdigit ( ( unsigned char ) * args [ arg ] ) ) {
ctx - > maxcnt = atoi ( args [ arg ] ) ; // number of entries to dump
}
else
MINOR: cli/pools: add pool name filtering capability to "show pools"
Now it becomes possible to match a pool name's prefix, for example:
$ socat - /tmp/haproxy.sock <<< "show pools match quic byusage"
Dumping pools usage. Use SIGQUIT to flush them.
- Pool quic_conn_r (65560 bytes) : 1337 allocated (87653720 bytes), ...
- Pool quic_crypto (1048 bytes) : 6685 allocated (7005880 bytes), ...
- Pool quic_conn (4056 bytes) : 1337 allocated (5422872 bytes), ...
- Pool quic_rxbuf (262168 bytes) : 8 allocated (2097344 bytes), ...
- Pool quic_connne (184 bytes) : 9359 allocated (1722056 bytes), ...
- Pool quic_frame (184 bytes) : 7938 allocated (1460592 bytes), ...
- Pool quic_tx_pac (152 bytes) : 6454 allocated (981008 bytes), ...
- Pool quic_tls_ke (56 bytes) : 12033 allocated (673848 bytes), ...
- Pool quic_rx_pac (408 bytes) : 1596 allocated (651168 bytes), ...
- Pool quic_tls_se (88 bytes) : 6685 allocated (588280 bytes), ...
- Pool quic_cstrea (88 bytes) : 4011 allocated (352968 bytes), ...
- Pool quic_tls_iv (24 bytes) : 12033 allocated (288792 bytes), ...
- Pool quic_dgram (344 bytes) : 732 allocated (251808 bytes), ...
- Pool quic_arng (56 bytes) : 4011 allocated (224616 bytes), ...
- Pool quic_conn_c (152 bytes) : 1337 allocated (203224 bytes), ...
Total: 15 pools, 109578176 bytes allocated, 109578176 used ...
In this case the reported total only concerns the dumped ones.
2022-11-21 04:02:29 -05:00
return cli_err ( appctx , " Expects either 'byname', 'bysize', 'byusage', 'match <pfx>', or a max number of output lines. \n " ) ;
2022-11-21 03:34:02 -05:00
}
return 0 ;
}
MINOR: cli/pools: add pool name filtering capability to "show pools"
Now it becomes possible to match a pool name's prefix, for example:
$ socat - /tmp/haproxy.sock <<< "show pools match quic byusage"
Dumping pools usage. Use SIGQUIT to flush them.
- Pool quic_conn_r (65560 bytes) : 1337 allocated (87653720 bytes), ...
- Pool quic_crypto (1048 bytes) : 6685 allocated (7005880 bytes), ...
- Pool quic_conn (4056 bytes) : 1337 allocated (5422872 bytes), ...
- Pool quic_rxbuf (262168 bytes) : 8 allocated (2097344 bytes), ...
- Pool quic_connne (184 bytes) : 9359 allocated (1722056 bytes), ...
- Pool quic_frame (184 bytes) : 7938 allocated (1460592 bytes), ...
- Pool quic_tx_pac (152 bytes) : 6454 allocated (981008 bytes), ...
- Pool quic_tls_ke (56 bytes) : 12033 allocated (673848 bytes), ...
- Pool quic_rx_pac (408 bytes) : 1596 allocated (651168 bytes), ...
- Pool quic_tls_se (88 bytes) : 6685 allocated (588280 bytes), ...
- Pool quic_cstrea (88 bytes) : 4011 allocated (352968 bytes), ...
- Pool quic_tls_iv (24 bytes) : 12033 allocated (288792 bytes), ...
- Pool quic_dgram (344 bytes) : 732 allocated (251808 bytes), ...
- Pool quic_arng (56 bytes) : 4011 allocated (224616 bytes), ...
- Pool quic_conn_c (152 bytes) : 1337 allocated (203224 bytes), ...
Total: 15 pools, 109578176 bytes allocated, 109578176 used ...
In this case the reported total only concerns the dumped ones.
2022-11-21 04:02:29 -05:00
/* release the "show pools" context */
static void cli_release_show_pools ( struct appctx * appctx )
{
struct show_pools_ctx * ctx = appctx - > svcctx ;
ha_free ( & ctx - > prefix ) ;
}
2022-05-17 13:07:51 -04:00
/* This function dumps memory usage information onto the stream connector's
2016-11-18 20:25:36 -05:00
* read buffer . It returns 0 as long as it does not complete , non - zero upon
* completion . No state is used .
*/
static int cli_io_handler_dump_pools ( struct appctx * appctx )
{
2022-11-21 03:34:02 -05:00
struct show_pools_ctx * ctx = appctx - > svcctx ;
MINOR: cli/pools: add pool name filtering capability to "show pools"
Now it becomes possible to match a pool name's prefix, for example:
$ socat - /tmp/haproxy.sock <<< "show pools match quic byusage"
Dumping pools usage. Use SIGQUIT to flush them.
- Pool quic_conn_r (65560 bytes) : 1337 allocated (87653720 bytes), ...
- Pool quic_crypto (1048 bytes) : 6685 allocated (7005880 bytes), ...
- Pool quic_conn (4056 bytes) : 1337 allocated (5422872 bytes), ...
- Pool quic_rxbuf (262168 bytes) : 8 allocated (2097344 bytes), ...
- Pool quic_connne (184 bytes) : 9359 allocated (1722056 bytes), ...
- Pool quic_frame (184 bytes) : 7938 allocated (1460592 bytes), ...
- Pool quic_tx_pac (152 bytes) : 6454 allocated (981008 bytes), ...
- Pool quic_tls_ke (56 bytes) : 12033 allocated (673848 bytes), ...
- Pool quic_rx_pac (408 bytes) : 1596 allocated (651168 bytes), ...
- Pool quic_tls_se (88 bytes) : 6685 allocated (588280 bytes), ...
- Pool quic_cstrea (88 bytes) : 4011 allocated (352968 bytes), ...
- Pool quic_tls_iv (24 bytes) : 12033 allocated (288792 bytes), ...
- Pool quic_dgram (344 bytes) : 732 allocated (251808 bytes), ...
- Pool quic_arng (56 bytes) : 4011 allocated (224616 bytes), ...
- Pool quic_conn_c (152 bytes) : 1337 allocated (203224 bytes), ...
Total: 15 pools, 109578176 bytes allocated, 109578176 used ...
In this case the reported total only concerns the dumped ones.
2022-11-21 04:02:29 -05:00
dump_pools_to_trash ( ctx - > by_what , ctx - > maxcnt , ctx - > prefix ) ;
2022-05-18 09:07:19 -04:00
if ( applet_putchk ( appctx , & trash ) = = - 1 )
2016-11-18 20:25:36 -05:00
return 0 ;
return 1 ;
}
2018-11-26 05:44:35 -05:00
/* callback used to create early pool <name> of size <size> and store the
* resulting pointer into < ptr > . If the allocation fails , it quits with after
* emitting an error message .
*/
void create_pool_callback ( struct pool_head * * ptr , char * name , unsigned int size )
{
* ptr = create_pool ( name , size , MEM_F_SHARED ) ;
if ( ! * ptr ) {
ha_alert ( " Failed to allocate pool '%s' of size %u : %s. Aborting. \n " ,
name , size , strerror ( errno ) ) ;
exit ( 1 ) ;
}
}
2018-11-26 11:09:46 -05:00
/* Initializes all per-thread arrays on startup */
static void init_pools ( )
{
2021-04-16 18:31:38 -04:00
int thr ;
2018-11-26 11:09:46 -05:00
for ( thr = 0 ; thr < MAX_THREADS ; thr + + ) {
2021-09-30 13:02:18 -04:00
LIST_INIT ( & ha_thread_ctx [ thr ] . pool_lru_head ) ;
2018-11-26 11:09:46 -05:00
}
2022-02-22 10:23:09 -05:00
2021-09-15 04:05:48 -04:00
detect_allocator ( ) ;
2018-11-26 11:09:46 -05:00
}
INITCALL0 ( STG_PREPARE , init_pools ) ;
2018-11-26 05:44:35 -05:00
2021-09-15 04:41:24 -04:00
/* Report in build options if trim is supported */
static void pools_register_build_options ( void )
{
2023-03-22 13:01:41 -04:00
if ( ! using_default_allocator ) {
2021-09-15 04:41:24 -04:00
char * ptr = NULL ;
2023-03-22 13:01:41 -04:00
memprintf ( & ptr , " Running with a replaced memory allocator (e.g. via LD_PRELOAD). " ) ;
2021-09-15 04:41:24 -04:00
hap_register_build_opts ( ptr , 1 ) ;
2023-03-22 13:01:41 -04:00
mark_tainted ( TAINTED_REPLACED_MEM_ALLOCATOR ) ;
2021-09-15 04:41:24 -04:00
}
}
INITCALL0 ( STG_REGISTER , pools_register_build_options ) ;
2016-11-18 20:25:36 -05:00
/* register cli keywords */
static struct cli_kw_list cli_kws = { { } , {
MINOR: cli/pools: add pool name filtering capability to "show pools"
Now it becomes possible to match a pool name's prefix, for example:
$ socat - /tmp/haproxy.sock <<< "show pools match quic byusage"
Dumping pools usage. Use SIGQUIT to flush them.
- Pool quic_conn_r (65560 bytes) : 1337 allocated (87653720 bytes), ...
- Pool quic_crypto (1048 bytes) : 6685 allocated (7005880 bytes), ...
- Pool quic_conn (4056 bytes) : 1337 allocated (5422872 bytes), ...
- Pool quic_rxbuf (262168 bytes) : 8 allocated (2097344 bytes), ...
- Pool quic_connne (184 bytes) : 9359 allocated (1722056 bytes), ...
- Pool quic_frame (184 bytes) : 7938 allocated (1460592 bytes), ...
- Pool quic_tx_pac (152 bytes) : 6454 allocated (981008 bytes), ...
- Pool quic_tls_ke (56 bytes) : 12033 allocated (673848 bytes), ...
- Pool quic_rx_pac (408 bytes) : 1596 allocated (651168 bytes), ...
- Pool quic_tls_se (88 bytes) : 6685 allocated (588280 bytes), ...
- Pool quic_cstrea (88 bytes) : 4011 allocated (352968 bytes), ...
- Pool quic_tls_iv (24 bytes) : 12033 allocated (288792 bytes), ...
- Pool quic_dgram (344 bytes) : 732 allocated (251808 bytes), ...
- Pool quic_arng (56 bytes) : 4011 allocated (224616 bytes), ...
- Pool quic_conn_c (152 bytes) : 1337 allocated (203224 bytes), ...
Total: 15 pools, 109578176 bytes allocated, 109578176 used ...
In this case the reported total only concerns the dumped ones.
2022-11-21 04:02:29 -05:00
{ { " 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 } ,
2016-11-18 20:25:36 -05:00
{ { } , }
} } ;
2018-11-25 13:14:37 -05:00
INITCALL1 ( STG_REGISTER , cli_register_kw , & cli_kws ) ;
2016-11-18 20:25:36 -05:00
2019-01-29 09:20:16 -05:00
/* config parser for global "tune.fail-alloc" */
static int mem_parse_global_fail_alloc ( char * * args , int section_type , struct proxy * curpx ,
2021-03-22 06:21:36 -04:00
const struct proxy * defpx , const char * file , int line ,
char * * err )
2019-01-29 09:20:16 -05:00
{
if ( too_many_args ( 1 , args , err , NULL ) )
return - 1 ;
mem_fail_rate = atoi ( args [ 1 ] ) ;
if ( mem_fail_rate < 0 | | mem_fail_rate > 100 ) {
memprintf ( err , " '%s' expects a numeric value between 0 and 100. " , args [ 0 ] ) ;
return - 1 ;
}
return 0 ;
}
2022-12-19 02:15:57 -05:00
/* config parser for global "tune.memory.hot-size" */
static int mem_parse_global_hot_size ( char * * args , int section_type , struct proxy * curpx ,
const struct proxy * defpx , const char * file , int line ,
char * * err )
{
long size ;
if ( too_many_args ( 1 , args , err , NULL ) )
return - 1 ;
size = atol ( args [ 1 ] ) ;
if ( size < = 0 ) {
memprintf ( err , " '%s' expects a strictly positive value. " , args [ 0 ] ) ;
return - 1 ;
}
global . tune . pool_cache_size = size ;
return 0 ;
}
2022-03-08 04:41:40 -05:00
/* config parser for global "no-memory-trimming" */
static int mem_parse_global_no_mem_trim ( char * * args , int section_type , struct proxy * curpx ,
const struct proxy * defpx , const char * file , int line ,
char * * err )
{
if ( too_many_args ( 0 , args , err , NULL ) )
return - 1 ;
disable_trim = 1 ;
return 0 ;
}
2019-01-29 09:20:16 -05:00
/* register global config keywords */
static struct cfg_kw_list mem_cfg_kws = { ILH , {
{ CFG_GLOBAL , " tune.fail-alloc " , mem_parse_global_fail_alloc } ,
2022-12-19 02:15:57 -05:00
{ CFG_GLOBAL , " tune.memory.hot-size " , mem_parse_global_hot_size } ,
2022-03-08 04:41:40 -05:00
{ CFG_GLOBAL , " no-memory-trimming " , mem_parse_global_no_mem_trim } ,
2019-01-29 09:20:16 -05:00
{ 0 , NULL , NULL }
} } ;
INITCALL1 ( STG_REGISTER , cfg_register_keywords , & mem_cfg_kws ) ;
2007-05-13 12:26:08 -04:00
/*
* Local variables :
* c - indent - level : 8
* c - basic - offset : 8
* End :
*/