Skip to contentSkip to conversation

iommu/vt-d: Detach domain before using a private one

PATCH In review Lu Baolu 3 participants 6 messages Aug 6, 2019 – Aug 9, 2019 IOMMU

lore
Share & tools
lore

Lu Baolu

author
#0 lore raw

PATCH 1/2 iommu/vt-d: Detach domain before using a private one

When the default domain of a group doesn't work for a device, the iommu driver will try to use a private domain. The domain which was previously attached to the device must be detached.

Cc Ashok Raj <[email protected]>
Cc Jacob Pan <[email protected]>
Cc Kevin Tian <[email protected]>
Cc Alex Williamson <[email protected]>
Fixes 942067f1b6b97 ("iommu/vt-d: Identify default domains replaced with private")
---
 drivers/iommu/intel-iommu.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
drivers/iommu/intel-iommu.c +5 -1
index 3e22fa6ae8c8..37259b7f95a7 100644
@@ -3449,6 +3449,7 @@ static bool iommu_need_mapping(struct device *dev)
 				dmar_domain = to_dmar_domain(domain);
 				dmar_domain->flags |= DOMAIN_FLAG_LOSE_CHILDREN;
 			}
+			dmar_remove_one_dev_info(dev);
 			get_private_domain_for_dev(dev);
 		}
 
@@ -4803,7 +4804,8 @@ static void dmar_remove_one_dev_info(struct device *dev)
 
 	spin_lock_irqsave(&device_domain_lock, flags);
 	info = dev->archdata.iommu;
-	__dmar_remove_one_dev_info(info);
+	if (info)
+		__dmar_remove_one_dev_info(info);
 	spin_unlock_irqrestore(&device_domain_lock, flags);
 }
 
@@ -5281,6 +5283,7 @@ static int intel_iommu_add_device(struct device *dev)
 		if (device_def_domain_type(dev) == IOMMU_DOMAIN_IDENTITY) {
 			ret = iommu_request_dm_for_dev(dev);
 			if (ret) {
+				dmar_remove_one_dev_info(dev);
 				dmar_domain->flags |= DOMAIN_FLAG_LOSE_CHILDREN;
 				domain_add_dev_info(si_domain, dev);
 				dev_info(dev,
@@ -5291,6 +5294,7 @@ static int intel_iommu_add_device(struct device *dev)
 		if (device_def_domain_type(dev) == IOMMU_DOMAIN_DMA) {
 			ret = iommu_request_dma_domain_for_dev(dev);
 			if (ret) {
+				dmar_remove_one_dev_info(dev);
 				dmar_domain->flags |= DOMAIN_FLAG_LOSE_CHILDREN;
 				if (!get_private_domain_for_dev(dev)) {
 					dev_warn(dev,
Signature · 1 line
2.17.1
author
#1 lore raw

PATCH 2/2 iommu/vt-d: Fix possible use-after-free of private domain

Multiple devices might share a private domain. One real example is a pci bridge and all devices behind it. When remove a private domain, make sure that it has been detached from all devices to avoid use-after-free case.

Cc Ashok Raj <[email protected]>
Cc Jacob Pan <[email protected]>
Cc Kevin Tian <[email protected]>
Cc Alex Williamson <[email protected]>
Fixes 942067f1b6b97 ("iommu/vt-d: Identify default domains replaced with private")
---
 drivers/iommu/intel-iommu.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/iommu/intel-iommu.c +2 -1
index 37259b7f95a7..12d094d08c0a 100644
@@ -4791,7 +4791,8 @@ static void __dmar_remove_one_dev_info(struct device_domain_info *info)
 
 	/* free the private domain */
 	if (domain->flags & DOMAIN_FLAG_LOSE_CHILDREN &&
-	    !(domain->flags & DOMAIN_FLAG_STATIC_IDENTITY))
+	    !(domain->flags & DOMAIN_FLAG_STATIC_IDENTITY) &&
+	    list_empty(&domain->devices))
 		domain_exit(info->domain);
 
 	free_devinfo_mem(info);
Signature · 6 lines
2.17.1

_______________________________________________
iommu mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/iommu
Tested-by on v1 2/2
#2 lore raw

Re: [PATCH 2/2] iommu/vt-d: Fix possible use-after-free of private domain

Quoted 5 lines · On Tue, 6 Aug 2019 08:14:09 +0800 Lu Baolu <[email protected]> wrote:

Multiple devices might share a private domain. One real example is a pci bridge and all devices behind it. When remove a private domain, make sure that it has been detached from all devices to avoid use-after-free case.

Quoted 7 lines
Cc Ashok Raj <[email protected]>
Cc Jacob Pan <[email protected]>
Cc Kevin Tian <[email protected]>
Cc Alex Williamson <[email protected]>
Fixes 942067f1b6b97 ("iommu/vt-d: Identify default domains replaced with private")

---

Quoted 8 lines
 drivers/iommu/intel-iommu.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/iommu/intel-iommu.c +0 -0
index 37259b7f95a7..12d094d08c0a 100644

drivers/iommu/intel-iommu.c static void __dmar_remove_one_dev_info(struct device_domain_info *info) · line 4798

Quoted 9 lines
drivers/iommu/intel-iommu.c +2 -1
@@ -4791,7 +4791,8 @@ static void __dmar_remove_one_dev_info(struct device_domain_info *info)
 
 	/* free the private domain */
 	if (domain->flags & DOMAIN_FLAG_LOSE_CHILDREN &&
-	    !(domain->flags & DOMAIN_FLAG_STATIC_IDENTITY))
+	    !(domain->flags & DOMAIN_FLAG_STATIC_IDENTITY) &&
+	    list_empty(&domain->devices))
 		domain_exit(info->domain);
 
 	free_devinfo_mem(info);

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

Tested-by on v1 1/2
#3 lore raw
In reply to Lu Baolu
Quoted 12 lines · On Tue, 6 Aug 2019 08:14:08 +0800 Lu Baolu <[email protected]> wrote:

When the default domain of a group doesn't work for a device, the iommu driver will try to use a private domain. The domain which was previously attached to the device must be detached.

Cc Ashok Raj <[email protected]>
Cc Jacob Pan <[email protected]>
Cc Kevin Tian <[email protected]>
Cc Alex Williamson <[email protected]>
Fixes 942067f1b6b97 ("iommu/vt-d: Identify default domains replaced with private")
Quoted 1 line

---

This series resolves the issue I reported against:
iommu/vt-d: Cleanup get_valid_domain_for_dev()

Thanks,
Alex

Quoted 34 lines
 drivers/iommu/intel-iommu.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
drivers/iommu/intel-iommu.c +4 -1
index 3e22fa6ae8c8..37259b7f95a7 100644
@@ -3449,6 +3449,7 @@ static bool iommu_need_mapping(struct device *dev)
 				dmar_domain = to_dmar_domain(domain);
 				dmar_domain->flags |= DOMAIN_FLAG_LOSE_CHILDREN;
 			}
+			dmar_remove_one_dev_info(dev);
 			get_private_domain_for_dev(dev);
 		}
 
@@ -4803,7 +4804,8 @@ static void dmar_remove_one_dev_info(struct device *dev)
 
 	spin_lock_irqsave(&device_domain_lock, flags);
 	info = dev->archdata.iommu;
-	__dmar_remove_one_dev_info(info);
+	if (info)
+		__dmar_remove_one_dev_info(info);
 	spin_unlock_irqrestore(&device_domain_lock, flags);
 }
 
@@ -5281,6 +5283,7 @@ static int intel_iommu_add_device(struct device *dev)
 		if (device_def_domain_type(dev) == IOMMU_DOMAIN_IDENTITY) {
 			ret = iommu_request_dm_for_dev(dev);
 			if (ret) {
+				dmar_remove_one_dev_info(dev);
 				dmar_domain->flags |= DOMAIN_FLAG_LOSE_CHILDREN;
 				domain_add_dev_info(si_domain, dev);
 				dev_info(dev,

drivers/iommu/intel-iommu.c static int intel_iommu_add_device(struct device *dev) · line 5300

Quoted 7 lines
drivers/iommu/intel-iommu.c +1 -0
@@ -5291,6 +5294,7 @@ static int intel_iommu_add_device(struct device *dev)
 		if (device_def_domain_type(dev) == IOMMU_DOMAIN_DMA) {
 			ret = iommu_request_dma_domain_for_dev(dev);
 			if (ret) {
+				dmar_remove_one_dev_info(dev);
 				dmar_domain->flags |= DOMAIN_FLAG_LOSE_CHILDREN;
 				if (!get_private_domain_for_dev(dev)) {
 					dev_warn(dev,

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

author
#4 lore raw

Hi Alex,

Quoted 17 lines · Alex Williamson
Quoted 12 lines · On Tue, 6 Aug 2019 08:14:08 +0800 Lu Baolu <[email protected]> wrote:

When the default domain of a group doesn't work for a device, the iommu driver will try to use a private domain. The domain which was previously attached to the device must be detached.

Cc Ashok Raj <[email protected]>
Cc Jacob Pan <[email protected]>
Cc Kevin Tian <[email protected]>
Cc Alex Williamson <[email protected]>
Fixes 942067f1b6b97 ("iommu/vt-d: Identify default domains replaced with private")
Quoted 1 line

---

Quoted 4 lines

This series resolves the issue I reported against:
iommu/vt-d: Cleanup get_valid_domain_for_dev()

Thank you for testing it.

Best regards,
Baolu

Quoted 47 lines

Thanks,
Alex

Quoted 41 lines
  drivers/iommu/intel-iommu.c | 6 +++++-
  1 file changed, 5 insertions(+), 1 deletion(-)
drivers/iommu/intel-iommu.c +5 -1
index 3e22fa6ae8c8..37259b7f95a7 100644
@@ -3449,6 +3449,7 @@ static bool iommu_need_mapping(struct device *dev)
  				dmar_domain = to_dmar_domain(domain);
  				dmar_domain->flags |= DOMAIN_FLAG_LOSE_CHILDREN;
  			}
+			dmar_remove_one_dev_info(dev);
  			get_private_domain_for_dev(dev);
  		}
 
@@ -4803,7 +4804,8 @@ static void dmar_remove_one_dev_info(struct device *dev)
 
  	spin_lock_irqsave(&device_domain_lock, flags);
  	info = dev->archdata.iommu;
-	__dmar_remove_one_dev_info(info);
+	if (info)
+		__dmar_remove_one_dev_info(info);
  	spin_unlock_irqrestore(&device_domain_lock, flags);
  }
 
@@ -5281,6 +5283,7 @@ static int intel_iommu_add_device(struct device *dev)
  		if (device_def_domain_type(dev) == IOMMU_DOMAIN_IDENTITY) {
  			ret = iommu_request_dm_for_dev(dev);
  			if (ret) {
+				dmar_remove_one_dev_info(dev);
  				dmar_domain->flags |= DOMAIN_FLAG_LOSE_CHILDREN;
  				domain_add_dev_info(si_domain, dev);
  				dev_info(dev,
@@ -5291,6 +5294,7 @@ static int intel_iommu_add_device(struct device *dev)
  		if (device_def_domain_type(dev) == IOMMU_DOMAIN_DMA) {
  			ret = iommu_request_dma_domain_for_dev(dev);
  			if (ret) {
+				dmar_remove_one_dev_info(dev);
  				dmar_domain->flags |= DOMAIN_FLAG_LOSE_CHILDREN;
  				if (!get_private_domain_for_dev(dev)) {
  					dev_warn(dev,