Skip to contentSkip to conversation

iommu: Fix refcount leak in iommu_device_claim_dma_owner

PATCH In review Miaoqian Lin 3 participants 3 messages Dec 30, 2022 – Jan 13, 2023 IOMMU

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 iommu: Fix refcount leak in iommu_device_claim_dma_owner

iommu_group_get() returns the group with the reference incremented. Move iommu_group_get() after owner check to fix the refcount leak.

Fixes 89395ccedbc1 ("iommu: Add device-centric DMA ownership interfaces")
---
 drivers/iommu/iommu.c | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)
drivers/iommu/iommu.c +5 -3
index de91dd88705b..3a7dd8b61fab 100644
@@ -3185,14 +3185,16 @@ EXPORT_SYMBOL_GPL(iommu_group_claim_dma_owner);
  */
 int iommu_device_claim_dma_owner(struct device *dev, void *owner)
 {
-	struct iommu_group *group = iommu_group_get(dev);
+	struct iommu_group *group = NULL;
 	int ret = 0;
 
-	if (!group)
-		return -ENODEV;
 	if (WARN_ON(!owner))
 		return -EINVAL;
 
+	group = iommu_group_get(dev);
+	if (!group)
+		return -ENODEV;
+
 	mutex_lock(&group->mutex);
 	if (group->owner_cnt) {
 		if (group->owner != owner) {
Signature · 1 line
2.25.1
Reviewed-by on v1 1/1
#1 lore raw
Quoted 2 lines · Miaoqian Lin

iommu_group_get() returns the group with the reference incremented. Move iommu_group_get() after owner check to fix the refcount leak.

An empty @owner is a more serious problem than refcount leak. It should not happen, so a WARN_ON() was added there.

Anyway, your change makes the code better.

Quoted 12 lines
Fixes 89395ccedbc1 ("iommu: Add device-centric DMA ownership interfaces")
---
  drivers/iommu/iommu.c | 8 +++++---
  1 file changed, 5 insertions(+), 3 deletions(-)
drivers/iommu/iommu.c +0 -0
index de91dd88705b..3a7dd8b61fab 100644

drivers/iommu/iommu.c EXPORT_SYMBOL_GPL(iommu_group_claim_dma_owner); · line 3188

Quoted 5 lines
drivers/iommu/iommu.c +1 -1
@@ -3185,14 +3185,16 @@ EXPORT_SYMBOL_GPL(iommu_group_claim_dma_owner);
   */
  int iommu_device_claim_dma_owner(struct device *dev, void *owner)
  {
-	struct iommu_group *group = iommu_group_get(dev);
+	struct iommu_group *group = NULL;

No need to set NULL to group.

Quoted 4 lines
drivers/iommu/iommu.c +0 -2
  	int ret = 0;
 
-	if (!group)
-		return -ENODEV;

drivers/iommu/iommu.c EXPORT_SYMBOL_GPL(iommu_group_claim_dma_owner); · line 3200

Quoted 10 lines
drivers/iommu/iommu.c +4 -0
  	if (WARN_ON(!owner))
  		return -EINVAL;
 
+	group = iommu_group_get(dev);
+	if (!group)
+		return -ENODEV;
+
  	mutex_lock(&group->mutex);
  	if (group->owner_cnt) {
  		if (group->owner != owner) {

Others look good to me.

--
Best regards,
baolu

maintainer
#2 lore raw
In reply to Miaoqian Lin
Quoted 8 lines · Miaoqian Lin

iommu_group_get() returns the group with the reference incremented. Move iommu_group_get() after owner check to fix the refcount leak.

Fixes 89395ccedbc1 ("iommu: Add device-centric DMA ownership interfaces")
---
 drivers/iommu/iommu.c | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)

Applied for 6.2, thanks.