kvm.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] vfio: Do not manipulate iommu dma_owner for fake iommu groups
@ 2022-05-19 17:03 Jason Gunthorpe
  2022-05-20  1:33 ` Tian, Kevin
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: Jason Gunthorpe @ 2022-05-19 17:03 UTC (permalink / raw)
  To: Alex Williamson, Cornelia Huck, iommu, kvm, Robin Murphy
  Cc: Eric Farman, Joerg Roedel, Kevin Tian

Since asserting dma ownership now causes the group to have its DMA blocked
the iommu layer requires a working iommu. This means the dma_owner APIs
cannot be used on the fake groups that VFIO creates. Test for this and
avoid calling them.

Otherwise asserting dma ownership will fail for VFIO mdev devices as a
BLOCKING iommu_domain cannot be allocated due to the NULL iommu ops.

Fixes: 0286300e6045 ("iommu: iommu_group_claim_dma_owner() must always assign a domain")
Reported-by: Eric Farman <farman@linux.ibm.com>
Tested-by: Eric Farman <farman@linux.ibm.com>
Signed-off-by: Jason Gunthorpe <jgg@nvidia.com>
---
 drivers/vfio/vfio.c | 15 ++++++++++-----
 1 file changed, 10 insertions(+), 5 deletions(-)

Sort of a v2 from here:

https://lore.kernel.org/all/20220518191446.GU1343366@nvidia.com/

diff --git a/drivers/vfio/vfio.c b/drivers/vfio/vfio.c
index cfcff7764403fc..f5ed03897210c3 100644
--- a/drivers/vfio/vfio.c
+++ b/drivers/vfio/vfio.c
@@ -927,7 +927,8 @@ static void __vfio_group_unset_container(struct vfio_group *group)
 		driver->ops->detach_group(container->iommu_data,
 					  group->iommu_group);
 
-	iommu_group_release_dma_owner(group->iommu_group);
+	if (group->type == VFIO_IOMMU)
+		iommu_group_release_dma_owner(group->iommu_group);
 
 	group->container = NULL;
 	group->container_users = 0;
@@ -1001,9 +1002,11 @@ static int vfio_group_set_container(struct vfio_group *group, int container_fd)
 		goto unlock_out;
 	}
 
-	ret = iommu_group_claim_dma_owner(group->iommu_group, f.file);
-	if (ret)
-		goto unlock_out;
+	if (group->type == VFIO_IOMMU) {
+		ret = iommu_group_claim_dma_owner(group->iommu_group, f.file);
+		if (ret)
+			goto unlock_out;
+	}
 
 	driver = container->iommu_driver;
 	if (driver) {
@@ -1011,7 +1014,9 @@ static int vfio_group_set_container(struct vfio_group *group, int container_fd)
 						group->iommu_group,
 						group->type);
 		if (ret) {
-			iommu_group_release_dma_owner(group->iommu_group);
+			if (group->type == VFIO_IOMMU)
+				iommu_group_release_dma_owner(
+					group->iommu_group);
 			goto unlock_out;
 		}
 	}

base-commit: 7ab5e10eda02da1d9562ffde562c51055d368e9c
-- 
2.36.0


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

* RE: [PATCH] vfio: Do not manipulate iommu dma_owner for fake iommu groups
  2022-05-19 17:03 [PATCH] vfio: Do not manipulate iommu dma_owner for fake iommu groups Jason Gunthorpe
@ 2022-05-20  1:33 ` Tian, Kevin
  2022-05-20  5:32 ` Christoph Hellwig
  2022-05-24 16:07 ` Alex Williamson
  2 siblings, 0 replies; 4+ messages in thread
From: Tian, Kevin @ 2022-05-20  1:33 UTC (permalink / raw)
  To: Jason Gunthorpe, Alex Williamson, Cornelia Huck, iommu, kvm,
	Robin Murphy
  Cc: Eric Farman, Rodel, Jorg

