[Devel] [PATCH hci-8.1] drivers/vhost/blk: drop completed requests when the vq has no worker

Alexander Ivanov alexander.ivanov at virtuozzo.com
Wed Nov 18 19:46:37 MSK 2026


vhost_blk_req_done() ignores the return value of vhost_vq_work_queue().
Once a vhost_task worker is killed, vhost_worker_killed() clears
vq->worker, so a request completing after that stays on llhead forever:
its bio, iov and bounce buffer leak and req_inflight[] is never
decremented. vhost_blk_flush() then waits for it with no timeout, which
hangs release, VHOST_RESET_OWNER and VHOST_SET_FEATURES.

Free such requests from a work item on the system workqueue.
Commit 1eceddeeb6a9 ("vhost-scsi: Handle vhost_vq_work_queue failures
for cmds") frees them from the completion path for vhost-scsi, but
vhost-blk cannot do that in bi_end_io: iov and the bounce buffer are
kvmalloc()ed with guest controlled sizes, and vfree() may sleep when
bi_end_io runs in atomic process context, e.g. under the blk-mq dispatch
RCU lock.

Once a request is on llhead, the worker or the drop work may finish it,
let vhost_blk_flush() return and free req and blk while
vhost_blk_req_done() still uses them. Hold an extra req_inflight[]
reference until the callback is done with blk. bi_end_io may run in
interrupt context, so make flush_lock irq-safe. Take it only when
req_inflight[] drops to zero, so the completion path normally stays
lock-free, and drop to zero under the lock, so vhost_blk_flush() cannot
return and free blk before the completion path unlocks it. Wait for the
drop work before freeing the requests.

https://virtuozzo.atlassian.net/browse/VSTOR-152368
Feature: vhost-blk: in-kernel accelerator for virtio-blk guests

Signed-off-by: Alexander Ivanov <alexander.ivanov at virtuozzo.com>
---
 drivers/vhost/blk.c | 68 ++++++++++++++++++++++++++++++++++-----------
 1 file changed, 52 insertions(+), 16 deletions(-)

diff --git a/drivers/vhost/blk.c b/drivers/vhost/blk.c
index b170439a1806..a0edb3fb7f26 100644
--- a/drivers/vhost/blk.c
+++ b/drivers/vhost/blk.c
@@ -90,6 +90,7 @@ struct vhost_blk_vq {
 	struct iovec iov[UIO_MAXIOV];
 	struct llist_head llhead;
 	struct vhost_work work;
+	struct work_struct drop_work;
 };
 
 struct vhost_blk {
@@ -145,6 +146,8 @@ static inline int vhost_blk_set_status(struct vhost_blk_req *req, u8 status)
 	return 0;
 }
 
+static void vhost_blk_put_inflight(struct vhost_blk *blk, int bin);
+
 static void vhost_blk_req_done(struct bio *bio)
 {
 	struct vhost_blk_req *req = bio->bi_private;
@@ -167,8 +170,15 @@ static void vhost_blk_req_done(struct bio *bio)
 	}
 
 	if (atomic_dec_and_test(&req->bio_nr)) {
-		llist_add(&req->llnode, &req->blk_vq->llhead);
-		vhost_vq_work_queue(&req->blk_vq->vq, &req->blk_vq->work);
+		struct vhost_blk_vq *blk_vq = req->blk_vq;
+		struct vhost_blk *blk = req->blk;
+		int bin = req->req_bin;
+
+		atomic_inc(&blk->req_inflight[bin]);
+		llist_add(&req->llnode, &blk_vq->llhead);
+		if (!vhost_vq_work_queue(&blk_vq->vq, &blk_vq->work))
+			schedule_work(&blk_vq->drop_work);
+		vhost_blk_put_inflight(blk, bin);
 	}
 }
 
@@ -399,22 +409,45 @@ static inline void vhost_blk_bio_send(struct vhost_blk_req *req)
 
 static inline void note_request(struct vhost_blk_req *req)
 {
-	spin_lock(&req->blk->flush_lock);
+	spin_lock_irq(&req->blk->flush_lock);
 	req->req_bin = req->blk->new_req_bin;
 	atomic_inc(&req->blk->req_inflight[req->req_bin]);
-	spin_unlock(&req->blk->flush_lock);
+	spin_unlock_irq(&req->blk->flush_lock);
 }
 
-static inline void forget_request(struct vhost_blk_req *req)
+static void vhost_blk_put_inflight(struct vhost_blk *blk, int bin)
 {
-	struct vhost_blk *blk = req->blk;
-	int zero;
+	unsigned long flags;
 
-	spin_lock(&blk->flush_lock);
-	zero = atomic_dec_and_test(&blk->req_inflight[req->req_bin]);
-	if (zero && blk->during_flush)
+	if (!atomic_dec_and_lock_irqsave(&blk->req_inflight[bin],
+					 &blk->flush_lock, flags))
+		return;
+	if (blk->during_flush)
 		wake_up(&blk->flush_wait);
-	spin_unlock(&blk->flush_lock);
+	spin_unlock_irqrestore(&blk->flush_lock, flags);
+}
+
+static inline void forget_request(struct vhost_blk_req *req)
+{
+	vhost_blk_put_inflight(req->blk, req->req_bin);
+}
+
+static void vhost_blk_drop_req(struct vhost_blk_req *req)
+{
+	vhost_blk_req_cleanup(req);
+	forget_request(req);
+}
+
+static void vhost_blk_drop_reqs(struct work_struct *work)
+{
+	struct vhost_blk_vq *blk_vq;
+	struct vhost_blk_req *req, *tmp;
+	struct llist_node *llnode;
+
+	blk_vq = container_of(work, struct vhost_blk_vq, drop_work);
+	llnode = llist_del_all(&blk_vq->llhead);
+	llist_for_each_entry_safe(req, tmp, llnode, llnode)
+		vhost_blk_drop_req(req);
 }
 
 static int vhost_blk_req_submit(struct vhost_blk_req *req)
@@ -641,12 +674,12 @@ static void vhost_blk_flush(struct vhost_blk *blk)
 {
 	int flush_bin;
 
-	spin_lock(&blk->flush_lock);
+	spin_lock_irq(&blk->flush_lock);
 	WARN_ON(blk->during_flush);
 	blk->during_flush = 1;
 	flush_bin = blk->new_req_bin;
 	blk->new_req_bin = !blk->new_req_bin;
-	spin_unlock(&blk->flush_lock);
+	spin_unlock_irq(&blk->flush_lock);
 
 	vhost_dev_flush(&blk->dev);
 	/*
@@ -660,9 +693,9 @@ static void vhost_blk_flush(struct vhost_blk *blk)
 	 */
 	wait_event(blk->flush_wait, !atomic_read(&blk->req_inflight[flush_bin]));
 
-	spin_lock(&blk->flush_lock);
+	spin_lock_irq(&blk->flush_lock);
 	blk->during_flush = 0;
-	spin_unlock(&blk->flush_lock);
+	spin_unlock_irq(&blk->flush_lock);
 }
 
 static void vhost_blk_drop_backend(struct vhost_blk *blk)
@@ -684,6 +717,7 @@ static void vhost_blk_drop_backend(struct vhost_blk *blk)
 	vhost_blk_flush(blk);
 
 	for (i = 0; i < VHOST_BLK_VQ_MAX; i++) {
+		flush_work(&blk->vqs[i].drop_work);
 		kvfree(blk->vqs[i].req);
 		blk->vqs[i].req = NULL;
 	}
@@ -756,8 +790,10 @@ static int vhost_blk_open(struct inode *inode, struct file *file)
 		       VHOST_DEV_WEIGHT, VHOST_DEV_PKT_WEIGHT, true, NULL);
 	file->private_data = blk;
 
-	for (i = 0; i < VHOST_BLK_VQ_MAX; i++)
+	for (i = 0; i < VHOST_BLK_VQ_MAX; i++) {
 		vhost_work_init(&blk->vqs[i].work, vhost_blk_handle_host_kick);
+		INIT_WORK(&blk->vqs[i].drop_work, vhost_blk_drop_reqs);
+	}
 
 	return ret;
 out_blk:
-- 
2.53.0



More information about the Devel mailing list