]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
BUG/MINOR: illegal use of the malloc_trim() function if jemalloc is used
authorMiroslav Zagorac <mzagorac@haproxy.com>
Wed, 22 Mar 2023 11:52:19 +0000 (12:52 +0100)
committerWilly Tarreau <w@1wt.eu>
Wed, 22 Mar 2023 13:14:50 +0000 (14:14 +0100)
In the event that HAProxy is linked with the jemalloc library, it is still
shown that malloc_trim() is enabled when executing "haproxy -vv":
  ..
  Support for malloc_trim() is enabled.
  ..

It's not so much a problem as it is that malloc_trim() is called in the
pat_ref_purge_range() function without any checking.

This was solved by setting the using_default_allocator variable to the
correct value in the detect_allocator() function and before calling
malloc_trim() it is checked whether the function should be called.

include/haproxy/pool.h
src/pattern.c
src/pool.c

index 16146604b2cb9515e7fee970f51063860b119a4e..a12990d2458246827d00056c5b9bb36741e8a90a 100644 (file)
@@ -101,6 +101,7 @@ extern int mem_poison_byte;
 /* set of POOL_DBG_* flags */
 extern uint pool_debugging;
 
+int is_trim_enabled(void);
 void *pool_get_from_os(struct pool_head *pool);
 void pool_put_to_os(struct pool_head *pool, void *ptr);
 void *pool_alloc_nocache(struct pool_head *pool);
index a2557dea1bd6ce2b32887a91df70c54bf281443d..71f25a43b35fe3f8162143c1c436b8fa20ab7abc 100644 (file)
@@ -2084,7 +2084,7 @@ int pat_ref_purge_range(struct pat_ref *ref, uint from, uint to, int budget)
                HA_RWLOCK_WRUNLOCK(PATEXP_LOCK, &expr->lock);
 
 #if defined(HA_HAVE_MALLOC_TRIM)
-       if (done) {
+       if (done && is_trim_enabled()) {
                malloc_trim(0);
        }
 #endif
index 345681a0c33f6c5301068f3bdea94c95a1f4befb..214d950fb7beed7fe6e31726b150caf6c04eaacf 100644 (file)
@@ -177,11 +177,10 @@ static void detect_allocator(void)
 
        my_mallctl = mallctl;
 #endif
-
-       if (!my_mallctl) {
+       if (!my_mallctl)
                my_mallctl = get_sym_curr_addr("mallctl");
-               using_default_allocator = (my_mallctl == NULL);
-       }
+
+       using_default_allocator = (my_mallctl == NULL);
 
        if (!my_mallctl) {
 #if defined(HA_HAVE_MALLOC_TRIM)
@@ -212,7 +211,7 @@ static void detect_allocator(void)
        }
 }
 
-static int is_trim_enabled(void)
+int is_trim_enabled(void)
 {
        return using_default_allocator;
 }