Skip to contentSkip to conversation

iommu/amd: Add proper error check in two functions

PATCH Superseded Wan Zongshun 2 participants 2 messages May 10, 2016 – Jun 15, 2016 IOMMU

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 iommu/amd: Add proper error check in two functions

From: Wan Zongshun <[email protected]>

This patch is to do the following:

1. Add error check for caller of iommu_device_create.

2. Add error check for caller of iommu_device_link and move 'iommu = amd_iommu_rlookup_table[dev_data->devid]' out of iommuv2 capability condition that make iommu_device_link also use the 'iommu' to make code more clear and no more than 80 characters.

---
 drivers/iommu/amd_iommu.c      | 12 ++++--------
 drivers/iommu/amd_iommu_init.c |  2 ++
 2 files changed, 6 insertions(+), 8 deletions(-)
drivers/iommu/amd_iommu.c +4 -8
index 547cdd4..232a3b9 100644
@@ -414,6 +414,7 @@ out:
 static int iommu_init_device(struct device *dev)
 {
 	struct iommu_dev_data *dev_data;
+	struct amd_iommu *iommu;
 	int devid;
 
 	if (dev->archdata.iommu)
@@ -427,19 +428,14 @@ static int iommu_init_device(struct device *dev)
 	if (!dev_data)
 		return -ENOMEM;
 
-	if (dev_is_pci(dev) && pci_iommuv2_capable(to_pci_dev(dev))) {
-		struct amd_iommu *iommu;
+	iommu = amd_iommu_rlookup_table[dev_data->devid];
 
-		iommu = amd_iommu_rlookup_table[dev_data->devid];
+	if (dev_is_pci(dev) && pci_iommuv2_capable(to_pci_dev(dev)))
 		dev_data->iommu_v2 = iommu->is_iommu_v2;
-	}
 
 	dev->archdata.iommu = dev_data;
 
-	iommu_device_link(amd_iommu_rlookup_table[dev_data->devid]->iommu_dev,
-			  dev);
-
-	return 0;
+	return iommu_device_link(iommu->iommu_dev, dev);
 }
 
 static void iommu_ignore_device(struct device *dev)
drivers/iommu/amd_iommu_init.c +2 -0
index 9e00341..73fa986 100644
@@ -1526,6 +1526,8 @@ static int iommu_init_pci(struct amd_iommu *iommu)
 	iommu->iommu_dev = iommu_device_create(&iommu->dev->dev, iommu,
 					       amd_iommu_groups, "ivhd%d",
 					       iommu->index);
+	if (IS_ERR(iommu->iommu_dev))
+		return PTR_ERR(iommu->iommu_dev);
 
 	return pci_enable_device(iommu->dev);
 }
Signature · 1 line
1.9.1
maintainer
#1 lore raw
Quoted 40 lines · Wan Zongshun

From: Wan Zongshun <[email protected]>

This patch is to do the following:

1. Add error check for caller of iommu_device_create.

2. Add error check for caller of iommu_device_link and move 'iommu = amd_iommu_rlookup_table[dev_data->devid]' out of iommuv2 capability condition that make iommu_device_link also use the 'iommu' to make code more clear and no more than 80 characters.

---
 drivers/iommu/amd_iommu.c      | 12 ++++--------
 drivers/iommu/amd_iommu_init.c |  2 ++
 2 files changed, 6 insertions(+), 8 deletions(-)
drivers/iommu/amd_iommu.c +3 -3
index 547cdd4..232a3b9 100644
@@ -414,6 +414,7 @@ out:
 static int iommu_init_device(struct device *dev)
 {
 	struct iommu_dev_data *dev_data;
+	struct amd_iommu *iommu;
 	int devid;
 
 	if (dev->archdata.iommu)
@@ -427,19 +428,14 @@ static int iommu_init_device(struct device *dev)
 	if (!dev_data)
 		return -ENOMEM;
 
-	if (dev_is_pci(dev) && pci_iommuv2_capable(to_pci_dev(dev))) {
-		struct amd_iommu *iommu;
+	iommu = amd_iommu_rlookup_table[dev_data->devid];
 
-		iommu = amd_iommu_rlookup_table[dev_data->devid];
+	if (dev_is_pci(dev) && pci_iommuv2_capable(to_pci_dev(dev)))

drivers/iommu/amd_iommu.c static int iommu_init_device(struct device *dev) · line 438

Quoted 10 lines
drivers/iommu/amd_iommu.c +1 -5
 		dev_data->iommu_v2 = iommu->is_iommu_v2;
-	}
 
 	dev->archdata.iommu = dev_data;
 
-	iommu_device_link(amd_iommu_rlookup_table[dev_data->devid]->iommu_dev,
-			  dev);
-
-	return 0;
+	return iommu_device_link(iommu->iommu_dev, dev);

I don't like to fail the whole iommu_init_device() function here, since iommu_device_link only creates sysfs entries. Please just check for an error return code and print a message if one happened. The function should return 0 then.

Joerg