[PATCH 2/5] nodedev: Lock @priv sooner

Michal Privoznik mprivozn at redhat.com
Mon Apr 12 16:22:21 UTC 2021


The nodedev driver private data object @priv is created by
calling udevEventDataNew(). After that, driver->privateData
pointer is set to the freshly allocated object and only a few
lines after all of this the object is locked. Technically it is
safe because there should not be any other thread at this point,
but defensive style of programming says it's better if the object
is locked before driver's privateData is set.

Signed-off-by: Michal Privoznik <mprivozn at redhat.com>
---
 src/node_device/node_device_udev.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)

diff --git a/src/node_device/node_device_udev.c b/src/node_device/node_device_udev.c
index 04e1094e21..da3754ff80 100644
--- a/src/node_device/node_device_udev.c
+++ b/src/node_device/node_device_udev.c
@@ -2212,21 +2212,21 @@ nodeStateInitialize(bool privileged,
         !(priv = udevEventDataNew()))
         goto cleanup;
 
+    virObjectLock(priv);
+
     driver->privateData = priv;
     driver->nodeDeviceEventState = virObjectEventStateNew();
 
     if (udevPCITranslateInit(privileged) < 0)
-        goto cleanup;
+        goto unlock;
 
     udev = udev_new();
     if (!udev) {
         virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                        _("failed to create udev context"));
-        goto cleanup;
+        goto unlock;
     }
 
-    virObjectLock(priv);
-
     priv->udev_monitor = udev_monitor_new_from_netlink(udev, "udev");
     if (!priv->udev_monitor) {
         virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
-- 
2.26.3




More information about the libvir-list mailing list