Skip to contentSkip to conversation

vhost-blk: in-kernel accelerator for virtio-blk guests

RFC PATCH In review Andrey Zhadchenko 3 participants 16 messages Oct 13, 2022 – Jun 24, 2024 1 message not in the archive

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

v2 cover vhost-blk: in-kernel accelerator for virtio-blk guests

As there is some interest from QEMU userspace, I am sending second version of this patchet.

Main addition is a few patches about vhost multithreading so vhost-blk can be scaled. Generally the idea is not a new one - somehow attach workers to the virtqueues and do the work on them.

I have seen several previous attemps like cgroup-aware worker pools or the userspace threads, but they seem very complicated and involve a lot of subsystems. Probably just spawning a few more vhost threads can do a good job.

As this is RFC, I did not convert any vhost users except vhost_blk. If anyone is interested in this regarding other modules, please tell me. I can test it to see if it is beneficial and maybe send multithreading separately.
Also multithreading part may eventually be of help with vdpa-blk.

---

Although QEMU virtio-blk is quite fast, there is still some room for improvements. Disk latency can be reduced if we handle virito-blk requests in host kernel so we avoid a lot of syscalls and context switches. The idea is quite simple - QEMU gives us block device and we translate any incoming virtio requests into bio and push them into bdev. The biggest disadvantage of this vhost-blk flavor is raw format. Luckily Kirill Thai proposed device mapper driver for QCOW2 format to attach files as block devices: https://www.spinics.net/lists/kernel/msg4292965.html

Also by using kernel modules we can bypass iothread limitation and finaly scale block requests with cpus for high-performance devices.

There have already been several attempts to write vhost-blk:

Asias' version: https://lkml.org/lkml/2012/12/1/174
Badari's version: https://lwn.net/Articles/379864/
Vitaly's https://lwn.net/Articles/770965/

The main difference between them is API to access backend file. The fastest one is Asias's version with bio flavor. It is also the most reviewed and have the most features. So vhost_blk module is partially based on it. Multiple virtqueue support was addded, some places reworked. Added support for several vhost workers.

test setup and results:
fio --direct=1 --rw=randread --bs=4k --ioengine=libaio --iodepth=128 QEMU drive options: cache=none
filesystem: xfs

SSD:

               | randread, IOPS  | randwrite, IOPS |
Host           |      95.8k	 |	85.3k	   |
QEMU virtio    |      61.5k	 |	79.9k	   |
QEMU vhost-blk |      95.6k	 |	84.3k	   |

RAMDISK (vq == vcpu == numjobs):

                 | randread, IOPS | randwrite, IOPS |
virtio, 1vcpu    |	133k	  |	 133k       |
virtio, 2vcpu    |	305k      |	 306k       |
virtio, 4vcpu    |	310k	  |	 298k       |
virtio, 8vcpu    |	271k      |	 252k       |
vhost-blk, 1vcpu |	110k	  |	 113k       |
vhost-blk, 2vcpu |	247k	  |	 252k       |
vhost-blk, 4vcpu |	558k	  |	 556k       |
vhost-blk, 8vcpu |	576k	  |	 575k       | *single kernel thread
vhost-blk, 8vcpu |      803k      |      779k       | *two kernel threads

v2:
Re-measured virtio performance with aio=threads and iothread on latest QEMU

vhost-blk changes:
- removed unused VHOST_BLK_VQ
- reworked bio handling a bit: now add all pages from signle iov into
bio until it is full istead of allocating one bio per page
- changed how to calculate sector incrementation
- check move_iovec() in vhost_blk_req_handle()
- remove snprintf check and better check ret from copy_to_iter for
VIRTIO_BLK_ID_BYTES requests
- discard vq request if vhost_blk_req_handle() returned negative code
- forbid to change nonzero backend in vhost_blk_set_backend(). First of
all, QEMU sets backend only once. Also if we want to change backend when we already running requests we need to be much more careful in vhost_blk_handle_guest_kick() as it is not taking any references. If userspace want to change backend that bad it can always reset device.
- removed EXPERIMENTAL from Kconfig

Andrey Zhadchenko (10):
drivers/vhost: vhost-blk accelerator for virtio-blk guests drivers/vhost: use array to store workers drivers/vhost: adjust vhost to flush all workers drivers/vhost: rework cgroups attachment to be worker aware drivers/vhost: rework worker creation drivers/vhost: add ioctl to increase the number of workers drivers/vhost: assign workers to virtqueues drivers/vhost: add API to queue work at virtqueue's worker drivers/vhost: allow polls to be bound to workers via vqs drivers/vhost: queue vhost_blk works at vq workers

 drivers/vhost/Kconfig      |  12 +
 drivers/vhost/Makefile     |   3 +
 drivers/vhost/blk.c        | 819 +++++++++++++++++++++++++++++++++++++
 drivers/vhost/vhost.c      | 263 +++++++++---
 drivers/vhost/vhost.h      |  21 +-
 include/uapi/linux/vhost.h |  13 +
 6 files changed, 1064 insertions(+), 67 deletions(-)
 create mode 100644 drivers/vhost/blk.c
Signature · 1 line
2.31.1
#1 lore raw

RFC PATCH v2 1/10 drivers/vhost: vhost-blk accelerator for virtio-blk guests

Although QEMU virtio is quite fast, there is still some room for improvements. Disk latency can be reduced if we handle virito-blk requests in host kernel istead of passing them to QEMU. The patch adds vhost-blk kernel module to do so.

Some test setups:
fio --direct=1 --rw=randread --bs=4k --ioengine=libaio --iodepth=128 QEMU drive options: cache=none
filesystem: xfs

SSD:

               | randread, IOPS  | randwrite, IOPS |
Host           |      95.8k      |      85.3k      |
QEMU virtio    |      57.5k      |      79.4k      |
QEMU vhost-blk |      95.6k      |      84.3k      |

RAMDISK (vq == vcpu = numjobs):

                 | randread, IOPS | randwrite, IOPS |
virtio, 1vcpu    |      133k      |	 133k       |
virtio, 2vcpu    |      305k      |      306k       |
virtio, 4vcpu    |      310k      |      298k       |
vhost-blk, 1vcpu |      110k      |	 113k       |
vhost-blk, 2vcpu |      247k      |      252k       |
vhost-blk, 4vcpu |	558k	  |	 556k       |

v2:
- removed unused VHOST_BLK_VQ
- reworked bio handling a bit: now add all pages from signle iov into
bio until it is full istead of allocating one bio per page
- changed sector incrementation calculation
- check move_iovec() in vhost_blk_req_handle()
- remove snprintf check and better check ret from copy_to_iter for
VIRTIO_BLK_ID_BYTES requests
- discard vq request if vhost_blk_req_handle() returned negative code
- forbid to change nonzero backend in vhost_blk_set_backend(). First of
all, QEMU sets backend only once. Also if we want to change backend when we already running requests we need to be much more careful in vhost_blk_handle_guest_kick() as it is not taking any references. If userspace want to change backend that bad it can always reset device.
- removed EXPERIMENTAL from Kconfig

---
 drivers/vhost/Kconfig      |  12 +
 drivers/vhost/Makefile     |   3 +
 drivers/vhost/blk.c        | 820 +++++++++++++++++++++++++++++++++++++
 include/uapi/linux/vhost.h |   4 +
 4 files changed, 839 insertions(+)
 create mode 100644 drivers/vhost/blk.c
drivers/vhost/Kconfig +12 -0
index 587fbae06182..e1389bf0c10b 100644
@@ -89,4 +89,16 @@ config VHOST_CROSS_ENDIAN_LEGACY
 
 	  If unsure, say "N".
 
+config VHOST_BLK
+	tristate "Host kernel accelerator for virtio-blk"
+	depends on BLOCK && EVENTFD
+	select VHOST
+	default n
+	help
+	  This kernel module can be loaded in host kernel to accelerate
+	  guest vm with virtio-blk driver.
+
+	  To compile this driver as a module, choose M here: the module will
+	  be called vhost_blk.
+
 endif
drivers/vhost/Makefile +3 -0
index f3e1897cce85..c76cc4f5fcd8 100644
@@ -17,3 +17,6 @@ obj-$(CONFIG_VHOST)	+= vhost.o
 
 obj-$(CONFIG_VHOST_IOTLB) += vhost_iotlb.o
 vhost_iotlb-y := iotlb.o
+
+obj-$(CONFIG_VHOST_BLK) += vhost_blk.o
+vhost_blk-y := blk.o
drivers/vhost/blk.c new file +820 -0

820 lines not on this page · raw message

include/uapi/linux/vhost.h +4 -0
index f9f115a7c75b..56a709e31709 100644
@@ -180,4 +180,8 @@
  */
 #define VHOST_VDPA_SUSPEND		_IO(VHOST_VIRTIO, 0x7D)
 
+/* VHOST_BLK specific defines */
+#define VHOST_BLK_SET_BACKEND		_IOW(VHOST_VIRTIO, 0xFF, \
+					     struct vhost_vring_file)
+
 #endif
Signature · 1 line
2.31.1
#2 lore raw

RFC PATCH v2 2/10 drivers/vhost: use array to store workers

In reply to Andrey Zhadchenko

We want to support several vhost workers. The first step is to rework vhost to use array of workers rather than single pointer. Update creation and cleanup routines.

---
 drivers/vhost/vhost.c | 75 +++++++++++++++++++++++++++++++------------
 drivers/vhost/vhost.h | 10 +++++-
 2 files changed, 63 insertions(+), 22 deletions(-)
drivers/vhost/vhost.c +54 -21
index 40097826cff0..40cc50c33c66 100644
@@ -231,11 +231,24 @@ void vhost_poll_stop(struct vhost_poll *poll)
 }
 EXPORT_SYMBOL_GPL(vhost_poll_stop);
 
+static void vhost_work_queue_at_worker(struct vhost_worker *w,
+				       struct vhost_work *work)
+{
+	if (!test_and_set_bit(VHOST_WORK_QUEUED, &work->flags)) {
+		/* We can only add the work to the list after we're
+		 * sure it was not in the list.
+		 * test_and_set_bit() implies a memory barrier.
+		 */
+		llist_add(&work->node, &w->work_list);
+		wake_up_process(w->worker);
+	}
+}
+
 void vhost_dev_flush(struct vhost_dev *dev)
 {
 	struct vhost_flush_struct flush;
 
-	if (dev->worker) {
+	if (dev->workers[0].worker) {
 		init_completion(&flush.wait_event);
 		vhost_work_init(&flush.work, vhost_flush_work);
 
@@ -247,17 +260,12 @@ EXPORT_SYMBOL_GPL(vhost_dev_flush);
 
 void vhost_work_queue(struct vhost_dev *dev, struct vhost_work *work)
 {
-	if (!dev->worker)
+	struct vhost_worker *w = &dev->workers[0];
+
+	if (!w->worker)
 		return;
 
-	if (!test_and_set_bit(VHOST_WORK_QUEUED, &work->flags)) {
-		/* We can only add the work to the list after we're
-		 * sure it was not in the list.
-		 * test_and_set_bit() implies a memory barrier.
-		 */
-		llist_add(&work->node, &dev->work_list);
-		wake_up_process(dev->worker);
-	}
+	vhost_work_queue_at_worker(w, work);
 }
 EXPORT_SYMBOL_GPL(vhost_work_queue);
 
@@ -333,9 +341,29 @@ static void vhost_vq_reset(struct vhost_dev *dev,
 	__vhost_vq_meta_reset(vq);
 }
 
+static void vhost_worker_reset(struct vhost_worker *w)
+{
+	init_llist_head(&w->work_list);
+	w->worker = NULL;
+}
+
+void vhost_cleanup_workers(struct vhost_dev *dev)
+{
+	int i;
+
+	for (i = 0; i < dev->nworkers; ++i) {
+		WARN_ON(!llist_empty(&dev->workers[i].work_list));
+		kthread_stop(dev->workers[i].worker);
+		vhost_worker_reset(&dev->workers[i]);
+	}
+
+	dev->nworkers = 0;
+}
+
 static int vhost_worker(void *data)
 {
-	struct vhost_dev *dev = data;
+	struct vhost_worker *w = data;
+	struct vhost_dev *dev = w->dev;
 	struct vhost_work *work, *work_next;
 	struct llist_node *node;
 
@@ -350,7 +378,7 @@ static int vhost_worker(void *data)
 			break;
 		}
 
-		node = llist_del_all(&dev->work_list);
+		node = llist_del_all(&w->work_list);
 		if (!node)
 			schedule();
 
@@ -473,7 +501,6 @@ void vhost_dev_init(struct vhost_dev *dev,
 	dev->umem = NULL;
 	dev->iotlb = NULL;
 	dev->mm = NULL;
-	dev->worker = NULL;
 	dev->iov_limit = iov_limit;
 	dev->weight = weight;
 	dev->byte_weight = byte_weight;
@@ -485,6 +512,11 @@ void vhost_dev_init(struct vhost_dev *dev,
 	INIT_LIST_HEAD(&dev->pending_list);
 	spin_lock_init(&dev->iotlb_lock);
 
+	dev->nworkers = 0;
+	for (i = 0; i < VHOST_MAX_WORKERS; ++i) {
+		dev->workers[i].dev = dev;
+		vhost_worker_reset(&dev->workers[i]);
+	}
 
 	for (i = 0; i < dev->nvqs; ++i) {
 		vq = dev->vqs[i];
@@ -594,7 +626,8 @@ long vhost_dev_set_owner(struct vhost_dev *dev)
 			goto err_worker;
 		}
 
-		dev->worker = worker;
+		dev->workers[0].worker = worker;
+		dev->nworkers = 1;
 		wake_up_process(worker); /* avoid contributing to loadavg */
 
 		err = vhost_attach_cgroups(dev);
@@ -608,9 +641,10 @@ long vhost_dev_set_owner(struct vhost_dev *dev)
 
 	return 0;
 err_cgroup:
-	if (dev->worker) {
-		kthread_stop(dev->worker);
-		dev->worker = NULL;
+	dev->nworkers = 0;
+	if (dev->workers[0].worker) {
+		kthread_stop(dev->workers[0].worker);
+		dev->workers[0].worker = NULL;
 	}
 err_worker:
 	vhost_detach_mm(dev);
@@ -693,6 +727,7 @@ void vhost_dev_cleanup(struct vhost_dev *dev)
 			eventfd_ctx_put(dev->vqs[i]->call_ctx.ctx);
 		vhost_vq_reset(dev, dev->vqs[i]);
 	}
+
 	vhost_dev_free_iovecs(dev);
 	if (dev->log_ctx)
 		eventfd_ctx_put(dev->log_ctx);
@@ -704,10 +739,8 @@ void vhost_dev_cleanup(struct vhost_dev *dev)
 	dev->iotlb = NULL;
 	vhost_clear_msg(dev);
 	wake_up_interruptible_poll(&dev->wait, EPOLLIN | EPOLLRDNORM);
-	WARN_ON(!llist_empty(&dev->work_list));
-	if (dev->worker) {
-		kthread_stop(dev->worker);
-		dev->worker = NULL;
+	if (dev->use_worker) {
+		vhost_cleanup_workers(dev);
 		dev->kcov_handle = 0;
 	}
 	vhost_detach_mm(dev);
drivers/vhost/vhost.h +9 -1
index d9109107af08..fc365b08fc38 100644
@@ -25,6 +25,13 @@ struct vhost_work {
 	unsigned long		flags;
 };
 
+#define VHOST_MAX_WORKERS 4
+struct vhost_worker {
+	struct task_struct *worker;
+	struct llist_head work_list;
+	struct vhost_dev *dev;
+};
+
 /* Poll a file (eventfd or socket) */
 /* Note: there's nothing vhost specific about this structure. */
 struct vhost_poll {
@@ -148,7 +155,8 @@ struct vhost_dev {
 	int nvqs;
 	struct eventfd_ctx *log_ctx;
 	struct llist_head work_list;
-	struct task_struct *worker;
+	struct vhost_worker workers[VHOST_MAX_WORKERS];
+	int nworkers;
 	struct vhost_iotlb *umem;
 	struct vhost_iotlb *iotlb;
 	spinlock_t iotlb_lock;
Signature · 1 line
2.31.1
#3 lore raw

RFC PATCH v2 3/10 drivers/vhost: adjust vhost to flush all workers

In reply to Andrey Zhadchenko

Make vhost_dev_flush support several workers and flush them simultaneously

---
 drivers/vhost/vhost.c | 16 ++++++++++------
 1 file changed, 10 insertions(+), 6 deletions(-)
drivers/vhost/vhost.c +10 -6
index 40cc50c33c66..5dae8e8f8163 100644
@@ -246,15 +246,19 @@ static void vhost_work_queue_at_worker(struct vhost_worker *w,
 
 void vhost_dev_flush(struct vhost_dev *dev)
 {
-	struct vhost_flush_struct flush;
+	struct vhost_flush_struct flush[VHOST_MAX_WORKERS];
+	int i, nworkers;
 
-	if (dev->workers[0].worker) {
-		init_completion(&flush.wait_event);
-		vhost_work_init(&flush.work, vhost_flush_work);
+	nworkers = READ_ONCE(dev->nworkers);
 
-		vhost_work_queue(dev, &flush.work);
-		wait_for_completion(&flush.wait_event);
+	for (i = 0; i < nworkers; i++) {
+		init_completion(&flush[i].wait_event);
+		vhost_work_init(&flush[i].work, vhost_flush_work);
+		vhost_work_queue_at_worker(&dev->workers[i], &flush[i].work);
 	}
+
+	for (i = 0; i < nworkers; i++)
+		wait_for_completion(&flush[i].wait_event);
 }
 EXPORT_SYMBOL_GPL(vhost_dev_flush);
 
Signature · 1 line
2.31.1
#4 lore raw

RFC PATCH v2 4/10 drivers/vhost: rework cgroups attachment to be worker aware

In reply to Andrey Zhadchenko

Rework vhost_attach_cgroups to manipulate specified worker. Implement vhost_worker_flush as we need to flush specific worker.

---
 drivers/vhost/vhost.c | 18 ++++++++++++++----
 1 file changed, 14 insertions(+), 4 deletions(-)
drivers/vhost/vhost.c +14 -4
index 5dae8e8f8163..e6bcb2605ee0 100644
@@ -262,6 +262,16 @@ void vhost_dev_flush(struct vhost_dev *dev)
 }
 EXPORT_SYMBOL_GPL(vhost_dev_flush);
 
+static void vhost_worker_flush(struct vhost_worker *w)
+{
+       struct vhost_flush_struct flush;
+
+       init_completion(&flush.wait_event);
+       vhost_work_init(&flush.work, vhost_flush_work);
+       vhost_work_queue_at_worker(w, &flush.work);
+       wait_for_completion(&flush.wait_event);
+}
+
 void vhost_work_queue(struct vhost_dev *dev, struct vhost_work *work)
 {
 	struct vhost_worker *w = &dev->workers[0];
@@ -559,14 +569,14 @@ static void vhost_attach_cgroups_work(struct vhost_work *work)
 	s->ret = cgroup_attach_task_all(s->owner, current);
 }
 
-static int vhost_attach_cgroups(struct vhost_dev *dev)
+static int vhost_worker_attach_cgroups(struct vhost_worker *w)
 {
 	struct vhost_attach_cgroups_struct attach;
 
 	attach.owner = current;
 	vhost_work_init(&attach.work, vhost_attach_cgroups_work);
-	vhost_work_queue(dev, &attach.work);
-	vhost_dev_flush(dev);
+	vhost_work_queue_at_worker(w, &attach.work);
+	vhost_worker_flush(w);
 	return attach.ret;
 }
 
@@ -634,7 +644,7 @@ long vhost_dev_set_owner(struct vhost_dev *dev)
 		dev->nworkers = 1;
 		wake_up_process(worker); /* avoid contributing to loadavg */
 
-		err = vhost_attach_cgroups(dev);
+		err = vhost_worker_attach_cgroups(&dev->workers[0]);
 		if (err)
 			goto err_cgroup;
 	}
Signature · 1 line
2.31.1
#5 lore raw

RFC PATCH v2 5/10 drivers/vhost: rework worker creation

In reply to Andrey Zhadchenko

Add function to create a vhost worker and add it into the device. Rework vhost_dev_set_owner

---
 drivers/vhost/vhost.c | 68 +++++++++++++++++++++++++------------------
 1 file changed, 40 insertions(+), 28 deletions(-)
drivers/vhost/vhost.c +40 -28
index e6bcb2605ee0..219ad453ca27 100644
@@ -617,53 +617,65 @@ static void vhost_detach_mm(struct vhost_dev *dev)
 	dev->mm = NULL;
 }
 
-/* Caller should have device mutex */
-long vhost_dev_set_owner(struct vhost_dev *dev)
+static int vhost_add_worker(struct vhost_dev *dev)
 {
+	struct vhost_worker *w = &dev->workers[dev->nworkers];
 	struct task_struct *worker;
 	int err;
 
+	if (dev->nworkers == VHOST_MAX_WORKERS)
+		return -E2BIG;
+
+	worker = kthread_create(vhost_worker, w,
+				"vhost-%d-%d", current->pid, dev->nworkers);
+	if (IS_ERR(worker))
+		return PTR_ERR(worker);
+
+	w->worker = worker;
+	wake_up_process(worker); /* avoid contributing to loadavg */
+
+	err = vhost_worker_attach_cgroups(w);
+	if (err)
+		goto cleanup;
+
+	dev->nworkers++;
+	return 0;
+
+cleanup:
+	kthread_stop(worker);
+	w->worker = NULL;
+
+	return err;
+}
+
+/* Caller should have device mutex */
+long vhost_dev_set_owner(struct vhost_dev *dev)
+{
+	int err;
+
 	/* Is there an owner already? */
-	if (vhost_dev_has_owner(dev)) {
-		err = -EBUSY;
-		goto err_mm;
-	}
+	if (vhost_dev_has_owner(dev))
+		return -EBUSY;
 
 	vhost_attach_mm(dev);
 
 	dev->kcov_handle = kcov_common_handle();
 	if (dev->use_worker) {
-		worker = kthread_create(vhost_worker, dev,
-					"vhost-%d", current->pid);
-		if (IS_ERR(worker)) {
-			err = PTR_ERR(worker);
-			goto err_worker;
-		}
-
-		dev->workers[0].worker = worker;
-		dev->nworkers = 1;
-		wake_up_process(worker); /* avoid contributing to loadavg */
-
-		err = vhost_worker_attach_cgroups(&dev->workers[0]);
+		err = vhost_add_worker(dev);
 		if (err)
-			goto err_cgroup;
+			goto err_mm;
 	}
 
 	err = vhost_dev_alloc_iovecs(dev);
 	if (err)
-		goto err_cgroup;
+		goto err_worker;
 
 	return 0;
-err_cgroup:
-	dev->nworkers = 0;
-	if (dev->workers[0].worker) {
-		kthread_stop(dev->workers[0].worker);
-		dev->workers[0].worker = NULL;
-	}
 err_worker:
-	vhost_detach_mm(dev);
-	dev->kcov_handle = 0;
+	vhost_cleanup_workers(dev);
 err_mm:
+	vhost_detach_mm(dev);
+	dev->kcov_handle = 0;
 	return err;
 }
 EXPORT_SYMBOL_GPL(vhost_dev_set_owner);
Signature · 1 line
2.31.1
#6 lore raw

RFC PATCH v2 6/10 drivers/vhost: add ioctl to increase the number of workers

In reply to Andrey Zhadchenko

Finally add ioctl to allow userspace to create additional workers For now only allow to increase the number of workers

---
 drivers/vhost/vhost.c      | 32 +++++++++++++++++++++++++++++++-
 include/uapi/linux/vhost.h |  9 +++++++++
 2 files changed, 40 insertions(+), 1 deletion(-)
drivers/vhost/vhost.c +31 -1
index 219ad453ca27..09e3c4b1bd05 100644
@@ -648,6 +648,25 @@ static int vhost_add_worker(struct vhost_dev *dev)
 	return err;
 }
 
+static int vhost_set_workers(struct vhost_dev *dev, int n)
+{
+	int i, ret;
+
+	if (n > dev->nvqs)
+		n = dev->nvqs;
+
+	if (n > VHOST_MAX_WORKERS)
+		n = VHOST_MAX_WORKERS;
+
+	for (i = 0; i < n - dev->nworkers ; i++) {
+		ret = vhost_add_worker(dev);
+		if (ret)
+			break;
+	}
+
+	return ret;
+}
+
 /* Caller should have device mutex */
 long vhost_dev_set_owner(struct vhost_dev *dev)
 {
@@ -1821,7 +1840,7 @@ long vhost_dev_ioctl(struct vhost_dev *d, unsigned int ioctl, void __user *argp)
 	struct eventfd_ctx *ctx;
 	u64 p;
 	long r;
-	int i, fd;
+	int i, fd, n;
 
 	/* If you are not the owner, you can become one */
 	if (ioctl == VHOST_SET_OWNER) {
@@ -1878,6 +1897,17 @@ long vhost_dev_ioctl(struct vhost_dev *d, unsigned int ioctl, void __user *argp)
 		if (ctx)
 			eventfd_ctx_put(ctx);
 		break;
+	case VHOST_SET_NWORKERS:
+		r = get_user(n, (int __user *)argp);
+		if (r < 0)
+			break;
+		if (n < d->nworkers) {
+			r = -EINVAL;
+			break;
+		}
+
+		r = vhost_set_workers(d, n);
+		break;
 	default:
 		r = -ENOIOCTLCMD;
 		break;
include/uapi/linux/vhost.h +9 -0
index 56a709e31709..f78cec410460 100644
@@ -71,6 +71,15 @@
 #define VHOST_SET_VRING_ENDIAN _IOW(VHOST_VIRTIO, 0x13, struct vhost_vring_state)
 #define VHOST_GET_VRING_ENDIAN _IOW(VHOST_VIRTIO, 0x14, struct vhost_vring_state)
 
+/* Set number of vhost workers
+ * Currently nuber 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. */
 
Signature · 1 line
2.31.1
#7 lore raw

RFC PATCH v2 7/10 drivers/vhost: assign workers to virtqueues

In reply to Andrey Zhadchenko

Add worker pointer to every virtqueue. Add routine to assing workers to virtqueues and call it after any worker creation

---
 drivers/vhost/vhost.c | 14 ++++++++++++++
 drivers/vhost/vhost.h |  2 ++
 2 files changed, 16 insertions(+)
drivers/vhost/vhost.c +14 -0
index 09e3c4b1bd05..d80831d21fba 100644
@@ -353,6 +353,7 @@ static void vhost_vq_reset(struct vhost_dev *dev,
 	vq->iotlb = NULL;
 	vhost_vring_call_reset(&vq->call_ctx);
 	__vhost_vq_meta_reset(vq);
+	vq->worker = NULL;
 }
 
 static void vhost_worker_reset(struct vhost_worker *w)
@@ -667,6 +668,17 @@ static int vhost_set_workers(struct vhost_dev *dev, int n)
 	return ret;
 }
 
+static void vhost_assign_workers(struct vhost_dev *dev)
+{
+	int i, j = 0;
+
+	for (i = 0; i < dev->nvqs; i++) {
+		dev->vqs[i]->worker = &dev->workers[j];
+		if (++j == dev->nworkers)
+			j = 0;
+	}
+}
+
 /* Caller should have device mutex */
 long vhost_dev_set_owner(struct vhost_dev *dev)
 {
@@ -689,6 +701,7 @@ long vhost_dev_set_owner(struct vhost_dev *dev)
 	if (err)
 		goto err_worker;
 
+	vhost_assign_workers(dev);
 	return 0;
 err_worker:
 	vhost_cleanup_workers(dev);
@@ -1907,6 +1920,7 @@ long vhost_dev_ioctl(struct vhost_dev *d, unsigned int ioctl, void __user *argp)
 		}
 
 		r = vhost_set_workers(d, n);
+		vhost_assign_workers(d);
 		break;
 	default:
 		r = -ENOIOCTLCMD;
drivers/vhost/vhost.h +2 -0
index fc365b08fc38..b9a10c85b298 100644
@@ -137,6 +137,8 @@ struct vhost_virtqueue {
 	bool user_be;
 #endif
 	u32 busyloop_timeout;
+
+	struct vhost_worker *worker;
 };
 
 struct vhost_msg_node {
Signature · 1 line
2.31.1
#8 lore raw

RFC PATCH v2 8/10 drivers/vhost: add API to queue work at virtqueue's worker

In reply to Andrey Zhadchenko

Add routines to queue works on virtqueue assigned workers

---
 drivers/vhost/vhost.c | 22 ++++++++++++++++++++++
 drivers/vhost/vhost.h |  5 +++++
 2 files changed, 27 insertions(+)
drivers/vhost/vhost.c +22 -0
index d80831d21fba..fbf5fae1a6bf 100644
@@ -272,6 +272,17 @@ static void vhost_worker_flush(struct vhost_worker *w)
        wait_for_completion(&flush.wait_event);
 }
 
+void vhost_work_flush_vq(struct vhost_virtqueue *vq)
+{
+       struct vhost_worker *w = READ_ONCE(vq->worker);
+
+       if (!w)
+               return;
+
+       vhost_worker_flush(w);
+}
+EXPORT_SYMBOL_GPL(vhost_work_flush_vq);
+
 void vhost_work_queue(struct vhost_dev *dev, struct vhost_work *work)
 {
 	struct vhost_worker *w = &dev->workers[0];
@@ -283,6 +294,17 @@ void vhost_work_queue(struct vhost_dev *dev, struct vhost_work *work)
 }
 EXPORT_SYMBOL_GPL(vhost_work_queue);
 
+void vhost_work_vqueue(struct vhost_virtqueue *vq, struct vhost_work *work)
+{
+       struct vhost_worker *w = READ_ONCE(vq->worker);
+
+       if (!w)
+               return;
+
+       vhost_work_queue_at_worker(w, work);
+}
+EXPORT_SYMBOL_GPL(vhost_work_vqueue);
+
 /* A lockless hint for busy polling code to exit the loop */
 bool vhost_has_work(struct vhost_dev *dev)
 {
drivers/vhost/vhost.h +5 -0
index b9a10c85b298..bedf8b9d99de 100644
@@ -141,6 +141,11 @@ struct vhost_virtqueue {
 	struct vhost_worker *worker;
 };
 
+/* Queue the work on virtqueue assigned worker */
+void vhost_work_vqueue(struct vhost_virtqueue *vq, struct vhost_work *work);
+/* Flush virtqueue assigned worker */
+void vhost_work_flush_vq(struct vhost_virtqueue *vq);
+
 struct vhost_msg_node {
   union {
 	  struct vhost_msg msg;
Signature · 1 line
2.31.1
#9 lore raw

RFC PATCH v2 9/10 drivers/vhost: allow polls to be bound to workers via vqs

In reply to Andrey Zhadchenko

Allow vhost polls to be associated with vqs so we can queue them on assigned workers.
If polls are not associated with specific vqs queue them on the first virtqueue.

---
 drivers/vhost/vhost.c | 24 ++++++++++++++++--------
 drivers/vhost/vhost.h |  4 +++-
 2 files changed, 19 insertions(+), 9 deletions(-)
drivers/vhost/vhost.c +16 -8
index fbf5fae1a6bf..9c35908ebc4f 100644
@@ -170,7 +170,7 @@ static int vhost_poll_wakeup(wait_queue_entry_t *wait, unsigned mode, int sync,
 	if (!(key_to_poll(key) & poll->mask))
 		return 0;
 
-	if (!poll->dev->use_worker)
+	if (!poll->vq->dev->use_worker)
 		work->fn(work);
 	else
 		vhost_poll_queue(poll);
@@ -185,19 +185,27 @@ void vhost_work_init(struct vhost_work *work, vhost_work_fn_t fn)
 }
 EXPORT_SYMBOL_GPL(vhost_work_init);
 
-/* Init poll structure */
 void vhost_poll_init(struct vhost_poll *poll, vhost_work_fn_t fn,
 		     __poll_t mask, struct vhost_dev *dev)
+{
+       vhost_poll_init_vq(poll, fn, mask, dev->vqs[0]);
+}
+EXPORT_SYMBOL_GPL(vhost_poll_init);
+
+
+/* Init poll structure */
+void vhost_poll_init_vq(struct vhost_poll *poll, vhost_work_fn_t fn,
+                    __poll_t mask, struct vhost_virtqueue *vq)
 {
 	init_waitqueue_func_entry(&poll->wait, vhost_poll_wakeup);
 	init_poll_funcptr(&poll->table, vhost_poll_func);
 	poll->mask = mask;
-	poll->dev = dev;
+	poll->vq = vq;
 	poll->wqh = NULL;
 
 	vhost_work_init(&poll->work, fn);
 }
-EXPORT_SYMBOL_GPL(vhost_poll_init);
+EXPORT_SYMBOL_GPL(vhost_poll_init_vq);
 
 /* Start polling a file. We add ourselves to file's wait queue. The caller must
  * keep a reference to a file until after vhost_poll_stop is called. */
@@ -314,7 +322,7 @@ EXPORT_SYMBOL_GPL(vhost_has_work);
 
 void vhost_poll_queue(struct vhost_poll *poll)
 {
-	vhost_work_queue(poll->dev, &poll->work);
+	vhost_work_vqueue(poll->vq, &poll->work);
 }
 EXPORT_SYMBOL_GPL(vhost_poll_queue);
 
@@ -564,8 +572,8 @@ void vhost_dev_init(struct vhost_dev *dev,
 		mutex_init(&vq->mutex);
 		vhost_vq_reset(dev, vq);
 		if (vq->handle_kick)
-			vhost_poll_init(&vq->poll, vq->handle_kick,
-					EPOLLIN, dev);
+			vhost_poll_init_vq(&vq->poll, vq->handle_kick,
+					EPOLLIN, vq);
 	}
 }
 EXPORT_SYMBOL_GPL(vhost_dev_init);
@@ -1837,7 +1845,7 @@ long vhost_vring_ioctl(struct vhost_dev *d, unsigned int ioctl, void __user *arg
 	mutex_unlock(&vq->mutex);
 
 	if (pollstop && vq->handle_kick)
-		vhost_dev_flush(vq->poll.dev);
+		vhost_dev_flush(d);
 	return r;
 }
 EXPORT_SYMBOL_GPL(vhost_vring_ioctl);
drivers/vhost/vhost.h +3 -1
index bedf8b9d99de..fd0943de4d4f 100644
@@ -40,7 +40,7 @@ struct vhost_poll {
 	wait_queue_entry_t	wait;
 	struct vhost_work	work;
 	__poll_t		mask;
-	struct vhost_dev	*dev;
+	struct vhost_virtqueue	*vq;
 };
 
 void vhost_work_init(struct vhost_work *work, vhost_work_fn_t fn);
@@ -49,6 +49,8 @@ bool vhost_has_work(struct vhost_dev *dev);
 
 void vhost_poll_init(struct vhost_poll *poll, vhost_work_fn_t fn,
 		     __poll_t mask, struct vhost_dev *dev);
+void vhost_poll_init_vq(struct vhost_poll *poll, vhost_work_fn_t fn,
+		     __poll_t mask, struct vhost_virtqueue *vq);
 int vhost_poll_start(struct vhost_poll *poll, struct file *file);
 void vhost_poll_stop(struct vhost_poll *poll);
 void vhost_poll_queue(struct vhost_poll *poll);
Signature · 1 line
2.31.1
#10 lore raw

RFC PATCH v2 10/10 drivers/vhost: queue vhost_blk works at vq workers

In reply to Andrey Zhadchenko

Update vhost_blk to queue works on virtqueue workers. Together with previous changes this allows us to split virtio blk requests across several threads.

                        | randread, IOPS | randwrite, IOPS |
8vcpu, 1 kernel worker  |      576k      |      575k       |
8vcpu, 2 kernel workers |      803k      |      779k       |
---
 drivers/vhost/blk.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
drivers/vhost/blk.c +1 -2
index 42acdef452b3..f7db891ee985 100644
@@ -161,13 +161,12 @@ static inline int vhost_blk_set_status(struct vhost_blk_req *req, u8 status)
 static void vhost_blk_req_done(struct bio *bio)
 {
 	struct vhost_blk_req *req = bio->bi_private;
-	struct vhost_blk *blk = req->blk;
 
 	req->bio_err = blk_status_to_errno(bio->bi_status);
 
 	if (atomic_dec_and_test(&req->bio_nr)) {
 		llist_add(&req->llnode, &req->blk_vq->llhead);
-		vhost_work_queue(&blk->dev, &req->blk_vq->work);
+		vhost_work_vqueue(&req->blk_vq->vq, &req->blk_vq->work);
 	}
 
 	bio_put(bio);
Signature · 1 line
2.31.1
#11 lore raw
In reply to Andrey Zhadchenko

Sorry, I misspelled your email while sending the patchset. Linking you back as was originally intended

Kind regards, Andrey

Quoted 108 lines · Andrey Zhadchenko

As there is some interest from QEMU userspace, I am sending second version of this patchet.

Main addition is a few patches about vhost multithreading so vhost-blk can be scaled. Generally the idea is not a new one - somehow attach workers to the virtqueues and do the work on them.

I have seen several previous attemps like cgroup-aware worker pools or the userspace threads, but they seem very complicated and involve a lot of subsystems. Probably just spawning a few more vhost threads can do a good job.

As this is RFC, I did not convert any vhost users except vhost_blk. If anyone is interested in this regarding other modules, please tell me. I can test it to see if it is beneficial and maybe send multithreading separately.
Also multithreading part may eventually be of help with vdpa-blk.

---

Although QEMU virtio-blk is quite fast, there is still some room for improvements. Disk latency can be reduced if we handle virito-blk requests in host kernel so we avoid a lot of syscalls and context switches. The idea is quite simple - QEMU gives us block device and we translate any incoming virtio requests into bio and push them into bdev. The biggest disadvantage of this vhost-blk flavor is raw format. Luckily Kirill Thai proposed device mapper driver for QCOW2 format to attach files as block devices: https://www.spinics.net/lists/kernel/msg4292965.html

Also by using kernel modules we can bypass iothread limitation and finaly scale block requests with cpus for high-performance devices.

There have already been several attempts to write vhost-blk:

Asias' version: https://lkml.org/lkml/2012/12/1/174
Badari's version: https://lwn.net/Articles/379864/
Vitaly's https://lwn.net/Articles/770965/

The main difference between them is API to access backend file. The fastest one is Asias's version with bio flavor. It is also the most reviewed and have the most features. So vhost_blk module is partially based on it. Multiple virtqueue support was addded, some places reworked. Added support for several vhost workers.

test setup and results:
fio --direct=1 --rw=randread --bs=4k --ioengine=libaio --iodepth=128 QEMU drive options: cache=none
filesystem: xfs

SSD:

                | randread, IOPS  | randwrite, IOPS |
Host           |      95.8k	 |	85.3k	   |
QEMU virtio    |      61.5k	 |	79.9k	   |
QEMU vhost-blk |      95.6k	 |	84.3k	   |

RAMDISK (vq == vcpu == numjobs):

                  | randread, IOPS | randwrite, IOPS |
virtio, 1vcpu    |	133k	  |	 133k       |
virtio, 2vcpu    |	305k      |	 306k       |
virtio, 4vcpu    |	310k	  |	 298k       |
virtio, 8vcpu    |	271k      |	 252k       |
vhost-blk, 1vcpu |	110k	  |	 113k       |
vhost-blk, 2vcpu |	247k	  |	 252k       |
vhost-blk, 4vcpu |	558k	  |	 556k       |
vhost-blk, 8vcpu |	576k	  |	 575k       | *single kernel thread
vhost-blk, 8vcpu |      803k      |      779k       | *two kernel threads

v2:
Re-measured virtio performance with aio=threads and iothread on latest QEMU

vhost-blk changes:
- removed unused VHOST_BLK_VQ
- reworked bio handling a bit: now add all pages from signle iov into
bio until it is full istead of allocating one bio per page
- changed how to calculate sector incrementation
- check move_iovec() in vhost_blk_req_handle()
- remove snprintf check and better check ret from copy_to_iter for
VIRTIO_BLK_ID_BYTES requests
- discard vq request if vhost_blk_req_handle() returned negative code
- forbid to change nonzero backend in vhost_blk_set_backend(). First of
all, QEMU sets backend only once. Also if we want to change backend when we already running requests we need to be much more careful in vhost_blk_handle_guest_kick() as it is not taking any references. If userspace want to change backend that bad it can always reset device.
- removed EXPERIMENTAL from Kconfig

Andrey Zhadchenko (10):
drivers/vhost: vhost-blk accelerator for virtio-blk guests drivers/vhost: use array to store workers drivers/vhost: adjust vhost to flush all workers drivers/vhost: rework cgroups attachment to be worker aware drivers/vhost: rework worker creation drivers/vhost: add ioctl to increase the number of workers drivers/vhost: assign workers to virtqueues drivers/vhost: add API to queue work at virtqueue's worker drivers/vhost: allow polls to be bound to workers via vqs drivers/vhost: queue vhost_blk works at vq workers

  drivers/vhost/Kconfig      |  12 +
  drivers/vhost/Makefile     |   3 +
  drivers/vhost/blk.c        | 819 +++++++++++++++++++++++++++++++++++++
  drivers/vhost/vhost.c      | 263 +++++++++---
  drivers/vhost/vhost.h      |  21 +-
  include/uapi/linux/vhost.h |  13 +
  6 files changed, 1064 insertions(+), 67 deletions(-)

create mode 100644 drivers/vhost/blk.c

#12 lore raw

PATCH 1/1 Fix Issue: When VirtIO Backend providing VIRTIO_BLK_F_MQ feature, The file system of the front-end OS fails to be mounted.

---
 drivers/vhost/blk.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
drivers/vhost/blk.c +4 -2
index 44fbf253e773..0e946d9dfc33 100644
@@ -251,6 +251,7 @@ static int vhost_blk_bio_make(struct vhost_blk_req *req,
 	struct page **pages, *page;
 	struct bio *bio = NULL;
 	int bio_nr = 0;
+	sector_t sector_tmp;
 
 	if (unlikely(req->bi_opf == REQ_OP_FLUSH))
 		return vhost_blk_bio_make_simple(req, bdev);
@@ -270,6 +271,7 @@ static int vhost_blk_bio_make(struct vhost_blk_req *req,
 		req->bio = req->inline_bio;
 	}
 
+	sector_tmp = req->sector;
 	req->iov_nr = 0;
 	for (i = 0; i < iov_nr; i++) {
 		int pages_nr = iov_num_pages(&iov[i]);
@@ -302,7 +304,7 @@ static int vhost_blk_bio_make(struct vhost_blk_req *req,
 				bio = bio_alloc(GFP_KERNEL, pages_nr_total);
 				if (!bio)
 					goto fail;
-				bio->bi_iter.bi_sector  = req->sector;
+				bio->bi_iter.bi_sector  = sector_tmp;
 				bio_set_dev(bio, bdev);
 				bio->bi_private = req;
 				bio->bi_end_io  = vhost_blk_req_done;
@@ -314,7 +316,7 @@ static int vhost_blk_bio_make(struct vhost_blk_req *req,
 			iov_len		-= len;
 
 			pos = (iov_base & VHOST_BLK_SECTOR_MASK) + iov_len;
-			req->sector += pos >> VHOST_BLK_SECTOR_BITS;
+			sector_tmp += pos >> VHOST_BLK_SECTOR_BITS;
 		}
 
 		pages += pages_nr;
Signature · 1 line
2.25.1
#13 lore raw

Re: [PATCH] Fix Issue: When VirtIO Backend providing VIRTIO_BLK_F_MQ feature, The file system of the front-end OS fails to be mounted.

Hi

Thank you for the patch.
vhost-blk didn't spark enough interest to be reviewed and merged into the upstream and the code is not present here. I have forwarded your patch to relevant openvz kernel mailing list.

Quoted 42 lines · Lynch
---
  drivers/vhost/blk.c | 6 ++++--
  1 file changed, 4 insertions(+), 2 deletions(-)
drivers/vhost/blk.c +4 -2
index 44fbf253e773..0e946d9dfc33 100644
@@ -251,6 +251,7 @@ static int vhost_blk_bio_make(struct vhost_blk_req *req,
  	struct page **pages, *page;
  	struct bio *bio = NULL;
  	int bio_nr = 0;
+	sector_t sector_tmp;
 
  	if (unlikely(req->bi_opf == REQ_OP_FLUSH))
  		return vhost_blk_bio_make_simple(req, bdev);
@@ -270,6 +271,7 @@ static int vhost_blk_bio_make(struct vhost_blk_req *req,
  		req->bio = req->inline_bio;
  	}
 
+	sector_tmp = req->sector;
  	req->iov_nr = 0;
  	for (i = 0; i < iov_nr; i++) {
  		int pages_nr = iov_num_pages(&iov[i]);
@@ -302,7 +304,7 @@ static int vhost_blk_bio_make(struct vhost_blk_req *req,
  				bio = bio_alloc(GFP_KERNEL, pages_nr_total);
  				if (!bio)
  					goto fail;
-				bio->bi_iter.bi_sector  = req->sector;
+				bio->bi_iter.bi_sector  = sector_tmp;
  				bio_set_dev(bio, bdev);
  				bio->bi_private = req;
  				bio->bi_end_io  = vhost_blk_req_done;
@@ -314,7 +316,7 @@ static int vhost_blk_bio_make(struct vhost_blk_req *req,
  			iov_len		-= len;
 
  			pos = (iov_base & VHOST_BLK_SECTOR_MASK) + iov_len;
-			req->sector += pos >> VHOST_BLK_SECTOR_BITS;
+			sector_tmp += pos >> VHOST_BLK_SECTOR_BITS;
  		}
 
  		pages += pages_nr;
#14 lore raw

Re: [PATCH] Fix Issue: When VirtIO Backend providing VIRTIO_BLK_F_MQ feature, The file system of the front-end OS fails to be mounted.

Hi Lynch, Andrey,

thank you for the patch, but can you please describe the problem it fixes in a bit more details?
i see that the patch preserves original req->sector, but why/how that becomes important in case VIRTIO_BLK_F_MQ feature is set?

Thank you.

--
Best regards,

Konstantin Khorenko,
Virtuozzo Linux Kernel Team

Quoted 50 lines · Andrey Zhadchenko

Hi

Thank you for the patch.
vhost-blk didn't spark enough interest to be reviewed and merged into the upstream and the code is not present here.
I have forwarded your patch to relevant openvz kernel mailing list.

Quoted 42 lines · Lynch
---
   drivers/vhost/blk.c | 6 ++++--
   1 file changed, 4 insertions(+), 2 deletions(-)
drivers/vhost/blk.c +4 -2
index 44fbf253e773..0e946d9dfc33 100644
@@ -251,6 +251,7 @@ static int vhost_blk_bio_make(struct vhost_blk_req *req,
   	struct page **pages, *page;
   	struct bio *bio = NULL;
   	int bio_nr = 0;
+	sector_t sector_tmp;
 
   	if (unlikely(req->bi_opf == REQ_OP_FLUSH))
   		return vhost_blk_bio_make_simple(req, bdev);
@@ -270,6 +271,7 @@ static int vhost_blk_bio_make(struct vhost_blk_req *req,
   		req->bio = req->inline_bio;
   	}
 
+	sector_tmp = req->sector;
   	req->iov_nr = 0;
   	for (i = 0; i < iov_nr; i++) {
   		int pages_nr = iov_num_pages(&iov[i]);
@@ -302,7 +304,7 @@ static int vhost_blk_bio_make(struct vhost_blk_req *req,
   				bio = bio_alloc(GFP_KERNEL, pages_nr_total);
   				if (!bio)
   					goto fail;
-				bio->bi_iter.bi_sector  = req->sector;
+				bio->bi_iter.bi_sector  = sector_tmp;
   				bio_set_dev(bio, bdev);
   				bio->bi_private = req;
   				bio->bi_end_io  = vhost_blk_req_done;
@@ -314,7 +316,7 @@ static int vhost_blk_bio_make(struct vhost_blk_req *req,
   			iov_len		-= len;
 
   			pos = (iov_base & VHOST_BLK_SECTOR_MASK) + iov_len;
-			req->sector += pos >> VHOST_BLK_SECTOR_BITS;
+			sector_tmp += pos >> VHOST_BLK_SECTOR_BITS;
   		}
 
   		pages += pages_nr;

A message that is not in the archive (sent off-list or not yet received): CAHKanihHAjMF+OdozhRoq0ZMODRFZhT=Ze6b8q4S02Yp_cOwNg@mail.gmail.com

#16 lore raw

Re: [PATCH] Fix Issue: When VirtIO Backend providing VIRTIO_BLK_F_MQ feature, The file system of the front-end OS fails to be mounted.

Hi Lynch,

thank you very much for your reply and for willingness of making the global open source better. :)

From your replay i clearly understand that you have tried to increase the number of segments and that has boosted the performance and that it did not work properly before your fix.

But why this particular patch fixes the problem?

What was the exact root cause of the problem? (not high level "front-end OS did not boot", but on the low level - what caused the problem?)

As the patch effectively preserves the original req->sector of the request - how did it matter?

Is there a possibility that a single request is handled in parallel somewhere where req->sector is checked?

Or is the same request is handled sequentially and the second+ usages suffer from screwed req->sector value?

Can you please show the call traces?

Thank you!

--
Best regards,

Konstantin Khorenko,
Virtuozzo Linux Kernel Team

Quoted 109 lines · Lynch wu

Hi Konstantin:

  Thanks for your reply.
virtio_blk.c is the front-end driver code of the virtio block, which has the following code:
/* We need to know how many segments before we allocate. */
err = virtio_cread_feature(vdev, VIRTIO_BLK_F_SEG_MAX,
  struct virtio_blk_config, seg_max,
  &sg_elems);

/* We need at least one SG element, whatever they say. */ if (err || !sg_elems)
sg_elems = 1;

Eventually, the max number of segments will be configured through the blk_queue_max_segments interface. When I modify the value of the sg_elems due to performance reasons, such as 128, I will find that the front-end operating system cannot be started normally, which is manifested as the Android file system cannot be mounted normally.

You can do a simple test to modify the default value of sg_elems virtio_blk.c in your company's virtualization solution, as follows:
vzkernel$ git diff drivers/block/virtio_blk.c

drivers/block/virtio_blk.c +1 -1
index fd086179f980..0a216c9cb563 100644
@@ -719,7 +719,7 @@ static int virtblk_probe(struct virtio_device *vdev)
 
         /* We need at least one SG element, whatever they say. */
         if (err || !sg_elems)
-               sg_elems = 1;
+               sg_elems = 2;
 
         /* Prevent integer overflows and honor max vq size */
         sg_elems = min_t(u32, sg_elems, VIRTIO_BLK_MAX_SG_ELEMS - 2);

At this point, the expected test symptom is that the front-end OS does not boot properly.

In our virtualization solution, we used your company's blk.c as the back-end driver of Virtio Block, found the above problems, and made problem fixes, and at the same time, the performance of Virtio Block has also been improved, so we want to give back to the open source community.

Konstantin Khorenko <[email protected] <mailto:[email protected]>> 于2024年6月14日周五 19:23 写道:

    Hi Lynch, Andrey,

    thank you for the patch, but can you please describe the problem it fixes in a bit more details?
    i see that the patch preserves original req->sector, but why/how that becomes important in case
    VIRTIO_BLK_F_MQ feature is set?

    Thank you.

    --
    Best regards,

    Konstantin Khorenko,
    Virtuozzo Linux Kernel Team

    On 17.05.2024 11:09, Andrey Zhadchenko wrote:
     > Hi
     >
     > Thank you for the patch.
     > vhost-blk didn't spark enough interest to be reviewed and merged into
     > the upstream and the code is not present here.
     > I have forwarded your patch to relevant openvz kernel mailing list.
     >
     > On 5/17/24 07:34, Lynch wrote:
     >> ---
     >>    drivers/vhost/blk.c | 6 ++++--
     >>    1 file changed, 4 insertions(+), 2 deletions(-)
     >>
     >> diff --git a/drivers/vhost/blk.c b/drivers/vhost/blk.c
     >> index 44fbf253e773..0e946d9dfc33 100644
     >> --- a/drivers/vhost/blk.c
     >> +++ b/drivers/vhost/blk.c
     >> @@ -251,6 +251,7 @@ static int vhost_blk_bio_make(struct vhost_blk_req *req,
     >>      struct page **pages, *page;
     >>      struct bio *bio = NULL;
     >>      int bio_nr = 0;
     >> +    sector_t sector_tmp;
     >>
     >>      if (unlikely(req->bi_opf == REQ_OP_FLUSH))
     >>              return vhost_blk_bio_make_simple(req, bdev);
     >> @@ -270,6 +271,7 @@ static int vhost_blk_bio_make(struct vhost_blk_req *req,
     >>              req->bio = req->inline_bio;
     >>      }
     >>
     >> +    sector_tmp = req->sector;
     >>      req->iov_nr = 0;
     >>      for (i = 0; i < iov_nr; i++) {
     >>              int pages_nr = iov_num_pages(&iov[i]);
     >> @@ -302,7 +304,7 @@ static int vhost_blk_bio_make(struct vhost_blk_req *req,
     >>                              bio = bio_alloc(GFP_KERNEL, pages_nr_total);
     >>                              if (!bio)
     >>                                      goto fail;
     >> -                            bio->bi_iter.bi_sector  = req->sector;
     >> +                            bio->bi_iter.bi_sector  = sector_tmp;
     >>                              bio_set_dev(bio, bdev);
     >>                              bio->bi_private = req;
     >>                              bio->bi_end_io  = vhost_blk_req_done;
     >> @@ -314,7 +316,7 @@ static int vhost_blk_bio_make(struct vhost_blk_req *req,
     >>                      iov_len         -= len;
     >>
     >>                      pos = (iov_base & VHOST_BLK_SECTOR_MASK) + iov_len;
     >> -                    req->sector += pos >> VHOST_BLK_SECTOR_BITS;
     >> +                    sector_tmp += pos >> VHOST_BLK_SECTOR_BITS;
     >>              }
     >>
     >>              pages += pages_nr;