-----Original Message----- From: linux-usb-owner@xxxxxxxxxxxxxxx [mailto:linux-usb-owner@xxxxxxxxxxxxxxx] On Behalf Of Alan Stern Sent: Friday, March 23, 2012 10:35 PM To: Greg KH Cc: Ming Lei; USB list Subject: [PATCH v.2] USB documentation: explain lifetime rules for unlinking URBs This patch (as1534b) updates the documentation for usb_unlink_urb and related functions. It explains that the caller must prevent the URB being unlinked from getting deallocated while the unlink is taking place. Signed-off-by: Alan Stern <stern@xxxxxxxxxxxxxxxxxxx> CC: Ming Lei <tom.leiming@xxxxxxxxx> --- Ver. 2: Update the URB.txt file to explain the proper way to avoid races between deallocation and unlinking. Update the kerneldoc to explain the problem without giving possibly inappropriate suggestions on how to fix it. Documentation/usb/URB.txt | 22 ++++++++++++++++++++++ drivers/usb/core/urb.c | 12 ++++++++++++ 2 files changed, 34 insertions(+) Index: usb-3.3/Documentation/usb/URB.txt =================================================================== --- usb-3.3.orig/Documentation/usb/URB.txt +++ usb-3.3/Documentation/usb/URB.txt @@ -168,6 +168,28 @@ that if the completion handler or anyone they will get a -EPERM error. Thus you can be sure that when usb_kill_urb() returns, the URB is totally idle. +There is a lifetime issue to consider. An URB may complete at any +time, and the completion handler may free the URB. If this happens +while usb_unlink_urb or usb_kill_urb is running, it will cause a +memory-access violation. The driver is responsible for avoiding this, +which often means some sort of lock will be needed to prevent the URB +from being deallocated while it is still in use. + +On the other hand, since usb_unlink_urb may end up calling the +completion handler, the handler must not take any lock that is held +when usb_unlink_urb is invoked. The general solution to this problem +is to increment the URB's reference count while holding the lock, then +drop the lock and call usb_unlink_urb or usb_unlink_urb, and then /\ || Hi Alan, Do you means "call usb_unlink_urb or usb_kill_urb," above? Thanks, Alex -- To unsubscribe from this list: send the line "unsubscribe linux-usb" in the body of a message to majordomo@xxxxxxxxxxxxxxx More majordomo info at http://vger.kernel.org/majordomo-info.html