[Devel] [PATCH hci-8.1 v2 9/9] drivers/vhost/blk: fix a hang on release after the vhost worker is killed
Alexander Ivanov
alexander.ivanov at virtuozzo.com
Tue Nov 17 13:10:26 MSK 2026
When the owner is killed, vhost_worker_killed() detaches the worker
from the vqs. A bio completing after that fails to queue the
completion work, so its request stays on llhead and is never removed
from req_inflight. vhost_blk_flush() on release then waits forever
and the process is stuck in D state. Any kill of a VM with I/O in
flight can hit this.
If the work cannot be queued, drop the completed requests right in
the bio end_io: forget them without writing a status, as there is no
guest left to see it. llist_del_all() makes sure a request is dropped
once even if it races with the last run of the host kick handler.
forget_request() can now run in irq context, so take flush_lock with
interrupts disabled. The end_io may run in atomic context and the
request buffers may be vmalloc'ed, so free them on release instead:
a dropped request is the one left with ->inflight set.
Once a request is on llhead, a concurrent consumer may forget it and
let release free the request array and blk while the end_io is still
using blk_vq. Keep the end_io tail in an RCU read-side section and
wait for a grace period before freeing the requests. The same window
existed before with the host kick handler as the consumer.
https://virtuozzo.atlassian.net/browse/VSTOR-152175
Fixes: c57a98d65796 ("drivers/vhost: vhost-blk accelerator for virtio-blk guests")
Feature: vhost-blk: in-kernel accelerator for virtio-blk guests
Signed-off-by: Alexander Ivanov <alexander.ivanov at virtuozzo.com>
---
drivers/vhost/blk.c | 52 +++++++++++++++++++++++++++++++++------------
1 file changed, 39 insertions(+), 13 deletions(-)
diff --git a/drivers/vhost/blk.c b/drivers/vhost/blk.c
index b95358cdf3bc..b67255580031 100644
--- a/drivers/vhost/blk.c
+++ b/drivers/vhost/blk.c
@@ -146,6 +146,8 @@ static inline int vhost_blk_set_status(struct vhost_blk_req *req, u8 status)
return 0;
}
+static void vhost_blk_drop_completed(struct vhost_blk_vq *blk_vq);
+
static void vhost_blk_req_done(struct bio *bio)
{
struct vhost_blk_req *req = bio->bi_private;
@@ -168,8 +170,14 @@ 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;
+
+ /* req and blk may be freed once req is on llhead */
+ rcu_read_lock();
+ llist_add(&req->llnode, &blk_vq->llhead);
+ if (!vhost_vq_work_queue(&blk_vq->vq, &blk_vq->work))
+ vhost_blk_drop_completed(blk_vq);
+ rcu_read_unlock();
}
}
@@ -394,22 +402,34 @@ 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)
{
struct vhost_blk *blk = req->blk;
+ unsigned long flags;
int zero;
- spin_lock(&blk->flush_lock);
+ spin_lock_irqsave(&blk->flush_lock, flags);
zero = atomic_dec_and_test(&blk->req_inflight[req->req_bin]);
if (zero && blk->during_flush)
wake_up(&blk->flush_wait);
- spin_unlock(&blk->flush_lock);
+ spin_unlock_irqrestore(&blk->flush_lock, flags);
+}
+
+/* Called when the worker is killed: nobody will complete these to the guest */
+static void vhost_blk_drop_completed(struct vhost_blk_vq *blk_vq)
+{
+ struct vhost_blk_req *req, *tmp;
+ struct llist_node *llnode;
+
+ llnode = llist_del_all(&blk_vq->llhead);
+ llist_for_each_entry_safe(req, tmp, llnode, llnode)
+ forget_request(req);
}
static int vhost_blk_req_submit(struct vhost_blk_req *req)
@@ -688,12 +708,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);
/*
@@ -707,15 +727,15 @@ 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)
{
struct vhost_virtqueue *vq;
- int i;
+ int i, j;
if (!blk->backend)
return;
@@ -729,10 +749,16 @@ static void vhost_blk_drop_backend(struct vhost_blk *blk)
}
vhost_blk_flush(blk);
+ synchronize_rcu();
for (i = 0; i < VHOST_BLK_VQ_MAX; i++) {
- kvfree(blk->vqs[i].req);
- blk->vqs[i].req = NULL;
+ struct vhost_blk_vq *blk_vq = &blk->vqs[i];
+
+ for (j = 0; blk_vq->req && j < blk_vq->vq.num; j++)
+ if (blk_vq->req[j].inflight)
+ vhost_blk_req_cleanup(&blk_vq->req[j]);
+ kvfree(blk_vq->req);
+ blk_vq->req = NULL;
}
fput(blk->backend);
--
2.53.0
More information about the Devel
mailing list