Netdev List
 help / color / mirror / Atom feed
* [PATCH net-next 0/3] tun: trim per-packet work on the write path
@ 2026-11-12 15:06 Antonio Quartulli
  2026-11-12 15:06 ` [PATCH net-next 1/3] tun: don't probe the transport header of packets written by userspace Antonio Quartulli
                   ` (2 more replies)
  0 siblings, 3 replies; 6+ messages in thread
From: Antonio Quartulli @ 2026-11-12 15:06 UTC (permalink / raw)
  To: willemdebruijn.kernel, jasowangio
  Cc: Antonio Quartulli, andrew+netdev, davem, edumazet, kuba, pabeni,
	ast, daniel, hawk, john.fastabend, sdf, netdev, bpf

Profiling a link-bonding daemon on an embedded router (Cortex-A7), which
injects every packet with a write() to a tun device, showed tun doing
per-packet work its packets do not need:

1. a flow dissector run, only to set the transport header
2. a BH-disabled RCU section, only to check for an XDP program, which
   only TAP devices can have
3. the parsing of an all-zero virtio-net header without IFF_VNET_HDR

Per 1400-byte packet written and dropped at the raw table, instruction
counts (qemu -icount) drop by 6.5% for 1 (Cortex-A7), 2.3% for 2 and
1.6% for 3 (x86). The tun selftests pass.

Antonio Quartulli (3):
  tun: don't probe the transport header of packets written by userspace
  tun: don't disable BHs for the XDP program check of every packet
  tun: skip the virtio-net header parsing without IFF_VNET_HDR

 drivers/net/tun.c | 20 ++++++++++++--------
 1 file changed, 12 insertions(+), 8 deletions(-)

-- 
2.56.0


^ permalink raw reply	[flat|nested] 6+ messages in thread

* [PATCH net-next 1/3] tun: don't probe the transport header of packets written by userspace
  2026-11-12 15:06 [PATCH net-next 0/3] tun: trim per-packet work on the write path Antonio Quartulli
@ 2026-11-12 15:06 ` Antonio Quartulli
  2026-11-16 15:25   ` netdev-bot+sashiko
  2026-11-12 15:06 ` [PATCH net-next 2/3] tun: don't disable BHs for the XDP program check of every packet Antonio Quartulli
  2026-11-12 15:06 ` [PATCH net-next 3/3] tun: skip the virtio-net header parsing without IFF_VNET_HDR Antonio Quartulli
  2 siblings, 1 reply; 6+ messages in thread
From: Antonio Quartulli @ 2026-11-12 15:06 UTC (permalink / raw)
  To: willemdebruijn.kernel, jasowangio
  Cc: Antonio Quartulli, andrew+netdev, davem, edumazet, kuba, pabeni,
	ast, daniel, hawk, john.fastabend, sdf, netdev, bpf

tun_get_user() runs the flow dissector on every written packet only to
set the transport header. Packets that need it early already get it:
skb_partial_csum_set() for NEEDS_CSUM, __virtio_net_hdr_to_skb() for
GSO. For the rest, the IPv4/IPv6 receive paths set it, as for packets
from NICs.

Drop the probe: ~200 fewer instructions per 1400-byte packet, -6.5%
when dropped at the raw table, -2.4% through conntrack and NAT
(emulated Cortex-A7).

Signed-off-by: Antonio Quartulli <antonio@mandelbit.com>
---
 drivers/net/tun.c | 1 -
 1 file changed, 1 deletion(-)

diff --git a/drivers/net/tun.c b/drivers/net/tun.c
index 8920c797acfd..9fc60a73fb96 100644
--- a/drivers/net/tun.c
+++ b/drivers/net/tun.c
@@ -1960,7 +1960,6 @@ static ssize_t tun_get_user(struct tun_struct *tun, struct tun_file *tfile,
 		net_zcopy_complete(NULL, msg_control, false);
 
 	skb_reset_network_header(skb);
-	skb_probe_transport_header(skb);
 	skb_record_rx_queue(skb, tfile->queue_index);
 
 	if (skb_xdp) {
-- 
2.56.0


^ permalink raw reply related	[flat|nested] 6+ messages in thread

* [PATCH net-next 2/3] tun: don't disable BHs for the XDP program check of every packet
  2026-11-12 15:06 [PATCH net-next 0/3] tun: trim per-packet work on the write path Antonio Quartulli
  2026-11-12 15:06 ` [PATCH net-next 1/3] tun: don't probe the transport header of packets written by userspace Antonio Quartulli
@ 2026-11-12 15:06 ` Antonio Quartulli
  2026-11-16 15:25   ` netdev-bot+sashiko
  2026-11-12 15:06 ` [PATCH net-next 3/3] tun: skip the virtio-net header parsing without IFF_VNET_HDR Antonio Quartulli
  2 siblings, 1 reply; 6+ messages in thread
From: Antonio Quartulli @ 2026-11-12 15:06 UTC (permalink / raw)
  To: willemdebruijn.kernel, jasowangio
  Cc: Antonio Quartulli, andrew+netdev, davem, edumazet, kuba, pabeni,
	ast, daniel, hawk, john.fastabend, sdf, netdev, bpf

For every packet, tun_get_user() disables BHs and enters an RCU section
only to find out whether an XDP program is attached, which only TAP
devices can have.

Check with rcu_access_pointer() first, in tun_build_skb() too, and enter
the BH-disabled RCU section only with a program attached. A program
attached concurrently could already miss the packets in flight.

-2.3% instructions per 1400-byte packet dropped at the raw table, -3.0%
when written 64 per write() (emulated x86).

Signed-off-by: Antonio Quartulli <antonio@mandelbit.com>
---
 drivers/net/tun.c | 14 ++++++++------
 1 file changed, 8 insertions(+), 6 deletions(-)

diff --git a/drivers/net/tun.c b/drivers/net/tun.c
index 9fc60a73fb96..395923651f31 100644
--- a/drivers/net/tun.c
+++ b/drivers/net/tun.c
@@ -1702,13 +1702,12 @@ static struct sk_buff *tun_build_skb(struct tun_struct *tun,
 	int pad = TUN_RX_PAD;
 	int metasize = 0;
 	int err = 0;
+	bool xdp;
 
-	rcu_read_lock();
-	xdp_prog = rcu_dereference(tun->xdp_prog);
-	if (xdp_prog)
+	xdp = rcu_access_pointer(tun->xdp_prog);
+	if (xdp)
 		pad += XDP_PACKET_HEADROOM;
 	buflen += SKB_DATA_ALIGN(len + pad);
-	rcu_read_unlock();
 
 	alloc_frag->offset = ALIGN((u64)alloc_frag->offset, SMP_CACHE_BYTES);
 	if (unlikely(!skb_page_frag_refill(buflen, alloc_frag, GFP_KERNEL)))
@@ -1725,7 +1724,7 @@ static struct sk_buff *tun_build_skb(struct tun_struct *tun,
 	 * of xdp_prog above, this should be rare and for simplicity
 	 * we do XDP on skb in case the headroom is not enough.
 	 */
-	if (hdr->gso_type || !xdp_prog) {
+	if (hdr->gso_type || !xdp) {
 		*skb_xdp = 1;
 		return __tun_build_skb(tfile, alloc_frag, buf, buflen, len,
 				       pad, metasize);
@@ -1962,7 +1961,10 @@ static ssize_t tun_get_user(struct tun_struct *tun, struct tun_file *tfile,
 	skb_reset_network_header(skb);
 	skb_record_rx_queue(skb, tfile->queue_index);
 
-	if (skb_xdp) {
+	/* Only TAP devices can have an XDP program: don't disable BHs and
+	 * enter an RCU section for every packet just to find out it's unset.
+	 */
+	if (skb_xdp && rcu_access_pointer(tun->xdp_prog)) {
 		struct bpf_prog *xdp_prog;
 		int ret;
 
-- 
2.56.0


^ permalink raw reply related	[flat|nested] 6+ messages in thread

* [PATCH net-next 3/3] tun: skip the virtio-net header parsing without IFF_VNET_HDR
  2026-11-12 15:06 [PATCH net-next 0/3] tun: trim per-packet work on the write path Antonio Quartulli
  2026-11-12 15:06 ` [PATCH net-next 1/3] tun: don't probe the transport header of packets written by userspace Antonio Quartulli
  2026-11-12 15:06 ` [PATCH net-next 2/3] tun: don't disable BHs for the XDP program check of every packet Antonio Quartulli
@ 2026-11-12 15:06 ` Antonio Quartulli
  2 siblings, 0 replies; 6+ messages in thread
From: Antonio Quartulli @ 2026-11-12 15:06 UTC (permalink / raw)
  To: willemdebruijn.kernel, jasowangio
  Cc: Antonio Quartulli, andrew+netdev, davem, edumazet, kuba, pabeni,
	ast, daniel, hawk, john.fastabend, sdf, netdev, bpf

Without IFF_VNET_HDR, tun_get_user() still passes an all-zero header to
tun_vnet_hdr_tnl_to_skb(), which then only resets the MAC header. Do
just that.

-1.6% instructions per 1400-byte packet dropped at the raw table, -2.2%
when written 64 per write() (emulated x86).

Signed-off-by: Antonio Quartulli <antonio@mandelbit.com>
---
 drivers/net/tun.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)

diff --git a/drivers/net/tun.c b/drivers/net/tun.c
index 395923651f31..38f9da1ab462 100644
--- a/drivers/net/tun.c
+++ b/drivers/net/tun.c
@@ -1943,7 +1943,10 @@ static ssize_t tun_get_user(struct tun_struct *tun, struct tun_file *tfile,
 		break;
 	}
 
-	if (tun_vnet_hdr_tnl_to_skb(tun->flags, features, skb, &hdr)) {
+	if (!(tun->flags & IFF_VNET_HDR)) {
+		/* all a zeroed header would do */
+		skb_reset_mac_header(skb);
+	} else if (tun_vnet_hdr_tnl_to_skb(tun->flags, features, skb, &hdr)) {
 		atomic_long_inc(&tun->rx_frame_errors);
 		err = -EINVAL;
 		goto free_skb;
-- 
2.56.0


^ permalink raw reply related	[flat|nested] 6+ messages in thread

* Re: [PATCH net-next 1/3] tun: don't probe the transport header of packets written by userspace
  2026-11-12 15:06 ` [PATCH net-next 1/3] tun: don't probe the transport header of packets written by userspace Antonio Quartulli
@ 2026-11-16 15:25   ` netdev-bot+sashiko
  0 siblings, 0 replies; 6+ messages in thread
From: netdev-bot+sashiko @ 2026-11-16 15:25 UTC (permalink / raw)
  To: antonio
  Cc: willemdebruijn.kernel, jasowangio, andrew+netdev, davem, edumazet,
	kuba, pabeni, ast, daniel, hawk, john.fastabend, sdf, netdev, bpf

Thank you for your contribution! Sashiko AI review found 3 potential
issue(s) to consider.

Critical: 0 · High: 0 · Medium: 2 · Low: 1

If you believe none of the issues are legitimate, please reply to each
issue of Medium severity or higher and explain why the AI is wrong.
If any of them are legitimate and you plan to respin, please let patchwork
know by including "pw-bot: cr" as a separate line at the end of your reply
(one such reply per series is enough).

- [Medium] After the patch, a TAP packet that is neither GSO nor
  VIRTIO_NET_HDR_F_NEEDS_CSUM leaves tun_get_user() with…
- [Medium] On the default tun/tap receive path (no IFF_NAPI, so no GRO;
  delivery through tun_rx_batched()/netif_rx()), skb->transport_header…
- [Low] The patch removes skb_probe_transport_header() only from
  tun_get_user().

--

tun: don't probe the transport header of packets written by userspace

This commit removes the skb_probe_transport_header() call from
tun_get_user(). For NEEDS_CSUM and GSO packets it relies on
skb_partial_csum_set() and __virtio_net_hdr_to_skb() to set the transport
header, and for all other packets on the IPv4/IPv6 receive paths.

> diff --git a/drivers/net/tun.c b/drivers/net/tun.c
> index 8920c797acfd7..9fc60a73fb963 100644
> --- a/drivers/net/tun.c
> +++ b/drivers/net/tun.c
> @@ -1960,7 +1960,6 @@ static ssize_t tun_get_user(struct tun_struct *tun, struct tun_file *tfile,
>  		net_zcopy_complete(NULL, msg_control, false);
>  
>  	skb_reset_network_header(skb);
> -	skb_probe_transport_header(skb);
>  	skb_record_rx_queue(skb, tfile->queue_index);
>  
>  	if (skb_xdp) {

[Severity: Medium]
Without the probe, a TAP frame that is neither GSO nor
VIRTIO_NET_HDR_F_NEEDS_CSUM reaches tun's own do_xdp_generic() call with
transport_header still at the unset value (~0). This happens before
__netif_receive_skb_core() gets a chance to reset it.

Can the generic XDP copy path turn that sentinel into an offset that looks
valid but is wrong?

skb_xdp starts at 1 in tun_get_user(). It stays 1 for blocking, zerocopy,
large and IFF_NAPI_FRAGS writes, because tun_can_build_skb() rejects those.
These skbs come from tun_alloc_skb() with only tun->align of headroom, or
they are nonlinear, so they always take this branch:

net/core/dev.c:netif_receive_generic_xdp() {
    ...
	if (skb_cloned(skb) || skb_is_nonlinear(skb) ||
	    skb_headroom(skb) < XDP_PACKET_HEADROOM) {
		if (netif_skb_check_for_xdp(pskb, xdp_prog))
    ...
}

That branch reallocates through skb_pp_cow_data() or pskb_expand_head().
Both of them end up here:

net/core/skbuff.c:skb_headers_offset_update() {
    ...
	skb->transport_header += off;
	skb->network_header   += off;
	if (skb_mac_header_was_set(skb))
		skb->mac_header += off;
    ...
}

Only mac_header gets a was_set check. 0xffff + off wraps in the u16 to
off - 1, which points into the new uninitialized headroom just before
skb->data. Because of that, skb_transport_header_was_set() returns true,
and the fallback in __netif_receive_skb_core() is skipped:

	if (!skb_transport_header_was_set(skb))
		skb_reset_transport_header(skb);

Anything that runs before L3 would then see this offset: tc ingress,
bridge or OVS forwarding into another device's xmit path, and
XDP_TX/XDP_REDIRECT through generic_xdp_tx(). ip_rcv_core() and ipv6_rcv()
overwrite it, so locally delivered IP traffic is not affected.

Before this patch, skb_probe_transport_header() had already set a valid L4
offset for dissectable IPv4/IPv6 frames, so the reallocation shifted a
valid value.

The missing sentinel check in skb_headers_offset_update() is older than
this patch. However, this patch exposes all well-formed tun frames to it,
not only the ones the dissector rejected. The commit message says these
packets are handled "as for packets from NICs". For NICs in non-DEBUG_NET
builds, __netif_receive_skb_core() resets the transport header before
generic XDP runs, and this tun-local XDP path skips that reset.

[Severity: Medium]
On the default receive path (no IFF_NAPI, delivery through
tun_rx_batched() or netif_rx()), will tc ematches that use layer transport
on a tun/tap ingress still match?

For non-GSO, non-NEEDS_CSUM packets, nothing sets the L4 offset before
__netif_receive_skb_core() anymore. In non-DEBUG_NET builds that function
resets transport_header to the network header before tc ingress runs. With
CONFIG_DEBUG_NET the reset is compiled out and the header stays unset.

em_cmp, em_nbyte and em_text get their base pointer from here:

include/net/pkt_cls.h:tcf_get_base_ptr() {
    ...
		case TCF_LAYER_TRANSPORT:
			if (!skb_transport_header_was_set(skb))
				break;
			return skb_transport_header(skb);
    ...
}

Take an existing filter like "cmp(u16 at 2 layer transport eq 80)". It
used to match the destination port. Now it would compare bytes of the IP
header, or never match at all on DEBUG_NET builds.

ip_rcv_core() and ipv6_rcv() only set the transport header after tc
ingress and bridge handling, so the commit message's "the IPv4/IPv6
receive paths set it" doesn't cover these users.

tun_xdp_one() still probes, so the same filter now classifies a packet
differently depending on whether it came in through write()/sendmsg or
vhost-net batched TX.

[Severity: Low]
Should tun_xdp_one() get the same change? It still has the probe:

drivers/net/tun.c:tun_xdp_one() {
    ...
	skb->protocol = eth_type_trans(skb, tun->dev);
	skb_reset_network_header(skb);
	skb_probe_transport_header(skb);
	skb_record_rx_queue(skb, tfile->queue_index);
    ...
}

That path also carries packets written by userspace: vhost-net batched
guest TX goes through tun_sendmsg() with TUN_MSG_PTR. It builds the skb the
same way and delivers it through netif_receive_skb() or the NAPI queue.
The argument for dropping the probe applies to it unchanged.

The subject says the probe is dropped for "packets written by userspace",
but the vhost-net batched path keeps it. The two receive paths of one tun
device now also set up the transport header differently. If this is
intentional, could the commit message mention it and give the reason?

The tap.c call sites in tap_get_user() and tap_get_user_xdp() are a
different case. Those packets go to dev_queue_xmit() on the lower device,
so the receive-path reasoning doesn't apply to them.

-- 
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261112150612.3866546-1-antonio%40mandelbit.com

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH net-next 2/3] tun: don't disable BHs for the XDP program check of every packet
  2026-11-12 15:06 ` [PATCH net-next 2/3] tun: don't disable BHs for the XDP program check of every packet Antonio Quartulli
@ 2026-11-16 15:25   ` netdev-bot+sashiko
  0 siblings, 0 replies; 6+ messages in thread
From: netdev-bot+sashiko @ 2026-11-16 15:25 UTC (permalink / raw)
  To: antonio
  Cc: willemdebruijn.kernel, jasowangio, andrew+netdev, davem, edumazet,
	kuba, pabeni, ast, daniel, hawk, john.fastabend, sdf, netdev, bpf

Thank you for your contribution! Sashiko AI review found more severe
issue(s) in other patches of this series. This mail is only an FYI, in case
you want to address the nit picks below when you respin.

Low priority / nit picks for your consideration:

- [Low] In drivers/net/tun.c, the patch adds a function-scope `bool xdp`
  to tun_build_skb() (line 1705).

--

tun: don't disable BHs for the XDP program check of every packet

The patch removes the RCU/BH-disabled section that tun_get_user() and
tun_build_skb() entered for every packet just to check for an attached
XDP program. It checks first with rcu_access_pointer(), and only enters
the BH-disabled RCU section when a program is attached.

> diff --git a/drivers/net/tun.c b/drivers/net/tun.c
> index 9fc60a73fb963..395923651f312 100644
> --- a/drivers/net/tun.c
> +++ b/drivers/net/tun.c
> @@ -1702,13 +1702,12 @@ static struct sk_buff *tun_build_skb(struct tun_struct *tun,
>  	int pad = TUN_RX_PAD;
>  	int metasize = 0;
>  	int err = 0;
> +	bool xdp;
>  
> -	rcu_read_lock();
> -	xdp_prog = rcu_dereference(tun->xdp_prog);
> -	if (xdp_prog)
> +	xdp = rcu_access_pointer(tun->xdp_prog);
> +	if (xdp)

[Severity: Low]
This isn't a bug, but the new function-scope bool xdp in tun_build_skb()
is shadowed by an existing variable of the same name further down in the
same function:

drivers/net/tun.c:tun_build_skb() {
    ...
	xdp_prog = rcu_dereference(tun->xdp_prog);
	if (xdp_prog) {
		struct xdp_buff xdp;
		u32 act;

		xdp_init_buff(&xdp, buflen, &tfile->xdp_rxq);
		xdp_prepare_buff(&xdp, buf, pad, len, true);
    ...
}

The bool is only read before that block, so the behavior is correct.
However, the function now has two unrelated xdp identifiers of different
types, which gives a -Wshadow warning with W=2. It may also confuse
anyone editing the XDP block later.

Before this patch the gate reused xdp_prog, so there was no clash.

Could the bool get a different name, for example has_xdp or
xdp_attached?

The later patches in the series leave this unchanged. Both declarations
are still present at the end of the series (5b2d3d96af5d).

>  		pad += XDP_PACKET_HEADROOM;
>  	buflen += SKB_DATA_ALIGN(len + pad);
> -	rcu_read_unlock();

[ ... ]

-- 
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261112150612.3866546-1-antonio%40mandelbit.com

^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2026-11-16 15:25 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-12 15:06 [PATCH net-next 0/3] tun: trim per-packet work on the write path Antonio Quartulli
2026-11-12 15:06 ` [PATCH net-next 1/3] tun: don't probe the transport header of packets written by userspace Antonio Quartulli
2026-11-16 15:25   ` netdev-bot+sashiko
2026-11-12 15:06 ` [PATCH net-next 2/3] tun: don't disable BHs for the XDP program check of every packet Antonio Quartulli
2026-11-16 15:25   ` netdev-bot+sashiko
2026-11-12 15:06 ` [PATCH net-next 3/3] tun: skip the virtio-net header parsing without IFF_VNET_HDR Antonio Quartulli

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox