All Virtuozzo development lists (kernel + QEMU)
 help / color / mirror / Atom feed
* [PATCH hci-8.2 v8 0/7] vhost-blk: switch from VHOST_SET_NWORKERS to the mainstream worker ioctls
@ 2026-11-20 13:47 Alexander Ivanov
  2026-11-20 13:47 ` [PATCH hci-8.2 v8 1/7] vhost-backend: add vhost_set_fork_from_owner op Alexander Ivanov
                   ` (6 more replies)
  0 siblings, 7 replies; 9+ messages in thread
From: Alexander Ivanov @ 2026-11-20 13:47 UTC (permalink / raw)
  To: andrey.zhadchenko; +Cc: andrey.drobyshev, svt-core

The kernel drops our VHOST_SET_NWORKERS ioctl in favour of the
mainstream VHOST_NEW_WORKER/VHOST_ATTACH_VRING_WORKER, which vhost-blk
now supports.

Patch 1 wraps VHOST_SET_FORK_FROM_OWNER in a vhost backend op. Patch 2
refuses num-queues above VHOST_BLK_MAX_QUEUES at realize. Patch 3 is a
cleanup taking &s->dev into a local pointer. Patches 4 and 5 fix the
error path of vhost_blk_start(): it returned 0 on some failures and
left QEMU the owner of the vhost device. Patch 6 makes vhost-blk
create its num-threads workers with the mainstream ioctls, keeping the
same worker layout, and fail to start if the kernel cannot provide
them. Patch 7 drops VHOST_SET_NWORKERS from linux-headers.

v8:
- patch 6: warn when VHOST_SET_FORK_FROM_OWNER fails with a single
  thread instead of ignoring the failure silently

v7:
- rebase on the current hci-8.2
- patch 6: ignore a VHOST_SET_FORK_FROM_OWNER failure only for a single
  thread instead of keying on -ENOTTY: a kernel without the ioctl fails
  it with -EPERM
- patch 6: note in the commit message that num-threads of 0 and above
  num-queues behave as with VHOST_SET_NWORKERS
- add Reviewed-by tags

v6:
- rebase on the current hci-8.1
- new patch 4: keep the original error in vhost_blk_start() instead of
  returning the result of unbinding the guest notifiers
- new patch 5: undo VHOST_SET_OWNER when vhost_blk_start() fails
- patch 6: ignore -ENOTTY from VHOST_SET_FORK_FROM_OWNER, so kernels
  lacking it still start vhost-blk with a single thread
- patch 6: reset the owner when the worker ioctls fail
- patch 6: drop the claim that released vz10 kernels have
  VHOST_SET_FORK_FROM_OWNER from the commit message

v5:
- patch 4: fail to start vhost-blk on any worker ioctl failure instead
  of warning and running on fewer threads than requested
- patch 4: switch to vhost_task mode for num-threads=1 as well, so the
  default thread is of the same kind whatever the number of workers

v4:
- new patch 3: split the &s->dev -> hdev conversion out of patch 4
- patch 2: quote the error realize fails with, add Fixes:
- patch 4: do not key on the errno; on any worker ioctl failure warn
  and continue with the threads created so far instead of failing
  to start vhost-blk
- patch 4: drop the comments, explain in the commit message instead

v3:
- new patch 1: call VHOST_SET_FORK_FROM_OWNER, VHOST_NEW_WORKER and
  VHOST_ATTACH_VRING_WORKER through vhost_ops instead of raw ioctls
- new patch 2: refuse num-queues above VHOST_BLK_MAX_QUEUES at realize
  instead of clamping num-threads in patch 3
- patch 3: create each worker right before attaching its first vq

v2:
- patch 1: clamp num-threads to VHOST_BLK_MAX_QUEUES
- patch 1: note in the commit message that our kernel, unlike
  mainline, defaults to kthreads

https://virtuozzo.atlassian.net/browse/VSTOR-107537

Alexander Ivanov (7):
  vhost-backend: add vhost_set_fork_from_owner op
  vhost-blk: reject num-queues above VHOST_BLK_MAX_QUEUES
  vhost-blk: use a local vhost_dev pointer in start and stop
  vhost-blk: fix vhost_blk_start() returning 0 on failure
  vhost-blk: reset the owner when vhost_blk_start() fails
  vhost-blk: create kernel threads with the mainstream worker ioctls
  linux-headers: drop VHOST_SET_NWORKERS

 hw/block/vhost-blk.c              | 110 ++++++++++++++++++++++++------
 hw/virtio/vhost-backend.c         |   7 ++
 include/hw/virtio/vhost-backend.h |   3 +
 linux-headers/linux/vhost.h       |   9 ---
 4 files changed, 98 insertions(+), 31 deletions(-)

-- 
2.53.0


^ permalink raw reply	[flat|nested] 9+ messages in thread

* [PATCH hci-8.2 v8 1/7] vhost-backend: add vhost_set_fork_from_owner op
  2026-11-20 13:47 [PATCH hci-8.2 v8 0/7] vhost-blk: switch from VHOST_SET_NWORKERS to the mainstream worker ioctls Alexander Ivanov
@ 2026-11-20 13:47 ` Alexander Ivanov
  2026-11-20 13:47 ` [PATCH hci-8.2 v8 2/7] vhost-blk: reject num-queues above VHOST_BLK_MAX_QUEUES Alexander Ivanov
                   ` (5 subsequent siblings)
  6 siblings, 0 replies; 9+ messages in thread
From: Alexander Ivanov @ 2026-11-20 13:47 UTC (permalink / raw)
  To: andrey.zhadchenko; +Cc: andrey.drobyshev, svt-core, Sashiko

Wrap VHOST_SET_FORK_FROM_OWNER for the kernel backend next to the
worker ops, so devices switch the vhost worker mode the same way they
create and attach workers.

https://virtuozzo.atlassian.net/browse/VSTOR-107537

Signed-off-by: Alexander Ivanov <alexander.ivanov@virtuozzo.com>
Reviewed-by: Andrey Drobyshev <andrey.drobyshev@virtuozzo.com>
Reviewed-by: Andrey Zhadchenko <andrey.zhadchenko@virtuozzo.com>
Reviewed-by: Sashiko <no-reply@sashiko.virtuozzo.com>
---
 hw/virtio/vhost-backend.c         | 7 +++++++
 include/hw/virtio/vhost-backend.h | 3 +++
 2 files changed, 10 insertions(+)

diff --git a/hw/virtio/vhost-backend.c b/hw/virtio/vhost-backend.c
index 1447d129630..7f8c731a607 100644
--- a/hw/virtio/vhost-backend.c
+++ b/hw/virtio/vhost-backend.c
@@ -187,6 +187,12 @@ static int vhost_kernel_get_vring_worker(struct vhost_dev *dev,
     return vhost_kernel_call(dev, VHOST_GET_VRING_WORKER, worker);
 }
 
+static int vhost_kernel_set_fork_from_owner(struct vhost_dev *dev,
+                                            uint8_t fork_owner)
+{
+    return vhost_kernel_call(dev, VHOST_SET_FORK_FROM_OWNER, &fork_owner);
+}
+
 static int vhost_kernel_set_backend_cap(struct vhost_dev *dev)
 {
     uint64_t features;
@@ -386,6 +392,7 @@ const VhostOps kernel_ops = {
         .vhost_attach_vring_worker = vhost_kernel_attach_vring_worker,
         .vhost_new_worker = vhost_kernel_new_worker,
         .vhost_free_worker = vhost_kernel_free_worker,
+        .vhost_set_fork_from_owner = vhost_kernel_set_fork_from_owner,
         .vhost_set_features_ex = vhost_kernel_set_features,
         .vhost_get_features_ex = vhost_kernel_get_features,
         .vhost_set_backend_cap = vhost_kernel_set_backend_cap,
diff --git a/include/hw/virtio/vhost-backend.h b/include/hw/virtio/vhost-backend.h
index 18ce5ea9a07..78cd66378ef 100644
--- a/include/hw/virtio/vhost-backend.h
+++ b/include/hw/virtio/vhost-backend.h
@@ -95,6 +95,8 @@ typedef int (*vhost_new_worker_op)(struct vhost_dev *dev,
                                    struct vhost_worker_state *worker);
 typedef int (*vhost_free_worker_op)(struct vhost_dev *dev,
                                     struct vhost_worker_state *worker);
+typedef int (*vhost_set_fork_from_owner_op)(struct vhost_dev *dev,
+                                            uint8_t fork_owner);
 typedef int (*vhost_set_features_ex_op)(struct vhost_dev *dev,
                                         const uint64_t *features);
 typedef int (*vhost_get_features_ex_op)(struct vhost_dev *dev,
@@ -190,6 +192,7 @@ typedef struct VhostOps {
     vhost_free_worker_op vhost_free_worker;
     vhost_get_vring_worker_op vhost_get_vring_worker;
     vhost_attach_vring_worker_op vhost_attach_vring_worker;
+    vhost_set_fork_from_owner_op vhost_set_fork_from_owner;
     vhost_set_features_ex_op vhost_set_features_ex;
     vhost_get_features_ex_op vhost_get_features_ex;
     vhost_set_features_op vhost_set_features;
-- 
2.53.0


^ permalink raw reply	[flat|nested] 9+ messages in thread

* [PATCH hci-8.2 v8 2/7] vhost-blk: reject num-queues above VHOST_BLK_MAX_QUEUES
  2026-11-20 13:47 [PATCH hci-8.2 v8 0/7] vhost-blk: switch from VHOST_SET_NWORKERS to the mainstream worker ioctls Alexander Ivanov
  2026-11-20 13:47 ` [PATCH hci-8.2 v8 1/7] vhost-backend: add vhost_set_fork_from_owner op Alexander Ivanov
@ 2026-11-20 13:47 ` Alexander Ivanov
  2026-11-20 13:47 ` [PATCH hci-8.2 v8 3/7] vhost-blk: use a local vhost_dev pointer in start and stop Alexander Ivanov
                   ` (4 subsequent siblings)
  6 siblings, 0 replies; 9+ messages in thread
From: Alexander Ivanov @ 2026-11-20 13:47 UTC (permalink / raw)
  To: andrey.zhadchenko; +Cc: andrey.drobyshev, svt-core, Sashiko

Only the automatic num-queues value is capped at VHOST_BLK_MAX_QUEUES,
an explicit one is taken as is. The kernel vhost-blk device has that
many virtqueues and rejects any vq beyond them, so realize fails with
an error that does not point at num-queues:

  qemu-system-x86_64: vhost_set_vring_call failed: No buffer space available (105)
  qemu-system-x86_64: vhost-blk: vhost initialization failed: No buffer space available

Check num-queues against the limit up front instead.

https://virtuozzo.atlassian.net/browse/VSTOR-107537

Fixes: 0ff3cb5ff28f ("block: add vhost-blk backend")
Signed-off-by: Alexander Ivanov <alexander.ivanov@virtuozzo.com>
Reviewed-by: Andrey Drobyshev <andrey.drobyshev@virtuozzo.com>
Reviewed-by: Andrey Zhadchenko <andrey.zhadchenko@virtuozzo.com>
Reviewed-by: Sashiko <no-reply@sashiko.virtuozzo.com>
---
 hw/block/vhost-blk.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)

diff --git a/hw/block/vhost-blk.c b/hw/block/vhost-blk.c
index d8bbfcaff86..6c99de89120 100644
--- a/hw/block/vhost-blk.c
+++ b/hw/block/vhost-blk.c
@@ -599,8 +599,9 @@ static void vhost_blk_device_realize(DeviceState *dev, Error **errp)
                                VHOST_BLK_MAX_QUEUES);
     }
 
-    if (!conf->num_queues) {
-        error_setg(errp, "vhost-blk: num-queues property must be larger than 0");
+    if (!conf->num_queues || conf->num_queues > VHOST_BLK_MAX_QUEUES) {
+        error_setg(errp, "vhost-blk: invalid num-queues property (%" PRIu16 "), "
+                   "must be 1-%d", conf->num_queues, VHOST_BLK_MAX_QUEUES);
         return;
     }
 
-- 
2.53.0


^ permalink raw reply	[flat|nested] 9+ messages in thread

* [PATCH hci-8.2 v8 3/7] vhost-blk: use a local vhost_dev pointer in start and stop
  2026-11-20 13:47 [PATCH hci-8.2 v8 0/7] vhost-blk: switch from VHOST_SET_NWORKERS to the mainstream worker ioctls Alexander Ivanov
  2026-11-20 13:47 ` [PATCH hci-8.2 v8 1/7] vhost-backend: add vhost_set_fork_from_owner op Alexander Ivanov
  2026-11-20 13:47 ` [PATCH hci-8.2 v8 2/7] vhost-blk: reject num-queues above VHOST_BLK_MAX_QUEUES Alexander Ivanov
@ 2026-11-20 13:47 ` Alexander Ivanov
  2026-11-20 13:47 ` [PATCH hci-8.2 v8 4/7] vhost-blk: fix vhost_blk_start() returning 0 on failure Alexander Ivanov
                   ` (3 subsequent siblings)
  6 siblings, 0 replies; 9+ messages in thread
From: Alexander Ivanov @ 2026-11-20 13:47 UTC (permalink / raw)
  To: andrey.zhadchenko; +Cc: andrey.drobyshev, svt-core, Sashiko

Take &s->dev once into hdev in vhost_blk_start() and vhost_blk_stop()
instead of spelling it out at every use. No functional change.

https://virtuozzo.atlassian.net/browse/VSTOR-107537

Fixes: 0ff3cb5ff28f ("block: add vhost-blk backend")
Signed-off-by: Alexander Ivanov <alexander.ivanov@virtuozzo.com>
Reviewed-by: Andrey Zhadchenko <andrey.zhadchenko@virtuozzo.com>
Reviewed-by: Sashiko <no-reply@sashiko.virtuozzo.com>
Reviewed-by: Andrey Drobyshev <andrey.drobyshev@virtuozzo.com>
---
 hw/block/vhost-blk.c | 28 +++++++++++++++-------------
 1 file changed, 15 insertions(+), 13 deletions(-)

diff --git a/hw/block/vhost-blk.c b/hw/block/vhost-blk.c
index 6c99de89120..bbd24fa94b1 100644
--- a/hw/block/vhost-blk.c
+++ b/hw/block/vhost-blk.c
@@ -36,6 +36,7 @@
 static int vhost_blk_start(VirtIODevice *vdev)
 {
     VHostBlk *s = VHOST_BLK(vdev);
+    struct vhost_dev *hdev = &s->dev;
     struct vhost_vring_file backend;
     int ret, i, nworkers;
     BusState *qbus = BUS(qdev_get_parent_bus(DEVICE(vdev)));
@@ -71,22 +72,22 @@ static int vhost_blk_start(VirtIODevice *vdev)
         }
     }
 
-    ret = k->set_guest_notifiers(qbus->parent, s->dev.nvqs, true);
+    ret = k->set_guest_notifiers(qbus->parent, hdev->nvqs, true);
     if (ret < 0) {
         error_report("vhost-blk: unable to bind guest notifiers");
         return ret;
     }
 
-    ret = vhost_dev_enable_notifiers(&s->dev, vdev);
+    ret = vhost_dev_enable_notifiers(hdev, vdev);
     if (ret < 0) {
         error_report("vhost-blk: unable to enable dev notifiers: %s",
                      strerror(errno));
         goto err_set;
     }
 
-    s->dev.acked_features = vdev->guest_features & s->dev.features;
+    hdev->acked_features = vdev->guest_features & hdev->features;
 
-    ret = vhost_dev_start(&s->dev, vdev, false);
+    ret = vhost_dev_start(hdev, vdev, false);
     if (ret < 0) {
         error_report("vhost-blk: unable to start vhost dev");
         goto err_disable;
@@ -101,11 +102,11 @@ static int vhost_blk_start(VirtIODevice *vdev)
         goto err_stop;
     }
 
-    for (i = 0; i < s->dev.nvqs; i++) {
-        vhost_virtqueue_mask(&s->dev, vdev, i, false);
+    for (i = 0; i < hdev->nvqs; i++) {
+        vhost_virtqueue_mask(hdev, vdev, i, false);
     }
 
-    for (i = 0; i < s->dev.nvqs; i++) {
+    for (i = 0; i < hdev->nvqs; i++) {
         event_notifier_set(virtio_queue_get_host_notifier(virtio_get_queue(vdev, i)));
     }
 
@@ -114,11 +115,11 @@ static int vhost_blk_start(VirtIODevice *vdev)
     return 0;
 
 err_stop:
-    vhost_dev_stop(&s->dev, vdev, false);
+    vhost_dev_stop(hdev, vdev, false);
 err_disable:
-    vhost_dev_disable_notifiers(&s->dev, vdev);
+    vhost_dev_disable_notifiers(hdev, vdev);
 err_set:
-    ret = k->set_guest_notifiers(qbus->parent, s->dev.nvqs, false);
+    ret = k->set_guest_notifiers(qbus->parent, hdev->nvqs, false);
     if (ret < 0) {
         error_report("vhost-blk: unable to unbind guest notifiers");
     }
@@ -132,6 +133,7 @@ static void vhost_blk_stop(VirtIODevice *vdev)
     BusState *qbus = BUS(qdev_get_parent_bus(DEVICE(vdev)));
     VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(qbus);
     VHostBlk *s = VHOST_BLK(vdev);
+    struct vhost_dev *hdev = &s->dev;
     struct vhost_vring_file backend;
     int ret;
 
@@ -151,13 +153,13 @@ static void vhost_blk_stop(VirtIODevice *vdev)
     ret = ioctl(s->vhostfd, VHOST_BLK_SET_BACKEND, &backend);
     assert(ret == 0);
 
-    vhost_dev_stop(&s->dev, vdev, false);
+    vhost_dev_stop(hdev, vdev, false);
 
-    ret = k->set_guest_notifiers(qbus->parent, s->dev.nvqs, false);
+    ret = k->set_guest_notifiers(qbus->parent, hdev->nvqs, false);
     if (ret < 0) {
         error_report("vhost-blk: unable to unbind guest notifiers");
     }
-    vhost_dev_disable_notifiers(&s->dev, vdev);
+    vhost_dev_disable_notifiers(hdev, vdev);
 
     s->vhost_started = false;
 }
-- 
2.53.0


^ permalink raw reply	[flat|nested] 9+ messages in thread

* [PATCH hci-8.2 v8 4/7] vhost-blk: fix vhost_blk_start() returning 0 on failure
  2026-11-20 13:47 [PATCH hci-8.2 v8 0/7] vhost-blk: switch from VHOST_SET_NWORKERS to the mainstream worker ioctls Alexander Ivanov
                   ` (2 preceding siblings ...)
  2026-11-20 13:47 ` [PATCH hci-8.2 v8 3/7] vhost-blk: use a local vhost_dev pointer in start and stop Alexander Ivanov
@ 2026-11-20 13:47 ` Alexander Ivanov
  2026-11-20 13:47 ` [PATCH hci-8.2 v8 5/7] vhost-blk: reset the owner when vhost_blk_start() fails Alexander Ivanov
                   ` (2 subsequent siblings)
  6 siblings, 0 replies; 9+ messages in thread
From: Alexander Ivanov @ 2026-11-20 13:47 UTC (permalink / raw)
  To: andrey.zhadchenko; +Cc: andrey.drobyshev, svt-core, Sashiko

The error path stores the result of unbinding the guest notifiers in
ret, so a failure after binding them is reported as success once the
unbind succeeds: vhost_blk_set_status() logs nothing and the device
stays dead. Keep the original error in ret.

https://virtuozzo.atlassian.net/browse/VSTOR-107537

Fixes: 0ff3cb5ff28f ("block: add vhost-blk backend")
Signed-off-by: Alexander Ivanov <alexander.ivanov@virtuozzo.com>
Reviewed-by: Andrey Zhadchenko <andrey.zhadchenko@virtuozzo.com>
Reviewed-by: Sashiko <no-reply@sashiko.virtuozzo.com>
---
 hw/block/vhost-blk.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)

diff --git a/hw/block/vhost-blk.c b/hw/block/vhost-blk.c
index bbd24fa94b1..fab79da5a38 100644
--- a/hw/block/vhost-blk.c
+++ b/hw/block/vhost-blk.c
@@ -119,8 +119,7 @@ err_stop:
 err_disable:
     vhost_dev_disable_notifiers(hdev, vdev);
 err_set:
-    ret = k->set_guest_notifiers(qbus->parent, hdev->nvqs, false);
-    if (ret < 0) {
+    if (k->set_guest_notifiers(qbus->parent, hdev->nvqs, false) < 0) {
         error_report("vhost-blk: unable to unbind guest notifiers");
     }
 
-- 
2.53.0


^ permalink raw reply	[flat|nested] 9+ messages in thread

* [PATCH hci-8.2 v8 5/7] vhost-blk: reset the owner when vhost_blk_start() fails
  2026-11-20 13:47 [PATCH hci-8.2 v8 0/7] vhost-blk: switch from VHOST_SET_NWORKERS to the mainstream worker ioctls Alexander Ivanov
                   ` (3 preceding siblings ...)
  2026-11-20 13:47 ` [PATCH hci-8.2 v8 4/7] vhost-blk: fix vhost_blk_start() returning 0 on failure Alexander Ivanov
@ 2026-11-20 13:47 ` Alexander Ivanov
  2026-11-20 13:47 ` [PATCH hci-8.2 v8 6/7] vhost-blk: create kernel threads with the mainstream worker ioctls Alexander Ivanov
  2026-11-20 13:47 ` [PATCH hci-8.2 v8 7/7] linux-headers: drop VHOST_SET_NWORKERS Alexander Ivanov
  6 siblings, 0 replies; 9+ messages in thread
From: Alexander Ivanov @ 2026-11-20 13:47 UTC (permalink / raw)
  To: andrey.zhadchenko; +Cc: andrey.drobyshev, svt-core, Sashiko

Every failure after VHOST_SET_OWNER leaves QEMU the owner of the vhost
device with its worker threads running. The next start attempt then
fails on VHOST_SET_OWNER with -EBUSY until the guest resets the device.
Undo VHOST_SET_OWNER on these paths, as vhost_blk_reset() does.

https://virtuozzo.atlassian.net/browse/VSTOR-107537

Fixes: 0ff3cb5ff28f ("block: add vhost-blk backend")
Signed-off-by: Alexander Ivanov <alexander.ivanov@virtuozzo.com>
Reviewed-by: Andrey Zhadchenko <andrey.zhadchenko@virtuozzo.com>
Reviewed-by: Sashiko <no-reply@sashiko.virtuozzo.com>
Reviewed-by: Andrey Drobyshev <andrey.drobyshev@virtuozzo.com>
---
 hw/block/vhost-blk.c | 12 +++++++++---
 1 file changed, 9 insertions(+), 3 deletions(-)

diff --git a/hw/block/vhost-blk.c b/hw/block/vhost-blk.c
index fab79da5a38..0be51a10dbc 100644
--- a/hw/block/vhost-blk.c
+++ b/hw/block/vhost-blk.c
@@ -60,7 +60,8 @@ static int vhost_blk_start(VirtIODevice *vdev)
     nworkers = s->conf.num_threads;
     if (nworkers != 1 && ioctl(s->vhostfd, VHOST_SET_NWORKERS, &nworkers)) {
         error_report("vhost-blk: unable to set number of kernel threads");
-        return -EINVAL;
+        ret = -EINVAL;
+        goto err_reset_owner;
     }
 
     if (s->conf.serial) {
@@ -68,14 +69,15 @@ static int vhost_blk_start(VirtIODevice *vdev)
         if (ioctl(s->vhostfd, VHOST_BLK_SET_SERIAL, serial)) {
             error_report("vhost-blk: unable to set drive serial %s",
                          s->conf.serial);
-            return -EINVAL;
+            ret = -EINVAL;
+            goto err_reset_owner;
         }
     }
 
     ret = k->set_guest_notifiers(qbus->parent, hdev->nvqs, true);
     if (ret < 0) {
         error_report("vhost-blk: unable to bind guest notifiers");
-        return ret;
+        goto err_reset_owner;
     }
 
     ret = vhost_dev_enable_notifiers(hdev, vdev);
@@ -122,6 +124,10 @@ err_set:
     if (k->set_guest_notifiers(qbus->parent, hdev->nvqs, false) < 0) {
         error_report("vhost-blk: unable to unbind guest notifiers");
     }
+err_reset_owner:
+    if (ioctl(s->vhostfd, VHOST_RESET_OWNER, NULL)) {
+        error_report("vhost-blk: unable to reset owner: %s", strerror(errno));
+    }
 
     return ret;
 
-- 
2.53.0


^ permalink raw reply	[flat|nested] 9+ messages in thread

* [PATCH hci-8.2 v8 6/7] vhost-blk: create kernel threads with the mainstream worker ioctls
  2026-11-20 13:47 [PATCH hci-8.2 v8 0/7] vhost-blk: switch from VHOST_SET_NWORKERS to the mainstream worker ioctls Alexander Ivanov
                   ` (4 preceding siblings ...)
  2026-11-20 13:47 ` [PATCH hci-8.2 v8 5/7] vhost-blk: reset the owner when vhost_blk_start() fails Alexander Ivanov
@ 2026-11-20 13:47 ` Alexander Ivanov
  2026-11-20 13:51   ` Andrey Drobyshev
  2026-11-20 13:47 ` [PATCH hci-8.2 v8 7/7] linux-headers: drop VHOST_SET_NWORKERS Alexander Ivanov
  6 siblings, 1 reply; 9+ messages in thread
From: Alexander Ivanov @ 2026-11-20 13:47 UTC (permalink / raw)
  To: andrey.zhadchenko; +Cc: andrey.drobyshev, svt-core, Sashiko

num-threads was passed to our own VHOST_SET_NWORKERS kernel ioctl. The
kernel drops it with the revert of "drivers/vhost: add ioctl to
increase the number of workers" in favour of the mainstream worker
ioctls, which vhost-blk now supports. Create num-threads - 1 workers
with VHOST_NEW_WORKER and attach them to the virtqueues round-robin
with VHOST_ATTACH_VRING_WORKER, the layout VHOST_SET_NWORKERS produced.
num-threads of 0 still means one thread and a value above num-queues
is clamped to it, as VHOST_SET_NWORKERS did.

Our kernel, unlike mainline, defaults to kthread mode, where
VHOST_NEW_WORKER fails with -EFAULT rather than -ENOTTY. So switch the
device to vhost_task mode with VHOST_SET_FORK_FROM_OWNER before
VHOST_SET_OWNER. Do it for num-threads=1 as well, so the default
thread is of the same kind whatever the number of workers, but only
warn when it fails there: a single thread works in either mode. A kernel
without the ioctl fails it with -EPERM from the owner check rather
than -ENOTTY, so the error code cannot tell it apart.

Fail to start vhost-blk if any of the worker ioctls fails rather than
silently run on fewer threads than requested. num-threads above 1 now
requires a kernel whose vhost-blk handles them.

https://virtuozzo.atlassian.net/browse/VSTOR-107537

Signed-off-by: Alexander Ivanov <alexander.ivanov@virtuozzo.com>
Reviewed-by: Andrey Zhadchenko <andrey.zhadchenko@virtuozzo.com>
Reviewed-by: Sashiko <no-reply@sashiko.virtuozzo.com>
---
 hw/block/vhost-blk.c | 68 ++++++++++++++++++++++++++++++++++++++++----
 1 file changed, 63 insertions(+), 5 deletions(-)

diff --git a/hw/block/vhost-blk.c b/hw/block/vhost-blk.c
index 0be51a10dbc..c62f400ad46 100644
--- a/hw/block/vhost-blk.c
+++ b/hw/block/vhost-blk.c
@@ -33,6 +33,50 @@
 #include "migration/cpr.h"
 #include "system/runstate.h"
 
+/*
+ * Spread the virtqueues round-robin over nworkers kernel threads. Thread 0 is
+ * the default worker VHOST_SET_OWNER created; vq 0 and every nworkers-th vq
+ * after it stay on it.
+ */
+static int vhost_blk_set_workers(struct vhost_dev *hdev, int nworkers)
+{
+    uint32_t ids[VHOST_BLK_MAX_QUEUES];
+    int i, ret;
+
+    assert(nworkers <= ARRAY_SIZE(ids));
+
+    for (i = 1; i < hdev->nvqs; i++) {
+        int w = i % nworkers;
+        struct vhost_vring_worker vq_worker = { .index = i };
+
+        if (w == 0) {
+            continue;
+        }
+
+        if (i < nworkers) {
+            struct vhost_worker_state worker = { };
+
+            ret = hdev->vhost_ops->vhost_new_worker(hdev, &worker);
+            if (ret < 0) {
+                error_report("vhost-blk: unable to create kernel thread: %s",
+                             strerror(-ret));
+                return ret;
+            }
+            ids[w] = worker.worker_id;
+        }
+
+        vq_worker.worker_id = ids[w];
+        ret = hdev->vhost_ops->vhost_attach_vring_worker(hdev, &vq_worker);
+        if (ret < 0) {
+            error_report("vhost-blk: unable to attach kernel thread to "
+                         "queue %d: %s", i, strerror(-ret));
+            return ret;
+        }
+    }
+
+    return 0;
+}
+
 static int vhost_blk_start(VirtIODevice *vdev)
 {
     VHostBlk *s = VHOST_BLK(vdev);
@@ -52,16 +96,30 @@ static int vhost_blk_start(VirtIODevice *vdev)
         return 0;
     }
 
+    nworkers = MIN(s->conf.num_threads, hdev->nvqs);
+
+    ret = hdev->vhost_ops->vhost_set_fork_from_owner(hdev,
+                                                     VHOST_FORK_OWNER_TASK);
+    if (ret < 0) {
+        if (nworkers > 1) {
+            error_report("vhost-blk: unable to set vhost_task mode: %s",
+                         strerror(-ret));
+            return ret;
+        }
+        warn_report_once("vhost-blk: unable to set vhost_task mode, "
+                         "using a kthread worker: %s", strerror(-ret));
+    }
+
     if (ioctl(s->vhostfd, VHOST_SET_OWNER, NULL)) {
         error_report("vhost-blk: unable to set owner");
         return -ENOSYS;
     }
 
-    nworkers = s->conf.num_threads;
-    if (nworkers != 1 && ioctl(s->vhostfd, VHOST_SET_NWORKERS, &nworkers)) {
-        error_report("vhost-blk: unable to set number of kernel threads");
-        ret = -EINVAL;
-        goto err_reset_owner;
+    if (nworkers > 1) {
+        ret = vhost_blk_set_workers(hdev, nworkers);
+        if (ret < 0) {
+            goto err_reset_owner;
+        }
     }
 
     if (s->conf.serial) {
-- 
2.53.0


^ permalink raw reply	[flat|nested] 9+ messages in thread

* [PATCH hci-8.2 v8 7/7] linux-headers: drop VHOST_SET_NWORKERS
  2026-11-20 13:47 [PATCH hci-8.2 v8 0/7] vhost-blk: switch from VHOST_SET_NWORKERS to the mainstream worker ioctls Alexander Ivanov
                   ` (5 preceding siblings ...)
  2026-11-20 13:47 ` [PATCH hci-8.2 v8 6/7] vhost-blk: create kernel threads with the mainstream worker ioctls Alexander Ivanov
@ 2026-11-20 13:47 ` Alexander Ivanov
  6 siblings, 0 replies; 9+ messages in thread
From: Alexander Ivanov @ 2026-11-20 13:47 UTC (permalink / raw)
  To: andrey.zhadchenko; +Cc: andrey.drobyshev, svt-core, Sashiko

vhost-blk no longer uses it, and the kernel drops the ioctl.

https://virtuozzo.atlassian.net/browse/VSTOR-107537

Signed-off-by: Alexander Ivanov <alexander.ivanov@virtuozzo.com>
Reviewed-by: Andrey Drobyshev <andrey.drobyshev@virtuozzo.com>
Reviewed-by: Andrey Zhadchenko <andrey.zhadchenko@virtuozzo.com>
Reviewed-by: Sashiko <no-reply@sashiko.virtuozzo.com>
---
 linux-headers/linux/vhost.h | 9 ---------
 1 file changed, 9 deletions(-)

diff --git a/linux-headers/linux/vhost.h b/linux-headers/linux/vhost.h
index baad06412d4..b6eb628d973 100644
--- a/linux-headers/linux/vhost.h
+++ b/linux-headers/linux/vhost.h
@@ -102,15 +102,6 @@
 #define VHOST_GET_VRING_WORKER _IOWR(VHOST_VIRTIO, 0x16,		\
 				     struct vhost_vring_worker)
 
-/* Set number of vhost workers
- * Currently number of vhost workers can only be increased.
- * All workers are freed upon reset.
- * If the value is too big it is silently truncated to the maximum number of
- * supported vhost workers
- * Even if the error is returned it is possible that some workers were created
- */
-#define VHOST_SET_NWORKERS _IOW(VHOST_VIRTIO, 0x1F, int)
-
 /* The following ioctls use eventfd file descriptors to signal and poll
  * for events. */
 
-- 
2.53.0


^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [PATCH hci-8.2 v8 6/7] vhost-blk: create kernel threads with the mainstream worker ioctls
  2026-11-20 13:47 ` [PATCH hci-8.2 v8 6/7] vhost-blk: create kernel threads with the mainstream worker ioctls Alexander Ivanov
@ 2026-11-20 13:51   ` Andrey Drobyshev
  0 siblings, 0 replies; 9+ messages in thread
From: Andrey Drobyshev @ 2026-11-20 13:51 UTC (permalink / raw)
  To: Alexander Ivanov, andrey.zhadchenko; +Cc: svt-core, Sashiko

On 11/20/26 3:47 PM, Alexander Ivanov wrote:
> num-threads was passed to our own VHOST_SET_NWORKERS kernel ioctl. The
> kernel drops it with the revert of "drivers/vhost: add ioctl to
> increase the number of workers" in favour of the mainstream worker
> ioctls, which vhost-blk now supports. Create num-threads - 1 workers
> with VHOST_NEW_WORKER and attach them to the virtqueues round-robin
> with VHOST_ATTACH_VRING_WORKER, the layout VHOST_SET_NWORKERS produced.
> num-threads of 0 still means one thread and a value above num-queues
> is clamped to it, as VHOST_SET_NWORKERS did.
> 
> Our kernel, unlike mainline, defaults to kthread mode, where
> VHOST_NEW_WORKER fails with -EFAULT rather than -ENOTTY. So switch the
> device to vhost_task mode with VHOST_SET_FORK_FROM_OWNER before
> VHOST_SET_OWNER. Do it for num-threads=1 as well, so the default
> thread is of the same kind whatever the number of workers, but only
> warn when it fails there: a single thread works in either mode. A kernel
> without the ioctl fails it with -EPERM from the owner check rather
> than -ENOTTY, so the error code cannot tell it apart.
> 
> Fail to start vhost-blk if any of the worker ioctls fails rather than
> silently run on fewer threads than requested. num-threads above 1 now
> requires a kernel whose vhost-blk handles them.
> 
> https://virtuozzo.atlassian.net/browse/VSTOR-107537
> 
> Signed-off-by: Alexander Ivanov <alexander.ivanov@virtuozzo.com>
> Reviewed-by: Andrey Zhadchenko <andrey.zhadchenko@virtuozzo.com>
> Reviewed-by: Sashiko <no-reply@sashiko.virtuozzo.com>
> ---
>  [...]

Reviewed-by: Andrey Drobyshev <andrey.drobyshev@virtuozzo.com>


^ permalink raw reply	[flat|nested] 9+ messages in thread

end of thread, other threads:[~2026-11-20 13:51 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-11-20 13:47 [PATCH hci-8.2 v8 0/7] vhost-blk: switch from VHOST_SET_NWORKERS to the mainstream worker ioctls Alexander Ivanov
2026-11-20 13:47 ` [PATCH hci-8.2 v8 1/7] vhost-backend: add vhost_set_fork_from_owner op Alexander Ivanov
2026-11-20 13:47 ` [PATCH hci-8.2 v8 2/7] vhost-blk: reject num-queues above VHOST_BLK_MAX_QUEUES Alexander Ivanov
2026-11-20 13:47 ` [PATCH hci-8.2 v8 3/7] vhost-blk: use a local vhost_dev pointer in start and stop Alexander Ivanov
2026-11-20 13:47 ` [PATCH hci-8.2 v8 4/7] vhost-blk: fix vhost_blk_start() returning 0 on failure Alexander Ivanov
2026-11-20 13:47 ` [PATCH hci-8.2 v8 5/7] vhost-blk: reset the owner when vhost_blk_start() fails Alexander Ivanov
2026-11-20 13:47 ` [PATCH hci-8.2 v8 6/7] vhost-blk: create kernel threads with the mainstream worker ioctls Alexander Ivanov
2026-11-20 13:51   ` Andrey Drobyshev
2026-11-20 13:47 ` [PATCH hci-8.2 v8 7/7] linux-headers: drop VHOST_SET_NWORKERS Alexander Ivanov

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.