[dpdk-dev] net/virtio-user: fix not working on 32-bit system

Message ID 1492092776-57008-1-git-send-email-jianfeng.tan@intel.com (mailing list archive)
State Superseded, archived
Delegated to: Yuanhan Liu
Headers

Checks

Context Check Description
ci/checkpatch success coding style OK
ci/Intel-compilation success Compilation OK

Commit Message

Jianfeng Tan April 13, 2017, 2:12 p.m. UTC
  virtio-user cannot work on 32-bit system as higher 32-bit of the
addr field (64-bit) in the desc is filled with non-zero value
which should not happen for a 32-bit system.

This is a regression bug. For 32-bit system, the first 4 bytes
is the virtual address, with following 8 bytes pointing to
physical addr. With below wrong definition, both virtual address
and lower 4 bytes of physical addr are obtained.
  #define VIRTIO_MBUF_ADDR(mb, vq) \
	(*(uint64_t *)((uintptr_t)(mb) + (vq)->offset))

Fixes: 25f80d108780 ("net/virtio: fix packet corruption")
Cc: stable@dpdk.org

Signed-off-by: Jianfeng Tan <jianfeng.tan@intel.com>
---
 drivers/net/virtio/virtqueue.h | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
  

Comments

Yuanhan Liu April 14, 2017, 5:32 a.m. UTC | #1
On Thu, Apr 13, 2017 at 02:12:56PM +0000, Jianfeng Tan wrote:
> virtio-user cannot work on 32-bit system as higher 32-bit of the
> addr field (64-bit) in the desc is filled with non-zero value
> which should not happen for a 32-bit system.
> 
> This is a regression bug. For 32-bit system, the first 4 bytes
> is the virtual address, with following 8 bytes pointing to
> physical addr.

It took me a while to understand that you were trying to say "the first
4 bytes __of mbuf__ is ...".

> With below wrong definition, both virtual address
> and lower 4 bytes of physical addr are obtained.

Again, it's not complete. Something like "in the case of virtio-user,
buf_addr will be used for filling the desc addr, ...". will make it much
easier to understand.


>   #define VIRTIO_MBUF_ADDR(mb, vq) \
> 	(*(uint64_t *)((uintptr_t)(mb) + (vq)->offset))
> 
> Fixes: 25f80d108780 ("net/virtio: fix packet corruption")
> Cc: stable@dpdk.org
> 
> Signed-off-by: Jianfeng Tan <jianfeng.tan@intel.com>
> ---
>  drivers/net/virtio/virtqueue.h | 3 ++-
>  1 file changed, 2 insertions(+), 1 deletion(-)
> 
> diff --git a/drivers/net/virtio/virtqueue.h b/drivers/net/virtio/virtqueue.h
> index f9e3736..f43ea70 100644
> --- a/drivers/net/virtio/virtqueue.h
> +++ b/drivers/net/virtio/virtqueue.h
> @@ -72,7 +72,8 @@ struct rte_mbuf;
>   * Return the physical address (or virtual address in case of
>   * virtio-user) of mbuf data buffer.
>   */
> -#define VIRTIO_MBUF_ADDR(mb, vq) (*(uint64_t *)((uintptr_t)(mb) + (vq)->offset))
> +#define VIRTIO_MBUF_ADDR(mb, vq) \
> +	((uint64_t)((uintptr_t)(*(void **)((uintptr_t)(mb) + (vq)->offset))))

The "void **" cast makes it a bit complex (thus hard to read). I think
following should work?

    (uint64_t(*(uintptr_t *)((uintptr_t)(mb) + (vq)->offset)))

Besides, it deserves a comment.

	--yliu

>  #else
>  #define VIRTIO_MBUF_ADDR(mb, vq) ((mb)->buf_physaddr)
>  #endif
> -- 
> 2.7.4
  
Jianfeng Tan April 14, 2017, 5:53 a.m. UTC | #2
> -----Original Message-----
> From: Yuanhan Liu [mailto:yuanhan.liu@linux.intel.com]
> Sent: Friday, April 14, 2017 1:32 PM
> To: Tan, Jianfeng
> Cc: dev@dpdk.org; olivier.matz@6wind.com; stable@dpdk.org
> Subject: Re: [PATCH] net/virtio-user: fix not working on 32-bit system
> 
> On Thu, Apr 13, 2017 at 02:12:56PM +0000, Jianfeng Tan wrote:
> > virtio-user cannot work on 32-bit system as higher 32-bit of the
> > addr field (64-bit) in the desc is filled with non-zero value
> > which should not happen for a 32-bit system.
> >
> > This is a regression bug. For 32-bit system, the first 4 bytes
> > is the virtual address, with following 8 bytes pointing to
> > physical addr.
> 
> It took me a while to understand that you were trying to say "the first
> 4 bytes __of mbuf__ is ...".

