Linux PCI subsystem development
 help / color / mirror / Atom feed
* Re: [PATCH v1] PCI: Make PCIe link wait timeout configurable via pcie_link_timeout=
  2026-11-21 11:08 [PATCH v1] PCI: Make PCIe link wait timeout configurable via pcie_link_timeout= Hongtao Zhang
@ 2026-11-21 11:08 ` sashiko-bot
  0 siblings, 0 replies; 2+ messages in thread
From: sashiko-bot @ 2026-11-21 11:08 UTC (permalink / raw)
  To: Hongtao Zhang, linux-pci

Thank you for your contribution! Sashiko AI review found 3 potential
issues to consider:
- [Medium] PCI/DPC: 4000 ms timeout in pci_dpc_recovered() expires
  before link wait
- [Low] PCI: unnecessary EXPORT_SYMBOL_GPL and missing __ro_after_init
  on timeout
- [Low] PCI: inaccurate kernel-parameters.txt note on blind-sleep ports
It also noticed 1 related pre-existing issue this patch does not
introduce:
- [High] PCI: missing PCI_ERROR_RESPONSE check in
  pcie_wait_for_link_status() [1]
--

commit 79c6602cfac02cd18ceb2cc8a36f64252285fc19
Author: Hongtao Zhang <zhanghongtao35@huawei.com>

PCI: Make PCIe link wait timeout configurable via pcie_link_timeout=

This commit makes the PCIe link training and link state transition wait
timeout configurable at boot via the pcie_link_timeout= kernel parameter,
allowing values between 1000 ms and 10000 ms.

> diff --git a/Documentation/admin-guide/kernel-parameters.txt b/Documentation/admin-guide/kernel-parameters.txt
> index 396d196d6ea00..15d6507de968d 100644
> --- a/Documentation/admin-guide/kernel-parameters.txt
> +++ b/Documentation/admin-guide/kernel-parameters.txt
> @@ -5304,6 +5304,20 @@ Kernel parameters
>  		force	Enable ASPM even on devices that claim not to support it.
>  			WARNING: Forcing ASPM on may cause system lockups.
>  
> +	pcie_link_timeout=
> +			[PCIE] Timeout in milliseconds to wait for PCIe link
> +			training and link state (up/down) transitions, e.g.
> +			during hot-plug power-on, error recovery resets and
> +			runtime resume.
> +			Some devices, or specific device/board combinations,
> +			may need more than the PCIe spec's 1 sec to train the
> +			link; use this to raise the wait, e.g.
> +			pcie_link_timeout=3000
> +			Range: [1000, 10000] ms. Values out of range are
> +			ignored with a warning and the 1000 ms default is
> +			used. Only affects failure detection latency; healthy
> +			links return as soon as training completes.

[Severity: Low]
Is it accurate that this parameter only affects failure detection latency?
In pcie_wait_for_link_delay(), ports without link_active_reporting
unconditionally sleep for pcie_link_retrain_timeout_ms + delay even when the
link is healthy.

> diff --git a/drivers/pci/pci.c b/drivers/pci/pci.c
> index 7c6f2aba81443..a5996beeaebbd 100644
> --- a/drivers/pci/pci.c
> +++ b/drivers/pci/pci.c
> @@ -159,6 +159,25 @@ static bool pci_bridge_d3_disable;
>  /* Force bridge_d3 for all PCIe ports */
>  static bool pci_bridge_d3_force;
>  
> +/*
> + * Effective timeout (ms) for waiting on PCIe link training / state changes.
> + * Defaults to the spec-mandated 1 sec floor.  Devices or device/board
> + * combinations that legitimately need longer than 1 sec to train the link
> + * can raise this at boot via "pcie_link_timeout=<ms>".
> + */
> +unsigned int pcie_link_retrain_timeout_ms = PCIE_LINK_RETRAIN_TIMEOUT_MS;
> +EXPORT_SYMBOL_GPL(pcie_link_retrain_timeout_ms);

[Severity: Low]
pcie_link_retrain_timeout_ms is only declared in drivers/pci/pci.h, only
consumed by built-in code (drivers/pci/pci.c and drivers/pci/pcie/dpc.c),
and only modified during __init.  Should EXPORT_SYMBOL_GPL() be dropped and
the variable marked __ro_after_init?

[ ... ]

> @@ -4624,7 +4674,7 @@ static int pcie_wait_for_link_status(struct pci_dev *pdev,
>  	lnksta_mask = use_lt ? PCI_EXP_LNKSTA_LT : PCI_EXP_LNKSTA_DLLLA;
>  	lnksta_match = active ? lnksta_mask : 0;
>  
> -	end_jiffies = jiffies + msecs_to_jiffies(PCIE_LINK_RETRAIN_TIMEOUT_MS);
> +	end_jiffies = jiffies + msecs_to_jiffies(pcie_link_retrain_timeout_ms);
>  	do {
>  		pcie_capability_read_word(pdev, PCI_EXP_LNKSTA, &lnksta);
>  		if ((lnksta & lnksta_mask) == lnksta_match)

[Severity: High]
This problem wasn't introduced by this patch, but can
pcie_wait_for_link_status() misinterpret a disconnected device [1] as
an active link because lnksta is not checked for PCI_ERROR_RESPONSE
(0xffff)?

When a config read from a removed or unresponsive device populates lnksta
with 0xffff, (lnksta & lnksta_mask) == lnksta_match immediately succeeds
when active is true (falsely reporting that link training succeeded) and
never matches when active is false (blocking for the full
pcie_link_retrain_timeout_ms timeout).

[ ... ]

> diff --git a/drivers/pci/pcie/dpc.c b/drivers/pci/pcie/dpc.c
> index 2b779bd1d861b..ef04532d9c8ab 100644
> --- a/drivers/pci/pcie/dpc.c
> +++ b/drivers/pci/pcie/dpc.c
> @@ -163,7 +163,8 @@ pci_ers_result_t dpc_reset_link(struct pci_dev *pdev)
>  	 * to allow the Port to leave DPC.
>  	 */
>  	if (!pcie_wait_for_link(pdev, false))
> -		pci_info(pdev, "Data Link Layer Link Active not cleared in 1000 msec\n");
> +		pci_info(pdev, "Data Link Layer Link Active not cleared in %u msec\n",
> +			 pcie_link_retrain_timeout_ms);

[Severity: Medium]
Can pci_dpc_recovered() time out prematurely during DPC recovery when
pcie_link_timeout= is raised?

dpc_reset_link() waits up to pcie_link_retrain_timeout_ms in
pcie_wait_for_link(pdev, false) and again in
pci_bridge_wait_for_secondary_bus() before setting PCI_DPC_RECOVERED and
waking dpc_completed_waitqueue, whereas pci_dpc_recovered() still uses a
hardcoded 4000 ms timeout:

drivers/pci/pcie/dpc.c:pci_dpc_recovered() {
	...
	wait_event_timeout(dpc_completed_waitqueue, dpc_completed(pdev),
			   msecs_to_jiffies(4000));

	return test_and_clear_bit(PCI_DPC_RECOVERED, &pdev->priv_flags);
}

When pcie_link_timeout= is set above 4000 ms (or 2000 ms on ports without
link_active_reporting) and DPC link recovery takes longer than 4000 ms,
pci_dpc_recovered() returns false to pciehp_ist() while dpc_reset_link() is
still running.  pciehp_ist() then falls through to
pciehp_handle_presence_or_link_change() and tears down the hotplug slot
mid-recovery, and dpc_reset_link() later leaves a stale PCI_DPC_RECOVERED
bit set that causes pciehp_ist() to ignore the next hotplug event.

[1] https://sashiko.dev/#/bug/linux-139f3acd-7e0a-4e79-85a1-afc0e1e4f511

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261121110806.1158344-1-zhanghongtao35@huawei.com?part=1

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

* [PATCH v1] PCI: Make PCIe link wait timeout configurable via pcie_link_timeout=
@ 2026-11-21 11:08 Hongtao Zhang
  2026-11-21 11:08 ` sashiko-bot
  0 siblings, 1 reply; 2+ messages in thread
From: Hongtao Zhang @ 2026-11-21 11:08 UTC (permalink / raw)
  To: linux-pci, linux-kernel
  Cc: bhelgaas, ilpo.jarvinen, mani, sathyanarayanan.kuppuswamy,
	liuyongqiang13

PCIE_LINK_RETRAIN_TIMEOUT_MS fixes the timeout for waiting on PCIe
link training and link state transitions at 1000 ms, matching the
PCIe spec's minimum recommendation for declaring a non-responsive
device broken.

As hardware evolves, some combinations of newer devices and newer
boards may need more than 1 sec to bring the link up. Rather than
changing the compiled-in constant for each such case, make the
timeout runtime-configurable: keep 1000 ms as the default and allow
raising it at boot via "pcie_link_timeout=<ms>", range [1000, 10000].
This gives platform and device bring-up a flexible knob to
accommodate slower link training without any code change, while
out-of-range values fall back to the spec default. The ceiling keeps
failure detection, bus/slot lock hold times, and the blind-sleep path
on ports without Link Active Reporting well below upper-layer error
handling thresholds.

Accordingly, adjust the two DPC log messages that hardcoded "1000
msec" to print the actually configured timeout.

Signed-off-by: Hongtao Zhang <zhanghongtao35@huawei.com>
---
 .../admin-guide/kernel-parameters.txt         | 14 +++++
 drivers/pci/pci.c                             | 61 +++++++++++++++++--
 drivers/pci/pci.h                             | 10 +++
 drivers/pci/pcie/dpc.c                        |  6 +-
 4 files changed, 84 insertions(+), 7 deletions(-)

diff --git a/Documentation/admin-guide/kernel-parameters.txt b/Documentation/admin-guide/kernel-parameters.txt
index 0b3c140babd94..cde4faa5e3187 100644
--- a/Documentation/admin-guide/kernel-parameters.txt
+++ b/Documentation/admin-guide/kernel-parameters.txt
@@ -5304,6 +5304,20 @@ Kernel parameters
 		force	Enable ASPM even on devices that claim not to support it.
 			WARNING: Forcing ASPM on may cause system lockups.
 
+	pcie_link_timeout=
+			[PCIE] Timeout in milliseconds to wait for PCIe link
+			training and link state (up/down) transitions, e.g.
+			during hot-plug power-on, error recovery resets and
+			runtime resume.
+			Some devices, or specific device/board combinations,
+			may need more than the PCIe spec's 1 sec to train the
+			link; use this to raise the wait, e.g.
+			pcie_link_timeout=3000
+			Range: [1000, 10000] ms. Values out of range are
+			ignored with a warning and the 1000 ms default is
+			used. Only affects failure detection latency; healthy
+			links return as soon as training completes.
+
 	pcie_ports=	[PCIE] PCIe port services handling:
 		native	Use native PCIe services (PME, AER, DPC, PCIe hotplug)
 			even if the platform doesn't give the OS permission to
diff --git a/drivers/pci/pci.c b/drivers/pci/pci.c
index 7c6f2aba81443..a5996beeaebbd 100644
--- a/drivers/pci/pci.c
+++ b/drivers/pci/pci.c
@@ -159,6 +159,25 @@ static bool pci_bridge_d3_disable;
 /* Force bridge_d3 for all PCIe ports */
 static bool pci_bridge_d3_force;
 
+/*
+ * Effective timeout (ms) for waiting on PCIe link training / state changes.
+ * Defaults to the spec-mandated 1 sec floor.  Devices or device/board
+ * combinations that legitimately need longer than 1 sec to train the link
+ * can raise this at boot via "pcie_link_timeout=<ms>".
+ */
+unsigned int pcie_link_retrain_timeout_ms = PCIE_LINK_RETRAIN_TIMEOUT_MS;
+EXPORT_SYMBOL_GPL(pcie_link_retrain_timeout_ms);
+
+/*
+ * Upper bound for pcie_link_timeout=.  Generous headroom for slower link
+ * training, yet far below the ~30 sec abort threshold used by block/NVMe
+ * error handling so a single link wait cannot cascade into abort/reset
+ * loops above us.  On ports without Link Active Reporting the wait is a
+ * blind sleep that runs even for healthy devices, so the ceiling must
+ * bound that cost as well.
+ */
+#define PCIE_LINK_RETRAIN_TIMEOUT_MAX_MS	10000
+
 static int __init pcie_port_pm_setup(char *str)
 {
 	if (!strcmp(str, "off"))
@@ -169,6 +188,37 @@ static int __init pcie_port_pm_setup(char *str)
 }
 __setup("pcie_port_pm=", pcie_port_pm_setup);
 
+static int __init pcie_link_timeout_setup(char *str)
+{
+	unsigned int timeout;
+
+	if (kstrtouint(str, 10, &timeout)) {
+		pr_warn("PCIe: invalid pcie_link_timeout=%s, using default %u ms\n",
+			str, PCIE_LINK_RETRAIN_TIMEOUT_MS);
+		return 1;
+	}
+
+	/*
+	 * Floor: PCIe spec requires at least 1 sec before declaring a device
+	 * that has not responded as broken.  Ceiling: keep a single link wait
+	 * well below upper-layer timeouts so failure detection stays bounded
+	 * (see PCIE_LINK_RETRAIN_TIMEOUT_MAX_MS).
+	 */
+	if (timeout < PCIE_LINK_RETRAIN_TIMEOUT_MS ||
+	    timeout > PCIE_LINK_RETRAIN_TIMEOUT_MAX_MS) {
+		pr_warn("PCIe: pcie_link_timeout=%u out of range [%u, %u] ms, using default %u ms\n",
+			timeout, (unsigned int)PCIE_LINK_RETRAIN_TIMEOUT_MS,
+			(unsigned int)PCIE_LINK_RETRAIN_TIMEOUT_MAX_MS,
+			PCIE_LINK_RETRAIN_TIMEOUT_MS);
+		return 1;
+	}
+
+	pcie_link_retrain_timeout_ms = timeout;
+	pr_info("PCIe: link wait timeout set to %u ms\n", timeout);
+	return 1;
+}
+__setup("pcie_link_timeout=", pcie_link_timeout_setup);
+
 /**
  * pci_bus_max_busnr - returns maximum PCI bus number of given bus' children
  * @bus: pointer to PCI bus structure to search
@@ -4612,7 +4662,7 @@ static int pci_pm_reset(struct pci_dev *dev, bool probe)
  * @active: Waiting for active or inactive?
  *
  * Return 0 if successful, or -ETIMEDOUT if status has not changed within
- * PCIE_LINK_RETRAIN_TIMEOUT_MS milliseconds.
+ * pcie_link_retrain_timeout_ms milliseconds.
  */
 static int pcie_wait_for_link_status(struct pci_dev *pdev,
 				     bool use_lt, bool active)
@@ -4624,7 +4674,7 @@ static int pcie_wait_for_link_status(struct pci_dev *pdev,
 	lnksta_mask = use_lt ? PCI_EXP_LNKSTA_LT : PCI_EXP_LNKSTA_DLLLA;
 	lnksta_match = active ? lnksta_mask : 0;
 
-	end_jiffies = jiffies + msecs_to_jiffies(PCIE_LINK_RETRAIN_TIMEOUT_MS);
+	end_jiffies = jiffies + msecs_to_jiffies(pcie_link_retrain_timeout_ms);
 	do {
 		pcie_capability_read_word(pdev, PCI_EXP_LNKSTA, &lnksta);
 		if ((lnksta & lnksta_mask) == lnksta_match)
@@ -4650,7 +4700,7 @@ static int pcie_wait_for_link_status(struct pci_dev *pdev,
  * bit is valid.
  *
  * Return 0 if successful, or -ETIMEDOUT if training has not completed
- * within PCIE_LINK_RETRAIN_TIMEOUT_MS milliseconds.
+ * within pcie_link_retrain_timeout_ms milliseconds.
  */
 int pcie_retrain_link(struct pci_dev *pdev, bool use_lt)
 {
@@ -4715,10 +4765,11 @@ static bool pcie_wait_for_link_delay(struct pci_dev *pdev, bool active,
 
 	/*
 	 * Some controllers might not implement link active reporting. In this
-	 * case, we wait for 1000 ms + any delay requested by the caller.
+	 * case, we wait for pcie_link_retrain_timeout_ms + any delay requested
+	 * by the caller.
 	 */
 	if (!pdev->link_active_reporting) {
-		msleep(PCIE_LINK_RETRAIN_TIMEOUT_MS + delay);
+		msleep(pcie_link_retrain_timeout_ms + delay);
 		return true;
 	}
 
diff --git a/drivers/pci/pci.h b/drivers/pci/pci.h
index 77efa4c5c960e..e060e129f02a0 100644
--- a/drivers/pci/pci.h
+++ b/drivers/pci/pci.h
@@ -20,8 +20,18 @@ struct pcie_tlp_log;
 
 #define PCI_VSEC_ID_INTEL_TBT	0x1234	/* Thunderbolt */
 
+/*
+ * Default timeout for link retraining / link state transitions.  PCIe spec
+ * says software must allow at least 1 sec before declaring a device that
+ * has not responded as broken; keep that as the floor.  The effective value
+ * can be raised at boot via the "pcie_link_timeout=" parameter for devices
+ * that need longer to bring the link up.
+ */
 #define PCIE_LINK_RETRAIN_TIMEOUT_MS	1000
 
+/* Effective, boot-configurable link wait timeout in milliseconds. */
+extern unsigned int pcie_link_retrain_timeout_ms;
+
 /*
  * Power stable to PERST# inactive.
  *
diff --git a/drivers/pci/pcie/dpc.c b/drivers/pci/pcie/dpc.c
index 2b779bd1d861b..ef04532d9c8ab 100644
--- a/drivers/pci/pcie/dpc.c
+++ b/drivers/pci/pcie/dpc.c
@@ -163,7 +163,8 @@ pci_ers_result_t dpc_reset_link(struct pci_dev *pdev)
 	 * to allow the Port to leave DPC.
 	 */
 	if (!pcie_wait_for_link(pdev, false))
-		pci_info(pdev, "Data Link Layer Link Active not cleared in 1000 msec\n");
+		pci_info(pdev, "Data Link Layer Link Active not cleared in %u msec\n",
+			 pcie_link_retrain_timeout_ms);
 
 	if (pdev->dpc_rp_extensions && dpc_wait_rp_inactive(pdev)) {
 		clear_bit(PCI_DPC_RECOVERED, &pdev->priv_flags);
@@ -328,7 +329,8 @@ static void pci_clear_surpdn_errors(struct pci_dev *pdev)
 static void dpc_handle_surprise_removal(struct pci_dev *pdev)
 {
 	if (!pcie_wait_for_link(pdev, false)) {
-		pci_info(pdev, "Data Link Layer Link Active not cleared in 1000 msec\n");
+		pci_info(pdev, "Data Link Layer Link Active not cleared in %u msec\n",
+			 pcie_link_retrain_timeout_ms);
 		goto out;
 	}
 
-- 
2.43.0


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

end of thread, other threads:[~2026-11-21 11:08 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-21 11:08 [PATCH v1] PCI: Make PCIe link wait timeout configurable via pcie_link_timeout= Hongtao Zhang
2026-11-21 11:08 ` sashiko-bot

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