nodedev: avoid delay when defining a new mdev

When calling virNodeDeviceDefineXML() to define a new mediated device,
we call virMdevctlDefine() and then wait for the new device to appear in
the driver's device list before returning. This caused long delays due
to the behavior of nodeDeviceFindNewMediatedDevice(). This function
checks to see if the device is in the list and then waits for 5s before
checking again.

Because mdevctl is relatively slow to query the list of defined
devices[0], the newly-defined device was generally not in the device
list when we first checked. This results in libvirt almost always taking
at least 5s to complete this API call for mediated devices, which is
unacceptable.

In order to avoid this long delay, we resort to a workaround. If the
call to virMdevctlDefine() was successful, we can assume that this new
device will exist the next time we query mdevctl for new devices. So we
simply add this provisional device definition directly to the nodedev
driver's device list and return from the function. At some point in the
future, the mdevctl handler will run and the "official" device will be
processed, which will update the provisional device if any new details
need to be added.

The reason that this is not necessary for virNodeDeviceCreateXML() is
because detecting newly-created (not defined) mdevs happens through
udev instead of mdevctl. And nodeDeviceFindNewMediatedDevice() always
calls 'udevadm settle' before checking to see whether the device is in
the list. This allows us to wait just long enough for all udev events to
be processed, so the device is almost always in the list the first time
we check and so we almost never end up hitting the 5s sleep.

[0] on my machine, 'mdevctl list --defined' took around 0.8s to
complete for only 3 defined mdevs.

Signed-off-by: Jonathon Jongsma <jjongsma@redhat.com>
Reviewed-by: Erik Skultety <eskultet@redhat.com>
This commit is contained in:
Jonathon Jongsma 2021-03-01 14:36:10 -06:00
parent 9e8e93dc6a
commit afda589d05

View File

@ -1281,10 +1281,10 @@ nodeDeviceDefineXML(virConnect *conn,
unsigned int flags)
{
g_autoptr(virNodeDeviceDef) def = NULL;
virNodeDevice *device = NULL;
const char *virt_type = NULL;
g_autofree char *uuid = NULL;
g_autofree char *errmsg = NULL;
g_autofree char *name = NULL;
virCheckFlags(0, NULL);
@ -1324,9 +1324,19 @@ nodeDeviceDefineXML(virConnect *conn,
}
mdevGenerateDeviceName(def);
device = nodeDeviceFindNewMediatedDevice(conn, def->caps->data.mdev.uuid);
name = g_strdup(def->name);
return device;
/* Normally we would call nodeDeviceFindNewMediatedDevice() here to wait
* for the new device to appear. But mdevctl can take a while to query
* devices, and if nodeDeviceFindNewMediatedDevice() doesn't find the new
* device immediately it will wait for 5s before checking again. Since we
* have already received the uuid from virMdevctlDefine(), we can simply
* add the provisional device to the list and return it immediately and
* avoid this long delay. */
if (nodeDeviceUpdateMediatedDevice(g_steal_pointer(&def)) < 0)
return NULL;
return virGetNodeDevice(conn, name);
}