Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

USB: usbcore: Introduce usb_bulk_msg_killable()

The synchronous message API in usbcore (usb_control_msg(),
usb_bulk_msg(), and so on) uses uninterruptible waits. However,
drivers may call these routines in the context of a user thread, which
means it ought to be possible to at least kill them.

For this reason, introduce a new usb_bulk_msg_killable() function
which behaves the same as usb_bulk_msg() except for using
wait_for_completion_killable_timeout() instead of
wait_for_completion_timeout(). The same can be done later for
usb_control_msg() later on, if it turns out to be needed.

Signed-off-by: Alan Stern <stern@rowland.harvard.edu>
Suggested-by: Oliver Neukum <oneukum@suse.com>
Link: https://lore.kernel.org/linux-usb/3acfe838-6334-4f6d-be7c-4bb01704b33d@rowland.harvard.edu/
Fixes: 1da177e4c3f4 ("Linux-2.6.12-rc2")
CC: stable@vger.kernel.org
Link: https://patch.msgid.link/248628b4-cc83-4e81-a620-3ce4e0376d41@rowland.harvard.edu
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>

authored by

Alan Stern and committed by
Greg Kroah-Hartman
41690996 e293015b

+72 -12
+69 -10
drivers/usb/core/message.c
··· 42 42 43 43 44 44 /* 45 - * Starts urb and waits for completion or timeout. Note that this call 46 - * is NOT interruptible. Many device driver i/o requests should be 47 - * interruptible and therefore these drivers should implement their 48 - * own interruptible routines. 45 + * Starts urb and waits for completion or timeout. 46 + * Whether or not the wait is killable depends on the flag passed in. 47 + * For example, compare usb_bulk_msg() and usb_bulk_msg_killable(). 49 48 */ 50 - static int usb_start_wait_urb(struct urb *urb, int timeout, int *actual_length) 49 + static int usb_start_wait_urb(struct urb *urb, int timeout, int *actual_length, 50 + bool killable) 51 51 { 52 52 struct api_context ctx; 53 53 unsigned long expire; 54 54 int retval; 55 + long rc; 55 56 56 57 init_completion(&ctx.done); 57 58 urb->context = &ctx; ··· 62 61 goto out; 63 62 64 63 expire = timeout ? msecs_to_jiffies(timeout) : MAX_SCHEDULE_TIMEOUT; 65 - if (!wait_for_completion_timeout(&ctx.done, expire)) { 64 + if (killable) 65 + rc = wait_for_completion_killable_timeout(&ctx.done, expire); 66 + else 67 + rc = wait_for_completion_timeout(&ctx.done, expire); 68 + if (rc <= 0) { 66 69 usb_kill_urb(urb); 67 - retval = (ctx.status == -ENOENT ? -ETIMEDOUT : ctx.status); 70 + if (ctx.status != -ENOENT) 71 + retval = ctx.status; 72 + else if (rc == 0) 73 + retval = -ETIMEDOUT; 74 + else 75 + retval = rc; 68 76 69 77 dev_dbg(&urb->dev->dev, 70 - "%s timed out on ep%d%s len=%u/%u\n", 78 + "%s timed out or killed on ep%d%s len=%u/%u\n", 71 79 current->comm, 72 80 usb_endpoint_num(&urb->ep->desc), 73 81 usb_urb_dir_in(urb) ? "in" : "out", ··· 110 100 usb_fill_control_urb(urb, usb_dev, pipe, (unsigned char *)cmd, data, 111 101 len, usb_api_blocking_completion, NULL); 112 102 113 - retv = usb_start_wait_urb(urb, timeout, &length); 103 + retv = usb_start_wait_urb(urb, timeout, &length, false); 114 104 if (retv < 0) 115 105 return retv; 116 106 else ··· 395 385 usb_fill_bulk_urb(urb, usb_dev, pipe, data, len, 396 386 usb_api_blocking_completion, NULL); 397 387 398 - return usb_start_wait_urb(urb, timeout, actual_length); 388 + return usb_start_wait_urb(urb, timeout, actual_length, false); 399 389 } 400 390 EXPORT_SYMBOL_GPL(usb_bulk_msg); 391 + 392 + /** 393 + * usb_bulk_msg_killable - Builds a bulk urb, sends it off and waits for completion in a killable state 394 + * @usb_dev: pointer to the usb device to send the message to 395 + * @pipe: endpoint "pipe" to send the message to 396 + * @data: pointer to the data to send 397 + * @len: length in bytes of the data to send 398 + * @actual_length: pointer to a location to put the actual length transferred 399 + * in bytes 400 + * @timeout: time in msecs to wait for the message to complete before 401 + * timing out (if 0 the wait is forever) 402 + * 403 + * Context: task context, might sleep. 404 + * 405 + * This function is just like usb_blk_msg() except that it waits in a 406 + * killable state. 407 + * 408 + * Return: 409 + * If successful, 0. Otherwise a negative error number. The number of actual 410 + * bytes transferred will be stored in the @actual_length parameter. 411 + * 412 + */ 413 + int usb_bulk_msg_killable(struct usb_device *usb_dev, unsigned int pipe, 414 + void *data, int len, int *actual_length, int timeout) 415 + { 416 + struct urb *urb; 417 + struct usb_host_endpoint *ep; 418 + 419 + ep = usb_pipe_endpoint(usb_dev, pipe); 420 + if (!ep || len < 0) 421 + return -EINVAL; 422 + 423 + urb = usb_alloc_urb(0, GFP_KERNEL); 424 + if (!urb) 425 + return -ENOMEM; 426 + 427 + if ((ep->desc.bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) == 428 + USB_ENDPOINT_XFER_INT) { 429 + pipe = (pipe & ~(3 << 30)) | (PIPE_INTERRUPT << 30); 430 + usb_fill_int_urb(urb, usb_dev, pipe, data, len, 431 + usb_api_blocking_completion, NULL, 432 + ep->desc.bInterval); 433 + } else 434 + usb_fill_bulk_urb(urb, usb_dev, pipe, data, len, 435 + usb_api_blocking_completion, NULL); 436 + 437 + return usb_start_wait_urb(urb, timeout, actual_length, true); 438 + } 439 + EXPORT_SYMBOL_GPL(usb_bulk_msg_killable); 401 440 402 441 /*-------------------------------------------------------------------*/ 403 442
+3 -2
include/linux/usb.h
··· 1868 1868 extern int usb_interrupt_msg(struct usb_device *usb_dev, unsigned int pipe, 1869 1869 void *data, int len, int *actual_length, int timeout); 1870 1870 extern int usb_bulk_msg(struct usb_device *usb_dev, unsigned int pipe, 1871 - void *data, int len, int *actual_length, 1872 - int timeout); 1871 + void *data, int len, int *actual_length, int timeout); 1872 + extern int usb_bulk_msg_killable(struct usb_device *usb_dev, unsigned int pipe, 1873 + void *data, int len, int *actual_length, int timeout); 1873 1874 1874 1875 /* wrappers around usb_control_msg() for the most common standard requests */ 1875 1876 int usb_control_msg_send(struct usb_device *dev, __u8 endpoint, __u8 request,