diff options
author | Hans de Goede <hdegoede@redhat.com> | 2012-07-06 14:35:53 +0200 |
---|---|---|
committer | Hans de Goede <hdegoede@redhat.com> | 2012-08-22 13:49:59 +0200 |
commit | 85123a6b42811b900033366047dafad5fd828ffc (patch) | |
tree | 2a9fda4b4dbe725698f8fe68ddbbfee527cf0d5c | |
parent | 5cd457aa57ab697ac0e1464398b9388542d94ed2 (diff) |
linux_usbfs: Work around a driver binding race in reset handlingdisconnect-claim
I've been seeing these intermittent failures to reclaim an interface after
a device reset. After much debugging and inserting sleeps in strategic
places to make the race window larger I've found the following race:
1) A user is running some software using libusbx which will automatically
detect, and "bind" to, any newly plugged in USB-devices. For example
a virtual machine viewer with automatic USB-redirection
2) The user plugs in a new usb-storage device
3) The usb-storage driver is not yet loaded, udev spawns
"modprobe usb-storage", this blocks on disk-io
4) The libusbx app opens the device, claims all interfaces, does a device-reset
5) While the IOCTL_USBFS_RESET is running the modprobe completes
6) The driver registration blocks on an USB lock held by the reset code path
7) When the reset finishes the driver registration completes and the driver
binds itself to the device, before IOCTL_USBFS_RESET returns to userspace
8) libusbx tries to re-claim all interfaces it had claimed before the reset
9) libusbx fails as usb-storage is now bound to it
This patch works around this issue by simply unbinding the driver for all
interfaces which were claimed before the reset. Normally this is a no-op as
no driver (other then usbfs) can be bound for claimed interfaces before the
reset.
This patch also improves the error logging, and makes libusb_device_reset
properly return an error when re-claiming fails.
Signed-off-by: Hans de Goede <hdegoede@redhat.com>
-rw-r--r-- | libusb/os/linux_usbfs.c | 15 |
1 files changed, 13 insertions, 2 deletions
diff --git a/libusb/os/linux_usbfs.c b/libusb/os/linux_usbfs.c index 9cadca0..e908c74 100644 --- a/libusb/os/linux_usbfs.c +++ b/libusb/os/linux_usbfs.c @@ -108,6 +108,10 @@ static int sysfs_can_relate_devices = 0; /* do we have a descriptors file? */ static int sysfs_has_descriptors = 0; +static int op_detach_kernel_driver_and_claim( + struct libusb_device_handle *handle, int interface, + const char *driver_name); + struct linux_device_priv { char *sysfs_dir; unsigned char *dev_descriptor; @@ -1497,11 +1501,18 @@ static int op_reset_device(struct libusb_device_handle *handle) /* And re-claim any interfaces which were claimed before the reset */ for (i = 0; i < USB_MAXINTERFACES; i++) { if (handle->claimed_interfaces & (1L << i)) { - r = op_claim_interface(handle, i); + /* + * A driver may have completed modprobing during + * IOCTL_USBFS_RESET, and bound itself as soon as + * IOCTL_USBFS_RESET released the device lock + */ + r = op_detach_kernel_driver_and_claim(handle, i, NULL); if (r) { usbi_warn(HANDLE_CTX(handle), - "failed to re-claim interface %d after reset", i); + "failed to re-claim interface %d after reset: %s", + i, libusb_error_name(r)); handle->claimed_interfaces &= ~(1L << i); + ret = LIBUSB_ERROR_NOT_FOUND; } } } |