hw/arm/smmu-common: a fix to smmu_find_smmu_pcibus
PATCH 1/1 hw/arm/smmu-common: a fix to smmu_find_smmu_pcibus
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
--- 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.20.1
Hi Eric,
Quoted 17 lines · Eric Auger
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:
a2e1cd41ccfe796529abfd1b6aeb1dd4393762a2Signed-off-by Eric Auger <[email protected]>--- hw/arm/smmu-common.c | 1 + 1 file changed, 1 insertion(+)hw/arm/smmu-common.c +0
-0
hw/arm/smmu-common.c SMMUPciBus *smmu_find_smmu_pcibus(SMMUState *s, uint8_t bus_num) · line 307
Quoted 7 lines
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; }
Patch is easy to review but code not. By inverting the if() statement I find the code easier to review. The patch isn't however:
-- >8 --
@@ -284,25 +284,27 @@ inline int smmu_ptw(SMMUTransCfg *cfg, dma_addr_t iova, IOMMUAccessFlags perm,
/**
* The bus number is used for lookup when SID based invalidation occurs.
* In that case we lazily populate the SMMUPciBus array from the bus hash
* table. At the time the SMMUPciBus is created (smmu_find_add_as),
the bus
* numbers may not be always initialized yet.
*/
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;
+ if (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;
- }
+ 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 smmu_pci_bus;
+
+ return NULL;
}
---
The code is easier although:
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) {
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;
}What do you think?
Quoted 14 lines · Philippe Mathieu-Daudé
Hi Eric,
Quoted 11 lines · Eric Auger
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:
a2e1cd41ccfe796529abfd1b6aeb1dd4393762a2Signed-off-by Eric Auger <[email protected]>---
hw/arm/smmu-common.c| 1 +1 file changed, 1 insertion(+)
Quoted 16 lines
Quoted 12 lines
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;Quoted 2 lines
}
Patch is easy to review but code not. By inverting the
if()statement I find the code easier to review. The patch isn't however:
I used 'git-diff -W' instead of 'git-diff -w'. -w works better:
-- >8 --
@@ -290,10 +290,12 @@ 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];
-
- if (!smmu_pci_bus) {
GHashTableIter iter;+ if (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) {
@@ -301,8 +303,8 @@ SMMUPciBus *smmu_find_smmu_pcibus(SMMUState *s, uint8_t bus_num)
return smmu_pci_bus;
}
}- }
- return smmu_pci_bus;
+
+ return NULL;
}
---
Quoted 24 lines
The code is easier although:
SMMUPciBus *smmu_find_smmu_pcibus(SMMUState *s,
uint8_tbus_num) {
SMMUPciBus*smmu_pci_bus= s->smmu_pcibus_by_bus_num[bus_num]; GHashTableIter iter;if (
smmu_pci_bus) {
returnsmmu_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; returnsmmu_pci_bus;
}
}return NULL;
}What do you think?
Quoted 28 lines · Philippe Mathieu-Daudé
Hi Eric,
Patch is easy to review but code not. By inverting the
if()statement I find the code easier to review.SMMUPciBus *smmu_find_smmu_pcibus(SMMUState *s,
uint8_tbus_num){ SMMUPciBus *smmu_pci_bus = s->smmu_pcibus_by_bus_num[bus_num]; GHashTableIter iter; if (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; }
Quoted 1 line
What do you think?
I think I agree with Philippe here -- the current code is already a bit confusing in that it looks at first glance as if the "find the bus in the hash table" code works by falling through into the "we already had the cached value" code on success, but in fact the success case is dealt with by the return in the middle of the while loop and it's only the not-found case that falls through. Adding this patch on top fixes the bug but leaves in place the odd code structure that caused it. Rearranging it as Philippe does above makes it much clearer what's happening, I think.
Would one of you like to submit a patch that does it that way round, please?
thanks
-- PMM
Quoted 2 lines · Peter Maydell
Would one of you like to submit a patch that does it that way round, please?
Aha, I see you already did:
https://patchew.org/QEMU/[email protected]/
(I process my to-review queue mostly oldest-first).
thanks
-- PMM
Quoted 7 lines · Eric Auger
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:
a2e1cd41ccfe796529abfd1b6aeb1dd4393762a2Signed-off-by Eric Auger <[email protected]>
Signature · 1 line
Peter Xu