* [Devel] [PATCH hci-8.1] drivers/vhost/blk: drop completed requests when the vq has no worker
@ 2026-11-18 16:46 Alexander Ivanov
2026-11-18 16:53 ` no-reply
2026-11-19 7:24 ` Alexander Ivanov
0 siblings, 2 replies; 4+ messages in thread
From: Alexander Ivanov @ 2026-11-18 16:46 UTC (permalink / raw)
To: andrey.zhadchenko; +Cc: devel
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@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
_______________________________________________
Devel mailing list
Devel@openvz.org
https://lists.openvz.org/mailman/listinfo/devel
^ permalink raw reply [flat|nested] 4+ messages in thread* Re: [Devel] [PATCH hci-8.1] drivers/vhost/blk: drop completed requests when the vq has no worker
2026-11-18 16:46 [Devel] [PATCH hci-8.1] drivers/vhost/blk: drop completed requests when the vq has no worker Alexander Ivanov
@ 2026-11-18 16:53 ` no-reply
2026-11-19 7:24 ` Alexander Ivanov
1 sibling, 0 replies; 4+ messages in thread
From: no-reply @ 2026-11-18 16:53 UTC (permalink / raw)
To: Alexander Ivanov, andrey.zhadchenko; +Cc: devel
Thank you for your contribution! Sashiko AI review found 2 potential
issues to consider:
- [Low] vhost/blk: bug-fix commit lacks a Fixes: trailer
- [Low] vhost-blk: vhost_blk_drop_req() leaves req->inflight set on
dropped requests
--
commit ee94643eb92be4e55fe58e2e2a5d609d57dc2319
Author: Alexander Ivanov <alexander.ivanov@virtuozzo.com>
drivers/vhost/blk: drop completed requests when the vq has no worker
When vhost_blk_req_done() finds that the vq has no worker, it now frees the request from a work item on the system workqueue. The patch also holds an extra req_inflight[] reference while the callback uses blk, and makes flush_lock irq-safe.
> drivers/vhost/blk: drop completed requests when the vq has no worker
>
> 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.
> [ ... ]
>
> Signed-off-by: Alexander Ivanov <alexander.ivanov@virtuozzo.com>
[Severity: Low]
This looks like a bug fix for a long-standing ignored return value in
vhost_blk_req_done(). Should this include:
Fixes: c57a98d65796 ("drivers/vhost: vhost-blk accelerator for virtio-blk guests")
If 386033c1c98d ("drivers/vhost/blk: fix vhost_blk_req_done() softirq
handling") is judged the real introducer, it could point there instead.
> diff --git a/drivers/vhost/blk.c b/drivers/vhost/blk.c
> index b95358cdf3bc0..cb966e52acdd0 100644
> --- a/drivers/vhost/blk.c
> +++ b/drivers/vhost/blk.c
[ ... ]
> @@ -394,22 +404,45 @@ static inline void vhost_blk_bio_send(struct vhost_blk_req *req)
[ ... ]
> +static void vhost_blk_drop_req(struct vhost_blk_req *req)
> +{
> + vhost_blk_req_cleanup(req);
> + forget_request(req);
> +}
[Severity: Low]
Should vhost_blk_drop_req() clear req->inflight before forget_request(),
the way vhost_blk_handle_host_kick() does on normal completion?
A dropped request stays marked inflight in blk_vq->req[head]. If the
worker was killed, a new one is attached with VHOST_NEW_WORKER plus
VHOST_ATTACH_VRING_WORKER, and the guest then reuses that head,
vhost_blk_req_handle() fails with -EBUSY ("Guest reused in-flight head")
and the vq is stopped.
The commit message also does not say that dropped requests are never
completed to the guest.
Carry the trailer below into the next revision and Sashiko will not review this patch again.
Reviewed-by: Sashiko <no-reply@sashiko.virtuozzo.com>
--
Sashiko AI review · https://sashiko.vzint.dev/#/patchset/20261118164637.3464286-1-alexander.ivanov@virtuozzo.com?part=1
_______________________________________________
Devel mailing list
Devel@openvz.org
https://lists.openvz.org/mailman/listinfo/devel
^ permalink raw reply [flat|nested] 4+ messages in thread* Re: [Devel] [PATCH hci-8.1] drivers/vhost/blk: drop completed requests when the vq has no worker
2026-11-18 16:46 [Devel] [PATCH hci-8.1] drivers/vhost/blk: drop completed requests when the vq has no worker Alexander Ivanov
2026-11-18 16:53 ` no-reply
@ 2026-11-19 7:24 ` Alexander Ivanov
2026-11-19 11:36 ` Konstantin Khorenko
1 sibling, 1 reply; 4+ messages in thread
From: Alexander Ivanov @ 2026-11-19 7:24 UTC (permalink / raw)
To: devel
This bug was fixed in drivers/vhost/blk: fix a flush hang on requests
completed after worker kill.
_______________________________________________
Devel mailing list
Devel@openvz.org
https://lists.openvz.org/mailman/listinfo/devel
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2026-11-19 11:38 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-11-18 16:46 [Devel] [PATCH hci-8.1] drivers/vhost/blk: drop completed requests when the vq has no worker Alexander Ivanov
2026-11-18 16:53 ` no-reply
2026-11-19 7:24 ` Alexander Ivanov
2026-11-19 11:36 ` Konstantin Khorenko
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox