From: Lucas De Marchi <lucas.de.marchi@gmail.com>
To: linux-modules@vger.kernel.org
Cc: Luis Chamberlain <mcgrof@kernel.org>,
Lucas De Marchi <lucas.de.marchi@gmail.com>
Subject: [PATCH 2/5] libkmod: Extract finit_module vs init_module paths
Date: Thu, 1 Jun 2023 15:39:58 -0700 [thread overview]
Message-ID: <20230601224001.23397-3-lucas.de.marchi@gmail.com> (raw)
In-Reply-To: <20230601224001.23397-1-lucas.de.marchi@gmail.com>
Extract 2 functions to handle finit_module vs init_modules differences,
with a fallback from the former to the latter.
Signed-off-by: Lucas De Marchi <lucas.de.marchi@gmail.com>
---
libkmod/libkmod-module.c | 120 ++++++++++++++++++++++++---------------
1 file changed, 73 insertions(+), 47 deletions(-)
diff --git a/libkmod/libkmod-module.c b/libkmod/libkmod-module.c
index f352fe1..6ed5ad4 100644
--- a/libkmod/libkmod-module.c
+++ b/libkmod/libkmod-module.c
@@ -861,6 +861,73 @@ KMOD_EXPORT int kmod_module_remove_module(struct kmod_module *mod,
extern long init_module(const void *mem, unsigned long len, const char *args);
+static int do_finit_module(struct kmod_module *mod, unsigned int flags,
+ const char *args)
+{
+ unsigned int kernel_flags = 0;
+ int err;
+
+ /*
+ * Re-use ENOSYS, returned when there is no such syscall, so the
+ * fallback to init_module applies
+ */
+ if (!kmod_file_get_direct(mod->file))
+ return -ENOSYS;
+
+ if (flags & KMOD_INSERT_FORCE_VERMAGIC)
+ kernel_flags |= MODULE_INIT_IGNORE_VERMAGIC;
+ if (flags & KMOD_INSERT_FORCE_MODVERSION)
+ kernel_flags |= MODULE_INIT_IGNORE_MODVERSIONS;
+
+ err = finit_module(kmod_file_get_fd(mod->file), args, kernel_flags);
+ if (err < 0)
+ err = -errno;
+
+ return err;
+}
+
+static int do_init_module(struct kmod_module *mod, unsigned int flags,
+ const char *args)
+{
+ struct kmod_elf *elf;
+ const void *mem;
+ off_t size;
+ int err;
+
+ kmod_file_load_contents(mod->file);
+
+ if (flags & (KMOD_INSERT_FORCE_VERMAGIC | KMOD_INSERT_FORCE_MODVERSION)) {
+ elf = kmod_file_get_elf(mod->file);
+ if (elf == NULL) {
+ err = -errno;
+ return err;
+ }
+
+ if (flags & KMOD_INSERT_FORCE_MODVERSION) {
+ err = kmod_elf_strip_section(elf, "__versions");
+ if (err < 0)
+ INFO(mod->ctx, "Failed to strip modversion: %s\n", strerror(-err));
+ }
+
+ if (flags & KMOD_INSERT_FORCE_VERMAGIC) {
+ err = kmod_elf_strip_vermagic(elf);
+ if (err < 0)
+ INFO(mod->ctx, "Failed to strip vermagic: %s\n", strerror(-err));
+ }
+
+ mem = kmod_elf_get_memory(elf);
+ } else {
+ mem = kmod_file_get_contents(mod->file);
+ }
+ size = kmod_file_get_size(mod->file);
+
+ err = init_module(mem, size, args);
+ if (err < 0)
+ err = -errno;
+
+ return err;
+}
+
/**
* kmod_module_insert_module:
* @mod: kmod module
@@ -881,9 +948,6 @@ KMOD_EXPORT int kmod_module_insert_module(struct kmod_module *mod,
const char *options)
{
int err;
- const void *mem;
- off_t size;
- struct kmod_elf *elf;
const char *path;
const char *args = options ? options : "";
@@ -904,52 +968,14 @@ KMOD_EXPORT int kmod_module_insert_module(struct kmod_module *mod,
}
}
- if (kmod_file_get_direct(mod->file)) {
- unsigned int kernel_flags = 0;
-
- if (flags & KMOD_INSERT_FORCE_VERMAGIC)
- kernel_flags |= MODULE_INIT_IGNORE_VERMAGIC;
- if (flags & KMOD_INSERT_FORCE_MODVERSION)
- kernel_flags |= MODULE_INIT_IGNORE_MODVERSIONS;
-
- err = finit_module(kmod_file_get_fd(mod->file), args, kernel_flags);
- if (err == 0 || errno != ENOSYS)
- goto init_finished;
- }
-
- kmod_file_load_contents(mod->file);
-
- if (flags & (KMOD_INSERT_FORCE_VERMAGIC | KMOD_INSERT_FORCE_MODVERSION)) {
- elf = kmod_file_get_elf(mod->file);
- if (elf == NULL) {
- err = -errno;
- return err;
- }
+ err = do_finit_module(mod, flags, args);
+ if (err == -ENOSYS)
+ err = do_init_module(mod, flags, args);
- if (flags & KMOD_INSERT_FORCE_MODVERSION) {
- err = kmod_elf_strip_section(elf, "__versions");
- if (err < 0)
- INFO(mod->ctx, "Failed to strip modversion: %s\n", strerror(-err));
- }
-
- if (flags & KMOD_INSERT_FORCE_VERMAGIC) {
- err = kmod_elf_strip_vermagic(elf);
- if (err < 0)
- INFO(mod->ctx, "Failed to strip vermagic: %s\n", strerror(-err));
- }
-
- mem = kmod_elf_get_memory(elf);
- } else {
- mem = kmod_file_get_contents(mod->file);
- }
- size = kmod_file_get_size(mod->file);
+ if (err < 0)
+ INFO(mod->ctx, "Failed to insert module '%s': %s\n",
+ path, strerror(-err));
- err = init_module(mem, size, args);
-init_finished:
- if (err < 0) {
- err = -errno;
- INFO(mod->ctx, "Failed to insert module '%s': %m\n", path);
- }
return err;
}
--
2.40.1
next prev parent reply other threads:[~2023-06-01 22:40 UTC|newest]
Thread overview: 14+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-06-01 22:39 [PATCH 0/5] libkmod: Use kernel decompression support Lucas De Marchi
2023-06-01 22:39 ` [PATCH 1/5] libkmod: Do not inititialize file->memory on open Lucas De Marchi
2023-06-06 18:24 ` Luis Chamberlain
2023-06-01 22:39 ` Lucas De Marchi [this message]
2023-06-06 18:27 ` [PATCH 2/5] libkmod: Extract finit_module vs init_module paths Luis Chamberlain
2023-06-01 22:39 ` [PATCH 3/5] libkmod: Keep track of compression type Lucas De Marchi
2023-06-06 18:28 ` Luis Chamberlain
2023-06-01 22:40 ` [PATCH 4/5] libkmod: Keep track of in-kernel compression support Lucas De Marchi
2023-06-06 18:29 ` Luis Chamberlain
2023-06-06 18:30 ` Luis Chamberlain
2023-06-01 22:40 ` [PATCH 5/5] libkmod: Use kernel decompression when available Lucas De Marchi
2023-06-06 18:38 ` Luis Chamberlain
2023-06-06 19:01 ` Lucas De Marchi
2023-07-24 13:28 ` [PATCH 0/5] libkmod: Use kernel decompression support Lucas De Marchi
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20230601224001.23397-3-lucas.de.marchi@gmail.com \
--to=lucas.de.marchi@gmail.com \
--cc=linux-modules@vger.kernel.org \
--cc=mcgrof@kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox