hw/i386/intel_iommu: Simplify vtd_find_as_from_bus_num() logic
PATCH v2 1/1 hw/i386/intel_iommu: Simplify vtd_find_as_from_bus_num() logic
The vtd_find_as_from_bus_num() function was introduced (in commit dbaabb25f) in a code format that could return an incorrect pointer, which was later fixed by commit a2e1cd41ccf.
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).
---
Since v2: Re-worded commit description, patch sent without -w.
This patch is easier to review with 'git-diff -w' (--ignore-all-space):
@@ -987,14 +987,17 @@ static bool vtd_slpte_nonzero_rsvd(uint64_t slpte, uint32_t level)
static VTDBus *vtd_find_as_from_bus_num(IntelIOMMUState *s, uint8_t bus_num)
{
VTDBus *vtd_bus = s->vtd_as_by_bus_num[bus_num];- if (!vtd_bus) {
+ GHashTableIter iter;
+
+ if (vtd_bus) {
+ return vtd_bus;
+ }
+
/*
* Iterate over the registered buses to find the one which
* currently hold this bus number, and update the bus_num
* lookup table:
*/
- GHashTableIter iter;
-
g_hash_table_iter_init(&iter, s->vtd_as_by_busptr);
while (g_hash_table_iter_next(&iter, NULL, (void **)&vtd_bus)) {
if (pci_bus_num(vtd_bus->bus) == bus_num) {
@@ -1002,9 +1005,8 @@ static VTDBus *vtd_find_as_from_bus_num(IntelIOMMUState *s, uint8_t bus_num)
return vtd_bus;
}
}- vtd_bus = NULL;
- }
- return vtd_bus;
+
+ return NULL;
}
--- hw/i386/intel_iommu.c | 34 ++++++++++++++++++---------------- 1 file changed, 18 insertions(+), 16 deletions(-)
hw/i386/intel_iommu.c +18 -16
@@ -987,24 +987,26 @@ static bool vtd_slpte_nonzero_rsvd(uint64_t slpte, uint32_t level)static VTDBus *vtd_find_as_from_bus_num (IntelIOMMUState *s,uint8_t bus_num) {VTDBus *vtd_bus = s->vtd_as_by_bus_num[bus_num]; -if (!vtd_bus) { -/* -* Iterate over the registered buses to find the one which -* currently hold this bus number, and update the bus_num -* lookup table: -*/ -GHashTableIter iter; +GHashTableIter iter; -g_hash_table_iter_init (&iter, s->vtd_as_by_busptr); -while (g_hash_table_iter_next (&iter,NULL , (void **)&vtd_bus)) { -if (pci_bus_num (vtd_bus->bus) == bus_num) { - s->vtd_as_by_bus_num[bus_num] = vtd_bus; -return vtd_bus; - } - } - vtd_bus =NULL ; +if (vtd_bus) { +return vtd_bus; } -return vtd_bus; + +/* +* Iterate over the registered buses to find the one which +* currently hold this bus number, and update the bus_num +* lookup table: +*/ +g_hash_table_iter_init (&iter, s->vtd_as_by_busptr); +while (g_hash_table_iter_next (&iter,NULL , (void **)&vtd_bus)) { +if (pci_bus_num (vtd_bus->bus) == bus_num) { + s->vtd_as_by_bus_num[bus_num] = vtd_bus; +return vtd_bus; + } + } + +return NULL ; }/* Given the @iova, get relevant @slptep. @slpte_level will be the last level
Signature · 1 line
2.21.1
Quoted 35 lines · Philippe Mathieu-Daudé
The
vtd_find_as_from_bus_num()function was introduced (in commitdbaabb25f) in a code format that could return an incorrect pointer, which was later fixed by commita2e1cd41ccf.
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]>---
Since v2: Re-worded commit description, patch sent without -w.This patch is easier to review with 'git-diff -w' (--ignore-all-space):
hw/i386/intel_iommu.c +6
-3@@ -987,14 +987,17 @@ static bool vtd_slpte_nonzero_rsvd(uint64_t slpte, uint32_t level)static VTDBus *vtd_find_as_from_bus_num (IntelIOMMUState *s,uint8_t bus_num) {VTDBus *vtd_bus = s->vtd_as_by_bus_num[bus_num]; -if (!vtd_bus) { +GHashTableIter iter; + +if (vtd_bus) { +return vtd_bus; + } +/* * Iterate over the registered buses to find the one which * currently hold this bus number, and update the bus_num * lookup table: */ -GHashTableIter iter; -g_hash_table_iter_init (&iter, s->vtd_as_by_busptr);while (g_hash_table_iter_next (&iter,NULL , (void **)&vtd_bus)) {if (pci_bus_num (vtd_bus->bus) == bus_num) {
hw/i386/intel_iommu.c static VTDBus *vtd_find_as_from_bus_num(IntelIOMMUState *s, uint8_t bus_num) · line 1010
Quoted 9 lines
hw/i386/intel_iommu.c +2
-3@@ -1002,9 +1005,8 @@ static VTDBus *vtd_find_as_from_bus_num(IntelIOMMUState *s, uint8_t bus_num)return vtd_bus; } } - vtd_bus =NULL ; - } -return vtd_bus; + +return NULL ; }
(However we'll need to read below after all... :)
Signature · 1 line
Peter Xu
On Wed, Feb 26, 2020 at 03:20:54PM -0500, Peter Xu wrote:
[...]
Quoted 3 lines
(However we'll need to read below after all... :)
Reviewed-by Peter Xu <[email protected]>
As pointed out by Eric: it seems this patch cannot apply too... I think it's the extra "git diff -w" output that confused git-am.
Phil, would you mind repost without those? My r-b stands if the content keeps the same.
Thanks,
Signature · 1 line
Peter Xu
Paolo Bonzini
AppliedQuoted 86 lines · Philippe Mathieu-Daudé
The
vtd_find_as_from_bus_num()function was introduced (in commitdbaabb25f) in a code format that could return an incorrect pointer, which was later fixed by commita2e1cd41ccf.
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]>---
Since v2: Re-worded commit description, patch sent without -w.This patch is easier to review with 'git-diff -w' (--ignore-all-space):
(quoted diff) +8
-7@@ -987,14 +987,17 @@ static bool vtd_slpte_nonzero_rsvd(uint64_t slpte, uint32_t level) static VTDBus *vtd_find_as_from_bus_num(IntelIOMMUState *s, uint8_t bus_num) { VTDBus *vtd_bus = s->vtd_as_by_bus_num[bus_num]; - if (!vtd_bus) { + GHashTableIter iter; + + if (vtd_bus) { + return vtd_bus; + } + /* * Iterate over the registered buses to find the one which * currently hold this bus number, and update the bus_num * lookup table: */ - GHashTableIter iter; - g_hash_table_iter_init(&iter, s->vtd_as_by_busptr); while (g_hash_table_iter_next(&iter, NULL, (void **)&vtd_bus)) { if (pci_bus_num(vtd_bus->bus) == bus_num) {@@ -1002,9 +1005,8 @@ static VTDBus *vtd_find_as_from_bus_num(IntelIOMMUState *s, uint8_t bus_num) return vtd_bus; } } - vtd_bus = NULL; - } - return vtd_bus; + + return NULL; } --- hw/i386/intel_iommu.c | 34 ++++++++++++++++++---------------- 1 file changed, 18 insertions(+), 16 deletions(-)hw/i386/intel_iommu.c +11
-16@@ -987,24 +987,26 @@ static bool vtd_slpte_nonzero_rsvd(uint64_t slpte, uint32_t level)static VTDBus *vtd_find_as_from_bus_num (IntelIOMMUState *s,uint8_t bus_num) {VTDBus *vtd_bus = s->vtd_as_by_bus_num[bus_num]; -if (!vtd_bus) { -/* -* Iterate over the registered buses to find the one which -* currently hold this bus number, and update the bus_num -* lookup table: -*/ -GHashTableIter iter; +GHashTableIter iter; -g_hash_table_iter_init (&iter, s->vtd_as_by_busptr); -while (g_hash_table_iter_next (&iter,NULL , (void **)&vtd_bus)) { -if (pci_bus_num (vtd_bus->bus) == bus_num) { - s->vtd_as_by_bus_num[bus_num] = vtd_bus; -return vtd_bus; - } - } - vtd_bus =NULL ; +if (vtd_bus) { +return vtd_bus; } -return vtd_bus; + +/* +* Iterate over the registered buses to find the one which +* currently hold this bus number, and update the bus_num +* lookup table: +*/ +g_hash_table_iter_init (&iter, s->vtd_as_by_busptr); +while (g_hash_table_iter_next (&iter,NULL , (void **)&vtd_bus)) {
hw/i386/intel_iommu.c static bool vtd_slpte_nonzero_rsvd(uint64_t slpte, uint32_t level) · line 1012
Quoted 10 lines
hw/i386/intel_iommu.c +7
-0+if (pci_bus_num (vtd_bus->bus) == bus_num) { + s->vtd_as_by_bus_num[bus_num] = vtd_bus; +return vtd_bus; + } + } + +return NULL ; }/* Given the @iova, get relevant @slptep. @slpte_level will be the last level
Queued, thanks.
Paolo