Skip to contentSkip to conversation

PCI: Add Lane Margining at the Receiver debugfs interface

PATCH In review Priyank Rathod 3 participants 8 messages Nov 25

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

v8 cover PCI: Add Lane Margining at the Receiver debugfs interface

Lane Margining at the Receiver lets software move the sampling point of a receiver in time or in voltage while the link stays up, and read back the errors the receiver sees, to find out how much margin a link running at 16.0 GT/s or faster has. pcilmr in pciutils does this today by writing the capability registers directly from user space.

This series adds a debugfs interface for it. The kernel sets the link up for margining and puts it back afterwards:

- ASPM is temporarily inhibited for the session with an internal reference-counted helper in the ASPM driver, Hardware Autonomous Width/Speed Disable are set on both ends, and both ends are kept runtime resumed. All of this is undone when the session ends or either end of the link is removed.
- Before each margining command, the link is checked against the state the session started in.
- User space selects the receiver and the steps; the kernel does not run sweeps or interpret results.

Patch 1 adds the internal pcie_aspm_inhibit() helper in drivers/pci/pcie/aspm.c, patch 2 adds the LMR debugfs interface and its documentation, and patch 3 adds a kselftest.

The only PCI core changes outside drivers/pci/pcie/ are the init/exit hooks in probe.c and remove.c and internal declarations in drivers/pci/pci.h; drivers/pci/pci.c, drivers/pci/pci-driver.c, and include/linux/pci.h are not changed.

---
Note on the v4, v5, and v6 reposts on Oct 6:
Apologies for posting v4, v5, and v6 within a four-hour window. The root cause was reacting immediately to each incremental automated sashiko-bot report on the mailing list instead of running the full review pass locally first and batching feedback into a single revision:

1. v4 (16:09 UTC) reworked the series into two patches, dropping pcie_get_link_endpoints() and pci_aspm_inhibit() per Ilpo's review.
2. v5 (17:35 UTC) was sent after sashiko-bot flagged 'up=$([ "$rx" = 6 ] && echo "$dev" || echo "$partner")' in pcie_lmr.sh, mistaking the PCIe Upstream Port (receiver 6, the child device below the Downstream Port where aspm.c attaches the sysfs 'link/' directory via pcie_aspm_get_link()) for the Downstream Port. Although the logic in v4 was right, v5 renamed 'up' to 'child' and added a comment so human reviewers would not hit the same ambiguity.
3. v6 (20:59 UTC) was sent after sashiko-bot flagged on v5 that pci_lmr_reboot_notify() used mutex_trylock(&port->lock) under pci_lmr_mutex and could skip resetting a port if a debugfs command was in flight during reboot/kexec. v6 replaced that with a kref on struct pci_lmr_port and pci_lmr_end_sessions() so reboot/kexec waits for p->lock outside pci_lmr_mutex.

Changes in v8:
- Rebase onto v7.4-rc1.
- Replace pci_force_enable_link_state() and the bitmask return value of pcie_aspm_enabled() (both deferred before v7.4-rc1) with an internal reference-counted pcie_aspm_inhibit() helper in drivers/pci/pcie/aspm.c.
- Hold port->lock continuously across claiming and activating a session
in pci_lmr_start(), recheck pci_lmr_rebooting under pci_lmr_mutex,
wait on starting sessions in pci_lmr_find_session(), and drop runtime
PM and partner references outside the device locks in pci_lmr_stop().
- Preserve unread too_many_errors status in PCI_LMR_LANE_STS when pci_lmr_sync_lane() resets software offsets for a lane.
- Link to v7: https://lore.kernel.org/r/[email protected]

Changes in v7:
- Match PCI_LMR_LANE_RX | PCI_LMR_LANE_TYPE | PCI_LMR_LANE_PAYLOAD in pci_lmr_no_cmd() instead of U16_MAX, and format the error count in status_show() with %u and an explicit unsigned int cast.
- In pcie_lmr.sh, check all unreachable receiver numbers for the port
type and, on a Downstream Port, discover any retimers on the link
(receivers 2..5 via LNKSTA2), test caps and a timing step 1 -> 0
round-trip on each present retimer receiver, and verify that absent
retimer receivers are rejected with -EINVAL.
- Link to v6: https://lore.kernel.org/r/[email protected]

Changes in v6:
- Add a kref on struct pci_lmr_port and use pci_lmr_end_sessions() in pci_lmr_reboot_notify() so reboot/kexec waits for in-flight commands
outside pci_lmr_mutex (preserving the port->lock -> pci_lmr_mutex lock
order) instead of skipping ports on mutex_trylock() failure.
- Link to v5: https://lore.kernel.org/r/[email protected]

Changes in v5:
- Clarify in pcie_lmr.sh why the sysfs 'link/' check targets receiver 6
(and rename 'up' to 'child'): in PCIe terminology the Upstream Port
(receiver 6) is the child device below the Downstream Port, and in drivers/pci/pcie/aspm.c aspm_ctrl_attrs_are_visible() looks up the
link via pcie_aspm_get_link(pdev) -> pci_upstream_bridge(pdev)->link_state,
so '/sys/bus/pci/devices/<dev>/link/' is attached to the child device
below the Downstream Port, not to the Downstream Port itself.
- Link to v4: https://lore.kernel.org/r/[email protected]

Changes in v4:
- Drop the exported pcie_get_link_endpoints(); the partner lookup is
private to margin.c (Ilpo).
- Drop pci_aspm_inhibit() and all aspm.c changes. Use
pcie_aspm_enabled(), pci_disable_link_state() and pci_force_enable_link_state(), call neither when no ASPM state is
enabled, and restore Clock PM when CLKREQ# was enabled. ASPM Control
is checked on both ends before each command. The remaining
differences after a session are documented.
- Remove the MSampleMultipleReceivers capability bit.
- One receiver per session, selected with a port-level 'receiver'
file. A session starts with receiver 1 on a Downstream Port and 6
below it, as pcilmr does; receiver 0 is not used for margining.
- Add a per-lane 'status' file with the step response and error count,
and a per-port 'port' file with the Margining Port bits. Stop writing Margining Port Status.
- Locking: take both device locks with pci_dev_trylock() only while a
session starts or stops, require both ends to be added, and make no
runtime PM calls under the LMR locks.
- End sessions from pci_lmr_exit() for either end of the link.
- Commands fail with -EIO after a system suspend.
- Move debugfs to pcie_lmr_<device> at the debugfs root.
- Keep per-device state in margin.c instead of struct pci_dev.
- Split the selftest into its own patch, rename it pcie_lmr, use KTAP,
and only start a session on the port named by PCIE_LMR_DEV.
- Refer to registers by name instead of specification section numbers.
- Link to v3: https://lore.kernel.org/r/[email protected]

Changes in v3:
- Link to v2: https://lore.kernel.org/r/[email protected]

Changes in v2:
- Link to v1: https://lore.kernel.org/r/[email protected]

---
Priyank Rathod (3):
PCI/ASPM: Add internal pcie_aspm_inhibit() helper
PCI/LMR: Add Lane Margining at the Receiver debugfs interface selftests/pcie_lmr: Add tests for the Lane Margining debugfs interface

 Documentation/PCI/index.rst                  |    1 +
 Documentation/PCI/pcie-lmr.rst               |  155 +++
 MAINTAINERS                                  |    8 +
 drivers/pci/pci.h                            |   14 +
 drivers/pci/pcie/Kconfig                     |   11 +
 drivers/pci/pcie/Makefile                    |    1 +
 drivers/pci/pcie/aspm.c                      |   70 ++
 drivers/pci/pcie/margin.c                    | 1596 ++++++++++++++++++++++++++
 drivers/pci/probe.c                          |    1 +
 drivers/pci/remove.c                         |    1 +
 include/uapi/linux/pci_regs.h                |   18 +
 tools/testing/selftests/Makefile             |    1 +
 tools/testing/selftests/pcie_lmr/Makefile    |    3 +
 tools/testing/selftests/pcie_lmr/pcie_lmr.sh |  387 +++++++
 14 files changed, 2267 insertions(+)

---
base-commit: 6628e7d4c6b25c8d18f1345e3a20d21829aae7e4
change-id: 20260831-pcie-link-endpoints-978e100d5d06

Best regards,

Signature · 1 line
Priyank Rathod <[email protected]>
#1 lore raw

PATCH v8 1/3 PCI/ASPM: Add internal pcie_aspm_inhibit() helper

Internal PCIe services such as Lane Margining at the Receiver need to temporarily keep ASPM disabled on a link and restore its prior policy afterward without clobbering link->aspm_disable, link->aspm_default, L1 PM Substates, or Clock PM state.

pci_disable_link_state() permanently sets link->aspm_disable, and pci_enable_link_state() intentionally refuses to clear it. Introduce a PCI-internal, reference-counted pcie_aspm_inhibit() helper:

- Track temporary inhibitors via aspm_inhibit_cnt on struct pcie_link_state under pci_bus_sem and aspm_lock.
- Force state = 0 in pcie_config_aspm_link() whenever aspm_inhibit_cnt is non-zero, so concurrent policy or sysfs changes still update the saved state cleanly and take effect once all inhibitors release.
- Return -EPERM rather than overriding firmware-enabled ASPM when the OS lacks ASPM control, and return 0 when ASPM support is disabled.

---
 drivers/pci/pci.h       |  6 +++++
 drivers/pci/pcie/aspm.c | 70 +++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 76 insertions(+)
drivers/pci/pci.h +6 -0
index 77efa4c5c960..e24a6dbf48ce 100644
@@ -1150,6 +1150,7 @@ void pcie_aspm_powersave_config_link(struct pci_dev *pdev);
 void pci_configure_ltr(struct pci_dev *pdev);
 void pci_bridge_reconfigure_ltr(struct pci_dev *pdev);
 void pcie_encode_t_power_on(u32 t_power_on_us, u8 *scale, u8 *value);
+int pcie_aspm_inhibit(struct pci_dev *pdev, bool inhibit);
 #else
 static inline void pcie_aspm_remove_cap(struct pci_dev *pdev, u32 lnkcap) { }
 static inline void pcie_aspm_init_link_state(struct pci_dev *pdev) { }
@@ -1163,6 +1164,11 @@ static inline void pcie_encode_t_power_on(u32 t_power_on_us, u8 *scale, u8 *valu
 	*scale = 0;
 	*value = 0;
 }
+
+static inline int pcie_aspm_inhibit(struct pci_dev *pdev, bool inhibit)
+{
+	return 0;
+}
 #endif
 
 #ifdef CONFIG_PCIE_ECRC
drivers/pci/pcie/aspm.c +70 -0

88 lines not on this page · raw message

Signature · 1 line
2.56.1.831.g720f3f5937-goog
#2 lore raw

Re: [PATCH v8 1/3] PCI/ASPM: Add internal pcie_aspm_inhibit() helper

Quoted 8 lines · Priyank Rathod

Internal PCIe services such as Lane Margining at the Receiver need to temporarily keep ASPM disabled on a link and restore its prior policy afterward without clobbering link->aspm_disable, link->aspm_default, L1 PM Substates, or Clock PM state.

pci_disable_link_state() permanently sets link->aspm_disable, and pci_enable_link_state() intentionally refuses to clear it. Introduce a PCI-internal, reference-counted pcie_aspm_inhibit() helper:

As you've discovered, pci_disable_link_state() and pci_enable_link_state() are not symmetric pairs, which is problematic for some use cases like this one.

There's a series to fix that and to me it looks a prerequisite for this LMR series so you shouldn't be duplicate that effort with this inhibit thing.

Perhaps help in reviewing that ASPM series, and/or, if the work has stalled with things left to address, ask the previous submitter if you could take over in finishing that series.

Quoted 7 lines

- Track temporary inhibitors via aspm_inhibit_cnt on struct pcie_link_state under pci_bus_sem and aspm_lock.
- Force state = 0 in pcie_config_aspm_link() whenever aspm_inhibit_cnt is non-zero, so concurrent policy or sysfs changes still update the saved state cleanly and take effect once all inhibitors release.
- Return -EPERM rather than overriding firmware-enabled ASPM when the OS lacks ASPM control, and return 0 when ASPM support is disabled.

Generally, don't describe things that are not real lists using bullet points (LLM tend to overuse bullets).

Signature · 1 line
 i.
Quoted 128 lines
---
 drivers/pci/pci.h       |  6 +++++
 drivers/pci/pcie/aspm.c | 70 +++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 76 insertions(+)
drivers/pci/pci.h +6 -0
index 77efa4c5c960..e24a6dbf48ce 100644
@@ -1150,6 +1150,7 @@ void pcie_aspm_powersave_config_link(struct pci_dev *pdev);
 void pci_configure_ltr(struct pci_dev *pdev);
 void pci_bridge_reconfigure_ltr(struct pci_dev *pdev);
 void pcie_encode_t_power_on(u32 t_power_on_us, u8 *scale, u8 *value);
+int pcie_aspm_inhibit(struct pci_dev *pdev, bool inhibit);
 #else
 static inline void pcie_aspm_remove_cap(struct pci_dev *pdev, u32 lnkcap) { }
 static inline void pcie_aspm_init_link_state(struct pci_dev *pdev) { }
@@ -1163,6 +1164,11 @@ static inline void pcie_encode_t_power_on(u32 t_power_on_us, u8 *scale, u8 *valu
 	*scale = 0;
 	*value = 0;
 }
+
+static inline int pcie_aspm_inhibit(struct pci_dev *pdev, bool inhibit)
+{
+	return 0;
+}
 #endif
 
 #ifdef CONFIG_PCIE_ECRC
drivers/pci/pcie/aspm.c +70 -0
index b039b57f8466..0d43dc517098 100644
@@ -245,6 +245,8 @@ struct pcie_link_state {
 	u32 clkpm_enabled:1;		/* Current Clock PM state */
 	u32 clkpm_default:1;		/* Default Clock PM state by BIOS */
 	u32 clkpm_disable:1;		/* Clock PM disabled */
+
+	unsigned int aspm_inhibit_cnt;	/* Reference count of inhibitors */
 };
 
 static bool aspm_disabled, aspm_force;
@@ -1055,6 +1057,10 @@ static void pcie_config_aspm_link(struct pcie_link_state *link, u32 state)
 	/* Enable only the states that were not explicitly disabled */
 	state &= (link->aspm_capable & ~link->aspm_disable);
 
+	/* Temporary inhibition overrides configured policy without mutating it */
+	if (link->aspm_inhibit_cnt)
+		state = 0;
+
 	/* Can't enable any substates if L1 is not enabled */
 	if (!(state & PCIE_LINK_STATE_L1))
 		state &= ~PCIE_LINK_STATE_L1SS;
@@ -1538,6 +1544,70 @@ static int __pci_disable_link_state(struct pci_dev *pdev, int state, bool locked
 	return 0;
 }
 
+/**
+ * pcie_aspm_inhibit - Temporarily inhibit or re-allow ASPM on a link
+ * @pdev: PCI device below the link (Downstream component of the link)
+ * @inhibit: True to inhibit ASPM, false to release the inhibition
+ *
+ * Maintain a reference count of temporary ASPM inhibitions on @pdev's
+ * upstream link without altering link->aspm_disable, link->aspm_default,
+ * or Clock PM state.  On the first inhibitor, ASPM is disabled in LNKCTL
+ * (Downstream component before Upstream component per PCIe r6.0 sec 7.5.3.7).
+ * When the last inhibitor releases, the configured ASPM policy is restored.
+ *
+ * Return: 0 on success, -EINVAL if @pdev has no ASPM link state or on an
+ * unbalanced release, -EBUSY on counter overflow, or -EPERM if the OS
+ * lacks ASPM control while firmware has ASPM enabled on the link.
+ */
+int pcie_aspm_inhibit(struct pci_dev *pdev, bool inhibit)
+{
+	struct pcie_link_state *link;
+	int ret = 0;
+
+	if (!pdev)
+		return -EINVAL;
+
+	if (!aspm_support_enabled)
+		return 0;
+
+	down_read(&pci_bus_sem);
+	mutex_lock(&aspm_lock);
+
+	link = pcie_aspm_get_link(pdev);
+	if (!link) {
+		ret = -EINVAL;
+		goto unlock;
+	}
+
+	if (inhibit) {
+		if (aspm_disabled && link->aspm_enabled) {
+			pci_warn_once(pdev,
+				      "can't inhibit ASPM; OS doesn't have ASPM control\n");
+			ret = -EPERM;
+			goto unlock;
+		}
+		if (link->aspm_inhibit_cnt == UINT_MAX) {
+			ret = -EBUSY;
+			goto unlock;
+		}
+		if (++link->aspm_inhibit_cnt == 1)
+			pcie_config_aspm_link(link, 0);
+	} else {
+		if (WARN_ON_ONCE(!link->aspm_inhibit_cnt)) {
+			ret = -EINVAL;
+			goto unlock;
+		}
+		if (--link->aspm_inhibit_cnt == 0 && !aspm_disabled)
+			pcie_config_aspm_link(link, policy_to_aspm_state(link));
+	}
+
+unlock:
+	mutex_unlock(&aspm_lock);
+	up_read(&pci_bus_sem);
+
+	return ret;
+}
+
 int pci_disable_link_state_locked(struct pci_dev *pdev, int state)
 {
 	lockdep_assert_held_read(&pci_bus_sem);
#3 lore raw

Re: [PATCH v8 1/3] PCI/ASPM: Add internal pcie_aspm_inhibit() helper

Hi Ilpo,

Thanks for the feedback. I reached out to Manivannan, Bjorn, and you on the ASPM thread to check whether Manivannan has a v4 in progress or if I can take over finishing the core ASPM changes cleanly first, and to align on the per-state counter API details before implementing it.

Also noted on avoiding bullet lists in commit messages, and I will fix the port->dev lifetime issue in pci_lmr_release() that sashiko-bot flagged on patch 2/3 when preparing the next revision on top of the ASPM prerequisite.

Thanks,
Priyank

#4 lore raw

PATCH v8 2/3 PCI/LMR: Add Lane Margining at the Receiver debugfs interface

In reply to Priyank Rathod

Lane Margining at the Receiver moves the sampling point of a receiver away from its normal setting, in time or in voltage, while the link stays up, and reports the errors the receiver sees. It is available on links running at 16.0 GT/s or faster, through the Lane Margining at the Receiver Extended Capability.

Add a debugfs directory, pcie_lmr_<device>, for each Downstream Port and each Function 0 below a Downstream Port that has the capability.

Writing 1 to 'enable' starts a margining session on the link. ASPM is temporarily inhibited with pcie_aspm_inhibit(), Hardware Autonomous Width Disable and Hardware Autonomous Speed Disable are set on both ends, both ends are kept runtime resumed, and the port's Margining Ready bit is awaited. Writing 0, or removing either end of the link, ends the session and undoes these steps. Only one session can be active on a link.

During a session, 'receiver' selects the receiver, laneN/margin_timing and laneN/margin_voltage issue Step Margin commands, laneN/caps shows the parameters the receiver reports, and laneN/status shows the response to the last step command, including the error count. Before each command, the link is checked against the state the session started in: speed, width, Margining Ready, ASPM still off, and no system suspend since.

The kernel does not run margining sweeps or interpret the results; that is left to user space.

---
 Documentation/PCI/index.rst    |    1 +
 Documentation/PCI/pcie-lmr.rst |  155 ++++
 MAINTAINERS                    |    7 +
 drivers/pci/pci.h              |    8 +
 drivers/pci/pcie/Kconfig       |   11 +
 drivers/pci/pcie/Makefile      |    1 +
 drivers/pci/pcie/margin.c      | 1596 ++++++++++++++++++++++++++++++++++++++++
 drivers/pci/probe.c            |    1 +
 drivers/pci/remove.c           |    1 +
 include/uapi/linux/pci_regs.h  |   18 +
 10 files changed, 1799 insertions(+)
Documentation/PCI/index.rst +1 -0
index 5d720d2a415e..9170c98cbf3f 100644
@@ -20,3 +20,4 @@ PCI Bus Subsystem
    controller/index
    boot-interrupts
    tph
+   pcie-lmr
Documentation/PCI/pcie-lmr.rst new file +155 -0

155 lines not on this page · raw message

MAINTAINERS +7 -0
index 93abbb315065..4d00d6d4a062 100644
@@ -21792,6 +21792,13 @@ F:	Documentation/devicetree/bindings/pci/qcom,sa8255p-pcie-ep.yaml
 F:	drivers/pci/controller/dwc/pcie-qcom-common.c
 F:	drivers/pci/controller/dwc/pcie-qcom-ep.c
 
+PCIE LANE MARGINING AT THE RECEIVER (LMR)
+M:	Priyank Rathod <[email protected]>
+L:	[email protected]
+S:	Maintained
+F:	Documentation/PCI/pcie-lmr.rst
+F:	drivers/pci/pcie/margin.c
+
 PCMCIA SUBSYSTEM
 M:	Dominik Brodowski <[email protected]>
 S:	Odd Fixes
drivers/pci/pci.h +8 -0
index e24a6dbf48ce..3b0318e7d014 100644
@@ -773,6 +773,14 @@ static inline void pci_tsm_init(struct pci_dev *pdev) { }
 static inline void pci_tsm_destroy(struct pci_dev *pdev) { }
 #endif
 
+#ifdef CONFIG_PCIE_LMR
+void pci_lmr_init(struct pci_dev *dev);
+void pci_lmr_exit(struct pci_dev *dev);
+#else
+static inline void pci_lmr_init(struct pci_dev *dev) { }
+static inline void pci_lmr_exit(struct pci_dev *dev) { }
+#endif
+
 /**
  * pci_dev_set_io_state - Set the new error state if possible.
  *
drivers/pci/pcie/Kconfig +11 -0
index 207c2deae35f..b307460fa97c 100644
@@ -146,3 +146,14 @@ config PCIE_EDR
 	  the PCI Firmware Specification r3.2.  Enable this if you want to
 	  support hybrid DPC model which uses both firmware and OS to
 	  implement DPC.
+
+config PCIE_LMR
+	bool "PCI Express Lane Margining at the Receiver support"
+	depends on PCIEASPM && DEBUG_FS
+	help
+	  This option exposes the Lane Margining at the Receiver Extended
+	  Capability in debugfs (see Documentation/PCI/pcie-lmr.rst), so
+	  that user space can measure the timing and voltage margin of
+	  links running at 16.0 GT/s or faster.
+
+	  If unsure, say N.
drivers/pci/pcie/Makefile +1 -0
index b0b43a18c304..f7216f6a3bac 100644
@@ -14,3 +14,4 @@ obj-$(CONFIG_PCIE_PME)		+= pme.o
 obj-$(CONFIG_PCIE_DPC)		+= dpc.o
 obj-$(CONFIG_PCIE_PTM)		+= ptm.o
 obj-$(CONFIG_PCIE_EDR)		+= edr.o
+obj-$(CONFIG_PCIE_LMR)		+= margin.o
drivers/pci/pcie/margin.c new file +1596 -0

1,596 lines not on this page · raw message

drivers/pci/probe.c +1 -0
index 9a178e0e5f7f..2c30eb14e325 100644
@@ -2681,6 +2681,7 @@ static void pci_init_capabilities(struct pci_dev *dev)
 	pci_rebar_init(dev);		/* Resizable BAR */
 	pci_dev3_init(dev);		/* Device 3 capabilities */
 	pci_ide_init(dev);		/* Link Integrity and Data Encryption */
+	pci_lmr_init(dev);		/* Lane Margining at the Receiver */
 
 	pcie_report_downtraining(dev);
 	pci_init_reset_methods(dev);
drivers/pci/remove.c +1 -0
index e711ac1d4e38..c858386beff3 100644
@@ -37,6 +37,7 @@ static void pci_destroy_dev(struct pci_dev *dev)
 	platform_pci_remove_wake(dev);
 	pci_doe_sysfs_teardown(dev);
 	pci_npem_remove(dev);
+	pci_lmr_exit(dev);
 
 	/*
 	 * While device is in D0 drop the device from TSM link operations
include/uapi/linux/pci_regs.h +18 -0
index facaa324bd86..47daa1cebe0e 100644
@@ -711,6 +711,8 @@
 #define  PCI_EXP_LNKCTL2_TX_MARGIN	0x0380 /* Transmit Margin */
 #define  PCI_EXP_LNKCTL2_HASD		0x0020 /* HW Autonomous Speed Disable */
 #define PCI_EXP_LNKSTA2		0x32	/* Link Status 2 */
+#define  PCI_EXP_LNKSTA2_RETIMER	0x0040 /* Retimer Presence Detected */
+#define  PCI_EXP_LNKSTA2_2RETIMERS	0x0080 /* Two Retimers Presence Detected */
 #define  PCI_EXP_LNKSTA2_FLIT		0x0400 /* Flit Mode Status */
 #define PCI_CAP_EXP_ENDPOINT_SIZEOF_V2	0x34	/* end of v2 EPs w/ link */
 #define PCI_EXP_SLTCAP2		0x34	/* Slot Capabilities 2 */
@@ -757,6 +759,7 @@
 #define PCI_EXT_CAP_ID_VF_REBAR 0x24	/* VF Resizable BAR */
 #define PCI_EXT_CAP_ID_DLF	0x25	/* Data Link Feature */
 #define PCI_EXT_CAP_ID_PL_16GT	0x26	/* Physical Layer 16.0 GT/s */
+#define PCI_EXT_CAP_ID_LMR	0x27	/* Lane Margining at the Receiver */
 #define PCI_EXT_CAP_ID_NPEM	0x29	/* Native PCIe Enclosure Management */
 #define PCI_EXT_CAP_ID_PL_32GT  0x2A    /* Physical Layer 32.0 GT/s */
 #define PCI_EXT_CAP_ID_DOE	0x2E	/* Data Object Exchange */
@@ -1181,6 +1184,21 @@
 #define  PCI_PL_16GT_LE_CTRL_USP_TX_PRESET_MASK		0x000000F0
 #define  PCI_PL_16GT_LE_CTRL_USP_TX_PRESET_SHIFT	4
 
+/* Lane Margining at the Receiver */
+#define PCI_LMR_PORT_CAP		0x04	/* Port Capabilities */
+#define  PCI_LMR_PORT_CAP_USES_SW_READY	0x0001	/* Uses Driver Software */
+#define PCI_LMR_PORT_STS		0x06	/* Port Status */
+#define  PCI_LMR_PORT_STS_MARGIN_READY	0x0001	/* Margining Ready */
+#define  PCI_LMR_PORT_STS_SW_READY	0x0002	/* Margining Software Ready */
+#define PCI_LMR_LANE_CTRL		0x08	/* Lane Control, lane 0 */
+#define PCI_LMR_LANE_STS		0x0a	/* Lane Status, lane 0 */
+#define PCI_LMR_LANE_STRIDE		4	/* Lane N at +4*N */
+/* Fields of both Margining Lane Control and Margining Lane Status */
+#define  PCI_LMR_LANE_RX		0x0007	/* Receiver Number */
+#define  PCI_LMR_LANE_TYPE		0x0038	/* Margin Type */
+#define  PCI_LMR_LANE_USAGE		0x0040	/* Usage Model */
+#define  PCI_LMR_LANE_PAYLOAD		0xff00	/* Margin Payload */
+
 /* Physical Layer 32.0 GT/s */
 #define PCI_PL_32GT_LE_CTRL	0x20	/* Lane Equalization Control Register */
 
Signature · 1 line
2.56.1.831.g720f3f5937-goog
#5 lore raw

Re: [PATCH v8 2/3] PCI/LMR: Add Lane Margining at the Receiver debugfs interface

Thank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [Medium] PCI/LMR: use-after-free on port->dev in pci_lmr_release() on teardown race --

Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Medium] PCI/LMR: use-after-free on port->dev in pci_lmr_release() on teardown race
--

