diff --git a/src/suricata.c b/src/suricata.c index aebcecc3de..4fa1ef4e25 100644 --- a/src/suricata.c +++ b/src/suricata.c @@ -97,6 +97,7 @@ /* holds the cuda b2g module */ #include "util-mpm-b2g-cuda.h" +#include "util-cuda-handlers.h" #include "output.h" @@ -389,8 +390,10 @@ int main(int argc, char **argv) /* Initialize the configuration module. */ ConfInit(); +#ifdef __SC_CUDA_SUPPORT__ /* Init the CUDA environment */ SCCudaInitCudaEnvironment(); +#endif struct option long_opts[] = { {"dump-config", 0, &dump_config, 1}, @@ -731,6 +734,15 @@ int main(int argc, char **argv) else { uint32_t failed = UtRunTests(regex_arg); UtCleanup(); +#ifdef __SC_CUDA_SUPPORT__ + /* need this in case any of the cuda dispatcher threads are still + * running, kill them, so that we can free the cuda contexts. We + * need to free those cuda contexts so that next when we call + * deregister functions, we will need to attach to those contexts + * the contexts and its associated data */ + TmThreadKillThreads(); + SCCudaHlDeRegisterAllRegisteredModules(); +#endif if (failed) { exit(EXIT_FAILURE); } @@ -910,5 +922,13 @@ int main(int argc, char **argv) RunModeShutDown(); OutputDeregisterAll(); +#ifdef __SC_CUDA_SUPPORT__ + /* all cuda contexts attached to any threads should be free by now. + * if any host_thread is still attached to any cuda_context, they need + * to pop them by the time we reach here, if they aren't using those + * cuda contexts in any way */ + SCCudaHlDeRegisterAllRegisteredModules(); +#endif + exit(EXIT_SUCCESS); } diff --git a/src/util-cuda-handlers.c b/src/util-cuda-handlers.c index e9781bb280..8ea1965b23 100644 --- a/src/util-cuda-handlers.c +++ b/src/util-cuda-handlers.c @@ -514,6 +514,11 @@ int SCCudaHlDeRegisterModule(const char *name) return -1; } + /* the applicationg must take care to check that the following cuda context + * which is being freed is floating(not attached to any host thread) */ + if (data->cuda_context != 0) + SCCudaCtxPushCurrent(data->cuda_context); + /* looks like we do have a module registered by this name */ /* first clean the cuda device pointers */ device_ptr = data->device_ptrs; @@ -527,6 +532,9 @@ int SCCudaHlDeRegisterModule(const char *name) } data->device_ptrs = NULL; + if (data->name != NULL) + free((void *)data->name); + /* clean the dispatcher function registered */ data->SCCudaHlDispFunc = NULL; @@ -561,6 +569,29 @@ int SCCudaHlDeRegisterModule(const char *name) return -1; } +/** + * \brief DeRegister all the modules registered under cuda handlers. + */ +void SCCudaHlDeRegisterAllRegisteredModules(void) +{ + SCCudaHlModuleData *data = module_datas; + SCCudaHlModuleData *next_data = NULL; + + next_data = data; + while (data != NULL) { + next_data = data->next; + if (SCCudaHlDeRegisterModule(data->name) == -1) { + SCLogError(SC_ERR_CUDA_HANDLER_ERROR, "Error de-registering module " + "\"%s\"", data->name); + } + data = next_data; + } + + module_datas = NULL; + + return; +} + /** * \brief Pushes a cuda context for the calling thread. * diff --git a/src/util-cuda-handlers.h b/src/util-cuda-handlers.h index 7a7eefe900..79bfa945c4 100644 --- a/src/util-cuda-handlers.h +++ b/src/util-cuda-handlers.h @@ -55,6 +55,7 @@ int SCCudaHlGetModuleHandle(const char *); int SCCudaHlRegisterModule(const char *); int SCCudaHlDeRegisterModule(const char *); +void SCCudaHlDeRegisterAllRegisteredModules(void); int SCCudaHlPushCudaContextFromModule(const char *); diff --git a/src/util-mpm-b2g-cuda.c b/src/util-mpm-b2g-cuda.c index 7ac0cf8667..a6d2fce7c3 100644 --- a/src/util-mpm-b2g-cuda.c +++ b/src/util-mpm-b2g-cuda.c @@ -1637,6 +1637,12 @@ void B2gCudaDestroyCtx(MpmCtx *mpm_ctx) ctx->cuda_search_B2G = 0; } + if (ctx->cuda_scan_B2G != 0) { + if (SCCudaMemFree(ctx->cuda_scan_B2G) == -1) + SCLogError(SC_ERR_B2G_CUDA_ERROR, "Error freeing ctx->cuda_scan_B2G "); + ctx->cuda_scan_B2G = 0; + } + free(mpm_ctx->ctx); mpm_ctx->memory_cnt--; mpm_ctx->memory_size -= sizeof(B2gCudaCtx);