Skip to contentSkip to conversation

iommu: Flow ERR_PTR out from __iommu_domain_alloc()

PATCH Superseded Jason Gunthorpe 4 participants 9 messages Nov 1, 2023 – Nov 2, 2023 IOMMU

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 iommu: Flow ERR_PTR out from __iommu_domain_alloc()

Most of the calling code now has error handling that can carry an error code further up the call chain. Keep the exported interface iommu_domain_alloc() returning NULL and reflow the internal code to use ERR_PTR not NULL for domain allocation failure.

Optionally allow drivers to return ERR_PTR from any of the alloc ops. Many of the new ops (user, sva, etc) already return ERR_PTR, so having two rules is confusing and hard on drivers. This fixes a bug in DART that was returning ERR_PTR.

This also fixes a bug in iommu_group_alloc_default_domain() where it returned both NULL and ERR_PTR, now it always returns ERR_PTR.

Fixes 482feb5c6492 ("iommu/dart: Call apple_dart_finalize_domain() as part of alloc_paging()")
Fixes 1c68cbc64fe6 ("iommu: Add IOMMU_DOMAIN_PLATFORM")
---
 drivers/iommu/iommu.c | 53 +++++++++++++++++++++++++++++--------------
 1 file changed, 36 insertions(+), 17 deletions(-)
drivers/iommu/iommu.c +36 -17
index f17a1113f3d6a3..6bc044d2310487 100644
@@ -1802,10 +1802,10 @@ iommu_group_alloc_default_domain(struct iommu_group *group, int req_type)
 
 	/* Otherwise IDENTITY and DMA_FQ defaults will try DMA */
 	if (iommu_def_domain_type == IOMMU_DOMAIN_DMA)
-		return NULL;
+		return ERR_PTR(-ENODEV);
 	dom = __iommu_group_alloc_default_domain(group, IOMMU_DOMAIN_DMA);
 	if (!dom)
-		return NULL;
+		return dom;
 
 	pr_warn("Failed to allocate default IOMMU domain of type %u for group %s - Falling back to IOMMU_DOMAIN_DMA",
 		iommu_def_domain_type, group->name);
@@ -2094,10 +2094,17 @@ static struct iommu_domain *__iommu_domain_alloc(const struct iommu_ops *ops,
 	else if (ops->domain_alloc)
 		domain = ops->domain_alloc(alloc_type);
 	else
-		return NULL;
+		return ERR_PTR(-EOPNOTSUPP);
 
+	/*
+	 * Many domain_alloc ops now return ERR_PTR, make things easier for the
+	 * driver by accepting ERR_PTR from all domain_alloc ops instead of
+	 * having two rules.
+	 */
+	if (IS_ERR(domain))
+		return domain;
 	if (!domain)
-		return NULL;
+		return ERR_PTR(-ENOMEM);
 
 	domain->type = type;
 	/*
@@ -2110,9 +2117,14 @@ static struct iommu_domain *__iommu_domain_alloc(const struct iommu_ops *ops,
 	if (!domain->ops)
 		domain->ops = ops->default_domain_ops;
 
-	if (iommu_is_dma_domain(domain) && iommu_get_dma_cookie(domain)) {
-		iommu_domain_free(domain);
-		domain = NULL;
+	if (iommu_is_dma_domain(domain)) {
+		int rc;
+
+		rc = iommu_get_dma_cookie(domain);
+		if (rc) {
+			iommu_domain_free(domain);
+			return ERR_PTR(rc);
+		}
 	}
 	return domain;
 }
@@ -2129,10 +2141,15 @@ __iommu_group_domain_alloc(struct iommu_group *group, unsigned int type)
 
 struct iommu_domain *iommu_domain_alloc(const struct bus_type *bus)
 {
+	struct iommu_domain *domain;
+
 	if (bus == NULL || bus->iommu_ops == NULL)
 		return NULL;
-	return __iommu_domain_alloc(bus->iommu_ops, NULL,
+	domain = __iommu_domain_alloc(bus->iommu_ops, NULL,
 				    IOMMU_DOMAIN_UNMANAGED);
+	if (IS_ERR(domain))
+		return NULL;
+	return domain;
 }
 EXPORT_SYMBOL_GPL(iommu_domain_alloc);
 
@@ -3041,8 +3058,8 @@ static int iommu_setup_default_domain(struct iommu_group *group,
 		return -EINVAL;
 
 	dom = iommu_group_alloc_default_domain(group, req_type);
-	if (!dom)
-		return -ENODEV;
+	if (IS_ERR(dom))
+		return PTR_ERR(dom);
 
 	if (group->default_domain == dom)
 		return 0;
@@ -3243,21 +3260,23 @@ void iommu_device_unuse_default_domain(struct device *dev)
 
 static int __iommu_group_alloc_blocking_domain(struct iommu_group *group)
 {
+	struct iommu_domain *domain;
+
 	if (group->blocking_domain)
 		return 0;
 
-	group->blocking_domain =
-		__iommu_group_domain_alloc(group, IOMMU_DOMAIN_BLOCKED);
-	if (!group->blocking_domain) {
+	domain = __iommu_group_domain_alloc(group, IOMMU_DOMAIN_BLOCKED);
+	if (IS_ERR(domain)) {
 		/*
 		 * For drivers that do not yet understand IOMMU_DOMAIN_BLOCKED
 		 * create an empty domain instead.
 		 */
-		group->blocking_domain = __iommu_group_domain_alloc(
-			group, IOMMU_DOMAIN_UNMANAGED);
-		if (!group->blocking_domain)
-			return -EINVAL;
+		domain = __iommu_group_domain_alloc(group,
+						    IOMMU_DOMAIN_UNMANAGED);
+		if (IS_ERR(domain))
+			return PTR_ERR(domain);
 	}
+	group->blocking_domain = domain;
 	return 0;
 }
 

base-commit: e8cca466a84a75f8ff2a7a31173c99ee6d1c59d2

Signature · 1 line
2.42.0
#1 lore raw
Quoted 27 lines · Jason Gunthorpe

Most of the calling code now has error handling that can carry an error code further up the call chain. Keep the exported interface iommu_domain_alloc() returning NULL and reflow the internal code to use ERR_PTR not NULL for domain allocation failure.

Optionally allow drivers to return ERR_PTR from any of the alloc ops. Many of the new ops (user, sva, etc) already return ERR_PTR, so having two rules is confusing and hard on drivers. This fixes a bug in DART that was returning ERR_PTR.

This also fixes a bug in iommu_group_alloc_default_domain() where it returned both NULL and ERR_PTR, now it always returns ERR_PTR.

Fixes 482feb5c6492 ("iommu/dart: Call apple_dart_finalize_domain() as part of alloc_paging()")
Fixes 1c68cbc64fe6 ("iommu: Add IOMMU_DOMAIN_PLATFORM")
---
 drivers/iommu/iommu.c | 53 +++++++++++++++++++++++++++++--------------
 1 file changed, 36 insertions(+), 17 deletions(-)
drivers/iommu/iommu.c +0 -0
index f17a1113f3d6a3..6bc044d2310487 100644

drivers/iommu/iommu.c iommu_group_alloc_default_domain(struct iommu_group *group, int req_type) · line 1807

Quoted 7 lines
drivers/iommu/iommu.c +1 -1
@@ -1802,10 +1802,10 @@ iommu_group_alloc_default_domain(struct iommu_group *group, int req_type)
 
 	/* Otherwise IDENTITY and DMA_FQ defaults will try DMA */
 	if (iommu_def_domain_type == IOMMU_DOMAIN_DMA)
-		return NULL;
+		return ERR_PTR(-ENODEV);
 	dom = __iommu_group_alloc_default_domain(group, IOMMU_DOMAIN_DMA);
 	if (!dom)

Should this be if(IS_ERR(dom)) ?

Quoted 5 lines
drivers/iommu/iommu.c +1 -1
-		return NULL;
+		return dom;
 
 	pr_warn("Failed to allocate default IOMMU domain of type %u for group %s - Falling back to IOMMU_DOMAIN_DMA",
 		iommu_def_domain_type, group->name);
#2 lore raw
Quoted 29 lines · Jerry Snitselaar
Quoted 28 lines · Jason Gunthorpe

Most of the calling code now has error handling that can carry an error code further up the call chain. Keep the exported interface iommu_domain_alloc() returning NULL and reflow the internal code to use ERR_PTR not NULL for domain allocation failure.

Optionally allow drivers to return ERR_PTR from any of the alloc ops. Many of the new ops (user, sva, etc) already return ERR_PTR, so having two rules is confusing and hard on drivers. This fixes a bug in DART that was returning ERR_PTR.

This also fixes a bug in iommu_group_alloc_default_domain() where it returned both NULL and ERR_PTR, now it always returns ERR_PTR.

Fixes 482feb5c6492 ("iommu/dart: Call apple_dart_finalize_domain() as part of alloc_paging()")
Fixes 1c68cbc64fe6 ("iommu: Add IOMMU_DOMAIN_PLATFORM")
---
 drivers/iommu/iommu.c | 53 +++++++++++++++++++++++++++++--------------
 1 file changed, 36 insertions(+), 17 deletions(-)
drivers/iommu/iommu.c +0 -0
index f17a1113f3d6a3..6bc044d2310487 100644
@@ -1802,10 +1802,10 @@ iommu_group_alloc_default_domain(struct iommu_group *group, int req_type)
 
Quoted 8 lines

drivers/iommu/iommu.c iommu_group_alloc_default_domain(struct iommu_group *group, int req_type) · line 1807

Quoted 6 lines
drivers/iommu/iommu.c +1 -1
 	/* Otherwise IDENTITY and DMA_FQ defaults will try DMA */
 	if (iommu_def_domain_type == IOMMU_DOMAIN_DMA)
-		return NULL;
+		return ERR_PTR(-ENODEV);
 	dom = __iommu_group_alloc_default_domain(group, IOMMU_DOMAIN_DMA);
 	if (!dom)

Should this be if(IS_ERR(dom)) ?

Blah, I think I somehow missed this function:

@@ -1788,7 +1788,7 @@ iommu_group_alloc_default_domain(struct iommu_group *group, int req_type)

         */
        if (ops->default_domain) {
                if (req_type)
-                       return NULL;
+                       return ERR_PTR(-EINVAL);
                return ops->default_domain;
        }

@@ -1797,14 +1797,14 @@ iommu_group_alloc_default_domain(struct iommu_group *group, int req_type)

        /* The driver gave no guidance on what type to use, try the default */
        dom = __iommu_group_alloc_default_domain(group, iommu_def_domain_type);
-       if (dom)
+       if (!IS_ERR(dom))
                return dom;
 
        /* Otherwise IDENTITY and DMA_FQ defaults will try DMA */
        if (iommu_def_domain_type == IOMMU_DOMAIN_DMA)
-               return ERR_PTR(-ENODEV);
+               return ERR_PTR(-EINVAL);
        dom = __iommu_group_alloc_default_domain(group, IOMMU_DOMAIN_DMA);

- if (!dom)
+ if (IS_ERR(dom))
return dom;

pr_warn("Failed to allocate default IOMMU domain of type %u for group %s - Falling back to IOMMU_DOMAIN_DMA",

Thanks!
Jason

#3 lore raw
Quoted 6 lines · Jason Gunthorpe
        /* The driver gave no guidance on what type to use, try the default */
        dom = __iommu_group_alloc_default_domain(group, iommu_def_domain_type);
(quoted diff) +1 -1
-       if (dom)
+       if (!IS_ERR(dom))
                return dom;
 

Review

Quoted 10 lines
(quoted diff) +2 -2
        /* Otherwise IDENTITY and DMA_FQ defaults will try DMA */
        if (iommu_def_domain_type == IOMMU_DOMAIN_DMA)
-               return ERR_PTR(-ENODEV);
+               return ERR_PTR(-EINVAL);
        dom = __iommu_group_alloc_default_domain(group, IOMMU_DOMAIN_DMA);
-       if (!dom)
+       if (IS_ERR(dom))
                return dom;
 
        pr_warn("Failed to allocate default IOMMU domain of type %u for group %s - Falling back to IOMMU_DOMAIN_DMA",

Not related to your patch, but we should delete/move this warning as well. It's on a success path.

regards,
dan carpenter

#4 lore raw
Quoted 16 lines · Dan Carpenter
Quoted 15 lines · Jason Gunthorpe
        /* The driver gave no guidance on what type to use, try the default */
        dom = __iommu_group_alloc_default_domain(group, iommu_def_domain_type);
(quoted diff) +3 -3
-       if (dom)
+       if (!IS_ERR(dom))
                return dom;
 
        /* Otherwise IDENTITY and DMA_FQ defaults will try DMA */
        if (iommu_def_domain_type == IOMMU_DOMAIN_DMA)
-               return ERR_PTR(-ENODEV);
+               return ERR_PTR(-EINVAL);
        dom = __iommu_group_alloc_default_domain(group, IOMMU_DOMAIN_DMA);
-       if (!dom)
+       if (IS_ERR(dom))
                return dom;
Quoted 8 lines
Quoted 1 line

pr_warn("Failed to allocate default IOMMU domain of type %u for group %s - Falling back to IOMMU_DOMAIN_DMA",

Not related to your patch, but we should delete/move this warning as well. It's on a success path.

regards,
dan carpenter

It seems worthwhile to note due to the performance implications, if nothing else, of having to fall back to the dma domain type from the two types mentioned in the comment. I'm not sure where would be a better place since this is first spot where it knows that is going to be the case.

#5 lore raw
Quoted 26 lines · Jerry Snitselaar
Quoted 16 lines · Dan Carpenter
Quoted 15 lines · Jason Gunthorpe
        /* The driver gave no guidance on what type to use, try the default */
        dom = __iommu_group_alloc_default_domain(group, iommu_def_domain_type);
(quoted diff) +3 -3
-       if (dom)
+       if (!IS_ERR(dom))
                return dom;
 
        /* Otherwise IDENTITY and DMA_FQ defaults will try DMA */
        if (iommu_def_domain_type == IOMMU_DOMAIN_DMA)
-               return ERR_PTR(-ENODEV);
+               return ERR_PTR(-EINVAL);
        dom = __iommu_group_alloc_default_domain(group, IOMMU_DOMAIN_DMA);
-       if (!dom)
+       if (IS_ERR(dom))
                return dom;
Quoted 8 lines
Quoted 1 line

pr_warn("Failed to allocate default IOMMU domain of type %u for group %s - Falling back to IOMMU_DOMAIN_DMA",

Not related to your patch, but we should delete/move this warning as well. It's on a success path.

regards,
dan carpenter

Quoted 5 lines

It seems worthwhile to note due to the performance implications, if nothing else, of having to fall back to the dma domain type from the two types mentioned in the comment. I'm not sure where would be a better place since this is first spot where it knows that is going to be the case.

Maybe it should be a pr_info() instead of a pr_warn()? Maybe I'm just wrong.

regards,
dan carpenter

#6 lore raw
Quoted 27 lines · Dan Carpenter
Quoted 26 lines · Jerry Snitselaar
Quoted 24 lines · Dan Carpenter
Quoted 6 lines · Jason Gunthorpe
        /* The driver gave no guidance on what type to use, try the default */
        dom = __iommu_group_alloc_default_domain(group, iommu_def_domain_type);
(quoted diff) +1 -1
-       if (dom)
+       if (!IS_ERR(dom))
                return dom;
 

Review

Quoted 10 lines
(quoted diff) +2 -2
        /* Otherwise IDENTITY and DMA_FQ defaults will try DMA */
        if (iommu_def_domain_type == IOMMU_DOMAIN_DMA)
-               return ERR_PTR(-ENODEV);
+               return ERR_PTR(-EINVAL);
        dom = __iommu_group_alloc_default_domain(group, IOMMU_DOMAIN_DMA);
-       if (!dom)
+       if (IS_ERR(dom))
                return dom;
 
        pr_warn("Failed to allocate default IOMMU domain of type %u for group %s - Falling back to IOMMU_DOMAIN_DMA",

Not related to your patch, but we should delete/move this warning as well. It's on a success path.

regards,
dan carpenter

Quoted 8 lines
Quoted 5 lines

It seems worthwhile to note due to the performance implications, if nothing else, of having to fall back to the dma domain type from the two types mentioned in the comment. I'm not sure where would be a better place since this is first spot where it knows that is going to be the case.

Maybe it should be a pr_info() instead of a pr_warn()? Maybe I'm just wrong.

TBH, I'm not entirely sure what it is for, it has been there a long time.. I think it means that kernel config and command line options are incompatible with this iommu driver

But at this moment there are very few drivers that don't support IDENTITY (s390 notably, I suppose) and the DMA_FQ stuff is no longer driver visible, so it probably never triggers..

Jason

reviewer
#7 lore raw
Quoted 34 lines · Jason Gunthorpe
Quoted 33 lines · Dan Carpenter
Quoted 31 lines · Jerry Snitselaar
Quoted 16 lines · Dan Carpenter
Quoted 15 lines · Jason Gunthorpe
         /* The driver gave no guidance on what type to use, try the default */
         dom = __iommu_group_alloc_default_domain(group, iommu_def_domain_type);
(quoted diff) +3 -3
-       if (dom)
+       if (!IS_ERR(dom))
                 return dom;
 
         /* Otherwise IDENTITY and DMA_FQ defaults will try DMA */
         if (iommu_def_domain_type == IOMMU_DOMAIN_DMA)
-               return ERR_PTR(-ENODEV);
+               return ERR_PTR(-EINVAL);
         dom = __iommu_group_alloc_default_domain(group, IOMMU_DOMAIN_DMA);
-       if (!dom)
+       if (IS_ERR(dom))
                 return dom;
Quoted 8 lines
Quoted 1 line

pr_warn("Failed to allocate default IOMMU domain of type %u for group %s - Falling back to IOMMU_DOMAIN_DMA",

Not related to your patch, but we should delete/move this warning as well. It's on a success path.

regards,
dan carpenter

It seems worthwhile to note due to the performance implications, if nothing else, of having to fall back to the dma domain type from the two types mentioned in the comment. I'm not sure where would be a better place since this is first spot where it knows that is going to be the case.

Quoted 6 lines
Quoted 2 lines

Maybe it should be a pr_info() instead of a pr_warn()? Maybe I'm just wrong.

TBH, I'm not entirely sure what it is for, it has been there a long time.. I think it means that kernel config and command line options are incompatible with this iommu driver

Yes, it's a failure path in the sense that the user has requested something (via config or command line) that the IOMMU driver cannot provide. Falling back to allocating something different is more "damage limitation" than "success".

Quoted 3 lines

But at this moment there are very few drivers that don't support IDENTITY (s390 notably, I suppose) and the DMA_FQ stuff is no longer driver visible, so it probably never triggers..

Indeed if we get to the point where any platform/driver combination that would support IOMMU_DOMAIN_DMA also supports IOMMU_DOMAIN_IDENTITY anyway, then this fallback is no longer useful (and we could consequently also simplify some stuff further out), however as things stand I see at least apple-dart remaining a thorn in the side of that plan.

Thanks,
Robin.

#8 lore raw
Quoted 49 lines · Robin Murphy
Quoted 32 lines · Jason Gunthorpe
Quoted 31 lines · Dan Carpenter
Quoted 29 lines · Jerry Snitselaar
Quoted 16 lines · Dan Carpenter
Quoted 4 lines · Jason Gunthorpe
         /* The driver gave no guidance on what type to use, try the default */
         dom = __iommu_group_alloc_default_domain(group, iommu_def_domain_type);
(quoted diff) +1 -1
-       if (dom)
+       if (!IS_ERR(dom))

Review

Quoted 10 lines
(quoted diff) +2 -2
                 return dom;
         /* Otherwise IDENTITY and DMA_FQ defaults will try DMA */
         if (iommu_def_domain_type == IOMMU_DOMAIN_DMA)
-               return ERR_PTR(-ENODEV);
+               return ERR_PTR(-EINVAL);
         dom = __iommu_group_alloc_default_domain(group, IOMMU_DOMAIN_DMA);
-       if (!dom)
+       if (IS_ERR(dom))
                 return dom;
         pr_warn("Failed to allocate default IOMMU domain of type %u for group %s - Falling back to IOMMU_DOMAIN_DMA",
Quoted 6 lines

Not related to your patch, but we should delete/move this warning as well. It's on a success path.

regards,
dan carpenter

It seems worthwhile to note due to the performance implications, if nothing else, of having to fall back to the dma domain type from the two types mentioned in the comment. I'm not sure where would be a better place since this is first spot where it knows that is going to be the case.

Quoted 6 lines
Quoted 2 lines

Maybe it should be a pr_info() instead of a pr_warn()? Maybe I'm just wrong.

TBH, I'm not entirely sure what it is for, it has been there a long time.. I think it means that kernel config and command line options are incompatible with this iommu driver

Yes, it's a failure path in the sense that the user has requested something (via config or command line) that the IOMMU driver cannot provide. Falling back to allocating something different is more "damage limitation" than "success".

Quoted 3 lines

But at this moment there are very few drivers that don't support IDENTITY (s390 notably, I suppose) and the DMA_FQ stuff is no longer driver visible, so it probably never triggers..

Quoted 8 lines

Indeed if we get to the point where any platform/driver combination that would support IOMMU_DOMAIN_DMA also supports IOMMU_DOMAIN_IDENTITY anyway, then this fallback is no longer useful (and we could consequently also simplify some stuff further out), however as things stand I see at least apple-dart remaining a thorn in the side of that plan.

Thanks,
Robin.

Btw Robin, I was able to verify the PCI SAC change works well in a case recently. Went from a drop off of 40Gbps to 1Gbps, to no noticeable drop off.

Regards,
Jerry