[1/1] vhost: fix a double fetch when dequeue offloading

Message ID 91dc12662805a3867413940f856ba9454b91c579.1734588243.git.wangyunjian@huawei.com (mailing list archive)
State Superseded
Delegated to: Maxime Coquelin
Headers
Series [1/1] vhost: fix a double fetch when dequeue offloading |

Checks

Context Check Description
ci/checkpatch success coding style OK
ci/loongarch-compilation success Compilation OK
ci/loongarch-unit-testing success Unit Testing PASS
ci/Intel-compilation success Compilation OK
ci/intel-Testing success Testing PASS
ci/github-robot: build success github build: passed
ci/intel-Functional success Functional PASS
ci/iol-broadcom-Performance success Performance Testing PASS
ci/iol-mellanox-Performance success Performance Testing PASS
ci/iol-marvell-Functional success Functional Testing PASS
ci/iol-intel-Performance success Performance Testing PASS
ci/iol-unit-amd64-testing success Testing PASS
ci/iol-unit-arm64-testing success Testing PASS
ci/iol-intel-Functional success Functional Testing PASS
ci/iol-sample-apps-testing success Testing PASS
ci/iol-compile-arm64-testing success Testing PASS
ci/iol-abi-testing success Testing PASS
ci/iol-compile-amd64-testing success Testing PASS

Commit Message

Wangyunjian(wangyunjian,TongTu) Dec. 19, 2024, 6:38 a.m. UTC
The hdr->csum_start does two successive reads from user space to read a
variable length data structure. The result overflow if the data structure
changes between the two reads.

To fix this, we can prevent double fetch issue by copying virtio_hdr to
the temporary variable.

Fixes: 4dc4e33ffa10 ("net/virtio: fix Rx checksum calculation")
Cc: stable@dpdk.org

Signed-off-by: Yunjian Wang <wangyunjian@huawei.com>
---
 lib/vhost/virtio_net.c | 13 ++++++++-----
 1 file changed, 8 insertions(+), 5 deletions(-)
  

Comments

David Marchand Dec. 19, 2024, 8:24 a.m. UTC | #1
On Thu, Dec 19, 2024 at 7:38 AM Yunjian Wang <wangyunjian@huawei.com> wrote:
>
> The hdr->csum_start does two successive reads from user space to read a
> variable length data structure. The result overflow if the data structure
> changes between the two reads.
>
> To fix this, we can prevent double fetch issue by copying virtio_hdr to
> the temporary variable.
>
> Fixes: 4dc4e33ffa10 ("net/virtio: fix Rx checksum calculation")
> Cc: stable@dpdk.org
>
> Signed-off-by: Yunjian Wang <wangyunjian@huawei.com>
> ---
>  lib/vhost/virtio_net.c | 13 ++++++++-----
>  1 file changed, 8 insertions(+), 5 deletions(-)
>
> diff --git a/lib/vhost/virtio_net.c b/lib/vhost/virtio_net.c
> index 69901ab3b5..5c40ae7069 100644
> --- a/lib/vhost/virtio_net.c
> +++ b/lib/vhost/virtio_net.c
> @@ -2914,10 +2914,12 @@ desc_to_mbuf(struct virtio_net *dev, struct vhost_virtqueue *vq,
>                          * in a contiguous virtual area.
>                          */
>                         copy_vnet_hdr_from_desc(&tmp_hdr, buf_vec);
> -                       hdr = &tmp_hdr;
>                 } else {
> -                       hdr = (struct virtio_net_hdr *)((uintptr_t)buf_vec[0].buf_addr);
> +                       rte_memcpy((void *)(uintptr_t)&tmp_hdr,
> +                               (void *)(uintptr_t)buf_vec[0].buf_addr,
> +                               sizeof(struct virtio_net_hdr));
>                 }
> +               hdr = &tmp_hdr;
>         }

This will need some benchmark, as I remember putting rte_memcpy in
inlined helpers had some performance impact.

Instead, I would call copy_vnet_hdr_from_desc unconditionnally, and
store in a struct virtio_net_hdr hdr variable (+ a has_vnet_hdr
boolean to indicate validity).
Something like:
        if (virtio_net_with_host_offload(dev)) {
-               if (unlikely(buf_vec[0].buf_len < sizeof(struct
virtio_net_hdr))) {
-                       /*
-                        * No luck, the virtio-net header doesn't fit
-                        * in a contiguous virtual area.
-                        */
-                       copy_vnet_hdr_from_desc(&tmp_hdr, buf_vec);
-                       hdr = &tmp_hdr;
-               } else {
-                       hdr = (struct virtio_net_hdr
*)((uintptr_t)buf_vec[0].buf_addr);
-               }
+               copy_vnet_hdr_from_desc(&hdr, buf_vec);
+               has_vnet_hdr = true;
        }

(besides, in copy_vnet_hdr_from_desc, the while (cond) {} loop could
be changed to do a do {} while (cond), and that approach requires
performance numbers too)


>
>         for (vec_idx = 0; vec_idx < nr_vec; vec_idx++) {
> @@ -3363,7 +3365,7 @@ virtio_dev_tx_batch_packed(struct virtio_net *dev,
>  {
>         uint16_t avail_idx = vq->last_avail_idx;
>         uint32_t buf_offset = sizeof(struct virtio_net_hdr_mrg_rxbuf);
> -       struct virtio_net_hdr *hdr;
> +       struct virtio_net_hdr hdr;
>         uintptr_t desc_addrs[PACKED_BATCH_SIZE];
>         uint16_t ids[PACKED_BATCH_SIZE];
>         uint16_t i;
> @@ -3382,8 +3384,9 @@ virtio_dev_tx_batch_packed(struct virtio_net *dev,
>
>         if (virtio_net_with_host_offload(dev)) {
>                 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) {
> -                       hdr = (struct virtio_net_hdr *)(desc_addrs[i]);
> -                       vhost_dequeue_offload(dev, hdr, pkts[i], legacy_ol_flags);
> +                       rte_memcpy((void *)(uintptr_t)&hdr,
> +                               (void *)(uintptr_t)desc_addrs[i], sizeof(struct virtio_net_hdr));
> +                       vhost_dequeue_offload(dev, &hdr, pkts[i], legacy_ol_flags);
>                 }
>         }

Here too, there may be an impact with adding rte_memcpy.
Just do a copy like:

        if (virtio_net_with_host_offload(dev)) {
+               struct virtio_net_hdr hdr;
+
                vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) {
-                       hdr = (struct virtio_net_hdr *)(desc_addrs[i]);
-                       vhost_dequeue_offload(dev, hdr, pkts[i],
legacy_ol_flags);
+                       hdr = *(struct virtio_net_hdr *)(desc_addrs[i]);
+                       vhost_dequeue_offload(dev, &hdr, pkts[i],
legacy_ol_flags);
                }
  
Wangyunjian(wangyunjian,TongTu) Dec. 19, 2024, 11:02 a.m. UTC | #2
> -----Original Message-----
> From: David Marchand [mailto:david.marchand@redhat.com]
> Sent: Thursday, December 19, 2024 4:24 PM
> To: Wangyunjian(wangyunjian,TongTu) <wangyunjian@huawei.com>;
> maxime.coquelin@redhat.com
> Cc: dev@dpdk.org; chenbox@nvidia.com; Lilijun (Jerry)
> <jerry.lilijun@huawei.com>; xiawei (H) <xiawei40@huawei.com>;
> wangzengyuan <wangzengyuan@huawei.com>; stable@dpdk.org
> Subject: Re: [PATCH 1/1] vhost: fix a double fetch when dequeue offloading
> 
> On Thu, Dec 19, 2024 at 7:38 AM Yunjian Wang <wangyunjian@huawei.com>
> wrote:
> >
> > The hdr->csum_start does two successive reads from user space to read a
> > variable length data structure. The result overflow if the data structure
> > changes between the two reads.
> >
> > To fix this, we can prevent double fetch issue by copying virtio_hdr to
> > the temporary variable.
> >
> > Fixes: 4dc4e33ffa10 ("net/virtio: fix Rx checksum calculation")
> > Cc: stable@dpdk.org
> >
> > Signed-off-by: Yunjian Wang <wangyunjian@huawei.com>
> > ---
> >  lib/vhost/virtio_net.c | 13 ++++++++-----
> >  1 file changed, 8 insertions(+), 5 deletions(-)
> >
> > diff --git a/lib/vhost/virtio_net.c b/lib/vhost/virtio_net.c
> > index 69901ab3b5..5c40ae7069 100644
> > --- a/lib/vhost/virtio_net.c
> > +++ b/lib/vhost/virtio_net.c
> > @@ -2914,10 +2914,12 @@ desc_to_mbuf(struct virtio_net *dev, struct
> vhost_virtqueue *vq,
> >                          * in a contiguous virtual area.
> >                          */
> >                         copy_vnet_hdr_from_desc(&tmp_hdr,
> buf_vec);
> > -                       hdr = &tmp_hdr;
> >                 } else {
> > -                       hdr = (struct virtio_net_hdr
> *)((uintptr_t)buf_vec[0].buf_addr);
> > +                       rte_memcpy((void *)(uintptr_t)&tmp_hdr,
> > +                               (void
> *)(uintptr_t)buf_vec[0].buf_addr,
> > +                               sizeof(struct virtio_net_hdr));
> >                 }
> > +               hdr = &tmp_hdr;
> >         }
> 
> This will need some benchmark, as I remember putting rte_memcpy in
> inlined helpers had some performance impact.
> 
> Instead, I would call copy_vnet_hdr_from_desc unconditionnally, and
> store in a struct virtio_net_hdr hdr variable (+ a has_vnet_hdr
> boolean to indicate validity).
> Something like:
>         if (virtio_net_with_host_offload(dev)) {
> -               if (unlikely(buf_vec[0].buf_len < sizeof(struct
> virtio_net_hdr))) {
> -                       /*
> -                        * No luck, the virtio-net header doesn't fit
> -                        * in a contiguous virtual area.
> -                        */
> -                       copy_vnet_hdr_from_desc(&tmp_hdr, buf_vec);
> -                       hdr = &tmp_hdr;
> -               } else {
> -                       hdr = (struct virtio_net_hdr
> *)((uintptr_t)buf_vec[0].buf_addr);
> -               }
> +               copy_vnet_hdr_from_desc(&hdr, buf_vec);
> +               has_vnet_hdr = true;
>         }
> 
> (besides, in copy_vnet_hdr_from_desc, the while (cond) {} loop could
> be changed to do a do {} while (cond), and that approach requires
> performance numbers too)

How about this?
@@ -2904,8 +2904,8 @@ desc_to_mbuf(struct virtio_net *dev, struct vhost_virtqueue *vq,
        uint32_t hdr_remain = dev->vhost_hlen;
        uint32_t cpy_len;
        struct rte_mbuf *cur = m, *prev = m;
-       struct virtio_net_hdr tmp_hdr;
-       struct virtio_net_hdr *hdr = NULL;
+       bool has_vnet_hdr = false;
+       struct virtio_net_hdr hdr;
        uint16_t vec_idx;
        struct vhost_async *async = vq->async;
        struct async_inflight_info *pkts_info;
@@ -2921,11 +2921,11 @@ desc_to_mbuf(struct virtio_net *dev, struct vhost_virtqueue *vq,
                         * No luck, the virtio-net header doesn't fit
                         * in a contiguous virtual area.
                         */
-                       copy_vnet_hdr_from_desc(&tmp_hdr, buf_vec);
-                       hdr = &tmp_hdr;
+                       copy_vnet_hdr_from_desc(&hdr, buf_vec);
                } else {
-                       hdr = (struct virtio_net_hdr *)((uintptr_t)buf_vec[0].buf_addr);
+                       hdr = *(struct virtio_net_hdr *)((uintptr_t)buf_vec[0].buf_addr);
                }
+               has_vnet_hdr = true;
        }


> 
> 
> >
> >         for (vec_idx = 0; vec_idx < nr_vec; vec_idx++) {
> > @@ -3363,7 +3365,7 @@ virtio_dev_tx_batch_packed(struct virtio_net
> *dev,
> >  {
> >         uint16_t avail_idx = vq->last_avail_idx;
> >         uint32_t buf_offset = sizeof(struct virtio_net_hdr_mrg_rxbuf);
> > -       struct virtio_net_hdr *hdr;
> > +       struct virtio_net_hdr hdr;
> >         uintptr_t desc_addrs[PACKED_BATCH_SIZE];
> >         uint16_t ids[PACKED_BATCH_SIZE];
> >         uint16_t i;
> > @@ -3382,8 +3384,9 @@ virtio_dev_tx_batch_packed(struct virtio_net
> *dev,
> >
> >         if (virtio_net_with_host_offload(dev)) {
> >                 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) {
> > -                       hdr = (struct virtio_net_hdr *)(desc_addrs[i]);
> > -                       vhost_dequeue_offload(dev, hdr, pkts[i],
> legacy_ol_flags);
> > +                       rte_memcpy((void *)(uintptr_t)&hdr,
> > +                               (void *)(uintptr_t)desc_addrs[i],
> sizeof(struct virtio_net_hdr));
> > +                       vhost_dequeue_offload(dev, &hdr, pkts[i],
> legacy_ol_flags);
> >                 }
> >         }
> 
> Here too, there may be an impact with adding rte_memcpy.
> Just do a copy like:
> 
>         if (virtio_net_with_host_offload(dev)) {
> +               struct virtio_net_hdr hdr;
> +
>                 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) {
> -                       hdr = (struct virtio_net_hdr *)(desc_addrs[i]);
> -                       vhost_dequeue_offload(dev, hdr, pkts[i],
> legacy_ol_flags);
> +                       hdr = *(struct virtio_net_hdr *)(desc_addrs[i]);
> +                       vhost_dequeue_offload(dev, &hdr, pkts[i],
> legacy_ol_flags);
>                 }
> 

Thanks for your suggestion, will include them in next version.

> 
> --
> David Marchand
>
  
Stephen Hemminger Dec. 19, 2024, 4:15 p.m. UTC | #3
On Thu, 19 Dec 2024 14:38:28 +0800
Yunjian Wang <wangyunjian@huawei.com> wrote:

> -			hdr = (struct virtio_net_hdr *)((uintptr_t)buf_vec[0].buf_addr);
> +			rte_memcpy((void *)(uintptr_t)&tmp_hdr,
> +				(void *)(uintptr_t)buf_vec[0].buf_addr,
> +				sizeof(struct virtio_net_hdr));
>  		}

Do not introduce more rte_memcpy of a fixed size.
You don't need that many casts!
Why can you not use a structure assignment here.
  
Wangyunjian(wangyunjian,TongTu) Dec. 20, 2024, 2:17 a.m. UTC | #4
> -----Original Message-----
> From: Stephen Hemminger [mailto:stephen@networkplumber.org]
> Sent: Friday, December 20, 2024 12:16 AM
> To: Wangyunjian(wangyunjian,TongTu) <wangyunjian@huawei.com>
> Cc: dev@dpdk.org; maxime.coquelin@redhat.com; chenbox@nvidia.com;
> Lilijun (Jerry) <jerry.lilijun@huawei.com>; xiawei (H) <xiawei40@huawei.com>;
> wangzengyuan <wangzengyuan@huawei.com>; stable@dpdk.org
> Subject: Re: [PATCH 1/1] vhost: fix a double fetch when dequeue offloading
> 
> On Thu, 19 Dec 2024 14:38:28 +0800
> Yunjian Wang <wangyunjian@huawei.com> wrote:
> 
> > -			hdr = (struct virtio_net_hdr *)((uintptr_t)buf_vec[0].buf_addr);
> > +			rte_memcpy((void *)(uintptr_t)&tmp_hdr,
> > +				(void *)(uintptr_t)buf_vec[0].buf_addr,
> > +				sizeof(struct virtio_net_hdr));
> >  		}
> 
> Do not introduce more rte_memcpy of a fixed size.
> You don't need that many casts!
> Why can you not use a structure assignment here.

 The virtio_hdr is a shared component, and other fields within it are read
multiple times. This can potentially result in a double fetch scenario.
  
Stephen Hemminger Dec. 20, 2024, 4:59 a.m. UTC | #5
On Fri, 20 Dec 2024 02:17:12 +0000
"Wangyunjian(wangyunjian,TongTu)" <wangyunjian@huawei.com> wrote:

> > -----Original Message-----
> > From: Stephen Hemminger [mailto:stephen@networkplumber.org]
> > Sent: Friday, December 20, 2024 12:16 AM
> > To: Wangyunjian(wangyunjian,TongTu) <wangyunjian@huawei.com>
> > Cc: dev@dpdk.org; maxime.coquelin@redhat.com; chenbox@nvidia.com;
> > Lilijun (Jerry) <jerry.lilijun@huawei.com>; xiawei (H) <xiawei40@huawei.com>;
> > wangzengyuan <wangzengyuan@huawei.com>; stable@dpdk.org
> > Subject: Re: [PATCH 1/1] vhost: fix a double fetch when dequeue offloading
> > 
> > On Thu, 19 Dec 2024 14:38:28 +0800
> > Yunjian Wang <wangyunjian@huawei.com> wrote:
> >   
> > > -			hdr = (struct virtio_net_hdr *)((uintptr_t)buf_vec[0].buf_addr);
> > > +			rte_memcpy((void *)(uintptr_t)&tmp_hdr,
> > > +				(void *)(uintptr_t)buf_vec[0].buf_addr,
> > > +				sizeof(struct virtio_net_hdr));
> > >  		}  
> > 
> > Do not introduce more rte_memcpy of a fixed size.
> > You don't need that many casts!
> > Why can you not use a structure assignment here.  
> 
>  The virtio_hdr is a shared component, and other fields within it are read
> multiple times. This can potentially result in a double fetch scenario.


The point is do a copy, but not with rte_memcpy.
Also you need a rte_compiler_barrier() anyway.
  
Stephen Hemminger Dec. 20, 2024, 4:35 p.m. UTC | #6
On Thu, 19 Dec 2024 14:38:28 +0800
Yunjian Wang <wangyunjian@huawei.com> wrote:

> diff --git a/lib/vhost/virtio_net.c b/lib/vhost/virtio_net.c
> index 69901ab3b5..5c40ae7069 100644
> --- a/lib/vhost/virtio_net.c
> +++ b/lib/vhost/virtio_net.c
> @@ -2914,10 +2914,12 @@ desc_to_mbuf(struct virtio_net *dev, struct vhost_virtqueue *vq,
>  			 * in a contiguous virtual area.
>  			 */
>  			copy_vnet_hdr_from_desc(&tmp_hdr, buf_vec);
> -			hdr = &tmp_hdr;
>  		} else {
> -			hdr = (struct virtio_net_hdr *)((uintptr_t)buf_vec[0].buf_addr);
> +			rte_memcpy((void *)(uintptr_t)&tmp_hdr,
> +				(void *)(uintptr_t)buf_vec[0].buf_addr,
> +				sizeof(struct virtio_net_hdr));
>  		}
> +		hdr = &tmp_hdr;

Since this if block is just an optimization of the case where vnet header
is contiguous why not just always use copy_vnet_hdr_from_desc? and inline it?
  
Wangyunjian(wangyunjian,TongTu) Dec. 23, 2024, 2:45 a.m. UTC | #7
> -----Original Message-----
> From: Stephen Hemminger [mailto:stephen@networkplumber.org]
> Sent: Saturday, December 21, 2024 12:36 AM
> To: Wangyunjian(wangyunjian,TongTu) <wangyunjian@huawei.com>
> Cc: dev@dpdk.org; maxime.coquelin@redhat.com; chenbox@nvidia.com;
> Lilijun (Jerry) <jerry.lilijun@huawei.com>; xiawei (H) <xiawei40@huawei.com>;
> wangzengyuan <wangzengyuan@huawei.com>; stable@dpdk.org
> Subject: Re: [PATCH 1/1] vhost: fix a double fetch when dequeue offloading
> 
> On Thu, 19 Dec 2024 14:38:28 +0800
> Yunjian Wang <wangyunjian@huawei.com> wrote:
> 
> > diff --git a/lib/vhost/virtio_net.c b/lib/vhost/virtio_net.c
> > index 69901ab3b5..5c40ae7069 100644
> > --- a/lib/vhost/virtio_net.c
> > +++ b/lib/vhost/virtio_net.c
> > @@ -2914,10 +2914,12 @@ desc_to_mbuf(struct virtio_net *dev, struct
> vhost_virtqueue *vq,
> >  			 * in a contiguous virtual area.
> >  			 */
> >  			copy_vnet_hdr_from_desc(&tmp_hdr, buf_vec);
> > -			hdr = &tmp_hdr;
> >  		} else {
> > -			hdr = (struct virtio_net_hdr *)((uintptr_t)buf_vec[0].buf_addr);
> > +			rte_memcpy((void *)(uintptr_t)&tmp_hdr,
> > +				(void *)(uintptr_t)buf_vec[0].buf_addr,
> > +				sizeof(struct virtio_net_hdr));
> >  		}
> > +		hdr = &tmp_hdr;
> 
> Since this if block is just an optimization of the case where vnet header
> is contiguous why not just always use copy_vnet_hdr_from_desc? and inline it?

I also considered using the copy_vnet_hdr_from_desc function directly.
However, in most cases, the vnet header is continuous, and reusing
copy_vnet_hdr_from_desc results in additional operations.

Thanks,
Yunjian
  

Patch

diff --git a/lib/vhost/virtio_net.c b/lib/vhost/virtio_net.c
index 69901ab3b5..5c40ae7069 100644
--- a/lib/vhost/virtio_net.c
+++ b/lib/vhost/virtio_net.c
@@ -2914,10 +2914,12 @@  desc_to_mbuf(struct virtio_net *dev, struct vhost_virtqueue *vq,
 			 * in a contiguous virtual area.
 			 */
 			copy_vnet_hdr_from_desc(&tmp_hdr, buf_vec);
-			hdr = &tmp_hdr;
 		} else {
-			hdr = (struct virtio_net_hdr *)((uintptr_t)buf_vec[0].buf_addr);
+			rte_memcpy((void *)(uintptr_t)&tmp_hdr,
+				(void *)(uintptr_t)buf_vec[0].buf_addr,
+				sizeof(struct virtio_net_hdr));
 		}
+		hdr = &tmp_hdr;
 	}
 
 	for (vec_idx = 0; vec_idx < nr_vec; vec_idx++) {
@@ -3363,7 +3365,7 @@  virtio_dev_tx_batch_packed(struct virtio_net *dev,
 {
 	uint16_t avail_idx = vq->last_avail_idx;
 	uint32_t buf_offset = sizeof(struct virtio_net_hdr_mrg_rxbuf);
-	struct virtio_net_hdr *hdr;
+	struct virtio_net_hdr hdr;
 	uintptr_t desc_addrs[PACKED_BATCH_SIZE];
 	uint16_t ids[PACKED_BATCH_SIZE];
 	uint16_t i;
@@ -3382,8 +3384,9 @@  virtio_dev_tx_batch_packed(struct virtio_net *dev,
 
 	if (virtio_net_with_host_offload(dev)) {
 		vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) {
-			hdr = (struct virtio_net_hdr *)(desc_addrs[i]);
-			vhost_dequeue_offload(dev, hdr, pkts[i], legacy_ol_flags);
+			rte_memcpy((void *)(uintptr_t)&hdr,
+				(void *)(uintptr_t)desc_addrs[i], sizeof(struct virtio_net_hdr));
+			vhost_dequeue_offload(dev, &hdr, pkts[i], legacy_ol_flags);
 		}
 	}