usb-storage: fix deadlock involving host lock and scsi_done

Christoph Hellwig says that since version 4.12, the kernel switched to
using blk-mq by default.  The old code used a softirq for handling
request completions, but blk-mq can handle completions in the caller's
context.  This may cause a problem for usb-storage, because it invokes
the ->scsi_done callback while holding the host lock, and the
completion routine sometimes tries to acquire the same lock (when
running the error handler, for example).

The consequence is that the existing code will sometimes deadlock upon
error completion of a SCSI command (with a lockdep warning).

This is easy enough to fix, since usb-storage doesn't really need to
hold the host lock while the callback runs.  It was simpler to write
it that way, but moving the call outside the locked region is pretty
easy and there's no downside.  That's what this patch does.

Signed-off-by: Alan Stern <stern@rowland.harvard.edu>
Reported-and-tested-by: Arthur Marsh <arthur.marsh@internode.on.net>
CC: Christoph Hellwig <hch@lst.de>
CC: <stable@vger.kernel.org>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
This commit is contained in:
Alan Stern 2017-07-26 11:49:19 -04:00 коммит произвёл Greg Kroah-Hartman
Родитель 89f23d51de
Коммит 8b52291a07
1 изменённых файлов: 12 добавлений и 6 удалений

Просмотреть файл

@ -315,6 +315,7 @@ static int usb_stor_control_thread(void * __us)
{ {
struct us_data *us = (struct us_data *)__us; struct us_data *us = (struct us_data *)__us;
struct Scsi_Host *host = us_to_host(us); struct Scsi_Host *host = us_to_host(us);
struct scsi_cmnd *srb;
for (;;) { for (;;) {
usb_stor_dbg(us, "*** thread sleeping\n"); usb_stor_dbg(us, "*** thread sleeping\n");
@ -330,6 +331,7 @@ static int usb_stor_control_thread(void * __us)
scsi_lock(host); scsi_lock(host);
/* When we are called with no command pending, we're done */ /* When we are called with no command pending, we're done */
srb = us->srb;
if (us->srb == NULL) { if (us->srb == NULL) {
scsi_unlock(host); scsi_unlock(host);
mutex_unlock(&us->dev_mutex); mutex_unlock(&us->dev_mutex);
@ -398,14 +400,11 @@ static int usb_stor_control_thread(void * __us)
/* lock access to the state */ /* lock access to the state */
scsi_lock(host); scsi_lock(host);
/* indicate that the command is done */ /* was the command aborted? */
if (us->srb->result != DID_ABORT << 16) { if (us->srb->result == DID_ABORT << 16) {
usb_stor_dbg(us, "scsi cmd done, result=0x%x\n",
us->srb->result);
us->srb->scsi_done(us->srb);
} else {
SkipForAbort: SkipForAbort:
usb_stor_dbg(us, "scsi command aborted\n"); usb_stor_dbg(us, "scsi command aborted\n");
srb = NULL; /* Don't call srb->scsi_done() */
} }
/* /*
@ -429,6 +428,13 @@ SkipForAbort:
/* unlock the device pointers */ /* unlock the device pointers */
mutex_unlock(&us->dev_mutex); mutex_unlock(&us->dev_mutex);
/* now that the locks are released, notify the SCSI core */
if (srb) {
usb_stor_dbg(us, "scsi cmd done, result=0x%x\n",
srb->result);
srb->scsi_done(srb);
}
} /* for (;;) */ } /* for (;;) */
/* Wait until we are told to stop */ /* Wait until we are told to stop */