Oops, yes, missed that.

> 
> > With below wrong definition, both virtual address
> > and lower 4 bytes of physical addr are obtained.
> 
> Again, it's not complete. Something like "in the case of virtio-user,
> buf_addr will be used for filling the desc addr, ...". will make it much
> easier to understand.

Yes.

> 
> 
> >   #define VIRTIO_MBUF_ADDR(mb, vq) \
> > 	(*(uint64_t *)((uintptr_t)(mb) + (vq)->offset))
> >
> > Fixes: 25f80d108780 ("net/virtio: fix packet corruption")
> > Cc: stable@dpdk.org
> >
> > Signed-off-by: Jianfeng Tan <jianfeng.tan@intel.com>
> > ---
> >  drivers/net/virtio/virtqueue.h | 3 ++-
> >  1 file changed, 2 insertions(+), 1 deletion(-)
> >
> > diff --git a/drivers/net/virtio/virtqueue.h b/drivers/net/virtio/virtqueue.h
> > index f9e3736..f43ea70 100644
> > --- a/drivers/net/virtio/virtqueue.h
> > +++ b/drivers/net/virtio/virtqueue.h
> > @@ -72,7 +72,8 @@ struct rte_mbuf;
> >   * Return the physical address (or virtual address in case of
> >   * virtio-user) of mbuf data buffer.
> >   */
> > -#define VIRTIO_MBUF_ADDR(mb, vq) (*(uint64_t *)((uintptr_t)(mb) +
> (vq)->offset))
> > +#define VIRTIO_MBUF_ADDR(mb, vq) \
> > +	((uint64_t)((uintptr_t)(*(void **)((uintptr_t)(mb) + (vq)->offset))))
> 
> The "void **" cast makes it a bit complex (thus hard to read). I think
> following should work?

Yes, uintptr_t can work. I thought void ** is easier to understand, meaning a convert to a pointer which pointing to a pointer. I usually use uintptr_t only for converter from pointer to integer, not the opposite way.

> 
>     (uint64_t(*(uintptr_t *)((uintptr_t)(mb) + (vq)->offset)))
> 
> Besides, it deserves a comment.

Will add comment in next version.

Thanks,
Jianfeng

> 
> 	--yliu
> 
> >  #else
> >  #define VIRTIO_MBUF_ADDR(mb, vq) ((mb)->buf_physaddr)
> >  #endif
> > --
> > 2.7.4
  
Yuanhan Liu April 14, 2017, 6:20 a.m. UTC | #3
On Fri, Apr 14, 2017 at 05:53:55AM +0000, Tan, Jianfeng wrote:
> > > diff --git a/drivers/net/virtio/virtqueue.h b/drivers/net/virtio/virtqueue.h
> > > index f9e3736..f43ea70 100644
> > > --- a/drivers/net/virtio/virtqueue.h
> > > +++ b/drivers/net/virtio/virtqueue.h
> > > @@ -72,7 +72,8 @@ struct rte_mbuf;
> > >   * Return the physical address (or virtual address in case of
> > >   * virtio-user) of mbuf data buffer.
> > >   */
> > > -#define VIRTIO_MBUF_ADDR(mb, vq) (*(uint64_t *)((uintptr_t)(mb) +
> > (vq)->offset))
> > > +#define VIRTIO_MBUF_ADDR(mb, vq) \
> > > +	((uint64_t)((uintptr_t)(*(void **)((uintptr_t)(mb) + (vq)->offset))))
> > 
> > The "void **" cast makes it a bit complex (thus hard to read). I think
> > following should work?
> 
> Yes, uintptr_t can work. I thought void ** is easier to understand, meaning a convert to a pointer which pointing to a pointer.

It's twisted, isn't it? :)

> I usually use uintptr_t only for converter from pointer to integer, not the opposite way.

Yes, that's a typical usage. But the fact of the matter is uintptr_t
represents the word size, which is exactly what needed in this case.

	--yliu
> 
> > 
> >     (uint64_t(*(uintptr_t *)((uintptr_t)(mb) + (vq)->offset)))
> > 
> > Besides, it deserves a comment.
> 
> Will add comment in next version.
> 
> Thanks,
> Jianfeng
> 
> > 
> > 	--yliu
> > 
> > >  #else
> > >  #define VIRTIO_MBUF_ADDR(mb, vq) ((mb)->buf_physaddr)
> > >  #endif
> > > --
> > > 2.7.4
  
Jianfeng Tan April 14, 2017, 6:56 a.m. UTC | #4
> -----Original Message-----
> From: Yuanhan Liu [mailto:yuanhan.liu@linux.intel.com]
> Sent: Friday, April 14, 2017 2:20 PM
> To: Tan, Jianfeng
> Cc: dev@dpdk.org; olivier.matz@6wind.com; stable@dpdk.org
> Subject: Re: [PATCH] net/virtio-user: fix not working on 32-bit system
> 
> On Fri, Apr 14, 2017 at 05:53:55AM +0000, Tan, Jianfeng wrote:
> > > > diff --git a/drivers/net/virtio/virtqueue.h
> b/drivers/net/virtio/virtqueue.h
> > > > index f9e3736..f43ea70 100644
> > > > --- a/drivers/net/virtio/virtqueue.h
> > > > +++ b/drivers/net/virtio/virtqueue.h
> > > > @@ -72,7 +72,8 @@ struct rte_mbuf;
> > > >   * Return the physical address (or virtual address in case of
> > > >   * virtio-user) of mbuf data buffer.
> > > >   */
> > > > -#define VIRTIO_MBUF_ADDR(mb, vq) (*(uint64_t *)((uintptr_t)(mb)
> +
> > > (vq)->offset))
> > > > +#define VIRTIO_MBUF_ADDR(mb, vq) \
> > > > +	((uint64_t)((uintptr_t)(*(void **)((uintptr_t)(mb) + (vq)->offset))))
> > >
> > > The "void **" cast makes it a bit complex (thus hard to read). I think
> > > following should work?
> >
> > Yes, uintptr_t can work. I thought void ** is easier to understand, meaning
> a convert to a pointer which pointing to a pointer.
> 
> It's twisted, isn't it? :)
> 
> > I usually use uintptr_t only for converter from pointer to integer, not the
> opposite way.
> 
> Yes, that's a typical usage. But the fact of the matter is uintptr_t
> represents the word size, which is exactly what needed in this case.

Another fold, if you refer to the definition of struct rte_mbuf, the first field is defined as void * instead of uintptr_t. I think that why I prefer to use ((void *)*) in the beginning.

Thanks,
Jianfeng

> 
> 	--yliu
> >
> > >
> > >     (uint64_t(*(uintptr_t *)((uintptr_t)(mb) + (vq)->offset)))
> > >
> > > Besides, it deserves a comment.
> >
> > Will add comment in next version.
> >
> > Thanks,
> > Jianfeng
> >
> > >
> > > 	--yliu
> > >
> > > >  #else
> > > >  #define VIRTIO_MBUF_ADDR(mb, vq) ((mb)->buf_physaddr)
> > > >  #endif
> > > > --
> > > > 2.7.4
  
Yuanhan Liu April 14, 2017, 7:01 a.m. UTC | #5
On Fri, Apr 14, 2017 at 06:56:01AM +0000, Tan, Jianfeng wrote:
> 
> 
> > -----Original Message-----
> > From: Yuanhan Liu [mailto:yuanhan.liu@linux.intel.com]
> > Sent: Friday, April 14, 2017 2:20 PM
> > To: Tan, Jianfeng
> > Cc: dev@dpdk.org; olivier.matz@6wind.com; stable@dpdk.org
> > Subject: Re: [PATCH] net/virtio-user: fix not working on 32-bit system
> > 
> > On Fri, Apr 14, 2017 at 05:53:55AM +0000, Tan, Jianfeng wrote:
> > > > > diff --git a/drivers/net/virtio/virtqueue.h
> > b/drivers/net/virtio/virtqueue.h
> > > > > index f9e3736..f43ea70 100644
> > > > > --- a/drivers/net/virtio/virtqueue.h
> > > > > +++ b/drivers/net/virtio/virtqueue.h
> > > > > @@ -72,7 +72,8 @@ struct rte_mbuf;
> > > > >   * Return the physical address (or virtual address in case of
> > > > >   * virtio-user) of mbuf data buffer.
> > > > >   */
> > > > > -#define VIRTIO_MBUF_ADDR(mb, vq) (*(uint64_t *)((uintptr_t)(mb)
> > +
> > > > (vq)->offset))
> > > > > +#define VIRTIO_MBUF_ADDR(mb, vq) \
> > > > > +	((uint64_t)((uintptr_t)(*(void **)((uintptr_t)(mb) + (vq)->offset))))
> > > >
> > > > The "void **" cast makes it a bit complex (thus hard to read). I think
> > > > following should work?
> > >
> > > Yes, uintptr_t can work. I thought void ** is easier to understand, meaning
> > a convert to a pointer which pointing to a pointer.
> > 
> > It's twisted, isn't it? :)
> > 
> > > I usually use uintptr_t only for converter from pointer to integer, not the
> > opposite way.
> > 
> > Yes, that's a typical usage. But the fact of the matter is uintptr_t
> > represents the word size, which is exactly what needed in this case.
> 
> Another fold, if you refer to the definition of struct rte_mbuf, the first field is defined as void * instead of uintptr_t. I think that why I prefer to use ((void *)*) in the beginning.