commit c126cec11a02e704f7895b57855ddcc4e39ac680
Author: Priyank Rathod <[email protected]>

PCI/LMR: Add Lane Margining at the Receiver debugfs interface

This commit adds a debugfs interface for the PCIe Lane Margining at the Receiver Extended Capability on Downstream Ports and Function 0 devices below them, allowing user space to run margining sessions on links at 16.0 GT/s or faster.

Quoted 5 lines
drivers/pci/pcie/margin.c new file +0 -0
new file mode 100644
index 0000000000000..df73fed885fa5

[ ... ]

Quoted 35 lines
drivers/pci/pcie/margin.c new file +35 -0
+static struct pci_dev *pci_lmr_teardown(struct pci_lmr_port *port)
+{
+	struct pci_dev *partner = port->partner;
+	bool removing;
+
+	lockdep_assert_held(&port->lock);
+
+	if (port->state != LMR_ACTIVE)
+		return NULL;
+
+	removing = !pci_dev_is_added(port->dev) || !pci_dev_is_added(partner);
+
+	if (!pci_dev_is_disconnected(port->dev))
+		pci_lmr_reset_lanes(port);
+	pci_lmr_autonomous_restore(port);
+	if (!removing)
+		pci_lmr_aspm_restore(port);
+	else
+		port->aspm_inhibited = false;
+
+	pci_lmr_forget_rx(port);
+	scoped_guard(mutex, &pci_lmr_mutex) {
+		port->partner = NULL;
+		pci_lmr_set_state(port, LMR_IDLE);
+	}
+
+	return partner;
+}
+
+static void pci_lmr_release(struct pci_lmr_port *port, struct pci_dev *partner)
+{
+	pm_runtime_put_sync(&partner->dev);
+	pm_runtime_put_sync(&port->dev->dev);
+	pci_dev_put(partner);
+}

[ ... ]

Quoted 36 lines
drivers/pci/pcie/margin.c new file +36 -0
+static void pci_lmr_end_sessions(struct pci_dev *dev)
+{
+	struct pci_lmr_port *p;
+	struct pci_dev *ended;
+
+	for (;;) {
+		scoped_guard(mutex, &pci_lmr_mutex)
+			p = pci_lmr_find_session(dev);
+		if (!p)
+			break;
+
+		scoped_guard(mutex, &p->lock)
+			ended = pci_lmr_teardown(p);
+		if (ended)
+			pci_lmr_release(p, ended);
+		pci_lmr_port_put(p);
+	}
+}
+
+/**
+ * pci_lmr_exit - End margining on a device that is being removed
+ * @dev: PCI device
+ *
+ * Called from pci_destroy_dev() for every device.  Removes the debugfs
+ * directory of @dev, waiting for file operations in progress, and ends
+ * every session that involves @dev, whichever end of the link hosts it.
+ */
+void pci_lmr_exit(struct pci_dev *dev)
+{
+	struct pci_lmr_port *port;
+
+	scoped_guard(mutex, &pci_lmr_mutex)
+		port = pci_lmr_find_port(dev);
+
+	if (port)
+		debugfs_remove(port->dir);

drivers/pci/pcie/margin.c line 1201

Quoted 10 lines
drivers/pci/pcie/margin.c new file +10 -0
+
+	pci_lmr_end_sessions(dev);
+
+	if (!port)
+		return;
+
+	scoped_guard(mutex, &pci_lmr_mutex)
+		list_del(&port->node);
+	pci_lmr_port_put(port);
+}

[Severity: Medium]
Can pci_lmr_release() use port->dev after it has been freed if another context tears the session down concurrently with pci_destroy_dev(port->dev)?

Neither pci_lmr_init() nor pci_lmr_start() takes a pci_dev_get() reference on port->dev, and port->kref only pins struct pci_lmr_port. Because pci_lmr_teardown() sets port->state to LMR_IDLE before its caller drops port->lock and runs pci_lmr_release(), pci_lmr_exit(port->dev) does not wait for pci_lmr_release() to finish:

CPU0 CPU1
pci_destroy_dev(partner)
pci_lmr_exit(partner)

    pci_lmr_end_sessions(partner)
      p = pci_lmr_find_session(...)
      pci_lmr_teardown(p)
        p->partner = NULL
        p->state = LMR_IDLE
                                      pci_destroy_dev(p->dev)
                                        pci_lmr_exit(p->dev)
                                          debugfs_remove(p->dir)
                                          pci_lmr_end_sessions(p->dev)
                                            /* state is LMR_IDLE, returns */
                                          list_del(&p->node)
                                          pci_lmr_port_put(p)
                                        device_del(&p->dev->dev)
                                        put_device(&p->dev->dev)
      pci_lmr_release(p, ended)
        pm_runtime_put_sync(&p->dev->dev)  <-- use-after-free

Taking a pci_dev_get(port->dev) reference for the lifetime of the session (or of struct pci_lmr_port) and keeping the session non-idle until pci_lmr_release() completes would prevent pci_destroy_dev() from tearing down and freeing port->dev early.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
#6 lore raw

PATCH v8 3/3 selftests/pcie_lmr: Add tests for the Lane Margining debugfs interface

In reply to Priyank Rathod

Add a KTAP test for the Lane Margining at the Receiver debugfs interface.

By default it only runs checks that do not start a margining session, on every pcie_lmr_* directory: the files exist, malformed input is rejected, and commands fail with ENOTCONN without a session.

If PCIE_LMR_DEV names a port, it also starts a session on that link, checks the receiver and step bounds the receiver reports, discovers and tests any retimer receivers on the link when run on a Downstream Port, and checks that Link Control and Link Control 2 of both ends are the same after the session as before it.

---
 MAINTAINERS                                  |   1 +
 tools/testing/selftests/Makefile             |   1 +
 tools/testing/selftests/pcie_lmr/Makefile    |   3 +
 tools/testing/selftests/pcie_lmr/pcie_lmr.sh | 387 +++++++++++++++++++++++++++
 4 files changed, 392 insertions(+)
MAINTAINERS +1 -0
index 4d00d6d4a062..e95698b32dc4 100644
@@ -21798,6 +21798,7 @@ L:	[email protected]
 S:	Maintained
 F:	Documentation/PCI/pcie-lmr.rst
 F:	drivers/pci/pcie/margin.c
+F:	tools/testing/selftests/pcie_lmr/
 
 PCMCIA SUBSYSTEM
 M:	Dominik Brodowski <[email protected]>
tools/testing/selftests/Makefile +1 -0
index 07c3719f8b04..41d597f649a8 100644
@@ -103,6 +103,7 @@ TARGETS += nfsd
 TARGETS += nolibc
 TARGETS += pci_endpoint
 TARGETS += pcie_bwctrl
+TARGETS += pcie_lmr
 TARGETS += perf_events
 TARGETS += pidfd
 TARGETS += pid_namespace
tools/testing/selftests/pcie_lmr/Makefile new file +3 -0
new file mode 100644
index 000000000000..101c90e7ee9e
@@ -0,0 +1,3 @@
+# SPDX-License-Identifier: GPL-2.0
+TEST_PROGS = pcie_lmr.sh
+include ../lib.mk
tools/testing/selftests/pcie_lmr/pcie_lmr.sh new file +387 -0

387 lines not on this page · raw message

Signature · 1 line
2.56.1.831.g720f3f5937-goog
#7 lore raw
In reply to Priyank Rathod
Sashiko has finished reviewing this 3-patch series:

Sashiko has finished reviewing this 3-patch series:

- Patches 1 and 3: no issues found.
- Patch 2 ("PCI/LMR: Add Lane Margining at the Receiver debugfs..."): 1 potential issue (1 Medium)

A detailed review has been sent as a separate reply to patch 2.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]