Skip to contentSkip to conversation

xen/vpci: miscellaneous fixes

PATCH In review Roger Pau Monne 4 participants 16 messages Aug 14, 2025 – Sep 10, 2025

lore
Share & tools
lore
#0 lore raw

cover xen/vpci: miscellaneous fixes

Hello,

The following series started with the report in:

https://lore.kernel.org/xen-devel/[email protected]/

And ended up doing a bit of cleanup on the way. I think the overall result is nicer, I never really liked the deferral of the setting of the command and ROM BAR registers, as it makes the logic much harder to follow. I've done some testing, but I will probably do some specific testing to ensure the error paths work as expected - as I don't have a system that trigger those. Posting it ahead because some patches are fairly trivial, and to get feedback on the approach.

Thanks, Roger.

Roger Pau Monne (5):
xen/vpci: purge BAR rangeset contents before use xen/vpci: make BAR mapping more resilient for the hardware domain xen/vpci: simplify handling of memory decoding and ROM enable writes vpci/msix: move MSI-X hole punching as a result of memory decoding
enable
xen/vpci: only check BAR validity once

 xen/drivers/vpci/header.c | 186 ++++++++++++++++----------------------
 xen/include/xen/vpci.h    |   5 +
 2 files changed, 82 insertions(+), 109 deletions(-)
Signature · 1 line
2.49.0
#1 lore raw

PATCH 1/5 xen/vpci: purge BAR rangeset contents before use

It's possible for the BAR rangeset to contain stale data when used in modify_bars() if previous calls to modify_bars() resulted in errors.

Fix by always purging the rangeset before usage, thus getting rid of any previous stale data. Remove the ASSERT that checked whether the rangeset was empty, it was incorrect.

Fixes 52ebde3cfae2 ('vpci/header: program p2m with guest BAR view')
---
 xen/drivers/vpci/header.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)
xen/drivers/vpci/header.c +6 -2
index 469f4977441a..b9756b364300 100644
@@ -338,6 +338,12 @@ static int modify_bars(const struct pci_dev *pdev, uint16_t cmd, bool rom_only)
         if ( !bar->mem )
             continue;
 
+        /*
+         * Empty before use, previous failures might have left stale data in
+         * the rangeset.
+         */
+        rangeset_purge(bar->mem);
+
         if ( !MAPPABLE_BAR(bar) ||
              (rom_only ? bar->type != VPCI_BAR_ROM
                        : (bar->type == VPCI_BAR_ROM && !header->rom_enabled)) ||
@@ -353,8 +359,6 @@ static int modify_bars(const struct pci_dev *pdev, uint16_t cmd, bool rom_only)
             continue;
         }
 
-        ASSERT(rangeset_is_empty(bar->mem));
-
         /*
          * Make sure that the guest set address has the same page offset
          * as the physical address on the host or otherwise things won't work as
Signature · 1 line
2.49.0
Reviewed-by on v1 1/5
#2 lore raw

Re: [PATCH 1/5] xen/vpci: purge BAR rangeset contents before use

Quoted 9 lines · Roger Pau Monne

It's possible for the BAR rangeset to contain stale data when used in modify_bars() if previous calls to modify_bars() resulted in errors.

Fix by always purging the rangeset before usage, thus getting rid of any previous stale data. Remove the ASSERT that checked whether the rangeset was empty, it was incorrect.

Fixes 52ebde3cfae2 ('vpci/header: program p2m with guest BAR view')
#3 lore raw

PATCH 2/5 xen/vpci: make BAR mapping more resilient for the hardware domain

In reply to Roger Pau Monne

The logic in map_range() will bubble up failures to the upper layer, which will result in any remaining regions being skip, and for the non-hardware domain case the owner domain of the device would be destroyed. However for the hardware domain the intent is to continue execution, hopping the failure to modify the p2m could be worked around by the hardware domain.

To accomplish that in a better way, ignore failures and skip the range in that case, possibly continuing to map further ranges.

Since the error path in vpci_process_pending() should only be used by domUs now, and it will unconditionally end up calling domain_crash(), simplify
it: there's no need to cleanup if the domain will be destroyed.

No functional change for domUs intended.

---
 xen/drivers/vpci/header.c | 51 +++++++++++++++++++++------------------
 1 file changed, 28 insertions(+), 23 deletions(-)
xen/drivers/vpci/header.c +28 -23
index b9756b364300..1035dcca242d 100644
@@ -64,7 +64,8 @@ static int cf_check map_range(
             printk(XENLOG_G_WARNING
                    "%pd denied access to MMIO range [%#lx, %#lx]\n",
                    map->d, map_mfn, m_end);
-            return -EPERM;
+            rc = -EPERM;
+            goto done;
         }
 
         rc = xsm_iomem_mapping(XSM_HOOK, map->d, map_mfn, m_end, map->map);
@@ -73,7 +74,7 @@ static int cf_check map_range(
             printk(XENLOG_G_WARNING
                    "%pd XSM denied access to MMIO range [%#lx, %#lx]: %d\n",
                    map->d, map_mfn, m_end, rc);
-            return rc;
+            goto done;
         }
 
         /*
@@ -87,17 +88,27 @@ static int cf_check map_range(
 
         rc = map->map ? map_mmio_regions(map->d, _gfn(s), size, _mfn(map_mfn))
                       : unmap_mmio_regions(map->d, _gfn(s), size, _mfn(map_mfn));
-        if ( rc == 0 )
-        {
-            *c += size;
-            break;
-        }
         if ( rc < 0 )
         {
             printk(XENLOG_G_WARNING
                    "Failed to %smap [%lx %lx] -> [%lx %lx] for %pd: %d\n",
                    map->map ? "" : "un", s, e, map_mfn,
                    map_mfn + size, map->d, rc);
+            goto done;
+        }
+        if ( rc == 0 )
+        {
+ done:
+            if ( is_hardware_domain(map->d) )
+            {
+                /*
+                 * Ignore failures for the hardware domain and skip the range.
+                 * Do it as a best effort workaround to attempt to get the
+                 * hardware domain to boot.
+                 */
+                rc = 0;
+                *c += size;
+            }
             break;
         }
         ASSERT(rc < size);
@@ -213,28 +224,22 @@ bool vpci_process_pending(struct vcpu *v)
             return true;
         }
 
-        if ( rc )
+        if ( rc && !is_hardware_domain(v->domain) )
         {
-            spin_lock(&pdev->vpci->lock);
-            /* Disable memory decoding unconditionally on failure. */
-            modify_decoding(pdev, v->vpci.cmd & ~PCI_COMMAND_MEMORY,
-                            false);
-            spin_unlock(&pdev->vpci->lock);
-
-            /* Clean all the rangesets */
-            for ( i = 0; i < ARRAY_SIZE(header->bars); i++ )
-                if ( !rangeset_is_empty(header->bars[i].mem) )
-                     rangeset_purge(header->bars[i].mem);
-
-            v->vpci.pdev = NULL;
-
             read_unlock(&v->domain->pci_lock);
 
-            if ( !is_hardware_domain(v->domain) )
-                domain_crash(v->domain);
+            domain_crash(v->domain);
 
             return false;
         }
+        ASSERT(!rc);
+        /*
+         * Purge rangeset to deal with the hardware domain having triggered an
+         * error.  It shouldn't be possible, as map_range() will always shallow
+         * errors for hardware domain owned devices, and
+         * rangeset_consume_ranges() itself doesn't generate any errors.
+         */
+        rangeset_purge(bar->mem);
     }
     v->vpci.pdev = NULL;
 
Signature · 1 line
2.49.0
#4 lore raw

Re: [PATCH 2/5] xen/vpci: make BAR mapping more resilient for the hardware domain

Quoted 61 lines · Roger Pau Monne

The logic in map_range() will bubble up failures to the upper layer, which will result in any remaining regions being skip, and for the non-hardware domain case the owner domain of the device would be destroyed. However for the hardware domain the intent is to continue execution, hopping the failure to modify the p2m could be worked around by the hardware domain.

To accomplish that in a better way, ignore failures and skip the range in that case, possibly continuing to map further ranges.

Since the error path in vpci_process_pending() should only be used by domUs now, and it will unconditionally end up calling domain_crash(), simplify
it: there's no need to cleanup if the domain will be destroyed.

No functional change for domUs intended.

---
 xen/drivers/vpci/header.c | 51 +++++++++++++++++++++------------------
 1 file changed, 28 insertions(+), 23 deletions(-)
xen/drivers/vpci/header.c +6 -7
index b9756b364300..1035dcca242d 100644
@@ -64,7 +64,8 @@ static int cf_check map_range(
             printk(XENLOG_G_WARNING
                    "%pd denied access to MMIO range [%#lx, %#lx]\n",
                    map->d, map_mfn, m_end);
-            return -EPERM;
+            rc = -EPERM;
+            goto done;
         }
 
         rc = xsm_iomem_mapping(XSM_HOOK, map->d, map_mfn, m_end, map->map);
@@ -73,7 +74,7 @@ static int cf_check map_range(
             printk(XENLOG_G_WARNING
                    "%pd XSM denied access to MMIO range [%#lx, %#lx]: %d\n",
                    map->d, map_mfn, m_end, rc);
-            return rc;
+            goto done;
         }
 
         /*
@@ -87,17 +88,27 @@ static int cf_check map_range(
 
         rc = map->map ? map_mmio_regions(map->d, _gfn(s), size, _mfn(map_mfn))
                       : unmap_mmio_regions(map->d, _gfn(s), size, _mfn(map_mfn));
-        if ( rc == 0 )
-        {
-            *c += size;
-            break;
-        }
         if ( rc < 0 )
         {
             printk(XENLOG_G_WARNING
                    "Failed to %smap [%lx %lx] -> [%lx %lx] for %pd: %d\n",
                    map->map ? "" : "un", s, e, map_mfn,
                    map_mfn + size, map->d, rc);
+            goto done;
+        }
+        if ( rc == 0 )

xen/drivers/vpci/header.c static int cf_check map_range( · line 109

Quoted 10 lines
xen/drivers/vpci/header.c +10 -0
+        {
+ done:
+            if ( is_hardware_domain(map->d) )
+            {
+                /*
+                 * Ignore failures for the hardware domain and skip the range.
+                 * Do it as a best effort workaround to attempt to get the
+                 * hardware domain to boot.
+                 */
+                rc = 0;

If we return success and size is zero, we will potentially attempt to map/unmap the same region again in an infinite loop. rangeset_consume_ranges would invoke map_range again directly without returning to vpci_process_pending.

xen/drivers/vpci/header.c static int cf_check map_range( · line 110

Quoted 1 line
xen/drivers/vpci/header.c +1 -0
+                *c += size;

This line is now only executed for hwdom, but ...

xen/drivers/vpci/header.c static int cf_check map_range( · line 111

Quoted 1 line
xen/drivers/vpci/header.c +1 -0
+            }

... it should go outside of the hwdom check because domUs still need it.

Quoted 4 lines
xen/drivers/vpci/header.c +0 -0
             break;
         }
         ASSERT(rc < size);

xen/drivers/vpci/header.c bool vpci_process_pending(struct vcpu *v) · line 221

Quoted 10 lines
xen/drivers/vpci/header.c +1 -5
@@ -213,28 +224,22 @@ bool vpci_process_pending(struct vcpu *v)
             return true;
         }
 
-        if ( rc )
+        if ( rc && !is_hardware_domain(v->domain) )
         {
-            spin_lock(&pdev->vpci->lock);
-            /* Disable memory decoding unconditionally on failure. */
-            modify_decoding(pdev, v->vpci.cmd & ~PCI_COMMAND_MEMORY,
-                            false);

This path could be taken for either map or unmap. Do we still want to disable memory decoding in case of unmap?

Quoted 15 lines
xen/drivers/vpci/header.c +1 -11
-            spin_unlock(&pdev->vpci->lock);
-
-            /* Clean all the rangesets */
-            for ( i = 0; i < ARRAY_SIZE(header->bars); i++ )
-                if ( !rangeset_is_empty(header->bars[i].mem) )
-                     rangeset_purge(header->bars[i].mem);
-
-            v->vpci.pdev = NULL;
-
             read_unlock(&v->domain->pci_lock);
 
-            if ( !is_hardware_domain(v->domain) )
-                domain_crash(v->domain);
+            domain_crash(v->domain);
 

xen/drivers/vpci/header.c bool vpci_process_pending(struct vcpu *v) · line 242

Quoted 10 lines
xen/drivers/vpci/header.c +8 -0
             return false;
         }
+        ASSERT(!rc);
+        /*
+         * Purge rangeset to deal with the hardware domain having triggered an
+         * error.  It shouldn't be possible, as map_range() will always shallow
+         * errors for hardware domain owned devices, and
+         * rangeset_consume_ranges() itself doesn't generate any errors.
+         */
+        rangeset_purge(bar->mem);

Reiterating what was mentioned above: if map_range returned 0 without incrementing *c, we won't make it back here.

Quoted 3 lines
xen/drivers/vpci/header.c +0 -0
     }
     v->vpci.pdev = NULL;
 
#5 lore raw

Re: [PATCH 2/5] xen/vpci: make BAR mapping more resilient for the hardware domain

Quoted 68 lines · Stewart Hildebrand
Quoted 67 lines · Roger Pau Monne

The logic in map_range() will bubble up failures to the upper layer, which will result in any remaining regions being skip, and for the non-hardware domain case the owner domain of the device would be destroyed. However for the hardware domain the intent is to continue execution, hopping the failure to modify the p2m could be worked around by the hardware domain.

To accomplish that in a better way, ignore failures and skip the range in that case, possibly continuing to map further ranges.

Since the error path in vpci_process_pending() should only be used by domUs now, and it will unconditionally end up calling domain_crash(), simplify
it: there's no need to cleanup if the domain will be destroyed.

No functional change for domUs intended.

---
 xen/drivers/vpci/header.c | 51 +++++++++++++++++++++------------------
 1 file changed, 28 insertions(+), 23 deletions(-)
xen/drivers/vpci/header.c +12 -7
index b9756b364300..1035dcca242d 100644
@@ -64,7 +64,8 @@ static int cf_check map_range(
             printk(XENLOG_G_WARNING
                    "%pd denied access to MMIO range [%#lx, %#lx]\n",
                    map->d, map_mfn, m_end);
-            return -EPERM;
+            rc = -EPERM;
+            goto done;
         }
 
         rc = xsm_iomem_mapping(XSM_HOOK, map->d, map_mfn, m_end, map->map);
@@ -73,7 +74,7 @@ static int cf_check map_range(
             printk(XENLOG_G_WARNING
                    "%pd XSM denied access to MMIO range [%#lx, %#lx]: %d\n",
                    map->d, map_mfn, m_end, rc);
-            return rc;
+            goto done;
         }
 
         /*
@@ -87,17 +88,27 @@ static int cf_check map_range(
 
         rc = map->map ? map_mmio_regions(map->d, _gfn(s), size, _mfn(map_mfn))
                       : unmap_mmio_regions(map->d, _gfn(s), size, _mfn(map_mfn));
-        if ( rc == 0 )
-        {
-            *c += size;
-            break;
-        }
         if ( rc < 0 )
         {
             printk(XENLOG_G_WARNING
                    "Failed to %smap [%lx %lx] -> [%lx %lx] for %pd: %d\n",
                    map->map ? "" : "un", s, e, map_mfn,
                    map_mfn + size, map->d, rc);
+            goto done;
+        }
+        if ( rc == 0 )
+        {
+ done:
+            if ( is_hardware_domain(map->d) )
+            {
+                /*
+                 * Ignore failures for the hardware domain and skip the range.
Quoted 8 lines

xen/drivers/vpci/header.c static int cf_check map_range( · line 109

Quoted 4 lines
xen/drivers/vpci/header.c +4 -0
+                 * Do it as a best effort workaround to attempt to get the
+                 * hardware domain to boot.
+                 */
+                rc = 0;

If we return success and size is zero, we will potentially attempt to map/unmap the same region again in an infinite loop. rangeset_consume_ranges would invoke map_range again directly without returning to vpci_process_pending.

Sorry, I sent the previous email too soon, I see now that it shouldn't be possible for size to be zero.

Quoted 59 lines

xen/drivers/vpci/header.c static int cf_check map_range( · line 110

Quoted 2 lines
xen/drivers/vpci/header.c +1 -0
+                *c += size;

This line is now only executed for hwdom, but ...

xen/drivers/vpci/header.c static int cf_check map_range( · line 111

Quoted 1 line
xen/drivers/vpci/header.c +1 -0
+            }

... it should go outside of the hwdom check because domUs still need it.

Quoted 4 lines
xen/drivers/vpci/header.c +0 -0
             break;
         }
         ASSERT(rc < size);

xen/drivers/vpci/header.c bool vpci_process_pending(struct vcpu *v) · line 221

Quoted 10 lines
xen/drivers/vpci/header.c +1 -5
@@ -213,28 +224,22 @@ bool vpci_process_pending(struct vcpu *v)
             return true;
         }
 
-        if ( rc )
+        if ( rc && !is_hardware_domain(v->domain) )
         {
-            spin_lock(&pdev->vpci->lock);
-            /* Disable memory decoding unconditionally on failure. */
-            modify_decoding(pdev, v->vpci.cmd & ~PCI_COMMAND_MEMORY,
-                            false);

This path could be taken for either map or unmap. Do we still want to disable memory decoding in case of unmap?

Quoted 15 lines
xen/drivers/vpci/header.c +1 -11
-            spin_unlock(&pdev->vpci->lock);
-
-            /* Clean all the rangesets */
-            for ( i = 0; i < ARRAY_SIZE(header->bars); i++ )
-                if ( !rangeset_is_empty(header->bars[i].mem) )
-                     rangeset_purge(header->bars[i].mem);
-
-            v->vpci.pdev = NULL;
-
             read_unlock(&v->domain->pci_lock);
 
-            if ( !is_hardware_domain(v->domain) )
-                domain_crash(v->domain);
+            domain_crash(v->domain);
 

xen/drivers/vpci/header.c bool vpci_process_pending(struct vcpu *v) · line 242

Quoted 10 lines
xen/drivers/vpci/header.c +8 -0
             return false;
         }
+        ASSERT(!rc);
+        /*
+         * Purge rangeset to deal with the hardware domain having triggered an
+         * error.  It shouldn't be possible, as map_range() will always shallow
+         * errors for hardware domain owned devices, and
+         * rangeset_consume_ranges() itself doesn't generate any errors.
+         */
+        rangeset_purge(bar->mem);

Reiterating what was mentioned above: if map_range returned 0 without incrementing *c, we won't make it back here.

Quoted 3 lines
xen/drivers/vpci/header.c +0 -0
     }
     v->vpci.pdev = NULL;
 
#6 lore raw

Re: [PATCH 2/5] xen/vpci: make BAR mapping more resilient for the hardware domain

In reply to Stewart Hildebrand
Quoted 92 lines · Stewart Hildebrand
Quoted 61 lines · Roger Pau Monne

The logic in map_range() will bubble up failures to the upper layer, which will result in any remaining regions being skip, and for the non-hardware domain case the owner domain of the device would be destroyed. However for the hardware domain the intent is to continue execution, hopping the failure to modify the p2m could be worked around by the hardware domain.

To accomplish that in a better way, ignore failures and skip the range in that case, possibly continuing to map further ranges.

Since the error path in vpci_process_pending() should only be used by domUs now, and it will unconditionally end up calling domain_crash(), simplify
it: there's no need to cleanup if the domain will be destroyed.

No functional change for domUs intended.

---
 xen/drivers/vpci/header.c | 51 +++++++++++++++++++++------------------
 1 file changed, 28 insertions(+), 23 deletions(-)
xen/drivers/vpci/header.c +6 -7
index b9756b364300..1035dcca242d 100644
@@ -64,7 +64,8 @@ static int cf_check map_range(
             printk(XENLOG_G_WARNING
                    "%pd denied access to MMIO range [%#lx, %#lx]\n",
                    map->d, map_mfn, m_end);
-            return -EPERM;
+            rc = -EPERM;
+            goto done;
         }
 
         rc = xsm_iomem_mapping(XSM_HOOK, map->d, map_mfn, m_end, map->map);
