Skip to contentSkip to conversation

Support hiding capability when its initialization fails

PATCH Superseded Jiqian Chen 3 participants 41 messages Apr 9, 2025 – Apr 17, 2025

v2 of 12 Superseded Latest is v12 →

lore
Share & tools
lore
#0 lore raw

v2 cover Support hiding capability when its initialization fails

Hi,

This series is to
emulate legacy and extended capability list for host, including patch #1, #2, #3, hide capability when its initialization fails, including patch #4,
remove all related registers and other resources when failure, including patch #5, #6, #7, #8.

Best regards,
Jiqian Chen.
---
cc: Andrew Cooper <[email protected]>
cc: Anthony PERARD <[email protected]>
cc: Michal Orzel <[email protected]>
cc: Jan Beulich <[email protected]>
cc: Julien Grall <[email protected]>
cc: "Roger Pau Monné" <[email protected]>
cc: Stefano Stabellini <[email protected]>
---
Jiqian Chen (8):
driver/pci: Get next capability without passing caps vpci/header: Emulate legacy capability list for host vpci/header: Emulate extended capability list for host
vpci: Hide capability when it fails to initialize
vpci: Refactor vpci_remove_register to remove matched registers vpci/rebar: Remove registers when init_rebar() fails vpci/msi: Free MSI resources when init_msi() fails vpci/msix: Add function to clean MSIX resources

 tools/tests/vpci/main.c           |   4 +-
 xen/arch/x86/hvm/intercept.c      |  44 +++++++
 xen/arch/x86/include/asm/hvm/io.h |   3 +
 xen/drivers/pci/pci.c             |   6 +-
 xen/drivers/vpci/header.c         | 180 ++++++++++++++++-----------
 xen/drivers/vpci/msi.c            |  49 ++++++--
 xen/drivers/vpci/msix.c           |  67 +++++++++-
 xen/drivers/vpci/rebar.c          |  27 ++--
 xen/drivers/vpci/vpci.c           | 198 ++++++++++++++++++++++++------
 xen/include/xen/pci.h             |   2 +-
 xen/include/xen/pci_regs.h        |   1 +
 xen/include/xen/vpci.h            |  30 +++--
 xen/include/xen/xen.lds.h         |   2 +-
 13 files changed, 461 insertions(+), 152 deletions(-)
Signature · 1 line
2.34.1
#1 lore raw

PATCH v2 1/8 driver/pci: Get next capability without passing caps

Modify function pci_find_next_cap_ttl to support returning position of next capability when array "caps" is empty or size "n" is zero.

That can help caller to get next capability offset if caller just has a information of current capability offset.

That will be used in a follow-on change.

---
cc: Andrew Cooper <[email protected]>
cc: Anthony PERARD <[email protected]>
cc: Michal Orzel <[email protected]>
cc: Jan Beulich <[email protected]>
cc: Julien Grall <[email protected]>
cc: "Roger Pau Monné" <[email protected]>
cc: Stefano Stabellini <[email protected]>
---
v1->v2 changes:
new patch

Best regards,
Jiqian Chen.

---
 xen/drivers/pci/pci.c | 6 +++++-
 xen/include/xen/pci.h | 2 +-
 2 files changed, 6 insertions(+), 2 deletions(-)
xen/drivers/pci/pci.c +5 -1
index edf5b9f7ae9f..ec81d0db6133 100644
@@ -40,7 +40,7 @@ unsigned int pci_find_cap_offset(pci_sbdf_t sbdf, unsigned int cap)
 }
 
 unsigned int pci_find_next_cap_ttl(pci_sbdf_t sbdf, unsigned int pos,
-                                   const unsigned int caps[], unsigned int n,
+                                   const unsigned int *caps, unsigned int n,
                                    unsigned int *ttl)
 {
     while ( (*ttl)-- )
@@ -55,6 +55,10 @@ unsigned int pci_find_next_cap_ttl(pci_sbdf_t sbdf, unsigned int pos,
 
         if ( id == 0xff )
             break;
+
+        if ( !caps || n == 0 )
+            return pos;
+
         for ( i = 0; i < n; i++ )
         {
             if ( id == caps[i] )
xen/include/xen/pci.h +1 -1
index ef601966533e..cc84f2cbebfc 100644
@@ -251,7 +251,7 @@ int pci_mmcfg_write(unsigned int seg, unsigned int bus,
                     unsigned int devfn, int reg, int len, u32 value);
 unsigned int pci_find_cap_offset(pci_sbdf_t sbdf, unsigned int cap);
 unsigned int pci_find_next_cap_ttl(pci_sbdf_t sbdf, unsigned int pos,
-                                   const unsigned int caps[], unsigned int n,
+                                   const unsigned int *caps, unsigned int n,
                                    unsigned int *ttl);
 unsigned int pci_find_next_cap(pci_sbdf_t sbdf, unsigned int pos,
                                unsigned int cap);
Signature · 1 line
2.34.1
#2 lore raw

Re: [PATCH v2 1/8] driver/pci: Get next capability without passing caps

xen/drivers/pci/pci.c unsigned int pci_find_cap_offset(pci_sbdf_t sbdf, unsigned int cap) · line 43

Quoted 8 lines · Jiqian Chen
xen/drivers/pci/pci.c +1 -1
@@ -40,7 +40,7 @@ unsigned int pci_find_cap_offset(pci_sbdf_t sbdf, unsigned int cap)
 }
 
 unsigned int pci_find_next_cap_ttl(pci_sbdf_t sbdf, unsigned int pos,
-                                   const unsigned int caps[], unsigned int n,
+                                   const unsigned int *caps, unsigned int n,

I don't follow the need for this change.

xen/drivers/pci/pci.c unsigned int pci_find_next_cap_ttl(pci_sbdf_t sbdf, unsigned int pos, · line 60

Quoted 7 lines
xen/drivers/pci/pci.c +3 -0
@@ -55,6 +55,10 @@ unsigned int pci_find_next_cap_ttl(pci_sbdf_t sbdf, unsigned int pos,
 
         if ( id == 0xff )
             break;
+
+        if ( !caps || n == 0 )
+            return pos;

Checking n to be zero ought to suffice here? In that case it doesn't matter what caps is. Plus if n is non-zero, it clearly is an error if caps was NULL.

Jan

#3 lore raw

Re: [PATCH v2 1/8] driver/pci: Get next capability without passing caps

Quoted 11 lines · Jan Beulich

xen/drivers/pci/pci.c unsigned int pci_find_cap_offset(pci_sbdf_t sbdf, unsigned int cap) · line 43

Quoted 8 lines · Jiqian Chen
xen/drivers/pci/pci.c +1 -1
@@ -40,7 +40,7 @@ unsigned int pci_find_cap_offset(pci_sbdf_t sbdf, unsigned int cap)
 }
 
 unsigned int pci_find_next_cap_ttl(pci_sbdf_t sbdf, unsigned int pos,
-                                   const unsigned int caps[], unsigned int n,
+                                   const unsigned int *caps, unsigned int n,

I don't follow the need for this change.

This changed is for my next patch "vpci/header: Emulate legacy capability list for host".
Currently, vpci only emulates capability list for domU, not for dom0.
For domU, vpci exposes a fixed capability array which calls "supported_caps".
My changes want to emulate capability list for dom0.
I think vpci should expose all devices capabilities to dom0.
When I emulate it, I need to iterate over all capabilities without another fixed array,
so I need this function to return the position of next capability directly when passing a zero length array to this function.

Quoted 11 lines

xen/drivers/pci/pci.c unsigned int pci_find_next_cap_ttl(pci_sbdf_t sbdf, unsigned int pos, · line 60

Quoted 8 lines
xen/drivers/pci/pci.c +3 -0
@@ -55,6 +55,10 @@ unsigned int pci_find_next_cap_ttl(pci_sbdf_t sbdf, unsigned int pos,
 
         if ( id == 0xff )
             break;
+
+        if ( !caps || n == 0 )
+            return pos;

Checking n to be zero ought to suffice here? In that case it doesn't matter what caps is. Plus if n is non-zero, it clearly is an error if caps was NULL.

Two checking is to prevent null pointer errors.
But as you said, if checking n to be zero is enough, then I don't need to change the definition of this function. I will change in next version.

Quoted 2 lines

Jan

Signature · 2 lines
Best regards,
Jiqian Chen.
#4 lore raw

Re: [PATCH v2 1/8] driver/pci: Get next capability without passing caps

Quoted 12 lines · Jiqian
Quoted 11 lines · Jan Beulich

xen/drivers/pci/pci.c unsigned int pci_find_cap_offset(pci_sbdf_t sbdf, unsigned int cap) · line 43

Quoted 8 lines · Jiqian Chen
xen/drivers/pci/pci.c +1 -1
@@ -40,7 +40,7 @@ unsigned int pci_find_cap_offset(pci_sbdf_t sbdf, unsigned int cap)
 }
 
 unsigned int pci_find_next_cap_ttl(pci_sbdf_t sbdf, unsigned int pos,
-                                   const unsigned int caps[], unsigned int n,
+                                   const unsigned int *caps, unsigned int n,

I don't follow the need for this change.

Quoted 7 lines

This changed is for my next patch "vpci/header: Emulate legacy capability list for host".
Currently, vpci only emulates capability list for domU, not for dom0.
For domU, vpci exposes a fixed capability array which calls "supported_caps".
My changes want to emulate capability list for dom0.
I think vpci should expose all devices capabilities to dom0.
When I emulate it, I need to iterate over all capabilities without another fixed array,
so I need this function to return the position of next capability directly when passing a zero length array to this function.

This doesn't answer my question. The change you need for the next patch is the hunk below, not the one above. Aiui at least.

Quoted 10 lines
Quoted 10 lines

xen/drivers/pci/pci.c unsigned int pci_find_next_cap_ttl(pci_sbdf_t sbdf, unsigned int pos, · line 60

Quoted 7 lines
xen/drivers/pci/pci.c +3 -0
@@ -55,6 +55,10 @@ unsigned int pci_find_next_cap_ttl(pci_sbdf_t sbdf, unsigned int pos,
 
         if ( id == 0xff )
             break;
+
+        if ( !caps || n == 0 )
+            return pos;

Checking n to be zero ought to suffice here? In that case it doesn't matter what caps is. Plus if n is non-zero, it clearly is an error if caps was NULL.

Quoted 3 lines

Two checking is to prevent null pointer errors.
But as you said, if checking n to be zero is enough, then I don't need to change the definition of this function. I will change in next version.

If you really wanted to, you could add e.g. ASSERT(caps) after this if().

Jan

#5 lore raw

Re: [PATCH v2 1/8] driver/pci: Get next capability without passing caps

Quoted 21 lines · Jan Beulich
Quoted 12 lines · Jiqian
Quoted 11 lines · Jan Beulich

xen/drivers/pci/pci.c unsigned int pci_find_cap_offset(pci_sbdf_t sbdf, unsigned int cap) · line 43

Quoted 8 lines · Jiqian Chen
xen/drivers/pci/pci.c +1 -1
@@ -40,7 +40,7 @@ unsigned int pci_find_cap_offset(pci_sbdf_t sbdf, unsigned int cap)
 }
 
 unsigned int pci_find_next_cap_ttl(pci_sbdf_t sbdf, unsigned int pos,
-                                   const unsigned int caps[], unsigned int n,
+                                   const unsigned int *caps, unsigned int n,

I don't follow the need for this change.

Quoted 7 lines

This changed is for my next patch "vpci/header: Emulate legacy capability list for host".
Currently, vpci only emulates capability list for domU, not for dom0.
For domU, vpci exposes a fixed capability array which calls "supported_caps".
My changes want to emulate capability list for dom0.
I think vpci should expose all devices capabilities to dom0.
When I emulate it, I need to iterate over all capabilities without another fixed array,
so I need this function to return the position of next capability directly when passing a zero length array to this function.

Quoted 2 lines

This doesn't answer my question. The change you need for the next patch is the hunk below, not the one above. Aiui at least.

Ah, yes.
I will abandon changing this function definition in next version since it just needs to check "n == 0".

Quoted 11 lines
Quoted 11 lines
Quoted 11 lines

xen/drivers/pci/pci.c unsigned int pci_find_next_cap_ttl(pci_sbdf_t sbdf, unsigned int pos, · line 60

Quoted 8 lines
xen/drivers/pci/pci.c +3 -0
@@ -55,6 +55,10 @@ unsigned int pci_find_next_cap_ttl(pci_sbdf_t sbdf, unsigned int pos,
 
         if ( id == 0xff )
             break;
+
+        if ( !caps || n == 0 )
+            return pos;

Checking n to be zero ought to suffice here? In that case it doesn't matter what caps is. Plus if n is non-zero, it clearly is an error if caps was NULL.

Quoted 5 lines
Quoted 3 lines

Two checking is to prevent null pointer errors.
But as you said, if checking n to be zero is enough, then I don't need to change the definition of this function. I will change in next version.

If you really wanted to, you could add e.g. ASSERT(caps) after this if().

OK, will add in next version, too.

Quoted 2 lines

Jan

Signature · 2 lines
Best regards,
Jiqian Chen.
#6 lore raw

PATCH v2 2/8 vpci/header: Emulate legacy capability list for host

In reply to Jiqian Chen

Current logic of init_header() only emulates legacy capability list for guest, expand it to emulate for host too. So that it will be easy to hide a capability whose initialization fails and no need to distinguish host or guest.

---
cc: "Roger Pau Monné" <[email protected]>
---
v1->v2 changes:
new patch

Best regards,
Jiqian Chen.

---
 xen/drivers/vpci/header.c | 139 ++++++++++++++++++++------------------
 1 file changed, 74 insertions(+), 65 deletions(-)
xen/drivers/vpci/header.c +74 -65
index ef6c965c081c..0910eb940e23 100644
@@ -745,6 +745,76 @@ static int bar_add_rangeset(const struct pci_dev *pdev, struct vpci_bar *bar,
     return !bar->mem ? -ENOMEM : 0;
 }
 
+/* These capabilities can be exposed to the guest, that vPCI can handle. */
+static const unsigned int guest_supported_caps[] = {
+    PCI_CAP_ID_MSI,
+    PCI_CAP_ID_MSIX,
+};
+
+static int vpci_init_capability_list(struct pci_dev *pdev)
+{
+    int rc;
+    bool mask_cap_list = false;
+    bool is_hwdom = is_hardware_domain(pdev->domain);
+    const unsigned int *caps = is_hwdom ? NULL : guest_supported_caps;
+    const unsigned int n = is_hwdom ? 0 : ARRAY_SIZE(guest_supported_caps);
+
+    if ( pci_conf_read16(pdev->sbdf, PCI_STATUS) & PCI_STATUS_CAP_LIST )
+    {
+        unsigned int next, ttl = 48;
+
+        next = pci_find_next_cap_ttl(pdev->sbdf, PCI_CAPABILITY_LIST,
+                                     caps, n, &ttl);
+
+        rc = vpci_add_register(pdev->vpci, vpci_read_val, NULL,
+                               PCI_CAPABILITY_LIST, 1,
+                               (void *)(uintptr_t)next);
+        if ( rc )
+            return rc;
+
+        next &= ~3;
+
+        if ( !next && !is_hwdom )
+            /*
+             * If we don't have any supported capabilities to expose to the
+             * guest, mask the PCI_STATUS_CAP_LIST bit in the status register.
+             */
+            mask_cap_list = true;
+
+        while ( next && ttl )
+        {
+            unsigned int pos = next;
+
+            next = pci_find_next_cap_ttl(pdev->sbdf, pos + PCI_CAP_LIST_NEXT,
+                                         caps, n, &ttl);
+
+            rc = vpci_add_register(pdev->vpci, vpci_hw_read8, NULL,
+                                   pos + PCI_CAP_LIST_ID, 1, NULL);
+            if ( rc )
+                return rc;
+
+            rc = vpci_add_register(pdev->vpci, vpci_read_val, NULL,
+                                   pos + PCI_CAP_LIST_NEXT, 1,
+                                   (void *)(uintptr_t)next);
+            if ( rc )
+                return rc;
+
+            next &= ~3;
+        }
+    }
+
+    /* Utilize rsvdp_mask to hide PCI_STATUS_CAP_LIST from the guest. */
+    rc = vpci_add_register_mask(pdev->vpci, vpci_hw_read16, vpci_hw_write16,
+                                PCI_STATUS, 2, NULL,
+                                PCI_STATUS_RO_MASK &
+                                    ~(mask_cap_list ? PCI_STATUS_CAP_LIST : 0),
+                                PCI_STATUS_RW1C_MASK,
+                                mask_cap_list ? PCI_STATUS_CAP_LIST : 0,
+                                PCI_STATUS_RSVDZ_MASK);
+
+    return rc;
+}
+
 static int cf_check init_header(struct pci_dev *pdev)
 {
     uint16_t cmd;
@@ -753,7 +823,6 @@ static int cf_check init_header(struct pci_dev *pdev)
     struct vpci_header *header = &pdev->vpci->header;
     struct vpci_bar *bars = header->bars;
     int rc;
-    bool mask_cap_list = false;
     bool is_hwdom = is_hardware_domain(pdev->domain);
 
     ASSERT(rw_is_write_locked(&pdev->domain->pci_lock));
@@ -794,61 +863,12 @@ static int cf_check init_header(struct pci_dev *pdev)
     if ( rc )
         return rc;
 
+    rc = vpci_init_capability_list(pdev);
+    if ( rc )
+        return rc;
+
     if ( !is_hwdom )
     {
-        if ( pci_conf_read16(pdev->sbdf, PCI_STATUS) & PCI_STATUS_CAP_LIST )
-        {
-            /* Only expose capabilities to the guest that vPCI can handle. */
-            unsigned int next, ttl = 48;
-            static const unsigned int supported_caps[] = {
-                PCI_CAP_ID_MSI,
-                PCI_CAP_ID_MSIX,
-            };
-
-            next = pci_find_next_cap_ttl(pdev->sbdf, PCI_CAPABILITY_LIST,
-                                         supported_caps,
-                                         ARRAY_SIZE(supported_caps), &ttl);
-
-            rc = vpci_add_register(pdev->vpci, vpci_read_val, NULL,
-                                   PCI_CAPABILITY_LIST, 1,
-                                   (void *)(uintptr_t)next);
-            if ( rc )
-                return rc;
-
-            next &= ~3;
-
-            if ( !next )
-                /*
-                 * If we don't have any supported capabilities to expose to the
-                 * guest, mask the PCI_STATUS_CAP_LIST bit in the status
-                 * register.
-                 */
-                mask_cap_list = true;
-
-            while ( next && ttl )
-            {
-                unsigned int pos = next;
-
-                next = pci_find_next_cap_ttl(pdev->sbdf,
-                                             pos + PCI_CAP_LIST_NEXT,
-                                             supported_caps,
-                                             ARRAY_SIZE(supported_caps), &ttl);
-
-                rc = vpci_add_register(pdev->vpci, vpci_hw_read8, NULL,
-                                       pos + PCI_CAP_LIST_ID, 1, NULL);
-                if ( rc )
-                    return rc;
-
-                rc = vpci_add_register(pdev->vpci, vpci_read_val, NULL,
-                                       pos + PCI_CAP_LIST_NEXT, 1,
-                                       (void *)(uintptr_t)next);
-                if ( rc )
-                    return rc;
-
-                next &= ~3;
-            }
-        }
-
         /* Extended capabilities read as zero, write ignore */
         rc = vpci_add_register(pdev->vpci, vpci_read_val, NULL, 0x100, 4,
                                (void *)0);
@@ -856,17 +876,6 @@ static int cf_check init_header(struct pci_dev *pdev)
             return rc;
     }
 
-    /* Utilize rsvdp_mask to hide PCI_STATUS_CAP_LIST from the guest. */
-    rc = vpci_add_register_mask(pdev->vpci, vpci_hw_read16, vpci_hw_write16,
-                                PCI_STATUS, 2, NULL,
-                                PCI_STATUS_RO_MASK &
-                                    ~(mask_cap_list ? PCI_STATUS_CAP_LIST : 0),
-                                PCI_STATUS_RW1C_MASK,
-                                mask_cap_list ? PCI_STATUS_CAP_LIST : 0,
-                                PCI_STATUS_RSVDZ_MASK);
-    if ( rc )
-        return rc;
-
     if ( pdev->ignore_bars )
         return 0;
 
Signature · 1 line
2.34.1
#7 lore raw

Re: [PATCH v2 2/8] vpci/header: Emulate legacy capability list for host

Quoted 4 lines · Jiqian Chen

Current logic of init_header() only emulates legacy capability list for guest, expand it to emulate for host too. So that it will be easy to hide a capability whose initialization fails and no need to distinguish host or guest.

Before I (maybe) look at the patch as a hole: When you say "guest" you mean "DomU" (which is a common pair of synonyms we use), whereas when you say "host" you mean "Dom0"? The latter is not a common pair of synonyms; "host" generally covers the entire system, including Xen and all domains. See e.g. wording that we use in XSAs.

Jan

#8 lore raw

Re: [PATCH v2 2/8] vpci/header: Emulate legacy capability list for host

Quoted 11 lines · Jan Beulich
Quoted 4 lines · Jiqian Chen

Current logic of init_header() only emulates legacy capability list for guest, expand it to emulate for host too. So that it will be easy to hide a capability whose initialization fails and no need to distinguish host or guest.

Before I (maybe) look at the patch as a hole: When you say "guest" you mean "DomU" (which is a common pair of synonyms we use), whereas when you say "host" you mean "Dom0"? The latter is not a common pair of synonyms; "host" generally covers the entire system, including Xen and all domains. See e.g. wording that we use in XSAs.

Yes, guest means domU, host means dom0(or hardware domain?), I will change my word in next version Thank you!

Quoted 2 lines

Jan

Signature · 2 lines
Best regards,
Jiqian Chen.
#9 lore raw

Re: [PATCH v2 2/8] vpci/header: Emulate legacy capability list for host

Quoted 7 lines · Jiqian
Quoted 6 lines · Jan Beulich
Quoted 4 lines · Jiqian Chen

Current logic of init_header() only emulates legacy capability list for guest, expand it to emulate for host too. So that it will be easy to hide a capability whose initialization fails and no need to distinguish host or guest.

Quoted 6 lines
Quoted 5 lines

Before I (maybe) look at the patch as a hole: When you say "guest" you mean "DomU" (which is a common pair of synonyms we use), whereas when you say "host" you mean "Dom0"? The latter is not a common pair of synonyms; "host" generally covers the entire system, including Xen and all domains. See e.g. wording that we use in XSAs.

Yes, guest means domU, host means dom0(or hardware domain?),

We're less strict about that latter distinction - in most contexts dom0 == hwdom is assumed.

Quoted 1 line

I will change my word in next version

Thanks.

Jan

#10 lore raw

Re: [PATCH v2 2/8] vpci/header: Emulate legacy capability list for host

In reply to Jiqian Chen
Quoted 4 lines · Jiqian Chen

Current logic of init_header() only emulates legacy capability list for guest, expand it to emulate for host too. So that it will be easy to hide a capability whose initialization fails and no need to distinguish host or guest.

It might be best if the initial code movement of the logic in init_header() into it's own separate function was done as a non-functional change, and a later patch added support for dom0.

It's easier to then spot the differences that you are adding to support dom0.

Quoted 18 lines

---
cc: "Roger Pau Monné" <[email protected]>
---
v1->v2 changes:
new patch

Best regards,
Jiqian Chen.

---
 xen/drivers/vpci/header.c | 139 ++++++++++++++++++++------------------
 1 file changed, 74 insertions(+), 65 deletions(-)
xen/drivers/vpci/header.c +0 -0
index ef6c965c081c..0910eb940e23 100644

xen/drivers/vpci/header.c static int bar_add_rangeset(const struct pci_dev *pdev, struct vpci_bar *bar, · line 752

Quoted 8 lines
xen/drivers/vpci/header.c +5 -0
@@ -745,6 +745,76 @@ static int bar_add_rangeset(const struct pci_dev *pdev, struct vpci_bar *bar,
     return !bar->mem ? -ENOMEM : 0;
 }
 
+/* These capabilities can be exposed to the guest, that vPCI can handle. */
+static const unsigned int guest_supported_caps[] = {
+    PCI_CAP_ID_MSI,
+    PCI_CAP_ID_MSIX,
+};

Is there a reason this needs to be defined outside of the function scope? So far it's only used by vpci_init_capability_list().

Quoted 30 lines
xen/drivers/vpci/header.c +30 -0
+
+static int vpci_init_capability_list(struct pci_dev *pdev)
+{
+    int rc;
+    bool mask_cap_list = false;
+    bool is_hwdom = is_hardware_domain(pdev->domain);
+    const unsigned int *caps = is_hwdom ? NULL : guest_supported_caps;
+    const unsigned int n = is_hwdom ? 0 : ARRAY_SIZE(guest_supported_caps);
+
+    if ( pci_conf_read16(pdev->sbdf, PCI_STATUS) & PCI_STATUS_CAP_LIST )
+    {
+        unsigned int next, ttl = 48;
+
+        next = pci_find_next_cap_ttl(pdev->sbdf, PCI_CAPABILITY_LIST,
+                                     caps, n, &ttl);
+
+        rc = vpci_add_register(pdev->vpci, vpci_read_val, NULL,
+                               PCI_CAPABILITY_LIST, 1,
+                               (void *)(uintptr_t)next);
+        if ( rc )
+            return rc;
+
+        next &= ~3;
+
+        if ( !next && !is_hwdom )
+            /*
+             * If we don't have any supported capabilities to expose to the
+             * guest, mask the PCI_STATUS_CAP_LIST bit in the status register.
+             */
+            mask_cap_list = true;

xen/drivers/vpci/header.c static int bar_add_rangeset(const struct pci_dev *pdev, struct vpci_bar *bar, · line 792

Quoted 10 lines
xen/drivers/vpci/header.c +10 -0
+
+        while ( next && ttl )
+        {
+            unsigned int pos = next;
+
+            next = pci_find_next_cap_ttl(pdev->sbdf, pos + PCI_CAP_LIST_NEXT,
+                                         caps, n, &ttl);
+
+            rc = vpci_add_register(pdev->vpci, vpci_hw_read8, NULL,
+                                   pos + PCI_CAP_LIST_ID, 1, NULL);

There's no need to add this handler for the hardware domain, that's already the default behavior in that case.

Thanks, Roger.

#11 lore raw

Re: [PATCH v2 2/8] vpci/header: Emulate legacy capability list for host

Quoted 6 lines · Roger Pau Monné
Quoted 4 lines · Jiqian Chen

Current logic of init_header() only emulates legacy capability list for guest, expand it to emulate for host too. So that it will be easy to hide a capability whose initialization fails and no need to distinguish host or guest.

Quoted 6 lines

It might be best if the initial code movement of the logic in init_header() into it's own separate function was done as a non-functional change, and a later patch added support for dom0.

It's easier to then spot the differences that you are adding to support dom0.

Got it, I will re-arrange my patch in next version.

Quoted 22 lines
Quoted 22 lines

---
cc: "Roger Pau Monné" <[email protected]>
---
v1->v2 changes:
new patch

Best regards,
Jiqian Chen.

---
 xen/drivers/vpci/header.c | 139 ++++++++++++++++++++------------------
 1 file changed, 74 insertions(+), 65 deletions(-)
xen/drivers/vpci/header.c +0 -0
index ef6c965c081c..0910eb940e23 100644
@@ -745,6 +745,76 @@ static int bar_add_rangeset(const struct pci_dev *pdev, struct vpci_bar *bar,
     return !bar->mem ? -ENOMEM : 0;
 }
 
Quoted 8 lines

xen/drivers/vpci/header.c static int bar_add_rangeset(const struct pci_dev *pdev, struct vpci_bar *bar, · line 752

Quoted 5 lines
xen/drivers/vpci/header.c +5 -0
+/* These capabilities can be exposed to the guest, that vPCI can handle. */
+static const unsigned int guest_supported_caps[] = {
+    PCI_CAP_ID_MSI,
+    PCI_CAP_ID_MSIX,
+};

Is there a reason this needs to be defined outside of the function scope? So far it's only used by vpci_init_capability_list().

Because, for dom0 I don't need to pass this array, so I need to set below parameter "caps" to be NULL or guest_supported_caps according to the type of domain. If inside the function, I can't to do that since "caps" is const, I think.
Do you have any suggestions?

Quoted 36 lines
Quoted 36 lines
xen/drivers/vpci/header.c +35 -0
+
+static int vpci_init_capability_list(struct pci_dev *pdev)
+{
+    int rc;
+    bool mask_cap_list = false;
+    bool is_hwdom = is_hardware_domain(pdev->domain);
+    const unsigned int *caps = is_hwdom ? NULL : guest_supported_caps;
+    const unsigned int n = is_hwdom ? 0 : ARRAY_SIZE(guest_supported_caps);
+
+    if ( pci_conf_read16(pdev->sbdf, PCI_STATUS) & PCI_STATUS_CAP_LIST )
+    {
+        unsigned int next, ttl = 48;
+
+        next = pci_find_next_cap_ttl(pdev->sbdf, PCI_CAPABILITY_LIST,
+                                     caps, n, &ttl);
+
+        rc = vpci_add_register(pdev->vpci, vpci_read_val, NULL,
+                               PCI_CAPABILITY_LIST, 1,
+                               (void *)(uintptr_t)next);
+        if ( rc )
+            return rc;
+
+        next &= ~3;
+
+        if ( !next && !is_hwdom )
+            /*
+             * If we don't have any supported capabilities to expose to the
+             * guest, mask the PCI_STATUS_CAP_LIST bit in the status register.
+             */
+            mask_cap_list = true;
+
+        while ( next && ttl )
+        {
+            unsigned int pos = next;
+
Quoted 8 lines

xen/drivers/vpci/header.c static int bar_add_rangeset(const struct pci_dev *pdev, struct vpci_bar *bar, · line 792

Quoted 5 lines
xen/drivers/vpci/header.c +5 -0
+            next = pci_find_next_cap_ttl(pdev->sbdf, pos + PCI_CAP_LIST_NEXT,
+                                         caps, n, &ttl);
+
+            rc = vpci_add_register(pdev->vpci, vpci_hw_read8, NULL,
+                                   pos + PCI_CAP_LIST_ID, 1, NULL);

There's no need to add this handler for the hardware domain, that's already the default behavior in that case.

But if not, I have no handler to remove from capability list in next patch's hiding function vpci_capability_mask(), then I can't success to hide it.

Quoted 2 lines

Thanks, Roger.

Signature · 2 lines
Best regards,
Jiqian Chen.
#12 lore raw

Re: [PATCH v2 2/8] vpci/header: Emulate legacy capability list for host

Quoted 16 lines · Jiqian
Quoted 7 lines · Roger Pau Monné
Quoted 6 lines · Jiqian Chen
xen/drivers/vpci/header.c +0 -0
@@ -745,6 +745,76 @@ static int bar_add_rangeset(const struct pci_dev *pdev, struct vpci_bar *bar,
     return !bar->mem ? -ENOMEM : 0;
 }
 
Quoted 8 lines

xen/drivers/vpci/header.c static int bar_add_rangeset(const struct pci_dev *pdev, struct vpci_bar *bar, · line 752

Quoted 5 lines
xen/drivers/vpci/header.c +5 -0
+/* These capabilities can be exposed to the guest, that vPCI can handle. */
+static const unsigned int guest_supported_caps[] = {
+    PCI_CAP_ID_MSI,
+    PCI_CAP_ID_MSIX,
+};

Is there a reason this needs to be defined outside of the function scope? So far it's only used by vpci_init_capability_list().

Quoted 2 lines

Because, for dom0 I don't need to pass this array, so I need to set below parameter "caps" to be NULL or guest_supported_caps according to the type of domain. If inside the function, I can't to do that since "caps" is const, I think.

Why would the more narrow scope of the array affect how it can be used?

Jan

#13 lore raw

Re: [PATCH v2 2/8] vpci/header: Emulate legacy capability list for host

Quoted 17 lines · Jan Beulich
Quoted 16 lines · Jiqian
Quoted 15 lines · Roger Pau Monné

xen/drivers/vpci/header.c static int bar_add_rangeset(const struct pci_dev *pdev, struct vpci_bar *bar, · line 752

Quoted 11 lines · Jiqian Chen
xen/drivers/vpci/header.c +5 -0
@@ -745,6 +745,76 @@ static int bar_add_rangeset(const struct pci_dev *pdev, struct vpci_bar *bar,
     return !bar->mem ? -ENOMEM : 0;
 }
 
+/* These capabilities can be exposed to the guest, that vPCI can handle. */
+static const unsigned int guest_supported_caps[] = {
+    PCI_CAP_ID_MSI,
+    PCI_CAP_ID_MSIX,
+};

Is there a reason this needs to be defined outside of the function scope? So far it's only used by vpci_init_capability_list().

Quoted 4 lines
Quoted 2 lines

Because, for dom0 I don't need to pass this array, so I need to set below parameter "caps" to be NULL or guest_supported_caps according to the type of domain. If inside the function, I can't to do that since "caps" is const, I think.

Why would the more narrow scope of the array affect how it can be used?

Sorry, I have thought about it again and it can be put it inside the function. I will change in the next version.

Quoted 2 lines

Jan

Signature · 2 lines
Best regards,
Jiqian Chen.
#14 lore raw

Re: [PATCH v2 2/8] vpci/header: Emulate legacy capability list for host

In reply to Chen, Jiqian
Quoted 44 lines · Jiqian
Quoted 35 lines · Roger Pau Monné
Quoted 34 lines · Jiqian Chen
xen/drivers/vpci/header.c +34 -0
+static int vpci_init_capability_list(struct pci_dev *pdev)
+{
+    int rc;
+    bool mask_cap_list = false;
+    bool is_hwdom = is_hardware_domain(pdev->domain);
+    const unsigned int *caps = is_hwdom ? NULL : guest_supported_caps;
+    const unsigned int n = is_hwdom ? 0 : ARRAY_SIZE(guest_supported_caps);
+
+    if ( pci_conf_read16(pdev->sbdf, PCI_STATUS) & PCI_STATUS_CAP_LIST )
+    {
+        unsigned int next, ttl = 48;
+
+        next = pci_find_next_cap_ttl(pdev->sbdf, PCI_CAPABILITY_LIST,
+                                     caps, n, &ttl);
+
+        rc = vpci_add_register(pdev->vpci, vpci_read_val, NULL,
+                               PCI_CAPABILITY_LIST, 1,
+                               (void *)(uintptr_t)next);
+        if ( rc )
+            return rc;
+
+        next &= ~3;
+
+        if ( !next && !is_hwdom )
+            /*
+             * If we don't have any supported capabilities to expose to the
+             * guest, mask the PCI_STATUS_CAP_LIST bit in the status register.
+             */
+            mask_cap_list = true;
+
+        while ( next && ttl )
+        {
+            unsigned int pos = next;
+
Quoted 8 lines

xen/drivers/vpci/header.c static int bar_add_rangeset(const struct pci_dev *pdev, struct vpci_bar *bar, · line 792

Quoted 5 lines
xen/drivers/vpci/header.c +5 -0
+            next = pci_find_next_cap_ttl(pdev->sbdf, pos + PCI_CAP_LIST_NEXT,
+                                         caps, n, &ttl);
+
+            rc = vpci_add_register(pdev->vpci, vpci_hw_read8, NULL,
+                                   pos + PCI_CAP_LIST_ID, 1, NULL);

There's no need to add this handler for the hardware domain, that's already the default behavior in that case.

Quoted 2 lines

But if not, I have no handler to remove from capability list in next patch's hiding function vpci_capability_mask(), then I can't success to hide it.

Oh, I see, I have further comments on that approach, see the comments on the followup patches.

Thanks, Roger.

#15 lore raw

Re: [PATCH v2 2/8] vpci/header: Emulate legacy capability list for host

In reply to Jiqian Chen
Quoted 82 lines · Jiqian Chen

Current logic of init_header() only emulates legacy capability list for guest, expand it to emulate for host too. So that it will be easy to hide a capability whose initialization fails and no need to distinguish host or guest.

---
cc: "Roger Pau Monné" <[email protected]>
---
v1->v2 changes:
new patch

Best regards,
Jiqian Chen.

---
 xen/drivers/vpci/header.c | 139 ++++++++++++++++++++------------------
 1 file changed, 74 insertions(+), 65 deletions(-)
xen/drivers/vpci/header.c +56 -0
index ef6c965c081c..0910eb940e23 100644
@@ -745,6 +745,76 @@ static int bar_add_rangeset(const struct pci_dev *pdev, struct vpci_bar *bar,
     return !bar->mem ? -ENOMEM : 0;
 }
 
+/* These capabilities can be exposed to the guest, that vPCI can handle. */
+static const unsigned int guest_supported_caps[] = {
+    PCI_CAP_ID_MSI,
+    PCI_CAP_ID_MSIX,
+};
+
+static int vpci_init_capability_list(struct pci_dev *pdev)
+{
+    int rc;
+    bool mask_cap_list = false;
+    bool is_hwdom = is_hardware_domain(pdev->domain);
+    const unsigned int *caps = is_hwdom ? NULL : guest_supported_caps;
+    const unsigned int n = is_hwdom ? 0 : ARRAY_SIZE(guest_supported_caps);
+
+    if ( pci_conf_read16(pdev->sbdf, PCI_STATUS) & PCI_STATUS_CAP_LIST )
+    {
+        unsigned int next, ttl = 48;
+
+        next = pci_find_next_cap_ttl(pdev->sbdf, PCI_CAPABILITY_LIST,
+                                     caps, n, &ttl);
+
+        rc = vpci_add_register(pdev->vpci, vpci_read_val, NULL,
+                               PCI_CAPABILITY_LIST, 1,
+                               (void *)(uintptr_t)next);
+        if ( rc )
+            return rc;
+
+        next &= ~3;
+
+        if ( !next && !is_hwdom )
+            /*
+             * If we don't have any supported capabilities to expose to the
+             * guest, mask the PCI_STATUS_CAP_LIST bit in the status register.
+             */
+            mask_cap_list = true;
+
+        while ( next && ttl )
+        {
+            unsigned int pos = next;
+
+            next = pci_find_next_cap_ttl(pdev->sbdf, pos + PCI_CAP_LIST_NEXT,
+                                         caps, n, &ttl);
+
+            rc = vpci_add_register(pdev->vpci, vpci_hw_read8, NULL,
+                                   pos + PCI_CAP_LIST_ID, 1, NULL);
+            if ( rc )
+                return rc;
+
+            rc = vpci_add_register(pdev->vpci, vpci_read_val, NULL,
+                                   pos + PCI_CAP_LIST_NEXT, 1,
+                                   (void *)(uintptr_t)next);
+            if ( rc )
+                return rc;
+
+            next &= ~3;
+        }

xen/drivers/vpci/header.c static int bar_add_rangeset(const struct pci_dev *pdev, struct vpci_bar *bar, · line 813

Quoted 10 lines
xen/drivers/vpci/header.c +10 -0
+    }
+
+    /* Utilize rsvdp_mask to hide PCI_STATUS_CAP_LIST from the guest. */
+    rc = vpci_add_register_mask(pdev->vpci, vpci_hw_read16, vpci_hw_write16,
+                                PCI_STATUS, 2, NULL,
+                                PCI_STATUS_RO_MASK &
+                                    ~(mask_cap_list ? PCI_STATUS_CAP_LIST : 0),
+                                PCI_STATUS_RW1C_MASK,
+                                mask_cap_list ? PCI_STATUS_CAP_LIST : 0,
+                                PCI_STATUS_RSVDZ_MASK);

One further remark I've forgot to make on the previous reply: I'm unsure we want to do this for dom0, as we in general allow dom0 unfiltered write access (unless it's for accesses mediated by Xen).

Thanks, Roger.

#16 lore raw

Re: [PATCH v2 2/8] vpci/header: Emulate legacy capability list for host

Quoted 27 lines · Roger Pau Monné
Quoted 26 lines · Jiqian Chen
xen/drivers/vpci/header.c +26 -0
+        while ( next && ttl )
+        {
+            unsigned int pos = next;
+
+            next = pci_find_next_cap_ttl(pdev->sbdf, pos + PCI_CAP_LIST_NEXT,
+                                         caps, n, &ttl);
+
+            rc = vpci_add_register(pdev->vpci, vpci_hw_read8, NULL,
+                                   pos + PCI_CAP_LIST_ID, 1, NULL);
+            if ( rc )
+                return rc;
+
+            rc = vpci_add_register(pdev->vpci, vpci_read_val, NULL,
+                                   pos + PCI_CAP_LIST_NEXT, 1,
+                                   (void *)(uintptr_t)next);
+            if ( rc )
+                return rc;
+
+            next &= ~3;
+        }
+    }
+
+    /* Utilize rsvdp_mask to hide PCI_STATUS_CAP_LIST from the guest. */
+    rc = vpci_add_register_mask(pdev->vpci, vpci_hw_read16, vpci_hw_write16,
+                                PCI_STATUS, 2, NULL,
+                                PCI_STATUS_RO_MASK &
Quoted 8 lines

xen/drivers/vpci/header.c static int bar_add_rangeset(const struct pci_dev *pdev, struct vpci_bar *bar, · line 813

Quoted 4 lines
xen/drivers/vpci/header.c +4 -0
+                                    ~(mask_cap_list ? PCI_STATUS_CAP_LIST : 0),
+                                PCI_STATUS_RW1C_MASK,
+                                mask_cap_list ? PCI_STATUS_CAP_LIST : 0,
+                                PCI_STATUS_RSVDZ_MASK);

One further remark I've forgot to make on the previous reply: I'm unsure we want to do this for dom0, as we in general allow dom0 unfiltered write access (unless it's for accesses mediated by Xen).

This part is the original implementation before my patches. If you want to restrict this only for domUs, not for dom0. I will add a new first patch to do that.

Quoted 2 lines

Thanks, Roger.

Signature · 2 lines
Best regards,
Jiqian Chen.
#17 lore raw

Re: [PATCH v2 2/8] vpci/header: Emulate legacy capability list for host

Quoted 36 lines · Jiqian
Quoted 27 lines · Roger Pau Monné
Quoted 26 lines · Jiqian Chen
xen/drivers/vpci/header.c +26 -0
+        while ( next && ttl )
+        {
+            unsigned int pos = next;
+
+            next = pci_find_next_cap_ttl(pdev->sbdf, pos + PCI_CAP_LIST_NEXT,
+                                         caps, n, &ttl);
+
+            rc = vpci_add_register(pdev->vpci, vpci_hw_read8, NULL,
+                                   pos + PCI_CAP_LIST_ID, 1, NULL);
+            if ( rc )
+                return rc;
+
+            rc = vpci_add_register(pdev->vpci, vpci_read_val, NULL,
+                                   pos + PCI_CAP_LIST_NEXT, 1,
+                                   (void *)(uintptr_t)next);
+            if ( rc )
+                return rc;
+
+            next &= ~3;
+        }
+    }
+
+    /* Utilize rsvdp_mask to hide PCI_STATUS_CAP_LIST from the guest. */
+    rc = vpci_add_register_mask(pdev->vpci, vpci_hw_read16, vpci_hw_write16,
+                                PCI_STATUS, 2, NULL,
+                                PCI_STATUS_RO_MASK &
Quoted 8 lines

xen/drivers/vpci/header.c static int bar_add_rangeset(const struct pci_dev *pdev, struct vpci_bar *bar, · line 813

Quoted 4 lines
xen/drivers/vpci/header.c +4 -0
+                                    ~(mask_cap_list ? PCI_STATUS_CAP_LIST : 0),
+                                PCI_STATUS_RW1C_MASK,
+                                mask_cap_list ? PCI_STATUS_CAP_LIST : 0,
+                                PCI_STATUS_RSVDZ_MASK);

One further remark I've forgot to make on the previous reply: I'm unsure we want to do this for dom0, as we in general allow dom0 unfiltered write access (unless it's for accesses mediated by Xen).

Quoted 2 lines

This part is the original implementation before my patches. If you want to restrict this only for domUs, not for dom0.

Oh, indeed. I was confused thinking it was inside the !is_hardware_domain() conditional. Leave it like that, your series is already long enough.

Thanks, Roger.

#18 lore raw

PATCH v2 3/8 vpci/header: Emulate extended capability list for host

In reply to Jiqian Chen

Add a new function to emulate extended capability list for host, and call it in init_header(). So that, it will be easy to hide a capability whose initialization fails.

As for the extended capability list of guest, keep hiding it.

---
cc: "Roger Pau Monné" <[email protected]>
---
v1->v2 changes:
new patch

Best regards,
Jiqian Chen.

---
 xen/drivers/vpci/header.c | 44 ++++++++++++++++++++++++++++++++-------
 1 file changed, 36 insertions(+), 8 deletions(-)
xen/drivers/vpci/header.c +36 -8
index 0910eb940e23..6833d456566b 100644
@@ -815,6 +815,39 @@ static int vpci_init_capability_list(struct pci_dev *pdev)
     return rc;
 }
 
+static int vpci_init_ext_capability_list(struct pci_dev *pdev)
+{
+    int rc;
+    u32 header;
+    unsigned int pos = 0x100U, ttl = 480;
+
+    if ( !is_hardware_domain(pdev->domain) )
+    {
+        /* Extended capabilities read as zero, write ignore */
+        rc = vpci_add_register(pdev->vpci, vpci_read_val, NULL,
+                               pos, 4, (void *)0);
+        if ( rc )
+            return rc;
+    }
+
+    while ( pos && ttl-- )
+    {
+        header = pci_conf_read32(pdev->sbdf, pos);
+
+        rc = vpci_add_register(pdev->vpci, vpci_read_val, NULL,
+                               pos, 4, (void *)(uintptr_t)header);
+        if ( rc )
+            return rc;
+
+        if ( (header == 0) || (header == -1) )
+            return 0;
+
+        pos = PCI_EXT_CAP_NEXT(header);
+    }
+
+    return 0;
+}
+
 static int cf_check init_header(struct pci_dev *pdev)
 {
     uint16_t cmd;
@@ -867,14 +900,9 @@ static int cf_check init_header(struct pci_dev *pdev)
     if ( rc )
         return rc;
 
-    if ( !is_hwdom )
-    {
-        /* Extended capabilities read as zero, write ignore */
-        rc = vpci_add_register(pdev->vpci, vpci_read_val, NULL, 0x100, 4,
-                               (void *)0);
-        if ( rc )
-            return rc;
-    }
+    rc = vpci_init_ext_capability_list(pdev);
+    if ( rc )
+        return rc;
 
     if ( pdev->ignore_bars )
         return 0;
Signature · 1 line
2.34.1
#19 lore raw

Re: [PATCH v2 3/8] vpci/header: Emulate extended capability list for host

Quoted 24 lines · Jiqian Chen

Add a new function to emulate extended capability list for host, and call it in init_header(). So that, it will be easy to hide a capability whose initialization fails.

As for the extended capability list of guest, keep hiding it.

---
cc: "Roger Pau Monné" <[email protected]>
---
v1->v2 changes:
new patch

Best regards,
Jiqian Chen.

---
 xen/drivers/vpci/header.c | 44 ++++++++++++++++++++++++++++++++-------
 1 file changed, 36 insertions(+), 8 deletions(-)
xen/drivers/vpci/header.c +0 -0
index 0910eb940e23..6833d456566b 100644

xen/drivers/vpci/header.c static int vpci_init_capability_list(struct pci_dev *pdev) · line 821

Quoted 7 lines
xen/drivers/vpci/header.c +4 -0
@@ -815,6 +815,39 @@ static int vpci_init_capability_list(struct pci_dev *pdev)
     return rc;
 }
 
+static int vpci_init_ext_capability_list(struct pci_dev *pdev)
+{
+    int rc;
+    u32 header;

uint32_t would be preferred.

xen/drivers/vpci/header.c static int vpci_init_capability_list(struct pci_dev *pdev) · line 830

Quoted 9 lines
xen/drivers/vpci/header.c +9 -0
+    unsigned int pos = 0x100U, ttl = 480;
+
+    if ( !is_hardware_domain(pdev->domain) )
+    {
+        /* Extended capabilities read as zero, write ignore */
+        rc = vpci_add_register(pdev->vpci, vpci_read_val, NULL,
+                               pos, 4, (void *)0);
+        if ( rc )
+            return rc;

I think you want to unconditionally return here, otherwise you will most likely add a duplicated handler over "pos" when going inside the loop below?

Also for domU we don't want to expose any extended capabilities yet.

xen/drivers/vpci/header.c static int vpci_init_capability_list(struct pci_dev *pdev) · line 837

Quoted 7 lines
xen/drivers/vpci/header.c +7 -0
+    }
+
+    while ( pos && ttl-- )
+    {
+        header = pci_conf_read32(pdev->sbdf, pos);
+
+        rc = vpci_add_register(pdev->vpci, vpci_read_val, NULL,

You don't want to pass NULL here, as that would prevent dom0 from writing to the register, you instead want to pass vpci_hw_write32 I think.

xen/drivers/vpci/header.c static int vpci_init_capability_list(struct pci_dev *pdev) · line 845

Quoted 8 lines
xen/drivers/vpci/header.c +8 -0
+                               pos, 4, (void *)(uintptr_t)header);
+        if ( rc )
+            return rc;
+
+        if ( (header == 0) || (header == -1) )
+            return 0;
+
+        pos = PCI_EXT_CAP_NEXT(header);

Don't you need to check that pos >= 0x100? Possibly done in the while loop condition: while ( pos >= 0x100 && ... )

Thanks, Roger.

#20 lore raw

Re: [PATCH v2 3/8] vpci/header: Emulate extended capability list for host

Quoted 45 lines · Roger Pau Monné
Quoted 24 lines · Jiqian Chen

Add a new function to emulate extended capability list for host, and call it in init_header(). So that, it will be easy to hide a capability whose initialization fails.

As for the extended capability list of guest, keep hiding it.

---
cc: "Roger Pau Monné" <[email protected]>
---
v1->v2 changes:
new patch

Best regards,
Jiqian Chen.

---
 xen/drivers/vpci/header.c | 44 ++++++++++++++++++++++++++++++++-------
 1 file changed, 36 insertions(+), 8 deletions(-)
xen/drivers/vpci/header.c +0 -0
index 0910eb940e23..6833d456566b 100644

xen/drivers/vpci/header.c static int vpci_init_capability_list(struct pci_dev *pdev) · line 821

Quoted 7 lines
xen/drivers/vpci/header.c +4 -0
@@ -815,6 +815,39 @@ static int vpci_init_capability_list(struct pci_dev *pdev)
     return rc;
 }
 
+static int vpci_init_ext_capability_list(struct pci_dev *pdev)
+{
+    int rc;
+    u32 header;

uint32_t would be preferred.

xen/drivers/vpci/header.c static int vpci_init_capability_list(struct pci_dev *pdev) · line 830

Quoted 9 lines
xen/drivers/vpci/header.c +9 -0
+    unsigned int pos = 0x100U, ttl = 480;
+
+    if ( !is_hardware_domain(pdev->domain) )
+    {
+        /* Extended capabilities read as zero, write ignore */
+        rc = vpci_add_register(pdev->vpci, vpci_read_val, NULL,
+                               pos, 4, (void *)0);
+        if ( rc )
+            return rc;
Quoted 3 lines

I think you want to unconditionally return here, otherwise you will most likely add a duplicated handler over "pos" when going inside the loop below?

Oh, it's my bad.
I should return here for any rc.

Quoted 14 lines

Also for domU we don't want to expose any extended capabilities yet.

xen/drivers/vpci/header.c static int vpci_init_capability_list(struct pci_dev *pdev) · line 837

Quoted 7 lines
xen/drivers/vpci/header.c +7 -0
+    }
+
+    while ( pos && ttl-- )
+    {
+        header = pci_conf_read32(pdev->sbdf, pos);
+
+        rc = vpci_add_register(pdev->vpci, vpci_read_val, NULL,

You don't want to pass NULL here, as that would prevent dom0 from writing to the register, you instead want to pass vpci_hw_write32 I think.

Will change in next version.

Quoted 12 lines

xen/drivers/vpci/header.c static int vpci_init_capability_list(struct pci_dev *pdev) · line 845

Quoted 9 lines
xen/drivers/vpci/header.c +8 -0
+                               pos, 4, (void *)(uintptr_t)header);
+        if ( rc )
+            return rc;
+
+        if ( (header == 0) || (header == -1) )
+            return 0;
+
+        pos = PCI_EXT_CAP_NEXT(header);

Don't you need to check that pos >= 0x100? Possibly done in the while loop condition: while ( pos >= 0x100 && ... )

Yes, will change in next version.

Quoted 2 lines

Thanks, Roger.

Signature · 2 lines
Best regards,
Jiqian Chen.
#21 lore raw

Re: [PATCH v2 3/8] vpci/header: Emulate extended capability list for host

In reply to Jiqian Chen
Quoted 22 lines · Jiqian Chen
xen/drivers/vpci/header.c +16 -0
@@ -815,6 +815,39 @@ static int vpci_init_capability_list(struct pci_dev *pdev)
     return rc;
 }
 
+static int vpci_init_ext_capability_list(struct pci_dev *pdev)
+{
+    int rc;
+    u32 header;
+    unsigned int pos = 0x100U, ttl = 480;
+
+    if ( !is_hardware_domain(pdev->domain) )
+    {
+        /* Extended capabilities read as zero, write ignore */
+        rc = vpci_add_register(pdev->vpci, vpci_read_val, NULL,
+                               pos, 4, (void *)0);
+        if ( rc )
+            return rc;
+    }
+
+    while ( pos && ttl-- )

xen/drivers/vpci/header.c static int vpci_init_capability_list(struct pci_dev *pdev) · line 843

Quoted 10 lines
xen/drivers/vpci/header.c +10 -0
+    {
+        header = pci_conf_read32(pdev->sbdf, pos);
+
+        rc = vpci_add_register(pdev->vpci, vpci_read_val, NULL,
+                               pos, 4, (void *)(uintptr_t)header);
+        if ( rc )
+            return rc;
+
+        if ( (header == 0) || (header == -1) )
+            return 0;

I realize pci_find_next_ext_capability() also has such a check, but even there it's not really clear to me why compare not only against 0, but also again -1 (aka ~0).

Jan

#22 lore raw

Re: [PATCH v2 3/8] vpci/header: Emulate extended capability list for host

Quoted 29 lines · Jan Beulich
Quoted 28 lines · Jiqian Chen
xen/drivers/vpci/header.c +22 -0
@@ -815,6 +815,39 @@ static int vpci_init_capability_list(struct pci_dev *pdev)
     return rc;
 }
 
+static int vpci_init_ext_capability_list(struct pci_dev *pdev)
+{
+    int rc;
+    u32 header;
+    unsigned int pos = 0x100U, ttl = 480;
+
+    if ( !is_hardware_domain(pdev->domain) )
+    {
+        /* Extended capabilities read as zero, write ignore */
+        rc = vpci_add_register(pdev->vpci, vpci_read_val, NULL,
+                               pos, 4, (void *)0);
+        if ( rc )
+            return rc;
+    }
+
+    while ( pos && ttl-- )
+    {
+        header = pci_conf_read32(pdev->sbdf, pos);
+
+        rc = vpci_add_register(pdev->vpci, vpci_read_val, NULL,
+                               pos, 4, (void *)(uintptr_t)header);
+        if ( rc )
Quoted 8 lines

xen/drivers/vpci/header.c static int vpci_init_capability_list(struct pci_dev *pdev) · line 843

Quoted 4 lines
xen/drivers/vpci/header.c +4 -0
+            return rc;
+
+        if ( (header == 0) || (header == -1) )
+            return 0;

I realize pci_find_next_ext_capability() also has such a check, but even there it's not really clear to me why compare not only against 0, but also again -1 (aka ~0).

Thank you for raising this question.
When I coded this part, I also had this confuse since pci_find_next_ext_capability() has this check, so I chose to keep the same check.
Do you think I need to remove this -1 check?

Quoted 2 lines

Jan

Signature · 2 lines
Best regards,
Jiqian Chen.
#23 lore raw

Re: [PATCH v2 3/8] vpci/header: Emulate extended capability list for host

Quoted 38 lines · Jiqian
Quoted 29 lines · Jan Beulich
Quoted 28 lines · Jiqian Chen
xen/drivers/vpci/header.c +22 -0
@@ -815,6 +815,39 @@ static int vpci_init_capability_list(struct pci_dev *pdev)
     return rc;
 }
 
+static int vpci_init_ext_capability_list(struct pci_dev *pdev)
+{
+    int rc;
+    u32 header;
+    unsigned int pos = 0x100U, ttl = 480;
+
+    if ( !is_hardware_domain(pdev->domain) )
+    {
+        /* Extended capabilities read as zero, write ignore */
+        rc = vpci_add_register(pdev->vpci, vpci_read_val, NULL,
+                               pos, 4, (void *)0);
+        if ( rc )
+            return rc;
+    }
+
+    while ( pos && ttl-- )
+    {
+        header = pci_conf_read32(pdev->sbdf, pos);
+
+        rc = vpci_add_register(pdev->vpci, vpci_read_val, NULL,
+                               pos, 4, (void *)(uintptr_t)header);
+        if ( rc )
Quoted 8 lines

xen/drivers/vpci/header.c static int vpci_init_capability_list(struct pci_dev *pdev) · line 843

Quoted 4 lines
xen/drivers/vpci/header.c +4 -0
+            return rc;
+
+        if ( (header == 0) || (header == -1) )
+            return 0;

I realize pci_find_next_ext_capability() also has such a check, but even there it's not really clear to me why compare not only against 0, but also again -1 (aka ~0).

Quoted 4 lines

Thank you for raising this question.
When I coded this part, I also had this confuse since pci_find_next_ext_capability() has this check, so I chose to keep the same check.
Do you think I need to remove this -1 check?

Unless you can explain why it's needed (perhaps by figuring out why the other one is there), I'd say - yes.

Jan

#24 lore raw

PATCH v2 4/8 vpci: Hide capability when it fails to initialize

In reply to Jiqian Chen

When vpci fails to initialize a capability of a device, it just return error instead of catching and processing exception. That makes the entire device unusable.

So, refactor REGISTER_VPCI_INIT to contain more capability specific information, and use new functions to hide capability when initialization fails in vpci_assign_device().

Those new functions remove the failed legacy/extended capability from the emulated legacy/extended capability list.

What's more, change the definition of init_header() since it is not a capability and it is needed for all devices' PCI config space.

Note: call vpci_make_msix_hole() in the end of init_msix() since the change of sequence of init_header() and init_msix().

---
cc: "Roger Pau Monné" <[email protected]>
cc: Andrew Cooper <[email protected]>
cc: Anthony PERARD <[email protected]>
cc: Michal Orzel <[email protected]>
cc: Jan Beulich <[email protected]>
cc: Julien Grall <[email protected]>
cc: Stefano Stabellini <[email protected]>
---
v1->v2 changes:
* Removed the "priorities" of initializing capabilities since it isn't used anymore.
* Added new function vpci_capability_mask() and vpci_ext_capability_mask() to remove failed capability from list.
* Called vpci_make_msix_hole() in the end of init_msix().

Best regards,
Jiqian Chen.

---
 xen/drivers/vpci/header.c  |   3 +-
 xen/drivers/vpci/msi.c     |   2 +-
 xen/drivers/vpci/msix.c    |   8 +-
 xen/drivers/vpci/rebar.c   |   2 +-
 xen/drivers/vpci/vpci.c    | 175 +++++++++++++++++++++++++++++++------
 xen/include/xen/pci_regs.h |   1 +
 xen/include/xen/vpci.h     |  26 ++++--
 xen/include/xen/xen.lds.h  |   2 +-
 8 files changed, 179 insertions(+), 40 deletions(-)
xen/drivers/vpci/header.c +1 -2
index 6833d456566b..51a67d76ad8a 100644
@@ -848,7 +848,7 @@ static int vpci_init_ext_capability_list(struct pci_dev *pdev)
     return 0;
 }
 
-static int cf_check init_header(struct pci_dev *pdev)
+int vpci_init_header(struct pci_dev *pdev)
 {
     uint16_t cmd;
     uint64_t addr, size;
@@ -1044,7 +1044,6 @@ static int cf_check init_header(struct pci_dev *pdev)
     pci_conf_write16(pdev->sbdf, PCI_COMMAND, cmd);
     return rc;
 }
-REGISTER_VPCI_INIT(init_header, VPCI_PRIORITY_MIDDLE);
 
 /*
  * Local variables:
xen/drivers/vpci/msi.c +1 -1
index 66e5a8a116be..ca89ae9b9c22 100644
@@ -270,7 +270,7 @@ static int cf_check init_msi(struct pci_dev *pdev)
 
     return 0;
 }
-REGISTER_VPCI_INIT(init_msi, VPCI_PRIORITY_LOW);
+REGISTER_VPCI_LEGACY_CAP(PCI_CAP_ID_MSI, init_msi);
 
 void vpci_dump_msi(void)
 {
xen/drivers/vpci/msix.c +6 -2
index 6bd8c55bb48e..6537374c79a0 100644
@@ -751,9 +751,13 @@ static int cf_check init_msix(struct pci_dev *pdev)
     pdev->vpci->msix = msix;
     list_add(&msix->next, &d->arch.hvm.msix_tables);
 
-    return 0;
+    spin_lock(&pdev->vpci->lock);
+    rc = vpci_make_msix_hole(pdev);
+    spin_unlock(&pdev->vpci->lock);
+
+    return rc
 }
-REGISTER_VPCI_INIT(init_msix, VPCI_PRIORITY_HIGH);
+REGISTER_VPCI_LEGACY_CAP(PCI_CAP_ID_MSIX, init_msix);
 
 /*
  * Local variables:
xen/drivers/vpci/rebar.c +1 -1
index 793937449af7..79858e5dc92f 100644
@@ -118,7 +118,7 @@ static int cf_check init_rebar(struct pci_dev *pdev)
 
     return 0;
 }
-REGISTER_VPCI_INIT(init_rebar, VPCI_PRIORITY_LOW);
+REGISTER_VPCI_EXTEND_CAP(PCI_EXT_CAP_ID_REBAR, init_rebar);
 
 /*
  * Local variables:
xen/drivers/vpci/vpci.c +150 -25

206 lines not on this page · raw message

xen/include/xen/pci_regs.h +1 -0
index 27b4f44eedf3..5fe6653fded4 100644
@@ -449,6 +449,7 @@
 #define PCI_EXT_CAP_ID(header)		((header) & 0x0000ffff)
 #define PCI_EXT_CAP_VER(header)		(((header) >> 16) & 0xf)
 #define PCI_EXT_CAP_NEXT(header)	(((header) >> 20) & 0xffc)
+#define PCI_EXT_CAP_NEXT_MASK		0xFFC00000U
 
 #define PCI_EXT_CAP_ID_ERR	1
 #define PCI_EXT_CAP_ID_VC	2
xen/include/xen/vpci.h +18 -8
index 807401b2eaa2..5016ded64d89 100644
@@ -13,11 +13,11 @@ typedef uint32_t vpci_read_t(const struct pci_dev *pdev, unsigned int reg,
 typedef void vpci_write_t(const struct pci_dev *pdev, unsigned int reg,
                           uint32_t val, void *data);
 
-typedef int vpci_register_init_t(struct pci_dev *dev);
-
-#define VPCI_PRIORITY_HIGH      "1"
-#define VPCI_PRIORITY_MIDDLE    "5"
-#define VPCI_PRIORITY_LOW       "9"
+typedef struct {
+    unsigned int id;
+    bool is_ext;
+    int (*init)(struct pci_dev *pdev);
+} vpci_capability_t;
 
 #define VPCI_ECAM_BDF(addr)     (((addr) & 0x0ffff000) >> 12)
 
@@ -29,9 +29,19 @@ typedef int vpci_register_init_t(struct pci_dev *dev);
  */
 #define VPCI_MAX_VIRT_DEV       (PCI_SLOT(~0) + 1)
 
-#define REGISTER_VPCI_INIT(x, p)                \
-  static vpci_register_init_t *const x##_entry  \
-               __used_section(".data.vpci." p) = (x)
+#define REGISTER_VPCI_CAP(cap, x, ext) \
+  static vpci_capability_t x##_t = { \
+        .id = (cap), \
+        .init = (x), \
+        .is_ext = (ext), \
+  }; \
+  static vpci_capability_t *const x##_entry  \
+               __used_section(".data.vpci.") = &(x##_t)
+
+#define REGISTER_VPCI_LEGACY_CAP(cap, x) REGISTER_VPCI_CAP(cap, x, false)
+#define REGISTER_VPCI_EXTEND_CAP(cap, x) REGISTER_VPCI_CAP(cap, x, true)
+
+int __must_check vpci_init_header(struct pci_dev *pdev);
 
 /* Assign vPCI to device by adding handlers. */
 int __must_check vpci_assign_device(struct pci_dev *pdev);
xen/include/xen/xen.lds.h +1 -1
index 16a9b1ba03db..c73222112dd3 100644
@@ -187,7 +187,7 @@
 #define VPCI_ARRAY               \
        . = ALIGN(POINTER_ALIGN); \
        __start_vpci_array = .;   \
-       *(SORT(.data.vpci.*))     \
+       *(.data.vpci.*)     \
        __end_vpci_array = .;
 #else
 #define VPCI_ARRAY
Signature · 1 line
2.34.1
#25 lore raw

Re: [PATCH v2 4/8] vpci: Hide capability when it fails to initialize

Quoted 10 lines · Jiqian Chen

When vpci fails to initialize a capability of a device, it just return error instead of catching and processing exception. That makes the entire device unusable.

So, refactor REGISTER_VPCI_INIT to contain more capability specific information, and use new functions to hide capability when initialization fails in vpci_assign_device().

Those new functions remove the failed legacy/extended capability from the emulated legacy/extended capability list.

I think this needs to be at least 2 different changes.

First change adds the usage of REGISTER_VPCI_{LEGACY,EXTENDED}_CAP() helpers, while second change introduces the masking of capabilities on initialization failure.

Otherwise review is a bit complicated.

Quoted 157 lines

What's more, change the definition of init_header() since it is not a capability and it is needed for all devices' PCI config space.

Note: call vpci_make_msix_hole() in the end of init_msix() since the change of sequence of init_header() and init_msix().

---
cc: "Roger Pau Monné" <[email protected]>
cc: Andrew Cooper <[email protected]>
cc: Anthony PERARD <[email protected]>
cc: Michal Orzel <[email protected]>
cc: Jan Beulich <[email protected]>
cc: Julien Grall <[email protected]>
cc: Stefano Stabellini <[email protected]>
---
v1->v2 changes:
* Removed the "priorities" of initializing capabilities since it isn't used anymore.
* Added new function vpci_capability_mask() and vpci_ext_capability_mask() to remove failed capability from list.
* Called vpci_make_msix_hole() in the end of init_msix().

Best regards,
Jiqian Chen.

---
 xen/drivers/vpci/header.c  |   3 +-
 xen/drivers/vpci/msi.c     |   2 +-
 xen/drivers/vpci/msix.c    |   8 +-
 xen/drivers/vpci/rebar.c   |   2 +-
 xen/drivers/vpci/vpci.c    | 175 +++++++++++++++++++++++++++++++------
 xen/include/xen/pci_regs.h |   1 +
 xen/include/xen/vpci.h     |  26 ++++--
 xen/include/xen/xen.lds.h  |   2 +-
 8 files changed, 179 insertions(+), 40 deletions(-)
xen/drivers/vpci/header.c +1 -2
index 6833d456566b..51a67d76ad8a 100644
@@ -848,7 +848,7 @@ static int vpci_init_ext_capability_list(struct pci_dev *pdev)
     return 0;
 }
 
-static int cf_check init_header(struct pci_dev *pdev)
+int vpci_init_header(struct pci_dev *pdev)
 {
     uint16_t cmd;
     uint64_t addr, size;
@@ -1044,7 +1044,6 @@ static int cf_check init_header(struct pci_dev *pdev)
     pci_conf_write16(pdev->sbdf, PCI_COMMAND, cmd);
     return rc;
 }
-REGISTER_VPCI_INIT(init_header, VPCI_PRIORITY_MIDDLE);
 
 /*
  * Local variables:
xen/drivers/vpci/msi.c +1 -1
index 66e5a8a116be..ca89ae9b9c22 100644
@@ -270,7 +270,7 @@ static int cf_check init_msi(struct pci_dev *pdev)
 
     return 0;
 }
-REGISTER_VPCI_INIT(init_msi, VPCI_PRIORITY_LOW);
+REGISTER_VPCI_LEGACY_CAP(PCI_CAP_ID_MSI, init_msi);
 
 void vpci_dump_msi(void)
 {
xen/drivers/vpci/msix.c +6 -2
index 6bd8c55bb48e..6537374c79a0 100644
@@ -751,9 +751,13 @@ static int cf_check init_msix(struct pci_dev *pdev)
     pdev->vpci->msix = msix;
     list_add(&msix->next, &d->arch.hvm.msix_tables);
 
-    return 0;
+    spin_lock(&pdev->vpci->lock);
+    rc = vpci_make_msix_hole(pdev);
+    spin_unlock(&pdev->vpci->lock);
+
+    return rc
 }
-REGISTER_VPCI_INIT(init_msix, VPCI_PRIORITY_HIGH);
+REGISTER_VPCI_LEGACY_CAP(PCI_CAP_ID_MSIX, init_msix);
 
 /*
  * Local variables:
xen/drivers/vpci/rebar.c +1 -1
index 793937449af7..79858e5dc92f 100644
@@ -118,7 +118,7 @@ static int cf_check init_rebar(struct pci_dev *pdev)
 
     return 0;
 }
-REGISTER_VPCI_INIT(init_rebar, VPCI_PRIORITY_LOW);
+REGISTER_VPCI_EXTEND_CAP(PCI_EXT_CAP_ID_REBAR, init_rebar);
 
 /*
  * Local variables:
xen/drivers/vpci/vpci.c +38 -2
index 1e6aa5d799b9..f1f125bfdab1 100644
@@ -35,9 +35,25 @@ struct vpci_register {
     uint32_t rsvdz_mask;
 };
 
+static int vpci_register_cmp(const struct vpci_register *r1,
+                             const struct vpci_register *r2)
+{
+    /* Return 0 if registers overlap. */
+    if ( r1->offset < r2->offset + r2->size &&
+         r2->offset < r1->offset + r1->size )
+        return 0;
+    if ( r1->offset < r2->offset )
+        return -1;
+    if ( r1->offset > r2->offset )
+        return 1;
+
+    ASSERT_UNREACHABLE();
+    return 0;
+}
+
 #ifdef __XEN__
-extern vpci_register_init_t *const __start_vpci_array[];
-extern vpci_register_init_t *const __end_vpci_array[];
+extern vpci_capability_t *const __start_vpci_array[];
+extern vpci_capability_t *const __end_vpci_array[];
 #define NUM_VPCI_INIT (__end_vpci_array - __start_vpci_array)
 
 #ifdef CONFIG_HAS_VPCI_GUEST_SUPPORT
@@ -83,6 +99,133 @@ static int assign_virtual_sbdf(struct pci_dev *pdev)
 
 #endif /* CONFIG_HAS_VPCI_GUEST_SUPPORT */
 
+static void vpci_capability_mask(struct pci_dev *pdev,
+                                 const unsigned int cap)
+{
+    const unsigned int size = 1;
+    const unsigned int offset = pci_find_cap_offset(pdev->sbdf, cap);
+    const struct vpci_register r = { .offset = offset, .size = size };
+    struct vpci_register *rm;
+    struct vpci *vpci = pdev->vpci;
+
+    spin_lock(&vpci->lock);
+    list_for_each_entry ( rm, &vpci->handlers, node )
+    {
+        int cmp = vpci_register_cmp(&r, rm);
+
+        if ( !cmp && rm->offset == offset && rm->size == size )
+        {
+            struct vpci_register *pre = list_entry(rm->node.prev,
+                                                   struct vpci_register,
+                                                   node);
+            struct vpci_register *next = list_entry(rm->node.next,

xen/drivers/vpci/vpci.c static int assign_virtual_sbdf(struct pci_dev *pdev) · line 131

Quoted 10 lines
xen/drivers/vpci/vpci.c +10 -0
+                                                    struct vpci_register,
+                                                    node);
+
+            pre->private = next->private;
+
+            /* PCI_CAP_LIST_ID register of current capability */
+            list_del(&rm->node);
+            /* PCI_CAP_LIST_NEXT register of current capability */
+            list_del(&next->node);
+            spin_unlock(&vpci->lock);

Are you sure this works as intended? The list is sorted, so if there further handlers in between the two capabilities, like when handling MSI capability, the next handler in the list won't point to the next capability list handler.

Quoted 21 lines
xen/drivers/vpci/vpci.c +21 -0
+
+            xfree(rm);
+            xfree(next);
+            return;
+        }
+        if ( cmp <= 0 )
+            break;
+    }
+    spin_unlock(&vpci->lock);
+}
+
+static void vpci_ext_capability_mask(struct pci_dev *pdev,
+                                     const unsigned int cap)
+{
+    const unsigned int size = 4;
+    const unsigned int offset = pci_find_ext_capability(pdev->sbdf, cap);
+    const struct vpci_register r = { .offset = offset, .size = size };
+    struct vpci_register *rm;
+    struct vpci *vpci = pdev->vpci;
+
+    spin_lock(&vpci->lock);

xen/drivers/vpci/vpci.c static int assign_virtual_sbdf(struct pci_dev *pdev) · line 162

Quoted 10 lines
xen/drivers/vpci/vpci.c +10 -0
+    list_for_each_entry ( rm, &vpci->handlers, node )
+    {
+        int cmp = vpci_register_cmp(&r, rm);
+
+        if ( !cmp && rm->offset == offset && rm->size == size )
+        {
+            struct vpci_register *pre;
+            u32 pre_header, header = (u32)(uintptr_t)rm->private;
+
+            if ( offset == 0x100U && PCI_EXT_CAP_NEXT(header) == 0 )

It would be safer to check for next < 0x100 rather than explicitly 0.

xen/drivers/vpci/vpci.c static int assign_virtual_sbdf(struct pci_dev *pdev) · line 168

Quoted 6 lines
xen/drivers/vpci/vpci.c +6 -0
+            {
+                rm->private = (void *)(uintptr_t)0;
+                spin_unlock(&vpci->lock);
+                return;
+            }
+            else if ( offset == 0x100U )

There's no need for the else branch, as the previous if has a return.

xen/drivers/vpci/vpci.c static int assign_virtual_sbdf(struct pci_dev *pdev) · line 179

Quoted 11 lines
xen/drivers/vpci/vpci.c +11 -0
+            {
+                pre = rm;
+                rm = list_entry(rm->node.next, struct vpci_register, node);
+                pre->private = rm->private;
+            }
+            else
+            {
+                pre = list_entry(rm->node.prev, struct vpci_register, node);
+                pre_header = (u32)(uintptr_t)pre->private;
+                pre->private =
+                    (void *)(uintptr_t)((pre_header & !PCI_EXT_CAP_NEXT_MASK) |

I think you want ~PCI_EXT_CAP_NEXT_MASK rather than !PCI_EXT_CAP_NEXT_MASK?

xen/drivers/vpci/vpci.c static int assign_virtual_sbdf(struct pci_dev *pdev) · line 185

Quoted 6 lines
xen/drivers/vpci/vpci.c +6 -0
+                                        (header & PCI_EXT_CAP_NEXT_MASK));
+            }
+            list_del(&rm->node);
+            spin_unlock(&vpci->lock);
+            xfree(rm);
+            return;

Kind of the same complaint I had on the previous patch, this seems to assume that capability handlers are always consecutive in the list of handlers, which I don't think it's the case.

Quoted 57 lines
xen/drivers/vpci/vpci.c +43 -1
+        }
+        if ( cmp <= 0 )
+            break;
+    }
+    spin_unlock(&vpci->lock);
+}
+
+static void vpci_init_capabilities(struct pci_dev *pdev)
+{
+    for ( unsigned int i = 0; i < NUM_VPCI_INIT; i++ )
+    {
+        const vpci_capability_t *capability = __start_vpci_array[i];
+        const unsigned int cap = capability->id;
+        const bool is_ext = capability->is_ext;
+        unsigned int pos;
+        int rc;
+
+        if ( !is_hardware_domain(pdev->domain) && is_ext )
+            continue;
+
+        if ( is_ext )
+            pos = pci_find_ext_capability(pdev->sbdf, cap);
+        else
+            pos = pci_find_cap_offset(pdev->sbdf, cap);
+
+        if ( !pos )
+            continue;
+
+        rc = capability->init(pdev);
+
+        if ( rc )
+        {
+            printk(XENLOG_WARNING "%pd %pp: %s cap %u init fail rc=%d, mask it\n",
+                   pdev->domain, &pdev->sbdf,
+                   is_ext ? "extended" : "legacy", cap, rc);
+            if ( is_ext )
+                vpci_ext_capability_mask(pdev, cap);
+            else
+                vpci_capability_mask(pdev, cap);
+        }
+    }
+}
+
 void vpci_deassign_device(struct pci_dev *pdev)
 {
     unsigned int i;
@@ -128,7 +271,6 @@ void vpci_deassign_device(struct pci_dev *pdev)
 
 int vpci_assign_device(struct pci_dev *pdev)
 {
-    unsigned int i;
     const unsigned long *ro_map;
     int rc = 0;
 
@@ -159,12 +301,11 @@ int vpci_assign_device(struct pci_dev *pdev)
         goto out;
 #endif

xen/drivers/vpci/vpci.c int vpci_assign_device(struct pci_dev *pdev) · line 306

Quoted 10 lines
xen/drivers/vpci/vpci.c +3 -6
 
-    for ( i = 0; i < NUM_VPCI_INIT; i++ )
-    {
-        rc = __start_vpci_array[i](pdev);
-        if ( rc )
-            break;
-    }
+    rc = vpci_init_header(pdev);
+    if ( rc )
+        goto out;

If you use the out label here you can remove the __maybe_unused attribute from it.

Quoted 69 lines
xen/drivers/vpci/vpci.c +2 -16
+
+    vpci_init_capabilities(pdev);
 
  out: __maybe_unused;
     if ( rc )
@@ -174,22 +315,6 @@ int vpci_assign_device(struct pci_dev *pdev)
 }
 #endif /* __XEN__ */
 
-static int vpci_register_cmp(const struct vpci_register *r1,
-                             const struct vpci_register *r2)
-{
-    /* Return 0 if registers overlap. */
-    if ( r1->offset < r2->offset + r2->size &&
-         r2->offset < r1->offset + r1->size )
-        return 0;
-    if ( r1->offset < r2->offset )
-        return -1;
-    if ( r1->offset > r2->offset )
-        return 1;
-
-    ASSERT_UNREACHABLE();
-    return 0;
-}
-
 /* Dummy hooks, writes are ignored, reads return 1's */
 static uint32_t cf_check vpci_ignored_read(
     const struct pci_dev *pdev, unsigned int reg, void *data)
xen/include/xen/pci_regs.h +1 -0
index 27b4f44eedf3..5fe6653fded4 100644
@@ -449,6 +449,7 @@
 #define PCI_EXT_CAP_ID(header)		((header) & 0x0000ffff)
 #define PCI_EXT_CAP_VER(header)		(((header) >> 16) & 0xf)
 #define PCI_EXT_CAP_NEXT(header)	(((header) >> 20) & 0xffc)
+#define PCI_EXT_CAP_NEXT_MASK		0xFFC00000U
 
 #define PCI_EXT_CAP_ID_ERR	1
 #define PCI_EXT_CAP_ID_VC	2
xen/include/xen/vpci.h +6 -8
index 807401b2eaa2..5016ded64d89 100644
@@ -13,11 +13,11 @@ typedef uint32_t vpci_read_t(const struct pci_dev *pdev, unsigned int reg,
 typedef void vpci_write_t(const struct pci_dev *pdev, unsigned int reg,
                           uint32_t val, void *data);
 
-typedef int vpci_register_init_t(struct pci_dev *dev);
-
-#define VPCI_PRIORITY_HIGH      "1"
-#define VPCI_PRIORITY_MIDDLE    "5"
-#define VPCI_PRIORITY_LOW       "9"
+typedef struct {
+    unsigned int id;
+    bool is_ext;
+    int (*init)(struct pci_dev *pdev);
+} vpci_capability_t;
 
 #define VPCI_ECAM_BDF(addr)     (((addr) & 0x0ffff000) >> 12)
 
@@ -29,9 +29,19 @@ typedef int vpci_register_init_t(struct pci_dev *dev);
  */
 #define VPCI_MAX_VIRT_DEV       (PCI_SLOT(~0) + 1)
 
-#define REGISTER_VPCI_INIT(x, p)                \
-  static vpci_register_init_t *const x##_entry  \
-               __used_section(".data.vpci." p) = (x)
+#define REGISTER_VPCI_CAP(cap, x, ext) \

xen/include/xen/vpci.h typedef int vpci_register_init_t(struct pci_dev *dev); · line 42

Quoted 10 lines
xen/include/xen/vpci.h +10 -0
+  static vpci_capability_t x##_t = { \
+        .id = (cap), \
+        .init = (x), \
+        .is_ext = (ext), \
+  }; \
+  static vpci_capability_t *const x##_entry  \
+               __used_section(".data.vpci.") = &(x##_t)
+
+#define REGISTER_VPCI_LEGACY_CAP(cap, x) REGISTER_VPCI_CAP(cap, x, false)
+#define REGISTER_VPCI_EXTEND_CAP(cap, x) REGISTER_VPCI_CAP(cap, x, true)

Nit: I would use EXTENDED here, there's no need to keep both defines the same length.

Thanks, Roger.

#26 lore raw

Re: [PATCH v2 4/8] vpci: Hide capability when it fails to initialize

Quoted 184 lines · Roger Pau Monné
Quoted 10 lines · Jiqian Chen

When vpci fails to initialize a capability of a device, it just return error instead of catching and processing exception. That makes the entire device unusable.

So, refactor REGISTER_VPCI_INIT to contain more capability specific information, and use new functions to hide capability when initialization fails in vpci_assign_device().

Those new functions remove the failed legacy/extended capability from the emulated legacy/extended capability list.

I think this needs to be at least 2 different changes.

First change adds the usage of REGISTER_VPCI_{LEGACY,EXTENDED}_CAP() helpers, while second change introduces the masking of capabilities on initialization failure.

Otherwise review is a bit complicated.

Quoted 164 lines

What's more, change the definition of init_header() since it is not a capability and it is needed for all devices' PCI config space.

Note: call vpci_make_msix_hole() in the end of init_msix() since the change of sequence of init_header() and init_msix().

---
cc: "Roger Pau Monné" <[email protected]>
cc: Andrew Cooper <[email protected]>
cc: Anthony PERARD <[email protected]>
cc: Michal Orzel <[email protected]>
cc: Jan Beulich <[email protected]>
cc: Julien Grall <[email protected]>
cc: Stefano Stabellini <[email protected]>
---
v1->v2 changes:
* Removed the "priorities" of initializing capabilities since it isn't used anymore.
* Added new function vpci_capability_mask() and vpci_ext_capability_mask() to remove failed capability from list.
* Called vpci_make_msix_hole() in the end of init_msix().

Best regards,
Jiqian Chen.

---
 xen/drivers/vpci/header.c  |   3 +-
 xen/drivers/vpci/msi.c     |   2 +-
 xen/drivers/vpci/msix.c    |   8 +-
 xen/drivers/vpci/rebar.c   |   2 +-
 xen/drivers/vpci/vpci.c    | 175 +++++++++++++++++++++++++++++++------
 xen/include/xen/pci_regs.h |   1 +
 xen/include/xen/vpci.h     |  26 ++++--
 xen/include/xen/xen.lds.h  |   2 +-
 8 files changed, 179 insertions(+), 40 deletions(-)
xen/drivers/vpci/header.c +1 -2
index 6833d456566b..51a67d76ad8a 100644
@@ -848,7 +848,7 @@ static int vpci_init_ext_capability_list(struct pci_dev *pdev)
     return 0;
 }
 
-static int cf_check init_header(struct pci_dev *pdev)
+int vpci_init_header(struct pci_dev *pdev)
 {
     uint16_t cmd;
     uint64_t addr, size;
@@ -1044,7 +1044,6 @@ static int cf_check init_header(struct pci_dev *pdev)
     pci_conf_write16(pdev->sbdf, PCI_COMMAND, cmd);
     return rc;
 }
-REGISTER_VPCI_INIT(init_header, VPCI_PRIORITY_MIDDLE);
 
 /*
  * Local variables:
xen/drivers/vpci/msi.c +1 -1
index 66e5a8a116be..ca89ae9b9c22 100644
@@ -270,7 +270,7 @@ static int cf_check init_msi(struct pci_dev *pdev)
 
     return 0;
 }
-REGISTER_VPCI_INIT(init_msi, VPCI_PRIORITY_LOW);
+REGISTER_VPCI_LEGACY_CAP(PCI_CAP_ID_MSI, init_msi);
 
 void vpci_dump_msi(void)
 {
xen/drivers/vpci/msix.c +6 -2
index 6bd8c55bb48e..6537374c79a0 100644
@@ -751,9 +751,13 @@ static int cf_check init_msix(struct pci_dev *pdev)
     pdev->vpci->msix = msix;
     list_add(&msix->next, &d->arch.hvm.msix_tables);
 
-    return 0;
+    spin_lock(&pdev->vpci->lock);
+    rc = vpci_make_msix_hole(pdev);
+    spin_unlock(&pdev->vpci->lock);
+
+    return rc
 }
-REGISTER_VPCI_INIT(init_msix, VPCI_PRIORITY_HIGH);
+REGISTER_VPCI_LEGACY_CAP(PCI_CAP_ID_MSIX, init_msix);
 
 /*
  * Local variables:
xen/drivers/vpci/rebar.c +1 -1
index 793937449af7..79858e5dc92f 100644
@@ -118,7 +118,7 @@ static int cf_check init_rebar(struct pci_dev *pdev)
 
     return 0;
 }
-REGISTER_VPCI_INIT(init_rebar, VPCI_PRIORITY_LOW);
+REGISTER_VPCI_EXTEND_CAP(PCI_EXT_CAP_ID_REBAR, init_rebar);
 
 /*
  * Local variables:
xen/drivers/vpci/vpci.c +45 -2
index 1e6aa5d799b9..f1f125bfdab1 100644
@@ -35,9 +35,25 @@ struct vpci_register {
     uint32_t rsvdz_mask;
 };
 
+static int vpci_register_cmp(const struct vpci_register *r1,
+                             const struct vpci_register *r2)
+{
+    /* Return 0 if registers overlap. */
+    if ( r1->offset < r2->offset + r2->size &&
+         r2->offset < r1->offset + r1->size )
+        return 0;
+    if ( r1->offset < r2->offset )
+        return -1;
+    if ( r1->offset > r2->offset )
+        return 1;
+
+    ASSERT_UNREACHABLE();
+    return 0;
+}
+
 #ifdef __XEN__
-extern vpci_register_init_t *const __start_vpci_array[];
-extern vpci_register_init_t *const __end_vpci_array[];
+extern vpci_capability_t *const __start_vpci_array[];
+extern vpci_capability_t *const __end_vpci_array[];
 #define NUM_VPCI_INIT (__end_vpci_array - __start_vpci_array)
 
 #ifdef CONFIG_HAS_VPCI_GUEST_SUPPORT
@@ -83,6 +99,133 @@ static int assign_virtual_sbdf(struct pci_dev *pdev)
 
 #endif /* CONFIG_HAS_VPCI_GUEST_SUPPORT */
 
+static void vpci_capability_mask(struct pci_dev *pdev,
+                                 const unsigned int cap)
+{
+    const unsigned int size = 1;
+    const unsigned int offset = pci_find_cap_offset(pdev->sbdf, cap);
+    const struct vpci_register r = { .offset = offset, .size = size };
+    struct vpci_register *rm;
+    struct vpci *vpci = pdev->vpci;
+
+    spin_lock(&vpci->lock);
+    list_for_each_entry ( rm, &vpci->handlers, node )
+    {
+        int cmp = vpci_register_cmp(&r, rm);
+
+        if ( !cmp && rm->offset == offset && rm->size == size )
+        {
+            struct vpci_register *pre = list_entry(rm->node.prev,
+                                                   struct vpci_register,
+                                                   node);
+            struct vpci_register *next = list_entry(rm->node.next,
+                                                    struct vpci_register,
+                                                    node);
+
+            pre->private = next->private;
+
+            /* PCI_CAP_LIST_ID register of current capability */
+            list_del(&rm->node);
Quoted 8 lines

xen/drivers/vpci/vpci.c static int assign_virtual_sbdf(struct pci_dev *pdev) · line 131

Quoted 3 lines
xen/drivers/vpci/vpci.c +3 -0
+            /* PCI_CAP_LIST_NEXT register of current capability */
+            list_del(&next->node);
+            spin_unlock(&vpci->lock);

Are you sure this works as intended? The list is sorted, so if there further handlers in between the two capabilities, like when handling MSI capability, the next handler in the list won't point to the next capability list handler.

Oh, I thought the capability list is also sorted in the hardware.
Since it is not that case. My method would not work as intended.
I will change to get the capability position and the previous capability position from the hardware, and then according to the two position to get handlers from vpci handlers.

And I will change my patch according to your other comments in this email. Thank you for detail review.

Quoted 225 lines
Quoted 22 lines
xen/drivers/vpci/vpci.c +21 -0
+
+            xfree(rm);
+            xfree(next);
+            return;
+        }
+        if ( cmp <= 0 )
+            break;
+    }
+    spin_unlock(&vpci->lock);
+}
+
+static void vpci_ext_capability_mask(struct pci_dev *pdev,
+                                     const unsigned int cap)
+{
+    const unsigned int size = 4;
+    const unsigned int offset = pci_find_ext_capability(pdev->sbdf, cap);
+    const struct vpci_register r = { .offset = offset, .size = size };
+    struct vpci_register *rm;
+    struct vpci *vpci = pdev->vpci;
+
+    spin_lock(&vpci->lock);

xen/drivers/vpci/vpci.c static int assign_virtual_sbdf(struct pci_dev *pdev) · line 162

Quoted 10 lines
xen/drivers/vpci/vpci.c +10 -0
+    list_for_each_entry ( rm, &vpci->handlers, node )
+    {
+        int cmp = vpci_register_cmp(&r, rm);
+
+        if ( !cmp && rm->offset == offset && rm->size == size )
+        {
+            struct vpci_register *pre;
+            u32 pre_header, header = (u32)(uintptr_t)rm->private;
+
+            if ( offset == 0x100U && PCI_EXT_CAP_NEXT(header) == 0 )

It would be safer to check for next < 0x100 rather than explicitly 0.

xen/drivers/vpci/vpci.c static int assign_virtual_sbdf(struct pci_dev *pdev) · line 168

Quoted 6 lines
xen/drivers/vpci/vpci.c +6 -0
+            {
+                rm->private = (void *)(uintptr_t)0;
+                spin_unlock(&vpci->lock);
+                return;
+            }
+            else if ( offset == 0x100U )

There's no need for the else branch, as the previous if has a return.

xen/drivers/vpci/vpci.c static int assign_virtual_sbdf(struct pci_dev *pdev) · line 179

Quoted 11 lines
xen/drivers/vpci/vpci.c +11 -0
+            {
+                pre = rm;
+                rm = list_entry(rm->node.next, struct vpci_register, node);
+                pre->private = rm->private;
+            }
+            else
+            {
+                pre = list_entry(rm->node.prev, struct vpci_register, node);
+                pre_header = (u32)(uintptr_t)pre->private;
+                pre->private =
+                    (void *)(uintptr_t)((pre_header & !PCI_EXT_CAP_NEXT_MASK) |

I think you want ~PCI_EXT_CAP_NEXT_MASK rather than !PCI_EXT_CAP_NEXT_MASK?

xen/drivers/vpci/vpci.c static int assign_virtual_sbdf(struct pci_dev *pdev) · line 185

Quoted 6 lines
xen/drivers/vpci/vpci.c +6 -0
+                                        (header & PCI_EXT_CAP_NEXT_MASK));
+            }
+            list_del(&rm->node);
+            spin_unlock(&vpci->lock);
+            xfree(rm);
+            return;

Kind of the same complaint I had on the previous patch, this seems to assume that capability handlers are always consecutive in the list of handlers, which I don't think it's the case.

Quoted 57 lines
xen/drivers/vpci/vpci.c +43 -1
+        }
+        if ( cmp <= 0 )
+            break;
+    }
+    spin_unlock(&vpci->lock);
+}
+
+static void vpci_init_capabilities(struct pci_dev *pdev)
+{
+    for ( unsigned int i = 0; i < NUM_VPCI_INIT; i++ )
+    {
+        const vpci_capability_t *capability = __start_vpci_array[i];
+        const unsigned int cap = capability->id;
+        const bool is_ext = capability->is_ext;
+        unsigned int pos;
+        int rc;
+
+        if ( !is_hardware_domain(pdev->domain) && is_ext )
+            continue;
+
+        if ( is_ext )
+            pos = pci_find_ext_capability(pdev->sbdf, cap);
+        else
+            pos = pci_find_cap_offset(pdev->sbdf, cap);
+
+        if ( !pos )
+            continue;
+
+        rc = capability->init(pdev);
+
+        if ( rc )
+        {
+            printk(XENLOG_WARNING "%pd %pp: %s cap %u init fail rc=%d, mask it\n",
+                   pdev->domain, &pdev->sbdf,
+                   is_ext ? "extended" : "legacy", cap, rc);
+            if ( is_ext )
+                vpci_ext_capability_mask(pdev, cap);
+            else
+                vpci_capability_mask(pdev, cap);
+        }
+    }
+}
+
 void vpci_deassign_device(struct pci_dev *pdev)
 {
     unsigned int i;
@@ -128,7 +271,6 @@ void vpci_deassign_device(struct pci_dev *pdev)
 
 int vpci_assign_device(struct pci_dev *pdev)
 {
-    unsigned int i;
     const unsigned long *ro_map;
     int rc = 0;
 
@@ -159,12 +301,11 @@ int vpci_assign_device(struct pci_dev *pdev)
         goto out;
 #endif

xen/drivers/vpci/vpci.c int vpci_assign_device(struct pci_dev *pdev) · line 306

Quoted 10 lines
xen/drivers/vpci/vpci.c +3 -6
 
-    for ( i = 0; i < NUM_VPCI_INIT; i++ )
-    {
-        rc = __start_vpci_array[i](pdev);
-        if ( rc )
-            break;
-    }
+    rc = vpci_init_header(pdev);
+    if ( rc )
+        goto out;

If you use the out label here you can remove the __maybe_unused attribute from it.

Quoted 69 lines
xen/drivers/vpci/vpci.c +2 -16
+
+    vpci_init_capabilities(pdev);
 
  out: __maybe_unused;
     if ( rc )
@@ -174,22 +315,6 @@ int vpci_assign_device(struct pci_dev *pdev)
 }
 #endif /* __XEN__ */
 
-static int vpci_register_cmp(const struct vpci_register *r1,
-                             const struct vpci_register *r2)
-{
-    /* Return 0 if registers overlap. */
-    if ( r1->offset < r2->offset + r2->size &&
-         r2->offset < r1->offset + r1->size )
-        return 0;
-    if ( r1->offset < r2->offset )
-        return -1;
-    if ( r1->offset > r2->offset )
-        return 1;
-
-    ASSERT_UNREACHABLE();
-    return 0;
-}
-
 /* Dummy hooks, writes are ignored, reads return 1's */
 static uint32_t cf_check vpci_ignored_read(
     const struct pci_dev *pdev, unsigned int reg, void *data)
xen/include/xen/pci_regs.h +1 -0
index 27b4f44eedf3..5fe6653fded4 100644
@@ -449,6 +449,7 @@
 #define PCI_EXT_CAP_ID(header)		((header) & 0x0000ffff)
 #define PCI_EXT_CAP_VER(header)		(((header) >> 16) & 0xf)
 #define PCI_EXT_CAP_NEXT(header)	(((header) >> 20) & 0xffc)
+#define PCI_EXT_CAP_NEXT_MASK		0xFFC00000U
 
 #define PCI_EXT_CAP_ID_ERR	1
 #define PCI_EXT_CAP_ID_VC	2
xen/include/xen/vpci.h +6 -8
index 807401b2eaa2..5016ded64d89 100644
@@ -13,11 +13,11 @@ typedef uint32_t vpci_read_t(const struct pci_dev *pdev, unsigned int reg,
 typedef void vpci_write_t(const struct pci_dev *pdev, unsigned int reg,
                           uint32_t val, void *data);
 
-typedef int vpci_register_init_t(struct pci_dev *dev);
-
-#define VPCI_PRIORITY_HIGH      "1"
-#define VPCI_PRIORITY_MIDDLE    "5"
-#define VPCI_PRIORITY_LOW       "9"
+typedef struct {
+    unsigned int id;
+    bool is_ext;
+    int (*init)(struct pci_dev *pdev);
+} vpci_capability_t;
 
 #define VPCI_ECAM_BDF(addr)     (((addr) & 0x0ffff000) >> 12)
 
@@ -29,9 +29,19 @@ typedef int vpci_register_init_t(struct pci_dev *dev);
  */
 #define VPCI_MAX_VIRT_DEV       (PCI_SLOT(~0) + 1)
 
-#define REGISTER_VPCI_INIT(x, p)                \
-  static vpci_register_init_t *const x##_entry  \
-               __used_section(".data.vpci." p) = (x)
+#define REGISTER_VPCI_CAP(cap, x, ext) \

xen/include/xen/vpci.h typedef int vpci_register_init_t(struct pci_dev *dev); · line 42

Quoted 10 lines
xen/include/xen/vpci.h +10 -0
+  static vpci_capability_t x##_t = { \
+        .id = (cap), \
+        .init = (x), \
+        .is_ext = (ext), \
+  }; \
+  static vpci_capability_t *const x##_entry  \
+               __used_section(".data.vpci.") = &(x##_t)
+
+#define REGISTER_VPCI_LEGACY_CAP(cap, x) REGISTER_VPCI_CAP(cap, x, false)
+#define REGISTER_VPCI_EXTEND_CAP(cap, x) REGISTER_VPCI_CAP(cap, x, true)

Nit: I would use EXTENDED here, there's no need to keep both defines the same length.

Thanks, Roger.

Signature · 2 lines
Best regards,
Jiqian Chen.
#27 lore raw

PATCH v2 5/8 vpci: Refactor vpci_remove_register to remove matched registers

In reply to Jiqian Chen

vpci_remove_register() only supports removing a register in a time, but the follow-on changes need to remove all registers within a range. And it is only used for test. So, refactor it to support removing all matched registers in a calling time.

And it is no matter to remove a non exist register, so remove the __must_check prefix.

---
cc: "Roger Pau Monné" <[email protected]>
cc: Anthony PERARD <[email protected]>
---
v1->v2 changes:
new patch

Best regards,
Jiqian Chen.

---
 tools/tests/vpci/main.c |  4 ++--
 xen/drivers/vpci/vpci.c | 23 ++++++++++++-----------
 xen/include/xen/vpci.h  |  4 ++--
 3 files changed, 16 insertions(+), 15 deletions(-)
tools/tests/vpci/main.c +2 -2
index 33223db3eb77..ca72877d60cd 100644
@@ -132,10 +132,10 @@ static void vpci_write32_mask(const struct pci_dev *pdev, unsigned int reg,
                                   rsvdz_mask))
 
 #define VPCI_REMOVE_REG(off, size)                                          \
-    assert(!vpci_remove_register(test_pdev.vpci, off, size))
+    assert(!vpci_remove_registers(test_pdev.vpci, off, size))
 
 #define VPCI_REMOVE_INVALID_REG(off, size)                                  \
-    assert(vpci_remove_register(test_pdev.vpci, off, size))
+    assert(vpci_remove_registers(test_pdev.vpci, off, size))
 
 /* Read a 32b register using all possible sizes. */
 void multiread4_check(unsigned int reg, uint32_t val)
xen/drivers/vpci/vpci.c +12 -11
index f1f125bfdab1..115d3c5f0c84 100644
@@ -418,34 +418,35 @@ int vpci_add_register_mask(struct vpci *vpci, vpci_read_t *read_handler,
     return 0;
 }
 
-int vpci_remove_register(struct vpci *vpci, unsigned int offset,
-                         unsigned int size)
+int vpci_remove_registers(struct vpci *vpci, unsigned int offset,
+                          unsigned int size)
 {
     const struct vpci_register r = { .offset = offset, .size = size };
     struct vpci_register *rm;
+    int rc = -ENOENT;
 
     spin_lock(&vpci->lock);
     list_for_each_entry ( rm, &vpci->handlers, node )
     {
         int cmp = vpci_register_cmp(&r, rm);
 
-        /*
-         * NB: do not use a switch so that we can use break to
-         * get out of the list loop earlier if required.
-         */
-        if ( !cmp && rm->offset == offset && rm->size == size )
+        if ( !cmp )
         {
+            struct vpci_register *prev =
+                list_entry(rm->node.prev, struct vpci_register, node);
+
             list_del(&rm->node);
-            spin_unlock(&vpci->lock);
             xfree(rm);
-            return 0;
+            rm = prev;
+            rc = 0;
         }
-        if ( cmp <= 0 )
+
+        if ( cmp < 0 )
             break;
     }
     spin_unlock(&vpci->lock);
 
-    return -ENOENT;
+    return rc;
 }
 
 /* Wrappers for performing reads/writes to the underlying hardware. */
xen/include/xen/vpci.h +2 -2
index 5016ded64d89..ee9163ca6a56 100644
@@ -67,8 +67,8 @@ static inline int __must_check vpci_add_register(struct vpci *vpci,
                                   size, data, 0, 0, 0, 0);
 }
 
-int __must_check vpci_remove_register(struct vpci *vpci, unsigned int offset,
-                                      unsigned int size);
+int vpci_remove_registers(struct vpci *vpci, unsigned int offset,
+                          unsigned int size);
 
 /* Generic read/write handlers for the PCI config space. */
 uint32_t vpci_read(pci_sbdf_t sbdf, unsigned int reg, unsigned int size);
Signature · 1 line
2.34.1
#28 lore raw

Re: [PATCH v2 5/8] vpci: Refactor vpci_remove_register to remove matched registers

Quoted 47 lines · Jiqian Chen

vpci_remove_register() only supports removing a register in a time, but the follow-on changes need to remove all registers within a range. And it is only used for test. So, refactor it to support removing all matched registers in a calling time.

And it is no matter to remove a non exist register, so remove the __must_check prefix.

---
cc: "Roger Pau Monné" <[email protected]>
cc: Anthony PERARD <[email protected]>
---
v1->v2 changes:
new patch

Best regards,
Jiqian Chen.

---
 tools/tests/vpci/main.c |  4 ++--
 xen/drivers/vpci/vpci.c | 23 ++++++++++++-----------
 xen/include/xen/vpci.h  |  4 ++--
 3 files changed, 16 insertions(+), 15 deletions(-)
tools/tests/vpci/main.c +2 -2
index 33223db3eb77..ca72877d60cd 100644
@@ -132,10 +132,10 @@ static void vpci_write32_mask(const struct pci_dev *pdev, unsigned int reg,
                                   rsvdz_mask))
 
 #define VPCI_REMOVE_REG(off, size)                                          \
-    assert(!vpci_remove_register(test_pdev.vpci, off, size))
+    assert(!vpci_remove_registers(test_pdev.vpci, off, size))
 
 #define VPCI_REMOVE_INVALID_REG(off, size)                                  \
-    assert(vpci_remove_register(test_pdev.vpci, off, size))
+    assert(vpci_remove_registers(test_pdev.vpci, off, size))
 
 /* Read a 32b register using all possible sizes. */
 void multiread4_check(unsigned int reg, uint32_t val)
xen/drivers/vpci/vpci.c +0 -0
index f1f125bfdab1..115d3c5f0c84 100644
@@ -418,34 +418,35 @@ int vpci_add_register_mask(struct vpci *vpci, vpci_read_t *read_handler,
     return 0;

xen/drivers/vpci/vpci.c int vpci_add_register_mask(struct vpci *vpci, vpci_read_t *read_handler, · line 426

Quoted 10 lines
xen/drivers/vpci/vpci.c +3 -2
 }
 
-int vpci_remove_register(struct vpci *vpci, unsigned int offset,
-                         unsigned int size)
+int vpci_remove_registers(struct vpci *vpci, unsigned int offset,
+                          unsigned int size)
 {
     const struct vpci_register r = { .offset = offset, .size = size };
     struct vpci_register *rm;
+    int rc = -ENOENT;

Thinking about this, not sure returning ENOENT makes much sense now, as the (new) purpose of the function is to zap all handlers from a range, without possibly prior knowledge whether there are any handlers in the range.

Quoted 12 lines
xen/drivers/vpci/vpci.c +1 -5
     spin_lock(&vpci->lock);
     list_for_each_entry ( rm, &vpci->handlers, node )
     {
         int cmp = vpci_register_cmp(&r, rm);
 
-        /*
-         * NB: do not use a switch so that we can use break to
-         * get out of the list loop earlier if required.
-         */
-        if ( !cmp && rm->offset == offset && rm->size == size )
+        if ( !cmp )

xen/drivers/vpci/vpci.c int vpci_add_register_mask(struct vpci *vpci, vpci_read_t *read_handler, · line 441

Quoted 10 lines
xen/drivers/vpci/vpci.c +5 -2
         {
+            struct vpci_register *prev =
+                list_entry(rm->node.prev, struct vpci_register, node);
+
             list_del(&rm->node);
-            spin_unlock(&vpci->lock);
             xfree(rm);
-            return 0;
+            rm = prev;
+            rc = 0;

I think you want some extra checks here to ensure you are not removing partially overlapping registers. IOW, you need to assert the register in rm is fully contained inside the range to zap.

It might be best to simply not use vpci_register_cmp(), and instead open code the comparison here to ensure "rm" is fuly contained inside the [offset, offset + size - 1] range. You will need to return an error if there's a partially overlapping register.

Thanks, Roger.

#29 lore raw

PATCH v2 6/8 vpci/rebar: Remove registers when init_rebar() fails

In reply to Jiqian Chen

When init_rebar() fails, the previous new changes will hide Rebar capability, it can't rely on vpci_deassign_device() to remove all Rebar related registers anymore, those registers must be removed in failure path of init_rebar.

To do that, call vpci_remove_registers() to remove all possible registered registers in the failure patch.

---
cc: "Roger Pau Monné" <[email protected]>
---
v1->v2 changes:
* Called vpci_remove_registers() to remove all possible registered registers instead of using a array to record all registered register.

Best regards,
Jiqian Chen.

---
 xen/drivers/vpci/rebar.c | 25 ++++++++++++++-----------
 1 file changed, 14 insertions(+), 11 deletions(-)
xen/drivers/vpci/rebar.c +14 -11
index 79858e5dc92f..e030937956e3 100644
@@ -51,6 +51,7 @@ static void cf_check rebar_ctrl_write(const struct pci_dev *pdev,
 
 static int cf_check init_rebar(struct pci_dev *pdev)
 {
+    int rc = 0;
     uint32_t ctrl;
     unsigned int nbars;
     unsigned int rebar_offset = pci_find_ext_capability(pdev->sbdf,
@@ -70,7 +71,6 @@ static int cf_check init_rebar(struct pci_dev *pdev)
     nbars = MASK_EXTR(ctrl, PCI_REBAR_CTRL_NBAR_MASK);
     for ( unsigned int i = 0; i < nbars; i++ )
     {
-        int rc;
         struct vpci_bar *bar;
         unsigned int index;
 
@@ -80,7 +80,7 @@ static int cf_check init_rebar(struct pci_dev *pdev)
         {
             printk(XENLOG_ERR "%pd %pp: too big BAR number %u in REBAR_CTRL\n",
                    pdev->domain, &pdev->sbdf, index);
-            continue;
+            goto fail;
         }
 
         bar = &pdev->vpci->header.bars[index];
@@ -88,7 +88,7 @@ static int cf_check init_rebar(struct pci_dev *pdev)
         {
             printk(XENLOG_ERR "%pd %pp: BAR%u is not in memory space\n",
                    pdev->domain, &pdev->sbdf, index);
-            continue;
+            goto fail;
         }
 
         rc = vpci_add_register(pdev->vpci, vpci_hw_read32, rebar_ctrl_write,
@@ -97,14 +97,7 @@ static int cf_check init_rebar(struct pci_dev *pdev)
         {
             printk(XENLOG_ERR "%pd %pp: BAR%u fail to add reg of REBAR_CTRL rc=%d\n",
                    pdev->domain, &pdev->sbdf, index, rc);
-            /*
-             * Ideally we would hide the ReBar capability on error, but code
-             * for doing so still needs to be written. Use continue instead
-             * to keep any already setup register hooks, as returning an
-             * error will cause the hardware domain to get unmediated access
-             * to all device registers.
-             */
-            continue;
+            goto fail;
         }
 
         bar->resizable_sizes =
@@ -117,6 +110,16 @@ static int cf_check init_rebar(struct pci_dev *pdev)
     }
 
     return 0;
+
+ fail:
+    /*
+     * Remove all possible registered registers except header.
+     * Header register will be removed in mask function.
+     */
+    vpci_remove_registers(pdev->vpci, rebar_offset + PCI_REBAR_CAP(0),
+                          PCI_REBAR_CTRL(nbars - 1));
+
+    return rc;
 }
 REGISTER_VPCI_EXTEND_CAP(PCI_EXT_CAP_ID_REBAR, init_rebar);
 
Signature · 1 line
2.34.1
#30 lore raw

Re: [PATCH v2 6/8] vpci/rebar: Remove registers when init_rebar() fails

Quoted 51 lines · Jiqian Chen

When init_rebar() fails, the previous new changes will hide Rebar capability, it can't rely on vpci_deassign_device() to remove all Rebar related registers anymore, those registers must be removed in failure path of init_rebar.

To do that, call vpci_remove_registers() to remove all possible registered registers in the failure patch.

---
cc: "Roger Pau Monné" <[email protected]>
---
v1->v2 changes:
* Called vpci_remove_registers() to remove all possible registered registers instead of using a array to record all registered register.

Best regards,
Jiqian Chen.

---
 xen/drivers/vpci/rebar.c | 25 ++++++++++++++-----------
 1 file changed, 14 insertions(+), 11 deletions(-)
xen/drivers/vpci/rebar.c +2 -2
index 79858e5dc92f..e030937956e3 100644
@@ -51,6 +51,7 @@ static void cf_check rebar_ctrl_write(const struct pci_dev *pdev,
 
 static int cf_check init_rebar(struct pci_dev *pdev)
 {
+    int rc = 0;
     uint32_t ctrl;
     unsigned int nbars;
     unsigned int rebar_offset = pci_find_ext_capability(pdev->sbdf,
@@ -70,7 +71,6 @@ static int cf_check init_rebar(struct pci_dev *pdev)
     nbars = MASK_EXTR(ctrl, PCI_REBAR_CTRL_NBAR_MASK);
     for ( unsigned int i = 0; i < nbars; i++ )
     {
-        int rc;
         struct vpci_bar *bar;
         unsigned int index;
 
@@ -80,7 +80,7 @@ static int cf_check init_rebar(struct pci_dev *pdev)
         {
             printk(XENLOG_ERR "%pd %pp: too big BAR number %u in REBAR_CTRL\n",
                    pdev->domain, &pdev->sbdf, index);
-            continue;
+            goto fail;
         }
 
         bar = &pdev->vpci->header.bars[index];

xen/drivers/vpci/rebar.c static int cf_check init_rebar(struct pci_dev *pdev) · line 91

Quoted 5 lines
xen/drivers/vpci/rebar.c +1 -1
@@ -88,7 +88,7 @@ static int cf_check init_rebar(struct pci_dev *pdev)
         {
             printk(XENLOG_ERR "%pd %pp: BAR%u is not in memory space\n",
                    pdev->domain, &pdev->sbdf, index);
-            continue;
+            goto fail;

I think you need to set rc to an error code for the two chunks above, otherwise you jump into the fail label with rc == 0 AFAICT.

Thanks, Roger.

#31 lore raw

PATCH v2 7/8 vpci/msi: Free MSI resources when init_msi() fails

In reply to Jiqian Chen

When init_msi() fails, the previous new changes will hide MSI capability, it can't rely on vpci_deassign_device() to remove all MSI related resources anymore, those resources must be cleaned up in failure path of init_msi.

To do that, add a new function to free MSI resources.

---
cc: "Roger Pau Monné" <[email protected]>
---
v1->v2 changes:
* Added a new function fini_msi to free all MSI resources instead of using an array to record registered registers.

---
 xen/drivers/vpci/msi.c | 47 +++++++++++++++++++++++++++++++++---------
 1 file changed, 37 insertions(+), 10 deletions(-)
xen/drivers/vpci/msi.c +37 -10
index ca89ae9b9c22..48a466dba0ef 100644
@@ -193,6 +193,33 @@ static void cf_check mask_write(
     msi->mask = val;
 }
 
+/* 12 is size of MSI structure for 32-bit Message Address without PVM */
+#define MSI_STRUCTURE_SIZE_32 12
+
+static void fini_msi(struct pci_dev *pdev)
+{
+    unsigned int size = MSI_STRUCTURE_SIZE_32;
+
+    if ( !pdev->vpci->msi )
+        return;
+
+    if ( pdev->vpci->msi->address64 )
+        size += 4;
+    if ( pdev->vpci->msi->masking )
+        size += 4;
+
+    /*
+     * Remove all possible registered registers except capability ID
+     * register and next capability pointer register, which will be
+     * removed in mask function.
+     */
+    vpci_remove_registers(pdev->vpci,
+                          msi_control_reg(pdev->msi_pos),
+                          size - PCI_MSI_FLAGS);
+    xfree(pdev->vpci->msi);
+    pdev->vpci->msi = NULL;
+}
+
 static int cf_check init_msi(struct pci_dev *pdev)
 {
     unsigned int pos = pdev->msi_pos;
@@ -209,12 +236,7 @@ static int cf_check init_msi(struct pci_dev *pdev)
     ret = vpci_add_register(pdev->vpci, control_read, control_write,
                             msi_control_reg(pos), 2, pdev->vpci->msi);
     if ( ret )
-        /*
-         * NB: there's no need to free the msi struct or remove the register
-         * handlers form the config space, the caller will take care of the
-         * cleanup.
-         */
-        return ret;
+        goto fail;
 
     /* Get the maximum number of vectors the device supports. */
     control = pci_conf_read16(pdev->sbdf, msi_control_reg(pos));
@@ -237,20 +259,20 @@ static int cf_check init_msi(struct pci_dev *pdev)
     ret = vpci_add_register(pdev->vpci, address_read, address_write,
                             msi_lower_address_reg(pos), 4, pdev->vpci->msi);
     if ( ret )
-        return ret;
+        goto fail;
 
     ret = vpci_add_register(pdev->vpci, data_read, data_write,
                             msi_data_reg(pos, pdev->vpci->msi->address64), 2,
                             pdev->vpci->msi);
     if ( ret )
-        return ret;
+        goto fail;
 
     if ( pdev->vpci->msi->address64 )
     {
         ret = vpci_add_register(pdev->vpci, address_hi_read, address_hi_write,
                                 msi_upper_address_reg(pos), 4, pdev->vpci->msi);
         if ( ret )
-            return ret;
+            goto fail;
     }
 
     if ( pdev->vpci->msi->masking )
@@ -260,7 +282,7 @@ static int cf_check init_msi(struct pci_dev *pdev)
                                                   pdev->vpci->msi->address64),
                                 4, pdev->vpci->msi);
         if ( ret )
-            return ret;
+            goto fail;
         /*
          * FIXME: do not add any handler for the pending bits for the hardware
          * domain, which means direct access. This will be revisited when
@@ -269,6 +291,11 @@ static int cf_check init_msi(struct pci_dev *pdev)
     }
 
     return 0;
+
+ fail:
+    fini_msi(pdev);
+
+    return ret;
 }
 REGISTER_VPCI_LEGACY_CAP(PCI_CAP_ID_MSI, init_msi);
 
Signature · 1 line
2.34.1
#32 lore raw

Re: [PATCH v2 7/8] vpci/msi: Free MSI resources when init_msi() fails

Quoted 22 lines · Jiqian Chen

When init_msi() fails, the previous new changes will hide MSI capability, it can't rely on vpci_deassign_device() to remove all MSI related resources anymore, those resources must be cleaned up in failure path of init_msi.

To do that, add a new function to free MSI resources.

---
cc: "Roger Pau Monné" <[email protected]>
---
v1->v2 changes:
* Added a new function fini_msi to free all MSI resources instead of using an array to record registered registers.

---
 xen/drivers/vpci/msi.c | 47 +++++++++++++++++++++++++++++++++---------
 1 file changed, 37 insertions(+), 10 deletions(-)
xen/drivers/vpci/msi.c +0 -0
index ca89ae9b9c22..48a466dba0ef 100644

xen/drivers/vpci/msi.c static void cf_check mask_write( · line 197

Quoted 5 lines
xen/drivers/vpci/msi.c +2 -0
@@ -193,6 +193,33 @@ static void cf_check mask_write(
     msi->mask = val;
 }
 
+/* 12 is size of MSI structure for 32-bit Message Address without PVM */
+#define MSI_STRUCTURE_SIZE_32 12

I'm confused by this. The minimum size of the capability is 4 bytes for the capability pointer, plus 4 bytes for the message address, plus 2 bytes for the message data. So that's 10 bytes in total.

I think it would be best if you calculate the size based on the existing msi_ macros.

if ( masking )

    end = msi_pending_bits_reg(msi_pos, address64);
else
    end = msi_mask_bits_reg(msi_pos, address64) - 2;

size = end - msi_control_reg(msi_pos);

Quoted 76 lines
xen/drivers/vpci/msi.c +30 -10
+
+static void fini_msi(struct pci_dev *pdev)
+{
+    unsigned int size = MSI_STRUCTURE_SIZE_32;
+
+    if ( !pdev->vpci->msi )
+        return;
+
+    if ( pdev->vpci->msi->address64 )
+        size += 4;
+    if ( pdev->vpci->msi->masking )
+        size += 4;
+
+    /*
+     * Remove all possible registered registers except capability ID
+     * register and next capability pointer register, which will be
+     * removed in mask function.
+     *msi_mask_bits_reg/
+    vpci_remove_registers(pdev->vpci,
+                          msi_control_reg(pdev->msi_pos),
+                          size - PCI_MSI_FLAGS);
+    xfree(pdev->vpci->msi);
+    pdev->vpci->msi = NULL;
+}
+
 static int cf_check init_msi(struct pci_dev *pdev)
 {
     unsigned int pos = pdev->msi_pos;
@@ -209,12 +236,7 @@ static int cf_check init_msi(struct pci_dev *pdev)
     ret = vpci_add_register(pdev->vpci, control_read, control_write,
                             msi_control_reg(pos), 2, pdev->vpci->msi);
     if ( ret )
-        /*
-         * NB: there's no need to free the msi struct or remove the register
-         * handlers form the config space, the caller will take care of the
-         * cleanup.
-         */
-        return ret;
+        goto fail;
 
     /* Get the maximum number of vectors the device supports. */
     control = pci_conf_read16(pdev->sbdf, msi_control_reg(pos));
@@ -237,20 +259,20 @@ static int cf_check init_msi(struct pci_dev *pdev)
     ret = vpci_add_register(pdev->vpci, address_read, address_write,
                             msi_lower_address_reg(pos), 4, pdev->vpci->msi);
     if ( ret )
-        return ret;
+        goto fail;
 
     ret = vpci_add_register(pdev->vpci, data_read, data_write,
                             msi_data_reg(pos, pdev->vpci->msi->address64), 2,
                             pdev->vpci->msi);
     if ( ret )
-        return ret;
+        goto fail;
 
     if ( pdev->vpci->msi->address64 )
     {
         ret = vpci_add_register(pdev->vpci, address_hi_read, address_hi_write,
                                 msi_upper_address_reg(pos), 4, pdev->vpci->msi);
         if ( ret )
-            return ret;
+            goto fail;
     }
 
     if ( pdev->vpci->msi->masking )
@@ -260,7 +282,7 @@ static int cf_check init_msi(struct pci_dev *pdev)
                                                   pdev->vpci->msi->address64),
                                 4, pdev->vpci->msi);
         if ( ret )
-            return ret;
+            goto fail;
         /*
          * FIXME: do not add any handler for the pending bits for the hardware
          * domain, which means direct access. This will be revisited when

xen/drivers/vpci/msi.c static int cf_check init_msi(struct pci_dev *pdev) · line 300

Quoted 10 lines
xen/drivers/vpci/msi.c +5 -0
@@ -269,6 +291,11 @@ static int cf_check init_msi(struct pci_dev *pdev)
     }
 
     return 0;
+
+ fail:
+    fini_msi(pdev);
+
+    return ret;
 }
 REGISTER_VPCI_LEGACY_CAP(PCI_CAP_ID_MSI, init_msi);

I wonder if the fini_msi should be referenced in REGISTER_VPCI_{EXTENDED,LEGACY}_CAP(), so that the caller of init_msi() can call fini_msi() on failure and thus avoid all those fail hooks and labels, as the caller can take care of the cleanup.

Thanks, Roger.

#33 lore raw

Re: [PATCH v2 7/8] vpci/msi: Free MSI resources when init_msi() fails

Quoted 26 lines · Roger Pau Monné
Quoted 25 lines · Jiqian Chen

When init_msi() fails, the previous new changes will hide MSI capability, it can't rely on vpci_deassign_device() to remove all MSI related resources anymore, those resources must be cleaned up in failure path of init_msi.

To do that, add a new function to free MSI resources.

---
cc: "Roger Pau Monné" <[email protected]>
---
v1->v2 changes:
* Added a new function fini_msi to free all MSI resources instead of using an array to record registered registers.

---
 xen/drivers/vpci/msi.c | 47 +++++++++++++++++++++++++++++++++---------
 1 file changed, 37 insertions(+), 10 deletions(-)
xen/drivers/vpci/msi.c +0 -0
index ca89ae9b9c22..48a466dba0ef 100644
@@ -193,6 +193,33 @@ static void cf_check mask_write(
     msi->mask = val;
 }
 
Quoted 6 lines

xen/drivers/vpci/msi.c static void cf_check mask_write( · line 197

Quoted 2 lines
xen/drivers/vpci/msi.c +2 -0
+/* 12 is size of MSI structure for 32-bit Message Address without PVM */
+#define MSI_STRUCTURE_SIZE_32 12

I'm confused by this. The minimum size of the capability is 4 bytes for the capability pointer, plus 4 bytes for the message address, plus 2 bytes for the message data. So that's 10 bytes in total.

The remain 2 bytes is Extended Message Data, PCIe spec has this register's definition even though it is optional.

Quoted 10 lines

I think it would be best if you calculate the size based on the existing msi_ macros.

if ( masking )

    end = msi_pending_bits_reg(msi_pos, address64);
else
    end = msi_mask_bits_reg(msi_pos, address64) - 2;

size = end - msi_control_reg(msi_pos);

Thanks, I will change to this in next version.

Quoted 79 lines
Quoted 79 lines
xen/drivers/vpci/msi.c +30 -10
+
+static void fini_msi(struct pci_dev *pdev)
+{
+    unsigned int size = MSI_STRUCTURE_SIZE_32;
+
+    if ( !pdev->vpci->msi )
+        return;
+
+    if ( pdev->vpci->msi->address64 )
+        size += 4;
+    if ( pdev->vpci->msi->masking )
+        size += 4;
+
+    /*
+     * Remove all possible registered registers except capability ID
+     * register and next capability pointer register, which will be
+     * removed in mask function.
+     *msi_mask_bits_reg/
+    vpci_remove_registers(pdev->vpci,
+                          msi_control_reg(pdev->msi_pos),
+                          size - PCI_MSI_FLAGS);
+    xfree(pdev->vpci->msi);
+    pdev->vpci->msi = NULL;
+}
+
 static int cf_check init_msi(struct pci_dev *pdev)
 {
     unsigned int pos = pdev->msi_pos;
@@ -209,12 +236,7 @@ static int cf_check init_msi(struct pci_dev *pdev)
     ret = vpci_add_register(pdev->vpci, control_read, control_write,
                             msi_control_reg(pos), 2, pdev->vpci->msi);
     if ( ret )
-        /*
-         * NB: there's no need to free the msi struct or remove the register
-         * handlers form the config space, the caller will take care of the
-         * cleanup.
-         */
-        return ret;
+        goto fail;
 
     /* Get the maximum number of vectors the device supports. */
     control = pci_conf_read16(pdev->sbdf, msi_control_reg(pos));
@@ -237,20 +259,20 @@ static int cf_check init_msi(struct pci_dev *pdev)
     ret = vpci_add_register(pdev->vpci, address_read, address_write,
                             msi_lower_address_reg(pos), 4, pdev->vpci->msi);
     if ( ret )
-        return ret;
+        goto fail;
 
     ret = vpci_add_register(pdev->vpci, data_read, data_write,
                             msi_data_reg(pos, pdev->vpci->msi->address64), 2,
                             pdev->vpci->msi);
     if ( ret )
-        return ret;
+        goto fail;
 
     if ( pdev->vpci->msi->address64 )
     {
         ret = vpci_add_register(pdev->vpci, address_hi_read, address_hi_write,
                                 msi_upper_address_reg(pos), 4, pdev->vpci->msi);
         if ( ret )
-            return ret;
+            goto fail;
     }
 
     if ( pdev->vpci->msi->masking )
@@ -260,7 +282,7 @@ static int cf_check init_msi(struct pci_dev *pdev)
                                                   pdev->vpci->msi->address64),
                                 4, pdev->vpci->msi);
         if ( ret )
-            return ret;
+            goto fail;
         /*
          * FIXME: do not add any handler for the pending bits for the hardware
          * domain, which means direct access. This will be revisited when
@@ -269,6 +291,11 @@ static int cf_check init_msi(struct pci_dev *pdev)
     }
 
Quoted 13 lines

xen/drivers/vpci/msi.c static int cf_check init_msi(struct pci_dev *pdev) · line 300

Quoted 8 lines
xen/drivers/vpci/msi.c +5 -0
     return 0;
+
+ fail:
+    fini_msi(pdev);
+
+    return ret;
 }
 REGISTER_VPCI_LEGACY_CAP(PCI_CAP_ID_MSI, init_msi);

I wonder if the fini_msi should be referenced in REGISTER_VPCI_{EXTENDED,LEGACY}_CAP(), so that the caller of init_msi() can call fini_msi() on failure and thus avoid all those fail hooks and labels, as the caller can take care of the cleanup.

Got it. I will add a hook for fini_x function.

Quoted 2 lines

Thanks, Roger.

Signature · 2 lines
Best regards,
Jiqian Chen.
#34 lore raw

Re: [PATCH v2 7/8] vpci/msi: Free MSI resources when init_msi() fails

Quoted 27 lines · Jiqian
Quoted 26 lines · Roger Pau Monné
Quoted 25 lines · Jiqian Chen

When init_msi() fails, the previous new changes will hide MSI capability, it can't rely on vpci_deassign_device() to remove all MSI related resources anymore, those resources must be cleaned up in failure path of init_msi.

To do that, add a new function to free MSI resources.

---
cc: "Roger Pau Monné" <[email protected]>
---
v1->v2 changes:
* Added a new function fini_msi to free all MSI resources instead of using an array to record registered registers.

---
 xen/drivers/vpci/msi.c | 47 +++++++++++++++++++++++++++++++++---------
 1 file changed, 37 insertions(+), 10 deletions(-)
xen/drivers/vpci/msi.c +0 -0
index ca89ae9b9c22..48a466dba0ef 100644
@@ -193,6 +193,33 @@ static void cf_check mask_write(
     msi->mask = val;
 }
 
Quoted 7 lines
Quoted 6 lines

xen/drivers/vpci/msi.c static void cf_check mask_write( · line 197

Quoted 2 lines
xen/drivers/vpci/msi.c +2 -0
+/* 12 is size of MSI structure for 32-bit Message Address without PVM */
+#define MSI_STRUCTURE_SIZE_32 12

I'm confused by this. The minimum size of the capability is 4 bytes for the capability pointer, plus 4 bytes for the message address, plus 2 bytes for the message data. So that's 10 bytes in total.

The remain 2 bytes is Extended Message Data, PCIe spec has this register's definition even though it is optional.

I was looking at the PCI Local Bus Specification rev 3.0 (because that has nicer figures IMO), and such field is not listed there. It's listed in the PCI Express 6.0.1 specification. I have to admit I've completely forgot about such optional feature.

We never expose this bit to the guest in the control register, and consequently we never explicitly handle accesses to what would be the extended message data register, neither allow guests to enable it.

If/when we add support for such field we will likely need to adjust fini_msi() to also zap it if present.

Thanks, Roger.

#35 lore raw

PATCH v2 8/8 vpci/msix: Add function to clean MSIX resources

In reply to Jiqian Chen

When init_msix() fails, it needs to clean all MSIX resources. So, add a new function fini_msix() to do that.

And to unregister the mmio handler of vpci_msix_table_ops, add a new function.

---
cc: Jan Beulich <[email protected]>
cc: Andrew Cooper <[email protected]>
cc: "Roger Pau Monné" <[email protected]>
---
v1->v2 changes:
new patch.

Best regards,
Jiqian Chen.

---
 xen/arch/x86/hvm/intercept.c      | 44 ++++++++++++++++++++++
 xen/arch/x86/include/asm/hvm/io.h |  3 ++
 xen/drivers/vpci/msix.c           | 61 ++++++++++++++++++++++++++++---
 3 files changed, 103 insertions(+), 5 deletions(-)
xen/arch/x86/hvm/intercept.c +44 -0
index da22c386763e..5eacf51d4d2c 100644
@@ -276,6 +276,50 @@ void register_mmio_handler(struct domain *d,
     handler->mmio.ops = ops;
 }
 
+void unregister_mmio_handler(struct domain *d,
+                             const struct hvm_mmio_ops *ops)
+{
+    unsigned int i, count = d->arch.hvm.io_handler_count;
+
+    ASSERT(d->arch.hvm.io_handler);
+
+    if ( !count )
+        return;
+
+    for ( i = 0; i < count; i++ )
+        if ( d->arch.hvm.io_handler[i].type == IOREQ_TYPE_COPY &&
+             d->arch.hvm.io_handler[i].mmio.ops == ops )
+            break;
+
+    if ( i == count )
+        return;
+
+    for ( ; i < count - 1; i++ )
+    {
+        struct hvm_io_handler *curr = &d->arch.hvm.io_handler[i];
+        struct hvm_io_handler *next = &d->arch.hvm.io_handler[i + 1];
+
+        curr->type = next->type;
+        curr->ops = next->ops;
+        if ( next->type == IOREQ_TYPE_COPY )
+        {
+            curr->portio.port = 0;
+            curr->portio.size = 0;
+            curr->portio.action = 0;
+            curr->mmio.ops = next->mmio.ops;
+        }
+        else
+        {
+            curr->mmio.ops = 0;
+            curr->portio.port = next->portio.port;
+            curr->portio.size = next->portio.size;
+            curr->portio.action = next->portio.action;
+        }
+    }
+
+    d->arch.hvm.io_handler_count--;
+}
+
 void register_portio_handler(struct domain *d, unsigned int port,
                              unsigned int size, portio_action_t action)
 {
xen/arch/x86/include/asm/hvm/io.h +3 -0
index 565bad300d20..018d2745fd99 100644
@@ -75,6 +75,9 @@ bool hvm_mmio_internal(paddr_t gpa);
 void register_mmio_handler(struct domain *d,
                            const struct hvm_mmio_ops *ops);
 
+void unregister_mmio_handler(struct domain *d,
+                             const struct hvm_mmio_ops *ops);
+
 void register_portio_handler(
     struct domain *d, unsigned int port, unsigned int size,
     portio_action_t action);
xen/drivers/vpci/msix.c +56 -5

79 lines not on this page · raw message

Signature · 1 line
2.34.1
#36 lore raw

Re: [PATCH v2 8/8] vpci/msix: Add function to clean MSIX resources

Quoted 61 lines · Jiqian Chen

When init_msix() fails, it needs to clean all MSIX resources. So, add a new function fini_msix() to do that.

And to unregister the mmio handler of vpci_msix_table_ops, add a new function.

---
cc: Jan Beulich <[email protected]>
cc: Andrew Cooper <[email protected]>
cc: "Roger Pau Monné" <[email protected]>
---
v1->v2 changes:
new patch.

Best regards,
Jiqian Chen.

---
 xen/arch/x86/hvm/intercept.c      | 44 ++++++++++++++++++++++
 xen/arch/x86/include/asm/hvm/io.h |  3 ++
 xen/drivers/vpci/msix.c           | 61 ++++++++++++++++++++++++++++---
 3 files changed, 103 insertions(+), 5 deletions(-)
xen/arch/x86/hvm/intercept.c +30 -0
index da22c386763e..5eacf51d4d2c 100644
@@ -276,6 +276,50 @@ void register_mmio_handler(struct domain *d,
     handler->mmio.ops = ops;
 }
 
+void unregister_mmio_handler(struct domain *d,
+                             const struct hvm_mmio_ops *ops)
+{
+    unsigned int i, count = d->arch.hvm.io_handler_count;
+
+    ASSERT(d->arch.hvm.io_handler);
+
+    if ( !count )
+        return;
+
+    for ( i = 0; i < count; i++ )
+        if ( d->arch.hvm.io_handler[i].type == IOREQ_TYPE_COPY &&
+             d->arch.hvm.io_handler[i].mmio.ops == ops )
+            break;
+
+    if ( i == count )
+        return;
+
+    for ( ; i < count - 1; i++ )
+    {
+        struct hvm_io_handler *curr = &d->arch.hvm.io_handler[i];
+        struct hvm_io_handler *next = &d->arch.hvm.io_handler[i + 1];
+
+        curr->type = next->type;
+        curr->ops = next->ops;
+        if ( next->type == IOREQ_TYPE_COPY )
+        {
+            curr->portio.port = 0;
+            curr->portio.size = 0;
+            curr->portio.action = 0;

xen/arch/x86/hvm/intercept.c void register_mmio_handler(struct domain *d, · line 318

Quoted 10 lines
xen/arch/x86/hvm/intercept.c +10 -0
+            curr->mmio.ops = next->mmio.ops;
+        }
+        else
+        {
+            curr->mmio.ops = 0;
+            curr->portio.port = next->portio.port;
+            curr->portio.size = next->portio.size;
+            curr->portio.action = next->portio.action;
+        }
+    }

Can't you use memmove() instead of a for loop?

memmove(&d->arch.hvm.io_handler[i], &d->arch.hvm.io_handler[i + 1],

	sizeof(d->arch.hvm.io_handler[0]) * (count - i - 1));
Quoted 29 lines
xen/arch/x86/hvm/intercept.c +4 -0
+
+    d->arch.hvm.io_handler_count--;
+}
+
 void register_portio_handler(struct domain *d, unsigned int port,
                              unsigned int size, portio_action_t action)
 {
xen/arch/x86/include/asm/hvm/io.h +3 -0
index 565bad300d20..018d2745fd99 100644
@@ -75,6 +75,9 @@ bool hvm_mmio_internal(paddr_t gpa);
 void register_mmio_handler(struct domain *d,
                            const struct hvm_mmio_ops *ops);
 
+void unregister_mmio_handler(struct domain *d,
+                             const struct hvm_mmio_ops *ops);
+
 void register_portio_handler(
     struct domain *d, unsigned int port, unsigned int size,
     portio_action_t action);
xen/drivers/vpci/msix.c +0 -0
index 6537374c79a0..60654d4f6d0b 100644
@@ -703,6 +703,54 @@ int vpci_make_msix_hole(const struct pci_dev *pdev)
     return 0;
 }
 

xen/drivers/vpci/msix.c int vpci_make_msix_hole(const struct pci_dev *pdev) · line 715

Quoted 10 lines
xen/drivers/vpci/msix.c +10 -0
+static void fini_msix(struct pci_dev *pdev)
+{
+    struct vpci *vpci = pdev->vpci;
+
+    if ( !vpci->msix )
+        return;
+
+    list_del(&vpci->msix->next);
+    if ( list_empty(&pdev->domain->arch.hvm.msix_tables) )
+        unregister_mmio_handler(pdev->domain, &vpci_msix_table_ops);

At the point the MMIO handler is added the capability initialization cannot fail, so arguably if the MSI-X handler is registered there will always be at least one functional MSI-X capability that requires it.

IOW: you can likely drop the addition of unregister_mmio_handler() and avoid the removal of the MMIO handler. Worst case a domain will end up with a dummy handler that does nothing, but it won't cause malfunctions.

Quoted 18 lines
xen/drivers/vpci/msix.c +18 -0
+
+    /* Remove any MSIX regions if present. */
+    for ( unsigned int i = 0;
+          vpci->msix && i < ARRAY_SIZE(vpci->msix->tables);
+          i++ )
+    {
+        unsigned long start = PFN_DOWN(vmsix_table_addr(pdev->vpci, i));
+        unsigned long end = PFN_DOWN(vmsix_table_addr(pdev->vpci, i) +
+                                     vmsix_table_size(pdev->vpci, i) - 1);
+
+        for ( unsigned int j = 0; j < ARRAY_SIZE(vpci->header.bars); j++ )
+        {
+            int rc;
+            const struct vpci_bar *bar = &vpci->header.bars[j];
+
+            if ( rangeset_is_empty(bar->mem) )
+                continue;
+

xen/drivers/vpci/msix.c int vpci_make_msix_hole(const struct pci_dev *pdev) · line 743

Quoted 10 lines
xen/drivers/vpci/msix.c +10 -0
+            rc = rangeset_remove_range(bar->mem, start, end);
+            if ( rc )
+            {
+                gprintk(XENLOG_WARNING,
+                       "%pp: failed to remove MSIX table [%lx, %lx]: %d\n",
+                        &pdev->sbdf, start, end, rc);
+                return;
+            }
+        }
+    }

There's no need to do any of this rangeset manipulation. The BAR rangesets are re-created for any map/unmap request, and hence should be empty unless there's a concurrent operation going on (which won't be the case when initializing the capabilities).

xen/drivers/vpci/msix.c int vpci_make_msix_hole(const struct pci_dev *pdev) · line 747

Quoted 4 lines
xen/drivers/vpci/msix.c +4 -0
+
+    for ( unsigned int i = 0; i < ARRAY_SIZE(vpci->msix->table); i++ )
+        if ( vpci->msix->table[i] )
+            iounmap(vpci->msix->table[i]);

The MSI-X init function never maps tables, so the code here (given it's current usage) will also never unmap anything. However I would also like to use this code for vPCI deassing, at which point the logic will get used.

Thanks, Roger.

#37 lore raw

Re: [PATCH v2 8/8] vpci/msix: Add function to clean MSIX resources

Quoted 10 lines · Roger Pau Monné

xen/drivers/vpci/msix.c int vpci_make_msix_hole(const struct pci_dev *pdev) · line 747

Quoted 4 lines · Jiqian Chen
xen/drivers/vpci/msix.c +4 -0
+
+    for ( unsigned int i = 0; i < ARRAY_SIZE(vpci->msix->table); i++ )
+        if ( vpci->msix->table[i] )
+            iounmap(vpci->msix->table[i]);

The MSI-X init function never maps tables, so the code here (given it's current usage) will also never unmap anything. However I would also like to use this code for vPCI deassing, at which point the logic will get used.

So, I still need to keep this part, right?

Quoted 2 lines

Thanks, Roger.

Signature · 2 lines
Best regards,
Jiqian Chen.
#38 lore raw

Re: [PATCH v2 8/8] vpci/msix: Add function to clean MSIX resources

Quoted 7 lines · Jiqian
Quoted 6 lines · Roger Pau Monné
Quoted 4 lines · Jiqian Chen
xen/drivers/vpci/msix.c +4 -0
+
+    for ( unsigned int i = 0; i < ARRAY_SIZE(vpci->msix->table); i++ )
+        if ( vpci->msix->table[i] )
+            iounmap(vpci->msix->table[i]);
Quoted 5 lines
Quoted 4 lines

The MSI-X init function never maps tables, so the code here (given it's current usage) will also never unmap anything. However I would also like to use this code for vPCI deassing, at which point the logic will get used.

So, I still need to keep this part, right?

Yes, I guess leave it for now, we can always rip it later. I would like to see how doing the cleanup in the caller in case of failure looks.

Then we can likely extend such cleanup to be done in the deassign case also.

Thanks, Roger.

#39 lore raw

Re: [PATCH v2 8/8] vpci/msix: Add function to clean MSIX resources

In reply to Jiqian Chen
Quoted 39 lines · Jiqian Chen
xen/arch/x86/hvm/intercept.c +33 -0
@@ -276,6 +276,50 @@ void register_mmio_handler(struct domain *d,
     handler->mmio.ops = ops;
 }
 
+void unregister_mmio_handler(struct domain *d,
+                             const struct hvm_mmio_ops *ops)
+{
+    unsigned int i, count = d->arch.hvm.io_handler_count;
+
+    ASSERT(d->arch.hvm.io_handler);
+
+    if ( !count )
+        return;
+
+    for ( i = 0; i < count; i++ )
+        if ( d->arch.hvm.io_handler[i].type == IOREQ_TYPE_COPY &&
+             d->arch.hvm.io_handler[i].mmio.ops == ops )
+            break;
+
+    if ( i == count )
+        return;
+
+    for ( ; i < count - 1; i++ )
+    {
+        struct hvm_io_handler *curr = &d->arch.hvm.io_handler[i];
+        struct hvm_io_handler *next = &d->arch.hvm.io_handler[i + 1];
+
+        curr->type = next->type;
+        curr->ops = next->ops;
+        if ( next->type == IOREQ_TYPE_COPY )
+        {
+            curr->portio.port = 0;
+            curr->portio.size = 0;
+            curr->portio.action = 0;
+            curr->mmio.ops = next->mmio.ops;
+        }
+        else

xen/arch/x86/hvm/intercept.c void register_mmio_handler(struct domain *d, · line 321

Quoted 10 lines
xen/arch/x86/hvm/intercept.c +10 -0
+        {
+            curr->mmio.ops = 0;
+            curr->portio.port = next->portio.port;
+            curr->portio.size = next->portio.size;
+            curr->portio.action = next->portio.action;
+        }
+    }
+
+    d->arch.hvm.io_handler_count--;
+}

To add on what Roger said: This is inherently non-atomic, so the domain would need pausing to do such removal in a race-free way. Hence why we deliberately didn't have such a function so far, aiui. (The removal may be safe in the specific case you use it helow, but there's nothing here preventing it from being used elsewhere, without paying attention to the raciness.)

Jan

#40 lore raw

Re: [PATCH v2 8/8] vpci/msix: Add function to clean MSIX resources

Quoted 49 lines · Jan Beulich
Quoted 48 lines · Jiqian Chen
xen/arch/x86/hvm/intercept.c +42 -0
@@ -276,6 +276,50 @@ void register_mmio_handler(struct domain *d,
     handler->mmio.ops = ops;
 }
 
+void unregister_mmio_handler(struct domain *d,
+                             const struct hvm_mmio_ops *ops)
+{
+    unsigned int i, count = d->arch.hvm.io_handler_count;
+
+    ASSERT(d->arch.hvm.io_handler);
+
+    if ( !count )
+        return;
+
+    for ( i = 0; i < count; i++ )
+        if ( d->arch.hvm.io_handler[i].type == IOREQ_TYPE_COPY &&
+             d->arch.hvm.io_handler[i].mmio.ops == ops )
+            break;
+
+    if ( i == count )
+        return;
+
+    for ( ; i < count - 1; i++ )
+    {
+        struct hvm_io_handler *curr = &d->arch.hvm.io_handler[i];
+        struct hvm_io_handler *next = &d->arch.hvm.io_handler[i + 1];
+
+        curr->type = next->type;
+        curr->ops = next->ops;
+        if ( next->type == IOREQ_TYPE_COPY )
+        {
+            curr->portio.port = 0;
+            curr->portio.size = 0;
+            curr->portio.action = 0;
+            curr->mmio.ops = next->mmio.ops;
+        }
+        else
+        {
+            curr->mmio.ops = 0;
+            curr->portio.port = next->portio.port;
+            curr->portio.size = next->portio.size;
+            curr->portio.action = next->portio.action;
+        }
+    }
+
+    d->arch.hvm.io_handler_count--;
Quoted 8 lines

xen/arch/x86/hvm/intercept.c void register_mmio_handler(struct domain *d, · line 321

Quoted 1 line
xen/arch/x86/hvm/intercept.c +1 -0
+}

To add on what Roger said: This is inherently non-atomic, so the domain would need pausing to do such removal in a race-free way. Hence why we deliberately didn't have such a function so far, aiui. (The removal may be safe in the specific case you use it helow, but there's nothing here preventing it from being used elsewhere, without paying attention to the raciness.)

Make sense, thanks for your detailed inputs.
I will delete this part in next version.

Quoted 2 lines

Jan

Signature · 2 lines
Best regards,
Jiqian Chen.