diff options
author | Alan Stern <stern@rowland.harvard.edu> | 2015-01-21 14:02:43 -0500 |
---|---|---|
committer | Greg Kroah-Hartman <gregkh@linuxfoundation.org> | 2015-01-25 07:54:17 -0500 |
commit | 524134d422316a59d5464ccbc12036bbe90c5563 (patch) | |
tree | 2b13402e0012e1f021ae852097a4e7882223b619 /drivers/usb/core | |
parent | 9636c37843c9355c1ab6adcd8491186cbdc3b950 (diff) |
USB: don't cancel queued resets when unbinding drivers
The USB stack provides a mechanism for drivers to request an
asynchronous device reset (usb_queue_reset_device()). The mechanism
uses a work item (reset_ws) embedded in the usb_interface structure
used by the driver, and the reset is carried out by a work queue
routine.
The asynchronous reset can race with driver unbinding. When this
happens, we try to cancel the queued reset before unbinding the
driver, on the theory that the driver won't care about any resets once
it is unbound.
However, thanks to the fact that lockdep now tracks work queue
accesses, this can provoke a lockdep warning in situations where the
device reset causes another interface's driver to be unbound; see
http://marc.info/?l=linux-usb&m=141893165203776&w=2
for an example. The reason is that the work routine for reset_ws in
one interface calls cancel_queued_work() for the reset_ws in another
interface. Lockdep thinks this might lead to a work routine trying to
cancel itself. The simplest solution is not to cancel queued resets
when unbinding drivers.
This means we now need to acquire a reference to the usb_interface
when queuing a reset_ws work item and to drop the reference when the
work routine finishes. We also need to make sure that the
usb_interface structure doesn't outlive its parent usb_device; this
means acquiring and dropping a reference when the interface is created
and destroyed.
In addition, cancelling a queued reset can fail (if the device is in
the middle of an earlier reset), and this can cause usb_reset_device()
to try to rebind an interface that has been deallocated (see
http://marc.info/?l=linux-usb&m=142175717016628&w=2 for details).
Acquiring the extra references prevents this failure.
Signed-off-by: Alan Stern <stern@rowland.harvard.edu>
Reported-by: Russell King - ARM Linux <linux@arm.linux.org.uk>
Reported-by: Olivier Sobrie <olivier@sobrie.be>
Tested-by: Olivier Sobrie <olivier@sobrie.be>
Cc: stable <stable@vger.kernel.org> # 3.19
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Diffstat (limited to 'drivers/usb/core')
-rw-r--r-- | drivers/usb/core/driver.c | 17 | ||||
-rw-r--r-- | drivers/usb/core/hub.c | 25 | ||||
-rw-r--r-- | drivers/usb/core/message.c | 23 |
3 files changed, 12 insertions, 53 deletions
diff --git a/drivers/usb/core/driver.c b/drivers/usb/core/driver.c index 874dec31a111..c76ec9758ce3 100644 --- a/drivers/usb/core/driver.c +++ b/drivers/usb/core/driver.c | |||
@@ -275,21 +275,6 @@ static int usb_unbind_device(struct device *dev) | |||
275 | return 0; | 275 | return 0; |
276 | } | 276 | } |
277 | 277 | ||
278 | /* | ||
279 | * Cancel any pending scheduled resets | ||
280 | * | ||
281 | * [see usb_queue_reset_device()] | ||
282 | * | ||
283 | * Called after unconfiguring / when releasing interfaces. See | ||
284 | * comments in __usb_queue_reset_device() regarding | ||
285 | * udev->reset_running. | ||
286 | */ | ||
287 | static void usb_cancel_queued_reset(struct usb_interface *iface) | ||
288 | { | ||
289 | if (iface->reset_running == 0) | ||
290 | cancel_work_sync(&iface->reset_ws); | ||
291 | } | ||
292 | |||
293 | /* called from driver core with dev locked */ | 278 | /* called from driver core with dev locked */ |
294 | static int usb_probe_interface(struct device *dev) | 279 | static int usb_probe_interface(struct device *dev) |
295 | { | 280 | { |
@@ -380,7 +365,6 @@ static int usb_probe_interface(struct device *dev) | |||
380 | usb_set_intfdata(intf, NULL); | 365 | usb_set_intfdata(intf, NULL); |
381 | intf->needs_remote_wakeup = 0; | 366 | intf->needs_remote_wakeup = 0; |
382 | intf->condition = USB_INTERFACE_UNBOUND; | 367 | intf->condition = USB_INTERFACE_UNBOUND; |
383 | usb_cancel_queued_reset(intf); | ||
384 | 368 | ||
385 | /* If the LPM disable succeeded, balance the ref counts. */ | 369 | /* If the LPM disable succeeded, balance the ref counts. */ |
386 | if (!lpm_disable_error) | 370 | if (!lpm_disable_error) |
@@ -425,7 +409,6 @@ static int usb_unbind_interface(struct device *dev) | |||
425 | usb_disable_interface(udev, intf, false); | 409 | usb_disable_interface(udev, intf, false); |
426 | 410 | ||
427 | driver->disconnect(intf); | 411 | driver->disconnect(intf); |
428 | usb_cancel_queued_reset(intf); | ||
429 | 412 | ||
430 | /* Free streams */ | 413 | /* Free streams */ |
431 | for (i = 0, j = 0; i < intf->cur_altsetting->desc.bNumEndpoints; i++) { | 414 | for (i = 0, j = 0; i < intf->cur_altsetting->desc.bNumEndpoints; i++) { |
diff --git a/drivers/usb/core/hub.c b/drivers/usb/core/hub.c index aeb50bb6ba9c..b4bfa3ac4b12 100644 --- a/drivers/usb/core/hub.c +++ b/drivers/usb/core/hub.c | |||
@@ -5589,26 +5589,19 @@ EXPORT_SYMBOL_GPL(usb_reset_device); | |||
5589 | * possible; depending on how the driver attached to each interface | 5589 | * possible; depending on how the driver attached to each interface |
5590 | * handles ->pre_reset(), the second reset might happen or not. | 5590 | * handles ->pre_reset(), the second reset might happen or not. |
5591 | * | 5591 | * |
5592 | * - If a driver is unbound and it had a pending reset, the reset will | 5592 | * - If the reset is delayed so long that the interface is unbound from |
5593 | * be cancelled. | 5593 | * its driver, the reset will be skipped. |
5594 | * | 5594 | * |
5595 | * - This function can be called during .probe() or .disconnect() | 5595 | * - This function can be called during .probe(). It can also be called |
5596 | * times. On return from .disconnect(), any pending resets will be | 5596 | * during .disconnect(), but doing so is pointless because the reset |
5597 | * cancelled. | 5597 | * will not occur. If you really want to reset the device during |
5598 | * | 5598 | * .disconnect(), call usb_reset_device() directly -- but watch out |
5599 | * There is no no need to lock/unlock the @reset_ws as schedule_work() | 5599 | * for nested unbinding issues! |
5600 | * does its own. | ||
5601 | * | ||
5602 | * NOTE: We don't do any reference count tracking because it is not | ||
5603 | * needed. The lifecycle of the work_struct is tied to the | ||
5604 | * usb_interface. Before destroying the interface we cancel the | ||
5605 | * work_struct, so the fact that work_struct is queued and or | ||
5606 | * running means the interface (and thus, the device) exist and | ||
5607 | * are referenced. | ||
5608 | */ | 5600 | */ |
5609 | void usb_queue_reset_device(struct usb_interface *iface) | 5601 | void usb_queue_reset_device(struct usb_interface *iface) |
5610 | { | 5602 | { |
5611 | schedule_work(&iface->reset_ws); | 5603 | if (schedule_work(&iface->reset_ws)) |
5604 | usb_get_intf(iface); | ||
5612 | } | 5605 | } |
5613 | EXPORT_SYMBOL_GPL(usb_queue_reset_device); | 5606 | EXPORT_SYMBOL_GPL(usb_queue_reset_device); |
5614 | 5607 | ||
diff --git a/drivers/usb/core/message.c b/drivers/usb/core/message.c index f7b7713cfb2a..f368d2053da5 100644 --- a/drivers/usb/core/message.c +++ b/drivers/usb/core/message.c | |||
@@ -1551,6 +1551,7 @@ static void usb_release_interface(struct device *dev) | |||
1551 | altsetting_to_usb_interface_cache(intf->altsetting); | 1551 | altsetting_to_usb_interface_cache(intf->altsetting); |
1552 | 1552 | ||
1553 | kref_put(&intfc->ref, usb_release_interface_cache); | 1553 | kref_put(&intfc->ref, usb_release_interface_cache); |
1554 | usb_put_dev(interface_to_usbdev(intf)); | ||
1554 | kfree(intf); | 1555 | kfree(intf); |
1555 | } | 1556 | } |
1556 | 1557 | ||
@@ -1626,24 +1627,6 @@ static struct usb_interface_assoc_descriptor *find_iad(struct usb_device *dev, | |||
1626 | 1627 | ||
1627 | /* | 1628 | /* |
1628 | * Internal function to queue a device reset | 1629 | * Internal function to queue a device reset |
1629 | * | ||
1630 | * This is initialized into the workstruct in 'struct | ||
1631 | * usb_device->reset_ws' that is launched by | ||
1632 | * message.c:usb_set_configuration() when initializing each 'struct | ||
1633 | * usb_interface'. | ||
1634 | * | ||
1635 | * It is safe to get the USB device without reference counts because | ||
1636 | * the life cycle of @iface is bound to the life cycle of @udev. Then, | ||
1637 | * this function will be ran only if @iface is alive (and before | ||
1638 | * freeing it any scheduled instances of it will have been cancelled). | ||
1639 | * | ||
1640 | * We need to set a flag (usb_dev->reset_running) because when we call | ||
1641 | * the reset, the interfaces might be unbound. The current interface | ||
1642 | * cannot try to remove the queued work as it would cause a deadlock | ||
1643 | * (you cannot remove your work from within your executing | ||
1644 | * workqueue). This flag lets it know, so that | ||
1645 | * usb_cancel_queued_reset() doesn't try to do it. | ||
1646 | * | ||
1647 | * See usb_queue_reset_device() for more details | 1630 | * See usb_queue_reset_device() for more details |
1648 | */ | 1631 | */ |
1649 | static void __usb_queue_reset_device(struct work_struct *ws) | 1632 | static void __usb_queue_reset_device(struct work_struct *ws) |
@@ -1655,11 +1638,10 @@ static void __usb_queue_reset_device(struct work_struct *ws) | |||
1655 | 1638 | ||
1656 | rc = usb_lock_device_for_reset(udev, iface); | 1639 | rc = usb_lock_device_for_reset(udev, iface); |
1657 | if (rc >= 0) { | 1640 | if (rc >= 0) { |
1658 | iface->reset_running = 1; | ||
1659 | usb_reset_device(udev); | 1641 | usb_reset_device(udev); |
1660 | iface->reset_running = 0; | ||
1661 | usb_unlock_device(udev); | 1642 | usb_unlock_device(udev); |
1662 | } | 1643 | } |
1644 | usb_put_intf(iface); /* Undo _get_ in usb_queue_reset_device() */ | ||
1663 | } | 1645 | } |
1664 | 1646 | ||
1665 | 1647 | ||
@@ -1854,6 +1836,7 @@ free_interfaces: | |||
1854 | dev_set_name(&intf->dev, "%d-%s:%d.%d", | 1836 | dev_set_name(&intf->dev, "%d-%s:%d.%d", |
1855 | dev->bus->busnum, dev->devpath, | 1837 | dev->bus->busnum, dev->devpath, |
1856 | configuration, alt->desc.bInterfaceNumber); | 1838 | configuration, alt->desc.bInterfaceNumber); |
1839 | usb_get_dev(dev); | ||
1857 | } | 1840 | } |
1858 | kfree(new_interfaces); | 1841 | kfree(new_interfaces); |
1859 | 1842 | ||