[PATCH v2] hw/usb/host-libusb: keep autoscan enabled for bus+addr selector

Timo Teräs posted 1 patch 2 days, 19 hours ago
Patches applied successfully (tree, apply log)
git fetch https://github.com/patchew-project/qemu tags/patchew/20260722-usbhost-disconnect-v2-1-41f3d55b49f0@iki.fi
hw/usb/host-libusb.c | 14 ++++----------
1 file changed, 4 insertions(+), 10 deletions(-)
[PATCH v2] hw/usb/host-libusb: keep autoscan enabled for bus+addr selector
Posted by Timo Teräs 2 days, 19 hours ago
This is partial revert of commit e058fa2dd599.

Add back and enable the autoscan functionality for bus+addr
selectors. This fixes a regression that USB device removal
was not propagated to the guest in all scenarios.

The exact behaviour likely depends on the host libusb
configuration and how the guest is using the USB device
(whether or not there is active URBs). The regression likely
concerns devices to which there is no active URBs all the time,
but the disconnection event is monitored: e.g. mass storage
devices to which URBs happen only during IO, but removal should
be seen even if there is no IO activity.

Fixes: e058fa2dd599 ("usb-host: add special case for bus+addr")
Resolves: https://gitlab.com/qemu-project/qemu/-/issues/3111
Acked-by: Gerd Hoffmann <kraxel@redhat.com>
Signed-off-by: Timo Teräs <timo.teras@iki.fi>
---
Changes in v2:
- Contrary the the in-between mail, this probably does not affect
  hotplug insertion: the hostaddr/device file would not stay the same.
  Thinking further, the regression likely depends on the usage pattern
  and whether the device is actively accessed or not.
- Updated commit message accordingly, no code changes
- Added Acked-by: Gerd Hoffmann <kraxel@redhat.com>
- Link to v1: https://lore.kernel.org/qemu-devel/20250912-usbhost-disconnect-v1-1-9c7c5fa9b50a@iki.fi

To: qemu-devel@nongnu.org
---
 hw/usb/host-libusb.c | 14 ++++----------
 1 file changed, 4 insertions(+), 10 deletions(-)

diff --git a/hw/usb/host-libusb.c b/hw/usb/host-libusb.c
index b74670ae25..f0baf6b2f2 100644
--- a/hw/usb/host-libusb.c
+++ b/hw/usb/host-libusb.c
@@ -90,7 +90,6 @@ struct USBHostDevice {
     uint32_t                         iso_urb_frames;
     uint32_t                         options;
     uint32_t                         loglevel;
-    bool                             needs_autoscan;
     bool                             allow_one_guest_reset;
     bool                             allow_all_guest_resets;
     bool                             suppress_remote_wake;
@@ -1211,7 +1210,6 @@ static void usb_host_realize(USBDevice *udev, Error **errp)
 #if LIBUSB_API_VERSION >= 0x01000107 && !defined(CONFIG_WIN32)
     if (s->hostdevice) {
         int fd;
-        s->needs_autoscan = false;
         fd = qemu_open(s->hostdevice, O_RDWR, errp);
         if (fd < 0) {
             return;
@@ -1227,7 +1225,6 @@ static void usb_host_realize(USBDevice *udev, Error **errp)
         !s->match.vendor_id &&
         !s->match.product_id &&
         !s->match.port) {
-        s->needs_autoscan = false;
         ldev = usb_host_find_ref(s->match.bus_num,
                                  s->match.addr);
         if (!ldev) {
@@ -1242,14 +1239,13 @@ static void usb_host_realize(USBDevice *udev, Error **errp)
                        s->match.bus_num, s->match.addr);
             return;
         }
-    } else {
-        s->needs_autoscan = true;
-        QTAILQ_INSERT_TAIL(&hostdevs, s, next);
-        usb_host_auto_check(NULL);
     }
 
     s->exit.notify = usb_host_exit_notifier;
     qemu_add_exit_notifier(&s->exit);
+
+    QTAILQ_INSERT_TAIL(&hostdevs, s, next);
+    usb_host_auto_check(NULL);
 }
 
 static void usb_host_instance_init(Object *obj)
@@ -1267,9 +1263,7 @@ static void usb_host_unrealize(USBDevice *udev)
     USBHostDevice *s = USB_HOST_DEVICE(udev);
 
     qemu_remove_exit_notifier(&s->exit);
-    if (s->needs_autoscan) {
-        QTAILQ_REMOVE(&hostdevs, s, next);
-    }
+    QTAILQ_REMOVE(&hostdevs, s, next);
     usb_host_close(s);
 }
 

---
base-commit: 190d5d7fd725ff754f94e8e0cbfb69f279c82b5d
change-id: 20250912-usbhost-disconnect-192ecaa8fc3e

Best regards,
--  
Timo Teräs <timo.teras@iki.fi>