Skip to content

Commit

Permalink
[PATCH] USB Storage: close a race condition in disconnect near queuec…
Browse files Browse the repository at this point in the history
…ommand

This patch started life as as534, and has been re-diffed against the latest
tree.

usb-storage has a small loophole, a window between the time queuecommand
accepts a new command and the time the control thread starts to execute
it.  If disconnect is called during that window, the driver won't cancel
the pending command -- we've been relying on the SCSI core to cancel it
for us during host removal.  But it's better for usb-storage to cancel
it;  this avoids races and reduces reliance on the SCSI core.
Fortunately cancelling these commands is easy to do; the key is to do it
_before_ calling scsi_remove_host.

Signed-off-by: Alan Stern <stern@rowland.harvard.edu>
Signed-off-by: Matthew Dharm <mdharm-usb@one-eyed-alien.net>
Signed-off-by: Greg Kroah-Hartman <gregkh@suse.de>
  • Loading branch information
Matthew Dharm authored and Greg Kroah-Hartman committed Sep 8, 2005
1 parent 77f4632 commit 26186ba
Showing 1 changed file with 13 additions and 0 deletions.
13 changes: 13 additions & 0 deletions drivers/usb/storage/usb.c
Original file line number Diff line number Diff line change
Expand Up @@ -833,6 +833,19 @@ static void quiesce_and_remove_host(struct us_data *us)
/* Wait for the current command to finish, then remove the host */
down(&us->dev_semaphore);
up(&us->dev_semaphore);

/* queuecommand won't accept any new commands and the control
* thread won't execute a previously-queued command. If there
* is such a command pending, complete it with an error. */
if (us->srb) {
us->srb->result = DID_NO_CONNECT << 16;
scsi_lock(us_to_host(us));
us->srb->scsi_done(us->srb);
us->srb = NULL;
scsi_unlock(us_to_host(us));
}

/* Now we own no commands so it's safe to remove the SCSI host */
scsi_remove_host(us_to_host(us));
}

Expand Down

0 comments on commit 26186ba

Please sign in to comment.