Linux PCI subsystem development
 help / color / mirror / Atom feed
From: Bjorn Helgaas <helgaas@kernel.org>
To: Gokulkrishna Sekar <gokulkrishnasr@gmail.com>
Cc: linux-pci@vger.kernel.org, bhelgaas@google.com,
	mika.westerberg@linux.intel.com, linux-kernel@vger.kernel.org,
	Gokulkrishna Sekar <Gokulkrishna.Sekar@harman.com>
Subject: Re: [PATCH] PCI: Encode corrected PCIe port type with FIELD_PREP()
Date: Wed, 18 Nov 2026 13:15:12 -0600	[thread overview]
Message-ID: <20261118191512.GA707364@bhelgaas> (raw)
In-Reply-To: <20261118162547.GA597321@bhelgaas>

On Wed, Nov 18, 2026 at 10:25:47AM -0600, Bjorn Helgaas wrote:
> 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@harman.com>
> > ---
> >  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);

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.

> >  		}
> >  	} 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
> > 

      reply	other threads:[~2026-11-18 19:15 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-18 15:35 [PATCH] PCI: Encode corrected PCIe port type with FIELD_PREP() Gokulkrishna Sekar
2026-11-18 15:40 ` sashiko-bot
2026-11-18 16:25 ` Bjorn Helgaas
2026-11-18 19:15   ` Bjorn Helgaas [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261118191512.GA707364@bhelgaas \
    --to=helgaas@kernel.org \
    --cc=Gokulkrishna.Sekar@harman.com \
    --cc=bhelgaas@google.com \
    --cc=gokulkrishnasr@gmail.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-pci@vger.kernel.org \
    --cc=mika.westerberg@linux.intel.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox