提交 28795828 编写于 作者: S Serge Hallyn 提交者: Eric Blake

Change locking for udev monitor and callbacks

We're seeing bugs apparently resulting from thread unsafety of
libpciaccess, such as
https://bugs.launchpad.net/ubuntu/+source/libvirt/+bug/726099
To prevent those, as suggested by danpb on irc, move the
nodeDeviceLock(driverState) higher into the callers.  In
particular:

  udevDeviceMonitorStartup should hold the lock while calling
  udevEnumerateDevices(), and udevEventHandleCallback should hold it
  over its entire execution.

It's not clear to me whether it is ok to hold the
nodeDeviceLock while taking the virNodeDeviceObjLock(dev) on a
device.  If not, then the lock will need to be dropped around
the calling of udevSetupSystemDev(), and udevAddOneDevice()
may not actually be safe to call from higher layers with the
driverstate lock held.

libvirt 0.8.8 with this patch on it seems to work fine for me.
Assuming it looks ok and I haven't done anything obviously dumb,
I'll ask the bug submitters to try this patch.
Signed-off-by: NSerge Hallyn <serge.hallyn@ubuntu.com>
上级 25b39315
...@@ -1202,7 +1202,6 @@ static int udevRemoveOneDevice(struct udev_device *device) ...@@ -1202,7 +1202,6 @@ static int udevRemoveOneDevice(struct udev_device *device)
int ret = 0; int ret = 0;
name = udev_device_get_syspath(device); name = udev_device_get_syspath(device);
nodeDeviceLock(driverState);
dev = virNodeDeviceFindBySysfsPath(&driverState->devs, name); dev = virNodeDeviceFindBySysfsPath(&driverState->devs, name);
if (dev != NULL) { if (dev != NULL) {
...@@ -1214,7 +1213,6 @@ static int udevRemoveOneDevice(struct udev_device *device) ...@@ -1214,7 +1213,6 @@ static int udevRemoveOneDevice(struct udev_device *device)
name); name);
ret = -1; ret = -1;
} }
nodeDeviceUnlock(driverState);
return ret; return ret;
} }
...@@ -1316,9 +1314,7 @@ static int udevAddOneDevice(struct udev_device *device) ...@@ -1316,9 +1314,7 @@ static int udevAddOneDevice(struct udev_device *device)
/* If this is a device change, the old definition will be freed /* If this is a device change, the old definition will be freed
* and the current definition will take its place. */ * and the current definition will take its place. */
nodeDeviceLock(driverState);
dev = virNodeDeviceAssignDef(&driverState->devs, def); dev = virNodeDeviceAssignDef(&driverState->devs, def);
nodeDeviceUnlock(driverState);
if (dev == NULL) { if (dev == NULL) {
VIR_ERROR(_("Failed to create device for '%s'"), def->name); VIR_ERROR(_("Failed to create device for '%s'"), def->name);
...@@ -1442,6 +1438,7 @@ static void udevEventHandleCallback(int watch ATTRIBUTE_UNUSED, ...@@ -1442,6 +1438,7 @@ static void udevEventHandleCallback(int watch ATTRIBUTE_UNUSED,
const char *action = NULL; const char *action = NULL;
int udev_fd = -1; int udev_fd = -1;
nodeDeviceLock(driverState);
udev_fd = udev_monitor_get_fd(udev_monitor); udev_fd = udev_monitor_get_fd(udev_monitor);
if (fd != udev_fd) { if (fd != udev_fd) {
VIR_ERROR(_("File descriptor returned by udev %d does not " VIR_ERROR(_("File descriptor returned by udev %d does not "
...@@ -1470,6 +1467,7 @@ static void udevEventHandleCallback(int watch ATTRIBUTE_UNUSED, ...@@ -1470,6 +1467,7 @@ static void udevEventHandleCallback(int watch ATTRIBUTE_UNUSED,
out: out:
udev_device_unref(device); udev_device_unref(device);
nodeDeviceUnlock(driverState);
return; return;
} }
...@@ -1647,10 +1645,9 @@ static int udevDeviceMonitorStartup(int privileged) ...@@ -1647,10 +1645,9 @@ static int udevDeviceMonitorStartup(int privileged)
priv->udev_monitor = udev_monitor_new_from_netlink(udev, "udev"); priv->udev_monitor = udev_monitor_new_from_netlink(udev, "udev");
if (priv->udev_monitor == NULL) { if (priv->udev_monitor == NULL) {
VIR_FREE(priv); VIR_FREE(priv);
nodeDeviceUnlock(driverState);
VIR_ERROR0(_("udev_monitor_new_from_netlink returned NULL")); VIR_ERROR0(_("udev_monitor_new_from_netlink returned NULL"));
ret = -1; ret = -1;
goto out; goto out_unlock;
} }
udev_monitor_enable_receiving(priv->udev_monitor); udev_monitor_enable_receiving(priv->udev_monitor);
...@@ -1670,26 +1667,26 @@ static int udevDeviceMonitorStartup(int privileged) ...@@ -1670,26 +1667,26 @@ static int udevDeviceMonitorStartup(int privileged)
VIR_EVENT_HANDLE_READABLE, VIR_EVENT_HANDLE_READABLE,
udevEventHandleCallback, NULL, NULL); udevEventHandleCallback, NULL, NULL);
if (priv->watch == -1) { if (priv->watch == -1) {
nodeDeviceUnlock(driverState);
ret = -1; ret = -1;
goto out; goto out_unlock;
} }
nodeDeviceUnlock(driverState);
/* Create a fictional 'computer' device to root the device tree. */ /* Create a fictional 'computer' device to root the device tree. */
if (udevSetupSystemDev() != 0) { if (udevSetupSystemDev() != 0) {
ret = -1; ret = -1;
goto out; goto out_unlock;
} }
/* Populate with known devices */ /* Populate with known devices */
if (udevEnumerateDevices(udev) != 0) { if (udevEnumerateDevices(udev) != 0) {
ret = -1; ret = -1;
goto out; goto out_unlock;
} }
out_unlock:
nodeDeviceUnlock(driverState);
out: out:
if (ret == -1) { if (ret == -1) {
udevDeviceMonitorShutdown(); udevDeviceMonitorShutdown();
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册