All of lore.kernel.org
 help / color / mirror / Atom feed
* [[iommufd] PATCH v3 0/2] Make mdev driver dma_unmap callback tolerant to unmaps come before device open
@ 2022-12-02 13:54 Yi Liu
  2022-12-02 13:54 ` [[iommufd] PATCH v3 1/2] i915/gvt: Move gvt mapping cache initialization to intel_vgpu_init_dev() Yi Liu
                   ` (2 more replies)
  0 siblings, 3 replies; 5+ messages in thread
From: Yi Liu @ 2022-12-02 13:54 UTC (permalink / raw)
  To: jgg
  Cc: alex.williamson, kevin.tian, kvm, mjrosato, chao.p.peng,
	yi.l.liu, yi.y.sun, intel-gvt-dev, linux-s390, Tony Krowiak,
	Halil Pasic, Jason Herne, Zhenyu Wang, Zhi Wang

Jason's "Connect VFIO to IOMMUFD" introduces vfio iommufd compat mode. Under
this mode, vfio_iommufd_bind() creates an access which has an unmap callback,
which can be called immediately. This means mdev drivers may receive unmap
requests before the mdev is opened. For now, there are only three drivers
(gvt, vfio-ap and vfio-ccw) providing dma_unmap(). vfio-ccw is fine with
such requests. While gvt-g and vfio-ap may have potential problem with such
requests due to internal implementation. This series tries to enhance the two
drivers.

This series is based on Jason's below branch.

https://github.com/jgunthorpe/linux/tree/iommufd

(commit: 2079f24ce168f580a30e8eea94e660461d7d0d7a)

Change:
v3:
 - Add explicit check on vgpu->nr_cache_entries, explicitly reset gvt cache (Zhenyu)
 - Add Tony Krowiak's r-b for patch 0002

v2: https://lore.kernel.org/kvm/20221129105831.466954-1-yi.l.liu@intel.com/
 - Refine the cover letter and commit message of patch 0001 (Kevin)
 - Rename patch 0001 to better fit the commit message
 - Add r-b from Zhi for patch 0001
 - Tweak iova range test to assume page-aligned for patch 0002 (Jason)
 - Remove break so all queues within range are removed for patch 0002 (Kevin)

v1: https://lore.kernel.org/kvm/20221123134832.429589-1-yi.l.liu@intel.com/

Cc: Tony Krowiak <akrowiak@linux.ibm.com>
Cc: Halil Pasic <pasic@linux.ibm.com>
Cc: Jason Herne <jjherne@linux.ibm.com>
Cc: linux-s390@vger.kernel.org
Cc: Zhenyu Wang <zhenyuw@linux.intel.com>
Cc: Zhi Wang <zhi.a.wang@intel.com>
Cc: Kevin Tian <kevin.tian@intel.com>
Cc: intel-gvt-dev@lists.freedesktop.org

Regards,
	Yi Liu

Matthew Rosato (1):
  iommufd PATCH v2 2/2] vfio/ap: validate iova during dma_unmap and
    trigger irq disable

Yi Liu (1):
  i915/gvt: Move gvt mapping cache initialization to
    intel_vgpu_init_dev()

 drivers/gpu/drm/i915/gvt/kvmgt.c  | 18 ++++++++++++++----
 drivers/s390/crypto/vfio_ap_ops.c | 18 +++++++++++++++++-
 2 files changed, 31 insertions(+), 5 deletions(-)

-- 
2.34.1


^ permalink raw reply	[flat|nested] 5+ messages in thread

* [[iommufd] PATCH v3 1/2] i915/gvt: Move gvt mapping cache initialization to intel_vgpu_init_dev()
  2022-12-02 13:54 [[iommufd] PATCH v3 0/2] Make mdev driver dma_unmap callback tolerant to unmaps come before device open Yi Liu
@ 2022-12-02 13:54 ` Yi Liu
  2022-12-05  2:07   ` Zhenyu Wang
  2022-12-02 13:54 ` [[iommufd] PATCH v3 2/2] iommufd PATCH v2 2/2] vfio/ap: validate iova during dma_unmap and trigger irq disable Yi Liu
  2022-12-02 16:33 ` [[iommufd] PATCH v3 0/2] Make mdev driver dma_unmap callback tolerant to unmaps come before device open Jason Gunthorpe
  2 siblings, 1 reply; 5+ messages in thread
From: Yi Liu @ 2022-12-02 13:54 UTC (permalink / raw)
  To: jgg
  Cc: alex.williamson, kevin.tian, kvm, mjrosato, chao.p.peng,
	yi.l.liu, yi.y.sun, intel-gvt-dev, linux-s390, Zhenyu Wang,
	Zhi Wang

vfio container registers .dma_unmap() callback after the device is opened.
So it's fine for mdev drivers to initialize internal mapping cache in
.open_device(). See vfio_device_container_register().

Now with iommufd an access ops with an unmap callback is registered
when the device is bound to iommufd which is before .open_device()
is called. This implies gvt's .dma_unmap() could be called before its
internal mapping cache is initialized.

The fix is moving gvt mapping cache initialization to vGPU init. While
at it also move ptable initialization together.

Cc: Zhenyu Wang <zhenyuw@linux.intel.com>
Cc: Zhi Wang <zhi.a.wang@intel.com>
Cc: Kevin Tian <kevin.tian@intel.com>
Cc: intel-gvt-dev@lists.freedesktop.org
Reviewed-by: Zhi Wang <zhi.a.wang@intel.com>
Signed-off-by: Yi Liu <yi.l.liu@intel.com>
---
 drivers/gpu/drm/i915/gvt/kvmgt.c | 18 ++++++++++++++----
 1 file changed, 14 insertions(+), 4 deletions(-)

diff --git a/drivers/gpu/drm/i915/gvt/kvmgt.c b/drivers/gpu/drm/i915/gvt/kvmgt.c
index 7a45e5360caf..aaf0d9e8da95 100644
--- a/drivers/gpu/drm/i915/gvt/kvmgt.c
+++ b/drivers/gpu/drm/i915/gvt/kvmgt.c
@@ -671,9 +671,6 @@ static int intel_vgpu_open_device(struct vfio_device *vfio_dev)
 
 	vgpu->attached = true;
 
-	kvmgt_protect_table_init(vgpu);
-	gvt_cache_init(vgpu);
-
 	vgpu->track_node.track_write = kvmgt_page_track_write;
 	vgpu->track_node.track_flush_slot = kvmgt_page_track_flush_slot;
 	kvm_page_track_register_notifier(vgpu->vfio_device.kvm,
@@ -718,6 +715,11 @@ static void intel_vgpu_close_device(struct vfio_device *vfio_dev)
 	kvmgt_protect_table_destroy(vgpu);
 	gvt_cache_destroy(vgpu);
 
+	WARN_ON(vgpu->nr_cache_entries);
+
+	vgpu->gfn_cache = RB_ROOT;
+	vgpu->dma_addr_cache = RB_ROOT;
+
 	intel_vgpu_release_msi_eventfd_ctx(vgpu);
 
 	vgpu->attached = false;
@@ -1451,9 +1453,17 @@ static int intel_vgpu_init_dev(struct vfio_device *vfio_dev)
 	struct intel_vgpu *vgpu = vfio_dev_to_vgpu(vfio_dev);
 	struct intel_vgpu_type *type =
 		container_of(mdev->type, struct intel_vgpu_type, type);
+	int ret;
 
 	vgpu->gvt = kdev_to_i915(mdev->type->parent->dev)->gvt;
-	return intel_gvt_create_vgpu(vgpu, type->conf);
+	ret = intel_gvt_create_vgpu(vgpu, type->conf);
+	if (ret)
+		return ret;
+
+	kvmgt_protect_table_init(vgpu);
+	gvt_cache_init(vgpu);
+
+	return 0;
 }
 
 static void intel_vgpu_release_dev(struct vfio_device *vfio_dev)
-- 
2.34.1


^ permalink raw reply related	[flat|nested] 5+ messages in thread

