]> git.proxmox.com Git - mirror_ubuntu-focal-kernel.git/blame - drivers/usb/usbip/stub_dev.c
usbip: usbip_host: fix stub_dev lock context imbalance regression
[mirror_ubuntu-focal-kernel.git] / drivers / usb / usbip / stub_dev.c
CommitLineData
5fd54ace 1// SPDX-License-Identifier: GPL-2.0+
4d7b5c7f
TH
2/*
3 * Copyright (C) 2003-2008 Takahiro Hirofuchi
4d7b5c7f
TH
4 */
5
7aaacb43 6#include <linux/device.h>
3d0a2a22 7#include <linux/file.h>
9720b4bc 8#include <linux/kthread.h>
99c97852 9#include <linux/module.h>
5a0e3ad6 10
4d7b5c7f
TH
11#include "usbip_common.h"
12#include "stub.h"
13
4d7b5c7f 14/*
d012c2a5 15 * usbip_status shows the status of usbip-host as long as this driver is bound
16 * to the target device.
4d7b5c7f 17 */
b1f56aca
GKH
18static ssize_t usbip_status_show(struct device *dev,
19 struct device_attribute *attr, char *buf)
4d7b5c7f
TH
20{
21 struct stub_device *sdev = dev_get_drvdata(dev);
22 int status;
23
24 if (!sdev) {
25 dev_err(dev, "sdev is null\n");
26 return -ENODEV;
27 }
28
dcf14779 29 spin_lock_irq(&sdev->ud.lock);
4d7b5c7f 30 status = sdev->ud.status;
dcf14779 31 spin_unlock_irq(&sdev->ud.lock);
4d7b5c7f
TH
32
33 return snprintf(buf, PAGE_SIZE, "%d\n", status);
34}
b1f56aca 35static DEVICE_ATTR_RO(usbip_status);
4d7b5c7f
TH
36
37/*
38 * usbip_sockfd gets a socket descriptor of an established TCP connection that
39 * is used to transfer usbip requests by kernel threads. -1 is a magic number
40 * by which usbip connection is finished.
41 */
ca35910a 42static ssize_t usbip_sockfd_store(struct device *dev, struct device_attribute *attr,
4d7b5c7f
TH
43 const char *buf, size_t count)
44{
45 struct stub_device *sdev = dev_get_drvdata(dev);
46 int sockfd = 0;
47 struct socket *socket;
f8cfc023 48 int rv;
4d7b5c7f
TH
49
50 if (!sdev) {
51 dev_err(dev, "sdev is null\n");
52 return -ENODEV;
53 }
54
f8cfc023
EO
55 rv = sscanf(buf, "%d", &sockfd);
56 if (rv != 1)
57 return -EINVAL;
4d7b5c7f
TH
58
59 if (sockfd != -1) {
964ea96e 60 int err;
3eed8c03 61
4d7b5c7f
TH
62 dev_info(dev, "stub up\n");
63
dcf14779 64 spin_lock_irq(&sdev->ud.lock);
4d7b5c7f
TH
65
66 if (sdev->ud.status != SDEV_ST_AVAILABLE) {
67 dev_err(dev, "not ready\n");
31398f63 68 goto err;
4d7b5c7f
TH
69 }
70
964ea96e 71 socket = sockfd_lookup(sockfd, &err);
31398f63
KK
72 if (!socket)
73 goto err;
74
4d7b5c7f 75 sdev->ud.tcp_socket = socket;
009f41ae 76 sdev->ud.sockfd = sockfd;
4d7b5c7f 77
dcf14779 78 spin_unlock_irq(&sdev->ud.lock);
4d7b5c7f 79
8c4e5834
KK
80 sdev->ud.tcp_rx = kthread_get_run(stub_rx_loop, &sdev->ud,
81 "stub_rx");
82 sdev->ud.tcp_tx = kthread_get_run(stub_tx_loop, &sdev->ud,
83 "stub_tx");
4d7b5c7f 84
dcf14779 85 spin_lock_irq(&sdev->ud.lock);
4d7b5c7f 86 sdev->ud.status = SDEV_ST_USED;
dcf14779 87 spin_unlock_irq(&sdev->ud.lock);
4d7b5c7f
TH
88
89 } else {
90 dev_info(dev, "stub down\n");
91
dcf14779 92 spin_lock_irq(&sdev->ud.lock);
31398f63
KK
93 if (sdev->ud.status != SDEV_ST_USED)
94 goto err;
95
dcf14779 96 spin_unlock_irq(&sdev->ud.lock);
4d7b5c7f
TH
97
98 usbip_event_add(&sdev->ud, SDEV_EVENT_DOWN);
99 }
100
101 return count;
31398f63
KK
102
103err:
104 spin_unlock_irq(&sdev->ud.lock);
964ea96e 105 return -EINVAL;
4d7b5c7f 106}
ca35910a 107static DEVICE_ATTR_WO(usbip_sockfd);
4d7b5c7f
TH
108
109static int stub_add_files(struct device *dev)
110{
111 int err = 0;
112
113 err = device_create_file(dev, &dev_attr_usbip_status);
114 if (err)
115 goto err_status;
116
117 err = device_create_file(dev, &dev_attr_usbip_sockfd);
118 if (err)
119 goto err_sockfd;
120
121 err = device_create_file(dev, &dev_attr_usbip_debug);
122 if (err)
123 goto err_debug;
124
125 return 0;
126
127err_debug:
128 device_remove_file(dev, &dev_attr_usbip_sockfd);
4d7b5c7f
TH
129err_sockfd:
130 device_remove_file(dev, &dev_attr_usbip_status);
4d7b5c7f
TH
131err_status:
132 return err;
133}
134
135static void stub_remove_files(struct device *dev)
136{
137 device_remove_file(dev, &dev_attr_usbip_status);
138 device_remove_file(dev, &dev_attr_usbip_sockfd);
139 device_remove_file(dev, &dev_attr_usbip_debug);
140}
141
4d7b5c7f
TH
142static void stub_shutdown_connection(struct usbip_device *ud)
143{
144 struct stub_device *sdev = container_of(ud, struct stub_device, ud);
145
146 /*
147 * When removing an exported device, kernel panic sometimes occurred
148 * and then EIP was sk_wait_data of stub_rx thread. Is this because
149 * sk_wait_data returned though stub_rx thread was already finished by
150 * step 1?
151 */
152 if (ud->tcp_socket) {
90120d15 153 dev_dbg(&sdev->udev->dev, "shutdown sockfd %d\n", ud->sockfd);
4d7b5c7f
TH
154 kernel_sock_shutdown(ud->tcp_socket, SHUT_RDWR);
155 }
156
157 /* 1. stop threads */
b7caecb8 158 if (ud->tcp_rx) {
ba46ce30 159 kthread_stop_put(ud->tcp_rx);
b7caecb8 160 ud->tcp_rx = NULL;
161 }
162 if (ud->tcp_tx) {
ba46ce30 163 kthread_stop_put(ud->tcp_tx);
b7caecb8 164 ud->tcp_tx = NULL;
165 }
4d7b5c7f 166
4d7b5c7f 167 /*
87352760 168 * 2. close the socket
169 *
170 * tcp_socket is freed after threads are killed so that usbip_xmit does
171 * not touch NULL socket.
4d7b5c7f
TH
172 */
173 if (ud->tcp_socket) {
964ea96e 174 sockfd_put(ud->tcp_socket);
4d7b5c7f 175 ud->tcp_socket = NULL;
009f41ae 176 ud->sockfd = -1;
4d7b5c7f
TH
177 }
178
179 /* 3. free used data */
180 stub_device_cleanup_urbs(sdev);
181
182 /* 4. free stub_unlink */
183 {
184 unsigned long flags;
185 struct stub_unlink *unlink, *tmp;
186
187 spin_lock_irqsave(&sdev->priv_lock, flags);
4d7b5c7f
TH
188 list_for_each_entry_safe(unlink, tmp, &sdev->unlink_tx, list) {
189 list_del(&unlink->list);
190 kfree(unlink);
191 }
87352760 192 list_for_each_entry_safe(unlink, tmp, &sdev->unlink_free,
193 list) {
4d7b5c7f
TH
194 list_del(&unlink->list);
195 kfree(unlink);
196 }
4d7b5c7f
TH
197 spin_unlock_irqrestore(&sdev->priv_lock, flags);
198 }
199}
200
201static void stub_device_reset(struct usbip_device *ud)
202{
203 struct stub_device *sdev = container_of(ud, struct stub_device, ud);
2d8f4595 204 struct usb_device *udev = sdev->udev;
4d7b5c7f
TH
205 int ret;
206
1a4b6f66 207 dev_dbg(&udev->dev, "device reset");
2d8f4595 208
8c7003a3 209 ret = usb_lock_device_for_reset(udev, NULL);
4d7b5c7f
TH
210 if (ret < 0) {
211 dev_err(&udev->dev, "lock for reset\n");
dcf14779 212 spin_lock_irq(&ud->lock);
4d7b5c7f 213 ud->status = SDEV_ST_ERROR;
dcf14779 214 spin_unlock_irq(&ud->lock);
4d7b5c7f
TH
215 return;
216 }
217
218 /* try to reset the device */
219 ret = usb_reset_device(udev);
4d7b5c7f
TH
220 usb_unlock_device(udev);
221
dcf14779 222 spin_lock_irq(&ud->lock);
4d7b5c7f
TH
223 if (ret) {
224 dev_err(&udev->dev, "device reset\n");
225 ud->status = SDEV_ST_ERROR;
4d7b5c7f
TH
226 } else {
227 dev_info(&udev->dev, "device reset\n");
228 ud->status = SDEV_ST_AVAILABLE;
4d7b5c7f 229 }
dcf14779 230 spin_unlock_irq(&ud->lock);
4d7b5c7f
TH
231}
232
233static void stub_device_unusable(struct usbip_device *ud)
234{
dcf14779 235 spin_lock_irq(&ud->lock);
4d7b5c7f 236 ud->status = SDEV_ST_ERROR;
dcf14779 237 spin_unlock_irq(&ud->lock);
4d7b5c7f
TH
238}
239
4d7b5c7f
TH
240/**
241 * stub_device_alloc - allocate a new stub_device struct
8c7003a3 242 * @udev: usb_device of a new device
4d7b5c7f
TH
243 *
244 * Allocates and initializes a new stub_device struct.
245 */
b7945b77 246static struct stub_device *stub_device_alloc(struct usb_device *udev)
4d7b5c7f
TH
247{
248 struct stub_device *sdev;
b7945b77
VM
249 int busnum = udev->bus->busnum;
250 int devnum = udev->devnum;
4d7b5c7f 251
b7945b77 252 dev_dbg(&udev->dev, "allocating stub device");
4d7b5c7f
TH
253
254 /* yes, it's a new device */
255 sdev = kzalloc(sizeof(struct stub_device), GFP_KERNEL);
78110bb8 256 if (!sdev)
4d7b5c7f 257 return NULL;
4d7b5c7f 258
2d8f4595 259 sdev->udev = usb_get_dev(udev);
4d7b5c7f
TH
260
261 /*
262 * devid is defined with devnum when this driver is first allocated.
263 * devnum may change later if a device is reset. However, devid never
264 * changes during a usbip connection.
265 */
b744a45f 266 sdev->devid = (busnum << 16) | devnum;
267 sdev->ud.side = USBIP_STUB;
268 sdev->ud.status = SDEV_ST_AVAILABLE;
4d7b5c7f 269 spin_lock_init(&sdev->ud.lock);
b744a45f 270 sdev->ud.tcp_socket = NULL;
009f41ae 271 sdev->ud.sockfd = -1;
4d7b5c7f
TH
272
273 INIT_LIST_HEAD(&sdev->priv_init);
274 INIT_LIST_HEAD(&sdev->priv_tx);
275 INIT_LIST_HEAD(&sdev->priv_free);
276 INIT_LIST_HEAD(&sdev->unlink_free);
277 INIT_LIST_HEAD(&sdev->unlink_tx);
4d7b5c7f
TH
278 spin_lock_init(&sdev->priv_lock);
279
280 init_waitqueue_head(&sdev->tx_waitq);
281
282 sdev->ud.eh_ops.shutdown = stub_shutdown_connection;
283 sdev->ud.eh_ops.reset = stub_device_reset;
284 sdev->ud.eh_ops.unusable = stub_device_unusable;
285
286 usbip_start_eh(&sdev->ud);
287
b7945b77 288 dev_dbg(&udev->dev, "register new device\n");
1a4b6f66 289
4d7b5c7f
TH
290 return sdev;
291}
292
b94b3a62 293static void stub_device_free(struct stub_device *sdev)
4d7b5c7f 294{
4d7b5c7f 295 kfree(sdev);
4d7b5c7f
TH
296}
297
b7945b77 298static int stub_probe(struct usb_device *udev)
4d7b5c7f 299{
4d7b5c7f 300 struct stub_device *sdev = NULL;
b7945b77 301 const char *udev_busid = dev_name(&udev->dev);
aa5873e9 302 struct bus_id_priv *busid_priv;
22076557 303 int rc = 0;
0c9e8b3c 304 char save_status;
4d7b5c7f 305
28b68acc 306 dev_dbg(&udev->dev, "Enter probe\n");
4d7b5c7f 307
0c9e8b3c
SK
308 /* Not sure if this is our device. Allocate here to avoid
309 * calling alloc while holding busid_table lock.
310 */
311 sdev = stub_device_alloc(udev);
312 if (!sdev)
313 return -ENOMEM;
314
4d7b5c7f 315 /* check we should claim or not by busid_table */
aa5873e9 316 busid_priv = get_busid_priv(udev_busid);
87352760 317 if (!busid_priv || (busid_priv->status == STUB_BUSID_REMOV) ||
b744a45f 318 (busid_priv->status == STUB_BUSID_OTHER)) {
b7945b77 319 dev_info(&udev->dev,
6165cc51
EW
320 "%s is not in match_busid table... skip!\n",
321 udev_busid);
4d7b5c7f
TH
322
323 /*
324 * Return value should be ENODEV or ENOXIO to continue trying
325 * other matched drivers by the driver core.
326 * See driver_probe_device() in driver/base/dd.c
327 */
22076557 328 rc = -ENODEV;
3ea3091f
SK
329 if (!busid_priv)
330 goto sdev_free;
331
332 goto call_put_busid_priv;
4d7b5c7f
TH
333 }
334
1a4b6f66 335 if (udev->descriptor.bDeviceClass == USB_CLASS_HUB) {
336 dev_dbg(&udev->dev, "%s is a usb hub device... skip!\n",
337 udev_busid);
22076557 338 rc = -ENODEV;
3ea3091f 339 goto call_put_busid_priv;
4d7b5c7f
TH
340 }
341
342 if (!strcmp(udev->bus->bus_name, "vhci_hcd")) {
6165cc51
EW
343 dev_dbg(&udev->dev,
344 "%s is attached on vhci_hcd... skip!\n",
345 udev_busid);
346
22076557 347 rc = -ENODEV;
3ea3091f 348 goto call_put_busid_priv;
4d7b5c7f
TH
349 }
350
4d7b5c7f 351
b7945b77
VM
352 dev_info(&udev->dev,
353 "usbip-host: register new device (bus %u dev %u)\n",
354 udev->bus->busnum, udev->devnum);
4d7b5c7f 355
aa5873e9
EK
356 busid_priv->shutdown_busid = 0;
357
b7945b77
VM
358 /* set private data to usb_device */
359 dev_set_drvdata(&udev->dev, sdev);
0c9e8b3c 360
aa5873e9 361 busid_priv->sdev = sdev;
a46034ca 362 busid_priv->udev = udev;
4d7b5c7f 363
0c9e8b3c
SK
364 save_status = busid_priv->status;
365 busid_priv->status = STUB_BUSID_ALLOC;
366
3ea3091f
SK
367 /* release the busid_lock */
368 put_busid_priv(busid_priv);
369
6080cd0e
VM
370 /*
371 * Claim this hub port.
372 * It doesn't matter what value we pass as owner
373 * (struct dev_state) as long as it is unique.
374 */
375 rc = usb_hub_claim_port(udev->parent, udev->portnum,
9b6f0c4b 376 (struct usb_dev_state *) udev);
6080cd0e
VM
377 if (rc) {
378 dev_dbg(&udev->dev, "unable to claim port\n");
3ff67445 379 goto err_port;
6080cd0e
VM
380 }
381
3ff67445
AK
382 rc = stub_add_files(&udev->dev);
383 if (rc) {
b7945b77 384 dev_err(&udev->dev, "stub_add_files for %s\n", udev_busid);
3ff67445 385 goto err_files;
4d7b5c7f
TH
386 }
387
0c9e8b3c 388 return 0;
22076557 389
3ff67445
AK
390err_files:
391 usb_hub_release_port(udev->parent, udev->portnum,
392 (struct usb_dev_state *) udev);
393err_port:
394 dev_set_drvdata(&udev->dev, NULL);
395 usb_put_dev(udev);
3ff67445 396
0c9e8b3c
SK
397 /* we already have busid_priv, just lock busid_lock */
398 spin_lock(&busid_priv->busid_lock);
3ff67445 399 busid_priv->sdev = NULL;
0c9e8b3c 400 busid_priv->status = save_status;
3ea3091f
SK
401 spin_unlock(&busid_priv->busid_lock);
402 /* lock is released - go to free */
403 goto sdev_free;
404
405call_put_busid_priv:
0c9e8b3c 406 /* release the busid_lock */
22076557 407 put_busid_priv(busid_priv);
0c9e8b3c 408
3ea3091f
SK
409sdev_free:
410 stub_device_free(sdev);
411
3ff67445 412 return rc;
4d7b5c7f
TH
413}
414
aa5873e9
EK
415static void shutdown_busid(struct bus_id_priv *busid_priv)
416{
0c9e8b3c 417 usbip_event_add(&busid_priv->sdev->ud, SDEV_EVENT_REMOVED);
aa5873e9 418
0c9e8b3c
SK
419 /* wait for the stop of the event handler */
420 usbip_stop_eh(&busid_priv->sdev->ud);
aa5873e9
EK
421}
422
4d7b5c7f
TH
423/*
424 * called in usb_disconnect() or usb_deregister()
425 * but only if actconfig(active configuration) exists
426 */
b7945b77 427static void stub_disconnect(struct usb_device *udev)
4d7b5c7f 428{
aa5873e9 429 struct stub_device *sdev;
b7945b77 430 const char *udev_busid = dev_name(&udev->dev);
aa5873e9 431 struct bus_id_priv *busid_priv;
6080cd0e 432 int rc;
aa5873e9 433
28b68acc 434 dev_dbg(&udev->dev, "Enter disconnect\n");
4d7b5c7f 435
1a4b6f66 436 busid_priv = get_busid_priv(udev_busid);
aa5873e9
EK
437 if (!busid_priv) {
438 BUG();
439 return;
440 }
441
b7945b77 442 sdev = dev_get_drvdata(&udev->dev);
aa5873e9 443
4d7b5c7f
TH
444 /* get stub_device */
445 if (!sdev) {
b7945b77 446 dev_err(&udev->dev, "could not get device");
3ea3091f
SK
447 /* release busid_lock */
448 put_busid_priv(busid_priv);
449 return;
4d7b5c7f
TH
450 }
451
b7945b77 452 dev_set_drvdata(&udev->dev, NULL);
4d7b5c7f 453
0c9e8b3c
SK
454 /* release busid_lock before call to remove device files */
455 put_busid_priv(busid_priv);
456
4d7b5c7f 457 /*
c7f00899 458 * NOTE: rx/tx threads are invoked for each usb_device.
4d7b5c7f 459 */
b7945b77 460 stub_remove_files(&udev->dev);
4d7b5c7f 461
6080cd0e
VM
462 /* release port */
463 rc = usb_hub_release_port(udev->parent, udev->portnum,
9b6f0c4b 464 (struct usb_dev_state *) udev);
6080cd0e
VM
465 if (rc) {
466 dev_dbg(&udev->dev, "unable to release port\n");
0c9e8b3c 467 return;
6080cd0e
VM
468 }
469
c7f00899 470 /* If usb reset is called from event handler */
bb7871ad 471 if (usbip_in_eh(current))
0c9e8b3c
SK
472 return;
473
474 /* we already have busid_priv, just lock busid_lock */
475 spin_lock(&busid_priv->busid_lock);
476 if (!busid_priv->shutdown_busid)
477 busid_priv->shutdown_busid = 1;
478 /* release busid_lock */
3ea3091f 479 spin_unlock(&busid_priv->busid_lock);
4d7b5c7f 480
c7f00899 481 /* shutdown the current connection */
aa5873e9 482 shutdown_busid(busid_priv);
4d7b5c7f 483
2d8f4595 484 usb_put_dev(sdev->udev);
2d8f4595 485
0c9e8b3c
SK
486 /* we already have busid_priv, just lock busid_lock */
487 spin_lock(&busid_priv->busid_lock);
c7f00899 488 /* free sdev */
aa5873e9 489 busid_priv->sdev = NULL;
4d7b5c7f
TH
490 stub_device_free(sdev);
491
7510df3f 492 if (busid_priv->status == STUB_BUSID_ALLOC)
aa5873e9 493 busid_priv->status = STUB_BUSID_ADDED;
0c9e8b3c 494 /* release busid_lock */
3ea3091f
SK
495 spin_unlock(&busid_priv->busid_lock);
496 return;
4d7b5c7f 497}
d012c2a5 498
b7945b77 499#ifdef CONFIG_PM
553a1a50 500
b7945b77
VM
501/* These functions need usb_port_suspend and usb_port_resume,
502 * which reside in drivers/usb/core/usb.h. Skip for now. */
503
504static int stub_suspend(struct usb_device *udev, pm_message_t message)
1aee199c 505{
b7945b77
VM
506 dev_dbg(&udev->dev, "stub_suspend\n");
507
1aee199c
AM
508 return 0;
509}
510
b7945b77 511static int stub_resume(struct usb_device *udev, pm_message_t message)
1aee199c 512{
b7945b77
VM
513 dev_dbg(&udev->dev, "stub_resume\n");
514
1aee199c
AM
515 return 0;
516}
517
b7945b77
VM
518#endif /* CONFIG_PM */
519
520struct usb_device_driver stub_driver = {
d012c2a5 521 .name = "usbip-host",
522 .probe = stub_probe,
523 .disconnect = stub_disconnect,
b7945b77
VM
524#ifdef CONFIG_PM
525 .suspend = stub_suspend,
526 .resume = stub_resume,
527#endif
528 .supports_autosuspend = 0,
97c451ca 529};