Linux PCI subsystem development
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Matt Thompson" <matt@boondocker.io>
Cc: linux-pci@vger.kernel.org
Subject: Re: [PATCH RFC v3 4/6] PCI: Resume downstream ports before sysfs Link Control writes
Date: Mon, 16 Nov 2026 10:04:05 +0000	[thread overview]
Message-ID: <sashiko-outbox-216688@kernel.org> (raw)
In-Reply-To: <20261116-rfc-b4-v3-4-0bf052b68f2a@boondocker.io>

Thank you for your contribution! Sashiko AI review found 2 potential
issues to consider:
- [Medium] PCI: sysfs: race between lockless pm_runtime_suspended() and
  RPM_RESUMING in pci_write_config()
- [Medium] PCI: incomplete runtime resume scope for config writes in
  pci_write_config()
--

commit 98319c505535590b91bff76fabaefdd4333fe854
Author: Matt Thompson <matt@boondocker.io>

PCI: Resume downstream ports before sysfs Link Control writes

This commit resumes runtime-suspended PCIe downstream ports in
pci_write_config() before writing bytes that overlap PCI_EXP_LNKCTL so a
subsequent runtime resume does not overwrite the new Link Control value
with saved state.

> diff --git a/drivers/pci/pci-sysfs.c b/drivers/pci/pci-sysfs.c
> index 6669e9ad5bd3b..ff47ea1f90433 100644
> --- a/drivers/pci/pci-sysfs.c
> +++ b/drivers/pci/pci-sysfs.c
> @@ -786,6 +786,7 @@ static ssize_t pci_write_config(struct file *filp, struct kobject *kobj,
>  	struct pci_dev *dev = to_pci_dev(kobj_to_dev(kobj));
>  	unsigned int size = count;
>  	loff_t init_off = off;
> +	loff_t lnkctl = pci_pcie_cap(dev) + PCI_EXP_LNKCTL;
>  	u8 *data = (u8 *) buf;
>  	int ret;
>  
> @@ -809,6 +810,21 @@ static ssize_t pci_write_config(struct file *filp, struct kobject *kobj,
>  
>  	pci_config_pm_runtime_get(dev);
>  
> +	/*
> +	 * Link Control can be written while config space remains accessible in
> +	 * runtime suspend. Restore saved state first so a later resume cannot
> +	 * replace the new link setting with the saved value.
> +	 */
> +	if (count && pci_is_pcie(dev) && pcie_downstream_port(dev) &&
> +	    pm_runtime_suspended(&dev->dev) &&

[Severity: Medium]
Can a concurrent runtime resume cause pm_runtime_suspended() to return
false before saved PCIe state has been restored?

When the port is runtime-suspended outside D3cold,
pci_config_pm_runtime_get() increments usage_count via
pm_runtime_get_noresume() and returns with dev->power.runtime_status still
at RPM_SUSPENDED.  If another thread begins resuming the port at the same
time, rpm_resume() sets runtime_status to RPM_RESUMING and drops
dev->power.lock while running pci_pm_runtime_resume():

CPU0                                  CPU1
pci_scan_bridge_extend()              pci_write_config()
  pm_runtime_get_sync()                 pci_config_pm_runtime_get()
    rpm_resume()
      runtime_status = RPM_RESUMING
      rpm_callback()
                                        pm_runtime_suspended() -> false
                                        pci_user_write_config_word(LNKCTL)
        pci_pm_runtime_resume()
          ...
            pci_restore_pcie_state()
              pcie_capability_write_word(LNKCTL)  <-- overwrites new value

Because pm_runtime_suspended() only matches RPM_SUSPENDED, CPU1 skips
pm_runtime_resume() instead of waiting in rpm_resume() for the in-flight
transition to finish, and pci_restore_pcie_state() overwrites the new
PCI_EXP_LNKCTL value with the stale pre-suspend value.  Checking
!pm_runtime_active(&dev->dev) (or calling pm_runtime_resume()
unconditionally) would wait for an in-flight resume to complete.

> +	    off < lnkctl + sizeof(u16) && off + count > lnkctl) {
> +		ret = pm_runtime_resume(&dev->dev);
> +		if (ret < 0) {
> +			pci_config_pm_runtime_put(dev);
> +			return ret;
> +		}
> +	}

[Severity: Medium]
Does restricting the runtime resume to PCI_EXP_LNKCTL on downstream ports
leave other saved registers vulnerable to being overwritten on resume?

When a PCI device runtime-suspends, pci_save_state() saves the standard
config header and PCIe capability registers including PCI_EXP_DEVCTL,
PCI_EXP_SLTCTL, PCI_EXP_RTCTL, PCI_EXP_DEVCTL2, PCI_EXP_LNKCTL2, and
PCI_EXP_SLTCTL2, all of which pci_restore_pcie_state() writes back on
resume.  If userspace writes PCI_EXP_LNKCTL2 (for example, to set Target
Link Speed) while the port is runtime-suspended and then writes
PCI_EXP_LNKCTL to retrain or enable the link, the second write triggers
pm_runtime_resume() -> pci_restore_pcie_state() and clobbers the
PCI_EXP_LNKCTL2 change with the pre-suspend value.  Writes to other saved
registers followed by a rescan, or config writes through
proc_bus_pci_write(), have the same issue because pci_config_pm_runtime_get()
only resumes devices in PCI_D3cold.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261116-rfc-b4-v3-0-0bf052b68f2a@boondocker.io?part=4

  reply	other threads:[~2026-11-16 10:04 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-16  9:35 [PATCH RFC v3 0/6] PCI: rockchip-host: Handle unavailable downstream config space Matt Thompson via B4 Relay
2026-11-16  9:35 ` [PATCH RFC v3 1/6] PCI: rockchip-host: Skip downstream config access while link is down Matt Thompson via B4 Relay
2026-11-16  9:46   ` sashiko-bot
2026-11-16  9:35 ` [PATCH RFC v3 2/6] arm64: Allow synchronous abort fixups for guarded MMIO reads Matt Thompson via B4 Relay
2026-11-16  9:47   ` sashiko-bot
2026-11-16  9:35 ` [PATCH RFC v3 3/6] PCI: rockchip-host: Recover downstream configuration access Matt Thompson via B4 Relay
2026-11-16  9:52   ` sashiko-bot
2026-11-16  9:35 ` [PATCH RFC v3 4/6] PCI: Resume downstream ports before sysfs Link Control writes Matt Thompson via B4 Relay
2026-11-16 10:04   ` sashiko-bot [this message]
2026-11-16  9:35 ` [PATCH RFC v3 5/6] PCI: rockchip-host: Defer retraining during initial link training Matt Thompson via B4 Relay
2026-11-16  9:51   ` sashiko-bot
2026-11-16  9:35 ` [PATCH RFC v3 6/6] PCI: rockchip-host: Mask the client correctable error interrupt Matt Thompson via B4 Relay
2026-11-16  9:54   ` sashiko-bot

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=sashiko-outbox-216688@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=linux-pci@vger.kernel.org \
    --cc=matt@boondocker.io \
    --cc=sashiko-reviews@lists.linux.dev \
    /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