[v13,02/19] bus/pci: fix PCI address compare

Message ID 20180712011514.45006-3-qi.z.zhang@intel.com (mailing list archive)
State Superseded, archived
Delegated to: Thomas Monjalon
Headers
Series [v13,01/19] ethdev: add function to release port in local process |

Checks

Context Check Description
ci/checkpatch success coding style OK
ci/Intel-compilation fail apply issues

Commit Message

Qi Zhang July 12, 2018, 1:14 a.m. UTC
  When use memcmp to compare two PCI address, sizeof(struct rte_pci_addr)
is 4 bytes aligned, and it is 8. While only 7 byte of struct rte_pci_addr
is valid. So compare the 8th byte will cause the unexpected result, which
happens when repeatedly attach/detach a device.

Fixes: c752998b5e2e ("pci: introduce library and driver")
Cc: stable@dpdk.org

Signed-off-by: Qi Zhang <qi.z.zhang@intel.com>
---
 drivers/bus/pci/linux/pci_vfio.c | 13 ++++++++++++-
 1 file changed, 12 insertions(+), 1 deletion(-)
  

Comments

Burakov, Anatoly July 12, 2018, 9:24 a.m. UTC | #1
On 12-Jul-18 2:14 AM, Qi Zhang wrote:
> When use memcmp to compare two PCI address, sizeof(struct rte_pci_addr)
> is 4 bytes aligned, and it is 8. While only 7 byte of struct rte_pci_addr
> is valid. So compare the 8th byte will cause the unexpected result, which
> happens when repeatedly attach/detach a device.
> 
> Fixes: c752998b5e2e ("pci: introduce library and driver")
> Cc: stable@dpdk.org
> 
> Signed-off-by: Qi Zhang <qi.z.zhang@intel.com>
> ---
>   drivers/bus/pci/linux/pci_vfio.c | 13 ++++++++++++-
>   1 file changed, 12 insertions(+), 1 deletion(-)
> 
> diff --git a/drivers/bus/pci/linux/pci_vfio.c b/drivers/bus/pci/linux/pci_vfio.c
> index aeeaa9ed8..dd25c3542 100644
> --- a/drivers/bus/pci/linux/pci_vfio.c
> +++ b/drivers/bus/pci/linux/pci_vfio.c
> @@ -43,6 +43,17 @@ static struct rte_tailq_elem rte_vfio_tailq = {
>   };
>   EAL_REGISTER_TAILQ(rte_vfio_tailq)
>   
> +/* Compair two pci address */
> +static int pci_addr_cmp(struct rte_pci_addr *addr1, struct rte_pci_addr *addr2)
> +{
> +	if (addr1->domain == addr2->domain &&
> +		addr1->bus == addr2->bus &&
> +		addr1->devid == addr2->devid &&
> +		addr1->function == addr2->function)
> +		return 0;
> +	return 1;
> +}

Generally, change looks OK to me, but I think we already have this 
function in PCI library - rte_pci_addr_cmp(). Is there a specific reason 
to reimplement it here?

> +
>   int
>   pci_vfio_read_config(const struct rte_intr_handle *intr_handle,
>   		    void *buf, size_t len, off_t offs)
> @@ -642,7 +653,7 @@ pci_vfio_unmap_resource(struct rte_pci_device *dev)
>   	vfio_res_list = RTE_TAILQ_CAST(rte_vfio_tailq.head, mapped_pci_res_list);
>   	/* Get vfio_res */
>   	TAILQ_FOREACH(vfio_res, vfio_res_list, next) {
> -		if (memcmp(&vfio_res->pci_addr, &dev->addr, sizeof(dev->addr)))
> +		if (pci_addr_cmp(&vfio_res->pci_addr, &dev->addr))
>   			continue;
>   		break;
>   	}
>
  
Gaëtan Rivet July 12, 2018, 9:32 a.m. UTC | #2
Hi,

On Thu, Jul 12, 2018 at 10:24:44AM +0100, Burakov, Anatoly wrote:
> On 12-Jul-18 2:14 AM, Qi Zhang wrote:
> > When use memcmp to compare two PCI address, sizeof(struct rte_pci_addr)
> > is 4 bytes aligned, and it is 8. While only 7 byte of struct rte_pci_addr
> > is valid. So compare the 8th byte will cause the unexpected result, which
> > happens when repeatedly attach/detach a device.
> > 
> > Fixes: c752998b5e2e ("pci: introduce library and driver")

Shouldn't be the original commit be

Fixes: 94c0776b1bad ("vfio: support hotplug")

instead?

> > Cc: stable@dpdk.org
> > 
> > Signed-off-by: Qi Zhang <qi.z.zhang@intel.com>
> > ---
> >   drivers/bus/pci/linux/pci_vfio.c | 13 ++++++++++++-
> >   1 file changed, 12 insertions(+), 1 deletion(-)
> > 
> > diff --git a/drivers/bus/pci/linux/pci_vfio.c b/drivers/bus/pci/linux/pci_vfio.c
> > index aeeaa9ed8..dd25c3542 100644
> > --- a/drivers/bus/pci/linux/pci_vfio.c
> > +++ b/drivers/bus/pci/linux/pci_vfio.c
> > @@ -43,6 +43,17 @@ static struct rte_tailq_elem rte_vfio_tailq = {
> >   };
> >   EAL_REGISTER_TAILQ(rte_vfio_tailq)
> > +/* Compair two pci address */
> > +static int pci_addr_cmp(struct rte_pci_addr *addr1, struct rte_pci_addr *addr2)
> > +{
> > +	if (addr1->domain == addr2->domain &&
> > +		addr1->bus == addr2->bus &&
> > +		addr1->devid == addr2->devid &&
> > +		addr1->function == addr2->function)
> > +		return 0;
> > +	return 1;
> > +}
> 
> Generally, change looks OK to me, but I think we already have this function
> in PCI library - rte_pci_addr_cmp(). Is there a specific reason to
> reimplement it here?
> 

+1

> > +
> >   int
> >   pci_vfio_read_config(const struct rte_intr_handle *intr_handle,
> >   		    void *buf, size_t len, off_t offs)
> > @@ -642,7 +653,7 @@ pci_vfio_unmap_resource(struct rte_pci_device *dev)
> >   	vfio_res_list = RTE_TAILQ_CAST(rte_vfio_tailq.head, mapped_pci_res_list);
> >   	/* Get vfio_res */
> >   	TAILQ_FOREACH(vfio_res, vfio_res_list, next) {
> > -		if (memcmp(&vfio_res->pci_addr, &dev->addr, sizeof(dev->addr)))
> > +		if (pci_addr_cmp(&vfio_res->pci_addr, &dev->addr))
> >   			continue;
> >   		break;
> >   	}
> > 
> 
> 
> -- 
> Thanks,
> Anatoly
  
Qi Zhang July 12, 2018, 11:53 a.m. UTC | #3
> -----Original Message-----
> From: Burakov, Anatoly
> Sent: Thursday, July 12, 2018 5:25 PM
> To: Zhang, Qi Z <qi.z.zhang@intel.com>; thomas@monjalon.net
> Cc: Ananyev, Konstantin <konstantin.ananyev@intel.com>; dev@dpdk.org;
> Richardson, Bruce <bruce.richardson@intel.com>; Yigit, Ferruh
> <ferruh.yigit@intel.com>; Shelton, Benjamin H
> <benjamin.h.shelton@intel.com>; Vangati, Narender
> <narender.vangati@intel.com>; stable@dpdk.org
> Subject: Re: [PATCH v13 02/19] bus/pci: fix PCI address compare
> 
> On 12-Jul-18 2:14 AM, Qi Zhang wrote:
> > When use memcmp to compare two PCI address, sizeof(struct
> > rte_pci_addr) is 4 bytes aligned, and it is 8. While only 7 byte of
> > struct rte_pci_addr is valid. So compare the 8th byte will cause the
> > unexpected result, which happens when repeatedly attach/detach a device.
> >
> > Fixes: c752998b5e2e ("pci: introduce library and driver")
> > Cc: stable@dpdk.org
> >
> > Signed-off-by: Qi Zhang <qi.z.zhang@intel.com>
> > ---
> >   drivers/bus/pci/linux/pci_vfio.c | 13 ++++++++++++-
> >   1 file changed, 12 insertions(+), 1 deletion(-)
> >
> > diff --git a/drivers/bus/pci/linux/pci_vfio.c
> > b/drivers/bus/pci/linux/pci_vfio.c
> > index aeeaa9ed8..dd25c3542 100644
> > --- a/drivers/bus/pci/linux/pci_vfio.c
> > +++ b/drivers/bus/pci/linux/pci_vfio.c
> > @@ -43,6 +43,17 @@ static struct rte_tailq_elem rte_vfio_tailq = {
> >   };
> >   EAL_REGISTER_TAILQ(rte_vfio_tailq)
> >
> > +/* Compair two pci address */
> > +static int pci_addr_cmp(struct rte_pci_addr *addr1, struct
> > +rte_pci_addr *addr2) {
> > +	if (addr1->domain == addr2->domain &&
> > +		addr1->bus == addr2->bus &&
> > +		addr1->devid == addr2->devid &&
> > +		addr1->function == addr2->function)
> > +		return 0;
> > +	return 1;
> > +}
> 
> Generally, change looks OK to me, but I think we already have this function in
> PCI library - rte_pci_addr_cmp(). Is there a specific reason to reimplement it
> here?

NO, and rte_pci_addr_cmp is what I want :), thanks!
> 
> > +
> >   int
> >   pci_vfio_read_config(const struct rte_intr_handle *intr_handle,
> >   		    void *buf, size_t len, off_t offs) @@ -642,7 +653,7 @@
> > pci_vfio_unmap_resource(struct rte_pci_device *dev)
> >   	vfio_res_list = RTE_TAILQ_CAST(rte_vfio_tailq.head,
> mapped_pci_res_list);
> >   	/* Get vfio_res */
> >   	TAILQ_FOREACH(vfio_res, vfio_res_list, next) {
> > -		if (memcmp(&vfio_res->pci_addr, &dev->addr, sizeof(dev->addr)))
> > +		if (pci_addr_cmp(&vfio_res->pci_addr, &dev->addr))
> >   			continue;
> >   		break;
> >   	}
> >
> 
> 
> --
> Thanks,
> Anatoly
  
Qi Zhang July 12, 2018, 11:57 a.m. UTC | #4
> -----Original Message-----
> From: Gaëtan Rivet [mailto:gaetan.rivet@6wind.com]
> Sent: Thursday, July 12, 2018 5:32 PM
> To: Burakov, Anatoly <anatoly.burakov@intel.com>
> Cc: Zhang, Qi Z <qi.z.zhang@intel.com>; thomas@monjalon.net; Ananyev,
> Konstantin <konstantin.ananyev@intel.com>; dev@dpdk.org; Richardson,
> Bruce <bruce.richardson@intel.com>; Yigit, Ferruh <ferruh.yigit@intel.com>;
> Shelton, Benjamin H <benjamin.h.shelton@intel.com>; Vangati, Narender
> <narender.vangati@intel.com>; stable@dpdk.org
> Subject: Re: [dpdk-dev] [PATCH v13 02/19] bus/pci: fix PCI address compare
> 
> Hi,
> 
> On Thu, Jul 12, 2018 at 10:24:44AM +0100, Burakov, Anatoly wrote:
> > On 12-Jul-18 2:14 AM, Qi Zhang wrote:
> > > When use memcmp to compare two PCI address, sizeof(struct
> > > rte_pci_addr) is 4 bytes aligned, and it is 8. While only 7 byte of
> > > struct rte_pci_addr is valid. So compare the 8th byte will cause the
> > > unexpected result, which happens when repeatedly attach/detach a
> device.
> > >
> > > Fixes: c752998b5e2e ("pci: introduce library and driver")
> 
> Shouldn't be the original commit be
> 
> Fixes: 94c0776b1bad ("vfio: support hotplug")
> 
> instead?

You are right, this should be one that introduce the issue, thanks!

> 
> > > Cc: stable@dpdk.org
> > >
> > > Signed-off-by: Qi Zhang <qi.z.zhang@intel.com>
> > > ---
> > >   drivers/bus/pci/linux/pci_vfio.c | 13 ++++++++++++-
> > >   1 file changed, 12 insertions(+), 1 deletion(-)
> > >
> > > diff --git a/drivers/bus/pci/linux/pci_vfio.c
> > > b/drivers/bus/pci/linux/pci_vfio.c
> > > index aeeaa9ed8..dd25c3542 100644
> > > --- a/drivers/bus/pci/linux/pci_vfio.c
> > > +++ b/drivers/bus/pci/linux/pci_vfio.c
> > > @@ -43,6 +43,17 @@ static struct rte_tailq_elem rte_vfio_tailq = {
> > >   };
> > >   EAL_REGISTER_TAILQ(rte_vfio_tailq)
> > > +/* Compair two pci address */
> > > +static int pci_addr_cmp(struct rte_pci_addr *addr1, struct
> > > +rte_pci_addr *addr2) {
> > > +	if (addr1->domain == addr2->domain &&
> > > +		addr1->bus == addr2->bus &&
> > > +		addr1->devid == addr2->devid &&
> > > +		addr1->function == addr2->function)
> > > +		return 0;
> > > +	return 1;
> > > +}
> >
> > Generally, change looks OK to me, but I think we already have this
> > function in PCI library - rte_pci_addr_cmp(). Is there a specific
> > reason to reimplement it here?
> >
> 
> +1
> 
> > > +
> > >   int
> > >   pci_vfio_read_config(const struct rte_intr_handle *intr_handle,
> > >   		    void *buf, size_t len, off_t offs) @@ -642,7 +653,7 @@
> > > pci_vfio_unmap_resource(struct rte_pci_device *dev)
> > >   	vfio_res_list = RTE_TAILQ_CAST(rte_vfio_tailq.head,
> mapped_pci_res_list);
> > >   	/* Get vfio_res */
> > >   	TAILQ_FOREACH(vfio_res, vfio_res_list, next) {
> > > -		if (memcmp(&vfio_res->pci_addr, &dev->addr, sizeof(dev->addr)))
> > > +		if (pci_addr_cmp(&vfio_res->pci_addr, &dev->addr))
> > >   			continue;
> > >   		break;
> > >   	}
> > >
> >
> >
> > --
> > Thanks,
> > Anatoly
> 
> --
> Gaëtan Rivet
> 6WIND
  

Patch

diff --git a/drivers/bus/pci/linux/pci_vfio.c b/drivers/bus/pci/linux/pci_vfio.c
index aeeaa9ed8..dd25c3542 100644
--- a/drivers/bus/pci/linux/pci_vfio.c
+++ b/drivers/bus/pci/linux/pci_vfio.c
@@ -43,6 +43,17 @@  static struct rte_tailq_elem rte_vfio_tailq = {
 };
 EAL_REGISTER_TAILQ(rte_vfio_tailq)
 
+/* Compair two pci address */
+static int pci_addr_cmp(struct rte_pci_addr *addr1, struct rte_pci_addr *addr2)
+{
+	if (addr1->domain == addr2->domain &&
+		addr1->bus == addr2->bus &&
+		addr1->devid == addr2->devid &&
+		addr1->function == addr2->function)
+		return 0;
+	return 1;
+}
+
 int
 pci_vfio_read_config(const struct rte_intr_handle *intr_handle,
 		    void *buf, size_t len, off_t offs)
@@ -642,7 +653,7 @@  pci_vfio_unmap_resource(struct rte_pci_device *dev)
 	vfio_res_list = RTE_TAILQ_CAST(rte_vfio_tailq.head, mapped_pci_res_list);
 	/* Get vfio_res */
 	TAILQ_FOREACH(vfio_res, vfio_res_list, next) {
-		if (memcmp(&vfio_res->pci_addr, &dev->addr, sizeof(dev->addr)))
+		if (pci_addr_cmp(&vfio_res->pci_addr, &dev->addr))
 			continue;
 		break;
 	}