Skip to contentSkip to conversation

hw/arm/smmu-common: Avoid smmu_find_smmu_pcibus return dangling pointer

PATCH Applied Philippe Mathieu-Daudé 2 participants 4 messages Feb 27, 2020 – Mar 2, 2020

lore
Share & tools
lore
#0 lore raw

v3 cover hw/arm/smmu-common: Avoid smmu_find_smmu_pcibus return dangling pointer

This series include the previous patch from Eric, then a code refactor to avoid similar mistakes in the future.

Eric Auger (1):
hw/arm/smmu-common: a fix to smmu_find_smmu_pcibus

Philippe Mathieu-Daudé (1):
hw/arm/smmu-common: Simplify smmu_find_smmu_pcibus() logic

 hw/arm/smmu-common.c | 20 +++++++++++---------
 1 file changed, 11 insertions(+), 9 deletions(-)
Signature · 1 line
2.21.1
#1 lore raw

PATCH v3 1/2 hw/arm/smmu-common: a fix to smmu_find_smmu_pcibus

From: Eric Auger <[email protected]>

Make sure a null SMMUPciBus is returned in case we were not able to identify a pci bus matching the @bus_num.

This matches the fix done on intel iommu in commit:
a2e1cd41ccfe796529abfd1b6aeb1dd4393762a2

Message-Id: <[email protected]>

---
 hw/arm/smmu-common.c | 1 +
 1 file changed, 1 insertion(+)
hw/arm/smmu-common.c +1 -0
index 0f2573f004..67d7b2d0fd 100644
@@ -301,6 +301,7 @@ SMMUPciBus *smmu_find_smmu_pcibus(SMMUState *s, uint8_t bus_num)
                 return smmu_pci_bus;
             }
         }
+        smmu_pci_bus = NULL;
     }
     return smmu_pci_bus;
 }
Signature · 1 line
2.21.1
#2 lore raw

PATCH v3 2/2 hw/arm/smmu-common: Simplify smmu_find_smmu_pcibus() logic

In reply to Philippe Mathieu-Daudé

The smmu_find_smmu_pcibus() function was introduced (in commit cac994ef43b) in a code format that could return an incorrect pointer, which was then fixed by the previous commit. We could have avoided this by writing the if() statement differently. Do it now, in case this function is re-used. The code is easier to review (harder to miss bugs).

---
This patch is easier to review with 'git-diff -w' (--ignore-all-space)

v3: Fixed typo reported by Eric

---
 hw/arm/smmu-common.c | 25 +++++++++++++------------
 1 file changed, 13 insertions(+), 12 deletions(-)
hw/arm/smmu-common.c +13 -12
index 67d7b2d0fd..e13a5f4a7c 100644
@@ -290,20 +290,21 @@ inline int smmu_ptw(SMMUTransCfg *cfg, dma_addr_t iova, IOMMUAccessFlags perm,
 SMMUPciBus *smmu_find_smmu_pcibus(SMMUState *s, uint8_t bus_num)
 {
     SMMUPciBus *smmu_pci_bus = s->smmu_pcibus_by_bus_num[bus_num];
+    GHashTableIter iter;
 
-    if (!smmu_pci_bus) {
-        GHashTableIter iter;
-
-        g_hash_table_iter_init(&iter, s->smmu_pcibus_by_busptr);
-        while (g_hash_table_iter_next(&iter, NULL, (void **)&smmu_pci_bus)) {
-            if (pci_bus_num(smmu_pci_bus->bus) == bus_num) {
-                s->smmu_pcibus_by_bus_num[bus_num] = smmu_pci_bus;
-                return smmu_pci_bus;
-            }
-        }
-        smmu_pci_bus = NULL;
+    if (smmu_pci_bus) {
+        return smmu_pci_bus;
     }
-    return smmu_pci_bus;
+
+    g_hash_table_iter_init(&iter, s->smmu_pcibus_by_busptr);
+    while (g_hash_table_iter_next(&iter, NULL, (void **)&smmu_pci_bus)) {
+        if (pci_bus_num(smmu_pci_bus->bus) == bus_num) {
+            s->smmu_pcibus_by_bus_num[bus_num] = smmu_pci_bus;
+            return smmu_pci_bus;
+        }
+    }
+
+    return NULL;
 }
 
 static AddressSpace *smmu_find_add_as(PCIBus *bus, void *opaque, int devfn)
Signature · 1 line
2.21.1
#3 lore raw
In reply to Philippe Mathieu-Daudé
Quoted 4 lines · Philippe Mathieu-Daudé

This series include the previous patch from Eric, then a code refactor to avoid similar mistakes in the future.

Quoted 8 lines

Eric Auger (1):
hw/arm/smmu-common: a fix to smmu_find_smmu_pcibus

Philippe Mathieu-Daudé (1):
hw/arm/smmu-common: Simplify smmu_find_smmu_pcibus() logic

 hw/arm/smmu-common.c | 20 +++++++++++---------
 1 file changed, 11 insertions(+), 9 deletions(-)

Applied to target-arm.next, thanks.

-- PMM