PCI: pciehp: Refactor infinite loop in pcie_poll_cmd()
PATCH 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.
--- drivers/pci/hotplug/pciehp_hpc.c | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-)
drivers/pci/hotplug/pciehp_hpc.c +2 -4
@@ -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.23.0
Quoted 9 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.
No functional changes implied.
Bjorn, any comment on this? It would be nice to have in since contributors are unable to know which style to use. This patch makes similar places follow the same style.
Quoted 35 lines
Signed-off-by Andy Shevchenko <[email protected]>--- drivers/pci/hotplug/pciehp_hpc.c | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-)drivers/pci/hotplug/pciehp_hpc.c +2
-4@@ -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.23.0
Signature · 2 lines
With Best Regards, Andy Shevchenko
Hi Andy,
Quoted 2 lines · Andy Shevchenko
Quoted 1 line · Andy Shevchenko
Infinite timeout loops are hard to read.
Why do you find infinite timeout loops hard to read? I personally find that emphasizing the common case to be more redable. An ideal loop for me would look like:
do {
do_stuff();
if (timeout) {
complain();
break()
}
} while (what_we_expect_has_not_happened());Cheers,
Alex
Quoted 49 lines
Quoted 8 lines
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.
Bjorn, any comment on this? It would be nice to have in since contributors are unable to know which style to use. This patch makes similar places follow the same style.
Quoted 35 lines
Signed-off-by Andy Shevchenko <[email protected]>--- drivers/pci/hotplug/pciehp_hpc.c | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-)drivers/pci/hotplug/pciehp_hpc.c +2
-4@@ -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.23.0
Quoted 1 line · Andy Shevchenko
No functional changes implied.
[snip]
Quoted 7 lines
drivers/pci/hotplug/pciehp_hpc.c +1
-1-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);
drivers/pci/hotplug/pciehp_hpc.c static int pcie_poll_cmd(struct controller *ctrl, int timeout) · line 88
Quoted 10 lines
drivers/pci/hotplug/pciehp_hpc.c +1
-3return 1 ; } -if (timeout <0 ) -break ;msleep (10 ); timeout -=10 ; - } + }while (timeout >0 );return 0 ;/* timeout */ }
If you really want to ensure no funcitonal change, I think the end of the loop needs to be 'while (timeout >= 0);'
Andrew Murray
Reviewed-byQuoted 17 lines · Keith Busch
Quoted 1 line · Andy Shevchenko
No functional changes implied.
[snip]
Quoted 12 lines
drivers/pci/hotplug/pciehp_hpc.c +1
-3-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 );
Quoted 8 lines
drivers/pci/hotplug/pciehp_hpc.c static int pcie_poll_cmd(struct controller *ctrl, int timeout) · line 88
Quoted 5 lines
drivers/pci/hotplug/pciehp_hpc.c +1
-1timeout -=10 ; - } + }while (timeout >0 );return 0 ;/* timeout */ }If you really want to ensure no funcitonal change, I think the end of the loop needs to be 'while (timeout >= 0);'
With this suggested change, you can add:
I can't get too excited by coding styles, however I find this more readable now, due to the fact that the loop is clearly bounded.
Quoted 6 lines · Alex G.
Hi Andy,
Quoted 2 lines · Andy Shevchenko
Quoted 1 line · Andy Shevchenko
Infinite timeout loops are hard to read.
Quoted 8 lines
Why do you find infinite timeout loops hard to read? I personally find that emphasizing the common case to be more redable. An ideal loop for me would look like:
do { do_stuff(); if (timeout) {
Invariant conditional inside loop? okay...
Quoted 4 lines
complain(); break() } } while (what_we_expect_has_not_happened());
Signature · 2 lines
With Best Regards, Andy Shevchenko
Quoted 27 lines · Andrew Murray
Quoted 17 lines · Keith Busch
Quoted 1 line · Andy Shevchenko
No functional changes implied.
[snip]
Quoted 12 lines
drivers/pci/hotplug/pciehp_hpc.c +1
-3-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 );Quoted 8 lines
drivers/pci/hotplug/pciehp_hpc.c static int pcie_poll_cmd(struct controller *ctrl, int timeout) · line 88
Quoted 5 lines
drivers/pci/hotplug/pciehp_hpc.c +1
-1timeout -=10 ; - } + }while (timeout >0 );return 0 ;/* timeout */ }If you really want to ensure no funcitonal change, I think the end of the loop needs to be 'while (timeout >= 0);'
Quoted 6 lines
With this suggested change, you can add:
Reviewed-by Andrew Murray <[email protected]>I can't get too excited by coding styles, however I find this more readable now, due to the fact that the loop is clearly bounded.
Thank you, Keith and Andrew, I'll submit v2 soon.
Signature · 2 lines
With Best Regards, Andy Shevchenko