All of lore.kernel.org
 help / color / mirror / Atom feed
* [Accel-config] [PATCH v1 3/5] accel-config: Check and return error if kernel module is not loaded
@ 2021-02-22 20:27 ramesh.thomas
  0 siblings, 0 replies; only message in thread
From: ramesh.thomas @ 2021-02-22 20:27 UTC (permalink / raw)
  To: accel-config

[-- Attachment #1: Type: text/plain, Size: 1996 bytes --]

From: Ramesh Thomas <ramesh.thomas(a)intel.com>

Check if kernel module is loaded before running commands. Return failure
if not loaded.

Signed-off-by: Ramesh Thomas <ramesh.thomas(a)intel.com>
---
 accfg/accel-config.c | 44 +++++++++++++++++++++++++++++++++++++++++---
 1 file changed, 41 insertions(+), 3 deletions(-)

diff --git a/accfg/accel-config.c b/accfg/accel-config.c
index b096a5c..f12e449 100644
--- a/accfg/accel-config.c
+++ b/accfg/accel-config.c
@@ -7,6 +7,7 @@
 #include <unistd.h>
 #include <sys/stat.h>
 #include <sys/types.h>
+#include <libkmod.h>
 #include <builtin.h>
 #include <accfg/libaccel_config.h>
 #include <ccan/array_size/array_size.h>
@@ -74,9 +75,34 @@ static struct cmd_struct commands[] = {
 #endif
 };
 
+static int idxd_kmod_init(struct kmod_ctx **ctx, struct kmod_module **mod)
+{
+	int rc;
+
+	*ctx = kmod_new(NULL, NULL);
+	if (!*ctx)
+		return -ENXIO;
+
+	rc = kmod_module_new_from_name(*ctx, "idxd", mod);
+	if (rc < 0) {
+		kmod_unref(*ctx);
+		return rc;
+	}
+
+	rc = kmod_module_get_initstate(*mod);
+	if (rc < 0) {
+		kmod_module_unref(*mod);
+		kmod_unref(*ctx);
+	}
+
+	return rc;
+}
+
 int main(int argc, const char **argv)
 {
 	struct accfg_ctx *ctx;
+	struct kmod_ctx *kmod_ctx;
+	struct kmod_module *mod;
 	int rc;
 
 	/* Look for flags.. */
@@ -95,15 +121,27 @@ int main(int argc, const char **argv)
 		return 0;
 	}
 
+	rc = idxd_kmod_init(&kmod_ctx, &mod);
+	if (rc < 0) {
+		fprintf(stderr, "Failed initializing kernel module\n");
+		goto error_exit;
+	}
+
 	rc = accfg_new(&ctx);
-	if (rc)
-		return 0;
+	if (rc) {
+		kmod_module_unref(mod);
+		kmod_unref(kmod_ctx);
+		goto error_exit;
+	}
+
 	rc = main_handle_internal_command(argc, argv, ctx, commands,
 				     ARRAY_SIZE(commands));
 	accfg_unref(ctx);
+	kmod_module_unref(mod);
+	kmod_unref(kmod_ctx);
 
 	if (!rc)
 		return EXIT_SUCCESS;
-
+error_exit:
 	return EXIT_FAILURE;
 }
-- 
2.26.2

^ permalink raw reply related	[flat|nested] only message in thread

only message in thread, other threads:[~2021-02-22 20:27 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-02-22 20:27 [Accel-config] [PATCH v1 3/5] accel-config: Check and return error if kernel module is not loaded ramesh.thomas

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.