Skip to contentSkip to conversation

module: fix early stop for load module function

PATCH In review Hector Cao 2 participants 4 messages Apr 30 – Jun 10

lore
Share & tools
lore

Hector Cao

author
#0 lore raw

v2 cover module: fix early stop for load module function

Hello,

please find the v2 submission for fixing the early stop of module loading function.

v2:
* better management of the error (errp) to avoid assert on reuse
* set rv return code to -1 to stay compliant with the load_module() API specification in case the module load fails.

Hector Cao (1):
module: fix early stop for load module function

 util/module.c | 51 ++++++++++++++++++++++++++++++++++-----------------
 1 file changed, 34 insertions(+), 17 deletions(-)
Signature · 1 line
2.43.0
author
#1 lore raw

PATCH v2 1/1 module: fix early stop for load module function

Commit c551fb0b53d regressed module-loading behavior. Historically, when loading from one module directory failed, QEMU continued searching the remaining directories. That commit made QEMU stop if a matching file was found but could not be loaded (for example, due to a build mismatch).

As a result, QEMU can fail to load the correct module, typically when the build option module-upgrades is enabled and at upgrades multiple module directories exist.

In addition to the regression on the behavior, this commit introduces also a leak in the function module_load_dso() that does not remove the entries from the list dso_init_list when the the module load fails for build mismatch.

This commit has 2 folds:
- restores the correct legacy behavior + set rv to -1 in case of load failure for one found module.
- fix the memory leak in module_load_dso()

Fixes c551fb0b53d ("module: add Error arguments to module_load and module_load_qom")
---
 util/module.c | 51 ++++++++++++++++++++++++++++++++++-----------------
 1 file changed, 34 insertions(+), 17 deletions(-)
util/module.c +34 -17
index 09dc43f51eb..435b34e354f 100644
@@ -184,6 +184,11 @@ static bool module_load_dso(const char *fname, bool export_symbols,
                 "Only modules from the same build can be loaded.\n");
         }
         g_module_close(g_module);
+        /* Clean up any dso init entries added during DSO initialization */
+        QTAILQ_FOREACH_SAFE(e, &dso_init_list, node, next) {
+            QTAILQ_REMOVE(&dso_init_list, e, node);
+            g_free(e);
+        }
         return false;
     }
 
@@ -279,29 +284,41 @@ int module_load(const char *prefix, const char *name, Error **errp)
         }
     }
 
-    for (i = 0; i < n_dirs; i++) {
+    /*
+     * Try to load module in given folders until we load it successfully or no
+     * more to look.
+     * If no module can be loaded successfully, errp is set to the last encountered error
+     */
+    rv = 0;
+    for (i = 0; (i < n_dirs) && (rv != 1); i++) {
         char *fname = g_strdup_printf("%s/%s%s",
                                       dirs[i], module_name, CONFIG_HOST_DSOSUF);
         int ret = access(fname, F_OK);
-        if (ret != 0 && (errno == ENOENT || errno == ENOTDIR)) {
-            /*
-             * if we don't find the module in this dir, try the next one.
-             * If we don't find it in any dir, that can be fine too: user
-             * did not install the module. We will return 0 in this case
-             * with no error set.
-             */
-            g_free(fname);
-            continue;
-        } else if (ret != 0) {
-            /* most common is EACCES here */
-            error_setg_errno(errp, errno, "error trying to access %s", fname);
-        } else if (module_load_dso(fname, export_symbols, errp)) {
-            rv = 1; /* module successfully loaded */
+        if (ret == 0) {
+            if (module_load_dso(fname, export_symbols, errp)) {
+                /* module successfully loaded */
+                rv = 1;
+            } else {
+                /* fail to load a module */
+                rv = -1;
+            }
+        } else {
+            /* The file exists but cannot be accessed -> report an error and continue */
+            if ((errno != ENOENT) && (errno != ENOTDIR)) {
+                /* most common is EACCES here */
+                error_setg_errno(errp, errno, "error trying to access %s", fname);
+                rv = -1;
+            }
         }
+
         g_free(fname);
-        goto out;
+
+        /* If we have more to look at, report the current error */
+        if ((i < n_dirs - 1) && (rv != 1) && errp && *errp) {
+            error_report_err(*errp);
+            *errp = NULL;
+        }
     }
-    rv = 0; /* module not found */
 
 out:
     if (rv <= 0) {
Signature · 1 line
2.43.0
Reviewed-by on v2 1/1
#2 lore raw
Quoted 5 lines · Hector Cao

Commit c551fb0b53d regressed module-loading behavior. Historically, when loading from one module directory failed, QEMU continued searching the remaining directories. That commit made QEMU stop if a matching file was found but could not be loaded (for example, due to a build mismatch).

Thanks for the v2, Hector. The errp handling with error_report_err() looks correct now — errors are reported and cleared between iterations, and the last error is propagated to the caller.

One minor edge case: if an earlier directory has a build mismatch (rv = -1) but the retained module directory doesn't have the file at all (ENOENT), rv stays at -1 with *errp == NULL. The caller gets a failure return without an error message. In practice this is unlikely since /run/qemu/<version>/ should contain the module if it was retained, so I don't think this needs to block the patch.

--
Gabriel Hartmann
Senior Systems Engineer

NETWAYS Managed Services GmbH | Deutschherrnstr. 15-19 | D-90429 Nuernberg
Tel: +49 911 92885-0 | Fax: +49 911 92885-77
CEO: Julian Hein, Bernd Erk, Sebastian Saemann | AG Nuernberg HRB25207
https://www.netways.de | [email protected]

** Nuremberg Digital Festival | Open Source Night 23.06.26 | https://netways.de/nuedigital2026 ** ** Open Tech Day 2026 | Software Defined Storage - October | Nuremberg - https://opentechday.de/ ** ** Open Source Monitoring Conference 2026 - November | Nuremberg - https://osmc.de ** ** NETWAYS Web Services - https://nws.netways.de **
** NETWAYS Trainings - https://netways.de/trainings **

author
#3 lore raw

Thanks Gabriel for the review,

May I ask a maintainer to review this and approve it and give me some confidence so I can downstream it to Distros?

Best,

Quoted 38 lines · Gabriel Hartmann
Quoted 5 lines · Hector Cao

Commit c551fb0b53d regressed module-loading behavior. Historically, when loading from one module directory failed, QEMU continued searching the remaining directories. That commit made QEMU stop if a matching file was found but could not be loaded (for example, due to a build mismatch).

Thanks for the v2, Hector. The errp handling with error_report_err() looks correct now — errors are reported and cleared between iterations, and the last error is propagated to the caller.

One minor edge case: if an earlier directory has a build mismatch (rv = -1) but the retained module directory doesn't have the file at all (ENOENT), rv stays at -1 with *errp == NULL. The caller gets a failure return without an error message. In practice this is unlikely since /run/qemu/<version>/ should contain the module if it was retained, so I don't think this needs to block the patch.

--
Gabriel Hartmann
Senior Systems Engineer

NETWAYS Managed Services GmbH | Deutschherrnstr. 15-19 | D-90429 Nuernberg
Tel: +49 911 92885-0 | Fax: +49 911 92885-77
CEO: Julian Hein, Bernd Erk, Sebastian Saemann | AG Nuernberg HRB25207
https://www.netways.de | [email protected]

** Nuremberg Digital Festival | Open Source Night 23.06.26 |
https://netways.de/nuedigital2026 **
** Open Tech Day 2026 | Software Defined Storage - October | Nuremberg -
https://opentechday.de/ **
** Open Source Monitoring Conference 2026 - November | Nuremberg -
https://osmc.de **
** NETWAYS Web Services - https://nws.netways.de **
** NETWAYS Trainings - https://netways.de/trainings **

Signature · 7 lines
Hector CAO
Software Engineer – Server Team / Virtualization
[email protected]
https://launc <https://launchpad.net/~hectorcao>hpad.net/~hectorcao
<https://launchpad.net/~hectorcao>

<https://launchpad.net/~hectorcao>