aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/block
diff options
context:
space:
mode:
authorAsias He <asias@redhat.com>2012-05-04 08:22:04 -0400
committerRusty Russell <rusty@rustcorp.com.au>2012-05-21 22:46:13 -0400
commitb79d866c8b7014a51f611a64c40546109beaf24a (patch)
treeb20a73e60302c74e9a3e0663819cac6c52470c2b /drivers/block
parent90e03207f468e84258270ad07095ef50f925c17d (diff)
virtio-blk: Fix hot-unplug race in remove method
If we reset the virtio-blk device before the requests already dispatched to the virtio-blk driver from the block layer are finised, we will stuck in blk_cleanup_queue() and the remove will fail. blk_cleanup_queue() calls blk_drain_queue() to drain all requests queued before DEAD marking. However it will never success if the device is already stopped. We'll have q->in_flight[] > 0, so the drain will not finish. How to reproduce the race: 1. hot-plug a virtio-blk device 2. keep reading/writing the device in guest 3. hot-unplug while the device is busy serving I/O Test: ~1000 rounds of hot-plug/hot-unplug test passed with this patch. Changes in v3: - Drop blk_abort_queue and blk_abort_request - Use __blk_end_request_all to complete request dispatched to driver Changes in v2: - Drop req_in_flight - Use virtqueue_detach_unused_buf to get request dispatched to driver Signed-off-by: Asias He <asias@redhat.com> Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
Diffstat (limited to 'drivers/block')
-rw-r--r--drivers/block/virtio_blk.c11
1 files changed, 11 insertions, 0 deletions
diff --git a/drivers/block/virtio_blk.c b/drivers/block/virtio_blk.c
index 0d39f2f4294a..9d2223bba90c 100644
--- a/drivers/block/virtio_blk.c
+++ b/drivers/block/virtio_blk.c
@@ -583,6 +583,8 @@ static void __devexit virtblk_remove(struct virtio_device *vdev)
583{ 583{
584 struct virtio_blk *vblk = vdev->priv; 584 struct virtio_blk *vblk = vdev->priv;
585 int index = vblk->index; 585 int index = vblk->index;
586 struct virtblk_req *vbr;
587 unsigned long flags;
586 588
587 /* Prevent config work handler from accessing the device. */ 589 /* Prevent config work handler from accessing the device. */
588 mutex_lock(&vblk->config_lock); 590 mutex_lock(&vblk->config_lock);
@@ -598,6 +600,15 @@ static void __devexit virtblk_remove(struct virtio_device *vdev)
598 flush_work(&vblk->config_work); 600 flush_work(&vblk->config_work);
599 601
600 del_gendisk(vblk->disk); 602 del_gendisk(vblk->disk);
603
604 /* Abort requests dispatched to driver. */
605 spin_lock_irqsave(&vblk->lock, flags);
606 while ((vbr = virtqueue_detach_unused_buf(vblk->vq))) {
607 __blk_end_request_all(vbr->req, -EIO);
608 mempool_free(vbr, vblk->pool);
609 }
610 spin_unlock_irqrestore(&vblk->lock, flags);
611
601 blk_cleanup_queue(vblk->disk->queue); 612 blk_cleanup_queue(vblk->disk->queue);
602 put_disk(vblk->disk); 613 put_disk(vblk->disk);
603 mempool_destroy(vblk->pool); 614 mempool_destroy(vblk->pool);