[PATCH i-g-t v4 6/6] lib/igt_kmod: Do not leak kmod ctx

Lucas De Marchi lucas.demarchi at intel.com
Tue Nov 19 05:29:54 UTC 2024


kmod_ctx() is used as a cheap way to cache the kmod ctx. Make sure it's
released when the process exit by using igt_install_exit_handler(),
which also guarantees children forked with igt_fork* don't free it on
exit. Normal forks are still susceptible to breakage, but if it that
happens, let it break so we can fix.

Reviewed-by: Janusz Krzysztofik <janusz.krzysztofik at linux.intel.com>
Signed-off-by: Lucas De Marchi <lucas.demarchi at intel.com>
---
 lib/igt_kmod.c | 20 ++++++++++++++------
 1 file changed, 14 insertions(+), 6 deletions(-)

diff --git a/lib/igt_kmod.c b/lib/igt_kmod.c
index 3f77a0c94..eef3657c3 100644
--- a/lib/igt_kmod.c
+++ b/lib/igt_kmod.c
@@ -71,14 +71,20 @@ static void squelch(void *data, int priority,
 {
 }
 
+static struct kmod_ctx *ctx__;
+
+static void free_kmod_ctx(int sig)
+{
+	kmod_unref(ctx__);
+}
+
 static struct kmod_ctx *kmod_ctx(void)
 {
-	static struct kmod_ctx *ctx;
 	const char **config_paths = NULL;
 	char *config_paths_str;
 	char *dirname;
 
-	if (ctx)
+	if (ctx__)
 		goto out;
 
 	dirname = getenv("IGT_KMOD_DIRNAME");
@@ -112,14 +118,16 @@ static struct kmod_ctx *kmod_ctx(void)
 		config_paths[i] = NULL;
 	}
 
-	ctx = kmod_new(dirname, config_paths);
-	igt_assert(ctx != NULL);
+	ctx__ = kmod_new(dirname, config_paths);
+	igt_assert(ctx__ != NULL);
 
 	free(config_paths);
+	igt_install_exit_handler(free_kmod_ctx);
+
+	kmod_set_log_fn(ctx__, squelch, NULL);
 
-	kmod_set_log_fn(ctx, squelch, NULL);
 out:
-	return ctx;
+	return ctx__;
 }
 
 /**
-- 
2.47.0



More information about the igt-dev mailing list