Skip to contentSkip to conversation

PCI: Encode corrected PCIe port type with FIELD_PREP()

PATCH In review Gokulkrishna Sekar 3 participants 4 messages Nov 18 PCI

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 PCI: Encode corrected PCIe port type with FIELD_PREP()

set_pcie_port_type() validates PCIe port topology and corrects invalid upstream/downstream port type information in the cached pcie_flags_reg value.

When performing these corrections, the code clears the PCI_EXP_FLAGS_TYPE field but writes PCI_EXP_TYPE_UPSTREAM or PCI_EXP_TYPE_DOWNSTREAM directly into pcie_flags_reg. The PCI_EXP_TYPE_* values are decoded port type numbers, not values encoded for the PCI_EXP_FLAGS_TYPE bitfield, so the corrected type is stored in the wrong bit position.

As a result, the cached port type may not match the corrected topology and subsequent users of pcie_flags_reg can observe an incorrect PCIe device type.

Use FIELD_PREP(PCI_EXP_FLAGS_TYPE, ...) when updating the cached field so the corrected port type is encoded consistently with the format used in the PCI Express Capabilities Register.

Fixes ca78410403dd ("PCI: Get rid of dev->has_secondary_link flag")
---
 drivers/pci/probe.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
drivers/pci/probe.c +4 -2
index 9a178e0e5f7f..7b09a4463097 100644
@@ -1674,7 +1674,8 @@ void set_pcie_port_type(struct pci_dev *pdev)
 		if (pcie_downstream_port(parent)) {
 			pci_info(pdev, "claims to be downstream port but is acting as upstream port, correcting type\n");
 			pdev->pcie_flags_reg &= ~PCI_EXP_FLAGS_TYPE;
-			pdev->pcie_flags_reg |= PCI_EXP_TYPE_UPSTREAM;
+			pdev->pcie_flags_reg |= FIELD_PREP(PCI_EXP_FLAGS_TYPE,
+							   PCI_EXP_TYPE_UPSTREAM);
 		}
 	} else if (type == PCI_EXP_TYPE_UPSTREAM) {
 		/*
@@ -1685,7 +1686,8 @@ void set_pcie_port_type(struct pci_dev *pdev)
 		if (pci_pcie_type(parent) == PCI_EXP_TYPE_UPSTREAM) {
 			pci_info(pdev, "claims to be upstream port but is acting as downstream port, correcting type\n");
 			pdev->pcie_flags_reg &= ~PCI_EXP_FLAGS_TYPE;
-			pdev->pcie_flags_reg |= PCI_EXP_TYPE_DOWNSTREAM;
+			pdev->pcie_flags_reg |= FIELD_PREP(PCI_EXP_FLAGS_TYPE,
+							   PCI_EXP_TYPE_DOWNSTREAM);
 		}
 	}
 }
Signature · 1 line
2.56.1
#1 lore raw
Sashiko has reviewed this patch and found no issues.
Quoted 15 lines

set_pcie_port_type() validates PCIe port topology and corrects invalid upstream/downstream port type information in the cached pcie_flags_reg value.

When performing these corrections, the code clears the PCI_EXP_FLAGS_TYPE field but writes PCI_EXP_TYPE_UPSTREAM or PCI_EXP_TYPE_DOWNSTREAM directly into pcie_flags_reg. The PCI_EXP_TYPE_* values are decoded port type numbers, not values encoded for the PCI_EXP_FLAGS_TYPE bitfield, so the corrected type is stored in the wrong bit position.

As a result, the cached port type may not match the corrected topology and subsequent users of pcie_flags_reg can observe an incorrect PCIe device type.

Quoted 6 lines

Use FIELD_PREP(PCI_EXP_FLAGS_TYPE, ...) when updating the cached field so the corrected port type is encoded consistently with the format used in the PCI Express Capabilities Register.

Fixes ca78410403dd ("PCI: Get rid of dev->has_secondary_link flag")

Sashiko has reviewed this patch and found no issues.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
#2 lore raw
In reply to Gokulkrishna Sekar
Quoted 11 lines · Gokulkrishna Sekar

set_pcie_port_type() validates PCIe port topology and corrects invalid upstream/downstream port type information in the cached pcie_flags_reg value.

When performing these corrections, the code clears the PCI_EXP_FLAGS_TYPE field but writes PCI_EXP_TYPE_UPSTREAM or PCI_EXP_TYPE_DOWNSTREAM directly into pcie_flags_reg. The PCI_EXP_TYPE_* values are decoded port type numbers, not values encoded for the PCI_EXP_FLAGS_TYPE bitfield, so the corrected type is stored in the wrong bit position.

Quoted 3 lines

As a result, the cached port type may not match the corrected topology and subsequent users of pcie_flags_reg can observe an incorrect PCIe device type.

Looks like subsequent users would think the device was a PCIe Endpoint (PCI_EXP_TYPE_ENDPOINT==0x0) instead of an Upstream or Downstream Port, but only for devices that claim to be the wrong type in the first place, so it probably doesn't actually affect many systems.

But it seems like a candidate for stable anyway.

Quoted 37 lines

Use FIELD_PREP(PCI_EXP_FLAGS_TYPE, ...) when updating the cached field so the corrected port type is encoded consistently with the format used in the PCI Express Capabilities Register.

Fixes ca78410403dd ("PCI: Get rid of dev->has_secondary_link flag")
---
 drivers/pci/probe.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
drivers/pci/probe.c +4 -2
index 9a178e0e5f7f..7b09a4463097 100644
@@ -1674,7 +1674,8 @@ void set_pcie_port_type(struct pci_dev *pdev)
 		if (pcie_downstream_port(parent)) {
 			pci_info(pdev, "claims to be downstream port but is acting as upstream port, correcting type\n");
 			pdev->pcie_flags_reg &= ~PCI_EXP_FLAGS_TYPE;
-			pdev->pcie_flags_reg |= PCI_EXP_TYPE_UPSTREAM;
+			pdev->pcie_flags_reg |= FIELD_PREP(PCI_EXP_FLAGS_TYPE,
+							   PCI_EXP_TYPE_UPSTREAM);
 		}
 	} else if (type == PCI_EXP_TYPE_UPSTREAM) {
 		/*
@@ -1685,7 +1686,8 @@ void set_pcie_port_type(struct pci_dev *pdev)
 		if (pci_pcie_type(parent) == PCI_EXP_TYPE_UPSTREAM) {
 			pci_info(pdev, "claims to be upstream port but is acting as downstream port, correcting type\n");
 			pdev->pcie_flags_reg &= ~PCI_EXP_FLAGS_TYPE;
-			pdev->pcie_flags_reg |= PCI_EXP_TYPE_DOWNSTREAM;
+			pdev->pcie_flags_reg |= FIELD_PREP(PCI_EXP_FLAGS_TYPE,
+							   PCI_EXP_TYPE_DOWNSTREAM);
 		}
 	}
 }
Signature · 1 line
2.56.1
#3 lore raw
Quoted 33 lines · Bjorn Helgaas
Quoted 11 lines · Gokulkrishna Sekar

set_pcie_port_type() validates PCIe port topology and corrects invalid upstream/downstream port type information in the cached pcie_flags_reg value.

When performing these corrections, the code clears the PCI_EXP_FLAGS_TYPE field but writes PCI_EXP_TYPE_UPSTREAM or PCI_EXP_TYPE_DOWNSTREAM directly into pcie_flags_reg. The PCI_EXP_TYPE_* values are decoded port type numbers, not values encoded for the PCI_EXP_FLAGS_TYPE bitfield, so the corrected type is stored in the wrong bit position.

Quoted 3 lines

As a result, the cached port type may not match the corrected topology and subsequent users of pcie_flags_reg can observe an incorrect PCIe device type.

Looks like subsequent users would think the device was a PCIe Endpoint (PCI_EXP_TYPE_ENDPOINT==0x0) instead of an Upstream or Downstream Port, but only for devices that claim to be the wrong type in the first place, so it probably doesn't actually affect many systems.

But it seems like a candidate for stable anyway.

Quoted 10 lines

Use FIELD_PREP(PCI_EXP_FLAGS_TYPE, ...) when updating the cached field so the corrected port type is encoded consistently with the format used in the PCI Express Capabilities Register.

Fixes ca78410403dd ("PCI: Get rid of dev->has_secondary_link flag")
---
 drivers/pci/probe.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
Quoted 11 lines

drivers/pci/probe.c void set_pcie_port_type(struct pci_dev *pdev) · line 1678

Quoted 11 lines
drivers/pci/probe.c +2 -1
index 9a178e0e5f7f..7b09a4463097 100644
@@ -1674,7 +1674,8 @@ void set_pcie_port_type(struct pci_dev *pdev)
 		if (pcie_downstream_port(parent)) {
 			pci_info(pdev, "claims to be downstream port but is acting as upstream port, correcting type\n");
 			pdev->pcie_flags_reg &= ~PCI_EXP_FLAGS_TYPE;
-			pdev->pcie_flags_reg |= PCI_EXP_TYPE_UPSTREAM;
+			pdev->pcie_flags_reg |= FIELD_PREP(PCI_EXP_FLAGS_TYPE,
+							   PCI_EXP_TYPE_UPSTREAM);

Oh, I forgot: I think this is a candidate for FIELD_MODIFY() instead of the "&= ~PCI_EXP_FLAGS_TYPE, FIELD_PREP(PCI_EXP_FLAGS_TYPE)" sequence so we don't have to repeat PCI_EXP_FLAGS_TYPE.

Quoted 16 lines
Quoted 16 lines
drivers/pci/probe.c +2 -1
 		}
 	} else if (type == PCI_EXP_TYPE_UPSTREAM) {
 		/*
@@ -1685,7 +1686,8 @@ void set_pcie_port_type(struct pci_dev *pdev)
 		if (pci_pcie_type(parent) == PCI_EXP_TYPE_UPSTREAM) {
 			pci_info(pdev, "claims to be upstream port but is acting as downstream port, correcting type\n");
 			pdev->pcie_flags_reg &= ~PCI_EXP_FLAGS_TYPE;
-			pdev->pcie_flags_reg |= PCI_EXP_TYPE_DOWNSTREAM;
+			pdev->pcie_flags_reg |= FIELD_PREP(PCI_EXP_FLAGS_TYPE,
+							   PCI_EXP_TYPE_DOWNSTREAM);
 		}
 	}
 }
Signature · 1 line
2.56.1