> From: Jason Gunthorpe <jgg@nvidia.com>
> Sent: Friday, May 20, 2022 1:04 AM
> 
> Since asserting dma ownership now causes the group to have its DMA
> blocked
> the iommu layer requires a working iommu. This means the dma_owner APIs
> cannot be used on the fake groups that VFIO creates. Test for this and
> avoid calling them.
> 
> Otherwise asserting dma ownership will fail for VFIO mdev devices as a
> BLOCKING iommu_domain cannot be allocated due to the NULL iommu ops.
> 
> Fixes: 0286300e6045 ("iommu: iommu_group_claim_dma_owner() must
> always assign a domain")
> Reported-by: Eric Farman <farman@linux.ibm.com>
> Tested-by: Eric Farman <farman@linux.ibm.com>
> Signed-off-by: Jason Gunthorpe <jgg@nvidia.com>

Reviewed-by: Kevin Tian <kevin.tian@intel.com>

> ---
>  drivers/vfio/vfio.c | 15 ++++++++++-----
>  1 file changed, 10 insertions(+), 5 deletions(-)
> 
> Sort of a v2 from here:
> 
> https://lore.kernel.org/all/20220518191446.GU1343366@nvidia.com/
> 
> diff --git a/drivers/vfio/vfio.c b/drivers/vfio/vfio.c
> index cfcff7764403fc..f5ed03897210c3 100644
> --- a/drivers/vfio/vfio.c
> +++ b/drivers/vfio/vfio.c
> @@ -927,7 +927,8 @@ static void __vfio_group_unset_container(struct
> vfio_group *group)
>  		driver->ops->detach_group(container->iommu_data,
>  					  group->iommu_group);
> 
> -	iommu_group_release_dma_owner(group->iommu_group);
> +	if (group->type == VFIO_IOMMU)
> +		iommu_group_release_dma_owner(group->iommu_group);
> 
>  	group->container = NULL;
>  	group->container_users = 0;
> @@ -1001,9 +1002,11 @@ static int vfio_group_set_container(struct
> vfio_group *group, int container_fd)
>  		goto unlock_out;
>  	}
> 
> -	ret = iommu_group_claim_dma_owner(group->iommu_group, f.file);
> -	if (ret)
> -		goto unlock_out;
> +	if (group->type == VFIO_IOMMU) {
> +		ret = iommu_group_claim_dma_owner(group-
> >iommu_group, f.file);
> +		if (ret)
> +			goto unlock_out;
> +	}
> 
>  	driver = container->iommu_driver;
>  	if (driver) {
> @@ -1011,7 +1014,9 @@ static int vfio_group_set_container(struct
> vfio_group *group, int container_fd)
>  						group->iommu_group,
>  						group->type);
>  		if (ret) {
> -			iommu_group_release_dma_owner(group-
> >iommu_group);
> +			if (group->type == VFIO_IOMMU)
> +				iommu_group_release_dma_owner(
> +					group->iommu_group);
>  			goto unlock_out;
>  		}
>  	}
> 
> base-commit: 7ab5e10eda02da1d9562ffde562c51055d368e9c
> --
> 2.36.0


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

* Re: [PATCH] vfio: Do not manipulate iommu dma_owner for fake iommu groups
  2022-05-19 17:03 [PATCH] vfio: Do not manipulate iommu dma_owner for fake iommu groups Jason Gunthorpe
  2022-05-20  1:33 ` Tian, Kevin
@ 2022-05-20  5:32 ` Christoph Hellwig
  2022-05-24 16:07 ` Alex Williamson
  2 siblings, 0 replies; 4+ messages in thread
From: Christoph Hellwig @ 2022-05-20  5:32 UTC (permalink / raw)
  To: Jason Gunthorpe
  Cc: Alex Williamson, Cornelia Huck, iommu, kvm, Robin Murphy,
	Eric Farman, Joerg Roedel, Kevin Tian

On Thu, May 19, 2022 at 02:03:48PM -0300, Jason Gunthorpe via iommu wrote:
> Since asserting dma ownership now causes the group to have its DMA blocked
> the iommu layer requires a working iommu. This means the dma_owner APIs
> cannot be used on the fake groups that VFIO creates. Test for this and
> avoid calling them.
> 
> Otherwise asserting dma ownership will fail for VFIO mdev devices as a
> BLOCKING iommu_domain cannot be allocated due to the NULL iommu ops.

Fake iommu groups come back to bite again, part 42..

The patch looks good:

Reviewed-by: Christoph Hellwig <hch@lst.de>

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

* Re: [PATCH] vfio: Do not manipulate iommu dma_owner for fake iommu groups
  2022-05-19 17:03 [PATCH] vfio: Do not manipulate iommu dma_owner for fake iommu groups Jason Gunthorpe
  2022-05-20  1:33 ` Tian, Kevin
  2022-05-20  5:32 ` Christoph Hellwig
@ 2022-05-24 16:07 ` Alex Williamson
  2 siblings, 0 replies; 4+ messages in thread
From: Alex Williamson @ 2022-05-24 16:07 UTC (permalink / raw)
  To: Jason Gunthorpe
  Cc: Cornelia Huck, iommu, kvm, Robin Murphy, Eric Farman,
	Joerg Roedel, Kevin Tian

On Thu, 19 May 2022 14:03:48 -0300
Jason Gunthorpe <jgg@nvidia.com> wrote:

> Since asserting dma ownership now causes the group to have its DMA blocked
> the iommu layer requires a working iommu. This means the dma_owner APIs
> cannot be used on the fake groups that VFIO creates. Test for this and
> avoid calling them.
> 
> Otherwise asserting dma ownership will fail for VFIO mdev devices as a
> BLOCKING iommu_domain cannot be allocated due to the NULL iommu ops.
> 
> Fixes: 0286300e6045 ("iommu: iommu_group_claim_dma_owner() must always assign a domain")
> Reported-by: Eric Farman <farman@linux.ibm.com>
> Tested-by: Eric Farman <farman@linux.ibm.com>
> Signed-off-by: Jason Gunthorpe <jgg@nvidia.com>
> ---
>  drivers/vfio/vfio.c | 15 ++++++++++-----
>  1 file changed, 10 insertions(+), 5 deletions(-)

Applied to vfio next branch for v5.19.  Thanks,

Alex


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

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

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-05-19 17:03 [PATCH] vfio: Do not manipulate iommu dma_owner for fake iommu groups Jason Gunthorpe
2022-05-20  1:33 ` Tian, Kevin
2022-05-20  5:32 ` Christoph Hellwig
2022-05-24 16:07 ` Alex Williamson

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).