Skip to contentSkip to conversation

iommu/dma: Fix missing mutex_init() in iommu_get_msi_cookie()

PATCH In review Yang Yingliang 3 participants 5 messages Jun 27, 2022 – Aug 1, 2022

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 iommu/dma: Fix missing mutex_init() in iommu_get_msi_cookie()

cookie_alloc() is called by iommu_get_dma_cookie() and iommu_get_msi_cookie(), but the mutex is only initialized in iommu_get_dma_cookie(), move mutex_init() into cookie_alloc() to make sure the mutex will be initialized.

Fixes ac9a5d522bb8 ("iommu/dma: Fix race condition during iova_domain initialization")
---
 drivers/iommu/dma-iommu.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/iommu/dma-iommu.c +1 -1
index 1910f4f1612b..e29157380c48 100644
@@ -294,6 +294,7 @@ static struct iommu_dma_cookie *cookie_alloc(enum iommu_dma_cookie_type type)
 	if (cookie) {
 		INIT_LIST_HEAD(&cookie->msi_page_list);
 		cookie->type = type;
+		mutex_init(&cookie->mutex);
 	}
 	return cookie;
 }
@@ -311,7 +312,6 @@ int iommu_get_dma_cookie(struct iommu_domain *domain)
 	if (!domain->iova_cookie)
 		return -ENOMEM;
 
-	mutex_init(&domain->iova_cookie->mutex);
 	return 0;
 }
 
Signature · 1 line
2.25.1
maintainer
#1 lore raw

Adding Robin.

Quoted 33 lines · Yang Yingliang

cookie_alloc() is called by iommu_get_dma_cookie() and iommu_get_msi_cookie(), but the mutex is only initialized in iommu_get_dma_cookie(), move mutex_init() into cookie_alloc() to make sure the mutex will be initialized.

Fixes ac9a5d522bb8 ("iommu/dma: Fix race condition during iova_domain initialization")
---
 drivers/iommu/dma-iommu.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/iommu/dma-iommu.c +1 -1
index 1910f4f1612b..e29157380c48 100644
@@ -294,6 +294,7 @@ static struct iommu_dma_cookie *cookie_alloc(enum iommu_dma_cookie_type type)
 	if (cookie) {
 		INIT_LIST_HEAD(&cookie->msi_page_list);
 		cookie->type = type;
+		mutex_init(&cookie->mutex);
 	}
 	return cookie;
 }
@@ -311,7 +312,6 @@ int iommu_get_dma_cookie(struct iommu_domain *domain)
 	if (!domain->iova_cookie)
 		return -ENOMEM;
 
-	mutex_init(&domain->iova_cookie->mutex);
 	return 0;
 }
 
Signature · 1 line
2.25.1
maintainer
#2 lore raw
Quoted 6 lines · Joerg Roedel

Adding Robin.

Quoted 3 lines · Yang Yingliang

cookie_alloc() is called by iommu_get_dma_cookie() and iommu_get_msi_cookie(), but the mutex is only initialized in iommu_get_dma_cookie(), move mutex_init() into cookie_alloc() to make sure the mutex will be initialized.

The mutex is only used in iommu_dma_init_domain(), which is only called by iommu_setup_dma_ops() for IOMMU_DOMAIN_DMA domains. How is there a problem here?

Robin.

Quoted 29 lines
Quoted 29 lines
Fixes ac9a5d522bb8 ("iommu/dma: Fix race condition during iova_domain initialization")
---
  drivers/iommu/dma-iommu.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
drivers/iommu/dma-iommu.c +1 -1
index 1910f4f1612b..e29157380c48 100644
@@ -294,6 +294,7 @@ static struct iommu_dma_cookie *cookie_alloc(enum iommu_dma_cookie_type type)
  	if (cookie) {
  		INIT_LIST_HEAD(&cookie->msi_page_list);
  		cookie->type = type;
+		mutex_init(&cookie->mutex);
  	}
  	return cookie;
  }
@@ -311,7 +312,6 @@ int iommu_get_dma_cookie(struct iommu_domain *domain)
  	if (!domain->iova_cookie)
  		return -ENOMEM;
 
-	mutex_init(&domain->iova_cookie->mutex);
  	return 0;
  }
 
Signature · 1 line
2.25.1
#3 lore raw

Hi,

Quoted 9 lines · Robin Murphy
Quoted 6 lines · Joerg Roedel

Adding Robin.

Quoted 3 lines · Yang Yingliang

cookie_alloc() is called by iommu_get_dma_cookie() and iommu_get_msi_cookie(), but the mutex is only initialized in iommu_get_dma_cookie(), move mutex_init() into cookie_alloc() to make sure the mutex will be initialized.

The mutex is only used in iommu_dma_init_domain(), which is only called by iommu_setup_dma_ops() for IOMMU_DOMAIN_DMA domains. How is there a problem here?

It's no problem now, but I thinks it's better to initialize the 'mutex' in cookie_alloc() to make code stronger.

Thanks,
Yang

Quoted 33 lines

Robin.

Quoted 21 lines
Quoted 21 lines
Fixes ac9a5d522bb8 ("iommu/dma: Fix race condition during iova_domain initialization")

---
  drivers/iommu/dma-iommu.c | 2 +-

  1 file changed, 1 insertion(+), 1 deletion(-)
drivers/iommu/dma-iommu.c +1 -0
index 1910f4f1612b..e29157380c48 100644
@@ -294,6 +294,7 @@ static struct iommu_dma_cookie 
 *cookie_alloc(enum iommu_dma_cookie_type type)
       if (cookie) {
           INIT_LIST_HEAD(&cookie->msi_page_list);
           cookie->type = type;
+        mutex_init(&cookie->mutex);
       }
       return cookie;

  }

drivers/iommu/dma-iommu.c +0 -0
@@ -311,7 +312,6 @@ int iommu_get_dma_cookie(struct iommu_domain 
Quoted 8 lines
Quoted 8 lines
drivers/iommu/dma-iommu.c +0 -0
 *domain)
       if (!domain->iova_cookie)
           return -ENOMEM;
   -    mutex_init(&domain->iova_cookie->mutex);
       return 0;
   }

  --
2.25.1

.

maintainer
#4 lore raw
Quoted 6 lines · Yang Yingliang

Hi,

Quoted 3 lines · Robin Murphy
Quoted 2 lines · Joerg Roedel

Adding Robin.

Quoted 7 lines
Quoted 6 lines
Quoted 4 lines · Yang Yingliang
Quoted 3 lines

cookie_alloc() is called by iommu_get_dma_cookie() and iommu_get_msi_cookie(), but the mutex is only initialized in iommu_get_dma_cookie(), move mutex_init() into cookie_alloc() to make sure the mutex will be initialized.

The mutex is only used in iommu_dma_init_domain(), which is only called by iommu_setup_dma_ops() for IOMMU_DOMAIN_DMA domains. How is there a problem here?

It's no problem now, but I thinks it's better to initialize the 'mutex' in cookie_alloc() to make code stronger.

Stronger against what, though? The sole reason this mutex exists at all is as a temporary measure to protect the IOVA domain from concurrent initialisation - I suppose in hindsight it might have made sense to define it inside the union, but I don't see much point in churning that now. I'd rather spend the time on continuing to get the iommu_probe_device() path sorted out so that we don't have the whole problematic driver-probe-time-replay mess in the first place and this mutex can be reverted ASAP.

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Fixes ac9a5d522bb8 ("iommu/dma: Fix race condition during iova_domain initialization")

Please teach your robot to care about things that actually matter. All it's "reporting" here is that it isn't clever enough to follow control flow across multiple compilation units, otherwise it should have seen the matching iommu_is_dma_domain() checks between __iommu_domain_alloc() and iommu_setup_dma_ops(). Having to explain this is not a good use of the kernel community's time and effort.

Thanks,
Robin.

Quoted 28 lines
Quoted 28 lines
Quoted 19 lines
Quoted 19 lines

---
  drivers/iommu/dma-iommu.c | 2 +-

  1 file changed, 1 insertion(+), 1 deletion(-)
drivers/iommu/dma-iommu.c +1 -0
index 1910f4f1612b..e29157380c48 100644
@@ -294,6 +294,7 @@ static struct iommu_dma_cookie 
 *cookie_alloc(enum iommu_dma_cookie_type type)
       if (cookie) {
           INIT_LIST_HEAD(&cookie->msi_page_list);
           cookie->type = type;
+        mutex_init(&cookie->mutex);
       }
       return cookie;

  }

drivers/iommu/dma-iommu.c +0 -0
@@ -311,7 +312,6 @@ int iommu_get_dma_cookie(struct iommu_domain 
Quoted 8 lines
Quoted 8 lines
drivers/iommu/dma-iommu.c +0 -0
 *domain)
       if (!domain->iova_cookie)
           return -ENOMEM;
   -    mutex_init(&domain->iova_cookie->mutex);
       return 0;
   }

  --
2.25.1

.