Skip to contentSkip to conversation

vfio/pci: Improve extended capability comments, skip masked caps

PATCH In review Alex Williamson 3 participants 5 messages Feb 21, 2017 – Feb 22, 2017

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 vfio/pci: Improve extended capability comments, skip masked caps

Since commit 4bb571d857d9 ("pci/pcie: don't assume cap id 0 is reserved") removes the internal use of extended capability ID 0, the comment here becomes invalid. However, peeling back the onion, the code is still correct and we still can't seed the capability chain with ID 0, unless we want to muck with using the version number to force the header to be non-zero, which is much uglier to deal with. The comment also now covers some of the subtleties of using cap ID 0, such as transparently indicating absence of capabilities if none are added. This doesn't detract from the correctness of the referenced commit as vfio in the kernel also uses capability ID zero to mask capabilties. In fact, we should skip zero capabilities precisely because the kernel might also expose such a capability at the head position and re-introduce the problem.

Cc Peter Xu <[email protected]>
Cc Michael S. Tsirkin <[email protected]>
---
 hw/vfio/pci.c |   31 +++++++++++++++++++++----------
 1 file changed, 21 insertions(+), 10 deletions(-)
hw/vfio/pci.c +21 -10
index f2ba9b6cfafc..03a3d0154976 100644
@@ -1880,16 +1880,26 @@ static void vfio_add_ext_cap(VFIOPCIDevice *vdev)
     /*
      * Extended capabilities are chained with each pointing to the next, so we
      * can drop anything other than the head of the chain simply by modifying
-     * the previous next pointer.  For the head of the chain, we can modify the
-     * capability ID to something that cannot match a valid capability.  ID
-     * 0 is reserved for this since absence of capabilities is indicated by
-     * 0 for the ID, version, AND next pointer.  However, pcie_add_capability()
-     * uses ID 0 as reserved for list management and will incorrectly match and
-     * assert if we attempt to pre-load the head of the chain with this ID.
-     * Use ID 0xFFFF temporarily since it is also seems to be reserved in
-     * part for identifying absence of capabilities in a root complex register
-     * block.  If the ID still exists after adding capabilities, switch back to
-     * zero.  We'll mark this entire first dword as emulated for this purpose.
+     * the previous next pointer.  Seed the head of the chain here such that
+     * we can simply skip any capabilities we want to drop below, regardless
+     * of their position in the chain.  If this stub capability still exists
+     * after we add the capabilities we want to expose, update the capability
+     * ID to zero.  Note that we cannot seed with the capability header being
+     * zero as this conflicts with definition of an absent capability chain
+     * and prevents capabilities beyond the head of the list from being added.
+     * By replacing the dummy capability ID with zero after walking the device
+     * chain, we also transparently mark extended capabilities as absent if
+     * no capabilities were added.  Note that the PCIe spec defines an absence
+     * of extended capabilities to be determined by a value of zero for the
+     * capability ID, version, AND next pointer.  A non-zero next pointer
+     * should be sufficient to indicate additional capabilities are present,
+     * which will occur if we call pcie_add_capability() below.  The entire
+     * first dword is emulated to support this.
+     *
+     * NB. The kernel side does similar masking, so be prepared that our
+     * view of the device may also contain a capability ID zero in the head
+     * of the chain.  Skip it for the same reason that we cannot seed the
+     * chain with a zero capability.
      */
     pci_set_long(pdev->config + PCI_CONFIG_SPACE_SIZE,
                  PCI_EXT_CAP(0xFFFF, 0, 0));
@@ -1915,6 +1925,7 @@ static void vfio_add_ext_cap(VFIOPCIDevice *vdev)
                                    PCI_EXT_CAP_NEXT_MASK);
 
         switch (cap_id) {
+        case 0: /* kernel masked capability */
         case PCI_EXT_CAP_ID_SRIOV: /* Read-only VF BARs confuse OVMF */
         case PCI_EXT_CAP_ID_ARI: /* XXX Needs next function virtualization */
             trace_vfio_add_ext_cap_dropped(vdev->vbasedev.name, cap_id, next);
Reviewed-by on v1 1/1
#1 lore raw

[cc Jintack]

Quoted 64 lines · Alex Williamson

Since commit 4bb571d857d9 ("pci/pcie: don't assume cap id 0 is reserved") removes the internal use of extended capability ID 0, the comment here becomes invalid. However, peeling back the onion, the code is still correct and we still can't seed the capability chain with ID 0, unless we want to muck with using the version number to force the header to be non-zero, which is much uglier to deal with. The comment also now covers some of the subtleties of using cap ID 0, such as transparently indicating absence of capabilities if none are added. This doesn't detract from the correctness of the referenced commit as vfio in the kernel also uses capability ID zero to mask capabilties. In fact, we should skip zero capabilities precisely because the kernel might also expose such a capability at the head position and re-introduce the problem.

Cc Peter Xu <[email protected]>
Cc Michael S. Tsirkin <[email protected]>
---
 hw/vfio/pci.c |   31 +++++++++++++++++++++----------
 1 file changed, 21 insertions(+), 10 deletions(-)
hw/vfio/pci.c +20 -10
index f2ba9b6cfafc..03a3d0154976 100644
@@ -1880,16 +1880,26 @@ static void vfio_add_ext_cap(VFIOPCIDevice *vdev)
     /*
      * Extended capabilities are chained with each pointing to the next, so we
      * can drop anything other than the head of the chain simply by modifying
-     * the previous next pointer.  For the head of the chain, we can modify the
-     * capability ID to something that cannot match a valid capability.  ID
-     * 0 is reserved for this since absence of capabilities is indicated by
-     * 0 for the ID, version, AND next pointer.  However, pcie_add_capability()
-     * uses ID 0 as reserved for list management and will incorrectly match and
-     * assert if we attempt to pre-load the head of the chain with this ID.
-     * Use ID 0xFFFF temporarily since it is also seems to be reserved in
-     * part for identifying absence of capabilities in a root complex register
-     * block.  If the ID still exists after adding capabilities, switch back to
-     * zero.  We'll mark this entire first dword as emulated for this purpose.
+     * the previous next pointer.  Seed the head of the chain here such that
+     * we can simply skip any capabilities we want to drop below, regardless
+     * of their position in the chain.  If this stub capability still exists
+     * after we add the capabilities we want to expose, update the capability
+     * ID to zero.  Note that we cannot seed with the capability header being
+     * zero as this conflicts with definition of an absent capability chain
+     * and prevents capabilities beyond the head of the list from being added.
+     * By replacing the dummy capability ID with zero after walking the device
+     * chain, we also transparently mark extended capabilities as absent if
+     * no capabilities were added.  Note that the PCIe spec defines an absence
+     * of extended capabilities to be determined by a value of zero for the
+     * capability ID, version, AND next pointer.  A non-zero next pointer
+     * should be sufficient to indicate additional capabilities are present,
+     * which will occur if we call pcie_add_capability() below.  The entire
+     * first dword is emulated to support this.
+     *
+     * NB. The kernel side does similar masking, so be prepared that our
+     * view of the device may also contain a capability ID zero in the head
+     * of the chain.  Skip it for the same reason that we cannot seed the
+     * chain with a zero capability.
      */
     pci_set_long(pdev->config + PCI_CONFIG_SPACE_SIZE,
                  PCI_EXT_CAP(0xFFFF, 0, 0));

hw/vfio/pci.c static void vfio_add_ext_cap(VFIOPCIDevice *vdev) · line 1931

Quoted 7 lines
hw/vfio/pci.c +1 -0
@@ -1915,6 +1925,7 @@ static void vfio_add_ext_cap(VFIOPCIDevice *vdev)
                                    PCI_EXT_CAP_NEXT_MASK);
 
         switch (cap_id) {
+        case 0: /* kernel masked capability */
         case PCI_EXT_CAP_ID_SRIOV: /* Read-only VF BARs confuse OVMF */
         case PCI_EXT_CAP_ID_ARI: /* XXX Needs next function virtualization */
             trace_vfio_add_ext_cap_dropped(vdev->vbasedev.name, cap_id, next);

Since this bug is originally reported by Jintack, maybe we can also add:

Jintack, if you want to test it and provide your tested-by, it would be nice as well. ;)

Actually I just found that the bug still exist after Michael's fix (I thought it was fixed). So we definitely need this patch or equivalent. However, I would still slightly prefer removing the wrapping hack since after all we need to touch it (and I do feel like that's error prone...). So, Alex, do you like below one instead?

--------8<----------

hw/vfio/pci.c +18 -28
index 332f41d..6942c1d 100644
@@ -1877,25 +1877,6 @@ static void vfio_add_ext_cap(VFIOPCIDevice *vdev)
      */
     config = g_memdup(pdev->config, vdev->config_size);
 
-    /*
-     * Extended capabilities are chained with each pointing to the next, so we
-     * can drop anything other than the head of the chain simply by modifying
-     * the previous next pointer.  For the head of the chain, we can modify the
-     * capability ID to something that cannot match a valid capability.  ID
-     * 0 is reserved for this since absence of capabilities is indicated by
-     * 0 for the ID, version, AND next pointer.  However, pcie_add_capability()
-     * uses ID 0 as reserved for list management and will incorrectly match and
-     * assert if we attempt to pre-load the head of the chain with this ID.
-     * Use ID 0xFFFF temporarily since it is also seems to be reserved in
-     * part for identifying absence of capabilities in a root complex register
-     * block.  If the ID still exists after adding capabilities, switch back to
-     * zero.  We'll mark this entire first dword as emulated for this purpose.
-     */
-    pci_set_long(pdev->config + PCI_CONFIG_SPACE_SIZE,
-                 PCI_EXT_CAP(0xFFFF, 0, 0));
-    pci_set_long(pdev->wmask + PCI_CONFIG_SPACE_SIZE, 0);
-    pci_set_long(vdev->emulated_config_bits + PCI_CONFIG_SPACE_SIZE, ~0);
-
     for (next = PCI_CONFIG_SPACE_SIZE; next;
          next = PCI_EXT_CAP_NEXT(pci_get_long(config + next))) {
         header = pci_get_long(config + next);
@@ -1910,24 +1891,33 @@ static void vfio_add_ext_cap(VFIOPCIDevice *vdev)
          */
         size = vfio_ext_cap_max_size(config, next);
 
-        /* Use emulated next pointer to allow dropping extended caps */
-        pci_long_test_and_set_mask(vdev->emulated_config_bits + next,
-                                   PCI_EXT_CAP_NEXT_MASK);
+        /* Use emulated header to allow dropping extended caps */
+        pci_set_long(vdev->emulated_config_bits + next, 0xffffffffULL);
 
         switch (cap_id) {
         case PCI_EXT_CAP_ID_SRIOV: /* Read-only VF BARs confuse OVMF */
         case PCI_EXT_CAP_ID_ARI: /* XXX Needs next function virtualization */
+        case PCI_EXT_CAP_ID_VC:
+            /*
+             * For dropped capabilities, we keep their slot but
+             * replace them with a header containing cap_id=0 &&
+             * cap_ver=1. We do this reservation mostly to make sure
+             * the head ecap (at offset 0x100) will always be there.
+             * Anyway it won't hurt if we keep the rest of the dropped
+             * ones as well.
+             *
+             * Here we use non-zero cap_ver because we want to "mark"
+             * this ecap as "available" - from PCIe spec (chap 7.9.1),
+             * it marked out that cap_id=cap_ver=next=0 means empty
+             * ecap, and we really don't want it to be an "empty" slot
+             * especially for the head ecap (we need a head, always!).
+             */
+            pcie_add_capability(pdev, 0, 1, next, size);
             trace_vfio_add_ext_cap_dropped(vdev->vbasedev.name, cap_id, next);
             break;
         default:
             pcie_add_capability(pdev, cap_id, cap_ver, next, size);
         }
-
-    }
-
-    /* Cleanup chain head ID if necessary */
-    if (pci_get_word(pdev->config + PCI_CONFIG_SPACE_SIZE) == 0xFFFF) {
-        pci_set_word(pdev->config + PCI_CONFIG_SPACE_SIZE, 0);
     }
 
     g_free(config);

------->8--------

The new patch will keep all the dropped ecaps (so we may see more than one cap_id=0x0000 field), which I don't know whether would be a drawback. OTOH, it provides benefits like:

- we can remove the wrapping hack, so the code is much readable and less error prone imho

- we can avoid using the assumption that 0xffff cap_id is reserved

I can live with both patches though. :-)

Thanks!

-- peterx

#2 lore raw
Quoted 171 lines · On Wed, 22 Feb 2017 11:08:51 +0800 Peter Xu <[email protected]> wrote:

[cc Jintack]

Quoted 64 lines · Alex Williamson

Since commit 4bb571d857d9 ("pci/pcie: don't assume cap id 0 is reserved") removes the internal use of extended capability ID 0, the comment here becomes invalid. However, peeling back the onion, the code is still correct and we still can't seed the capability chain with ID 0, unless we want to muck with using the version number to force the header to be non-zero, which is much uglier to deal with. The comment also now covers some of the subtleties of using cap ID 0, such as transparently indicating absence of capabilities if none are added. This doesn't detract from the correctness of the referenced commit as vfio in the kernel also uses capability ID zero to mask capabilties. In fact, we should skip zero capabilities precisely because the kernel might also expose such a capability at the head position and re-introduce the problem.

Cc Peter Xu <[email protected]>
Cc Michael S. Tsirkin <[email protected]>
---
 hw/vfio/pci.c |   31 +++++++++++++++++++++----------
 1 file changed, 21 insertions(+), 10 deletions(-)
