* [PATCH net-next 1/2] tun: remove unnecessary tun_xdp_hdr structure
@ 2025-06-12 8:32 Jason Wang
2025-06-12 8:32 ` [PATCH net-next 2/2] vhost-net: reduce one userspace copy when building XDP buff Jason Wang
` (2 more replies)
0 siblings, 3 replies; 7+ messages in thread
From: Jason Wang @ 2025-06-12 8:32 UTC (permalink / raw)
To: mst, jasowang
Cc: eperezma, kvm, virtualization, netdev, willemdebruijn.kernel,
davem, andrew+netdev, edumazet, kuba, pabeni
With f95f0f95cfb7("net, xdp: Introduce xdp_init_buff utility routine"),
buffer length could be stored as frame size so there's no need to have
a dedicated tun_xdp_hdr structure. We can simply store virtio net
header instead.
Signed-off-by: Jason Wang <jasowang@redhat.com>
---
drivers/net/tap.c | 5 ++---
drivers/net/tun.c | 5 ++---
drivers/vhost/net.c | 8 ++------
include/linux/if_tun.h | 5 -----
4 files changed, 6 insertions(+), 17 deletions(-)
diff --git a/drivers/net/tap.c b/drivers/net/tap.c
index bdf0788d8e66..d82eb7276a8b 100644
--- a/drivers/net/tap.c
+++ b/drivers/net/tap.c
@@ -1044,9 +1044,8 @@ static const struct file_operations tap_fops = {
static int tap_get_user_xdp(struct tap_queue *q, struct xdp_buff *xdp)
{
- struct tun_xdp_hdr *hdr = xdp->data_hard_start;
- struct virtio_net_hdr *gso = &hdr->gso;
- int buflen = hdr->buflen;
+ struct virtio_net_hdr *gso = xdp->data_hard_start;
+ int buflen = xdp->frame_sz;
int vnet_hdr_len = 0;
struct tap_dev *tap;
struct sk_buff *skb;
diff --git a/drivers/net/tun.c b/drivers/net/tun.c
index 1207196cbbed..90055947a54b 100644
--- a/drivers/net/tun.c
+++ b/drivers/net/tun.c
@@ -2356,13 +2356,12 @@ static int tun_xdp_one(struct tun_struct *tun,
struct tun_page *tpage)
{
unsigned int datasize = xdp->data_end - xdp->data;
- struct tun_xdp_hdr *hdr = xdp->data_hard_start;
- struct virtio_net_hdr *gso = &hdr->gso;
+ struct virtio_net_hdr *gso = xdp->data_hard_start;
struct bpf_prog *xdp_prog;
struct sk_buff *skb = NULL;
struct sk_buff_head *queue;
u32 rxhash = 0, act;
- int buflen = hdr->buflen;
+ int buflen = xdp->frame_sz;
int metasize = 0;
int ret = 0;
bool skb_xdp = false;
diff --git a/drivers/vhost/net.c b/drivers/vhost/net.c
index 7cbfc7d718b3..777eb6193985 100644
--- a/drivers/vhost/net.c
+++ b/drivers/vhost/net.c
@@ -668,7 +668,6 @@ static int vhost_net_build_xdp(struct vhost_net_virtqueue *nvq,
struct socket *sock = vhost_vq_get_backend(vq);
struct virtio_net_hdr *gso;
struct xdp_buff *xdp = &nvq->xdp[nvq->batched_xdp];
- struct tun_xdp_hdr *hdr;
size_t len = iov_iter_count(from);
int headroom = vhost_sock_xdp(sock) ? XDP_PACKET_HEADROOM : 0;
int buflen = SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
@@ -691,15 +690,13 @@ static int vhost_net_build_xdp(struct vhost_net_virtqueue *nvq,
if (unlikely(!buf))
return -ENOMEM;
- copied = copy_from_iter(buf + offsetof(struct tun_xdp_hdr, gso),
- sock_hlen, from);
+ copied = copy_from_iter(buf, sock_hlen, from);
if (copied != sock_hlen) {
ret = -EFAULT;
goto err;
}
- hdr = buf;
- gso = &hdr->gso;
+ gso = buf;
if (!sock_hlen)
memset(buf, 0, pad);
@@ -727,7 +724,6 @@ static int vhost_net_build_xdp(struct vhost_net_virtqueue *nvq,
xdp_init_buff(xdp, buflen, NULL);
xdp_prepare_buff(xdp, buf, pad, len, true);
- hdr->buflen = buflen;
++nvq->batched_xdp;
diff --git a/include/linux/if_tun.h b/include/linux/if_tun.h
index 043d442994b0..80166eb62f41 100644
--- a/include/linux/if_tun.h
+++ b/include/linux/if_tun.h
@@ -19,11 +19,6 @@ struct tun_msg_ctl {
void *ptr;
};
-struct tun_xdp_hdr {
- int buflen;
- struct virtio_net_hdr gso;
-};
-
#if defined(CONFIG_TUN) || defined(CONFIG_TUN_MODULE)
struct socket *tun_get_socket(struct file *);
struct ptr_ring *tun_get_tx_ring(struct file *file);
--
2.34.1
^ permalink raw reply related [flat|nested] 7+ messages in thread
* [PATCH net-next 2/2] vhost-net: reduce one userspace copy when building XDP buff
2025-06-12 8:32 [PATCH net-next 1/2] tun: remove unnecessary tun_xdp_hdr structure Jason Wang
@ 2025-06-12 8:32 ` Jason Wang
2025-06-13 2:15 ` Willem de Bruijn
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
2 siblings, 1 reply; 7+ messages in thread
From: Jason Wang @ 2025-06-12 8:32 UTC (permalink / raw)
To: mst, jasowang
Cc: eperezma, kvm, virtualization, netdev, willemdebruijn.kernel,
davem, andrew+netdev, edumazet, kuba, pabeni
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>
---
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);
xdp_init_buff(xdp, buflen, NULL);
xdp_prepare_buff(xdp, buf, pad, len, true);
--
2.34.1
^ permalink raw reply related [flat|nested] 7+ messages in thread
* Re: [PATCH net-next 1/2] tun: remove unnecessary tun_xdp_hdr structure
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:03 ` Willem de Bruijn
2025-06-24 0:58 ` Jason Wang
2 siblings, 0 replies; 7+ messages in thread
From: Willem de Bruijn @ 2025-06-13 2:03 UTC (permalink / raw)
To: Jason Wang, mst, jasowang
Cc: eperezma, kvm, virtualization, netdev, willemdebruijn.kernel,
davem, andrew+netdev, edumazet, kuba, pabeni
Jason Wang wrote:
> With f95f0f95cfb7("net, xdp: Introduce xdp_init_buff utility routine"),
> buffer length could be stored as frame size so there's no need to have
> a dedicated tun_xdp_hdr structure. We can simply store virtio net
> header instead.
>
> Signed-off-by: Jason Wang <jasowang@redhat.com>
Acked-by: Willem de Bruijn <willemb@google.com>
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [PATCH net-next 2/2] vhost-net: reduce one userspace copy when building XDP buff
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
2025-06-16 3:01 ` Jason Wang
0 siblings, 1 reply; 7+ messages in thread
From: Willem de Bruijn @ 2025-06-13 2:15 UTC (permalink / raw)
To: Jason Wang, mst, jasowang
Cc: eperezma, kvm, virtualization, netdev, willemdebruijn.kernel,
davem, andrew+netdev, edumazet, kuba, pabeni
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
>
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [PATCH net-next 2/2] vhost-net: reduce one userspace copy when building XDP buff
2025-06-13 2:15 ` Willem de Bruijn
@ 2025-06-16 3:01 ` Jason Wang
2025-06-24 14:05 ` Paolo Abeni
0 siblings, 1 reply; 7+ messages in thread
From: Jason Wang @ 2025-06-16 3:01 UTC (permalink / raw)
To: Willem de Bruijn
Cc: mst, eperezma, kvm, virtualization, netdev, davem, andrew+netdev,
edumazet, kuba, pabeni
On Fri, Jun 13, 2025 at 10:16 AM Willem de Bruijn
<willemdebruijn.kernel@gmail.com> wrote:
>
> 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.
We had this:
int pad = SKB_DATA_ALIGN(VHOST_NET_RX_PAD + headroom + nvq->sock_hlen);
int sock_hlen = nvq->sock_hlen;
So pad - sock_len is guaranteed to be greater than zero.
If this is not obvious, I can add a comment in the next version.
Thanks
>
> > xdp_init_buff(xdp, buflen, NULL);
> > xdp_prepare_buff(xdp, buf, pad, len, true);
> > --
> > 2.34.1
> >
>
>
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [PATCH net-next 1/2] tun: remove unnecessary tun_xdp_hdr structure
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:03 ` [PATCH net-next 1/2] tun: remove unnecessary tun_xdp_hdr structure Willem de Bruijn
@ 2025-06-24 0:58 ` Jason Wang
2 siblings, 0 replies; 7+ messages in thread
From: Jason Wang @ 2025-06-24 0:58 UTC (permalink / raw)
To: mst, kuba, pabeni
Cc: eperezma, kvm, virtualization, netdev, willemdebruijn.kernel,
jasowang, andrew+netdev, edumazet, davem
On Thu, Jun 12, 2025 at 4:32 PM Jason Wang <jasowang@redhat.com> wrote:
>
> With f95f0f95cfb7("net, xdp: Introduce xdp_init_buff utility routine"),
> buffer length could be stored as frame size so there's no need to have
> a dedicated tun_xdp_hdr structure. We can simply store virtio net
> header instead.
>
> Signed-off-by: Jason Wang <jasowang@redhat.com>
> ---
Hello maintainers:
Are we ok with this series?
Thanks
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [PATCH net-next 2/2] vhost-net: reduce one userspace copy when building XDP buff
2025-06-16 3:01 ` Jason Wang
@ 2025-06-24 14:05 ` Paolo Abeni
0 siblings, 0 replies; 7+ messages in thread
From: Paolo Abeni @ 2025-06-24 14:05 UTC (permalink / raw)
To: Jason Wang, Willem de Bruijn
Cc: mst, eperezma, kvm, virtualization, netdev, davem, andrew+netdev,
edumazet, kuba
On 6/16/25 5:01 AM, Jason Wang wrote:
> On Fri, Jun 13, 2025 at 10:16 AM Willem de Bruijn
> <willemdebruijn.kernel@gmail.com> wrote:
>>
>> 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.
>
> We had this:
>
> int pad = SKB_DATA_ALIGN(VHOST_NET_RX_PAD + headroom + nvq->sock_hlen);
> int sock_hlen = nvq->sock_hlen;
>
> So pad - sock_len is guaranteed to be greater than zero.
>
> If this is not obvious, I can add a comment in the next version.
The relevant initializations are not visible in the patch itself, so I
think either a comment in the code or in the commit message would be useful.
Thanks,
Paolo
^ permalink raw reply [flat|nested] 7+ messages in thread
end of thread, other threads:[~2025-06-24 14:05 UTC | newest]
Thread overview: 7+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
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
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
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).