Skip to contentSkip to conversation

virtio_blk: Fix disk deletion hang on device surprise removal

PATCH Superseded Parav Pandit 4 participants 20 messages May 21, 2025 – May 26, 2025

Subsystems (4)

v1 of 5 Superseded Latest is v5 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 virtio_blk: Fix disk deletion hang on device surprise removal

When the PCI device is surprise removed, requests may not complete the device as the VQ is marked as broken. Due to this, the disk deletion hangs.

Fix it by aborting the requests when the VQ is broken.

With this fix now fio completes swiftly.
An alternative of IO timeout has been considered, however when the driver knows about unresponsive block device, swiftly clearing them enables users and upper layers to react quickly.

Verified with multiple device unplug iterations with pending requests in virtio used ring and some pending with the device.

Fixes 43bb40c5b926 ("virtio_pci: Support surprise removal of virtio pci device")
Reported-by [email protected]

---
changelog:
v0->v1:
- Fixed comments from Stefan to rename a cleanup function
- Improved logic for handling any outstanding requests in bio layer
- improved cancel callback to sync with ongoing done()

---
 drivers/block/virtio_blk.c | 95 ++++++++++++++++++++++++++++++++++++++
 1 file changed, 95 insertions(+)
drivers/block/virtio_blk.c +95 -0
index 7cffea01d868..5212afdbd3c7 100644
@@ -435,6 +435,13 @@ static blk_status_t virtio_queue_rq(struct blk_mq_hw_ctx *hctx,
 	blk_status_t status;
 	int err;
 
+	/* Immediately fail all incoming requests if the vq is broken.
+	 * Once the queue is unquiesced, upper block layer flushes any pending
+	 * queued requests; fail them right away.
+	 */
+	if (unlikely(virtqueue_is_broken(vblk->vqs[qid].vq)))
+		return BLK_STS_IOERR;
+
 	status = virtblk_prep_rq(hctx, vblk, req, vbr);
 	if (unlikely(status))
 		return status;
@@ -508,6 +515,11 @@ static void virtio_queue_rqs(struct rq_list *rqlist)
 	while ((req = rq_list_pop(rqlist))) {
 		struct virtio_blk_vq *this_vq = get_virtio_blk_vq(req->mq_hctx);
 
+		if (unlikely(virtqueue_is_broken(this_vq->vq))) {
+			rq_list_add_tail(&requeue_list, req);
+			continue;
+		}
+
 		if (vq && vq != this_vq)
 			virtblk_add_req_batch(vq, &submit_list);
 		vq = this_vq;
@@ -1554,6 +1566,87 @@ static int virtblk_probe(struct virtio_device *vdev)
 	return err;
 }
 
+static bool virtblk_request_cancel(struct request *rq, void *data)
+{
+	struct virtblk_req *vbr = blk_mq_rq_to_pdu(rq);
+	struct virtio_blk *vblk = data;
+	struct virtio_blk_vq *vq;
+	unsigned long flags;
+
+	vq = &vblk->vqs[rq->mq_hctx->queue_num];
+
+	spin_lock_irqsave(&vq->lock, flags);
+
+	vbr->in_hdr.status = VIRTIO_BLK_S_IOERR;
+	if (blk_mq_request_started(rq) && !blk_mq_request_completed(rq))
+		blk_mq_complete_request(rq);
+
+	spin_unlock_irqrestore(&vq->lock, flags);
+	return true;
+}
+
+static void virtblk_broken_device_cleanup(struct virtio_blk *vblk)
+{
+	struct request_queue *q = vblk->disk->queue;
+
+	if (!virtqueue_is_broken(vblk->vqs[0].vq))
+		return;
+
+	/* Start freezing the queue, so that new requests keeps waitng at the
+	 * door of bio_queue_enter(). We cannot fully freeze the queue because
+	 * freezed queue is an empty queue and there are pending requests, so
+	 * only start freezing it.
+	 */
+	blk_freeze_queue_start(q);
+
+	/* When quiescing completes, all ongoing dispatches have completed
+	 * and no new dispatch will happen towards the driver.
+	 * This ensures that later when cancel is attempted, then are not
+	 * getting processed by the queue_rq() or queue_rqs() handlers.
+	 */
+	blk_mq_quiesce_queue(q);
+
+	/*
+	 * Synchronize with any ongoing VQ callbacks, effectively quiescing
+	 * the device and preventing it from completing further requests
+	 * to the block layer. Any outstanding, incomplete requests will be
+	 * completed by virtblk_request_cancel().
+	 */
+	virtio_synchronize_cbs(vblk->vdev);
+
+	/* At this point, no new requests can enter the queue_rq() and
+	 * completion routine will not complete any new requests either for the
+	 * broken vq. Hence, it is safe to cancel all requests which are
+	 * started.
+	 */
+	blk_mq_tagset_busy_iter(&vblk->tag_set, virtblk_request_cancel, vblk);
+	blk_mq_tagset_wait_completed_request(&vblk->tag_set);
+
+	/* All pending requests are cleaned up. Time to resume so that disk
+	 * deletion can be smooth. Start the HW queues so that when queue is
+	 * unquiesced requests can again enter the driver.
+	 */
+	blk_mq_start_stopped_hw_queues(q, true);
+
+	/* Unquiescing will trigger dispatching any pending requests to the
+	 * driver which has crossed bio_queue_enter() to the driver.
+	 */
+	blk_mq_unquiesce_queue(q);
+
+	/* Wait for all pending dispatches to terminate which may have been
+	 * initiated after unquiescing.
+	 */
+	blk_mq_freeze_queue_wait(q);
+
+	/* Mark the disk dead so that once queue unfreeze, the requests
+	 * waiting at the door of bio_queue_enter() can be aborted right away.
+	 */
+	blk_mark_disk_dead(vblk->disk);
+
+	/* Unfreeze the queue so that any waiting requests will be aborted. */
+	blk_mq_unfreeze_queue_nomemrestore(q);
+}
+
 static void virtblk_remove(struct virtio_device *vdev)
 {
 	struct virtio_blk *vblk = vdev->priv;
@@ -1561,6 +1654,8 @@ static void virtblk_remove(struct virtio_device *vdev)
 	/* Make sure no work handler is accessing the device. */
 	flush_work(&vblk->config_work);
 
+	virtblk_broken_device_cleanup(vblk);
+
 	del_gendisk(vblk->disk);
 	blk_mq_free_tag_set(&vblk->tag_set);
 
Signature · 1 line
2.34.1
#1 lore raw
Quoted 21 lines · Parav Pandit

When the PCI device is surprise removed, requests may not complete the device as the VQ is marked as broken. Due to this, the disk deletion hangs.

Fix it by aborting the requests when the VQ is broken.

With this fix now fio completes swiftly.
An alternative of IO timeout has been considered, however when the driver knows about unresponsive block device, swiftly clearing them enables users and upper layers to react quickly.

Verified with multiple device unplug iterations with pending requests in virtio used ring and some pending with the device.

Fixes 43bb40c5b926 ("virtio_pci: Support surprise removal of virtio pci device")
Reported-by [email protected]
Quoted 7 lines

---
changelog:
v0->v1:
- Fixed comments from Stefan to rename a cleanup function
- Improved logic for handling any outstanding requests in bio layer
- improved cancel callback to sync with ongoing done()

thanks for the patch!
questions:

Quoted 12 lines
---
 drivers/block/virtio_blk.c | 95 ++++++++++++++++++++++++++++++++++++++
 1 file changed, 95 insertions(+)
drivers/block/virtio_blk.c +0 -0
index 7cffea01d868..5212afdbd3c7 100644
@@ -435,6 +435,13 @@ static blk_status_t virtio_queue_rq(struct blk_mq_hw_ctx *hctx,
 	blk_status_t status;
 	int err;
 

drivers/block/virtio_blk.c static blk_status_t virtio_queue_rq(struct blk_mq_hw_ctx *hctx, · line 447

Quoted 10 lines
drivers/block/virtio_blk.c +7 -0
+	/* Immediately fail all incoming requests if the vq is broken.
+	 * Once the queue is unquiesced, upper block layer flushes any pending
+	 * queued requests; fail them right away.
+	 */
+	if (unlikely(virtqueue_is_broken(vblk->vqs[qid].vq)))
+		return BLK_STS_IOERR;
+
 	status = virtblk_prep_rq(hctx, vblk, req, vbr);
 	if (unlikely(status))
 		return status;

just below this:

        spin_lock_irqsave(&vblk->vqs[qid].lock, flags);
        err = virtblk_add_req(vblk->vqs[qid].vq, vbr);
        if (err) {

and virtblk_add_req calls virtqueue_add_sgs, so it will fail on a broken vq.

Why do we need to check it one extra time here?

drivers/block/virtio_blk.c static void virtio_queue_rqs(struct rq_list *rqlist) · line 525

Quoted 12 lines
drivers/block/virtio_blk.c +5 -0
@@ -508,6 +515,11 @@ static void virtio_queue_rqs(struct rq_list *rqlist)
 	while ((req = rq_list_pop(rqlist))) {
 		struct virtio_blk_vq *this_vq = get_virtio_blk_vq(req->mq_hctx);
 
+		if (unlikely(virtqueue_is_broken(this_vq->vq))) {
+			rq_list_add_tail(&requeue_list, req);
+			continue;
+		}
+
 		if (vq && vq != this_vq)
 			virtblk_add_req_batch(vq, &submit_list);
 		vq = this_vq;

similarly

Quoted 99 lines
drivers/block/virtio_blk.c +83 -0
@@ -1554,6 +1566,87 @@ static int virtblk_probe(struct virtio_device *vdev)
 	return err;
 }
 
+static bool virtblk_request_cancel(struct request *rq, void *data)
+{
+	struct virtblk_req *vbr = blk_mq_rq_to_pdu(rq);
+	struct virtio_blk *vblk = data;
+	struct virtio_blk_vq *vq;
+	unsigned long flags;
+
+	vq = &vblk->vqs[rq->mq_hctx->queue_num];
+
+	spin_lock_irqsave(&vq->lock, flags);
+
+	vbr->in_hdr.status = VIRTIO_BLK_S_IOERR;
+	if (blk_mq_request_started(rq) && !blk_mq_request_completed(rq))
+		blk_mq_complete_request(rq);
+
+	spin_unlock_irqrestore(&vq->lock, flags);
+	return true;
+}
+
+static void virtblk_broken_device_cleanup(struct virtio_blk *vblk)
+{
+	struct request_queue *q = vblk->disk->queue;
+
+	if (!virtqueue_is_broken(vblk->vqs[0].vq))
+		return;
+
+	/* Start freezing the queue, so that new requests keeps waitng at the
+	 * door of bio_queue_enter(). We cannot fully freeze the queue because
+	 * freezed queue is an empty queue and there are pending requests, so
+	 * only start freezing it.
+	 */
+	blk_freeze_queue_start(q);
+
+	/* When quiescing completes, all ongoing dispatches have completed
+	 * and no new dispatch will happen towards the driver.
+	 * This ensures that later when cancel is attempted, then are not
+	 * getting processed by the queue_rq() or queue_rqs() handlers.
+	 */
+	blk_mq_quiesce_queue(q);
+
+	/*
+	 * Synchronize with any ongoing VQ callbacks, effectively quiescing
+	 * the device and preventing it from completing further requests
+	 * to the block layer. Any outstanding, incomplete requests will be
+	 * completed by virtblk_request_cancel().
+	 */
+	virtio_synchronize_cbs(vblk->vdev);
+
+	/* At this point, no new requests can enter the queue_rq() and
+	 * completion routine will not complete any new requests either for the
+	 * broken vq. Hence, it is safe to cancel all requests which are
+	 * started.
+	 */
+	blk_mq_tagset_busy_iter(&vblk->tag_set, virtblk_request_cancel, vblk);
+	blk_mq_tagset_wait_completed_request(&vblk->tag_set);
+
+	/* All pending requests are cleaned up. Time to resume so that disk
+	 * deletion can be smooth. Start the HW queues so that when queue is
+	 * unquiesced requests can again enter the driver.
+	 */
+	blk_mq_start_stopped_hw_queues(q, true);
+
+	/* Unquiescing will trigger dispatching any pending requests to the
+	 * driver which has crossed bio_queue_enter() to the driver.
+	 */
+	blk_mq_unquiesce_queue(q);
+
+	/* Wait for all pending dispatches to terminate which may have been
+	 * initiated after unquiescing.
+	 */
+	blk_mq_freeze_queue_wait(q);
+
+	/* Mark the disk dead so that once queue unfreeze, the requests
+	 * waiting at the door of bio_queue_enter() can be aborted right away.
+	 */
+	blk_mark_disk_dead(vblk->disk);
+
+	/* Unfreeze the queue so that any waiting requests will be aborted. */
+	blk_mq_unfreeze_queue_nomemrestore(q);
+}
+
 static void virtblk_remove(struct virtio_device *vdev)
 {
 	struct virtio_blk *vblk = vdev->priv;
@@ -1561,6 +1654,8 @@ static void virtblk_remove(struct virtio_device *vdev)
 	/* Make sure no work handler is accessing the device. */
 	flush_work(&vblk->config_work);
 
+	virtblk_broken_device_cleanup(vblk);
+
 	del_gendisk(vblk->disk);
 	blk_mq_free_tag_set(&vblk->tag_set);
 
Signature · 1 line
2.34.1
#2 lore raw
Quoted 68 lines

From: Michael S. Tsirkin <[email protected]>
Sent: Wednesday, May 21, 2025 1:48 PM

Quoted 11 lines · Parav Pandit

When the PCI device is surprise removed, requests may not complete the device as the VQ is marked as broken. Due to this, the disk deletion hangs.

Fix it by aborting the requests when the VQ is broken.

With this fix now fio completes swiftly.
An alternative of IO timeout has been considered, however when the driver knows about unresponsive block device, swiftly clearing them enables users and upper layers to react quickly.

Quoted 9 lines

Verified with multiple device unplug iterations with pending requests in virtio used ring and some pending with the device.

Fixes 43bb40c5b926 ("virtio_pci: Support surprise removal of virtio

pci device")

Closes:

https://lore.kernel.org/virtualization/c45dd68698cd47238c55fb73ca9b474

Quoted 11 lines

[email protected]/

---
changelog:
v0->v1:
- Fixed comments from Stefan to rename a cleanup function
- Improved logic for handling any outstanding requests in bio layer
- improved cancel callback to sync with ongoing done()

thanks for the patch!
questions:

drivers/block/virtio_blk.c static blk_status_t virtio_queue_rq(struct

Quoted 10 lines
---
 drivers/block/virtio_blk.c | 95
++++++++++++++++++++++++++++++++++++++
 1 file changed, 95 insertions(+)
drivers/block/virtio_blk.c +0 -0
index 7cffea01d868..5212afdbd3c7 100644
@@ -435,6 +435,13 @@ static blk_status_t virtio_queue_rq(struct

blk_mq_hw_ctx *hctx,

drivers/block/virtio_blk.c static blk_status_t virtio_queue_rq(struct blk_mq_hw_ctx *hctx,

Quoted 5 lines
drivers/block/virtio_blk.c +2 -0
 	blk_status_t status;
 	int err;
 
+	/* Immediately fail all incoming requests if the vq is broken.
+	 * Once the queue is unquiesced, upper block layer flushes any

pending

drivers/block/virtio_blk.c static blk_status_t virtio_queue_rq(struct blk_mq_hw_ctx *hctx, · line 447

Quoted 8 lines
drivers/block/virtio_blk.c +5 -0
+	 * queued requests; fail them right away.
+	 */
+	if (unlikely(virtqueue_is_broken(vblk->vqs[qid].vq)))
+		return BLK_STS_IOERR;
+
 	status = virtblk_prep_rq(hctx, vblk, req, vbr);
 	if (unlikely(status))
 		return status;

just below this:

Quoted 9 lines
        spin_lock_irqsave(&vblk->vqs[qid].lock, flags);
        err = virtblk_add_req(vblk->vqs[qid].vq, vbr);
        if (err) {

and virtblk_add_req calls virtqueue_add_sgs, so it will fail on a broken vq.

Why do we need to check it one extra time here?

It may work, but for some reason if the hw queue is stopped in this flow, it can hang the IOs flushing.
I considered it risky to rely on the error code ENOSPC returned by non virtio-blk driver.
In other words, if lower layer changed for some reason, we may end up in stopping the hw queue when broken, and requests would hang.

Compared to that one-time entry check seems more robust.

Quoted 7 lines
Quoted 7 lines
drivers/block/virtio_blk.c +0 -0
@@ -508,6 +515,11 @@ static void virtio_queue_rqs(struct rq_list *rqlist)
 	while ((req = rq_list_pop(rqlist))) {
 		struct virtio_blk_vq *this_vq = get_virtio_blk_vq(req-
 mq_hctx);
 
Quoted 11 lines
Quoted 8 lines
drivers/block/virtio_blk.c +5 -0
+		if (unlikely(virtqueue_is_broken(this_vq->vq))) {
+			rq_list_add_tail(&requeue_list, req);
+			continue;
+		}
+
 		if (vq && vq != this_vq)
 			virtblk_add_req_batch(vq, &submit_list);

vq = this_vq;

similarly

The error code is not surfacing up here from virtblk_add_req().
It would end up adding checking for special error code here as well to abort by translating broken VQ -> EIO to break the loop in virtblk_add_req_batch().

Weighing on specific error code-based data path that may require audit from lower layers now and future, an explicit check of broken in this layer could be better.

[..]

#5 lore raw
In reply to Parav Pandit
Quoted 75 lines · Parav Pandit
Quoted 75 lines

From: Michael S. Tsirkin <[email protected]>
Sent: Wednesday, May 21, 2025 1:48 PM

Quoted 11 lines · Parav Pandit

When the PCI device is surprise removed, requests may not complete the device as the VQ is marked as broken. Due to this, the disk deletion hangs.

Fix it by aborting the requests when the VQ is broken.

With this fix now fio completes swiftly.
An alternative of IO timeout has been considered, however when the driver knows about unresponsive block device, swiftly clearing them enables users and upper layers to react quickly.

Quoted 9 lines

Verified with multiple device unplug iterations with pending requests in virtio used ring and some pending with the device.

Fixes 43bb40c5b926 ("virtio_pci: Support surprise removal of virtio

pci device")

Closes:

https://lore.kernel.org/virtualization/c45dd68698cd47238c55fb73ca9b474

Quoted 11 lines

[email protected]/

---
changelog:
v0->v1:
- Fixed comments from Stefan to rename a cleanup function
- Improved logic for handling any outstanding requests in bio layer
- improved cancel callback to sync with ongoing done()

thanks for the patch!
questions:

drivers/block/virtio_blk.c static blk_status_t virtio_queue_rq(struct

Quoted 10 lines
---
 drivers/block/virtio_blk.c | 95
++++++++++++++++++++++++++++++++++++++
 1 file changed, 95 insertions(+)
drivers/block/virtio_blk.c +0 -0
index 7cffea01d868..5212afdbd3c7 100644
@@ -435,6 +435,13 @@ static blk_status_t virtio_queue_rq(struct

blk_mq_hw_ctx *hctx,

drivers/block/virtio_blk.c static blk_status_t virtio_queue_rq(struct blk_mq_hw_ctx *hctx,

Quoted 5 lines
drivers/block/virtio_blk.c +2 -0
 	blk_status_t status;
 	int err;
 
+	/* Immediately fail all incoming requests if the vq is broken.
+	 * Once the queue is unquiesced, upper block layer flushes any

pending

drivers/block/virtio_blk.c static blk_status_t virtio_queue_rq(struct blk_mq_hw_ctx *hctx, · line 447

Quoted 8 lines
drivers/block/virtio_blk.c +5 -0
+	 * queued requests; fail them right away.
+	 */
+	if (unlikely(virtqueue_is_broken(vblk->vqs[qid].vq)))
+		return BLK_STS_IOERR;
+
 	status = virtblk_prep_rq(hctx, vblk, req, vbr);
 	if (unlikely(status))
 		return status;

just below this:

        spin_lock_irqsave(&vblk->vqs[qid].lock, flags);
        err = virtblk_add_req(vblk->vqs[qid].vq, vbr);
        if (err) {

and virtblk_add_req calls virtqueue_add_sgs, so it will fail on a broken vq.

Quoted 8 lines
Quoted 2 lines

Why do we need to check it one extra time here?

It may work, but for some reason if the hw queue is stopped in this flow, it can hang the IOs flushing.

I considered it risky to rely on the error code ENOSPC returned by non virtio-blk driver.
In other words, if lower layer changed for some reason, we may end up in stopping the hw queue when broken, and requests would hang.

Compared to that one-time entry check seems more robust.

I don't get it.
Checking twice in a row is more robust?
What am I missing?
Can you describe the scenario in more detail?

Quoted 19 lines
Quoted 8 lines
Quoted 8 lines
drivers/block/virtio_blk.c +0 -0
@@ -508,6 +515,11 @@ static void virtio_queue_rqs(struct rq_list *rqlist)
 	while ((req = rq_list_pop(rqlist))) {
 		struct virtio_blk_vq *this_vq = get_virtio_blk_vq(req-
 mq_hctx);
 
Quoted 11 lines
Quoted 8 lines
drivers/block/virtio_blk.c +5 -0
+		if (unlikely(virtqueue_is_broken(this_vq->vq))) {
+			rq_list_add_tail(&requeue_list, req);
+			continue;
+		}
+
 		if (vq && vq != this_vq)
 			virtblk_add_req_batch(vq, &submit_list);

vq = this_vq;

similarly

Quoted 1 line

The error code is not surfacing up here from virtblk_add_req().

but wait a sec:

static void virtblk_add_req_batch(struct virtio_blk_vq *vq,
struct rq_list *rqlist)

{       
        struct request *req; 
        unsigned long flags;
        bool kick;
        
        spin_lock_irqsave(&vq->lock, flags);
        
        while ((req = rq_list_pop(rqlist))) {
                struct virtblk_req *vbr = blk_mq_rq_to_pdu(req);
                int err;
                        
                err = virtblk_add_req(vq->vq, vbr);
                if (err) {
                        virtblk_unmap_data(req, vbr);
                        virtblk_cleanup_cmd(req);
                        blk_mq_requeue_request(req, true);
                }
        }

        kick = virtqueue_kick_prepare(vq->vq);
        spin_unlock_irqrestore(&vq->lock, flags);

        if (kick)
                virtqueue_notify(vq->vq);
}

it actually handles the error internally?

Quoted 5 lines

It would end up adding checking for special error code here as well to abort by translating broken VQ -> EIO to break the loop in virtblk_add_req_batch().

Weighing on specific error code-based data path that may require audit from lower layers now and future, an explicit check of broken in this layer could be better.

[..]

Checking add was successful is preferred because it has to be done *anyway* - device can get broken after you check before add.

So I would like to understand why are we also checking explicitly and I do not get it so far.

#4 lore raw
In reply to Parav Pandit
Quoted 9 lines · Parav Pandit
Quoted 6 lines

From: Parav Pandit <[email protected]>
Sent: Wednesday, May 21, 2025 2:45 PM
To: Michael S. Tsirkin <[email protected]>
Cc: [email protected]; [email protected]; [email protected]; [email protected];

I had a typo in the addressing block driver's mailing list. Will resend with right email shortly.

let's figure out what this does first. it looks ATM like it papers over some kind of bug elsewhere.

#6 lore raw
In reply to Michael S. Tsirkin
Quoted 96 lines

From: Michael S. Tsirkin <[email protected]>
Sent: Wednesday, May 21, 2025 2:49 PM
To: Parav Pandit <[email protected]>
Cc: [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; NBU-Contact-Li Rongqing (EXTERNAL) <[email protected]>; Chaitanya Kulkarni <[email protected]>; [email protected]; [email protected]; [email protected]; Max Gurtovoy <[email protected]>; Israel Rukshin <[email protected]>
Subject: Re: [PATCH v1] virtio_blk: Fix disk deletion hang on device surprise removal

Quoted 75 lines · Parav Pandit
Quoted 75 lines

From: Michael S. Tsirkin <[email protected]>
Sent: Wednesday, May 21, 2025 1:48 PM

Quoted 11 lines · Parav Pandit

When the PCI device is surprise removed, requests may not complete the device as the VQ is marked as broken. Due to this, the disk deletion hangs.

Fix it by aborting the requests when the VQ is broken.

With this fix now fio completes swiftly.
An alternative of IO timeout has been considered, however when the driver knows about unresponsive block device, swiftly clearing them enables users and upper layers to react quickly.

Quoted 9 lines

Verified with multiple device unplug iterations with pending requests in virtio used ring and some pending with the device.

Fixes 43bb40c5b926 ("virtio_pci: Support surprise removal of

virtio pci device")

Closes:

https://lore.kernel.org/virtualization/c45dd68698cd47238c55fb73ca9b4
74

Quoted 11 lines

[email protected]/

---
changelog:
v0->v1:
- Fixed comments from Stefan to rename a cleanup function
- Improved logic for handling any outstanding requests in bio layer
- improved cancel callback to sync with ongoing done()

thanks for the patch!
questions:

drivers/block/virtio_blk.c static blk_status_t virtio_queue_rq(struct

Quoted 11 lines
---
 drivers/block/virtio_blk.c | 95
++++++++++++++++++++++++++++++++++++++
 1 file changed, 95 insertions(+)
(quoted diff) +0 -0

b/drivers/block/virtio_blk.c index 7cffea01d868..5212afdbd3c7 100644

drivers/block/virtio_blk.c +0 -0
@@ -435,6 +435,13 @@ static blk_status_t virtio_queue_rq(struct

blk_mq_hw_ctx *hctx,

drivers/block/virtio_blk.c static blk_status_t virtio_queue_rq(struct blk_mq_hw_ctx *hctx,

Quoted 5 lines
drivers/block/virtio_blk.c +2 -0
 	blk_status_t status;
 	int err;
 
+	/* Immediately fail all incoming requests if the vq is broken.
+	 * Once the queue is unquiesced, upper block layer flushes any

pending

drivers/block/virtio_blk.c static blk_status_t virtio_queue_rq(struct blk_mq_hw_ctx *hctx, · line 447

Quoted 8 lines
drivers/block/virtio_blk.c +5 -0
+	 * queued requests; fail them right away.
+	 */
+	if (unlikely(virtqueue_is_broken(vblk->vqs[qid].vq)))
+		return BLK_STS_IOERR;
+
 	status = virtblk_prep_rq(hctx, vblk, req, vbr);
 	if (unlikely(status))
 		return status;

just below this:

        spin_lock_irqsave(&vblk->vqs[qid].lock, flags);
        err = virtblk_add_req(vblk->vqs[qid].vq, vbr);
        if (err) {
Quoted 5 lines
Quoted 4 lines

and virtblk_add_req calls virtqueue_add_sgs, so it will fail on a broken vq.

Why do we need to check it one extra time here?

It may work, but for some reason if the hw queue is stopped in this flow, it

can hang the IOs flushing.

Quoted 1 line

I considered it risky to rely on the error code ENOSPC returned by non virtio-

Quoted 8 lines

blk driver.

Quoted 1 line

In other words, if lower layer changed for some reason, we may end up in

stopping the hw queue when broken, and requests would hang.

Quoted 2 lines

Compared to that one-time entry check seems more robust.

I don't get it.
Checking twice in a row is more robust?

No. I am not confident on the relying on the error code -ENOSPC from layers outside of virtio-blk driver.

If for a broken VQ, ENOSPC arrives, then hw queue is stopped and requests could be stuck.

Quoted 56 lines

What am I missing?
Can you describe the scenario in more detail?

Quoted 4 lines
Quoted 4 lines

drivers/block/virtio_blk.c static void virtio_queue_rqs(struct rq_list

Quoted 4 lines
drivers/block/virtio_blk.c +0 -0
@@ -508,6 +515,11 @@ static void virtio_queue_rqs(struct rq_list

*rqlist)

Quoted 15 lines
Quoted 4 lines
Quoted 4 lines
drivers/block/virtio_blk.c +0 -0
 	while ((req = rq_list_pop(rqlist))) {
 		struct virtio_blk_vq *this_vq = get_virtio_blk_vq(req-
 mq_hctx);
 
Quoted 11 lines
Quoted 8 lines
drivers/block/virtio_blk.c +5 -0
+		if (unlikely(virtqueue_is_broken(this_vq->vq))) {
+			rq_list_add_tail(&requeue_list, req);
+			continue;
+		}
+
 		if (vq && vq != this_vq)
 			virtblk_add_req_batch(vq, &submit_list);

vq = this_vq;

similarly

Quoted 1 line

The error code is not surfacing up here from virtblk_add_req().

but wait a sec:

static void virtblk_add_req_batch(struct virtio_blk_vq *vq,
struct rq_list *rqlist)

{
        struct request *req;
        unsigned long flags;
        bool kick;

        spin_lock_irqsave(&vq->lock, flags);

        while ((req = rq_list_pop(rqlist))) {
                struct virtblk_req *vbr = blk_mq_rq_to_pdu(req);
                int err;

                err = virtblk_add_req(vq->vq, vbr);
                if (err) {
                        virtblk_unmap_data(req, vbr);
                        virtblk_cleanup_cmd(req);
                        blk_mq_requeue_request(req, true);
                }
        }

        kick = virtqueue_kick_prepare(vq->vq);
        spin_unlock_irqrestore(&vq->lock, flags);

        if (kick)
                virtqueue_notify(vq->vq); }
Quoted 2 lines

it actually handles the error internally?

For all the errors it requeues the request here.

Quoted 10 lines
Quoted 4 lines

It would end up adding checking for special error code here as well to abort

by translating broken VQ -> EIO to break the loop in virtblk_add_req_batch().

Quoted 2 lines

Weighing on specific error code-based data path that may require audit from

lower layers now and future, an explicit check of broken in this layer could be better.

Quoted 1 line
Quoted 8 lines
Quoted 1 line

[..]

Checking add was successful is preferred because it has to be done *anyway* - device can get broken after you check before add.

So I would like to understand why are we also checking explicitly and I do not get it so far.

checking explicitly to not depend on specific error code-based logic.

#7 lore raw
Quoted 97 lines · Parav Pandit
Quoted 97 lines

From: Michael S. Tsirkin <[email protected]>
Sent: Wednesday, May 21, 2025 2:49 PM
To: Parav Pandit <[email protected]>
Cc: [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; NBU-Contact-Li Rongqing (EXTERNAL) <[email protected]>; Chaitanya Kulkarni <[email protected]>; [email protected]; [email protected]; [email protected]; Max Gurtovoy <[email protected]>; Israel Rukshin <[email protected]>
Subject: Re: [PATCH v1] virtio_blk: Fix disk deletion hang on device surprise removal

Quoted 75 lines · Parav Pandit
Quoted 75 lines

From: Michael S. Tsirkin <[email protected]>
Sent: Wednesday, May 21, 2025 1:48 PM

Quoted 11 lines · Parav Pandit

When the PCI device is surprise removed, requests may not complete the device as the VQ is marked as broken. Due to this, the disk deletion hangs.

Fix it by aborting the requests when the VQ is broken.

With this fix now fio completes swiftly.
An alternative of IO timeout has been considered, however when the driver knows about unresponsive block device, swiftly clearing them enables users and upper layers to react quickly.

Quoted 9 lines

Verified with multiple device unplug iterations with pending requests in virtio used ring and some pending with the device.

Fixes 43bb40c5b926 ("virtio_pci: Support surprise removal of

virtio pci device")

Closes:

https://lore.kernel.org/virtualization/c45dd68698cd47238c55fb73ca9b4
74

Quoted 11 lines

[email protected]/

---
changelog:
v0->v1:
- Fixed comments from Stefan to rename a cleanup function
- Improved logic for handling any outstanding requests in bio layer
- improved cancel callback to sync with ongoing done()

thanks for the patch!
questions:

drivers/block/virtio_blk.c static blk_status_t virtio_queue_rq(struct

Quoted 11 lines
---
 drivers/block/virtio_blk.c | 95
++++++++++++++++++++++++++++++++++++++
 1 file changed, 95 insertions(+)
(quoted diff) +0 -0

b/drivers/block/virtio_blk.c index 7cffea01d868..5212afdbd3c7 100644

drivers/block/virtio_blk.c +0 -0
@@ -435,6 +435,13 @@ static blk_status_t virtio_queue_rq(struct

blk_mq_hw_ctx *hctx,

drivers/block/virtio_blk.c static blk_status_t virtio_queue_rq(struct blk_mq_hw_ctx *hctx,

Quoted 5 lines
drivers/block/virtio_blk.c +2 -0
 	blk_status_t status;
 	int err;
 
+	/* Immediately fail all incoming requests if the vq is broken.
+	 * Once the queue is unquiesced, upper block layer flushes any

pending

drivers/block/virtio_blk.c static blk_status_t virtio_queue_rq(struct blk_mq_hw_ctx *hctx, · line 447

Quoted 8 lines
drivers/block/virtio_blk.c +5 -0
+	 * queued requests; fail them right away.
+	 */
+	if (unlikely(virtqueue_is_broken(vblk->vqs[qid].vq)))
+		return BLK_STS_IOERR;
+
 	status = virtblk_prep_rq(hctx, vblk, req, vbr);
 	if (unlikely(status))
 		return status;

just below this:

        spin_lock_irqsave(&vblk->vqs[qid].lock, flags);
        err = virtblk_add_req(vblk->vqs[qid].vq, vbr);
        if (err) {
Quoted 5 lines
Quoted 4 lines

and virtblk_add_req calls virtqueue_add_sgs, so it will fail on a broken vq.

Why do we need to check it one extra time here?

It may work, but for some reason if the hw queue is stopped in this flow, it

can hang the IOs flushing.

Quoted 1 line

I considered it risky to rely on the error code ENOSPC returned by non virtio-

blk driver.

Quoted 8 lines
Quoted 7 lines
Quoted 1 line

In other words, if lower layer changed for some reason, we may end up in

stopping the hw queue when broken, and requests would hang.

Quoted 2 lines

Compared to that one-time entry check seems more robust.

I don't get it.
Checking twice in a row is more robust?

No. I am not confident on the relying on the error code -ENOSPC from layers outside of virtio-blk driver.

You can rely on virtio core to return an error on a broken vq. The error won't be -ENOSPC though, why would it?

Quoted 1 line

If for a broken VQ, ENOSPC arrives, then hw queue is stopped and requests could be stuck.

Can you describe the scenario in more detail pls? where does ENOSPC arrive from? when is the vq get broken ...

Quoted 52 lines
Quoted 52 lines

What am I missing?
Can you describe the scenario in more detail?

Quoted 4 lines
Quoted 4 lines

drivers/block/virtio_blk.c static void virtio_queue_rqs(struct rq_list

Quoted 4 lines
drivers/block/virtio_blk.c +0 -0
@@ -508,6 +515,11 @@ static void virtio_queue_rqs(struct rq_list

*rqlist)

Quoted 15 lines
Quoted 4 lines
Quoted 4 lines
drivers/block/virtio_blk.c +0 -0
 	while ((req = rq_list_pop(rqlist))) {
 		struct virtio_blk_vq *this_vq = get_virtio_blk_vq(req-
 mq_hctx);
 
Quoted 11 lines
Quoted 8 lines
drivers/block/virtio_blk.c +5 -0
+		if (unlikely(virtqueue_is_broken(this_vq->vq))) {
+			rq_list_add_tail(&requeue_list, req);
+			continue;
+		}
+
 		if (vq && vq != this_vq)
 			virtblk_add_req_batch(vq, &submit_list);

vq = this_vq;

similarly

Quoted 1 line

The error code is not surfacing up here from virtblk_add_req().

but wait a sec:

static void virtblk_add_req_batch(struct virtio_blk_vq *vq,
struct rq_list *rqlist)

{
        struct request *req;
        unsigned long flags;
        bool kick;

        spin_lock_irqsave(&vq->lock, flags);

        while ((req = rq_list_pop(rqlist))) {
                struct virtblk_req *vbr = blk_mq_rq_to_pdu(req);
                int err;

                err = virtblk_add_req(vq->vq, vbr);
                if (err) {
                        virtblk_unmap_data(req, vbr);
                        virtblk_cleanup_cmd(req);
                        blk_mq_requeue_request(req, true);
                }
        }

        kick = virtqueue_kick_prepare(vq->vq);
        spin_unlock_irqrestore(&vq->lock, flags);
Quoted 7 lines
Quoted 6 lines
        if (kick)
                virtqueue_notify(vq->vq); }

it actually handles the error internally?

For all the errors it requeues the request here.

ok and they will not prevent removal will they?

Quoted 13 lines
Quoted 13 lines
Quoted 4 lines

It would end up adding checking for special error code here as well to abort

by translating broken VQ -> EIO to break the loop in virtblk_add_req_batch().

Quoted 2 lines

Weighing on specific error code-based data path that may require audit from

lower layers now and future, an explicit check of broken in this layer could be better.

Quoted 2 lines

[..]

Quoted 7 lines
Quoted 5 lines

Checking add was successful is preferred because it has to be done *anyway* - device can get broken after you check before add.

So I would like to understand why are we also checking explicitly and I do not get it so far.

checking explicitly to not depend on specific error code-based logic.

I do not understand. You must handle vq add errors anyway.

Signature · 1 line
MST
#8 lore raw
Quoted 115 lines

From: Michael S. Tsirkin <[email protected]>
Sent: Wednesday, May 21, 2025 3:46 PM

Quoted 92 lines · Parav Pandit
Quoted 92 lines

From: Michael S. Tsirkin <[email protected]>
Sent: Wednesday, May 21, 2025 2:49 PM
To: Parav Pandit <[email protected]>
Cc: [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; NBU-Contact-Li Rongqing (EXTERNAL) <[email protected]>; Chaitanya Kulkarni <[email protected]>; [email protected]; [email protected]; [email protected]; Max Gurtovoy <[email protected]>; Israel Rukshin <[email protected]>
Subject: Re: [PATCH v1] virtio_blk: Fix disk deletion hang on device surprise removal

Quoted 78 lines · Parav Pandit
Quoted 78 lines

From: Michael S. Tsirkin <[email protected]>
Sent: Wednesday, May 21, 2025 1:48 PM

Quoted 11 lines · Parav Pandit

When the PCI device is surprise removed, requests may not complete the device as the VQ is marked as broken. Due to this, the disk deletion hangs.

Fix it by aborting the requests when the VQ is broken.

With this fix now fio completes swiftly.
An alternative of IO timeout has been considered, however when the driver knows about unresponsive block device, swiftly clearing them enables users and upper layers to react quickly.

Quoted 9 lines

Verified with multiple device unplug iterations with pending requests in virtio used ring and some pending with the device.

Fixes 43bb40c5b926 ("virtio_pci: Support surprise removal of

virtio pci device")

Closes:

https://lore.kernel.org/virtualization/c45dd68698cd47238c55fb73c
a9b4
74

Quoted 11 lines

[email protected]/

---
changelog:
v0->v1:
- Fixed comments from Stefan to rename a cleanup function
- Improved logic for handling any outstanding requests in bio layer
- improved cancel callback to sync with ongoing done()

thanks for the patch!
questions:

Quoted 11 lines
---
 drivers/block/virtio_blk.c | 95
++++++++++++++++++++++++++++++++++++++
 1 file changed, 95 insertions(+)
(quoted diff) +0 -0

b/drivers/block/virtio_blk.c index 7cffea01d868..5212afdbd3c7 100644

drivers/block/virtio_blk.c +0 -0
@@ -435,6 +435,13 @@ static blk_status_t
Quoted 1 line

virtio_queue_rq(struct

blk_mq_hw_ctx *hctx,

drivers/block/virtio_blk.c static blk_status_t virtio_queue_rq(struct blk_mq_hw_ctx *hctx,

Quoted 6 lines
drivers/block/virtio_blk.c +3 -0
 	blk_status_t status;
 	int err;
 
+	/* Immediately fail all incoming requests if the vq is broken.
+	 * Once the queue is unquiesced, upper block layer flushes
+any

pending

drivers/block/virtio_blk.c static blk_status_t virtio_queue_rq(struct blk_mq_hw_ctx *hctx, · line 447

Quoted 8 lines
drivers/block/virtio_blk.c +5 -0
+	 * queued requests; fail them right away.
+	 */
+	if (unlikely(virtqueue_is_broken(vblk->vqs[qid].vq)))
+		return BLK_STS_IOERR;
+
 	status = virtblk_prep_rq(hctx, vblk, req, vbr);
 	if (unlikely(status))
 		return status;

just below this:

        spin_lock_irqsave(&vblk->vqs[qid].lock, flags);
        err = virtblk_add_req(vblk->vqs[qid].vq, vbr);
        if (err) {
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

and virtblk_add_req calls virtqueue_add_sgs, so it will fail on a broken

vq.

Quoted 16 lines
Quoted 16 lines
Quoted 5 lines
Quoted 3 lines

Why do we need to check it one extra time here?

It may work, but for some reason if the hw queue is stopped in this flow, it

can hang the IOs flushing.

Quoted 2 lines

I considered it risky to rely on the error code ENOSPC returned by non virtio-

blk driver.

Quoted 2 lines

In other words, if lower layer changed for some reason, we may end up in

stopping the hw queue when broken, and requests would hang.

Quoted 2 lines

Compared to that one-time entry check seems more robust.

Quoted 8 lines
Quoted 3 lines
Quoted 2 lines

I don't get it.
Checking twice in a row is more robust?

No. I am not confident on the relying on the error code -ENOSPC from layers

outside of virtio-blk driver.

You can rely on virtio core to return an error on a broken vq. The error won't be -ENOSPC though, why would it?

Presently that is not the API contract between virtio core and driver. When the VQ is broken the error code is EIO. This is from the code inspection.

If you prefer to rely on the code inspection of lower layer to define the virtio-blk, I am fine and remove the two checks. I just find it fragile, but if you prefer this way, I am fine.

Quoted 6 lines
Quoted 1 line

If for a broken VQ, ENOSPC arrives, then hw queue is stopped and requests

could be stuck.

Can you describe the scenario in more detail pls? where does ENOSPC arrive from? when is the vq get broken ...

ENOSPC arrives when it fails to enqueue the request in present form. EIO arrives when VQ is broken.

If in the future, ENOSPC arrives for broken VQ, following flow can trigger a hang.

cpu_0:
virtblk_broken_device_cleanup()
...

    blk_mq_unquiesce_queue();
    ... stage_1:
    blk_mq_freeze_queue_wait().

Cpu_1:
Queue_rq()
virtio_queue_rq()
virtblk_add_req()
-ENOSPC
Stop_hw_queue()
At this point, new requests in block layer may get stuck and may not be enqueued to queue_rq().

Quoted 57 lines
Quoted 57 lines
Quoted 57 lines

What am I missing?
Can you describe the scenario in more detail?

Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
drivers/block/virtio_blk.c +0 -0
@@ -508,6 +515,11 @@ static void virtio_queue_rqs(struct

rq_list

*rqlist)

Quoted 15 lines
Quoted 4 lines
Quoted 4 lines
drivers/block/virtio_blk.c +0 -0
 	while ((req = rq_list_pop(rqlist))) {
 		struct virtio_blk_vq *this_vq = get_virtio_blk_vq(req-

mq_hctx);

Quoted 11 lines

drivers/block/virtio_blk.c static void virtio_queue_rqs(struct rq_list *rqlist) · line 525

Quoted 8 lines
drivers/block/virtio_blk.c +5 -0
+		if (unlikely(virtqueue_is_broken(this_vq->vq))) {
+			rq_list_add_tail(&requeue_list, req);
+			continue;
+		}
+
 		if (vq && vq != this_vq)
 			virtblk_add_req_batch(vq, &submit_list);
 		vq = this_vq;

similarly

Quoted 1 line

The error code is not surfacing up here from virtblk_add_req().

but wait a sec:

static void virtblk_add_req_batch(struct virtio_blk_vq *vq,

                struct rq_list *rqlist) {
        struct request *req;
        unsigned long flags;
        bool kick;

        spin_lock_irqsave(&vq->lock, flags);

        while ((req = rq_list_pop(rqlist))) {
                struct virtblk_req *vbr = blk_mq_rq_to_pdu(req);
                int err;

                err = virtblk_add_req(vq->vq, vbr);
                if (err) {
                        virtblk_unmap_data(req, vbr);
                        virtblk_cleanup_cmd(req);
                        blk_mq_requeue_request(req, true);
                }
        }

        kick = virtqueue_kick_prepare(vq->vq);
        spin_unlock_irqrestore(&vq->lock, flags);

        if (kick)
                virtqueue_notify(vq->vq); }
Quoted 6 lines
Quoted 3 lines
Quoted 2 lines

it actually handles the error internally?

For all the errors it requeues the request here.

ok and they will not prevent removal will they?

It should not prevent removal.
One must be careful every single time changing it to make sure that hw queues are not stopped in lower layer, but may be this is ok.

Quoted 20 lines
Quoted 7 lines
Quoted 7 lines
Quoted 6 lines

It would end up adding checking for special error code here as well to abort

by translating broken VQ -> EIO to break the loop in

virtblk_add_req_batch().

Quoted 12 lines
Quoted 12 lines
Quoted 3 lines

Weighing on specific error code-based data path that may require audit from

lower layers now and future, an explicit check of broken in this layer could be better.

Quoted 2 lines

[..]

Checking add was successful is preferred because it has to be done *anyway* - device can get broken after you check before add.

Quoted 7 lines
Quoted 4 lines
Quoted 2 lines

So I would like to understand why are we also checking explicitly and I do not get it so far.

checking explicitly to not depend on specific error code-based logic.

I do not understand. You must handle vq add errors anyway.

I believe removal of the two vq broken checks should also be fine.
I would probably add the comment in the code indicating virtio block driver assumes that ENOSPC is not returned for broken VQ.

#9 lore raw
Quoted 117 lines · Parav Pandit
Quoted 117 lines

From: Michael S. Tsirkin <[email protected]>
Sent: Wednesday, May 21, 2025 3:46 PM

Quoted 92 lines · Parav Pandit
Quoted 92 lines

From: Michael S. Tsirkin <[email protected]>
Sent: Wednesday, May 21, 2025 2:49 PM
To: Parav Pandit <[email protected]>
Cc: [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; NBU-Contact-Li Rongqing (EXTERNAL) <[email protected]>; Chaitanya Kulkarni <[email protected]>; [email protected]; [email protected]; [email protected]; Max Gurtovoy <[email protected]>; Israel Rukshin <[email protected]>
Subject: Re: [PATCH v1] virtio_blk: Fix disk deletion hang on device surprise removal

Quoted 78 lines · Parav Pandit
Quoted 78 lines

From: Michael S. Tsirkin <[email protected]>
Sent: Wednesday, May 21, 2025 1:48 PM

Quoted 11 lines · Parav Pandit

When the PCI device is surprise removed, requests may not complete the device as the VQ is marked as broken. Due to this, the disk deletion hangs.

Fix it by aborting the requests when the VQ is broken.

With this fix now fio completes swiftly.
An alternative of IO timeout has been considered, however when the driver knows about unresponsive block device, swiftly clearing them enables users and upper layers to react quickly.

Quoted 9 lines

Verified with multiple device unplug iterations with pending requests in virtio used ring and some pending with the device.

Fixes 43bb40c5b926 ("virtio_pci: Support surprise removal of

virtio pci device")

Closes:

https://lore.kernel.org/virtualization/c45dd68698cd47238c55fb73c
a9b4
74

Quoted 11 lines

[email protected]/

---
changelog:
v0->v1:
- Fixed comments from Stefan to rename a cleanup function
- Improved logic for handling any outstanding requests in bio layer
- improved cancel callback to sync with ongoing done()

thanks for the patch!
questions:

Quoted 11 lines
---
 drivers/block/virtio_blk.c | 95
++++++++++++++++++++++++++++++++++++++
 1 file changed, 95 insertions(+)
(quoted diff) +0 -0

b/drivers/block/virtio_blk.c index 7cffea01d868..5212afdbd3c7 100644

drivers/block/virtio_blk.c +0 -0
@@ -435,6 +435,13 @@ static blk_status_t
Quoted 1 line

virtio_queue_rq(struct

blk_mq_hw_ctx *hctx,

drivers/block/virtio_blk.c

Quoted 6 lines
drivers/block/virtio_blk.c +3 -0
 	blk_status_t status;
 	int err;
 
+	/* Immediately fail all incoming requests if the vq is broken.
+	 * Once the queue is unquiesced, upper block layer flushes
+any

pending

drivers/block/virtio_blk.c

Quoted 8 lines
drivers/block/virtio_blk.c +5 -0
+	 * queued requests; fail them right away.
+	 */
+	if (unlikely(virtqueue_is_broken(vblk->vqs[qid].vq)))
+		return BLK_STS_IOERR;
+
 	status = virtblk_prep_rq(hctx, vblk, req, vbr);
 	if (unlikely(status))
 		return status;

just below this:

        spin_lock_irqsave(&vblk->vqs[qid].lock, flags);
        err = virtblk_add_req(vblk->vqs[qid].vq, vbr);
        if (err) {
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

and virtblk_add_req calls virtqueue_add_sgs, so it will fail on a broken

vq.

Quoted 18 lines
Quoted 18 lines
Quoted 5 lines
Quoted 3 lines

Why do we need to check it one extra time here?

It may work, but for some reason if the hw queue is stopped in this flow, it

can hang the IOs flushing.

Quoted 2 lines

I considered it risky to rely on the error code ENOSPC returned by non virtio-

blk driver.

Quoted 2 lines

In other words, if lower layer changed for some reason, we may end up in

stopping the hw queue when broken, and requests would hang.

Quoted 2 lines

Compared to that one-time entry check seems more robust.

I don't get it.
Checking twice in a row is more robust?

Quoted 8 lines
Quoted 6 lines
Quoted 1 line

No. I am not confident on the relying on the error code -ENOSPC from layers

outside of virtio-blk driver.

You can rely on virtio core to return an error on a broken vq. The error won't be -ENOSPC though, why would it?

Presently that is not the API contract between virtio core and driver. When the VQ is broken the error code is EIO. This is from the code inspection.

yes

Quoted 2 lines

If you prefer to rely on the code inspection of lower layer to define the virtio-blk, I am fine and remove the two checks. I just find it fragile, but if you prefer this way, I am fine.

I think it's better, yes.

Quoted 116 lines
Quoted 6 lines
Quoted 1 line

If for a broken VQ, ENOSPC arrives, then hw queue is stopped and requests

could be stuck.

Can you describe the scenario in more detail pls? where does ENOSPC arrive from? when is the vq get broken ...

ENOSPC arrives when it fails to enqueue the request in present form. EIO arrives when VQ is broken.

If in the future, ENOSPC arrives for broken VQ, following flow can trigger a hang.

cpu_0:
virtblk_broken_device_cleanup()
...

    blk_mq_unquiesce_queue();
    ... stage_1:
    blk_mq_freeze_queue_wait().

Cpu_1:
Queue_rq()
virtio_queue_rq()
virtblk_add_req()
-ENOSPC
Stop_hw_queue()
At this point, new requests in block layer may get stuck and may not be enqueued to queue_rq().

Quoted 57 lines
Quoted 57 lines
Quoted 57 lines

What am I missing?
Can you describe the scenario in more detail?

Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
drivers/block/virtio_blk.c +0 -0
@@ -508,6 +515,11 @@ static void virtio_queue_rqs(struct

rq_list

*rqlist)

Quoted 15 lines
Quoted 4 lines
Quoted 4 lines
drivers/block/virtio_blk.c +0 -0
 	while ((req = rq_list_pop(rqlist))) {
 		struct virtio_blk_vq *this_vq = get_virtio_blk_vq(req-

mq_hctx);

Quoted 11 lines

drivers/block/virtio_blk.c

Quoted 8 lines
drivers/block/virtio_blk.c +5 -0
+		if (unlikely(virtqueue_is_broken(this_vq->vq))) {
+			rq_list_add_tail(&requeue_list, req);
+			continue;
+		}
+
 		if (vq && vq != this_vq)
 			virtblk_add_req_batch(vq, &submit_list);
 		vq = this_vq;

similarly

Quoted 1 line

The error code is not surfacing up here from virtblk_add_req().

but wait a sec:

static void virtblk_add_req_batch(struct virtio_blk_vq *vq,

                struct rq_list *rqlist) {
        struct request *req;
        unsigned long flags;
        bool kick;

        spin_lock_irqsave(&vq->lock, flags);

        while ((req = rq_list_pop(rqlist))) {
                struct virtblk_req *vbr = blk_mq_rq_to_pdu(req);
                int err;

                err = virtblk_add_req(vq->vq, vbr);
                if (err) {
                        virtblk_unmap_data(req, vbr);
                        virtblk_cleanup_cmd(req);
                        blk_mq_requeue_request(req, true);
                }
        }

        kick = virtqueue_kick_prepare(vq->vq);
        spin_unlock_irqrestore(&vq->lock, flags);

        if (kick)
                virtqueue_notify(vq->vq); }
Quoted 6 lines
Quoted 3 lines
Quoted 2 lines

it actually handles the error internally?

For all the errors it requeues the request here.

ok and they will not prevent removal will they?

It should not prevent removal.
One must be careful every single time changing it to make sure that hw queues are not stopped in lower layer, but may be this is ok.

Quoted 23 lines
Quoted 7 lines
Quoted 7 lines
Quoted 6 lines

It would end up adding checking for special error code here as well to abort

by translating broken VQ -> EIO to break the loop in

virtblk_add_req_batch().

Quoted 15 lines
Quoted 14 lines
Quoted 3 lines

Weighing on specific error code-based data path that may require audit from

lower layers now and future, an explicit check of broken in this layer could be better.

Quoted 2 lines

[..]

Checking add was successful is preferred because it has to be done *anyway* - device can get broken after you check before add.

So I would like to understand why are we also checking explicitly and I do not get it so far.

Quoted 7 lines
Quoted 4 lines
Quoted 1 line

checking explicitly to not depend on specific error code-based logic.

I do not understand. You must handle vq add errors anyway.

I believe removal of the two vq broken checks should also be fine.
I would probably add the comment in the code indicating virtio block driver assumes that ENOSPC is not returned for broken VQ.

You can include this in the series if you like. Tweak to taste:

-->

virtio: document ENOSPC

drivers handle ENOSPC specially since it's an error one can get from a working VQ. Document the semantics.

---

drivers/virtio/virtio_ring.c +4 -0
index b784aab66867..97ab0cce527d 100644
@@ -2296,6 +2296,10 @@ static inline int virtqueue_add(struct virtqueue *_vq,
  * at the same time (except where noted).
  *
  * Returns zero or a negative error (ie. ENOSPC, ENOMEM, EIO).
+ *
+ * NB: ENOSPC is a special code that is only returned on an attempt to add a
+ * buffer to a full VQ. It indicates that some buffers are outstanding and that
+ * the operation can be retried after some buffers have been used.
  */
 int virtqueue_add_sgs(struct virtqueue *_vq,
 		      struct scatterlist *sgs[],
#10 lore raw
Quoted 138 lines

From: Michael S. Tsirkin <[email protected]>
Sent: Wednesday, May 21, 2025 4:13 PM

Quoted 27 lines · Parav Pandit
Quoted 27 lines

From: Michael S. Tsirkin <[email protected]>
Sent: Wednesday, May 21, 2025 3:46 PM

Quoted 22 lines · Parav Pandit
Quoted 22 lines

From: Michael S. Tsirkin <[email protected]>
Sent: Wednesday, May 21, 2025 2:49 PM
To: Parav Pandit <[email protected]>
Cc: [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; NBU-Contact-Li Rongqing (EXTERNAL) <[email protected]>; Chaitanya Kulkarni <[email protected]>; [email protected]; [email protected]; [email protected]; Max Gurtovoy <[email protected]>; Israel Rukshin <[email protected]>
Subject: Re: [PATCH v1] virtio_blk: Fix disk deletion hang on device surprise removal

Quoted 8 lines · Parav Pandit
Quoted 8 lines

From: Michael S. Tsirkin <[email protected]>
Sent: Wednesday, May 21, 2025 1:48 PM

Quoted 4 lines · Parav Pandit

When the PCI device is surprise removed, requests may not complete the device as the VQ is marked as broken. Due to this, the disk deletion hangs.

Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines

Fix it by aborting the requests when the VQ is broken.

With this fix now fio completes swiftly.
An alternative of IO timeout has been considered, however when the driver knows about unresponsive block device, swiftly clearing them enables users and upper layers to react

quickly.

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

Verified with multiple device unplug iterations with pending requests in virtio used ring and some pending with the

device.

Quoted 86 lines
Quoted 86 lines
Quoted 63 lines
Quoted 63 lines
Quoted 63 lines
Quoted 63 lines
Quoted 7 lines
Fixes 43bb40c5b926 ("virtio_pci: Support surprise removal

of virtio pci device")

Closes:

https://lore.kernel.org/virtualization/c45dd68698cd47238c55f
b73c
a9b4
74

Quoted 11 lines

[email protected]/

---
changelog:
v0->v1:
- Fixed comments from Stefan to rename a cleanup function
- Improved logic for handling any outstanding requests in bio layer
- improved cancel callback to sync with ongoing done()

thanks for the patch!
questions:

Quoted 12 lines
---
 drivers/block/virtio_blk.c | 95
++++++++++++++++++++++++++++++++++++++
 1 file changed, 95 insertions(+)
(quoted diff) +0 -0

b/drivers/block/virtio_blk.c index
7cffea01d868..5212afdbd3c7
100644

drivers/block/virtio_blk.c +0 -0
@@ -435,6 +435,13 @@ static blk_status_t
Quoted 1 line

virtio_queue_rq(struct

blk_mq_hw_ctx *hctx,

drivers/block/virtio_blk.c

Quoted 6 lines
drivers/block/virtio_blk.c +3 -0
 	blk_status_t status;
 	int err;
 
+	/* Immediately fail all incoming requests if the vq is broken.
+	 * Once the queue is unquiesced, upper block layer
+flushes any

pending

drivers/block/virtio_blk.c

Quoted 8 lines
drivers/block/virtio_blk.c +5 -0
+	 * queued requests; fail them right away.
+	 */
+	if (unlikely(virtqueue_is_broken(vblk->vqs[qid].vq)))
+		return BLK_STS_IOERR;
+
 	status = virtblk_prep_rq(hctx, vblk, req, vbr);
 	if (unlikely(status))
 		return status;

just below this:

        spin_lock_irqsave(&vblk->vqs[qid].lock, flags);
        err = virtblk_add_req(vblk->vqs[qid].vq, vbr);
        if (err) {
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

and virtblk_add_req calls virtqueue_add_sgs, so it will fail on a broken

vq.

Quoted 20 lines
Quoted 10 lines
Quoted 5 lines
Quoted 3 lines

Why do we need to check it one extra time here?

It may work, but for some reason if the hw queue is stopped in this flow, it

can hang the IOs flushing.

Quoted 2 lines

I considered it risky to rely on the error code ENOSPC returned by non virtio-

blk driver.

Quoted 8 lines
Quoted 2 lines

In other words, if lower layer changed for some reason, we may end up in

stopping the hw queue when broken, and requests would hang.

Quoted 2 lines

Compared to that one-time entry check seems more robust.

I don't get it.
Checking twice in a row is more robust?

No. I am not confident on the relying on the error code -ENOSPC from layers

Quoted 7 lines
Quoted 5 lines

outside of virtio-blk driver.

You can rely on virtio core to return an error on a broken vq. The error won't be -ENOSPC though, why would it?

Presently that is not the API contract between virtio core and driver. When the VQ is broken the error code is EIO. This is from the code

inspection.

Quoted 8 lines

yes

Quoted 1 line

If you prefer to rely on the code inspection of lower layer to define the virtio-

blk, I am fine and remove the two checks.

Quoted 1 line

I just find it fragile, but if you prefer this way, I am fine.

I think it's better, yes.

Ok. Will adapt this in v2.

Quoted 124 lines
Quoted 11 lines
Quoted 7 lines
Quoted 2 lines

If for a broken VQ, ENOSPC arrives, then hw queue is stopped and requests

could be stuck.

Can you describe the scenario in more detail pls? where does ENOSPC arrive from? when is the vq get broken ...

ENOSPC arrives when it fails to enqueue the request in present form. EIO arrives when VQ is broken.

If in the future, ENOSPC arrives for broken VQ, following flow can trigger a

hang.

Quoted 9 lines

cpu_0:
virtblk_broken_device_cleanup()
...

    blk_mq_unquiesce_queue();
    ... stage_1:
    blk_mq_freeze_queue_wait().
Quoted 7 lines

Cpu_1:
Queue_rq()
virtio_queue_rq()
virtblk_add_req()
-ENOSPC
Stop_hw_queue()
At this point, new requests in block layer may get stuck and may not

be enqueued to queue_rq().

Quoted 58 lines
Quoted 58 lines
Quoted 58 lines
Quoted 58 lines

What am I missing?
Can you describe the scenario in more detail?

Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
drivers/block/virtio_blk.c +0 -0
@@ -508,6 +515,11 @@ static void virtio_queue_rqs(struct

rq_list

*rqlist)

Quoted 15 lines
Quoted 4 lines
Quoted 4 lines
drivers/block/virtio_blk.c +0 -0
 	while ((req = rq_list_pop(rqlist))) {
 		struct virtio_blk_vq *this_vq = get_virtio_blk_vq(req-

mq_hctx);

Quoted 11 lines

drivers/block/virtio_blk.c

Quoted 8 lines
drivers/block/virtio_blk.c +5 -0
+		if (unlikely(virtqueue_is_broken(this_vq->vq))) {
+			rq_list_add_tail(&requeue_list, req);
+			continue;
+		}
+
 		if (vq && vq != this_vq)
 			virtblk_add_req_batch(vq, &submit_list);
 		vq = this_vq;

similarly

Quoted 1 line

The error code is not surfacing up here from virtblk_add_req().

but wait a sec:

static void virtblk_add_req_batch(struct virtio_blk_vq *vq,

                struct rq_list *rqlist) {
        struct request *req;
        unsigned long flags;
        bool kick;

        spin_lock_irqsave(&vq->lock, flags);

        while ((req = rq_list_pop(rqlist))) {
                struct virtblk_req *vbr = blk_mq_rq_to_pdu(req);
                int err;

                err = virtblk_add_req(vq->vq, vbr);
                if (err) {
                        virtblk_unmap_data(req, vbr);
                        virtblk_cleanup_cmd(req);
                        blk_mq_requeue_request(req, true);
                }
        }

        kick = virtqueue_kick_prepare(vq->vq);
        spin_unlock_irqrestore(&vq->lock, flags);

        if (kick)
                virtqueue_notify(vq->vq); }
Quoted 8 lines
Quoted 6 lines
Quoted 3 lines
Quoted 2 lines

it actually handles the error internally?

For all the errors it requeues the request here.

ok and they will not prevent removal will they?

It should not prevent removal.
One must be careful every single time changing it to make sure that hw

queues are not stopped in lower layer, but may be this is ok.

Quoted 28 lines
Quoted 28 lines
Quoted 8 lines
Quoted 8 lines
Quoted 7 lines

It would end up adding checking for special error code here as well to abort

by translating broken VQ -> EIO to break the loop in

virtblk_add_req_batch().

Quoted 17 lines
Quoted 9 lines
Quoted 3 lines

Weighing on specific error code-based data path that may require audit from

lower layers now and future, an explicit check of broken in this layer could be better.

Quoted 2 lines

[..]

Quoted 6 lines

Checking add was successful is preferred because it has to be done
*anyway* - device can get broken after you check before add.

So I would like to understand why are we also checking explicitly and I do not get it so far.

checking explicitly to not depend on specific error code-based logic.

Quoted 8 lines
Quoted 4 lines
Quoted 1 line

I do not understand. You must handle vq add errors anyway.

I believe removal of the two vq broken checks should also be fine. I would probably add the comment in the code indicating virtio block driver

assumes that ENOSPC is not returned for broken VQ.

You can include this in the series if you like. Tweak to taste:

Thanks for the patch. This virtio core comment update can be done at later point without fixes tag. Will park for the later.

Quoted 29 lines

-->

virtio: document ENOSPC

drivers handle ENOSPC specially since it's an error one can get from a working VQ. Document the semantics.

---

drivers/virtio/virtio_ring.c index renamed from drivers/virtio/virtio_ring.c +0 -0

b784aab66867..97ab0cce527d 100644

drivers/virtio/virtio_ring.c +6 -0
@@ -2296,6 +2296,10 @@ static inline int virtqueue_add(struct virtqueue
 *_vq,
  * at the same time (except where noted).
  *
  * Returns zero or a negative error (ie. ENOSPC, ENOMEM, EIO).
+ *
+ * NB: ENOSPC is a special code that is only returned on an attempt to
+ add a
+ * buffer to a full VQ. It indicates that some buffers are outstanding
+ and that
+ * the operation can be retried after some buffers have been used.
  */
 int virtqueue_add_sgs(struct virtqueue *_vq,
 		      struct scatterlist *sgs[],
#11 lore raw
Quoted 4 lines · Parav Pandit
Quoted 2 lines

You can include this in the series if you like. Tweak to taste:

Thanks for the patch. This virtio core comment update can be done at later point without fixes tag. Will park for the later.

no problem with that, either.

#12 lore raw
In reply to Parav Pandit
Quoted 82 lines · Parav Pandit

When the PCI device is surprise removed, requests may not complete the device as the VQ is marked as broken. Due to this, the disk deletion hangs.

Fix it by aborting the requests when the VQ is broken.

With this fix now fio completes swiftly.
An alternative of IO timeout has been considered, however when the driver knows about unresponsive block device, swiftly clearing them enables users and upper layers to react quickly.

Verified with multiple device unplug iterations with pending requests in virtio used ring and some pending with the device.

Fixes 43bb40c5b926 ("virtio_pci: Support surprise removal of virtio pci device")
Reported-by [email protected]

---
changelog:
v0->v1:
- Fixed comments from Stefan to rename a cleanup function
- Improved logic for handling any outstanding requests in bio layer
- improved cancel callback to sync with ongoing done()

---
 drivers/block/virtio_blk.c | 95 ++++++++++++++++++++++++++++++++++++++
 1 file changed, 95 insertions(+)
drivers/block/virtio_blk.c +27 -0
index 7cffea01d868..5212afdbd3c7 100644
@@ -435,6 +435,13 @@ static blk_status_t virtio_queue_rq(struct blk_mq_hw_ctx *hctx,
 	blk_status_t status;
 	int err;
 
+	/* Immediately fail all incoming requests if the vq is broken.
+	 * Once the queue is unquiesced, upper block layer flushes any pending
+	 * queued requests; fail them right away.
+	 */
+	if (unlikely(virtqueue_is_broken(vblk->vqs[qid].vq)))
+		return BLK_STS_IOERR;
+
 	status = virtblk_prep_rq(hctx, vblk, req, vbr);
 	if (unlikely(status))
 		return status;
@@ -508,6 +515,11 @@ static void virtio_queue_rqs(struct rq_list *rqlist)
 	while ((req = rq_list_pop(rqlist))) {
 		struct virtio_blk_vq *this_vq = get_virtio_blk_vq(req->mq_hctx);
 
+		if (unlikely(virtqueue_is_broken(this_vq->vq))) {
+			rq_list_add_tail(&requeue_list, req);
+			continue;
+		}
+
 		if (vq && vq != this_vq)
 			virtblk_add_req_batch(vq, &submit_list);
 		vq = this_vq;
@@ -1554,6 +1566,87 @@ static int virtblk_probe(struct virtio_device *vdev)
 	return err;
 }
 
+static bool virtblk_request_cancel(struct request *rq, void *data)
+{
+	struct virtblk_req *vbr = blk_mq_rq_to_pdu(rq);
+	struct virtio_blk *vblk = data;
+	struct virtio_blk_vq *vq;
+	unsigned long flags;
+
+	vq = &vblk->vqs[rq->mq_hctx->queue_num];
+
+	spin_lock_irqsave(&vq->lock, flags);
+
+	vbr->in_hdr.status = VIRTIO_BLK_S_IOERR;
+	if (blk_mq_request_started(rq) && !blk_mq_request_completed(rq))
+		blk_mq_complete_request(rq);
+

drivers/block/virtio_blk.c static int virtblk_probe(struct virtio_device *vdev) · line 1593

Quoted 10 lines
drivers/block/virtio_blk.c +10 -0
+	spin_unlock_irqrestore(&vq->lock, flags);
+	return true;
+}
+
+static void virtblk_broken_device_cleanup(struct virtio_blk *vblk)
+{
+	struct request_queue *q = vblk->disk->queue;
+
+	if (!virtqueue_is_broken(vblk->vqs[0].vq))
+		return;

Can a subset of virtqueues be broken? If so, then this code doesn't handle it.

drivers/block/virtio_blk.c static int virtblk_probe(struct virtio_device *vdev) · line 1595

Quoted 2 lines
drivers/block/virtio_blk.c +2 -0
+
+	/* Start freezing the queue, so that new requests keeps waitng at the

s/waitng/waiting/

Quoted 17 lines
drivers/block/virtio_blk.c +17 -0
+	 * door of bio_queue_enter(). We cannot fully freeze the queue because
+	 * freezed queue is an empty queue and there are pending requests, so
+	 * only start freezing it.
+	 */
+	blk_freeze_queue_start(q);
+
+	/* When quiescing completes, all ongoing dispatches have completed
+	 * and no new dispatch will happen towards the driver.
+	 * This ensures that later when cancel is attempted, then are not
+	 * getting processed by the queue_rq() or queue_rqs() handlers.
+	 */
+	blk_mq_quiesce_queue(q);
+
+	/*
+	 * Synchronize with any ongoing VQ callbacks, effectively quiescing
+	 * the device and preventing it from completing further requests
+	 * to the block layer. Any outstanding, incomplete requests will be

drivers/block/virtio_blk.c static int virtblk_probe(struct virtio_device *vdev) · line 1622

Quoted 10 lines
drivers/block/virtio_blk.c +10 -0
+	 * completed by virtblk_request_cancel().
+	 */
+	virtio_synchronize_cbs(vblk->vdev);
+
+	/* At this point, no new requests can enter the queue_rq() and
+	 * completion routine will not complete any new requests either for the
+	 * broken vq. Hence, it is safe to cancel all requests which are
+	 * started.
+	 */
+	blk_mq_tagset_busy_iter(&vblk->tag_set, virtblk_request_cancel, vblk);

Although virtio_synchronize_cbs() was called, a broken/malicious device can still raise IRQs. Would that lead to use-after-free or similar undefined behavior for requests that have been submitted to the device?

It seems safer to reset the device before marking the requests as failed.

Quoted 42 lines
drivers/block/virtio_blk.c +29 -0
+	blk_mq_tagset_wait_completed_request(&vblk->tag_set);
+
+	/* All pending requests are cleaned up. Time to resume so that disk
+	 * deletion can be smooth. Start the HW queues so that when queue is
+	 * unquiesced requests can again enter the driver.
+	 */
+	blk_mq_start_stopped_hw_queues(q, true);
+
+	/* Unquiescing will trigger dispatching any pending requests to the
+	 * driver which has crossed bio_queue_enter() to the driver.
+	 */
+	blk_mq_unquiesce_queue(q);
+
+	/* Wait for all pending dispatches to terminate which may have been
+	 * initiated after unquiescing.
+	 */
+	blk_mq_freeze_queue_wait(q);
+
+	/* Mark the disk dead so that once queue unfreeze, the requests
+	 * waiting at the door of bio_queue_enter() can be aborted right away.
+	 */
+	blk_mark_disk_dead(vblk->disk);
+
+	/* Unfreeze the queue so that any waiting requests will be aborted. */
+	blk_mq_unfreeze_queue_nomemrestore(q);
+}
+
 static void virtblk_remove(struct virtio_device *vdev)
 {
 	struct virtio_blk *vblk = vdev->priv;
@@ -1561,6 +1654,8 @@ static void virtblk_remove(struct virtio_device *vdev)
 	/* Make sure no work handler is accessing the device. */
 	flush_work(&vblk->config_work);
 
+	virtblk_broken_device_cleanup(vblk);
+
 	del_gendisk(vblk->disk);
 	blk_mq_free_tag_set(&vblk->tag_set);
 
Signature · 1 line
2.34.1
#13 lore raw
Quoted 98 lines

From: Stefan Hajnoczi <[email protected]>
Sent: Wednesday, May 21, 2025 8:27 PM

drivers/block/virtio_blk.c static blk_status_t virtio_queue_rq(struct

Quoted 42 lines · Parav Pandit

When the PCI device is surprise removed, requests may not complete the device as the VQ is marked as broken. Due to this, the disk deletion hangs.

Fix it by aborting the requests when the VQ is broken.

With this fix now fio completes swiftly.
An alternative of IO timeout has been considered, however when the driver knows about unresponsive block device, swiftly clearing them enables users and upper layers to react quickly.

Verified with multiple device unplug iterations with pending requests in virtio used ring and some pending with the device.

Fixes 43bb40c5b926 ("virtio_pci: Support surprise removal of virtio

pci device")

Closes:
https://lore.kernel.org/virtualization/c45dd68698cd47238c55fb73ca9b474
[email protected]/

---
changelog:
v0->v1:
- Fixed comments from Stefan to rename a cleanup function
- Improved logic for handling any outstanding requests in bio layer
- improved cancel callback to sync with ongoing done()

---
 drivers/block/virtio_blk.c | 95
++++++++++++++++++++++++++++++++++++++
 1 file changed, 95 insertions(+)
drivers/block/virtio_blk.c +0 -0
index 7cffea01d868..5212afdbd3c7 100644
@@ -435,6 +435,13 @@ static blk_status_t virtio_queue_rq(struct

blk_mq_hw_ctx *hctx,

drivers/block/virtio_blk.c static blk_status_t virtio_queue_rq(struct blk_mq_hw_ctx *hctx,

Quoted 5 lines
drivers/block/virtio_blk.c +2 -0
 	blk_status_t status;
 	int err;
 
+	/* Immediately fail all incoming requests if the vq is broken.
+	 * Once the queue is unquiesced, upper block layer flushes any

pending

drivers/block/virtio_blk.c static int virtblk_probe(struct virtio_device

Quoted 22 lines
drivers/block/virtio_blk.c +10 -0
+	 * queued requests; fail them right away.
+	 */
+	if (unlikely(virtqueue_is_broken(vblk->vqs[qid].vq)))
+		return BLK_STS_IOERR;
+
 	status = virtblk_prep_rq(hctx, vblk, req, vbr);
 	if (unlikely(status))
 		return status;
@@ -508,6 +515,11 @@ static void virtio_queue_rqs(struct rq_list *rqlist)
 	while ((req = rq_list_pop(rqlist))) {
 		struct virtio_blk_vq *this_vq = get_virtio_blk_vq(req-
 mq_hctx);
 
+		if (unlikely(virtqueue_is_broken(this_vq->vq))) {
+			rq_list_add_tail(&requeue_list, req);
+			continue;
+		}
+
 		if (vq && vq != this_vq)
 			virtblk_add_req_batch(vq, &submit_list);
drivers/block/virtio_blk.c +0 -0
 		vq = this_vq;
@@ -1554,6 +1566,87 @@ static int virtblk_probe(struct virtio_device

*vdev)

Quoted 20 lines
drivers/block/virtio_blk.c +17 -0
 	return err;
 }
 
+static bool virtblk_request_cancel(struct request *rq, void *data) {
+	struct virtblk_req *vbr = blk_mq_rq_to_pdu(rq);
+	struct virtio_blk *vblk = data;
+	struct virtio_blk_vq *vq;
+	unsigned long flags;
+
+	vq = &vblk->vqs[rq->mq_hctx->queue_num];
+
+	spin_lock_irqsave(&vq->lock, flags);
+
+	vbr->in_hdr.status = VIRTIO_BLK_S_IOERR;
+	if (blk_mq_request_started(rq) && !blk_mq_request_completed(rq))
+		blk_mq_complete_request(rq);
+
+	spin_unlock_irqrestore(&vq->lock, flags);
+	return true;
+}
Quoted 8 lines

drivers/block/virtio_blk.c static int virtblk_probe(struct virtio_device *vdev) · line 1593

Quoted 6 lines
drivers/block/virtio_blk.c +6 -0
+
+static void virtblk_broken_device_cleanup(struct virtio_blk *vblk) {
+	struct request_queue *q = vblk->disk->queue;
+
+	if (!virtqueue_is_broken(vblk->vqs[0].vq))
+		return;

Can a subset of virtqueues be broken? If so, then this code doesn't handle it.

On device removal all the VQs are broken. This check only uses a VQ to decide on. In future may be more elaborate API to have virtio_dev_broken() can be added. Prefer to keep this patch without extending many APIs given it has Fixes tag.

Quoted 7 lines

drivers/block/virtio_blk.c static int virtblk_probe(struct virtio_device · line 1594

Quoted 4 lines
drivers/block/virtio_blk.c +3 -0
+
+	/* Start freezing the queue, so that new requests keeps waitng at
+the

s/waitng/waiting/

Ack.

Quoted 26 lines

drivers/block/virtio_blk.c static int virtblk_probe(struct virtio_device · line 1595

Quoted 1 line
drivers/block/virtio_blk.c +1 -0
+	 * door of bio_queue_enter(). We cannot fully freeze the queue

because

drivers/block/virtio_blk.c static int virtblk_probe(struct virtio_device · line 1596

Quoted 1 line
drivers/block/virtio_blk.c +1 -0
+	 * freezed queue is an empty queue and there are pending requests,

so

Quoted 11 lines
drivers/block/virtio_blk.c +11 -0
+	 * only start freezing it.
+	 */
+	blk_freeze_queue_start(q);
+
+	/* When quiescing completes, all ongoing dispatches have completed
+	 * and no new dispatch will happen towards the driver.
+	 * This ensures that later when cancel is attempted, then are not
+	 * getting processed by the queue_rq() or queue_rqs() handlers.
+	 */
+	blk_mq_quiesce_queue(q);
+

drivers/block/virtio_blk.c static int virtblk_probe(struct virtio_device *vdev)

Quoted 10 lines
drivers/block/virtio_blk.c +10 -0
+	/*
+	 * Synchronize with any ongoing VQ callbacks, effectively quiescing
+	 * the device and preventing it from completing further requests
+	 * to the block layer. Any outstanding, incomplete requests will be
+	 * completed by virtblk_request_cancel().
+	 */
+	virtio_synchronize_cbs(vblk->vdev);
+
+	/* At this point, no new requests can enter the queue_rq() and
+	 * completion routine will not complete any new requests either for

the

Quoted 10 lines

drivers/block/virtio_blk.c static int virtblk_probe(struct virtio_device *vdev)

Quoted 5 lines
drivers/block/virtio_blk.c +5 -0
+	 * broken vq. Hence, it is safe to cancel all requests which are
+	 * started.
+	 */
+	blk_mq_tagset_busy_iter(&vblk->tag_set, virtblk_request_cancel,
+vblk);

Although virtio_synchronize_cbs() was called, a broken/malicious device can still raise IRQs. Would that lead to use-after-free or similar undefined behavior for requests that have been submitted to the device?

It shouldn't because vring_interrupt() also checks for the broken VQ before invoking the _done(). Once the VQ is broken and even if _done() is invoked, it wont progress further on get_buf(). And VQs are freed later in del_vq() after the device is reset as you suggested.

Quoted 2 lines

It seems safer to reset the device before marking the requests as failed.

Such addition should be avoided because when the device is surprise removed, even reset will not complete.

Quoted 44 lines

drivers/block/virtio_blk.c static int virtblk_probe(struct virtio_device *vdev)

Quoted 4 lines
drivers/block/virtio_blk.c +4 -0
+	blk_mq_tagset_wait_completed_request(&vblk->tag_set);
+
+	/* All pending requests are cleaned up. Time to resume so that disk
+	 * deletion can be smooth. Start the HW queues so that when queue

is

Quoted 6 lines
drivers/block/virtio_blk.c +6 -0
+	 * unquiesced requests can again enter the driver.
+	 */
+	blk_mq_start_stopped_hw_queues(q, true);
+
+	/* Unquiescing will trigger dispatching any pending requests to the
+	 * driver which has crossed bio_queue_enter() to the driver.

drivers/block/virtio_blk.c static int virtblk_probe(struct virtio_device *vdev)

Quoted 10 lines
drivers/block/virtio_blk.c +10 -0
+	 */
+	blk_mq_unquiesce_queue(q);
+
+	/* Wait for all pending dispatches to terminate which may have been
+	 * initiated after unquiescing.
+	 */
+	blk_mq_freeze_queue_wait(q);
+
+	/* Mark the disk dead so that once queue unfreeze, the requests
+	 * waiting at the door of bio_queue_enter() can be aborted right

away.

drivers/block/virtio_blk.c static int virtblk_probe(struct virtio_device *vdev)

Quoted 4 lines
drivers/block/virtio_blk.c +4 -0
+	 */
+	blk_mark_disk_dead(vblk->disk);
+
+	/* Unfreeze the queue so that any waiting requests will be aborted.

*/

Quoted 17 lines
drivers/block/virtio_blk.c +3 -0
+	blk_mq_unfreeze_queue_nomemrestore(q);
+}
+
 static void virtblk_remove(struct virtio_device *vdev)  {
 	struct virtio_blk *vblk = vdev->priv; @@ -1561,6 +1654,8 @@ static
 void virtblk_remove(struct virtio_device *vdev)
drivers/block/virtio_blk.c +2 -1
 	/* Make sure no work handler is accessing the device. */
 	flush_work(&vblk->config_work);
 
+	virtblk_broken_device_cleanup(vblk);
+
 	del_gendisk(vblk->disk);
 	blk_mq_free_tag_set(&vblk->tag_set);
 
--

2.34.1

#14 lore raw
Quoted 104 lines · Parav Pandit
Quoted 96 lines

From: Stefan Hajnoczi <[email protected]>
Sent: Wednesday, May 21, 2025 8:27 PM

drivers/block/virtio_blk.c static blk_status_t virtio_queue_rq(struct

Quoted 42 lines · Parav Pandit

When the PCI device is surprise removed, requests may not complete the device as the VQ is marked as broken. Due to this, the disk deletion hangs.

Fix it by aborting the requests when the VQ is broken.

With this fix now fio completes swiftly.
An alternative of IO timeout has been considered, however when the driver knows about unresponsive block device, swiftly clearing them enables users and upper layers to react quickly.

Verified with multiple device unplug iterations with pending requests in virtio used ring and some pending with the device.

Fixes 43bb40c5b926 ("virtio_pci: Support surprise removal of virtio

pci device")

Closes:
https://lore.kernel.org/virtualization/c45dd68698cd47238c55fb73ca9b474
[email protected]/

---
changelog:
v0->v1:
- Fixed comments from Stefan to rename a cleanup function
- Improved logic for handling any outstanding requests in bio layer
- improved cancel callback to sync with ongoing done()

---
 drivers/block/virtio_blk.c | 95
++++++++++++++++++++++++++++++++++++++
 1 file changed, 95 insertions(+)
drivers/block/virtio_blk.c +0 -0
index 7cffea01d868..5212afdbd3c7 100644
@@ -435,6 +435,13 @@ static blk_status_t virtio_queue_rq(struct

blk_mq_hw_ctx *hctx,

drivers/block/virtio_blk.c static blk_status_t virtio_queue_rq(struct blk_mq_hw_ctx *hctx,

Quoted 5 lines
drivers/block/virtio_blk.c +2 -0
    blk_status_t status;
    int err;
 
+   /* Immediately fail all incoming requests if the vq is broken.
+    * Once the queue is unquiesced, upper block layer flushes any

pending

drivers/block/virtio_blk.c static int virtblk_probe(struct virtio_device

Quoted 22 lines
drivers/block/virtio_blk.c +10 -0
+    * queued requests; fail them right away.
+    */
+   if (unlikely(virtqueue_is_broken(vblk->vqs[qid].vq)))
+           return BLK_STS_IOERR;
+
    status = virtblk_prep_rq(hctx, vblk, req, vbr);
    if (unlikely(status))
            return status;
@@ -508,6 +515,11 @@ static void virtio_queue_rqs(struct rq_list *rqlist)
    while ((req = rq_list_pop(rqlist))) {
            struct virtio_blk_vq *this_vq = get_virtio_blk_vq(req-
 mq_hctx);
 
+           if (unlikely(virtqueue_is_broken(this_vq->vq))) {
+                   rq_list_add_tail(&requeue_list, req);
+                   continue;
+           }
+
            if (vq && vq != this_vq)
                    virtblk_add_req_batch(vq, &submit_list);
            vq = this_vq;
drivers/block/virtio_blk.c +0 -0
@@ -1554,6 +1566,87 @@ static int virtblk_probe(struct virtio_device

*vdev)

Quoted 20 lines
drivers/block/virtio_blk.c +17 -0
    return err;
 }
 
+static bool virtblk_request_cancel(struct request *rq, void *data) {
+   struct virtblk_req *vbr = blk_mq_rq_to_pdu(rq);
+   struct virtio_blk *vblk = data;
+   struct virtio_blk_vq *vq;
+   unsigned long flags;
+
+   vq = &vblk->vqs[rq->mq_hctx->queue_num];
+
+   spin_lock_irqsave(&vq->lock, flags);
+
+   vbr->in_hdr.status = VIRTIO_BLK_S_IOERR;
+   if (blk_mq_request_started(rq) && !blk_mq_request_completed(rq))
+           blk_mq_complete_request(rq);
+
+   spin_unlock_irqrestore(&vq->lock, flags);
+   return true;
+}
Quoted 8 lines

drivers/block/virtio_blk.c static int virtblk_probe(struct virtio_device *vdev) · line 1593

Quoted 6 lines
drivers/block/virtio_blk.c +6 -0
+
+static void virtblk_broken_device_cleanup(struct virtio_blk *vblk) {
+   struct request_queue *q = vblk->disk->queue;
+
+   if (!virtqueue_is_broken(vblk->vqs[0].vq))
+           return;

Can a subset of virtqueues be broken? If so, then this code doesn't handle it.

Quoted 3 lines

On device removal all the VQs are broken. This check only uses a VQ to decide on. In future may be more elaborate API to have virtio_dev_broken() can be added. Prefer to keep this patch without extending many APIs given it has Fixes tag.

virtblk_remove() is called not just when a PCI device is hot unplugged. For example, removing the virtio_blk kernel module or unbinding a specific virtio device instance also calls it.

My concern is that virtblk_broken_device_cleanup() is only intended for the cases where all virtqueues are broken or none are broken. If just the first virtqueue is broken then it completes requests on operational virtqueues and they may still raise an interrupt.

The use-after-free I'm thinking about is when virtblk_request_cancel() -> ... -> blk_mq_end_request() has been called on a virtqueue that is not broken, followed by virtblk_done() using the struct request obtained from blk_mq_rq_from_pdu().

Maybe just adding a virtqueue_is_broken() check in virtblk_request_cancel() is enough to skip requests that are still in-flight on operational virtqueues.

Quoted 42 lines
Quoted 8 lines

drivers/block/virtio_blk.c static int virtblk_probe(struct virtio_device · line 1594

Quoted 5 lines
drivers/block/virtio_blk.c +3 -0
+
+   /* Start freezing the queue, so that new requests keeps waitng at
+the

s/waitng/waiting/

Ack.

Quoted 32 lines

drivers/block/virtio_blk.c static int virtblk_probe(struct virtio_device · line 1595

Quoted 1 line
drivers/block/virtio_blk.c +1 -0
+    * door of bio_queue_enter(). We cannot fully freeze the queue

because

drivers/block/virtio_blk.c static int virtblk_probe(struct virtio_device · line 1596

Quoted 1 line
drivers/block/virtio_blk.c +1 -0
+    * freezed queue is an empty queue and there are pending requests,

so

Quoted 11 lines
drivers/block/virtio_blk.c +11 -0
+    * only start freezing it.
+    */
+   blk_freeze_queue_start(q);
+
+   /* When quiescing completes, all ongoing dispatches have completed
+    * and no new dispatch will happen towards the driver.
+    * This ensures that later when cancel is attempted, then are not
+    * getting processed by the queue_rq() or queue_rqs() handlers.
+    */
+   blk_mq_quiesce_queue(q);
+

drivers/block/virtio_blk.c static int virtblk_probe(struct virtio_device *vdev)

Quoted 10 lines
drivers/block/virtio_blk.c +10 -0
+   /*
+    * Synchronize with any ongoing VQ callbacks, effectively quiescing
+    * the device and preventing it from completing further requests
+    * to the block layer. Any outstanding, incomplete requests will be
+    * completed by virtblk_request_cancel().
+    */
+   virtio_synchronize_cbs(vblk->vdev);
+
+   /* At this point, no new requests can enter the queue_rq() and
+    * completion routine will not complete any new requests either for

the

Quoted 5 lines
drivers/block/virtio_blk.c +5 -0
+    * broken vq. Hence, it is safe to cancel all requests which are
+    * started.
+    */
+   blk_mq_tagset_busy_iter(&vblk->tag_set, virtblk_request_cancel,
+vblk);
Quoted 7 lines
Quoted 4 lines

Although virtio_synchronize_cbs() was called, a broken/malicious device can still raise IRQs. Would that lead to use-after-free or similar undefined behavior for requests that have been submitted to the device?

It shouldn't because vring_interrupt() also checks for the broken VQ before invoking the _done(). Once the VQ is broken and even if _done() is invoked, it wont progress further on get_buf(). And VQs are freed later in del_vq() after the device is reset as you suggested.

See above about a scenario where a race can happen.

Quoted 4 lines
Quoted 3 lines

It seems safer to reset the device before marking the requests as failed.

Such addition should be avoided because when the device is surprise removed, even reset will not complete.

The virtblk_remove() function modified by this patch calls virtio_reset_device(). Is the expected behavior after this patch that virtblk_remove() spins forever?

Quoted 46 lines
Quoted 45 lines

drivers/block/virtio_blk.c static int virtblk_probe(struct virtio_device *vdev)

Quoted 5 lines
drivers/block/virtio_blk.c +4 -0
+   blk_mq_tagset_wait_completed_request(&vblk->tag_set);
+
+   /* All pending requests are cleaned up. Time to resume so that disk
+    * deletion can be smooth. Start the HW queues so that when queue

is

Quoted 6 lines
drivers/block/virtio_blk.c +6 -0
+    * unquiesced requests can again enter the driver.
+    */
+   blk_mq_start_stopped_hw_queues(q, true);
+
+   /* Unquiescing will trigger dispatching any pending requests to the
+    * driver which has crossed bio_queue_enter() to the driver.

drivers/block/virtio_blk.c static int virtblk_probe(struct virtio_device *vdev)

Quoted 10 lines
drivers/block/virtio_blk.c +10 -0
+    */
+   blk_mq_unquiesce_queue(q);
+
+   /* Wait for all pending dispatches to terminate which may have been
+    * initiated after unquiescing.
+    */
+   blk_mq_freeze_queue_wait(q);
+
+   /* Mark the disk dead so that once queue unfreeze, the requests
+    * waiting at the door of bio_queue_enter() can be aborted right

away.

drivers/block/virtio_blk.c static int virtblk_probe(struct virtio_device *vdev)

Quoted 4 lines
drivers/block/virtio_blk.c +4 -0
+    */
+   blk_mark_disk_dead(vblk->disk);
+
+   /* Unfreeze the queue so that any waiting requests will be aborted.

*/

Quoted 17 lines
drivers/block/virtio_blk.c +3 -0
+   blk_mq_unfreeze_queue_nomemrestore(q);
+}
+
 static void virtblk_remove(struct virtio_device *vdev)  {
    struct virtio_blk *vblk = vdev->priv; @@ -1561,6 +1654,8 @@ static
 void virtblk_remove(struct virtio_device *vdev)
    /* Make sure no work handler is accessing the device. */
    flush_work(&vblk->config_work);
drivers/block/virtio_blk.c +2 -1
+   virtblk_broken_device_cleanup(vblk);
+
    del_gendisk(vblk->disk);
    blk_mq_free_tag_set(&vblk->tag_set);
 
--

2.34.1

#15 lore raw
Quoted 115 lines

From: Stefan Hajnoczi <[email protected]>
Sent: Thursday, May 22, 2025 8:06 PM

Quoted 55 lines · Parav Pandit
Quoted 55 lines

From: Stefan Hajnoczi <[email protected]>
Sent: Wednesday, May 21, 2025 8:27 PM

drivers/block/virtio_blk.c static blk_status_t virtio_queue_rq(struct

Quoted 44 lines · Parav Pandit

When the PCI device is surprise removed, requests may not complete the device as the VQ is marked as broken. Due to this, the disk deletion hangs.

Fix it by aborting the requests when the VQ is broken.

With this fix now fio completes swiftly.
An alternative of IO timeout has been considered, however when the driver knows about unresponsive block device, swiftly clearing them enables users and upper layers to react quickly.

Verified with multiple device unplug iterations with pending requests in virtio used ring and some pending with the device.

Fixes 43bb40c5b926 ("virtio_pci: Support surprise removal of

virtio pci device")

Closes:
https://lore.kernel.org/virtualization/c45dd68698cd47238c55fb73ca9
b474
[email protected]/

---
changelog:
v0->v1:
- Fixed comments from Stefan to rename a cleanup function
- Improved logic for handling any outstanding requests in bio layer
- improved cancel callback to sync with ongoing done()

---
 drivers/block/virtio_blk.c | 95
++++++++++++++++++++++++++++++++++++++
 1 file changed, 95 insertions(+)
(quoted diff) +0 -0

b/drivers/block/virtio_blk.c index 7cffea01d868..5212afdbd3c7 100644

drivers/block/virtio_blk.c +0 -0
@@ -435,6 +435,13 @@ static blk_status_t virtio_queue_rq(struct

blk_mq_hw_ctx *hctx,

drivers/block/virtio_blk.c static blk_status_t virtio_queue_rq(struct blk_mq_hw_ctx *hctx,

Quoted 5 lines
drivers/block/virtio_blk.c +2 -0
    blk_status_t status;
    int err;
 
+   /* Immediately fail all incoming requests if the vq is broken.
+    * Once the queue is unquiesced, upper block layer flushes any

pending

Quoted 9 lines
Quoted 9 lines

drivers/block/virtio_blk.c static void virtio_queue_rqs(struct rq_list

Quoted 9 lines
drivers/block/virtio_blk.c +5 -0
+    * queued requests; fail them right away.
+    */
+   if (unlikely(virtqueue_is_broken(vblk->vqs[qid].vq)))
+           return BLK_STS_IOERR;
+
    status = virtblk_prep_rq(hctx, vblk, req, vbr);
    if (unlikely(status))
            return status;
@@ -508,6 +515,11 @@ static void virtio_queue_rqs(struct rq_list

*rqlist)

Quoted 35 lines
Quoted 35 lines
Quoted 13 lines
drivers/block/virtio_blk.c +5 -0
    while ((req = rq_list_pop(rqlist))) {
            struct virtio_blk_vq *this_vq = get_virtio_blk_vq(req-
 mq_hctx);
 
+           if (unlikely(virtqueue_is_broken(this_vq->vq))) {
+                   rq_list_add_tail(&requeue_list, req);
+                   continue;
+           }
+
            if (vq && vq != this_vq)
                    virtblk_add_req_batch(vq, &submit_list);
            vq = this_vq;
drivers/block/virtio_blk.c +0 -0
@@ -1554,6 +1566,87 @@ static int virtblk_probe(struct
Quoted 1 line

virtio_device

*vdev)

Quoted 20 lines
    return err;

}

drivers/block/virtio_blk.c +17 -0
+static bool virtblk_request_cancel(struct request *rq, void *data) {
+   struct virtblk_req *vbr = blk_mq_rq_to_pdu(rq);
+   struct virtio_blk *vblk = data;
+   struct virtio_blk_vq *vq;
+   unsigned long flags;
+
+   vq = &vblk->vqs[rq->mq_hctx->queue_num];
+
+   spin_lock_irqsave(&vq->lock, flags);
+
+   vbr->in_hdr.status = VIRTIO_BLK_S_IOERR;
+   if (blk_mq_request_started(rq) && !blk_mq_request_completed(rq))
+           blk_mq_complete_request(rq);
+
+   spin_unlock_irqrestore(&vq->lock, flags);
+   return true;
+}
Quoted 8 lines
Quoted 8 lines

drivers/block/virtio_blk.c static int virtblk_probe(struct virtio_device *vdev) · line 1593

Quoted 6 lines
drivers/block/virtio_blk.c +6 -0
+
+static void virtblk_broken_device_cleanup(struct virtio_blk *vblk) {
+   struct request_queue *q = vblk->disk->queue;
+
+   if (!virtqueue_is_broken(vblk->vqs[0].vq))
+           return;

Can a subset of virtqueues be broken? If so, then this code doesn't handle

it.

Quoted 1 line

On device removal all the VQs are broken. This check only uses a VQ to decide

Quoted 9 lines

on.

Quoted 1 line

In future may be more elaborate API to have virtio_dev_broken() can be

added.

Quoted 1 line

Prefer to keep this patch without extending many APIs given it has Fixes tag.

virtblk_remove() is called not just when a PCI device is hot unplugged. For example, removing the virtio_blk kernel module or unbinding a specific virtio device instance also calls it.

This is ok.

Quoted 5 lines

My concern is that virtblk_broken_device_cleanup() is only intended for the cases where all virtqueues are broken or none are broken. If just the first virtqueue is broken then it completes requests on operational virtqueues and they may still raise an interrupt.

I see that vq broken is extended for each reset scenario too lately in vp_modern_enable_vq_after_reset(). So yes, this patch which was intended for original surprise removal bug where vq broken was not done for reset cases.

I believe for fixing the cited patch, device->broken flag should be used. Max indicated this in an internal review, but I was inclined to avoid adding many changes. And hence reuse vq broken.

So one option is to extend,

virtio_break_device() to have a flag like below and check during remove().
dev->broken = true;

or to revert the patch, 43bb40c5b926, which Michael was not linking.

Quoted 5 lines

The use-after-free I'm thinking about is when virtblk_request_cancel() -> ... -> blk_mq_end_request() has been called on a virtqueue that is not broken, followed by virtblk_done() using the struct request obtained from blk_mq_rq_from_pdu().

This can happen for case when nonsurprise removal is done possibly.

Quoted 3 lines

Maybe just adding a virtqueue_is_broken() check in virtblk_request_cancel() is enough to skip requests that are still in-flight on operational virtqueues.

Well, the idea of calling request_cancel() iterator only if the VQ is broken. So in regular remove() this should not be called. Existing flow is better.

Quoted 59 lines
Quoted 48 lines
Quoted 9 lines

drivers/block/virtio_blk.c

Quoted 6 lines
drivers/block/virtio_blk.c +3 -0
+
+   /* Start freezing the queue, so that new requests keeps waitng
+at the

s/waitng/waiting/

Ack.

Quoted 37 lines
Quoted 1 line

+ * door of bio_queue_enter(). We cannot fully freeze the queue

because

Quoted 2 lines

+ * freezed queue is an empty queue and there are pending
+ requests,

so

Quoted 12 lines
drivers/block/virtio_blk.c +12 -0
+    * only start freezing it.
+    */
+   blk_freeze_queue_start(q);
+
+   /* When quiescing completes, all ongoing dispatches have completed
+    * and no new dispatch will happen towards the driver.
+    * This ensures that later when cancel is attempted, then are not
+    * getting processed by the queue_rq() or queue_rqs() handlers.
+    */
+   blk_mq_quiesce_queue(q);
+
+   /*

drivers/block/virtio_blk.c static int virtblk_probe(struct virtio_device *vdev)

Quoted 10 lines
drivers/block/virtio_blk.c +10 -0
+    * Synchronize with any ongoing VQ callbacks, effectively quiescing
+    * the device and preventing it from completing further requests
+    * to the block layer. Any outstanding, incomplete requests will be
+    * completed by virtblk_request_cancel().
+    */
+   virtio_synchronize_cbs(vblk->vdev);
+
+   /* At this point, no new requests can enter the queue_rq() and
+    * completion routine will not complete any new requests
+ either for

the

drivers/block/virtio_blk.c static int virtblk_probe(struct virtio_device *vdev)

Quoted 5 lines
drivers/block/virtio_blk.c +5 -0
+    * broken vq. Hence, it is safe to cancel all requests which are
+    * started.
+    */
+   blk_mq_tagset_busy_iter(&vblk->tag_set,
+virtblk_request_cancel, vblk);

Although virtio_synchronize_cbs() was called, a broken/malicious device can still raise IRQs. Would that lead to use-after-free or similar undefined behavior for requests that have been submitted to the

device?

Quoted 2 lines
Quoted 1 line

It shouldn't because vring_interrupt() also checks for the broken VQ before

invoking the _done().

Quoted 1 line

Once the VQ is broken and even if _done() is invoked, it wont progress

further on get_buf().

Quoted 1 line

And VQs are freed later in del_vq() after the device is reset as you suggested.

See above about a scenario where a race can happen.

Quoted 1 line
Quoted 8 lines
Quoted 3 lines
Quoted 2 lines

It seems safer to reset the device before marking the requests as failed.

Such addition should be avoided because when the device is surprise

removed, even reset will not complete.

The virtblk_remove() function modified by this patch calls virtio_reset_device(). Is the expected behavior after this patch that virtblk_remove() spins forever?

If the PCI device is truly removed physically, then yes.
This patch is not addressing such problem that existed even before the patch in fixes tag.

I have experienced this already. Adding that support is relatively bigger change (than this fix).

Quoted 49 lines
Quoted 49 lines
Quoted 48 lines

drivers/block/virtio_blk.c static int virtblk_probe(struct virtio_device *vdev)

Quoted 7 lines
drivers/block/virtio_blk.c +5 -0
+   blk_mq_tagset_wait_completed_request(&vblk->tag_set);
+
+   /* All pending requests are cleaned up. Time to resume so that disk
+    * deletion can be smooth. Start the HW queues so that when
+ queue

is

Quoted 7 lines
drivers/block/virtio_blk.c +7 -0
+    * unquiesced requests can again enter the driver.
+    */
+   blk_mq_start_stopped_hw_queues(q, true);
+
+   /* Unquiescing will trigger dispatching any pending requests to the
+    * driver which has crossed bio_queue_enter() to the driver.
+    */

drivers/block/virtio_blk.c static int virtblk_probe(struct virtio_device *vdev)

Quoted 10 lines
drivers/block/virtio_blk.c +10 -0
+   blk_mq_unquiesce_queue(q);
+
+   /* Wait for all pending dispatches to terminate which may have been
+    * initiated after unquiescing.
+    */
+   blk_mq_freeze_queue_wait(q);
+
+   /* Mark the disk dead so that once queue unfreeze, the requests
+    * waiting at the door of bio_queue_enter() can be aborted
+ right

away.

drivers/block/virtio_blk.c static int virtblk_probe(struct virtio_device *vdev)

Quoted 4 lines
drivers/block/virtio_blk.c +4 -0
+    */
+   blk_mark_disk_dead(vblk->disk);
+
+   /* Unfreeze the queue so that any waiting requests will be aborted.

*/

Quoted 17 lines
drivers/block/virtio_blk.c +3 -0
+   blk_mq_unfreeze_queue_nomemrestore(q);
+}
+
 static void virtblk_remove(struct virtio_device *vdev)  {
    struct virtio_blk *vblk = vdev->priv; @@ -1561,6 +1654,8 @@

static void virtblk_remove(struct virtio_device *vdev)

    /* Make sure no work handler is accessing the device. */
    flush_work(&vblk->config_work);
drivers/block/virtio_blk.c +2 -1
+   virtblk_broken_device_cleanup(vblk);
+
    del_gendisk(vblk->disk);
    blk_mq_free_tag_set(&vblk->tag_set);
 
--

2.34.1

#16 lore raw
Quoted 223 lines · Parav Pandit
Quoted 116 lines

From: Stefan Hajnoczi <[email protected]>
Sent: Thursday, May 22, 2025 8:06 PM

Quoted 55 lines · Parav Pandit
Quoted 55 lines

From: Stefan Hajnoczi <[email protected]>
Sent: Wednesday, May 21, 2025 8:27 PM

drivers/block/virtio_blk.c static blk_status_t virtio_queue_rq(struct

Quoted 44 lines · Parav Pandit

When the PCI device is surprise removed, requests may not complete the device as the VQ is marked as broken. Due to this, the disk deletion hangs.

Fix it by aborting the requests when the VQ is broken.

With this fix now fio completes swiftly.
An alternative of IO timeout has been considered, however when the driver knows about unresponsive block device, swiftly clearing them enables users and upper layers to react quickly.

Verified with multiple device unplug iterations with pending requests in virtio used ring and some pending with the device.

Fixes 43bb40c5b926 ("virtio_pci: Support surprise removal of

virtio pci device")

Closes:
https://lore.kernel.org/virtualization/c45dd68698cd47238c55fb73ca9
b474
[email protected]/

---
changelog:
v0->v1:
- Fixed comments from Stefan to rename a cleanup function
- Improved logic for handling any outstanding requests in bio layer
- improved cancel callback to sync with ongoing done()

---
 drivers/block/virtio_blk.c | 95
++++++++++++++++++++++++++++++++++++++
 1 file changed, 95 insertions(+)
(quoted diff) +0 -0

b/drivers/block/virtio_blk.c index 7cffea01d868..5212afdbd3c7 100644

drivers/block/virtio_blk.c +0 -0
@@ -435,6 +435,13 @@ static blk_status_t virtio_queue_rq(struct

blk_mq_hw_ctx *hctx,

drivers/block/virtio_blk.c static blk_status_t virtio_queue_rq(struct blk_mq_hw_ctx *hctx,

Quoted 5 lines
drivers/block/virtio_blk.c +2 -0
    blk_status_t status;
    int err;
 
+   /* Immediately fail all incoming requests if the vq is broken.
+    * Once the queue is unquiesced, upper block layer flushes any

pending

Quoted 9 lines
Quoted 9 lines

drivers/block/virtio_blk.c static void virtio_queue_rqs(struct rq_list

Quoted 9 lines
drivers/block/virtio_blk.c +5 -0
+    * queued requests; fail them right away.
+    */
+   if (unlikely(virtqueue_is_broken(vblk->vqs[qid].vq)))
+           return BLK_STS_IOERR;
+
    status = virtblk_prep_rq(hctx, vblk, req, vbr);
    if (unlikely(status))
            return status;
@@ -508,6 +515,11 @@ static void virtio_queue_rqs(struct rq_list

*rqlist)

Quoted 35 lines
Quoted 35 lines
Quoted 13 lines
drivers/block/virtio_blk.c +5 -0
    while ((req = rq_list_pop(rqlist))) {
            struct virtio_blk_vq *this_vq = get_virtio_blk_vq(req-
 mq_hctx);
 
+           if (unlikely(virtqueue_is_broken(this_vq->vq))) {
+                   rq_list_add_tail(&requeue_list, req);
+                   continue;
+           }
+
            if (vq && vq != this_vq)
                    virtblk_add_req_batch(vq, &submit_list);
            vq = this_vq;
drivers/block/virtio_blk.c +0 -0
@@ -1554,6 +1566,87 @@ static int virtblk_probe(struct
Quoted 1 line

virtio_device

*vdev)

Quoted 20 lines
    return err;

}

drivers/block/virtio_blk.c +17 -0
+static bool virtblk_request_cancel(struct request *rq, void *data) {
+   struct virtblk_req *vbr = blk_mq_rq_to_pdu(rq);
+   struct virtio_blk *vblk = data;
+   struct virtio_blk_vq *vq;
+   unsigned long flags;
+
+   vq = &vblk->vqs[rq->mq_hctx->queue_num];
+
+   spin_lock_irqsave(&vq->lock, flags);
+
+   vbr->in_hdr.status = VIRTIO_BLK_S_IOERR;
+   if (blk_mq_request_started(rq) && !blk_mq_request_completed(rq))
+           blk_mq_complete_request(rq);
+
+   spin_unlock_irqrestore(&vq->lock, flags);
+   return true;
+}
Quoted 8 lines
Quoted 8 lines

drivers/block/virtio_blk.c static int virtblk_probe(struct virtio_device *vdev) · line 1593

Quoted 6 lines
drivers/block/virtio_blk.c +6 -0
+
+static void virtblk_broken_device_cleanup(struct virtio_blk *vblk) {
+   struct request_queue *q = vblk->disk->queue;
+
+   if (!virtqueue_is_broken(vblk->vqs[0].vq))
+           return;

Can a subset of virtqueues be broken? If so, then this code doesn't handle

it.

Quoted 1 line

On device removal all the VQs are broken. This check only uses a VQ to decide

Quoted 9 lines

on.

Quoted 1 line

In future may be more elaborate API to have virtio_dev_broken() can be

added.

Quoted 1 line

Prefer to keep this patch without extending many APIs given it has Fixes tag.

virtblk_remove() is called not just when a PCI device is hot unplugged. For example, removing the virtio_blk kernel module or unbinding a specific virtio device instance also calls it.

This is ok.

Quoted 5 lines

My concern is that virtblk_broken_device_cleanup() is only intended for the cases where all virtqueues are broken or none are broken. If just the first virtqueue is broken then it completes requests on operational virtqueues and they may still raise an interrupt.

I see that vq broken is extended for each reset scenario too lately in vp_modern_enable_vq_after_reset(). So yes, this patch which was intended for original surprise removal bug where vq broken was not done for reset cases.

I believe for fixing the cited patch, device->broken flag should be used. Max indicated this in an internal review, but I was inclined to avoid adding many changes. And hence reuse vq broken.

So one option is to extend,

virtio_break_device() to have a flag like below and check during remove().
dev->broken = true;

or to revert the patch, 43bb40c5b926, which Michael was not linking.

Quoted 5 lines

The use-after-free I'm thinking about is when virtblk_request_cancel() -> ... -> blk_mq_end_request() has been called on a virtqueue that is not broken, followed by virtblk_done() using the struct request obtained from blk_mq_rq_from_pdu().

This can happen for case when nonsurprise removal is done possibly.

Quoted 3 lines

Maybe just adding a virtqueue_is_broken() check in virtblk_request_cancel() is enough to skip requests that are still in-flight on operational virtqueues.

Well, the idea of calling request_cancel() iterator only if the VQ is broken. So in regular remove() this should not be called. Existing flow is better.

Quoted 64 lines
Quoted 48 lines
Quoted 9 lines

drivers/block/virtio_blk.c

Quoted 6 lines
drivers/block/virtio_blk.c +3 -0
+
+   /* Start freezing the queue, so that new requests keeps waitng
+at the

s/waitng/waiting/

Ack.

Quoted 37 lines
Quoted 1 line

+ * door of bio_queue_enter(). We cannot fully freeze the queue

because

Quoted 2 lines

+ * freezed queue is an empty queue and there are pending
+ requests,

so

Quoted 12 lines
drivers/block/virtio_blk.c +12 -0
+    * only start freezing it.
+    */
+   blk_freeze_queue_start(q);
+
+   /* When quiescing completes, all ongoing dispatches have completed
+    * and no new dispatch will happen towards the driver.
+    * This ensures that later when cancel is attempted, then are not
+    * getting processed by the queue_rq() or queue_rqs() handlers.
+    */
+   blk_mq_quiesce_queue(q);
+
+   /*

drivers/block/virtio_blk.c static int virtblk_probe(struct virtio_device *vdev)

Quoted 10 lines
drivers/block/virtio_blk.c +10 -0
+    * Synchronize with any ongoing VQ callbacks, effectively quiescing
+    * the device and preventing it from completing further requests
+    * to the block layer. Any outstanding, incomplete requests will be
+    * completed by virtblk_request_cancel().
+    */
+   virtio_synchronize_cbs(vblk->vdev);
+
+   /* At this point, no new requests can enter the queue_rq() and
+    * completion routine will not complete any new requests
+ either for

the

drivers/block/virtio_blk.c static int virtblk_probe(struct virtio_device *vdev)

Quoted 5 lines
drivers/block/virtio_blk.c +5 -0
+    * broken vq. Hence, it is safe to cancel all requests which are
+    * started.
+    */
+   blk_mq_tagset_busy_iter(&vblk->tag_set,
+virtblk_request_cancel, vblk);

Although virtio_synchronize_cbs() was called, a broken/malicious device can still raise IRQs. Would that lead to use-after-free or similar undefined behavior for requests that have been submitted to the

device?

Quoted 2 lines
Quoted 1 line

It shouldn't because vring_interrupt() also checks for the broken VQ before

invoking the _done().

Quoted 1 line

Once the VQ is broken and even if _done() is invoked, it wont progress

further on get_buf().

Quoted 1 line

And VQs are freed later in del_vq() after the device is reset as you suggested.

See above about a scenario where a race can happen.

Quoted 4 lines
Quoted 3 lines

It seems safer to reset the device before marking the requests as failed.

Such addition should be avoided because when the device is surprise

removed, even reset will not complete.

Quoted 7 lines
Quoted 3 lines

The virtblk_remove() function modified by this patch calls virtio_reset_device(). Is the expected behavior after this patch that virtblk_remove() spins forever?

If the PCI device is truly removed physically, then yes.
This patch is not addressing such problem that existed even before the patch in fixes tag.

I have experienced this already. Adding that support is relatively bigger change (than this fix).

Perhaps a full solution rather than a partial solution would end up being simpler and cleaner. Instead of cutting out a special code path for the virtio-blk PCI surprise unplug case, tackling how the core virtio subsystem should handle PCI surprise unplug may give virtio_blk.c more helpful virtio APIs that make it less complex. It's up to you.

Stefan

Quoted 50 lines
Quoted 50 lines
Quoted 50 lines
Quoted 49 lines

drivers/block/virtio_blk.c static int virtblk_probe(struct virtio_device *vdev)

Quoted 8 lines
drivers/block/virtio_blk.c +5 -0
+   blk_mq_tagset_wait_completed_request(&vblk->tag_set);
+
+   /* All pending requests are cleaned up. Time to resume so that disk
+    * deletion can be smooth. Start the HW queues so that when
+ queue

is

Quoted 7 lines
drivers/block/virtio_blk.c +7 -0
+    * unquiesced requests can again enter the driver.
+    */
+   blk_mq_start_stopped_hw_queues(q, true);
+
+   /* Unquiescing will trigger dispatching any pending requests to the
+    * driver which has crossed bio_queue_enter() to the driver.
+    */

drivers/block/virtio_blk.c static int virtblk_probe(struct virtio_device *vdev)

Quoted 10 lines
drivers/block/virtio_blk.c +10 -0
+   blk_mq_unquiesce_queue(q);
+
+   /* Wait for all pending dispatches to terminate which may have been
+    * initiated after unquiescing.
+    */
+   blk_mq_freeze_queue_wait(q);
+
+   /* Mark the disk dead so that once queue unfreeze, the requests
+    * waiting at the door of bio_queue_enter() can be aborted
+ right

away.

drivers/block/virtio_blk.c static int virtblk_probe(struct virtio_device *vdev)

Quoted 4 lines
drivers/block/virtio_blk.c +4 -0
+    */
+   blk_mark_disk_dead(vblk->disk);
+
+   /* Unfreeze the queue so that any waiting requests will be aborted.

*/

Quoted 17 lines
drivers/block/virtio_blk.c +3 -0
+   blk_mq_unfreeze_queue_nomemrestore(q);
+}
+
 static void virtblk_remove(struct virtio_device *vdev)  {
    struct virtio_blk *vblk = vdev->priv; @@ -1561,6 +1654,8 @@

static void virtblk_remove(struct virtio_device *vdev)

    /* Make sure no work handler is accessing the device. */
    flush_work(&vblk->config_work);
drivers/block/virtio_blk.c +2 -1
+   virtblk_broken_device_cleanup(vblk);
+
    del_gendisk(vblk->disk);
    blk_mq_free_tag_set(&vblk->tag_set);
 
--

2.34.1

#17 lore raw
Quoted 262 lines

From: Stefan Hajnoczi <[email protected]>
Sent: Thursday, May 22, 2025 11:43 PM

Quoted 6 lines · Parav Pandit
Quoted 6 lines

From: Stefan Hajnoczi <[email protected]>
Sent: Thursday, May 22, 2025 8:06 PM

On Wed, May 21, 2025 at 10:57 PM Parav Pandit <[email protected]>

Quoted 87 lines · wrote:
Quoted 87 lines
Quoted 58 lines
Quoted 58 lines

From: Stefan Hajnoczi <[email protected]>
Sent: Wednesday, May 21, 2025 8:27 PM

Quoted 45 lines · Parav Pandit

When the PCI device is surprise removed, requests may not complete the device as the VQ is marked as broken. Due to this, the disk deletion hangs.

Fix it by aborting the requests when the VQ is broken.

With this fix now fio completes swiftly.
An alternative of IO timeout has been considered, however when the driver knows about unresponsive block device, swiftly clearing them enables users and upper layers to react quickly.

Verified with multiple device unplug iterations with pending requests in virtio used ring and some pending with the device.

Fixes 43bb40c5b926 ("virtio_pci: Support surprise removal of

virtio pci device")

Closes:
https://lore.kernel.org/virtualization/c45dd68698cd47238c55fb7
3ca9
b474
[email protected]/

---
changelog:
v0->v1:
- Fixed comments from Stefan to rename a cleanup function
- Improved logic for handling any outstanding requests in bio layer
- improved cancel callback to sync with ongoing done()

---
 drivers/block/virtio_blk.c | 95
++++++++++++++++++++++++++++++++++++++
 1 file changed, 95 insertions(+)
(quoted diff) +0 -0

b/drivers/block/virtio_blk.c index 7cffea01d868..5212afdbd3c7 100644

drivers/block/virtio_blk.c +0 -0
@@ -435,6 +435,13 @@ static blk_status_t
Quoted 1 line

virtio_queue_rq(struct

blk_mq_hw_ctx *hctx,

drivers/block/virtio_blk.c

Quoted 6 lines
    blk_status_t status;
    int err;
drivers/block/virtio_blk.c +3 -0
+   /* Immediately fail all incoming requests if the vq is broken.
+    * Once the queue is unquiesced, upper block layer flushes
+ any

pending

Quoted 10 lines
Quoted 10 lines
Quoted 10 lines
drivers/block/virtio_blk.c +5 -0
+    * queued requests; fail them right away.
+    */
+   if (unlikely(virtqueue_is_broken(vblk->vqs[qid].vq)))
+           return BLK_STS_IOERR;
+
    status = virtblk_prep_rq(hctx, vblk, req, vbr);
    if (unlikely(status))
            return status;
@@ -508,6 +515,11 @@ static void virtio_queue_rqs(struct

rq_list

*rqlist)

Quoted 18 lines
Quoted 18 lines
Quoted 13 lines

while ((req = rq_list_pop(rqlist))) {
struct virtio_blk_vq *this_vq =
get_virtio_blk_vq(req- mq_hctx);

drivers/block/virtio_blk.c +5 -0
+           if (unlikely(virtqueue_is_broken(this_vq->vq))) {
+                   rq_list_add_tail(&requeue_list, req);
+                   continue;
+           }
+
            if (vq && vq != this_vq)
                    virtblk_add_req_batch(vq, &submit_list);
            vq = this_vq;
@@ -1554,6 +1566,87 @@ static int virtblk_probe(struct
Quoted 1 line

virtio_device

*vdev)

Quoted 3 lines
    return err;

}

Quoted 12 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
drivers/block/virtio_blk.c +4 -0
+static bool virtblk_request_cancel(struct request *rq, void *data) {
+   struct virtblk_req *vbr = blk_mq_rq_to_pdu(rq);
+   struct virtio_blk *vblk = data;
+   struct virtio_blk_vq *vq;
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

drivers/block/virtio_blk.c static int virtblk_probe(struct virtio_device *vdev)

Quoted 8 lines
drivers/block/virtio_blk.c +8 -0
+   unsigned long flags;
+
+   vq = &vblk->vqs[rq->mq_hctx->queue_num];
+
+   spin_lock_irqsave(&vq->lock, flags);
+
+   vbr->in_hdr.status = VIRTIO_BLK_S_IOERR;
+   if (blk_mq_request_started(rq) &&

!blk_mq_request_completed(rq))

Quoted 14 lines
Quoted 14 lines
Quoted 14 lines
Quoted 14 lines

drivers/block/virtio_blk.c static int virtblk_probe(struct virtio_device *vdev) · line 1593

Quoted 11 lines
drivers/block/virtio_blk.c +11 -0
+           blk_mq_complete_request(rq);
+
+   spin_unlock_irqrestore(&vq->lock, flags);
+   return true;
+}
+
+static void virtblk_broken_device_cleanup(struct virtio_blk *vblk) {
+   struct request_queue *q = vblk->disk->queue;
+
+   if (!virtqueue_is_broken(vblk->vqs[0].vq))
+           return;

Can a subset of virtqueues be broken? If so, then this code doesn't handle

Quoted 8 lines
Quoted 8 lines

it.

Quoted 2 lines

On device removal all the VQs are broken. This check only uses a VQ to decide

on.

Quoted 2 lines

In future may be more elaborate API to have virtio_dev_broken() can be

added.

Quoted 1 line

Prefer to keep this patch without extending many APIs given it has Fixes

tag.

Quoted 5 lines
Quoted 5 lines

virtblk_remove() is called not just when a PCI device is hot unplugged. For example, removing the virtio_blk kernel module or unbinding a specific virtio device instance also calls it.

Quoted 8 lines

This is ok.

Quoted 5 lines

My concern is that virtblk_broken_device_cleanup() is only intended for the cases where all virtqueues are broken or none are broken. If just the first virtqueue is broken then it completes requests on operational virtqueues and they may still raise an interrupt.

I see that vq broken is extended for each reset scenario too lately in

vp_modern_enable_vq_after_reset().

Quoted 1 line

So yes, this patch which was intended for original surprise removal bug

where vq broken was not done for reset cases.

Quoted 3 lines

I believe for fixing the cited patch, device->broken flag should be used. Max indicated this in an internal review, but I was inclined to avoid adding

many changes.

Quoted 16 lines

And hence reuse vq broken.

So one option is to extend,

virtio_break_device() to have a flag like below and check during remove().
dev->broken = true;

or to revert the patch, 43bb40c5b926, which Michael was not linking.

Quoted 7 lines

The use-after-free I'm thinking about is when virtblk_request_cancel()
-> ... -> blk_mq_end_request() has been called on a virtqueue that -> is
not broken, followed by virtblk_done() using the struct request obtained from blk_mq_rq_from_pdu().

Quoted 30 lines

This can happen for case when nonsurprise removal is done possibly.

Quoted 3 lines

Maybe just adding a virtqueue_is_broken() check in virtblk_request_cancel() is enough to skip requests that are still in-flight on operational virtqueues.

Well, the idea of calling request_cancel() iterator only if the VQ is broken. So in regular remove() this should not be called. Existing flow is better.

Quoted 22 lines
Quoted 22 lines
Quoted 9 lines

drivers/block/virtio_blk.c

Quoted 6 lines
drivers/block/virtio_blk.c +3 -0
+
+   /* Start freezing the queue, so that new requests keeps
+waitng at the

s/waitng/waiting/

Ack.

Quoted 11 lines
Quoted 2 lines

+ * door of bio_queue_enter(). We cannot fully freeze the
+ queue

because

Quoted 2 lines

+ * freezed queue is an empty queue and there are pending
+ requests,

so

Quoted 5 lines
drivers/block/virtio_blk.c +5 -0
+    * only start freezing it.
+    */
+   blk_freeze_queue_start(q);
+
+   /* When quiescing completes, all ongoing dispatches have

completed

Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

drivers/block/virtio_blk.c static int virtblk_probe(struct virtio_device *vdev)

Quoted 8 lines
drivers/block/virtio_blk.c +8 -0
+    * and no new dispatch will happen towards the driver.
+    * This ensures that later when cancel is attempted, then are not
+    * getting processed by the queue_rq() or queue_rqs() handlers.
+    */
+   blk_mq_quiesce_queue(q);
+
+   /*
+    * Synchronize with any ongoing VQ callbacks, effectively

quiescing

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

drivers/block/virtio_blk.c

Quoted 2 lines
drivers/block/virtio_blk.c +2 -0
+    * the device and preventing it from completing further requests
+    * to the block layer. Any outstanding, incomplete requests will

be

Quoted 20 lines
Quoted 20 lines
Quoted 14 lines
Quoted 14 lines

drivers/block/virtio_blk.c static int virtblk_probe(struct virtio_device *vdev)

Quoted 7 lines
drivers/block/virtio_blk.c +7 -0
+    * completed by virtblk_request_cancel().
+    */
+   virtio_synchronize_cbs(vblk->vdev);
+
+   /* At this point, no new requests can enter the queue_rq() and
+    * completion routine will not complete any new requests
+ either for

the

Quoted 5 lines
drivers/block/virtio_blk.c +5 -0
+    * broken vq. Hence, it is safe to cancel all requests which are
+    * started.
+    */
+   blk_mq_tagset_busy_iter(&vblk->tag_set,
+virtblk_request_cancel, vblk);
Quoted 4 lines
Quoted 4 lines

Although virtio_synchronize_cbs() was called, a broken/malicious device can still raise IRQs. Would that lead to use-after-free or similar undefined behavior for requests that have been submitted to the

device?

Quoted 1 line
Quoted 1 line
Quoted 7 lines
Quoted 7 lines
Quoted 2 lines

It shouldn't because vring_interrupt() also checks for the broken VQ before

invoking the _done().

Quoted 2 lines

Once the VQ is broken and even if _done() is invoked, it wont progress

further on get_buf().

Quoted 1 line

And VQs are freed later in del_vq() after the device is reset as you

suggested.

Quoted 5 lines
Quoted 5 lines

See above about a scenario where a race can happen.

Quoted 2 lines
Quoted 2 lines

It seems safer to reset the device before marking the requests as

failed.

Quoted 11 lines
Quoted 9 lines
Quoted 3 lines
Quoted 1 line

Such addition should be avoided because when the device is surprise

removed, even reset will not complete.

The virtblk_remove() function modified by this patch calls virtio_reset_device(). Is the expected behavior after this patch that
virtblk_remove() spins forever?

If the PCI device is truly removed physically, then yes. This patch is not addressing such problem that existed even before the

patch in fixes tag.

Quoted 1 line
Quoted 9 lines
Quoted 1 line

I have experienced this already. Adding that support is relatively bigger

change (than this fix).

Perhaps a full solution rather than a partial solution would end up being simpler and cleaner. Instead of cutting out a special code path for the virtio- blk PCI surprise unplug case, tackling how the core virtio subsystem should handle PCI surprise unplug may give virtio_blk.c more helpful virtio APIs that make it less complex. It's up to you.

Each upper layer is different in how to quiesce it. so not sure how that code can be generalized in lower layers. But I agree that a at minimum common PCI layer signaling layer indicating device surprise removal is useful.

For the full solution it appeared lot more changes needed likely on top of this. So present objective is restore the regression caused by the commit in fixes tag.

#18 lore raw
In reply to Parav Pandit

Hi Stefan,

Quoted 152 lines

From: Parav Pandit <[email protected]>
Sent: Thursday, May 22, 2025 8:26 PM

Quoted 4 lines

From: Stefan Hajnoczi <[email protected]>
Sent: Thursday, May 22, 2025 8:06 PM

On Wed, May 21, 2025 at 10:57 PM Parav Pandit <[email protected]>

Quoted 85 lines · wrote:
Quoted 57 lines
Quoted 57 lines

From: Stefan Hajnoczi <[email protected]>
Sent: Wednesday, May 21, 2025 8:27 PM

drivers/block/virtio_blk.c static blk_status_t virtio_queue_rq(struct

Quoted 45 lines · Parav Pandit

When the PCI device is surprise removed, requests may not complete the device as the VQ is marked as broken. Due to this, the disk deletion hangs.

Fix it by aborting the requests when the VQ is broken.

With this fix now fio completes swiftly.
An alternative of IO timeout has been considered, however when the driver knows about unresponsive block device, swiftly clearing them enables users and upper layers to react quickly.

Verified with multiple device unplug iterations with pending requests in virtio used ring and some pending with the device.

Fixes 43bb40c5b926 ("virtio_pci: Support surprise removal of

virtio pci device")

Closes:
https://lore.kernel.org/virtualization/c45dd68698cd47238c55fb73c
a9
b474
[email protected]/

---
changelog:
v0->v1:
- Fixed comments from Stefan to rename a cleanup function
- Improved logic for handling any outstanding requests in bio layer
- improved cancel callback to sync with ongoing done()

---
 drivers/block/virtio_blk.c | 95
++++++++++++++++++++++++++++++++++++++
 1 file changed, 95 insertions(+)
(quoted diff) +0 -0

b/drivers/block/virtio_blk.c index 7cffea01d868..5212afdbd3c7 100644

drivers/block/virtio_blk.c +0 -0
@@ -435,6 +435,13 @@ static blk_status_t virtio_queue_rq(struct

blk_mq_hw_ctx *hctx,

drivers/block/virtio_blk.c static blk_status_t virtio_queue_rq(struct blk_mq_hw_ctx *hctx,

Quoted 6 lines
drivers/block/virtio_blk.c +3 -0
    blk_status_t status;
    int err;
 
+   /* Immediately fail all incoming requests if the vq is broken.
+    * Once the queue is unquiesced, upper block layer flushes
+ any

pending

Quoted 9 lines
Quoted 9 lines

drivers/block/virtio_blk.c static void virtio_queue_rqs(struct rq_list

Quoted 9 lines
drivers/block/virtio_blk.c +5 -0
+    * queued requests; fail them right away.
+    */
+   if (unlikely(virtqueue_is_broken(vblk->vqs[qid].vq)))
+           return BLK_STS_IOERR;
+
    status = virtblk_prep_rq(hctx, vblk, req, vbr);
    if (unlikely(status))
            return status;
@@ -508,6 +515,11 @@ static void virtio_queue_rqs(struct rq_list

*rqlist)

Quoted 18 lines
Quoted 18 lines
Quoted 13 lines
drivers/block/virtio_blk.c +5 -0
    while ((req = rq_list_pop(rqlist))) {
            struct virtio_blk_vq *this_vq =
 get_virtio_blk_vq(req- mq_hctx);
 
+           if (unlikely(virtqueue_is_broken(this_vq->vq))) {
+                   rq_list_add_tail(&requeue_list, req);
+                   continue;
+           }
+
            if (vq && vq != this_vq)
                    virtblk_add_req_batch(vq, &submit_list);
            vq = this_vq;
drivers/block/virtio_blk.c +0 -0
@@ -1554,6 +1566,87 @@ static int virtblk_probe(struct
Quoted 1 line

virtio_device

*vdev)

Quoted 3 lines
    return err;

}

Quoted 12 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
drivers/block/virtio_blk.c +4 -0
+static bool virtblk_request_cancel(struct request *rq, void *data) {
+   struct virtblk_req *vbr = blk_mq_rq_to_pdu(rq);
+   struct virtio_blk *vblk = data;
+   struct virtio_blk_vq *vq;
Quoted 8 lines
Quoted 8 lines

drivers/block/virtio_blk.c static int virtblk_probe(struct virtio_device *vdev)

Quoted 8 lines
drivers/block/virtio_blk.c +8 -0
+   unsigned long flags;
+
+   vq = &vblk->vqs[rq->mq_hctx->queue_num];
+
+   spin_lock_irqsave(&vq->lock, flags);
+
+   vbr->in_hdr.status = VIRTIO_BLK_S_IOERR;
+   if (blk_mq_request_started(rq) &&

!blk_mq_request_completed(rq))

Quoted 14 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
drivers/block/virtio_blk.c +6 -0
+           blk_mq_complete_request(rq);
+
+   spin_unlock_irqrestore(&vq->lock, flags);
+   return true;
+}
+
Quoted 8 lines
Quoted 8 lines

drivers/block/virtio_blk.c static int virtblk_probe(struct virtio_device *vdev) · line 1593

Quoted 5 lines
drivers/block/virtio_blk.c +5 -0
+static void virtblk_broken_device_cleanup(struct virtio_blk *vblk) {
+   struct request_queue *q = vblk->disk->queue;
+
+   if (!virtqueue_is_broken(vblk->vqs[0].vq))
+           return;

Can a subset of virtqueues be broken? If so, then this code doesn't handle

Quoted 8 lines

it.

Quoted 2 lines

On device removal all the VQs are broken. This check only uses a VQ to decide

on.

Quoted 2 lines

In future may be more elaborate API to have virtio_dev_broken() can be

added.

Quoted 1 line

Prefer to keep this patch without extending many APIs given it has Fixes

tag.

Quoted 5 lines

virtblk_remove() is called not just when a PCI device is hot unplugged. For example, removing the virtio_blk kernel module or unbinding a specific virtio device instance also calls it.

This is ok.

Quoted 5 lines

My concern is that virtblk_broken_device_cleanup() is only intended for the cases where all virtqueues are broken or none are broken. If just the first virtqueue is broken then it completes requests on operational virtqueues and they may still raise an interrupt.

I see that vq broken is extended for each reset scenario too lately in vp_modern_enable_vq_after_reset().
So yes, this patch which was intended for original surprise removal bug where vq broken was not done for reset cases.

I believe for fixing the cited patch, device->broken flag should be used. Max indicated this in an internal review, but I was inclined to avoid adding many changes.
And hence reuse vq broken.

Quoted 5 lines

So one option is to extend,

virtio_break_device() to have a flag like below and check during remove().
dev->broken = true;

I dig further.
VQ resize is the only user of dynamically break-unbreak a VQ; and specific only to vnet device. So vq[0].broken check in this patch is sufficient in this proposed function without adding above dev->broken check.

If no further comments, I would like to post v2 addressing your and Michael's inputs. Please let me know.

Quoted 2 lines

or to revert the patch, 43bb40c5b926, which Michael was not linking.

#19 lore raw
Quoted 161 lines · Parav Pandit

Hi Stefan,

Quoted 152 lines

From: Parav Pandit <[email protected]>
Sent: Thursday, May 22, 2025 8:26 PM

Quoted 4 lines

From: Stefan Hajnoczi <[email protected]>
Sent: Thursday, May 22, 2025 8:06 PM

On Wed, May 21, 2025 at 10:57 PM Parav Pandit <[email protected]>

Quoted 85 lines · wrote:
Quoted 57 lines
Quoted 57 lines

From: Stefan Hajnoczi <[email protected]>
Sent: Wednesday, May 21, 2025 8:27 PM

drivers/block/virtio_blk.c static blk_status_t virtio_queue_rq(struct

Quoted 45 lines · Parav Pandit

When the PCI device is surprise removed, requests may not complete the device as the VQ is marked as broken. Due to this, the disk deletion hangs.

Fix it by aborting the requests when the VQ is broken.

With this fix now fio completes swiftly.
An alternative of IO timeout has been considered, however when the driver knows about unresponsive block device, swiftly clearing them enables users and upper layers to react quickly.

Verified with multiple device unplug iterations with pending requests in virtio used ring and some pending with the device.

Fixes 43bb40c5b926 ("virtio_pci: Support surprise removal of

virtio pci device")

Closes:
https://lore.kernel.org/virtualization/c45dd68698cd47238c55fb73c
a9
b474
[email protected]/

---
changelog:
v0->v1:
- Fixed comments from Stefan to rename a cleanup function
- Improved logic for handling any outstanding requests in bio layer
- improved cancel callback to sync with ongoing done()

---
 drivers/block/virtio_blk.c | 95
++++++++++++++++++++++++++++++++++++++
 1 file changed, 95 insertions(+)
(quoted diff) +0 -0

b/drivers/block/virtio_blk.c index 7cffea01d868..5212afdbd3c7 100644

drivers/block/virtio_blk.c +0 -0
@@ -435,6 +435,13 @@ static blk_status_t virtio_queue_rq(struct

blk_mq_hw_ctx *hctx,

drivers/block/virtio_blk.c static blk_status_t virtio_queue_rq(struct blk_mq_hw_ctx *hctx,

Quoted 6 lines
drivers/block/virtio_blk.c +3 -0
    blk_status_t status;
    int err;
 
+   /* Immediately fail all incoming requests if the vq is broken.
+    * Once the queue is unquiesced, upper block layer flushes
+ any

pending

Quoted 9 lines
Quoted 9 lines

drivers/block/virtio_blk.c static void virtio_queue_rqs(struct rq_list

Quoted 9 lines
drivers/block/virtio_blk.c +5 -0
+    * queued requests; fail them right away.
+    */
+   if (unlikely(virtqueue_is_broken(vblk->vqs[qid].vq)))
+           return BLK_STS_IOERR;
+
    status = virtblk_prep_rq(hctx, vblk, req, vbr);
    if (unlikely(status))
            return status;
@@ -508,6 +515,11 @@ static void virtio_queue_rqs(struct rq_list

*rqlist)

Quoted 18 lines
Quoted 18 lines
Quoted 13 lines
drivers/block/virtio_blk.c +5 -0
    while ((req = rq_list_pop(rqlist))) {
            struct virtio_blk_vq *this_vq =
 get_virtio_blk_vq(req- mq_hctx);
 
+           if (unlikely(virtqueue_is_broken(this_vq->vq))) {
+                   rq_list_add_tail(&requeue_list, req);
+                   continue;
+           }
+
            if (vq && vq != this_vq)
                    virtblk_add_req_batch(vq, &submit_list);
            vq = this_vq;
drivers/block/virtio_blk.c +0 -0
@@ -1554,6 +1566,87 @@ static int virtblk_probe(struct
Quoted 1 line

virtio_device

*vdev)

Quoted 3 lines
    return err;

}

Quoted 12 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
drivers/block/virtio_blk.c +4 -0
+static bool virtblk_request_cancel(struct request *rq, void *data) {
+   struct virtblk_req *vbr = blk_mq_rq_to_pdu(rq);
+   struct virtio_blk *vblk = data;
+   struct virtio_blk_vq *vq;
Quoted 8 lines
Quoted 8 lines

drivers/block/virtio_blk.c static int virtblk_probe(struct virtio_device *vdev)

Quoted 8 lines
drivers/block/virtio_blk.c +8 -0
+   unsigned long flags;
+
+   vq = &vblk->vqs[rq->mq_hctx->queue_num];
+
+   spin_lock_irqsave(&vq->lock, flags);
+
+   vbr->in_hdr.status = VIRTIO_BLK_S_IOERR;
+   if (blk_mq_request_started(rq) &&

!blk_mq_request_completed(rq))

Quoted 14 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
drivers/block/virtio_blk.c +6 -0
+           blk_mq_complete_request(rq);
+
+   spin_unlock_irqrestore(&vq->lock, flags);
+   return true;
+}
+
Quoted 8 lines
Quoted 8 lines

drivers/block/virtio_blk.c static int virtblk_probe(struct virtio_device *vdev) · line 1593

Quoted 5 lines
drivers/block/virtio_blk.c +5 -0
+static void virtblk_broken_device_cleanup(struct virtio_blk *vblk) {
+   struct request_queue *q = vblk->disk->queue;
+
+   if (!virtqueue_is_broken(vblk->vqs[0].vq))
+           return;

Can a subset of virtqueues be broken? If so, then this code doesn't handle

Quoted 8 lines

it.

Quoted 2 lines

On device removal all the VQs are broken. This check only uses a VQ to decide

on.

Quoted 2 lines

In future may be more elaborate API to have virtio_dev_broken() can be

added.

Quoted 1 line

Prefer to keep this patch without extending many APIs given it has Fixes

tag.

Quoted 5 lines

virtblk_remove() is called not just when a PCI device is hot unplugged. For example, removing the virtio_blk kernel module or unbinding a specific virtio device instance also calls it.

This is ok.

Quoted 5 lines

My concern is that virtblk_broken_device_cleanup() is only intended for the cases where all virtqueues are broken or none are broken. If just the first virtqueue is broken then it completes requests on operational virtqueues and they may still raise an interrupt.

I see that vq broken is extended for each reset scenario too lately in vp_modern_enable_vq_after_reset().
So yes, this patch which was intended for original surprise removal bug where vq broken was not done for reset cases.

I believe for fixing the cited patch, device->broken flag should be used. Max indicated this in an internal review, but I was inclined to avoid adding many changes.
And hence reuse vq broken.

Quoted 5 lines

So one option is to extend,

virtio_break_device() to have a flag like below and check during remove().
dev->broken = true;

Quoted 6 lines

I dig further.
VQ resize is the only user of dynamically break-unbreak a VQ; and specific only to vnet device. So vq[0].broken check in this patch is sufficient in this proposed function without adding above dev->broken check.

If no further comments, I would like to post v2 addressing your and Michael's inputs. Please let me know.

Yes, please go ahead with the next revision.

Stefan