Skip to content

Commit a398d5e

Browse files
mbizonfreeboxgregkh
authored andcommitted
usb-storage: fix deadlock when a scsi command timeouts more than once
With faulty usb-storage devices, read/write can timeout, in that case the SCSI layer will abort and re-issue the command. USB storage has no internal timeout, it relies on SCSI layer aborting commands via .eh_abort_handler() for non those responsive devices. After two consecutive timeouts of the same command, SCSI layer calls .eh_device_reset_handler(), without calling .eh_abort_handler() first. With usb-storage, this causes a deadlock: -> .eh_device_reset_handler -> device_reset -> mutex_lock(&(us->dev_mutex)); mutex already by usb_stor_control_thread(), which is waiting for command completion: -> usb_stor_control_thread (mutex taken here) -> usb_stor_invoke_transport -> usb_stor_Bulk_transport -> usb_stor_bulk_srb -> usb_stor_bulk_transfer_sglist -> usb_sg_wait Make sure we cancel any pending command in .eh_device_reset_handler() to avoid this. Signed-off-by: Maxime Bizon <[email protected]> Cc: [email protected] Cc: stable <[email protected]> Link: https://lore.kernel.org/all/ZEllnjMKT8ulZbJh@sakura/ Reviewed-by: Alan Stern <[email protected]> Acked-by: Alan Stern <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Greg Kroah-Hartman <[email protected]>
1 parent 8018018 commit a398d5e

File tree

1 file changed

+21
-7
lines changed

1 file changed

+21
-7
lines changed

drivers/usb/storage/scsiglue.c

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -406,22 +406,25 @@ static DEF_SCSI_QCMD(queuecommand)
406406
***********************************************************************/
407407

408408
/* Command timeout and abort */
409-
static int command_abort(struct scsi_cmnd *srb)
409+
static int command_abort_matching(struct us_data *us, struct scsi_cmnd *srb_match)
410410
{
411-
struct us_data *us = host_to_us(srb->device->host);
412-
413-
usb_stor_dbg(us, "%s called\n", __func__);
414-
415411
/*
416412
* us->srb together with the TIMED_OUT, RESETTING, and ABORTING
417413
* bits are protected by the host lock.
418414
*/
419415
scsi_lock(us_to_host(us));
420416

421-
/* Is this command still active? */
422-
if (us->srb != srb) {
417+
/* is there any active pending command to abort ? */
418+
if (!us->srb) {
423419
scsi_unlock(us_to_host(us));
424420
usb_stor_dbg(us, "-- nothing to abort\n");
421+
return SUCCESS;
422+
}
423+
424+
/* Does the command match the passed srb if any ? */
425+
if (srb_match && us->srb != srb_match) {
426+
scsi_unlock(us_to_host(us));
427+
usb_stor_dbg(us, "-- pending command mismatch\n");
425428
return FAILED;
426429
}
427430

@@ -444,6 +447,14 @@ static int command_abort(struct scsi_cmnd *srb)
444447
return SUCCESS;
445448
}
446449

450+
static int command_abort(struct scsi_cmnd *srb)
451+
{
452+
struct us_data *us = host_to_us(srb->device->host);
453+
454+
usb_stor_dbg(us, "%s called\n", __func__);
455+
return command_abort_matching(us, srb);
456+
}
457+
447458
/*
448459
* This invokes the transport reset mechanism to reset the state of the
449460
* device
@@ -455,6 +466,9 @@ static int device_reset(struct scsi_cmnd *srb)
455466

456467
usb_stor_dbg(us, "%s called\n", __func__);
457468

469+
/* abort any pending command before reset */
470+
command_abort_matching(us, NULL);
471+
458472
/* lock the device pointers and do the reset */
459473
mutex_lock(&(us->dev_mutex));
460474
result = us->transport_reset(us);

0 commit comments

Comments
 (0)