Re: [PATCH] PCI: Encode corrected PCIe port type with FIELD_PREP()
From: Bjorn Helgaas
Date: Wed Nov 18 2026 - 11:26:14 EST
On Wed, Nov 18, 2026 at 03:35:05PM +0000, Gokulkrishna Sekar wrote:
> 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.
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.
> 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")
> Signed-off-by: Gokulkrishna Sekar <Gokulkrishna.Sekar@xxxxxxxxxx>
> ---
> drivers/pci/probe.c | 6 ++++--
> 1 file changed, 4 insertions(+), 2 deletions(-)
>
> diff --git a/drivers/pci/probe.c b/drivers/pci/probe.c
> index 9a178e0e5f7f..7b09a4463097 100644
> --- a/drivers/pci/probe.c
> +++ b/drivers/pci/probe.c
> @@ -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);
> }
> }
> }
> --
> 2.56.1
>