@@ -73,7 +74,7 @@ static int cf_check map_range(
             printk(XENLOG_G_WARNING
                    "%pd XSM denied access to MMIO range [%#lx, %#lx]: %d\n",
                    map->d, map_mfn, m_end, rc);
-            return rc;
+            goto done;
         }
 
         /*
@@ -87,17 +88,27 @@ static int cf_check map_range(
 
         rc = map->map ? map_mmio_regions(map->d, _gfn(s), size, _mfn(map_mfn))
                       : unmap_mmio_regions(map->d, _gfn(s), size, _mfn(map_mfn));
-        if ( rc == 0 )
-        {
-            *c += size;
-            break;
-        }
         if ( rc < 0 )
         {
             printk(XENLOG_G_WARNING
                    "Failed to %smap [%lx %lx] -> [%lx %lx] for %pd: %d\n",
                    map->map ? "" : "un", s, e, map_mfn,
                    map_mfn + size, map->d, rc);
+            goto done;
+        }
+        if ( rc == 0 )

xen/drivers/vpci/header.c static int cf_check map_range( · line 109

Quoted 10 lines
xen/drivers/vpci/header.c +10 -0
+        {
+ done:
+            if ( is_hardware_domain(map->d) )
+            {
+                /*
+                 * Ignore failures for the hardware domain and skip the range.
+                 * Do it as a best effort workaround to attempt to get the
+                 * hardware domain to boot.
+                 */
+                rc = 0;

If we return success and size is zero, we will potentially attempt to map/unmap the same region again in an infinite loop. rangeset_consume_ranges would invoke map_range again directly without returning to vpci_process_pending.

xen/drivers/vpci/header.c static int cf_check map_range( · line 110

Quoted 1 line
xen/drivers/vpci/header.c +1 -0
+                *c += size;

This line is now only executed for hwdom, but ...

xen/drivers/vpci/header.c static int cf_check map_range( · line 111

Quoted 1 line
xen/drivers/vpci/header.c +1 -0
+            }

... it should go outside of the hwdom check because domUs still need it.

Quoted 7 lines
xen/drivers/vpci/header.c +0 -0
             break;
         }
         ASSERT(rc < size);
@@ -213,28 +224,22 @@ bool vpci_process_pending(struct vcpu *v)
             return true;
         }
 
Quoted 10 lines

xen/drivers/vpci/header.c bool vpci_process_pending(struct vcpu *v) · line 221

Quoted 7 lines
xen/drivers/vpci/header.c +1 -5
-        if ( rc )
+        if ( rc && !is_hardware_domain(v->domain) )
         {
-            spin_lock(&pdev->vpci->lock);
-            /* Disable memory decoding unconditionally on failure. */
-            modify_decoding(pdev, v->vpci.cmd & ~PCI_COMMAND_MEMORY,
-                            false);

This path could be taken for either map or unmap. Do we still want to disable memory decoding in case of unmap?

Does it make an effective difference? For the hardware domain we should never get here, and for domUs the domain will be destroyed, so disabling memory decoding is not helpful?

Thanks, Roger.

#7 lore raw

Re: [PATCH 2/5] xen/vpci: make BAR mapping more resilient for the hardware domain

Quoted 26 lines · Roger Pau Monné
Quoted 25 lines · Stewart Hildebrand
Quoted 24 lines · Roger Pau Monne

The logic in map_range() will bubble up failures to the upper layer, which will result in any remaining regions being skip, and for the non-hardware domain case the owner domain of the device would be destroyed. However for the hardware domain the intent is to continue execution, hopping the failure to modify the p2m could be worked around by the hardware domain.

To accomplish that in a better way, ignore failures and skip the range in that case, possibly continuing to map further ranges.

Since the error path in vpci_process_pending() should only be used by domUs now, and it will unconditionally end up calling domain_crash(), simplify
it: there's no need to cleanup if the domain will be destroyed.

No functional change for domUs intended.

---
 xen/drivers/vpci/header.c | 51 +++++++++++++++++++++------------------
 1 file changed, 28 insertions(+), 23 deletions(-)
xen/drivers/vpci/header.c +0 -0
index b9756b364300..1035dcca242d 100644

<snip>

Quoted 15 lines
Quoted 4 lines
Quoted 4 lines
xen/drivers/vpci/header.c +0 -0
@@ -213,28 +224,22 @@ bool vpci_process_pending(struct vcpu *v)
             return true;
         }
 
Quoted 10 lines

xen/drivers/vpci/header.c bool vpci_process_pending(struct vcpu *v) · line 221

Quoted 7 lines
xen/drivers/vpci/header.c +1 -5
-        if ( rc )
+        if ( rc && !is_hardware_domain(v->domain) )
         {
-            spin_lock(&pdev->vpci->lock);
-            /* Disable memory decoding unconditionally on failure. */
-            modify_decoding(pdev, v->vpci.cmd & ~PCI_COMMAND_MEMORY,
-                            false);

This path could be taken for either map or unmap. Do we still want to disable memory decoding in case of unmap?

Quoted 3 lines

Does it make an effective difference? For the hardware domain we should never get here, and for domUs the domain will be destroyed, so disabling memory decoding is not helpful?

Since the domU will be destroyed, the PCI device will get assigned back to hwdom or put into quarantine. In case of quarantine, it shouldn't matter. In case of assignment back to hwdom, I think by keeping memory decoding enabled it could potentially trigger additional p2m operations.

In any case, I don't have a strong opinion on disabling memory decoding vs not, but I think the commit message ought to mention something about it.

#8 lore raw

Re: [PATCH 2/5] xen/vpci: make BAR mapping more resilient for the hardware domain

In reply to Stewart Hildebrand
Quoted 77 lines · Stewart Hildebrand
Quoted 61 lines · Roger Pau Monne

The logic in map_range() will bubble up failures to the upper layer, which will result in any remaining regions being skip, and for the non-hardware domain case the owner domain of the device would be destroyed. However for the hardware domain the intent is to continue execution, hopping the failure to modify the p2m could be worked around by the hardware domain.

To accomplish that in a better way, ignore failures and skip the range in that case, possibly continuing to map further ranges.

Since the error path in vpci_process_pending() should only be used by domUs now, and it will unconditionally end up calling domain_crash(), simplify
it: there's no need to cleanup if the domain will be destroyed.

No functional change for domUs intended.

---
 xen/drivers/vpci/header.c | 51 +++++++++++++++++++++------------------
 1 file changed, 28 insertions(+), 23 deletions(-)
xen/drivers/vpci/header.c +6 -7
index b9756b364300..1035dcca242d 100644
@@ -64,7 +64,8 @@ static int cf_check map_range(
             printk(XENLOG_G_WARNING
                    "%pd denied access to MMIO range [%#lx, %#lx]\n",
                    map->d, map_mfn, m_end);
-            return -EPERM;
+            rc = -EPERM;
+            goto done;
         }
 
         rc = xsm_iomem_mapping(XSM_HOOK, map->d, map_mfn, m_end, map->map);
@@ -73,7 +74,7 @@ static int cf_check map_range(
             printk(XENLOG_G_WARNING
                    "%pd XSM denied access to MMIO range [%#lx, %#lx]: %d\n",
                    map->d, map_mfn, m_end, rc);
-            return rc;
+            goto done;
         }
 
         /*
@@ -87,17 +88,27 @@ static int cf_check map_range(
 
         rc = map->map ? map_mmio_regions(map->d, _gfn(s), size, _mfn(map_mfn))
                       : unmap_mmio_regions(map->d, _gfn(s), size, _mfn(map_mfn));
-        if ( rc == 0 )
-        {
-            *c += size;
-            break;
-        }
         if ( rc < 0 )
         {
             printk(XENLOG_G_WARNING
                    "Failed to %smap [%lx %lx] -> [%lx %lx] for %pd: %d\n",
                    map->map ? "" : "un", s, e, map_mfn,
                    map_mfn + size, map->d, rc);
+            goto done;
+        }
+        if ( rc == 0 )

xen/drivers/vpci/header.c static int cf_check map_range( · line 109

Quoted 10 lines
xen/drivers/vpci/header.c +10 -0
+        {
+ done:
+            if ( is_hardware_domain(map->d) )
+            {
+                /*
+                 * Ignore failures for the hardware domain and skip the range.
+                 * Do it as a best effort workaround to attempt to get the
+                 * hardware domain to boot.
+                 */
+                rc = 0;

If we return success and size is zero, we will potentially attempt to map/unmap the same region again in an infinite loop. rangeset_consume_ranges would invoke map_range again directly without returning to vpci_process_pending.

Quoted 7 lines

xen/drivers/vpci/header.c static int cf_check map_range( · line 110

Quoted 1 line
xen/drivers/vpci/header.c +1 -0
+                *c += size;

This line is now only executed for hwdom, but ...

xen/drivers/vpci/header.c static int cf_check map_range( · line 111

Quoted 1 line
xen/drivers/vpci/header.c +1 -0
+            }

... it should go outside of the hwdom check because domUs still need it.

Indeed, this should be:

            if ( is_hardware_domain(map->d) )
                /*
                 * Ignore failures for the hardware domain and skip the range.
                 * Do it as a best effort workaround to attempt to get the
                 * hardware domain to boot.
                 */
                rc = 0;

            *c += size;
            break;

Otherwise domU won't make progress. It would be helpful to have some domU testing in the CI loop, otherwise I have no way to test the domU side when modifying vPCI.

Thanks, Roger.

#9 lore raw

PATCH 3/5 xen/vpci: simplify handling of memory decoding and ROM enable writes

In reply to Roger Pau Monne

Deferring the actual write of the PCI register bit, either the memory decoding or the ROM enable is not helpful, and adds an unnecessary amount of complexity to the preemptible handling of BAR related p2m modifications.

In the hardware domain case, whether the PCI register write is done ahead or after the p2m changes doesn't matter, a hardware domain has plenty of ways to mess with the PCI register state if it wants to. Any poking at the BAR p2m regions ahead of the guest write having completed will be undefined.

On the other hand, for domUs the memory decoding bit shouldn't really change as a result of guest actions, and should always be enabled. Guest toggling the memory decoding command register should only result in p2m modifications, but no propagation to the device PCI registers. Having memory decoding unconditionally enabled ensures the domU attempting to perform p2m accesses while the p2m changes are taking place will always access the BAR contents. This is not the current behavior for domUs, so add a note that it would preferably done that way.

---
 xen/drivers/vpci/header.c | 42 +++++++++++++++++++--------------------
 1 file changed, 21 insertions(+), 21 deletions(-)
xen/drivers/vpci/header.c +21 -21
index 1035dcca242d..1a501a0ba47e 100644
@@ -163,8 +163,6 @@ static void modify_decoding(const struct pci_dev *pdev, uint16_t cmd,
             if ( pci_check_bar(pdev, _mfn(PFN_DOWN(bar->addr)),
                                _mfn(PFN_DOWN(bar->addr + bar->size - 1))) )
                 bar->enabled = map;
-            header->rom_enabled = map;
-            pci_conf_write32(pdev->sbdf, rom_pos, val);
             return;
         }
 
@@ -174,14 +172,6 @@ static void modify_decoding(const struct pci_dev *pdev, uint16_t cmd,
                            _mfn(PFN_DOWN(bar->addr + bar->size - 1))) )
             bar->enabled = map;
     }
-
-    if ( !rom_only )
-    {
-        pci_conf_write16(pdev->sbdf, PCI_COMMAND, cmd);
-        header->bars_mapped = map;
-    }
-    else
-        ASSERT_UNREACHABLE();
 }
 
 bool vpci_process_pending(struct vcpu *v)
@@ -547,16 +537,23 @@ static void cf_check cmd_write(
      * decoding one. Bits that are not allowed for DomU are already
      * handled above and by the rsvdp_mask.
      */
-    if ( header->bars_mapped != !!(cmd & PCI_COMMAND_MEMORY) )
+    if ( header->bars_mapped != !!(cmd & PCI_COMMAND_MEMORY) &&
+         modify_bars(pdev, cmd, false) )
         /*
          * Ignore the error. No memory has been added or removed from the p2m
          * (because the actual p2m changes are deferred in defer_map) and the
          * memory decoding bit has not been changed, so leave everything as-is,
          * hoping the guest will realize and try again.
          */
-        modify_bars(pdev, cmd, false);
-    else
-        pci_conf_write16(pdev->sbdf, reg, cmd);
+        return;
+
+    /*
+     * FIXME: for domUs we don't want the guest toggling the memory decoding
+     * bit.  It should be set in vpci_init_header() and guest attempts to
+     * modify it should only lead to guest p2m changes.
+     */
+    header->bars_mapped = cmd & PCI_COMMAND_MEMORY;
+    pci_conf_write16(pdev->sbdf, reg, cmd);
 }
 
 static uint32_t cf_check guest_cmd_read(
@@ -712,17 +709,11 @@ static void cf_check rom_write(
         rom->guest_addr = rom->addr;
     }
 
-    if ( !header->bars_mapped || rom->enabled == new_enabled )
-    {
-        /* Just update the ROM BAR field. */
-        header->rom_enabled = new_enabled;
-        pci_conf_write32(pdev->sbdf, reg, val);
-    }
     /*
      * Pass PCI_COMMAND_MEMORY or 0 to signal a map/unmap request, note that
      * this fabricated command is never going to be written to the register.
      */
-    else if ( modify_bars(pdev, new_enabled ? PCI_COMMAND_MEMORY : 0, true) )
+    if ( modify_bars(pdev, new_enabled ? PCI_COMMAND_MEMORY : 0, true) )
         /*
          * No memory has been added or removed from the p2m (because the actual
          * p2m changes are deferred in defer_map) and the ROM enable bit has
@@ -733,6 +724,8 @@ static void cf_check rom_write(
          */
         return;
 
+    header->rom_enabled = new_enabled;
+    pci_conf_write32(pdev->sbdf, reg, val);
     if ( !new_enabled )
     {
         rom->addr = val & PCI_ROM_ADDRESS_MASK;
@@ -1057,6 +1050,13 @@ int vpci_init_header(struct pci_dev *pdev)
             goto fail;
     }
 
+    if ( cmd & PCI_COMMAND_MEMORY )
+    {
+        /* Restore command register value. */
+        header->bars_mapped = true;
+        pci_conf_write16(pdev->sbdf, PCI_COMMAND, cmd);
+    }
+
     return (cmd & PCI_COMMAND_MEMORY) ? modify_bars(pdev, cmd, false) : 0;
 
  fail:
Signature · 1 line
2.49.0
#10 lore raw

Re: [PATCH 3/5] xen/vpci: simplify handling of memory decoding and ROM enable writes

Quoted 10 lines · Roger Pau Monne

Deferring the actual write of the PCI register bit, either the memory decoding or the ROM enable is not helpful, and adds an unnecessary amount of complexity to the preemptible handling of BAR related p2m modifications.

In the hardware domain case, whether the PCI register write is done ahead or after the p2m changes doesn't matter, a hardware domain has plenty of ways to mess with the PCI register state if it wants to. Any poking at the BAR p2m regions ahead of the guest write having completed will be undefined.

Quoted 9 lines

On the other hand, for domUs the memory decoding bit shouldn't really change as a result of guest actions, and should always be enabled. Guest toggling the memory decoding command register should only result in p2m modifications, but no propagation to the device PCI registers. Having memory decoding unconditionally enabled ensures the domU attempting to perform p2m accesses while the p2m changes are taking place will always access the BAR contents. This is not the current behavior for domUs, so add a note that it would preferably done that way.

Nit: I think you want:
Resolves: https://gitlab.com/xen-project/xen/-/issues/98

Quoted 10 lines
---
 xen/drivers/vpci/header.c | 42 +++++++++++++++++++--------------------
 1 file changed, 21 insertions(+), 21 deletions(-)
xen/drivers/vpci/header.c +0 -0
index 1035dcca242d..1a501a0ba47e 100644

xen/drivers/vpci/header.c static void modify_decoding(const struct pci_dev *pdev, uint16_t cmd, · line 167

Quoted 5 lines
xen/drivers/vpci/header.c +0 -2
@@ -163,8 +163,6 @@ static void modify_decoding(const struct pci_dev *pdev, uint16_t cmd,
             if ( pci_check_bar(pdev, _mfn(PFN_DOWN(bar->addr)),
                                _mfn(PFN_DOWN(bar->addr + bar->size - 1))) )
                 bar->enabled = map;
-            header->rom_enabled = map;
-            pci_conf_write32(pdev->sbdf, rom_pos, val);

rom_pos and val are unused

drivers/vpci/header.c: In function ‘modify_decoding’:
drivers/vpci/header.c:160:22: error: unused variable ‘val’ [-Werror=unused-variable]
160 | uint32_t val = bar->addr |
| ^~~
drivers/vpci/header.c:158:26: error: unused variable ‘rom_pos’ [-Werror=unused-variable]
158 | unsigned int rom_pos = (i == PCI_HEADER_NORMAL_NR_BARS)
| ^~~~~~~

Other than that the patch looks good to me

Quoted 88 lines
xen/drivers/vpci/header.c +21 -19
             return;
         }
 
@@ -174,14 +172,6 @@ static void modify_decoding(const struct pci_dev *pdev, uint16_t cmd,
                            _mfn(PFN_DOWN(bar->addr + bar->size - 1))) )
             bar->enabled = map;
     }
-
-    if ( !rom_only )
-    {
-        pci_conf_write16(pdev->sbdf, PCI_COMMAND, cmd);
-        header->bars_mapped = map;
-    }
-    else
-        ASSERT_UNREACHABLE();
 }
 
 bool vpci_process_pending(struct vcpu *v)
@@ -547,16 +537,23 @@ static void cf_check cmd_write(
      * decoding one. Bits that are not allowed for DomU are already
      * handled above and by the rsvdp_mask.
      */
-    if ( header->bars_mapped != !!(cmd & PCI_COMMAND_MEMORY) )
+    if ( header->bars_mapped != !!(cmd & PCI_COMMAND_MEMORY) &&
+         modify_bars(pdev, cmd, false) )
         /*
          * Ignore the error. No memory has been added or removed from the p2m
          * (because the actual p2m changes are deferred in defer_map) and the
          * memory decoding bit has not been changed, so leave everything as-is,
          * hoping the guest will realize and try again.
          */
-        modify_bars(pdev, cmd, false);
-    else
-        pci_conf_write16(pdev->sbdf, reg, cmd);
+        return;
+
+    /*
+     * FIXME: for domUs we don't want the guest toggling the memory decoding
+     * bit.  It should be set in vpci_init_header() and guest attempts to
+     * modify it should only lead to guest p2m changes.
+     */
+    header->bars_mapped = cmd & PCI_COMMAND_MEMORY;
+    pci_conf_write16(pdev->sbdf, reg, cmd);
 }
 
 static uint32_t cf_check guest_cmd_read(
@@ -712,17 +709,11 @@ static void cf_check rom_write(
         rom->guest_addr = rom->addr;
     }
 
-    if ( !header->bars_mapped || rom->enabled == new_enabled )
-    {
-        /* Just update the ROM BAR field. */
-        header->rom_enabled = new_enabled;
-        pci_conf_write32(pdev->sbdf, reg, val);
-    }
     /*
      * Pass PCI_COMMAND_MEMORY or 0 to signal a map/unmap request, note that
      * this fabricated command is never going to be written to the register.
      */
-    else if ( modify_bars(pdev, new_enabled ? PCI_COMMAND_MEMORY : 0, true) )
+    if ( modify_bars(pdev, new_enabled ? PCI_COMMAND_MEMORY : 0, true) )
         /*
          * No memory has been added or removed from the p2m (because the actual
          * p2m changes are deferred in defer_map) and the ROM enable bit has
@@ -733,6 +724,8 @@ static void cf_check rom_write(
          */
         return;
 
+    header->rom_enabled = new_enabled;
+    pci_conf_write32(pdev->sbdf, reg, val);
     if ( !new_enabled )
     {
         rom->addr = val & PCI_ROM_ADDRESS_MASK;
@@ -1057,6 +1050,13 @@ int vpci_init_header(struct pci_dev *pdev)
             goto fail;
     }
 
+    if ( cmd & PCI_COMMAND_MEMORY )
+    {
+        /* Restore command register value. */
+        header->bars_mapped = true;
+        pci_conf_write16(pdev->sbdf, PCI_COMMAND, cmd);
+    }
+
     return (cmd & PCI_COMMAND_MEMORY) ? modify_bars(pdev, cmd, false) : 0;
 
  fail:
#11 lore raw

PATCH 4/5 vpci/msix: move MSI-X hole punching as a result of memory decoding enable

In reply to Roger Pau Monne

Deferring the p2m unpopulate of the MSI-X table region to after the BAR are mapped in the p2m is not needed. The aim of vpci_make_msix_hole() is to cope with the BAR containing the MSI-X table being positioned over a reserved e820 region, by making sure the MSI-X table region is unpopulated on the p2m. Note that reserved e820 regions are identity mapped in the hardware domain p2m by default.

Doing it before or after the BAR p2m changes are done is irrelevant, hence do it ahead of the p2m changes, as that's simpler.

---
 xen/drivers/vpci/header.c | 18 +++++-------------
 1 file changed, 5 insertions(+), 13 deletions(-)
xen/drivers/vpci/header.c +5 -13
index 1a501a0ba47e..255c6d54b406 100644
@@ -133,19 +133,6 @@ static void modify_decoding(const struct pci_dev *pdev, uint16_t cmd,
     bool map = cmd & PCI_COMMAND_MEMORY;
     unsigned int i;
 
-    /*
-     * Make sure there are no mappings in the MSIX MMIO areas, so that accesses
-     * can be trapped (and emulated) by Xen when the memory decoding bit is
-     * enabled.
-     *
-     * FIXME: punching holes after the p2m has been set up might be racy for
-     * DomU usage, needs to be revisited.
-     */
-#ifdef CONFIG_HAS_PCI_MSI
-    if ( map && !rom_only && vpci_make_msix_hole(pdev) )
-        return;
-#endif
-
     for ( i = 0; i < ARRAY_SIZE(header->bars); i++ )
     {
         struct vpci_bar *bar = &header->bars[i];
@@ -547,6 +534,11 @@ static void cf_check cmd_write(
          */
         return;
 
+#ifdef CONFIG_HAS_PCI_MSI
+    /* Unpopulate MSI-X table region, so accesses trap into Xen. */
+    if ( (cmd & PCI_COMMAND_MEMORY) && vpci_make_msix_hole(pdev) )
+        return;
+#endif
     /*
      * FIXME: for domUs we don't want the guest toggling the memory decoding
      * bit.  It should be set in vpci_init_header() and guest attempts to
Signature · 1 line
2.49.0
Reviewed-by on v1 4/5
#12 lore raw

Re: [PATCH 4/5] vpci/msix: move MSI-X hole punching as a result of memory decoding enable

Quoted 2 lines · Roger Pau Monne

Deferring the p2m unpopulate of the MSI-X table region to after the BAR are mapped in the p2m is not needed.

For my own understanding: This is because we would never actively map such a region; we always subtract it out of what is going to be mapped. If so ...

Quoted 10 lines

The aim of vpci_make_msix_hole() is to
cope with the BAR containing the MSI-X table being positioned over a reserved e820 region, by making sure the MSI-X table region is unpopulated on the p2m. Note that reserved e820 regions are identity mapped in the hardware domain p2m by default.

Doing it before or after the BAR p2m changes are done is irrelevant, hence do it ahead of the p2m changes, as that's simpler.

Jan

#13 lore raw

PATCH 5/5 xen/vpci: only check BAR validity once

In reply to Roger Pau Monne

Avoid multiple calls to pci_check_bar() for the same memory decoding related operation, as each call can possibly print a warning message avoid a BAR being in an invalid position.

Store whether the BAR is validly positioned in modify_bars(), and used that cached value for the whole operation. This allows to get rid of modify_decoding(), as the setting of whether the BAR is enabled or not can be easily done in vpci_process_pending() itself, without the need for an external helper.

Fixes 4acab25a9300 ('x86/vpci: fix handling of BAR overlaps with non-hole regions')
---
 xen/drivers/vpci/header.c | 69 ++++++++++-----------------------------
 xen/include/xen/vpci.h    |  5 +++
 2 files changed, 23 insertions(+), 51 deletions(-)
xen/drivers/vpci/header.c +18 -51
index 255c6d54b406..2071bed81676 100644
@@ -121,46 +121,6 @@ static int cf_check map_range(
     return rc;
 }
 
-/*
- * The rom_only parameter is used to signal the map/unmap helpers that the ROM
- * BAR's enable bit has changed with the memory decoding bit already enabled.
- * If rom_only is not set then it's the memory decoding bit that changed.
- */
-static void modify_decoding(const struct pci_dev *pdev, uint16_t cmd,
-                            bool rom_only)
-{
-    struct vpci_header *header = &pdev->vpci->header;
-    bool map = cmd & PCI_COMMAND_MEMORY;
-    unsigned int i;
-
-    for ( i = 0; i < ARRAY_SIZE(header->bars); i++ )
-    {
-        struct vpci_bar *bar = &header->bars[i];
-
-        if ( !MAPPABLE_BAR(bar) )
-            continue;
-
-        if ( rom_only && bar->type == VPCI_BAR_ROM )
-        {
-            unsigned int rom_pos = (i == PCI_HEADER_NORMAL_NR_BARS)
-                                   ? PCI_ROM_ADDRESS : PCI_ROM_ADDRESS1;
-            uint32_t val = bar->addr |
-                           (map ? PCI_ROM_ADDRESS_ENABLE : 0);
-
-            if ( pci_check_bar(pdev, _mfn(PFN_DOWN(bar->addr)),
-                               _mfn(PFN_DOWN(bar->addr + bar->size - 1))) )
-                bar->enabled = map;
-            return;
-        }
-
-        if ( !rom_only &&
-             (bar->type != VPCI_BAR_ROM || header->rom_enabled) &&
-             pci_check_bar(pdev, _mfn(PFN_DOWN(bar->addr)),
-                           _mfn(PFN_DOWN(bar->addr + bar->size - 1))) )
-            bar->enabled = map;
-    }
-}
-
 bool vpci_process_pending(struct vcpu *v)
 {
     const struct pci_dev *pdev = v->vpci.pdev;
@@ -190,8 +150,10 @@ bool vpci_process_pending(struct vcpu *v)
         };
         int rc;
 
+        ASSERT(bar->valid || rangeset_is_empty(bar->mem));
+
         if ( rangeset_is_empty(bar->mem) )
-            continue;
+            goto next;
 
         rc = rangeset_consume_ranges(bar->mem, map_range, &data);
 
@@ -217,13 +179,13 @@ bool vpci_process_pending(struct vcpu *v)
          * rangeset_consume_ranges() itself doesn't generate any errors.
          */
         rangeset_purge(bar->mem);
+
+ next:
+        if ( bar->valid )
+            bar->enabled = v->vpci.cmd & PCI_COMMAND_MEMORY;
     }
     v->vpci.pdev = NULL;
 
-    spin_lock(&pdev->vpci->lock);
-    modify_decoding(pdev, v->vpci.cmd, v->vpci.rom_only);
-    spin_unlock(&pdev->vpci->lock);
-
     read_unlock(&v->domain->pci_lock);
 
     return false;
@@ -243,10 +205,8 @@ static int __init apply_map(struct domain *d, const struct pci_dev *pdev,
         struct vpci_bar *bar = &header->bars[i];
         struct map_data data = { .d = d, .map = true, .bar = bar };
 
-        if ( rangeset_is_empty(bar->mem) )
-            continue;
-
-        while ( (rc = rangeset_consume_ranges(bar->mem, map_range,
+        while ( bar->mem &&
+                (rc = rangeset_consume_ranges(bar->mem, map_range,
                                               &data)) == -ERESTART )
         {
             /*
@@ -258,9 +218,10 @@ static int __init apply_map(struct domain *d, const struct pci_dev *pdev,
             process_pending_softirqs();
             write_lock(&d->pci_lock);
         }
+
+        if ( bar->valid )
+            bar->enabled = true;
     }
-    if ( !rc )
-        modify_decoding(pdev, cmd, false);
 
     return rc;
 }
@@ -326,6 +287,9 @@ static int modify_bars(const struct pci_dev *pdev, uint16_t cmd, bool rom_only)
          */
         rangeset_purge(bar->mem);
 
+        /* Reset whether the BAR is valid, will be checked below. */
+        bar->valid = false;
+
         if ( !MAPPABLE_BAR(bar) ||
              (rom_only ? bar->type != VPCI_BAR_ROM
                        : (bar->type == VPCI_BAR_ROM && !header->rom_enabled)) ||
@@ -341,6 +305,8 @@ static int modify_bars(const struct pci_dev *pdev, uint16_t cmd, bool rom_only)
             continue;
         }
 
+        bar->valid = true;
+
         /*
          * Make sure that the guest set address has the same page offset
          * as the physical address on the host or otherwise things won't work as
@@ -539,6 +505,7 @@ static void cf_check cmd_write(
     if ( (cmd & PCI_COMMAND_MEMORY) && vpci_make_msix_hole(pdev) )
         return;
 #endif
+
     /*
      * FIXME: for domUs we don't want the guest toggling the memory decoding
      * bit.  It should be set in vpci_init_header() and guest attempts to
xen/include/xen/vpci.h +5 -0
index 17cfecb0aabf..6bdbbb842f58 100644
@@ -128,6 +128,11 @@ struct vpci {
             bool prefetchable : 1;
             /* Store whether the BAR is mapped into guest p2m. */
             bool enabled      : 1;
+            /*
+             * Is the BAR position valid?  Used to store intermediate state
+             * before BAR modifications are applied to the p2m.
+             */
+            bool valid        : 1;
         } bars[PCI_HEADER_NORMAL_NR_BARS + 1];
         /* At most 6 BARS + 1 expansion ROM BAR. */
 
Signature · 1 line
2.49.0
#14 lore raw

Re: [PATCH 5/5] xen/vpci: only check BAR validity once

Quoted 3 lines · Roger Pau Monne

Avoid multiple calls to pci_check_bar() for the same memory decoding related operation, as each call can possibly print a warning message avoid a BAR being in an invalid position.

s/avoid/about/ ?

xen/drivers/vpci/header.c bool vpci_process_pending(struct vcpu *v) · line 185

Quoted 8 lines
xen/drivers/vpci/header.c +4 -0
@@ -217,13 +179,13 @@ bool vpci_process_pending(struct vcpu *v)
          * rangeset_consume_ranges() itself doesn't generate any errors.
          */
         rangeset_purge(bar->mem);
+
+ next:
+        if ( bar->valid )
+            bar->enabled = v->vpci.cmd & PCI_COMMAND_MEMORY;

Isn't it at least latently risky to possibly leave ->enabled set to true when ->valid is false?

Quoted 27 lines
xen/drivers/vpci/header.c +5 -6
@@ -243,10 +205,8 @@ static int __init apply_map(struct domain *d, const struct pci_dev *pdev,
         struct vpci_bar *bar = &header->bars[i];
         struct map_data data = { .d = d, .map = true, .bar = bar };
 
-        if ( rangeset_is_empty(bar->mem) )
-            continue;
-
-        while ( (rc = rangeset_consume_ranges(bar->mem, map_range,
+        while ( bar->mem &&
+                (rc = rangeset_consume_ranges(bar->mem, map_range,
                                               &data)) == -ERESTART )
         {
             /*
@@ -258,9 +218,10 @@ static int __init apply_map(struct domain *d, const struct pci_dev *pdev,
             process_pending_softirqs();
             write_lock(&d->pci_lock);
         }
+
+        if ( bar->valid )
+            bar->enabled = true;
     }
-    if ( !rc )
-        modify_decoding(pdev, cmd, false);
 
     return rc;
 }

xen/drivers/vpci/header.c static int modify_bars(const struct pci_dev *pdev, uint16_t cmd, bool rom_only) · line 291

Quoted 5 lines
xen/drivers/vpci/header.c +2 -0
@@ -326,6 +287,9 @@ static int modify_bars(const struct pci_dev *pdev, uint16_t cmd, bool rom_only)
          */
         rangeset_purge(bar->mem);
 
+        /* Reset whether the BAR is valid, will be checked below. */
+        bar->valid = false;

Just that I can't spot any check further down. It's only ...

xen/drivers/vpci/header.c static int modify_bars(const struct pci_dev *pdev, uint16_t cmd, bool rom_only) · line 308

Quoted 5 lines
xen/drivers/vpci/header.c +1 -0
@@ -341,6 +305,8 @@ static int modify_bars(const struct pci_dev *pdev, uint16_t cmd, bool rom_only)
             continue;
         }
 
+        bar->valid = true;

... this setting to true.

xen/drivers/vpci/header.c static void cf_check cmd_write( · line 511

Quoted 8 lines
xen/drivers/vpci/header.c +1 -0
@@ -539,6 +505,7 @@ static void cf_check cmd_write(
     if ( (cmd & PCI_COMMAND_MEMORY) && vpci_make_msix_hole(pdev) )
         return;
 #endif
+
     /*
      * FIXME: for domUs we don't want the guest toggling the memory decoding
      * bit.  It should be set in vpci_init_header() and guest attempts to

This change was probably meant to go into an earlier patch?

Jan

#15 lore raw
In reply to Roger Pau Monne
Quoted 4 lines · Roger Pau Monne

Hello,

The following series started with the report in:

Quoted 8 lines

https://lore.kernel.org/xen-devel/[email protected]/

And ended up doing a bit of cleanup on the way. I think the overall result is nicer, I never really liked the deferral of the setting of the command and ROM BAR registers, as it makes the logic much harder to follow. I've done some testing, but I will probably do some specific testing to ensure the error paths work as expected - as I don't have a system that trigger those.

I have applied this series to my local environment and run some normal graphics apps without finding errors currently. If you need to do some specific tests, I am happy to help testing.

Quoted 16 lines

Posting it ahead because some patches are fairly trivial, and to get feedback on the approach.

Thanks, Roger.

Roger Pau Monne (5):
xen/vpci: purge BAR rangeset contents before use xen/vpci: make BAR mapping more resilient for the hardware domain xen/vpci: simplify handling of memory decoding and ROM enable writes vpci/msix: move MSI-X hole punching as a result of memory decoding
enable
xen/vpci: only check BAR validity once

 xen/drivers/vpci/header.c | 186 ++++++++++++++++----------------------
 xen/include/xen/vpci.h    |   5 +
 2 files changed, 82 insertions(+), 109 deletions(-)
Signature · 2 lines
Best regards,
Jiqian Chen.