Message ID | 20230302113421.174582-7-sgarzare@redhat.com (mailing list archive) |
---|---|
State | New, archived |
Headers | show |
Series | vdpa_sim: add support for user VA | expand |
On Thu, Mar 02, 2023 at 12:34:19PM +0100, Stefano Garzarella wrote: > Let's use our own kthread to run device jobs. > This allows us more flexibility, especially we can attach the kthread > to the user address space when vDPA uses user's VA. > > Signed-off-by: Stefano Garzarella <sgarzare@redhat.com> > --- > drivers/vdpa/vdpa_sim/vdpa_sim.h | 3 ++- > drivers/vdpa/vdpa_sim/vdpa_sim.c | 17 ++++++++++++----- > 2 files changed, 14 insertions(+), 6 deletions(-) > > diff --git a/drivers/vdpa/vdpa_sim/vdpa_sim.h b/drivers/vdpa/vdpa_sim/vdpa_sim.h > index acee20faaf6a..ce83f9130a5d 100644 > --- a/drivers/vdpa/vdpa_sim/vdpa_sim.h > +++ b/drivers/vdpa/vdpa_sim/vdpa_sim.h > @@ -57,7 +57,8 @@ struct vdpasim_dev_attr { > struct vdpasim { > struct vdpa_device vdpa; > struct vdpasim_virtqueue *vqs; > - struct work_struct work; > + struct kthread_worker *worker; > + struct kthread_work work; > struct vdpasim_dev_attr dev_attr; > /* spinlock to synchronize virtqueue state */ > spinlock_t lock; > diff --git a/drivers/vdpa/vdpa_sim/vdpa_sim.c b/drivers/vdpa/vdpa_sim/vdpa_sim.c > index a6ee830efc38..6feb29726c2a 100644 > --- a/drivers/vdpa/vdpa_sim/vdpa_sim.c > +++ b/drivers/vdpa/vdpa_sim/vdpa_sim.c > @@ -11,8 +11,8 @@ > #include <linux/module.h> > #include <linux/device.h> > #include <linux/kernel.h> > +#include <linux/kthread.h> > #include <linux/slab.h> > -#include <linux/sched.h> > #include <linux/dma-map-ops.h> > #include <linux/vringh.h> > #include <linux/vdpa.h> > @@ -116,7 +116,7 @@ static void vdpasim_do_reset(struct vdpasim *vdpasim) > static const struct vdpa_config_ops vdpasim_config_ops; > static const struct vdpa_config_ops vdpasim_batch_config_ops; > > -static void vdpasim_work_fn(struct work_struct *work) > +static void vdpasim_work_fn(struct kthread_work *work) > { > struct vdpasim *vdpasim = container_of(work, struct vdpasim, work); > > @@ -159,7 +159,13 @@ struct vdpasim *vdpasim_create(struct vdpasim_dev_attr *dev_attr, > > vdpasim = vdpa_to_sim(vdpa); > vdpasim->dev_attr = *dev_attr; > - INIT_WORK(&vdpasim->work, vdpasim_work_fn); > + > + kthread_init_work(&vdpasim->work, vdpasim_work_fn); > + vdpasim->worker = kthread_create_worker(0, "vDPA sim worker: %s", > + dev_attr->name); > + if (IS_ERR(vdpasim->worker)) > + goto err_iommu; Branching to err_iommu will result in a call to put_device(dev)... > + > spin_lock_init(&vdpasim->lock); > spin_lock_init(&vdpasim->iommu_lock); ... but dev is not initialised until the line following this hunk, which is: dev = &vdpasim->vdpa.dev; In order to avoid leaking dev I _think_ the correct approach is to move the initialisation of dev above the branch to err_iommu, perhaps above the call to kthread_init_work() is a good place. This does move the assignment outside the locks above. But I _think_ that is ok. > @@ -212,7 +218,7 @@ EXPORT_SYMBOL_GPL(vdpasim_create); > > void vdpasim_schedule_work(struct vdpasim *vdpasim) > { > - schedule_work(&vdpasim->work); > + kthread_queue_work(vdpasim->worker, &vdpasim->work); > } > EXPORT_SYMBOL_GPL(vdpasim_schedule_work); > > @@ -612,7 +618,8 @@ static void vdpasim_free(struct vdpa_device *vdpa) > struct vdpasim *vdpasim = vdpa_to_sim(vdpa); > int i; > > - cancel_work_sync(&vdpasim->work); > + kthread_cancel_work_sync(&vdpasim->work); > + kthread_destroy_worker(vdpasim->worker); > > for (i = 0; i < vdpasim->dev_attr.nvqs; i++) { > vringh_kiov_cleanup(&vdpasim->vqs[i].out_iov); > -- > 2.39.2 >
On Thu, Mar 02, 2023 at 04:30:04PM +0100, Simon Horman wrote: >On Thu, Mar 02, 2023 at 12:34:19PM +0100, Stefano Garzarella wrote: >> Let's use our own kthread to run device jobs. >> This allows us more flexibility, especially we can attach the kthread >> to the user address space when vDPA uses user's VA. >> >> Signed-off-by: Stefano Garzarella <sgarzare@redhat.com> >> --- >> drivers/vdpa/vdpa_sim/vdpa_sim.h | 3 ++- >> drivers/vdpa/vdpa_sim/vdpa_sim.c | 17 ++++++++++++----- >> 2 files changed, 14 insertions(+), 6 deletions(-) >> >> diff --git a/drivers/vdpa/vdpa_sim/vdpa_sim.h b/drivers/vdpa/vdpa_sim/vdpa_sim.h >> index acee20faaf6a..ce83f9130a5d 100644 >> --- a/drivers/vdpa/vdpa_sim/vdpa_sim.h >> +++ b/drivers/vdpa/vdpa_sim/vdpa_sim.h >> @@ -57,7 +57,8 @@ struct vdpasim_dev_attr { >> struct vdpasim { >> struct vdpa_device vdpa; >> struct vdpasim_virtqueue *vqs; >> - struct work_struct work; >> + struct kthread_worker *worker; >> + struct kthread_work work; >> struct vdpasim_dev_attr dev_attr; >> /* spinlock to synchronize virtqueue state */ >> spinlock_t lock; >> diff --git a/drivers/vdpa/vdpa_sim/vdpa_sim.c b/drivers/vdpa/vdpa_sim/vdpa_sim.c >> index a6ee830efc38..6feb29726c2a 100644 >> --- a/drivers/vdpa/vdpa_sim/vdpa_sim.c >> +++ b/drivers/vdpa/vdpa_sim/vdpa_sim.c >> @@ -11,8 +11,8 @@ >> #include <linux/module.h> >> #include <linux/device.h> >> #include <linux/kernel.h> >> +#include <linux/kthread.h> >> #include <linux/slab.h> >> -#include <linux/sched.h> >> #include <linux/dma-map-ops.h> >> #include <linux/vringh.h> >> #include <linux/vdpa.h> >> @@ -116,7 +116,7 @@ static void vdpasim_do_reset(struct vdpasim *vdpasim) >> static const struct vdpa_config_ops vdpasim_config_ops; >> static const struct vdpa_config_ops vdpasim_batch_config_ops; >> >> -static void vdpasim_work_fn(struct work_struct *work) >> +static void vdpasim_work_fn(struct kthread_work *work) >> { >> struct vdpasim *vdpasim = container_of(work, struct vdpasim, work); >> >> @@ -159,7 +159,13 @@ struct vdpasim *vdpasim_create(struct vdpasim_dev_attr *dev_attr, >> >> vdpasim = vdpa_to_sim(vdpa); >> vdpasim->dev_attr = *dev_attr; >> - INIT_WORK(&vdpasim->work, vdpasim_work_fn); >> + >> + kthread_init_work(&vdpasim->work, vdpasim_work_fn); >> + vdpasim->worker = kthread_create_worker(0, "vDPA sim worker: %s", >> + dev_attr->name); >> + if (IS_ERR(vdpasim->worker)) >> + goto err_iommu; > >Branching to err_iommu will result in a call to put_device(dev)... Good catch! > >> + >> spin_lock_init(&vdpasim->lock); >> spin_lock_init(&vdpasim->iommu_lock); > >... but dev is not initialised until the line following this hunk, >which is: > > dev = &vdpasim->vdpa.dev; > >In order to avoid leaking dev I _think_ the correct approach >is to move the initialisation of dev above the branch to >err_iommu, perhaps above the call to kthread_init_work() >is a good place. Yep, I agree. I'll fix in v3. Thanks, Stefano > >This does move the assignment outside the locks above. >But I _think_ that is ok. > >> @@ -212,7 +218,7 @@ EXPORT_SYMBOL_GPL(vdpasim_create); >> >> void vdpasim_schedule_work(struct vdpasim *vdpasim) >> { >> - schedule_work(&vdpasim->work); >> + kthread_queue_work(vdpasim->worker, &vdpasim->work); >> } >> EXPORT_SYMBOL_GPL(vdpasim_schedule_work); >> >> @@ -612,7 +618,8 @@ static void vdpasim_free(struct vdpa_device *vdpa) >> struct vdpasim *vdpasim = vdpa_to_sim(vdpa); >> int i; >> >> - cancel_work_sync(&vdpasim->work); >> + kthread_cancel_work_sync(&vdpasim->work); >> + kthread_destroy_worker(vdpasim->worker); >> >> for (i = 0; i < vdpasim->dev_attr.nvqs; i++) { >> vringh_kiov_cleanup(&vdpasim->vqs[i].out_iov); >> -- >> 2.39.2 >> >
Hi Stefano, I love your patch! Perhaps something to improve: [auto build test WARNING on mst-vhost/linux-next] [also build test WARNING on linus/master next-20230303] [cannot apply to v6.2] [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/Stefano-Garzarella/vdpa-add-bind_mm-unbind_mm-callbacks/20230302-193850 base: https://git.kernel.org/pub/scm/linux/kernel/git/mst/vhost.git linux-next patch link: https://lore.kernel.org/r/20230302113421.174582-7-sgarzare%40redhat.com patch subject: [PATCH v2 6/8] vdpa_sim: use kthread worker config: i386-allyesconfig (https://download.01.org/0day-ci/archive/20230305/202303051841.bPAIzJRy-lkp@intel.com/config) compiler: clang version 14.0.6 (https://github.com/llvm/llvm-project f28c006a5895fc0e329fe15fead81e37457cb1d1) reproduce (this is a W=1 build): wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross chmod +x ~/bin/make.cross # https://github.com/intel-lab-lkp/linux/commit/5b2107457ac0e7b1bb0aa3635ebf13b02e82bb78 git remote add linux-review https://github.com/intel-lab-lkp/linux git fetch --no-tags linux-review Stefano-Garzarella/vdpa-add-bind_mm-unbind_mm-callbacks/20230302-193850 git checkout 5b2107457ac0e7b1bb0aa3635ebf13b02e82bb78 # save the config file mkdir build_dir && cp config build_dir/.config COMPILER_INSTALL_PATH=$HOME/0day COMPILER=clang make.cross W=1 O=build_dir ARCH=i386 olddefconfig COMPILER_INSTALL_PATH=$HOME/0day COMPILER=clang make.cross W=1 O=build_dir ARCH=i386 SHELL=/bin/bash drivers/net/wireless/ath/ath10k/ drivers/vdpa/vdpa_sim/ fs/erofs/ If you fix the issue, kindly add following tag where applicable | Reported-by: kernel test robot <lkp@intel.com> | Link: https://lore.kernel.org/oe-kbuild-all/202303051841.bPAIzJRy-lkp@intel.com/ All warnings (new ones prefixed by >>): >> drivers/vdpa/vdpa_sim/vdpa_sim.c:166:6: warning: variable 'dev' is used uninitialized whenever 'if' condition is true [-Wsometimes-uninitialized] if (IS_ERR(vdpasim->worker)) ^~~~~~~~~~~~~~~~~~~~~~~ drivers/vdpa/vdpa_sim/vdpa_sim.c:213:13: note: uninitialized use occurs here put_device(dev); ^~~ drivers/vdpa/vdpa_sim/vdpa_sim.c:166:2: note: remove the 'if' if its condition is always false if (IS_ERR(vdpasim->worker)) ^~~~~~~~~~~~~~~~~~~~~~~~~~~~ drivers/vdpa/vdpa_sim/vdpa_sim.c:132:20: note: initialize the variable 'dev' to silence this warning struct device *dev; ^ = NULL 1 warning generated. vim +166 drivers/vdpa/vdpa_sim/vdpa_sim.c 125 126 struct vdpasim *vdpasim_create(struct vdpasim_dev_attr *dev_attr, 127 const struct vdpa_dev_set_config *config) 128 { 129 const struct vdpa_config_ops *ops; 130 struct vdpa_device *vdpa; 131 struct vdpasim *vdpasim; 132 struct device *dev; 133 int i, ret = -ENOMEM; 134 135 if (!dev_attr->alloc_size) 136 return ERR_PTR(-EINVAL); 137 138 if (config->mask & BIT_ULL(VDPA_ATTR_DEV_FEATURES)) { 139 if (config->device_features & 140 ~dev_attr->supported_features) 141 return ERR_PTR(-EINVAL); 142 dev_attr->supported_features = 143 config->device_features; 144 } 145 146 if (batch_mapping) 147 ops = &vdpasim_batch_config_ops; 148 else 149 ops = &vdpasim_config_ops; 150 151 vdpa = __vdpa_alloc_device(NULL, ops, 152 dev_attr->ngroups, dev_attr->nas, 153 dev_attr->alloc_size, 154 dev_attr->name, false); 155 if (IS_ERR(vdpa)) { 156 ret = PTR_ERR(vdpa); 157 goto err_alloc; 158 } 159 160 vdpasim = vdpa_to_sim(vdpa); 161 vdpasim->dev_attr = *dev_attr; 162 163 kthread_init_work(&vdpasim->work, vdpasim_work_fn); 164 vdpasim->worker = kthread_create_worker(0, "vDPA sim worker: %s", 165 dev_attr->name); > 166 if (IS_ERR(vdpasim->worker)) 167 goto err_iommu; 168 169 spin_lock_init(&vdpasim->lock); 170 spin_lock_init(&vdpasim->iommu_lock); 171 172 dev = &vdpasim->vdpa.dev; 173 dev->dma_mask = &dev->coherent_dma_mask; 174 if (dma_set_mask_and_coherent(dev, DMA_BIT_MASK(64))) 175 goto err_iommu; 176 vdpasim->vdpa.mdev = dev_attr->mgmt_dev; 177 178 vdpasim->config = kzalloc(dev_attr->config_size, GFP_KERNEL); 179 if (!vdpasim->config) 180 goto err_iommu; 181 182 vdpasim->vqs = kcalloc(dev_attr->nvqs, sizeof(struct vdpasim_virtqueue), 183 GFP_KERNEL); 184 if (!vdpasim->vqs) 185 goto err_iommu; 186 187 vdpasim->iommu = kmalloc_array(vdpasim->dev_attr.nas, 188 sizeof(*vdpasim->iommu), GFP_KERNEL); 189 if (!vdpasim->iommu) 190 goto err_iommu; 191 192 vdpasim->iommu_pt = kmalloc_array(vdpasim->dev_attr.nas, 193 sizeof(*vdpasim->iommu_pt), GFP_KERNEL); 194 if (!vdpasim->iommu_pt) 195 goto err_iommu; 196 197 for (i = 0; i < vdpasim->dev_attr.nas; i++) 198 vhost_iotlb_init(&vdpasim->iommu[i], max_iotlb_entries, 0); 199 200 vdpasim->buffer = kvmalloc(dev_attr->buffer_size, GFP_KERNEL); 201 if (!vdpasim->buffer) 202 goto err_iommu; 203 204 for (i = 0; i < dev_attr->nvqs; i++) 205 vringh_set_iotlb(&vdpasim->vqs[i].vring, &vdpasim->iommu[0], 206 &vdpasim->iommu_lock); 207 208 vdpasim->vdpa.dma_dev = dev; 209 210 return vdpasim; 211 212 err_iommu: 213 put_device(dev); 214 err_alloc: 215 return ERR_PTR(ret); 216 } 217 EXPORT_SYMBOL_GPL(vdpasim_create); 218
On Thu, Mar 2, 2023 at 7:35 PM Stefano Garzarella <sgarzare@redhat.com> wrote: > > Let's use our own kthread to run device jobs. > This allows us more flexibility, especially we can attach the kthread > to the user address space when vDPA uses user's VA. > > Signed-off-by: Stefano Garzarella <sgarzare@redhat.com> Acked-by: Jason Wang <jasowang@redhat.com> Thanks > --- > drivers/vdpa/vdpa_sim/vdpa_sim.h | 3 ++- > drivers/vdpa/vdpa_sim/vdpa_sim.c | 17 ++++++++++++----- > 2 files changed, 14 insertions(+), 6 deletions(-) > > diff --git a/drivers/vdpa/vdpa_sim/vdpa_sim.h b/drivers/vdpa/vdpa_sim/vdpa_sim.h > index acee20faaf6a..ce83f9130a5d 100644 > --- a/drivers/vdpa/vdpa_sim/vdpa_sim.h > +++ b/drivers/vdpa/vdpa_sim/vdpa_sim.h > @@ -57,7 +57,8 @@ struct vdpasim_dev_attr { > struct vdpasim { > struct vdpa_device vdpa; > struct vdpasim_virtqueue *vqs; > - struct work_struct work; > + struct kthread_worker *worker; > + struct kthread_work work; > struct vdpasim_dev_attr dev_attr; > /* spinlock to synchronize virtqueue state */ > spinlock_t lock; > diff --git a/drivers/vdpa/vdpa_sim/vdpa_sim.c b/drivers/vdpa/vdpa_sim/vdpa_sim.c > index a6ee830efc38..6feb29726c2a 100644 > --- a/drivers/vdpa/vdpa_sim/vdpa_sim.c > +++ b/drivers/vdpa/vdpa_sim/vdpa_sim.c > @@ -11,8 +11,8 @@ > #include <linux/module.h> > #include <linux/device.h> > #include <linux/kernel.h> > +#include <linux/kthread.h> > #include <linux/slab.h> > -#include <linux/sched.h> > #include <linux/dma-map-ops.h> > #include <linux/vringh.h> > #include <linux/vdpa.h> > @@ -116,7 +116,7 @@ static void vdpasim_do_reset(struct vdpasim *vdpasim) > static const struct vdpa_config_ops vdpasim_config_ops; > static const struct vdpa_config_ops vdpasim_batch_config_ops; > > -static void vdpasim_work_fn(struct work_struct *work) > +static void vdpasim_work_fn(struct kthread_work *work) > { > struct vdpasim *vdpasim = container_of(work, struct vdpasim, work); > > @@ -159,7 +159,13 @@ struct vdpasim *vdpasim_create(struct vdpasim_dev_attr *dev_attr, > > vdpasim = vdpa_to_sim(vdpa); > vdpasim->dev_attr = *dev_attr; > - INIT_WORK(&vdpasim->work, vdpasim_work_fn); > + > + kthread_init_work(&vdpasim->work, vdpasim_work_fn); > + vdpasim->worker = kthread_create_worker(0, "vDPA sim worker: %s", > + dev_attr->name); > + if (IS_ERR(vdpasim->worker)) > + goto err_iommu; > + > spin_lock_init(&vdpasim->lock); > spin_lock_init(&vdpasim->iommu_lock); > > @@ -212,7 +218,7 @@ EXPORT_SYMBOL_GPL(vdpasim_create); > > void vdpasim_schedule_work(struct vdpasim *vdpasim) > { > - schedule_work(&vdpasim->work); > + kthread_queue_work(vdpasim->worker, &vdpasim->work); > } > EXPORT_SYMBOL_GPL(vdpasim_schedule_work); > > @@ -612,7 +618,8 @@ static void vdpasim_free(struct vdpa_device *vdpa) > struct vdpasim *vdpasim = vdpa_to_sim(vdpa); > int i; > > - cancel_work_sync(&vdpasim->work); > + kthread_cancel_work_sync(&vdpasim->work); > + kthread_destroy_worker(vdpasim->worker); > > for (i = 0; i < vdpasim->dev_attr.nvqs; i++) { > vringh_kiov_cleanup(&vdpasim->vqs[i].out_iov); > -- > 2.39.2 >
diff --git a/drivers/vdpa/vdpa_sim/vdpa_sim.h b/drivers/vdpa/vdpa_sim/vdpa_sim.h index acee20faaf6a..ce83f9130a5d 100644 --- a/drivers/vdpa/vdpa_sim/vdpa_sim.h +++ b/drivers/vdpa/vdpa_sim/vdpa_sim.h @@ -57,7 +57,8 @@ struct vdpasim_dev_attr { struct vdpasim { struct vdpa_device vdpa; struct vdpasim_virtqueue *vqs; - struct work_struct work; + struct kthread_worker *worker; + struct kthread_work work; struct vdpasim_dev_attr dev_attr; /* spinlock to synchronize virtqueue state */ spinlock_t lock; diff --git a/drivers/vdpa/vdpa_sim/vdpa_sim.c b/drivers/vdpa/vdpa_sim/vdpa_sim.c index a6ee830efc38..6feb29726c2a 100644 --- a/drivers/vdpa/vdpa_sim/vdpa_sim.c +++ b/drivers/vdpa/vdpa_sim/vdpa_sim.c @@ -11,8 +11,8 @@ #include <linux/module.h> #include <linux/device.h> #include <linux/kernel.h> +#include <linux/kthread.h> #include <linux/slab.h> -#include <linux/sched.h> #include <linux/dma-map-ops.h> #include <linux/vringh.h> #include <linux/vdpa.h> @@ -116,7 +116,7 @@ static void vdpasim_do_reset(struct vdpasim *vdpasim) static const struct vdpa_config_ops vdpasim_config_ops; static const struct vdpa_config_ops vdpasim_batch_config_ops; -static void vdpasim_work_fn(struct work_struct *work) +static void vdpasim_work_fn(struct kthread_work *work) { struct vdpasim *vdpasim = container_of(work, struct vdpasim, work); @@ -159,7 +159,13 @@ struct vdpasim *vdpasim_create(struct vdpasim_dev_attr *dev_attr, vdpasim = vdpa_to_sim(vdpa); vdpasim->dev_attr = *dev_attr; - INIT_WORK(&vdpasim->work, vdpasim_work_fn); + + kthread_init_work(&vdpasim->work, vdpasim_work_fn); + vdpasim->worker = kthread_create_worker(0, "vDPA sim worker: %s", + dev_attr->name); + if (IS_ERR(vdpasim->worker)) + goto err_iommu; + spin_lock_init(&vdpasim->lock); spin_lock_init(&vdpasim->iommu_lock); @@ -212,7 +218,7 @@ EXPORT_SYMBOL_GPL(vdpasim_create); void vdpasim_schedule_work(struct vdpasim *vdpasim) { - schedule_work(&vdpasim->work); + kthread_queue_work(vdpasim->worker, &vdpasim->work); } EXPORT_SYMBOL_GPL(vdpasim_schedule_work); @@ -612,7 +618,8 @@ static void vdpasim_free(struct vdpa_device *vdpa) struct vdpasim *vdpasim = vdpa_to_sim(vdpa); int i; - cancel_work_sync(&vdpasim->work); + kthread_cancel_work_sync(&vdpasim->work); + kthread_destroy_worker(vdpasim->worker); for (i = 0; i < vdpasim->dev_attr.nvqs; i++) { vringh_kiov_cleanup(&vdpasim->vqs[i].out_iov);
Let's use our own kthread to run device jobs. This allows us more flexibility, especially we can attach the kthread to the user address space when vDPA uses user's VA. Signed-off-by: Stefano Garzarella <sgarzare@redhat.com> --- drivers/vdpa/vdpa_sim/vdpa_sim.h | 3 ++- drivers/vdpa/vdpa_sim/vdpa_sim.c | 17 ++++++++++++----- 2 files changed, 14 insertions(+), 6 deletions(-)