vhost: multiple worker support
Subsystems (4)
Mike Christie
maintainercover vhost: multiple worker support
The following patches apply over mst's vhost branch and were tested againt that branch and also mkp's 5.13 branch which has some vhost-scsi changes.
These patches allow us to support multiple vhost workers per device. I ended up just doing Stefan's original idea where userspace has the kernel create a worker and we pass back the pid. This has the benefit over the workqueue and userspace thread approach where we only have one'ish code path in the kernel.
The kernel patches here allow us to then do N workers device and also share workers across devices.
I included a patch for qemu so you can get an idea of how it works.
TODO:
-----
- polling
- Allow sharing workers across devices. Kernel support is added and I hacked up userspace to test, but I'm still working on a sane way to manage it in userspace.
- Bind to specific CPUs. Commands like "virsh emulatorpin" work with these patches and allow us to set the group of vhost threads to different CPUs. But we can also set a specific vq's worker to run on a CPU.
- I'm handling old kernel by just checking for EPERM. Does this require a feature?
Results:
--------
When running with the null_blk driver and vhost-scsi I can get 1.2 million IOPs by just running a simple
fio --filename=/dev/sda --direct=1 --rw=randrw --bs=4k --ioengine=libaio --iodepth=128 --numjobs=8 --time_based --group_reporting --name=iops --runtime=60 --eta-newline=1
The VM has 8 vCPUs and sda has 8 virtqueues and we can do a total of 1024 cmds per devices. To get 1.2 million IOPs I did have to tune and ran the virsh emulatorpin command so the vhost threads were running on different CPUs than the VM. If the vhost threads share CPUs then I get around 800K.
For a more real device that are also CPU hogs like iscsi, I can still get 1 million IOPs using 1 dm-multipath device over 8 iscsi paths (natively it gets 1.1 million IOPs).
_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization
Mike Christie
maintainerRFC PATCH 1/14 vhost: remove work arg from vhost_work_flush
vhost_work_flush doesn't do anything with the work arg. This patch drops it and then renames vhost_work_flush to vhost_work_dev_flush to reflect that the function flushes all the works in the dev and not just a specific queue or work item.
--- drivers/vhost/scsi.c | 4 ++-- drivers/vhost/vhost.c | 8 ++++---- drivers/vhost/vhost.h | 2 +- drivers/vhost/vsock.c | 2 +- 4 files changed, 8 insertions(+), 8 deletions(-)
drivers/vhost/scsi.c +2 -2
@@ -1455,8 +1455,8 @@ static void vhost_scsi_flush(struct vhost_scsi *vs)/* Flush both the vhost poll and vhost work */ for (i =0 ; i <VHOST_SCSI_MAX_VQ ; i++)vhost_scsi_flush_vq (vs, i); -vhost_work_flush (&vs->dev, &vs->vs_completion_work); -vhost_work_flush (&vs->dev, &vs->vs_event_work); +vhost_work_dev_flush (&vs->dev); +vhost_work_dev_flush (&vs->dev);/* Wait for all reqs issued before the flush to be finished */ for (i =0 ; i <VHOST_SCSI_MAX_VQ ; i++)
drivers/vhost/vhost.c +4 -4
@@ -231,7 +231,7 @@ void vhost_poll_stop(struct vhost_poll *poll) }EXPORT_SYMBOL_GPL (vhost_poll_stop); -void vhost_work_flush (struct vhost_dev *dev,struct vhost_work *work) +void vhost_work_dev_flush (struct vhost_dev *dev) {struct vhost_flush_struct flush;
@@ -243,13 +243,13 @@ void vhost_work_flush(struct vhost_dev *dev, struct vhost_work *work)wait_for_completion (&flush.wait_event); } } -EXPORT_SYMBOL_GPL (vhost_work_flush); +EXPORT_SYMBOL_GPL (vhost_work_dev_flush);/* Flush any work that has been scheduled. When calling this, don't hold any * locks that are also used by the callback. */ void vhost_poll_flush (struct vhost_poll *poll) { -vhost_work_flush (poll->dev, &poll->work); +vhost_work_dev_flush (poll->dev); }EXPORT_SYMBOL_GPL (vhost_poll_flush);
@@ -538,7 +538,7 @@ static int vhost_attach_cgroups(struct vhost_dev *dev) attach.owner = current;vhost_work_init (&attach.work, vhost_attach_cgroups_work);vhost_work_queue (dev, &attach.work); -vhost_work_flush (dev, &attach.work); +vhost_work_dev_flush (dev);return attach.ret; }
drivers/vhost/vhost.h +1 -1
@@ -46,7 +46,7 @@ int vhost_poll_start(struct vhost_poll *poll, struct file *file);void vhost_poll_stop (struct vhost_poll *poll);void vhost_poll_flush (struct vhost_poll *poll);void vhost_poll_queue (struct vhost_poll *poll); -void vhost_work_flush (struct vhost_dev *dev,struct vhost_work *work); +void vhost_work_dev_flush (struct vhost_dev *dev);long vhost_vring_ioctl (struct vhost_dev *d,unsigned int ioctl,void __user *argp);struct vhost_log {
drivers/vhost/vsock.c +1 -1
@@ -663,7 +663,7 @@ static void vhost_vsock_flush(struct vhost_vsock *vsock)for (i =0 ; i <ARRAY_SIZE (vsock->vqs); i++)if (vsock->vqs[i].handle_kick)vhost_poll_flush (&vsock->vqs[i].poll); -vhost_work_flush (&vsock->dev, &vsock->send_pkt_work); +vhost_work_dev_flush (&vsock->dev); }static void vhost_vsock_reset_orphans (struct sock *sk)
Signature · 6 lines
2.25.1 _______________________________________________ Virtualization mailing list [email protected] https://lists.linuxfoundation.org/mailman/listinfo/virtualization
Mike Christie
maintainerRFC PATCH 1/1 QEMU vhost-scsi: add support for VHOST_SET_VRING_WORKER
In reply to Mike ChristieThis patch adds support for the proposed ioctl that allows userspace to create virtqueue workers. For vhost-scsi you can set virtqueue_workers to:
0: default behavior where we have 1 worker for all vqs.
-1: create a worker per vq
Quoted 1 line
0: create N workers and allow the vqs to share them by assigning a
vq a worker by just doing round robin.
TODO:
- Allow sharing workers across devices.
- Bind to specific CPUs. Commands like "virsh emulatorpin" allow us to set the group of vhost threads to different CPUs. But we can also set a specific vq's worker to run on a CPU.
- I'm handling old kernel by just checking for EPERM. Does this require a feature?
--- hw/scsi/vhost-scsi.c | 85 +++++++++++++++++++- hw/virtio/vhost-backend.c | 8 ++ include/hw/virtio/vhost-backend.h | 4 + include/hw/virtio/virtio-scsi.h | 1 + include/standard-headers/linux/vhost_types.h | 9 +++ linux-headers/linux/vhost.h | 7 ++ 6 files changed, 111 insertions(+), 3 deletions(-)
hw/scsi/vhost-scsi.c +82 -3
@@ -163,6 +163,76 @@ static const VMStateDescription vmstate_virtio_vhost_scsi = { .pre_save = vhost_scsi_pre_save, }; +static int vhost_scsi_set_workers (VHostSCSICommon *vsc,int vq_workers) +{ +struct vhost_dev *dev = &vsc->dev; +int worker_index =0 , num_workers =0 ; +struct vhost_vring_worker w; +pid_t *workers =NULL ; +int i, ret; + +if (vq_workers <-1 ) +return -EINVAL ; + +if (vq_workers >0 ) { +if (vq_workers > dev->nvqs) + vq_workers = dev->nvqs; + + workers =g_malloc0 (vq_workers *sizeof (pid_t)); + } + + w.pid =-1 ; +for (i =0 ; i < dev->nvqs; i++) { + w.index = i; + +switch (vq_workers) { +case -1 : +/* +* ctl/evt share the first worker since it will be rare for them +* to send cmds while IO is running. The rest of the vqs get their +* own worker. +*/ +if (i >VHOST_SCSI_VQ_NUM_FIXED ) + w.pid =-1 ; +break ; +case 0 : +/* All vqs share 1 worker. Pass back the pid we got the first run */ +break ; +default : +/* Each worker handles N vqs. */ +if (num_workers == vq_workers) { + w.pid = workers[worker_index]; + + worker_index++; +if (worker_index == vq_workers) + worker_index =0 ; + }else { + w.pid =-1 ; + } + +break ; + } + + ret = dev->vhost_ops->vhost_set_vring_worker (dev, &w); +/* Ignore for now. Add feature in final patch */ +if (ret == -EPERM ) { + ret =0 ; + goto free_workers; + }else if (ret) + goto free_workers; + +if (vq_workers >0 && num_workers < vq_workers) { + workers[num_workers] = w.pid; + num_workers++; + } + } + +free_workers: +if (workers) +g_free (workers); +return ret; +} +static void vhost_scsi_realize (DeviceState *dev,Error **errp) {VirtIOSCSICommon *vs =VIRTIO_SCSI_COMMON (dev);
@@ -226,6 +296,13 @@ static void vhost_scsi_realize(DeviceState *dev, Error **errp) goto free_vqs; } + ret =vhost_scsi_set_workers (vsc, vs->conf.virtqueue_workers); +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 ;
@@ -271,18 +348,20 @@ static Property vhost_scsi_properties[] = {DEFINE_PROP_STRING ("wwpn" , VirtIOSCSICommon, conf.wwpn),DEFINE_PROP_UINT32 ("boot_tpgt" , VirtIOSCSICommon, conf.boot_tpgt,0 ),DEFINE_PROP_UINT32 ("num_queues" , VirtIOSCSICommon, conf.num_queues, -VIRTIO_SCSI_AUTO_NUM_QUEUES ), +8 ),DEFINE_PROP_UINT32 ("virtqueue_size" , VirtIOSCSICommon, conf.virtqueue_size, -128 ), +1024 ),DEFINE_PROP_BOOL ("seg_max_adjust" , VirtIOSCSICommon, conf.seg_max_adjust, true),DEFINE_PROP_UINT32 ("max_sectors" , VirtIOSCSICommon, conf.max_sectors,0xFFFF ), -DEFINE_PROP_UINT32 ("cmd_per_lun" , VirtIOSCSICommon, conf.cmd_per_lun,128 ), +DEFINE_PROP_UINT32 ("cmd_per_lun" , VirtIOSCSICommon, conf.cmd_per_lun,1024 ),DEFINE_PROP_BIT64 ("t10_pi" , VHostSCSICommon, host_features,VIRTIO_SCSI_F_T10_PI , false),DEFINE_PROP_BOOL ("migratable" , VHostSCSICommon, migratable, false), +DEFINE_PROP_INT32 ("virtqueue_workers" , VirtIOSCSICommon, + conf.virtqueue_workers,0 ), DEFINE_PROP_END_OF_LIST(), };
hw/virtio/vhost-backend.c +8 -0
@@ -150,6 +150,13 @@ 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_set_vring_worker (struct vhost_dev *dev, +struct vhost_vring_worker *worker) +{ +return vhost_kernel_call (dev,VHOST_SET_VRING_WORKER , worker); +} + +static int vhost_kernel_set_features (struct vhost_dev *dev,uint64_t features) {
@@ -311,6 +318,7 @@ static const VhostOps kernel_ops = { .vhost_set_vring_call = vhost_kernel_set_vring_call, .vhost_set_vring_busyloop_timeout = vhost_kernel_set_vring_busyloop_timeout, + .vhost_set_vring_worker = vhost_kernel_set_vring_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 +4 -0
@@ -33,6 +33,7 @@ struct vhost_memory;struct vhost_vring_file ;struct vhost_vring_state ;struct vhost_vring_addr ; +struct vhost_vring_worker ;struct vhost_scsi_target ;struct vhost_iotlb_msg ;struct vhost_virtqueue ;
@@ -70,6 +71,8 @@ typedef int (*vhost_set_vring_call_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_set_vring_worker_op )(struct vhost_dev *dev, +struct vhost_vring_worker *worker);typedef int (*vhost_set_features_op )(struct vhost_dev *dev,uint64_t features); typedef int (*vhost_get_features_op)(struct vhost_dev *dev,
@@ -145,6 +148,7 @@ typedef struct VhostOps {vhost_set_vring_kick_op vhost_set_vring_kick;vhost_set_vring_call_op vhost_set_vring_call;vhost_set_vring_busyloop_timeout_op vhost_set_vring_busyloop_timeout; +vhost_set_vring_worker_op vhost_set_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;
include/hw/virtio/virtio-scsi.h +1 -0
@@ -58,6 +58,7 @@ struct VirtIOSCSIConf {#ifdef CONFIG_VHOST_SCSI char *vhostfd;char *wwpn; +int virtqueue_workers;#endif CharBackend chardev;uint32_t boot_tpgt;
include/standard-headers/linux/vhost_types.h +9 -0
@@ -47,6 +47,15 @@ struct vhost_vring_addr {uint64_t log_guest_addr; }; +struct vhost_vring_worker { +unsigned int index; +/* +* The pid of the vhost worker that the vq will be bound to. If -1, +* a new worker will be created and it's pid will be returned in pid. +*/ +pid_t pid; +}; +/* no alignment requirement */ struct vhost_iotlb_msg {uint64_t iova;
linux-headers/linux/vhost.h +7 -0
@@ -70,6 +70,13 @@#define VHOST_VRING_BIG_ENDIAN 1#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) +/* Create/bind a vhost worker to a virtqueue. If pid > 0 and matches an existing +* vhost_worker thread it will be bound to the vq. If pid is -1, then a new +* worker will be created and bound to the vq. +*/ +#define VHOST_SET_VRING_WORKER _IOWR(VHOST_VIRTIO, 0x15, struct vhost_vring_worker) +/* Return the vqs worker's pid. If no worker is set pid is -1 */ +#define VHOST_GET_VRING_WORKER _IOR(VHOST_VIRTIO, 0x16, struct vhost_vring_worker)/* The following ioctls use eventfd file descriptors to signal and poll * for events. */
Signature · 6 lines
2.25.1 _______________________________________________ Virtualization mailing list [email protected] https://lists.linuxfoundation.org/mailman/listinfo/virtualization
Mike Christie
maintainerRFC PATCH 2/14 vhost-scsi: remove extra flushes
In reply to Mike ChristieThe vhost work flush function was flushing the entire work queue, so there is no need for the double vhost_work_dev_flush calls in vhost_scsi_flush.
And we do not need to call vhost_poll_flush for each poller because that call also ends up flushing the same work queue thread the vhost_work_dev_flush call flushed.
--- drivers/vhost/scsi.c | 8 -------- 1 file changed, 8 deletions(-)
drivers/vhost/scsi.c +0 -8
@@ -1430,11 +1430,6 @@ static void vhost_scsi_handle_kick(struct vhost_work *work)vhost_scsi_handle_vq (vs, vq); } -static void vhost_scsi_flush_vq (struct vhost_scsi *vs,int index) -{ -vhost_poll_flush (&vs->vqs[index].vq.poll); -} -/* Callers must hold dev mutex */ static void vhost_scsi_flush (struct vhost_scsi *vs) {
@@ -1453,9 +1448,6 @@ static void vhost_scsi_flush(struct vhost_scsi *vs)kref_put (&old_inflight[i]->kref, vhost_scsi_done_inflight);/* Flush both the vhost poll and vhost work */ -for (i =0 ; i <VHOST_SCSI_MAX_VQ ; i++) -vhost_scsi_flush_vq (vs, i); -vhost_work_dev_flush (&vs->dev);vhost_work_dev_flush (&vs->dev);/* Wait for all reqs issued before the flush to be finished */
Signature · 6 lines
2.25.1 _______________________________________________ Virtualization mailing list [email protected] https://lists.linuxfoundation.org/mailman/listinfo/virtualization
Mike Christie
maintainerRFC PATCH 3/14 vhost-scsi: reduce flushes during endpoint clearing
In reply to Mike Christievhost_scsi_flush will flush everything, so we can clear the backends then flush, then destroy. We don't need to flush before each vq destruction because after the flush we will have made sure there can be no new cmds started and there are no running cmds.
--- drivers/vhost/scsi.c | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-)
drivers/vhost/scsi.c +6 -5
@@ -1732,11 +1732,12 @@ vhost_scsi_clear_endpoint(struct vhost_scsi *vs,mutex_lock (&vq->mutex);vhost_vq_set_backend (vq,NULL );mutex_unlock (&vq->mutex); -/* -* Make sure cmds are not running before tearing them -* down. -*/ -vhost_scsi_flush (vs); + } +/* Make sure cmds are not running before tearing them down. */ +vhost_scsi_flush (vs); + +for (i =0 ; i <VHOST_SCSI_MAX_VQ ; i++) { + vq = &vs->vqs[i].vq;vhost_scsi_destroy_vq_cmds (vq); } }
Signature · 6 lines
2.25.1 _______________________________________________ Virtualization mailing list [email protected] https://lists.linuxfoundation.org/mailman/listinfo/virtualization
Mike Christie
maintainerRFC PATCH 4/14 vhost: fix poll coding style
In reply to Mike ChristieWe use like 3 coding styles in this struct. Switch to just tabs.
--- drivers/vhost/vhost.h | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-)
drivers/vhost/vhost.h +6 -6
@@ -28,12 +28,12 @@ struct vhost_work {/* Poll a file (eventfd or socket) */ /* Note: there's nothing vhost specific about this structure. */ struct vhost_poll { -poll_table table; -wait_queue_head_t *wqh; -wait_queue_entry_t wait; -struct vhost_work work; -__poll_t mask; -struct vhost_dev *dev; +poll_table table; +wait_queue_head_t *wqh; +wait_queue_entry_t wait; +struct vhost_work work; +__poll_t mask; +struct vhost_dev *dev; };void vhost_work_init (struct vhost_work *work,vhost_work_fn_t fn);
Signature · 6 lines
2.25.1 _______________________________________________ Virtualization mailing list [email protected] https://lists.linuxfoundation.org/mailman/listinfo/virtualization
Mike Christie
maintainerRFC PATCH 5/14 vhost: move worker thread fields to new struct
In reply to Mike ChristieThis is just a prep patch. It moves the worker related fields to a new vhost_worker struct and moves the code around to create some helpers that will be used in the next patches.
--- drivers/vhost/vhost.c | 94 +++++++++++++++++++++++++++++-------------- drivers/vhost/vhost.h | 9 ++++- 2 files changed, 70 insertions(+), 33 deletions(-)
drivers/vhost/vhost.c +63 -31
@@ -263,8 +263,8 @@ void vhost_work_queue(struct vhost_dev *dev, struct vhost_work *work)* 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); +llist_add (&work->node, &dev->worker->work_list); +wake_up_process (dev->worker->task); } }EXPORT_SYMBOL_GPL (vhost_work_queue);
@@ -272,7 +272,7 @@ EXPORT_SYMBOL_GPL(vhost_work_queue);/* A lockless hint for busy polling code to exit the loop */ bool vhost_has_work (struct vhost_dev *dev) { -return !llist_empty (&dev->work_list); +return dev->worker && !llist_empty (&dev->worker->work_list); }EXPORT_SYMBOL_GPL (vhost_has_work);
@@ -343,7 +343,8 @@ static void vhost_vq_reset(struct vhost_dev *dev,static int vhost_worker (void *data) { -struct vhost_dev *dev = data; +struct vhost_worker *worker = data; +struct vhost_dev *dev = worker->dev;struct vhost_work *work, *work_next;struct llist_node *node;
@@ -358,7 +359,7 @@ static int vhost_worker(void *data)break ; } - node =llist_del_all (&dev->work_list); + node =llist_del_all (&worker->work_list);if (!node)schedule ();
@@ -487,7 +488,6 @@ void vhost_dev_init(struct vhost_dev *dev, dev->byte_weight = byte_weight; dev->use_worker = use_worker; dev->msg_handler = msg_handler; -init_llist_head (&dev->work_list);init_waitqueue_head (&dev->wait);INIT_LIST_HEAD (&dev->read_list);INIT_LIST_HEAD (&dev->pending_list);
@@ -579,10 +579,59 @@ static void vhost_detach_mm(struct vhost_dev *dev) dev->mm =NULL ; } +static void vhost_worker_free (struct vhost_dev *dev) +{ +struct vhost_worker *worker = dev->worker; + +if (!worker) +return ; + + dev->worker =NULL ; +WARN_ON (!llist_empty (&worker->work_list)); +kthread_stop (worker->task); +kfree (worker); +} + +static int vhost_worker_create (struct vhost_dev *dev) +{ +struct vhost_worker *worker; +struct task_struct *task; +int ret; + + worker =kzalloc (sizeof (*worker),GFP_KERNEL ); +if (!worker) +return -ENOMEM ; + + dev->worker = worker; + worker->dev = dev; +init_llist_head (&worker->work_list); + + task =kthread_create (vhost_worker, worker,"vhost-%d" , current->pid); +if (IS_ERR (task)) { + ret =PTR_ERR (task); + goto free_worker; + } + + worker->task = task; +wake_up_process (task);/* avoid contributing to loadavg */ + + ret =vhost_attach_cgroups (dev); +if (ret) + goto stop_worker; + +return 0 ; + +stop_worker: +kthread_stop (worker->task); +free_worker: +kfree (worker); + dev->worker =NULL ; +return ret; +} +/* Caller should have device mutex */ long vhost_dev_set_owner (struct vhost_dev *dev) { -struct task_struct *worker;int err;/* Is there an owner already? */
@@ -595,31 +644,18 @@ long vhost_dev_set_owner(struct vhost_dev *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->worker = worker; -wake_up_process (worker);/* avoid contributing to loadavg */ - - err =vhost_attach_cgroups (dev); + err =vhost_worker_create (dev);if (err) - goto err_cgroup; + goto err_worker; } err =vhost_dev_alloc_iovecs (dev);if (err) - goto err_cgroup; + goto err_iovecs;return 0 ; -err_cgroup: -if (dev->worker) { -kthread_stop (dev->worker); - dev->worker =NULL ; - } +err_iovecs: +vhost_worker_free (dev); err_worker:vhost_detach_mm (dev); dev->kcov_handle =0 ;
@@ -712,13 +748,9 @@ 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 ; - dev->kcov_handle =0 ; - } +vhost_worker_free (dev);vhost_detach_mm (dev); + dev->kcov_handle =0 ; }EXPORT_SYMBOL_GPL (vhost_dev_cleanup);
drivers/vhost/vhost.h +7 -2
@@ -25,6 +25,12 @@ struct vhost_work {unsigned long flags; }; +struct vhost_worker { +struct task_struct *task; +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 {
@@ -149,8 +155,7 @@ struct vhost_dev {struct vhost_virtqueue **vqs;int nvqs;struct eventfd_ctx *log_ctx; -struct llist_head work_list; -struct task_struct *worker; +struct vhost_worker *worker;struct vhost_iotlb *umem;struct vhost_iotlb *iotlb;spinlock_t iotlb_lock;
Signature · 6 lines
2.25.1 _______________________________________________ Virtualization mailing list [email protected] https://lists.linuxfoundation.org/mailman/listinfo/virtualization
Mike Christie
maintainerRFC PATCH 6/14 vhost: move vhost worker creation to kick setup
In reply to Mike ChristieThe next patch will add new ioctls that allows userspace to create workers and bind them to devs and vqs after VHOST_SET_OWNER. To support older tools, newer tools that want to go wild with worker threads, and newer tools that want the old/default behavior this patch moves the default worker setup to the kick setup.
After the first vq's kick/poll setup is done we could start to get works queued, so this is the point when we must have a worker setup. If we are using older tools or the newer tools just want the default single vhost thread per dev behavior then it will automatically be done here. If the tools are using the newer ioctls that have already created the workers then we also detect that here and do nothing.
--- drivers/vhost/vhost.c | 23 ++++++++++++++++------- 1 file changed, 16 insertions(+), 7 deletions(-)
drivers/vhost/vhost.c +16 -7
@@ -629,6 +629,15 @@ static int vhost_worker_create(struct vhost_dev *dev)return ret; } +/* Caller must have device mutex */ +static int vhost_worker_try_create_def (struct vhost_dev *dev) +{ +if (!dev->use_worker || dev->worker) +return 0 ; + +return vhost_worker_create (dev); +} +/* Caller should have device mutex */ long vhost_dev_set_owner (struct vhost_dev *dev) {
@@ -643,11 +652,6 @@ long vhost_dev_set_owner(struct vhost_dev *dev)vhost_attach_mm (dev); dev->kcov_handle =kcov_common_handle (); -if (dev->use_worker) { - err =vhost_worker_create (dev); -if (err) - goto err_worker; - } err =vhost_dev_alloc_iovecs (dev);if (err)
@@ -655,8 +659,6 @@ long vhost_dev_set_owner(struct vhost_dev *dev)return 0 ; err_iovecs: -vhost_worker_free (dev); -err_worker:vhost_detach_mm (dev); dev->kcov_handle =0 ; err_mm:
@@ -1665,6 +1667,13 @@ long vhost_vring_ioctl(struct vhost_dev *d, unsigned int ioctl, void __user *arg r = -EFAULT ;break ; } + +if (f.fd !=VHOST_FILE_UNBIND ) { + r =vhost_worker_try_create_def (d); +if (r) +break ; + } + eventfp = f.fd ==VHOST_FILE_UNBIND ?NULL :eventfd_fget (f.fd);if (IS_ERR (eventfp)) { r =PTR_ERR (eventfp);
Signature · 6 lines
2.25.1 _______________________________________________ Virtualization mailing list [email protected] https://lists.linuxfoundation.org/mailman/listinfo/virtualization
Mike Christie
maintainerRFC PATCH 7/14 vhost: modify internal functions to take a vhost_worker
In reply to Mike ChristieThe final patches will allow us to have multiple vhost_workers per device and be able to share them across devices. To prepare for that, this patch allow us our internal work queueing, flush and cgroup attach functions to take a vhost_worker as an arg.
The poll code required a change to the driver, so the next patch will convert that code.
--- drivers/vhost/vhost.c | 136 ++++++++++++++++++++++++++++-------------- drivers/vhost/vhost.h | 4 +- 2 files changed, 94 insertions(+), 46 deletions(-)
drivers/vhost/vhost.c +91 -45
@@ -231,20 +231,6 @@ void vhost_poll_stop(struct vhost_poll *poll) }EXPORT_SYMBOL_GPL (vhost_poll_stop); -void vhost_work_dev_flush (struct vhost_dev *dev) -{ -struct vhost_flush_struct flush; - -if (dev->worker) { -init_completion (&flush.wait_event); -vhost_work_init (&flush.work, vhost_flush_work); - -vhost_work_queue (dev, &flush.work); -wait_for_completion (&flush.wait_event); - } -} -EXPORT_SYMBOL_GPL (vhost_work_dev_flush); -/* Flush any work that has been scheduled. When calling this, don't hold any * locks that are also used by the callback. */ void vhost_poll_flush (struct vhost_poll *poll)
@@ -253,26 +239,62 @@ void vhost_poll_flush(struct vhost_poll *poll) }EXPORT_SYMBOL_GPL (vhost_poll_flush); -void vhost_work_queue (struct vhost_dev *dev,struct vhost_work *work) +static void vhost_work_queue_on (struct vhost_work *work, +struct vhost_worker *worker) { -if (!dev->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->worker->work_list); -wake_up_process (dev->worker->task); +llist_add (&work->node, &worker->work_list); +wake_up_process (worker->task); } } + +void vhost_work_queue (struct vhost_dev *dev,struct vhost_work *work) +{ +if (!dev->workers) +return ; +/* +* devs with use_worker=true created by tools that do not support the +* worker creation ioctl will always have at least one worker. +*/ +vhost_work_queue_on (work, dev->workers[0 ]); +}EXPORT_SYMBOL_GPL (vhost_work_queue); +static void vhost_work_dev_flush_on (struct vhost_worker *worker) +{ +struct vhost_flush_struct flush; + +init_completion (&flush.wait_event); +vhost_work_init (&flush.work, vhost_flush_work); + +vhost_work_queue_on (&flush.work, worker); +wait_for_completion (&flush.wait_event); +} + +void vhost_work_dev_flush (struct vhost_dev *dev) +{ +int i; + +for (i =0 ; i < dev->num_workers; i++) +vhost_work_dev_flush_on (dev->workers[i]); +} +EXPORT_SYMBOL_GPL (vhost_work_dev_flush); +/* A lockless hint for busy polling code to exit the loop */ bool vhost_has_work (struct vhost_dev *dev) { -return dev->worker && !llist_empty (&dev->worker->work_list); +int i; + +for (i =0 ; i < dev->num_workers; i++) { +if (!llist_empty (&dev->workers[i]->work_list)) +return true; + } + +return false; }EXPORT_SYMBOL_GPL (vhost_has_work);
@@ -482,7 +504,8 @@ void vhost_dev_init(struct vhost_dev *dev, dev->umem =NULL ; dev->iotlb =NULL ; dev->mm =NULL ; - dev->worker =NULL ; + dev->workers =NULL ; + dev->num_workers =0 ; dev->iov_limit = iov_limit; dev->weight = weight; dev->byte_weight = byte_weight;
@@ -531,14 +554,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_attach_cgroups_on (struct vhost_worker *worker) {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_work_dev_flush (dev); +vhost_work_queue_on (&attach.work, worker); +vhost_work_dev_flush_on (worker);return attach.ret; }
@@ -579,20 +602,29 @@ static void vhost_detach_mm(struct vhost_dev *dev) dev->mm =NULL ; } -static void vhost_worker_free (struct vhost_dev *dev) +static void vhost_worker_free (struct vhost_worker *worker) { -struct vhost_worker *worker = dev->worker; - -if (!worker) -return ; - - dev->worker =NULL ;WARN_ON (!llist_empty (&worker->work_list));kthread_stop (worker->task);kfree (worker); } -static int vhost_worker_create (struct vhost_dev *dev) +static void vhost_workers_free (struct vhost_dev *dev) +{ +int i; + +if (!dev->workers) +return ; + +for (i =0 ; i < dev->num_workers; i++) +vhost_worker_free (dev->workers[i]); + +kfree (dev->workers); + dev->num_workers =0 ; + dev->workers =NULL ; +} + +static struct vhost_worker *vhost_worker_create (struct vhost_dev *dev) {struct vhost_worker *worker;struct task_struct *task;
@@ -600,42 +632,56 @@ static int vhost_worker_create(struct vhost_dev *dev) worker =kzalloc (sizeof (*worker),GFP_KERNEL );if (!worker) -return -ENOMEM ; +return NULL ; - dev->worker = worker; + worker->id = dev->num_workers; worker->dev = dev;init_llist_head (&worker->work_list); task =kthread_create (vhost_worker, worker,"vhost-%d" , current->pid); -if (IS_ERR (task)) { - ret =PTR_ERR (task); +if (IS_ERR (task)) goto free_worker; - } worker->task = task;wake_up_process (task);/* avoid contributing to loadavg */ - ret =vhost_attach_cgroups (dev); + ret =vhost_attach_cgroups_on (worker);if (ret) goto stop_worker; -return 0 ; +return worker; stop_worker:kthread_stop (worker->task); free_worker:kfree (worker); - dev->worker =NULL ; -return ret; +return NULL ; }/* Caller must have device mutex */ static int vhost_worker_try_create_def (struct vhost_dev *dev) { -if (!dev->use_worker || dev->worker) +struct vhost_worker *worker; + +if (!dev->use_worker || dev->workers)return 0 ; -return vhost_worker_create (dev); + dev->workers =kcalloc (1 ,sizeof (struct vhost_worker *),GFP_KERNEL ); +if (!dev->workers) +return -ENOMEM ; + + worker =vhost_worker_create (dev); +if (!worker) + goto free_workers; + + dev->workers[worker->id] = worker; + dev->num_workers++; +return 0 ; + +free_workers: +kfree (dev->workers); + dev->workers =NULL ; +return -ENOMEM ; }/* Caller should have device mutex */
@@ -750,7 +796,7 @@ void vhost_dev_cleanup(struct vhost_dev *dev) dev->iotlb =NULL ;vhost_clear_msg (dev);wake_up_interruptible_poll (&dev->wait,EPOLLIN |EPOLLRDNORM ); -vhost_worker_free (dev); +vhost_workers_free (dev);vhost_detach_mm (dev); dev->kcov_handle =0 ; }
drivers/vhost/vhost.h +3 -1
@@ -29,6 +29,7 @@ struct vhost_worker {struct task_struct *task;struct llist_head work_list;struct vhost_dev *dev; +int id; };/* Poll a file (eventfd or socket) */
@@ -155,7 +156,8 @@ struct vhost_dev {struct vhost_virtqueue **vqs;int nvqs;struct eventfd_ctx *log_ctx; -struct vhost_worker *worker; +struct vhost_worker **workers; +int num_workers;struct vhost_iotlb *umem;struct vhost_iotlb *iotlb;spinlock_t iotlb_lock;
Signature · 6 lines
2.25.1 _______________________________________________ Virtualization mailing list [email protected] https://lists.linuxfoundation.org/mailman/listinfo/virtualization
Mike Christie
maintainerRFC PATCH 8/14 vhost: allow vhost_polls to use different vhost_workers
In reply to Mike ChristieThis allows vhost_polls to use the worker the vq the poll is associated with.
This also exports a helper vhost_vq_work_queue which is used here internally, and will be used in the vhost-scsi patches.
--- drivers/vhost/net.c | 6 ++++-- drivers/vhost/vhost.c | 19 ++++++++++++++++--- drivers/vhost/vhost.h | 6 +++++- 3 files changed, 25 insertions(+), 6 deletions(-)
drivers/vhost/net.c +4 -2
@@ -1334,8 +1334,10 @@ static int vhost_net_open(struct inode *inode, struct file *f)VHOST_NET_PKT_WEIGHT ,VHOST_NET_WEIGHT , true, NULL); -vhost_poll_init (n->poll +VHOST_NET_VQ_TX , handle_tx_net,EPOLLOUT , dev); -vhost_poll_init (n->poll +VHOST_NET_VQ_RX , handle_rx_net,EPOLLIN , dev); +vhost_poll_init (n->poll +VHOST_NET_VQ_TX , handle_tx_net,EPOLLOUT , dev, + vqs[VHOST_NET_VQ_TX ]); +vhost_poll_init (n->poll +VHOST_NET_VQ_RX , handle_rx_net,EPOLLIN , dev, + vqs[VHOST_NET_VQ_RX ]); f->private_data = n; n->page_frag.page =NULL ;
drivers/vhost/vhost.c +16 -3
@@ -187,13 +187,15 @@ 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) +__poll_t mask,struct vhost_dev *dev, +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->wqh =NULL ; + poll->vq = vq;vhost_work_init (&poll->work, fn); }
@@ -298,9 +300,15 @@ bool vhost_has_work(struct vhost_dev *dev) }EXPORT_SYMBOL_GPL (vhost_has_work); +void vhost_vq_work_queue (struct vhost_virtqueue *vq,struct vhost_work *work) +{ +vhost_work_queue_on (work, vq->worker); +} +EXPORT_SYMBOL_GPL (vhost_vq_work_queue); +void vhost_poll_queue (struct vhost_poll *poll) { -vhost_work_queue (poll->dev, &poll->work); +vhost_vq_work_queue (poll->vq, &poll->work); }EXPORT_SYMBOL_GPL (vhost_poll_queue);
@@ -359,6 +367,7 @@ static void vhost_vq_reset(struct vhost_dev *dev, vq->busyloop_timeout =0 ; vq->umem =NULL ; vq->iotlb =NULL ; + vq->worker =NULL ;vhost_vring_call_reset (&vq->call_ctx);__vhost_vq_meta_reset (vq); }
@@ -527,7 +536,7 @@ void vhost_dev_init(struct vhost_dev *dev,vhost_vq_reset (dev, vq);if (vq->handle_kick)vhost_poll_init (&vq->poll, vq->handle_kick, -EPOLLIN , dev); +EPOLLIN , dev, vq); } }EXPORT_SYMBOL_GPL (vhost_dev_init);
@@ -662,6 +671,7 @@ static struct vhost_worker *vhost_worker_create(struct vhost_dev *dev)static int vhost_worker_try_create_def (struct vhost_dev *dev) {struct vhost_worker *worker; +int i;if (!dev->use_worker || dev->workers)return 0 ;
@@ -674,6 +684,9 @@ static int vhost_worker_try_create_def(struct vhost_dev *dev)if (!worker) goto free_workers; +for (i =0 ; i < dev->nvqs; i++) + dev->vqs[i]->worker = worker; + dev->workers[worker->id] = worker; dev->num_workers++;return 0 ;
drivers/vhost/vhost.h +5 -1
@@ -41,14 +41,17 @@ struct vhost_poll {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);void vhost_work_queue (struct vhost_dev *dev,struct vhost_work *work);bool vhost_has_work (struct vhost_dev *dev); +void vhost_vq_work_queue (struct vhost_virtqueue *vq,struct vhost_work *work);void vhost_poll_init (struct vhost_poll *poll,vhost_work_fn_t fn, -__poll_t mask,struct vhost_dev *dev); +__poll_t mask,struct vhost_dev *dev, +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_flush (struct vhost_poll *poll);
@@ -76,6 +79,7 @@ struct vhost_vring_call {/* The virtqueue structure describes a queue attached to a device. */ struct vhost_virtqueue {struct vhost_dev *dev; +struct vhost_worker *worker;/* The actual ring of buffers. */ struct mutex mutex;
Signature · 6 lines
2.25.1 _______________________________________________ Virtualization mailing list [email protected] https://lists.linuxfoundation.org/mailman/listinfo/virtualization
Mike Christie
maintainerRFC PATCH 9/14 vhost-scsi: flush IO vqs then send TMF rsp
In reply to Mike ChristieWith one worker we will always send the scsi cmd responses then send the TMF rsp, because LIO will always complete the scsi cmds first which calls vhost_scsi_release_cmd to add them to the work queue.
When the next patches adds multiple worker support, the worker threads could still be sending their responses when the tmf's work is run. So this patch has vhost-scsi flush the IO vqs on other worker threads before we send the tmf response.
--- drivers/vhost/scsi.c | 17 ++++++++++++++--- drivers/vhost/vhost.c | 6 ++++++ drivers/vhost/vhost.h | 1 + 3 files changed, 21 insertions(+), 3 deletions(-)
drivers/vhost/scsi.c +14 -3
@@ -1168,12 +1168,23 @@ static void vhost_scsi_tmf_resp_work(struct vhost_work *work) { struct vhost_scsi_tmf *tmf = container_of(work, struct vhost_scsi_tmf, vwork); -int resp_code; +int resp_code, i; + +if (tmf->scsi_resp ==TMR_FUNCTION_COMPLETE ) { +/* +* When processing a TMF, lio completes the cmds then the +* TMF, so with one worker the TMF always completes after +* cmds. For multiple worker support, we must flush the +* IO vqs that do not share a worker with the ctl vq (vqs +* 3 and up) to make sure they have completed their cmds. +*/ +for (i =1 ; i < tmf->vhost->dev.num_workers; i++) +vhost_vq_work_flush (&tmf->vhost->vqs[i +VHOST_SCSI_VQ_IO ].vq); -if (tmf->scsi_resp ==TMR_FUNCTION_COMPLETE ) resp_code =VIRTIO_SCSI_S_FUNCTION_SUCCEEDED ; -else + }else { resp_code =VIRTIO_SCSI_S_FUNCTION_REJECTED ; + }vhost_scsi_send_tmf_resp (tmf->vhost, &tmf->svq->vq, tmf->in_iovs, tmf->vq_desc, &tmf->resp_iov, resp_code);
drivers/vhost/vhost.c +6 -0
@@ -300,6 +300,12 @@ bool vhost_has_work(struct vhost_dev *dev) }EXPORT_SYMBOL_GPL (vhost_has_work); +void vhost_vq_work_flush (struct vhost_virtqueue *vq) +{ +vhost_work_dev_flush_on (vq->worker); +} +EXPORT_SYMBOL_GPL (vhost_vq_work_flush); +void vhost_vq_work_queue (struct vhost_virtqueue *vq,struct vhost_work *work) {vhost_work_queue_on (work, vq->worker);
drivers/vhost/vhost.h +1 -0
@@ -47,6 +47,7 @@ struct vhost_poll {void vhost_work_init (struct vhost_work *work,vhost_work_fn_t fn);void vhost_work_queue (struct vhost_dev *dev,struct vhost_work *work);bool vhost_has_work (struct vhost_dev *dev); +void vhost_vq_work_flush (struct vhost_virtqueue *vq);void vhost_vq_work_queue (struct vhost_virtqueue *vq,struct vhost_work *work); void vhost_poll_init(struct vhost_poll *poll, vhost_work_fn_t fn,
Signature · 6 lines
2.25.1 _______________________________________________ Virtualization mailing list [email protected] https://lists.linuxfoundation.org/mailman/listinfo/virtualization
Mike Christie
maintainerRFC PATCH 10/14 vhost-scsi: make SCSI cmd completion per vq
In reply to Mike ChristieThis patch separates the scsi cmd completion code paths so we can complete cmds based on their vq instead of having all cmds complete on the same worker/CPU. This will be useful with the next patch that allows us to create mulitple worker threads and bind them to different vqs, so we can have completions running on different threads/CPUs.
--- drivers/vhost/scsi.c | 48 +++++++++++++++++++++++--------------------- 1 file changed, 25 insertions(+), 23 deletions(-)
drivers/vhost/scsi.c +25 -23
@@ -176,6 +176,7 @@ enum {struct vhost_scsi_virtqueue {struct vhost_virtqueue vq; +struct vhost_scsi *vs;/* * Reference counting for inflight reqs, used for flush operation. At * each time, one reference tracks new commands submitted, while we
@@ -190,6 +191,9 @@ struct vhost_scsi_virtqueue {struct vhost_scsi_cmd *scsi_cmds;struct sbitmap scsi_tags;int max_cmds; + +struct vhost_work completion_work; +struct llist_head completion_list; };struct vhost_scsi {
@@ -200,9 +204,6 @@ struct vhost_scsi {struct vhost_dev dev;struct vhost_scsi_virtqueue vqs[VHOST_SCSI_MAX_VQ ]; -struct vhost_work vs_completion_work;/* cmd completion work item */ -struct llist_head vs_completion_list;/* cmd completion queue */ -struct vhost_work vs_event_work;/* evt injection work item */ struct llist_head vs_event_list;/* evt injection queue */
@@ -377,10 +378,11 @@ static void vhost_scsi_release_cmd(struct se_cmd *se_cmd) }else {struct vhost_scsi_cmd *cmd =container_of (se_cmd, struct vhost_scsi_cmd, tvc_se_cmd); -struct vhost_scsi *vs = cmd->tvc_vhost; +struct vhost_scsi_virtqueue *svq =container_of (cmd->tvc_vq, + struct vhost_scsi_virtqueue, vq); -llist_add (&cmd->tvc_completion_list, &vs->vs_completion_list); -vhost_work_queue (&vs->dev, &vs->vs_completion_work); +llist_add (&cmd->tvc_completion_list, &svq->completion_list); +vhost_vq_work_queue (&svq->vq, &svq->completion_work); } }
@@ -543,18 +545,17 @@ static void vhost_scsi_evt_work(struct vhost_work *work)*/ static void vhost_scsi_complete_cmd_work (struct vhost_work *work) { - struct vhost_scsi *vs = container_of(work, struct vhost_scsi, - vs_completion_work); -DECLARE_BITMAP (signal,VHOST_SCSI_MAX_VQ ); +struct vhost_scsi_virtqueue *svq =container_of (work, + struct vhost_scsi_virtqueue, completion_work);struct virtio_scsi_cmd_resp v_rsp;struct vhost_scsi_cmd *cmd, *t;struct llist_node *llnode;struct se_cmd *se_cmd;struct iov_iter iov_iter; -int ret, vq; +bool signal = false; +int ret; -bitmap_zero (signal,VHOST_SCSI_MAX_VQ ); - llnode =llist_del_all (&vs->vs_completion_list); + llnode =llist_del_all (&svq->completion_list);llist_for_each_entry_safe (cmd, t, llnode, tvc_completion_list) { se_cmd = &cmd->tvc_se_cmd;
@@ -574,21 +575,16 @@ static void vhost_scsi_complete_cmd_work(struct vhost_work *work) cmd->tvc_in_iovs, sizeof(v_rsp)); ret =copy_to_iter (&v_rsp,sizeof (v_rsp), &iov_iter);if (likely (ret ==sizeof (v_rsp))) { -struct vhost_scsi_virtqueue *q; + signal = true;vhost_add_used (cmd->tvc_vq, cmd->tvc_vq_desc,0 ); - q = container_of(cmd->tvc_vq, struct vhost_scsi_virtqueue, vq); - vq = q - vs->vqs; -__set_bit (vq, signal); }else pr_err ("Faulted on virtio_scsi_cmd_resp\n" );vhost_scsi_release_cmd_res (se_cmd); } - vq =-1 ; -while ((vq =find_next_bit (signal,VHOST_SCSI_MAX_VQ , vq +1 )) - <VHOST_SCSI_MAX_VQ ) -vhost_signal (&vs->dev, &vs->vqs[vq].vq); +if (signal) +vhost_signal (&svq->vs->dev, &svq->vq); } static struct vhost_scsi_cmd *
@@ -1799,6 +1795,7 @@ static int vhost_scsi_set_features(struct vhost_scsi *vs, u64 features)static int vhost_scsi_open (struct inode *inode,struct file *f) { +struct vhost_scsi_virtqueue *svq;struct vhost_scsi *vs;struct vhost_virtqueue **vqs;int r = -ENOMEM , i;
@@ -1811,7 +1808,6 @@ static int vhost_scsi_open(struct inode *inode, struct file *f)if (!vqs) goto err_vqs; -vhost_work_init (&vs->vs_completion_work, vhost_scsi_complete_cmd_work);vhost_work_init (&vs->vs_event_work, vhost_scsi_evt_work); vs->vs_events_nr =0 ;
@@ -1822,8 +1818,14 @@ static int vhost_scsi_open(struct inode *inode, struct file *f) vs->vqs[VHOST_SCSI_VQ_CTL ].vq.handle_kick = vhost_scsi_ctl_handle_kick; vs->vqs[VHOST_SCSI_VQ_EVT ].vq.handle_kick = vhost_scsi_evt_handle_kick;for (i =VHOST_SCSI_VQ_IO ; i <VHOST_SCSI_MAX_VQ ; i++) { - vqs[i] = &vs->vqs[i].vq; - vs->vqs[i].vq.handle_kick = vhost_scsi_handle_kick; + svq = &vs->vqs[i]; + + vqs[i] = &svq->vq; + svq->vs = vs; +init_llist_head (&svq->completion_list); +vhost_work_init (&svq->completion_work, + vhost_scsi_complete_cmd_work); + svq->vq.handle_kick = vhost_scsi_handle_kick; }vhost_dev_init (&vs->dev, vqs,VHOST_SCSI_MAX_VQ ,UIO_MAXIOV ,VHOST_SCSI_WEIGHT ,0 , true,NULL );
Signature · 6 lines
2.25.1 _______________________________________________ Virtualization mailing list [email protected] https://lists.linuxfoundation.org/mailman/listinfo/virtualization
Mike Christie
maintainerRFC PATCH 11/14 vhost: allow userspace to create workers
In reply to Mike ChristieThis patch allows userspace to create workers and bind them to vqs, so you can have N workers per dev and also share N workers with M vqs. The next patch will allow sharing across devices.
--- drivers/vhost/vhost.c | 95 +++++++++++++++++++++++++++++++- drivers/vhost/vhost.h | 3 + include/uapi/linux/vhost.h | 6 ++ include/uapi/linux/vhost_types.h | 9 +++ 4 files changed, 111 insertions(+), 2 deletions(-)
drivers/vhost/vhost.c +93 -2
@@ -42,6 +42,9 @@ module_param(max_iotlb_entries, int, 0444);MODULE_PARM_DESC (max_iotlb_entries,"Maximum number of iotlb entries. (default: 2048)" ); +static LIST_HEAD (vhost_workers_list); +static DEFINE_SPINLOCK (vhost_workers_lock); +enum {VHOST_MEMORY_F_LOG =0x1 , };
@@ -617,8 +620,16 @@ static void vhost_detach_mm(struct vhost_dev *dev) dev->mm =NULL ; } -static void vhost_worker_free (struct vhost_worker *worker) +static void vhost_worker_put (struct vhost_worker *worker) { +spin_lock (&vhost_workers_lock); +if (!refcount_dec_and_test (&worker->refcount)) { +spin_unlock (&vhost_workers_lock); +return ; + } +list_del (&worker->list); +spin_unlock (&vhost_workers_lock); +WARN_ON (!llist_empty (&worker->work_list));kthread_stop (worker->task);kfree (worker);
@@ -632,7 +643,7 @@ static void vhost_workers_free(struct vhost_dev *dev)return ;for (i =0 ; i < dev->num_workers; i++) -vhost_worker_free (dev->workers[i]); +vhost_worker_put (dev->workers[i]);kfree (dev->workers); dev->num_workers =0 ;
@@ -652,6 +663,8 @@ static struct vhost_worker *vhost_worker_create(struct vhost_dev *dev) worker->id = dev->num_workers; worker->dev = dev;init_llist_head (&worker->work_list); +INIT_LIST_HEAD (&worker->list); +refcount_set (&worker->refcount,1 ); task =kthread_create (vhost_worker, worker,"vhost-%d" , current->pid);if (IS_ERR (task))
@@ -664,6 +677,9 @@ static struct vhost_worker *vhost_worker_create(struct vhost_dev *dev)if (ret) goto stop_worker; +spin_lock (&vhost_workers_lock); +list_add_tail (&worker->list, &vhost_workers_list); +spin_unlock (&vhost_workers_lock);return worker; stop_worker:
@@ -673,6 +689,71 @@ static struct vhost_worker *vhost_worker_create(struct vhost_dev *dev)return NULL ; } +static struct vhost_worker *vhost_worker_find (struct vhost_dev *dev,pid_t pid) +{ +struct vhost_worker *worker; + +/* TODO hash on pid? */ +spin_lock (&vhost_workers_lock); +list_for_each_entry (worker, &vhost_workers_list, list) { +if (worker->task->pid != pid) +continue ; + +/* tmp - next patch allows sharing across devs */ +if (worker->dev != dev) { +spin_unlock (&vhost_workers_lock); +return NULL ; + } + +refcount_inc (&worker->refcount); +spin_unlock (&vhost_workers_lock); +return worker; + } +spin_unlock (&vhost_workers_lock); +return NULL ; +} + +/* Caller must have device mutex */ +static int vhost_vq_set_worker (struct vhost_virtqueue *vq, +struct vhost_vring_worker *info) +{ +struct vhost_dev *dev = vq->dev; +struct vhost_worker *worker; + +if (vq->worker) { +/* TODO - support changing while works are running */ +return -EBUSY ; + } + +if (info->pid ==-1 ) { + worker =vhost_worker_create (dev); +if (!worker) +return -ENOMEM ; + + info->pid = worker->task->pid; + }else { + worker =vhost_worker_find (dev, info->pid); +if (!worker) +return -ENODEV ; + } + +if (!dev->workers) { + dev->workers =kcalloc (vq->dev->nvqs, +sizeof (struct vhost_worker *), +GFP_KERNEL ); +if (!dev->workers) { +vhost_worker_put (worker); +return -ENOMEM ; + } + } + + vq->worker = worker; + + dev->workers[dev->num_workers] = worker; + dev->num_workers++; +return 0 ; +} +/* Caller must have device mutex */ static int vhost_worker_try_create_def (struct vhost_dev *dev) {
@@ -1680,6 +1761,7 @@ long vhost_vring_ioctl(struct vhost_dev *d, unsigned int ioctl, void __user *argstruct eventfd_ctx *ctx =NULL ;u32 __user *idxp = argp;struct vhost_virtqueue *vq; +struct vhost_vring_worker w;struct vhost_vring_state s;struct vhost_vring_file f;u32 idx;
@@ -1794,6 +1876,15 @@ long vhost_vring_ioctl(struct vhost_dev *d, unsigned int ioctl, void __user *argif (copy_to_user (argp, &s,sizeof (s))) r = -EFAULT ;break ; +case VHOST_SET_VRING_WORKER : +if (copy_from_user (&w, argp,sizeof (w))) { + r = -EFAULT ; +break ; + } + r =vhost_vq_set_worker (vq, &w); +if (!r &©_to_user (argp, &w,sizeof (w))) + r = -EFAULT ; +break ;default : r = -ENOIOCTLCMD ; }
drivers/vhost/vhost.h +3 -0
@@ -14,6 +14,7 @@#include <linux/atomic.h> #include <linux/vhost_iotlb.h> #include <linux/irqbypass.h> +#include <linux/refcount.h> struct vhost_work ;typedef void (*vhost_work_fn_t )(struct vhost_work *work);
@@ -28,6 +29,8 @@ struct vhost_work {struct vhost_worker {struct task_struct *task;struct llist_head work_list; +struct list_head list; +refcount_t refcount;struct vhost_dev *dev;int id; };
include/uapi/linux/vhost.h +6 -0
@@ -70,6 +70,12 @@#define VHOST_VRING_BIG_ENDIAN 1#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) +/* Create/bind a vhost worker to a virtqueue. If pid > 0 and matches an existing +* vhost_worker thread it will be bound to the vq. If pid is -1, then a new +* worker will be created and bound to the vq. +*/ +#define VHOST_SET_VRING_WORKER _IOWR(VHOST_VIRTIO, 0x15, struct vhost_vring_worker) +/* The following ioctls use eventfd file descriptors to signal and poll * for events. */
include/uapi/linux/vhost_types.h +9 -0
@@ -47,6 +47,15 @@ struct vhost_vring_addr {__u64 log_guest_addr; }; +struct vhost_vring_worker { +unsigned int index; +/* +* The pid of the vhost worker that the vq will be bound to. If -1, +* a new worker will be created and it's pid will be returned in pid. +*/ +__kernel_pid_t pid; +}; +/* no alignment requirement */ struct vhost_iotlb_msg {__u64 iova;
Signature · 6 lines
2.25.1 _______________________________________________ Virtualization mailing list [email protected] https://lists.linuxfoundation.org/mailman/listinfo/virtualization
Stefano Garzarella
maintainerRe: [PATCH RFC 11/14] vhost: allow userspace to create workers
Quoted 88 lines · Mike Christie
This patch allows userspace to create workers and bind them to vqs, so you can have N workers per dev and also share N workers with M vqs. The next patch will allow sharing across devices.
Signed-off-by Mike Christie <[email protected]>---
drivers/vhost/vhost.c | 95 +++++++++++++++++++++++++++++++- drivers/vhost/vhost.h | 3 + include/uapi/linux/vhost.h | 6 ++ include/uapi/linux/vhost_types.h | 9 +++ 4 files changed, 111 insertions(+), 2 deletions(-)drivers/vhost/vhost.c +18
-2@@ -42,6 +42,9 @@ module_param(max_iotlb_entries, int, 0444);MODULE_PARM_DESC (max_iotlb_entries,"Maximum number of iotlb entries. (default: 2048)" ); +static LIST_HEAD (vhost_workers_list); +static DEFINE_SPINLOCK (vhost_workers_lock); +enum {VHOST_MEMORY_F_LOG =0x1 , };@@ -617,8 +620,16 @@ static void vhost_detach_mm(struct vhost_dev *dev) dev->mm =NULL ; } -static void vhost_worker_free (struct vhost_worker *worker) +static void vhost_worker_put (struct vhost_worker *worker) { +spin_lock (&vhost_workers_lock); +if (!refcount_dec_and_test (&worker->refcount)) { +spin_unlock (&vhost_workers_lock); +return ; + } +list_del (&worker->list); +spin_unlock (&vhost_workers_lock); +WARN_ON (!llist_empty (&worker->work_list));kthread_stop (worker->task);kfree (worker);@@ -632,7 +643,7 @@ static void vhost_workers_free(struct vhost_dev *dev)return ;for (i =0 ; i < dev->num_workers; i++) -vhost_worker_free (dev->workers[i]); +vhost_worker_put (dev->workers[i]);kfree (dev->workers); dev->num_workers =0 ;@@ -652,6 +663,8 @@ static struct vhost_worker *vhost_worker_create(struct vhost_dev *dev) worker->id = dev->num_workers; worker->dev = dev;init_llist_head (&worker->work_list); +INIT_LIST_HEAD (&worker->list); +refcount_set (&worker->refcount,1 ); task =kthread_create (vhost_worker, worker,"vhost-%d" , current->pid);if (IS_ERR (task))@@ -664,6 +677,9 @@ static struct vhost_worker *vhost_worker_create(struct vhost_dev *dev)if (ret) goto stop_worker; +spin_lock (&vhost_workers_lock); +list_add_tail (&worker->list, &vhost_workers_list); +spin_unlock (&vhost_workers_lock);return worker;
stop_worker:drivers/vhost/vhost.c +12
-0@@ -673,6 +689,71 @@ static struct vhost_worker *vhost_worker_create(struct vhost_dev *dev)return NULL ; } +static struct vhost_worker *vhost_worker_find (struct vhost_dev *dev,pid_t pid) +{ +struct vhost_worker *worker; + +/* TODO hash on pid? */ +spin_lock (&vhost_workers_lock); +list_for_each_entry (worker, &vhost_workers_list, list) { +if (worker->task->pid != pid) +continue ; + +/* tmp - next patch allows sharing across devs */ +if (worker->dev != dev) {
drivers/vhost/vhost.c static struct vhost_worker *vhost_worker_create(struct vhost_dev *dev) · line 713
Quoted 10 lines
drivers/vhost/vhost.c +10
-0+spin_unlock (&vhost_workers_lock); +return NULL ; + } + +refcount_inc (&worker->refcount); +spin_unlock (&vhost_workers_lock); +return worker; + } +spin_unlock (&vhost_workers_lock); +return NULL ;
I would like to have a single point where we release the lock to avoid future issues, how about changing vhost_worker_find() to:
static struct vhost_worker *vhost_worker_find(struct vhost_dev *dev, pid_t pid)
{
struct vhost_worker *worker, *found_worker = NULL;
spin_lock(&vhost_workers_lock);
list_for_each_entry(worker, &vhost_workers_list, list) {
if (worker->task->pid == pid) {
/* tmp - next patch allows sharing across devs */
if (worker->dev != dev)
break;
found_worker = worker;
refcount_inc(&found_worker->refcount);
break;
}
}
spin_unlock(&vhost_workers_lock);
return found_worker;
}Quoted 96 lines
drivers/vhost/vhost.c +53
-0+} + +/* Caller must have device mutex */ +static int vhost_vq_set_worker (struct vhost_virtqueue *vq, +struct vhost_vring_worker *info) +{ +struct vhost_dev *dev = vq->dev; +struct vhost_worker *worker; + +if (vq->worker) { +/* TODO - support changing while works are running */ +return -EBUSY ; + } + +if (info->pid ==-1 ) { + worker =vhost_worker_create (dev); +if (!worker) +return -ENOMEM ; + + info->pid = worker->task->pid; + }else { + worker =vhost_worker_find (dev, info->pid); +if (!worker) +return -ENODEV ; + } + +if (!dev->workers) { + dev->workers =kcalloc (vq->dev->nvqs, +sizeof (struct vhost_worker *), +GFP_KERNEL ); +if (!dev->workers) { +vhost_worker_put (worker); +return -ENOMEM ; + } + } + + vq->worker = worker; + + dev->workers[dev->num_workers] = worker; + dev->num_workers++; +return 0 ; +} +/* Caller must have device mutex */ static int vhost_worker_try_create_def (struct vhost_dev *dev) {@@ -1680,6 +1761,7 @@ long vhost_vring_ioctl(struct vhost_dev *d, unsigned int ioctl, void __user *argstruct eventfd_ctx *ctx =NULL ;u32 __user *idxp = argp;struct vhost_virtqueue *vq; +struct vhost_vring_worker w;struct vhost_vring_state s;struct vhost_vring_file f;u32 idx;@@ -1794,6 +1876,15 @@ long vhost_vring_ioctl(struct vhost_dev *d, unsigned int ioctl, void __user *argif (copy_to_user (argp, &s,sizeof (s))) r = -EFAULT ;break ; +case VHOST_SET_VRING_WORKER : +if (copy_from_user (&w, argp,sizeof (w))) { + r = -EFAULT ; +break ; + } + r =vhost_vq_set_worker (vq, &w); +if (!r &©_to_user (argp, &w,sizeof (w))) + r = -EFAULT ; +break ;default : r = -ENOIOCTLCMD ; }drivers/vhost/vhost.h +3
-0@@ -14,6 +14,7 @@#include <linux/atomic.h> #include <linux/vhost_iotlb.h> #include <linux/irqbypass.h> +#include <linux/refcount.h> struct vhost_work ;typedef void (*vhost_work_fn_t )(struct vhost_work *work);@@ -28,6 +29,8 @@ struct vhost_work {struct vhost_worker {struct task_struct *task;struct llist_head work_list; +struct list_head list; +refcount_t refcount;struct vhost_dev *dev;int id; };include/uapi/linux/vhost.h +0
-0
include/uapi/linux/vhost.h line 74
Quoted 5 lines
include/uapi/linux/vhost.h +2
-0@@ -70,6 +70,12 @@#define VHOST_VRING_BIG_ENDIAN 1#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) +/* Create/bind a vhost worker to a virtqueue. If pid > 0 and matches an existing +* vhost_worker thread it will be bound to the vq. If pid is -1, then a new
What about adding a macro for -1? (e.g. VHOST_VRING_NEW_WORKER)
Thanks,
Stefano
_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization
Mike Christie
maintainerRe: [PATCH RFC 11/14] vhost: allow userspace to create workers
Quoted 114 lines · Stefano Garzarella
Quoted 88 lines · Mike Christie
This patch allows userspace to create workers and bind them to vqs, so you can have N workers per dev and also share N workers with M vqs. The next patch will allow sharing across devices.
Signed-off-by Mike Christie <[email protected]>---
drivers/vhost/vhost.c | 95 +++++++++++++++++++++++++++++++- drivers/vhost/vhost.h | 3 + include/uapi/linux/vhost.h | 6 ++include/uapi/linux/vhost_types.h| 9 +++4 files changed, 111 insertions(+), 2 deletions(-)drivers/vhost/vhost.c +18
-2@@ -42,6 +42,9 @@ module_param(max_iotlb_entries, int, 0444);MODULE_PARM_DESC (max_iotlb_entries, "Maximum number of iotlb entries. (default: 2048)"); +static LIST_HEAD (vhost_workers_list); +static DEFINE_SPINLOCK (vhost_workers_lock); +enum { VHOST_MEMORY_F_LOG = 0x1, };@@ -617,8 +620,16 @@ static void vhost_detach_mm(struct vhost_dev *dev) dev->mm = NULL; } -static void vhost_worker_free (struct vhost_worker *worker) +static void vhost_worker_put (struct vhost_worker *worker) { + spin_lock(&vhost_workers_lock); + if (!refcount_dec_and_test(&worker->refcount)) { + spin_unlock(&vhost_workers_lock); + return; + } + list_del(&worker->list); + spin_unlock(&vhost_workers_lock); + WARN_ON(!llist_empty(&worker->work_list)); kthread_stop(worker->task); kfree(worker);@@ -632,7 +643,7 @@ static void vhost_workers_free(struct vhost_dev *dev) return; for (i = 0; i < dev->num_workers; i++) - vhost_worker_free(dev->workers[i]); + vhost_worker_put(dev->workers[i]); kfree(dev->workers); dev->num_workers = 0;@@ -652,6 +663,8 @@ static struct vhost_worker *vhost_worker_create(struct vhost_dev *dev) worker->id = dev->num_workers; worker->dev = dev; init_llist_head(&worker->work_list); + INIT_LIST_HEAD(&worker->list); + refcount_set(&worker->refcount, 1); task = kthread_create(vhost_worker, worker, "vhost-%d", current->pid); if (IS_ERR(task))@@ -664,6 +677,9 @@ static struct vhost_worker *vhost_worker_create(struct vhost_dev *dev) if (ret) goto stop_worker; + spin_lock(&vhost_workers_lock); + list_add_tail(&worker->list, &vhost_workers_list); + spin_unlock(&vhost_workers_lock); return worker;
stop_worker:drivers/vhost/vhost.c +12
-0@@ -673,6 +689,71 @@ static struct vhost_worker *vhost_worker_create(struct vhost_dev *dev) return NULL; } +static struct vhost_worker *vhost_worker_find (struct vhost_dev *dev,pid_t pid) +{ + struct vhost_worker *worker; + + /* TODO hash on pid? */ + spin_lock(&vhost_workers_lock); + list_for_each_entry(worker, &vhost_workers_list, list) { + if (worker->task->pid != pid) + continue; + + /* tmp - next patch allows sharing across devs */ + if (worker->dev != dev) {drivers/vhost/vhost.c static struct vhost_worker *vhost_worker_create(struct vhost_dev *dev) · line 713
Quoted 10 lines
drivers/vhost/vhost.c +10
-0+ spin_unlock(&vhost_workers_lock); + return NULL; + } + + refcount_inc(&worker->refcount); + spin_unlock(&vhost_workers_lock); + return worker; + } + spin_unlock(&vhost_workers_lock); + return NULL;I would like to have a single point where we release the lock to avoid future issues, how about changing
vhost_worker_find()to:static struct
vhost_worker*vhost_worker_find(structvhost_dev*dev,pid_tpid) {
structvhost_worker*worker,*found_worker= NULL;spin_lock(&vhost_workers_lock);
list_for_each_entry(worker, &vhost_workers_list, list){ if (worker->task->pid== pid) {
/* tmp - next patch allows sharing across devs */ if (worker->dev!= dev)
break;
Quoted 8 lines
found_worker= worker;refcount_inc(&found_worker->refcount); break;
}
}
spin_unlock(&vhost_workers_lock); returnfound_worker;
}
Nice. Will do.
Quoted 96 lines
Quoted 96 lines
drivers/vhost/vhost.c +53
-0+} + +/* Caller must have device mutex */ +static int vhost_vq_set_worker (struct vhost_virtqueue *vq, + struct vhost_vring_worker *info) +{ + struct vhost_dev *dev = vq->dev; + struct vhost_worker *worker; + + if (vq->worker) { + /* TODO - support changing while works are running */ + return -EBUSY; + } + + if (info->pid == -1) { + worker = vhost_worker_create(dev); + if (!worker) + return -ENOMEM; + + info->pid = worker->task->pid; + } else { + worker = vhost_worker_find(dev, info->pid); + if (!worker) + return -ENODEV; + } + + if (!dev->workers) { + dev->workers = kcalloc(vq->dev->nvqs, + sizeof(struct vhost_worker *), + GFP_KERNEL); + if (!dev->workers) { + vhost_worker_put(worker); + return -ENOMEM; + } + } + + vq->worker = worker; + + dev->workers[dev->num_workers] = worker; + dev->num_workers++; + return 0; +} +/* Caller must have device mutex */ static int vhost_worker_try_create_def (struct vhost_dev *dev) {@@ -1680,6 +1761,7 @@ long vhost_vring_ioctl(struct vhost_dev *d, unsigned int ioctl, void __user *arg struct eventfd_ctx *ctx = NULL; u32 __user *idxp = argp; struct vhost_virtqueue *vq; + struct vhost_vring_worker w; struct vhost_vring_state s; struct vhost_vring_file f; u32 idx;@@ -1794,6 +1876,15 @@ long vhost_vring_ioctl(struct vhost_dev *d, unsigned int ioctl, void __user *arg if (copy_to_user(argp, &s, sizeof(s))) r = -EFAULT; break; + case VHOST_SET_VRING_WORKER: + if (copy_from_user(&w, argp, sizeof(w))) { + r = -EFAULT; + break; + } + r = vhost_vq_set_worker(vq, &w); + if (!r && copy_to_user(argp, &w, sizeof(w))) + r = -EFAULT; + break;default:
r =-ENOIOCTLCMD;
}drivers/vhost/vhost.h +3
-0@@ -14,6 +14,7 @@#include <linux/atomic.h> #include <linux/vhost_iotlb.h> #include <linux/irqbypass.h> +#include <linux/refcount.h> struct vhost_work ;typedef void (*vhost_work_fn_t )(struct vhost_work *work);@@ -28,6 +29,8 @@ struct vhost_work {struct vhost_worker { struct task_struct *task; struct llist_head work_list; + struct list_head list; + refcount_t refcount; struct vhost_dev *dev; int id; };include/uapi/linux/vhost.h +0
-0
Quoted 8 lines
include/uapi/linux/vhost.h line 74
Quoted 6 lines
include/uapi/linux/vhost.h +2
-0@@ -70,6 +70,12 @@#define VHOST_VRING_BIG_ENDIAN 1#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) +/* Create/bind a vhost worker to a virtqueue. If pid > 0 and matches an existing +* vhost_worker thread it will be bound to the vq. If pid is -1, then a new What about adding a macro for -1? (e.g.
VHOST_VRING_NEW_WORKER)
Yeah, that is nicer than a magic number. Will do.
_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization
Mike Christie
maintainerRFC PATCH 12/14 vhost: add vhost_dev pointer to vhost_work
In reply to Mike ChristieThe next patch allows a vhost_worker to handle different devices. To prepare for that, this patch adds a pointer to the device on the work so we can get to the different mms in the vhost_worker thread.
--- drivers/vhost/scsi.c | 7 ++++--- drivers/vhost/vhost.c | 24 ++++++++++++++---------- drivers/vhost/vhost.h | 10 ++++++---- drivers/vhost/vsock.c | 3 ++- 4 files changed, 26 insertions(+), 18 deletions(-)
drivers/vhost/scsi.c +4 -3
@@ -1808,7 +1808,7 @@ static int vhost_scsi_open(struct inode *inode, struct file *f)if (!vqs) goto err_vqs; -vhost_work_init (&vs->vs_event_work, vhost_scsi_evt_work); +vhost_work_init (&vs->dev, &vs->vs_event_work, vhost_scsi_evt_work); vs->vs_events_nr =0 ; vs->vs_events_missed = false;
@@ -1823,7 +1823,7 @@ static int vhost_scsi_open(struct inode *inode, struct file *f) vqs[i] = &svq->vq; svq->vs = vs;init_llist_head (&svq->completion_list); -vhost_work_init (&svq->completion_work, +vhost_work_init (&vs->dev, &svq->completion_work, vhost_scsi_complete_cmd_work); svq->vq.handle_kick = vhost_scsi_handle_kick; }
@@ -2017,7 +2017,8 @@ static int vhost_scsi_port_link(struct se_portal_group *se_tpg,if (!tmf)return -ENOMEM ;INIT_LIST_HEAD (&tmf->queue_entry); -vhost_work_init (&tmf->vwork, vhost_scsi_tmf_resp_work); +vhost_work_init (&tpg->vhost_scsi->dev, &tmf->vwork, + vhost_scsi_tmf_resp_work);mutex_lock (&vhost_scsi_mutex);
drivers/vhost/vhost.c +14 -10
@@ -181,10 +181,12 @@ static int vhost_poll_wakeup(wait_queue_entry_t *wait, unsigned mode, int sync,return 0 ; } -void vhost_work_init (struct vhost_work *work,vhost_work_fn_t fn) +void vhost_work_init (struct vhost_dev *dev,struct vhost_work *work, +vhost_work_fn_t fn) {clear_bit (VHOST_WORK_QUEUED , &work->flags); work->fn = fn; + work->dev = dev; }EXPORT_SYMBOL_GPL (vhost_work_init);
@@ -200,7 +202,7 @@ void vhost_poll_init(struct vhost_poll *poll, vhost_work_fn_t fn, poll->wqh =NULL ; poll->vq = vq; -vhost_work_init (&poll->work, fn); +vhost_work_init (dev, &poll->work, fn); }EXPORT_SYMBOL_GPL (vhost_poll_init);
@@ -269,12 +271,13 @@ void vhost_work_queue(struct vhost_dev *dev, struct vhost_work *work) }EXPORT_SYMBOL_GPL (vhost_work_queue); -static void vhost_work_dev_flush_on (struct vhost_worker *worker) +static void vhost_work_dev_flush_on (struct vhost_dev *dev, +struct vhost_worker *worker) {struct vhost_flush_struct flush;init_completion (&flush.wait_event); -vhost_work_init (&flush.work, vhost_flush_work); +vhost_work_init (dev, &flush.work, vhost_flush_work);vhost_work_queue_on (&flush.work, worker);wait_for_completion (&flush.wait_event);
@@ -285,7 +288,7 @@ void vhost_work_dev_flush(struct vhost_dev *dev)int i;for (i =0 ; i < dev->num_workers; i++) -vhost_work_dev_flush_on (dev->workers[i]); +vhost_work_dev_flush_on (dev, dev->workers[i]); }EXPORT_SYMBOL_GPL (vhost_work_dev_flush);
@@ -305,7 +308,7 @@ EXPORT_SYMBOL_GPL(vhost_has_work);void vhost_vq_work_flush (struct vhost_virtqueue *vq) { -vhost_work_dev_flush_on (vq->worker); +vhost_work_dev_flush_on (vq->dev, vq->worker); }EXPORT_SYMBOL_GPL (vhost_vq_work_flush);
@@ -572,14 +575,15 @@ static void vhost_attach_cgroups_work(struct vhost_work *work) s->ret =cgroup_attach_task_all (s->owner, current); } -static int vhost_attach_cgroups_on (struct vhost_worker *worker) +static int vhost_attach_cgroups_on (struct vhost_dev *dev, +struct vhost_worker *worker) {struct vhost_attach_cgroups_struct attach; attach.owner = current; -vhost_work_init (&attach.work, vhost_attach_cgroups_work); +vhost_work_init (dev, &attach.work, vhost_attach_cgroups_work);vhost_work_queue_on (&attach.work, worker); -vhost_work_dev_flush_on (worker); +vhost_work_dev_flush_on (dev, worker);return attach.ret; }
@@ -673,7 +677,7 @@ static struct vhost_worker *vhost_worker_create(struct vhost_dev *dev) worker->task = task;wake_up_process (task);/* avoid contributing to loadavg */ - ret =vhost_attach_cgroups_on (worker); + ret =vhost_attach_cgroups_on (dev, worker);if (ret) goto stop_worker;
drivers/vhost/vhost.h +6 -4
@@ -21,9 +21,10 @@ typedef void (*vhost_work_fn_t)(struct vhost_work *work);#define VHOST_WORK_QUEUED 1struct vhost_work { -struct llist_node node; -vhost_work_fn_t fn; -unsigned long flags; +struct llist_node node; +vhost_work_fn_t fn; +unsigned long flags; +struct vhost_dev *dev; };struct vhost_worker {
@@ -47,7 +48,8 @@ struct vhost_poll {struct vhost_virtqueue *vq; }; -void vhost_work_init (struct vhost_work *work,vhost_work_fn_t fn); +void vhost_work_init (struct vhost_dev *dev,struct vhost_work *work, +vhost_work_fn_t fn);void vhost_work_queue (struct vhost_dev *dev,struct vhost_work *work);bool vhost_has_work (struct vhost_dev *dev);void vhost_vq_work_flush (struct vhost_virtqueue *vq);
drivers/vhost/vsock.c +2 -1
@@ -648,7 +648,8 @@ static int vhost_vsock_dev_open(struct inode *inode, struct file *file) file->private_data = vsock;spin_lock_init (&vsock->send_pkt_list_lock);INIT_LIST_HEAD (&vsock->send_pkt_list); -vhost_work_init (&vsock->send_pkt_work, vhost_transport_send_pkt_work); +vhost_work_init (&vsock->dev, &vsock->send_pkt_work, + vhost_transport_send_pkt_work);return 0 ; out:
Signature · 6 lines
2.25.1 _______________________________________________ Virtualization mailing list [email protected] https://lists.linuxfoundation.org/mailman/listinfo/virtualization
Stefano Garzarella
maintainerRe: [PATCH RFC 12/14] vhost: add vhost_dev pointer to vhost_work
Quoted 35 lines · Mike Christie
The next patch allows a
vhost_workerto handle different devices. To prepare for that, this patch adds a pointer to the device on the work so we can get to the different mms in thevhost_workerthread.Signed-off-by Mike Christie <[email protected]>---
drivers/vhost/scsi.c | 7 ++++--- drivers/vhost/vhost.c | 24 ++++++++++++++---------- drivers/vhost/vhost.h | 10 ++++++---- drivers/vhost/vsock.c | 3 ++- 4 files changed, 26 insertions(+), 18 deletions(-)drivers/vhost/scsi.c +2
-2@@ -1808,7 +1808,7 @@ static int vhost_scsi_open(struct inode *inode, struct file *f)if (!vqs) goto err_vqs; -vhost_work_init (&vs->vs_event_work, vhost_scsi_evt_work); +vhost_work_init (&vs->dev, &vs->vs_event_work, vhost_scsi_evt_work); vs->vs_events_nr =0 ; vs->vs_events_missed = false;@@ -1823,7 +1823,7 @@ static int vhost_scsi_open(struct inode *inode, struct file *f) vqs[i] = &svq->vq; svq->vs = vs;init_llist_head (&svq->completion_list); -vhost_work_init (&svq->completion_work, +vhost_work_init (&vs->dev, &svq->completion_work, vhost_scsi_complete_cmd_work); svq->vq.handle_kick = vhost_scsi_handle_kick; }
drivers/vhost/scsi.c static int vhost_scsi_port_link(struct se_portal_group *se_tpg, · line 2022
Quoted 7 lines
drivers/vhost/scsi.c +2
-1@@ -2017,7 +2017,8 @@ static int vhost_scsi_port_link(struct se_portal_group *se_tpg,if (!tmf)return -ENOMEM ;INIT_LIST_HEAD (&tmf->queue_entry); -vhost_work_init (&tmf->vwork, vhost_scsi_tmf_resp_work); +vhost_work_init (&tpg->vhost_scsi->dev, &tmf->vwork, + vhost_scsi_tmf_resp_work);
checkpatch.pl --strict complains here:
CHECK: Alignment should match open parenthesis
#74: FILE: drivers/vhost/scsi.c:2036:
+ vhost_work_init(&tpg->vhost_scsi->dev, &tmf->vwork,
+ vhost_scsi_tmf_resp_work);
Quoted 97 lines
drivers/vhost/scsi.c +0
-0mutex_lock (&vhost_scsi_mutex);drivers/vhost/vhost.c +14
-10@@ -181,10 +181,12 @@ static int vhost_poll_wakeup(wait_queue_entry_t *wait, unsigned mode, int sync,return 0 ; } -void vhost_work_init (struct vhost_work *work,vhost_work_fn_t fn) +void vhost_work_init (struct vhost_dev *dev,struct vhost_work *work, +vhost_work_fn_t fn) {clear_bit (VHOST_WORK_QUEUED , &work->flags); work->fn = fn; + work->dev = dev; }EXPORT_SYMBOL_GPL (vhost_work_init);@@ -200,7 +202,7 @@ void vhost_poll_init(struct vhost_poll *poll, vhost_work_fn_t fn, poll->wqh =NULL ; poll->vq = vq; -vhost_work_init (&poll->work, fn); +vhost_work_init (dev, &poll->work, fn); }EXPORT_SYMBOL_GPL (vhost_poll_init);@@ -269,12 +271,13 @@ void vhost_work_queue(struct vhost_dev *dev, struct vhost_work *work) }EXPORT_SYMBOL_GPL (vhost_work_queue); -static void vhost_work_dev_flush_on (struct vhost_worker *worker) +static void vhost_work_dev_flush_on (struct vhost_dev *dev, +struct vhost_worker *worker) {struct vhost_flush_struct flush;init_completion (&flush.wait_event); -vhost_work_init (&flush.work, vhost_flush_work); +vhost_work_init (dev, &flush.work, vhost_flush_work);vhost_work_queue_on (&flush.work, worker);wait_for_completion (&flush.wait_event);@@ -285,7 +288,7 @@ void vhost_work_dev_flush(struct vhost_dev *dev)int i;for (i =0 ; i < dev->num_workers; i++) -vhost_work_dev_flush_on (dev->workers[i]); +vhost_work_dev_flush_on (dev, dev->workers[i]); }EXPORT_SYMBOL_GPL (vhost_work_dev_flush);@@ -305,7 +308,7 @@ EXPORT_SYMBOL_GPL(vhost_has_work);void vhost_vq_work_flush (struct vhost_virtqueue *vq) { -vhost_work_dev_flush_on (vq->worker); +vhost_work_dev_flush_on (vq->dev, vq->worker); }EXPORT_SYMBOL_GPL (vhost_vq_work_flush);@@ -572,14 +575,15 @@ static void vhost_attach_cgroups_work(struct vhost_work *work) s->ret =cgroup_attach_task_all (s->owner, current); } -static int vhost_attach_cgroups_on (struct vhost_worker *worker) +static int vhost_attach_cgroups_on (struct vhost_dev *dev, +struct vhost_worker *worker) {struct vhost_attach_cgroups_struct attach; attach.owner = current; -vhost_work_init (&attach.work, vhost_attach_cgroups_work); +vhost_work_init (dev, &attach.work, vhost_attach_cgroups_work);vhost_work_queue_on (&attach.work, worker); -vhost_work_dev_flush_on (worker); +vhost_work_dev_flush_on (dev, worker);return attach.ret; }@@ -673,7 +677,7 @@ static struct vhost_worker *vhost_worker_create(struct vhost_dev *dev) worker->task = task;wake_up_process (task);/* avoid contributing to loadavg */ - ret =vhost_attach_cgroups_on (worker); + ret =vhost_attach_cgroups_on (dev, worker);if (ret) goto stop_worker;drivers/vhost/vhost.h +0
-0
drivers/vhost/vhost.h typedef void (*vhost_work_fn_t)(struct vhost_work *work); · line 26
Quoted 9 lines
drivers/vhost/vhost.h +3
-3@@ -21,9 +21,10 @@ typedef void (*vhost_work_fn_t)(struct vhost_work *work);#define VHOST_WORK_QUEUED 1struct vhost_work { -struct llist_node node; -vhost_work_fn_t fn; -unsigned long flags; +struct llist_node node; +vhost_work_fn_t fn; +unsigned long flags;
Maybe we should move these changes in another patch since they are not related.
Thanks,
Stefano
Quoted 27 lines
drivers/vhost/vhost.h +3
-1+struct vhost_dev *dev; };struct vhost_worker {@@ -47,7 +48,8 @@ struct vhost_poll {struct vhost_virtqueue *vq; }; -void vhost_work_init (struct vhost_work *work,vhost_work_fn_t fn); +void vhost_work_init (struct vhost_dev *dev,struct vhost_work *work, +vhost_work_fn_t fn);void vhost_work_queue (struct vhost_dev *dev,struct vhost_work *work);bool vhost_has_work (struct vhost_dev *dev);void vhost_vq_work_flush (struct vhost_virtqueue *vq);drivers/vhost/vsock.c +2
-1@@ -648,7 +648,8 @@ static int vhost_vsock_dev_open(struct inode *inode, struct file *file) file->private_data = vsock;spin_lock_init (&vsock->send_pkt_list_lock);INIT_LIST_HEAD (&vsock->send_pkt_list); -vhost_work_init (&vsock->send_pkt_work, vhost_transport_send_pkt_work); +vhost_work_init (&vsock->dev, &vsock->send_pkt_work, + vhost_transport_send_pkt_work);return 0 ;
Quoted 4 lines
out:
Signature · 1 line
2.25.1
_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization
Mike Christie
maintainerRe: [PATCH RFC 12/14] vhost: add vhost_dev pointer to vhost_work
Quoted 46 lines · Stefano Garzarella
Quoted 35 lines · Mike Christie
The next patch allows a
vhost_workerto handle different devices. To prepare for that, this patch adds a pointer to the device on the work so we can get to the different mms in thevhost_workerthread.Signed-off-by Mike Christie <[email protected]>---
drivers/vhost/scsi.c | 7 ++++---drivers/vhost/vhost.c | 24 ++++++++++++++---------- drivers/vhost/vhost.h | 10 ++++++---- drivers/vhost/vsock.c | 3 ++- 4 files changed, 26 insertions(+), 18 deletions(-)drivers/vhost/scsi.c +2
-2@@ -1808,7 +1808,7 @@ static int vhost_scsi_open(struct inode *inode, struct file *f) if (!vqs) goto err_vqs; - vhost_work_init(&vs->vs_event_work, vhost_scsi_evt_work); + vhost_work_init(&vs->dev, &vs->vs_event_work, vhost_scsi_evt_work); vs->vs_events_nr = 0; vs->vs_events_missed = false;@@ -1823,7 +1823,7 @@ static int vhost_scsi_open(struct inode *inode, struct file *f) vqs[i] = &svq->vq; svq->vs = vs; init_llist_head(&svq->completion_list); - vhost_work_init(&svq->completion_work, + vhost_work_init(&vs->dev, &svq->completion_work, vhost_scsi_complete_cmd_work); svq->vq.handle_kick = vhost_scsi_handle_kick; }drivers/vhost/scsi.c static int vhost_scsi_port_link(struct se_portal_group *se_tpg, · line 2022
Quoted 7 lines
drivers/vhost/scsi.c +2
-1@@ -2017,7 +2017,8 @@ static int vhost_scsi_port_link(struct se_portal_group *se_tpg, if (!tmf) return -ENOMEM; INIT_LIST_HEAD(&tmf->queue_entry); - vhost_work_init(&tmf->vwork, vhost_scsi_tmf_resp_work); + vhost_work_init(&tpg->vhost_scsi->dev, &tmf->vwork, + vhost_scsi_tmf_resp_work);
checkpatch.pl --strictcomplains here:
Quoted 5 lines
CHECK: Alignment should match open parenthesis #74: FILE:
drivers/vhost/scsi.c:2036:
+vhost_work_init(&tpg->vhost_scsi->dev, &tmf->vwork, + vhost_scsi_tmf_resp_work);
Will fix and use strict from now on too.
Quoted 97 lines
Quoted 97 lines
drivers/vhost/scsi.c +0
-0mutex_lock(&vhost_scsi_mutex);drivers/vhost/vhost.c +13
-9@@ -181,10 +181,12 @@ static int vhost_poll_wakeup(wait_queue_entry_t *wait, unsigned mode, int sync, return 0; } -void vhost_work_init (struct vhost_work *work,vhost_work_fn_t fn) +void vhost_work_init (struct vhost_dev *dev,struct vhost_work *work, + vhost_work_fn_t fn) { clear_bit(VHOST_WORK_QUEUED, &work->flags); work->fn = fn; + work->dev = dev; }EXPORT_SYMBOL_GPL (vhost_work_init);@@ -200,7 +202,7 @@ void vhost_poll_init(struct vhost_poll *poll, vhost_work_fn_t fn, poll->wqh = NULL; poll->vq = vq; - vhost_work_init(&poll->work, fn); + vhost_work_init(dev, &poll->work, fn); }EXPORT_SYMBOL_GPL (vhost_poll_init);@@ -269,12 +271,13 @@ void vhost_work_queue(struct vhost_dev *dev, struct vhost_work *work) }EXPORT_SYMBOL_GPL (vhost_work_queue); -static void vhost_work_dev_flush_on (struct vhost_worker *worker) +static void vhost_work_dev_flush_on (struct vhost_dev *dev, + struct vhost_worker *worker) { struct vhost_flush_struct flush; init_completion(&flush.wait_event); - vhost_work_init(&flush.work, vhost_flush_work); + vhost_work_init(dev, &flush.work, vhost_flush_work); vhost_work_queue_on(&flush.work, worker); wait_for_completion(&flush.wait_event);@@ -285,7 +288,7 @@ void vhost_work_dev_flush(struct vhost_dev *dev) int i; for (i = 0; i < dev->num_workers; i++) - vhost_work_dev_flush_on(dev->workers[i]); + vhost_work_dev_flush_on(dev, dev->workers[i]); }EXPORT_SYMBOL_GPL (vhost_work_dev_flush);@@ -305,7 +308,7 @@ EXPORT_SYMBOL_GPL(vhost_has_work);void vhost_vq_work_flush (struct vhost_virtqueue *vq) { - vhost_work_dev_flush_on(vq->worker); + vhost_work_dev_flush_on(vq->dev, vq->worker); }EXPORT_SYMBOL_GPL (vhost_vq_work_flush);@@ -572,14 +575,15 @@ static void vhost_attach_cgroups_work(struct vhost_work *work) s->ret = cgroup_attach_task_all(s->owner, current); } -static int vhost_attach_cgroups_on (struct vhost_worker *worker) +static int vhost_attach_cgroups_on (struct vhost_dev *dev, + struct vhost_worker *worker) { struct vhost_attach_cgroups_struct attach; attach.owner = current; - vhost_work_init(&attach.work, vhost_attach_cgroups_work); + vhost_work_init(dev, &attach.work, vhost_attach_cgroups_work); vhost_work_queue_on(&attach.work, worker); - vhost_work_dev_flush_on(worker); + vhost_work_dev_flush_on(dev, worker); return attach.ret; }@@ -673,7 +677,7 @@ static struct vhost_worker *vhost_worker_create(struct vhost_dev *dev) worker->task = task;wake_up_process(task); /* avoid contributing to loadavg */
- ret =
vhost_attach_cgroups_on(worker);
+ ret =vhost_attach_cgroups_on(dev, worker);
if (ret)
gotostop_worker;drivers/vhost/vhost.h +0
-0@@ -21,9 +21,10 @@ typedef void (*vhost_work_fn_t)(struct vhost_work *work);
Quoted 10 lines
drivers/vhost/vhost.h typedef void (*vhost_work_fn_t)(struct vhost_work *work); · line 26
Quoted 8 lines
drivers/vhost/vhost.h +3
-3#define VHOST_WORK_QUEUED 1struct vhost_work { - struct llist_node node; - vhost_work_fn_t fn; - unsigned long flags; + struct llist_node node; + vhost_work_fn_t fn; + unsigned long flags;Maybe we should move these changes in another patch since they are not related.
Will do.
_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization
Mike Christie
maintainerRFC PATCH 13/14 vhost: support sharing workers across devs
In reply to Mike ChristieThis allows a worker to handle multiple device's vqs.
TODO:
- How to handle if the devices are in different cgroups/VMs.
--- drivers/vhost/vhost.c | 18 +++++++----------- drivers/vhost/vhost.h | 1 - 2 files changed, 7 insertions(+), 12 deletions(-)
drivers/vhost/vhost.c +7 -11
@@ -387,12 +387,10 @@ static void vhost_vq_reset(struct vhost_dev *dev,static int vhost_worker (void *data) {struct vhost_worker *worker = data; -struct vhost_dev *dev = worker->dev;struct vhost_work *work, *work_next; +struct vhost_dev *dev;struct llist_node *node; -kthread_use_mm (dev->mm); -for (;;) {/* mb paired w/ kthread_stop */ set_current_state (TASK_INTERRUPTIBLE );
@@ -411,15 +409,20 @@ static int vhost_worker(void *data)smp_wmb ();llist_for_each_entry_safe (work, work_next, node, node) {clear_bit (VHOST_WORK_QUEUED , &work->flags); + dev = work->dev; + +kthread_use_mm (dev->mm); +__set_current_state (TASK_RUNNING );kcov_remote_start_common (dev->kcov_handle); work->fn (work);kcov_remote_stop ();if (need_resched ())schedule (); + +kthread_unuse_mm (dev->mm); } } -kthread_unuse_mm (dev->mm);return 0 ; }
@@ -665,7 +668,6 @@ static struct vhost_worker *vhost_worker_create(struct vhost_dev *dev)return NULL ; worker->id = dev->num_workers; - worker->dev = dev;init_llist_head (&worker->work_list);INIT_LIST_HEAD (&worker->list);refcount_set (&worker->refcount,1 );
@@ -703,12 +705,6 @@ static struct vhost_worker *vhost_worker_find(struct vhost_dev *dev, pid_t pid)if (worker->task->pid != pid)continue ; -/* tmp - next patch allows sharing across devs */ -if (worker->dev != dev) { -spin_unlock (&vhost_workers_lock); -return NULL ; - } -refcount_inc (&worker->refcount);spin_unlock (&vhost_workers_lock);return worker;
drivers/vhost/vhost.h +0 -1
@@ -32,7 +32,6 @@ struct vhost_worker {struct llist_head work_list;struct list_head list;refcount_t refcount; -struct vhost_dev *dev;int id; };
Signature · 6 lines
2.25.1 _______________________________________________ Virtualization mailing list [email protected] https://lists.linuxfoundation.org/mailman/listinfo/virtualization
Mike Christie
maintainerRFC PATCH 14/14 vhost: allow userspace to query vq worker mappings
In reply to Mike ChristieAdd an ioctl cmd to allow userspace to figure out the vq's worker.
--- drivers/vhost/vhost.c | 10 ++++++++++ include/uapi/linux/vhost.h | 3 ++- 2 files changed, 12 insertions(+), 1 deletion(-)
drivers/vhost/vhost.c +10 -0
@@ -1885,6 +1885,16 @@ long vhost_vring_ioctl(struct vhost_dev *d, unsigned int ioctl, void __user *argif (!r &©_to_user (argp, &w,sizeof (w))) r = -EFAULT ;break ; +case VHOST_GET_VRING_WORKER : + w.index = idx; + w.pid =-1 ; + +if (vq->worker) + w.pid = vq->worker->task->pid; + +if (copy_to_user (argp, &w,sizeof (w))) + r = -EFAULT ; +break ;default : r = -ENOIOCTLCMD ; }
include/uapi/linux/vhost.h +2 -1
@@ -75,7 +75,8 @@* worker will be created and bound to the vq. */ #define VHOST_SET_VRING_WORKER _IOWR(VHOST_VIRTIO, 0x15, struct vhost_vring_worker) - +/* Return the vqs worker's pid. If no worker is set pid is -1 */ +#define VHOST_GET_VRING_WORKER _IOR(VHOST_VIRTIO, 0x16, struct vhost_vring_worker)/* The following ioctls use eventfd file descriptors to signal and poll * for events. */
Signature · 6 lines
2.25.1 _______________________________________________ Virtualization mailing list [email protected] https://lists.linuxfoundation.org/mailman/listinfo/virtualization
Stefano Garzarella
maintainerHi Mike,
Quoted 4 lines · Mike Christie
The following patches apply over mst's vhost branch and were tested againt that branch and also mkp's 5.13 branch which has some vhost-scsi changes.
Quoted 8 lines
These patches allow us to support multiple vhost workers per device. I ended up just doing Stefan's original idea where userspace has the kernel create a worker and we pass back the pid. This has the benefit over the workqueue and userspace thread approach where we only have one'ish code path in the kernel.
The kernel patches here allow us to then do N workers device and also share workers across devices.
I took a first look and left a few comments.
In general it looks good to me, I'm just worried if it's okay to use the kthread pid directly or it's better to use an internal id.
For example I think if this can be affected by the pid namespaces or some security issues. I admit I don't know much about this topic, so this might be silly.
Quoted 14 lines
I included a patch for qemu so you can get an idea of how it works.
TODO:
-----
- polling
- Allow sharing workers across devices. Kernel support is added and I hacked up userspace to test, but I'm still working on a sane way to manage it in userspace.
- Bind to specific CPUs. Commands like "virsh emulatorpin" work with these patches and allow us to set the group of vhost threads to different CPUs. But we can also set a specific vq's worker to run on a CPU.
- I'm handling old kernel by just checking for EPERM. Does this require a feature?
Do you mean when the new ioctls are not available? We do not return ENOIOCTLCMD?
In this case I think is fine to check the return value.
Thanks,
Stefano
_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization
Mike Christie
maintainerQuoted 16 lines · Stefano Garzarella
Hi Mike,
Quoted 4 lines · Mike Christie
The following patches apply over mst's vhost branch and were tested againt that branch and also mkp's 5.13 branch which has some vhost-scsi changes.
Quoted 8 lines
These patches allow us to support multiple vhost workers per device. I ended up just doing Stefan's original idea where userspace has the kernel create a worker and we pass back the pid. This has the benefit over the workqueue and userspace thread approach where we only have one'ish code path in the kernel.
The kernel patches here allow us to then do N workers device and also share workers across devices.
Quoted 7 lines
I took a first look and left a few comments.
In general it looks good to me, I'm just worried if it's okay to use the kthread pid directly or it's better to use an internal id.
For example I think if this can be affected by the pid namespaces or some security issues. I admit I don't know much about this topic, so this might be silly.
Ah yeah, that was my other TODO. I did the lazy loop and left the "hash on pid" TODO in patch 11 because I was not sure. I thought it was ok, because only the userspace process that does the VHOST_SET_OWNER call can do the other ioctls.
I can do pid or use a xarray/ida/idr type if struct and pass that id between user/kernel space if it's preferred.
Quoted 15 lines
Quoted 14 lines
I included a patch for qemu so you can get an idea of how it works.
TODO:
-----
- polling
- Allow sharing workers across devices. Kernel support is added and I hacked up userspace to test, but I'm still working on a sane way to manage it in userspace.
- Bind to specific CPUs. Commands like "virsh emulatorpin" work with these patches and allow us to set the group of vhost threads to different CPUs. But we can also set a specific vq's worker to run on a CPU.
- I'm handling old kernel by just checking for EPERM. Does this require a feature?
Quoted 1 line
Do you mean when the new ioctls are not available? We do not return ENOIOCTLCMD?
vhost_vring_ioctl returns -ENOIOCTLCMD but that does not get propagated to the app. Check out the comment in include/linux/errno.h and fs/ioctl.c:vfs_ioctl() where -ENOIOCTLCMD is caught and -ENOTTY is returned.
To make this reply a little complicated note that at the time when I wrote the code I thought something was translating -ENOTTY to -EPERM but then after posting I realized that ioctl() always returns -1 on error (I thought the -1 was a -EPERM from the kernel). errno is set to -ENOTTY as expected when a ioctl is not implemented, so the -EPERM references should be errno and -ENOTTY.
_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization
Stefano Garzarella
maintainerQuoted 21 lines · Mike Christie
Quoted 20 lines · Stefano Garzarella
Hi Mike,
Quoted 4 lines · Mike Christie
The following patches apply over mst's vhost branch and were tested againt that branch and also mkp's 5.13 branch which has some vhost-scsi changes.
Quoted 8 lines
These patches allow us to support multiple vhost workers per device. I ended up just doing Stefan's original idea where userspace has the kernel create a worker and we pass back the pid. This has the benefit over the workqueue and userspace thread approach where we only have one'ish code path in the kernel.
The kernel patches here allow us to then do N workers device and also share workers across devices.
I took a first look and left a few comments.
In general it looks good to me, I'm just worried if it's okay to use the kthread pid directly or it's better to use an internal id.
Quoted 8 lines
Quoted 3 lines
For example I think if this can be affected by the pid namespaces or some security issues. I admit I don't know much about this topic, so this might be silly.
Ah yeah, that was my other TODO. I did the lazy loop and left the "hash on pid" TODO in patch 11 because I was not sure. I thought it was ok, because only the userspace process that does the
VHOST_SET_OWNERcall can do the other ioctls.
Oh I see.
Quoted 4 lines
I can do pid or use a xarray/ida/idr type if struct and pass that id between user/kernel space if it's preferred.
Let's see what others say, it was just a doubt I had while looking at the patches.
Quoted 16 lines
Quoted 16 lines
Quoted 15 lines
I included a patch for qemu so you can get an idea of how it works.
TODO:
-----
- polling
- Allow sharing workers across devices. Kernel support is added and I hacked up userspace to test, but I'm still working on a sane way to manage it in userspace.
- Bind to specific CPUs. Commands like "virsh emulatorpin" work with these patches and allow us to set the group of vhost threads to different CPUs. But we can also set a specific vq's worker to run on a CPU.
- I'm handling old kernel by just checking for EPERM. Does this require a feature?
Quoted 4 lines
Quoted 1 line
Do you mean when the new ioctls are not available? We do not return ENOIOCTLCMD?
vhost_vring_ioctlreturns-ENOIOCTLCMDbut that does not get propagated to the app. Check out the comment ininclude/linux/errno.hand fs/ioctl.c:vfs_ioctl() where-ENOIOCTLCMDis caught and-ENOTTYis returned.
Ah right!
Quoted 7 lines
To make this reply a little complicated note that at the time when I wrote the code I thought something was translating
-ENOTTYto-EPERMbut then after posting I realized thatioctl()always returns -1 on error (I thought the -1 was a-EPERMfrom the kernel). errno is set to-ENOTTYas expected when a ioctl is not implemented, so the-EPERMreferences should be errno and-ENOTTY.
Thanks for the clarification.
However looking better maybe it would make sense to add a new VHOST_BACKEND_F_* feature bit for this functionality since we are adding 2 new ioctls to use together.
I saw that we used the VHOST_BACKEND_F_* only for the new IOTLB features, so maybe for this functionality the error returned by ioctl() could be enough. Let's see what others think.
Thanks,
Stefano
_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization