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

usbip: auto retry for concurrent attach

This patch adds recovery from false busy state on concurrent attach
operation.

The procedure of attach operation is as below.
1) Find an unused port in /sys/devices/platform/vhci_hcd/status.
(userspace)
2) Request attach found port to driver through
/sys/devices/platform/vhci_hcd/attach. (userspace)
3) Lock table, reserve requested port and unlock table. (vhci driver)

Attaching more than one remote devices concurrently, same unused port
number will be found in step-1. Then one request will succeed and
others will fail even though there are some unused ports.

With this patch, driver returns EBUSY when requested port has already
been used. In this case, attach command retries from step-1: finding
another unused port. If there's no unused port, the attach operation
will fail in step-1. Otherwise it retries automatically using another
unused port.

vhci-hcd's interface (only errno) is changed as following.

Current errno New errno Condition
EINVAL same as left specified port number is in invalid
range
EAGAIN same as left platform_get_drvdata() failed
EINVAL same as left specified socket fd is not valid
EINVAL EBUSY specified port status is not free

The errno EBUSY was not used in userspace
src/usbip_attach.c:import_device(). It is needed to distinguish the
condition to be able to retry from other unrecoverable errors.

It is possible to avoid this failure by introducing userspace exclusive
control. But it's exaggerated for this special condition. The locking
itself has done in driver.
As an alternate solution, userspace doesn't specify port number, driver
searches unused port and it returns port number to the userspace. With
this solution, the interface is much different than this patch.

Signed-off-by: Nobuo Iwata <nobuo.iwata@fujixerox.co.jp>
Acked-by: Shuah Khan <shuahkh@osg.samsung.com>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>

authored by

Nobuo Iwata and committed by
Greg Kroah-Hartman
a38711a8 aa759365

+25 -16
+5 -1
drivers/usb/usbip/vhci_sysfs.c
··· 366 366 sockfd_put(socket); 367 367 368 368 dev_err(dev, "port %d already used\n", rhport); 369 - return -EINVAL; 369 + /* 370 + * Will be retried from userspace 371 + * if there's another free port. 372 + */ 373 + return -EBUSY; 370 374 } 371 375 372 376 dev_info(dev, "pdev(%u) rhport(%u) sockfd(%d)\n",
+20 -15
tools/usb/usbip/src/usbip_attach.c
··· 99 99 rc = usbip_vhci_driver_open(); 100 100 if (rc < 0) { 101 101 err("open vhci_driver"); 102 - return -1; 102 + goto err_out; 103 103 } 104 104 105 - port = usbip_vhci_get_free_port(speed); 106 - if (port < 0) { 107 - err("no free port"); 108 - usbip_vhci_driver_close(); 109 - return -1; 110 - } 105 + do { 106 + port = usbip_vhci_get_free_port(speed); 107 + if (port < 0) { 108 + err("no free port"); 109 + goto err_driver_close; 110 + } 111 111 112 - dbg("got free port %d", port); 112 + dbg("got free port %d", port); 113 113 114 - rc = usbip_vhci_attach_device(port, sockfd, udev->busnum, 115 - udev->devnum, udev->speed); 116 - if (rc < 0) { 117 - err("import device"); 118 - usbip_vhci_driver_close(); 119 - return -1; 120 - } 114 + rc = usbip_vhci_attach_device(port, sockfd, udev->busnum, 115 + udev->devnum, udev->speed); 116 + if (rc < 0 && errno != EBUSY) { 117 + err("import device"); 118 + goto err_driver_close; 119 + } 120 + } while (rc < 0); 121 121 122 122 usbip_vhci_driver_close(); 123 123 124 124 return port; 125 + 126 + err_driver_close: 127 + usbip_vhci_driver_close(); 128 + err_out: 129 + return -1; 125 130 } 126 131 127 132 static int query_import_device(int sockfd, char *busid)