Skip to content

[linux-6.6.y] PCI: Fix PCIe unplug Enumeration Long Blocking Issue#1692

Closed
leoliu-oc wants to merge 1 commit into
deepin-community:linux-6.6.yfrom
leoliu-oc:linux-6.6.y-102-pci-unplug
Closed

[linux-6.6.y] PCI: Fix PCIe unplug Enumeration Long Blocking Issue#1692
leoliu-oc wants to merge 1 commit into
deepin-community:linux-6.6.yfrom
leoliu-oc:linux-6.6.y-102-pci-unplug

Conversation

@leoliu-oc
Copy link
Copy Markdown
Contributor

@leoliu-oc leoliu-oc commented May 12, 2026

When a USB4 device is inserted, a PCIe tunnel will be established. Then, the PCIe hierarchy within the USB4 device will be enumerated. If the device is suddenly unplugged during the enumeration process, it is possible that the function pci_find_next_ext_capability called when the PCI driver scans the PCIe extended capability may take a very long time, perhaps more than 20 seconds.

Summary by Sourcery

Bug Fixes:

  • Avoid prolonged pci_find_next_ext_capability loops by detecting and aborting on 0xffffffff PCIe extended capability headers for unplugged devices.

When a USB4 device is inserted, a PCIe tunnel will be established.
Then, the PCIe hierarchy within the USB4 device will be enumerated.
If the device is suddenly unplugged during the enumeration process,
it is possible that the function pci_find_next_ext_capability called when
the PCI driver scans the PCIe extended capability may take a very long
time, perhaps more than 20 seconds.

Signed-off-by: LeoLiu-oc <leoliu-oc@zhaoxin.com>
@sourcery-ai
Copy link
Copy Markdown

sourcery-ai Bot commented May 12, 2026

Reviewer's guide (collapsed on small PRs)

Reviewer's Guide

Adds an early exit in pci_find_next_ext_capability() to avoid long blocking when a device disappears during PCIe extended capability enumeration by checking for an all-ones header value and returning immediately.

Sequence diagram for pci_find_next_ext_capability early exit on unplugged device

sequenceDiagram
    participant PciDriver
    participant pci_find_next_ext_capability
    participant PciDevice

    PciDriver->>pci_find_next_ext_capability: pci_find_next_ext_capability(dev, start, cap)
    activate pci_find_next_ext_capability
    pci_find_next_ext_capability->>PciDevice: pci_read_config_dword(dev, pos, header)
    alt header is 0xffffffff
        pci_find_next_ext_capability-->>PciDriver: return 0
    else header is valid
        loop scan extended capabilities
            pci_find_next_ext_capability->>PciDevice: pci_read_config_dword(dev, next_pos, header)
            alt PCI_EXT_CAP_ID(header) == cap
                pci_find_next_ext_capability-->>PciDriver: return pos
            end
        end
    end
    deactivate pci_find_next_ext_capability
Loading

File-Level Changes

Change Details Files
Prevent long blocking in PCIe extended capability scanning when the device is unplugged mid-enumeration by early-returning on an invalid all-ones capability header.
  • Add a defensive check for header == 0xffffffff inside the capability scan loop.
  • Return 0 immediately when an all-ones header is encountered to terminate the scan instead of continuing to traverse the capability list.
drivers/pci/pci.c

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@deepin-ci-robot
Copy link
Copy Markdown

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by:
Once this PR has been reviewed and has the lgtm label, please assign avenger-285714 for approval. For more information see the Code Review Process.

The full list of commands accepted by this bot can be found here.

Details Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@deepin-ci-robot
Copy link
Copy Markdown

Hi @leoliu-oc. Thanks for your PR.

I'm waiting for a deepin-community member to verify that this patch is reasonable to test. If it is, they should reply with /ok-to-test on its own line. Until that is done, I will not automatically test new commits in this PR, but the usual testing commands by org members will still work. Regular contributors should join the org to skip this step.

Once the patch is verified, the new status will be reflected by the ok-to-test label.

I understand the commands that are listed here.

Details

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository.

Copy link
Copy Markdown

@sourcery-ai sourcery-ai Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey - I've left some high level feedback:

  • Consider adding a brief comment explaining why a header of 0xffffffff indicates an unplugged device or invalid config space so future readers understand the reason for this early return.
  • Instead of directly checking for 0xffffffff here, consider centralizing the unplugged/invalid config space detection (e.g., via pci_device_is_present() or a helper) so similar checks are consistent and reusable across PCIe capability walkers.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- Consider adding a brief comment explaining why a header of 0xffffffff indicates an unplugged device or invalid config space so future readers understand the reason for this early return.
- Instead of directly checking for 0xffffffff here, consider centralizing the unplugged/invalid config space detection (e.g., via pci_device_is_present() or a helper) so similar checks are consistent and reusable across PCIe capability walkers.

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR hardens PCIe extended capability enumeration against devices that are unplugged mid-scan (e.g., during USB4 PCIe tunnel enumeration), preventing long stalls in pci_find_next_ext_capability() when config reads return an all-ones error response.

Changes:

  • Abort extended capability traversal early when the capability header reads as 0xffffffff (error response for a non-responding/unplugged device).

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread drivers/pci/pci.c
return 0;

while (ttl-- > 0) {
if (header == 0xffffffff)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants