[dpdk-dev,2/2] pci: rearrange logic from compare loop

Message ID 1428963071-4226-3-git-send-email-stephen@networkplumber.org (mailing list archive)
State Accepted, archived
Headers

Commit Message

Stephen Hemminger April 13, 2015, 10:11 p.m. UTC
Do some cleanup of pci scan loop.
  * check errors first
  * don't initialize variables where not necessary
  * cuddle else (follow existing style)
  * chop off conditional after return

Signed-off-by: Stephen Hemminger <stephen@networkplumber.org>

---
 lib/librte_eal/linuxapp/eal/eal_pci.c | 24 ++++++++++++------------
 1 file changed, 12 insertions(+), 12 deletions(-)
  

Comments

Michael Qiu April 14, 2015, 9:30 a.m. UTC | #1
On 4/14/2015 6:11 AM, Stephen Hemminger wrote:
> Do some cleanup of pci scan loop.
>   * check errors first
>   * don't initialize variables where not necessary

Why? It should be better to initialize variables when define it.

Thanks,
Michael
>   * cuddle else (follow existing style)
>   * chop off conditional after return
>
> Signed-off-by: Stephen Hemminger <stephen@networkplumber.org>
>
> ---
>  lib/librte_eal/linuxapp/eal/eal_pci.c | 24 ++++++++++++------------
>  1 file changed, 12 insertions(+), 12 deletions(-)
>
> diff --git a/lib/librte_eal/linuxapp/eal/eal_pci.c b/lib/librte_eal/linuxapp/eal/eal_pci.c
> index c98a778..d96b1c4 100644
> --- a/lib/librte_eal/linuxapp/eal/eal_pci.c
> +++ b/lib/librte_eal/linuxapp/eal/eal_pci.c
> @@ -337,6 +337,12 @@ pci_scan_one(const char *dirname, uint16_t domain, uint8_t bus,
>  	/* parse driver */
>  	snprintf(filename, sizeof(filename), "%s/driver", dirname);
>  	ret = pci_get_kernel_driver_by_path(filename, driver);
> +	if (ret < 0) {
> +		RTE_LOG(ERR, EAL, "Fail to get kernel driver\n");
> +		free(dev);
> +		return -1;
> +	}
> +
>  	if (!ret) {
>  		if (!strcmp(driver, "vfio-pci"))
>  			dev->kdrv = RTE_KDRV_VFIO;
> @@ -346,37 +352,31 @@ pci_scan_one(const char *dirname, uint16_t domain, uint8_t bus,
>  			dev->kdrv = RTE_KDRV_UIO_GENERIC;
>  		else
>  			dev->kdrv = RTE_KDRV_UNKNOWN;
> -	} else if (ret < 0) {
> -		RTE_LOG(ERR, EAL, "Fail to get kernel driver\n");
> -		free(dev);
> -		return -1;
>  	} else
>  		dev->kdrv = RTE_KDRV_UNKNOWN;
>  
>  	/* device is valid, add in list (sorted) */
>  	if (TAILQ_EMPTY(&pci_device_list)) {
>  		TAILQ_INSERT_TAIL(&pci_device_list, dev, next);
> -	}
> -	else {
> -		struct rte_pci_device *dev2 = NULL;
> +	} else {
> +		struct rte_pci_device *dev2;
>  		int ret;
>  
>  		TAILQ_FOREACH(dev2, &pci_device_list, next) {
>  			ret = rte_eal_compare_pci_addr(&dev->addr, &dev2->addr);
>  			if (ret > 0)
>  				continue;
> -			else if (ret < 0) {
> +
> +			if (ret < 0) {
>  				TAILQ_INSERT_BEFORE(dev2, dev, next);
> -				return 0;
>  			} else { /* already registered */
>  				dev2->kdrv = dev->kdrv;
>  				dev2->max_vfs = dev->max_vfs;
> -				memmove(dev2->mem_resource,
> -					dev->mem_resource,
> +				memmove(dev2->mem_resource, dev->mem_resource,
>  					sizeof(dev->mem_resource));
>  				free(dev);
> -				return 0;
>  			}
> +			return 0;
>  		}
>  		TAILQ_INSERT_TAIL(&pci_device_list, dev, next);
>  	}
  
Stephen Hemminger April 14, 2015, 5:28 p.m. UTC | #2
There is no need to initialize a variable that is only used as a loop
variable.



On Tue, Apr 14, 2015 at 2:30 AM, Qiu, Michael <michael.qiu@intel.com> wrote:

> On 4/14/2015 6:11 AM, Stephen Hemminger wrote:
> > Do some cleanup of pci scan loop.
> >   * check errors first
> >   * don't initialize variables where not necessary
>
> Why? It should be better to initialize variables when define it.
>
> Thanks,
> Michael
> >   * cuddle else (follow existing style)
> >   * chop off conditional after return
> >
> > Signed-off-by: Stephen Hemminger <stephen@networkplumber.org>
> >
> > ---
> >  lib/librte_eal/linuxapp/eal/eal_pci.c | 24 ++++++++++++------------
> >  1 file changed, 12 insertions(+), 12 deletions(-)
> >
> > diff --git a/lib/librte_eal/linuxapp/eal/eal_pci.c
> b/lib/librte_eal/linuxapp/eal/eal_pci.c
> > index c98a778..d96b1c4 100644
> > --- a/lib/librte_eal/linuxapp/eal/eal_pci.c
> > +++ b/lib/librte_eal/linuxapp/eal/eal_pci.c
> > @@ -337,6 +337,12 @@ pci_scan_one(const char *dirname, uint16_t domain,
> uint8_t bus,
> >       /* parse driver */
> >       snprintf(filename, sizeof(filename), "%s/driver", dirname);
> >       ret = pci_get_kernel_driver_by_path(filename, driver);
> > +     if (ret < 0) {
> > +             RTE_LOG(ERR, EAL, "Fail to get kernel driver\n");
> > +             free(dev);
> > +             return -1;
> > +     }
> > +
> >       if (!ret) {
> >               if (!strcmp(driver, "vfio-pci"))
> >                       dev->kdrv = RTE_KDRV_VFIO;
> > @@ -346,37 +352,31 @@ pci_scan_one(const char *dirname, uint16_t domain,
> uint8_t bus,
> >                       dev->kdrv = RTE_KDRV_UIO_GENERIC;
> >               else
> >                       dev->kdrv = RTE_KDRV_UNKNOWN;
> > -     } else if (ret < 0) {
> > -             RTE_LOG(ERR, EAL, "Fail to get kernel driver\n");
> > -             free(dev);
> > -             return -1;
> >       } else
> >               dev->kdrv = RTE_KDRV_UNKNOWN;
> >
> >       /* device is valid, add in list (sorted) */
> >       if (TAILQ_EMPTY(&pci_device_list)) {
> >               TAILQ_INSERT_TAIL(&pci_device_list, dev, next);
> > -     }
> > -     else {
> > -             struct rte_pci_device *dev2 = NULL;
> > +     } else {
> > +             struct rte_pci_device *dev2;
> >               int ret;
> >
> >               TAILQ_FOREACH(dev2, &pci_device_list, next) {
> >                       ret = rte_eal_compare_pci_addr(&dev->addr,
> &dev2->addr);
> >                       if (ret > 0)
> >                               continue;
> > -                     else if (ret < 0) {
> > +
> > +                     if (ret < 0) {
> >                               TAILQ_INSERT_BEFORE(dev2, dev, next);
> > -                             return 0;
> >                       } else { /* already registered */
> >                               dev2->kdrv = dev->kdrv;
> >                               dev2->max_vfs = dev->max_vfs;
> > -                             memmove(dev2->mem_resource,
> > -                                     dev->mem_resource,
> > +                             memmove(dev2->mem_resource,
> dev->mem_resource,
> >                                       sizeof(dev->mem_resource));
> >                               free(dev);
> > -                             return 0;
> >                       }
> > +                     return 0;
> >               }
> >               TAILQ_INSERT_TAIL(&pci_device_list, dev, next);
> >       }
>
>
  
Bruce Richardson May 19, 2015, 10:12 a.m. UTC | #3
On Mon, Apr 13, 2015 at 03:11:11PM -0700, Stephen Hemminger wrote:
> Do some cleanup of pci scan loop.
>   * check errors first
>   * don't initialize variables where not necessary
>   * cuddle else (follow existing style)
>   * chop off conditional after return
> 
> Signed-off-by: Stephen Hemminger <stephen@networkplumber.org>

Acked-by: Bruce Richardson <bruce.richardson@intel.com>

> 
> ---
>  lib/librte_eal/linuxapp/eal/eal_pci.c | 24 ++++++++++++------------
>  1 file changed, 12 insertions(+), 12 deletions(-)
> 
> diff --git a/lib/librte_eal/linuxapp/eal/eal_pci.c b/lib/librte_eal/linuxapp/eal/eal_pci.c
> index c98a778..d96b1c4 100644
> --- a/lib/librte_eal/linuxapp/eal/eal_pci.c
> +++ b/lib/librte_eal/linuxapp/eal/eal_pci.c
> @@ -337,6 +337,12 @@ pci_scan_one(const char *dirname, uint16_t domain, uint8_t bus,
>  	/* parse driver */
>  	snprintf(filename, sizeof(filename), "%s/driver", dirname);
>  	ret = pci_get_kernel_driver_by_path(filename, driver);
> +	if (ret < 0) {
> +		RTE_LOG(ERR, EAL, "Fail to get kernel driver\n");
> +		free(dev);
> +		return -1;
> +	}
> +
>  	if (!ret) {
>  		if (!strcmp(driver, "vfio-pci"))
>  			dev->kdrv = RTE_KDRV_VFIO;
> @@ -346,37 +352,31 @@ pci_scan_one(const char *dirname, uint16_t domain, uint8_t bus,
>  			dev->kdrv = RTE_KDRV_UIO_GENERIC;
>  		else
>  			dev->kdrv = RTE_KDRV_UNKNOWN;
> -	} else if (ret < 0) {
> -		RTE_LOG(ERR, EAL, "Fail to get kernel driver\n");
> -		free(dev);
> -		return -1;
>  	} else
>  		dev->kdrv = RTE_KDRV_UNKNOWN;
>  
>  	/* device is valid, add in list (sorted) */
>  	if (TAILQ_EMPTY(&pci_device_list)) {
>  		TAILQ_INSERT_TAIL(&pci_device_list, dev, next);
> -	}
> -	else {
> -		struct rte_pci_device *dev2 = NULL;
> +	} else {
> +		struct rte_pci_device *dev2;
>  		int ret;
>  
>  		TAILQ_FOREACH(dev2, &pci_device_list, next) {
>  			ret = rte_eal_compare_pci_addr(&dev->addr, &dev2->addr);
>  			if (ret > 0)
>  				continue;
> -			else if (ret < 0) {
> +
> +			if (ret < 0) {
>  				TAILQ_INSERT_BEFORE(dev2, dev, next);
> -				return 0;
>  			} else { /* already registered */
>  				dev2->kdrv = dev->kdrv;
>  				dev2->max_vfs = dev->max_vfs;
> -				memmove(dev2->mem_resource,
> -					dev->mem_resource,
> +				memmove(dev2->mem_resource, dev->mem_resource,
>  					sizeof(dev->mem_resource));
>  				free(dev);
> -				return 0;
>  			}
> +			return 0;
>  		}
>  		TAILQ_INSERT_TAIL(&pci_device_list, dev, next);
>  	}
> -- 
> 2.1.4
>
  

Patch

diff --git a/lib/librte_eal/linuxapp/eal/eal_pci.c b/lib/librte_eal/linuxapp/eal/eal_pci.c
index c98a778..d96b1c4 100644
--- a/lib/librte_eal/linuxapp/eal/eal_pci.c
+++ b/lib/librte_eal/linuxapp/eal/eal_pci.c
@@ -337,6 +337,12 @@  pci_scan_one(const char *dirname, uint16_t domain, uint8_t bus,
 	/* parse driver */
 	snprintf(filename, sizeof(filename), "%s/driver", dirname);
 	ret = pci_get_kernel_driver_by_path(filename, driver);
+	if (ret < 0) {
+		RTE_LOG(ERR, EAL, "Fail to get kernel driver\n");
+		free(dev);
+		return -1;
+	}
+
 	if (!ret) {
 		if (!strcmp(driver, "vfio-pci"))
 			dev->kdrv = RTE_KDRV_VFIO;
@@ -346,37 +352,31 @@  pci_scan_one(const char *dirname, uint16_t domain, uint8_t bus,
 			dev->kdrv = RTE_KDRV_UIO_GENERIC;
 		else
 			dev->kdrv = RTE_KDRV_UNKNOWN;
-	} else if (ret < 0) {
-		RTE_LOG(ERR, EAL, "Fail to get kernel driver\n");
-		free(dev);
-		return -1;
 	} else
 		dev->kdrv = RTE_KDRV_UNKNOWN;
 
 	/* device is valid, add in list (sorted) */
 	if (TAILQ_EMPTY(&pci_device_list)) {
 		TAILQ_INSERT_TAIL(&pci_device_list, dev, next);
-	}
-	else {
-		struct rte_pci_device *dev2 = NULL;
+	} else {
+		struct rte_pci_device *dev2;
 		int ret;
 
 		TAILQ_FOREACH(dev2, &pci_device_list, next) {
 			ret = rte_eal_compare_pci_addr(&dev->addr, &dev2->addr);
 			if (ret > 0)
 				continue;
-			else if (ret < 0) {
+
+			if (ret < 0) {
 				TAILQ_INSERT_BEFORE(dev2, dev, next);
-				return 0;
 			} else { /* already registered */
 				dev2->kdrv = dev->kdrv;
 				dev2->max_vfs = dev->max_vfs;
-				memmove(dev2->mem_resource,
-					dev->mem_resource,
+				memmove(dev2->mem_resource, dev->mem_resource,
 					sizeof(dev->mem_resource));
 				free(dev);
-				return 0;
 			}
+			return 0;
 		}
 		TAILQ_INSERT_TAIL(&pci_device_list, dev, next);
 	}