Skip to contentSkip to conversation

PCI: pciehp: Refactor infinite loop in pcie_poll_cmd()

PATCH Applied Andy Shevchenko 2 participants 2 messages Nov 8, 2019 – Nov 12, 2019 PCI

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 PCI: pciehp: Refactor infinite loop in pcie_poll_cmd()

Infinite timeout loops are hard to read. Refactor it to plausible 'do {} while ()'.

Note, the supplied timeout can't be negative for current use, though if it's not dividable to 10, we may go below 0, that's why type of the parameter is int. And thus, we may move the check to the loop condition.

No functional changes implied.

---
- corrected loop conditional to include 0 comparison (Keith)
- added Rb (Andrew)

 drivers/pci/hotplug/pciehp_hpc.c | 6 ++----
 1 file changed, 2 insertions(+), 4 deletions(-)
drivers/pci/hotplug/pciehp_hpc.c +2 -4
index 86d97f3112f0..764384153c7d 100644
@@ -68,7 +68,7 @@ static int pcie_poll_cmd(struct controller *ctrl, int timeout)
 	struct pci_dev *pdev = ctrl_dev(ctrl);
 	u16 slot_status;
 
-	while (true) {
+	do {
 		pcie_capability_read_word(pdev, PCI_EXP_SLTSTA, &slot_status);
 		if (slot_status == (u16) ~0) {
 			ctrl_info(ctrl, "%s: no response from device\n",
@@ -81,11 +81,9 @@ static int pcie_poll_cmd(struct controller *ctrl, int timeout)
 						   PCI_EXP_SLTSTA_CC);
 			return 1;
 		}
-		if (timeout < 0)
-			break;
 		msleep(10);
 		timeout -= 10;
-	}
+	} while (timeout >= 0);
 	return 0;	/* timeout */
 }
 
Signature · 1 line
2.24.0.rc1
#1 lore raw
Quoted 8 lines · Andy Shevchenko

Infinite timeout loops are hard to read. Refactor it to plausible 'do {} while ()'.

Note, the supplied timeout can't be negative for current use, though if it's not dividable to 10, we may go below 0, that's why type of the parameter is int. And thus, we may move the check to the loop condition.

Quoted 4 lines

No functional changes implied.

Applied to pci/hotplug for v5.5, thanks!

Quoted 35 lines

---
- corrected loop conditional to include 0 comparison (Keith)
- added Rb (Andrew)

 drivers/pci/hotplug/pciehp_hpc.c | 6 ++----
 1 file changed, 2 insertions(+), 4 deletions(-)
drivers/pci/hotplug/pciehp_hpc.c +2 -4
index 86d97f3112f0..764384153c7d 100644
@@ -68,7 +68,7 @@ static int pcie_poll_cmd(struct controller *ctrl, int timeout)
 	struct pci_dev *pdev = ctrl_dev(ctrl);
 	u16 slot_status;
 
-	while (true) {
+	do {
 		pcie_capability_read_word(pdev, PCI_EXP_SLTSTA, &slot_status);
 		if (slot_status == (u16) ~0) {
 			ctrl_info(ctrl, "%s: no response from device\n",
@@ -81,11 +81,9 @@ static int pcie_poll_cmd(struct controller *ctrl, int timeout)
 						   PCI_EXP_SLTSTA_CC);
 			return 1;
 		}
-		if (timeout < 0)
-			break;
 		msleep(10);
 		timeout -= 10;
-	}
+	} while (timeout >= 0);
 	return 0;	/* timeout */
 }
 
Signature · 1 line
2.24.0.rc1