From: Victor Julien Date: Wed, 22 Nov 2023 08:03:09 +0000 (+0100) Subject: flow: fix condition signalling X-Git-Tag: suricata-8.0.0-beta1~2034 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=3107a4953d087b80d54cfecf2f5d489e7b35ee09;p=thirdparty%2Fsuricata.git flow: fix condition signalling Signal threads while holding lock. This should make the signalling more reliable. From PTHREAD_COND(3): "Unlocking the mutex and suspending on the condition variable is done atomically. Thus, if all threads always acquire the mutex before signaling the condition, this guarantees that the condition cannot be signaled (and thus ignored) between the time a thread locks the mutex and the time it waits on the condition variable." Ticket: #6569. --- diff --git a/src/flow-manager.c b/src/flow-manager.c index e5e1aa2702..bcc1498c8b 100644 --- a/src/flow-manager.c +++ b/src/flow-manager.c @@ -84,10 +84,24 @@ SC_ATOMIC_DECLARE(uint32_t, flowrec_cnt); SC_ATOMIC_DECLARE(uint32_t, flowrec_busy); SC_ATOMIC_EXTERN(unsigned int, flow_flags); -SCCtrlCondT flow_manager_ctrl_cond; -SCCtrlMutex flow_manager_ctrl_mutex; -SCCtrlCondT flow_recycler_ctrl_cond; -SCCtrlMutex flow_recycler_ctrl_mutex; +static SCCtrlCondT flow_manager_ctrl_cond = PTHREAD_COND_INITIALIZER; +static SCCtrlMutex flow_manager_ctrl_mutex = PTHREAD_MUTEX_INITIALIZER; +static SCCtrlCondT flow_recycler_ctrl_cond = PTHREAD_COND_INITIALIZER; +static SCCtrlMutex flow_recycler_ctrl_mutex = PTHREAD_MUTEX_INITIALIZER; + +void FlowWakeupFlowManagerThread(void) +{ + SCCtrlMutexLock(&flow_manager_ctrl_mutex); + SCCtrlCondSignal(&flow_manager_ctrl_cond); + SCCtrlMutexUnlock(&flow_manager_ctrl_mutex); +} + +void FlowWakeupFlowRecyclerThread(void) +{ + SCCtrlMutexLock(&flow_recycler_ctrl_mutex); + SCCtrlCondSignal(&flow_recycler_ctrl_cond); + SCCtrlMutexUnlock(&flow_recycler_ctrl_mutex); +} void FlowTimeoutsInit(void) { @@ -942,9 +956,6 @@ void FlowManagerThreadSpawn(void) } flowmgr_number = (uint32_t)setting; - SCCtrlCondInit(&flow_manager_ctrl_cond, NULL); - SCCtrlMutexInit(&flow_manager_ctrl_mutex, NULL); - SCLogConfig("using %u flow manager threads", flowmgr_number); StatsRegisterGlobalCounter("flow.memuse", FlowGetMemuse); @@ -1148,9 +1159,6 @@ void FlowRecyclerThreadSpawn(void) } flowrec_number = (uint32_t)setting; - SCCtrlCondInit(&flow_recycler_ctrl_cond, NULL); - SCCtrlMutexInit(&flow_recycler_ctrl_mutex, NULL); - SCLogConfig("using %u flow recycler threads", flowrec_number); for (uint32_t u = 0; u < flowrec_number; u++) { diff --git a/src/flow-manager.h b/src/flow-manager.h index 157358d170..7cdd017000 100644 --- a/src/flow-manager.h +++ b/src/flow-manager.h @@ -24,13 +24,8 @@ #ifndef __FLOW_MANAGER_H__ #define __FLOW_MANAGER_H__ -/** flow manager scheduling condition */ -extern SCCtrlCondT flow_manager_ctrl_cond; -extern SCCtrlMutex flow_manager_ctrl_mutex; -#define FlowWakeupFlowManagerThread() SCCtrlCondSignal(&flow_manager_ctrl_cond) -extern SCCtrlCondT flow_recycler_ctrl_cond; -extern SCCtrlMutex flow_recycler_ctrl_mutex; -#define FlowWakeupFlowRecyclerThread() SCCtrlCondSignal(&flow_recycler_ctrl_cond) +void FlowWakeupFlowManagerThread(void); +void FlowWakeupFlowRecyclerThread(void); #define FlowTimeoutsReset() FlowTimeoutsInit() void FlowTimeoutsInit(void);