Skip to contentSkip to conversation

iommu: Check dev->iommu in iommu_dev_xxx functions

PATCH Shameerali Kolothum Thodi 2 participants 5 messages Feb 12, 2021 – Feb 16, 2021 IOMMU

lore
Share & tools
lore
#0 lore raw

Hi Robin/Joerg,

Quoted 17 lines

-----Original Message-----
From: Shameer Kolothum [mailto:[email protected]]
Sent: 01 February 2021 12:41
To: [email protected]; [email protected]
Cc: [email protected]; [email protected]; [email protected]; [email protected]; Zengtao (B) <[email protected]>; [email protected]
Subject: [Linuxarm] [PATCH v2] iommu: Check dev->iommu in iommu_dev_xxx functions

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.

Also added iommu_ops to the "struct dev_iommu" and set it if the dev is successfully associated with an iommu.

Quoted 6 lines
Fixes a3a195929d40 ("iommu: Add APIs for multiple domains per device")

---
v1 --> v2:
-Added iommu_ops to struct dev_iommu based on the discussion with Robin. -Rebased against iommu-tree core branch.

A gentle ping on this...

Thanks,
Shameer

Quoted 84 lines
---
 drivers/iommu/iommu.c | 19 +++++++------------
 include/linux/iommu.h |  2 ++
 2 files changed, 9 insertions(+), 12 deletions(-)
drivers/iommu/iommu.c +0 -0
index fd76e2f579fe..6023d0b7c542 100644
@@ -217,6 +217,7 @@ static int __iommu_probe_device(struct device *dev,

struct list_head *group_list

drivers/iommu/iommu.c +3 -4
 	}
 
 	dev->iommu->iommu_dev = iommu_dev;
+	dev->iommu->ops = iommu_dev->ops;
 
 	group = iommu_group_get_for_dev(dev);
 	if (IS_ERR(group)) {
@@ -2865,10 +2866,8 @@ 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 (ops && ops->dev_enable_feat)
-		return ops->dev_enable_feat(dev, feat);
+	if (dev->iommu && dev->iommu->ops->dev_enable_feat)
+		return dev->iommu->ops->dev_enable_feat(dev, feat);
 
 	return -ENODEV;

}

drivers/iommu/iommu.c +2 -4
@@ -2881,10 +2880,8 @@
 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 (ops && ops->dev_disable_feat)
-		return ops->dev_disable_feat(dev, feat);
+	if (dev->iommu && dev->iommu->ops->dev_disable_feat)
+		return dev->iommu->ops->dev_disable_feat(dev, feat);
 
drivers/iommu/iommu.c +2 -4
 	return -EBUSY;
 }
@@ -2892,10 +2889,8 @@
 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 (ops && ops->dev_feat_enabled)
-		return ops->dev_feat_enabled(dev, feat);
+	if (dev->iommu && dev->iommu->ops->dev_feat_enabled)
+		return dev->iommu->ops->dev_feat_enabled(dev, feat);
 
drivers/iommu/iommu.c +0 -0
 	return false;
 }
