]> git.ipfire.org Git - thirdparty/kernel/stable.git/commitdiff
PCI: Add pcie_get_link_speed() helper for safe array access
authorHans Zhang <18255117159@163.com>
Fri, 13 Mar 2026 16:55:18 +0000 (00:55 +0800)
committerManivannan Sadhasivam <mani@kernel.org>
Thu, 26 Mar 2026 18:30:08 +0000 (00:00 +0530)
The pcie_link_speed[] array is indexed by PCIe generation numbers
(1 = 2.5 GT/s, 2 = 5 GT/s, ...).  Several drivers use it directly,
which can lead to out-of-bounds accesses if an invalid generation
number is used.

Introduce a helper function pcie_get_link_speed() that returns the
pci_bus_speed value for a given generation number, or PCI_SPEED_UNKNOWN if
the generation is out of range.  This will allow us to safely handle
invalid values after the range check is removed from
of_pci_get_max_link_speed().

Signed-off-by: Hans Zhang <18255117159@163.com>
[mani: Fixed kernel-doc]
Signed-off-by: Manivannan Sadhasivam <mani@kernel.org>
Acked-by: Bjorn Helgaas <bhelgaas@google.com>
Link: https://patch.msgid.link/20260313165522.123518-2-18255117159@163.com
drivers/pci/pci.h
drivers/pci/probe.c

index 13d998fbacce6698514d92500dfea03cc562cdc2..409aca7d737ac5a6833e275f643d784fe02aad4b 100644 (file)
@@ -108,6 +108,8 @@ struct pcie_tlp_log;
                                 PCI_EXP_DEVCTL_FERE | PCI_EXP_DEVCTL_URRE)
 
 extern const unsigned char pcie_link_speed[];
+unsigned char pcie_get_link_speed(unsigned int speed);
+
 extern bool pci_early_dump;
 
 extern struct mutex pci_rescan_remove_lock;
index bccc7a4bdd794384b7877d453c7989941471c999..f85da50a3d8358c2fabeaa84240a67045f2c8a79 100644 (file)
@@ -783,6 +783,22 @@ const unsigned char pcie_link_speed[] = {
 };
 EXPORT_SYMBOL_GPL(pcie_link_speed);
 
+/**
+ * pcie_get_link_speed - Get speed value from PCIe generation number
+ * @speed: PCIe speed (1-based: 1 = 2.5GT, 2 = 5GT, ...)
+ *
+ * Returns the speed value (e.g., PCIE_SPEED_2_5GT) if @speed is valid,
+ * otherwise returns PCI_SPEED_UNKNOWN.
+ */
+unsigned char pcie_get_link_speed(unsigned int speed)
+{
+       if (speed >= ARRAY_SIZE(pcie_link_speed))
+               return PCI_SPEED_UNKNOWN;
+
+       return pcie_link_speed[speed];
+}
+EXPORT_SYMBOL_GPL(pcie_get_link_speed);
+
 const char *pci_speed_string(enum pci_bus_speed speed)
 {
        /* Indexed by the pci_bus_speed enum */