* [PATCH net-next v2 0/2] tun: trim per-packet work on the write path @ 2026-11-24 23:59 Antonio Quartulli 2026-11-24 23:59 ` [PATCH net-next v2 1/2] tun: don't disable BHs for the XDP program check of every packet Antonio Quartulli 2026-11-24 23:59 ` [PATCH net-next v2 2/2] tun: skip the virtio-net header parsing without IFF_VNET_HDR Antonio Quartulli 0 siblings, 2 replies; 4+ messages in thread From: Antonio Quartulli @ 2026-11-24 23:59 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 that writes every packet to a tun device showed tun doing per-packet work its packets do not need: 1. a BH-disabled RCU section, only to check for tun's XDP program, which only TAP devices can have 2. the parsing of an all-zero virtio-net header without IFF_VNET_HDR Together they cut the instructions per 1400-byte UDP packet written to a TUN device by 3.2% when an iptables raw-table rule drops it on ingress, and by 1.9% when it is forwarded to a dummy device. Counted under qemu -icount on x86_64, with ERMS/FSRM hidden so that user copies are not counted per byte; the counts include the syscall, so tun's own share of the saving is larger. The tun selftests pass. v2: - Drop the patch that stopped probing the transport header: tc ematches and generic XDP rely on it (Sashiko) - Rename the XDP check variable, which shadowed another one (Sashiko) - Patch 2: adapt to the has_eth argument tun_vnet_hdr_tnl_to_skb() gained in 73e08d50eb13 - Rebase on net-next and re-measure v1: https://lore.kernel.org/netdev/20261112150612.3866546-1-antonio@mandelbit.com/ Antonio Quartulli (2): 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 | 31 ++++++++++++++++++++----------- 1 file changed, 20 insertions(+), 11 deletions(-) base-commit: a4c450594f5d1dc0fc4daacfb95d40554d99dead -- 2.56.1 ^ permalink raw reply [flat|nested] 4+ messages in thread
* [PATCH net-next v2 1/2] tun: don't disable BHs for the XDP program check of every packet 2026-11-24 23:59 [PATCH net-next v2 0/2] tun: trim per-packet work on the write path Antonio Quartulli @ 2026-11-24 23:59 ` Antonio Quartulli 2026-11-24 23:59 ` [PATCH net-next v2 2/2] tun: skip the virtio-net header parsing without IFF_VNET_HDR Antonio Quartulli 1 sibling, 0 replies; 4+ messages in thread From: Antonio Quartulli @ 2026-11-24 23:59 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 tun's XDP program is attached, which only TAP devices can have. tun_build_skb() takes the RCU read lock for the same NULL test. Test the pointer with rcu_access_pointer() instead, and enter the BH-disabled RCU section in tun_get_user() only with a program attached. A program attached concurrently could already miss the packets in flight. 1.2% fewer instructions per 1400-byte UDP packet written and dropped at the raw table, 0.7% fewer when forwarded (qemu -icount, x86_64). 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 76a5ac2359b9..408bb0b40348 100644 --- a/drivers/net/tun.c +++ b/drivers/net/tun.c @@ -1709,14 +1709,13 @@ static struct sk_buff *tun_build_skb(struct tun_struct *tun, size_t copied; int pad = TUN_RX_PAD; int metasize = 0; + bool has_xdp; int err = 0; - rcu_read_lock(); - xdp_prog = rcu_dereference(tun->xdp_prog); - if (xdp_prog) + has_xdp = rcu_access_pointer(tun->xdp_prog); + if (has_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))) @@ -1733,7 +1732,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 || !has_xdp) { *skb_xdp = 1; return __tun_build_skb(tfile, alloc_frag, buf, buflen, len, pad, metasize); @@ -1972,7 +1971,10 @@ static ssize_t tun_get_user(struct tun_struct *tun, struct tun_file *tfile, skb_probe_transport_header(skb); skb_record_rx_queue(skb, tfile->queue_index); - if (skb_xdp) { + /* Don't disable BHs and enter an RCU section for every packet only + * to find that no program is attached, the common case. + */ + if (skb_xdp && rcu_access_pointer(tun->xdp_prog)) { struct bpf_prog *xdp_prog; int ret; -- 2.56.1 ^ permalink raw reply related [flat|nested] 4+ messages in thread
* [PATCH net-next v2 2/2] tun: skip the virtio-net header parsing without IFF_VNET_HDR 2026-11-24 23:59 [PATCH net-next v2 0/2] tun: trim per-packet work on the write path Antonio Quartulli 2026-11-24 23:59 ` [PATCH net-next v2 1/2] tun: don't disable BHs for the XDP program check of every packet Antonio Quartulli @ 2026-11-24 23:59 ` Antonio Quartulli 2026-11-26 18:48 ` willemb 1 sibling, 1 reply; 4+ messages in thread From: Antonio Quartulli @ 2026-11-24 23:59 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. The parser is not inlined, so that costs two nested calls per packet. Reset the MAC header directly, as tap_get_user() already does. 2.0% fewer instructions per 1400-byte UDP packet written and dropped at the raw table, 1.2% fewer when forwarded (qemu -icount, x86_64). Signed-off-by: Antonio Quartulli <antonio@mandelbit.com> --- drivers/net/tun.c | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/drivers/net/tun.c b/drivers/net/tun.c index 408bb0b40348..79aaadd9a2aa 100644 --- a/drivers/net/tun.c +++ b/drivers/net/tun.c @@ -1951,11 +1951,18 @@ 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, - (tun->flags & TUN_TYPE_MASK) == IFF_TAP)) { - atomic_long_inc(&tun->rx_frame_errors); - err = -EINVAL; - goto free_skb; + if (tun->flags & IFF_VNET_HDR) { + bool has_eth = (tun->flags & TUN_TYPE_MASK) == IFF_TAP; + + if (tun_vnet_hdr_tnl_to_skb(tun->flags, features, skb, &hdr, + has_eth)) { + atomic_long_inc(&tun->rx_frame_errors); + err = -EINVAL; + goto free_skb; + } + } else { + /* a zero hdr would only reset the MAC header */ + skb_reset_mac_header(skb); } if ((tun->flags & TUN_TYPE_MASK) == IFF_TAP) -- 2.56.1 ^ permalink raw reply related [flat|nested] 4+ messages in thread
* Re: [PATCH net-next v2 2/2] tun: skip the virtio-net header parsing without IFF_VNET_HDR 2026-11-24 23:59 ` [PATCH net-next v2 2/2] tun: skip the virtio-net header parsing without IFF_VNET_HDR Antonio Quartulli @ 2026-11-26 18:48 ` willemb 0 siblings, 0 replies; 4+ messages in thread From: willemb @ 2026-11-26 18:48 UTC (permalink / raw) To: Antonio Quartulli, willemdebruijn.kernel, jasowangio Cc: Antonio Quartulli, andrew+netdev, davem, edumazet, kuba, pabeni, ast, daniel, hawk, john.fastabend, sdf, netdev, bpf Antonio Quartulli wrote: > 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. The > parser is not inlined, so that costs two nested calls per packet. Reset > the MAC header directly, as tap_get_user() already does. > 2.0% fewer instructions per 1400-byte UDP packet written and dropped at > the raw table, 1.2% fewer when forwarded (qemu -icount, x86_64). > Signed-off-by: Antonio Quartulli <antonio@mandelbit.com> > --- > drivers/net/tun.c | 17 ++++++++++++----- > 1 file changed, 12 insertions(+), 5 deletions(-) > diff --git a/drivers/net/tun.c b/drivers/net/tun.c > index 408bb0b40348..79aaadd9a2aa 100644 > --- a/drivers/net/tun.c > +++ b/drivers/net/tun.c > @@ -1951,11 +1951,18 @@ 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, > - (tun->flags & TUN_TYPE_MASK) == IFF_TAP)) { > - atomic_long_inc(&tun->rx_frame_errors); > - err = -EINVAL; > - goto free_skb; > + if (tun->flags & IFF_VNET_HDR) { Checking tun->flags & IFF_VNET_HDR a second time here is not tied to the earlier check at line 1832 and introduces a narrow TOCTOU race with tun_set_iff() snapshot `unsigned int flags = READ_ONCE(tun->flags);` once at the start of tun_get_user() > + bool has_eth = (tun->flags & TUN_TYPE_MASK) == IFF_TAP; > + > + if (tun_vnet_hdr_tnl_to_skb(tun->flags, features, skb, &hdr, > + has_eth)) { > + atomic_long_inc(&tun->rx_frame_errors); > + err = -EINVAL; > + goto free_skb; > + } > + } else { > + /* a zero hdr would only reset the MAC header */ > + skb_reset_mac_header(skb); > } > if ((tun->flags & TUN_TYPE_MASK) == IFF_TAP) > -- > 2.56.1 ^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2026-11-26 18:49 UTC | newest] Thread overview: 4+ messages (download: mbox.gz follow: Atom feed -- links below jump to the message on this page -- 2026-11-24 23:59 [PATCH net-next v2 0/2] tun: trim per-packet work on the write path Antonio Quartulli 2026-11-24 23:59 ` [PATCH net-next v2 1/2] tun: don't disable BHs for the XDP program check of every packet Antonio Quartulli 2026-11-24 23:59 ` [PATCH net-next v2 2/2] tun: skip the virtio-net header parsing without IFF_VNET_HDR Antonio Quartulli 2026-11-26 18:48 ` willemb
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox