Skip to contentSkip to conversation

iommu/vt-d: Fix PCI bus rescan device hot add

PATCH Superseded Jacob Pan 2 participants 5 messages Jan 13, 2022 – Jan 14, 2022

Subsystems (2)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Jacob Pan

author
#0 lore raw

PATCH 1/1 iommu/vt-d: Fix PCI bus rescan device hot add

During PCI bus rescan, adding new devices involve two notifiers.
1. dmar_pci_bus_notifier()
2. iommu_bus_notifier()
The current code sets #1 as low priority (INT_MIN) which resulted in #2 being invoked first. The result is that struct device pointer cannot be found in DRHD search for the new device's DMAR/IOMMU. Subsequently, the device is put under the "catch-all" IOMMU instead of the correct one.

This could cause system hang when device TLB invalidation is sent to the wrong IOMMU. Invalidation timeout error or hard lockup can be observed.

This patch fixes the issue by setting a higher priority for dmar_pci_bus_notifier. DRHD search for a new device will find the correct IOMMU.

Fixes 59ce0515cdaf ("iommu/vt-d: Update DRHD/RMRR/ATSR device scope")
---
 drivers/iommu/intel/dmar.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/iommu/intel/dmar.c +1 -1
index 915bff76fe96..5d07e5b89c2e 100644
@@ -385,7 +385,7 @@ static int dmar_pci_bus_notifier(struct notifier_block *nb,
 
 static struct notifier_block dmar_pci_bus_nb = {
 	.notifier_call = dmar_pci_bus_notifier,
-	.priority = INT_MIN,
+	.priority = INT_MAX,
 };
 
 static struct dmar_drhd_unit *
Signature · 1 line
2.25.1
maintainer
#1 lore raw

Hi Jacob,

Quoted 28 lines · Jacob Pan

During PCI bus rescan, adding new devices involve two notifiers.
1. dmar_pci_bus_notifier()
2. iommu_bus_notifier()
The current code sets #1 as low priority (INT_MIN) which resulted in #2 being invoked first. The result is that struct device pointer cannot be found in DRHD search for the new device's DMAR/IOMMU. Subsequently, the device is put under the "catch-all" IOMMU instead of the correct one.

This could cause system hang when device TLB invalidation is sent to the wrong IOMMU. Invalidation timeout error or hard lockup can be observed.

This patch fixes the issue by setting a higher priority for dmar_pci_bus_notifier. DRHD search for a new device will find the correct IOMMU.

Fixes 59ce0515cdaf ("iommu/vt-d: Update DRHD/RMRR/ATSR device scope")
---
  drivers/iommu/intel/dmar.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
drivers/iommu/intel/dmar.c +0 -0
index 915bff76fe96..5d07e5b89c2e 100644

drivers/iommu/intel/dmar.c static int dmar_pci_bus_notifier(struct notifier_block *nb, · line 391

Quoted 8 lines
drivers/iommu/intel/dmar.c +1 -1
@@ -385,7 +385,7 @@ static int dmar_pci_bus_notifier(struct notifier_block *nb,
 
  static struct notifier_block dmar_pci_bus_nb = {
  	.notifier_call = dmar_pci_bus_notifier,
-	.priority = INT_MIN,
+	.priority = INT_MAX,
  };
 
  static struct dmar_drhd_unit *

Nice catch! dmar_pci_bus_add_dev() should take place *before* iommu_probe_device(). This change enforces this with a higher notifier priority for dmar callback.

Comparably, dmar_pci_bus_del_dev() should take place *after* iommu_release_device(). Perhaps we can use two notifiers, one for ADD_DEVICE (with .priority=INT_MAX) and the other for REMOVE_DEVICE (with .priority=INT_MIN)?

Best regards,
baolu

author
#2 lore raw

Hi BaoLu,

Quoted 44 lines · Lu Baolu

Hi Jacob,

Quoted 28 lines · Jacob Pan

During PCI bus rescan, adding new devices involve two notifiers.
1. dmar_pci_bus_notifier()
2. iommu_bus_notifier()
The current code sets #1 as low priority (INT_MIN) which resulted in #2 being invoked first. The result is that struct device pointer cannot be found in DRHD search for the new device's DMAR/IOMMU. Subsequently, the device is put under the "catch-all" IOMMU instead of the correct one.

This could cause system hang when device TLB invalidation is sent to the wrong IOMMU. Invalidation timeout error or hard lockup can be observed.

This patch fixes the issue by setting a higher priority for dmar_pci_bus_notifier. DRHD search for a new device will find the correct IOMMU.

Fixes 59ce0515cdaf ("iommu/vt-d: Update DRHD/RMRR/ATSR device scope")
---
  drivers/iommu/intel/dmar.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
drivers/iommu/intel/dmar.c +0 -0
index 915bff76fe96..5d07e5b89c2e 100644

drivers/iommu/intel/dmar.c static int dmar_pci_bus_notifier(struct · line 391

Quoted 8 lines
drivers/iommu/intel/dmar.c +1 -1
@@ -385,7 +385,7 @@ static int dmar_pci_bus_notifier(struct
 notifier_block *nb, 
  static struct notifier_block dmar_pci_bus_nb = {
  	.notifier_call = dmar_pci_bus_notifier,
-	.priority = INT_MIN,
+	.priority = INT_MAX,
  };
 
  static struct dmar_drhd_unit *

Nice catch! dmar_pci_bus_add_dev() should take place *before* iommu_probe_device(). This change enforces this with a higher notifier priority for dmar callback.

Quoted 5 lines

Comparably, dmar_pci_bus_del_dev() should take place *after* iommu_release_device(). Perhaps we can use two notifiers, one for ADD_DEVICE (with .priority=INT_MAX) and the other for REMOVE_DEVICE (with .priority=INT_MIN)?

Since device_to_iommu() lookup in intel_iommu_release_device() only checks if device is under "an" IOMMU, not "the" IOMMU. Then the remove path order is not needed, right?

I know this is not robust, but having so many notifiers with implicit priority is not clean either.

Perhaps, we should have explicit priority defined around iommu_bus notifier? i.e.

@@ -1841,6 +1841,7 @@ static int iommu_bus_init(struct bus_type *bus, const

struct iommu_ops *ops) return -ENOMEM; 
        nb->notifier_call = iommu_bus_notifier;

+ nb->priority = IOMMU_BUS_NOTIFY_PRIORITY;

static struct notifier_block dmar_pci_bus_add_nb = {
.notifier_call = dmar_pci_bus_notifier,
- .priority = INT_MIN,
+ .priority = IOMMU_BUS_NOTIFY_PRIORITY + 1,
};

static struct notifier_block dmar_pci_bus_remove_nb = {
.notifier_call = dmar_pci_bus_notifier,
- .priority = INT_MIN,
+ .priority = IOMMU_BUS_NOTIFY_PRIORITY - 1,
};

Quoted 2 lines

Best regards,
baolu

Thanks,

Jacob

maintainer
#3 lore raw
Quoted 70 lines · Jacob Pan
Quoted 43 lines · Lu Baolu

Hi Jacob,

Quoted 28 lines · Jacob Pan

During PCI bus rescan, adding new devices involve two notifiers.
1. dmar_pci_bus_notifier()
2. iommu_bus_notifier()
The current code sets #1 as low priority (INT_MIN) which resulted in #2 being invoked first. The result is that struct device pointer cannot be found in DRHD search for the new device's DMAR/IOMMU. Subsequently, the device is put under the "catch-all" IOMMU instead of the correct one.

This could cause system hang when device TLB invalidation is sent to the wrong IOMMU. Invalidation timeout error or hard lockup can be observed.

This patch fixes the issue by setting a higher priority for dmar_pci_bus_notifier. DRHD search for a new device will find the correct IOMMU.

Fixes 59ce0515cdaf ("iommu/vt-d: Update DRHD/RMRR/ATSR device scope")
---
   drivers/iommu/intel/dmar.c | 2 +-
   1 file changed, 1 insertion(+), 1 deletion(-)
drivers/iommu/intel/dmar.c +0 -0
index 915bff76fe96..5d07e5b89c2e 100644

drivers/iommu/intel/dmar.c static int dmar_pci_bus_notifier(struct · line 391

Quoted 8 lines
drivers/iommu/intel/dmar.c +1 -1
@@ -385,7 +385,7 @@ static int dmar_pci_bus_notifier(struct
 notifier_block *nb,
   static struct notifier_block dmar_pci_bus_nb = {
   	.notifier_call = dmar_pci_bus_notifier,
-	.priority = INT_MIN,
+	.priority = INT_MAX,
   };
 
   static struct dmar_drhd_unit *

Nice catch! dmar_pci_bus_add_dev() should take place*before* iommu_probe_device(). This change enforces this with a higher notifier priority for dmar callback.

Quoted 5 lines

Comparably, dmar_pci_bus_del_dev() should take place*after* iommu_release_device(). Perhaps we can use two notifiers, one for ADD_DEVICE (with .priority=INT_MAX) and the other for REMOVE_DEVICE (with .priority=INT_MIN)?

Since device_to_iommu() lookup in intel_iommu_release_device() only checks if device is under "an" IOMMU, not "the" IOMMU. Then the remove path order is not needed, right?

I know this is not robust, but having so many notifiers with implicit priority is not clean either.

Perhaps, we should have explicit priority defined around iommu_bus notifier? i.e.

(quoted diff) +1 -0
@@ -1841,6 +1841,7 @@ static int iommu_bus_init(struct bus_type *bus, const
 struct iommu_ops *ops) return -ENOMEM;
         nb->notifier_call = iommu_bus_notifier;
 
+       nb->priority = IOMMU_BUS_NOTIFY_PRIORITY;
 
 
  static struct notifier_block dmar_pci_bus_add_nb = {

.notifier_call = dmar_pci_bus_notifier,

Review

Quoted 9 lines
(quoted diff) +2 -2
-       .priority = INT_MIN,
+       .priority = IOMMU_BUS_NOTIFY_PRIORITY + 1,
  };
 
  static struct notifier_block dmar_pci_bus_remove_nb = {
         .notifier_call = dmar_pci_bus_notifier,
-       .priority = INT_MIN,
+       .priority = IOMMU_BUS_NOTIFY_PRIORITY - 1,
  };

IOMMU_BUS_NOTIFY_PRIORITY by default is 0. So you can simply use 1 and -1? Adding a comment around it will be helpful.

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

author
#4 lore raw

Hi Lu,

Quoted 77 lines · Lu Baolu
Quoted 76 lines · Jacob Pan
Quoted 45 lines · Lu Baolu

Hi Jacob,

Quoted 30 lines · Jacob Pan

During PCI bus rescan, adding new devices involve two notifiers.
1. dmar_pci_bus_notifier()
2. iommu_bus_notifier()
The current code sets #1 as low priority (INT_MIN) which resulted in #2 being invoked first. The result is that struct device pointer cannot be found in DRHD search for the new device's DMAR/IOMMU. Subsequently, the device is put under the "catch-all" IOMMU instead of the correct one.

This could cause system hang when device TLB invalidation is sent to the wrong IOMMU. Invalidation timeout error or hard lockup can be observed.

This patch fixes the issue by setting a higher priority for dmar_pci_bus_notifier. DRHD search for a new device will find the correct IOMMU.

Fixes 59ce0515cdaf ("iommu/vt-d: Update DRHD/RMRR/ATSR device scope")
---
   drivers/iommu/intel/dmar.c | 2 +-
   1 file changed, 1 insertion(+), 1 deletion(-)
drivers/iommu/intel/dmar.c +0 -0
index 915bff76fe96..5d07e5b89c2e 100644

drivers/iommu/intel/dmar.c static int dmar_pci_bus_notifier(struct · line 391

Quoted 8 lines
drivers/iommu/intel/dmar.c +1 -1
@@ -385,7 +385,7 @@ static int dmar_pci_bus_notifier(struct
 notifier_block *nb,
   static struct notifier_block dmar_pci_bus_nb = {
   	.notifier_call = dmar_pci_bus_notifier,
-	.priority = INT_MIN,
+	.priority = INT_MAX,
   };
 
   static struct dmar_drhd_unit *

Nice catch! dmar_pci_bus_add_dev() should take place*before* iommu_probe_device(). This change enforces this with a higher notifier priority for dmar callback.

Quoted 5 lines

Comparably, dmar_pci_bus_del_dev() should take place*after* iommu_release_device(). Perhaps we can use two notifiers, one for ADD_DEVICE (with .priority=INT_MAX) and the other for REMOVE_DEVICE (with .priority=INT_MIN)?

Since device_to_iommu() lookup in intel_iommu_release_device() only checks if device is under "an" IOMMU, not "the" IOMMU. Then the remove path order is not needed, right?

I know this is not robust, but having so many notifiers with implicit priority is not clean either.

Perhaps, we should have explicit priority defined around iommu_bus notifier? i.e.

(quoted diff) +1 -0
@@ -1841,6 +1841,7 @@ static int iommu_bus_init(struct bus_type *bus,
 const struct iommu_ops *ops) return -ENOMEM;
         nb->notifier_call = iommu_bus_notifier;
 
+       nb->priority = IOMMU_BUS_NOTIFY_PRIORITY;
 
 
  static struct notifier_block dmar_pci_bus_add_nb = {

.notifier_call = dmar_pci_bus_notifier,

drivers/iommu/intel/dmar.c +1 -1
-       .priority = INT_MIN,
+       .priority = IOMMU_BUS_NOTIFY_PRIORITY + 1,
  };
Quoted 9 lines

drivers/iommu/intel/dmar.c static int dmar_pci_bus_notifier(struct notifier_block *nb, · line 389

Quoted 5 lines
  static struct notifier_block dmar_pci_bus_remove_nb = {
         .notifier_call = dmar_pci_bus_notifier,
drivers/iommu/intel/dmar.c +1 -1
-       .priority = INT_MIN,
+       .priority = IOMMU_BUS_NOTIFY_PRIORITY - 1,
  };  

IOMMU_BUS_NOTIFY_PRIORITY by default is 0. So you can simply use 1 and -1? Adding a comment around it will be helpful.

Yeah, I will add comment.

Thanks,

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