diff mbox series

[2/2] modprobe: Allow passing path to module

Message ID 20230111152936.101040-3-gustavo.sousa@intel.com (mailing list archive)
State New, archived
Headers show
Series Allow passing module files to modprobe | expand

Commit Message

Gustavo Sousa Jan. 11, 2023, 3:29 p.m. UTC
This is useful to kernel module developers for testing a just compiled
module: instead of using insmod, they can load the module from the path
while getting all the benefits of modprobe (e.g. module dependency
resolution).

Signed-off-by: Gustavo Sousa <gustavo.sousa@intel.com>
---
 man/modprobe.xml                              |   5 +++
 testsuite/populate-modules.sh                 |   1 +
 .../lib/modules/4.4.4/modules.alias           |   1 +
 .../lib/modules/4.4.4/modules.alias.bin       | Bin 0 -> 12 bytes
 .../lib/modules/4.4.4/modules.builtin.bin     |   0
 .../lib/modules/4.4.4/modules.dep             |   1 +
 .../lib/modules/4.4.4/modules.dep.bin         | Bin 0 -> 73 bytes
 .../lib/modules/4.4.4/modules.devname         |   0
 .../lib/modules/4.4.4/modules.softdep         |   1 +
 .../lib/modules/4.4.4/modules.symbols         |   1 +
 .../lib/modules/4.4.4/modules.symbols.bin     | Bin 0 -> 12 bytes
 .../module-from-path/proc/modules             |   0
 testsuite/test-modprobe.c                     |  22 ++++++++++++
 tools/modprobe.c                              |  34 ++++++++++++------
 14 files changed, 56 insertions(+), 10 deletions(-)
 create mode 100644 testsuite/rootfs-pristine/test-modprobe/module-from-path/lib/modules/4.4.4/modules.alias
 create mode 100644 testsuite/rootfs-pristine/test-modprobe/module-from-path/lib/modules/4.4.4/modules.alias.bin
 create mode 100644 testsuite/rootfs-pristine/test-modprobe/module-from-path/lib/modules/4.4.4/modules.builtin.bin
 create mode 100644 testsuite/rootfs-pristine/test-modprobe/module-from-path/lib/modules/4.4.4/modules.dep
 create mode 100644 testsuite/rootfs-pristine/test-modprobe/module-from-path/lib/modules/4.4.4/modules.dep.bin
 create mode 100644 testsuite/rootfs-pristine/test-modprobe/module-from-path/lib/modules/4.4.4/modules.devname
 create mode 100644 testsuite/rootfs-pristine/test-modprobe/module-from-path/lib/modules/4.4.4/modules.softdep
 create mode 100644 testsuite/rootfs-pristine/test-modprobe/module-from-path/lib/modules/4.4.4/modules.symbols
 create mode 100644 testsuite/rootfs-pristine/test-modprobe/module-from-path/lib/modules/4.4.4/modules.symbols.bin
 create mode 100644 testsuite/rootfs-pristine/test-modprobe/module-from-path/proc/modules

Comments