* [[iommufd] PATCH v3 2/2] iommufd PATCH v2 2/2] vfio/ap: validate iova during dma_unmap and trigger irq disable
  2022-12-02 13:54 [[iommufd] PATCH v3 0/2] Make mdev driver dma_unmap callback tolerant to unmaps come before device open Yi Liu
  2022-12-02 13:54 ` [[iommufd] PATCH v3 1/2] i915/gvt: Move gvt mapping cache initialization to intel_vgpu_init_dev() Yi Liu
@ 2022-12-02 13:54 ` Yi Liu
  2022-12-02 16:33 ` [[iommufd] PATCH v3 0/2] Make mdev driver dma_unmap callback tolerant to unmaps come before device open Jason Gunthorpe
  2 siblings, 0 replies; 5+ messages in thread
From: Yi Liu @ 2022-12-02 13:54 UTC (permalink / raw)
  To: jgg
  Cc: alex.williamson, kevin.tian, kvm, mjrosato, chao.p.peng,
	yi.l.liu, yi.y.sun, intel-gvt-dev, linux-s390, Tony Krowiak,
	Halil Pasic, Jason Herne

From: Matthew Rosato <mjrosato@linux.ibm.com>

Currently, each mapped iova is stashed in its associated vfio_ap_queue;
when we get an unmap request, validate that it matches with one or more
of these stashed values before attempting unpins.

Each stashed iova represents IRQ that was enabled for a queue.  Therefore,
if a match is found, trigger IRQ disable for this queue to ensure that
underlying firmware will no longer try to use the associated pfn after
the page is unpinned. IRQ disable will also handle the associated unpin.

Cc: Tony Krowiak <akrowiak@linux.ibm.com>
Cc: Halil Pasic <pasic@linux.ibm.com>
Cc: Jason Herne <jjherne@linux.ibm.com>
Cc: linux-s390@vger.kernel.org
Reviewed-by: Tony Krowiak <akrowiak@linux.ibm.com>
Signed-off-by: Matthew Rosato <mjrosato@linux.ibm.com>
Signed-off-by: Yi Liu <yi.l.liu@intel.com>
---
 drivers/s390/crypto/vfio_ap_ops.c | 18 +++++++++++++++++-
 1 file changed, 17 insertions(+), 1 deletion(-)

diff --git a/drivers/s390/crypto/vfio_ap_ops.c b/drivers/s390/crypto/vfio_ap_ops.c
index 0b4cc8c597ae..8bf353d46820 100644
--- a/drivers/s390/crypto/vfio_ap_ops.c
+++ b/drivers/s390/crypto/vfio_ap_ops.c
@@ -1535,13 +1535,29 @@ static int vfio_ap_mdev_set_kvm(struct ap_matrix_mdev *matrix_mdev,
 	return 0;
 }
 
+static void unmap_iova(struct ap_matrix_mdev *matrix_mdev, u64 iova, u64 length)
+{
+	struct ap_queue_table *qtable = &matrix_mdev->qtable;
+	struct vfio_ap_queue *q;
+	int loop_cursor;
+
+	hash_for_each(qtable->queues, loop_cursor, q, mdev_qnode) {
+		if (q->saved_iova >= iova && q->saved_iova < iova + length)
+			vfio_ap_irq_disable(q);
+	}
+}
+
 static void vfio_ap_mdev_dma_unmap(struct vfio_device *vdev, u64 iova,
 				   u64 length)
 {
 	struct ap_matrix_mdev *matrix_mdev =
 		container_of(vdev, struct ap_matrix_mdev, vdev);
 
-	vfio_unpin_pages(&matrix_mdev->vdev, iova, 1);
+	mutex_lock(&matrix_dev->mdevs_lock);
+
+	unmap_iova(matrix_mdev, iova, length);
+
+	mutex_unlock(&matrix_dev->mdevs_lock);
 }
 
 /**
-- 
2.34.1


^ permalink raw reply related	[flat|nested] 5+ messages in thread

* Re: [[iommufd] PATCH v3 0/2] Make mdev driver dma_unmap callback tolerant to unmaps come before device open
  2022-12-02 13:54 [[iommufd] PATCH v3 0/2] Make mdev driver dma_unmap callback tolerant to unmaps come before device open Yi Liu
  2022-12-02 13:54 ` [[iommufd] PATCH v3 1/2] i915/gvt: Move gvt mapping cache initialization to intel_vgpu_init_dev() Yi Liu
  2022-12-02 13:54 ` [[iommufd] PATCH v3 2/2] iommufd PATCH v2 2/2] vfio/ap: validate iova during dma_unmap and trigger irq disable Yi Liu
@ 2022-12-02 16:33 ` Jason Gunthorpe
  2 siblings, 0 replies; 5+ messages in thread
From: Jason Gunthorpe @ 2022-12-02 16:33 UTC (permalink / raw)
  To: Yi Liu
  Cc: alex.williamson, kevin.tian, kvm, mjrosato, chao.p.peng,
	yi.y.sun, intel-gvt-dev, linux-s390, Tony Krowiak, Halil Pasic,
	Jason Herne, Zhenyu Wang, Zhi Wang

On Fri, Dec 02, 2022 at 05:54:00AM -0800, Yi Liu wrote:

> Matthew Rosato (1):
>   iommufd PATCH v2 2/2] vfio/ap: validate iova during dma_unmap and
>     trigger irq disable
> 
> Yi Liu (1):
>   i915/gvt: Move gvt mapping cache initialization to
>     intel_vgpu_init_dev()

I refreshed the patches with this version

Jason

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [[iommufd] PATCH v3 1/2] i915/gvt: Move gvt mapping cache initialization to intel_vgpu_init_dev()
  2022-12-02 13:54 ` [[iommufd] PATCH v3 1/2] i915/gvt: Move gvt mapping cache initialization to intel_vgpu_init_dev() Yi Liu
@ 2022-12-05  2:07   ` Zhenyu Wang
  0 siblings, 0 replies; 5+ messages in thread
From: Zhenyu Wang @ 2022-12-05  2:07 UTC (permalink / raw)
  To: Yi Liu
  Cc: jgg, alex.williamson, kevin.tian, kvm, mjrosato, chao.p.peng,
	yi.y.sun, intel-gvt-dev, linux-s390, Zhenyu Wang, Zhi Wang

[-- Attachment #1: Type: text/plain, Size: 2694 bytes --]

On 2022.12.02 05:54:01 -0800, Yi Liu wrote:
> vfio container registers .dma_unmap() callback after the device is opened.
> So it's fine for mdev drivers to initialize internal mapping cache in
> .open_device(). See vfio_device_container_register().
> 
> Now with iommufd an access ops with an unmap callback is registered
> when the device is bound to iommufd which is before .open_device()
> is called. This implies gvt's .dma_unmap() could be called before its
> internal mapping cache is initialized.
> 
> The fix is moving gvt mapping cache initialization to vGPU init. While
> at it also move ptable initialization together.
> 
> Cc: Zhenyu Wang <zhenyuw@linux.intel.com>
> Cc: Zhi Wang <zhi.a.wang@intel.com>
> Cc: Kevin Tian <kevin.tian@intel.com>
> Cc: intel-gvt-dev@lists.freedesktop.org
> Reviewed-by: Zhi Wang <zhi.a.wang@intel.com>
> Signed-off-by: Yi Liu <yi.l.liu@intel.com>
> ---
>  drivers/gpu/drm/i915/gvt/kvmgt.c | 18 ++++++++++++++----
>  1 file changed, 14 insertions(+), 4 deletions(-)
> 
> diff --git a/drivers/gpu/drm/i915/gvt/kvmgt.c b/drivers/gpu/drm/i915/gvt/kvmgt.c
> index 7a45e5360caf..aaf0d9e8da95 100644
> --- a/drivers/gpu/drm/i915/gvt/kvmgt.c
> +++ b/drivers/gpu/drm/i915/gvt/kvmgt.c
> @@ -671,9 +671,6 @@ static int intel_vgpu_open_device(struct vfio_device *vfio_dev)
>  
>  	vgpu->attached = true;
>  
> -	kvmgt_protect_table_init(vgpu);
> -	gvt_cache_init(vgpu);
> -
>  	vgpu->track_node.track_write = kvmgt_page_track_write;
>  	vgpu->track_node.track_flush_slot = kvmgt_page_track_flush_slot;
>  	kvm_page_track_register_notifier(vgpu->vfio_device.kvm,
> @@ -718,6 +715,11 @@ static void intel_vgpu_close_device(struct vfio_device *vfio_dev)
>  	kvmgt_protect_table_destroy(vgpu);
>  	gvt_cache_destroy(vgpu);
>  
> +	WARN_ON(vgpu->nr_cache_entries);
> +
> +	vgpu->gfn_cache = RB_ROOT;
> +	vgpu->dma_addr_cache = RB_ROOT;
> +
>  	intel_vgpu_release_msi_eventfd_ctx(vgpu);
>  
>  	vgpu->attached = false;
> @@ -1451,9 +1453,17 @@ static int intel_vgpu_init_dev(struct vfio_device *vfio_dev)
>  	struct intel_vgpu *vgpu = vfio_dev_to_vgpu(vfio_dev);
>  	struct intel_vgpu_type *type =
>  		container_of(mdev->type, struct intel_vgpu_type, type);
> +	int ret;
>  
>  	vgpu->gvt = kdev_to_i915(mdev->type->parent->dev)->gvt;
> -	return intel_gvt_create_vgpu(vgpu, type->conf);
> +	ret = intel_gvt_create_vgpu(vgpu, type->conf);
> +	if (ret)
> +		return ret;
> +
> +	kvmgt_protect_table_init(vgpu);
> +	gvt_cache_init(vgpu);
> +
> +	return 0;
>  }
>  
>  static void intel_vgpu_release_dev(struct vfio_device *vfio_dev)
> -- 

Reviewed-by: Zhenyu Wang <zhenyuw@linux.intel.com>

thanks!

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 195 bytes --]

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2022-12-05  2:08 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-12-02 13:54 [[iommufd] PATCH v3 0/2] Make mdev driver dma_unmap callback tolerant to unmaps come before device open Yi Liu
2022-12-02 13:54 ` [[iommufd] PATCH v3 1/2] i915/gvt: Move gvt mapping cache initialization to intel_vgpu_init_dev() Yi Liu
2022-12-05  2:07   ` Zhenyu Wang
2022-12-02 13:54 ` [[iommufd] PATCH v3 2/2] iommufd PATCH v2 2/2] vfio/ap: validate iova during dma_unmap and trigger irq disable Yi Liu
2022-12-02 16:33 ` [[iommufd] PATCH v3 0/2] Make mdev driver dma_unmap callback tolerant to unmaps come before device open Jason Gunthorpe

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.