But the type is hidden here: isn't this the purpose you were introducing
the "offset" here? Besides, it could be type "phys_addr_t".

	--yliu
  
Jianfeng Tan April 14, 2017, 7:14 a.m. UTC | #6
> -----Original Message-----
> From: Yuanhan Liu [mailto:yuanhan.liu@linux.intel.com]
> Sent: Friday, April 14, 2017 3:01 PM
> To: Tan, Jianfeng
> Cc: dev@dpdk.org; olivier.matz@6wind.com; stable@dpdk.org
> Subject: Re: [PATCH] net/virtio-user: fix not working on 32-bit system
> 
> On Fri, Apr 14, 2017 at 06:56:01AM +0000, Tan, Jianfeng wrote:
> >
> >
> > > -----Original Message-----
> > > From: Yuanhan Liu [mailto:yuanhan.liu@linux.intel.com]
> > > Sent: Friday, April 14, 2017 2:20 PM
> > > To: Tan, Jianfeng
> > > Cc: dev@dpdk.org; olivier.matz@6wind.com; stable@dpdk.org
> > > Subject: Re: [PATCH] net/virtio-user: fix not working on 32-bit system
> > >
> > > On Fri, Apr 14, 2017 at 05:53:55AM +0000, Tan, Jianfeng wrote:
> > > > > > diff --git a/drivers/net/virtio/virtqueue.h
> > > b/drivers/net/virtio/virtqueue.h
> > > > > > index f9e3736..f43ea70 100644
> > > > > > --- a/drivers/net/virtio/virtqueue.h
> > > > > > +++ b/drivers/net/virtio/virtqueue.h
> > > > > > @@ -72,7 +72,8 @@ struct rte_mbuf;
> > > > > >   * Return the physical address (or virtual address in case of
> > > > > >   * virtio-user) of mbuf data buffer.
> > > > > >   */
> > > > > > -#define VIRTIO_MBUF_ADDR(mb, vq) (*(uint64_t
> *)((uintptr_t)(mb)
> > > +
> > > > > (vq)->offset))
> > > > > > +#define VIRTIO_MBUF_ADDR(mb, vq) \
> > > > > > +	((uint64_t)((uintptr_t)(*(void **)((uintptr_t)(mb) + (vq)-
> >offset))))
> > > > >
> > > > > The "void **" cast makes it a bit complex (thus hard to read). I think
> > > > > following should work?
> > > >
> > > > Yes, uintptr_t can work. I thought void ** is easier to understand,
> meaning
> > > a convert to a pointer which pointing to a pointer.
> > >
> > > It's twisted, isn't it? :)
> > >
> > > > I usually use uintptr_t only for converter from pointer to integer, not
> the
> > > opposite way.
> > >
> > > Yes, that's a typical usage. But the fact of the matter is uintptr_t
> > > represents the word size, which is exactly what needed in this case.
> >
> > Another fold, if you refer to the definition of struct rte_mbuf, the first field
> is defined as void * instead of uintptr_t. I think that why I prefer to use ((void
> *)*) in the beginning.
> 
> But the type is hidden here: isn't this the purpose you were introducing
> the "offset" here? Besides, it could be type "phys_addr_t".
> 

Fair enough.
  

Patch

diff --git a/drivers/net/virtio/virtqueue.h b/drivers/net/virtio/virtqueue.h
index f9e3736..f43ea70 100644
--- a/drivers/net/virtio/virtqueue.h
+++ b/drivers/net/virtio/virtqueue.h
@@ -72,7 +72,8 @@  struct rte_mbuf;
  * Return the physical address (or virtual address in case of
  * virtio-user) of mbuf data buffer.
  */
-#define VIRTIO_MBUF_ADDR(mb, vq) (*(uint64_t *)((uintptr_t)(mb) + (vq)->offset))
+#define VIRTIO_MBUF_ADDR(mb, vq) \
+	((uint64_t)((uintptr_t)(*(void **)((uintptr_t)(mb) + (vq)->offset))))
 #else
 #define VIRTIO_MBUF_ADDR(mb, vq) ((mb)->buf_physaddr)
 #endif