Lucas De Marchi Jan. 12, 2023, 9:40 p.m. UTC | #1
On Wed, Jan 11, 2023 at 12:29:36PM -0300, Gustavo Sousa wrote:
>This is useful to kernel module developers for testing a just compiled
>module: instead of using insmod, they can load the module from the path
>while getting all the benefits of modprobe (e.g. module dependency
>resolution).
>
>Signed-off-by: Gustavo Sousa <gustavo.sousa@intel.com>
>---
> man/modprobe.xml                              |   5 +++
> testsuite/populate-modules.sh                 |   1 +
> .../lib/modules/4.4.4/modules.alias           |   1 +
> .../lib/modules/4.4.4/modules.alias.bin       | Bin 0 -> 12 bytes
> .../lib/modules/4.4.4/modules.builtin.bin     |   0
> .../lib/modules/4.4.4/modules.dep             |   1 +
> .../lib/modules/4.4.4/modules.dep.bin         | Bin 0 -> 73 bytes
> .../lib/modules/4.4.4/modules.devname         |   0
> .../lib/modules/4.4.4/modules.softdep         |   1 +
> .../lib/modules/4.4.4/modules.symbols         |   1 +
> .../lib/modules/4.4.4/modules.symbols.bin     | Bin 0 -> 12 bytes
> .../module-from-path/proc/modules             |   0
> testsuite/test-modprobe.c                     |  22 ++++++++++++
> tools/modprobe.c                              |  34 ++++++++++++------
> 14 files changed, 56 insertions(+), 10 deletions(-)
> create mode 100644 testsuite/rootfs-pristine/test-modprobe/module-from-path/lib/modules/4.4.4/modules.alias
> create mode 100644 testsuite/rootfs-pristine/test-modprobe/module-from-path/lib/modules/4.4.4/modules.alias.bin
> create mode 100644 testsuite/rootfs-pristine/test-modprobe/module-from-path/lib/modules/4.4.4/modules.builtin.bin
> create mode 100644 testsuite/rootfs-pristine/test-modprobe/module-from-path/lib/modules/4.4.4/modules.dep
> create mode 100644 testsuite/rootfs-pristine/test-modprobe/module-from-path/lib/modules/4.4.4/modules.dep.bin
> create mode 100644 testsuite/rootfs-pristine/test-modprobe/module-from-path/lib/modules/4.4.4/modules.devname
> create mode 100644 testsuite/rootfs-pristine/test-modprobe/module-from-path/lib/modules/4.4.4/modules.softdep
> create mode 100644 testsuite/rootfs-pristine/test-modprobe/module-from-path/lib/modules/4.4.4/modules.symbols
> create mode 100644 testsuite/rootfs-pristine/test-modprobe/module-from-path/lib/modules/4.4.4/modules.symbols.bin
> create mode 100644 testsuite/rootfs-pristine/test-modprobe/module-from-path/proc/modules
>
>diff --git a/man/modprobe.xml b/man/modprobe.xml
>index db39c7a18bb7..615466977f6a 100644
>--- a/man/modprobe.xml
>+++ b/man/modprobe.xml
>@@ -115,6 +115,11 @@
>       kernel (in addition to any options listed in the configuration
>       file).
>     </para>
>+    <para>
>+      When loading modules, <replaceable>modulename</replaceable> can also
>+      be a path to the module. If the path is relative, it must
>+      explicitly start with "./".

we may extend this to mention that loading from a random location may fail
when the depmod database is not updated.  The depmod database is usually
updated with kernel (or module) installation - the dependencies there may not be
up-to-date if a random path is used.


>+    </para>
>   </refsect1>
>
>   <refsect1><title>OPTIONS</title>
>diff --git a/testsuite/populate-modules.sh b/testsuite/populate-modules.sh
>index 099f02669156..652279eda728 100755
>--- a/testsuite/populate-modules.sh
>+++ b/testsuite/populate-modules.sh
>@@ -56,6 +56,7 @@ map=(
>     ["test-modprobe/alias-to-none/lib/modules/4.4.4/kernel/"]="mod-simple.ko"
>     ["test-modprobe/module-param-kcmdline/lib/modules/4.4.4/kernel/"]="mod-simple.ko"
>     ["test-modprobe/external/lib/modules/external/"]="mod-simple.ko"
>+    ["test-modprobe/module-from-path/home/foo/"]="mod-simple.ko"
>     ["test-depmod/modules-order-compressed/lib/modules/4.4.4/kernel/drivers/block/cciss.ko"]="mod-fake-cciss.ko"
>     ["test-depmod/modules-order-compressed/lib/modules/4.4.4/kernel/drivers/scsi/hpsa.ko"]="mod-fake-hpsa.ko"
>     ["test-depmod/modules-order-compressed/lib/modules/4.4.4/kernel/drivers/scsi/scsi_mod.ko"]="mod-fake-scsi-mod.ko"
>diff --git a/testsuite/rootfs-pristine/test-modprobe/module-from-path/lib/modules/4.4.4/modules.alias b/testsuite/rootfs-pristine/test-modprobe/module-from-path/lib/modules/4.4.4/modules.alias
>new file mode 100644
>index 000000000000..ba76e1815af0
>--- /dev/null
>+++ b/testsuite/rootfs-pristine/test-modprobe/module-from-path/lib/modules/4.4.4/modules.alias
>@@ -0,0 +1 @@
>+# Aliases extracted from modules themselves.
>diff --git a/testsuite/rootfs-pristine/test-modprobe/module-from-path/lib/modules/4.4.4/modules.alias.bin b/testsuite/rootfs-pristine/test-modprobe/module-from-path/lib/modules/4.4.4/modules.alias.bin
>new file mode 100644
>index 0000000000000000000000000000000000000000..7075435f6268c4d815aec093d61e26647666ba76
>GIT binary patch
>literal 12
>TcmdnM{w17&iGh)Ufq@4A6;A>Z
>
>literal 0
>HcmV?d00001
>
>diff --git a/testsuite/rootfs-pristine/test-modprobe/module-from-path/lib/modules/4.4.4/modules.builtin.bin b/testsuite/rootfs-pristine/test-modprobe/module-from-path/lib/modules/4.4.4/modules.builtin.bin
>new file mode 100644
>index 000000000000..e69de29bb2d1
>diff --git a/testsuite/rootfs-pristine/test-modprobe/module-from-path/lib/modules/4.4.4/modules.dep b/testsuite/rootfs-pristine/test-modprobe/module-from-path/lib/modules/4.4.4/modules.dep
>new file mode 100644
>index 000000000000..e612900c5de7
>--- /dev/null
>+++ b/testsuite/rootfs-pristine/test-modprobe/module-from-path/lib/modules/4.4.4/modules.dep
>@@ -0,0 +1 @@
>+/lib/modules/external/mod-simple.ko:
>diff --git a/testsuite/rootfs-pristine/test-modprobe/module-from-path/lib/modules/4.4.4/modules.dep.bin b/testsuite/rootfs-pristine/test-modprobe/module-from-path/lib/modules/4.4.4/modules.dep.bin
>new file mode 100644
>index 0000000000000000000000000000000000000000..556e3c8142d5d85dba5b557474907f9f9dd99dcb
>GIT binary patch
>literal 73
>zcmdnM{w17&iGfjpfx$UHCB8T_w;(5#0SFjDgnmwDl74P}N@-4Nv3_brNorAEVh%_^
>S7ot!vJKu^SH}?Po0}lY-ZWUAj
>
>literal 0
>HcmV?d00001
>
>diff --git a/testsuite/rootfs-pristine/test-modprobe/module-from-path/lib/modules/4.4.4/modules.devname b/testsuite/rootfs-pristine/test-modprobe/module-from-path/lib/modules/4.4.4/modules.devname
>new file mode 100644
>index 000000000000..e69de29bb2d1
>diff --git a/testsuite/rootfs-pristine/test-modprobe/module-from-path/lib/modules/4.4.4/modules.softdep b/testsuite/rootfs-pristine/test-modprobe/module-from-path/lib/modules/4.4.4/modules.softdep
>new file mode 100644
>index 000000000000..5554ccca7f9e
>--- /dev/null
>+++ b/testsuite/rootfs-pristine/test-modprobe/module-from-path/lib/modules/4.4.4/modules.softdep
>@@ -0,0 +1 @@
>+# Soft dependencies extracted from modules themselves.
>diff --git a/testsuite/rootfs-pristine/test-modprobe/module-from-path/lib/modules/4.4.4/modules.symbols b/testsuite/rootfs-pristine/test-modprobe/module-from-path/lib/modules/4.4.4/modules.symbols
>new file mode 100644
>index 000000000000..618c345f7e93
>--- /dev/null
>+++ b/testsuite/rootfs-pristine/test-modprobe/module-from-path/lib/modules/4.4.4/modules.symbols
>@@ -0,0 +1 @@
>+# Aliases for symbols, used by symbol_request().
>diff --git a/testsuite/rootfs-pristine/test-modprobe/module-from-path/lib/modules/4.4.4/modules.symbols.bin b/testsuite/rootfs-pristine/test-modprobe/module-from-path/lib/modules/4.4.4/modules.symbols.bin
>new file mode 100644
>index 0000000000000000000000000000000000000000..7075435f6268c4d815aec093d61e26647666ba76
>GIT binary patch
>literal 12
>TcmdnM{w17&iGh)Ufq@4A6;A>Z
>
>literal 0
>HcmV?d00001
>
>diff --git a/testsuite/rootfs-pristine/test-modprobe/module-from-path/proc/modules b/testsuite/rootfs-pristine/test-modprobe/module-from-path/proc/modules
>new file mode 100644
>index 000000000000..e69de29bb2d1
>diff --git a/testsuite/test-modprobe.c b/testsuite/test-modprobe.c
>index 0255f1aaccb5..3f8a430c09e4 100644
>--- a/testsuite/test-modprobe.c
>+++ b/testsuite/test-modprobe.c
>@@ -422,4 +422,26 @@ DEFINE_TEST(modprobe_external,
> 	.modules_loaded = "mod-simple",
> 	);
>
>+static noreturn int modprobe_module_from_path(const struct test *t)
>+{
>+	const char *progname = ABS_TOP_BUILDDIR "/tools/modprobe";
>+	const char *const args[] = {
>+		progname,
>+		"/home/foo/mod-simple.ko",
>+		NULL,
>+	};
>+
>+	test_spawn_prog(progname, args);
>+	exit(EXIT_FAILURE);
>+}

missing a test for relative path?

>+DEFINE_TEST(modprobe_module_from_path,
>+	.description = "check modprobe able to load module given as a direct path",
>+	.config = {
>+		[TC_UNAME_R] = "4.4.4",
>+		[TC_ROOTFS] = TESTSUITE_ROOTFS "test-modprobe/module-from-path",
>+		[TC_INIT_MODULE_RETCODES] = "",
>+	},
>+	.modules_loaded = "mod-simple",
>+	);
>+
> TESTSUITE_MAIN();
>diff --git a/tools/modprobe.c b/tools/modprobe.c
>index d4012fab39f8..3b7897c1b8e4 100644
>--- a/tools/modprobe.c
>+++ b/tools/modprobe.c
>@@ -614,14 +614,23 @@ static int insmod(struct kmod_ctx *ctx, const char *alias,
> 						const char *extra_options)
> {
> 	struct kmod_list *l, *list = NULL;
>+	struct kmod_module *mod = NULL;
> 	int err, flags = 0;
>
>-	err = kmod_module_new_from_lookup(ctx, alias, &list);
>-
>-	if (list == NULL || err < 0) {
>-		LOG("Module %s not found in directory %s\n", alias,
>-			ctx ? kmod_get_dirname(ctx) : "(missing)");
>-		return -ENOENT;
>+	if (strncmp(alias, "/", 1) == 0 || strncmp(alias, "./", 2) == 0) {

an alias may start with / or ./, so in theory there could be
regressions. At least in the kernel we have none:

$ cat /lib/modules/$(uname -r)/modules.alias | grep /
alias devname:net/tun tun
alias dmi:bvnIBM:*:pnIBM3850M2/x3950M2-* ibmaem
alias devname:mapper/control dm_mod
alias dmi:*:svnMICRO-STARINTERNATIONAL*:pnU90/U100:* msi_laptop
alias dmi*:svn*SAMSUNGELECTRONICSCO.,LTD.*:pn*300V3Z/300V4Z/300V5Z*: samsung_laptop
alias dmi*:svn*SAMSUNGELECTRONICSCO.,LTD.*:pn*730U3E/740U3E*: samsung_laptop
alias dmi*:svn*SAMSUNGELECTRONICSCO.,LTD.*:pn*NC210/NC110*:rn*NC210/NC110*: samsung_laptop
alias dmi*:svn*SAMSUNGELECTRONICSCO.,LTD.*:pn*NF110/NF210/NF310*:rn*NF110/NF210/NF310*: samsung_laptop
alias dmi*:svn*SAMSUNGELECTRONICSCO.,LTD.*:pn*N150/N210/N220*:rn*N150/N210/N220*: samsung_laptop
alias dmi*:svn*SAMSUNGELECTRONICSCO.,LTD.*:pn*N145P/N250P/N260P*:rn*N145P/N250P/N260P*: samsung_laptop
alias dmi*:svn*SAMSUNGELECTRONICSCO.,LTD.*:pn*R40/R41*:rn*R40/R41*: samsung_laptop
alias devname:vfio/vfio vfio
alias devname:snd/timer snd_timer
alias devname:snd/seq snd_seq

But it could come from user configuration in /etc. Realistically
speaking I've never seen a "user alias" with /, so I guess we can keep
it like this. If we see regressions, we may try to use a fallback
approach to kmod_module_new_from_lookup() if kmod_module_new_from_path()
fails with ENOENT.

>+		err = kmod_module_new_from_path(ctx, alias, &mod);
>+		if (err < 0) {
>+			LOG("Failed to get module from path %s: %s\n", alias,
>+				strerror(-err));
>+			return -ENOENT;
>+		}
>+	} else {
>+		err = kmod_module_new_from_lookup(ctx, alias, &list);
>+		if (list == NULL || err < 0) {
>+			LOG("Module %s not found in directory %s\n", alias,
>+				ctx ? kmod_get_dirname(ctx) : "(missing)");
>+			return -ENOENT;
>+		}
> 	}
>
> 	if (strip_modversion || force)
>@@ -642,13 +651,18 @@ static int insmod(struct kmod_ctx *ctx, const char *alias,
> 	if (first_time)
> 		flags |= KMOD_PROBE_FAIL_ON_LOADED;
>
>-	kmod_list_foreach(l, list) {
>-		struct kmod_module *mod = kmod_module_get_module(l);
>+	/* If module is loaded from path */
>+	if (mod != NULL) {
> 		err = insmod_insert(mod, flags, extra_options);
> 		kmod_module_unref(mod);
>+	} else {
>+		kmod_list_foreach(l, list) {
>+			mod = kmod_module_get_module(l);
>+			err = insmod_insert(mod, flags, extra_options);
>+			kmod_module_unref(mod);
>+		}
>+		kmod_module_unref_list(list);
> 	}
>-
>-	kmod_module_unref_list(list);

feel free to add a Reviewed-by: Lucas De Marchi <lucas.demarchi@intel.com>
with the changes mentioned above.

thanks
Lucas De Marchi

> 	return err;
> }
>
>-- 
>2.39.0
>
diff mbox series

Patch

diff --git a/man/modprobe.xml b/man/modprobe.xml
index db39c7a18bb7..615466977f6a 100644
--- a/man/modprobe.xml
+++ b/man/modprobe.xml
@@ -115,6 +115,11 @@ 
       kernel (in addition to any options listed in the configuration
       file).
     </para>
+    <para>
+      When loading modules, <replaceable>modulename</replaceable> can also
+      be a path to the module. If the path is relative, it must
+      explicitly start with "./".
+    </para>
   </refsect1>
 
   <refsect1><title>OPTIONS</title>
diff --git a/testsuite/populate-modules.sh b/testsuite/populate-modules.sh
index 099f02669156..652279eda728 100755
--- a/testsuite/populate-modules.sh
+++ b/testsuite/populate-modules.sh
@@ -56,6 +56,7 @@  map=(
     ["test-modprobe/alias-to-none/lib/modules/4.4.4/kernel/"]="mod-simple.ko"
     ["test-modprobe/module-param-kcmdline/lib/modules/4.4.4/kernel/"]="mod-simple.ko"
     ["test-modprobe/external/lib/modules/external/"]="mod-simple.ko"
+    ["test-modprobe/module-from-path/home/foo/"]="mod-simple.ko"
     ["test-depmod/modules-order-compressed/lib/modules/4.4.4/kernel/drivers/block/cciss.ko"]="mod-fake-cciss.ko"
     ["test-depmod/modules-order-compressed/lib/modules/4.4.4/kernel/drivers/scsi/hpsa.ko"]="mod-fake-hpsa.ko"
     ["test-depmod/modules-order-compressed/lib/modules/4.4.4/kernel/drivers/scsi/scsi_mod.ko"]="mod-fake-scsi-mod.ko"
diff --git a/testsuite/rootfs-pristine/test-modprobe/module-from-path/lib/modules/4.4.4/modules.alias b/testsuite/rootfs-pristine/test-modprobe/module-from-path/lib/modules/4.4.4/modules.alias
new file mode 100644
index 000000000000..ba76e1815af0
--- /dev/null
+++ b/testsuite/rootfs-pristine/test-modprobe/module-from-path/lib/modules/4.4.4/modules.alias
@@ -0,0 +1 @@ 
+# Aliases extracted from modules themselves.
diff --git a/testsuite/rootfs-pristine/test-modprobe/module-from-path/lib/modules/4.4.4/modules.alias.bin b/testsuite/rootfs-pristine/test-modprobe/module-from-path/lib/modules/4.4.4/modules.alias.bin
new file mode 100644
index 0000000000000000000000000000000000000000..7075435f6268c4d815aec093d61e26647666ba76
GIT binary patch
literal 12
TcmdnM{w17&iGh)Ufq@4A6;A>Z

literal 0
HcmV?d00001

diff --git a/testsuite/rootfs-pristine/test-modprobe/module-from-path/lib/modules/4.4.4/modules.builtin.bin b/testsuite/rootfs-pristine/test-modprobe/module-from-path/lib/modules/4.4.4/modules.builtin.bin
new file mode 100644
index 000000000000..e69de29bb2d1
diff --git a/testsuite/rootfs-pristine/test-modprobe/module-from-path/lib/modules/4.4.4/modules.dep b/testsuite/rootfs-pristine/test-modprobe/module-from-path/lib/modules/4.4.4/modules.dep
new file mode 100644
index 000000000000..e612900c5de7
--- /dev/null
+++ b/testsuite/rootfs-pristine/test-modprobe/module-from-path/lib/modules/4.4.4/modules.dep
@@ -0,0 +1 @@ 
+/lib/modules/external/mod-simple.ko:
diff --git a/testsuite/rootfs-pristine/test-modprobe/module-from-path/lib/modules/4.4.4/modules.dep.bin b/testsuite/rootfs-pristine/test-modprobe/module-from-path/lib/modules/4.4.4/modules.dep.bin
new file mode 100644
index 0000000000000000000000000000000000000000..556e3c8142d5d85dba5b557474907f9f9dd99dcb
GIT binary patch
literal 73
zcmdnM{w17&iGfjpfx$UHCB8T_w;(5#0SFjDgnmwDl74P}N@-4Nv3_brNorAEVh%_^
S7ot!vJKu^SH}?Po0}lY-ZWUAj

literal 0
HcmV?d00001

diff --git a/testsuite/rootfs-pristine/test-modprobe/module-from-path/lib/modules/4.4.4/modules.devname b/testsuite/rootfs-pristine/test-modprobe/module-from-path/lib/modules/4.4.4/modules.devname
new file mode 100644
index 000000000000..e69de29bb2d1
diff --git a/testsuite/rootfs-pristine/test-modprobe/module-from-path/lib/modules/4.4.4/modules.softdep b/testsuite/rootfs-pristine/test-modprobe/module-from-path/lib/modules/4.4.4/modules.softdep
new file mode 100644
index 000000000000..5554ccca7f9e
--- /dev/null
+++ b/testsuite/rootfs-pristine/test-modprobe/module-from-path/lib/modules/4.4.4/modules.softdep
@@ -0,0 +1 @@ 
+# Soft dependencies extracted from modules themselves.
diff --git a/testsuite/rootfs-pristine/test-modprobe/module-from-path/lib/modules/4.4.4/modules.symbols b/testsuite/rootfs-pristine/test-modprobe/module-from-path/lib/modules/4.4.4/modules.symbols
new file mode 100644
index 000000000000..618c345f7e93
--- /dev/null
+++ b/testsuite/rootfs-pristine/test-modprobe/module-from-path/lib/modules/4.4.4/modules.symbols
@@ -0,0 +1 @@ 
+# Aliases for symbols, used by symbol_request().
diff --git a/testsuite/rootfs-pristine/test-modprobe/module-from-path/lib/modules/4.4.4/modules.symbols.bin b/testsuite/rootfs-pristine/test-modprobe/module-from-path/lib/modules/4.4.4/modules.symbols.bin
new file mode 100644
index 0000000000000000000000000000000000000000..7075435f6268c4d815aec093d61e26647666ba76
GIT binary patch
literal 12
TcmdnM{w17&iGh)Ufq@4A6;A>Z

literal 0
HcmV?d00001

diff --git a/testsuite/rootfs-pristine/test-modprobe/module-from-path/proc/modules b/testsuite/rootfs-pristine/test-modprobe/module-from-path/proc/modules
new file mode 100644
index 000000000000..e69de29bb2d1
diff --git a/testsuite/test-modprobe.c b/testsuite/test-modprobe.c
index 0255f1aaccb5..3f8a430c09e4 100644
--- a/testsuite/test-modprobe.c
+++ b/testsuite/test-modprobe.c
@@ -422,4 +422,26 @@  DEFINE_TEST(modprobe_external,
 	.modules_loaded = "mod-simple",
 	);
 
+static noreturn int modprobe_module_from_path(const struct test *t)
+{
+	const char *progname = ABS_TOP_BUILDDIR "/tools/modprobe";
+	const char *const args[] = {
+		progname,
+		"/home/foo/mod-simple.ko",
+		NULL,
+	};
+
+	test_spawn_prog(progname, args);
+	exit(EXIT_FAILURE);
+}
+DEFINE_TEST(modprobe_module_from_path,
+	.description = "check modprobe able to load module given as a direct path",
+	.config = {
+		[TC_UNAME_R] = "4.4.4",
+		[TC_ROOTFS] = TESTSUITE_ROOTFS "test-modprobe/module-from-path",
+		[TC_INIT_MODULE_RETCODES] = "",
+	},
+	.modules_loaded = "mod-simple",
+	);
+
 TESTSUITE_MAIN();
diff --git a/tools/modprobe.c b/tools/modprobe.c
index d4012fab39f8..3b7897c1b8e4 100644
--- a/tools/modprobe.c
+++ b/tools/modprobe.c
@@ -614,14 +614,23 @@  static int insmod(struct kmod_ctx *ctx, const char *alias,
 						const char *extra_options)
 {
 	struct kmod_list *l, *list = NULL;
+	struct kmod_module *mod = NULL;
 	int err, flags = 0;
 
-	err = kmod_module_new_from_lookup(ctx, alias, &list);
-
-	if (list == NULL || err < 0) {
-		LOG("Module %s not found in directory %s\n", alias,
-			ctx ? kmod_get_dirname(ctx) : "(missing)");
-		return -ENOENT;
+	if (strncmp(alias, "/", 1) == 0 || strncmp(alias, "./", 2) == 0) {
+		err = kmod_module_new_from_path(ctx, alias, &mod);
+		if (err < 0) {
+			LOG("Failed to get module from path %s: %s\n", alias,
+				strerror(-err));
+			return -ENOENT;
+		}
+	} else {
+		err = kmod_module_new_from_lookup(ctx, alias, &list);
+		if (list == NULL || err < 0) {
+			LOG("Module %s not found in directory %s\n", alias,
+				ctx ? kmod_get_dirname(ctx) : "(missing)");
+			return -ENOENT;
+		}
 	}
 
 	if (strip_modversion || force)
@@ -642,13 +651,18 @@  static int insmod(struct kmod_ctx *ctx, const char *alias,
 	if (first_time)
 		flags |= KMOD_PROBE_FAIL_ON_LOADED;
 
-	kmod_list_foreach(l, list) {
-		struct kmod_module *mod = kmod_module_get_module(l);
+	/* If module is loaded from path */
+	if (mod != NULL) {
 		err = insmod_insert(mod, flags, extra_options);
 		kmod_module_unref(mod);
+	} else {
+		kmod_list_foreach(l, list) {
+			mod = kmod_module_get_module(l);
+			err = insmod_insert(mod, flags, extra_options);
+			kmod_module_unref(mod);
+		}
+		kmod_module_unref_list(list);
 	}
-
-	kmod_module_unref_list(list);
 	return err;
 }