[libvirt PATCH] hypervisor: Call virWaitForDevices() after detaching host devices

Pavel Hrdina via Devel posted 1 patch 1 week ago
Patches applied successfully (tree, apply log)
git fetch https://github.com/patchew-project/libvirt tags/patchew/03d047bd50ada3fb573d83ebcd6018b8367d1ace.1774540324.git.phrdina@redhat.com
src/hypervisor/virhostdev.c | 3 +++
1 file changed, 3 insertions(+)
[libvirt PATCH] hypervisor: Call virWaitForDevices() after detaching host devices
Posted by Pavel Hrdina via Devel 1 week ago
From: Pavel Hrdina <phrdina@redhat.com>

On systems with selinux enabled starting a VM with managed host device
using IOMMUFD backend can run into race-condition where both libvirt and
udev are setting selinux label on /dev/vfio/devices/vfioX device. If
udev is the last one to set selinux label starting VM fails with:

error: internal error: QEMU unexpectedly closed the monitor (vm='test'): 2026-03-26T15:47:36.620422Z qemu-kvm: -device {"driver":"vfio-pci","id":"hostdev0","iommufd":"iommufd0","fd":"20","bus":"pci.7","addr":"0x0"}: vfio hostdev0: Failed to add fd 20 to KVM VFIO device: Invalid argument

We need to wait for udev to finish processing all events.

Signed-off-by: Pavel Hrdina <phrdina@redhat.com>
---
 src/hypervisor/virhostdev.c | 3 +++
 1 file changed, 3 insertions(+)

diff --git a/src/hypervisor/virhostdev.c b/src/hypervisor/virhostdev.c
index f4d2176204..b063a44f62 100644
--- a/src/hypervisor/virhostdev.c
+++ b/src/hypervisor/virhostdev.c
@@ -782,6 +782,9 @@ virHostdevPreparePCIDevicesImpl(virHostdevManager *mgr,
         }
     }
 
+    /* Step 2.5: Wait for udev to handle all events for devices. */
+    virWaitForDevices();
+
     /* At this point, all devices are attached to the stub driver and have
      * been marked as inactive */
 
-- 
2.53.0
Re: [libvirt PATCH] hypervisor: Call virWaitForDevices() after detaching host devices
Posted by Ján Tomko via Devel 6 days, 6 hours ago
On a Thursday in 2026, Pavel Hrdina via Devel wrote:
>From: Pavel Hrdina <phrdina@redhat.com>
>
>On systems with selinux enabled starting a VM with managed host device
>using IOMMUFD backend can run into race-condition where both libvirt and
>udev are setting selinux label on /dev/vfio/devices/vfioX device. If
>udev is the last one to set selinux label starting VM fails with:
>
>error: internal error: QEMU unexpectedly closed the monitor (vm='test'): 2026-03-26T15:47:36.620422Z qemu-kvm: -device {"driver":"vfio-pci","id":"hostdev0","iommufd":"iommufd0","fd":"20","bus":"pci.7","addr":"0x0"}: vfio hostdev0: Failed to add fd 20 to KVM VFIO device: Invalid argument
>
>We need to wait for udev to finish processing all events.
>
>Signed-off-by: Pavel Hrdina <phrdina@redhat.com>
>---
> src/hypervisor/virhostdev.c | 3 +++
> 1 file changed, 3 insertions(+)
>

Reviewed-by: Ján Tomko <jtomko@redhat.com>

Jano