hw/arm/smmu-common: Avoid smmu_find_smmu_pcibus return dangling pointer
v2 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.
v1: https://www.mail-archive.com/[email protected]/msg683451.html
Supersedes: <[email protected]>
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
PATCH v2 2/2 hw/arm/smmu-common: Simplify smmu_find_smmu_pcibus() logic
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 avoid 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)
--- hw/arm/smmu-common.c | 25 +++++++++++++------------ 1 file changed, 13 insertions(+), 12 deletions(-)
hw/arm/smmu-common.c +13 -12
@@ -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
PATCH v2 1/2 hw/arm/smmu-common: a fix to smmu_find_smmu_pcibus
In reply to Philippe Mathieu-Daudé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
@@ -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
Re: [PATCH v2 2/2] hw/arm/smmu-common: Simplify smmu_find_smmu_pcibus() logic
In reply to Philippe Mathieu-DaudéHi Philippe,
Quoted 4 lines · Philippe Mathieu-Daudé
The
smmu_find_smmu_pcibus()function was introduced (in commitcac994ef43b) in a code format that could return an incorrect pointer, which was then fixed by the previous commit.
We could have avoid this by writing theif()statement differently.
nit: avoided
Quoted 39 lines
Do it now, in case this function is re-used. The code is easier to review (harder to miss bugs).
Signed-off-by Philippe Mathieu-Daudé <[email protected]>---
This patch is easier to review with 'git-diff -w' (--ignore-all-space)--- hw/arm/smmu-common.c | 25 +++++++++++++------------ 1 file changed, 13 insertions(+), 12 deletions(-)hw/arm/smmu-common.c +6
-12@@ -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)) {
hw/arm/smmu-common.c inline int smmu_ptw(SMMUTransCfg *cfg, dma_addr_t iova, IOMMUAccessFlags perm, · line 310
Quoted 10 lines
hw/arm/smmu-common.c +7
-0+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)
Thanks!
Eric
Re: [PATCH v2 2/2] hw/arm/smmu-common: Simplify smmu_find_smmu_pcibus() logic
In reply to Philippe Mathieu-DaudéQuoted 8 lines · Philippe Mathieu-Daudé
The
smmu_find_smmu_pcibus()function was introduced (in commitcac994ef43b) in a code format that could return an incorrect pointer, which was then fixed by the previous commit.
We could have avoid this by writing theif()statement differently. Do it now, in case this function is re-used. The code is easier to review (harder to miss bugs).Signed-off-by Philippe Mathieu-Daudé <[email protected]>
Signature · 1 line
Peter Xu