hw/vfio/pci.c +20 -10
index f2ba9b6cfafc..03a3d0154976 100644
@@ -1880,16 +1880,26 @@ static void vfio_add_ext_cap(VFIOPCIDevice *vdev)
     /*
      * Extended capabilities are chained with each pointing to the next, so we
      * can drop anything other than the head of the chain simply by modifying
-     * the previous next pointer.  For the head of the chain, we can modify the
-     * capability ID to something that cannot match a valid capability.  ID
-     * 0 is reserved for this since absence of capabilities is indicated by
-     * 0 for the ID, version, AND next pointer.  However, pcie_add_capability()
-     * uses ID 0 as reserved for list management and will incorrectly match and
-     * assert if we attempt to pre-load the head of the chain with this ID.
-     * Use ID 0xFFFF temporarily since it is also seems to be reserved in
-     * part for identifying absence of capabilities in a root complex register
-     * block.  If the ID still exists after adding capabilities, switch back to
-     * zero.  We'll mark this entire first dword as emulated for this purpose.
+     * the previous next pointer.  Seed the head of the chain here such that
+     * we can simply skip any capabilities we want to drop below, regardless
+     * of their position in the chain.  If this stub capability still exists
+     * after we add the capabilities we want to expose, update the capability
+     * ID to zero.  Note that we cannot seed with the capability header being
+     * zero as this conflicts with definition of an absent capability chain
+     * and prevents capabilities beyond the head of the list from being added.
+     * By replacing the dummy capability ID with zero after walking the device
+     * chain, we also transparently mark extended capabilities as absent if
+     * no capabilities were added.  Note that the PCIe spec defines an absence
+     * of extended capabilities to be determined by a value of zero for the
+     * capability ID, version, AND next pointer.  A non-zero next pointer
+     * should be sufficient to indicate additional capabilities are present,
+     * which will occur if we call pcie_add_capability() below.  The entire
+     * first dword is emulated to support this.
+     *
+     * NB. The kernel side does similar masking, so be prepared that our
+     * view of the device may also contain a capability ID zero in the head
+     * of the chain.  Skip it for the same reason that we cannot seed the
+     * chain with a zero capability.
      */
     pci_set_long(pdev->config + PCI_CONFIG_SPACE_SIZE,
                  PCI_EXT_CAP(0xFFFF, 0, 0));

hw/vfio/pci.c static void vfio_add_ext_cap(VFIOPCIDevice *vdev) · line 1931

Quoted 7 lines
hw/vfio/pci.c +1 -0
@@ -1915,6 +1925,7 @@ static void vfio_add_ext_cap(VFIOPCIDevice *vdev)
                                    PCI_EXT_CAP_NEXT_MASK);
 
         switch (cap_id) {
+        case 0: /* kernel masked capability */
         case PCI_EXT_CAP_ID_SRIOV: /* Read-only VF BARs confuse OVMF */
         case PCI_EXT_CAP_ID_ARI: /* XXX Needs next function virtualization */
             trace_vfio_add_ext_cap_dropped(vdev->vbasedev.name, cap_id, next);

Since this bug is originally reported by Jintack, maybe we can also add:

Jintack, if you want to test it and provide your tested-by, it would be nice as well. ;)

Actually I just found that the bug still exist after Michael's fix (I thought it was fixed). So we definitely need this patch or equivalent. However, I would still slightly prefer removing the wrapping hack since after all we need to touch it (and I do feel like that's error prone...). So, Alex, do you like below one instead?

--------8<----------

hw/vfio/pci.c +18 -29
index 332f41d..6942c1d 100644
@@ -1877,25 +1877,6 @@ static void vfio_add_ext_cap(VFIOPCIDevice *vdev)
      */
     config = g_memdup(pdev->config, vdev->config_size);
 
-    /*
-     * Extended capabilities are chained with each pointing to the next, so we
-     * can drop anything other than the head of the chain simply by modifying
-     * the previous next pointer.  For the head of the chain, we can modify the
-     * capability ID to something that cannot match a valid capability.  ID
-     * 0 is reserved for this since absence of capabilities is indicated by
-     * 0 for the ID, version, AND next pointer.  However, pcie_add_capability()
-     * uses ID 0 as reserved for list management and will incorrectly match and
-     * assert if we attempt to pre-load the head of the chain with this ID.
-     * Use ID 0xFFFF temporarily since it is also seems to be reserved in
-     * part for identifying absence of capabilities in a root complex register
-     * block.  If the ID still exists after adding capabilities, switch back to
-     * zero.  We'll mark this entire first dword as emulated for this purpose.
-     */
-    pci_set_long(pdev->config + PCI_CONFIG_SPACE_SIZE,
-                 PCI_EXT_CAP(0xFFFF, 0, 0));
-    pci_set_long(pdev->wmask + PCI_CONFIG_SPACE_SIZE, 0);
-    pci_set_long(vdev->emulated_config_bits + PCI_CONFIG_SPACE_SIZE, ~0);
-
     for (next = PCI_CONFIG_SPACE_SIZE; next;
          next = PCI_EXT_CAP_NEXT(pci_get_long(config + next))) {
         header = pci_get_long(config + next);
@@ -1910,24 +1891,33 @@ static void vfio_add_ext_cap(VFIOPCIDevice *vdev)
          */
         size = vfio_ext_cap_max_size(config, next);
 
-        /* Use emulated next pointer to allow dropping extended caps */
-        pci_long_test_and_set_mask(vdev->emulated_config_bits + next,
-                                   PCI_EXT_CAP_NEXT_MASK);
+        /* Use emulated header to allow dropping extended caps */
+        pci_set_long(vdev->emulated_config_bits + next, 0xffffffffULL);
 
         switch (cap_id) {
         case PCI_EXT_CAP_ID_SRIOV: /* Read-only VF BARs confuse OVMF */
         case PCI_EXT_CAP_ID_ARI: /* XXX Needs next function virtualization */
+        case PCI_EXT_CAP_ID_VC:
+            /*
+             * For dropped capabilities, we keep their slot but
+             * replace them with a header containing cap_id=0 &&
+             * cap_ver=1. We do this reservation mostly to make sure
+             * the head ecap (at offset 0x100) will always be there.
+             * Anyway it won't hurt if we keep the rest of the dropped
+             * ones as well.
+             *
+             * Here we use non-zero cap_ver because we want to "mark"
+             * this ecap as "available" - from PCIe spec (chap 7.9.1),
+             * it marked out that cap_id=cap_ver=next=0 means empty
+             * ecap, and we really don't want it to be an "empty" slot
+             * especially for the head ecap (we need a head, always!).
+             */
+            pcie_add_capability(pdev, 0, 1, next, size);
             trace_vfio_add_ext_cap_dropped(vdev->vbasedev.name, cap_id, next);
             break;
         default:
             pcie_add_capability(pdev, cap_id, cap_ver, next, size);
         }
-
-    }
-
-    /* Cleanup chain head ID if necessary */
-    if (pci_get_word(pdev->config + PCI_CONFIG_SPACE_SIZE) == 0xFFFF) {
-        pci_set_word(pdev->config + PCI_CONFIG_SPACE_SIZE, 0);
     }
 
     g_free(config);
------->8--------  

The new patch will keep all the dropped ecaps (so we may see more than one cap_id=0x0000 field), which I don't know whether would be a drawback. OTOH, it provides benefits like:

Quoted 6 lines

- we can remove the wrapping hack, so the code is much readable and less error prone imho

- we can avoid using the assumption that 0xffff cap_id is reserved

I can live with both patches though. :-)

I prefer the existing code. I don't really see why you consider it a hack. I think it's pretty elegant that we can ignore the header through the course of iterating through the capabilities, that we drop other masked capabilities out of the chain, and that we can so easily and transparently insert a zero ID at the end to serve the dual purpose of replacing the temporary ID and nullifying the list if nothing was added. The 0xffff capability ID is a perfectly safe assumption, not only are we ridiculously far from allocating that ID, but it's arguably a reserved value due to its use in the root complex register block. I also don't see any evidence that it's error prone, the entire point is that we can arbitrarily skip capability IDs in the body of the loop and the result is a correct, minimal capability chain. OTOH, leaving masked capabilities in the chain with an arbitrary version number seems messy.

The real question is why are you sneaking the virtual channel capability into the list of masked capabilities? Thanks,

Alex

Tested-by
#3 lore raw

On Tue, Feb 21, 2017 at 08:54:31PM -0700, Alex Williamson wrote:

[...]

Quoted 6 lines

I prefer the existing code. I don't really see why you consider it a hack. I think it's pretty elegant that we can ignore the header through the course of iterating through the capabilities, that we drop other masked capabilities out of the chain, and that we can so easily and transparently insert a zero ID at the end to serve the dual purpose of replacing the temporary ID and nullifying the list if nothing was

Quoted 8 lines

added. The 0xffff capability ID is a perfectly safe assumption, not only are we ridiculously far from allocating that ID, but it's arguably a reserved value due to its use in the root complex register block. I also don't see any evidence that it's error prone, the entire point is that we can arbitrarily skip capability IDs in the body of the loop and the result is a correct, minimal capability chain. OTOH, leaving masked capabilities in the chain with an arbitrary version number seems messy.

I see. Then please also pick this one:

Quoted 3 lines

The real question is why are you sneaking the virtual channel capability into the list of masked capabilities? Thanks,

Oooops. I should remove that line. It's for my testing purpose (I need to "fake" a device that with 0x100 masked to test my patch, while my SD card reader did has this VC cap at 0x100 :-). Since we now have a choice already, please just ignore that line along with the whole patch. ;)

Thanks,

-- peterx

Tested-by
#4 lore raw
In reply to Peter Xu
Quoted 107 lines · Peter Xu

[cc Jintack]

Quoted 26 lines · Alex Williamson

