Skip to contentSkip to conversation

iommu: move the adding group info to proper place

PATCH In review Li RongQing 4 participants 7 messages Jan 7, 2025 – Feb 11, 2025 IOMMU

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 iommu: move the adding group info to proper place

After commit fa0828036488 ("iommu: Split iommu_group_add_device()"), the adding group information is left in iommu_group_alloc_device, but which only allocate group device, not attach the device to iommu group;

so move the print to iommu probing and adding functions

---
Diff with v1: add log when iommu_group_alloc_device failed in iommu_group_add_device

 drivers/iommu/iommu.c | 21 ++++++++++++++-------
 1 file changed, 14 insertions(+), 7 deletions(-)
drivers/iommu/iommu.c +14 -7
index 599030e..2eaa8ab 100644
@@ -586,6 +586,9 @@ static int __iommu_probe_device(struct device *dev, struct list_head *group_list
 
 	mutex_unlock(&group->mutex);
 
+	trace_add_device_to_group(group->id, dev);
+	dev_info(dev, "Adding to iommu group %d\n", group->id);
+
 	return 0;
 
 err_remove_gdev:
@@ -594,6 +597,7 @@ static int __iommu_probe_device(struct device *dev, struct list_head *group_list
 err_put_group:
 	iommu_deinit_device(dev);
 	mutex_unlock(&group->mutex);
+	dev_err(dev, "Failed to add to iommu group %d: %d\n", group->id, ret);
 	iommu_group_put(group);
 
 	return ret;
@@ -1192,10 +1196,6 @@ static struct group_device *iommu_group_alloc_device(struct iommu_group *group,
 		goto err_free_name;
 	}
 
-	trace_add_device_to_group(group->id, dev);
-
-	dev_info(dev, "Adding to iommu group %d\n", group->id);
-
 	return device;
 
 err_free_name:
@@ -1204,7 +1204,6 @@ static struct group_device *iommu_group_alloc_device(struct iommu_group *group,
 	sysfs_remove_link(&dev->kobj, "iommu_group");
 err_free_device:
 	kfree(device);
-	dev_err(dev, "Failed to add to iommu group %d: %d\n", group->id, ret);
 	return ERR_PTR(ret);
 }
 
@@ -1219,10 +1218,14 @@ static struct group_device *iommu_group_alloc_device(struct iommu_group *group,
 int iommu_group_add_device(struct iommu_group *group, struct device *dev)
 {
 	struct group_device *gdev;
+	int ret;
 
 	gdev = iommu_group_alloc_device(group, dev);
-	if (IS_ERR(gdev))
-		return PTR_ERR(gdev);
+	if (IS_ERR(gdev)) {
+		ret = PTR_ERR(gdev);
+		dev_err(dev, "Failed to add to iommu group %d: %d\n", group->id, ret);
+		return ret;
+	}
 
 	iommu_group_ref_get(group);
 	dev->iommu_group = group;
@@ -1230,6 +1233,10 @@ int iommu_group_add_device(struct iommu_group *group, struct device *dev)
 	mutex_lock(&group->mutex);
 	list_add_tail(&gdev->list, &group->devices);
 	mutex_unlock(&group->mutex);
+
+	trace_add_device_to_group(group->id, dev);
+	dev_info(dev, "Adding to iommu group %d\n", group->id);
+
 	return 0;
 }
 EXPORT_SYMBOL_GPL(iommu_group_add_device);
Signature · 1 line
2.9.4
Acked-by on v2 1/1
#1 lore raw
Quoted 8 lines · Li RongQing

After commit fa0828036488 ("iommu: Split iommu_group_add_device()"), the adding group information is left in iommu_group_alloc_device, but which only allocate group device, not attach the device to iommu group;

so move the print to iommu probing and adding functions

LGTM, followed through since [1].

Thanks,
Praan

[1]
https://lore.kernel.org/all/[email protected]/

#2 lore raw
In reply to Li RongQing

Hi Li,

Quoted 58 lines · Li RongQing

After commit fa0828036488 ("iommu: Split iommu_group_add_device()"), the adding group information is left in iommu_group_alloc_device, but which only allocate group device, not attach the device to iommu group;

so move the print to iommu probing and adding functions

---
Diff with v1: add log when iommu_group_alloc_device failed in iommu_group_add_device

 drivers/iommu/iommu.c | 21 ++++++++++++++-------
 1 file changed, 14 insertions(+), 7 deletions(-)
drivers/iommu/iommu.c +4 -5
index 599030e..2eaa8ab 100644
@@ -586,6 +586,9 @@ static int __iommu_probe_device(struct device *dev, struct list_head *group_list
 
 	mutex_unlock(&group->mutex);
 
+	trace_add_device_to_group(group->id, dev);
+	dev_info(dev, "Adding to iommu group %d\n", group->id);
+
 	return 0;
 
 err_remove_gdev:
@@ -594,6 +597,7 @@ static int __iommu_probe_device(struct device *dev, struct list_head *group_list
 err_put_group:
 	iommu_deinit_device(dev);
 	mutex_unlock(&group->mutex);
+	dev_err(dev, "Failed to add to iommu group %d: %d\n", group->id, ret);
 	iommu_group_put(group);
 
 	return ret;
@@ -1192,10 +1196,6 @@ static struct group_device *iommu_group_alloc_device(struct iommu_group *group,
 		goto err_free_name;
 	}
 
-	trace_add_device_to_group(group->id, dev);
-
-	dev_info(dev, "Adding to iommu group %d\n", group->id);
-
 	return device;
 
 err_free_name:
@@ -1204,7 +1204,6 @@ static struct group_device *iommu_group_alloc_device(struct iommu_group *group,
 	sysfs_remove_link(&dev->kobj, "iommu_group");
 err_free_device:
 	kfree(device);
-	dev_err(dev, "Failed to add to iommu group %d: %d\n", group->id, ret);
 	return ERR_PTR(ret);
 }
 
@@ -1219,10 +1218,14 @@ static struct group_device *iommu_group_alloc_device(struct iommu_group *group,
 int iommu_group_add_device(struct iommu_group *group, struct device *dev)
 {

drivers/iommu/iommu.c static struct group_device *iommu_group_alloc_device(struct iommu_group *group, · line 1227

Quoted 10 lines
drivers/iommu/iommu.c +5 -2
 	struct group_device *gdev;
+	int ret;
 
 	gdev = iommu_group_alloc_device(group, dev);
-	if (IS_ERR(gdev))
-		return PTR_ERR(gdev);
+	if (IS_ERR(gdev)) {
+		ret = PTR_ERR(gdev);
+		dev_err(dev, "Failed to add to iommu group %d: %d\n", group->id, ret);
+		return ret;

Why not just return PTR_ERR(gdev)?

-Vasant

#3 lore raw
Quoted 10 lines

drivers/iommu/iommu.c static struct group_device *iommu_group_alloc_device(struct iommu_group *group,

Quoted 5 lines
drivers/iommu/iommu.c +3 -2
-	if (IS_ERR(gdev))
-		return PTR_ERR(gdev);
+	if (IS_ERR(gdev)) {
+		ret = PTR_ERR(gdev);
+		dev_err(dev, "Failed to add to iommu group %d: %d\n", group->id,

ret);

drivers/iommu/iommu.c static struct group_device *iommu_group_alloc_device(struct iommu_group *group, · line 1227

Quoted 1 line
drivers/iommu/iommu.c +1 -0
+		return ret;

Why not just return PTR_ERR(gdev)?

Since dev_err will print PTR_ERR(gdev), so I define a local variable to conversion once

Thanks

-Li

Quoted 1 line

-Vasant

Reviewed-by
#4 lore raw

Hi Li,

Quoted 6 lines · Li,Rongqing
Quoted 6 lines

drivers/iommu/iommu.c static struct group_device *iommu_group_alloc_device(struct iommu_group *group,

Quoted 5 lines
drivers/iommu/iommu.c +3 -2
-	if (IS_ERR(gdev))
-		return PTR_ERR(gdev);
+	if (IS_ERR(gdev)) {
+		ret = PTR_ERR(gdev);
+		dev_err(dev, "Failed to add to iommu group %d: %d\n", group->id,

ret);

Quoted 6 lines
Quoted 4 lines

drivers/iommu/iommu.c static struct group_device *iommu_group_alloc_device(struct iommu_group *group, · line 1227

Quoted 1 line
drivers/iommu/iommu.c +1 -0
+		return ret;

Why not just return PTR_ERR(gdev)?

Since dev_err will print PTR_ERR(gdev), so I define a local variable to conversion once

Ok. Thanks

-Vasant

#5 lore raw
In reply to Li RongQing
Quoted 9 lines

After commit fa0828036488 ("iommu: Split iommu_group_add_device()"), the adding group information is left in iommu_group_alloc_device, but which only allocate group device, not attach the device to iommu group;

so move the print to iommu probing and adding functions

Ping

Thanks

-LiRongQing

Quoted 84 lines

---
Diff with v1: add log when iommu_group_alloc_device failed in iommu_group_add_device

 drivers/iommu/iommu.c | 21 ++++++++++++++-------
 1 file changed, 14 insertions(+), 7 deletions(-)
drivers/iommu/iommu.c index renamed from drivers/iommu/iommu.c +0 -0

599030e..2eaa8ab 100644

drivers/iommu/iommu.c +0 -0
@@ -586,6 +586,9 @@ static int __iommu_probe_device(struct device *dev,

struct list_head *group_list

drivers/iommu/iommu.c +3 -0
 	mutex_unlock(&group->mutex);
 
+	trace_add_device_to_group(group->id, dev);
+	dev_info(dev, "Adding to iommu group %d\n", group->id);
+
 	return 0;
 
 err_remove_gdev:
@@ -594,6 +597,7 @@ static int __iommu_probe_device(struct device *dev,

struct list_head *group_list
err_put_group:

drivers/iommu/iommu.c +1 -4
 	iommu_deinit_device(dev);
 	mutex_unlock(&group->mutex);
+	dev_err(dev, "Failed to add to iommu group %d: %d\n", group->id, ret);
 	iommu_group_put(group);
 
 	return ret;
@@ -1192,10 +1196,6 @@ static struct group_device
 *iommu_group_alloc_device(struct iommu_group *group,
 		goto err_free_name;
 	}
 
-	trace_add_device_to_group(group->id, dev);
-
-	dev_info(dev, "Adding to iommu group %d\n", group->id);
-
 	return device;
 

err_free_name:

drivers/iommu/iommu.c +6 -3
@@ -1204,7 +1204,6 @@ static struct group_device
 *iommu_group_alloc_device(struct iommu_group *group,
 	sysfs_remove_link(&dev->kobj, "iommu_group");
 err_free_device:
 	kfree(device);
-	dev_err(dev, "Failed to add to iommu group %d: %d\n", group->id, ret);
 	return ERR_PTR(ret);
 }
@@ -1219,10 +1218,14 @@ static struct group_device
 *iommu_group_alloc_device(struct iommu_group *group,  int
 iommu_group_add_device(struct iommu_group *group, struct device *dev)  {
 	struct group_device *gdev;
+	int ret;
 
 	gdev = iommu_group_alloc_device(group, dev);
-	if (IS_ERR(gdev))
-		return PTR_ERR(gdev);
+	if (IS_ERR(gdev)) {
+		ret = PTR_ERR(gdev);
+		dev_err(dev, "Failed to add to iommu group %d: %d\n", group->id,
 ret);
+		return ret;
+	}
 
 	iommu_group_ref_get(group);
drivers/iommu/iommu.c +4 -0
 	dev->iommu_group = group;
@@ -1230,6 +1233,10 @@ int iommu_group_add_device(struct
 iommu_group *group, struct device *dev)
 	mutex_lock(&group->mutex);
 	list_add_tail(&gdev->list, &group->devices);
 	mutex_unlock(&group->mutex);
+
+	trace_add_device_to_group(group->id, dev);
+	dev_info(dev, "Adding to iommu group %d\n", group->id);
+
 	return 0;
 }

EXPORT_SYMBOL_GPL(iommu_group_add_device);
--
2.9.4

reviewer
#6 lore raw
In reply to Li RongQing
Quoted 5 lines · Li RongQing

After commit fa0828036488 ("iommu: Split iommu_group_add_device()"), the adding group information is left in iommu_group_alloc_device, but which only allocate group device, not attach the device to iommu group;

so move the print to iommu probing and adding functions

But why? In what way is it justifiably better to have duplicate copies of the same code when the fact is that the only parts of the iommu_group_add_device() operation which can fail *are* the ones inside iommu_group_alloc_device()?

Quoted 13 lines

---
Diff with v1: add log when iommu_group_alloc_device failed in iommu_group_add_device

  drivers/iommu/iommu.c | 21 ++++++++++++++-------
  1 file changed, 14 insertions(+), 7 deletions(-)
drivers/iommu/iommu.c +0 -0
index 599030e..2eaa8ab 100644

drivers/iommu/iommu.c static int __iommu_probe_device(struct device *dev, struct list_head *group_list · line 590

Quoted 5 lines
drivers/iommu/iommu.c +2 -0
@@ -586,6 +586,9 @@ static int __iommu_probe_device(struct device *dev, struct list_head *group_list
 
  	mutex_unlock(&group->mutex);
 
+	trace_add_device_to_group(group->id, dev);
+	dev_info(dev, "Adding to iommu group %d\n", group->id);

If you want to argue semantics, this is now just as much in the wrong place as you claim the current error message to be: by this point we are no longer, as implied, about to add the device to the group, we've already finished doing that and potentially various other subsequent things too.

drivers/iommu/iommu.c static int __iommu_probe_device(struct device *dev, struct list_head *group_list · line 600

Quoted 9 lines
drivers/iommu/iommu.c +2 -0
+
  	return 0;
 
  err_remove_gdev:
@@ -594,6 +597,7 @@ static int __iommu_probe_device(struct device *dev, struct list_head *group_list
  err_put_group:
  	iommu_deinit_device(dev);
  	mutex_unlock(&group->mutex);
+	dev_err(dev, "Failed to add to iommu group %d: %d\n", group->id, ret);

This is inaccurate, because we can also get here for further error reasons after the device technically *was* successfully added to the group (in terms of the nominal iommu_group_add_device() operation within the wider "probe device" functionality here).

Quoted 40 lines
drivers/iommu/iommu.c +6 -7
  	iommu_group_put(group);
 
  	return ret;
@@ -1192,10 +1196,6 @@ static struct group_device *iommu_group_alloc_device(struct iommu_group *group,
  		goto err_free_name;
  	}
 
-	trace_add_device_to_group(group->id, dev);
-
-	dev_info(dev, "Adding to iommu group %d\n", group->id);
-
  	return device;
 
  err_free_name:
@@ -1204,7 +1204,6 @@ static struct group_device *iommu_group_alloc_device(struct iommu_group *group,
  	sysfs_remove_link(&dev->kobj, "iommu_group");
  err_free_device:
  	kfree(device);
-	dev_err(dev, "Failed to add to iommu group %d: %d\n", group->id, ret);
  	return ERR_PTR(ret);
  }
 
@@ -1219,10 +1218,14 @@ static struct group_device *iommu_group_alloc_device(struct iommu_group *group,
  int iommu_group_add_device(struct iommu_group *group, struct device *dev)
  {
  	struct group_device *gdev;
+	int ret;
 
  	gdev = iommu_group_alloc_device(group, dev);
-	if (IS_ERR(gdev))
-		return PTR_ERR(gdev);
+	if (IS_ERR(gdev)) {
+		ret = PTR_ERR(gdev);
+		dev_err(dev, "Failed to add to iommu group %d: %d\n", group->id, ret);
+		return ret;
+	}
 
  	iommu_group_ref_get(group);
  	dev->iommu_group = group;

drivers/iommu/iommu.c int iommu_group_add_device(struct iommu_group *group, struct device *dev) · line 1238

Quoted 6 lines
drivers/iommu/iommu.c +3 -0
@@ -1230,6 +1233,10 @@ int iommu_group_add_device(struct iommu_group *group, struct device *dev)
  	mutex_lock(&group->mutex);
  	list_add_tail(&gdev->list, &group->devices);
  	mutex_unlock(&group->mutex);
+
+	trace_add_device_to_group(group->id, dev);
+	dev_info(dev, "Adding to iommu group %d\n", group->id);

Again, this one is now semantically wrong too.

Thanks,
Robin.

Quoted 4 lines
drivers/iommu/iommu.c +1 -0
+
  	return 0;
  }
  EXPORT_SYMBOL_GPL(iommu_group_add_device);