Message ID | 20230227111135.61728-12-yi.l.liu@intel.com (mailing list archive) |
---|---|
State | New, archived |
Headers | show |
Series | Add vfio_device cdev for iommufd support | expand |
On Mon, Feb 27, 2023 at 03:11:27AM -0800, Yi Liu wrote: > diff --git a/drivers/vfio/fsl-mc/vfio_fsl_mc.c b/drivers/vfio/fsl-mc/vfio_fsl_mc.c > index c89a047a4cd8..d540cf683d93 100644 > --- a/drivers/vfio/fsl-mc/vfio_fsl_mc.c > +++ b/drivers/vfio/fsl-mc/vfio_fsl_mc.c > @@ -594,6 +594,7 @@ static const struct vfio_device_ops vfio_fsl_mc_ops = { > .bind_iommufd = vfio_iommufd_physical_bind, > .unbind_iommufd = vfio_iommufd_physical_unbind, > .attach_ioas = vfio_iommufd_physical_attach_ioas, > + .detach_ioas = vfio_iommufd_physical_detach_ioas, > }; > > static struct fsl_mc_driver vfio_fsl_mc_driver = { > diff --git a/drivers/vfio/iommufd.c b/drivers/vfio/iommufd.c > index beef6ca21107..bfaa9876499b 100644 > --- a/drivers/vfio/iommufd.c > +++ b/drivers/vfio/iommufd.c > @@ -88,6 +88,14 @@ int vfio_iommufd_physical_attach_ioas(struct vfio_device *vdev, u32 *pt_id) > { > int rc; > > + lockdep_assert_held(&vdev->dev_set->lock); > + > + if (!vdev->iommufd_device) > + return -EINVAL; This should be a WARN_ON. The vdev->iommufd_ctx should be NULL if it hasn't been bound, and it can't be bound unless the iommufd_device/attach was created. > @@ -96,6 +104,18 @@ int vfio_iommufd_physical_attach_ioas(struct vfio_device *vdev, u32 *pt_id) > } > EXPORT_SYMBOL_GPL(vfio_iommufd_physical_attach_ioas); > > +void vfio_iommufd_physical_detach_ioas(struct vfio_device *vdev) > +{ > + lockdep_assert_held(&vdev->dev_set->lock); > + > + if (!vdev->iommufd_device || !vdev->iommufd_attached) > + return; Same Jason
> From: Jason Gunthorpe <jgg@nvidia.com> > Sent: Tuesday, February 28, 2023 2:45 AM > > On Mon, Feb 27, 2023 at 03:11:27AM -0800, Yi Liu wrote: > > diff --git a/drivers/vfio/fsl-mc/vfio_fsl_mc.c b/drivers/vfio/fsl- > mc/vfio_fsl_mc.c > > index c89a047a4cd8..d540cf683d93 100644 > > --- a/drivers/vfio/fsl-mc/vfio_fsl_mc.c > > +++ b/drivers/vfio/fsl-mc/vfio_fsl_mc.c > > @@ -594,6 +594,7 @@ static const struct vfio_device_ops > vfio_fsl_mc_ops = { > > .bind_iommufd = vfio_iommufd_physical_bind, > > .unbind_iommufd = vfio_iommufd_physical_unbind, > > .attach_ioas = vfio_iommufd_physical_attach_ioas, > > + .detach_ioas = vfio_iommufd_physical_detach_ioas, > > }; > > > > static struct fsl_mc_driver vfio_fsl_mc_driver = { > > diff --git a/drivers/vfio/iommufd.c b/drivers/vfio/iommufd.c > > index beef6ca21107..bfaa9876499b 100644 > > --- a/drivers/vfio/iommufd.c > > +++ b/drivers/vfio/iommufd.c > > @@ -88,6 +88,14 @@ int vfio_iommufd_physical_attach_ioas(struct > vfio_device *vdev, u32 *pt_id) > > { > > int rc; > > > > + lockdep_assert_held(&vdev->dev_set->lock); > > + > > + if (!vdev->iommufd_device) > > + return -EINVAL; > > This should be a WARN_ON. The vdev->iommufd_ctx should be NULL if it > hasn't been bound, and it can't be bound unless the > iommufd_device/attach was created. sure. But it is a user-triggerable warn. If userspace triggers it on purpose, will it be a bad thing for kernel? Maybe use dev_warn_ratelimited()? > > @@ -96,6 +104,18 @@ int vfio_iommufd_physical_attach_ioas(struct > vfio_device *vdev, u32 *pt_id) > > } > > EXPORT_SYMBOL_GPL(vfio_iommufd_physical_attach_ioas); > > > > +void vfio_iommufd_physical_detach_ioas(struct vfio_device *vdev) > > +{ > > + lockdep_assert_held(&vdev->dev_set->lock); > > + > > + if (!vdev->iommufd_device || !vdev->iommufd_attached) > > + return; > > Same Sure. Will apply same warn when above comment is aligned. Regards, Yi Liu
On Tue, Feb 28, 2023 at 02:57:42AM +0000, Liu, Yi L wrote: > > From: Jason Gunthorpe <jgg@nvidia.com> > > Sent: Tuesday, February 28, 2023 2:45 AM > > > > On Mon, Feb 27, 2023 at 03:11:27AM -0800, Yi Liu wrote: > > > diff --git a/drivers/vfio/fsl-mc/vfio_fsl_mc.c b/drivers/vfio/fsl- > > mc/vfio_fsl_mc.c > > > index c89a047a4cd8..d540cf683d93 100644 > > > --- a/drivers/vfio/fsl-mc/vfio_fsl_mc.c > > > +++ b/drivers/vfio/fsl-mc/vfio_fsl_mc.c > > > @@ -594,6 +594,7 @@ static const struct vfio_device_ops > > vfio_fsl_mc_ops = { > > > .bind_iommufd = vfio_iommufd_physical_bind, > > > .unbind_iommufd = vfio_iommufd_physical_unbind, > > > .attach_ioas = vfio_iommufd_physical_attach_ioas, > > > + .detach_ioas = vfio_iommufd_physical_detach_ioas, > > > }; > > > > > > static struct fsl_mc_driver vfio_fsl_mc_driver = { > > > diff --git a/drivers/vfio/iommufd.c b/drivers/vfio/iommufd.c > > > index beef6ca21107..bfaa9876499b 100644 > > > --- a/drivers/vfio/iommufd.c > > > +++ b/drivers/vfio/iommufd.c > > > @@ -88,6 +88,14 @@ int vfio_iommufd_physical_attach_ioas(struct > > vfio_device *vdev, u32 *pt_id) > > > { > > > int rc; > > > > > > + lockdep_assert_held(&vdev->dev_set->lock); > > > + > > > + if (!vdev->iommufd_device) > > > + return -EINVAL; > > > > This should be a WARN_ON. The vdev->iommufd_ctx should be NULL if it > > hasn't been bound, and it can't be bound unless the > > iommufd_device/attach was created. > > sure. But it is a user-triggerable warn. If userspace triggers it on > purpose, will it be a bad thing for kernel? Maybe use > dev_warn_ratelimited()? How can it be user triggerable? You shouldn't be able to reach this function until the device is bound because the ioctl should be after the is it bound check Jason
> From: Jason Gunthorpe <jgg@nvidia.com> > Sent: Tuesday, February 28, 2023 8:33 PM > > On Tue, Feb 28, 2023 at 02:57:42AM +0000, Liu, Yi L wrote: > > > From: Jason Gunthorpe <jgg@nvidia.com> > > > Sent: Tuesday, February 28, 2023 2:45 AM > > > > > > On Mon, Feb 27, 2023 at 03:11:27AM -0800, Yi Liu wrote: > > > > diff --git a/drivers/vfio/fsl-mc/vfio_fsl_mc.c b/drivers/vfio/fsl- > > > mc/vfio_fsl_mc.c > > > > index c89a047a4cd8..d540cf683d93 100644 > > > > --- a/drivers/vfio/fsl-mc/vfio_fsl_mc.c > > > > +++ b/drivers/vfio/fsl-mc/vfio_fsl_mc.c > > > > @@ -594,6 +594,7 @@ static const struct vfio_device_ops > > > vfio_fsl_mc_ops = { > > > > .bind_iommufd = vfio_iommufd_physical_bind, > > > > .unbind_iommufd = vfio_iommufd_physical_unbind, > > > > .attach_ioas = vfio_iommufd_physical_attach_ioas, > > > > + .detach_ioas = vfio_iommufd_physical_detach_ioas, > > > > }; > > > > > > > > static struct fsl_mc_driver vfio_fsl_mc_driver = { > > > > diff --git a/drivers/vfio/iommufd.c b/drivers/vfio/iommufd.c > > > > index beef6ca21107..bfaa9876499b 100644 > > > > --- a/drivers/vfio/iommufd.c > > > > +++ b/drivers/vfio/iommufd.c > > > > @@ -88,6 +88,14 @@ int vfio_iommufd_physical_attach_ioas(struct > > > vfio_device *vdev, u32 *pt_id) > > > > { > > > > int rc; > > > > > > > > + lockdep_assert_held(&vdev->dev_set->lock); > > > > + > > > > + if (!vdev->iommufd_device) > > > > + return -EINVAL; > > > > > > This should be a WARN_ON. The vdev->iommufd_ctx should be NULL if it > > > hasn't been bound, and it can't be bound unless the > > > iommufd_device/attach was created. > > > > sure. But it is a user-triggerable warn. If userspace triggers it on > > purpose, will it be a bad thing for kernel? Maybe use > > dev_warn_ratelimited()? > > How can it be user triggerable? You shouldn't be able to reach this > function until the device is bound because the ioctl should be after > the is it bound check Oh, yes. it is! ioctls are blocked until bound to iommufd. Regards, Yi Liu
diff --git a/Documentation/driver-api/vfio.rst b/Documentation/driver-api/vfio.rst index 50b690f7f663..44527420f20d 100644 --- a/Documentation/driver-api/vfio.rst +++ b/Documentation/driver-api/vfio.rst @@ -279,6 +279,7 @@ similar to a file operations structure:: struct iommufd_ctx *ictx, u32 *out_device_id); void (*unbind_iommufd)(struct vfio_device *vdev); int (*attach_ioas)(struct vfio_device *vdev, u32 *pt_id); + void (*detach_ioas)(struct vfio_device *vdev); int (*open_device)(struct vfio_device *vdev); void (*close_device)(struct vfio_device *vdev); ssize_t (*read)(struct vfio_device *vdev, char __user *buf, @@ -315,9 +316,10 @@ container_of(). - The [un]bind_iommufd callbacks are issued when the device is bound to and unbound from iommufd. - - The attach_ioas callback is issued when the device is attached to an - IOAS managed by the bound iommufd. The attached IOAS is automatically - detached when the device is unbound from iommufd. + - The [de]attach_ioas callback is issued when the device is attached to + and detached from an IOAS managed by the bound iommufd. However, the + attached IOAS can also be automatically detached when the device is + unbound from iommufd. - The read/write/mmap callbacks implement the device region access defined by the device's own VFIO_DEVICE_GET_REGION_INFO ioctl. diff --git a/drivers/vfio/fsl-mc/vfio_fsl_mc.c b/drivers/vfio/fsl-mc/vfio_fsl_mc.c index c89a047a4cd8..d540cf683d93 100644 --- a/drivers/vfio/fsl-mc/vfio_fsl_mc.c +++ b/drivers/vfio/fsl-mc/vfio_fsl_mc.c @@ -594,6 +594,7 @@ static const struct vfio_device_ops vfio_fsl_mc_ops = { .bind_iommufd = vfio_iommufd_physical_bind, .unbind_iommufd = vfio_iommufd_physical_unbind, .attach_ioas = vfio_iommufd_physical_attach_ioas, + .detach_ioas = vfio_iommufd_physical_detach_ioas, }; static struct fsl_mc_driver vfio_fsl_mc_driver = { diff --git a/drivers/vfio/iommufd.c b/drivers/vfio/iommufd.c index beef6ca21107..bfaa9876499b 100644 --- a/drivers/vfio/iommufd.c +++ b/drivers/vfio/iommufd.c @@ -88,6 +88,14 @@ int vfio_iommufd_physical_attach_ioas(struct vfio_device *vdev, u32 *pt_id) { int rc; + lockdep_assert_held(&vdev->dev_set->lock); + + if (!vdev->iommufd_device) + return -EINVAL; + + if (vdev->iommufd_attached) + return -EBUSY; + rc = iommufd_device_attach(vdev->iommufd_device, pt_id); if (rc) return rc; @@ -96,6 +104,18 @@ int vfio_iommufd_physical_attach_ioas(struct vfio_device *vdev, u32 *pt_id) } EXPORT_SYMBOL_GPL(vfio_iommufd_physical_attach_ioas); +void vfio_iommufd_physical_detach_ioas(struct vfio_device *vdev) +{ + lockdep_assert_held(&vdev->dev_set->lock); + + if (!vdev->iommufd_device || !vdev->iommufd_attached) + return; + + iommufd_device_detach(vdev->iommufd_device); + vdev->iommufd_attached = false; +} +EXPORT_SYMBOL_GPL(vfio_iommufd_physical_detach_ioas); + /* * The emulated standard ops mean that vfio_device is going to use the * "mdev path" and will call vfio_pin_pages()/vfio_dma_rw(). Drivers using this diff --git a/drivers/vfio/pci/hisilicon/hisi_acc_vfio_pci.c b/drivers/vfio/pci/hisilicon/hisi_acc_vfio_pci.c index a117eaf21c14..b2f9778c8366 100644 --- a/drivers/vfio/pci/hisilicon/hisi_acc_vfio_pci.c +++ b/drivers/vfio/pci/hisilicon/hisi_acc_vfio_pci.c @@ -1373,6 +1373,7 @@ static const struct vfio_device_ops hisi_acc_vfio_pci_migrn_ops = { .bind_iommufd = vfio_iommufd_physical_bind, .unbind_iommufd = vfio_iommufd_physical_unbind, .attach_ioas = vfio_iommufd_physical_attach_ioas, + .detach_ioas = vfio_iommufd_physical_detach_ioas, }; static const struct vfio_device_ops hisi_acc_vfio_pci_ops = { @@ -1391,6 +1392,7 @@ static const struct vfio_device_ops hisi_acc_vfio_pci_ops = { .bind_iommufd = vfio_iommufd_physical_bind, .unbind_iommufd = vfio_iommufd_physical_unbind, .attach_ioas = vfio_iommufd_physical_attach_ioas, + .detach_ioas = vfio_iommufd_physical_detach_ioas, }; static int hisi_acc_vfio_pci_probe(struct pci_dev *pdev, const struct pci_device_id *id) diff --git a/drivers/vfio/pci/mlx5/main.c b/drivers/vfio/pci/mlx5/main.c index e897537a9e8a..6fc3410989eb 100644 --- a/drivers/vfio/pci/mlx5/main.c +++ b/drivers/vfio/pci/mlx5/main.c @@ -1326,6 +1326,7 @@ static const struct vfio_device_ops mlx5vf_pci_ops = { .bind_iommufd = vfio_iommufd_physical_bind, .unbind_iommufd = vfio_iommufd_physical_unbind, .attach_ioas = vfio_iommufd_physical_attach_ioas, + .detach_ioas = vfio_iommufd_physical_detach_ioas, }; static int mlx5vf_pci_probe(struct pci_dev *pdev, diff --git a/drivers/vfio/pci/vfio_pci.c b/drivers/vfio/pci/vfio_pci.c index 29091ee2e984..cb5b7f865d58 100644 --- a/drivers/vfio/pci/vfio_pci.c +++ b/drivers/vfio/pci/vfio_pci.c @@ -141,6 +141,7 @@ static const struct vfio_device_ops vfio_pci_ops = { .bind_iommufd = vfio_iommufd_physical_bind, .unbind_iommufd = vfio_iommufd_physical_unbind, .attach_ioas = vfio_iommufd_physical_attach_ioas, + .detach_ioas = vfio_iommufd_physical_detach_ioas, }; static int vfio_pci_probe(struct pci_dev *pdev, const struct pci_device_id *id) diff --git a/drivers/vfio/platform/vfio_amba.c b/drivers/vfio/platform/vfio_amba.c index 83fe54015595..6464b3939ebc 100644 --- a/drivers/vfio/platform/vfio_amba.c +++ b/drivers/vfio/platform/vfio_amba.c @@ -119,6 +119,7 @@ static const struct vfio_device_ops vfio_amba_ops = { .bind_iommufd = vfio_iommufd_physical_bind, .unbind_iommufd = vfio_iommufd_physical_unbind, .attach_ioas = vfio_iommufd_physical_attach_ioas, + .detach_ioas = vfio_iommufd_physical_detach_ioas, }; static const struct amba_id pl330_ids[] = { diff --git a/drivers/vfio/platform/vfio_platform.c b/drivers/vfio/platform/vfio_platform.c index 22a1efca32a8..8cf22fa65baa 100644 --- a/drivers/vfio/platform/vfio_platform.c +++ b/drivers/vfio/platform/vfio_platform.c @@ -108,6 +108,7 @@ static const struct vfio_device_ops vfio_platform_ops = { .bind_iommufd = vfio_iommufd_physical_bind, .unbind_iommufd = vfio_iommufd_physical_unbind, .attach_ioas = vfio_iommufd_physical_attach_ioas, + .detach_ioas = vfio_iommufd_physical_detach_ioas, }; static struct platform_driver vfio_platform_driver = { diff --git a/drivers/vfio/vfio_main.c b/drivers/vfio/vfio_main.c index 19ea65a87072..a51c0a0e8a1a 100644 --- a/drivers/vfio/vfio_main.c +++ b/drivers/vfio/vfio_main.c @@ -250,7 +250,8 @@ static int __vfio_register_dev(struct vfio_device *device, if (WARN_ON(device->ops->bind_iommufd && (!device->ops->unbind_iommufd || - !device->ops->attach_ioas))) + !device->ops->attach_ioas || + !device->ops->detach_ioas))) return -EINVAL; /* diff --git a/include/linux/vfio.h b/include/linux/vfio.h index f2d3d6997ad7..9815a8c4ac7c 100644 --- a/include/linux/vfio.h +++ b/include/linux/vfio.h @@ -73,7 +73,9 @@ struct vfio_device { * @bind_iommufd: Called when binding the device to an iommufd * @unbind_iommufd: Opposite of bind_iommufd * @attach_ioas: Called when attaching device to an IOAS/HWPT managed by the - * bound iommufd. Undo in unbind_iommufd. + * bound iommufd. Undo in unbind_iommufd if @detach_ioas is not + * called + * @detach_ioas: Opposite of attach_ioas * @open_device: Called when the first file descriptor is opened for this device * @close_device: Opposite of open_device * @read: Perform read(2) on device file descriptor @@ -97,6 +99,7 @@ struct vfio_device_ops { struct iommufd_ctx *ictx, u32 *out_device_id); void (*unbind_iommufd)(struct vfio_device *vdev); int (*attach_ioas)(struct vfio_device *vdev, u32 *pt_id); + void (*detach_ioas)(struct vfio_device *vdev); int (*open_device)(struct vfio_device *vdev); void (*close_device)(struct vfio_device *vdev); ssize_t (*read)(struct vfio_device *vdev, char __user *buf, @@ -118,6 +121,7 @@ int vfio_iommufd_physical_bind(struct vfio_device *vdev, struct iommufd_ctx *ictx, u32 *out_device_id); void vfio_iommufd_physical_unbind(struct vfio_device *vdev); int vfio_iommufd_physical_attach_ioas(struct vfio_device *vdev, u32 *pt_id); +void vfio_iommufd_physical_detach_ioas(struct vfio_device *vdev); int vfio_iommufd_emulated_bind(struct vfio_device *vdev, struct iommufd_ctx *ictx, u32 *out_device_id); void vfio_iommufd_emulated_unbind(struct vfio_device *vdev); @@ -130,6 +134,8 @@ int vfio_iommufd_emulated_attach_ioas(struct vfio_device *vdev, u32 *pt_id); ((void (*)(struct vfio_device *vdev)) NULL) #define vfio_iommufd_physical_attach_ioas \ ((int (*)(struct vfio_device *vdev, u32 *pt_id)) NULL) +#define vfio_iommufd_physical_detach_ioas \ + ((void (*)(struct vfio_device *vdev)) NULL) #define vfio_iommufd_emulated_bind \ ((int (*)(struct vfio_device *vdev, struct iommufd_ctx *ictx, \ u32 *out_device_id)) NULL)