vhost-scsi: Support worker ioctls
v2 cover vhost-scsi: Support worker ioctls
The following patches allow users to configure the vhost worker threads for vhost-scsi. With vhost-net we get a worker thread per rx/tx virtqueue pair, but for vhost-scsi we get one worker for all workqueues. This becomes a bottlneck after 2 queues are used.
In the upstream linux kernel commit:
we enabled the vhost layer to be able to create a worker thread and attach it to a virtqueue.
This patchset adds support to vhost-scsi to use these ioctls so we are no longer limited to the single worker.
v2:
- Make config option a bool instead of an int.
PATCH v2 1/2 vhost: Add worker backend callouts
This adds the vhost backend callouts for the worker ioctls added in the 6.4 linux kernel commit:
c1ecd8e95007 ("vhost: allow userspace to create workers")
--- hw/virtio/vhost-backend.c | 28 ++++++++++++++++++++++++++++ include/hw/virtio/vhost-backend.h | 14 ++++++++++++++ 2 files changed, 42 insertions(+)
hw/virtio/vhost-backend.c +28 -0
@@ -158,6 +158,30 @@ static int vhost_kernel_set_vring_busyloop_timeout(struct vhost_dev *dev,return vhost_kernel_call (dev,VHOST_SET_VRING_BUSYLOOP_TIMEOUT , s); } +static int vhost_kernel_new_worker (struct vhost_dev *dev, +struct vhost_worker_state *worker) +{ +return vhost_kernel_call (dev,VHOST_NEW_WORKER , worker); +} + +static int vhost_kernel_free_worker (struct vhost_dev *dev, +struct vhost_worker_state *worker) +{ +return vhost_kernel_call (dev,VHOST_FREE_WORKER , worker); +} + +static int vhost_kernel_attach_vring_worker (struct vhost_dev *dev, +struct vhost_vring_worker *worker) +{ +return vhost_kernel_call (dev,VHOST_ATTACH_VRING_WORKER , worker); +} + +static int vhost_kernel_get_vring_worker (struct vhost_dev *dev, +struct vhost_vring_worker *worker) +{ +return vhost_kernel_call (dev,VHOST_GET_VRING_WORKER , worker); +} +static int vhost_kernel_set_features (struct vhost_dev *dev,uint64_t features) {
@@ -313,6 +337,10 @@ const VhostOps kernel_ops = { .vhost_set_vring_err = vhost_kernel_set_vring_err, .vhost_set_vring_busyloop_timeout = vhost_kernel_set_vring_busyloop_timeout, + .vhost_get_vring_worker = vhost_kernel_get_vring_worker, + .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_features = vhost_kernel_set_features, .vhost_get_features = vhost_kernel_get_features, .vhost_set_backend_cap = vhost_kernel_set_backend_cap,
include/hw/virtio/vhost-backend.h +14 -0
@@ -33,6 +33,8 @@ struct vhost_memory;struct vhost_vring_file ;struct vhost_vring_state ;struct vhost_vring_addr ; +struct vhost_vring_worker ; +struct vhost_worker_state ;struct vhost_scsi_target ;struct vhost_iotlb_msg ;struct vhost_virtqueue ;
@@ -73,6 +75,14 @@ typedef int (*vhost_set_vring_err_op)(struct vhost_dev *dev, struct vhost_vring_file *file);typedef int (*vhost_set_vring_busyloop_timeout_op )(struct vhost_dev *dev,struct vhost_vring_state *r); +typedef int (*vhost_attach_vring_worker_op )(struct vhost_dev *dev, +struct vhost_vring_worker *worker); +typedef int (*vhost_get_vring_worker_op )(struct vhost_dev *dev, +struct vhost_vring_worker *worker); +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_features_op )(struct vhost_dev *dev,uint64_t features); typedef int (*vhost_get_features_op)(struct vhost_dev *dev,
@@ -151,6 +161,10 @@ typedef struct VhostOps {vhost_set_vring_call_op vhost_set_vring_call;vhost_set_vring_err_op vhost_set_vring_err;vhost_set_vring_busyloop_timeout_op vhost_set_vring_busyloop_timeout; +vhost_new_worker_op vhost_new_worker; +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_features_op vhost_set_features;vhost_get_features_op vhost_get_features;vhost_set_backend_cap_op vhost_set_backend_cap;
Signature · 1 line
2.34.1
Re: [PATCH v2 1/2] vhost: Add worker backend callouts
Quoted 10 lines · Mike Christie
This adds the vhost backend callouts for the worker ioctls added in the 6.4 linux kernel commit:
c1ecd8e95007("vhost: allow userspace to create workers")Signed-off-by Mike Christie <[email protected]>---
hw/virtio/vhost-backend.c| 28 ++++++++++++++++++++++++++++include/hw/virtio/vhost-backend.h| 14 ++++++++++++++2 files changed, 42 insertions(+)
Quoted 89 lines
hw/virtio/vhost-backend.c +28
-0@@ -158,6 +158,30 @@ static int vhost_kernel_set_vring_busyloop_timeout(struct vhost_dev *dev,return vhost_kernel_call (dev,VHOST_SET_VRING_BUSYLOOP_TIMEOUT , s); } +static int vhost_kernel_new_worker (struct vhost_dev *dev, +struct vhost_worker_state *worker) +{ +return vhost_kernel_call (dev,VHOST_NEW_WORKER , worker); +} + +static int vhost_kernel_free_worker (struct vhost_dev *dev, +struct vhost_worker_state *worker) +{ +return vhost_kernel_call (dev,VHOST_FREE_WORKER , worker); +} + +static int vhost_kernel_attach_vring_worker (struct vhost_dev *dev, +struct vhost_vring_worker *worker) +{ +return vhost_kernel_call (dev,VHOST_ATTACH_VRING_WORKER , worker); +} + +static int vhost_kernel_get_vring_worker (struct vhost_dev *dev, +struct vhost_vring_worker *worker) +{ +return vhost_kernel_call (dev,VHOST_GET_VRING_WORKER , worker); +} +static int vhost_kernel_set_features (struct vhost_dev *dev,uint64_t features) {@@ -313,6 +337,10 @@ const VhostOps kernel_ops = { .vhost_set_vring_err = vhost_kernel_set_vring_err, .vhost_set_vring_busyloop_timeout = vhost_kernel_set_vring_busyloop_timeout, + .vhost_get_vring_worker = vhost_kernel_get_vring_worker, + .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_features = vhost_kernel_set_features, .vhost_get_features = vhost_kernel_get_features, .vhost_set_backend_cap = vhost_kernel_set_backend_cap,include/hw/virtio/vhost-backend.h +14
-0@@ -33,6 +33,8 @@ struct vhost_memory;struct vhost_vring_file ;struct vhost_vring_state ;struct vhost_vring_addr ; +struct vhost_vring_worker ; +struct vhost_worker_state ;struct vhost_scsi_target ;struct vhost_iotlb_msg ;struct vhost_virtqueue ;@@ -73,6 +75,14 @@ typedef int (*vhost_set_vring_err_op)(struct vhost_dev *dev, struct vhost_vring_file *file);typedef int (*vhost_set_vring_busyloop_timeout_op )(struct vhost_dev *dev,struct vhost_vring_state *r); +typedef int (*vhost_attach_vring_worker_op )(struct vhost_dev *dev, +struct vhost_vring_worker *worker); +typedef int (*vhost_get_vring_worker_op )(struct vhost_dev *dev, +struct vhost_vring_worker *worker); +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_features_op )(struct vhost_dev *dev,uint64_t features); typedef int (*vhost_get_features_op)(struct vhost_dev *dev,@@ -151,6 +161,10 @@ typedef struct VhostOps {vhost_set_vring_call_op vhost_set_vring_call;vhost_set_vring_err_op vhost_set_vring_err;vhost_set_vring_busyloop_timeout_op vhost_set_vring_busyloop_timeout; +vhost_new_worker_op vhost_new_worker; +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_features_op vhost_set_features;vhost_get_features_op vhost_get_features;vhost_set_backend_cap_op vhost_set_backend_cap;Signature · 1 line
2.34.1
PATCH v2 2/2 vhost-scsi: Add support for a worker thread per virtqueue
In reply to Mike ChristieThis adds support for vhost-scsi to be able to create a worker thread per virtqueue. Right now for vhost-net we get a worker thread per tx/rx virtqueue pair which scales nicely as we add more virtqueues and CPUs, but for scsi we get the single worker thread that's shared by all virtqueues. When trying to send IO to more than 2 virtqueues the single thread becomes a bottlneck.
This patch adds a new setting, workers_per_virtqueue, which can be set to:
false: Existing behavior where we get the single worker thread.
true: Create a worker per IO virtqueue.
--- hw/scsi/vhost-scsi.c | 60 +++++++++++++++++++++++++++++++++ include/hw/virtio/virtio-scsi.h | 1 + 2 files changed, 61 insertions(+)
hw/scsi/vhost-scsi.c +60 -0
@@ -165,6 +165,57 @@ static const VMStateDescription vmstate_virtio_vhost_scsi = { .pre_save = vhost_scsi_pre_save, }; +static int vhost_scsi_set_workers (VHostSCSICommon *vsc,bool per_virtqueue) +{ +struct vhost_dev *dev = &vsc->dev; +struct vhost_vring_worker vq_worker; +struct vhost_worker_state worker; +int i, ret; + +/* Use default worker */ +if (!per_virtqueue || dev->nvqs ==VHOST_SCSI_VQ_NUM_FIXED +1 ) { +return 0 ; + } + +/* +* ctl/evt share the first worker since it will be rare for them +* to send cmds while IO is running. +*/ +for (i =VHOST_SCSI_VQ_NUM_FIXED +1 ; i < dev->nvqs; i++) { +memset (&worker,0 ,sizeof (worker)); + + ret = dev->vhost_ops->vhost_new_worker (dev, &worker); +if (ret == -ENOTTY ) { +/* +* worker ioctls are not implemented so just ignore and +* and continue device setup. +*/ + ret =0 ; +break ; + }else if (ret) { +break ; + } + +memset (&vq_worker,0 ,sizeof (vq_worker)); + vq_worker.worker_id = worker.worker_id; + vq_worker.index = i; + + ret = dev->vhost_ops->vhost_attach_vring_worker (dev, &vq_worker); +if (ret == -ENOTTY ) { +/* +* It's a bug for the kernel to have supported the worker creation +* ioctl but not attach. +*/ + dev->vhost_ops->vhost_free_worker (dev, &worker); +break ; + }else if (ret) { +break ; + } + } + +return ret; +} +static void vhost_scsi_realize (DeviceState *dev,Error **errp) {VirtIOSCSICommon *vs =VIRTIO_SCSI_COMMON (dev);
@@ -232,6 +283,13 @@ static void vhost_scsi_realize(DeviceState *dev, Error **errp) goto free_vqs; } + ret =vhost_scsi_set_workers (vsc, vs->conf.worker_per_virtqueue); +if (ret <0 ) { +error_setg (errp,"vhost-scsi: vhost worker setup failed: %s" , +strerror (-ret)); + goto free_vqs; + } +/* At present, channel and lun both are 0 for bootable vhost-scsi disk */ vsc->channel =0 ; vsc->lun =0 ;
@@ -297,6 +355,8 @@ static Property vhost_scsi_properties[] = { VIRTIO_SCSI_F_T10_PI, false),DEFINE_PROP_BOOL ("migratable" , VHostSCSICommon, migratable, false), +DEFINE_PROP_BOOL ("worker_per_virtqueue" , VirtIOSCSICommon, + conf.worker_per_virtqueue, false), DEFINE_PROP_END_OF_LIST(), };
include/hw/virtio/virtio-scsi.h +1 -0
@@ -51,6 +51,7 @@ typedef struct virtio_scsi_config VirtIOSCSIConfig;struct VirtIOSCSIConf {uint32_t num_queues;uint32_t virtqueue_size; +bool worker_per_virtqueue;bool seg_max_adjust;uint32_t max_sectors;uint32_t cmd_per_lun;
Signature · 1 line
2.34.1
Re: [PATCH v2 2/2] vhost-scsi: Add support for a worker thread per virtqueue
Quoted 42 lines · Mike Christie
This adds support for vhost-scsi to be able to create a worker thread per virtqueue. Right now for vhost-net we get a worker thread per tx/rx virtqueue pair which scales nicely as we add more virtqueues and CPUs, but for scsi we get the single worker thread that's shared by all virtqueues. When trying to send IO to more than 2 virtqueues the single thread becomes a bottlneck.
This patch adds a new setting,
workers_per_virtqueue, which can be set to:false: Existing behavior where we get the single worker thread.
true: Create a worker per IO virtqueue.Signed-off-by Mike Christie <[email protected]>---
hw/scsi/vhost-scsi.c| 60 +++++++++++++++++++++++++++++++++include/hw/virtio/virtio-scsi.h| 1 +2 files changed, 61 insertions(+)hw/scsi/vhost-scsi.c +15
-0@@ -165,6 +165,57 @@ static const VMStateDescription vmstate_virtio_vhost_scsi = { .pre_save = vhost_scsi_pre_save, }; +static int vhost_scsi_set_workers (VHostSCSICommon *vsc,bool per_virtqueue) +{ +struct vhost_dev *dev = &vsc->dev; +struct vhost_vring_worker vq_worker; +struct vhost_worker_state worker; +int i, ret; + +/* Use default worker */ +if (!per_virtqueue || dev->nvqs ==VHOST_SCSI_VQ_NUM_FIXED +1 ) { +return 0 ; + } + +/* +* ctl/evt share the first worker since it will be rare for them +* to send cmds while IO is running.
hw/scsi/vhost-scsi.c static const VMStateDescription vmstate_virtio_vhost_scsi = { · line 192
Quoted 10 lines
hw/scsi/vhost-scsi.c +10
-0+*/ +for (i =VHOST_SCSI_VQ_NUM_FIXED +1 ; i < dev->nvqs; i++) { +memset (&worker,0 ,sizeof (worker)); + + ret = dev->vhost_ops->vhost_new_worker (dev, &worker); +if (ret == -ENOTTY ) { +/* +* worker ioctls are not implemented so just ignore and +* and continue device setup. +*/
IIUC here the user has asked to use a worker for each virtqueue, but the kernel does not support it so we ignore it.
Should we at least print a warning?
The rest LGTM!
Stefano
Quoted 67 lines
hw/scsi/vhost-scsi.c +35
-0+ ret =0 ; +break ; + }else if (ret) { +break ; + } + +memset (&vq_worker,0 ,sizeof (vq_worker)); + vq_worker.worker_id = worker.worker_id; + vq_worker.index = i; + + ret = dev->vhost_ops->vhost_attach_vring_worker (dev, &vq_worker); +if (ret == -ENOTTY ) { +/* +* It's a bug for the kernel to have supported the worker creation +* ioctl but not attach. +*/ + dev->vhost_ops->vhost_free_worker (dev, &worker); +break ; + }else if (ret) { +break ; + } + } + +return ret; +} +static void vhost_scsi_realize (DeviceState *dev,Error **errp) {VirtIOSCSICommon *vs =VIRTIO_SCSI_COMMON (dev);@@ -232,6 +283,13 @@ static void vhost_scsi_realize(DeviceState *dev, Error **errp) goto free_vqs; } + ret =vhost_scsi_set_workers (vsc, vs->conf.worker_per_virtqueue); +if (ret <0 ) { +error_setg (errp,"vhost-scsi: vhost worker setup failed: %s" , +strerror (-ret)); + goto free_vqs; + } +/* At present, channel and lun both are 0 for bootable vhost-scsi disk */ vsc->channel =0 ; vsc->lun =0 ;@@ -297,6 +355,8 @@ static Property vhost_scsi_properties[] = { VIRTIO_SCSI_F_T10_PI, false),DEFINE_PROP_BOOL ("migratable" , VHostSCSICommon, migratable, false), +DEFINE_PROP_BOOL ("worker_per_virtqueue" , VirtIOSCSICommon, + conf.worker_per_virtqueue, false), DEFINE_PROP_END_OF_LIST(), };include/hw/virtio/virtio-scsi.h +1
-0@@ -51,6 +51,7 @@ typedef struct virtio_scsi_config VirtIOSCSIConfig;struct VirtIOSCSIConf {uint32_t num_queues;uint32_t virtqueue_size; +bool worker_per_virtqueue;bool seg_max_adjust;uint32_t max_sectors;uint32_t cmd_per_lun;Signature · 1 line
2.34.1
Re: [PATCH v2 2/2] vhost-scsi: Add support for a worker thread per virtqueue
Quoted 57 lines · Stefano Garzarella
Quoted 42 lines · Mike Christie
This adds support for vhost-scsi to be able to create a worker thread per virtqueue. Right now for vhost-net we get a worker thread per tx/rx virtqueue pair which scales nicely as we add more virtqueues and CPUs, but for scsi we get the single worker thread that's shared by all virtqueues. When trying to send IO to more than 2 virtqueues the single thread becomes a bottlneck.
This patch adds a new setting,
workers_per_virtqueue, which can be set to:false: Existing behavior where we get the single worker thread.
true: Create a worker per IO virtqueue.Signed-off-by Mike Christie <[email protected]>---
hw/scsi/vhost-scsi.c | 60 +++++++++++++++++++++++++++++++++include/hw/virtio/virtio-scsi.h| 1 +2 files changed, 61 insertions(+)hw/scsi/vhost-scsi.c +15
-0@@ -165,6 +165,57 @@ static const VMStateDescription vmstate_virtio_vhost_scsi = { .pre_save = vhost_scsi_pre_save, }; +static int vhost_scsi_set_workers (VHostSCSICommon *vsc,bool per_virtqueue) +{ + struct vhost_dev *dev = &vsc->dev; + struct vhost_vring_worker vq_worker; + struct vhost_worker_state worker; + int i, ret; + + /* Use default worker */ + if (!per_virtqueue || dev->nvqs == VHOST_SCSI_VQ_NUM_FIXED + 1) { + return 0; + } + + /* +* ctl/evt share the first worker since it will be rare for them +* to send cmds while IO is running. hw/scsi/vhost-scsi.c static const VMStateDescription vmstate_virtio_vhost_scsi = { · line 192
Quoted 10 lines
hw/scsi/vhost-scsi.c +10
-0+*/ + for (i = VHOST_SCSI_VQ_NUM_FIXED + 1; i < dev->nvqs; i++) { + memset(&worker, 0, sizeof(worker)); + + ret = dev->vhost_ops->vhost_new_worker(dev, &worker); + if (ret == -ENOTTY) { + /* +* worker ioctls are not implemented so just ignore and +* and continue device setup. +*/ IIUC here the user has asked to use a worker for each virtqueue, but the kernel does not support it so we ignore it.
Quoted 2 lines
Should we at least print a warning?
We should. I'll add it.
Quoted 9 lines · Mike Christie
The following patches allow users to configure the vhost worker threads for vhost-scsi. With vhost-net we get a worker thread per rx/tx virtqueue pair, but for vhost-scsi we get one worker for all workqueues. This becomes a bottlneck after 2 queues are used.
In the upstream linux kernel commit:
Quoted 10 lines
we enabled the vhost layer to be able to create a worker thread and attach it to a virtqueue.
This patchset adds support to vhost-scsi to use these ioctls so we are no longer limited to the single worker.
v2:
- Make config option a bool instead of an int.
Aside from Stefano's comment asking for a warning when the kernel doesn't support the vhost worker ioctl: