From: Jeff Lucovsky Date: Sun, 27 Oct 2024 13:53:31 +0000 (-0400) Subject: thash/memcap: Use atomics for memcap X-Git-Tag: suricata-8.0.0-beta1~714 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=83bdcda932b369da2d82857168b27442b8a8fbbf;p=thirdparty%2Fsuricata.git thash/memcap: Use atomics for memcap Issue: 845 Maintain the memcap as an atomic counter so changes through the unix-socket interface can be supported. --- diff --git a/src/util-thash.c b/src/util-thash.c index 3787454a37..be6e930bcc 100644 --- a/src/util-thash.c +++ b/src/util-thash.c @@ -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)); } } } diff --git a/src/util-thash.h b/src/util-thash.h index 803a5f477c..5d4a61f10b 100644 --- a/src/util-thash.h +++ b/src/util-thash.h @@ -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)