From: Vasileios Almpanis <vasileios.almpanis@virtuozzo.com>
Subject: Re: [Devel] [PATCH VZ10 5/5] drivers/vhost/blk: rework queue/backend setup
Date: Thu, 20 Aug 2026 14:11:04 +0000 [thread overview]
Message-ID: <178723506422.906560.7120237971816041698.b4-review@b4> (raw)
In-Reply-To: <20260818150920.13123-6-andrey.zhadchenko@virtuozzo.com>
> vhost_blk_setup() is pretty bad: silently refusing changed vq->num
> if requests are already allocated, fetching user input second time
> (double-fetch vulnerability).
> To handle this, tie request allocation to backend existence. After
> all, if there is no backend, there is no point in having requests.
> Also expand it to get rid of boilerplate drop_backend, flush,
> fput sequence in a few places.
>
> https://virtuozzo.atlassian.net/browse/VSTOR-138640
Fixes: 40a5928ec730 ("drivers/vhost: vhost-blk accelerator for virtio-blk guests")
> Signed-off-by: Andrey Zhadchenko <andrey.zhadchenko@virtuozzo.com>
>
> diff --git a/drivers/vhost/blk.c b/drivers/vhost/blk.c
> index 070e5ce30a16..17ca135867e6 100644
> --- a/drivers/vhost/blk.c
> +++ b/drivers/vhost/blk.c
> @@ -647,11 +647,14 @@ static void vhost_blk_flush(struct vhost_blk *blk)
> spin_unlock(&blk->flush_lock);
> }
>
> -static inline void vhost_blk_drop_backends(struct vhost_blk *blk)
> +static void vhost_blk_drop_backend(struct vhost_blk *blk)
> {
> struct vhost_virtqueue *vq;
> int i;
>
> + if (!blk->backend)
> + return;
> +
> for (i = 0; i < VHOST_BLK_VQ_MAX; i++) {
> vq = &blk->vqs[i].vq;
>
> @@ -659,6 +662,45 @@ static inline void vhost_blk_drop_backends(struct vhost_blk *blk)
> vhost_vq_set_backend(vq, NULL);
> mutex_unlock(&vq->mutex);
> }
> +
> + vhost_blk_flush(blk);
> +
> + for (i = 0; i < VHOST_BLK_VQ_MAX; i++) {
> + kvfree(blk->vqs[i].req);
> + blk->vqs[i].req = NULL;
> + }
> +
> + fput(blk->backend);
> + blk->backend = NULL;
> +}
> +
> +static int vhost_blk_setup_vqs(struct vhost_blk *blk)
> +{
> + struct vhost_virtqueue *vq;
> + int i;
> +
> + for (i = 0; i < VHOST_BLK_VQ_MAX; i++) {
> + vq = &blk->vqs[i].vq;
> +
> + if (!vhost_vq_is_setup(vq))
> + continue;
> +
> + blk->vqs[i].req = kvmalloc_array(vq->num, sizeof(struct vhost_blk_req),
> + GFP_KERNEL);
> + if (!blk->vqs[i].req)
> + return -ENOMEM;
> +
> + mutex_lock(&vq->mutex);
> + vhost_vq_set_backend(vq, blk->backend);
> + if (vhost_vq_init_access(vq)) {
> + mutex_unlock(&vq->mutex);
> + return -EFAULT;
> + }
> + mutex_unlock(&vq->mutex);
NIT: extra blank line
> +
> + }
> +
> + return 0;
> }
>
> static int vhost_blk_open(struct inode *inode, struct file *file)
> @@ -711,16 +753,10 @@ static int vhost_blk_open(struct inode *inode, struct file *file)
> static int vhost_blk_release(struct inode *inode, struct file *f)
> {
> struct vhost_blk *blk = f->private_data;
> - int i;
>
> - vhost_blk_drop_backends(blk);
> - vhost_blk_flush(blk);
> + vhost_blk_drop_backend(blk);
> vhost_dev_stop(&blk->dev);
> - if (blk->backend)
> - fput(blk->backend);
> vhost_dev_cleanup(&blk->dev);
> - for (i = 0; i < VHOST_BLK_VQ_MAX; i++)
> - kvfree(blk->vqs[i].req);
> kfree(blk->dev.vqs);
> kvfree(blk);
>
> @@ -754,32 +790,19 @@ static int vhost_blk_set_features(struct vhost_blk *blk, u64 features)
>
> static long vhost_blk_set_backend(struct vhost_blk *blk, int fd)
> {
> - struct vhost_virtqueue *vq;
> struct file *file;
> struct inode *inode;
> - int ret, i;
> + int ret;
>
> mutex_lock(&blk->dev.mutex);
> ret = vhost_dev_check_owner(&blk->dev);
> if (ret)
> goto out_dev;
>
> - /*
> - * fd < 0 means "stop the device". Detach the backend from every vq so
> - * vhost_blk_handle_guest_kick() stops fetching descriptors, drain the
> - * in-flight requests, and release the backing file.
> - */
> + /* fd < 0 means "stop the device" */
> if (fd < 0) {
> - if (!blk->backend) {
> - ret = 0; /* already stopped */
> - goto out_dev;
> - }
> - vhost_blk_drop_backends(blk);
> - vhost_blk_flush(blk);
> - fput(blk->backend);
> - blk->backend = NULL;
> ret = 0;
> - goto out_dev;
> + goto out_drop;
> }
>
> if (blk->backend) {
> @@ -796,31 +819,21 @@ static long vhost_blk_set_backend(struct vhost_blk *blk, int fd)
> inode = file->f_mapping->host;
> if (!S_ISBLK(inode->i_mode)) {
> ret = -EFAULT;
> - goto out_file;
> + fput(file);
> + goto out_dev;
> }
>
> - for (i = 0; i < VHOST_BLK_VQ_MAX; i++) {
> - vq = &blk->vqs[i].vq;
> - if (!vhost_vq_access_ok(vq)) {
> - ret = -EFAULT;
> - goto out_drop;
> - }
> -
> - mutex_lock(&vq->mutex);
> - vhost_vq_set_backend(vq, file);
> - ret = vhost_vq_init_access(vq);
> - mutex_unlock(&vq->mutex);
> - }
NIT: extra blank line
--
Vasileios Almpanis <vasileios.almpanis@virtuozzo.com>
prev parent reply other threads:[~2026-08-20 14:11 UTC|newest]
Thread overview: 10+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-08-18 15:09 [Devel] [PATCH VZ10 0/5] vhost-blk: fix protocol handling and backend setup Andrey Zhadchenko
2026-08-18 15:09 ` [Devel] [PATCH VZ10 1/5] drivers/vhost/blk: harden get_id command Andrey Zhadchenko
2026-08-18 15:09 ` [Devel] [PATCH VZ10 2/5] drivers/vhost/blk: report correct used-ring lengths Andrey Zhadchenko
2026-08-20 14:11 ` Vasileios Almpanis
2026-08-24 15:04 ` Andrey Zhadchenko
2026-08-18 15:09 ` [Devel] [PATCH VZ10 3/5] drivers/vhost/blk: fix flush support Andrey Zhadchenko
2026-08-18 15:09 ` [Devel] [PATCH VZ10 4/5] drivers/vhost/blk: fix sector alignment calculation Andrey Zhadchenko
2026-08-18 22:57 ` Andrey Zhadchenko
2026-08-18 15:09 ` [Devel] [PATCH VZ10 5/5] drivers/vhost/blk: rework queue/backend setup Andrey Zhadchenko
2026-08-20 14:11 ` Vasileios Almpanis [this message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=178723506422.906560.7120237971816041698.b4-review@b4 \
--to=vasileios.almpanis@virtuozzo.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox