kvm.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Willem de Bruijn <willemdebruijn.kernel@gmail.com>
To: Jason Wang <jasowang@redhat.com>,  mst@redhat.com,  jasowang@redhat.com
Cc: eperezma@redhat.com,  kvm@vger.kernel.org,
	 virtualization@lists.linux.dev,  netdev@vger.kernel.org,
	 willemdebruijn.kernel@gmail.com,  davem@davemloft.net,
	 andrew+netdev@lunn.ch,  edumazet@google.com,  kuba@kernel.org,
	 pabeni@redhat.com
Subject: Re: [PATCH net-next 2/2] vhost-net: reduce one userspace copy when building XDP buff
Date: Thu, 12 Jun 2025 22:15:58 -0400	[thread overview]
Message-ID: <684b89de38e3_dcc452944e@willemb.c.googlers.com.notmuch> (raw)
In-Reply-To: <20250612083213.2704-2-jasowang@redhat.com>

Jason Wang wrote:
> We used to do twice copy_from_iter() to copy virtio-net and packet
> separately. This introduce overheads for userspace access hardening as
> well as SMAP (for x86 it's stac/clac). So this patch tries to use one
> copy_from_iter() to copy them once and move the virtio-net header
> afterwards to reduce overheads.
> 
> Testpmd + vhost_net shows 10% improvement from 5.45Mpps to 6.0Mpps.
> 
> Signed-off-by: Jason Wang <jasowang@redhat.com>

Acked-by: Willem de Bruijn <willemb@google.com>

> ---
>  drivers/vhost/net.c | 13 ++++---------
>  1 file changed, 4 insertions(+), 9 deletions(-)
> 
> diff --git a/drivers/vhost/net.c b/drivers/vhost/net.c
> index 777eb6193985..2845e0a473ea 100644
> --- a/drivers/vhost/net.c
> +++ b/drivers/vhost/net.c
> @@ -690,13 +690,13 @@ static int vhost_net_build_xdp(struct vhost_net_virtqueue *nvq,
>  	if (unlikely(!buf))
>  		return -ENOMEM;
>  
> -	copied = copy_from_iter(buf, sock_hlen, from);
> -	if (copied != sock_hlen) {
> +	copied = copy_from_iter(buf + pad - sock_hlen, len, from);
> +	if (copied != len) {
>  		ret = -EFAULT;
>  		goto err;
>  	}
>  
> -	gso = buf;
> +	gso = buf + pad - sock_hlen;
>  
>  	if (!sock_hlen)
>  		memset(buf, 0, pad);
> @@ -715,12 +715,7 @@ static int vhost_net_build_xdp(struct vhost_net_virtqueue *nvq,
>  		}
>  	}
>  
> -	len -= sock_hlen;
> -	copied = copy_from_iter(buf + pad, len, from);
> -	if (copied != len) {
> -		ret = -EFAULT;
> -		goto err;
> -	}
> +	memcpy(buf, buf + pad - sock_hlen, sock_hlen);

It's not trivial to see that the dst and src do not overlap, and does
does not need memmove.

Minimal pad that I can find is 32B and and maximal sock_hlen is 12B.

So this is safe. But not obviously so. Unfortunately, these offsets
are not all known at compile time, so a BUILD_BUG_ON is not possible.

>  	xdp_init_buff(xdp, buflen, NULL);
>  	xdp_prepare_buff(xdp, buf, pad, len, true);
> -- 
> 2.34.1
> 



  reply	other threads:[~2025-06-13  2:15 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-06-12  8:32 [PATCH net-next 1/2] tun: remove unnecessary tun_xdp_hdr structure Jason Wang
2025-06-12  8:32 ` [PATCH net-next 2/2] vhost-net: reduce one userspace copy when building XDP buff Jason Wang
2025-06-13  2:15   ` Willem de Bruijn [this message]
2025-06-16  3:01     ` Jason Wang
2025-06-24 14:05       ` Paolo Abeni
2025-06-13  2:03 ` [PATCH net-next 1/2] tun: remove unnecessary tun_xdp_hdr structure Willem de Bruijn
2025-06-24  0:58 ` Jason Wang

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=684b89de38e3_dcc452944e@willemb.c.googlers.com.notmuch \
    --to=willemdebruijn.kernel@gmail.com \
    --cc=andrew+netdev@lunn.ch \
    --cc=davem@davemloft.net \
    --cc=edumazet@google.com \
    --cc=eperezma@redhat.com \
    --cc=jasowang@redhat.com \
    --cc=kuba@kernel.org \
    --cc=kvm@vger.kernel.org \
    --cc=mst@redhat.com \
    --cc=netdev@vger.kernel.org \
    --cc=pabeni@redhat.com \
    --cc=virtualization@lists.linux.dev \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).