tun: bound the NAPI receive queue
Subsystems (6)
v6 cover tun: bound the NAPI receive queue
With IFF_NAPI, tun queues written packets for NAPI on an unbounded list, which an unprivileged user can grow until the system runs out of memory. Patch 1 bounds it by netdev_max_backlog; patch 2 adds a kselftest for the vhost-net paths into that queue.
v6:
- Complete the zerocopy buffer of a dropped packet with net_zcopy_put(), as a success, so that vhost-net does not fall back to copying (Willem)
- selftest: slim it down, and add a wrapper that loads vhost_net and also runs the zerocopy variant (Willem)
- Rebase on net
v5: https://lore.kernel.org/netdev/[email protected]/
- Take at most budget packets in the NAPI poll instead of keeping the leftovers on a separate queue (Willem)
- Keep only the lockless check, before allocating, also in tun_xdp_one() (Willem)
- Say that an OOM is still possible with large packets and many queues, if hard to reach in practice (Sashiko)
- selftest: little-endian ring, buffers returned in any order, lower netdev_max_backlog only after setup and restore it first, any non-zero experimental_zcopytx, .gitignore entry (Sashiko)
- Rebase on net, which conflicted in selftests/net/config
v4: https://lore.kernel.org/netdev/[email protected]/
- Keep the poll's leftovers on a separate queue, like process_backlog():
v3 did not bound the queue when the poll ran on another CPU (Sashiko)
- Schedule NAPI when dropping: with netdev_max_backlog below NAPI_POLL_WEIGHT, MSG_MORE senders (vhost-net) stalled the queue (Sashiko)
- Use goto free_skb for the locked drop (Willem)
- Add a vhost-net kselftest (patch 2); the tun kselftest passes (Willem)
- Cc vhost, kselftest and XDP maintainers and the Fixes author/acker
v3: https://lore.kernel.org/netdev/[email protected]/
- Also bound the sendmsg() paths, which share the queue: zerocopy buffers are completed when dropped early, and the batched XDP buffers of tun_xdp_one() are covered too (Willem)
- Drop the IFF_NAPI_FRAGS exclusion (Willem)
- Recheck under the queue lock before queueing (Willem)
- Compare with > like enqueue_to_backlog() (Willem)
- Keep the includes sorted (Willem)
- Add the reproducer and a testing statement to the commit message
v2: https://lore.kernel.org/netdev/[email protected]/
- Cc netdev and the maintainers
Testing, in an x86-64 VM with 1 and 2 vCPUs, on net 7e1392a42ea2:
- Reproducer from patch 1, 15 seconds: memory use stays flat, no OOM; forwarding rate unchanged (113755 packets in 3 s without the patch, 113990 with it).
- tun_vhost (patch 2), through the wrapper and with vhost_net experimental_zcopytx=0 and =1: passes with patch 1. Without it, every variant fails; removing the tun_xdp_one() check fails the batch variant, removing the zerocopy completion fails the zerocopy one, and removing the NAPI scheduling on drop fails the copy and zerocopy ones.
- tun kselftest: pass:91 fail:0 xfail:12, as without the patch.
Notes:
- Affected since 943170998b20 (v4.15).
- Mitigation: disallow unprivileged user namespaces or restrict access to /dev/net/tun.
Antonio Quartulli (2):
tun: bound the NAPI receive queue
selftests: net: add a vhost-net test for the tun NAPI queue bound
drivers/net/tun.c | 55 +++++- tools/testing/selftests/net/.gitignore | 1 + tools/testing/selftests/net/Makefile | 2 + tools/testing/selftests/net/config | 1 + tools/testing/selftests/net/tun_vhost.c | 222 +++++++++++++++++++++++ tools/testing/selftests/net/tun_vhost.sh | 23 +++ 6 files changed, 296 insertions(+), 8 deletions(-) create mode 100644 tools/testing/selftests/net/tun_vhost.c create mode 100755 tools/testing/selftests/net/tun_vhost.sh
base-commit: ead6fa9a0ab194f02a2f36d634b193e9c5f9a1c6
Signature · 1 line
2.56.0
PATCH v6 1/2 tun: bound the NAPI receive queue
With IFF_NAPI, tun_get_user() queues every written packet for NAPI on an unbounded list. When the poll runs asynchronously (threaded NAPI) and is slower than the writer, the queue grows until the system runs out of memory. An unprivileged user can do this from its own user and network namespaces, and the queued skbs are not charged to its memcg:
in a test VM, a uid 1000 process limited to 128 MB caused a global OOM in ~6 seconds. The socket send buffer cannot bound this: tun sets it to INT_MAX, and the build_skb() and XDP batch paths do not check it.
Drop packets once more than netdev_max_backlog of them are queued, like enqueue_to_backlog() does, and count them as rx_dropped, before allocating and copying them. This covers write(), sendmsg() and the batches of XDP buffers vhost-net passes to tun_sendmsg(), which share the queue. Zerocopy buffers of dropped packets are completed. The check is lockless, so concurrent writers can each still add one packet.
The NAPI poll moved the whole queue out, processed up to its budget and put the rest back: while it ran, the queue looked empty to writers, so with the poll on another CPU the backlog could still grow without limit. Take at most budget packets instead, so that the queue never looks emptier than it is.
Dropping because the queue is full also schedules NAPI. vhost-net sends with MSG_MORE while it has more packets, and tun_get_user() schedules NAPI only after the last one or once more than NAPI_POLL_WEIGHT are queued. With a netdev_max_backlog below that, the queue would not drain until vhost-net ran out of packets; with zerocopy, vhost-net never would, as it stops sending when too many queued buffers are pending.
Reproducer, as an unprivileged user in a new user and network namespace, without kernel modifications or fault injection:
ip tuntap add t0 mode tun; ip addr add 10.11.0.1/24 dev t0 ip link add d0 type dummy; ip addr add 10.20.0.1/24 dev d0 ip link set t0 up; ip link set d0 up; sysctl net.ipv4.ip_forward=1 install ~500 iptables FORWARD rules and a MASQUERADE rule on d0 open t0 with IFF_TUN | IFF_NO_PI | IFF_NAPI
echo 1 > /sys/class/net/t0/threaded
write() 1400-byte IPv4 packets for 10.20.0.2 in a loop
Tested in an x86-64 VM with 1 and 2 vCPUs: with the reproducer running for 15 seconds, with 1400 to 65000-byte packets, memory use stays flat and the forwarding rate is unchanged; the tun kselftest passes, as it does without the patch; and a vhost-net test driving the batch, copy and zerocopy paths (next patch) passes, while it fails without the patch.
Still open: the limit counts packets rather than bytes and applies per queue, so with large packets and many queues (IFF_MULTI_QUEUE works in a user namespace too) the queued skbs, which are not charged to the writer's memcg, can still add up to an OOM. This is hard to reach in
practice: with large packets the writer's copy is slower than the NAPI poll, so the queue does not fill. With 64 KB writes the reproducer never reached the limit and memory use stayed flat.
--- drivers/net/tun.c | 55 ++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 47 insertions(+), 8 deletions(-)
drivers/net/tun.c +47 -8
@@ -58,6 +58,7 @@#include <linux/nsproxy.h> #include <linux/virtio_net.h> #include <linux/rcupdate.h> +#include <net/hotdata.h> #include <net/net_namespace.h> #include <net/netns/generic.h> #include <net/rtnetlink.h>
@@ -227,6 +228,16 @@ struct veth {static void tun_flow_init (struct tun_struct *tun);static void tun_flow_uninit (struct tun_struct *tun); +/* In IFF_NAPI mode written packets wait in the tfile's sk_write_queue for the +* NAPI poll, which may run later in a kthread or ksoftirqd. Bound that queue +* like the per-CPU backlog of netif_rx(). +*/ +static bool tun_napi_queue_full (const struct sk_buff_head *queue) +{ +return skb_queue_len_lockless (queue) > +READ_ONCE (net_hotdata.max_backlog); +} +static int tun_napi_receive (struct napi_struct *napi,int budget) { struct tun_file *tfile = container_of(napi, struct tun_file, napi);
@@ -237,21 +248,24 @@ static int tun_napi_receive(struct napi_struct *napi, int budget)__skb_queue_head_init (&process_queue); +/* Take at most budget skbs, so that the queue never looks emptier to +* writers than it is. +*/ spin_lock (&queue->lock); -skb_queue_splice_tail_init (queue, &process_queue); +if (skb_queue_len (queue) <= budget) { +skb_queue_splice_tail_init (queue, &process_queue); + }else { +while (skb_queue_len (&process_queue) < budget && + (skb =__skb_dequeue (queue))) +__skb_queue_tail (&process_queue, skb); + }spin_unlock (&queue->lock); -while (received < budget && (skb =__skb_dequeue (&process_queue))) { +while ((skb =__skb_dequeue (&process_queue))) {napi_gro_receive (napi, skb); ++received; } -if (!skb_queue_empty (&process_queue)) { -spin_lock (&queue->lock); -skb_queue_splice (&process_queue, queue); -spin_unlock (&queue->lock); - } -return received; }
@@ -1862,6 +1876,20 @@ static ssize_t tun_get_user(struct tun_struct *tun, struct tun_file *tfile, zerocopy = true; } +/* NAPI queue full: drop before allocating, and schedule NAPI, which a +* MSG_MORE sender may not have done yet. Lockless: concurrent writers +* can each still add one. +*/ +if (tfile->napi_enabled && +unlikely (tun_napi_queue_full (&tfile->sk.sk_write_queue))) { +local_bh_disable (); +napi_schedule (&tfile->napi); +local_bh_enable (); +net_zcopy_put (msg_control); +dev_core_stats_rx_dropped_inc (tun->dev); +return total_len; + } +if (!frags &&tun_can_build_skb (tun, tfile, len, noblock, zerocopy)) {/* For the packet that is not easy to be processed * (e.g gso or jumbo packet), we will do it at after
@@ -2568,6 +2596,17 @@ static int tun_xdp_one(struct tun_struct *tun,return -EIO ; } +/* NAPI queue full: drop, and schedule NAPI so that it drains even if +* nothing in this batch gets queued +*/ +if (tfile->napi_enabled && +unlikely (tun_napi_queue_full (&tfile->sk.sk_write_queue))) { +napi_schedule (&tfile->napi); +put_page (virt_to_head_page (xdp->data)); +dev_core_stats_rx_dropped_inc (tun->dev); +return 0 ; + } + xdp_prog =rcu_dereference (tun->xdp_prog);if (xdp_prog) {if (gso->gso_type) {
Signature · 1 line
2.56.0
[email protected]
AI reviewSashiko has reviewed this patch and found no issues.
Quoted 19 lines
With
IFF_NAPI,tun_get_user()queues every written packet for NAPI on an unbounded list. When the poll runs asynchronously (threaded NAPI) and is slower than the writer, the queue grows until the system runs out of memory. An unprivileged user can do this from its own user and network namespaces, and the queued skbs are not charged to its memcg:
in a test VM, a uid 1000 process limited to 128 MB caused a global OOM in ~6 seconds. The socket send buffer cannot bound this: tun sets it toINT_MAX, and thebuild_skb()and XDP batch paths do not check it.Drop packets once more than
netdev_max_backlogof them are queued, likeenqueue_to_backlog()does, and count them asrx_dropped, before allocating and copying them. This coverswrite(),sendmsg()and the batches of XDP buffers vhost-net passes totun_sendmsg(), which share the queue. Zerocopy buffers of dropped packets are completed. The check is lockless, so concurrent writers can each still add one packet. [ ... ]
poll, so the queue does not fill. With 64 KB writes the reproducer never reached the limit and memory use stayed flat.
Quoted 2 lines
Fixes 943170998b20 ("tun: enable NAPI for TUN/TAP driver")Signed-off-by Antonio Quartulli <[email protected]>
Sashiko has reviewed this patch and found no issues.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
[email protected]
AI reviewThank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider.
Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider.
Critical: 0 · High: 1 · Medium: 1 · 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).
- [High] The new bound does not close the OOM it is meant to fix.
- [Medium] The queue-full drop is checked before the code decides whether the packet will be queued at all, so it also drops traffic that never…
- [Low] The new comment in tun_napi_receive() (drivers/net/tun.c:251-253) says the poll takes 'at most budget skbs, so that the queue never…
--
tun: bound the NAPI receive queue
This patch puts a limit on the IFF_NAPI sk_write_queue in tun. Once more than netdev_max_backlog packets are queued, tun_get_user() and tun_xdp_one() drop new packets and schedule NAPI. The poll now takes at most budget skbs at a time, instead of splicing out the whole queue.
Quoted 4 lines
drivers/net/tun.c +0
-0
[ ... ]
drivers/net/tun.c struct veth { · line 239
Quoted 13 lines
drivers/net/tun.c +9
-0@@ -227,6 +228,16 @@ struct veth {static void tun_flow_init (struct tun_struct *tun);static void tun_flow_uninit (struct tun_struct *tun); +/* In IFF_NAPI mode written packets wait in the tfile's sk_write_queue for the +* NAPI poll, which may run later in a kthread or ksoftirqd. Bound that queue +* like the per-CPU backlog of netif_rx(). +*/ +static bool tun_napi_queue_full (const struct sk_buff_head *queue) +{ +return skb_queue_len_lockless (queue) > +READ_ONCE (net_hotdata.max_backlog); +}
[Severity: High]
Does this limit prevent the global OOM described in the commit message? The only limit is a per-tfile packet count against the global net_hotdata.max_backlog. The queued skbs are still not charged to the writer's memcg.
An unprivileged user in its own user and network namespace can multiply this cap. tun_set_iff() only needs CAP_NET_ADMIN in the user namespace to create a multi-queue device:
drivers/net/tun.c:tun_set_iff() {
...
int queues = ifr->ifr_flags & IFF_MULTI_QUEUE ?
MAX_TAP_QUEUES : 1;
if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
return -EPERM;
...
}That allows up to 256 queues per device, plus more devices and more namespaces. netdev_max_backlog itself can only be changed from init_net.
Each queue can hold about max_backlog + budget + one skb per concurrent writer. The reproducer sends 1400-byte TUN packets, with a truesize of about 2.3 KB. That works out to about 2.3 MB per queue and about 600 MB for one 256-queue device. A few such devices would reach several GB of uncharged memory, whatever the memcg limit is.
The commit message says:
"This is hard to reach in practice: with large packets the writer's copy is slower than the NAPI poll, so the queue does not fill."
That covers large packets on a single queue. Does it also cover small packets spread over many queues, which is the reproducer's own traffic pattern?
The writer also controls the per-packet poll cost through the number of iptables rules. So larger packets can fill each queue too, at about 64 MB per queue with 64 KB packets.
Would this need byte or memcg accounting of the queued skbs, or a per-owner limit, instead of a per-queue packet count?
Quoted 10 lines
drivers/net/tun.c +3
-0static int tun_napi_receive (struct napi_struct *napi,int budget) { struct tun_file *tfile = container_of(napi, struct tun_file, napi);@@ -237,21 +248,24 @@ static int tun_napi_receive(struct napi_struct *napi, int budget)__skb_queue_head_init (&process_queue); +/* Take at most budget skbs, so that the queue never looks emptier to +* writers than it is. +*/
drivers/net/tun.c static int tun_napi_receive(struct napi_struct *napi, int budget) · line 262
Quoted 10 lines
drivers/net/tun.c +8
-1+spin_lock (&queue->lock); -skb_queue_splice_tail_init (queue, &process_queue); +if (skb_queue_len (queue) <= budget) { +skb_queue_splice_tail_init (queue, &process_queue); + }else { +while (skb_queue_len (&process_queue) < budget && + (skb =__skb_dequeue (queue))) +__skb_queue_tail (&process_queue, skb); + }spin_unlock (&queue->lock);
[Severity: Low]
This isn't a bug, but is this comment accurate? The poll still moves up to budget skbs from sk_write_queue into the local process_queue. It then drops the lock and calls napi_gro_receive() on them.
tun_napi_queue_full() only reads the qlen of sk_write_queue. So while the poll runs, writers can see a queue up to budget skbs shorter than the real backlog.
Would "at most budget emptier" be a better description? The effective limit would then be about max_backlog + budget + one skb per concurrent writer.
[ ... ]
Quoted 19 lines
drivers/net/tun.c +14
-0@@ -1862,6 +1876,20 @@ static ssize_t tun_get_user(struct tun_struct *tun, struct tun_file *tfile, zerocopy = true; } +/* NAPI queue full: drop before allocating, and schedule NAPI, which a +* MSG_MORE sender may not have done yet. Lockless: concurrent writers +* can each still add one. +*/ +if (tfile->napi_enabled && +unlikely (tun_napi_queue_full (&tfile->sk.sk_write_queue))) { +local_bh_disable (); +napi_schedule (&tfile->napi); +local_bh_enable (); +net_zcopy_put (msg_control); +dev_core_stats_rx_dropped_inc (tun->dev); +return total_len; + } +if (!frags &&tun_can_build_skb (tun, tfile, len, noblock, zerocopy)) {
[ ... ]
Quoted 6 lines
drivers/net/tun.c +2
-0@@ -2568,6 +2596,17 @@ static int tun_xdp_one(struct tun_struct *tun,return -EIO ; } +/* NAPI queue full: drop, and schedule NAPI so that it drains even if +* nothing in this batch gets queued
drivers/net/tun.c static int tun_xdp_one(struct tun_struct *tun, · line 2610
Quoted 10 lines
drivers/net/tun.c +9
-0+*/ +if (tfile->napi_enabled && +unlikely (tun_napi_queue_full (&tfile->sk.sk_write_queue))) { +napi_schedule (&tfile->napi); +put_page (virt_to_head_page (xdp->data)); +dev_core_stats_rx_dropped_inc (tun->dev); +return 0 ; + } + xdp_prog =rcu_dereference (tun->xdp_prog);
[Severity: Medium]
Could these checks drop traffic that would never be queued on sk_write_queue?
In tun_xdp_one() the check runs before the attached XDP program. Frames the program would XDP_REDIRECT or XDP_TX return early and never reach __skb_queue_tail(). They are now dropped and counted as rx_dropped whenever the NAPI queue is over the limit.
The check in tun_get_user() runs before tun_build_skb(). That is where native XDP runs for the TAP build_skb path, and non-PASS verdicts return there without building or queueing an skb:
drivers/net/tun.c:tun_build_skb() {
...
if (err == XDP_REDIRECT)
xdp_do_flush();
if (err != XDP_PASS)
goto out;
...
}It also runs before the generic do_xdp_generic() call.
With IFF_NAPI_FRAGS, tun_get_user() passes the skb directly to napi_gro_frags() and never touches sk_write_queue. The check is gated only on tfile->napi_enabled, which is also set in frags mode.
tun_xdp_one() still queues into sk_write_queue whenever napi_enabled is set. So small packets batched by vhost-net can fill the queue, and then the larger packets that go through tun_get_user() and the frags path are dropped as well.
Take a vhost-net/XDP setup where most traffic is redirected and only some goes to the local stack. Would the redirected traffic start dropping as soon as the local stack path backs up?
The commit message says this works "like enqueue_to_backlog()", but enqueue_to_backlog() only drops skbs it would actually enqueue. Could the check move to the places where the skb is actually queued? If not, could the commit message mention this side effect?
Signature · 1 line
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261113212237.1742984-1-antonio%40mandelbit.com
PATCH v6 2/2 selftests: net: add a vhost-net test for the tun NAPI queue bound
In reply to Antonio QuartulliFeed a tap device with IFF_NAPI from vhost-net with netdev_max_backlog set to 8, in the three ways vhost-net passes packets to tun: batches of XDP buffers, single packets with MSG_MORE and zerocopy buffers. Check that excess packets are dropped and counted, that the queue keeps draining and that every buffer comes back.
The wrapper loads vhost_net and, if it can reload it, also runs the zerocopy variant with experimental_zcopytx=1.
--- tools/testing/selftests/net/.gitignore | 1 + tools/testing/selftests/net/Makefile | 2 + tools/testing/selftests/net/config | 1 + tools/testing/selftests/net/tun_vhost.c | 222 +++++++++++++++++++++++ tools/testing/selftests/net/tun_vhost.sh | 23 +++ 5 files changed, 249 insertions(+) create mode 100644 tools/testing/selftests/net/tun_vhost.c create mode 100755 tools/testing/selftests/net/tun_vhost.sh
tools/testing/selftests/net/.gitignore +1 -0
@@ -56,6 +56,7 @@ timestamping tls tools tun +tun_vhost txring_overwrite txtimestamp udp_splice_checksum
tools/testing/selftests/net/Makefile +2 -0
@@ -123,6 +123,7 @@ TEST_PROGS := \ test_vxlan_vnifiltering.sh \ tfo_passive.sh \ traceroute.sh \ + tun_vhost.sh \ txtimestamp.sh \ udpgro.sh \ udpgro_bench.sh \
@@ -177,6 +178,7 @@ TEST_GEN_FILES := \ tcp_mmap \ tfo \ timestamping \ + tun_vhost \ txring_overwrite \ txtimestamp \ udpgso \
tools/testing/selftests/net/config +1 -0
@@ -136,6 +136,7 @@ CONFIG_TRACEPOINTS=y CONFIG_TUN=y CONFIG_USER_NS=y CONFIG_VETH=y +CONFIG_VHOST_NET=m CONFIG_VLAN_8021Q=y CONFIG_VXLAN=m CONFIG_XFRM_INTERFACE=m
tools/testing/selftests/net/tun_vhost.c new file +222 -0
222 lines not on this page · raw message
tools/testing/selftests/net/tun_vhost.sh new file +23 -0
@@ -0,0 +1,23 @@ +#!/bin/bash +# SPDX-License-Identifier: GPL-2.0 +# +# Run tun_vhost with vhost_net loaded, and once more with zerocopy enabled +# if the module can be reloaded. + +ksft_skip=4 +param=/sys/module/vhost_net/parameters/experimental_zcopytx + +modprobe -q vhost_net +[ -e /dev/vhost-net ] || exit $ksft_skip + +./tun_vhost +ret=$? + +zc=$(cat "$param" 2>/dev/null) +if [ "$zc" = 0 ] && modprobe -q -r vhost_net 2>/dev/null; then + modprobe vhost_net experimental_zcopytx=1 && + { ./tun_vhost -v zerocopy || ret=1; } + modprobe -q -r vhost_net + modprobe vhost_net experimental_zcopytx="$zc" +fi +exit $ret
Signature · 1 line
2.56.0
[email protected]
AI reviewRe: [PATCH net v6 2/2] selftests: net: add a vhost-net test for the tun NAPI queue bound
Sashiko has reviewed this patch and found no issues.
Quoted 10 lines
Feed a tap device with
IFF_NAPIfrom vhost-net withnetdev_max_backlogset to 8, in the three ways vhost-net passes packets to tun: batches of XDP buffers, single packets withMSG_MOREand zerocopy buffers. Check that excess packets are dropped and counted, that the queue keeps draining and that every buffer comes back.The wrapper loads
vhost_netand, if it can reload it, also runs the zerocopy variant with experimental_zcopytx=1.Signed-off-by Antonio Quartulli <[email protected]>
Sashiko has reviewed this patch and found no issues.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
[email protected]
AI reviewRe: [PATCH net v6 2/2] selftests: net: add a vhost-net test for the tun NAPI queue bound
In reply to Antonio QuartulliThank you for your contribution! Sashiko AI review found 6 potential issue(s) to consider.
Thank you for your contribution! Sashiko AI review found 6 potential issue(s) to consider.
Critical: 0 · High: 0 · Medium: 4 · Low: 2
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] In tools/testing/selftests/net/tun_vhost.c, TEST_F(tun_vhost, napi_queue_bounded) opens /proc/sys/net/core/netdev_max_backlog at…
- [Medium] napi_queue_bounded() writes "8" to the system-wide /proc/sys/net/core/netdev_max_backlog (line 198) and restores it only…
- [Medium] tools/testing/selftests/net/tun_vhost.sh does not reliably restore vhost_net's module parameter.
- [Medium] In the zerocopy variant, when ASSERT_LT(ms, 5000) fires because buffers were not returned, __bail() runs FIXTURE_TEARDOWN,…
- [Low] The test is named after the tun NAPI queue bound, but it cannot catch a regression in the poll half of that fix.
- [Low] The comment in tun_vhost.c FIXTURE_SETUP (lines 126-128) says the IPv4 frame for another host is one "which the stack drops without…
--
selftests: net: add a vhost-net test for the tun NAPI queue bound
This adds a kselftest that feeds an IFF_NAPI tap device from vhost-net with netdev_max_backlog lowered to 8, covering XDP batches, MSG_MORE single packets and zerocopy buffers. It checks that excess packets are dropped and counted, that the queue keeps draining and that every buffer comes back. A wrapper script loads vhost_net and, if possible, reloads it with experimental_zcopytx=1 for the zerocopy variant.
Quoted 6 lines
tools/testing/selftests/net/tun_vhost.c new file +0
-0@@ -0,0 +1,222 @@
[ ... ]
Quoted 2 lines
tools/testing/selftests/net/tun_vhost.c new file +2
-0+FIXTURE_SETUP (tun_vhost) +{
[ ... ]
tools/testing/selftests/net/tun_vhost.c line 124
Quoted 5 lines
tools/testing/selftests/net/tun_vhost.c new file +5
-0+ fd =socket (AF_INET ,SOCK_DGRAM ,0 ); +ASSERT_EQ (ioctl (fd,SIOCGIFFLAGS , &ifr),0 ); + ifr.ifr_flags |=IFF_UP ; +ASSERT_EQ (ioctl (fd,SIOCSIFFLAGS , &ifr),0 ); +close (fd);
[Severity: Low]
Would this test notice a regression in the poll side of the tun NAPI queue bound, the "dequeue at most budget" change in tun_napi_receive()?
The tap is brought up here, but threaded NAPI is never turned on through /sys/class/net/<ifname>/threaded. So the NAPI poll runs inline from the vhost worker's local_bh_enable(), serialised with the writer, and the queue is already empty when the next packet arrives.
Turning on threaded NAPI would not be enough either. netdev_max_backlog=8 is below the NAPI budget of 64. The old behaviour (splice the whole queue, process budget, splice the rest back) would process everything it took out and never put anything back.
The checks at the end of napi_queue_bounded():
EXPECT_GT(dropped, 0);
EXPECT_GE(rx, PKTS / 16);
EXPECT_EQ(rx + dropped, PKTS);would all still pass if that tun_napi_receive() change were reverted. The drop threshold and the schedule-NAPI-on-drop changes do look covered.
tools/testing/selftests/net/tun_vhost.c line 128
Quoted 4 lines
tools/testing/selftests/net/tun_vhost.c new file +4
-0+ +/* each buffer: vnet header and an IPv4 frame for another host, which +* the stack drops without counting it +*/
[Severity: Low]
Is this comment accurate? The destination MAC 02:00:00:00:00:02 makes eth_type_trans() mark the frame PACKET_OTHERHOST, and ip_rcv_core() counts it:
net/ipv4/ip_input.c:ip_rcv_core() {
...
if (skb->pkt_type == PACKET_OTHERHOST) {
dev_core_stats_rx_otherhost_dropped_inc(skb->dev);
drop_reason = SKB_DROP_REASON_OTHERHOST;
goto drop;
}
...
}dev_get_stats() reports this as rx_otherhost_dropped. The drop is counted, just not in rx_dropped, which is the only counter the test reads.
[ ... ]
tools/testing/selftests/net/tun_vhost.c line 179
Quoted 7 lines
tools/testing/selftests/net/tun_vhost.c new file +7
-0+FIXTURE_TEARDOWN (tun_vhost) +{ +/* restore first: closing vhost blocks if buffers are not returned */ +if (self->bl_fd >=0 && +pwrite (self->bl_fd, self->bl,strlen (self->bl),0 ) <0 ) +TH_LOG ("cannot restore netdev_max_backlog to %s" , self->bl); +close (self->vhost);
[Severity: Medium]
In the zerocopy variant, can this close() leave the test process stuck in D state for good?
When ASSERT_LT(ms, 5000) fires because buffers were not returned, the teardown runs while skbs in the tap's NAPI queue still hold zerocopy ubufs. The last fput of the vhost fd then goes through:
vhost_net_release()
vhost_net_flush()
vhost_net_ubuf_put_and_wait()
wait_event(ubufs->wait, !atomic_read(&ubufs->refcount));That wait is uninterruptible and has no timeout. Nothing in the teardown schedules the tap's NAPI, and restoring netdev_max_backlog does not do it either. The tun file can only be released, which purges the queue, after sockfd_put(tx_sock). In vhost_net_release() that call comes after the flush:
vhost_net_stop(n, &tx_sock, &rx_sock); vhost_net_flush(n); ... if (tx_sock) sockfd_put(tx_sock);
One concrete case is a regression in the schedule-NAPI-on-drop part of the tun fix. The final non-MSG_MORE packet would be dropped without a napi_schedule(), and several zerocopy skbs would stay queued.
The harness SIGKILL on timeout cannot reap such a task. It keeps /dev/vhost-net and the tap open, keeps vhost_net from being unloaded (which also breaks the restore in tun_vhost.sh), and triggers hung task warnings.
Would kicking the queue before close(self->vhost) avoid this? For example, a write() to the tap fd without MSG_MORE would make tun_get_user() call napi_schedule().
Quoted 5 lines
tools/testing/selftests/net/tun_vhost.c new file +5
-0+close (self->tap); +} + +TEST_F(tun_vhost, napi_queue_bounded) +{
[ ... ]
tools/testing/selftests/net/tun_vhost.c line 194
Quoted 3 lines
tools/testing/selftests/net/tun_vhost.c new file +3
-0+/* lowered only here, so that a failed setup leaves it alone */ + self->bl_fd =open ("/proc/sys/net/core/netdev_max_backlog" ,O_RDWR ); +ASSERT_GE (self->bl_fd,0 );
[Severity: Medium]
What happens when this runs outside init_net? netdev_max_backlog is in net_core_table, which is only registered for init_net:
net/core/sysctl_net_core.c:sysctl_core_init() {
...
register_net_sysctl(&init_net, "net/core", net_core_table);
...
}The per-netns table does not include it. In any other network namespace the file is missing, so this open() fails with ENOENT.
run_kselftest.sh -n runs each test in a fresh netns through run_in_netns() in kselftest/runner.sh. FIXTURE_SETUP still passes there, because /dev/vhost-net, /dev/net/tun, TUNSETIFF and SIOCSIFFLAGS all work inside the namespace. In that mode the test reports FAIL rather than SKIP every time.
Should this SKIP when the sysctl is not present?
Quoted 8 lines
tools/testing/selftests/net/tun_vhost.c new file +8
-0+ n =pread (self->bl_fd, self->bl,sizeof (self->bl) -1 ,0 ); +ASSERT_GT (n,0 ); + self->bl[n] ='\0' ; +ASSERT_EQ (pwrite (self->bl_fd,"8" ,1 ,0 ),1 ); + +/* post the whole ring, wait for every buffer to come back, repeat */ +for (n =0 ; n <PKTS ; n +=RING ) { + idx +=RING ;
tools/testing/selftests/net/tun_vhost.c line 212
Quoted 10 lines
tools/testing/selftests/net/tun_vhost.c new file +10
-0+__atomic_store_n (&self->vr.avail->idx,htole16 (idx), + __ATOMIC_RELEASE); +ASSERT_EQ (eventfd_write (self->kick,1 ),0 ); +for (ms =0 ;le16toh (__atomic_load_n (&self->vr.used->idx, + __ATOMIC_ACQUIRE)) != idx; + ms++) { + ASSERT_LT(ms, 5000) TH_LOG("buffers not returned"); +usleep (1000 ); + } + }
[Severity: Medium]
Can netdev_max_backlog stay at 8 for the whole system if this loop runs past the harness timeout?
The only restore is in FIXTURE_TEARDOWN, which runs only in the forked test child. With FIXTURE_TEARDOWN, unlike FIXTURE_TEARDOWN_PARENT, _teardown_parent is false, so the parent-side teardown does nothing.
TEST_F uses the default 30 second timeout. On timeout, __wait_for_test() does:
kill(-(t->pid), SIGKILL);
so no cleanup runs in the child.
The 5000 x usleep(1000) bound resets for each of the PKTS / RING = 64 rings. The body can therefore run for about 320 seconds without any assertion firing. A drain that is slow but still making progress, for example on a debug or KASAN kernel, would hit the timeout.
tun_vhost.sh does not save or restore the value either. Later tests and workloads on the host would then run with netdev_max_backlog=8.
[ ... ]
Quoted 6 lines
tools/testing/selftests/net/tun_vhost.sh new file +0
-0@@ -0,0 +1,23 @@
[ ... ]
tools/testing/selftests/net/tun_vhost.sh line 22
Quoted 13 lines
tools/testing/selftests/net/tun_vhost.sh new file +13
-0+modprobe -q vhost_net +[ -e /dev/vhost-net ] || exit $ksft_skip + +./tun_vhost +ret=$? + +zc=$(cat "$param" 2>/dev/null) +if [ "$zc" = 0 ] && modprobe -q -r vhost_net 2>/dev/null; then + modprobe vhost_net experimental_zcopytx=1 && + { ./tun_vhost -v zerocopy || ret=1; } + modprobe -q -r vhost_net + modprobe vhost_net experimental_zcopytx="$zc" +fi
[Severity: Medium]
Can experimental_zcopytx still be 1 after this script exits?
The parameter is read-only at runtime:
drivers/vhost/net.c:module_param(experimental_zcopytx, int, 0444);
so only a reload can change it back. The result of the second modprobe -q -r is not checked. Removal fails if something still holds /dev/vhost-net open, such as a tun_vhost child stuck in vhost_net_release() as described above, or another vhost user that started in the meantime. In that case the next modprobe finds the module already loaded and returns success without changing anything.
The script also sets no EXIT or INT trap, so interrupting it during the zerocopy run skips the restore.
A separate point: the first modprobe -q vhost_net always loads the module, and nothing unloads it afterwards if it was not loaded before.
Quoted 1 line
+exit $ret
Signature · 1 line
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261113212237.1742984-1-antonio%40mandelbit.com
[email protected]
AI reviewThis is an automated message. This series looks like a fix, but its commit messages seem to be missing some information:
Hi!
This is an automated message. This series looks like a fix, but its commit messages seem to be missing some information:
- How the issue was discovered, e.g. hit in production, hit during development, syzbot report, manual code inspection, LLM or static analysis tool scan.
Please do not repost the series just to address the above. Instead, reply to this email with the missing information, so that reviewers can take it into account. If the series needs another revision for other reasons, please include the information in the commit messages then.
The evaluation is done by an LLM so it may be wrong, if you think that is the case please reply and explain.