diff mbox series

[v1] PCI: pciehp: Refactor infinite loop in pcie_poll_cmd()

Message ID 20191011090230.81080-1-andriy.shevchenko@linux.intel.com
State Superseded
Delegated to: Bjorn Helgaas
Headers show
Series [v1] PCI: pciehp: Refactor infinite loop in pcie_poll_cmd() | expand

Commit Message

Andy Shevchenko Oct. 11, 2019, 9:02 a.m. UTC
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.

Signed-off-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
---
 drivers/pci/hotplug/pciehp_hpc.c | 6 ++----
 1 file changed, 2 insertions(+), 4 deletions(-)

Comments

Andy Shevchenko Nov. 7, 2019, 3:15 p.m. UTC | #1
On Fri, Oct 11, 2019 at 12:02:30PM +0300, Andy Shevchenko wrote:
> 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.

> 
> Signed-off-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
> ---
>  drivers/pci/hotplug/pciehp_hpc.c | 6 ++----
>  1 file changed, 2 insertions(+), 4 deletions(-)
> 
> diff --git a/drivers/pci/hotplug/pciehp_hpc.c b/drivers/pci/hotplug/pciehp_hpc.c
> index 1a522c1c4177..e397c78ca232 100644
> --- a/drivers/pci/hotplug/pciehp_hpc.c
> +++ b/drivers/pci/hotplug/pciehp_hpc.c
> @@ -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 */
>  }
>  
> -- 
> 2.23.0
>
Alex G. Nov. 7, 2019, 5:21 p.m. UTC | #2
Hi Andy,
On 11/7/19 9:15 AM, Andy Shevchenko wrote:
> On Fri, Oct 11, 2019 at 12:02:30PM +0300, Andy Shevchenko wrote:
>> 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

>> 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.
> 
>>
>> Signed-off-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
>> ---
>>   drivers/pci/hotplug/pciehp_hpc.c | 6 ++----
>>   1 file changed, 2 insertions(+), 4 deletions(-)
>>
>> diff --git a/drivers/pci/hotplug/pciehp_hpc.c b/drivers/pci/hotplug/pciehp_hpc.c
>> index 1a522c1c4177..e397c78ca232 100644
>> --- a/drivers/pci/hotplug/pciehp_hpc.c
>> +++ b/drivers/pci/hotplug/pciehp_hpc.c
>> @@ -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 */
>>   }
>>   
>> -- 
>> 2.23.0
>>
>
Keith Busch Nov. 7, 2019, 10:37 p.m. UTC | #3
On Fri, Oct 11, 2019 at 12:02:30PM +0300, Andy Shevchenko wrote:
> No functional changes implied.

[snip] 

> -	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 */
>  }

If you really want to ensure no funcitonal change, I think the end of
the loop needs to be 'while (timeout >= 0);'
Andrew Murray Nov. 8, 2019, 10:18 a.m. UTC | #4
On Fri, Nov 08, 2019 at 07:37:26AM +0900, Keith Busch wrote:
> On Fri, Oct 11, 2019 at 12:02:30PM +0300, Andy Shevchenko wrote:
> > No functional changes implied.
> 
> [snip] 
> 
> > -	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 */
> >  }
> 
> 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:

Reviewed-by: Andrew Murray <andrew.murray@arm.com>

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.
Andy Shevchenko Nov. 8, 2019, 11:08 a.m. UTC | #5
On Thu, Nov 07, 2019 at 11:21:12AM -0600, Alex G. wrote:
> 
> Hi Andy,
> On 11/7/19 9:15 AM, Andy Shevchenko wrote:
> > On Fri, Oct 11, 2019 at 12:02:30PM +0300, Andy Shevchenko wrote:
> > > 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) {

Invariant conditional inside loop? okay...

> 			complain();
> 			break()
> 		}
> 	} while (what_we_expect_has_not_happened());
Andy Shevchenko Nov. 8, 2019, 11:10 a.m. UTC | #6
On Fri, Nov 08, 2019 at 10:18:16AM +0000, Andrew Murray wrote:
> On Fri, Nov 08, 2019 at 07:37:26AM +0900, Keith Busch wrote:
> > On Fri, Oct 11, 2019 at 12:02:30PM +0300, Andy Shevchenko wrote:
> > > No functional changes implied.
> > 
> > [snip] 
> > 
> > > -	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 */
> > >  }
> > 
> > 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:
> 
> Reviewed-by: Andrew Murray <andrew.murray@arm.com>
> 
> 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.
diff mbox series

Patch

diff --git a/drivers/pci/hotplug/pciehp_hpc.c b/drivers/pci/hotplug/pciehp_hpc.c
index 1a522c1c4177..e397c78ca232 100644
--- a/drivers/pci/hotplug/pciehp_hpc.c
+++ b/drivers/pci/hotplug/pciehp_hpc.c
@@ -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 */
 }