Skip to contentSkip to conversation

x86_iommu: Fix segfault when starting on non-PCI machines

PATCH Superseded Mohammed Gamal 3 participants 8 messages Sep 18, 2017 – Sep 20, 2017

v4 of 5 Superseded Latest is v5 →

lore
Share & tools
lore
#0 lore raw

v4 cover x86_iommu: Fix segfault when starting on non-PCI machines

Starting qemu with
qemu-system-x86_64 -S -M isapc -device {amd|intel}-iommu leads to a segfault. The code assume PCI bus is present and tries to access the bus structure without checking.

The patch series moves the error checks from vtd_realize() and amdvi_realize() to the generic x86_iommu_realize() and adds a check for PCI bus presence.

v3 --> v4:
* Restore correct object_dynamic_cast() in x86_iommu_realize()
* Remove redundant casting in callee functions. Implemented in a new patch

Mohammed Gamal (3):
x86_iommu: Move machine check to x86_iommu_realize() intel_iommu, amd_iommu: Remove redundant object_dynamic_cast calls
x86_iommu: check if machine has PCI bus

 hw/i386/amd_iommu.c   | 13 ++-----------
 hw/i386/intel_iommu.c | 13 ++-----------
 hw/i386/x86-iommu.c   | 13 +++++++++++++
 3 files changed, 17 insertions(+), 22 deletions(-)
Signature · 1 line
1.8.3.1
#1 lore raw

PATCH v4 1/3 x86_iommu: Move machine check to x86_iommu_realize()

Instead of having the same error checks in vtd_realize() and amdvi_realize(), move that over to the generic x86_iommu_realize().

---
 hw/i386/amd_iommu.c   | 10 +---------
 hw/i386/intel_iommu.c | 10 +---------
 hw/i386/x86-iommu.c   | 13 +++++++++++++
 3 files changed, 15 insertions(+), 18 deletions(-)
hw/i386/amd_iommu.c +1 -9
index 334938a..839f01f 100644
@@ -1141,18 +1141,10 @@ static void amdvi_realize(DeviceState *dev, Error **err)
     AMDVIState *s = AMD_IOMMU_DEVICE(dev);
     X86IOMMUState *x86_iommu = X86_IOMMU_DEVICE(dev);
     MachineState *ms = MACHINE(qdev_get_machine());
-    MachineClass *mc = MACHINE_GET_CLASS(ms);
     PCMachineState *pcms =
         PC_MACHINE(object_dynamic_cast(OBJECT(ms), TYPE_PC_MACHINE));
-    PCIBus *bus;
+    PCIBus *bus = pcms->bus;
 
-    if (!pcms) {
-        error_setg(err, "Machine-type '%s' not supported by amd-iommu",
-                   mc->name);
-        return;
-    }
-
-    bus = pcms->bus;
     s->iotlb = g_hash_table_new_full(amdvi_uint64_hash,
                                      amdvi_uint64_equal, g_free, g_free);
 
hw/i386/intel_iommu.c +1 -9
index 3a5bb0b..aa01812 100644
@@ -3027,20 +3027,12 @@ static bool vtd_decide_config(IntelIOMMUState *s, Error **errp)
 static void vtd_realize(DeviceState *dev, Error **errp)
 {
     MachineState *ms = MACHINE(qdev_get_machine());
-    MachineClass *mc = MACHINE_GET_CLASS(ms);
     PCMachineState *pcms =
         PC_MACHINE(object_dynamic_cast(OBJECT(ms), TYPE_PC_MACHINE));
-    PCIBus *bus;
+    PCIBus *bus = pcms->bus;
     IntelIOMMUState *s = INTEL_IOMMU_DEVICE(dev);
     X86IOMMUState *x86_iommu = X86_IOMMU_DEVICE(dev);
 
-    if (!pcms) {
-        error_setg(errp, "Machine-type '%s' not supported by intel-iommu",
-                   mc->name);
-        return;
-    }
-
-    bus = pcms->bus;
     x86_iommu->type = TYPE_INTEL;
 
     if (!vtd_decide_config(s, errp)) {
hw/i386/x86-iommu.c +13 -0
index 293caf8..51de519 100644
@@ -21,6 +21,8 @@
 #include "hw/sysbus.h"
 #include "hw/boards.h"
 #include "hw/i386/x86-iommu.h"
+#include "hw/i386/pc.h"
+#include "qapi/error.h"
 #include "qemu/error-report.h"
 #include "trace.h"
 
@@ -80,7 +82,18 @@ static void x86_iommu_realize(DeviceState *dev, Error **errp)
 {
     X86IOMMUState *x86_iommu = X86_IOMMU_DEVICE(dev);
     X86IOMMUClass *x86_class = X86_IOMMU_GET_CLASS(dev);
+    MachineState *ms = MACHINE(qdev_get_machine());
+    MachineClass *mc = MACHINE_GET_CLASS(ms);
+    PCMachineState *pcms =
+        PC_MACHINE(object_dynamic_cast(OBJECT(ms), TYPE_PC_MACHINE));
     QLIST_INIT(&x86_iommu->iec_notifiers);
+
+    if (!pcms) {
+        error_setg(errp, "Machine-type '%s' not supported by IOMMU",
+                   mc->name);
+        return;
+    }
+
     if (x86_class->realize) {
         x86_class->realize(dev, errp);
     }
Signature · 1 line
1.8.3.1
Reviewed-by on v4 1/3
#2 lore raw

Re: [Qemu-devel] [PATCH v4 1/3] x86_iommu: Move machine check to x86_iommu_realize()

Quoted 5 lines · Mohammed Gamal

Instead of having the same error checks in vtd_realize() and amdvi_realize(), move that over to the generic x86_iommu_realize().

If patch 2/3 is squashed into this one:

Signature · 1 line
Eduardo
#3 lore raw

PATCH v4 2/3 intel_iommu, amd_iommu: Remove redundant object_dynamic_cast calls

In reply to Mohammed Gamal

Now that the calling x86_iommu_realize() calls object_dynamic_cast(), doing the cast in amdvi_realize() and vtd_realize() is not needed.

Use PC_MACHINE macro directly.

---
 hw/i386/amd_iommu.c   | 3 +--
 hw/i386/intel_iommu.c | 3 +--
 2 files changed, 2 insertions(+), 4 deletions(-)
hw/i386/amd_iommu.c +1 -2
index 839f01f..f2e1868 100644
@@ -1141,8 +1141,7 @@ static void amdvi_realize(DeviceState *dev, Error **err)
     AMDVIState *s = AMD_IOMMU_DEVICE(dev);
     X86IOMMUState *x86_iommu = X86_IOMMU_DEVICE(dev);
     MachineState *ms = MACHINE(qdev_get_machine());
-    PCMachineState *pcms =
-        PC_MACHINE(object_dynamic_cast(OBJECT(ms), TYPE_PC_MACHINE));
+    PCMachineState *pcms = PC_MACHINE(ms);
     PCIBus *bus = pcms->bus;
 
     s->iotlb = g_hash_table_new_full(amdvi_uint64_hash,
hw/i386/intel_iommu.c +1 -2
index aa01812..0138b3b 100644
@@ -3027,8 +3027,7 @@ static bool vtd_decide_config(IntelIOMMUState *s, Error **errp)
 static void vtd_realize(DeviceState *dev, Error **errp)
 {
     MachineState *ms = MACHINE(qdev_get_machine());
-    PCMachineState *pcms =
-        PC_MACHINE(object_dynamic_cast(OBJECT(ms), TYPE_PC_MACHINE));
+    PCMachineState *pcms = PC_MACHINE(ms);
     PCIBus *bus = pcms->bus;
     IntelIOMMUState *s = INTEL_IOMMU_DEVICE(dev);
     X86IOMMUState *x86_iommu = X86_IOMMU_DEVICE(dev);
Signature · 1 line
1.8.3.1
Reviewed-by on v4 2/3
#4 lore raw

Re: [Qemu-devel] [PATCH v4 2/3] intel_iommu, amd_iommu: Remove redundant object_dynamic_cast calls

Quoted 6 lines · Mohammed Gamal

Now that the calling x86_iommu_realize() calls object_dynamic_cast(), doing the cast in amdvi_realize() and vtd_realize() is not needed.

Use PC_MACHINE macro directly.

I suggest squashing this into patch 1/3. It doesn't need to be a separate patch.

Signature · 1 line
Eduardo
#5 lore raw

PATCH v4 3/3 x86_iommu: check if machine has PCI bus

In reply to Mohammed Gamal

Starting qemu with
qemu-system-x86_64 -S -M isapc -device {amd|intel}-iommu leads to a segfault. The code assume PCI bus is present and tries to access the bus structure without checking.

Since Intel VT-d and AMDVI should only work with PCI, add a check for PCI bus and return error if not present.

---
 hw/i386/x86-iommu.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
hw/i386/x86-iommu.c +1 -1
index 51de519..8a01a2d 100644
@@ -88,7 +88,7 @@ static void x86_iommu_realize(DeviceState *dev, Error **errp)
         PC_MACHINE(object_dynamic_cast(OBJECT(ms), TYPE_PC_MACHINE));
     QLIST_INIT(&x86_iommu->iec_notifiers);
 
-    if (!pcms) {
+    if (!pcms || !pcms->bus) {
         error_setg(errp, "Machine-type '%s' not supported by IOMMU",
                    mc->name);
         return;
Signature · 1 line
1.8.3.1
Reviewed-by on v4 3/3
#6 lore raw

Re: [Qemu-devel] [PATCH v4 3/3] x86_iommu: check if machine has PCI bus

Quoted 9 lines · Mohammed Gamal

Starting qemu with
qemu-system-x86_64 -S -M isapc -device {amd|intel}-iommu leads to a segfault. The code assume PCI bus is present and tries to access the bus structure without checking.

Since Intel VT-d and AMDVI should only work with PCI, add a check for PCI bus and return error if not present.

Signature · 1 line
Eduardo
Reviewed-by on the v4 series
#7 lore raw
In reply to Mohammed Gamal
Quoted 14 lines · Mohammed Gamal

Starting qemu with
qemu-system-x86_64 -S -M isapc -device {amd|intel}-iommu leads to a segfault. The code assume PCI bus is present and tries to access the bus structure without checking.

The patch series moves the error checks from vtd_realize() and amdvi_realize() to the generic x86_iommu_realize() and adds a check for PCI bus presence.

v3 --> v4:
* Restore correct object_dynamic_cast() in x86_iommu_realize()
* Remove redundant casting in callee functions. Implemented in a new patch

Quoted 4 lines

Mohammed Gamal (3):
x86_iommu: Move machine check to x86_iommu_realize() intel_iommu, amd_iommu: Remove redundant object_dynamic_cast calls
x86_iommu: check if machine has PCI bus

After patch 2 squashed to patch 1 (patch 2 itself is not valid; the problem only appears after patch 1):

Quoted 9 lines
 hw/i386/amd_iommu.c   | 13 ++-----------
 hw/i386/intel_iommu.c | 13 ++-----------
 hw/i386/x86-iommu.c   | 13 +++++++++++++
 3 files changed, 17 insertions(+), 22 deletions(-)
Signature · 1 line
1.8.3.1
Signature · 1 line
Peter Xu