Skip to contentSkip to conversation

virtio_blk: fix race between start and stop queue

PATCH In review Ming Lei 4 participants 10 messages May 15, 2014 – May 19, 2014

Subsystems (3)
lore
Share & tools
lore

Ming Lei

author
#0 lore raw

PATCH 1/1 virtio_blk: fix race between start and stop queue

When there isn't enough vring descriptor for adding to vq, blk-mq will be put as stopped state until some of pending descriptors are completed & freed.

Unfortunately, the vq's interrupt may come just before blk-mq's BLK_MQ_S_STOPPED flag is set, so the blk-mq will still be kept as stopped even though lots of descriptors are completed and freed in the interrupt handler. The worst case is that all pending descriptors are freed in the interrupt handler, and the queue is kept as stopped forever.

This patch fixes the problem by starting/stopping blk-mq with holding vq_lock.

Cc Jens Axboe <[email protected]>
Cc Rusty Russell <[email protected]>
---
 drivers/block/virtio_blk.c |   13 ++++++++++---
 1 file changed, 10 insertions(+), 3 deletions(-)
drivers/block/virtio_blk.c +10 -3
index 7a51f06..97f53ac 100644
@@ -42,6 +42,9 @@ struct virtio_blk
 	/* enable config space updates */
 	bool config_enable;
 
+	/* if the request queue is stopped, protected by vq_lock */
+	bool queue_stopped;
+
 	/* What host tells us, plus 2 for header & tailer. */
 	unsigned int sg_elems;
 
@@ -147,11 +150,13 @@ static void virtblk_done(struct virtqueue *vq)
 		if (unlikely(virtqueue_is_broken(vq)))
 			break;
 	} while (!virtqueue_enable_cb(vq));
-	spin_unlock_irqrestore(&vblk->vq_lock, flags);
 
 	/* In case queue is stopped waiting for more buffers. */
-	if (req_done)
+	if (req_done && vblk->queue_stopped) {
 		blk_mq_start_stopped_hw_queues(vblk->disk->queue, true);
+		vblk->queue_stopped = false;
+	}
+	spin_unlock_irqrestore(&vblk->vq_lock, flags);
 }
 
 static int virtio_queue_rq(struct blk_mq_hw_ctx *hctx, struct request *req)
@@ -205,8 +210,9 @@ static int virtio_queue_rq(struct blk_mq_hw_ctx *hctx, struct request *req)
 	err = __virtblk_add_req(vblk->vq, vbr, vbr->sg, num);
 	if (err) {
 		virtqueue_kick(vblk->vq);
-		spin_unlock_irqrestore(&vblk->vq_lock, flags);
 		blk_mq_stop_hw_queue(hctx);
+		vblk->queue_stopped = true;
+		spin_unlock_irqrestore(&vblk->vq_lock, flags);
 		/* Out of mem doesn't actually happen, since we fall back
 		 * to direct descriptors */
 		if (err == -ENOMEM || err == -ENOSPC)
@@ -598,6 +604,7 @@ static int virtblk_probe(struct virtio_device *vdev)
 	vblk->disk->fops = &virtblk_fops;
 	vblk->disk->driverfs_dev = &vdev->dev;
 	vblk->index = index;
+	vblk->queue_stopped = false;
 
 	/* configure queue flush support */
 	virtblk_update_cache_mode(vdev);
Signature · 1 line
1.7.9.5
maintainer
#1 lore raw
Quoted 11 lines · Ming Lei

When there isn't enough vring descriptor for adding to vq, blk-mq will be put as stopped state until some of pending descriptors are completed & freed.

Unfortunately, the vq's interrupt may come just before blk-mq's BLK_MQ_S_STOPPED flag is set, so the blk-mq will still be kept as stopped even though lots of descriptors are completed and freed in the interrupt handler. The worst case is that all pending descriptors are freed in the interrupt handler, and the queue is kept as stopped forever.

Quoted 2 lines

This patch fixes the problem by starting/stopping blk-mq with holding vq_lock.

Why not just use blk_mq_start_hw_queues()?

Signature · 1 line
Jens Axboe
#2 lore raw
Quoted 4 lines · Jens Axboe
Quoted 2 lines

This patch fixes the problem by starting/stopping blk-mq with holding vq_lock.

Why not just use blk_mq_start_hw_queues()?

That would kick off a work item on every I/O completion, something you didn't particularly like in scsi-mq :)

maintainer
#3 lore raw
Quoted 8 lines · Christoph Hellwig
Quoted 4 lines · Jens Axboe
Quoted 2 lines

This patch fixes the problem by starting/stopping blk-mq with holding vq_lock.

Why not just use blk_mq_start_hw_queues()?

That would kick off a work item on every I/O completion, something you didn't particularly like in scsi-mq :)

It would suck, I sent a followup after that one :-)

And yes, it still needs improving in scsi-mq too.

Signature · 1 line
Jens Axboe
maintainer
#4 lore raw
In reply to Jens Axboe
Quoted 12 lines · Jens Axboe
Quoted 11 lines · Ming Lei

