iommu.lists.linux-foundation.org archive mirror
 help / color / mirror / Atom feed
* [PATCH v3] iommu: Check dev->iommu in iommu_dev_xxx functions
@ 2021-03-03 17:36 Shameer Kolothum
  2021-03-09 12:50 ` Robin Murphy
  2021-03-18  9:48 ` Joerg Roedel
  0 siblings, 2 replies; 3+ messages in thread
From: Shameer Kolothum @ 2021-03-03 17:36 UTC (permalink / raw)
  To: linux-kernel, iommu
  Cc: jean-philippe, will, linuxarm, prime.zeng, robin.murphy

The device iommu probe/attach might have failed leaving dev->iommu
to NULL and device drivers may still invoke these functions resulting
in a crash in iommu vendor driver code.

Hence make sure we check that.

Fixes: a3a195929d40 ("iommu: Add APIs for multiple domains per device")
Signed-off-by: Shameer Kolothum <shameerali.kolothum.thodi@huawei.com>
---
v2 --> v3
 -Removed iommu_ops from struct dev_iommu.
v1 --> v2:
 -Added iommu_ops to struct dev_iommu based on the discussion with Robin.
 -Rebased against iommu-tree core branch.
---
 drivers/iommu/iommu.c | 24 +++++++++++++++---------
 1 file changed, 15 insertions(+), 9 deletions(-)

diff --git a/drivers/iommu/iommu.c b/drivers/iommu/iommu.c
index d0b0a15dba84..e10cfa99057c 100644
--- a/drivers/iommu/iommu.c
+++ b/drivers/iommu/iommu.c
@@ -2878,10 +2878,12 @@ EXPORT_SYMBOL_GPL(iommu_fwspec_add_ids);
  */
 int iommu_dev_enable_feature(struct device *dev, enum iommu_dev_features feat)
 {
-	const struct iommu_ops *ops = dev->bus->iommu_ops;
+	if (dev->iommu && dev->iommu->iommu_dev) {
+		const struct iommu_ops *ops = dev->iommu->iommu_dev->ops;
 
-	if (ops && ops->dev_enable_feat)
-		return ops->dev_enable_feat(dev, feat);
+		if (ops->dev_enable_feat)
+			return ops->dev_enable_feat(dev, feat);
+	}
 
 	return -ENODEV;
 }
@@ -2894,10 +2896,12 @@ EXPORT_SYMBOL_GPL(iommu_dev_enable_feature);
  */
 int iommu_dev_disable_feature(struct device *dev, enum iommu_dev_features feat)
 {
-	const struct iommu_ops *ops = dev->bus->iommu_ops;
+	if (dev->iommu && dev->iommu->iommu_dev) {
+		const struct iommu_ops *ops = dev->iommu->iommu_dev->ops;
 
-	if (ops && ops->dev_disable_feat)
-		return ops->dev_disable_feat(dev, feat);
+		if (ops->dev_disable_feat)
+			return ops->dev_disable_feat(dev, feat);
+	}
 
 	return -EBUSY;
 }
@@ -2905,10 +2909,12 @@ EXPORT_SYMBOL_GPL(iommu_dev_disable_feature);
 
 bool iommu_dev_feature_enabled(struct device *dev, enum iommu_dev_features feat)
 {
-	const struct iommu_ops *ops = dev->bus->iommu_ops;
+	if (dev->iommu && dev->iommu->iommu_dev) {
+		const struct iommu_ops *ops = dev->iommu->iommu_dev->ops;
 
-	if (ops && ops->dev_feat_enabled)
-		return ops->dev_feat_enabled(dev, feat);
+		if (ops->dev_feat_enabled)
+			return ops->dev_feat_enabled(dev, feat);
+	}
 
 	return false;
 }
-- 
2.17.1

_______________________________________________
iommu mailing list
iommu@lists.linux-foundation.org
https://lists.linuxfoundation.org/mailman/listinfo/iommu

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

* Re: [PATCH v3] iommu: Check dev->iommu in iommu_dev_xxx functions
  2021-03-03 17:36 [PATCH v3] iommu: Check dev->iommu in iommu_dev_xxx functions Shameer Kolothum
@ 2021-03-09 12:50 ` Robin Murphy
  2021-03-18  9:48 ` Joerg Roedel
  1 sibling, 0 replies; 3+ messages in thread
From: Robin Murphy @ 2021-03-09 12:50 UTC (permalink / raw)
  To: Shameer Kolothum, linux-kernel, iommu
  Cc: jean-philippe, will, linuxarm, prime.zeng

On 2021-03-03 17:36, Shameer Kolothum wrote:
> The device iommu probe/attach might have failed leaving dev->iommu
> to NULL and device drivers may still invoke these functions resulting
> in a crash in iommu vendor driver code.
> 
> Hence make sure we check that.

Reviewed-by: Robin Murphy <robin.murphy@arm.com>

