Linux PCI subsystem development
 help / color / mirror / Atom feed
* [PATCH v2 0/4] PCI/ASPM: keep software state and hardware in sync
@ 2026-11-30  1:43 Yaxiong Tian
  2026-11-30  1:45 ` [PATCH v2 1/4] PCI/ASPM: Only update ASPM and CLKREQ bits in the saved LNKCTL Yaxiong Tian
                   ` (4 more replies)
  0 siblings, 5 replies; 6+ messages in thread
From: Yaxiong Tian @ 2026-11-30  1:43 UTC (permalink / raw)
  To: bhelgaas, mani, krishna.chundru, ilpo.jarvinen, lukas,
	sathyanarayanan.kuppuswamy, david.e.box, vidyas
  Cc: linux-pci, Yaxiong Tian

Four places where the ASPM core ends up recording one state while the
hardware is in another.  The first three are about the same invariant:
link->aspm_enabled and the saved LNKCTL must match what is programmed in
the device.  The fourth is a NULL dereference on a link that cannot be
managed at all.

1/4 Only update ASPM and CLKREQ bits in the saved LNKCTL

    pci_update_aspm_saved_state() rebuilt the whole saved LNKCTL from a
    value read back from hardware.  When that read failed, or returned
    PCI_ERROR_RESPONSE, the snapshot became zeroes or all ones, and
    pci_restore_pcie_state() wrote that back: ASPMC and CCC were lost in
    the first case, ASPMC was forced to L0s+L1 in the second.

2/4 Update saved LNKCTL for every function of the link

    pcie_config_aspm_link() programs LNKCTL on every function of the
    downstream component, but refreshed only function 0's snapshot.  A
    driver calling pci_restore_state() from slot_reset() put the other
    functions back to a stale ASPMC value, leaving the functions of one
    link disagreeing.

3/4 Disable ASPM left on by firmware after a HEADER quirk

    A HEADER quirk that clears the cached L0s/L1 support makes
    pcie_aspm_cap_init() return before reading LNKCTL.  Firmware may have
    enabled ASPM on that link, and because link->aspm_enabled stays zero,
    every later request finds the link "already disabled" and leaves the
    hardware alone.  Mark the states disabled and configure the link
    right away, as the blacklist path does.  Doing it here also
    closes the same window on the blacklist path, which only cleared
    LNKCTL.ASPMC from a later pcie_config_aspm_path() that is skipped
    under POLICY_POWERSAVE and POLICY_POWER_SUPERSAVE.

4/4 Skip links whose downstream bus has no function 0

    alloc_pcie_link_state() stores pci_function_0() without checking the
    result, and pcie_aspm_cap_init() dereferences it.  Reachable only
    when function 0 is missing, which needs a hypervisor that passes
    through individual functions (s390, loongarch, Jailhouse).
    Pre-existing; split out so the fix can be reviewed on its own.

The first two were found by inspection while porting the L0s/L1 caching
series; the third is a regression from 30579eebba6a, which is in v6.18;
the fourth predates all of them.  The third and the fourth were exercised
in a QEMU guest, as described in their commit messages; none of them has
been reproduced on hardware.

Yaxiong Tian (4):
  PCI/ASPM: Only update ASPM and CLKREQ bits in the saved LNKCTL
  PCI/ASPM: Update saved LNKCTL for every function of the link
  PCI/ASPM: Disable ASPM left on by firmware after a HEADER quirk
  PCI/ASPM: Skip links whose downstream bus has no function 0

 drivers/pci/pcie/aspm.c | 71 +++++++++++++++++++++++++++++++----------
 1 file changed, 54 insertions(+), 17 deletions(-)


---
Changes in v2:
- 1/4, 2/4: unchanged.
- 3/4: configure the link right away instead of waiting for a later
  pcie_config_aspm_path(), which is skipped under POLICY_POWERSAVE and
  POLICY_POWER_SUPERSAVE, and handle the blacklist path the same way;
  mark only L0s and L1 as enabled so that the L1 PM Substate bits do not
  end up in link->aspm_enabled when either end is not in D0; add
  Cc: stable.
- 4/4: new: skip links whose downstream bus has no function 0.
- 3/4, 4/4: note how they were exercised in a QEMU guest.
-- 
2.43.0


^ permalink raw reply	[flat|nested] 6+ messages in thread

* [PATCH v2 1/4] PCI/ASPM: Only update ASPM and CLKREQ bits in the saved LNKCTL
  2026-11-30  1:43 [PATCH v2 0/4] PCI/ASPM: keep software state and hardware in sync Yaxiong Tian
@ 2026-11-30  1:45 ` Yaxiong Tian
  2026-11-30  1:45 ` [PATCH v2 2/4] PCI/ASPM: Update saved LNKCTL for every function of the link Yaxiong Tian
                   ` (3 subsequent siblings)
  4 siblings, 0 replies; 6+ messages in thread
From: Yaxiong Tian @ 2026-11-30  1:45 UTC (permalink / raw)
  To: bhelgaas, mani, krishna.chundru, ilpo.jarvinen, lukas,
	sathyanarayanan.kuppuswamy, david.e.box, vidyas
  Cc: linux-pci, Yaxiong Tian

The comment above this code says it updates the ASPM and CLKREQ bits of
LNKCTL in the snapshot, and explains that CCC cannot change after
enumeration.  The code did not do that: it rebuilt the whole word from
the value read back from hardware, so every bit of cap[1] was replaced,
and the result of pcie_capability_read_word() was ignored.

An unusable read then destroys the snapshot in either of two ways.  A
failed read sets the value to zero, and pci_restore_pcie_state() writes
those zeroes back: ASPMC is cleared, so ASPM is disabled, and CCC is
cleared with nothing in the kernel programming it again after
enumeration (pcie_aspm_configure_common_clock() is only called from
pcie_aspm_cap_init()), so the link stays misconfigured until the next
enumeration.  A read that succeeds but returns PCI_ERROR_RESPONSE is
just as unusable, and worse: on an unreachable device the bridge
synthesizes all ones, so ASPMC would be recorded as L0s+L1 and CLKREQ_EN
as set, and the restore would force ASPM on.  In both cases
link->aspm_enabled still records the old state, so
pcie_config_aspm_link() does not correct the hardware either.

Such reads are reachable: pcie_config_aspm_link() runs for every link in
the system when the ASPM policy changes, including links whose devices
are already inaccessible, for example runtime suspended in D3cold.

Keep the snapshot untouched when the read is unusable, and merge only
the ASPM and CLKREQ bits into it when the read returns a usable value.

No functional change when the read returns a usable value.

Fixes: 6d4266675279 ("PCI/ASPM: Update save_state when configuration changes")
Signed-off-by: Yaxiong Tian <tianyaxiong@kylinos.cn>
---
 drivers/pci/pcie/aspm.c | 15 ++++++++++++---
 1 file changed, 12 insertions(+), 3 deletions(-)

diff --git a/drivers/pci/pcie/aspm.c b/drivers/pci/pcie/aspm.c
index b039b57f8466..f1ef3d41f089 100644
--- a/drivers/pci/pcie/aspm.c
+++ b/drivers/pci/pcie/aspm.c
@@ -331,7 +331,16 @@ static void pci_update_aspm_saved_state(struct pci_dev *dev)
 	if (!save_state)
 		return;
 
-	pcie_capability_read_word(dev, PCI_EXP_LNKCTL, &lnkctl);
+	/*
+	 * Both a failed read and a read that returns PCI_ERROR_RESPONSE
+	 * leave the snapshot unusable: the first sets *val to zero, the
+	 * second reports all ones from an unreachable device.  Writing
+	 * either back would clear ASPMC and CCC, or set ASPMC to L0s+L1
+	 * and CLKREQ_EN, when pci_restore_pcie_state() runs.
+	 */
+	if (pcie_capability_read_word(dev, PCI_EXP_LNKCTL, &lnkctl) ||
+	    PCI_POSSIBLE_ERROR(lnkctl))
+		return;
 
 	/*
 	 * Update ASPM and CLKREQ bits of LNKCTL in save_state. We only
@@ -339,11 +348,11 @@ static void pci_update_aspm_saved_state(struct pci_dev *dev)
 	 * change after being captured in save_state.
 	 */
 	aspm_ctl = lnkctl & (PCI_EXP_LNKCTL_ASPMC | PCI_EXP_LNKCTL_CLKREQ_EN);
-	lnkctl &= ~(PCI_EXP_LNKCTL_ASPMC | PCI_EXP_LNKCTL_CLKREQ_EN);
 
 	/* Depends on pci_save_pcie_state(): cap[1] is LNKCTL */
 	cap = (u16 *)&save_state->cap.data[0];
-	cap[1] = lnkctl | aspm_ctl;
+	cap[1] = (cap[1] & ~(PCI_EXP_LNKCTL_ASPMC |
+			     PCI_EXP_LNKCTL_CLKREQ_EN)) | aspm_ctl;
 }
 
 static void pcie_set_clkpm_nocheck(struct pcie_link_state *link, int enable)
-- 
2.43.0


^ permalink raw reply related	[flat|nested] 6+ messages in thread

* [PATCH v2 2/4] PCI/ASPM: Update saved LNKCTL for every function of the link
  2026-11-30  1:43 [PATCH v2 0/4] PCI/ASPM: keep software state and hardware in sync Yaxiong Tian
  2026-11-30  1:45 ` [PATCH v2 1/4] PCI/ASPM: Only update ASPM and CLKREQ bits in the saved LNKCTL Yaxiong Tian
@ 2026-11-30  1:45 ` Yaxiong Tian
  2026-11-30  1:45 ` [PATCH v2 3/4] PCI/ASPM: Disable ASPM left on by firmware after a HEADER quirk Yaxiong Tian
                   ` (2 subsequent siblings)
  4 siblings, 0 replies; 6+ messages in thread
From: Yaxiong Tian @ 2026-11-30  1:45 UTC (permalink / raw)
  To: bhelgaas, mani, krishna.chundru, ilpo.jarvinen, lukas,
	sathyanarayanan.kuppuswamy, david.e.box, vidyas
  Cc: linux-pci, Yaxiong Tian

pcie_config_aspm_link() programs PCI_EXP_LNKCTL on every function of the
downstream component, as recommended by PCIe r7.0, sec 7.5.3.7:

	list_for_each_entry(child, &linkbus->devices, bus_list)
		pcie_config_aspm_dev(child, dwstream);

Only the saved state of function 0 (link->downstream) is refreshed,
though.  The other functions keep whatever ASPMC value
pci_save_state() captured, so a driver that calls pci_restore_state()
from its slot_reset() hook restores the stale value on them.  After
that the functions of the link disagree on ASPM Control, which is the
state sec 7.5.3.7 asks software to avoid.

Refresh the saved state of each function right after its LNKCTL has
been programmed.  pci_save_aspm_l1ss_state() stays on function 0, since
the L1 PM Substates capability is only implemented there.

Found by inspection; not yet reproduced on hardware.

Fixes: 6d4266675279 ("PCI/ASPM: Update save_state when configuration changes")
Signed-off-by: Yaxiong Tian <tianyaxiong@kylinos.cn>
---
 drivers/pci/pcie/aspm.c | 14 +++++++++++---
 1 file changed, 11 insertions(+), 3 deletions(-)

diff --git a/drivers/pci/pcie/aspm.c b/drivers/pci/pcie/aspm.c
index f1ef3d41f089..6dbae7985bdc 100644
--- a/drivers/pci/pcie/aspm.c
+++ b/drivers/pci/pcie/aspm.c
@@ -1114,11 +1114,19 @@ static void pcie_config_aspm_link(struct pcie_link_state *link, u32 state)
 
 	link->aspm_enabled = state;
 
-	/* Update latest ASPM configuration in saved context */
+	/*
+	 * Update the saved LNKCTL of every function that was programmed
+	 * above; otherwise pci_restore_state() from a driver's slot_reset()
+	 * would put the other functions back to the stale ASPMC value and
+	 * leave the functions of the link disagreeing.
+	 */
+	pci_update_aspm_saved_state(parent);
+	list_for_each_entry(child, &linkbus->devices, bus_list)
+		pci_update_aspm_saved_state(child);
+
+	/* L1 PM Substates are only implemented in function 0 */
 	pci_save_aspm_l1ss_state(link->downstream);
-	pci_update_aspm_saved_state(link->downstream);
 	pci_save_aspm_l1ss_state(parent);
-	pci_update_aspm_saved_state(parent);
 }
 
 static void pcie_config_aspm_path(struct pcie_link_state *link)
-- 
2.43.0


^ permalink raw reply related	[flat|nested] 6+ messages in thread

* [PATCH v2 3/4] PCI/ASPM: Disable ASPM left on by firmware after a HEADER quirk
  2026-11-30  1:43 [PATCH v2 0/4] PCI/ASPM: keep software state and hardware in sync Yaxiong Tian
  2026-11-30  1:45 ` [PATCH v2 1/4] PCI/ASPM: Only update ASPM and CLKREQ bits in the saved LNKCTL Yaxiong Tian
  2026-11-30  1:45 ` [PATCH v2 2/4] PCI/ASPM: Update saved LNKCTL for every function of the link Yaxiong Tian
@ 2026-11-30  1:45 ` Yaxiong Tian
  2026-11-30  1:45 ` [PATCH v2 4/4] PCI/ASPM: Skip links whose downstream bus has no function 0 Yaxiong Tian
  2026-11-30  1:54 ` [PATCH v2 0/4] PCI/ASPM: keep software state and hardware in sync sashiko-bot
  4 siblings, 0 replies; 6+ messages in thread
From: Yaxiong Tian @ 2026-11-30  1:45 UTC (permalink / raw)
  To: bhelgaas, mani, krishna.chundru, ilpo.jarvinen, lukas,
	sathyanarayanan.kuppuswamy, david.e.box, vidyas
  Cc: linux-pci, Yaxiong Tian, stable

"PCI/ASPM: Convert quirks to override advertised link states" turned
the ASPM quirks into HEADER quirks that clear the cached L0s/L1 support
instead of calling pci_disable_link_state(), so that ASPM is never
enabled on the affected devices in the first place.

pcie_aspm_cap_init() returns before reading LNKCTL when the cached L0s
and L1 support of the two ends has no overlap.  A HEADER quirk can empty
that cache on hardware that does implement ASPM, and firmware may well
have enabled ASPM on it already, so the link is left running with ASPM
on while the kernel believes it is off: link->aspm_enabled stays zero,
so pcie_config_aspm_link() sees the link as already being in the
requested (disabled) state and returns without touching the hardware.
No later request can correct it either, for the same reason.  This
affects [1b21:1080], whose quirk was converted, and [1957:0451], whose
Root Port quirk was added by "Avoid L0s and L1 on Freescale [1957:0451]
Root Ports", under every policy including the default one.

The blacklist path has the same shape: it marks L0s and L1 as enabled
and all states as disabled so that a later pcie_config_aspm_path()
clears LNKCTL.ASPMC.  That later call is skipped under
POLICY_POWERSAVE and POLICY_POWER_SUPERSAVE, and
pcie_aspm_powersave_config_link() only runs when a downstream device
is enabled, so a blacklisted link can keep the firmware settings as
well.  Handle both cases in one place: mark the states and configure
the link right away, without waiting for a pcie_config_aspm_path() that
may never come.

L0s and L1 suffice as the "enabled" marker, whose only purpose is to
keep pcie_config_aspm_link() from returning early.  The blacklist path
used to mark PCIE_LINK_STATE_ASPM_ALL, which includes the L1 PM
Substates; when either end is not in D0, pcie_config_aspm_link() copies
those bits from link->aspm_enabled into the state it applies and
records, leaving pcie_aspm_enabled() to report L1.1 and L1.2 as enabled
on a link that has just been switched off.

The link is configured even when aspm_disabled is set: the FINAL quirks
went through pci_disable_link_state(), which disables ASPM regardless,
and this is meant to restore that behaviour.

A link whose hardware really lacks ASPM support is not affected in
practice: it never advertises ASPM, so clearing ASPMC is harmless.

The L1 PM Substate enables are intentionally left alone.  They cannot
take effect while L1 is disabled.

Found by inspection.  In a QEMU guest with the same HEADER quirk applied
to the emulated root port, the kernel issues these LNKCTL writes only
with this patch; not yet reproduced on hardware.

