Skip to contentSkip to conversation

PCI/sysfs: document the link speed and width attributes

PATCH Superseded Ferran Duarri 4 participants 9 messages Aug 20 – Aug 21

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

PATCH 1/1 PCI/sysfs: document the link speed and width attributes

max_link_speed, max_link_width, current_link_speed and current_link_width have been exported under /sys/bus/pci/devices/.../ since 2018, by
commit 56c1af4606f0 ("PCI: Add sysfs max_link_speed/width, current_link_speed/width, etc"), and none of the four appear anywhere in Documentation/ABI.

The gap matters most for current_link_speed. current_link_speed_show() performs a fresh PCI_EXP_LNKSTA read on every open, so the value reflects the link state at that instant. Modern GPUs retrain their link continuously as part of idle power management, which means a single read can legitimately return any speed the link supports, not the speed the link will use under load.

Observed on an RTX 5070 in a PCIe 4.0 x16 slot, same boot, no configuration change between the two reads: 5.0 GT/s while idle, 16.0 GT/s under load. Comparing current_link_speed against max_link_speed at idle is therefore not a valid test for a degraded link, though it reads like one.

Document all four attributes, note that the max_* pair is the ceiling negotiated between device and platform capability (so an endpoint's own capability may be higher than what max_link_speed reports), and state explicitly that current_link_speed is instantaneous, that comparing it against max_link_speed at idle is not a valid degradation test, and that callers wanting a stable figure should sample under load or use the max_* attributes.

No functional change.

Forward-Port-Notes: Documentation/ABI/testing/sysfs-bus-pci

---
 Documentation/ABI/testing/sysfs-bus-pci | 65 +++++++++++++++++++++++++
 1 file changed, 65 insertions(+)
Documentation/ABI/testing/sysfs-bus-pci +65 -0
index b767db2..ad18bd6 100644
@@ -174,6 +174,71 @@ Description:
 		similiar to writing 1 to their individual "reset" file, so use
 		with caution.
 
+What:		/sys/bus/pci/devices/.../max_link_speed
+Date:		September 2018
+Contact:	[email protected]
+Description:
+		The maximum link speed this device's link can operate at, as a
+		human-readable string such as "16.0 GT/s PCIe". Read from the
+		Max Link Speed field of the device's Link Capabilities register.
+
+		This is the ceiling the link may negotiate, which is the lower
+		of what the two ends of the link support. An endpoint capable of
+		a higher speed than the port above it will report that higher
+		speed here while the port above it reports the lower one, and
+		the link will train at the lower of the two. To reason about a
+		link, read this attribute on both ends.
+
+		Present only for PCI Express devices.
+
+What:		/sys/bus/pci/devices/.../max_link_width
+Date:		September 2018
+Contact:	[email protected]
+Description:
+		The maximum link width this device's link can operate at, in
+		lanes, e.g. "16". Read from the Maximum Link Width field of the
+		device's Link Capabilities register.
+
+		Present only for PCI Express devices.
+
+What:		/sys/bus/pci/devices/.../current_link_speed
+Date:		September 2018
+Contact:	[email protected]
+Description:
+		The speed the link is operating at right now, as a
+		human-readable string such as "16.0 GT/s PCIe". Read fresh from
+		the device's Link Status register on every read of this file;
+		nothing is cached.
+
+		This value is instantaneous and may change at any time. A link
+		is permitted to retrain to a lower speed and back, and devices
+		with aggressive link power management (GPUs in particular) do so
+		routinely while idle. Two reads seconds apart, with no
+		configuration change in between, can legitimately differ by
+		several generations.
+
+		Consequently, comparing this attribute against max_link_speed is
+		not by itself a test for a degraded link: an idle device will
+		frequently report a lower speed and is working correctly.
+		Callers that need a figure representing what the link will
+		actually deliver should sample while the device is under load,
+		or use max_link_speed if what they want is the ceiling.
+
+		Present only for PCI Express devices.
+
+What:		/sys/bus/pci/devices/.../current_link_width
+Date:		September 2018
+Contact:	[email protected]
+Description:
+		The width the link is operating at right now, in lanes, e.g.
+		"16". Read fresh from the device's Link Status register on every
+		read of this file.
+
+		As with current_link_speed, this is instantaneous. Links may
+		also narrow and re-widen under link power management.
+
+		Present only for PCI Express devices.
+
 What:		/sys/bus/pci/devices/.../vpd
 Date:		February 2008
 Contact:	Ben Hutchings <[email protected]>
#1 lore raw
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Medium] The added documentation incorrectly describes `max_link_speed` and `max_link_width` as the…

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] The added documentation incorrectly describes max_link_speed and max_link_width as the negotiated link ceiling (the minimum of what both ends support), which contradicts the underlying implementation. The max_link_speed_show() and max_link_width_show() sysfs callbacks return the device's own capabilities, not the negotiated link ceiling. The documentation for max_link_speed contradicts its own first sentence by subsequently acknowledging the endpoint will report its own higher speed. Furthermore, the current_link_speed documentation incorrectly advises users to "use max_link_speed if what they want is the ceiling" of what the link will actually deliver, which can lead user-space tools to overestimate the link capacity if the upstream port is slower. Lastly, the claim that the speed is "Read from the Max Link Speed field of the device's Link Capabilities register" is outdated; since PCIe r3.0, the kernel determines this primarily via the Supported Link Speeds Vector in the Link Capabilities 2 Register (PCI_EXP_LNKCAP2). --

--- Patch [1]: [PATCH] PCI/sysfs: document the link speed and width attributes --- commit 24d4206c0d1e19c29d8e3f471fd2f8be01ffaa11
Author: Ferran Duarri <[email protected]>

PCI/sysfs: document the link speed and width attributes

This commit documents the max_link_speed, max_link_width, current_link_speed, and current_link_width sysfs attributes. It clarifies that current link attributes represent instantaneous values and should be sampled under load.

Quoted 5 lines
Documentation/ABI/testing/sysfs-bus-pci +0 -0
index b767db2c52cb7..ad18bd634ee6a 100644

Documentation/ABI/testing/sysfs-bus-pci Description: · line 183

Quoted 10 lines
Documentation/ABI/testing/sysfs-bus-pci +7 -0
@@ -174,6 +174,71 @@ Description:
 		similiar to writing 1 to their individual "reset" file, so use
 		with caution.
 
+What:		/sys/bus/pci/devices/.../max_link_speed
+Date:		September 2018
+Contact:	[email protected]
+Description:
+		The maximum link speed this device's link can operate at, as a
+		human-readable string such as "16.0 GT/s PCIe". Read from the
+		Max Link Speed field of the device's Link Capabilities register.

[Severity: Medium]
Is the claim that this is read from the Max Link Speed field of the Link Capabilities register fully accurate?

Since PCIe r3.0, the kernel primarily determines this capability via the Supported Link Speeds Vector in the Link Capabilities 2 Register (PCI_EXP_LNKCAP2).

Documentation/ABI/testing/sysfs-bus-pci Description: · line 190

Quoted 7 lines
Documentation/ABI/testing/sysfs-bus-pci +7 -0
+
+		This is the ceiling the link may negotiate, which is the lower
+		of what the two ends of the link support. An endpoint capable of
+		a higher speed than the port above it will report that higher
+		speed here while the port above it reports the lower one, and
+		the link will train at the lower of the two. To reason about a
+		link, read this attribute on both ends.

[Severity: Medium]
Does this description contradict itself and the sysfs implementation?

The documentation states this is the "ceiling the link may negotiate, which is the lower of what the two ends of the link support", but then acknowledges the endpoint will report its own higher capability.

Because the max_link_speed_show() and max_link_width_show() sysfs callbacks return the device's own capabilities rather than the negotiated link ceiling, calling it the "ceiling the link may negotiate" could cause user-space tools to overestimate link capacities.

[ ... ]

Quoted 12 lines
Documentation/ABI/testing/sysfs-bus-pci +12 -0
+What:		/sys/bus/pci/devices/.../current_link_speed
+Date:		September 2018
+Contact:	[email protected]
+Description:
+		The speed the link is operating at right now, as a
+		human-readable string such as "16.0 GT/s PCIe". Read fresh from
+		the device's Link Status register on every read of this file;
+		nothing is cached.
+
+		This value is instantaneous and may change at any time. A link
+		is permitted to retrain to a lower speed and back, and devices
+		with aggressive link power management (GPUs in particular) do so

Documentation/ABI/testing/sysfs-bus-pci Description: · line 225

Quoted 10 lines
Documentation/ABI/testing/sysfs-bus-pci +10 -0
+		routinely while idle. Two reads seconds apart, with no
+		configuration change in between, can legitimately differ by
+		several generations.
+
+		Consequently, comparing this attribute against max_link_speed is
+		not by itself a test for a degraded link: an idle device will
+		frequently report a lower speed and is working correctly.
+		Callers that need a figure representing what the link will
+		actually deliver should sample while the device is under load,
+		or use max_link_speed if what they want is the ceiling.

[Severity: Medium]
Is it correct to advise users to use max_link_speed if they want the link's functional ceiling?

If the upstream port is slower than the device, relying on the device's max_link_speed will overestimate what the link will actually deliver. This could lead user-space tools to incorrectly evaluate the available capacity.

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

PATCH v2 1/1 PCI/sysfs: document the link speed and width attributes

In reply to Ferran Duarri

max_link_speed, max_link_width, current_link_speed and current_link_width have been exported under /sys/bus/pci/devices/.../ since 2018, by
commit 56c1af4606f0 ("PCI: Add sysfs max_link_speed/width, current_link_speed/width, etc"), and none of the four appear anywhere in Documentation/ABI.

The gap matters most for current_link_speed. current_link_speed_show() performs a fresh PCI_EXP_LNKSTA read on every open, so the value reflects the link state at that instant. Modern GPUs retrain their link continuously as part of idle power management, which means a single read can legitimately return any speed the link supports, not the speed the link will use under load.

Observed on an RTX 5070 in a PCIe 4.0 x16 slot, same boot, no configuration change between the two reads: 5.0 GT/s while idle, 16.0 GT/s under load. Comparing current_link_speed against max_link_speed at idle is therefore not a valid test for a degraded link, though it reads like one.

