linux_usbfs: Work around a driver binding race in reset handling
authorHans de Goede <hdegoede@redhat.com>
Thu, 13 Jun 2013 20:37:29 +0000 (22:37 +0200)
committerHans de Goede <hdegoede@redhat.com>
Wed, 19 Jun 2013 09:04:15 +0000 (11:04 +0200)
commit08befee03cff76f62918a82d92696aa84d01515c
tree05e75b397dc2a883587775c2c86b9dacfc4027ef
parentd4e993192aa81a1867bddf90d53cf750ec21e7b1
linux_usbfs: Work around a driver binding race in reset handling

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>
libusb/os/linux_usbfs.c
libusb/version_nano.h