Skip to content

Commit 2ca359f

Browse files
oneukumgregkh
authored andcommitted
Revert "USB: rio500: simplify locking"
This reverts commit d710734. This simplification causes a deadlock. Reported-by: [email protected] Fixes: d710734 ("USB: rio500: simplify locking") Cc: stable <[email protected]> Signed-off-by: Oliver Neukum <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Greg Kroah-Hartman <[email protected]>
1 parent c43f28d commit 2ca359f

File tree

1 file changed

+27
-16
lines changed

1 file changed

+27
-16
lines changed

drivers/usb/misc/rio500.c

Lines changed: 27 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ struct rio_usb_data {
5151
char *obuf, *ibuf; /* transfer buffers */
5252
char bulk_in_ep, bulk_out_ep; /* Endpoint assignments */
5353
wait_queue_head_t wait_q; /* for timeouts */
54+
struct mutex lock; /* general race avoidance */
5455
};
5556

5657
static DEFINE_MUTEX(rio500_mutex);
@@ -62,15 +63,18 @@ static int open_rio(struct inode *inode, struct file *file)
6263

6364
/* against disconnect() */
6465
mutex_lock(&rio500_mutex);
66+
mutex_lock(&(rio->lock));
6567

6668
if (rio->isopen || !rio->present) {
69+
mutex_unlock(&(rio->lock));
6770
mutex_unlock(&rio500_mutex);
6871
return -EBUSY;
6972
}
7073
rio->isopen = 1;
7174

7275
init_waitqueue_head(&rio->wait_q);
7376

77+
mutex_unlock(&(rio->lock));
7478

7579
dev_info(&rio->rio_dev->dev, "Rio opened.\n");
7680
mutex_unlock(&rio500_mutex);
@@ -84,6 +88,7 @@ static int close_rio(struct inode *inode, struct file *file)
8488

8589
/* against disconnect() */
8690
mutex_lock(&rio500_mutex);
91+
mutex_lock(&(rio->lock));
8792

8893
rio->isopen = 0;
8994
if (!rio->present) {
@@ -95,6 +100,7 @@ static int close_rio(struct inode *inode, struct file *file)
95100
} else {
96101
dev_info(&rio->rio_dev->dev, "Rio closed.\n");
97102
}
103+
mutex_unlock(&(rio->lock));
98104
mutex_unlock(&rio500_mutex);
99105
return 0;
100106
}
@@ -109,7 +115,7 @@ static long ioctl_rio(struct file *file, unsigned int cmd, unsigned long arg)
109115
int retries;
110116
int retval=0;
111117

112-
mutex_lock(&rio500_mutex);
118+
mutex_lock(&(rio->lock));
113119
/* Sanity check to make sure rio is connected, powered, etc */
114120
if (rio->present == 0 || rio->rio_dev == NULL) {
115121
retval = -ENODEV;
@@ -253,7 +259,7 @@ static long ioctl_rio(struct file *file, unsigned int cmd, unsigned long arg)
253259

254260

255261
err_out:
256-
mutex_unlock(&rio500_mutex);
262+
mutex_unlock(&(rio->lock));
257263
return retval;
258264
}
259265

@@ -273,12 +279,12 @@ write_rio(struct file *file, const char __user *buffer,
273279
int errn = 0;
274280
int intr;
275281

276-
intr = mutex_lock_interruptible(&rio500_mutex);
282+
intr = mutex_lock_interruptible(&(rio->lock));
277283
if (intr)
278284
return -EINTR;
279285
/* Sanity check to make sure rio is connected, powered, etc */
280286
if (rio->present == 0 || rio->rio_dev == NULL) {
281-
mutex_unlock(&rio500_mutex);
287+
mutex_unlock(&(rio->lock));
282288
return -ENODEV;
283289
}
284290

@@ -301,7 +307,7 @@ write_rio(struct file *file, const char __user *buffer,
301307
goto error;
302308
}
303309
if (signal_pending(current)) {
304-
mutex_unlock(&rio500_mutex);
310+
mutex_unlock(&(rio->lock));
305311
return bytes_written ? bytes_written : -EINTR;
306312
}
307313

@@ -339,12 +345,12 @@ write_rio(struct file *file, const char __user *buffer,
339345
buffer += copy_size;
340346
} while (count > 0);
341347

342-
mutex_unlock(&rio500_mutex);
348+
mutex_unlock(&(rio->lock));
343349

344350
return bytes_written ? bytes_written : -EIO;
345351

346352
error:
347-
mutex_unlock(&rio500_mutex);
353+
mutex_unlock(&(rio->lock));
348354
return errn;
349355
}
350356