Document all four. For the max_* pair, state that each reports the capability of the device it is read from and not a property of the link: a link trains at the lower of what its two ends support, so an endpoint capable of more than the port above it reports the higher figure while that port reports the lower one. Record where each value comes from, which differs between the two attributes. max_link_speed is derived from the Supported Link Speeds Vector in Link Capabilities 2, capped by Max Link Speed in Link Capabilities, synthesized from the latter alone on devices predating PCIe r3.0, and cached at enumeration. max_link_width is read from Maximum Link Width in Link Capabilities on each access.

For current_link_speed, state that it is instantaneous, that comparing it against max_link_speed at idle is not a valid degradation test, and that callers wanting what the link will actually deliver should sample under load -- noting that max_link_speed is not that figure either, being one end's capability rather than the link's.

No functional change.

---
Changes in v2, all corrections to what v1 claimed rather than new material:

- max_link_speed: v1 called it "the ceiling the link may negotiate, which is the lower of what the two ends of the link support". That is wrong, and v1 contradicted it one sentence later. max_link_speed_show() calls pcie_get_speed_cap(), which returns the capability of the device being read and never consults the other end of the link.
- max_link_speed: v1 said the value is read from the Max Link Speed field of Link Capabilities. pcie_get_supported_speeds() derives it from the Supported Link Speeds Vector in Link Capabilities 2, masks it against Max Link Speed, and synthesizes from Max Link Speed alone only on devices predating PCIe r3.0.
- max_link_speed: v1 did not say the value is read once at enumeration and cached in pci_dev->supported_speeds. Since the current_link_speed entry states that nothing is cached there, a reader could reasonably infer the same of max_link_speed. It does not hold.
- current_link_speed: v1 advised callers wanting the ceiling to use max_link_speed. That overestimates whenever the upstream port is the slower end. v2 says to sample under load and warns that max_link_speed is one end's capability, not the link's.
- max_link_width: register attribution was correct and is unchanged in substance, reworded only for the same device-versus-link distinction.
- Dropped a private Forward-Port-Notes: trailer that should not have been in the commit message.

 Documentation/ABI/testing/sysfs-bus-pci | 78 +++++++++++++++++++++++++
 1 file changed, 78 insertions(+)
Documentation/ABI/testing/sysfs-bus-pci +78 -0
index b767db2c52cb..ee1846f3dafa 100644
@@ -174,6 +174,84 @@ Description:
 		similiar to writing 1 to their individual "reset" file, so use
 		with caution.
 
+What:		/sys/bus/pci/devices/.../max_link_speed
+Date:		September 2018
+Contact:	[email protected]
+Description:
+		The maximum link speed this device is capable of, as a
+		human-readable string such as "16.0 GT/s PCIe".
+
+		Derived from the Supported Link Speeds Vector in the device's
+		Link Capabilities 2 register, capped by the Max Link Speed
+		field in Link Capabilities. Devices predating PCIe r3.0 have no
+		Link Capabilities 2, and for those the value is synthesized
+		from Max Link Speed alone. Read once during enumeration and
+		cached thereafter, so unlike current_link_speed it does not
+		change between reads.
+
+		This is the device's own capability, not a property of the
+		link. A link trains at the lower of what its two ends support,
+		so an endpoint capable of a higher speed than the port above it
+		reports that higher speed here while the port reports the lower
+		one. Reading one end therefore does not tell you what the link
+		will do; read both ends and take the lower.
+
+		Present only for PCI Express devices.
+
+What:		/sys/bus/pci/devices/.../max_link_width
+Date:		September 2018
+Contact:	[email protected]
+Description:
+		The maximum link width this device is capable of, in lanes,
+		e.g. "16". Read from the Maximum Link Width field of the
+		device's Link Capabilities register.
+
+		As with max_link_speed this is the device's own capability, not
+		a property of the link; a link trains at the lower of what its
+		two ends support.
+
+		Present only for PCI Express devices.
+
+What:		/sys/bus/pci/devices/.../current_link_speed
+Date:		September 2018
+Contact:	[email protected]
+Description:
+		The speed the link is operating at right now, as a
+		human-readable string such as "16.0 GT/s PCIe". Read fresh from
+		the device's Link Status register on every read of this file;
+		nothing is cached.
+
+		This value is instantaneous and may change at any time. A link
+		is permitted to retrain to a lower speed and back, and devices
+		with aggressive link power management (GPUs in particular) do so
+		routinely while idle. Two reads seconds apart, with no
+		configuration change in between, can legitimately differ by
+		several generations.
+
+		Consequently, comparing this attribute against max_link_speed is
+		not by itself a test for a degraded link: an idle device will
+		frequently report a lower speed and is working correctly.
+		Callers that need a figure representing what the link will
+		actually deliver should sample while the device is under load.
+		max_link_speed is not that figure either: it reports one end's
+		capability, and the link is limited by the lower of its two
+		ends.
+
+		Present only for PCI Express devices.
+
+What:		/sys/bus/pci/devices/.../current_link_width
+Date:		September 2018
+Contact:	[email protected]
+Description:
+		The width the link is operating at right now, in lanes, e.g.
+		"16". Read fresh from the device's Link Status register on every
+		read of this file.
+
+		As with current_link_speed, this is instantaneous. Links may
+		also narrow and re-widen under link power management.
+
+		Present only for PCI Express devices.
+
 What:		/sys/bus/pci/devices/.../vpd
 Date:		February 2008
 Contact:	Ben Hutchings <[email protected]>
Signature · 1 line
2.53.0
#3 lore raw
Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 18 lines

max_link_speed, max_link_width, current_link_speed and current_link_width have been exported under /sys/bus/pci/devices/.../ since 2018, by
commit 56c1af4606f0 ("PCI: Add sysfs max_link_speed/width, current_link_speed/width, etc"), and none of the four appear anywhere in Documentation/ABI.

The gap matters most for current_link_speed. current_link_speed_show() performs a fresh PCI_EXP_LNKSTA read on every open, so the value reflects the link state at that instant. Modern GPUs retrain their link continuously as part of idle power management, which means a single read can legitimately return any speed the link supports, not the speed the link will use under load.

Observed on an RTX 5070 in a PCIe 4.0 x16 slot, same boot, no configuration change between the two reads: 5.0 GT/s while idle, 16.0 GT/s under load. Comparing current_link_speed against max_link_speed at idle is therefore not [ ... ]
end's capability rather than the link's.

Quoted 3 lines

No functional change.

Sashiko has reviewed this patch and found no issues. It looks great!

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

PATCH v2 1/1 PCI/sysfs: document the link speed and width attributes

In reply to Ferran Duarri

max_link_speed, max_link_width, current_link_speed and current_link_width have been exported under /sys/bus/pci/devices/.../ since 2018, by
commit 56c1af4606f0 ("PCI: Add sysfs max_link_speed/width, current_link_speed/width, etc"), and none of the four appear anywhere in Documentation/ABI.

The gap matters most for current_link_speed. current_link_speed_show() performs a fresh PCI_EXP_LNKSTA read on every open, so the value reflects the link state at that instant. Modern GPUs retrain their link continuously as part of idle power management, which means a single read can legitimately return any speed the link supports, not the speed the link will use under load.

Observed on an RTX 5070 in a PCIe 4.0 x16 slot, same boot, no configuration change between the two reads: 5.0 GT/s while idle, 16.0 GT/s under load. Comparing current_link_speed against max_link_speed at idle is therefore not a valid test for a degraded link, though it reads like one.

Document all four. For the max_* pair, state that each reports the capability of the device it is read from and not a property of the link: a link trains at the lower of what its two ends support, so an endpoint capable of more than the port above it reports the higher figure while that port reports the lower one. Record where each value comes from, which differs between the two attributes. max_link_speed is derived from the Supported Link Speeds Vector in Link Capabilities 2, capped by Max Link Speed in Link Capabilities, synthesized from the latter alone on devices predating PCIe r3.0, and cached at enumeration. max_link_width is read from Maximum Link Width in Link Capabilities on each access.

For current_link_speed, state that it is instantaneous, that comparing it against max_link_speed at idle is not a valid degradation test, and that callers wanting what the link will actually deliver should sample under load -- noting that max_link_speed is not that figure either, being one end's capability rather than the link's.

No functional change.

---
Changes in v2, all corrections to what v1 claimed rather than new material:

- max_link_speed: v1 called it "the ceiling the link may negotiate, which is the lower of what the two ends of the link support". That is wrong, and v1 contradicted it one sentence later. max_link_speed_show() calls pcie_get_speed_cap(), which returns the capability of the device being read and never consults the other end of the link.
- max_link_speed: v1 said the value is read from the Max Link Speed field of Link Capabilities. pcie_get_supported_speeds() derives it from the Supported Link Speeds Vector in Link Capabilities 2, masks it against Max Link Speed, and synthesizes from Max Link Speed alone only on devices predating PCIe r3.0.
- max_link_speed: v1 did not say the value is read once at enumeration and cached in pci_dev->supported_speeds. Since the current_link_speed entry states that nothing is cached there, a reader could reasonably infer the same of max_link_speed. It does not hold.
- current_link_speed: v1 advised callers wanting the ceiling to use max_link_speed. That overestimates whenever the upstream port is the slower end. v2 says to sample under load and warns that max_link_speed is one end's capability, not the link's.
- max_link_width: register attribution was correct and is unchanged in substance, reworded only for the same device-versus-link distinction.
- Dropped a private Forward-Port-Notes: trailer that should not have been in the commit message.

 Documentation/ABI/testing/sysfs-bus-pci | 78 +++++++++++++++++++++++++
 1 file changed, 78 insertions(+)
Documentation/ABI/testing/sysfs-bus-pci +78 -0
index b767db2c52cb..ee1846f3dafa 100644
@@ -174,6 +174,84 @@ Description:
 		similiar to writing 1 to their individual "reset" file, so use
 		with caution.
 
+What:		/sys/bus/pci/devices/.../max_link_speed
+Date:		September 2018
+Contact:	[email protected]
+Description:
+		The maximum link speed this device is capable of, as a
+		human-readable string such as "16.0 GT/s PCIe".
+
+		Derived from the Supported Link Speeds Vector in the device's
+		Link Capabilities 2 register, capped by the Max Link Speed
+		field in Link Capabilities. Devices predating PCIe r3.0 have no
+		Link Capabilities 2, and for those the value is synthesized
+		from Max Link Speed alone. Read once during enumeration and
+		cached thereafter, so unlike current_link_speed it does not
+		change between reads.
+
+		This is the device's own capability, not a property of the
+		link. A link trains at the lower of what its two ends support,
+		so an endpoint capable of a higher speed than the port above it
+		reports that higher speed here while the port reports the lower
+		one. Reading one end therefore does not tell you what the link
+		will do; read both ends and take the lower.
+
+		Present only for PCI Express devices.
+
+What:		/sys/bus/pci/devices/.../max_link_width
+Date:		September 2018
+Contact:	[email protected]
+Description:
+		The maximum link width this device is capable of, in lanes,
+		e.g. "16". Read from the Maximum Link Width field of the
+		device's Link Capabilities register.
+
+		As with max_link_speed this is the device's own capability, not
+		a property of the link; a link trains at the lower of what its
+		two ends support.
+
+		Present only for PCI Express devices.
+
+What:		/sys/bus/pci/devices/.../current_link_speed
+Date:		September 2018
+Contact:	[email protected]
+Description:
+		The speed the link is operating at right now, as a
+		human-readable string such as "16.0 GT/s PCIe". Read fresh from
+		the device's Link Status register on every read of this file;
+		nothing is cached.
+
+		This value is instantaneous and may change at any time. A link
+		is permitted to retrain to a lower speed and back, and devices
+		with aggressive link power management (GPUs in particular) do so
+		routinely while idle. Two reads seconds apart, with no
+		configuration change in between, can legitimately differ by
+		several generations.
+
+		Consequently, comparing this attribute against max_link_speed is
+		not by itself a test for a degraded link: an idle device will
+		frequently report a lower speed and is working correctly.
+		Callers that need a figure representing what the link will
+		actually deliver should sample while the device is under load.
+		max_link_speed is not that figure either: it reports one end's
+		capability, and the link is limited by the lower of its two
+		ends.
+
+		Present only for PCI Express devices.
+
+What:		/sys/bus/pci/devices/.../current_link_width
+Date:		September 2018
+Contact:	[email protected]
+Description:
+		The width the link is operating at right now, in lanes, e.g.
+		"16". Read fresh from the device's Link Status register on every
+		read of this file.
+
+		As with current_link_speed, this is instantaneous. Links may
+		also narrow and re-widen under link power management.
+
+		Present only for PCI Express devices.
+
 What:		/sys/bus/pci/devices/.../vpd
 Date:		February 2008
 Contact:	Ben Hutchings <[email protected]>
Signature · 1 line
2.53.0
#5 lore raw
Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 18 lines

max_link_speed, max_link_width, current_link_speed and current_link_width have been exported under /sys/bus/pci/devices/.../ since 2018, by
commit 56c1af4606f0 ("PCI: Add sysfs max_link_speed/width, current_link_speed/width, etc"), and none of the four appear anywhere in Documentation/ABI.

The gap matters most for current_link_speed. current_link_speed_show() performs a fresh PCI_EXP_LNKSTA read on every open, so the value reflects the link state at that instant. Modern GPUs retrain their link continuously as part of idle power management, which means a single read can legitimately return any speed the link supports, not the speed the link will use under load.

Observed on an RTX 5070 in a PCIe 4.0 x16 slot, same boot, no configuration change between the two reads: 5.0 GT/s while idle, 16.0 GT/s under load. Comparing current_link_speed against max_link_speed at idle is therefore not [ ... ]
end's capability rather than the link's.

Quoted 3 lines

No functional change.

Sashiko has reviewed this patch and found no issues. It looks great!

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
#6 lore raw
In reply to Ferran Duarri
Quoted 34 lines · Ferran Duarri

max_link_speed, max_link_width, current_link_speed and current_link_width have been exported under /sys/bus/pci/devices/.../ since 2018, by
commit 56c1af4606f0 ("PCI: Add sysfs max_link_speed/width, current_link_speed/width, etc"), and none of the four appear anywhere in Documentation/ABI.

The gap matters most for current_link_speed. current_link_speed_show() performs a fresh PCI_EXP_LNKSTA read on every open, so the value reflects the link state at that instant. Modern GPUs retrain their link continuously as part of idle power management, which means a single read can legitimately return any speed the link supports, not the speed the link will use under load.

Observed on an RTX 5070 in a PCIe 4.0 x16 slot, same boot, no configuration change between the two reads: 5.0 GT/s while idle, 16.0 GT/s under load. Comparing current_link_speed against max_link_speed at idle is therefore not a valid test for a degraded link, though it reads like one.

Document all four. For the max_* pair, state that each reports the capability of the device it is read from and not a property of the link: a link trains at the lower of what its two ends support, so an endpoint capable of more than the port above it reports the higher figure while that port reports the lower one. Record where each value comes from, which differs between the two attributes. max_link_speed is derived from the Supported Link Speeds Vector in Link Capabilities 2, capped by Max Link Speed in Link Capabilities, synthesized from the latter alone on devices predating PCIe r3.0, and cached at enumeration. max_link_width is read from Maximum Link Width in Link Capabilities on each access.

For current_link_speed, state that it is instantaneous, that comparing it against max_link_speed at idle is not a valid degradation test, and that callers wanting what the link will actually deliver should sample under load -- noting that max_link_speed is not that figure either, being one end's capability rather than the link's.

Quoted 3 lines

No functional change.

Did you forget the Assisted-by: tag?

Quoted 2 lines

---
Changes in v2, all corrections to what v1 claimed rather than new material:

You sent 2 v2 patches :(

#7 lore raw
In reply to Ferran Duarri
Quoted 76 lines · Ferran Duarri

max_link_speed, max_link_width, current_link_speed and current_link_width have been exported under /sys/bus/pci/devices/.../ since 2018, by
commit 56c1af4606f0 ("PCI: Add sysfs max_link_speed/width, current_link_speed/width, etc"), and none of the four appear anywhere in Documentation/ABI.

The gap matters most for current_link_speed. current_link_speed_show() performs a fresh PCI_EXP_LNKSTA read on every open, so the value reflects the link state at that instant. Modern GPUs retrain their link continuously as part of idle power management, which means a single read can legitimately return any speed the link supports, not the speed the link will use under load.

Observed on an RTX 5070 in a PCIe 4.0 x16 slot, same boot, no configuration change between the two reads: 5.0 GT/s while idle, 16.0 GT/s under load. Comparing current_link_speed against max_link_speed at idle is therefore not a valid test for a degraded link, though it reads like one.

Document all four. For the max_* pair, state that each reports the capability of the device it is read from and not a property of the link: a link trains at the lower of what its two ends support, so an endpoint capable of more than the port above it reports the higher figure while that port reports the lower one. Record where each value comes from, which differs between the two attributes. max_link_speed is derived from the Supported Link Speeds Vector in Link Capabilities 2, capped by Max Link Speed in Link Capabilities, synthesized from the latter alone on devices predating PCIe r3.0, and cached at enumeration. max_link_width is read from Maximum Link Width in Link Capabilities on each access.

For current_link_speed, state that it is instantaneous, that comparing it against max_link_speed at idle is not a valid degradation test, and that callers wanting what the link will actually deliver should sample under load -- noting that max_link_speed is not that figure either, being one end's capability rather than the link's.

No functional change.

---
Changes in v2, all corrections to what v1 claimed rather than new material:

- max_link_speed: v1 called it "the ceiling the link may negotiate, which is the lower of what the two ends of the link support". That is wrong, and v1 contradicted it one sentence later. max_link_speed_show() calls pcie_get_speed_cap(), which returns the capability of the device being read and never consults the other end of the link.
- max_link_speed: v1 said the value is read from the Max Link Speed field of Link Capabilities. pcie_get_supported_speeds() derives it from the Supported Link Speeds Vector in Link Capabilities 2, masks it against Max Link Speed, and synthesizes from Max Link Speed alone only on devices predating PCIe r3.0.
- max_link_speed: v1 did not say the value is read once at enumeration and cached in pci_dev->supported_speeds. Since the current_link_speed entry states that nothing is cached there, a reader could reasonably infer the same of max_link_speed. It does not hold.
- current_link_speed: v1 advised callers wanting the ceiling to use max_link_speed. That overestimates whenever the upstream port is the slower end. v2 says to sample under load and warns that max_link_speed is one end's capability, not the link's.
- max_link_width: register attribution was correct and is unchanged in substance, reworded only for the same device-versus-link distinction.
- Dropped a private Forward-Port-Notes: trailer that should not have been in the commit message.

 Documentation/ABI/testing/sysfs-bus-pci | 78 +++++++++++++++++++++++++
 1 file changed, 78 insertions(+)
Documentation/ABI/testing/sysfs-bus-pci +2 -0
index b767db2c52cb..ee1846f3dafa 100644
@@ -174,6 +174,84 @@ Description:
 		similiar to writing 1 to their individual "reset" file, so use
 		with caution.
 
+What:		/sys/bus/pci/devices/.../max_link_speed
+Date:		September 2018

Documentation/ABI/testing/sysfs-bus-pci Description: · line 188

Quoted 10 lines
Documentation/ABI/testing/sysfs-bus-pci +10 -0
+Contact:	[email protected]
+Description:
+		The maximum link speed this device is capable of, as a
+		human-readable string such as "16.0 GT/s PCIe".
+
+		Derived from the Supported Link Speeds Vector in the device's
+		Link Capabilities 2 register, capped by the Max Link Speed
+		field in Link Capabilities. Devices predating PCIe r3.0 have no
+		Link Capabilities 2, and for those the value is synthesized
+		from Max Link Speed alone.

IMO it's unnecessary detail to say it's synthetized. For <= PCIe r3.0, kernel does indeed synthetize something, but that "supported speeds" which is a set of values. pcie_get_speed_cap() used in max_link_speed_show() only returns the max value out of that set which, for sane devices, should be same as Max Link Speed.

Quoted 41 lines

Read once during enumeration and

Documentation/ABI/testing/sysfs-bus-pci +40 -0
+		cached thereafter, so unlike current_link_speed it does not
+		change between reads.
+
+		This is the device's own capability, not a property of the
+		link. A link trains at the lower of what its two ends support,
+		so an endpoint capable of a higher speed than the port above it
+		reports that higher speed here while the port reports the lower
+		one. Reading one end therefore does not tell you what the link
+		will do; read both ends and take the lower.
+
+		Present only for PCI Express devices.
+
+What:		/sys/bus/pci/devices/.../max_link_width
+Date:		September 2018
+Contact:	[email protected]
+Description:
+		The maximum link width this device is capable of, in lanes,
+		e.g. "16". Read from the Maximum Link Width field of the
+		device's Link Capabilities register.
+
+		As with max_link_speed this is the device's own capability, not
+		a property of the link; a link trains at the lower of what its
+		two ends support.
+
+		Present only for PCI Express devices.
+
+What:		/sys/bus/pci/devices/.../current_link_speed
+Date:		September 2018
+Contact:	[email protected]
+Description:
+		The speed the link is operating at right now, as a
+		human-readable string such as "16.0 GT/s PCIe". Read fresh from
+		the device's Link Status register on every read of this file;
+		nothing is cached.
+
+		This value is instantaneous and may change at any time. A link
+		is permitted to retrain to a lower speed and back, and devices
+		with aggressive link power management (GPUs in particular) do so
+		routinely while idle. Two reads seconds apart, with no
+		configuration change in between, can legitimately differ by

Documentation/ABI/testing/sysfs-bus-pci Description: · line 238

Quoted 10 lines
Documentation/ABI/testing/sysfs-bus-pci +10 -0
+		several generations.
+
+		Consequently, comparing this attribute against max_link_speed is
+		not by itself a test for a degraded link: an idle device will
+		frequently report a lower speed and is working correctly.
+		Callers that need a figure representing what the link will
+		actually deliver should sample while the device is under load.
+		max_link_speed is not that figure either: it reports one end's
+		capability, and the link is limited by the lower of its two
+		ends.

The speed may also be capped by Target Link Speed in Link Control 2 that is under control of PCIe BW Controller (bwctrl).

Quoted 20 lines
Documentation/ABI/testing/sysfs-bus-pci +16 -0
+
+		Present only for PCI Express devices.
+
+What:		/sys/bus/pci/devices/.../current_link_width
+Date:		September 2018
+Contact:	[email protected]
+Description:
+		The width the link is operating at right now, in lanes, e.g.
+		"16". Read fresh from the device's Link Status register on every
+		read of this file.
+
+		As with current_link_speed, this is instantaneous. Links may
+		also narrow and re-widen under link power management.
+
+		Present only for PCI Express devices.
+
 What:		/sys/bus/pci/devices/.../vpd
 Date:		February 2008
 Contact:	Ben Hutchings <[email protected]>
Signature · 1 line
 i.
#8 lore raw

PATCH v3 1/1 PCI/sysfs: document the link speed and width attributes

In reply to Ferran Duarri

max_link_speed, max_link_width, current_link_speed and current_link_width have been exported under /sys/bus/pci/devices/.../ since 2018, by
commit 56c1af4606f0 ("PCI: Add sysfs max_link_speed/width, current_link_speed/width, etc"), and none of the four appear anywhere in Documentation/ABI.

The gap matters most for current_link_speed. current_link_speed_show() performs a fresh PCI_EXP_LNKSTA read on every open, so the value reflects the link state at that instant. Modern GPUs retrain their link continuously as part of idle power management, which means a single read can legitimately return any speed the link supports, not the speed the link will use under load.

Observed on an RTX 5070 in a PCIe 4.0 x16 slot, same boot, no configuration change between the two reads: 5.0 GT/s while idle, 16.0 GT/s under load. Comparing current_link_speed against max_link_speed at idle is therefore not a valid test for a degraded link, though it reads like one.

Document all four. For the max_* pair, state that each reports the capability of the device it is read from and not a property of the link: a link trains at the lower of what its two ends support, so an endpoint capable of more than the port above it reports the higher figure while that port reports the lower one. Record where each value comes from, which differs between the two attributes. max_link_speed is derived from the Supported Link Speeds Vector in Link Capabilities 2, capped by Max Link Speed in Link Capabilities, and cached at enumeration. max_link_width is read from
Maximum Link Width in Link Capabilities on each access.

For current_link_speed, state that it is instantaneous, that comparing it against max_link_speed at idle is not a valid degradation test, and that callers wanting what the link will actually deliver should sample under load -- noting that max_link_speed is not that figure either, being one end's capability rather than the link's, and that the speed may also be capped by Target Link Speed in Link Control 2 under bwctrl's control.

No functional change.

Assisted-by Claude:claude-opus-5 checkpatch patch-audit

---
Changes in v3, all from Ilpo's review of v2:

- max_link_speed: drop the "synthesized from Max Link Speed alone on pre-r3.0 devices" sentence. It was unnecessary detail and imprecise:
what pcie_get_supported_speeds() synthesizes there is a SET of supported speeds, and max_link_speed_show() reports only the maximum of that set, which on a sane device equals Max Link Speed anyway.
- current_link_speed: note that the speed may also be capped by Target Link Speed in Link Control 2, managed by the PCIe bandwidth controller (bwctrl), and that a link held there is configured, not degraded.
- Add the Assisted-by: tag. v1 and v2 were written with an AI coding assistant and neither said so, which Documentation/process/coding- assistants.rst requires. Thanks for catching it. The content is unchanged from v2; this exists to carry the tag and to be the single v2 replacement, since v2 went out twice by my mistake.

Changes in v2, all corrections to what v1 claimed rather than new material:

- max_link_speed: v1 called it "the ceiling the link may negotiate, which is the lower of what the two ends of the link support". That is wrong, and v1 contradicted it one sentence later. max_link_speed_show() calls pcie_get_speed_cap(), which returns the capability of the device being read and never consults the other end of the link.
- max_link_speed: v1 said the value is read from the Max Link Speed field of Link Capabilities. pcie_get_supported_speeds() derives it from the Supported Link Speeds Vector in Link Capabilities 2, masks it against Max Link Speed, and synthesizes from Max Link Speed alone only on devices predating PCIe r3.0.
- max_link_speed: v1 did not say the value is read once at enumeration and cached in pci_dev->supported_speeds. Since the current_link_speed entry states that nothing is cached there, a reader could reasonably infer the same of max_link_speed. It does not hold.
- current_link_speed: v1 advised callers wanting the ceiling to use max_link_speed. That overestimates whenever the upstream port is the slower end. v2 says to sample under load and warns that max_link_speed is one end's capability, not the link's.
- max_link_width: register attribution was correct and is unchanged in substance, reworded only for the same device-versus-link distinction.
- Dropped a private Forward-Port-Notes: trailer that should not have been in the commit message.

 Documentation/ABI/testing/sysfs-bus-pci | 78 +++++++++++++++++++++++++
 1 file changed, 78 insertions(+)
Documentation/ABI/testing/sysfs-bus-pci +81 -0
index b767db2c52cb..ee1846f3dafa 100644
@@ -174,6 +174,84 @@ Description:
 		similiar to writing 1 to their individual "reset" file, so use
 		with caution.
 
+What:		/sys/bus/pci/devices/.../max_link_speed
+Date:		September 2018
+Contact:	[email protected]
+Description:
+		The maximum link speed this device is capable of, as a
+		human-readable string such as "16.0 GT/s PCIe".
+
+		Derived from the Supported Link Speeds Vector in the device's
+		Link Capabilities 2 register, capped by the Max Link Speed
+		field in Link Capabilities. Read once during enumeration and
+		cached thereafter, so unlike current_link_speed it does not
+		change between reads.
+
+		This is the device's own capability, not a property of the
+		link. A link trains at the lower of what its two ends support,
+		so an endpoint capable of a higher speed than the port above it
+		reports that higher speed here while the port reports the lower
+		one. Reading one end therefore does not tell you what the link
+		will do; read both ends and take the lower.
+
+		Present only for PCI Express devices.
+
+What:		/sys/bus/pci/devices/.../max_link_width
+Date:		September 2018
+Contact:	[email protected]
+Description:
+		The maximum link width this device is capable of, in lanes,
+		e.g. "16". Read from the Maximum Link Width field of the
+		device's Link Capabilities register.
+
+		As with max_link_speed this is the device's own capability, not
+		a property of the link; a link trains at the lower of what its
+		two ends support.
+
+		Present only for PCI Express devices.
+
+What:		/sys/bus/pci/devices/.../current_link_speed
+Date:		September 2018
+Contact:	[email protected]
+Description:
+		The speed the link is operating at right now, as a
+		human-readable string such as "16.0 GT/s PCIe". Read fresh from
+		the device's Link Status register on every read of this file;
+		nothing is cached.
+
+		This value is instantaneous and may change at any time. A link
+		is permitted to retrain to a lower speed and back, and devices
+		with aggressive link power management (GPUs in particular) do so
+		routinely while idle. Two reads seconds apart, with no
+		configuration change in between, can legitimately differ by
+		several generations.
+
+		Consequently, comparing this attribute against max_link_speed is
+		not by itself a test for a degraded link: an idle device will
+		frequently report a lower speed and is working correctly.
+		Callers that need a figure representing what the link will
+		actually deliver should sample while the device is under load.
+		max_link_speed is not that figure either: it reports one end's
+		capability, and the link is limited by the lower of its two
+		ends.
+
+		The speed may also be capped below both ends' capability by
+		the Target Link Speed field in Link Control 2, which the PCIe
+		bandwidth controller (bwctrl) manages. A link held there is
+		operating as configured, not degraded.
+
+		Present only for PCI Express devices.
+
+What:		/sys/bus/pci/devices/.../current_link_width
+Date:		September 2018
+Contact:	[email protected]
+Description:
+		The width the link is operating at right now, in lanes, e.g.
+		"16". Read fresh from the device's Link Status register on every
+		read of this file.
+
+		As with current_link_speed, this is instantaneous. Links may
+		also narrow and re-widen under link power management.
+
+		Present only for PCI Express devices.
+

What: /sys/bus/pci/devices/.../vpd
Date: February 2008
Contact: Ben Hutchings <[email protected]>

Signature · 1 line
2.53.0