When there isn't enough vring descriptor for adding to vq, blk-mq will be put as stopped state until some of pending descriptors are completed & freed.

Unfortunately, the vq's interrupt may come just before blk-mq's BLK_MQ_S_STOPPED flag is set, so the blk-mq will still be kept as stopped even though lots of descriptors are completed and freed in the interrupt handler. The worst case is that all pending descriptors are freed in the interrupt handler, and the queue is kept as stopped forever.

Quoted 4 lines
Quoted 2 lines

This patch fixes the problem by starting/stopping blk-mq with holding vq_lock.

Why not just use blk_mq_start_hw_queues()?

Or, if you want to maintain current heuristics, just move the start and stop under the vq_lock. That should prevent the race, as far as I can tell. Not sure what that extra queue_stopped would buy you, seems a lot cleaner to just maintain this state exclusively in the queue.

Signature · 1 line
Jens Axboe
drivers/block/virtio_blk.c +3 -2
index 7a51f065edcd..2e328231a795 100644
@@ -147,11 +147,12 @@ static void virtblk_done(struct virtqueue *vq)
 		if (unlikely(virtqueue_is_broken(vq)))
 			break;
 	} while (!virtqueue_enable_cb(vq));
-	spin_unlock_irqrestore(&vblk->vq_lock, flags);
 
 	/* In case queue is stopped waiting for more buffers. */
 	if (req_done)
 		blk_mq_start_stopped_hw_queues(vblk->disk->queue, true);
+
+	spin_unlock_irqrestore(&vblk->vq_lock, flags);
 }
 
 static int virtio_queue_rq(struct blk_mq_hw_ctx *hctx, struct request *req)
@@ -205,8 +206,8 @@ static int virtio_queue_rq(struct blk_mq_hw_ctx *hctx, struct request *req)
 	err = __virtblk_add_req(vblk->vq, vbr, vbr->sg, num);
 	if (err) {
 		virtqueue_kick(vblk->vq);
-		spin_unlock_irqrestore(&vblk->vq_lock, flags);
 		blk_mq_stop_hw_queue(hctx);
+		spin_unlock_irqrestore(&vblk->vq_lock, flags);
 		/* Out of mem doesn't actually happen, since we fall back
 		 * to direct descriptors */
 		if (err == -ENOMEM || err == -ENOSPC)
author
#5 lore raw
Quoted 19 lines · Jens Axboe
Quoted 18 lines · Jens Axboe
Quoted 14 lines · Ming Lei

When there isn't enough vring descriptor for adding to vq, blk-mq will be put as stopped state until some of pending descriptors are completed & freed.

Unfortunately, the vq's interrupt may come just before blk-mq's BLK_MQ_S_STOPPED flag is set, so the blk-mq will still be kept as stopped even though lots of descriptors are completed and freed in the interrupt handler. The worst case is that all pending descriptors are freed in the interrupt handler, and the queue is kept as stopped forever.

This patch fixes the problem by starting/stopping blk-mq with holding vq_lock.

Quoted 7 lines
Quoted 1 line

Why not just use blk_mq_start_hw_queues()?

Or, if you want to maintain current heuristics, just move the start and stop under the vq_lock. That should prevent the race, as far as I can tell. Not sure what that extra queue_stopped would buy you, seems a lot cleaner to just maintain this state exclusively in the queue.

Yes.

But the flag can avoid to call blk_mq_start_stopped_hw_queues() unnecessarily, which needn't at most of times. Considered that the interrupt may happen with very high frequency, I suggest to introduce the extra flag.

Thanks,

Signature · 1 line
Ming Lei
maintainer
#6 lore raw
Quoted 28 lines · Ming Lei
Quoted 19 lines · Jens Axboe
Quoted 18 lines · Jens Axboe
Quoted 14 lines · Ming Lei

When there isn't enough vring descriptor for adding to vq, blk-mq will be put as stopped state until some of pending descriptors are completed & freed.

Unfortunately, the vq's interrupt may come just before blk-mq's BLK_MQ_S_STOPPED flag is set, so the blk-mq will still be kept as stopped even though lots of descriptors are completed and freed in the interrupt handler. The worst case is that all pending descriptors are freed in the interrupt handler, and the queue is kept as stopped forever.

This patch fixes the problem by starting/stopping blk-mq with holding vq_lock.

Quoted 7 lines
Quoted 1 line

Why not just use blk_mq_start_hw_queues()?

Or, if you want to maintain current heuristics, just move the start and stop under the vq_lock. That should prevent the race, as far as I can tell. Not sure what that extra queue_stopped would buy you, seems a lot cleaner to just maintain this state exclusively in the queue.

Quoted 6 lines

Yes.

But the flag can avoid to call blk_mq_start_stopped_hw_queues() unnecessarily, which needn't at most of times. Considered that the interrupt may happen with very high frequency, I suggest to introduce the extra flag.

virtio-blk just has one queue, so the flag is at least pointless for now. And since the other code stops all of them anyway, I don't see any reason not to just rely on that.

Signature · 1 line
Jens Axboe
author
#7 lore raw
Quoted 6 lines · Jens Axboe

On 2014-05-16 09:15, Ming Lei wrote:

virtio-blk just has one queue, so the flag is at least pointless for now. And since the other code stops all of them anyway, I don't see any reason not to just rely on that.

OK, if you don't care the little loading of calling blk_mq_start_stopped_hw_queues() for each interrupt, I will remove the flag.

Thanks,

Signature · 1 line
Ming Lei
#8 lore raw
In reply to Ming Lei
Quoted 11 lines · Ming Lei

When there isn't enough vring descriptor for adding to vq, blk-mq will be put as stopped state until some of pending descriptors are completed & freed.

Unfortunately, the vq's interrupt may come just before blk-mq's BLK_MQ_S_STOPPED flag is set, so the blk-mq will still be kept as stopped even though lots of descriptors are completed and freed in the interrupt handler. The worst case is that all pending descriptors are freed in the interrupt handler, and the queue is kept as stopped forever.

Quoted 2 lines

This patch fixes the problem by starting/stopping blk-mq with holding vq_lock.

OK, but why the flag? Isn't moving the

        blk_mq_start_stopped_hw_queues(vblk->disk->queue, true);

inside the lock sufficient?

Cheers,
Rusty.

Quoted 59 lines
Cc Jens Axboe <[email protected]>
Cc Rusty Russell <[email protected]>
---
 drivers/block/virtio_blk.c |   13 ++++++++++---
 1 file changed, 10 insertions(+), 3 deletions(-)
drivers/block/virtio_blk.c +10 -3
index 7a51f06..97f53ac 100644
@@ -42,6 +42,9 @@ struct virtio_blk
 	/* enable config space updates */
 	bool config_enable;
 
+	/* if the request queue is stopped, protected by vq_lock */
+	bool queue_stopped;
+
 	/* What host tells us, plus 2 for header & tailer. */
 	unsigned int sg_elems;
 
@@ -147,11 +150,13 @@ static void virtblk_done(struct virtqueue *vq)
 		if (unlikely(virtqueue_is_broken(vq)))
 			break;
 	} while (!virtqueue_enable_cb(vq));
-	spin_unlock_irqrestore(&vblk->vq_lock, flags);
 
 	/* In case queue is stopped waiting for more buffers. */
-	if (req_done)
+	if (req_done && vblk->queue_stopped) {
 		blk_mq_start_stopped_hw_queues(vblk->disk->queue, true);
+		vblk->queue_stopped = false;
+	}
+	spin_unlock_irqrestore(&vblk->vq_lock, flags);
 }
 
 static int virtio_queue_rq(struct blk_mq_hw_ctx *hctx, struct request *req)
@@ -205,8 +210,9 @@ static int virtio_queue_rq(struct blk_mq_hw_ctx *hctx, struct request *req)
 	err = __virtblk_add_req(vblk->vq, vbr, vbr->sg, num);
 	if (err) {
 		virtqueue_kick(vblk->vq);
-		spin_unlock_irqrestore(&vblk->vq_lock, flags);
 		blk_mq_stop_hw_queue(hctx);
+		vblk->queue_stopped = true;
+		spin_unlock_irqrestore(&vblk->vq_lock, flags);
 		/* Out of mem doesn't actually happen, since we fall back
 		 * to direct descriptors */
 		if (err == -ENOMEM || err == -ENOSPC)
@@ -598,6 +604,7 @@ static int virtblk_probe(struct virtio_device *vdev)
 	vblk->disk->fops = &virtblk_fops;
 	vblk->disk->driverfs_dev = &vdev->dev;
 	vblk->index = index;
+	vblk->queue_stopped = false;
 
 	/* configure queue flush support */
 	virtblk_update_cache_mode(vdev);
Signature · 1 line
1.7.9.5
author
#9 lore raw
Quoted 12 lines · Rusty Russell
Quoted 11 lines · Ming Lei

When there isn't enough vring descriptor for adding to vq, blk-mq will be put as stopped state until some of pending descriptors are completed & freed.

Unfortunately, the vq's interrupt may come just before blk-mq's BLK_MQ_S_STOPPED flag is set, so the blk-mq will still be kept as stopped even though lots of descriptors are completed and freed in the interrupt handler. The worst case is that all pending descriptors are freed in the interrupt handler, and the queue is kept as stopped forever.

Quoted 7 lines
Quoted 2 lines

This patch fixes the problem by starting/stopping blk-mq with holding vq_lock.

OK, but why the flag? Isn't moving the

        blk_mq_start_stopped_hw_queues(vblk->disk->queue, true);

inside the lock sufficient?

The flag has been removed in v1. I introduced the flag just for avoiding unnecessary calling blk_mq_start_stopped_hw_queues() at most of time, but it isn't a big deal.

Thanks,

Signature · 1 line
Ming Lei