]> git.ipfire.org Git - thirdparty/suricata.git/commitdiff
threading: set min thread stack size; set it early 9380/head 9381/head
authorVictor Julien <vjulien@oisf.net>
Thu, 10 Aug 2023 14:31:29 +0000 (16:31 +0200)
committerVictor Julien <vjulien@oisf.net>
Fri, 11 Aug 2023 05:17:09 +0000 (07:17 +0200)
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: #6265.

18 files changed:
src/runmode-af-packet.c
src/runmode-af-xdp.c
src/runmode-dpdk.c
src/runmode-erf-dag.c
src/runmode-erf-file.c
src/runmode-ipfw.c
src/runmode-napatech.c
src/runmode-netmap.c
src/runmode-nflog.c
src/runmode-nfq.c
src/runmode-pcap-file.c
src/runmode-pcap.c
src/runmode-pfring.c
src/runmode-windivert.c
src/runmodes.c
src/runmodes.h
src/suricata.c
src/tests/fuzz/fuzz_decodepcapfile.c

index fd21c26293f3d88577dcb06aaab45ee919290a8c..b8ad0bfac0c39c28f683ec783e9616737a8bd436 100644 (file)
@@ -765,8 +765,6 @@ int RunModeIdsAFPAutoFp(void)
     int ret;
     const char *live_dev = NULL;
 
-    RunModeInitialize();
-
     TimeModeSetLive();
 
     (void)ConfGet("af-packet.live-interface", &live_dev);
@@ -804,7 +802,6 @@ int RunModeIdsAFPSingle(void)
     int ret;
     const char *live_dev = NULL;
 
-    RunModeInitialize();
     TimeModeSetLive();
 
     (void)ConfGet("af-packet.live-interface", &live_dev);
@@ -846,7 +843,6 @@ int RunModeIdsAFPWorkers(void)
     int ret;
     const char *live_dev = NULL;
 
-    RunModeInitialize();
     TimeModeSetLive();
 
     (void)ConfGet("af-packet.live-interface", &live_dev);
index 61d038f97c0cf5fa807d170898a82f7110ab7e0e..33a47d167aee1294034b14158b854fbd7176061a 100644 (file)
@@ -330,7 +330,6 @@ int RunModeIdsAFXDPSingle(void)
     int ret;
     const char *live_dev = NULL;
 
-    RunModeInitialize();
     TimeModeSetLive();
 
     (void)ConfGet("af-xdp.live-interface", &live_dev);
@@ -365,7 +364,6 @@ int RunModeIdsAFXDPWorkers(void)
     int ret;
     const char *live_dev = NULL;
 
-    RunModeInitialize();
     TimeModeSetLive();
 
     (void)ConfGet("af-xdp.live-interface", &live_dev);
index 5a934e8b9ed5ffb93147ab3845f099854ae30656..feba401b4130b2eea18f48f67a384abe5e8ac8a1 100644 (file)
@@ -1649,7 +1649,6 @@ int RunModeIdsDpdkWorkers(void)
 #ifdef HAVE_DPDK
     int ret;
 
-    RunModeInitialize();
     TimeModeSetLive();
 
     InitEal();
index f2fafbb3c3e2a5e3a2eda2501650cacf361e349b..503d1a15562ab0b8e92a6faf3e13d54f06ac9e76 100644 (file)
@@ -72,8 +72,6 @@ int RunModeIdsErfDagSingle(void)
 
     SCEnter();
 
-    RunModeInitialize();
-
     TimeModeSetLive();
 
     ret = RunModeSetLiveCaptureSingle(ParseDagConfig,
@@ -97,8 +95,6 @@ int RunModeIdsErfDagAutoFp(void)
 
     SCEnter();
 
-    RunModeInitialize();
-
     TimeModeSetLive();
 
     ret = RunModeSetLiveCaptureAutoFp(ParseDagConfig, DagConfigGetThreadCount, "ReceiveErfDag",
@@ -118,8 +114,6 @@ int RunModeIdsErfDagWorkers(void)
 
     SCEnter();
 
-    RunModeInitialize();
-
     TimeModeSetLive();
 
     ret = RunModeSetLiveCaptureWorkers(ParseDagConfig, DagConfigGetThreadCount, "ReceiveErfDag",
index ac7386871a3b6716934f8aa98ffae6389f392b1e..dc09509d041141b87c1dd30ccaa2b1072802f385 100644 (file)
@@ -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. */
@@ -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");
index 1aae3e13b4df5f0ea3a985d6c4c9fe4d8f4508ce..de72f0623d064ebf67d905d284226ca097a6c4ca 100644 (file)
@@ -63,8 +63,6 @@ int RunModeIpsIPFWAutoFp(void)
     int ret = 0;
 #ifdef IPFW
 
-    RunModeInitialize();
-
     TimeModeSetLive();
 
     LiveDeviceHasNoStats();
@@ -83,8 +81,6 @@ int RunModeIpsIPFWWorker(void)
     int ret = 0;
 #ifdef IPFW
 
-    RunModeInitialize();
-
     TimeModeSetLive();
 
     LiveDeviceHasNoStats();
index 912126b9feaba380e337d9691c7df2bc96bf5f34..cb8f560ea35098d0c411e56f4cdc1b2e7e051368 100644 (file)
@@ -216,7 +216,6 @@ static int NapatechInit(int runmode)
 {
     int status;
 
-    RunModeInitialize();
     TimeModeSetLive();
 
     /* Initialize the API and check version compatibility */
index 5110a820eef93502b82084288b9a0fcb8c7459d2..927dc718856c65ac54e63cb0d3a3bee31e2a9571 100644 (file)
@@ -399,7 +399,6 @@ static int NetmapRunModeInit(NetmapRunMode_t runmode)
 {
     SCEnter();
 
-    RunModeInitialize();
     TimeModeSetLive();
 
     const char *live_dev = NULL;
index f744eae5909c0e0245d50d1b0e095e1cd6a1ef75..28b9ae5efd648e75b1f4e8c5d28af42222d6ebd8 100644 (file)
@@ -153,7 +153,6 @@ static int RunModeIdsNflogAutoFp(void)
     SCEnter();
 
 #ifdef HAVE_NFLOG
-    RunModeInitialize();
     TimeModeSetLive();
 
     int ret = RunModeSetLiveCaptureAutoFp(ParseNflogConfig, NflogConfigGeThreadsCount,
@@ -173,7 +172,6 @@ static int RunModeIdsNflogSingle(void)
     SCEnter();
 
 #ifdef HAVE_NFLOG
-    RunModeInitialize();
     TimeModeSetLive();
 
     int ret = RunModeSetLiveCaptureSingle(ParseNflogConfig, NflogConfigGeThreadsCount,
@@ -193,7 +191,6 @@ static int RunModeIdsNflogWorkers(void)
     SCEnter();
 
 #ifdef HAVE_NFLOG
-    RunModeInitialize();
     TimeModeSetLive();
 
     int ret = RunModeSetLiveCaptureWorkers(ParseNflogConfig, NflogConfigGeThreadsCount,
index f20e05a3cc18206d6f0de531d4fe521c4fa1ab83..d9ea93c746f3c0f0126580c6002ec733fbec24f7 100644 (file)
@@ -60,8 +60,6 @@ int RunModeIpsNFQAutoFp(void)
     int ret = 0;
 #ifdef NFQ
 
-    RunModeInitialize();
-
     TimeModeSetLive();
 
     LiveDeviceHasNoStats();
@@ -80,8 +78,6 @@ int RunModeIpsNFQWorker(void)
     int ret = 0;
 #ifdef NFQ
 
-    RunModeInitialize();
-
     TimeModeSetLive();
 
     LiveDeviceHasNoStats();
index 660c5304114e8b0bb613cf3e69af08f046b85038..e03592514fee435df041637183d7b7b0c47eaa59 100644 (file)
@@ -64,7 +64,6 @@ int RunModeFilePcapSingle(void)
         FatalError("Failed retrieving pcap-file from Conf");
     }
 
-    RunModeInitialize();
     TimeModeSetOffline();
 
     PcapFileGlobalInit();
@@ -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");
index d9f0967ccb9f924ba5b42ee1f04c6fcad7508786..bfefe3ec0e4133ef60fee175cace7b810e9407b5 100644 (file)
@@ -235,7 +235,6 @@ int RunModeIdsPcapSingle(void)
 
     SCEnter();
 
-    RunModeInitialize();
     TimeModeSetLive();
 
     (void)ConfGet("pcap.single-pcap-dev", &live_dev);
@@ -275,7 +274,6 @@ int RunModeIdsPcapAutoFp(void)
     const char *live_dev = NULL;
 
     SCEnter();
-    RunModeInitialize();
     TimeModeSetLive();
 
     (void) ConfGet("pcap.single-pcap-dev", &live_dev);
@@ -303,7 +301,6 @@ int RunModeIdsPcapWorkers(void)
     const char *live_dev = NULL;
     SCEnter();
 
-    RunModeInitialize();
     TimeModeSetLive();
 
     (void) ConfGet("pcap.single-pcap-dev", &live_dev);
index 7580fdd6cc14bb0c91b0e659058352b833642224..b0af83b4bfc25eef0369a1892aacec2c45d4edeb 100644 (file)
@@ -450,8 +450,6 @@ int RunModeIdsPfringAutoFp(void)
     const char *live_dev = NULL;
     ConfigIfaceParserFunc tparser;
 
-    RunModeInitialize();
-
     TimeModeSetLive();
 
     ret = GetDevAndParser(&live_dev, &tparser);
@@ -481,8 +479,6 @@ int RunModeIdsPfringSingle(void)
     const char *live_dev = NULL;
     ConfigIfaceParserFunc tparser;
 
-    RunModeInitialize();
-
     TimeModeSetLive();
 
     ret = GetDevAndParser(&live_dev, &tparser);
@@ -515,8 +511,6 @@ int RunModeIdsPfringWorkers(void)
     const char *live_dev = NULL;
     ConfigIfaceParserFunc tparser;
 
-    RunModeInitialize();
-
     TimeModeSetLive();
 
     ret = GetDevAndParser(&live_dev, &tparser);
index 7c2ccfe46a8e9d30d6e05ade2b5ebc84db289953..a4514270e9646fb177ab786679d32f1c215da4f7 100644 (file)
@@ -54,8 +54,6 @@ int RunModeIpsWinDivertAutoFp(void)
     SCEnter();
     int ret = 0;
 #ifdef WINDIVERT
-    RunModeInitialize();
-
     TimeModeSetLive();
 
     LiveDeviceHasNoStats();
index 5362f4caf684eed176b8c760cbf296b84be27257..348adfa25d43c968bff60aef56fc4d091e4c8f90 100644 (file)
@@ -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) {
@@ -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);
index afa3f8d8f0bf983ddd0a42cd063875c7bc55ac51..57b31b6d405493d5c2bb484ef31bb49d0966fc82 100644 (file)
@@ -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);
 
index 1fdc041e9a71253a012d6a43a496606ca7c96a88..7fe469acf24d04061acf63947fe576d35b7b8e43 100644 (file)
@@ -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");
index dbe042206f27a58162182ba999741e5f1fdd1dc4..cf508697f05bbc159d60c5903e0708449b87f50e 100644 (file)
@@ -54,7 +54,7 @@ int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
 
         PostConfLoadedSetup(&surifuzz);
 
-        RunModeInitialize();
+        RunModeInitializeThreadSettings();
         TimeModeSetOffline();
         PcapFileGlobalInit();