Since commit 4bb571d857d9 ("pci/pcie: don't assume cap id 0 is reserved") removes the internal use of extended capability ID 0, the comment here becomes invalid. However, peeling back the onion, the code is still correct and we still can't seed the capability chain with ID 0, unless we want to muck with using the version number to force the header to be non-zero, which is much uglier to deal with. The comment also now covers some of the subtleties of using cap ID 0, such as transparently indicating absence of capabilities if none are added. This doesn't detract from the correctness of the referenced commit as vfio in the kernel also uses capability ID zero to mask capabilties. In fact, we should skip zero capabilities precisely because the kernel might also expose such a capability at the head position and re-introduce the problem.

Cc Peter Xu <[email protected]>
Cc Michael S. Tsirkin <[email protected]>
---
 hw/vfio/pci.c |   31 +++++++++++++++++++++----------
 1 file changed, 21 insertions(+), 10 deletions(-)
hw/vfio/pci.c +0 -0
index f2ba9b6cfafc..03a3d0154976 100644

hw/vfio/pci.c static void vfio_add_ext_cap(VFIOPCIDevice *vdev) · line 1881

Quoted 2 lines
hw/vfio/pci.c +0 -0
@@ -1880,16 +1880,26 @@ static void vfio_add_ext_cap(VFIOPCIDevice *vdev)
     /*
      * Extended capabilities are chained with each pointing to the

next, so we

hw/vfio/pci.c static void vfio_add_ext_cap(VFIOPCIDevice *vdev) · line 1882

Quoted 1 line
hw/vfio/pci.c +0 -0
      * can drop anything other than the head of the chain simply by

modifying

hw/vfio/pci.c static void vfio_add_ext_cap(VFIOPCIDevice *vdev) · line 1883

Quoted 1 line
hw/vfio/pci.c +0 -1
-     * the previous next pointer.  For the head of the chain, we can

modify the

hw/vfio/pci.c static void vfio_add_ext_cap(VFIOPCIDevice *vdev) · line 1884

Quoted 1 line
hw/vfio/pci.c +0 -1
-     * capability ID to something that cannot match a valid

capability. ID

hw/vfio/pci.c static void vfio_add_ext_cap(VFIOPCIDevice *vdev) · line 1885

Quoted 1 line
hw/vfio/pci.c +0 -1
-     * 0 is reserved for this since absence of capabilities is

indicated by

hw/vfio/pci.c static void vfio_add_ext_cap(VFIOPCIDevice *vdev) · line 1886

Quoted 1 line
hw/vfio/pci.c +0 -1
-     * 0 for the ID, version, AND next pointer.  However,

pcie_add_capability()

hw/vfio/pci.c static void vfio_add_ext_cap(VFIOPCIDevice *vdev) · line 1887

Quoted 1 line
hw/vfio/pci.c +0 -1
-     * uses ID 0 as reserved for list management and will incorrectly

match and

hw/vfio/pci.c static void vfio_add_ext_cap(VFIOPCIDevice *vdev) · line 1888

Quoted 1 line
hw/vfio/pci.c +0 -1
-     * assert if we attempt to pre-load the head of the chain with this

ID.

hw/vfio/pci.c static void vfio_add_ext_cap(VFIOPCIDevice *vdev) · line 1889

Quoted 1 line
hw/vfio/pci.c +0 -1
-     * Use ID 0xFFFF temporarily since it is also seems to be reserved

in

hw/vfio/pci.c static void vfio_add_ext_cap(VFIOPCIDevice *vdev) · line 1890

Quoted 1 line
hw/vfio/pci.c +0 -1
-     * part for identifying absence of capabilities in a root complex

register

hw/vfio/pci.c static void vfio_add_ext_cap(VFIOPCIDevice *vdev) · line 1891

Quoted 1 line
hw/vfio/pci.c +0 -1
-     * block.  If the ID still exists after adding capabilities, switch

back to

hw/vfio/pci.c static void vfio_add_ext_cap(VFIOPCIDevice *vdev) · line 1892

Quoted 1 line
hw/vfio/pci.c +0 -1
-     * zero.  We'll mark this entire first dword as emulated for this

purpose.

hw/vfio/pci.c static void vfio_add_ext_cap(VFIOPCIDevice *vdev) · line 1883

Quoted 1 line
hw/vfio/pci.c +1 -0
+     * the previous next pointer.  Seed the head of the chain here such

that

hw/vfio/pci.c static void vfio_add_ext_cap(VFIOPCIDevice *vdev) · line 1884

Quoted 1 line
hw/vfio/pci.c +1 -0
+     * we can simply skip any capabilities we want to drop below,

regardless

hw/vfio/pci.c static void vfio_add_ext_cap(VFIOPCIDevice *vdev) · line 1885

Quoted 1 line
hw/vfio/pci.c +1 -0
+     * of their position in the chain.  If this stub capability still

exists

hw/vfio/pci.c static void vfio_add_ext_cap(VFIOPCIDevice *vdev) · line 1886

Quoted 1 line
hw/vfio/pci.c +1 -0
+     * after we add the capabilities we want to expose, update the

capability

hw/vfio/pci.c static void vfio_add_ext_cap(VFIOPCIDevice *vdev) · line 1887

Quoted 1 line
hw/vfio/pci.c +1 -0
+     * ID to zero.  Note that we cannot seed with the capability header

being

hw/vfio/pci.c static void vfio_add_ext_cap(VFIOPCIDevice *vdev) · line 1888

Quoted 1 line
hw/vfio/pci.c +1 -0
+     * zero as this conflicts with definition of an absent capability

chain

hw/vfio/pci.c static void vfio_add_ext_cap(VFIOPCIDevice *vdev) · line 1889

Quoted 1 line
hw/vfio/pci.c +1 -0
+     * and prevents capabilities beyond the head of the list from being

added.

hw/vfio/pci.c static void vfio_add_ext_cap(VFIOPCIDevice *vdev) · line 1890

Quoted 1 line
hw/vfio/pci.c +1 -0
+     * By replacing the dummy capability ID with zero after walking the

device

hw/vfio/pci.c static void vfio_add_ext_cap(VFIOPCIDevice *vdev) · line 1891

Quoted 1 line
hw/vfio/pci.c +1 -0
+     * chain, we also transparently mark extended capabilities as

absent if

hw/vfio/pci.c static void vfio_add_ext_cap(VFIOPCIDevice *vdev) · line 1892

Quoted 1 line
hw/vfio/pci.c +1 -0
+     * no capabilities were added.  Note that the PCIe spec defines an

absence

hw/vfio/pci.c static void vfio_add_ext_cap(VFIOPCIDevice *vdev) · line 1893

Quoted 1 line
hw/vfio/pci.c +1 -0
+     * of extended capabilities to be determined by a value of zero for

the

hw/vfio/pci.c static void vfio_add_ext_cap(VFIOPCIDevice *vdev) · line 1894

Quoted 1 line
hw/vfio/pci.c +1 -0
+     * capability ID, version, AND next pointer.  A non-zero next

pointer

hw/vfio/pci.c static void vfio_add_ext_cap(VFIOPCIDevice *vdev) · line 1895

Quoted 1 line
hw/vfio/pci.c +1 -0
+     * should be sufficient to indicate additional capabilities are

present,

hw/vfio/pci.c static void vfio_add_ext_cap(VFIOPCIDevice *vdev) · line 1896

Quoted 1 line
hw/vfio/pci.c +1 -0
+     * which will occur if we call pcie_add_capability() below.  The

entire

hw/vfio/pci.c static void vfio_add_ext_cap(VFIOPCIDevice *vdev) · line 1900

Quoted 4 lines
hw/vfio/pci.c +4 -0
+     * first dword is emulated to support this.
+     *
+     * NB. The kernel side does similar masking, so be prepared that our
+     * view of the device may also contain a capability ID zero in the

head

hw/vfio/pci.c static void vfio_add_ext_cap(VFIOPCIDevice *vdev) · line 1901

Quoted 1 line
hw/vfio/pci.c +1 -0
+     * of the chain.  Skip it for the same reason that we cannot seed

the

hw/vfio/pci.c static void vfio_add_ext_cap(VFIOPCIDevice *vdev) · line 1930

Quoted 11 lines
hw/vfio/pci.c +2 -0
+     * chain with a zero capability.
      */
     pci_set_long(pdev->config + PCI_CONFIG_SPACE_SIZE,
                  PCI_EXT_CAP(0xFFFF, 0, 0));
@@ -1915,6 +1925,7 @@ static void vfio_add_ext_cap(VFIOPCIDevice *vdev)
                                    PCI_EXT_CAP_NEXT_MASK);
 
         switch (cap_id) {
+        case 0: /* kernel masked capability */
         case PCI_EXT_CAP_ID_SRIOV: /* Read-only VF BARs confuse OVMF */
         case PCI_EXT_CAP_ID_ARI: /* XXX Needs next function

virtualization */

hw/vfio/pci.c static void vfio_add_ext_cap(VFIOPCIDevice *vdev) · line 1931

Quoted 1 line
hw/vfio/pci.c +0 -0
             trace_vfio_add_ext_cap_dropped(vdev->vbasedev.name,

cap_id, next);

Quoted 1 line
Quoted 8 lines

Since this bug is originally reported by Jintack, maybe we can also add:

Jintack, if you want to test it and provide your tested-by, it would be nice as well. ;)

I believe this patch is to fix the error I got before.
qemu-system-x86_64: hw/pci/pcie.c:686: pcie_add_capability: Assertion `prev >= 0x100' failed.

If so,

Quoted 111 lines

Actually I just found that the bug still exist after Michael's fix (I thought it was fixed). So we definitely need this patch or equivalent. However, I would still slightly prefer removing the wrapping hack since after all we need to touch it (and I do feel like that's error prone...). So, Alex, do you like below one instead?

--------8<----------

hw/vfio/pci.c +0 -2
index 332f41d..6942c1d 100644
@@ -1877,25 +1877,6 @@ static void vfio_add_ext_cap(VFIOPCIDevice *vdev)
      */
     config = g_memdup(pdev->config, vdev->config_size);
 
-    /*
-     * Extended capabilities are chained with each pointing to the next,

so we
- * can drop anything other than the head of the chain simply by
modifying
- * the previous next pointer. For the head of the chain, we can
modify the
- * capability ID to something that cannot match a valid capability.
ID
- * 0 is reserved for this since absence of capabilities is indicated
by
- * 0 for the ID, version, AND next pointer. However,
pcie_add_capability()
- * uses ID 0 as reserved for list management and will incorrectly
match and
- * assert if we attempt to pre-load the head of the chain with this
ID.

hw/vfio/pci.c +0 -2
-     * Use ID 0xFFFF temporarily since it is also seems to be reserved in
-     * part for identifying absence of capabilities in a root complex

register
- * block. If the ID still exists after adding capabilities, switch
back to
- * zero. We'll mark this entire first dword as emulated for this
purpose.

hw/vfio/pci.c +2 -9
-     */
-    pci_set_long(pdev->config + PCI_CONFIG_SPACE_SIZE,
-                 PCI_EXT_CAP(0xFFFF, 0, 0));
-    pci_set_long(pdev->wmask + PCI_CONFIG_SPACE_SIZE, 0);
-    pci_set_long(vdev->emulated_config_bits + PCI_CONFIG_SPACE_SIZE, ~0);
-
     for (next = PCI_CONFIG_SPACE_SIZE; next;
          next = PCI_EXT_CAP_NEXT(pci_get_long(config + next))) {
         header = pci_get_long(config + next);
@@ -1910,24 +1891,33 @@ static void vfio_add_ext_cap(VFIOPCIDevice *vdev)
          */
         size = vfio_ext_cap_max_size(config, next);
 
-        /* Use emulated next pointer to allow dropping extended caps */
-        pci_long_test_and_set_mask(vdev->emulated_config_bits + next,
-                                   PCI_EXT_CAP_NEXT_MASK);
+        /* Use emulated header to allow dropping extended caps */
+        pci_set_long(vdev->emulated_config_bits + next, 0xffffffffULL);
 
         switch (cap_id) {
         case PCI_EXT_CAP_ID_SRIOV: /* Read-only VF BARs confuse OVMF */
         case PCI_EXT_CAP_ID_ARI: /* XXX Needs next function

virtualization */

hw/vfio/pci.c +16 -0
+        case PCI_EXT_CAP_ID_VC:
+            /*
+             * For dropped capabilities, we keep their slot but
+             * replace them with a header containing cap_id=0 &&
+             * cap_ver=1. We do this reservation mostly to make sure
+             * the head ecap (at offset 0x100) will always be there.
+             * Anyway it won't hurt if we keep the rest of the dropped
+             * ones as well.
+             *
+             * Here we use non-zero cap_ver because we want to "mark"
+             * this ecap as "available" - from PCIe spec (chap 7.9.1),
+             * it marked out that cap_id=cap_ver=next=0 means empty
+             * ecap, and we really don't want it to be an "empty" slot
+             * especially for the head ecap (we need a head, always!).
+             */
+            pcie_add_capability(pdev, 0, 1, next, size);
             trace_vfio_add_ext_cap_dropped(vdev->vbasedev.name, cap_id,
next);
             break;
         default:
             pcie_add_capability(pdev, cap_id, cap_ver, next, size);
         }
hw/vfio/pci.c +0 -7
-
-    }
-
-    /* Cleanup chain head ID if necessary */
-    if (pci_get_word(pdev->config + PCI_CONFIG_SPACE_SIZE) == 0xFFFF) {
-        pci_set_word(pdev->config + PCI_CONFIG_SPACE_SIZE, 0);
     }
 
     g_free(config);
------->8--------

The new patch will keep all the dropped ecaps (so we may see more than one cap_id=0x0000 field), which I don't know whether would be a drawback. OTOH, it provides benefits like:

- we can remove the wrapping hack, so the code is much readable and less error prone imho

- we can avoid using the assumption that 0xffff cap_id is reserved

I can live with both patches though. :-)

Thanks!

-- peterx