Skip to contentSkip to conversation

pci-bridge: avoid linking a single downstream port more than once

PATCH Superseded Yao Xingtao via 3 participants 5 messages Jul 17, 2024 – Jul 18, 2024

v2 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 pci-bridge: avoid linking a single downstream port more than once

Since the downstream port is not checked, two slots can be linked to a single port. However, this can prevent the driver from detecting the device properly.

It is necessary to ensure that a downstream port is not linked more than once.

Links: https://lore.kernel.org/qemu-devel/OSZPR01MB6453BC61D2FF4035F18084EF8DDC2@OSZPR01MB6453.jpnprd01.prod.outlook.com

---
V1[1] -> V2:
- Move downstream port check forward

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

---
 hw/pci-bridge/cxl_downstream.c     | 5 +++++
 hw/pci-bridge/pcie_root_port.c     | 5 +++++
 hw/pci-bridge/xio3130_downstream.c | 5 +++++
 3 files changed, 15 insertions(+)
hw/pci-bridge/cxl_downstream.c +5 -0
index 742da07a015a..af81ddfeec13 100644
@@ -142,6 +142,11 @@ static void cxl_dsp_realize(PCIDevice *d, Error **errp)
     MemoryRegion *component_bar = &cregs->component_registers;
     int rc;
 
+    if (pcie_find_port_by_pn(pci_get_bus(d), p->port) != NULL) {
+        error_setg(errp, "Can't link port, error %d", -EBUSY);
+        return;
+    }
+
     pci_bridge_initfn(d, TYPE_PCIE_BUS);
     pcie_port_init_reg(d);
 
hw/pci-bridge/pcie_root_port.c +5 -0
index 09a34786bc62..a540204bda27 100644
@@ -67,6 +67,11 @@ static void rp_realize(PCIDevice *d, Error **errp)
     PCIERootPortClass *rpc = PCIE_ROOT_PORT_GET_CLASS(d);
     int rc;
 
+    if (pcie_find_port_by_pn(pci_get_bus(d), p->port) != NULL) {
+        error_setg(errp, "Can't link port, error %d", -EBUSY);
+        return;
+    }
+
     pci_config_set_interrupt_pin(d->config, 1);
     if (d->cap_present & QEMU_PCIE_CAP_CXL) {
         pci_bridge_initfn(d, TYPE_CXL_BUS);
hw/pci-bridge/xio3130_downstream.c +5 -0
index 907d5105b019..63f6baa615fd 100644
@@ -69,6 +69,11 @@ static void xio3130_downstream_realize(PCIDevice *d, Error **errp)
     PCIESlot *s = PCIE_SLOT(d);
     int rc;
 
+    if (pcie_find_port_by_pn(pci_get_bus(d), p->port) != NULL) {
+        error_setg(errp, "Can't link port, error %d", -EBUSY);
+        return;
+    }
+
     pci_bridge_initfn(d, TYPE_PCIE_BUS);
     pcie_port_init_reg(d);
 
Signature · 1 line
2.37.3
#1 lore raw

Hi Yao,

Quoted 26 lines · Yao Xingtao via

Since the downstream port is not checked, two slots can be linked to a single port. However, this can prevent the driver from detecting the device properly.

It is necessary to ensure that a downstream port is not linked more than once.

Links: https://lore.kernel.org/qemu-devel/OSZPR01MB6453BC61D2FF4035F18084EF8DDC2@OSZPR01MB6453.jpnprd01.prod.outlook.com

---
V1[1] -> V2:
- Move downstream port check forward

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

---
  hw/pci-bridge/cxl_downstream.c     | 5 +++++
  hw/pci-bridge/pcie_root_port.c     | 5 +++++
  hw/pci-bridge/xio3130_downstream.c | 5 +++++
  3 files changed, 15 insertions(+)
hw/pci-bridge/cxl_downstream.c +0 -0
index 742da07a015a..af81ddfeec13 100644

hw/pci-bridge/cxl_downstream.c static void cxl_dsp_realize(PCIDevice *d, Error **errp) · line 147

Quoted 6 lines
hw/pci-bridge/cxl_downstream.c +3 -0
@@ -142,6 +142,11 @@ static void cxl_dsp_realize(PCIDevice *d, Error **errp)
      MemoryRegion *component_bar = &cregs->component_registers;
      int rc;
 
+    if (pcie_find_port_by_pn(pci_get_bus(d), p->port) != NULL) {
+        error_setg(errp, "Can't link port, error %d", -EBUSY);
+        return;

Could pcie_cap_slot_init() be a good place to check for that?

Otherwise IMHO we should add a helper in "hw/pci/pcie.h" and call it here, not duplicate this code in each model.

Quoted 4 lines
hw/pci-bridge/cxl_downstream.c +2 -0
+    }
+
      pci_bridge_initfn(d, TYPE_PCIE_BUS);
      pcie_port_init_reg(d);
#2 lore raw
Quoted 48 lines

-----Original Message-----
From: Philippe Mathieu-Daudé <[email protected]>
Sent: Wednesday, July 17, 2024 6:18 PM
To: Yao, Xingtao/姚 幸涛 <[email protected]>; [email protected]; [email protected]
Cc: [email protected]; [email protected]
Subject: Re: [PATCH v2] pci-bridge: avoid linking a single downstream port more than once

Hi Yao,

Quoted 8 lines · Yao Xingtao via

Since the downstream port is not checked, two slots can be linked to a single port. However, this can prevent the driver from detecting the device properly.

It is necessary to ensure that a downstream port is not linked more than once.

Links:

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

Quoted 7 lines

---
V1[1] -> V2:
- Move downstream port check forward

[1]

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

Review

Quoted 7 lines
---
  hw/pci-bridge/cxl_downstream.c     | 5 +++++
  hw/pci-bridge/pcie_root_port.c     | 5 +++++
  hw/pci-bridge/xio3130_downstream.c | 5 +++++
  3 files changed, 15 insertions(+)
(quoted diff) +0 -0

b/hw/pci-bridge/cxl_downstream.c

Quoted 7 lines

index 742da07a015a..af81ddfeec13 100644

hw/pci-bridge/cxl_downstream.c +0 -0
@@ -142,6 +142,11 @@ static void cxl_dsp_realize(PCIDevice *d, Error **errp)
      MemoryRegion *component_bar = &cregs->component_registers;
      int rc;
 
Quoted 8 lines

hw/pci-bridge/cxl_downstream.c static void cxl_dsp_realize(PCIDevice *d, Error **errp) · line 147

Quoted 3 lines
hw/pci-bridge/cxl_downstream.c +3 -0
+    if (pcie_find_port_by_pn(pci_get_bus(d), p->port) != NULL) {
+        error_setg(errp, "Can't link port, error %d", -EBUSY);
+        return;

Could pcie_cap_slot_init() be a good place to check for that?

Otherwise IMHO we should add a helper in "hw/pci/pcie.h" and call it here, not duplicate this code in each model.

Yes, thanks for your comment.
I think pcie_cap_init() may be better.

Quoted 5 lines
Quoted 5 lines
hw/pci-bridge/cxl_downstream.c +2 -0
+    }
+
      pci_bridge_initfn(d, TYPE_PCIE_BUS);
      pcie_port_init_reg(d);
#3 lore raw
In reply to Yao Xingtao via
Quoted 9 lines · Yao Xingtao

Since the downstream port is not checked, two slots can be linked to a single port. However, this can prevent the driver from detecting the device properly.

It is necessary to ensure that a downstream port is not linked more than once.

Links: https://lore.kernel.org/qemu-devel/OSZPR01MB6453BC61D2FF4035F18084EF8DDC2@OSZPR01MB6453.jpnprd01.prod.outlook.com

You also need to take ARI into account.
That can look like slot != 0.

Quoted 61 lines

---
V1[1] -> V2:
- Move downstream port check forward

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

---
 hw/pci-bridge/cxl_downstream.c     | 5 +++++
 hw/pci-bridge/pcie_root_port.c     | 5 +++++
 hw/pci-bridge/xio3130_downstream.c | 5 +++++
 3 files changed, 15 insertions(+)
hw/pci-bridge/cxl_downstream.c +5 -0
index 742da07a015a..af81ddfeec13 100644
@@ -142,6 +142,11 @@ static void cxl_dsp_realize(PCIDevice *d, Error **errp)
     MemoryRegion *component_bar = &cregs->component_registers;
     int rc;
 
+    if (pcie_find_port_by_pn(pci_get_bus(d), p->port) != NULL) {
+        error_setg(errp, "Can't link port, error %d", -EBUSY);
+        return;
+    }
+
     pci_bridge_initfn(d, TYPE_PCIE_BUS);
     pcie_port_init_reg(d);
 
hw/pci-bridge/pcie_root_port.c +5 -0
index 09a34786bc62..a540204bda27 100644
@@ -67,6 +67,11 @@ static void rp_realize(PCIDevice *d, Error **errp)
     PCIERootPortClass *rpc = PCIE_ROOT_PORT_GET_CLASS(d);
     int rc;
 
+    if (pcie_find_port_by_pn(pci_get_bus(d), p->port) != NULL) {
+        error_setg(errp, "Can't link port, error %d", -EBUSY);
+        return;
+    }
+
     pci_config_set_interrupt_pin(d->config, 1);
     if (d->cap_present & QEMU_PCIE_CAP_CXL) {
         pci_bridge_initfn(d, TYPE_CXL_BUS);
hw/pci-bridge/xio3130_downstream.c +5 -0
index 907d5105b019..63f6baa615fd 100644
@@ -69,6 +69,11 @@ static void xio3130_downstream_realize(PCIDevice *d, Error **errp)
     PCIESlot *s = PCIE_SLOT(d);
     int rc;
 
+    if (pcie_find_port_by_pn(pci_get_bus(d), p->port) != NULL) {
+        error_setg(errp, "Can't link port, error %d", -EBUSY);
+        return;
+    }
+
     pci_bridge_initfn(d, TYPE_PCIE_BUS);
     pcie_port_init_reg(d);
 
Signature · 1 line
2.37.3
#4 lore raw
Quoted 19 lines

-----Original Message-----
From: Michael S. Tsirkin <[email protected]>
Sent: Wednesday, July 17, 2024 8:04 PM
To: Yao, Xingtao/姚 幸涛 <[email protected]>
Cc: [email protected]; [email protected]; [email protected]
Subject: Re: [PATCH v2] pci-bridge: avoid linking a single downstream port more than once

Quoted 7 lines · Yao Xingtao

Since the downstream port is not checked, two slots can be linked to a single port. However, this can prevent the driver from detecting the device properly.

It is necessary to ensure that a downstream port is not linked more than once.

Quoted 7 lines
Quoted 1 line

Links:

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

Quoted 1 line

You also need to take ARI into account. That can look like slot != 0.

Sorry, I'm not familiar with the PCIe protocol, could you explain it in detail?