PCI/pcie: Add PCIe Lane Margining at Receiver (LMR) support
Subsystems (3)
PATCH v6 1/1 PCI/pcie: Add PCIe Lane Margining at Receiver (LMR) support
Per PCIe Base Specification r6.0, sec 8.4.4 ("Lane Margining at Receiver"), PCIe devices operating at 16.0 GT/s (Gen 4) or higher data rates support the Lane Margining at Receiver Extended Capability (ID 0x27), and it is mandatory for receivers operating at 64.0 GT/s (Gen 6) or higher data rates. Lane Margining allows software to evaluate high-speed link margins by measuring timing and voltage steps for each individual physical lane and receiver.
Add driver and debugfs support for PCIe Lane Margining at Receiver:
- Add Lane Margining at Receiver Extended Capability register definitions (PCI_EXT_CAP_ID_LMR, PCI_LMR_PORT_CAP, PCI_LMR_PORT_STS, PCI_LMR_LANE_CTRL, PCI_LMR_LANE_STS) to <uapi/linux/pci_regs.h>.
- Add Kconfig option CONFIG_PCIE_LMR (under drivers/pci/pcie/Kconfig) dependent on DEBUG_FS.
- Implement drivers/pci/pcie/margin.c to probe the capability on Gen4+ links and expose per-device debugfs entries under:
/sys/kernel/debug/pci/pcie_lmr_<pci_dev_name>/
providing control over margining enablement, receiver selection, and execution of timing/voltage margin step commands. Distinguish between missing mandatory LMR capability on Gen6+ vs optional on Gen4/Gen5.
- Hook pci_lmr_init() into pci_init_capabilities() during device probe in drivers/pci/probe.c and pci_lmr_exit() into drivers/pci/remove.c.
- Add kselftest script under tools/testing/selftests/pcie_lmt/pcie_lmt.sh to test debugfs capability reads, enablement, and stepping.
- Add MAINTAINERS entry for PCIe Lane Margining at Receiver (LMR).
---
Changes in v6:
- Added kernel documentation under Documentation/PCI/pcie-lmr.rst and indexed in Documentation/PCI/index.rst (Ilpo Järvinen).
- Updated MAINTAINERS with Documentation/PCI/pcie-lmr.rst (Ilpo Järvinen).
- Aligned capability bit naming and comments with PCIe Base Specification r6.0 sec 8.4.4 Table "Report Margining Capabilities Payload" (Ilpo Järvinen).
- Clarified Sample Multiple Receivers concurrency verification and rules across physical lanes in kerneldoc and documentation (Ilpo Järvinen).
- Refactored pci_lmr_run_cmd() to pass struct pci_margin_dev *mdev directly, eliminating redundant NULL checks and using mdev->num_lanes (Ilpo Järvinen).
- Converted PCI config read/write return checking across all helpers to pcibios_err_to_errno() (Ilpo Järvinen).
- Reversed return logic in pci_lmr_demargin_lane() to return early on error (Ilpo Järvinen).
- Refactored margin_lane_step_write() to eliminate bool is_voltage parameter, using command type (LMR_TYPE_TIMING / LMR_TYPE_VOLTAGE) and switch/case with consolidated bounds checks (Ilpo Järvinen).
- Renamed __pci_suspend_lmr_locked() to pci_lmr_disable_locked() to avoid PM terminology confusion and added lockdep_assert_held(&mdev->lock) (Ilpo Järvinen).
- Replaced -EACCES with -EBUSY across debugfs show/write callbacks when margining is inactive (Ilpo Järvinen).
- Clarified comment for active operating link speed check (Gen4+ capability vs dynamically operating speed) in margin_enable_write() (Ilpo Järvinen).
- Added WARN_ON_ONCE(!dev) check in pci_lmr_init() (Ilpo Järvinen).
- Demoted capability detection log message from pci_info to pci_dbg to prevent boot log noise (Ilpo Järvinen).
- Fixed timing step mask extraction in pci_lmr_cache_rx_info() to use 6-bit LMR_TIMING_STEP_MASK (sashiko-bot).
- Resumed runtime PM via pm_runtime_resume_and_get() before performing config space reads in margin_enable_write() (sashiko-bot).
- Switched to pm_runtime_put_sync() during margining teardown (sashiko-bot).
- Link to v5: https://lore.kernel.org/r/[email protected]
PCI/pcie: Add PCIe Lane Margining at Receiver (LMR) support
Per PCIe Base Specification r6.0, section 8.4.4 ("Lane Margining at Receiver"), PCIe devices operating at 16.0 GT/s (Gen 4) or higher data rates support the Lane Margining at Receiver Extended Capability (ID 0x27), and it is mandatory for receivers operating at 64.0 GT/s (Gen 6) or higher data rates.
Lane Margining allows system software to evaluate high-speed link signal integrity and margins by measuring timing and voltage steps for each physical lane and receiver independently.
This series introduces kernel driver support, debugfs controls, and a kselftest automation script for PCIe Lane Margining at Receiver (LMR/LMT).
==============================================================================
1. How to Enable & Configure
==============================================================================
Enable the Kconfig option under PCI support:
CONFIG_PCIE_LMR=y (or =m)
(Depends on CONFIG_PCI and CONFIG_DEBUG_FS)
Upon boot or device hotplug on Gen4+ links (>= 16.0 GT/s), the driver probes Extended Capability ID 0x27 and exposes per-device debugfs interfaces:
/sys/kernel/debug/pci/pcie_lmr_<domain>:<bus>:<dev>.<func>/
==============================================================================
2. How to Use the Debugfs Interface (Manual Margining)
==============================================================================
Inspect device-wide margining capabilities and port status:
# Inspect root device LMR capabilities & status cat /sys/kernel/debug/pci/pcie_lmr_0000:01:00.0/capabilities cat /sys/kernel/debug/pci/pcie_lmr_0000:01:00.0/port_status
Enable active Lane Margining on the device:
# Enable Lane Margining state machine
echo 1 > /sys/kernel/debug/pci/pcie_lmr_0000:01:00.0/enable
Inspect and step individual lanes (e.g. lane0):
# Select target receiver (0 = local receiver, 1..6 = retimers/link partners) echo 0 > /sys/kernel/debug/pci/pcie_lmr_0000:01:00.0/lane0/receiver
# Check available timing and voltage steps for this receiver cat /sys/kernel/debug/pci/pcie_lmr_0000:01:00.0/lane0/caps cat /sys/kernel/debug/pci/pcie_lmr_0000:01:00.0/lane0/num_timing_steps cat /sys/kernel/debug/pci/pcie_lmr_0000:01:00.0/lane0/num_voltage_steps
# Step timing margin or voltage margin offset
echo 2 > /sys/kernel/debug/pci/pcie_lmr_0000:01:00.0/lane0/margin_timing echo 1 > /sys/kernel/debug/pci/pcie_lmr_0000:01:00.0/lane0/margin_voltage
# Reset margin offset back to nominal (0)
echo 0 > /sys/kernel/debug/pci/pcie_lmr_0000:01:00.0/lane0/margin_timing echo 0 > /sys/kernel/debug/pci/pcie_lmr_0000:01:00.0/lane0/margin_voltage
Disable Lane Margining when finished:
echo 0 > /sys/kernel/debug/pci/pcie_lmr_0000:01:00.0/enable
==============================================================================
3. How to Run Automated Kselftests Using the Test Script
==============================================================================
An automated kselftest script is included to test capability reads, receiver selection, and margining commands across all enumerated LMR devices:
# Run directly as root
sudo ./tools/testing/selftests/pcie_lmt/pcie_lmt.sh
Or run via the kselftest Makefile harness:
make -C tools/testing/selftests TARGETS=pcie_lmt run_tests
Sample script output on an LMR-capable device:
pcie_lmt: testing PCIe LMR debugfs entries
pcie_lmt: probing device pcie_lmr_0000:01:00.0
pcie_lmr_0000:01:00.0: capabilities read OK
pcie_lmr_0000:01:00.0: port_status read OK
pcie_lmr_0000:01:00.0: margining enabled OK
pcie_lmr_0000:01:00.0: testing lane0
pcie_lmr_0000:01:00.0: testing lane1
pcie_lmr_0000:01:00.0: margining disabled OK pcie_lmt [PASS]
To: Bjorn Helgaas <[email protected]>
To: Shuah Khan <[email protected]>
Cc: [email protected]
Cc: [email protected]
Cc: [email protected]
Cc: Ilpo Järvinen <[email protected]>
Changes in v5:
- Sorted #include directives alphabetically and added missing includes for bits.h, bitfield.h, cleanup.h, overflow.h, and slab.h (Ilpo Järvinen).
- Converted bitmasks to GENMASK() and BIT() macros and used FIELD_PREP() and FIELD_GET() instead of manual bit shifts (Ilpo Järvinen).
- Added pci_lmr_sts_payload() helper to cleanly extract the status payload byte before applying step and capability masks (Ilpo Järvinen).
- Replaced manual mutex locking sequences with guard(mutex)(&mdev->lock) across show and write callbacks to simplify control flow (Ilpo Järvinen).
- Documented mutex lock protection scope in kerneldoc for struct pci_margin_dev (Ilpo Järvinen).
- Used standard PCI_POSSIBLE_ERROR(), str_yes_no(), and scnprintf() helpers throughout the driver (Ilpo Järvinen).
- Clarified receiver range (0..6 per PCIe r6.0 sec 8.4.4; 7 reserved) in comments and validation checks (Ilpo Järvinen).
- Deduplicated timing and voltage show/write handlers using margin_lane_steps_show() and margin_lane_step_write() (Ilpo Järvinen).
- Placed speed check immediately following pcie_get_speed_cap() and handled PCI_SPEED_UNKNOWN (Ilpo Järvinen).
- Converted lanes in struct pci_margin_dev to a flexible array member with __counted_by(num_lanes) allocated via struct_size() (Ilpo Järvinen).
Changes in v4:
- Added Sample Multiple Receivers (Bit 5) concurrency verification in margin_lane_timing_write() and margin_lane_voltage_write() per PCIe r6.0 sec 8.4.4, returning -EBUSY if another lane on the same receiver is already margined when simultaneous lane margining is not supported.
- Added active operating link speed verification (PCI_EXP_LNKSTA_CLS >= 16.0 GT/s) in margin_enable_write() before enabling LMR, as LMR commands are physically undefined on links operating at Gen1/Gen2/Gen3 speeds.
- Added fast-path hardware NAK detection in pci_lmr_run_cmd() to return -EOPNOTSUPP immediately if a receiver echoes MTYPE == NO_CMD (0x7) after command issuance rather than waiting 150ms for a timeout.
- Added pci_reset_lmr() hooked into __pci_reset_function_locked() to synchronize software state and demargin on FLR or Secondary Bus Reset.
- Comprehensive NULL pointer checks and array/lane/receiver bounds checks added across all internal helpers and debugfs write handlers.
- Added MAINTAINERS entry for PCIe Lane Margining at Receiver (LMR).
Changes in v2:
- Fixed NO_CMD (0x7) clearing in pci_lmr_run_cmd() before issuing new commands per PCIe r6.0 sec 8.4.4.
- Protected plane->rx updates with mdev->lock in margin_lane_receiver_write().
- Corrected Margining Port Capabilities bit definition to PCI_LMR_PORT_CAP_USES_SW_READY (0x0001) in <uapi/linux/pci_regs.h>.
- Updated kselftest script (pcie_lmt.sh) to locate LMR debugfs entries.
- Validated integer bounds against LMR_MAX_TIMING_STEP / LMR_MAX_VOLTAGE_STEP before narrowing u8 cast.
- Moved mdev->enabled checks inside mutex_lock(&mdev->lock) to eliminate TOCTOU races.
- Checked return values of all pci_read_config_word() calls, propagating -EIO on failure.
- Eliminated dead store of cap in margin_enable_write().
- Explicitly checked speed == PCIE_SPEED_64_0GT in pci_lmr_init() to avoid misidentifying PCI_SPEED_UNKNOWN (0xFF) as Gen6.
--- Documentation/PCI/index.rst | 1 + Documentation/PCI/pcie-lmr.rst | 171 +++++ MAINTAINERS | 8 + drivers/pci/pci-driver.c | 1 + drivers/pci/pci.c | 4 +- drivers/pci/pci.h | 12 + drivers/pci/pcie/Kconfig | 12 + drivers/pci/pcie/Makefile | 1 + drivers/pci/pcie/margin.c | 1061 ++++++++++++++++++++++++++ drivers/pci/probe.c | 1 + drivers/pci/remove.c | 1 + include/linux/pci.h | 6 + include/uapi/linux/pci_regs.h | 18 + tools/testing/selftests/Makefile | 1 + tools/testing/selftests/pcie_lmt/Makefile | 3 + tools/testing/selftests/pcie_lmt/pcie_lmt.sh | 105 +++ 16 files changed, 1405 insertions(+), 1 deletion(-)
Documentation/PCI/index.rst +1 -0
@@ -20,3 +20,4 @@ PCI Bus Subsystem controller/index boot-interrupts tph + pcie-lmr
Documentation/PCI/pcie-lmr.rst new file +171 -0
171 lines not on this page · raw message
MAINTAINERS +8 -0
@@ -21059,6 +21059,14 @@ 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 RECEIVER (LMR) +M: Priyank Rathod <[email protected]> +L: [email protected] +S: Maintained +F: Documentation/PCI/pcie-lmr.rst +F: drivers/pci/pcie/margin.c +F: tools/testing/selftests/pcie_lmt/ + PCMCIA SUBSYSTEM M: Dominik Brodowski <[email protected]> S: Odd Fixes
drivers/pci/pci-driver.c +1 -0
@@ -821,6 +821,7 @@ static int pci_pm_suspend(struct device *dev)* since Coffee Lake, to enter a lower-power PM state. */ pci_suspend_ptm (pci_dev); +pci_suspend_lmr (pci_dev);if (pci_has_legacy_pm_support (pci_dev))return pci_legacy_suspend (dev,PMSG_SUSPEND );
drivers/pci/pci.c +3 -1
@@ -5145,8 +5145,10 @@ int __pci_reset_function_locked(struct pci_dev *dev) method = &pci_reset_fn_methods[m];pci_dbg (dev,"reset via %s\n" , method->name); rc = method->reset_fn (dev,PCI_RESET_DO_RESET ); -if (!rc) +if (!rc) { +pci_reset_lmr (dev);return 0 ; + }pci_dbg (dev,"%s failed with %d\n" , method->name, rc);if (rc != -ENOTTY )
drivers/pci/pci.h +12 -0
@@ -1023,6 +1023,18 @@ static inline void pci_no_tph(void) { }static inline void pci_tph_init (struct pci_dev *dev) { }#endif +#ifdef CONFIG_PCIE_LMR +void pci_lmr_init (struct pci_dev *dev); +void pci_lmr_exit (struct pci_dev *dev); +void pci_suspend_lmr (struct pci_dev *dev); +void pci_reset_lmr (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) { } +static inline void pci_suspend_lmr (struct pci_dev *dev) { } +static inline void pci_reset_lmr (struct pci_dev *dev) { } +#endif +#ifdef CONFIG_PCIE_PTM void pci_ptm_init (struct pci_dev *dev);void pci_save_ptm_state (struct pci_dev *dev);
drivers/pci/pcie/Kconfig +12 -0
@@ -137,6 +137,18 @@ config PCIE_PTM This is only useful if you have devices that support PTM, but it is safe to enable even if you don't. +config PCIE_LMR + bool "PCI Express Lane Margining at Receiver Support" + depends on DEBUG_FS + help + This enables the PCI Express Lane Margining at Receiver support. + Lane Margining allows software to determine the voltage and + timing margin of each lane on a PCIe link (16.0 GT/s and above). + The margining data is exposed via debugfs. + + This is only useful if you have devices that support lane + margining, but it is safe to enable even if you don't. + config PCIE_EDR bool "PCI Express Error Disconnect Recover support" depends on PCIE_DPC && ACPI
drivers/pci/pcie/Makefile +1 -0
@@ -13,4 +13,5 @@ obj-$(CONFIG_PCIEAER_INJECT) += aer_inject.o obj-$(CONFIG_PCIE_PME) += pme.o obj-$(CONFIG_PCIE_DPC) += dpc.o obj-$(CONFIG_PCIE_PTM) += ptm.o +obj-$(CONFIG_PCIE_LMR) += margin.o obj-$(CONFIG_PCIE_EDR) += edr.o
drivers/pci/pcie/margin.c new file +1061 -0
1,061 lines not on this page · raw message
drivers/pci/probe.c +1 -0
@@ -2666,6 +2666,7 @@ static void pci_init_capabilities(struct pci_dev *dev)pci_pasid_init (dev);/* Process Address Space ID */ pci_acs_init (dev);/* Access Control Services */ pci_ptm_init (dev);/* Precision Time Measurement */ +pci_lmr_init (dev);/* Lane Margining at Receiver */ pci_aer_init (dev);/* Advanced Error Reporting */ pci_dpc_init (dev);/* Downstream Port Containment */ pci_rcec_init (dev);/* Root Complex Event Collector */
drivers/pci/remove.c +1 -0
@@ -36,6 +36,7 @@ static void pci_destroy_dev(struct pci_dev *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/linux/pci.h +6 -0
@@ -349,6 +349,8 @@ struct rcec_ea;* number resources to allow for hierarchy expansion. * @is_pciehp: PCIe Hot-Plug Capable bridge. */ +struct pci_margin_dev ; +struct pci_dev {struct list_head bus_list;/* Node in per-bus list */ struct pci_bus *bus;/* Bus this device is on */
@@ -528,6 +530,10 @@ struct pci_dev {atomic_t ptm_enable_cnt;u8 ptm_granularity;#endif +#ifdef CONFIG_PCIE_LMR +u16 lmr_cap;/* Lane Margining Capability */ +struct pci_margin_dev *lmr; +#endif #ifdef CONFIG_PCI_MSI void __iomem *msix_base;raw_spinlock_t msi_lock;
include/uapi/linux/pci_regs.h +18 -0
@@ -757,6 +757,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 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 +1182,23 @@#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 Receiver */ +#define PCI_LMR_PORT_CAP 0x04/* Margining Port Capabilities */ +#define PCI_LMR_PORT_CAP_USES_SW_READY 0x0001/* Margining Uses Software Ready */ +#define PCI_LMR_PORT_STS 0x06/* Margining Port Status */ +#define PCI_LMR_PORT_STS_MARGIN_READY 0x0001/* Margining Ready */ +#define PCI_LMR_PORT_STS_SW_READY 0x0002/* Margining SW Ready */ +#define PCI_LMR_LANE_CTRL 0x08/* Margining Lane Control */ +#define PCI_LMR_LANE_CTRL_RX_NUM 0x0007/* Receiver Number */ +#define PCI_LMR_LANE_CTRL_MTYPE 0x0038/* Margining Type */ +#define PCI_LMR_LANE_CTRL_USAGE 0x0040/* Margining Usage Model */ +#define PCI_LMR_LANE_CTRL_PAYLOAD 0xFF00/* Margining Payload */ +#define PCI_LMR_LANE_STS 0x0A/* Margining Lane Status */ +#define PCI_LMR_LANE_STS_RX_NUM 0x0007/* Receiver Number */ +#define PCI_LMR_LANE_STS_MTYPE 0x0038/* Margining Type */ +#define PCI_LMR_LANE_STS_USAGE 0x0040/* Margining Usage Model */ +#define PCI_LMR_LANE_STS_PAYLOAD 0xFF00/* Margining Payload */ +/* Physical Layer 32.0 GT/s */ #define PCI_PL_32GT_LE_CTRL 0x20/* Lane Equalization Control Register */
tools/testing/selftests/Makefile +1 -0
@@ -91,6 +91,7 @@ TARGETS += net/tcp_ao TARGETS += nolibc TARGETS += pci_endpoint TARGETS += pcie_bwctrl +TARGETS += pcie_lmt TARGETS += perf_events TARGETS += pidfd TARGETS += pid_namespace
tools/testing/selftests/pcie_lmt/Makefile new file +3 -0
@@ -0,0 +1,3 @@ +# SPDX-License-Identifier: GPL-2.0 +TEST_PROGS = pcie_lmt.sh +include ../lib.mk
tools/testing/selftests/pcie_lmt/pcie_lmt.sh new file +105 -0
105 lines not on this page · raw message
---
base-commit: 0f23d56f17fdfc7db69d51f64c8b91bbab947aa9
change-id: 20260818-pcie-lmt-3044d586aaec
Best regards,
Signature · 1 line
Priyank Rathod <[email protected]>
[email protected]
AI reviewThank you for your contribution! Sashiko AI review found 8 potential issue(s) to consider: - [High] Device ASPM state is permanently disabled after a PCI reset because `pci_restore_state()` clobbers…
Thank you for your contribution! Sashiko AI review found 8 potential issue(s) to consider:
- [High] Device ASPM state is permanently disabled after a PCI reset because pci_restore_state() clobbers the correctly restored LMR ASPM configuration.
- [High] Concurrent execution of sysfs device reset and device removal leads to a Use-After-Free of dev->lmr and its mutex.
- [High] Incorrect teardown ordering in pci_lmr_exit() allows concurrent debugfs writes to permanently leak an RPM reference and leave upstream ASPM disabled.
- [Medium] LMR software state is not suspended during hibernation, leading to a mismatch between software tracking and hardware state upon thaw/restore.
- [Medium] The driver violates PCIe specification requirements by failing to automatically issue a Demargin command before reversing margin directions.
- [Medium] The error cleanup path in margin_enable_write() clobbers the original negative error code, causing the function to silently return success.
- [Medium] Secondary Bus Resets (SBR) bypass device-specific reset handlers, permanently desynchronizing LMR software state and leaking the runtime PM reference.
- [Medium] The driver writes 1 to reserved direction bits when configuring devices that only support symmetric margining.
--
--- Patch [1]: [PATCH v6] PCI/pcie: Add PCIe Lane Margining at Receiver (LMR) support --- commit 23e30598998c8cd47f12107ec01b8ff50f90586b
Author: Priyank Rathod <[email protected]>
Subject: PCI/pcie: Add PCIe Lane Margining at Receiver (LMR) support
This patch introduces support for PCIe Lane Margining at Receiver (LMR). It adds debugfs controls to allow manual execution of timing and voltage margin steps on capable receivers, along with basic state management.
drivers/pci/pci-driver.c static int pci_pm_suspend(struct device *dev) · line 827
Quoted 10 lines
drivers/pci/pci-driver.c +1
-0@@ -821,6 +821,7 @@ static int pci_pm_suspend(struct device *dev)* since Coffee Lake, to enter a lower-power PM state. */ pci_suspend_ptm (pci_dev); +pci_suspend_lmr (pci_dev);if (pci_has_legacy_pm_support (pci_dev))return pci_legacy_suspend (dev,PMSG_SUSPEND );
[Severity: Medium]
Does this miss hibernation (S4) support?
The LMR suspend is hooked into pci_pm_suspend() for S3 sleep, but pci_pm_freeze() and pci_pm_poweroff() don't appear to call pci_suspend_lmr(). If the system hibernates, the device loses power and the margining state resets, but upon resume, mdev->enabled would still be true, leaving ASPM tracking desynchronized and a leaked runtime PM reference.
drivers/pci/pci.c int __pci_reset_function_locked(struct pci_dev *dev) · line 5153
Quoted 13 lines
drivers/pci/pci.c +3
-1@@ -5145,8 +5145,10 @@ int __pci_reset_function_locked(struct pci_dev *dev) method = &pci_reset_fn_methods[m];pci_dbg (dev,"reset via %s\n" , method->name); rc = method->reset_fn (dev,PCI_RESET_DO_RESET ); -if (!rc) +if (!rc) { +pci_reset_lmr (dev);return 0 ; + }pci_dbg (dev,"%s failed with %d\n" , method->name, rc);
[Severity: High]
Will the ASPM restoration in pci_reset_lmr() be immediately clobbered here?
The pci_reset_function() sequence calls pci_save_state() before this, and pci_restore_state() afterwards. If pci_reset_lmr() restores the ASPM hardware state to its original configuration, won't pci_restore_state() subsequently blindly overwrite it with the saved config space (where ASPM was disabled by LMR), leaving ASPM permanently disabled?
[Severity: Medium]
By hooking LMR reset cleanup exclusively here in __pci_reset_function_locked(), do we miss Secondary Bus Resets (SBR)?
If a root user triggers a bus reset via the parent bridge, the downstream devices are reset using a different path. The hardware LMR state would be wiped, but pci_reset_lmr() is never invoked, leaving mdev->enabled as true and failing to drop the runtime PM reference.
Quoted 2 lines
drivers/pci/pcie/margin.c +0
-0
[ ... ]
drivers/pci/pcie/margin.c static ssize_t margin_enable_write(struct file *file, const char __user *user_buf, · line 568
Quoted 13 lines
drivers/pci/pcie/margin.c +9
-0@@ -557,6 +557,13 @@ static ssize_t margin_enable_write(struct file *file, const char __user *user_buf, mdev->enabled = true;return count; +err_sw_ready: +if (cap &PCI_LMR_PORT_CAP_USES_SW_READY ) { + ret =pci_read_config_word (dev, mdev->cap +PCI_LMR_PORT_STS , &sts); +if (ret ==PCIBIOS_SUCCESSFUL ) { + sts &= ~PCI_LMR_PORT_STS_SW_READY ; +pci_write_config_word (dev, mdev->cap +PCI_LMR_PORT_STS , sts); + } + } +err_aspm:
[Severity: Medium]
Does this error cleanup path accidentally clobber the original negative error code?
If an error like -ETIMEDOUT occurs above, the jump to err_sw_ready issues a pci_read_config_word(). If that read succeeds, ret is overwritten with PCIBIOS_SUCCESSFUL (0). The function will then silently return 0 instead of propagating the actual failure to userspace.
[ ... ]
drivers/pci/pcie/margin.c static ssize_t margin_lane_step_write(struct file *file, const char __user *user_buf, · line 740
Quoted 10 lines
drivers/pci/pcie/margin.c +6
-0@@ -732,6 +732,10 @@ static ssize_t margin_lane_step_write(struct file *file, const char __user *user_buf,if (!mdev->enabled)return -EBUSY ; +if (val ==0 ) { + ret =pci_lmr_demargin_lane (plane); +return ret ? ret : count; + } + + ret =pci_lmr_cache_rx_info (plane, plane->rx);
[Severity: Medium]
Are we missing an automatic demargin when changing margin directions?
The code issues a demargin command if the requested value is exactly 0, but if a user sequentially writes a positive offset and then a negative offset (or vice versa), the driver issues the new step command immediately. Per the PCIe Base Specification, software must issue a Demargin (or Clear Margin) command before changing margin directions to avoid undefined behavior or NAKs.
[ ... ]
drivers/pci/pcie/margin.c static ssize_t margin_lane_step_write(struct file *file, const char __user *user_buf,
Quoted 10 lines
drivers/pci/pcie/margin.c +0
-0@@ -754,6 +758,11 @@ static ssize_t margin_lane_step_write(struct file *file, const char __user *user_buf,if (val <0 ) {if (!(caps &LMR_CAP_IND_LEFT_RIGHT_TIMING ))return -EINVAL ; step = -val; dir =LMR_STEP_DIR_DECREASE ; }else { step = val; dir =LMR_STEP_DIR_INCREASE ; }
[Severity: Medium]
Does this write 1 to a reserved direction bit on symmetric-only receivers?
For positive margins, the code unconditionally assigns
dir = LMR_STEP_DIR_INCREASE (1). If the receiver doesn't support independent left/right or up/down margining, the PCIe Base Specification designates the direction bits as Reserved, so they should be written as 0.
[ ... ]
Quoted 4 lines
drivers/pci/pcie/margin.c +0
-0@@ -983,6 +983,16 @@ void pci_lmr_exit(struct pci_dev *dev) {struct pci_margin_dev *mdev = dev->lmr;
drivers/pci/pcie/margin.c void pci_lmr_exit(struct pci_dev *dev) · line 995
Quoted 10 lines
drivers/pci/pcie/margin.c +9
-0+if (!dev || !mdev) +return ; + +pci_suspend_lmr (dev); + +pci_margin_debugfs_remove (mdev); +mutex_destroy (&mdev->lock); +kfree (mdev); + dev->lmr =NULL ; }
[Severity: High]
Does this teardown ordering allow concurrent debugfs writes to leak a runtime PM reference?
By calling pci_suspend_lmr() before pci_margin_debugfs_remove(), a concurrent write to the enable file could be blocked on mdev->lock. Once pci_suspend_lmr() sets enabled = false and releases the lock, the write thread wakes up, sees enabled is false, enables LMR again, acquires a runtime PM reference, and disables ASPM. Then pci_margin_debugfs_remove() completes and the structure is freed, abandoning the newly created hardware and RPM state.
[ ... ]
drivers/pci/pcie/margin.c void pci_reset_lmr(struct pci_dev *dev) · line 1047
Quoted 10 lines
drivers/pci/pcie/margin.c +5
-0@@ -1029,6 +1039,11 @@ void pci_reset_lmr(struct pci_dev *dev) {struct pci_margin_dev *mdev = dev->lmr; +if (!dev || !mdev) +return ; + +guard (mutex)(&mdev->lock); +pci_lmr_reset_software_state_locked (mdev); }
[Severity: High]
Can a concurrent device removal and sysfs reset trigger a use-after-free here?
In pci_lmr_exit(), mdev is freed and dev->lmr is set to NULL without holding the device_lock. If a concurrent sysfs reset triggers pci_reset_function() (which acquires device_lock) and calls pci_reset_lmr(), it might read the dev->lmr pointer just before pci_lmr_exit() nullifies it. It would then attempt to lock the mutex of the already freed mdev structure.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1