All Virtuozzo development lists (kernel + QEMU)
 help / color / mirror / Atom feed
From: Alexander Ivanov <alexander.ivanov@virtuozzo.com>
To: andrey.zhadchenko@virtuozzo.com
Cc: andrey.drobyshev@virtuozzo.com, svt-core@virtuozzo.com
Subject: [PATCH hci-8.1 1/2] vhost-blk: create kernel threads with the mainstream worker ioctls
Date: Tue, 27 Oct 2026 12:38:56 +0100	[thread overview]
Message-ID: <20261027113857.2928048-2-alexander.ivanov@virtuozzo.com> (raw)
In-Reply-To: <20261027113857.2928048-1-alexander.ivanov@virtuozzo.com>

num-threads was passed to our own VHOST_SET_NWORKERS kernel ioctl.
The kernel is dropping it in favour of the mainstream worker ioctls,
which vhost-blk now supports, so use those: create num-threads - 1
workers with VHOST_NEW_WORKER and attach them to the virtqueues
round-robin with VHOST_ATTACH_VRING_WORKER, the default worker being
the first one. This is the layout VHOST_SET_NWORKERS produced.

VHOST_NEW_WORKER works only in vhost_task mode, while the kernel
defaults to kthreads, so switch the device to vhost_task mode with
VHOST_SET_FORK_FROM_OWNER before VHOST_SET_OWNER. VHOST_RESET_OWNER
restores the default, hence this is done on every start. With
num-threads=1 nothing changes: no worker ioctls, kthread mode.

On a kernel without these ioctls warn and run a single thread rather
than fail to start vhost-blk.

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

Signed-off-by: Alexander Ivanov <alexander.ivanov@virtuozzo.com>
---
 hw/block/vhost-blk.c | 75 +++++++++++++++++++++++++++++++++++++++++---
 1 file changed, 71 insertions(+), 4 deletions(-)

diff --git a/hw/block/vhost-blk.c b/hw/block/vhost-blk.c
index abddff02c6d..99fec9533a9 100644
--- a/hw/block/vhost-blk.c
+++ b/hw/block/vhost-blk.c
@@ -33,6 +33,51 @@
 #include "migration/cpr.h"
 #include "system/runstate.h"
 
+/*
+ * Spread the virtqueues round-robin over num-threads workers, the first one
+ * being the default worker that VHOST_SET_OWNER created.
+ */
+static int vhost_blk_set_workers(VHostBlk *s, int nworkers)
+{
+    uint32_t ids[VHOST_BLK_MAX_QUEUES];
+    struct vhost_worker_state worker;
+    struct vhost_vring_worker vq_worker;
+    int ret, i;
+
+    for (i = 1; i < nworkers; i++) {
+        memset(&worker, 0, sizeof(worker));
+        if (ioctl(s->vhostfd, VHOST_NEW_WORKER, &worker)) {
+            ret = -errno;
+            if (ret == -ENOTTY && i == 1) {
+                warn_report_once("vhost-blk: kernel does not support worker "
+                                 "ioctls, using a single kernel thread");
+                return 0;
+            }
+            error_report("vhost-blk: unable to create kernel thread: %s",
+                         strerror(-ret));
+            return ret;
+        }
+        ids[i] = worker.worker_id;
+    }
+
+    for (i = 0; i < s->dev.nvqs; i++) {
+        if (i % nworkers == 0) {
+            continue;
+        }
+        memset(&vq_worker, 0, sizeof(vq_worker));
+        vq_worker.index = i;
+        vq_worker.worker_id = ids[i % nworkers];
+        if (ioctl(s->vhostfd, VHOST_ATTACH_VRING_WORKER, &vq_worker)) {
+            ret = -errno;
+            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);
@@ -51,15 +96,37 @@ static int vhost_blk_start(VirtIODevice *vdev)
         return 0;
     }
 
+    nworkers = MIN(s->conf.num_threads, s->dev.nvqs);
+    if (nworkers > 1) {
+        /*
+         * VHOST_NEW_WORKER works only in vhost_task mode, and
+         * VHOST_RESET_OWNER brings the kthread default back.
+         */
+        uint8_t fork_owner = VHOST_FORK_OWNER_TASK;
+
+        if (ioctl(s->vhostfd, VHOST_SET_FORK_FROM_OWNER, &fork_owner)) {
+            ret = -errno;
+            if (ret != -ENOTTY) {
+                error_report("vhost-blk: unable to set vhost task mode: %s",
+                             strerror(-ret));
+                return ret;
+            }
+            warn_report_once("vhost-blk: kernel does not support vhost "
+                             "tasks, using a single kernel thread");
+            nworkers = 1;
+        }
+    }
+
     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");
-        return -EINVAL;
+    if (nworkers > 1) {
+        ret = vhost_blk_set_workers(s, nworkers);
+        if (ret < 0) {
+            return ret;
+        }
     }
 
     if (s->conf.serial) {
-- 
2.53.0


  reply	other threads:[~2026-10-27 11:39 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-10-27 11:38 [PATCH hci-8.1 0/2] vhost-blk: switch from VHOST_SET_NWORKERS to " Alexander Ivanov
2026-10-27 11:38 ` Alexander Ivanov [this message]
2026-10-27 11:43   ` [PATCH hci-8.1 1/2] vhost-blk: create kernel threads with " no-reply
2026-10-27 12:11     ` Alexander Ivanov
2026-10-27 11:38 ` [PATCH hci-8.1 2/2] linux-headers: drop VHOST_SET_NWORKERS Alexander Ivanov
2026-10-27 11:42   ` no-reply
2026-10-27 12:29     ` Alexander Ivanov

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=20261027113857.2928048-2-alexander.ivanov@virtuozzo.com \
    --to=alexander.ivanov@virtuozzo.com \
    --cc=andrey.drobyshev@virtuozzo.com \
    --cc=andrey.zhadchenko@virtuozzo.com \
    --cc=svt-core@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 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.