vhost: Add support of kthread API
Subsystems (2)
v2 cover vhost: Add support of kthread API
In commit 6e890c5d5021 ("vhost: use vhost_tasks for worker threads"),
vhost is now a child of the owner thread. but some old userspace is confused, In these patch we will let userspace to select the mode
In this PATCH, the support of kthread is added back.
a module_param is added to select which mode we are using, and a new UAPI is introduced to allow the userspace app to set which mode they want to use.
Changelog v2:
1. Change the module_param's name to enforce_inherit_owner, and the default value is true.
2. Change the UAPI's name to VHOST_SET_INHERIT_FROM_OWNER.
Tested with QEMU.
Cindy Lu (7):
vhost: Add a new modparam to allow userspace select vhost_task
vhost: Add kthread support in function vhost_worker_queue()
vhost: Add kthread support in function vhost_workers_free()
vhost: Add the vhost_worker to support kthread
vhost: Add the cgroup related function
vhost: Add kthread support in function vhost_worker_create
vhost: Add new UAPI to support change to task mode
drivers/vhost/vhost.c | 249 +++++++++++++++++++++++++++++++++++-- drivers/vhost/vhost.h | 1 + include/uapi/linux/vhost.h | 2 + 3 files changed, 243 insertions(+), 9 deletions(-)
Signature · 1 line
2.45.0
PATCH v2 1/7 vhost: Add a new modparam to allow userspace select vhost_task
The vhost is now using vhost_task and working as a child of the owner thread. While this makes sense from containerization POV, some old userspace is confused, as previously vhost not and so was allowed to steal cpu resources from outside the container. So we add the kthread API support back
Add a new module parameter to allow userspace to select behaviour between using kthread and task
--- drivers/vhost/vhost.c | 4 ++++ 1 file changed, 4 insertions(+)
drivers/vhost/vhost.c +4 -0
@@ -41,6 +41,10 @@ static int max_iotlb_entries = 2048;module_param (max_iotlb_entries, int,0444 );MODULE_PARM_DESC (max_iotlb_entries,"Maximum number of iotlb entries. (default: 2048)" ); +bool enforce_inherit_owner = true; +module_param (enforce_inherit_owner, bool,0444 ); +MODULE_PARM_DESC (enforce_inherit_owner, +"enforce vhost use vhost_task(default: Y)" );enum {VHOST_MEMORY_F_LOG =0x1 ,
Signature · 1 line
2.45.0
kernel test robot
build botRe: [PATCH v2 1/7] vhost: Add a new modparam to allow userspace select vhost_task
kernel test robot noticed the following build warnings:
Hi Cindy,
kernel test robot noticed the following build warnings:
[auto build test WARNING on mst-vhost/linux-next]
[also build test WARNING on linus/master v6.12-rc1 next-20241004] [If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]
url: https://github.com/intel-lab-lkp/linux/commits/Cindy-Lu/vhost-Add-a-new-modparam-to-allow-userspace-select-vhost_task/20241004-100307 base: https://git.kernel.org/pub/scm/linux/kernel/git/mst/vhost.git linux-next patch link: https://lore.kernel.org/r/20241004015937.2286459-2-lulu%40redhat.com patch subject: [PATCH v2 1/7] vhost: Add a new modparam to allow userspace select vhost_task config: x86_64-randconfig-122-20241005 (https://download.01.org/0day-ci/archive/20241005/[email protected]/config) compiler: clang version 18.1.8 (https://github.com/llvm/llvm-project 3b5b5c1ec4a3095ab096dd780e84d7ab81f3d7ff) reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20241005/[email protected]/reproduce)
If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/
sparse warnings: (new ones prefixed by >>)
>> drivers/vhost/vhost.c:44:6: sparse: sparse: symbol 'enforce_inherit_owner' was not declared. Should it be static?
drivers/vhost/vhost.c:1899:54: sparse: sparse: self-comparison always evaluates to false drivers/vhost/vhost.c:1900:54: sparse: sparse: self-comparison always evaluates to false drivers/vhost/vhost.c:1901:55: sparse: sparse: self-comparison always evaluates to false drivers/vhost/vhost.c:2156:46: sparse: sparse: self-comparison always evaluates to false drivers/vhost/vhost.c:2236:48: sparse: sparse: self-comparison always evaluates to false drivers/vhost/vhost.c: note: in included file (through include/linux/wait.h, include/linux/eventfd.h):
include/linux/list.h:83:21: sparse: sparse: self-comparison always evaluates to true
vim +/enforce_inherit_owner +44 drivers/vhost/vhost.c
35
36 static ushort max_mem_regions = 64;
37 module_param(max_mem_regions, ushort, 0444);
38 MODULE_PARM_DESC(max_mem_regions,
39 "Maximum number of memory regions in memory map. (default: 64)");
40 static int max_iotlb_entries = 2048;
41 module_param(max_iotlb_entries, int, 0444);
42 MODULE_PARM_DESC(max_iotlb_entries,
43 "Maximum number of iotlb entries. (default: 2048)");
> 44 bool enforce_inherit_owner = true;
45 module_param(enforce_inherit_owner, bool, 0444);
46 MODULE_PARM_DESC(enforce_inherit_owner,
47 "enforce vhost use vhost_task(default: Y)");
48 Signature · 2 lines
0-DAY CI Kernel Test Service https://github.com/intel/lkp-tests/wiki
Re: [PATCH v2 1/7] vhost: Add a new modparam to allow userspace select vhost_task
In reply to Cindy LuQuoted 3 lines · Cindy Lu
The vhost is now using
vhost_taskand working as a child of the owner thread. While this makes sense from containerization POV, some old userspace is confused, as previously vhost not
not what?
Quoted 2 lines
and so was allowed to steal cpu resources
from outside the container. So we add the kthread API support back
Sorry, but it's not clear the reason.
I understand that we want to provide a way to bring back the previous behavior when we had kthreads, but why do we want that? Do you have examples where the new mechanism is causing problems?
Quoted 14 lines
Add a new module parameter to allow userspace to select behaviour between using kthread and task
Signed-off-by Cindy Lu <[email protected]>---
drivers/vhost/vhost.c| 4 ++++1 file changed, 4 insertions(+)drivers/vhost/vhost.c +0
-0
drivers/vhost/vhost.c static int max_iotlb_entries = 2048; · line 44
Quoted 4 lines
drivers/vhost/vhost.c +1
-0@@ -41,6 +41,10 @@ static int max_iotlb_entries = 2048;module_param (max_iotlb_entries, int,0444 );MODULE_PARM_DESC (max_iotlb_entries,"Maximum number of iotlb entries. (default: 2048)" ); +bool enforce_inherit_owner = true;
^
This should be static:
$ make -j6 O=build C=2 drivers/vhost/ ... CHECK ../drivers/vhost/vhost.c ../drivers/vhost/vhost.c:45:6: warning: symbol 'enforce_inherit_owner' was not declared. Should it be static?
drivers/vhost/vhost.c static int max_iotlb_entries = 2048; · line 47
Quoted 3 lines
drivers/vhost/vhost.c +3
-0+module_param (enforce_inherit_owner, bool,0444 ); +MODULE_PARM_DESC (enforce_inherit_owner, +"enforce vhost use vhost_task(default: Y)" );
I would follow the style of the other 2 parameters:
"Enforce vhost use vhost_task. (default: Y)"
With a view to simplifying bisection, we added this parameter in this patch, but it does nothing, so IMHO we should only add it at the end of the series when we have all the code ready. Maybe you can just add enforce_inherit_owner here or in the first patch where you need it, but I'd expose it with module_param() only when we have all the pieces in place.
About the param name, I'm not sure "enforce" is the right word, since IIUC the user can still change it using the ioctl. It would seem that set enforce_inherit_owner to true, it is always forced, but instead ioctl allows you to change it, right?
Is it more of a default behavior?
Something like inherit_owner_default ?
Thanks,
Stefano
Re: [PATCH v2 1/7] vhost: Add a new modparam to allow userspace select vhost_task
Quoted 8 lines · Stefano Garzarella
Quoted 3 lines · Cindy Lu
The vhost is now using
vhost_taskand working as a child of the owner thread. While this makes sense from containerization POV, some old userspace is confused, as previously vhost notnot what?
Quoted 9 lines
Quoted 2 lines
and so was allowed to steal cpu resources
from outside the container. So we add the kthread API support backSorry, but it's not clear the reason.
I understand that we want to provide a way to bring back the previous behavior when we had kthreads, but why do we want that?
Do you have examples where the new mechanism is causing problems?
The main difference is whose (kthreadd or the owner) attributes (namespace, affinities) would vhost thread inherit.
The owner's attributes tend to be different from kthreadd, so management might be surprised for example, vhost might be created in different namespaces or having different scheduling affinities.
Thanks
Re: [PATCH v2 1/7] vhost: Add a new modparam to allow userspace select vhost_task
Quoted 19 lines · Jason Wang
Quoted 8 lines · Stefano Garzarella
Quoted 3 lines · Cindy Lu
The vhost is now using
vhost_taskand working as a child of the owner thread. While this makes sense from containerization POV, some old userspace is confused, as previously vhost notnot what?
Quoted 9 lines
Quoted 2 lines
and so was allowed to steal cpu resources
from outside the container. So we add the kthread API support backSorry, but it's not clear the reason.
I understand that we want to provide a way to bring back the previous behavior when we had kthreads, but why do we want that?
Do you have examples where the new mechanism is causing problems?
Quoted 6 lines
The main difference is whose (kthreadd or the owner) attributes (namespace, affinities) would vhost thread inherit.
The owner's attributes tend to be different from kthreadd, so management might be surprised for example, vhost might be created in different namespaces or having different scheduling affinities.
Okay, so this requires some API to allow the managment to understand how the device vhost will be created.
But why do we need to restore the old behavior with a kthread where the resource accounting is completely disconnected from userspace? For the old managments that don't expect this?
BTW I would suggest adding all this information in this commit, in the parameter/IOCTL documentation, and in the cover letter because IMHO it is very important.
Thanks,
Stefano
Re: [PATCH v2 1/7] vhost: Add a new modparam to allow userspace select vhost_task
Quoted 28 lines · Stefano Garzarella
Quoted 19 lines · Jason Wang
Quoted 8 lines · Stefano Garzarella
Quoted 3 lines · Cindy Lu
The vhost is now using
vhost_taskand working as a child of the owner thread. While this makes sense from containerization POV, some old userspace is confused, as previously vhost notnot what?
Quoted 9 lines
Quoted 2 lines
and so was allowed to steal cpu resources
from outside the container. So we add the kthread API support backSorry, but it's not clear the reason.
I understand that we want to provide a way to bring back the previous behavior when we had kthreads, but why do we want that?
Do you have examples where the new mechanism is causing problems?Quoted 6 lines
The main difference is whose (kthreadd or the owner) attributes (namespace, affinities) would vhost thread inherit.
The owner's attributes tend to be different from kthreadd, so management might be surprised for example, vhost might be created in different namespaces or having different scheduling affinities.
Quoted 6 lines
Okay, so this requires some API to allow the managment to understand how the device vhost will be created.
But why do we need to restore the old behavior with a kthread where the resource accounting is completely disconnected from userspace? For the old managments that don't expect this?
Yes, as such change can be easily noticed by the user space that breaks existing management layers or tools.
Quoted 4 lines
BTW I would suggest adding all this information in this commit, in the parameter/IOCTL documentation, and in the cover letter because IMHO it is very important.
I've asked this in the past. Cindy, please make sure such information were included in the next version in the cover letter.
Thanks
Quoted 4 lines
Thanks,
Stefano
Re: [PATCH v2 1/7] vhost: Add a new modparam to allow userspace select vhost_task
Quoted 41 lines · Jason Wang
Quoted 28 lines · Stefano Garzarella
Quoted 19 lines · Jason Wang
Quoted 8 lines · Stefano Garzarella
Quoted 3 lines · Cindy Lu
The vhost is now using
vhost_taskand working as a child of the owner thread. While this makes sense from containerization POV, some old userspace is confused, as previously vhost notnot what?
Quoted 9 lines
Quoted 2 lines
and so was allowed to steal cpu resources
from outside the container. So we add the kthread API support backSorry, but it's not clear the reason.
I understand that we want to provide a way to bring back the previous behavior when we had kthreads, but why do we want that?
Do you have examples where the new mechanism is causing problems?Quoted 6 lines
The main difference is whose (kthreadd or the owner) attributes (namespace, affinities) would vhost thread inherit.
The owner's attributes tend to be different from kthreadd, so management might be surprised for example, vhost might be created in different namespaces or having different scheduling affinities.
Quoted 6 lines
Okay, so this requires some API to allow the managment to understand how the device vhost will be created.
But why do we need to restore the old behavior with a kthread where the resource accounting is completely disconnected from userspace? For the old managments that don't expect this?
Yes, as such change can be easily noticed by the user space that breaks existing management layers or tools.
Quoted 1 line
Quoted 9 lines
Quoted 3 lines
BTW I would suggest adding all this information in this commit, in the parameter/IOCTL documentation, and in the cover letter because IMHO it is very important.
I've asked this in the past. Cindy, please make sure such information were included in the next version in the cover letter.
Thanks
Thanks Jason, I will add this
Thanks
cindy
Quoted 5 lines
Quoted 4 lines
Thanks,
Stefano
Re: [PATCH v2 1/7] vhost: Add a new modparam to allow userspace select vhost_task
In reply to Stefano GarzarellaQuoted 63 lines · Stefano Garzarella
Quoted 3 lines · Cindy Lu
The vhost is now using
vhost_taskand working as a child of the owner thread. While this makes sense from containerization POV, some old userspace is confused, as previously vhost notnot what?
Quoted 2 lines
and so was allowed to steal cpu resources
from outside the container. So we add the kthread API support backSorry, but it's not clear the reason.
I understand that we want to provide a way to bring back the previous behavior when we had kthreads, but why do we want that?
Do you have examples where the new mechanism is causing problems?Quoted 14 lines
Add a new module parameter to allow userspace to select behaviour between using kthread and task
Signed-off-by Cindy Lu <[email protected]>---
drivers/vhost/vhost.c| 4 ++++1 file changed, 4 insertions(+)drivers/vhost/vhost.c +0
-0drivers/vhost/vhost.c static int max_iotlb_entries = 2048; · line 44
Quoted 4 lines
drivers/vhost/vhost.c +1
-0@@ -41,6 +41,10 @@ static int max_iotlb_entries = 2048;module_param (max_iotlb_entries, int,0444 );MODULE_PARM_DESC (max_iotlb_entries,"Maximum number of iotlb entries. (default: 2048)" ); +bool enforce_inherit_owner = true;^This should be static:
$ make -j6 O=build C=2 drivers/vhost/ ... CHECK ../drivers/vhost/vhost.c ../drivers/vhost/vhost.c:45:6: warning: symbol 'enforce_inherit_owner' was not declared. Should it be static?drivers/vhost/vhost.c static int max_iotlb_entries = 2048; · line 47
Quoted 3 lines
drivers/vhost/vhost.c +3
-0+module_param (enforce_inherit_owner, bool,0444 ); +MODULE_PARM_DESC (enforce_inherit_owner, +"enforce vhost use vhost_task(default: Y)" );I would follow the style of the other 2 parameters:
"Enforce vhost usevhost_task. (default: Y)"With a view to simplifying bisection, we added this parameter in this patch, but it does nothing, so IMHO we should only add it at the end of the series when we have all the code ready. Maybe you can just add
enforce_inherit_ownerhere or in the first patch where you need it, but I'd expose it withmodule_param()only when we have all the pieces in place.About the param name, I'm not sure "enforce" is the right word, since IIUC the user can still change it using the ioctl. It would seem that set
enforce_inherit_ownerto true, it is always forced, but instead ioctl allows you to change it, right?
Quoted 6 lines
Is it more of a default behavior?
Something likeinherit_owner_default?Thanks,
Stefano
thanks, Stefano, I will change this
thanks
cindy
PATCH v2 2/7 vhost: Add kthread support in function vhost_worker_queue()
In reply to Cindy LuAdded back the previously removed function vhost_worker_queue() and renamed it to vhost_worker_queue_khtread(). The new vhost_worker_queue() will select the different mode based on the value of the parameter.
The old function vhost_work_queue() was change to support task in commit 6e890c5d5021 ("vhost: use vhost_tasks for worker threads") changed in
commit f9010dbdce91 ("fork, vhost: Use CLONE_THREAD to fix freezer/ps regression") and also was change the name of function to vhost_worker_queue() in commit 0921dddcb589 ("vhost: take worker or vq instead of dev for queueing")
--- drivers/vhost/vhost.c | 30 ++++++++++++++++++++++++++++-- drivers/vhost/vhost.h | 1 + 2 files changed, 29 insertions(+), 2 deletions(-)
drivers/vhost/vhost.c +28 -2
@@ -237,8 +237,8 @@ void vhost_poll_stop(struct vhost_poll *poll) }EXPORT_SYMBOL_GPL (vhost_poll_stop); -static void vhost_worker_queue (struct vhost_worker *worker, -struct vhost_work *work) +static void vhost_worker_queue_task (struct vhost_worker *worker, +struct vhost_work *work) {if (!test_and_set_bit (VHOST_WORK_QUEUED , &work->flags)) {/* We can only add the work to the list after we're
@@ -250,6 +250,32 @@ static void vhost_worker_queue(struct vhost_worker *worker, } } +static void vhost_work_queue_kthread (struct vhost_worker *worker, +struct vhost_work *work) +{ +if (!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, &worker->work_list); + +wake_up_process (worker->task); + } +} + +static void vhost_worker_queue (struct vhost_worker *worker, +struct vhost_work *work) +{ +if (enforce_inherit_owner) { +vhost_worker_queue_task (worker, work); + }else { +vhost_work_queue_kthread (worker, work); + } +}bool vhost_vq_work_queue (struct vhost_virtqueue *vq,struct vhost_work *work) {struct vhost_worker *worker;
drivers/vhost/vhost.h +1 -0
@@ -27,6 +27,7 @@ struct vhost_work { };struct vhost_worker { +struct task_struct *task;struct vhost_task *vtsk;struct vhost_dev *dev;/* Used to serialize device wide flushing with worker swapping. */
Signature · 1 line
2.45.0
PATCH v2 3/7 vhost: Add kthread support in function vhost_workers_free()
In reply to Cindy LuAdded back the previously removed function vhost_workers_free() and renamed it to vhost_workers_free_khtread(). The new vhost_workers_free() will select the different mode based on the value of the parameter.
The old function vhost_workers_free was change to support task in commit 6e890c5d5021 ("vhost: use vhost_tasks for worker threads")
also changed in
commit a284f09effea ("vhost: Fix crash during early vhost_transport_send_pkt calls") change to xarray in
commit 1cdaafa1b8b4 ("vhost: replace single worker pointer with xarray")
--- drivers/vhost/vhost.c | 52 ++++++++++++++++++++++++++++++++++++++----- 1 file changed, 47 insertions(+), 5 deletions(-)
drivers/vhost/vhost.c +47 -5
@@ -648,8 +648,21 @@ static void vhost_detach_mm(struct vhost_dev *dev) dev->mm =NULL ; } -static void vhost_worker_destroy (struct vhost_dev *dev, -struct vhost_worker *worker) +static void vhost_worker_destroy_kthread (struct vhost_dev *dev, +struct vhost_worker *worker) +{ +if (!worker) +return ; + +WARN_ON (!llist_empty (&worker->work_list)); + +xa_erase (&dev->worker_xa, worker->id); +kthread_stop (worker->task); +kfree (worker); +} + +static void vhost_worker_destroy_task (struct vhost_dev *dev, +struct vhost_worker *worker) {if (!worker)return ;
@@ -660,7 +673,7 @@ static void vhost_worker_destroy(struct vhost_dev *dev,kfree (worker); } -static void vhost_workers_free (struct vhost_dev *dev) +static void vhost_workers_free_task (struct vhost_dev *dev) {struct vhost_worker *worker;unsigned long i;
@@ -675,10 +688,36 @@ static void vhost_workers_free(struct vhost_dev *dev)* created but couldn't clean up (it forgot or crashed). */ xa_for_each (&dev->worker_xa, i, worker) -vhost_worker_destroy (dev, worker); +vhost_worker_destroy_task (dev, worker);xa_destroy (&dev->worker_xa); } +static void vhost_workers_free_kthread (struct vhost_dev *dev) +{ +struct vhost_worker *worker; +unsigned long i; + +if (!dev->use_worker) +return ; + +for (i =0 ; i < dev->nvqs; i++) +rcu_assign_pointer (dev->vqs[i]->worker,NULL ); +/* +* Free the default worker we created and cleanup workers userspace +* created but couldn't clean up (it forgot or crashed). +*/ +xa_for_each (&dev->worker_xa, i, worker) +vhost_worker_destroy_kthread (dev, worker); +xa_destroy (&dev->worker_xa); +} + +static void vhost_workers_free (struct vhost_dev *dev) +{ +if (enforce_inherit_owner) +vhost_workers_free_task (dev); +else +vhost_workers_free_kthread (dev); +}static struct vhost_worker *vhost_worker_create (struct vhost_dev *dev) {struct vhost_worker *worker;
@@ -846,7 +885,10 @@ static int vhost_free_worker(struct vhost_dev *dev,__vhost_worker_flush (worker);mutex_unlock (&worker->mutex); -vhost_worker_destroy (dev, worker); +if (enforce_inherit_owner) +vhost_worker_destroy_task (dev, worker); +else +vhost_worker_destroy_kthread (dev, worker);return 0 ; }
Signature · 1 line
2.45.0
Re: [PATCH v2 3/7] vhost: Add kthread support in function vhost_workers_free()
Quoted 78 lines · Cindy Lu
Added back the previously removed function
vhost_workers_free()and renamed it tovhost_workers_free_khtread(). The newvhost_workers_free()will select the different mode based on the value of the parameter.The old function
vhost_workers_freewas change to support task in commit6e890c5d5021("vhost: usevhost_tasksfor worker threads")
also changed in
commita284f09effea("vhost: Fix crash during earlyvhost_transport_send_pktcalls") change to xarray in
commit1cdaafa1b8b4("vhost: replace single worker pointer with xarray")Signed-off-by Cindy Lu <[email protected]>---
drivers/vhost/vhost.c| 52 ++++++++++++++++++++++++++++++++++++++-----1 file changed, 47 insertions(+), 5 deletions(-)drivers/vhost/vhost.c +33
-4@@ -648,8 +648,21 @@ static void vhost_detach_mm(struct vhost_dev *dev) dev->mm =NULL ; } -static void vhost_worker_destroy (struct vhost_dev *dev, -struct vhost_worker *worker) +static void vhost_worker_destroy_kthread (struct vhost_dev *dev, +struct vhost_worker *worker) +{ +if (!worker) +return ; + +WARN_ON (!llist_empty (&worker->work_list)); + +xa_erase (&dev->worker_xa, worker->id); +kthread_stop (worker->task); +kfree (worker); +} + +static void vhost_worker_destroy_task (struct vhost_dev *dev, +struct vhost_worker *worker) {if (!worker)return ;@@ -660,7 +673,7 @@ static void vhost_worker_destroy(struct vhost_dev *dev,kfree (worker); } -static void vhost_workers_free (struct vhost_dev *dev) +static void vhost_workers_free_task (struct vhost_dev *dev) {struct vhost_worker *worker;unsigned long i;@@ -675,10 +688,36 @@ static void vhost_workers_free(struct vhost_dev *dev)* created but couldn't clean up (it forgot or crashed). */ xa_for_each (&dev->worker_xa, i, worker) -vhost_worker_destroy (dev, worker); +vhost_worker_destroy_task (dev, worker);xa_destroy (&dev->worker_xa); } +static void vhost_workers_free_kthread (struct vhost_dev *dev) +{ +struct vhost_worker *worker; +unsigned long i; + +if (!dev->use_worker) +return ; + +for (i =0 ; i < dev->nvqs; i++) +rcu_assign_pointer (dev->vqs[i]->worker,NULL ); +/* +* Free the default worker we created and cleanup workers userspace +* created but couldn't clean up (it forgot or crashed). +*/ +xa_for_each (&dev->worker_xa, i, worker) +vhost_worker_destroy_kthread (dev, worker);
drivers/vhost/vhost.c static void vhost_workers_free(struct vhost_dev *dev) · line 720
Quoted 10 lines
drivers/vhost/vhost.c +10
-0+xa_destroy (&dev->worker_xa); +} + +static void vhost_workers_free (struct vhost_dev *dev) +{ +if (enforce_inherit_owner) +vhost_workers_free_task (dev); +else +vhost_workers_free_kthread (dev); +}
nit: new line here
Quoted 19 lines
drivers/vhost/vhost.c +4
-1static struct vhost_worker *vhost_worker_create (struct vhost_dev *dev) {struct vhost_worker *worker;@@ -846,7 +885,10 @@ static int vhost_free_worker(struct vhost_dev *dev,__vhost_worker_flush (worker);mutex_unlock (&worker->mutex); -vhost_worker_destroy (dev, worker); +if (enforce_inherit_owner) +vhost_worker_destroy_task (dev, worker); +else +vhost_worker_destroy_kthread (dev, worker);return 0 ; }Signature · 1 line
2.45.0
Re: [PATCH v2 3/7] vhost: Add kthread support in function vhost_workers_free()
Quoted 84 lines · Stefano Garzarella
Quoted 82 lines · Cindy Lu
Added back the previously removed function
vhost_workers_free()and renamed it tovhost_workers_free_khtread(). The newvhost_workers_free()will select the different mode based on the value of the parameter.The old function
vhost_workers_freewas change to support task in commit6e890c5d5021("vhost: usevhost_tasksfor worker threads")
also changed in
commita284f09effea("vhost: Fix crash during earlyvhost_transport_send_pktcalls") change to xarray in
commit1cdaafa1b8b4("vhost: replace single worker pointer with xarray")Signed-off-by Cindy Lu <[email protected]>---
drivers/vhost/vhost.c| 52 ++++++++++++++++++++++++++++++++++++++-----1 file changed, 47 insertions(+), 5 deletions(-)drivers/vhost/vhost.c +37
-4@@ -648,8 +648,21 @@ static void vhost_detach_mm(struct vhost_dev *dev) dev->mm =NULL ; } -static void vhost_worker_destroy (struct vhost_dev *dev, -struct vhost_worker *worker) +static void vhost_worker_destroy_kthread (struct vhost_dev *dev, +struct vhost_worker *worker) +{ +if (!worker) +return ; + +WARN_ON (!llist_empty (&worker->work_list)); + +xa_erase (&dev->worker_xa, worker->id); +kthread_stop (worker->task); +kfree (worker); +} + +static void vhost_worker_destroy_task (struct vhost_dev *dev, +struct vhost_worker *worker) {if (!worker)return ;@@ -660,7 +673,7 @@ static void vhost_worker_destroy(struct vhost_dev *dev,kfree (worker); } -static void vhost_workers_free (struct vhost_dev *dev) +static void vhost_workers_free_task (struct vhost_dev *dev) {struct vhost_worker *worker;unsigned long i;@@ -675,10 +688,36 @@ static void vhost_workers_free(struct vhost_dev *dev)* created but couldn't clean up (it forgot or crashed). */ xa_for_each (&dev->worker_xa, i, worker) -vhost_worker_destroy (dev, worker); +vhost_worker_destroy_task (dev, worker);xa_destroy (&dev->worker_xa); } +static void vhost_workers_free_kthread (struct vhost_dev *dev) +{ +struct vhost_worker *worker; +unsigned long i; + +if (!dev->use_worker) +return ; + +for (i =0 ; i < dev->nvqs; i++) +rcu_assign_pointer (dev->vqs[i]->worker,NULL ); +/* +* Free the default worker we created and cleanup workers userspace +* created but couldn't clean up (it forgot or crashed). +*/ +xa_for_each (&dev->worker_xa, i, worker) +vhost_worker_destroy_kthread (dev, worker); +xa_destroy (&dev->worker_xa); +} + +static void vhost_workers_free (struct vhost_dev *dev)
Quoted 9 lines
drivers/vhost/vhost.c static void vhost_workers_free(struct vhost_dev *dev) · line 720
Quoted 6 lines
drivers/vhost/vhost.c +6
-0+{ +if (enforce_inherit_owner) +vhost_workers_free_task (dev); +else +vhost_workers_free_kthread (dev); +}nit: new line here
sure, will fix this
Quoted 20 lines
Quoted 19 lines
drivers/vhost/vhost.c +4
-2static struct vhost_worker *vhost_worker_create (struct vhost_dev *dev) {struct vhost_worker *worker;@@ -846,7 +885,10 @@ static int vhost_free_worker(struct vhost_dev *dev,__vhost_worker_flush (worker);mutex_unlock (&worker->mutex); -vhost_worker_destroy (dev, worker); +if (enforce_inherit_owner) +vhost_worker_destroy_task (dev, worker); +else +vhost_worker_destroy_kthread (dev, worker);return 0 ; } --2.45.0
Re: [PATCH v2 3/7] vhost: Add kthread support in function vhost_workers_free()
In reply to Cindy Ludrivers/vhost/vhost.c static void vhost_workers_free(struct vhost_dev *dev) · line 720
Quoted 7 lines · Cindy Lu
drivers/vhost/vhost.c +7
-0+static void vhost_workers_free (struct vhost_dev *dev) +{ +if (enforce_inherit_owner) +vhost_workers_free_task (dev); +else +vhost_workers_free_kthread (dev); +}
With patch 7, userspace could change enforce_inherit_owner after we created thread and we would call the wrong function above.
Re: [PATCH v2 3/7] vhost: Add kthread support in function vhost_workers_free()
Quoted 13 lines · Mike Christie
drivers/vhost/vhost.c static void vhost_workers_free(struct vhost_dev *dev) · line 720
Quoted 7 lines · Cindy Lu
drivers/vhost/vhost.c +7
-0+static void vhost_workers_free (struct vhost_dev *dev) +{ +if (enforce_inherit_owner) +vhost_workers_free_task (dev); +else +vhost_workers_free_kthread (dev); +}With patch 7, userspace could change
enforce_inherit_ownerafter we created thread and we would call the wrong function above.
enforce_inherit_owner will only change before the owner was set. the process is like set enforce_inherit_owner---->set owner-> thread/task creating
in in patch 7's code I have add the check for vhost's owner, if the owner was set, the ioctl
to set enforce_inherit_owner will fail
Thanks
Cindy
Re: [PATCH v2 3/7] vhost: Add kthread support in function vhost_workers_free()
Quoted 14 lines · Cindy Lu
Quoted 13 lines · Mike Christie
drivers/vhost/vhost.c static void vhost_workers_free(struct vhost_dev *dev) · line 720
Quoted 7 lines · Cindy Lu
drivers/vhost/vhost.c +7
-0+static void vhost_workers_free (struct vhost_dev *dev) +{ +if (enforce_inherit_owner) +vhost_workers_free_task (dev); +else +vhost_workers_free_kthread (dev); +}With patch 7, userspace could change
enforce_inherit_ownerafter we created thread and we would call the wrong function above.
Quoted 1 line
enforce_inherit_ownerwill only change before the owner was set.
As I pointed out in patch 7, enforce_inherit_owner seems to be shared among all vhost devices, so what happens if for example a user sets it to /dev/vhost-net, while /dev/vhost-vsock is already initialized and therefore already has an owner?
Thanks,
Stefano
Quoted 9 lines
the process is like set enforce_inherit_owner---->set owner-> thread/task creating
in in patch 7's code I have add the check for vhost's owner, if the owner was set, the ioctl
to setenforce_inherit_ownerwill fail
Thanks
Cindy
Re: [PATCH v2 3/7] vhost: Add kthread support in function vhost_workers_free()
Quoted 18 lines · Stefano Garzarella
Quoted 14 lines · Cindy Lu
Quoted 13 lines · Mike Christie
drivers/vhost/vhost.c static void vhost_workers_free(struct vhost_dev *dev) · line 720
Quoted 7 lines · Cindy Lu
drivers/vhost/vhost.c +7
-0+static void vhost_workers_free (struct vhost_dev *dev) +{ +if (enforce_inherit_owner) +vhost_workers_free_task (dev); +else +vhost_workers_free_kthread (dev); +}With patch 7, userspace could change
enforce_inherit_ownerafter we created thread and we would call the wrong function above.Quoted 1 line
enforce_inherit_ownerwill only change before the owner was set.
Quoted 8 lines
As I pointed out in patch 7,
enforce_inherit_ownerseems to be shared among all vhost devices, so what happens if for example a user sets it to/dev/vhost-net, while/dev/vhost-vsockis already initialized and therefore already has an owner?Thanks,
Stefano
You are correct, I will think about this and provide a new version Thanks
cindy
Quoted 10 lines
Quoted 9 lines
the process is like set enforce_inherit_owner---->set owner-> thread/task creating
in in patch 7's code I have add the check for vhost's owner, if the owner was set, the ioctl
to setenforce_inherit_ownerwill fail
Thanks
Cindy
PATCH v2 4/7 vhost: Add the vhost_worker to support kthread
In reply to Cindy LuAdd back the previously removed vhost_worker function to support the kthread and rename it vhost_run_work_kthread_list.
The old function vhost_worker was change to support task in commit 6e890c5d5021 ("vhost: use vhost_tasks for worker threads") change to xarray in
commit 1cdaafa1b8b4 ("vhost: replace single worker pointer with xarray")
--- drivers/vhost/vhost.c | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+)
drivers/vhost/vhost.c +38 -0
@@ -418,6 +418,44 @@ static void vhost_vq_reset(struct vhost_dev *dev,__vhost_vq_meta_reset (vq); } +static int vhost_run_work_kthread_list (void *data) +{ +struct vhost_worker *worker = data; +struct vhost_work *work, *work_next; +struct vhost_dev *dev = worker->dev; +struct llist_node *node; + +kthread_use_mm (dev->mm); + +for (;;) { +/* mb paired w/ kthread_stop */ +set_current_state (TASK_INTERRUPTIBLE ); + +if (kthread_should_stop ()) { +__set_current_state (TASK_RUNNING ); +break ; + } + node =llist_del_all (&worker->work_list); +if (!node) +schedule (); + + node =llist_reverse_order (node); +/* make sure flag is seen after deletion */ +smp_wmb (); +llist_for_each_entry_safe (work, work_next, node, node) { +clear_bit (VHOST_WORK_QUEUED , &work->flags); +__set_current_state (TASK_RUNNING ); +kcov_remote_start_common (worker->kcov_handle); + work->fn (work); +kcov_remote_stop (); +cond_resched (); + } + } +kthread_unuse_mm (dev->mm); + +return 0 ; +} +static bool vhost_run_work_list (void *data) {struct vhost_worker *worker = data;
Signature · 1 line
2.45.0
Re: [PATCH v2 4/7] vhost: Add the vhost_worker to support kthread
Quoted 48 lines · Cindy Lu
Add back the previously removed
vhost_workerfunction to support the kthread and rename itvhost_run_work_kthread_list.The old function
vhost_workerwas change to support task in commit6e890c5d5021("vhost: usevhost_tasksfor worker threads") change to xarray in
commit1cdaafa1b8b4("vhost: replace single worker pointer with xarray")Signed-off-by Cindy Lu <[email protected]>--- drivers/vhost/vhost.c | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+)drivers/vhost/vhost.c +27
-0@@ -418,6 +418,44 @@ static void vhost_vq_reset(struct vhost_dev *dev,__vhost_vq_meta_reset (vq); } +static int vhost_run_work_kthread_list (void *data) +{ +struct vhost_worker *worker = data; +struct vhost_work *work, *work_next; +struct vhost_dev *dev = worker->dev; +struct llist_node *node; + +kthread_use_mm (dev->mm); + +for (;;) { +/* mb paired w/ kthread_stop */ +set_current_state (TASK_INTERRUPTIBLE ); + +if (kthread_should_stop ()) { +__set_current_state (TASK_RUNNING ); +break ; + } + node =llist_del_all (&worker->work_list); +if (!node) +schedule (); + + node =llist_reverse_order (node); +/* make sure flag is seen after deletion */ +smp_wmb (); +llist_for_each_entry_safe (work, work_next, node, node) { +clear_bit (VHOST_WORK_QUEUED , &work->flags); +__set_current_state (TASK_RUNNING );
drivers/vhost/vhost.c static void vhost_vq_reset(struct vhost_dev *dev, · line 457
Quoted 10 lines
drivers/vhost/vhost.c +10
-0+kcov_remote_start_common (worker->kcov_handle); + work->fn (work); +kcov_remote_stop (); +cond_resched (); + } + } +kthread_unuse_mm (dev->mm); + +return 0 ; +}
I think there is a lot of unneeded code duplication where in the functions you are adding there's only 1-3 lines different. To fix this I think we could:
1. Go really invasive and modify copy_process and its helpers so they take a task_struct instead of using "current". We can then just pass in "current" or kthreadd. We can then use most of the existing vhost code. We just need a per vhost_worker check/field for the mm and cgroup use like:
vhost_task_fn():
{
...
/* The mm would be passed in during creation for the kthread case */ if (vtsk->mm) kthread_use_mm(vtsk->mm);
Or
2. Go hacky and in the vhost code, when we get a VHOST_SET_OWNER call create a tmp kthread. The tmp kthread would then call the existing vhost_worker_create function. The resulting vhost_task would inherit the kthreadd settings like we want. We then just need a per vhost_worker check/field for the mm and cgroup use like above.
Or
3. There doesn't seem to be a lot of differences in the functions you are adding. In the function above the only differences are the mm calls and kthread should stop. In the destroy functions it's kthread_stop. In the queue function its wake_up_process. In create its kthread_create, stop and the cgroup functions.
I think we could add just some callouts on the vhost_task or vhost_worker for stop, wakeup and use mm. For create we would do something like
vhost_worker_create()
....
worker = kzalloc();
if (inherit from caller) {
worker->stop = vhost_task_stop;
worker->wakeup = vhost_task_wakeup;
worker->vtsk = vhost_task_create();
} else {
worker->stop = vhost_kthread_stop;
worker->wakeup = vhost_kthread_wakeup;
worker->tsk = kthread->create();
vhost_kthread_setup_cgroups();
}...
}
vhost_worker_destroy()
{
if (!worker)
return;
WARN_ON(!llist_empty(&worker->work_list));
xa_erase(&dev->worker_xa, worker->id);
worker->stop(worker);
kfree(worker);
}Re: [PATCH v2 4/7] vhost: Add the vhost_worker to support kthread
Quoted 55 lines · Mike Christie
Quoted 53 lines · Cindy Lu
Add back the previously removed
vhost_workerfunction to support the kthread and rename itvhost_run_work_kthread_list.The old function
vhost_workerwas change to support task in commit6e890c5d5021("vhost: usevhost_tasksfor worker threads") change to xarray in
commit1cdaafa1b8b4("vhost: replace single worker pointer with xarray")Signed-off-by Cindy Lu <[email protected]>--- drivers/vhost/vhost.c | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+)drivers/vhost/vhost.c +32
-0@@ -418,6 +418,44 @@ static void vhost_vq_reset(struct vhost_dev *dev,__vhost_vq_meta_reset (vq); } +static int vhost_run_work_kthread_list (void *data) +{ +struct vhost_worker *worker = data; +struct vhost_work *work, *work_next; +struct vhost_dev *dev = worker->dev; +struct llist_node *node; + +kthread_use_mm (dev->mm); + +for (;;) { +/* mb paired w/ kthread_stop */ +set_current_state (TASK_INTERRUPTIBLE ); + +if (kthread_should_stop ()) { +__set_current_state (TASK_RUNNING ); +break ; + } + node =llist_del_all (&worker->work_list); +if (!node) +schedule (); + + node =llist_reverse_order (node); +/* make sure flag is seen after deletion */ +smp_wmb (); +llist_for_each_entry_safe (work, work_next, node, node) { +clear_bit (VHOST_WORK_QUEUED , &work->flags); +__set_current_state (TASK_RUNNING ); +kcov_remote_start_common (worker->kcov_handle); + work->fn (work); +kcov_remote_stop (); +cond_resched (); + }
Quoted 9 lines
drivers/vhost/vhost.c static void vhost_vq_reset(struct vhost_dev *dev, · line 457
Quoted 5 lines
drivers/vhost/vhost.c +5
-0+ } +kthread_unuse_mm (dev->mm); + +return 0 ; +}I think there is a lot of unneeded code duplication where in the functions you are adding there's only 1-3 lines different. To fix this I think we could:
Thank you mike, I will try this and provide a new version thanks
cindy
Quoted 62 lines
1. Go really invasive and modify
copy_processand its helpers so they take atask_structinstead of using "current". We can then just pass in "current" or kthreadd. We can then use most of the existing vhost code. We just need a pervhost_workercheck/field for the mm and cgroup use like:
vhost_task_fn():
{
.../* The mm would be passed in during creation for the kthread case */ if (vtsk->mm) kthread_use_mm(vtsk->mm);Or
2. Go hacky and in the vhost code, when we get a
VHOST_SET_OWNERcall create a tmp kthread. The tmp kthread would then call the existingvhost_worker_createfunction. The resultingvhost_taskwould inherit the kthreadd settings like we want. We then just need a pervhost_workercheck/field for the mm and cgroup use like above.Or
3. There doesn't seem to be a lot of differences in the functions you are adding. In the function above the only differences are the mm calls and kthread should stop. In the destroy functions it's
kthread_stop. In the queue function itswake_up_process. In create itskthread_create, stop and the cgroup functions.I think we could add just some callouts on the
vhost_taskorvhost_workerfor stop, wakeup and use mm. For create we would do something like
vhost_worker_create()
....worker = kzalloc(); if (inherit from caller) { worker->stop = vhost_task_stop; worker->wakeup = vhost_task_wakeup; worker->vtsk = vhost_task_create(); } else { worker->stop = vhost_kthread_stop; worker->wakeup = vhost_kthread_wakeup; worker->tsk = kthread->create(); vhost_kthread_setup_cgroups(); }...
}
vhost_worker_destroy(){ if (!worker) return; WARN_ON(!llist_empty(&worker->work_list)); xa_erase(&dev->worker_xa, worker->id); worker->stop(worker); kfree(worker); }
PATCH v2 5/7 vhost: Add the cgroup related function
In reply to Cindy LuAdd back the previously removed cgroup function to support the kthread The biggest change for this part is in vhost_attach_cgroups() and vhost_worker_cgroups_kthread(). This is because of the change in struct dev->worker_xa.
The old function was remove in
commit 6e890c5d5021 ("vhost: use vhost_tasks for worker threads")
--- drivers/vhost/vhost.c | 52 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+)
drivers/vhost/vhost.c +52 -0
@@ -22,6 +22,7 @@#include <linux/slab.h> #include <linux/vmalloc.h> #include <linux/kthread.h> +#include <linux/cgroup.h> #include <linux/module.h> #include <linux/sort.h> #include <linux/sched/mm.h>
@@ -649,6 +650,57 @@ long vhost_dev_check_owner(struct vhost_dev *dev) }EXPORT_SYMBOL_GPL (vhost_dev_check_owner); +struct vhost_attach_cgroups_struct { +struct vhost_work work; +struct task_struct *owner; +int ret; +}; + +static void vhost_attach_cgroups_work (struct vhost_work *work) +{ +struct vhost_attach_cgroups_struct *s; + + s = container_of(work, struct vhost_attach_cgroups_struct, work); + s->ret =cgroup_attach_task_all (s->owner, current); +} + +static int vhost_worker_cgroups_kthread (struct vhost_worker *worker) +{ +struct vhost_flush_struct flush; +struct vhost_attach_cgroups_struct attach; + + attach.owner = current; + +vhost_work_init (&attach.work, vhost_attach_cgroups_work); +vhost_worker_queue (worker, &attach.work); + +init_completion (&flush.wait_event); +vhost_work_init (&flush.work, vhost_flush_work); +vhost_worker_queue (worker, &flush.work); +wait_for_completion (&flush.wait_event); + +return attach.ret; +} + +static int vhost_attach_cgroups (struct vhost_dev *dev) +{ +struct vhost_worker *worker; +unsigned long i; +int ret; + +/* +* Free the default worker we created and cleanup workers userspace +* created but couldn't clean up (it forgot or crashed). +*/ + +xa_for_each (&dev->worker_xa, i, worker) { + ret =vhost_worker_cgroups_kthread (worker); +if (ret) +return ret; + } +return ret; +} +/* Caller should have device mutex */ bool vhost_dev_has_owner (struct vhost_dev *dev) {
Signature · 1 line
2.45.0
PATCH v2 6/7 vhost: Add kthread support in function vhost_worker_create
In reply to Cindy LuSplit the function vhost_worker_create to support both task and kthread
Added back the previous old function vhost_worker_create and rename it to vhost_worker_create_khtread to support the khtread.
The new vhost_worker_create will be selected which to use based on the value of the parameter.
the old function vhost_worker_create was change to support task in commit 6e890c5d5021 ("vhost: use vhost_tasks for worker threads") also changed in
commit 1cdaafa1b8b4 ("vhost: replace single worker pointer with xarray") commit c011bb669ddc ("vhost: dynamically allocate vhost_worker")
--- drivers/vhost/vhost.c | 55 ++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 54 insertions(+), 1 deletion(-)
drivers/vhost/vhost.c +54 -1
@@ -808,7 +808,8 @@ static void vhost_workers_free(struct vhost_dev *dev)else vhost_workers_free_kthread (dev); } -static struct vhost_worker *vhost_worker_create (struct vhost_dev *dev) + +static struct vhost_worker *vhost_worker_create_task (struct vhost_dev *dev) {struct vhost_worker *worker;struct vhost_task *vtsk;
@@ -849,6 +850,50 @@ static struct vhost_worker *vhost_worker_create(struct vhost_dev *dev)return NULL ; } +static struct vhost_worker *vhost_worker_create_kthread (struct vhost_dev *dev) +{ +struct vhost_worker *worker; +struct task_struct *task; +int ret; +u32 id; + + worker =kzalloc (sizeof (*worker),GFP_KERNEL_ACCOUNT ); +if (!worker) +return NULL ; + + worker->dev = dev; + worker->kcov_handle =kcov_common_handle (); + +mutex_init (&worker->mutex); +init_llist_head (&worker->work_list); + + task =kthread_create (vhost_run_work_kthread_list, 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 =xa_alloc (&dev->worker_xa, &id, worker, xa_limit_32b,GFP_KERNEL ); +if (ret <0 ) + goto stop_worker; + worker->id = id; + + ret =vhost_attach_cgroups (dev); +if (ret) + goto stop_worker; + +return worker; + +stop_worker: +kthread_stop (worker->task); +free_worker: +kfree (worker); +return NULL ; +} +/* Caller must have device mutex */ static void __vhost_vq_attach_worker (struct vhost_virtqueue *vq,struct vhost_worker *worker)
@@ -937,6 +982,14 @@ static int vhost_vq_attach_worker(struct vhost_virtqueue *vq,return 0 ; } +static struct vhost_worker *vhost_worker_create (struct vhost_dev *dev) +{ +if (enforce_inherit_owner) +return vhost_worker_create_task (dev); +else +return vhost_worker_create_kthread (dev); +} +/* Caller must have device mutex */ static int vhost_new_worker (struct vhost_dev *dev,struct vhost_worker_state *info)
Signature · 1 line
2.45.0
Re: [PATCH v2 6/7] vhost: Add kthread support in function vhost_worker_create
drivers/vhost/vhost.c static int vhost_vq_attach_worker(struct vhost_virtqueue *vq, · line 991
Quoted 7 lines · Cindy Lu
drivers/vhost/vhost.c +7
-0+static struct vhost_worker *vhost_worker_create (struct vhost_dev *dev) +{ +if (enforce_inherit_owner) +return vhost_worker_create_task (dev); +else +return vhost_worker_create_kthread (dev); +}
The reason we are in this whole mess is because for vhost-scsi we wanted to create multiple worker threads as the single thread became a bottleneck. We couldn't use kthreads because they were not accounted for under thread that created it and we could exceed the NPROC_LIMIT.
So with the above code, we could hit that problem again if userspace did the VHOST_NEW_WORKER ioctl and enforce_inherit_owner=false.
You would want a check for enforce_inherit_owner in vhost_worker_ioctl.
Re: [PATCH v2 6/7] vhost: Add kthread support in function vhost_worker_create
Quoted 16 lines · Mike Christie
drivers/vhost/vhost.c static int vhost_vq_attach_worker(struct vhost_virtqueue *vq, · line 991
Quoted 7 lines · Cindy Lu
drivers/vhost/vhost.c +7
-0+static struct vhost_worker *vhost_worker_create (struct vhost_dev *dev) +{ +if (enforce_inherit_owner) +return vhost_worker_create_task (dev); +else +return vhost_worker_create_kthread (dev); +}The reason we are in this whole mess is because for vhost-scsi we wanted to create multiple worker threads as the single thread became a bottleneck. We couldn't use kthreads because they were not accounted for under thread that created it and we could exceed the
NPROC_LIMIT.
Quoted 5 lines
So with the above code, we could hit that problem again if userspace did the
VHOST_NEW_WORKERioctl and enforce_inherit_owner=false.You would want a check for
enforce_inherit_ownerinvhost_worker_ioctl.
sure, will add this check
Thanks
cindy
PATCH v2 7/7 vhost: Add new UAPI to support change to task mode
In reply to Cindy LuAdd a new UAPI to support setting the vhost device to use task mode. The user space application needs to use VHOST_SET_INHERIT_FROM_OWNER to set the mode. This setting must be set before VHOST_SET_OWNER is set.
--- drivers/vhost/vhost.c | 18 +++++++++++++++++- include/uapi/linux/vhost.h | 2 ++ 2 files changed, 19 insertions(+), 1 deletion(-)
drivers/vhost/vhost.c +17 -1
@@ -2341,8 +2341,24 @@ long vhost_dev_ioctl(struct vhost_dev *d, unsigned int ioctl, void __user *argp) {struct eventfd_ctx *ctx;u64 p; -long r; +long r =0 ;int i, fd; +bool inherit_owner; + +if (ioctl ==VHOST_SET_INHERIT_FROM_OWNER ) { +/* Is there an owner already? */ +if (vhost_dev_has_owner (d)) { + r = -EBUSY ; + goto done; + } +if (copy_from_user (&inherit_owner, argp, +sizeof (inherit_owner))) { + r = -EFAULT ; + goto done; + } + enforce_inherit_owner = inherit_owner; + goto done; + }/* If you are not the owner, you can become one */ if (ioctl ==VHOST_SET_OWNER ) {
include/uapi/linux/vhost.h +2 -0
@@ -235,4 +235,6 @@*/ #define VHOST_VDPA_GET_VRING_SIZE _IOWR(VHOST_VIRTIO, 0x82, \ struct vhost_vring_state) + +#define VHOST_SET_INHERIT_FROM_OWNER _IOW(VHOST_VIRTIO, 0x83, bool)#endif
Signature · 1 line
2.45.0
Re: [PATCH v2 7/7] vhost: Add new UAPI to support change to task mode
Quoted 4 lines · Cindy Lu
Add a new UAPI to support setting the vhost device to use task mode. The user space application needs to use
VHOST_SET_INHERIT_FROM_OWNERto set the mode. This setting must be set beforeVHOST_SET_OWNERis set.
Why?
Quoted 12 lines
Signed-off-by Cindy Lu <[email protected]>---
drivers/vhost/vhost.c| 18 +++++++++++++++++-include/uapi/linux/vhost.h| 2 ++2 files changed, 19 insertions(+), 1 deletion(-)drivers/vhost/vhost.c +0
-0
drivers/vhost/vhost.c long vhost_dev_ioctl(struct vhost_dev *d, unsigned int ioctl, void __user *argp) · line 2346
Quoted 7 lines
drivers/vhost/vhost.c +2
-1@@ -2341,8 +2341,24 @@ long vhost_dev_ioctl(struct vhost_dev *d, unsigned int ioctl, void __user *argp) {struct eventfd_ctx *ctx;u64 p; -long r; +long r =0 ;int i, fd; +bool inherit_owner;
^
This can be moved ...
drivers/vhost/vhost.c long vhost_dev_ioctl(struct vhost_dev *d, unsigned int ioctl, void __user *argp) · line 2348
Quoted 2 lines
drivers/vhost/vhost.c +2
-0+ +if (ioctl ==VHOST_SET_INHERIT_FROM_OWNER ) {
^
... here.drivers/vhost/vhost.c long vhost_dev_ioctl(struct vhost_dev *d, unsigned int ioctl, void __user *argp) · line 2349
Quoted 1 line
drivers/vhost/vhost.c +1
-0+/* Is there an owner already? */
This comment is superfluous, I would instead explain why there has to be an owner, what problem would we have if not?
drivers/vhost/vhost.c long vhost_dev_ioctl(struct vhost_dev *d, unsigned int ioctl, void __user *argp) · line 2359
Quoted 10 lines
drivers/vhost/vhost.c +10
-0+if (vhost_dev_has_owner (d)) { + r = -EBUSY ; + goto done; + } +if (copy_from_user (&inherit_owner, argp, +sizeof (inherit_owner))) { + r = -EFAULT ; + goto done; + } + enforce_inherit_owner = inherit_owner;
mmmm, I'm confused.
IIUC with this change, an user, for example, can call VHOST_SET_INHERIT_FROM_OWNER on /dev/vhost-vsock and change the behaviour of /dev/vhost-net.
Is that really what we want?
Maybe it's better if the module parameter controls the default of any device, but the ioctl changes the behavior only for that device.
Quoted 10 lines
drivers/vhost/vhost.c +2
-0+ goto done; + }/* If you are not the owner, you can become one */ if (ioctl ==VHOST_SET_OWNER ) {include/uapi/linux/vhost.h +0
-0
include/uapi/linux/vhost.h line 238
Quoted 4 lines
include/uapi/linux/vhost.h +1
-0@@ -235,4 +235,6 @@*/ #define VHOST_VDPA_GET_VRING_SIZE _IOWR(VHOST_VIRTIO, 0x82, \ struct vhost_vring_state) +
Please, add documentation here.
include/uapi/linux/vhost.h line 239
Quoted 1 line
include/uapi/linux/vhost.h +1
-0+#define VHOST_SET_INHERIT_FROM_OWNER _IOW(VHOST_VIRTIO, 0x83, bool)
^
Please, add a tab like we did for all previous definitions.
Thanks,
Stefano
Re: [PATCH v2 7/7] vhost: Add new UAPI to support change to task mode
In reply to Cindy LuQuoted 16 lines · Cindy Lu
Add a new UAPI to support setting the vhost device to use task mode. The user space application needs to use
VHOST_SET_INHERIT_FROM_OWNERto set the mode. This setting must be set beforeVHOST_SET_OWNERis set.Signed-off-by Cindy Lu <[email protected]>--- drivers/vhost/vhost.c | 18 +++++++++++++++++- include/uapi/linux/vhost.h | 2 ++ 2 files changed, 19 insertions(+), 1 deletion(-)drivers/vhost/vhost.c +0
-0
drivers/vhost/vhost.c long vhost_dev_ioctl(struct vhost_dev *d, unsigned int ioctl, void __user *argp) · line 2348
Quoted 9 lines
drivers/vhost/vhost.c +4
-1@@ -2341,8 +2341,24 @@ long vhost_dev_ioctl(struct vhost_dev *d, unsigned int ioctl, void __user *argp) {struct eventfd_ctx *ctx;u64 p; -long r; +long r =0 ;int i, fd; +bool inherit_owner; + +if (ioctl ==VHOST_SET_INHERIT_FROM_OWNER ) {
Maybe instead of a modparam and this ioctl we just want a new ioctl:
/*
* This will setup the owner based on the calling thread instead of
* using kthread.
*/
#define VHOST_INHERIT_OWNER _IO(VHOST_VIRTIO, 0x83)
It would initially be used by vhost-scsi when worker_per_virtqueue=true since that is a new use case and there will be no regressions.
For the other cases we default to VHOST_SET_OWNER. Other QEMU cases or tool XYZ can use the new ioctl when they are ready.
Re: [PATCH v2 7/7] vhost: Add new UAPI to support change to task mode
Quoted 36 lines · Mike Christie
Quoted 16 lines · Cindy Lu
Add a new UAPI to support setting the vhost device to use task mode. The user space application needs to use
VHOST_SET_INHERIT_FROM_OWNERto set the mode. This setting must be set beforeVHOST_SET_OWNERis set.Signed-off-by Cindy Lu <[email protected]>--- drivers/vhost/vhost.c | 18 +++++++++++++++++- include/uapi/linux/vhost.h | 2 ++ 2 files changed, 19 insertions(+), 1 deletion(-)drivers/vhost/vhost.c +0
-0drivers/vhost/vhost.c long vhost_dev_ioctl(struct vhost_dev *d, unsigned int ioctl, void __user *argp) · line 2348
Quoted 9 lines
drivers/vhost/vhost.c +4
-1@@ -2341,8 +2341,24 @@ long vhost_dev_ioctl(struct vhost_dev *d, unsigned int ioctl, void __user *argp) {struct eventfd_ctx *ctx;u64 p; -long r; +long r =0 ;int i, fd; +bool inherit_owner; + +if (ioctl ==VHOST_SET_INHERIT_FROM_OWNER ) {Maybe instead of a modparam and this ioctl we just want a new ioctl:
/*
* This will setup the owner based on the calling thread instead of
* using kthread.
*/
#defineVHOST_INHERIT_OWNER_IO(VHOST_VIRTIO, 0x83)
Quoted 6 lines
It would initially be used by vhost-scsi when worker_per_virtqueue=true since that is a new use case and there will be no regressions.
For the other cases we default to
VHOST_SET_OWNER. Other QEMU cases or tool XYZ can use the new ioctl when they are ready.
If I understand correctly, this means the default vhost function is using kthread?
Thanks
Cindy
Michael S. Tsirkin
maintainerRe: [PATCH v2 7/7] vhost: Add new UAPI to support change to task mode
In reply to Mike ChristieQuoted 29 lines · Mike Christie
Quoted 16 lines · Cindy Lu
Add a new UAPI to support setting the vhost device to use task mode. The user space application needs to use
VHOST_SET_INHERIT_FROM_OWNERto set the mode. This setting must be set beforeVHOST_SET_OWNERis set.Signed-off-by Cindy Lu <[email protected]>--- drivers/vhost/vhost.c | 18 +++++++++++++++++- include/uapi/linux/vhost.h | 2 ++ 2 files changed, 19 insertions(+), 1 deletion(-)drivers/vhost/vhost.c +0
-0drivers/vhost/vhost.c long vhost_dev_ioctl(struct vhost_dev *d, unsigned int ioctl, void __user *argp) · line 2348
Quoted 9 lines
drivers/vhost/vhost.c +4
-1@@ -2341,8 +2341,24 @@ long vhost_dev_ioctl(struct vhost_dev *d, unsigned int ioctl, void __user *argp) {struct eventfd_ctx *ctx;u64 p; -long r; +long r =0 ;int i, fd; +bool inherit_owner; + +if (ioctl ==VHOST_SET_INHERIT_FROM_OWNER ) {Maybe instead of a modparam and this ioctl we just want a new ioctl:
Quoted 5 lines
/*
* This will setup the owner based on the calling thread instead of
* using kthread.
*/
#defineVHOST_INHERIT_OWNER_IO(VHOST_VIRTIO, 0x83)
I feel this is not good because it is insecure - process should not normally have a say in whether namespaces work correctly.
So we want the system admin to be able to block the old mode.
Quoted 5 lines
It would initially be used by vhost-scsi when worker_per_virtqueue=true since that is a new use case and there will be no regressions.
For the other cases we default to
VHOST_SET_OWNER. Other QEMU cases or tool XYZ can use the new ioctl when they are ready.
I do not like it that we switched default now we apparently will be switching it back. Will break some userspace whatever we do.
Signature · 1 line
MST
Re: [PATCH v2 7/7] vhost: Add new UAPI to support change to task mode
Quoted 37 lines · Michael S. Tsirkin
Quoted 29 lines · Mike Christie
Quoted 16 lines · Cindy Lu
Add a new UAPI to support setting the vhost device to use task mode. The user space application needs to use
VHOST_SET_INHERIT_FROM_OWNERto set the mode. This setting must be set beforeVHOST_SET_OWNERis set.Signed-off-by Cindy Lu <[email protected]>--- drivers/vhost/vhost.c | 18 +++++++++++++++++- include/uapi/linux/vhost.h | 2 ++ 2 files changed, 19 insertions(+), 1 deletion(-)drivers/vhost/vhost.c +0
-0drivers/vhost/vhost.c long vhost_dev_ioctl(struct vhost_dev *d, unsigned int ioctl, void __user *argp) · line 2348
Quoted 9 lines
drivers/vhost/vhost.c +4
-1@@ -2341,8 +2341,24 @@ long vhost_dev_ioctl(struct vhost_dev *d, unsigned int ioctl, void __user *argp) {struct eventfd_ctx *ctx;u64 p; -long r; +long r =0 ;int i, fd; +bool inherit_owner; + +if (ioctl ==VHOST_SET_INHERIT_FROM_OWNER ) {Maybe instead of a modparam and this ioctl we just want a new ioctl:
Quoted 5 lines
/*
* This will setup the owner based on the calling thread instead of
* using kthread.
*/
#defineVHOST_INHERIT_OWNER_IO(VHOST_VIRTIO, 0x83)
Quoted 3 lines
I feel this is not good because it is insecure -
process should not normally have a say in whether
namespaces work correctly.
Note there's still a lot of kthread users, so the "problem" is not specific to vhost.
Quoted 2 lines
So we want the system admin to be able to block the old mode.
Then we will break the userspace silently which seems not good.
Quoted 4 lines
Quoted 4 lines
It would initially be used by vhost-scsi when worker_per_virtqueue=true since that is a new use case and there will be no regressions.
Quoted 9 lines
Quoted 2 lines
For the other cases we default to
VHOST_SET_OWNER. Other QEMU cases or tool XYZ can use the new ioctl when they are ready.I do not like it that we switched default now we apparently will be switching it back. Will break some userspace whatever we do.
--
MST
Thanks