> Fixes: a3a195929d40 ("iommu: Add APIs for multiple domains per device")
> Signed-off-by: Shameer Kolothum <shameerali.kolothum.thodi@huawei.com>
> ---
> v2 --> v3
>   -Removed iommu_ops from struct dev_iommu.
> v1 --> v2:
>   -Added iommu_ops to struct dev_iommu based on the discussion with Robin.
>   -Rebased against iommu-tree core branch.
> ---
>   drivers/iommu/iommu.c | 24 +++++++++++++++---------
>   1 file changed, 15 insertions(+), 9 deletions(-)
> 
> diff --git a/drivers/iommu/iommu.c b/drivers/iommu/iommu.c
> index d0b0a15dba84..e10cfa99057c 100644
> --- a/drivers/iommu/iommu.c
> +++ b/drivers/iommu/iommu.c
> @@ -2878,10 +2878,12 @@ EXPORT_SYMBOL_GPL(iommu_fwspec_add_ids);
>    */
>   int iommu_dev_enable_feature(struct device *dev, enum iommu_dev_features feat)
>   {
> -	const struct iommu_ops *ops = dev->bus->iommu_ops;
> +	if (dev->iommu && dev->iommu->iommu_dev) {
> +		const struct iommu_ops *ops = dev->iommu->iommu_dev->ops;
>   
> -	if (ops && ops->dev_enable_feat)
> -		return ops->dev_enable_feat(dev, feat);
> +		if (ops->dev_enable_feat)
> +			return ops->dev_enable_feat(dev, feat);
> +	}
>   
>   	return -ENODEV;
>   }
> @@ -2894,10 +2896,12 @@ EXPORT_SYMBOL_GPL(iommu_dev_enable_feature);
>    */
>   int iommu_dev_disable_feature(struct device *dev, enum iommu_dev_features feat)
>   {
> -	const struct iommu_ops *ops = dev->bus->iommu_ops;
> +	if (dev->iommu && dev->iommu->iommu_dev) {
> +		const struct iommu_ops *ops = dev->iommu->iommu_dev->ops;
>   
> -	if (ops && ops->dev_disable_feat)
> -		return ops->dev_disable_feat(dev, feat);
> +		if (ops->dev_disable_feat)
> +			return ops->dev_disable_feat(dev, feat);
> +	}
>   
>   	return -EBUSY;
>   }
> @@ -2905,10 +2909,12 @@ EXPORT_SYMBOL_GPL(iommu_dev_disable_feature);
>   
>   bool iommu_dev_feature_enabled(struct device *dev, enum iommu_dev_features feat)
>   {
> -	const struct iommu_ops *ops = dev->bus->iommu_ops;
> +	if (dev->iommu && dev->iommu->iommu_dev) {
> +		const struct iommu_ops *ops = dev->iommu->iommu_dev->ops;
>   
> -	if (ops && ops->dev_feat_enabled)
> -		return ops->dev_feat_enabled(dev, feat);
> +		if (ops->dev_feat_enabled)
> +			return ops->dev_feat_enabled(dev, feat);
> +	}
>   
>   	return false;
>   }
> 
_______________________________________________
iommu mailing list
iommu@lists.linux-foundation.org
https://lists.linuxfoundation.org/mailman/listinfo/iommu

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

* Re: [PATCH v3] iommu: Check dev->iommu in iommu_dev_xxx functions
  2021-03-03 17:36 [PATCH v3] iommu: Check dev->iommu in iommu_dev_xxx functions Shameer Kolothum
  2021-03-09 12:50 ` Robin Murphy
@ 2021-03-18  9:48 ` Joerg Roedel
  1 sibling, 0 replies; 3+ messages in thread
From: Joerg Roedel @ 2021-03-18  9:48 UTC (permalink / raw)
  To: Shameer Kolothum
  Cc: jean-philippe, will, linux-kernel, linuxarm, iommu, prime.zeng,
	robin.murphy

On Wed, Mar 03, 2021 at 05:36:11PM +0000, Shameer Kolothum wrote:
> The device iommu probe/attach might have failed leaving dev->iommu
> to NULL and device drivers may still invoke these functions resulting
> in a crash in iommu vendor driver code.
> 
> Hence make sure we check that.
> 
> Fixes: a3a195929d40 ("iommu: Add APIs for multiple domains per device")
> Signed-off-by: Shameer Kolothum <shameerali.kolothum.thodi@huawei.com>
> ---
> v2 --> v3
>  -Removed iommu_ops from struct dev_iommu.
> v1 --> v2:
>  -Added iommu_ops to struct dev_iommu based on the discussion with Robin.
>  -Rebased against iommu-tree core branch.
> ---
>  drivers/iommu/iommu.c | 24 +++++++++++++++---------
>  1 file changed, 15 insertions(+), 9 deletions(-)

Applied, thanks.
_______________________________________________
iommu mailing list
iommu@lists.linux-foundation.org
https://lists.linuxfoundation.org/mailman/listinfo/iommu

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

end of thread, other threads:[~2021-03-18  9:48 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-03-03 17:36 [PATCH v3] iommu: Check dev->iommu in iommu_dev_xxx functions Shameer Kolothum
2021-03-09 12:50 ` Robin Murphy
2021-03-18  9:48 ` Joerg Roedel

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).