diff mbox series

[v3,02/15] iommu: Add device-centric DMA ownership interfaces

Message ID 2-v3-402a7d6459de+24b-iommufd_jgg@nvidia.com
State Superseded
Headers show
Series IOMMUFD Generic interface | expand

Commit Message

Jason Gunthorpe Oct. 25, 2022, 6:12 p.m. UTC
From: Lu Baolu <baolu.lu@linux.intel.com>

These complement the group interfaces and are for use by VFIO. The main
difference is that multiple devices in the same group can all share the
ownership by passing the same ownership pointer.

Move the common code into shared functions.

Signed-off-by: Lu Baolu <baolu.lu@linux.intel.com>
Signed-off-by: Jason Gunthorpe <jgg@nvidia.com>
---
 drivers/iommu/iommu.c | 116 +++++++++++++++++++++++++++++++++---------
 include/linux/iommu.h |  13 +++++
 2 files changed, 104 insertions(+), 25 deletions(-)

Comments

Tian, Kevin Nov. 3, 2022, 5:11 a.m. UTC | #1
> From: Jason Gunthorpe <jgg@nvidia.com>
> Sent: Wednesday, October 26, 2022 2:12 AM
> 
> From: Lu Baolu <baolu.lu@linux.intel.com>
> 
> These complement the group interfaces and are for use by VFIO. The main

s/VFIO/iommufd/

> difference is that multiple devices in the same group can all share the
> ownership by passing the same ownership pointer.
> 
> Move the common code into shared functions.
> 
> Signed-off-by: Lu Baolu <baolu.lu@linux.intel.com>
> Signed-off-by: Jason Gunthorpe <jgg@nvidia.com>
> ---
>  drivers/iommu/iommu.c | 116 +++++++++++++++++++++++++++++++++-------
> --
>  include/linux/iommu.h |  13 +++++
>  2 files changed, 104 insertions(+), 25 deletions(-)
> 
> diff --git a/drivers/iommu/iommu.c b/drivers/iommu/iommu.c
> index 65a3b3d886dc00..e34dde68ae2b0d 100644
> --- a/drivers/iommu/iommu.c
> +++ b/drivers/iommu/iommu.c
> @@ -3148,14 +3148,33 @@ static int
> __iommu_group_alloc_blocking_domain(struct iommu_group *group)
>  	return 0;
>  }
> 
> +static int __iommu_take_dma_ownership(struct iommu_group *group, void
> *owner)
> +{
> +	int ret;
> +
> +	if (group->domain && group->domain != group->default_domain)
> +		return  -EBUSY;

remove the space before -EBUSY

> +int iommu_device_claim_dma_owner(struct device *dev, void *owner)
>  {
> -	int ret;
> +	struct iommu_group *group = iommu_group_get(dev);
> +	int ret = 0;
> +
> +	if (!group)
> +		return -ENODEV;
> 
>  	mutex_lock(&group->mutex);
> -	if (WARN_ON(!group->owner_cnt || !group->owner))
> +	if (group->owner_cnt) {
> +		if (group->owner != owner) {
> +			ret = -EPERM;
> +			goto unlock_out;
> +		}

check owner!=NULL otherwise this call may inadvertently succeed
if the caller assigns a NULL owner while the group has already been
grabbed by a kernel driver.
Jason Gunthorpe Nov. 4, 2022, 7:32 p.m. UTC | #2
On Thu, Nov 03, 2022 at 05:11:00AM +0000, Tian, Kevin wrote:
> > From: Jason Gunthorpe <jgg@nvidia.com>
> > Sent: Wednesday, October 26, 2022 2:12 AM
> > 
> > From: Lu Baolu <baolu.lu@linux.intel.com>
> > 
> > These complement the group interfaces and are for use by VFIO. The main
> 
> s/VFIO/iommufd/

Done:

These complement the group interfaces used by VFIO and are for use by
iommufd. The main difference is that multiple devices in the same group
can all share the ownership by passing the same ownership pointer.


> > difference is that multiple devices in the same group can all share the
> > ownership by passing the same ownership pointer.
> > 
> > Move the common code into shared functions.
> > 
> > Signed-off-by: Lu Baolu <baolu.lu@linux.intel.com>
> > Signed-off-by: Jason Gunthorpe <jgg@nvidia.com>
> > ---
> >  drivers/iommu/iommu.c | 116 +++++++++++++++++++++++++++++++++-------
> 
> remove the space before -EBUSY

Done
 
> > +int iommu_device_claim_dma_owner(struct device *dev, void *owner)
> >  {
> > -	int ret;
> > +	struct iommu_group *group = iommu_group_get(dev);
> > +	int ret = 0;
> > +
> > +	if (!group)
> > +		return -ENODEV;
> > 
> >  	mutex_lock(&group->mutex);
> > -	if (WARN_ON(!group->owner_cnt || !group->owner))
> > +	if (group->owner_cnt) {
> > +		if (group->owner != owner) {
> > +			ret = -EPERM;
> > +			goto unlock_out;
> > +		}
> 
> check owner!=NULL otherwise this call may inadvertently succeed
> if the caller assigns a NULL owner while the group has already been
> grabbed by a kernel driver.

Ah, this is a missed assertion we have already - basically owner ==
NULL is not a valid user owner since we are using it to mean 'dma API
owner'.

So, like this:

@@ -3112,6 +3112,9 @@ static int __iommu_take_dma_ownership(struct iommu_group *group, void *owner)
 {
        int ret;
 
+       if (WARN_ON(!owner))
+               return -EINVAL;
+
        if ((group->domain && group->domain != group->default_domain) ||
            !xa_empty(&group->pasid_array))
                return -EBUSY;
@@ -3141,6 +3144,9 @@ int iommu_group_claim_dma_owner(struct iommu_group *group, void *owner)
 {
        int ret = 0;
 
+       if (WARN_ON(!owner))
+               return -EINVAL;
+
        mutex_lock(&group->mutex);
        if (group->owner_cnt) {
                ret = -EPERM;

Thanks,
Jason
diff mbox series

Patch

diff --git a/drivers/iommu/iommu.c b/drivers/iommu/iommu.c
index 65a3b3d886dc00..e34dde68ae2b0d 100644
--- a/drivers/iommu/iommu.c
+++ b/drivers/iommu/iommu.c
@@ -3148,14 +3148,33 @@  static int __iommu_group_alloc_blocking_domain(struct iommu_group *group)
 	return 0;
 }
 
+static int __iommu_take_dma_ownership(struct iommu_group *group, void *owner)
+{
+	int ret;
+
+	if (group->domain && group->domain != group->default_domain)
+		return  -EBUSY;
+
+	ret = __iommu_group_alloc_blocking_domain(group);
+	if (ret)
+		return ret;
+	ret = __iommu_group_set_domain(group, group->blocking_domain);
+	if (ret)
+		return ret;
+
+	group->owner = owner;
+	group->owner_cnt++;
+	return 0;
+}
+
 /**
  * iommu_group_claim_dma_owner() - Set DMA ownership of a group
  * @group: The group.
  * @owner: Caller specified pointer. Used for exclusive ownership.
  *
- * This is to support backward compatibility for vfio which manages
- * the dma ownership in iommu_group level. New invocations on this
- * interface should be prohibited.
+ * This is to support backward compatibility for vfio which manages the dma
+ * ownership in iommu_group level. New invocations on this interface should be
+ * prohibited. Only a single owner may exist for a group.
  */
 int iommu_group_claim_dma_owner(struct iommu_group *group, void *owner)
 {
@@ -3165,23 +3184,9 @@  int iommu_group_claim_dma_owner(struct iommu_group *group, void *owner)
 	if (group->owner_cnt) {
 		ret = -EPERM;
 		goto unlock_out;
-	} else {
-		if (group->domain && group->domain != group->default_domain) {
-			ret = -EBUSY;
-			goto unlock_out;
-		}
-
-		ret = __iommu_group_alloc_blocking_domain(group);
-		if (ret)
-			goto unlock_out;
-
-		ret = __iommu_group_set_domain(group, group->blocking_domain);
-		if (ret)
-			goto unlock_out;
-		group->owner = owner;
 	}
 
-	group->owner_cnt++;
+	ret = __iommu_take_dma_ownership(group, owner);
 unlock_out:
 	mutex_unlock(&group->mutex);
 
@@ -3190,29 +3195,90 @@  int iommu_group_claim_dma_owner(struct iommu_group *group, void *owner)
 EXPORT_SYMBOL_GPL(iommu_group_claim_dma_owner);
 
 /**
- * iommu_group_release_dma_owner() - Release DMA ownership of a group
- * @group: The group.
+ * iommu_device_claim_dma_owner() - Set DMA ownership of a device
+ * @dev: The device.
+ * @owner: Caller specified pointer. Used for exclusive ownership.
  *
- * Release the DMA ownership claimed by iommu_group_claim_dma_owner().
+ * Claim the DMA ownership of a device. Multiple devices in the same group may
+ * concurrently claim ownership if they present the same owner value. Returns 0
+ * on success and error code on failure
  */
-void iommu_group_release_dma_owner(struct iommu_group *group)
+int iommu_device_claim_dma_owner(struct device *dev, void *owner)
 {
-	int ret;
+	struct iommu_group *group = iommu_group_get(dev);
+	int ret = 0;
+
+	if (!group)
+		return -ENODEV;
 
 	mutex_lock(&group->mutex);
-	if (WARN_ON(!group->owner_cnt || !group->owner))
+	if (group->owner_cnt) {
+		if (group->owner != owner) {
+			ret = -EPERM;
+			goto unlock_out;
+		}
+		group->owner_cnt++;
 		goto unlock_out;
+	}
+
+	ret = __iommu_take_dma_ownership(group, owner);
+unlock_out:
+	mutex_unlock(&group->mutex);
+	iommu_group_put(group);
+
+	return ret;
+}
+EXPORT_SYMBOL_GPL(iommu_device_claim_dma_owner);
+
+static void __iommu_release_dma_ownership(struct iommu_group *group)
+{
+	int ret;
+
+	if (WARN_ON(!group->owner_cnt || !group->owner))
+		return;
 
 	group->owner_cnt = 0;
 	group->owner = NULL;
 	ret = __iommu_group_set_domain(group, group->default_domain);
 	WARN(ret, "iommu driver failed to attach the default domain");
+}
 
-unlock_out:
+/**
+ * iommu_group_release_dma_owner() - Release DMA ownership of a group
+ * @group: The group.
+ *
+ * Release the DMA ownership claimed by iommu_group_claim_dma_owner().
+ */
+void iommu_group_release_dma_owner(struct iommu_group *group)
+{
+	mutex_lock(&group->mutex);
+	__iommu_release_dma_ownership(group);
 	mutex_unlock(&group->mutex);
 }
 EXPORT_SYMBOL_GPL(iommu_group_release_dma_owner);
 
+/**
+ * iommu_device_release_dma_owner() - Release DMA ownership of a device
+ * @group: The device.
+ *
+ * Release the DMA ownership claimed by iommu_device_claim_dma_owner().
+ */
+void iommu_device_release_dma_owner(struct device *dev)
+{
+	struct iommu_group *group = iommu_group_get(dev);
+
+	mutex_lock(&group->mutex);
+	if (group->owner_cnt > 1) {
+		group->owner_cnt--;
+		goto unlock_out;
+	}
+	__iommu_release_dma_ownership(group);
+unlock_out:
+	mutex_unlock(&group->mutex);
+	iommu_group_put(group);
+}
+EXPORT_SYMBOL_GPL(iommu_device_release_dma_owner);
+
 /**
  * iommu_group_dma_owner_claimed() - Query group dma ownership status
  * @group: The group.
diff --git a/include/linux/iommu.h b/include/linux/iommu.h
index 40cf2d8393465f..b33c962d7b851b 100644
--- a/include/linux/iommu.h
+++ b/include/linux/iommu.h
@@ -681,6 +681,9 @@  int iommu_group_claim_dma_owner(struct iommu_group *group, void *owner);
 void iommu_group_release_dma_owner(struct iommu_group *group);
 bool iommu_group_dma_owner_claimed(struct iommu_group *group);
 
+int iommu_device_claim_dma_owner(struct device *dev, void *owner);
+void iommu_device_release_dma_owner(struct device *dev);
+
 #else /* CONFIG_IOMMU_API */
 
 struct iommu_ops {};
@@ -1043,6 +1046,16 @@  static inline bool iommu_group_dma_owner_claimed(struct iommu_group *group)
 {
 	return false;
 }
+
+static inline int
+iommu_device_claim_dma_owner(struct device *dev, void *owner)
+{
+	return -ENODEV;
+}
+
+static inline void iommu_device_release_dma_owner(struct device *dev)
+{
+}
 #endif /* CONFIG_IOMMU_API */
 
 /**