diff mbox series

[v6,19/24] vfio-iommufd: Add detach_ioas support for emulated VFIO devices

Message ID 20230308132903.465159-20-yi.l.liu@intel.com (mailing list archive)
State New, archived
Headers show
Series cover-letter: Add vfio_device cdev for iommufd support | expand

Commit Message

Yi Liu March 8, 2023, 1:28 p.m. UTC
this prepares for adding DETACH ioctl for emulated VFIO devices.

Signed-off-by: Yi Liu <yi.l.liu@intel.com>
Reviewed-by: Kevin Tian <kevin.tian@intel.com>
Tested-by: Terrence Xu <terrence.xu@intel.com>
Tested-by: Nicolin Chen <nicolinc@nvidia.com>
Tested-by: Matthew Rosato <mjrosato@linux.ibm.com>
---
 drivers/gpu/drm/i915/gvt/kvmgt.c  |  1 +
 drivers/s390/cio/vfio_ccw_ops.c   |  1 +
 drivers/s390/crypto/vfio_ap_ops.c |  1 +
 drivers/vfio/iommufd.c            | 14 +++++++++++++-
 include/linux/vfio.h              |  3 +++
 5 files changed, 19 insertions(+), 1 deletion(-)

Comments

Nicolin Chen March 10, 2023, 11:42 p.m. UTC | #1
On Wed, Mar 08, 2023 at 05:28:58AM -0800, Yi Liu wrote:
> External email: Use caution opening links or attachments
> 
> 
> this prepares for adding DETACH ioctl for emulated VFIO devices.
> 
> Signed-off-by: Yi Liu <yi.l.liu@intel.com>
> Reviewed-by: Kevin Tian <kevin.tian@intel.com>
> Tested-by: Terrence Xu <terrence.xu@intel.com>
> Tested-by: Nicolin Chen <nicolinc@nvidia.com>
> Tested-by: Matthew Rosato <mjrosato@linux.ibm.com>
> ---
>  drivers/gpu/drm/i915/gvt/kvmgt.c  |  1 +
>  drivers/s390/cio/vfio_ccw_ops.c   |  1 +
>  drivers/s390/crypto/vfio_ap_ops.c |  1 +
>  drivers/vfio/iommufd.c            | 14 +++++++++++++-
>  include/linux/vfio.h              |  3 +++
>  5 files changed, 19 insertions(+), 1 deletion(-)
> 
> diff --git a/drivers/gpu/drm/i915/gvt/kvmgt.c b/drivers/gpu/drm/i915/gvt/kvmgt.c
> index de675d799c7d..9cd9e9da60dd 100644
> --- a/drivers/gpu/drm/i915/gvt/kvmgt.c
> +++ b/drivers/gpu/drm/i915/gvt/kvmgt.c
> @@ -1474,6 +1474,7 @@ static const struct vfio_device_ops intel_vgpu_dev_ops = {
>         .bind_iommufd   = vfio_iommufd_emulated_bind,
>         .unbind_iommufd = vfio_iommufd_emulated_unbind,
>         .attach_ioas    = vfio_iommufd_emulated_attach_ioas,
> +       .detach_ioas    = vfio_iommufd_emulated_detach_ioas,
>  };
> 
>  static int intel_vgpu_probe(struct mdev_device *mdev)
> diff --git a/drivers/s390/cio/vfio_ccw_ops.c b/drivers/s390/cio/vfio_ccw_ops.c
> index 5b53b94f13c7..cba4971618ff 100644
> --- a/drivers/s390/cio/vfio_ccw_ops.c
> +++ b/drivers/s390/cio/vfio_ccw_ops.c
> @@ -632,6 +632,7 @@ static const struct vfio_device_ops vfio_ccw_dev_ops = {
>         .bind_iommufd = vfio_iommufd_emulated_bind,
>         .unbind_iommufd = vfio_iommufd_emulated_unbind,
>         .attach_ioas = vfio_iommufd_emulated_attach_ioas,
> +       .detach_ioas = vfio_iommufd_emulated_detach_ioas,
>  };
> 
>  struct mdev_driver vfio_ccw_mdev_driver = {
> diff --git a/drivers/s390/crypto/vfio_ap_ops.c b/drivers/s390/crypto/vfio_ap_ops.c
> index 72e10abb103a..9902e62e7a17 100644
> --- a/drivers/s390/crypto/vfio_ap_ops.c
> +++ b/drivers/s390/crypto/vfio_ap_ops.c
> @@ -1844,6 +1844,7 @@ static const struct vfio_device_ops vfio_ap_matrix_dev_ops = {
>         .bind_iommufd = vfio_iommufd_emulated_bind,
>         .unbind_iommufd = vfio_iommufd_emulated_unbind,
>         .attach_ioas = vfio_iommufd_emulated_attach_ioas,
> +       .detach_ioas = vfio_iommufd_emulated_detach_ioas,
>  };
> 
>  static struct mdev_driver vfio_ap_matrix_driver = {
> diff --git a/drivers/vfio/iommufd.c b/drivers/vfio/iommufd.c
> index c06494e322f9..8a9457d0a33c 100644
> --- a/drivers/vfio/iommufd.c
> +++ b/drivers/vfio/iommufd.c
> @@ -218,8 +218,20 @@ int vfio_iommufd_emulated_attach_ioas(struct vfio_device *vdev, u32 *pt_id)
>  {
>         lockdep_assert_held(&vdev->dev_set->lock);
> 
> -       if (!vdev->iommufd_access)
> +       if (WARN_ON(!vdev->iommufd_access))
>                 return -ENOENT;
>         return iommufd_access_set_ioas(vdev->iommufd_access, *pt_id);
>  }
>  EXPORT_SYMBOL_GPL(vfio_iommufd_emulated_attach_ioas);
> +
> +void vfio_iommufd_emulated_detach_ioas(struct vfio_device *vdev)
> +{
> +       lockdep_assert_held(&vdev->dev_set->lock);
> +
> +       if (WARN_ON(!vdev->iommufd_access))
> +               return;
> +
[...]
> +       iommufd_access_destroy(vdev->iommufd_access);
> +       vdev->iommufd_access = NULL;

After moving access allocation/destroy to bind/unbind, here it
should be:
	iommufd_access_set_ioas(vdev->iommufd_access, 0);

Thanks
Nic
Yi Liu March 15, 2023, 6:15 a.m. UTC | #2
> From: Nicolin Chen <nicolinc@nvidia.com>
> Sent: Saturday, March 11, 2023 7:43 AM
> On Wed, Mar 08, 2023 at 05:28:58AM -0800, Yi Liu wrote:
> > External email: Use caution opening links or attachments
> >
> >
> > this prepares for adding DETACH ioctl for emulated VFIO devices.
> >
> > Signed-off-by: Yi Liu <yi.l.liu@intel.com>
> > Reviewed-by: Kevin Tian <kevin.tian@intel.com>
> > Tested-by: Terrence Xu <terrence.xu@intel.com>
> > Tested-by: Nicolin Chen <nicolinc@nvidia.com>
> > Tested-by: Matthew Rosato <mjrosato@linux.ibm.com>
> > ---
> >  drivers/gpu/drm/i915/gvt/kvmgt.c  |  1 +
> >  drivers/s390/cio/vfio_ccw_ops.c   |  1 +
> >  drivers/s390/crypto/vfio_ap_ops.c |  1 +
> >  drivers/vfio/iommufd.c            | 14 +++++++++++++-
> >  include/linux/vfio.h              |  3 +++
> >  5 files changed, 19 insertions(+), 1 deletion(-)
> >
> > diff --git a/drivers/gpu/drm/i915/gvt/kvmgt.c
> b/drivers/gpu/drm/i915/gvt/kvmgt.c
> > index de675d799c7d..9cd9e9da60dd 100644
> > --- a/drivers/gpu/drm/i915/gvt/kvmgt.c
> > +++ b/drivers/gpu/drm/i915/gvt/kvmgt.c
> > @@ -1474,6 +1474,7 @@ static const struct vfio_device_ops
> intel_vgpu_dev_ops = {
> >         .bind_iommufd   = vfio_iommufd_emulated_bind,
> >         .unbind_iommufd = vfio_iommufd_emulated_unbind,
> >         .attach_ioas    = vfio_iommufd_emulated_attach_ioas,
> > +       .detach_ioas    = vfio_iommufd_emulated_detach_ioas,
> >  };
> >
> >  static int intel_vgpu_probe(struct mdev_device *mdev)
> > diff --git a/drivers/s390/cio/vfio_ccw_ops.c
> b/drivers/s390/cio/vfio_ccw_ops.c
> > index 5b53b94f13c7..cba4971618ff 100644
> > --- a/drivers/s390/cio/vfio_ccw_ops.c
> > +++ b/drivers/s390/cio/vfio_ccw_ops.c
> > @@ -632,6 +632,7 @@ static const struct vfio_device_ops
> vfio_ccw_dev_ops = {
> >         .bind_iommufd = vfio_iommufd_emulated_bind,
> >         .unbind_iommufd = vfio_iommufd_emulated_unbind,
> >         .attach_ioas = vfio_iommufd_emulated_attach_ioas,
> > +       .detach_ioas = vfio_iommufd_emulated_detach_ioas,
> >  };
> >
> >  struct mdev_driver vfio_ccw_mdev_driver = {
> > diff --git a/drivers/s390/crypto/vfio_ap_ops.c
> b/drivers/s390/crypto/vfio_ap_ops.c
> > index 72e10abb103a..9902e62e7a17 100644
> > --- a/drivers/s390/crypto/vfio_ap_ops.c
> > +++ b/drivers/s390/crypto/vfio_ap_ops.c
> > @@ -1844,6 +1844,7 @@ static const struct vfio_device_ops
> vfio_ap_matrix_dev_ops = {
> >         .bind_iommufd = vfio_iommufd_emulated_bind,
> >         .unbind_iommufd = vfio_iommufd_emulated_unbind,
> >         .attach_ioas = vfio_iommufd_emulated_attach_ioas,
> > +       .detach_ioas = vfio_iommufd_emulated_detach_ioas,
> >  };
> >
> >  static struct mdev_driver vfio_ap_matrix_driver = {
> > diff --git a/drivers/vfio/iommufd.c b/drivers/vfio/iommufd.c
> > index c06494e322f9..8a9457d0a33c 100644
> > --- a/drivers/vfio/iommufd.c
> > +++ b/drivers/vfio/iommufd.c
> > @@ -218,8 +218,20 @@ int vfio_iommufd_emulated_attach_ioas(struct
> vfio_device *vdev, u32 *pt_id)
> >  {
> >         lockdep_assert_held(&vdev->dev_set->lock);
> >
> > -       if (!vdev->iommufd_access)
> > +       if (WARN_ON(!vdev->iommufd_access))
> >                 return -ENOENT;
> >         return iommufd_access_set_ioas(vdev->iommufd_access, *pt_id);
> >  }
> >  EXPORT_SYMBOL_GPL(vfio_iommufd_emulated_attach_ioas);
> > +
> > +void vfio_iommufd_emulated_detach_ioas(struct vfio_device *vdev)
> > +{
> > +       lockdep_assert_held(&vdev->dev_set->lock);
> > +
> > +       if (WARN_ON(!vdev->iommufd_access))
> > +               return;
> > +
> [...]
> > +       iommufd_access_destroy(vdev->iommufd_access);
> > +       vdev->iommufd_access = NULL;
> 
> After moving access allocation/destroy to bind/unbind, here it
> should be:
> 	iommufd_access_set_ioas(vdev->iommufd_access, 0);

You are right.

Regards,
Yi Liu
Nicolin Chen March 15, 2023, 6:25 a.m. UTC | #3
On Wed, Mar 15, 2023 at 06:15:02AM +0000, Liu, Yi L wrote:

> > > +void vfio_iommufd_emulated_detach_ioas(struct vfio_device *vdev)
> > > +{
> > > +       lockdep_assert_held(&vdev->dev_set->lock);
> > > +
> > > +       if (WARN_ON(!vdev->iommufd_access))
> > > +               return;
> > > +
> > [...]
> > > +       iommufd_access_destroy(vdev->iommufd_access);
> > > +       vdev->iommufd_access = NULL;
> >
> > After moving access allocation/destroy to bind/unbind, here it
> > should be:
> >       iommufd_access_set_ioas(vdev->iommufd_access, 0);
> 
> You are right.

Yet...iommufd_access_set_ioas is getting reworked with my patch:
In another thread, Jason suggested to have iommufd_acces_detach
API, and I am trying to finalize it with Jason/Kevin.
https://lore.kernel.org/kvm/BN9PR11MB5276738DC59AC1B4A66AB3C38CBF9@BN9PR11MB5276.namprd11.prod.outlook.com/

Nic
diff mbox series

Patch

diff --git a/drivers/gpu/drm/i915/gvt/kvmgt.c b/drivers/gpu/drm/i915/gvt/kvmgt.c
index de675d799c7d..9cd9e9da60dd 100644
--- a/drivers/gpu/drm/i915/gvt/kvmgt.c
+++ b/drivers/gpu/drm/i915/gvt/kvmgt.c
@@ -1474,6 +1474,7 @@  static const struct vfio_device_ops intel_vgpu_dev_ops = {
 	.bind_iommufd	= vfio_iommufd_emulated_bind,
 	.unbind_iommufd = vfio_iommufd_emulated_unbind,
 	.attach_ioas	= vfio_iommufd_emulated_attach_ioas,
+	.detach_ioas	= vfio_iommufd_emulated_detach_ioas,
 };
 
 static int intel_vgpu_probe(struct mdev_device *mdev)
diff --git a/drivers/s390/cio/vfio_ccw_ops.c b/drivers/s390/cio/vfio_ccw_ops.c
index 5b53b94f13c7..cba4971618ff 100644
--- a/drivers/s390/cio/vfio_ccw_ops.c
+++ b/drivers/s390/cio/vfio_ccw_ops.c
@@ -632,6 +632,7 @@  static const struct vfio_device_ops vfio_ccw_dev_ops = {
 	.bind_iommufd = vfio_iommufd_emulated_bind,
 	.unbind_iommufd = vfio_iommufd_emulated_unbind,
 	.attach_ioas = vfio_iommufd_emulated_attach_ioas,
+	.detach_ioas = vfio_iommufd_emulated_detach_ioas,
 };
 
 struct mdev_driver vfio_ccw_mdev_driver = {
diff --git a/drivers/s390/crypto/vfio_ap_ops.c b/drivers/s390/crypto/vfio_ap_ops.c
index 72e10abb103a..9902e62e7a17 100644
--- a/drivers/s390/crypto/vfio_ap_ops.c
+++ b/drivers/s390/crypto/vfio_ap_ops.c
@@ -1844,6 +1844,7 @@  static const struct vfio_device_ops vfio_ap_matrix_dev_ops = {
 	.bind_iommufd = vfio_iommufd_emulated_bind,
 	.unbind_iommufd = vfio_iommufd_emulated_unbind,
 	.attach_ioas = vfio_iommufd_emulated_attach_ioas,
+	.detach_ioas = vfio_iommufd_emulated_detach_ioas,
 };
 
 static struct mdev_driver vfio_ap_matrix_driver = {
diff --git a/drivers/vfio/iommufd.c b/drivers/vfio/iommufd.c
index c06494e322f9..8a9457d0a33c 100644
--- a/drivers/vfio/iommufd.c
+++ b/drivers/vfio/iommufd.c
@@ -218,8 +218,20 @@  int vfio_iommufd_emulated_attach_ioas(struct vfio_device *vdev, u32 *pt_id)
 {
 	lockdep_assert_held(&vdev->dev_set->lock);
 
-	if (!vdev->iommufd_access)
+	if (WARN_ON(!vdev->iommufd_access))
 		return -ENOENT;
 	return iommufd_access_set_ioas(vdev->iommufd_access, *pt_id);
 }
 EXPORT_SYMBOL_GPL(vfio_iommufd_emulated_attach_ioas);
+
+void vfio_iommufd_emulated_detach_ioas(struct vfio_device *vdev)
+{
+	lockdep_assert_held(&vdev->dev_set->lock);
+
+	if (WARN_ON(!vdev->iommufd_access))
+		return;
+
+	iommufd_access_destroy(vdev->iommufd_access);
+	vdev->iommufd_access = NULL;
+}
+EXPORT_SYMBOL_GPL(vfio_iommufd_emulated_detach_ioas);
diff --git a/include/linux/vfio.h b/include/linux/vfio.h
index 8982e48a30e2..5a1c5b6f1a63 100644
--- a/include/linux/vfio.h
+++ b/include/linux/vfio.h
@@ -126,6 +126,7 @@  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);
 int vfio_iommufd_emulated_attach_ioas(struct vfio_device *vdev, u32 *pt_id);
+void vfio_iommufd_emulated_detach_ioas(struct vfio_device *vdev);
 #else
 #define vfio_iommufd_physical_bind                                      \
 	((int (*)(struct vfio_device *vdev, struct iommufd_ctx *ictx,   \
@@ -145,6 +146,8 @@  int vfio_iommufd_emulated_attach_ioas(struct vfio_device *vdev, u32 *pt_id);
 	((void (*)(struct vfio_device *vdev)) NULL)
 #define vfio_iommufd_emulated_attach_ioas \
 	((int (*)(struct vfio_device *vdev, u32 *pt_id)) NULL)
+#define vfio_iommufd_emulated_detach_ioas \
+	((void (*)(struct vfio_device *vdev)) NULL)
 #endif
 
 /**