@@ -292,7 +292,12 @@ int iommu_probe_device(struct device *dev)
mutex_lock(&group->mutex);
iommu_alloc_default_domain(group, dev);
- if (group->default_domain) {
+ /*
+ * If any device in the group has been initialized for user dma,
+ * avoid attaching the default domain.
+ */
+ if (group->default_domain &&
+ group->dma_owner != DMA_OWNER_PRIVATE_DOMAIN_USER) {
ret = __iommu_attach_device(group->default_domain, dev);
if (ret) {
mutex_unlock(&group->mutex);
@@ -2323,7 +2328,7 @@ static int __iommu_attach_group(struct iommu_domain *domain,
{
int ret;
- if (group->default_domain && group->domain != group->default_domain)
+ if (group->domain && group->domain != group->default_domain)
return -EBUSY;
ret = __iommu_group_for_each_dev(group, domain,
@@ -2360,7 +2365,12 @@ static void __iommu_detach_group(struct iommu_domain *domain,
{
int ret;
- if (!group->default_domain) {
+ /*
+ * If any device in the group has been initialized for user dma,
+ * avoid re-attaching the default domain.
+ */
+ if (!group->default_domain ||
+ group->dma_owner == DMA_OWNER_PRIVATE_DOMAIN_USER) {
__iommu_group_for_each_dev(group, domain,
iommu_group_do_detach_device);
group->domain = NULL;
@@ -3373,6 +3383,16 @@ static int iommu_group_set_dma_owner(struct iommu_group *group,
group->owner_cookie = owner_cookie;
group->owner_cnt++;
+ /*
+ * We must ensure that any device DMAs issued after this call
+ * are discarded. DMAs can only reach real memory once someone
+ * has attached a real domain.
+ */
+ if (owner == DMA_OWNER_PRIVATE_DOMAIN_USER &&
+ group->domain &&
+ !WARN_ON(group->domain != group->default_domain))
+ __iommu_detach_group(group->domain, group);
+
unlock_out:
mutex_unlock(&group->mutex);
@@ -3391,6 +3411,15 @@ static void iommu_group_release_dma_owner(struct iommu_group *group,
group->dma_owner = DMA_OWNER_DMA_API;
+ /*
+ * The UNMANAGED domain should be detached before all USER
+ * owners have been released.
+ */
+ if (owner == DMA_OWNER_PRIVATE_DOMAIN_USER) {
+ if (!WARN_ON(group->domain) && group->default_domain)
+ __iommu_attach_group(group->default_domain, group);
+ }
+
unlock_out:
mutex_unlock(&group->mutex);
}
When an iommu group has DMA_OWNER_PRIVATE_DOMAIN_USER set for the first time, it is a contract that the group could be assigned to userspace from now on. The group must be detached from the default iommu domain and all devices in this group are blocked from doing DMA until it is attached to a user controlled iommu_domain. Correspondingly, the default domain should be reattached after the last DMA_OWNER_USER is released. Signed-off-by: Lu Baolu <baolu.lu@linux.intel.com> --- drivers/iommu/iommu.c | 35 ++++++++++++++++++++++++++++++++--- 1 file changed, 32 insertions(+), 3 deletions(-)