PCI: Check if pcie_capability_read_*() reads ~0
Subsystems (6)
PATCH v4 8/12 PCI: Check if pcie_capability_read_*() reads ~0
On failure pcie_capability_read_*() sets it's last parameter, val to 0. However, with Patch 12/12, it is possible that val is set to ~0 on failure. This would introduce a bug because
(x & x) == (~0 & x).
Since ~0 is an invalid value in here,
Add extra check for ~0 in the if condition to ensure success or failure.
--- drivers/pci/probe.c | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-)
drivers/pci/probe.c +6 -6
@@ -1124,7 +1124,7 @@ static void pci_enable_crs(struct pci_dev *pdev)/* Enable CRS Software Visibility if supported */ pcie_capability_read_word (pdev,PCI_EXP_RTCAP , &root_cap); -if (root_cap &PCI_EXP_RTCAP_CRSVIS ) +if ((root_cap != (u16 )~0 ) && (root_cap &PCI_EXP_RTCAP_CRSVIS ))pcie_capability_set_word (pdev,PCI_EXP_RTCTL ,PCI_EXP_RTCTL_CRSSVE ); }
@@ -1521,7 +1521,7 @@ void set_pcie_hotplug_bridge(struct pci_dev *pdev)u32 reg32;pcie_capability_read_dword (pdev,PCI_EXP_SLTCAP , ®32); -if (reg32 &PCI_EXP_SLTCAP_HPC ) +if ((reg32 != (u32 )~0 ) && (reg32 &PCI_EXP_SLTCAP_HPC )) pdev->is_hotplug_bridge =1 ; }
@@ -2060,7 +2060,7 @@ bool pcie_relaxed_ordering_enabled(struct pci_dev *dev)pcie_capability_read_word (dev,PCI_EXP_DEVCTL , &v); -return !!(v &PCI_EXP_DEVCTL_RELAX_EN ); +return ((v != (u16 )~0 ) && (v &PCI_EXP_DEVCTL_RELAX_EN )); }EXPORT_SYMBOL (pcie_relaxed_ordering_enabled);
@@ -2101,11 +2101,11 @@ static void pci_configure_ltr(struct pci_dev *dev)return ;pcie_capability_read_dword (dev,PCI_EXP_DEVCAP2 , &cap); -if (!(cap &PCI_EXP_DEVCAP2_LTR )) +if ((cap == (u32 )~0 ) || !(cap &PCI_EXP_DEVCAP2_LTR ))return ;pcie_capability_read_dword (dev,PCI_EXP_DEVCTL2 , &ctl); -if (ctl &PCI_EXP_DEVCTL2_LTR_EN ) { +if ((ctl != (u32 )~0 ) && (ctl &PCI_EXP_DEVCTL2_LTR_EN )) {if (pci_pcie_type (dev) ==PCI_EXP_TYPE_ROOT_PORT ) { dev->ltr_path =1 ;return ;
@@ -2147,7 +2147,7 @@ static void pci_configure_eetlp_prefix(struct pci_dev *dev)return ;pcie_capability_read_dword (dev,PCI_EXP_DEVCAP2 , &cap); -if (!(cap &PCI_EXP_DEVCAP2_EE_PREFIX )) +if ((cap == (u32 )~0 ) || !(cap &PCI_EXP_DEVCAP2_EE_PREFIX ))return ; pcie_type =pci_pcie_type (dev);
Signature · 1 line
2.18.4
PATCH v4 9/12 PCI/PM: Check if pcie_capability_read_*() reads ~0
On failure pcie_capability_read_*() sets it's last parameter, val to 0. However, with Patch 12/12, it is possible that val is set to ~0 on failure. This would introduce a bug because
(x & x) == (~0 & x).
Since ~0 is an invalid value in here,
Add extra check for ~0 to ensure success or failure.
pci_enable_atomic_ops_to_root():
Continue looping through the device heirarchy on failure.
pcie_wait_for_link_delay():
Add extra check for ~0 to the condition for breaking out of the loop. Delay only on success otherwise report error and return false.
pcie_bandwidth_available():
On read failure move up the device heirarchy and continue.
pcie_get_speed_cap():
On read failure, report error and return PCI_SPEED_UNKNOWN
--- drivers/pci/pci.c | 34 ++++++++++++++++++++++++---------- 1 file changed, 24 insertions(+), 10 deletions(-)
drivers/pci/pci.c +24 -10
@@ -3216,7 +3216,7 @@ void pci_configure_ari(struct pci_dev *dev)return ;pcie_capability_read_dword (bridge,PCI_EXP_DEVCAP2 , &cap); -if (!(cap &PCI_EXP_DEVCAP2_ARI )) +if ((cap == (u32 )~0 ) || !(cap &PCI_EXP_DEVCAP2_ARI ))return ;if (pci_find_ext_capability (dev,PCI_EXT_CAP_ID_ARI )) {
@@ -3635,13 +3635,13 @@ int pci_enable_atomic_ops_to_root(struct pci_dev *dev, u32 cap_mask)/* Ensure switch ports support AtomicOp routing */ case PCI_EXP_TYPE_UPSTREAM :case PCI_EXP_TYPE_DOWNSTREAM : -if (!(cap &PCI_EXP_DEVCAP2_ATOMIC_ROUTE )) +if ((cap == (u32 )~0 ) || !(cap &PCI_EXP_DEVCAP2_ATOMIC_ROUTE ))return -EINVAL ;break ;/* Ensure root port supports all the sizes we care about */ case PCI_EXP_TYPE_ROOT_PORT : -if ((cap & cap_mask) != cap_mask) + if ((cap == (u32)~0) || ((cap & cap_mask) != cap_mask)return -EINVAL ;break ; }
@@ -3650,7 +3650,7 @@ int pci_enable_atomic_ops_to_root(struct pci_dev *dev, u32 cap_mask)if (pci_pcie_type (bridge) ==PCI_EXP_TYPE_UPSTREAM ) {pcie_capability_read_dword (bridge,PCI_EXP_DEVCTL2 , &ctl2); -if (ctl2 &PCI_EXP_DEVCTL2_ATOMIC_EGRESS_BLOCK ) +if ((ctl2 != (u32 )~0 ) && (ctl2 &PCI_EXP_DEVCTL2_ATOMIC_EGRESS_BLOCK ))return -EINVAL ; }
@@ -4512,7 +4512,7 @@ bool pcie_has_flr(struct pci_dev *dev)return false;pcie_capability_read_dword (dev,PCI_EXP_DEVCAP , &cap); -return cap &PCI_EXP_DEVCAP_FLR ; +return ((cap != (u32 )~0 ) && (cap &PCI_EXP_DEVCAP_FLR )); }EXPORT_SYMBOL_GPL (pcie_has_flr);
@@ -4672,19 +4672,19 @@ static bool pcie_wait_for_link_delay(struct pci_dev *pdev, bool active,for (;;) {pcie_capability_read_word (pdev,PCI_EXP_LNKSTA , &lnk_status); ret = !!(lnk_status &PCI_EXP_LNKSTA_DLLLA ); -if (ret == active) +if ((lnk_status != (u16 )~0 ) && (ret == active))break ;if (timeout <=0 )break ;msleep (10 ); timeout -=10 ; } -if (active && ret) +if ((lnk_status != (u16 )~0 ) && active && ret)msleep (delay); -else if (ret != active) +else if ((lnk_status == (u16 )~0 ) || (ret != active))pci_info (pdev,"Data Link Layer Link Active not %s in 1000 msec\n" , active ?"set" :"cleared" ); -return ret == active; +return ((lnk_status != (u16 )~0 ) && (ret == active)); }/**
@@ -5773,6 +5773,11 @@ u32 pcie_bandwidth_available(struct pci_dev *dev, struct pci_dev **limiting_dev,while (dev) {pcie_capability_read_word (dev,PCI_EXP_LNKSTA , &lnksta); +if (lnksta == (u16 )~0 ) { + dev =pci_upstream_bridge (dev); +continue ; + } + next_speed = pcie_link_speed[lnksta &PCI_EXP_LNKSTA_CLS ]; next_width = (lnksta &PCI_EXP_LNKSTA_NLW ) >>PCI_EXP_LNKSTA_NLW_SHIFT ;
@@ -5819,12 +5824,21 @@ enum pci_bus_speed pcie_get_speed_cap(struct pci_dev *dev)* where only 2.5 GT/s and 5.0 GT/s speeds were defined. */ pcie_capability_read_dword (dev,PCI_EXP_LNKCAP2 , &lnkcap2); +if (lnkcap2 == (u32 )~0 ) { +dev_err (dev,"Read link speed capability has failed.\n" ); +return PCI_SPEED_UNKNOWN ; + }/* PCIe r3.0-compliant */ if (lnkcap2)return PCIE_LNKCAP2_SLS2SPEED (lnkcap2);pcie_capability_read_dword (dev,PCI_EXP_LNKCAP , &lnkcap); +if (lnkcap == (u32 )~0 ) { +dev_err (dev,"Read link speed capability has failed.\n" ); +return PCI_SPEED_UNKNOWN ; + } +if ((lnkcap &PCI_EXP_LNKCAP_SLS ) ==PCI_EXP_LNKCAP_SLS_5_0GB )return PCIE_SPEED_5_0GT ;else if ((lnkcap &PCI_EXP_LNKCAP_SLS ) ==PCI_EXP_LNKCAP_SLS_2_5GB )
@@ -5846,7 +5860,7 @@ enum pcie_link_width pcie_get_width_cap(struct pci_dev *dev)u32 lnkcap;pcie_capability_read_dword (dev,PCI_EXP_LNKCAP , &lnkcap); -if (lnkcap) +if (lnkcap && (lnkcap != (u32 )~0 ))return (lnkcap &PCI_EXP_LNKCAP_MLW ) >>4 ;return PCIE_LNK_WIDTH_UNKNOWN ;
Signature · 6 lines
2.18.4 _______________________________________________ Linux-kernel-mentees mailing list [email protected] https://lists.linuxfoundation.org/mailman/listinfo/linux-kernel-mentees
PATCH v4 10/12 PCI/AER: Check if pcie_capability_read_*() reads ~0
In reply to Saheed O. BolarinwaOn failure pcie_capability_read_*() sets it's last parameter, val to 0. However, with Patch 12/12, it is possible that val is set to ~0 on failure. This would introduce a bug because
(x & x) == (~0 & x).
Since ~0 is an invalid value in here,
Add extra check for ~0 to the if condition to confirm failure.
--- drivers/pci/pcie/aer.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/pci/pcie/aer.c +1 -1
@@ -829,7 +829,7 @@ static bool is_error_source(struct pci_dev *dev, struct aer_err_info *e_info)/* Check if AER is enabled */ pcie_capability_read_word (dev,PCI_EXP_DEVCTL , ®16); -if (!(reg16 &PCI_EXP_AER_FLAGS )) +if ((reg16 == (u16 )~0 ) || !(reg16 &PCI_EXP_AER_FLAGS ))return false;if (!aer)
Signature · 1 line
2.18.4
PATCH v4 11/12 PCI/ASPM: Check if pcie_capability_read_*() reads ~0
In reply to Saheed O. BolarinwaOn failure pcie_capability_read_*() sets it's last parameter, val to 0. However, with Patch 12/12, it is possible that val is set to ~0 on failure. This would introduce a bug because
(x & x) == (~0 & x).
Since ~0 is an invalid value in here,
Add extra check for ~0 to the if condition to confirm failure.
--- drivers/pci/pcie/aspm.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-)
drivers/pci/pcie/aspm.c +5 -5
@@ -223,7 +223,7 @@ static bool pcie_retrain_link(struct pcie_link_state *link) end_jiffies = jiffies +LINK_RETRAIN_TIMEOUT ;do {pcie_capability_read_word (parent,PCI_EXP_LNKSTA , ®16); -if (!(reg16 &PCI_EXP_LNKSTA_LT )) +if ((reg16 == (u16 )~0 ) || !(reg16 &PCI_EXP_LNKSTA_LT ))break ;msleep (1 ); }while (time_before (jiffies, end_jiffies));
@@ -250,23 +250,23 @@ static void pcie_aspm_configure_common_clock(struct pcie_link_state *link)/* Check downstream component if bit Slot Clock Configuration is 1 */ pcie_capability_read_word (child,PCI_EXP_LNKSTA , ®16); -if (!(reg16 &PCI_EXP_LNKSTA_SLC )) +if ((reg16 == (u16 )~0 ) || !(reg16 &PCI_EXP_LNKSTA_SLC )) same_clock =0 ;/* Check upstream component if bit Slot Clock Configuration is 1 */ pcie_capability_read_word (parent,PCI_EXP_LNKSTA , ®16); -if (!(reg16 &PCI_EXP_LNKSTA_SLC )) +if ((reg16 == (u16 )~0 ) || !(reg16 &PCI_EXP_LNKSTA_SLC )) same_clock =0 ;/* Port might be already in common clock mode */ pcie_capability_read_word (parent,PCI_EXP_LNKCTL , ®16); -if (same_clock && (reg16 &PCI_EXP_LNKCTL_CCC )) { +if ((reg16 != (u16 )~0 ) && same_clock && (reg16 &PCI_EXP_LNKCTL_CCC )) {bool consistent = true;list_for_each_entry (child, &linkbus->devices, bus_list) {pcie_capability_read_word (child,PCI_EXP_LNKCTL , ®16); -if (!(reg16 &PCI_EXP_LNKCTL_CCC )) { +if ((reg16 == (u16 )~0 ) || !(reg16 &PCI_EXP_LNKCTL_CCC )) { consistent = false;break ; }
Signature · 6 lines
2.18.4 _______________________________________________ Linux-kernel-mentees mailing list [email protected] https://lists.linuxfoundation.org/mailman/listinfo/linux-kernel-mentees
PATCH v4 12/12 PCI: Remove '*val = 0' from pcie_capability_read_*()
In reply to Saheed O. BolarinwaThere are several reasons why a PCI capability read may fail whether the device is present or not. If this happens, pcie_capability_read_*() will return -EINVAL/PCIBIOS_BAD_REGISTER_NUMBER or PCIBIOS_DEVICE_NOT_FOUND and *val is set to 0.
This behaviour if further ensured by this code inside pcie_capability_read_*()
ret = pci_read_config_dword(dev, pci_pcie_cap(dev) + pos, val); /*
* Reset *val to 0 if pci_read_config_dword() fails, it may
* have been written as 0xFFFFFFFF if hardware error happens
* during pci_read_config_dword().
*/
if (ret)
*val = 0;
return ret;
a) Since all pci_generic_config_read() does is read a register value, it may return success after reading a ~0 which *may* have been fabricated by the PCI host bridge due to a read timeout. Hence pci_read_config_*() will return success with a fabricated ~0 in *val, indicating a problem. In this case, the assumed behaviour of pcie_capability_read_*() will be wrong. To avoid error slipping through, more checks are necessary.
b) pci_read_config_*() will return PCIBIOS_DEVICE_NOT_FOUND only if dev->error_state = pci_channel_io_perm_failure (i.e. pci_dev_is_disconnected()) or if pci_generic_config_read() can't find the device. In both cases *val is initially set to ~0 but as shown in the code above pcie_capability_read_*() resets it back to 0. Even with this effort, drivers still have to perform validation checks more so if 0 is a valid value.
Most drivers only consider the case (b) and in some cases, there is the expectation that on timeout *val has a fabricated value of ~0, which *may* not always be true as explained in (a).
In any case, checks need to be done to validate the value read and maybe confirm which error has occurred. It is better left to the drivers to do.
Remove the reset of *val to 0 when pci_read_config_*() fails.
--- drivers/pci/access.c | 14 -------------- 1 file changed, 14 deletions(-)
drivers/pci/access.c +0 -14
@@ -413,13 +413,6 @@ int pcie_capability_read_word(struct pci_dev *dev, int pos, u16 *val)if (pcie_capability_reg_implemented (dev, pos)) { ret =pci_read_config_word (dev,pci_pcie_cap (dev) + pos, val); -/* -* Reset *val to 0 if pci_read_config_word() fails, it may -* have been written as 0xFFFF if hardware error happens -* during pci_read_config_word(). -*/ -if (ret) - *val =0 ;return ret; }
@@ -448,13 +441,6 @@ int pcie_capability_read_dword(struct pci_dev *dev, int pos, u32 *val)if (pcie_capability_reg_implemented (dev, pos)) { ret =pci_read_config_dword (dev,pci_pcie_cap (dev) + pos, val); -/* -* Reset *val to 0 if pci_read_config_dword() fails, it may -* have been written as 0xFFFFFFFF if hardware error happens -* during pci_read_config_dword(). -*/ -if (ret) - *val =0 ;return ret; }
Signature · 1 line
2.18.4