include/linux/iommu.h +1 -0
index 524ffc2ff64f..ff0c76bdfb67 100644
@@ -354,6 +354,7 @@ struct iommu_fault_param {
  * @fault_param: IOMMU detected device fault reporting data
  * @fwspec:	 IOMMU fwspec data
  * @iommu_dev:	 IOMMU device this device is linked to
+ * @ops:	 iommu-ops for talking to the iommu_dev
  * @priv:	 IOMMU Driver private data
  *
  * TODO: migrate other per device data pointers under iommu_dev_data,

e.g.

include/linux/iommu.h +1 -1
@@ -364,6 +365,7 @@ struct dev_iommu {
 	struct iommu_fault_param	*fault_param;
 	struct iommu_fwspec		*fwspec;
 	struct iommu_device		*iommu_dev;
+	const struct iommu_ops		*ops;
 	void				*priv;
 };
 
--
Quoted 4 lines

2.17.1
_______________________________________________
Linuxarm mailing list -- [email protected]
To unsubscribe send an email to [email protected]

_______________________________________________
iommu mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/iommu

reviewer
#1 lore raw
Quoted 19 lines · Shameerali Kolothum Thodi

Hi Robin/Joerg,

Quoted 17 lines

-----Original Message-----
From: Shameer Kolothum [mailto:[email protected]]
Sent: 01 February 2021 12:41
To: [email protected]; [email protected]
Cc: [email protected]; [email protected]; [email protected]; [email protected]; Zengtao (B) <[email protected]>; [email protected]
Subject: [Linuxarm] [PATCH v2] iommu: Check dev->iommu in iommu_dev_xxx functions

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.

Also added iommu_ops to the "struct dev_iommu" and set it if the dev is successfully associated with an iommu.

Quoted 8 lines
Quoted 6 lines
Fixes a3a195929d40 ("iommu: Add APIs for multiple domains per device")

---
v1 --> v2:
-Added iommu_ops to struct dev_iommu based on the discussion with Robin. -Rebased against iommu-tree core branch.

A gentle ping on this...

Is there a convincing justification for maintaining yet another copy of the ops pointer rather than simply dereferencing iommu_dev->ops at point of use?

Robin.

Quoted 86 lines

Thanks,
Shameer

Quoted 83 lines
---
  drivers/iommu/iommu.c | 19 +++++++------------
  include/linux/iommu.h |  2 ++
  2 files changed, 9 insertions(+), 12 deletions(-)
drivers/iommu/iommu.c +0 -0
index fd76e2f579fe..6023d0b7c542 100644
@@ -217,6 +217,7 @@ static int __iommu_probe_device(struct device *dev,

struct list_head *group_list
}

  	dev->iommu->iommu_dev = iommu_dev;
drivers/iommu/iommu.c +3 -4
+	dev->iommu->ops = iommu_dev->ops;
 
  	group = iommu_group_get_for_dev(dev);
  	if (IS_ERR(group)) {
@@ -2865,10 +2866,8 @@ 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 (ops && ops->dev_enable_feat)
-		return ops->dev_enable_feat(dev, feat);
+	if (dev->iommu && dev->iommu->ops->dev_enable_feat)
+		return dev->iommu->ops->dev_enable_feat(dev, feat);
 
  	return -ENODEV;
  }
drivers/iommu/iommu.c +2 -4
@@ -2881,10 +2880,8 @@
 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 (ops && ops->dev_disable_feat)
-		return ops->dev_disable_feat(dev, feat);
+	if (dev->iommu && dev->iommu->ops->dev_disable_feat)
+		return dev->iommu->ops->dev_disable_feat(dev, feat);
 
  	return -EBUSY;
  }
drivers/iommu/iommu.c +2 -4
@@ -2892,10 +2889,8 @@
 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 (ops && ops->dev_feat_enabled)
-		return ops->dev_feat_enabled(dev, feat);
+	if (dev->iommu && dev->iommu->ops->dev_feat_enabled)
+		return dev->iommu->ops->dev_feat_enabled(dev, feat);
 
  	return false;
  }
include/linux/iommu.h +1 -0
index 524ffc2ff64f..ff0c76bdfb67 100644
@@ -354,6 +354,7 @@ struct iommu_fault_param {
   * @fault_param: IOMMU detected device fault reporting data
   * @fwspec:	 IOMMU fwspec data
   * @iommu_dev:	 IOMMU device this device is linked to
+ * @ops:	 iommu-ops for talking to the iommu_dev
   * @priv:	 IOMMU Driver private data
   *
   * TODO: migrate other per device data pointers under iommu_dev_data,

e.g.

include/linux/iommu.h +1 -0
@@ -364,6 +365,7 @@ struct dev_iommu {
  	struct iommu_fault_param	*fault_param;
  	struct iommu_fwspec		*fwspec;
  	struct iommu_device		*iommu_dev;
+	const struct iommu_ops		*ops;
  	void				*priv;
  };
 
Quoted 5 lines
Quoted 5 lines

--
2.17.1
_______________________________________________
Linuxarm mailing list -- [email protected]
To unsubscribe send an email to [email protected]

_______________________________________________
iommu mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/iommu

#2 lore raw
Quoted 38 lines

-----Original Message-----
From: Robin Murphy [mailto:[email protected]]
Sent: 12 February 2021 16:39
To: Shameerali Kolothum Thodi <[email protected]>; [email protected]; [email protected]
Cc: [email protected]; [email protected]; [email protected]; Zengtao (B) <[email protected]>; [email protected]
Subject: Re: [PATCH v2] iommu: Check dev->iommu in iommu_dev_xxx functions

Quoted 10 lines · Shameerali Kolothum Thodi

Hi Robin/Joerg,

Quoted 8 lines

-----Original Message-----
From: Shameer Kolothum [mailto:[email protected]]
Sent: 01 February 2021 12:41
To: [email protected]; [email protected]
Cc: [email protected]; [email protected]; [email protected]; [email protected]; Zengtao (B) <[email protected]>; [email protected]
Subject: [Linuxarm] [PATCH v2] iommu: Check dev->iommu in

iommu_dev_xxx

Quoted 11 lines
Quoted 11 lines

functions

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.

Also added iommu_ops to the "struct dev_iommu" and set it if the dev is successfully associated with an iommu.

Fixes a3a195929d40 ("iommu: Add APIs for multiple domains per device")
Signed-off-by Shameer Kolothum

<[email protected]>

Quoted 3 lines
Quoted 3 lines

---
v1 --> v2:
-Added iommu_ops to struct dev_iommu based on the discussion with

Quoted 9 lines

Robin.

Quoted 3 lines
Quoted 1 line

-Rebased against iommu-tree core branch.

A gentle ping on this...

Is there a convincing justification for maintaining yet another copy of the ops pointer rather than simply dereferencing iommu_dev->ops at point of use?

TBH, nothing I can think of now. That was mainly the way I interpreted your suggestion from the v1. Now it looks like you didn’t mean it :). I am Ok to rework it to dereference it from iommu_dev. Please let me know.

Thanks,
Shameer

Quoted 91 lines

Robin.

Quoted 8 lines

Thanks,
Shameer

Quoted 5 lines
---
  drivers/iommu/iommu.c | 19 +++++++------------
  include/linux/iommu.h |  2 ++
  2 files changed, 9 insertions(+), 12 deletions(-)
Quoted 5 lines

drivers/iommu/iommu.c static int __iommu_probe_device(struct device

Quoted 5 lines
drivers/iommu/iommu.c +0 -0
index fd76e2f579fe..6023d0b7c542 100644
@@ -217,6 +217,7 @@ static int __iommu_probe_device(struct device

*dev,

Quoted 9 lines

drivers/iommu/iommu.c

Quoted 9 lines

struct list_head *group_list
}

  	dev->iommu->iommu_dev = iommu_dev;
drivers/iommu/iommu.c +1 -0
+	dev->iommu->ops = iommu_dev->ops;
 
  	group = iommu_group_get_for_dev(dev);
  	if (IS_ERR(group)) {
@@ -2865,10 +2866,8 @@

EXPORT_SYMBOL_GPL(iommu_fwspec_add_ids);

Quoted 41 lines
Quoted 41 lines
drivers/iommu/iommu.c +2 -4
   */
  int iommu_dev_enable_feature(struct device *dev, enum
 iommu_dev_features feat)
  {
-	const struct iommu_ops *ops = dev->bus->iommu_ops;
-
-	if (ops && ops->dev_enable_feat)
-		return ops->dev_enable_feat(dev, feat);
+	if (dev->iommu && dev->iommu->ops->dev_enable_feat)
+		return dev->iommu->ops->dev_enable_feat(dev, feat);
 
  	return -ENODEV;
  }
drivers/iommu/iommu.c +2 -4
@@ -2881,10 +2880,8 @@
 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 (ops && ops->dev_disable_feat)
-		return ops->dev_disable_feat(dev, feat);
+	if (dev->iommu && dev->iommu->ops->dev_disable_feat)
+		return dev->iommu->ops->dev_disable_feat(dev, feat);
 
  	return -EBUSY;
  }
drivers/iommu/iommu.c +2 -4
@@ -2892,10 +2889,8 @@
 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 (ops && ops->dev_feat_enabled)
-		return ops->dev_feat_enabled(dev, feat);
+	if (dev->iommu && dev->iommu->ops->dev_feat_enabled)
+		return dev->iommu->ops->dev_feat_enabled(dev, feat);
 
Quoted 14 lines
Quoted 7 lines
  	return false;
  }
include/linux/iommu.h +0 -0
index 524ffc2ff64f..ff0c76bdfb67 100644

include/linux/iommu.h struct iommu_fault_param { · line 360

Quoted 7 lines
include/linux/iommu.h +1 -0
@@ -354,6 +354,7 @@ struct iommu_fault_param {
   * @fault_param: IOMMU detected device fault reporting data
   * @fwspec:	 IOMMU fwspec data
   * @iommu_dev:	 IOMMU device this device is linked to
+ * @ops:	 iommu-ops for talking to the iommu_dev
   * @priv:	 IOMMU Driver private data
   *
   * TODO: migrate other per device data pointers under

iommu_dev_data,

Quoted 9 lines
Quoted 9 lines

e.g.

include/linux/iommu.h +1 -0
@@ -364,6 +365,7 @@ struct dev_iommu {
  	struct iommu_fault_param	*fault_param;
  	struct iommu_fwspec		*fwspec;
  	struct iommu_device		*iommu_dev;
+	const struct iommu_ops		*ops;
  	void				*priv;
  };
 
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines

--
2.17.1
_______________________________________________
Linuxarm mailing list -- [email protected]
To unsubscribe send an email to [email protected]

_______________________________________________
iommu mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/iommu

#3 lore raw
In reply to Robin Murphy
Quoted 62 lines

-----Original Message-----
From: Shameerali Kolothum Thodi
Sent: 12 February 2021 16:45
To: 'Robin Murphy' <[email protected]>; [email protected]; [email protected]
Cc: [email protected]; [email protected]; [email protected]; Zengtao (B) <[email protected]>; [email protected]
Subject: RE: [PATCH v2] iommu: Check dev->iommu in iommu_dev_xxx functions

Quoted 8 lines

-----Original Message-----
From: Robin Murphy [mailto:[email protected]]
Sent: 12 February 2021 16:39
To: Shameerali Kolothum Thodi <[email protected]>; [email protected]; [email protected]
Cc: [email protected]; [email protected]; [email protected]; Zengtao (B) <[email protected]>; [email protected]
Subject: Re: [PATCH v2] iommu: Check dev->iommu in iommu_dev_xxx

functions

Quoted 6 lines · Shameerali Kolothum Thodi
Quoted 4 lines

Hi Robin/Joerg,

Quoted 2 lines

-----Original Message-----
From: Shameer Kolothum

[mailto:[email protected]]

Quoted 6 lines
Quoted 6 lines
Quoted 6 lines

Sent: 01 February 2021 12:41
To: [email protected]; [email protected]
Cc: [email protected]; [email protected]; [email protected]; [email protected]; Zengtao (B) <[email protected]>; [email protected]
Subject: [Linuxarm] [PATCH v2] iommu: Check dev->iommu in

Quoted 11 lines

iommu_dev_xxx

Quoted 10 lines
Quoted 10 lines

functions

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.

Also added iommu_ops to the "struct dev_iommu" and set it if the dev is successfully associated with an iommu.

Fixes a3a195929d40 ("iommu: Add APIs for multiple domains per

device")

Quoted 5 lines
Quoted 1 line
Quoted 1 line
Signed-off-by Shameer Kolothum

<[email protected]>

Quoted 3 lines
Quoted 3 lines

---
v1 --> v2:
-Added iommu_ops to struct dev_iommu based on the discussion with

Quoted 9 lines

Robin.

Quoted 3 lines
Quoted 1 line

-Rebased against iommu-tree core branch.

A gentle ping on this...

Is there a convincing justification for maintaining yet another copy of the ops pointer rather than simply dereferencing iommu_dev->ops at point of use?

Quoted 5 lines

TBH, nothing I can think of now. That was mainly the way I interpreted your suggestion
from the v1. Now it looks like you didn’t mean it :). I am Ok to rework it to dereference
it from iommu_dev. Please let me know.

So we can do something like this,

index fd76e2f579fe..5fd31a3cec18 100644

drivers/iommu/iommu.c +5 -3
@@ -2865,10 +2865,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 && dev->iommu->iommu_dev->ops)
+               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;
 }

Again, not sure we need to do the checking for iommu->dev and ops here. If the dev->iommu is set, is it safe to assume that we have a valid iommu->iommu_dev and ops always? (May be it is safer to do the checking in case something else breaks this assumption in future). Please let me know your thoughts.

Thanks,
Shameer

_______________________________________________
iommu mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/iommu

reviewer
#4 lore raw
Quoted 90 lines · Shameerali Kolothum Thodi
Quoted 62 lines

-----Original Message-----
From: Shameerali Kolothum Thodi
Sent: 12 February 2021 16:45
To: 'Robin Murphy' <[email protected]>; [email protected]; [email protected]
Cc: [email protected]; [email protected]; [email protected]; Zengtao (B) <[email protected]>; [email protected]
Subject: RE: [PATCH v2] iommu: Check dev->iommu in iommu_dev_xxx functions

Quoted 8 lines

-----Original Message-----
From: Robin Murphy [mailto:[email protected]]
Sent: 12 February 2021 16:39
To: Shameerali Kolothum Thodi <[email protected]>; [email protected]; [email protected]
Cc: [email protected]; [email protected]; [email protected]; Zengtao (B) <[email protected]>; [email protected]
Subject: Re: [PATCH v2] iommu: Check dev->iommu in iommu_dev_xxx

functions

Quoted 6 lines · Shameerali Kolothum Thodi
Quoted 4 lines

Hi Robin/Joerg,

Quoted 2 lines

-----Original Message-----
From: Shameer Kolothum

[mailto:[email protected]]

Quoted 6 lines
Quoted 6 lines
Quoted 6 lines

Sent: 01 February 2021 12:41
To: [email protected]; [email protected]
Cc: [email protected]; [email protected]; [email protected]; [email protected]; Zengtao (B) <[email protected]>; [email protected]
Subject: [Linuxarm] [PATCH v2] iommu: Check dev->iommu in

Quoted 11 lines

iommu_dev_xxx

Quoted 10 lines
Quoted 10 lines

functions

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.

Also added iommu_ops to the "struct dev_iommu" and set it if the dev is successfully associated with an iommu.

Fixes a3a195929d40 ("iommu: Add APIs for multiple domains per

device")

Quoted 5 lines
Quoted 1 line
Quoted 1 line
Signed-off-by Shameer Kolothum

<[email protected]>

Quoted 3 lines
Quoted 3 lines

---
v1 --> v2:
-Added iommu_ops to struct dev_iommu based on the discussion with

Quoted 9 lines

Robin.

Quoted 3 lines
Quoted 1 line

-Rebased against iommu-tree core branch.

A gentle ping on this...

Is there a convincing justification for maintaining yet another copy of the ops pointer rather than simply dereferencing iommu_dev->ops at point of use?

Quoted 5 lines

TBH, nothing I can think of now. That was mainly the way I interpreted your suggestion
from the v1. Now it looks like you didn’t mean it :). I am Ok to rework it to dereference
it from iommu_dev. Please let me know.

So we can do something like this,

index fd76e2f579fe..5fd31a3cec18 100644

drivers/iommu/iommu.c +5 -3
@@ -2865,10 +2865,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 && dev->iommu->iommu_dev->ops)
+               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;
  }
Quoted 4 lines

Again, not sure we need to do the checking for iommu->dev and ops here. If the dev->iommu is set, is it safe to assume that we have a valid iommu->iommu_dev and ops always? (May be it is safer to do the checking in case something else breaks this assumption in future). Please let me know your thoughts.

I think it *could* happen that dev->iommu is set by iommu_fwspec_init() but iommu_probe_device() later refuses the device for whatever reason, so we would still need to check iommu->iommu_dev to be completely safe. We can assume iommu_dev->ops is valid, since if the IOMMU driver has returned something bogus from .probe_device then it's a major bug in that driver and crashing is the best indicator :)

Robin.

Quoted 10 lines

Thanks,
Shameer

_______________________________________________
iommu mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/iommu

_______________________________________________
iommu mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/iommu