@@ -361,12 +367,12 @@ read_rio(struct file *file, char __user *buffer, size_t count, loff_t * ppos)
361367
char *ibuf;
362368
int intr;
363369

364-
intr = mutex_lock_interruptible(&rio500_mutex);
370+
intr = mutex_lock_interruptible(&(rio->lock));
365371
if (intr)
366372
return -EINTR;
367373
/* Sanity check to make sure rio is connected, powered, etc */
368374
if (rio->present == 0 || rio->rio_dev == NULL) {
369-
mutex_unlock(&rio500_mutex);
375+
mutex_unlock(&(rio->lock));
370376
return -ENODEV;
371377
}
372378

@@ -377,11 +383,11 @@ read_rio(struct file *file, char __user *buffer, size_t count, loff_t * ppos)
377383

378384
while (count > 0) {
379385
if (signal_pending(current)) {
380-
mutex_unlock(&rio500_mutex);
386+
mutex_unlock(&(rio->lock));
381387
return read_count ? read_count : -EINTR;
382388
}
383389
if (!rio->rio_dev) {
384-
mutex_unlock(&rio500_mutex);
390+
mutex_unlock(&(rio->lock));
385391
return -ENODEV;
386392
}
387393
this_read = (count >= IBUF_SIZE) ? IBUF_SIZE : count;
@@ -399,7 +405,7 @@ read_rio(struct file *file, char __user *buffer, size_t count, loff_t * ppos)
399405
count = this_read = partial;
400406
} else if (result == -ETIMEDOUT || result == 15) { /* FIXME: 15 ??? */
401407
if (!maxretry--) {
402-
mutex_unlock(&rio500_mutex);
408+
mutex_unlock(&(rio->lock));
403409
dev_err(&rio->rio_dev->dev,
404410
"read_rio: maxretry timeout\n");
405411
return -ETIME;
@@ -409,27 +415,27 @@ read_rio(struct file *file, char __user *buffer, size_t count, loff_t * ppos)
409415
finish_wait(&rio->wait_q, &wait);
410416
continue;
411417
} else if (result != -EREMOTEIO) {
412-
mutex_unlock(&rio500_mutex);
418+
mutex_unlock(&(rio->lock));
413419
dev_err(&rio->rio_dev->dev,
414420
"Read Whoops - result:%d partial:%u this_read:%u\n",
415421
result, partial, this_read);
416422
return -EIO;
417423
} else {
418-
mutex_unlock(&rio500_mutex);
424+
mutex_unlock(&(rio->lock));
419425
return (0);
420426
}
421427

422428
if (this_read) {
423429
if (copy_to_user(buffer, ibuf, this_read)) {
424-
mutex_unlock(&rio500_mutex);
430+
mutex_unlock(&(rio->lock));
425431
return -EFAULT;
426432
}
427433
count -= this_read;
428434
read_count += this_read;
429435
buffer += this_read;
430436
}
431437
}
432-
mutex_unlock(&rio500_mutex);
438+
mutex_unlock(&(rio->lock));
433439
return read_count;
434440
}
435441

@@ -494,6 +500,8 @@ static int probe_rio(struct usb_interface *intf,
494500
}
495501
dev_dbg(&intf->dev, "ibuf address:%p\n", rio->ibuf);
496502

503+
mutex_init(&(rio->lock));
504+
497505
usb_set_intfdata (intf, rio);
498506
rio->present = 1;
499507
bail_out:
@@ -511,10 +519,12 @@ static void disconnect_rio(struct usb_interface *intf)
511519
if (rio) {
512520
usb_deregister_dev(intf, &usb_rio_class);
513521

522+
mutex_lock(&(rio->lock));
514523
if (rio->isopen) {
515524
rio->isopen = 0;
516525
/* better let it finish - the release will do whats needed */
517526
rio->rio_dev = NULL;
527+
mutex_unlock(&(rio->lock));
518528
mutex_unlock(&rio500_mutex);
519529
return;
520530
}
@@ -524,6 +534,7 @@ static void disconnect_rio(struct usb_interface *intf)
524534
dev_info(&intf->dev, "USB Rio disconnected.\n");
525535

526536
rio->present = 0;
537+
mutex_unlock(&(rio->lock));
527538
}
528539
mutex_unlock(&rio500_mutex);
529540
}

0 commit comments

Comments
 (0)