]> git.ipfire.org Git - thirdparty/suricata.git/commitdiff
thash/memcap: Use atomics for memcap
authorJeff Lucovsky <jlucovsky@oisf.net>
Sun, 27 Oct 2024 13:53:31 +0000 (09:53 -0400)
committerVictor Julien <victor@inliniac.net>
Wed, 13 Nov 2024 09:53:58 +0000 (10:53 +0100)
Issue: 845

Maintain the memcap as an atomic counter so changes through the
unix-socket interface can be supported.

src/util-thash.c
src/util-thash.h

index 3787454a37f4442f5c944aa5ffb8c81c4676f56c..be6e930bcc5f60454ceb171c7468fa31bfe58b2d 100644 (file)
@@ -228,12 +228,15 @@ static int THashInitConfig(THashTableContext *ctx, const char *cnf_prefix)
     GET_VAR(cnf_prefix, "memcap");
     if ((ConfGet(varname, &conf_val)) == 1)
     {
-        if (ParseSizeStringU64(conf_val, &ctx->config.memcap) < 0) {
+        uint64_t memcap;
+        if (ParseSizeStringU64(conf_val, &memcap) < 0) {
             SCLogError("Error parsing %s "
                        "from conf file - %s.  Killing engine",
                     varname, conf_val);
             return -1;
         }
+        SC_ATOMIC_INIT(ctx->config.memcap);
+        SC_ATOMIC_SET(ctx->config.memcap, memcap);
     }
     GET_VAR(cnf_prefix, "hash-size");
     if ((ConfGet(varname, &conf_val)) == 1)
@@ -261,7 +264,7 @@ static int THashInitConfig(THashTableContext *ctx, const char *cnf_prefix)
                    "Memcap: %" PRIu64 ", Hash table size %" PRIu64 ". Calculate "
                    "total hash size by multiplying \"hash-size\" with %" PRIuMAX ", "
                    "which is the hash bucket size.",
-                ctx->config.memcap, hash_size, (uintmax_t)sizeof(THashHashRow));
+                SC_ATOMIC_GET(ctx->config.memcap), hash_size, (uintmax_t)sizeof(THashHashRow));
         return -1;
     }
     ctx->array = SCMallocAligned(ctx->config.hash_size * sizeof(THashHashRow), CLS);
@@ -283,7 +286,7 @@ static int THashInitConfig(THashTableContext *ctx, const char *cnf_prefix)
             SCLogError("preallocating data failed: "
                        "max thash memcap reached. Memcap %" PRIu64 ", "
                        "Memuse %" PRIu64 ".",
-                    ctx->config.memcap,
+                    SC_ATOMIC_GET(ctx->config.memcap),
                     ((uint64_t)SC_ATOMIC_GET(ctx->memuse) + THASH_DATA_SIZE(ctx)));
             return -1;
         }
@@ -323,12 +326,12 @@ THashTableContext *THashInit(const char *cnf_prefix, size_t data_size,
      unless defined by the rule keyword */
 #ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
     // limit memcap size to default when fuzzing
-    ctx->config.memcap = THASH_DEFAULT_MEMCAP;
+    SC_ATOMIC_SET(ctx->config.memcap, THASH_DEFAULT_MEMCAP);
 #else
     if (memcap > 0) {
-        ctx->config.memcap = memcap;
+        SC_ATOMIC_SET(ctx->config.memcap, memcap);
     } else {
-        ctx->config.memcap = reset_memcap ? UINT64_MAX : THASH_DEFAULT_MEMCAP;
+        SC_ATOMIC_SET(ctx->config.memcap, reset_memcap ? UINT64_MAX : THASH_DEFAULT_MEMCAP);
     }
 #endif
     ctx->config.prealloc = THASH_DEFAULT_PREALLOC;
@@ -349,8 +352,9 @@ THashTableContext *THashInit(const char *cnf_prefix, size_t data_size,
  * */
 void THashConsolidateMemcap(THashTableContext *ctx)
 {
-    ctx->config.memcap = MAX(SC_ATOMIC_GET(ctx->memuse), ctx->config.memcap);
-    SCLogDebug("memcap after load set to: %" PRIu64, ctx->config.memcap);
+    SC_ATOMIC_SET(
+            ctx->config.memcap, MAX(SC_ATOMIC_GET(ctx->memuse), SC_ATOMIC_GET(ctx->config.memcap)));
+    SCLogDebug("memcap after load set to: %" PRIu64, SC_ATOMIC_GET(ctx->config.memcap));
 }
 
 /** \brief shutdown the flow engine
@@ -598,7 +602,7 @@ static THashData *THashDataGetNew(THashTableContext *ctx, void *data)
                     SC_ATOMIC_SET(ctx->memcap_reached, true);
                 }
                 SCLogError("Adding data will exceed memcap: %" PRIu64 ", current memuse: %" PRIu64,
-                        (ctx)->config.memcap, SC_ATOMIC_GET(ctx->memuse));
+                        SC_ATOMIC_GET((ctx)->config.memcap), SC_ATOMIC_GET(ctx->memuse));
             }
         }
     }
index 803a5f477c3c490be33ae937628f0826925afdbe..5d4a61f10b12d90cd409c89bf8092a204919e389 100644 (file)
@@ -122,7 +122,7 @@ typedef int (*THashOutputFunc)(void *output_ctx, const uint8_t *data, const uint
 typedef int (*THashFormatFunc)(const void *in_data, char *output, size_t output_size);
 
 typedef struct THashDataConfig_ {
-    uint64_t memcap;
+    SC_ATOMIC_DECLARE(uint64_t, memcap);
     uint32_t hash_rand;
     uint32_t hash_size;
     uint32_t prealloc;
@@ -161,8 +161,9 @@ typedef struct THashTableContext_ {
  *  \retval 1 it fits
  *  \retval 0 no fit
  */
-#define THASH_CHECK_MEMCAP(ctx, size) \
-    ((((uint64_t)SC_ATOMIC_GET((ctx)->memuse) + (uint64_t)(size)) <= (ctx)->config.memcap))
+#define THASH_CHECK_MEMCAP(ctx, size)                                                              \
+    ((((uint64_t)SC_ATOMIC_GET((ctx)->memuse) + (uint64_t)(size)) <=                               \
+            SC_ATOMIC_GET((ctx)->config.memcap)))
 
 #define THashIncrUsecnt(h) \
     (void)SC_ATOMIC_ADD((h)->use_cnt, 1)