Fixes: 30579eebba6a ("PCI/ASPM: Convert quirks to override advertised link states")
Cc: stable@vger.kernel.org
Signed-off-by: Yaxiong Tian <tianyaxiong@kylinos.cn>
---
 drivers/pci/pcie/aspm.c | 33 ++++++++++++++++++++++-----------
 1 file changed, 22 insertions(+), 11 deletions(-)

diff --git a/drivers/pci/pcie/aspm.c b/drivers/pci/pcie/aspm.c
index 6dbae7985bdc..2ce8e2df1063 100644
--- a/drivers/pci/pcie/aspm.c
+++ b/drivers/pci/pcie/aspm.c
@@ -940,6 +940,8 @@ static void pcie_aspm_override_default_link_state(struct pcie_link_state *link)
 	}
 }
 
+static void pcie_config_aspm_link(struct pcie_link_state *link, u32 state);
+
 static void pcie_aspm_cap_init(struct pcie_link_state *link, int blacklist)
 {
 	struct pci_dev *child = link->downstream, *parent = link->pdev;
@@ -947,20 +949,29 @@ static void pcie_aspm_cap_init(struct pcie_link_state *link, int blacklist)
 	u16 parent_lnkctl, child_lnkctl;
 	struct pci_bus *linkbus = parent->subordinate;
 
-	if (blacklist) {
-		/* Set enabled/disable so that we will disable ASPM later */
-		link->aspm_enabled = PCIE_LINK_STATE_ASPM_ALL;
-		link->aspm_disable = PCIE_LINK_STATE_ASPM_ALL;
-		return;
-	}
-
 	/*
-	 * If ASPM not supported, don't mess with the clocks and link,
-	 * bail out now.
+	 * Nothing to enable if ASPM is blacklisted, or if the two ends do
+	 * not share L0s or L1 support.  Mark the states disabled and
+	 * configure the link right away, so LNKCTL.ASPMC ends up cleared
+	 * instead of waiting for a pcie_config_aspm_path() that may never
+	 * come: the cached support may have been removed by a HEADER quirk
+	 * while firmware left ASPM enabled on hardware that does implement
+	 * it.
+	 *
+	 * L0s and L1 are only marked as enabled so that
+	 * pcie_config_aspm_link() does not find the link in the requested
+	 * state.  Leave the L1 PM Substates out: when either end is not in
+	 * D0, it copies their bits from link->aspm_enabled into the state it
+	 * applies and records.
 	 */
-	if (!(parent->aspm_l0s_support && child->aspm_l0s_support) &&
-	    !(parent->aspm_l1_support && child->aspm_l1_support))
+	if (blacklist ||
+	    (!(parent->aspm_l0s_support && child->aspm_l0s_support) &&
+	     !(parent->aspm_l1_support && child->aspm_l1_support))) {
+		link->aspm_enabled = PCIE_LINK_STATE_L0S | PCIE_LINK_STATE_L1;
+		link->aspm_disable = PCIE_LINK_STATE_ASPM_ALL;
+		pcie_config_aspm_link(link, 0);
 		return;
+	}
 
 	/* Configure common clock before checking latencies */
 	pcie_aspm_configure_common_clock(link);
-- 
2.43.0


^ permalink raw reply related	[flat|nested] 6+ messages in thread

* [PATCH v2 4/4] PCI/ASPM: Skip links whose downstream bus has no function 0
  2026-11-30  1:43 [PATCH v2 0/4] PCI/ASPM: keep software state and hardware in sync Yaxiong Tian
                   ` (2 preceding siblings ...)
  2026-11-30  1:45 ` [PATCH v2 3/4] PCI/ASPM: Disable ASPM left on by firmware after a HEADER quirk Yaxiong Tian
@ 2026-11-30  1:45 ` Yaxiong Tian
  2026-11-30  1:54 ` [PATCH v2 0/4] PCI/ASPM: keep software state and hardware in sync sashiko-bot
  4 siblings, 0 replies; 6+ messages in thread
From: Yaxiong Tian @ 2026-11-30  1:45 UTC (permalink / raw)
  To: bhelgaas, mani, krishna.chundru, ilpo.jarvinen, lukas,
	sathyanarayanan.kuppuswamy, david.e.box, vidyas
  Cc: linux-pci, Yaxiong Tian

alloc_pcie_link_state() stores pci_function_0() in link->downstream
without checking the result.  pci_function_0() returns NULL when the
subordinate bus has no function 0.  pci_scan_slot() normally prevents
that by stopping the slot scan, but it continues when a hypervisor
passes through individual PCI functions (s390, loongarch, Jailhouse).
pcie_aspm_cap_init() then dereferences the NULL child in the L0s/L1
support test, and pcie_config_aspm_link() would do the same in the D0
check.

A link without function 0 cannot be managed anyway: pci_function_0() is
also used to find the device that implements the L1 PM Substates
capability.  Refuse to create the link state, so every later path exits
through its existing !link check.

Found by Sashiko, and reproduced in a QEMU guest (with
hypervisor_isolated_pci_functions() forced to true) on a slot that has
only function 1: without this patch the kernel oopses in
pcie_config_aspm_link().  Not reproduced on hardware.

Fixes: 3bd7db63a841 ("PCI/ASPM: Always set link->downstream to avoid NULL dereference on remove")
Signed-off-by: Yaxiong Tian <tianyaxiong@kylinos.cn>
---
 drivers/pci/pcie/aspm.c | 9 +++++++++
 1 file changed, 9 insertions(+)

diff --git a/drivers/pci/pcie/aspm.c b/drivers/pci/pcie/aspm.c
index 2ce8e2df1063..e7cbce8ae70f 100644
--- a/drivers/pci/pcie/aspm.c
+++ b/drivers/pci/pcie/aspm.c
@@ -1200,6 +1200,15 @@ static struct pcie_link_state *alloc_pcie_link_state(struct pci_dev *pdev)
 	INIT_LIST_HEAD(&link->sibling);
 	link->pdev = pdev;
 	link->downstream = pci_function_0(pdev->subordinate);
+	if (!link->downstream) {
+		/*
+		 * Function 0 is missing, so there is no downstream device to
+		 * configure ASPM on and no device that implements the L1 PM
+		 * Substates capability.  Leave the link alone.
+		 */
+		kfree(link);
+		return NULL;
+	}
 
 	/*
 	 * Root Ports and PCI/PCI-X to PCIe Bridges are roots of PCIe
-- 
2.43.0


^ permalink raw reply related	[flat|nested] 6+ messages in thread

* Re: [PATCH v2 0/4] PCI/ASPM: keep software state and hardware in sync
  2026-11-30  1:43 [PATCH v2 0/4] PCI/ASPM: keep software state and hardware in sync Yaxiong Tian
                   ` (3 preceding siblings ...)
  2026-11-30  1:45 ` [PATCH v2 4/4] PCI/ASPM: Skip links whose downstream bus has no function 0 Yaxiong Tian
@ 2026-11-30  1:54 ` sashiko-bot
  4 siblings, 0 replies; 6+ messages in thread
From: sashiko-bot @ 2026-11-30  1:54 UTC (permalink / raw)
  To: Yaxiong Tian; +Cc: linux-pci

Sashiko has reviewed this 4-patch series and found no issues.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261130014347.164678-1-tianyaxiong@kylinos.cn

^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2026-11-30  1:54 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-30  1:43 [PATCH v2 0/4] PCI/ASPM: keep software state and hardware in sync Yaxiong Tian
2026-11-30  1:45 ` [PATCH v2 1/4] PCI/ASPM: Only update ASPM and CLKREQ bits in the saved LNKCTL Yaxiong Tian
2026-11-30  1:45 ` [PATCH v2 2/4] PCI/ASPM: Update saved LNKCTL for every function of the link Yaxiong Tian
2026-11-30  1:45 ` [PATCH v2 3/4] PCI/ASPM: Disable ASPM left on by firmware after a HEADER quirk Yaxiong Tian
2026-11-30  1:45 ` [PATCH v2 4/4] PCI/ASPM: Skip links whose downstream bus has no function 0 Yaxiong Tian
2026-11-30  1:54 ` [PATCH v2 0/4] PCI/ASPM: keep software state and hardware in sync sashiko-bot

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox