Commit 89395cce authored by Lu Baolu's avatar Lu Baolu Committed by Jason Gunthorpe

iommu: Add device-centric DMA ownership interfaces

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.

Move the common code into shared functions.

Link: https://lore.kernel.org/r/2-v6-a196d26f289e+11787-iommufd_jgg@nvidia.comTested-by: default avatarNicolin Chen <nicolinc@nvidia.com>
Tested-by: default avatarYi Liu <yi.l.liu@intel.com>
Tested-by: default avatarLixiao Yang <lixiao.yang@intel.com>
Tested-by: default avatarMatthew Rosato <mjrosato@linux.ibm.com>
Reviewed-by: default avatarKevin Tian <kevin.tian@intel.com>
Reviewed-by: default avatarEric Auger <eric.auger@redhat.com>
Signed-off-by: default avatarLu Baolu <baolu.lu@linux.intel.com>
Signed-off-by: default avatarJason Gunthorpe <jgg@nvidia.com>
parent 4989764d
...@@ -3108,41 +3108,49 @@ static int __iommu_group_alloc_blocking_domain(struct iommu_group *group) ...@@ -3108,41 +3108,49 @@ static int __iommu_group_alloc_blocking_domain(struct iommu_group *group)
return 0; return 0;
} }
static int __iommu_take_dma_ownership(struct iommu_group *group, void *owner)
{
int ret;
if ((group->domain && group->domain != group->default_domain) ||
!xa_empty(&group->pasid_array))
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 * iommu_group_claim_dma_owner() - Set DMA ownership of a group
* @group: The group. * @group: The group.
* @owner: Caller specified pointer. Used for exclusive ownership. * @owner: Caller specified pointer. Used for exclusive ownership.
* *
* This is to support backward compatibility for vfio which manages * This is to support backward compatibility for vfio which manages the dma
* the dma ownership in iommu_group level. New invocations on this * ownership in iommu_group level. New invocations on this interface should be
* interface should be prohibited. * prohibited. Only a single owner may exist for a group.
*/ */
int iommu_group_claim_dma_owner(struct iommu_group *group, void *owner) int iommu_group_claim_dma_owner(struct iommu_group *group, void *owner)
{ {
int ret = 0; int ret = 0;
if (WARN_ON(!owner))
return -EINVAL;
mutex_lock(&group->mutex); mutex_lock(&group->mutex);
if (group->owner_cnt) { if (group->owner_cnt) {
ret = -EPERM; ret = -EPERM;
goto unlock_out; goto unlock_out;
} else {
if ((group->domain && group->domain != group->default_domain) ||
!xa_empty(&group->pasid_array)) {
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: unlock_out:
mutex_unlock(&group->mutex); mutex_unlock(&group->mutex);
...@@ -3151,30 +3159,91 @@ int iommu_group_claim_dma_owner(struct iommu_group *group, void *owner) ...@@ -3151,30 +3159,91 @@ int iommu_group_claim_dma_owner(struct iommu_group *group, void *owner)
EXPORT_SYMBOL_GPL(iommu_group_claim_dma_owner); EXPORT_SYMBOL_GPL(iommu_group_claim_dma_owner);
/** /**
* iommu_group_release_dma_owner() - Release DMA ownership of a group * iommu_device_claim_dma_owner() - Set DMA ownership of a device
* @group: The group. * @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;
if (WARN_ON(!owner))
return -EINVAL;
mutex_lock(&group->mutex); mutex_lock(&group->mutex);
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 || if (WARN_ON(!group->owner_cnt || !group->owner ||
!xa_empty(&group->pasid_array))) !xa_empty(&group->pasid_array)))
goto unlock_out; return;
group->owner_cnt = 0; group->owner_cnt = 0;
group->owner = NULL; group->owner = NULL;
ret = __iommu_group_set_domain(group, group->default_domain); ret = __iommu_group_set_domain(group, group->default_domain);
WARN(ret, "iommu driver failed to attach the 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
* @dev: The device
*
* 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); mutex_unlock(&group->mutex);
} }
EXPORT_SYMBOL_GPL(iommu_group_release_dma_owner); 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--;
else
__iommu_release_dma_ownership(group);
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 * iommu_group_dma_owner_claimed() - Query group dma ownership status
* @group: The group. * @group: The group.
......
...@@ -707,6 +707,9 @@ int iommu_group_claim_dma_owner(struct iommu_group *group, void *owner); ...@@ -707,6 +707,9 @@ int iommu_group_claim_dma_owner(struct iommu_group *group, void *owner);
void iommu_group_release_dma_owner(struct iommu_group *group); void iommu_group_release_dma_owner(struct iommu_group *group);
bool iommu_group_dma_owner_claimed(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);
struct iommu_domain *iommu_sva_domain_alloc(struct device *dev, struct iommu_domain *iommu_sva_domain_alloc(struct device *dev,
struct mm_struct *mm); struct mm_struct *mm);
int iommu_attach_device_pasid(struct iommu_domain *domain, int iommu_attach_device_pasid(struct iommu_domain *domain,
...@@ -1064,6 +1067,15 @@ static inline bool iommu_group_dma_owner_claimed(struct iommu_group *group) ...@@ -1064,6 +1067,15 @@ static inline bool iommu_group_dma_owner_claimed(struct iommu_group *group)
return false; return false;
} }
static inline void iommu_device_release_dma_owner(struct device *dev)
{
}
static inline int iommu_device_claim_dma_owner(struct device *dev, void *owner)
{
return -ENODEV;
}
static inline struct iommu_domain * static inline struct iommu_domain *
iommu_sva_domain_alloc(struct device *dev, struct mm_struct *mm) iommu_sva_domain_alloc(struct device *dev, struct mm_struct *mm)
{ {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment