From: Victor Julien Date: Wed, 18 Sep 2024 08:48:29 +0000 (+0200) Subject: threads: seal after setup; unseal at shutdown X-Git-Tag: suricata-8.0.0-beta1~586 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=b75d1ab37f9c2ff413ffceac89484e0c7b134b40;p=thirdparty%2Fsuricata.git threads: seal after setup; unseal at shutdown The idea of sealing the thread store is that its members can be accessed w/o holding a lock to the whole store at runtime. --- diff --git a/src/runmodes.c b/src/runmodes.c index 006199bb94..cd5eed32b5 100644 --- a/src/runmodes.c +++ b/src/runmodes.c @@ -436,6 +436,7 @@ void RunModeDispatch(int runmode, const char *custom_mode, const char *capture_p BypassedFlowManagerThreadSpawn(); } StatsSpawnThreads(); + TmThreadsSealThreads(); } } diff --git a/src/suricata.c b/src/suricata.c index 12224e64b6..7c238c48cc 100644 --- a/src/suricata.c +++ b/src/suricata.c @@ -2258,6 +2258,8 @@ void PostRunDeinit(const int runmode, struct timeval *start_time) if (runmode == RUNMODE_UNIX_SOCKET) return; + TmThreadsUnsealThreads(); + /* needed by FlowWorkToDoCleanup */ PacketPoolInit(); diff --git a/src/tm-threads.c b/src/tm-threads.c index 84ae12513e..42200dbd43 100644 --- a/src/tm-threads.c +++ b/src/tm-threads.c @@ -1,4 +1,4 @@ -/* Copyright (C) 2007-2022 Open Information Security Foundation +/* Copyright (C) 2007-2024 Open Information Security Foundation * * You can copy, redistribute or modify this Program under the terms of * the GNU General Public License version 2 as published by the Free @@ -2083,9 +2083,26 @@ typedef struct Threads_ { int threads_cnt; } Threads; +static bool thread_store_sealed = false; static Threads thread_store = { NULL, 0, 0 }; static SCMutex thread_store_lock = SCMUTEX_INITIALIZER; +void TmThreadsSealThreads(void) +{ + SCMutexLock(&thread_store_lock); + DEBUG_VALIDATE_BUG_ON(thread_store_sealed); + thread_store_sealed = true; + SCMutexUnlock(&thread_store_lock); +} + +void TmThreadsUnsealThreads(void) +{ + SCMutexLock(&thread_store_lock); + DEBUG_VALIDATE_BUG_ON(!thread_store_sealed); + thread_store_sealed = false; + SCMutexUnlock(&thread_store_lock); +} + void TmThreadsListThreads(void) { SCMutexLock(&thread_store_lock); @@ -2113,6 +2130,7 @@ void TmThreadsListThreads(void) int TmThreadsRegisterThread(ThreadVars *tv, const int type) { SCMutexLock(&thread_store_lock); + DEBUG_VALIDATE_BUG_ON(thread_store_sealed); if (thread_store.threads == NULL) { thread_store.threads = SCCalloc(STEP, sizeof(Thread)); BUG_ON(thread_store.threads == NULL); @@ -2162,6 +2180,7 @@ int TmThreadsRegisterThread(ThreadVars *tv, const int type) void TmThreadsUnregisterThread(const int id) { SCMutexLock(&thread_store_lock); + DEBUG_VALIDATE_BUG_ON(thread_store_sealed); if (id <= 0 || id > (int)thread_store.threads_size) { SCMutexUnlock(&thread_store_lock); return; diff --git a/src/tm-threads.h b/src/tm-threads.h index 1e9d43c218..dde0f2029e 100644 --- a/src/tm-threads.h +++ b/src/tm-threads.h @@ -1,4 +1,4 @@ -/* Copyright (C) 2007-2011 Open Information Security Foundation +/* Copyright (C) 2007-2024 Open Information Security Foundation * * You can copy, redistribute or modify this Program under the terms of * the GNU General Public License version 2 as published by the Free @@ -276,6 +276,8 @@ static inline void TmThreadsCaptureBreakLoop(ThreadVars *tv) } } +void TmThreadsSealThreads(void); +void TmThreadsUnsealThreads(void); void TmThreadsListThreads(void); int TmThreadsRegisterThread(ThreadVars *tv, const int type); void TmThreadsUnregisterThread(const int id);