Skip to content

Commit

Permalink
threading: set min thread stack size; set it early
Browse files Browse the repository at this point in the history
Multi-tenancy uses loader threads that initialize detection engines. During
this, esp the AC family of MPM implementations, there is significant stack
usage. In most OS' threads have a lower stack size by default. In Linux, when
using the Musl C library, a thread by default gets 128KiB.

This patch does 2 things:

1. it centralizes the handling of the `threading.stack-size`. It it is not
   longer handled by the runmodes, but called from the global initialization
   logic.

2. it sets a minimum per thread stack size of 512k, unless `threading.stack-size`
   is set.

Ticket: OISF#6265.
  • Loading branch information
victorjulien committed Aug 11, 2023
1 parent 92fce2f commit becb8ce
Show file tree
Hide file tree
Showing 18 changed files with 13 additions and 47 deletions.
4 changes: 0 additions & 4 deletions src/runmode-af-packet.c
Original file line number Diff line number Diff line change
Expand Up @@ -765,8 +765,6 @@ int RunModeIdsAFPAutoFp(void)
int ret;
const char *live_dev = NULL;

RunModeInitialize();

TimeModeSetLive();

(void)ConfGet("af-packet.live-interface", &live_dev);
Expand Down Expand Up @@ -804,7 +802,6 @@ int RunModeIdsAFPSingle(void)
int ret;
const char *live_dev = NULL;

RunModeInitialize();
TimeModeSetLive();

(void)ConfGet("af-packet.live-interface", &live_dev);
Expand Down Expand Up @@ -846,7 +843,6 @@ int RunModeIdsAFPWorkers(void)
int ret;
const char *live_dev = NULL;

RunModeInitialize();
TimeModeSetLive();

(void)ConfGet("af-packet.live-interface", &live_dev);
Expand Down
2 changes: 0 additions & 2 deletions src/runmode-af-xdp.c
Original file line number Diff line number Diff line change
Expand Up @@ -330,7 +330,6 @@ int RunModeIdsAFXDPSingle(void)
int ret;
const char *live_dev = NULL;

RunModeInitialize();
TimeModeSetLive();

(void)ConfGet("af-xdp.live-interface", &live_dev);
Expand Down Expand Up @@ -365,7 +364,6 @@ int RunModeIdsAFXDPWorkers(void)
int ret;
const char *live_dev = NULL;

RunModeInitialize();
TimeModeSetLive();

(void)ConfGet("af-xdp.live-interface", &live_dev);
Expand Down
1 change: 0 additions & 1 deletion src/runmode-dpdk.c
Original file line number Diff line number Diff line change
Expand Up @@ -1649,7 +1649,6 @@ int RunModeIdsDpdkWorkers(void)
#ifdef HAVE_DPDK
int ret;

RunModeInitialize();
TimeModeSetLive();

InitEal();
Expand Down
6 changes: 0 additions & 6 deletions src/runmode-erf-dag.c
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,6 @@ int RunModeIdsErfDagSingle(void)

SCEnter();

RunModeInitialize();

TimeModeSetLive();

ret = RunModeSetLiveCaptureSingle(ParseDagConfig,
Expand All @@ -97,8 +95,6 @@ int RunModeIdsErfDagAutoFp(void)

SCEnter();

RunModeInitialize();

TimeModeSetLive();

ret = RunModeSetLiveCaptureAutoFp(ParseDagConfig, DagConfigGetThreadCount, "ReceiveErfDag",
Expand All @@ -118,8 +114,6 @@ int RunModeIdsErfDagWorkers(void)

SCEnter();

RunModeInitialize();

TimeModeSetLive();

ret = RunModeSetLiveCaptureWorkers(ParseDagConfig, DagConfigGetThreadCount, "ReceiveErfDag",
Expand Down
4 changes: 0 additions & 4 deletions src/runmode-erf-file.c
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,6 @@ int RunModeErfFileSingle(void)
FatalError("Failed to get erf-file.file from config.");
}

RunModeInitialize();

TimeModeSetOffline();

/* Basically the same setup as PCAP files. */
Expand Down Expand Up @@ -113,8 +111,6 @@ int RunModeErfFileAutoFp(void)
char *queues = NULL;
uint16_t thread;

RunModeInitialize();

const char *file = NULL;
if (ConfGet("erf-file.file", &file) == 0) {
FatalError("Failed retrieving erf-file.file from config");
Expand Down
4 changes: 0 additions & 4 deletions src/runmode-ipfw.c
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,6 @@ int RunModeIpsIPFWAutoFp(void)
int ret = 0;
#ifdef IPFW

RunModeInitialize();

TimeModeSetLive();

LiveDeviceHasNoStats();
Expand All @@ -83,8 +81,6 @@ int RunModeIpsIPFWWorker(void)
int ret = 0;
#ifdef IPFW

RunModeInitialize();

TimeModeSetLive();

LiveDeviceHasNoStats();
Expand Down
1 change: 0 additions & 1 deletion src/runmode-napatech.c
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,6 @@ static int NapatechInit(int runmode)
{
int status;

RunModeInitialize();
TimeModeSetLive();

/* Initialize the API and check version compatibility */
Expand Down
1 change: 0 additions & 1 deletion src/runmode-netmap.c
Original file line number Diff line number Diff line change
Expand Up @@ -399,7 +399,6 @@ static int NetmapRunModeInit(NetmapRunMode_t runmode)
{
SCEnter();

RunModeInitialize();
TimeModeSetLive();

const char *live_dev = NULL;
Expand Down
3 changes: 0 additions & 3 deletions src/runmode-nflog.c
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,6 @@ static int RunModeIdsNflogAutoFp(void)
SCEnter();

#ifdef HAVE_NFLOG
RunModeInitialize();
TimeModeSetLive();

int ret = RunModeSetLiveCaptureAutoFp(ParseNflogConfig, NflogConfigGeThreadsCount,
Expand All @@ -173,7 +172,6 @@ static int RunModeIdsNflogSingle(void)
SCEnter();

#ifdef HAVE_NFLOG
RunModeInitialize();
TimeModeSetLive();

int ret = RunModeSetLiveCaptureSingle(ParseNflogConfig, NflogConfigGeThreadsCount,
Expand All @@ -193,7 +191,6 @@ static int RunModeIdsNflogWorkers(void)
SCEnter();

#ifdef HAVE_NFLOG
RunModeInitialize();
TimeModeSetLive();

int ret = RunModeSetLiveCaptureWorkers(ParseNflogConfig, NflogConfigGeThreadsCount,
Expand Down
4 changes: 0 additions & 4 deletions src/runmode-nfq.c
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,6 @@ int RunModeIpsNFQAutoFp(void)
int ret = 0;
#ifdef NFQ

RunModeInitialize();

TimeModeSetLive();

LiveDeviceHasNoStats();
Expand All @@ -80,8 +78,6 @@ int RunModeIpsNFQWorker(void)
int ret = 0;
#ifdef NFQ

RunModeInitialize();

TimeModeSetLive();

LiveDeviceHasNoStats();
Expand Down
3 changes: 0 additions & 3 deletions src/runmode-pcap-file.c
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,6 @@ int RunModeFilePcapSingle(void)
FatalError("Failed retrieving pcap-file from Conf");
}

RunModeInitialize();
TimeModeSetOffline();

PcapFileGlobalInit();
Expand Down Expand Up @@ -130,8 +129,6 @@ int RunModeFilePcapAutoFp(void)
char *queues = NULL;
uint16_t thread;

RunModeInitialize();

const char *file = NULL;
if (ConfGet("pcap-file.file", &file) == 0) {
FatalError("Failed retrieving pcap-file from Conf");
Expand Down
3 changes: 0 additions & 3 deletions src/runmode-pcap.c
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,6 @@ int RunModeIdsPcapSingle(void)

SCEnter();

RunModeInitialize();
TimeModeSetLive();

(void)ConfGet("pcap.single-pcap-dev", &live_dev);
Expand Down Expand Up @@ -275,7 +274,6 @@ int RunModeIdsPcapAutoFp(void)
const char *live_dev = NULL;

SCEnter();
RunModeInitialize();
TimeModeSetLive();

(void) ConfGet("pcap.single-pcap-dev", &live_dev);
Expand Down Expand Up @@ -303,7 +301,6 @@ int RunModeIdsPcapWorkers(void)
const char *live_dev = NULL;
SCEnter();

RunModeInitialize();
TimeModeSetLive();

(void) ConfGet("pcap.single-pcap-dev", &live_dev);
Expand Down
6 changes: 0 additions & 6 deletions src/runmode-pfring.c
Original file line number Diff line number Diff line change
Expand Up @@ -450,8 +450,6 @@ int RunModeIdsPfringAutoFp(void)
const char *live_dev = NULL;
ConfigIfaceParserFunc tparser;

RunModeInitialize();

TimeModeSetLive();

ret = GetDevAndParser(&live_dev, &tparser);
Expand Down Expand Up @@ -481,8 +479,6 @@ int RunModeIdsPfringSingle(void)
const char *live_dev = NULL;
ConfigIfaceParserFunc tparser;

RunModeInitialize();

TimeModeSetLive();

ret = GetDevAndParser(&live_dev, &tparser);
Expand Down Expand Up @@ -515,8 +511,6 @@ int RunModeIdsPfringWorkers(void)
const char *live_dev = NULL;
ConfigIfaceParserFunc tparser;

RunModeInitialize();

TimeModeSetLive();

ret = GetDevAndParser(&live_dev, &tparser);
Expand Down
2 changes: 0 additions & 2 deletions src/runmode-windivert.c
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,6 @@ int RunModeIpsWinDivertAutoFp(void)
SCEnter();
int ret = 0;
#ifdef WINDIVERT
RunModeInitialize();

TimeModeSetLive();

LiveDeviceHasNoStats();
Expand Down
11 changes: 10 additions & 1 deletion src/runmodes.c
Original file line number Diff line number Diff line change
Expand Up @@ -980,7 +980,7 @@ float threading_detect_ratio = 1;
/**
* Initialize multithreading settings.
*/
void RunModeInitialize(void)
void RunModeInitializeThreadSettings(void)
{
threading_set_cpu_affinity = FALSE;
if ((ConfGetBool("threading.set-cpu-affinity", &threading_set_cpu_affinity)) == 0) {
Expand Down Expand Up @@ -1009,6 +1009,15 @@ void RunModeInitialize(void)
FatalError("Failed to initialize thread_stack_size output, invalid limit: %s", ss);
}
}
} else {
pthread_attr_t attr;
pthread_attr_init(&attr);
size_t size;
if (pthread_attr_getstacksize(&attr, &size) == 0 && size < 512 * 1024) {
threading_set_stack_size = 512 * 1024;
SCLogNotice("thread stack size of %" PRIuMAX " to too small: setting to 512k",
(uintmax_t)size);
}
}

SCLogDebug("threading.stack-size %" PRIu64, threading_set_stack_size);
Expand Down
2 changes: 1 addition & 1 deletion src/runmodes.h
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ void RunModeDispatch(int, const char *, const char *capture_plugin_name, const c
void RunModeRegisterRunModes(void);
void RunModeRegisterNewRunMode(enum RunModes, const char *, const char *, int (*RunModeFunc)(void),
void (*RunModeIsIPSEnabled)(void));
void RunModeInitialize(void);
void RunModeInitializeThreadSettings(void);
void RunModeInitializeOutputs(void);
void RunModeShutDown(void);

Expand Down
1 change: 1 addition & 0 deletions src/suricata.c
Original file line number Diff line number Diff line change
Expand Up @@ -2941,6 +2941,7 @@ int SuricataMain(int argc, char **argv)

LogVersion(&suricata);
UtilCpuPrintSummary();
RunModeInitializeThreadSettings();

if (suricata.run_mode == RUNMODE_CONF_TEST)
SCLogInfo("Running suricata under test mode");
Expand Down
2 changes: 1 addition & 1 deletion src/tests/fuzz/fuzz_decodepcapfile.c
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)

PostConfLoadedSetup(&surifuzz);

RunModeInitialize();
RunModeInitializeThreadSettings();
TimeModeSetOffline();
PcapFileGlobalInit();

Expand Down

0 comments on commit becb8ce

Please sign in to comment.