diff mbox

[V2,4/4] PCI: consolidate return value check for pci_find_(ext_)capability

Message ID 1435627004-6029-5-git-send-email-weiyang@linux.vnet.ibm.com (mailing list archive)
State New, archived
Delegated to: Bjorn Helgaas
Headers show

Commit Message

Wei Yang June 30, 2015, 1:16 a.m. UTC
The return value of the pci_find_(ext_)capability is the position of this
Cap.  After previous two patches clean up, the position returned is an
unsigned value. Only 0 indicates the Cap is not presented.

This patch consolidates the form of check from (pos <= 0)to (!pos).

Signed-off-by: Wei Yang <weiyang@linux.vnet.ibm.com>
---
 drivers/pci/pci.c |    6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

Comments

Bjorn Helgaas July 14, 2015, 10 p.m. UTC | #1
On Tue, Jun 30, 2015 at 09:16:44AM +0800, Wei Yang wrote:
> The return value of the pci_find_(ext_)capability is the position of this
> Cap.  After previous two patches clean up, the position returned is an
> unsigned value. Only 0 indicates the Cap is not presented.
> 
> This patch consolidates the form of check from (pos <= 0)to (!pos).
> 
> Signed-off-by: Wei Yang <weiyang@linux.vnet.ibm.com>

Applied to pci/misc with changelog as below.

It seems pretty clear to me that pci_find_capability() returns either 0 or
a u8 value.  pci_find_ext_capability() does return an int.  It looks to me
like it can never be negative, but if you wanted it to be even more clear,
you could easily change just pci_find_next_ext_capability() to use a u16
for "pos".  That would be very simple and wouldn't change any interfaces.

commit d5fa86074987b1b5fcbfba8c9315e75ff7262f71
Author: Wei Yang <weiyang@linux.vnet.ibm.com>
Date:   Tue Jun 30 09:16:44 2015 +0800

    PCI: Simplify pci_find_(ext_)capability() return value checks
    
    The return value of the pci_find_(ext_)capability() is either zero or the
    position of a capability.  It is never negative.
    
    This patch consolidates the form of check from (pos <= 0) to (!pos).
    
    Signed-off-by: Wei Yang <weiyang@linux.vnet.ibm.com>
    Signed-off-by: Bjorn Helgaas <bhelgaas@google.com>
--
To unsubscribe from this list: send the line "unsubscribe linux-pci" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Wei Yang July 15, 2015, 2:02 a.m. UTC | #2
On Tue, Jul 14, 2015 at 05:00:21PM -0500, Bjorn Helgaas wrote:
>On Tue, Jun 30, 2015 at 09:16:44AM +0800, Wei Yang wrote:
>> The return value of the pci_find_(ext_)capability is the position of this
>> Cap.  After previous two patches clean up, the position returned is an
>> unsigned value. Only 0 indicates the Cap is not presented.
>> 
>> This patch consolidates the form of check from (pos <= 0)to (!pos).
>> 
>> Signed-off-by: Wei Yang <weiyang@linux.vnet.ibm.com>
>
>Applied to pci/misc with changelog as below.
>
>It seems pretty clear to me that pci_find_capability() returns either 0 or
>a u8 value.  pci_find_ext_capability() does return an int.  It looks to me
>like it can never be negative, but if you wanted it to be even more clear,
>you could easily change just pci_find_next_ext_capability() to use a u16
>for "pos".  That would be very simple and wouldn't change any interfaces.

pci_find_capability() will return either 0 or a u8 value, while in the code
the return value is an "int" type. So for the first sight, it may not that
immediate. The same as pci_find_ext_capability().

This is the reason for patch 2/3. The purpose is to make the return type
reflect the value it will return.

Patch 3 does exactly what you said, use a u16 for "pos" in
pci_find_next_ext_capability().

>
>commit d5fa86074987b1b5fcbfba8c9315e75ff7262f71
>Author: Wei Yang <weiyang@linux.vnet.ibm.com>
>Date:   Tue Jun 30 09:16:44 2015 +0800
>
>    PCI: Simplify pci_find_(ext_)capability() return value checks
>    
>    The return value of the pci_find_(ext_)capability() is either zero or the
>    position of a capability.  It is never negative.
>    
>    This patch consolidates the form of check from (pos <= 0) to (!pos).
>    
>    Signed-off-by: Wei Yang <weiyang@linux.vnet.ibm.com>
>    Signed-off-by: Bjorn Helgaas <bhelgaas@google.com>
Bjorn Helgaas July 15, 2015, 2:11 a.m. UTC | #3
On Tue, Jul 14, 2015 at 9:02 PM, Wei Yang <weiyang@linux.vnet.ibm.com> wrote:
> On Tue, Jul 14, 2015 at 05:00:21PM -0500, Bjorn Helgaas wrote:
>>On Tue, Jun 30, 2015 at 09:16:44AM +0800, Wei Yang wrote:
>>> The return value of the pci_find_(ext_)capability is the position of this
>>> Cap.  After previous two patches clean up, the position returned is an
>>> unsigned value. Only 0 indicates the Cap is not presented.
>>>
>>> This patch consolidates the form of check from (pos <= 0)to (!pos).
>>>
>>> Signed-off-by: Wei Yang <weiyang@linux.vnet.ibm.com>
>>
>>Applied to pci/misc with changelog as below.
>>
>>It seems pretty clear to me that pci_find_capability() returns either 0 or
>>a u8 value.  pci_find_ext_capability() does return an int.  It looks to me
>>like it can never be negative, but if you wanted it to be even more clear,
>>you could easily change just pci_find_next_ext_capability() to use a u16
>>for "pos".  That would be very simple and wouldn't change any interfaces.
>
> pci_find_capability() will return either 0 or a u8 value, while in the code
> the return value is an "int" type. So for the first sight, it may not that
> immediate. The same as pci_find_ext_capability().
>
> This is the reason for patch 2/3. The purpose is to make the return type
> reflect the value it will return.
>
> Patch 3 does exactly what you said, use a u16 for "pos" in
> pci_find_next_ext_capability().

Yes.  Patch 3 contains this hunk:

@@ -272,11 +272,11 @@ EXPORT_SYMBOL(pci_bus_find_capability);
  * not support it.  Some capabilities can occur several times, e.g., the
  * vendor-specific capability, and this provides a way to find them all.
  */
-int pci_find_next_ext_capability(struct pci_dev *dev, int start, int cap)
+u16 pci_find_next_ext_capability(struct pci_dev *dev, int start, u16 cap)
 {
        u32 header;
        int ttl;
-       int pos = PCI_CFG_SPACE_SIZE;
+       u16 pos = PCI_CFG_SPACE_SIZE;

        /* minimum 8 bytes per capability */
        ttl = (PCI_CFG_SPACE_EXP_SIZE - PCI_CFG_SPACE_SIZE) / 8;

I think the second piece (changing "pos" from int to u16) is enough to
make it easy to analyze, and it only affects this function, so that
seems obviously fine.

The first piece (changing the return type) changes the signature of an
exported interface.  That is more work, e.g., it's more of a hassle
for distros to backport if they want to preserve interfaces, so it's
not quite as obvious that it's worthwhile.

Bjorn
--
To unsubscribe from this list: send the line "unsubscribe linux-pci" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Wei Yang July 15, 2015, 5:46 a.m. UTC | #4
On Tue, Jul 14, 2015 at 09:11:54PM -0500, Bjorn Helgaas wrote:
>On Tue, Jul 14, 2015 at 9:02 PM, Wei Yang <weiyang@linux.vnet.ibm.com> wrote:
>> On Tue, Jul 14, 2015 at 05:00:21PM -0500, Bjorn Helgaas wrote:
>>>On Tue, Jun 30, 2015 at 09:16:44AM +0800, Wei Yang wrote:
>>>> The return value of the pci_find_(ext_)capability is the position of this
>>>> Cap.  After previous two patches clean up, the position returned is an
>>>> unsigned value. Only 0 indicates the Cap is not presented.
>>>>
>>>> This patch consolidates the form of check from (pos <= 0)to (!pos).
>>>>
>>>> Signed-off-by: Wei Yang <weiyang@linux.vnet.ibm.com>
>>>
>>>Applied to pci/misc with changelog as below.
>>>
>>>It seems pretty clear to me that pci_find_capability() returns either 0 or
>>>a u8 value.  pci_find_ext_capability() does return an int.  It looks to me
>>>like it can never be negative, but if you wanted it to be even more clear,
>>>you could easily change just pci_find_next_ext_capability() to use a u16
>>>for "pos".  That would be very simple and wouldn't change any interfaces.
>>
>> pci_find_capability() will return either 0 or a u8 value, while in the code
>> the return value is an "int" type. So for the first sight, it may not that
>> immediate. The same as pci_find_ext_capability().
>>
>> This is the reason for patch 2/3. The purpose is to make the return type
>> reflect the value it will return.
>>
>> Patch 3 does exactly what you said, use a u16 for "pos" in
>> pci_find_next_ext_capability().
>
>Yes.  Patch 3 contains this hunk:
>
>@@ -272,11 +272,11 @@ EXPORT_SYMBOL(pci_bus_find_capability);
>  * not support it.  Some capabilities can occur several times, e.g., the
>  * vendor-specific capability, and this provides a way to find them all.
>  */
>-int pci_find_next_ext_capability(struct pci_dev *dev, int start, int cap)
>+u16 pci_find_next_ext_capability(struct pci_dev *dev, int start, u16 cap)
> {
>        u32 header;
>        int ttl;
>-       int pos = PCI_CFG_SPACE_SIZE;
>+       u16 pos = PCI_CFG_SPACE_SIZE;
>
>        /* minimum 8 bytes per capability */
>        ttl = (PCI_CFG_SPACE_EXP_SIZE - PCI_CFG_SPACE_SIZE) / 8;
>
>I think the second piece (changing "pos" from int to u16) is enough to
>make it easy to analyze, and it only affects this function, so that
>seems obviously fine.
>
>The first piece (changing the return type) changes the signature of an
>exported interface.  That is more work, e.g., it's more of a hassle
>for distros to backport if they want to preserve interfaces, so it's
>not quite as obvious that it's worthwhile.

OK, I got your concern.

>
>Bjorn
>--
>To unsubscribe from this list: send the line "unsubscribe linux-pci" in
>the body of a message to majordomo@vger.kernel.org
>More majordomo info at  http://vger.kernel.org/majordomo-info.html
diff mbox

Patch

diff --git a/drivers/pci/pci.c b/drivers/pci/pci.c
index 4bd3a0c..e1282aa 100644
--- a/drivers/pci/pci.c
+++ b/drivers/pci/pci.c
@@ -971,7 +971,7 @@  static int pci_save_pcix_state(struct pci_dev *dev)
 	struct pci_cap_saved_state *save_state;
 
 	pos = pci_find_capability(dev, PCI_CAP_ID_PCIX);
-	if (pos <= 0)
+	if (!pos)
 		return 0;
 
 	save_state = pci_find_saved_cap(dev, PCI_CAP_ID_PCIX);
@@ -995,7 +995,7 @@  static void pci_restore_pcix_state(struct pci_dev *dev)
 
 	save_state = pci_find_saved_cap(dev, PCI_CAP_ID_PCIX);
 	pos = pci_find_capability(dev, PCI_CAP_ID_PCIX);
-	if (!save_state || pos <= 0)
+	if (!save_state || !pos)
 		return;
 	cap = (u16 *)&save_state->cap.data[0];
 
@@ -2159,7 +2159,7 @@  static int _pci_add_cap_save_buffer(struct pci_dev *dev, u16 cap,
 	else
 		pos = pci_find_capability(dev, cap);
 
-	if (pos <= 0)
+	if (!pos)
 		return 0;
 
 	save_state = kzalloc(sizeof(*save_state) + size, GFP_KERNEL);